blob: d8c6986f9ea60b5dc80816dda8ecc88a07be1a37 [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"
Chris Lattner60f36222009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Douglas Gregore3e01a22009-04-01 22:41:11 +000016#include "clang/Lex/Preprocessor.h"
Chris Lattnerda48a8e2006-08-04 05:25:55 +000017#include "clang/Basic/LangOptions.h"
Chris Lattner1ce41ed2009-01-20 19:11:22 +000018#include "llvm/ADT/STLExtras.h"
Douglas Gregor52537682009-03-19 00:18:19 +000019#include <cstring>
Chris Lattnerb9093cd2006-08-04 04:39:53 +000020using namespace clang;
21
Chris Lattner3b0f3ef2008-11-22 08:32:36 +000022
23static DiagnosticBuilder Diag(Diagnostic &D, SourceLocation Loc,
24 SourceManager &SrcMgr, unsigned DiagID) {
25 return D.Report(FullSourceLoc(Loc, SrcMgr), DiagID);
26}
27
Chris Lattner1ce41ed2009-01-20 19:11:22 +000028/// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function.
29/// "TheDeclarator" is the declarator that this will be added to.
30DeclaratorChunk DeclaratorChunk::getFunction(bool hasProto, bool isVariadic,
Douglas Gregor94349fd2009-02-18 07:07:28 +000031 SourceLocation EllipsisLoc,
Chris Lattner1ce41ed2009-01-20 19:11:22 +000032 ParamInfo *ArgInfo,
33 unsigned NumArgs,
34 unsigned TypeQuals,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +000035 bool hasExceptionSpec,
Sebastian Redlfb3f1792009-05-31 11:47:27 +000036 SourceLocation ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +000037 bool hasAnyExceptionSpec,
38 ActionBase::TypeTy **Exceptions,
Sebastian Redld6434562009-05-29 18:02:33 +000039 SourceRange *ExceptionRanges,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +000040 unsigned NumExceptions,
Chris Lattner1ce41ed2009-01-20 19:11:22 +000041 SourceLocation Loc,
42 Declarator &TheDeclarator) {
43 DeclaratorChunk I;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +000044 I.Kind = Function;
45 I.Loc = Loc;
46 I.Fun.hasPrototype = hasProto;
47 I.Fun.isVariadic = isVariadic;
48 I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding();
49 I.Fun.DeleteArgInfo = false;
50 I.Fun.TypeQuals = TypeQuals;
51 I.Fun.NumArgs = NumArgs;
52 I.Fun.ArgInfo = 0;
53 I.Fun.hasExceptionSpec = hasExceptionSpec;
Sebastian Redlfb3f1792009-05-31 11:47:27 +000054 I.Fun.ThrowLoc = ThrowLoc.getRawEncoding();
Sebastian Redl2b9cacb2009-04-29 17:30:04 +000055 I.Fun.hasAnyExceptionSpec = hasAnyExceptionSpec;
56 I.Fun.NumExceptions = NumExceptions;
57 I.Fun.Exceptions = 0;
58
Chris Lattner1ce41ed2009-01-20 19:11:22 +000059 // new[] an argument array if needed.
60 if (NumArgs) {
61 // If the 'InlineParams' in Declarator is unused and big enough, put our
62 // parameter list there (in an effort to avoid new/delete traffic). If it
63 // is already used (consider a function returning a function pointer) or too
64 // small (function taking too many arguments), go to the heap.
65 if (!TheDeclarator.InlineParamsUsed &&
66 NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
67 I.Fun.ArgInfo = TheDeclarator.InlineParams;
68 I.Fun.DeleteArgInfo = false;
69 TheDeclarator.InlineParamsUsed = true;
70 } else {
71 I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs];
72 I.Fun.DeleteArgInfo = true;
73 }
74 memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs);
75 }
Sebastian Redl2b9cacb2009-04-29 17:30:04 +000076 // new[] an exception array if needed
77 if (NumExceptions) {
Sebastian Redld6434562009-05-29 18:02:33 +000078 I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions];
79 for (unsigned i = 0; i != NumExceptions; ++i) {
80 I.Fun.Exceptions[i].Ty = Exceptions[i];
81 I.Fun.Exceptions[i].Range = ExceptionRanges[i];
82 }
Sebastian Redl2b9cacb2009-04-29 17:30:04 +000083 }
Chris Lattner1ce41ed2009-01-20 19:11:22 +000084 return I;
85}
Chris Lattner3b0f3ef2008-11-22 08:32:36 +000086
Chris Lattnerb9093cd2006-08-04 04:39:53 +000087/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
Chris Lattnere0c51162009-02-27 18:35:46 +000088/// declaration specifier includes.
Chris Lattnerb9093cd2006-08-04 04:39:53 +000089///
90unsigned DeclSpec::getParsedSpecifiers() const {
91 unsigned Res = 0;
Chris Lattnerf63f89a2006-08-05 03:28:50 +000092 if (StorageClassSpec != SCS_unspecified ||
93 SCS_thread_specified)
Chris Lattnerb9093cd2006-08-04 04:39:53 +000094 Res |= PQ_StorageClassSpecifier;
Mike Stump65643c62008-06-19 19:52:46 +000095
Chris Lattner4d8f8732006-11-28 05:05:08 +000096 if (TypeQualifiers != TQ_unspecified)
Chris Lattnerb9093cd2006-08-04 04:39:53 +000097 Res |= PQ_TypeQualifier;
98
Chris Lattnerf055d432006-11-28 04:28:12 +000099 if (hasTypeSpecifier())
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000100 Res |= PQ_TypeSpecifier;
101
Douglas Gregor61956c42008-10-31 09:07:45 +0000102 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000103 Res |= PQ_FunctionSpecifier;
104 return Res;
105}
106
Chris Lattner7bd11fe2007-01-23 04:35:33 +0000107const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000108 switch (S) {
109 default: assert(0 && "Unknown typespec!");
110 case DeclSpec::SCS_unspecified: return "unspecified";
111 case DeclSpec::SCS_typedef: return "typedef";
112 case DeclSpec::SCS_extern: return "extern";
113 case DeclSpec::SCS_static: return "static";
114 case DeclSpec::SCS_auto: return "auto";
115 case DeclSpec::SCS_register: return "register";
Eli Friedmand5c0eed2009-04-19 20:27:55 +0000116 case DeclSpec::SCS_private_extern: return "__private_extern__";
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000117 case DeclSpec::SCS_mutable: return "mutable";
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000118 }
119}
120
Steve Naroffab468cb2008-02-12 04:08:59 +0000121bool DeclSpec::BadSpecifier(SCS S, const char *&PrevSpec) {
Steve Naroffab468cb2008-02-12 04:08:59 +0000122 PrevSpec = getSpecifierName(S);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000123 return true;
124}
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000125
Steve Naroffab468cb2008-02-12 04:08:59 +0000126bool DeclSpec::BadSpecifier(TSW W, const char *&PrevSpec) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000127 switch (W) {
Steve Naroffab468cb2008-02-12 04:08:59 +0000128 case TSW_unspecified: PrevSpec = "unspecified"; break;
129 case TSW_short: PrevSpec = "short"; break;
130 case TSW_long: PrevSpec = "long"; break;
131 case TSW_longlong: PrevSpec = "long long"; break;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000132 }
133 return true;
134}
135
Steve Naroffab468cb2008-02-12 04:08:59 +0000136bool DeclSpec::BadSpecifier(TSC C, const char *&PrevSpec) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000137 switch (C) {
Steve Naroffab468cb2008-02-12 04:08:59 +0000138 case TSC_unspecified: PrevSpec = "unspecified"; break;
139 case TSC_imaginary: PrevSpec = "imaginary"; break;
140 case TSC_complex: PrevSpec = "complex"; break;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000141 }
142 return true;
143}
144
145
Steve Naroffab468cb2008-02-12 04:08:59 +0000146bool DeclSpec::BadSpecifier(TSS S, const char *&PrevSpec) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000147 switch (S) {
Steve Naroffab468cb2008-02-12 04:08:59 +0000148 case TSS_unspecified: PrevSpec = "unspecified"; break;
149 case TSS_signed: PrevSpec = "signed"; break;
150 case TSS_unsigned: PrevSpec = "unsigned"; break;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000151 }
152 return true;
153}
154
Chris Lattner69680ea2007-01-23 04:34:43 +0000155const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000156 switch (T) {
Chris Lattner839713c2006-08-04 06:15:52 +0000157 default: assert(0 && "Unknown typespec!");
158 case DeclSpec::TST_unspecified: return "unspecified";
159 case DeclSpec::TST_void: return "void";
160 case DeclSpec::TST_char: return "char";
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000161 case DeclSpec::TST_wchar: return "wchar_t";
Chris Lattner839713c2006-08-04 06:15:52 +0000162 case DeclSpec::TST_int: return "int";
163 case DeclSpec::TST_float: return "float";
164 case DeclSpec::TST_double: return "double";
165 case DeclSpec::TST_bool: return "_Bool";
166 case DeclSpec::TST_decimal32: return "_Decimal32";
167 case DeclSpec::TST_decimal64: return "_Decimal64";
168 case DeclSpec::TST_decimal128: return "_Decimal128";
Chris Lattnerda72c822006-08-13 22:16:42 +0000169 case DeclSpec::TST_enum: return "enum";
Chris Lattner861a2262008-04-13 18:59:07 +0000170 case DeclSpec::TST_class: return "class";
Chris Lattnerda72c822006-08-13 22:16:42 +0000171 case DeclSpec::TST_union: return "union";
172 case DeclSpec::TST_struct: return "struct";
Douglas Gregor9817f4a2009-02-09 15:09:02 +0000173 case DeclSpec::TST_typename: return "type-name";
Steve Naroffad373bd2007-07-31 12:34:36 +0000174 case DeclSpec::TST_typeofType:
175 case DeclSpec::TST_typeofExpr: return "typeof";
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000176 }
Chris Lattner839713c2006-08-04 06:15:52 +0000177}
178
Steve Naroffab468cb2008-02-12 04:08:59 +0000179bool DeclSpec::BadSpecifier(TST T, const char *&PrevSpec) {
Steve Naroffab468cb2008-02-12 04:08:59 +0000180 PrevSpec = getSpecifierName(T);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000181 return true;
182}
183
Steve Naroffab468cb2008-02-12 04:08:59 +0000184bool DeclSpec::BadSpecifier(TQ T, const char *&PrevSpec) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000185 switch (T) {
186 case DeclSpec::TQ_unspecified: PrevSpec = "unspecified"; break;
187 case DeclSpec::TQ_const: PrevSpec = "const"; break;
188 case DeclSpec::TQ_restrict: PrevSpec = "restrict"; break;
189 case DeclSpec::TQ_volatile: PrevSpec = "volatile"; break;
190 }
191 return true;
192}
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000193
Chris Lattner4d8f8732006-11-28 05:05:08 +0000194bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
195 const char *&PrevSpec) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000196 if (StorageClassSpec != SCS_unspecified)
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000197 return BadSpecifier((SCS)StorageClassSpec, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000198 StorageClassSpec = S;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000199 StorageClassSpecLoc = Loc;
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000200 assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield");
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000201 return false;
202}
203
Chris Lattner4d8f8732006-11-28 05:05:08 +0000204bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
205 const char *&PrevSpec) {
Chris Lattner353f5742006-11-28 04:50:12 +0000206 if (SCS_thread_specified) {
207 PrevSpec = "__thread";
208 return true;
209 }
210 SCS_thread_specified = true;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000211 SCS_threadLoc = Loc;
Chris Lattner353f5742006-11-28 04:50:12 +0000212 return false;
213}
214
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000215
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000216/// These methods set the specified attribute of the DeclSpec, but return true
217/// and ignore the request if invalid (e.g. "extern" then "auto" is
218/// specified).
Chris Lattnerb20e8942006-11-28 05:30:29 +0000219bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
220 const char *&PrevSpec) {
Chris Lattner353f5742006-11-28 04:50:12 +0000221 if (TypeSpecWidth != TSW_unspecified &&
222 // Allow turning long -> long long.
223 (W != TSW_longlong || TypeSpecWidth != TSW_long))
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000224 return BadSpecifier((TSW)TypeSpecWidth, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000225 TypeSpecWidth = W;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000226 TSWLoc = Loc;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000227 return false;
228}
229
Chris Lattnerb20e8942006-11-28 05:30:29 +0000230bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
231 const char *&PrevSpec) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000232 if (TypeSpecComplex != TSC_unspecified)
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000233 return BadSpecifier((TSC)TypeSpecComplex, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000234 TypeSpecComplex = C;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000235 TSCLoc = Loc;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000236 return false;
237}
238
Chris Lattnerb20e8942006-11-28 05:30:29 +0000239bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
240 const char *&PrevSpec) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000241 if (TypeSpecSign != TSS_unspecified)
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000242 return BadSpecifier((TSS)TypeSpecSign, PrevSpec);
Chris Lattnerdeb42f52006-08-04 05:26:52 +0000243 TypeSpecSign = S;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000244 TSSLoc = Loc;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000245 return false;
246}
247
Chris Lattnerb20e8942006-11-28 05:30:29 +0000248bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
Douglas Gregord6ab8742009-05-28 23:31:59 +0000249 const char *&PrevSpec, void *Rep,
250 bool Owned) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000251 if (TypeSpecType != TST_unspecified)
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000252 return BadSpecifier((TST)TypeSpecType, PrevSpec);
Chris Lattnerdeb42f52006-08-04 05:26:52 +0000253 TypeSpecType = T;
Chris Lattnerb9d572a2007-01-23 04:58:34 +0000254 TypeRep = Rep;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000255 TSTLoc = Loc;
Douglas Gregord6ab8742009-05-28 23:31:59 +0000256 TypeSpecOwned = Owned;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000257 return false;
258}
259
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000260bool DeclSpec::SetTypeSpecError() {
261 TypeSpecType = TST_error;
262 TypeRep = 0;
263 TSTLoc = SourceLocation();
264 return false;
265}
266
Chris Lattner60809f52006-11-28 05:18:46 +0000267bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000268 const LangOptions &Lang) {
269 // Duplicates turn into warnings pre-C99.
270 if ((TypeQualifiers & T) && !Lang.C99)
271 return BadSpecifier(T, PrevSpec);
272 TypeQualifiers |= T;
Chris Lattner60809f52006-11-28 05:18:46 +0000273
274 switch (T) {
275 default: assert(0 && "Unknown type qualifier!");
276 case TQ_const: TQ_constLoc = Loc; break;
277 case TQ_restrict: TQ_restrictLoc = Loc; break;
278 case TQ_volatile: TQ_volatileLoc = Loc; break;
279 }
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000280 return false;
281}
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000282
Chris Lattner1b22eed2006-11-28 05:12:07 +0000283bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec){
Chris Lattnera925dc62006-11-28 04:33:46 +0000284 // 'inline inline' is ok.
285 FS_inline_specified = true;
Chris Lattner1b22eed2006-11-28 05:12:07 +0000286 FS_inlineLoc = Loc;
Chris Lattnera925dc62006-11-28 04:33:46 +0000287 return false;
288}
289
Douglas Gregor61956c42008-10-31 09:07:45 +0000290bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec){
291 // 'virtual virtual' is ok.
292 FS_virtual_specified = true;
293 FS_virtualLoc = Loc;
294 return false;
295}
296
297bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec){
298 // 'explicit explicit' is ok.
299 FS_explicit_specified = true;
300 FS_explicitLoc = Loc;
301 return false;
302}
303
Anders Carlssoncd8db412009-05-06 04:46:28 +0000304bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec) {
305 if (Friend_specified) {
306 PrevSpec = "friend";
307 return true;
308 }
309
310 Friend_specified = true;
311 FriendLoc = Loc;
312 return false;
313}
Chris Lattnera925dc62006-11-28 04:33:46 +0000314
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000315/// Finish - This does final analysis of the declspec, rejecting things like
316/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
317/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
318/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Douglas Gregore3e01a22009-04-01 22:41:11 +0000319void DeclSpec::Finish(Diagnostic &D, Preprocessor &PP) {
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000320 // Check the type specifier components first.
Douglas Gregore3e01a22009-04-01 22:41:11 +0000321 SourceManager &SrcMgr = PP.getSourceManager();
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000322
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000323 // signed/unsigned are only valid with int/char/wchar_t.
Chris Lattner839713c2006-08-04 06:15:52 +0000324 if (TypeSpecSign != TSS_unspecified) {
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000325 if (TypeSpecType == TST_unspecified)
326 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000327 else if (TypeSpecType != TST_int &&
328 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000329 Diag(D, TSSLoc, SrcMgr, diag::err_invalid_sign_spec)
330 << getSpecifierName((TST)TypeSpecType);
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000331 // signed double -> double.
Chris Lattner839713c2006-08-04 06:15:52 +0000332 TypeSpecSign = TSS_unspecified;
333 }
334 }
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000335
Chris Lattner839713c2006-08-04 06:15:52 +0000336 // Validate the width of the type.
337 switch (TypeSpecWidth) {
338 case TSW_unspecified: break;
339 case TSW_short: // short int
340 case TSW_longlong: // long long int
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000341 if (TypeSpecType == TST_unspecified)
342 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
343 else if (TypeSpecType != TST_int) {
Ted Kremenekd4e5fba2007-12-11 21:27:55 +0000344 Diag(D, TSWLoc, SrcMgr,
Chris Lattner36982e42007-05-16 17:49:37 +0000345 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000346 : diag::err_invalid_longlong_spec)
347 << getSpecifierName((TST)TypeSpecType);
Chris Lattner839713c2006-08-04 06:15:52 +0000348 TypeSpecType = TST_int;
349 }
350 break;
351 case TSW_long: // long double, long int
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000352 if (TypeSpecType == TST_unspecified)
353 TypeSpecType = TST_int; // long -> long int.
354 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000355 Diag(D, TSWLoc, SrcMgr, diag::err_invalid_long_spec)
356 << getSpecifierName((TST)TypeSpecType);
Chris Lattner839713c2006-08-04 06:15:52 +0000357 TypeSpecType = TST_int;
358 }
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000359 break;
Chris Lattner839713c2006-08-04 06:15:52 +0000360 }
361
Chris Lattner0e894622006-08-13 19:58:17 +0000362 // TODO: if the implementation does not implement _Complex or _Imaginary,
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000363 // disallow their use. Need information about the backend.
364 if (TypeSpecComplex != TSC_unspecified) {
365 if (TypeSpecType == TST_unspecified) {
Douglas Gregore3e01a22009-04-01 22:41:11 +0000366 Diag(D, TSCLoc, SrcMgr, diag::ext_plain_complex)
367 << CodeModificationHint::CreateInsertion(
368 PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
369 " double");
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000370 TypeSpecType = TST_double; // _Complex -> _Complex double.
371 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000372 // Note that this intentionally doesn't include _Complex _Bool.
Ted Kremenekd4e5fba2007-12-11 21:27:55 +0000373 Diag(D, TSTLoc, SrcMgr, diag::ext_integer_complex);
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000374 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000375 Diag(D, TSCLoc, SrcMgr, diag::err_invalid_complex_spec)
376 << getSpecifierName((TST)TypeSpecType);
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000377 TypeSpecComplex = TSC_unspecified;
378 }
379 }
Chris Lattner8e90ef62006-08-05 03:30:45 +0000380
381 // Okay, now we can infer the real type.
Chris Lattner8e90ef62006-08-05 03:30:45 +0000382
Chris Lattner0e894622006-08-13 19:58:17 +0000383 // TODO: return "auto function" and other bad things based on the real type.
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000384
Chris Lattner1ae23292006-08-04 06:42:31 +0000385 // 'data definition has no type or storage class'?
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000386}
Daniel Dunbarc74b5cc2008-08-11 03:45:03 +0000387
Sebastian Redla2b5e312008-12-28 15:28:59 +0000388bool DeclSpec::isMissingDeclaratorOk() {
389 TST tst = getTypeSpecType();
390 return (tst == TST_union
391 || tst == TST_struct
392 || tst == TST_class
393 || tst == TST_enum
Douglas Gregorc6f58fe2009-01-12 22:49:06 +0000394 ) && getTypeRep() != 0 && StorageClassSpec != DeclSpec::SCS_typedef;
Sebastian Redla2b5e312008-12-28 15:28:59 +0000395}