[cfe-commits] r61998 - in /cfe/trunk: include/clang/AST/Decl.h lib/AST/Decl.cpp lib/AST/DeclBase.cpp lib/Sema/SemaDecl.cpp
Douglas Gregor
dgregor at apple.com
Fri Jan 9 10:51:29 PST 2009
Author: dgregor
Date: Fri Jan 9 12:51:29 2009
New Revision: 61998
URL: http://llvm.org/viewvc/llvm-project?rev=61998&view=rev
Log:
Make sure that ScopedDecls passed to DeclContext::addDecl are added into their lexical context
Modified:
cfe/trunk/include/clang/AST/Decl.h
cfe/trunk/lib/AST/Decl.cpp
cfe/trunk/lib/AST/DeclBase.cpp
cfe/trunk/lib/Sema/SemaDecl.cpp
Modified: cfe/trunk/include/clang/AST/Decl.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Decl.h?rev=61998&r1=61997&r2=61998&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Decl.h (original)
+++ cfe/trunk/include/clang/AST/Decl.h Fri Jan 9 12:51:29 2009
@@ -144,11 +144,15 @@
protected:
ScopedDecl(Kind DK, DeclContext *DC, SourceLocation L,
DeclarationName N, ScopedDecl *PrevDecl = 0)
- : NamedDecl(DK, L, N), NextDeclarator(PrevDecl),
+ : NamedDecl(DK, L, N), NextDeclarator(PrevDecl),
DeclCtx(reinterpret_cast<uintptr_t>(DC)) {}
virtual ~ScopedDecl();
-
+
+ /// setDeclContext - Set both the semantic and lexical DeclContext
+ /// to DC.
+ void setDeclContext(DeclContext *DC);
+
public:
const DeclContext *getDeclContext() const {
if (isInSemaDC())
@@ -532,6 +536,12 @@
QualType getOriginalType() const;
+ /// setOwningFunction - Sets the function declaration that owns this
+ /// ParmVarDecl. Since ParmVarDecls are often created before the
+ /// FunctionDecls that own them, this routine is required to update
+ /// the DeclContext appropriately.
+ void setOwningFunction(DeclContext *FD) { setDeclContext(FD); }
+
// Implement isa/cast/dyncast/etc.
static bool classof(const Decl *D) {
return (D->getKind() == ParmVar ||
Modified: cfe/trunk/lib/AST/Decl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Decl.cpp?rev=61998&r1=61997&r2=61998&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Decl.cpp (original)
+++ cfe/trunk/lib/AST/Decl.cpp Fri Jan 9 12:51:29 2009
@@ -164,6 +164,13 @@
// ScopedDecl Implementation
//===----------------------------------------------------------------------===//
+void ScopedDecl::setDeclContext(DeclContext *DC) {
+ if (isOutOfSemaDC())
+ delete getMultipleDC();
+
+ DeclCtx = reinterpret_cast<uintptr_t>(DC);
+}
+
void ScopedDecl::setLexicalDeclContext(DeclContext *DC) {
if (DC == getLexicalDeclContext())
return;
Modified: cfe/trunk/lib/AST/DeclBase.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclBase.cpp?rev=61998&r1=61997&r2=61998&view=diff
==============================================================================
--- cfe/trunk/lib/AST/DeclBase.cpp (original)
+++ cfe/trunk/lib/AST/DeclBase.cpp Fri Jan 9 12:51:29 2009
@@ -514,6 +514,7 @@
}
void DeclContext::addDecl(ASTContext &Context, ScopedDecl *D, bool AllowLookup) {
+ assert(D->getLexicalDeclContext() == this && "Decl inserted into wrong lexical context");
Decls.push_back(D);
if (AllowLookup)
D->getDeclContext()->insert(Context, D);
@@ -599,7 +600,6 @@
if (LookupPtr.getPointer())
insertImpl(D);
-
// If we are a transparent context, insert into our parent context,
// too. This operation is recursive.
if (isTransparentContext())
Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=61998&r1=61997&r2=61998&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Fri Jan 9 12:51:29 2009
@@ -441,7 +441,13 @@
// TUScope is the translation-unit scope to insert this function into.
+ // FIXME: This is hideous. We need to teach PushOnScopeChains to
+ // relate Scopes to DeclContexts, and probably eliminate CurContext
+ // entirely, but we're not there yet.
+ DeclContext *SavedContext = CurContext;
+ CurContext = Context.getTranslationUnitDecl();
PushOnScopeChains(New, TUScope);
+ CurContext = SavedContext;
return New;
}
@@ -2705,6 +2711,8 @@
// Introduce our parameters into the function scope
for (unsigned p = 0, NumParams = FD->getNumParams(); p < NumParams; ++p) {
ParmVarDecl *Param = FD->getParamDecl(p);
+ Param->setOwningFunction(FD);
+
// If this has an identifier, add it to the scope stack.
if (Param->getIdentifier())
PushOnScopeChains(Param, FnBodyScope);
More information about the cfe-commits
mailing list