[cfe-commits] r78471 - in /cfe/trunk: include/clang/Parse/Action.h lib/Frontend/PrintParserCallbacks.cpp lib/Parse/ParseDecl.cpp lib/Sema/Sema.h lib/Sema/SemaDecl.cpp lib/Sema/SemaTemplateInstantiateDecl.cpp

Edward O'Callaghan eocallaghan at auroraux.org
Sat Aug 8 07:36:57 PDT 2009


Author: evocallaghan
Date: Sat Aug  8 09:36:57 2009
New Revision: 78471

URL: http://llvm.org/viewvc/llvm-project?rev=78471&view=rev
Log:
Patch should implement packed enums - PR4098. Credit to Anders Johnsen.

Modified:
    cfe/trunk/include/clang/Parse/Action.h
    cfe/trunk/lib/Frontend/PrintParserCallbacks.cpp
    cfe/trunk/lib/Parse/ParseDecl.cpp
    cfe/trunk/lib/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp

Modified: cfe/trunk/include/clang/Parse/Action.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Parse/Action.h?rev=78471&r1=78470&r2=78471&view=diff

==============================================================================
--- cfe/trunk/include/clang/Parse/Action.h (original)
+++ cfe/trunk/include/clang/Parse/Action.h Sat Aug  8 09:36:57 2009
@@ -530,7 +530,8 @@
   }
   virtual void ActOnEnumBody(SourceLocation EnumLoc, SourceLocation LBraceLoc,
                              SourceLocation RBraceLoc, DeclPtrTy EnumDecl,
-                             DeclPtrTy *Elements, unsigned NumElements) {}
+                             DeclPtrTy *Elements, unsigned NumElements,
+                             Scope *S, AttributeList *AttrList) {}
 
   //===--------------------------------------------------------------------===//
   // Statement Parsing Callbacks.

Modified: cfe/trunk/lib/Frontend/PrintParserCallbacks.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PrintParserCallbacks.cpp?rev=78471&r1=78470&r2=78471&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/PrintParserCallbacks.cpp (original)
+++ cfe/trunk/lib/Frontend/PrintParserCallbacks.cpp Sat Aug  8 09:36:57 2009
@@ -245,7 +245,8 @@
 
     virtual void ActOnEnumBody(SourceLocation EnumLoc, SourceLocation LBraceLoc,
                                SourceLocation RBraceLoc, DeclPtrTy EnumDecl,
-                               DeclPtrTy *Elements, unsigned NumElements) {
+                               DeclPtrTy *Elements, unsigned NumElements,
+                               Scope *S, AttributeList *AttrList) {
       Out << __FUNCTION__ << "\n";
     }
 

Modified: cfe/trunk/lib/Parse/ParseDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseDecl.cpp?rev=78471&r1=78470&r2=78471&view=diff

==============================================================================
--- cfe/trunk/lib/Parse/ParseDecl.cpp (original)
+++ cfe/trunk/lib/Parse/ParseDecl.cpp Sat Aug  8 09:36:57 2009
@@ -1707,14 +1707,15 @@
   // Eat the }.
   SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
 
-  Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
-                        EnumConstantDecls.data(), EnumConstantDecls.size());
-  
-  Action::AttrTy *AttrList = 0;
+  AttributeList *Attr = 0;
   // If attributes exist after the identifier list, parse them.
   if (Tok.is(tok::kw___attribute))
-    AttrList = ParseAttributes(); // FIXME: where do they do?
+    Attr = ParseAttributes();
 
