Branch data 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 : 2142019 : AstDeclaration* SAL_CALL scopeAsDecl(AstScope* pScope)
45 : : {
46 [ - + ]: 2142019 : if (pScope == NULL) return NULL;
47 : :
48 [ + + + + : 2142019 : switch( pScope->getScopeNodeType() )
+ + - + +
- ]
49 : : {
50 : : case NT_service:
51 : : case NT_singleton:
52 [ + - ]: 46096 : return (AstService*)(pScope);
53 : : case NT_module:
54 : : case NT_root:
55 [ + - ]: 1487796 : return (AstModule*)(pScope);
56 : : case NT_constants:
57 [ + - ]: 14757 : return (AstConstants*)(pScope);
58 : : case NT_interface:
59 [ + - ]: 248384 : return (AstInterface*)(pScope);
60 : : case NT_operation:
61 [ + - ]: 129528 : return (AstOperation*)(pScope);
62 : : case NT_exception:
63 [ + - ]: 16243 : return (AstException*)(pScope);
64 : : case NT_union:
65 [ # # ]: 0 : return (AstUnion*)(pScope);
66 : : case NT_struct:
67 [ + - ]: 120613 : return (AstStruct*)(pScope);
68 : : case NT_enum:
69 [ + - ]: 78602 : return (AstEnum*)(pScope);
70 : : default:
71 : 2142019 : return NULL;
72 : : }
73 : : }
74 : :
75 : 767633 : AstScope* SAL_CALL declAsScope(AstDeclaration* pDecl)
76 : : {
77 [ - + ]: 767633 : if (pDecl == NULL) return NULL;
78 : :
79 [ - - + + : 767633 : 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 [ + - ]: 766393 : return (AstModule*)(pDecl);
89 : : case NT_constants:
90 [ + - ]: 1240 : 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 : 767633 : return NULL;
103 : : }
104 : : }
105 : :
106 : 166 : static void SAL_CALL predefineXInterface(AstModule* pRoot)
107 : : {
108 : : // define the modules com::sun::star::uno
109 : 166 : AstModule* pParentScope = pRoot;
110 [ + - ][ + - ]: 166 : AstModule* pModule = new AstModule(OString("com"), pParentScope);
[ + - ]
111 : 166 : pModule->setPredefined(true);
112 : 166 : pParentScope->addDeclaration(pModule);
113 : 166 : pParentScope = pModule;
114 [ + - ][ + - ]: 166 : pModule = new AstModule(OString("sun"), pParentScope);
[ + - ]
115 : 166 : pModule->setPredefined(true);
116 : 166 : pParentScope->addDeclaration(pModule);
117 : 166 : pParentScope = pModule;
118 [ + - ][ + - ]: 166 : pModule = new AstModule(OString("star"), pParentScope);
[ + - ]
119 : 166 : pModule->setPredefined(true);
120 : 166 : pParentScope->addDeclaration(pModule);
121 : 166 : pParentScope = pModule;
122 [ + - ][ + - ]: 166 : pModule = new AstModule(OString("uno"), pParentScope);
[ + - ]
123 : 166 : pModule->setPredefined(true);
124 : 166 : pParentScope->addDeclaration(pModule);
125 : 166 : pParentScope = pModule;
126 : :
127 : : // define XInterface
128 [ + - ][ + - ]: 166 : AstInterface* pInterface = new AstInterface(OString("XInterface"), NULL, pParentScope);
[ + - ]
129 : 166 : pInterface->setDefined();
130 : 166 : pInterface->setPredefined(true);
131 : 166 : pInterface->setPublished();
132 : 166 : pParentScope->addDeclaration(pInterface);
133 : :
134 : : // define XInterface::queryInterface
135 : 166 : AstOperation* pOp = new AstOperation(0, (AstType*)(pRoot->lookupPrimitiveType(ET_any)),
136 [ + - ][ + - ]: 166 : OString("queryInterface"), pInterface);
[ + - ]
137 : : AstParameter* pParam = new AstParameter(DIR_IN, false,
138 : 166 : (AstType*)(pRoot->lookupPrimitiveType(ET_type)),
139 [ + - ][ + - ]: 166 : OString("aType"), pOp);
[ + - ]
140 : 166 : pOp->addDeclaration(pParam);
141 : 166 : pInterface->addMember(pOp);
142 : :
143 : : // define XInterface::acquire
144 : 166 : pOp = new AstOperation(1, (AstType*)(pRoot->lookupPrimitiveType(ET_void)),
145 [ + - ][ + - ]: 166 : OString("acquire"), pInterface);
[ + - ]
146 : 166 : pInterface->addMember(pOp);
147 : :
148 : : // define XInterface::release
149 : 166 : pOp = new AstOperation(1, (AstType*)(pRoot->lookupPrimitiveType(ET_void)),
150 [ + - ][ + - ]: 166 : OString("release"), pInterface);
[ + - ]
151 : 166 : pInterface->addMember(pOp);
152 : 166 : }
153 : :
154 : 6144 : static void SAL_CALL initializePredefinedTypes(AstModule* pRoot)
155 : : {
156 : 6144 : AstBaseType* pPredefined = NULL;
157 [ + - ]: 6144 : if ( pRoot )
158 : : {
159 [ + - ][ + - ]: 6144 : pPredefined = new AstBaseType(ET_long, OString("long"), pRoot);
[ + - ]
160 : 6144 : pRoot->addDeclaration(pPredefined);
161 : :
162 [ + - ][ + - ]: 6144 : pPredefined = new AstBaseType(ET_ulong, OString("unsigned long"), pRoot);
[ + - ]
163 : 6144 : pRoot->addDeclaration(pPredefined);
164 : :
165 [ + - ][ + - ]: 6144 : pPredefined = new AstBaseType(ET_hyper, OString("hyper"), pRoot);
[ + - ]
166 : 6144 : pRoot->addDeclaration(pPredefined);
167 : :
168 [ + - ][ + - ]: 6144 : pPredefined = new AstBaseType(ET_uhyper, OString("unsigned hyper"), pRoot);
[ + - ]
169 : 6144 : pRoot->addDeclaration(pPredefined);
170 : :
171 [ + - ][ + - ]: 6144 : pPredefined = new AstBaseType(ET_short, OString("short"), pRoot);
[ + - ]
172 : 6144 : pRoot->addDeclaration(pPredefined);
173 : :
174 [ + - ][ + - ]: 6144 : pPredefined = new AstBaseType(ET_ushort, OString("unsigned short"), pRoot);
[ + - ]
175 : 6144 : pRoot->addDeclaration(pPredefined);
176 : :
177 [ + - ][ + - ]: 6144 : pPredefined = new AstBaseType(ET_float, OString("float"), pRoot);
[ + - ]
178 : 6144 : pRoot->addDeclaration(pPredefined);
179 : :
180 [ + - ][ + - ]: 6144 : pPredefined = new AstBaseType(ET_double, OString("double"), pRoot);
[ + - ]
181 : 6144 : pRoot->addDeclaration(pPredefined);
182 : :
183 [ + - ][ + - ]: 6144 : pPredefined = new AstBaseType(ET_char, OString("char"), pRoot);
[ + - ]
184 : 6144 : pRoot->addDeclaration(pPredefined);
185 : :
186 [ + - ][ + - ]: 6144 : pPredefined = new AstBaseType(ET_byte, OString("byte"), pRoot);
[ + - ]
187 : 6144 : pRoot->addDeclaration(pPredefined);
188 : :
189 [ + - ][ + - ]: 6144 : pPredefined = new AstBaseType(ET_any, OString("any"), pRoot);
[ + - ]
190 : 6144 : pRoot->addDeclaration(pPredefined);
191 : :
192 [ + - ][ + - ]: 6144 : pPredefined = new AstBaseType(ET_string, OString("string"), pRoot);
[ + - ]
193 : 6144 : pRoot->addDeclaration(pPredefined);
194 : :
195 [ + - ][ + - ]: 6144 : pPredefined = new AstBaseType(ET_type, OString("type"), pRoot);
[ + - ]
196 : 6144 : pRoot->addDeclaration(pPredefined);
197 : :
198 [ + - ][ + - ]: 6144 : pPredefined = new AstBaseType(ET_boolean, OString("boolean"), pRoot);
[ + - ]
199 : 6144 : pRoot->addDeclaration(pPredefined);
200 : :
201 [ + - ][ + - ]: 6144 : pPredefined = new AstBaseType(ET_void, OString("void"), pRoot);
[ + - ]
202 : 6144 : pRoot->addDeclaration(pPredefined);
203 : : }
204 : 6144 : }
205 : :
206 : 166 : 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 [ + - ]: 166 : , m_parseState(PS_NoState)
217 : : {
218 [ + - ][ + - ]: 166 : m_pScopes = new AstStack();
219 : : // init root object after construction
220 : 166 : m_pRoot = NULL;
221 [ + - ]: 166 : m_pErrorHandler = new ErrorHandler();
222 [ + - ]: 166 : m_bGenerateDoc = m_pOptions->isValid("-C");
223 : 166 : }
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 : 166 : void Idlc::init()
236 : : {
237 [ - + ]: 166 : if ( m_pRoot )
238 [ # # ]: 0 : delete m_pRoot;
239 : :
240 [ + - ][ + - ]: 166 : m_pRoot = new AstModule(NT_root, OString(), NULL);
241 : :
242 : : // push the root node on the stack
243 [ + - ]: 166 : m_pScopes->push(m_pRoot);
244 : 166 : initializePredefinedTypes(m_pRoot);
245 : 166 : predefineXInterface(m_pRoot);
246 : 166 : }
247 : :
248 : 5978 : void Idlc::reset()
249 : : {
250 : 5978 : m_bIsDocValid = sal_False;
251 : 5978 : m_bIsInMainfile = sal_True;
252 : 5978 : m_published = false;
253 : :
254 : 5978 : m_errorCount = 0;
255 : 5978 : m_warningCount = 0;
256 : 5978 : m_lineNumber = 0;
257 : 5978 : m_parseState = PS_NoState;
258 : :
259 : 5978 : m_fileName = OString();
260 : 5978 : m_mainFileName = OString();
261 : 5978 : m_realFileName = OString();
262 : 5978 : m_documentation = OString();
263 : :
264 : 5978 : m_pScopes->clear();
265 [ + - ]: 5978 : if ( m_pRoot)
266 [ + - ]: 5978 : delete m_pRoot;
267 : :
268 [ + - ][ + - ]: 5978 : m_pRoot = new AstModule(NT_root, OString(), NULL);
269 : :
270 : : // push the root node on the stack
271 [ + - ]: 5978 : m_pScopes->push(m_pRoot);
272 : 5978 : initializePredefinedTypes(m_pRoot);
273 : 5978 : }
274 : :
275 : 884034 : sal_Bool Idlc::isDocValid()
276 : : {
277 [ - + ]: 884034 : if ( m_bGenerateDoc )
278 : 0 : return m_bIsDocValid;
279 : 884034 : return sal_False;;
280 : : }
281 : :
282 : 3517311 : static void lcl_writeString(::osl::File & rFile, ::osl::FileBase::RC & o_rRC,
283 : : ::rtl::OString const& rString)
284 : : {
285 : 3517311 : sal_uInt64 nWritten(0);
286 [ + - ]: 3517311 : if (::osl::FileBase::E_None == o_rRC) {
287 [ + - ]: 3517311 : o_rRC = rFile.write(rString.getStr(), rString.getLength(), nWritten);
288 [ - + ]: 3517311 : if (static_cast<sal_uInt64>(rString.getLength()) != nWritten) {
289 : 0 : o_rRC = ::osl::FileBase::E_INVAL; //?
290 : : }
291 : : }
292 : 3517311 : }
293 : :
294 : : struct WriteDep
295 : : {
296 : : ::osl::File& m_rFile;
297 : : ::osl::FileBase::RC & m_rRC;
298 : 5977 : explicit WriteDep(::osl::File & rFile, ::osl::FileBase::RC & rRC)
299 : 5977 : : m_rFile(rFile), m_rRC(rRC) { }
300 : 874845 : void operator() (::rtl::OString const& rEntry)
301 : : {
302 [ + - ]: 874845 : lcl_writeString(m_rFile, m_rRC, " \\\n ");
303 : 874845 : lcl_writeString(m_rFile, m_rRC, rEntry);
304 : 874845 : }
305 : : };
306 : :
307 : : // write a dummy target for one included file, so the incremental build does
308 : : // not break with "No rule to make target" if the included file is removed
309 : : struct WriteDummy
310 : : {
311 : : ::osl::File& m_rFile;
312 : : ::osl::FileBase::RC & m_rRC;
313 : 5977 : explicit WriteDummy(::osl::File & rFile, ::osl::FileBase::RC & rRC)
314 : 5977 : : m_rFile(rFile), m_rRC(rRC) { }
315 : 874845 : void operator() (::rtl::OString const& rEntry)
316 : : {
317 : 874845 : lcl_writeString(m_rFile, m_rRC, rEntry);
318 [ + - ]: 874845 : lcl_writeString(m_rFile, m_rRC, ":\n\n");
319 : 874845 : }
320 : : };
321 : :
322 : : bool
323 : 5977 : Idlc::dumpDeps(::rtl::OString const& rDepFile, ::rtl::OString const& rTarget)
324 : : {
325 : : ::osl::File depFile(
326 [ + - ][ + - ]: 5977 : ::rtl::OStringToOUString(rDepFile, osl_getThreadTextEncoding()));
327 : : ::osl::FileBase::RC rc =
328 [ + - ]: 5977 : depFile.open(osl_File_OpenFlag_Write | osl_File_OpenFlag_Create);
329 [ - + ]: 5977 : if (::osl::FileBase::E_None != rc) {
330 : 0 : return false;
331 : : }
332 [ + - ]: 5977 : lcl_writeString(depFile, rc, rTarget);
333 [ - + ]: 5977 : if (::osl::FileBase::E_None != rc) {
334 : 0 : return false;
335 : : }
336 [ + - ]: 5977 : lcl_writeString(depFile, rc, " :");
337 [ - + ]: 5977 : if (::osl::FileBase::E_None != rc) {
338 : 0 : return false;
339 : : }
340 [ + - ]: 5977 : m_includes.erase(getRealFileName()); // eeek, that is a temp file...
341 : : ::std::for_each(m_includes.begin(), m_includes.end(),
342 [ + - ]: 5977 : WriteDep(depFile, rc));
343 [ + - ]: 5977 : lcl_writeString(depFile, rc, "\n\n");
344 : : ::std::for_each(m_includes.begin(), m_includes.end(),
345 [ + - ]: 5977 : WriteDummy(depFile, rc));
346 [ - + ]: 5977 : if (::osl::FileBase::E_None != rc) {
347 : 0 : return false;
348 : : }
349 [ + - ]: 5977 : rc = depFile.close();
350 [ + - ]: 5977 : return ::osl::FileBase::E_None == rc;
351 : : }
352 : :
353 : : static Idlc* pStaticIdlc = NULL;
354 : :
355 : 37080848 : Idlc* SAL_CALL idlc()
356 : : {
357 : 37080848 : return pStaticIdlc;
358 : : }
359 : :
360 : 166 : Idlc* SAL_CALL setIdlc(Options* pOptions)
361 : : {
362 [ - + ]: 166 : if ( pStaticIdlc )
363 : : {
364 [ # # ]: 0 : delete pStaticIdlc;
365 : : }
366 [ + - ]: 166 : pStaticIdlc = new Idlc(pOptions);
367 : 166 : pStaticIdlc->init();
368 : 166 : return pStaticIdlc;
369 : : }
370 : :
371 : 177740 : AstDeclaration const * resolveTypedefs(AstDeclaration const * type) {
372 [ + - ]: 177740 : if (type != 0) {
373 [ + + ]: 181151 : while (type->getNodeType() == NT_typedef) {
374 : 3411 : type = static_cast< AstTypeDef const * >(type)->getBaseType();
375 : : }
376 : : }
377 : 177740 : return type;
378 : : }
379 : :
380 : 30 : AstDeclaration const * deconstructAndResolveTypedefs(
381 : : AstDeclaration const * type, sal_Int32 * rank)
382 : : {
383 : 30 : *rank = 0;
384 : 38 : for (;;) {
385 [ - + ]: 38 : if (type == 0) {
386 : 0 : return 0;
387 : : }
388 [ - + + ]: 38 : switch (type->getNodeType()) {
389 : : case NT_typedef:
390 : 0 : type = static_cast< AstTypeDef const * >(type)->getBaseType();
391 : 0 : break;
392 : : case NT_sequence:
393 : 8 : ++(*rank);
394 : 8 : type = static_cast< AstSequence const * >(type)->getMemberType();
395 : 8 : break;
396 : : default:
397 : 30 : return type;
398 : : }
399 : : }
400 : : }
401 : :
402 : 44787 : AstInterface const * resolveInterfaceTypedefs(AstType const * type) {
403 : 44787 : AstDeclaration const * decl = resolveTypedefs(type);
404 : : OSL_ASSERT(decl->getNodeType() == NT_interface);
405 : 44787 : return static_cast< AstInterface const * >(decl);
406 : : }
407 : :
408 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|