[PATCH] Instantiation all of local class members.

Richard Smith richard at metafoo.co.uk
Mon Jun 8 13:38:48 PDT 2015


This looks like a good start; I'm happy for you to go ahead with this as-is and deal with the local function declaration case separately if you prefer, or (since that change will require significant changes to this patch) rework this patch to deal with both cases together.


================
Comment at: lib/Sema/SemaTemplateInstantiateDecl.cpp:1825-1841
@@ +1824,19 @@
+
+    // If this is a method of a local class, as per DR1484 its default arguments
+    // must be instantiated now.
+    CXXRecordDecl *ClassD = cast<CXXRecordDecl>(D->getDeclContext());
+    if (ClassD->isLocalClass() && !ClassD->isLambda()) {
+      ParmVarDecl *OldParm = D->getParamDecl(P);
+      if (Expr *OldDefArg = OldParm->getDefaultArg()) {
+        ExprResult NewDefArg;
+        {
+          Sema::ContextRAII SavedContext(SemaRef, ClassD);
+          LocalInstantiationScope Local(SemaRef);
+          NewDefArg = SemaRef.SubstExpr(OldDefArg, TemplateArgs);
+        }
+        if (NewDefArg.isInvalid())
+          D->setInvalidDecl();
+        Params[P]->setDefaultArg(NewDefArg.get());
+      }
+    }
+  }
----------------
I don't think this is the right place for this; we should do this if we're in a local function declaration within a function definition too. Consider:

    template<typename T> void f() {
      int g(int defarg = T::error);
    }                        
    template void f<int>();

Can you do the eager instantiation in `Sema::SubstParmVarDecl` instead?

================
Comment at: lib/Sema/SemaTemplateInstantiateDecl.cpp:3268-3273
@@ -3248,2 +3267,8 @@
     // exception specification.
+    // DR1484: Local classes and their members are instantiated along with the
+    // containing function.
+    bool InLocalClass = false;
+    if (CXXRecordDecl *Class = dyn_cast<CXXRecordDecl>(Tmpl->getDeclContext()))
+      if (Class->isLocalClass())
+        InLocalClass = true;
     if (SemaRef.getLangOpts().CPlusPlus11 &&
----------------
This should likewise also apply to the case of a function declaration within a function definition.

http://reviews.llvm.org/D9990

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/






More information about the cfe-commits mailing list