Implement parsing of nested-name-specifiers that involve template-ids, e.g.,

  std::vector<int>::allocator_type

When we parse a template-id that names a type, it will become either a
template-id annotation (which is a parsed representation of a
template-id that has not yet been through semantic analysis) or a
typename annotation (where semantic analysis has resolved the
template-id to an actual type), depending on the context. We only
produce a type in contexts where we know that we only need type
information, e.g., in a type specifier. Otherwise, we create a
template-id annotation that can later be "upgraded" by transforming it
into a typename annotation when the parser needs a type. This occurs,
for example, when we've parsed "std::vector<int>" above and then see
the '::' after it. However, it means that when writing something like
this:

  template<> class Outer::Inner<int> { ... };

We have two tokens to represent Outer::Inner<int>: one token for the
nested name specifier Outer::, and one template-id annotation token
for Inner<int>, which will be passed to semantic analysis to define
the class template specialization.

Most of the churn in the template tests in this patch come from an
improvement in our error recovery from ill-formed template-ids.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@65467 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/test/SemaTemplate/class-template-spec.cpp b/test/SemaTemplate/class-template-spec.cpp
index 9347bf5..86cd52c 100644
--- a/test/SemaTemplate/class-template-spec.cpp
+++ b/test/SemaTemplate/class-template-spec.cpp
@@ -40,3 +40,5 @@
   x1->foo(); // okay: refers to #1
   x2->bar(); // okay: refers to #2
 }
+
+// FIXME: diagnose specializations in a different namespace
diff --git a/test/SemaTemplate/default-arguments.cpp b/test/SemaTemplate/default-arguments.cpp
index 94976e9..cc0da19 100644
--- a/test/SemaTemplate/default-arguments.cpp
+++ b/test/SemaTemplate/default-arguments.cpp
@@ -5,8 +5,7 @@
 X<int, 1> *x1;
 X<int> *x2;
 
-X<> *x3; // expected-error{{too few template arguments for class template 'X'}} \
-        // FIXME: expected-error{{expected unqualified-id}}
+X<> *x3; // expected-error{{too few template arguments for class template 'X'}}
 
 template<typename U = float, int M> struct X;
 
diff --git a/test/SemaTemplate/nested-name-spec-template.cpp b/test/SemaTemplate/nested-name-spec-template.cpp
new file mode 100644
index 0000000..4007f23
--- /dev/null
+++ b/test/SemaTemplate/nested-name-spec-template.cpp
@@ -0,0 +1,50 @@
+// RUN: clang -fsyntax-only -verify %s
+
+namespace N { 
+  namespace M {
+    template<typename T> struct Promote; // expected-note{{previous definition is here}}
+    
+    template<> struct Promote<short> {
+      typedef int type;
+    };
+    
+    template<> struct Promote<int> {
+      typedef int type;
+    };
+    
+    template<> struct Promote<float> {
+      typedef double type;
+    };
+    
+    Promote<short>::type *ret_intptr(int* ip) { return ip; }
+    Promote<int>::type *ret_intptr2(int* ip) { return ip; }
+  }
+
+  M::Promote<int>::type *ret_intptr3(int* ip) { return ip; }
+  M::template Promote<int>::type *ret_intptr4(int* ip) { return ip; }
+}
+
+N::M::Promote<int>::type *ret_intptr5(int* ip) { return ip; }
+::N::M::Promote<int>::type *ret_intptr6(int* ip) { return ip; }
+
+
+N::M::template; // expected-error{{expected template name after 'template' keyword in nested name specifier}} \
+               // expected-error{{expected unqualified-id}}
+
+N::M::template Promote; // expected-error{{expected '<' after 'template Promote' in nested name specifier}} \
+// expected-error{{C++ requires a type specifier for all declarations}} \
+// expected-error{{redefinition of 'Promote' as different kind of symbol}} \
+// expected-error{{no member named 'Promote'}}
+
+namespace N {
+  template<typename T> struct A;
+
+  template<>
+  struct A<int> {
+    struct X;
+  };
+}
+
+struct ::N::A<int>::X {
+  int foo;
+};
diff --git a/test/SemaTemplate/temp_arg.cpp b/test/SemaTemplate/temp_arg.cpp
index b8bb279..b974c62 100644
--- a/test/SemaTemplate/temp_arg.cpp
+++ b/test/SemaTemplate/temp_arg.cpp
@@ -8,7 +8,5 @@
 
 A<int, 0, X> * a1;
 
