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

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


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

URL: http://llvm.org/viewvc/llvm-project?rev=38949&view=rev
Log:
Build AST's for sizeof/alignof an expr.

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

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

==============================================================================
--- cfe/cfe/trunk/AST/Expr.cpp (original)
+++ cfe/cfe/trunk/AST/Expr.cpp Wed Jul 11 11:26:08 2007
@@ -66,6 +66,8 @@
   case LNot:    return "!";
   case Real:    return "__real";
   case Imag:    return "__imag";
+  case SizeOf:  return "sizeof";
+  case AlignOf: return "alignof";
   }
 }
 

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

==============================================================================
--- cfe/cfe/trunk/AST/Sema.cpp (original)
+++ cfe/cfe/trunk/AST/Sema.cpp Wed Jul 11 11:26:08 2007
@@ -165,16 +165,18 @@
   UnaryOperator::Opcode Opc;
   switch (Tok.getKind()) {
   default: assert(0 && "Unknown unary op!");
-  case tok::plusplus:   Opc = UnaryOperator::PreInc; break;
-  case tok::minusminus: Opc = UnaryOperator::PreDec; break;
-  case tok::amp:        Opc = UnaryOperator::AddrOf; break;
-  case tok::star:       Opc = UnaryOperator::Deref; break;
-  case tok::plus:       Opc = UnaryOperator::Plus; break;
-  case tok::minus:      Opc = UnaryOperator::Minus; break;
-  case tok::tilde:      Opc = UnaryOperator::Not; break;
-  case tok::exclaim:    Opc = UnaryOperator::LNot; break;
-  case tok::kw___real:  Opc = UnaryOperator::Real; break;
-  case tok::kw___imag:  Opc = UnaryOperator::Imag; break;
+  case tok::plusplus:     Opc = UnaryOperator::PreInc; break;
+  case tok::minusminus:   Opc = UnaryOperator::PreDec; break;
+  case tok::amp:          Opc = UnaryOperator::AddrOf; break;
+  case tok::star:         Opc = UnaryOperator::Deref; break;
+  case tok::plus:         Opc = UnaryOperator::Plus; break;
+  case tok::minus:        Opc = UnaryOperator::Minus; break;
+  case tok::tilde:        Opc = UnaryOperator::Not; break;
+  case tok::exclaim:      Opc = UnaryOperator::LNot; break;
+  case tok::kw___real:    Opc = UnaryOperator::Real; break;
+  case tok::kw___imag:    Opc = UnaryOperator::Imag; break;
+  case tok::kw_sizeof:    Opc = UnaryOperator::SizeOf; break;
+  case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
   }
 
   if (!FullLocInfo)

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

==============================================================================
--- cfe/cfe/trunk/Parse/ParseExpr.cpp (original)
+++ cfe/cfe/trunk/Parse/ParseExpr.cpp Wed Jul 11 11:26:08 2007
@@ -657,19 +657,34 @@
   assert((Tok.getKind() == tok::kw_sizeof ||
           Tok.getKind() == tok::kw___alignof) &&
          "Not a sizeof/alignof expression!");
+  LexerToken OpTok = Tok;
   ConsumeToken();
   
   // If the operand doesn't start with an '(', it must be an expression.
   // TODO: Build AST.
-  if (Tok.getKind() != tok::l_paren)
-    return ParseCastExpression(true);
+  ExprResult Operand;
+  if (Tok.getKind() != tok::l_paren) {
+    Operand = ParseCastExpression(true);
+  } else {
+    // If it starts with a '(', we know that it is either a parenthesized
+    // type-name, or it is a unary-expression that starts with a compound
+    // literal, or starts with a primary-expression that is a parenthesized
+    // expression.
+    ParenParseOption ExprType = CastExpr;
+    Operand = ParseParenExpression(ExprType);
+    
+    // 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.
+      return ExprResult(false);
+    }
+  }
   
-  // If it starts with a '(', we know that it is either a parenthesized
-  // type-name, or it is a unary-expression that starts with a compound literal,
-  // or starts with a primary-expression that is a parenthesized expression.
-  ParenParseOption ExprType = CastExpr;
-  // TODO: Build AST.
-  return ParseParenExpression(ExprType);
+  // If we get here, the operand to the sizeof/alignof was an expresion.
+  if (!Operand.isInvalid)
+    Operand = Actions.ParseUnaryOp(OpTok, Operand.Val);
+  return Operand;
 }
 
 /// ParseBuiltinPrimaryExpression

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

