blob: 3564f2657b02021883c499d12ba6d9ecfb4f9470 [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"
Peter Collingbourneb8b0e752011-10-06 03:01:00 +000018#include "clang/Sema/Sema.h"
Douglas Gregorc34348a2011-02-24 17:54:50 +000019#include "clang/AST/ASTContext.h"
Douglas Gregor555f57e2011-06-25 00:56:27 +000020#include "clang/AST/Expr.h"
Douglas Gregor2e4c34a2011-02-24 00:17:56 +000021#include "clang/AST/NestedNameSpecifier.h"
22#include "clang/AST/TypeLoc.h"
Douglas Gregor9b3064b2009-04-01 22:41:11 +000023#include "clang/Lex/Preprocessor.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000024#include "clang/Basic/LangOptions.h"
Chris Lattner5af2f352009-01-20 19:11:22 +000025#include "llvm/ADT/STLExtras.h"
John McCall32d335e2009-08-03 18:47:27 +000026#include "llvm/Support/ErrorHandling.h"
Douglas Gregore4e5b052009-03-19 00:18:19 +000027#include <cstring>
Reid Spencer5f016e22007-07-11 17:01:13 +000028using namespace clang;
29
Chris Lattner254be6a2008-11-22 08:32:36 +000030
David Blaikied6471f72011-09-25 23:23:43 +000031static DiagnosticBuilder Diag(DiagnosticsEngine &D, SourceLocation Loc,
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000032 unsigned DiagID) {
33 return D.Report(Loc, DiagID);
Chris Lattner254be6a2008-11-22 08:32:36 +000034}
35
Douglas Gregor314b97f2009-11-10 19:49:08 +000036
37void UnqualifiedId::setTemplateId(TemplateIdAnnotation *TemplateId) {
38 assert(TemplateId && "NULL template-id annotation?");
39 Kind = IK_TemplateId;
40 this->TemplateId = TemplateId;
41 StartLocation = TemplateId->TemplateNameLoc;
42 EndLocation = TemplateId->RAngleLoc;
43}
44
Douglas Gregor0efc2c12010-01-13 17:31:36 +000045void UnqualifiedId::setConstructorTemplateId(TemplateIdAnnotation *TemplateId) {
46 assert(TemplateId && "NULL template-id annotation?");
47 Kind = IK_ConstructorTemplateId;
48 this->TemplateId = TemplateId;
49 StartLocation = TemplateId->TemplateNameLoc;
50 EndLocation = TemplateId->RAngleLoc;
51}
52
Douglas Gregor2e4c34a2011-02-24 00:17:56 +000053void CXXScopeSpec::Extend(ASTContext &Context, SourceLocation TemplateKWLoc,
54 TypeLoc TL, SourceLocation ColonColonLoc) {
Douglas Gregor5f791bb2011-02-28 23:58:31 +000055 Builder.Extend(Context, TemplateKWLoc, TL, ColonColonLoc);
Douglas Gregor2e4c34a2011-02-24 00:17:56 +000056 if (Range.getBegin().isInvalid())
57 Range.setBegin(TL.getBeginLoc());
58 Range.setEnd(ColonColonLoc);
Douglas Gregorc34348a2011-02-24 17:54:50 +000059
Douglas Gregor5f791bb2011-02-28 23:58:31 +000060 assert(Range == Builder.getSourceRange() &&
Douglas Gregorc34348a2011-02-24 17:54:50 +000061 "NestedNameSpecifierLoc range computation incorrect");
Douglas Gregor2e4c34a2011-02-24 00:17:56 +000062}
63
64void CXXScopeSpec::Extend(ASTContext &Context, IdentifierInfo *Identifier,
65 SourceLocation IdentifierLoc,
66 SourceLocation ColonColonLoc) {
Douglas Gregor5f791bb2011-02-28 23:58:31 +000067 Builder.Extend(Context, Identifier, IdentifierLoc, ColonColonLoc);
68
Douglas Gregor2e4c34a2011-02-24 00:17:56 +000069 if (Range.getBegin().isInvalid())
70 Range.setBegin(IdentifierLoc);
71 Range.setEnd(ColonColonLoc);
Douglas Gregorc34348a2011-02-24 17:54:50 +000072
Douglas Gregor5f791bb2011-02-28 23:58:31 +000073 assert(Range == Builder.getSourceRange() &&
Douglas Gregorc34348a2011-02-24 17:54:50 +000074 "NestedNameSpecifierLoc range computation incorrect");
Douglas Gregor2e4c34a2011-02-24 00:17:56 +000075}
76
77void CXXScopeSpec::Extend(ASTContext &Context, NamespaceDecl *Namespace,
78 SourceLocation NamespaceLoc,
79 SourceLocation ColonColonLoc) {
Douglas Gregor5f791bb2011-02-28 23:58:31 +000080 Builder.Extend(Context, Namespace, NamespaceLoc, ColonColonLoc);
81
Douglas Gregor2e4c34a2011-02-24 00:17:56 +000082 if (Range.getBegin().isInvalid())
83 Range.setBegin(NamespaceLoc);
84 Range.setEnd(ColonColonLoc);
Douglas Gregorc34348a2011-02-24 17:54:50 +000085
Douglas Gregor5f791bb2011-02-28 23:58:31 +000086 assert(Range == Builder.getSourceRange() &&
Douglas Gregorc34348a2011-02-24 17:54:50 +000087 "NestedNameSpecifierLoc range computation incorrect");
Douglas Gregor2e4c34a2011-02-24 00:17:56 +000088}
89
Douglas Gregor14aba762011-02-24 02:36:08 +000090void CXXScopeSpec::Extend(ASTContext &Context, NamespaceAliasDecl *Alias,
91 SourceLocation AliasLoc,
92 SourceLocation ColonColonLoc) {
Douglas Gregor5f791bb2011-02-28 23:58:31 +000093 Builder.Extend(Context, Alias, AliasLoc, ColonColonLoc);
94
Douglas Gregor14aba762011-02-24 02:36:08 +000095 if (Range.getBegin().isInvalid())
96 Range.setBegin(AliasLoc);
97 Range.setEnd(ColonColonLoc);
Douglas Gregorc34348a2011-02-24 17:54:50 +000098
Douglas Gregor5f791bb2011-02-28 23:58:31 +000099 assert(Range == Builder.getSourceRange() &&
Douglas Gregorc34348a2011-02-24 17:54:50 +0000100 "NestedNameSpecifierLoc range computation incorrect");
Douglas Gregor14aba762011-02-24 02:36:08 +0000101}
102
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000103void CXXScopeSpec::MakeGlobal(ASTContext &Context,
104 SourceLocation ColonColonLoc) {
Douglas Gregor5f791bb2011-02-28 23:58:31 +0000105 Builder.MakeGlobal(Context, ColonColonLoc);
106
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000107 Range = SourceRange(ColonColonLoc);
Douglas Gregorc34348a2011-02-24 17:54:50 +0000108
Douglas Gregor5f791bb2011-02-28 23:58:31 +0000109 assert(Range == Builder.getSourceRange() &&
Douglas Gregorc34348a2011-02-24 17:54:50 +0000110 "NestedNameSpecifierLoc range computation incorrect");
111}
112
113void CXXScopeSpec::MakeTrivial(ASTContext &Context,
114 NestedNameSpecifier *Qualifier, SourceRange R) {
Douglas Gregor5f791bb2011-02-28 23:58:31 +0000115 Builder.MakeTrivial(Context, Qualifier, R);
Douglas Gregorc34348a2011-02-24 17:54:50 +0000116 Range = R;
Douglas Gregorc34348a2011-02-24 17:54:50 +0000117}
118
119void CXXScopeSpec::Adopt(NestedNameSpecifierLoc Other) {
120 if (!Other) {
121 Range = SourceRange();
Douglas Gregor5f791bb2011-02-28 23:58:31 +0000122 Builder.Clear();
Douglas Gregorc34348a2011-02-24 17:54:50 +0000123 return;
124 }
Douglas Gregor5f791bb2011-02-28 23:58:31 +0000125
Douglas Gregorc34348a2011-02-24 17:54:50 +0000126 Range = Other.getSourceRange();
Douglas Gregor5f791bb2011-02-28 23:58:31 +0000127 Builder.Adopt(Other);
Douglas Gregorc34348a2011-02-24 17:54:50 +0000128}
129
John McCall9dc71d22011-07-06 06:57:57 +0000130SourceLocation CXXScopeSpec::getLastQualifierNameLoc() const {
131 if (!Builder.getRepresentation())
132 return SourceLocation();
133 return Builder.getTemporary().getLocalBeginLoc();
134}
135
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000136NestedNameSpecifierLoc
137CXXScopeSpec::getWithLocInContext(ASTContext &Context) const {
Douglas Gregorb46ae392011-03-03 21:48:55 +0000138 if (!Builder.getRepresentation())
Douglas Gregorc34348a2011-02-24 17:54:50 +0000139 return NestedNameSpecifierLoc();
140
Douglas Gregor5f791bb2011-02-28 23:58:31 +0000141 return Builder.getWithLocInContext(Context);
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000142}
143
Chris Lattner5af2f352009-01-20 19:11:22 +0000144/// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function.
145/// "TheDeclarator" is the declarator that this will be added to.
John McCall0b7e6782011-03-24 11:26:52 +0000146DeclaratorChunk DeclaratorChunk::getFunction(bool hasProto, bool isVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +0000147 SourceLocation EllipsisLoc,
Chris Lattner5af2f352009-01-20 19:11:22 +0000148 ParamInfo *ArgInfo,
149 unsigned NumArgs,
150 unsigned TypeQuals,
Douglas Gregor83f51722011-01-26 03:43:54 +0000151 bool RefQualifierIsLvalueRef,
152 SourceLocation RefQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +0000153 SourceLocation MutableLoc,
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000154 ExceptionSpecificationType
155 ESpecType,
156 SourceLocation ESpecLoc,
John McCallb3d87482010-08-24 05:47:05 +0000157 ParsedType *Exceptions,
Sebastian Redlef65f062009-05-29 18:02:33 +0000158 SourceRange *ExceptionRanges,
Sebastian Redl7dc81342009-04-29 17:30:04 +0000159 unsigned NumExceptions,
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000160 Expr *NoexceptExpr,
Abramo Bagnara796aa442011-03-12 11:17:06 +0000161 SourceLocation LocalRangeBegin,
162 SourceLocation LocalRangeEnd,
Douglas Gregordab60ad2010-10-01 18:44:50 +0000163 Declarator &TheDeclarator,
164 ParsedType TrailingReturnType) {
Chris Lattner5af2f352009-01-20 19:11:22 +0000165 DeclaratorChunk I;
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000166 I.Kind = Function;
Abramo Bagnara796aa442011-03-12 11:17:06 +0000167 I.Loc = LocalRangeBegin;
168 I.EndLoc = LocalRangeEnd;
John McCall0b7e6782011-03-24 11:26:52 +0000169 I.Fun.AttrList = 0;
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000170 I.Fun.hasPrototype = hasProto;
171 I.Fun.isVariadic = isVariadic;
172 I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding();
173 I.Fun.DeleteArgInfo = false;
174 I.Fun.TypeQuals = TypeQuals;
175 I.Fun.NumArgs = NumArgs;
176 I.Fun.ArgInfo = 0;
Douglas Gregor83f51722011-01-26 03:43:54 +0000177 I.Fun.RefQualifierIsLValueRef = RefQualifierIsLvalueRef;
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000178 I.Fun.RefQualifierLoc = RefQualifierLoc.getRawEncoding();
Douglas Gregor90ebed02011-07-13 21:47:47 +0000179 I.Fun.MutableLoc = MutableLoc.getRawEncoding();
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000180 I.Fun.ExceptionSpecType = ESpecType;
181 I.Fun.ExceptionSpecLoc = ESpecLoc.getRawEncoding();
182 I.Fun.NumExceptions = 0;
183 I.Fun.Exceptions = 0;
184 I.Fun.NoexceptExpr = 0;
Douglas Gregordab60ad2010-10-01 18:44:50 +0000185 I.Fun.TrailingReturnType = TrailingReturnType.getAsOpaquePtr();
Sebastian Redl7dc81342009-04-29 17:30:04 +0000186
Chris Lattner5af2f352009-01-20 19:11:22 +0000187 // new[] an argument array if needed.
188 if (NumArgs) {
189 // If the 'InlineParams' in Declarator is unused and big enough, put our
190 // parameter list there (in an effort to avoid new/delete traffic). If it
191 // is already used (consider a function returning a function pointer) or too
192 // small (function taking too many arguments), go to the heap.
Mike Stump1eb44332009-09-09 15:08:12 +0000193 if (!TheDeclarator.InlineParamsUsed &&
Chris Lattner5af2f352009-01-20 19:11:22 +0000194 NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
195 I.Fun.ArgInfo = TheDeclarator.InlineParams;
196 I.Fun.DeleteArgInfo = false;
197 TheDeclarator.InlineParamsUsed = true;
198 } else {
199 I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs];
200 I.Fun.DeleteArgInfo = true;
201 }
202 memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs);
203 }
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000204
205 // Check what exception specification information we should actually store.
206 switch (ESpecType) {
207 default: break; // By default, save nothing.
208 case EST_Dynamic:
209 // new[] an exception array if needed
210 if (NumExceptions) {
211 I.Fun.NumExceptions = NumExceptions;
212 I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions];
213 for (unsigned i = 0; i != NumExceptions; ++i) {
214 I.Fun.Exceptions[i].Ty = Exceptions[i];
215 I.Fun.Exceptions[i].Range = ExceptionRanges[i];
216 }
Sebastian Redlef65f062009-05-29 18:02:33 +0000217 }
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000218 break;
219
220 case EST_ComputedNoexcept:
221 I.Fun.NoexceptExpr = NoexceptExpr;
222 break;
Sebastian Redl7dc81342009-04-29 17:30:04 +0000223 }
Chris Lattner5af2f352009-01-20 19:11:22 +0000224 return I;
225}
Chris Lattner254be6a2008-11-22 08:32:36 +0000226
Douglas Gregor555f57e2011-06-25 00:56:27 +0000227bool Declarator::isDeclarationOfFunction() const {
Richard Smith1ab0d902011-06-25 02:28:38 +0000228 for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) {
229 switch (DeclTypeInfo[i].Kind) {
230 case DeclaratorChunk::Function:
231 return true;
232 case DeclaratorChunk::Paren:
233 continue;
234 case DeclaratorChunk::Pointer:
235 case DeclaratorChunk::Reference:
236 case DeclaratorChunk::Array:
237 case DeclaratorChunk::BlockPointer:
238 case DeclaratorChunk::MemberPointer:
239 return false;
240 }
241 llvm_unreachable("Invalid type chunk");
242 return false;
243 }
Douglas Gregor555f57e2011-06-25 00:56:27 +0000244
245 switch (DS.getTypeSpecType()) {
Eli Friedmanb001de72011-10-06 23:00:33 +0000246 case TST_atomic:
Douglas Gregor555f57e2011-06-25 00:56:27 +0000247 case TST_auto:
248 case TST_bool:
249 case TST_char:
250 case TST_char16:
251 case TST_char32:
252 case TST_class:
253 case TST_decimal128:
254 case TST_decimal32:
255 case TST_decimal64:
256 case TST_double:
257 case TST_enum:
258 case TST_error:
259 case TST_float:
260 case TST_int:
261 case TST_struct:
262 case TST_union:
263 case TST_unknown_anytype:
264 case TST_unspecified:
265 case TST_void:
266 case TST_wchar:
267 return false;
268
269 case TST_decltype:
270 case TST_typeofExpr:
271 if (Expr *E = DS.getRepAsExpr())
272 return E->getType()->isFunctionType();
273 return false;
274
275 case TST_underlyingType:
276 case TST_typename:
277 case TST_typeofType: {
278 QualType QT = DS.getRepAsType().get();
279 if (QT.isNull())
280 return false;
281
282 if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT))
283 QT = LIT->getType();
284
285 if (QT.isNull())
286 return false;
287
288 return QT->isFunctionType();
289 }
290 }
291
292 return false;
293}
294
Reid Spencer5f016e22007-07-11 17:01:13 +0000295/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
Chris Lattner2a327d12009-02-27 18:35:46 +0000296/// declaration specifier includes.
Reid Spencer5f016e22007-07-11 17:01:13 +0000297///
298unsigned DeclSpec::getParsedSpecifiers() const {
299 unsigned Res = 0;
300 if (StorageClassSpec != SCS_unspecified ||
301 SCS_thread_specified)
302 Res |= PQ_StorageClassSpecifier;
Mike Stumpd4204332008-06-19 19:52:46 +0000303
Reid Spencer5f016e22007-07-11 17:01:13 +0000304 if (TypeQualifiers != TQ_unspecified)
305 Res |= PQ_TypeQualifier;
Mike Stump1eb44332009-09-09 15:08:12 +0000306
Reid Spencer5f016e22007-07-11 17:01:13 +0000307 if (hasTypeSpecifier())
308 Res |= PQ_TypeSpecifier;
Mike Stump1eb44332009-09-09 15:08:12 +0000309
Douglas Gregorb48fe382008-10-31 09:07:45 +0000310 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
Reid Spencer5f016e22007-07-11 17:01:13 +0000311 Res |= PQ_FunctionSpecifier;
312 return Res;
313}
314
John McCallfec54012009-08-03 20:12:06 +0000315template <class T> static bool BadSpecifier(T TNew, T TPrev,
316 const char *&PrevSpec,
317 unsigned &DiagID) {
John McCall32d335e2009-08-03 18:47:27 +0000318 PrevSpec = DeclSpec::getSpecifierName(TPrev);
John McCallfec54012009-08-03 20:12:06 +0000319 DiagID = (TNew == TPrev ? diag::ext_duplicate_declspec
320 : diag::err_invalid_decl_spec_combination);
John McCall32d335e2009-08-03 18:47:27 +0000321 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000322}
John McCall32d335e2009-08-03 18:47:27 +0000323
Reid Spencer5f016e22007-07-11 17:01:13 +0000324const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
325 switch (S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000326 case DeclSpec::SCS_unspecified: return "unspecified";
327 case DeclSpec::SCS_typedef: return "typedef";
328 case DeclSpec::SCS_extern: return "extern";
329 case DeclSpec::SCS_static: return "static";
330 case DeclSpec::SCS_auto: return "auto";
331 case DeclSpec::SCS_register: return "register";
Eli Friedman63054b32009-04-19 20:27:55 +0000332 case DeclSpec::SCS_private_extern: return "__private_extern__";
Sebastian Redl669d5d72008-11-14 23:42:31 +0000333 case DeclSpec::SCS_mutable: return "mutable";
Reid Spencer5f016e22007-07-11 17:01:13 +0000334 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000335 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000336}
337
John McCall32d335e2009-08-03 18:47:27 +0000338const char *DeclSpec::getSpecifierName(TSW W) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000339 switch (W) {
John McCall32d335e2009-08-03 18:47:27 +0000340 case TSW_unspecified: return "unspecified";
341 case TSW_short: return "short";
342 case TSW_long: return "long";
343 case TSW_longlong: return "long long";
Reid Spencer5f016e22007-07-11 17:01:13 +0000344 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000345 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000346}
347
John McCall32d335e2009-08-03 18:47:27 +0000348const char *DeclSpec::getSpecifierName(TSC C) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000349 switch (C) {
John McCall32d335e2009-08-03 18:47:27 +0000350 case TSC_unspecified: return "unspecified";
351 case TSC_imaginary: return "imaginary";
352 case TSC_complex: return "complex";
Reid Spencer5f016e22007-07-11 17:01:13 +0000353 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000354 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000355}
356
357
John McCall32d335e2009-08-03 18:47:27 +0000358const char *DeclSpec::getSpecifierName(TSS S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000359 switch (S) {
John McCall32d335e2009-08-03 18:47:27 +0000360 case TSS_unspecified: return "unspecified";
361 case TSS_signed: return "signed";
362 case TSS_unsigned: return "unsigned";
Reid Spencer5f016e22007-07-11 17:01:13 +0000363 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000364 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000365}
366
367const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
368 switch (T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000369 case DeclSpec::TST_unspecified: return "unspecified";
370 case DeclSpec::TST_void: return "void";
371 case DeclSpec::TST_char: return "char";
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000372 case DeclSpec::TST_wchar: return "wchar_t";
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000373 case DeclSpec::TST_char16: return "char16_t";
374 case DeclSpec::TST_char32: return "char32_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000375 case DeclSpec::TST_int: return "int";
376 case DeclSpec::TST_float: return "float";
377 case DeclSpec::TST_double: return "double";
378 case DeclSpec::TST_bool: return "_Bool";
379 case DeclSpec::TST_decimal32: return "_Decimal32";
380 case DeclSpec::TST_decimal64: return "_Decimal64";
381 case DeclSpec::TST_decimal128: return "_Decimal128";
382 case DeclSpec::TST_enum: return "enum";
Chris Lattner99dc9142008-04-13 18:59:07 +0000383 case DeclSpec::TST_class: return "class";
Reid Spencer5f016e22007-07-11 17:01:13 +0000384 case DeclSpec::TST_union: return "union";
385 case DeclSpec::TST_struct: return "struct";
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000386 case DeclSpec::TST_typename: return "type-name";
Steve Naroffd1861fd2007-07-31 12:34:36 +0000387 case DeclSpec::TST_typeofType:
388 case DeclSpec::TST_typeofExpr: return "typeof";
John McCall32d335e2009-08-03 18:47:27 +0000389 case DeclSpec::TST_auto: return "auto";
390 case DeclSpec::TST_decltype: return "(decltype)";
Sean Huntca63c202011-05-24 22:41:36 +0000391 case DeclSpec::TST_underlyingType: return "__underlying_type";
John McCalla5fc4722011-04-09 22:50:59 +0000392 case DeclSpec::TST_unknown_anytype: return "__unknown_anytype";
Eli Friedmanb001de72011-10-06 23:00:33 +0000393 case DeclSpec::TST_atomic: return "_Atomic";
John McCall32d335e2009-08-03 18:47:27 +0000394 case DeclSpec::TST_error: return "(error)";
Reid Spencer5f016e22007-07-11 17:01:13 +0000395 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000396 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000397}
398
John McCall32d335e2009-08-03 18:47:27 +0000399const char *DeclSpec::getSpecifierName(TQ T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000400 switch (T) {
John McCall32d335e2009-08-03 18:47:27 +0000401 case DeclSpec::TQ_unspecified: return "unspecified";
402 case DeclSpec::TQ_const: return "const";
403 case DeclSpec::TQ_restrict: return "restrict";
404 case DeclSpec::TQ_volatile: return "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +0000405 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000406 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000407}
408
Peter Collingbourneb8b0e752011-10-06 03:01:00 +0000409bool DeclSpec::SetStorageClassSpec(Sema &S, SCS SC, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000410 const char *&PrevSpec,
Peter Collingbourneb8b0e752011-10-06 03:01:00 +0000411 unsigned &DiagID) {
412 // OpenCL 1.1 6.8g: "The extern, static, auto and register storage-class
413 // specifiers are not supported."
Peter Collingbournee2f82f72011-02-11 19:59:54 +0000414 // It seems sensible to prohibit private_extern too
Peter Collingbourneb8b0e752011-10-06 03:01:00 +0000415 // The cl_clang_storage_class_specifiers extension enables support for
416 // these storage-class specifiers.
417 if (S.getLangOptions().OpenCL &&
418 !S.getOpenCLOptions().cl_clang_storage_class_specifiers) {
419 switch (SC) {
Peter Collingbournee2f82f72011-02-11 19:59:54 +0000420 case SCS_extern:
421 case SCS_private_extern:
422 case SCS_auto:
423 case SCS_register:
424 case SCS_static:
425 DiagID = diag::err_not_opencl_storage_class_specifier;
Peter Collingbourneb8b0e752011-10-06 03:01:00 +0000426 PrevSpec = getSpecifierName(SC);
Peter Collingbournee2f82f72011-02-11 19:59:54 +0000427 return true;
428 default:
429 break;
430 }
431 }
432
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000433 if (StorageClassSpec != SCS_unspecified) {
Richard Smith8f4fb192011-09-04 19:54:14 +0000434 // Maybe this is an attempt to use C++0x 'auto' outside of C++0x mode.
435 bool isInvalid = true;
Peter Collingbourneb8b0e752011-10-06 03:01:00 +0000436 if (TypeSpecType == TST_unspecified && S.getLangOptions().CPlusPlus) {
437 if (SC == SCS_auto)
Richard Smith8f4fb192011-09-04 19:54:14 +0000438 return SetTypeSpecType(TST_auto, Loc, PrevSpec, DiagID);
439 if (StorageClassSpec == SCS_auto) {
440 isInvalid = SetTypeSpecType(TST_auto, StorageClassSpecLoc,
441 PrevSpec, DiagID);
442 assert(!isInvalid && "auto SCS -> TST recovery failed");
443 }
444 }
445
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000446 // Changing storage class is allowed only if the previous one
447 // was the 'extern' that is part of a linkage specification and
448 // the new storage class is 'typedef'.
Richard Smith8f4fb192011-09-04 19:54:14 +0000449 if (isInvalid &&
450 !(SCS_extern_in_linkage_spec &&
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000451 StorageClassSpec == SCS_extern &&
Peter Collingbourneb8b0e752011-10-06 03:01:00 +0000452 SC == SCS_typedef))
453 return BadSpecifier(SC, (SCS)StorageClassSpec, PrevSpec, DiagID);
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000454 }
Peter Collingbourneb8b0e752011-10-06 03:01:00 +0000455 StorageClassSpec = SC;
Reid Spencer5f016e22007-07-11 17:01:13 +0000456 StorageClassSpecLoc = Loc;
Peter Collingbourneb8b0e752011-10-06 03:01:00 +0000457 assert((unsigned)SC == StorageClassSpec && "SCS constants overflow bitfield");
Reid Spencer5f016e22007-07-11 17:01:13 +0000458 return false;
459}
460
Mike Stump1eb44332009-09-09 15:08:12 +0000461bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000462 const char *&PrevSpec,
463 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000464 if (SCS_thread_specified) {
465 PrevSpec = "__thread";
John McCallfec54012009-08-03 20:12:06 +0000466 DiagID = diag::ext_duplicate_declspec;
Reid Spencer5f016e22007-07-11 17:01:13 +0000467 return true;
468 }
469 SCS_thread_specified = true;
470 SCS_threadLoc = Loc;
471 return false;
472}
473
Reid Spencer5f016e22007-07-11 17:01:13 +0000474/// These methods set the specified attribute of the DeclSpec, but return true
475/// and ignore the request if invalid (e.g. "extern" then "auto" is
476/// specified).
477bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000478 const char *&PrevSpec,
479 unsigned &DiagID) {
Abramo Bagnara2553eaf2011-03-06 22:21:56 +0000480 // Overwrite TSWLoc only if TypeSpecWidth was unspecified, so that
481 // for 'long long' we will keep the source location of the first 'long'.
482 if (TypeSpecWidth == TSW_unspecified)
483 TSWLoc = Loc;
484 // Allow turning long -> long long.
485 else if (W != TSW_longlong || TypeSpecWidth != TSW_long)
John McCallfec54012009-08-03 20:12:06 +0000486 return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000487 TypeSpecWidth = W;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000488 if (TypeAltiVecVector && !TypeAltiVecBool &&
489 ((TypeSpecWidth == TSW_long) || (TypeSpecWidth == TSW_longlong))) {
John Thompson82287d12010-02-05 00:12:22 +0000490 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
491 DiagID = diag::warn_vector_long_decl_spec_combination;
492 return true;
493 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000494 return false;
495}
496
Mike Stump1eb44332009-09-09 15:08:12 +0000497bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000498 const char *&PrevSpec,
499 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000500 if (TypeSpecComplex != TSC_unspecified)
John McCallfec54012009-08-03 20:12:06 +0000501 return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000502 TypeSpecComplex = C;
503 TSCLoc = Loc;
504 return false;
505}
506
Mike Stump1eb44332009-09-09 15:08:12 +0000507bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000508 const char *&PrevSpec,
509 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000510 if (TypeSpecSign != TSS_unspecified)
John McCallfec54012009-08-03 20:12:06 +0000511 return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000512 TypeSpecSign = S;
513 TSSLoc = Loc;
514 return false;
515}
516
517bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000518 const char *&PrevSpec,
519 unsigned &DiagID,
John McCallb3d87482010-08-24 05:47:05 +0000520 ParsedType Rep) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000521 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep);
522}
523
524bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
525 SourceLocation TagNameLoc,
526 const char *&PrevSpec,
527 unsigned &DiagID,
528 ParsedType Rep) {
John McCallb3d87482010-08-24 05:47:05 +0000529 assert(isTypeRep(T) && "T does not store a type");
530 assert(Rep && "no type provided!");
531 if (TypeSpecType != TST_unspecified) {
532 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
533 DiagID = diag::err_invalid_decl_spec_combination;
534 return true;
535 }
536 TypeSpecType = T;
537 TypeRep = Rep;
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000538 TSTLoc = TagKwLoc;
539 TSTNameLoc = TagNameLoc;
John McCallb3d87482010-08-24 05:47:05 +0000540 TypeSpecOwned = false;
541 return false;
542}
543
544bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
545 const char *&PrevSpec,
546 unsigned &DiagID,
547 Expr *Rep) {
548 assert(isExprRep(T) && "T does not store an expr");
549 assert(Rep && "no expression provided!");
550 if (TypeSpecType != TST_unspecified) {
551 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
552 DiagID = diag::err_invalid_decl_spec_combination;
553 return true;
554 }
555 TypeSpecType = T;
556 ExprRep = Rep;
557 TSTLoc = Loc;
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000558 TSTNameLoc = Loc;
John McCallb3d87482010-08-24 05:47:05 +0000559 TypeSpecOwned = false;
560 return false;
561}
562
563bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
564 const char *&PrevSpec,
565 unsigned &DiagID,
566 Decl *Rep, bool Owned) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000567 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep, Owned);
568}
569
570bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
571 SourceLocation TagNameLoc,
572 const char *&PrevSpec,
573 unsigned &DiagID,
574 Decl *Rep, bool Owned) {
John McCallb3d87482010-08-24 05:47:05 +0000575 assert(isDeclRep(T) && "T does not store a decl");
576 // Unlike the other cases, we don't assert that we actually get a decl.
577
578 if (TypeSpecType != TST_unspecified) {
579 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
580 DiagID = diag::err_invalid_decl_spec_combination;
581 return true;
582 }
583 TypeSpecType = T;
584 DeclRep = Rep;
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000585 TSTLoc = TagKwLoc;
586 TSTNameLoc = TagNameLoc;
John McCallb3d87482010-08-24 05:47:05 +0000587 TypeSpecOwned = Owned;
588 return false;
589}
590
591bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
592 const char *&PrevSpec,
593 unsigned &DiagID) {
594 assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) &&
595 "rep required for these type-spec kinds!");
John McCallfec54012009-08-03 20:12:06 +0000596 if (TypeSpecType != TST_unspecified) {
597 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
598 DiagID = diag::err_invalid_decl_spec_combination;
599 return true;
600 }
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000601 TSTLoc = Loc;
602 TSTNameLoc = Loc;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000603 if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) {
604 TypeAltiVecBool = true;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000605 return false;
606 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000607 TypeSpecType = T;
John McCallb3d87482010-08-24 05:47:05 +0000608 TypeSpecOwned = false;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000609 if (TypeAltiVecVector && !TypeAltiVecBool && (TypeSpecType == TST_double)) {
John Thompson82287d12010-02-05 00:12:22 +0000610 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
Chris Lattner788b0fd2010-06-23 06:00:24 +0000611 DiagID = diag::err_invalid_vector_decl_spec;
John Thompson82287d12010-02-05 00:12:22 +0000612 return true;
613 }
614 return false;
615}
616
617bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
618 const char *&PrevSpec, unsigned &DiagID) {
619 if (TypeSpecType != TST_unspecified) {
620 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
621 DiagID = diag::err_invalid_vector_decl_spec_combination;
622 return true;
623 }
624 TypeAltiVecVector = isAltiVecVector;
625 AltiVecLoc = Loc;
626 return false;
627}
628
629bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
630 const char *&PrevSpec, unsigned &DiagID) {
Chris Lattner788b0fd2010-06-23 06:00:24 +0000631 if (!TypeAltiVecVector || TypeAltiVecPixel ||
632 (TypeSpecType != TST_unspecified)) {
John Thompson82287d12010-02-05 00:12:22 +0000633 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
634 DiagID = diag::err_invalid_pixel_decl_spec_combination;
635 return true;
636 }
John Thompson82287d12010-02-05 00:12:22 +0000637 TypeAltiVecPixel = isAltiVecPixel;
638 TSTLoc = Loc;
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000639 TSTNameLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000640 return false;
641}
642
Douglas Gregorddc29e12009-02-06 22:42:48 +0000643bool DeclSpec::SetTypeSpecError() {
644 TypeSpecType = TST_error;
John McCall9e46b8c2010-08-26 17:22:34 +0000645 TypeSpecOwned = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000646 TSTLoc = SourceLocation();
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000647 TSTNameLoc = SourceLocation();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000648 return false;
649}
650
Reid Spencer5f016e22007-07-11 17:01:13 +0000651bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000652 unsigned &DiagID, const LangOptions &Lang) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000653 // Duplicates turn into warnings pre-C99.
654 if ((TypeQualifiers & T) && !Lang.C99)
John McCallfec54012009-08-03 20:12:06 +0000655 return BadSpecifier(T, T, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000656 TypeQualifiers |= T;
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Reid Spencer5f016e22007-07-11 17:01:13 +0000658 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000659 default: llvm_unreachable("Unknown type qualifier!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000660 case TQ_const: TQ_constLoc = Loc; break;
661 case TQ_restrict: TQ_restrictLoc = Loc; break;
662 case TQ_volatile: TQ_volatileLoc = Loc; break;
663 }
664 return false;
665}
666
John McCallfec54012009-08-03 20:12:06 +0000667bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
668 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000669 // 'inline inline' is ok.
670 FS_inline_specified = true;
671 FS_inlineLoc = Loc;
672 return false;
673}
674
John McCallfec54012009-08-03 20:12:06 +0000675bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec,
676 unsigned &DiagID) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000677 // 'virtual virtual' is ok.
678 FS_virtual_specified = true;
679 FS_virtualLoc = Loc;
680 return false;
681}
682
John McCallfec54012009-08-03 20:12:06 +0000683bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec,
684 unsigned &DiagID) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000685 // 'explicit explicit' is ok.
686 FS_explicit_specified = true;
687 FS_explicitLoc = Loc;
688 return false;
689}
690
John McCallfec54012009-08-03 20:12:06 +0000691bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
692 unsigned &DiagID) {
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000693 if (Friend_specified) {
694 PrevSpec = "friend";
John McCallfec54012009-08-03 20:12:06 +0000695 DiagID = diag::ext_duplicate_declspec;
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000696 return true;
697 }
John McCallfec54012009-08-03 20:12:06 +0000698
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000699 Friend_specified = true;
700 FriendLoc = Loc;
701 return false;
702}
Reid Spencer5f016e22007-07-11 17:01:13 +0000703
Douglas Gregor8d267c52011-09-09 02:06:17 +0000704bool DeclSpec::setModulePrivateSpec(SourceLocation Loc, const char *&PrevSpec,
705 unsigned &DiagID) {
706 if (isModulePrivateSpecified()) {
707 PrevSpec = "__module_private__";
708 DiagID = diag::ext_duplicate_declspec;
709 return true;
710 }
711
712 ModulePrivateLoc = Loc;
713 return false;
714}
715
Sebastian Redl2ac67232009-11-05 15:47:02 +0000716bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
717 unsigned &DiagID) {
718 // 'constexpr constexpr' is ok.
719 Constexpr_specified = true;
720 ConstexprLoc = Loc;
721 return false;
722}
723
John McCalld226f652010-08-21 09:40:31 +0000724void DeclSpec::setProtocolQualifiers(Decl * const *Protos,
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000725 unsigned NP,
726 SourceLocation *ProtoLocs,
727 SourceLocation LAngleLoc) {
728 if (NP == 0) return;
John McCalld226f652010-08-21 09:40:31 +0000729 ProtocolQualifiers = new Decl*[NP];
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000730 ProtocolLocs = new SourceLocation[NP];
John McCalld226f652010-08-21 09:40:31 +0000731 memcpy((void*)ProtocolQualifiers, Protos, sizeof(Decl*)*NP);
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000732 memcpy(ProtocolLocs, ProtoLocs, sizeof(SourceLocation)*NP);
733 NumProtocolQualifiers = NP;
734 ProtocolLAngleLoc = LAngleLoc;
735}
736
Douglas Gregorddf889a2010-01-18 18:04:31 +0000737void DeclSpec::SaveWrittenBuiltinSpecs() {
738 writtenBS.Sign = getTypeSpecSign();
739 writtenBS.Width = getTypeSpecWidth();
740 writtenBS.Type = getTypeSpecType();
741 // Search the list of attributes for the presence of a mode attribute.
742 writtenBS.ModeAttr = false;
John McCall7f040a92010-12-24 02:08:15 +0000743 AttributeList* attrs = getAttributes().getList();
Douglas Gregorddf889a2010-01-18 18:04:31 +0000744 while (attrs) {
745 if (attrs->getKind() == AttributeList::AT_mode) {
746 writtenBS.ModeAttr = true;
747 break;
748 }
749 attrs = attrs->getNext();
750 }
751}
752
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000753void DeclSpec::SaveStorageSpecifierAsWritten() {
754 if (SCS_extern_in_linkage_spec && StorageClassSpec == SCS_extern)
755 // If 'extern' is part of a linkage specification,
756 // then it is not a storage class "as written".
757 StorageClassSpecAsWritten = SCS_unspecified;
758 else
759 StorageClassSpecAsWritten = StorageClassSpec;
760}
761
Reid Spencer5f016e22007-07-11 17:01:13 +0000762/// Finish - This does final analysis of the declspec, rejecting things like
763/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
764/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
765/// DeclSpec is guaranteed self-consistent, even if an error occurred.
David Blaikied6471f72011-09-25 23:23:43 +0000766void DeclSpec::Finish(DiagnosticsEngine &D, Preprocessor &PP) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000767 // Before possibly changing their values, save specs as written.
768 SaveWrittenBuiltinSpecs();
Douglas Gregor16573fa2010-04-19 22:54:31 +0000769 SaveStorageSpecifierAsWritten();
Douglas Gregorddf889a2010-01-18 18:04:31 +0000770
Reid Spencer5f016e22007-07-11 17:01:13 +0000771 // Check the type specifier components first.
772
Chris Lattner788b0fd2010-06-23 06:00:24 +0000773 // Validate and finalize AltiVec vector declspec.
774 if (TypeAltiVecVector) {
775 if (TypeAltiVecBool) {
776 // Sign specifiers are not allowed with vector bool. (PIM 2.1)
777 if (TypeSpecSign != TSS_unspecified) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000778 Diag(D, TSSLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner788b0fd2010-06-23 06:00:24 +0000779 << getSpecifierName((TSS)TypeSpecSign);
780 }
781
782 // Only char/int are valid with vector bool. (PIM 2.1)
Duncan Sands2e964a922010-06-23 19:34:52 +0000783 if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) &&
784 (TypeSpecType != TST_int)) || TypeAltiVecPixel) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000785 Diag(D, TSTLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner788b0fd2010-06-23 06:00:24 +0000786 << (TypeAltiVecPixel ? "__pixel" :
787 getSpecifierName((TST)TypeSpecType));
788 }
789
790 // Only 'short' is valid with vector bool. (PIM 2.1)
791 if ((TypeSpecWidth != TSW_unspecified) && (TypeSpecWidth != TSW_short))
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000792 Diag(D, TSWLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner788b0fd2010-06-23 06:00:24 +0000793 << getSpecifierName((TSW)TypeSpecWidth);
794
795 // Elements of vector bool are interpreted as unsigned. (PIM 2.1)
796 if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) ||
797 (TypeSpecWidth != TSW_unspecified))
798 TypeSpecSign = TSS_unsigned;
799 }
800
801 if (TypeAltiVecPixel) {
802 //TODO: perform validation
803 TypeSpecType = TST_int;
804 TypeSpecSign = TSS_unsigned;
805 TypeSpecWidth = TSW_short;
John McCall9e46b8c2010-08-26 17:22:34 +0000806 TypeSpecOwned = false;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000807 }
808 }
809
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000810 // signed/unsigned are only valid with int/char/wchar_t.
Reid Spencer5f016e22007-07-11 17:01:13 +0000811 if (TypeSpecSign != TSS_unspecified) {
812 if (TypeSpecType == TST_unspecified)
813 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000814 else if (TypeSpecType != TST_int &&
815 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000816 Diag(D, TSSLoc, diag::err_invalid_sign_spec)
Chris Lattner254be6a2008-11-22 08:32:36 +0000817 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000818 // signed double -> double.
819 TypeSpecSign = TSS_unspecified;
820 }
821 }
822
823 // Validate the width of the type.
824 switch (TypeSpecWidth) {
825 case TSW_unspecified: break;
826 case TSW_short: // short int
827 case TSW_longlong: // long long int
828 if (TypeSpecType == TST_unspecified)
829 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
830 else if (TypeSpecType != TST_int) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000831 Diag(D, TSWLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000832 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
Chris Lattner254be6a2008-11-22 08:32:36 +0000833 : diag::err_invalid_longlong_spec)
834 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000835 TypeSpecType = TST_int;
John McCall9e46b8c2010-08-26 17:22:34 +0000836 TypeSpecOwned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000837 }
838 break;
839 case TSW_long: // long double, long int
840 if (TypeSpecType == TST_unspecified)
841 TypeSpecType = TST_int; // long -> long int.
842 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000843 Diag(D, TSWLoc, diag::err_invalid_long_spec)
Chris Lattner254be6a2008-11-22 08:32:36 +0000844 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000845 TypeSpecType = TST_int;
John McCall9e46b8c2010-08-26 17:22:34 +0000846 TypeSpecOwned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000847 }
848 break;
849 }
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Reid Spencer5f016e22007-07-11 17:01:13 +0000851 // TODO: if the implementation does not implement _Complex or _Imaginary,
852 // disallow their use. Need information about the backend.
853 if (TypeSpecComplex != TSC_unspecified) {
854 if (TypeSpecType == TST_unspecified) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000855 Diag(D, TSCLoc, diag::ext_plain_complex)
Douglas Gregor849b2432010-03-31 17:46:05 +0000856 << FixItHint::CreateInsertion(
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000857 PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
858 " double");
Reid Spencer5f016e22007-07-11 17:01:13 +0000859 TypeSpecType = TST_double; // _Complex -> _Complex double.
860 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
861 // Note that this intentionally doesn't include _Complex _Bool.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000862 Diag(D, TSTLoc, diag::ext_integer_complex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000863 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000864 Diag(D, TSCLoc, diag::err_invalid_complex_spec)
Chris Lattner254be6a2008-11-22 08:32:36 +0000865 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000866 TypeSpecComplex = TSC_unspecified;
867 }
868 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000869
Richard Smith8f4fb192011-09-04 19:54:14 +0000870 // If no type specifier was provided and we're parsing a language where
871 // the type specifier is not optional, but we got 'auto' as a storage
872 // class specifier, then assume this is an attempt to use C++0x's 'auto'
873 // type specifier.
874 // FIXME: Does Microsoft really support implicit int in C++?
Francois Pichet62ec1f22011-09-17 17:15:52 +0000875 if (PP.getLangOptions().CPlusPlus && !PP.getLangOptions().MicrosoftExt &&
Richard Smith8f4fb192011-09-04 19:54:14 +0000876 TypeSpecType == TST_unspecified && StorageClassSpec == SCS_auto) {
877 TypeSpecType = TST_auto;
878 StorageClassSpec = StorageClassSpecAsWritten = SCS_unspecified;
879 TSTLoc = TSTNameLoc = StorageClassSpecLoc;
880 StorageClassSpecLoc = SourceLocation();
881 }
882 // Diagnose if we've recovered from an ill-formed 'auto' storage class
883 // specifier in a pre-C++0x dialect of C++.
884 if (!PP.getLangOptions().CPlusPlus0x && TypeSpecType == TST_auto)
885 Diag(D, TSTLoc, diag::ext_auto_type_specifier);
886 if (PP.getLangOptions().CPlusPlus && !PP.getLangOptions().CPlusPlus0x &&
887 StorageClassSpec == SCS_auto)
888 Diag(D, StorageClassSpecLoc, diag::warn_auto_storage_class)
889 << FixItHint::CreateRemoval(StorageClassSpecLoc);
890
John McCall67d1a672009-08-06 02:15:43 +0000891 // C++ [class.friend]p6:
892 // No storage-class-specifier shall appear in the decl-specifier-seq
893 // of a friend declaration.
894 if (isFriendSpecified() && getStorageClassSpec()) {
895 DeclSpec::SCS SC = getStorageClassSpec();
896 const char *SpecName = getSpecifierName(SC);
897
898 SourceLocation SCLoc = getStorageClassSpecLoc();
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000899 SourceLocation SCEndLoc = SCLoc.getLocWithOffset(strlen(SpecName));
John McCall67d1a672009-08-06 02:15:43 +0000900
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000901 Diag(D, SCLoc, diag::err_friend_storage_spec)
John McCall67d1a672009-08-06 02:15:43 +0000902 << SpecName
Douglas Gregor849b2432010-03-31 17:46:05 +0000903 << FixItHint::CreateRemoval(SourceRange(SCLoc, SCEndLoc));
John McCall67d1a672009-08-06 02:15:43 +0000904
905 ClearStorageClassSpecs();
906 }
907
Douglas Gregor6274d302011-09-09 21:14:29 +0000908 assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType));
909
Reid Spencer5f016e22007-07-11 17:01:13 +0000910 // Okay, now we can infer the real type.
Mike Stump1eb44332009-09-09 15:08:12 +0000911
Reid Spencer5f016e22007-07-11 17:01:13 +0000912 // TODO: return "auto function" and other bad things based on the real type.
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Reid Spencer5f016e22007-07-11 17:01:13 +0000914 // 'data definition has no type or storage class'?
915}
Daniel Dunbare4858a62008-08-11 03:45:03 +0000916
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000917bool DeclSpec::isMissingDeclaratorOk() {
918 TST tst = getTypeSpecType();
John McCallb3d87482010-08-24 05:47:05 +0000919 return isDeclRep(tst) && getRepAsDecl() != 0 &&
920 StorageClassSpec != DeclSpec::SCS_typedef;
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000921}
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000922
923void UnqualifiedId::clear() {
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000924 Kind = IK_Identifier;
925 Identifier = 0;
926 StartLocation = SourceLocation();
927 EndLocation = SourceLocation();
928}
929
930void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc,
931 OverloadedOperatorKind Op,
932 SourceLocation SymbolLocations[3]) {
933 Kind = IK_OperatorFunctionId;
934 StartLocation = OperatorLoc;
935 EndLocation = OperatorLoc;
936 OperatorFunctionId.Operator = Op;
937 for (unsigned I = 0; I != 3; ++I) {
938 OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding();
939
940 if (SymbolLocations[I].isValid())
941 EndLocation = SymbolLocations[I];
942 }
943}
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000944
Anders Carlssoncc54d592011-01-22 16:56:46 +0000945bool VirtSpecifiers::SetSpecifier(Specifier VS, SourceLocation Loc,
Anders Carlsson46127a92011-01-22 15:58:16 +0000946 const char *&PrevSpec) {
Douglas Gregorf5251602011-03-08 17:10:18 +0000947 LastLocation = Loc;
948
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000949 if (Specifiers & VS) {
950 PrevSpec = getSpecifierName(VS);
951 return true;
952 }
953
954 Specifiers |= VS;
955
956 switch (VS) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000957 default: llvm_unreachable("Unknown specifier!");
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000958 case VS_Override: VS_overrideLoc = Loc; break;
959 case VS_Final: VS_finalLoc = Loc; break;
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000960 }
Anders Carlsson46127a92011-01-22 15:58:16 +0000961
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000962 return false;
963}
964
Anders Carlssoncc54d592011-01-22 16:56:46 +0000965const char *VirtSpecifiers::getSpecifierName(Specifier VS) {
Anders Carlssonc46bb7d2011-01-22 15:11:37 +0000966 switch (VS) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000967 default: llvm_unreachable("Unknown specifier");
Anders Carlssonc46bb7d2011-01-22 15:11:37 +0000968 case VS_Override: return "override";
969 case VS_Final: return "final";
Anders Carlssonc46bb7d2011-01-22 15:11:37 +0000970 }
971}