[cfe-commits] r38836 - in /cfe/cfe/trunk: Parse/ParseDecl.cpp include/clang/Parse/DeclSpec.h

sabre at cs.uiuc.edu sabre at cs.uiuc.edu
Wed Jul 11 09:25:03 PDT 2007


Author: sabre
Date: Wed Jul 11 11:25:03 2007
New Revision: 38836

URL: http://llvm.org/viewvc/llvm-project?rev=38836&view=rev
Log:
Start capturing pointer and array declarator info.

Modified:
    cfe/cfe/trunk/Parse/ParseDecl.cpp
    cfe/cfe/trunk/include/clang/Parse/DeclSpec.h

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

==============================================================================
--- cfe/cfe/trunk/Parse/ParseDecl.cpp (original)
+++ cfe/cfe/trunk/Parse/ParseDecl.cpp Wed Jul 11 11:25:03 2007
@@ -243,25 +243,6 @@
 }
 
 
-/// ParseDeclarator
-///       declarator: [C99 6.7.5]
-///         pointer[opt] direct-declarator
-///
-///       pointer: [C99 6.7.5]
-///         '*' type-qualifier-list[opt]
-///         '*' type-qualifier-list[opt] pointer
-///
-void Parser::ParseDeclarator(Declarator &D) {
-  while (Tok.getKind() == tok::star) {  // '*' -> pointer.
-    ConsumeToken();  // Eat the *.
-    DeclSpec DS;
-    ParseTypeQualifierListOpt(DS);
-    // TODO: do something with DS.
-  }
-  
-  ParseDirectDeclarator(D);
-}
-
 /// ParseTypeQualifierListOpt
 ///       type-qualifier-list: [C99 6.7.5]
 ///         type-qualifier
@@ -305,6 +286,31 @@
   }
 }
 
+/// ParseDeclarator
+///       declarator: [C99 6.7.5]
+///         pointer[opt] direct-declarator
+///
+///       pointer: [C99 6.7.5]
+///         '*' type-qualifier-list[opt]
+///         '*' type-qualifier-list[opt] pointer
+///
+void Parser::ParseDeclarator(Declarator &D) {
+  if (Tok.getKind() != tok::star)
+    return ParseDirectDeclarator(D);
+  
+  // Otherwise, '*' -> pointer.
+  SourceLocation Loc = Tok.getLocation();
+  ConsumeToken();  // Eat the *.
+  DeclSpec DS;
+  ParseTypeQualifierListOpt(DS);
+  
+  // Recursively parse the declarator.
+  ParseDeclarator(D);
+  
+  // Remember that we parsed a pointer type, and remember the type-quals.
+  D.AddTypeInfo(DeclaratorTypeInfo::getPointer(DS.TypeQualifiers, Loc));
+}
+
 
 /// ParseDirectDeclarator
 ///       direct-declarator: [C99 6.7.5]
@@ -555,7 +561,6 @@
   // If there is a type-qualifier-list, read it now.
   DeclSpec DS;
   ParseTypeQualifierListOpt(DS);
-  // TODO: do something with DS.
   
   // If we haven't already read 'static', check to see if there is one after the
   // type-qualifier-list.
@@ -589,8 +594,10 @@
     }
   }
   
+  void *NumElts = 0;
   if (!isStar && Tok.getKind() != tok::r_square) {
     // Parse the assignment-expression now.
+    NumElts = /*FIXME: parse array size expr*/0;
     assert(0 && "expr parsing not impl yet!");
   }
   
@@ -600,8 +607,13 @@
   // it was not a constant expression.
   if (!getLang().C99) {
     // TODO: check C90 array constant exprness.
-    if (isStar || StaticLoc.isValid() || 0/*constantexpr*/)
+    if (isStar || StaticLoc.isValid() || 0/*NumElts is constantexpr*/)
       Diag(StartLoc, diag::ext_c99_array_usage);
   }
+  
+  // Remember that we parsed a pointer type, and remember the type-quals.
+  D.AddTypeInfo(DeclaratorTypeInfo::getArray(DS.TypeQualifiers,
+                                             StaticLoc.isValid(), isStar,
+                                             NumElts, StartLoc));
 }
 

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

