blob: 2a765d212486a6c28a3e1609c13a45a4990276b1 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- SemaDeclSpec.cpp - Declaration Specifier Semantic Analysis -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-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 Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for declaration specifiers.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/DeclSpec.h"
Chris Lattner545f39e2009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Douglas Gregor1ba5cb32009-04-01 22:41:11 +000016#include "clang/Lex/Preprocessor.h"
Chris Lattner4b009652007-07-25 00:24:17 +000017#include "clang/Basic/LangOptions.h"
Chris Lattnerdefaf412009-01-20 19:11:22 +000018#include "llvm/ADT/STLExtras.h"
Douglas Gregor734b4ba2009-03-19 00:18:19 +000019#include <cstring>
Chris Lattner4b009652007-07-25 00:24:17 +000020using namespace clang;
21
Chris Lattner5a63b092008-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 Lattnerdefaf412009-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 Gregor88a25f82009-02-18 07:07:28 +000031 SourceLocation EllipsisLoc,
Chris Lattnerdefaf412009-01-20 19:11:22 +000032 ParamInfo *ArgInfo,
33 unsigned NumArgs,
34 unsigned TypeQuals,
Sebastian Redl35f3a5b2009-04-29 17:30:04 +000035 bool hasExceptionSpec,
36 bool hasAnyExceptionSpec,
37 ActionBase::TypeTy **Exceptions,
38 unsigned NumExceptions,
Chris Lattnerdefaf412009-01-20 19:11:22 +000039 SourceLocation Loc,
40 Declarator &TheDeclarator) {
41 DeclaratorChunk I;
Sebastian Redl35f3a5b2009-04-29 17:30:04 +000042 I.Kind = Function;
43 I.Loc = Loc;
44 I.Fun.hasPrototype = hasProto;
45 I.Fun.isVariadic = isVariadic;
46 I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding();
47 I.Fun.DeleteArgInfo = false;
48 I.Fun.TypeQuals = TypeQuals;
49 I.Fun.NumArgs = NumArgs;
50 I.Fun.ArgInfo = 0;
51 I.Fun.hasExceptionSpec = hasExceptionSpec;
52 I.Fun.hasAnyExceptionSpec = hasAnyExceptionSpec;
53 I.Fun.NumExceptions = NumExceptions;
54 I.Fun.Exceptions = 0;
55
Chris Lattnerdefaf412009-01-20 19:11:22 +000056 // new[] an argument array if needed.
57 if (NumArgs) {
58 // If the 'InlineParams' in Declarator is unused and big enough, put our
59 // parameter list there (in an effort to avoid new/delete traffic). If it
60 // is already used (consider a function returning a function pointer) or too
61 // small (function taking too many arguments), go to the heap.
62 if (!TheDeclarator.InlineParamsUsed &&
63 NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
64 I.Fun.ArgInfo = TheDeclarator.InlineParams;
65 I.Fun.DeleteArgInfo = false;
66 TheDeclarator.InlineParamsUsed = true;
67 } else {
68 I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs];
69 I.Fun.DeleteArgInfo = true;
70 }
71 memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs);
72 }
Sebastian Redl35f3a5b2009-04-29 17:30:04 +000073 // new[] an exception array if needed
74 if (NumExceptions) {
75 I.Fun.Exceptions = new ActionBase::TypeTy*[NumExceptions];
76 memcpy(I.Fun.Exceptions, Exceptions,
77 sizeof(ActionBase::TypeTy*)*NumExceptions);
78 }
Chris Lattnerdefaf412009-01-20 19:11:22 +000079 return I;
80}
Chris Lattner5a63b092008-11-22 08:32:36 +000081
Chris Lattner4b009652007-07-25 00:24:17 +000082/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
Chris Lattner92eca3e2009-02-27 18:35:46 +000083/// declaration specifier includes.
Chris Lattner4b009652007-07-25 00:24:17 +000084///
85unsigned DeclSpec::getParsedSpecifiers() const {
86 unsigned Res = 0;
87 if (StorageClassSpec != SCS_unspecified ||
88 SCS_thread_specified)
89 Res |= PQ_StorageClassSpecifier;
Mike Stumpea5a8b32008-06-19 19:52:46 +000090
Chris Lattner4b009652007-07-25 00:24:17 +000091 if (TypeQualifiers != TQ_unspecified)
92 Res |= PQ_TypeQualifier;
93
94 if (hasTypeSpecifier())
95 Res |= PQ_TypeSpecifier;
96
Douglas Gregorf15ac4b2008-10-31 09:07:45 +000097 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
Chris Lattner4b009652007-07-25 00:24:17 +000098 Res |= PQ_FunctionSpecifier;
99 return Res;
100}
101
102const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
103 switch (S) {
104 default: assert(0 && "Unknown typespec!");
105 case DeclSpec::SCS_unspecified: return "unspecified";
106 case DeclSpec::SCS_typedef: return "typedef";
107 case DeclSpec::SCS_extern: return "extern";
108 case DeclSpec::SCS_static: return "static";
109 case DeclSpec::SCS_auto: return "auto";
110 case DeclSpec::SCS_register: return "register";
Eli Friedman1a3e74c2009-04-19 20:27:55 +0000111 case DeclSpec::SCS_private_extern: return "__private_extern__";
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000112 case DeclSpec::SCS_mutable: return "mutable";
Chris Lattner4b009652007-07-25 00:24:17 +0000113 }
114}
115
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000116bool DeclSpec::BadSpecifier(SCS S, const char *&PrevSpec) {
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000117 PrevSpec = getSpecifierName(S);
Chris Lattner4b009652007-07-25 00:24:17 +0000118 return true;
119}
120
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000121bool DeclSpec::BadSpecifier(TSW W, const char *&PrevSpec) {
Chris Lattner4b009652007-07-25 00:24:17 +0000122 switch (W) {
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000123 case TSW_unspecified: PrevSpec = "unspecified"; break;
124 case TSW_short: PrevSpec = "short"; break;
125 case TSW_long: PrevSpec = "long"; break;
126 case TSW_longlong: PrevSpec = "long long"; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000127 }
128 return true;
129}
130
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000131bool DeclSpec::BadSpecifier(TSC C, const char *&PrevSpec) {
Chris Lattner4b009652007-07-25 00:24:17 +0000132 switch (C) {
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000133 case TSC_unspecified: PrevSpec = "unspecified"; break;
134 case TSC_imaginary: PrevSpec = "imaginary"; break;
135 case TSC_complex: PrevSpec = "complex"; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000136 }
137 return true;
138}
139
140
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000141bool DeclSpec::BadSpecifier(TSS S, const char *&PrevSpec) {
Chris Lattner4b009652007-07-25 00:24:17 +0000142 switch (S) {
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000143 case TSS_unspecified: PrevSpec = "unspecified"; break;
144 case TSS_signed: PrevSpec = "signed"; break;
145 case TSS_unsigned: PrevSpec = "unsigned"; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000146 }
147 return true;
148}
149
150const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
151 switch (T) {
152 default: assert(0 && "Unknown typespec!");
153 case DeclSpec::TST_unspecified: return "unspecified";
154 case DeclSpec::TST_void: return "void";
155 case DeclSpec::TST_char: return "char";
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000156 case DeclSpec::TST_wchar: return "wchar_t";
Chris Lattner4b009652007-07-25 00:24:17 +0000157 case DeclSpec::TST_int: return "int";
158 case DeclSpec::TST_float: return "float";
159 case DeclSpec::TST_double: return "double";
160 case DeclSpec::TST_bool: return "_Bool";
161 case DeclSpec::TST_decimal32: return "_Decimal32";
162 case DeclSpec::TST_decimal64: return "_Decimal64";
163 case DeclSpec::TST_decimal128: return "_Decimal128";
164 case DeclSpec::TST_enum: return "enum";
Chris Lattner2e78db32008-04-13 18:59:07 +0000165 case DeclSpec::TST_class: return "class";
Chris Lattner4b009652007-07-25 00:24:17 +0000166 case DeclSpec::TST_union: return "union";
167 case DeclSpec::TST_struct: return "struct";
Douglas Gregora60c62e2009-02-09 15:09:02 +0000168 case DeclSpec::TST_typename: return "type-name";
Steve Naroff7cbb1462007-07-31 12:34:36 +0000169 case DeclSpec::TST_typeofType:
170 case DeclSpec::TST_typeofExpr: return "typeof";
Chris Lattner4b009652007-07-25 00:24:17 +0000171 }
172}
173
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000174bool DeclSpec::BadSpecifier(TST T, const char *&PrevSpec) {
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000175 PrevSpec = getSpecifierName(T);
Chris Lattner4b009652007-07-25 00:24:17 +0000176 return true;
177}
178
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000179bool DeclSpec::BadSpecifier(TQ T, const char *&PrevSpec) {
Chris Lattner4b009652007-07-25 00:24:17 +0000180 switch (T) {
181 case DeclSpec::TQ_unspecified: PrevSpec = "unspecified"; break;
182 case DeclSpec::TQ_const: PrevSpec = "const"; break;
183 case DeclSpec::TQ_restrict: PrevSpec = "restrict"; break;
184 case DeclSpec::TQ_volatile: PrevSpec = "volatile"; break;
185 }
186 return true;
187}
188
189bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
190 const char *&PrevSpec) {
191 if (StorageClassSpec != SCS_unspecified)
Chris Lattner5a63b092008-11-22 08:32:36 +0000192 return BadSpecifier((SCS)StorageClassSpec, PrevSpec);
Chris Lattner4b009652007-07-25 00:24:17 +0000193 StorageClassSpec = S;
194 StorageClassSpecLoc = Loc;
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000195 assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield");
Chris Lattner4b009652007-07-25 00:24:17 +0000196 return false;
197}
198
199bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
200 const char *&PrevSpec) {
201 if (SCS_thread_specified) {
202 PrevSpec = "__thread";
203 return true;
204 }
205 SCS_thread_specified = true;
206 SCS_threadLoc = Loc;
207 return false;
208}
209
210
211/// These methods set the specified attribute of the DeclSpec, but return true
212/// and ignore the request if invalid (e.g. "extern" then "auto" is
213/// specified).
214bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
215 const char *&PrevSpec) {
216 if (TypeSpecWidth != TSW_unspecified &&
217 // Allow turning long -> long long.
218 (W != TSW_longlong || TypeSpecWidth != TSW_long))
Chris Lattner5a63b092008-11-22 08:32:36 +0000219 return BadSpecifier((TSW)TypeSpecWidth, PrevSpec);
Chris Lattner4b009652007-07-25 00:24:17 +0000220 TypeSpecWidth = W;
221 TSWLoc = Loc;
222 return false;
223}
224
225bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
226 const char *&PrevSpec) {
227 if (TypeSpecComplex != TSC_unspecified)
Chris Lattner5a63b092008-11-22 08:32:36 +0000228 return BadSpecifier((TSC)TypeSpecComplex, PrevSpec);
Chris Lattner4b009652007-07-25 00:24:17 +0000229 TypeSpecComplex = C;
230 TSCLoc = Loc;
231 return false;
232}
233
234bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
235 const char *&PrevSpec) {
236 if (TypeSpecSign != TSS_unspecified)
Chris Lattner5a63b092008-11-22 08:32:36 +0000237 return BadSpecifier((TSS)TypeSpecSign, PrevSpec);
Chris Lattner4b009652007-07-25 00:24:17 +0000238 TypeSpecSign = S;
239 TSSLoc = Loc;
240 return false;
241}
242
243bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
Douglas Gregor71f06032009-05-28 23:31:59 +0000244 const char *&PrevSpec, void *Rep,
245 bool Owned) {
Chris Lattner4b009652007-07-25 00:24:17 +0000246 if (TypeSpecType != TST_unspecified)
Chris Lattner5a63b092008-11-22 08:32:36 +0000247 return BadSpecifier((TST)TypeSpecType, PrevSpec);
Chris Lattner4b009652007-07-25 00:24:17 +0000248 TypeSpecType = T;
249 TypeRep = Rep;
250 TSTLoc = Loc;
Douglas Gregor71f06032009-05-28 23:31:59 +0000251 TypeSpecOwned = Owned;
Chris Lattner4b009652007-07-25 00:24:17 +0000252 return false;
253}
254
Douglas Gregord406b032009-02-06 22:42:48 +0000255bool DeclSpec::SetTypeSpecError() {
256 TypeSpecType = TST_error;
257 TypeRep = 0;
258 TSTLoc = SourceLocation();
259 return false;
260}
261
Chris Lattner4b009652007-07-25 00:24:17 +0000262bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
263 const LangOptions &Lang) {
264 // Duplicates turn into warnings pre-C99.
265 if ((TypeQualifiers & T) && !Lang.C99)
266 return BadSpecifier(T, PrevSpec);
267 TypeQualifiers |= T;
268
269 switch (T) {
270 default: assert(0 && "Unknown type qualifier!");
271 case TQ_const: TQ_constLoc = Loc; break;
272 case TQ_restrict: TQ_restrictLoc = Loc; break;
273 case TQ_volatile: TQ_volatileLoc = Loc; break;
274 }
275 return false;
276}
277
278bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec){
279 // 'inline inline' is ok.
280 FS_inline_specified = true;
281 FS_inlineLoc = Loc;
282 return false;
283}
284
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000285bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec){
286 // 'virtual virtual' is ok.
287 FS_virtual_specified = true;
288 FS_virtualLoc = Loc;
289 return false;
290}
291
292bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec){
293 // 'explicit explicit' is ok.
294 FS_explicit_specified = true;
295 FS_explicitLoc = Loc;
296 return false;
297}
298
Anders Carlsson6c2ad5a2009-05-06 04:46:28 +0000299bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec) {
300 if (Friend_specified) {
301 PrevSpec = "friend";
302 return true;
303 }
304
305 Friend_specified = true;
306 FriendLoc = Loc;
307 return false;
308}
Chris Lattner4b009652007-07-25 00:24:17 +0000309
310/// Finish - This does final analysis of the declspec, rejecting things like
311/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
312/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
313/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Douglas Gregor1ba5cb32009-04-01 22:41:11 +0000314void DeclSpec::Finish(Diagnostic &D, Preprocessor &PP) {
Chris Lattner4b009652007-07-25 00:24:17 +0000315 // Check the type specifier components first.
Douglas Gregor1ba5cb32009-04-01 22:41:11 +0000316 SourceManager &SrcMgr = PP.getSourceManager();
Chris Lattner4b009652007-07-25 00:24:17 +0000317
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000318 // signed/unsigned are only valid with int/char/wchar_t.
Chris Lattner4b009652007-07-25 00:24:17 +0000319 if (TypeSpecSign != TSS_unspecified) {
320 if (TypeSpecType == TST_unspecified)
321 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000322 else if (TypeSpecType != TST_int &&
323 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
Chris Lattner5a63b092008-11-22 08:32:36 +0000324 Diag(D, TSSLoc, SrcMgr, diag::err_invalid_sign_spec)
325 << getSpecifierName((TST)TypeSpecType);
Chris Lattner4b009652007-07-25 00:24:17 +0000326 // signed double -> double.
327 TypeSpecSign = TSS_unspecified;
328 }
329 }
330
331 // Validate the width of the type.
332 switch (TypeSpecWidth) {
333 case TSW_unspecified: break;
334 case TSW_short: // short int
335 case TSW_longlong: // long long int
336 if (TypeSpecType == TST_unspecified)
337 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
338 else if (TypeSpecType != TST_int) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000339 Diag(D, TSWLoc, SrcMgr,
Chris Lattner4b009652007-07-25 00:24:17 +0000340 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
Chris Lattner5a63b092008-11-22 08:32:36 +0000341 : diag::err_invalid_longlong_spec)
342 << getSpecifierName((TST)TypeSpecType);
Chris Lattner4b009652007-07-25 00:24:17 +0000343 TypeSpecType = TST_int;
344 }
345 break;
346 case TSW_long: // long double, long int
347 if (TypeSpecType == TST_unspecified)
348 TypeSpecType = TST_int; // long -> long int.
349 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Chris Lattner5a63b092008-11-22 08:32:36 +0000350 Diag(D, TSWLoc, SrcMgr, diag::err_invalid_long_spec)
351 << getSpecifierName((TST)TypeSpecType);
Chris Lattner4b009652007-07-25 00:24:17 +0000352 TypeSpecType = TST_int;
353 }
354 break;
355 }
356
357 // TODO: if the implementation does not implement _Complex or _Imaginary,
358 // disallow their use. Need information about the backend.
359 if (TypeSpecComplex != TSC_unspecified) {
360 if (TypeSpecType == TST_unspecified) {
Douglas Gregor1ba5cb32009-04-01 22:41:11 +0000361 Diag(D, TSCLoc, SrcMgr, diag::ext_plain_complex)
362 << CodeModificationHint::CreateInsertion(
363 PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
364 " double");
Chris Lattner4b009652007-07-25 00:24:17 +0000365 TypeSpecType = TST_double; // _Complex -> _Complex double.
366 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
367 // Note that this intentionally doesn't include _Complex _Bool.
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000368 Diag(D, TSTLoc, SrcMgr, diag::ext_integer_complex);
Chris Lattner4b009652007-07-25 00:24:17 +0000369 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Chris Lattner5a63b092008-11-22 08:32:36 +0000370 Diag(D, TSCLoc, SrcMgr, diag::err_invalid_complex_spec)
371 << getSpecifierName((TST)TypeSpecType);
Chris Lattner4b009652007-07-25 00:24:17 +0000372 TypeSpecComplex = TSC_unspecified;
373 }
374 }
Chris Lattner4b009652007-07-25 00:24:17 +0000375
376 // Okay, now we can infer the real type.
377
378 // TODO: return "auto function" and other bad things based on the real type.
379
380 // 'data definition has no type or storage class'?
381}
Daniel Dunbarcc7b1602008-08-11 03:45:03 +0000382
Sebastian Redlb7605e82008-12-28 15:28:59 +0000383bool DeclSpec::isMissingDeclaratorOk() {
384 TST tst = getTypeSpecType();
385 return (tst == TST_union
386 || tst == TST_struct
387 || tst == TST_class
388 || tst == TST_enum
Douglas Gregorb748fc52009-01-12 22:49:06 +0000389 ) && getTypeRep() != 0 && StorageClassSpec != DeclSpec::SCS_typedef;
Sebastian Redlb7605e82008-12-28 15:28:59 +0000390}