blob: e592deecd0439f2dbdcee0e1e23e1e280eba7a93 [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"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Douglas Gregor9b3064b2009-04-01 22:41:11 +000016#include "clang/Lex/Preprocessor.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "clang/Basic/LangOptions.h"
Chris Lattner5af2f352009-01-20 19:11:22 +000018#include "llvm/ADT/STLExtras.h"
Douglas Gregore4e5b052009-03-19 00:18:19 +000019#include <cstring>
Reid Spencer5f016e22007-07-11 17:01:13 +000020using namespace clang;
21
Chris Lattner254be6a2008-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 Lattner5af2f352009-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 Gregor965acbb2009-02-18 07:07:28 +000031 SourceLocation EllipsisLoc,
Chris Lattner5af2f352009-01-20 19:11:22 +000032 ParamInfo *ArgInfo,
33 unsigned NumArgs,
34 unsigned TypeQuals,
Sebastian Redl7dc81342009-04-29 17:30:04 +000035 bool hasExceptionSpec,
36 bool hasAnyExceptionSpec,
37 ActionBase::TypeTy **Exceptions,
38 unsigned NumExceptions,
Chris Lattner5af2f352009-01-20 19:11:22 +000039 SourceLocation Loc,
40 Declarator &TheDeclarator) {
41 DeclaratorChunk I;
Sebastian Redl7dc81342009-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 Lattner5af2f352009-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 Redl7dc81342009-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 Lattner5af2f352009-01-20 19:11:22 +000079 return I;
80}
Chris Lattner254be6a2008-11-22 08:32:36 +000081
Reid Spencer5f016e22007-07-11 17:01:13 +000082/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
Chris Lattner2a327d12009-02-27 18:35:46 +000083/// declaration specifier includes.
Reid Spencer5f016e22007-07-11 17:01:13 +000084///
85unsigned DeclSpec::getParsedSpecifiers() const {
86 unsigned Res = 0;
87 if (StorageClassSpec != SCS_unspecified ||
88 SCS_thread_specified)
89 Res |= PQ_StorageClassSpecifier;
Mike Stumpd4204332008-06-19 19:52:46 +000090
Reid Spencer5f016e22007-07-11 17:01:13 +000091 if (TypeQualifiers != TQ_unspecified)
92 Res |= PQ_TypeQualifier;
93
94 if (hasTypeSpecifier())
95 Res |= PQ_TypeSpecifier;
96
Douglas Gregorb48fe382008-10-31 09:07:45 +000097 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
Reid Spencer5f016e22007-07-11 17:01:13 +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 Friedman63054b32009-04-19 20:27:55 +0000111 case DeclSpec::SCS_private_extern: return "__private_extern__";
Sebastian Redl669d5d72008-11-14 23:42:31 +0000112 case DeclSpec::SCS_mutable: return "mutable";
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 }
114}
115
Steve Naroff95324142008-02-12 04:08:59 +0000116bool DeclSpec::BadSpecifier(SCS S, const char *&PrevSpec) {
Steve Naroff95324142008-02-12 04:08:59 +0000117 PrevSpec = getSpecifierName(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000118 return true;
119}
120
Steve Naroff95324142008-02-12 04:08:59 +0000121bool DeclSpec::BadSpecifier(TSW W, const char *&PrevSpec) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 switch (W) {
Steve Naroff95324142008-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;
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 }
128 return true;
129}
130
Steve Naroff95324142008-02-12 04:08:59 +0000131bool DeclSpec::BadSpecifier(TSC C, const char *&PrevSpec) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 switch (C) {
Steve Naroff95324142008-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;
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 }
137 return true;
138}
139
140
Steve Naroff95324142008-02-12 04:08:59 +0000141bool DeclSpec::BadSpecifier(TSS S, const char *&PrevSpec) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 switch (S) {
Steve Naroff95324142008-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;
Reid Spencer5f016e22007-07-11 17:01:13 +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";
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000156 case DeclSpec::TST_wchar: return "wchar_t";
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner99dc9142008-04-13 18:59:07 +0000165 case DeclSpec::TST_class: return "class";
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 case DeclSpec::TST_union: return "union";
167 case DeclSpec::TST_struct: return "struct";
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000168 case DeclSpec::TST_typename: return "type-name";
Steve Naroffd1861fd2007-07-31 12:34:36 +0000169 case DeclSpec::TST_typeofType:
170 case DeclSpec::TST_typeofExpr: return "typeof";
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 }
172}
173
Steve Naroff95324142008-02-12 04:08:59 +0000174bool DeclSpec::BadSpecifier(TST T, const char *&PrevSpec) {
Steve Naroff95324142008-02-12 04:08:59 +0000175 PrevSpec = getSpecifierName(T);
Reid Spencer5f016e22007-07-11 17:01:13 +0000176 return true;
177}
178
Steve Naroff95324142008-02-12 04:08:59 +0000179bool DeclSpec::BadSpecifier(TQ T, const char *&PrevSpec) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner254be6a2008-11-22 08:32:36 +0000192 return BadSpecifier((SCS)StorageClassSpec, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000193 StorageClassSpec = S;
194 StorageClassSpecLoc = Loc;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000195 assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield");
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner254be6a2008-11-22 08:32:36 +0000219 return BadSpecifier((TSW)TypeSpecWidth, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner254be6a2008-11-22 08:32:36 +0000228 return BadSpecifier((TSC)TypeSpecComplex, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner254be6a2008-11-22 08:32:36 +0000237 return BadSpecifier((TSS)TypeSpecSign, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000238 TypeSpecSign = S;
239 TSSLoc = Loc;
240 return false;
241}
242
243bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000244 const char *&PrevSpec, void *Rep) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000245 if (TypeSpecType != TST_unspecified)
Chris Lattner254be6a2008-11-22 08:32:36 +0000246 return BadSpecifier((TST)TypeSpecType, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000247 TypeSpecType = T;
248 TypeRep = Rep;
249 TSTLoc = Loc;
250 return false;
251}
252
Douglas Gregorddc29e12009-02-06 22:42:48 +0000253bool DeclSpec::SetTypeSpecError() {
254 TypeSpecType = TST_error;
255 TypeRep = 0;
256 TSTLoc = SourceLocation();
257 return false;
258}
259
Reid Spencer5f016e22007-07-11 17:01:13 +0000260bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
261 const LangOptions &Lang) {
262 // Duplicates turn into warnings pre-C99.
263 if ((TypeQualifiers & T) && !Lang.C99)
264 return BadSpecifier(T, PrevSpec);
265 TypeQualifiers |= T;
266
267 switch (T) {
268 default: assert(0 && "Unknown type qualifier!");
269 case TQ_const: TQ_constLoc = Loc; break;
270 case TQ_restrict: TQ_restrictLoc = Loc; break;
271 case TQ_volatile: TQ_volatileLoc = Loc; break;
272 }
273 return false;
274}
275
276bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec){
277 // 'inline inline' is ok.
278 FS_inline_specified = true;
279 FS_inlineLoc = Loc;
280 return false;
281}
282
Douglas Gregorb48fe382008-10-31 09:07:45 +0000283bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec){
284 // 'virtual virtual' is ok.
285 FS_virtual_specified = true;
286 FS_virtualLoc = Loc;
287 return false;
288}
289
290bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec){
291 // 'explicit explicit' is ok.
292 FS_explicit_specified = true;
293 FS_explicitLoc = Loc;
294 return false;
295}
296
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000297bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec) {
298 if (Friend_specified) {
299 PrevSpec = "friend";
300 return true;
301 }
302
303 Friend_specified = true;
304 FriendLoc = Loc;
305 return false;
306}
Reid Spencer5f016e22007-07-11 17:01:13 +0000307
308/// Finish - This does final analysis of the declspec, rejecting things like
309/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
310/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
311/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000312void DeclSpec::Finish(Diagnostic &D, Preprocessor &PP) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000313 // Check the type specifier components first.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000314 SourceManager &SrcMgr = PP.getSourceManager();
Reid Spencer5f016e22007-07-11 17:01:13 +0000315
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000316 // signed/unsigned are only valid with int/char/wchar_t.
Reid Spencer5f016e22007-07-11 17:01:13 +0000317 if (TypeSpecSign != TSS_unspecified) {
318 if (TypeSpecType == TST_unspecified)
319 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000320 else if (TypeSpecType != TST_int &&
321 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000322 Diag(D, TSSLoc, SrcMgr, diag::err_invalid_sign_spec)
323 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000324 // signed double -> double.
325 TypeSpecSign = TSS_unspecified;
326 }
327 }
328
329 // Validate the width of the type.
330 switch (TypeSpecWidth) {
331 case TSW_unspecified: break;
332 case TSW_short: // short int
333 case TSW_longlong: // long long int
334 if (TypeSpecType == TST_unspecified)
335 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
336 else if (TypeSpecType != TST_int) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000337 Diag(D, TSWLoc, SrcMgr,
Reid Spencer5f016e22007-07-11 17:01:13 +0000338 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
Chris Lattner254be6a2008-11-22 08:32:36 +0000339 : diag::err_invalid_longlong_spec)
340 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000341 TypeSpecType = TST_int;
342 }
343 break;
344 case TSW_long: // long double, long int
345 if (TypeSpecType == TST_unspecified)
346 TypeSpecType = TST_int; // long -> long int.
347 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000348 Diag(D, TSWLoc, SrcMgr, diag::err_invalid_long_spec)
349 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000350 TypeSpecType = TST_int;
351 }
352 break;
353 }
354
355 // TODO: if the implementation does not implement _Complex or _Imaginary,
356 // disallow their use. Need information about the backend.
357 if (TypeSpecComplex != TSC_unspecified) {
358 if (TypeSpecType == TST_unspecified) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000359 Diag(D, TSCLoc, SrcMgr, diag::ext_plain_complex)
360 << CodeModificationHint::CreateInsertion(
361 PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
362 " double");
Reid Spencer5f016e22007-07-11 17:01:13 +0000363 TypeSpecType = TST_double; // _Complex -> _Complex double.
364 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
365 // Note that this intentionally doesn't include _Complex _Bool.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000366 Diag(D, TSTLoc, SrcMgr, diag::ext_integer_complex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000367 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000368 Diag(D, TSCLoc, SrcMgr, diag::err_invalid_complex_spec)
369 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000370 TypeSpecComplex = TSC_unspecified;
371 }
372 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000373
374 // Okay, now we can infer the real type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000375
376 // TODO: return "auto function" and other bad things based on the real type.
377
378 // 'data definition has no type or storage class'?
379}
Daniel Dunbare4858a62008-08-11 03:45:03 +0000380
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000381bool DeclSpec::isMissingDeclaratorOk() {
382 TST tst = getTypeSpecType();
383 return (tst == TST_union
384 || tst == TST_struct
385 || tst == TST_class
386 || tst == TST_enum
Douglas Gregor4920f1f2009-01-12 22:49:06 +0000387 ) && getTypeRep() != 0 && StorageClassSpec != DeclSpec::SCS_typedef;
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000388}