Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / SemaTemplate / deduction-guide.cpp
blob16c7083df29d0c81275bea5221a6db906911a69f
1 // RUN: %clang_cc1 -std=c++2a -verify -ast-dump -ast-dump-decl-types -ast-dump-filter "deduction guide" %s | FileCheck %s --strict-whitespace
3 template<auto ...> struct X {};
4 template<template<typename X, X> typename> struct Y {};
5 template<typename ...> struct Z {};
7 template<typename T, typename ...Ts> struct A {
8 template<Ts ...Ns, T *...Ps> A(X<Ps...>, Ts (*...qs)[Ns]);
9 };
10 int arr1[3], arr2[3];
11 short arr3[4];
12 A a(X<&arr1, &arr2>{}, &arr1, &arr2, &arr3);
13 using AT = decltype(a);
14 using AT = A<int[3], int, int, short>;
16 // CHECK-LABEL: Dumping <deduction guide for A>:
17 // CHECK: FunctionTemplateDecl
18 // CHECK: |-TemplateTypeParmDecl {{.*}} typename depth 0 index 0 T
19 // CHECK: |-TemplateTypeParmDecl {{.*}} typename depth 0 index 1 ... Ts
20 // CHECK: |-NonTypeTemplateParmDecl {{.*}} 'Ts...' depth 0 index 2 ... Ns
21 // CHECK: |-NonTypeTemplateParmDecl {{.*}} 'T *' depth 0 index 3 ... Ps
22 // CHECK: |-CXXDeductionGuideDecl
23 // CHECK: | |-ParmVarDecl {{.*}} 'X<Ps...>'
24 // CHECK: | `-ParmVarDecl {{.*}} 'Ts (*)[Ns]...' pack
25 // CHECK: `-CXXDeductionGuideDecl
26 // CHECK: |-TemplateArgument type 'int[3]'
27 // CHECK: |-TemplateArgument pack
28 // CHECK: | |-TemplateArgument type 'int'
29 // CHECK: | |-TemplateArgument type 'int'
30 // CHECK: | `-TemplateArgument type 'short'
31 // CHECK: |-TemplateArgument pack
32 // CHECK: | |-TemplateArgument integral 3
33 // CHECK: | |-TemplateArgument integral 3
34 // CHECK: | `-TemplateArgument integral 4
35 // CHECK: |-TemplateArgument pack
36 // CHECK: | |-TemplateArgument decl
37 // CHECK: | | `-Var {{.*}} 'arr1' 'int[3]'
38 // CHECK: | `-TemplateArgument decl
39 // CHECK: | `-Var {{.*}} 'arr2' 'int[3]'
40 // CHECK: |-ParmVarDecl {{.*}} 'X<&arr1, &arr2>'
41 // CHECK: |-ParmVarDecl {{.*}} 'int (*)[3]'
42 // CHECK: |-ParmVarDecl {{.*}} 'int (*)[3]'
43 // CHECK: `-ParmVarDecl {{.*}} 'short (*)[4]'
44 // CHECK: FunctionProtoType {{.*}} 'auto (X<Ps...>, Ts (*)[Ns]...) -> A<T, Ts...>' dependent trailing_return
45 // CHECK: |-InjectedClassNameType {{.*}} 'A<T, Ts...>' dependent
46 // CHECK: |-ElaboratedType {{.*}} 'X<Ps...>' sugar dependent
47 // CHECK: | `-TemplateSpecializationType {{.*}} 'X<Ps...>' dependent X
48 // CHECK: | `-TemplateArgument expr
49 // CHECK: | `-PackExpansionExpr {{.*}} 'T *'
50 // CHECK: | `-DeclRefExpr {{.*}} 'T *' NonTypeTemplateParm {{.*}} 'Ps' 'T *'
51 // CHECK: `-PackExpansionType {{.*}} 'Ts (*)[Ns]...' dependent
52 // CHECK: `-PointerType {{.*}} 'Ts (*)[Ns]' dependent contains_unexpanded_pack
53 // CHECK: `-ParenType {{.*}} 'Ts[Ns]' sugar dependent contains_unexpanded_pack
54 // CHECK: `-DependentSizedArrayType {{.*}} 'Ts[Ns]' dependent contains_unexpanded_pack
55 // CHECK: |-TemplateTypeParmType {{.*}} 'Ts' dependent contains_unexpanded_pack depth 0 index 1 pack
56 // CHECK: | `-TemplateTypeParm {{.*}} 'Ts'
57 // CHECK: `-DeclRefExpr {{.*}} 'Ts' NonTypeTemplateParm {{.*}} 'Ns' 'Ts...'
59 template<typename T, T V> struct B {
60 template<typename U, U W> B(X<W, V>);
62 B b(X<nullptr, 'x'>{});
63 using BT = decltype(b);
64 using BT = B<char, 'x'>;
66 // CHECK-LABEL: Dumping <deduction guide for B>:
67 // CHECK: FunctionTemplateDecl
68 // CHECK: |-TemplateTypeParmDecl {{.*}} typename depth 0 index 0 T
69 // CHECK: |-NonTypeTemplateParmDecl {{.*}} 'T' depth 0 index 1 V
70 // CHECK: |-TemplateTypeParmDecl {{.*}} typename depth 0 index 2 U
71 // CHECK: |-NonTypeTemplateParmDecl {{.*}} 'type-parameter-0-2' depth 0 index 3 W
72 // CHECK: |-CXXDeductionGuideDecl {{.*}} 'auto (X<W, V>) -> B<T, V>'
73 // CHECK: | `-ParmVarDecl {{.*}} 'X<W, V>'
74 // CHECK: `-CXXDeductionGuideDecl {{.*}} 'auto (X<nullptr, 'x'>) -> B<char, 'x'>'
75 // CHECK: |-TemplateArgument type 'char'
76 // CHECK: |-TemplateArgument integral 120
77 // CHECK: |-TemplateArgument type 'std::nullptr_t'
78 // CHECK: |-TemplateArgument nullptr
79 // CHECK: `-ParmVarDecl {{.*}} 'X<nullptr, 'x'>'
80 // CHECK: FunctionProtoType {{.*}} 'auto (X<W, V>) -> B<T, V>' dependent trailing_return
81 // CHECK: |-InjectedClassNameType {{.*}} 'B<T, V>' dependent
82 // CHECK: `-TemplateSpecializationType {{.*}} 'X<W, V>' dependent X
83 // CHECK: |-TemplateArgument expr
84 // CHECK: | `-DeclRefExpr {{.*}} 'type-parameter-0-2' NonTypeTemplateParm {{.*}} 'W' 'type-parameter-0-2'
85 // CHECK: `-TemplateArgument expr
86 // CHECK: `-DeclRefExpr {{.*}} 'T' NonTypeTemplateParm {{.*}} 'V' 'T'
88 template<typename A> struct C {
89 template<template<typename X, X> typename T, typename U, U V = 0> C(A, Y<T>, U);
91 C c(1, Y<B>{}, 2);
92 using CT = decltype(c);
93 using CT = C<int>;
95 // CHECK-LABEL: Dumping <deduction guide for C>:
96 // CHECK: FunctionTemplateDecl
97 // CHECK: |-TemplateTypeParmDecl {{.*}} typename depth 0 index 0 A
98 // CHECK: |-TemplateTemplateParmDecl {{.*}} depth 0 index 1 T
99 // CHECK: | |-TemplateTypeParmDecl {{.*}} typename depth 1 index 0 X
100 // CHECK: | `-NonTypeTemplateParmDecl {{.*}} 'X' depth 1 index 1
101 // CHECK: |-TemplateTypeParmDecl {{.*}} typename depth 0 index 2 U
102 // CHECK: |-NonTypeTemplateParmDecl {{.*}} 'type-parameter-0-2' depth 0 index 3 V
103 // CHECK: | `-TemplateArgument expr
104 // CHECK: | `-IntegerLiteral {{.*}} 'int' 0
105 // CHECK: |-CXXDeductionGuideDecl {{.*}} 'auto (A, Y<>, type-parameter-0-2) -> C<A>'
106 // CHECK: | |-ParmVarDecl {{.*}} 'A'
107 // CHECK: | |-ParmVarDecl {{.*}} 'Y<>'
108 // CHECK: | `-ParmVarDecl {{.*}} 'type-parameter-0-2'
109 // CHECK: `-CXXDeductionGuideDecl {{.*}} 'auto (int, Y<B>, int) -> C<int>'
110 // CHECK: |-TemplateArgument type 'int'
111 // CHECK: |-TemplateArgument template B
112 // CHECK: |-TemplateArgument type 'int'
113 // CHECK: |-TemplateArgument integral 0
114 // CHECK: |-ParmVarDecl {{.*}} 'int'
115 // CHECK: |-ParmVarDecl {{.*}} 'Y<B>'
116 // CHECK: `-ParmVarDecl {{.*}} 'int'
117 // CHECK: FunctionProtoType {{.*}} 'auto (A, Y<>, type-parameter-0-2) -> C<A>' dependent trailing_return cdecl
118 // CHECK: |-InjectedClassNameType {{.*}} 'C<A>' dependent
119 // CHECK: |-TemplateTypeParmType {{.*}} 'A' dependent depth 0 index 0
120 // CHECK: | `-TemplateTypeParm {{.*}} 'A'
121 // CHECK: |-ElaboratedType {{.*}} 'Y<>' sugar dependent
122 // CHECK: | `-TemplateSpecializationType {{.*}} 'Y<>' dependent Y
123 // CHECK: | `-TemplateArgument template
124 // CHECK: `-TemplateTypeParmType {{.*}} 'type-parameter-0-2' dependent depth 0 index 2
126 template<typename ...T> struct D { // expected-note {{candidate}}
127 template<typename... U> using B = int(int (*...p)(T, U));
128 template<typename U1, typename U2> D(B<U1, U2>*); // expected-note {{candidate}}
130 int f(int(int, int), int(int, int));
131 // FIXME: We can't deduce this because we can't deduce through a
132 // SubstTemplateTypeParmPackType.
133 D d = f; // expected-error {{no viable}}
134 using DT = decltype(d);
135 using DT = D<int, int>;
137 // CHECK-LABEL: Dumping <deduction guide for D>:
138 // CHECK: FunctionTemplateDecl
139 // CHECK: |-TemplateTypeParmDecl {{.*}} typename depth 0 index 0 ... T
140 // CHECK: |-TemplateTypeParmDecl {{.*}} typename depth 0 index 1 U1
141 // CHECK: |-TemplateTypeParmDecl {{.*}} typename depth 0 index 2 U2
142 // CHECK: `-CXXDeductionGuideDecl {{.*}} 'auto (B<type-parameter-0-1, type-parameter-0-2> *) -> D<T...>'
143 // CHECK: `-ParmVarDecl {{.*}} 'B<type-parameter-0-1, type-parameter-0-2> *'
144 // CHECK: FunctionProtoType {{.*}} 'auto (B<type-parameter-0-1, type-parameter-0-2> *) -> D<T...>' dependent trailing_return
145 // CHECK: |-InjectedClassNameType {{.*}} 'D<T...>' dependent
146 // CHECK: `-PointerType {{.*}} 'B<type-parameter-0-1, type-parameter-0-2> *' dependent
147 // CHECK: `-TemplateSpecializationType {{.*}} 'B<type-parameter-0-1, type-parameter-0-2>' sugar dependent alias B
148 // CHECK: |-TemplateArgument type 'type-parameter-0-1'
149 // CHECK: |-TemplateArgument type 'type-parameter-0-2'
150 // CHECK: `-FunctionProtoType {{.*}} 'int (int (*)(T, U)...)' dependent cdecl
151 // CHECK: |-BuiltinType {{.*}} 'int'
152 // CHECK: `-PackExpansionType {{.*}} 'int (*)(T, U)...' dependent expansions 2
153 // CHECK: `-PointerType {{.*}} 'int (*)(T, U)' dependent contains_unexpanded_pack
154 // CHECK: `-ParenType {{.*}} 'int (T, U)' sugar dependent contains_unexpanded_pack
155 // CHECK: `-FunctionProtoType {{.*}} 'int (T, U)' dependent contains_unexpanded_pack cdecl
156 // CHECK: |-BuiltinType {{.*}} 'int'
157 // CHECK: |-TemplateTypeParmType {{.*}} 'T' dependent contains_unexpanded_pack depth 0 index 0 pack
158 // CHECK: | `-TemplateTypeParm {{.*}} 'T'
159 // CHECK: `-SubstTemplateTypeParmPackType {{.*}} 'U' dependent contains_unexpanded_pack typename depth 1 index 0 ... U
160 // CHECK: |-TypeAliasTemplate {{.*}} 'B'
161 // CHECK: `-TemplateArgument pack
162 // CHECK: |-TemplateArgument type 'type-parameter-0-1'
163 // CHECK-NOT: Subst
164 // CHECK: | `-TemplateTypeParmType
165 // CHECK: `-TemplateArgument type 'type-parameter-0-2'
166 // CHECK-NOT: Subst
167 // CHECK: `-TemplateTypeParmType
169 template<int ...N> struct E { // expected-note {{candidate}}
170 template<int ...M> using B = Z<X<N, M>...>;
171 template<int M1, int M2> E(B<M1, M2>); // expected-note {{candidate}}
173 // FIXME: We can't deduce this because we can't deduce through a
174 // SubstNonTypeTemplateParmPackExpr.
175 E e = Z<X<1, 2>, X<3, 4>>(); // expected-error {{no viable}}
176 using ET = decltype(e);
177 using ET = E<1, 3>;
179 // CHECK-LABEL: Dumping <deduction guide for E>:
180 // CHECK: FunctionTemplateDecl
181 // CHECK: |-NonTypeTemplateParmDecl [[N:0x[0-9a-f]*]] {{.*}} 'int' depth 0 index 0 ... N
182 // CHECK: |-NonTypeTemplateParmDecl [[M1:0x[0-9a-f]*]] {{.*}} 'int' depth 0 index 1 M1
183 // CHECK: |-NonTypeTemplateParmDecl [[M2:0x[0-9a-f]*]] {{.*}} 'int' depth 0 index 2 M2
184 // CHECK: `-CXXDeductionGuideDecl {{.*}} 'auto (B<M1, M2>) -> E<N...>'
185 // CHECK: `-ParmVarDecl {{.*}} 'B<M1, M2>':'Z<X<N, M>...>'
186 // CHECK: FunctionProtoType {{.*}} 'auto (B<M1, M2>) -> E<N...>' dependent trailing_return
187 // CHECK: |-InjectedClassNameType {{.*}} 'E<N...>' dependent
188 // CHECK: `-TemplateSpecializationType {{.*}} 'B<M1, M2>' sugar dependent alias B
189 // CHECK: |-TemplateArgument expr
190 // CHECK: | `-DeclRefExpr {{.*}} 'int' NonTypeTemplateParm {{.*}} 'M1' 'int'
191 // CHECK: |-TemplateArgument expr
192 // CHECK: | `-DeclRefExpr {{.*}} 'int' NonTypeTemplateParm {{.*}} 'M2' 'int'
193 // CHECK: `-TemplateSpecializationType {{.*}} 'Z<X<N, M>...>' dependent Z
194 // CHECK: `-TemplateArgument type 'X<N, M>...'
195 // CHECK: `-PackExpansionType {{.*}} 'X<N, M>...' dependent expansions 2
196 // CHECK: `-TemplateSpecializationType {{.*}} 'X<N, M>' dependent contains_unexpanded_pack X
197 // CHECK: |-TemplateArgument expr
198 // CHECK-NOT: Subst
199 // CHECK: | `-DeclRefExpr {{.*}} 'int' NonTypeTemplateParm [[N]] 'N' 'int'
200 // CHECK: `-TemplateArgument expr
201 // CHECK: `-SubstNonTypeTemplateParmPackExpr {{.*}} 'int'
202 // CHECK: |-NonTypeTemplateParmDecl {{.*}} referenced 'int' depth 1 index 0 ... M
203 // CHECK: `-TemplateArgument pack
204 // CHECK: |-TemplateArgument expr
205 // CHECK-NOT: Subst
206 // CHECK: | `-DeclRefExpr {{.*}} 'int' NonTypeTemplateParm [[M1]] 'M1' 'int'
207 // CHECK: `-TemplateArgument expr
208 // CHECK-NOT: Subst
209 // CHECK: `-DeclRefExpr {{.*}} 'int' NonTypeTemplateParm [[M2]] 'M2' 'int'
211 template <char = 'x'> struct F;
213 template <char> struct F {
214 template <typename U>
215 requires(false) F(U);
216 template <typename U>
217 requires(true) F(U);
220 F s(0);
222 // CHECK-LABEL: Dumping <deduction guide for F>:
223 // CHECK: FunctionTemplateDecl
224 // CHECK: |-NonTypeTemplateParmDecl {{.*}} 'char' depth 0 index 0
225 // CHECK: `-TemplateArgument expr
226 // CHECK: | |-inherited from NonTypeTemplateParm {{.*}} '' 'char'
227 // CHECK: | `-CharacterLiteral {{.*}} 'char' 120
228 // CHECK: |-TemplateTypeParmDecl {{.*}} typename depth 0 index 1 U
229 // CHECK: |-ParenExpr {{.*}} 'bool'
230 // CHECK: | `-CXXBoolLiteralExpr {{.*}} 'bool' false
231 // CHECK: |-CXXDeductionGuideDecl {{.*}} implicit <deduction guide for F> 'auto (type-parameter-0-1) -> F<>'
232 // CHECK: | `-ParmVarDecl {{.*}} 'type-parameter-0-1'
233 // CHECK: `-CXXDeductionGuideDecl {{.*}} implicit <deduction guide for F> 'auto (int) -> F<>'
234 // CHECK: |-TemplateArgument integral 120
235 // CHECK: |-TemplateArgument type 'int'
236 // CHECK: | `-BuiltinType {{.*}} 'int'
237 // CHECK: `-ParmVarDecl {{.*}} 'int'
238 // CHECK: FunctionProtoType {{.*}} 'auto (type-parameter-0-1) -> F<>' dependent trailing_return cdecl
239 // CHECK: |-InjectedClassNameType {{.*}} 'F<>' dependent
240 // CHECK: | `-CXXRecord {{.*}} 'F'
241 // CHECK: `-TemplateTypeParmType {{.*}} 'type-parameter-0-1' dependent depth 0 index 1