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>
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
)
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
);
54 if ( (pDecl
->getNodeType() == pDeclaration
->getNodeType()) &&
55 (pDecl
->getNodeType() == NT_sequence
56 || pDecl
->getNodeType() == NT_instantiated_struct
) )
60 if ( (pDeclaration
->getNodeType() == NT_interface
)
61 && (pDecl
->getNodeType() == NT_interface
)
62 && !(static_cast<AstInterface
*>(pDeclaration
)->isDefined()) )
64 m_declarations
.push_back(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
);
78 ErrorHandler::error2(EIDL_REDEF_SCOPE
, scopeAsDecl(this), pDecl
);
82 m_declarations
.push_back(pDecl
);
86 sal_uInt16
AstScope::getNodeCount(NodeType nodeType
)
88 DeclList::const_iterator iter
= getIteratorBegin();
89 DeclList::const_iterator end
= getIteratorEnd();
90 AstDeclaration
* pDecl
= NULL
;
96 if ( pDecl
->getNodeType() == nodeType
)
103 AstDeclaration
* AstScope::lookupByName(const OString
& scopedName
)
105 AstDeclaration
* pDecl
= NULL
;
106 AstScope
* pScope
= NULL
;
107 if (scopedName
.isEmpty())
110 // If name starts with "::" start look up in global scope
111 if ( isGlobal(scopedName
) )
113 pDecl
= scopeAsDecl(this);
117 pScope
= pDecl
->getScope();
118 // If this is the global scope ...
121 // look up the scopedName part after "::"
122 OString subName
= scopedName
.copy(2);
123 pDecl
= lookupByName(subName
);
125 //return pScope->lookupByName();
127 // OK, not global scope yet, so simply iterate with parent scope
128 pDecl
= pScope
->lookupByName(scopedName
);
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
);
140 bFindFirstScope
= false;
142 // OK, not found. Go down parent scope chain
143 pDecl
= scopeAsDecl(this);
146 pScope
= pDecl
->getScope();
148 pDecl
= pScope
->lookupByName(scopedName
);
152 // Special case for scope which is an interface. We
153 // have to look in the inherited interfaces as well.
156 if (m_nodeType
== NT_interface
)
157 pDecl
= lookupInInherited(scopedName
);
162 if ( bFindFirstScope
&& (firstScope
!= scopedName
) )
165 sal_Int32 nOffset
= 2;
168 pScope
= declAsScope(pDecl
);
171 pDecl
= pScope
->lookupByNameLocal(scopedName
.getToken(nOffset
, ':', i
));
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();
186 pDecl
= pScope
->lookupByName(scopedName
);
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
)
207 if ( pDecl
->getLocalName() == name
)
214 AstDeclaration
* AstScope::lookupInInherited(const OString
& scopedName
) const
216 const AstInterface
* pInterface
= dynamic_cast<const AstInterface
*>(this);
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
);
238 pDecl
= resolved
->lookupInInherited(scopedName
);
247 AstDeclaration
* AstScope::lookupPrimitiveType(ExprType type
)
249 AstDeclaration
* pDecl
= NULL
;
250 AstScope
* pScope
= NULL
;
252 pDecl
= scopeAsDecl(this);
255 pScope
= pDecl
->getScope();
257 return pScope
->lookupPrimitiveType(type
);
265 typeName
= OString("short");
268 typeName
= OString("unsigned short");
271 typeName
= OString("long");
274 typeName
= OString("unsigned long");
277 typeName
= OString("hyper");
280 typeName
= OString("unsigned hyper");
283 typeName
= OString("float");
286 typeName
= OString("double");
289 typeName
= OString("char");
292 typeName
= OString("byte");
295 typeName
= OString("boolean");
298 typeName
= OString("any");
301 typeName
= OString("void");
304 typeName
= OString("type");
307 typeName
= OString("string");
311 pDecl
= lookupByNameLocal(typeName
);
313 if ( pDecl
&& (pDecl
->getNodeType() == NT_predefined
) )
315 AstBaseType
* pBaseType
= static_cast<AstBaseType
*>(pDecl
);
317 if ( pBaseType
->getExprType() == type
)
324 AstDeclaration
* AstScope::lookupForAdd(AstDeclaration
* pDecl
)
329 AstDeclaration
* pRetDecl
= lookupByNameLocal(pDecl
->getLocalName());
334 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */