[cfe-commits] r38950 - in /cfe/cfe/trunk: AST/Expr.cpp AST/Sema.cpp Parse/ParseDecl.cpp Parse/ParseExpr.cpp Parse/ParseStmt.cpp Sema/Sema.cpp include/clang/AST/Expr.h include/clang/Parse/Action.h include/clang/Parse/Parser.h

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


Author: sabre
Date: Wed Jul 11 11:26:08 2007
New Revision: 38950

URL: http://llvm.org/viewvc/llvm-project?rev=38950&view=rev
Log:
Build CastExpr AST nodes

Modified:
    cfe/cfe/trunk/AST/Expr.cpp
    cfe/cfe/trunk/AST/Sema.cpp
    cfe/cfe/trunk/Parse/ParseDecl.cpp
    cfe/cfe/trunk/Parse/ParseExpr.cpp
    cfe/cfe/trunk/Parse/ParseStmt.cpp
    cfe/cfe/trunk/Sema/Sema.cpp
    cfe/cfe/trunk/include/clang/AST/Expr.h
    cfe/cfe/trunk/include/clang/Parse/Action.h
    cfe/cfe/trunk/include/clang/Parse/Parser.h

Modified: cfe/cfe/trunk/AST/Expr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/AST/Expr.cpp?rev=38950&r1=38949&r2=38950&view=diff

==============================================================================
--- cfe/cfe/trunk/AST/Expr.cpp (original)
+++ cfe/cfe/trunk/AST/Expr.cpp Wed Jul 11 11:26:08 2007
@@ -128,6 +128,15 @@
   std::cerr << "member";
 }
 
+
+void CastExpr::dump_impl() const {
+  std::cerr << "'('";
+  // TODO PRINT TYPE
+  std::cerr << "<type>";
+  std::cerr << "')'";
+  Op->dump();
+}
+
 /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
 /// corresponds to, e.g. "<<=".
 const char *BinaryOperator::getOpcodeStr(Opcode Op) {

Modified: cfe/cfe/trunk/AST/Sema.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/AST/Sema.cpp?rev=38950&r1=38949&r2=38950&view=diff

==============================================================================
--- cfe/cfe/trunk/AST/Sema.cpp (original)
+++ cfe/cfe/trunk/AST/Sema.cpp Wed Jul 11 11:26:08 2007
@@ -67,6 +67,8 @@
                                    SourceLocation *CommaLocs,
                                    SourceLocation RParenLoc);
   
+  virtual ExprResult ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
+                                   SourceLocation RParenLoc, ExprTy *Op);
   
   virtual ExprResult ParseBinOp(const LexerToken &Tok, ExprTy *LHS,ExprTy *RHS);
   
@@ -236,6 +238,16 @@
                            CommaLocs, RParenLoc);
 }
 
