blob: 83bf36c71630f3c66a00a42e3e29fc17b1e997b4 [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"
Daniel Dunbare4858a62008-08-11 03:45:03 +000015#include "clang/Basic/Diagnostic.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,
30 ParamInfo *ArgInfo,
31 unsigned NumArgs,
32 unsigned TypeQuals,
33 SourceLocation Loc,
34 Declarator &TheDeclarator) {
35 DeclaratorChunk I;
36 I.Kind = Function;
37 I.Loc = Loc;
38 I.Fun.hasPrototype = hasProto;
39 I.Fun.isVariadic = isVariadic;
40 I.Fun.DeleteArgInfo = false;
41 I.Fun.TypeQuals = TypeQuals;
42 I.Fun.NumArgs = NumArgs;
43 I.Fun.ArgInfo = 0;
44
45 // new[] an argument array if needed.
46 if (NumArgs) {
47 // If the 'InlineParams' in Declarator is unused and big enough, put our
48 // parameter list there (in an effort to avoid new/delete traffic). If it
49 // is already used (consider a function returning a function pointer) or too
50 // small (function taking too many arguments), go to the heap.
51 if (!TheDeclarator.InlineParamsUsed &&
52 NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
53 I.Fun.ArgInfo = TheDeclarator.InlineParams;
54 I.Fun.DeleteArgInfo = false;
55 TheDeclarator.InlineParamsUsed = true;
56 } else {
57 I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs];
58 I.Fun.DeleteArgInfo = true;
59 }
60 memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs);
61 }
62 return I;
63}
Chris Lattner254be6a2008-11-22 08:32:36 +000064
Reid Spencer5f016e22007-07-11 17:01:13 +000065/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
66///
67unsigned DeclSpec::getParsedSpecifiers() const {
68 unsigned Res = 0;
69 if (StorageClassSpec != SCS_unspecified ||
70 SCS_thread_specified)
71 Res |= PQ_StorageClassSpecifier;
Mike Stumpd4204332008-06-19 19:52:46 +000072
Reid Spencer5f016e22007-07-11 17:01:13 +000073 if (TypeQualifiers != TQ_unspecified)
74 Res |= PQ_TypeQualifier;
75
76 if (hasTypeSpecifier())
77 Res |= PQ_TypeSpecifier;
78
Douglas Gregorb48fe382008-10-31 09:07:45 +000079 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
Reid Spencer5f016e22007-07-11 17:01:13 +000080 Res |= PQ_FunctionSpecifier;
81 return Res;
82}
83
84const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
85 switch (S) {
86 default: assert(0 && "Unknown typespec!");
87 case DeclSpec::SCS_unspecified: return "unspecified";
88 case DeclSpec::SCS_typedef: return "typedef";
89 case DeclSpec::SCS_extern: return "extern";
90 case DeclSpec::SCS_static: return "static";
91 case DeclSpec::SCS_auto: return "auto";
92 case DeclSpec::SCS_register: return "register";
Sebastian Redl669d5d72008-11-14 23:42:31 +000093 case DeclSpec::SCS_mutable: return "mutable";
Reid Spencer5f016e22007-07-11 17:01:13 +000094 }
95}
96
Steve Naroff95324142008-02-12 04:08:59 +000097bool DeclSpec::BadSpecifier(SCS S, const char *&PrevSpec) {
Steve Naroff95324142008-02-12 04:08:59 +000098 PrevSpec = getSpecifierName(S);
Reid Spencer5f016e22007-07-11 17:01:13 +000099 return true;
100}
101
Steve Naroff95324142008-02-12 04:08:59 +0000102bool DeclSpec::BadSpecifier(TSW W, const char *&PrevSpec) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000103 switch (W) {
Steve Naroff95324142008-02-12 04:08:59 +0000104 case TSW_unspecified: PrevSpec = "unspecified"; break;
105 case TSW_short: PrevSpec = "short"; break;
106 case TSW_long: PrevSpec = "long"; break;
107 case TSW_longlong: PrevSpec = "long long"; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 }
109 return true;
110}
111
Steve Naroff95324142008-02-12 04:08:59 +0000112bool DeclSpec::BadSpecifier(TSC C, const char *&PrevSpec) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 switch (C) {
Steve Naroff95324142008-02-12 04:08:59 +0000114 case TSC_unspecified: PrevSpec = "unspecified"; break;
115 case TSC_imaginary: PrevSpec = "imaginary"; break;
116 case TSC_complex: PrevSpec = "complex"; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000117 }
118 return true;
119}
120
121
Steve Naroff95324142008-02-12 04:08:59 +0000122bool DeclSpec::BadSpecifier(TSS S, const char *&PrevSpec) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 switch (S) {
Steve Naroff95324142008-02-12 04:08:59 +0000124 case TSS_unspecified: PrevSpec = "unspecified"; break;
125 case TSS_signed: PrevSpec = "signed"; break;
126 case TSS_unsigned: PrevSpec = "unsigned"; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 }
128 return true;
129}
130
131const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
132 switch (T) {
133 default: assert(0 && "Unknown typespec!");
134 case DeclSpec::TST_unspecified: return "unspecified";
135 case DeclSpec::TST_void: return "void";
136 case DeclSpec::TST_char: return "char";
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000137 case DeclSpec::TST_wchar: return "wchar_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000138 case DeclSpec::TST_int: return "int";
139 case DeclSpec::TST_float: return "float";
140 case DeclSpec::TST_double: return "double";
141 case DeclSpec::TST_bool: return "_Bool";
142 case DeclSpec::TST_decimal32: return "_Decimal32";
143 case DeclSpec::TST_decimal64: return "_Decimal64";
144 case DeclSpec::TST_decimal128: return "_Decimal128";
145 case DeclSpec::TST_enum: return "enum";
Chris Lattner99dc9142008-04-13 18:59:07 +0000146 case DeclSpec::TST_class: return "class";
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 case DeclSpec::TST_union: return "union";
148 case DeclSpec::TST_struct: return "struct";
149 case DeclSpec::TST_typedef: return "typedef";
Steve Naroffd1861fd2007-07-31 12:34:36 +0000150 case DeclSpec::TST_typeofType:
151 case DeclSpec::TST_typeofExpr: return "typeof";
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 }
153}
154
Steve Naroff95324142008-02-12 04:08:59 +0000155bool DeclSpec::BadSpecifier(TST T, const char *&PrevSpec) {
Steve Naroff95324142008-02-12 04:08:59 +0000156 PrevSpec = getSpecifierName(T);
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 return true;
158}
159
Steve Naroff95324142008-02-12 04:08:59 +0000160bool DeclSpec::BadSpecifier(TQ T, const char *&PrevSpec) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 switch (T) {
162 case DeclSpec::TQ_unspecified: PrevSpec = "unspecified"; break;
163 case DeclSpec::TQ_const: PrevSpec = "const"; break;
164 case DeclSpec::TQ_restrict: PrevSpec = "restrict"; break;
165 case DeclSpec::TQ_volatile: PrevSpec = "volatile"; break;
166 }
167 return true;
168}
169
170bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
171 const char *&PrevSpec) {
172 if (StorageClassSpec != SCS_unspecified)
Chris Lattner254be6a2008-11-22 08:32:36 +0000173 return BadSpecifier((SCS)StorageClassSpec, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000174 StorageClassSpec = S;
175 StorageClassSpecLoc = Loc;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000176 assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield");
Reid Spencer5f016e22007-07-11 17:01:13 +0000177 return false;
178}
179
180bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
181 const char *&PrevSpec) {
182 if (SCS_thread_specified) {
183 PrevSpec = "__thread";
184 return true;
185 }
186 SCS_thread_specified = true;
187 SCS_threadLoc = Loc;
188 return false;
189}
190
191
192/// These methods set the specified attribute of the DeclSpec, but return true
193/// and ignore the request if invalid (e.g. "extern" then "auto" is
194/// specified).
195bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
196 const char *&PrevSpec) {
197 if (TypeSpecWidth != TSW_unspecified &&
198 // Allow turning long -> long long.
199 (W != TSW_longlong || TypeSpecWidth != TSW_long))
Chris Lattner254be6a2008-11-22 08:32:36 +0000200 return BadSpecifier((TSW)TypeSpecWidth, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000201 TypeSpecWidth = W;
202 TSWLoc = Loc;
203 return false;
204}
205
206bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
207 const char *&PrevSpec) {
208 if (TypeSpecComplex != TSC_unspecified)
Chris Lattner254be6a2008-11-22 08:32:36 +0000209 return BadSpecifier((TSC)TypeSpecComplex, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000210 TypeSpecComplex = C;
211 TSCLoc = Loc;
212 return false;
213}
214
215bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
216 const char *&PrevSpec) {
217 if (TypeSpecSign != TSS_unspecified)
Chris Lattner254be6a2008-11-22 08:32:36 +0000218 return BadSpecifier((TSS)TypeSpecSign, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000219 TypeSpecSign = S;
220 TSSLoc = Loc;
221 return false;
222}
223
224bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
Argyrios Kyrtzidis39caa082008-08-01 10:35:27 +0000225 const char *&PrevSpec, Action::TypeTy *Rep) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000226 if (TypeSpecType != TST_unspecified)
Chris Lattner254be6a2008-11-22 08:32:36 +0000227 return BadSpecifier((TST)TypeSpecType, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000228 TypeSpecType = T;
229 TypeRep = Rep;
230 TSTLoc = Loc;
231 return false;
232}
233
234bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
235 const LangOptions &Lang) {
236 // Duplicates turn into warnings pre-C99.
237 if ((TypeQualifiers & T) && !Lang.C99)
238 return BadSpecifier(T, PrevSpec);
239 TypeQualifiers |= T;
240
241 switch (T) {
242 default: assert(0 && "Unknown type qualifier!");
243 case TQ_const: TQ_constLoc = Loc; break;
244 case TQ_restrict: TQ_restrictLoc = Loc; break;
245 case TQ_volatile: TQ_volatileLoc = Loc; break;
246 }
247 return false;
248}
249
250bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec){
251 // 'inline inline' is ok.
252 FS_inline_specified = true;
253 FS_inlineLoc = Loc;
254 return false;
255}
256
Douglas Gregorb48fe382008-10-31 09:07:45 +0000257bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec){
258 // 'virtual virtual' is ok.
259 FS_virtual_specified = true;
260 FS_virtualLoc = Loc;
261 return false;
262}
263
264bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec){
265 // 'explicit explicit' is ok.
266 FS_explicit_specified = true;
267 FS_explicitLoc = Loc;
268 return false;
269}
270
Reid Spencer5f016e22007-07-11 17:01:13 +0000271
272/// Finish - This does final analysis of the declspec, rejecting things like
273/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
274/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
275/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000276void DeclSpec::Finish(Diagnostic &D, SourceManager& SrcMgr,
277 const LangOptions &Lang) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000278 // Check the type specifier components first.
279
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000280 // signed/unsigned are only valid with int/char/wchar_t.
Reid Spencer5f016e22007-07-11 17:01:13 +0000281 if (TypeSpecSign != TSS_unspecified) {
282 if (TypeSpecType == TST_unspecified)
283 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000284 else if (TypeSpecType != TST_int &&
285 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000286 Diag(D, TSSLoc, SrcMgr, diag::err_invalid_sign_spec)
287 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000288 // signed double -> double.
289 TypeSpecSign = TSS_unspecified;
290 }
291 }
292
293 // Validate the width of the type.
294 switch (TypeSpecWidth) {
295 case TSW_unspecified: break;
296 case TSW_short: // short int
297 case TSW_longlong: // long long int
298 if (TypeSpecType == TST_unspecified)
299 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
300 else if (TypeSpecType != TST_int) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000301 Diag(D, TSWLoc, SrcMgr,
Reid Spencer5f016e22007-07-11 17:01:13 +0000302 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
Chris Lattner254be6a2008-11-22 08:32:36 +0000303 : diag::err_invalid_longlong_spec)
304 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000305 TypeSpecType = TST_int;
306 }
307 break;
308 case TSW_long: // long double, long int
309 if (TypeSpecType == TST_unspecified)
310 TypeSpecType = TST_int; // long -> long int.
311 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000312 Diag(D, TSWLoc, SrcMgr, diag::err_invalid_long_spec)
313 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000314 TypeSpecType = TST_int;
315 }
316 break;
317 }
318
319 // TODO: if the implementation does not implement _Complex or _Imaginary,
320 // disallow their use. Need information about the backend.
321 if (TypeSpecComplex != TSC_unspecified) {
322 if (TypeSpecType == TST_unspecified) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000323 Diag(D, TSCLoc, SrcMgr, diag::ext_plain_complex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000324 TypeSpecType = TST_double; // _Complex -> _Complex double.
325 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
326 // Note that this intentionally doesn't include _Complex _Bool.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000327 Diag(D, TSTLoc, SrcMgr, diag::ext_integer_complex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000328 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000329 Diag(D, TSCLoc, SrcMgr, diag::err_invalid_complex_spec)
330 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000331 TypeSpecComplex = TSC_unspecified;
332 }
333 }
334
335 // Verify __thread.
336 if (SCS_thread_specified) {
337 if (StorageClassSpec == SCS_unspecified) {
338 StorageClassSpec = SCS_extern; // '__thread int' -> 'extern __thread int'
339 } else if (StorageClassSpec != SCS_extern &&
340 StorageClassSpec != SCS_static) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000341 Diag(D, getStorageClassSpecLoc(), SrcMgr, diag::err_invalid_thread_spec)
342 << getSpecifierName((SCS)StorageClassSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000343 SCS_thread_specified = false;
344 }
345 }
346
347 // Okay, now we can infer the real type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000348
349 // TODO: return "auto function" and other bad things based on the real type.
350
351 // 'data definition has no type or storage class'?
352}
Daniel Dunbare4858a62008-08-11 03:45:03 +0000353
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000354bool DeclSpec::isMissingDeclaratorOk() {
355 TST tst = getTypeSpecType();
356 return (tst == TST_union
357 || tst == TST_struct
358 || tst == TST_class
359 || tst == TST_enum
Douglas Gregor4920f1f2009-01-12 22:49:06 +0000360 ) && getTypeRep() != 0 && StorageClassSpec != DeclSpec::SCS_typedef;
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000361}