[cfe-commits] r40794 - in /cfe/trunk: AST/StmtPrinter.cpp Parse/ParseExpr.cpp Sema/Sema.h Sema/SemaExpr.cpp include/clang/AST/Expr.h include/clang/AST/StmtNodes.def include/clang/Basic/DiagnosticKinds.def include/clang/Parse/Action.h test/Parser/builtin_types_compatible.c

Steve Naroff snaroff at apple.com
Fri Aug 3 14:21:27 PDT 2007


Author: snaroff
Date: Fri Aug  3 16:21:27 2007
New Revision: 40794

URL: http://llvm.org/viewvc/llvm-project?rev=40794&view=rev
Log:

Implement __builtin_choose_expr.


Modified:
    cfe/trunk/AST/StmtPrinter.cpp
    cfe/trunk/Parse/ParseExpr.cpp
    cfe/trunk/Sema/Sema.h
    cfe/trunk/Sema/SemaExpr.cpp
    cfe/trunk/include/clang/AST/Expr.h
    cfe/trunk/include/clang/AST/StmtNodes.def
    cfe/trunk/include/clang/Basic/DiagnosticKinds.def
    cfe/trunk/include/clang/Parse/Action.h
    cfe/trunk/test/Parser/builtin_types_compatible.c

Modified: cfe/trunk/AST/StmtPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/AST/StmtPrinter.cpp?rev=40794&r1=40793&r2=40794&view=diff

==============================================================================
--- cfe/trunk/AST/StmtPrinter.cpp (original)
+++ cfe/trunk/AST/StmtPrinter.cpp Fri Aug  3 16:21:27 2007
@@ -487,6 +487,15 @@
   OS << Node->getArgType2().getAsString() << ")";
 }
 
+void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
+  OS << "__builtin_choose_expr(";
+  PrintExpr(Node->getCond());
+  OS << ",";
+  PrintExpr(Node->getLHS());
+  OS << ",";
+  PrintExpr(Node->getRHS());
+  OS << ")";
+}
 
 // C++
 

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

==============================================================================
--- cfe/trunk/Parse/ParseExpr.cpp (original)
+++ cfe/trunk/Parse/ParseExpr.cpp Fri Aug  3 16:21:27 2007
@@ -805,19 +805,35 @@
       }
     }
     break;
-  case tok::kw___builtin_choose_expr:
-    Res = ParseAssignmentExpression();
-    
+  case tok::kw___builtin_choose_expr: {
+    ExprResult Cond = ParseAssignmentExpression();
+    if (Cond.isInvalid) {
+      SkipUntil(tok::r_paren);
+      return Cond;
+    }
     if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
       return ExprResult(true);
     
-    Res = ParseAssignmentExpression();
-    
+    ExprResult Expr1 = ParseAssignmentExpression();
+    if (Expr1.isInvalid) {
+      SkipUntil(tok::r_paren);
+      return Expr1;
+    }    
     if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
       return ExprResult(true);
     
-    Res = ParseAssignmentExpression();
-    break;
+    ExprResult Expr2 = ParseAssignmentExpression();
+    if (Expr2.isInvalid) {
+      SkipUntil(tok::r_paren);
+      return Expr2;
+    }    
+    if (Tok.getKind() != tok::r_paren) {
+      Diag(Tok, diag::err_expected_rparen);
+      return ExprResult(true);
+    }
+    return Actions.ParseChooseExpr(StartLoc, Cond.Val, Expr1.Val, Expr2.Val,
+                                   ConsumeParen());
+  }
   case tok::kw___builtin_types_compatible_p:
     TypeTy *Ty1 = ParseTypeName();
     

Modified: cfe/trunk/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/Sema.h?rev=40794&r1=40793&r2=40794&view=diff

==============================================================================
--- cfe/trunk/Sema/Sema.h (original)
+++ cfe/trunk/Sema/Sema.h Fri Aug  3 16:21:27 2007
@@ -285,6 +285,11 @@
   virtual ExprResult ParseTypesCompatibleExpr(SourceLocation BuiltinLoc, 
                                               TypeTy *arg1, TypeTy *arg2,
                                               SourceLocation RPLoc);
+                                              
+  // __builtin_choose_expr(constExpr, expr1, expr2)
+  virtual ExprResult ParseChooseExpr(SourceLocation BuiltinLoc, 
+                                     ExprTy *cond, ExprTy *expr1, ExprTy *expr2,
+                                     SourceLocation RPLoc);
   
   /// ParseCXXCasts - Parse {dynamic,static,reinterpret,const}_cast's.
   virtual ExprResult ParseCXXCasts(SourceLocation OpLoc, tok::TokenKind Kind,

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

==============================================================================
--- cfe/trunk/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/Sema/SemaExpr.cpp Fri Aug  3 16:21:27 2007
@@ -1584,3 +1584,25 @@
   return new TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, argT2, RPLoc);
 }
 
+Sema::ExprResult Sema::ParseChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond, 
+                                       ExprTy *expr1, ExprTy *expr2,
+                                       SourceLocation RPLoc) {
+  Expr *CondExpr = static_cast<Expr*>(cond);
+  Expr *LHSExpr = static_cast<Expr*>(expr1);
+  Expr *RHSExpr = static_cast<Expr*>(expr2);
+  
+  assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)");
+
+  // The conditional expression is required to be a constant expression.
+  llvm::APSInt condEval(32);
+  SourceLocation ExpLoc;
+  if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc))
+    return Diag(ExpLoc, diag::err_typecheck_choose_expr_requires_constant,
+                 CondExpr->getSourceRange());
+
+  // If the condition is > zero, then the AST type is the same as the LSHExpr.
+  QualType resType = condEval.getZExtValue() ? LHSExpr->getType() : 
+                                               RHSExpr->getType();
+  return new ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, resType, RPLoc);
+}
+

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

