xref: /aosp_15_r20/external/clang/test/Modules/cxx-templates.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: rm -rf %t
2*67e74705SXin Li // RUN: not %clang_cc1 -x objective-c++ -fmodules -fimplicit-module-maps -fno-modules-error-recovery -fmodules-cache-path=%t -I %S/Inputs %s -std=c++14 -ast-dump-lookups 2>/dev/null | FileCheck %s --check-prefix=CHECK-GLOBAL
3*67e74705SXin Li // RUN: not %clang_cc1 -x objective-c++ -fmodules -fimplicit-module-maps -fno-modules-error-recovery -fmodules-cache-path=%t -I %S/Inputs %s -std=c++14 -ast-dump-lookups -ast-dump-filter N 2>/dev/null | FileCheck %s --check-prefix=CHECK-NAMESPACE-N
4*67e74705SXin Li // RUN: not %clang_cc1 -x objective-c++ -fmodules -fimplicit-module-maps -fno-modules-error-recovery -fmodules-cache-path=%t -I %S/Inputs %s -std=c++14 -ast-dump -ast-dump-filter SomeTemplate 2>/dev/null | FileCheck %s --check-prefix=CHECK-DUMP
5*67e74705SXin Li // RUN: %clang_cc1 -x objective-c++ -fmodules -fimplicit-module-maps -fno-modules-error-recovery -fmodules-cache-path=%t -I %S/Inputs %s -verify -std=c++14
6*67e74705SXin Li // RUN: %clang_cc1 -x objective-c++ -fmodules -fimplicit-module-maps -fno-modules-error-recovery -fmodules-cache-path=%t -I %S/Inputs %s -verify -std=c++14 -DEARLY_IMPORT
7*67e74705SXin Li 
8*67e74705SXin Li #ifdef EARLY_IMPORT
9*67e74705SXin Li #include "cxx-templates-textual.h"
10*67e74705SXin Li #endif
11*67e74705SXin Li 
12*67e74705SXin Li @import cxx_templates_a;
13*67e74705SXin Li @import cxx_templates_b;
14*67e74705SXin Li @import cxx_templates_c;
15*67e74705SXin Li @import cxx_templates_d;
16*67e74705SXin Li @import cxx_templates_common;
17*67e74705SXin Li 
18*67e74705SXin Li template<typename, char> struct Tmpl_T_C {};
19*67e74705SXin Li template<typename, int, int> struct Tmpl_T_I_I {};
20*67e74705SXin Li 
21*67e74705SXin Li template<typename A, typename B, A> struct Tmpl_T_T_A {};
22*67e74705SXin Li template<typename A, typename B, B> struct Tmpl_T_T_B {};
23*67e74705SXin Li 
24*67e74705SXin Li template<int> struct UseInt {};
25*67e74705SXin Li 
g()26*67e74705SXin Li void g() {
27*67e74705SXin Li   f(0);
28*67e74705SXin Li   f<double>(1.0);
29*67e74705SXin Li   f<int>();
30*67e74705SXin Li   f(); // expected-error {{no matching function}}
31*67e74705SXin Li   // expected-note@Inputs/cxx-templates-a.h:3 {{couldn't infer template argument}}
32*67e74705SXin Li   // expected-note@Inputs/cxx-templates-a.h:4 {{requires 1 argument}}
33*67e74705SXin Li 
34*67e74705SXin Li   N::f(0);
35*67e74705SXin Li   N::f<double>(1.0);
36*67e74705SXin Li   N::f<int>();
37*67e74705SXin Li   N::f(); // expected-error {{no matching function}}
38*67e74705SXin Li   // expected-note@Inputs/cxx-templates-b.h:6 {{couldn't infer template argument}}
39*67e74705SXin Li   // expected-note@Inputs/cxx-templates-b.h:7 {{requires single argument}}
40*67e74705SXin Li 
41*67e74705SXin Li   template_param_kinds_1<0>(); // ok, from cxx-templates-a.h
42*67e74705SXin Li   template_param_kinds_1<int>(); // ok, from cxx-templates-b.h
43*67e74705SXin Li 
44*67e74705SXin Li   template_param_kinds_2<Tmpl_T_C>(); // expected-error {{no matching function}}
45*67e74705SXin Li   // expected-note@Inputs/cxx-templates-a.h:11 {{invalid explicitly-specified argument}}
46*67e74705SXin Li   // expected-note@Inputs/cxx-templates-b.h:11 {{invalid explicitly-specified argument}}
47*67e74705SXin Li 
48*67e74705SXin Li   template_param_kinds_2<Tmpl_T_I_I>(); // expected-error {{ambiguous}}
49*67e74705SXin Li   // expected-note@Inputs/cxx-templates-a.h:11 {{candidate}}
50*67e74705SXin Li   // expected-note@Inputs/cxx-templates-b.h:11 {{candidate}}
51*67e74705SXin Li 
52*67e74705SXin Li   // FIXME: This should be valid, but we incorrectly match the template template
53*67e74705SXin Li   // argument against both template template parameters.
54*67e74705SXin Li   template_param_kinds_3<Tmpl_T_T_A>(); // expected-error {{ambiguous}}
55*67e74705SXin Li   // expected-note@Inputs/cxx-templates-a.h:12 {{candidate}}
56*67e74705SXin Li   // expected-note@Inputs/cxx-templates-b.h:12 {{candidate}}
57*67e74705SXin Li   template_param_kinds_3<Tmpl_T_T_B>(); // expected-error {{ambiguous}}
58*67e74705SXin Li   // expected-note@Inputs/cxx-templates-a.h:12 {{candidate}}
59*67e74705SXin Li   // expected-note@Inputs/cxx-templates-b.h:12 {{candidate}}
60*67e74705SXin Li 
61*67e74705SXin Li   // Trigger the instantiation of a template in 'a' that uses a type defined in
62*67e74705SXin Li   // 'common'. That type is not visible here.
63*67e74705SXin Li   PerformDelayedLookup(defined_in_common);
64*67e74705SXin Li 
65*67e74705SXin Li   // Likewise, but via a default argument.
66*67e74705SXin Li   PerformDelayedLookupInDefaultArgument(defined_in_common);
67*67e74705SXin Li 
68*67e74705SXin Li   // Trigger the instantiation of a template in 'b' that uses a type defined in
69*67e74705SXin Li   // 'b_impl'. That type is not visible here.
70*67e74705SXin Li   UseDefinedInBImpl<int>();
71*67e74705SXin Li 
72*67e74705SXin Li   // Trigger the instantiation of a template in 'a' that uses a type defined in
73*67e74705SXin Li   // 'b_impl', via a template defined in 'b'. Since the type is visible from
74*67e74705SXin Li   // within 'b', the instantiation succeeds.
75*67e74705SXin Li   UseDefinedInBImplIndirectly(defined_in_b_impl);
76*67e74705SXin Li 
77*67e74705SXin Li   // Trigger the instantiation of a template in 'a' that uses a type defined in
78*67e74705SXin Li   // 'b_impl'. That type is not visible here, nor in 'a'. This fails; there is
79*67e74705SXin Li   // no reason why DefinedInBImpl should be visible here.
80*67e74705SXin Li   //
81*67e74705SXin Li   // We turn off error recovery for modules in this test (so we don't get an
82*67e74705SXin Li   // implicit import of cxx_templates_b_impl), and that results in us producing
83*67e74705SXin Li   // a big spew of errors here.
84*67e74705SXin Li   //
85*67e74705SXin Li   // expected-error@Inputs/cxx-templates-a.h:19 {{definition of 'DefinedInBImpl' must be imported}}
86*67e74705SXin Li   // expected-note@Inputs/cxx-templates-b-impl.h:1 +{{definition is here}}
87*67e74705SXin Li   // expected-error@Inputs/cxx-templates-a.h:19 +{{}}
88*67e74705SXin Li   // expected-error@Inputs/cxx-templates-a.h:20 +{{}}
89*67e74705SXin Li   PerformDelayedLookup(defined_in_b_impl); // expected-note {{in instantiation of}}
90*67e74705SXin Li 
91*67e74705SXin Li   merge_templates_a = merge_templates_b; // ok, same type
92*67e74705SXin Li 
93*67e74705SXin Li   using T = decltype(enum_a_from_a);
94*67e74705SXin Li   using T = decltype(enum_b_from_b);
95*67e74705SXin Li   T e = true ? enum_a_from_a : enum_b_from_b;
96*67e74705SXin Li 
97*67e74705SXin Li   UseRedeclaredEnum<int>(UseInt<1>());
98*67e74705SXin Li   // FIXME: Reintroduce this once we merge function template specializations.
99*67e74705SXin Li   //static_assert(UseRedeclaredEnumA == UseRedeclaredEnumB, "");
100*67e74705SXin Li   //static_assert(UseRedeclaredEnumA == UseRedeclaredEnum<int>, "");
101*67e74705SXin Li   //static_assert(UseRedeclaredEnumB == UseRedeclaredEnum<int>, "");
102*67e74705SXin Li   static_assert(enum_c_from_a == enum_c_from_b, "");
103*67e74705SXin Li   CommonTemplate<int> cti;
104*67e74705SXin Li   CommonTemplate<int>::E eee = CommonTemplate<int>::c;
105*67e74705SXin Li 
106*67e74705SXin Li   TemplateInstantiationVisibility<char[1]> tiv1;
107*67e74705SXin Li   TemplateInstantiationVisibility<char[2]> tiv2;
108*67e74705SXin Li   TemplateInstantiationVisibility<char[3]> tiv3; // expected-error 5{{must be imported from module 'cxx_templates_b_impl'}}
109*67e74705SXin Li   // [email protected]:10 3{{explicit specialization declared here}}
110*67e74705SXin Li   // [email protected]:10 2{{previous definition is here}}
111*67e74705SXin Li   TemplateInstantiationVisibility<char[4]> tiv4;
112*67e74705SXin Li 
113*67e74705SXin Li   int &p = WithPartialSpecializationUse().f();
114*67e74705SXin Li   int &q = WithExplicitSpecializationUse().inner_template<int>();
115*67e74705SXin Li   int *r = PartiallyInstantiatePartialSpec<int*>::bar();
116*67e74705SXin Li 
117*67e74705SXin Li   (void)&WithImplicitSpecialMembers<int>::n;
118*67e74705SXin Li 
119*67e74705SXin Li   MergeClassTemplateSpecializations_string s;
120*67e74705SXin Li 
121*67e74705SXin Li   extern TestInjectedClassName::A *use_a;
122*67e74705SXin Li   extern TestInjectedClassName::C *use_c;
123*67e74705SXin Li   TestInjectedClassName::UseD();
124*67e74705SXin Li }
125*67e74705SXin Li 
126*67e74705SXin Li static_assert(Outer<int>::Inner<int>::f() == 1, "");
127*67e74705SXin Li static_assert(Outer<int>::Inner<int>::g() == 2, "");
128*67e74705SXin Li 
129*67e74705SXin Li static_assert(MergeTemplateDefinitions<int>::f() == 1, "");
130*67e74705SXin Li static_assert(MergeTemplateDefinitions<int>::g() == 2, "");
131*67e74705SXin Li 
132*67e74705SXin Li RedeclaredAsFriend<int> raf1;
133*67e74705SXin Li RedeclareTemplateAsFriend<double> rtaf;
134*67e74705SXin Li RedeclaredAsFriend<double> raf2;
135*67e74705SXin Li 
136*67e74705SXin Li MergeSpecializations<int*>::partially_specialized_in_a spec_in_a_1;
137*67e74705SXin Li MergeSpecializations<int&>::partially_specialized_in_b spec_in_b_1;
138*67e74705SXin Li MergeSpecializations<int[]>::partially_specialized_in_c spec_in_c_1;
139*67e74705SXin Li MergeSpecializations<char>::explicitly_specialized_in_a spec_in_a_2;
140*67e74705SXin Li MergeSpecializations<double>::explicitly_specialized_in_b spec_in_b_2;
141*67e74705SXin Li MergeSpecializations<bool>::explicitly_specialized_in_c spec_in_c_2;
142*67e74705SXin Li 
143*67e74705SXin Li MergeAnonUnionMember<> maum_main;
144*67e74705SXin Li typedef DontWalkPreviousDeclAfterMerging<int> dwpdam_typedef_2;
145*67e74705SXin Li dwpdam_typedef::type dwpdam_typedef_use;
146*67e74705SXin Li DontWalkPreviousDeclAfterMerging<int>::Inner::type dwpdam;
147*67e74705SXin Li 
148*67e74705SXin Li using AliasTemplateMergingTest = WithAliasTemplate<int>::X<char>;
149*67e74705SXin Li 
AnonymousDeclsMergingTest(WithAnonymousDecls<int> WAD,WithAnonymousDecls<char> WADC)150*67e74705SXin Li int AnonymousDeclsMergingTest(WithAnonymousDecls<int> WAD, WithAnonymousDecls<char> WADC) {
151*67e74705SXin Li   return InstantiateWithAnonymousDeclsA(WAD) +
152*67e74705SXin Li          InstantiateWithAnonymousDeclsB(WAD) +
153*67e74705SXin Li          InstantiateWithAnonymousDeclsB2(WADC) +
154*67e74705SXin Li          InstantiateWithAnonymousDeclsD(WADC);
155*67e74705SXin Li }
156*67e74705SXin Li 
157*67e74705SXin Li @import cxx_templates_common;
158*67e74705SXin Li 
159*67e74705SXin Li typedef SomeTemplate<int*> SomeTemplateIntPtr;
160*67e74705SXin Li typedef SomeTemplate<int&> SomeTemplateIntRef;
161*67e74705SXin Li SomeTemplate<char*> some_template_char_ptr;
162*67e74705SXin Li SomeTemplate<char&> some_template_char_ref;
163*67e74705SXin Li 
testImplicitSpecialMembers(SomeTemplate<char[1]> & a,const SomeTemplate<char[1]> & b,SomeTemplate<char[2]> & c,const SomeTemplate<char[2]> & d)164*67e74705SXin Li void testImplicitSpecialMembers(SomeTemplate<char[1]> &a,
165*67e74705SXin Li                                 const SomeTemplate<char[1]> &b,
166*67e74705SXin Li                                 SomeTemplate<char[2]> &c,
167*67e74705SXin Li                                 const SomeTemplate<char[2]> &d) {
168*67e74705SXin Li   a = b;
169*67e74705SXin Li   c = d;
170*67e74705SXin Li }
171*67e74705SXin Li 
testFriendInClassTemplate(Std::WithFriend<int> wfi)172*67e74705SXin Li bool testFriendInClassTemplate(Std::WithFriend<int> wfi) {
173*67e74705SXin Li   return wfi != wfi;
174*67e74705SXin Li }
175*67e74705SXin Li 
176*67e74705SXin Li namespace hidden_specializations {
177*67e74705SXin Li   // [email protected]:* 1+{{here}}
test()178*67e74705SXin Li   void test() {
179*67e74705SXin Li     // For functions, uses that would trigger instantiations of definitions are
180*67e74705SXin Li     // not allowed.
181*67e74705SXin Li     fn<void>(); // ok
182*67e74705SXin Li     fn<char>(); // ok
183*67e74705SXin Li     fn<int>(); // expected-error 1+{{explicit specialization of 'fn<int>' must be imported}}
184*67e74705SXin Li     cls<void>::nested_fn(); // expected-error 1+{{explicit specialization of 'nested_fn' must be imported}}
185*67e74705SXin Li     cls<void>::nested_fn_t<int>(); // expected-error 1+{{explicit specialization of 'nested_fn_t' must be imported}}
186*67e74705SXin Li     cls<void>::nested_fn_t<char>(); // expected-error 1+{{explicit specialization of 'nested_fn_t' must be imported}}
187*67e74705SXin Li 
188*67e74705SXin Li     // For classes, uses that would trigger instantiations of definitions are
189*67e74705SXin Li     // not allowed.
190*67e74705SXin Li     cls<void> *k0; // ok
191*67e74705SXin Li     cls<char> *k1; // ok
192*67e74705SXin Li     cls<int> *k2; // ok
193*67e74705SXin Li     cls<int*> *k3; // ok
194*67e74705SXin Li     cls<void>::nested_cls *nk1; // ok
195*67e74705SXin Li     cls<void>::nested_cls_t<int> *nk2; // ok
196*67e74705SXin Li     cls<void>::nested_cls_t<char> *nk3; // ok
197*67e74705SXin Li     cls<int> uk1; // expected-error 1+{{explicit specialization of 'cls<int>' must be imported}} expected-error 1+{{definition of}}
198*67e74705SXin Li     cls<int*> uk3; // expected-error 1+{{partial specialization of 'cls<type-parameter-0-0 *>' must be imported}} expected-error 1+{{definition of}}
199*67e74705SXin Li     cls<char*> uk4; // expected-error 1+{{partial specialization of 'cls<type-parameter-0-0 *>' must be imported}} expected-error 1+{{definition of}}
200*67e74705SXin Li     cls<void>::nested_cls unk1; // expected-error 1+{{explicit specialization of 'nested_cls' must be imported}} expected-error 1+{{definition of}}
201*67e74705SXin Li     cls<void>::nested_cls_t<int> unk2; // expected-error 1+{{explicit specialization of 'nested_cls_t' must be imported}} expected-error 1+{{definition of}}
202*67e74705SXin Li     cls<void>::nested_cls_t<char> unk3; // expected-error 1+{{explicit specialization of 'nested_cls_t' must be imported}}
203*67e74705SXin Li 
204*67e74705SXin Li     // For enums, uses that would trigger instantiations of definitions are not
205*67e74705SXin Li     // allowed.
206*67e74705SXin Li     cls<void>::nested_enum e; // ok
207*67e74705SXin Li     (void)cls<void>::nested_enum::e; // expected-error 1+{{definition of 'nested_enum' must be imported}} expected-error 1+{{declaration of 'e'}}
208*67e74705SXin Li 
209*67e74705SXin Li     // For variable template specializations, no uses are allowed because
210*67e74705SXin Li     // specializations can change the type.
211*67e74705SXin Li     (void)sizeof(var<void>); // ok
212*67e74705SXin Li     (void)sizeof(var<char>); // ok
213*67e74705SXin Li     (void)sizeof(var<int>); // expected-error 1+{{explicit specialization of 'var<int>' must be imported}}
214*67e74705SXin Li     (void)sizeof(var<int*>); // expected-error 1+{{partial specialization of 'var<type-parameter-0-0 *>' must be imported}}
215*67e74705SXin Li     (void)sizeof(var<char*>); // expected-error 1+{{partial specialization of 'var<type-parameter-0-0 *>' must be imported}}
216*67e74705SXin Li     (void)sizeof(cls<void>::nested_var); // ok
217*67e74705SXin Li     (void)cls<void>::nested_var; // expected-error 1+{{explicit specialization of 'nested_var' must be imported}}
218*67e74705SXin Li     (void)sizeof(cls<void>::nested_var_t<int>); // expected-error 1+{{explicit specialization of 'nested_var_t' must be imported}}
219*67e74705SXin Li     (void)sizeof(cls<void>::nested_var_t<char>); // expected-error 1+{{explicit specialization of 'nested_var_t' must be imported}}
220*67e74705SXin Li   }
221*67e74705SXin Li 
nested_fn()222*67e74705SXin Li   void cls<int>::nested_fn() {} // expected-error 1+{{explicit specialization of 'cls<int>' must be imported}} expected-error 1+{{definition of}}
223*67e74705SXin Li   struct cls<int>::nested_cls {}; // expected-error 1+{{explicit specialization of 'cls<int>' must be imported}} expected-error 1+{{definition of}}
224*67e74705SXin Li   int cls<int>::nested_var; // expected-error 1+{{explicit specialization of 'cls<int>' must be imported}} expected-error 1+{{definition of}}
225*67e74705SXin Li   enum cls<int>::nested_enum : int {}; // expected-error 1+{{explicit specialization of 'cls<int>' must be imported}} expected-error 1+{{definition of}}
226*67e74705SXin Li 
nested_fn()227*67e74705SXin Li   template<typename T> void cls<T*>::nested_fn() {} // expected-error 1+{{partial specialization of 'cls<type-parameter-0-0 *>' must be imported}}
228*67e74705SXin Li   template<typename T> struct cls<T*>::nested_cls {}; // expected-error 1+{{partial specialization of 'cls<type-parameter-0-0 *>' must be imported}}
229*67e74705SXin Li   template<typename T> int cls<T*>::nested_var; // expected-error 1+{{partial specialization of 'cls<type-parameter-0-0 *>' must be imported}}
230*67e74705SXin Li   template<typename T> enum cls<T*>::nested_enum : int {}; // expected-error 1+{{partial specialization of 'cls<type-parameter-0-0 *>' must be imported}}
231*67e74705SXin Li }
232*67e74705SXin Li 
233*67e74705SXin Li namespace Std {
234*67e74705SXin Li   void g(); // expected-error {{functions that differ only in their return type cannot be overloaded}}
235*67e74705SXin Li   // [email protected]:21 {{previous}}
236*67e74705SXin Li }
237*67e74705SXin Li 
238*67e74705SXin Li // CHECK-GLOBAL:      DeclarationName 'f'
239*67e74705SXin Li // CHECK-GLOBAL-NEXT: |-FunctionTemplate {{.*}} 'f'
240*67e74705SXin Li // CHECK-GLOBAL-NEXT: |-FunctionTemplate {{.*}} 'f'
241*67e74705SXin Li // CHECK-GLOBAL-NEXT: |-FunctionTemplate {{.*}} 'f'
242*67e74705SXin Li // CHECK-GLOBAL-NEXT: `-FunctionTemplate {{.*}} 'f'
243*67e74705SXin Li 
244*67e74705SXin Li // CHECK-NAMESPACE-N:      DeclarationName 'f'
245*67e74705SXin Li // CHECK-NAMESPACE-N-NEXT: |-FunctionTemplate {{.*}} 'f'
246*67e74705SXin Li // CHECK-NAMESPACE-N-NEXT: |-FunctionTemplate {{.*}} 'f'
247*67e74705SXin Li // CHECK-NAMESPACE-N-NEXT: |-FunctionTemplate {{.*}} 'f'
248*67e74705SXin Li // CHECK-NAMESPACE-N-NEXT: `-FunctionTemplate {{.*}} 'f'
249*67e74705SXin Li 
250*67e74705SXin Li // CHECK-DUMP:      ClassTemplateDecl {{.*}} <{{.*[/\\]}}cxx-templates-common.h:1:1, {{.*}}>  col:{{.*}} in cxx_templates_common SomeTemplate
251*67e74705SXin Li // CHECK-DUMP:        ClassTemplateSpecializationDecl {{.*}} prev {{.*}} SomeTemplate
252*67e74705SXin Li // CHECK-DUMP-NEXT:     TemplateArgument type 'char [1]'
253*67e74705SXin Li // CHECK-DUMP:        ClassTemplateSpecializationDecl {{.*}} SomeTemplate definition
254*67e74705SXin Li // CHECK-DUMP-NEXT:     TemplateArgument type 'char [1]'
255*67e74705SXin Li // CHECK-DUMP:        ClassTemplateSpecializationDecl {{.*}} prev {{.*}} SomeTemplate
256*67e74705SXin Li // CHECK-DUMP-NEXT:     TemplateArgument type 'char [2]'
257*67e74705SXin Li // CHECK-DUMP:        ClassTemplateSpecializationDecl {{.*}} SomeTemplate definition
258*67e74705SXin Li // CHECK-DUMP-NEXT:     TemplateArgument type 'char [2]'
259