]> granicus.if.org Git - clang/commitdiff
Implement basic code generation of constructor calls. We can now compile:
authorAnders Carlsson <andersca@mac.com>
Fri, 17 Apr 2009 00:06:03 +0000 (00:06 +0000)
committerAnders Carlsson <andersca@mac.com>
Fri, 17 Apr 2009 00:06:03 +0000 (00:06 +0000)
struct S {
S(int, int);
};

void f() {
S s(10, 10);
}

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

lib/CodeGen/CGCXX.cpp
lib/CodeGen/CGExpr.cpp
lib/CodeGen/CGExprAgg.cpp
lib/CodeGen/CodeGenFunction.h

index f76bd122a056bf4176c0a68afb42bc06ed6abe3d..e61cef349829cce41ff01ed01a8f8c7c09fa0dd3 100644 (file)
@@ -145,6 +145,51 @@ void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D,
   SetLLVMFunctionAttributesForDefinition(D, Fn);
 }
 
+void
+CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D, 
+                                        CXXCtorType Type, 
+                                        llvm::Value *This,
+                                        CallExpr::const_arg_iterator ArgBeg,
+                                        CallExpr::const_arg_iterator ArgEnd) {
+  CallArgList Args;
+  
+  // Push the 'this' pointer.
+  Args.push_back(std::make_pair(RValue::get(This), 
+                                D->getThisType(getContext())));
+  
+  EmitCallArgs(Args, D->getType()->getAsFunctionProtoType(), ArgBeg, ArgEnd);
+  
+  EmitCall(CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args), 
+           CGM.GetAddrOfCXXConstructor(D, Type), Args, D);
+}
+
+LValue 
+CodeGenFunction::EmitCXXTemporaryObjectExprLValue(
+                                              const CXXTemporaryObjectExpr *E) {
+  // Allocate the destination.
+  llvm::Value *Dest = CreateTempAlloca(ConvertType(E->getType()), "tmp");
+  
+  EmitCXXTemporaryObjectExpr(Dest, E);
+  
+  return LValue::MakeAddr(Dest, E->getType().getCVRQualifiers(),
+                          getContext().getObjCGCAttrKind(E->getType()));
+}
+
+void 
+CodeGenFunction::EmitCXXTemporaryObjectExpr(llvm::Value *Dest, 
+                                            const CXXTemporaryObjectExpr *E) {
+  assert(Dest && "Must have a destination!");
+  
+  const CXXRecordDecl *RD = 
+  cast<CXXRecordDecl>(E->getType()->getAsRecordType()->getDecl());
+  if (RD->hasTrivialConstructor())
+    return;
+  
+  // Call the constructor.
+  EmitCXXConstructorCall(E->getConstructor(), Ctor_Complete, Dest, 
+                         E->arg_begin(), E->arg_end());
+}
+
 static bool canGenerateCXXConstructor(const CXXConstructorDecl *D, 
                                       ASTContext &Context) {
   const CXXRecordDecl *RD = D->getParent();
index bf862e72c3d0a852114ca60cbc2b32930c302780..f7f3a5ea72f8eb4197766afe32c02d13d482a43b 100644 (file)
@@ -191,6 +191,8 @@ LValue CodeGenFunction::EmitLValue(const Expr *E) {
   case Expr::CXXReinterpretCastExprClass:
   case Expr::CXXConstCastExprClass:
     return EmitCastLValue(cast<CastExpr>(E));
+  case Expr::CXXTemporaryObjectExprClass:
+    return EmitCXXTemporaryObjectExprLValue(cast<CXXTemporaryObjectExpr>(E));
   }
 }
 
index acca396c0b077390fbd20e2ea4c2e753bf6e8a54..9d6df5c820d5330285afec32f3b03dfbacd7c0a4 100644 (file)
@@ -14,6 +14,7 @@
 #include "CodeGenFunction.h"
 #include "CodeGenModule.h"
 #include "clang/AST/ASTContext.h"
+#include "clang/AST/DeclCXX.h"
 #include "clang/AST/StmtVisitor.h"
 #include "llvm/Constants.h"
 #include "llvm/Function.h"
@@ -96,6 +97,7 @@ public:
   void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
     Visit(DAE->getExpr());
   }
+  void VisitCXXTemporaryObjectExpr(const CXXTemporaryObjectExpr *E);
   void VisitVAArgExpr(VAArgExpr *E);
 
   void EmitInitializationToLValue(Expr *E, LValue Address);
@@ -290,6 +292,18 @@ void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
     CGF.EmitAggregateCopy(DestPtr, ArgPtr, VE->getType());
 }
 
+void
+AggExprEmitter::VisitCXXTemporaryObjectExpr(const CXXTemporaryObjectExpr *E) {
+  llvm::Value *This = 0;
+  
+  if (DestPtr)
+    This = DestPtr;
+  else 
+    This = CGF.CreateTempAlloca(CGF.ConvertType(E->getType()), "tmp");
+  
+  CGF.EmitCXXTemporaryObjectExpr(This, E);
+}
+
 void AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) {
   // FIXME: Are initializers affected by volatile?
   if (isa<ImplicitValueInitExpr>(E)) {
index e96b815dd5e33c0ff0726e32336d4871f7c3138c..d511da3f7ff363d91af825c88006023c1a669955 100644 (file)
@@ -25,6 +25,7 @@
 #include "CGBlocks.h"
 #include "CGBuilder.h"
 #include "CGCall.h"
+#include "CGCXX.h"
 #include "CGValue.h"
 
 namespace llvm {
@@ -458,6 +459,11 @@ public:
   /// generating code for an C++ member function.
   llvm::Value *LoadCXXThis();
   
+  void EmitCXXConstructorCall(const CXXConstructorDecl *D, CXXCtorType Type, 
+                              llvm::Value *This,
+                              CallExpr::const_arg_iterator ArgBeg,
+                              CallExpr::const_arg_iterator ArgEnd);
+
   //===--------------------------------------------------------------------===//
   //                            Declaration Emission
   //===--------------------------------------------------------------------===//
@@ -632,6 +638,7 @@ public:
   LValue EmitBlockDeclRefLValue(const BlockDeclRefExpr *E);
 
   LValue EmitCXXConditionDeclLValue(const CXXConditionDeclExpr *E);
+  LValue EmitCXXTemporaryObjectExprLValue(const CXXTemporaryObjectExpr *E);
 
   LValue EmitObjCMessageExprLValue(const ObjCMessageExpr *E);
   LValue EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E);
@@ -743,6 +750,9 @@ public:
   void GenerateStaticCXXBlockVarDeclInit(const VarDecl &D,
                                          llvm::GlobalVariable *GV);
 
+  void EmitCXXTemporaryObjectExpr(llvm::Value *Dest, 
+                                  const CXXTemporaryObjectExpr *E);
+  
   //===--------------------------------------------------------------------===//
   //                             Internal Helpers
   //===--------------------------------------------------------------------===//