blob: cad3b58d7646dd3be4d1a34e641f1f0e98fc9e62 [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 Gregorc34348a2011-02-24 17:54:50 +000017#include "clang/AST/ASTContext.h"
Douglas Gregor2e4c34a2011-02-24 00:17:56 +000018#include "clang/AST/NestedNameSpecifier.h"
19#include "clang/AST/TypeLoc.h"
Douglas Gregor9b3064b2009-04-01 22:41:11 +000020#include "clang/Lex/Preprocessor.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "clang/Basic/LangOptions.h"
Chris Lattner5af2f352009-01-20 19:11:22 +000022#include "llvm/ADT/STLExtras.h"
John McCall32d335e2009-08-03 18:47:27 +000023#include "llvm/Support/ErrorHandling.h"
Douglas Gregore4e5b052009-03-19 00:18:19 +000024#include <cstring>
Reid Spencer5f016e22007-07-11 17:01:13 +000025using namespace clang;
26
Chris Lattner254be6a2008-11-22 08:32:36 +000027
28static DiagnosticBuilder Diag(Diagnostic &D, SourceLocation Loc,
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000029 unsigned DiagID) {
30 return D.Report(Loc, DiagID);
Chris Lattner254be6a2008-11-22 08:32:36 +000031}
32
Douglas Gregor314b97f2009-11-10 19:49:08 +000033
34void UnqualifiedId::setTemplateId(TemplateIdAnnotation *TemplateId) {
35 assert(TemplateId && "NULL template-id annotation?");
36 Kind = IK_TemplateId;
37 this->TemplateId = TemplateId;
38 StartLocation = TemplateId->TemplateNameLoc;
39 EndLocation = TemplateId->RAngleLoc;
40}
41
Douglas Gregor0efc2c12010-01-13 17:31:36 +000042void UnqualifiedId::setConstructorTemplateId(TemplateIdAnnotation *TemplateId) {
43 assert(TemplateId && "NULL template-id annotation?");
44 Kind = IK_ConstructorTemplateId;
45 this->TemplateId = TemplateId;
46 StartLocation = TemplateId->TemplateNameLoc;
47 EndLocation = TemplateId->RAngleLoc;
48}
49
Douglas Gregor2e4c34a2011-02-24 00:17:56 +000050void CXXScopeSpec::Extend(ASTContext &Context, SourceLocation TemplateKWLoc,
51 TypeLoc TL, SourceLocation ColonColonLoc) {
Douglas Gregor5f791bb2011-02-28 23:58:31 +000052 Builder.Extend(Context, TemplateKWLoc, TL, ColonColonLoc);
Douglas Gregor2e4c34a2011-02-24 00:17:56 +000053 if (Range.getBegin().isInvalid())
54 Range.setBegin(TL.getBeginLoc());
55 Range.setEnd(ColonColonLoc);
Douglas Gregorc34348a2011-02-24 17:54:50 +000056
Douglas Gregor5f791bb2011-02-28 23:58:31 +000057 assert(Range == Builder.getSourceRange() &&
Douglas Gregorc34348a2011-02-24 17:54:50 +000058 "NestedNameSpecifierLoc range computation incorrect");
Douglas Gregor2e4c34a2011-02-24 00:17:56 +000059}
60
61void CXXScopeSpec::Extend(ASTContext &Context, IdentifierInfo *Identifier,
62 SourceLocation IdentifierLoc,
63 SourceLocation ColonColonLoc) {
Douglas Gregor5f791bb2011-02-28 23:58:31 +000064 Builder.Extend(Context, Identifier, IdentifierLoc, ColonColonLoc);
65
Douglas Gregor2e4c34a2011-02-24 00:17:56 +000066 if (Range.getBegin().isInvalid())
67 Range.setBegin(IdentifierLoc);
68 Range.setEnd(ColonColonLoc);
Douglas Gregorc34348a2011-02-24 17:54:50 +000069
Douglas Gregor5f791bb2011-02-28 23:58:31 +000070 assert(Range == Builder.getSourceRange() &&
Douglas Gregorc34348a2011-02-24 17:54:50 +000071 "NestedNameSpecifierLoc range computation incorrect");
Douglas Gregor2e4c34a2011-02-24 00:17:56 +000072}
73
74void CXXScopeSpec::Extend(ASTContext &Context, NamespaceDecl *Namespace,
75 SourceLocation NamespaceLoc,
76 SourceLocation ColonColonLoc) {
Douglas Gregor5f791bb2011-02-28 23:58:31 +000077 Builder.Extend(Context, Namespace, NamespaceLoc, ColonColonLoc);
78
Douglas Gregor2e4c34a2011-02-24 00:17:56 +000079 if (Range.getBegin().isInvalid())
80 Range.setBegin(NamespaceLoc);
81 Range.setEnd(ColonColonLoc);
Douglas Gregorc34348a2011-02-24 17:54:50 +000082
Douglas Gregor5f791bb2011-02-28 23:58:31 +000083 assert(Range == Builder.getSourceRange() &&
Douglas Gregorc34348a2011-02-24 17:54:50 +000084 "NestedNameSpecifierLoc range computation incorrect");
Douglas Gregor2e4c34a2011-02-24 00:17:56 +000085}
86
Douglas Gregor14aba762011-02-24 02:36:08 +000087void CXXScopeSpec::Extend(ASTContext &Context, NamespaceAliasDecl *Alias,
88 SourceLocation AliasLoc,
89 SourceLocation ColonColonLoc) {
Douglas Gregor5f791bb2011-02-28 23:58:31 +000090 Builder.Extend(Context, Alias, AliasLoc, ColonColonLoc);
91
Douglas Gregor14aba762011-02-24 02:36:08 +000092 if (Range.getBegin().isInvalid())
93 Range.setBegin(AliasLoc);
94 Range.setEnd(ColonColonLoc);
Douglas Gregorc34348a2011-02-24 17:54:50 +000095
Douglas Gregor5f791bb2011-02-28 23:58:31 +000096 assert(Range == Builder.getSourceRange() &&
Douglas Gregorc34348a2011-02-24 17:54:50 +000097 "NestedNameSpecifierLoc range computation incorrect");
Douglas Gregor14aba762011-02-24 02:36:08 +000098}
99
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000100void CXXScopeSpec::MakeGlobal(ASTContext &Context,
101 SourceLocation ColonColonLoc) {
Douglas Gregor5f791bb2011-02-28 23:58:31 +0000102 Builder.MakeGlobal(Context, ColonColonLoc);
103
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000104 Range = SourceRange(ColonColonLoc);
Douglas Gregorc34348a2011-02-24 17:54:50 +0000105
Douglas Gregor5f791bb2011-02-28 23:58:31 +0000106 assert(Range == Builder.getSourceRange() &&
Douglas Gregorc34348a2011-02-24 17:54:50 +0000107 "NestedNameSpecifierLoc range computation incorrect");
108}
109
110void CXXScopeSpec::MakeTrivial(ASTContext &Context,
111 NestedNameSpecifier *Qualifier, SourceRange R) {
Douglas Gregor5f791bb2011-02-28 23:58:31 +0000112 Builder.MakeTrivial(Context, Qualifier, R);
Douglas Gregorc34348a2011-02-24 17:54:50 +0000113 Range = R;
Douglas Gregorc34348a2011-02-24 17:54:50 +0000114}
115
116void CXXScopeSpec::Adopt(NestedNameSpecifierLoc Other) {
117 if (!Other) {
118 Range = SourceRange();
Douglas Gregor5f791bb2011-02-28 23:58:31 +0000119 Builder.Clear();
Douglas Gregorc34348a2011-02-24 17:54:50 +0000120 return;
121 }
Douglas Gregor5f791bb2011-02-28 23:58:31 +0000122
Douglas Gregorc34348a2011-02-24 17:54:50 +0000123 Range = Other.getSourceRange();
Douglas Gregor5f791bb2011-02-28 23:58:31 +0000124 Builder.Adopt(Other);
Douglas Gregorc34348a2011-02-24 17:54:50 +0000125}
126
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000127NestedNameSpecifierLoc
128CXXScopeSpec::getWithLocInContext(ASTContext &Context) const {
Douglas Gregorb46ae392011-03-03 21:48:55 +0000129 if (!Builder.getRepresentation())
Douglas Gregorc34348a2011-02-24 17:54:50 +0000130 return NestedNameSpecifierLoc();
131
Douglas Gregor5f791bb2011-02-28 23:58:31 +0000132 return Builder.getWithLocInContext(Context);
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000133}
134
Chris Lattner5af2f352009-01-20 19:11:22 +0000135/// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function.
136/// "TheDeclarator" is the declarator that this will be added to.
John McCall0b7e6782011-03-24 11:26:52 +0000137DeclaratorChunk DeclaratorChunk::getFunction(bool hasProto, bool isVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +0000138 SourceLocation EllipsisLoc,
Chris Lattner5af2f352009-01-20 19:11:22 +0000139 ParamInfo *ArgInfo,
140 unsigned NumArgs,
141 unsigned TypeQuals,
Douglas Gregor83f51722011-01-26 03:43:54 +0000142 bool RefQualifierIsLvalueRef,
143 SourceLocation RefQualifierLoc,
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000144 ExceptionSpecificationType
145 ESpecType,
146 SourceLocation ESpecLoc,
John McCallb3d87482010-08-24 05:47:05 +0000147 ParsedType *Exceptions,
Sebastian Redlef65f062009-05-29 18:02:33 +0000148 SourceRange *ExceptionRanges,
Sebastian Redl7dc81342009-04-29 17:30:04 +0000149 unsigned NumExceptions,
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000150 Expr *NoexceptExpr,
Abramo Bagnara796aa442011-03-12 11:17:06 +0000151 SourceLocation LocalRangeBegin,
152 SourceLocation LocalRangeEnd,
Douglas Gregordab60ad2010-10-01 18:44:50 +0000153 Declarator &TheDeclarator,
154 ParsedType TrailingReturnType) {
Chris Lattner5af2f352009-01-20 19:11:22 +0000155 DeclaratorChunk I;
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000156 I.Kind = Function;
Abramo Bagnara796aa442011-03-12 11:17:06 +0000157 I.Loc = LocalRangeBegin;
158 I.EndLoc = LocalRangeEnd;
John McCall0b7e6782011-03-24 11:26:52 +0000159 I.Fun.AttrList = 0;
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000160 I.Fun.hasPrototype = hasProto;
161 I.Fun.isVariadic = isVariadic;
162 I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding();
163 I.Fun.DeleteArgInfo = false;
164 I.Fun.TypeQuals = TypeQuals;
165 I.Fun.NumArgs = NumArgs;
166 I.Fun.ArgInfo = 0;
Douglas Gregor83f51722011-01-26 03:43:54 +0000167 I.Fun.RefQualifierIsLValueRef = RefQualifierIsLvalueRef;
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000168 I.Fun.RefQualifierLoc = RefQualifierLoc.getRawEncoding();
169 I.Fun.ExceptionSpecType = ESpecType;
170 I.Fun.ExceptionSpecLoc = ESpecLoc.getRawEncoding();
171 I.Fun.NumExceptions = 0;
172 I.Fun.Exceptions = 0;
173 I.Fun.NoexceptExpr = 0;
Douglas Gregordab60ad2010-10-01 18:44:50 +0000174 I.Fun.TrailingReturnType = TrailingReturnType.getAsOpaquePtr();
Sebastian Redl7dc81342009-04-29 17:30:04 +0000175
Chris Lattner5af2f352009-01-20 19:11:22 +0000176 // new[] an argument array if needed.
177 if (NumArgs) {
178 // If the 'InlineParams' in Declarator is unused and big enough, put our
179 // parameter list there (in an effort to avoid new/delete traffic). If it
180 // is already used (consider a function returning a function pointer) or too
181 // small (function taking too many arguments), go to the heap.
Mike Stump1eb44332009-09-09 15:08:12 +0000182 if (!TheDeclarator.InlineParamsUsed &&
Chris Lattner5af2f352009-01-20 19:11:22 +0000183 NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
184 I.Fun.ArgInfo = TheDeclarator.InlineParams;
185 I.Fun.DeleteArgInfo = false;
186 TheDeclarator.InlineParamsUsed = true;
187 } else {
188 I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs];
189 I.Fun.DeleteArgInfo = true;
190 }
191 memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs);
192 }
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000193
194 // Check what exception specification information we should actually store.
195 switch (ESpecType) {
196 default: break; // By default, save nothing.
197 case EST_Dynamic:
198 // new[] an exception array if needed
199 if (NumExceptions) {
200 I.Fun.NumExceptions = NumExceptions;
201 I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions];
202 for (unsigned i = 0; i != NumExceptions; ++i) {
203 I.Fun.Exceptions[i].Ty = Exceptions[i];
204 I.Fun.Exceptions[i].Range = ExceptionRanges[i];
205 }
Sebastian Redlef65f062009-05-29 18:02:33 +0000206 }
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000207 break;
208
209 case EST_ComputedNoexcept:
210 I.Fun.NoexceptExpr = NoexceptExpr;
211 break;
Sebastian Redl7dc81342009-04-29 17:30:04 +0000212 }
Chris Lattner5af2f352009-01-20 19:11:22 +0000213 return I;
214}
Chris Lattner254be6a2008-11-22 08:32:36 +0000215
Reid Spencer5f016e22007-07-11 17:01:13 +0000216/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
Chris Lattner2a327d12009-02-27 18:35:46 +0000217/// declaration specifier includes.
Reid Spencer5f016e22007-07-11 17:01:13 +0000218///
219unsigned DeclSpec::getParsedSpecifiers() const {
220 unsigned Res = 0;
221 if (StorageClassSpec != SCS_unspecified ||
222 SCS_thread_specified)
223 Res |= PQ_StorageClassSpecifier;
Mike Stumpd4204332008-06-19 19:52:46 +0000224
Reid Spencer5f016e22007-07-11 17:01:13 +0000225 if (TypeQualifiers != TQ_unspecified)
226 Res |= PQ_TypeQualifier;
Mike Stump1eb44332009-09-09 15:08:12 +0000227
Reid Spencer5f016e22007-07-11 17:01:13 +0000228 if (hasTypeSpecifier())
229 Res |= PQ_TypeSpecifier;
Mike Stump1eb44332009-09-09 15:08:12 +0000230
Douglas Gregorb48fe382008-10-31 09:07:45 +0000231 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
Reid Spencer5f016e22007-07-11 17:01:13 +0000232 Res |= PQ_FunctionSpecifier;
233 return Res;
234}
235
John McCallfec54012009-08-03 20:12:06 +0000236template <class T> static bool BadSpecifier(T TNew, T TPrev,
237 const char *&PrevSpec,
238 unsigned &DiagID) {
John McCall32d335e2009-08-03 18:47:27 +0000239 PrevSpec = DeclSpec::getSpecifierName(TPrev);
John McCallfec54012009-08-03 20:12:06 +0000240 DiagID = (TNew == TPrev ? diag::ext_duplicate_declspec
241 : diag::err_invalid_decl_spec_combination);
John McCall32d335e2009-08-03 18:47:27 +0000242 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000243}
John McCall32d335e2009-08-03 18:47:27 +0000244
Reid Spencer5f016e22007-07-11 17:01:13 +0000245const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
246 switch (S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000247 case DeclSpec::SCS_unspecified: return "unspecified";
248 case DeclSpec::SCS_typedef: return "typedef";
249 case DeclSpec::SCS_extern: return "extern";
250 case DeclSpec::SCS_static: return "static";
251 case DeclSpec::SCS_auto: return "auto";
252 case DeclSpec::SCS_register: return "register";
Eli Friedman63054b32009-04-19 20:27:55 +0000253 case DeclSpec::SCS_private_extern: return "__private_extern__";
Sebastian Redl669d5d72008-11-14 23:42:31 +0000254 case DeclSpec::SCS_mutable: return "mutable";
Reid Spencer5f016e22007-07-11 17:01:13 +0000255 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000256 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000257}
258
John McCall32d335e2009-08-03 18:47:27 +0000259const char *DeclSpec::getSpecifierName(TSW W) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000260 switch (W) {
John McCall32d335e2009-08-03 18:47:27 +0000261 case TSW_unspecified: return "unspecified";
262 case TSW_short: return "short";
263 case TSW_long: return "long";
264 case TSW_longlong: return "long long";
Reid Spencer5f016e22007-07-11 17:01:13 +0000265 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000266 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000267}
268
John McCall32d335e2009-08-03 18:47:27 +0000269const char *DeclSpec::getSpecifierName(TSC C) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000270 switch (C) {
John McCall32d335e2009-08-03 18:47:27 +0000271 case TSC_unspecified: return "unspecified";
272 case TSC_imaginary: return "imaginary";
273 case TSC_complex: return "complex";
Reid Spencer5f016e22007-07-11 17:01:13 +0000274 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000275 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000276}
277
278
John McCall32d335e2009-08-03 18:47:27 +0000279const char *DeclSpec::getSpecifierName(TSS S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000280 switch (S) {
John McCall32d335e2009-08-03 18:47:27 +0000281 case TSS_unspecified: return "unspecified";
282 case TSS_signed: return "signed";
283 case TSS_unsigned: return "unsigned";
Reid Spencer5f016e22007-07-11 17:01:13 +0000284 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000285 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000286}
287
288const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
289 switch (T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000290 case DeclSpec::TST_unspecified: return "unspecified";
291 case DeclSpec::TST_void: return "void";
292 case DeclSpec::TST_char: return "char";
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000293 case DeclSpec::TST_wchar: return "wchar_t";
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000294 case DeclSpec::TST_char16: return "char16_t";
295 case DeclSpec::TST_char32: return "char32_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000296 case DeclSpec::TST_int: return "int";
297 case DeclSpec::TST_float: return "float";
298 case DeclSpec::TST_double: return "double";
299 case DeclSpec::TST_bool: return "_Bool";
300 case DeclSpec::TST_decimal32: return "_Decimal32";
301 case DeclSpec::TST_decimal64: return "_Decimal64";
302 case DeclSpec::TST_decimal128: return "_Decimal128";
303 case DeclSpec::TST_enum: return "enum";
Chris Lattner99dc9142008-04-13 18:59:07 +0000304 case DeclSpec::TST_class: return "class";
Reid Spencer5f016e22007-07-11 17:01:13 +0000305 case DeclSpec::TST_union: return "union";
306 case DeclSpec::TST_struct: return "struct";
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000307 case DeclSpec::TST_typename: return "type-name";
Steve Naroffd1861fd2007-07-31 12:34:36 +0000308 case DeclSpec::TST_typeofType:
309 case DeclSpec::TST_typeofExpr: return "typeof";
John McCall32d335e2009-08-03 18:47:27 +0000310 case DeclSpec::TST_auto: return "auto";
311 case DeclSpec::TST_decltype: return "(decltype)";
312 case DeclSpec::TST_error: return "(error)";
Reid Spencer5f016e22007-07-11 17:01:13 +0000313 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000314 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000315}
316
John McCall32d335e2009-08-03 18:47:27 +0000317const char *DeclSpec::getSpecifierName(TQ T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000318 switch (T) {
John McCall32d335e2009-08-03 18:47:27 +0000319 case DeclSpec::TQ_unspecified: return "unspecified";
320 case DeclSpec::TQ_const: return "const";
321 case DeclSpec::TQ_restrict: return "restrict";
322 case DeclSpec::TQ_volatile: return "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +0000323 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000324 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000325}
326
327bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000328 const char *&PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +0000329 unsigned &DiagID,
330 const LangOptions &Lang) {
331 // OpenCL prohibits extern, auto, register, and static
332 // It seems sensible to prohibit private_extern too
333 if (Lang.OpenCL) {
334 switch (S) {
335 case SCS_extern:
336 case SCS_private_extern:
337 case SCS_auto:
338 case SCS_register:
339 case SCS_static:
340 DiagID = diag::err_not_opencl_storage_class_specifier;
341 PrevSpec = getSpecifierName(S);
342 return true;
343 default:
344 break;
345 }
346 }
347
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000348 if (StorageClassSpec != SCS_unspecified) {
349 // Changing storage class is allowed only if the previous one
350 // was the 'extern' that is part of a linkage specification and
351 // the new storage class is 'typedef'.
352 if (!(SCS_extern_in_linkage_spec &&
353 StorageClassSpec == SCS_extern &&
354 S == SCS_typedef))
355 return BadSpecifier(S, (SCS)StorageClassSpec, PrevSpec, DiagID);
356 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000357 StorageClassSpec = S;
358 StorageClassSpecLoc = Loc;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000359 assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield");
Reid Spencer5f016e22007-07-11 17:01:13 +0000360 return false;
361}
362
Mike Stump1eb44332009-09-09 15:08:12 +0000363bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000364 const char *&PrevSpec,
365 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000366 if (SCS_thread_specified) {
367 PrevSpec = "__thread";
John McCallfec54012009-08-03 20:12:06 +0000368 DiagID = diag::ext_duplicate_declspec;
Reid Spencer5f016e22007-07-11 17:01:13 +0000369 return true;
370 }
371 SCS_thread_specified = true;
372 SCS_threadLoc = Loc;
373 return false;
374}
375
Reid Spencer5f016e22007-07-11 17:01:13 +0000376/// These methods set the specified attribute of the DeclSpec, but return true
377/// and ignore the request if invalid (e.g. "extern" then "auto" is
378/// specified).
379bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000380 const char *&PrevSpec,
381 unsigned &DiagID) {
Abramo Bagnara2553eaf2011-03-06 22:21:56 +0000382 // Overwrite TSWLoc only if TypeSpecWidth was unspecified, so that
383 // for 'long long' we will keep the source location of the first 'long'.
384 if (TypeSpecWidth == TSW_unspecified)
385 TSWLoc = Loc;
386 // Allow turning long -> long long.
387 else if (W != TSW_longlong || TypeSpecWidth != TSW_long)
John McCallfec54012009-08-03 20:12:06 +0000388 return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000389 TypeSpecWidth = W;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000390 if (TypeAltiVecVector && !TypeAltiVecBool &&
391 ((TypeSpecWidth == TSW_long) || (TypeSpecWidth == TSW_longlong))) {
John Thompson82287d12010-02-05 00:12:22 +0000392 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
393 DiagID = diag::warn_vector_long_decl_spec_combination;
394 return true;
395 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000396 return false;
397}
398
Mike Stump1eb44332009-09-09 15:08:12 +0000399bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000400 const char *&PrevSpec,
401 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000402 if (TypeSpecComplex != TSC_unspecified)
John McCallfec54012009-08-03 20:12:06 +0000403 return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000404 TypeSpecComplex = C;
405 TSCLoc = Loc;
406 return false;
407}
408
Mike Stump1eb44332009-09-09 15:08:12 +0000409bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000410 const char *&PrevSpec,
411 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000412 if (TypeSpecSign != TSS_unspecified)
John McCallfec54012009-08-03 20:12:06 +0000413 return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000414 TypeSpecSign = S;
415 TSSLoc = Loc;
416 return false;
417}
418
419bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000420 const char *&PrevSpec,
421 unsigned &DiagID,
John McCallb3d87482010-08-24 05:47:05 +0000422 ParsedType Rep) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000423 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep);
424}
425
426bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
427 SourceLocation TagNameLoc,
428 const char *&PrevSpec,
429 unsigned &DiagID,
430 ParsedType Rep) {
John McCallb3d87482010-08-24 05:47:05 +0000431 assert(isTypeRep(T) && "T does not store a type");
432 assert(Rep && "no type provided!");
433 if (TypeSpecType != TST_unspecified) {
434 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
435 DiagID = diag::err_invalid_decl_spec_combination;
436 return true;
437 }
438 TypeSpecType = T;
439 TypeRep = Rep;
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000440 TSTLoc = TagKwLoc;
441 TSTNameLoc = TagNameLoc;
John McCallb3d87482010-08-24 05:47:05 +0000442 TypeSpecOwned = false;
443 return false;
444}
445
446bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
447 const char *&PrevSpec,
448 unsigned &DiagID,
449 Expr *Rep) {
450 assert(isExprRep(T) && "T does not store an expr");
451 assert(Rep && "no expression provided!");
452 if (TypeSpecType != TST_unspecified) {
453 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
454 DiagID = diag::err_invalid_decl_spec_combination;
455 return true;
456 }
457 TypeSpecType = T;
458 ExprRep = Rep;
459 TSTLoc = Loc;
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000460 TSTNameLoc = Loc;
John McCallb3d87482010-08-24 05:47:05 +0000461 TypeSpecOwned = false;
462 return false;
463}
464
465bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
466 const char *&PrevSpec,
467 unsigned &DiagID,
468 Decl *Rep, bool Owned) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000469 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep, Owned);
470}
471
472bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
473 SourceLocation TagNameLoc,
474 const char *&PrevSpec,
475 unsigned &DiagID,
476 Decl *Rep, bool Owned) {
John McCallb3d87482010-08-24 05:47:05 +0000477 assert(isDeclRep(T) && "T does not store a decl");
478 // Unlike the other cases, we don't assert that we actually get a decl.
479
480 if (TypeSpecType != TST_unspecified) {
481 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
482 DiagID = diag::err_invalid_decl_spec_combination;
483 return true;
484 }
485 TypeSpecType = T;
486 DeclRep = Rep;
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000487 TSTLoc = TagKwLoc;
488 TSTNameLoc = TagNameLoc;
John McCallb3d87482010-08-24 05:47:05 +0000489 TypeSpecOwned = Owned;
490 return false;
491}
492
493bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
494 const char *&PrevSpec,
495 unsigned &DiagID) {
496 assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) &&
497 "rep required for these type-spec kinds!");
John McCallfec54012009-08-03 20:12:06 +0000498 if (TypeSpecType != TST_unspecified) {
499 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
500 DiagID = diag::err_invalid_decl_spec_combination;
501 return true;
502 }
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000503 TSTLoc = Loc;
504 TSTNameLoc = Loc;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000505 if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) {
506 TypeAltiVecBool = true;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000507 return false;
508 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000509 TypeSpecType = T;
John McCallb3d87482010-08-24 05:47:05 +0000510 TypeSpecOwned = false;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000511 if (TypeAltiVecVector && !TypeAltiVecBool && (TypeSpecType == TST_double)) {
John Thompson82287d12010-02-05 00:12:22 +0000512 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
Chris Lattner788b0fd2010-06-23 06:00:24 +0000513 DiagID = diag::err_invalid_vector_decl_spec;
John Thompson82287d12010-02-05 00:12:22 +0000514 return true;
515 }
516 return false;
517}
518
519bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
520 const char *&PrevSpec, unsigned &DiagID) {
521 if (TypeSpecType != TST_unspecified) {
522 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
523 DiagID = diag::err_invalid_vector_decl_spec_combination;
524 return true;
525 }
526 TypeAltiVecVector = isAltiVecVector;
527 AltiVecLoc = Loc;
528 return false;
529}
530
531bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
532 const char *&PrevSpec, unsigned &DiagID) {
Chris Lattner788b0fd2010-06-23 06:00:24 +0000533 if (!TypeAltiVecVector || TypeAltiVecPixel ||
534 (TypeSpecType != TST_unspecified)) {
John Thompson82287d12010-02-05 00:12:22 +0000535 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
536 DiagID = diag::err_invalid_pixel_decl_spec_combination;
537 return true;
538 }
John Thompson82287d12010-02-05 00:12:22 +0000539 TypeAltiVecPixel = isAltiVecPixel;
540 TSTLoc = Loc;
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000541 TSTNameLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000542 return false;
543}
544
Douglas Gregorddc29e12009-02-06 22:42:48 +0000545bool DeclSpec::SetTypeSpecError() {
546 TypeSpecType = TST_error;
John McCall9e46b8c2010-08-26 17:22:34 +0000547 TypeSpecOwned = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000548 TSTLoc = SourceLocation();
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000549 TSTNameLoc = SourceLocation();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000550 return false;
551}
552
Reid Spencer5f016e22007-07-11 17:01:13 +0000553bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000554 unsigned &DiagID, const LangOptions &Lang) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000555 // Duplicates turn into warnings pre-C99.
556 if ((TypeQualifiers & T) && !Lang.C99)
John McCallfec54012009-08-03 20:12:06 +0000557 return BadSpecifier(T, T, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000558 TypeQualifiers |= T;
Mike Stump1eb44332009-09-09 15:08:12 +0000559
Reid Spencer5f016e22007-07-11 17:01:13 +0000560 switch (T) {
561 default: assert(0 && "Unknown type qualifier!");
562 case TQ_const: TQ_constLoc = Loc; break;
563 case TQ_restrict: TQ_restrictLoc = Loc; break;
564 case TQ_volatile: TQ_volatileLoc = Loc; break;
565 }
566 return false;
567}
568
John McCallfec54012009-08-03 20:12:06 +0000569bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
570 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000571 // 'inline inline' is ok.
572 FS_inline_specified = true;
573 FS_inlineLoc = Loc;
574 return false;
575}
576
John McCallfec54012009-08-03 20:12:06 +0000577bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec,
578 unsigned &DiagID) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000579 // 'virtual virtual' is ok.
580 FS_virtual_specified = true;
581 FS_virtualLoc = Loc;
582 return false;
583}
584
John McCallfec54012009-08-03 20:12:06 +0000585bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec,
586 unsigned &DiagID) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000587 // 'explicit explicit' is ok.
588 FS_explicit_specified = true;
589 FS_explicitLoc = Loc;
590 return false;
591}
592
John McCallfec54012009-08-03 20:12:06 +0000593bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
594 unsigned &DiagID) {
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000595 if (Friend_specified) {
596 PrevSpec = "friend";
John McCallfec54012009-08-03 20:12:06 +0000597 DiagID = diag::ext_duplicate_declspec;
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000598 return true;
599 }
John McCallfec54012009-08-03 20:12:06 +0000600
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000601 Friend_specified = true;
602 FriendLoc = Loc;
603 return false;
604}
Reid Spencer5f016e22007-07-11 17:01:13 +0000605
Sebastian Redl2ac67232009-11-05 15:47:02 +0000606bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
607 unsigned &DiagID) {
608 // 'constexpr constexpr' is ok.
609 Constexpr_specified = true;
610 ConstexprLoc = Loc;
611 return false;
612}
613
John McCalld226f652010-08-21 09:40:31 +0000614void DeclSpec::setProtocolQualifiers(Decl * const *Protos,
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000615 unsigned NP,
616 SourceLocation *ProtoLocs,
617 SourceLocation LAngleLoc) {
618 if (NP == 0) return;
John McCalld226f652010-08-21 09:40:31 +0000619 ProtocolQualifiers = new Decl*[NP];
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000620 ProtocolLocs = new SourceLocation[NP];
John McCalld226f652010-08-21 09:40:31 +0000621 memcpy((void*)ProtocolQualifiers, Protos, sizeof(Decl*)*NP);
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000622 memcpy(ProtocolLocs, ProtoLocs, sizeof(SourceLocation)*NP);
623 NumProtocolQualifiers = NP;
624 ProtocolLAngleLoc = LAngleLoc;
625}
626
Douglas Gregorddf889a2010-01-18 18:04:31 +0000627void DeclSpec::SaveWrittenBuiltinSpecs() {
628 writtenBS.Sign = getTypeSpecSign();
629 writtenBS.Width = getTypeSpecWidth();
630 writtenBS.Type = getTypeSpecType();
631 // Search the list of attributes for the presence of a mode attribute.
632 writtenBS.ModeAttr = false;
John McCall7f040a92010-12-24 02:08:15 +0000633 AttributeList* attrs = getAttributes().getList();
Douglas Gregorddf889a2010-01-18 18:04:31 +0000634 while (attrs) {
635 if (attrs->getKind() == AttributeList::AT_mode) {
636 writtenBS.ModeAttr = true;
637 break;
638 }
639 attrs = attrs->getNext();
640 }
641}
642
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000643void DeclSpec::SaveStorageSpecifierAsWritten() {
644 if (SCS_extern_in_linkage_spec && StorageClassSpec == SCS_extern)
645 // If 'extern' is part of a linkage specification,
646 // then it is not a storage class "as written".
647 StorageClassSpecAsWritten = SCS_unspecified;
648 else
649 StorageClassSpecAsWritten = StorageClassSpec;
650}
651
Reid Spencer5f016e22007-07-11 17:01:13 +0000652/// Finish - This does final analysis of the declspec, rejecting things like
653/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
654/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
655/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000656void DeclSpec::Finish(Diagnostic &D, Preprocessor &PP) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000657 // Before possibly changing their values, save specs as written.
658 SaveWrittenBuiltinSpecs();
Douglas Gregor16573fa2010-04-19 22:54:31 +0000659 SaveStorageSpecifierAsWritten();
Douglas Gregorddf889a2010-01-18 18:04:31 +0000660
Reid Spencer5f016e22007-07-11 17:01:13 +0000661 // Check the type specifier components first.
662
Chris Lattner788b0fd2010-06-23 06:00:24 +0000663 // Validate and finalize AltiVec vector declspec.
664 if (TypeAltiVecVector) {
665 if (TypeAltiVecBool) {
666 // Sign specifiers are not allowed with vector bool. (PIM 2.1)
667 if (TypeSpecSign != TSS_unspecified) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000668 Diag(D, TSSLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner788b0fd2010-06-23 06:00:24 +0000669 << getSpecifierName((TSS)TypeSpecSign);
670 }
671
672 // Only char/int are valid with vector bool. (PIM 2.1)
Duncan Sands2e964a922010-06-23 19:34:52 +0000673 if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) &&
674 (TypeSpecType != TST_int)) || TypeAltiVecPixel) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000675 Diag(D, TSTLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner788b0fd2010-06-23 06:00:24 +0000676 << (TypeAltiVecPixel ? "__pixel" :
677 getSpecifierName((TST)TypeSpecType));
678 }
679
680 // Only 'short' is valid with vector bool. (PIM 2.1)
681 if ((TypeSpecWidth != TSW_unspecified) && (TypeSpecWidth != TSW_short))
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000682 Diag(D, TSWLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner788b0fd2010-06-23 06:00:24 +0000683 << getSpecifierName((TSW)TypeSpecWidth);
684
685 // Elements of vector bool are interpreted as unsigned. (PIM 2.1)
686 if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) ||
687 (TypeSpecWidth != TSW_unspecified))
688 TypeSpecSign = TSS_unsigned;
689 }
690
691 if (TypeAltiVecPixel) {
692 //TODO: perform validation
693 TypeSpecType = TST_int;
694 TypeSpecSign = TSS_unsigned;
695 TypeSpecWidth = TSW_short;
John McCall9e46b8c2010-08-26 17:22:34 +0000696 TypeSpecOwned = false;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000697 }
698 }
699
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000700 // signed/unsigned are only valid with int/char/wchar_t.
Reid Spencer5f016e22007-07-11 17:01:13 +0000701 if (TypeSpecSign != TSS_unspecified) {
702 if (TypeSpecType == TST_unspecified)
703 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000704 else if (TypeSpecType != TST_int &&
705 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000706 Diag(D, TSSLoc, diag::err_invalid_sign_spec)
Chris Lattner254be6a2008-11-22 08:32:36 +0000707 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000708 // signed double -> double.
709 TypeSpecSign = TSS_unspecified;
710 }
711 }
712
713 // Validate the width of the type.
714 switch (TypeSpecWidth) {
715 case TSW_unspecified: break;
716 case TSW_short: // short int
717 case TSW_longlong: // long long int
718 if (TypeSpecType == TST_unspecified)
719 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
720 else if (TypeSpecType != TST_int) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000721 Diag(D, TSWLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000722 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
Chris Lattner254be6a2008-11-22 08:32:36 +0000723 : diag::err_invalid_longlong_spec)
724 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000725 TypeSpecType = TST_int;
John McCall9e46b8c2010-08-26 17:22:34 +0000726 TypeSpecOwned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000727 }
728 break;
729 case TSW_long: // long double, long int
730 if (TypeSpecType == TST_unspecified)
731 TypeSpecType = TST_int; // long -> long int.
732 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000733 Diag(D, TSWLoc, diag::err_invalid_long_spec)
Chris Lattner254be6a2008-11-22 08:32:36 +0000734 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000735 TypeSpecType = TST_int;
John McCall9e46b8c2010-08-26 17:22:34 +0000736 TypeSpecOwned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000737 }
738 break;
739 }
Mike Stump1eb44332009-09-09 15:08:12 +0000740
Reid Spencer5f016e22007-07-11 17:01:13 +0000741 // TODO: if the implementation does not implement _Complex or _Imaginary,
742 // disallow their use. Need information about the backend.
743 if (TypeSpecComplex != TSC_unspecified) {
744 if (TypeSpecType == TST_unspecified) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000745 Diag(D, TSCLoc, diag::ext_plain_complex)
Douglas Gregor849b2432010-03-31 17:46:05 +0000746 << FixItHint::CreateInsertion(
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000747 PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
748 " double");
Reid Spencer5f016e22007-07-11 17:01:13 +0000749 TypeSpecType = TST_double; // _Complex -> _Complex double.
750 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
751 // Note that this intentionally doesn't include _Complex _Bool.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000752 Diag(D, TSTLoc, diag::ext_integer_complex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000753 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000754 Diag(D, TSCLoc, diag::err_invalid_complex_spec)
Chris Lattner254be6a2008-11-22 08:32:36 +0000755 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000756 TypeSpecComplex = TSC_unspecified;
757 }
758 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000759
John McCall67d1a672009-08-06 02:15:43 +0000760 // C++ [class.friend]p6:
761 // No storage-class-specifier shall appear in the decl-specifier-seq
762 // of a friend declaration.
763 if (isFriendSpecified() && getStorageClassSpec()) {
764 DeclSpec::SCS SC = getStorageClassSpec();
765 const char *SpecName = getSpecifierName(SC);
766
767 SourceLocation SCLoc = getStorageClassSpecLoc();
768 SourceLocation SCEndLoc = SCLoc.getFileLocWithOffset(strlen(SpecName));
769
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000770 Diag(D, SCLoc, diag::err_friend_storage_spec)
John McCall67d1a672009-08-06 02:15:43 +0000771 << SpecName
Douglas Gregor849b2432010-03-31 17:46:05 +0000772 << FixItHint::CreateRemoval(SourceRange(SCLoc, SCEndLoc));
John McCall67d1a672009-08-06 02:15:43 +0000773
774 ClearStorageClassSpecs();
775 }
776
John McCall9e46b8c2010-08-26 17:22:34 +0000777 assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType));
778
Reid Spencer5f016e22007-07-11 17:01:13 +0000779 // Okay, now we can infer the real type.
Mike Stump1eb44332009-09-09 15:08:12 +0000780
Reid Spencer5f016e22007-07-11 17:01:13 +0000781 // TODO: return "auto function" and other bad things based on the real type.
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Reid Spencer5f016e22007-07-11 17:01:13 +0000783 // 'data definition has no type or storage class'?
784}
Daniel Dunbare4858a62008-08-11 03:45:03 +0000785
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000786bool DeclSpec::isMissingDeclaratorOk() {
787 TST tst = getTypeSpecType();
John McCallb3d87482010-08-24 05:47:05 +0000788 return isDeclRep(tst) && getRepAsDecl() != 0 &&
789 StorageClassSpec != DeclSpec::SCS_typedef;
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000790}
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000791
792void UnqualifiedId::clear() {
793 if (Kind == IK_TemplateId)
794 TemplateId->Destroy();
795
796 Kind = IK_Identifier;
797 Identifier = 0;
798 StartLocation = SourceLocation();
799 EndLocation = SourceLocation();
800}
801
802void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc,
803 OverloadedOperatorKind Op,
804 SourceLocation SymbolLocations[3]) {
805 Kind = IK_OperatorFunctionId;
806 StartLocation = OperatorLoc;
807 EndLocation = OperatorLoc;
808 OperatorFunctionId.Operator = Op;
809 for (unsigned I = 0; I != 3; ++I) {
810 OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding();
811
812 if (SymbolLocations[I].isValid())
813 EndLocation = SymbolLocations[I];
814 }
815}
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000816
Anders Carlssoncc54d592011-01-22 16:56:46 +0000817bool VirtSpecifiers::SetSpecifier(Specifier VS, SourceLocation Loc,
Anders Carlsson46127a92011-01-22 15:58:16 +0000818 const char *&PrevSpec) {
Douglas Gregorf5251602011-03-08 17:10:18 +0000819 LastLocation = Loc;
820
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000821 if (Specifiers & VS) {
822 PrevSpec = getSpecifierName(VS);
823 return true;
824 }
825
826 Specifiers |= VS;
827
828 switch (VS) {
829 default: assert(0 && "Unknown specifier!");
830 case VS_Override: VS_overrideLoc = Loc; break;
831 case VS_Final: VS_finalLoc = Loc; break;
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000832 }
Anders Carlsson46127a92011-01-22 15:58:16 +0000833
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000834 return false;
835}
836
Anders Carlssoncc54d592011-01-22 16:56:46 +0000837const char *VirtSpecifiers::getSpecifierName(Specifier VS) {
Anders Carlssonc46bb7d2011-01-22 15:11:37 +0000838 switch (VS) {
839 default: assert(0 && "Unknown specifier");
840 case VS_Override: return "override";
841 case VS_Final: return "final";
Anders Carlssonc46bb7d2011-01-22 15:11:37 +0000842 }
843}