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