==============================================================================
--- cfe/cfe/trunk/Sema/Sema.cpp (original)
+++ cfe/cfe/trunk/Sema/Sema.cpp Wed Jul 11 11:26:08 2007
@@ -165,16 +165,18 @@
   UnaryOperator::Opcode Opc;
   switch (Tok.getKind()) {
   default: assert(0 && "Unknown unary op!");
-  case tok::plusplus:   Opc = UnaryOperator::PreInc; break;
-  case tok::minusminus: Opc = UnaryOperator::PreDec; break;
-  case tok::amp:        Opc = UnaryOperator::AddrOf; break;
-  case tok::star:       Opc = UnaryOperator::Deref; break;
-  case tok::plus:       Opc = UnaryOperator::Plus; break;
-  case tok::minus:      Opc = UnaryOperator::Minus; break;
-  case tok::tilde:      Opc = UnaryOperator::Not; break;
-  case tok::exclaim:    Opc = UnaryOperator::LNot; break;
-  case tok::kw___real:  Opc = UnaryOperator::Real; break;
-  case tok::kw___imag:  Opc = UnaryOperator::Imag; break;
+  case tok::plusplus:     Opc = UnaryOperator::PreInc; break;
+  case tok::minusminus:   Opc = UnaryOperator::PreDec; break;
+  case tok::amp:          Opc = UnaryOperator::AddrOf; break;
+  case tok::star:         Opc = UnaryOperator::Deref; break;
+  case tok::plus:         Opc = UnaryOperator::Plus; break;
+  case tok::minus:        Opc = UnaryOperator::Minus; break;
+  case tok::tilde:        Opc = UnaryOperator::Not; break;
+  case tok::exclaim:      Opc = UnaryOperator::LNot; break;
+  case tok::kw___real:    Opc = UnaryOperator::Real; break;
+  case tok::kw___imag:    Opc = UnaryOperator::Imag; break;
+  case tok::kw_sizeof:    Opc = UnaryOperator::SizeOf; break;
+  case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
   }
 
   if (!FullLocInfo)

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=38949&r1=38948&r2=38949&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
@@ -75,8 +75,9 @@
 };
 
 
-/// UnaryOperator - This represents the unary-expression's (except sizeof), the
-/// postinc/postdec operators from postfix-expression, and various extensions.
+/// UnaryOperator - This represents the unary-expression's (except sizeof of
+/// types), the postinc/postdec operators from postfix-expression, and various
+/// extensions.
 class UnaryOperator : public Expr {
 public:
   enum Opcode {
@@ -85,7 +86,8 @@
     AddrOf, Deref,    // [C99 6.5.3.2] Address and indirection operators.
     Plus, Minus,      // [C99 6.5.3.3] Unary arithmetic operators.
     Not, LNot,        // [C99 6.5.3.3] Unary arithmetic operators.
-    Real, Imag        // "__real expr"/"__imag expr" Extension.
+    Real, Imag,       // "__real expr"/"__imag expr" Extension.
+    SizeOf, AlignOf   // [C99 6.5.3.4] Sizeof (expr, not type) operator.
   };
 
   UnaryOperator(Expr *input, Opcode opc)

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=38949&r1=38948&r2=38949&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
@@ -99,10 +99,7 @@
     return Val;  // Default impl returns operand.
   }
 
-  // Binary/Unary Operators.  'Tok' is the token for the operator.
-  virtual ExprResult ParseUnaryOp(const LexerToken &Tok, ExprTy *Input) {
-    return 0;
-  }
+  // Postfix Expressions.
   virtual ExprResult ParsePostfixUnaryOp(const LexerToken &Tok, ExprTy *Input) {
     return 0;
   }
@@ -127,6 +124,11 @@
                                    SourceLocation RParenLoc) {
     return 0;
   }
+  
+  // Unary Operators.  'Tok' is the token for the operator.
+  virtual ExprResult ParseUnaryOp(const LexerToken &Tok, ExprTy *Input) {
+    return 0;
+  }
 
   virtual ExprResult ParseBinOp(const LexerToken &Tok,
                                 ExprTy *LHS, ExprTy *RHS) {





More information about the cfe-commits mailing list