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