38#include "llvm/ADT/ArrayRef.h"
39#include "llvm/ADT/SmallPtrSet.h"
40#include "llvm/ADT/SmallString.h"
41#include "llvm/IR/DerivedTypes.h"
42#include "llvm/Support/ErrorHandling.h"
57 if (D.
getContext() != DeclaratorContext::BlockLiteral ||
76 bool useExpansionLoc =
true;
77 switch (
attr.getKind()) {
78 case ParsedAttr::AT_ObjCGC:
81 case ParsedAttr::AT_ObjCOwnership:
87 useExpansionLoc =
false;
92 StringRef name =
attr.getAttrName()->getName();
97 if (useExpansionLoc && loc.
isMacroID() && II) {
98 if (II->
isStr(
"strong")) {
100 }
else if (II->
isStr(
"weak")) {
105 S.
Diag(loc, diag::warn_type_attribute_wrong_type) << name << WhichType
111#define OBJC_POINTER_TYPE_ATTRS_CASELIST \
112 case ParsedAttr::AT_ObjCGC: \
113 case ParsedAttr::AT_ObjCOwnership
116#define CALLING_CONV_ATTRS_CASELIST \
117 case ParsedAttr::AT_CDecl: \
118 case ParsedAttr::AT_FastCall: \
119 case ParsedAttr::AT_StdCall: \
120 case ParsedAttr::AT_ThisCall: \
121 case ParsedAttr::AT_RegCall: \
122 case ParsedAttr::AT_Pascal: \
123 case ParsedAttr::AT_SwiftCall: \
124 case ParsedAttr::AT_SwiftAsyncCall: \
125 case ParsedAttr::AT_VectorCall: \
126 case ParsedAttr::AT_AArch64VectorPcs: \
127 case ParsedAttr::AT_AArch64SVEPcs: \
128 case ParsedAttr::AT_AMDGPUKernelCall: \
129 case ParsedAttr::AT_MSABI: \
130 case ParsedAttr::AT_SysVABI: \
131 case ParsedAttr::AT_Pcs: \
132 case ParsedAttr::AT_IntelOclBicc: \
133 case ParsedAttr::AT_PreserveMost: \
134 case ParsedAttr::AT_PreserveAll
137#define FUNCTION_TYPE_ATTRS_CASELIST \
138 case ParsedAttr::AT_NSReturnsRetained: \
139 case ParsedAttr::AT_NoReturn: \
140 case ParsedAttr::AT_Regparm: \
141 case ParsedAttr::AT_CmseNSCall: \
142 case ParsedAttr::AT_AnyX86NoCallerSavedRegisters: \
143 case ParsedAttr::AT_AnyX86NoCfCheck: \
144 CALLING_CONV_ATTRS_CASELIST
147#define MS_TYPE_ATTRS_CASELIST \
148 case ParsedAttr::AT_Ptr32: \
149 case ParsedAttr::AT_Ptr64: \
150 case ParsedAttr::AT_SPtr: \
151 case ParsedAttr::AT_UPtr
154#define NULLABILITY_TYPE_ATTRS_CASELIST \
155 case ParsedAttr::AT_TypeNonNull: \
156 case ParsedAttr::AT_TypeNullable: \
157 case ParsedAttr::AT_TypeNullableResult: \
158 case ParsedAttr::AT_TypeNullUnspecified
163 class TypeProcessingState {
187 using TypeAttrPair = std::pair<const AttributedType*, const Attr*>;
189 bool AttrsForTypesSorted =
true;
193 llvm::DenseMap<const MacroQualifiedType *, SourceLocation> LocsForMacros;
201 : sema(sema), declarator(declarator),
204 Sema &getSema()
const {
212 bool isProcessingDeclSpec()
const {
216 unsigned getCurrentChunkIndex()
const {
220 void setCurrentChunkIndex(
unsigned idx) {
226 if (isProcessingDeclSpec())
227 return getMutableDeclSpec().getAttributes();
232 void saveDeclSpecAttrs() {
234 if (!savedAttrs.empty())
237 DeclSpec &spec = getMutableDeclSpec();
238 llvm::append_range(savedAttrs,
245 ignoredTypeAttrs.push_back(&
attr);
251 for (
auto *
Attr : ignoredTypeAttrs)
261 AttrsForTypes.push_back({cast<AttributedType>(T.
getTypePtr()), A});
262 AttrsForTypesSorted =
false;
267 QualType getBTFTagAttributedType(
const BTFTypeTagAttr *BTFAttr,
279 auto *NewAttrTy = cast<AttributedType>(T.
getTypePtr());
280 for (TypeAttrPair &A : AttrsForTypes) {
281 if (A.first == AttrTy)
284 AttrsForTypesSorted =
false;
291 if (!AttrsForTypesSorted) {
292 llvm::stable_sort(AttrsForTypes, llvm::less_first());
293 AttrsForTypesSorted =
true;
298 for (
auto It = std::partition_point(
299 AttrsForTypes.begin(), AttrsForTypes.end(),
300 [=](
const TypeAttrPair &A) { return A.first < AT; });
301 It != AttrsForTypes.end() && It->first == AT; ++It) {
303 const Attr *Result = It->second;
304 It->second =
nullptr;
309 llvm_unreachable(
"no Attr* for AttributedType*");
314 auto FoundLoc = LocsForMacros.find(MQT);
315 assert(FoundLoc != LocsForMacros.end() &&
316 "Unable to find macro expansion location for MacroQualifedType");
317 return FoundLoc->second;
322 LocsForMacros[MQT] = Loc;
325 void setParsedNoDeref(
bool parsed) { parsedNoDeref = parsed; }
327 bool didParseNoDeref()
const {
return parsedNoDeref; }
329 ~TypeProcessingState() {
330 if (savedAttrs.empty())
333 getMutableDeclSpec().getAttributes().clearListOnly();
335 getMutableDeclSpec().getAttributes().addAtEnd(AL);
339 DeclSpec &getMutableDeclSpec()
const {
380 if (
attr.getKind() == ParsedAttr::AT_ObjCGC)
382 assert(
attr.getKind() == ParsedAttr::AT_ObjCOwnership);
397 bool onlyBlockPointers) {
403 for (; i != 0; --i) {
405 switch (fnChunk.
Kind) {
421 for (--i; i != 0; --i) {
423 switch (ptrChunk.
Kind) {
433 if (onlyBlockPointers)
442 llvm_unreachable(
"bad declarator chunk kind");
448 llvm_unreachable(
"bad declarator chunk kind");
465 Declarator &declarator = state.getDeclarator();
468 for (
unsigned i = state.getCurrentChunkIndex(); i != 0; --i) {
470 switch (chunk.
Kind) {
476 if (state.isProcessingDeclSpec() &&
477 attr.getKind() == ParsedAttr::AT_ObjCOwnership)
480 if (!destChunk) destChunk = &chunk;
493 if (state.isProcessingDeclSpec() &&
494 attr.getKind() == ParsedAttr::AT_ObjCOwnership) {
521 Declarator &declarator = state.getDeclarator();
525 unsigned innermost = -1U;
526 bool considerDeclSpec =
true;
529 switch (chunk.
Kind) {
543 considerDeclSpec =
false;
551 if (considerDeclSpec) {
556 state.saveDeclSpecAttrs();
565 if (innermost != -1U) {
573 state.addIgnoredTypeAttr(
attr);
582 Declarator &declarator = state.getDeclarator();
586 for (
unsigned i = state.getCurrentChunkIndex(); i != 0; --i) {
588 switch (chunk.
Kind) {
614 Declarator &declarator = state.getDeclarator();
633 state.saveDeclSpecAttrs();
637 state,
attr, state.getCurrentAttributes(), declSpecType))
642 state.addIgnoredTypeAttr(
attr);
652 Declarator &declarator = state.getDeclarator();
662 state.addIgnoredTypeAttr(
attr);
686 if (
attr.isStandardAttributeSyntax())
689 switch (
attr.getKind()) {
706 case ParsedAttr::AT_ObjCKindOf:
719 Declarator &declarator = state.getDeclarator();
765 std::nullopt, loc, loc, declarator));
780 typedef std::pair<DeclSpec::TQ, SourceLocation> QualLoc;
785 if (!(RemoveTQs & Qual.first))
789 if (TypeQuals & Qual.first)
790 S.
Diag(Qual.second, DiagID)
795 TypeQuals &= ~Qual.first;
809 if (AL.isInvalid() || !AL.isTypeAttr())
812 diag::warn_block_literal_attributes_on_omitted_return_type)
814 ToBeRemoved.push_back(&AL);
824 diag::warn_block_literal_qualifiers_on_omitted_return_type);
837 if (!objcObjectType || !objcObjectType->getInterface()) {
838 S.
Diag(loc, diag::err_objc_type_args_non_class)
851 S.
Diag(loc, diag::err_objc_type_args_non_parameterized_class)
862 if (objcObjectType->isSpecialized()) {
863 S.
Diag(loc, diag::err_objc_type_args_specialized_class)
875 unsigned numTypeParams = typeParams->
size();
876 bool anyPackExpansions =
false;
877 for (
unsigned i = 0, n = typeArgs.size(); i != n; ++i) {
885 bool diagnosed =
false;
888 rangeToRemove =
attr.getLocalSourceRange();
889 if (
attr.getTypePtr()->getImmediateNullability()) {
890 typeArg =
attr.getTypePtr()->getModifiedType();
892 diag::err_objc_type_arg_explicit_nullability)
900 if (!rebuilding && !diagnosed) {
901 S.
Diag(qual.getBeginLoc(), diag::err_objc_type_arg_qualified)
910 finalTypeArgs.push_back(typeArg);
913 anyPackExpansions =
true;
917 if (!anyPackExpansions) {
918 if (i < numTypeParams) {
919 typeParam = typeParams->
begin()[i];
922 S.
Diag(loc, diag::err_objc_type_args_wrong_arity)
943 assert(anyPackExpansions &&
"Too many arguments?");
952 if (typeArgObjC->isObjCIdType()) {
955 if (boundObjC->isObjCIdType())
964 diag::err_objc_type_arg_does_not_match_bound)
981 assert(anyPackExpansions &&
"Too many arguments?");
992 diag::err_objc_type_arg_does_not_match_bound)
1010 diag::err_objc_type_arg_not_id_compatible)
1020 if (!anyPackExpansions && finalTypeArgs.size() != numTypeParams) {
1021 S.
Diag(loc, diag::err_objc_type_args_wrong_arity)
1022 << (typeArgs.size() < typeParams->
size())
1046 if (!Protocols.empty()) {
1052 <<
SourceRange(ProtocolLAngleLoc, ProtocolRAngleLoc);
1055 if (FailOnError &&
Result.isNull())
1067 bool FailOnError,
bool Rebuilding) {
1069 if (!TypeArgs.empty()) {
1072 SourceRange(TypeArgsLAngleLoc, TypeArgsRAngleLoc),
1073 FailOnError, Rebuilding);
1074 if (FailOnError &&
Result.isNull())
1078 if (!Protocols.empty()) {
1083 Diag(Loc, diag::err_invalid_protocol_qualifiers)
1084 <<
SourceRange(ProtocolLAngleLoc, ProtocolRAngleLoc);
1087 if (FailOnError &&
Result.isNull())
1113 auto ObjCObjectTL = ObjCObjectPointerTL.getPointeeLoc()
1123 ObjCObjectTL.setProtocolLAngleLoc(lAngleLoc);
1124 ObjCObjectTL.setProtocolRAngleLoc(rAngleLoc);
1125 for (
unsigned i = 0, n = protocols.size(); i != n; ++i)
1126 ObjCObjectTL.setProtocolLoc(i, protocolLocs[i]);
1154 for (
unsigned i = 0, n = TypeArgs.size(); i != n; ++i) {
1158 ActualTypeArgInfos.clear();
1162 assert(TypeArgInfo &&
"No type source info?");
1163 ActualTypeArgInfos.push_back(TypeArgInfo);
1169 TypeArgsLAngleLoc, ActualTypeArgInfos, TypeArgsRAngleLoc,
1173 ProtocolLocs, ProtocolRAngleLoc,
1189 ResultTL = ObjCObjectPointerTL.getPointeeLoc();
1194 if (OTPTL.getNumProtocols() > 0) {
1195 assert(OTPTL.getNumProtocols() == Protocols.size());
1196 OTPTL.setProtocolLAngleLoc(ProtocolLAngleLoc);
1197 OTPTL.setProtocolRAngleLoc(ProtocolRAngleLoc);
1198 for (
unsigned i = 0, n = Protocols.size(); i != n; ++i)
1199 OTPTL.setProtocolLoc(i, ProtocolLocs[i]);
1209 if (ObjCObjectTL.getNumTypeArgs() > 0) {
1210 assert(ObjCObjectTL.getNumTypeArgs() == ActualTypeArgInfos.size());
1211 ObjCObjectTL.setTypeArgsLAngleLoc(TypeArgsLAngleLoc);
1212 ObjCObjectTL.setTypeArgsRAngleLoc(TypeArgsRAngleLoc);
1213 for (
unsigned i = 0, n = ActualTypeArgInfos.size(); i != n; ++i)
1214 ObjCObjectTL.setTypeArgTInfo(i, ActualTypeArgInfos[i]);
1221 if (ObjCObjectTL.getNumProtocols() > 0) {
1222 assert(ObjCObjectTL.getNumProtocols() == Protocols.size());
1223 ObjCObjectTL.setProtocolLAngleLoc(ProtocolLAngleLoc);
1224 ObjCObjectTL.setProtocolRAngleLoc(ProtocolRAngleLoc);
1225 for (
unsigned i = 0, n = Protocols.size(); i != n; ++i)
1226 ObjCObjectTL.setProtocolLoc(i, ProtocolLocs[i]);
1233 ObjCObjectTL.setHasBaseTypeAsWritten(
true);
1234 if (ObjCObjectTL.getType() == T)
1235 ObjCObjectTL.getBaseLoc().initializeFullCopy(BaseTypeInfo->
getTypeLoc());
1237 ObjCObjectTL.getBaseLoc().initialize(
Context, Loc);
1243static OpenCLAccessAttr::Spelling
1246 if (AL.getKind() == ParsedAttr::AT_OpenCLAccess)
1247 return static_cast<OpenCLAccessAttr::Spelling
>(AL.getSemanticSpelling());
1248 return OpenCLAccessAttr::Keyword_read_only;
1253 switch (SwitchTST) {
1254#define TRANSFORM_TYPE_TRAIT_DEF(Enum, Trait) \
1256 return UnaryTransformType::Enum;
1257#include "clang/Basic/TransformTypeTraits.def"
1259 llvm_unreachable(
"attempted to parse a non-unary transform builtin");
1273 Sema &S = state.getSema();
1274 Declarator &declarator = state.getDeclarator();
1294 "Unknown TSS value");
1308 "Unknown TSS value");
1317 "Unknown TSS value");
1322 "Unknown TSS value");
1327 "Unknown TSS value");
1355 S.
Diag(DeclLoc, diag::warn_missing_type_specifier)
1364 S.
Diag(DeclLoc, diag::err_missing_type_specifier)
1373 S.
Diag(DeclLoc, diag::err_missing_actual_pipe_type)
1378 "implicit int is disabled?");
1379 S.
Diag(DeclLoc, diag::ext_missing_type_specifier)
1406 diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong);
1431 diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong);
1464 llvm_unreachable(
"Unable to specify long long as _Accum width");
1487 llvm_unreachable(
"Unable to specify long long as _Fract width");
1538 ?
"cl_khr_fp64 and __opencl_c_fp64"
1588 "No qualifiers on tag names!");
1604 "Can't handle qualifiers on typedef names yet!");
1617 assert(!
Result.isNull() &&
"Didn't get a type for typeof?");
1618 if (!
Result->isDependentType())
1630 assert(E &&
"Didn't get an expression for typeof?");
1644 assert(E &&
"Didn't get an expression for decltype?");
1653#define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) case DeclSpec::TST_##Trait:
1654#include "clang/Basic/TransformTypeTraits.def"
1656 assert(!
Result.isNull() &&
"Didn't get a type for the transformation?");
1676 TypeConstraintConcept =
1677 cast<ConceptDecl>(TemplateId->Template.get().getAsTemplateDecl());
1682 TemplateId->NumArgs);
1684 for (
const auto &ArgLoc : TemplateArgsInfo.
arguments())
1685 TemplateArgs.push_back(ArgLoc.getArgument());
1692 TypeConstraintConcept, TemplateArgs);
1706 assert(!
Result.isNull() &&
"Didn't get a type for _Atomic?");
1714#define GENERIC_IMAGE_TYPE(ImgType, Id) \
1715 case DeclSpec::TST_##ImgType##_t: \
1716 switch (getImageAccess(DS.getAttributes())) { \
1717 case OpenCLAccessAttr::Keyword_write_only: \
1718 Result = Context.Id##WOTy; \
1720 case OpenCLAccessAttr::Keyword_read_write: \
1721 Result = Context.Id##RWTy; \
1723 case OpenCLAccessAttr::Keyword_read_only: \
1724 Result = Context.Id##ROTy; \
1726 case OpenCLAccessAttr::SpellingNotCalculated: \
1727 llvm_unreachable("Spelling not yet calculated"); \
1730#include "clang/Basic/OpenCLImageTypes.def"
1741 if (
Result->containsErrors())
1746 bool IsOpenCLC30Compatible =
1756 (IsOpenCLC30Compatible &&
1759 << 0 <<
Result <<
"__opencl_c_images";
1761 }
else if (
Result->isOCLImage3dWOType() &&
1766 << (IsOpenCLC30Compatible
1767 ?
"cl_khr_3d_image_writes and __opencl_c_3d_image_writes"
1768 :
"cl_khr_3d_image_writes");
1789 assert(typeSize > 0 &&
"type size for vector must be greater than 0 bits");
1818 if (AL.slidesFromDeclToDeclSpecLegacyBehavior()) {
1829 if (AL.isStandardAttributeSyntax() && AL.isClangScope() &&
1830 !(AL.getKind() == ParsedAttr::AT_MatrixType &&
1832 S.
Diag(AL.getLoc(), diag::warn_type_attribute_deprecated_on_decl)
1858 if (
Result->isFunctionType()) {
1862 ? diag::warn_typecheck_function_qualifiers_ignored
1863 : diag::warn_typecheck_function_qualifiers_unspecified);
1876 if (TypeQuals &&
Result->isReferenceType()) {
1878 S, DS, TypeQuals,
Result,
1880 diag::warn_typecheck_reference_qualifiers);
1887 && TypeQuals &
Result.getCVRQualifiers()) {
1911 assert(!
Result.isNull() &&
"This function should not return a null type");
1926 const auto *AT = dyn_cast<AutoType>(T);
1927 return AT && AT->isGNUAutoType();
1944 unsigned DiagID = 0;
1960 DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
1967 DiagID = diag::err_typecheck_invalid_restrict_not_pointer;
1981 unsigned CVRAU,
const DeclSpec *DS) {
2015 Split.Quals.addCVRQualifiers(CVR);
2034 if (!
type->isObjCLifetimeType() ||
2044 if (
type.isConstQualified()) {
2050 }
else if (
type->isObjCARCImplicitlyUnretainedType()) {
2068 diag::err_arc_indirect_no_ownership,
type, isReference));
2070 S.
Diag(loc, diag::err_arc_indirect_no_ownership) <<
type << isReference;
2074 assert(implicitLifetime &&
"didn't infer any lifetime!");
2115enum QualifiedFunctionKind { QFK_BlockPointer, QFK_Pointer, QFK_Reference };
2121 QualifiedFunctionKind QFK) {
2128 S.
Diag(Loc, diag::err_compound_qualified_function_type)
2140 Diag(Loc, diag::err_qualified_function_typeid)
2172 Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
2180 Diag(Loc, diag::err_opencl_function_pointer) << 0;
2185 Diag(Loc, diag::err_hlsl_pointers_unsupported) << 0;
2204 Diag(Loc, diag::err_wasm_reference_pr) << 0;
2229 "Unresolved overloaded function type");
2257 Diag(Loc, diag::err_reference_to_void);
2262 Diag(Loc, diag::err_hlsl_pointers_unsupported) << 1;
2272 Diag(Loc, diag::err_opencl_function_pointer) << 1;
2286 Diag(Loc, diag::err_wasm_reference_pr) << 1;
2333 llvm::APSInt Bits(32);
2340 size_t NumBits = Bits.getZExtValue();
2341 if (!IsUnsigned && NumBits < 2) {
2342 Diag(Loc, diag::err_bit_int_bad_size) << 0;
2346 if (IsUnsigned && NumBits < 1) {
2347 Diag(Loc, diag::err_bit_int_bad_size) << 1;
2353 Diag(Loc, diag::err_bit_int_max_size)
2367 llvm::APSInt &SizeVal,
unsigned VLADiag,
2392 VLADiagnoser(
unsigned VLADiag,
bool VLAIsError)
2393 : VLADiag(VLADiag), VLAIsError(VLAIsError) {}
2397 return S.
Diag(Loc, diag::err_array_size_non_int) << T;
2402 IsVLA = !VLAIsError;
2403 return S.
Diag(Loc, VLADiag);
2408 return S.
Diag(Loc, diag::ext_vla_folded_to_constant);
2410 } Diagnoser(VLADiag, VLAIsError);
2414 if (Diagnoser.IsVLA)
2428 if (Size.isMultipleOf(Alignment))
2431 Diag(Loc, diag::err_array_element_alignment)
2432 << EltTy << Size.getQuantity() << Alignment.
getQuantity();
2452 Expr *ArraySize,
unsigned Quals,
2469 Diag(Loc, diag::err_illegal_decl_array_of_references)
2475 Diag(Loc, diag::err_array_incomplete_or_sizeless_type) << 0 << T;
2480 diag::err_array_of_abstract_type))
2487 if (!MPTy->getClass()->isDependentType())
2494 diag::err_array_incomplete_or_sizeless_type))
2499 Diag(Loc, diag::err_array_incomplete_or_sizeless_type) << 1 << T;
2504 Diag(Loc, diag::err_illegal_decl_array_of_functions)
2512 if (EltTy->getDecl()->hasFlexibleArrayMember())
2513 Diag(Loc, diag::ext_flexible_array_in_array) << T;
2515 Diag(Loc, diag::err_objc_array_of_interfaces) << T;
2526 ArraySize =
Result.get();
2530 if (ArraySize && !ArraySize->
isPRValue()) {
2535 ArraySize =
Result.get();
2553 VLADiag = diag::err_opencl_vla;
2556 VLADiag = diag::warn_vla_used;
2559 VLADiag = diag::err_vla_in_sfinae;
2562 VLADiag = diag::err_openmp_vla_in_task_untied;
2565 VLADiag = diag::ext_vla;
2606 if (ConstVal.isSigned() && ConstVal.isNegative()) {
2613 diag::err_typecheck_negative_array_size)
2617 if (ConstVal == 0) {
2622 : diag::ext_typecheck_zero_array_size)
2627 unsigned ActiveSizeBits =
2631 : ConstVal.getActiveBits();
2646 IsCUDADevice ? diag::err_cuda_vla : diag::err_vla_unsupported)
2654 : diag::ext_c99_array_usage)
2665 Diag(Loc, diag::err_opencl_invalid_type_array) << ArrType;
2682 Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << CurType;
2688 if (!llvm::isPowerOf2_32(NumBits) || NumBits < 8) {
2689 Diag(AttrLoc, diag::err_attribute_invalid_bitint_vector_type)
2699 std::optional<llvm::APSInt> VecSize =
2702 Diag(AttrLoc, diag::err_attribute_argument_type)
2713 if (!VecSize->isIntN(61)) {
2715 Diag(AttrLoc, diag::err_attribute_size_too_large)
2719 uint64_t VectorSizeBits = VecSize->getZExtValue() * 8;
2722 if (VectorSizeBits == 0) {
2723 Diag(AttrLoc, diag::err_attribute_zero_size)
2728 if (!TypeSize || VectorSizeBits % TypeSize) {
2729 Diag(AttrLoc, diag::err_attribute_invalid_size)
2734 if (VectorSizeBits / TypeSize > std::numeric_limits<uint32_t>::max()) {
2735 Diag(AttrLoc, diag::err_attribute_size_too_large)
2762 Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
2769 if (!llvm::isPowerOf2_32(NumBits) || NumBits < 8) {
2770 Diag(AttrLoc, diag::err_attribute_invalid_bitint_vector_type)
2777 std::optional<llvm::APSInt> vecSize =
2780 Diag(AttrLoc, diag::err_attribute_argument_type)
2786 if (!vecSize->isIntN(32)) {
2787 Diag(AttrLoc, diag::err_attribute_size_too_large)
2793 unsigned vectorSize =
static_cast<unsigned>(vecSize->getZExtValue());
2795 if (vectorSize == 0) {
2796 Diag(AttrLoc, diag::err_attribute_zero_size)
2810 "Should never build a matrix type when it is disabled");
2815 Diag(AttrLoc, diag::err_attribute_invalid_matrix_type) << ElementTy;
2824 std::optional<llvm::APSInt> ValueRows =
2826 std::optional<llvm::APSInt> ValueColumns =
2833 if (!ValueRows && !ValueColumns) {
2834 Diag(AttrLoc, diag::err_attribute_argument_type)
2842 Diag(AttrLoc, diag::err_attribute_argument_type)
2848 if (!ValueColumns) {
2849 Diag(AttrLoc, diag::err_attribute_argument_type)
2855 unsigned MatrixRows =
static_cast<unsigned>(ValueRows->getZExtValue());
2856 unsigned MatrixColumns =
static_cast<unsigned>(ValueColumns->getZExtValue());
2857 if (MatrixRows == 0 && MatrixColumns == 0) {
2858 Diag(AttrLoc, diag::err_attribute_zero_size)
2859 <<
"matrix" << RowRange << ColRange;
2862 if (MatrixRows == 0) {
2863 Diag(AttrLoc, diag::err_attribute_zero_size) <<
"matrix" << RowRange;
2866 if (MatrixColumns == 0) {
2867 Diag(AttrLoc, diag::err_attribute_zero_size) <<
"matrix" << ColRange;
2871 Diag(AttrLoc, diag::err_attribute_size_too_large)
2872 << RowRange <<
"matrix row";
2876 Diag(AttrLoc, diag::err_attribute_size_too_large)
2877 << ColRange <<
"matrix column";
2885 Diag(Loc, diag::err_func_returning_array_function)
2893 Diag(Loc, diag::err_parameters_retval_cannot_have_fp16_type) << 1 <<
2901 Diag(Loc, diag::err_object_cannot_be_passed_returned_by_value)
2914 Diag(Loc, diag::warn_deprecated_volatile_return) << T;
2929 bool emittedError =
false;
2931 enum class RequiredCC { OnlySwift, SwiftOrSwiftAsync };
2932 auto checkCompatible = [&](
unsigned paramIndex, RequiredCC required) {
2934 (required == RequiredCC::OnlySwift)
2937 if (isCompatible || emittedError)
2939 S.
Diag(getParamLoc(paramIndex), diag::err_swift_param_attr_not_swiftcall)
2941 << (required == RequiredCC::OnlySwift);
2942 emittedError =
true;
2944 for (
size_t paramIndex = 0, numParams = paramTypes.size();
2945 paramIndex != numParams; ++paramIndex) {
2954 checkCompatible(paramIndex, RequiredCC::SwiftOrSwiftAsync);
2955 if (paramIndex != 0 &&
2958 S.
Diag(getParamLoc(paramIndex),
2959 diag::err_swift_indirect_result_not_first);
2964 checkCompatible(paramIndex, RequiredCC::SwiftOrSwiftAsync);
2973 checkCompatible(paramIndex, RequiredCC::OnlySwift);
2974 if (paramIndex == 0 ||
2977 S.
Diag(getParamLoc(paramIndex),
2978 diag::err_swift_error_result_not_after_swift_context);
2982 llvm_unreachable(
"bad ABI kind");
2994 for (
unsigned Idx = 0, Cnt = ParamTypes.size(); Idx < Cnt; ++Idx) {
2998 Diag(Loc, diag::err_param_with_void_type);
3003 Diag(Loc, diag::err_parameters_retval_cannot_have_fp16_type) << 0 <<
3011 Diag(Loc, diag::warn_deprecated_volatile_param) << ParamType;
3013 ParamTypes[Idx] = ParamType;
3018 [=](
unsigned i) {
return Loc; });
3047 Diag(Loc, diag::err_distant_exception_spec);
3054 Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
3060 Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
3065 if (!Class->isDependentType() && !Class->isRecordType()) {
3066 Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
3073 Diag(Loc, diag::err_opencl_function_pointer) << 0;
3078 Diag(Loc, diag::err_hlsl_pointers_unsupported) << 0;
3108 Diag(Loc, diag::err_nonfunction_block_type);
3124 if (TInfo) *TInfo =
nullptr;
3129 if (
const LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) {
3130 QT = LIT->getType();
3131 DI = LIT->getTypeSourceInfo();
3134 if (TInfo) *TInfo = DI;
3140 unsigned chunkIndex);
3147 Sema &S = state.getSema();
3148 Declarator &declarator = state.getDeclarator();
3154 unsigned outermostPointerIndex = 0;
3156 unsigned numPointers = 0;
3158 unsigned chunkIndex = i;
3160 switch (chunk.
Kind) {
3170 outermostPointerIndex = chunkIndex;
3178 if (numPointers != 1)
return;
3180 outermostPointerIndex = chunkIndex;
3198 if (numPointers == 1) {
3216 }
else if (numPointers == 2) {
3229 if (AL.getKind() == ParsedAttr::AT_ObjCOwnership)
3233 outermostPointerIndex);
3255 }
const QualKinds[5] = {
3264 unsigned NumQuals = 0;
3269 for (
auto &E : QualKinds) {
3270 if (Quals & E.Mask) {
3271 if (!QualStr.empty()) QualStr +=
' ';
3288 << QualStr << NumQuals << FixIts[0] << FixIts[1] << FixIts[2] << FixIts[3];
3294 unsigned FunctionChunkIndex) {
3304 for (
unsigned OuterChunkIndex = FunctionChunkIndex + 1,
3306 OuterChunkIndex != End; ++OuterChunkIndex) {
3308 switch (OuterChunk.
Kind) {
3315 diag::warn_qual_return_type,
3341 llvm_unreachable(
"unknown declarator chunk kind");
3362static std::pair<QualType, TypeSourceInfo *>
3366 Sema &S = state.getSema();
3370 const unsigned AutoParameterPosition = Info.
TemplateParams.size();
3384 TemplateParameterDepth, AutoParameterPosition,
3387 IsParameterPack,
Auto->isConstrained());
3392 if (
Auto->isConstrained()) {
3399 for (
unsigned Idx = 0; Idx < AutoLoc.
getNumArgs(); ++Idx) {
3452 QualType NewT = state.ReplaceAutoType(T, Replacement);
3456 return {NewT, NewTSI};
3465 Sema &SemaRef = state.getSema();
3468 ReturnTypeInfo =
nullptr;
3471 TagDecl *OwnedTagDecl =
nullptr;
3521 bool DeducedIsTrailingReturnType =
false;
3525 DeducedIsTrailingReturnType =
true;
3535 bool IsCXXAutoType =
3537 bool IsDeducedReturnType =
false;
3574 assert(Info &&
"No LambdaScopeInfo on the stack!");
3580 if (!DeducedIsTrailingReturnType)
3589 if (isa<ObjCContainerDecl>(SemaRef.
CurContext)) {
3592 switch (cast<TagDecl>(SemaRef.
CurContext)->getTagKind()) {
3593 case TTK_Enum: llvm_unreachable(
"unhandled tag kind");
3609 if (isa<DeducedTemplateSpecializationType>(Deduced) &&
3621 if (isa<DeducedTemplateSpecializationType>(Deduced) &&
3635 if (!SemaRef.
getLangOpts().CPlusPlus14 || !IsCXXAutoType)
3637 IsDeducedReturnType =
true;
3640 if (!SemaRef.
getLangOpts().CPlusPlus14 || !IsCXXAutoType)
3642 IsDeducedReturnType =
true;
3645 if (isa<DeducedTemplateSpecializationType>(Deduced))
3647 if (SemaRef.
getLangOpts().CPlusPlus2b && IsCXXAutoType &&
3648 !
Auto->isDecltypeAuto())
3667 if (!IsCXXAutoType && !isa<DeducedTemplateSpecializationType>(Deduced))
3681 (!SemaRef.
getLangOpts().CPlusPlus11 || !IsCXXAutoType))
3691 switch (
Auto->getKeyword()) {
3697 assert(isa<DeducedTemplateSpecializationType>(Deduced) &&
3698 "unknown auto type");
3702 auto *DTST = dyn_cast<DeducedTemplateSpecializationType>(Deduced);
3705 SemaRef.
Diag(AutoRange.
getBegin(), diag::err_auto_not_allowed)
3707 <<
QualType(Deduced, 0) << AutoRange;
3709 SemaRef.
Diag(TD->getLocation(), diag::note_template_decl_here);
3718 ? diag::warn_cxx11_compat_generic_lambda
3719 : IsDeducedReturnType
3720 ? diag::warn_cxx11_compat_deduced_return_type
3721 : diag::warn_cxx98_compat_auto_type_specifier)
3730 unsigned DiagID = 0;
3736 llvm_unreachable(
"parser should not have allowed this");
3752 DiagID = diag::err_type_defined_in_alias_template;
3764 DiagID = diag::err_type_defined_in_type_specifier;
3774 DiagID = diag::err_type_defined_in_param_type;
3780 DiagID = diag::err_type_defined_in_condition;
3791 assert(!T.
isNull() &&
"This function should not return a null type");
3800 assert(FTI.
isAmbiguous &&
"no direct-initializer / function ambiguity");
3830 FTI.
NumParams ? diag::warn_parens_disambiguated_as_function_declaration
3831 : diag::warn_empty_parens_are_function_decl)
3842 if (Comma.getFileID() != Name.getFileID() ||
3843 Comma.getSpellingLineNumber() != Name.getSpellingLineNumber()) {
3850 Result.suppressDiagnostics();
3863 S.
Diag(B, diag::note_additional_parens_for_variable_declaration)
3878 S.
Diag(DeclType.
Loc, diag::note_empty_parens_default_ctor)
3883 if (Init.empty() && S.
LangOpts.CPlusPlus11)
3886 S.
Diag(DeclType.
Loc, diag::note_empty_parens_zero_initialize)
3897 "do not have redundant top-level parentheses");
3906 bool CouldBeTemporaryObject =
3913 bool StartsWithDeclaratorId =
true;
3921 StartsWithDeclaratorId =
false;
3926 CouldBeTemporaryObject =
false;
3934 CouldBeTemporaryObject =
false;
3935 StartsWithDeclaratorId =
false;
3945 CouldBeTemporaryObject =
false;
3952 CouldBeTemporaryObject =
false;
3953 StartsWithDeclaratorId =
false;
3963 if (CouldBeTemporaryObject) {
3967 CouldBeTemporaryObject =
false;
3968 Result.suppressDiagnostics();
3973 if (!CouldBeTemporaryObject) {
3986 S.
Diag(
Paren.Loc, diag::warn_redundant_parens_around_declarator)
3992 S.
Diag(
Paren.Loc, diag::warn_parens_disambiguated_as_variable_declaration)
3995 if (!RD || !RD->hasDefinition() || RD->hasNonTrivialDestructor())
3996 S.
Diag(
Paren.Loc, diag::note_raii_guard_add_name)
4001 S.
Diag(D.
getBeginLoc(), diag::note_function_style_cast_add_parentheses)
4004 S.
Diag(
Paren.Loc, diag::note_remove_parens_for_variable_declaration)
4020 switch (AL.getKind()) {
4038 bool IsCXXInstanceMethod =
false;
4044 unsigned I = ChunkIndex;
4045 bool FoundNonParen =
false;
4046 while (I && !FoundNonParen) {
4049 FoundNonParen =
true;
4052 if (FoundNonParen) {
4055 IsCXXInstanceMethod =
4060 IsCXXInstanceMethod =
true;
4067 IsCXXInstanceMethod =
4075 IsCXXInstanceMethod);
4083 if (AL.getKind() == ParsedAttr::AT_OpenCLKernel) {
4093 if (Triple.getArch() == llvm::Triple::spirv32 ||
4094 Triple.getArch() == llvm::Triple::spirv64) {
4096 if (AL.getKind() == ParsedAttr::AT_CUDAGlobal) {
4110 enum class SimplePointerKind {
4119 switch (nullability) {
4121 if (!Ident__Nonnull)
4123 return Ident__Nonnull;
4126 if (!Ident__Nullable)
4128 return Ident__Nullable;
4131 if (!Ident__Nullable_result)
4133 return Ident__Nullable_result;
4136 if (!Ident__Null_unspecified)
4138 return Ident__Null_unspecified;
4140 llvm_unreachable(
"Unknown nullability kind.");
4148 return Ident_NSError;
4155 if (AL.getKind() == ParsedAttr::AT_TypeNonNull ||
4156 AL.getKind() == ParsedAttr::AT_TypeNullable ||
4157 AL.getKind() == ParsedAttr::AT_TypeNullableResult ||
4158 AL.getKind() == ParsedAttr::AT_TypeNullUnspecified)
4167 enum class PointerDeclaratorKind {
4175 MaybePointerToCFRef,
4179 NSErrorPointerPointer,
4185 enum class PointerWrappingDeclaratorKind {
4199static PointerDeclaratorKind
4201 PointerWrappingDeclaratorKind &wrappingKind) {
4202 unsigned numNormalPointers = 0;
4205 if (
type->isDependentType())
4206 return PointerDeclaratorKind::NonPointer;
4211 switch (chunk.
Kind) {
4213 if (numNormalPointers == 0)
4214 wrappingKind = PointerWrappingDeclaratorKind::Array;
4223 return numNormalPointers > 0 ? PointerDeclaratorKind::MultiLevelPointer
4224 : PointerDeclaratorKind::SingleLevelPointer;
4230 if (numNormalPointers == 0)
4231 wrappingKind = PointerWrappingDeclaratorKind::Reference;
4235 ++numNormalPointers;
4236 if (numNormalPointers > 2)
4237 return PointerDeclaratorKind::MultiLevelPointer;
4243 unsigned numTypeSpecifierPointers = 0;
4247 ++numNormalPointers;
4249 if (numNormalPointers > 2)
4250 return PointerDeclaratorKind::MultiLevelPointer;
4252 type = ptrType->getPointeeType();
4253 ++numTypeSpecifierPointers;
4259 return numNormalPointers > 0 ? PointerDeclaratorKind::MultiLevelPointer
4260 : PointerDeclaratorKind::SingleLevelPointer;
4265 return numNormalPointers > 0 ? PointerDeclaratorKind::MultiLevelPointer
4266 : PointerDeclaratorKind::SingleLevelPointer;
4271 ++numNormalPointers;
4272 ++numTypeSpecifierPointers;
4275 if (
auto objcClassDecl = objcObjectPtr->getInterfaceDecl()) {
4277 numNormalPointers == 2 && numTypeSpecifierPointers < 2) {
4278 return PointerDeclaratorKind::NSErrorPointerPointer;
4287 if (objcClass->getInterface()->getIdentifier() == S.
getNSErrorIdent()) {
4288 if (numNormalPointers == 2 && numTypeSpecifierPointers < 2)
4289 return PointerDeclaratorKind::NSErrorPointerPointer;
4296 if (numNormalPointers == 0)
4297 return PointerDeclaratorKind::NonPointer;
4303 if (numNormalPointers == 2 && numTypeSpecifierPointers < 2 &&
4305 return PointerDeclaratorKind::CFErrorRefPointer;
4313 switch (numNormalPointers) {
4315 return PointerDeclaratorKind::NonPointer;
4318 return PointerDeclaratorKind::SingleLevelPointer;
4321 return PointerDeclaratorKind::MaybePointerToCFRef;
4324 return PointerDeclaratorKind::MultiLevelPointer;
4339 if (
auto bridgeAttr = RD->
getAttr<ObjCBridgeAttr>())
4340 bridgedType = bridgeAttr->getBridgedType();
4341 else if (
auto bridgeAttr = RD->
getAttr<ObjCBridgeMutableAttr>())
4342 bridgedType = bridgeAttr->getBridgedType();
4358 if (ctx->isFunctionOrMethod())
4361 if (ctx->isFileContext())
4372 bool invalid =
false;
4374 if (invalid || !sloc.
isFile())
4392template <
typename DiagBuilderT>
4401 if (!FixItLoc.
isValid() || FixItLoc == PointerLoc)
4410 InsertionTextBuf +=
" ";
4411 StringRef InsertionText = InsertionTextBuf.str();
4414 InsertionText = InsertionText.drop_back();
4415 }
else if (NextChar[-1] ==
'[') {
4416 if (NextChar[0] ==
']')
4417 InsertionText = InsertionText.drop_back().drop_front();
4419 InsertionText = InsertionText.drop_front();
4422 InsertionText = InsertionText.drop_back().drop_front();
4429 SimplePointerKind PointerKind,
4434 if (PointerKind == SimplePointerKind::Array) {
4435 S.
Diag(PointerLoc, diag::warn_nullability_missing_array);
4437 S.
Diag(PointerLoc, diag::warn_nullability_missing)
4438 <<
static_cast<unsigned>(PointerKind);
4441 auto FixItLoc = PointerEndLoc.
isValid() ? PointerEndLoc : PointerLoc;
4442 if (FixItLoc.isMacroID())
4446 auto Diag = S.
Diag(FixItLoc, diag::note_nullability_fix_it);
4447 Diag << static_cast<unsigned>(Nullability);
4448 Diag << static_cast<unsigned>(PointerKind);
4477 if (pointerKind == SimplePointerKind::Array)
4478 diagKind = diag::warn_nullability_missing_array;
4480 diagKind = diag::warn_nullability_missing;
4486 fileNullability.
PointerKind =
static_cast<unsigned>(pointerKind);
4518 auto kind =
static_cast<SimplePointerKind
>(fileNullability.
PointerKind);
4533 unsigned i = endIndex;
4561template<
typename AttrT>
4564 return ::new (Ctx) AttrT(Ctx, AL);
4571 return createSimpleAttr<TypeNonNullAttr>(Ctx,
Attr);
4574 return createSimpleAttr<TypeNullableAttr>(Ctx,
Attr);
4577 return createSimpleAttr<TypeNullableResultAttr>(Ctx,
Attr);
4580 return createSimpleAttr<TypeNullUnspecifiedAttr>(Ctx,
Attr);
4582 llvm_unreachable(
"unknown NullabilityKind");
4593 if (ASOld != ASNew) {
4594 S.
Diag(AttrLoc, diag::err_attribute_address_multiple_qualifiers);
4599 diag::warn_attribute_address_multiple_identical_qualifiers);
4611 Sema &S = state.getSema();
4621 bool IsTypedefName =
4637 bool IsClassTemplateDeduction = isa<DeducedTemplateSpecializationType>(DT);
4640 unsigned Index = E - I - 1;
4642 unsigned DiagId = IsClassTemplateDeduction
4643 ? diag::err_deduced_class_template_compound_type
4644 : diag::err_decltype_auto_compound_type;
4645 unsigned DiagKind = 0;
4646 switch (DeclChunk.
Kind) {
4649 if (IsClassTemplateDeduction) {
4655 if (IsClassTemplateDeduction) {
4663 DiagId = diag::err_decltype_auto_function_declarator_not_declaration;
4681 S.
Diag(DeclChunk.
Loc, DiagId) << DiagKind;
4689 std::optional<NullabilityKind> inferNullability;
4690 bool inferNullabilityCS =
false;
4691 bool inferNullabilityInnerOnly =
false;
4692 bool inferNullabilityInnerOnlyComplete =
false;
4695 bool inAssumeNonNullRegion =
false;
4697 if (assumeNonNullLoc.
isValid()) {
4698 inAssumeNonNullRegion =
true;
4712 } complainAboutMissingNullability = CAMN_No;
4713 unsigned NumPointersRemaining = 0;
4714 auto complainAboutInferringWithinChunk = PointerWrappingDeclaratorKind::None;
4716 if (IsTypedefName) {
4720 complainAboutMissingNullability = CAMN_InnerPointers;
4725 ++NumPointersRemaining;
4730 switch (chunk.
Kind) {
4738 ++NumPointersRemaining;
4746 ++NumPointersRemaining;
4752 switch (
auto context = state.getDeclarator().getContext()) {
4763 complainAboutMissingNullability = CAMN_No;
4768 if (state.getDeclarator().isObjCWeakProperty()) {
4771 complainAboutMissingNullability = CAMN_No;
4772 if (inAssumeNonNullRegion) {
4782 complainAboutMissingNullability = CAMN_Yes;
4785 auto wrappingKind = PointerWrappingDeclaratorKind::None;
4787 case PointerDeclaratorKind::NonPointer:
4788 case PointerDeclaratorKind::MultiLevelPointer:
4792 case PointerDeclaratorKind::SingleLevelPointer:
4794 if (inAssumeNonNullRegion) {
4795 complainAboutInferringWithinChunk = wrappingKind;
4802 case PointerDeclaratorKind::CFErrorRefPointer:
4803 case PointerDeclaratorKind::NSErrorPointerPointer:
4810 case PointerDeclaratorKind::MaybePointerToCFRef:
4815 auto hasCFReturnsAttr =
4817 return AttrList.hasAttribute(ParsedAttr::AT_CFReturnsRetained) ||
4818 AttrList.hasAttribute(ParsedAttr::AT_CFReturnsNotRetained);
4823 hasCFReturnsAttr(InnermostChunk->getAttrs()) ||
4826 inferNullabilityInnerOnly =
true;
4836 complainAboutMissingNullability = CAMN_Yes;
4864 auto isVaList = [&S](
QualType T) ->
bool {
4870 if (typedefTy->getDecl() == vaListTypedef)
4872 if (
auto *name = typedefTy->getDecl()->getIdentifier())
4873 if (name->isStr(
"va_list"))
4875 typedefTy = typedefTy->desugar()->getAs<
TypedefType>();
4876 }
while (typedefTy);
4882 auto inferPointerNullability =
4887 if (NumPointersRemaining > 0)
4888 --NumPointersRemaining;
4895 if (inferNullability && !inferNullabilityInnerOnlyComplete) {
4903 attrs.addAtEnd(nullabilityAttr);
4905 if (inferNullabilityCS) {
4906 state.getDeclarator().getMutableDeclSpec().getObjCQualifiers()
4910 if (pointerLoc.isValid() &&
4911 complainAboutInferringWithinChunk !=
4912 PointerWrappingDeclaratorKind::None) {
4914 S.
Diag(pointerLoc, diag::warn_nullability_inferred_on_nested_type);
4915 Diag << static_cast<int>(complainAboutInferringWithinChunk);
4919 if (inferNullabilityInnerOnly)
4920 inferNullabilityInnerOnlyComplete =
true;
4921 return nullabilityAttr;
4926 switch (complainAboutMissingNullability) {
4930 case CAMN_InnerPointers:
4931 if (NumPointersRemaining == 0)
4948 if (NumPointersRemaining > 0)
4949 --NumPointersRemaining;
4951 SimplePointerKind pointerKind = SimplePointerKind::Pointer;
4953 pointerKind = SimplePointerKind::BlockPointer;
4955 pointerKind = SimplePointerKind::MemberPointer;
4957 if (
auto *
attr = inferPointerNullability(