From 1c1f9324337dd0f42c6e73b4b41455c2e138d743 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Tue, 28 Aug 2007 18:24:31 +0000 Subject: [PATCH] Change EnumDecl to store its corresponding integer type directly in it. Remove TargetInfo::getEnumPolicy, as there is only one policy that we support right now. llvm-svn: 41548 --- clang/AST/ASTContext.cpp | 19 +------------------ clang/CodeGen/CodeGenTypes.cpp | 2 +- clang/Sema/SemaDecl.cpp | 2 +- clang/include/clang/AST/ASTContext.h | 4 ---- clang/include/clang/AST/Decl.h | 13 ++++++++++++- clang/include/clang/Basic/TargetInfo.h | 14 -------------- 6 files changed, 15 insertions(+), 39 deletions(-) diff --git a/clang/AST/ASTContext.cpp b/clang/AST/ASTContext.cpp index bc737483d2b6..a68ebb5be651 100644 --- a/clang/AST/ASTContext.cpp +++ b/clang/AST/ASTContext.cpp @@ -233,7 +233,7 @@ ASTContext::getTypeInfo(QualType T, SourceLocation L) { Size = Layout.getSize(); Align = Layout.getAlignment(); } else if (EnumDecl *ED = dyn_cast(TT->getDecl())) { - return getTypeInfo(getEnumDeclIntegerType(ED), L); + return getTypeInfo(ED->getIntegerType(), L); } else { assert(0 && "Unimplemented type sizes!"); } @@ -312,23 +312,6 @@ const RecordLayout &ASTContext::getRecordLayout(const RecordDecl *D, return *NewEntry; } -/// getEnumDeclIntegerType - returns the integer type compatible with the -/// given enum type. -QualType ASTContext::getEnumDeclIntegerType(const EnumDecl *ED) const { - if (const EnumConstantDecl *C = ED->getEnumConstantList()) - return C->getType(); - - // If the enum list is empty, it is typed as if it contained a single zero - // element [C++ dcl.enum] and is illegal in C (as an extension, we treat it - // the same as C++ does). - switch (Target.getEnumTypePolicy(ED->getLocation())) { - default: assert(0 && "Unknown enum layout policy"); - case TargetInfo::AlwaysInt: return UnsignedIntTy; // 0 -> unsigned - case TargetInfo::ShortestType: return UnsignedCharTy; // 0 -> unsigned char - } -} - - //===----------------------------------------------------------------------===// // Type creation/memoization methods //===----------------------------------------------------------------------===// diff --git a/clang/CodeGen/CodeGenTypes.cpp b/clang/CodeGen/CodeGenTypes.cpp index 8c6c89054fdf..08c46b54bd03 100644 --- a/clang/CodeGen/CodeGenTypes.cpp +++ b/clang/CodeGen/CodeGenTypes.cpp @@ -146,7 +146,7 @@ const llvm::Type *CodeGenTypes::ConvertType(QualType T) { if (!TD->isDefinition()) { ResultType = llvm::OpaqueType::get(); } else if (TD->getKind() == Decl::Enum) { - return ConvertType(Context.getEnumDeclIntegerType(cast(TD))); + return ConvertType(cast(TD)->getIntegerType()); } else if (TD->getKind() == Decl::Struct) { const RecordDecl *RD = cast(TD); std::vector Fields; diff --git a/clang/Sema/SemaDecl.cpp b/clang/Sema/SemaDecl.cpp index 20aedec163cd..6ccdf2c396a6 100644 --- a/clang/Sema/SemaDecl.cpp +++ b/clang/Sema/SemaDecl.cpp @@ -1069,7 +1069,7 @@ void Sema::ParseEnumBody(SourceLocation EnumLoc, DeclTy *EnumDeclX, // FIXME: Install type in Enum and constant values. - Enum->defineElements(EltList); + Enum->defineElements(EltList, BestType); } void Sema::AddTopLevelDecl(Decl *current, Decl *last) { diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h index b03c46144d2b..782b2fcfcab9 100644 --- a/clang/include/clang/AST/ASTContext.h +++ b/clang/include/clang/AST/ASTContext.h @@ -147,10 +147,6 @@ public: /// position information. const RecordLayout &getRecordLayout(const RecordDecl *D, SourceLocation L); - /// getEnumDeclIntegerType - returns the integer type compatible with the - /// given enum type. - QualType getEnumDeclIntegerType(const EnumDecl *ED) const; - //===--------------------------------------------------------------------===// // Type Operators //===--------------------------------------------------------------------===// diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h index 1ffcb171de98..7c8059304546 100644 --- a/clang/include/clang/AST/Decl.h +++ b/clang/include/clang/AST/Decl.h @@ -412,6 +412,11 @@ class EnumDecl : public TagDecl { /// ElementList - this is a linked list of EnumConstantDecl's which are linked /// together through their getNextDeclarator pointers. EnumConstantDecl *ElementList; + + /// IntegerType - This represent the integer type that the enum corresponds + /// 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, Decl *PrevDecl) : TagDecl(Enum, L, Id, PrevDecl) { @@ -421,12 +426,18 @@ public: /// defineElements - When created, EnumDecl correspond to a forward declared /// enum. This method is used to mark the decl as being defined, with the /// specified list of enums. - void defineElements(EnumConstantDecl *ListHead) { + void defineElements(EnumConstantDecl *ListHead, QualType NewType) { assert(!isDefinition() && "Cannot redefine enums!"); ElementList = ListHead; setDefinition(true); + + IntegerType = NewType; } + /// getIntegerType - Return the integer type this enum decl corresponds to. + /// This returns a null qualtype for an enum forward definition. + QualType getIntegerType() const { return IntegerType; } + /// getEnumConstantList - Return the first EnumConstantDecl in the enum. /// EnumConstantDecl *getEnumConstantList() { return ElementList; } diff --git a/clang/include/clang/Basic/TargetInfo.h b/clang/include/clang/Basic/TargetInfo.h index 898c4bb5761a..689fbfc3715d 100644 --- a/clang/include/clang/Basic/TargetInfo.h +++ b/clang/include/clang/Basic/TargetInfo.h @@ -172,20 +172,6 @@ public: Align = WCharAlign; } - /// EnumTypePolicy - This enum value contains a list of the various policies - /// that a target can have about how enums are converted to integer types. - enum EnumTypePolicy { - AlwaysInt, // 'int' or 'unsigned' - ShortestType // -fshort-enums - }; - - /// getEnumTypePolicy - get the target's policy for what type an enum should - /// be compatible with. - EnumTypePolicy getEnumTypePolicy(SourceLocation Loc) { - // FIXME: implement correctly - return AlwaysInt; - } - /// getIntMaxTWidth - Return the size of intmax_t and uintmax_t for this /// target, in bits. unsigned getIntMaxTWidth(SourceLocation Loc) {