references.cpp revision 33fd1fc1814a5573c972840d49317989e20deace
1// RUN: %clang_cc1 -verify -emit-llvm -o - %s | FileCheck %s
2void t1() {
3  extern int& a;
4  int b = a;
5}
6
7void t2(int& a) {
8  int b = a;
9}
10
11int g;
12int& gr = g;
13int& grr = gr;
14void t3() {
15  int b = gr;
16}
17
18// Test reference binding.
19
20struct C { int a; };
21void f(const bool&);
22void f(const int&);
23void f(const _Complex int&);
24void f(const C&);
25
26C aggregate_return();
27
28bool& bool_reference_return();
29int& int_reference_return();
30_Complex int& complex_int_reference_return();
31C& aggregate_reference_return();
32
33void test_bool() {
34  bool a = true;
35  f(a);
36
37  f(true);
38
39  bool_reference_return() = true;
40  a = bool_reference_return();
41
42  struct { const bool& b; } b = { true };
43}
44
45void test_scalar() {
46  int a = 10;
47  f(a);
48
49  struct { int bitfield : 3; } s = { 3 };
50  f(s.bitfield);
51
52  f(10);
53
54  __attribute((vector_size(16))) typedef int vec4;
55  f((vec4){1,2,3,4}[0]);
56
57  int_reference_return() = 10;
58  a = int_reference_return();
59
60  struct { const int& a; } agg = { 10 };
61}
62
63void test_complex() {
64  _Complex int a = 10i;
65  f(a);
66
67  f(10i);
68
69  complex_int_reference_return() = 10i;
70  a = complex_int_reference_return();
71
72  struct { const _Complex int &a; } agg = { 10i };
73}
74
75void test_aggregate() {
76  C c;
77  f(c);
78
79  f(aggregate_return());
80  aggregate_reference_return().a = 10;
81
82  c = aggregate_reference_return();
83
84  struct { const C& a; } agg = { C() };
85}
86
87int& reference_return() {
88  return g;
89}
90
91int reference_decl() {
92  int& a = g;
93  const int& b = 1;
94  return a+b;
95}
96
97struct A {
98  int& b();
99};
100
101void f(A* a) {
102  int b = a->b();
103}
104
105// PR5122
106void *foo = 0;
107void * const & kFoo = foo;
108
109struct D : C { D(); ~D(); };
110
111void h() {
112  // CHECK: call void @_ZN1DD1Ev
113  const C& c = D();
114}
115
116namespace T {
117  struct A {
118    A();
119    ~A();
120  };
121
122  struct B {
123    B();
124    ~B();
125    A f();
126  };
127
128  void f() {
129    // CHECK: call void @_ZN1T1BC1Ev
130    // CHECK: call void @_ZN1T1B1fEv
131    // CHECK: call void @_ZN1T1BD1Ev
132    const A& a = B().f();
133    // CHECK: call void @_ZN1T1fEv
134    f();
135    // CHECK: call void @_ZN1T1AD1Ev
136  }
137}
138
139// PR5227.
140namespace PR5227 {
141void f(int &a) {
142  (a = 10) = 20;
143}
144}
145
146// PR5590
147struct s0;
148struct s1 { struct s0 &s0; };
149void f0(s1 a) { s1 b = a; }
150
151// PR6024
152// CHECK: @_Z2f2v()
153// CHECK: alloca
154// CHECK: store
155// CHECK: load
156const int &f2() { return 0; }
157