blob: 5c26ec6857c409f2947e972213cb41f746cb5b4e [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 Lattner839713c2006-08-04 06:15:52 +000017#include "clang/Basic/SourceLocation.h"
Chris Lattnerb9093cd2006-08-04 04:39:53 +000018using namespace clang;
19
20/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
21///
22unsigned DeclSpec::getParsedSpecifiers() const {
23 unsigned Res = 0;
Chris Lattnerf63f89a2006-08-05 03:28:50 +000024 if (StorageClassSpec != SCS_unspecified ||
25 SCS_thread_specified)
Chris Lattnerb9093cd2006-08-04 04:39:53 +000026 Res |= PQ_StorageClassSpecifier;
Mike Stump65643c62008-06-19 19:52:46 +000027
Chris Lattner4d8f8732006-11-28 05:05:08 +000028 if (TypeQualifiers != TQ_unspecified)
Chris Lattnerb9093cd2006-08-04 04:39:53 +000029 Res |= PQ_TypeQualifier;
30
Chris Lattnerf055d432006-11-28 04:28:12 +000031 if (hasTypeSpecifier())
Chris Lattnerb9093cd2006-08-04 04:39:53 +000032 Res |= PQ_TypeSpecifier;
33
Chris Lattnerf63f89a2006-08-05 03:28:50 +000034 if (FS_inline_specified)
Chris Lattnerb9093cd2006-08-04 04:39:53 +000035 Res |= PQ_FunctionSpecifier;
36 return Res;
37}
38
Chris Lattner7bd11fe2007-01-23 04:35:33 +000039const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +000040 switch (S) {
41 default: assert(0 && "Unknown typespec!");
42 case DeclSpec::SCS_unspecified: return "unspecified";
43 case DeclSpec::SCS_typedef: return "typedef";
44 case DeclSpec::SCS_extern: return "extern";
45 case DeclSpec::SCS_static: return "static";
46 case DeclSpec::SCS_auto: return "auto";
47 case DeclSpec::SCS_register: return "register";
48 }
49}
50
Steve Naroffab468cb2008-02-12 04:08:59 +000051bool DeclSpec::BadSpecifier(SCS S, const char *&PrevSpec) {
Steve Naroffab468cb2008-02-12 04:08:59 +000052 PrevSpec = getSpecifierName(S);
Chris Lattnerf63f89a2006-08-05 03:28:50 +000053 return true;
54}
Chris Lattnerb9093cd2006-08-04 04:39:53 +000055
Steve Naroffab468cb2008-02-12 04:08:59 +000056bool DeclSpec::BadSpecifier(TSW W, const char *&PrevSpec) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +000057 switch (W) {
Steve Naroffab468cb2008-02-12 04:08:59 +000058 case TSW_unspecified: PrevSpec = "unspecified"; break;
59 case TSW_short: PrevSpec = "short"; break;
60 case TSW_long: PrevSpec = "long"; break;
61 case TSW_longlong: PrevSpec = "long long"; break;
Chris Lattnerb9093cd2006-08-04 04:39:53 +000062 }
63 return true;
64}
65
Steve Naroffab468cb2008-02-12 04:08:59 +000066bool DeclSpec::BadSpecifier(TSC C, const char *&PrevSpec) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +000067 switch (C) {
Steve Naroffab468cb2008-02-12 04:08:59 +000068 case TSC_unspecified: PrevSpec = "unspecified"; break;
69 case TSC_imaginary: PrevSpec = "imaginary"; break;
70 case TSC_complex: PrevSpec = "complex"; break;
Chris Lattnerb9093cd2006-08-04 04:39:53 +000071 }
72 return true;
73}
74
75
Steve Naroffab468cb2008-02-12 04:08:59 +000076bool DeclSpec::BadSpecifier(TSS S, const char *&PrevSpec) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +000077 switch (S) {
Steve Naroffab468cb2008-02-12 04:08:59 +000078 case TSS_unspecified: PrevSpec = "unspecified"; break;
79 case TSS_signed: PrevSpec = "signed"; break;
80 case TSS_unsigned: PrevSpec = "unsigned"; break;
Chris Lattnerb9093cd2006-08-04 04:39:53 +000081 }
82 return true;
83}
84
Chris Lattner69680ea2007-01-23 04:34:43 +000085const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +000086 switch (T) {
Chris Lattner839713c2006-08-04 06:15:52 +000087 default: assert(0 && "Unknown typespec!");
88 case DeclSpec::TST_unspecified: return "unspecified";
89 case DeclSpec::TST_void: return "void";
90 case DeclSpec::TST_char: return "char";
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +000091 case DeclSpec::TST_wchar: return "wchar_t";
Chris Lattner839713c2006-08-04 06:15:52 +000092 case DeclSpec::TST_int: return "int";
93 case DeclSpec::TST_float: return "float";
94 case DeclSpec::TST_double: return "double";
95 case DeclSpec::TST_bool: return "_Bool";
96 case DeclSpec::TST_decimal32: return "_Decimal32";
97 case DeclSpec::TST_decimal64: return "_Decimal64";
98 case DeclSpec::TST_decimal128: return "_Decimal128";
Chris Lattnerda72c822006-08-13 22:16:42 +000099 case DeclSpec::TST_enum: return "enum";
Chris Lattner861a2262008-04-13 18:59:07 +0000100 case DeclSpec::TST_class: return "class";
Chris Lattnerda72c822006-08-13 22:16:42 +0000101 case DeclSpec::TST_union: return "union";
102 case DeclSpec::TST_struct: return "struct";
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000103 case DeclSpec::TST_typedef: return "typedef";
Steve Naroffad373bd2007-07-31 12:34:36 +0000104 case DeclSpec::TST_typeofType:
105 case DeclSpec::TST_typeofExpr: return "typeof";
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000106 }
Chris Lattner839713c2006-08-04 06:15:52 +0000107}
108
Steve Naroffab468cb2008-02-12 04:08:59 +0000109bool DeclSpec::BadSpecifier(TST T, const char *&PrevSpec) {
Steve Naroffab468cb2008-02-12 04:08:59 +0000110 PrevSpec = getSpecifierName(T);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000111 return true;
112}
113
Steve Naroffab468cb2008-02-12 04:08:59 +0000114bool DeclSpec::BadSpecifier(TQ T, const char *&PrevSpec) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000115 switch (T) {
116 case DeclSpec::TQ_unspecified: PrevSpec = "unspecified"; break;
117 case DeclSpec::TQ_const: PrevSpec = "const"; break;
118 case DeclSpec::TQ_restrict: PrevSpec = "restrict"; break;
119 case DeclSpec::TQ_volatile: PrevSpec = "volatile"; break;
120 }
121 return true;
122}
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000123
Chris Lattner4d8f8732006-11-28 05:05:08 +0000124bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
125 const char *&PrevSpec) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000126 if (StorageClassSpec != SCS_unspecified)
Chris Lattner9724ce12007-12-02 00:47:03 +0000127 return BadSpecifier( (SCS)StorageClassSpec, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000128 StorageClassSpec = S;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000129 StorageClassSpecLoc = Loc;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000130 return false;
131}
132
Chris Lattner4d8f8732006-11-28 05:05:08 +0000133bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
134 const char *&PrevSpec) {
Chris Lattner353f5742006-11-28 04:50:12 +0000135 if (SCS_thread_specified) {
136 PrevSpec = "__thread";
137 return true;
138 }
139 SCS_thread_specified = true;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000140 SCS_threadLoc = Loc;
Chris Lattner353f5742006-11-28 04:50:12 +0000141 return false;
142}
143
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000144
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000145/// These methods set the specified attribute of the DeclSpec, but return true
146/// and ignore the request if invalid (e.g. "extern" then "auto" is
147/// specified).
Chris Lattnerb20e8942006-11-28 05:30:29 +0000148bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
149 const char *&PrevSpec) {
Chris Lattner353f5742006-11-28 04:50:12 +0000150 if (TypeSpecWidth != TSW_unspecified &&
151 // Allow turning long -> long long.
152 (W != TSW_longlong || TypeSpecWidth != TSW_long))
Chris Lattner9724ce12007-12-02 00:47:03 +0000153 return BadSpecifier( (TSW)TypeSpecWidth, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000154 TypeSpecWidth = W;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000155 TSWLoc = Loc;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000156 return false;
157}
158
Chris Lattnerb20e8942006-11-28 05:30:29 +0000159bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
160 const char *&PrevSpec) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000161 if (TypeSpecComplex != TSC_unspecified)
Chris Lattner9724ce12007-12-02 00:47:03 +0000162 return BadSpecifier( (TSC)TypeSpecComplex, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000163 TypeSpecComplex = C;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000164 TSCLoc = Loc;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000165 return false;
166}
167
Chris Lattnerb20e8942006-11-28 05:30:29 +0000168bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
169 const char *&PrevSpec) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000170 if (TypeSpecSign != TSS_unspecified)
Chris Lattner9724ce12007-12-02 00:47:03 +0000171 return BadSpecifier( (TSS)TypeSpecSign, PrevSpec);
Chris Lattnerdeb42f52006-08-04 05:26:52 +0000172 TypeSpecSign = S;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000173 TSSLoc = Loc;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000174 return false;
175}
176
Chris Lattnerb20e8942006-11-28 05:30:29 +0000177bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
Argyrios Kyrtzidis25d05e82008-08-01 10:35:27 +0000178 const char *&PrevSpec, Action::TypeTy *Rep) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000179 if (TypeSpecType != TST_unspecified)
Chris Lattner9724ce12007-12-02 00:47:03 +0000180 return BadSpecifier( (TST)TypeSpecType, PrevSpec);
Chris Lattnerdeb42f52006-08-04 05:26:52 +0000181 TypeSpecType = T;
Chris Lattnerb9d572a2007-01-23 04:58:34 +0000182 TypeRep = Rep;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000183 TSTLoc = Loc;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000184 return false;
185}
186
Chris Lattner60809f52006-11-28 05:18:46 +0000187bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000188 const LangOptions &Lang) {
189 // Duplicates turn into warnings pre-C99.
190 if ((TypeQualifiers & T) && !Lang.C99)
191 return BadSpecifier(T, PrevSpec);
192 TypeQualifiers |= T;
Chris Lattner60809f52006-11-28 05:18:46 +0000193
194 switch (T) {
195 default: assert(0 && "Unknown type qualifier!");
196 case TQ_const: TQ_constLoc = Loc; break;
197 case TQ_restrict: TQ_restrictLoc = Loc; break;
198 case TQ_volatile: TQ_volatileLoc = Loc; break;
199 }
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000200 return false;
201}
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000202
Chris Lattner1b22eed2006-11-28 05:12:07 +0000203bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec){
Chris Lattnera925dc62006-11-28 04:33:46 +0000204 // 'inline inline' is ok.
205 FS_inline_specified = true;
Chris Lattner1b22eed2006-11-28 05:12:07 +0000206 FS_inlineLoc = Loc;
Chris Lattnera925dc62006-11-28 04:33:46 +0000207 return false;
208}
209
210
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000211/// Finish - This does final analysis of the declspec, rejecting things like
212/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
213/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
214/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Ted Kremenekd4e5fba2007-12-11 21:27:55 +0000215void DeclSpec::Finish(Diagnostic &D, SourceManager& SrcMgr,
216 const LangOptions &Lang) {
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000217 // Check the type specifier components first.
218
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000219 // signed/unsigned are only valid with int/char/wchar_t.
Chris Lattner839713c2006-08-04 06:15:52 +0000220 if (TypeSpecSign != TSS_unspecified) {
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000221 if (TypeSpecType == TST_unspecified)
222 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000223 else if (TypeSpecType != TST_int &&
224 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
Ted Kremenekd4e5fba2007-12-11 21:27:55 +0000225 Diag(D, TSSLoc, SrcMgr, diag::err_invalid_sign_spec,
Chris Lattner9724ce12007-12-02 00:47:03 +0000226 getSpecifierName( (TST)TypeSpecType));
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000227 // signed double -> double.
Chris Lattner839713c2006-08-04 06:15:52 +0000228 TypeSpecSign = TSS_unspecified;
229 }
230 }
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000231
Chris Lattner839713c2006-08-04 06:15:52 +0000232 // Validate the width of the type.
233 switch (TypeSpecWidth) {
234 case TSW_unspecified: break;
235 case TSW_short: // short int
236 case TSW_longlong: // long long int
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000237 if (TypeSpecType == TST_unspecified)
238 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
239 else if (TypeSpecType != TST_int) {
Ted Kremenekd4e5fba2007-12-11 21:27:55 +0000240 Diag(D, TSWLoc, SrcMgr,
Chris Lattner36982e42007-05-16 17:49:37 +0000241 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
242 : diag::err_invalid_longlong_spec,
Chris Lattner9724ce12007-12-02 00:47:03 +0000243 getSpecifierName( (TST)TypeSpecType));
Chris Lattner839713c2006-08-04 06:15:52 +0000244 TypeSpecType = TST_int;
245 }
246 break;
247 case TSW_long: // long double, long int
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000248 if (TypeSpecType == TST_unspecified)
249 TypeSpecType = TST_int; // long -> long int.
250 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Ted Kremenekd4e5fba2007-12-11 21:27:55 +0000251 Diag(D, TSWLoc, SrcMgr, diag::err_invalid_long_spec,
Chris Lattner9724ce12007-12-02 00:47:03 +0000252 getSpecifierName( (TST)TypeSpecType));
Chris Lattner839713c2006-08-04 06:15:52 +0000253 TypeSpecType = TST_int;
254 }
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000255 break;
Chris Lattner839713c2006-08-04 06:15:52 +0000256 }
257
Chris Lattner0e894622006-08-13 19:58:17 +0000258 // TODO: if the implementation does not implement _Complex or _Imaginary,
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000259 // disallow their use. Need information about the backend.
260 if (TypeSpecComplex != TSC_unspecified) {
261 if (TypeSpecType == TST_unspecified) {
Ted Kremenekd4e5fba2007-12-11 21:27:55 +0000262 Diag(D, TSCLoc, SrcMgr, diag::ext_plain_complex);
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000263 TypeSpecType = TST_double; // _Complex -> _Complex double.
264 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000265 // Note that this intentionally doesn't include _Complex _Bool.
Ted Kremenekd4e5fba2007-12-11 21:27:55 +0000266 Diag(D, TSTLoc, SrcMgr, diag::ext_integer_complex);
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000267 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Ted Kremenekd4e5fba2007-12-11 21:27:55 +0000268 Diag(D, TSCLoc, SrcMgr, diag::err_invalid_complex_spec,
Chris Lattner9724ce12007-12-02 00:47:03 +0000269 getSpecifierName( (TST)TypeSpecType));
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000270 TypeSpecComplex = TSC_unspecified;
271 }
272 }
Chris Lattner1ae23292006-08-04 06:42:31 +0000273
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000274 // Verify __thread.
275 if (SCS_thread_specified) {
276 if (StorageClassSpec == SCS_unspecified) {
277 StorageClassSpec = SCS_extern; // '__thread int' -> 'extern __thread int'
278 } else if (StorageClassSpec != SCS_extern &&
279 StorageClassSpec != SCS_static) {
Ted Kremenekd4e5fba2007-12-11 21:27:55 +0000280 Diag(D, getStorageClassSpecLoc(), SrcMgr, diag::err_invalid_thread_spec,
Chris Lattner9724ce12007-12-02 00:47:03 +0000281 getSpecifierName( (SCS)StorageClassSpec));
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000282 SCS_thread_specified = false;
283 }
284 }
Chris Lattner8e90ef62006-08-05 03:30:45 +0000285
286 // Okay, now we can infer the real type.
Chris Lattner8e90ef62006-08-05 03:30:45 +0000287
Chris Lattner0e894622006-08-13 19:58:17 +0000288 // TODO: return "auto function" and other bad things based on the real type.
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000289
Chris Lattner1ae23292006-08-04 06:42:31 +0000290 // 'data definition has no type or storage class'?
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000291}
Daniel Dunbarc74b5cc2008-08-11 03:45:03 +0000292
293void DeclSpec::Diag(Diagnostic &D, SourceLocation Loc, SourceManager& SrcMgr,
294 unsigned DiagID) {
295 D.Report(FullSourceLoc(Loc,SrcMgr), DiagID);
296}
297
298void DeclSpec::Diag(Diagnostic &D, SourceLocation Loc, SourceManager& SrcMgr,
299 unsigned DiagID, const std::string &info) {
300 D.Report(FullSourceLoc(Loc,SrcMgr), DiagID, &info, 1);
301}