blob: a61188c3c47a337c1ece5e7836516468342c1f49 [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
John McCall9dc71d22011-07-06 06:57:57 +0000129SourceLocation CXXScopeSpec::getLastQualifierNameLoc() const {
130 if (!Builder.getRepresentation())
131 return SourceLocation();
132 return Builder.getTemporary().getLocalBeginLoc();
133}
134
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000135NestedNameSpecifierLoc
136CXXScopeSpec::getWithLocInContext(ASTContext &Context) const {
Douglas Gregorb46ae392011-03-03 21:48:55 +0000137 if (!Builder.getRepresentation())
Douglas Gregorc34348a2011-02-24 17:54:50 +0000138 return NestedNameSpecifierLoc();
139
Douglas Gregor5f791bb2011-02-28 23:58:31 +0000140 return Builder.getWithLocInContext(Context);
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000141}
142
Chris Lattner5af2f352009-01-20 19:11:22 +0000143/// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function.
144/// "TheDeclarator" is the declarator that this will be added to.
John McCall0b7e6782011-03-24 11:26:52 +0000145DeclaratorChunk DeclaratorChunk::getFunction(bool hasProto, bool isVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +0000146 SourceLocation EllipsisLoc,
Chris Lattner5af2f352009-01-20 19:11:22 +0000147 ParamInfo *ArgInfo,
148 unsigned NumArgs,
149 unsigned TypeQuals,
Douglas Gregor83f51722011-01-26 03:43:54 +0000150 bool RefQualifierIsLvalueRef,
151 SourceLocation RefQualifierLoc,
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000152 ExceptionSpecificationType
153 ESpecType,
154 SourceLocation ESpecLoc,
John McCallb3d87482010-08-24 05:47:05 +0000155 ParsedType *Exceptions,
Sebastian Redlef65f062009-05-29 18:02:33 +0000156 SourceRange *ExceptionRanges,
Sebastian Redl7dc81342009-04-29 17:30:04 +0000157 unsigned NumExceptions,
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000158 Expr *NoexceptExpr,
Abramo Bagnara796aa442011-03-12 11:17:06 +0000159 SourceLocation LocalRangeBegin,
160 SourceLocation LocalRangeEnd,
Douglas Gregordab60ad2010-10-01 18:44:50 +0000161 Declarator &TheDeclarator,
162 ParsedType TrailingReturnType) {
Chris Lattner5af2f352009-01-20 19:11:22 +0000163 DeclaratorChunk I;
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000164 I.Kind = Function;
Abramo Bagnara796aa442011-03-12 11:17:06 +0000165 I.Loc = LocalRangeBegin;
166 I.EndLoc = LocalRangeEnd;
John McCall0b7e6782011-03-24 11:26:52 +0000167 I.Fun.AttrList = 0;
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000168 I.Fun.hasPrototype = hasProto;
169 I.Fun.isVariadic = isVariadic;
170 I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding();
171 I.Fun.DeleteArgInfo = false;
172 I.Fun.TypeQuals = TypeQuals;
173 I.Fun.NumArgs = NumArgs;
174 I.Fun.ArgInfo = 0;
Douglas Gregor83f51722011-01-26 03:43:54 +0000175 I.Fun.RefQualifierIsLValueRef = RefQualifierIsLvalueRef;
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000176 I.Fun.RefQualifierLoc = RefQualifierLoc.getRawEncoding();
177 I.Fun.ExceptionSpecType = ESpecType;
178 I.Fun.ExceptionSpecLoc = ESpecLoc.getRawEncoding();
179 I.Fun.NumExceptions = 0;
180 I.Fun.Exceptions = 0;
181 I.Fun.NoexceptExpr = 0;
Douglas Gregordab60ad2010-10-01 18:44:50 +0000182 I.Fun.TrailingReturnType = TrailingReturnType.getAsOpaquePtr();
Sebastian Redl7dc81342009-04-29 17:30:04 +0000183
Chris Lattner5af2f352009-01-20 19:11:22 +0000184 // new[] an argument array if needed.
185 if (NumArgs) {
186 // If the 'InlineParams' in Declarator is unused and big enough, put our
187 // parameter list there (in an effort to avoid new/delete traffic). If it
188 // is already used (consider a function returning a function pointer) or too
189 // small (function taking too many arguments), go to the heap.
Mike Stump1eb44332009-09-09 15:08:12 +0000190 if (!TheDeclarator.InlineParamsUsed &&
Chris Lattner5af2f352009-01-20 19:11:22 +0000191 NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
192 I.Fun.ArgInfo = TheDeclarator.InlineParams;
193 I.Fun.DeleteArgInfo = false;
194 TheDeclarator.InlineParamsUsed = true;
195 } else {
196 I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs];
197 I.Fun.DeleteArgInfo = true;
198 }
199 memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs);
200 }
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000201
202 // Check what exception specification information we should actually store.
203 switch (ESpecType) {
204 default: break; // By default, save nothing.
205 case EST_Dynamic:
206 // new[] an exception array if needed
207 if (NumExceptions) {
208 I.Fun.NumExceptions = NumExceptions;
209 I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions];
210 for (unsigned i = 0; i != NumExceptions; ++i) {
211 I.Fun.Exceptions[i].Ty = Exceptions[i];
212 I.Fun.Exceptions[i].Range = ExceptionRanges[i];
213 }
Sebastian Redlef65f062009-05-29 18:02:33 +0000214 }
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000215 break;
216
217 case EST_ComputedNoexcept:
218 I.Fun.NoexceptExpr = NoexceptExpr;
219 break;
Sebastian Redl7dc81342009-04-29 17:30:04 +0000220 }
Chris Lattner5af2f352009-01-20 19:11:22 +0000221 return I;
222}
Chris Lattner254be6a2008-11-22 08:32:36 +0000223
Douglas Gregor555f57e2011-06-25 00:56:27 +0000224bool Declarator::isDeclarationOfFunction() const {
Richard Smith1ab0d902011-06-25 02:28:38 +0000225 for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) {
226 switch (DeclTypeInfo[i].Kind) {
227 case DeclaratorChunk::Function:
228 return true;
229 case DeclaratorChunk::Paren:
230 continue;
231 case DeclaratorChunk::Pointer:
232 case DeclaratorChunk::Reference:
233 case DeclaratorChunk::Array:
234 case DeclaratorChunk::BlockPointer:
235 case DeclaratorChunk::MemberPointer:
236 return false;
237 }
238 llvm_unreachable("Invalid type chunk");
239 return false;
240 }
Douglas Gregor555f57e2011-06-25 00:56:27 +0000241
242 switch (DS.getTypeSpecType()) {
243 case TST_auto:
244 case TST_bool:
245 case TST_char:
246 case TST_char16:
247 case TST_char32:
248 case TST_class:
249 case TST_decimal128:
250 case TST_decimal32:
251 case TST_decimal64:
252 case TST_double:
253 case TST_enum:
254 case TST_error:
255 case TST_float:
256 case TST_int:
257 case TST_struct:
258 case TST_union:
259 case TST_unknown_anytype:
260 case TST_unspecified:
261 case TST_void:
262 case TST_wchar:
263 return false;
264
265 case TST_decltype:
266 case TST_typeofExpr:
267 if (Expr *E = DS.getRepAsExpr())
268 return E->getType()->isFunctionType();
269 return false;
270
271 case TST_underlyingType:
272 case TST_typename:
273 case TST_typeofType: {
274 QualType QT = DS.getRepAsType().get();
275 if (QT.isNull())
276 return false;
277
278 if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT))
279 QT = LIT->getType();
280
281 if (QT.isNull())
282 return false;
283
284 return QT->isFunctionType();
285 }
286 }
287
288 return false;
289}
290
Reid Spencer5f016e22007-07-11 17:01:13 +0000291/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
Chris Lattner2a327d12009-02-27 18:35:46 +0000292/// declaration specifier includes.
Reid Spencer5f016e22007-07-11 17:01:13 +0000293///
294unsigned DeclSpec::getParsedSpecifiers() const {
295 unsigned Res = 0;
296 if (StorageClassSpec != SCS_unspecified ||
297 SCS_thread_specified)
298 Res |= PQ_StorageClassSpecifier;
Mike Stumpd4204332008-06-19 19:52:46 +0000299
Reid Spencer5f016e22007-07-11 17:01:13 +0000300 if (TypeQualifiers != TQ_unspecified)
301 Res |= PQ_TypeQualifier;
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Reid Spencer5f016e22007-07-11 17:01:13 +0000303 if (hasTypeSpecifier())
304 Res |= PQ_TypeSpecifier;
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Douglas Gregorb48fe382008-10-31 09:07:45 +0000306 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
Reid Spencer5f016e22007-07-11 17:01:13 +0000307 Res |= PQ_FunctionSpecifier;
308 return Res;
309}
310
John McCallfec54012009-08-03 20:12:06 +0000311template <class T> static bool BadSpecifier(T TNew, T TPrev,
312 const char *&PrevSpec,
313 unsigned &DiagID) {
John McCall32d335e2009-08-03 18:47:27 +0000314 PrevSpec = DeclSpec::getSpecifierName(TPrev);
John McCallfec54012009-08-03 20:12:06 +0000315 DiagID = (TNew == TPrev ? diag::ext_duplicate_declspec
316 : diag::err_invalid_decl_spec_combination);
John McCall32d335e2009-08-03 18:47:27 +0000317 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000318}
John McCall32d335e2009-08-03 18:47:27 +0000319
Reid Spencer5f016e22007-07-11 17:01:13 +0000320const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
321 switch (S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000322 case DeclSpec::SCS_unspecified: return "unspecified";
323 case DeclSpec::SCS_typedef: return "typedef";
324 case DeclSpec::SCS_extern: return "extern";
325 case DeclSpec::SCS_static: return "static";
326 case DeclSpec::SCS_auto: return "auto";
327 case DeclSpec::SCS_register: return "register";
Eli Friedman63054b32009-04-19 20:27:55 +0000328 case DeclSpec::SCS_private_extern: return "__private_extern__";
Sebastian Redl669d5d72008-11-14 23:42:31 +0000329 case DeclSpec::SCS_mutable: return "mutable";
Reid Spencer5f016e22007-07-11 17:01:13 +0000330 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000331 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000332}
333
John McCall32d335e2009-08-03 18:47:27 +0000334const char *DeclSpec::getSpecifierName(TSW W) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000335 switch (W) {
John McCall32d335e2009-08-03 18:47:27 +0000336 case TSW_unspecified: return "unspecified";
337 case TSW_short: return "short";
338 case TSW_long: return "long";
339 case TSW_longlong: return "long long";
Reid Spencer5f016e22007-07-11 17:01:13 +0000340 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000341 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000342}
343
John McCall32d335e2009-08-03 18:47:27 +0000344const char *DeclSpec::getSpecifierName(TSC C) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000345 switch (C) {
John McCall32d335e2009-08-03 18:47:27 +0000346 case TSC_unspecified: return "unspecified";
347 case TSC_imaginary: return "imaginary";
348 case TSC_complex: return "complex";
Reid Spencer5f016e22007-07-11 17:01:13 +0000349 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000350 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000351}
352
353
John McCall32d335e2009-08-03 18:47:27 +0000354const char *DeclSpec::getSpecifierName(TSS S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000355 switch (S) {
John McCall32d335e2009-08-03 18:47:27 +0000356 case TSS_unspecified: return "unspecified";
357 case TSS_signed: return "signed";
358 case TSS_unsigned: return "unsigned";
Reid Spencer5f016e22007-07-11 17:01:13 +0000359 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000360 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000361}
362
363const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
364 switch (T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000365 case DeclSpec::TST_unspecified: return "unspecified";
366 case DeclSpec::TST_void: return "void";
367 case DeclSpec::TST_char: return "char";
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000368 case DeclSpec::TST_wchar: return "wchar_t";
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000369 case DeclSpec::TST_char16: return "char16_t";
370 case DeclSpec::TST_char32: return "char32_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000371 case DeclSpec::TST_int: return "int";
372 case DeclSpec::TST_float: return "float";
373 case DeclSpec::TST_double: return "double";
374 case DeclSpec::TST_bool: return "_Bool";
375 case DeclSpec::TST_decimal32: return "_Decimal32";
376 case DeclSpec::TST_decimal64: return "_Decimal64";
377 case DeclSpec::TST_decimal128: return "_Decimal128";
378 case DeclSpec::TST_enum: return "enum";
Chris Lattner99dc9142008-04-13 18:59:07 +0000379 case DeclSpec::TST_class: return "class";
Reid Spencer5f016e22007-07-11 17:01:13 +0000380 case DeclSpec::TST_union: return "union";
381 case DeclSpec::TST_struct: return "struct";
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000382 case DeclSpec::TST_typename: return "type-name";
Steve Naroffd1861fd2007-07-31 12:34:36 +0000383 case DeclSpec::TST_typeofType:
384 case DeclSpec::TST_typeofExpr: return "typeof";
John McCall32d335e2009-08-03 18:47:27 +0000385 case DeclSpec::TST_auto: return "auto";
386 case DeclSpec::TST_decltype: return "(decltype)";
Sean Huntca63c202011-05-24 22:41:36 +0000387 case DeclSpec::TST_underlyingType: return "__underlying_type";
John McCalla5fc4722011-04-09 22:50:59 +0000388 case DeclSpec::TST_unknown_anytype: return "__unknown_anytype";
John McCall32d335e2009-08-03 18:47:27 +0000389 case DeclSpec::TST_error: return "(error)";
Reid Spencer5f016e22007-07-11 17:01:13 +0000390 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000391 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000392}
393
John McCall32d335e2009-08-03 18:47:27 +0000394const char *DeclSpec::getSpecifierName(TQ T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000395 switch (T) {
John McCall32d335e2009-08-03 18:47:27 +0000396 case DeclSpec::TQ_unspecified: return "unspecified";
397 case DeclSpec::TQ_const: return "const";
398 case DeclSpec::TQ_restrict: return "restrict";
399 case DeclSpec::TQ_volatile: return "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +0000400 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000401 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000402}
403
404bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000405 const char *&PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +0000406 unsigned &DiagID,
407 const LangOptions &Lang) {
408 // OpenCL prohibits extern, auto, register, and static
409 // It seems sensible to prohibit private_extern too
410 if (Lang.OpenCL) {
411 switch (S) {
412 case SCS_extern:
413 case SCS_private_extern:
414 case SCS_auto:
415 case SCS_register:
416 case SCS_static:
417 DiagID = diag::err_not_opencl_storage_class_specifier;
418 PrevSpec = getSpecifierName(S);
419 return true;
420 default:
421 break;
422 }
423 }
424
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000425 if (StorageClassSpec != SCS_unspecified) {
426 // Changing storage class is allowed only if the previous one
427 // was the 'extern' that is part of a linkage specification and
428 // the new storage class is 'typedef'.
429 if (!(SCS_extern_in_linkage_spec &&
430 StorageClassSpec == SCS_extern &&
431 S == SCS_typedef))
432 return BadSpecifier(S, (SCS)StorageClassSpec, PrevSpec, DiagID);
433 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000434 StorageClassSpec = S;
435 StorageClassSpecLoc = Loc;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000436 assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield");
Reid Spencer5f016e22007-07-11 17:01:13 +0000437 return false;
438}
439
Mike Stump1eb44332009-09-09 15:08:12 +0000440bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000441 const char *&PrevSpec,
442 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000443 if (SCS_thread_specified) {
444 PrevSpec = "__thread";
John McCallfec54012009-08-03 20:12:06 +0000445 DiagID = diag::ext_duplicate_declspec;
Reid Spencer5f016e22007-07-11 17:01:13 +0000446 return true;
447 }
448 SCS_thread_specified = true;
449 SCS_threadLoc = Loc;
450 return false;
451}
452
Reid Spencer5f016e22007-07-11 17:01:13 +0000453/// These methods set the specified attribute of the DeclSpec, but return true
454/// and ignore the request if invalid (e.g. "extern" then "auto" is
455/// specified).
456bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000457 const char *&PrevSpec,
458 unsigned &DiagID) {
Abramo Bagnara2553eaf2011-03-06 22:21:56 +0000459 // Overwrite TSWLoc only if TypeSpecWidth was unspecified, so that
460 // for 'long long' we will keep the source location of the first 'long'.
461 if (TypeSpecWidth == TSW_unspecified)
462 TSWLoc = Loc;
463 // Allow turning long -> long long.
464 else if (W != TSW_longlong || TypeSpecWidth != TSW_long)
John McCallfec54012009-08-03 20:12:06 +0000465 return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000466 TypeSpecWidth = W;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000467 if (TypeAltiVecVector && !TypeAltiVecBool &&
468 ((TypeSpecWidth == TSW_long) || (TypeSpecWidth == TSW_longlong))) {
John Thompson82287d12010-02-05 00:12:22 +0000469 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
470 DiagID = diag::warn_vector_long_decl_spec_combination;
471 return true;
472 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000473 return false;
474}
475
Mike Stump1eb44332009-09-09 15:08:12 +0000476bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000477 const char *&PrevSpec,
478 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000479 if (TypeSpecComplex != TSC_unspecified)
John McCallfec54012009-08-03 20:12:06 +0000480 return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000481 TypeSpecComplex = C;
482 TSCLoc = Loc;
483 return false;
484}
485
Mike Stump1eb44332009-09-09 15:08:12 +0000486bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000487 const char *&PrevSpec,
488 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000489 if (TypeSpecSign != TSS_unspecified)
John McCallfec54012009-08-03 20:12:06 +0000490 return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000491 TypeSpecSign = S;
492 TSSLoc = Loc;
493 return false;
494}
495
496bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000497 const char *&PrevSpec,
498 unsigned &DiagID,
John McCallb3d87482010-08-24 05:47:05 +0000499 ParsedType Rep) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000500 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep);
501}
502
503bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
504 SourceLocation TagNameLoc,
505 const char *&PrevSpec,
506 unsigned &DiagID,
507 ParsedType Rep) {
John McCallb3d87482010-08-24 05:47:05 +0000508 assert(isTypeRep(T) && "T does not store a type");
509 assert(Rep && "no type provided!");
510 if (TypeSpecType != TST_unspecified) {
511 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
512 DiagID = diag::err_invalid_decl_spec_combination;
513 return true;
514 }
515 TypeSpecType = T;
516 TypeRep = Rep;
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000517 TSTLoc = TagKwLoc;
518 TSTNameLoc = TagNameLoc;
John McCallb3d87482010-08-24 05:47:05 +0000519 TypeSpecOwned = false;
520 return false;
521}
522
523bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
524 const char *&PrevSpec,
525 unsigned &DiagID,
526 Expr *Rep) {
527 assert(isExprRep(T) && "T does not store an expr");
528 assert(Rep && "no expression provided!");
529 if (TypeSpecType != TST_unspecified) {
530 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
531 DiagID = diag::err_invalid_decl_spec_combination;
532 return true;
533 }
534 TypeSpecType = T;
535 ExprRep = Rep;
536 TSTLoc = Loc;
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000537 TSTNameLoc = Loc;
John McCallb3d87482010-08-24 05:47:05 +0000538 TypeSpecOwned = false;
539 return false;
540}
541
542bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
543 const char *&PrevSpec,
544 unsigned &DiagID,
545 Decl *Rep, bool Owned) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000546 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep, Owned);
547}
548
549bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
550 SourceLocation TagNameLoc,
551 const char *&PrevSpec,
552 unsigned &DiagID,
553 Decl *Rep, bool Owned) {
John McCallb3d87482010-08-24 05:47:05 +0000554 assert(isDeclRep(T) && "T does not store a decl");
555 // Unlike the other cases, we don't assert that we actually get a decl.
556
557 if (TypeSpecType != TST_unspecified) {
558 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
559 DiagID = diag::err_invalid_decl_spec_combination;
560 return true;
561 }
562 TypeSpecType = T;
563 DeclRep = Rep;
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000564 TSTLoc = TagKwLoc;
565 TSTNameLoc = TagNameLoc;
John McCallb3d87482010-08-24 05:47:05 +0000566 TypeSpecOwned = Owned;
567 return false;
568}
569
570bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
571 const char *&PrevSpec,
572 unsigned &DiagID) {
573 assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) &&
574 "rep required for these type-spec kinds!");
John McCallfec54012009-08-03 20:12:06 +0000575 if (TypeSpecType != TST_unspecified) {
576 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
577 DiagID = diag::err_invalid_decl_spec_combination;
578 return true;
579 }
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000580 TSTLoc = Loc;
581 TSTNameLoc = Loc;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000582 if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) {
583 TypeAltiVecBool = true;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000584 return false;
585 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000586 TypeSpecType = T;
John McCallb3d87482010-08-24 05:47:05 +0000587 TypeSpecOwned = false;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000588 if (TypeAltiVecVector && !TypeAltiVecBool && (TypeSpecType == TST_double)) {
John Thompson82287d12010-02-05 00:12:22 +0000589 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
Chris Lattner788b0fd2010-06-23 06:00:24 +0000590 DiagID = diag::err_invalid_vector_decl_spec;
John Thompson82287d12010-02-05 00:12:22 +0000591 return true;
592 }
593 return false;
594}
595
596bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
597 const char *&PrevSpec, unsigned &DiagID) {
598 if (TypeSpecType != TST_unspecified) {
599 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
600 DiagID = diag::err_invalid_vector_decl_spec_combination;
601 return true;
602 }
603 TypeAltiVecVector = isAltiVecVector;
604 AltiVecLoc = Loc;
605 return false;
606}
607
608bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
609 const char *&PrevSpec, unsigned &DiagID) {
Chris Lattner788b0fd2010-06-23 06:00:24 +0000610 if (!TypeAltiVecVector || TypeAltiVecPixel ||
611 (TypeSpecType != TST_unspecified)) {
John Thompson82287d12010-02-05 00:12:22 +0000612 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
613 DiagID = diag::err_invalid_pixel_decl_spec_combination;
614 return true;
615 }
John Thompson82287d12010-02-05 00:12:22 +0000616 TypeAltiVecPixel = isAltiVecPixel;
617 TSTLoc = Loc;
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000618 TSTNameLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000619 return false;
620}
621
Douglas Gregorddc29e12009-02-06 22:42:48 +0000622bool DeclSpec::SetTypeSpecError() {
623 TypeSpecType = TST_error;
John McCall9e46b8c2010-08-26 17:22:34 +0000624 TypeSpecOwned = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000625 TSTLoc = SourceLocation();
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000626 TSTNameLoc = SourceLocation();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000627 return false;
628}
629
Reid Spencer5f016e22007-07-11 17:01:13 +0000630bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000631 unsigned &DiagID, const LangOptions &Lang) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000632 // Duplicates turn into warnings pre-C99.
633 if ((TypeQualifiers & T) && !Lang.C99)
John McCallfec54012009-08-03 20:12:06 +0000634 return BadSpecifier(T, T, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000635 TypeQualifiers |= T;
Mike Stump1eb44332009-09-09 15:08:12 +0000636
Reid Spencer5f016e22007-07-11 17:01:13 +0000637 switch (T) {
638 default: assert(0 && "Unknown type qualifier!");
639 case TQ_const: TQ_constLoc = Loc; break;
640 case TQ_restrict: TQ_restrictLoc = Loc; break;
641 case TQ_volatile: TQ_volatileLoc = Loc; break;
642 }
643 return false;
644}
645
John McCallfec54012009-08-03 20:12:06 +0000646bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
647 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000648 // 'inline inline' is ok.
649 FS_inline_specified = true;
650 FS_inlineLoc = Loc;
651 return false;
652}
653
John McCallfec54012009-08-03 20:12:06 +0000654bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec,
655 unsigned &DiagID) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000656 // 'virtual virtual' is ok.
657 FS_virtual_specified = true;
658 FS_virtualLoc = Loc;
659 return false;
660}
661
John McCallfec54012009-08-03 20:12:06 +0000662bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec,
663 unsigned &DiagID) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000664 // 'explicit explicit' is ok.
665 FS_explicit_specified = true;
666 FS_explicitLoc = Loc;
667 return false;
668}
669
John McCallfec54012009-08-03 20:12:06 +0000670bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
671 unsigned &DiagID) {
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000672 if (Friend_specified) {
673 PrevSpec = "friend";
John McCallfec54012009-08-03 20:12:06 +0000674 DiagID = diag::ext_duplicate_declspec;
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000675 return true;
676 }
John McCallfec54012009-08-03 20:12:06 +0000677
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000678 Friend_specified = true;
679 FriendLoc = Loc;
680 return false;
681}
Reid Spencer5f016e22007-07-11 17:01:13 +0000682
Sebastian Redl2ac67232009-11-05 15:47:02 +0000683bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
684 unsigned &DiagID) {
685 // 'constexpr constexpr' is ok.
686 Constexpr_specified = true;
687 ConstexprLoc = Loc;
688 return false;
689}
690
John McCalld226f652010-08-21 09:40:31 +0000691void DeclSpec::setProtocolQualifiers(Decl * const *Protos,
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000692 unsigned NP,
693 SourceLocation *ProtoLocs,
694 SourceLocation LAngleLoc) {
695 if (NP == 0) return;
John McCalld226f652010-08-21 09:40:31 +0000696 ProtocolQualifiers = new Decl*[NP];
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000697 ProtocolLocs = new SourceLocation[NP];
John McCalld226f652010-08-21 09:40:31 +0000698 memcpy((void*)ProtocolQualifiers, Protos, sizeof(Decl*)*NP);
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000699 memcpy(ProtocolLocs, ProtoLocs, sizeof(SourceLocation)*NP);
700 NumProtocolQualifiers = NP;
701 ProtocolLAngleLoc = LAngleLoc;
702}
703
Douglas Gregorddf889a2010-01-18 18:04:31 +0000704void DeclSpec::SaveWrittenBuiltinSpecs() {
705 writtenBS.Sign = getTypeSpecSign();
706 writtenBS.Width = getTypeSpecWidth();
707 writtenBS.Type = getTypeSpecType();
708 // Search the list of attributes for the presence of a mode attribute.
709 writtenBS.ModeAttr = false;
John McCall7f040a92010-12-24 02:08:15 +0000710 AttributeList* attrs = getAttributes().getList();
Douglas Gregorddf889a2010-01-18 18:04:31 +0000711 while (attrs) {
712 if (attrs->getKind() == AttributeList::AT_mode) {
713 writtenBS.ModeAttr = true;
714 break;
715 }
716 attrs = attrs->getNext();
717 }
718}
719
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000720void DeclSpec::SaveStorageSpecifierAsWritten() {
721 if (SCS_extern_in_linkage_spec && StorageClassSpec == SCS_extern)
722 // If 'extern' is part of a linkage specification,
723 // then it is not a storage class "as written".
724 StorageClassSpecAsWritten = SCS_unspecified;
725 else
726 StorageClassSpecAsWritten = StorageClassSpec;
727}
728
Reid Spencer5f016e22007-07-11 17:01:13 +0000729/// Finish - This does final analysis of the declspec, rejecting things like
730/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
731/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
732/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000733void DeclSpec::Finish(Diagnostic &D, Preprocessor &PP) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000734 // Before possibly changing their values, save specs as written.
735 SaveWrittenBuiltinSpecs();
Douglas Gregor16573fa2010-04-19 22:54:31 +0000736 SaveStorageSpecifierAsWritten();
Douglas Gregorddf889a2010-01-18 18:04:31 +0000737
Reid Spencer5f016e22007-07-11 17:01:13 +0000738 // Check the type specifier components first.
739
Chris Lattner788b0fd2010-06-23 06:00:24 +0000740 // Validate and finalize AltiVec vector declspec.
741 if (TypeAltiVecVector) {
742 if (TypeAltiVecBool) {
743 // Sign specifiers are not allowed with vector bool. (PIM 2.1)
744 if (TypeSpecSign != TSS_unspecified) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000745 Diag(D, TSSLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner788b0fd2010-06-23 06:00:24 +0000746 << getSpecifierName((TSS)TypeSpecSign);
747 }
748
749 // Only char/int are valid with vector bool. (PIM 2.1)
Duncan Sands2e964a922010-06-23 19:34:52 +0000750 if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) &&
751 (TypeSpecType != TST_int)) || TypeAltiVecPixel) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000752 Diag(D, TSTLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner788b0fd2010-06-23 06:00:24 +0000753 << (TypeAltiVecPixel ? "__pixel" :
754 getSpecifierName((TST)TypeSpecType));
755 }
756
757 // Only 'short' is valid with vector bool. (PIM 2.1)
758 if ((TypeSpecWidth != TSW_unspecified) && (TypeSpecWidth != TSW_short))
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000759 Diag(D, TSWLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner788b0fd2010-06-23 06:00:24 +0000760 << getSpecifierName((TSW)TypeSpecWidth);
761
762 // Elements of vector bool are interpreted as unsigned. (PIM 2.1)
763 if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) ||
764 (TypeSpecWidth != TSW_unspecified))
765 TypeSpecSign = TSS_unsigned;
766 }
767
768 if (TypeAltiVecPixel) {
769 //TODO: perform validation
770 TypeSpecType = TST_int;
771 TypeSpecSign = TSS_unsigned;
772 TypeSpecWidth = TSW_short;
John McCall9e46b8c2010-08-26 17:22:34 +0000773 TypeSpecOwned = false;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000774 }
775 }
776
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000777 // signed/unsigned are only valid with int/char/wchar_t.
Reid Spencer5f016e22007-07-11 17:01:13 +0000778 if (TypeSpecSign != TSS_unspecified) {
779 if (TypeSpecType == TST_unspecified)
780 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000781 else if (TypeSpecType != TST_int &&
782 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000783 Diag(D, TSSLoc, diag::err_invalid_sign_spec)
Chris Lattner254be6a2008-11-22 08:32:36 +0000784 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000785 // signed double -> double.
786 TypeSpecSign = TSS_unspecified;
787 }
788 }
789
790 // Validate the width of the type.
791 switch (TypeSpecWidth) {
792 case TSW_unspecified: break;
793 case TSW_short: // short int
794 case TSW_longlong: // long long int
795 if (TypeSpecType == TST_unspecified)
796 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
797 else if (TypeSpecType != TST_int) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000798 Diag(D, TSWLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000799 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
Chris Lattner254be6a2008-11-22 08:32:36 +0000800 : diag::err_invalid_longlong_spec)
801 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000802 TypeSpecType = TST_int;
John McCall9e46b8c2010-08-26 17:22:34 +0000803 TypeSpecOwned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000804 }
805 break;
806 case TSW_long: // long double, long int
807 if (TypeSpecType == TST_unspecified)
808 TypeSpecType = TST_int; // long -> long int.
809 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000810 Diag(D, TSWLoc, diag::err_invalid_long_spec)
Chris Lattner254be6a2008-11-22 08:32:36 +0000811 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000812 TypeSpecType = TST_int;
John McCall9e46b8c2010-08-26 17:22:34 +0000813 TypeSpecOwned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000814 }
815 break;
816 }
Mike Stump1eb44332009-09-09 15:08:12 +0000817
Reid Spencer5f016e22007-07-11 17:01:13 +0000818 // TODO: if the implementation does not implement _Complex or _Imaginary,
819 // disallow their use. Need information about the backend.
820 if (TypeSpecComplex != TSC_unspecified) {
821 if (TypeSpecType == TST_unspecified) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000822 Diag(D, TSCLoc, diag::ext_plain_complex)
Douglas Gregor849b2432010-03-31 17:46:05 +0000823 << FixItHint::CreateInsertion(
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000824 PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
825 " double");
Reid Spencer5f016e22007-07-11 17:01:13 +0000826 TypeSpecType = TST_double; // _Complex -> _Complex double.
827 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
828 // Note that this intentionally doesn't include _Complex _Bool.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000829 Diag(D, TSTLoc, diag::ext_integer_complex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000830 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000831 Diag(D, TSCLoc, diag::err_invalid_complex_spec)
Chris Lattner254be6a2008-11-22 08:32:36 +0000832 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000833 TypeSpecComplex = TSC_unspecified;
834 }
835 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000836
John McCall67d1a672009-08-06 02:15:43 +0000837 // C++ [class.friend]p6:
838 // No storage-class-specifier shall appear in the decl-specifier-seq
839 // of a friend declaration.
840 if (isFriendSpecified() && getStorageClassSpec()) {
841 DeclSpec::SCS SC = getStorageClassSpec();
842 const char *SpecName = getSpecifierName(SC);
843
844 SourceLocation SCLoc = getStorageClassSpecLoc();
845 SourceLocation SCEndLoc = SCLoc.getFileLocWithOffset(strlen(SpecName));
846
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000847 Diag(D, SCLoc, diag::err_friend_storage_spec)
John McCall67d1a672009-08-06 02:15:43 +0000848 << SpecName
Douglas Gregor849b2432010-03-31 17:46:05 +0000849 << FixItHint::CreateRemoval(SourceRange(SCLoc, SCEndLoc));
John McCall67d1a672009-08-06 02:15:43 +0000850
851 ClearStorageClassSpecs();
852 }
853
John McCall9e46b8c2010-08-26 17:22:34 +0000854 assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType));
855
Reid Spencer5f016e22007-07-11 17:01:13 +0000856 // Okay, now we can infer the real type.
Mike Stump1eb44332009-09-09 15:08:12 +0000857
Reid Spencer5f016e22007-07-11 17:01:13 +0000858 // TODO: return "auto function" and other bad things based on the real type.
Mike Stump1eb44332009-09-09 15:08:12 +0000859
Reid Spencer5f016e22007-07-11 17:01:13 +0000860 // 'data definition has no type or storage class'?
861}
Daniel Dunbare4858a62008-08-11 03:45:03 +0000862
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000863bool DeclSpec::isMissingDeclaratorOk() {
864 TST tst = getTypeSpecType();
John McCallb3d87482010-08-24 05:47:05 +0000865 return isDeclRep(tst) && getRepAsDecl() != 0 &&
866 StorageClassSpec != DeclSpec::SCS_typedef;
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000867}
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000868
869void UnqualifiedId::clear() {
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000870 Kind = IK_Identifier;
871 Identifier = 0;
872 StartLocation = SourceLocation();
873 EndLocation = SourceLocation();
874}
875
876void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc,
877 OverloadedOperatorKind Op,
878 SourceLocation SymbolLocations[3]) {
879 Kind = IK_OperatorFunctionId;
880 StartLocation = OperatorLoc;
881 EndLocation = OperatorLoc;
882 OperatorFunctionId.Operator = Op;
883 for (unsigned I = 0; I != 3; ++I) {
884 OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding();
885
886 if (SymbolLocations[I].isValid())
887 EndLocation = SymbolLocations[I];
888 }
889}
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000890
Anders Carlssoncc54d592011-01-22 16:56:46 +0000891bool VirtSpecifiers::SetSpecifier(Specifier VS, SourceLocation Loc,
Anders Carlsson46127a92011-01-22 15:58:16 +0000892 const char *&PrevSpec) {
Douglas Gregorf5251602011-03-08 17:10:18 +0000893 LastLocation = Loc;
894
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000895 if (Specifiers & VS) {
896 PrevSpec = getSpecifierName(VS);
897 return true;
898 }
899
900 Specifiers |= VS;
901
902 switch (VS) {
903 default: assert(0 && "Unknown specifier!");
904 case VS_Override: VS_overrideLoc = Loc; break;
905 case VS_Final: VS_finalLoc = Loc; break;
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000906 }
Anders Carlsson46127a92011-01-22 15:58:16 +0000907
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000908 return false;
909}
910
Anders Carlssoncc54d592011-01-22 16:56:46 +0000911const char *VirtSpecifiers::getSpecifierName(Specifier VS) {
Anders Carlssonc46bb7d2011-01-22 15:11:37 +0000912 switch (VS) {
913 default: assert(0 && "Unknown specifier");
914 case VS_Override: return "override";
915 case VS_Final: return "final";
Anders Carlssonc46bb7d2011-01-22 15:11:37 +0000916 }
917}