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