blob: a498182df71a18a867e96fe5d05de43cfa669e89 [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"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/Basic/LangOptions.h"
Chris Lattner5af2f352009-01-20 19:11:22 +000017#include "llvm/ADT/STLExtras.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018using namespace clang;
19
Chris Lattner254be6a2008-11-22 08:32:36 +000020
21static DiagnosticBuilder Diag(Diagnostic &D, SourceLocation Loc,
22 SourceManager &SrcMgr, unsigned DiagID) {
23 return D.Report(FullSourceLoc(Loc, SrcMgr), DiagID);
24}
25
26
Chris Lattner5af2f352009-01-20 19:11:22 +000027/// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function.
28/// "TheDeclarator" is the declarator that this will be added to.
29DeclaratorChunk DeclaratorChunk::getFunction(bool hasProto, bool isVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +000030 SourceLocation EllipsisLoc,
Chris Lattner5af2f352009-01-20 19:11:22 +000031 ParamInfo *ArgInfo,
32 unsigned NumArgs,
33 unsigned TypeQuals,
34 SourceLocation Loc,
35 Declarator &TheDeclarator) {
36 DeclaratorChunk I;
37 I.Kind = Function;
38 I.Loc = Loc;
39 I.Fun.hasPrototype = hasProto;
40 I.Fun.isVariadic = isVariadic;
Douglas Gregor965acbb2009-02-18 07:07:28 +000041 I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding();
Chris Lattner5af2f352009-01-20 19:11:22 +000042 I.Fun.DeleteArgInfo = false;
43 I.Fun.TypeQuals = TypeQuals;
44 I.Fun.NumArgs = NumArgs;
45 I.Fun.ArgInfo = 0;
46
47 // new[] an argument array if needed.
48 if (NumArgs) {
49 // If the 'InlineParams' in Declarator is unused and big enough, put our
50 // parameter list there (in an effort to avoid new/delete traffic). If it
51 // is already used (consider a function returning a function pointer) or too
52 // small (function taking too many arguments), go to the heap.
53 if (!TheDeclarator.InlineParamsUsed &&
54 NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
55 I.Fun.ArgInfo = TheDeclarator.InlineParams;
56 I.Fun.DeleteArgInfo = false;
57 TheDeclarator.InlineParamsUsed = true;
58 } else {
59 I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs];
60 I.Fun.DeleteArgInfo = true;
61 }
62 memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs);
63 }
64 return I;
65}
Chris Lattner254be6a2008-11-22 08:32:36 +000066
Reid Spencer5f016e22007-07-11 17:01:13 +000067/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
Chris Lattner2a327d12009-02-27 18:35:46 +000068/// declaration specifier includes.
Reid Spencer5f016e22007-07-11 17:01:13 +000069///
70unsigned DeclSpec::getParsedSpecifiers() const {
71 unsigned Res = 0;
72 if (StorageClassSpec != SCS_unspecified ||
73 SCS_thread_specified)
74 Res |= PQ_StorageClassSpecifier;
Mike Stumpd4204332008-06-19 19:52:46 +000075
Reid Spencer5f016e22007-07-11 17:01:13 +000076 if (TypeQualifiers != TQ_unspecified)
77 Res |= PQ_TypeQualifier;
78
79 if (hasTypeSpecifier())
80 Res |= PQ_TypeSpecifier;
81
Douglas Gregorb48fe382008-10-31 09:07:45 +000082 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
Reid Spencer5f016e22007-07-11 17:01:13 +000083 Res |= PQ_FunctionSpecifier;
84 return Res;
85}
86
87const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
88 switch (S) {
89 default: assert(0 && "Unknown typespec!");
90 case DeclSpec::SCS_unspecified: return "unspecified";
91 case DeclSpec::SCS_typedef: return "typedef";
92 case DeclSpec::SCS_extern: return "extern";
93 case DeclSpec::SCS_static: return "static";
94 case DeclSpec::SCS_auto: return "auto";
95 case DeclSpec::SCS_register: return "register";
Sebastian Redl669d5d72008-11-14 23:42:31 +000096 case DeclSpec::SCS_mutable: return "mutable";
Reid Spencer5f016e22007-07-11 17:01:13 +000097 }
98}
99
Steve Naroff95324142008-02-12 04:08:59 +0000100bool DeclSpec::BadSpecifier(SCS S, const char *&PrevSpec) {
Steve Naroff95324142008-02-12 04:08:59 +0000101 PrevSpec = getSpecifierName(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000102 return true;
103}
104
Steve Naroff95324142008-02-12 04:08:59 +0000105bool DeclSpec::BadSpecifier(TSW W, const char *&PrevSpec) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000106 switch (W) {
Steve Naroff95324142008-02-12 04:08:59 +0000107 case TSW_unspecified: PrevSpec = "unspecified"; break;
108 case TSW_short: PrevSpec = "short"; break;
109 case TSW_long: PrevSpec = "long"; break;
110 case TSW_longlong: PrevSpec = "long long"; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000111 }
112 return true;
113}
114
Steve Naroff95324142008-02-12 04:08:59 +0000115bool DeclSpec::BadSpecifier(TSC C, const char *&PrevSpec) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000116 switch (C) {
Steve Naroff95324142008-02-12 04:08:59 +0000117 case TSC_unspecified: PrevSpec = "unspecified"; break;
118 case TSC_imaginary: PrevSpec = "imaginary"; break;
119 case TSC_complex: PrevSpec = "complex"; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 }
121 return true;
122}
123
124
Steve Naroff95324142008-02-12 04:08:59 +0000125bool DeclSpec::BadSpecifier(TSS S, const char *&PrevSpec) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000126 switch (S) {
Steve Naroff95324142008-02-12 04:08:59 +0000127 case TSS_unspecified: PrevSpec = "unspecified"; break;
128 case TSS_signed: PrevSpec = "signed"; break;
129 case TSS_unsigned: PrevSpec = "unsigned"; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000130 }
131 return true;
132}
133
134const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
135 switch (T) {
136 default: assert(0 && "Unknown typespec!");
137 case DeclSpec::TST_unspecified: return "unspecified";
138 case DeclSpec::TST_void: return "void";
139 case DeclSpec::TST_char: return "char";
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000140 case DeclSpec::TST_wchar: return "wchar_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 case DeclSpec::TST_int: return "int";
142 case DeclSpec::TST_float: return "float";
143 case DeclSpec::TST_double: return "double";
144 case DeclSpec::TST_bool: return "_Bool";
145 case DeclSpec::TST_decimal32: return "_Decimal32";
146 case DeclSpec::TST_decimal64: return "_Decimal64";
147 case DeclSpec::TST_decimal128: return "_Decimal128";
148 case DeclSpec::TST_enum: return "enum";
Chris Lattner99dc9142008-04-13 18:59:07 +0000149 case DeclSpec::TST_class: return "class";
Reid Spencer5f016e22007-07-11 17:01:13 +0000150 case DeclSpec::TST_union: return "union";
151 case DeclSpec::TST_struct: return "struct";
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000152 case DeclSpec::TST_typename: return "type-name";
Steve Naroffd1861fd2007-07-31 12:34:36 +0000153 case DeclSpec::TST_typeofType:
154 case DeclSpec::TST_typeofExpr: return "typeof";
Reid Spencer5f016e22007-07-11 17:01:13 +0000155 }
156}
157
Steve Naroff95324142008-02-12 04:08:59 +0000158bool DeclSpec::BadSpecifier(TST T, const char *&PrevSpec) {
Steve Naroff95324142008-02-12 04:08:59 +0000159 PrevSpec = getSpecifierName(T);
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 return true;
161}
162
Steve Naroff95324142008-02-12 04:08:59 +0000163bool DeclSpec::BadSpecifier(TQ T, const char *&PrevSpec) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 switch (T) {
165 case DeclSpec::TQ_unspecified: PrevSpec = "unspecified"; break;
166 case DeclSpec::TQ_const: PrevSpec = "const"; break;
167 case DeclSpec::TQ_restrict: PrevSpec = "restrict"; break;
168 case DeclSpec::TQ_volatile: PrevSpec = "volatile"; break;
169 }
170 return true;
171}
172
173bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
174 const char *&PrevSpec) {
175 if (StorageClassSpec != SCS_unspecified)
Chris Lattner254be6a2008-11-22 08:32:36 +0000176 return BadSpecifier((SCS)StorageClassSpec, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000177 StorageClassSpec = S;
178 StorageClassSpecLoc = Loc;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000179 assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield");
Reid Spencer5f016e22007-07-11 17:01:13 +0000180 return false;
181}
182
183bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
184 const char *&PrevSpec) {
185 if (SCS_thread_specified) {
186 PrevSpec = "__thread";
187 return true;
188 }
189 SCS_thread_specified = true;
190 SCS_threadLoc = Loc;
191 return false;
192}
193
194
195/// These methods set the specified attribute of the DeclSpec, but return true
196/// and ignore the request if invalid (e.g. "extern" then "auto" is
197/// specified).
198bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
199 const char *&PrevSpec) {
200 if (TypeSpecWidth != TSW_unspecified &&
201 // Allow turning long -> long long.
202 (W != TSW_longlong || TypeSpecWidth != TSW_long))
Chris Lattner254be6a2008-11-22 08:32:36 +0000203 return BadSpecifier((TSW)TypeSpecWidth, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000204 TypeSpecWidth = W;
205 TSWLoc = Loc;
206 return false;
207}
208
209bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
210 const char *&PrevSpec) {
211 if (TypeSpecComplex != TSC_unspecified)
Chris Lattner254be6a2008-11-22 08:32:36 +0000212 return BadSpecifier((TSC)TypeSpecComplex, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 TypeSpecComplex = C;
214 TSCLoc = Loc;
215 return false;
216}
217
218bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
219 const char *&PrevSpec) {
220 if (TypeSpecSign != TSS_unspecified)
Chris Lattner254be6a2008-11-22 08:32:36 +0000221 return BadSpecifier((TSS)TypeSpecSign, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000222 TypeSpecSign = S;
223 TSSLoc = Loc;
224 return false;
225}
226
227bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
Argyrios Kyrtzidis39caa082008-08-01 10:35:27 +0000228 const char *&PrevSpec, Action::TypeTy *Rep) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000229 if (TypeSpecType != TST_unspecified)
Chris Lattner254be6a2008-11-22 08:32:36 +0000230 return BadSpecifier((TST)TypeSpecType, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000231 TypeSpecType = T;
232 TypeRep = Rep;
233 TSTLoc = Loc;
234 return false;
235}
236
Douglas Gregorddc29e12009-02-06 22:42:48 +0000237bool DeclSpec::SetTypeSpecError() {
238 TypeSpecType = TST_error;
239 TypeRep = 0;
240 TSTLoc = SourceLocation();
241 return false;
242}
243
Reid Spencer5f016e22007-07-11 17:01:13 +0000244bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
245 const LangOptions &Lang) {
246 // Duplicates turn into warnings pre-C99.
247 if ((TypeQualifiers & T) && !Lang.C99)
248 return BadSpecifier(T, PrevSpec);
249 TypeQualifiers |= T;
250
251 switch (T) {
252 default: assert(0 && "Unknown type qualifier!");
253 case TQ_const: TQ_constLoc = Loc; break;
254 case TQ_restrict: TQ_restrictLoc = Loc; break;
255 case TQ_volatile: TQ_volatileLoc = Loc; break;
256 }
257 return false;
258}
259
260bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec){
261 // 'inline inline' is ok.
262 FS_inline_specified = true;
263 FS_inlineLoc = Loc;
264 return false;
265}
266
Douglas Gregorb48fe382008-10-31 09:07:45 +0000267bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec){
268 // 'virtual virtual' is ok.
269 FS_virtual_specified = true;
270 FS_virtualLoc = Loc;
271 return false;
272}
273
274bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec){
275 // 'explicit explicit' is ok.
276 FS_explicit_specified = true;
277 FS_explicitLoc = Loc;
278 return false;
279}
280
Reid Spencer5f016e22007-07-11 17:01:13 +0000281
282/// Finish - This does final analysis of the declspec, rejecting things like
283/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
284/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
285/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000286void DeclSpec::Finish(Diagnostic &D, SourceManager& SrcMgr,
287 const LangOptions &Lang) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000288 // Check the type specifier components first.
289
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000290 // signed/unsigned are only valid with int/char/wchar_t.
Reid Spencer5f016e22007-07-11 17:01:13 +0000291 if (TypeSpecSign != TSS_unspecified) {
292 if (TypeSpecType == TST_unspecified)
293 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000294 else if (TypeSpecType != TST_int &&
295 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000296 Diag(D, TSSLoc, SrcMgr, diag::err_invalid_sign_spec)
297 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000298 // signed double -> double.
299 TypeSpecSign = TSS_unspecified;
300 }
301 }
302
303 // Validate the width of the type.
304 switch (TypeSpecWidth) {
305 case TSW_unspecified: break;
306 case TSW_short: // short int
307 case TSW_longlong: // long long int
308 if (TypeSpecType == TST_unspecified)
309 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
310 else if (TypeSpecType != TST_int) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000311 Diag(D, TSWLoc, SrcMgr,
Reid Spencer5f016e22007-07-11 17:01:13 +0000312 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
Chris Lattner254be6a2008-11-22 08:32:36 +0000313 : diag::err_invalid_longlong_spec)
314 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000315 TypeSpecType = TST_int;
316 }
317 break;
318 case TSW_long: // long double, long int
319 if (TypeSpecType == TST_unspecified)
320 TypeSpecType = TST_int; // long -> long int.
321 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000322 Diag(D, TSWLoc, SrcMgr, diag::err_invalid_long_spec)
323 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000324 TypeSpecType = TST_int;
325 }
326 break;
327 }
328
329 // TODO: if the implementation does not implement _Complex or _Imaginary,
330 // disallow their use. Need information about the backend.
331 if (TypeSpecComplex != TSC_unspecified) {
332 if (TypeSpecType == TST_unspecified) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000333 Diag(D, TSCLoc, SrcMgr, diag::ext_plain_complex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000334 TypeSpecType = TST_double; // _Complex -> _Complex double.
335 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
336 // Note that this intentionally doesn't include _Complex _Bool.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000337 Diag(D, TSTLoc, SrcMgr, diag::ext_integer_complex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000338 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000339 Diag(D, TSCLoc, SrcMgr, diag::err_invalid_complex_spec)
340 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000341 TypeSpecComplex = TSC_unspecified;
342 }
343 }
344
345 // Verify __thread.
346 if (SCS_thread_specified) {
347 if (StorageClassSpec == SCS_unspecified) {
348 StorageClassSpec = SCS_extern; // '__thread int' -> 'extern __thread int'
349 } else if (StorageClassSpec != SCS_extern &&
350 StorageClassSpec != SCS_static) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000351 Diag(D, getStorageClassSpecLoc(), SrcMgr, diag::err_invalid_thread_spec)
352 << getSpecifierName((SCS)StorageClassSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000353 SCS_thread_specified = false;
354 }
355 }
356
357 // Okay, now we can infer the real type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000358
359 // TODO: return "auto function" and other bad things based on the real type.
360
361 // 'data definition has no type or storage class'?
362}
Daniel Dunbare4858a62008-08-11 03:45:03 +0000363
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000364bool DeclSpec::isMissingDeclaratorOk() {
365 TST tst = getTypeSpecType();
366 return (tst == TST_union
367 || tst == TST_struct
368 || tst == TST_class
369 || tst == TST_enum
Douglas Gregor4920f1f2009-01-12 22:49:06 +0000370 ) && getTypeRep() != 0 && StorageClassSpec != DeclSpec::SCS_typedef;
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000371}