-A<float, 1, X, double> *a2; // expected-error{{too many template arguments for class template 'A'}} \
-          // expected-error{{unqualified-id}}
-A<float, 1> *a3; // expected-error{{too few template arguments for class template 'A'}} \
-          // expected-error{{unqualified-id}}
+A<float, 1, X, double> *a2; // expected-error{{too many template arguments for class template 'A'}}
+A<float, 1> *a3; // expected-error{{too few template arguments for class template 'A'}}
diff --git a/test/SemaTemplate/temp_arg_nontype.cpp b/test/SemaTemplate/temp_arg_nontype.cpp
index 89dbf23..d67cc4e 100644
--- a/test/SemaTemplate/temp_arg_nontype.cpp
+++ b/test/SemaTemplate/temp_arg_nontype.cpp
@@ -3,26 +3,21 @@
 
 A<0> *a0;
 
-A<int()> *a1; // expected-error{{template argument for non-type template parameter is treated as type 'int (void)'}} \
-              // FIXME: expected-error{{unqualified-id}}
+A<int()> *a1; // expected-error{{template argument for non-type template parameter is treated as type 'int (void)'}}
 
-A<int> *a2; // expected-error{{template argument for non-type template parameter must be an expression}} \
-              // FIXME: expected-error{{unqualified-id}}
+A<int> *a2; // expected-error{{template argument for non-type template parameter must be an expression}}
 
 A<1 >> 2> *a3;
 
 // C++ [temp.arg.nontype]p5:
 A<A> *a4; // expected-error{{must have an integral or enumeration type}} \
-          // FIXME: the error message above is a bit lame \
-          // FIXME: expected-error{{expected unqualified-id}}
+          // FIXME: the error message above is a bit lame
 
 enum E { Enumerator = 17 };
-A<E> *a5; // expected-error{{template argument for non-type template parameter must be an expression}} \
-          // FIXME: expected-error{{unqualified-id}}
+A<E> *a5; // expected-error{{template argument for non-type template parameter must be an expression}}
 template<E Value> struct A1; // expected-note{{template parameter is declared here}}
 A1<Enumerator> *a6; // okay
-A1<17> *a7; // expected-error{{non-type template argument of type 'int' cannot be converted to a value of type 'enum E'}} \
-          // FIXME: expected-error{{expected unqualified-id}}
+A1<17> *a7; // expected-error{{non-type template argument of type 'int' cannot be converted to a value of type 'enum E'}}
 
 const long LongValue = 12345678;
 A<LongValue> *a8;
@@ -30,8 +25,7 @@
 A<ShortValue> *a9;
 
 int f(int);
-A<f(17)> *a10; // expected-error{{non-type template argument of type 'int' is not an integral constant expression}} \
-          // FIXME: expected-error{{expected unqualified-id}}
+A<f(17)> *a10; // expected-error{{non-type template argument of type 'int' is not an integral constant expression}}
 
 class X {
 public:
@@ -39,8 +33,7 @@
   X(int, int);
   operator int() const;
 };
-A<X(17, 42)> *a11; // expected-error{{non-type template argument of type 'class X' must have an integral or enumeration type}} \
-                   // FIXME:expected-error{{expected unqualified-id}}
+A<X(17, 42)> *a11; // expected-error{{non-type template argument of type 'class X' must have an integral or enumeration type}}
 
 template<X const *Ptr> struct A2;
 
@@ -50,8 +43,7 @@
 A2<X_ptr> *a12;
 A2<array_of_Xs> *a13;
 A2<&an_X> *a13_2;
-A2<(&an_X)> *a13_2; // expected-error{{non-type template argument cannot be surrounded by parentheses}} \
-                    // FIXME: expected-error{{unqualified-id}}
+A2<(&an_X)> *a13_3; // expected-error{{non-type template argument cannot be surrounded by parentheses}}
 
 float f(float);
 
@@ -66,10 +58,8 @@
 A3<&h> *a14_2;
 A3<f> *a14_3;
 A3<&f> *a14_4;
