[cfe-commits] r68852 - in /cfe/trunk: lib/CodeGen/CGExprConstant.cpp test/CodeGenCXX/const-init.cpp

Anders Carlsson andersca at mac.com
Fri Apr 10 18:08:03 PDT 2009


Author: andersca
Date: Fri Apr 10 20:08:03 2009
New Revision: 68852

URL: http://llvm.org/viewvc/llvm-project?rev=68852&view=rev
Log:
Add support for generating reference initialization code.

Added:
    cfe/trunk/test/CodeGenCXX/const-init.cpp
Modified:
    cfe/trunk/lib/CodeGen/CGExprConstant.cpp

Modified: cfe/trunk/lib/CodeGen/CGExprConstant.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprConstant.cpp?rev=68852&r1=68851&r2=68852&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprConstant.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprConstant.cpp Fri Apr 10 20:08:03 2009
@@ -474,7 +474,26 @@
                                                 CodeGenFunction *CGF) {
   Expr::EvalResult Result;
   
-  if (E->Evaluate(Result, Context)) {
+  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 
+    Success = E->Evaluate(Result, Context);
+  
+  if (Success) {
     assert(!Result.HasSideEffects && 
            "Constant expr should not have any side effects!");
     switch (Result.Val.getKind()) {
@@ -482,7 +501,7 @@
       assert(0 && "Constant expressions should be initialized.");
       return 0;
     case APValue::LValue: {
-      const llvm::Type *DestTy = getTypes().ConvertTypeForMem(E->getType());
+      const llvm::Type *DestTy = getTypes().ConvertTypeForMem(DestType);
       llvm::Constant *Offset = 
         llvm::ConstantInt::get(llvm::Type::Int64Ty, 
                                Result.Val.getLValueOffset());

Added: cfe/trunk/test/CodeGenCXX/const-init.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/const-init.cpp?rev=68852&view=auto

==============================================================================
--- cfe/trunk/test/CodeGenCXX/const-init.cpp (added)
+++ cfe/trunk/test/CodeGenCXX/const-init.cpp Fri Apr 10 20:08:03 2009
@@ -0,0 +1,11 @@
+// RUN: clang-cc -verify -emit-llvm -o %t %s
+
+int a = 10;
+int &ar = a;
+
+void f();
+void (&fr)() = f;
+
+struct S { int& a; };
+S s = { a };
+





More information about the cfe-commits mailing list