]> granicus.if.org Git - clang/commitdiff
implement sizeof(enum x), patch inspired by Keith Bauer.
authorChris Lattner <sabre@nondot.org>
Mon, 27 Aug 2007 17:38:00 +0000 (17:38 +0000)
committerChris Lattner <sabre@nondot.org>
Mon, 27 Aug 2007 17:38:00 +0000 (17:38 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@41500 91177308-0d34-0410-b5e6-96231b3b80d8

AST/ASTContext.cpp
include/clang/AST/ASTContext.h

index 4b79582e524ffa4a8fdb809ce660b4ac13e5bc50..1a740b524570e7f258a0b549b6a835b6b8b6bd30 100644 (file)
@@ -227,13 +227,16 @@ ASTContext::getTypeInfo(QualType T, SourceLocation L) {
     break;
   }
   case Type::Tagged:
-    RecordType *RT = dyn_cast<RecordType>(cast<TagType>(T));
-    if (!RT)
-      // FIXME: Handle enums.
+    TagType *TT = cast<TagType>(T);
+    if (RecordType *RT = dyn_cast<RecordType>(TT)) {
+      const RecordLayout &Layout = getRecordLayout(RT->getDecl(), L);
+      Size = Layout.getSize();
+      Align = Layout.getAlignment();
+    } else if (EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl())) {
+      return getTypeInfo(getEnumDeclIntegerType(ED), L);
+    } else {
       assert(0 && "Unimplemented type sizes!");
-    const RecordLayout &Layout = getRecordLayout(RT->getDecl(), L);
-    Size = Layout.getSize();
-    Align = Layout.getAlignment();
+    }
     break;
   }
   
@@ -309,6 +312,22 @@ const RecordLayout &ASTContext::getRecordLayout(const RecordDecl *D,
   return *NewEntry;
 }
 
+/// getEnumDeclIntegerType - returns the integer type compatible with the
+/// given enum type.
+QualType ASTContext::getEnumDeclIntegerType(EnumDecl *ED) const {
+  if (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
@@ -775,4 +794,4 @@ QualType ASTContext::getCFConstantStringType() {
   }
   
   return getTagDeclType(CFConstantStringTypeDecl);
-}
+}
\ No newline at end of file
index 2bfc6af89a36a99c24679efbe7c54e3bf538ad5d..8973087a527e8dac38e62cfcb0790d641c71f174 100644 (file)
@@ -147,6 +147,10 @@ public:
   /// position information.
   const RecordLayout &getRecordLayout(const RecordDecl *D, SourceLocation L);
   
+  /// getEnumDeclIntegerType - returns the integer type compatible with the
+  /// given enum type.
+  QualType getEnumDeclIntegerType(EnumDecl *ED) const;
+  
   //===--------------------------------------------------------------------===//
   //                            Type Operators
   //===--------------------------------------------------------------------===//