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/astscope.hxx>
21 : #include <idlc/astbasetype.hxx>
22 : #include <idlc/astinterface.hxx>
23 : #include <idlc/errorhandler.hxx>
24 :
25 :
26 : using namespace ::rtl;
27 :
28 0 : bool isGlobal(const OString& scopedName)
29 : {
30 0 : return scopedName.isEmpty() || scopedName.startsWith(":");
31 : }
32 :
33 0 : AstScope::AstScope(NodeType nodeType)
34 0 : : m_nodeType(nodeType)
35 : {
36 :
37 0 : }
38 :
39 0 : AstScope::~AstScope()
40 : {
41 :
42 0 : }
43 :
44 0 : AstDeclaration* AstScope::addDeclaration(AstDeclaration* pDecl)
45 : {
46 0 : AstDeclaration* pDeclaration = NULL;
47 :
48 0 : if ((pDeclaration = lookupForAdd(pDecl)) != NULL)
49 : {
50 0 : if ( pDecl->hasAncestor(pDeclaration) )
51 : {
52 0 : idlc()->error()->error2(EIDL_REDEF_SCOPE, pDecl, pDeclaration);
53 0 : return NULL;
54 : }
55 0 : if ( (pDecl->getNodeType() == pDeclaration->getNodeType()) &&
56 0 : (pDecl->getNodeType() == NT_sequence
57 0 : || pDecl->getNodeType() == NT_instantiated_struct) )
58 : {
59 0 : return pDeclaration;
60 : }
61 0 : if ( (pDeclaration->getNodeType() == NT_interface)
62 0 : && (pDecl->getNodeType() == NT_interface)
63 0 : && !((AstInterface*)pDeclaration)->isDefined() )
64 : {
65 0 : m_declarations.push_back(pDecl);
66 0 : return pDecl;
67 : }
68 0 : if ( (NT_service == m_nodeType) &&
69 0 : ( ((pDecl->getNodeType() == NT_interface_member)
70 0 : && (pDeclaration->getNodeType() == NT_interface)) ||
71 0 : ((pDecl->getNodeType() == NT_service_member)
72 0 : && (pDeclaration->getNodeType() == NT_service)) )
73 : )
74 : {
75 0 : m_declarations.push_back(pDecl);
76 0 : return pDecl;
77 : }
78 :
79 0 : idlc()->error()->error2(EIDL_REDEF_SCOPE, scopeAsDecl(this), pDecl);
80 0 : return NULL;
81 : }
82 :
83 0 : m_declarations.push_back(pDecl);
84 0 : return pDecl;
85 : }
86 :
87 0 : sal_uInt16 AstScope::getNodeCount(NodeType nodeType)
88 : {
89 0 : DeclList::const_iterator iter = getIteratorBegin();
90 0 : DeclList::const_iterator end = getIteratorEnd();
91 0 : AstDeclaration* pDecl = NULL;
92 0 : sal_uInt16 count = 0;
93 :
94 0 : while ( iter != end )
95 : {
96 0 : pDecl = *iter;
97 0 : if ( pDecl->getNodeType() == nodeType )
98 0 : count++;
99 0 : ++iter;
100 : }
101 0 : return count;
102 : }
103 :
104 0 : AstDeclaration* AstScope::lookupByName(const OString& scopedName)
105 : {
106 0 : AstDeclaration* pDecl = NULL;
107 0 : AstScope* pScope = NULL;
108 0 : if (scopedName.isEmpty())
109 0 : return NULL;
110 :
111 : // If name starts with "::" start look up in global scope
112 0 : if ( isGlobal(scopedName) )
113 : {
114 0 : pDecl = scopeAsDecl(this);
115 0 : if ( !pDecl )
116 0 : return NULL;
117 :
118 0 : pScope = pDecl->getScope();
119 : // If this is the global scope ...
120 0 : if ( !pScope )
121 : {
122 : // look up the scopedName part after "::"
123 0 : OString subName = scopedName.copy(2);
124 0 : pDecl = lookupByName(subName);
125 0 : return pDecl;
126 : //return pScope->lookupByName();
127 : }
128 : // OK, not global scope yet, so simply iterate with parent scope
129 0 : pDecl = pScope->lookupByName(scopedName);
130 0 : return pDecl;
131 : }
132 :
133 : // The name does not start with "::"
134 : // Look up in the local scope and start with the first scope
135 0 : sal_Int32 nIndex = scopedName.indexOf(':');
136 0 : OString firstScope = nIndex > 0 ? scopedName.copy(0, nIndex) : scopedName;
137 0 : bool bFindFirstScope = true;
138 0 : pDecl = lookupByNameLocal(firstScope);
139 0 : if ( !pDecl )
140 : {
141 0 : bFindFirstScope = false;
142 :
143 : // OK, not found. Go down parent scope chain
144 0 : pDecl = scopeAsDecl(this);
145 0 : if ( pDecl )
146 : {
147 0 : pScope = pDecl->getScope();
148 0 : if ( pScope )
149 0 : pDecl = pScope->lookupByName(scopedName);
150 : else
151 0 : pDecl = NULL;
152 :
153 : // Special case for scope which is an interface. We
154 : // have to look in the inherited interfaces as well.
155 0 : if ( !pDecl )
156 : {
157 0 : if (m_nodeType == NT_interface)
158 0 : pDecl = lookupInInherited(scopedName);
159 : }
160 : }
161 : }
162 :
163 0 : if ( bFindFirstScope && (firstScope != scopedName) )
164 : {
165 0 : sal_Int32 i = 0;
166 0 : sal_Int32 nOffset = 2;
167 0 : do
168 : {
169 0 : pScope = declAsScope(pDecl);
170 0 : if( pScope )
171 : {
172 0 : pDecl = pScope->lookupByNameLocal(scopedName.getToken(nOffset, ':', i ));
173 0 : nOffset = 1;
174 : }
175 0 : if( !pDecl )
176 0 : break;
177 0 : } while( i != -1 );
178 :
179 0 : if ( !pDecl )
180 : {
181 : // last try if is not the global scope and the scopeName isn't specify global too
182 0 : pDecl = scopeAsDecl(this);
183 0 : if ( pDecl && (pDecl->getLocalName() != "") )
184 : {
185 0 : pScope = pDecl->getScope();
186 0 : if ( pScope )
187 0 : pDecl = pScope->lookupByName(scopedName);
188 : } else
189 : {
190 0 : pDecl = NULL;
191 : }
192 : }
193 :
194 : }
195 :
196 0 : return pDecl;
197 : }
198 :
199 0 : AstDeclaration* AstScope::lookupByNameLocal(const OString& name) const
200 : {
201 0 : DeclList::const_iterator iter(m_declarations.begin());
202 0 : DeclList::const_iterator end(m_declarations.end());
203 0 : AstDeclaration* pDecl = NULL;
204 :
205 0 : while ( iter != end )
206 : {
207 0 : pDecl = *iter;
208 0 : if ( pDecl->getLocalName() == name )
209 0 : return pDecl;
210 0 : ++iter;
211 : }
212 0 : return NULL;
213 : }
214 :
215 0 : AstDeclaration* AstScope::lookupInInherited(const OString& scopedName) const
216 : {
217 0 : AstInterface* pInterface = (AstInterface*)this;
218 :
219 0 : if ( !pInterface )
220 0 : return NULL;
221 :
222 : // Can't look in an interface which was not yet defined
223 0 : if ( !pInterface->getScope() )
224 : {
225 0 : idlc()->error()->forwardLookupError(pInterface, scopedName);
226 : }
227 :
228 : // OK, loop through inherited interfaces. Stop when you find it
229 : AstInterface::InheritedInterfaces::const_iterator iter(
230 0 : pInterface->getAllInheritedInterfaces().begin());
231 : AstInterface::InheritedInterfaces::const_iterator end(
232 0 : pInterface->getAllInheritedInterfaces().end());
233 0 : while ( iter != end )
234 : {
235 0 : AstInterface const * resolved = iter->getResolved();
236 0 : AstDeclaration* pDecl = resolved->lookupByNameLocal(scopedName);
237 0 : if ( pDecl )
238 0 : return pDecl;
239 0 : pDecl = resolved->lookupInInherited(scopedName);
240 0 : if ( pDecl )
241 0 : return pDecl;
242 0 : ++iter;
243 : }
244 : // Not found
245 0 : return NULL;
246 : }
247 :
248 0 : AstDeclaration* AstScope::lookupPrimitiveType(ExprType type)
249 : {
250 0 : AstDeclaration* pDecl = NULL;
251 0 : AstScope* pScope = NULL;
252 0 : OString typeName;
253 0 : pDecl = scopeAsDecl(this);
254 0 : if ( !pDecl )
255 0 : return NULL;
256 0 : pScope = pDecl->getScope();
257 0 : if ( pScope)
258 0 : return pScope->lookupPrimitiveType(type);
259 :
260 0 : switch (type)
261 : {
262 : case ET_none:
263 : OSL_ASSERT(false);
264 0 : break;
265 : case ET_short:
266 0 : typeName = OString("short");
267 0 : break;
268 : case ET_ushort:
269 0 : typeName = OString("unsigned short");
270 0 : break;
271 : case ET_long:
272 0 : typeName = OString("long");
273 0 : break;
274 : case ET_ulong:
275 0 : typeName = OString("unsigned long");
276 0 : break;
277 : case ET_hyper:
278 0 : typeName = OString("hyper");
279 0 : break;
280 : case ET_uhyper:
281 0 : typeName = OString("unsigned hyper");
282 0 : break;
283 : case ET_float:
284 0 : typeName = OString("float");
285 0 : break;
286 : case ET_double:
287 0 : typeName = OString("double");
288 0 : break;
289 : case ET_char:
290 0 : typeName = OString("char");
291 0 : break;
292 : case ET_byte:
293 0 : typeName = OString("byte");
294 0 : break;
295 : case ET_boolean:
296 0 : typeName = OString("boolean");
297 0 : break;
298 : case ET_any:
299 0 : typeName = OString("any");
300 0 : break;
301 : case ET_void:
302 0 : typeName = OString("void");
303 0 : break;
304 : case ET_type:
305 0 : typeName = OString("type");
306 0 : break;
307 : case ET_string:
308 0 : typeName = OString("string");
309 0 : break;
310 : }
311 :
312 0 : pDecl = lookupByNameLocal(typeName);
313 :
314 0 : if ( pDecl && (pDecl->getNodeType() == NT_predefined) )
315 : {
316 0 : AstBaseType* pBaseType = (AstBaseType*)pDecl;
317 :
318 0 : if ( pBaseType->getExprType() == type )
319 0 : return pDecl;
320 : }
321 :
322 0 : return NULL;
323 : }
324 :
325 0 : AstDeclaration* AstScope::lookupForAdd(AstDeclaration* pDecl)
326 : {
327 0 : if ( !pDecl )
328 0 : return NULL;
329 :
330 0 : AstDeclaration* pRetDecl = lookupByNameLocal(pDecl->getLocalName());
331 :
332 0 : return pRetDecl;
333 : }
334 :
335 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|