[cfe-commits] r39176 - in /cfe/cfe/trunk: AST/ASTContext.cpp AST/SemaType.cpp AST/Type.cpp Sema/SemaType.cpp include/clang/AST/ASTContext.h include/clang/AST/Type.h

sabre at cs.uiuc.edu sabre at cs.uiuc.edu
Wed Jul 11 09:40:36 PDT 2007


Author: sabre
Date: Wed Jul 11 11:40:35 2007
New Revision: 39176

URL: http://llvm.org/viewvc/llvm-project?rev=39176&view=rev
Log:
Implement parsing, printing and AST'ing of array types (except for the bounds).
This allows us to handle:

int (*A)[restrict static 4][6];

for example.

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

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

==============================================================================
--- cfe/cfe/trunk/AST/ASTContext.cpp (original)
+++ cfe/cfe/trunk/AST/ASTContext.cpp Wed Jul 11 11:40:35 2007
@@ -72,16 +72,17 @@
 
 /// getPointerType - Return the uniqued reference to the type for a pointer to
 /// the specified type.
-TypeRef ASTContext::getPointerType(const TypeRef &T) {
+TypeRef ASTContext::getPointerType(TypeRef T) {
   // FIXME: This is obviously braindead!
   // Unique pointers, to guarantee there is only one pointer of a particular
   // structure.
   for (unsigned i = 0, e = Types.size(); i != e; ++i)
     if (PointerType *PTy = dyn_cast<PointerType>(Types[i]))
-      if (PTy->getPointee() == T)
+      if (PTy->getPointeeType() == T)
         return Types[i];
   
-  
+  // If the pointee type isn't canonical, this won't be a canonical type either,
+  // so fill in the canonical type field.
   Type *Canonical = 0;
   if (!T->isCanonical())
     Canonical = getPointerType(T.getCanonicalType()).getTypePtr();
@@ -90,4 +91,31 @@
   return Types.back();
 }
 
+/// getArrayType - Return the unique reference to the type for an array of the
+/// specified element type.
+TypeRef ASTContext::getArrayType(TypeRef EltTy,ArrayType::ArraySizeModifier ASM,
+                                 unsigned EltTypeQuals, void *NumElts) {
+#warning "IGNORING SIZE"
+  
+  // FIXME: This is obviously braindead!
+  // Unique array, to guarantee there is only one array of a particular
+  // structure.
+  for (unsigned i = 0, e = Types.size(); i != e; ++i)
+    if (ArrayType *ATy = dyn_cast<ArrayType>(Types[i]))
+      if (ATy->getElementType() == EltTy &&
+          ATy->getSizeModifier() == ASM &&
+          ATy->getIndexTypeQualifier() == EltTypeQuals)
+        return Types[i];
+  
+  // If the element type isn't canonical, this won't be a canonical type either,
+  // so fill in the canonical type field.
+  Type *Canonical = 0;
+  if (!EltTy->isCanonical())
+    Canonical = getArrayType(EltTy.getCanonicalType(), ASM, EltTypeQuals,
+                             NumElts).getTypePtr();
+  
+  Types.push_back(new ArrayType(EltTy, ASM, EltTypeQuals, Canonical));
+  return Types.back();
+}
+
 

Modified: cfe/cfe/trunk/AST/SemaType.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/AST/SemaType.cpp?rev=39176&r1=39175&r2=39176&view=diff

==============================================================================
--- cfe/cfe/trunk/AST/SemaType.cpp (original)
+++ cfe/cfe/trunk/AST/SemaType.cpp Wed Jul 11 11:40:35 2007
@@ -98,7 +98,19 @@
       // Apply the pointer typequals to the pointer object.
       T = T.getQualifiedType(DeclType.Ptr.TypeQuals);
       break;
-    case DeclaratorTypeInfo::Array:
+    case DeclaratorTypeInfo::Array: {
+      const DeclaratorTypeInfo::ArrayTypeInfo &ATI = DeclType.Arr;
+      ArrayType::ArraySizeModifier ASM;
+      if (ATI.isStar)
+        ASM = ArrayType::Star;
+      else if (ATI.hasStatic)
+        ASM = ArrayType::Static;
+      else
+        ASM = ArrayType::Normal;
+      
+      T = Context.getArrayType(T, ASM, ATI.TypeQuals, ATI.NumElts);
+      break;
+    }
     case DeclaratorTypeInfo::Function:
       return TypeRef();   // FIXME: implement these!
     }

