blob: 72ac3112f9dcf8bb0175734f1f9d050579c90b5d [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"
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"
Douglas Gregore4e5b052009-03-19 00:18:19 +000018#include <cstring>
Reid Spencer5f016e22007-07-11 17:01:13 +000019using namespace clang;
20
Chris Lattner254be6a2008-11-22 08:32:36 +000021
22static DiagnosticBuilder Diag(Diagnostic &D, SourceLocation Loc,
23 SourceManager &SrcMgr, unsigned DiagID) {
24 return D.Report(FullSourceLoc(Loc, SrcMgr), DiagID);
25}
26
Douglas Gregore4e5b052009-03-19 00:18:19 +000027/// \brief Double the capacity of this scope specifier.
28void CXXScopeSpec::reallocate() {
29 Action::CXXScopeTy **Data = new Action::CXXScopeTy *[Capacity * 2];
30
31 Action::CXXScopeTy **From
32 = Capacity == 4? &InlineScopeReps[0] : ManyScopeReps;
33 std::memcpy(Data, From, Capacity * sizeof(Action::CXXScopeTy *));
34
35 if (Capacity > 4)
36 delete [] ManyScopeReps;
37 ManyScopeReps = Data;
38 Capacity *= 2;
39}
40
41CXXScopeSpec::CXXScopeSpec(const CXXScopeSpec &SS)
42 : Range(SS.Range), NumScopeReps(SS.NumScopeReps), Capacity(SS.Capacity) {
43
44 if (Capacity > 4) {
45 ManyScopeReps = new Action::CXXScopeTy *[Capacity];
46 memcpy(ManyScopeReps, SS.ManyScopeReps,
47 Capacity * sizeof(Action::CXXScopeTy *));
48 } else {
49 memcpy(InlineScopeReps, SS.InlineScopeReps,
50 Capacity * sizeof(Action::CXXScopeTy *));
51 }
52}
53
54CXXScopeSpec &CXXScopeSpec::operator=(const CXXScopeSpec &SS) {
55 // FIXME: Does not provide the strong exception safety guarantee.
56 this->~CXXScopeSpec();
57 new (this) CXXScopeSpec(SS);
58 return *this;
59}
60
61void *CXXScopeSpec::buildAnnotationData() const {
62 uintptr_t *Data = (uintptr_t *)malloc(sizeof(uintptr_t) * (size() + 1));
63 Data[0] = size();
64 for (unsigned I = 0; I < size(); ++I)
65 Data[I + 1] = reinterpret_cast<uintptr_t>(getScopeRep(I));
66 return Data;
67}
68
69void CXXScopeSpec::setFromAnnotationData(void *DataIn) {
70 uintptr_t *Data = static_cast<uintptr_t *>(DataIn);
71 NumScopeReps = *Data;
72
73 // Allocate enough space for the annotation data.
74 if (NumScopeReps > Capacity) {
75 if (Capacity > 4)
76 delete [] ManyScopeReps;
77
78 Capacity = NumScopeReps;
79 ManyScopeReps = new Action::CXXScopeTy *[Capacity];
80 }
81
82 if (Capacity > 4)
83 std::memcpy(ManyScopeReps, Data + 1, sizeof(uintptr_t) * NumScopeReps);
84 else
85 std::memcpy(InlineScopeReps, Data + 1, sizeof(uintptr_t) * NumScopeReps);
86}
Chris Lattner254be6a2008-11-22 08:32:36 +000087
Chris Lattner5af2f352009-01-20 19:11:22 +000088/// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function.
89/// "TheDeclarator" is the declarator that this will be added to.
90DeclaratorChunk DeclaratorChunk::getFunction(bool hasProto, bool isVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +000091 SourceLocation EllipsisLoc,
Chris Lattner5af2f352009-01-20 19:11:22 +000092 ParamInfo *ArgInfo,
93 unsigned NumArgs,
94 unsigned TypeQuals,
95 SourceLocation Loc,
96 Declarator &TheDeclarator) {
97 DeclaratorChunk I;
98 I.Kind = Function;
99 I.Loc = Loc;
100 I.Fun.hasPrototype = hasProto;
101 I.Fun.isVariadic = isVariadic;
Douglas Gregor965acbb2009-02-18 07:07:28 +0000102 I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding();
Chris Lattner5af2f352009-01-20 19:11:22 +0000103 I.Fun.DeleteArgInfo = false;
104 I.Fun.TypeQuals = TypeQuals;
105 I.Fun.NumArgs = NumArgs;
106 I.Fun.ArgInfo = 0;
107
108 // new[] an argument array if needed.
109 if (NumArgs) {
110 // If the 'InlineParams' in Declarator is unused and big enough, put our
111 // parameter list there (in an effort to avoid new/delete traffic). If it
112 // is already used (consider a function returning a function pointer) or too
113 // small (function taking too many arguments), go to the heap.
114 if (!TheDeclarator.InlineParamsUsed &&
115 NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
116 I.Fun.ArgInfo = TheDeclarator.InlineParams;
117 I.Fun.DeleteArgInfo = false;
118 TheDeclarator.InlineParamsUsed = true;
119 } else {
120 I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs];
121 I.Fun.DeleteArgInfo = true;
122 }
123 memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs);
124 }
125 return I;
126}
Chris Lattner254be6a2008-11-22 08:32:36 +0000127
Reid Spencer5f016e22007-07-11 17:01:13 +0000128/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
Chris Lattner2a327d12009-02-27 18:35:46 +0000129/// declaration specifier includes.
Reid Spencer5f016e22007-07-11 17:01:13 +0000130///
131unsigned DeclSpec::getParsedSpecifiers() const {
132 unsigned Res = 0;
133 if (StorageClassSpec != SCS_unspecified ||
134 SCS_thread_specified)
135 Res |= PQ_StorageClassSpecifier;
Mike Stumpd4204332008-06-19 19:52:46 +0000136
Reid Spencer5f016e22007-07-11 17:01:13 +0000137 if (TypeQualifiers != TQ_unspecified)
138 Res |= PQ_TypeQualifier;
139
140 if (hasTypeSpecifier())
141 Res |= PQ_TypeSpecifier;
142
Douglas Gregorb48fe382008-10-31 09:07:45 +0000143 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
Reid Spencer5f016e22007-07-11 17:01:13 +0000144 Res |= PQ_FunctionSpecifier;
145 return Res;
146}
147
148const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
149 switch (S) {
150 default: assert(0 && "Unknown typespec!");
151 case DeclSpec::SCS_unspecified: return "unspecified";
152 case DeclSpec::SCS_typedef: return "typedef";
153 case DeclSpec::SCS_extern: return "extern";
154 case DeclSpec::SCS_static: return "static";
155 case DeclSpec::SCS_auto: return "auto";
156 case DeclSpec::SCS_register: return "register";
Sebastian Redl669d5d72008-11-14 23:42:31 +0000157 case DeclSpec::SCS_mutable: return "mutable";
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 }
159}
160
Steve Naroff95324142008-02-12 04:08:59 +0000161bool DeclSpec::BadSpecifier(SCS S, const char *&PrevSpec) {
Steve Naroff95324142008-02-12 04:08:59 +0000162 PrevSpec = getSpecifierName(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 return true;
164}
165
Steve Naroff95324142008-02-12 04:08:59 +0000166bool DeclSpec::BadSpecifier(TSW W, const char *&PrevSpec) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000167 switch (W) {
Steve Naroff95324142008-02-12 04:08:59 +0000168 case TSW_unspecified: PrevSpec = "unspecified"; break;
169 case TSW_short: PrevSpec = "short"; break;
170 case TSW_long: PrevSpec = "long"; break;
171 case TSW_longlong: PrevSpec = "long long"; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 }
173 return true;
174}
175
Steve Naroff95324142008-02-12 04:08:59 +0000176bool DeclSpec::BadSpecifier(TSC C, const char *&PrevSpec) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000177 switch (C) {
Steve Naroff95324142008-02-12 04:08:59 +0000178 case TSC_unspecified: PrevSpec = "unspecified"; break;
179 case TSC_imaginary: PrevSpec = "imaginary"; break;
180 case TSC_complex: PrevSpec = "complex"; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000181 }
182 return true;
183}
184
185
Steve Naroff95324142008-02-12 04:08:59 +0000186bool DeclSpec::BadSpecifier(TSS S, const char *&PrevSpec) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000187 switch (S) {
Steve Naroff95324142008-02-12 04:08:59 +0000188 case TSS_unspecified: PrevSpec = "unspecified"; break;
189 case TSS_signed: PrevSpec = "signed"; break;
190 case TSS_unsigned: PrevSpec = "unsigned"; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 }
192 return true;
193}
194
195const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
196 switch (T) {
197 default: assert(0 && "Unknown typespec!");
198 case DeclSpec::TST_unspecified: return "unspecified";
199 case DeclSpec::TST_void: return "void";
200 case DeclSpec::TST_char: return "char";
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000201 case DeclSpec::TST_wchar: return "wchar_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000202 case DeclSpec::TST_int: return "int";
203 case DeclSpec::TST_float: return "float";
204 case DeclSpec::TST_double: return "double";
205 case DeclSpec::TST_bool: return "_Bool";
206 case DeclSpec::TST_decimal32: return "_Decimal32";
207 case DeclSpec::TST_decimal64: return "_Decimal64";
208 case DeclSpec::TST_decimal128: return "_Decimal128";
209 case DeclSpec::TST_enum: return "enum";
Chris Lattner99dc9142008-04-13 18:59:07 +0000210 case DeclSpec::TST_class: return "class";
Reid Spencer5f016e22007-07-11 17:01:13 +0000211 case DeclSpec::TST_union: return "union";
212 case DeclSpec::TST_struct: return "struct";
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000213 case DeclSpec::TST_typename: return "type-name";
Steve Naroffd1861fd2007-07-31 12:34:36 +0000214 case DeclSpec::TST_typeofType:
215 case DeclSpec::TST_typeofExpr: return "typeof";
Reid Spencer5f016e22007-07-11 17:01:13 +0000216 }
217}
218
Steve Naroff95324142008-02-12 04:08:59 +0000219bool DeclSpec::BadSpecifier(TST T, const char *&PrevSpec) {
Steve Naroff95324142008-02-12 04:08:59 +0000220 PrevSpec = getSpecifierName(T);
Reid Spencer5f016e22007-07-11 17:01:13 +0000221 return true;
222}
223
Steve Naroff95324142008-02-12 04:08:59 +0000224bool DeclSpec::BadSpecifier(TQ T, const char *&PrevSpec) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000225 switch (T) {
226 case DeclSpec::TQ_unspecified: PrevSpec = "unspecified"; break;
227 case DeclSpec::TQ_const: PrevSpec = "const"; break;
228 case DeclSpec::TQ_restrict: PrevSpec = "restrict"; break;
229 case DeclSpec::TQ_volatile: PrevSpec = "volatile"; break;
230 }
231 return true;
232}
233
234bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
235 const char *&PrevSpec) {
236 if (StorageClassSpec != SCS_unspecified)
Chris Lattner254be6a2008-11-22 08:32:36 +0000237 return BadSpecifier((SCS)StorageClassSpec, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000238 StorageClassSpec = S;
239 StorageClassSpecLoc = Loc;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000240 assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield");
Reid Spencer5f016e22007-07-11 17:01:13 +0000241 return false;
242}
243
244bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
245 const char *&PrevSpec) {
246 if (SCS_thread_specified) {
247 PrevSpec = "__thread";
248 return true;
249 }
250 SCS_thread_specified = true;
251 SCS_threadLoc = Loc;
252 return false;
253}
254
255
256/// These methods set the specified attribute of the DeclSpec, but return true
257/// and ignore the request if invalid (e.g. "extern" then "auto" is
258/// specified).
259bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
260 const char *&PrevSpec) {
261 if (TypeSpecWidth != TSW_unspecified &&
262 // Allow turning long -> long long.
263 (W != TSW_longlong || TypeSpecWidth != TSW_long))
Chris Lattner254be6a2008-11-22 08:32:36 +0000264 return BadSpecifier((TSW)TypeSpecWidth, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000265 TypeSpecWidth = W;
266 TSWLoc = Loc;
267 return false;
268}
269
270bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
271 const char *&PrevSpec) {
272 if (TypeSpecComplex != TSC_unspecified)
Chris Lattner254be6a2008-11-22 08:32:36 +0000273 return BadSpecifier((TSC)TypeSpecComplex, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000274 TypeSpecComplex = C;
275 TSCLoc = Loc;
276 return false;
277}
278
279bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
280 const char *&PrevSpec) {
281 if (TypeSpecSign != TSS_unspecified)
Chris Lattner254be6a2008-11-22 08:32:36 +0000282 return BadSpecifier((TSS)TypeSpecSign, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000283 TypeSpecSign = S;
284 TSSLoc = Loc;
285 return false;
286}
287
288bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
Argyrios Kyrtzidis39caa082008-08-01 10:35:27 +0000289 const char *&PrevSpec, Action::TypeTy *Rep) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000290 if (TypeSpecType != TST_unspecified)
Chris Lattner254be6a2008-11-22 08:32:36 +0000291 return BadSpecifier((TST)TypeSpecType, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000292 TypeSpecType = T;
293 TypeRep = Rep;
294 TSTLoc = Loc;
295 return false;
296}
297
Douglas Gregorddc29e12009-02-06 22:42:48 +0000298bool DeclSpec::SetTypeSpecError() {
299 TypeSpecType = TST_error;
300 TypeRep = 0;
301 TSTLoc = SourceLocation();
302 return false;
303}
304
Reid Spencer5f016e22007-07-11 17:01:13 +0000305bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
306 const LangOptions &Lang) {
307 // Duplicates turn into warnings pre-C99.
308 if ((TypeQualifiers & T) && !Lang.C99)
309 return BadSpecifier(T, PrevSpec);
310 TypeQualifiers |= T;
311
312 switch (T) {
313 default: assert(0 && "Unknown type qualifier!");
314 case TQ_const: TQ_constLoc = Loc; break;
315 case TQ_restrict: TQ_restrictLoc = Loc; break;
316 case TQ_volatile: TQ_volatileLoc = Loc; break;
317 }
318 return false;
319}
320
321bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec){
322 // 'inline inline' is ok.
323 FS_inline_specified = true;
324 FS_inlineLoc = Loc;
325 return false;
326}
327
Douglas Gregorb48fe382008-10-31 09:07:45 +0000328bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec){
329 // 'virtual virtual' is ok.
330 FS_virtual_specified = true;
331 FS_virtualLoc = Loc;
332 return false;
333}
334
335bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec){
336 // 'explicit explicit' is ok.
337 FS_explicit_specified = true;
338 FS_explicitLoc = Loc;
339 return false;
340}
341
Reid Spencer5f016e22007-07-11 17:01:13 +0000342
343/// Finish - This does final analysis of the declspec, rejecting things like
344/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
345/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
346/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000347void DeclSpec::Finish(Diagnostic &D, SourceManager& SrcMgr,
348 const LangOptions &Lang) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000349 // Check the type specifier components first.
350
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000351 // signed/unsigned are only valid with int/char/wchar_t.
Reid Spencer5f016e22007-07-11 17:01:13 +0000352 if (TypeSpecSign != TSS_unspecified) {
353 if (TypeSpecType == TST_unspecified)
354 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000355 else if (TypeSpecType != TST_int &&
356 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000357 Diag(D, TSSLoc, SrcMgr, diag::err_invalid_sign_spec)
358 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000359 // signed double -> double.
360 TypeSpecSign = TSS_unspecified;
361 }
362 }
363
364 // Validate the width of the type.
365 switch (TypeSpecWidth) {
366 case TSW_unspecified: break;
367 case TSW_short: // short int
368 case TSW_longlong: // long long int
369 if (TypeSpecType == TST_unspecified)
370 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
371 else if (TypeSpecType != TST_int) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000372 Diag(D, TSWLoc, SrcMgr,
Reid Spencer5f016e22007-07-11 17:01:13 +0000373 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
Chris Lattner254be6a2008-11-22 08:32:36 +0000374 : diag::err_invalid_longlong_spec)
375 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000376 TypeSpecType = TST_int;
377 }
378 break;
379 case TSW_long: // long double, long int
380 if (TypeSpecType == TST_unspecified)
381 TypeSpecType = TST_int; // long -> long int.
382 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000383 Diag(D, TSWLoc, SrcMgr, diag::err_invalid_long_spec)
384 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000385 TypeSpecType = TST_int;
386 }
387 break;
388 }
389
390 // TODO: if the implementation does not implement _Complex or _Imaginary,
391 // disallow their use. Need information about the backend.
392 if (TypeSpecComplex != TSC_unspecified) {
393 if (TypeSpecType == TST_unspecified) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000394 Diag(D, TSCLoc, SrcMgr, diag::ext_plain_complex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000395 TypeSpecType = TST_double; // _Complex -> _Complex double.
396 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
397 // Note that this intentionally doesn't include _Complex _Bool.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000398 Diag(D, TSTLoc, SrcMgr, diag::ext_integer_complex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000399 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000400 Diag(D, TSCLoc, SrcMgr, diag::err_invalid_complex_spec)
401 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000402 TypeSpecComplex = TSC_unspecified;
403 }
404 }
405
406 // Verify __thread.
407 if (SCS_thread_specified) {
408 if (StorageClassSpec == SCS_unspecified) {
409 StorageClassSpec = SCS_extern; // '__thread int' -> 'extern __thread int'
410 } else if (StorageClassSpec != SCS_extern &&
411 StorageClassSpec != SCS_static) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000412 Diag(D, getStorageClassSpecLoc(), SrcMgr, diag::err_invalid_thread_spec)
413 << getSpecifierName((SCS)StorageClassSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000414 SCS_thread_specified = false;
415 }
416 }
417
418 // Okay, now we can infer the real type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000419
420 // TODO: return "auto function" and other bad things based on the real type.
421
422 // 'data definition has no type or storage class'?
423}
Daniel Dunbare4858a62008-08-11 03:45:03 +0000424
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000425bool DeclSpec::isMissingDeclaratorOk() {
426 TST tst = getTypeSpecType();
427 return (tst == TST_union
428 || tst == TST_struct
429 || tst == TST_class
430 || tst == TST_enum
Douglas Gregor4920f1f2009-01-12 22:49:06 +0000431 ) && getTypeRep() != 0 && StorageClassSpec != DeclSpec::SCS_typedef;
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000432}