]> granicus.if.org Git - clang/commitdiff
C++1y: provide full 'auto' return type deduction for lambda expressions. This
authorRichard Smith <richard-llvm@metafoo.co.uk>
Sun, 12 May 2013 03:09:35 +0000 (03:09 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Sun, 12 May 2013 03:09:35 +0000 (03:09 +0000)
completes the implementation of N3638.

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

docs/LanguageExtensions.rst
lib/Lex/PPMacroExpansion.cpp
lib/Sema/SemaStmt.cpp
test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.spec.auto/p12-1y.cpp [new file with mode: 0644]
test/CXX/expr/expr.prim/expr.prim.lambda/p4-1y.cpp [new file with mode: 0644]
test/CXX/expr/expr.prim/expr.prim.lambda/p4.cpp
test/Lexer/has_feature_cxx0x.cpp
www/cxx_status.html

index 324feafa98f04c14a893cc3d00c142391c92b310..0c56d5eda778883a86cf9e25181f5c84375ef201 100644 (file)
@@ -852,7 +852,6 @@ Use ``__has_feature(cxx_return_type_deduction)`` or
 ``__has_extension(cxx_return_type_deduction)`` to determine if support
 for return type deduction for functions (using ``auto`` as a return type)
 is enabled.
-Clang's implementation of this feature is incomplete.
 
 C++1y runtime-sized arrays
 ^^^^^^^^^^^^^^^^^^^^^^^^^^
index 24c6217ced2a248c293a89132b9309a993e24033..3abca1f14985891395d89183a5f9f16ab73cc6b5 100644 (file)
@@ -798,7 +798,7 @@ static bool HasFeature(const Preprocessor &PP, const IdentifierInfo *II) {
            //.Case("cxx_generalized_capture", LangOpts.CPlusPlus1y)
            //.Case("cxx_generic_lambda", LangOpts.CPlusPlus1y)
            //.Case("cxx_relaxed_constexpr", LangOpts.CPlusPlus1y)
-           //.Case("cxx_return_type_deduction", LangOpts.CPlusPlus1y)
+           .Case("cxx_return_type_deduction", LangOpts.CPlusPlus1y)
            //.Case("cxx_runtime_array", LangOpts.CPlusPlus1y)
            .Case("cxx_aggregate_nsdmi", LangOpts.CPlusPlus1y)
            //.Case("cxx_variable_templates", LangOpts.CPlusPlus1y)
index b5ff7a30a9e21bc86d5484c48408ab97a09fb17f..6d568c4d0b0a82d5af56dc56d6d5cff781990a51 100644 (file)
@@ -2362,22 +2362,37 @@ Sema::PerformMoveOrCopyInitialization(const InitializedEntity &Entity,
 StmtResult
 Sema::ActOnCapScopeReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) {
   // If this is the first return we've seen, infer the return type.
-  // [expr.prim.lambda]p4 in C++11; block literals follow a superset of those
-  // rules which allows multiple return statements.
+  // [expr.prim.lambda]p4 in C++11; block literals follow the same rules.
   CapturingScopeInfo *CurCap = cast<CapturingScopeInfo>(getCurFunction());
   QualType FnRetType = CurCap->ReturnType;
 
   // For blocks/lambdas with implicit return types, we check each return
   // statement individually, and deduce the common return type when the block
   // or lambda is completed.
-  if (CurCap->HasImplicitReturnType) {
+  if (AutoType *AT =
+          FnRetType.isNull() ? 0 : FnRetType->getContainedAutoType()) {
+    // In C++1y, the return type may involve 'auto'.
+    FunctionDecl *FD = cast<LambdaScopeInfo>(CurCap)->CallOperator;
+    if (CurContext->isDependentContext()) {
+      // C++1y [dcl.spec.auto]p12:
+      //   Return type deduction [...] occurs when the definition is
+      //   instantiated even if the function body contains a return
+      //   statement with a non-type-dependent operand.
+      CurCap->ReturnType = FnRetType = Context.DependentTy;
+    } else if (DeduceFunctionTypeFromReturnExpr(FD, ReturnLoc, RetValExp, AT)) {
+      FD->setInvalidDecl();
+      return StmtError();
+    } else
+      CurCap->ReturnType = FnRetType = FD->getResultType();
+  } else if (CurCap->HasImplicitReturnType) {
+    // FIXME: Fold this into the 'auto' codepath above.
     if (RetValExp && !isa<InitListExpr>(RetValExp)) {
       ExprResult Result = DefaultFunctionArrayLvalueConversion(RetValExp);
       if (Result.isInvalid())
         return StmtError();
       RetValExp = Result.take();
 
-      if (!RetValExp->isTypeDependent())
+      if (!CurContext->isDependentContext())
         FnRetType = RetValExp->getType();
       else
         FnRetType = CurCap->ReturnType = Context.DependentTy;
@@ -2553,7 +2568,6 @@ Sema::ActOnReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) {
   if (RetValExp && DiagnoseUnexpandedParameterPack(RetValExp))
     return StmtError();
 
-  // FIXME: Unify this and C++1y auto function handling.
   if (isa<CapturingScopeInfo>(getCurFunction()))
     return ActOnCapScopeReturnStmt(ReturnLoc, RetValExp);
 
diff --git a/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.spec.auto/p12-1y.cpp b/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.spec.auto/p12-1y.cpp
new file mode 100644 (file)
index 0000000..ff2abf9
--- /dev/null
@@ -0,0 +1,17 @@
+// RUN: %clang_cc1 -std=c++11 -verify %s
+// RUN: %clang_cc1 -std=c++1y -verify %s
+
+template<typename T> struct S { typedef int type; };
+
+template<typename T> void f() {
+  auto x = [] { return 0; } ();
+  // FIXME: We should be able to produce a 'missing typename' diagnostic here.
+  S<decltype(x)>::type n; // expected-error 2{{}}
+}
+
+#if __cplusplus > 201103L
+template<typename T> void g() {
+  auto x = [] () -> auto { return 0; } ();
+  S<decltype(x)>::type n; // expected-error 2{{}}
+}
+#endif
diff --git a/test/CXX/expr/expr.prim/expr.prim.lambda/p4-1y.cpp b/test/CXX/expr/expr.prim/expr.prim.lambda/p4-1y.cpp
new file mode 100644 (file)
index 0000000..79ee76f
--- /dev/null
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -fsyntax-only -std=c++1y %s -verify
+
+int a;
+int &b = [] (int &r) -> decltype(auto) { return r; } (a);
+int &c = [] (int &r) -> decltype(auto) { return (r); } (a);
+int &d = [] (int &r) -> auto & { return r; } (a);
+int &e = [] (int &r) -> auto { return r; } (a); // expected-error {{cannot bind to a temporary}}
+int &f = [] (int r) -> decltype(auto) { return r; } (a); // expected-error {{cannot bind to a temporary}}
+int &g = [] (int r) -> decltype(auto) { return (r); } (a); // expected-warning {{reference to stack}}
index f580e7e4c46f191093c93f3d67417ba3b3b393de..368b3f695b9821fb758083a7b666abbd18de06dc 100644 (file)
@@ -1,4 +1,5 @@
 // RUN: %clang_cc1 -fsyntax-only -std=c++11 %s -verify
+// RUN: %clang_cc1 -fsyntax-only -std=c++1y %s -verify
 
 void missing_lambda_declarator() {
   [](){}();
index 62a965caacb1f050b0d9b21ba61e45bc1f22104a..590c168768d1edaba16df9da5463507e89ac7dfe 100644 (file)
@@ -346,3 +346,13 @@ int no_aggregate_nsdmi();
 // CHECK-1Y: has_aggregate_nsdmi
 // CHECK-11: no_aggregate_nsdmi
 // CHECK-NO-11: no_aggregate_nsdmi
+
+#if __has_feature(cxx_return_type_deduction)
+int has_return_type_deduction();
+#else
+int no_return_type_deduction();
+#endif
+
+// CHECK-1Y: has_return_type_deduction
+// CHECK-11: no_return_type_deduction
+// CHECK-NO-11: no_return_type_deduction
index ee1827ecf174e26c82c3502b9cd5e357b873811b..7643c7a7b1ceb33c19ba9d4647d0e41e5769bd28 100644 (file)
@@ -431,7 +431,7 @@ available.</p>
     </tr>
     <tr>
       <td>Return type deduction for normal functions</td>
-      <td class="partial" align="center">Partial</td>
+      <td class="svn" align="center">SVN</td>
     </tr>
     <tr>
       <td>Runtime-sized arrays with automatic storage duration</td>