+Action::ExprResult ASTBuilder::
+ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
+              SourceLocation RParenLoc, ExprTy *Op) {
+  if (!FullLocInfo)
+    return new CastExpr((Type*)Ty, (Expr*)Op);
+  else
+    return new CastExprLOC(LParenLoc, (Type*)Ty, RParenLoc, (Expr*)Op);
+}
+
+
 
 // Binary Operators.  'Tok' is the token for the operator.
 Action::ExprResult ASTBuilder::ParseBinOp(const LexerToken &Tok, ExprTy *LHS,

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

==============================================================================
--- cfe/cfe/trunk/Parse/ParseDecl.cpp (original)
+++ cfe/cfe/trunk/Parse/ParseDecl.cpp Wed Jul 11 11:26:08 2007
@@ -23,7 +23,7 @@
 /// ParseTypeName
 ///       type-name: [C99 6.7.6]
 ///         specifier-qualifier-list abstract-declarator[opt]
-void Parser::ParseTypeName() {
+Parser::TypeTy *Parser::ParseTypeName() {
   // Parse the common declaration-specifiers piece.
   DeclSpec DS;
   ParseSpecifierQualifierList(DS);
@@ -31,6 +31,9 @@
   // Parse the abstract-declarator, if present.
   Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
   ParseDeclarator(DeclaratorInfo);
+  
+  // TODO: Return something useful as the type, obtained from actions.
+  return 0;
 }
 
 /// ParseAttributes - Parse a non-empty attributes list.

Modified: cfe/cfe/trunk/Parse/ParseExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Parse/ParseExpr.cpp?rev=38950&r1=38949&r2=38950&view=diff

==============================================================================
--- cfe/cfe/trunk/Parse/ParseExpr.cpp (original)
+++ cfe/cfe/trunk/Parse/ParseExpr.cpp Wed Jul 11 11:26:08 2007
@@ -430,12 +430,15 @@
   // suffixes.  Cases that cannot be followed by postfix exprs should
   // return without invoking ParsePostfixExpressionSuffix.
   switch (Tok.getKind()) {
-  case tok::l_paren:
+  case tok::l_paren: {
     // If this expression is limited to being a unary-expression, the parent can
     // not start a cast expression.
     ParenParseOption ParenExprType =
       isUnaryExpression ? CompoundLiteral : CastExpr;
-    Res = ParseParenExpression(ParenExprType);
+    TypeTy *CastTy;
+    SourceLocation LParenLoc = Tok.getLocation();
+    SourceLocation RParenLoc;
+    Res = ParseParenExpression(ParenExprType, CastTy, RParenLoc);
     if (Res.isInvalid) return Res;
     
     switch (ParenExprType) {
@@ -448,11 +451,16 @@
     case CastExpr:
       // We parsed '(' type-name ')' and the thing after it wasn't a '{'.  Parse
       // the cast-expression that follows it next.
-      return ParseCastExpression(false);
+      // TODO: For cast expression with CastTy.
+      Res = ParseCastExpression(false);
+      if (!Res.isInvalid)
+        Res = Actions.ParseCastExpr(LParenLoc, CastTy, RParenLoc, Res.Val);
+      return Res;
     }
       
     // These can be followed by postfix-expr pieces.
     return ParsePostfixExpressionSuffix(Res);
+  }
     
     // primary-expression
   case tok::numeric_constant:
@@ -661,7 +669,6 @@
   ConsumeToken();
   
   // If the operand doesn't start with an '(', it must be an expression.
-  // TODO: Build AST.
   ExprResult Operand;
   if (Tok.getKind() != tok::l_paren) {
     Operand = ParseCastExpression(true);
@@ -671,12 +678,15 @@
     // literal, or starts with a primary-expression that is a parenthesized
     // expression.
     ParenParseOption ExprType = CastExpr;
-    Operand = ParseParenExpression(ExprType);
+    TypeTy *CastTy;
+    SourceLocation RParenLoc;
+    Operand = ParseParenExpression(ExprType, CastTy, RParenLoc);
     
     // If ParseParenExpression parsed a '(typename)' sequence only, the this is
     // sizeof/alignof a type.  Otherwise, it is sizeof/alignof an expression.
     if (ExprType == CastExpr) {
-      // TODO: Get type from ParseParenExpression and build AST here.
+      // TODO: Build AST here for sizeof type.
+      CastTy;
       return ExprResult(false);
     }
   }
@@ -831,11 +841,14 @@
 ///       cast-expression: [C99 6.5.4]
 ///         '(' type-name ')' cast-expression
 ///
-Parser::ExprResult Parser::ParseParenExpression(ParenParseOption &ExprType) {
+Parser::ExprResult Parser::ParseParenExpression(ParenParseOption &ExprType,
+                                                TypeTy *&CastTy,
+                                                SourceLocation &RParenLoc) {
   assert(Tok.getKind() == tok::l_paren && "Not a paren expr!");
   SourceLocation OpenLoc = Tok.getLocation();
   ConsumeParen();
   ExprResult Result(false);
+  CastTy = 0;
   
   if (ExprType >= CompoundStmt && Tok.getKind() == tok::l_brace &&
       !getLang().NoExtensions) {
@@ -845,11 +858,16 @@
     // TODO: Build AST for GNU compound stmt.
   } else if (ExprType >= CompoundLiteral && isTypeSpecifierQualifier()) {
     // Otherwise, this is a compound literal expression or cast expression.
-    ParseTypeName();
+    TypeTy *Ty = ParseTypeName();
 
     // Match the ')'.
-    MatchRHSPunctuation(tok::r_paren, OpenLoc);
-
+    if (Tok.getKind() == tok::r_paren) {
+      RParenLoc = Tok.getLocation();
+      ConsumeParen();
+    } else {
+      MatchRHSPunctuation(tok::r_paren, OpenLoc);
+    }
+    
     if (Tok.getKind() == tok::l_brace) {
       if (!getLang().C99)   // Compound literals don't exist in C90.
         Diag(OpenLoc, diag::ext_c99_compound_literal);
@@ -857,9 +875,11 @@
       ExprType = CompoundLiteral;
       // TODO: Build AST for compound literal.
     } else if (ExprType == CastExpr) {
-      // Note that this doesn't parse the subsequence cast-expression.
+      // Note that this doesn't parse the subsequence cast-expression, it just
+      // returns the parsed type to the callee.
       ExprType = CastExpr;
-      // TODO: Build AST for cast in caller.
+      CastTy = Ty;
+      return ExprResult(false);
     } else {
       Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
       return ExprResult(true);
@@ -875,8 +895,14 @@
   // Match the ')'.
   if (Result.isInvalid)
     SkipUntil(tok::r_paren);
-  else
-    MatchRHSPunctuation(tok::r_paren, OpenLoc);
+  else {
+    if (Tok.getKind() == tok::r_paren) {
+      RParenLoc = Tok.getLocation();
+      ConsumeParen();
+    } else {
+      MatchRHSPunctuation(tok::r_paren, OpenLoc);
+    }
+  }
   
   return Result;
 }

Modified: cfe/cfe/trunk/Parse/ParseStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Parse/ParseStmt.cpp?rev=38950&r1=38949&r2=38950&view=diff

==============================================================================
--- cfe/cfe/trunk/Parse/ParseStmt.cpp (original)
+++ cfe/cfe/trunk/Parse/ParseStmt.cpp Wed Jul 11 11:26:08 2007
@@ -356,8 +356,7 @@
   }
   
   // Parse the condition.
-  ParenParseOption ParenExprType = SimpleExpr;
-  ParseParenExpression(ParenExprType);
+  ParseSimpleParenExpression();
   
   // Read the if condition.
   ParseStatement();
@@ -383,8 +382,7 @@
   }
   
   // Parse the condition.
-  ParenParseOption ParenExprType = SimpleExpr;
-  ParseParenExpression(ParenExprType);
+  ParseSimpleParenExpression();
   
   // Read the body statement.
   ParseStatement();
@@ -404,8 +402,7 @@
   }
   
   // Parse the condition.
-  ParenParseOption ParenExprType = SimpleExpr;
-  ParseParenExpression(ParenExprType);
+  ParseSimpleParenExpression();
   
   // Read the body statement.
   ParseStatement();
@@ -438,8 +435,7 @@
   }
   
   // Parse the condition.
-  ParenParseOption ParenExprType = SimpleExpr;
-  ParseParenExpression(ParenExprType);
+  ParseSimpleParenExpression();
 }
 
 /// ParseForStatement
@@ -659,8 +655,7 @@
     }
     
     // Read the parenthesized expression.
