1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify %s
2f4a2713aSLionel Sambuc
3f4a2713aSLionel Sambuc void f(int i, int j, int k = 3);
4f4a2713aSLionel Sambuc void f(int i, int j, int k);
5f4a2713aSLionel Sambuc void f(int i, int j = 2, int k);
6f4a2713aSLionel Sambuc void f(int i, int j, int k);
7f4a2713aSLionel Sambuc void f(int i = 1, int j, int k);
8f4a2713aSLionel Sambuc void f(int i, int j, int k);
9f4a2713aSLionel Sambuc
i()10f4a2713aSLionel Sambuc void i()
11f4a2713aSLionel Sambuc {
12f4a2713aSLionel Sambuc f();
13f4a2713aSLionel Sambuc f(0);
14f4a2713aSLionel Sambuc f(0, 1);
15f4a2713aSLionel Sambuc f(0, 1, 2);
16f4a2713aSLionel Sambuc }
17f4a2713aSLionel Sambuc
18f4a2713aSLionel Sambuc
f1(int i,int i,int j)19f4a2713aSLionel Sambuc int f1(int i, // expected-note {{previous declaration is here}}
20f4a2713aSLionel Sambuc int i, int j) { // expected-error {{redefinition of parameter 'i'}}
21f4a2713aSLionel Sambuc i = 17;
22f4a2713aSLionel Sambuc return j;
23f4a2713aSLionel Sambuc }
24f4a2713aSLionel Sambuc
25f4a2713aSLionel Sambuc int x;
26f4a2713aSLionel Sambuc void g(int x, int y = x); // expected-error {{default argument references parameter 'x'}}
27f4a2713aSLionel Sambuc
28f4a2713aSLionel Sambuc void g2(int x, int y, int z = x + y); // expected-error {{default argument references parameter 'x'}} expected-error {{default argument references parameter 'y'}}
29f4a2713aSLionel Sambuc
30f4a2713aSLionel Sambuc class X {
31f4a2713aSLionel Sambuc void f(X* x = this); // expected-error{{invalid use of 'this' outside of a non-static member function}}
32f4a2713aSLionel Sambuc
g()33f4a2713aSLionel Sambuc void g() {
34f4a2713aSLionel Sambuc int f(X* x = this); // expected-error{{default argument references 'this'}}
35f4a2713aSLionel Sambuc }
36f4a2713aSLionel Sambuc };
37f4a2713aSLionel Sambuc
38f4a2713aSLionel Sambuc // C++ [dcl.fct.default]p6
39f4a2713aSLionel Sambuc class C {
40f4a2713aSLionel Sambuc static int x;
41f4a2713aSLionel Sambuc void f(int i = 3); // expected-note{{previous definition is here}}
42f4a2713aSLionel Sambuc void g(int i, int j = x);
43f4a2713aSLionel Sambuc
44f4a2713aSLionel Sambuc void h();
45f4a2713aSLionel Sambuc };
f(int i=3)46f4a2713aSLionel Sambuc void C::f(int i = 3) // expected-error{{redefinition of default argument}}
47f4a2713aSLionel Sambuc { }
48f4a2713aSLionel Sambuc
g(int i=88,int j)49f4a2713aSLionel Sambuc void C::g(int i = 88, int j) {}
50f4a2713aSLionel Sambuc
h()51f4a2713aSLionel Sambuc void C::h() {
52f4a2713aSLionel Sambuc g(); // okay
53f4a2713aSLionel Sambuc }
54f4a2713aSLionel Sambuc
55f4a2713aSLionel Sambuc // C++ [dcl.fct.default]p9
56f4a2713aSLionel Sambuc struct Y {
57f4a2713aSLionel Sambuc int a;
58f4a2713aSLionel Sambuc int mem1(int i = a); // expected-error{{invalid use of non-static data member 'a'}}
59f4a2713aSLionel Sambuc int mem2(int i = b); // OK; use Y::b
60f4a2713aSLionel Sambuc int mem3(int i);
61f4a2713aSLionel Sambuc int mem4(int i);
62f4a2713aSLionel Sambuc
63f4a2713aSLionel Sambuc struct Nested {
64f4a2713aSLionel Sambuc int mem5(int i = b, // OK; use Y::b
65f4a2713aSLionel Sambuc int j = c, // OK; use Y::Nested::c
66f4a2713aSLionel Sambuc int k = j, // expected-error{{default argument references parameter 'j'}}
67f4a2713aSLionel Sambuc int l = a, // expected-error{{invalid use of non-static data member 'a'}}
68f4a2713aSLionel Sambuc Nested* self = this, // expected-error{{invalid use of 'this' outside of a non-static member function}}
69f4a2713aSLionel Sambuc int m); // expected-error{{missing default argument on parameter 'm'}}
70f4a2713aSLionel Sambuc static int c;
71f4a2713aSLionel Sambuc Nested(int i = 42);
72f4a2713aSLionel Sambuc };
73f4a2713aSLionel Sambuc
74f4a2713aSLionel Sambuc int mem7(Nested n = Nested());
75f4a2713aSLionel Sambuc
76f4a2713aSLionel Sambuc static int b;
77f4a2713aSLionel Sambuc };
78f4a2713aSLionel Sambuc
mem3(int i=b)79f4a2713aSLionel Sambuc int Y::mem3(int i = b) { return i; } // OK; use X::b
80f4a2713aSLionel Sambuc
mem4(int i=a)81f4a2713aSLionel Sambuc int Y::mem4(int i = a) // expected-error{{invalid use of non-static data member 'a'}}
82f4a2713aSLionel Sambuc { return i; }
83f4a2713aSLionel Sambuc
84f4a2713aSLionel Sambuc
85f4a2713aSLionel Sambuc // Try to verify that default arguments interact properly with copy
86f4a2713aSLionel Sambuc // constructors.
87f4a2713aSLionel Sambuc class Z {
88f4a2713aSLionel Sambuc public:
89f4a2713aSLionel Sambuc Z(Z&, int i = 17); // expected-note 3 {{candidate constructor}}
90f4a2713aSLionel Sambuc
f(Z & z)91f4a2713aSLionel Sambuc void f(Z& z) {
92f4a2713aSLionel Sambuc Z z2; // expected-error{{no matching constructor for initialization}}
93f4a2713aSLionel Sambuc Z z3(z);
94f4a2713aSLionel Sambuc }
95f4a2713aSLionel Sambuc
test_Z(const Z & z)96f4a2713aSLionel Sambuc void test_Z(const Z& z) {
97f4a2713aSLionel Sambuc Z z2(z); // expected-error{{no matching constructor for initialization of 'Z'}}
98f4a2713aSLionel Sambuc }
99f4a2713aSLionel Sambuc };
100f4a2713aSLionel Sambuc
test_Z(const Z & z)101f4a2713aSLionel Sambuc void test_Z(const Z& z) {
102f4a2713aSLionel Sambuc Z z2(z); // expected-error{{no matching constructor for initialization of 'Z'}}
103f4a2713aSLionel Sambuc }
104f4a2713aSLionel Sambuc
105f4a2713aSLionel Sambuc struct ZZ {
106f4a2713aSLionel Sambuc static ZZ g(int = 17);
107f4a2713aSLionel Sambuc
108f4a2713aSLionel Sambuc void f(ZZ z = g()); // expected-error{{no matching constructor for initialization}} \
109f4a2713aSLionel Sambuc // expected-note{{passing argument to parameter 'z' here}}
110f4a2713aSLionel Sambuc
111f4a2713aSLionel Sambuc ZZ(ZZ&, int = 17); // expected-note{{candidate constructor}}
112f4a2713aSLionel Sambuc };
113f4a2713aSLionel Sambuc
114f4a2713aSLionel Sambuc // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#325
115f4a2713aSLionel Sambuc class C2 {
116f4a2713aSLionel Sambuc static void g(int = f()); // expected-error{{use of default argument to function 'f' that is declared later in class 'C2'}}
117f4a2713aSLionel Sambuc static int f(int = 10); // expected-note{{default argument declared here}}
118f4a2713aSLionel Sambuc };
119f4a2713aSLionel Sambuc
120f4a2713aSLionel Sambuc // Make sure we actually parse the default argument for an inline definition
121f4a2713aSLionel Sambuc class XX {
A(int length=-1)122f4a2713aSLionel Sambuc void A(int length = -1 ) { }
B()123f4a2713aSLionel Sambuc void B() { A(); }
124f4a2713aSLionel Sambuc };
125*0a6a1f1dSLionel Sambuc
126*0a6a1f1dSLionel Sambuc template <int I = (1 * I)> struct S {}; // expected-error-re {{use of undeclared identifier 'I'{{$}}}}
127*0a6a1f1dSLionel Sambuc S<1> s;
128*0a6a1f1dSLionel Sambuc
129*0a6a1f1dSLionel Sambuc template <int I1 = I2, int I2 = 1> struct T {}; // expected-error-re {{use of undeclared identifier 'I2'{{$}}}}
130*0a6a1f1dSLionel Sambuc T<0, 1> t;
131