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