xref: /minix3/external/bsd/llvm/dist/clang/test/Modules/cxx-templates.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc // RUN: rm -rf %t
2*0a6a1f1dSLionel Sambuc // RUN: not %clang_cc1 -x objective-c++ -fmodules -fno-modules-error-recovery -fmodules-cache-path=%t -I %S/Inputs %s -std=c++11 -ast-dump-lookups | FileCheck %s --check-prefix=CHECK-GLOBAL
3*0a6a1f1dSLionel Sambuc // RUN: not %clang_cc1 -x objective-c++ -fmodules -fno-modules-error-recovery -fmodules-cache-path=%t -I %S/Inputs %s -std=c++11 -ast-dump-lookups -ast-dump-filter N | FileCheck %s --check-prefix=CHECK-NAMESPACE-N
4*0a6a1f1dSLionel Sambuc // RUN: not %clang_cc1 -x objective-c++ -fmodules -fno-modules-error-recovery -fmodules-cache-path=%t -I %S/Inputs %s -std=c++11 -ast-dump | FileCheck %s --check-prefix=CHECK-DUMP
5*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -x objective-c++ -fmodules -fno-modules-error-recovery -fmodules-cache-path=%t -I %S/Inputs %s -verify -std=c++11
6*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -x objective-c++ -fmodules -fno-modules-error-recovery -fmodules-cache-path=%t -I %S/Inputs %s -verify -std=c++11 -DEARLY_IMPORT
7*0a6a1f1dSLionel Sambuc 
8*0a6a1f1dSLionel Sambuc #ifdef EARLY_IMPORT
9*0a6a1f1dSLionel Sambuc #include "cxx-templates-textual.h"
10*0a6a1f1dSLionel Sambuc #endif
11f4a2713aSLionel Sambuc 
12f4a2713aSLionel Sambuc @import cxx_templates_a;
13f4a2713aSLionel Sambuc @import cxx_templates_b;
14f4a2713aSLionel Sambuc @import cxx_templates_c;
15*0a6a1f1dSLionel Sambuc @import cxx_templates_d;
16f4a2713aSLionel Sambuc @import cxx_templates_common;
17f4a2713aSLionel Sambuc 
18f4a2713aSLionel Sambuc template<typename, char> struct Tmpl_T_C {};
19f4a2713aSLionel Sambuc template<typename, int, int> struct Tmpl_T_I_I {};
20f4a2713aSLionel Sambuc 
21f4a2713aSLionel Sambuc template<typename A, typename B, A> struct Tmpl_T_T_A {};
22f4a2713aSLionel Sambuc template<typename A, typename B, B> struct Tmpl_T_T_B {};
23f4a2713aSLionel Sambuc 
24f4a2713aSLionel Sambuc template<int> struct UseInt {};
25f4a2713aSLionel Sambuc 
g()26f4a2713aSLionel Sambuc void g() {
27f4a2713aSLionel Sambuc   f(0);
28f4a2713aSLionel Sambuc   f<double>(1.0);
29f4a2713aSLionel Sambuc   f<int>();
30f4a2713aSLionel Sambuc   f(); // expected-error {{no matching function}}
31*0a6a1f1dSLionel Sambuc #ifdef EARLY_IMPORT
32*0a6a1f1dSLionel Sambuc   // FIXME: The textual inclusion above shouldn't affect this!
33*0a6a1f1dSLionel Sambuc   // expected-note@Inputs/cxx-templates-a.h:3 {{couldn't infer template argument}}
34*0a6a1f1dSLionel Sambuc   // expected-note@Inputs/cxx-templates-a.h:4 {{requires 1 argument}}
35*0a6a1f1dSLionel Sambuc #else
36f4a2713aSLionel Sambuc   // expected-note@Inputs/cxx-templates-b.h:3 {{couldn't infer template argument}}
37f4a2713aSLionel Sambuc   // expected-note@Inputs/cxx-templates-b.h:4 {{requires single argument}}
38*0a6a1f1dSLionel Sambuc #endif
39f4a2713aSLionel Sambuc 
40f4a2713aSLionel Sambuc   N::f(0);
41f4a2713aSLionel Sambuc   N::f<double>(1.0);
42f4a2713aSLionel Sambuc   N::f<int>();
43f4a2713aSLionel Sambuc   N::f(); // expected-error {{no matching function}}
44f4a2713aSLionel Sambuc   // expected-note@Inputs/cxx-templates-b.h:6 {{couldn't infer template argument}}
45*0a6a1f1dSLionel Sambuc   // expected-note@Inputs/cxx-templates-b.h:7 {{requires single argument}}
46f4a2713aSLionel Sambuc 
47f4a2713aSLionel Sambuc   template_param_kinds_1<0>(); // ok, from cxx-templates-a.h
48f4a2713aSLionel Sambuc   template_param_kinds_1<int>(); // ok, from cxx-templates-b.h
49f4a2713aSLionel Sambuc 
50f4a2713aSLionel Sambuc   template_param_kinds_2<Tmpl_T_C>(); // expected-error {{no matching function}}
51f4a2713aSLionel Sambuc   // expected-note@Inputs/cxx-templates-a.h:11 {{invalid explicitly-specified argument}}
52f4a2713aSLionel Sambuc   // expected-note@Inputs/cxx-templates-b.h:11 {{invalid explicitly-specified argument}}
53f4a2713aSLionel Sambuc 
54f4a2713aSLionel Sambuc   template_param_kinds_2<Tmpl_T_I_I>(); // expected-error {{ambiguous}}
55f4a2713aSLionel Sambuc   // expected-note@Inputs/cxx-templates-a.h:11 {{candidate}}
56f4a2713aSLionel Sambuc   // expected-note@Inputs/cxx-templates-b.h:11 {{candidate}}
57f4a2713aSLionel Sambuc 
58f4a2713aSLionel Sambuc   // FIXME: This should be valid, but we incorrectly match the template template
59f4a2713aSLionel Sambuc   // argument against both template template parameters.
60f4a2713aSLionel Sambuc   template_param_kinds_3<Tmpl_T_T_A>(); // expected-error {{ambiguous}}
61f4a2713aSLionel Sambuc   // expected-note@Inputs/cxx-templates-a.h:12 {{candidate}}
62f4a2713aSLionel Sambuc   // expected-note@Inputs/cxx-templates-b.h:12 {{candidate}}
63f4a2713aSLionel Sambuc   template_param_kinds_3<Tmpl_T_T_B>(); // expected-error {{ambiguous}}
64f4a2713aSLionel Sambuc   // expected-note@Inputs/cxx-templates-a.h:12 {{candidate}}
65f4a2713aSLionel Sambuc   // expected-note@Inputs/cxx-templates-b.h:12 {{candidate}}
66f4a2713aSLionel Sambuc 
67f4a2713aSLionel Sambuc   // Trigger the instantiation of a template in 'a' that uses a type defined in
68f4a2713aSLionel Sambuc   // 'common'. That type is not visible here.
69f4a2713aSLionel Sambuc   PerformDelayedLookup(defined_in_common);
70f4a2713aSLionel Sambuc 
71f4a2713aSLionel Sambuc   // Likewise, but via a default argument.
72f4a2713aSLionel Sambuc   PerformDelayedLookupInDefaultArgument(defined_in_common);
73f4a2713aSLionel Sambuc 
74f4a2713aSLionel Sambuc   // Trigger the instantiation of a template in 'b' that uses a type defined in
75f4a2713aSLionel Sambuc   // 'b_impl'. That type is not visible here.
76f4a2713aSLionel Sambuc   UseDefinedInBImpl<int>();
77f4a2713aSLionel Sambuc 
78f4a2713aSLionel Sambuc   // Trigger the instantiation of a template in 'a' that uses a type defined in
79f4a2713aSLionel Sambuc   // 'b_impl', via a template defined in 'b'. Since the type is visible from
80f4a2713aSLionel Sambuc   // within 'b', the instantiation succeeds.
81f4a2713aSLionel Sambuc   UseDefinedInBImplIndirectly(defined_in_b_impl);
82f4a2713aSLionel Sambuc 
83f4a2713aSLionel Sambuc   // Trigger the instantiation of a template in 'a' that uses a type defined in
84f4a2713aSLionel Sambuc   // 'b_impl'. That type is not visible here, nor in 'a'. This fails; there is
85f4a2713aSLionel Sambuc   // no reason why DefinedInBImpl should be visible here.
86*0a6a1f1dSLionel Sambuc   //
87*0a6a1f1dSLionel Sambuc   // We turn off error recovery for modules in this test (so we don't get an
88*0a6a1f1dSLionel Sambuc   // implicit import of cxx_templates_b_impl), and that results in us producing
89*0a6a1f1dSLionel Sambuc   // a big spew of errors here.
90*0a6a1f1dSLionel Sambuc   //
91f4a2713aSLionel Sambuc   // expected-error@Inputs/cxx-templates-a.h:19 {{definition of 'DefinedInBImpl' must be imported}}
92*0a6a1f1dSLionel Sambuc   // expected-note@Inputs/cxx-templates-b-impl.h:1 +{{definition is here}}
93*0a6a1f1dSLionel Sambuc   // expected-error@Inputs/cxx-templates-a.h:19 +{{}}
94*0a6a1f1dSLionel Sambuc   // expected-error@Inputs/cxx-templates-a.h:20 +{{}}
95f4a2713aSLionel Sambuc   PerformDelayedLookup(defined_in_b_impl); // expected-note {{in instantiation of}}
96f4a2713aSLionel Sambuc 
97f4a2713aSLionel Sambuc   merge_templates_a = merge_templates_b; // ok, same type
98f4a2713aSLionel Sambuc 
99f4a2713aSLionel Sambuc   using T = decltype(enum_a_from_a);
100f4a2713aSLionel Sambuc   using T = decltype(enum_b_from_b);
101f4a2713aSLionel Sambuc   T e = true ? enum_a_from_a : enum_b_from_b;
102f4a2713aSLionel Sambuc 
103f4a2713aSLionel Sambuc   UseRedeclaredEnum<int>(UseInt<1>());
104f4a2713aSLionel Sambuc   // FIXME: Reintroduce this once we merge function template specializations.
105f4a2713aSLionel Sambuc   //static_assert(UseRedeclaredEnumA == UseRedeclaredEnumB, "");
106f4a2713aSLionel Sambuc   //static_assert(UseRedeclaredEnumA == UseRedeclaredEnum<int>, "");
107f4a2713aSLionel Sambuc   //static_assert(UseRedeclaredEnumB == UseRedeclaredEnum<int>, "");
108f4a2713aSLionel Sambuc   static_assert(enum_c_from_a == enum_c_from_b, "");
109f4a2713aSLionel Sambuc   CommonTemplate<int> cti;
110f4a2713aSLionel Sambuc   CommonTemplate<int>::E eee = CommonTemplate<int>::c;
111*0a6a1f1dSLionel Sambuc 
112*0a6a1f1dSLionel Sambuc   TemplateInstantiationVisibility<char[1]> tiv1;
113*0a6a1f1dSLionel Sambuc   TemplateInstantiationVisibility<char[2]> tiv2;
114*0a6a1f1dSLionel Sambuc   TemplateInstantiationVisibility<char[3]> tiv3; // expected-error {{must be imported from module 'cxx_templates_b_impl'}}
115*0a6a1f1dSLionel Sambuc   // expected-note@cxx-templates-b-impl.h:10 {{previous definition is here}}
116*0a6a1f1dSLionel Sambuc   TemplateInstantiationVisibility<char[4]> tiv4;
117*0a6a1f1dSLionel Sambuc 
118*0a6a1f1dSLionel Sambuc   int &p = WithPartialSpecializationUse().f();
119*0a6a1f1dSLionel Sambuc   int &q = WithExplicitSpecializationUse().inner_template<int>();
120*0a6a1f1dSLionel Sambuc   int *r = PartiallyInstantiatePartialSpec<int*>::bar();
121*0a6a1f1dSLionel Sambuc 
122*0a6a1f1dSLionel Sambuc   (void)&WithImplicitSpecialMembers<int>::n;
123*0a6a1f1dSLionel Sambuc 
124*0a6a1f1dSLionel Sambuc   MergeClassTemplateSpecializations_string s;
125*0a6a1f1dSLionel Sambuc 
126*0a6a1f1dSLionel Sambuc   extern TestInjectedClassName::A *use_a;
127*0a6a1f1dSLionel Sambuc   extern TestInjectedClassName::C *use_c;
128*0a6a1f1dSLionel Sambuc   TestInjectedClassName::UseD();
129f4a2713aSLionel Sambuc }
130f4a2713aSLionel Sambuc 
131*0a6a1f1dSLionel Sambuc static_assert(Outer<int>::Inner<int>::f() == 1, "");
132*0a6a1f1dSLionel Sambuc static_assert(Outer<int>::Inner<int>::g() == 2, "");
133*0a6a1f1dSLionel Sambuc 
134*0a6a1f1dSLionel Sambuc #ifndef EARLY_IMPORT
135*0a6a1f1dSLionel Sambuc // FIXME: The textual inclusion above shouldn't cause this to fail!
136*0a6a1f1dSLionel Sambuc static_assert(MergeTemplateDefinitions<int>::f() == 1, "");
137*0a6a1f1dSLionel Sambuc static_assert(MergeTemplateDefinitions<int>::g() == 2, "");
138*0a6a1f1dSLionel Sambuc 
139f4a2713aSLionel Sambuc RedeclaredAsFriend<int> raf1;
140f4a2713aSLionel Sambuc RedeclareTemplateAsFriend<double> rtaf;
141f4a2713aSLionel Sambuc RedeclaredAsFriend<double> raf2;
142f4a2713aSLionel Sambuc 
143f4a2713aSLionel Sambuc MergeSpecializations<int*>::partially_specialized_in_a spec_in_a_1;
144f4a2713aSLionel Sambuc MergeSpecializations<int&>::partially_specialized_in_b spec_in_b_1;
145f4a2713aSLionel Sambuc MergeSpecializations<int[]>::partially_specialized_in_c spec_in_c_1;
146f4a2713aSLionel Sambuc MergeSpecializations<char>::explicitly_specialized_in_a spec_in_a_2;
147f4a2713aSLionel Sambuc MergeSpecializations<double>::explicitly_specialized_in_b spec_in_b_2;
148f4a2713aSLionel Sambuc MergeSpecializations<bool>::explicitly_specialized_in_c spec_in_c_2;
149*0a6a1f1dSLionel Sambuc #endif
150*0a6a1f1dSLionel Sambuc 
151*0a6a1f1dSLionel Sambuc MergeAnonUnionMember<> maum_main;
152*0a6a1f1dSLionel Sambuc typedef DontWalkPreviousDeclAfterMerging<int> dwpdam_typedef_2;
153*0a6a1f1dSLionel Sambuc dwpdam_typedef::type dwpdam_typedef_use;
154*0a6a1f1dSLionel Sambuc DontWalkPreviousDeclAfterMerging<int>::Inner::type dwpdam;
155*0a6a1f1dSLionel Sambuc 
156*0a6a1f1dSLionel Sambuc using AliasTemplateMergingTest = WithAliasTemplate<int>::X<char>;
157*0a6a1f1dSLionel Sambuc 
AnonymousDeclsMergingTest(WithAnonymousDecls<int> WAD,WithAnonymousDecls<char> WADC)158*0a6a1f1dSLionel Sambuc int AnonymousDeclsMergingTest(WithAnonymousDecls<int> WAD, WithAnonymousDecls<char> WADC) {
159*0a6a1f1dSLionel Sambuc   return InstantiateWithAnonymousDeclsA(WAD) +
160*0a6a1f1dSLionel Sambuc          InstantiateWithAnonymousDeclsB(WAD) +
161*0a6a1f1dSLionel Sambuc          InstantiateWithAnonymousDeclsB2(WADC) +
162*0a6a1f1dSLionel Sambuc          InstantiateWithAnonymousDeclsD(WADC);
163*0a6a1f1dSLionel Sambuc }
164f4a2713aSLionel Sambuc 
165f4a2713aSLionel Sambuc @import cxx_templates_common;
166f4a2713aSLionel Sambuc 
167f4a2713aSLionel Sambuc typedef SomeTemplate<int*> SomeTemplateIntPtr;
168f4a2713aSLionel Sambuc typedef SomeTemplate<int&> SomeTemplateIntRef;
169f4a2713aSLionel Sambuc SomeTemplate<char*> some_template_char_ptr;
170f4a2713aSLionel Sambuc SomeTemplate<char&> some_template_char_ref;
171f4a2713aSLionel Sambuc 
testImplicitSpecialMembers(SomeTemplate<char[1]> & a,const SomeTemplate<char[1]> & b,SomeTemplate<char[2]> & c,const SomeTemplate<char[2]> & d)172f4a2713aSLionel Sambuc void testImplicitSpecialMembers(SomeTemplate<char[1]> &a,
173f4a2713aSLionel Sambuc                                 const SomeTemplate<char[1]> &b,
174f4a2713aSLionel Sambuc                                 SomeTemplate<char[2]> &c,
175f4a2713aSLionel Sambuc                                 const SomeTemplate<char[2]> &d) {
176f4a2713aSLionel Sambuc   a = b;
177f4a2713aSLionel Sambuc   c = d;
178f4a2713aSLionel Sambuc }
179f4a2713aSLionel Sambuc 
testFriendInClassTemplate(Std::WithFriend<int> wfi)180*0a6a1f1dSLionel Sambuc bool testFriendInClassTemplate(Std::WithFriend<int> wfi) {
181*0a6a1f1dSLionel Sambuc   return wfi != wfi;
182*0a6a1f1dSLionel Sambuc }
183*0a6a1f1dSLionel Sambuc 
184*0a6a1f1dSLionel Sambuc namespace Std {
185*0a6a1f1dSLionel Sambuc   void g(); // expected-error {{functions that differ only in their return type cannot be overloaded}}
186*0a6a1f1dSLionel Sambuc   // expected-note@cxx-templates-common.h:21 {{previous}}
187*0a6a1f1dSLionel Sambuc }
188*0a6a1f1dSLionel Sambuc 
189f4a2713aSLionel Sambuc // CHECK-GLOBAL:      DeclarationName 'f'
190f4a2713aSLionel Sambuc // CHECK-GLOBAL-NEXT: |-FunctionTemplate {{.*}} 'f'
191f4a2713aSLionel Sambuc // CHECK-GLOBAL-NEXT: `-FunctionTemplate {{.*}} 'f'
192f4a2713aSLionel Sambuc 
193f4a2713aSLionel Sambuc // CHECK-NAMESPACE-N:      DeclarationName 'f'
194f4a2713aSLionel Sambuc // CHECK-NAMESPACE-N-NEXT: |-FunctionTemplate {{.*}} 'f'
195f4a2713aSLionel Sambuc // CHECK-NAMESPACE-N-NEXT: `-FunctionTemplate {{.*}} 'f'
196*0a6a1f1dSLionel Sambuc 
197*0a6a1f1dSLionel Sambuc // CHECK-DUMP:      ClassTemplateDecl {{.*}} <{{.*[/\\]}}cxx-templates-common.h:1:1, {{.*}}>  col:{{.*}} in cxx_templates_common SomeTemplate
198*0a6a1f1dSLionel Sambuc // CHECK-DUMP:        ClassTemplateSpecializationDecl {{.*}} prev [[CHAR2:[^ ]*]] {{.*}} SomeTemplate
199*0a6a1f1dSLionel Sambuc // CHECK-DUMP-NEXT:     TemplateArgument type 'char [2]'
200*0a6a1f1dSLionel Sambuc // CHECK-DUMP:        ClassTemplateSpecializationDecl [[CHAR2]] {{.*}} SomeTemplate definition
201*0a6a1f1dSLionel Sambuc // CHECK-DUMP-NEXT:     TemplateArgument type 'char [2]'
202*0a6a1f1dSLionel Sambuc // CHECK-DUMP:        ClassTemplateSpecializationDecl {{.*}} prev [[CHAR1:[^ ]*]] {{.*}} SomeTemplate
203*0a6a1f1dSLionel Sambuc // CHECK-DUMP-NEXT:     TemplateArgument type 'char [1]'
204*0a6a1f1dSLionel Sambuc // CHECK-DUMP:        ClassTemplateSpecializationDecl [[CHAR1]] {{.*}} SomeTemplate definition
205*0a6a1f1dSLionel Sambuc // CHECK-DUMP-NEXT:     TemplateArgument type 'char [1]'
206