[PATCH] D23096: [Sema] Pass CombineWithOuterScope = true to constructor of LocalInstantiationScope

Serge Pavlov via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 6 23:47:30 PDT 2016


sepavloff added inline comments.

================
Comment at: include/clang/Sema/Sema.h:6619
@@ -6618,1 +6618,3 @@
 
+  ArrayRef<TemplateArgument> varTemplateArguments() const;
+
----------------
I would propose to use something like 'getVarTemplateArguments' to make the name more similar to other methods.

================
Comment at: lib/Sema/SemaTemplateInstantiate.cpp:193-205
@@ -185,2 +192,15 @@
 
+ArrayRef<TemplateArgument> Sema::varTemplateArguments() const {
+  if (ActiveTemplateInstantiations.empty())
+    return ArrayRef<TemplateArgument>();
+
+  const auto &Inst = ActiveTemplateInstantiations.back();
+
+  if (const auto *VD = dyn_cast_or_null<VarDecl>(Inst.Entity))
+    if (VD->getDescribedVarTemplate() || VD->getTemplateSpecializationKind())
+      return Inst.template_arguments();
+
+  return ArrayRef<TemplateArgument>();
+}
+
 bool Sema::ActiveTemplateInstantiation::isInstantiationRecord() const {
----------------
The code does not seem to be the best solution. It assumes that if instantiation of a variable is requested, its arguments define all parameters needed to instantiate code. It is not so for the code:
```
template<typename T2>
struct C1 {
  template<typename T>
  static int v1;
};

template<typename T2>
template<typename T>
int C1<T2>::v1 = [](int a = T2(1)) { return a; }();

int main(int argc, char *argv[]) {
  (void) C1<long*>::v1<int>;
  return 0;
}
``` 
This problem is caused by different nature of variable templates. When instantiating a function or a class all instantiated entities go to the contexts defined by the class or the function respectively. A variable does not represent a declaration context and its initializer and all declarations created during the instantiation go to enclosing context. So logic in `getTemplateInstantiationArgs` does not work.
May be we should extend the logic of `getTemplateInstantiationArgs` so that it supports variable template and still remains recursive?


https://reviews.llvm.org/D23096





More information about the cfe-commits mailing list