-    ParenParseOption ExprTy = SimpleExpr;
-    ExprResult Res = ParseParenExpression(ExprTy);
+    ExprResult Res = ParseSimpleParenExpression();
     if (Res.isInvalid) {
       SkipUntil(tok::r_paren);
       return;

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

==============================================================================
--- cfe/cfe/trunk/Sema/Sema.cpp (original)
+++ cfe/cfe/trunk/Sema/Sema.cpp Wed Jul 11 11:26:08 2007
@@ -67,6 +67,8 @@
                                    SourceLocation *CommaLocs,
                                    SourceLocation RParenLoc);
   
+  virtual ExprResult ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
+                                   SourceLocation RParenLoc, ExprTy *Op);
   
   virtual ExprResult ParseBinOp(const LexerToken &Tok, ExprTy *LHS,ExprTy *RHS);
   
@@ -236,6 +238,16 @@
                            CommaLocs, RParenLoc);
 }
 
+Action::ExprResult ASTBuilder::
+ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
+              SourceLocation RParenLoc, ExprTy *Op) {
+  if (!FullLocInfo)
+    return new CastExpr((Type*)Ty, (Expr*)Op);
+  else
+    return new CastExprLOC(LParenLoc, (Type*)Ty, RParenLoc, (Expr*)Op);
+}
+
+
 
 // Binary Operators.  'Tok' is the token for the operator.
 Action::ExprResult ASTBuilder::ParseBinOp(const LexerToken &Tok, ExprTy *LHS,

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

==============================================================================
--- cfe/cfe/trunk/include/clang/AST/Expr.h (original)
+++ cfe/cfe/trunk/include/clang/AST/Expr.h Wed Jul 11 11:26:08 2007
@@ -21,6 +21,7 @@
 namespace clang {
   class IdentifierInfo;
   class Decl;
+  class Type;
   
 /// Expr - This represents one expression etc.  
 ///
@@ -112,7 +113,6 @@
 
 };
 
-
 //===----------------------------------------------------------------------===//
 // Postfix Operators.
 //===----------------------------------------------------------------------===//
@@ -198,6 +198,27 @@
 
 };
 
