1a5728872c7702ddd09537c95bc3cbd20e1f2fb09Daniel Dunbar// RUN: %clang_cc1 -fsyntax-only -verify %s
2d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
3d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregortemplate void *; // expected-error{{expected unqualified-id}}
4d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
5d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregortemplate typedef void f0; // expected-error{{explicit instantiation of typedef}}
6d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
7d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregorint v0; // expected-note{{refers here}}
8d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregortemplate int v0; // expected-error{{does not refer}}
9d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
10d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregortemplate<typename T>
11d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregorstruct X0 {
12d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  static T value;
13d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
14d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  T f0(T x) {
15d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    return x + 1;  // expected-error{{invalid operands}}
16d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  }
1750800fc551ac6b8a95cca662223e7f061bbd169aDavid Blaikie  T* f0(T*, T*) { return T(); } // expected-warning{{expression which evaluates to zero treated as a null pointer constant of type 'int *'}}
184384712b3a0aedd7c68d6abdb0407850f7b46c8bLarisse Voufo
19651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  template <typename U> T f0(T, U) { return T(); } // expected-note-re {{candidate template ignored: could not match 'int (int, U){{( __attribute__\(\(thiscall\)\))?}}' against 'int (int){{( __attribute__\(\(thiscall\)\))?}} const'}} \
204384712b3a0aedd7c68d6abdb0407850f7b46c8bLarisse Voufo                                                   // expected-note {{candidate template ignored: could not match 'int' against 'int *'}}
21d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor};
22d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
23558c03222c77873a934b002073667a3c971fe8a9Douglas Gregortemplate<typename T>
24558c03222c77873a934b002073667a3c971fe8a9Douglas GregorT X0<T>::value; // expected-error{{no matching constructor}}
25558c03222c77873a934b002073667a3c971fe8a9Douglas Gregor
26d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregortemplate int X0<int>::value;
27d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
28220ccbf2c9ef97034cce80561f9f46c4f1f63bc7John McCallstruct NotDefaultConstructible { // expected-note{{candidate constructor (the implicit copy constructor)}}
29b1622a1fd7b7f4ab8d00d0183d17c90ad25c14e3John McCall  NotDefaultConstructible(int); // expected-note{{candidate constructor}}
30d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor};
31d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
32558c03222c77873a934b002073667a3c971fe8a9Douglas Gregortemplate NotDefaultConstructible X0<NotDefaultConstructible>::value; // expected-note{{instantiation}}
33d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
34d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregortemplate int X0<int>::f0(int);
3550800fc551ac6b8a95cca662223e7f061bbd169aDavid Blaikietemplate int* X0<int>::f0(int*, int*); // expected-note{{in instantiation of member function 'X0<int>::f0' requested here}}
36d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregortemplate int X0<int>::f0(int, float);
37d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
38d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregortemplate int X0<int>::f0(int) const; // expected-error{{does not refer}}
39d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregortemplate int* X0<int>::f0(int*, float*); // expected-error{{does not refer}}
40d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
41d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregorstruct X1 { };
42d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregortypedef int X1::*MemPtr;
43d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
44d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregortemplate MemPtr X0<MemPtr>::f0(MemPtr); // expected-note{{requested here}}
45d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
46d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregorstruct X2 {
47d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  int f0(int); // expected-note{{refers here}}
48d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
49558c03222c77873a934b002073667a3c971fe8a9Douglas Gregor  template<typename T> T f1(T) { return T(); }
50558c03222c77873a934b002073667a3c971fe8a9Douglas Gregor  template<typename T> T* f1(T*) { return 0; }
51d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
52558c03222c77873a934b002073667a3c971fe8a9Douglas Gregor  template<typename T, typename U> void f2(T, U*) { } // expected-note{{candidate}}
53558c03222c77873a934b002073667a3c971fe8a9Douglas Gregor  template<typename T, typename U> void f2(T*, U) { } // expected-note{{candidate}}
54d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor};
55d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
56d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregortemplate int X2::f0(int); // expected-error{{not an instantiation}}
57d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
58d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregortemplate int *X2::f1(int *); // okay
59d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
60d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregortemplate void X2::f2(int *, int *); // expected-error{{ambiguous}}
61db422dffb720ff41d0b60e228f45c685600ffa9eDouglas Gregor
624384712b3a0aedd7c68d6abdb0407850f7b46c8bLarisse Voufotemplate <typename T>
634384712b3a0aedd7c68d6abdb0407850f7b46c8bLarisse Voufovoid print_type() {} // expected-note {{candidate template ignored: could not match 'void ()' against 'void (float *)'}}
64db422dffb720ff41d0b60e228f45c685600ffa9eDouglas Gregor
65db422dffb720ff41d0b60e228f45c685600ffa9eDouglas Gregortemplate void print_type<int>();
66db422dffb720ff41d0b60e228f45c685600ffa9eDouglas Gregortemplate void print_type<float>();
67db422dffb720ff41d0b60e228f45c685600ffa9eDouglas Gregor
684384712b3a0aedd7c68d6abdb0407850f7b46c8bLarisse Voufotemplate <typename T>
694384712b3a0aedd7c68d6abdb0407850f7b46c8bLarisse Voufovoid print_type(T *) {} // expected-note {{candidate template ignored: could not match 'void (int *)' against 'void (float *)'}}
70db422dffb720ff41d0b60e228f45c685600ffa9eDouglas Gregor
71db422dffb720ff41d0b60e228f45c685600ffa9eDouglas Gregortemplate void print_type(int*);
72db422dffb720ff41d0b60e228f45c685600ffa9eDouglas Gregortemplate void print_type<int>(float*); // expected-error{{does not refer}}
73db422dffb720ff41d0b60e228f45c685600ffa9eDouglas Gregor
74db422dffb720ff41d0b60e228f45c685600ffa9eDouglas Gregorvoid print_type(double*);
75db422dffb720ff41d0b60e228f45c685600ffa9eDouglas Gregortemplate void print_type<double>(double*);
76b2f81cf7f8445e0c65c0428f4fbb0442566916b8Douglas Gregor
77b2f81cf7f8445e0c65c0428f4fbb0442566916b8Douglas Gregor// PR5069
78b2f81cf7f8445e0c65c0428f4fbb0442566916b8Douglas Gregortemplate<int I> void foo0 (int (&)[I + 1]) { }
79b2f81cf7f8445e0c65c0428f4fbb0442566916b8Douglas Gregortemplate void foo0<2> (int (&)[3]);
80291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth
81291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruthnamespace explicit_instantiation_after_implicit_instantiation {
82291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth  template <int I> struct X0 { static int x; };
83291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth  template <int I> int X0<I>::x;
84291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth  void test1() { (void)&X0<1>::x; }
85291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth  template struct X0<1>;
86291b441a095b289c30d9f311fe37a2cf5ed6fd45Chandler Carruth}
87669eed8d676458c701f45f7fd686f01de2dee53bDouglas Gregor
887306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregortemplate<typename> struct X3 { };
897306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregorinline template struct X3<int>; // expected-warning{{ignoring 'inline' keyword on explicit template instantiation}}
907306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregorstatic template struct X3<float>; // expected-warning{{ignoring 'static' keyword on explicit template instantiation}}
917306ebfacfa51ba5270fd20f162f62d2ed813485Douglas Gregor
92d941fa4ff0d0d64b5a541dbd4f99693bff7f6e8dSebastian Redlnamespace PR7622 {
93669eed8d676458c701f45f7fd686f01de2dee53bDouglas Gregor  template<typename,typename=int>
94669eed8d676458c701f45f7fd686f01de2dee53bDouglas Gregor  struct basic_streambuf;
95669eed8d676458c701f45f7fd686f01de2dee53bDouglas Gregor
96669eed8d676458c701f45f7fd686f01de2dee53bDouglas Gregor  template<typename,typename>
97669eed8d676458c701f45f7fd686f01de2dee53bDouglas Gregor  struct basic_streambuf{friend bob<>()}; // expected-error{{unknown type name 'bob'}} \
984384712b3a0aedd7c68d6abdb0407850f7b46c8bLarisse Voufo                                          // expected-error{{expected member name or ';' after declaration specifiers}}
99d941fa4ff0d0d64b5a541dbd4f99693bff7f6e8dSebastian Redl  template struct basic_streambuf<int>;
100d941fa4ff0d0d64b5a541dbd4f99693bff7f6e8dSebastian Redl}
1019241057266d3460392cbb7fec6ec942d3330ece3Argyrios Kyrtzidis
1029241057266d3460392cbb7fec6ec942d3330ece3Argyrios Kyrtzidis// Test that we do not crash.
1039241057266d3460392cbb7fec6ec942d3330ece3Argyrios Kyrtzidisclass TC1 {
1049241057266d3460392cbb7fec6ec942d3330ece3Argyrios Kyrtzidis  class TC2 {
1059241057266d3460392cbb7fec6ec942d3330ece3Argyrios Kyrtzidis    template // FIXME: error here.
1069241057266d3460392cbb7fec6ec942d3330ece3Argyrios Kyrtzidis    void foo() { }
1079241057266d3460392cbb7fec6ec942d3330ece3Argyrios Kyrtzidis   };
1089241057266d3460392cbb7fec6ec942d3330ece3Argyrios Kyrtzidis};
109313a9f46ac8cfae224ccfb2a5ffc88630189d24cSerge Pavlov
110313a9f46ac8cfae224ccfb2a5ffc88630189d24cSerge Pavlovnamespace PR8020 {
111313a9f46ac8cfae224ccfb2a5ffc88630189d24cSerge Pavlov  template <typename T> struct X { X() {} };
112313a9f46ac8cfae224ccfb2a5ffc88630189d24cSerge Pavlov  template<> struct X<int> { X(); };
113313a9f46ac8cfae224ccfb2a5ffc88630189d24cSerge Pavlov  template X<int>::X() {}  // expected-error{{function cannot be defined in an explicit instantiation}}
114313a9f46ac8cfae224ccfb2a5ffc88630189d24cSerge Pavlov}
115313a9f46ac8cfae224ccfb2a5ffc88630189d24cSerge Pavlov
116313a9f46ac8cfae224ccfb2a5ffc88630189d24cSerge Pavlovnamespace PR10086 {
117313a9f46ac8cfae224ccfb2a5ffc88630189d24cSerge Pavlov  template void foobar(int i) {}  // expected-error{{function cannot be defined in an explicit instantiation}}
118313a9f46ac8cfae224ccfb2a5ffc88630189d24cSerge Pavlov  int func() {
119313a9f46ac8cfae224ccfb2a5ffc88630189d24cSerge Pavlov    foobar(5);
120313a9f46ac8cfae224ccfb2a5ffc88630189d24cSerge Pavlov  }
121313a9f46ac8cfae224ccfb2a5ffc88630189d24cSerge Pavlov}
122d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith
123d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smithnamespace undefined_static_data_member {
124d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  template<typename T> struct A {
125d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    static int a; // expected-note {{here}}
126d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    template<typename U> static int b; // expected-note {{here}} expected-warning {{extension}}
127d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  };
128d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  struct B {
129d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    template<typename U> static int c; // expected-note {{here}} expected-warning {{extension}}
130d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  };
131d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith
132d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  template int A<int>::a; // expected-error {{explicit instantiation of undefined static data member 'a' of class template 'undefined_static_data_member::A<int>'}}
133d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  template int A<int>::b<int>; // expected-error {{explicit instantiation of undefined variable template 'undefined_static_data_member::A<int>::b<int>'}}
134d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  template int B::c<int>; // expected-error {{explicit instantiation of undefined variable template 'undefined_static_data_member::B::c<int>'}}
135d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith
136d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith
137d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  template<typename T> struct C {
138d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    static int a;
139d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    template<typename U> static int b; // expected-warning {{extension}}
140d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  };
141d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  struct D {
142d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith    template<typename U> static int c; // expected-warning {{extension}}
143d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  };
144d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  template<typename T> int C<T>::a;
145d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  template<typename T> template<typename U> int C<T>::b; // expected-warning {{extension}}
146d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  template<typename U> int D::c; // expected-warning {{extension}}
147d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith
148d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  template int C<int>::a;
149d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  template int C<int>::b<int>;
150d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith  template int D::c<int>;
151d0629eb137d06bf6d46a430abdb7fa044909298bRichard Smith}
152