1 // RUN: %clang_cc1 -std=c++11 -emit-llvm %s -o - -fms-extensions -fdelayed-template-parsing -triple=i386-pc-win32 | FileCheck %s
2 // RUN: %clang_cc1 -std=c++11 -emit-llvm %s -o - -fms-extensions -fdelayed-template-parsing -triple=x86_64-pc-win32 | FileCheck -check-prefix X64 %s
3 
4 template<typename T>
5 class Class {
6  public:
Class()7   Class() {}
8 };
9 
10 class Typename { };
11 
12 template<typename T>
13 class Nested { };
14 
15 template<bool flag>
16 class BoolTemplate {
17  public:
BoolTemplate()18   BoolTemplate() {}
19 };
20 
21 template<int param>
22 class IntTemplate {
23  public:
IntTemplate()24   IntTemplate() {}
25 };
26 
27 template<unsigned param>
28 class UnsignedIntTemplate {
29 public:
UnsignedIntTemplate()30   UnsignedIntTemplate() {}
31 };
32 
33 template<long long param>
34 class LongLongTemplate {
35  public:
LongLongTemplate()36   LongLongTemplate() {}
37 };
38 
39 template<unsigned long long param>
40 class UnsignedLongLongTemplate {
41  public:
UnsignedLongLongTemplate()42   UnsignedLongLongTemplate() {}
43 };
44 
45 template<>
46 class BoolTemplate<true> {
47  public:
BoolTemplate()48   BoolTemplate() {}
Foo(T arg)49   template<class T> void Foo(T arg) {}
50 };
51 
template_mangling()52 void template_mangling() {
53   Class<Typename> c1;
54 // CHECK: call {{.*}} @"\01??0?$Class@VTypename@@@@QAE@XZ"
55 // X64: call {{.*}} @"\01??0?$Class@VTypename@@@@QEAA@XZ"
56 
57   Class<const Typename> c1_const;
58 // CHECK: call {{.*}} @"\01??0?$Class@$$CBVTypename@@@@QAE@XZ"
59 // X64: call {{.*}} @"\01??0?$Class@$$CBVTypename@@@@QEAA@XZ"
60   Class<volatile Typename> c1_volatile;
61 // CHECK: call {{.*}} @"\01??0?$Class@$$CCVTypename@@@@QAE@XZ"
62 // X64: call {{.*}} @"\01??0?$Class@$$CCVTypename@@@@QEAA@XZ"
63   Class<const volatile Typename> c1_cv;
64 // CHECK: call {{.*}} @"\01??0?$Class@$$CDVTypename@@@@QAE@XZ"
65 // X64: call {{.*}} @"\01??0?$Class@$$CDVTypename@@@@QEAA@XZ"
66 
67   Class<Nested<Typename> > c2;
68 // CHECK: call {{.*}} @"\01??0?$Class@V?$Nested@VTypename@@@@@@QAE@XZ"
69 // X64: call {{.*}} @"\01??0?$Class@V?$Nested@VTypename@@@@@@QEAA@XZ"
70 
71   Class<int * const> c_intpc;
72 // CHECK: call {{.*}} @"\01??0?$Class@QAH@@QAE@XZ"
73 // X64: call {{.*}} @"\01??0?$Class@QEAH@@QEAA@XZ"
74   Class<int()> c_ft;
75 // CHECK: call {{.*}} @"\01??0?$Class@$$A6AHXZ@@QAE@XZ"
76 // X64: call {{.*}} @"\01??0?$Class@$$A6AHXZ@@QEAA@XZ"
77   Class<int[]> c_inti;
78 // CHECK: call {{.*}} @"\01??0?$Class@$$BY0A@H@@QAE@XZ"
79 // X64: call {{.*}} @"\01??0?$Class@$$BY0A@H@@QEAA@XZ"
80   Class<int[5]> c_int5;
81 // CHECK: call {{.*}} @"\01??0?$Class@$$BY04H@@QAE@XZ"
82 // X64: call {{.*}} @"\01??0?$Class@$$BY04H@@QEAA@XZ"
83   Class<const int[5]> c_intc5;
84 // CHECK: call {{.*}} @"\01??0?$Class@$$BY04$$CBH@@QAE@XZ"
85 // X64: call {{.*}} @"\01??0?$Class@$$BY04$$CBH@@QEAA@XZ"
86   Class<int * const[5]> c_intpc5;
87 // CHECK: call {{.*}} @"\01??0?$Class@$$BY04QAH@@QAE@XZ"
88 // X64: call {{.*}} @"\01??0?$Class@$$BY04QEAH@@QEAA@XZ"
89 
90   BoolTemplate<false> _false;
91 // CHECK: call {{.*}} @"\01??0?$BoolTemplate@$0A@@@QAE@XZ"
92 // X64: call {{.*}} @"\01??0?$BoolTemplate@$0A@@@QEAA@XZ"
93 
94   BoolTemplate<true> _true;
95   // PR13158
96   _true.Foo(1);
97 // CHECK: call {{.*}} @"\01??0?$BoolTemplate@$00@@QAE@XZ"
98 // X64: call {{.*}} @"\01??0?$BoolTemplate@$00@@QEAA@XZ"
99 // CHECK: call {{.*}} @"\01??$Foo@H@?$BoolTemplate@$00@@QAEXH@Z"
100 // X64: call {{.*}} @"\01??$Foo@H@?$BoolTemplate@$00@@QEAAXH@Z"
101 
102   IntTemplate<0> zero;
103 // CHECK: call {{.*}} @"\01??0?$IntTemplate@$0A@@@QAE@XZ"
104 // X64: call {{.*}} @"\01??0?$IntTemplate@$0A@@@QEAA@XZ"
105 
106   IntTemplate<5> five;
107 // CHECK: call {{.*}} @"\01??0?$IntTemplate@$04@@QAE@XZ"
108 // X64: call {{.*}} @"\01??0?$IntTemplate@$04@@QEAA@XZ"
109 
110   IntTemplate<11> eleven;
111 // CHECK: call {{.*}} @"\01??0?$IntTemplate@$0L@@@QAE@XZ"
112 // X64: call {{.*}} @"\01??0?$IntTemplate@$0L@@@QEAA@XZ"
113 
114   IntTemplate<256> _256;
115 // CHECK: call {{.*}} @"\01??0?$IntTemplate@$0BAA@@@QAE@XZ"
116 // X64: call {{.*}} @"\01??0?$IntTemplate@$0BAA@@@QEAA@XZ"
117 
118   IntTemplate<513> _513;
119 // CHECK: call {{.*}} @"\01??0?$IntTemplate@$0CAB@@@QAE@XZ"
120 // X64: call {{.*}} @"\01??0?$IntTemplate@$0CAB@@@QEAA@XZ"
121 
122   IntTemplate<1026> _1026;
123 // CHECK: call {{.*}} @"\01??0?$IntTemplate@$0EAC@@@QAE@XZ"
124 // X64: call {{.*}} @"\01??0?$IntTemplate@$0EAC@@@QEAA@XZ"
125 
126   IntTemplate<65535> ffff;
127 // CHECK: call {{.*}} @"\01??0?$IntTemplate@$0PPPP@@@QAE@XZ"
128 // X64: call {{.*}} @"\01??0?$IntTemplate@$0PPPP@@@QEAA@XZ"
129 
130   IntTemplate<-1>  neg_1;
131 // CHECK: call {{.*}} @"\01??0?$IntTemplate@$0?0@@QAE@XZ"
132 // X64: call {{.*}} @"\01??0?$IntTemplate@$0?0@@QEAA@XZ"
133   IntTemplate<-9>  neg_9;
134 // CHECK: call {{.*}} @"\01??0?$IntTemplate@$0?8@@QAE@XZ"
135 // X64: call {{.*}} @"\01??0?$IntTemplate@$0?8@@QEAA@XZ"
136   IntTemplate<-10> neg_10;
137 // CHECK: call {{.*}} @"\01??0?$IntTemplate@$0?9@@QAE@XZ"
138 // X64: call {{.*}} @"\01??0?$IntTemplate@$0?9@@QEAA@XZ"
139   IntTemplate<-11> neg_11;
140 // CHECK: call {{.*}} @"\01??0?$IntTemplate@$0?L@@@QAE@XZ"
141 // X64: call {{.*}} @"\01??0?$IntTemplate@$0?L@@@QEAA@XZ"
142 
143   UnsignedIntTemplate<4294967295> ffffffff;
144 // CHECK: call {{.*}} @"\01??0?$UnsignedIntTemplate@$0PPPPPPPP@@@QAE@XZ"
145 // X64: call {{.*}} @"\01??0?$UnsignedIntTemplate@$0PPPPPPPP@@@QEAA@XZ"
146 
147   LongLongTemplate<-9223372036854775807LL-1LL> int64_min;
148 // CHECK: call {{.*}} @"\01??0?$LongLongTemplate@$0?IAAAAAAAAAAAAAAA@@@QAE@XZ"
149 // X64: call {{.*}} @"\01??0?$LongLongTemplate@$0?IAAAAAAAAAAAAAAA@@@QEAA@XZ"
150   LongLongTemplate<9223372036854775807LL>      int64_max;
151 // CHECK: call {{.*}} @"\01??0?$LongLongTemplate@$0HPPPPPPPPPPPPPPP@@@QAE@XZ"
152 // X64: call {{.*}} @"\01??0?$LongLongTemplate@$0HPPPPPPPPPPPPPPP@@@QEAA@XZ"
153   UnsignedLongLongTemplate<18446744073709551615ULL> uint64_max;
154 // CHECK: call {{.*}} @"\01??0?$UnsignedLongLongTemplate@$0?0@@QAE@XZ"
155 // X64: call {{.*}} @"\01??0?$UnsignedLongLongTemplate@$0?0@@QEAA@XZ"
156   UnsignedLongLongTemplate<(unsigned long long)-1>  uint64_neg_1;
157 // CHECK: call {{.*}} @"\01??0?$UnsignedLongLongTemplate@$0?0@@QAE@XZ"
158 // X64: call {{.*}} @"\01??0?$UnsignedLongLongTemplate@$0?0@@QEAA@XZ"
159 }
160 
161 namespace space {
foo(const T & l)162   template<class T> const T& foo(const T& l) { return l; }
163 }
164 // CHECK: "\01??$foo@H@space@@YAABHABH@Z"
165 // X64: "\01??$foo@H@space@@YAAEBHAEBH@Z"
166 
use()167 void use() {
168   space::foo(42);
169 }
170 
171 // PR13455
172 typedef void (*FunctionPointer)(void);
173 
174 template <FunctionPointer function>
FunctionPointerTemplate()175 void FunctionPointerTemplate() {
176   function();
177 }
178 
spam()179 void spam() {
180   FunctionPointerTemplate<spam>();
181 // CHECK: "\01??$FunctionPointerTemplate@$1?spam@@YAXXZ@@YAXXZ"
182 // X64: "\01??$FunctionPointerTemplate@$1?spam@@YAXXZ@@YAXXZ"
183 }
184 
185 // Unlike Itanium, there is no character code to indicate an argument pack.
186 // Tested with MSVC 2013, the first version which supports variadic templates.
187 
variadic_fn_template(const Ts &...args)188 template <typename ...Ts> void variadic_fn_template(const Ts &...args) { }
variadic_fn_instantiate()189 void variadic_fn_instantiate() {
190   variadic_fn_template(0, 1, 3, 4);
191   variadic_fn_template(0, 1, 'a', "b");
192 }
193 // CHECK: "\01??$variadic_fn_template@HHHH@@YAXABH000@Z"
194 // CHECK: "\01??$variadic_fn_template@HHD$$BY01D@@YAXABH0ABDAAY01$$CBD@Z"
195 
196 template <typename ...Ts>
197 struct VariadicClass {
VariadicClassVariadicClass198   VariadicClass() { }
199   int x;
200 };
variadic_class_instantiate()201 void variadic_class_instantiate() {
202   VariadicClass<int, char, bool> a;
203   VariadicClass<bool, char, int> b;
204 }
205 // CHECK: call {{.*}} @"\01??0?$VariadicClass@HD_N@@QAE@XZ"
206 // CHECK: call {{.*}} @"\01??0?$VariadicClass@_NDH@@QAE@XZ"
207 
208 template <typename T>
209 struct Second {};
210 
211 template <typename T, template <class> class>
212 struct Type {};
213 
214 template <template <class> class T>
215 struct Type2 {};
216 
217 template <template <class> class T, bool B>
218 struct Thing;
219 
220 template <template <class> class T>
221 struct Thing<T, false> { };
222 
223 template <template <class> class T>
224 struct Thing<T, true> { };
225 
template_template_fun(Type<Thing<Second,true>,Second>)226 void template_template_fun(Type<Thing<Second, true>, Second>) { }
227 // CHECK: "\01?template_template_fun@@YAXU?$Type@U?$Thing@USecond@@$00@@USecond@@@@@Z"
228 
229 template <typename T>
230 void template_template_specialization();
231 
232 template <>
template_template_specialization()233 void template_template_specialization<void (Type<Thing<Second, true>, Second>)>() {
234 }
235 // CHECK: "\01??$template_template_specialization@$$A6AXU?$Type@U?$Thing@USecond@@$00@@USecond@@@@@Z@@YAXXZ"
236 
237 // PR16788
238 template <decltype(nullptr)> struct S1 {};
f(S1<nullptr>)239 void f(S1<nullptr>) {}
240 // CHECK: "\01?f@@YAXU?$S1@$0A@@@@Z"
241 
242 struct record {
243   int first;
244   int second;
245 };
246 template <const record &>
247 struct type1 {
248 };
249 extern const record inst;
recref(type1<inst>)250 void recref(type1<inst>) {}
251 // CHECK: "\01?recref@@YAXU?$type1@$E?inst@@3Urecord@@B@@@Z"
252 
253 struct _GUID {};
254 struct __declspec(uuid("{12345678-1234-1234-1234-1234567890aB}")) uuid;
255 
256 template <typename T, const _GUID *G = &__uuidof(T)>
257 struct UUIDType1 {};
258 
259 template <typename T, const _GUID &G = __uuidof(T)>
260 struct UUIDType2 {};
261 
fun(UUIDType1<uuid> a)262 void fun(UUIDType1<uuid> a) {}
263 // CHECK: "\01?fun@@YAXU?$UUIDType1@Uuuid@@$1?_GUID_12345678_1234_1234_1234_1234567890ab@@3U__s_GUID@@B@@@Z"
fun(UUIDType2<uuid> b)264 void fun(UUIDType2<uuid> b) {}
265 // CHECK: "\01?fun@@YAXU?$UUIDType2@Uuuid@@$E?_GUID_12345678_1234_1234_1234_1234567890ab@@3U__s_GUID@@B@@@Z"
266 
267 template <typename T> struct TypeWithFriendDefinition {
FunctionDefinedWithInjectedName(TypeWithFriendDefinition<T>)268   friend void FunctionDefinedWithInjectedName(TypeWithFriendDefinition<T>) {}
269 };
270 // CHECK: call {{.*}} @"\01?FunctionDefinedWithInjectedName@@YAXU?$TypeWithFriendDefinition@H@@@Z"
CallFunctionDefinedWithInjectedName()271 void CallFunctionDefinedWithInjectedName() {
272   FunctionDefinedWithInjectedName(TypeWithFriendDefinition<int>());
273 }
274 // CHECK: @"\01?FunctionDefinedWithInjectedName@@YAXU?$TypeWithFriendDefinition@H@@@Z"
275 
276 // We need to be able to feed GUIDs through a couple rounds of template
277 // substitution.
278 template <const _GUID *G>
279 struct UUIDType3 {
fooUUIDType3280   void foo() {}
281 };
282 template <const _GUID *G>
283 struct UUIDType4 : UUIDType3<G> {
barUUIDType4284   void bar() { UUIDType4::foo(); }
285 };
286 template struct UUIDType4<&__uuidof(uuid)>;
287 // CHECK: "\01?bar@?$UUIDType4@$1?_GUID_12345678_1234_1234_1234_1234567890ab@@3U__s_GUID@@B@@QAEXXZ"
288 // CHECK: "\01?foo@?$UUIDType3@$1?_GUID_12345678_1234_1234_1234_1234567890ab@@3U__s_GUID@@B@@QAEXXZ"
289