From: Chris Lattner Date: Sat, 15 Mar 2008 06:12:44 +0000 (+0000) Subject: start switching decls over to using an allocator controlled by ASTContext. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6c2b6eb8d836da19007f7540709e16d5e39a1cba;p=clang start switching decls over to using an allocator controlled by ASTContext. Right now only some ctors are switched over. I need to switch them all over so I can change the dtor over. This lets us experiment with region allocation and other things in the future. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@48390 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/AST/ASTContext.cpp b/AST/ASTContext.cpp index 3d312fa4a8..ad54010c50 100644 --- a/AST/ASTContext.cpp +++ b/AST/ASTContext.cpp @@ -1047,9 +1047,9 @@ QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) { // getCFConstantStringType - Return the type used for constant CFStrings. QualType ASTContext::getCFConstantStringType() { if (!CFConstantStringTypeDecl) { - CFConstantStringTypeDecl = new RecordDecl(Decl::Struct, SourceLocation(), - &Idents.get("NSConstantString"), - 0); + CFConstantStringTypeDecl = + RecordDecl::Create(Decl::Struct, SourceLocation(), + &Idents.get("NSConstantString"), 0, *this); QualType FieldTypes[4]; // const int *isa; diff --git a/AST/Decl.cpp b/AST/Decl.cpp index 32ca4198f5..aad8f212bf 100644 --- a/AST/Decl.cpp +++ b/AST/Decl.cpp @@ -13,6 +13,7 @@ #include "clang/AST/Decl.h" #include "clang/AST/DeclObjC.h" +#include "clang/AST/ASTContext.h" #include "clang/AST/Attr.h" #include "clang/Basic/IdentifierTable.h" #include "llvm/ADT/DenseMap.h" @@ -200,6 +201,37 @@ void Decl::addDeclKind(Kind k) { } } +//===----------------------------------------------------------------------===// +// Decl Allocation/Deallocation Method Implementations +//===----------------------------------------------------------------------===// + +EnumConstantDecl *EnumConstantDecl::Create(SourceLocation L, IdentifierInfo *Id, + QualType T, Expr *E, + const llvm::APSInt &V, + ScopedDecl *PrevDecl, ASTContext &C){ + void *Mem = C.getAllocator().Allocate(); + return new (Mem) EnumConstantDecl(L, Id, T, E, V, PrevDecl); +} + +TypedefDecl *TypedefDecl::Create(SourceLocation L, IdentifierInfo *Id, + QualType T, ScopedDecl *PD, ASTContext &C) { + void *Mem = C.getAllocator().Allocate(); + return new (Mem) TypedefDecl(L, Id, T, PD); +} + +EnumDecl *EnumDecl::Create(SourceLocation L, IdentifierInfo *Id, + ScopedDecl *PrevDecl, ASTContext &C) { + void *Mem = C.getAllocator().Allocate(); + return new (Mem) EnumDecl(L, Id, PrevDecl); +} + +RecordDecl *RecordDecl::Create(Kind DK, SourceLocation L, IdentifierInfo *Id, + ScopedDecl *PrevDecl, ASTContext &C) { + void *Mem = C.getAllocator().Allocate(); + return new (Mem) RecordDecl(DK, L, Id, PrevDecl); +} + + //===----------------------------------------------------------------------===// // Decl Implementation //===----------------------------------------------------------------------===// @@ -292,7 +324,6 @@ FieldDecl* RecordDecl::getMember(IdentifierInfo *name) { return 0; } - //===----------------------------------------------------------------------===// // Objective-C Decl Implementation //===----------------------------------------------------------------------===// diff --git a/Driver/RewriteTest.cpp b/Driver/RewriteTest.cpp index e947f243f2..652ce1c82c 100644 --- a/Driver/RewriteTest.cpp +++ b/Driver/RewriteTest.cpp @@ -796,7 +796,7 @@ Stmt *RewriteTest::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV) { std::string RecName = clsDeclared->getIdentifier()->getName(); RecName += "_IMPL"; IdentifierInfo *II = &Context->Idents.get(RecName.c_str()); - RecordDecl *RD = new RecordDecl(Decl::Struct, SourceLocation(), II, 0); + RecordDecl *RD = RecordDecl::Create(Decl::Struct, SourceLocation(), II, 0, *Context); assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); CastExpr *castExpr = new CastExpr(castT, IV->getBase(), SourceLocation()); @@ -1622,8 +1622,9 @@ void RewriteTest::SynthMsgSendFunctionDecl() { void RewriteTest::SynthMsgSendSuperFunctionDecl() { IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper"); llvm::SmallVector ArgTys; - RecordDecl *RD = new RecordDecl(Decl::Struct, SourceLocation(), - &Context->Idents.get("objc_super"), 0); + RecordDecl *RD = RecordDecl::Create(Decl::Struct, SourceLocation(), + &Context->Idents.get("objc_super"), 0, + *Context); QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); ArgTys.push_back(argT); @@ -1662,8 +1663,9 @@ void RewriteTest::SynthMsgSendSuperStretFunctionDecl() { IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper_stret"); llvm::SmallVector ArgTys; - RecordDecl *RD = new RecordDecl(Decl::Struct, SourceLocation(), - &Context->Idents.get("objc_super"), 0); + RecordDecl *RD = RecordDecl::Create(Decl::Struct, SourceLocation(), + &Context->Idents.get("objc_super"), 0, + *Context); QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); ArgTys.push_back(argT); @@ -1758,35 +1760,41 @@ Stmt *RewriteTest::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { ObjCInterfaceDecl *RewriteTest::isSuperReceiver(Expr *recExpr) { // check if we are sending a message to 'super' - if (CurMethodDecl && CurMethodDecl->isInstance()) { - if (CastExpr *CE = dyn_cast(recExpr)) { - if (DeclRefExpr *DRE = dyn_cast(CE->getSubExpr())) { - if (ParmVarDecl *PVD = dyn_cast(DRE->getDecl())) { - if (!strcmp(PVD->getName(), "self")) { - // is this id type? - if (CE->getType()->isObjCQualifiedIdType()) - return 0; - if (const PointerType *PT = CE->getType()->getAsPointerType()) { - if (ObjCInterfaceType *IT = - dyn_cast(PT->getPointeeType())) { - if (IT->getDecl() == - CurMethodDecl->getClassInterface()->getSuperClass()) - return IT->getDecl(); - } - } - } - } - } - } - } - return 0; + if (!CurMethodDecl || !CurMethodDecl->isInstance()) return 0; + + CastExpr *CE = dyn_cast(recExpr); + if (!CE) return 0; + + DeclRefExpr *DRE = dyn_cast(CE->getSubExpr()); + if (!DRE) return 0; + + ParmVarDecl *PVD = dyn_cast(DRE->getDecl()); + if (!PVD) return 0; + + if (strcmp(PVD->getName(), "self") != 0) + return 0; + + // is this id type? + if (CE->getType()->isObjCQualifiedIdType()) + return 0; + const PointerType *PT = CE->getType()->getAsPointerType(); + if (!PT) return 0; + + ObjCInterfaceType *IT = dyn_cast(PT->getPointeeType()); + if (!IT) return 0; + + if (IT->getDecl() != CurMethodDecl->getClassInterface()->getSuperClass()) + return 0; + + return IT->getDecl(); } // struct objc_super { struct objc_object *receiver; struct objc_class *super; }; QualType RewriteTest::getSuperStructType() { if (!SuperStructDecl) { - SuperStructDecl = new RecordDecl(Decl::Struct, SourceLocation(), - &Context->Idents.get("objc_super"), 0); + SuperStructDecl = RecordDecl::Create(Decl::Struct, SourceLocation(), + &Context->Idents.get("objc_super"), 0, + *Context); QualType FieldTypes[2]; // struct objc_object *receiver; @@ -1806,8 +1814,9 @@ QualType RewriteTest::getSuperStructType() { QualType RewriteTest::getConstantStringStructType() { if (!ConstantStringDecl) { - ConstantStringDecl = new RecordDecl(Decl::Struct, SourceLocation(), - &Context->Idents.get("__NSConstantStringImpl"), 0); + ConstantStringDecl = RecordDecl::Create(Decl::Struct, SourceLocation(), + &Context->Idents.get("__NSConstantStringImpl"), 0, + *Context); QualType FieldTypes[4]; // struct objc_object *receiver; diff --git a/Sema/Sema.cpp b/Sema/Sema.cpp index 5b7a3094d1..d8549198be 100644 --- a/Sema/Sema.cpp +++ b/Sema/Sema.cpp @@ -59,15 +59,16 @@ void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) { TUScope->AddDecl(IDecl); // Synthesize "typedef struct objc_selector *SEL;" - RecordDecl *SelTag = new RecordDecl(Decl::Struct, SourceLocation(), - &Context.Idents.get("objc_selector"), 0); + RecordDecl *SelTag = RecordDecl::Create(Decl::Struct, SourceLocation(), + &Context.Idents.get("objc_selector"), + 0, Context); SelTag->getIdentifier()->setFETokenInfo(SelTag); TUScope->AddDecl(SelTag); QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag)); - TypedefDecl *SelTypedef = new TypedefDecl(SourceLocation(), - &Context.Idents.get("SEL"), - SelT, 0); + TypedefDecl *SelTypedef = TypedefDecl::Create(SourceLocation(), + &Context.Idents.get("SEL"), + SelT, 0, Context); SelTypedef->getIdentifier()->setFETokenInfo(SelTypedef); TUScope->AddDecl(SelTypedef); Context.setObjCSelType(SelTypedef); @@ -96,12 +97,12 @@ Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer) // and make sure the decls get inserted into TUScope! if (PP.getLangOptions().ObjC1) { // Synthesize "typedef struct objc_class *Class;" - RecordDecl *ClassTag = new RecordDecl(Decl::Struct, SourceLocation(), - &IT.get("objc_class"), 0); + RecordDecl *ClassTag = RecordDecl::Create(Decl::Struct, SourceLocation(), + &IT.get("objc_class"), 0,Context); QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag)); - TypedefDecl *ClassTypedef = new TypedefDecl(SourceLocation(), - &Context.Idents.get("Class"), - ClassT, 0); + TypedefDecl *ClassTypedef = + TypedefDecl::Create(SourceLocation(), &Context.Idents.get("Class"), + ClassT, 0, Context); Context.setObjCClassType(ClassTypedef); // Synthesize "@class Protocol; @@ -110,15 +111,16 @@ Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer) Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl)); // Synthesize "typedef struct objc_object { Class isa; } *id;" - RecordDecl *ObjectTag = new RecordDecl(Decl::Struct, SourceLocation(), - &IT.get("objc_object"), 0); + RecordDecl *ObjectTag = + RecordDecl::Create(Decl::Struct, SourceLocation(), &IT.get("objc_object"), + 0, Context); FieldDecl *IsaDecl = new FieldDecl(SourceLocation(), 0, Context.getObjCClassType()); ObjectTag->defineBody(&IsaDecl, 1); QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag)); - TypedefDecl *IdTypedef = new TypedefDecl(SourceLocation(), - &Context.Idents.get("id"), - ObjT, 0); + TypedefDecl *IdTypedef = TypedefDecl::Create(SourceLocation(), + &Context.Idents.get("id"), + ObjT, 0, Context); Context.setObjCIdType(IdTypedef); } TUScope = 0; diff --git a/Sema/SemaDecl.cpp b/Sema/SemaDecl.cpp index 300a38259b..90f9133d4b 100644 --- a/Sema/SemaDecl.cpp +++ b/Sema/SemaDecl.cpp @@ -1175,8 +1175,9 @@ TypedefDecl *Sema::ParseTypedefDecl(Scope *S, Declarator &D, QualType T, assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); // Scope manipulation handled by caller. - TypedefDecl *NewTD = new TypedefDecl(D.getIdentifierLoc(), D.getIdentifier(), - T, LastDeclarator); + TypedefDecl *NewTD = TypedefDecl::Create(D.getIdentifierLoc(), + D.getIdentifier(), + T, LastDeclarator, Context); if (D.getInvalidType()) NewTD->setInvalidDecl(); return NewTD; @@ -1255,7 +1256,7 @@ Sema::DeclTy *Sema::ActOnTag(Scope *S, unsigned TagType, TagKind TK, case Decl::Enum: // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.: // enum X { A, B, C } D; D should chain to X. - New = new EnumDecl(Loc, Name, 0); + New = EnumDecl::Create(Loc, Name, 0, Context); // If this is an undefined enum, warn. if (TK != TK_Definition) Diag(Loc, diag::ext_forward_ref_enum); break; @@ -1264,7 +1265,7 @@ Sema::DeclTy *Sema::ActOnTag(Scope *S, unsigned TagType, TagKind TK, case Decl::Class: // FIXME: Tag decls should be chained to any simultaneous vardecls, e.g.: // struct X { int A; } D; D should chain to X. - New = new RecordDecl(Kind, Loc, Name, 0); + New = RecordDecl::Create(Kind, Loc, Name, 0, Context); break; } @@ -1577,8 +1578,9 @@ Sema::DeclTy *Sema::ActOnEnumConstant(Scope *S, DeclTy *theEnumDecl, } } - EnumConstantDecl *New = new EnumConstantDecl(IdLoc, Id, EltTy, Val, EnumVal, - LastEnumConst); + EnumConstantDecl *New = + EnumConstantDecl::Create(IdLoc, Id, EltTy, Val, EnumVal, LastEnumConst, + Context); // Register this decl in the current scope stack. New->setNext(Id->getFETokenInfo()); diff --git a/include/clang/AST/ASTContext.h b/include/clang/AST/ASTContext.h index 66ac9fd08a..c6f40b7a52 100644 --- a/include/clang/AST/ASTContext.h +++ b/include/clang/AST/ASTContext.h @@ -22,6 +22,7 @@ #include "llvm/ADT/StringMap.h" #include "llvm/ADT/FoldingSet.h" #include "llvm/Bitcode/SerializationFwd.h" +#include "llvm/Support/Allocator.h" #include namespace clang { @@ -75,13 +76,15 @@ class ASTContext { RecordDecl *CFConstantStringTypeDecl; SourceManager &SourceMgr; + llvm::MallocAllocator Allocator; public: TargetInfo &Target; IdentifierTable &Idents; SelectorTable &Selectors; SourceManager& getSourceManager() { return SourceMgr; } - + llvm::MallocAllocator &getAllocator() { return Allocator; } + FullSourceLoc getFullLoc(SourceLocation Loc) const { return FullSourceLoc(Loc,SourceMgr); } diff --git a/include/clang/AST/Decl.h b/include/clang/AST/Decl.h index 06c0d73d08..9e8f065ae6 100644 --- a/include/clang/AST/Decl.h +++ b/include/clang/AST/Decl.h @@ -171,8 +171,8 @@ public: } } // global temp stats (until we have a per-module visitor) - static void addDeclKind(const Kind k); - static bool CollectingStats(bool enable=false); + static void addDeclKind(Kind k); + static bool CollectingStats(bool Enable = false); static void PrintStats(); // Implement isa/cast/dyncast/etc. @@ -539,11 +539,17 @@ protected: class EnumConstantDecl : public ValueDecl { Expr *Init; // an integer constant expression llvm::APSInt Val; // The value. -public: +protected: EnumConstantDecl(SourceLocation L, IdentifierInfo *Id, QualType T, Expr *E, const llvm::APSInt &V, ScopedDecl *PrevDecl) : ValueDecl(EnumConstant, L, Id, T, PrevDecl), Init(E), Val(V) {} + ~EnumConstantDecl() {} +public: + static EnumConstantDecl *Create(SourceLocation L, IdentifierInfo *Id, + QualType T, Expr *E, const llvm::APSInt &V, + ScopedDecl *PrevDecl, ASTContext &C); + const Expr *getInitExpr() const { return Init; } Expr *getInitExpr() { return Init; } const llvm::APSInt &getInitVal() const { return Val; } @@ -591,9 +597,13 @@ public: class TypedefDecl : public TypeDecl { /// UnderlyingType - This is the type the typedef is set to. QualType UnderlyingType; -public: TypedefDecl(SourceLocation L, IdentifierInfo *Id, QualType T, ScopedDecl *PD) : TypeDecl(Typedef, L, Id, PD), UnderlyingType(T) {} + ~TypedefDecl() {} +public: + + static TypedefDecl *Create(SourceLocation L, IdentifierInfo *Id, QualType T, + ScopedDecl *PD, ASTContext &C); QualType getUnderlyingType() const { return UnderlyingType; } void setUnderlyingType(QualType newType) { UnderlyingType = newType; } @@ -660,12 +670,16 @@ class EnumDecl : public TagDecl { /// to for code generation purposes. Note that the enumerator constants may /// have a different type than this does. QualType IntegerType; -public: + EnumDecl(SourceLocation L, IdentifierInfo *Id, ScopedDecl *PrevDecl) : TagDecl(Enum, L, Id, PrevDecl) { - ElementList = 0; - IntegerType = QualType(); - } + ElementList = 0; + IntegerType = QualType(); + } + ~EnumDecl() {} +public: + static EnumDecl *Create(SourceLocation L, IdentifierInfo *Id, + ScopedDecl *PrevDecl, ASTContext &C); /// defineElements - When created, EnumDecl correspond to a forward declared /// enum. This method is used to mark the decl as being defined, with the @@ -715,15 +729,21 @@ class RecordDecl : public TagDecl { /// Members/NumMembers - This is a new[]'d array of pointers to Decls. FieldDecl **Members; // Null if not defined. int NumMembers; // -1 if not defined. -public: - RecordDecl(Kind DK, SourceLocation L, IdentifierInfo *Id, ScopedDecl*PrevDecl) - : TagDecl(DK, L, Id, PrevDecl) { + + RecordDecl(Kind DK, SourceLocation L, IdentifierInfo *Id, + ScopedDecl *PrevDecl) : TagDecl(DK, L, Id, PrevDecl) { HasFlexibleArrayMember = false; assert(classof(static_cast(this)) && "Invalid Kind!"); Members = 0; NumMembers = -1; } + ~RecordDecl() {} +public: + + static RecordDecl *Create(Kind DK, SourceLocation L, IdentifierInfo *Id, + ScopedDecl *PrevDecl, ASTContext &C); + bool hasFlexibleArrayMember() const { return HasFlexibleArrayMember; } void setHasFlexibleArrayMember(bool V) { HasFlexibleArrayMember = V; }