blob: d742ef2ed093a93033a25f9ea0b33fdd5a12eeb6 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- SemaDeclSpec.cpp - Declaration Specifier Semantic Analysis -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for declaration specifiers.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/DeclSpec.h"
Chris Lattner545f39e2009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Douglas Gregor1ba5cb32009-04-01 22:41:11 +000016#include "clang/Lex/Preprocessor.h"
Chris Lattner4b009652007-07-25 00:24:17 +000017#include "clang/Basic/LangOptions.h"
Chris Lattnerdefaf412009-01-20 19:11:22 +000018#include "llvm/ADT/STLExtras.h"
Douglas Gregor734b4ba2009-03-19 00:18:19 +000019#include <cstring>
Chris Lattner4b009652007-07-25 00:24:17 +000020using namespace clang;
21
Chris Lattner5a63b092008-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 Lattnerdefaf412009-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 Gregor88a25f82009-02-18 07:07:28 +000031 SourceLocation EllipsisLoc,
Chris Lattnerdefaf412009-01-20 19:11:22 +000032 ParamInfo *ArgInfo,
33 unsigned NumArgs,
34 unsigned TypeQuals,
Sebastian Redl35f3a5b2009-04-29 17:30:04 +000035 bool hasExceptionSpec,
36 bool hasAnyExceptionSpec,
37 ActionBase::TypeTy **Exceptions,
38 unsigned NumExceptions,
Chris Lattnerdefaf412009-01-20 19:11:22 +000039 SourceLocation Loc,
40 Declarator &TheDeclarator) {
41 DeclaratorChunk I;
Sebastian Redl35f3a5b2009-04-29 17:30:04 +000042 I.Kind = Function;
43 I.Loc = Loc;
44 I.Fun.hasPrototype = hasProto;
45 I.Fun.isVariadic = isVariadic;
46 I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding();
47 I.Fun.DeleteArgInfo = false;
48 I.Fun.TypeQuals = TypeQuals;
49 I.Fun.NumArgs = NumArgs;
50 I.Fun.ArgInfo = 0;
51 I.Fun.hasExceptionSpec = hasExceptionSpec;
52 I.Fun.hasAnyExceptionSpec = hasAnyExceptionSpec;
53 I.Fun.NumExceptions = NumExceptions;
54 I.Fun.Exceptions = 0;
55
Chris Lattnerdefaf412009-01-20 19:11:22 +000056 // new[] an argument array if needed.
57 if (NumArgs) {
58 // If the 'InlineParams' in Declarator is unused and big enough, put our
59 // parameter list there (in an effort to avoid new/delete traffic). If it
60 // is already used (consider a function returning a function pointer) or too
61 // small (function taking too many arguments), go to the heap.
62 if (!TheDeclarator.InlineParamsUsed &&
63 NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
64 I.Fun.ArgInfo = TheDeclarator.InlineParams;
65 I.Fun.DeleteArgInfo = false;
66 TheDeclarator.InlineParamsUsed = true;
67 } else {
68 I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs];
69 I.Fun.DeleteArgInfo = true;
70 }
71 memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs);
72 }
Sebastian Redl35f3a5b2009-04-29 17:30:04 +000073 // new[] an exception array if needed
74 if (NumExceptions) {
75 I.Fun.Exceptions = new ActionBase::TypeTy*[NumExceptions];
76 memcpy(I.Fun.Exceptions, Exceptions,
77 sizeof(ActionBase::TypeTy*)*NumExceptions);
78 }
Chris Lattnerdefaf412009-01-20 19:11:22 +000079 return I;
80}
Chris Lattner5a63b092008-11-22 08:32:36 +000081
Chris Lattner4b009652007-07-25 00:24:17 +000082/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
Chris Lattner92eca3e2009-02-27 18:35:46 +000083/// declaration specifier includes.
Chris Lattner4b009652007-07-25 00:24:17 +000084///
85unsigned DeclSpec::getParsedSpecifiers() const {
86 unsigned Res = 0;
87 if (StorageClassSpec != SCS_unspecified ||
88 SCS_thread_specified)
89 Res |= PQ_StorageClassSpecifier;
Mike Stumpea5a8b32008-06-19 19:52:46 +000090
Chris Lattner4b009652007-07-25 00:24:17 +000091 if (TypeQualifiers != TQ_unspecified)
92 Res |= PQ_TypeQualifier;
93
94 if (hasTypeSpecifier())
95 Res |= PQ_TypeSpecifier;
96
Douglas Gregorf15ac4b2008-10-31 09:07:45 +000097 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
Chris Lattner4b009652007-07-25 00:24:17 +000098 Res |= PQ_FunctionSpecifier;
99 return Res;
100}
101
102const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
103 switch (S) {
104 default: assert(0 && "Unknown typespec!");
105 case DeclSpec::SCS_unspecified: return "unspecified";
106 case DeclSpec::SCS_typedef: return "typedef";
107 case DeclSpec::SCS_extern: return "extern";
108 case DeclSpec::SCS_static: return "static";
109 case DeclSpec::SCS_auto: return "auto";
110 case DeclSpec::SCS_register: return "register";
Eli Friedman1a3e74c2009-04-19 20:27:55 +0000111 case DeclSpec::SCS_private_extern: return "__private_extern__";
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000112 case DeclSpec::SCS_mutable: return "mutable";
Chris Lattner4b009652007-07-25 00:24:17 +0000113 }
114}
115
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000116bool DeclSpec::BadSpecifier(SCS S, const char *&PrevSpec) {
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000117 PrevSpec = getSpecifierName(S);
Chris Lattner4b009652007-07-25 00:24:17 +0000118 return true;
119}
120
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000121bool DeclSpec::BadSpecifier(TSW W, const char *&PrevSpec) {
Chris Lattner4b009652007-07-25 00:24:17 +0000122 switch (W) {
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000123 case TSW_unspecified: PrevSpec = "unspecified"; break;
124 case TSW_short: PrevSpec = "short"; break;
125 case TSW_long: PrevSpec = "long"; break;
126 case TSW_longlong: PrevSpec = "long long"; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000127 }
128 return true;
129}
130
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000131bool DeclSpec::BadSpecifier(TSC C, const char *&PrevSpec) {
Chris Lattner4b009652007-07-25 00:24:17 +0000132 switch (C) {
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000133 case TSC_unspecified: PrevSpec = "unspecified"; break;
134 case TSC_imaginary: PrevSpec = "imaginary"; break;
135 case TSC_complex: PrevSpec = "complex"; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000136 }
137 return true;
138}
139
140
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000141bool DeclSpec::BadSpecifier(TSS S, const char *&PrevSpec) {
Chris Lattner4b009652007-07-25 00:24:17 +0000142 switch (S) {
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000143 case TSS_unspecified: PrevSpec = "unspecified"; break;
144 case TSS_signed: PrevSpec = "signed"; break;
145 case TSS_unsigned: PrevSpec = "unsigned"; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000146 }
147 return true;
148}
149
150const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
151 switch (T) {
152 default: assert(0 && "Unknown typespec!");
153 case DeclSpec::TST_unspecified: return "unspecified";
154 case DeclSpec::TST_void: return "void";
155 case DeclSpec::TST_char: return "char";
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000156 case DeclSpec::TST_wchar: return "wchar_t";
Chris Lattner4b009652007-07-25 00:24:17 +0000157 case DeclSpec::TST_int: return "int";
158 case DeclSpec::TST_float: return "float";
159 case DeclSpec::TST_double: return "double";
160 case DeclSpec::TST_bool: return "_Bool";
161 case DeclSpec::TST_decimal32: return "_Decimal32";
162 case DeclSpec::TST_decimal64: return "_Decimal64";
163 case DeclSpec::TST_decimal128: return "_Decimal128";
164 case DeclSpec::TST_enum: return "enum";
Chris Lattner2e78db32008-04-13 18:59:07 +0000165 case DeclSpec::TST_class: return "class";
Chris Lattner4b009652007-07-25 00:24:17 +0000166 case DeclSpec::TST_union: return "union";
167 case DeclSpec::TST_struct: return "struct";
Douglas Gregora60c62e2009-02-09 15:09:02 +0000168 case DeclSpec::TST_typename: return "type-name";
Steve Naroff7cbb1462007-07-31 12:34:36 +0000169 case DeclSpec::TST_typeofType:
170 case DeclSpec::TST_typeofExpr: return "typeof";
Chris Lattner4b009652007-07-25 00:24:17 +0000171 }
172}
173
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000174bool DeclSpec::BadSpecifier(TST T, const char *&PrevSpec) {
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000175 PrevSpec = getSpecifierName(T);
Chris Lattner4b009652007-07-25 00:24:17 +0000176 return true;
177}
178
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000179bool DeclSpec::BadSpecifier(TQ T, const char *&PrevSpec) {
Chris Lattner4b009652007-07-25 00:24:17 +0000180 switch (T) {
181 case DeclSpec::TQ_unspecified: PrevSpec = "unspecified"; break;
182 case DeclSpec::TQ_const: PrevSpec = "const"; break;
183 case DeclSpec::TQ_restrict: PrevSpec = "restrict"; break;
184 case DeclSpec::TQ_volatile: PrevSpec = "volatile"; break;
185 }
186 return true;
187}
188
189bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
190 const char *&PrevSpec) {
191 if (StorageClassSpec != SCS_unspecified)
Chris Lattner5a63b092008-11-22 08:32:36 +0000192 return BadSpecifier((SCS)StorageClassSpec, PrevSpec);
Chris Lattner4b009652007-07-25 00:24:17 +0000193 StorageClassSpec = S;
194 StorageClassSpecLoc = Loc;
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000195 assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield");
Chris Lattner4b009652007-07-25 00:24:17 +0000196 return false;
197}
198
199bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
200 const char *&PrevSpec) {
201 if (SCS_thread_specified) {
202 PrevSpec = "__thread";
203 return true;
204 }
205 SCS_thread_specified = true;
206 SCS_threadLoc = Loc;
207 return false;
208}
209
210
211/// These methods set the specified attribute of the DeclSpec, but return true
212/// and ignore the request if invalid (e.g. "extern" then "auto" is
213/// specified).
214bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
215 const char *&PrevSpec) {
216 if (TypeSpecWidth != TSW_unspecified &&
217 // Allow turning long -> long long.
218 (W != TSW_longlong || TypeSpecWidth != TSW_long))
Chris Lattner5a63b092008-11-22 08:32:36 +0000219 return BadSpecifier((TSW)TypeSpecWidth, PrevSpec);
Chris Lattner4b009652007-07-25 00:24:17 +0000220 TypeSpecWidth = W;
221 TSWLoc = Loc;
222 return false;
223}
224
225bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
226 const char *&PrevSpec) {
227 if (TypeSpecComplex != TSC_unspecified)
Chris Lattner5a63b092008-11-22 08:32:36 +0000228 return BadSpecifier((TSC)TypeSpecComplex, PrevSpec);
Chris Lattner4b009652007-07-25 00:24:17 +0000229 TypeSpecComplex = C;
230 TSCLoc = Loc;
231 return false;
232}
233
234bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
235 const char *&PrevSpec) {
236 if (TypeSpecSign != TSS_unspecified)
Chris Lattner5a63b092008-11-22 08:32:36 +0000237 return BadSpecifier((TSS)TypeSpecSign, PrevSpec);
Chris Lattner4b009652007-07-25 00:24:17 +0000238 TypeSpecSign = S;
239 TSSLoc = Loc;
240 return false;
241}
242
243bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000244 const char *&PrevSpec, void *Rep) {
Chris Lattner4b009652007-07-25 00:24:17 +0000245 if (TypeSpecType != TST_unspecified)
Chris Lattner5a63b092008-11-22 08:32:36 +0000246 return BadSpecifier((TST)TypeSpecType, PrevSpec);
Chris Lattner4b009652007-07-25 00:24:17 +0000247 TypeSpecType = T;
248 TypeRep = Rep;
249 TSTLoc = Loc;
250 return false;
251}
252
Douglas Gregord406b032009-02-06 22:42:48 +0000253bool DeclSpec::SetTypeSpecError() {
254 TypeSpecType = TST_error;
255 TypeRep = 0;
256 TSTLoc = SourceLocation();
257 return false;
258}
259
Chris Lattner4b009652007-07-25 00:24:17 +0000260bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
261 const LangOptions &Lang) {
262 // Duplicates turn into warnings pre-C99.
263 if ((TypeQualifiers & T) && !Lang.C99)
264 return BadSpecifier(T, PrevSpec);
265 TypeQualifiers |= T;
266
267 switch (T) {
268 default: assert(0 && "Unknown type qualifier!");
269 case TQ_const: TQ_constLoc = Loc; break;
270 case TQ_restrict: TQ_restrictLoc = Loc; break;
271 case TQ_volatile: TQ_volatileLoc = Loc; break;
272 }
273 return false;
274}
275
276bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec){
277 // 'inline inline' is ok.
278 FS_inline_specified = true;
279 FS_inlineLoc = Loc;
280 return false;
281}
282
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000283bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec){
284 // 'virtual virtual' is ok.
285 FS_virtual_specified = true;
286 FS_virtualLoc = Loc;
287 return false;
288}
289
290bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec){
291 // 'explicit explicit' is ok.
292 FS_explicit_specified = true;
293 FS_explicitLoc = Loc;
294 return false;
295}
296
Chris Lattner4b009652007-07-25 00:24:17 +0000297
298/// Finish - This does final analysis of the declspec, rejecting things like
299/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
300/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
301/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Douglas Gregor1ba5cb32009-04-01 22:41:11 +0000302void DeclSpec::Finish(Diagnostic &D, Preprocessor &PP) {
Chris Lattner4b009652007-07-25 00:24:17 +0000303 // Check the type specifier components first.
Douglas Gregor1ba5cb32009-04-01 22:41:11 +0000304 SourceManager &SrcMgr = PP.getSourceManager();
Chris Lattner4b009652007-07-25 00:24:17 +0000305
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000306 // signed/unsigned are only valid with int/char/wchar_t.
Chris Lattner4b009652007-07-25 00:24:17 +0000307 if (TypeSpecSign != TSS_unspecified) {
308 if (TypeSpecType == TST_unspecified)
309 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000310 else if (TypeSpecType != TST_int &&
311 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
Chris Lattner5a63b092008-11-22 08:32:36 +0000312 Diag(D, TSSLoc, SrcMgr, diag::err_invalid_sign_spec)
313 << getSpecifierName((TST)TypeSpecType);
Chris Lattner4b009652007-07-25 00:24:17 +0000314 // signed double -> double.
315 TypeSpecSign = TSS_unspecified;
316 }
317 }
318
319 // Validate the width of the type.
320 switch (TypeSpecWidth) {
321 case TSW_unspecified: break;
322 case TSW_short: // short int
323 case TSW_longlong: // long long int
324 if (TypeSpecType == TST_unspecified)
325 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
326 else if (TypeSpecType != TST_int) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000327 Diag(D, TSWLoc, SrcMgr,
Chris Lattner4b009652007-07-25 00:24:17 +0000328 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
Chris Lattner5a63b092008-11-22 08:32:36 +0000329 : diag::err_invalid_longlong_spec)
330 << getSpecifierName((TST)TypeSpecType);
Chris Lattner4b009652007-07-25 00:24:17 +0000331 TypeSpecType = TST_int;
332 }
333 break;
334 case TSW_long: // long double, long int
335 if (TypeSpecType == TST_unspecified)
336 TypeSpecType = TST_int; // long -> long int.
337 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Chris Lattner5a63b092008-11-22 08:32:36 +0000338 Diag(D, TSWLoc, SrcMgr, diag::err_invalid_long_spec)
339 << getSpecifierName((TST)TypeSpecType);
Chris Lattner4b009652007-07-25 00:24:17 +0000340 TypeSpecType = TST_int;
341 }
342 break;
343 }
344
345 // TODO: if the implementation does not implement _Complex or _Imaginary,
346 // disallow their use. Need information about the backend.
347 if (TypeSpecComplex != TSC_unspecified) {
348 if (TypeSpecType == TST_unspecified) {
Douglas Gregor1ba5cb32009-04-01 22:41:11 +0000349 Diag(D, TSCLoc, SrcMgr, diag::ext_plain_complex)
350 << CodeModificationHint::CreateInsertion(
351 PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
352 " double");
Chris Lattner4b009652007-07-25 00:24:17 +0000353 TypeSpecType = TST_double; // _Complex -> _Complex double.
354 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
355 // Note that this intentionally doesn't include _Complex _Bool.
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000356 Diag(D, TSTLoc, SrcMgr, diag::ext_integer_complex);
Chris Lattner4b009652007-07-25 00:24:17 +0000357 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Chris Lattner5a63b092008-11-22 08:32:36 +0000358 Diag(D, TSCLoc, SrcMgr, diag::err_invalid_complex_spec)
359 << getSpecifierName((TST)TypeSpecType);
Chris Lattner4b009652007-07-25 00:24:17 +0000360 TypeSpecComplex = TSC_unspecified;
361 }
362 }
Chris Lattner4b009652007-07-25 00:24:17 +0000363
364 // Okay, now we can infer the real type.
365
366 // TODO: return "auto function" and other bad things based on the real type.
367
368 // 'data definition has no type or storage class'?
369}
Daniel Dunbarcc7b1602008-08-11 03:45:03 +0000370
Sebastian Redlb7605e82008-12-28 15:28:59 +0000371bool DeclSpec::isMissingDeclaratorOk() {
372 TST tst = getTypeSpecType();
373 return (tst == TST_union
374 || tst == TST_struct
375 || tst == TST_class
376 || tst == TST_enum
Douglas Gregorb748fc52009-01-12 22:49:06 +0000377 ) && getTypeRep() != 0 && StorageClassSpec != DeclSpec::SCS_typedef;
Sebastian Redlb7605e82008-12-28 15:28:59 +0000378}