1 //===--- DeclSpec.cpp - Declaration Specifier Semantic Analysis -----------===//
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
7 //===----------------------------------------------------------------------===//
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"
26 using namespace clang
;
29 void UnqualifiedId::setTemplateId(TemplateIdAnnotation
*TemplateId
) {
30 assert(TemplateId
&& "NULL template-id annotation?");
31 assert(!TemplateId
->isInvalid() &&
32 "should not convert invalid template-ids to unqualified-ids");
34 Kind
= UnqualifiedIdKind::IK_TemplateId
;
35 this->TemplateId
= TemplateId
;
36 StartLocation
= TemplateId
->TemplateNameLoc
;
37 EndLocation
= TemplateId
->RAngleLoc
;
40 void UnqualifiedId::setConstructorTemplateId(TemplateIdAnnotation
*TemplateId
) {
41 assert(TemplateId
&& "NULL template-id annotation?");
42 assert(!TemplateId
->isInvalid() &&
43 "should not convert invalid template-ids to unqualified-ids");
45 Kind
= UnqualifiedIdKind::IK_ConstructorTemplateId
;
46 this->TemplateId
= TemplateId
;
47 StartLocation
= TemplateId
->TemplateNameLoc
;
48 EndLocation
= TemplateId
->RAngleLoc
;
51 void CXXScopeSpec::Extend(ASTContext
&Context
, SourceLocation TemplateKWLoc
,
52 TypeLoc TL
, SourceLocation ColonColonLoc
) {
53 Builder
.Extend(Context
, TemplateKWLoc
, TL
, ColonColonLoc
);
54 if (Range
.getBegin().isInvalid())
55 Range
.setBegin(TL
.getBeginLoc());
56 Range
.setEnd(ColonColonLoc
);
58 assert(Range
== Builder
.getSourceRange() &&
59 "NestedNameSpecifierLoc range computation incorrect");
62 void CXXScopeSpec::Extend(ASTContext
&Context
, IdentifierInfo
*Identifier
,
63 SourceLocation IdentifierLoc
,
64 SourceLocation ColonColonLoc
) {
65 Builder
.Extend(Context
, Identifier
, IdentifierLoc
, ColonColonLoc
);
67 if (Range
.getBegin().isInvalid())
68 Range
.setBegin(IdentifierLoc
);
69 Range
.setEnd(ColonColonLoc
);
71 assert(Range
== Builder
.getSourceRange() &&
72 "NestedNameSpecifierLoc range computation incorrect");
75 void CXXScopeSpec::Extend(ASTContext
&Context
, NamespaceDecl
*Namespace
,
76 SourceLocation NamespaceLoc
,
77 SourceLocation ColonColonLoc
) {
78 Builder
.Extend(Context
, Namespace
, NamespaceLoc
, ColonColonLoc
);
80 if (Range
.getBegin().isInvalid())
81 Range
.setBegin(NamespaceLoc
);
82 Range
.setEnd(ColonColonLoc
);
84 assert(Range
== Builder
.getSourceRange() &&
85 "NestedNameSpecifierLoc range computation incorrect");
88 void CXXScopeSpec::Extend(ASTContext
&Context
, NamespaceAliasDecl
*Alias
,
89 SourceLocation AliasLoc
,
90 SourceLocation ColonColonLoc
) {
91 Builder
.Extend(Context
, Alias
, AliasLoc
, ColonColonLoc
);
93 if (Range
.getBegin().isInvalid())
94 Range
.setBegin(AliasLoc
);
95 Range
.setEnd(ColonColonLoc
);
97 assert(Range
== Builder
.getSourceRange() &&
98 "NestedNameSpecifierLoc range computation incorrect");
101 void CXXScopeSpec::MakeGlobal(ASTContext
&Context
,
102 SourceLocation ColonColonLoc
) {
103 Builder
.MakeGlobal(Context
, ColonColonLoc
);
105 Range
= SourceRange(ColonColonLoc
);
107 assert(Range
== Builder
.getSourceRange() &&
108 "NestedNameSpecifierLoc range computation incorrect");
111 void CXXScopeSpec::MakeSuper(ASTContext
&Context
, CXXRecordDecl
*RD
,
112 SourceLocation SuperLoc
,
113 SourceLocation ColonColonLoc
) {
114 Builder
.MakeSuper(Context
, RD
, SuperLoc
, ColonColonLoc
);
116 Range
.setBegin(SuperLoc
);
117 Range
.setEnd(ColonColonLoc
);
119 assert(Range
== Builder
.getSourceRange() &&
120 "NestedNameSpecifierLoc range computation incorrect");
123 void CXXScopeSpec::MakeTrivial(ASTContext
&Context
,
124 NestedNameSpecifier
*Qualifier
, SourceRange R
) {
125 Builder
.MakeTrivial(Context
, Qualifier
, R
);
129 void CXXScopeSpec::Adopt(NestedNameSpecifierLoc Other
) {
131 Range
= SourceRange();
136 Range
= Other
.getSourceRange();
137 Builder
.Adopt(Other
);
138 assert(Range
== Builder
.getSourceRange() &&
139 "NestedNameSpecifierLoc range computation incorrect");
142 SourceLocation
CXXScopeSpec::getLastQualifierNameLoc() const {
143 if (!Builder
.getRepresentation())
144 return SourceLocation();
145 return Builder
.getTemporary().getLocalBeginLoc();
148 NestedNameSpecifierLoc
149 CXXScopeSpec::getWithLocInContext(ASTContext
&Context
) const {
150 if (!Builder
.getRepresentation())
151 return NestedNameSpecifierLoc();
153 return Builder
.getWithLocInContext(Context
);
156 /// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function.
157 /// "TheDeclarator" is the declarator that this will be added to.
158 DeclaratorChunk
DeclaratorChunk::getFunction(bool hasProto
,
160 SourceLocation LParenLoc
,
163 SourceLocation EllipsisLoc
,
164 SourceLocation RParenLoc
,
165 bool RefQualifierIsLvalueRef
,
166 SourceLocation RefQualifierLoc
,
167 SourceLocation MutableLoc
,
168 ExceptionSpecificationType
170 SourceRange ESpecRange
,
171 ParsedType
*Exceptions
,
172 SourceRange
*ExceptionRanges
,
173 unsigned NumExceptions
,
175 CachedTokens
*ExceptionSpecTokens
,
178 SourceLocation LocalRangeBegin
,
179 SourceLocation LocalRangeEnd
,
180 Declarator
&TheDeclarator
,
181 TypeResult TrailingReturnType
,
183 TrailingReturnTypeLoc
,
184 DeclSpec
*MethodQualifiers
) {
185 assert(!(MethodQualifiers
&& MethodQualifiers
->getTypeQualifiers() & DeclSpec::TQ_atomic
) &&
186 "function cannot have _Atomic qualifier");
190 I
.Loc
= LocalRangeBegin
;
191 I
.EndLoc
= LocalRangeEnd
;
192 new (&I
.Fun
) FunctionTypeInfo
;
193 I
.Fun
.hasPrototype
= hasProto
;
194 I
.Fun
.isVariadic
= EllipsisLoc
.isValid();
195 I
.Fun
.isAmbiguous
= isAmbiguous
;
196 I
.Fun
.LParenLoc
= LParenLoc
;
197 I
.Fun
.EllipsisLoc
= EllipsisLoc
;
198 I
.Fun
.RParenLoc
= RParenLoc
;
199 I
.Fun
.DeleteParams
= false;
200 I
.Fun
.NumParams
= NumParams
;
201 I
.Fun
.Params
= nullptr;
202 I
.Fun
.RefQualifierIsLValueRef
= RefQualifierIsLvalueRef
;
203 I
.Fun
.RefQualifierLoc
= RefQualifierLoc
;
204 I
.Fun
.MutableLoc
= MutableLoc
;
205 I
.Fun
.ExceptionSpecType
= ESpecType
;
206 I
.Fun
.ExceptionSpecLocBeg
= ESpecRange
.getBegin();
207 I
.Fun
.ExceptionSpecLocEnd
= ESpecRange
.getEnd();
208 I
.Fun
.NumExceptionsOrDecls
= 0;
209 I
.Fun
.Exceptions
= nullptr;
210 I
.Fun
.NoexceptExpr
= nullptr;
211 I
.Fun
.HasTrailingReturnType
= TrailingReturnType
.isUsable() ||
212 TrailingReturnType
.isInvalid();
213 I
.Fun
.TrailingReturnType
= TrailingReturnType
.get();
214 I
.Fun
.TrailingReturnTypeLoc
= TrailingReturnTypeLoc
;
215 I
.Fun
.MethodQualifiers
= nullptr;
216 I
.Fun
.QualAttrFactory
= nullptr;
218 if (MethodQualifiers
&& (MethodQualifiers
->getTypeQualifiers() ||
219 MethodQualifiers
->getAttributes().size())) {
220 auto &attrs
= MethodQualifiers
->getAttributes();
221 I
.Fun
.MethodQualifiers
= new DeclSpec(attrs
.getPool().getFactory());
222 MethodQualifiers
->forEachCVRUQualifier(
223 [&](DeclSpec::TQ TypeQual
, StringRef PrintName
, SourceLocation SL
) {
224 I
.Fun
.MethodQualifiers
->SetTypeQual(TypeQual
, SL
);
226 I
.Fun
.MethodQualifiers
->getAttributes().takeAllFrom(attrs
);
227 I
.Fun
.MethodQualifiers
->getAttributePool().takeAllFrom(attrs
.getPool());
230 assert(I
.Fun
.ExceptionSpecType
== ESpecType
&& "bitfield overflow");
232 // new[] a parameter array if needed.
234 // If the 'InlineParams' in Declarator is unused and big enough, put our
235 // parameter list there (in an effort to avoid new/delete traffic). If it
236 // is already used (consider a function returning a function pointer) or too
237 // small (function with too many parameters), go to the heap.
238 if (!TheDeclarator
.InlineStorageUsed
&&
239 NumParams
<= std::size(TheDeclarator
.InlineParams
)) {
240 I
.Fun
.Params
= TheDeclarator
.InlineParams
;
241 new (I
.Fun
.Params
) ParamInfo
[NumParams
];
242 I
.Fun
.DeleteParams
= false;
243 TheDeclarator
.InlineStorageUsed
= true;
245 I
.Fun
.Params
= new DeclaratorChunk::ParamInfo
[NumParams
];
246 I
.Fun
.DeleteParams
= true;
248 for (unsigned i
= 0; i
< NumParams
; i
++)
249 I
.Fun
.Params
[i
] = std::move(Params
[i
]);
252 // Check what exception specification information we should actually store.
254 default: break; // By default, save nothing.
256 // new[] an exception array if needed
258 I
.Fun
.NumExceptionsOrDecls
= NumExceptions
;
259 I
.Fun
.Exceptions
= new DeclaratorChunk::TypeAndRange
[NumExceptions
];
260 for (unsigned i
= 0; i
!= NumExceptions
; ++i
) {
261 I
.Fun
.Exceptions
[i
].Ty
= Exceptions
[i
];
262 I
.Fun
.Exceptions
[i
].Range
= ExceptionRanges
[i
];
267 case EST_DependentNoexcept
:
268 case EST_NoexceptFalse
:
269 case EST_NoexceptTrue
:
270 I
.Fun
.NoexceptExpr
= NoexceptExpr
;
274 I
.Fun
.ExceptionSpecTokens
= ExceptionSpecTokens
;
278 if (!DeclsInPrototype
.empty()) {
279 assert(ESpecType
== EST_None
&& NumExceptions
== 0 &&
280 "cannot have exception specifiers and decls in prototype");
281 I
.Fun
.NumExceptionsOrDecls
= DeclsInPrototype
.size();
282 // Copy the array of decls into stable heap storage.
283 I
.Fun
.DeclsInPrototype
= new NamedDecl
*[DeclsInPrototype
.size()];
284 for (size_t J
= 0; J
< DeclsInPrototype
.size(); ++J
)
285 I
.Fun
.DeclsInPrototype
[J
] = DeclsInPrototype
[J
];
291 void Declarator::setDecompositionBindings(
292 SourceLocation LSquareLoc
,
293 MutableArrayRef
<DecompositionDeclarator::Binding
> Bindings
,
294 SourceLocation RSquareLoc
) {
295 assert(!hasName() && "declarator given multiple names!");
297 BindingGroup
.LSquareLoc
= LSquareLoc
;
298 BindingGroup
.RSquareLoc
= RSquareLoc
;
299 BindingGroup
.NumBindings
= Bindings
.size();
300 Range
.setEnd(RSquareLoc
);
302 // We're now past the identifier.
303 SetIdentifier(nullptr, LSquareLoc
);
304 Name
.EndLocation
= RSquareLoc
;
306 // Allocate storage for bindings and stash them away.
307 if (Bindings
.size()) {
308 if (!InlineStorageUsed
&& Bindings
.size() <= std::size(InlineBindings
)) {
309 BindingGroup
.Bindings
= InlineBindings
;
310 BindingGroup
.DeleteBindings
= false;
311 InlineStorageUsed
= true;
313 BindingGroup
.Bindings
=
314 new DecompositionDeclarator::Binding
[Bindings
.size()];
315 BindingGroup
.DeleteBindings
= true;
317 std::uninitialized_move(Bindings
.begin(), Bindings
.end(),
318 BindingGroup
.Bindings
);
322 bool Declarator::isDeclarationOfFunction() const {
323 for (unsigned i
= 0, i_end
= DeclTypeInfo
.size(); i
< i_end
; ++i
) {
324 switch (DeclTypeInfo
[i
].Kind
) {
325 case DeclaratorChunk::Function
:
327 case DeclaratorChunk::Paren
:
329 case DeclaratorChunk::Pointer
:
330 case DeclaratorChunk::Reference
:
331 case DeclaratorChunk::Array
:
332 case DeclaratorChunk::BlockPointer
:
333 case DeclaratorChunk::MemberPointer
:
334 case DeclaratorChunk::Pipe
:
337 llvm_unreachable("Invalid type chunk");
340 switch (DS
.getTypeSpecType()) {
369 case TST_unknown_anytype
:
370 case TST_unspecified
:
374 case TST_typename_pack_indexing
:
375 #define GENERIC_IMAGE_TYPE(ImgType, Id) case TST_##ImgType##_t:
376 #include "clang/Basic/OpenCLImageTypes.def"
377 #define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) case TST_##Name:
378 #include "clang/Basic/HLSLIntangibleTypes.def"
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.
387 case TST_typeof_unqualExpr
:
389 if (Expr
*E
= DS
.getRepAsExpr())
390 return E
->getType()->isFunctionType();
393 #define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) case TST_##Trait:
394 #include "clang/Basic/TransformTypeTraits.def"
396 case TST_typeof_unqualType
:
397 case TST_typeofType
: {
398 QualType QT
= DS
.getRepAsType().get();
402 if (const LocInfoType
*LIT
= dyn_cast
<LocInfoType
>(QT
))
408 return QT
->isFunctionType();
412 llvm_unreachable("Invalid TypeSpecType!");
415 bool Declarator::isStaticMember() {
416 assert(getContext() == DeclaratorContext::Member
);
417 return getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static
||
418 (!isDeclarationOfFunction() && !getTemplateParameterLists().empty()) ||
419 (getName().getKind() == UnqualifiedIdKind::IK_OperatorFunctionId
&&
420 CXXMethodDecl::isStaticOverloadedOperator(
421 getName().OperatorFunctionId
.Operator
));
424 bool Declarator::isExplicitObjectMemberFunction() {
425 if (!isFunctionDeclarator())
427 DeclaratorChunk::FunctionTypeInfo
&Fun
= getFunctionTypeInfo();
429 auto *P
= dyn_cast_or_null
<ParmVarDecl
>(Fun
.Params
[0].Param
);
430 if (P
&& P
->isExplicitObjectParameter())
436 bool Declarator::isCtorOrDtor() {
437 return (getName().getKind() == UnqualifiedIdKind::IK_ConstructorName
) ||
438 (getName().getKind() == UnqualifiedIdKind::IK_DestructorName
);
441 void DeclSpec::forEachCVRUQualifier(
442 llvm::function_ref
<void(TQ
, StringRef
, SourceLocation
)> Handle
) {
443 if (TypeQualifiers
& TQ_const
)
444 Handle(TQ_const
, "const", TQ_constLoc
);
445 if (TypeQualifiers
& TQ_volatile
)
446 Handle(TQ_volatile
, "volatile", TQ_volatileLoc
);
447 if (TypeQualifiers
& TQ_restrict
)
448 Handle(TQ_restrict
, "restrict", TQ_restrictLoc
);
449 if (TypeQualifiers
& TQ_unaligned
)
450 Handle(TQ_unaligned
, "unaligned", TQ_unalignedLoc
);
453 void DeclSpec::forEachQualifier(
454 llvm::function_ref
<void(TQ
, StringRef
, SourceLocation
)> Handle
) {
455 forEachCVRUQualifier(Handle
);
456 // FIXME: Add code below to iterate through the attributes and call Handle.
459 bool DeclSpec::hasTagDefinition() const {
462 return cast
<TagDecl
>(getRepAsDecl())->isCompleteDefinition();
465 /// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
466 /// declaration specifier includes.
468 unsigned DeclSpec::getParsedSpecifiers() const {
470 if (StorageClassSpec
!= SCS_unspecified
||
471 ThreadStorageClassSpec
!= TSCS_unspecified
)
472 Res
|= PQ_StorageClassSpecifier
;
474 if (TypeQualifiers
!= TQ_unspecified
)
475 Res
|= PQ_TypeQualifier
;
477 if (hasTypeSpecifier())
478 Res
|= PQ_TypeSpecifier
;
480 if (FS_inline_specified
|| FS_virtual_specified
|| hasExplicitSpecifier() ||
481 FS_noreturn_specified
|| FS_forceinline_specified
)
482 Res
|= PQ_FunctionSpecifier
;
486 template <class T
> static bool BadSpecifier(T TNew
, T TPrev
,
487 const char *&PrevSpec
,
489 bool IsExtension
= true) {
490 PrevSpec
= DeclSpec::getSpecifierName(TPrev
);
492 DiagID
= diag::err_invalid_decl_spec_combination
;
494 DiagID
= IsExtension
? diag::ext_warn_duplicate_declspec
:
495 diag::warn_duplicate_declspec
;
499 const char *DeclSpec::getSpecifierName(DeclSpec::SCS S
) {
501 case DeclSpec::SCS_unspecified
: return "unspecified";
502 case DeclSpec::SCS_typedef
: return "typedef";
503 case DeclSpec::SCS_extern
: return "extern";
504 case DeclSpec::SCS_static
: return "static";
505 case DeclSpec::SCS_auto
: return "auto";
506 case DeclSpec::SCS_register
: return "register";
507 case DeclSpec::SCS_private_extern
: return "__private_extern__";
508 case DeclSpec::SCS_mutable
: return "mutable";
510 llvm_unreachable("Unknown typespec!");
513 const char *DeclSpec::getSpecifierName(DeclSpec::TSCS S
) {
515 case DeclSpec::TSCS_unspecified
: return "unspecified";
516 case DeclSpec::TSCS___thread
: return "__thread";
517 case DeclSpec::TSCS_thread_local
: return "thread_local";
518 case DeclSpec::TSCS__Thread_local
: return "_Thread_local";
520 llvm_unreachable("Unknown typespec!");
523 const char *DeclSpec::getSpecifierName(TypeSpecifierWidth W
) {
525 case TypeSpecifierWidth::Unspecified
:
526 return "unspecified";
527 case TypeSpecifierWidth::Short
:
529 case TypeSpecifierWidth::Long
:
531 case TypeSpecifierWidth::LongLong
:
534 llvm_unreachable("Unknown typespec!");
537 const char *DeclSpec::getSpecifierName(TSC C
) {
539 case TSC_unspecified
: return "unspecified";
540 case TSC_imaginary
: return "imaginary";
541 case TSC_complex
: return "complex";
543 llvm_unreachable("Unknown typespec!");
546 const char *DeclSpec::getSpecifierName(TypeSpecifierSign S
) {
548 case TypeSpecifierSign::Unspecified
:
549 return "unspecified";
550 case TypeSpecifierSign::Signed
:
552 case TypeSpecifierSign::Unsigned
:
555 llvm_unreachable("Unknown typespec!");
558 const char *DeclSpec::getSpecifierName(DeclSpec::TST T
,
559 const PrintingPolicy
&Policy
) {
561 case DeclSpec::TST_unspecified
: return "unspecified";
562 case DeclSpec::TST_void
: return "void";
563 case DeclSpec::TST_char
: return "char";
564 case DeclSpec::TST_wchar
: return Policy
.MSWChar
? "__wchar_t" : "wchar_t";
565 case DeclSpec::TST_char8
: return "char8_t";
566 case DeclSpec::TST_char16
: return "char16_t";
567 case DeclSpec::TST_char32
: return "char32_t";
568 case DeclSpec::TST_int
: return "int";
569 case DeclSpec::TST_int128
: return "__int128";
570 case DeclSpec::TST_bitint
: return "_BitInt";
571 case DeclSpec::TST_half
: return "half";
572 case DeclSpec::TST_float
: return "float";
573 case DeclSpec::TST_double
: return "double";
574 case DeclSpec::TST_accum
: return "_Accum";
575 case DeclSpec::TST_fract
: return "_Fract";
576 case DeclSpec::TST_float16
: return "_Float16";
577 case DeclSpec::TST_float128
: return "__float128";
578 case DeclSpec::TST_ibm128
: return "__ibm128";
579 case DeclSpec::TST_bool
: return Policy
.Bool
? "bool" : "_Bool";
580 case DeclSpec::TST_decimal32
: return "_Decimal32";
581 case DeclSpec::TST_decimal64
: return "_Decimal64";
582 case DeclSpec::TST_decimal128
: return "_Decimal128";
583 case DeclSpec::TST_enum
: return "enum";
584 case DeclSpec::TST_class
: return "class";
585 case DeclSpec::TST_union
: return "union";
586 case DeclSpec::TST_struct
: return "struct";
587 case DeclSpec::TST_interface
: return "__interface";
588 case DeclSpec::TST_typename
: return "type-name";
589 case DeclSpec::TST_typename_pack_indexing
:
590 return "type-name-pack-indexing";
591 case DeclSpec::TST_typeofType
:
592 case DeclSpec::TST_typeofExpr
: return "typeof";
593 case DeclSpec::TST_typeof_unqualType
:
594 case DeclSpec::TST_typeof_unqualExpr
: return "typeof_unqual";
595 case DeclSpec::TST_auto
: return "auto";
596 case DeclSpec::TST_auto_type
: return "__auto_type";
597 case DeclSpec::TST_decltype
: return "(decltype)";
598 case DeclSpec::TST_decltype_auto
: return "decltype(auto)";
599 #define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) \
600 case DeclSpec::TST_##Trait: \
602 #include "clang/Basic/TransformTypeTraits.def"
603 case DeclSpec::TST_unknown_anytype
: return "__unknown_anytype";
604 case DeclSpec::TST_atomic
: return "_Atomic";
605 case DeclSpec::TST_BFloat16
: return "__bf16";
606 #define GENERIC_IMAGE_TYPE(ImgType, Id) \
607 case DeclSpec::TST_##ImgType##_t: \
608 return #ImgType "_t";
609 #include "clang/Basic/OpenCLImageTypes.def"
610 #define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) \
611 case DeclSpec::TST_##Name: \
613 #include "clang/Basic/HLSLIntangibleTypes.def"
614 case DeclSpec::TST_error
: return "(error)";
616 llvm_unreachable("Unknown typespec!");
619 const char *DeclSpec::getSpecifierName(ConstexprSpecKind C
) {
621 case ConstexprSpecKind::Unspecified
:
622 return "unspecified";
623 case ConstexprSpecKind::Constexpr
:
625 case ConstexprSpecKind::Consteval
:
627 case ConstexprSpecKind::Constinit
:
630 llvm_unreachable("Unknown ConstexprSpecKind");
633 const char *DeclSpec::getSpecifierName(TQ T
) {
635 case DeclSpec::TQ_unspecified
: return "unspecified";
636 case DeclSpec::TQ_const
: return "const";
637 case DeclSpec::TQ_restrict
: return "restrict";
638 case DeclSpec::TQ_volatile
: return "volatile";
639 case DeclSpec::TQ_atomic
: return "_Atomic";
640 case DeclSpec::TQ_unaligned
: return "__unaligned";
642 llvm_unreachable("Unknown typespec!");
645 bool DeclSpec::SetStorageClassSpec(Sema
&S
, SCS SC
, SourceLocation Loc
,
646 const char *&PrevSpec
,
648 const PrintingPolicy
&Policy
) {
649 // OpenCL v1.1 s6.8g: "The extern, static, auto and register storage-class
650 // specifiers are not supported.
651 // It seems sensible to prohibit private_extern too
652 // The cl_clang_storage_class_specifiers extension enables support for
653 // these storage-class specifiers.
654 // OpenCL v1.2 s6.8 changes this to "The auto and register storage-class
655 // specifiers are not supported."
656 if (S
.getLangOpts().OpenCL
&&
657 !S
.getOpenCLOptions().isAvailableOption(
658 "cl_clang_storage_class_specifiers", S
.getLangOpts())) {
661 case SCS_private_extern
:
663 if (S
.getLangOpts().getOpenCLCompatibleVersion() < 120) {
664 DiagID
= diag::err_opencl_unknown_type_specifier
;
665 PrevSpec
= getSpecifierName(SC
);
671 DiagID
= diag::err_opencl_unknown_type_specifier
;
672 PrevSpec
= getSpecifierName(SC
);
679 if (StorageClassSpec
!= SCS_unspecified
) {
680 // Maybe this is an attempt to use C++11 'auto' outside of C++11 mode.
681 bool isInvalid
= true;
682 if (TypeSpecType
== TST_unspecified
&& S
.getLangOpts().CPlusPlus
) {
684 return SetTypeSpecType(TST_auto
, Loc
, PrevSpec
, DiagID
, Policy
);
685 if (StorageClassSpec
== SCS_auto
) {
686 isInvalid
= SetTypeSpecType(TST_auto
, StorageClassSpecLoc
,
687 PrevSpec
, DiagID
, Policy
);
688 assert(!isInvalid
&& "auto SCS -> TST recovery failed");
692 // Changing storage class is allowed only if the previous one
693 // was the 'extern' that is part of a linkage specification and
694 // the new storage class is 'typedef'.
696 !(SCS_extern_in_linkage_spec
&&
697 StorageClassSpec
== SCS_extern
&&
699 return BadSpecifier(SC
, (SCS
)StorageClassSpec
, PrevSpec
, DiagID
);
701 StorageClassSpec
= SC
;
702 StorageClassSpecLoc
= Loc
;
703 assert((unsigned)SC
== StorageClassSpec
&& "SCS constants overflow bitfield");
707 bool DeclSpec::SetStorageClassSpecThread(TSCS TSC
, SourceLocation Loc
,
708 const char *&PrevSpec
,
710 if (ThreadStorageClassSpec
!= TSCS_unspecified
)
711 return BadSpecifier(TSC
, (TSCS
)ThreadStorageClassSpec
, PrevSpec
, DiagID
);
713 ThreadStorageClassSpec
= TSC
;
714 ThreadStorageClassSpecLoc
= Loc
;
718 /// These methods set the specified attribute of the DeclSpec, but return true
719 /// and ignore the request if invalid (e.g. "extern" then "auto" is
721 bool DeclSpec::SetTypeSpecWidth(TypeSpecifierWidth W
, SourceLocation Loc
,
722 const char *&PrevSpec
, unsigned &DiagID
,
723 const PrintingPolicy
&Policy
) {
724 // Overwrite TSWRange.Begin only if TypeSpecWidth was unspecified, so that
725 // for 'long long' we will keep the source location of the first 'long'.
726 if (getTypeSpecWidth() == TypeSpecifierWidth::Unspecified
)
727 TSWRange
.setBegin(Loc
);
728 // Allow turning long -> long long.
729 else if (W
!= TypeSpecifierWidth::LongLong
||
730 getTypeSpecWidth() != TypeSpecifierWidth::Long
)
731 return BadSpecifier(W
, getTypeSpecWidth(), PrevSpec
, DiagID
);
732 TypeSpecWidth
= static_cast<unsigned>(W
);
733 // Remember location of the last 'long'
734 TSWRange
.setEnd(Loc
);
738 bool DeclSpec::SetTypeSpecComplex(TSC C
, SourceLocation Loc
,
739 const char *&PrevSpec
,
741 if (TypeSpecComplex
!= TSC_unspecified
)
742 return BadSpecifier(C
, (TSC
)TypeSpecComplex
, PrevSpec
, DiagID
);
748 bool DeclSpec::SetTypeSpecSign(TypeSpecifierSign S
, SourceLocation Loc
,
749 const char *&PrevSpec
, unsigned &DiagID
) {
750 if (getTypeSpecSign() != TypeSpecifierSign::Unspecified
)
751 return BadSpecifier(S
, getTypeSpecSign(), PrevSpec
, DiagID
);
752 TypeSpecSign
= static_cast<unsigned>(S
);
757 bool DeclSpec::SetTypeSpecType(TST T
, SourceLocation Loc
,
758 const char *&PrevSpec
,
761 const PrintingPolicy
&Policy
) {
762 return SetTypeSpecType(T
, Loc
, Loc
, PrevSpec
, DiagID
, Rep
, Policy
);
765 bool DeclSpec::SetTypeSpecType(TST T
, SourceLocation TagKwLoc
,
766 SourceLocation TagNameLoc
,
767 const char *&PrevSpec
,
770 const PrintingPolicy
&Policy
) {
771 assert(isTypeRep(T
) && "T does not store a type");
772 assert(Rep
&& "no type provided!");
773 if (TypeSpecType
== TST_error
)
775 if (TypeSpecType
!= TST_unspecified
) {
776 PrevSpec
= DeclSpec::getSpecifierName((TST
) TypeSpecType
, Policy
);
777 DiagID
= diag::err_invalid_decl_spec_combination
;
783 TSTNameLoc
= TagNameLoc
;
784 TypeSpecOwned
= false;
786 if (T
== TST_typename_pack_indexing
) {
787 // we got there from a an annotation. Reconstruct the type
789 QualType QT
= Rep
.get();
790 const PackIndexingType
*LIT
= cast
<PackIndexingType
>(QT
);
791 TypeRep
= ParsedType::make(LIT
->getPattern());
792 PackIndexingExpr
= LIT
->getIndexExpr();
797 bool DeclSpec::SetTypeSpecType(TST T
, SourceLocation Loc
,
798 const char *&PrevSpec
,
801 const PrintingPolicy
&Policy
) {
802 assert(isExprRep(T
) && "T does not store an expr");
803 assert(Rep
&& "no expression provided!");
804 if (TypeSpecType
== TST_error
)
806 if (TypeSpecType
!= TST_unspecified
) {
807 PrevSpec
= DeclSpec::getSpecifierName((TST
) TypeSpecType
, Policy
);
808 DiagID
= diag::err_invalid_decl_spec_combination
;
815 TypeSpecOwned
= false;
819 bool DeclSpec::SetTypeSpecType(TST T
, SourceLocation Loc
,
820 const char *&PrevSpec
,
822 Decl
*Rep
, bool Owned
,
823 const PrintingPolicy
&Policy
) {
824 return SetTypeSpecType(T
, Loc
, Loc
, PrevSpec
, DiagID
, Rep
, Owned
, Policy
);
827 bool DeclSpec::SetTypeSpecType(TST T
, SourceLocation TagKwLoc
,
828 SourceLocation TagNameLoc
,
829 const char *&PrevSpec
,
831 Decl
*Rep
, bool Owned
,
832 const PrintingPolicy
&Policy
) {
833 assert(isDeclRep(T
) && "T does not store a decl");
834 // Unlike the other cases, we don't assert that we actually get a decl.
836 if (TypeSpecType
== TST_error
)
838 if (TypeSpecType
!= TST_unspecified
) {
839 PrevSpec
= DeclSpec::getSpecifierName((TST
) TypeSpecType
, Policy
);
840 DiagID
= diag::err_invalid_decl_spec_combination
;
846 TSTNameLoc
= TagNameLoc
;
847 TypeSpecOwned
= Owned
&& Rep
!= nullptr;
851 bool DeclSpec::SetTypeSpecType(TST T
, SourceLocation Loc
, const char *&PrevSpec
,
852 unsigned &DiagID
, TemplateIdAnnotation
*Rep
,
853 const PrintingPolicy
&Policy
) {
854 assert(T
== TST_auto
|| T
== TST_decltype_auto
);
855 ConstrainedAuto
= true;
857 return SetTypeSpecType(T
, Loc
, PrevSpec
, DiagID
, Policy
);
860 bool DeclSpec::SetTypeSpecType(TST T
, SourceLocation Loc
,
861 const char *&PrevSpec
,
863 const PrintingPolicy
&Policy
) {
864 assert(!isDeclRep(T
) && !isTypeRep(T
) && !isExprRep(T
) &&
865 "rep required for these type-spec kinds!");
866 if (TypeSpecType
== TST_error
)
868 if (TypeSpecType
!= TST_unspecified
) {
869 PrevSpec
= DeclSpec::getSpecifierName((TST
) TypeSpecType
, Policy
);
870 DiagID
= diag::err_invalid_decl_spec_combination
;
875 if (TypeAltiVecVector
&& (T
== TST_bool
) && !TypeAltiVecBool
) {
876 TypeAltiVecBool
= true;
880 TypeSpecOwned
= false;
884 bool DeclSpec::SetTypeSpecSat(SourceLocation Loc
, const char *&PrevSpec
,
888 DiagID
= diag::warn_duplicate_declspec
;
897 bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector
, SourceLocation Loc
,
898 const char *&PrevSpec
, unsigned &DiagID
,
899 const PrintingPolicy
&Policy
) {
900 if (TypeSpecType
== TST_error
)
902 if (TypeSpecType
!= TST_unspecified
) {
903 PrevSpec
= DeclSpec::getSpecifierName((TST
) TypeSpecType
, Policy
);
904 DiagID
= diag::err_invalid_vector_decl_spec_combination
;
907 TypeAltiVecVector
= isAltiVecVector
;
912 bool DeclSpec::SetTypePipe(bool isPipe
, SourceLocation Loc
,
913 const char *&PrevSpec
, unsigned &DiagID
,
914 const PrintingPolicy
&Policy
) {
915 if (TypeSpecType
== TST_error
)
917 if (TypeSpecType
!= TST_unspecified
) {
918 PrevSpec
= DeclSpec::getSpecifierName((TST
)TypeSpecType
, Policy
);
919 DiagID
= diag::err_invalid_decl_spec_combination
;
924 TypeSpecPipe
= static_cast<unsigned>(TypeSpecifiersPipe::Pipe
);
929 bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel
, SourceLocation Loc
,
930 const char *&PrevSpec
, unsigned &DiagID
,
931 const PrintingPolicy
&Policy
) {
932 if (TypeSpecType
== TST_error
)
934 if (!TypeAltiVecVector
|| TypeAltiVecPixel
||
935 (TypeSpecType
!= TST_unspecified
)) {
936 PrevSpec
= DeclSpec::getSpecifierName((TST
) TypeSpecType
, Policy
);
937 DiagID
= diag::err_invalid_pixel_decl_spec_combination
;
940 TypeAltiVecPixel
= isAltiVecPixel
;
946 bool DeclSpec::SetTypeAltiVecBool(bool isAltiVecBool
, SourceLocation Loc
,
947 const char *&PrevSpec
, unsigned &DiagID
,
948 const PrintingPolicy
&Policy
) {
949 if (TypeSpecType
== TST_error
)
951 if (!TypeAltiVecVector
|| TypeAltiVecBool
||
952 (TypeSpecType
!= TST_unspecified
)) {
953 PrevSpec
= DeclSpec::getSpecifierName((TST
) TypeSpecType
, Policy
);
954 DiagID
= diag::err_invalid_vector_bool_decl_spec
;
957 TypeAltiVecBool
= isAltiVecBool
;
963 bool DeclSpec::SetTypeSpecError() {
964 TypeSpecType
= TST_error
;
965 TypeSpecOwned
= false;
966 TSTLoc
= SourceLocation();
967 TSTNameLoc
= SourceLocation();
971 bool DeclSpec::SetBitIntType(SourceLocation KWLoc
, Expr
*BitsExpr
,
972 const char *&PrevSpec
, unsigned &DiagID
,
973 const PrintingPolicy
&Policy
) {
974 assert(BitsExpr
&& "no expression provided!");
975 if (TypeSpecType
== TST_error
)
978 if (TypeSpecType
!= TST_unspecified
) {
979 PrevSpec
= DeclSpec::getSpecifierName((TST
) TypeSpecType
, Policy
);
980 DiagID
= diag::err_invalid_decl_spec_combination
;
984 TypeSpecType
= TST_bitint
;
988 TypeSpecOwned
= false;
992 void DeclSpec::SetPackIndexingExpr(SourceLocation EllipsisLoc
,
993 Expr
*IndexingExpr
) {
994 assert(TypeSpecType
== TST_typename
&&
995 "pack indexing can only be applied to typename");
996 TypeSpecType
= TST_typename_pack_indexing
;
997 PackIndexingExpr
= IndexingExpr
;
998 this->EllipsisLoc
= EllipsisLoc
;
1001 bool DeclSpec::SetTypeQual(TQ T
, SourceLocation Loc
, const char *&PrevSpec
,
1002 unsigned &DiagID
, const LangOptions
&Lang
) {
1003 // Duplicates are permitted in C99 onwards, but are not permitted in C89 or
1004 // C++. However, since this is likely not what the user intended, we will
1005 // always warn. We do not need to set the qualifier's location since we
1007 if (TypeQualifiers
& T
) {
1008 bool IsExtension
= true;
1010 IsExtension
= false;
1011 return BadSpecifier(T
, T
, PrevSpec
, DiagID
, IsExtension
);
1014 return SetTypeQual(T
, Loc
);
1017 bool DeclSpec::SetTypeQual(TQ T
, SourceLocation Loc
) {
1018 TypeQualifiers
|= T
;
1021 case TQ_unspecified
: break;
1022 case TQ_const
: TQ_constLoc
= Loc
; return false;
1023 case TQ_restrict
: TQ_restrictLoc
= Loc
; return false;
1024 case TQ_volatile
: TQ_volatileLoc
= Loc
; return false;
1025 case TQ_unaligned
: TQ_unalignedLoc
= Loc
; return false;
1026 case TQ_atomic
: TQ_atomicLoc
= Loc
; return false;
1029 llvm_unreachable("Unknown type qualifier!");
1032 bool DeclSpec::setFunctionSpecInline(SourceLocation Loc
, const char *&PrevSpec
,
1034 // 'inline inline' is ok. However, since this is likely not what the user
1035 // intended, we will always warn, similar to duplicates of type qualifiers.
1036 if (FS_inline_specified
) {
1037 DiagID
= diag::warn_duplicate_declspec
;
1038 PrevSpec
= "inline";
1041 FS_inline_specified
= true;
1046 bool DeclSpec::setFunctionSpecForceInline(SourceLocation Loc
, const char *&PrevSpec
,
1048 if (FS_forceinline_specified
) {
1049 DiagID
= diag::warn_duplicate_declspec
;
1050 PrevSpec
= "__forceinline";
1053 FS_forceinline_specified
= true;
1054 FS_forceinlineLoc
= Loc
;
1058 bool DeclSpec::setFunctionSpecVirtual(SourceLocation Loc
,
1059 const char *&PrevSpec
,
1061 // 'virtual virtual' is ok, but warn as this is likely not what the user
1063 if (FS_virtual_specified
) {
1064 DiagID
= diag::warn_duplicate_declspec
;
1065 PrevSpec
= "virtual";
1068 FS_virtual_specified
= true;
1069 FS_virtualLoc
= Loc
;
1073 bool DeclSpec::setFunctionSpecExplicit(SourceLocation Loc
,
1074 const char *&PrevSpec
, unsigned &DiagID
,
1075 ExplicitSpecifier ExplicitSpec
,
1076 SourceLocation CloseParenLoc
) {
1077 // 'explicit explicit' is ok, but warn as this is likely not what the user
1079 if (hasExplicitSpecifier()) {
1080 DiagID
= (ExplicitSpec
.getExpr() || FS_explicit_specifier
.getExpr())
1081 ? diag::err_duplicate_declspec
1082 : diag::ext_warn_duplicate_declspec
;
1083 PrevSpec
= "explicit";
1086 FS_explicit_specifier
= ExplicitSpec
;
1087 FS_explicitLoc
= Loc
;
1088 FS_explicitCloseParenLoc
= CloseParenLoc
;
1092 bool DeclSpec::setFunctionSpecNoreturn(SourceLocation Loc
,
1093 const char *&PrevSpec
,
1095 // '_Noreturn _Noreturn' is ok, but warn as this is likely not what the user
1097 if (FS_noreturn_specified
) {
1098 DiagID
= diag::warn_duplicate_declspec
;
1099 PrevSpec
= "_Noreturn";
1102 FS_noreturn_specified
= true;
1103 FS_noreturnLoc
= Loc
;
1107 bool DeclSpec::SetFriendSpec(SourceLocation Loc
, const char *&PrevSpec
,
1109 if (isFriendSpecified()) {
1110 PrevSpec
= "friend";
1111 DiagID
= diag::warn_duplicate_declspec
;
1115 FriendSpecifiedFirst
= isEmpty();
1120 bool DeclSpec::setModulePrivateSpec(SourceLocation Loc
, const char *&PrevSpec
,
1122 if (isModulePrivateSpecified()) {
1123 PrevSpec
= "__module_private__";
1124 DiagID
= diag::ext_warn_duplicate_declspec
;
1128 ModulePrivateLoc
= Loc
;
1132 bool DeclSpec::SetConstexprSpec(ConstexprSpecKind ConstexprKind
,
1133 SourceLocation Loc
, const char *&PrevSpec
,
1135 if (getConstexprSpecifier() != ConstexprSpecKind::Unspecified
)
1136 return BadSpecifier(ConstexprKind
, getConstexprSpecifier(), PrevSpec
,
1138 ConstexprSpecifier
= static_cast<unsigned>(ConstexprKind
);
1143 void DeclSpec::SaveWrittenBuiltinSpecs() {
1144 writtenBS
.Sign
= static_cast<int>(getTypeSpecSign());
1145 writtenBS
.Width
= static_cast<int>(getTypeSpecWidth());
1146 writtenBS
.Type
= getTypeSpecType();
1147 // Search the list of attributes for the presence of a mode attribute.
1148 writtenBS
.ModeAttr
= getAttributes().hasAttribute(ParsedAttr::AT_Mode
);
1151 /// Finish - This does final analysis of the declspec, rejecting things like
1152 /// "_Complex" (lacking an FP type). After calling this method, DeclSpec is
1153 /// guaranteed to be self-consistent, even if an error occurred.
1154 void DeclSpec::Finish(Sema
&S
, const PrintingPolicy
&Policy
) {
1155 // Before possibly changing their values, save specs as written.
1156 SaveWrittenBuiltinSpecs();
1158 // Check the type specifier components first. No checking for an invalid
1160 if (TypeSpecType
== TST_error
)
1163 // If decltype(auto) is used, no other type specifiers are permitted.
1164 if (TypeSpecType
== TST_decltype_auto
&&
1165 (getTypeSpecWidth() != TypeSpecifierWidth::Unspecified
||
1166 TypeSpecComplex
!= TSC_unspecified
||
1167 getTypeSpecSign() != TypeSpecifierSign::Unspecified
||
1168 TypeAltiVecVector
|| TypeAltiVecPixel
|| TypeAltiVecBool
||
1170 const unsigned NumLocs
= 9;
1171 SourceLocation ExtraLocs
[NumLocs
] = {
1172 TSWRange
.getBegin(), TSCLoc
, TSSLoc
,
1173 AltiVecLoc
, TQ_constLoc
, TQ_restrictLoc
,
1174 TQ_volatileLoc
, TQ_atomicLoc
, TQ_unalignedLoc
};
1175 FixItHint Hints
[NumLocs
];
1176 SourceLocation FirstLoc
;
1177 for (unsigned I
= 0; I
!= NumLocs
; ++I
) {
1178 if (ExtraLocs
[I
].isValid()) {
1179 if (FirstLoc
.isInvalid() ||
1180 S
.getSourceManager().isBeforeInTranslationUnit(ExtraLocs
[I
],
1182 FirstLoc
= ExtraLocs
[I
];
1183 Hints
[I
] = FixItHint::CreateRemoval(ExtraLocs
[I
]);
1186 TypeSpecWidth
= static_cast<unsigned>(TypeSpecifierWidth::Unspecified
);
1187 TypeSpecComplex
= TSC_unspecified
;
1188 TypeSpecSign
= static_cast<unsigned>(TypeSpecifierSign::Unspecified
);
1189 TypeAltiVecVector
= TypeAltiVecPixel
= TypeAltiVecBool
= false;
1191 S
.Diag(TSTLoc
, diag::err_decltype_auto_cannot_be_combined
)
1192 << Hints
[0] << Hints
[1] << Hints
[2] << Hints
[3]
1193 << Hints
[4] << Hints
[5] << Hints
[6] << Hints
[7];
1196 // Validate and finalize AltiVec vector declspec.
1197 if (TypeAltiVecVector
) {
1198 // No vector long long without VSX (or ZVector).
1199 if ((getTypeSpecWidth() == TypeSpecifierWidth::LongLong
) &&
1200 !S
.Context
.getTargetInfo().hasFeature("vsx") &&
1201 !S
.getLangOpts().ZVector
)
1202 S
.Diag(TSWRange
.getBegin(), diag::err_invalid_vector_long_long_decl_spec
);
1204 // No vector __int128 prior to Power8.
1205 if ((TypeSpecType
== TST_int128
) &&
1206 !S
.Context
.getTargetInfo().hasFeature("power8-vector"))
1207 S
.Diag(TSTLoc
, diag::err_invalid_vector_int128_decl_spec
);
1209 // Complex vector types are not supported.
1210 if (TypeSpecComplex
!= TSC_unspecified
)
1211 S
.Diag(TSCLoc
, diag::err_invalid_vector_complex_decl_spec
);
1212 else if (TypeAltiVecBool
) {
1213 // Sign specifiers are not allowed with vector bool. (PIM 2.1)
1214 if (getTypeSpecSign() != TypeSpecifierSign::Unspecified
) {
1215 S
.Diag(TSSLoc
, diag::err_invalid_vector_bool_decl_spec
)
1216 << getSpecifierName(getTypeSpecSign());
1218 // Only char/int are valid with vector bool prior to Power10.
1219 // Power10 adds instructions that produce vector bool data
1220 // for quadwords as well so allow vector bool __int128.
1221 if (((TypeSpecType
!= TST_unspecified
) && (TypeSpecType
!= TST_char
) &&
1222 (TypeSpecType
!= TST_int
) && (TypeSpecType
!= TST_int128
)) ||
1224 S
.Diag(TSTLoc
, diag::err_invalid_vector_bool_decl_spec
)
1225 << (TypeAltiVecPixel
? "__pixel" :
1226 getSpecifierName((TST
)TypeSpecType
, Policy
));
1228 // vector bool __int128 requires Power10.
1229 if ((TypeSpecType
== TST_int128
) &&
1230 (!S
.Context
.getTargetInfo().hasFeature("power10-vector")))
1231 S
.Diag(TSTLoc
, diag::err_invalid_vector_bool_int128_decl_spec
);
1233 // Only 'short' and 'long long' are valid with vector bool. (PIM 2.1)
1234 if ((getTypeSpecWidth() != TypeSpecifierWidth::Unspecified
) &&
1235 (getTypeSpecWidth() != TypeSpecifierWidth::Short
) &&
1236 (getTypeSpecWidth() != TypeSpecifierWidth::LongLong
))
1237 S
.Diag(TSWRange
.getBegin(), diag::err_invalid_vector_bool_decl_spec
)
1238 << getSpecifierName(getTypeSpecWidth());
1240 // Elements of vector bool are interpreted as unsigned. (PIM 2.1)
1241 if ((TypeSpecType
== TST_char
) || (TypeSpecType
== TST_int
) ||
1242 (TypeSpecType
== TST_int128
) ||
1243 (getTypeSpecWidth() != TypeSpecifierWidth::Unspecified
))
1244 TypeSpecSign
= static_cast<unsigned>(TypeSpecifierSign::Unsigned
);
1245 } else if (TypeSpecType
== TST_double
) {
1246 // vector long double and vector long long double are never allowed.
1247 // vector double is OK for Power7 and later, and ZVector.
1248 if (getTypeSpecWidth() == TypeSpecifierWidth::Long
||
1249 getTypeSpecWidth() == TypeSpecifierWidth::LongLong
)
1250 S
.Diag(TSWRange
.getBegin(),
1251 diag::err_invalid_vector_long_double_decl_spec
);
1252 else if (!S
.Context
.getTargetInfo().hasFeature("vsx") &&
1253 !S
.getLangOpts().ZVector
)
1254 S
.Diag(TSTLoc
, diag::err_invalid_vector_double_decl_spec
);
1255 } else if (TypeSpecType
== TST_float
) {
1256 // vector float is unsupported for ZVector unless we have the
1257 // vector-enhancements facility 1 (ISA revision 12).
1258 if (S
.getLangOpts().ZVector
&&
1259 !S
.Context
.getTargetInfo().hasFeature("arch12"))
1260 S
.Diag(TSTLoc
, diag::err_invalid_vector_float_decl_spec
);
1261 } else if (getTypeSpecWidth() == TypeSpecifierWidth::Long
) {
1262 // Vector long is unsupported for ZVector, or without VSX, and deprecated
1264 // It has also been historically deprecated on AIX (as an alias for
1265 // "vector int" in both 32-bit and 64-bit modes). It was then made
1266 // unsupported in the Clang-based XL compiler since the deprecated type
1267 // has a number of conflicting semantics and continuing to support it
1268 // is a disservice to users.
1269 if (S
.getLangOpts().ZVector
||
1270 !S
.Context
.getTargetInfo().hasFeature("vsx") ||
1271 S
.Context
.getTargetInfo().getTriple().isOSAIX())
1272 S
.Diag(TSWRange
.getBegin(), diag::err_invalid_vector_long_decl_spec
);
1274 S
.Diag(TSWRange
.getBegin(),
1275 diag::warn_vector_long_decl_spec_combination
)
1276 << getSpecifierName((TST
)TypeSpecType
, Policy
);
1279 if (TypeAltiVecPixel
) {
1280 //TODO: perform validation
1281 TypeSpecType
= TST_int
;
1282 TypeSpecSign
= static_cast<unsigned>(TypeSpecifierSign::Unsigned
);
1283 TypeSpecWidth
= static_cast<unsigned>(TypeSpecifierWidth::Short
);
1284 TypeSpecOwned
= false;
1288 bool IsFixedPointType
=
1289 TypeSpecType
== TST_accum
|| TypeSpecType
== TST_fract
;
1291 // signed/unsigned are only valid with int/char/wchar_t/_Accum.
1292 if (getTypeSpecSign() != TypeSpecifierSign::Unspecified
) {
1293 if (TypeSpecType
== TST_unspecified
)
1294 TypeSpecType
= TST_int
; // unsigned -> unsigned int, signed -> signed int.
1295 else if (TypeSpecType
!= TST_int
&& TypeSpecType
!= TST_int128
&&
1296 TypeSpecType
!= TST_char
&& TypeSpecType
!= TST_wchar
&&
1297 !IsFixedPointType
&& TypeSpecType
!= TST_bitint
) {
1298 S
.Diag(TSSLoc
, diag::err_invalid_sign_spec
)
1299 << getSpecifierName((TST
)TypeSpecType
, Policy
);
1300 // signed double -> double.
1301 TypeSpecSign
= static_cast<unsigned>(TypeSpecifierSign::Unspecified
);
1305 // Validate the width of the type.
1306 switch (getTypeSpecWidth()) {
1307 case TypeSpecifierWidth::Unspecified
:
1309 case TypeSpecifierWidth::Short
: // short int
1310 case TypeSpecifierWidth::LongLong
: // long long int
1311 if (TypeSpecType
== TST_unspecified
)
1312 TypeSpecType
= TST_int
; // short -> short int, long long -> long long int.
1313 else if (!(TypeSpecType
== TST_int
||
1314 (IsFixedPointType
&&
1315 getTypeSpecWidth() != TypeSpecifierWidth::LongLong
))) {
1316 S
.Diag(TSWRange
.getBegin(), diag::err_invalid_width_spec
)
1317 << (int)TypeSpecWidth
<< getSpecifierName((TST
)TypeSpecType
, Policy
);
1318 TypeSpecType
= TST_int
;
1319 TypeSpecSat
= false;
1320 TypeSpecOwned
= false;
1323 case TypeSpecifierWidth::Long
: // long double, long int
1324 if (TypeSpecType
== TST_unspecified
)
1325 TypeSpecType
= TST_int
; // long -> long int.
1326 else if (TypeSpecType
!= TST_int
&& TypeSpecType
!= TST_double
&&
1327 !IsFixedPointType
) {
1328 S
.Diag(TSWRange
.getBegin(), diag::err_invalid_width_spec
)
1329 << (int)TypeSpecWidth
<< getSpecifierName((TST
)TypeSpecType
, Policy
);
1330 TypeSpecType
= TST_int
;
1331 TypeSpecSat
= false;
1332 TypeSpecOwned
= false;
1337 // TODO: if the implementation does not implement _Complex, disallow their
1338 // use. Need information about the backend.
1339 if (TypeSpecComplex
!= TSC_unspecified
) {
1340 if (TypeSpecType
== TST_unspecified
) {
1341 S
.Diag(TSCLoc
, diag::ext_plain_complex
)
1342 << FixItHint::CreateInsertion(
1343 S
.getLocForEndOfToken(getTypeSpecComplexLoc()),
1345 TypeSpecType
= TST_double
; // _Complex -> _Complex double.
1346 } else if (TypeSpecType
== TST_int
|| TypeSpecType
== TST_char
||
1347 TypeSpecType
== TST_bitint
) {
1348 // Note that this intentionally doesn't include _Complex _Bool.
1349 if (!S
.getLangOpts().CPlusPlus
)
1350 S
.Diag(TSTLoc
, diag::ext_integer_complex
);
1351 } else if (TypeSpecType
!= TST_float
&& TypeSpecType
!= TST_double
&&
1352 TypeSpecType
!= TST_float128
&& TypeSpecType
!= TST_float16
&&
1353 TypeSpecType
!= TST_ibm128
) {
1355 S
.Diag(TSCLoc
, diag::err_invalid_complex_spec
)
1356 << getSpecifierName((TST
)TypeSpecType
, Policy
);
1357 TypeSpecComplex
= TSC_unspecified
;
1361 // C11 6.7.1/3, C++11 [dcl.stc]p1, GNU TLS: __thread, thread_local and
1362 // _Thread_local can only appear with the 'static' and 'extern' storage class
1363 // specifiers. We also allow __private_extern__ as an extension.
1364 if (ThreadStorageClassSpec
!= TSCS_unspecified
) {
1365 switch (StorageClassSpec
) {
1366 case SCS_unspecified
:
1368 case SCS_private_extern
:
1372 if (S
.getSourceManager().isBeforeInTranslationUnit(
1373 getThreadStorageClassSpecLoc(), getStorageClassSpecLoc()))
1374 S
.Diag(getStorageClassSpecLoc(),
1375 diag::err_invalid_decl_spec_combination
)
1376 << DeclSpec::getSpecifierName(getThreadStorageClassSpec())
1377 << SourceRange(getThreadStorageClassSpecLoc());
1379 S
.Diag(getThreadStorageClassSpecLoc(),
1380 diag::err_invalid_decl_spec_combination
)
1381 << DeclSpec::getSpecifierName(getStorageClassSpec())
1382 << SourceRange(getStorageClassSpecLoc());
1383 // Discard the thread storage class specifier to recover.
1384 ThreadStorageClassSpec
= TSCS_unspecified
;
1385 ThreadStorageClassSpecLoc
= SourceLocation();
1387 if (S
.getLangOpts().C23
&&
1388 getConstexprSpecifier() == ConstexprSpecKind::Constexpr
) {
1389 S
.Diag(ConstexprLoc
, diag::err_invalid_decl_spec_combination
)
1390 << DeclSpec::getSpecifierName(getThreadStorageClassSpec())
1391 << SourceRange(getThreadStorageClassSpecLoc());
1395 if (S
.getLangOpts().C23
&&
1396 getConstexprSpecifier() == ConstexprSpecKind::Constexpr
&&
1397 StorageClassSpec
== SCS_extern
) {
1398 S
.Diag(ConstexprLoc
, diag::err_invalid_decl_spec_combination
)
1399 << DeclSpec::getSpecifierName(getStorageClassSpec())
1400 << SourceRange(getStorageClassSpecLoc());
1403 // If no type specifier was provided and we're parsing a language where
1404 // the type specifier is not optional, but we got 'auto' as a storage
1405 // class specifier, then assume this is an attempt to use C++0x's 'auto'
1407 if (S
.getLangOpts().CPlusPlus
&&
1408 TypeSpecType
== TST_unspecified
&& StorageClassSpec
== SCS_auto
) {
1409 TypeSpecType
= TST_auto
;
1410 StorageClassSpec
= SCS_unspecified
;
1411 TSTLoc
= TSTNameLoc
= StorageClassSpecLoc
;
1412 StorageClassSpecLoc
= SourceLocation();
1414 // Diagnose if we've recovered from an ill-formed 'auto' storage class
1415 // specifier in a pre-C++11 dialect of C++ or in a pre-C23 dialect of C.
1416 if (!S
.getLangOpts().CPlusPlus11
&& !S
.getLangOpts().C23
&&
1417 TypeSpecType
== TST_auto
)
1418 S
.Diag(TSTLoc
, diag::ext_auto_type_specifier
) << /*C++*/ 0;
1419 if (S
.getLangOpts().HLSL
&&
1420 S
.getLangOpts().getHLSLVersion() < LangOptions::HLSL_202y
&&
1421 TypeSpecType
== TST_auto
)
1422 S
.Diag(TSTLoc
, diag::ext_hlsl_auto_type_specifier
) << /*HLSL*/ 1;
1423 if (S
.getLangOpts().CPlusPlus
&& !S
.getLangOpts().CPlusPlus11
&&
1424 StorageClassSpec
== SCS_auto
)
1425 S
.Diag(StorageClassSpecLoc
, diag::warn_auto_storage_class
)
1426 << FixItHint::CreateRemoval(StorageClassSpecLoc
);
1427 if (TypeSpecType
== TST_char8
)
1428 S
.Diag(TSTLoc
, diag::warn_cxx17_compat_unicode_type
);
1429 else if (TypeSpecType
== TST_char16
|| TypeSpecType
== TST_char32
)
1430 S
.Diag(TSTLoc
, diag::warn_cxx98_compat_unicode_type
)
1431 << (TypeSpecType
== TST_char16
? "char16_t" : "char32_t");
1432 if (getConstexprSpecifier() == ConstexprSpecKind::Constexpr
)
1433 S
.Diag(ConstexprLoc
, diag::warn_cxx98_compat_constexpr
);
1434 else if (getConstexprSpecifier() == ConstexprSpecKind::Consteval
)
1435 S
.Diag(ConstexprLoc
, diag::warn_cxx20_compat_consteval
);
1436 else if (getConstexprSpecifier() == ConstexprSpecKind::Constinit
)
1437 S
.Diag(ConstexprLoc
, diag::warn_cxx20_compat_constinit
);
1438 // C++ [class.friend]p6:
1439 // No storage-class-specifier shall appear in the decl-specifier-seq
1440 // of a friend declaration.
1441 if (isFriendSpecified() &&
1442 (getStorageClassSpec() || getThreadStorageClassSpec())) {
1443 SmallString
<32> SpecName
;
1444 SourceLocation SCLoc
;
1445 FixItHint StorageHint
, ThreadHint
;
1447 if (DeclSpec::SCS SC
= getStorageClassSpec()) {
1448 SpecName
= getSpecifierName(SC
);
1449 SCLoc
= getStorageClassSpecLoc();
1450 StorageHint
= FixItHint::CreateRemoval(SCLoc
);
1453 if (DeclSpec::TSCS TSC
= getThreadStorageClassSpec()) {
1454 if (!SpecName
.empty()) SpecName
+= " ";
1455 SpecName
+= getSpecifierName(TSC
);
1456 SCLoc
= getThreadStorageClassSpecLoc();
1457 ThreadHint
= FixItHint::CreateRemoval(SCLoc
);
1460 S
.Diag(SCLoc
, diag::err_friend_decl_spec
)
1461 << SpecName
<< StorageHint
<< ThreadHint
;
1463 ClearStorageClassSpecs();
1466 // C++11 [dcl.fct.spec]p5:
1467 // The virtual specifier shall be used only in the initial
1468 // declaration of a non-static class member function;
1469 // C++11 [dcl.fct.spec]p6:
1470 // The explicit specifier shall be used only in the declaration of
1471 // a constructor or conversion function within its class
1473 if (isFriendSpecified() && (isVirtualSpecified() || hasExplicitSpecifier())) {
1476 SourceLocation SCLoc
;
1478 if (isVirtualSpecified()) {
1479 Keyword
= "virtual";
1480 SCLoc
= getVirtualSpecLoc();
1481 Hint
= FixItHint::CreateRemoval(SCLoc
);
1483 Keyword
= "explicit";
1484 SCLoc
= getExplicitSpecLoc();
1485 Hint
= FixItHint::CreateRemoval(getExplicitSpecRange());
1488 S
.Diag(SCLoc
, diag::err_friend_decl_spec
)
1491 FS_virtual_specified
= false;
1492 FS_explicit_specifier
= ExplicitSpecifier();
1493 FS_virtualLoc
= FS_explicitLoc
= SourceLocation();
1496 assert(!TypeSpecOwned
|| isDeclRep((TST
) TypeSpecType
));
1498 // Okay, now we can infer the real type.
1500 // TODO: return "auto function" and other bad things based on the real type.
1502 // 'data definition has no type or storage class'?
1505 bool DeclSpec::isMissingDeclaratorOk() {
1506 TST tst
= getTypeSpecType();
1507 return isDeclRep(tst
) && getRepAsDecl() != nullptr &&
1508 StorageClassSpec
!= DeclSpec::SCS_typedef
;
1511 void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc
,
1512 OverloadedOperatorKind Op
,
1513 SourceLocation SymbolLocations
[3]) {
1514 Kind
= UnqualifiedIdKind::IK_OperatorFunctionId
;
1515 StartLocation
= OperatorLoc
;
1516 EndLocation
= OperatorLoc
;
1517 new (&OperatorFunctionId
) struct OFI
;
1518 OperatorFunctionId
.Operator
= Op
;
1519 for (unsigned I
= 0; I
!= 3; ++I
) {
1520 OperatorFunctionId
.SymbolLocations
[I
] = SymbolLocations
[I
];
1522 if (SymbolLocations
[I
].isValid())
1523 EndLocation
= SymbolLocations
[I
];
1527 bool VirtSpecifiers::SetSpecifier(Specifier VS
, SourceLocation Loc
,
1528 const char *&PrevSpec
) {
1529 if (!FirstLocation
.isValid())
1530 FirstLocation
= Loc
;
1534 if (Specifiers
& VS
) {
1535 PrevSpec
= getSpecifierName(VS
);
1542 default: llvm_unreachable("Unknown specifier!");
1543 case VS_Override
: VS_overrideLoc
= Loc
; break;
1546 case VS_Final
: VS_finalLoc
= Loc
; break;
1547 case VS_Abstract
: VS_abstractLoc
= Loc
; break;
1553 const char *VirtSpecifiers::getSpecifierName(Specifier VS
) {
1555 default: llvm_unreachable("Unknown specifier");
1556 case VS_Override
: return "override";
1557 case VS_Final
: return "final";
1558 case VS_GNU_Final
: return "__final";
1559 case VS_Sealed
: return "sealed";
1560 case VS_Abstract
: return "abstract";