==============================================================================
--- cfe/cfe/trunk/include/clang/Parse/DeclSpec.h (original)
+++ cfe/cfe/trunk/include/clang/Parse/DeclSpec.h Wed Jul 11 11:25:03 2007
@@ -16,6 +16,7 @@
 
 #include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/SourceLocation.h"
+#include "llvm/ADT/SmallVector.h"
 
 namespace llvm {
 namespace clang {
@@ -131,13 +132,79 @@
 };
 
 
+/// DeclaratorTypeInfo - One instance of this struct is used for each type in a
+/// declarator that is parsed.
+///
+/// This is intended to be a small value object.
+struct DeclaratorTypeInfo {
+  enum {
+    Pointer, Array, Function
+  } Kind;
+  
+  /// Loc - The place where this type was defined.
+  SourceLocation Loc;
+  
+  struct PointerTypeInfo {
+    /// The type qualifiers: const/volatile/restrict.
+    unsigned TypeQuals : 3;
+  };
+  struct ArrayTypeInfo {
+    /// The type qualifiers for the array: const/volatile/restrict.
+    unsigned TypeQuals : 3;
+    
+    /// True if this dimension included the 'static' keyword.
+    bool Static : 1;
+    
+    /// True if this dimension was [*].  In this case, NumElts is null.
+    bool Star : 1;
+    
+    /// This is the size of the array, or null if [] or [*] was specified.
+    /// FIXME: make this be an expression* when we have expressions.
+    void *NumElts;
+  };
+  
+  union {
+    PointerTypeInfo Ptr;
+    ArrayTypeInfo Arr;
+  };
+  
+  
+  /// getPointer - Return a DeclaratorTypeInfo for a pointer.
+  ///
+  static DeclaratorTypeInfo getPointer(unsigned TypeQuals, SourceLocation Loc) {
+    DeclaratorTypeInfo I;
+    I.Kind          = Pointer;
+    I.Loc           = Loc;
+    I.Ptr.TypeQuals = TypeQuals;
+    return I;
+  }
+  /// getArray - Return a DeclaratorTypeInfo for an array.
+  ///
+  static DeclaratorTypeInfo getArray(unsigned TypeQuals, bool isStatic,
+                                     bool isStar, void *NumElts,
+                                     SourceLocation Loc) {
+    DeclaratorTypeInfo I;
+    I.Kind          = Array;
+    I.Loc           = Loc;
+    I.Arr.TypeQuals = TypeQuals;
+    I.Arr.Static    = isStatic;
+    I.Arr.Star      = isStar;
+    I.Arr.NumElts   = NumElts;
+    return I;
+  }
+};
+
+
 /// DeclaratorInfo - Information about one declarator, including the parsed type
 /// information and the identifier.  When the declarator is fully formed, this
 /// is turned into the appropriate Decl object.
 ///
 /// Declarators come in two types: normal declarators and abstract declarators.
 /// Abstract declarators are used when parsing types, and don't have an
-/// identifier.  Normal declarators do have ID's.  One strange case s 
+/// identifier.  Normal declarators do have ID's.
+///
+/// This is NOT intended to be a small value object: this should be a transient
+/// object that lives on the stack.
 class Declarator {
   const DeclSpec &DS;
   IdentifierInfo *Identifier;
@@ -157,6 +224,11 @@
   /// Context - Where we are parsing this declarator.
   ///
   TheContext Context;
+  
+  /// DeclTypeInfo - The final pieces of information is information about each
+  /// type parsed as we parse it.
+  SmallVector<DeclaratorTypeInfo, 8> DeclTypeInfo;
+  
 public:
   Declarator(const DeclSpec &ds, TheContext C)
     : DS(ds), Identifier(0), Context(C) {
@@ -186,6 +258,10 @@
     Identifier = ID;
     IdentifierLoc = Loc;
   }
+  
+  void AddTypeInfo(const DeclaratorTypeInfo &TI) {
+    DeclTypeInfo.push_back(TI);
+  }
 };
 
   





More information about the cfe-commits mailing list