blob: d450a32e77df20286d9943ac14972294d1686496 [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"
Douglas Gregor555f57e2011-06-25 00:56:27 +000016#include "clang/Sema/LocInfoType.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/ParsedTemplate.h"
Douglas Gregorc34348a2011-02-24 17:54:50 +000018#include "clang/AST/ASTContext.h"
Douglas Gregor555f57e2011-06-25 00:56:27 +000019#include "clang/AST/Expr.h"
Douglas Gregor2e4c34a2011-02-24 00:17:56 +000020#include "clang/AST/NestedNameSpecifier.h"
21#include "clang/AST/TypeLoc.h"
Douglas Gregor9b3064b2009-04-01 22:41:11 +000022#include "clang/Lex/Preprocessor.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023#include "clang/Basic/LangOptions.h"
Chris Lattner5af2f352009-01-20 19:11:22 +000024#include "llvm/ADT/STLExtras.h"
John McCall32d335e2009-08-03 18:47:27 +000025#include "llvm/Support/ErrorHandling.h"
Douglas Gregore4e5b052009-03-19 00:18:19 +000026#include <cstring>
Reid Spencer5f016e22007-07-11 17:01:13 +000027using namespace clang;
28
Chris Lattner254be6a2008-11-22 08:32:36 +000029
30static DiagnosticBuilder Diag(Diagnostic &D, SourceLocation Loc,
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000031 unsigned DiagID) {
32 return D.Report(Loc, DiagID);
Chris Lattner254be6a2008-11-22 08:32:36 +000033}
34
Douglas Gregor314b97f2009-11-10 19:49:08 +000035
36void UnqualifiedId::setTemplateId(TemplateIdAnnotation *TemplateId) {
37 assert(TemplateId && "NULL template-id annotation?");
38 Kind = IK_TemplateId;
39 this->TemplateId = TemplateId;
40 StartLocation = TemplateId->TemplateNameLoc;
41 EndLocation = TemplateId->RAngleLoc;
42}
43
Douglas Gregor0efc2c12010-01-13 17:31:36 +000044void UnqualifiedId::setConstructorTemplateId(TemplateIdAnnotation *TemplateId) {
45 assert(TemplateId && "NULL template-id annotation?");
46 Kind = IK_ConstructorTemplateId;
47 this->TemplateId = TemplateId;
48 StartLocation = TemplateId->TemplateNameLoc;
49 EndLocation = TemplateId->RAngleLoc;
50}
51
Douglas Gregor2e4c34a2011-02-24 00:17:56 +000052void CXXScopeSpec::Extend(ASTContext &Context, SourceLocation TemplateKWLoc,
53 TypeLoc TL, SourceLocation ColonColonLoc) {
Douglas Gregor5f791bb2011-02-28 23:58:31 +000054 Builder.Extend(Context, TemplateKWLoc, TL, ColonColonLoc);
Douglas Gregor2e4c34a2011-02-24 00:17:56 +000055 if (Range.getBegin().isInvalid())
56 Range.setBegin(TL.getBeginLoc());
57 Range.setEnd(ColonColonLoc);
Douglas Gregorc34348a2011-02-24 17:54:50 +000058
Douglas Gregor5f791bb2011-02-28 23:58:31 +000059 assert(Range == Builder.getSourceRange() &&
Douglas Gregorc34348a2011-02-24 17:54:50 +000060 "NestedNameSpecifierLoc range computation incorrect");
Douglas Gregor2e4c34a2011-02-24 00:17:56 +000061}
62
63void CXXScopeSpec::Extend(ASTContext &Context, IdentifierInfo *Identifier,
64 SourceLocation IdentifierLoc,
65 SourceLocation ColonColonLoc) {
Douglas Gregor5f791bb2011-02-28 23:58:31 +000066 Builder.Extend(Context, Identifier, IdentifierLoc, ColonColonLoc);
67
Douglas Gregor2e4c34a2011-02-24 00:17:56 +000068 if (Range.getBegin().isInvalid())
69 Range.setBegin(IdentifierLoc);
70 Range.setEnd(ColonColonLoc);
Douglas Gregorc34348a2011-02-24 17:54:50 +000071
Douglas Gregor5f791bb2011-02-28 23:58:31 +000072 assert(Range == Builder.getSourceRange() &&
Douglas Gregorc34348a2011-02-24 17:54:50 +000073 "NestedNameSpecifierLoc range computation incorrect");
Douglas Gregor2e4c34a2011-02-24 00:17:56 +000074}
75
76void CXXScopeSpec::Extend(ASTContext &Context, NamespaceDecl *Namespace,
77 SourceLocation NamespaceLoc,
78 SourceLocation ColonColonLoc) {
Douglas Gregor5f791bb2011-02-28 23:58:31 +000079 Builder.Extend(Context, Namespace, NamespaceLoc, ColonColonLoc);
80
Douglas Gregor2e4c34a2011-02-24 00:17:56 +000081 if (Range.getBegin().isInvalid())
82 Range.setBegin(NamespaceLoc);
83 Range.setEnd(ColonColonLoc);
Douglas Gregorc34348a2011-02-24 17:54:50 +000084
Douglas Gregor5f791bb2011-02-28 23:58:31 +000085 assert(Range == Builder.getSourceRange() &&
Douglas Gregorc34348a2011-02-24 17:54:50 +000086 "NestedNameSpecifierLoc range computation incorrect");
Douglas Gregor2e4c34a2011-02-24 00:17:56 +000087}
88
Douglas Gregor14aba762011-02-24 02:36:08 +000089void CXXScopeSpec::Extend(ASTContext &Context, NamespaceAliasDecl *Alias,
90 SourceLocation AliasLoc,
91 SourceLocation ColonColonLoc) {
Douglas Gregor5f791bb2011-02-28 23:58:31 +000092 Builder.Extend(Context, Alias, AliasLoc, ColonColonLoc);
93
Douglas Gregor14aba762011-02-24 02:36:08 +000094 if (Range.getBegin().isInvalid())
95 Range.setBegin(AliasLoc);
96 Range.setEnd(ColonColonLoc);
Douglas Gregorc34348a2011-02-24 17:54:50 +000097
Douglas Gregor5f791bb2011-02-28 23:58:31 +000098 assert(Range == Builder.getSourceRange() &&
Douglas Gregorc34348a2011-02-24 17:54:50 +000099 "NestedNameSpecifierLoc range computation incorrect");
Douglas Gregor14aba762011-02-24 02:36:08 +0000100}
101
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000102void CXXScopeSpec::MakeGlobal(ASTContext &Context,
103 SourceLocation ColonColonLoc) {
Douglas Gregor5f791bb2011-02-28 23:58:31 +0000104 Builder.MakeGlobal(Context, ColonColonLoc);
105
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000106 Range = SourceRange(ColonColonLoc);
Douglas Gregorc34348a2011-02-24 17:54:50 +0000107
Douglas Gregor5f791bb2011-02-28 23:58:31 +0000108 assert(Range == Builder.getSourceRange() &&
Douglas Gregorc34348a2011-02-24 17:54:50 +0000109 "NestedNameSpecifierLoc range computation incorrect");
110}
111
112void CXXScopeSpec::MakeTrivial(ASTContext &Context,
113 NestedNameSpecifier *Qualifier, SourceRange R) {
Douglas Gregor5f791bb2011-02-28 23:58:31 +0000114 Builder.MakeTrivial(Context, Qualifier, R);
Douglas Gregorc34348a2011-02-24 17:54:50 +0000115 Range = R;
Douglas Gregorc34348a2011-02-24 17:54:50 +0000116}
117
118void CXXScopeSpec::Adopt(NestedNameSpecifierLoc Other) {
119 if (!Other) {
120 Range = SourceRange();
Douglas Gregor5f791bb2011-02-28 23:58:31 +0000121 Builder.Clear();
Douglas Gregorc34348a2011-02-24 17:54:50 +0000122 return;
123 }
Douglas Gregor5f791bb2011-02-28 23:58:31 +0000124
Douglas Gregorc34348a2011-02-24 17:54:50 +0000125 Range = Other.getSourceRange();
Douglas Gregor5f791bb2011-02-28 23:58:31 +0000126 Builder.Adopt(Other);
Douglas Gregorc34348a2011-02-24 17:54:50 +0000127}
128
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000129NestedNameSpecifierLoc
130CXXScopeSpec::getWithLocInContext(ASTContext &Context) const {
Douglas Gregorb46ae392011-03-03 21:48:55 +0000131 if (!Builder.getRepresentation())
Douglas Gregorc34348a2011-02-24 17:54:50 +0000132 return NestedNameSpecifierLoc();
133
Douglas Gregor5f791bb2011-02-28 23:58:31 +0000134 return Builder.getWithLocInContext(Context);
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000135}
136
Chris Lattner5af2f352009-01-20 19:11:22 +0000137/// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function.
138/// "TheDeclarator" is the declarator that this will be added to.
John McCall0b7e6782011-03-24 11:26:52 +0000139DeclaratorChunk DeclaratorChunk::getFunction(bool hasProto, bool isVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +0000140 SourceLocation EllipsisLoc,
Chris Lattner5af2f352009-01-20 19:11:22 +0000141 ParamInfo *ArgInfo,
142 unsigned NumArgs,
143 unsigned TypeQuals,
Douglas Gregor83f51722011-01-26 03:43:54 +0000144 bool RefQualifierIsLvalueRef,
145 SourceLocation RefQualifierLoc,
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000146 ExceptionSpecificationType
147 ESpecType,
148 SourceLocation ESpecLoc,
John McCallb3d87482010-08-24 05:47:05 +0000149 ParsedType *Exceptions,
Sebastian Redlef65f062009-05-29 18:02:33 +0000150 SourceRange *ExceptionRanges,
Sebastian Redl7dc81342009-04-29 17:30:04 +0000151 unsigned NumExceptions,
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000152 Expr *NoexceptExpr,
Abramo Bagnara796aa442011-03-12 11:17:06 +0000153 SourceLocation LocalRangeBegin,
154 SourceLocation LocalRangeEnd,
Douglas Gregordab60ad2010-10-01 18:44:50 +0000155 Declarator &TheDeclarator,
156 ParsedType TrailingReturnType) {
Chris Lattner5af2f352009-01-20 19:11:22 +0000157 DeclaratorChunk I;
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000158 I.Kind = Function;
Abramo Bagnara796aa442011-03-12 11:17:06 +0000159 I.Loc = LocalRangeBegin;
160 I.EndLoc = LocalRangeEnd;
John McCall0b7e6782011-03-24 11:26:52 +0000161 I.Fun.AttrList = 0;
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000162 I.Fun.hasPrototype = hasProto;
163 I.Fun.isVariadic = isVariadic;
164 I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding();
165 I.Fun.DeleteArgInfo = false;
166 I.Fun.TypeQuals = TypeQuals;
167 I.Fun.NumArgs = NumArgs;
168 I.Fun.ArgInfo = 0;
Douglas Gregor83f51722011-01-26 03:43:54 +0000169 I.Fun.RefQualifierIsLValueRef = RefQualifierIsLvalueRef;
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000170 I.Fun.RefQualifierLoc = RefQualifierLoc.getRawEncoding();
171 I.Fun.ExceptionSpecType = ESpecType;
172 I.Fun.ExceptionSpecLoc = ESpecLoc.getRawEncoding();
173 I.Fun.NumExceptions = 0;
174 I.Fun.Exceptions = 0;
175 I.Fun.NoexceptExpr = 0;
Douglas Gregordab60ad2010-10-01 18:44:50 +0000176 I.Fun.TrailingReturnType = TrailingReturnType.getAsOpaquePtr();
Sebastian Redl7dc81342009-04-29 17:30:04 +0000177
Chris Lattner5af2f352009-01-20 19:11:22 +0000178 // new[] an argument array if needed.
179 if (NumArgs) {
180 // If the 'InlineParams' in Declarator is unused and big enough, put our
181 // parameter list there (in an effort to avoid new/delete traffic). If it
182 // is already used (consider a function returning a function pointer) or too
183 // small (function taking too many arguments), go to the heap.
Mike Stump1eb44332009-09-09 15:08:12 +0000184 if (!TheDeclarator.InlineParamsUsed &&
Chris Lattner5af2f352009-01-20 19:11:22 +0000185 NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
186 I.Fun.ArgInfo = TheDeclarator.InlineParams;
187 I.Fun.DeleteArgInfo = false;
188 TheDeclarator.InlineParamsUsed = true;
189 } else {
190 I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs];
191 I.Fun.DeleteArgInfo = true;
192 }
193 memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs);
194 }
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000195
196 // Check what exception specification information we should actually store.
197 switch (ESpecType) {
198 default: break; // By default, save nothing.
199 case EST_Dynamic:
200 // new[] an exception array if needed
201 if (NumExceptions) {
202 I.Fun.NumExceptions = NumExceptions;
203 I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions];
204 for (unsigned i = 0; i != NumExceptions; ++i) {
205 I.Fun.Exceptions[i].Ty = Exceptions[i];
206 I.Fun.Exceptions[i].Range = ExceptionRanges[i];
207 }
Sebastian Redlef65f062009-05-29 18:02:33 +0000208 }
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000209 break;
210
211 case EST_ComputedNoexcept:
212 I.Fun.NoexceptExpr = NoexceptExpr;
213 break;
Sebastian Redl7dc81342009-04-29 17:30:04 +0000214 }
Chris Lattner5af2f352009-01-20 19:11:22 +0000215 return I;
216}
Chris Lattner254be6a2008-11-22 08:32:36 +0000217
Douglas Gregor555f57e2011-06-25 00:56:27 +0000218bool Declarator::isDeclarationOfFunction() const {
219 if (isFunctionDeclarator())
220 return true;
221
222 switch (DS.getTypeSpecType()) {
223 case TST_auto:
224 case TST_bool:
225 case TST_char:
226 case TST_char16:
227 case TST_char32:
228 case TST_class:
229 case TST_decimal128:
230 case TST_decimal32:
231 case TST_decimal64:
232 case TST_double:
233 case TST_enum:
234 case TST_error:
235 case TST_float:
236 case TST_int:
237 case TST_struct:
238 case TST_union:
239 case TST_unknown_anytype:
240 case TST_unspecified:
241 case TST_void:
242 case TST_wchar:
243 return false;
244
245 case TST_decltype:
246 case TST_typeofExpr:
247 if (Expr *E = DS.getRepAsExpr())
248 return E->getType()->isFunctionType();
249 return false;
250
251 case TST_underlyingType:
252 case TST_typename:
253 case TST_typeofType: {
254 QualType QT = DS.getRepAsType().get();
255 if (QT.isNull())
256 return false;
257
258 if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT))
259 QT = LIT->getType();
260
261 if (QT.isNull())
262 return false;
263
264 return QT->isFunctionType();
265 }
266 }
267
268 return false;
269}
270
Reid Spencer5f016e22007-07-11 17:01:13 +0000271/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
Chris Lattner2a327d12009-02-27 18:35:46 +0000272/// declaration specifier includes.
Reid Spencer5f016e22007-07-11 17:01:13 +0000273///
274unsigned DeclSpec::getParsedSpecifiers() const {
275 unsigned Res = 0;
276 if (StorageClassSpec != SCS_unspecified ||
277 SCS_thread_specified)
278 Res |= PQ_StorageClassSpecifier;
Mike Stumpd4204332008-06-19 19:52:46 +0000279
Reid Spencer5f016e22007-07-11 17:01:13 +0000280 if (TypeQualifiers != TQ_unspecified)
281 Res |= PQ_TypeQualifier;
Mike Stump1eb44332009-09-09 15:08:12 +0000282
Reid Spencer5f016e22007-07-11 17:01:13 +0000283 if (hasTypeSpecifier())
284 Res |= PQ_TypeSpecifier;
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Douglas Gregorb48fe382008-10-31 09:07:45 +0000286 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
Reid Spencer5f016e22007-07-11 17:01:13 +0000287 Res |= PQ_FunctionSpecifier;
288 return Res;
289}
290
John McCallfec54012009-08-03 20:12:06 +0000291template <class T> static bool BadSpecifier(T TNew, T TPrev,
292 const char *&PrevSpec,
293 unsigned &DiagID) {
John McCall32d335e2009-08-03 18:47:27 +0000294 PrevSpec = DeclSpec::getSpecifierName(TPrev);
John McCallfec54012009-08-03 20:12:06 +0000295 DiagID = (TNew == TPrev ? diag::ext_duplicate_declspec
296 : diag::err_invalid_decl_spec_combination);
John McCall32d335e2009-08-03 18:47:27 +0000297 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000298}
John McCall32d335e2009-08-03 18:47:27 +0000299
Reid Spencer5f016e22007-07-11 17:01:13 +0000300const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
301 switch (S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000302 case DeclSpec::SCS_unspecified: return "unspecified";
303 case DeclSpec::SCS_typedef: return "typedef";
304 case DeclSpec::SCS_extern: return "extern";
305 case DeclSpec::SCS_static: return "static";
306 case DeclSpec::SCS_auto: return "auto";
307 case DeclSpec::SCS_register: return "register";
Eli Friedman63054b32009-04-19 20:27:55 +0000308 case DeclSpec::SCS_private_extern: return "__private_extern__";
Sebastian Redl669d5d72008-11-14 23:42:31 +0000309 case DeclSpec::SCS_mutable: return "mutable";
Reid Spencer5f016e22007-07-11 17:01:13 +0000310 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000311 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000312}
313
John McCall32d335e2009-08-03 18:47:27 +0000314const char *DeclSpec::getSpecifierName(TSW W) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000315 switch (W) {
John McCall32d335e2009-08-03 18:47:27 +0000316 case TSW_unspecified: return "unspecified";
317 case TSW_short: return "short";
318 case TSW_long: return "long";
319 case TSW_longlong: return "long long";
Reid Spencer5f016e22007-07-11 17:01:13 +0000320 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000321 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000322}
323
John McCall32d335e2009-08-03 18:47:27 +0000324const char *DeclSpec::getSpecifierName(TSC C) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000325 switch (C) {
John McCall32d335e2009-08-03 18:47:27 +0000326 case TSC_unspecified: return "unspecified";
327 case TSC_imaginary: return "imaginary";
328 case TSC_complex: return "complex";
Reid Spencer5f016e22007-07-11 17:01:13 +0000329 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000330 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000331}
332
333
John McCall32d335e2009-08-03 18:47:27 +0000334const char *DeclSpec::getSpecifierName(TSS S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000335 switch (S) {
John McCall32d335e2009-08-03 18:47:27 +0000336 case TSS_unspecified: return "unspecified";
337 case TSS_signed: return "signed";
338 case TSS_unsigned: return "unsigned";
Reid Spencer5f016e22007-07-11 17:01:13 +0000339 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000340 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000341}
342
343const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
344 switch (T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000345 case DeclSpec::TST_unspecified: return "unspecified";
346 case DeclSpec::TST_void: return "void";
347 case DeclSpec::TST_char: return "char";
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000348 case DeclSpec::TST_wchar: return "wchar_t";
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000349 case DeclSpec::TST_char16: return "char16_t";
350 case DeclSpec::TST_char32: return "char32_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000351 case DeclSpec::TST_int: return "int";
352 case DeclSpec::TST_float: return "float";
353 case DeclSpec::TST_double: return "double";
354 case DeclSpec::TST_bool: return "_Bool";
355 case DeclSpec::TST_decimal32: return "_Decimal32";
356 case DeclSpec::TST_decimal64: return "_Decimal64";
357 case DeclSpec::TST_decimal128: return "_Decimal128";
358 case DeclSpec::TST_enum: return "enum";
Chris Lattner99dc9142008-04-13 18:59:07 +0000359 case DeclSpec::TST_class: return "class";
Reid Spencer5f016e22007-07-11 17:01:13 +0000360 case DeclSpec::TST_union: return "union";
361 case DeclSpec::TST_struct: return "struct";
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000362 case DeclSpec::TST_typename: return "type-name";
Steve Naroffd1861fd2007-07-31 12:34:36 +0000363 case DeclSpec::TST_typeofType:
364 case DeclSpec::TST_typeofExpr: return "typeof";
John McCall32d335e2009-08-03 18:47:27 +0000365 case DeclSpec::TST_auto: return "auto";
366 case DeclSpec::TST_decltype: return "(decltype)";
Sean Huntca63c202011-05-24 22:41:36 +0000367 case DeclSpec::TST_underlyingType: return "__underlying_type";
John McCalla5fc4722011-04-09 22:50:59 +0000368 case DeclSpec::TST_unknown_anytype: return "__unknown_anytype";
John McCall32d335e2009-08-03 18:47:27 +0000369 case DeclSpec::TST_error: return "(error)";
Reid Spencer5f016e22007-07-11 17:01:13 +0000370 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000371 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000372}
373
John McCall32d335e2009-08-03 18:47:27 +0000374const char *DeclSpec::getSpecifierName(TQ T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000375 switch (T) {
John McCall32d335e2009-08-03 18:47:27 +0000376 case DeclSpec::TQ_unspecified: return "unspecified";
377 case DeclSpec::TQ_const: return "const";
378 case DeclSpec::TQ_restrict: return "restrict";
379 case DeclSpec::TQ_volatile: return "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +0000380 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000381 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000382}
383
384bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000385 const char *&PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +0000386 unsigned &DiagID,
387 const LangOptions &Lang) {
388 // OpenCL prohibits extern, auto, register, and static
389 // It seems sensible to prohibit private_extern too
390 if (Lang.OpenCL) {
391 switch (S) {
392 case SCS_extern:
393 case SCS_private_extern:
394 case SCS_auto:
395 case SCS_register:
396 case SCS_static:
397 DiagID = diag::err_not_opencl_storage_class_specifier;
398 PrevSpec = getSpecifierName(S);
399 return true;
400 default:
401 break;
402 }
403 }
404
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000405 if (StorageClassSpec != SCS_unspecified) {
406 // Changing storage class is allowed only if the previous one
407 // was the 'extern' that is part of a linkage specification and
408 // the new storage class is 'typedef'.
409 if (!(SCS_extern_in_linkage_spec &&
410 StorageClassSpec == SCS_extern &&
411 S == SCS_typedef))
412 return BadSpecifier(S, (SCS)StorageClassSpec, PrevSpec, DiagID);
413 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000414 StorageClassSpec = S;
415 StorageClassSpecLoc = Loc;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000416 assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield");
Reid Spencer5f016e22007-07-11 17:01:13 +0000417 return false;
418}
419
Mike Stump1eb44332009-09-09 15:08:12 +0000420bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000421 const char *&PrevSpec,
422 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000423 if (SCS_thread_specified) {
424 PrevSpec = "__thread";
John McCallfec54012009-08-03 20:12:06 +0000425 DiagID = diag::ext_duplicate_declspec;
Reid Spencer5f016e22007-07-11 17:01:13 +0000426 return true;
427 }
428 SCS_thread_specified = true;
429 SCS_threadLoc = Loc;
430 return false;
431}
432
Reid Spencer5f016e22007-07-11 17:01:13 +0000433/// These methods set the specified attribute of the DeclSpec, but return true
434/// and ignore the request if invalid (e.g. "extern" then "auto" is
435/// specified).
436bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000437 const char *&PrevSpec,
438 unsigned &DiagID) {
Abramo Bagnara2553eaf2011-03-06 22:21:56 +0000439 // Overwrite TSWLoc only if TypeSpecWidth was unspecified, so that
440 // for 'long long' we will keep the source location of the first 'long'.
441 if (TypeSpecWidth == TSW_unspecified)
442 TSWLoc = Loc;
443 // Allow turning long -> long long.
444 else if (W != TSW_longlong || TypeSpecWidth != TSW_long)
John McCallfec54012009-08-03 20:12:06 +0000445 return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000446 TypeSpecWidth = W;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000447 if (TypeAltiVecVector && !TypeAltiVecBool &&
448 ((TypeSpecWidth == TSW_long) || (TypeSpecWidth == TSW_longlong))) {
John Thompson82287d12010-02-05 00:12:22 +0000449 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
450 DiagID = diag::warn_vector_long_decl_spec_combination;
451 return true;
452 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000453 return false;
454}
455
Mike Stump1eb44332009-09-09 15:08:12 +0000456bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000457 const char *&PrevSpec,
458 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000459 if (TypeSpecComplex != TSC_unspecified)
John McCallfec54012009-08-03 20:12:06 +0000460 return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000461 TypeSpecComplex = C;
462 TSCLoc = Loc;
463 return false;
464}
465
Mike Stump1eb44332009-09-09 15:08:12 +0000466bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000467 const char *&PrevSpec,
468 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000469 if (TypeSpecSign != TSS_unspecified)
John McCallfec54012009-08-03 20:12:06 +0000470 return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000471 TypeSpecSign = S;
472 TSSLoc = Loc;
473 return false;
474}
475
476bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000477 const char *&PrevSpec,
478 unsigned &DiagID,
John McCallb3d87482010-08-24 05:47:05 +0000479 ParsedType Rep) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000480 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep);
481}
482
483bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
484 SourceLocation TagNameLoc,
485 const char *&PrevSpec,
486 unsigned &DiagID,
487 ParsedType Rep) {
John McCallb3d87482010-08-24 05:47:05 +0000488 assert(isTypeRep(T) && "T does not store a type");
489 assert(Rep && "no type provided!");
490 if (TypeSpecType != TST_unspecified) {
491 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
492 DiagID = diag::err_invalid_decl_spec_combination;
493 return true;
494 }
495 TypeSpecType = T;
496 TypeRep = Rep;
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000497 TSTLoc = TagKwLoc;
498 TSTNameLoc = TagNameLoc;
John McCallb3d87482010-08-24 05:47:05 +0000499 TypeSpecOwned = false;
500 return false;
501}
502
503bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
504 const char *&PrevSpec,
505 unsigned &DiagID,
506 Expr *Rep) {
507 assert(isExprRep(T) && "T does not store an expr");
508 assert(Rep && "no expression provided!");
509 if (TypeSpecType != TST_unspecified) {
510 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
511 DiagID = diag::err_invalid_decl_spec_combination;
512 return true;
513 }
514 TypeSpecType = T;
515 ExprRep = Rep;
516 TSTLoc = Loc;
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000517 TSTNameLoc = Loc;
John McCallb3d87482010-08-24 05:47:05 +0000518 TypeSpecOwned = false;
519 return false;
520}
521
522bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
523 const char *&PrevSpec,
524 unsigned &DiagID,
525 Decl *Rep, bool Owned) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000526 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep, Owned);
527}
528
529bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
530 SourceLocation TagNameLoc,
531 const char *&PrevSpec,
532 unsigned &DiagID,
533 Decl *Rep, bool Owned) {
John McCallb3d87482010-08-24 05:47:05 +0000534 assert(isDeclRep(T) && "T does not store a decl");
535 // Unlike the other cases, we don't assert that we actually get a decl.
536
537 if (TypeSpecType != TST_unspecified) {
538 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
539 DiagID = diag::err_invalid_decl_spec_combination;
540 return true;
541 }
542 TypeSpecType = T;
543 DeclRep = Rep;
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000544 TSTLoc = TagKwLoc;
545 TSTNameLoc = TagNameLoc;
John McCallb3d87482010-08-24 05:47:05 +0000546 TypeSpecOwned = Owned;
547 return false;
548}
549
550bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
551 const char *&PrevSpec,
552 unsigned &DiagID) {
553 assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) &&
554 "rep required for these type-spec kinds!");
John McCallfec54012009-08-03 20:12:06 +0000555 if (TypeSpecType != TST_unspecified) {
556 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
557 DiagID = diag::err_invalid_decl_spec_combination;
558 return true;
559 }
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000560 TSTLoc = Loc;
561 TSTNameLoc = Loc;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000562 if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) {
563 TypeAltiVecBool = true;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000564 return false;
565 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000566 TypeSpecType = T;
John McCallb3d87482010-08-24 05:47:05 +0000567 TypeSpecOwned = false;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000568 if (TypeAltiVecVector && !TypeAltiVecBool && (TypeSpecType == TST_double)) {
John Thompson82287d12010-02-05 00:12:22 +0000569 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
Chris Lattner788b0fd2010-06-23 06:00:24 +0000570 DiagID = diag::err_invalid_vector_decl_spec;
John Thompson82287d12010-02-05 00:12:22 +0000571 return true;
572 }
573 return false;
574}
575
576bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
577 const char *&PrevSpec, unsigned &DiagID) {
578 if (TypeSpecType != TST_unspecified) {
579 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
580 DiagID = diag::err_invalid_vector_decl_spec_combination;
581 return true;
582 }
583 TypeAltiVecVector = isAltiVecVector;
584 AltiVecLoc = Loc;
585 return false;
586}
587
588bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
589 const char *&PrevSpec, unsigned &DiagID) {
Chris Lattner788b0fd2010-06-23 06:00:24 +0000590 if (!TypeAltiVecVector || TypeAltiVecPixel ||
591 (TypeSpecType != TST_unspecified)) {
John Thompson82287d12010-02-05 00:12:22 +0000592 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
593 DiagID = diag::err_invalid_pixel_decl_spec_combination;
594 return true;
595 }
John Thompson82287d12010-02-05 00:12:22 +0000596 TypeAltiVecPixel = isAltiVecPixel;
597 TSTLoc = Loc;
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000598 TSTNameLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000599 return false;
600}
601
Douglas Gregorddc29e12009-02-06 22:42:48 +0000602bool DeclSpec::SetTypeSpecError() {
603 TypeSpecType = TST_error;
John McCall9e46b8c2010-08-26 17:22:34 +0000604 TypeSpecOwned = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000605 TSTLoc = SourceLocation();
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000606 TSTNameLoc = SourceLocation();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000607 return false;
608}
609
Reid Spencer5f016e22007-07-11 17:01:13 +0000610bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000611 unsigned &DiagID, const LangOptions &Lang) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000612 // Duplicates turn into warnings pre-C99.
613 if ((TypeQualifiers & T) && !Lang.C99)
John McCallfec54012009-08-03 20:12:06 +0000614 return BadSpecifier(T, T, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000615 TypeQualifiers |= T;
Mike Stump1eb44332009-09-09 15:08:12 +0000616
Reid Spencer5f016e22007-07-11 17:01:13 +0000617 switch (T) {
618 default: assert(0 && "Unknown type qualifier!");
619 case TQ_const: TQ_constLoc = Loc; break;
620 case TQ_restrict: TQ_restrictLoc = Loc; break;
621 case TQ_volatile: TQ_volatileLoc = Loc; break;
622 }
623 return false;
624}
625
John McCallfec54012009-08-03 20:12:06 +0000626bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
627 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000628 // 'inline inline' is ok.
629 FS_inline_specified = true;
630 FS_inlineLoc = Loc;
631 return false;
632}
633
John McCallfec54012009-08-03 20:12:06 +0000634bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec,
635 unsigned &DiagID) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000636 // 'virtual virtual' is ok.
637 FS_virtual_specified = true;
638 FS_virtualLoc = Loc;
639 return false;
640}
641
John McCallfec54012009-08-03 20:12:06 +0000642bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec,
643 unsigned &DiagID) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000644 // 'explicit explicit' is ok.
645 FS_explicit_specified = true;
646 FS_explicitLoc = Loc;
647 return false;
648}
649
John McCallfec54012009-08-03 20:12:06 +0000650bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
651 unsigned &DiagID) {
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000652 if (Friend_specified) {
653 PrevSpec = "friend";
John McCallfec54012009-08-03 20:12:06 +0000654 DiagID = diag::ext_duplicate_declspec;
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000655 return true;
656 }
John McCallfec54012009-08-03 20:12:06 +0000657
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000658 Friend_specified = true;
659 FriendLoc = Loc;
660 return false;
661}
Reid Spencer5f016e22007-07-11 17:01:13 +0000662
Sebastian Redl2ac67232009-11-05 15:47:02 +0000663bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
664 unsigned &DiagID) {
665 // 'constexpr constexpr' is ok.
666 Constexpr_specified = true;
667 ConstexprLoc = Loc;
668 return false;
669}
670
John McCalld226f652010-08-21 09:40:31 +0000671void DeclSpec::setProtocolQualifiers(Decl * const *Protos,
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000672 unsigned NP,
673 SourceLocation *ProtoLocs,
674 SourceLocation LAngleLoc) {
675 if (NP == 0) return;
John McCalld226f652010-08-21 09:40:31 +0000676 ProtocolQualifiers = new Decl*[NP];
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000677 ProtocolLocs = new SourceLocation[NP];
John McCalld226f652010-08-21 09:40:31 +0000678 memcpy((void*)ProtocolQualifiers, Protos, sizeof(Decl*)*NP);
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000679 memcpy(ProtocolLocs, ProtoLocs, sizeof(SourceLocation)*NP);
680 NumProtocolQualifiers = NP;
681 ProtocolLAngleLoc = LAngleLoc;
682}
683
Douglas Gregorddf889a2010-01-18 18:04:31 +0000684void DeclSpec::SaveWrittenBuiltinSpecs() {
685 writtenBS.Sign = getTypeSpecSign();
686 writtenBS.Width = getTypeSpecWidth();
687 writtenBS.Type = getTypeSpecType();
688 // Search the list of attributes for the presence of a mode attribute.
689 writtenBS.ModeAttr = false;
John McCall7f040a92010-12-24 02:08:15 +0000690 AttributeList* attrs = getAttributes().getList();
Douglas Gregorddf889a2010-01-18 18:04:31 +0000691 while (attrs) {
692 if (attrs->getKind() == AttributeList::AT_mode) {
693 writtenBS.ModeAttr = true;
694 break;
695 }
696 attrs = attrs->getNext();
697 }
698}
699
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000700void DeclSpec::SaveStorageSpecifierAsWritten() {
701 if (SCS_extern_in_linkage_spec && StorageClassSpec == SCS_extern)
702 // If 'extern' is part of a linkage specification,
703 // then it is not a storage class "as written".
704 StorageClassSpecAsWritten = SCS_unspecified;
705 else
706 StorageClassSpecAsWritten = StorageClassSpec;
707}
708
Reid Spencer5f016e22007-07-11 17:01:13 +0000709/// Finish - This does final analysis of the declspec, rejecting things like
710/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
711/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
712/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000713void DeclSpec::Finish(Diagnostic &D, Preprocessor &PP) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000714 // Before possibly changing their values, save specs as written.
715 SaveWrittenBuiltinSpecs();
Douglas Gregor16573fa2010-04-19 22:54:31 +0000716 SaveStorageSpecifierAsWritten();
Douglas Gregorddf889a2010-01-18 18:04:31 +0000717
Reid Spencer5f016e22007-07-11 17:01:13 +0000718 // Check the type specifier components first.
719
Chris Lattner788b0fd2010-06-23 06:00:24 +0000720 // Validate and finalize AltiVec vector declspec.
721 if (TypeAltiVecVector) {
722 if (TypeAltiVecBool) {
723 // Sign specifiers are not allowed with vector bool. (PIM 2.1)
724 if (TypeSpecSign != TSS_unspecified) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000725 Diag(D, TSSLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner788b0fd2010-06-23 06:00:24 +0000726 << getSpecifierName((TSS)TypeSpecSign);
727 }
728
729 // Only char/int are valid with vector bool. (PIM 2.1)
Duncan Sands2e964a922010-06-23 19:34:52 +0000730 if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) &&
731 (TypeSpecType != TST_int)) || TypeAltiVecPixel) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000732 Diag(D, TSTLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner788b0fd2010-06-23 06:00:24 +0000733 << (TypeAltiVecPixel ? "__pixel" :
734 getSpecifierName((TST)TypeSpecType));
735 }
736
737 // Only 'short' is valid with vector bool. (PIM 2.1)
738 if ((TypeSpecWidth != TSW_unspecified) && (TypeSpecWidth != TSW_short))
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000739 Diag(D, TSWLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner788b0fd2010-06-23 06:00:24 +0000740 << getSpecifierName((TSW)TypeSpecWidth);
741
742 // Elements of vector bool are interpreted as unsigned. (PIM 2.1)
743 if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) ||
744 (TypeSpecWidth != TSW_unspecified))
745 TypeSpecSign = TSS_unsigned;
746 }
747
748 if (TypeAltiVecPixel) {
749 //TODO: perform validation
750 TypeSpecType = TST_int;
751 TypeSpecSign = TSS_unsigned;
752 TypeSpecWidth = TSW_short;
John McCall9e46b8c2010-08-26 17:22:34 +0000753 TypeSpecOwned = false;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000754 }
755 }
756
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000757 // signed/unsigned are only valid with int/char/wchar_t.
Reid Spencer5f016e22007-07-11 17:01:13 +0000758 if (TypeSpecSign != TSS_unspecified) {
759 if (TypeSpecType == TST_unspecified)
760 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000761 else if (TypeSpecType != TST_int &&
762 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000763 Diag(D, TSSLoc, diag::err_invalid_sign_spec)
Chris Lattner254be6a2008-11-22 08:32:36 +0000764 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000765 // signed double -> double.
766 TypeSpecSign = TSS_unspecified;
767 }
768 }
769
770 // Validate the width of the type.
771 switch (TypeSpecWidth) {
772 case TSW_unspecified: break;
773 case TSW_short: // short int
774 case TSW_longlong: // long long int
775 if (TypeSpecType == TST_unspecified)
776 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
777 else if (TypeSpecType != TST_int) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000778 Diag(D, TSWLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000779 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
Chris Lattner254be6a2008-11-22 08:32:36 +0000780 : diag::err_invalid_longlong_spec)
781 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000782 TypeSpecType = TST_int;
John McCall9e46b8c2010-08-26 17:22:34 +0000783 TypeSpecOwned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000784 }
785 break;
786 case TSW_long: // long double, long int
787 if (TypeSpecType == TST_unspecified)
788 TypeSpecType = TST_int; // long -> long int.
789 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000790 Diag(D, TSWLoc, diag::err_invalid_long_spec)
Chris Lattner254be6a2008-11-22 08:32:36 +0000791 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000792 TypeSpecType = TST_int;
John McCall9e46b8c2010-08-26 17:22:34 +0000793 TypeSpecOwned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000794 }
795 break;
796 }
Mike Stump1eb44332009-09-09 15:08:12 +0000797
Reid Spencer5f016e22007-07-11 17:01:13 +0000798 // TODO: if the implementation does not implement _Complex or _Imaginary,
799 // disallow their use. Need information about the backend.
800 if (TypeSpecComplex != TSC_unspecified) {
801 if (TypeSpecType == TST_unspecified) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000802 Diag(D, TSCLoc, diag::ext_plain_complex)
Douglas Gregor849b2432010-03-31 17:46:05 +0000803 << FixItHint::CreateInsertion(
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000804 PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
805 " double");
Reid Spencer5f016e22007-07-11 17:01:13 +0000806 TypeSpecType = TST_double; // _Complex -> _Complex double.
807 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
808 // Note that this intentionally doesn't include _Complex _Bool.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000809 Diag(D, TSTLoc, diag::ext_integer_complex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000810 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000811 Diag(D, TSCLoc, diag::err_invalid_complex_spec)
Chris Lattner254be6a2008-11-22 08:32:36 +0000812 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000813 TypeSpecComplex = TSC_unspecified;
814 }
815 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000816
John McCall67d1a672009-08-06 02:15:43 +0000817 // C++ [class.friend]p6:
818 // No storage-class-specifier shall appear in the decl-specifier-seq
819 // of a friend declaration.
820 if (isFriendSpecified() && getStorageClassSpec()) {
821 DeclSpec::SCS SC = getStorageClassSpec();
822 const char *SpecName = getSpecifierName(SC);
823
824 SourceLocation SCLoc = getStorageClassSpecLoc();
825 SourceLocation SCEndLoc = SCLoc.getFileLocWithOffset(strlen(SpecName));
826
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000827 Diag(D, SCLoc, diag::err_friend_storage_spec)
John McCall67d1a672009-08-06 02:15:43 +0000828 << SpecName
Douglas Gregor849b2432010-03-31 17:46:05 +0000829 << FixItHint::CreateRemoval(SourceRange(SCLoc, SCEndLoc));
John McCall67d1a672009-08-06 02:15:43 +0000830
831 ClearStorageClassSpecs();
832 }
833
John McCall9e46b8c2010-08-26 17:22:34 +0000834 assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType));
835
Reid Spencer5f016e22007-07-11 17:01:13 +0000836 // Okay, now we can infer the real type.
Mike Stump1eb44332009-09-09 15:08:12 +0000837
Reid Spencer5f016e22007-07-11 17:01:13 +0000838 // TODO: return "auto function" and other bad things based on the real type.
Mike Stump1eb44332009-09-09 15:08:12 +0000839
Reid Spencer5f016e22007-07-11 17:01:13 +0000840 // 'data definition has no type or storage class'?
841}
Daniel Dunbare4858a62008-08-11 03:45:03 +0000842
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000843bool DeclSpec::isMissingDeclaratorOk() {
844 TST tst = getTypeSpecType();
John McCallb3d87482010-08-24 05:47:05 +0000845 return isDeclRep(tst) && getRepAsDecl() != 0 &&
846 StorageClassSpec != DeclSpec::SCS_typedef;
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000847}
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000848
849void UnqualifiedId::clear() {
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000850 Kind = IK_Identifier;
851 Identifier = 0;
852 StartLocation = SourceLocation();
853 EndLocation = SourceLocation();
854}
855
856void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc,
857 OverloadedOperatorKind Op,
858 SourceLocation SymbolLocations[3]) {
859 Kind = IK_OperatorFunctionId;
860 StartLocation = OperatorLoc;
861 EndLocation = OperatorLoc;
862 OperatorFunctionId.Operator = Op;
863 for (unsigned I = 0; I != 3; ++I) {
864 OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding();
865
866 if (SymbolLocations[I].isValid())
867 EndLocation = SymbolLocations[I];
868 }
869}
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000870
Anders Carlssoncc54d592011-01-22 16:56:46 +0000871bool VirtSpecifiers::SetSpecifier(Specifier VS, SourceLocation Loc,
Anders Carlsson46127a92011-01-22 15:58:16 +0000872 const char *&PrevSpec) {
Douglas Gregorf5251602011-03-08 17:10:18 +0000873 LastLocation = Loc;
874
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000875 if (Specifiers & VS) {
876 PrevSpec = getSpecifierName(VS);
877 return true;
878 }
879
880 Specifiers |= VS;
881
882 switch (VS) {
883 default: assert(0 && "Unknown specifier!");
884 case VS_Override: VS_overrideLoc = Loc; break;
885 case VS_Final: VS_finalLoc = Loc; break;
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000886 }
Anders Carlsson46127a92011-01-22 15:58:16 +0000887
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000888 return false;
889}
890
Anders Carlssoncc54d592011-01-22 16:56:46 +0000891const char *VirtSpecifiers::getSpecifierName(Specifier VS) {
Anders Carlssonc46bb7d2011-01-22 15:11:37 +0000892 switch (VS) {
893 default: assert(0 && "Unknown specifier");
894 case VS_Override: return "override";
895 case VS_Final: return "final";
Anders Carlssonc46bb7d2011-01-22 15:11:37 +0000896 }
897}