]> granicus.if.org Git - clang/commitdiff
Fix up constant expression handling to deal with the address
authorEli Friedman <eli.friedman@gmail.com>
Wed, 27 May 2009 06:04:58 +0000 (06:04 +0000)
committerEli Friedman <eli.friedman@gmail.com>
Wed, 27 May 2009 06:04:58 +0000 (06:04 +0000)
of a reference correctly.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@72463 91177308-0d34-0410-b5e6-96231b3b80d8

lib/AST/ExprConstant.cpp
lib/CodeGen/CGExprConstant.cpp
test/CodeGenCXX/references.cpp

index 7651884aa60974395ff04efcd2f247a499d07f2b..d0d8b81bab064073a541f1a0c4584e8aa9a8162b 100644 (file)
@@ -178,11 +178,20 @@ static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) {
 }
 
 APValue LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E)
-{ 
+{
   if (!E->hasGlobalStorage())
     return APValue();
-  
-  return APValue(E, 0); 
+
+  if (isa<FunctionDecl>(E->getDecl())) {
+    return APValue(E, 0);
+  } else if (VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) {
+    if (!VD->getType()->isReferenceType())
+      return APValue(E, 0);
+    if (VD->getInit())
+      return Visit(VD->getInit());
+  }
+
+  return APValue();
 }
 
 APValue LValueExprEvaluator::VisitBlockExpr(BlockExpr *E)
index 0df03ec3a270be8026e0e059575604f1adbf896d..b30bafb51051c34504158852beea4c501c20e08a 100644 (file)
@@ -478,21 +478,9 @@ llvm::Constant *CodeGenModule::EmitConstantExpr(const Expr *E,
   
   bool Success = false;
   
-  if (DestType->isReferenceType()) {
-    // If the destination type is a reference type, we need to evaluate it
-    // as an lvalue.
-    if (E->EvaluateAsLValue(Result, Context)) {
-      if (const Expr *LVBase = Result.Val.getLValueBase()) {
-        if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LVBase)) {
-          const ValueDecl *VD = cast<ValueDecl>(DRE->getDecl());
-
-          // We can only initialize a reference with an lvalue if the lvalue
-          // is not a reference itself.
-          Success = !VD->getType()->isReferenceType();
-        }
-      }
-    }
-  } else 
+  if (DestType->isReferenceType())
+    Success = E->EvaluateAsLValue(Result, Context);
+  else 
     Success = E->Evaluate(Result, Context);
   
   if (Success) {
index 5d4ac06c5bb64b1c1a0e7d6e3ebabc0be5299df1..9d5a2ed9eef8a36c5c0189846b32677a8ec3d431 100644 (file)
@@ -11,6 +11,7 @@ void t2(int& a) {
 
 int g;
 int& gr = g;
+int& grr = gr;
 void t3() {
   int b = gr;
 }