From 9e151e154780e9cd443336143af1e996d1f387e5 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sat, 15 Mar 2008 21:10:16 +0000 Subject: [PATCH] switch the VarDecl allocation model to go through ASTContext. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@48396 91177308-0d34-0410-b5e6-96231b3b80d8 --- AST/Decl.cpp | 22 ++++++++++++++++++ Driver/RewriteTest.cpp | 4 ++-- Sema/SemaDecl.cpp | 24 ++++++++++--------- Sema/SemaDeclObjC.cpp | 5 ++-- include/clang/AST/Decl.h | 50 +++++++++++++++++++++++----------------- 5 files changed, 69 insertions(+), 36 deletions(-) diff --git a/AST/Decl.cpp b/AST/Decl.cpp index aad8f212bf..a171589f13 100644 --- a/AST/Decl.cpp +++ b/AST/Decl.cpp @@ -205,6 +205,28 @@ void Decl::addDeclKind(Kind k) { // Decl Allocation/Deallocation Method Implementations //===----------------------------------------------------------------------===// +BlockVarDecl *BlockVarDecl::Create(SourceLocation L, IdentifierInfo *Id, + QualType T, StorageClass S, + ScopedDecl *PrevDecl, ASTContext &C) { + void *Mem = C.getAllocator().Allocate(); + return new (Mem) BlockVarDecl(L, Id, T, S, PrevDecl); +} + + +FileVarDecl *FileVarDecl::Create(SourceLocation L, IdentifierInfo *Id, + QualType T, StorageClass S, + ScopedDecl *PrevDecl, ASTContext &C) { + void *Mem = C.getAllocator().Allocate(); + return new (Mem) FileVarDecl(L, Id, T, S, PrevDecl); +} + +ParmVarDecl *ParmVarDecl::Create(SourceLocation L, IdentifierInfo *Id, + QualType T, StorageClass S, + ScopedDecl *PrevDecl, ASTContext &C) { + void *Mem = C.getAllocator().Allocate(); + return new (Mem) ParmVarDecl(L, Id, T, S, PrevDecl); +} + EnumConstantDecl *EnumConstantDecl::Create(SourceLocation L, IdentifierInfo *Id, QualType T, Expr *E, const llvm::APSInt &V, diff --git a/Driver/RewriteTest.cpp b/Driver/RewriteTest.cpp index 652ce1c82c..dfad8ecccf 100644 --- a/Driver/RewriteTest.cpp +++ b/Driver/RewriteTest.cpp @@ -1744,9 +1744,9 @@ Stmt *RewriteTest::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { InsertText(SourceLocation::getFileLoc(MainFileID, 0), StrObjDecl.c_str(), StrObjDecl.size()); - FileVarDecl *NewVD = new FileVarDecl(SourceLocation(), + FileVarDecl *NewVD = FileVarDecl::Create(SourceLocation(), &Context->Idents.get(S.c_str()), strType, - VarDecl::Static, NULL); + VarDecl::Static, NULL, *Context); DeclRefExpr *DRE = new DeclRefExpr(NewVD, strType, SourceLocation()); Expr *Unop = new UnaryOperator(DRE, UnaryOperator::AddrOf, Context->getPointerType(DRE->getType()), diff --git a/Sema/SemaDecl.cpp b/Sema/SemaDecl.cpp index 90f9133d4b..13d49b0980 100644 --- a/Sema/SemaDecl.cpp +++ b/Sema/SemaDecl.cpp @@ -777,13 +777,13 @@ Sema::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *lastDecl) { VarDecl *NewVD; VarDecl::StorageClass SC; switch (D.getDeclSpec().getStorageClassSpec()) { - default: assert(0 && "Unknown storage class!"); - case DeclSpec::SCS_unspecified: SC = VarDecl::None; break; - case DeclSpec::SCS_extern: SC = VarDecl::Extern; break; - case DeclSpec::SCS_static: SC = VarDecl::Static; break; - case DeclSpec::SCS_auto: SC = VarDecl::Auto; break; - case DeclSpec::SCS_register: SC = VarDecl::Register; break; - case DeclSpec::SCS_private_extern: SC = VarDecl::PrivateExtern; break; + default: assert(0 && "Unknown storage class!"); + case DeclSpec::SCS_unspecified: SC = VarDecl::None; break; + case DeclSpec::SCS_extern: SC = VarDecl::Extern; break; + case DeclSpec::SCS_static: SC = VarDecl::Static; break; + case DeclSpec::SCS_auto: SC = VarDecl::Auto; break; + case DeclSpec::SCS_register: SC = VarDecl::Register; break; + case DeclSpec::SCS_private_extern: SC = VarDecl::PrivateExtern; break; } if (S->getParent() == 0) { // C99 6.9p2: The storage-class specifiers auto and register shall not @@ -793,9 +793,11 @@ Sema::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *lastDecl) { R.getAsString()); InvalidDecl = true; } - NewVD = new FileVarDecl(D.getIdentifierLoc(), II, R, SC, LastDeclarator); + NewVD = FileVarDecl::Create(D.getIdentifierLoc(), II, R, SC, + LastDeclarator, Context); } else { - NewVD = new BlockVarDecl(D.getIdentifierLoc(), II, R, SC, LastDeclarator); + NewVD = BlockVarDecl::Create(D.getIdentifierLoc(), II, R, SC, + LastDeclarator, Context); } // Handle attributes prior to checking for duplicates in MergeVarDecl HandleDeclAttributes(NewVD, D.getDeclSpec().getAttributes(), @@ -1014,8 +1016,8 @@ Sema::ActOnParamDeclarator(struct DeclaratorChunk::ParamInfo &PI, } else if (parmDeclType->isFunctionType()) parmDeclType = Context.getPointerType(parmDeclType); - ParmVarDecl *New = new ParmVarDecl(PI.IdentLoc, II, parmDeclType, - VarDecl::None, 0); + ParmVarDecl *New = ParmVarDecl::Create(PI.IdentLoc, II, parmDeclType, + VarDecl::None, 0, Context); if (PI.InvalidType) New->setInvalidDecl(); diff --git a/Sema/SemaDeclObjC.cpp b/Sema/SemaDeclObjC.cpp index 04ce426dec..1e60a39647 100644 --- a/Sema/SemaDeclObjC.cpp +++ b/Sema/SemaDeclObjC.cpp @@ -823,8 +823,9 @@ Sema::DeclTy *Sema::ActOnMethodDeclaration( argType = QualType::getFromOpaquePtr(ArgTypes[i]); else argType = Context.getObjCIdType(); - ParmVarDecl* Param = new ParmVarDecl(SourceLocation(/*FIXME*/), ArgNames[i], - argType, VarDecl::None, 0); + ParmVarDecl* Param = ParmVarDecl::Create(SourceLocation(/*FIXME*/), + ArgNames[i], argType, + VarDecl::None, 0, Context); Param->setObjCDeclQualifier( CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier())); Params.push_back(Param); diff --git a/include/clang/AST/Decl.h b/include/clang/AST/Decl.h index 9e8f065ae6..df484fbca9 100644 --- a/include/clang/AST/Decl.h +++ b/include/clang/AST/Decl.h @@ -294,6 +294,17 @@ public: enum StorageClass { None, Auto, Register, Extern, Static, PrivateExtern }; +private: + Expr *Init; + // FIXME: This can be packed into the bitfields in Decl. + unsigned SClass : 3; + + friend class StmtIteratorBase; +protected: + VarDecl(Kind DK, SourceLocation L, IdentifierInfo *Id, QualType T, + StorageClass SC, ScopedDecl *PrevDecl) + : ValueDecl(DK, L, Id, T, PrevDecl), Init(0) { SClass = SC; } +public: StorageClass getStorageClass() const { return (StorageClass)SClass; } const Expr *getInit() const { return Init; } @@ -322,17 +333,7 @@ public: return D->getKind() >= VarFirst && D->getKind() <= VarLast; } static bool classof(const VarDecl *D) { return true; } -protected: - VarDecl(Kind DK, SourceLocation L, IdentifierInfo *Id, QualType T, - StorageClass SC, ScopedDecl *PrevDecl) - : ValueDecl(DK, L, Id, T, PrevDecl), Init(0) { SClass = SC; } -private: - Expr *Init; - // FIXME: This can be packed into the bitfields in Decl. - unsigned SClass : 3; - - friend class StmtIteratorBase; - + protected: void EmitInRec(llvm::Serializer& S) const; void ReadInRec(llvm::Deserializer& D); @@ -349,11 +350,13 @@ protected: /// BlockVarDecl - Represent a local variable declaration. class BlockVarDecl : public VarDecl { -public: BlockVarDecl(SourceLocation L, IdentifierInfo *Id, QualType T, StorageClass S, ScopedDecl *PrevDecl) : VarDecl(BlockVar, L, Id, T, S, PrevDecl) {} - +public: + static BlockVarDecl *Create(SourceLocation L, IdentifierInfo *Id, QualType T, + StorageClass S, ScopedDecl *PrevDecl, + ASTContext &C); // Implement isa/cast/dyncast/etc. static bool classof(const Decl *D) { return D->getKind() == BlockVar; } static bool classof(const BlockVarDecl *D) { return true; } @@ -370,10 +373,13 @@ protected: /// definitions (C99 6.9.2p2) using our type system (without storing a /// pointer to the decl's scope, which is transient). class FileVarDecl : public VarDecl { -public: FileVarDecl(SourceLocation L, IdentifierInfo *Id, QualType T, StorageClass S, ScopedDecl *PrevDecl) : VarDecl(FileVar, L, Id, T, S, PrevDecl) {} +public: + static FileVarDecl *Create(SourceLocation L, IdentifierInfo *Id, QualType T, + StorageClass S, ScopedDecl *PrevDecl, + ASTContext &C); // Implement isa/cast/dyncast/etc. static bool classof(const Decl *D) { return D->getKind() == FileVar; } @@ -388,11 +394,19 @@ protected: /// ParmVarDecl - Represent a parameter to a function. class ParmVarDecl : public VarDecl { -public: + // NOTE: VC++ treats enums as signed, avoid using the ObjCDeclQualifier enum + /// FIXME: Also can be paced into the bitfields in Decl. + /// in, inout, etc. + unsigned objcDeclQualifier : 6; + ParmVarDecl(SourceLocation L, IdentifierInfo *Id, QualType T, StorageClass S, ScopedDecl *PrevDecl) : VarDecl(ParmVar, L, Id, T, S, PrevDecl), objcDeclQualifier(OBJC_TQ_None) {} +public: + static ParmVarDecl *Create(SourceLocation L, IdentifierInfo *Id, QualType T, + StorageClass S, ScopedDecl *PrevDecl, + ASTContext &C); ObjCDeclQualifier getObjCDeclQualifier() const { return ObjCDeclQualifier(objcDeclQualifier); @@ -404,12 +418,6 @@ public: static bool classof(const Decl *D) { return D->getKind() == ParmVar; } static bool classof(const ParmVarDecl *D) { return true; } -private: - // NOTE: VC++ treats enums as signed, avoid using the ObjCDeclQualifier enum - /// FIXME: Also can be paced into the bitfields in Decl. - /// in, inout, etc. - unsigned objcDeclQualifier : 6; - protected: /// EmitImpl - Serialize this ParmVarDecl. Called by Decl::Emit. virtual void EmitImpl(llvm::Serializer& S) const; -- 2.40.0