update dev300-m58
[ooovba.git] / idlc / source / idlc.cxx
blob83b0696b8dae543d17f7b845b38862a0eee2ffc1
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: idlc.cxx,v $
10 * $Revision: 1.12 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_idlc.hxx"
33 #include <idlc/idlc.hxx>
34 #include <idlc/errorhandler.hxx>
35 #include <idlc/astscope.hxx>
36 #include <idlc/astmodule.hxx>
37 #include <idlc/astservice.hxx>
38 #include <idlc/astconstants.hxx>
39 #include <idlc/astexception.hxx>
40 #include <idlc/astunion.hxx>
41 #include <idlc/astenum.hxx>
42 #include <idlc/astinterface.hxx>
43 #include <idlc/astoperation.hxx>
44 #include <idlc/astbasetype.hxx>
45 #include "idlc/astdeclaration.hxx"
46 #include "idlc/astparameter.hxx"
47 #include "idlc/astsequence.hxx"
48 #include "idlc/asttype.hxx"
49 #include "idlc/asttypedef.hxx"
51 #include "osl/diagnose.h"
53 using namespace ::rtl;
55 AstDeclaration* SAL_CALL scopeAsDecl(AstScope* pScope)
57 if (pScope == NULL) return NULL;
59 switch( pScope->getScopeNodeType() )
61 case NT_service:
62 case NT_singleton:
63 return (AstService*)(pScope);
64 case NT_module:
65 case NT_root:
66 return (AstModule*)(pScope);
67 case NT_constants:
68 return (AstConstants*)(pScope);
69 case NT_interface:
70 return (AstInterface*)(pScope);
71 case NT_operation:
72 return (AstOperation*)(pScope);
73 case NT_exception:
74 return (AstException*)(pScope);
75 case NT_union:
76 return (AstUnion*)(pScope);
77 case NT_struct:
78 return (AstStruct*)(pScope);
79 case NT_enum:
80 return (AstEnum*)(pScope);
81 default:
82 return NULL;
86 AstScope* SAL_CALL declAsScope(AstDeclaration* pDecl)
88 if (pDecl == NULL) return NULL;
90 switch(pDecl->getNodeType())
92 case NT_interface:
93 return (AstInterface*)(pDecl);
94 case NT_service:
95 case NT_singleton:
96 return (AstService*)(pDecl);
97 case NT_module:
98 case NT_root:
99 return (AstModule*)(pDecl);
100 case NT_constants:
101 return (AstConstants*)(pDecl);
102 case NT_exception:
103 return (AstException*)(pDecl);
104 case NT_union:
105 return (AstUnion*)(pDecl);
106 case NT_struct:
107 return (AstStruct*)(pDecl);
108 case NT_enum:
109 return (AstEnum*)(pDecl);
110 case NT_operation:
111 return (AstOperation*)(pDecl);
112 default:
113 return NULL;
117 static void SAL_CALL predefineXInterface(AstModule* pRoot)
119 // define the modules com::sun::star::uno
120 AstModule* pParentScope = pRoot;
121 AstModule* pModule = new AstModule(OString("com"), pParentScope);
122 pModule->setPredefined(true);
123 pParentScope->addDeclaration(pModule);
124 pParentScope = pModule;
125 pModule = new AstModule(OString("sun"), pParentScope);
126 pModule->setPredefined(true);
127 pParentScope->addDeclaration(pModule);
128 pParentScope = pModule;
129 pModule = new AstModule(OString("star"), pParentScope);
130 pModule->setPredefined(true);
131 pParentScope->addDeclaration(pModule);
132 pParentScope = pModule;
133 pModule = new AstModule(OString("uno"), pParentScope);
134 pModule->setPredefined(true);
135 pParentScope->addDeclaration(pModule);
136 pParentScope = pModule;
138 // define XInterface
139 AstInterface* pInterface = new AstInterface(OString("XInterface"), NULL, pParentScope);
140 pInterface->setDefined();
141 pInterface->setPredefined(true);
142 pInterface->setPublished();
143 pParentScope->addDeclaration(pInterface);
145 // define XInterface::queryInterface
146 AstOperation* pOp = new AstOperation(0, (AstType*)(pRoot->lookupPrimitiveType(ET_any)),
147 OString("queryInterface"), pInterface);
148 AstParameter* pParam = new AstParameter(DIR_IN, false,
149 (AstType*)(pRoot->lookupPrimitiveType(ET_type)),
150 OString("aType"), pOp);
151 pOp->addDeclaration(pParam);
152 pInterface->addMember(pOp);
154 // define XInterface::acquire
155 pOp = new AstOperation(1, (AstType*)(pRoot->lookupPrimitiveType(ET_void)),
156 OString("acquire"), pInterface);
157 pInterface->addMember(pOp);
159 // define XInterface::release
160 pOp = new AstOperation(1, (AstType*)(pRoot->lookupPrimitiveType(ET_void)),
161 OString("release"), pInterface);
162 pInterface->addMember(pOp);
165 static void SAL_CALL initializePredefinedTypes(AstModule* pRoot)
167 AstBaseType* pPredefined = NULL;
168 if ( pRoot )
170 pPredefined = new AstBaseType(ET_long, OString("long"), pRoot);
171 pRoot->addDeclaration(pPredefined);
173 pPredefined = new AstBaseType(ET_ulong, OString("unsigned long"), pRoot);
174 pRoot->addDeclaration(pPredefined);
176 pPredefined = new AstBaseType(ET_hyper, OString("hyper"), pRoot);
177 pRoot->addDeclaration(pPredefined);
179 pPredefined = new AstBaseType(ET_uhyper, OString("unsigned hyper"), pRoot);
180 pRoot->addDeclaration(pPredefined);
182 pPredefined = new AstBaseType(ET_short, OString("short"), pRoot);
183 pRoot->addDeclaration(pPredefined);
185 pPredefined = new AstBaseType(ET_ushort, OString("unsigned short"), pRoot);
186 pRoot->addDeclaration(pPredefined);
188 pPredefined = new AstBaseType(ET_float, OString("float"), pRoot);
189 pRoot->addDeclaration(pPredefined);
191 pPredefined = new AstBaseType(ET_double, OString("double"), pRoot);
192 pRoot->addDeclaration(pPredefined);
194 pPredefined = new AstBaseType(ET_char, OString("char"), pRoot);
195 pRoot->addDeclaration(pPredefined);
197 pPredefined = new AstBaseType(ET_byte, OString("byte"), pRoot);
198 pRoot->addDeclaration(pPredefined);
200 pPredefined = new AstBaseType(ET_any, OString("any"), pRoot);
201 pRoot->addDeclaration(pPredefined);
203 pPredefined = new AstBaseType(ET_string, OString("string"), pRoot);
204 pRoot->addDeclaration(pPredefined);
206 pPredefined = new AstBaseType(ET_type, OString("type"), pRoot);
207 pRoot->addDeclaration(pPredefined);
209 pPredefined = new AstBaseType(ET_boolean, OString("boolean"), pRoot);
210 pRoot->addDeclaration(pPredefined);
212 pPredefined = new AstBaseType(ET_void, OString("void"), pRoot);
213 pRoot->addDeclaration(pPredefined);
217 Idlc::Idlc(Options* pOptions)
218 : m_pOptions(pOptions)
219 , m_bIsDocValid(sal_False)
220 , m_bIsInMainfile(sal_True)
221 , m_published(false)
222 , m_errorCount(0)
223 , m_warningCount(0)
224 , m_lineNumber(0)
225 , m_parseState(PS_NoState)
227 m_pScopes = new AstStack();
228 // init root object after construction
229 m_pRoot = NULL;
230 m_pErrorHandler = new ErrorHandler();
231 m_bGenerateDoc = m_pOptions->isValid("-C");
234 Idlc::~Idlc()
236 if (m_pRoot)
237 delete m_pRoot;
238 if (m_pScopes)
239 delete m_pScopes;
240 if (m_pErrorHandler)
241 delete m_pErrorHandler;
244 void Idlc::init()
246 if ( m_pRoot )
247 delete m_pRoot;
249 m_pRoot = new AstModule(NT_root, OString(), NULL);
251 // push the root node on the stack
252 m_pScopes->push(m_pRoot);
253 initializePredefinedTypes(m_pRoot);
254 predefineXInterface(m_pRoot);
257 void Idlc::reset()
259 m_bIsDocValid = sal_False;
260 m_bIsInMainfile = sal_True;
261 m_published = false;
263 m_errorCount = 0;
264 m_warningCount = 0;
265 m_lineNumber = 0;
266 m_parseState = PS_NoState;
268 m_fileName = OString();
269 m_mainFileName = OString();
270 m_realFileName = OString();
271 m_documentation = OString();
273 m_pScopes->clear();
274 if ( m_pRoot)
275 delete m_pRoot;
277 m_pRoot = new AstModule(NT_root, OString(), NULL);
279 // push the root node on the stack
280 m_pScopes->push(m_pRoot);
281 initializePredefinedTypes(m_pRoot);
284 sal_Bool Idlc::isDocValid()
286 if ( m_bGenerateDoc )
287 return m_bIsDocValid;
288 return sal_False;;
291 static Idlc* pStaticIdlc = NULL;
293 Idlc* SAL_CALL idlc()
295 return pStaticIdlc;
298 Idlc* SAL_CALL setIdlc(Options* pOptions)
300 if ( pStaticIdlc )
302 delete pStaticIdlc;
304 pStaticIdlc = new Idlc(pOptions);
305 pStaticIdlc->init();
306 return pStaticIdlc;
309 AstDeclaration const * resolveTypedefs(AstDeclaration const * type) {
310 if (type != 0) {
311 while (type->getNodeType() == NT_typedef) {
312 type = static_cast< AstTypeDef const * >(type)->getBaseType();
315 return type;
318 AstDeclaration const * deconstructAndResolveTypedefs(
319 AstDeclaration const * type, sal_Int32 * rank)
321 *rank = 0;
322 for (;;) {
323 if (type == 0) {
324 return 0;
326 switch (type->getNodeType()) {
327 case NT_typedef:
328 type = static_cast< AstTypeDef const * >(type)->getBaseType();
329 break;
330 case NT_sequence:
331 ++(*rank);
332 type = static_cast< AstSequence const * >(type)->getMemberType();
333 break;
334 default:
335 return type;
340 AstInterface const * resolveInterfaceTypedefs(AstType const * type) {
341 AstDeclaration const * decl = resolveTypedefs(type);
342 OSL_ASSERT(decl->getNodeType() == NT_interface);
343 return static_cast< AstInterface const * >(decl);