From: Eli Friedman Date: Wed, 27 May 2009 05:39:06 +0000 (+0000) Subject: Add IRGen support for local variables of reference type. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4a18784dea763be146df68546e6dbf4233c33077;p=clang Add IRGen support for local variables of reference type. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@72462 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/CodeGen/CGDecl.cpp b/lib/CodeGen/CGDecl.cpp index 52bb90e7f8..bcad77be51 100644 --- a/lib/CodeGen/CGDecl.cpp +++ b/lib/CodeGen/CGDecl.cpp @@ -332,11 +332,6 @@ void CodeGenFunction::EmitLocalBlockVarDecl(const VarDecl &D) { DI->EmitDeclareOfAutoVariable(&D, DeclPtr, Builder); } - if (D.getType()->isReferenceType()) { - CGM.ErrorUnsupported(&D, "declaration with reference type"); - return; - } - // If this local has an initializer, emit it now. if (const Expr *Init = D.getInit()) { llvm::Value *Loc = DeclPtr; @@ -344,7 +339,10 @@ void CodeGenFunction::EmitLocalBlockVarDecl(const VarDecl &D) { bool needsCopyDispose = BlockRequiresCopying(Ty); Loc = Builder.CreateStructGEP(DeclPtr, needsCopyDispose*2+4, "x"); } - if (!hasAggregateLLVMType(Init->getType())) { + if (Ty->isReferenceType()) { + llvm::Value *V = EmitReferenceBindingToExpr(Init, Ty).getScalarVal(); + EmitStoreOfScalar(V, Loc, false, Ty); + } else if (!hasAggregateLLVMType(Init->getType())) { llvm::Value *V = EmitScalarExpr(Init); EmitStoreOfScalar(V, Loc, D.getType().isVolatileQualified(), D.getType()); diff --git a/test/CodeGenCXX/references.cpp b/test/CodeGenCXX/references.cpp index 5c5518b38b..5d4ac06c5b 100644 --- a/test/CodeGenCXX/references.cpp +++ b/test/CodeGenCXX/references.cpp @@ -78,3 +78,9 @@ void test_aggregate() { int& reference_return() { return g; } + +int reference_decl() { + int& a = g; + const int& b = 1; + return a+b; +}