Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <idlc/idlc.hxx>
21 : #include <idlc/errorhandler.hxx>
22 : #include <idlc/astscope.hxx>
23 : #include <idlc/astmodule.hxx>
24 : #include <idlc/astservice.hxx>
25 : #include <idlc/astconstants.hxx>
26 : #include <idlc/astexception.hxx>
27 : #include <idlc/astunion.hxx>
28 : #include <idlc/astenum.hxx>
29 : #include <idlc/astinterface.hxx>
30 : #include <idlc/astoperation.hxx>
31 : #include <idlc/astbasetype.hxx>
32 : #include "idlc/astdeclaration.hxx"
33 : #include "idlc/astparameter.hxx"
34 : #include "idlc/astsequence.hxx"
35 : #include "idlc/asttype.hxx"
36 : #include "idlc/asttypedef.hxx"
37 :
38 : #include <osl/diagnose.h>
39 : #include <osl/file.hxx>
40 : #include <osl/thread.h>
41 :
42 : using namespace ::rtl;
43 :
44 0 : AstDeclaration* SAL_CALL scopeAsDecl(AstScope* pScope)
45 : {
46 0 : if (pScope == NULL) return NULL;
47 :
48 0 : switch( pScope->getScopeNodeType() )
49 : {
50 : case NT_service:
51 : case NT_singleton:
52 0 : return (AstService*)(pScope);
53 : case NT_module:
54 : case NT_root:
55 0 : return (AstModule*)(pScope);
56 : case NT_constants:
57 0 : return (AstConstants*)(pScope);
58 : case NT_interface:
59 0 : return (AstInterface*)(pScope);
60 : case NT_operation:
61 0 : return (AstOperation*)(pScope);
62 : case NT_exception:
63 0 : return (AstException*)(pScope);
64 : case NT_union:
65 0 : return (AstUnion*)(pScope);
66 : case NT_struct:
67 0 : return (AstStruct*)(pScope);
68 : case NT_enum:
69 0 : return (AstEnum*)(pScope);
70 : default:
71 0 : return NULL;
72 : }
73 : }
74 :
75 0 : AstScope* SAL_CALL declAsScope(AstDeclaration* pDecl)
76 : {
77 0 : if (pDecl == NULL) return NULL;
78 :
79 0 : switch(pDecl->getNodeType())
80 : {
81 : case NT_interface:
82 0 : return (AstInterface*)(pDecl);
83 : case NT_service:
84 : case NT_singleton:
85 0 : return (AstService*)(pDecl);
86 : case NT_module:
87 : case NT_root:
88 0 : return (AstModule*)(pDecl);
89 : case NT_constants:
90 0 : return (AstConstants*)(pDecl);
91 : case NT_exception:
92 0 : return (AstException*)(pDecl);
93 : case NT_union:
94 0 : return (AstUnion*)(pDecl);
95 : case NT_struct:
96 0 : return (AstStruct*)(pDecl);
97 : case NT_enum:
98 0 : return (AstEnum*)(pDecl);
99 : case NT_operation:
100 0 : return (AstOperation*)(pDecl);
101 : default:
102 0 : return NULL;
103 : }
104 : }
105 :
106 0 : static void SAL_CALL predefineXInterface(AstModule* pRoot)
107 : {
108 : // define the modules com::sun::star::uno
109 0 : AstModule* pParentScope = pRoot;
110 0 : AstModule* pModule = new AstModule(OString("com"), pParentScope);
111 0 : pModule->setPredefined(true);
112 0 : pParentScope->addDeclaration(pModule);
113 0 : pParentScope = pModule;
114 0 : pModule = new AstModule(OString("sun"), pParentScope);
115 0 : pModule->setPredefined(true);
116 0 : pParentScope->addDeclaration(pModule);
117 0 : pParentScope = pModule;
118 0 : pModule = new AstModule(OString("star"), pParentScope);
119 0 : pModule->setPredefined(true);
120 0 : pParentScope->addDeclaration(pModule);
121 0 : pParentScope = pModule;
122 0 : pModule = new AstModule(OString("uno"), pParentScope);
123 0 : pModule->setPredefined(true);
124 0 : pParentScope->addDeclaration(pModule);
125 0 : pParentScope = pModule;
126 :
127 : // define XInterface
128 0 : AstInterface* pInterface = new AstInterface(OString("XInterface"), NULL, pParentScope);
129 0 : pInterface->setDefined();
130 0 : pInterface->setPredefined(true);
131 0 : pInterface->setPublished();
132 0 : pParentScope->addDeclaration(pInterface);
133 :
134 : // define XInterface::queryInterface
135 0 : AstOperation* pOp = new AstOperation((AstType*)(pRoot->lookupPrimitiveType(ET_any)),
136 0 : OString("queryInterface"), pInterface);
137 : AstParameter* pParam = new AstParameter(DIR_IN, false,
138 0 : (AstType*)(pRoot->lookupPrimitiveType(ET_type)),
139 0 : OString("aType"), pOp);
140 0 : pOp->addDeclaration(pParam);
141 0 : pInterface->addMember(pOp);
142 :
143 : // define XInterface::acquire
144 0 : pOp = new AstOperation((AstType*)(pRoot->lookupPrimitiveType(ET_void)),
145 0 : OString("acquire"), pInterface);
146 0 : pInterface->addMember(pOp);
147 :
148 : // define XInterface::release
149 0 : pOp = new AstOperation((AstType*)(pRoot->lookupPrimitiveType(ET_void)),
150 0 : OString("release"), pInterface);
151 0 : pInterface->addMember(pOp);
152 0 : }
153 :
154 0 : static void SAL_CALL initializePredefinedTypes(AstModule* pRoot)
155 : {
156 0 : AstBaseType* pPredefined = NULL;
157 0 : if ( pRoot )
158 : {
159 0 : pPredefined = new AstBaseType(ET_long, OString("long"), pRoot);
160 0 : pRoot->addDeclaration(pPredefined);
161 :
162 0 : pPredefined = new AstBaseType(ET_ulong, OString("unsigned long"), pRoot);
163 0 : pRoot->addDeclaration(pPredefined);
164 :
165 0 : pPredefined = new AstBaseType(ET_hyper, OString("hyper"), pRoot);
166 0 : pRoot->addDeclaration(pPredefined);
167 :
168 0 : pPredefined = new AstBaseType(ET_uhyper, OString("unsigned hyper"), pRoot);
169 0 : pRoot->addDeclaration(pPredefined);
170 :
171 0 : pPredefined = new AstBaseType(ET_short, OString("short"), pRoot);
172 0 : pRoot->addDeclaration(pPredefined);
173 :
174 0 : pPredefined = new AstBaseType(ET_ushort, OString("unsigned short"), pRoot);
175 0 : pRoot->addDeclaration(pPredefined);
176 :
177 0 : pPredefined = new AstBaseType(ET_float, OString("float"), pRoot);
178 0 : pRoot->addDeclaration(pPredefined);
179 :
180 0 : pPredefined = new AstBaseType(ET_double, OString("double"), pRoot);
181 0 : pRoot->addDeclaration(pPredefined);
182 :
183 0 : pPredefined = new AstBaseType(ET_char, OString("char"), pRoot);
184 0 : pRoot->addDeclaration(pPredefined);
185 :
186 0 : pPredefined = new AstBaseType(ET_byte, OString("byte"), pRoot);
187 0 : pRoot->addDeclaration(pPredefined);
188 :
189 0 : pPredefined = new AstBaseType(ET_any, OString("any"), pRoot);
190 0 : pRoot->addDeclaration(pPredefined);
191 :
192 0 : pPredefined = new AstBaseType(ET_string, OString("string"), pRoot);
193 0 : pRoot->addDeclaration(pPredefined);
194 :
195 0 : pPredefined = new AstBaseType(ET_type, OString("type"), pRoot);
196 0 : pRoot->addDeclaration(pPredefined);
197 :
198 0 : pPredefined = new AstBaseType(ET_boolean, OString("boolean"), pRoot);
199 0 : pRoot->addDeclaration(pPredefined);
200 :
201 0 : pPredefined = new AstBaseType(ET_void, OString("void"), pRoot);
202 0 : pRoot->addDeclaration(pPredefined);
203 : }
204 0 : }
205 :
206 0 : Idlc::Idlc(Options* pOptions)
207 : : m_pOptions(pOptions)
208 : , m_bIsDocValid(sal_False)
209 : , m_bIsInMainfile(sal_True)
210 : , m_published(false)
211 : , m_errorCount(0)
212 : , m_warningCount(0)
213 : , m_lineNumber(0)
214 : , m_offsetStart(0)
215 : , m_offsetEnd(0)
216 0 : , m_parseState(PS_NoState)
217 : {
218 0 : m_pScopes = new AstStack();
219 : // init root object after construction
220 0 : m_pRoot = NULL;
221 0 : m_pErrorHandler = new ErrorHandler();
222 0 : m_bGenerateDoc = m_pOptions->isValid("-C");
223 0 : }
224 :
225 0 : Idlc::~Idlc()
226 : {
227 0 : if (m_pRoot)
228 0 : delete m_pRoot;
229 0 : if (m_pScopes)
230 0 : delete m_pScopes;
231 0 : if (m_pErrorHandler)
232 0 : delete m_pErrorHandler;
233 0 : }
234 :
235 0 : void Idlc::init()
236 : {
237 0 : if ( m_pRoot )
238 0 : delete m_pRoot;
239 :
240 0 : m_pRoot = new AstModule(NT_root, OString(), NULL);
241 :
242 : // push the root node on the stack
243 0 : m_pScopes->push(m_pRoot);
244 0 : initializePredefinedTypes(m_pRoot);
245 0 : predefineXInterface(m_pRoot);
246 0 : }
247 :
248 0 : void Idlc::reset()
249 : {
250 0 : m_bIsDocValid = sal_False;
251 0 : m_bIsInMainfile = sal_True;
252 0 : m_published = false;
253 :
254 0 : m_errorCount = 0;
255 0 : m_warningCount = 0;
256 0 : m_lineNumber = 0;
257 0 : m_parseState = PS_NoState;
258 :
259 0 : m_fileName = OString();
260 0 : m_mainFileName = OString();
261 0 : m_realFileName = OString();
262 0 : m_documentation = OString();
263 :
264 0 : m_pScopes->clear();
265 0 : if ( m_pRoot)
266 0 : delete m_pRoot;
267 :
268 0 : m_pRoot = new AstModule(NT_root, OString(), NULL);
269 :
270 : // push the root node on the stack
271 0 : m_pScopes->push(m_pRoot);
272 0 : initializePredefinedTypes(m_pRoot);
273 :
274 0 : m_includes.clear();
275 0 : }
276 :
277 0 : OUString Idlc::processDocumentation()
278 : {
279 0 : OUString doc;
280 0 : if (m_bIsDocValid) {
281 0 : OString raw(getDocumentation());
282 0 : if (m_bGenerateDoc) {
283 0 : doc = OStringToOUString(raw, RTL_TEXTENCODING_UTF8);
284 0 : } else if (raw.indexOf("@deprecated") != -1) {
285 : //TODO: this check is somewhat crude
286 0 : doc = "@deprecated";
287 0 : }
288 : }
289 0 : return doc;
290 : }
291 :
292 0 : static void lcl_writeString(::osl::File & rFile, ::osl::FileBase::RC & o_rRC,
293 : ::rtl::OString const& rString)
294 : {
295 0 : sal_uInt64 nWritten(0);
296 0 : if (::osl::FileBase::E_None == o_rRC) {
297 0 : o_rRC = rFile.write(rString.getStr(), rString.getLength(), nWritten);
298 0 : if (static_cast<sal_uInt64>(rString.getLength()) != nWritten) {
299 0 : o_rRC = ::osl::FileBase::E_INVAL; //?
300 : }
301 : }
302 0 : }
303 :
304 : struct WriteDep
305 : {
306 : ::osl::File& m_rFile;
307 : ::osl::FileBase::RC & m_rRC;
308 0 : explicit WriteDep(::osl::File & rFile, ::osl::FileBase::RC & rRC)
309 0 : : m_rFile(rFile), m_rRC(rRC) { }
310 0 : void operator() (::rtl::OString const& rEntry)
311 : {
312 0 : lcl_writeString(m_rFile, m_rRC, " \\\n ");
313 0 : lcl_writeString(m_rFile, m_rRC, rEntry);
314 0 : }
315 : };
316 :
317 : // write a dummy target for one included file, so the incremental build does
318 : // not break with "No rule to make target" if the included file is removed
319 : struct WriteDummy
320 : {
321 : ::osl::File& m_rFile;
322 : ::osl::FileBase::RC & m_rRC;
323 0 : explicit WriteDummy(::osl::File & rFile, ::osl::FileBase::RC & rRC)
324 0 : : m_rFile(rFile), m_rRC(rRC) { }
325 0 : void operator() (::rtl::OString const& rEntry)
326 : {
327 0 : lcl_writeString(m_rFile, m_rRC, rEntry);
328 0 : lcl_writeString(m_rFile, m_rRC, ":\n\n");
329 0 : }
330 : };
331 :
332 : bool
333 0 : Idlc::dumpDeps(::rtl::OString const& rDepFile, ::rtl::OString const& rTarget)
334 : {
335 : ::osl::File depFile(
336 0 : ::rtl::OStringToOUString(rDepFile, osl_getThreadTextEncoding()));
337 : ::osl::FileBase::RC rc =
338 0 : depFile.open(osl_File_OpenFlag_Write | osl_File_OpenFlag_Create);
339 0 : if (::osl::FileBase::E_None != rc) {
340 0 : return false;
341 : }
342 0 : lcl_writeString(depFile, rc, rTarget);
343 0 : if (::osl::FileBase::E_None != rc) {
344 0 : return false;
345 : }
346 0 : lcl_writeString(depFile, rc, " :");
347 0 : if (::osl::FileBase::E_None != rc) {
348 0 : return false;
349 : }
350 0 : m_includes.erase(getRealFileName()); // eeek, that is a temp file...
351 : ::std::for_each(m_includes.begin(), m_includes.end(),
352 0 : WriteDep(depFile, rc));
353 0 : lcl_writeString(depFile, rc, "\n\n");
354 : ::std::for_each(m_includes.begin(), m_includes.end(),
355 0 : WriteDummy(depFile, rc));
356 0 : if (::osl::FileBase::E_None != rc) {
357 0 : return false;
358 : }
359 0 : rc = depFile.close();
360 0 : return ::osl::FileBase::E_None == rc;
361 : }
362 :
363 : static Idlc* pStaticIdlc = NULL;
364 :
365 0 : Idlc* SAL_CALL idlc()
366 : {
367 0 : return pStaticIdlc;
368 : }
369 :
370 0 : Idlc* SAL_CALL setIdlc(Options* pOptions)
371 : {
372 0 : if ( pStaticIdlc )
373 : {
374 0 : delete pStaticIdlc;
375 : }
376 0 : pStaticIdlc = new Idlc(pOptions);
377 0 : pStaticIdlc->init();
378 0 : return pStaticIdlc;
379 : }
380 :
381 0 : AstDeclaration const * resolveTypedefs(AstDeclaration const * type) {
382 0 : if (type != 0) {
383 0 : while (type->getNodeType() == NT_typedef) {
384 0 : type = static_cast< AstTypeDef const * >(type)->getBaseType();
385 : }
386 : }
387 0 : return type;
388 : }
389 :
390 0 : AstDeclaration const * deconstructAndResolveTypedefs(
391 : AstDeclaration const * type, sal_Int32 * rank)
392 : {
393 0 : *rank = 0;
394 0 : for (;;) {
395 0 : if (type == 0) {
396 0 : return 0;
397 : }
398 0 : switch (type->getNodeType()) {
399 : case NT_typedef:
400 0 : type = static_cast< AstTypeDef const * >(type)->getBaseType();
401 0 : break;
402 : case NT_sequence:
403 0 : ++(*rank);
404 0 : type = static_cast< AstSequence const * >(type)->getMemberType();
405 0 : break;
406 : default:
407 0 : return type;
408 : }
409 : }
410 : }
411 :
412 0 : AstInterface const * resolveInterfaceTypedefs(AstType const * type) {
413 0 : AstDeclaration const * decl = resolveTypedefs(type);
414 : OSL_ASSERT(decl->getNodeType() == NT_interface);
415 0 : return static_cast< AstInterface const * >(decl);
416 : }
417 :
418 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|