+  Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
+                        EnumConstantDecls.data(), EnumConstantDecls.size(),
+                        CurScope, Attr);
+  
   EnumScope.Exit();
   Actions.ActOnTagFinishDefinition(CurScope, EnumDecl, RBraceLoc);
 }

Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=78471&r1=78470&r2=78471&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Sat Aug  8 09:36:57 2009
@@ -619,7 +619,8 @@
                                       SourceLocation EqualLoc, ExprTy *Val);
   virtual void ActOnEnumBody(SourceLocation EnumLoc, SourceLocation LBraceLoc,
                              SourceLocation RBraceLoc, DeclPtrTy EnumDecl,
-                             DeclPtrTy *Elements, unsigned NumElements);
+                             DeclPtrTy *Elements, unsigned NumElements,
+                             Scope *S, AttributeList *Attr);
 
   DeclContext *getContainingDC(DeclContext *DC);
 

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=78471&r1=78470&r2=78471&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Sat Aug  8 09:36:57 2009
@@ -5059,14 +5059,20 @@
 
 void Sema::ActOnEnumBody(SourceLocation EnumLoc, SourceLocation LBraceLoc,
                          SourceLocation RBraceLoc, DeclPtrTy EnumDeclX,
-                         DeclPtrTy *Elements, unsigned NumElements) {
+                         DeclPtrTy *Elements, unsigned NumElements,
+                         Scope *S, AttributeList *Attr) {
   EnumDecl *Enum = cast<EnumDecl>(EnumDeclX.getAs<Decl>());
   QualType EnumType = Context.getTypeDeclType(Enum);
+
+  if (Attr)
+    ProcessDeclAttributeList(S, Enum, Attr);
   
   // TODO: If the result value doesn't fit in an int, it must be a long or long
   // long value.  ISO C does not support this, but GCC does as an extension,
   // emit a warning.
   unsigned IntWidth = Context.Target.getIntWidth();
+  unsigned CharWidth = Context.Target.getCharWidth();
+  unsigned ShortWidth = Context.Target.getShortWidth();
   
   // Verify that all the values are okay, compute the size of the values, and
   // reverse the list.
@@ -5108,14 +5114,25 @@
   }
   
   // Figure out the type that should be used for this enum.
-  // FIXME: Support attribute(packed) on enums and -fshort-enums.
+  // FIXME: Support -fshort-enums.
   QualType BestType;
   unsigned BestWidth;
-  
+
+  bool Packed = Enum->getAttr<PackedAttr>() ? true : false;
+
   if (NumNegativeBits) {
     // If there is a negative value, figure out the smallest integer type (of 
     // int/long/longlong) that fits.
-    if (NumNegativeBits <= IntWidth && NumPositiveBits < IntWidth) {
+    // If it's packed, check also if it fits a char or a short.
+    if (Packed && NumNegativeBits <= CharWidth && NumPositiveBits < CharWidth) {
+        BestType = Context.SignedCharTy;
+        BestWidth = CharWidth;
+    } else if (Packed && NumNegativeBits <= ShortWidth && 
+               NumPositiveBits < ShortWidth) {
+        BestType = Context.ShortTy;
+        BestWidth = ShortWidth;
+    }
+    else if (NumNegativeBits <= IntWidth && NumPositiveBits < IntWidth) {
       BestType = Context.IntTy;
       BestWidth = IntWidth;
     } else {
@@ -5134,7 +5151,15 @@
   } else {
     // If there is no negative value, figure out which of uint, ulong, ulonglong
     // fits.
-    if (NumPositiveBits <= IntWidth) {
+    // If it's packed, check also if it fits a char or a short.
+    if (Packed && NumPositiveBits <= CharWidth) {
+        BestType = Context.UnsignedCharTy;
+        BestWidth = CharWidth;
+    } else if (Packed && NumPositiveBits <= ShortWidth) {
+        BestType = Context.UnsignedShortTy;
+        BestWidth = ShortWidth;
+    }
+    else if (NumPositiveBits <= IntWidth) {
       BestType = Context.UnsignedIntTy;
       BestWidth = IntWidth;
     } else if (NumPositiveBits <=

Modified: cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp?rev=78471&r1=78470&r2=78471&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp Sat Aug  8 09:36:57 2009
@@ -283,9 +283,11 @@
   }
       
   // FIXME: Fixup LBraceLoc and RBraceLoc
+  // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
   SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
                         Sema::DeclPtrTy::make(Enum),
-                        &Enumerators[0], Enumerators.size());
+                        &Enumerators[0], Enumerators.size(),
+                        0, 0);
 
   return Enum;
 }





More information about the cfe-commits mailing list