1 // RUN: %clang_cc1 -std=c++1z -ast-print %s > %t
2 // RUN: FileCheck < %t %s -check-prefix=CHECK1
3 // RUN: FileCheck < %t %s -check-prefix=CHECK2
4 // RUN: %clang_cc1 -std=c++1z -ast-dump %s | FileCheck --check-prefix=DUMP %s
5
6 template <int X, typename Y, int Z = 5>
7 struct foo {
8 int constant;
foofoo9 foo() {}
getSumfoo10 Y getSum() { return Y(X + Z); }
11 };
12
13 template <int A, typename B>
bar()14 B bar() {
15 return B(A);
16 }
17
baz()18 void baz() {
19 int x = bar<5, int>();
20 int y = foo<5, int>().getSum();
21 double z = foo<2, double, 3>().getSum();
22 }
23
24 // Template definition - foo
25 // CHECK1: template <int X, typename Y, int Z = 5> struct foo {
26 // CHECK2: template <int X, typename Y, int Z = 5> struct foo {
27
28 // Template instantiation - foo
29 // Since the order of instantiation may vary during runs, run FileCheck twice
30 // to make sure each instantiation is in the correct spot.
31 // CHECK1: template<> struct foo<5, int, 5> {
32 // CHECK2: template<> struct foo<2, double, 3> {
33
34 // Template definition - bar
35 // CHECK1: template <int A, typename B> B bar()
36 // CHECK2: template <int A, typename B> B bar()
37
38 // Template instantiation - bar
39 // CHECK1: template<> int bar<5, int>()
40 // CHECK2: template<> int bar<5, int>()
41
42 // CHECK1-LABEL: template <typename ...T> struct A {
43 // CHECK1-NEXT: template <T ...x[3]> struct B {
44 template <typename ...T> struct A {
45 template <T ...x[3]> struct B {};
46 };
47
48 // CHECK1-LABEL: template <typename ...T> void f() {
49 // CHECK1-NEXT: A<T[3]...> a;
f()50 template <typename ...T> void f() {
51 A<T[3]...> a;
52 }
53
54 namespace test2 {
55 void func(int);
56 void func(float);
57 template<typename T>
tmpl()58 void tmpl() {
59 func(T());
60 }
61
62 // DUMP: UnresolvedLookupExpr {{.*}} <col:3> '<overloaded function type>' lvalue (ADL) = 'func'
63 }
64
65 namespace test3 {
66 template<typename T> struct A {};
67 template<typename T> A(T) -> A<int>;
68 // CHECK1: template <typename T> A(T) -> A<int>;
69 }
70
71 namespace test4 {
72 template <unsigned X, auto A>
73 struct foo {
74 static void fn();
75 };
76
77 // Prints using an "integral" template argument. Test that this correctly
78 // includes the type for the auto argument and omits it for the fixed
79 // type/unsigned argument (see
80 // TemplateParameterList::shouldIncludeTypeForArgument)
81 // CHECK1: {{^ }}template<> struct foo<0, 0L> {
82 // CHECK1: {{^ }}void test(){{ }}{
83 // CHECK1: {{^ }}foo<0, 0 + 0L>::fn();
test()84 void test() {
85 foo<0, 0 + 0L>::fn();
86 }
87
88 // Prints using an "expression" template argument. This renders based on the way
89 // the user wrote the arguments (including that + expression) - so it's not
90 // powered by the shouldIncludeTypeForArgument functionality.
91 // Not sure if this it's intentional that these two specializations are rendered
92 // differently in this way.
93 // CHECK1: {{^ }}template<> struct foo<1, 0 + 0L> {
94 template struct foo<1, 0 + 0L>;
95 }
96
97 namespace test5 {
f()98 template<long> void f() {}
99 void (*p)() = f<0>;
f()100 template<unsigned = 0> void f() {}
101 void (*q)() = f<>;
102 // Not perfect - this code in the dump would be ambiguous, but it's the best we
103 // can do to differentiate these two implicit specializations.
104 // CHECK1: template<> void f<0L>()
105 // CHECK1: template<> void f<0U>()
106 }
107
108 namespace test6 {
109 template <class D>
110 constexpr bool C = true;
111
112 template <class Key>
func()113 void func() {
114 C<Key>;
115 // DUMP: UnresolvedLookupExpr {{.*}} '<dependent type>' lvalue (no ADL) = 'C'
116 // DUMP-NEXT: `-TemplateArgument type 'Key'
117 // DUMP-NEXT: `-TemplateTypeParmType {{.*}} 'Key' dependent depth 0 index 0
118 // DUMP-NEXT: `-TemplateTypeParm {{.*}} 'Key'
119 }
120 }
121