[cfe-commits] r41500 - in /cfe/trunk: AST/ASTContext.cpp include/clang/AST/ASTContext.h

Chris Lattner sabre at nondot.org
Mon Aug 27 10:38:00 PDT 2007


Author: lattner
Date: Mon Aug 27 12:38:00 2007
New Revision: 41500

URL: http://llvm.org/viewvc/llvm-project?rev=41500&view=rev
Log:
implement sizeof(enum x), patch inspired by Keith Bauer.

Modified:
    cfe/trunk/AST/ASTContext.cpp
    cfe/trunk/include/clang/AST/ASTContext.h

Modified: cfe/trunk/AST/ASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/AST/ASTContext.cpp?rev=41500&r1=41499&r2=41500&view=diff

==============================================================================
--- cfe/trunk/AST/ASTContext.cpp (original)
+++ cfe/trunk/AST/ASTContext.cpp Mon Aug 27 12:38:00 2007
@@ -227,13 +227,16 @@
     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 @@
   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 @@
   }
   
   return getTagDeclType(CFConstantStringTypeDecl);
-}
+}
\ No newline at end of file

Modified: cfe/trunk/include/clang/AST/ASTContext.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ASTContext.h?rev=41500&r1=41499&r2=41500&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/ASTContext.h (original)
+++ cfe/trunk/include/clang/AST/ASTContext.h Mon Aug 27 12:38:00 2007
@@ -147,6 +147,10 @@
   /// 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
   //===--------------------------------------------------------------------===//





More information about the cfe-commits mailing list