1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
26 using namespace ::rtl
;
28 sal_Bool
isGlobal(const OString
& scopedName
)
30 if (scopedName
.isEmpty() || (scopedName
.indexOf(':') == 0))
37 AstScope::AstScope(NodeType nodeType
)
38 : m_nodeType(nodeType
)
48 AstDeclaration
* AstScope::addDeclaration(AstDeclaration
* pDecl
)
50 AstDeclaration
* pDeclaration
= NULL
;
52 if ((pDeclaration
= lookupForAdd(pDecl
)) != NULL
)
54 if (pDecl
->getNodeType() == NT_union_branch
)
56 m_declarations
.push_back(pDecl
);
59 if ( pDecl
->hasAncestor(pDeclaration
) )
61 idlc()->error()->error2(EIDL_REDEF_SCOPE
, pDecl
, pDeclaration
);
64 if ( (pDecl
->getNodeType() == pDeclaration
->getNodeType()) &&
65 (pDecl
->getNodeType() == NT_sequence
66 || pDecl
->getNodeType() == NT_array
67 || pDecl
->getNodeType() == NT_instantiated_struct
) )
71 if ( (pDeclaration
->getNodeType() == NT_interface
)
72 && (pDecl
->getNodeType() == NT_interface
)
73 && !((AstInterface
*)pDeclaration
)->isDefined() )
75 m_declarations
.push_back(pDecl
);
78 if ( (NT_service
== m_nodeType
) &&
79 ( ((pDecl
->getNodeType() == NT_interface_member
)
80 && (pDeclaration
->getNodeType() == NT_interface
)) ||
81 ((pDecl
->getNodeType() == NT_service_member
)
82 && (pDeclaration
->getNodeType() == NT_service
)) )
85 m_declarations
.push_back(pDecl
);
89 idlc()->error()->error2(EIDL_REDEF_SCOPE
, scopeAsDecl(this), pDecl
);
93 m_declarations
.push_back(pDecl
);
97 sal_uInt16
AstScope::getNodeCount(NodeType nodeType
)
99 DeclList::const_iterator iter
= getIteratorBegin();
100 DeclList::const_iterator end
= getIteratorEnd();
101 AstDeclaration
* pDecl
= NULL
;
102 sal_uInt16 count
= 0;
104 while ( iter
!= end
)
107 if ( pDecl
->getNodeType() == nodeType
)
114 AstDeclaration
* AstScope::lookupByName(const OString
& scopedName
)
116 AstDeclaration
* pDecl
= NULL
;
117 AstScope
* pScope
= NULL
;
118 if (scopedName
.isEmpty())
121 // If name starts with "::" start look up in global scope
122 if ( isGlobal(scopedName
) )
124 pDecl
= scopeAsDecl(this);
128 pScope
= pDecl
->getScope();
129 // If this is the global scope ...
132 // look up the scopedName part after "::"
133 OString subName
= scopedName
.copy(2);
134 pDecl
= lookupByName(subName
);
136 //return pScope->lookupByName();
138 // OK, not global scope yet, so simply iterate with parent scope
139 pDecl
= pScope
->lookupByName(scopedName
);
143 // The name does not start with "::"
144 // Look up in the local scope and start with the first scope
145 sal_Int32 nIndex
= scopedName
.indexOf(':');
146 OString firstScope
= nIndex
> 0 ? scopedName
.copy(0, nIndex
) : scopedName
;
147 sal_Bool bFindFirstScope
= sal_True
;
148 pDecl
= lookupByNameLocal(firstScope
);
151 bFindFirstScope
= sal_False
;
153 // OK, not found. Go down parent scope chain
154 pDecl
= scopeAsDecl(this);
157 pScope
= pDecl
->getScope();
159 pDecl
= pScope
->lookupByName(scopedName
);
163 // Special case for scope which is an interface. We
164 // have to look in the inherited interfaces as well.
167 if (m_nodeType
== NT_interface
)
168 pDecl
= lookupInInherited(scopedName
);
173 if ( bFindFirstScope
&& (firstScope
!= scopedName
) )
176 sal_Int32 nOffset
= 2;
179 pScope
= declAsScope(pDecl
);
182 pDecl
= pScope
->lookupByNameLocal(scopedName
.getToken(nOffset
, ':', i
));
191 // last try if is not the global scope and the scopeName isn't specify global too
192 pDecl
= scopeAsDecl(this);
193 if ( pDecl
&& (pDecl
->getLocalName() != "") )
195 pScope
= pDecl
->getScope();
197 pDecl
= pScope
->lookupByName(scopedName
);
209 AstDeclaration
* AstScope::lookupByNameLocal(const OString
& name
) const
211 DeclList::const_iterator
iter(m_declarations
.begin());
212 DeclList::const_iterator
end(m_declarations
.end());
213 AstDeclaration
* pDecl
= NULL
;
215 while ( iter
!= end
)
218 if ( pDecl
->getLocalName() == name
)
225 AstDeclaration
* AstScope::lookupInInherited(const OString
& scopedName
) const
227 AstInterface
* pInterface
= (AstInterface
*)this;
232 // Can't look in an interface which was not yet defined
233 if ( !pInterface
->getScope() )
235 idlc()->error()->forwardLookupError(pInterface
, scopedName
);
238 // OK, loop through inherited interfaces. Stop when you find it
239 AstInterface::InheritedInterfaces::const_iterator
iter(
240 pInterface
->getAllInheritedInterfaces().begin());
241 AstInterface::InheritedInterfaces::const_iterator
end(
242 pInterface
->getAllInheritedInterfaces().end());
243 while ( iter
!= end
)
245 AstInterface
const * resolved
= iter
->getResolved();
246 AstDeclaration
* pDecl
= resolved
->lookupByNameLocal(scopedName
);
249 pDecl
= resolved
->lookupInInherited(scopedName
);
258 AstDeclaration
* AstScope::lookupPrimitiveType(ExprType type
)
260 AstDeclaration
* pDecl
= NULL
;
261 AstScope
* pScope
= NULL
;
262 AstBaseType
* pBaseType
= NULL
;
264 pDecl
= scopeAsDecl(this);
267 pScope
= pDecl
->getScope();
269 return pScope
->lookupPrimitiveType(type
);
277 typeName
= OString("short");
280 typeName
= OString("unsigned short");
283 typeName
= OString("long");
286 typeName
= OString("unsigned long");
289 typeName
= OString("hyper");
292 typeName
= OString("unsigned hyper");
295 typeName
= OString("float");
298 typeName
= OString("double");
301 typeName
= OString("char");
304 typeName
= OString("byte");
307 typeName
= OString("boolean");
310 typeName
= OString("any");
313 typeName
= OString("void");
316 typeName
= OString("type");
319 typeName
= OString("string");
323 pDecl
= lookupByNameLocal(typeName
);
325 if ( pDecl
&& (pDecl
->getNodeType() == NT_predefined
) )
327 pBaseType
= (AstBaseType
*)pDecl
;
329 if ( pBaseType
->getExprType() == type
)
336 AstDeclaration
* AstScope::lookupForAdd(AstDeclaration
* pDecl
)
341 AstDeclaration
* pRetDecl
= lookupByNameLocal(pDecl
->getLocalName());
346 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */