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