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