xref: /minix3/external/bsd/llvm/dist/clang/test/SemaTemplate/instantiate-expr-1.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify %s
2*f4a2713aSLionel Sambuc template<int I, int J>
3*f4a2713aSLionel Sambuc struct Bitfields {
4*f4a2713aSLionel Sambuc   int simple : I; // expected-error{{bit-field 'simple' has zero width}}
5*f4a2713aSLionel Sambuc   int parens : (J);
6*f4a2713aSLionel Sambuc };
7*f4a2713aSLionel Sambuc 
8*f4a2713aSLionel Sambuc void test_Bitfields(Bitfields<0, 5> *b) {
9*f4a2713aSLionel Sambuc   (void)sizeof(Bitfields<10, 5>);
10*f4a2713aSLionel Sambuc   (void)sizeof(Bitfields<0, 1>); // expected-note{{in instantiation of template class 'Bitfields<0, 1>' requested here}}
11*f4a2713aSLionel Sambuc }
12*f4a2713aSLionel Sambuc 
13*f4a2713aSLionel Sambuc template<int I, int J>
14*f4a2713aSLionel Sambuc struct BitfieldPlus {
15*f4a2713aSLionel Sambuc   int bitfield : I + J; // expected-error{{bit-field 'bitfield' has zero width}}
16*f4a2713aSLionel Sambuc };
17*f4a2713aSLionel Sambuc 
18*f4a2713aSLionel Sambuc void test_BitfieldPlus() {
19*f4a2713aSLionel Sambuc   (void)sizeof(BitfieldPlus<0, 1>);
20*f4a2713aSLionel Sambuc   (void)sizeof(BitfieldPlus<-5, 5>); // expected-note{{in instantiation of template class 'BitfieldPlus<-5, 5>' requested here}}
21*f4a2713aSLionel Sambuc }
22*f4a2713aSLionel Sambuc 
23*f4a2713aSLionel Sambuc template<int I, int J>
24*f4a2713aSLionel Sambuc struct BitfieldMinus {
25*f4a2713aSLionel Sambuc   int bitfield : I - J; // expected-error{{bit-field 'bitfield' has negative width (-1)}} \
26*f4a2713aSLionel Sambuc   // expected-error{{bit-field 'bitfield' has zero width}}
27*f4a2713aSLionel Sambuc };
28*f4a2713aSLionel Sambuc 
29*f4a2713aSLionel Sambuc void test_BitfieldMinus() {
30*f4a2713aSLionel Sambuc   (void)sizeof(BitfieldMinus<5, 1>);
31*f4a2713aSLionel Sambuc   (void)sizeof(BitfieldMinus<0, 1>); // expected-note{{in instantiation of template class 'BitfieldMinus<0, 1>' requested here}}
32*f4a2713aSLionel Sambuc   (void)sizeof(BitfieldMinus<5, 5>); // expected-note{{in instantiation of template class 'BitfieldMinus<5, 5>' requested here}}
33*f4a2713aSLionel Sambuc }
34*f4a2713aSLionel Sambuc 
35*f4a2713aSLionel Sambuc template<int I, int J>
36*f4a2713aSLionel Sambuc struct BitfieldDivide {
37*f4a2713aSLionel Sambuc   int bitfield : I / J; // expected-error{{expression is not an integral constant expression}} \
38*f4a2713aSLionel Sambuc                         // expected-note{{division by zero}}
39*f4a2713aSLionel Sambuc };
40*f4a2713aSLionel Sambuc 
41*f4a2713aSLionel Sambuc void test_BitfieldDivide() {
42*f4a2713aSLionel Sambuc   (void)sizeof(BitfieldDivide<5, 1>);
43*f4a2713aSLionel Sambuc   (void)sizeof(BitfieldDivide<5, 0>); // expected-note{{in instantiation of template class 'BitfieldDivide<5, 0>' requested here}}
44*f4a2713aSLionel Sambuc }
45*f4a2713aSLionel Sambuc 
46*f4a2713aSLionel Sambuc template<typename T, T I, int J>
47*f4a2713aSLionel Sambuc struct BitfieldDep {
48*f4a2713aSLionel Sambuc   int bitfield : I + J;
49*f4a2713aSLionel Sambuc };
50*f4a2713aSLionel Sambuc 
51*f4a2713aSLionel Sambuc void test_BitfieldDep() {
52*f4a2713aSLionel Sambuc   (void)sizeof(BitfieldDep<int, 1, 5>);
53*f4a2713aSLionel Sambuc }
54*f4a2713aSLionel Sambuc 
55*f4a2713aSLionel Sambuc template<int I>
56*f4a2713aSLionel Sambuc struct BitfieldNeg {
57*f4a2713aSLionel Sambuc   int bitfield : (-I); // expected-error{{bit-field 'bitfield' has negative width (-5)}}
58*f4a2713aSLionel Sambuc };
59*f4a2713aSLionel Sambuc 
60*f4a2713aSLionel Sambuc template<typename T, T I>
61*f4a2713aSLionel Sambuc struct BitfieldNeg2 {
62*f4a2713aSLionel Sambuc   int bitfield : (-I); // expected-error{{bit-field 'bitfield' has negative width (-5)}}
63*f4a2713aSLionel Sambuc };
64*f4a2713aSLionel Sambuc 
65*f4a2713aSLionel Sambuc void test_BitfieldNeg() {
66*f4a2713aSLionel Sambuc   (void)sizeof(BitfieldNeg<-5>); // okay
67*f4a2713aSLionel Sambuc   (void)sizeof(BitfieldNeg<5>); // expected-note{{in instantiation of template class 'BitfieldNeg<5>' requested here}}
68*f4a2713aSLionel Sambuc   (void)sizeof(BitfieldNeg2<int, -5>); // okay
69*f4a2713aSLionel Sambuc   (void)sizeof(BitfieldNeg2<int, 5>); // expected-note{{in instantiation of template class 'BitfieldNeg2<int, 5>' requested here}}
70*f4a2713aSLionel Sambuc }
71*f4a2713aSLionel Sambuc 
72*f4a2713aSLionel Sambuc template<typename T>
73*f4a2713aSLionel Sambuc void increment(T &x) {
74*f4a2713aSLionel Sambuc   (void)++x;
75*f4a2713aSLionel Sambuc }
76*f4a2713aSLionel Sambuc 
77*f4a2713aSLionel Sambuc struct Incrementable {
78*f4a2713aSLionel Sambuc   Incrementable &operator++();
79*f4a2713aSLionel Sambuc };
80*f4a2713aSLionel Sambuc 
81*f4a2713aSLionel Sambuc void test_increment(Incrementable inc) {
82*f4a2713aSLionel Sambuc   increment(inc);
83*f4a2713aSLionel Sambuc }
84*f4a2713aSLionel Sambuc 
85*f4a2713aSLionel Sambuc template<typename T>
86*f4a2713aSLionel Sambuc void add(const T &x) {
87*f4a2713aSLionel Sambuc   (void)(x + x);
88*f4a2713aSLionel Sambuc }
89*f4a2713aSLionel Sambuc 
90*f4a2713aSLionel Sambuc namespace PR6237 {
91*f4a2713aSLionel Sambuc   template <typename T>
92*f4a2713aSLionel Sambuc   void f(T t) {
93*f4a2713aSLionel Sambuc     t++;
94*f4a2713aSLionel Sambuc   }
95*f4a2713aSLionel Sambuc 
96*f4a2713aSLionel Sambuc   struct B { };
97*f4a2713aSLionel Sambuc   B operator++(B &, int);
98*f4a2713aSLionel Sambuc 
99*f4a2713aSLionel Sambuc   template void f(B);
100*f4a2713aSLionel Sambuc }
101*f4a2713aSLionel Sambuc 
102*f4a2713aSLionel Sambuc struct Addable {
103*f4a2713aSLionel Sambuc   Addable operator+(const Addable&) const;
104*f4a2713aSLionel Sambuc };
105*f4a2713aSLionel Sambuc 
106*f4a2713aSLionel Sambuc void test_add(Addable &a) {
107*f4a2713aSLionel Sambuc   add(a);
108*f4a2713aSLionel Sambuc }
109*f4a2713aSLionel Sambuc 
110*f4a2713aSLionel Sambuc struct CallOperator {
111*f4a2713aSLionel Sambuc   int &operator()(int);
112*f4a2713aSLionel Sambuc   double &operator()(double);
113*f4a2713aSLionel Sambuc };
114*f4a2713aSLionel Sambuc 
115*f4a2713aSLionel Sambuc template<typename Result, typename F, typename Arg1>
116*f4a2713aSLionel Sambuc Result test_call_operator(F f, Arg1 arg1) {
117*f4a2713aSLionel Sambuc   // PR5266: non-dependent invocations of a function call operator.
118*f4a2713aSLionel Sambuc   CallOperator call_op;
119*f4a2713aSLionel Sambuc   int &ir = call_op(17);
120*f4a2713aSLionel Sambuc   return f(arg1);
121*f4a2713aSLionel Sambuc }
122*f4a2713aSLionel Sambuc 
123*f4a2713aSLionel Sambuc void test_call_operator(CallOperator call_op, int i, double d) {
124*f4a2713aSLionel Sambuc   int &ir = test_call_operator<int&>(call_op, i);
125*f4a2713aSLionel Sambuc   double &dr = test_call_operator<double&>(call_op, d);
126*f4a2713aSLionel Sambuc }
127*f4a2713aSLionel Sambuc 
128*f4a2713aSLionel Sambuc template<typename T>
129*f4a2713aSLionel Sambuc void test_asm(T t) {
130*f4a2713aSLionel Sambuc   asm ("nop" : "=r"(*t) : "r"(*t)); // expected-error {{indirection requires pointer operand ('int' invalid)}}
131*f4a2713aSLionel Sambuc }
132*f4a2713aSLionel Sambuc 
133*f4a2713aSLionel Sambuc void test_asm() {
134*f4a2713aSLionel Sambuc   int* a;
135*f4a2713aSLionel Sambuc   test_asm(a);
136*f4a2713aSLionel Sambuc 
137*f4a2713aSLionel Sambuc   int b;
138*f4a2713aSLionel Sambuc   test_asm(b); // expected-note {{in instantiation of function template specialization 'test_asm<int>' requested here}}
139*f4a2713aSLionel Sambuc }
140*f4a2713aSLionel Sambuc 
141*f4a2713aSLionel Sambuc namespace PR6424 {
142*f4a2713aSLionel Sambuc   template<int I> struct X {
143*f4a2713aSLionel Sambuc     X() {
144*f4a2713aSLionel Sambuc       int *ip = I; // expected-error{{cannot initialize a variable of type 'int *' with an rvalue of type 'int'}}
145*f4a2713aSLionel Sambuc     }
146*f4a2713aSLionel Sambuc   };
147*f4a2713aSLionel Sambuc 
148*f4a2713aSLionel Sambuc   template<int> struct Y {
149*f4a2713aSLionel Sambuc     typedef X<7> X7;
150*f4a2713aSLionel Sambuc 
151*f4a2713aSLionel Sambuc     void f() { X7(); } // expected-note{{instantiation}}
152*f4a2713aSLionel Sambuc   };
153*f4a2713aSLionel Sambuc 
154*f4a2713aSLionel Sambuc   template void Y<3>::f();
155*f4a2713aSLionel Sambuc 
156*f4a2713aSLionel Sambuc   template<int I>
157*f4a2713aSLionel Sambuc   struct X2 {
158*f4a2713aSLionel Sambuc     void *operator new(__SIZE_TYPE__) {
159*f4a2713aSLionel Sambuc       int *ip = I; // expected-error{{cannot initialize a variable of type 'int *' with an rvalue of type 'int'}}
160*f4a2713aSLionel Sambuc       return ip;
161*f4a2713aSLionel Sambuc     }
162*f4a2713aSLionel Sambuc   };
163*f4a2713aSLionel Sambuc 
164*f4a2713aSLionel Sambuc   template<int> struct Y2 {
165*f4a2713aSLionel Sambuc     typedef X2<7> X;
166*f4a2713aSLionel Sambuc     void f() {
167*f4a2713aSLionel Sambuc       new X(); // expected-note{{instantiation of}}
168*f4a2713aSLionel Sambuc     }
169*f4a2713aSLionel Sambuc   };
170*f4a2713aSLionel Sambuc 
171*f4a2713aSLionel Sambuc   template void Y2<3>::f();
172*f4a2713aSLionel Sambuc 
173*f4a2713aSLionel Sambuc   template<typename T>
174*f4a2713aSLionel Sambuc   void rdar10283928(int count) {
175*f4a2713aSLionel Sambuc     (void)new char[count]();
176*f4a2713aSLionel Sambuc   }
177*f4a2713aSLionel Sambuc 
178*f4a2713aSLionel Sambuc   template void rdar10283928<int>(int);
179*f4a2713aSLionel Sambuc }
180*f4a2713aSLionel Sambuc 
181*f4a2713aSLionel Sambuc namespace PR10864 {
182*f4a2713aSLionel Sambuc   template<typename T> class Vals {};
183*f4a2713aSLionel Sambuc   template<> class Vals<int> { public: static const int i = 1; };
184*f4a2713aSLionel Sambuc   template<> class Vals<float> { public: static const double i; };
185*f4a2713aSLionel Sambuc   template<typename T> void test_asm_tied(T o) {
186*f4a2713aSLionel Sambuc     __asm("addl $1, %0" : "=r" (o) : "0"(Vals<T>::i)); // expected-error {{input with type 'double' matching output with type 'float'}}
187*f4a2713aSLionel Sambuc   }
188*f4a2713aSLionel Sambuc   void test_asm_tied() {
189*f4a2713aSLionel Sambuc     test_asm_tied(1);
190*f4a2713aSLionel Sambuc     test_asm_tied(1.f); // expected-note {{instantiation of}}
191*f4a2713aSLionel Sambuc   }
192*f4a2713aSLionel Sambuc }
193