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