blob: 258dc87c446bf3ebff92b3bc4615f6f55393c0ec [file] [log] [blame]
Chris Lattner289ab7b2006-11-08 06:54:53 +00001//===--- SemaDeclSpec.cpp - Declaration Specifier Semantic Analysis -------===//
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner289ab7b2006-11-08 06:54:53 +000010// This file implements semantic analysis for declaration specifiers.
Chris Lattnerb9093cd2006-08-04 04:39:53 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner288e86ff12006-11-11 23:03:42 +000014#include "clang/Parse/DeclSpec.h"
Daniel Dunbarc74b5cc2008-08-11 03:45:03 +000015#include "clang/Basic/Diagnostic.h"
Chris Lattnerda48a8e2006-08-04 05:25:55 +000016#include "clang/Basic/LangOptions.h"
Chris Lattnerb9093cd2006-08-04 04:39:53 +000017using namespace clang;
18
Chris Lattner3b0f3ef2008-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
Chris Lattnerb9093cd2006-08-04 04:39:53 +000027/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
28///
29unsigned DeclSpec::getParsedSpecifiers() const {
30 unsigned Res = 0;
Chris Lattnerf63f89a2006-08-05 03:28:50 +000031 if (StorageClassSpec != SCS_unspecified ||
32 SCS_thread_specified)
Chris Lattnerb9093cd2006-08-04 04:39:53 +000033 Res |= PQ_StorageClassSpecifier;
Mike Stump65643c62008-06-19 19:52:46 +000034
Chris Lattner4d8f8732006-11-28 05:05:08 +000035 if (TypeQualifiers != TQ_unspecified)
Chris Lattnerb9093cd2006-08-04 04:39:53 +000036 Res |= PQ_TypeQualifier;
37
Chris Lattnerf055d432006-11-28 04:28:12 +000038 if (hasTypeSpecifier())
Chris Lattnerb9093cd2006-08-04 04:39:53 +000039 Res |= PQ_TypeSpecifier;
40
Douglas Gregor61956c42008-10-31 09:07:45 +000041 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
Chris Lattnerb9093cd2006-08-04 04:39:53 +000042 Res |= PQ_FunctionSpecifier;
43 return Res;
44}
45
Chris Lattner7bd11fe2007-01-23 04:35:33 +000046const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +000047 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 Redlccdfaba2008-11-14 23:42:31 +000055 case DeclSpec::SCS_mutable: return "mutable";
Chris Lattnerf63f89a2006-08-05 03:28:50 +000056 }
57}
58
Steve Naroffab468cb2008-02-12 04:08:59 +000059bool DeclSpec::BadSpecifier(SCS S, const char *&PrevSpec) {
Steve Naroffab468cb2008-02-12 04:08:59 +000060 PrevSpec = getSpecifierName(S);
Chris Lattnerf63f89a2006-08-05 03:28:50 +000061 return true;
62}
Chris Lattnerb9093cd2006-08-04 04:39:53 +000063
Steve Naroffab468cb2008-02-12 04:08:59 +000064bool DeclSpec::BadSpecifier(TSW W, const char *&PrevSpec) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +000065 switch (W) {
Steve Naroffab468cb2008-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;
Chris Lattnerb9093cd2006-08-04 04:39:53 +000070 }
71 return true;
72}
73
Steve Naroffab468cb2008-02-12 04:08:59 +000074bool DeclSpec::BadSpecifier(TSC C, const char *&PrevSpec) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +000075 switch (C) {
Steve Naroffab468cb2008-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;
Chris Lattnerb9093cd2006-08-04 04:39:53 +000079 }
80 return true;
81}
82
83
Steve Naroffab468cb2008-02-12 04:08:59 +000084bool DeclSpec::BadSpecifier(TSS S, const char *&PrevSpec) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +000085 switch (S) {
Steve Naroffab468cb2008-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;
Chris Lattnerb9093cd2006-08-04 04:39:53 +000089 }
90 return true;
91}
92
Chris Lattner69680ea2007-01-23 04:34:43 +000093const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +000094 switch (T) {
Chris Lattner839713c2006-08-04 06:15:52 +000095 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 Kyrtzidis40e9e482008-08-09 16:51:54 +000099 case DeclSpec::TST_wchar: return "wchar_t";
Chris Lattner839713c2006-08-04 06:15:52 +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";
Chris Lattnerda72c822006-08-13 22:16:42 +0000107 case DeclSpec::TST_enum: return "enum";
Chris Lattner861a2262008-04-13 18:59:07 +0000108 case DeclSpec::TST_class: return "class";
Chris Lattnerda72c822006-08-13 22:16:42 +0000109 case DeclSpec::TST_union: return "union";
110 case DeclSpec::TST_struct: return "struct";
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000111 case DeclSpec::TST_typedef: return "typedef";
Steve Naroffad373bd2007-07-31 12:34:36 +0000112 case DeclSpec::TST_typeofType:
113 case DeclSpec::TST_typeofExpr: return "typeof";
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000114 }
Chris Lattner839713c2006-08-04 06:15:52 +0000115}
116
Steve Naroffab468cb2008-02-12 04:08:59 +0000117bool DeclSpec::BadSpecifier(TST T, const char *&PrevSpec) {
Steve Naroffab468cb2008-02-12 04:08:59 +0000118 PrevSpec = getSpecifierName(T);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000119 return true;
120}
121
Steve Naroffab468cb2008-02-12 04:08:59 +0000122bool DeclSpec::BadSpecifier(TQ T, const char *&PrevSpec) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +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}
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000131
Chris Lattner4d8f8732006-11-28 05:05:08 +0000132bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
133 const char *&PrevSpec) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000134 if (StorageClassSpec != SCS_unspecified)
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000135 return BadSpecifier((SCS)StorageClassSpec, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000136 StorageClassSpec = S;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000137 StorageClassSpecLoc = Loc;
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000138 assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield");
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000139 return false;
140}
141
Chris Lattner4d8f8732006-11-28 05:05:08 +0000142bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
143 const char *&PrevSpec) {
Chris Lattner353f5742006-11-28 04:50:12 +0000144 if (SCS_thread_specified) {
145 PrevSpec = "__thread";
146 return true;
147 }
148 SCS_thread_specified = true;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000149 SCS_threadLoc = Loc;
Chris Lattner353f5742006-11-28 04:50:12 +0000150 return false;
151}
152
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000153
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000154/// 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).
Chris Lattnerb20e8942006-11-28 05:30:29 +0000157bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
158 const char *&PrevSpec) {
Chris Lattner353f5742006-11-28 04:50:12 +0000159 if (TypeSpecWidth != TSW_unspecified &&
160 // Allow turning long -> long long.
161 (W != TSW_longlong || TypeSpecWidth != TSW_long))
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000162 return BadSpecifier((TSW)TypeSpecWidth, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000163 TypeSpecWidth = W;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000164 TSWLoc = Loc;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000165 return false;
166}
167
Chris Lattnerb20e8942006-11-28 05:30:29 +0000168bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
169 const char *&PrevSpec) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000170 if (TypeSpecComplex != TSC_unspecified)
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000171 return BadSpecifier((TSC)TypeSpecComplex, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000172 TypeSpecComplex = C;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000173 TSCLoc = Loc;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000174 return false;
175}
176
Chris Lattnerb20e8942006-11-28 05:30:29 +0000177bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
178 const char *&PrevSpec) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000179 if (TypeSpecSign != TSS_unspecified)
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000180 return BadSpecifier((TSS)TypeSpecSign, PrevSpec);
Chris Lattnerdeb42f52006-08-04 05:26:52 +0000181 TypeSpecSign = S;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000182 TSSLoc = Loc;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000183 return false;
184}
185
Chris Lattnerb20e8942006-11-28 05:30:29 +0000186bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
Argyrios Kyrtzidis25d05e82008-08-01 10:35:27 +0000187 const char *&PrevSpec, Action::TypeTy *Rep) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000188 if (TypeSpecType != TST_unspecified)
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000189 return BadSpecifier((TST)TypeSpecType, PrevSpec);
Chris Lattnerdeb42f52006-08-04 05:26:52 +0000190 TypeSpecType = T;
Chris Lattnerb9d572a2007-01-23 04:58:34 +0000191 TypeRep = Rep;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000192 TSTLoc = Loc;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000193 return false;
194}
195
Chris Lattner60809f52006-11-28 05:18:46 +0000196bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000197 const LangOptions &Lang) {
198 // Duplicates turn into warnings pre-C99.
199 if ((TypeQualifiers & T) && !Lang.C99)
200 return BadSpecifier(T, PrevSpec);
201 TypeQualifiers |= T;
Chris Lattner60809f52006-11-28 05:18:46 +0000202
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 }
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000209 return false;
210}
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000211
Chris Lattner1b22eed2006-11-28 05:12:07 +0000212bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec){
Chris Lattnera925dc62006-11-28 04:33:46 +0000213 // 'inline inline' is ok.
214 FS_inline_specified = true;
Chris Lattner1b22eed2006-11-28 05:12:07 +0000215 FS_inlineLoc = Loc;
Chris Lattnera925dc62006-11-28 04:33:46 +0000216 return false;
217}
218
Douglas Gregor61956c42008-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
Chris Lattnera925dc62006-11-28 04:33:46 +0000233
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000234/// 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 Kremenekd4e5fba2007-12-11 21:27:55 +0000238void DeclSpec::Finish(Diagnostic &D, SourceManager& SrcMgr,
239 const LangOptions &Lang) {
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000240 // Check the type specifier components first.
241
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000242 // signed/unsigned are only valid with int/char/wchar_t.
Chris Lattner839713c2006-08-04 06:15:52 +0000243 if (TypeSpecSign != TSS_unspecified) {
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000244 if (TypeSpecType == TST_unspecified)
245 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000246 else if (TypeSpecType != TST_int &&
247 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000248 Diag(D, TSSLoc, SrcMgr, diag::err_invalid_sign_spec)
249 << getSpecifierName((TST)TypeSpecType);
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000250 // signed double -> double.
Chris Lattner839713c2006-08-04 06:15:52 +0000251 TypeSpecSign = TSS_unspecified;
252 }
253 }
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000254
Chris Lattner839713c2006-08-04 06:15:52 +0000255 // 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
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000260 if (TypeSpecType == TST_unspecified)
261 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
262 else if (TypeSpecType != TST_int) {
Ted Kremenekd4e5fba2007-12-11 21:27:55 +0000263 Diag(D, TSWLoc, SrcMgr,
Chris Lattner36982e42007-05-16 17:49:37 +0000264 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000265 : diag::err_invalid_longlong_spec)
266 << getSpecifierName((TST)TypeSpecType);
Chris Lattner839713c2006-08-04 06:15:52 +0000267 TypeSpecType = TST_int;
268 }
269 break;
270 case TSW_long: // long double, long int
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000271 if (TypeSpecType == TST_unspecified)
272 TypeSpecType = TST_int; // long -> long int.
273 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000274 Diag(D, TSWLoc, SrcMgr, diag::err_invalid_long_spec)
275 << getSpecifierName((TST)TypeSpecType);
Chris Lattner839713c2006-08-04 06:15:52 +0000276 TypeSpecType = TST_int;
277 }
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000278 break;
Chris Lattner839713c2006-08-04 06:15:52 +0000279 }
280
Chris Lattner0e894622006-08-13 19:58:17 +0000281 // TODO: if the implementation does not implement _Complex or _Imaginary,
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000282 // disallow their use. Need information about the backend.
283 if (TypeSpecComplex != TSC_unspecified) {
284 if (TypeSpecType == TST_unspecified) {
Ted Kremenekd4e5fba2007-12-11 21:27:55 +0000285 Diag(D, TSCLoc, SrcMgr, diag::ext_plain_complex);
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000286 TypeSpecType = TST_double; // _Complex -> _Complex double.
287 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000288 // Note that this intentionally doesn't include _Complex _Bool.
Ted Kremenekd4e5fba2007-12-11 21:27:55 +0000289 Diag(D, TSTLoc, SrcMgr, diag::ext_integer_complex);
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000290 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000291 Diag(D, TSCLoc, SrcMgr, diag::err_invalid_complex_spec)
292 << getSpecifierName((TST)TypeSpecType);
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000293 TypeSpecComplex = TSC_unspecified;
294 }
295 }
Chris Lattner1ae23292006-08-04 06:42:31 +0000296
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000297 // 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 Lattner3b0f3ef2008-11-22 08:32:36 +0000303 Diag(D, getStorageClassSpecLoc(), SrcMgr, diag::err_invalid_thread_spec)
304 << getSpecifierName((SCS)StorageClassSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000305 SCS_thread_specified = false;
306 }
307 }
Chris Lattner8e90ef62006-08-05 03:30:45 +0000308
309 // Okay, now we can infer the real type.
Chris Lattner8e90ef62006-08-05 03:30:45 +0000310
Chris Lattner0e894622006-08-13 19:58:17 +0000311 // TODO: return "auto function" and other bad things based on the real type.
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000312
Chris Lattner1ae23292006-08-04 06:42:31 +0000313 // 'data definition has no type or storage class'?
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000314}
Daniel Dunbarc74b5cc2008-08-11 03:45:03 +0000315
Sebastian Redla2b5e312008-12-28 15:28:59 +0000316bool DeclSpec::isMissingDeclaratorOk() {
317 TST tst = getTypeSpecType();
318 return (tst == TST_union
319 || tst == TST_struct
320 || tst == TST_class
321 || tst == TST_enum
322 ) && getTypeRep() != 0;
323}