blob: 28c81496390bc2af542b9f2c93b0e3a5dcf11de0 [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
68///
69unsigned DeclSpec::getParsedSpecifiers() const {
70 unsigned Res = 0;
71 if (StorageClassSpec != SCS_unspecified ||
72 SCS_thread_specified)
73 Res |= PQ_StorageClassSpecifier;
Mike Stumpd4204332008-06-19 19:52:46 +000074
Reid Spencer5f016e22007-07-11 17:01:13 +000075 if (TypeQualifiers != TQ_unspecified)
76 Res |= PQ_TypeQualifier;
77
78 if (hasTypeSpecifier())
79 Res |= PQ_TypeSpecifier;
80
Douglas Gregorb48fe382008-10-31 09:07:45 +000081 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
Reid Spencer5f016e22007-07-11 17:01:13 +000082 Res |= PQ_FunctionSpecifier;
83 return Res;
84}
85
86const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
87 switch (S) {
88 default: assert(0 && "Unknown typespec!");
89 case DeclSpec::SCS_unspecified: return "unspecified";
90 case DeclSpec::SCS_typedef: return "typedef";
91 case DeclSpec::SCS_extern: return "extern";
92 case DeclSpec::SCS_static: return "static";
93 case DeclSpec::SCS_auto: return "auto";
94 case DeclSpec::SCS_register: return "register";
Sebastian Redl669d5d72008-11-14 23:42:31 +000095 case DeclSpec::SCS_mutable: return "mutable";
Reid Spencer5f016e22007-07-11 17:01:13 +000096 }
97}
98
Steve Naroff95324142008-02-12 04:08:59 +000099bool DeclSpec::BadSpecifier(SCS S, const char *&PrevSpec) {
Steve Naroff95324142008-02-12 04:08:59 +0000100 PrevSpec = getSpecifierName(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000101 return true;
102}
103
Steve Naroff95324142008-02-12 04:08:59 +0000104bool DeclSpec::BadSpecifier(TSW W, const char *&PrevSpec) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000105 switch (W) {
Steve Naroff95324142008-02-12 04:08:59 +0000106 case TSW_unspecified: PrevSpec = "unspecified"; break;
107 case TSW_short: PrevSpec = "short"; break;
108 case TSW_long: PrevSpec = "long"; break;
109 case TSW_longlong: PrevSpec = "long long"; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 }
111 return true;
112}
113
Steve Naroff95324142008-02-12 04:08:59 +0000114bool DeclSpec::BadSpecifier(TSC C, const char *&PrevSpec) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 switch (C) {
Steve Naroff95324142008-02-12 04:08:59 +0000116 case TSC_unspecified: PrevSpec = "unspecified"; break;
117 case TSC_imaginary: PrevSpec = "imaginary"; break;
118 case TSC_complex: PrevSpec = "complex"; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 }
120 return true;
121}
122
123
Steve Naroff95324142008-02-12 04:08:59 +0000124bool DeclSpec::BadSpecifier(TSS S, const char *&PrevSpec) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 switch (S) {
Steve Naroff95324142008-02-12 04:08:59 +0000126 case TSS_unspecified: PrevSpec = "unspecified"; break;
127 case TSS_signed: PrevSpec = "signed"; break;
128 case TSS_unsigned: PrevSpec = "unsigned"; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 }
130 return true;
131}
132
133const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
134 switch (T) {
135 default: assert(0 && "Unknown typespec!");
136 case DeclSpec::TST_unspecified: return "unspecified";
137 case DeclSpec::TST_void: return "void";
138 case DeclSpec::TST_char: return "char";
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000139 case DeclSpec::TST_wchar: return "wchar_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000140 case DeclSpec::TST_int: return "int";
141 case DeclSpec::TST_float: return "float";
142 case DeclSpec::TST_double: return "double";
143 case DeclSpec::TST_bool: return "_Bool";
144 case DeclSpec::TST_decimal32: return "_Decimal32";
145 case DeclSpec::TST_decimal64: return "_Decimal64";
146 case DeclSpec::TST_decimal128: return "_Decimal128";
147 case DeclSpec::TST_enum: return "enum";
Chris Lattner99dc9142008-04-13 18:59:07 +0000148 case DeclSpec::TST_class: return "class";
Reid Spencer5f016e22007-07-11 17:01:13 +0000149 case DeclSpec::TST_union: return "union";
150 case DeclSpec::TST_struct: return "struct";
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000151 case DeclSpec::TST_typename: return "type-name";
Steve Naroffd1861fd2007-07-31 12:34:36 +0000152 case DeclSpec::TST_typeofType:
153 case DeclSpec::TST_typeofExpr: return "typeof";
Reid Spencer5f016e22007-07-11 17:01:13 +0000154 }
155}
156
Steve Naroff95324142008-02-12 04:08:59 +0000157bool DeclSpec::BadSpecifier(TST T, const char *&PrevSpec) {
Steve Naroff95324142008-02-12 04:08:59 +0000158 PrevSpec = getSpecifierName(T);
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 return true;
160}
161
Steve Naroff95324142008-02-12 04:08:59 +0000162bool DeclSpec::BadSpecifier(TQ T, const char *&PrevSpec) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 switch (T) {
164 case DeclSpec::TQ_unspecified: PrevSpec = "unspecified"; break;
165 case DeclSpec::TQ_const: PrevSpec = "const"; break;
166 case DeclSpec::TQ_restrict: PrevSpec = "restrict"; break;
167 case DeclSpec::TQ_volatile: PrevSpec = "volatile"; break;
168 }
169 return true;
170}
171
172bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
173 const char *&PrevSpec) {
174 if (StorageClassSpec != SCS_unspecified)
Chris Lattner254be6a2008-11-22 08:32:36 +0000175 return BadSpecifier((SCS)StorageClassSpec, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000176 StorageClassSpec = S;
177 StorageClassSpecLoc = Loc;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000178 assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield");
Reid Spencer5f016e22007-07-11 17:01:13 +0000179 return false;
180}
181
182bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
183 const char *&PrevSpec) {
184 if (SCS_thread_specified) {
185 PrevSpec = "__thread";
186 return true;
187 }
188 SCS_thread_specified = true;
189 SCS_threadLoc = Loc;
190 return false;
191}
192
193
194/// These methods set the specified attribute of the DeclSpec, but return true
195/// and ignore the request if invalid (e.g. "extern" then "auto" is
196/// specified).
197bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
198 const char *&PrevSpec) {
199 if (TypeSpecWidth != TSW_unspecified &&
200 // Allow turning long -> long long.
201 (W != TSW_longlong || TypeSpecWidth != TSW_long))
Chris Lattner254be6a2008-11-22 08:32:36 +0000202 return BadSpecifier((TSW)TypeSpecWidth, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000203 TypeSpecWidth = W;
204 TSWLoc = Loc;
205 return false;
206}
207
208bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
209 const char *&PrevSpec) {
210 if (TypeSpecComplex != TSC_unspecified)
Chris Lattner254be6a2008-11-22 08:32:36 +0000211 return BadSpecifier((TSC)TypeSpecComplex, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000212 TypeSpecComplex = C;
213 TSCLoc = Loc;
214 return false;
215}
216
217bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
218 const char *&PrevSpec) {
219 if (TypeSpecSign != TSS_unspecified)
Chris Lattner254be6a2008-11-22 08:32:36 +0000220 return BadSpecifier((TSS)TypeSpecSign, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000221 TypeSpecSign = S;
222 TSSLoc = Loc;
223 return false;
224}
225
226bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
Argyrios Kyrtzidis39caa082008-08-01 10:35:27 +0000227 const char *&PrevSpec, Action::TypeTy *Rep) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000228 if (TypeSpecType != TST_unspecified)
Chris Lattner254be6a2008-11-22 08:32:36 +0000229 return BadSpecifier((TST)TypeSpecType, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000230 TypeSpecType = T;
231 TypeRep = Rep;
232 TSTLoc = Loc;
233 return false;
234}
235
Douglas Gregorddc29e12009-02-06 22:42:48 +0000236bool DeclSpec::SetTypeSpecError() {
237 TypeSpecType = TST_error;
238 TypeRep = 0;
239 TSTLoc = SourceLocation();
240 return false;
241}
242
Reid Spencer5f016e22007-07-11 17:01:13 +0000243bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
244 const LangOptions &Lang) {
245 // Duplicates turn into warnings pre-C99.
246 if ((TypeQualifiers & T) && !Lang.C99)
247 return BadSpecifier(T, PrevSpec);
248 TypeQualifiers |= T;
249
250 switch (T) {
251 default: assert(0 && "Unknown type qualifier!");
252 case TQ_const: TQ_constLoc = Loc; break;
253 case TQ_restrict: TQ_restrictLoc = Loc; break;
254 case TQ_volatile: TQ_volatileLoc = Loc; break;
255 }
256 return false;
257}
258
259bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec){
260 // 'inline inline' is ok.
261 FS_inline_specified = true;
262 FS_inlineLoc = Loc;
263 return false;
264}
265
Douglas Gregorb48fe382008-10-31 09:07:45 +0000266bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec){
267 // 'virtual virtual' is ok.
268 FS_virtual_specified = true;
269 FS_virtualLoc = Loc;
270 return false;
271}
272
273bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec){
274 // 'explicit explicit' is ok.
275 FS_explicit_specified = true;
276 FS_explicitLoc = Loc;
277 return false;
278}
279
Reid Spencer5f016e22007-07-11 17:01:13 +0000280
281/// Finish - This does final analysis of the declspec, rejecting things like
282/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
283/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
284/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000285void DeclSpec::Finish(Diagnostic &D, SourceManager& SrcMgr,
286 const LangOptions &Lang) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000287 // Check the type specifier components first.
288
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000289 // signed/unsigned are only valid with int/char/wchar_t.
Reid Spencer5f016e22007-07-11 17:01:13 +0000290 if (TypeSpecSign != TSS_unspecified) {
291 if (TypeSpecType == TST_unspecified)
292 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000293 else if (TypeSpecType != TST_int &&
294 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000295 Diag(D, TSSLoc, SrcMgr, diag::err_invalid_sign_spec)
296 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000297 // signed double -> double.
298 TypeSpecSign = TSS_unspecified;
299 }
300 }
301
302 // Validate the width of the type.
303 switch (TypeSpecWidth) {
304 case TSW_unspecified: break;
305 case TSW_short: // short int
306 case TSW_longlong: // long long int
307 if (TypeSpecType == TST_unspecified)
308 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
309 else if (TypeSpecType != TST_int) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000310 Diag(D, TSWLoc, SrcMgr,
Reid Spencer5f016e22007-07-11 17:01:13 +0000311 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
Chris Lattner254be6a2008-11-22 08:32:36 +0000312 : diag::err_invalid_longlong_spec)
313 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000314 TypeSpecType = TST_int;
315 }
316 break;
317 case TSW_long: // long double, long int
318 if (TypeSpecType == TST_unspecified)
319 TypeSpecType = TST_int; // long -> long int.
320 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000321 Diag(D, TSWLoc, SrcMgr, diag::err_invalid_long_spec)
322 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000323 TypeSpecType = TST_int;
324 }
325 break;
326 }
327
328 // TODO: if the implementation does not implement _Complex or _Imaginary,
329 // disallow their use. Need information about the backend.
330 if (TypeSpecComplex != TSC_unspecified) {
331 if (TypeSpecType == TST_unspecified) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000332 Diag(D, TSCLoc, SrcMgr, diag::ext_plain_complex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000333 TypeSpecType = TST_double; // _Complex -> _Complex double.
334 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
335 // Note that this intentionally doesn't include _Complex _Bool.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000336 Diag(D, TSTLoc, SrcMgr, diag::ext_integer_complex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000337 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000338 Diag(D, TSCLoc, SrcMgr, diag::err_invalid_complex_spec)
339 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000340 TypeSpecComplex = TSC_unspecified;
341 }
342 }
343
344 // Verify __thread.
345 if (SCS_thread_specified) {
346 if (StorageClassSpec == SCS_unspecified) {
347 StorageClassSpec = SCS_extern; // '__thread int' -> 'extern __thread int'
348 } else if (StorageClassSpec != SCS_extern &&
349 StorageClassSpec != SCS_static) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000350 Diag(D, getStorageClassSpecLoc(), SrcMgr, diag::err_invalid_thread_spec)
351 << getSpecifierName((SCS)StorageClassSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000352 SCS_thread_specified = false;
353 }
354 }
355
356 // Okay, now we can infer the real type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000357
358 // TODO: return "auto function" and other bad things based on the real type.
359
360 // 'data definition has no type or storage class'?
361}
Daniel Dunbare4858a62008-08-11 03:45:03 +0000362
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000363bool DeclSpec::isMissingDeclaratorOk() {
364 TST tst = getTypeSpecType();
365 return (tst == TST_union
366 || tst == TST_struct
367 || tst == TST_class
368 || tst == TST_enum
Douglas Gregor4920f1f2009-01-12 22:49:06 +0000369 ) && getTypeRep() != 0 && StorageClassSpec != DeclSpec::SCS_typedef;
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000370}