blob: 40675de98928c71ca617c71b624a97c092660513 [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,
35 SourceLocation Loc,
36 Declarator &TheDeclarator) {
37 DeclaratorChunk I;
38 I.Kind = Function;
39 I.Loc = Loc;
40 I.Fun.hasPrototype = hasProto;
41 I.Fun.isVariadic = isVariadic;
Douglas Gregor965acbb2009-02-18 07:07:28 +000042 I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding();
Chris Lattner5af2f352009-01-20 19:11:22 +000043 I.Fun.DeleteArgInfo = false;
44 I.Fun.TypeQuals = TypeQuals;
45 I.Fun.NumArgs = NumArgs;
46 I.Fun.ArgInfo = 0;
47
48 // new[] an argument array if needed.
49 if (NumArgs) {
50 // If the 'InlineParams' in Declarator is unused and big enough, put our
51 // parameter list there (in an effort to avoid new/delete traffic). If it
52 // is already used (consider a function returning a function pointer) or too
53 // small (function taking too many arguments), go to the heap.
54 if (!TheDeclarator.InlineParamsUsed &&
55 NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
56 I.Fun.ArgInfo = TheDeclarator.InlineParams;
57 I.Fun.DeleteArgInfo = false;
58 TheDeclarator.InlineParamsUsed = true;
59 } else {
60 I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs];
61 I.Fun.DeleteArgInfo = true;
62 }
63 memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs);
64 }
65 return I;
66}
Chris Lattner254be6a2008-11-22 08:32:36 +000067
Reid Spencer5f016e22007-07-11 17:01:13 +000068/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
Chris Lattner2a327d12009-02-27 18:35:46 +000069/// declaration specifier includes.
Reid Spencer5f016e22007-07-11 17:01:13 +000070///
71unsigned DeclSpec::getParsedSpecifiers() const {
72 unsigned Res = 0;
73 if (StorageClassSpec != SCS_unspecified ||
74 SCS_thread_specified)
75 Res |= PQ_StorageClassSpecifier;
Mike Stumpd4204332008-06-19 19:52:46 +000076
Reid Spencer5f016e22007-07-11 17:01:13 +000077 if (TypeQualifiers != TQ_unspecified)
78 Res |= PQ_TypeQualifier;
79
80 if (hasTypeSpecifier())
81 Res |= PQ_TypeSpecifier;
82
Douglas Gregorb48fe382008-10-31 09:07:45 +000083 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
Reid Spencer5f016e22007-07-11 17:01:13 +000084 Res |= PQ_FunctionSpecifier;
85 return Res;
86}
87
88const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
89 switch (S) {
90 default: assert(0 && "Unknown typespec!");
91 case DeclSpec::SCS_unspecified: return "unspecified";
92 case DeclSpec::SCS_typedef: return "typedef";
93 case DeclSpec::SCS_extern: return "extern";
94 case DeclSpec::SCS_static: return "static";
95 case DeclSpec::SCS_auto: return "auto";
96 case DeclSpec::SCS_register: return "register";
Sebastian Redl669d5d72008-11-14 23:42:31 +000097 case DeclSpec::SCS_mutable: return "mutable";
Reid Spencer5f016e22007-07-11 17:01:13 +000098 }
99}
100
Steve Naroff95324142008-02-12 04:08:59 +0000101bool DeclSpec::BadSpecifier(SCS S, const char *&PrevSpec) {
Steve Naroff95324142008-02-12 04:08:59 +0000102 PrevSpec = getSpecifierName(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000103 return true;
104}
105
Steve Naroff95324142008-02-12 04:08:59 +0000106bool DeclSpec::BadSpecifier(TSW W, const char *&PrevSpec) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000107 switch (W) {
Steve Naroff95324142008-02-12 04:08:59 +0000108 case TSW_unspecified: PrevSpec = "unspecified"; break;
109 case TSW_short: PrevSpec = "short"; break;
110 case TSW_long: PrevSpec = "long"; break;
111 case TSW_longlong: PrevSpec = "long long"; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000112 }
113 return true;
114}
115
Steve Naroff95324142008-02-12 04:08:59 +0000116bool DeclSpec::BadSpecifier(TSC C, const char *&PrevSpec) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000117 switch (C) {
Steve Naroff95324142008-02-12 04:08:59 +0000118 case TSC_unspecified: PrevSpec = "unspecified"; break;
119 case TSC_imaginary: PrevSpec = "imaginary"; break;
120 case TSC_complex: PrevSpec = "complex"; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000121 }
122 return true;
123}
124
125
Steve Naroff95324142008-02-12 04:08:59 +0000126bool DeclSpec::BadSpecifier(TSS S, const char *&PrevSpec) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 switch (S) {
Steve Naroff95324142008-02-12 04:08:59 +0000128 case TSS_unspecified: PrevSpec = "unspecified"; break;
129 case TSS_signed: PrevSpec = "signed"; break;
130 case TSS_unsigned: PrevSpec = "unsigned"; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 }
132 return true;
133}
134
135const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
136 switch (T) {
137 default: assert(0 && "Unknown typespec!");
138 case DeclSpec::TST_unspecified: return "unspecified";
139 case DeclSpec::TST_void: return "void";
140 case DeclSpec::TST_char: return "char";
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000141 case DeclSpec::TST_wchar: return "wchar_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 case DeclSpec::TST_int: return "int";
143 case DeclSpec::TST_float: return "float";
144 case DeclSpec::TST_double: return "double";
145 case DeclSpec::TST_bool: return "_Bool";
146 case DeclSpec::TST_decimal32: return "_Decimal32";
147 case DeclSpec::TST_decimal64: return "_Decimal64";
148 case DeclSpec::TST_decimal128: return "_Decimal128";
149 case DeclSpec::TST_enum: return "enum";
Chris Lattner99dc9142008-04-13 18:59:07 +0000150 case DeclSpec::TST_class: return "class";
Reid Spencer5f016e22007-07-11 17:01:13 +0000151 case DeclSpec::TST_union: return "union";
152 case DeclSpec::TST_struct: return "struct";
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000153 case DeclSpec::TST_typename: return "type-name";
Steve Naroffd1861fd2007-07-31 12:34:36 +0000154 case DeclSpec::TST_typeofType:
155 case DeclSpec::TST_typeofExpr: return "typeof";
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 }
157}
158
Steve Naroff95324142008-02-12 04:08:59 +0000159bool DeclSpec::BadSpecifier(TST T, const char *&PrevSpec) {
Steve Naroff95324142008-02-12 04:08:59 +0000160 PrevSpec = getSpecifierName(T);
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 return true;
162}
163
Steve Naroff95324142008-02-12 04:08:59 +0000164bool DeclSpec::BadSpecifier(TQ T, const char *&PrevSpec) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000165 switch (T) {
166 case DeclSpec::TQ_unspecified: PrevSpec = "unspecified"; break;
167 case DeclSpec::TQ_const: PrevSpec = "const"; break;
168 case DeclSpec::TQ_restrict: PrevSpec = "restrict"; break;
169 case DeclSpec::TQ_volatile: PrevSpec = "volatile"; break;
170 }
171 return true;
172}
173
174bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
175 const char *&PrevSpec) {
176 if (StorageClassSpec != SCS_unspecified)
Chris Lattner254be6a2008-11-22 08:32:36 +0000177 return BadSpecifier((SCS)StorageClassSpec, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 StorageClassSpec = S;
179 StorageClassSpecLoc = Loc;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000180 assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield");
Reid Spencer5f016e22007-07-11 17:01:13 +0000181 return false;
182}
183
184bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
185 const char *&PrevSpec) {
186 if (SCS_thread_specified) {
187 PrevSpec = "__thread";
188 return true;
189 }
190 SCS_thread_specified = true;
191 SCS_threadLoc = Loc;
192 return false;
193}
194
195
196/// These methods set the specified attribute of the DeclSpec, but return true
197/// and ignore the request if invalid (e.g. "extern" then "auto" is
198/// specified).
199bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
200 const char *&PrevSpec) {
201 if (TypeSpecWidth != TSW_unspecified &&
202 // Allow turning long -> long long.
203 (W != TSW_longlong || TypeSpecWidth != TSW_long))
Chris Lattner254be6a2008-11-22 08:32:36 +0000204 return BadSpecifier((TSW)TypeSpecWidth, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000205 TypeSpecWidth = W;
206 TSWLoc = Loc;
207 return false;
208}
209
210bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
211 const char *&PrevSpec) {
212 if (TypeSpecComplex != TSC_unspecified)
Chris Lattner254be6a2008-11-22 08:32:36 +0000213 return BadSpecifier((TSC)TypeSpecComplex, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000214 TypeSpecComplex = C;
215 TSCLoc = Loc;
216 return false;
217}
218
219bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
220 const char *&PrevSpec) {
221 if (TypeSpecSign != TSS_unspecified)
Chris Lattner254be6a2008-11-22 08:32:36 +0000222 return BadSpecifier((TSS)TypeSpecSign, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000223 TypeSpecSign = S;
224 TSSLoc = Loc;
225 return false;
226}
227
228bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000229 const char *&PrevSpec, void *Rep) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000230 if (TypeSpecType != TST_unspecified)
Chris Lattner254be6a2008-11-22 08:32:36 +0000231 return BadSpecifier((TST)TypeSpecType, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000232 TypeSpecType = T;
233 TypeRep = Rep;
234 TSTLoc = Loc;
235 return false;
236}
237
Douglas Gregorddc29e12009-02-06 22:42:48 +0000238bool DeclSpec::SetTypeSpecError() {
239 TypeSpecType = TST_error;
240 TypeRep = 0;
241 TSTLoc = SourceLocation();
242 return false;
243}
244
Reid Spencer5f016e22007-07-11 17:01:13 +0000245bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
246 const LangOptions &Lang) {
247 // Duplicates turn into warnings pre-C99.
248 if ((TypeQualifiers & T) && !Lang.C99)
249 return BadSpecifier(T, PrevSpec);
250 TypeQualifiers |= T;
251
252 switch (T) {
253 default: assert(0 && "Unknown type qualifier!");
254 case TQ_const: TQ_constLoc = Loc; break;
255 case TQ_restrict: TQ_restrictLoc = Loc; break;
256 case TQ_volatile: TQ_volatileLoc = Loc; break;
257 }
258 return false;
259}
260
261bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec){
262 // 'inline inline' is ok.
263 FS_inline_specified = true;
264 FS_inlineLoc = Loc;
265 return false;
266}
267
Douglas Gregorb48fe382008-10-31 09:07:45 +0000268bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec){
269 // 'virtual virtual' is ok.
270 FS_virtual_specified = true;
271 FS_virtualLoc = Loc;
272 return false;
273}
274
275bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec){
276 // 'explicit explicit' is ok.
277 FS_explicit_specified = true;
278 FS_explicitLoc = Loc;
279 return false;
280}
281
Reid Spencer5f016e22007-07-11 17:01:13 +0000282
283/// Finish - This does final analysis of the declspec, rejecting things like
284/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
285/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
286/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000287void DeclSpec::Finish(Diagnostic &D, Preprocessor &PP) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000288 // Check the type specifier components first.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000289 SourceManager &SrcMgr = PP.getSourceManager();
Reid Spencer5f016e22007-07-11 17:01:13 +0000290
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000291 // signed/unsigned are only valid with int/char/wchar_t.
Reid Spencer5f016e22007-07-11 17:01:13 +0000292 if (TypeSpecSign != TSS_unspecified) {
293 if (TypeSpecType == TST_unspecified)
294 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000295 else if (TypeSpecType != TST_int &&
296 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000297 Diag(D, TSSLoc, SrcMgr, diag::err_invalid_sign_spec)
298 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000299 // signed double -> double.
300 TypeSpecSign = TSS_unspecified;
301 }
302 }
303
304 // Validate the width of the type.
305 switch (TypeSpecWidth) {
306 case TSW_unspecified: break;
307 case TSW_short: // short int
308 case TSW_longlong: // long long int
309 if (TypeSpecType == TST_unspecified)
310 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
311 else if (TypeSpecType != TST_int) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000312 Diag(D, TSWLoc, SrcMgr,
Reid Spencer5f016e22007-07-11 17:01:13 +0000313 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
Chris Lattner254be6a2008-11-22 08:32:36 +0000314 : diag::err_invalid_longlong_spec)
315 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000316 TypeSpecType = TST_int;
317 }
318 break;
319 case TSW_long: // long double, long int
320 if (TypeSpecType == TST_unspecified)
321 TypeSpecType = TST_int; // long -> long int.
322 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000323 Diag(D, TSWLoc, SrcMgr, diag::err_invalid_long_spec)
324 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000325 TypeSpecType = TST_int;
326 }
327 break;
328 }
329
330 // TODO: if the implementation does not implement _Complex or _Imaginary,
331 // disallow their use. Need information about the backend.
332 if (TypeSpecComplex != TSC_unspecified) {
333 if (TypeSpecType == TST_unspecified) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000334 Diag(D, TSCLoc, SrcMgr, diag::ext_plain_complex)
335 << CodeModificationHint::CreateInsertion(
336 PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
337 " double");
Reid Spencer5f016e22007-07-11 17:01:13 +0000338 TypeSpecType = TST_double; // _Complex -> _Complex double.
339 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
340 // Note that this intentionally doesn't include _Complex _Bool.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000341 Diag(D, TSTLoc, SrcMgr, diag::ext_integer_complex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000342 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000343 Diag(D, TSCLoc, SrcMgr, diag::err_invalid_complex_spec)
344 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000345 TypeSpecComplex = TSC_unspecified;
346 }
347 }
348
349 // Verify __thread.
350 if (SCS_thread_specified) {
351 if (StorageClassSpec == SCS_unspecified) {
352 StorageClassSpec = SCS_extern; // '__thread int' -> 'extern __thread int'
353 } else if (StorageClassSpec != SCS_extern &&
354 StorageClassSpec != SCS_static) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000355 Diag(D, getStorageClassSpecLoc(), SrcMgr, diag::err_invalid_thread_spec)
356 << getSpecifierName((SCS)StorageClassSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000357 SCS_thread_specified = false;
358 }
359 }
360
361 // Okay, now we can infer the real type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000362
363 // TODO: return "auto function" and other bad things based on the real type.
364
365 // 'data definition has no type or storage class'?
366}
Daniel Dunbare4858a62008-08-11 03:45:03 +0000367
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000368bool DeclSpec::isMissingDeclaratorOk() {
369 TST tst = getTypeSpecType();
370 return (tst == TST_union
371 || tst == TST_struct
372 || tst == TST_class
373 || tst == TST_enum
Douglas Gregor4920f1f2009-01-12 22:49:06 +0000374 ) && getTypeRep() != 0 && StorageClassSpec != DeclSpec::SCS_typedef;
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000375}