blob: e4d76c8fd56c2250a117fd43820c6fc459aced1c [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
David Blaikied6471f72011-09-25 23:23:43 +000030static DiagnosticBuilder Diag(DiagnosticsEngine &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,
Douglas Gregor90ebed02011-07-13 21:47:47 +0000152 SourceLocation MutableLoc,
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000153 ExceptionSpecificationType
154 ESpecType,
155 SourceLocation ESpecLoc,
John McCallb3d87482010-08-24 05:47:05 +0000156 ParsedType *Exceptions,
Sebastian Redlef65f062009-05-29 18:02:33 +0000157 SourceRange *ExceptionRanges,
Sebastian Redl7dc81342009-04-29 17:30:04 +0000158 unsigned NumExceptions,
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000159 Expr *NoexceptExpr,
Abramo Bagnara796aa442011-03-12 11:17:06 +0000160 SourceLocation LocalRangeBegin,
161 SourceLocation LocalRangeEnd,
Douglas Gregordab60ad2010-10-01 18:44:50 +0000162 Declarator &TheDeclarator,
163 ParsedType TrailingReturnType) {
Chris Lattner5af2f352009-01-20 19:11:22 +0000164 DeclaratorChunk I;
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000165 I.Kind = Function;
Abramo Bagnara796aa442011-03-12 11:17:06 +0000166 I.Loc = LocalRangeBegin;
167 I.EndLoc = LocalRangeEnd;
John McCall0b7e6782011-03-24 11:26:52 +0000168 I.Fun.AttrList = 0;
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000169 I.Fun.hasPrototype = hasProto;
170 I.Fun.isVariadic = isVariadic;
171 I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding();
172 I.Fun.DeleteArgInfo = false;
173 I.Fun.TypeQuals = TypeQuals;
174 I.Fun.NumArgs = NumArgs;
175 I.Fun.ArgInfo = 0;
Douglas Gregor83f51722011-01-26 03:43:54 +0000176 I.Fun.RefQualifierIsLValueRef = RefQualifierIsLvalueRef;
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000177 I.Fun.RefQualifierLoc = RefQualifierLoc.getRawEncoding();
Douglas Gregor90ebed02011-07-13 21:47:47 +0000178 I.Fun.MutableLoc = MutableLoc.getRawEncoding();
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000179 I.Fun.ExceptionSpecType = ESpecType;
180 I.Fun.ExceptionSpecLoc = ESpecLoc.getRawEncoding();
181 I.Fun.NumExceptions = 0;
182 I.Fun.Exceptions = 0;
183 I.Fun.NoexceptExpr = 0;
Douglas Gregordab60ad2010-10-01 18:44:50 +0000184 I.Fun.TrailingReturnType = TrailingReturnType.getAsOpaquePtr();
Sebastian Redl7dc81342009-04-29 17:30:04 +0000185
Chris Lattner5af2f352009-01-20 19:11:22 +0000186 // new[] an argument array if needed.
187 if (NumArgs) {
188 // If the 'InlineParams' in Declarator is unused and big enough, put our
189 // parameter list there (in an effort to avoid new/delete traffic). If it
190 // is already used (consider a function returning a function pointer) or too
191 // small (function taking too many arguments), go to the heap.
Mike Stump1eb44332009-09-09 15:08:12 +0000192 if (!TheDeclarator.InlineParamsUsed &&
Chris Lattner5af2f352009-01-20 19:11:22 +0000193 NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
194 I.Fun.ArgInfo = TheDeclarator.InlineParams;
195 I.Fun.DeleteArgInfo = false;
196 TheDeclarator.InlineParamsUsed = true;
197 } else {
198 I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs];
199 I.Fun.DeleteArgInfo = true;
200 }
201 memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs);
202 }
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000203
204 // Check what exception specification information we should actually store.
205 switch (ESpecType) {
206 default: break; // By default, save nothing.
207 case EST_Dynamic:
208 // new[] an exception array if needed
209 if (NumExceptions) {
210 I.Fun.NumExceptions = NumExceptions;
211 I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions];
212 for (unsigned i = 0; i != NumExceptions; ++i) {
213 I.Fun.Exceptions[i].Ty = Exceptions[i];
214 I.Fun.Exceptions[i].Range = ExceptionRanges[i];
215 }
Sebastian Redlef65f062009-05-29 18:02:33 +0000216 }
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000217 break;
218
219 case EST_ComputedNoexcept:
220 I.Fun.NoexceptExpr = NoexceptExpr;
221 break;
Sebastian Redl7dc81342009-04-29 17:30:04 +0000222 }
Chris Lattner5af2f352009-01-20 19:11:22 +0000223 return I;
224}
Chris Lattner254be6a2008-11-22 08:32:36 +0000225
Douglas Gregor555f57e2011-06-25 00:56:27 +0000226bool Declarator::isDeclarationOfFunction() const {
Richard Smith1ab0d902011-06-25 02:28:38 +0000227 for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) {
228 switch (DeclTypeInfo[i].Kind) {
229 case DeclaratorChunk::Function:
230 return true;
231 case DeclaratorChunk::Paren:
232 continue;
233 case DeclaratorChunk::Pointer:
234 case DeclaratorChunk::Reference:
235 case DeclaratorChunk::Array:
236 case DeclaratorChunk::BlockPointer:
237 case DeclaratorChunk::MemberPointer:
238 return false;
239 }
240 llvm_unreachable("Invalid type chunk");
241 return false;
242 }
Douglas Gregor555f57e2011-06-25 00:56:27 +0000243
244 switch (DS.getTypeSpecType()) {
245 case TST_auto:
246 case TST_bool:
247 case TST_char:
248 case TST_char16:
249 case TST_char32:
250 case TST_class:
251 case TST_decimal128:
252 case TST_decimal32:
253 case TST_decimal64:
254 case TST_double:
255 case TST_enum:
256 case TST_error:
257 case TST_float:
258 case TST_int:
259 case TST_struct:
260 case TST_union:
261 case TST_unknown_anytype:
262 case TST_unspecified:
263 case TST_void:
264 case TST_wchar:
265 return false;
266
267 case TST_decltype:
268 case TST_typeofExpr:
269 if (Expr *E = DS.getRepAsExpr())
270 return E->getType()->isFunctionType();
271 return false;
272
273 case TST_underlyingType:
274 case TST_typename:
275 case TST_typeofType: {
276 QualType QT = DS.getRepAsType().get();
277 if (QT.isNull())
278 return false;
279
280 if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT))
281 QT = LIT->getType();
282
283 if (QT.isNull())
284 return false;
285
286 return QT->isFunctionType();
287 }
288 }
289
290 return false;
291}
292
Reid Spencer5f016e22007-07-11 17:01:13 +0000293/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
Chris Lattner2a327d12009-02-27 18:35:46 +0000294/// declaration specifier includes.
Reid Spencer5f016e22007-07-11 17:01:13 +0000295///
296unsigned DeclSpec::getParsedSpecifiers() const {
297 unsigned Res = 0;
298 if (StorageClassSpec != SCS_unspecified ||
299 SCS_thread_specified)
300 Res |= PQ_StorageClassSpecifier;
Mike Stumpd4204332008-06-19 19:52:46 +0000301
Reid Spencer5f016e22007-07-11 17:01:13 +0000302 if (TypeQualifiers != TQ_unspecified)
303 Res |= PQ_TypeQualifier;
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Reid Spencer5f016e22007-07-11 17:01:13 +0000305 if (hasTypeSpecifier())
306 Res |= PQ_TypeSpecifier;
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Douglas Gregorb48fe382008-10-31 09:07:45 +0000308 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
Reid Spencer5f016e22007-07-11 17:01:13 +0000309 Res |= PQ_FunctionSpecifier;
310 return Res;
311}
312
John McCallfec54012009-08-03 20:12:06 +0000313template <class T> static bool BadSpecifier(T TNew, T TPrev,
314 const char *&PrevSpec,
315 unsigned &DiagID) {
John McCall32d335e2009-08-03 18:47:27 +0000316 PrevSpec = DeclSpec::getSpecifierName(TPrev);
John McCallfec54012009-08-03 20:12:06 +0000317 DiagID = (TNew == TPrev ? diag::ext_duplicate_declspec
318 : diag::err_invalid_decl_spec_combination);
John McCall32d335e2009-08-03 18:47:27 +0000319 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000320}
John McCall32d335e2009-08-03 18:47:27 +0000321
Reid Spencer5f016e22007-07-11 17:01:13 +0000322const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
323 switch (S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000324 case DeclSpec::SCS_unspecified: return "unspecified";
325 case DeclSpec::SCS_typedef: return "typedef";
326 case DeclSpec::SCS_extern: return "extern";
327 case DeclSpec::SCS_static: return "static";
328 case DeclSpec::SCS_auto: return "auto";
329 case DeclSpec::SCS_register: return "register";
Eli Friedman63054b32009-04-19 20:27:55 +0000330 case DeclSpec::SCS_private_extern: return "__private_extern__";
Sebastian Redl669d5d72008-11-14 23:42:31 +0000331 case DeclSpec::SCS_mutable: return "mutable";
Reid Spencer5f016e22007-07-11 17:01:13 +0000332 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000333 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000334}
335
John McCall32d335e2009-08-03 18:47:27 +0000336const char *DeclSpec::getSpecifierName(TSW W) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000337 switch (W) {
John McCall32d335e2009-08-03 18:47:27 +0000338 case TSW_unspecified: return "unspecified";
339 case TSW_short: return "short";
340 case TSW_long: return "long";
341 case TSW_longlong: return "long long";
Reid Spencer5f016e22007-07-11 17:01:13 +0000342 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000343 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000344}
345
John McCall32d335e2009-08-03 18:47:27 +0000346const char *DeclSpec::getSpecifierName(TSC C) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000347 switch (C) {
John McCall32d335e2009-08-03 18:47:27 +0000348 case TSC_unspecified: return "unspecified";
349 case TSC_imaginary: return "imaginary";
350 case TSC_complex: return "complex";
Reid Spencer5f016e22007-07-11 17:01:13 +0000351 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000352 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000353}
354
355
John McCall32d335e2009-08-03 18:47:27 +0000356const char *DeclSpec::getSpecifierName(TSS S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000357 switch (S) {
John McCall32d335e2009-08-03 18:47:27 +0000358 case TSS_unspecified: return "unspecified";
359 case TSS_signed: return "signed";
360 case TSS_unsigned: return "unsigned";
Reid Spencer5f016e22007-07-11 17:01:13 +0000361 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000362 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000363}
364
365const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
366 switch (T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000367 case DeclSpec::TST_unspecified: return "unspecified";
368 case DeclSpec::TST_void: return "void";
369 case DeclSpec::TST_char: return "char";
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000370 case DeclSpec::TST_wchar: return "wchar_t";
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000371 case DeclSpec::TST_char16: return "char16_t";
372 case DeclSpec::TST_char32: return "char32_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000373 case DeclSpec::TST_int: return "int";
374 case DeclSpec::TST_float: return "float";
375 case DeclSpec::TST_double: return "double";
376 case DeclSpec::TST_bool: return "_Bool";
377 case DeclSpec::TST_decimal32: return "_Decimal32";
378 case DeclSpec::TST_decimal64: return "_Decimal64";
379 case DeclSpec::TST_decimal128: return "_Decimal128";
380 case DeclSpec::TST_enum: return "enum";
Chris Lattner99dc9142008-04-13 18:59:07 +0000381 case DeclSpec::TST_class: return "class";
Reid Spencer5f016e22007-07-11 17:01:13 +0000382 case DeclSpec::TST_union: return "union";
383 case DeclSpec::TST_struct: return "struct";
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000384 case DeclSpec::TST_typename: return "type-name";
Steve Naroffd1861fd2007-07-31 12:34:36 +0000385 case DeclSpec::TST_typeofType:
386 case DeclSpec::TST_typeofExpr: return "typeof";
John McCall32d335e2009-08-03 18:47:27 +0000387 case DeclSpec::TST_auto: return "auto";
388 case DeclSpec::TST_decltype: return "(decltype)";
Sean Huntca63c202011-05-24 22:41:36 +0000389 case DeclSpec::TST_underlyingType: return "__underlying_type";
John McCalla5fc4722011-04-09 22:50:59 +0000390 case DeclSpec::TST_unknown_anytype: return "__unknown_anytype";
John McCall32d335e2009-08-03 18:47:27 +0000391 case DeclSpec::TST_error: return "(error)";
Reid Spencer5f016e22007-07-11 17:01:13 +0000392 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000393 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000394}
395
John McCall32d335e2009-08-03 18:47:27 +0000396const char *DeclSpec::getSpecifierName(TQ T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000397 switch (T) {
John McCall32d335e2009-08-03 18:47:27 +0000398 case DeclSpec::TQ_unspecified: return "unspecified";
399 case DeclSpec::TQ_const: return "const";
400 case DeclSpec::TQ_restrict: return "restrict";
401 case DeclSpec::TQ_volatile: return "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +0000402 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000403 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000404}
405
406bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000407 const char *&PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +0000408 unsigned &DiagID,
409 const LangOptions &Lang) {
410 // OpenCL prohibits extern, auto, register, and static
411 // It seems sensible to prohibit private_extern too
412 if (Lang.OpenCL) {
413 switch (S) {
414 case SCS_extern:
415 case SCS_private_extern:
416 case SCS_auto:
417 case SCS_register:
418 case SCS_static:
419 DiagID = diag::err_not_opencl_storage_class_specifier;
420 PrevSpec = getSpecifierName(S);
421 return true;
422 default:
423 break;
424 }
425 }
426
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000427 if (StorageClassSpec != SCS_unspecified) {
Richard Smith8f4fb192011-09-04 19:54:14 +0000428 // Maybe this is an attempt to use C++0x 'auto' outside of C++0x mode.
429 bool isInvalid = true;
430 if (TypeSpecType == TST_unspecified && Lang.CPlusPlus) {
431 if (S == SCS_auto)
432 return SetTypeSpecType(TST_auto, Loc, PrevSpec, DiagID);
433 if (StorageClassSpec == SCS_auto) {
434 isInvalid = SetTypeSpecType(TST_auto, StorageClassSpecLoc,
435 PrevSpec, DiagID);
436 assert(!isInvalid && "auto SCS -> TST recovery failed");
437 }
438 }
439
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000440 // Changing storage class is allowed only if the previous one
441 // was the 'extern' that is part of a linkage specification and
442 // the new storage class is 'typedef'.
Richard Smith8f4fb192011-09-04 19:54:14 +0000443 if (isInvalid &&
444 !(SCS_extern_in_linkage_spec &&
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000445 StorageClassSpec == SCS_extern &&
446 S == SCS_typedef))
447 return BadSpecifier(S, (SCS)StorageClassSpec, PrevSpec, DiagID);
448 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000449 StorageClassSpec = S;
450 StorageClassSpecLoc = Loc;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000451 assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield");
Reid Spencer5f016e22007-07-11 17:01:13 +0000452 return false;
453}
454
Mike Stump1eb44332009-09-09 15:08:12 +0000455bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000456 const char *&PrevSpec,
457 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000458 if (SCS_thread_specified) {
459 PrevSpec = "__thread";
John McCallfec54012009-08-03 20:12:06 +0000460 DiagID = diag::ext_duplicate_declspec;
Reid Spencer5f016e22007-07-11 17:01:13 +0000461 return true;
462 }
463 SCS_thread_specified = true;
464 SCS_threadLoc = Loc;
465 return false;
466}
467
Reid Spencer5f016e22007-07-11 17:01:13 +0000468/// These methods set the specified attribute of the DeclSpec, but return true
469/// and ignore the request if invalid (e.g. "extern" then "auto" is
470/// specified).
471bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000472 const char *&PrevSpec,
473 unsigned &DiagID) {
Abramo Bagnara2553eaf2011-03-06 22:21:56 +0000474 // Overwrite TSWLoc only if TypeSpecWidth was unspecified, so that
475 // for 'long long' we will keep the source location of the first 'long'.
476 if (TypeSpecWidth == TSW_unspecified)
477 TSWLoc = Loc;
478 // Allow turning long -> long long.
479 else if (W != TSW_longlong || TypeSpecWidth != TSW_long)
John McCallfec54012009-08-03 20:12:06 +0000480 return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000481 TypeSpecWidth = W;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000482 if (TypeAltiVecVector && !TypeAltiVecBool &&
483 ((TypeSpecWidth == TSW_long) || (TypeSpecWidth == TSW_longlong))) {
John Thompson82287d12010-02-05 00:12:22 +0000484 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
485 DiagID = diag::warn_vector_long_decl_spec_combination;
486 return true;
487 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000488 return false;
489}
490
Mike Stump1eb44332009-09-09 15:08:12 +0000491bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000492 const char *&PrevSpec,
493 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000494 if (TypeSpecComplex != TSC_unspecified)
John McCallfec54012009-08-03 20:12:06 +0000495 return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000496 TypeSpecComplex = C;
497 TSCLoc = Loc;
498 return false;
499}
500
Mike Stump1eb44332009-09-09 15:08:12 +0000501bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000502 const char *&PrevSpec,
503 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000504 if (TypeSpecSign != TSS_unspecified)
John McCallfec54012009-08-03 20:12:06 +0000505 return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000506 TypeSpecSign = S;
507 TSSLoc = Loc;
508 return false;
509}
510
511bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000512 const char *&PrevSpec,
513 unsigned &DiagID,
John McCallb3d87482010-08-24 05:47:05 +0000514 ParsedType Rep) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000515 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep);
516}
517
518bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
519 SourceLocation TagNameLoc,
520 const char *&PrevSpec,
521 unsigned &DiagID,
522 ParsedType Rep) {
John McCallb3d87482010-08-24 05:47:05 +0000523 assert(isTypeRep(T) && "T does not store a type");
524 assert(Rep && "no type provided!");
525 if (TypeSpecType != TST_unspecified) {
526 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
527 DiagID = diag::err_invalid_decl_spec_combination;
528 return true;
529 }
530 TypeSpecType = T;
531 TypeRep = Rep;
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000532 TSTLoc = TagKwLoc;
533 TSTNameLoc = TagNameLoc;
John McCallb3d87482010-08-24 05:47:05 +0000534 TypeSpecOwned = false;
535 return false;
536}
537
538bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
539 const char *&PrevSpec,
540 unsigned &DiagID,
541 Expr *Rep) {
542 assert(isExprRep(T) && "T does not store an expr");
543 assert(Rep && "no expression provided!");
544 if (TypeSpecType != TST_unspecified) {
545 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
546 DiagID = diag::err_invalid_decl_spec_combination;
547 return true;
548 }
549 TypeSpecType = T;
550 ExprRep = Rep;
551 TSTLoc = Loc;
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000552 TSTNameLoc = Loc;
John McCallb3d87482010-08-24 05:47:05 +0000553 TypeSpecOwned = false;
554 return false;
555}
556
557bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
558 const char *&PrevSpec,
559 unsigned &DiagID,
560 Decl *Rep, bool Owned) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000561 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep, Owned);
562}
563
564bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
565 SourceLocation TagNameLoc,
566 const char *&PrevSpec,
567 unsigned &DiagID,
568 Decl *Rep, bool Owned) {
John McCallb3d87482010-08-24 05:47:05 +0000569 assert(isDeclRep(T) && "T does not store a decl");
570 // Unlike the other cases, we don't assert that we actually get a decl.
571
572 if (TypeSpecType != TST_unspecified) {
573 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
574 DiagID = diag::err_invalid_decl_spec_combination;
575 return true;
576 }
577 TypeSpecType = T;
578 DeclRep = Rep;
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000579 TSTLoc = TagKwLoc;
580 TSTNameLoc = TagNameLoc;
John McCallb3d87482010-08-24 05:47:05 +0000581 TypeSpecOwned = Owned;
582 return false;
583}
584
585bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
586 const char *&PrevSpec,
587 unsigned &DiagID) {
588 assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) &&
589 "rep required for these type-spec kinds!");
John McCallfec54012009-08-03 20:12:06 +0000590 if (TypeSpecType != TST_unspecified) {
591 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
592 DiagID = diag::err_invalid_decl_spec_combination;
593 return true;
594 }
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000595 TSTLoc = Loc;
596 TSTNameLoc = Loc;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000597 if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) {
598 TypeAltiVecBool = true;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000599 return false;
600 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000601 TypeSpecType = T;
John McCallb3d87482010-08-24 05:47:05 +0000602 TypeSpecOwned = false;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000603 if (TypeAltiVecVector && !TypeAltiVecBool && (TypeSpecType == TST_double)) {
John Thompson82287d12010-02-05 00:12:22 +0000604 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
Chris Lattner788b0fd2010-06-23 06:00:24 +0000605 DiagID = diag::err_invalid_vector_decl_spec;
John Thompson82287d12010-02-05 00:12:22 +0000606 return true;
607 }
608 return false;
609}
610
611bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
612 const char *&PrevSpec, unsigned &DiagID) {
613 if (TypeSpecType != TST_unspecified) {
614 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
615 DiagID = diag::err_invalid_vector_decl_spec_combination;
616 return true;
617 }
618 TypeAltiVecVector = isAltiVecVector;
619 AltiVecLoc = Loc;
620 return false;
621}
622
623bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
624 const char *&PrevSpec, unsigned &DiagID) {
Chris Lattner788b0fd2010-06-23 06:00:24 +0000625 if (!TypeAltiVecVector || TypeAltiVecPixel ||
626 (TypeSpecType != TST_unspecified)) {
John Thompson82287d12010-02-05 00:12:22 +0000627 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
628 DiagID = diag::err_invalid_pixel_decl_spec_combination;
629 return true;
630 }
John Thompson82287d12010-02-05 00:12:22 +0000631 TypeAltiVecPixel = isAltiVecPixel;
632 TSTLoc = Loc;
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000633 TSTNameLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000634 return false;
635}
636
Douglas Gregorddc29e12009-02-06 22:42:48 +0000637bool DeclSpec::SetTypeSpecError() {
638 TypeSpecType = TST_error;
John McCall9e46b8c2010-08-26 17:22:34 +0000639 TypeSpecOwned = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000640 TSTLoc = SourceLocation();
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000641 TSTNameLoc = SourceLocation();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000642 return false;
643}
644
Reid Spencer5f016e22007-07-11 17:01:13 +0000645bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000646 unsigned &DiagID, const LangOptions &Lang) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000647 // Duplicates turn into warnings pre-C99.
648 if ((TypeQualifiers & T) && !Lang.C99)
John McCallfec54012009-08-03 20:12:06 +0000649 return BadSpecifier(T, T, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000650 TypeQualifiers |= T;
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Reid Spencer5f016e22007-07-11 17:01:13 +0000652 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000653 default: llvm_unreachable("Unknown type qualifier!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000654 case TQ_const: TQ_constLoc = Loc; break;
655 case TQ_restrict: TQ_restrictLoc = Loc; break;
656 case TQ_volatile: TQ_volatileLoc = Loc; break;
657 }
658 return false;
659}
660
John McCallfec54012009-08-03 20:12:06 +0000661bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
662 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000663 // 'inline inline' is ok.
664 FS_inline_specified = true;
665 FS_inlineLoc = Loc;
666 return false;
667}
668
John McCallfec54012009-08-03 20:12:06 +0000669bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec,
670 unsigned &DiagID) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000671 // 'virtual virtual' is ok.
672 FS_virtual_specified = true;
673 FS_virtualLoc = Loc;
674 return false;
675}
676
John McCallfec54012009-08-03 20:12:06 +0000677bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec,
678 unsigned &DiagID) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000679 // 'explicit explicit' is ok.
680 FS_explicit_specified = true;
681 FS_explicitLoc = Loc;
682 return false;
683}
684
John McCallfec54012009-08-03 20:12:06 +0000685bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
686 unsigned &DiagID) {
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000687 if (Friend_specified) {
688 PrevSpec = "friend";
John McCallfec54012009-08-03 20:12:06 +0000689 DiagID = diag::ext_duplicate_declspec;
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000690 return true;
691 }
John McCallfec54012009-08-03 20:12:06 +0000692
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000693 Friend_specified = true;
694 FriendLoc = Loc;
695 return false;
696}
Reid Spencer5f016e22007-07-11 17:01:13 +0000697
Douglas Gregor8d267c52011-09-09 02:06:17 +0000698bool DeclSpec::setModulePrivateSpec(SourceLocation Loc, const char *&PrevSpec,
699 unsigned &DiagID) {
700 if (isModulePrivateSpecified()) {
701 PrevSpec = "__module_private__";
702 DiagID = diag::ext_duplicate_declspec;
703 return true;
704 }
705
706 ModulePrivateLoc = Loc;
707 return false;
708}
709
Sebastian Redl2ac67232009-11-05 15:47:02 +0000710bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
711 unsigned &DiagID) {
712 // 'constexpr constexpr' is ok.
713 Constexpr_specified = true;
714 ConstexprLoc = Loc;
715 return false;
716}
717
John McCalld226f652010-08-21 09:40:31 +0000718void DeclSpec::setProtocolQualifiers(Decl * const *Protos,
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000719 unsigned NP,
720 SourceLocation *ProtoLocs,
721 SourceLocation LAngleLoc) {
722 if (NP == 0) return;
John McCalld226f652010-08-21 09:40:31 +0000723 ProtocolQualifiers = new Decl*[NP];
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000724 ProtocolLocs = new SourceLocation[NP];
John McCalld226f652010-08-21 09:40:31 +0000725 memcpy((void*)ProtocolQualifiers, Protos, sizeof(Decl*)*NP);
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000726 memcpy(ProtocolLocs, ProtoLocs, sizeof(SourceLocation)*NP);
727 NumProtocolQualifiers = NP;
728 ProtocolLAngleLoc = LAngleLoc;
729}
730
Douglas Gregorddf889a2010-01-18 18:04:31 +0000731void DeclSpec::SaveWrittenBuiltinSpecs() {
732 writtenBS.Sign = getTypeSpecSign();
733 writtenBS.Width = getTypeSpecWidth();
734 writtenBS.Type = getTypeSpecType();
735 // Search the list of attributes for the presence of a mode attribute.
736 writtenBS.ModeAttr = false;
John McCall7f040a92010-12-24 02:08:15 +0000737 AttributeList* attrs = getAttributes().getList();
Douglas Gregorddf889a2010-01-18 18:04:31 +0000738 while (attrs) {
739 if (attrs->getKind() == AttributeList::AT_mode) {
740 writtenBS.ModeAttr = true;
741 break;
742 }
743 attrs = attrs->getNext();
744 }
745}
746
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000747void DeclSpec::SaveStorageSpecifierAsWritten() {
748 if (SCS_extern_in_linkage_spec && StorageClassSpec == SCS_extern)
749 // If 'extern' is part of a linkage specification,
750 // then it is not a storage class "as written".
751 StorageClassSpecAsWritten = SCS_unspecified;
752 else
753 StorageClassSpecAsWritten = StorageClassSpec;
754}
755
Reid Spencer5f016e22007-07-11 17:01:13 +0000756/// Finish - This does final analysis of the declspec, rejecting things like
757/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
758/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
759/// DeclSpec is guaranteed self-consistent, even if an error occurred.
David Blaikied6471f72011-09-25 23:23:43 +0000760void DeclSpec::Finish(DiagnosticsEngine &D, Preprocessor &PP) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000761 // Before possibly changing their values, save specs as written.
762 SaveWrittenBuiltinSpecs();
Douglas Gregor16573fa2010-04-19 22:54:31 +0000763 SaveStorageSpecifierAsWritten();
Douglas Gregorddf889a2010-01-18 18:04:31 +0000764
Reid Spencer5f016e22007-07-11 17:01:13 +0000765 // Check the type specifier components first.
766
Chris Lattner788b0fd2010-06-23 06:00:24 +0000767 // Validate and finalize AltiVec vector declspec.
768 if (TypeAltiVecVector) {
769 if (TypeAltiVecBool) {
770 // Sign specifiers are not allowed with vector bool. (PIM 2.1)
771 if (TypeSpecSign != TSS_unspecified) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000772 Diag(D, TSSLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner788b0fd2010-06-23 06:00:24 +0000773 << getSpecifierName((TSS)TypeSpecSign);
774 }
775
776 // Only char/int are valid with vector bool. (PIM 2.1)
Duncan Sands2e964a922010-06-23 19:34:52 +0000777 if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) &&
778 (TypeSpecType != TST_int)) || TypeAltiVecPixel) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000779 Diag(D, TSTLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner788b0fd2010-06-23 06:00:24 +0000780 << (TypeAltiVecPixel ? "__pixel" :
781 getSpecifierName((TST)TypeSpecType));
782 }
783
784 // Only 'short' is valid with vector bool. (PIM 2.1)
785 if ((TypeSpecWidth != TSW_unspecified) && (TypeSpecWidth != TSW_short))
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000786 Diag(D, TSWLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner788b0fd2010-06-23 06:00:24 +0000787 << getSpecifierName((TSW)TypeSpecWidth);
788
789 // Elements of vector bool are interpreted as unsigned. (PIM 2.1)
790 if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) ||
791 (TypeSpecWidth != TSW_unspecified))
792 TypeSpecSign = TSS_unsigned;
793 }
794
795 if (TypeAltiVecPixel) {
796 //TODO: perform validation
797 TypeSpecType = TST_int;
798 TypeSpecSign = TSS_unsigned;
799 TypeSpecWidth = TSW_short;
John McCall9e46b8c2010-08-26 17:22:34 +0000800 TypeSpecOwned = false;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000801 }
802 }
803
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000804 // signed/unsigned are only valid with int/char/wchar_t.
Reid Spencer5f016e22007-07-11 17:01:13 +0000805 if (TypeSpecSign != TSS_unspecified) {
806 if (TypeSpecType == TST_unspecified)
807 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000808 else if (TypeSpecType != TST_int &&
809 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000810 Diag(D, TSSLoc, diag::err_invalid_sign_spec)
Chris Lattner254be6a2008-11-22 08:32:36 +0000811 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000812 // signed double -> double.
813 TypeSpecSign = TSS_unspecified;
814 }
815 }
816
817 // Validate the width of the type.
818 switch (TypeSpecWidth) {
819 case TSW_unspecified: break;
820 case TSW_short: // short int
821 case TSW_longlong: // long long int
822 if (TypeSpecType == TST_unspecified)
823 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
824 else if (TypeSpecType != TST_int) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000825 Diag(D, TSWLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000826 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
Chris Lattner254be6a2008-11-22 08:32:36 +0000827 : diag::err_invalid_longlong_spec)
828 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000829 TypeSpecType = TST_int;
John McCall9e46b8c2010-08-26 17:22:34 +0000830 TypeSpecOwned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000831 }
832 break;
833 case TSW_long: // long double, long int
834 if (TypeSpecType == TST_unspecified)
835 TypeSpecType = TST_int; // long -> long int.
836 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000837 Diag(D, TSWLoc, diag::err_invalid_long_spec)
Chris Lattner254be6a2008-11-22 08:32:36 +0000838 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000839 TypeSpecType = TST_int;
John McCall9e46b8c2010-08-26 17:22:34 +0000840 TypeSpecOwned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000841 }
842 break;
843 }
Mike Stump1eb44332009-09-09 15:08:12 +0000844
Reid Spencer5f016e22007-07-11 17:01:13 +0000845 // TODO: if the implementation does not implement _Complex or _Imaginary,
846 // disallow their use. Need information about the backend.
847 if (TypeSpecComplex != TSC_unspecified) {
848 if (TypeSpecType == TST_unspecified) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000849 Diag(D, TSCLoc, diag::ext_plain_complex)
Douglas Gregor849b2432010-03-31 17:46:05 +0000850 << FixItHint::CreateInsertion(
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000851 PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
852 " double");
Reid Spencer5f016e22007-07-11 17:01:13 +0000853 TypeSpecType = TST_double; // _Complex -> _Complex double.
854 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
855 // Note that this intentionally doesn't include _Complex _Bool.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000856 Diag(D, TSTLoc, diag::ext_integer_complex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000857 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000858 Diag(D, TSCLoc, diag::err_invalid_complex_spec)
Chris Lattner254be6a2008-11-22 08:32:36 +0000859 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000860 TypeSpecComplex = TSC_unspecified;
861 }
862 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000863
Richard Smith8f4fb192011-09-04 19:54:14 +0000864 // If no type specifier was provided and we're parsing a language where
865 // the type specifier is not optional, but we got 'auto' as a storage
866 // class specifier, then assume this is an attempt to use C++0x's 'auto'
867 // type specifier.
868 // FIXME: Does Microsoft really support implicit int in C++?
Francois Pichet62ec1f22011-09-17 17:15:52 +0000869 if (PP.getLangOptions().CPlusPlus && !PP.getLangOptions().MicrosoftExt &&
Richard Smith8f4fb192011-09-04 19:54:14 +0000870 TypeSpecType == TST_unspecified && StorageClassSpec == SCS_auto) {
871 TypeSpecType = TST_auto;
872 StorageClassSpec = StorageClassSpecAsWritten = SCS_unspecified;
873 TSTLoc = TSTNameLoc = StorageClassSpecLoc;
874 StorageClassSpecLoc = SourceLocation();
875 }
876 // Diagnose if we've recovered from an ill-formed 'auto' storage class
877 // specifier in a pre-C++0x dialect of C++.
878 if (!PP.getLangOptions().CPlusPlus0x && TypeSpecType == TST_auto)
879 Diag(D, TSTLoc, diag::ext_auto_type_specifier);
880 if (PP.getLangOptions().CPlusPlus && !PP.getLangOptions().CPlusPlus0x &&
881 StorageClassSpec == SCS_auto)
882 Diag(D, StorageClassSpecLoc, diag::warn_auto_storage_class)
883 << FixItHint::CreateRemoval(StorageClassSpecLoc);
884
John McCall67d1a672009-08-06 02:15:43 +0000885 // C++ [class.friend]p6:
886 // No storage-class-specifier shall appear in the decl-specifier-seq
887 // of a friend declaration.
888 if (isFriendSpecified() && getStorageClassSpec()) {
889 DeclSpec::SCS SC = getStorageClassSpec();
890 const char *SpecName = getSpecifierName(SC);
891
892 SourceLocation SCLoc = getStorageClassSpecLoc();
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000893 SourceLocation SCEndLoc = SCLoc.getLocWithOffset(strlen(SpecName));
John McCall67d1a672009-08-06 02:15:43 +0000894
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000895 Diag(D, SCLoc, diag::err_friend_storage_spec)
John McCall67d1a672009-08-06 02:15:43 +0000896 << SpecName
Douglas Gregor849b2432010-03-31 17:46:05 +0000897 << FixItHint::CreateRemoval(SourceRange(SCLoc, SCEndLoc));
John McCall67d1a672009-08-06 02:15:43 +0000898
899 ClearStorageClassSpecs();
900 }
901
Douglas Gregor6274d302011-09-09 21:14:29 +0000902 assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType));
903
Reid Spencer5f016e22007-07-11 17:01:13 +0000904 // Okay, now we can infer the real type.
Mike Stump1eb44332009-09-09 15:08:12 +0000905
Reid Spencer5f016e22007-07-11 17:01:13 +0000906 // TODO: return "auto function" and other bad things based on the real type.
Mike Stump1eb44332009-09-09 15:08:12 +0000907
Reid Spencer5f016e22007-07-11 17:01:13 +0000908 // 'data definition has no type or storage class'?
909}
Daniel Dunbare4858a62008-08-11 03:45:03 +0000910
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000911bool DeclSpec::isMissingDeclaratorOk() {
912 TST tst = getTypeSpecType();
John McCallb3d87482010-08-24 05:47:05 +0000913 return isDeclRep(tst) && getRepAsDecl() != 0 &&
914 StorageClassSpec != DeclSpec::SCS_typedef;
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000915}
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000916
917void UnqualifiedId::clear() {
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000918 Kind = IK_Identifier;
919 Identifier = 0;
920 StartLocation = SourceLocation();
921 EndLocation = SourceLocation();
922}
923
924void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc,
925 OverloadedOperatorKind Op,
926 SourceLocation SymbolLocations[3]) {
927 Kind = IK_OperatorFunctionId;
928 StartLocation = OperatorLoc;
929 EndLocation = OperatorLoc;
930 OperatorFunctionId.Operator = Op;
931 for (unsigned I = 0; I != 3; ++I) {
932 OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding();
933
934 if (SymbolLocations[I].isValid())
935 EndLocation = SymbolLocations[I];
936 }
937}
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000938
Anders Carlssoncc54d592011-01-22 16:56:46 +0000939bool VirtSpecifiers::SetSpecifier(Specifier VS, SourceLocation Loc,
Anders Carlsson46127a92011-01-22 15:58:16 +0000940 const char *&PrevSpec) {
Douglas Gregorf5251602011-03-08 17:10:18 +0000941 LastLocation = Loc;
942
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000943 if (Specifiers & VS) {
944 PrevSpec = getSpecifierName(VS);
945 return true;
946 }
947
948 Specifiers |= VS;
949
950 switch (VS) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000951 default: llvm_unreachable("Unknown specifier!");
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000952 case VS_Override: VS_overrideLoc = Loc; break;
953 case VS_Final: VS_finalLoc = Loc; break;
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000954 }
Anders Carlsson46127a92011-01-22 15:58:16 +0000955
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000956 return false;
957}
958
Anders Carlssoncc54d592011-01-22 16:56:46 +0000959const char *VirtSpecifiers::getSpecifierName(Specifier VS) {
Anders Carlssonc46bb7d2011-01-22 15:11:37 +0000960 switch (VS) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000961 default: llvm_unreachable("Unknown specifier");
Anders Carlssonc46bb7d2011-01-22 15:11:37 +0000962 case VS_Override: return "override";
963 case VS_Final: return "final";
Anders Carlssonc46bb7d2011-01-22 15:11:37 +0000964 }
965}