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