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