Modified: cfe/cfe/trunk/AST/Type.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/AST/Type.cpp?rev=39176&r1=39175&r2=39176&view=diff

==============================================================================
--- cfe/cfe/trunk/AST/Type.cpp (original)
+++ cfe/cfe/trunk/AST/Type.cpp Wed Jul 11 11:40:35 2007
@@ -18,15 +18,6 @@
 
 Type::~Type() {}
 
-//===----------------------------------------------------------------------===//
-// Type Construction
-//===----------------------------------------------------------------------===//
-
-PointerType::PointerType(TypeRef Pointee, Type *Canonical)
-  : Type(Pointer, Canonical), PointeeType(Pointee) {
-}
-
-
 
 //===----------------------------------------------------------------------===//
 // Type Printing
@@ -37,6 +28,17 @@
   std::cerr << "\n";
 }
 
+static void PrintTypeQualList(std::ostream &OS, unsigned TypeQuals) {
+  // Note: funkiness to ensure we get a space only between quals.
+  bool NonePrinted = true;
+  if (TypeQuals & TypeRef::Const)
+    OS << "const", NonePrinted = false;
+  if (TypeQuals & TypeRef::Volatile)
+    OS << (NonePrinted+" volatile"), NonePrinted = false;
+  if (TypeQuals & TypeRef::Restrict)
+    OS << (NonePrinted+" restrict"), NonePrinted = false;
+  }
+
 void TypeRef::print(std::ostream &OS) const {
   if (isNull()) {
     OS << "NULL TYPE\n";
@@ -46,12 +48,10 @@
   getTypePtr()->print(OS);
   
   // Print qualifiers as appropriate.
-  if (isConstQualified())
-    OS << " const";
-  if (isVolatileQualified())
-    OS << " volatile";
-  if (isRestrictQualified())
-    OS << " restrict";
+  if (unsigned TQ = getQualifiers()) {
+    OS << " ";
+    PrintTypeQualList(OS, TQ);
+  }
 }
 
 void BuiltinType::print(std::ostream &OS) const {
@@ -62,3 +62,20 @@
   PointeeType.print(OS);
   OS << "*";
 }
+
+void ArrayType::print(std::ostream &OS) const {
+  ElementType.print(OS);
+  OS << "[";
+  
+  if (IndexTypeQuals) {
+    PrintTypeQualList(OS, IndexTypeQuals);
+    OS << " ";
+  }
+  
+  if (SizeModifier == Static)
+    OS << "static";
+  else if (SizeModifier == Star)
+    OS << "*";
+  
+  OS << "]";
+}

Modified: cfe/cfe/trunk/Sema/SemaType.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Sema/SemaType.cpp?rev=39176&r1=39175&r2=39176&view=diff

==============================================================================
--- cfe/cfe/trunk/Sema/SemaType.cpp (original)
+++ cfe/cfe/trunk/Sema/SemaType.cpp Wed Jul 11 11:40:35 2007
@@ -98,7 +98,19 @@
       // Apply the pointer typequals to the pointer object.
       T = T.getQualifiedType(DeclType.Ptr.TypeQuals);
       break;
-    case DeclaratorTypeInfo::Array:
+    case DeclaratorTypeInfo::Array: {
+      const DeclaratorTypeInfo::ArrayTypeInfo &ATI = DeclType.Arr;
+      ArrayType::ArraySizeModifier ASM;
+      if (ATI.isStar)
+        ASM = ArrayType::Star;
+      else if (ATI.hasStatic)
+        ASM = ArrayType::Static;
+      else
+        ASM = ArrayType::Normal;
+      
+      T = Context.getArrayType(T, ASM, ATI.TypeQuals, ATI.NumElts);
+      break;
+    }
     case DeclaratorTypeInfo::Function:
       return TypeRef();   // FIXME: implement these!
     }

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

==============================================================================
--- cfe/cfe/trunk/include/clang/AST/ASTContext.h (original)
+++ cfe/cfe/trunk/include/clang/AST/ASTContext.h Wed Jul 11 11:40:35 2007
@@ -47,7 +47,13 @@
   
   /// getPointerType - Return the uniqued reference to the type for a pointer to
   /// the specified type.
-  TypeRef getPointerType(const TypeRef &T);
+  TypeRef getPointerType(TypeRef T);
+  
+  /// getArrayType - Return the unique reference to the type for an array of the
+  /// specified element type.
+  TypeRef getArrayType(TypeRef EltTy, ArrayType::ArraySizeModifier ASM,
+                       unsigned EltTypeQuals, void *NumElts);
+                       
   
 private:
   void InitBuiltinTypes();

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