+/// CastExpr - [C99 6.5.4] Cast Operators.
+///
+class CastExpr : public Expr {
+  Type *Ty;
+  Expr *Op;
+public:
+  CastExpr(Type *ty, Expr *op) : Ty(ty), Op(op) {}
+  
+  virtual void dump_impl() const;
+};
+
+class CastExprLOC : public CastExpr {
+  SourceLocation LParenLoc, RParenLoc;
+public:
+  CastExprLOC(SourceLocation lparenloc, Type *Ty, SourceLocation rparenloc,
+              Expr *Op)
+  : CastExpr(Ty, Op), LParenLoc(lparenloc), RParenLoc(rparenloc) {
+  }
+};
+
+
 class BinaryOperator : public Expr {
 public:
   enum Opcode {

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

==============================================================================
--- cfe/cfe/trunk/include/clang/Parse/Action.h (original)
+++ cfe/cfe/trunk/include/clang/Parse/Action.h Wed Jul 11 11:26:08 2007
@@ -48,6 +48,7 @@
   // what types are required to be identical for the actions.
   typedef void ExprTy;
   typedef void DeclTy;
+  typedef void TypeTy;
   
   /// ExprResult - This structure is used while parsing/acting on expressions.
   /// It encapsulates both the expression object returned by the action, plus
@@ -130,6 +131,11 @@
     return 0;
   }
 
+  virtual ExprResult ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
+                                   SourceLocation RParenLoc, ExprTy *Op) {
+    return 0;
+  }
+  
   virtual ExprResult ParseBinOp(const LexerToken &Tok,
                                 ExprTy *LHS, ExprTy *RHS) {
     return 0;

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

==============================================================================
--- cfe/cfe/trunk/include/clang/Parse/Parser.h (original)
+++ cfe/cfe/trunk/include/clang/Parse/Parser.h Wed Jul 11 11:26:08 2007
@@ -48,6 +48,7 @@
   // different actual classes based on the actions in place.
   typedef Action::ExprTy ExprTy;
   typedef Action::DeclTy DeclTy;
+  typedef Action::TypeTy TypeTy;
   
   // Parsing methods.
   
@@ -243,7 +244,15 @@
     CompoundLiteral, // Also allow '(' type-name ')' '{' ... '}'
     CastExpr         // Also allow '(' type-name ')' <anything>
   };
-  ExprResult ParseParenExpression(ParenParseOption &ExprType);
+  ExprResult ParseParenExpression(ParenParseOption &ExprType, TypeTy *&CastTy,
+                                  SourceLocation &RParenLoc);
+  
+  ExprResult ParseSimpleParenExpression() {  // Parse SimpleExpr only.
+    ParenParseOption Op = SimpleExpr;
+    TypeTy *CastTy;
+    SourceLocation RParenLoc;
+    return ParseParenExpression(Op, CastTy, RParenLoc);
+  }
   ExprResult ParseStringLiteralExpression();
   
   //===--------------------------------------------------------------------===//
@@ -284,7 +293,7 @@
   bool isDeclarationSpecifier() const;
   bool isTypeSpecifierQualifier() const;
 
-  void ParseTypeName();
+  TypeTy *ParseTypeName();
   void ParseAttributes();
   
   /// ParseDeclarator - Parse and verify a newly-initialized declarator.





More information about the cfe-commits mailing list