blob: 39984974bfd43c47f1edc033a3473d0e7eb78ef8 [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 Gregor2e4c34a2011-02-24 00:17:56 +000017#include "clang/AST/NestedNameSpecifier.h"
18#include "clang/AST/TypeLoc.h"
Douglas Gregor9b3064b2009-04-01 22:41:11 +000019#include "clang/Lex/Preprocessor.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "clang/Basic/LangOptions.h"
Chris Lattner5af2f352009-01-20 19:11:22 +000021#include "llvm/ADT/STLExtras.h"
John McCall32d335e2009-08-03 18:47:27 +000022#include "llvm/Support/ErrorHandling.h"
Douglas Gregore4e5b052009-03-19 00:18:19 +000023#include <cstring>
Reid Spencer5f016e22007-07-11 17:01:13 +000024using namespace clang;
25
Chris Lattner254be6a2008-11-22 08:32:36 +000026
27static DiagnosticBuilder Diag(Diagnostic &D, SourceLocation Loc,
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000028 unsigned DiagID) {
29 return D.Report(Loc, DiagID);
Chris Lattner254be6a2008-11-22 08:32:36 +000030}
31
Douglas Gregor314b97f2009-11-10 19:49:08 +000032
33void UnqualifiedId::setTemplateId(TemplateIdAnnotation *TemplateId) {
34 assert(TemplateId && "NULL template-id annotation?");
35 Kind = IK_TemplateId;
36 this->TemplateId = TemplateId;
37 StartLocation = TemplateId->TemplateNameLoc;
38 EndLocation = TemplateId->RAngleLoc;
39}
40
Douglas Gregor0efc2c12010-01-13 17:31:36 +000041void UnqualifiedId::setConstructorTemplateId(TemplateIdAnnotation *TemplateId) {
42 assert(TemplateId && "NULL template-id annotation?");
43 Kind = IK_ConstructorTemplateId;
44 this->TemplateId = TemplateId;
45 StartLocation = TemplateId->TemplateNameLoc;
46 EndLocation = TemplateId->RAngleLoc;
47}
48
Douglas Gregor2e4c34a2011-02-24 00:17:56 +000049void CXXScopeSpec::Extend(ASTContext &Context, SourceLocation TemplateKWLoc,
50 TypeLoc TL, SourceLocation ColonColonLoc) {
51 ScopeRep = NestedNameSpecifier::Create(Context, ScopeRep,
52 TemplateKWLoc.isValid(),
53 TL.getTypePtr());
54 if (Range.getBegin().isInvalid())
55 Range.setBegin(TL.getBeginLoc());
56 Range.setEnd(ColonColonLoc);
57}
58
59void CXXScopeSpec::Extend(ASTContext &Context, IdentifierInfo *Identifier,
60 SourceLocation IdentifierLoc,
61 SourceLocation ColonColonLoc) {
62 ScopeRep = NestedNameSpecifier::Create(Context, ScopeRep, Identifier);
63 if (Range.getBegin().isInvalid())
64 Range.setBegin(IdentifierLoc);
65 Range.setEnd(ColonColonLoc);
66}
67
68void CXXScopeSpec::Extend(ASTContext &Context, NamespaceDecl *Namespace,
69 SourceLocation NamespaceLoc,
70 SourceLocation ColonColonLoc) {
71 ScopeRep = NestedNameSpecifier::Create(Context, ScopeRep, Namespace);
72 if (Range.getBegin().isInvalid())
73 Range.setBegin(NamespaceLoc);
74 Range.setEnd(ColonColonLoc);
75}
76
77void CXXScopeSpec::MakeGlobal(ASTContext &Context,
78 SourceLocation ColonColonLoc) {
79 assert(!ScopeRep && "Already have a nested-name-specifier!?");
80 ScopeRep = NestedNameSpecifier::GlobalSpecifier(Context);
81 Range = SourceRange(ColonColonLoc);
82}
83
Chris Lattner5af2f352009-01-20 19:11:22 +000084/// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function.
85/// "TheDeclarator" is the declarator that this will be added to.
John McCall7f040a92010-12-24 02:08:15 +000086DeclaratorChunk DeclaratorChunk::getFunction(const ParsedAttributes &attrs,
87 bool hasProto, bool isVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +000088 SourceLocation EllipsisLoc,
Chris Lattner5af2f352009-01-20 19:11:22 +000089 ParamInfo *ArgInfo,
90 unsigned NumArgs,
91 unsigned TypeQuals,
Douglas Gregor83f51722011-01-26 03:43:54 +000092 bool RefQualifierIsLvalueRef,
93 SourceLocation RefQualifierLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +000094 bool hasExceptionSpec,
Sebastian Redl3cc97262009-05-31 11:47:27 +000095 SourceLocation ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +000096 bool hasAnyExceptionSpec,
John McCallb3d87482010-08-24 05:47:05 +000097 ParsedType *Exceptions,
Sebastian Redlef65f062009-05-29 18:02:33 +000098 SourceRange *ExceptionRanges,
Sebastian Redl7dc81342009-04-29 17:30:04 +000099 unsigned NumExceptions,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +0000100 SourceLocation LPLoc,
101 SourceLocation RPLoc,
Douglas Gregordab60ad2010-10-01 18:44:50 +0000102 Declarator &TheDeclarator,
103 ParsedType TrailingReturnType) {
Chris Lattner5af2f352009-01-20 19:11:22 +0000104 DeclaratorChunk I;
Sebastian Redl7dc81342009-04-29 17:30:04 +0000105 I.Kind = Function;
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +0000106 I.Loc = LPLoc;
107 I.EndLoc = RPLoc;
John McCall7f040a92010-12-24 02:08:15 +0000108 I.Fun.AttrList = attrs.getList();
Sebastian Redl7dc81342009-04-29 17:30:04 +0000109 I.Fun.hasPrototype = hasProto;
110 I.Fun.isVariadic = isVariadic;
111 I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding();
112 I.Fun.DeleteArgInfo = false;
113 I.Fun.TypeQuals = TypeQuals;
114 I.Fun.NumArgs = NumArgs;
115 I.Fun.ArgInfo = 0;
Douglas Gregor83f51722011-01-26 03:43:54 +0000116 I.Fun.RefQualifierIsLValueRef = RefQualifierIsLvalueRef;
117 I.Fun.RefQualifierLoc = RefQualifierLoc.getRawEncoding();
Sebastian Redl7dc81342009-04-29 17:30:04 +0000118 I.Fun.hasExceptionSpec = hasExceptionSpec;
Sebastian Redl3cc97262009-05-31 11:47:27 +0000119 I.Fun.ThrowLoc = ThrowLoc.getRawEncoding();
Sebastian Redl7dc81342009-04-29 17:30:04 +0000120 I.Fun.hasAnyExceptionSpec = hasAnyExceptionSpec;
121 I.Fun.NumExceptions = NumExceptions;
122 I.Fun.Exceptions = 0;
Douglas Gregordab60ad2010-10-01 18:44:50 +0000123 I.Fun.TrailingReturnType = TrailingReturnType.getAsOpaquePtr();
Sebastian Redl7dc81342009-04-29 17:30:04 +0000124
Chris Lattner5af2f352009-01-20 19:11:22 +0000125 // new[] an argument array if needed.
126 if (NumArgs) {
127 // If the 'InlineParams' in Declarator is unused and big enough, put our
128 // parameter list there (in an effort to avoid new/delete traffic). If it
129 // is already used (consider a function returning a function pointer) or too
130 // small (function taking too many arguments), go to the heap.
Mike Stump1eb44332009-09-09 15:08:12 +0000131 if (!TheDeclarator.InlineParamsUsed &&
Chris Lattner5af2f352009-01-20 19:11:22 +0000132 NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
133 I.Fun.ArgInfo = TheDeclarator.InlineParams;
134 I.Fun.DeleteArgInfo = false;
135 TheDeclarator.InlineParamsUsed = true;
136 } else {
137 I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs];
138 I.Fun.DeleteArgInfo = true;
139 }
140 memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs);
141 }
Sebastian Redl7dc81342009-04-29 17:30:04 +0000142 // new[] an exception array if needed
143 if (NumExceptions) {
Sebastian Redlef65f062009-05-29 18:02:33 +0000144 I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions];
145 for (unsigned i = 0; i != NumExceptions; ++i) {
146 I.Fun.Exceptions[i].Ty = Exceptions[i];
147 I.Fun.Exceptions[i].Range = ExceptionRanges[i];
148 }
Sebastian Redl7dc81342009-04-29 17:30:04 +0000149 }
Chris Lattner5af2f352009-01-20 19:11:22 +0000150 return I;
151}
Chris Lattner254be6a2008-11-22 08:32:36 +0000152
Reid Spencer5f016e22007-07-11 17:01:13 +0000153/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
Chris Lattner2a327d12009-02-27 18:35:46 +0000154/// declaration specifier includes.
Reid Spencer5f016e22007-07-11 17:01:13 +0000155///
156unsigned DeclSpec::getParsedSpecifiers() const {
157 unsigned Res = 0;
158 if (StorageClassSpec != SCS_unspecified ||
159 SCS_thread_specified)
160 Res |= PQ_StorageClassSpecifier;
Mike Stumpd4204332008-06-19 19:52:46 +0000161
Reid Spencer5f016e22007-07-11 17:01:13 +0000162 if (TypeQualifiers != TQ_unspecified)
163 Res |= PQ_TypeQualifier;
Mike Stump1eb44332009-09-09 15:08:12 +0000164
Reid Spencer5f016e22007-07-11 17:01:13 +0000165 if (hasTypeSpecifier())
166 Res |= PQ_TypeSpecifier;
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Douglas Gregorb48fe382008-10-31 09:07:45 +0000168 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
Reid Spencer5f016e22007-07-11 17:01:13 +0000169 Res |= PQ_FunctionSpecifier;
170 return Res;
171}
172
John McCallfec54012009-08-03 20:12:06 +0000173template <class T> static bool BadSpecifier(T TNew, T TPrev,
174 const char *&PrevSpec,
175 unsigned &DiagID) {
John McCall32d335e2009-08-03 18:47:27 +0000176 PrevSpec = DeclSpec::getSpecifierName(TPrev);
John McCallfec54012009-08-03 20:12:06 +0000177 DiagID = (TNew == TPrev ? diag::ext_duplicate_declspec
178 : diag::err_invalid_decl_spec_combination);
John McCall32d335e2009-08-03 18:47:27 +0000179 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000180}
John McCall32d335e2009-08-03 18:47:27 +0000181
Reid Spencer5f016e22007-07-11 17:01:13 +0000182const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
183 switch (S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000184 case DeclSpec::SCS_unspecified: return "unspecified";
185 case DeclSpec::SCS_typedef: return "typedef";
186 case DeclSpec::SCS_extern: return "extern";
187 case DeclSpec::SCS_static: return "static";
188 case DeclSpec::SCS_auto: return "auto";
189 case DeclSpec::SCS_register: return "register";
Eli Friedman63054b32009-04-19 20:27:55 +0000190 case DeclSpec::SCS_private_extern: return "__private_extern__";
Sebastian Redl669d5d72008-11-14 23:42:31 +0000191 case DeclSpec::SCS_mutable: return "mutable";
Reid Spencer5f016e22007-07-11 17:01:13 +0000192 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000193 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000194}
195
John McCall32d335e2009-08-03 18:47:27 +0000196const char *DeclSpec::getSpecifierName(TSW W) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000197 switch (W) {
John McCall32d335e2009-08-03 18:47:27 +0000198 case TSW_unspecified: return "unspecified";
199 case TSW_short: return "short";
200 case TSW_long: return "long";
201 case TSW_longlong: return "long long";
Reid Spencer5f016e22007-07-11 17:01:13 +0000202 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000203 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000204}
205
John McCall32d335e2009-08-03 18:47:27 +0000206const char *DeclSpec::getSpecifierName(TSC C) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000207 switch (C) {
John McCall32d335e2009-08-03 18:47:27 +0000208 case TSC_unspecified: return "unspecified";
209 case TSC_imaginary: return "imaginary";
210 case TSC_complex: return "complex";
Reid Spencer5f016e22007-07-11 17:01:13 +0000211 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000212 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000213}
214
215
John McCall32d335e2009-08-03 18:47:27 +0000216const char *DeclSpec::getSpecifierName(TSS S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000217 switch (S) {
John McCall32d335e2009-08-03 18:47:27 +0000218 case TSS_unspecified: return "unspecified";
219 case TSS_signed: return "signed";
220 case TSS_unsigned: return "unsigned";
Reid Spencer5f016e22007-07-11 17:01:13 +0000221 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000222 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000223}
224
225const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
226 switch (T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000227 case DeclSpec::TST_unspecified: return "unspecified";
228 case DeclSpec::TST_void: return "void";
229 case DeclSpec::TST_char: return "char";
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000230 case DeclSpec::TST_wchar: return "wchar_t";
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000231 case DeclSpec::TST_char16: return "char16_t";
232 case DeclSpec::TST_char32: return "char32_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000233 case DeclSpec::TST_int: return "int";
234 case DeclSpec::TST_float: return "float";
235 case DeclSpec::TST_double: return "double";
236 case DeclSpec::TST_bool: return "_Bool";
237 case DeclSpec::TST_decimal32: return "_Decimal32";
238 case DeclSpec::TST_decimal64: return "_Decimal64";
239 case DeclSpec::TST_decimal128: return "_Decimal128";
240 case DeclSpec::TST_enum: return "enum";
Chris Lattner99dc9142008-04-13 18:59:07 +0000241 case DeclSpec::TST_class: return "class";
Reid Spencer5f016e22007-07-11 17:01:13 +0000242 case DeclSpec::TST_union: return "union";
243 case DeclSpec::TST_struct: return "struct";
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000244 case DeclSpec::TST_typename: return "type-name";
Steve Naroffd1861fd2007-07-31 12:34:36 +0000245 case DeclSpec::TST_typeofType:
246 case DeclSpec::TST_typeofExpr: return "typeof";
John McCall32d335e2009-08-03 18:47:27 +0000247 case DeclSpec::TST_auto: return "auto";
248 case DeclSpec::TST_decltype: return "(decltype)";
249 case DeclSpec::TST_error: return "(error)";
Reid Spencer5f016e22007-07-11 17:01:13 +0000250 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000251 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000252}
253
John McCall32d335e2009-08-03 18:47:27 +0000254const char *DeclSpec::getSpecifierName(TQ T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000255 switch (T) {
John McCall32d335e2009-08-03 18:47:27 +0000256 case DeclSpec::TQ_unspecified: return "unspecified";
257 case DeclSpec::TQ_const: return "const";
258 case DeclSpec::TQ_restrict: return "restrict";
259 case DeclSpec::TQ_volatile: return "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +0000260 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000261 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000262}
263
264bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000265 const char *&PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +0000266 unsigned &DiagID,
267 const LangOptions &Lang) {
268 // OpenCL prohibits extern, auto, register, and static
269 // It seems sensible to prohibit private_extern too
270 if (Lang.OpenCL) {
271 switch (S) {
272 case SCS_extern:
273 case SCS_private_extern:
274 case SCS_auto:
275 case SCS_register:
276 case SCS_static:
277 DiagID = diag::err_not_opencl_storage_class_specifier;
278 PrevSpec = getSpecifierName(S);
279 return true;
280 default:
281 break;
282 }
283 }
284
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000285 if (StorageClassSpec != SCS_unspecified) {
286 // Changing storage class is allowed only if the previous one
287 // was the 'extern' that is part of a linkage specification and
288 // the new storage class is 'typedef'.
289 if (!(SCS_extern_in_linkage_spec &&
290 StorageClassSpec == SCS_extern &&
291 S == SCS_typedef))
292 return BadSpecifier(S, (SCS)StorageClassSpec, PrevSpec, DiagID);
293 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000294 StorageClassSpec = S;
295 StorageClassSpecLoc = Loc;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000296 assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield");
Reid Spencer5f016e22007-07-11 17:01:13 +0000297 return false;
298}
299
Mike Stump1eb44332009-09-09 15:08:12 +0000300bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000301 const char *&PrevSpec,
302 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000303 if (SCS_thread_specified) {
304 PrevSpec = "__thread";
John McCallfec54012009-08-03 20:12:06 +0000305 DiagID = diag::ext_duplicate_declspec;
Reid Spencer5f016e22007-07-11 17:01:13 +0000306 return true;
307 }
308 SCS_thread_specified = true;
309 SCS_threadLoc = Loc;
310 return false;
311}
312
Reid Spencer5f016e22007-07-11 17:01:13 +0000313/// These methods set the specified attribute of the DeclSpec, but return true
314/// and ignore the request if invalid (e.g. "extern" then "auto" is
315/// specified).
316bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000317 const char *&PrevSpec,
318 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000319 if (TypeSpecWidth != TSW_unspecified &&
320 // Allow turning long -> long long.
321 (W != TSW_longlong || TypeSpecWidth != TSW_long))
John McCallfec54012009-08-03 20:12:06 +0000322 return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000323 TypeSpecWidth = W;
324 TSWLoc = Loc;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000325 if (TypeAltiVecVector && !TypeAltiVecBool &&
326 ((TypeSpecWidth == TSW_long) || (TypeSpecWidth == TSW_longlong))) {
John Thompson82287d12010-02-05 00:12:22 +0000327 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
328 DiagID = diag::warn_vector_long_decl_spec_combination;
329 return true;
330 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000331 return false;
332}
333
Mike Stump1eb44332009-09-09 15:08:12 +0000334bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000335 const char *&PrevSpec,
336 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000337 if (TypeSpecComplex != TSC_unspecified)
John McCallfec54012009-08-03 20:12:06 +0000338 return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000339 TypeSpecComplex = C;
340 TSCLoc = Loc;
341 return false;
342}
343
Mike Stump1eb44332009-09-09 15:08:12 +0000344bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000345 const char *&PrevSpec,
346 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000347 if (TypeSpecSign != TSS_unspecified)
John McCallfec54012009-08-03 20:12:06 +0000348 return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000349 TypeSpecSign = S;
350 TSSLoc = Loc;
351 return false;
352}
353
354bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000355 const char *&PrevSpec,
356 unsigned &DiagID,
John McCallb3d87482010-08-24 05:47:05 +0000357 ParsedType Rep) {
358 assert(isTypeRep(T) && "T does not store a type");
359 assert(Rep && "no type provided!");
360 if (TypeSpecType != TST_unspecified) {
361 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
362 DiagID = diag::err_invalid_decl_spec_combination;
363 return true;
364 }
365 TypeSpecType = T;
366 TypeRep = Rep;
367 TSTLoc = Loc;
368 TypeSpecOwned = false;
369 return false;
370}
371
372bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
373 const char *&PrevSpec,
374 unsigned &DiagID,
375 Expr *Rep) {
376 assert(isExprRep(T) && "T does not store an expr");
377 assert(Rep && "no expression provided!");
378 if (TypeSpecType != TST_unspecified) {
379 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
380 DiagID = diag::err_invalid_decl_spec_combination;
381 return true;
382 }
383 TypeSpecType = T;
384 ExprRep = Rep;
385 TSTLoc = Loc;
386 TypeSpecOwned = false;
387 return false;
388}
389
390bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
391 const char *&PrevSpec,
392 unsigned &DiagID,
393 Decl *Rep, bool Owned) {
394 assert(isDeclRep(T) && "T does not store a decl");
395 // Unlike the other cases, we don't assert that we actually get a decl.
396
397 if (TypeSpecType != TST_unspecified) {
398 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
399 DiagID = diag::err_invalid_decl_spec_combination;
400 return true;
401 }
402 TypeSpecType = T;
403 DeclRep = Rep;
404 TSTLoc = Loc;
405 TypeSpecOwned = Owned;
406 return false;
407}
408
409bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
410 const char *&PrevSpec,
411 unsigned &DiagID) {
412 assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) &&
413 "rep required for these type-spec kinds!");
John McCallfec54012009-08-03 20:12:06 +0000414 if (TypeSpecType != TST_unspecified) {
415 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
416 DiagID = diag::err_invalid_decl_spec_combination;
417 return true;
418 }
Chris Lattner788b0fd2010-06-23 06:00:24 +0000419 if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) {
420 TypeAltiVecBool = true;
421 TSTLoc = Loc;
422 return false;
423 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000424 TypeSpecType = T;
Reid Spencer5f016e22007-07-11 17:01:13 +0000425 TSTLoc = Loc;
John McCallb3d87482010-08-24 05:47:05 +0000426 TypeSpecOwned = false;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000427 if (TypeAltiVecVector && !TypeAltiVecBool && (TypeSpecType == TST_double)) {
John Thompson82287d12010-02-05 00:12:22 +0000428 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
Chris Lattner788b0fd2010-06-23 06:00:24 +0000429 DiagID = diag::err_invalid_vector_decl_spec;
John Thompson82287d12010-02-05 00:12:22 +0000430 return true;
431 }
432 return false;
433}
434
435bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
436 const char *&PrevSpec, unsigned &DiagID) {
437 if (TypeSpecType != TST_unspecified) {
438 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
439 DiagID = diag::err_invalid_vector_decl_spec_combination;
440 return true;
441 }
442 TypeAltiVecVector = isAltiVecVector;
443 AltiVecLoc = Loc;
444 return false;
445}
446
447bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
448 const char *&PrevSpec, unsigned &DiagID) {
Chris Lattner788b0fd2010-06-23 06:00:24 +0000449 if (!TypeAltiVecVector || TypeAltiVecPixel ||
450 (TypeSpecType != TST_unspecified)) {
John Thompson82287d12010-02-05 00:12:22 +0000451 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
452 DiagID = diag::err_invalid_pixel_decl_spec_combination;
453 return true;
454 }
John Thompson82287d12010-02-05 00:12:22 +0000455 TypeAltiVecPixel = isAltiVecPixel;
456 TSTLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000457 return false;
458}
459
Douglas Gregorddc29e12009-02-06 22:42:48 +0000460bool DeclSpec::SetTypeSpecError() {
461 TypeSpecType = TST_error;
John McCall9e46b8c2010-08-26 17:22:34 +0000462 TypeSpecOwned = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000463 TSTLoc = SourceLocation();
464 return false;
465}
466
Reid Spencer5f016e22007-07-11 17:01:13 +0000467bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000468 unsigned &DiagID, const LangOptions &Lang) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000469 // Duplicates turn into warnings pre-C99.
470 if ((TypeQualifiers & T) && !Lang.C99)
John McCallfec54012009-08-03 20:12:06 +0000471 return BadSpecifier(T, T, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000472 TypeQualifiers |= T;
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Reid Spencer5f016e22007-07-11 17:01:13 +0000474 switch (T) {
475 default: assert(0 && "Unknown type qualifier!");
476 case TQ_const: TQ_constLoc = Loc; break;
477 case TQ_restrict: TQ_restrictLoc = Loc; break;
478 case TQ_volatile: TQ_volatileLoc = Loc; break;
479 }
480 return false;
481}
482
John McCallfec54012009-08-03 20:12:06 +0000483bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
484 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000485 // 'inline inline' is ok.
486 FS_inline_specified = true;
487 FS_inlineLoc = Loc;
488 return false;
489}
490
John McCallfec54012009-08-03 20:12:06 +0000491bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec,
492 unsigned &DiagID) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000493 // 'virtual virtual' is ok.
494 FS_virtual_specified = true;
495 FS_virtualLoc = Loc;
496 return false;
497}
498
John McCallfec54012009-08-03 20:12:06 +0000499bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec,
500 unsigned &DiagID) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000501 // 'explicit explicit' is ok.
502 FS_explicit_specified = true;
503 FS_explicitLoc = Loc;
504 return false;
505}
506
John McCallfec54012009-08-03 20:12:06 +0000507bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
508 unsigned &DiagID) {
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000509 if (Friend_specified) {
510 PrevSpec = "friend";
John McCallfec54012009-08-03 20:12:06 +0000511 DiagID = diag::ext_duplicate_declspec;
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000512 return true;
513 }
John McCallfec54012009-08-03 20:12:06 +0000514
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000515 Friend_specified = true;
516 FriendLoc = Loc;
517 return false;
518}
Reid Spencer5f016e22007-07-11 17:01:13 +0000519
Sebastian Redl2ac67232009-11-05 15:47:02 +0000520bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
521 unsigned &DiagID) {
522 // 'constexpr constexpr' is ok.
523 Constexpr_specified = true;
524 ConstexprLoc = Loc;
525 return false;
526}
527
John McCalld226f652010-08-21 09:40:31 +0000528void DeclSpec::setProtocolQualifiers(Decl * const *Protos,
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000529 unsigned NP,
530 SourceLocation *ProtoLocs,
531 SourceLocation LAngleLoc) {
532 if (NP == 0) return;
John McCalld226f652010-08-21 09:40:31 +0000533 ProtocolQualifiers = new Decl*[NP];
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000534 ProtocolLocs = new SourceLocation[NP];
John McCalld226f652010-08-21 09:40:31 +0000535 memcpy((void*)ProtocolQualifiers, Protos, sizeof(Decl*)*NP);
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000536 memcpy(ProtocolLocs, ProtoLocs, sizeof(SourceLocation)*NP);
537 NumProtocolQualifiers = NP;
538 ProtocolLAngleLoc = LAngleLoc;
539}
540
Douglas Gregorddf889a2010-01-18 18:04:31 +0000541void DeclSpec::SaveWrittenBuiltinSpecs() {
542 writtenBS.Sign = getTypeSpecSign();
543 writtenBS.Width = getTypeSpecWidth();
544 writtenBS.Type = getTypeSpecType();
545 // Search the list of attributes for the presence of a mode attribute.
546 writtenBS.ModeAttr = false;
John McCall7f040a92010-12-24 02:08:15 +0000547 AttributeList* attrs = getAttributes().getList();
Douglas Gregorddf889a2010-01-18 18:04:31 +0000548 while (attrs) {
549 if (attrs->getKind() == AttributeList::AT_mode) {
550 writtenBS.ModeAttr = true;
551 break;
552 }
553 attrs = attrs->getNext();
554 }
555}
556
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000557void DeclSpec::SaveStorageSpecifierAsWritten() {
558 if (SCS_extern_in_linkage_spec && StorageClassSpec == SCS_extern)
559 // If 'extern' is part of a linkage specification,
560 // then it is not a storage class "as written".
561 StorageClassSpecAsWritten = SCS_unspecified;
562 else
563 StorageClassSpecAsWritten = StorageClassSpec;
564}
565
Reid Spencer5f016e22007-07-11 17:01:13 +0000566/// Finish - This does final analysis of the declspec, rejecting things like
567/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
568/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
569/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000570void DeclSpec::Finish(Diagnostic &D, Preprocessor &PP) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000571 // Before possibly changing their values, save specs as written.
572 SaveWrittenBuiltinSpecs();
Douglas Gregor16573fa2010-04-19 22:54:31 +0000573 SaveStorageSpecifierAsWritten();
Douglas Gregorddf889a2010-01-18 18:04:31 +0000574
Reid Spencer5f016e22007-07-11 17:01:13 +0000575 // Check the type specifier components first.
576
Chris Lattner788b0fd2010-06-23 06:00:24 +0000577 // Validate and finalize AltiVec vector declspec.
578 if (TypeAltiVecVector) {
579 if (TypeAltiVecBool) {
580 // Sign specifiers are not allowed with vector bool. (PIM 2.1)
581 if (TypeSpecSign != TSS_unspecified) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000582 Diag(D, TSSLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner788b0fd2010-06-23 06:00:24 +0000583 << getSpecifierName((TSS)TypeSpecSign);
584 }
585
586 // Only char/int are valid with vector bool. (PIM 2.1)
Duncan Sands2e964a922010-06-23 19:34:52 +0000587 if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) &&
588 (TypeSpecType != TST_int)) || TypeAltiVecPixel) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000589 Diag(D, TSTLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner788b0fd2010-06-23 06:00:24 +0000590 << (TypeAltiVecPixel ? "__pixel" :
591 getSpecifierName((TST)TypeSpecType));
592 }
593
594 // Only 'short' is valid with vector bool. (PIM 2.1)
595 if ((TypeSpecWidth != TSW_unspecified) && (TypeSpecWidth != TSW_short))
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000596 Diag(D, TSWLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner788b0fd2010-06-23 06:00:24 +0000597 << getSpecifierName((TSW)TypeSpecWidth);
598
599 // Elements of vector bool are interpreted as unsigned. (PIM 2.1)
600 if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) ||
601 (TypeSpecWidth != TSW_unspecified))
602 TypeSpecSign = TSS_unsigned;
603 }
604
605 if (TypeAltiVecPixel) {
606 //TODO: perform validation
607 TypeSpecType = TST_int;
608 TypeSpecSign = TSS_unsigned;
609 TypeSpecWidth = TSW_short;
John McCall9e46b8c2010-08-26 17:22:34 +0000610 TypeSpecOwned = false;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000611 }
612 }
613
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000614 // signed/unsigned are only valid with int/char/wchar_t.
Reid Spencer5f016e22007-07-11 17:01:13 +0000615 if (TypeSpecSign != TSS_unspecified) {
616 if (TypeSpecType == TST_unspecified)
617 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000618 else if (TypeSpecType != TST_int &&
619 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000620 Diag(D, TSSLoc, diag::err_invalid_sign_spec)
Chris Lattner254be6a2008-11-22 08:32:36 +0000621 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000622 // signed double -> double.
623 TypeSpecSign = TSS_unspecified;
624 }
625 }
626
627 // Validate the width of the type.
628 switch (TypeSpecWidth) {
629 case TSW_unspecified: break;
630 case TSW_short: // short int
631 case TSW_longlong: // long long int
632 if (TypeSpecType == TST_unspecified)
633 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
634 else if (TypeSpecType != TST_int) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000635 Diag(D, TSWLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000636 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
Chris Lattner254be6a2008-11-22 08:32:36 +0000637 : diag::err_invalid_longlong_spec)
638 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000639 TypeSpecType = TST_int;
John McCall9e46b8c2010-08-26 17:22:34 +0000640 TypeSpecOwned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000641 }
642 break;
643 case TSW_long: // long double, long int
644 if (TypeSpecType == TST_unspecified)
645 TypeSpecType = TST_int; // long -> long int.
646 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000647 Diag(D, TSWLoc, diag::err_invalid_long_spec)
Chris Lattner254be6a2008-11-22 08:32:36 +0000648 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000649 TypeSpecType = TST_int;
John McCall9e46b8c2010-08-26 17:22:34 +0000650 TypeSpecOwned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000651 }
652 break;
653 }
Mike Stump1eb44332009-09-09 15:08:12 +0000654
Reid Spencer5f016e22007-07-11 17:01:13 +0000655 // TODO: if the implementation does not implement _Complex or _Imaginary,
656 // disallow their use. Need information about the backend.
657 if (TypeSpecComplex != TSC_unspecified) {
658 if (TypeSpecType == TST_unspecified) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000659 Diag(D, TSCLoc, diag::ext_plain_complex)
Douglas Gregor849b2432010-03-31 17:46:05 +0000660 << FixItHint::CreateInsertion(
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000661 PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
662 " double");
Reid Spencer5f016e22007-07-11 17:01:13 +0000663 TypeSpecType = TST_double; // _Complex -> _Complex double.
664 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
665 // Note that this intentionally doesn't include _Complex _Bool.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000666 Diag(D, TSTLoc, diag::ext_integer_complex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000667 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000668 Diag(D, TSCLoc, diag::err_invalid_complex_spec)
Chris Lattner254be6a2008-11-22 08:32:36 +0000669 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000670 TypeSpecComplex = TSC_unspecified;
671 }
672 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000673
John McCall67d1a672009-08-06 02:15:43 +0000674 // C++ [class.friend]p6:
675 // No storage-class-specifier shall appear in the decl-specifier-seq
676 // of a friend declaration.
677 if (isFriendSpecified() && getStorageClassSpec()) {
678 DeclSpec::SCS SC = getStorageClassSpec();
679 const char *SpecName = getSpecifierName(SC);
680
681 SourceLocation SCLoc = getStorageClassSpecLoc();
682 SourceLocation SCEndLoc = SCLoc.getFileLocWithOffset(strlen(SpecName));
683
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000684 Diag(D, SCLoc, diag::err_friend_storage_spec)
John McCall67d1a672009-08-06 02:15:43 +0000685 << SpecName
Douglas Gregor849b2432010-03-31 17:46:05 +0000686 << FixItHint::CreateRemoval(SourceRange(SCLoc, SCEndLoc));
John McCall67d1a672009-08-06 02:15:43 +0000687
688 ClearStorageClassSpecs();
689 }
690
John McCall9e46b8c2010-08-26 17:22:34 +0000691 assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType));
692
Reid Spencer5f016e22007-07-11 17:01:13 +0000693 // Okay, now we can infer the real type.
Mike Stump1eb44332009-09-09 15:08:12 +0000694
Reid Spencer5f016e22007-07-11 17:01:13 +0000695 // TODO: return "auto function" and other bad things based on the real type.
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Reid Spencer5f016e22007-07-11 17:01:13 +0000697 // 'data definition has no type or storage class'?
698}
Daniel Dunbare4858a62008-08-11 03:45:03 +0000699
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000700bool DeclSpec::isMissingDeclaratorOk() {
701 TST tst = getTypeSpecType();
John McCallb3d87482010-08-24 05:47:05 +0000702 return isDeclRep(tst) && getRepAsDecl() != 0 &&
703 StorageClassSpec != DeclSpec::SCS_typedef;
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000704}
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000705
706void UnqualifiedId::clear() {
707 if (Kind == IK_TemplateId)
708 TemplateId->Destroy();
709
710 Kind = IK_Identifier;
711 Identifier = 0;
712 StartLocation = SourceLocation();
713 EndLocation = SourceLocation();
714}
715
716void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc,
717 OverloadedOperatorKind Op,
718 SourceLocation SymbolLocations[3]) {
719 Kind = IK_OperatorFunctionId;
720 StartLocation = OperatorLoc;
721 EndLocation = OperatorLoc;
722 OperatorFunctionId.Operator = Op;
723 for (unsigned I = 0; I != 3; ++I) {
724 OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding();
725
726 if (SymbolLocations[I].isValid())
727 EndLocation = SymbolLocations[I];
728 }
729}
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000730
Anders Carlssoncc54d592011-01-22 16:56:46 +0000731bool VirtSpecifiers::SetSpecifier(Specifier VS, SourceLocation Loc,
Anders Carlsson46127a92011-01-22 15:58:16 +0000732 const char *&PrevSpec) {
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000733 if (Specifiers & VS) {
734 PrevSpec = getSpecifierName(VS);
735 return true;
736 }
737
738 Specifiers |= VS;
739
740 switch (VS) {
741 default: assert(0 && "Unknown specifier!");
742 case VS_Override: VS_overrideLoc = Loc; break;
743 case VS_Final: VS_finalLoc = Loc; break;
744 case VS_New: VS_newLoc = Loc; break;
745 }
Anders Carlsson46127a92011-01-22 15:58:16 +0000746
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000747 return false;
748}
749
Anders Carlssoncc54d592011-01-22 16:56:46 +0000750const char *VirtSpecifiers::getSpecifierName(Specifier VS) {
Anders Carlssonc46bb7d2011-01-22 15:11:37 +0000751 switch (VS) {
752 default: assert(0 && "Unknown specifier");
753 case VS_Override: return "override";
754 case VS_Final: return "final";
755 case VS_New: return "new";
756 }
757}
Anders Carlsson46127a92011-01-22 15:58:16 +0000758
Anders Carlssoncc54d592011-01-22 16:56:46 +0000759bool ClassVirtSpecifiers::SetSpecifier(Specifier CVS, SourceLocation Loc,
Anders Carlsson46127a92011-01-22 15:58:16 +0000760 const char *&PrevSpec) {
761 if (Specifiers & CVS) {
762 PrevSpec = getSpecifierName(CVS);
763 return true;
764 }
765
766 Specifiers |= CVS;
767
768 switch (CVS) {
769 default: assert(0 && "Unknown specifier!");
770 case CVS_Final: CVS_finalLoc = Loc; break;
771 case CVS_Explicit: CVS_explicitLoc = Loc; break;
772 }
773
774 return false;
775}
776
Anders Carlssoncc54d592011-01-22 16:56:46 +0000777const char *ClassVirtSpecifiers::getSpecifierName(Specifier CVS) {
Anders Carlsson46127a92011-01-22 15:58:16 +0000778 switch (CVS) {
779 default: assert(0 && "Unknown specifier");
780 case CVS_Final: return "final";
781 case CVS_Explicit: return "explicit";
782 }
783}
784