==============================================================================
--- cfe/cfe/trunk/include/clang/AST/Type.h (original)
+++ cfe/cfe/trunk/include/clang/AST/Type.h Wed Jul 11 11:40:35 2007
@@ -54,6 +54,9 @@
     ThePtr |= Quals;
   }
   
+  unsigned getQualifiers() const {
+    return ThePtr & CVRFlags;
+  }
   Type *getTypePtr() const {
     return reinterpret_cast<Type*>(ThePtr & ~CVRFlags);
   }
@@ -80,9 +83,6 @@
   bool isRestrictQualified() const {
     return ThePtr & Restrict;
   }
-  unsigned getQualifiers() const {
-    return ThePtr & CVRFlags;
-  }
   
   TypeRef getQualifiedType(unsigned TQs) const {
     return TypeRef(getTypePtr(), TQs);
@@ -138,18 +138,18 @@
 class Type {
 public:
   enum TypeClass {
-    Builtin,
-    Pointer,
-    Typedef
-    // ...
+    Builtin, Pointer, Array, Typedef
   };
 private:
-  TypeClass TC : 4;
   Type *CanonicalType;
+
+  /// TypeClass bitfield - Enum that specifies what subclass this belongs to.
+  /// Note that this should stay at the end of the ivars for Type so that
+  /// subclasses can pack their bitfields into the same word.
+  TypeClass TC : 3;
 public:
-  
   Type(TypeClass tc, Type *Canonical)
-    : TC(tc), CanonicalType(Canonical ? Canonical : this) {}
+    : CanonicalType(Canonical ? Canonical : this), TC(tc) {}
   virtual ~Type();
   
   TypeClass getTypeClass() const { return TC; }
@@ -176,13 +176,17 @@
   static bool classof(const BuiltinType *) { return true; }
 };
 
+/// PointerType - C99 6.7.4.1 - Pointer Declarators.
+///
 class PointerType : public Type {
   TypeRef PointeeType;
-  PointerType(TypeRef Pointee, Type *CanonicalPtr = 0);
+  PointerType(TypeRef Pointee, Type *CanonicalPtr) :
+    Type(Pointer, CanonicalPtr), PointeeType(Pointee) {
+  }
   friend class ASTContext;  // ASTContext creates these.
 public:
     
-  TypeRef getPointee() const { return PointeeType; }
+  TypeRef getPointeeType() const { return PointeeType; }
   
   virtual void print(std::ostream &OS) const;
   
@@ -190,6 +194,45 @@
   static bool classof(const PointerType *) { return true; }
 };
 
+/// PointerType - C99 6.7.4.2 - Array Declarators.
+///
+class ArrayType : public Type {
+public:
+  /// ArraySizeModifier - Capture whether this is a normal array (e.g. int X[4])
+  /// an array with a static size (e.g. int X[static 4]), or with a star size
+  /// (e.g. int X[*]).
+  enum ArraySizeModifier {
+    Normal, Static, Star
+  };
+private:
+  /// NOTE: These fields are packed into the bitfields space in the Type class.
+  ArraySizeModifier SizeModifier : 2;
+  
+  /// IndexTypeQuals - Capture qualifiers in declarations like:
+  /// 'int X[static restrict 4]'.
+  unsigned IndexTypeQuals : 3;
+  
+  /// ElementType - The element type of the array.
+  TypeRef ElementType;
+  
+  /// FIXME: Capture size for VLA or constant size.
+
+  ArrayType(TypeRef et, ArraySizeModifier sm, unsigned tq, Type *can)
+    : Type(Array, can), SizeModifier(sm), IndexTypeQuals(tq), ElementType(et) {}
+  friend class ASTContext;  // ASTContext creates these.
+public:
+    
+  TypeRef getElementType() const { return ElementType; }
+  ArraySizeModifier getSizeModifier() const { return SizeModifier; }
+  unsigned getIndexTypeQualifier() const { return IndexTypeQuals; }
+  
+  virtual void print(std::ostream &OS) const;
+  
+  static bool classof(const Type *T) { return T->getTypeClass() == Array; }
+  static bool classof(const ArrayType *) { return true; }
+};
+
+
 class TypedefType : public Type {
   // Decl * here.
 public:





More information about the cfe-commits mailing list