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/Type.cpp b/lib/AST/Type.cpp
index 24cf853..ad5026b 100644
--- a/lib/AST/Type.cpp
+++ b/lib/AST/Type.cpp
@@ -510,6 +510,8 @@
     // FIXME: In C++, enum types are never integer types.
     if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
       return true;
+  if (isa<FixedWidthIntType>(CanonicalType))
+    return true;
   if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
     return VT->getElementType()->isIntegerType();
   if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
@@ -525,6 +527,8 @@
     if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
       return true;  // Complete enum types are integral.
                     // FIXME: In C++, enum types are never integral.
+  if (isa<FixedWidthIntType>(CanonicalType))
+    return true;
   if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
     return ASQT->getBaseType()->isIntegralType();
   return false;
@@ -578,6 +582,10 @@
   if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
     return ET->getDecl()->getIntegerType()->isSignedIntegerType();
   
+  if (const FixedWidthIntType *FWIT =
+          dyn_cast<FixedWidthIntType>(CanonicalType))
+    return FWIT->isSigned();
+  
   if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
     return VT->getElementType()->isSignedIntegerType();
   if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
@@ -598,6 +606,10 @@
   if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
     return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
 
+  if (const FixedWidthIntType *FWIT =
+          dyn_cast<FixedWidthIntType>(CanonicalType))
+    return !FWIT->isSigned();
+
   if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
     return VT->getElementType()->isUnsignedIntegerType();
   if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
@@ -635,6 +647,8 @@
            BT->getKind() <= BuiltinType::LongDouble;
   if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
     return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
+  if (isa<FixedWidthIntType>(CanonicalType))
+    return true;
   if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
     return VT->getElementType()->isRealType();
   if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
@@ -650,6 +664,8 @@
     // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
     // If a body isn't seen by the time we get here, return false.
     return ET->getDecl()->isDefinition();
+  if (isa<FixedWidthIntType>(CanonicalType))
+    return true;
   if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
     return ASQT->getBaseType()->isArithmeticType();
   return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
@@ -667,6 +683,8 @@
   }
   if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
     return ASQT->getBaseType()->isScalarType();
+  if (isa<FixedWidthIntType>(CanonicalType))
+    return true;
   return isa<PointerType>(CanonicalType) ||
          isa<BlockPointerType>(CanonicalType) ||
          isa<MemberPointerType>(CanonicalType) ||
@@ -1014,6 +1032,21 @@
   }
 }
 
+void FixedWidthIntType::getAsStringInternal(std::string &S) const {
+  // FIXME: Once we get bitwidth attribute, write as
+  // "int __attribute__((bitwidth(x)))".
+  std::string prefix = "__clang_fixedwidth";
+  prefix += llvm::utostr_32(Width);
+  prefix += (char)(Signed ? 'S' : 'U');
+  if (S.empty()) {
+    S = prefix;
+  } else {
+    // Prefix the basic type, e.g. 'int X'.
+    S = prefix + S;
+  }
+}
+
+
 void ComplexType::getAsStringInternal(std::string &S) const {
   ElementType->getAsStringInternal(S);
   S = "_Complex " + S;