blob: c5b883e46d3f539f4112ed78e3697815871c72d7 [file] [log] [blame]
Chris Lattner289ab7b2006-11-08 06:54:53 +00001//===--- SemaDeclSpec.cpp - Declaration Specifier Semantic Analysis -------===//
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner289ab7b2006-11-08 06:54:53 +000010// This file implements semantic analysis for declaration specifiers.
Chris Lattnerb9093cd2006-08-04 04:39:53 +000011//
12//===----------------------------------------------------------------------===//
13
John McCall8b0666c2010-08-20 18:27:03 +000014#include "clang/Parse/ParseDiagnostic.h" // FIXME: remove this back-dependency!
15#include "clang/Sema/DeclSpec.h"
Douglas Gregorc15b0cf2011-06-25 00:56:27 +000016#include "clang/Sema/LocInfoType.h"
John McCall8b0666c2010-08-20 18:27:03 +000017#include "clang/Sema/ParsedTemplate.h"
Douglas Gregor869ad452011-02-24 17:54:50 +000018#include "clang/AST/ASTContext.h"
Douglas Gregorc15b0cf2011-06-25 00:56:27 +000019#include "clang/AST/Expr.h"
Douglas Gregor90c99722011-02-24 00:17:56 +000020#include "clang/AST/NestedNameSpecifier.h"
21#include "clang/AST/TypeLoc.h"
Douglas Gregore3e01a22009-04-01 22:41:11 +000022#include "clang/Lex/Preprocessor.h"
Chris Lattnerda48a8e2006-08-04 05:25:55 +000023#include "clang/Basic/LangOptions.h"
Chris Lattner1ce41ed2009-01-20 19:11:22 +000024#include "llvm/ADT/STLExtras.h"
John McCall898cd0f2009-08-03 18:47:27 +000025#include "llvm/Support/ErrorHandling.h"
Douglas Gregor52537682009-03-19 00:18:19 +000026#include <cstring>
Chris Lattnerb9093cd2006-08-04 04:39:53 +000027using namespace clang;
28
Chris Lattner3b0f3ef2008-11-22 08:32:36 +000029
30static DiagnosticBuilder Diag(Diagnostic &D, SourceLocation Loc,
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000031 unsigned DiagID) {
32 return D.Report(Loc, DiagID);
Chris Lattner3b0f3ef2008-11-22 08:32:36 +000033}
34
Douglas Gregorb53edfb2009-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 Gregor9de54ea2010-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 Gregor90c99722011-02-24 00:17:56 +000052void CXXScopeSpec::Extend(ASTContext &Context, SourceLocation TemplateKWLoc,
53 TypeLoc TL, SourceLocation ColonColonLoc) {
Douglas Gregor9b272512011-02-28 23:58:31 +000054 Builder.Extend(Context, TemplateKWLoc, TL, ColonColonLoc);
Douglas Gregor90c99722011-02-24 00:17:56 +000055 if (Range.getBegin().isInvalid())
56 Range.setBegin(TL.getBeginLoc());
57 Range.setEnd(ColonColonLoc);
Douglas Gregor869ad452011-02-24 17:54:50 +000058
Douglas Gregor9b272512011-02-28 23:58:31 +000059 assert(Range == Builder.getSourceRange() &&
Douglas Gregor869ad452011-02-24 17:54:50 +000060 "NestedNameSpecifierLoc range computation incorrect");
Douglas Gregor90c99722011-02-24 00:17:56 +000061}
62
63void CXXScopeSpec::Extend(ASTContext &Context, IdentifierInfo *Identifier,
64 SourceLocation IdentifierLoc,
65 SourceLocation ColonColonLoc) {
Douglas Gregor9b272512011-02-28 23:58:31 +000066 Builder.Extend(Context, Identifier, IdentifierLoc, ColonColonLoc);
67
Douglas Gregor90c99722011-02-24 00:17:56 +000068 if (Range.getBegin().isInvalid())
69 Range.setBegin(IdentifierLoc);
70 Range.setEnd(ColonColonLoc);
Douglas Gregor869ad452011-02-24 17:54:50 +000071
Douglas Gregor9b272512011-02-28 23:58:31 +000072 assert(Range == Builder.getSourceRange() &&
Douglas Gregor869ad452011-02-24 17:54:50 +000073 "NestedNameSpecifierLoc range computation incorrect");
Douglas Gregor90c99722011-02-24 00:17:56 +000074}
75
76void CXXScopeSpec::Extend(ASTContext &Context, NamespaceDecl *Namespace,
77 SourceLocation NamespaceLoc,
78 SourceLocation ColonColonLoc) {
Douglas Gregor9b272512011-02-28 23:58:31 +000079 Builder.Extend(Context, Namespace, NamespaceLoc, ColonColonLoc);
80
Douglas Gregor90c99722011-02-24 00:17:56 +000081 if (Range.getBegin().isInvalid())
82 Range.setBegin(NamespaceLoc);
83 Range.setEnd(ColonColonLoc);
Douglas Gregor869ad452011-02-24 17:54:50 +000084
Douglas Gregor9b272512011-02-28 23:58:31 +000085 assert(Range == Builder.getSourceRange() &&
Douglas Gregor869ad452011-02-24 17:54:50 +000086 "NestedNameSpecifierLoc range computation incorrect");
Douglas Gregor90c99722011-02-24 00:17:56 +000087}
88
Douglas Gregor7b26ff92011-02-24 02:36:08 +000089void CXXScopeSpec::Extend(ASTContext &Context, NamespaceAliasDecl *Alias,
90 SourceLocation AliasLoc,
91 SourceLocation ColonColonLoc) {
Douglas Gregor9b272512011-02-28 23:58:31 +000092 Builder.Extend(Context, Alias, AliasLoc, ColonColonLoc);
93
Douglas Gregor7b26ff92011-02-24 02:36:08 +000094 if (Range.getBegin().isInvalid())
95 Range.setBegin(AliasLoc);
96 Range.setEnd(ColonColonLoc);
Douglas Gregor869ad452011-02-24 17:54:50 +000097
Douglas Gregor9b272512011-02-28 23:58:31 +000098 assert(Range == Builder.getSourceRange() &&
Douglas Gregor869ad452011-02-24 17:54:50 +000099 "NestedNameSpecifierLoc range computation incorrect");
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000100}
101
Douglas Gregor90c99722011-02-24 00:17:56 +0000102void CXXScopeSpec::MakeGlobal(ASTContext &Context,
103 SourceLocation ColonColonLoc) {
Douglas Gregor9b272512011-02-28 23:58:31 +0000104 Builder.MakeGlobal(Context, ColonColonLoc);
105
Douglas Gregor90c99722011-02-24 00:17:56 +0000106 Range = SourceRange(ColonColonLoc);
Douglas Gregor869ad452011-02-24 17:54:50 +0000107
Douglas Gregor9b272512011-02-28 23:58:31 +0000108 assert(Range == Builder.getSourceRange() &&
Douglas Gregor869ad452011-02-24 17:54:50 +0000109 "NestedNameSpecifierLoc range computation incorrect");
110}
111
112void CXXScopeSpec::MakeTrivial(ASTContext &Context,
113 NestedNameSpecifier *Qualifier, SourceRange R) {
Douglas Gregor9b272512011-02-28 23:58:31 +0000114 Builder.MakeTrivial(Context, Qualifier, R);
Douglas Gregor869ad452011-02-24 17:54:50 +0000115 Range = R;
Douglas Gregor869ad452011-02-24 17:54:50 +0000116}
117
118void CXXScopeSpec::Adopt(NestedNameSpecifierLoc Other) {
119 if (!Other) {
120 Range = SourceRange();
Douglas Gregor9b272512011-02-28 23:58:31 +0000121 Builder.Clear();
Douglas Gregor869ad452011-02-24 17:54:50 +0000122 return;
123 }
Douglas Gregor9b272512011-02-28 23:58:31 +0000124
Douglas Gregor869ad452011-02-24 17:54:50 +0000125 Range = Other.getSourceRange();
Douglas Gregor9b272512011-02-28 23:58:31 +0000126 Builder.Adopt(Other);
Douglas Gregor869ad452011-02-24 17:54:50 +0000127}
128
Douglas Gregor14454802011-02-25 02:25:35 +0000129NestedNameSpecifierLoc
130CXXScopeSpec::getWithLocInContext(ASTContext &Context) const {
Douglas Gregor2b1ca9e2011-03-03 21:48:55 +0000131 if (!Builder.getRepresentation())
Douglas Gregor869ad452011-02-24 17:54:50 +0000132 return NestedNameSpecifierLoc();
133
Douglas Gregor9b272512011-02-28 23:58:31 +0000134 return Builder.getWithLocInContext(Context);
Douglas Gregor90c99722011-02-24 00:17:56 +0000135}
136
Chris Lattner1ce41ed2009-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 McCall084e83d2011-03-24 11:26:52 +0000139DeclaratorChunk DeclaratorChunk::getFunction(bool hasProto, bool isVariadic,
Douglas Gregor94349fd2009-02-18 07:07:28 +0000140 SourceLocation EllipsisLoc,
Chris Lattner1ce41ed2009-01-20 19:11:22 +0000141 ParamInfo *ArgInfo,
142 unsigned NumArgs,
143 unsigned TypeQuals,
Douglas Gregor54992352011-01-26 03:43:54 +0000144 bool RefQualifierIsLvalueRef,
145 SourceLocation RefQualifierLoc,
Sebastian Redl802a4532011-03-05 22:42:13 +0000146 ExceptionSpecificationType
147 ESpecType,
148 SourceLocation ESpecLoc,
John McCallba7bf592010-08-24 05:47:05 +0000149 ParsedType *Exceptions,
Sebastian Redld6434562009-05-29 18:02:33 +0000150 SourceRange *ExceptionRanges,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +0000151 unsigned NumExceptions,
Sebastian Redl802a4532011-03-05 22:42:13 +0000152 Expr *NoexceptExpr,
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000153 SourceLocation LocalRangeBegin,
154 SourceLocation LocalRangeEnd,
Douglas Gregor7fb25412010-10-01 18:44:50 +0000155 Declarator &TheDeclarator,
156 ParsedType TrailingReturnType) {
Chris Lattner1ce41ed2009-01-20 19:11:22 +0000157 DeclaratorChunk I;
Sebastian Redl802a4532011-03-05 22:42:13 +0000158 I.Kind = Function;
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000159 I.Loc = LocalRangeBegin;
160 I.EndLoc = LocalRangeEnd;
John McCall084e83d2011-03-24 11:26:52 +0000161 I.Fun.AttrList = 0;
Sebastian Redl802a4532011-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 Gregor54992352011-01-26 03:43:54 +0000169 I.Fun.RefQualifierIsLValueRef = RefQualifierIsLvalueRef;
Sebastian Redl802a4532011-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 Gregor7fb25412010-10-01 18:44:50 +0000176 I.Fun.TrailingReturnType = TrailingReturnType.getAsOpaquePtr();
Sebastian Redl2b9cacb2009-04-29 17:30:04 +0000177
Chris Lattner1ce41ed2009-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 Stump11289f42009-09-09 15:08:12 +0000184 if (!TheDeclarator.InlineParamsUsed &&
Chris Lattner1ce41ed2009-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 Redl802a4532011-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 Redld6434562009-05-29 18:02:33 +0000208 }
Sebastian Redl802a4532011-03-05 22:42:13 +0000209 break;
210
211 case EST_ComputedNoexcept:
212 I.Fun.NoexceptExpr = NoexceptExpr;
213 break;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +0000214 }
Chris Lattner1ce41ed2009-01-20 19:11:22 +0000215 return I;
216}
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000217
Douglas Gregorc15b0cf2011-06-25 00:56:27 +0000218bool Declarator::isDeclarationOfFunction() const {
Richard Smithcfcdf3a2011-06-25 02:28:38 +0000219 for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) {
220 switch (DeclTypeInfo[i].Kind) {
221 case DeclaratorChunk::Function:
222 return true;
223 case DeclaratorChunk::Paren:
224 continue;
225 case DeclaratorChunk::Pointer:
226 case DeclaratorChunk::Reference:
227 case DeclaratorChunk::Array:
228 case DeclaratorChunk::BlockPointer:
229 case DeclaratorChunk::MemberPointer:
230 return false;
231 }
232 llvm_unreachable("Invalid type chunk");
233 return false;
234 }
Douglas Gregorc15b0cf2011-06-25 00:56:27 +0000235
236 switch (DS.getTypeSpecType()) {
237 case TST_auto:
238 case TST_bool:
239 case TST_char:
240 case TST_char16:
241 case TST_char32:
242 case TST_class:
243 case TST_decimal128:
244 case TST_decimal32:
245 case TST_decimal64:
246 case TST_double:
247 case TST_enum:
248 case TST_error:
249 case TST_float:
250 case TST_int:
251 case TST_struct:
252 case TST_union:
253 case TST_unknown_anytype:
254 case TST_unspecified:
255 case TST_void:
256 case TST_wchar:
257 return false;
258
259 case TST_decltype:
260 case TST_typeofExpr:
261 if (Expr *E = DS.getRepAsExpr())
262 return E->getType()->isFunctionType();
263 return false;
264
265 case TST_underlyingType:
266 case TST_typename:
267 case TST_typeofType: {
268 QualType QT = DS.getRepAsType().get();
269 if (QT.isNull())
270 return false;
271
272 if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT))
273 QT = LIT->getType();
274
275 if (QT.isNull())
276 return false;
277
278 return QT->isFunctionType();
279 }
280 }
281
282 return false;
283}
284
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000285/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
Chris Lattnere0c51162009-02-27 18:35:46 +0000286/// declaration specifier includes.
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000287///
288unsigned DeclSpec::getParsedSpecifiers() const {
289 unsigned Res = 0;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000290 if (StorageClassSpec != SCS_unspecified ||
291 SCS_thread_specified)
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000292 Res |= PQ_StorageClassSpecifier;
Mike Stump65643c62008-06-19 19:52:46 +0000293
Chris Lattner4d8f8732006-11-28 05:05:08 +0000294 if (TypeQualifiers != TQ_unspecified)
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000295 Res |= PQ_TypeQualifier;
Mike Stump11289f42009-09-09 15:08:12 +0000296
Chris Lattnerf055d432006-11-28 04:28:12 +0000297 if (hasTypeSpecifier())
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000298 Res |= PQ_TypeSpecifier;
Mike Stump11289f42009-09-09 15:08:12 +0000299
Douglas Gregor61956c42008-10-31 09:07:45 +0000300 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000301 Res |= PQ_FunctionSpecifier;
302 return Res;
303}
304
John McCall49bfce42009-08-03 20:12:06 +0000305template <class T> static bool BadSpecifier(T TNew, T TPrev,
306 const char *&PrevSpec,
307 unsigned &DiagID) {
John McCall898cd0f2009-08-03 18:47:27 +0000308 PrevSpec = DeclSpec::getSpecifierName(TPrev);
John McCall49bfce42009-08-03 20:12:06 +0000309 DiagID = (TNew == TPrev ? diag::ext_duplicate_declspec
310 : diag::err_invalid_decl_spec_combination);
John McCall898cd0f2009-08-03 18:47:27 +0000311 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000312}
John McCall898cd0f2009-08-03 18:47:27 +0000313
Chris Lattner7bd11fe2007-01-23 04:35:33 +0000314const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000315 switch (S) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000316 case DeclSpec::SCS_unspecified: return "unspecified";
317 case DeclSpec::SCS_typedef: return "typedef";
318 case DeclSpec::SCS_extern: return "extern";
319 case DeclSpec::SCS_static: return "static";
320 case DeclSpec::SCS_auto: return "auto";
321 case DeclSpec::SCS_register: return "register";
Eli Friedmand5c0eed2009-04-19 20:27:55 +0000322 case DeclSpec::SCS_private_extern: return "__private_extern__";
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000323 case DeclSpec::SCS_mutable: return "mutable";
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000324 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000325 llvm_unreachable("Unknown typespec!");
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000326}
327
John McCall898cd0f2009-08-03 18:47:27 +0000328const char *DeclSpec::getSpecifierName(TSW W) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000329 switch (W) {
John McCall898cd0f2009-08-03 18:47:27 +0000330 case TSW_unspecified: return "unspecified";
331 case TSW_short: return "short";
332 case TSW_long: return "long";
333 case TSW_longlong: return "long long";
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000334 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000335 llvm_unreachable("Unknown typespec!");
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000336}
337
John McCall898cd0f2009-08-03 18:47:27 +0000338const char *DeclSpec::getSpecifierName(TSC C) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000339 switch (C) {
John McCall898cd0f2009-08-03 18:47:27 +0000340 case TSC_unspecified: return "unspecified";
341 case TSC_imaginary: return "imaginary";
342 case TSC_complex: return "complex";
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000343 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000344 llvm_unreachable("Unknown typespec!");
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000345}
346
347
John McCall898cd0f2009-08-03 18:47:27 +0000348const char *DeclSpec::getSpecifierName(TSS S) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000349 switch (S) {
John McCall898cd0f2009-08-03 18:47:27 +0000350 case TSS_unspecified: return "unspecified";
351 case TSS_signed: return "signed";
352 case TSS_unsigned: return "unsigned";
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000353 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000354 llvm_unreachable("Unknown typespec!");
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000355}
356
Chris Lattner69680ea2007-01-23 04:34:43 +0000357const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000358 switch (T) {
Chris Lattner839713c2006-08-04 06:15:52 +0000359 case DeclSpec::TST_unspecified: return "unspecified";
360 case DeclSpec::TST_void: return "void";
361 case DeclSpec::TST_char: return "char";
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000362 case DeclSpec::TST_wchar: return "wchar_t";
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000363 case DeclSpec::TST_char16: return "char16_t";
364 case DeclSpec::TST_char32: return "char32_t";
Chris Lattner839713c2006-08-04 06:15:52 +0000365 case DeclSpec::TST_int: return "int";
366 case DeclSpec::TST_float: return "float";
367 case DeclSpec::TST_double: return "double";
368 case DeclSpec::TST_bool: return "_Bool";
369 case DeclSpec::TST_decimal32: return "_Decimal32";
370 case DeclSpec::TST_decimal64: return "_Decimal64";
371 case DeclSpec::TST_decimal128: return "_Decimal128";
Chris Lattnerda72c822006-08-13 22:16:42 +0000372 case DeclSpec::TST_enum: return "enum";
Chris Lattner861a2262008-04-13 18:59:07 +0000373 case DeclSpec::TST_class: return "class";
Chris Lattnerda72c822006-08-13 22:16:42 +0000374 case DeclSpec::TST_union: return "union";
375 case DeclSpec::TST_struct: return "struct";
Douglas Gregor9817f4a2009-02-09 15:09:02 +0000376 case DeclSpec::TST_typename: return "type-name";
Steve Naroffad373bd2007-07-31 12:34:36 +0000377 case DeclSpec::TST_typeofType:
378 case DeclSpec::TST_typeofExpr: return "typeof";
John McCall898cd0f2009-08-03 18:47:27 +0000379 case DeclSpec::TST_auto: return "auto";
380 case DeclSpec::TST_decltype: return "(decltype)";
Alexis Hunte852b102011-05-24 22:41:36 +0000381 case DeclSpec::TST_underlyingType: return "__underlying_type";
John McCall39439732011-04-09 22:50:59 +0000382 case DeclSpec::TST_unknown_anytype: return "__unknown_anytype";
John McCall898cd0f2009-08-03 18:47:27 +0000383 case DeclSpec::TST_error: return "(error)";
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000384 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000385 llvm_unreachable("Unknown typespec!");
Chris Lattner839713c2006-08-04 06:15:52 +0000386}
387
John McCall898cd0f2009-08-03 18:47:27 +0000388const char *DeclSpec::getSpecifierName(TQ T) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000389 switch (T) {
John McCall898cd0f2009-08-03 18:47:27 +0000390 case DeclSpec::TQ_unspecified: return "unspecified";
391 case DeclSpec::TQ_const: return "const";
392 case DeclSpec::TQ_restrict: return "restrict";
393 case DeclSpec::TQ_volatile: return "volatile";
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000394 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000395 llvm_unreachable("Unknown typespec!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000396}
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000397
Chris Lattner4d8f8732006-11-28 05:05:08 +0000398bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
John McCall49bfce42009-08-03 20:12:06 +0000399 const char *&PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +0000400 unsigned &DiagID,
401 const LangOptions &Lang) {
402 // OpenCL prohibits extern, auto, register, and static
403 // It seems sensible to prohibit private_extern too
404 if (Lang.OpenCL) {
405 switch (S) {
406 case SCS_extern:
407 case SCS_private_extern:
408 case SCS_auto:
409 case SCS_register:
410 case SCS_static:
411 DiagID = diag::err_not_opencl_storage_class_specifier;
412 PrevSpec = getSpecifierName(S);
413 return true;
414 default:
415 break;
416 }
417 }
418
Abramo Bagnaraed5b6892010-07-30 16:47:02 +0000419 if (StorageClassSpec != SCS_unspecified) {
420 // Changing storage class is allowed only if the previous one
421 // was the 'extern' that is part of a linkage specification and
422 // the new storage class is 'typedef'.
423 if (!(SCS_extern_in_linkage_spec &&
424 StorageClassSpec == SCS_extern &&
425 S == SCS_typedef))
426 return BadSpecifier(S, (SCS)StorageClassSpec, PrevSpec, DiagID);
427 }
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000428 StorageClassSpec = S;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000429 StorageClassSpecLoc = Loc;
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000430 assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield");
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000431 return false;
432}
433
Mike Stump11289f42009-09-09 15:08:12 +0000434bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
John McCall49bfce42009-08-03 20:12:06 +0000435 const char *&PrevSpec,
436 unsigned &DiagID) {
Chris Lattner353f5742006-11-28 04:50:12 +0000437 if (SCS_thread_specified) {
438 PrevSpec = "__thread";
John McCall49bfce42009-08-03 20:12:06 +0000439 DiagID = diag::ext_duplicate_declspec;
Chris Lattner353f5742006-11-28 04:50:12 +0000440 return true;
441 }
442 SCS_thread_specified = true;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000443 SCS_threadLoc = Loc;
Chris Lattner353f5742006-11-28 04:50:12 +0000444 return false;
445}
446
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000447/// These methods set the specified attribute of the DeclSpec, but return true
448/// and ignore the request if invalid (e.g. "extern" then "auto" is
449/// specified).
Chris Lattnerb20e8942006-11-28 05:30:29 +0000450bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
John McCall49bfce42009-08-03 20:12:06 +0000451 const char *&PrevSpec,
452 unsigned &DiagID) {
Abramo Bagnaraead67d92011-03-06 22:21:56 +0000453 // Overwrite TSWLoc only if TypeSpecWidth was unspecified, so that
454 // for 'long long' we will keep the source location of the first 'long'.
455 if (TypeSpecWidth == TSW_unspecified)
456 TSWLoc = Loc;
457 // Allow turning long -> long long.
458 else if (W != TSW_longlong || TypeSpecWidth != TSW_long)
John McCall49bfce42009-08-03 20:12:06 +0000459 return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000460 TypeSpecWidth = W;
Chris Lattner37141f42010-06-23 06:00:24 +0000461 if (TypeAltiVecVector && !TypeAltiVecBool &&
462 ((TypeSpecWidth == TSW_long) || (TypeSpecWidth == TSW_longlong))) {
John Thompson22334602010-02-05 00:12:22 +0000463 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
464 DiagID = diag::warn_vector_long_decl_spec_combination;
465 return true;
466 }
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000467 return false;
468}
469
Mike Stump11289f42009-09-09 15:08:12 +0000470bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
John McCall49bfce42009-08-03 20:12:06 +0000471 const char *&PrevSpec,
472 unsigned &DiagID) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000473 if (TypeSpecComplex != TSC_unspecified)
John McCall49bfce42009-08-03 20:12:06 +0000474 return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000475 TypeSpecComplex = C;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000476 TSCLoc = Loc;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000477 return false;
478}
479
Mike Stump11289f42009-09-09 15:08:12 +0000480bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
John McCall49bfce42009-08-03 20:12:06 +0000481 const char *&PrevSpec,
482 unsigned &DiagID) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000483 if (TypeSpecSign != TSS_unspecified)
John McCall49bfce42009-08-03 20:12:06 +0000484 return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID);
Chris Lattnerdeb42f52006-08-04 05:26:52 +0000485 TypeSpecSign = S;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000486 TSSLoc = Loc;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000487 return false;
488}
489
Chris Lattnerb20e8942006-11-28 05:30:29 +0000490bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
John McCall49bfce42009-08-03 20:12:06 +0000491 const char *&PrevSpec,
492 unsigned &DiagID,
John McCallba7bf592010-08-24 05:47:05 +0000493 ParsedType Rep) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +0000494 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep);
495}
496
497bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
498 SourceLocation TagNameLoc,
499 const char *&PrevSpec,
500 unsigned &DiagID,
501 ParsedType Rep) {
John McCallba7bf592010-08-24 05:47:05 +0000502 assert(isTypeRep(T) && "T does not store a type");
503 assert(Rep && "no type provided!");
504 if (TypeSpecType != TST_unspecified) {
505 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
506 DiagID = diag::err_invalid_decl_spec_combination;
507 return true;
508 }
509 TypeSpecType = T;
510 TypeRep = Rep;
Abramo Bagnara9875a3c2011-03-16 20:16:18 +0000511 TSTLoc = TagKwLoc;
512 TSTNameLoc = TagNameLoc;
John McCallba7bf592010-08-24 05:47:05 +0000513 TypeSpecOwned = false;
514 return false;
515}
516
517bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
518 const char *&PrevSpec,
519 unsigned &DiagID,
520 Expr *Rep) {
521 assert(isExprRep(T) && "T does not store an expr");
522 assert(Rep && "no expression provided!");
523 if (TypeSpecType != TST_unspecified) {
524 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
525 DiagID = diag::err_invalid_decl_spec_combination;
526 return true;
527 }
528 TypeSpecType = T;
529 ExprRep = Rep;
530 TSTLoc = Loc;
Abramo Bagnara9875a3c2011-03-16 20:16:18 +0000531 TSTNameLoc = Loc;
John McCallba7bf592010-08-24 05:47:05 +0000532 TypeSpecOwned = false;
533 return false;
534}
535
536bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
537 const char *&PrevSpec,
538 unsigned &DiagID,
539 Decl *Rep, bool Owned) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +0000540 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep, Owned);
541}
542
543bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
544 SourceLocation TagNameLoc,
545 const char *&PrevSpec,
546 unsigned &DiagID,
547 Decl *Rep, bool Owned) {
John McCallba7bf592010-08-24 05:47:05 +0000548 assert(isDeclRep(T) && "T does not store a decl");
549 // Unlike the other cases, we don't assert that we actually get a decl.
550
551 if (TypeSpecType != TST_unspecified) {
552 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
553 DiagID = diag::err_invalid_decl_spec_combination;
554 return true;
555 }
556 TypeSpecType = T;
557 DeclRep = Rep;
Abramo Bagnara9875a3c2011-03-16 20:16:18 +0000558 TSTLoc = TagKwLoc;
559 TSTNameLoc = TagNameLoc;
John McCallba7bf592010-08-24 05:47:05 +0000560 TypeSpecOwned = Owned;
561 return false;
562}
563
564bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
565 const char *&PrevSpec,
566 unsigned &DiagID) {
567 assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) &&
568 "rep required for these type-spec kinds!");
John McCall49bfce42009-08-03 20:12:06 +0000569 if (TypeSpecType != TST_unspecified) {
570 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
571 DiagID = diag::err_invalid_decl_spec_combination;
572 return true;
573 }
Abramo Bagnara9875a3c2011-03-16 20:16:18 +0000574 TSTLoc = Loc;
575 TSTNameLoc = Loc;
Chris Lattner37141f42010-06-23 06:00:24 +0000576 if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) {
577 TypeAltiVecBool = true;
Chris Lattner37141f42010-06-23 06:00:24 +0000578 return false;
579 }
Chris Lattnerdeb42f52006-08-04 05:26:52 +0000580 TypeSpecType = T;
John McCallba7bf592010-08-24 05:47:05 +0000581 TypeSpecOwned = false;
Chris Lattner37141f42010-06-23 06:00:24 +0000582 if (TypeAltiVecVector && !TypeAltiVecBool && (TypeSpecType == TST_double)) {
John Thompson22334602010-02-05 00:12:22 +0000583 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
Chris Lattner37141f42010-06-23 06:00:24 +0000584 DiagID = diag::err_invalid_vector_decl_spec;
John Thompson22334602010-02-05 00:12:22 +0000585 return true;
586 }
587 return false;
588}
589
590bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
591 const char *&PrevSpec, unsigned &DiagID) {
592 if (TypeSpecType != TST_unspecified) {
593 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
594 DiagID = diag::err_invalid_vector_decl_spec_combination;
595 return true;
596 }
597 TypeAltiVecVector = isAltiVecVector;
598 AltiVecLoc = Loc;
599 return false;
600}
601
602bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
603 const char *&PrevSpec, unsigned &DiagID) {
Chris Lattner37141f42010-06-23 06:00:24 +0000604 if (!TypeAltiVecVector || TypeAltiVecPixel ||
605 (TypeSpecType != TST_unspecified)) {
John Thompson22334602010-02-05 00:12:22 +0000606 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
607 DiagID = diag::err_invalid_pixel_decl_spec_combination;
608 return true;
609 }
John Thompson22334602010-02-05 00:12:22 +0000610 TypeAltiVecPixel = isAltiVecPixel;
611 TSTLoc = Loc;
Abramo Bagnara9875a3c2011-03-16 20:16:18 +0000612 TSTNameLoc = Loc;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000613 return false;
614}
615
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000616bool DeclSpec::SetTypeSpecError() {
617 TypeSpecType = TST_error;
John McCalla3707cc2010-08-26 17:22:34 +0000618 TypeSpecOwned = false;
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000619 TSTLoc = SourceLocation();
Abramo Bagnara9875a3c2011-03-16 20:16:18 +0000620 TSTNameLoc = SourceLocation();
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000621 return false;
622}
623
Chris Lattner60809f52006-11-28 05:18:46 +0000624bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +0000625 unsigned &DiagID, const LangOptions &Lang) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000626 // Duplicates turn into warnings pre-C99.
627 if ((TypeQualifiers & T) && !Lang.C99)
John McCall49bfce42009-08-03 20:12:06 +0000628 return BadSpecifier(T, T, PrevSpec, DiagID);
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000629 TypeQualifiers |= T;
Mike Stump11289f42009-09-09 15:08:12 +0000630
Chris Lattner60809f52006-11-28 05:18:46 +0000631 switch (T) {
632 default: assert(0 && "Unknown type qualifier!");
633 case TQ_const: TQ_constLoc = Loc; break;
634 case TQ_restrict: TQ_restrictLoc = Loc; break;
635 case TQ_volatile: TQ_volatileLoc = Loc; break;
636 }
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000637 return false;
638}
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000639
John McCall49bfce42009-08-03 20:12:06 +0000640bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
641 unsigned &DiagID) {
Chris Lattnera925dc62006-11-28 04:33:46 +0000642 // 'inline inline' is ok.
643 FS_inline_specified = true;
Chris Lattner1b22eed2006-11-28 05:12:07 +0000644 FS_inlineLoc = Loc;
Chris Lattnera925dc62006-11-28 04:33:46 +0000645 return false;
646}
647
John McCall49bfce42009-08-03 20:12:06 +0000648bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec,
649 unsigned &DiagID) {
Douglas Gregor61956c42008-10-31 09:07:45 +0000650 // 'virtual virtual' is ok.
651 FS_virtual_specified = true;
652 FS_virtualLoc = Loc;
653 return false;
654}
655
John McCall49bfce42009-08-03 20:12:06 +0000656bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec,
657 unsigned &DiagID) {
Douglas Gregor61956c42008-10-31 09:07:45 +0000658 // 'explicit explicit' is ok.
659 FS_explicit_specified = true;
660 FS_explicitLoc = Loc;
661 return false;
662}
663
John McCall49bfce42009-08-03 20:12:06 +0000664bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
665 unsigned &DiagID) {
Anders Carlssoncd8db412009-05-06 04:46:28 +0000666 if (Friend_specified) {
667 PrevSpec = "friend";
John McCall49bfce42009-08-03 20:12:06 +0000668 DiagID = diag::ext_duplicate_declspec;
Anders Carlssoncd8db412009-05-06 04:46:28 +0000669 return true;
670 }
John McCall49bfce42009-08-03 20:12:06 +0000671
Anders Carlssoncd8db412009-05-06 04:46:28 +0000672 Friend_specified = true;
673 FriendLoc = Loc;
674 return false;
675}
Chris Lattnera925dc62006-11-28 04:33:46 +0000676
Sebastian Redl39c2a8b2009-11-05 15:47:02 +0000677bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
678 unsigned &DiagID) {
679 // 'constexpr constexpr' is ok.
680 Constexpr_specified = true;
681 ConstexprLoc = Loc;
682 return false;
683}
684
John McCall48871652010-08-21 09:40:31 +0000685void DeclSpec::setProtocolQualifiers(Decl * const *Protos,
Argyrios Kyrtzidis5ec645b2009-09-29 19:42:11 +0000686 unsigned NP,
687 SourceLocation *ProtoLocs,
688 SourceLocation LAngleLoc) {
689 if (NP == 0) return;
John McCall48871652010-08-21 09:40:31 +0000690 ProtocolQualifiers = new Decl*[NP];
Argyrios Kyrtzidis5ec645b2009-09-29 19:42:11 +0000691 ProtocolLocs = new SourceLocation[NP];
John McCall48871652010-08-21 09:40:31 +0000692 memcpy((void*)ProtocolQualifiers, Protos, sizeof(Decl*)*NP);
Argyrios Kyrtzidis5ec645b2009-09-29 19:42:11 +0000693 memcpy(ProtocolLocs, ProtoLocs, sizeof(SourceLocation)*NP);
694 NumProtocolQualifiers = NP;
695 ProtocolLAngleLoc = LAngleLoc;
696}
697
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000698void DeclSpec::SaveWrittenBuiltinSpecs() {
699 writtenBS.Sign = getTypeSpecSign();
700 writtenBS.Width = getTypeSpecWidth();
701 writtenBS.Type = getTypeSpecType();
702 // Search the list of attributes for the presence of a mode attribute.
703 writtenBS.ModeAttr = false;
John McCall53fa7142010-12-24 02:08:15 +0000704 AttributeList* attrs = getAttributes().getList();
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000705 while (attrs) {
706 if (attrs->getKind() == AttributeList::AT_mode) {
707 writtenBS.ModeAttr = true;
708 break;
709 }
710 attrs = attrs->getNext();
711 }
712}
713
Abramo Bagnaraed5b6892010-07-30 16:47:02 +0000714void DeclSpec::SaveStorageSpecifierAsWritten() {
715 if (SCS_extern_in_linkage_spec && StorageClassSpec == SCS_extern)
716 // If 'extern' is part of a linkage specification,
717 // then it is not a storage class "as written".
718 StorageClassSpecAsWritten = SCS_unspecified;
719 else
720 StorageClassSpecAsWritten = StorageClassSpec;
721}
722
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000723/// Finish - This does final analysis of the declspec, rejecting things like
724/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
725/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
726/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Douglas Gregore3e01a22009-04-01 22:41:11 +0000727void DeclSpec::Finish(Diagnostic &D, Preprocessor &PP) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000728 // Before possibly changing their values, save specs as written.
729 SaveWrittenBuiltinSpecs();
Douglas Gregorc4df4072010-04-19 22:54:31 +0000730 SaveStorageSpecifierAsWritten();
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000731
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000732 // Check the type specifier components first.
733
Chris Lattner37141f42010-06-23 06:00:24 +0000734 // Validate and finalize AltiVec vector declspec.
735 if (TypeAltiVecVector) {
736 if (TypeAltiVecBool) {
737 // Sign specifiers are not allowed with vector bool. (PIM 2.1)
738 if (TypeSpecSign != TSS_unspecified) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000739 Diag(D, TSSLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner37141f42010-06-23 06:00:24 +0000740 << getSpecifierName((TSS)TypeSpecSign);
741 }
742
743 // Only char/int are valid with vector bool. (PIM 2.1)
Duncan Sandsd3e231e2010-06-23 19:34:52 +0000744 if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) &&
745 (TypeSpecType != TST_int)) || TypeAltiVecPixel) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000746 Diag(D, TSTLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner37141f42010-06-23 06:00:24 +0000747 << (TypeAltiVecPixel ? "__pixel" :
748 getSpecifierName((TST)TypeSpecType));
749 }
750
751 // Only 'short' is valid with vector bool. (PIM 2.1)
752 if ((TypeSpecWidth != TSW_unspecified) && (TypeSpecWidth != TSW_short))
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000753 Diag(D, TSWLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner37141f42010-06-23 06:00:24 +0000754 << getSpecifierName((TSW)TypeSpecWidth);
755
756 // Elements of vector bool are interpreted as unsigned. (PIM 2.1)
757 if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) ||
758 (TypeSpecWidth != TSW_unspecified))
759 TypeSpecSign = TSS_unsigned;
760 }
761
762 if (TypeAltiVecPixel) {
763 //TODO: perform validation
764 TypeSpecType = TST_int;
765 TypeSpecSign = TSS_unsigned;
766 TypeSpecWidth = TSW_short;
John McCalla3707cc2010-08-26 17:22:34 +0000767 TypeSpecOwned = false;
Chris Lattner37141f42010-06-23 06:00:24 +0000768 }
769 }
770
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000771 // signed/unsigned are only valid with int/char/wchar_t.
Chris Lattner839713c2006-08-04 06:15:52 +0000772 if (TypeSpecSign != TSS_unspecified) {
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000773 if (TypeSpecType == TST_unspecified)
774 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000775 else if (TypeSpecType != TST_int &&
776 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000777 Diag(D, TSSLoc, diag::err_invalid_sign_spec)
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000778 << getSpecifierName((TST)TypeSpecType);
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000779 // signed double -> double.
Chris Lattner839713c2006-08-04 06:15:52 +0000780 TypeSpecSign = TSS_unspecified;
781 }
782 }
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000783
Chris Lattner839713c2006-08-04 06:15:52 +0000784 // Validate the width of the type.
785 switch (TypeSpecWidth) {
786 case TSW_unspecified: break;
787 case TSW_short: // short int
788 case TSW_longlong: // long long int
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000789 if (TypeSpecType == TST_unspecified)
790 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
791 else if (TypeSpecType != TST_int) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000792 Diag(D, TSWLoc,
Chris Lattner36982e42007-05-16 17:49:37 +0000793 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000794 : diag::err_invalid_longlong_spec)
795 << getSpecifierName((TST)TypeSpecType);
Chris Lattner839713c2006-08-04 06:15:52 +0000796 TypeSpecType = TST_int;
John McCalla3707cc2010-08-26 17:22:34 +0000797 TypeSpecOwned = false;
Chris Lattner839713c2006-08-04 06:15:52 +0000798 }
799 break;
800 case TSW_long: // long double, long int
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000801 if (TypeSpecType == TST_unspecified)
802 TypeSpecType = TST_int; // long -> long int.
803 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000804 Diag(D, TSWLoc, diag::err_invalid_long_spec)
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000805 << getSpecifierName((TST)TypeSpecType);
Chris Lattner839713c2006-08-04 06:15:52 +0000806 TypeSpecType = TST_int;
John McCalla3707cc2010-08-26 17:22:34 +0000807 TypeSpecOwned = false;
Chris Lattner839713c2006-08-04 06:15:52 +0000808 }
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000809 break;
Chris Lattner839713c2006-08-04 06:15:52 +0000810 }
Mike Stump11289f42009-09-09 15:08:12 +0000811
Chris Lattner0e894622006-08-13 19:58:17 +0000812 // TODO: if the implementation does not implement _Complex or _Imaginary,
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000813 // disallow their use. Need information about the backend.
814 if (TypeSpecComplex != TSC_unspecified) {
815 if (TypeSpecType == TST_unspecified) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000816 Diag(D, TSCLoc, diag::ext_plain_complex)
Douglas Gregora771f462010-03-31 17:46:05 +0000817 << FixItHint::CreateInsertion(
Douglas Gregore3e01a22009-04-01 22:41:11 +0000818 PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
819 " double");
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000820 TypeSpecType = TST_double; // _Complex -> _Complex double.
821 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000822 // Note that this intentionally doesn't include _Complex _Bool.
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000823 Diag(D, TSTLoc, diag::ext_integer_complex);
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000824 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000825 Diag(D, TSCLoc, diag::err_invalid_complex_spec)
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000826 << getSpecifierName((TST)TypeSpecType);
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000827 TypeSpecComplex = TSC_unspecified;
828 }
829 }
Chris Lattner8e90ef62006-08-05 03:30:45 +0000830
John McCall07e91c02009-08-06 02:15:43 +0000831 // C++ [class.friend]p6:
832 // No storage-class-specifier shall appear in the decl-specifier-seq
833 // of a friend declaration.
834 if (isFriendSpecified() && getStorageClassSpec()) {
835 DeclSpec::SCS SC = getStorageClassSpec();
836 const char *SpecName = getSpecifierName(SC);
837
838 SourceLocation SCLoc = getStorageClassSpecLoc();
839 SourceLocation SCEndLoc = SCLoc.getFileLocWithOffset(strlen(SpecName));
840
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000841 Diag(D, SCLoc, diag::err_friend_storage_spec)
John McCall07e91c02009-08-06 02:15:43 +0000842 << SpecName
Douglas Gregora771f462010-03-31 17:46:05 +0000843 << FixItHint::CreateRemoval(SourceRange(SCLoc, SCEndLoc));
John McCall07e91c02009-08-06 02:15:43 +0000844
845 ClearStorageClassSpecs();
846 }
847
John McCalla3707cc2010-08-26 17:22:34 +0000848 assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType));
849
Chris Lattner8e90ef62006-08-05 03:30:45 +0000850 // Okay, now we can infer the real type.
Mike Stump11289f42009-09-09 15:08:12 +0000851
Chris Lattner0e894622006-08-13 19:58:17 +0000852 // TODO: return "auto function" and other bad things based on the real type.
Mike Stump11289f42009-09-09 15:08:12 +0000853
Chris Lattner1ae23292006-08-04 06:42:31 +0000854 // 'data definition has no type or storage class'?
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000855}
Daniel Dunbarc74b5cc2008-08-11 03:45:03 +0000856
Sebastian Redla2b5e312008-12-28 15:28:59 +0000857bool DeclSpec::isMissingDeclaratorOk() {
858 TST tst = getTypeSpecType();
John McCallba7bf592010-08-24 05:47:05 +0000859 return isDeclRep(tst) && getRepAsDecl() != 0 &&
860 StorageClassSpec != DeclSpec::SCS_typedef;
Sebastian Redla2b5e312008-12-28 15:28:59 +0000861}
Douglas Gregor7861a802009-11-03 01:35:08 +0000862
863void UnqualifiedId::clear() {
Douglas Gregor7861a802009-11-03 01:35:08 +0000864 Kind = IK_Identifier;
865 Identifier = 0;
866 StartLocation = SourceLocation();
867 EndLocation = SourceLocation();
868}
869
870void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc,
871 OverloadedOperatorKind Op,
872 SourceLocation SymbolLocations[3]) {
873 Kind = IK_OperatorFunctionId;
874 StartLocation = OperatorLoc;
875 EndLocation = OperatorLoc;
876 OperatorFunctionId.Operator = Op;
877 for (unsigned I = 0; I != 3; ++I) {
878 OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding();
879
880 if (SymbolLocations[I].isValid())
881 EndLocation = SymbolLocations[I];
882 }
883}
Anders Carlsson56104902011-01-17 03:05:47 +0000884
Anders Carlsson4b63d0e2011-01-22 16:56:46 +0000885bool VirtSpecifiers::SetSpecifier(Specifier VS, SourceLocation Loc,
Anders Carlssonf2ca3892011-01-22 15:58:16 +0000886 const char *&PrevSpec) {
Douglas Gregorf2f08062011-03-08 17:10:18 +0000887 LastLocation = Loc;
888
Anders Carlsson56104902011-01-17 03:05:47 +0000889 if (Specifiers & VS) {
890 PrevSpec = getSpecifierName(VS);
891 return true;
892 }
893
894 Specifiers |= VS;
895
896 switch (VS) {
897 default: assert(0 && "Unknown specifier!");
898 case VS_Override: VS_overrideLoc = Loc; break;
899 case VS_Final: VS_finalLoc = Loc; break;
Anders Carlsson56104902011-01-17 03:05:47 +0000900 }
Anders Carlssonf2ca3892011-01-22 15:58:16 +0000901
Anders Carlsson56104902011-01-17 03:05:47 +0000902 return false;
903}
904
Anders Carlsson4b63d0e2011-01-22 16:56:46 +0000905const char *VirtSpecifiers::getSpecifierName(Specifier VS) {
Anders Carlssona6d35012011-01-22 15:11:37 +0000906 switch (VS) {
907 default: assert(0 && "Unknown specifier");
908 case VS_Override: return "override";
909 case VS_Final: return "final";
Anders Carlssona6d35012011-01-22 15:11:37 +0000910 }
911}