1// RUN: %clang_cc1 -fsyntax-only -verify %s
2// RUN: %clang_cc1 -fsyntax-only -verify %s -fdelayed-template-parsing -DDELAYED_TEMPLATE_PARSING
3// RUN: %clang_cc1 -fsyntax-only -verify -std=gnu++1z %s
4
5
6
7// Errors
8export class foo { };   // expected-error {{expected template}}
9template  x;            // expected-error {{C++ requires a type specifier for all declarations}} \
10                        // expected-error {{does not refer}}
11export template x;      // expected-error {{expected '<' after 'template'}}
12export template<class T> class x0; // expected-warning {{exported templates are unsupported}}
13template < ;            // expected-error {{expected template parameter}} \
14// expected-error{{expected ',' or '>' in template-parameter-list}} \
15// expected-warning {{declaration does not declare anything}}
16template <int +> struct x1; // expected-error {{expected ',' or '>' in template-parameter-list}}
17
18// verifies that we only walk to the ',' & still produce errors on the rest of the template parameters
19template <int +, T> struct x2; // expected-error {{expected ',' or '>' in template-parameter-list}} \
20                                expected-error {{expected unqualified-id}}
21template<template<int+>> struct x3; // expected-error {{expected ',' or '>' in template-parameter-list}} \
22                                         expected-error {{template template parameter requires 'class' after the parameter list}}
23template <template X> struct Err1; // expected-error {{expected '<' after 'template'}} \
24// expected-error{{extraneous}}
25template <template <typename> > struct Err2;       // expected-error {{template template parameter requires 'class' after the parameter list}}
26template <template <typename> Foo> struct Err3;    // expected-error {{template template parameter requires 'class' after the parameter list}}
27
28template <template <typename> typename Foo> struct Cxx1z;
29#if __cplusplus <= 201402L
30// expected-warning@-2 {{extension}}
31#endif
32
33// Template function declarations
34template <typename T> void foo();
35template <typename T, typename U> void foo();
36
37// Template function definitions.
38template <typename T> void foo() { }
39
40// Template class (forward) declarations
41template <typename T> struct A;
42template <typename T, typename U> struct b;
43template <typename> struct C;
44template <typename, typename> struct D;
45
46// Forward declarations with default parameters?
47template <typename T = int> class X1;
48template <typename = int> class X2;
49
50// Forward declarations w/template template parameters
51template <template <typename> class T> class TTP1;
52template <template <typename> class> class TTP2;
53template <template <typename> class T = foo> class TTP3; // expected-error{{must be a class template}}
54template <template <typename> class = foo> class TTP3; // expected-error{{must be a class template}}
55template <template <typename X, typename Y> class T> class TTP5;
56
57// Forward declarations with non-type params
58template <int> class NTP0;
59template <int N> class NTP1;
60template <int N = 5> class NTP2;
61template <int = 10> class NTP3;
62template <unsigned int N = 12u> class NTP4;
63template <unsigned int = 12u> class NTP5;
64template <unsigned = 15u> class NTP6;
65template <typename T, T Obj> class NTP7;
66
67// Template class declarations
68template <typename T> struct A { };
69template <typename T, typename U> struct B { };
70
71// Template parameter shadowing
72template<typename T, // expected-note{{template parameter is declared here}}
73         typename T> // expected-error{{declaration of 'T' shadows template parameter}}
74  void shadow1();
75
76template<typename T> // expected-note{{template parameter is declared here}}
77void shadow2(int T); // expected-error{{declaration of 'T' shadows template parameter}}
78
79template<typename T> // expected-note{{template parameter is declared here}}
80class T { // expected-error{{declaration of 'T' shadows template parameter}}
81};
82
83template<int Size> // expected-note{{template parameter is declared here}}
84void shadow3(int Size); // expected-error{{declaration of 'Size' shadows template parameter}}
85
86// <rdar://problem/6952203>
87template<typename T> // expected-note{{here}}
88struct shadow4 {
89  int T; // expected-error{{shadows}}
90};
91
92template<typename T> // expected-note{{here}}
93struct shadow5 {
94  int T(int, float); // expected-error{{shadows}}
95};
96
97template<typename T, // expected-note{{template parameter is declared here}}
98         T T> // expected-error{{declaration of 'T' shadows template parameter}}
99void shadow6();
100
101template<typename T, // expected-note{{template parameter is declared here}}
102         template<typename> class T> // expected-error{{declaration of 'T' shadows template parameter}}
103void shadow7();
104
105// PR8302
106template<template<typename> class T> struct shadow8 { // expected-note{{template parameter is declared here}}
107  template<template<typename> class T> struct inner; // expected-error{{declaration of 'T' shadows template parameter}}
108};
109
110// Non-type template parameters in scope
111template<int Size>
112void f(int& i) {
113  i = Size;
114 #ifdef DELAYED_TEMPLATE_PARSING
115  Size = i;
116 #else
117  Size = i; // expected-error{{expression is not assignable}}
118 #endif
119}
120
121template<typename T>
122const T& min(const T&, const T&);
123
124void f2() {
125  int x;
126  A< typeof(x>1) > a;
127}
128
129
130// PR3844
131template <> struct S<int> { }; // expected-error{{explicit specialization of non-template struct 'S'}}
132template <> union U<int> { }; // expected-error{{explicit specialization of non-template union 'U'}}
133
134namespace PR6184 {
135  namespace N {
136    template <typename T>
137    void bar(typename T::x);
138  }
139
140  template <typename T>
141  void N::bar(typename T::x) { }
142}
143
144// This PR occurred only in template parsing mode.
145namespace PR17637 {
146template <int>
147struct L {
148  template <typename T>
149  struct O {
150    template <typename U>
151    static void Fun(U);
152  };
153};
154
155template <int k>
156template <typename T>
157template <typename U>
158void L<k>::O<T>::Fun(U) {}
159
160void Instantiate() { L<0>::O<int>::Fun(0); }
161
162}
163
164namespace explicit_partial_specializations {
165typedef char (&oneT)[1];
166typedef char (&twoT)[2];
167typedef char (&threeT)[3];
168typedef char (&fourT)[4];
169typedef char (&fiveT)[5];
170typedef char (&sixT)[6];
171
172char one[1];
173char two[2];
174char three[3];
175char four[4];
176char five[5];
177char six[6];
178
179template<bool b> struct bool_ { typedef int type; };
180template<> struct bool_<false> {  };
181
182#define XCAT(x,y) x ## y
183#define CAT(x,y) XCAT(x,y)
184#define sassert(_b_) bool_<(_b_)>::type CAT(var, __LINE__);
185
186
187template <int>
188struct L {
189  template <typename T>
190  struct O {
191    template <typename U>
192    static oneT Fun(U);
193
194  };
195};
196template <int k>
197template <typename T>
198template <typename U>
199oneT L<k>::O<T>::Fun(U) { return one; }
200
201template<>
202template<>
203template<typename U>
204oneT L<0>::O<char>::Fun(U) { return one; }
205
206
207void Instantiate() {
208  sassert(sizeof(L<0>::O<int>::Fun(0)) == sizeof(one));
209  sassert(sizeof(L<0>::O<char>::Fun(0)) == sizeof(one));
210}
211
212}
213