]> granicus.if.org Git - clang/commitdiff
path to ir-gen 12.3.1 Conversion by constructor
authorFariborz Jahanian <fjahanian@apple.com>
Fri, 28 Aug 2009 22:04:50 +0000 (22:04 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Fri, 28 Aug 2009 22:04:50 +0000 (22:04 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@80398 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/AST/Expr.h
lib/AST/StmtDumper.cpp
lib/Sema/SemaExprCXX.cpp
test/CodeGenCXX/constructor-conversion.cpp [new file with mode: 0644]

index 827877e79807d9e981974f4c7b4efa2682992b0d..64ab17982b3843ebb5730c43160c25e10c40ed41 100644 (file)
@@ -1203,7 +1203,10 @@ public:
 
     /// CK_UserDefinedConversion - Conversion using a user defined type 
     /// conversion function.
-    CK_UserDefinedConversion
+    CK_UserDefinedConversion,
+
+    /// CK_ConstructorConversion - Conversion by constructor
+    CK_ConstructorConversion
   };
   
   struct CastInfo {
index 5f680ea5429c79d88a8275ba01fd7c401e4301c6..720188377d5d01bb4e037068294387c6810af795 100644 (file)
@@ -333,6 +333,9 @@ void StmtDumper::VisitCastExpr(CastExpr *Node) {
   case CastExpr::CK_UserDefinedConversion:
     fprintf(F, "<UserDefinedConversion>");
     break;
+  case CastExpr::CK_ConstructorConversion:
+    fprintf(F, "<ConstructorConversion>");
+    break;
   }
 }
 
index 61891e788339b6d9889b40aa2a57584794f011d9..fe67ec6faced53f169bf07a0406a7afb575547b4 100644 (file)
@@ -920,9 +920,10 @@ Sema::PerformImplicitConversion(Expr *&From, QualType ToType,
     break;
 
   case ImplicitConversionSequence::UserDefinedConversion:
-      // FIXME. Support other kinds of user defined convesions.
-      if (CXXConversionDecl *CV = 
-            dyn_cast<CXXConversionDecl>(ICS.UserDefined.ConversionFunction))
+    {
+      FunctionDecl *FD = ICS.UserDefined.ConversionFunction;
+      CastExpr::CastKind CastKind = CastExpr::CK_Unknown;
+      if (CXXConversionDecl *CV = dyn_cast<CXXConversionDecl>(FD)) {
         // FIXME. Get actual Source Location.
         From = 
           new (Context) CXXFunctionalCastExpr(ToType.getNonReferenceType(),
@@ -930,10 +931,22 @@ Sema::PerformImplicitConversion(Expr *&From, QualType ToType,
                                             CastExpr::CK_UserDefinedConversion,
                                             From, CV,
                                             SourceLocation());
-    ImpCastExprToType(From, ToType.getNonReferenceType(), 
-                      CastExpr::CK_Unknown,
-                      ToType->isLValueReferenceType());
-    return false;
+        CastKind = CastExpr::CK_UserDefinedConversion;
+      }
+      else if (CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) {
+        // FIXME. Do we need to check for isLValueReferenceType?
+        DefaultFunctionArrayConversion(From);
+        OwningExprResult InitResult = 
+          BuildCXXConstructExpr(ToType.getNonReferenceType(), 
+                                CD, &From, 1);
+        From = InitResult.takeAs<Expr>();
+        CastKind = CastExpr::CK_ConstructorConversion ;
+      }
+      ImpCastExprToType(From, ToType.getNonReferenceType(),
+                        CastKind,
+                        ToType->isLValueReferenceType());
+      return false;
+    }
 
   case ImplicitConversionSequence::EllipsisConversion:
     assert(false && "Cannot perform an ellipsis conversion");
diff --git a/test/CodeGenCXX/constructor-conversion.cpp b/test/CodeGenCXX/constructor-conversion.cpp
new file mode 100644 (file)
index 0000000..3fafe61
--- /dev/null
@@ -0,0 +1,58 @@
+// RUN: clang-cc -triple x86_64-apple-darwin -std=c++0x -S %s -o %t-64.s &&
+// RUN: FileCheck -check-prefix LP64 --input-file=%t-64.s %s &&
+// RUN: clang-cc -triple i386-apple-darwin -std=c++0x -S %s -o %t-32.s &&
+// RUN: FileCheck -check-prefix LP32 --input-file=%t-32.s %s &&
+// RUN: true
+
+extern "C" int printf(...);
+
+class X { // ...
+public: 
+       X(int) : iX(2), fX(2.3) , name("HELLO\n") {  }
+
+       X(const char* arg, int ix=0) { iX = ix; fX = 6.0; name = arg+ix; }
+       X(): iX(100), fX(1.2) {}
+       int iX;
+       float fX;
+       const char *name;
+       void pr(void) {
+         printf("iX = %d  fX = %f name = %s\n", iX, fX, name);
+       }
+};
+
+void g(X arg) {
+  arg.pr();
+}
+
+void f(X arg) {
+
+  X a = 1;     // a = X(1)
+
+  a.pr();
+
+  X b = "Jessie"; //  b=X("Jessie",0)
+
+  b.pr();
+
+
+  a = 2;         // a = X(2)
+
+  a.pr();
+
+}
+
+
+int main()
+{
+       X x;
+       f(x);
+       g(3); // g(X(3))
+}
+
+// CHECK-LP64: call     __ZN1XC1Ei
+// CHECK-LP64: call     __ZN1XC1EPKci
+// CHECK-LP64: call     __ZN1XC1Ev
+
+// CHECK-LP32: call     L__ZN1XC1Ei
+// CHECK-LP32: call     L__ZN1XC1EPKci
+// CHECK-LP32: call     L__ZN1XC1Ev