blob: 8cbf9d9fed180a20735b5828166e29e268f78c48 [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 McCall7f040a92010-12-24 02:08:15 +0000137DeclaratorChunk DeclaratorChunk::getFunction(const ParsedAttributes &attrs,
138 bool hasProto, bool isVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +0000139 SourceLocation EllipsisLoc,
Chris Lattner5af2f352009-01-20 19:11:22 +0000140 ParamInfo *ArgInfo,
141 unsigned NumArgs,
142 unsigned TypeQuals,
Douglas Gregor83f51722011-01-26 03:43:54 +0000143 bool RefQualifierIsLvalueRef,
144 SourceLocation RefQualifierLoc,
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000145 ExceptionSpecificationType
146 ESpecType,
147 SourceLocation ESpecLoc,
John McCallb3d87482010-08-24 05:47:05 +0000148 ParsedType *Exceptions,
Sebastian Redlef65f062009-05-29 18:02:33 +0000149 SourceRange *ExceptionRanges,
Sebastian Redl7dc81342009-04-29 17:30:04 +0000150 unsigned NumExceptions,
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000151 Expr *NoexceptExpr,
Abramo Bagnara796aa442011-03-12 11:17:06 +0000152 SourceLocation LocalRangeBegin,
153 SourceLocation LocalRangeEnd,
Douglas Gregordab60ad2010-10-01 18:44:50 +0000154 Declarator &TheDeclarator,
155 ParsedType TrailingReturnType) {
Chris Lattner5af2f352009-01-20 19:11:22 +0000156 DeclaratorChunk I;
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000157 I.Kind = Function;
Abramo Bagnara796aa442011-03-12 11:17:06 +0000158 I.Loc = LocalRangeBegin;
159 I.EndLoc = LocalRangeEnd;
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000160 I.Fun.AttrList = attrs.getList();
161 I.Fun.hasPrototype = hasProto;
162 I.Fun.isVariadic = isVariadic;
163 I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding();
164 I.Fun.DeleteArgInfo = false;
165 I.Fun.TypeQuals = TypeQuals;
166 I.Fun.NumArgs = NumArgs;
167 I.Fun.ArgInfo = 0;
Douglas Gregor83f51722011-01-26 03:43:54 +0000168 I.Fun.RefQualifierIsLValueRef = RefQualifierIsLvalueRef;
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000169 I.Fun.RefQualifierLoc = RefQualifierLoc.getRawEncoding();
170 I.Fun.ExceptionSpecType = ESpecType;
171 I.Fun.ExceptionSpecLoc = ESpecLoc.getRawEncoding();
172 I.Fun.NumExceptions = 0;
173 I.Fun.Exceptions = 0;
174 I.Fun.NoexceptExpr = 0;
Douglas Gregordab60ad2010-10-01 18:44:50 +0000175 I.Fun.TrailingReturnType = TrailingReturnType.getAsOpaquePtr();
Sebastian Redl7dc81342009-04-29 17:30:04 +0000176
Chris Lattner5af2f352009-01-20 19:11:22 +0000177 // new[] an argument array if needed.
178 if (NumArgs) {
179 // If the 'InlineParams' in Declarator is unused and big enough, put our
180 // parameter list there (in an effort to avoid new/delete traffic). If it
181 // is already used (consider a function returning a function pointer) or too
182 // small (function taking too many arguments), go to the heap.
Mike Stump1eb44332009-09-09 15:08:12 +0000183 if (!TheDeclarator.InlineParamsUsed &&
Chris Lattner5af2f352009-01-20 19:11:22 +0000184 NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
185 I.Fun.ArgInfo = TheDeclarator.InlineParams;
186 I.Fun.DeleteArgInfo = false;
187 TheDeclarator.InlineParamsUsed = true;
188 } else {
189 I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs];
190 I.Fun.DeleteArgInfo = true;
191 }
192 memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs);
193 }
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000194
195 // Check what exception specification information we should actually store.
196 switch (ESpecType) {
197 default: break; // By default, save nothing.
198 case EST_Dynamic:
199 // new[] an exception array if needed
200 if (NumExceptions) {
201 I.Fun.NumExceptions = NumExceptions;
202 I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions];
203 for (unsigned i = 0; i != NumExceptions; ++i) {
204 I.Fun.Exceptions[i].Ty = Exceptions[i];
205 I.Fun.Exceptions[i].Range = ExceptionRanges[i];
206 }
Sebastian Redlef65f062009-05-29 18:02:33 +0000207 }
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000208 break;
209
210 case EST_ComputedNoexcept:
211 I.Fun.NoexceptExpr = NoexceptExpr;
212 break;
Sebastian Redl7dc81342009-04-29 17:30:04 +0000213 }
Chris Lattner5af2f352009-01-20 19:11:22 +0000214 return I;
215}
Chris Lattner254be6a2008-11-22 08:32:36 +0000216
Reid Spencer5f016e22007-07-11 17:01:13 +0000217/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
Chris Lattner2a327d12009-02-27 18:35:46 +0000218/// declaration specifier includes.
Reid Spencer5f016e22007-07-11 17:01:13 +0000219///
220unsigned DeclSpec::getParsedSpecifiers() const {
221 unsigned Res = 0;
222 if (StorageClassSpec != SCS_unspecified ||
223 SCS_thread_specified)
224 Res |= PQ_StorageClassSpecifier;
Mike Stumpd4204332008-06-19 19:52:46 +0000225
Reid Spencer5f016e22007-07-11 17:01:13 +0000226 if (TypeQualifiers != TQ_unspecified)
227 Res |= PQ_TypeQualifier;
Mike Stump1eb44332009-09-09 15:08:12 +0000228
Reid Spencer5f016e22007-07-11 17:01:13 +0000229 if (hasTypeSpecifier())
230 Res |= PQ_TypeSpecifier;
Mike Stump1eb44332009-09-09 15:08:12 +0000231
Douglas Gregorb48fe382008-10-31 09:07:45 +0000232 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
Reid Spencer5f016e22007-07-11 17:01:13 +0000233 Res |= PQ_FunctionSpecifier;
234 return Res;
235}
236
John McCallfec54012009-08-03 20:12:06 +0000237template <class T> static bool BadSpecifier(T TNew, T TPrev,
238 const char *&PrevSpec,
239 unsigned &DiagID) {
John McCall32d335e2009-08-03 18:47:27 +0000240 PrevSpec = DeclSpec::getSpecifierName(TPrev);
John McCallfec54012009-08-03 20:12:06 +0000241 DiagID = (TNew == TPrev ? diag::ext_duplicate_declspec
242 : diag::err_invalid_decl_spec_combination);
John McCall32d335e2009-08-03 18:47:27 +0000243 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000244}
John McCall32d335e2009-08-03 18:47:27 +0000245
Reid Spencer5f016e22007-07-11 17:01:13 +0000246const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
247 switch (S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000248 case DeclSpec::SCS_unspecified: return "unspecified";
249 case DeclSpec::SCS_typedef: return "typedef";
250 case DeclSpec::SCS_extern: return "extern";
251 case DeclSpec::SCS_static: return "static";
252 case DeclSpec::SCS_auto: return "auto";
253 case DeclSpec::SCS_register: return "register";
Eli Friedman63054b32009-04-19 20:27:55 +0000254 case DeclSpec::SCS_private_extern: return "__private_extern__";
Sebastian Redl669d5d72008-11-14 23:42:31 +0000255 case DeclSpec::SCS_mutable: return "mutable";
Reid Spencer5f016e22007-07-11 17:01:13 +0000256 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000257 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000258}
259
John McCall32d335e2009-08-03 18:47:27 +0000260const char *DeclSpec::getSpecifierName(TSW W) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000261 switch (W) {
John McCall32d335e2009-08-03 18:47:27 +0000262 case TSW_unspecified: return "unspecified";
263 case TSW_short: return "short";
264 case TSW_long: return "long";
265 case TSW_longlong: return "long long";
Reid Spencer5f016e22007-07-11 17:01:13 +0000266 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000267 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000268}
269
John McCall32d335e2009-08-03 18:47:27 +0000270const char *DeclSpec::getSpecifierName(TSC C) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000271 switch (C) {
John McCall32d335e2009-08-03 18:47:27 +0000272 case TSC_unspecified: return "unspecified";
273 case TSC_imaginary: return "imaginary";
274 case TSC_complex: return "complex";
Reid Spencer5f016e22007-07-11 17:01:13 +0000275 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000276 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000277}
278
279
John McCall32d335e2009-08-03 18:47:27 +0000280const char *DeclSpec::getSpecifierName(TSS S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000281 switch (S) {
John McCall32d335e2009-08-03 18:47:27 +0000282 case TSS_unspecified: return "unspecified";
283 case TSS_signed: return "signed";
284 case TSS_unsigned: return "unsigned";
Reid Spencer5f016e22007-07-11 17:01:13 +0000285 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000286 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000287}
288
289const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
290 switch (T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000291 case DeclSpec::TST_unspecified: return "unspecified";
292 case DeclSpec::TST_void: return "void";
293 case DeclSpec::TST_char: return "char";
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000294 case DeclSpec::TST_wchar: return "wchar_t";
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000295 case DeclSpec::TST_char16: return "char16_t";
296 case DeclSpec::TST_char32: return "char32_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000297 case DeclSpec::TST_int: return "int";
298 case DeclSpec::TST_float: return "float";
299 case DeclSpec::TST_double: return "double";
300 case DeclSpec::TST_bool: return "_Bool";
301 case DeclSpec::TST_decimal32: return "_Decimal32";
302 case DeclSpec::TST_decimal64: return "_Decimal64";
303 case DeclSpec::TST_decimal128: return "_Decimal128";
304 case DeclSpec::TST_enum: return "enum";
Chris Lattner99dc9142008-04-13 18:59:07 +0000305 case DeclSpec::TST_class: return "class";
Reid Spencer5f016e22007-07-11 17:01:13 +0000306 case DeclSpec::TST_union: return "union";
307 case DeclSpec::TST_struct: return "struct";
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000308 case DeclSpec::TST_typename: return "type-name";
Steve Naroffd1861fd2007-07-31 12:34:36 +0000309 case DeclSpec::TST_typeofType:
310 case DeclSpec::TST_typeofExpr: return "typeof";
John McCall32d335e2009-08-03 18:47:27 +0000311 case DeclSpec::TST_auto: return "auto";
312 case DeclSpec::TST_decltype: return "(decltype)";
313 case DeclSpec::TST_error: return "(error)";
Reid Spencer5f016e22007-07-11 17:01:13 +0000314 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000315 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000316}
317
John McCall32d335e2009-08-03 18:47:27 +0000318const char *DeclSpec::getSpecifierName(TQ T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000319 switch (T) {
John McCall32d335e2009-08-03 18:47:27 +0000320 case DeclSpec::TQ_unspecified: return "unspecified";
321 case DeclSpec::TQ_const: return "const";
322 case DeclSpec::TQ_restrict: return "restrict";
323 case DeclSpec::TQ_volatile: return "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +0000324 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000325 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000326}
327
328bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000329 const char *&PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +0000330 unsigned &DiagID,
331 const LangOptions &Lang) {
332 // OpenCL prohibits extern, auto, register, and static
333 // It seems sensible to prohibit private_extern too
334 if (Lang.OpenCL) {
335 switch (S) {
336 case SCS_extern:
337 case SCS_private_extern:
338 case SCS_auto:
339 case SCS_register:
340 case SCS_static:
341 DiagID = diag::err_not_opencl_storage_class_specifier;
342 PrevSpec = getSpecifierName(S);
343 return true;
344 default:
345 break;
346 }
347 }
348
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000349 if (StorageClassSpec != SCS_unspecified) {
350 // Changing storage class is allowed only if the previous one
351 // was the 'extern' that is part of a linkage specification and
352 // the new storage class is 'typedef'.
353 if (!(SCS_extern_in_linkage_spec &&
354 StorageClassSpec == SCS_extern &&
355 S == SCS_typedef))
356 return BadSpecifier(S, (SCS)StorageClassSpec, PrevSpec, DiagID);
357 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000358 StorageClassSpec = S;
359 StorageClassSpecLoc = Loc;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000360 assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield");
Reid Spencer5f016e22007-07-11 17:01:13 +0000361 return false;
362}
363
Mike Stump1eb44332009-09-09 15:08:12 +0000364bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000365 const char *&PrevSpec,
366 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000367 if (SCS_thread_specified) {
368 PrevSpec = "__thread";
John McCallfec54012009-08-03 20:12:06 +0000369 DiagID = diag::ext_duplicate_declspec;
Reid Spencer5f016e22007-07-11 17:01:13 +0000370 return true;
371 }
372 SCS_thread_specified = true;
373 SCS_threadLoc = Loc;
374 return false;
375}
376
Reid Spencer5f016e22007-07-11 17:01:13 +0000377/// These methods set the specified attribute of the DeclSpec, but return true
378/// and ignore the request if invalid (e.g. "extern" then "auto" is
379/// specified).
380bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000381 const char *&PrevSpec,
382 unsigned &DiagID) {
Abramo Bagnara2553eaf2011-03-06 22:21:56 +0000383 // Overwrite TSWLoc only if TypeSpecWidth was unspecified, so that
384 // for 'long long' we will keep the source location of the first 'long'.
385 if (TypeSpecWidth == TSW_unspecified)
386 TSWLoc = Loc;
387 // Allow turning long -> long long.
388 else if (W != TSW_longlong || TypeSpecWidth != TSW_long)
John McCallfec54012009-08-03 20:12:06 +0000389 return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000390 TypeSpecWidth = W;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000391 if (TypeAltiVecVector && !TypeAltiVecBool &&
392 ((TypeSpecWidth == TSW_long) || (TypeSpecWidth == TSW_longlong))) {
John Thompson82287d12010-02-05 00:12:22 +0000393 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
394 DiagID = diag::warn_vector_long_decl_spec_combination;
395 return true;
396 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000397 return false;
398}
399
Mike Stump1eb44332009-09-09 15:08:12 +0000400bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000401 const char *&PrevSpec,
402 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000403 if (TypeSpecComplex != TSC_unspecified)
John McCallfec54012009-08-03 20:12:06 +0000404 return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000405 TypeSpecComplex = C;
406 TSCLoc = Loc;
407 return false;
408}
409
Mike Stump1eb44332009-09-09 15:08:12 +0000410bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000411 const char *&PrevSpec,
412 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000413 if (TypeSpecSign != TSS_unspecified)
John McCallfec54012009-08-03 20:12:06 +0000414 return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000415 TypeSpecSign = S;
416 TSSLoc = Loc;
417 return false;
418}
419
420bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000421 const char *&PrevSpec,
422 unsigned &DiagID,
John McCallb3d87482010-08-24 05:47:05 +0000423 ParsedType Rep) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000424 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep);
425}
426
427bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
428 SourceLocation TagNameLoc,
429 const char *&PrevSpec,
430 unsigned &DiagID,
431 ParsedType Rep) {
John McCallb3d87482010-08-24 05:47:05 +0000432 assert(isTypeRep(T) && "T does not store a type");
433 assert(Rep && "no type provided!");
434 if (TypeSpecType != TST_unspecified) {
435 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
436 DiagID = diag::err_invalid_decl_spec_combination;
437 return true;
438 }
439 TypeSpecType = T;
440 TypeRep = Rep;
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000441 TSTLoc = TagKwLoc;
442 TSTNameLoc = TagNameLoc;
John McCallb3d87482010-08-24 05:47:05 +0000443 TypeSpecOwned = false;
444 return false;
445}
446
447bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
448 const char *&PrevSpec,
449 unsigned &DiagID,
450 Expr *Rep) {
451 assert(isExprRep(T) && "T does not store an expr");
452 assert(Rep && "no expression provided!");
453 if (TypeSpecType != TST_unspecified) {
454 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
455 DiagID = diag::err_invalid_decl_spec_combination;
456 return true;
457 }
458 TypeSpecType = T;
459 ExprRep = Rep;
460 TSTLoc = Loc;
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000461 TSTNameLoc = Loc;
John McCallb3d87482010-08-24 05:47:05 +0000462 TypeSpecOwned = false;
463 return false;
464}
465
466bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
467 const char *&PrevSpec,
468 unsigned &DiagID,
469 Decl *Rep, bool Owned) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000470 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep, Owned);
471}
472
473bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
474 SourceLocation TagNameLoc,
475 const char *&PrevSpec,
476 unsigned &DiagID,
477 Decl *Rep, bool Owned) {
John McCallb3d87482010-08-24 05:47:05 +0000478 assert(isDeclRep(T) && "T does not store a decl");
479 // Unlike the other cases, we don't assert that we actually get a decl.
480
481 if (TypeSpecType != TST_unspecified) {
482 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
483 DiagID = diag::err_invalid_decl_spec_combination;
484 return true;
485 }
486 TypeSpecType = T;
487 DeclRep = Rep;
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000488 TSTLoc = TagKwLoc;
489 TSTNameLoc = TagNameLoc;
John McCallb3d87482010-08-24 05:47:05 +0000490 TypeSpecOwned = Owned;
491 return false;
492}
493
494bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
495 const char *&PrevSpec,
496 unsigned &DiagID) {
497 assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) &&
498 "rep required for these type-spec kinds!");
John McCallfec54012009-08-03 20:12:06 +0000499 if (TypeSpecType != TST_unspecified) {
500 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
501 DiagID = diag::err_invalid_decl_spec_combination;
502 return true;
503 }
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000504 TSTLoc = Loc;
505 TSTNameLoc = Loc;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000506 if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) {
507 TypeAltiVecBool = true;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000508 return false;
509 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000510 TypeSpecType = T;
John McCallb3d87482010-08-24 05:47:05 +0000511 TypeSpecOwned = false;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000512 if (TypeAltiVecVector && !TypeAltiVecBool && (TypeSpecType == TST_double)) {
John Thompson82287d12010-02-05 00:12:22 +0000513 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
Chris Lattner788b0fd2010-06-23 06:00:24 +0000514 DiagID = diag::err_invalid_vector_decl_spec;
John Thompson82287d12010-02-05 00:12:22 +0000515 return true;
516 }
517 return false;
518}
519
520bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
521 const char *&PrevSpec, unsigned &DiagID) {
522 if (TypeSpecType != TST_unspecified) {
523 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
524 DiagID = diag::err_invalid_vector_decl_spec_combination;
525 return true;
526 }
527 TypeAltiVecVector = isAltiVecVector;
528 AltiVecLoc = Loc;
529 return false;
530}
531
532bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
533 const char *&PrevSpec, unsigned &DiagID) {
Chris Lattner788b0fd2010-06-23 06:00:24 +0000534 if (!TypeAltiVecVector || TypeAltiVecPixel ||
535 (TypeSpecType != TST_unspecified)) {
John Thompson82287d12010-02-05 00:12:22 +0000536 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
537 DiagID = diag::err_invalid_pixel_decl_spec_combination;
538 return true;
539 }
John Thompson82287d12010-02-05 00:12:22 +0000540 TypeAltiVecPixel = isAltiVecPixel;
541 TSTLoc = Loc;
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000542 TSTNameLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000543 return false;
544}
545
Douglas Gregorddc29e12009-02-06 22:42:48 +0000546bool DeclSpec::SetTypeSpecError() {
547 TypeSpecType = TST_error;
John McCall9e46b8c2010-08-26 17:22:34 +0000548 TypeSpecOwned = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000549 TSTLoc = SourceLocation();
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000550 TSTNameLoc = SourceLocation();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000551 return false;
552}
553
Reid Spencer5f016e22007-07-11 17:01:13 +0000554bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000555 unsigned &DiagID, const LangOptions &Lang) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000556 // Duplicates turn into warnings pre-C99.
557 if ((TypeQualifiers & T) && !Lang.C99)
John McCallfec54012009-08-03 20:12:06 +0000558 return BadSpecifier(T, T, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000559 TypeQualifiers |= T;
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Reid Spencer5f016e22007-07-11 17:01:13 +0000561 switch (T) {
562 default: assert(0 && "Unknown type qualifier!");
563 case TQ_const: TQ_constLoc = Loc; break;
564 case TQ_restrict: TQ_restrictLoc = Loc; break;
565 case TQ_volatile: TQ_volatileLoc = Loc; break;
566 }
567 return false;
568}
569
John McCallfec54012009-08-03 20:12:06 +0000570bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
571 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000572 // 'inline inline' is ok.
573 FS_inline_specified = true;
574 FS_inlineLoc = Loc;
575 return false;
576}
577
John McCallfec54012009-08-03 20:12:06 +0000578bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec,
579 unsigned &DiagID) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000580 // 'virtual virtual' is ok.
581 FS_virtual_specified = true;
582 FS_virtualLoc = Loc;
583 return false;
584}
585
John McCallfec54012009-08-03 20:12:06 +0000586bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec,
587 unsigned &DiagID) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000588 // 'explicit explicit' is ok.
589 FS_explicit_specified = true;
590 FS_explicitLoc = Loc;
591 return false;
592}
593
John McCallfec54012009-08-03 20:12:06 +0000594bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
595 unsigned &DiagID) {
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000596 if (Friend_specified) {
597 PrevSpec = "friend";
John McCallfec54012009-08-03 20:12:06 +0000598 DiagID = diag::ext_duplicate_declspec;
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000599 return true;
600 }
John McCallfec54012009-08-03 20:12:06 +0000601
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000602 Friend_specified = true;
603 FriendLoc = Loc;
604 return false;
605}
Reid Spencer5f016e22007-07-11 17:01:13 +0000606
Sebastian Redl2ac67232009-11-05 15:47:02 +0000607bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
608 unsigned &DiagID) {
609 // 'constexpr constexpr' is ok.
610 Constexpr_specified = true;
611 ConstexprLoc = Loc;
612 return false;
613}
614
John McCalld226f652010-08-21 09:40:31 +0000615void DeclSpec::setProtocolQualifiers(Decl * const *Protos,
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000616 unsigned NP,
617 SourceLocation *ProtoLocs,
618 SourceLocation LAngleLoc) {
619 if (NP == 0) return;
John McCalld226f652010-08-21 09:40:31 +0000620 ProtocolQualifiers = new Decl*[NP];
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000621 ProtocolLocs = new SourceLocation[NP];
John McCalld226f652010-08-21 09:40:31 +0000622 memcpy((void*)ProtocolQualifiers, Protos, sizeof(Decl*)*NP);
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000623 memcpy(ProtocolLocs, ProtoLocs, sizeof(SourceLocation)*NP);
624 NumProtocolQualifiers = NP;
625 ProtocolLAngleLoc = LAngleLoc;
626}
627
Douglas Gregorddf889a2010-01-18 18:04:31 +0000628void DeclSpec::SaveWrittenBuiltinSpecs() {
629 writtenBS.Sign = getTypeSpecSign();
630 writtenBS.Width = getTypeSpecWidth();
631 writtenBS.Type = getTypeSpecType();
632 // Search the list of attributes for the presence of a mode attribute.
633 writtenBS.ModeAttr = false;
John McCall7f040a92010-12-24 02:08:15 +0000634 AttributeList* attrs = getAttributes().getList();
Douglas Gregorddf889a2010-01-18 18:04:31 +0000635 while (attrs) {
636 if (attrs->getKind() == AttributeList::AT_mode) {
637 writtenBS.ModeAttr = true;
638 break;
639 }
640 attrs = attrs->getNext();
641 }
642}
643
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000644void DeclSpec::SaveStorageSpecifierAsWritten() {
645 if (SCS_extern_in_linkage_spec && StorageClassSpec == SCS_extern)
646 // If 'extern' is part of a linkage specification,
647 // then it is not a storage class "as written".
648 StorageClassSpecAsWritten = SCS_unspecified;
649 else
650 StorageClassSpecAsWritten = StorageClassSpec;
651}
652
Reid Spencer5f016e22007-07-11 17:01:13 +0000653/// Finish - This does final analysis of the declspec, rejecting things like
654/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
655/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
656/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000657void DeclSpec::Finish(Diagnostic &D, Preprocessor &PP) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000658 // Before possibly changing their values, save specs as written.
659 SaveWrittenBuiltinSpecs();
Douglas Gregor16573fa2010-04-19 22:54:31 +0000660 SaveStorageSpecifierAsWritten();
Douglas Gregorddf889a2010-01-18 18:04:31 +0000661
Reid Spencer5f016e22007-07-11 17:01:13 +0000662 // Check the type specifier components first.
663
Chris Lattner788b0fd2010-06-23 06:00:24 +0000664 // Validate and finalize AltiVec vector declspec.
665 if (TypeAltiVecVector) {
666 if (TypeAltiVecBool) {
667 // Sign specifiers are not allowed with vector bool. (PIM 2.1)
668 if (TypeSpecSign != TSS_unspecified) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000669 Diag(D, TSSLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner788b0fd2010-06-23 06:00:24 +0000670 << getSpecifierName((TSS)TypeSpecSign);
671 }
672
673 // Only char/int are valid with vector bool. (PIM 2.1)
Duncan Sands2e964a922010-06-23 19:34:52 +0000674 if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) &&
675 (TypeSpecType != TST_int)) || TypeAltiVecPixel) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000676 Diag(D, TSTLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner788b0fd2010-06-23 06:00:24 +0000677 << (TypeAltiVecPixel ? "__pixel" :
678 getSpecifierName((TST)TypeSpecType));
679 }
680
681 // Only 'short' is valid with vector bool. (PIM 2.1)
682 if ((TypeSpecWidth != TSW_unspecified) && (TypeSpecWidth != TSW_short))
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000683 Diag(D, TSWLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner788b0fd2010-06-23 06:00:24 +0000684 << getSpecifierName((TSW)TypeSpecWidth);
685
686 // Elements of vector bool are interpreted as unsigned. (PIM 2.1)
687 if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) ||
688 (TypeSpecWidth != TSW_unspecified))
689 TypeSpecSign = TSS_unsigned;
690 }
691
692 if (TypeAltiVecPixel) {
693 //TODO: perform validation
694 TypeSpecType = TST_int;
695 TypeSpecSign = TSS_unsigned;
696 TypeSpecWidth = TSW_short;
John McCall9e46b8c2010-08-26 17:22:34 +0000697 TypeSpecOwned = false;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000698 }
699 }
700
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000701 // signed/unsigned are only valid with int/char/wchar_t.
Reid Spencer5f016e22007-07-11 17:01:13 +0000702 if (TypeSpecSign != TSS_unspecified) {
703 if (TypeSpecType == TST_unspecified)
704 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000705 else if (TypeSpecType != TST_int &&
706 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000707 Diag(D, TSSLoc, diag::err_invalid_sign_spec)
Chris Lattner254be6a2008-11-22 08:32:36 +0000708 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000709 // signed double -> double.
710 TypeSpecSign = TSS_unspecified;
711 }
712 }
713
714 // Validate the width of the type.
715 switch (TypeSpecWidth) {
716 case TSW_unspecified: break;
717 case TSW_short: // short int
718 case TSW_longlong: // long long int
719 if (TypeSpecType == TST_unspecified)
720 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
721 else if (TypeSpecType != TST_int) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000722 Diag(D, TSWLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000723 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
Chris Lattner254be6a2008-11-22 08:32:36 +0000724 : diag::err_invalid_longlong_spec)
725 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000726 TypeSpecType = TST_int;
John McCall9e46b8c2010-08-26 17:22:34 +0000727 TypeSpecOwned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000728 }
729 break;
730 case TSW_long: // long double, long int
731 if (TypeSpecType == TST_unspecified)
732 TypeSpecType = TST_int; // long -> long int.
733 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000734 Diag(D, TSWLoc, diag::err_invalid_long_spec)
Chris Lattner254be6a2008-11-22 08:32:36 +0000735 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000736 TypeSpecType = TST_int;
John McCall9e46b8c2010-08-26 17:22:34 +0000737 TypeSpecOwned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000738 }
739 break;
740 }
Mike Stump1eb44332009-09-09 15:08:12 +0000741
Reid Spencer5f016e22007-07-11 17:01:13 +0000742 // TODO: if the implementation does not implement _Complex or _Imaginary,
743 // disallow their use. Need information about the backend.
744 if (TypeSpecComplex != TSC_unspecified) {
745 if (TypeSpecType == TST_unspecified) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000746 Diag(D, TSCLoc, diag::ext_plain_complex)
Douglas Gregor849b2432010-03-31 17:46:05 +0000747 << FixItHint::CreateInsertion(
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000748 PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
749 " double");
Reid Spencer5f016e22007-07-11 17:01:13 +0000750 TypeSpecType = TST_double; // _Complex -> _Complex double.
751 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
752 // Note that this intentionally doesn't include _Complex _Bool.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000753 Diag(D, TSTLoc, diag::ext_integer_complex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000754 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000755 Diag(D, TSCLoc, diag::err_invalid_complex_spec)
Chris Lattner254be6a2008-11-22 08:32:36 +0000756 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000757 TypeSpecComplex = TSC_unspecified;
758 }
759 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000760
John McCall67d1a672009-08-06 02:15:43 +0000761 // C++ [class.friend]p6:
762 // No storage-class-specifier shall appear in the decl-specifier-seq
763 // of a friend declaration.
764 if (isFriendSpecified() && getStorageClassSpec()) {
765 DeclSpec::SCS SC = getStorageClassSpec();
766 const char *SpecName = getSpecifierName(SC);
767
768 SourceLocation SCLoc = getStorageClassSpecLoc();
769 SourceLocation SCEndLoc = SCLoc.getFileLocWithOffset(strlen(SpecName));
770
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000771 Diag(D, SCLoc, diag::err_friend_storage_spec)
John McCall67d1a672009-08-06 02:15:43 +0000772 << SpecName
Douglas Gregor849b2432010-03-31 17:46:05 +0000773 << FixItHint::CreateRemoval(SourceRange(SCLoc, SCEndLoc));
John McCall67d1a672009-08-06 02:15:43 +0000774
775 ClearStorageClassSpecs();
776 }
777
John McCall9e46b8c2010-08-26 17:22:34 +0000778 assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType));
779
Reid Spencer5f016e22007-07-11 17:01:13 +0000780 // Okay, now we can infer the real type.
Mike Stump1eb44332009-09-09 15:08:12 +0000781
Reid Spencer5f016e22007-07-11 17:01:13 +0000782 // TODO: return "auto function" and other bad things based on the real type.
Mike Stump1eb44332009-09-09 15:08:12 +0000783
Reid Spencer5f016e22007-07-11 17:01:13 +0000784 // 'data definition has no type or storage class'?
785}
Daniel Dunbare4858a62008-08-11 03:45:03 +0000786
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000787bool DeclSpec::isMissingDeclaratorOk() {
788 TST tst = getTypeSpecType();
John McCallb3d87482010-08-24 05:47:05 +0000789 return isDeclRep(tst) && getRepAsDecl() != 0 &&
790 StorageClassSpec != DeclSpec::SCS_typedef;
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000791}
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000792
793void UnqualifiedId::clear() {
794 if (Kind == IK_TemplateId)
795 TemplateId->Destroy();
796
797 Kind = IK_Identifier;
798 Identifier = 0;
799 StartLocation = SourceLocation();
800 EndLocation = SourceLocation();
801}
802
803void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc,
804 OverloadedOperatorKind Op,
805 SourceLocation SymbolLocations[3]) {
806 Kind = IK_OperatorFunctionId;
807 StartLocation = OperatorLoc;
808 EndLocation = OperatorLoc;
809 OperatorFunctionId.Operator = Op;
810 for (unsigned I = 0; I != 3; ++I) {
811 OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding();
812
813 if (SymbolLocations[I].isValid())
814 EndLocation = SymbolLocations[I];
815 }
816}
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000817
Anders Carlssoncc54d592011-01-22 16:56:46 +0000818bool VirtSpecifiers::SetSpecifier(Specifier VS, SourceLocation Loc,
Anders Carlsson46127a92011-01-22 15:58:16 +0000819 const char *&PrevSpec) {
Douglas Gregorf5251602011-03-08 17:10:18 +0000820 LastLocation = Loc;
821
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000822 if (Specifiers & VS) {
823 PrevSpec = getSpecifierName(VS);
824 return true;
825 }
826
827 Specifiers |= VS;
828
829 switch (VS) {
830 default: assert(0 && "Unknown specifier!");
831 case VS_Override: VS_overrideLoc = Loc; break;
832 case VS_Final: VS_finalLoc = Loc; break;
833 case VS_New: VS_newLoc = Loc; break;
834 }
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";
844 case VS_New: return "new";
845 }
846}
Anders Carlsson46127a92011-01-22 15:58:16 +0000847
Anders Carlssoncc54d592011-01-22 16:56:46 +0000848bool ClassVirtSpecifiers::SetSpecifier(Specifier CVS, SourceLocation Loc,
Anders Carlsson46127a92011-01-22 15:58:16 +0000849 const char *&PrevSpec) {
850 if (Specifiers & CVS) {
851 PrevSpec = getSpecifierName(CVS);
852 return true;
853 }
854
855 Specifiers |= CVS;
856
857 switch (CVS) {
858 default: assert(0 && "Unknown specifier!");
859 case CVS_Final: CVS_finalLoc = Loc; break;
860 case CVS_Explicit: CVS_explicitLoc = Loc; break;
861 }
862
863 return false;
864}
865
Anders Carlssoncc54d592011-01-22 16:56:46 +0000866const char *ClassVirtSpecifiers::getSpecifierName(Specifier CVS) {
Anders Carlsson46127a92011-01-22 15:58:16 +0000867 switch (CVS) {
868 default: assert(0 && "Unknown specifier");
869 case CVS_Final: return "final";
870 case CVS_Explicit: return "explicit";
871 }
872}
873