blob: 8b3b2851c1e0c008bd9a6ef6715200ab2678e567 [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,
Sebastian Redl3cc97262009-05-31 11:47:27 +000036 SourceLocation ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +000037 bool hasAnyExceptionSpec,
38 ActionBase::TypeTy **Exceptions,
Sebastian Redlef65f062009-05-29 18:02:33 +000039 SourceRange *ExceptionRanges,
Sebastian Redl7dc81342009-04-29 17:30:04 +000040 unsigned NumExceptions,
Chris Lattner5af2f352009-01-20 19:11:22 +000041 SourceLocation Loc,
42 Declarator &TheDeclarator) {
43 DeclaratorChunk I;
Sebastian Redl7dc81342009-04-29 17:30:04 +000044 I.Kind = Function;
45 I.Loc = Loc;
46 I.Fun.hasPrototype = hasProto;
47 I.Fun.isVariadic = isVariadic;
48 I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding();
49 I.Fun.DeleteArgInfo = false;
50 I.Fun.TypeQuals = TypeQuals;
51 I.Fun.NumArgs = NumArgs;
52 I.Fun.ArgInfo = 0;
53 I.Fun.hasExceptionSpec = hasExceptionSpec;
Sebastian Redl3cc97262009-05-31 11:47:27 +000054 I.Fun.ThrowLoc = ThrowLoc.getRawEncoding();
Sebastian Redl7dc81342009-04-29 17:30:04 +000055 I.Fun.hasAnyExceptionSpec = hasAnyExceptionSpec;
56 I.Fun.NumExceptions = NumExceptions;
57 I.Fun.Exceptions = 0;
58
Chris Lattner5af2f352009-01-20 19:11:22 +000059 // new[] an argument array if needed.
60 if (NumArgs) {
61 // If the 'InlineParams' in Declarator is unused and big enough, put our
62 // parameter list there (in an effort to avoid new/delete traffic). If it
63 // is already used (consider a function returning a function pointer) or too
64 // small (function taking too many arguments), go to the heap.
65 if (!TheDeclarator.InlineParamsUsed &&
66 NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
67 I.Fun.ArgInfo = TheDeclarator.InlineParams;
68 I.Fun.DeleteArgInfo = false;
69 TheDeclarator.InlineParamsUsed = true;
70 } else {
71 I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs];
72 I.Fun.DeleteArgInfo = true;
73 }
74 memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs);
75 }
Sebastian Redl7dc81342009-04-29 17:30:04 +000076 // new[] an exception array if needed
77 if (NumExceptions) {
Sebastian Redlef65f062009-05-29 18:02:33 +000078 I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions];
79 for (unsigned i = 0; i != NumExceptions; ++i) {
80 I.Fun.Exceptions[i].Ty = Exceptions[i];
81 I.Fun.Exceptions[i].Range = ExceptionRanges[i];
82 }
Sebastian Redl7dc81342009-04-29 17:30:04 +000083 }
Chris Lattner5af2f352009-01-20 19:11:22 +000084 return I;
85}
Chris Lattner254be6a2008-11-22 08:32:36 +000086
Reid Spencer5f016e22007-07-11 17:01:13 +000087/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
Chris Lattner2a327d12009-02-27 18:35:46 +000088/// declaration specifier includes.
Reid Spencer5f016e22007-07-11 17:01:13 +000089///
90unsigned DeclSpec::getParsedSpecifiers() const {
91 unsigned Res = 0;
92 if (StorageClassSpec != SCS_unspecified ||
93 SCS_thread_specified)
94 Res |= PQ_StorageClassSpecifier;
Mike Stumpd4204332008-06-19 19:52:46 +000095
Reid Spencer5f016e22007-07-11 17:01:13 +000096 if (TypeQualifiers != TQ_unspecified)
97 Res |= PQ_TypeQualifier;
98
99 if (hasTypeSpecifier())
100 Res |= PQ_TypeSpecifier;
101
Douglas Gregorb48fe382008-10-31 09:07:45 +0000102 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
Reid Spencer5f016e22007-07-11 17:01:13 +0000103 Res |= PQ_FunctionSpecifier;
104 return Res;
105}
106
107const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
108 switch (S) {
109 default: assert(0 && "Unknown typespec!");
110 case DeclSpec::SCS_unspecified: return "unspecified";
111 case DeclSpec::SCS_typedef: return "typedef";
112 case DeclSpec::SCS_extern: return "extern";
113 case DeclSpec::SCS_static: return "static";
114 case DeclSpec::SCS_auto: return "auto";
115 case DeclSpec::SCS_register: return "register";
Eli Friedman63054b32009-04-19 20:27:55 +0000116 case DeclSpec::SCS_private_extern: return "__private_extern__";
Sebastian Redl669d5d72008-11-14 23:42:31 +0000117 case DeclSpec::SCS_mutable: return "mutable";
Reid Spencer5f016e22007-07-11 17:01:13 +0000118 }
119}
120
Steve Naroff95324142008-02-12 04:08:59 +0000121bool DeclSpec::BadSpecifier(SCS S, const char *&PrevSpec) {
Steve Naroff95324142008-02-12 04:08:59 +0000122 PrevSpec = getSpecifierName(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 return true;
124}
125
Steve Naroff95324142008-02-12 04:08:59 +0000126bool DeclSpec::BadSpecifier(TSW W, const char *&PrevSpec) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 switch (W) {
Steve Naroff95324142008-02-12 04:08:59 +0000128 case TSW_unspecified: PrevSpec = "unspecified"; break;
129 case TSW_short: PrevSpec = "short"; break;
130 case TSW_long: PrevSpec = "long"; break;
131 case TSW_longlong: PrevSpec = "long long"; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 }
133 return true;
134}
135
Steve Naroff95324142008-02-12 04:08:59 +0000136bool DeclSpec::BadSpecifier(TSC C, const char *&PrevSpec) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000137 switch (C) {
Steve Naroff95324142008-02-12 04:08:59 +0000138 case TSC_unspecified: PrevSpec = "unspecified"; break;
139 case TSC_imaginary: PrevSpec = "imaginary"; break;
140 case TSC_complex: PrevSpec = "complex"; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 }
142 return true;
143}
144
145
Steve Naroff95324142008-02-12 04:08:59 +0000146bool DeclSpec::BadSpecifier(TSS S, const char *&PrevSpec) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 switch (S) {
Steve Naroff95324142008-02-12 04:08:59 +0000148 case TSS_unspecified: PrevSpec = "unspecified"; break;
149 case TSS_signed: PrevSpec = "signed"; break;
150 case TSS_unsigned: PrevSpec = "unsigned"; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000151 }
152 return true;
153}
154
155const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
156 switch (T) {
157 default: assert(0 && "Unknown typespec!");
158 case DeclSpec::TST_unspecified: return "unspecified";
159 case DeclSpec::TST_void: return "void";
160 case DeclSpec::TST_char: return "char";
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000161 case DeclSpec::TST_wchar: return "wchar_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000162 case DeclSpec::TST_int: return "int";
163 case DeclSpec::TST_float: return "float";
164 case DeclSpec::TST_double: return "double";
165 case DeclSpec::TST_bool: return "_Bool";
166 case DeclSpec::TST_decimal32: return "_Decimal32";
167 case DeclSpec::TST_decimal64: return "_Decimal64";
168 case DeclSpec::TST_decimal128: return "_Decimal128";
169 case DeclSpec::TST_enum: return "enum";
Chris Lattner99dc9142008-04-13 18:59:07 +0000170 case DeclSpec::TST_class: return "class";
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 case DeclSpec::TST_union: return "union";
172 case DeclSpec::TST_struct: return "struct";
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000173 case DeclSpec::TST_typename: return "type-name";
Steve Naroffd1861fd2007-07-31 12:34:36 +0000174 case DeclSpec::TST_typeofType:
175 case DeclSpec::TST_typeofExpr: return "typeof";
Anders Carlssone89d1592009-06-26 18:41:36 +0000176 case DeclSpec::TST_auto: return "auto";
Reid Spencer5f016e22007-07-11 17:01:13 +0000177 }
178}
179
Steve Naroff95324142008-02-12 04:08:59 +0000180bool DeclSpec::BadSpecifier(TST T, const char *&PrevSpec) {
Steve Naroff95324142008-02-12 04:08:59 +0000181 PrevSpec = getSpecifierName(T);
Reid Spencer5f016e22007-07-11 17:01:13 +0000182 return true;
183}
184
Steve Naroff95324142008-02-12 04:08:59 +0000185bool DeclSpec::BadSpecifier(TQ T, const char *&PrevSpec) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 switch (T) {
187 case DeclSpec::TQ_unspecified: PrevSpec = "unspecified"; break;
188 case DeclSpec::TQ_const: PrevSpec = "const"; break;
189 case DeclSpec::TQ_restrict: PrevSpec = "restrict"; break;
190 case DeclSpec::TQ_volatile: PrevSpec = "volatile"; break;
191 }
192 return true;
193}
194
195bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
196 const char *&PrevSpec) {
197 if (StorageClassSpec != SCS_unspecified)
Chris Lattner254be6a2008-11-22 08:32:36 +0000198 return BadSpecifier((SCS)StorageClassSpec, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000199 StorageClassSpec = S;
200 StorageClassSpecLoc = Loc;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000201 assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield");
Reid Spencer5f016e22007-07-11 17:01:13 +0000202 return false;
203}
204
205bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
206 const char *&PrevSpec) {
207 if (SCS_thread_specified) {
208 PrevSpec = "__thread";
209 return true;
210 }
211 SCS_thread_specified = true;
212 SCS_threadLoc = Loc;
213 return false;
214}
215
216
217/// These methods set the specified attribute of the DeclSpec, but return true
218/// and ignore the request if invalid (e.g. "extern" then "auto" is
219/// specified).
220bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
221 const char *&PrevSpec) {
222 if (TypeSpecWidth != TSW_unspecified &&
223 // Allow turning long -> long long.
224 (W != TSW_longlong || TypeSpecWidth != TSW_long))
Chris Lattner254be6a2008-11-22 08:32:36 +0000225 return BadSpecifier((TSW)TypeSpecWidth, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000226 TypeSpecWidth = W;
227 TSWLoc = Loc;
228 return false;
229}
230
231bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
232 const char *&PrevSpec) {
233 if (TypeSpecComplex != TSC_unspecified)
Chris Lattner254be6a2008-11-22 08:32:36 +0000234 return BadSpecifier((TSC)TypeSpecComplex, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000235 TypeSpecComplex = C;
236 TSCLoc = Loc;
237 return false;
238}
239
240bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
241 const char *&PrevSpec) {
242 if (TypeSpecSign != TSS_unspecified)
Chris Lattner254be6a2008-11-22 08:32:36 +0000243 return BadSpecifier((TSS)TypeSpecSign, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000244 TypeSpecSign = S;
245 TSSLoc = Loc;
246 return false;
247}
248
249bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
Douglas Gregor402abb52009-05-28 23:31:59 +0000250 const char *&PrevSpec, void *Rep,
251 bool Owned) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000252 if (TypeSpecType != TST_unspecified)
Chris Lattner254be6a2008-11-22 08:32:36 +0000253 return BadSpecifier((TST)TypeSpecType, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000254 TypeSpecType = T;
255 TypeRep = Rep;
256 TSTLoc = Loc;
Douglas Gregor402abb52009-05-28 23:31:59 +0000257 TypeSpecOwned = Owned;
Reid Spencer5f016e22007-07-11 17:01:13 +0000258 return false;
259}
260
Douglas Gregorddc29e12009-02-06 22:42:48 +0000261bool DeclSpec::SetTypeSpecError() {
262 TypeSpecType = TST_error;
263 TypeRep = 0;
264 TSTLoc = SourceLocation();
265 return false;
266}
267
Reid Spencer5f016e22007-07-11 17:01:13 +0000268bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
269 const LangOptions &Lang) {
270 // Duplicates turn into warnings pre-C99.
271 if ((TypeQualifiers & T) && !Lang.C99)
272 return BadSpecifier(T, PrevSpec);
273 TypeQualifiers |= T;
274
275 switch (T) {
276 default: assert(0 && "Unknown type qualifier!");
277 case TQ_const: TQ_constLoc = Loc; break;
278 case TQ_restrict: TQ_restrictLoc = Loc; break;
279 case TQ_volatile: TQ_volatileLoc = Loc; break;
280 }
281 return false;
282}
283
284bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec){
285 // 'inline inline' is ok.
286 FS_inline_specified = true;
287 FS_inlineLoc = Loc;
288 return false;
289}
290
Douglas Gregorb48fe382008-10-31 09:07:45 +0000291bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec){
292 // 'virtual virtual' is ok.
293 FS_virtual_specified = true;
294 FS_virtualLoc = Loc;
295 return false;
296}
297
298bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec){
299 // 'explicit explicit' is ok.
300 FS_explicit_specified = true;
301 FS_explicitLoc = Loc;
302 return false;
303}
304
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000305bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec) {
306 if (Friend_specified) {
307 PrevSpec = "friend";
308 return true;
309 }
310
311 Friend_specified = true;
312 FriendLoc = Loc;
313 return false;
314}
Reid Spencer5f016e22007-07-11 17:01:13 +0000315
316/// Finish - This does final analysis of the declspec, rejecting things like
317/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
318/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
319/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000320void DeclSpec::Finish(Diagnostic &D, Preprocessor &PP) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000321 // Check the type specifier components first.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000322 SourceManager &SrcMgr = PP.getSourceManager();
Reid Spencer5f016e22007-07-11 17:01:13 +0000323
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000324 // signed/unsigned are only valid with int/char/wchar_t.
Reid Spencer5f016e22007-07-11 17:01:13 +0000325 if (TypeSpecSign != TSS_unspecified) {
326 if (TypeSpecType == TST_unspecified)
327 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000328 else if (TypeSpecType != TST_int &&
329 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000330 Diag(D, TSSLoc, SrcMgr, diag::err_invalid_sign_spec)
331 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000332 // signed double -> double.
333 TypeSpecSign = TSS_unspecified;
334 }
335 }
336
337 // Validate the width of the type.
338 switch (TypeSpecWidth) {
339 case TSW_unspecified: break;
340 case TSW_short: // short int
341 case TSW_longlong: // long long int
342 if (TypeSpecType == TST_unspecified)
343 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
344 else if (TypeSpecType != TST_int) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000345 Diag(D, TSWLoc, SrcMgr,
Reid Spencer5f016e22007-07-11 17:01:13 +0000346 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
Chris Lattner254be6a2008-11-22 08:32:36 +0000347 : diag::err_invalid_longlong_spec)
348 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000349 TypeSpecType = TST_int;
350 }
351 break;
352 case TSW_long: // long double, long int
353 if (TypeSpecType == TST_unspecified)
354 TypeSpecType = TST_int; // long -> long int.
355 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000356 Diag(D, TSWLoc, SrcMgr, diag::err_invalid_long_spec)
357 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000358 TypeSpecType = TST_int;
359 }
360 break;
361 }
362
363 // TODO: if the implementation does not implement _Complex or _Imaginary,
364 // disallow their use. Need information about the backend.
365 if (TypeSpecComplex != TSC_unspecified) {
366 if (TypeSpecType == TST_unspecified) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000367 Diag(D, TSCLoc, SrcMgr, diag::ext_plain_complex)
368 << CodeModificationHint::CreateInsertion(
369 PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
370 " double");
Reid Spencer5f016e22007-07-11 17:01:13 +0000371 TypeSpecType = TST_double; // _Complex -> _Complex double.
372 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
373 // Note that this intentionally doesn't include _Complex _Bool.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000374 Diag(D, TSTLoc, SrcMgr, diag::ext_integer_complex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000375 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000376 Diag(D, TSCLoc, SrcMgr, diag::err_invalid_complex_spec)
377 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000378 TypeSpecComplex = TSC_unspecified;
379 }
380 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000381
382 // Okay, now we can infer the real type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000383
384 // TODO: return "auto function" and other bad things based on the real type.
385
386 // 'data definition has no type or storage class'?
387}
Daniel Dunbare4858a62008-08-11 03:45:03 +0000388
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000389bool DeclSpec::isMissingDeclaratorOk() {
390 TST tst = getTypeSpecType();
391 return (tst == TST_union
392 || tst == TST_struct
393 || tst == TST_class
394 || tst == TST_enum
Douglas Gregor4920f1f2009-01-12 22:49:06 +0000395 ) && getTypeRep() != 0 && StorageClassSpec != DeclSpec::SCS_typedef;
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000396}