38#include "llvm/ADT/ArrayRef.h"
39#include "llvm/ADT/SmallPtrSet.h"
40#include "llvm/ADT/SmallString.h"
41#include "llvm/ADT/StringExtras.h"
42#include "llvm/IR/DerivedTypes.h"
43#include "llvm/Support/ErrorHandling.h"
58 if (D.
getContext() != DeclaratorContext::BlockLiteral ||
77 bool useExpansionLoc =
true;
78 switch (
attr.getKind()) {
79 case ParsedAttr::AT_ObjCGC:
82 case ParsedAttr::AT_ObjCOwnership:
88 useExpansionLoc =
false;
93 StringRef name =
attr.getAttrName()->getName();
98 if (useExpansionLoc && loc.
isMacroID() && II) {
99 if (II->
isStr(
"strong")) {
101 }
else if (II->
isStr(
"weak")) {
106 S.
Diag(loc,
attr.isRegularKeywordAttribute()
107 ? diag::err_type_attribute_wrong_type
108 : diag::warn_type_attribute_wrong_type)
109 << name << WhichType <<
type;
114#define OBJC_POINTER_TYPE_ATTRS_CASELIST \
115 case ParsedAttr::AT_ObjCGC: \
116 case ParsedAttr::AT_ObjCOwnership
119#define CALLING_CONV_ATTRS_CASELIST \
120 case ParsedAttr::AT_CDecl: \
121 case ParsedAttr::AT_FastCall: \
122 case ParsedAttr::AT_StdCall: \
123 case ParsedAttr::AT_ThisCall: \
124 case ParsedAttr::AT_RegCall: \
125 case ParsedAttr::AT_Pascal: \
126 case ParsedAttr::AT_SwiftCall: \
127 case ParsedAttr::AT_SwiftAsyncCall: \
128 case ParsedAttr::AT_VectorCall: \
129 case ParsedAttr::AT_AArch64VectorPcs: \
130 case ParsedAttr::AT_AArch64SVEPcs: \
131 case ParsedAttr::AT_AMDGPUKernelCall: \
132 case ParsedAttr::AT_MSABI: \
133 case ParsedAttr::AT_SysVABI: \
134 case ParsedAttr::AT_Pcs: \
135 case ParsedAttr::AT_IntelOclBicc: \
136 case ParsedAttr::AT_PreserveMost: \
137 case ParsedAttr::AT_PreserveAll
140#define FUNCTION_TYPE_ATTRS_CASELIST \
141 case ParsedAttr::AT_NSReturnsRetained: \
142 case ParsedAttr::AT_NoReturn: \
143 case ParsedAttr::AT_Regparm: \
144 case ParsedAttr::AT_CmseNSCall: \
145 case ParsedAttr::AT_ArmStreaming: \
146 case ParsedAttr::AT_ArmStreamingCompatible: \
147 case ParsedAttr::AT_ArmSharedZA: \
148 case ParsedAttr::AT_ArmPreservesZA: \
149 case ParsedAttr::AT_AnyX86NoCallerSavedRegisters: \
150 case ParsedAttr::AT_AnyX86NoCfCheck: \
151 CALLING_CONV_ATTRS_CASELIST
154#define MS_TYPE_ATTRS_CASELIST \
155 case ParsedAttr::AT_Ptr32: \
156 case ParsedAttr::AT_Ptr64: \
157 case ParsedAttr::AT_SPtr: \
158 case ParsedAttr::AT_UPtr
161#define NULLABILITY_TYPE_ATTRS_CASELIST \
162 case ParsedAttr::AT_TypeNonNull: \
163 case ParsedAttr::AT_TypeNullable: \
164 case ParsedAttr::AT_TypeNullableResult: \
165 case ParsedAttr::AT_TypeNullUnspecified
170 class TypeProcessingState {
194 using TypeAttrPair = std::pair<const AttributedType*, const Attr*>;
196 bool AttrsForTypesSorted =
true;
200 llvm::DenseMap<const MacroQualifiedType *, SourceLocation> LocsForMacros;
208 : sema(sema), declarator(declarator),
211 Sema &getSema()
const {
219 bool isProcessingDeclSpec()
const {
223 unsigned getCurrentChunkIndex()
const {
227 void setCurrentChunkIndex(
unsigned idx) {
233 if (isProcessingDeclSpec())
234 return getMutableDeclSpec().getAttributes();
239 void saveDeclSpecAttrs() {
241 if (!savedAttrs.empty())
244 DeclSpec &spec = getMutableDeclSpec();
245 llvm::append_range(savedAttrs,
252 ignoredTypeAttrs.push_back(&
attr);
258 for (
auto *
Attr : ignoredTypeAttrs)
268 AttrsForTypes.push_back({cast<AttributedType>(T.
getTypePtr()), A});
269 AttrsForTypesSorted =
false;
274 QualType getBTFTagAttributedType(
const BTFTypeTagAttr *BTFAttr,
286 auto *NewAttrTy = cast<AttributedType>(T.
getTypePtr());
287 for (TypeAttrPair &A : AttrsForTypes) {
288 if (A.first == AttrTy)
291 AttrsForTypesSorted =
false;
298 if (!AttrsForTypesSorted) {
299 llvm::stable_sort(AttrsForTypes, llvm::less_first());
300 AttrsForTypesSorted =
true;
305 for (
auto It = std::partition_point(
306 AttrsForTypes.begin(), AttrsForTypes.end(),
307 [=](
const TypeAttrPair &A) { return A.first < AT; });
308 It != AttrsForTypes.end() && It->first == AT; ++It) {
310 const Attr *Result = It->second;
311 It->second =
nullptr;
316 llvm_unreachable(
"no Attr* for AttributedType*");
321 auto FoundLoc = LocsForMacros.find(MQT);
322 assert(FoundLoc != LocsForMacros.end() &&
323 "Unable to find macro expansion location for MacroQualifedType");
324 return FoundLoc->second;
329 LocsForMacros[MQT] = Loc;
332 void setParsedNoDeref(
bool parsed) { parsedNoDeref = parsed; }
334 bool didParseNoDeref()
const {
return parsedNoDeref; }
336 ~TypeProcessingState() {
337 if (savedAttrs.empty())
340 getMutableDeclSpec().getAttributes().clearListOnly();
342 getMutableDeclSpec().getAttributes().addAtEnd(AL);
346 DeclSpec &getMutableDeclSpec()
const {
389 if (
attr.getKind() == ParsedAttr::AT_ObjCGC)
391 assert(
attr.getKind() == ParsedAttr::AT_ObjCOwnership);
406 bool onlyBlockPointers) {
412 for (; i != 0; --i) {
414 switch (fnChunk.
Kind) {
430 for (--i; i != 0; --i) {
432 switch (ptrChunk.
Kind) {
442 if (onlyBlockPointers)
451 llvm_unreachable(
"bad declarator chunk kind");
457 llvm_unreachable(
"bad declarator chunk kind");
474 Declarator &declarator = state.getDeclarator();
477 for (
unsigned i = state.getCurrentChunkIndex(); i != 0; --i) {
479 switch (chunk.
Kind) {
485 if (state.isProcessingDeclSpec() &&
486 attr.getKind() == ParsedAttr::AT_ObjCOwnership)
489 if (!destChunk) destChunk = &chunk;
502 if (state.isProcessingDeclSpec() &&
503 attr.getKind() == ParsedAttr::AT_ObjCOwnership) {
530 Declarator &declarator = state.getDeclarator();
534 unsigned innermost = -1U;
535 bool considerDeclSpec =
true;
538 switch (chunk.
Kind) {
552 considerDeclSpec =
false;
560 if (considerDeclSpec) {
565 state.saveDeclSpecAttrs();
574 if (innermost != -1U) {
582 state.addIgnoredTypeAttr(
attr);
591 Declarator &declarator = state.getDeclarator();
595 for (
unsigned i = state.getCurrentChunkIndex(); i != 0; --i) {
597 switch (chunk.
Kind) {
624 Declarator &declarator = state.getDeclarator();
644 state.saveDeclSpecAttrs();
648 state,
attr, state.getCurrentAttributes(), declSpecType, CFT))
653 state.addIgnoredTypeAttr(
attr);
663 Declarator &declarator = state.getDeclarator();
673 state.addIgnoredTypeAttr(
attr);
698 if (
attr.isStandardAttributeSyntax() ||
attr.isRegularKeywordAttribute())
701 switch (
attr.getKind()) {
718 case ParsedAttr::AT_ObjCKindOf:
731 Declarator &declarator = state.getDeclarator();
777 std::nullopt, loc, loc, declarator));
792 typedef std::pair<DeclSpec::TQ, SourceLocation> QualLoc;
797 if (!(RemoveTQs & Qual.first))
801 if (TypeQuals & Qual.first)
802 S.
Diag(Qual.second, DiagID)
807 TypeQuals &= ~Qual.first;
821 if (AL.isInvalid() || !AL.isTypeAttr())
824 diag::warn_block_literal_attributes_on_omitted_return_type)
826 ToBeRemoved.push_back(&AL);
836 diag::warn_block_literal_qualifiers_on_omitted_return_type);
849 if (!objcObjectType || !objcObjectType->getInterface()) {
850 S.
Diag(loc, diag::err_objc_type_args_non_class)
863 S.
Diag(loc, diag::err_objc_type_args_non_parameterized_class)
874 if (objcObjectType->isSpecialized()) {
875 S.
Diag(loc, diag::err_objc_type_args_specialized_class)
887 unsigned numTypeParams = typeParams->
size();
888 bool anyPackExpansions =
false;
889 for (
unsigned i = 0, n = typeArgs.size(); i != n; ++i) {
897 bool diagnosed =
false;
900 rangeToRemove =
attr.getLocalSourceRange();
901 if (
attr.getTypePtr()->getImmediateNullability()) {
902 typeArg =
attr.getTypePtr()->getModifiedType();
904 diag::err_objc_type_arg_explicit_nullability)
912 if (!rebuilding && !diagnosed) {
913 S.
Diag(qual.getBeginLoc(), diag::err_objc_type_arg_qualified)
922 finalTypeArgs.push_back(typeArg);
925 anyPackExpansions =
true;
929 if (!anyPackExpansions) {
930 if (i < numTypeParams) {
931 typeParam = typeParams->
begin()[i];
934 S.
Diag(loc, diag::err_objc_type_args_wrong_arity)
955 assert(anyPackExpansions &&
"Too many arguments?");
964 if (typeArgObjC->isObjCIdType()) {
967 if (boundObjC->isObjCIdType())
976 diag::err_objc_type_arg_does_not_match_bound)
993 assert(anyPackExpansions &&
"Too many arguments?");
1004 diag::err_objc_type_arg_does_not_match_bound)
1022 diag::err_objc_type_arg_not_id_compatible)
1032 if (!anyPackExpansions && finalTypeArgs.size() != numTypeParams) {
1033 S.
Diag(loc, diag::err_objc_type_args_wrong_arity)
1034 << (typeArgs.size() < typeParams->
size())
1058 if (!Protocols.empty()) {
1064 <<
SourceRange(ProtocolLAngleLoc, ProtocolRAngleLoc);
1067 if (FailOnError &&
Result.isNull())
1079 bool FailOnError,
bool Rebuilding) {
1081 if (!TypeArgs.empty()) {
1084 SourceRange(TypeArgsLAngleLoc, TypeArgsRAngleLoc),
1085 FailOnError, Rebuilding);
1086 if (FailOnError &&
Result.isNull())
1090 if (!Protocols.empty()) {
1095 Diag(Loc, diag::err_invalid_protocol_qualifiers)
1096 <<
SourceRange(ProtocolLAngleLoc, ProtocolRAngleLoc);
1099 if (FailOnError &&
Result.isNull())
1125 auto ObjCObjectTL = ObjCObjectPointerTL.getPointeeLoc()
1135 ObjCObjectTL.setProtocolLAngleLoc(lAngleLoc);
1136 ObjCObjectTL.setProtocolRAngleLoc(rAngleLoc);
1137 for (
unsigned i = 0, n = protocols.size(); i != n; ++i)
1138 ObjCObjectTL.setProtocolLoc(i, protocolLocs[i]);
1166 for (
unsigned i = 0, n = TypeArgs.size(); i != n; ++i) {
1170 ActualTypeArgInfos.clear();
1174 assert(TypeArgInfo &&
"No type source info?");
1175 ActualTypeArgInfos.push_back(TypeArgInfo);
1181 TypeArgsLAngleLoc, ActualTypeArgInfos, TypeArgsRAngleLoc,
1185 ProtocolLocs, ProtocolRAngleLoc,
1201 ResultTL = ObjCObjectPointerTL.getPointeeLoc();
1206 if (OTPTL.getNumProtocols() > 0) {
1207 assert(OTPTL.getNumProtocols() == Protocols.size());
1208 OTPTL.setProtocolLAngleLoc(ProtocolLAngleLoc);
1209 OTPTL.setProtocolRAngleLoc(ProtocolRAngleLoc);
1210 for (
unsigned i = 0, n = Protocols.size(); i != n; ++i)
1211 OTPTL.setProtocolLoc(i, ProtocolLocs[i]);
1221 if (ObjCObjectTL.getNumTypeArgs() > 0) {
1222 assert(ObjCObjectTL.getNumTypeArgs() == ActualTypeArgInfos.size());
1223 ObjCObjectTL.setTypeArgsLAngleLoc(TypeArgsLAngleLoc);
1224 ObjCObjectTL.setTypeArgsRAngleLoc(TypeArgsRAngleLoc);
1225 for (
unsigned i = 0, n = ActualTypeArgInfos.size(); i != n; ++i)
1226 ObjCObjectTL.setTypeArgTInfo(i, ActualTypeArgInfos[i]);
1233 if (ObjCObjectTL.getNumProtocols() > 0) {
1234 assert(ObjCObjectTL.getNumProtocols() == Protocols.size());
1235 ObjCObjectTL.setProtocolLAngleLoc(ProtocolLAngleLoc);
1236 ObjCObjectTL.setProtocolRAngleLoc(ProtocolRAngleLoc);
1237 for (
unsigned i = 0, n = Protocols.size(); i != n; ++i)
1238 ObjCObjectTL.setProtocolLoc(i, ProtocolLocs[i]);
1245 ObjCObjectTL.setHasBaseTypeAsWritten(
true);
1246 if (ObjCObjectTL.getType() == T)
1247 ObjCObjectTL.getBaseLoc().initializeFullCopy(BaseTypeInfo->
getTypeLoc());
1249 ObjCObjectTL.getBaseLoc().initialize(
Context, Loc);
1255static OpenCLAccessAttr::Spelling
1258 if (AL.getKind() == ParsedAttr::AT_OpenCLAccess)
1259 return static_cast<OpenCLAccessAttr::Spelling
>(AL.getSemanticSpelling());
1260 return OpenCLAccessAttr::Keyword_read_only;
1265 switch (SwitchTST) {
1266#define TRANSFORM_TYPE_TRAIT_DEF(Enum, Trait) \
1268 return UnaryTransformType::Enum;
1269#include "clang/Basic/TransformTypeTraits.def"
1271 llvm_unreachable(
"attempted to parse a non-unary transform builtin");
1285 Sema &S = state.getSema();
1286 Declarator &declarator = state.getDeclarator();
1306 "Unknown TSS value");
1320 "Unknown TSS value");
1329 "Unknown TSS value");
1334 "Unknown TSS value");
1339 "Unknown TSS value");
1367 S.
Diag(DeclLoc, diag::warn_missing_type_specifier)
1376 S.
Diag(DeclLoc, diag::err_missing_type_specifier)
1385 S.
Diag(DeclLoc, diag::err_missing_actual_pipe_type)
1390 "implicit int is disabled?");
1391 S.
Diag(DeclLoc, diag::ext_missing_type_specifier)
1418 diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong);
1443 diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong);
1476 llvm_unreachable(
"Unable to specify long long as _Accum width");
1499 llvm_unreachable(
"Unable to specify long long as _Fract width");
1550 ?
"cl_khr_fp64 and __opencl_c_fp64"
1600 "No qualifiers on tag names!");
1616 "Can't handle qualifiers on typedef names yet!");
1629 assert(!
Result.isNull() &&
"Didn't get a type for typeof?");
1630 if (!
Result->isDependentType())
1642 assert(E &&
"Didn't get an expression for typeof?");
1656 assert(E &&
"Didn't get an expression for decltype?");
1665#define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) case DeclSpec::TST_##Trait:
1666#include "clang/Basic/TransformTypeTraits.def"
1668 assert(!
Result.isNull() &&
"Didn't get a type for the transformation?");
1688 TypeConstraintConcept =
1689 cast<ConceptDecl>(TemplateId->Template.get().getAsTemplateDecl());
1694 TemplateId->NumArgs);
1696 for (
const auto &ArgLoc : TemplateArgsInfo.
arguments())
1697 TemplateArgs.push_back(ArgLoc.getArgument());
1704 TypeConstraintConcept, TemplateArgs);
1718 assert(!
Result.isNull() &&
"Didn't get a type for _Atomic?");
1726#define GENERIC_IMAGE_TYPE(ImgType, Id) \
1727 case DeclSpec::TST_##ImgType##_t: \
1728 switch (getImageAccess(DS.getAttributes())) { \
1729 case OpenCLAccessAttr::Keyword_write_only: \
1730 Result = Context.Id##WOTy; \
1732 case OpenCLAccessAttr::Keyword_read_write: \
1733 Result = Context.Id##RWTy; \
1735 case OpenCLAccessAttr::Keyword_read_only: \
1736 Result = Context.Id##ROTy; \
1738 case OpenCLAccessAttr::SpellingNotCalculated: \
1739 llvm_unreachable("Spelling not yet calculated"); \
1742#include "clang/Basic/OpenCLImageTypes.def"
1753 if (
Result->containsErrors())
1758 bool IsOpenCLC30Compatible =
1768 (IsOpenCLC30Compatible &&
1771 << 0 <<
Result <<
"__opencl_c_images";
1773 }
else if (
Result->isOCLImage3dWOType() &&
1778 << (IsOpenCLC30Compatible
1779 ?
"cl_khr_3d_image_writes and __opencl_c_3d_image_writes"
1780 :
"cl_khr_3d_image_writes");
1801 assert(typeSize > 0 &&
"type size for vector must be greater than 0 bits");
1830 if (AL.slidesFromDeclToDeclSpecLegacyBehavior()) {
1841 if (AL.isStandardAttributeSyntax() && AL.isClangScope() &&
1842 !(AL.getKind() == ParsedAttr::AT_MatrixType &&
1844 S.
Diag(AL.getLoc(), diag::warn_type_attribute_deprecated_on_decl)
1870 if (
Result->isFunctionType()) {
1874 ? diag::warn_typecheck_function_qualifiers_ignored
1875 : diag::warn_typecheck_function_qualifiers_unspecified);
1888 if (TypeQuals &&
Result->isReferenceType()) {
1890 S, DS, TypeQuals,
Result,
1892 diag::warn_typecheck_reference_qualifiers);
1899 && TypeQuals &
Result.getCVRQualifiers()) {
1923 assert(!
Result.isNull() &&
"This function should not return a null type");
1938 const auto *AT = dyn_cast<AutoType>(T);
1939 return AT && AT->isGNUAutoType();
1956 unsigned DiagID = 0;
1972 DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
1979 DiagID = diag::err_typecheck_invalid_restrict_not_pointer;
1993 unsigned CVRAU,
const DeclSpec *DS) {
2027 Split.Quals.addCVRQualifiers(CVR);
2046 if (!
type->isObjCLifetimeType() ||
2056 if (
type.isConstQualified()) {
2062 }
else if (
type->isObjCARCImplicitlyUnretainedType()) {
2080 diag::err_arc_indirect_no_ownership,
type, isReference));
2082 S.
Diag(loc, diag::err_arc_indirect_no_ownership) <<
type << isReference;
2086 assert(implicitLifetime &&
"didn't infer any lifetime!");
2127enum QualifiedFunctionKind { QFK_BlockPointer, QFK_Pointer, QFK_Reference };
2133 QualifiedFunctionKind QFK) {
2140 S.
Diag(Loc, diag::err_compound_qualified_function_type)
2152 Diag(Loc, diag::err_qualified_function_typeid)
2184 Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
2192 Diag(Loc, diag::err_opencl_function_pointer) << 0;
2197 Diag(Loc, diag::err_hlsl_pointers_unsupported) << 0;
2217 Diag(Loc, diag::err_wasm_reference_pr) << 0;
2223 Diag(Loc, diag::err_wasm_table_pr) << 0;
2249 "Unresolved overloaded function type");
2277 Diag(Loc, diag::err_reference_to_void);
2282 Diag(Loc, diag::err_hlsl_pointers_unsupported) << 1;
2292 Diag(Loc, diag::err_opencl_function_pointer) << 1;
2306 Diag(Loc, diag::err_wasm_reference_pr) << 1;
2310 Diag(Loc, diag::err_wasm_table_pr) << 1;
2357 llvm::APSInt Bits(32);
2364 size_t NumBits = Bits.getZExtValue();
2365 if (!IsUnsigned && NumBits < 2) {
2366 Diag(Loc, diag::err_bit_int_bad_size) << 0;
2370 if (IsUnsigned && NumBits < 1) {
2371 Diag(Loc, diag::err_bit_int_bad_size) << 1;
2377 Diag(Loc, diag::err_bit_int_max_size)
2391 llvm::APSInt &SizeVal,
unsigned VLADiag,
2416 VLADiagnoser(
unsigned VLADiag,
bool VLAIsError)
2417 : VLADiag(VLADiag), VLAIsError(VLAIsError) {}
2421 return S.
Diag(Loc, diag::err_array_size_non_int) << T;
2426 IsVLA = !VLAIsError;
2427 return S.
Diag(Loc, VLADiag);
2432 return S.
Diag(Loc, diag::ext_vla_folded_to_constant);
2434 } Diagnoser(VLADiag, VLAIsError);
2438 if (Diagnoser.IsVLA)
2452 if (Size.isMultipleOf(Alignment))
2455 Diag(Loc, diag::err_array_element_alignment)
2456 << EltTy << Size.getQuantity() << Alignment.
getQuantity();
2476 Expr *ArraySize,
unsigned Quals,
2493 Diag(Loc, diag::err_illegal_decl_array_of_references)
2499 Diag(Loc, diag::err_array_incomplete_or_sizeless_type) << 0 << T;
2504 diag::err_array_of_abstract_type))
2511 if (!MPTy->getClass()->isDependentType())
2519 diag::err_array_incomplete_or_sizeless_type))
2525 const auto *ATy = dyn_cast<ArrayType>(T);
2526 if (ATy && ATy->getElementType().isWebAssemblyReferenceType()) {
2527 Diag(Loc, diag::err_wasm_reftype_multidimensional_array);
2533 Diag(Loc, diag::err_array_incomplete_or_sizeless_type) << 1 << T;
2538 Diag(Loc, diag::err_illegal_decl_array_of_functions)
2546 if (EltTy->getDecl()->hasFlexibleArrayMember())
2547 Diag(Loc, diag::ext_flexible_array_in_array) << T;
2549 Diag(Loc, diag::err_objc_array_of_interfaces) << T;
2560 ArraySize =
Result.get();
2564 if (ArraySize && !ArraySize->
isPRValue()) {
2569 ArraySize =
Result.get();
2587 VLADiag = diag::err_opencl_vla;
2590 VLADiag = diag::warn_vla_used;
2593 VLADiag = diag::err_vla_in_sfinae;
2596 VLADiag = diag::err_openmp_vla_in_task_untied;
2599 VLADiag = diag::ext_vla;
2640 if (ConstVal.isSigned() && ConstVal.isNegative()) {
2647 diag::err_typecheck_negative_array_size)
2656 : diag::ext_typecheck_zero_array_size)
2661 unsigned ActiveSizeBits =
2665 : ConstVal.getActiveBits();
2680 IsCUDADevice ? diag::err_cuda_vla : diag::err_vla_unsupported)
2688 : diag::ext_c99_array_usage)
2699 Diag(Loc, diag::err_opencl_invalid_type_array) << ArrType;
2716 Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << CurType;
2721 unsigned NumBits = BIT->getNumBits();
2722 if (!llvm::isPowerOf2_32(NumBits) || NumBits < 8) {
2723 Diag(AttrLoc, diag::err_attribute_invalid_bitint_vector_type)
2733 std::optional<llvm::APSInt> VecSize =
2736 Diag(AttrLoc, diag::err_attribute_argument_type)
2747 if (!VecSize->isIntN(61)) {
2749 Diag(AttrLoc, diag::err_attribute_size_too_large)
2753 uint64_t VectorSizeBits = VecSize->getZExtValue() * 8;
2756 if (VectorSizeBits == 0) {
2757 Diag(AttrLoc, diag::err_attribute_zero_size)
2762 if (!TypeSize || VectorSizeBits % TypeSize) {
2763 Diag(AttrLoc, diag::err_attribute_invalid_size)
2768 if (VectorSizeBits / TypeSize > std::numeric_limits<uint32_t>::max()) {
2769 Diag(AttrLoc, diag::err_attribute_size_too_large)
2796 Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
2803 if (!llvm::isPowerOf2_32(NumBits) || NumBits < 8) {
2804 Diag(AttrLoc, diag::err_attribute_invalid_bitint_vector_type)
2811 std::optional<llvm::APSInt> vecSize =
2814 Diag(AttrLoc, diag::err_attribute_argument_type)
2820 if (!vecSize->isIntN(32)) {
2821 Diag(AttrLoc, diag::err_attribute_size_too_large)
2827 unsigned vectorSize =
static_cast<unsigned>(vecSize->getZExtValue());
2829 if (vectorSize == 0) {
2830 Diag(AttrLoc, diag::err_attribute_zero_size)
2844 "Should never build a matrix type when it is disabled");
2849 Diag(AttrLoc, diag::err_attribute_invalid_matrix_type) << ElementTy;
2858 std::optional<llvm::APSInt> ValueRows =
2860 std::optional<llvm::APSInt> ValueColumns =
2867 if (!ValueRows && !ValueColumns) {
2868 Diag(AttrLoc, diag::err_attribute_argument_type)
2876 Diag(AttrLoc, diag::err_attribute_argument_type)
2882 if (!ValueColumns) {
2883 Diag(AttrLoc, diag::err_attribute_argument_type)
2889 unsigned MatrixRows =
static_cast<unsigned>(ValueRows->getZExtValue());
2890 unsigned MatrixColumns =
static_cast<unsigned>(ValueColumns->getZExtValue());
2891 if (MatrixRows == 0 && MatrixColumns == 0) {
2892 Diag(AttrLoc, diag::err_attribute_zero_size)
2893 <<
"matrix" << RowRange << ColRange;
2896 if (MatrixRows == 0) {
2897 Diag(AttrLoc, diag::err_attribute_zero_size) <<
"matrix" << RowRange;
2900 if (MatrixColumns == 0) {
2901 Diag(AttrLoc, diag::err_attribute_zero_size) <<
"matrix" << ColRange;
2905 Diag(AttrLoc, diag::err_attribute_size_too_large)
2906 << RowRange <<
"matrix row";
2910 Diag(AttrLoc, diag::err_attribute_size_too_large)
2911 << ColRange <<
"matrix column";
2919 Diag(Loc, diag::err_func_returning_array_function)
2927 Diag(Loc, diag::err_parameters_retval_cannot_have_fp16_type) << 1 <<
2935 Diag(Loc, diag::err_object_cannot_be_passed_returned_by_value)
2948 Diag(Loc, diag::warn_deprecated_volatile_return) << T;
2963 bool emittedError =
false;
2965 enum class RequiredCC { OnlySwift, SwiftOrSwiftAsync };
2966 auto checkCompatible = [&](
unsigned paramIndex, RequiredCC required) {
2968 (required == RequiredCC::OnlySwift)
2971 if (isCompatible || emittedError)
2973 S.
Diag(getParamLoc(paramIndex), diag::err_swift_param_attr_not_swiftcall)
2975 << (required == RequiredCC::OnlySwift);
2976 emittedError =
true;
2978 for (
size_t paramIndex = 0, numParams = paramTypes.size();
2979 paramIndex != numParams; ++paramIndex) {
2988 checkCompatible(paramIndex, RequiredCC::SwiftOrSwiftAsync);
2989 if (paramIndex != 0 &&
2992 S.
Diag(getParamLoc(paramIndex),
2993 diag::err_swift_indirect_result_not_first);
2998 checkCompatible(paramIndex, RequiredCC::SwiftOrSwiftAsync);
3007 checkCompatible(paramIndex, RequiredCC::OnlySwift);
3008 if (paramIndex == 0 ||
3011 S.
Diag(getParamLoc(paramIndex),
3012 diag::err_swift_error_result_not_after_swift_context);
3016 llvm_unreachable(
"bad ABI kind");
3028 for (
unsigned Idx = 0, Cnt = ParamTypes.size(); Idx < Cnt; ++Idx) {
3032 Diag(Loc, diag::err_param_with_void_type);
3037 Diag(Loc, diag::err_parameters_retval_cannot_have_fp16_type) << 0 <<
3041 Diag(Loc, diag::err_wasm_table_as_function_parameter);
3048 Diag(Loc, diag::warn_deprecated_volatile_param) << ParamType;
3050 ParamTypes[Idx] = ParamType;
3055 [=](
unsigned i) {
return Loc; });
3084 Diag(Loc, diag::err_distant_exception_spec);
3091 Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
3097 Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
3102 if (!Class->isDependentType() && !Class->isRecordType()) {
3103 Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
3110 Diag(Loc, diag::err_opencl_function_pointer) << 0;
3115 Diag(Loc, diag::err_hlsl_pointers_unsupported) << 0;
3145 Diag(Loc, diag::err_nonfunction_block_type);
3161 if (TInfo) *TInfo =
nullptr;
3166 if (
const LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) {
3167 QT = LIT->getType();
3168 DI = LIT->getTypeSourceInfo();
3171 if (TInfo) *TInfo = DI;
3177 unsigned chunkIndex);
3184 Sema &S = state.getSema();
3185 Declarator &declarator = state.getDeclarator();
3191 unsigned outermostPointerIndex = 0;
3193 unsigned numPointers = 0;
3195 unsigned chunkIndex = i;
3197 switch (chunk.
Kind) {
3207 outermostPointerIndex = chunkIndex;
3215 if (numPointers != 1)
return;
3217 outermostPointerIndex = chunkIndex;
3235 if (numPointers == 1) {
3253 }
else if (numPointers == 2) {
3266 if (AL.getKind() == ParsedAttr::AT_ObjCOwnership)
3270 outermostPointerIndex);
3292 }
const QualKinds[5] = {
3301 unsigned NumQuals = 0;
3306 for (
auto &E : QualKinds) {
3307 if (Quals & E.Mask) {
3308 if (!QualStr.empty()) QualStr +=
' ';
3325 << QualStr << NumQuals << FixIts[0] << FixIts[1] << FixIts[2] << FixIts[3];
3331 unsigned FunctionChunkIndex) {
3341 for (
unsigned OuterChunkIndex = FunctionChunkIndex + 1,
3343 OuterChunkIndex != End; ++OuterChunkIndex) {
3345 switch (OuterChunk.
Kind) {
3352 diag::warn_qual_return_type,
3378 llvm_unreachable(
"unknown declarator chunk kind");
3399static std::pair<QualType, TypeSourceInfo *>
3403 Sema &S = state.getSema();
3407 const unsigned AutoParameterPosition = Info.
TemplateParams.size();
3421 TemplateParameterDepth, AutoParameterPosition,
3424 IsParameterPack,
Auto->isConstrained());
3429 if (
Auto->isConstrained()) {
3436 for (
unsigned Idx = 0; Idx < AutoLoc.
getNumArgs(); ++Idx) {
3489 QualType NewT = state.ReplaceAutoType(T, Replacement);
3493 return {NewT, NewTSI};
3502 Sema &SemaRef = state.getSema();
3505 ReturnTypeInfo =
nullptr;
3508 TagDecl *OwnedTagDecl =
nullptr;
3559 bool DeducedIsTrailingReturnType =
false;
3563 DeducedIsTrailingReturnType =
true;
3573 bool IsCXXAutoType =
3575 bool IsDeducedReturnType =
false;
3612 assert(Info &&
"No LambdaScopeInfo on the stack!");
3618 if (!DeducedIsTrailingReturnType)
3627 if (isa<ObjCContainerDecl>(SemaRef.
CurContext)) {
3630 switch (cast<TagDecl>(SemaRef.
CurContext)->getTagKind()) {
3631 case TTK_Enum: llvm_unreachable(
"unhandled tag kind");
3633 case TTK_Union: Error = Cxx ? 3 : 4;
break;
3647 if (isa<DeducedTemplateSpecializationType>(Deduced) &&
3659 if (isa<DeducedTemplateSpecializationType>(Deduced) &&
3673 if (!SemaRef.
getLangOpts().CPlusPlus14 || !IsCXXAutoType)
3675 IsDeducedReturnType =
true;
3678 if (!SemaRef.
getLangOpts().CPlusPlus14 || !IsCXXAutoType)
3680 IsDeducedReturnType =
true;
3683 if (isa<DeducedTemplateSpecializationType>(Deduced))
3685 if (SemaRef.
getLangOpts().CPlusPlus23 && IsCXXAutoType &&
3686 !
Auto->isDecltypeAuto())
3705 if (!IsCXXAutoType && !isa<DeducedTemplateSpecializationType>(Deduced))
3719 (!SemaRef.
getLangOpts().CPlusPlus11 || !IsCXXAutoType))
3729 switch (
Auto->getKeyword()) {
3735 assert(isa<DeducedTemplateSpecializationType>(Deduced) &&
3736 "unknown auto type");
3740 auto *DTST = dyn_cast<DeducedTemplateSpecializationType>(Deduced);
3743 SemaRef.
Diag(AutoRange.
getBegin(), diag::err_auto_not_allowed)
3745 <<
QualType(Deduced, 0) << AutoRange;
3747 SemaRef.
Diag(TD->getLocation(), diag::note_template_decl_here);
3756 ? diag::warn_cxx11_compat_generic_lambda
3757 : IsDeducedReturnType
3758 ? diag::warn_cxx11_compat_deduced_return_type
3759 : diag::warn_cxx98_compat_auto_type_specifier)
3768 unsigned DiagID = 0;
3774 llvm_unreachable(
"parser should not have allowed this");
3790 DiagID = diag::err_type_defined_in_alias_template;
3802 DiagID = diag::err_type_defined_in_type_specifier;
3812 DiagID = diag::err_type_defined_in_param_type;
3818 DiagID = diag::err_type_defined_in_condition;
3829 assert(!T.
isNull() &&
"This function should not return a null type");
3838 assert(FTI.
isAmbiguous &&
"no direct-initializer / function ambiguity");
3868 FTI.
NumParams ? diag::warn_parens_disambiguated_as_function_declaration
3869 : diag::warn_empty_parens_are_function_decl)
3880 if (Comma.getFileID() != Name.getFileID() ||
3881 Comma.getSpellingLineNumber() != Name.getSpellingLineNumber()) {
3888 Result.suppressDiagnostics();
3901 S.
Diag(B, diag::note_additional_parens_for_variable_declaration)
3916 S.
Diag(DeclType.
Loc, diag::note_empty_parens_default_ctor)
3921 if (Init.empty() && S.
LangOpts.CPlusPlus11)
3924 S.
Diag(DeclType.
Loc, diag::note_empty_parens_zero_initialize)
3935 "do not have redundant top-level parentheses");
3944 bool CouldBeTemporaryObject =
3951 bool StartsWithDeclaratorId =
true;
3959 StartsWithDeclaratorId =
false;
3964 CouldBeTemporaryObject =
false;
3972 CouldBeTemporaryObject =
false;
3973 StartsWithDeclaratorId =
false;
3983 CouldBeTemporaryObject =
false;
3990 CouldBeTemporaryObject =
false;
3991 StartsWithDeclaratorId =
false;
4001 if (CouldBeTemporaryObject) {
4005 CouldBeTemporaryObject =
false;
4006 Result.suppressDiagnostics();
4011 if (!CouldBeTemporaryObject) {
4024 S.
Diag(
Paren.Loc, diag::warn_redundant_parens_around_declarator)
4030 S.
Diag(
Paren.Loc, diag::warn_parens_disambiguated_as_variable_declaration)
4033 if (!RD || !RD->hasDefinition() || RD->hasNonTrivialDestructor())
4034 S.
Diag(
Paren.Loc, diag::note_raii_guard_add_name)
4039 S.
Diag(D.
getBeginLoc(), diag::note_function_style_cast_add_parentheses)
4042 S.
Diag(
Paren.Loc, diag::note_remove_parens_for_variable_declaration)
4058 switch (AL.getKind()) {
4077 bool IsCXXInstanceMethod =
false;
4083 unsigned I = ChunkIndex;
4084 bool FoundNonParen =
false;
4085 while (I && !FoundNonParen) {
4088 FoundNonParen =
true;
4091 if (FoundNonParen) {
4094 IsCXXInstanceMethod =
4099 IsCXXInstanceMethod =
4107 IsCXXInstanceMethod =
4115 IsCXXInstanceMethod);
4123 if (AL.getKind() == ParsedAttr::AT_OpenCLKernel) {
4133 if (Triple.getArch() == llvm::Triple::spirv32 ||
4134 Triple.getArch() == llvm::Triple::spirv64) {
4136 if (AL.getKind() == ParsedAttr::AT_CUDAGlobal) {
4150 enum class SimplePointerKind {
4159 switch (nullability) {
4161 if (!Ident__Nonnull)
4163 return Ident__Nonnull;
4166 if (!Ident__Nullable)
4168 return Ident__Nullable;
4171 if (!Ident__Nullable_result)
4173 return Ident__Nullable_result;
4176 if (!Ident__Null_unspecified)
4178 return Ident__Null_unspecified;
4180 llvm_unreachable(
"Unknown nullability kind.");
4188 return Ident_NSError;
4195 if (AL.getKind() == ParsedAttr::AT_TypeNonNull ||
4196 AL.getKind() == ParsedAttr::AT_TypeNullable ||
4197 AL.getKind() == ParsedAttr::AT_TypeNullableResult ||
4198 AL.getKind() == ParsedAttr::AT_TypeNullUnspecified)
4207 enum class PointerDeclaratorKind {
4215 MaybePointerToCFRef,
4219 NSErrorPointerPointer,
4225 enum class PointerWrappingDeclaratorKind {
4239static PointerDeclaratorKind
4241 PointerWrappingDeclaratorKind &wrappingKind) {
4242 unsigned numNormalPointers = 0;
4245 if (
type->isDependentType())
4246 return PointerDeclaratorKind::NonPointer;
4251 switch (chunk.
Kind) {
4253 if (numNormalPointers == 0)
4254 wrappingKind = PointerWrappingDeclaratorKind::Array;
4263 return numNormalPointers > 0 ? PointerDeclaratorKind::MultiLevelPointer
4264 : PointerDeclaratorKind::SingleLevelPointer;
4270 if (numNormalPointers == 0)
4271 wrappingKind = PointerWrappingDeclaratorKind::Reference;
4275 ++numNormalPointers;
4276 if (numNormalPointers > 2)
4277 return PointerDeclaratorKind::MultiLevelPointer;
4283 unsigned numTypeSpecifierPointers = 0;
4287 ++numNormalPointers;
4289 if (numNormalPointers > 2)
4290 return PointerDeclaratorKind::MultiLevelPointer;
4292 type = ptrType->getPointeeType();
4293 ++numTypeSpecifierPointers;
4299 return numNormalPointers > 0 ? PointerDeclaratorKind::MultiLevelPointer
4300 : PointerDeclaratorKind::SingleLevelPointer;
4305 return numNormalPointers > 0 ? PointerDeclaratorKind::MultiLevelPointer
4306 : PointerDeclaratorKind::SingleLevelPointer;
4311 ++numNormalPointers;
4312 ++numTypeSpecifierPointers;
4315 if (
auto objcClassDecl = objcObjectPtr->getInterfaceDecl()) {
4317 numNormalPointers == 2 && numTypeSpecifierPointers < 2) {
4318 return PointerDeclaratorKind::NSErrorPointerPointer;
4327 if (objcClass->getInterface()->getIdentifier() == S.
getNSErrorIdent()) {
4328 if (numNormalPointers == 2 && numTypeSpecifierPointers < 2)
4329 return PointerDeclaratorKind::NSErrorPointerPointer;
4336 if (numNormalPointers == 0)
4337 return PointerDeclaratorKind::NonPointer;
4343 if (numNormalPointers == 2 && numTypeSpecifierPointers < 2 &&
4345 return PointerDeclaratorKind::CFErrorRefPointer;
4353 switch (numNormalPointers) {
4355 return PointerDeclaratorKind::NonPointer;
4358 return PointerDeclaratorKind::SingleLevelPointer;
4361 return PointerDeclaratorKind::MaybePointerToCFRef;
4364 return PointerDeclaratorKind::MultiLevelPointer;
4379 if (
auto bridgeAttr = RD->
getAttr<ObjCBridgeAttr>())
4380 bridgedType = bridgeAttr->getBridgedType();
4381 else if (
auto bridgeAttr = RD->
getAttr<ObjCBridgeMutableAttr>())
4382 bridgedType = bridgeAttr->getBridgedType();
4398 if (ctx->isFunctionOrMethod())
4401 if (ctx->isFileContext())
4412 bool invalid =
false;
4414 if (invalid || !sloc.
isFile())
4432template <
typename DiagBuilderT>
4441 if (!FixItLoc.
isValid() || FixItLoc == PointerLoc)
4450 InsertionTextBuf +=
" ";
4451 StringRef InsertionText = InsertionTextBuf.str();
4454 InsertionText = InsertionText.drop_back();
4455 }
else if (NextChar[-1] ==
'[') {
4456 if (NextChar[0] ==
']')
4457 InsertionText = InsertionText.drop_back().drop_front();
4459 InsertionText = InsertionText.drop_front();
4462 InsertionText = InsertionText.drop_back().drop_front();
4469 SimplePointerKind PointerKind,
4474 if (PointerKind == SimplePointerKind::Array) {
4475 S.
Diag(PointerLoc, diag::warn_nullability_missing_array);
4477 S.
Diag(PointerLoc, diag::warn_nullability_missing)
4478 <<
static_cast<unsigned>(PointerKind);
4481 auto FixItLoc = PointerEndLoc.
isValid() ? PointerEndLoc : PointerLoc;
4482 if (FixItLoc.isMacroID())
4486 auto Diag = S.
Diag(FixItLoc, diag::note_nullability_fix_it);
4487 Diag << static_cast<unsigned>(Nullability);
4488 Diag << static_cast<unsigned>(PointerKind);
4517 if (pointerKind == SimplePointerKind::Array)
4518 diagKind = diag::warn_nullability_missing_array;
4520 diagKind = diag::warn_nullability_missing;
4526 fileNullability.
PointerKind =
static_cast<unsigned>(pointerKind);
4558 auto kind =
static_cast<SimplePointerKind
>(fileNullability.
PointerKind);
4573 unsigned i = endIndex;
4601template<
typename AttrT>
4604 return ::new (Ctx) AttrT(Ctx, AL);
4611 return createSimpleAttr<TypeNonNullAttr>(Ctx,
Attr);
4614 return createSimpleAttr<TypeNullableAttr>(Ctx,
Attr);
4617 return createSimpleAttr<TypeNullableResultAttr>(Ctx,
Attr);
4620 return createSimpleAttr<TypeNullUnspecifiedAttr>(Ctx,
Attr);
4622 llvm_unreachable(
"unknown NullabilityKind");
4633 if (ASOld != ASNew) {
4634 S.
Diag(AttrLoc, diag::err_attribute_address_multiple_qualifiers);
4639 diag::warn_attribute_address_multiple_identical_qualifiers);
4651 Sema &S = state.getSema();
4661 bool IsTypedefName =
4677 bool IsClassTemplateDeduction = isa<DeducedTemplateSpecializationType>(DT);
4680 unsigned Index = E - I - 1;
4682 unsigned DiagId = IsClassTemplateDeduction
4683 ? diag::err_deduced_class_template_compound_type
4684 : diag::err_decltype_auto_compound_type;
4685 unsigned DiagKind = 0;
4686 switch (DeclChunk.
Kind) {
4689 if (IsClassTemplateDeduction) {
4695 if (IsClassTemplateDeduction) {
4703 DiagId = diag::err_decltype_auto_function_declarator_not_declaration;
4721 S.
Diag(DeclChunk.
Loc, DiagId) << DiagKind;
4729 std::optional<NullabilityKind> inferNullability;
4730 bool inferNullabilityCS =
false;
4731 bool inferNullabilityInnerOnly =
false;
4732 bool inferNullabilityInnerOnlyComplete =
false;
4735 bool inAssumeNonNullRegion =
false;
4737 if (assumeNonNullLoc.
isValid()) {
4738 inAssumeNonNullRegion =
true;
4752 } complainAboutMissingNullability = CAMN_No;
4753 unsigned NumPointersRemaining = 0;
4754 auto complainAboutInferringWithinChunk = PointerWrappingDeclaratorKind::None;
4756 if (IsTypedefName) {
4760 complainAboutMissingNullability = CAMN_InnerPointers;
4765 ++NumPointersRemaining;
4770 switch (chunk.
Kind) {
4778 ++NumPointersRemaining;
4786 ++NumPointersRemaining;
4792 switch (
auto context = state.getDeclarator().getContext()) {
4803 complainAboutMissingNullability = CAMN_No;
4808 if (state.getDeclarator().isObjCWeakProperty()) {
4811 complainAboutMissingNullability = CAMN_No;
4812 if (inAssumeNonNullRegion) {
4822 complainAboutMissingNullability = CAMN_Yes;
4825 auto wrappingKind = PointerWrappingDeclaratorKind::None;
4827 case PointerDeclaratorKind::NonPointer:
4828 case PointerDeclaratorKind::MultiLevelPointer:
4832 case PointerDeclaratorKind::SingleLevelPointer:
4834 if (inAssumeNonNullRegion) {
4835 complainAboutInferringWithinChunk = wrappingKind;
4842 case PointerDeclaratorKind::CFErrorRefPointer:
4843 case PointerDeclaratorKind::NSErrorPointerPointer:
4850 case PointerDeclaratorKind::MaybePointerToCFRef:
4855 auto hasCFReturnsAttr =
4857 return AttrList.hasAttribute(ParsedAttr::AT_CFReturnsRetained) ||
4858 AttrList.hasAttribute(ParsedAttr::AT_CFReturnsNotRetained);
4863 hasCFReturnsAttr(InnermostChunk->getAttrs()) ||
4866 inferNullabilityInnerOnly =
true;
4876 complainAboutMissingNullability = CAMN_Yes;
4904 auto isVaList = [&S](
QualType T) ->
bool {
4910 if (typedefTy->getDecl() == vaListTypedef)
4912 if (
auto *name = typedefTy->getDecl()->getIdentifier())
4913 if (name->isStr(
"va_list"))
4915 typedefTy = typedefTy->desugar()->getAs<
TypedefType>();
4916 }
while (typedefTy);
4922 auto inferPointerNullability =
4927 if (NumPointersRemaining > 0)
4928 --NumPointersRemaining;
4935 if (inferNullability && !inferNullabilityInnerOnlyComplete) {
4938 ? ParsedAttr::Form::ContextSensitiveKeyword()
4939 : ParsedAttr::Form::Keyword(
false ,
4945 attrs.addAtEnd(nullabilityAttr);
4947 if (inferNullabilityCS) {
4948 state.getDeclarator().getMutableDeclSpec().getObjCQualifiers()
4952 if (pointerLoc.isValid() &&
4953 complainAboutInferringWithinChunk !=
4954 PointerWrappingDeclaratorKind::None) {
4956 S.
Diag(pointerLoc, diag::warn_nullability_inferred_on_nested_type);
4957 Diag << static_cast<int>(complainAboutInferringWithinChunk);
4961 if (inferNullabilityInnerOnly)