[flang][cuda] Adapt ExternalNameConversion to work in gpu module (#117039)
[llvm-project.git] / clang / test / AST / ByteCode / cxx17.cpp
blobe8559d8b9812ac3d7c43663c68bbaee648c68134
1 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -std=c++17 -verify=expected,both %s
2 // RUN: %clang_cc1 -std=c++17 -verify=ref,both %s
4 struct F { int a; int b;};
5 constexpr F getF() {
6 return {12, 3};
8 constexpr int f() {
9 auto [a1, b1] = getF();
10 auto [a2, b2] = getF();
12 return a1 + a2 + b1 + b2;
14 static_assert(f() == 30);
17 constexpr int structRefs() {
18 const auto &[a, b] = getF();
20 return a + b;
22 static_assert(structRefs() == 15);
24 constexpr int structRefs2() {
25 F f = getF();
26 const auto &[a, b] = f;
28 return a + b;
30 static_assert(structRefs2() == 15);
33 template<typename T>
34 struct Tuple {
35 T first;
36 T second;
37 constexpr Tuple(T a, T b) : first(a), second(b) {}
39 template<typename T>
40 constexpr T addTuple(const Tuple<T> &Tu) {
41 auto [a, b] = Tu;
42 return a + b;
45 template<typename T>
46 constexpr T addTuple2(const Tuple<T> &Tu) {
47 auto [a, b] = Tu;
48 return Tu.first + Tu.second;
51 constexpr Tuple<int> T1 = Tuple(1,2);
52 static_assert(addTuple(T1) == 3);
53 static_assert(addTuple2(T1) == 3);
55 constexpr Tuple<short> T2 = Tuple<short>(11,2);
56 static_assert(addTuple(T2) == 13);
57 static_assert(addTuple2(T2) == 13);
59 constexpr int Modify() {
60 auto T = Tuple<int>(10, 20);
61 auto &[x, y] = T;
62 x += 1;
63 y += 1;
64 return T.first + T.second;
66 static_assert(Modify() == 32, "");
68 constexpr int a() {
69 int a[2] = {5, 3};
70 auto [x, y] = a;
71 return x + y;
73 static_assert(a() == 8);
75 constexpr int b() {
76 int a[2] = {5, 3};
77 auto &[x, y] = a;
78 x += 1;
79 y += 2;
80 return a[0] + a[1];
82 static_assert(b() == 11);
84 namespace cwg1872 {
85 template<typename T> struct A : T {
86 constexpr int f() const { return 0; }
88 struct X {};
89 struct Y { virtual int f() const; };
90 struct Z : virtual X {};
92 constexpr int z = A<Z>().f(); // both-error {{must be initialized by a constant expression}} \
93 // both-note {{non-literal type 'A<Z>' cannot be used in a constant expression}}
96 /// The diagnostics between the two interpreters used to be different here.
97 struct S { int a; };
98 constexpr S getS() { // both-error {{constexpr function never produces a constant expression}}
99 (void)(1/0); // both-note 2{{division by zero}} \
100 // both-warning {{division by zero}}
101 return S{12};
103 constexpr S s = getS(); // both-error {{must be initialized by a constant expression}} \
104 // both-note {{in call to 'getS()'}} \
105 // both-note {{declared here}}
106 static_assert(s.a == 12, ""); // both-error {{not an integral constant expression}} \
107 // both-note {{initializer of 's' is not a constant expression}}