1a5728872c7702ddd09537c95bc3cbd20e1f2fb09Daniel Dunbar// RUN: %clang_cc1 -fsyntax-only -pedantic -verify %s
2930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor// C++ [dcl.init.aggr]p2
3930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregorstruct A {
4930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  int x;
5930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  struct B {
6930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    int i;
7930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    int j;
8930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  } b;
9930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor} a1 = { 1, { 2, 3 } };
10930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
11930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregorstruct NonAggregate {
12930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  NonAggregate();
13930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
14930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  int a, b;
15930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor};
167c2342dd4c9947806842e5aca3d2bb2e542853c9John McCallNonAggregate non_aggregate_test = { 1, 2 }; // expected-error{{non-aggregate type 'NonAggregate' cannot be initialized with an initializer list}}
17930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
1820599392a99956eaac4cf351a0935574090cb6c3Richard SmithNonAggregate non_aggregate_test2[2] = { { 1, 2 }, { 3, 4 } }; // expected-error 2 {{non-aggregate type 'NonAggregate' cannot be initialized with an initializer list}}
19930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
20930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
21930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor// C++ [dcl.init.aggr]p3
22930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas GregorA a_init = A();
23930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
24930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor// C++ [dcl.init.aggr]p4
25930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregorint x[] = { 1, 3, 5 };
26930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregorint x_sizecheck[(sizeof(x) / sizeof(int)) == 3? 1 : -1];
27930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregorint x2[] = { }; // expected-warning{{zero size arrays are an extension}}
28930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
29930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor// C++ [dcl.init.aggr]p5
30930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregorstruct StaticMemberTest {
31930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  int i;
32930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  static int s;
33930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  int *j;
34930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor} smt = { 1, &smt.i };
35930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
36930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor// C++ [dcl.init.aggr]p6
37b574e5630d66629ccc8f2432e60b59ae42f1f363Douglas Gregorchar cv[4] = { 'a', 's', 'd', 'f', 0 }; // expected-error{{excess elements in array initializer}}
38930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
39930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor// C++ [dcl.init.aggr]p7
40930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregorstruct TooFew { int a; char* b; int c; };
41a9bff30776888977f580c9cac212fd1583ee963eDouglas GregorTooFew too_few = { 1, "asdf" }; // expected-warning{{conversion from string literal to 'char *' is deprecated}}
42930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
43220ccbf2c9ef97034cce80561f9f46c4f1f63bc7John McCallstruct NoDefaultConstructor { // expected-note 3 {{candidate constructor (the implicit copy constructor)}} \
4416006c901315fa12a108b4e571f187f4b676e426Douglas Gregor                              // expected-note{{declared here}}
45b1622a1fd7b7f4ab8d00d0183d17c90ad25c14e3John McCall  NoDefaultConstructor(int); // expected-note 3 {{candidate constructor}}
4687fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor};
4716006c901315fa12a108b4e571f187f4b676e426Douglas Gregorstruct TooFewError { // expected-error{{implicit default constructor for}}
4887fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  int a;
4916006c901315fa12a108b4e571f187f4b676e426Douglas Gregor  NoDefaultConstructor nodef; // expected-note{{member is declared here}}
5087fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor};
5187fd703e097c27d63479cb83b687d4000a22bbb1Douglas GregorTooFewError too_few_okay = { 1, 1 };
5287fd703e097c27d63479cb83b687d4000a22bbb1Douglas GregorTooFewError too_few_error = { 1 }; // expected-error{{no matching constructor}}
5387fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor
547c2342dd4c9947806842e5aca3d2bb2e542853c9John McCallTooFewError too_few_okay2[2] = { 1, 1 }; // expected-note{{implicit default constructor for 'TooFewError' first required here}}
5587fd703e097c27d63479cb83b687d4000a22bbb1Douglas GregorTooFewError too_few_error2[2] = { 1 }; // expected-error{{no matching constructor}}
5687fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor
5739da0b8145eaec7da7004f9b3645c5c9f4f63b1dDouglas GregorNoDefaultConstructor too_few_error3[3] = { }; // expected-error {{no matching constructor}}
5887fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor
59930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor// C++ [dcl.init.aggr]p8
60930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregorstruct Empty { };
61930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregorstruct EmptyTest {
62930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  Empty s;
63930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  int i;
64930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor} empty_test = { { }, 3 };
65930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
66930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas GregorEmptyTest empty_test2 = { 3 }; // expected-error{{initializer for aggregate with no elements requires explicit braces}}
67930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
68930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregorstruct NonEmpty {
69930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  int a;
70930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  Empty empty;
71930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor};
72930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregorstruct NonEmptyTest {
73930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  NonEmpty a, b;
74930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor} non_empty_test = { { }, { } };
75930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
76930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor// C++ [dcl.init.aggr]p9
77930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregorstruct HasReference {
78930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  int i;
79930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  int &j; // expected-note{{uninitialized reference member is here}}
80930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor};
81930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregorint global_int;
82930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas GregorHasReference r1 = { 1, global_int };
832fe9b7fb07dff15dd15dd8755a9a9e6de0fe46fcRichard TrieuHasReference r2 = { 1 } ; // expected-error{{reference member of type 'int &' uninitialized}}
84930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
85930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor// C++ [dcl.init.aggr]p10
86930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor// Note: the behavior here is identical to C
87930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregorint xs[2][2] = { 3, 1, 4, 2 };
88930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregorfloat y[4][3] = { { 1 }, { 2 }, { 3 }, { 4 } };
89930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
90930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor// C++ [dcl.init.aggr]p11
91930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor// Note: the behavior here is identical to C
92930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregorfloat y2[4][3] = { { 1, 3, 5 }, { 2, 4, 6 }, { 3, 5, 7 } };
93930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregorfloat same_as_y2[4][3] = { 1, 3, 5, 2, 4, 6, 3, 5, 7 };
94930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
95930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor// C++ [dcl.init.aggr]p12
96930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregorstruct A2 {
97930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  int i;
98930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  operator int *();
99930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor};
100930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregorstruct B2 {
101930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  A2 a1, a2;
102930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  int *z;
103930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor};
104930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregorstruct C2 {
105930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  operator A2();
106930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor};
107930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregorstruct D2 {
108930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  operator int();
109930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor};
110930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas GregorA2 a2;
111930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas GregorC2 c2;
112930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas GregorD2 d2;
113930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas GregorB2 b2 = { 4, a2, a2 };
114930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas GregorB2 b2_2 = { 4, d2, 0 };
115734d9869efb5d126df53ba70a6060789887e0d68Douglas GregorB2 b2_3 = { c2, a2, a2 };
116930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
117930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor// C++ [dcl.init.aggr]p15:
118220ccbf2c9ef97034cce80561f9f46c4f1f63bc7John McCallunion u { int a; char* b; }; // expected-note{{candidate constructor (the implicit copy constructor)}}
119930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregoru u1 = { 1 };
120930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregoru u2 = u1;
1217abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregoru u3 = 1; // expected-error{{no viable conversion}}
122eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregoru u4 = { 0, "asdf" };  // expected-error{{excess elements in union initializer}}
12358f9e13e87e57236fee4b914eea9be6f92a1c345Chris Lattneru u5 = { "asdf" }; // expected-error{{cannot initialize a member subobject of type 'int' with an lvalue of type 'const char [5]'}}
124