]> granicus.if.org Git - clang/commitdiff
Remember the number of positive and negative bits used by the enumerators of
authorJohn McCall <rjmccall@apple.com>
Thu, 6 May 2010 08:49:23 +0000 (08:49 +0000)
committerJohn McCall <rjmccall@apple.com>
Thu, 6 May 2010 08:49:23 +0000 (08:49 +0000)
an enum in the enum decl itself.  Use some spare bits from TagDecl for this
purpose.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@103173 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/AST/Decl.h
lib/AST/ASTImporter.cpp
lib/AST/Decl.cpp
lib/Frontend/PCHReaderDecl.cpp
lib/Frontend/PCHWriterDecl.cpp
lib/Sema/SemaDecl.cpp

index 834c9a0c563fd34a12490a223e06bbfc884703e4..4c95d1da7f8fe1c291312da80c9f794164a6e2c2 100644 (file)
@@ -1651,6 +1651,12 @@ private:
   /// in the syntax of a declarator.
   bool IsEmbeddedInDeclarator : 1;
 
+protected:
+  // These are used by (and only defined for) EnumDecl.
+  unsigned NumPositiveBits : 8;
+  unsigned NumNegativeBits : 8;
+
+private:
   SourceLocation TagKeywordLoc;
   SourceLocation RBraceLoc;
 
@@ -1820,6 +1826,13 @@ class EnumDecl : public TagDecl {
   /// enumeration declared within the template.
   EnumDecl *InstantiatedFrom;
 
+  // The number of positive and negative bits required by the
+  // enumerators are stored in the SubclassBits field.
+  enum {
+    NumBitsWidth = 8,
+    NumBitsMask = (1 << NumBitsWidth) - 1
+  };
+
   EnumDecl(DeclContext *DC, SourceLocation L,
            IdentifierInfo *Id, EnumDecl *PrevDecl, SourceLocation TKL)
     : TagDecl(Enum, TK_enum, DC, L, Id, PrevDecl, TKL), InstantiatedFrom(0) {
@@ -1845,7 +1858,9 @@ public:
   /// added (via DeclContext::addDecl). NewType is the new underlying
   /// type of the enumeration type.
   void completeDefinition(QualType NewType,
-                          QualType PromotionType);
+                          QualType PromotionType,
+                          unsigned NumPositiveBits,
+                          unsigned NumNegativeBits);
 
   // enumerator_iterator - Iterates through the enumerators of this
   // enumeration.
@@ -1873,6 +1888,32 @@ public:
   /// \brief Set the underlying integer type.
   void setIntegerType(QualType T) { IntegerType = T; }
 
+  /// \brief Returns the width in bits requred to store all the
+  /// non-negative enumerators of this enum.
+  unsigned getNumPositiveBits() const {
+    return NumPositiveBits;
+  }
+  void setNumPositiveBits(unsigned Num) {
+    NumPositiveBits = Num;
+    assert(NumPositiveBits == Num && "can't store this bitcount");
+  }
+
+  /// \brief Returns the width in bits requred to store all the
+  /// negative enumerators of this enum.  These widths include
+  /// the rightmost leading 1;  that is:
+  /// 
+  /// MOST NEGATIVE ENUMERATOR     PATTERN     NUM NEGATIVE BITS
+  /// ------------------------     -------     -----------------
+  ///                       -1     1111111                     1
+  ///                      -10     1110110                     5
+  ///                     -101     1001011                     8
+  unsigned getNumNegativeBits() const {
+    return NumNegativeBits;
+  }
+  void setNumNegativeBits(unsigned Num) {
+    NumNegativeBits = Num;
+  }
+
   /// \brief Returns the enumeration (declared within the template)
   /// from which this enumeration type was instantiated, or NULL if
   /// this enumeration was not instantiated from any template.
index ae09d7978ea5924d4e76b955a5ae566a14c6636d..083c1bf7d8ad70661d426989af5af0ef94c6dd5f 100644 (file)
@@ -1617,7 +1617,12 @@ Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
     
     D2->startDefinition();
     ImportDeclContext(D);
-    D2->completeDefinition(T, ToPromotionType);
+
+    // FIXME: we might need to merge the number of positive or negative bits
+    // if the enumerator lists don't match.
+    D2->completeDefinition(T, ToPromotionType,
+                           D->getNumPositiveBits(),
+                           D->getNumNegativeBits());
   }
   
   return D2;
index e19a9fb7c22860b79684e135f28b8334b30ad5d7..0336ca1eaac4d83ee05b8698b75f64e2876402fe 100644 (file)
@@ -1572,10 +1572,14 @@ void EnumDecl::Destroy(ASTContext& C) {
 }
 
 void EnumDecl::completeDefinition(QualType NewType,
-                                  QualType NewPromotionType) {
+                                  QualType NewPromotionType,
+                                  unsigned NumPositiveBits,
+                                  unsigned NumNegativeBits) {
   assert(!isDefinition() && "Cannot redefine enums!");
   IntegerType = NewType;
   PromotionType = NewPromotionType;
+  setNumPositiveBits(NumPositiveBits);
+  setNumNegativeBits(NumNegativeBits);
   TagDecl::completeDefinition();
 }
 
index 6700dd3af45d4b79bb379cc79ebc96963e6c5198..7e126600b82fc0497972b664e0ba6e477baee3e8 100644 (file)
@@ -142,6 +142,8 @@ void PCHDeclReader::VisitEnumDecl(EnumDecl *ED) {
   VisitTagDecl(ED);
   ED->setIntegerType(Reader.GetType(Record[Idx++]));
   ED->setPromotionType(Reader.GetType(Record[Idx++]));
+  ED->setNumPositiveBits(Record[Idx++]);
+  ED->setNumNegativeBits(Record[Idx++]);
   // FIXME: C++ InstantiatedFrom
 }
 
index d3618df64ece482086650d1a0a909345c177a178..35850934382666e82ed1c475271dee146bbe7312 100644 (file)
@@ -137,6 +137,8 @@ void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) {
   VisitTagDecl(D);
   Writer.AddTypeRef(D->getIntegerType(), Record);
   Writer.AddTypeRef(D->getPromotionType(), Record);
+  Record.push_back(D->getNumPositiveBits());
+  Record.push_back(D->getNumNegativeBits());
   // FIXME: C++ InstantiatedFrom
   Code = pch::DECL_ENUM;
 }
index a802679b26e1e82fd6a8818a5cc050b174a49df5..bf3c17248369e41a6f9645a006fb705e6130dcf7 100644 (file)
@@ -6435,7 +6435,7 @@ void Sema::ActOnEnumBody(SourceLocation EnumLoc, SourceLocation LBraceLoc,
       ECD->setType(EnumType);
     }
 
-    Enum->completeDefinition(Context.DependentTy, Context.DependentTy);
+    Enum->completeDefinition(Context.DependentTy, Context.DependentTy, 0, 0);
     return;
   }
 
@@ -6616,7 +6616,8 @@ void Sema::ActOnEnumBody(SourceLocation EnumLoc, SourceLocation LBraceLoc,
       ECD->setType(NewTy);
   }
 
-  Enum->completeDefinition(BestType, BestPromotionType);
+  Enum->completeDefinition(BestType, BestPromotionType,
+                           NumPositiveBits, NumNegativeBits);
 }
 
 Sema::DeclPtrTy Sema::ActOnFileScopeAsmDecl(SourceLocation Loc,