[docs] Fix build-docs.sh
[llvm-project.git] / clang / lib / Sema / DeclSpec.cpp
blob0c4b79d8b96424cfeb86997d40dc6cc750641c70
1 //===--- DeclSpec.cpp - Declaration Specifier Semantic Analysis -----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements semantic analysis for declaration specifiers.
11 //===----------------------------------------------------------------------===//
13 #include "clang/Sema/DeclSpec.h"
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/DeclCXX.h"
16 #include "clang/AST/Expr.h"
17 #include "clang/AST/LocInfoType.h"
18 #include "clang/AST/TypeLoc.h"
19 #include "clang/Basic/LangOptions.h"
20 #include "clang/Basic/SourceManager.h"
21 #include "clang/Basic/Specifiers.h"
22 #include "clang/Basic/TargetInfo.h"
23 #include "clang/Sema/ParsedTemplate.h"
24 #include "clang/Sema/Sema.h"
25 #include "clang/Sema/SemaDiagnostic.h"
26 #include "llvm/ADT/STLExtras.h"
27 #include "llvm/ADT/SmallString.h"
28 #include <cstring>
29 using namespace clang;
32 void UnqualifiedId::setTemplateId(TemplateIdAnnotation *TemplateId) {
33 assert(TemplateId && "NULL template-id annotation?");
34 assert(!TemplateId->isInvalid() &&
35 "should not convert invalid template-ids to unqualified-ids");
37 Kind = UnqualifiedIdKind::IK_TemplateId;
38 this->TemplateId = TemplateId;
39 StartLocation = TemplateId->TemplateNameLoc;
40 EndLocation = TemplateId->RAngleLoc;
43 void UnqualifiedId::setConstructorTemplateId(TemplateIdAnnotation *TemplateId) {
44 assert(TemplateId && "NULL template-id annotation?");
45 assert(!TemplateId->isInvalid() &&
46 "should not convert invalid template-ids to unqualified-ids");
48 Kind = UnqualifiedIdKind::IK_ConstructorTemplateId;
49 this->TemplateId = TemplateId;
50 StartLocation = TemplateId->TemplateNameLoc;
51 EndLocation = TemplateId->RAngleLoc;
54 void CXXScopeSpec::Extend(ASTContext &Context, SourceLocation TemplateKWLoc,
55 TypeLoc TL, SourceLocation ColonColonLoc) {
56 Builder.Extend(Context, TemplateKWLoc, TL, ColonColonLoc);
57 if (Range.getBegin().isInvalid())
58 Range.setBegin(TL.getBeginLoc());
59 Range.setEnd(ColonColonLoc);
61 assert(Range == Builder.getSourceRange() &&
62 "NestedNameSpecifierLoc range computation incorrect");
65 void CXXScopeSpec::Extend(ASTContext &Context, IdentifierInfo *Identifier,
66 SourceLocation IdentifierLoc,
67 SourceLocation ColonColonLoc) {
68 Builder.Extend(Context, Identifier, IdentifierLoc, ColonColonLoc);
70 if (Range.getBegin().isInvalid())
71 Range.setBegin(IdentifierLoc);
72 Range.setEnd(ColonColonLoc);
74 assert(Range == Builder.getSourceRange() &&
75 "NestedNameSpecifierLoc range computation incorrect");
78 void CXXScopeSpec::Extend(ASTContext &Context, NamespaceDecl *Namespace,
79 SourceLocation NamespaceLoc,
80 SourceLocation ColonColonLoc) {
81 Builder.Extend(Context, Namespace, NamespaceLoc, ColonColonLoc);
83 if (Range.getBegin().isInvalid())
84 Range.setBegin(NamespaceLoc);
85 Range.setEnd(ColonColonLoc);
87 assert(Range == Builder.getSourceRange() &&
88 "NestedNameSpecifierLoc range computation incorrect");
91 void CXXScopeSpec::Extend(ASTContext &Context, NamespaceAliasDecl *Alias,
92 SourceLocation AliasLoc,
93 SourceLocation ColonColonLoc) {
94 Builder.Extend(Context, Alias, AliasLoc, ColonColonLoc);
96 if (Range.getBegin().isInvalid())
97 Range.setBegin(AliasLoc);
98 Range.setEnd(ColonColonLoc);
100 assert(Range == Builder.getSourceRange() &&
101 "NestedNameSpecifierLoc range computation incorrect");
104 void CXXScopeSpec::MakeGlobal(ASTContext &Context,
105 SourceLocation ColonColonLoc) {
106 Builder.MakeGlobal(Context, ColonColonLoc);
108 Range = SourceRange(ColonColonLoc);
110 assert(Range == Builder.getSourceRange() &&
111 "NestedNameSpecifierLoc range computation incorrect");
114 void CXXScopeSpec::MakeSuper(ASTContext &Context, CXXRecordDecl *RD,
115 SourceLocation SuperLoc,
116 SourceLocation ColonColonLoc) {
117 Builder.MakeSuper(Context, RD, SuperLoc, ColonColonLoc);
119 Range.setBegin(SuperLoc);
120 Range.setEnd(ColonColonLoc);
122 assert(Range == Builder.getSourceRange() &&
123 "NestedNameSpecifierLoc range computation incorrect");
126 void CXXScopeSpec::MakeTrivial(ASTContext &Context,
127 NestedNameSpecifier *Qualifier, SourceRange R) {
128 Builder.MakeTrivial(Context, Qualifier, R);
129 Range = R;
132 void CXXScopeSpec::Adopt(NestedNameSpecifierLoc Other) {
133 if (!Other) {
134 Range = SourceRange();
135 Builder.Clear();
136 return;
139 Range = Other.getSourceRange();
140 Builder.Adopt(Other);
141 assert(Range == Builder.getSourceRange() &&
142 "NestedNameSpecifierLoc range computation incorrect");
145 SourceLocation CXXScopeSpec::getLastQualifierNameLoc() const {
146 if (!Builder.getRepresentation())
147 return SourceLocation();
148 return Builder.getTemporary().getLocalBeginLoc();
151 NestedNameSpecifierLoc
152 CXXScopeSpec::getWithLocInContext(ASTContext &Context) const {
153 if (!Builder.getRepresentation())
154 return NestedNameSpecifierLoc();
156 return Builder.getWithLocInContext(Context);
159 /// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function.
160 /// "TheDeclarator" is the declarator that this will be added to.
161 DeclaratorChunk DeclaratorChunk::getFunction(bool hasProto,
162 bool isAmbiguous,
163 SourceLocation LParenLoc,
164 ParamInfo *Params,
165 unsigned NumParams,
166 SourceLocation EllipsisLoc,
167 SourceLocation RParenLoc,
168 bool RefQualifierIsLvalueRef,
169 SourceLocation RefQualifierLoc,
170 SourceLocation MutableLoc,
171 ExceptionSpecificationType
172 ESpecType,
173 SourceRange ESpecRange,
174 ParsedType *Exceptions,
175 SourceRange *ExceptionRanges,
176 unsigned NumExceptions,
177 Expr *NoexceptExpr,
178 CachedTokens *ExceptionSpecTokens,
179 ArrayRef<NamedDecl*>
180 DeclsInPrototype,
181 SourceLocation LocalRangeBegin,
182 SourceLocation LocalRangeEnd,
183 Declarator &TheDeclarator,
184 TypeResult TrailingReturnType,
185 SourceLocation
186 TrailingReturnTypeLoc,
187 DeclSpec *MethodQualifiers) {
188 assert(!(MethodQualifiers && MethodQualifiers->getTypeQualifiers() & DeclSpec::TQ_atomic) &&
189 "function cannot have _Atomic qualifier");
191 DeclaratorChunk I;
192 I.Kind = Function;
193 I.Loc = LocalRangeBegin;
194 I.EndLoc = LocalRangeEnd;
195 new (&I.Fun) FunctionTypeInfo;
196 I.Fun.hasPrototype = hasProto;
197 I.Fun.isVariadic = EllipsisLoc.isValid();
198 I.Fun.isAmbiguous = isAmbiguous;
199 I.Fun.LParenLoc = LParenLoc;
200 I.Fun.EllipsisLoc = EllipsisLoc;
201 I.Fun.RParenLoc = RParenLoc;
202 I.Fun.DeleteParams = false;
203 I.Fun.NumParams = NumParams;
204 I.Fun.Params = nullptr;
205 I.Fun.RefQualifierIsLValueRef = RefQualifierIsLvalueRef;
206 I.Fun.RefQualifierLoc = RefQualifierLoc;
207 I.Fun.MutableLoc = MutableLoc;
208 I.Fun.ExceptionSpecType = ESpecType;
209 I.Fun.ExceptionSpecLocBeg = ESpecRange.getBegin();
210 I.Fun.ExceptionSpecLocEnd = ESpecRange.getEnd();
211 I.Fun.NumExceptionsOrDecls = 0;
212 I.Fun.Exceptions = nullptr;
213 I.Fun.NoexceptExpr = nullptr;
214 I.Fun.HasTrailingReturnType = TrailingReturnType.isUsable() ||
215 TrailingReturnType.isInvalid();
216 I.Fun.TrailingReturnType = TrailingReturnType.get();
217 I.Fun.TrailingReturnTypeLoc = TrailingReturnTypeLoc;
218 I.Fun.MethodQualifiers = nullptr;
219 I.Fun.QualAttrFactory = nullptr;
221 if (MethodQualifiers && (MethodQualifiers->getTypeQualifiers() ||
222 MethodQualifiers->getAttributes().size())) {
223 auto &attrs = MethodQualifiers->getAttributes();
224 I.Fun.MethodQualifiers = new DeclSpec(attrs.getPool().getFactory());
225 MethodQualifiers->forEachCVRUQualifier(
226 [&](DeclSpec::TQ TypeQual, StringRef PrintName, SourceLocation SL) {
227 I.Fun.MethodQualifiers->SetTypeQual(TypeQual, SL);
229 I.Fun.MethodQualifiers->getAttributes().takeAllFrom(attrs);
230 I.Fun.MethodQualifiers->getAttributePool().takeAllFrom(attrs.getPool());
233 assert(I.Fun.ExceptionSpecType == ESpecType && "bitfield overflow");
235 // new[] a parameter array if needed.
236 if (NumParams) {
237 // If the 'InlineParams' in Declarator is unused and big enough, put our
238 // parameter list there (in an effort to avoid new/delete traffic). If it
239 // is already used (consider a function returning a function pointer) or too
240 // small (function with too many parameters), go to the heap.
241 if (!TheDeclarator.InlineStorageUsed &&
242 NumParams <= std::size(TheDeclarator.InlineParams)) {
243 I.Fun.Params = TheDeclarator.InlineParams;
244 new (I.Fun.Params) ParamInfo[NumParams];
245 I.Fun.DeleteParams = false;
246 TheDeclarator.InlineStorageUsed = true;
247 } else {
248 I.Fun.Params = new DeclaratorChunk::ParamInfo[NumParams];
249 I.Fun.DeleteParams = true;
251 for (unsigned i = 0; i < NumParams; i++)
252 I.Fun.Params[i] = std::move(Params[i]);
255 // Check what exception specification information we should actually store.
256 switch (ESpecType) {
257 default: break; // By default, save nothing.
258 case EST_Dynamic:
259 // new[] an exception array if needed
260 if (NumExceptions) {
261 I.Fun.NumExceptionsOrDecls = NumExceptions;
262 I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions];
263 for (unsigned i = 0; i != NumExceptions; ++i) {
264 I.Fun.Exceptions[i].Ty = Exceptions[i];
265 I.Fun.Exceptions[i].Range = ExceptionRanges[i];
268 break;
270 case EST_DependentNoexcept:
271 case EST_NoexceptFalse:
272 case EST_NoexceptTrue:
273 I.Fun.NoexceptExpr = NoexceptExpr;
274 break;
276 case EST_Unparsed:
277 I.Fun.ExceptionSpecTokens = ExceptionSpecTokens;
278 break;
281 if (!DeclsInPrototype.empty()) {
282 assert(ESpecType == EST_None && NumExceptions == 0 &&
283 "cannot have exception specifiers and decls in prototype");
284 I.Fun.NumExceptionsOrDecls = DeclsInPrototype.size();
285 // Copy the array of decls into stable heap storage.
286 I.Fun.DeclsInPrototype = new NamedDecl *[DeclsInPrototype.size()];
287 for (size_t J = 0; J < DeclsInPrototype.size(); ++J)
288 I.Fun.DeclsInPrototype[J] = DeclsInPrototype[J];
291 return I;
294 void Declarator::setDecompositionBindings(
295 SourceLocation LSquareLoc,
296 ArrayRef<DecompositionDeclarator::Binding> Bindings,
297 SourceLocation RSquareLoc) {
298 assert(!hasName() && "declarator given multiple names!");
300 BindingGroup.LSquareLoc = LSquareLoc;
301 BindingGroup.RSquareLoc = RSquareLoc;
302 BindingGroup.NumBindings = Bindings.size();
303 Range.setEnd(RSquareLoc);
305 // We're now past the identifier.
306 SetIdentifier(nullptr, LSquareLoc);
307 Name.EndLocation = RSquareLoc;
309 // Allocate storage for bindings and stash them away.
310 if (Bindings.size()) {
311 if (!InlineStorageUsed && Bindings.size() <= std::size(InlineBindings)) {
312 BindingGroup.Bindings = InlineBindings;
313 BindingGroup.DeleteBindings = false;
314 InlineStorageUsed = true;
315 } else {
316 BindingGroup.Bindings =
317 new DecompositionDeclarator::Binding[Bindings.size()];
318 BindingGroup.DeleteBindings = true;
320 std::uninitialized_copy(Bindings.begin(), Bindings.end(),
321 BindingGroup.Bindings);
325 bool Declarator::isDeclarationOfFunction() const {
326 for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) {
327 switch (DeclTypeInfo[i].Kind) {
328 case DeclaratorChunk::Function:
329 return true;
330 case DeclaratorChunk::Paren:
331 continue;
332 case DeclaratorChunk::Pointer:
333 case DeclaratorChunk::Reference:
334 case DeclaratorChunk::Array:
335 case DeclaratorChunk::BlockPointer:
336 case DeclaratorChunk::MemberPointer:
337 case DeclaratorChunk::Pipe:
338 return false;
340 llvm_unreachable("Invalid type chunk");
343 switch (DS.getTypeSpecType()) {
344 case TST_atomic:
345 case TST_auto:
346 case TST_auto_type:
347 case TST_bool:
348 case TST_char:
349 case TST_char8:
350 case TST_char16:
351 case TST_char32:
352 case TST_class:
353 case TST_decimal128:
354 case TST_decimal32:
355 case TST_decimal64:
356 case TST_double:
357 case TST_Accum:
358 case TST_Fract:
359 case TST_Float16:
360 case TST_float128:
361 case TST_ibm128:
362 case TST_enum:
363 case TST_error:
364 case TST_float:
365 case TST_half:
366 case TST_int:
367 case TST_int128:
368 case TST_bitint:
369 case TST_struct:
370 case TST_interface:
371 case TST_union:
372 case TST_unknown_anytype:
373 case TST_unspecified:
374 case TST_void:
375 case TST_wchar:
376 case TST_BFloat16:
377 #define GENERIC_IMAGE_TYPE(ImgType, Id) case TST_##ImgType##_t:
378 #include "clang/Basic/OpenCLImageTypes.def"
379 return false;
381 case TST_decltype_auto:
382 // This must have an initializer, so can't be a function declaration,
383 // even if the initializer has function type.
384 return false;
386 case TST_decltype:
387 case TST_typeofExpr:
388 if (Expr *E = DS.getRepAsExpr())
389 return E->getType()->isFunctionType();
390 return false;
392 #define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) case TST_##Trait:
393 #include "clang/Basic/TransformTypeTraits.def"
394 case TST_typename:
395 case TST_typeofType: {
396 QualType QT = DS.getRepAsType().get();
397 if (QT.isNull())
398 return false;
400 if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT))
401 QT = LIT->getType();
403 if (QT.isNull())
404 return false;
406 return QT->isFunctionType();
410 llvm_unreachable("Invalid TypeSpecType!");
413 bool Declarator::isStaticMember() {
414 assert(getContext() == DeclaratorContext::Member);
415 return getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static ||
416 (getName().getKind() == UnqualifiedIdKind::IK_OperatorFunctionId &&
417 CXXMethodDecl::isStaticOverloadedOperator(
418 getName().OperatorFunctionId.Operator));
421 bool Declarator::isCtorOrDtor() {
422 return (getName().getKind() == UnqualifiedIdKind::IK_ConstructorName) ||
423 (getName().getKind() == UnqualifiedIdKind::IK_DestructorName);
426 void DeclSpec::forEachCVRUQualifier(
427 llvm::function_ref<void(TQ, StringRef, SourceLocation)> Handle) {
428 if (TypeQualifiers & TQ_const)
429 Handle(TQ_const, "const", TQ_constLoc);
430 if (TypeQualifiers & TQ_volatile)
431 Handle(TQ_volatile, "volatile", TQ_volatileLoc);
432 if (TypeQualifiers & TQ_restrict)
433 Handle(TQ_restrict, "restrict", TQ_restrictLoc);
434 if (TypeQualifiers & TQ_unaligned)
435 Handle(TQ_unaligned, "unaligned", TQ_unalignedLoc);
438 void DeclSpec::forEachQualifier(
439 llvm::function_ref<void(TQ, StringRef, SourceLocation)> Handle) {
440 forEachCVRUQualifier(Handle);
441 // FIXME: Add code below to iterate through the attributes and call Handle.
444 bool DeclSpec::hasTagDefinition() const {
445 if (!TypeSpecOwned)
446 return false;
447 return cast<TagDecl>(getRepAsDecl())->isCompleteDefinition();
450 /// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
451 /// declaration specifier includes.
453 unsigned DeclSpec::getParsedSpecifiers() const {
454 unsigned Res = 0;
455 if (StorageClassSpec != SCS_unspecified ||
456 ThreadStorageClassSpec != TSCS_unspecified)
457 Res |= PQ_StorageClassSpecifier;
459 if (TypeQualifiers != TQ_unspecified)
460 Res |= PQ_TypeQualifier;
462 if (hasTypeSpecifier())
463 Res |= PQ_TypeSpecifier;
465 if (FS_inline_specified || FS_virtual_specified || hasExplicitSpecifier() ||
466 FS_noreturn_specified || FS_forceinline_specified)
467 Res |= PQ_FunctionSpecifier;
468 return Res;
471 template <class T> static bool BadSpecifier(T TNew, T TPrev,
472 const char *&PrevSpec,
473 unsigned &DiagID,
474 bool IsExtension = true) {
475 PrevSpec = DeclSpec::getSpecifierName(TPrev);
476 if (TNew != TPrev)
477 DiagID = diag::err_invalid_decl_spec_combination;
478 else
479 DiagID = IsExtension ? diag::ext_warn_duplicate_declspec :
480 diag::warn_duplicate_declspec;
481 return true;
484 const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
485 switch (S) {
486 case DeclSpec::SCS_unspecified: return "unspecified";
487 case DeclSpec::SCS_typedef: return "typedef";
488 case DeclSpec::SCS_extern: return "extern";
489 case DeclSpec::SCS_static: return "static";
490 case DeclSpec::SCS_auto: return "auto";
491 case DeclSpec::SCS_register: return "register";
492 case DeclSpec::SCS_private_extern: return "__private_extern__";
493 case DeclSpec::SCS_mutable: return "mutable";
495 llvm_unreachable("Unknown typespec!");
498 const char *DeclSpec::getSpecifierName(DeclSpec::TSCS S) {
499 switch (S) {
500 case DeclSpec::TSCS_unspecified: return "unspecified";
501 case DeclSpec::TSCS___thread: return "__thread";
502 case DeclSpec::TSCS_thread_local: return "thread_local";
503 case DeclSpec::TSCS__Thread_local: return "_Thread_local";
505 llvm_unreachable("Unknown typespec!");
508 const char *DeclSpec::getSpecifierName(TypeSpecifierWidth W) {
509 switch (W) {
510 case TypeSpecifierWidth::Unspecified:
511 return "unspecified";
512 case TypeSpecifierWidth::Short:
513 return "short";
514 case TypeSpecifierWidth::Long:
515 return "long";
516 case TypeSpecifierWidth::LongLong:
517 return "long long";
519 llvm_unreachable("Unknown typespec!");
522 const char *DeclSpec::getSpecifierName(TSC C) {
523 switch (C) {
524 case TSC_unspecified: return "unspecified";
525 case TSC_imaginary: return "imaginary";
526 case TSC_complex: return "complex";
528 llvm_unreachable("Unknown typespec!");
531 const char *DeclSpec::getSpecifierName(TypeSpecifierSign S) {
532 switch (S) {
533 case TypeSpecifierSign::Unspecified:
534 return "unspecified";
535 case TypeSpecifierSign::Signed:
536 return "signed";
537 case TypeSpecifierSign::Unsigned:
538 return "unsigned";
540 llvm_unreachable("Unknown typespec!");
543 const char *DeclSpec::getSpecifierName(DeclSpec::TST T,
544 const PrintingPolicy &Policy) {
545 switch (T) {
546 case DeclSpec::TST_unspecified: return "unspecified";
547 case DeclSpec::TST_void: return "void";
548 case DeclSpec::TST_char: return "char";
549 case DeclSpec::TST_wchar: return Policy.MSWChar ? "__wchar_t" : "wchar_t";
550 case DeclSpec::TST_char8: return "char8_t";
551 case DeclSpec::TST_char16: return "char16_t";
552 case DeclSpec::TST_char32: return "char32_t";
553 case DeclSpec::TST_int: return "int";
554 case DeclSpec::TST_int128: return "__int128";
555 case DeclSpec::TST_bitint: return "_BitInt";
556 case DeclSpec::TST_half: return "half";
557 case DeclSpec::TST_float: return "float";
558 case DeclSpec::TST_double: return "double";
559 case DeclSpec::TST_accum: return "_Accum";
560 case DeclSpec::TST_fract: return "_Fract";
561 case DeclSpec::TST_float16: return "_Float16";
562 case DeclSpec::TST_float128: return "__float128";
563 case DeclSpec::TST_ibm128: return "__ibm128";
564 case DeclSpec::TST_bool: return Policy.Bool ? "bool" : "_Bool";
565 case DeclSpec::TST_decimal32: return "_Decimal32";
566 case DeclSpec::TST_decimal64: return "_Decimal64";
567 case DeclSpec::TST_decimal128: return "_Decimal128";
568 case DeclSpec::TST_enum: return "enum";
569 case DeclSpec::TST_class: return "class";
570 case DeclSpec::TST_union: return "union";
571 case DeclSpec::TST_struct: return "struct";
572 case DeclSpec::TST_interface: return "__interface";
573 case DeclSpec::TST_typename: return "type-name";
574 case DeclSpec::TST_typeofType:
575 case DeclSpec::TST_typeofExpr: return "typeof";
576 case DeclSpec::TST_auto: return "auto";
577 case DeclSpec::TST_auto_type: return "__auto_type";
578 case DeclSpec::TST_decltype: return "(decltype)";
579 case DeclSpec::TST_decltype_auto: return "decltype(auto)";
580 #define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) \
581 case DeclSpec::TST_##Trait: \
582 return "__" #Trait;
583 #include "clang/Basic/TransformTypeTraits.def"
584 case DeclSpec::TST_unknown_anytype: return "__unknown_anytype";
585 case DeclSpec::TST_atomic: return "_Atomic";
586 case DeclSpec::TST_BFloat16: return "__bf16";
587 #define GENERIC_IMAGE_TYPE(ImgType, Id) \
588 case DeclSpec::TST_##ImgType##_t: \
589 return #ImgType "_t";
590 #include "clang/Basic/OpenCLImageTypes.def"
591 case DeclSpec::TST_error: return "(error)";
593 llvm_unreachable("Unknown typespec!");
596 const char *DeclSpec::getSpecifierName(ConstexprSpecKind C) {
597 switch (C) {
598 case ConstexprSpecKind::Unspecified:
599 return "unspecified";
600 case ConstexprSpecKind::Constexpr:
601 return "constexpr";
602 case ConstexprSpecKind::Consteval:
603 return "consteval";
604 case ConstexprSpecKind::Constinit:
605 return "constinit";
607 llvm_unreachable("Unknown ConstexprSpecKind");
610 const char *DeclSpec::getSpecifierName(TQ T) {
611 switch (T) {
612 case DeclSpec::TQ_unspecified: return "unspecified";
613 case DeclSpec::TQ_const: return "const";
614 case DeclSpec::TQ_restrict: return "restrict";
615 case DeclSpec::TQ_volatile: return "volatile";
616 case DeclSpec::TQ_atomic: return "_Atomic";
617 case DeclSpec::TQ_unaligned: return "__unaligned";
619 llvm_unreachable("Unknown typespec!");
622 bool DeclSpec::SetStorageClassSpec(Sema &S, SCS SC, SourceLocation Loc,
623 const char *&PrevSpec,
624 unsigned &DiagID,
625 const PrintingPolicy &Policy) {
626 // OpenCL v1.1 s6.8g: "The extern, static, auto and register storage-class
627 // specifiers are not supported.
628 // It seems sensible to prohibit private_extern too
629 // The cl_clang_storage_class_specifiers extension enables support for
630 // these storage-class specifiers.
631 // OpenCL v1.2 s6.8 changes this to "The auto and register storage-class
632 // specifiers are not supported."
633 if (S.getLangOpts().OpenCL &&
634 !S.getOpenCLOptions().isAvailableOption(
635 "cl_clang_storage_class_specifiers", S.getLangOpts())) {
636 switch (SC) {
637 case SCS_extern:
638 case SCS_private_extern:
639 case SCS_static:
640 if (S.getLangOpts().getOpenCLCompatibleVersion() < 120) {
641 DiagID = diag::err_opencl_unknown_type_specifier;
642 PrevSpec = getSpecifierName(SC);
643 return true;
645 break;
646 case SCS_auto:
647 case SCS_register:
648 DiagID = diag::err_opencl_unknown_type_specifier;
649 PrevSpec = getSpecifierName(SC);
650 return true;
651 default:
652 break;
656 if (StorageClassSpec != SCS_unspecified) {
657 // Maybe this is an attempt to use C++11 'auto' outside of C++11 mode.
658 bool isInvalid = true;
659 if (TypeSpecType == TST_unspecified && S.getLangOpts().CPlusPlus) {
660 if (SC == SCS_auto)
661 return SetTypeSpecType(TST_auto, Loc, PrevSpec, DiagID, Policy);
662 if (StorageClassSpec == SCS_auto) {
663 isInvalid = SetTypeSpecType(TST_auto, StorageClassSpecLoc,
664 PrevSpec, DiagID, Policy);
665 assert(!isInvalid && "auto SCS -> TST recovery failed");
669 // Changing storage class is allowed only if the previous one
670 // was the 'extern' that is part of a linkage specification and
671 // the new storage class is 'typedef'.
672 if (isInvalid &&
673 !(SCS_extern_in_linkage_spec &&
674 StorageClassSpec == SCS_extern &&
675 SC == SCS_typedef))
676 return BadSpecifier(SC, (SCS)StorageClassSpec, PrevSpec, DiagID);
678 StorageClassSpec = SC;
679 StorageClassSpecLoc = Loc;
680 assert((unsigned)SC == StorageClassSpec && "SCS constants overflow bitfield");
681 return false;
684 bool DeclSpec::SetStorageClassSpecThread(TSCS TSC, SourceLocation Loc,
685 const char *&PrevSpec,
686 unsigned &DiagID) {
687 if (ThreadStorageClassSpec != TSCS_unspecified)
688 return BadSpecifier(TSC, (TSCS)ThreadStorageClassSpec, PrevSpec, DiagID);
690 ThreadStorageClassSpec = TSC;
691 ThreadStorageClassSpecLoc = Loc;
692 return false;
695 /// These methods set the specified attribute of the DeclSpec, but return true
696 /// and ignore the request if invalid (e.g. "extern" then "auto" is
697 /// specified).
698 bool DeclSpec::SetTypeSpecWidth(TypeSpecifierWidth W, SourceLocation Loc,
699 const char *&PrevSpec, unsigned &DiagID,
700 const PrintingPolicy &Policy) {
701 // Overwrite TSWRange.Begin only if TypeSpecWidth was unspecified, so that
702 // for 'long long' we will keep the source location of the first 'long'.
703 if (getTypeSpecWidth() == TypeSpecifierWidth::Unspecified)
704 TSWRange.setBegin(Loc);
705 // Allow turning long -> long long.
706 else if (W != TypeSpecifierWidth::LongLong ||
707 getTypeSpecWidth() != TypeSpecifierWidth::Long)
708 return BadSpecifier(W, getTypeSpecWidth(), PrevSpec, DiagID);
709 TypeSpecWidth = static_cast<unsigned>(W);
710 // Remember location of the last 'long'
711 TSWRange.setEnd(Loc);
712 return false;
715 bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
716 const char *&PrevSpec,
717 unsigned &DiagID) {
718 if (TypeSpecComplex != TSC_unspecified)
719 return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID);
720 TypeSpecComplex = C;
721 TSCLoc = Loc;
722 return false;
725 bool DeclSpec::SetTypeSpecSign(TypeSpecifierSign S, SourceLocation Loc,
726 const char *&PrevSpec, unsigned &DiagID) {
727 if (getTypeSpecSign() != TypeSpecifierSign::Unspecified)
728 return BadSpecifier(S, getTypeSpecSign(), PrevSpec, DiagID);
729 TypeSpecSign = static_cast<unsigned>(S);
730 TSSLoc = Loc;
731 return false;
734 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
735 const char *&PrevSpec,
736 unsigned &DiagID,
737 ParsedType Rep,
738 const PrintingPolicy &Policy) {
739 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep, Policy);
742 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
743 SourceLocation TagNameLoc,
744 const char *&PrevSpec,
745 unsigned &DiagID,
746 ParsedType Rep,
747 const PrintingPolicy &Policy) {
748 assert(isTypeRep(T) && "T does not store a type");
749 assert(Rep && "no type provided!");
750 if (TypeSpecType == TST_error)
751 return false;
752 if (TypeSpecType != TST_unspecified) {
753 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
754 DiagID = diag::err_invalid_decl_spec_combination;
755 return true;
757 TypeSpecType = T;
758 TypeRep = Rep;
759 TSTLoc = TagKwLoc;
760 TSTNameLoc = TagNameLoc;
761 TypeSpecOwned = false;
762 return false;
765 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
766 const char *&PrevSpec,
767 unsigned &DiagID,
768 Expr *Rep,
769 const PrintingPolicy &Policy) {
770 assert(isExprRep(T) && "T does not store an expr");
771 assert(Rep && "no expression provided!");
772 if (TypeSpecType == TST_error)
773 return false;
774 if (TypeSpecType != TST_unspecified) {
775 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
776 DiagID = diag::err_invalid_decl_spec_combination;
777 return true;
779 TypeSpecType = T;
780 ExprRep = Rep;
781 TSTLoc = Loc;
782 TSTNameLoc = Loc;
783 TypeSpecOwned = false;
784 return false;
787 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
788 const char *&PrevSpec,
789 unsigned &DiagID,
790 Decl *Rep, bool Owned,
791 const PrintingPolicy &Policy) {
792 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep, Owned, Policy);
795 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
796 SourceLocation TagNameLoc,
797 const char *&PrevSpec,
798 unsigned &DiagID,
799 Decl *Rep, bool Owned,
800 const PrintingPolicy &Policy) {
801 assert(isDeclRep(T) && "T does not store a decl");
802 // Unlike the other cases, we don't assert that we actually get a decl.
804 if (TypeSpecType == TST_error)
805 return false;
806 if (TypeSpecType != TST_unspecified) {
807 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
808 DiagID = diag::err_invalid_decl_spec_combination;
809 return true;
811 TypeSpecType = T;
812 DeclRep = Rep;
813 TSTLoc = TagKwLoc;
814 TSTNameLoc = TagNameLoc;
815 TypeSpecOwned = Owned && Rep != nullptr;
816 return false;
819 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec,
820 unsigned &DiagID, TemplateIdAnnotation *Rep,
821 const PrintingPolicy &Policy) {
822 assert(T == TST_auto || T == TST_decltype_auto);
823 ConstrainedAuto = true;
824 TemplateIdRep = Rep;
825 return SetTypeSpecType(T, Loc, PrevSpec, DiagID, Policy);
828 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
829 const char *&PrevSpec,
830 unsigned &DiagID,
831 const PrintingPolicy &Policy) {
832 assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) &&
833 "rep required for these type-spec kinds!");
834 if (TypeSpecType == TST_error)
835 return false;
836 if (TypeSpecType != TST_unspecified) {
837 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
838 DiagID = diag::err_invalid_decl_spec_combination;
839 return true;
841 TSTLoc = Loc;
842 TSTNameLoc = Loc;
843 if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) {
844 TypeAltiVecBool = true;
845 return false;
847 TypeSpecType = T;
848 TypeSpecOwned = false;
849 return false;
852 bool DeclSpec::SetTypeSpecSat(SourceLocation Loc, const char *&PrevSpec,
853 unsigned &DiagID) {
854 // Cannot set twice
855 if (TypeSpecSat) {
856 DiagID = diag::warn_duplicate_declspec;
857 PrevSpec = "_Sat";
858 return true;
860 TypeSpecSat = true;
861 TSSatLoc = Loc;
862 return false;
865 bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
866 const char *&PrevSpec, unsigned &DiagID,
867 const PrintingPolicy &Policy) {
868 if (TypeSpecType == TST_error)
869 return false;
870 if (TypeSpecType != TST_unspecified) {
871 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
872 DiagID = diag::err_invalid_vector_decl_spec_combination;
873 return true;
875 TypeAltiVecVector = isAltiVecVector;
876 AltiVecLoc = Loc;
877 return false;
880 bool DeclSpec::SetTypePipe(bool isPipe, SourceLocation Loc,
881 const char *&PrevSpec, unsigned &DiagID,
882 const PrintingPolicy &Policy) {
883 if (TypeSpecType == TST_error)
884 return false;
885 if (TypeSpecType != TST_unspecified) {
886 PrevSpec = DeclSpec::getSpecifierName((TST)TypeSpecType, Policy);
887 DiagID = diag::err_invalid_decl_spec_combination;
888 return true;
891 if (isPipe) {
892 TypeSpecPipe = static_cast<unsigned>(TypeSpecifiersPipe::Pipe);
894 return false;
897 bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
898 const char *&PrevSpec, unsigned &DiagID,
899 const PrintingPolicy &Policy) {
900 if (TypeSpecType == TST_error)
901 return false;
902 if (!TypeAltiVecVector || TypeAltiVecPixel ||
903 (TypeSpecType != TST_unspecified)) {
904 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
905 DiagID = diag::err_invalid_pixel_decl_spec_combination;
906 return true;
908 TypeAltiVecPixel = isAltiVecPixel;
909 TSTLoc = Loc;
910 TSTNameLoc = Loc;
911 return false;
914 bool DeclSpec::SetTypeAltiVecBool(bool isAltiVecBool, SourceLocation Loc,
915 const char *&PrevSpec, unsigned &DiagID,
916 const PrintingPolicy &Policy) {
917 if (TypeSpecType == TST_error)
918 return false;
919 if (!TypeAltiVecVector || TypeAltiVecBool ||
920 (TypeSpecType != TST_unspecified)) {
921 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
922 DiagID = diag::err_invalid_vector_bool_decl_spec;
923 return true;
925 TypeAltiVecBool = isAltiVecBool;
926 TSTLoc = Loc;
927 TSTNameLoc = Loc;
928 return false;
931 bool DeclSpec::SetTypeSpecError() {
932 TypeSpecType = TST_error;
933 TypeSpecOwned = false;
934 TSTLoc = SourceLocation();
935 TSTNameLoc = SourceLocation();
936 return false;
939 bool DeclSpec::SetBitIntType(SourceLocation KWLoc, Expr *BitsExpr,
940 const char *&PrevSpec, unsigned &DiagID,
941 const PrintingPolicy &Policy) {
942 assert(BitsExpr && "no expression provided!");
943 if (TypeSpecType == TST_error)
944 return false;
946 if (TypeSpecType != TST_unspecified) {
947 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
948 DiagID = diag::err_invalid_decl_spec_combination;
949 return true;
952 TypeSpecType = TST_bitint;
953 ExprRep = BitsExpr;
954 TSTLoc = KWLoc;
955 TSTNameLoc = KWLoc;
956 TypeSpecOwned = false;
957 return false;
960 bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
961 unsigned &DiagID, const LangOptions &Lang) {
962 // Duplicates are permitted in C99 onwards, but are not permitted in C89 or
963 // C++. However, since this is likely not what the user intended, we will
964 // always warn. We do not need to set the qualifier's location since we
965 // already have it.
966 if (TypeQualifiers & T) {
967 bool IsExtension = true;
968 if (Lang.C99)
969 IsExtension = false;
970 return BadSpecifier(T, T, PrevSpec, DiagID, IsExtension);
973 return SetTypeQual(T, Loc);
976 bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc) {
977 TypeQualifiers |= T;
979 switch (T) {
980 case TQ_unspecified: break;
981 case TQ_const: TQ_constLoc = Loc; return false;
982 case TQ_restrict: TQ_restrictLoc = Loc; return false;
983 case TQ_volatile: TQ_volatileLoc = Loc; return false;
984 case TQ_unaligned: TQ_unalignedLoc = Loc; return false;
985 case TQ_atomic: TQ_atomicLoc = Loc; return false;
988 llvm_unreachable("Unknown type qualifier!");
991 bool DeclSpec::setFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
992 unsigned &DiagID) {
993 // 'inline inline' is ok. However, since this is likely not what the user
994 // intended, we will always warn, similar to duplicates of type qualifiers.
995 if (FS_inline_specified) {
996 DiagID = diag::warn_duplicate_declspec;
997 PrevSpec = "inline";
998 return true;
1000 FS_inline_specified = true;
1001 FS_inlineLoc = Loc;
1002 return false;
1005 bool DeclSpec::setFunctionSpecForceInline(SourceLocation Loc, const char *&PrevSpec,
1006 unsigned &DiagID) {
1007 if (FS_forceinline_specified) {
1008 DiagID = diag::warn_duplicate_declspec;
1009 PrevSpec = "__forceinline";
1010 return true;
1012 FS_forceinline_specified = true;
1013 FS_forceinlineLoc = Loc;
1014 return false;
1017 bool DeclSpec::setFunctionSpecVirtual(SourceLocation Loc,
1018 const char *&PrevSpec,
1019 unsigned &DiagID) {
1020 // 'virtual virtual' is ok, but warn as this is likely not what the user
1021 // intended.
1022 if (FS_virtual_specified) {
1023 DiagID = diag::warn_duplicate_declspec;
1024 PrevSpec = "virtual";
1025 return true;
1027 FS_virtual_specified = true;
1028 FS_virtualLoc = Loc;
1029 return false;
1032 bool DeclSpec::setFunctionSpecExplicit(SourceLocation Loc,
1033 const char *&PrevSpec, unsigned &DiagID,
1034 ExplicitSpecifier ExplicitSpec,
1035 SourceLocation CloseParenLoc) {
1036 // 'explicit explicit' is ok, but warn as this is likely not what the user
1037 // intended.
1038 if (hasExplicitSpecifier()) {
1039 DiagID = (ExplicitSpec.getExpr() || FS_explicit_specifier.getExpr())
1040 ? diag::err_duplicate_declspec
1041 : diag::ext_warn_duplicate_declspec;
1042 PrevSpec = "explicit";
1043 return true;
1045 FS_explicit_specifier = ExplicitSpec;
1046 FS_explicitLoc = Loc;
1047 FS_explicitCloseParenLoc = CloseParenLoc;
1048 return false;
1051 bool DeclSpec::setFunctionSpecNoreturn(SourceLocation Loc,
1052 const char *&PrevSpec,
1053 unsigned &DiagID) {
1054 // '_Noreturn _Noreturn' is ok, but warn as this is likely not what the user
1055 // intended.
1056 if (FS_noreturn_specified) {
1057 DiagID = diag::warn_duplicate_declspec;
1058 PrevSpec = "_Noreturn";
1059 return true;
1061 FS_noreturn_specified = true;
1062 FS_noreturnLoc = Loc;
1063 return false;
1066 bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
1067 unsigned &DiagID) {
1068 if (Friend_specified) {
1069 PrevSpec = "friend";
1070 // Keep the later location, so that we can later diagnose ill-formed
1071 // declarations like 'friend class X friend;'. Per [class.friend]p3,
1072 // 'friend' must be the first token in a friend declaration that is
1073 // not a function declaration.
1074 FriendLoc = Loc;
1075 DiagID = diag::warn_duplicate_declspec;
1076 return true;
1079 Friend_specified = true;
1080 FriendLoc = Loc;
1081 return false;
1084 bool DeclSpec::setModulePrivateSpec(SourceLocation Loc, const char *&PrevSpec,
1085 unsigned &DiagID) {
1086 if (isModulePrivateSpecified()) {
1087 PrevSpec = "__module_private__";
1088 DiagID = diag::ext_warn_duplicate_declspec;
1089 return true;
1092 ModulePrivateLoc = Loc;
1093 return false;
1096 bool DeclSpec::SetConstexprSpec(ConstexprSpecKind ConstexprKind,
1097 SourceLocation Loc, const char *&PrevSpec,
1098 unsigned &DiagID) {
1099 if (getConstexprSpecifier() != ConstexprSpecKind::Unspecified)
1100 return BadSpecifier(ConstexprKind, getConstexprSpecifier(), PrevSpec,
1101 DiagID);
1102 ConstexprSpecifier = static_cast<unsigned>(ConstexprKind);
1103 ConstexprLoc = Loc;
1104 return false;
1107 void DeclSpec::SaveWrittenBuiltinSpecs() {
1108 writtenBS.Sign = static_cast<int>(getTypeSpecSign());
1109 writtenBS.Width = static_cast<int>(getTypeSpecWidth());
1110 writtenBS.Type = getTypeSpecType();
1111 // Search the list of attributes for the presence of a mode attribute.
1112 writtenBS.ModeAttr = getAttributes().hasAttribute(ParsedAttr::AT_Mode);
1115 /// Finish - This does final analysis of the declspec, rejecting things like
1116 /// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
1117 /// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
1118 /// DeclSpec is guaranteed self-consistent, even if an error occurred.
1119 void DeclSpec::Finish(Sema &S, const PrintingPolicy &Policy) {
1120 // Before possibly changing their values, save specs as written.
1121 SaveWrittenBuiltinSpecs();
1123 // Check the type specifier components first. No checking for an invalid
1124 // type.
1125 if (TypeSpecType == TST_error)
1126 return;
1128 // If decltype(auto) is used, no other type specifiers are permitted.
1129 if (TypeSpecType == TST_decltype_auto &&
1130 (getTypeSpecWidth() != TypeSpecifierWidth::Unspecified ||
1131 TypeSpecComplex != TSC_unspecified ||
1132 getTypeSpecSign() != TypeSpecifierSign::Unspecified ||
1133 TypeAltiVecVector || TypeAltiVecPixel || TypeAltiVecBool ||
1134 TypeQualifiers)) {
1135 const unsigned NumLocs = 9;
1136 SourceLocation ExtraLocs[NumLocs] = {
1137 TSWRange.getBegin(), TSCLoc, TSSLoc,
1138 AltiVecLoc, TQ_constLoc, TQ_restrictLoc,
1139 TQ_volatileLoc, TQ_atomicLoc, TQ_unalignedLoc};
1140 FixItHint Hints[NumLocs];
1141 SourceLocation FirstLoc;
1142 for (unsigned I = 0; I != NumLocs; ++I) {
1143 if (ExtraLocs[I].isValid()) {
1144 if (FirstLoc.isInvalid() ||
1145 S.getSourceManager().isBeforeInTranslationUnit(ExtraLocs[I],
1146 FirstLoc))
1147 FirstLoc = ExtraLocs[I];
1148 Hints[I] = FixItHint::CreateRemoval(ExtraLocs[I]);
1151 TypeSpecWidth = static_cast<unsigned>(TypeSpecifierWidth::Unspecified);
1152 TypeSpecComplex = TSC_unspecified;
1153 TypeSpecSign = static_cast<unsigned>(TypeSpecifierSign::Unspecified);
1154 TypeAltiVecVector = TypeAltiVecPixel = TypeAltiVecBool = false;
1155 TypeQualifiers = 0;
1156 S.Diag(TSTLoc, diag::err_decltype_auto_cannot_be_combined)
1157 << Hints[0] << Hints[1] << Hints[2] << Hints[3]
1158 << Hints[4] << Hints[5] << Hints[6] << Hints[7];
1161 // Validate and finalize AltiVec vector declspec.
1162 if (TypeAltiVecVector) {
1163 // No vector long long without VSX (or ZVector).
1164 if ((getTypeSpecWidth() == TypeSpecifierWidth::LongLong) &&
1165 !S.Context.getTargetInfo().hasFeature("vsx") &&
1166 !S.getLangOpts().ZVector)
1167 S.Diag(TSWRange.getBegin(), diag::err_invalid_vector_long_long_decl_spec);
1169 // No vector __int128 prior to Power8.
1170 if ((TypeSpecType == TST_int128) &&
1171 !S.Context.getTargetInfo().hasFeature("power8-vector"))
1172 S.Diag(TSTLoc, diag::err_invalid_vector_int128_decl_spec);
1174 if (TypeAltiVecBool) {
1175 // Sign specifiers are not allowed with vector bool. (PIM 2.1)
1176 if (getTypeSpecSign() != TypeSpecifierSign::Unspecified) {
1177 S.Diag(TSSLoc, diag::err_invalid_vector_bool_decl_spec)
1178 << getSpecifierName(getTypeSpecSign());
1180 // Only char/int are valid with vector bool prior to Power10.
1181 // Power10 adds instructions that produce vector bool data
1182 // for quadwords as well so allow vector bool __int128.
1183 if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) &&
1184 (TypeSpecType != TST_int) && (TypeSpecType != TST_int128)) ||
1185 TypeAltiVecPixel) {
1186 S.Diag(TSTLoc, diag::err_invalid_vector_bool_decl_spec)
1187 << (TypeAltiVecPixel ? "__pixel" :
1188 getSpecifierName((TST)TypeSpecType, Policy));
1190 // vector bool __int128 requires Power10.
1191 if ((TypeSpecType == TST_int128) &&
1192 (!S.Context.getTargetInfo().hasFeature("power10-vector")))
1193 S.Diag(TSTLoc, diag::err_invalid_vector_bool_int128_decl_spec);
1195 // Only 'short' and 'long long' are valid with vector bool. (PIM 2.1)
1196 if ((getTypeSpecWidth() != TypeSpecifierWidth::Unspecified) &&
1197 (getTypeSpecWidth() != TypeSpecifierWidth::Short) &&
1198 (getTypeSpecWidth() != TypeSpecifierWidth::LongLong))
1199 S.Diag(TSWRange.getBegin(), diag::err_invalid_vector_bool_decl_spec)
1200 << getSpecifierName(getTypeSpecWidth());
1202 // Elements of vector bool are interpreted as unsigned. (PIM 2.1)
1203 if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) ||
1204 (TypeSpecType == TST_int128) ||
1205 (getTypeSpecWidth() != TypeSpecifierWidth::Unspecified))
1206 TypeSpecSign = static_cast<unsigned>(TypeSpecifierSign::Unsigned);
1207 } else if (TypeSpecType == TST_double) {
1208 // vector long double and vector long long double are never allowed.
1209 // vector double is OK for Power7 and later, and ZVector.
1210 if (getTypeSpecWidth() == TypeSpecifierWidth::Long ||
1211 getTypeSpecWidth() == TypeSpecifierWidth::LongLong)
1212 S.Diag(TSWRange.getBegin(),
1213 diag::err_invalid_vector_long_double_decl_spec);
1214 else if (!S.Context.getTargetInfo().hasFeature("vsx") &&
1215 !S.getLangOpts().ZVector)
1216 S.Diag(TSTLoc, diag::err_invalid_vector_double_decl_spec);
1217 } else if (TypeSpecType == TST_float) {
1218 // vector float is unsupported for ZVector unless we have the
1219 // vector-enhancements facility 1 (ISA revision 12).
1220 if (S.getLangOpts().ZVector &&
1221 !S.Context.getTargetInfo().hasFeature("arch12"))
1222 S.Diag(TSTLoc, diag::err_invalid_vector_float_decl_spec);
1223 } else if (getTypeSpecWidth() == TypeSpecifierWidth::Long) {
1224 // Vector long is unsupported for ZVector, or without VSX, and deprecated
1225 // for AltiVec.
1226 // It has also been historically deprecated on AIX (as an alias for
1227 // "vector int" in both 32-bit and 64-bit modes). It was then made
1228 // unsupported in the Clang-based XL compiler since the deprecated type
1229 // has a number of conflicting semantics and continuing to support it
1230 // is a disservice to users.
1231 if (S.getLangOpts().ZVector ||
1232 !S.Context.getTargetInfo().hasFeature("vsx") ||
1233 S.Context.getTargetInfo().getTriple().isOSAIX())
1234 S.Diag(TSWRange.getBegin(), diag::err_invalid_vector_long_decl_spec);
1235 else
1236 S.Diag(TSWRange.getBegin(),
1237 diag::warn_vector_long_decl_spec_combination)
1238 << getSpecifierName((TST)TypeSpecType, Policy);
1241 if (TypeAltiVecPixel) {
1242 //TODO: perform validation
1243 TypeSpecType = TST_int;
1244 TypeSpecSign = static_cast<unsigned>(TypeSpecifierSign::Unsigned);
1245 TypeSpecWidth = static_cast<unsigned>(TypeSpecifierWidth::Short);
1246 TypeSpecOwned = false;
1250 bool IsFixedPointType =
1251 TypeSpecType == TST_accum || TypeSpecType == TST_fract;
1253 // signed/unsigned are only valid with int/char/wchar_t/_Accum.
1254 if (getTypeSpecSign() != TypeSpecifierSign::Unspecified) {
1255 if (TypeSpecType == TST_unspecified)
1256 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
1257 else if (TypeSpecType != TST_int && TypeSpecType != TST_int128 &&
1258 TypeSpecType != TST_char && TypeSpecType != TST_wchar &&
1259 !IsFixedPointType && TypeSpecType != TST_bitint) {
1260 S.Diag(TSSLoc, diag::err_invalid_sign_spec)
1261 << getSpecifierName((TST)TypeSpecType, Policy);
1262 // signed double -> double.
1263 TypeSpecSign = static_cast<unsigned>(TypeSpecifierSign::Unspecified);
1267 // Validate the width of the type.
1268 switch (getTypeSpecWidth()) {
1269 case TypeSpecifierWidth::Unspecified:
1270 break;
1271 case TypeSpecifierWidth::Short: // short int
1272 case TypeSpecifierWidth::LongLong: // long long int
1273 if (TypeSpecType == TST_unspecified)
1274 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
1275 else if (!(TypeSpecType == TST_int ||
1276 (IsFixedPointType &&
1277 getTypeSpecWidth() != TypeSpecifierWidth::LongLong))) {
1278 S.Diag(TSWRange.getBegin(), diag::err_invalid_width_spec)
1279 << (int)TypeSpecWidth << getSpecifierName((TST)TypeSpecType, Policy);
1280 TypeSpecType = TST_int;
1281 TypeSpecSat = false;
1282 TypeSpecOwned = false;
1284 break;
1285 case TypeSpecifierWidth::Long: // long double, long int
1286 if (TypeSpecType == TST_unspecified)
1287 TypeSpecType = TST_int; // long -> long int.
1288 else if (TypeSpecType != TST_int && TypeSpecType != TST_double &&
1289 !IsFixedPointType) {
1290 S.Diag(TSWRange.getBegin(), diag::err_invalid_width_spec)
1291 << (int)TypeSpecWidth << getSpecifierName((TST)TypeSpecType, Policy);
1292 TypeSpecType = TST_int;
1293 TypeSpecSat = false;
1294 TypeSpecOwned = false;
1296 break;
1299 // TODO: if the implementation does not implement _Complex or _Imaginary,
1300 // disallow their use. Need information about the backend.
1301 if (TypeSpecComplex != TSC_unspecified) {
1302 if (TypeSpecType == TST_unspecified) {
1303 S.Diag(TSCLoc, diag::ext_plain_complex)
1304 << FixItHint::CreateInsertion(
1305 S.getLocForEndOfToken(getTypeSpecComplexLoc()),
1306 " double");
1307 TypeSpecType = TST_double; // _Complex -> _Complex double.
1308 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char ||
1309 TypeSpecType == TST_bitint) {
1310 // Note that this intentionally doesn't include _Complex _Bool.
1311 if (!S.getLangOpts().CPlusPlus)
1312 S.Diag(TSTLoc, diag::ext_integer_complex);
1313 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double &&
1314 TypeSpecType != TST_float128 && TypeSpecType != TST_float16 &&
1315 TypeSpecType != TST_ibm128) {
1316 // FIXME: __fp16?
1317 S.Diag(TSCLoc, diag::err_invalid_complex_spec)
1318 << getSpecifierName((TST)TypeSpecType, Policy);
1319 TypeSpecComplex = TSC_unspecified;
1323 // C11 6.7.1/3, C++11 [dcl.stc]p1, GNU TLS: __thread, thread_local and
1324 // _Thread_local can only appear with the 'static' and 'extern' storage class
1325 // specifiers. We also allow __private_extern__ as an extension.
1326 if (ThreadStorageClassSpec != TSCS_unspecified) {
1327 switch (StorageClassSpec) {
1328 case SCS_unspecified:
1329 case SCS_extern:
1330 case SCS_private_extern:
1331 case SCS_static:
1332 break;
1333 default:
1334 if (S.getSourceManager().isBeforeInTranslationUnit(
1335 getThreadStorageClassSpecLoc(), getStorageClassSpecLoc()))
1336 S.Diag(getStorageClassSpecLoc(),
1337 diag::err_invalid_decl_spec_combination)
1338 << DeclSpec::getSpecifierName(getThreadStorageClassSpec())
1339 << SourceRange(getThreadStorageClassSpecLoc());
1340 else
1341 S.Diag(getThreadStorageClassSpecLoc(),
1342 diag::err_invalid_decl_spec_combination)
1343 << DeclSpec::getSpecifierName(getStorageClassSpec())
1344 << SourceRange(getStorageClassSpecLoc());
1345 // Discard the thread storage class specifier to recover.
1346 ThreadStorageClassSpec = TSCS_unspecified;
1347 ThreadStorageClassSpecLoc = SourceLocation();
1351 // If no type specifier was provided and we're parsing a language where
1352 // the type specifier is not optional, but we got 'auto' as a storage
1353 // class specifier, then assume this is an attempt to use C++0x's 'auto'
1354 // type specifier.
1355 if (S.getLangOpts().CPlusPlus &&
1356 TypeSpecType == TST_unspecified && StorageClassSpec == SCS_auto) {
1357 TypeSpecType = TST_auto;
1358 StorageClassSpec = SCS_unspecified;
1359 TSTLoc = TSTNameLoc = StorageClassSpecLoc;
1360 StorageClassSpecLoc = SourceLocation();
1362 // Diagnose if we've recovered from an ill-formed 'auto' storage class
1363 // specifier in a pre-C++11 dialect of C++.
1364 if (!S.getLangOpts().CPlusPlus11 && TypeSpecType == TST_auto)
1365 S.Diag(TSTLoc, diag::ext_auto_type_specifier);
1366 if (S.getLangOpts().CPlusPlus && !S.getLangOpts().CPlusPlus11 &&
1367 StorageClassSpec == SCS_auto)
1368 S.Diag(StorageClassSpecLoc, diag::warn_auto_storage_class)
1369 << FixItHint::CreateRemoval(StorageClassSpecLoc);
1370 if (TypeSpecType == TST_char8)
1371 S.Diag(TSTLoc, diag::warn_cxx17_compat_unicode_type);
1372 else if (TypeSpecType == TST_char16 || TypeSpecType == TST_char32)
1373 S.Diag(TSTLoc, diag::warn_cxx98_compat_unicode_type)
1374 << (TypeSpecType == TST_char16 ? "char16_t" : "char32_t");
1375 if (getConstexprSpecifier() == ConstexprSpecKind::Constexpr)
1376 S.Diag(ConstexprLoc, diag::warn_cxx98_compat_constexpr);
1377 else if (getConstexprSpecifier() == ConstexprSpecKind::Consteval)
1378 S.Diag(ConstexprLoc, diag::warn_cxx20_compat_consteval);
1379 else if (getConstexprSpecifier() == ConstexprSpecKind::Constinit)
1380 S.Diag(ConstexprLoc, diag::warn_cxx20_compat_constinit);
1381 // C++ [class.friend]p6:
1382 // No storage-class-specifier shall appear in the decl-specifier-seq
1383 // of a friend declaration.
1384 if (isFriendSpecified() &&
1385 (getStorageClassSpec() || getThreadStorageClassSpec())) {
1386 SmallString<32> SpecName;
1387 SourceLocation SCLoc;
1388 FixItHint StorageHint, ThreadHint;
1390 if (DeclSpec::SCS SC = getStorageClassSpec()) {
1391 SpecName = getSpecifierName(SC);
1392 SCLoc = getStorageClassSpecLoc();
1393 StorageHint = FixItHint::CreateRemoval(SCLoc);
1396 if (DeclSpec::TSCS TSC = getThreadStorageClassSpec()) {
1397 if (!SpecName.empty()) SpecName += " ";
1398 SpecName += getSpecifierName(TSC);
1399 SCLoc = getThreadStorageClassSpecLoc();
1400 ThreadHint = FixItHint::CreateRemoval(SCLoc);
1403 S.Diag(SCLoc, diag::err_friend_decl_spec)
1404 << SpecName << StorageHint << ThreadHint;
1406 ClearStorageClassSpecs();
1409 // C++11 [dcl.fct.spec]p5:
1410 // The virtual specifier shall be used only in the initial
1411 // declaration of a non-static class member function;
1412 // C++11 [dcl.fct.spec]p6:
1413 // The explicit specifier shall be used only in the declaration of
1414 // a constructor or conversion function within its class
1415 // definition;
1416 if (isFriendSpecified() && (isVirtualSpecified() || hasExplicitSpecifier())) {
1417 StringRef Keyword;
1418 FixItHint Hint;
1419 SourceLocation SCLoc;
1421 if (isVirtualSpecified()) {
1422 Keyword = "virtual";
1423 SCLoc = getVirtualSpecLoc();
1424 Hint = FixItHint::CreateRemoval(SCLoc);
1425 } else {
1426 Keyword = "explicit";
1427 SCLoc = getExplicitSpecLoc();
1428 Hint = FixItHint::CreateRemoval(getExplicitSpecRange());
1431 S.Diag(SCLoc, diag::err_friend_decl_spec)
1432 << Keyword << Hint;
1434 FS_virtual_specified = false;
1435 FS_explicit_specifier = ExplicitSpecifier();
1436 FS_virtualLoc = FS_explicitLoc = SourceLocation();
1439 assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType));
1441 // Okay, now we can infer the real type.
1443 // TODO: return "auto function" and other bad things based on the real type.
1445 // 'data definition has no type or storage class'?
1448 bool DeclSpec::isMissingDeclaratorOk() {
1449 TST tst = getTypeSpecType();
1450 return isDeclRep(tst) && getRepAsDecl() != nullptr &&
1451 StorageClassSpec != DeclSpec::SCS_typedef;
1454 void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc,
1455 OverloadedOperatorKind Op,
1456 SourceLocation SymbolLocations[3]) {
1457 Kind = UnqualifiedIdKind::IK_OperatorFunctionId;
1458 StartLocation = OperatorLoc;
1459 EndLocation = OperatorLoc;
1460 new (&OperatorFunctionId) struct OFI;
1461 OperatorFunctionId.Operator = Op;
1462 for (unsigned I = 0; I != 3; ++I) {
1463 OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I];
1465 if (SymbolLocations[I].isValid())
1466 EndLocation = SymbolLocations[I];
1470 bool VirtSpecifiers::SetSpecifier(Specifier VS, SourceLocation Loc,
1471 const char *&PrevSpec) {
1472 if (!FirstLocation.isValid())
1473 FirstLocation = Loc;
1474 LastLocation = Loc;
1475 LastSpecifier = VS;
1477 if (Specifiers & VS) {
1478 PrevSpec = getSpecifierName(VS);
1479 return true;
1482 Specifiers |= VS;
1484 switch (VS) {
1485 default: llvm_unreachable("Unknown specifier!");
1486 case VS_Override: VS_overrideLoc = Loc; break;
1487 case VS_GNU_Final:
1488 case VS_Sealed:
1489 case VS_Final: VS_finalLoc = Loc; break;
1490 case VS_Abstract: VS_abstractLoc = Loc; break;
1493 return false;
1496 const char *VirtSpecifiers::getSpecifierName(Specifier VS) {
1497 switch (VS) {
1498 default: llvm_unreachable("Unknown specifier");
1499 case VS_Override: return "override";
1500 case VS_Final: return "final";
1501 case VS_GNU_Final: return "__final";
1502 case VS_Sealed: return "sealed";
1503 case VS_Abstract: return "abstract";