-A3<h2> *a14_6;  // expected-error{{non-type template argument of type 'float (*)(float)' cannot be converted to a value of type 'int (*)(int)'}} \
-// FIXME: expected-error{{expected unqualified-id}}
-A3<g> *a14_7; // expected-error{{non-type template argument of type '<overloaded function type>' cannot be converted to a value of type 'int (*)(int)'}}\
-// FIXME: expected-error{{expected unqualified-id}}
+A3<h2> *a14_6;  // expected-error{{non-type template argument of type 'float (*)(float)' cannot be converted to a value of type 'int (*)(int)'}}
+A3<g> *a14_7; // expected-error{{non-type template argument of type '<overloaded function type>' cannot be converted to a value of type 'int (*)(int)'}}
 // FIXME: the first error includes the string <overloaded function
 // type>, which makes Doug slightly unhappy.
 
@@ -79,18 +69,15 @@
 volatile X * X_volatile_ptr;
 template<X const &AnX> struct A4; // expected-note 2{{template parameter is declared here}}
 A4<an_X> *a15_1; // okay
-A4<*X_volatile_ptr> *a15_2; // expected-error{{reference binding of non-type template parameter of type 'class X const &' to template argument of type 'class X volatile' ignores qualifiers}} \
-                  // FIXME: expected-error{{expected unqualified-id}}
-A4<y> *15_3; //  expected-error{{non-type template parameter of reference type 'class X const &' cannot bind to template argument of type 'struct Y'}}\
-                  // FIXME: expected-error{{expected unqualified-id}}
+A4<*X_volatile_ptr> *a15_2; // expected-error{{reference binding of non-type template parameter of type 'class X const &' to template argument of type 'class X volatile' ignores qualifiers}}
+A4<y> *15_3; //  expected-error{{non-type template parameter of reference type 'class X const &' cannot bind to template argument of type 'struct Y'}} \
+            // FIXME: expected-error{{expected unqualified-id}}
 
 template<int (&fr)(int)> struct A5; // expected-note 2{{template parameter is declared here}}
 A5<h> *a16_1;
 A5<f> *a16_3;
-A5<h2> *a16_6;  // expected-error{{non-type template argument of type 'float (float)' cannot be converted to a value of type 'int (&)(int)'}} \
-// FIXME: expected-error{{expected unqualified-id}}
-A5<g> *a14_7; // expected-error{{non-type template argument of type '<overloaded function type>' cannot be converted to a value of type 'int (&)(int)'}}\
-// FIXME: expected-error{{expected unqualified-id}}
+A5<h2> *a16_6;  // expected-error{{non-type template argument of type 'float (float)' cannot be converted to a value of type 'int (&)(int)'}}
+A5<g> *a14_7; // expected-error{{non-type template argument of type '<overloaded function type>' cannot be converted to a value of type 'int (&)(int)'}}
 // FIXME: the first error includes the string <overloaded function
 // type>, which makes Doug slightly unhappy.
 
@@ -106,15 +93,12 @@
 template<int (Z::*pmf)(int)> struct A6; // expected-note{{template parameter is declared here}}
 A6<&Z::foo> *a17_1;
 A6<&Z::bar> *a17_2;
-A6<&Z::baz> *a17_3; // expected-error{{non-type template argument of type 'double (struct Z::*)(double)' cannot be converted to a value of type 'int (struct Z::*)(int)'}} \
-// FIXME: expected-error{{expected unqualified-id}}
+A6<&Z::baz> *a17_3; // expected-error{{non-type template argument of type 'double (struct Z::*)(double)' cannot be converted to a value of type 'int (struct Z::*)(int)'}}
 
 
 template<int Z::*pm> struct A7;  // expected-note{{template parameter is declared here}}
 template<int Z::*pm> struct A7c;
 A7<&Z::int_member> *a18_1;
 A7c<&Z::int_member> *a18_2;
