[cfe-commits] r57152 - in /cfe/trunk: include/clang/AST/Expr.h lib/AST/Expr.cpp lib/AST/ExprConstant.cpp

Chris Lattner sabre at nondot.org
Sun Oct 5 22:28:25 PDT 2008


Author: lattner
Date: Mon Oct  6 00:28:25 2008
New Revision: 57152

URL: http://llvm.org/viewvc/llvm-project?rev=57152&view=rev
Log:
Add a comment that describes tryEvaluate.  Make tryEvaluate fold
__builtin_constant_p properly, and add some scaffolding for
FloatExprEvaluator to eventually handle huge_val and inf.


Modified:
    cfe/trunk/include/clang/AST/Expr.h
    cfe/trunk/lib/AST/Expr.cpp
    cfe/trunk/lib/AST/ExprConstant.cpp

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

==============================================================================
--- cfe/trunk/include/clang/AST/Expr.h (original)
+++ cfe/trunk/include/clang/AST/Expr.h Mon Oct  6 00:28:25 2008
@@ -119,6 +119,10 @@
   /// isConstantExpr - Return true if this expression is a valid constant expr.
   bool isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const;
   
+  /// tryEvaluate - Return true if this is a constant which we can fold using
+  /// any crazy technique (that has nothing to do with language standards) that
+  /// we want to.  If this function returns true, it returns the folded constant
+  /// in Result.
   bool tryEvaluate(APValue& Result, ASTContext &Ctx) const;
 
   /// hasGlobalStorage - Return true if this expression has static storage

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

==============================================================================
--- cfe/trunk/lib/AST/Expr.cpp (original)
+++ cfe/trunk/lib/AST/Expr.cpp Mon Oct  6 00:28:25 2008
@@ -171,7 +171,7 @@
   if (isBuiltinCall() != Builtin::BI__builtin_classify_type)
     return false;
   
-  // The following enum mimics gcc's internal "typeclass.h" file.
+  // The following enum mimics the values returned by GCC.
   enum gcc_type_class {
     no_type_class = -1,
     void_type_class, integer_type_class, char_type_class,

Modified: cfe/trunk/lib/AST/ExprConstant.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprConstant.cpp?rev=57152&r1=57151&r2=57152&view=diff

==============================================================================
--- cfe/trunk/lib/AST/ExprConstant.cpp (original)
+++ cfe/trunk/lib/AST/ExprConstant.cpp Mon Oct  6 00:28:25 2008
@@ -245,14 +245,25 @@
   return Error(E->getLocStart(), diag::err_expr_not_constant);
 }
 
-
 bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
   Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
-  // __builtin_type_compatible_p is a constant.
-  if (E->isBuiltinClassifyType(Result))
-    return true;
   
-  return Error(E->getLocStart(), diag::err_expr_not_constant);
+  switch (E->isBuiltinCall()) {
+  default:
+    return Error(E->getLocStart(), diag::err_expr_not_constant);
+  case Builtin::BI__builtin_classify_type:
+    // __builtin_type_compatible_p is a constant.  Return its value.
+    E->isBuiltinClassifyType(Result);
+    return true;
+    
+  case Builtin::BI__builtin_constant_p: {
+    // __builtin_constant_p always has one operand: it returns true if that
+    // operand can be folded, false otherwise.
+    APValue Res;
+    Result = E->getArg(0)->tryEvaluate(Res, Info.Ctx);
+    return true;
+  }
+  }
 }
 
 bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
@@ -520,6 +531,7 @@
   }
 
   bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
+  bool VisitCallExpr(const CallExpr *E);
 
   bool VisitBinaryOperator(const BinaryOperator *E);
   bool VisitFloatingLiteral(const FloatingLiteral *E);
@@ -530,6 +542,22 @@
   return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
 }
 
+bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
+  //Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
+  
+  switch (E->isBuiltinCall()) {
+  case Builtin::BI__builtin_huge_val:
+  case Builtin::BI__builtin_huge_valf:
+  case Builtin::BI__builtin_huge_vall:
+  case Builtin::BI__builtin_inf:
+  case Builtin::BI__builtin_inff:
+  case Builtin::BI__builtin_infl:
+    // FIXME: Implement me.
+  default: return false;
+  }
+}
+
+
 bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
   // FIXME: Diagnostics?  I really don't understand how the warnings
   // and errors are supposed to work.
@@ -568,6 +596,10 @@
 // Top level TryEvaluate.
 //===----------------------------------------------------------------------===//
 
+/// tryEvaluate - Return true if this is a constant which we can fold using
+/// any crazy technique (that has nothing to do with language standards) that
+/// we want to.  If this function returns true, it returns the folded constant
+/// in Result.
 bool Expr::tryEvaluate(APValue &Result, ASTContext &Ctx) const {
   EvalInfo Info(Ctx);
   if (getType()->isIntegerType()) {





More information about the cfe-commits mailing list