blob: d7732c78ddbf087df1156d8f05a52a3149ff8607 [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()) {
246 case TST_auto:
247 case TST_bool:
248 case TST_char:
249 case TST_char16:
250 case TST_char32:
251 case TST_class:
252 case TST_decimal128:
253 case TST_decimal32:
254 case TST_decimal64:
255 case TST_double:
256 case TST_enum:
257 case TST_error:
258 case TST_float:
259 case TST_int:
260 case TST_struct:
261 case TST_union:
262 case TST_unknown_anytype:
263 case TST_unspecified:
264 case TST_void:
265 case TST_wchar:
266 return false;
267
268 case TST_decltype:
269 case TST_typeofExpr:
270 if (Expr *E = DS.getRepAsExpr())
271 return E->getType()->isFunctionType();
272 return false;
273
274 case TST_underlyingType:
275 case TST_typename:
276 case TST_typeofType: {
277 QualType QT = DS.getRepAsType().get();
278 if (QT.isNull())
279 return false;
280
281 if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT))
282 QT = LIT->getType();
283
284 if (QT.isNull())
285 return false;
286
287 return QT->isFunctionType();
288 }
289 }
290
291 return false;
292}
293
Reid Spencer5f016e22007-07-11 17:01:13 +0000294/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
Chris Lattner2a327d12009-02-27 18:35:46 +0000295/// declaration specifier includes.
Reid Spencer5f016e22007-07-11 17:01:13 +0000296///
297unsigned DeclSpec::getParsedSpecifiers() const {
298 unsigned Res = 0;
299 if (StorageClassSpec != SCS_unspecified ||
300 SCS_thread_specified)
301 Res |= PQ_StorageClassSpecifier;
Mike Stumpd4204332008-06-19 19:52:46 +0000302
Reid Spencer5f016e22007-07-11 17:01:13 +0000303 if (TypeQualifiers != TQ_unspecified)
304 Res |= PQ_TypeQualifier;
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Reid Spencer5f016e22007-07-11 17:01:13 +0000306 if (hasTypeSpecifier())
307 Res |= PQ_TypeSpecifier;
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Douglas Gregorb48fe382008-10-31 09:07:45 +0000309 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
Reid Spencer5f016e22007-07-11 17:01:13 +0000310 Res |= PQ_FunctionSpecifier;
311 return Res;
312}
313
John McCallfec54012009-08-03 20:12:06 +0000314template <class T> static bool BadSpecifier(T TNew, T TPrev,
315 const char *&PrevSpec,
316 unsigned &DiagID) {
John McCall32d335e2009-08-03 18:47:27 +0000317 PrevSpec = DeclSpec::getSpecifierName(TPrev);
John McCallfec54012009-08-03 20:12:06 +0000318 DiagID = (TNew == TPrev ? diag::ext_duplicate_declspec
319 : diag::err_invalid_decl_spec_combination);
John McCall32d335e2009-08-03 18:47:27 +0000320 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000321}
John McCall32d335e2009-08-03 18:47:27 +0000322
Reid Spencer5f016e22007-07-11 17:01:13 +0000323const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
324 switch (S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000325 case DeclSpec::SCS_unspecified: return "unspecified";
326 case DeclSpec::SCS_typedef: return "typedef";
327 case DeclSpec::SCS_extern: return "extern";
328 case DeclSpec::SCS_static: return "static";
329 case DeclSpec::SCS_auto: return "auto";
330 case DeclSpec::SCS_register: return "register";
Eli Friedman63054b32009-04-19 20:27:55 +0000331 case DeclSpec::SCS_private_extern: return "__private_extern__";
Sebastian Redl669d5d72008-11-14 23:42:31 +0000332 case DeclSpec::SCS_mutable: return "mutable";
Reid Spencer5f016e22007-07-11 17:01:13 +0000333 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000334 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000335}
336
John McCall32d335e2009-08-03 18:47:27 +0000337const char *DeclSpec::getSpecifierName(TSW W) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000338 switch (W) {
John McCall32d335e2009-08-03 18:47:27 +0000339 case TSW_unspecified: return "unspecified";
340 case TSW_short: return "short";
341 case TSW_long: return "long";
342 case TSW_longlong: return "long long";
Reid Spencer5f016e22007-07-11 17:01:13 +0000343 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000344 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000345}
346
John McCall32d335e2009-08-03 18:47:27 +0000347const char *DeclSpec::getSpecifierName(TSC C) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000348 switch (C) {
John McCall32d335e2009-08-03 18:47:27 +0000349 case TSC_unspecified: return "unspecified";
350 case TSC_imaginary: return "imaginary";
351 case TSC_complex: return "complex";
Reid Spencer5f016e22007-07-11 17:01:13 +0000352 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000353 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000354}
355
356
John McCall32d335e2009-08-03 18:47:27 +0000357const char *DeclSpec::getSpecifierName(TSS S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000358 switch (S) {
John McCall32d335e2009-08-03 18:47:27 +0000359 case TSS_unspecified: return "unspecified";
360 case TSS_signed: return "signed";
361 case TSS_unsigned: return "unsigned";
Reid Spencer5f016e22007-07-11 17:01:13 +0000362 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000363 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000364}
365
366const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
367 switch (T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000368 case DeclSpec::TST_unspecified: return "unspecified";
369 case DeclSpec::TST_void: return "void";
370 case DeclSpec::TST_char: return "char";
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000371 case DeclSpec::TST_wchar: return "wchar_t";
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000372 case DeclSpec::TST_char16: return "char16_t";
373 case DeclSpec::TST_char32: return "char32_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000374 case DeclSpec::TST_int: return "int";
375 case DeclSpec::TST_float: return "float";
376 case DeclSpec::TST_double: return "double";
377 case DeclSpec::TST_bool: return "_Bool";
378 case DeclSpec::TST_decimal32: return "_Decimal32";
379 case DeclSpec::TST_decimal64: return "_Decimal64";
380 case DeclSpec::TST_decimal128: return "_Decimal128";
381 case DeclSpec::TST_enum: return "enum";
Chris Lattner99dc9142008-04-13 18:59:07 +0000382 case DeclSpec::TST_class: return "class";
Reid Spencer5f016e22007-07-11 17:01:13 +0000383 case DeclSpec::TST_union: return "union";
384 case DeclSpec::TST_struct: return "struct";
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000385 case DeclSpec::TST_typename: return "type-name";
Steve Naroffd1861fd2007-07-31 12:34:36 +0000386 case DeclSpec::TST_typeofType:
387 case DeclSpec::TST_typeofExpr: return "typeof";
John McCall32d335e2009-08-03 18:47:27 +0000388 case DeclSpec::TST_auto: return "auto";
389 case DeclSpec::TST_decltype: return "(decltype)";
Sean Huntca63c202011-05-24 22:41:36 +0000390 case DeclSpec::TST_underlyingType: return "__underlying_type";
John McCalla5fc4722011-04-09 22:50:59 +0000391 case DeclSpec::TST_unknown_anytype: return "__unknown_anytype";
John McCall32d335e2009-08-03 18:47:27 +0000392 case DeclSpec::TST_error: return "(error)";
Reid Spencer5f016e22007-07-11 17:01:13 +0000393 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000394 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000395}
396
John McCall32d335e2009-08-03 18:47:27 +0000397const char *DeclSpec::getSpecifierName(TQ T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000398 switch (T) {
John McCall32d335e2009-08-03 18:47:27 +0000399 case DeclSpec::TQ_unspecified: return "unspecified";
400 case DeclSpec::TQ_const: return "const";
401 case DeclSpec::TQ_restrict: return "restrict";
402 case DeclSpec::TQ_volatile: return "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +0000403 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000404 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000405}
406
Peter Collingbourneb8b0e752011-10-06 03:01:00 +0000407bool DeclSpec::SetStorageClassSpec(Sema &S, SCS SC, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000408 const char *&PrevSpec,
Peter Collingbourneb8b0e752011-10-06 03:01:00 +0000409 unsigned &DiagID) {
410 // OpenCL 1.1 6.8g: "The extern, static, auto and register storage-class
411 // specifiers are not supported."
Peter Collingbournee2f82f72011-02-11 19:59:54 +0000412 // It seems sensible to prohibit private_extern too
Peter Collingbourneb8b0e752011-10-06 03:01:00 +0000413 // The cl_clang_storage_class_specifiers extension enables support for
414 // these storage-class specifiers.
415 if (S.getLangOptions().OpenCL &&
416 !S.getOpenCLOptions().cl_clang_storage_class_specifiers) {
417 switch (SC) {
Peter Collingbournee2f82f72011-02-11 19:59:54 +0000418 case SCS_extern:
419 case SCS_private_extern:
420 case SCS_auto:
421 case SCS_register:
422 case SCS_static:
423 DiagID = diag::err_not_opencl_storage_class_specifier;
Peter Collingbourneb8b0e752011-10-06 03:01:00 +0000424 PrevSpec = getSpecifierName(SC);
Peter Collingbournee2f82f72011-02-11 19:59:54 +0000425 return true;
426 default:
427 break;
428 }
429 }
430
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000431 if (StorageClassSpec != SCS_unspecified) {
Richard Smith8f4fb192011-09-04 19:54:14 +0000432 // Maybe this is an attempt to use C++0x 'auto' outside of C++0x mode.
433 bool isInvalid = true;
Peter Collingbourneb8b0e752011-10-06 03:01:00 +0000434 if (TypeSpecType == TST_unspecified && S.getLangOptions().CPlusPlus) {
435 if (SC == SCS_auto)
Richard Smith8f4fb192011-09-04 19:54:14 +0000436 return SetTypeSpecType(TST_auto, Loc, PrevSpec, DiagID);
437 if (StorageClassSpec == SCS_auto) {
438 isInvalid = SetTypeSpecType(TST_auto, StorageClassSpecLoc,
439 PrevSpec, DiagID);
440 assert(!isInvalid && "auto SCS -> TST recovery failed");
441 }
442 }
443
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000444 // Changing storage class is allowed only if the previous one
445 // was the 'extern' that is part of a linkage specification and
446 // the new storage class is 'typedef'.
Richard Smith8f4fb192011-09-04 19:54:14 +0000447 if (isInvalid &&
448 !(SCS_extern_in_linkage_spec &&
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000449 StorageClassSpec == SCS_extern &&
Peter Collingbourneb8b0e752011-10-06 03:01:00 +0000450 SC == SCS_typedef))
451 return BadSpecifier(SC, (SCS)StorageClassSpec, PrevSpec, DiagID);
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000452 }
Peter Collingbourneb8b0e752011-10-06 03:01:00 +0000453 StorageClassSpec = SC;
Reid Spencer5f016e22007-07-11 17:01:13 +0000454 StorageClassSpecLoc = Loc;
Peter Collingbourneb8b0e752011-10-06 03:01:00 +0000455 assert((unsigned)SC == StorageClassSpec && "SCS constants overflow bitfield");
Reid Spencer5f016e22007-07-11 17:01:13 +0000456 return false;
457}
458
Mike Stump1eb44332009-09-09 15:08:12 +0000459bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000460 const char *&PrevSpec,
461 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000462 if (SCS_thread_specified) {
463 PrevSpec = "__thread";
John McCallfec54012009-08-03 20:12:06 +0000464 DiagID = diag::ext_duplicate_declspec;
Reid Spencer5f016e22007-07-11 17:01:13 +0000465 return true;
466 }
467 SCS_thread_specified = true;
468 SCS_threadLoc = Loc;
469 return false;
470}
471
Reid Spencer5f016e22007-07-11 17:01:13 +0000472/// These methods set the specified attribute of the DeclSpec, but return true
473/// and ignore the request if invalid (e.g. "extern" then "auto" is
474/// specified).
475bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000476 const char *&PrevSpec,
477 unsigned &DiagID) {
Abramo Bagnara2553eaf2011-03-06 22:21:56 +0000478 // Overwrite TSWLoc only if TypeSpecWidth was unspecified, so that
479 // for 'long long' we will keep the source location of the first 'long'.
480 if (TypeSpecWidth == TSW_unspecified)
481 TSWLoc = Loc;
482 // Allow turning long -> long long.
483 else if (W != TSW_longlong || TypeSpecWidth != TSW_long)
John McCallfec54012009-08-03 20:12:06 +0000484 return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000485 TypeSpecWidth = W;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000486 if (TypeAltiVecVector && !TypeAltiVecBool &&
487 ((TypeSpecWidth == TSW_long) || (TypeSpecWidth == TSW_longlong))) {
John Thompson82287d12010-02-05 00:12:22 +0000488 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
489 DiagID = diag::warn_vector_long_decl_spec_combination;
490 return true;
491 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000492 return false;
493}
494
Mike Stump1eb44332009-09-09 15:08:12 +0000495bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000496 const char *&PrevSpec,
497 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000498 if (TypeSpecComplex != TSC_unspecified)
John McCallfec54012009-08-03 20:12:06 +0000499 return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000500 TypeSpecComplex = C;
501 TSCLoc = Loc;
502 return false;
503}
504
Mike Stump1eb44332009-09-09 15:08:12 +0000505bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000506 const char *&PrevSpec,
507 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000508 if (TypeSpecSign != TSS_unspecified)
John McCallfec54012009-08-03 20:12:06 +0000509 return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000510 TypeSpecSign = S;
511 TSSLoc = Loc;
512 return false;
513}
514
515bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000516 const char *&PrevSpec,
517 unsigned &DiagID,
John McCallb3d87482010-08-24 05:47:05 +0000518 ParsedType Rep) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000519 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep);
520}
521
522bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
523 SourceLocation TagNameLoc,
524 const char *&PrevSpec,
525 unsigned &DiagID,
526 ParsedType Rep) {
John McCallb3d87482010-08-24 05:47:05 +0000527 assert(isTypeRep(T) && "T does not store a type");
528 assert(Rep && "no type provided!");
529 if (TypeSpecType != TST_unspecified) {
530 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
531 DiagID = diag::err_invalid_decl_spec_combination;
532 return true;
533 }
534 TypeSpecType = T;
535 TypeRep = Rep;
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000536 TSTLoc = TagKwLoc;
537 TSTNameLoc = TagNameLoc;
John McCallb3d87482010-08-24 05:47:05 +0000538 TypeSpecOwned = false;
539 return false;
540}
541
542bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
543 const char *&PrevSpec,
544 unsigned &DiagID,
545 Expr *Rep) {
546 assert(isExprRep(T) && "T does not store an expr");
547 assert(Rep && "no expression provided!");
548 if (TypeSpecType != TST_unspecified) {
549 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
550 DiagID = diag::err_invalid_decl_spec_combination;
551 return true;
552 }
553 TypeSpecType = T;
554 ExprRep = Rep;
555 TSTLoc = Loc;
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000556 TSTNameLoc = Loc;
John McCallb3d87482010-08-24 05:47:05 +0000557 TypeSpecOwned = false;
558 return false;
559}
560
561bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
562 const char *&PrevSpec,
563 unsigned &DiagID,
564 Decl *Rep, bool Owned) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000565 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep, Owned);
566}
567
568bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
569 SourceLocation TagNameLoc,
570 const char *&PrevSpec,
571 unsigned &DiagID,
572 Decl *Rep, bool Owned) {
John McCallb3d87482010-08-24 05:47:05 +0000573 assert(isDeclRep(T) && "T does not store a decl");
574 // Unlike the other cases, we don't assert that we actually get a decl.
575
576 if (TypeSpecType != TST_unspecified) {
577 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
578 DiagID = diag::err_invalid_decl_spec_combination;
579 return true;
580 }
581 TypeSpecType = T;
582 DeclRep = Rep;
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000583 TSTLoc = TagKwLoc;
584 TSTNameLoc = TagNameLoc;
John McCallb3d87482010-08-24 05:47:05 +0000585 TypeSpecOwned = Owned;
586 return false;
587}
588
589bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
590 const char *&PrevSpec,
591 unsigned &DiagID) {
592 assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) &&
593 "rep required for these type-spec kinds!");
John McCallfec54012009-08-03 20:12:06 +0000594 if (TypeSpecType != TST_unspecified) {
595 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
596 DiagID = diag::err_invalid_decl_spec_combination;
597 return true;
598 }
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000599 TSTLoc = Loc;
600 TSTNameLoc = Loc;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000601 if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) {
602 TypeAltiVecBool = true;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000603 return false;
604 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000605 TypeSpecType = T;
John McCallb3d87482010-08-24 05:47:05 +0000606 TypeSpecOwned = false;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000607 if (TypeAltiVecVector && !TypeAltiVecBool && (TypeSpecType == TST_double)) {
John Thompson82287d12010-02-05 00:12:22 +0000608 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
Chris Lattner788b0fd2010-06-23 06:00:24 +0000609 DiagID = diag::err_invalid_vector_decl_spec;
John Thompson82287d12010-02-05 00:12:22 +0000610 return true;
611 }
612 return false;
613}
614
615bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
616 const char *&PrevSpec, unsigned &DiagID) {
617 if (TypeSpecType != TST_unspecified) {
618 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
619 DiagID = diag::err_invalid_vector_decl_spec_combination;
620 return true;
621 }
622 TypeAltiVecVector = isAltiVecVector;
623 AltiVecLoc = Loc;
624 return false;
625}
626
627bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
628 const char *&PrevSpec, unsigned &DiagID) {
Chris Lattner788b0fd2010-06-23 06:00:24 +0000629 if (!TypeAltiVecVector || TypeAltiVecPixel ||
630 (TypeSpecType != TST_unspecified)) {
John Thompson82287d12010-02-05 00:12:22 +0000631 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
632 DiagID = diag::err_invalid_pixel_decl_spec_combination;
633 return true;
634 }
John Thompson82287d12010-02-05 00:12:22 +0000635 TypeAltiVecPixel = isAltiVecPixel;
636 TSTLoc = Loc;
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000637 TSTNameLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000638 return false;
639}
640
Douglas Gregorddc29e12009-02-06 22:42:48 +0000641bool DeclSpec::SetTypeSpecError() {
642 TypeSpecType = TST_error;
John McCall9e46b8c2010-08-26 17:22:34 +0000643 TypeSpecOwned = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000644 TSTLoc = SourceLocation();
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000645 TSTNameLoc = SourceLocation();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000646 return false;
647}
648
Reid Spencer5f016e22007-07-11 17:01:13 +0000649bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000650 unsigned &DiagID, const LangOptions &Lang) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000651 // Duplicates turn into warnings pre-C99.
652 if ((TypeQualifiers & T) && !Lang.C99)
John McCallfec54012009-08-03 20:12:06 +0000653 return BadSpecifier(T, T, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000654 TypeQualifiers |= T;
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Reid Spencer5f016e22007-07-11 17:01:13 +0000656 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000657 default: llvm_unreachable("Unknown type qualifier!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000658 case TQ_const: TQ_constLoc = Loc; break;
659 case TQ_restrict: TQ_restrictLoc = Loc; break;
660 case TQ_volatile: TQ_volatileLoc = Loc; break;
661 }
662 return false;
663}
664
John McCallfec54012009-08-03 20:12:06 +0000665bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
666 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000667 // 'inline inline' is ok.
668 FS_inline_specified = true;
669 FS_inlineLoc = Loc;
670 return false;
671}
672
John McCallfec54012009-08-03 20:12:06 +0000673bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec,
674 unsigned &DiagID) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000675 // 'virtual virtual' is ok.
676 FS_virtual_specified = true;
677 FS_virtualLoc = Loc;
678 return false;
679}
680
John McCallfec54012009-08-03 20:12:06 +0000681bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec,
682 unsigned &DiagID) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000683 // 'explicit explicit' is ok.
684 FS_explicit_specified = true;
685 FS_explicitLoc = Loc;
686 return false;
687}
688
John McCallfec54012009-08-03 20:12:06 +0000689bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
690 unsigned &DiagID) {
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000691 if (Friend_specified) {
692 PrevSpec = "friend";
John McCallfec54012009-08-03 20:12:06 +0000693 DiagID = diag::ext_duplicate_declspec;
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000694 return true;
695 }
John McCallfec54012009-08-03 20:12:06 +0000696
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000697 Friend_specified = true;
698 FriendLoc = Loc;
699 return false;
700}
Reid Spencer5f016e22007-07-11 17:01:13 +0000701
Douglas Gregor8d267c52011-09-09 02:06:17 +0000702bool DeclSpec::setModulePrivateSpec(SourceLocation Loc, const char *&PrevSpec,
703 unsigned &DiagID) {
704 if (isModulePrivateSpecified()) {
705 PrevSpec = "__module_private__";
706 DiagID = diag::ext_duplicate_declspec;
707 return true;
708 }
709
710 ModulePrivateLoc = Loc;
711 return false;
712}
713
Sebastian Redl2ac67232009-11-05 15:47:02 +0000714bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
715 unsigned &DiagID) {
716 // 'constexpr constexpr' is ok.
717 Constexpr_specified = true;
718 ConstexprLoc = Loc;
719 return false;
720}
721
John McCalld226f652010-08-21 09:40:31 +0000722void DeclSpec::setProtocolQualifiers(Decl * const *Protos,
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000723 unsigned NP,
724 SourceLocation *ProtoLocs,
725 SourceLocation LAngleLoc) {
726 if (NP == 0) return;
John McCalld226f652010-08-21 09:40:31 +0000727 ProtocolQualifiers = new Decl*[NP];
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000728 ProtocolLocs = new SourceLocation[NP];
John McCalld226f652010-08-21 09:40:31 +0000729 memcpy((void*)ProtocolQualifiers, Protos, sizeof(Decl*)*NP);
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000730 memcpy(ProtocolLocs, ProtoLocs, sizeof(SourceLocation)*NP);
731 NumProtocolQualifiers = NP;
732 ProtocolLAngleLoc = LAngleLoc;
733}
734
Douglas Gregorddf889a2010-01-18 18:04:31 +0000735void DeclSpec::SaveWrittenBuiltinSpecs() {
736 writtenBS.Sign = getTypeSpecSign();
737 writtenBS.Width = getTypeSpecWidth();
738 writtenBS.Type = getTypeSpecType();
739 // Search the list of attributes for the presence of a mode attribute.
740 writtenBS.ModeAttr = false;
John McCall7f040a92010-12-24 02:08:15 +0000741 AttributeList* attrs = getAttributes().getList();
Douglas Gregorddf889a2010-01-18 18:04:31 +0000742 while (attrs) {
743 if (attrs->getKind() == AttributeList::AT_mode) {
744 writtenBS.ModeAttr = true;
745 break;
746 }
747 attrs = attrs->getNext();
748 }
749}
750
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000751void DeclSpec::SaveStorageSpecifierAsWritten() {
752 if (SCS_extern_in_linkage_spec && StorageClassSpec == SCS_extern)
753 // If 'extern' is part of a linkage specification,
754 // then it is not a storage class "as written".
755 StorageClassSpecAsWritten = SCS_unspecified;
756 else
757 StorageClassSpecAsWritten = StorageClassSpec;
758}
759
Reid Spencer5f016e22007-07-11 17:01:13 +0000760/// Finish - This does final analysis of the declspec, rejecting things like
761/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
762/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
763/// DeclSpec is guaranteed self-consistent, even if an error occurred.
David Blaikied6471f72011-09-25 23:23:43 +0000764void DeclSpec::Finish(DiagnosticsEngine &D, Preprocessor &PP) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000765 // Before possibly changing their values, save specs as written.
766 SaveWrittenBuiltinSpecs();
Douglas Gregor16573fa2010-04-19 22:54:31 +0000767 SaveStorageSpecifierAsWritten();
Douglas Gregorddf889a2010-01-18 18:04:31 +0000768
Reid Spencer5f016e22007-07-11 17:01:13 +0000769 // Check the type specifier components first.
770
Chris Lattner788b0fd2010-06-23 06:00:24 +0000771 // Validate and finalize AltiVec vector declspec.
772 if (TypeAltiVecVector) {
773 if (TypeAltiVecBool) {
774 // Sign specifiers are not allowed with vector bool. (PIM 2.1)
775 if (TypeSpecSign != TSS_unspecified) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000776 Diag(D, TSSLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner788b0fd2010-06-23 06:00:24 +0000777 << getSpecifierName((TSS)TypeSpecSign);
778 }
779
780 // Only char/int are valid with vector bool. (PIM 2.1)
Duncan Sands2e964a922010-06-23 19:34:52 +0000781 if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) &&
782 (TypeSpecType != TST_int)) || TypeAltiVecPixel) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000783 Diag(D, TSTLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner788b0fd2010-06-23 06:00:24 +0000784 << (TypeAltiVecPixel ? "__pixel" :
785 getSpecifierName((TST)TypeSpecType));
786 }
787
788 // Only 'short' is valid with vector bool. (PIM 2.1)
789 if ((TypeSpecWidth != TSW_unspecified) && (TypeSpecWidth != TSW_short))
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000790 Diag(D, TSWLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner788b0fd2010-06-23 06:00:24 +0000791 << getSpecifierName((TSW)TypeSpecWidth);
792
793 // Elements of vector bool are interpreted as unsigned. (PIM 2.1)
794 if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) ||
795 (TypeSpecWidth != TSW_unspecified))
796 TypeSpecSign = TSS_unsigned;
797 }
798
799 if (TypeAltiVecPixel) {
800 //TODO: perform validation
801 TypeSpecType = TST_int;
802 TypeSpecSign = TSS_unsigned;
803 TypeSpecWidth = TSW_short;
John McCall9e46b8c2010-08-26 17:22:34 +0000804 TypeSpecOwned = false;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000805 }
806 }
807
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000808 // signed/unsigned are only valid with int/char/wchar_t.
Reid Spencer5f016e22007-07-11 17:01:13 +0000809 if (TypeSpecSign != TSS_unspecified) {
810 if (TypeSpecType == TST_unspecified)
811 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000812 else if (TypeSpecType != TST_int &&
813 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000814 Diag(D, TSSLoc, diag::err_invalid_sign_spec)
Chris Lattner254be6a2008-11-22 08:32:36 +0000815 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000816 // signed double -> double.
817 TypeSpecSign = TSS_unspecified;
818 }
819 }
820
821 // Validate the width of the type.
822 switch (TypeSpecWidth) {
823 case TSW_unspecified: break;
824 case TSW_short: // short int
825 case TSW_longlong: // long long int
826 if (TypeSpecType == TST_unspecified)
827 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
828 else if (TypeSpecType != TST_int) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000829 Diag(D, TSWLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000830 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
Chris Lattner254be6a2008-11-22 08:32:36 +0000831 : diag::err_invalid_longlong_spec)
832 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000833 TypeSpecType = TST_int;
John McCall9e46b8c2010-08-26 17:22:34 +0000834 TypeSpecOwned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000835 }
836 break;
837 case TSW_long: // long double, long int
838 if (TypeSpecType == TST_unspecified)
839 TypeSpecType = TST_int; // long -> long int.
840 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000841 Diag(D, TSWLoc, diag::err_invalid_long_spec)
Chris Lattner254be6a2008-11-22 08:32:36 +0000842 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000843 TypeSpecType = TST_int;
John McCall9e46b8c2010-08-26 17:22:34 +0000844 TypeSpecOwned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000845 }
846 break;
847 }
Mike Stump1eb44332009-09-09 15:08:12 +0000848
Reid Spencer5f016e22007-07-11 17:01:13 +0000849 // TODO: if the implementation does not implement _Complex or _Imaginary,
850 // disallow their use. Need information about the backend.
851 if (TypeSpecComplex != TSC_unspecified) {
852 if (TypeSpecType == TST_unspecified) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000853 Diag(D, TSCLoc, diag::ext_plain_complex)
Douglas Gregor849b2432010-03-31 17:46:05 +0000854 << FixItHint::CreateInsertion(
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000855 PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
856 " double");
Reid Spencer5f016e22007-07-11 17:01:13 +0000857 TypeSpecType = TST_double; // _Complex -> _Complex double.
858 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
859 // Note that this intentionally doesn't include _Complex _Bool.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000860 Diag(D, TSTLoc, diag::ext_integer_complex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000861 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000862 Diag(D, TSCLoc, diag::err_invalid_complex_spec)
Chris Lattner254be6a2008-11-22 08:32:36 +0000863 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000864 TypeSpecComplex = TSC_unspecified;
865 }
866 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000867
Richard Smith8f4fb192011-09-04 19:54:14 +0000868 // If no type specifier was provided and we're parsing a language where
869 // the type specifier is not optional, but we got 'auto' as a storage
870 // class specifier, then assume this is an attempt to use C++0x's 'auto'
871 // type specifier.
872 // FIXME: Does Microsoft really support implicit int in C++?
Francois Pichet62ec1f22011-09-17 17:15:52 +0000873 if (PP.getLangOptions().CPlusPlus && !PP.getLangOptions().MicrosoftExt &&
Richard Smith8f4fb192011-09-04 19:54:14 +0000874 TypeSpecType == TST_unspecified && StorageClassSpec == SCS_auto) {
875 TypeSpecType = TST_auto;
876 StorageClassSpec = StorageClassSpecAsWritten = SCS_unspecified;
877 TSTLoc = TSTNameLoc = StorageClassSpecLoc;
878 StorageClassSpecLoc = SourceLocation();
879 }
880 // Diagnose if we've recovered from an ill-formed 'auto' storage class
881 // specifier in a pre-C++0x dialect of C++.
882 if (!PP.getLangOptions().CPlusPlus0x && TypeSpecType == TST_auto)
883 Diag(D, TSTLoc, diag::ext_auto_type_specifier);
884 if (PP.getLangOptions().CPlusPlus && !PP.getLangOptions().CPlusPlus0x &&
885 StorageClassSpec == SCS_auto)
886 Diag(D, StorageClassSpecLoc, diag::warn_auto_storage_class)
887 << FixItHint::CreateRemoval(StorageClassSpecLoc);
888
John McCall67d1a672009-08-06 02:15:43 +0000889 // C++ [class.friend]p6:
890 // No storage-class-specifier shall appear in the decl-specifier-seq
891 // of a friend declaration.
892 if (isFriendSpecified() && getStorageClassSpec()) {
893 DeclSpec::SCS SC = getStorageClassSpec();
894 const char *SpecName = getSpecifierName(SC);
895
896 SourceLocation SCLoc = getStorageClassSpecLoc();
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000897 SourceLocation SCEndLoc = SCLoc.getLocWithOffset(strlen(SpecName));
John McCall67d1a672009-08-06 02:15:43 +0000898
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000899 Diag(D, SCLoc, diag::err_friend_storage_spec)
John McCall67d1a672009-08-06 02:15:43 +0000900 << SpecName
Douglas Gregor849b2432010-03-31 17:46:05 +0000901 << FixItHint::CreateRemoval(SourceRange(SCLoc, SCEndLoc));
John McCall67d1a672009-08-06 02:15:43 +0000902
903 ClearStorageClassSpecs();
904 }
905
Douglas Gregor6274d302011-09-09 21:14:29 +0000906 assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType));
907
Reid Spencer5f016e22007-07-11 17:01:13 +0000908 // Okay, now we can infer the real type.
Mike Stump1eb44332009-09-09 15:08:12 +0000909
Reid Spencer5f016e22007-07-11 17:01:13 +0000910 // TODO: return "auto function" and other bad things based on the real type.
Mike Stump1eb44332009-09-09 15:08:12 +0000911
Reid Spencer5f016e22007-07-11 17:01:13 +0000912 // 'data definition has no type or storage class'?
913}
Daniel Dunbare4858a62008-08-11 03:45:03 +0000914
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000915bool DeclSpec::isMissingDeclaratorOk() {
916 TST tst = getTypeSpecType();
John McCallb3d87482010-08-24 05:47:05 +0000917 return isDeclRep(tst) && getRepAsDecl() != 0 &&
918 StorageClassSpec != DeclSpec::SCS_typedef;
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000919}
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000920
921void UnqualifiedId::clear() {
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000922 Kind = IK_Identifier;
923 Identifier = 0;
924 StartLocation = SourceLocation();
925 EndLocation = SourceLocation();
926}
927
928void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc,
929 OverloadedOperatorKind Op,
930 SourceLocation SymbolLocations[3]) {
931 Kind = IK_OperatorFunctionId;
932 StartLocation = OperatorLoc;
933 EndLocation = OperatorLoc;
934 OperatorFunctionId.Operator = Op;
935 for (unsigned I = 0; I != 3; ++I) {
936 OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding();
937
938 if (SymbolLocations[I].isValid())
939 EndLocation = SymbolLocations[I];
940 }
941}
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000942
Anders Carlssoncc54d592011-01-22 16:56:46 +0000943bool VirtSpecifiers::SetSpecifier(Specifier VS, SourceLocation Loc,
Anders Carlsson46127a92011-01-22 15:58:16 +0000944 const char *&PrevSpec) {
Douglas Gregorf5251602011-03-08 17:10:18 +0000945 LastLocation = Loc;
946
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000947 if (Specifiers & VS) {
948 PrevSpec = getSpecifierName(VS);
949 return true;
950 }
951
952 Specifiers |= VS;
953
954 switch (VS) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000955 default: llvm_unreachable("Unknown specifier!");
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000956 case VS_Override: VS_overrideLoc = Loc; break;
957 case VS_Final: VS_finalLoc = Loc; break;
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000958 }
Anders Carlsson46127a92011-01-22 15:58:16 +0000959
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000960 return false;
961}
962
Anders Carlssoncc54d592011-01-22 16:56:46 +0000963const char *VirtSpecifiers::getSpecifierName(Specifier VS) {
Anders Carlssonc46bb7d2011-01-22 15:11:37 +0000964 switch (VS) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000965 default: llvm_unreachable("Unknown specifier");
Anders Carlssonc46bb7d2011-01-22 15:11:37 +0000966 case VS_Override: return "override";
967 case VS_Final: return "final";
Anders Carlssonc46bb7d2011-01-22 15:11:37 +0000968 }
969}