diff options
author | Anders Carlsson <andersca@mac.com> | 2009-04-11 01:08:03 +0000 |
---|---|---|
committer | Anders Carlsson <andersca@mac.com> | 2009-04-11 01:08:03 +0000 |
commit | d8e39bbb847b35d8912db2d13096d588651a72f1 (patch) | |
tree | 745cb87b2a4b449dee9f83540beb9137a3cc8876 /clang/lib/CodeGen/CGExprConstant.cpp | |
parent | 4531be138c01f3481f1f7f0f541fb3e221c8542f (diff) | |
download | bcm5719-llvm-d8e39bbb847b35d8912db2d13096d588651a72f1.tar.gz bcm5719-llvm-d8e39bbb847b35d8912db2d13096d588651a72f1.zip |
Add support for generating reference initialization code.
llvm-svn: 68852
Diffstat (limited to 'clang/lib/CodeGen/CGExprConstant.cpp')
-rw-r--r-- | clang/lib/CodeGen/CGExprConstant.cpp | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/clang/lib/CodeGen/CGExprConstant.cpp b/clang/lib/CodeGen/CGExprConstant.cpp index a3fca20f4fb..2373ad64e92 100644 --- a/clang/lib/CodeGen/CGExprConstant.cpp +++ b/clang/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()); |