xref: /llvm-project/lldb/test/API/lang/cpp/operators/main.cpp (revision c131dfefe2b404dc1dbb32a02ea484fd7edaffdc)
1 #include <cstdlib>
2 
3 int side_effect = 0;
4 
5 struct B { int dummy = 2324; };
6 struct C {
7   void *operator new(std::size_t size) { C* r = ::new C; r->custom_new = true; return r; }
8   void *operator new[](std::size_t size) { C* r = static_cast<C*>(std::malloc(size)); r->custom_new = true; return r; }
9   void operator delete(void *p) { std::free(p); side_effect = 1; }
10   void operator delete[](void *p) { std::free(p); side_effect = 2; }
11 
12   bool custom_new = false;
13   B b;
14   B* operator->() { return &b; }
15   int operator->*(int) { return 2; }
16   int operator+(int) { return 44; }
17   int operator+=(int) { return 42; }
18   int operator++(int) { return 123; }
19   int operator++() { return 1234; }
20   int operator-(int) { return 34; }
21   int operator-=(int) { return 32; }
22   int operator--() { return 321; }
23   int operator--(int) { return 4321; }
24 
25   int operator*(int) { return 51; }
26   int operator*=(int) { return 52; }
27   int operator%(int) { return 53; }
28   int operator%=(int) { return 54; }
29   int operator/(int) { return 55; }
30   int operator/=(int) { return 56; }
31   int operator^(int) { return 57; }
32   int operator^=(int) { return 58; }
33 
34   int operator|(int) { return 61; }
35   int operator|=(int) { return 62; }
36   int operator||(int) { return 63; }
37   int operator&(int) { return 64; }
38   int operator&=(int) { return 65; }
39   int operator&&(int) { return 66; }
40 
41   int operator~() { return 71; }
42   int operator!() { return 72; }
43   int operator!=(int) { return 73; }
44   int operator=(int) { return 74; }
45   int operator==(int) { return 75; }
46 
47   int operator<(int) { return 81; }
48   int operator<<(int) { return 82; }
49   int operator<=(int) { return 83; }
50   int operator<<=(int) { return 84; }
51   int operator>(int) { return 85; }
52   int operator>>(int) { return 86; }
53   int operator>=(int) { return 87; }
54   int operator>>=(int) { return 88; }
55 
56   int operator,(int) { return 2012; }
57   int operator&() { return 2013; }
58 
59   int operator()(int) { return 91; }
60   int operator[](int) { return 92; }
61 
62   operator int() { return 11; }
63   operator long() { return 12; }
64 
65   // Make sure this doesn't collide with
66   // the real operator int.
67   int operatorint() { return 13; }
68   int operatornew() { return 14; }
69 };
70 
71 int main(int argc, char **argv) {
72   C c;
73   int result = c->dummy;
74   result = c->*4;
75   result += c+1;
76   result += c+=1;
77   result += c++;
78   result += ++c;
79   result += c-1;
80   result += c-=1;
81   result += c--;
82   result += --c;
83 
84   result += c * 4;
85   result += c *= 4;
86   result += c % 4;
87   result += c %= 4;
88   result += c / 4;
89   result += c /= 4;
90   result += c ^ 4;
91   result += c ^= 4;
92 
93   result += c | 4;
94   result += c |= 4;
95   result += c || 4;
96   result += c & 4;
97   result += c &= 4;
98   result += c && 4;
99 
100   result += ~c;
101   result += !c;
102   result += c!=1;
103   result += c=2;
104   result += c==2;
105 
106   result += c<2;
107   result += c<<2;
108   result += c<=2;
109   result += c<<=2;
110   result += c>2;
111   result += c>>2;
112   result += c>=2;
113   result += c>>=2;
114 
115   result += (c , 2);
116   result += &c;
117 
118   result += c(1);
119   result += c[1];
120 
121   result += static_cast<int>(c);
122   result += static_cast<long>(c);
123   result += c.operatorint();
124   result += c.operatornew();
125 
126   C *c2 = new C();
127   C *c3 = new C[3];
128 
129   //% self.expect("expr c->dummy", endstr=" 2324\n")
130   //% self.expect("expr c->*2", endstr=" 2\n")
131   //% self.expect("expr c + 44", endstr=" 44\n")
132   //% self.expect("expr c += 42", endstr=" 42\n")
133   //% self.expect("expr c++", endstr=" 123\n")
134   //% self.expect("expr ++c", endstr=" 1234\n")
135   //% self.expect("expr c - 34", endstr=" 34\n")
136   //% self.expect("expr c -= 32", endstr=" 32\n")
137   //% self.expect("expr c--", endstr=" 4321\n")
138   //% self.expect("expr --c", endstr=" 321\n")
139   //% self.expect("expr c * 3", endstr=" 51\n")
140   //% self.expect("expr c *= 3", endstr=" 52\n")
141   //% self.expect("expr c % 3", endstr=" 53\n")
142   //% self.expect("expr c %= 3", endstr=" 54\n")
143   //% self.expect("expr c / 3", endstr=" 55\n")
144   //% self.expect("expr c /= 3", endstr=" 56\n")
145   //% self.expect("expr c ^ 3", endstr=" 57\n")
146   //% self.expect("expr c ^= 3", endstr=" 58\n")
147   //% self.expect("expr c | 3", endstr=" 61\n")
148   //% self.expect("expr c |= 3", endstr=" 62\n")
149   //% self.expect("expr c || 3", endstr=" 63\n")
150   //% self.expect("expr c & 3", endstr=" 64\n")
151   //% self.expect("expr c &= 3", endstr=" 65\n")
152   //% self.expect("expr c && 3", endstr=" 66\n")
153   //% self.expect("expr ~c", endstr=" 71\n")
154   //% self.expect("expr !c", endstr=" 72\n")
155   //% self.expect("expr c!=1", endstr=" 73\n")
156   //% self.expect("expr c=1", endstr=" 74\n")
157   //% self.expect("expr c==1", endstr=" 75\n")
158   //% self.expect("expr c<1", endstr=" 81\n")
159   //% self.expect("expr c<<1", endstr=" 82\n")
160   //% self.expect("expr c<=1", endstr=" 83\n")
161   //% self.expect("expr c<<=1", endstr=" 84\n")
162   //% self.expect("expr c>1", endstr=" 85\n")
163   //% self.expect("expr c>>1", endstr=" 86\n")
164   //% self.expect("expr c>=1", endstr=" 87\n")
165   //% self.expect("expr c>>=1", endstr=" 88\n")
166   //% self.expect("expr c,1", endstr=" 2012\n")
167   //% self.expect("expr &c", endstr=" 2013\n")
168   //% self.expect("expr c(1)", endstr=" 91\n")
169   //% self.expect("expr c[1]", endstr=" 92\n")
170   //% self.expect("expr static_cast<int>(c)", endstr=" 11\n")
171   //% self.expect("expr static_cast<long>(c)", endstr=" 12\n")
172   //% self.expect("expr c.operatorint()", endstr=" 13\n")
173   //% self.expect("expr c.operatornew()", endstr=" 14\n")
174   //% self.expect("expr (new struct C)->custom_new", endstr=" true\n")
175   //% self.expect("expr (new struct C[1])->custom_new", endstr=" true\n")
176   //% self.expect("expr delete c2; side_effect", endstr=" = 1\n")
177   //% self.expect("expr delete[] c3; side_effect", endstr=" = 2\n")
178   delete c2;
179   delete[] c3;
180   return 0;
181 }
182