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