blob: b0ac36870ca65850a0348ac8cd90081afd38f978 [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"
Daniel Dunbare4858a62008-08-11 03:45:03 +000015#include "clang/Basic/Diagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/Basic/LangOptions.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017using namespace clang;
18
Chris Lattner254be6a2008-11-22 08:32:36 +000019
20static DiagnosticBuilder Diag(Diagnostic &D, SourceLocation Loc,
21 SourceManager &SrcMgr, unsigned DiagID) {
22 return D.Report(FullSourceLoc(Loc, SrcMgr), DiagID);
23}
24
25
26
Reid Spencer5f016e22007-07-11 17:01:13 +000027/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
28///
29unsigned DeclSpec::getParsedSpecifiers() const {
30 unsigned Res = 0;
31 if (StorageClassSpec != SCS_unspecified ||
32 SCS_thread_specified)
33 Res |= PQ_StorageClassSpecifier;
Mike Stumpd4204332008-06-19 19:52:46 +000034
Reid Spencer5f016e22007-07-11 17:01:13 +000035 if (TypeQualifiers != TQ_unspecified)
36 Res |= PQ_TypeQualifier;
37
38 if (hasTypeSpecifier())
39 Res |= PQ_TypeSpecifier;
40
Douglas Gregorb48fe382008-10-31 09:07:45 +000041 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
Reid Spencer5f016e22007-07-11 17:01:13 +000042 Res |= PQ_FunctionSpecifier;
43 return Res;
44}
45
46const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
47 switch (S) {
48 default: assert(0 && "Unknown typespec!");
49 case DeclSpec::SCS_unspecified: return "unspecified";
50 case DeclSpec::SCS_typedef: return "typedef";
51 case DeclSpec::SCS_extern: return "extern";
52 case DeclSpec::SCS_static: return "static";
53 case DeclSpec::SCS_auto: return "auto";
54 case DeclSpec::SCS_register: return "register";
Sebastian Redl669d5d72008-11-14 23:42:31 +000055 case DeclSpec::SCS_mutable: return "mutable";
Reid Spencer5f016e22007-07-11 17:01:13 +000056 }
57}
58
Steve Naroff95324142008-02-12 04:08:59 +000059bool DeclSpec::BadSpecifier(SCS S, const char *&PrevSpec) {
Steve Naroff95324142008-02-12 04:08:59 +000060 PrevSpec = getSpecifierName(S);
Reid Spencer5f016e22007-07-11 17:01:13 +000061 return true;
62}
63
Steve Naroff95324142008-02-12 04:08:59 +000064bool DeclSpec::BadSpecifier(TSW W, const char *&PrevSpec) {
Reid Spencer5f016e22007-07-11 17:01:13 +000065 switch (W) {
Steve Naroff95324142008-02-12 04:08:59 +000066 case TSW_unspecified: PrevSpec = "unspecified"; break;
67 case TSW_short: PrevSpec = "short"; break;
68 case TSW_long: PrevSpec = "long"; break;
69 case TSW_longlong: PrevSpec = "long long"; break;
Reid Spencer5f016e22007-07-11 17:01:13 +000070 }
71 return true;
72}
73
Steve Naroff95324142008-02-12 04:08:59 +000074bool DeclSpec::BadSpecifier(TSC C, const char *&PrevSpec) {
Reid Spencer5f016e22007-07-11 17:01:13 +000075 switch (C) {
Steve Naroff95324142008-02-12 04:08:59 +000076 case TSC_unspecified: PrevSpec = "unspecified"; break;
77 case TSC_imaginary: PrevSpec = "imaginary"; break;
78 case TSC_complex: PrevSpec = "complex"; break;
Reid Spencer5f016e22007-07-11 17:01:13 +000079 }
80 return true;
81}
82
83
Steve Naroff95324142008-02-12 04:08:59 +000084bool DeclSpec::BadSpecifier(TSS S, const char *&PrevSpec) {
Reid Spencer5f016e22007-07-11 17:01:13 +000085 switch (S) {
Steve Naroff95324142008-02-12 04:08:59 +000086 case TSS_unspecified: PrevSpec = "unspecified"; break;
87 case TSS_signed: PrevSpec = "signed"; break;
88 case TSS_unsigned: PrevSpec = "unsigned"; break;
Reid Spencer5f016e22007-07-11 17:01:13 +000089 }
90 return true;
91}
92
93const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
94 switch (T) {
95 default: assert(0 && "Unknown typespec!");
96 case DeclSpec::TST_unspecified: return "unspecified";
97 case DeclSpec::TST_void: return "void";
98 case DeclSpec::TST_char: return "char";
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000099 case DeclSpec::TST_wchar: return "wchar_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000100 case DeclSpec::TST_int: return "int";
101 case DeclSpec::TST_float: return "float";
102 case DeclSpec::TST_double: return "double";
103 case DeclSpec::TST_bool: return "_Bool";
104 case DeclSpec::TST_decimal32: return "_Decimal32";
105 case DeclSpec::TST_decimal64: return "_Decimal64";
106 case DeclSpec::TST_decimal128: return "_Decimal128";
107 case DeclSpec::TST_enum: return "enum";
Chris Lattner99dc9142008-04-13 18:59:07 +0000108 case DeclSpec::TST_class: return "class";
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 case DeclSpec::TST_union: return "union";
110 case DeclSpec::TST_struct: return "struct";
111 case DeclSpec::TST_typedef: return "typedef";
Steve Naroffd1861fd2007-07-31 12:34:36 +0000112 case DeclSpec::TST_typeofType:
113 case DeclSpec::TST_typeofExpr: return "typeof";
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 }
115}
116
Steve Naroff95324142008-02-12 04:08:59 +0000117bool DeclSpec::BadSpecifier(TST T, const char *&PrevSpec) {
Steve Naroff95324142008-02-12 04:08:59 +0000118 PrevSpec = getSpecifierName(T);
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 return true;
120}
121
Steve Naroff95324142008-02-12 04:08:59 +0000122bool DeclSpec::BadSpecifier(TQ T, const char *&PrevSpec) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 switch (T) {
124 case DeclSpec::TQ_unspecified: PrevSpec = "unspecified"; break;
125 case DeclSpec::TQ_const: PrevSpec = "const"; break;
126 case DeclSpec::TQ_restrict: PrevSpec = "restrict"; break;
127 case DeclSpec::TQ_volatile: PrevSpec = "volatile"; break;
128 }
129 return true;
130}
131
132bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
133 const char *&PrevSpec) {
134 if (StorageClassSpec != SCS_unspecified)
Chris Lattner254be6a2008-11-22 08:32:36 +0000135 return BadSpecifier((SCS)StorageClassSpec, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 StorageClassSpec = S;
137 StorageClassSpecLoc = Loc;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000138 assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield");
Reid Spencer5f016e22007-07-11 17:01:13 +0000139 return false;
140}
141
142bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
143 const char *&PrevSpec) {
144 if (SCS_thread_specified) {
145 PrevSpec = "__thread";
146 return true;
147 }
148 SCS_thread_specified = true;
149 SCS_threadLoc = Loc;
150 return false;
151}
152
153
154/// These methods set the specified attribute of the DeclSpec, but return true
155/// and ignore the request if invalid (e.g. "extern" then "auto" is
156/// specified).
157bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
158 const char *&PrevSpec) {
159 if (TypeSpecWidth != TSW_unspecified &&
160 // Allow turning long -> long long.
161 (W != TSW_longlong || TypeSpecWidth != TSW_long))
Chris Lattner254be6a2008-11-22 08:32:36 +0000162 return BadSpecifier((TSW)TypeSpecWidth, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 TypeSpecWidth = W;
164 TSWLoc = Loc;
165 return false;
166}
167
168bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
169 const char *&PrevSpec) {
170 if (TypeSpecComplex != TSC_unspecified)
Chris Lattner254be6a2008-11-22 08:32:36 +0000171 return BadSpecifier((TSC)TypeSpecComplex, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 TypeSpecComplex = C;
173 TSCLoc = Loc;
174 return false;
175}
176
177bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
178 const char *&PrevSpec) {
179 if (TypeSpecSign != TSS_unspecified)
Chris Lattner254be6a2008-11-22 08:32:36 +0000180 return BadSpecifier((TSS)TypeSpecSign, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000181 TypeSpecSign = S;
182 TSSLoc = Loc;
183 return false;
184}
185
186bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
Argyrios Kyrtzidis39caa082008-08-01 10:35:27 +0000187 const char *&PrevSpec, Action::TypeTy *Rep) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 if (TypeSpecType != TST_unspecified)
Chris Lattner254be6a2008-11-22 08:32:36 +0000189 return BadSpecifier((TST)TypeSpecType, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 TypeSpecType = T;
191 TypeRep = Rep;
192 TSTLoc = Loc;
193 return false;
194}
195
196bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
197 const LangOptions &Lang) {
198 // Duplicates turn into warnings pre-C99.
199 if ((TypeQualifiers & T) && !Lang.C99)
200 return BadSpecifier(T, PrevSpec);
201 TypeQualifiers |= T;
202
203 switch (T) {
204 default: assert(0 && "Unknown type qualifier!");
205 case TQ_const: TQ_constLoc = Loc; break;
206 case TQ_restrict: TQ_restrictLoc = Loc; break;
207 case TQ_volatile: TQ_volatileLoc = Loc; break;
208 }
209 return false;
210}
211
212bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec){
213 // 'inline inline' is ok.
214 FS_inline_specified = true;
215 FS_inlineLoc = Loc;
216 return false;
217}
218
Douglas Gregorb48fe382008-10-31 09:07:45 +0000219bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec){
220 // 'virtual virtual' is ok.
221 FS_virtual_specified = true;
222 FS_virtualLoc = Loc;
223 return false;
224}
225
226bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec){
227 // 'explicit explicit' is ok.
228 FS_explicit_specified = true;
229 FS_explicitLoc = Loc;
230 return false;
231}
232
Reid Spencer5f016e22007-07-11 17:01:13 +0000233
234/// Finish - This does final analysis of the declspec, rejecting things like
235/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
236/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
237/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000238void DeclSpec::Finish(Diagnostic &D, SourceManager& SrcMgr,
239 const LangOptions &Lang) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000240 // Check the type specifier components first.
241
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000242 // signed/unsigned are only valid with int/char/wchar_t.
Reid Spencer5f016e22007-07-11 17:01:13 +0000243 if (TypeSpecSign != TSS_unspecified) {
244 if (TypeSpecType == TST_unspecified)
245 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000246 else if (TypeSpecType != TST_int &&
247 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000248 Diag(D, TSSLoc, SrcMgr, diag::err_invalid_sign_spec)
249 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000250 // signed double -> double.
251 TypeSpecSign = TSS_unspecified;
252 }
253 }
254
255 // Validate the width of the type.
256 switch (TypeSpecWidth) {
257 case TSW_unspecified: break;
258 case TSW_short: // short int
259 case TSW_longlong: // long long int
260 if (TypeSpecType == TST_unspecified)
261 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
262 else if (TypeSpecType != TST_int) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000263 Diag(D, TSWLoc, SrcMgr,
Reid Spencer5f016e22007-07-11 17:01:13 +0000264 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
Chris Lattner254be6a2008-11-22 08:32:36 +0000265 : diag::err_invalid_longlong_spec)
266 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000267 TypeSpecType = TST_int;
268 }
269 break;
270 case TSW_long: // long double, long int
271 if (TypeSpecType == TST_unspecified)
272 TypeSpecType = TST_int; // long -> long int.
273 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000274 Diag(D, TSWLoc, SrcMgr, diag::err_invalid_long_spec)
275 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000276 TypeSpecType = TST_int;
277 }
278 break;
279 }
280
281 // TODO: if the implementation does not implement _Complex or _Imaginary,
282 // disallow their use. Need information about the backend.
283 if (TypeSpecComplex != TSC_unspecified) {
284 if (TypeSpecType == TST_unspecified) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000285 Diag(D, TSCLoc, SrcMgr, diag::ext_plain_complex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000286 TypeSpecType = TST_double; // _Complex -> _Complex double.
287 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
288 // Note that this intentionally doesn't include _Complex _Bool.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000289 Diag(D, TSTLoc, SrcMgr, diag::ext_integer_complex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000290 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000291 Diag(D, TSCLoc, SrcMgr, diag::err_invalid_complex_spec)
292 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000293 TypeSpecComplex = TSC_unspecified;
294 }
295 }
296
297 // Verify __thread.
298 if (SCS_thread_specified) {
299 if (StorageClassSpec == SCS_unspecified) {
300 StorageClassSpec = SCS_extern; // '__thread int' -> 'extern __thread int'
301 } else if (StorageClassSpec != SCS_extern &&
302 StorageClassSpec != SCS_static) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000303 Diag(D, getStorageClassSpecLoc(), SrcMgr, diag::err_invalid_thread_spec)
304 << getSpecifierName((SCS)StorageClassSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000305 SCS_thread_specified = false;
306 }
307 }
308
309 // Okay, now we can infer the real type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000310
311 // TODO: return "auto function" and other bad things based on the real type.
312
313 // 'data definition has no type or storage class'?
314}
Daniel Dunbare4858a62008-08-11 03:45:03 +0000315