fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / idlc / source / astscope.cxx
blobc4a84c4d8c2e76c30e05d5fdd35f9f4d0d942fb6
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <idlc/astscope.hxx>
21 #include <idlc/astbasetype.hxx>
22 #include <idlc/astinterface.hxx>
23 #include <idlc/errorhandler.hxx>
24 #include <osl/diagnose.h>
27 bool isGlobal(const OString& scopedName)
29 return scopedName.isEmpty() || scopedName.startsWith(":");
32 AstScope::AstScope(NodeType nodeType)
33 : m_nodeType(nodeType)
38 AstScope::~AstScope()
43 AstDeclaration* AstScope::addDeclaration(AstDeclaration* pDecl)
45 AstDeclaration* pDeclaration = NULL;
47 if ((pDeclaration = lookupForAdd(pDecl)) != NULL)
49 if ( pDecl->hasAncestor(pDeclaration) )
51 ErrorHandler::error2(EIDL_REDEF_SCOPE, pDecl, pDeclaration);
52 return NULL;
54 if ( (pDecl->getNodeType() == pDeclaration->getNodeType()) &&
55 (pDecl->getNodeType() == NT_sequence
56 || pDecl->getNodeType() == NT_instantiated_struct) )
58 return pDeclaration;
60 if ( (pDeclaration->getNodeType() == NT_interface)
61 && (pDecl->getNodeType() == NT_interface)
62 && !(static_cast<AstInterface*>(pDeclaration)->isDefined()) )
64 m_declarations.push_back(pDecl);
65 return pDecl;
67 if ( (NT_service == m_nodeType) &&
68 ( ((pDecl->getNodeType() == NT_interface_member)
69 && (pDeclaration->getNodeType() == NT_interface)) ||
70 ((pDecl->getNodeType() == NT_service_member)
71 && (pDeclaration->getNodeType() == NT_service)) )
74 m_declarations.push_back(pDecl);
75 return pDecl;
78 ErrorHandler::error2(EIDL_REDEF_SCOPE, scopeAsDecl(this), pDecl);
79 return NULL;
82 m_declarations.push_back(pDecl);
83 return pDecl;
86 sal_uInt16 AstScope::getNodeCount(NodeType nodeType)
88 DeclList::const_iterator iter = getIteratorBegin();
89 DeclList::const_iterator end = getIteratorEnd();
90 AstDeclaration* pDecl = NULL;
91 sal_uInt16 count = 0;
93 while ( iter != end )
95 pDecl = *iter;
96 if ( pDecl->getNodeType() == nodeType )
97 count++;
98 ++iter;
100 return count;
103 AstDeclaration* AstScope::lookupByName(const OString& scopedName)
105 AstDeclaration* pDecl = NULL;
106 AstScope* pScope = NULL;
107 if (scopedName.isEmpty())
108 return NULL;
110 // If name starts with "::" start look up in global scope
111 if ( isGlobal(scopedName) )
113 pDecl = scopeAsDecl(this);
114 if ( !pDecl )
115 return NULL;
117 pScope = pDecl->getScope();
118 // If this is the global scope ...
119 if ( !pScope )
121 // look up the scopedName part after "::"
122 OString subName = scopedName.copy(2);
123 pDecl = lookupByName(subName);
124 return pDecl;
125 //return pScope->lookupByName();
127 // OK, not global scope yet, so simply iterate with parent scope
128 pDecl = pScope->lookupByName(scopedName);
129 return pDecl;
132 // The name does not start with "::"
133 // Look up in the local scope and start with the first scope
134 sal_Int32 nIndex = scopedName.indexOf(':');
135 OString firstScope = nIndex > 0 ? scopedName.copy(0, nIndex) : scopedName;
136 bool bFindFirstScope = true;
137 pDecl = lookupByNameLocal(firstScope);
138 if ( !pDecl )
140 bFindFirstScope = false;
142 // OK, not found. Go down parent scope chain
143 pDecl = scopeAsDecl(this);
144 if ( pDecl )
146 pScope = pDecl->getScope();
147 if ( pScope )
148 pDecl = pScope->lookupByName(scopedName);
149 else
150 pDecl = NULL;
152 // Special case for scope which is an interface. We
153 // have to look in the inherited interfaces as well.
154 if ( !pDecl )
156 if (m_nodeType == NT_interface)
157 pDecl = lookupInInherited(scopedName);
162 if ( bFindFirstScope && (firstScope != scopedName) )
164 sal_Int32 i = 0;
165 sal_Int32 nOffset = 2;
168 pScope = declAsScope(pDecl);
169 if( pScope )
171 pDecl = pScope->lookupByNameLocal(scopedName.getToken(nOffset, ':', i ));
172 nOffset = 1;
174 if( !pDecl )
175 break;
176 } while( i != -1 );
178 if ( !pDecl )
180 // last try if is not the global scope and the scopeName isn't specify global too
181 pDecl = scopeAsDecl(this);
182 if ( pDecl && !pDecl->getLocalName().isEmpty() )
184 pScope = pDecl->getScope();
185 if ( pScope )
186 pDecl = pScope->lookupByName(scopedName);
187 } else
189 pDecl = NULL;
195 return pDecl;
198 AstDeclaration* AstScope::lookupByNameLocal(const OString& name) const
200 DeclList::const_iterator iter(m_declarations.begin());
201 DeclList::const_iterator end(m_declarations.end());
202 AstDeclaration* pDecl = NULL;
204 while ( iter != end )
206 pDecl = *iter;
207 if ( pDecl->getLocalName() == name )
208 return pDecl;
209 ++iter;
211 return NULL;
214 AstDeclaration* AstScope::lookupInInherited(const OString& scopedName) const
216 const AstInterface* pInterface = dynamic_cast<const AstInterface*>(this);
218 if ( !pInterface )
219 return NULL;
221 // Can't look in an interface which was not yet defined
222 if ( !pInterface->getScope() )
224 ErrorHandler::forwardLookupError(pInterface, scopedName);
227 // OK, loop through inherited interfaces. Stop when you find it
228 AstInterface::InheritedInterfaces::const_iterator iter(
229 pInterface->getAllInheritedInterfaces().begin());
230 AstInterface::InheritedInterfaces::const_iterator end(
231 pInterface->getAllInheritedInterfaces().end());
232 while ( iter != end )
234 AstInterface const * resolved = iter->getResolved();
235 AstDeclaration* pDecl = resolved->lookupByNameLocal(scopedName);
236 if ( pDecl )
237 return pDecl;
238 pDecl = resolved->lookupInInherited(scopedName);
239 if ( pDecl )
240 return pDecl;
241 ++iter;
243 // Not found
244 return NULL;
247 AstDeclaration* AstScope::lookupPrimitiveType(ExprType type)
249 AstDeclaration* pDecl = NULL;
250 AstScope* pScope = NULL;
251 OString typeName;
252 pDecl = scopeAsDecl(this);
253 if ( !pDecl )
254 return NULL;
255 pScope = pDecl->getScope();
256 if ( pScope)
257 return pScope->lookupPrimitiveType(type);
259 switch (type)
261 case ET_none:
262 OSL_ASSERT(false);
263 break;
264 case ET_short:
265 typeName = OString("short");
266 break;
267 case ET_ushort:
268 typeName = OString("unsigned short");
269 break;
270 case ET_long:
271 typeName = OString("long");
272 break;
273 case ET_ulong:
274 typeName = OString("unsigned long");
275 break;
276 case ET_hyper:
277 typeName = OString("hyper");
278 break;
279 case ET_uhyper:
280 typeName = OString("unsigned hyper");
281 break;
282 case ET_float:
283 typeName = OString("float");
284 break;
285 case ET_double:
286 typeName = OString("double");
287 break;
288 case ET_char:
289 typeName = OString("char");
290 break;
291 case ET_byte:
292 typeName = OString("byte");
293 break;
294 case ET_boolean:
295 typeName = OString("boolean");
296 break;
297 case ET_any:
298 typeName = OString("any");
299 break;
300 case ET_void:
301 typeName = OString("void");
302 break;
303 case ET_type:
304 typeName = OString("type");
305 break;
306 case ET_string:
307 typeName = OString("string");
308 break;
311 pDecl = lookupByNameLocal(typeName);
313 if ( pDecl && (pDecl->getNodeType() == NT_predefined) )
315 AstBaseType* pBaseType = static_cast<AstBaseType*>(pDecl);
317 if ( pBaseType->getExprType() == type )
318 return pDecl;
321 return NULL;
324 AstDeclaration* AstScope::lookupForAdd(AstDeclaration* pDecl)
326 if ( !pDecl )
327 return NULL;
329 AstDeclaration* pRetDecl = lookupByNameLocal(pDecl->getLocalName());
331 return pRetDecl;
334 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */