[cfe-commits] r49855 - in /cfe/trunk: Driver/RewriteObjC.cpp include/clang/AST/ASTContext.h include/clang/AST/Decl.h include/clang/AST/DeclBase.h lib/AST/ASTContext.cpp lib/AST/Decl.cpp lib/AST/DeclSerialization.cpp lib/Sema/Sema.cpp lib/Sema/SemaDecl.cpp
Argiris Kirtzidis
akyrtzi at gmail.com
Thu Apr 17 07:40:23 PDT 2008
Author: akirtzidis
Date: Thu Apr 17 09:40:12 2008
New Revision: 49855
URL: http://llvm.org/viewvc/llvm-project?rev=49855&view=rev
Log:
Addition of TranslationUnitDecl to the AST:
-Added TranslationUnitDecl class to serve as top declaration context
-ASTContext gets a TUDecl member and a getTranslationUnitDecl() function
-All ScopedDecls get the TUDecl as DeclContext when declared at global scope
Modified:
cfe/trunk/Driver/RewriteObjC.cpp
cfe/trunk/include/clang/AST/ASTContext.h
cfe/trunk/include/clang/AST/Decl.h
cfe/trunk/include/clang/AST/DeclBase.h
cfe/trunk/lib/AST/ASTContext.cpp
cfe/trunk/lib/AST/Decl.cpp
cfe/trunk/lib/AST/DeclSerialization.cpp
cfe/trunk/lib/Sema/Sema.cpp
cfe/trunk/lib/Sema/SemaDecl.cpp
Modified: cfe/trunk/Driver/RewriteObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/RewriteObjC.cpp?rev=49855&r1=49854&r2=49855&view=diff
==============================================================================
--- cfe/trunk/Driver/RewriteObjC.cpp (original)
+++ cfe/trunk/Driver/RewriteObjC.cpp Thu Apr 17 09:40:12 2008
@@ -42,6 +42,7 @@
ASTContext *Context;
SourceManager *SM;
+ TranslationUnitDecl *TUDecl;
unsigned MainFileID;
const char *MainFileStart, *MainFileEnd;
SourceLocation LastIncLoc;
@@ -255,6 +256,7 @@
void RewriteObjC::Initialize(ASTContext &context) {
Context = &context;
SM = &Context->getSourceManager();
+ TUDecl = Context->getTranslationUnitDecl();
MsgSendFunctionDecl = 0;
MsgSendSuperFunctionDecl = 0;
MsgSendStretFunctionDecl = 0;
@@ -820,7 +822,7 @@
std::string RecName = clsDeclared->getIdentifier()->getName();
RecName += "_IMPL";
IdentifierInfo *II = &Context->Idents.get(RecName.c_str());
- RecordDecl *RD = RecordDecl::Create(*Context, Decl::Struct, NULL,
+ RecordDecl *RD = RecordDecl::Create(*Context, Decl::Struct, TUDecl,
SourceLocation(), II, 0);
assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl");
QualType castT = Context->getPointerType(Context->getTagDeclType(RD));
@@ -1577,7 +1579,7 @@
QualType getFuncType = Context->getFunctionType(Context->getObjCSelType(),
&ArgTys[0], ArgTys.size(),
false /*isVariadic*/);
- SelGetUidFunctionDecl = FunctionDecl::Create(*Context, NULL,
+ SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
SourceLocation(),
SelGetUidIdent, getFuncType,
FunctionDecl::Extern, false, 0);
@@ -1592,7 +1594,7 @@
QualType getFuncType = Context->getFunctionType(Context->getObjCProtoType(),
&ArgTys[0], ArgTys.size(),
false /*isVariadic*/);
- GetProtocolFunctionDecl = FunctionDecl::Create(*Context, NULL,
+ GetProtocolFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
SourceLocation(),
SelGetProtoIdent, getFuncType,
FunctionDecl::Extern, false, 0);
@@ -1620,7 +1622,7 @@
QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(),
&ArgTys[0], ArgTys.size(),
false);
- SuperContructorFunctionDecl = FunctionDecl::Create(*Context, NULL,
+ SuperContructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
SourceLocation(),
msgSendIdent, msgSendType,
FunctionDecl::Extern, false, 0);
@@ -1639,7 +1641,7 @@
QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(),
&ArgTys[0], ArgTys.size(),
true /*isVariadic*/);
- MsgSendFunctionDecl = FunctionDecl::Create(*Context, NULL,
+ MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
SourceLocation(),
msgSendIdent, msgSendType,
FunctionDecl::Extern, false, 0);
@@ -1649,7 +1651,7 @@
void RewriteObjC::SynthMsgSendSuperFunctionDecl() {
IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper");
llvm::SmallVector<QualType, 16> ArgTys;
- RecordDecl *RD = RecordDecl::Create(*Context, Decl::Struct, NULL,
+ RecordDecl *RD = RecordDecl::Create(*Context, Decl::Struct, TUDecl,
SourceLocation(),
&Context->Idents.get("objc_super"), 0);
QualType argT = Context->getPointerType(Context->getTagDeclType(RD));
@@ -1661,7 +1663,7 @@
QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(),
&ArgTys[0], ArgTys.size(),
true /*isVariadic*/);
- MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, NULL,
+ MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
SourceLocation(),
msgSendIdent, msgSendType,
FunctionDecl::Extern, false, 0);
@@ -1680,7 +1682,7 @@
QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(),
&ArgTys[0], ArgTys.size(),
true /*isVariadic*/);
- MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, NULL,
+ MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
SourceLocation(),
msgSendIdent, msgSendType,
FunctionDecl::Extern, false, 0);
@@ -1692,7 +1694,7 @@
IdentifierInfo *msgSendIdent =
&Context->Idents.get("objc_msgSendSuper_stret");
llvm::SmallVector<QualType, 16> ArgTys;
- RecordDecl *RD = RecordDecl::Create(*Context, Decl::Struct, NULL,
+ RecordDecl *RD = RecordDecl::Create(*Context, Decl::Struct, TUDecl,
SourceLocation(),
&Context->Idents.get("objc_super"), 0);
QualType argT = Context->getPointerType(Context->getTagDeclType(RD));
@@ -1704,7 +1706,7 @@
QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(),
&ArgTys[0], ArgTys.size(),
true /*isVariadic*/);
- MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, NULL,
+ MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
SourceLocation(),
msgSendIdent, msgSendType,
FunctionDecl::Extern, false, 0);
@@ -1723,7 +1725,7 @@
QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(),
&ArgTys[0], ArgTys.size(),
true /*isVariadic*/);
- MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, NULL,
+ MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
SourceLocation(),
msgSendIdent, msgSendType,
FunctionDecl::Extern, false, 0);
@@ -1738,7 +1740,7 @@
QualType getClassType = Context->getFunctionType(Context->getObjCIdType(),
&ArgTys[0], ArgTys.size(),
false /*isVariadic*/);
- GetClassFunctionDecl = FunctionDecl::Create(*Context, NULL,
+ GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
SourceLocation(),
getClassIdent, getClassType,
FunctionDecl::Extern, false, 0);
@@ -1753,7 +1755,7 @@
QualType getClassType = Context->getFunctionType(Context->getObjCIdType(),
&ArgTys[0], ArgTys.size(),
false /*isVariadic*/);
- GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, NULL,
+ GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl,
SourceLocation(),
getClassIdent, getClassType,
FunctionDecl::Extern, false, 0);
@@ -1776,7 +1778,7 @@
// The minus 2 removes the begin/end double quotes.
Preamble += utostr(prettyBuf.str().size()-2) + "};\n";
- VarDecl *NewVD = VarDecl::Create(*Context, NULL, SourceLocation(),
+ VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(),
&Context->Idents.get(S.c_str()), strType,
VarDecl::Static, NULL);
DeclRefExpr *DRE = new DeclRefExpr(NewVD, strType, SourceLocation());
@@ -1824,7 +1826,7 @@
// struct objc_super { struct objc_object *receiver; struct objc_class *super; };
QualType RewriteObjC::getSuperStructType() {
if (!SuperStructDecl) {
- SuperStructDecl = RecordDecl::Create(*Context, Decl::Struct, NULL,
+ SuperStructDecl = RecordDecl::Create(*Context, Decl::Struct, TUDecl,
SourceLocation(),
&Context->Idents.get("objc_super"), 0);
QualType FieldTypes[2];
@@ -1847,7 +1849,7 @@
QualType RewriteObjC::getConstantStringStructType() {
if (!ConstantStringDecl) {
- ConstantStringDecl = RecordDecl::Create(*Context, Decl::Struct, NULL,
+ ConstantStringDecl = RecordDecl::Create(*Context, Decl::Struct, TUDecl,
SourceLocation(),
&Context->Idents.get("__NSConstantStringImpl"), 0);
QualType FieldTypes[4];
@@ -2988,3 +2990,4 @@
}
}
+
Modified: cfe/trunk/include/clang/AST/ASTContext.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ASTContext.h?rev=49855&r1=49854&r2=49855&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/ASTContext.h (original)
+++ cfe/trunk/include/clang/AST/ASTContext.h Thu Apr 17 09:40:12 2008
@@ -75,6 +75,8 @@
QualType ObjCConstantStringType;
RecordDecl *CFConstantStringTypeDecl;
+ TranslationUnitDecl *TUDecl;
+
SourceManager &SourceMgr;
llvm::MallocAllocator Allocator;
public:
@@ -88,7 +90,9 @@
FullSourceLoc getFullLoc(SourceLocation Loc) const {
return FullSourceLoc(Loc,SourceMgr);
}
-
+
+ TranslationUnitDecl *getTranslationUnitDecl() const { return TUDecl; }
+
/// This is intentionally not serialized. It is populated by the
/// ASTContext ctor, and there are no external pointers/references to
/// internal variables of BuiltinInfo.
@@ -113,6 +117,7 @@
if (size_reserve > 0) Types.reserve(size_reserve);
InitBuiltinTypes();
BuiltinInfo.InitializeBuiltins(idents, Target);
+ TUDecl = TranslationUnitDecl::Create(*this);
}
~ASTContext();
Modified: cfe/trunk/include/clang/AST/Decl.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Decl.h?rev=49855&r1=49854&r2=49855&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Decl.h (original)
+++ cfe/trunk/include/clang/AST/Decl.h Thu Apr 17 09:40:12 2008
@@ -22,6 +22,30 @@
class StringLiteral;
class IdentifierInfo;
+/// TranslationUnitDecl - The top declaration context.
+/// FIXME: The TranslationUnit class should probably be modified to serve as
+/// the top decl context. It would have ownership of the top decls so that the
+/// AST is self-contained and easily de/serializable.
+class TranslationUnitDecl : public Decl, public DeclContext {
+ TranslationUnitDecl()
+ : Decl(TranslationUnit, SourceLocation()),
+ DeclContext(TranslationUnit) {}
+public:
+ static TranslationUnitDecl *Create(ASTContext &C);
+ // Implement isa/cast/dyncast/etc.
+ static bool classof(const Decl *D) { return D->getKind() == TranslationUnit; }
+ static bool classof(const TranslationUnitDecl *D) { return true; }
+
+protected:
+ /// EmitImpl - Serialize this TranslationUnitDecl. Called by Decl::Emit.
+ virtual void EmitImpl(llvm::Serializer& S) const;
+
+ /// CreateImpl - Deserialize a TranslationUnitDecl. Called by Decl::Create.
+ static TranslationUnitDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C);
+
+ friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C);
+};
+
/// NamedDecl - This represents a decl with an identifier for a name. Many
/// decls have names, but not ObjCMethodDecl, @class, etc.
class NamedDecl : public Decl {
@@ -190,8 +214,7 @@
bool isFileVarDecl() const {
if (getKind() != Decl::Var)
return false;
- // FIXME: change when TranlationUnitDecl is added as a declaration context.
- if (!getDeclContext())
+ if (isa<TranslationUnitDecl>(getDeclContext()))
return true;
return false;
}
Modified: cfe/trunk/include/clang/AST/DeclBase.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/DeclBase.h?rev=49855&r1=49854&r2=49855&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/DeclBase.h (original)
+++ cfe/trunk/include/clang/AST/DeclBase.h Thu Apr 17 09:40:12 2008
@@ -19,6 +19,7 @@
#include "clang/Basic/SourceLocation.h"
namespace clang {
+class TranslationUnitDecl;
class FunctionDecl;
class ObjCMethodDecl;
class EnumDecl;
@@ -35,6 +36,7 @@
// enums below. The commented out names are abstract class names.
// Decl
+ TranslationUnit,
// NamedDecl
Field,
ObjCIvar,
@@ -195,6 +197,7 @@
/// DeclContext - This is used only as base class of specific decl types that
/// can act as declaration contexts. These decls are:
///
+/// TranslationUnitDecl
/// FunctionDecl
/// ObjCMethodDecl
/// EnumDecl
@@ -216,6 +219,8 @@
static To *CastTo(const From *D) {
Decl::Kind DK = KindTrait<From>::getKind(D);
switch(DK) {
+ case Decl::TranslationUnit:
+ return static_cast<TranslationUnitDecl*>(const_cast<From*>(D));
case Decl::Function:
return static_cast<FunctionDecl*>(const_cast<From*>(D));
case Decl::ObjCMethod:
@@ -256,6 +261,7 @@
static bool classof(const Decl *D) {
switch (D->getKind()) {
+ case Decl::TranslationUnit:
case Decl::Function:
case Decl::ObjCMethod:
case Decl::ObjCInterface:
@@ -266,6 +272,7 @@
}
}
static bool classof(const DeclContext *D) { return true; }
+ static bool classof(const TranslationUnitDecl *D) { return true; }
static bool classof(const FunctionDecl *D) { return true; }
static bool classof(const ObjCMethodDecl *D) { return true; }
static bool classof(const EnumDecl *D) { return true; }
Modified: cfe/trunk/lib/AST/ASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ASTContext.cpp?rev=49855&r1=49854&r2=49855&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ASTContext.cpp (original)
+++ cfe/trunk/lib/AST/ASTContext.cpp Thu Apr 17 09:40:12 2008
@@ -1175,7 +1175,7 @@
QualType ASTContext::getCFConstantStringType() {
if (!CFConstantStringTypeDecl) {
CFConstantStringTypeDecl =
- RecordDecl::Create(*this, Decl::Struct, NULL, SourceLocation(),
+ RecordDecl::Create(*this, Decl::Struct, TUDecl, SourceLocation(),
&Idents.get("NSConstantString"), 0);
QualType FieldTypes[4];
@@ -1727,6 +1727,8 @@
I!=E;++I)
(*I)->Emit(S);
+ S.EmitOwnedPtr(TUDecl);
+
// FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
}
@@ -1743,6 +1745,8 @@
for (unsigned i = 0; i < size_reserve; ++i)
Type::Create(*A,i,D);
+ A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
+
// FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
return A;
Modified: cfe/trunk/lib/AST/Decl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Decl.cpp?rev=49855&r1=49854&r2=49855&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Decl.cpp (original)
+++ cfe/trunk/lib/AST/Decl.cpp Thu Apr 17 09:40:12 2008
@@ -198,6 +198,7 @@
case ObjCPropertyImpl: nObjCPropertyImplDecl++; break;
case LinkageSpec: nLinkageSpecDecl++; break;
case FileScopeAsm: nFileScopeAsmDecl++; break;
+ case TranslationUnit: break;
}
}
@@ -205,6 +206,11 @@
// Decl Allocation/Deallocation Method Implementations
//===----------------------------------------------------------------------===//
+TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
+ void *Mem = C.getAllocator().Allocate<TranslationUnitDecl>();
+ return new (Mem) TranslationUnitDecl();
+}
+
VarDecl *VarDecl::Create(ASTContext &C, DeclContext *CD,
SourceLocation L,
IdentifierInfo *Id, QualType T,
@@ -213,7 +219,6 @@
return new (Mem) VarDecl(Var, CD, L, Id, T, S, PrevDecl);
}
-
ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *CD,
SourceLocation L, IdentifierInfo *Id,
QualType T, StorageClass S,
@@ -329,6 +334,7 @@
void Decl::Destroy(ASTContext& C) const {
switch (getKind()) {
+ CASE(TranslationUnit);
CASE(Field);
CASE(ObjCIvar);
CASE(ObjCCategory);
Modified: cfe/trunk/lib/AST/DeclSerialization.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclSerialization.cpp?rev=49855&r1=49854&r2=49855&view=diff
==============================================================================
--- cfe/trunk/lib/AST/DeclSerialization.cpp (original)
+++ cfe/trunk/lib/AST/DeclSerialization.cpp Thu Apr 17 09:40:12 2008
@@ -41,6 +41,9 @@
assert (false && "Not implemented.");
break;
+ case TranslationUnit:
+ return TranslationUnitDecl::CreateImpl(D, C);
+
case Var:
return VarDecl::CreateImpl(D, C);
@@ -192,6 +195,26 @@
}
//===----------------------------------------------------------------------===//
+// TranslationUnitDecl Serialization.
+//===----------------------------------------------------------------------===//
+
+void TranslationUnitDecl::EmitImpl(llvm::Serializer& S) const
+{
+ Decl::EmitInRec(S);
+}
+
+TranslationUnitDecl* TranslationUnitDecl::CreateImpl(Deserializer& D,
+ ASTContext& C) {
+ void *Mem = C.getAllocator().Allocate<TranslationUnitDecl>();
+ TranslationUnitDecl* decl =
+ new (Mem) TranslationUnitDecl();
+
+ decl->Decl::ReadInRec(D, C);
+
+ return decl;
+}
+
+//===----------------------------------------------------------------------===//
// VarDecl Serialization.
//===----------------------------------------------------------------------===//
Modified: cfe/trunk/lib/Sema/Sema.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.cpp?rev=49855&r1=49854&r2=49855&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/Sema.cpp (original)
+++ cfe/trunk/lib/Sema/Sema.cpp Thu Apr 17 09:40:12 2008
@@ -42,6 +42,7 @@
void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
TUScope = S;
+ CurContext = Context.getTranslationUnitDecl();
if (!PP.getLangOptions().ObjC1) return;
TypedefType *t;
@@ -93,14 +94,16 @@
// FIXME: Move this initialization up to Sema::ActOnTranslationUnitScope()
// and make sure the decls get inserted into TUScope!
if (PP.getLangOptions().ObjC1) {
+ TranslationUnitDecl *TUDecl = Context.getTranslationUnitDecl();
+
// Synthesize "typedef struct objc_class *Class;"
RecordDecl *ClassTag = RecordDecl::Create(Context, Decl::Struct,
- NULL,
+ TUDecl,
SourceLocation(),
&IT.get("objc_class"), 0);
QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag));
TypedefDecl *ClassTypedef =
- TypedefDecl::Create(Context, NULL, SourceLocation(),
+ TypedefDecl::Create(Context, TUDecl, SourceLocation(),
&Context.Idents.get("Class"), ClassT, 0);
Context.setObjCClassType(ClassTypedef);
@@ -113,14 +116,14 @@
// Synthesize "typedef struct objc_object { Class isa; } *id;"
RecordDecl *ObjectTag =
- RecordDecl::Create(Context, Decl::Struct, NULL,
+ RecordDecl::Create(Context, Decl::Struct, TUDecl,
SourceLocation(),
&IT.get("objc_object"), 0);
FieldDecl *IsaDecl = FieldDecl::Create(Context, SourceLocation(), 0,
Context.getObjCClassType());
ObjectTag->defineBody(&IsaDecl, 1);
QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag));
- TypedefDecl *IdTypedef = TypedefDecl::Create(Context, NULL,
+ TypedefDecl *IdTypedef = TypedefDecl::Create(Context, TUDecl,
SourceLocation(),
&Context.Idents.get("id"),
ObjT, 0);
Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=49855&r1=49854&r2=49855&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Thu Apr 17 09:40:12 2008
@@ -43,14 +43,18 @@
}
void Sema::PushDeclContext(DeclContext *CD) {
- assert(CD->getParent() == CurContext &&
+ assert( ( (CD->isFunctionOrMethod() && isa<TranslationUnitDecl>(CurContext))
+ || CD->getParent() == CurContext ) &&
"The next DeclContext should be directly contained in the current one.");
CurContext = CD;
}
void Sema::PopDeclContext() {
assert(CurContext && "DeclContext imbalance!");
- CurContext = CurContext->getParent();
+ // If CurContext is a ObjC method, getParent() will return NULL.
+ CurContext = CurContext->isFunctionOrMethod()
+ ? Context.getTranslationUnitDecl()
+ : CurContext->getParent();
}
/// Add this decl to the scope shadowed decl chains.
More information about the cfe-commits
mailing list