From: Alp Toker Date: Tue, 17 Dec 2013 01:22:38 +0000 (+0000) Subject: ASTContext: Refactor implicit record creation X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5f391fd43357fb4c9306206503125059bbf2dc40;p=clang ASTContext: Refactor implicit record creation Tidy up built-in record creation to reduce code duplication. Continuation of r197336. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@197452 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/ASTContext.h b/include/clang/AST/ASTContext.h index 4715498f39..b384d18a44 100644 --- a/include/clang/AST/ASTContext.h +++ b/include/clang/AST/ASTContext.h @@ -832,6 +832,11 @@ public: void PrintStats() const; const SmallVectorImpl& getTypes() const { return Types; } + /// \brief Create a new implicit TU-level CXXRecordDecl or RecordDecl + /// declaration. + RecordDecl *buildImplicitRecord(StringRef Name, + RecordDecl::TagKind TK = TTK_Struct) const; + /// \brief Create a new implicit TU-level typedef declaration. TypedefDecl *buildImplicitTypedef(QualType T, StringRef Name) const; diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp index 4ccd773120..a8777305b4 100644 --- a/lib/AST/ASTContext.cpp +++ b/lib/AST/ASTContext.cpp @@ -857,17 +857,16 @@ void ASTContext::PrintStats() const { BumpAlloc.PrintStats(); } -/// CreateRecordDecl - Utility to build implicit CXXRecordDecl or RecordDecl -/// instances. -static RecordDecl *CreateRecordDecl(const ASTContext &Ctx, - RecordDecl::TagKind TK, DeclContext *DC, - IdentifierInfo *Id) { +RecordDecl *ASTContext::buildImplicitRecord(StringRef Name, + RecordDecl::TagKind TK) const { SourceLocation Loc; RecordDecl *NewDecl; - if (Ctx.getLangOpts().CPlusPlus) - NewDecl = CXXRecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id); + if (getLangOpts().CPlusPlus) + NewDecl = CXXRecordDecl::Create(*this, TK, getTranslationUnitDecl(), Loc, + Loc, &Idents.get(Name)); else - NewDecl = RecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id); + NewDecl = RecordDecl::Create(*this, TK, getTranslationUnitDecl(), Loc, Loc, + &Idents.get(Name)); NewDecl->setImplicit(); return NewDecl; } @@ -897,8 +896,7 @@ TypedefDecl *ASTContext::getUInt128Decl() const { TypeDecl *ASTContext::getFloat128StubType() const { assert(LangOpts.CPlusPlus && "should only be called for c++"); if (!Float128StubDecl) - Float128StubDecl = CreateRecordDecl( - *this, TTK_Struct, getTranslationUnitDecl(), &Idents.get("__float128")); + Float128StubDecl = buildImplicitRecord("__float128"); return Float128StubDecl; } @@ -4564,9 +4562,7 @@ int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const { // getCFConstantStringType - Return the type used for constant CFStrings. QualType ASTContext::getCFConstantStringType() const { if (!CFConstantStringTypeDecl) { - CFConstantStringTypeDecl = - CreateRecordDecl(*this, TTK_Struct, TUDecl, - &Idents.get("NSConstantString")); + CFConstantStringTypeDecl = buildImplicitRecord("NSConstantString"); CFConstantStringTypeDecl->startDefinition(); QualType FieldTypes[4]; @@ -4601,8 +4597,7 @@ QualType ASTContext::getCFConstantStringType() const { QualType ASTContext::getObjCSuperType() const { if (ObjCSuperType.isNull()) { - RecordDecl *ObjCSuperTypeDecl = - CreateRecordDecl(*this, TTK_Struct, TUDecl, &Idents.get("objc_super")); + RecordDecl *ObjCSuperTypeDecl = buildImplicitRecord("objc_super"); TUDecl->addDecl(ObjCSuperTypeDecl); ObjCSuperType = getTagDeclType(ObjCSuperTypeDecl); } @@ -4619,12 +4614,11 @@ QualType ASTContext::getBlockDescriptorType() const { if (BlockDescriptorType) return getTagDeclType(BlockDescriptorType); - RecordDecl *T; + RecordDecl *RD; // FIXME: Needs the FlagAppleBlock bit. - T = CreateRecordDecl(*this, TTK_Struct, TUDecl, - &Idents.get("__block_descriptor")); - T->startDefinition(); - + RD = buildImplicitRecord("__block_descriptor"); + RD->startDefinition(); + QualType FieldTypes[] = { UnsignedLongTy, UnsignedLongTy, @@ -4636,20 +4630,17 @@ QualType ASTContext::getBlockDescriptorType() const { }; for (size_t i = 0; i < 2; ++i) { - FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), - SourceLocation(), - &Idents.get(FieldNames[i]), - FieldTypes[i], /*TInfo=*/0, - /*BitWidth=*/0, - /*Mutable=*/false, - ICIS_NoInit); + FieldDecl *Field = FieldDecl::Create( + *this, RD, SourceLocation(), SourceLocation(), + &Idents.get(FieldNames[i]), FieldTypes[i], /*TInfo=*/0, + /*BitWidth=*/0, /*Mutable=*/false, ICIS_NoInit); Field->setAccess(AS_public); - T->addDecl(Field); + RD->addDecl(Field); } - T->completeDefinition(); + RD->completeDefinition(); - BlockDescriptorType = T; + BlockDescriptorType = RD; return getTagDeclType(BlockDescriptorType); } @@ -4658,12 +4649,11 @@ QualType ASTContext::getBlockDescriptorExtendedType() const { if (BlockDescriptorExtendedType) return getTagDeclType(BlockDescriptorExtendedType); - RecordDecl *T; + RecordDecl *RD; // FIXME: Needs the FlagAppleBlock bit. - T = CreateRecordDecl(*this, TTK_Struct, TUDecl, - &Idents.get("__block_descriptor_withcopydispose")); - T->startDefinition(); - + RD = buildImplicitRecord("__block_descriptor_withcopydispose"); + RD->startDefinition(); + QualType FieldTypes[] = { UnsignedLongTy, UnsignedLongTy, @@ -4679,21 +4669,18 @@ QualType ASTContext::getBlockDescriptorExtendedType() const { }; for (size_t i = 0; i < 4; ++i) { - FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), - SourceLocation(), - &Idents.get(FieldNames[i]), - FieldTypes[i], /*TInfo=*/0, - /*BitWidth=*/0, - /*Mutable=*/false, - ICIS_NoInit); + FieldDecl *Field = FieldDecl::Create( + *this, RD, SourceLocation(), SourceLocation(), + &Idents.get(FieldNames[i]), FieldTypes[i], /*TInfo=*/0, + /*BitWidth=*/0, + /*Mutable=*/false, ICIS_NoInit); Field->setAccess(AS_public); - T->addDecl(Field); + RD->addDecl(Field); } - T->completeDefinition(); - - BlockDescriptorExtendedType = T; + RD->completeDefinition(); + BlockDescriptorExtendedType = RD; return getTagDeclType(BlockDescriptorExtendedType); } @@ -5804,9 +5791,7 @@ static TypedefDecl *CreateVoidPtrBuiltinVaListDecl(const ASTContext *Context) { static TypedefDecl * CreateAArch64ABIBuiltinVaListDecl(const ASTContext *Context) { // struct __va_list - RecordDecl *VaListTagDecl = - CreateRecordDecl(*Context, TTK_Struct, Context->getTranslationUnitDecl(), - &Context->Idents.get("__va_list")); + RecordDecl *VaListTagDecl = Context->buildImplicitRecord("__va_list"); if (Context->getLangOpts().CPlusPlus) { // namespace std { struct __va_list { NamespaceDecl *NS; @@ -5871,9 +5856,7 @@ static TypedefDecl *CreatePowerABIBuiltinVaListDecl(const ASTContext *Context) { // typedef struct __va_list_tag { RecordDecl *VaListTagDecl; - VaListTagDecl = CreateRecordDecl(*Context, TTK_Struct, - Context->getTranslationUnitDecl(), - &Context->Idents.get("__va_list_tag")); + VaListTagDecl = Context->buildImplicitRecord("__va_list_tag"); VaListTagDecl->startDefinition(); const size_t NumFields = 5; @@ -5936,9 +5919,7 @@ static TypedefDecl * CreateX86_64ABIBuiltinVaListDecl(const ASTContext *Context) { // typedef struct __va_list_tag { RecordDecl *VaListTagDecl; - VaListTagDecl = CreateRecordDecl(*Context, TTK_Struct, - Context->getTranslationUnitDecl(), - &Context->Idents.get("__va_list_tag")); + VaListTagDecl = Context->buildImplicitRecord("__va_list_tag"); VaListTagDecl->startDefinition(); const size_t NumFields = 4; @@ -6006,9 +5987,7 @@ static TypedefDecl *CreatePNaClABIBuiltinVaListDecl(const ASTContext *Context) { static TypedefDecl * CreateAAPCSABIBuiltinVaListDecl(const ASTContext *Context) { // struct __va_list - RecordDecl *VaListDecl = - CreateRecordDecl(*Context, TTK_Struct, Context->getTranslationUnitDecl(), - &Context->Idents.get("__va_list")); + RecordDecl *VaListDecl = Context->buildImplicitRecord("__va_list"); if (Context->getLangOpts().CPlusPlus) { // namespace std { struct __va_list { NamespaceDecl *NS; @@ -6049,9 +6028,7 @@ static TypedefDecl * CreateSystemZBuiltinVaListDecl(const ASTContext *Context) { // typedef struct __va_list_tag { RecordDecl *VaListTagDecl; - VaListTagDecl = CreateRecordDecl(*Context, TTK_Struct, - Context->getTranslationUnitDecl(), - &Context->Idents.get("__va_list_tag")); + VaListTagDecl = Context->buildImplicitRecord("__va_list_tag"); VaListTagDecl->startDefinition(); const size_t NumFields = 4; diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp index e3ab9df9a6..0d6ba33aad 100644 --- a/lib/CodeGen/CodeGenModule.cpp +++ b/lib/CodeGen/CodeGenModule.cpp @@ -2413,16 +2413,6 @@ CodeGenModule::GetAddrOfConstantCFString(const StringLiteral *Literal) { return GV; } -static RecordDecl * -CreateRecordDecl(const ASTContext &Ctx, RecordDecl::TagKind TK, - DeclContext *DC, IdentifierInfo *Id) { - SourceLocation Loc; - if (Ctx.getLangOpts().CPlusPlus) - return CXXRecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id); - else - return RecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id); -} - llvm::Constant * CodeGenModule::GetAddrOfConstantString(const StringLiteral *Literal) { unsigned StringLength = 0; @@ -2465,9 +2455,7 @@ CodeGenModule::GetAddrOfConstantString(const StringLiteral *Literal) { if (!NSConstantStringType) { // Construct the type for a constant NSString. - RecordDecl *D = CreateRecordDecl(Context, TTK_Struct, - Context.getTranslationUnitDecl(), - &Context.Idents.get("__builtin_NSString")); + RecordDecl *D = Context.buildImplicitRecord("__builtin_NSString"); D->startDefinition(); QualType FieldTypes[3]; @@ -2543,9 +2531,7 @@ CodeGenModule::GetAddrOfConstantString(const StringLiteral *Literal) { QualType CodeGenModule::getObjCFastEnumerationStateType() { if (ObjCFastEnumerationStateType.isNull()) { - RecordDecl *D = CreateRecordDecl(Context, TTK_Struct, - Context.getTranslationUnitDecl(), - &Context.Idents.get("__objcFastEnumerationState")); + RecordDecl *D = Context.buildImplicitRecord("__objcFastEnumerationState"); D->startDefinition(); QualType FieldTypes[] = {