blob: f3ff0c63797f08e12122ae094407be43db936673 [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,
Sebastian Redlaaacda92009-05-29 18:02:33 +000038 SourceRange *ExceptionRanges,
Sebastian Redl35f3a5b2009-04-29 17:30:04 +000039 unsigned NumExceptions,
Chris Lattnerdefaf412009-01-20 19:11:22 +000040 SourceLocation Loc,
41 Declarator &TheDeclarator) {
42 DeclaratorChunk I;
Sebastian Redl35f3a5b2009-04-29 17:30:04 +000043 I.Kind = Function;
44 I.Loc = Loc;
45 I.Fun.hasPrototype = hasProto;
46 I.Fun.isVariadic = isVariadic;
47 I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding();
48 I.Fun.DeleteArgInfo = false;
49 I.Fun.TypeQuals = TypeQuals;
50 I.Fun.NumArgs = NumArgs;
51 I.Fun.ArgInfo = 0;
52 I.Fun.hasExceptionSpec = hasExceptionSpec;
53 I.Fun.hasAnyExceptionSpec = hasAnyExceptionSpec;
54 I.Fun.NumExceptions = NumExceptions;
55 I.Fun.Exceptions = 0;
56
Chris Lattnerdefaf412009-01-20 19:11:22 +000057 // new[] an argument array if needed.
58 if (NumArgs) {
59 // If the 'InlineParams' in Declarator is unused and big enough, put our
60 // parameter list there (in an effort to avoid new/delete traffic). If it
61 // is already used (consider a function returning a function pointer) or too
62 // small (function taking too many arguments), go to the heap.
63 if (!TheDeclarator.InlineParamsUsed &&
64 NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
65 I.Fun.ArgInfo = TheDeclarator.InlineParams;
66 I.Fun.DeleteArgInfo = false;
67 TheDeclarator.InlineParamsUsed = true;
68 } else {
69 I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs];
70 I.Fun.DeleteArgInfo = true;
71 }
72 memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs);
73 }
Sebastian Redl35f3a5b2009-04-29 17:30:04 +000074 // new[] an exception array if needed
75 if (NumExceptions) {
Sebastian Redlaaacda92009-05-29 18:02:33 +000076 I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions];
77 for (unsigned i = 0; i != NumExceptions; ++i) {
78 I.Fun.Exceptions[i].Ty = Exceptions[i];
79 I.Fun.Exceptions[i].Range = ExceptionRanges[i];
80 }
Sebastian Redl35f3a5b2009-04-29 17:30:04 +000081 }
Chris Lattnerdefaf412009-01-20 19:11:22 +000082 return I;
83}
Chris Lattner5a63b092008-11-22 08:32:36 +000084
Chris Lattner4b009652007-07-25 00:24:17 +000085/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
Chris Lattner92eca3e2009-02-27 18:35:46 +000086/// declaration specifier includes.
Chris Lattner4b009652007-07-25 00:24:17 +000087///
88unsigned DeclSpec::getParsedSpecifiers() const {
89 unsigned Res = 0;
90 if (StorageClassSpec != SCS_unspecified ||
91 SCS_thread_specified)
92 Res |= PQ_StorageClassSpecifier;
Mike Stumpea5a8b32008-06-19 19:52:46 +000093
Chris Lattner4b009652007-07-25 00:24:17 +000094 if (TypeQualifiers != TQ_unspecified)
95 Res |= PQ_TypeQualifier;
96
97 if (hasTypeSpecifier())
98 Res |= PQ_TypeSpecifier;
99
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000100 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
Chris Lattner4b009652007-07-25 00:24:17 +0000101 Res |= PQ_FunctionSpecifier;
102 return Res;
103}
104
105const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
106 switch (S) {
107 default: assert(0 && "Unknown typespec!");
108 case DeclSpec::SCS_unspecified: return "unspecified";
109 case DeclSpec::SCS_typedef: return "typedef";
110 case DeclSpec::SCS_extern: return "extern";
111 case DeclSpec::SCS_static: return "static";
112 case DeclSpec::SCS_auto: return "auto";
113 case DeclSpec::SCS_register: return "register";
Eli Friedman1a3e74c2009-04-19 20:27:55 +0000114 case DeclSpec::SCS_private_extern: return "__private_extern__";
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000115 case DeclSpec::SCS_mutable: return "mutable";
Chris Lattner4b009652007-07-25 00:24:17 +0000116 }
117}
118
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000119bool DeclSpec::BadSpecifier(SCS S, const char *&PrevSpec) {
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000120 PrevSpec = getSpecifierName(S);
Chris Lattner4b009652007-07-25 00:24:17 +0000121 return true;
122}
123
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000124bool DeclSpec::BadSpecifier(TSW W, const char *&PrevSpec) {
Chris Lattner4b009652007-07-25 00:24:17 +0000125 switch (W) {
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000126 case TSW_unspecified: PrevSpec = "unspecified"; break;
127 case TSW_short: PrevSpec = "short"; break;
128 case TSW_long: PrevSpec = "long"; break;
129 case TSW_longlong: PrevSpec = "long long"; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000130 }
131 return true;
132}
133
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000134bool DeclSpec::BadSpecifier(TSC C, const char *&PrevSpec) {
Chris Lattner4b009652007-07-25 00:24:17 +0000135 switch (C) {
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000136 case TSC_unspecified: PrevSpec = "unspecified"; break;
137 case TSC_imaginary: PrevSpec = "imaginary"; break;
138 case TSC_complex: PrevSpec = "complex"; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000139 }
140 return true;
141}
142
143
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000144bool DeclSpec::BadSpecifier(TSS S, const char *&PrevSpec) {
Chris Lattner4b009652007-07-25 00:24:17 +0000145 switch (S) {
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000146 case TSS_unspecified: PrevSpec = "unspecified"; break;
147 case TSS_signed: PrevSpec = "signed"; break;
148 case TSS_unsigned: PrevSpec = "unsigned"; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000149 }
150 return true;
151}
152
153const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
154 switch (T) {
155 default: assert(0 && "Unknown typespec!");
156 case DeclSpec::TST_unspecified: return "unspecified";
157 case DeclSpec::TST_void: return "void";
158 case DeclSpec::TST_char: return "char";
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000159 case DeclSpec::TST_wchar: return "wchar_t";
Chris Lattner4b009652007-07-25 00:24:17 +0000160 case DeclSpec::TST_int: return "int";
161 case DeclSpec::TST_float: return "float";
162 case DeclSpec::TST_double: return "double";
163 case DeclSpec::TST_bool: return "_Bool";
164 case DeclSpec::TST_decimal32: return "_Decimal32";
165 case DeclSpec::TST_decimal64: return "_Decimal64";
166 case DeclSpec::TST_decimal128: return "_Decimal128";
167 case DeclSpec::TST_enum: return "enum";
Chris Lattner2e78db32008-04-13 18:59:07 +0000168 case DeclSpec::TST_class: return "class";
Chris Lattner4b009652007-07-25 00:24:17 +0000169 case DeclSpec::TST_union: return "union";
170 case DeclSpec::TST_struct: return "struct";
Douglas Gregora60c62e2009-02-09 15:09:02 +0000171 case DeclSpec::TST_typename: return "type-name";
Steve Naroff7cbb1462007-07-31 12:34:36 +0000172 case DeclSpec::TST_typeofType:
173 case DeclSpec::TST_typeofExpr: return "typeof";
Chris Lattner4b009652007-07-25 00:24:17 +0000174 }
175}
176
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000177bool DeclSpec::BadSpecifier(TST T, const char *&PrevSpec) {
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000178 PrevSpec = getSpecifierName(T);
Chris Lattner4b009652007-07-25 00:24:17 +0000179 return true;
180}
181
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000182bool DeclSpec::BadSpecifier(TQ T, const char *&PrevSpec) {
Chris Lattner4b009652007-07-25 00:24:17 +0000183 switch (T) {
184 case DeclSpec::TQ_unspecified: PrevSpec = "unspecified"; break;
185 case DeclSpec::TQ_const: PrevSpec = "const"; break;
186 case DeclSpec::TQ_restrict: PrevSpec = "restrict"; break;
187 case DeclSpec::TQ_volatile: PrevSpec = "volatile"; break;
188 }
189 return true;
190}
191
192bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
193 const char *&PrevSpec) {
194 if (StorageClassSpec != SCS_unspecified)
Chris Lattner5a63b092008-11-22 08:32:36 +0000195 return BadSpecifier((SCS)StorageClassSpec, PrevSpec);
Chris Lattner4b009652007-07-25 00:24:17 +0000196 StorageClassSpec = S;
197 StorageClassSpecLoc = Loc;
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000198 assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield");
Chris Lattner4b009652007-07-25 00:24:17 +0000199 return false;
200}
201
202bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
203 const char *&PrevSpec) {
204 if (SCS_thread_specified) {
205 PrevSpec = "__thread";
206 return true;
207 }
208 SCS_thread_specified = true;
209 SCS_threadLoc = Loc;
210 return false;
211}
212
213
214/// These methods set the specified attribute of the DeclSpec, but return true
215/// and ignore the request if invalid (e.g. "extern" then "auto" is
216/// specified).
217bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
218 const char *&PrevSpec) {
219 if (TypeSpecWidth != TSW_unspecified &&
220 // Allow turning long -> long long.
221 (W != TSW_longlong || TypeSpecWidth != TSW_long))
Chris Lattner5a63b092008-11-22 08:32:36 +0000222 return BadSpecifier((TSW)TypeSpecWidth, PrevSpec);
Chris Lattner4b009652007-07-25 00:24:17 +0000223 TypeSpecWidth = W;
224 TSWLoc = Loc;
225 return false;
226}
227
228bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
229 const char *&PrevSpec) {
230 if (TypeSpecComplex != TSC_unspecified)
Chris Lattner5a63b092008-11-22 08:32:36 +0000231 return BadSpecifier((TSC)TypeSpecComplex, PrevSpec);
Chris Lattner4b009652007-07-25 00:24:17 +0000232 TypeSpecComplex = C;
233 TSCLoc = Loc;
234 return false;
235}
236
237bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
238 const char *&PrevSpec) {
239 if (TypeSpecSign != TSS_unspecified)
Chris Lattner5a63b092008-11-22 08:32:36 +0000240 return BadSpecifier((TSS)TypeSpecSign, PrevSpec);
Chris Lattner4b009652007-07-25 00:24:17 +0000241 TypeSpecSign = S;
242 TSSLoc = Loc;
243 return false;
244}
245
246bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
Douglas Gregor71f06032009-05-28 23:31:59 +0000247 const char *&PrevSpec, void *Rep,
248 bool Owned) {
Chris Lattner4b009652007-07-25 00:24:17 +0000249 if (TypeSpecType != TST_unspecified)
Chris Lattner5a63b092008-11-22 08:32:36 +0000250 return BadSpecifier((TST)TypeSpecType, PrevSpec);
Chris Lattner4b009652007-07-25 00:24:17 +0000251 TypeSpecType = T;
252 TypeRep = Rep;
253 TSTLoc = Loc;
Douglas Gregor71f06032009-05-28 23:31:59 +0000254 TypeSpecOwned = Owned;
Chris Lattner4b009652007-07-25 00:24:17 +0000255 return false;
256}
257
Douglas Gregord406b032009-02-06 22:42:48 +0000258bool DeclSpec::SetTypeSpecError() {
259 TypeSpecType = TST_error;
260 TypeRep = 0;
261 TSTLoc = SourceLocation();
262 return false;
263}
264
Chris Lattner4b009652007-07-25 00:24:17 +0000265bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
266 const LangOptions &Lang) {
267 // Duplicates turn into warnings pre-C99.
268 if ((TypeQualifiers & T) && !Lang.C99)
269 return BadSpecifier(T, PrevSpec);
270 TypeQualifiers |= T;
271
272 switch (T) {
273 default: assert(0 && "Unknown type qualifier!");
274 case TQ_const: TQ_constLoc = Loc; break;
275 case TQ_restrict: TQ_restrictLoc = Loc; break;
276 case TQ_volatile: TQ_volatileLoc = Loc; break;
277 }
278 return false;
279}
280
281bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec){
282 // 'inline inline' is ok.
283 FS_inline_specified = true;
284 FS_inlineLoc = Loc;
285 return false;
286}
287
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000288bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec){
289 // 'virtual virtual' is ok.
290 FS_virtual_specified = true;
291 FS_virtualLoc = Loc;
292 return false;
293}
294
295bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec){
296 // 'explicit explicit' is ok.
297 FS_explicit_specified = true;
298 FS_explicitLoc = Loc;
299 return false;
300}
301
Anders Carlsson6c2ad5a2009-05-06 04:46:28 +0000302bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec) {
303 if (Friend_specified) {
304 PrevSpec = "friend";
305 return true;
306 }
307
308 Friend_specified = true;
309 FriendLoc = Loc;
310 return false;
311}
Chris Lattner4b009652007-07-25 00:24:17 +0000312
313/// Finish - This does final analysis of the declspec, rejecting things like
314/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
315/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
316/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Douglas Gregor1ba5cb32009-04-01 22:41:11 +0000317void DeclSpec::Finish(Diagnostic &D, Preprocessor &PP) {
Chris Lattner4b009652007-07-25 00:24:17 +0000318 // Check the type specifier components first.
Douglas Gregor1ba5cb32009-04-01 22:41:11 +0000319 SourceManager &SrcMgr = PP.getSourceManager();
Chris Lattner4b009652007-07-25 00:24:17 +0000320
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000321 // signed/unsigned are only valid with int/char/wchar_t.
Chris Lattner4b009652007-07-25 00:24:17 +0000322 if (TypeSpecSign != TSS_unspecified) {
323 if (TypeSpecType == TST_unspecified)
324 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000325 else if (TypeSpecType != TST_int &&
326 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
Chris Lattner5a63b092008-11-22 08:32:36 +0000327 Diag(D, TSSLoc, SrcMgr, diag::err_invalid_sign_spec)
328 << getSpecifierName((TST)TypeSpecType);
Chris Lattner4b009652007-07-25 00:24:17 +0000329 // signed double -> double.
330 TypeSpecSign = TSS_unspecified;
331 }
332 }
333
334 // Validate the width of the type.
335 switch (TypeSpecWidth) {
336 case TSW_unspecified: break;
337 case TSW_short: // short int
338 case TSW_longlong: // long long int
339 if (TypeSpecType == TST_unspecified)
340 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
341 else if (TypeSpecType != TST_int) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000342 Diag(D, TSWLoc, SrcMgr,
Chris Lattner4b009652007-07-25 00:24:17 +0000343 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
Chris Lattner5a63b092008-11-22 08:32:36 +0000344 : diag::err_invalid_longlong_spec)
345 << getSpecifierName((TST)TypeSpecType);
Chris Lattner4b009652007-07-25 00:24:17 +0000346 TypeSpecType = TST_int;
347 }
348 break;
349 case TSW_long: // long double, long int
350 if (TypeSpecType == TST_unspecified)
351 TypeSpecType = TST_int; // long -> long int.
352 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Chris Lattner5a63b092008-11-22 08:32:36 +0000353 Diag(D, TSWLoc, SrcMgr, diag::err_invalid_long_spec)
354 << getSpecifierName((TST)TypeSpecType);
Chris Lattner4b009652007-07-25 00:24:17 +0000355 TypeSpecType = TST_int;
356 }
357 break;
358 }
359
360 // TODO: if the implementation does not implement _Complex or _Imaginary,
361 // disallow their use. Need information about the backend.
362 if (TypeSpecComplex != TSC_unspecified) {
363 if (TypeSpecType == TST_unspecified) {
Douglas Gregor1ba5cb32009-04-01 22:41:11 +0000364 Diag(D, TSCLoc, SrcMgr, diag::ext_plain_complex)
365 << CodeModificationHint::CreateInsertion(
366 PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
367 " double");
Chris Lattner4b009652007-07-25 00:24:17 +0000368 TypeSpecType = TST_double; // _Complex -> _Complex double.
369 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
370 // Note that this intentionally doesn't include _Complex _Bool.
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000371 Diag(D, TSTLoc, SrcMgr, diag::ext_integer_complex);
Chris Lattner4b009652007-07-25 00:24:17 +0000372 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Chris Lattner5a63b092008-11-22 08:32:36 +0000373 Diag(D, TSCLoc, SrcMgr, diag::err_invalid_complex_spec)
374 << getSpecifierName((TST)TypeSpecType);
Chris Lattner4b009652007-07-25 00:24:17 +0000375 TypeSpecComplex = TSC_unspecified;
376 }
377 }
Chris Lattner4b009652007-07-25 00:24:17 +0000378
379 // Okay, now we can infer the real type.
380
381 // TODO: return "auto function" and other bad things based on the real type.
382
383 // 'data definition has no type or storage class'?
384}
Daniel Dunbarcc7b1602008-08-11 03:45:03 +0000385
Sebastian Redlb7605e82008-12-28 15:28:59 +0000386bool DeclSpec::isMissingDeclaratorOk() {
387 TST tst = getTypeSpecType();
388 return (tst == TST_union
389 || tst == TST_struct
390 || tst == TST_class
391 || tst == TST_enum
Douglas Gregorb748fc52009-01-12 22:49:06 +0000392 ) && getTypeRep() != 0 && StorageClassSpec != DeclSpec::SCS_typedef;
Sebastian Redlb7605e82008-12-28 15:28:59 +0000393}