1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: astscope.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_idlc.hxx"
33 #include <idlc/astscope.hxx>
34 #include <idlc/astbasetype.hxx>
35 #ifndef _IDLC_ASTINERFACE_HXX_
36 #include <idlc/astinterface.hxx>
38 #include <idlc/errorhandler.hxx>
41 using namespace ::rtl
;
43 sal_Bool
isGlobal(const OString
& scopedName
)
45 if ((scopedName
.getLength() == 0) || (scopedName
.indexOf(':') == 0))
52 AstScope::AstScope(NodeType nodeType
)
53 : m_nodeType(nodeType
)
63 AstDeclaration
* AstScope::addDeclaration(AstDeclaration
* pDecl
)
65 AstDeclaration
* pDeclaration
= NULL
;
67 if ((pDeclaration
= lookupForAdd(pDecl
)) != NULL
)
69 if (pDecl
->getNodeType() == NT_union_branch
)
71 m_declarations
.push_back(pDecl
);
74 if ( pDecl
->hasAncestor(pDeclaration
) )
76 idlc()->error()->error2(EIDL_REDEF_SCOPE
, pDecl
, pDeclaration
);
79 if ( (pDecl
->getNodeType() == pDeclaration
->getNodeType()) &&
80 (pDecl
->getNodeType() == NT_sequence
81 || pDecl
->getNodeType() == NT_array
82 || pDecl
->getNodeType() == NT_instantiated_struct
) )
86 if ( (pDeclaration
->getNodeType() == NT_interface
)
87 && (pDecl
->getNodeType() == NT_interface
)
88 && !((AstInterface
*)pDeclaration
)->isDefined() )
90 m_declarations
.push_back(pDecl
);
93 if ( (NT_service
== m_nodeType
) &&
94 ( ((pDecl
->getNodeType() == NT_interface_member
)
95 && (pDeclaration
->getNodeType() == NT_interface
)) ||
96 ((pDecl
->getNodeType() == NT_service_member
)
97 && (pDeclaration
->getNodeType() == NT_service
)) )
100 m_declarations
.push_back(pDecl
);
104 idlc()->error()->error2(EIDL_REDEF_SCOPE
, scopeAsDecl(this), pDecl
);
108 m_declarations
.push_back(pDecl
);
112 sal_uInt16
AstScope::getNodeCount(NodeType nodeType
)
114 DeclList::const_iterator iter
= getIteratorBegin();
115 DeclList::const_iterator end
= getIteratorEnd();
116 AstDeclaration
* pDecl
= NULL
;
117 sal_uInt16 count
= 0;
119 while ( iter
!= end
)
122 if ( pDecl
->getNodeType() == nodeType
)
129 AstDeclaration
* AstScope::lookupByName(const OString
& scopedName
)
131 AstDeclaration
* pDecl
= NULL
;
132 AstScope
* pScope
= NULL
;
133 if (scopedName
.getLength() == 0)
136 // If name starts with "::" start look up in global scope
137 if ( isGlobal(scopedName
) )
139 pDecl
= scopeAsDecl(this);
143 pScope
= pDecl
->getScope();
144 // If this is the global scope ...
147 // look up the scopedName part after "::"
148 OString subName
= scopedName
.copy(2);
149 pDecl
= lookupByName(subName
);
151 //return pScope->lookupByName();
153 // OK, not global scope yet, so simply iterate with parent scope
154 pDecl
= pScope
->lookupByName(scopedName
);
158 // The name does not start with "::"
159 // Look up in the local scope and start with the first scope
160 sal_Int32 nIndex
= scopedName
.indexOf(':');
161 OString firstScope
= nIndex
> 0 ? scopedName
.copy(0, nIndex
) : scopedName
;
162 sal_Bool bFindFirstScope
= sal_True
;
163 pDecl
= lookupByNameLocal(firstScope
);
166 bFindFirstScope
= sal_False
;
168 // OK, not found. Go down parent scope chain
169 pDecl
= scopeAsDecl(this);
172 pScope
= pDecl
->getScope();
174 pDecl
= pScope
->lookupByName(scopedName
);
178 // Special case for scope which is an interface. We
179 // have to look in the inherited interfaces as well.
182 if (m_nodeType
== NT_interface
)
183 pDecl
= lookupInInherited(scopedName
);
188 if ( bFindFirstScope
&& (firstScope
!= scopedName
) )
191 sal_Int32 nOffset
= 2;
194 pScope
= declAsScope(pDecl
);
197 pDecl
= pScope
->lookupByNameLocal(scopedName
.getToken(nOffset
, ':', i
));
206 // last try if is not the global scope and the scopeName isn't specify global too
207 pDecl
= scopeAsDecl(this);
208 if ( pDecl
&& (pDecl
->getLocalName() != "") )
210 pScope
= pDecl
->getScope();
212 pDecl
= pScope
->lookupByName(scopedName
);
224 AstDeclaration
* AstScope::lookupByNameLocal(const OString
& name
) const
226 DeclList::const_iterator
iter(m_declarations
.begin());
227 DeclList::const_iterator
end(m_declarations
.end());
228 AstDeclaration
* pDecl
= NULL
;
230 while ( iter
!= end
)
233 if ( pDecl
->getLocalName() == name
)
240 AstDeclaration
* AstScope::lookupInInherited(const OString
& scopedName
) const
242 AstInterface
* pInterface
= (AstInterface
*)this;
247 // Can't look in an interface which was not yet defined
248 if ( !pInterface
->getScope() )
250 idlc()->error()->forwardLookupError(pInterface
, scopedName
);
253 // OK, loop through inherited interfaces. Stop when you find it
254 AstInterface::InheritedInterfaces::const_iterator
iter(
255 pInterface
->getAllInheritedInterfaces().begin());
256 AstInterface::InheritedInterfaces::const_iterator
end(
257 pInterface
->getAllInheritedInterfaces().end());
258 while ( iter
!= end
)
260 AstInterface
const * resolved
= iter
->getResolved();
261 AstDeclaration
* pDecl
= resolved
->lookupByNameLocal(scopedName
);
264 pDecl
= resolved
->lookupInInherited(scopedName
);
273 AstDeclaration
* AstScope::lookupPrimitiveType(ExprType type
)
275 AstDeclaration
* pDecl
= NULL
;
276 AstScope
* pScope
= NULL
;
277 AstBaseType
* pBaseType
= NULL
;
279 pDecl
= scopeAsDecl(this);
282 pScope
= pDecl
->getScope();
284 return pScope
->lookupPrimitiveType(type
);
292 typeName
= OString("short");
295 typeName
= OString("unsigned short");
298 typeName
= OString("long");
301 typeName
= OString("unsigned long");
304 typeName
= OString("hyper");
307 typeName
= OString("unsigned hyper");
310 typeName
= OString("float");
313 typeName
= OString("double");
316 typeName
= OString("char");
319 typeName
= OString("byte");
322 typeName
= OString("boolean");
325 typeName
= OString("any");
328 typeName
= OString("void");
331 typeName
= OString("type");
334 typeName
= OString("string");
338 pDecl
= lookupByNameLocal(typeName
);
340 if ( pDecl
&& (pDecl
->getNodeType() == NT_predefined
) )
342 pBaseType
= (AstBaseType
*)pDecl
;
344 if ( pBaseType
->getExprType() == type
)
351 AstDeclaration
* AstScope::lookupForAdd(AstDeclaration
* pDecl
)
356 AstDeclaration
* pRetDecl
= lookupByNameLocal(pDecl
->getLocalName());