blob: d97dcfac807ef76f2e0dcc92c43333e32bf8ad57 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- SemaDeclSpec.cpp - Declaration Specifier Semantic Analysis -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for declaration specifiers.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/DeclSpec.h"
15#include "clang/Basic/LangOptions.h"
16#include "clang/Basic/SourceLocation.h"
17using namespace clang;
18
19/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
20///
21unsigned DeclSpec::getParsedSpecifiers() const {
22 unsigned Res = 0;
23 if (StorageClassSpec != SCS_unspecified ||
24 SCS_thread_specified)
25 Res |= PQ_StorageClassSpecifier;
Mike Stumpd4204332008-06-19 19:52:46 +000026
Reid Spencer5f016e22007-07-11 17:01:13 +000027 if (TypeQualifiers != TQ_unspecified)
28 Res |= PQ_TypeQualifier;
29
30 if (hasTypeSpecifier())
31 Res |= PQ_TypeSpecifier;
32
33 if (FS_inline_specified)
34 Res |= PQ_FunctionSpecifier;
35 return Res;
36}
37
38const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
39 switch (S) {
40 default: assert(0 && "Unknown typespec!");
41 case DeclSpec::SCS_unspecified: return "unspecified";
42 case DeclSpec::SCS_typedef: return "typedef";
43 case DeclSpec::SCS_extern: return "extern";
44 case DeclSpec::SCS_static: return "static";
45 case DeclSpec::SCS_auto: return "auto";
46 case DeclSpec::SCS_register: return "register";
47 }
48}
49
Steve Naroff95324142008-02-12 04:08:59 +000050bool DeclSpec::BadSpecifier(SCS S, const char *&PrevSpec) {
Steve Naroff95324142008-02-12 04:08:59 +000051 PrevSpec = getSpecifierName(S);
Reid Spencer5f016e22007-07-11 17:01:13 +000052 return true;
53}
54
Steve Naroff95324142008-02-12 04:08:59 +000055bool DeclSpec::BadSpecifier(TSW W, const char *&PrevSpec) {
Reid Spencer5f016e22007-07-11 17:01:13 +000056 switch (W) {
Steve Naroff95324142008-02-12 04:08:59 +000057 case TSW_unspecified: PrevSpec = "unspecified"; break;
58 case TSW_short: PrevSpec = "short"; break;
59 case TSW_long: PrevSpec = "long"; break;
60 case TSW_longlong: PrevSpec = "long long"; break;
Reid Spencer5f016e22007-07-11 17:01:13 +000061 }
62 return true;
63}
64
Steve Naroff95324142008-02-12 04:08:59 +000065bool DeclSpec::BadSpecifier(TSC C, const char *&PrevSpec) {
Reid Spencer5f016e22007-07-11 17:01:13 +000066 switch (C) {
Steve Naroff95324142008-02-12 04:08:59 +000067 case TSC_unspecified: PrevSpec = "unspecified"; break;
68 case TSC_imaginary: PrevSpec = "imaginary"; break;
69 case TSC_complex: PrevSpec = "complex"; break;
Reid Spencer5f016e22007-07-11 17:01:13 +000070 }
71 return true;
72}
73
74
Steve Naroff95324142008-02-12 04:08:59 +000075bool DeclSpec::BadSpecifier(TSS S, const char *&PrevSpec) {
Reid Spencer5f016e22007-07-11 17:01:13 +000076 switch (S) {
Steve Naroff95324142008-02-12 04:08:59 +000077 case TSS_unspecified: PrevSpec = "unspecified"; break;
78 case TSS_signed: PrevSpec = "signed"; break;
79 case TSS_unsigned: PrevSpec = "unsigned"; break;
Reid Spencer5f016e22007-07-11 17:01:13 +000080 }
81 return true;
82}
83
84const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
85 switch (T) {
86 default: assert(0 && "Unknown typespec!");
87 case DeclSpec::TST_unspecified: return "unspecified";
88 case DeclSpec::TST_void: return "void";
89 case DeclSpec::TST_char: return "char";
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000090 case DeclSpec::TST_wchar: return "wchar_t";
Reid Spencer5f016e22007-07-11 17:01:13 +000091 case DeclSpec::TST_int: return "int";
92 case DeclSpec::TST_float: return "float";
93 case DeclSpec::TST_double: return "double";
94 case DeclSpec::TST_bool: return "_Bool";
95 case DeclSpec::TST_decimal32: return "_Decimal32";
96 case DeclSpec::TST_decimal64: return "_Decimal64";
97 case DeclSpec::TST_decimal128: return "_Decimal128";
98 case DeclSpec::TST_enum: return "enum";
Chris Lattner99dc9142008-04-13 18:59:07 +000099 case DeclSpec::TST_class: return "class";
Reid Spencer5f016e22007-07-11 17:01:13 +0000100 case DeclSpec::TST_union: return "union";
101 case DeclSpec::TST_struct: return "struct";
102 case DeclSpec::TST_typedef: return "typedef";
Steve Naroffd1861fd2007-07-31 12:34:36 +0000103 case DeclSpec::TST_typeofType:
104 case DeclSpec::TST_typeofExpr: return "typeof";
Reid Spencer5f016e22007-07-11 17:01:13 +0000105 }
106}
107
Steve Naroff95324142008-02-12 04:08:59 +0000108bool DeclSpec::BadSpecifier(TST T, const char *&PrevSpec) {
Steve Naroff95324142008-02-12 04:08:59 +0000109 PrevSpec = getSpecifierName(T);
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 return true;
111}
112
Steve Naroff95324142008-02-12 04:08:59 +0000113bool DeclSpec::BadSpecifier(TQ T, const char *&PrevSpec) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 switch (T) {
115 case DeclSpec::TQ_unspecified: PrevSpec = "unspecified"; break;
116 case DeclSpec::TQ_const: PrevSpec = "const"; break;
117 case DeclSpec::TQ_restrict: PrevSpec = "restrict"; break;
118 case DeclSpec::TQ_volatile: PrevSpec = "volatile"; break;
119 }
120 return true;
121}
122
123bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
124 const char *&PrevSpec) {
125 if (StorageClassSpec != SCS_unspecified)
Chris Lattner2efcd2f2007-12-02 00:47:03 +0000126 return BadSpecifier( (SCS)StorageClassSpec, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 StorageClassSpec = S;
128 StorageClassSpecLoc = Loc;
129 return false;
130}
131
132bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
133 const char *&PrevSpec) {
134 if (SCS_thread_specified) {
135 PrevSpec = "__thread";
136 return true;
137 }
138 SCS_thread_specified = true;
139 SCS_threadLoc = Loc;
140 return false;
141}
142
143
144/// These methods set the specified attribute of the DeclSpec, but return true
145/// and ignore the request if invalid (e.g. "extern" then "auto" is
146/// specified).
147bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
148 const char *&PrevSpec) {
149 if (TypeSpecWidth != TSW_unspecified &&
150 // Allow turning long -> long long.
151 (W != TSW_longlong || TypeSpecWidth != TSW_long))
Chris Lattner2efcd2f2007-12-02 00:47:03 +0000152 return BadSpecifier( (TSW)TypeSpecWidth, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000153 TypeSpecWidth = W;
154 TSWLoc = Loc;
155 return false;
156}
157
158bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
159 const char *&PrevSpec) {
160 if (TypeSpecComplex != TSC_unspecified)
Chris Lattner2efcd2f2007-12-02 00:47:03 +0000161 return BadSpecifier( (TSC)TypeSpecComplex, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000162 TypeSpecComplex = C;
163 TSCLoc = Loc;
164 return false;
165}
166
167bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
168 const char *&PrevSpec) {
169 if (TypeSpecSign != TSS_unspecified)
Chris Lattner2efcd2f2007-12-02 00:47:03 +0000170 return BadSpecifier( (TSS)TypeSpecSign, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 TypeSpecSign = S;
172 TSSLoc = Loc;
173 return false;
174}
175
176bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
Argyrios Kyrtzidis39caa082008-08-01 10:35:27 +0000177 const char *&PrevSpec, Action::TypeTy *Rep) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 if (TypeSpecType != TST_unspecified)
Chris Lattner2efcd2f2007-12-02 00:47:03 +0000179 return BadSpecifier( (TST)TypeSpecType, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000180 TypeSpecType = T;
181 TypeRep = Rep;
182 TSTLoc = Loc;
183 return false;
184}
185
186bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
187 const LangOptions &Lang) {
188 // Duplicates turn into warnings pre-C99.
189 if ((TypeQualifiers & T) && !Lang.C99)
190 return BadSpecifier(T, PrevSpec);
191 TypeQualifiers |= T;
192
193 switch (T) {
194 default: assert(0 && "Unknown type qualifier!");
195 case TQ_const: TQ_constLoc = Loc; break;
196 case TQ_restrict: TQ_restrictLoc = Loc; break;
197 case TQ_volatile: TQ_volatileLoc = Loc; break;
198 }
199 return false;
200}
201
202bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec){
203 // 'inline inline' is ok.
204 FS_inline_specified = true;
205 FS_inlineLoc = Loc;
206 return false;
207}
208
209
210/// Finish - This does final analysis of the declspec, rejecting things like
211/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
212/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
213/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000214void DeclSpec::Finish(Diagnostic &D, SourceManager& SrcMgr,
215 const LangOptions &Lang) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000216 // Check the type specifier components first.
217
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000218 // signed/unsigned are only valid with int/char/wchar_t.
Reid Spencer5f016e22007-07-11 17:01:13 +0000219 if (TypeSpecSign != TSS_unspecified) {
220 if (TypeSpecType == TST_unspecified)
221 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000222 else if (TypeSpecType != TST_int &&
223 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000224 Diag(D, TSSLoc, SrcMgr, diag::err_invalid_sign_spec,
Chris Lattner2efcd2f2007-12-02 00:47:03 +0000225 getSpecifierName( (TST)TypeSpecType));
Reid Spencer5f016e22007-07-11 17:01:13 +0000226 // signed double -> double.
227 TypeSpecSign = TSS_unspecified;
228 }
229 }
230
231 // Validate the width of the type.
232 switch (TypeSpecWidth) {
233 case TSW_unspecified: break;
234 case TSW_short: // short int
235 case TSW_longlong: // long long int
236 if (TypeSpecType == TST_unspecified)
237 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
238 else if (TypeSpecType != TST_int) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000239 Diag(D, TSWLoc, SrcMgr,
Reid Spencer5f016e22007-07-11 17:01:13 +0000240 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
241 : diag::err_invalid_longlong_spec,
Chris Lattner2efcd2f2007-12-02 00:47:03 +0000242 getSpecifierName( (TST)TypeSpecType));
Reid Spencer5f016e22007-07-11 17:01:13 +0000243 TypeSpecType = TST_int;
244 }
245 break;
246 case TSW_long: // long double, long int
247 if (TypeSpecType == TST_unspecified)
248 TypeSpecType = TST_int; // long -> long int.
249 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000250 Diag(D, TSWLoc, SrcMgr, diag::err_invalid_long_spec,
Chris Lattner2efcd2f2007-12-02 00:47:03 +0000251 getSpecifierName( (TST)TypeSpecType));
Reid Spencer5f016e22007-07-11 17:01:13 +0000252 TypeSpecType = TST_int;
253 }
254 break;
255 }
256
257 // TODO: if the implementation does not implement _Complex or _Imaginary,
258 // disallow their use. Need information about the backend.
259 if (TypeSpecComplex != TSC_unspecified) {
260 if (TypeSpecType == TST_unspecified) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000261 Diag(D, TSCLoc, SrcMgr, diag::ext_plain_complex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000262 TypeSpecType = TST_double; // _Complex -> _Complex double.
263 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
264 // Note that this intentionally doesn't include _Complex _Bool.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000265 Diag(D, TSTLoc, SrcMgr, diag::ext_integer_complex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000266 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000267 Diag(D, TSCLoc, SrcMgr, diag::err_invalid_complex_spec,
Chris Lattner2efcd2f2007-12-02 00:47:03 +0000268 getSpecifierName( (TST)TypeSpecType));
Reid Spencer5f016e22007-07-11 17:01:13 +0000269 TypeSpecComplex = TSC_unspecified;
270 }
271 }
272
273 // Verify __thread.
274 if (SCS_thread_specified) {
275 if (StorageClassSpec == SCS_unspecified) {
276 StorageClassSpec = SCS_extern; // '__thread int' -> 'extern __thread int'
277 } else if (StorageClassSpec != SCS_extern &&
278 StorageClassSpec != SCS_static) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000279 Diag(D, getStorageClassSpecLoc(), SrcMgr, diag::err_invalid_thread_spec,
Chris Lattner2efcd2f2007-12-02 00:47:03 +0000280 getSpecifierName( (SCS)StorageClassSpec));
Reid Spencer5f016e22007-07-11 17:01:13 +0000281 SCS_thread_specified = false;
282 }
283 }
284
285 // Okay, now we can infer the real type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000286
287 // TODO: return "auto function" and other bad things based on the real type.
288
289 // 'data definition has no type or storage class'?
290}