-A7<&Z::float_member> *a18_3; // expected-error{{non-type template argument of type 'float struct Z::*' cannot be converted to a value of type 'int struct Z::*'}} \
-              // FIXME: expected-error{{unqualified-id}}
-A7c<(&Z::int_member)> *a18_3; // expected-error{{non-type template argument cannot be surrounded by parentheses}} \
-          // FIXME: expected-error{{expected unqualified-id}}
+A7<&Z::float_member> *a18_3; // expected-error{{non-type template argument of type 'float struct Z::*' cannot be converted to a value of type 'int struct Z::*'}}
+A7c<(&Z::int_member)> *a18_3; // expected-error{{non-type template argument cannot be surrounded by parentheses}}
diff --git a/test/SemaTemplate/temp_arg_template.cpp b/test/SemaTemplate/temp_arg_template.cpp
index e1b9bb4..781ada9 100644
--- a/test/SemaTemplate/temp_arg_template.cpp
+++ b/test/SemaTemplate/temp_arg_template.cpp
@@ -20,23 +20,18 @@
 A<N::Z> *a2;
 A< ::N::Z> *a3;
 
-A<Y> *a4; // expected-error{{template template argument has different template parameters than its corresponding template template parameter}} \
-          // FIXME::expected-error{{expected unqualified-id}}
-A<TooMany> *a5; // expected-error{{template template argument has different template parameters than its corresponding template template parameter}} \
-          // FIXME::expected-error{{expected unqualified-id}}
-B<X> *a6; // expected-error{{template template argument has different template parameters than its corresponding template template parameter}} \
-          // FIXME::expected-error{{expected unqualified-id}}
+A<Y> *a4; // expected-error{{template template argument has different template parameters than its corresponding template template parameter}}
+A<TooMany> *a5; // expected-error{{template template argument has different template parameters than its corresponding template template parameter}}
+B<X> *a6; // expected-error{{template template argument has different template parameters than its corresponding template template parameter}}
 C<Y> *a7;
-C<Ylong> *a8; // expected-error{{template template argument has different template parameters than its corresponding template template parameter}} \
-          // FIXME::expected-error{{expected unqualified-id}}
+C<Ylong> *a8; // expected-error{{template template argument has different template parameters than its corresponding template template parameter}}
 
 template<typename T> void f(int);
 
 // FIXME: we're right to provide an error message, but it should say
 // that we need a class template. We won't get this right until name
 // lookup of 'f' returns a TemplateDecl.
-A<f> *a9; // expected-error{{template argument for template template parameter must be a template}} \
-          // expected-error{{unqualified-id}}
+A<f> *a9; // expected-error{{template argument for template template parameter must be a template}}
 
 // FIXME: The code below is ill-formed, because of the evil digraph '<:'. 
 // We should provide a much better error message than we currently do.
diff --git a/test/SemaTemplate/temp_arg_type.cpp b/test/SemaTemplate/temp_arg_type.cpp
index 36764fc..216a283 100644
--- a/test/SemaTemplate/temp_arg_type.cpp
+++ b/test/SemaTemplate/temp_arg_type.cpp
@@ -2,11 +2,9 @@
 template<typename T> class A; // expected-note 2 {{template parameter is declared here}}
 
 // [temp.arg.type]p1
-A<0> *a1; // expected-error{{template argument for template type parameter must be a type}} \
-          // expected-error{{unqualified-id}}
+A<0> *a1; // expected-error{{template argument for template type parameter must be a type}}
 
-A<A> *a2; // expected-error{{template argument for template type parameter must be a type}} \
-          // expected-error{{unqualified-id}}
+A<A> *a2; // expected-error{{template argument for template type parameter must be a type}}
 
 A<int> *a3;
 A<int()> *a4; 
@@ -16,13 +14,12 @@
 // [temp.arg.type]p2
 void f() {
   class X { };
-  A<X> * a = 0; // expected-error{{template argument uses local type 'class X'}}\
-                // FIXME: expected-error{{expected expression}}
+  A<X> * a = 0; // expected-error{{template argument uses local type 'class X'}} \
+                // FIXME: expected-error{{use of undeclared identifier 'a'}}
 }
 
 struct { int x; } Unnamed; // expected-note{{unnamed type used in template argument was declared here}}
-A<__typeof__(Unnamed)> *a7; // expected-error{{template argument uses unnamed type}} \
-                            // FIXME: expected-error{{expected unqualified-id}}
+A<__typeof__(Unnamed)> *a7; // expected-error{{template argument uses unnamed type}}
 
 // FIXME: [temp.arg.type]p3. The check doesn't really belong here (it
 // belongs somewhere in the template instantiation section).