Initial implementation of arbitrary fixed-width integer types.  
Currently only used for 128-bit integers.

Note that we can't use the fixed-width integer types for other integer 
modes without other changes because glibc headers redefines (u)int*_t 
and friends using the mode attribute.  For example, this means that uint64_t
has to be compatible with unsigned __attribute((mode(DI))), and 
uint64_t is currently defined to long long.  And I have a feeling we'll 
run into issues if we try to define uint64_t as something which isn't 
either long or long long.

This doesn't get the alignment right in most cases, including 
the 128-bit integer case; I'll file a PR shortly.  The gist of the issue 
is that the targets don't really expose the information necessary to 
figure out the alignment outside of the target description, so there's a 
non-trivial amount of work involved in getting it working right.  That 
said, the alignment used is conservative, so the only issue with the 
current implementation is ABI compatibility.

This makes it trivial to add some sort of "bitwidth" attribute to make 
arbitrary-width integers; I'll do that in a followup.

We could also use this for stuff like the following for compatibility 
with gcc, but I have a feeling it would be a better idea for clang to be 
consistent between C and C++ modes rather than follow gcc's example for 
C mode.
struct {unsigned long long x : 33;} x;
unsigned long long a(void) {return x.x+1;}



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@64434 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp
index c953150..d4fe26e 100644
--- a/lib/AST/ASTContext.cpp
+++ b/lib/AST/ASTContext.cpp
@@ -373,6 +373,13 @@
       break;
     }
     break;
+  case Type::FixedWidthInt:
+    // FIXME: This isn't precisely correct; the width/alignment should depend
+    // on the available types for the target
+    Width = cast<FixedWidthIntType>(T)->getWidth();
+    Width = std::max(llvm::NextPowerOf2(Width - 1), 8ULL);
+    Align = Width;
+    break;
   case Type::ASQual:
     // FIXME: Pointers into different addr spaces could have different sizes and
     // alignment requirements: getPointerInfo should take an AddrSpace.
@@ -769,6 +776,14 @@
   return QualType(New, 0);
 }
 
+QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
+  llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
+     SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
+  FixedWidthIntType *&Entry = Map[Width];
+  if (!Entry)
+    Entry = new FixedWidthIntType(Width, Signed);
+  return QualType(Entry, 0);
+}
 
 /// getPointerType - Return the uniqued reference to the type for a pointer to
 /// the specified type.
@@ -1599,32 +1614,39 @@
 /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
 /// routine will assert if passed a built-in type that isn't an integer or enum,
 /// or if it is not canonicalized.
-static unsigned getIntegerRank(Type *T) {
+unsigned ASTContext::getIntegerRank(Type *T) {
   assert(T->isCanonical() && "T should be canonicalized");
-  if (isa<EnumType>(T))
-    return 4;
-  
+  if (EnumType* ET = dyn_cast<EnumType>(T))
+    T = ET->getDecl()->getIntegerType().getTypePtr();
+
+  // There are two things which impact the integer rank: the width, and
+  // the ordering of builtins.  The builtin ordering is encoded in the
+  // bottom three bits; the width is encoded in the bits above that.
+  if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
+    return FWIT->getWidth() << 3;
+  }
+
   switch (cast<BuiltinType>(T)->getKind()) {
   default: assert(0 && "getIntegerRank(): not a built-in integer");
   case BuiltinType::Bool:
-    return 1;
+    return 1 + (getIntWidth(BoolTy) << 3);
   case BuiltinType::Char_S:
   case BuiltinType::Char_U:
   case BuiltinType::SChar:
   case BuiltinType::UChar:
-    return 2;
+    return 2 + (getIntWidth(CharTy) << 3);
   case BuiltinType::Short:
   case BuiltinType::UShort:
-    return 3;
+    return 3 + (getIntWidth(ShortTy) << 3);
   case BuiltinType::Int:
   case BuiltinType::UInt:
-    return 4;
+    return 4 + (getIntWidth(IntTy) << 3);
   case BuiltinType::Long:
   case BuiltinType::ULong:
-    return 5;
+    return 5 + (getIntWidth(LongTy) << 3);
   case BuiltinType::LongLong:
   case BuiltinType::ULongLong:
-    return 6;
+    return 6 + (getIntWidth(LongLongTy) << 3);
   }
 }
 
@@ -2718,7 +2740,10 @@
 unsigned ASTContext::getIntWidth(QualType T) {
   if (T == BoolTy)
     return 1;
-  // At the moment, only bool has padding bits
+  if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
+    return FWIT->getWidth();
+  }
+  // For builtin types, just use the standard type sizing method
   return (unsigned)getTypeSize(T);
 }