==============================================================================
--- cfe/trunk/include/clang/AST/Expr.h (original)
+++ cfe/trunk/include/clang/AST/Expr.h Fri Aug  3 16:21:27 2007
@@ -776,6 +776,35 @@
   static bool classof(const TypesCompatibleExpr *) { return true; }
 };
 
+/// ChooseExpr - GNU builtin-in function __builtin_choose_expr.
+/// This AST node is similar to the conditional operator (?:) in C, with 
+/// the following exceptions:
+/// - the test expression much be a constant expression.
+/// - the expression returned has it's type unaltered by promotion rules.
+/// - does not evaluate the expression that was not chosen.
+class ChooseExpr : public Expr {
+  Expr *Cond, *LHS, *RHS;  // First, second, and third arguments.
+  SourceLocation BuiltinLoc, RParenLoc;
+public:
+  ChooseExpr(SourceLocation BLoc, Expr *cond, Expr *lhs, Expr *rhs, QualType t,
+             SourceLocation RP)
+    : Expr(ChooseExprClass, t),  
+      Cond(cond), LHS(lhs), RHS(rhs), BuiltinLoc(BLoc), RParenLoc(RP) {}
+    
+  Expr *getCond() const { return Cond; }
+  Expr *getLHS() const { return LHS; }
+  Expr *getRHS() const { return RHS; }
+    
+  virtual SourceRange getSourceRange() const {
+    return SourceRange(BuiltinLoc, RParenLoc);
+  }
+  virtual void visit(StmtVisitor &Visitor);
+  static bool classof(const Stmt *T) {
+    return T->getStmtClass() == ChooseExprClass; 
+  }
+  static bool classof(const ChooseExpr *) { return true; }
+};
+
 }  // end namespace clang
 
 #endif

Modified: cfe/trunk/include/clang/AST/StmtNodes.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/StmtNodes.def?rev=40794&r1=40793&r2=40794&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/StmtNodes.def (original)
+++ cfe/trunk/include/clang/AST/StmtNodes.def Fri Aug  3 16:21:27 2007
@@ -68,11 +68,12 @@
 STMT(50, AddrLabelExpr        , Expr)
 STMT(51, StmtExpr             , Expr)
 STMT(52, TypesCompatibleExpr  , Expr)
+STMT(53, ChooseExpr           , Expr)
 
 // C++ Expressions.
-STMT(53, CXXCastExpr          , Expr)
-STMT(54, CXXBoolLiteralExpr   , Expr)
-LAST_EXPR(54)
+STMT(54, CXXCastExpr          , Expr)
+STMT(55, CXXBoolLiteralExpr   , Expr)
+LAST_EXPR(55)
 
 #undef STMT
 #undef FIRST_STMT

Modified: cfe/trunk/include/clang/Basic/DiagnosticKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticKinds.def?rev=40794&r1=40793&r2=40794&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticKinds.def Fri Aug  3 16:21:27 2007
@@ -652,7 +652,8 @@
      "incompatible operand types ('%0' and '%1')")
 DIAG(ext_typecheck_cond_incompatible_pointers, WARNING,
      "pointer type mismatch ('%0' and '%1')")
-
+DIAG(err_typecheck_choose_expr_requires_constant, ERROR,
+     "'__builtin_choose_expr' requires a constant expression")
 DIAG(warn_unused_expr, WARNING,
      "expression result unused")
 

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

==============================================================================
--- cfe/trunk/include/clang/Parse/Action.h (original)
+++ cfe/trunk/include/clang/Parse/Action.h Fri Aug  3 16:21:27 2007
@@ -381,6 +381,12 @@
                                               SourceLocation RPLoc) {
     return 0;
   }
+  // __builtin_choose_expr(constExpr, expr1, expr2)
+  virtual ExprResult ParseChooseExpr(SourceLocation BuiltinLoc, 
+                                     ExprTy *cond, ExprTy *expr1, ExprTy *expr2,
+                                     SourceLocation RPLoc) {
+    return 0;
+  }
 
   //===------------------------- C++ Expressions --------------------------===//
   

Modified: cfe/trunk/test/Parser/builtin_types_compatible.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Parser/builtin_types_compatible.c?rev=40794&r1=40793&r2=40794&view=diff

==============================================================================
--- cfe/trunk/test/Parser/builtin_types_compatible.c (original)
+++ cfe/trunk/test/Parser/builtin_types_compatible.c Fri Aug  3 16:21:27 2007
@@ -31,5 +31,14 @@
   func_choose(a);
   func_choose(b);
   func_choose(d);
+
+  int c; 
+  struct xx { int a; } x, y;
+  
+  c = __builtin_choose_expr(a+3-7, b, x); // expected-error{{'__builtin_choose_expr' requires a constant expression}}
+  c = __builtin_choose_expr(0, b, x); // expected-error{{incompatible types assigning 'struct xx' to 'int'}}
+  c = __builtin_choose_expr(5+3-7, b, x);
+  y = __builtin_choose_expr(4+3-7, b, x);
+
 }
 





More information about the cfe-commits mailing list