blob: 8fbcc7ba43eed33013161d6107cfadc5bbf399d3 [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
John McCall19510852010-08-20 18:27:03 +000014#include "clang/Parse/ParseDiagnostic.h" // FIXME: remove this back-dependency!
15#include "clang/Sema/DeclSpec.h"
16#include "clang/Sema/ParsedTemplate.h"
Douglas Gregor9b3064b2009-04-01 22:41:11 +000017#include "clang/Lex/Preprocessor.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "clang/Basic/LangOptions.h"
Chris Lattner5af2f352009-01-20 19:11:22 +000019#include "llvm/ADT/STLExtras.h"
John McCall32d335e2009-08-03 18:47:27 +000020#include "llvm/Support/ErrorHandling.h"
Douglas Gregore4e5b052009-03-19 00:18:19 +000021#include <cstring>
Reid Spencer5f016e22007-07-11 17:01:13 +000022using namespace clang;
23
Chris Lattner254be6a2008-11-22 08:32:36 +000024
25static DiagnosticBuilder Diag(Diagnostic &D, SourceLocation Loc,
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000026 unsigned DiagID) {
27 return D.Report(Loc, DiagID);
Chris Lattner254be6a2008-11-22 08:32:36 +000028}
29
Douglas Gregor314b97f2009-11-10 19:49:08 +000030
31void UnqualifiedId::setTemplateId(TemplateIdAnnotation *TemplateId) {
32 assert(TemplateId && "NULL template-id annotation?");
33 Kind = IK_TemplateId;
34 this->TemplateId = TemplateId;
35 StartLocation = TemplateId->TemplateNameLoc;
36 EndLocation = TemplateId->RAngleLoc;
37}
38
Douglas Gregor0efc2c12010-01-13 17:31:36 +000039void UnqualifiedId::setConstructorTemplateId(TemplateIdAnnotation *TemplateId) {
40 assert(TemplateId && "NULL template-id annotation?");
41 Kind = IK_ConstructorTemplateId;
42 this->TemplateId = TemplateId;
43 StartLocation = TemplateId->TemplateNameLoc;
44 EndLocation = TemplateId->RAngleLoc;
45}
46
Chris Lattner5af2f352009-01-20 19:11:22 +000047/// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function.
48/// "TheDeclarator" is the declarator that this will be added to.
John McCall7f040a92010-12-24 02:08:15 +000049DeclaratorChunk DeclaratorChunk::getFunction(const ParsedAttributes &attrs,
50 bool hasProto, bool isVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +000051 SourceLocation EllipsisLoc,
Chris Lattner5af2f352009-01-20 19:11:22 +000052 ParamInfo *ArgInfo,
53 unsigned NumArgs,
54 unsigned TypeQuals,
Sebastian Redl7dc81342009-04-29 17:30:04 +000055 bool hasExceptionSpec,
Sebastian Redl3cc97262009-05-31 11:47:27 +000056 SourceLocation ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +000057 bool hasAnyExceptionSpec,
John McCallb3d87482010-08-24 05:47:05 +000058 ParsedType *Exceptions,
Sebastian Redlef65f062009-05-29 18:02:33 +000059 SourceRange *ExceptionRanges,
Sebastian Redl7dc81342009-04-29 17:30:04 +000060 unsigned NumExceptions,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +000061 SourceLocation LPLoc,
62 SourceLocation RPLoc,
Douglas Gregordab60ad2010-10-01 18:44:50 +000063 Declarator &TheDeclarator,
64 ParsedType TrailingReturnType) {
Chris Lattner5af2f352009-01-20 19:11:22 +000065 DeclaratorChunk I;
Sebastian Redl7dc81342009-04-29 17:30:04 +000066 I.Kind = Function;
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +000067 I.Loc = LPLoc;
68 I.EndLoc = RPLoc;
John McCall7f040a92010-12-24 02:08:15 +000069 I.Fun.AttrList = attrs.getList();
Sebastian Redl7dc81342009-04-29 17:30:04 +000070 I.Fun.hasPrototype = hasProto;
71 I.Fun.isVariadic = isVariadic;
72 I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding();
73 I.Fun.DeleteArgInfo = false;
74 I.Fun.TypeQuals = TypeQuals;
75 I.Fun.NumArgs = NumArgs;
76 I.Fun.ArgInfo = 0;
77 I.Fun.hasExceptionSpec = hasExceptionSpec;
Sebastian Redl3cc97262009-05-31 11:47:27 +000078 I.Fun.ThrowLoc = ThrowLoc.getRawEncoding();
Sebastian Redl7dc81342009-04-29 17:30:04 +000079 I.Fun.hasAnyExceptionSpec = hasAnyExceptionSpec;
80 I.Fun.NumExceptions = NumExceptions;
81 I.Fun.Exceptions = 0;
Douglas Gregordab60ad2010-10-01 18:44:50 +000082 I.Fun.TrailingReturnType = TrailingReturnType.getAsOpaquePtr();
Sebastian Redl7dc81342009-04-29 17:30:04 +000083
Chris Lattner5af2f352009-01-20 19:11:22 +000084 // new[] an argument array if needed.
85 if (NumArgs) {
86 // If the 'InlineParams' in Declarator is unused and big enough, put our
87 // parameter list there (in an effort to avoid new/delete traffic). If it
88 // is already used (consider a function returning a function pointer) or too
89 // small (function taking too many arguments), go to the heap.
Mike Stump1eb44332009-09-09 15:08:12 +000090 if (!TheDeclarator.InlineParamsUsed &&
Chris Lattner5af2f352009-01-20 19:11:22 +000091 NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
92 I.Fun.ArgInfo = TheDeclarator.InlineParams;
93 I.Fun.DeleteArgInfo = false;
94 TheDeclarator.InlineParamsUsed = true;
95 } else {
96 I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs];
97 I.Fun.DeleteArgInfo = true;
98 }
99 memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs);
100 }
Sebastian Redl7dc81342009-04-29 17:30:04 +0000101 // new[] an exception array if needed
102 if (NumExceptions) {
Sebastian Redlef65f062009-05-29 18:02:33 +0000103 I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions];
104 for (unsigned i = 0; i != NumExceptions; ++i) {
105 I.Fun.Exceptions[i].Ty = Exceptions[i];
106 I.Fun.Exceptions[i].Range = ExceptionRanges[i];
107 }
Sebastian Redl7dc81342009-04-29 17:30:04 +0000108 }
Chris Lattner5af2f352009-01-20 19:11:22 +0000109 return I;
110}
Chris Lattner254be6a2008-11-22 08:32:36 +0000111
Reid Spencer5f016e22007-07-11 17:01:13 +0000112/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
Chris Lattner2a327d12009-02-27 18:35:46 +0000113/// declaration specifier includes.
Reid Spencer5f016e22007-07-11 17:01:13 +0000114///
115unsigned DeclSpec::getParsedSpecifiers() const {
116 unsigned Res = 0;
117 if (StorageClassSpec != SCS_unspecified ||
118 SCS_thread_specified)
119 Res |= PQ_StorageClassSpecifier;
Mike Stumpd4204332008-06-19 19:52:46 +0000120
Reid Spencer5f016e22007-07-11 17:01:13 +0000121 if (TypeQualifiers != TQ_unspecified)
122 Res |= PQ_TypeQualifier;
Mike Stump1eb44332009-09-09 15:08:12 +0000123
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 if (hasTypeSpecifier())
125 Res |= PQ_TypeSpecifier;
Mike Stump1eb44332009-09-09 15:08:12 +0000126
Douglas Gregorb48fe382008-10-31 09:07:45 +0000127 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
Reid Spencer5f016e22007-07-11 17:01:13 +0000128 Res |= PQ_FunctionSpecifier;
129 return Res;
130}
131
John McCallfec54012009-08-03 20:12:06 +0000132template <class T> static bool BadSpecifier(T TNew, T TPrev,
133 const char *&PrevSpec,
134 unsigned &DiagID) {
John McCall32d335e2009-08-03 18:47:27 +0000135 PrevSpec = DeclSpec::getSpecifierName(TPrev);
John McCallfec54012009-08-03 20:12:06 +0000136 DiagID = (TNew == TPrev ? diag::ext_duplicate_declspec
137 : diag::err_invalid_decl_spec_combination);
John McCall32d335e2009-08-03 18:47:27 +0000138 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000139}
John McCall32d335e2009-08-03 18:47:27 +0000140
Reid Spencer5f016e22007-07-11 17:01:13 +0000141const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
142 switch (S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 case DeclSpec::SCS_unspecified: return "unspecified";
144 case DeclSpec::SCS_typedef: return "typedef";
145 case DeclSpec::SCS_extern: return "extern";
146 case DeclSpec::SCS_static: return "static";
147 case DeclSpec::SCS_auto: return "auto";
148 case DeclSpec::SCS_register: return "register";
Eli Friedman63054b32009-04-19 20:27:55 +0000149 case DeclSpec::SCS_private_extern: return "__private_extern__";
Sebastian Redl669d5d72008-11-14 23:42:31 +0000150 case DeclSpec::SCS_mutable: return "mutable";
Reid Spencer5f016e22007-07-11 17:01:13 +0000151 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000152 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000153}
154
John McCall32d335e2009-08-03 18:47:27 +0000155const char *DeclSpec::getSpecifierName(TSW W) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 switch (W) {
John McCall32d335e2009-08-03 18:47:27 +0000157 case TSW_unspecified: return "unspecified";
158 case TSW_short: return "short";
159 case TSW_long: return "long";
160 case TSW_longlong: return "long long";
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000162 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000163}
164
John McCall32d335e2009-08-03 18:47:27 +0000165const char *DeclSpec::getSpecifierName(TSC C) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 switch (C) {
John McCall32d335e2009-08-03 18:47:27 +0000167 case TSC_unspecified: return "unspecified";
168 case TSC_imaginary: return "imaginary";
169 case TSC_complex: return "complex";
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000171 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000172}
173
174
John McCall32d335e2009-08-03 18:47:27 +0000175const char *DeclSpec::getSpecifierName(TSS S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000176 switch (S) {
John McCall32d335e2009-08-03 18:47:27 +0000177 case TSS_unspecified: return "unspecified";
178 case TSS_signed: return "signed";
179 case TSS_unsigned: return "unsigned";
Reid Spencer5f016e22007-07-11 17:01:13 +0000180 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000181 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000182}
183
184const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
185 switch (T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 case DeclSpec::TST_unspecified: return "unspecified";
187 case DeclSpec::TST_void: return "void";
188 case DeclSpec::TST_char: return "char";
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000189 case DeclSpec::TST_wchar: return "wchar_t";
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000190 case DeclSpec::TST_char16: return "char16_t";
191 case DeclSpec::TST_char32: return "char32_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000192 case DeclSpec::TST_int: return "int";
193 case DeclSpec::TST_float: return "float";
194 case DeclSpec::TST_double: return "double";
195 case DeclSpec::TST_bool: return "_Bool";
196 case DeclSpec::TST_decimal32: return "_Decimal32";
197 case DeclSpec::TST_decimal64: return "_Decimal64";
198 case DeclSpec::TST_decimal128: return "_Decimal128";
199 case DeclSpec::TST_enum: return "enum";
Chris Lattner99dc9142008-04-13 18:59:07 +0000200 case DeclSpec::TST_class: return "class";
Reid Spencer5f016e22007-07-11 17:01:13 +0000201 case DeclSpec::TST_union: return "union";
202 case DeclSpec::TST_struct: return "struct";
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000203 case DeclSpec::TST_typename: return "type-name";
Steve Naroffd1861fd2007-07-31 12:34:36 +0000204 case DeclSpec::TST_typeofType:
205 case DeclSpec::TST_typeofExpr: return "typeof";
John McCall32d335e2009-08-03 18:47:27 +0000206 case DeclSpec::TST_auto: return "auto";
207 case DeclSpec::TST_decltype: return "(decltype)";
208 case DeclSpec::TST_error: return "(error)";
Reid Spencer5f016e22007-07-11 17:01:13 +0000209 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000210 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000211}
212
John McCall32d335e2009-08-03 18:47:27 +0000213const char *DeclSpec::getSpecifierName(TQ T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000214 switch (T) {
John McCall32d335e2009-08-03 18:47:27 +0000215 case DeclSpec::TQ_unspecified: return "unspecified";
216 case DeclSpec::TQ_const: return "const";
217 case DeclSpec::TQ_restrict: return "restrict";
218 case DeclSpec::TQ_volatile: return "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +0000219 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000220 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000221}
222
223bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000224 const char *&PrevSpec,
225 unsigned &DiagID) {
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000226 if (StorageClassSpec != SCS_unspecified) {
227 // Changing storage class is allowed only if the previous one
228 // was the 'extern' that is part of a linkage specification and
229 // the new storage class is 'typedef'.
230 if (!(SCS_extern_in_linkage_spec &&
231 StorageClassSpec == SCS_extern &&
232 S == SCS_typedef))
233 return BadSpecifier(S, (SCS)StorageClassSpec, PrevSpec, DiagID);
234 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000235 StorageClassSpec = S;
236 StorageClassSpecLoc = Loc;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000237 assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield");
Reid Spencer5f016e22007-07-11 17:01:13 +0000238 return false;
239}
240
Mike Stump1eb44332009-09-09 15:08:12 +0000241bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000242 const char *&PrevSpec,
243 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000244 if (SCS_thread_specified) {
245 PrevSpec = "__thread";
John McCallfec54012009-08-03 20:12:06 +0000246 DiagID = diag::ext_duplicate_declspec;
Reid Spencer5f016e22007-07-11 17:01:13 +0000247 return true;
248 }
249 SCS_thread_specified = true;
250 SCS_threadLoc = Loc;
251 return false;
252}
253
Reid Spencer5f016e22007-07-11 17:01:13 +0000254/// These methods set the specified attribute of the DeclSpec, but return true
255/// and ignore the request if invalid (e.g. "extern" then "auto" is
256/// specified).
257bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000258 const char *&PrevSpec,
259 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000260 if (TypeSpecWidth != TSW_unspecified &&
261 // Allow turning long -> long long.
262 (W != TSW_longlong || TypeSpecWidth != TSW_long))
John McCallfec54012009-08-03 20:12:06 +0000263 return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000264 TypeSpecWidth = W;
265 TSWLoc = Loc;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000266 if (TypeAltiVecVector && !TypeAltiVecBool &&
267 ((TypeSpecWidth == TSW_long) || (TypeSpecWidth == TSW_longlong))) {
John Thompson82287d12010-02-05 00:12:22 +0000268 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
269 DiagID = diag::warn_vector_long_decl_spec_combination;
270 return true;
271 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000272 return false;
273}
274
Mike Stump1eb44332009-09-09 15:08:12 +0000275bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000276 const char *&PrevSpec,
277 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000278 if (TypeSpecComplex != TSC_unspecified)
John McCallfec54012009-08-03 20:12:06 +0000279 return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000280 TypeSpecComplex = C;
281 TSCLoc = Loc;
282 return false;
283}
284
Mike Stump1eb44332009-09-09 15:08:12 +0000285bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000286 const char *&PrevSpec,
287 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000288 if (TypeSpecSign != TSS_unspecified)
John McCallfec54012009-08-03 20:12:06 +0000289 return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000290 TypeSpecSign = S;
291 TSSLoc = Loc;
292 return false;
293}
294
295bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000296 const char *&PrevSpec,
297 unsigned &DiagID,
John McCallb3d87482010-08-24 05:47:05 +0000298 ParsedType Rep) {
299 assert(isTypeRep(T) && "T does not store a type");
300 assert(Rep && "no type provided!");
301 if (TypeSpecType != TST_unspecified) {
302 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
303 DiagID = diag::err_invalid_decl_spec_combination;
304 return true;
305 }
306 TypeSpecType = T;
307 TypeRep = Rep;
308 TSTLoc = Loc;
309 TypeSpecOwned = false;
310 return false;
311}
312
313bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
314 const char *&PrevSpec,
315 unsigned &DiagID,
316 Expr *Rep) {
317 assert(isExprRep(T) && "T does not store an expr");
318 assert(Rep && "no expression provided!");
319 if (TypeSpecType != TST_unspecified) {
320 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
321 DiagID = diag::err_invalid_decl_spec_combination;
322 return true;
323 }
324 TypeSpecType = T;
325 ExprRep = Rep;
326 TSTLoc = Loc;
327 TypeSpecOwned = false;
328 return false;
329}
330
331bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
332 const char *&PrevSpec,
333 unsigned &DiagID,
334 Decl *Rep, bool Owned) {
335 assert(isDeclRep(T) && "T does not store a decl");
336 // Unlike the other cases, we don't assert that we actually get a decl.
337
338 if (TypeSpecType != TST_unspecified) {
339 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
340 DiagID = diag::err_invalid_decl_spec_combination;
341 return true;
342 }
343 TypeSpecType = T;
344 DeclRep = Rep;
345 TSTLoc = Loc;
346 TypeSpecOwned = Owned;
347 return false;
348}
349
350bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
351 const char *&PrevSpec,
352 unsigned &DiagID) {
353 assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) &&
354 "rep required for these type-spec kinds!");
John McCallfec54012009-08-03 20:12:06 +0000355 if (TypeSpecType != TST_unspecified) {
356 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
357 DiagID = diag::err_invalid_decl_spec_combination;
358 return true;
359 }
Chris Lattner788b0fd2010-06-23 06:00:24 +0000360 if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) {
361 TypeAltiVecBool = true;
362 TSTLoc = Loc;
363 return false;
364 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000365 TypeSpecType = T;
Reid Spencer5f016e22007-07-11 17:01:13 +0000366 TSTLoc = Loc;
John McCallb3d87482010-08-24 05:47:05 +0000367 TypeSpecOwned = false;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000368 if (TypeAltiVecVector && !TypeAltiVecBool && (TypeSpecType == TST_double)) {
John Thompson82287d12010-02-05 00:12:22 +0000369 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
Chris Lattner788b0fd2010-06-23 06:00:24 +0000370 DiagID = diag::err_invalid_vector_decl_spec;
John Thompson82287d12010-02-05 00:12:22 +0000371 return true;
372 }
373 return false;
374}
375
376bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
377 const char *&PrevSpec, unsigned &DiagID) {
378 if (TypeSpecType != TST_unspecified) {
379 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
380 DiagID = diag::err_invalid_vector_decl_spec_combination;
381 return true;
382 }
383 TypeAltiVecVector = isAltiVecVector;
384 AltiVecLoc = Loc;
385 return false;
386}
387
388bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
389 const char *&PrevSpec, unsigned &DiagID) {
Chris Lattner788b0fd2010-06-23 06:00:24 +0000390 if (!TypeAltiVecVector || TypeAltiVecPixel ||
391 (TypeSpecType != TST_unspecified)) {
John Thompson82287d12010-02-05 00:12:22 +0000392 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
393 DiagID = diag::err_invalid_pixel_decl_spec_combination;
394 return true;
395 }
John Thompson82287d12010-02-05 00:12:22 +0000396 TypeAltiVecPixel = isAltiVecPixel;
397 TSTLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000398 return false;
399}
400
Douglas Gregorddc29e12009-02-06 22:42:48 +0000401bool DeclSpec::SetTypeSpecError() {
402 TypeSpecType = TST_error;
John McCall9e46b8c2010-08-26 17:22:34 +0000403 TypeSpecOwned = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000404 TSTLoc = SourceLocation();
405 return false;
406}
407
Reid Spencer5f016e22007-07-11 17:01:13 +0000408bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000409 unsigned &DiagID, const LangOptions &Lang) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000410 // Duplicates turn into warnings pre-C99.
411 if ((TypeQualifiers & T) && !Lang.C99)
John McCallfec54012009-08-03 20:12:06 +0000412 return BadSpecifier(T, T, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000413 TypeQualifiers |= T;
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Reid Spencer5f016e22007-07-11 17:01:13 +0000415 switch (T) {
416 default: assert(0 && "Unknown type qualifier!");
417 case TQ_const: TQ_constLoc = Loc; break;
418 case TQ_restrict: TQ_restrictLoc = Loc; break;
419 case TQ_volatile: TQ_volatileLoc = Loc; break;
420 }
421 return false;
422}
423
John McCallfec54012009-08-03 20:12:06 +0000424bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
425 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000426 // 'inline inline' is ok.
427 FS_inline_specified = true;
428 FS_inlineLoc = Loc;
429 return false;
430}
431
John McCallfec54012009-08-03 20:12:06 +0000432bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec,
433 unsigned &DiagID) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000434 // 'virtual virtual' is ok.
435 FS_virtual_specified = true;
436 FS_virtualLoc = Loc;
437 return false;
438}
439
John McCallfec54012009-08-03 20:12:06 +0000440bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec,
441 unsigned &DiagID) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000442 // 'explicit explicit' is ok.
443 FS_explicit_specified = true;
444 FS_explicitLoc = Loc;
445 return false;
446}
447
John McCallfec54012009-08-03 20:12:06 +0000448bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
449 unsigned &DiagID) {
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000450 if (Friend_specified) {
451 PrevSpec = "friend";
John McCallfec54012009-08-03 20:12:06 +0000452 DiagID = diag::ext_duplicate_declspec;
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000453 return true;
454 }
John McCallfec54012009-08-03 20:12:06 +0000455
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000456 Friend_specified = true;
457 FriendLoc = Loc;
458 return false;
459}
Reid Spencer5f016e22007-07-11 17:01:13 +0000460
Sebastian Redl2ac67232009-11-05 15:47:02 +0000461bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
462 unsigned &DiagID) {
463 // 'constexpr constexpr' is ok.
464 Constexpr_specified = true;
465 ConstexprLoc = Loc;
466 return false;
467}
468
John McCalld226f652010-08-21 09:40:31 +0000469void DeclSpec::setProtocolQualifiers(Decl * const *Protos,
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000470 unsigned NP,
471 SourceLocation *ProtoLocs,
472 SourceLocation LAngleLoc) {
473 if (NP == 0) return;
John McCalld226f652010-08-21 09:40:31 +0000474 ProtocolQualifiers = new Decl*[NP];
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000475 ProtocolLocs = new SourceLocation[NP];
John McCalld226f652010-08-21 09:40:31 +0000476 memcpy((void*)ProtocolQualifiers, Protos, sizeof(Decl*)*NP);
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000477 memcpy(ProtocolLocs, ProtoLocs, sizeof(SourceLocation)*NP);
478 NumProtocolQualifiers = NP;
479 ProtocolLAngleLoc = LAngleLoc;
480}
481
Douglas Gregorddf889a2010-01-18 18:04:31 +0000482void DeclSpec::SaveWrittenBuiltinSpecs() {
483 writtenBS.Sign = getTypeSpecSign();
484 writtenBS.Width = getTypeSpecWidth();
485 writtenBS.Type = getTypeSpecType();
486 // Search the list of attributes for the presence of a mode attribute.
487 writtenBS.ModeAttr = false;
John McCall7f040a92010-12-24 02:08:15 +0000488 AttributeList* attrs = getAttributes().getList();
Douglas Gregorddf889a2010-01-18 18:04:31 +0000489 while (attrs) {
490 if (attrs->getKind() == AttributeList::AT_mode) {
491 writtenBS.ModeAttr = true;
492 break;
493 }
494 attrs = attrs->getNext();
495 }
496}
497
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000498void DeclSpec::SaveStorageSpecifierAsWritten() {
499 if (SCS_extern_in_linkage_spec && StorageClassSpec == SCS_extern)
500 // If 'extern' is part of a linkage specification,
501 // then it is not a storage class "as written".
502 StorageClassSpecAsWritten = SCS_unspecified;
503 else
504 StorageClassSpecAsWritten = StorageClassSpec;
505}
506
Reid Spencer5f016e22007-07-11 17:01:13 +0000507/// Finish - This does final analysis of the declspec, rejecting things like
508/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
509/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
510/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000511void DeclSpec::Finish(Diagnostic &D, Preprocessor &PP) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000512 // Before possibly changing their values, save specs as written.
513 SaveWrittenBuiltinSpecs();
Douglas Gregor16573fa2010-04-19 22:54:31 +0000514 SaveStorageSpecifierAsWritten();
Douglas Gregorddf889a2010-01-18 18:04:31 +0000515
Reid Spencer5f016e22007-07-11 17:01:13 +0000516 // Check the type specifier components first.
517
Chris Lattner788b0fd2010-06-23 06:00:24 +0000518 // Validate and finalize AltiVec vector declspec.
519 if (TypeAltiVecVector) {
520 if (TypeAltiVecBool) {
521 // Sign specifiers are not allowed with vector bool. (PIM 2.1)
522 if (TypeSpecSign != TSS_unspecified) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000523 Diag(D, TSSLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner788b0fd2010-06-23 06:00:24 +0000524 << getSpecifierName((TSS)TypeSpecSign);
525 }
526
527 // Only char/int are valid with vector bool. (PIM 2.1)
Duncan Sands2e964a922010-06-23 19:34:52 +0000528 if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) &&
529 (TypeSpecType != TST_int)) || TypeAltiVecPixel) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000530 Diag(D, TSTLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner788b0fd2010-06-23 06:00:24 +0000531 << (TypeAltiVecPixel ? "__pixel" :
532 getSpecifierName((TST)TypeSpecType));
533 }
534
535 // Only 'short' is valid with vector bool. (PIM 2.1)
536 if ((TypeSpecWidth != TSW_unspecified) && (TypeSpecWidth != TSW_short))
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000537 Diag(D, TSWLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner788b0fd2010-06-23 06:00:24 +0000538 << getSpecifierName((TSW)TypeSpecWidth);
539
540 // Elements of vector bool are interpreted as unsigned. (PIM 2.1)
541 if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) ||
542 (TypeSpecWidth != TSW_unspecified))
543 TypeSpecSign = TSS_unsigned;
544 }
545
546 if (TypeAltiVecPixel) {
547 //TODO: perform validation
548 TypeSpecType = TST_int;
549 TypeSpecSign = TSS_unsigned;
550 TypeSpecWidth = TSW_short;
John McCall9e46b8c2010-08-26 17:22:34 +0000551 TypeSpecOwned = false;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000552 }
553 }
554
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000555 // signed/unsigned are only valid with int/char/wchar_t.
Reid Spencer5f016e22007-07-11 17:01:13 +0000556 if (TypeSpecSign != TSS_unspecified) {
557 if (TypeSpecType == TST_unspecified)
558 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000559 else if (TypeSpecType != TST_int &&
560 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000561 Diag(D, TSSLoc, diag::err_invalid_sign_spec)
Chris Lattner254be6a2008-11-22 08:32:36 +0000562 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000563 // signed double -> double.
564 TypeSpecSign = TSS_unspecified;
565 }
566 }
567
568 // Validate the width of the type.
569 switch (TypeSpecWidth) {
570 case TSW_unspecified: break;
571 case TSW_short: // short int
572 case TSW_longlong: // long long int
573 if (TypeSpecType == TST_unspecified)
574 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
575 else if (TypeSpecType != TST_int) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000576 Diag(D, TSWLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000577 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
Chris Lattner254be6a2008-11-22 08:32:36 +0000578 : diag::err_invalid_longlong_spec)
579 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000580 TypeSpecType = TST_int;
John McCall9e46b8c2010-08-26 17:22:34 +0000581 TypeSpecOwned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000582 }
583 break;
584 case TSW_long: // long double, long int
585 if (TypeSpecType == TST_unspecified)
586 TypeSpecType = TST_int; // long -> long int.
587 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000588 Diag(D, TSWLoc, diag::err_invalid_long_spec)
Chris Lattner254be6a2008-11-22 08:32:36 +0000589 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000590 TypeSpecType = TST_int;
John McCall9e46b8c2010-08-26 17:22:34 +0000591 TypeSpecOwned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000592 }
593 break;
594 }
Mike Stump1eb44332009-09-09 15:08:12 +0000595
Reid Spencer5f016e22007-07-11 17:01:13 +0000596 // TODO: if the implementation does not implement _Complex or _Imaginary,
597 // disallow their use. Need information about the backend.
598 if (TypeSpecComplex != TSC_unspecified) {
599 if (TypeSpecType == TST_unspecified) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000600 Diag(D, TSCLoc, diag::ext_plain_complex)
Douglas Gregor849b2432010-03-31 17:46:05 +0000601 << FixItHint::CreateInsertion(
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000602 PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
603 " double");
Reid Spencer5f016e22007-07-11 17:01:13 +0000604 TypeSpecType = TST_double; // _Complex -> _Complex double.
605 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
606 // Note that this intentionally doesn't include _Complex _Bool.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000607 Diag(D, TSTLoc, diag::ext_integer_complex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000608 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000609 Diag(D, TSCLoc, diag::err_invalid_complex_spec)
Chris Lattner254be6a2008-11-22 08:32:36 +0000610 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000611 TypeSpecComplex = TSC_unspecified;
612 }
613 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000614
John McCall67d1a672009-08-06 02:15:43 +0000615 // C++ [class.friend]p6:
616 // No storage-class-specifier shall appear in the decl-specifier-seq
617 // of a friend declaration.
618 if (isFriendSpecified() && getStorageClassSpec()) {
619 DeclSpec::SCS SC = getStorageClassSpec();
620 const char *SpecName = getSpecifierName(SC);
621
622 SourceLocation SCLoc = getStorageClassSpecLoc();
623 SourceLocation SCEndLoc = SCLoc.getFileLocWithOffset(strlen(SpecName));
624
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000625 Diag(D, SCLoc, diag::err_friend_storage_spec)
John McCall67d1a672009-08-06 02:15:43 +0000626 << SpecName
Douglas Gregor849b2432010-03-31 17:46:05 +0000627 << FixItHint::CreateRemoval(SourceRange(SCLoc, SCEndLoc));
John McCall67d1a672009-08-06 02:15:43 +0000628
629 ClearStorageClassSpecs();
630 }
631
John McCall9e46b8c2010-08-26 17:22:34 +0000632 assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType));
633
Reid Spencer5f016e22007-07-11 17:01:13 +0000634 // Okay, now we can infer the real type.
Mike Stump1eb44332009-09-09 15:08:12 +0000635
Reid Spencer5f016e22007-07-11 17:01:13 +0000636 // TODO: return "auto function" and other bad things based on the real type.
Mike Stump1eb44332009-09-09 15:08:12 +0000637
Reid Spencer5f016e22007-07-11 17:01:13 +0000638 // 'data definition has no type or storage class'?
639}
Daniel Dunbare4858a62008-08-11 03:45:03 +0000640
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000641bool DeclSpec::isMissingDeclaratorOk() {
642 TST tst = getTypeSpecType();
John McCallb3d87482010-08-24 05:47:05 +0000643 return isDeclRep(tst) && getRepAsDecl() != 0 &&
644 StorageClassSpec != DeclSpec::SCS_typedef;
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000645}
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000646
647void UnqualifiedId::clear() {
648 if (Kind == IK_TemplateId)
649 TemplateId->Destroy();
650
651 Kind = IK_Identifier;
652 Identifier = 0;
653 StartLocation = SourceLocation();
654 EndLocation = SourceLocation();
655}
656
657void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc,
658 OverloadedOperatorKind Op,
659 SourceLocation SymbolLocations[3]) {
660 Kind = IK_OperatorFunctionId;
661 StartLocation = OperatorLoc;
662 EndLocation = OperatorLoc;
663 OperatorFunctionId.Operator = Op;
664 for (unsigned I = 0; I != 3; ++I) {
665 OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding();
666
667 if (SymbolLocations[I].isValid())
668 EndLocation = SymbolLocations[I];
669 }
670}
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000671
Anders Carlsson46127a92011-01-22 15:58:16 +0000672bool VirtSpecifiers::SetSpecifier(VirtSpecifier VS, SourceLocation Loc,
673 const char *&PrevSpec) {
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000674 if (Specifiers & VS) {
675 PrevSpec = getSpecifierName(VS);
676 return true;
677 }
678
679 Specifiers |= VS;
680
681 switch (VS) {
682 default: assert(0 && "Unknown specifier!");
683 case VS_Override: VS_overrideLoc = Loc; break;
684 case VS_Final: VS_finalLoc = Loc; break;
685 case VS_New: VS_newLoc = Loc; break;
686 }
Anders Carlsson46127a92011-01-22 15:58:16 +0000687
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000688 return false;
689}
690
Anders Carlssonc46bb7d2011-01-22 15:11:37 +0000691const char *VirtSpecifiers::getSpecifierName(VirtSpecifier VS) {
692 switch (VS) {
693 default: assert(0 && "Unknown specifier");
694 case VS_Override: return "override";
695 case VS_Final: return "final";
696 case VS_New: return "new";
697 }
698}
Anders Carlsson46127a92011-01-22 15:58:16 +0000699
700bool ClassVirtSpecifiers::SetSpecifier(ClassVirtSpecifier CVS,
701 SourceLocation Loc,
702 const char *&PrevSpec) {
703 if (Specifiers & CVS) {
704 PrevSpec = getSpecifierName(CVS);
705 return true;
706 }
707
708 Specifiers |= CVS;
709
710 switch (CVS) {
711 default: assert(0 && "Unknown specifier!");
712 case CVS_Final: CVS_finalLoc = Loc; break;
713 case CVS_Explicit: CVS_explicitLoc = Loc; break;
714 }
715
716 return false;
717}
718
719const char *ClassVirtSpecifiers::getSpecifierName(ClassVirtSpecifier CVS) {
720 switch (CVS) {
721 default: assert(0 && "Unknown specifier");
722 case CVS_Final: return "final";
723 case CVS_Explicit: return "explicit";
724 }
725}
726