blob: bcf14d916cb66b4346fef0be50a9b47f7d93df40 [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,
35 SourceLocation Loc,
36 Declarator &TheDeclarator) {
37 DeclaratorChunk I;
38 I.Kind = Function;
39 I.Loc = Loc;
40 I.Fun.hasPrototype = hasProto;
41 I.Fun.isVariadic = isVariadic;
Douglas Gregor88a25f82009-02-18 07:07:28 +000042 I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding();
Chris Lattnerdefaf412009-01-20 19:11:22 +000043 I.Fun.DeleteArgInfo = false;
44 I.Fun.TypeQuals = TypeQuals;
45 I.Fun.NumArgs = NumArgs;
46 I.Fun.ArgInfo = 0;
47
48 // new[] an argument array if needed.
49 if (NumArgs) {
50 // If the 'InlineParams' in Declarator is unused and big enough, put our
51 // parameter list there (in an effort to avoid new/delete traffic). If it
52 // is already used (consider a function returning a function pointer) or too
53 // small (function taking too many arguments), go to the heap.
54 if (!TheDeclarator.InlineParamsUsed &&
55 NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
56 I.Fun.ArgInfo = TheDeclarator.InlineParams;
57 I.Fun.DeleteArgInfo = false;
58 TheDeclarator.InlineParamsUsed = true;
59 } else {
60 I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs];
61 I.Fun.DeleteArgInfo = true;
62 }
63 memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs);
64 }
65 return I;
66}
Chris Lattner5a63b092008-11-22 08:32:36 +000067
Chris Lattner4b009652007-07-25 00:24:17 +000068/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
Chris Lattner92eca3e2009-02-27 18:35:46 +000069/// declaration specifier includes.
Chris Lattner4b009652007-07-25 00:24:17 +000070///
71unsigned DeclSpec::getParsedSpecifiers() const {
72 unsigned Res = 0;
73 if (StorageClassSpec != SCS_unspecified ||
74 SCS_thread_specified)
75 Res |= PQ_StorageClassSpecifier;
Mike Stumpea5a8b32008-06-19 19:52:46 +000076
Chris Lattner4b009652007-07-25 00:24:17 +000077 if (TypeQualifiers != TQ_unspecified)
78 Res |= PQ_TypeQualifier;
79
80 if (hasTypeSpecifier())
81 Res |= PQ_TypeSpecifier;
82
Douglas Gregorf15ac4b2008-10-31 09:07:45 +000083 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
Chris Lattner4b009652007-07-25 00:24:17 +000084 Res |= PQ_FunctionSpecifier;
85 return Res;
86}
87
88const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
89 switch (S) {
90 default: assert(0 && "Unknown typespec!");
91 case DeclSpec::SCS_unspecified: return "unspecified";
92 case DeclSpec::SCS_typedef: return "typedef";
93 case DeclSpec::SCS_extern: return "extern";
94 case DeclSpec::SCS_static: return "static";
95 case DeclSpec::SCS_auto: return "auto";
96 case DeclSpec::SCS_register: return "register";
Eli Friedman1a3e74c2009-04-19 20:27:55 +000097 case DeclSpec::SCS_private_extern: return "__private_extern__";
Sebastian Redl9f5337b2008-11-14 23:42:31 +000098 case DeclSpec::SCS_mutable: return "mutable";
Chris Lattner4b009652007-07-25 00:24:17 +000099 }
100}
101
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000102bool DeclSpec::BadSpecifier(SCS S, const char *&PrevSpec) {
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000103 PrevSpec = getSpecifierName(S);
Chris Lattner4b009652007-07-25 00:24:17 +0000104 return true;
105}
106
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000107bool DeclSpec::BadSpecifier(TSW W, const char *&PrevSpec) {
Chris Lattner4b009652007-07-25 00:24:17 +0000108 switch (W) {
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000109 case TSW_unspecified: PrevSpec = "unspecified"; break;
110 case TSW_short: PrevSpec = "short"; break;
111 case TSW_long: PrevSpec = "long"; break;
112 case TSW_longlong: PrevSpec = "long long"; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000113 }
114 return true;
115}
116
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000117bool DeclSpec::BadSpecifier(TSC C, const char *&PrevSpec) {
Chris Lattner4b009652007-07-25 00:24:17 +0000118 switch (C) {
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000119 case TSC_unspecified: PrevSpec = "unspecified"; break;
120 case TSC_imaginary: PrevSpec = "imaginary"; break;
121 case TSC_complex: PrevSpec = "complex"; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000122 }
123 return true;
124}
125
126
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000127bool DeclSpec::BadSpecifier(TSS S, const char *&PrevSpec) {
Chris Lattner4b009652007-07-25 00:24:17 +0000128 switch (S) {
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000129 case TSS_unspecified: PrevSpec = "unspecified"; break;
130 case TSS_signed: PrevSpec = "signed"; break;
131 case TSS_unsigned: PrevSpec = "unsigned"; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000132 }
133 return true;
134}
135
136const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
137 switch (T) {
138 default: assert(0 && "Unknown typespec!");
139 case DeclSpec::TST_unspecified: return "unspecified";
140 case DeclSpec::TST_void: return "void";
141 case DeclSpec::TST_char: return "char";
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000142 case DeclSpec::TST_wchar: return "wchar_t";
Chris Lattner4b009652007-07-25 00:24:17 +0000143 case DeclSpec::TST_int: return "int";
144 case DeclSpec::TST_float: return "float";
145 case DeclSpec::TST_double: return "double";
146 case DeclSpec::TST_bool: return "_Bool";
147 case DeclSpec::TST_decimal32: return "_Decimal32";
148 case DeclSpec::TST_decimal64: return "_Decimal64";
149 case DeclSpec::TST_decimal128: return "_Decimal128";
150 case DeclSpec::TST_enum: return "enum";
Chris Lattner2e78db32008-04-13 18:59:07 +0000151 case DeclSpec::TST_class: return "class";
Chris Lattner4b009652007-07-25 00:24:17 +0000152 case DeclSpec::TST_union: return "union";
153 case DeclSpec::TST_struct: return "struct";
Douglas Gregora60c62e2009-02-09 15:09:02 +0000154 case DeclSpec::TST_typename: return "type-name";
Steve Naroff7cbb1462007-07-31 12:34:36 +0000155 case DeclSpec::TST_typeofType:
156 case DeclSpec::TST_typeofExpr: return "typeof";
Chris Lattner4b009652007-07-25 00:24:17 +0000157 }
158}
159
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000160bool DeclSpec::BadSpecifier(TST T, const char *&PrevSpec) {
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000161 PrevSpec = getSpecifierName(T);
Chris Lattner4b009652007-07-25 00:24:17 +0000162 return true;
163}
164
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000165bool DeclSpec::BadSpecifier(TQ T, const char *&PrevSpec) {
Chris Lattner4b009652007-07-25 00:24:17 +0000166 switch (T) {
167 case DeclSpec::TQ_unspecified: PrevSpec = "unspecified"; break;
168 case DeclSpec::TQ_const: PrevSpec = "const"; break;
169 case DeclSpec::TQ_restrict: PrevSpec = "restrict"; break;
170 case DeclSpec::TQ_volatile: PrevSpec = "volatile"; break;
171 }
172 return true;
173}
174
175bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
176 const char *&PrevSpec) {
177 if (StorageClassSpec != SCS_unspecified)
Chris Lattner5a63b092008-11-22 08:32:36 +0000178 return BadSpecifier((SCS)StorageClassSpec, PrevSpec);
Chris Lattner4b009652007-07-25 00:24:17 +0000179 StorageClassSpec = S;
180 StorageClassSpecLoc = Loc;
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000181 assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield");
Chris Lattner4b009652007-07-25 00:24:17 +0000182 return false;
183}
184
185bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
186 const char *&PrevSpec) {
187 if (SCS_thread_specified) {
188 PrevSpec = "__thread";
189 return true;
190 }
191 SCS_thread_specified = true;
192 SCS_threadLoc = Loc;
193 return false;
194}
195
196
197/// These methods set the specified attribute of the DeclSpec, but return true
198/// and ignore the request if invalid (e.g. "extern" then "auto" is
199/// specified).
200bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
201 const char *&PrevSpec) {
202 if (TypeSpecWidth != TSW_unspecified &&
203 // Allow turning long -> long long.
204 (W != TSW_longlong || TypeSpecWidth != TSW_long))
Chris Lattner5a63b092008-11-22 08:32:36 +0000205 return BadSpecifier((TSW)TypeSpecWidth, PrevSpec);
Chris Lattner4b009652007-07-25 00:24:17 +0000206 TypeSpecWidth = W;
207 TSWLoc = Loc;
208 return false;
209}
210
211bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
212 const char *&PrevSpec) {
213 if (TypeSpecComplex != TSC_unspecified)
Chris Lattner5a63b092008-11-22 08:32:36 +0000214 return BadSpecifier((TSC)TypeSpecComplex, PrevSpec);
Chris Lattner4b009652007-07-25 00:24:17 +0000215 TypeSpecComplex = C;
216 TSCLoc = Loc;
217 return false;
218}
219
220bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
221 const char *&PrevSpec) {
222 if (TypeSpecSign != TSS_unspecified)
Chris Lattner5a63b092008-11-22 08:32:36 +0000223 return BadSpecifier((TSS)TypeSpecSign, PrevSpec);
Chris Lattner4b009652007-07-25 00:24:17 +0000224 TypeSpecSign = S;
225 TSSLoc = Loc;
226 return false;
227}
228
229bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000230 const char *&PrevSpec, void *Rep) {
Chris Lattner4b009652007-07-25 00:24:17 +0000231 if (TypeSpecType != TST_unspecified)
Chris Lattner5a63b092008-11-22 08:32:36 +0000232 return BadSpecifier((TST)TypeSpecType, PrevSpec);
Chris Lattner4b009652007-07-25 00:24:17 +0000233 TypeSpecType = T;
234 TypeRep = Rep;
235 TSTLoc = Loc;
236 return false;
237}
238
Douglas Gregord406b032009-02-06 22:42:48 +0000239bool DeclSpec::SetTypeSpecError() {
240 TypeSpecType = TST_error;
241 TypeRep = 0;
242 TSTLoc = SourceLocation();
243 return false;
244}
245
Chris Lattner4b009652007-07-25 00:24:17 +0000246bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
247 const LangOptions &Lang) {
248 // Duplicates turn into warnings pre-C99.
249 if ((TypeQualifiers & T) && !Lang.C99)
250 return BadSpecifier(T, PrevSpec);
251 TypeQualifiers |= T;
252
253 switch (T) {
254 default: assert(0 && "Unknown type qualifier!");
255 case TQ_const: TQ_constLoc = Loc; break;
256 case TQ_restrict: TQ_restrictLoc = Loc; break;
257 case TQ_volatile: TQ_volatileLoc = Loc; break;
258 }
259 return false;
260}
261
262bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec){
263 // 'inline inline' is ok.
264 FS_inline_specified = true;
265 FS_inlineLoc = Loc;
266 return false;
267}
268
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000269bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec){
270 // 'virtual virtual' is ok.
271 FS_virtual_specified = true;
272 FS_virtualLoc = Loc;
273 return false;
274}
275
276bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec){
277 // 'explicit explicit' is ok.
278 FS_explicit_specified = true;
279 FS_explicitLoc = Loc;
280 return false;
281}
282
Chris Lattner4b009652007-07-25 00:24:17 +0000283
284/// Finish - This does final analysis of the declspec, rejecting things like
285/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
286/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
287/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Douglas Gregor1ba5cb32009-04-01 22:41:11 +0000288void DeclSpec::Finish(Diagnostic &D, Preprocessor &PP) {
Chris Lattner4b009652007-07-25 00:24:17 +0000289 // Check the type specifier components first.
Douglas Gregor1ba5cb32009-04-01 22:41:11 +0000290 SourceManager &SrcMgr = PP.getSourceManager();
Chris Lattner4b009652007-07-25 00:24:17 +0000291
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000292 // signed/unsigned are only valid with int/char/wchar_t.
Chris Lattner4b009652007-07-25 00:24:17 +0000293 if (TypeSpecSign != TSS_unspecified) {
294 if (TypeSpecType == TST_unspecified)
295 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000296 else if (TypeSpecType != TST_int &&
297 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
Chris Lattner5a63b092008-11-22 08:32:36 +0000298 Diag(D, TSSLoc, SrcMgr, diag::err_invalid_sign_spec)
299 << getSpecifierName((TST)TypeSpecType);
Chris Lattner4b009652007-07-25 00:24:17 +0000300 // signed double -> double.
301 TypeSpecSign = TSS_unspecified;
302 }
303 }
304
305 // Validate the width of the type.
306 switch (TypeSpecWidth) {
307 case TSW_unspecified: break;
308 case TSW_short: // short int
309 case TSW_longlong: // long long int
310 if (TypeSpecType == TST_unspecified)
311 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
312 else if (TypeSpecType != TST_int) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000313 Diag(D, TSWLoc, SrcMgr,
Chris Lattner4b009652007-07-25 00:24:17 +0000314 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
Chris Lattner5a63b092008-11-22 08:32:36 +0000315 : diag::err_invalid_longlong_spec)
316 << getSpecifierName((TST)TypeSpecType);
Chris Lattner4b009652007-07-25 00:24:17 +0000317 TypeSpecType = TST_int;
318 }
319 break;
320 case TSW_long: // long double, long int
321 if (TypeSpecType == TST_unspecified)
322 TypeSpecType = TST_int; // long -> long int.
323 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Chris Lattner5a63b092008-11-22 08:32:36 +0000324 Diag(D, TSWLoc, SrcMgr, diag::err_invalid_long_spec)
325 << getSpecifierName((TST)TypeSpecType);
Chris Lattner4b009652007-07-25 00:24:17 +0000326 TypeSpecType = TST_int;
327 }
328 break;
329 }
330
331 // TODO: if the implementation does not implement _Complex or _Imaginary,
332 // disallow their use. Need information about the backend.
333 if (TypeSpecComplex != TSC_unspecified) {
334 if (TypeSpecType == TST_unspecified) {
Douglas Gregor1ba5cb32009-04-01 22:41:11 +0000335 Diag(D, TSCLoc, SrcMgr, diag::ext_plain_complex)
336 << CodeModificationHint::CreateInsertion(
337 PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
338 " double");
Chris Lattner4b009652007-07-25 00:24:17 +0000339 TypeSpecType = TST_double; // _Complex -> _Complex double.
340 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
341 // Note that this intentionally doesn't include _Complex _Bool.
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000342 Diag(D, TSTLoc, SrcMgr, diag::ext_integer_complex);
Chris Lattner4b009652007-07-25 00:24:17 +0000343 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Chris Lattner5a63b092008-11-22 08:32:36 +0000344 Diag(D, TSCLoc, SrcMgr, diag::err_invalid_complex_spec)
345 << getSpecifierName((TST)TypeSpecType);
Chris Lattner4b009652007-07-25 00:24:17 +0000346 TypeSpecComplex = TSC_unspecified;
347 }
348 }
Chris Lattner4b009652007-07-25 00:24:17 +0000349
350 // Okay, now we can infer the real type.
351
352 // TODO: return "auto function" and other bad things based on the real type.
353
354 // 'data definition has no type or storage class'?
355}
Daniel Dunbarcc7b1602008-08-11 03:45:03 +0000356
Sebastian Redlb7605e82008-12-28 15:28:59 +0000357bool DeclSpec::isMissingDeclaratorOk() {
358 TST tst = getTypeSpecType();
359 return (tst == TST_union
360 || tst == TST_struct
361 || tst == TST_class
362 || tst == TST_enum
Douglas Gregorb748fc52009-01-12 22:49:06 +0000363 ) && getTypeRep() != 0 && StorageClassSpec != DeclSpec::SCS_typedef;
Sebastian Redlb7605e82008-12-28 15:28:59 +0000364}