aboutsummaryrefslogtreecommitdiff
path: root/lib/CodeGen
diff options
context:
space:
mode:
authorAnders Carlsson <andersca@mac.com>2009-04-11 01:08:03 +0000
committerAnders Carlsson <andersca@mac.com>2009-04-11 01:08:03 +0000
commit1e5dc6e12ccda4e783c67fa9ae11be419d7b0573 (patch)
treeed4620629ecc81df9d44ac987ab37bb23ab88a14 /lib/CodeGen
parentb3bf76fb2442093ad871f1adeda608e881b9dee6 (diff)
Add support for generating reference initialization code.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@68852 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/CodeGen')
-rw-r--r--lib/CodeGen/CGExprConstant.cpp23
1 files changed, 21 insertions, 2 deletions
diff --git a/lib/CodeGen/CGExprConstant.cpp b/lib/CodeGen/CGExprConstant.cpp
index a3fca20f4f..2373ad64e9 100644
--- a/lib/CodeGen/CGExprConstant.cpp
+++ b/lib/CodeGen/CGExprConstant.cpp
@@ -474,7 +474,26 @@ llvm::Constant *CodeGenModule::EmitConstantExpr(const Expr *E,
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 @@ llvm::Constant *CodeGenModule::EmitConstantExpr(const Expr *E,
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());