blob: 5be16e7431c1365e269b013bf477ee3343b01b0f [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)";
Sean Huntca63c202011-05-24 22:41:36 +0000312 case DeclSpec::TST_underlyingType: return "__underlying_type";
John McCalla5fc4722011-04-09 22:50:59 +0000313 case DeclSpec::TST_unknown_anytype: return "__unknown_anytype";
John McCall32d335e2009-08-03 18:47:27 +0000314 case DeclSpec::TST_error: return "(error)";
Reid Spencer5f016e22007-07-11 17:01:13 +0000315 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000316 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000317}
318
John McCall32d335e2009-08-03 18:47:27 +0000319const char *DeclSpec::getSpecifierName(TQ T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000320 switch (T) {
John McCall32d335e2009-08-03 18:47:27 +0000321 case DeclSpec::TQ_unspecified: return "unspecified";
322 case DeclSpec::TQ_const: return "const";
323 case DeclSpec::TQ_restrict: return "restrict";
324 case DeclSpec::TQ_volatile: return "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +0000325 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000326 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000327}
328
329bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000330 const char *&PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +0000331 unsigned &DiagID,
332 const LangOptions &Lang) {
333 // OpenCL prohibits extern, auto, register, and static
334 // It seems sensible to prohibit private_extern too
335 if (Lang.OpenCL) {
336 switch (S) {
337 case SCS_extern:
338 case SCS_private_extern:
339 case SCS_auto:
340 case SCS_register:
341 case SCS_static:
342 DiagID = diag::err_not_opencl_storage_class_specifier;
343 PrevSpec = getSpecifierName(S);
344 return true;
345 default:
346 break;
347 }
348 }
349
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000350 if (StorageClassSpec != SCS_unspecified) {
351 // Changing storage class is allowed only if the previous one
352 // was the 'extern' that is part of a linkage specification and
353 // the new storage class is 'typedef'.
354 if (!(SCS_extern_in_linkage_spec &&
355 StorageClassSpec == SCS_extern &&
356 S == SCS_typedef))
357 return BadSpecifier(S, (SCS)StorageClassSpec, PrevSpec, DiagID);
358 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000359 StorageClassSpec = S;
360 StorageClassSpecLoc = Loc;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000361 assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield");
Reid Spencer5f016e22007-07-11 17:01:13 +0000362 return false;
363}
364
Mike Stump1eb44332009-09-09 15:08:12 +0000365bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000366 const char *&PrevSpec,
367 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000368 if (SCS_thread_specified) {
369 PrevSpec = "__thread";
John McCallfec54012009-08-03 20:12:06 +0000370 DiagID = diag::ext_duplicate_declspec;
Reid Spencer5f016e22007-07-11 17:01:13 +0000371 return true;
372 }
373 SCS_thread_specified = true;
374 SCS_threadLoc = Loc;
375 return false;
376}
377
Reid Spencer5f016e22007-07-11 17:01:13 +0000378/// These methods set the specified attribute of the DeclSpec, but return true
379/// and ignore the request if invalid (e.g. "extern" then "auto" is
380/// specified).
381bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000382 const char *&PrevSpec,
383 unsigned &DiagID) {
Abramo Bagnara2553eaf2011-03-06 22:21:56 +0000384 // Overwrite TSWLoc only if TypeSpecWidth was unspecified, so that
385 // for 'long long' we will keep the source location of the first 'long'.
386 if (TypeSpecWidth == TSW_unspecified)
387 TSWLoc = Loc;
388 // Allow turning long -> long long.
389 else if (W != TSW_longlong || TypeSpecWidth != TSW_long)
John McCallfec54012009-08-03 20:12:06 +0000390 return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000391 TypeSpecWidth = W;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000392 if (TypeAltiVecVector && !TypeAltiVecBool &&
393 ((TypeSpecWidth == TSW_long) || (TypeSpecWidth == TSW_longlong))) {
John Thompson82287d12010-02-05 00:12:22 +0000394 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
395 DiagID = diag::warn_vector_long_decl_spec_combination;
396 return true;
397 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000398 return false;
399}
400
Mike Stump1eb44332009-09-09 15:08:12 +0000401bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000402 const char *&PrevSpec,
403 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000404 if (TypeSpecComplex != TSC_unspecified)
John McCallfec54012009-08-03 20:12:06 +0000405 return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000406 TypeSpecComplex = C;
407 TSCLoc = Loc;
408 return false;
409}
410
Mike Stump1eb44332009-09-09 15:08:12 +0000411bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000412 const char *&PrevSpec,
413 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000414 if (TypeSpecSign != TSS_unspecified)
John McCallfec54012009-08-03 20:12:06 +0000415 return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000416 TypeSpecSign = S;
417 TSSLoc = Loc;
418 return false;
419}
420
421bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000422 const char *&PrevSpec,
423 unsigned &DiagID,
John McCallb3d87482010-08-24 05:47:05 +0000424 ParsedType Rep) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000425 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep);
426}
427
428bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
429 SourceLocation TagNameLoc,
430 const char *&PrevSpec,
431 unsigned &DiagID,
432 ParsedType Rep) {
John McCallb3d87482010-08-24 05:47:05 +0000433 assert(isTypeRep(T) && "T does not store a type");
434 assert(Rep && "no type provided!");
435 if (TypeSpecType != TST_unspecified) {
436 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
437 DiagID = diag::err_invalid_decl_spec_combination;
438 return true;
439 }
440 TypeSpecType = T;
441 TypeRep = Rep;
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000442 TSTLoc = TagKwLoc;
443 TSTNameLoc = TagNameLoc;
John McCallb3d87482010-08-24 05:47:05 +0000444 TypeSpecOwned = false;
445 return false;
446}
447
448bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
449 const char *&PrevSpec,
450 unsigned &DiagID,
451 Expr *Rep) {
452 assert(isExprRep(T) && "T does not store an expr");
453 assert(Rep && "no expression provided!");
454 if (TypeSpecType != TST_unspecified) {
455 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
456 DiagID = diag::err_invalid_decl_spec_combination;
457 return true;
458 }
459 TypeSpecType = T;
460 ExprRep = Rep;
461 TSTLoc = Loc;
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000462 TSTNameLoc = Loc;
John McCallb3d87482010-08-24 05:47:05 +0000463 TypeSpecOwned = false;
464 return false;
465}
466
467bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
468 const char *&PrevSpec,
469 unsigned &DiagID,
470 Decl *Rep, bool Owned) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000471 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep, Owned);
472}
473
474bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
475 SourceLocation TagNameLoc,
476 const char *&PrevSpec,
477 unsigned &DiagID,
478 Decl *Rep, bool Owned) {
John McCallb3d87482010-08-24 05:47:05 +0000479 assert(isDeclRep(T) && "T does not store a decl");
480 // Unlike the other cases, we don't assert that we actually get a decl.
481
482 if (TypeSpecType != TST_unspecified) {
483 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
484 DiagID = diag::err_invalid_decl_spec_combination;
485 return true;
486 }
487 TypeSpecType = T;
488 DeclRep = Rep;
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000489 TSTLoc = TagKwLoc;
490 TSTNameLoc = TagNameLoc;
John McCallb3d87482010-08-24 05:47:05 +0000491 TypeSpecOwned = Owned;
492 return false;
493}
494
495bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
496 const char *&PrevSpec,
497 unsigned &DiagID) {
498 assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) &&
499 "rep required for these type-spec kinds!");
John McCallfec54012009-08-03 20:12:06 +0000500 if (TypeSpecType != TST_unspecified) {
501 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
502 DiagID = diag::err_invalid_decl_spec_combination;
503 return true;
504 }
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000505 TSTLoc = Loc;
506 TSTNameLoc = Loc;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000507 if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) {
508 TypeAltiVecBool = true;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000509 return false;
510 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000511 TypeSpecType = T;
John McCallb3d87482010-08-24 05:47:05 +0000512 TypeSpecOwned = false;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000513 if (TypeAltiVecVector && !TypeAltiVecBool && (TypeSpecType == TST_double)) {
John Thompson82287d12010-02-05 00:12:22 +0000514 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
Chris Lattner788b0fd2010-06-23 06:00:24 +0000515 DiagID = diag::err_invalid_vector_decl_spec;
John Thompson82287d12010-02-05 00:12:22 +0000516 return true;
517 }
518 return false;
519}
520
521bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
522 const char *&PrevSpec, unsigned &DiagID) {
523 if (TypeSpecType != TST_unspecified) {
524 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
525 DiagID = diag::err_invalid_vector_decl_spec_combination;
526 return true;
527 }
528 TypeAltiVecVector = isAltiVecVector;
529 AltiVecLoc = Loc;
530 return false;
531}
532
533bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
534 const char *&PrevSpec, unsigned &DiagID) {
Chris Lattner788b0fd2010-06-23 06:00:24 +0000535 if (!TypeAltiVecVector || TypeAltiVecPixel ||
536 (TypeSpecType != TST_unspecified)) {
John Thompson82287d12010-02-05 00:12:22 +0000537 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
538 DiagID = diag::err_invalid_pixel_decl_spec_combination;
539 return true;
540 }
John Thompson82287d12010-02-05 00:12:22 +0000541 TypeAltiVecPixel = isAltiVecPixel;
542 TSTLoc = Loc;
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000543 TSTNameLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000544 return false;
545}
546
Douglas Gregorddc29e12009-02-06 22:42:48 +0000547bool DeclSpec::SetTypeSpecError() {
548 TypeSpecType = TST_error;
John McCall9e46b8c2010-08-26 17:22:34 +0000549 TypeSpecOwned = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000550 TSTLoc = SourceLocation();
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000551 TSTNameLoc = SourceLocation();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000552 return false;
553}
554
Reid Spencer5f016e22007-07-11 17:01:13 +0000555bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000556 unsigned &DiagID, const LangOptions &Lang) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000557 // Duplicates turn into warnings pre-C99.
558 if ((TypeQualifiers & T) && !Lang.C99)
John McCallfec54012009-08-03 20:12:06 +0000559 return BadSpecifier(T, T, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000560 TypeQualifiers |= T;
Mike Stump1eb44332009-09-09 15:08:12 +0000561
Reid Spencer5f016e22007-07-11 17:01:13 +0000562 switch (T) {
563 default: assert(0 && "Unknown type qualifier!");
564 case TQ_const: TQ_constLoc = Loc; break;
565 case TQ_restrict: TQ_restrictLoc = Loc; break;
566 case TQ_volatile: TQ_volatileLoc = Loc; break;
567 }
568 return false;
569}
570
John McCallfec54012009-08-03 20:12:06 +0000571bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
572 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000573 // 'inline inline' is ok.
574 FS_inline_specified = true;
575 FS_inlineLoc = Loc;
576 return false;
577}
578
John McCallfec54012009-08-03 20:12:06 +0000579bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec,
580 unsigned &DiagID) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000581 // 'virtual virtual' is ok.
582 FS_virtual_specified = true;
583 FS_virtualLoc = Loc;
584 return false;
585}
586
John McCallfec54012009-08-03 20:12:06 +0000587bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec,
588 unsigned &DiagID) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000589 // 'explicit explicit' is ok.
590 FS_explicit_specified = true;
591 FS_explicitLoc = Loc;
592 return false;
593}
594
John McCallfec54012009-08-03 20:12:06 +0000595bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
596 unsigned &DiagID) {
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000597 if (Friend_specified) {
598 PrevSpec = "friend";
John McCallfec54012009-08-03 20:12:06 +0000599 DiagID = diag::ext_duplicate_declspec;
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000600 return true;
601 }
John McCallfec54012009-08-03 20:12:06 +0000602
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000603 Friend_specified = true;
604 FriendLoc = Loc;
605 return false;
606}
Reid Spencer5f016e22007-07-11 17:01:13 +0000607
Sebastian Redl2ac67232009-11-05 15:47:02 +0000608bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
609 unsigned &DiagID) {
610 // 'constexpr constexpr' is ok.
611 Constexpr_specified = true;
612 ConstexprLoc = Loc;
613 return false;
614}
615
John McCalld226f652010-08-21 09:40:31 +0000616void DeclSpec::setProtocolQualifiers(Decl * const *Protos,
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000617 unsigned NP,
618 SourceLocation *ProtoLocs,
619 SourceLocation LAngleLoc) {
620 if (NP == 0) return;
John McCalld226f652010-08-21 09:40:31 +0000621 ProtocolQualifiers = new Decl*[NP];
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000622 ProtocolLocs = new SourceLocation[NP];
John McCalld226f652010-08-21 09:40:31 +0000623 memcpy((void*)ProtocolQualifiers, Protos, sizeof(Decl*)*NP);
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000624 memcpy(ProtocolLocs, ProtoLocs, sizeof(SourceLocation)*NP);
625 NumProtocolQualifiers = NP;
626 ProtocolLAngleLoc = LAngleLoc;
627}
628
Douglas Gregorddf889a2010-01-18 18:04:31 +0000629void DeclSpec::SaveWrittenBuiltinSpecs() {
630 writtenBS.Sign = getTypeSpecSign();
631 writtenBS.Width = getTypeSpecWidth();
632 writtenBS.Type = getTypeSpecType();
633 // Search the list of attributes for the presence of a mode attribute.
634 writtenBS.ModeAttr = false;
John McCall7f040a92010-12-24 02:08:15 +0000635 AttributeList* attrs = getAttributes().getList();
Douglas Gregorddf889a2010-01-18 18:04:31 +0000636 while (attrs) {
637 if (attrs->getKind() == AttributeList::AT_mode) {
638 writtenBS.ModeAttr = true;
639 break;
640 }
641 attrs = attrs->getNext();
642 }
643}
644
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000645void DeclSpec::SaveStorageSpecifierAsWritten() {
646 if (SCS_extern_in_linkage_spec && StorageClassSpec == SCS_extern)
647 // If 'extern' is part of a linkage specification,
648 // then it is not a storage class "as written".
649 StorageClassSpecAsWritten = SCS_unspecified;
650 else
651 StorageClassSpecAsWritten = StorageClassSpec;
652}
653
Reid Spencer5f016e22007-07-11 17:01:13 +0000654/// Finish - This does final analysis of the declspec, rejecting things like
655/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
656/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
657/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000658void DeclSpec::Finish(Diagnostic &D, Preprocessor &PP) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000659 // Before possibly changing their values, save specs as written.
660 SaveWrittenBuiltinSpecs();
Douglas Gregor16573fa2010-04-19 22:54:31 +0000661 SaveStorageSpecifierAsWritten();
Douglas Gregorddf889a2010-01-18 18:04:31 +0000662
Reid Spencer5f016e22007-07-11 17:01:13 +0000663 // Check the type specifier components first.
664
Chris Lattner788b0fd2010-06-23 06:00:24 +0000665 // Validate and finalize AltiVec vector declspec.
666 if (TypeAltiVecVector) {
667 if (TypeAltiVecBool) {
668 // Sign specifiers are not allowed with vector bool. (PIM 2.1)
669 if (TypeSpecSign != TSS_unspecified) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000670 Diag(D, TSSLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner788b0fd2010-06-23 06:00:24 +0000671 << getSpecifierName((TSS)TypeSpecSign);
672 }
673
674 // Only char/int are valid with vector bool. (PIM 2.1)
Duncan Sands2e964a922010-06-23 19:34:52 +0000675 if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) &&
676 (TypeSpecType != TST_int)) || TypeAltiVecPixel) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000677 Diag(D, TSTLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner788b0fd2010-06-23 06:00:24 +0000678 << (TypeAltiVecPixel ? "__pixel" :
679 getSpecifierName((TST)TypeSpecType));
680 }
681
682 // Only 'short' is valid with vector bool. (PIM 2.1)
683 if ((TypeSpecWidth != TSW_unspecified) && (TypeSpecWidth != TSW_short))
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000684 Diag(D, TSWLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner788b0fd2010-06-23 06:00:24 +0000685 << getSpecifierName((TSW)TypeSpecWidth);
686
687 // Elements of vector bool are interpreted as unsigned. (PIM 2.1)
688 if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) ||
689 (TypeSpecWidth != TSW_unspecified))
690 TypeSpecSign = TSS_unsigned;
691 }
692
693 if (TypeAltiVecPixel) {
694 //TODO: perform validation
695 TypeSpecType = TST_int;
696 TypeSpecSign = TSS_unsigned;
697 TypeSpecWidth = TSW_short;
John McCall9e46b8c2010-08-26 17:22:34 +0000698 TypeSpecOwned = false;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000699 }
700 }
701
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000702 // signed/unsigned are only valid with int/char/wchar_t.
Reid Spencer5f016e22007-07-11 17:01:13 +0000703 if (TypeSpecSign != TSS_unspecified) {
704 if (TypeSpecType == TST_unspecified)
705 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000706 else if (TypeSpecType != TST_int &&
707 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000708 Diag(D, TSSLoc, diag::err_invalid_sign_spec)
Chris Lattner254be6a2008-11-22 08:32:36 +0000709 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000710 // signed double -> double.
711 TypeSpecSign = TSS_unspecified;
712 }
713 }
714
715 // Validate the width of the type.
716 switch (TypeSpecWidth) {
717 case TSW_unspecified: break;
718 case TSW_short: // short int
719 case TSW_longlong: // long long int
720 if (TypeSpecType == TST_unspecified)
721 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
722 else if (TypeSpecType != TST_int) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000723 Diag(D, TSWLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000724 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
Chris Lattner254be6a2008-11-22 08:32:36 +0000725 : diag::err_invalid_longlong_spec)
726 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000727 TypeSpecType = TST_int;
John McCall9e46b8c2010-08-26 17:22:34 +0000728 TypeSpecOwned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000729 }
730 break;
731 case TSW_long: // long double, long int
732 if (TypeSpecType == TST_unspecified)
733 TypeSpecType = TST_int; // long -> long int.
734 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000735 Diag(D, TSWLoc, diag::err_invalid_long_spec)
Chris Lattner254be6a2008-11-22 08:32:36 +0000736 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000737 TypeSpecType = TST_int;
John McCall9e46b8c2010-08-26 17:22:34 +0000738 TypeSpecOwned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000739 }
740 break;
741 }
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Reid Spencer5f016e22007-07-11 17:01:13 +0000743 // TODO: if the implementation does not implement _Complex or _Imaginary,
744 // disallow their use. Need information about the backend.
745 if (TypeSpecComplex != TSC_unspecified) {
746 if (TypeSpecType == TST_unspecified) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000747 Diag(D, TSCLoc, diag::ext_plain_complex)
Douglas Gregor849b2432010-03-31 17:46:05 +0000748 << FixItHint::CreateInsertion(
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000749 PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
750 " double");
Reid Spencer5f016e22007-07-11 17:01:13 +0000751 TypeSpecType = TST_double; // _Complex -> _Complex double.
752 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
753 // Note that this intentionally doesn't include _Complex _Bool.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000754 Diag(D, TSTLoc, diag::ext_integer_complex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000755 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000756 Diag(D, TSCLoc, diag::err_invalid_complex_spec)
Chris Lattner254be6a2008-11-22 08:32:36 +0000757 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000758 TypeSpecComplex = TSC_unspecified;
759 }
760 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000761
John McCall67d1a672009-08-06 02:15:43 +0000762 // C++ [class.friend]p6:
763 // No storage-class-specifier shall appear in the decl-specifier-seq
764 // of a friend declaration.
765 if (isFriendSpecified() && getStorageClassSpec()) {
766 DeclSpec::SCS SC = getStorageClassSpec();
767 const char *SpecName = getSpecifierName(SC);
768
769 SourceLocation SCLoc = getStorageClassSpecLoc();
770 SourceLocation SCEndLoc = SCLoc.getFileLocWithOffset(strlen(SpecName));
771
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000772 Diag(D, SCLoc, diag::err_friend_storage_spec)
John McCall67d1a672009-08-06 02:15:43 +0000773 << SpecName
Douglas Gregor849b2432010-03-31 17:46:05 +0000774 << FixItHint::CreateRemoval(SourceRange(SCLoc, SCEndLoc));
John McCall67d1a672009-08-06 02:15:43 +0000775
776 ClearStorageClassSpecs();
777 }
778
John McCall9e46b8c2010-08-26 17:22:34 +0000779 assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType));
780
Reid Spencer5f016e22007-07-11 17:01:13 +0000781 // Okay, now we can infer the real type.
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Reid Spencer5f016e22007-07-11 17:01:13 +0000783 // TODO: return "auto function" and other bad things based on the real type.
Mike Stump1eb44332009-09-09 15:08:12 +0000784
Reid Spencer5f016e22007-07-11 17:01:13 +0000785 // 'data definition has no type or storage class'?
786}
Daniel Dunbare4858a62008-08-11 03:45:03 +0000787
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000788bool DeclSpec::isMissingDeclaratorOk() {
789 TST tst = getTypeSpecType();
John McCallb3d87482010-08-24 05:47:05 +0000790 return isDeclRep(tst) && getRepAsDecl() != 0 &&
791 StorageClassSpec != DeclSpec::SCS_typedef;
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000792}
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000793
794void UnqualifiedId::clear() {
795 if (Kind == IK_TemplateId)
796 TemplateId->Destroy();
797
798 Kind = IK_Identifier;
799 Identifier = 0;
800 StartLocation = SourceLocation();
801 EndLocation = SourceLocation();
802}
803
804void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc,
805 OverloadedOperatorKind Op,
806 SourceLocation SymbolLocations[3]) {
807 Kind = IK_OperatorFunctionId;
808 StartLocation = OperatorLoc;
809 EndLocation = OperatorLoc;
810 OperatorFunctionId.Operator = Op;
811 for (unsigned I = 0; I != 3; ++I) {
812 OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding();
813
814 if (SymbolLocations[I].isValid())
815 EndLocation = SymbolLocations[I];
816 }
817}
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000818
Anders Carlssoncc54d592011-01-22 16:56:46 +0000819bool VirtSpecifiers::SetSpecifier(Specifier VS, SourceLocation Loc,
Anders Carlsson46127a92011-01-22 15:58:16 +0000820 const char *&PrevSpec) {
Douglas Gregorf5251602011-03-08 17:10:18 +0000821 LastLocation = Loc;
822
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000823 if (Specifiers & VS) {
824 PrevSpec = getSpecifierName(VS);
825 return true;
826 }
827
828 Specifiers |= VS;
829
830 switch (VS) {
831 default: assert(0 && "Unknown specifier!");
832 case VS_Override: VS_overrideLoc = Loc; break;
833 case VS_Final: VS_finalLoc = Loc; break;
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000834 }
Anders Carlsson46127a92011-01-22 15:58:16 +0000835
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000836 return false;
837}
838
Anders Carlssoncc54d592011-01-22 16:56:46 +0000839const char *VirtSpecifiers::getSpecifierName(Specifier VS) {
Anders Carlssonc46bb7d2011-01-22 15:11:37 +0000840 switch (VS) {
841 default: assert(0 && "Unknown specifier");
842 case VS_Override: return "override";
843 case VS_Final: return "final";
Anders Carlssonc46bb7d2011-01-22 15:11:37 +0000844 }
845}