[cfe-commits] r69326 - in /cfe/trunk/lib/Sema: Sema.h SemaDecl.cpp SemaDeclCXX.cpp

Anders Carlsson andersca at mac.com
Thu Apr 16 16:50:50 PDT 2009


Author: andersca
Date: Thu Apr 16 18:50:50 2009
New Revision: 69326

URL: http://llvm.org/viewvc/llvm-project?rev=69326&view=rev
Log:
If a class has a non-trivial constructor that doesn't take any arguments, we will now make an implicit CXXTemporaryObjectExpr. So 

struct S {
  S();
};

void f() {
 S s;
}

's' here will implicitly be declared as.

S s = S();


Modified:
    cfe/trunk/lib/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/lib/Sema/SemaDeclCXX.cpp

Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=69326&r1=69325&r2=69326&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Thu Apr 16 18:50:50 2009
@@ -1418,6 +1418,13 @@
                                              SourceLocation *CommaLocs,
                                              SourceLocation RParenLoc);
 
+  /// InitializeVarWithConstructor - Creates an implicit 
+  /// CXXTemporaryObjectExpr and sets it as the passed in VarDecl initializer.
+  void InitializeVarWithConstructor(VarDecl *VD, 
+                                    CXXConstructorDecl *Constructor,
+                                    QualType DeclInitType, 
+                                    Expr **Exprs, unsigned NumExprs);
+  
   /// InitializationKind - Represents which kind of C++ initialization
   /// [dcl.init] a routine is to perform.
   enum InitializationKind {

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=69326&r1=69325&r2=69326&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Thu Apr 16 18:50:50 2009
@@ -2552,7 +2552,9 @@
       if (const ArrayType *Array = Context.getAsArrayType(Type))
         InitType = Array->getElementType();
       if (!Var->hasExternalStorage() && InitType->isRecordType()) {
-        const CXXConstructorDecl *Constructor = 0;
+        CXXRecordDecl *RD = 
+          cast<CXXRecordDecl>(InitType->getAsRecordType()->getDecl());
+        CXXConstructorDecl *Constructor = 0;
         if (!RequireCompleteType(Var->getLocation(), InitType, 
                                     diag::err_invalid_incomplete_type_use))
           Constructor
@@ -2564,6 +2566,8 @@
                                                  IK_Default);
         if (!Constructor)
           Var->setInvalidDecl();
+        else if (!RD->hasTrivialConstructor()) 
+          InitializeVarWithConstructor(Var, Constructor, InitType, 0, 0);
       }
     }
 

Modified: cfe/trunk/lib/Sema/SemaDeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclCXX.cpp?rev=69326&r1=69325&r2=69326&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclCXX.cpp Thu Apr 16 18:50:50 2009
@@ -1764,6 +1764,18 @@
   return DeclPtrTy::make(AliasDecl);
 }
 
+void Sema::InitializeVarWithConstructor(VarDecl *VD, 
+                                        CXXConstructorDecl *Constructor,
+                                        QualType DeclInitType, 
+                                        Expr **Exprs, unsigned NumExprs) {
+  Expr *Temp = 
+    new (Context) CXXTemporaryObjectExpr(Constructor, DeclInitType,
+                                         SourceLocation(), 
+                                         Exprs, NumExprs,
+                                         SourceLocation());
+  VD->setInit(Temp);
+}
+
 /// AddCXXDirectInitializerToDecl - This action is called immediately after 
 /// ActOnDeclarator, when a C++ direct initializer is present.
 /// e.g: "int x(1);"
@@ -1827,17 +1839,9 @@
     if (!Constructor)
       RealDecl->setInvalidDecl();
     else {
-      // Let clients know that initialization was done with a direct
-      // initializer.
       VDecl->setCXXDirectInitializer(true);
-
-      Expr *Temp = 
-        new (Context) CXXTemporaryObjectExpr(Constructor, DeclInitType,
-                                             SourceLocation(), 
-                                             (Expr**)Exprs.release(), 
-                                             NumExprs,
-                                             SourceLocation());
-      VDecl->setInit(Temp);
+      InitializeVarWithConstructor(VDecl, Constructor, DeclInitType, 
+                                   (Expr**)Exprs.release(), NumExprs);
     }
     return;
   }





More information about the cfe-commits mailing list