1a5728872c7702ddd09537c95bc3cbd20e1f2fb09Daniel Dunbar// RUN: %clang_cc1 -fsyntax-only -verify %s
208ad47cbd1f81fcb31dbc731c13b885a07e12704Bill Wendlingint g(int);
308ad47cbd1f81fcb31dbc731c13b885a07e12704Bill Wendling
408ad47cbd1f81fcb31dbc731c13b885a07e12704Bill Wendlingvoid f() {
508ad47cbd1f81fcb31dbc731c13b885a07e12704Bill Wendling  int i;
608ad47cbd1f81fcb31dbc731c13b885a07e12704Bill Wendling  int &r = i;
708ad47cbd1f81fcb31dbc731c13b885a07e12704Bill Wendling  r = 1;
808ad47cbd1f81fcb31dbc731c13b885a07e12704Bill Wendling  int *p = &r;
908ad47cbd1f81fcb31dbc731c13b885a07e12704Bill Wendling  int &rr = r;
103eb1c546857087b5e1377d172f37868ade960664Douglas Gregor  int (&rg)(int) = g;
1108ad47cbd1f81fcb31dbc731c13b885a07e12704Bill Wendling  rg(i);
1208ad47cbd1f81fcb31dbc731c13b885a07e12704Bill Wendling  int a[3];
133eb1c546857087b5e1377d172f37868ade960664Douglas Gregor  int (&ra)[3] = a;
1408ad47cbd1f81fcb31dbc731c13b885a07e12704Bill Wendling  ra[1] = i;
1508ad47cbd1f81fcb31dbc731c13b885a07e12704Bill Wendling  int *Q;
1608ad47cbd1f81fcb31dbc731c13b885a07e12704Bill Wendling  int *& P = Q;
1708ad47cbd1f81fcb31dbc731c13b885a07e12704Bill Wendling  P[1] = 1;
1808ad47cbd1f81fcb31dbc731c13b885a07e12704Bill Wendling}
19e39245b0673560fc9567fc9df10be1d896440b31Chris Lattner
20943140ee64e979325d25084bf4d768896601a7ceChris Lattnertypedef int t[1];
21943140ee64e979325d25084bf4d768896601a7ceChris Lattnervoid test2() {
22943140ee64e979325d25084bf4d768896601a7ceChris Lattner    t a;
23943140ee64e979325d25084bf4d768896601a7ceChris Lattner    t& b = a;
24943140ee64e979325d25084bf4d768896601a7ceChris Lattner
25943140ee64e979325d25084bf4d768896601a7ceChris Lattner
26943140ee64e979325d25084bf4d768896601a7ceChris Lattner    int c[3];
273eb1c546857087b5e1377d172f37868ade960664Douglas Gregor    int (&rc)[3] = c;
2827c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor}
2927c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor
3027c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor// C++ [dcl.init.ref]p5b1
3127c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregorstruct A { };
3227c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregorstruct B : A { } b;
3327c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor
3427c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregorvoid test3() {
3527c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor  double d = 2.0;
3627c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor  double& rd = d; // rd refers to d
3727c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor  const double& rcd = d; // rcd refers to d
3827c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor
3927c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor  A& ra = b; // ra refers to A subobject in b
4027c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor  const A& rca = b; // rca refers to A subobject in b
4127c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor}
4227c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor
4327c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas GregorB fB();
4427c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor
4527c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor// C++ [dcl.init.ref]p5b2
4627c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregorvoid test4() {
4720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  double& rd2 = 2.0; // expected-error{{non-const lvalue reference to type 'double' cannot bind to a temporary of type 'double'}}
4827c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor  int i = 2;
4920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  double& rd3 = i; // expected-error{{non-const lvalue reference to type 'double' cannot bind to a value of unrelated type 'int'}}
5027c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor
5127c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor  const A& rca = fB();
5227c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor}
5327c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor
5427c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregorvoid test5() {
5520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //  const double& rcd2 = 2; // rcd2 refers to temporary with value 2.0
5627c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor  const volatile int cvi = 1;
5758f9e13e87e57236fee4b914eea9be6f92a1c345Chris Lattner  const int& r = cvi; // expected-error{{binding of reference to type 'const int' to a value of type 'const volatile int' drops qualifiers}}
5827c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor}
5927c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor
6027c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor// C++ [dcl.init.ref]p3
6127c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregorint& test6(int& x) {
6227c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor  int& yo; // expected-error{{declaration of reference variable 'yo' requires an initializer}}
6327c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor
6427c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor  return x;
6527c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor}
6627c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregorint& not_initialized_error; // expected-error{{declaration of reference variable 'not_initialized_error' requires an initializer}}
6727c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregorextern int& not_initialized_okay;
6827c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor
69325e593a83b20d9bce3628aa78fda983b554814eDouglas Gregorclass Test6 { // expected-warning{{class 'Test6' does not declare any constructor to initialize its non-modifiable members}}
70325e593a83b20d9bce3628aa78fda983b554814eDouglas Gregor  int& okay; // expected-note{{reference member 'okay' will never be initialized}}
7127c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor};
7227c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor
7327c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregorstruct C : B, A { };
7427c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregor
7527c8dc06f65d7abcf6a7e7f64a7960c9a150ca01Douglas Gregorvoid test7(C& c) {
767c2342dd4c9947806842e5aca3d2bb2e542853c9John McCall  A& a1 = c; // expected-error {{ambiguous conversion from derived class 'C' to base class 'A':}}
77943140ee64e979325d25084bf4d768896601a7ceChris Lattner}
78f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor
79f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor// C++ [dcl.ref]p1, C++ [dcl.ref]p4
80f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregorvoid test8(int& const,// expected-error{{'const' qualifier may not be applied to a reference}}
81f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor
82f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor           void&,     // expected-error{{cannot form a reference to 'void'}}
8308631c5fa053867146b5ee8be658c229f6bf127cChris Lattner           int& &)    // expected-error{{type name declared as a reference to a reference}}
84f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor{
85f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor  typedef int& intref;
86f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor  typedef intref& intrefref; // C++ DR 106: reference collapsing
87f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor
88f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor  typedef intref const intref_c; // okay. FIXME: how do we verify that this is the same type as intref?
89f1f9b4e5c7fd087e78f2e387c01098d49d41e784Douglas Gregor}
9096be6917e1c4ba1f4435a14c9e7c6c139571d086Douglas Gregor
9196be6917e1c4ba1f4435a14c9e7c6c139571d086Douglas Gregor
9296be6917e1c4ba1f4435a14c9e7c6c139571d086Douglas Gregorclass string {
9396be6917e1c4ba1f4435a14c9e7c6c139571d086Douglas Gregor  char *Data;
9496be6917e1c4ba1f4435a14c9e7c6c139571d086Douglas Gregor  unsigned Length;
9596be6917e1c4ba1f4435a14c9e7c6c139571d086Douglas Gregorpublic:
9696be6917e1c4ba1f4435a14c9e7c6c139571d086Douglas Gregor  string();
9796be6917e1c4ba1f4435a14c9e7c6c139571d086Douglas Gregor  ~string();
9896be6917e1c4ba1f4435a14c9e7c6c139571d086Douglas Gregor};
9996be6917e1c4ba1f4435a14c9e7c6c139571d086Douglas Gregor
10096be6917e1c4ba1f4435a14c9e7c6c139571d086Douglas Gregorstring getInput();
10196be6917e1c4ba1f4435a14c9e7c6c139571d086Douglas Gregor
10296be6917e1c4ba1f4435a14c9e7c6c139571d086Douglas Gregorvoid test9() {
10396be6917e1c4ba1f4435a14c9e7c6c139571d086Douglas Gregor  string &s = getInput(); // expected-error{{lvalue reference}}
10496be6917e1c4ba1f4435a14c9e7c6c139571d086Douglas Gregor}
105093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson
106093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlssonvoid test10() {
107093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  __attribute((vector_size(16))) typedef int vec4;
108093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  typedef __attribute__(( ext_vector_type(4) )) int ext_vec4;
109093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson
110093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  vec4 v;
111093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  int &a = v[0]; // expected-error{{non-const reference cannot bind to vector element}}
112093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  const int &b = v[0];
113093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson
114093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  ext_vec4 ev;
115093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  int &c = ev.x; // expected-error{{non-const reference cannot bind to vector element}}
116093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson  const int &d = ev.x;
117093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson}
1189dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor
1199dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregornamespace PR7149 {
1209dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  template<typename T> struct X0
1219dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  {
1229dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor    T& first;
1239dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor    X0(T& p1) : first(p1) { }
1249dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  };
1259dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor
1269dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor
1279dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  void f()
1289dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  {
1299dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor    int p1[1];
1309dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor    X0< const int[1]> c(p1);
1319dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  }
1329dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor}
1335082d34e4107a44ac7f07b62f7a6c917e0e6e71eJohn McCall
1345082d34e4107a44ac7f07b62f7a6c917e0e6e71eJohn McCallnamespace PR8608 {
1355082d34e4107a44ac7f07b62f7a6c917e0e6e71eJohn McCall  bool& f(unsigned char& c) { return (bool&)c; }
1365082d34e4107a44ac7f07b62f7a6c917e0e6e71eJohn McCall}
1370124839d7fb3d846795190ba2ccf3951f27784feChandler Carruth
1380124839d7fb3d846795190ba2ccf3951f27784feChandler Carruth// The following crashed trying to recursively evaluate the LValue.
1395965b7c7ddf8d9635426943a05441c71cb59fef6Hans Wennborgconst int &do_not_crash = do_not_crash; // expected-warning{{reference 'do_not_crash' is not yet bound to a value when used within its own initialization}}
1404e47ecb6b2e399d2a7cc3a91d1eab9e501c5580fRichard Smith
1414e47ecb6b2e399d2a7cc3a91d1eab9e501c5580fRichard Smithnamespace ExplicitRefInit {
1424e47ecb6b2e399d2a7cc3a91d1eab9e501c5580fRichard Smith  // This is invalid: we can't copy-initialize an 'A' temporary using an
1434e47ecb6b2e399d2a7cc3a91d1eab9e501c5580fRichard Smith  // explicit constructor.
1444e47ecb6b2e399d2a7cc3a91d1eab9e501c5580fRichard Smith  struct A { explicit A(int); };
1454e47ecb6b2e399d2a7cc3a91d1eab9e501c5580fRichard Smith  const A &a(0); // expected-error {{reference to type 'const ExplicitRefInit::A' could not bind to an rvalue of type 'int'}}
1464e47ecb6b2e399d2a7cc3a91d1eab9e501c5580fRichard Smith}
147