clang 23.0.0git
SemaAPINotes.cpp
Go to the documentation of this file.
1//===--- SemaAPINotes.cpp - API Notes Handling ----------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the mapping from API notes to declaration attributes.
10//
11//===----------------------------------------------------------------------===//
12
13#include "TypeLocBuilder.h"
16#include "clang/AST/Decl.h"
17#include "clang/AST/DeclCXX.h"
18#include "clang/AST/DeclObjC.h"
19#include "clang/AST/TypeLoc.h"
22#include "clang/Lex/Lexer.h"
23#include "clang/Sema/SemaObjC.h"
25#include <stack>
26
27using namespace clang;
28
29namespace {
30enum class IsActive_t : bool { Inactive, Active };
31enum class IsSubstitution_t : bool { Original, Replacement };
32
33struct VersionedInfoMetadata {
34 /// An empty version refers to unversioned metadata.
35 VersionTuple Version;
36 unsigned IsActive : 1;
37 unsigned IsReplacement : 1;
38
39 VersionedInfoMetadata(VersionTuple Version, IsActive_t Active,
40 IsSubstitution_t Replacement)
41 : Version(Version), IsActive(Active == IsActive_t::Active),
42 IsReplacement(Replacement == IsSubstitution_t::Replacement) {}
43};
44} // end anonymous namespace
45
46/// Determine whether this is a multi-level pointer type.
48 QualType Pointee = Type->getPointeeType();
49 if (Pointee.isNull())
50 return false;
51
52 return Pointee->isAnyPointerType() || Pointee->isObjCObjectPointerType() ||
53 Pointee->isMemberPointerType();
54}
55
56static void applyAPINotesType(Sema &S, Decl *decl, StringRef typeString,
57 VersionedInfoMetadata metadata) {
58 if (typeString.empty())
59
60 return;
61
62 // Version-independent APINotes add "type" annotations
63 // with a versioned attribute for the client to select and apply.
65 auto *typeAttr = SwiftTypeAttr::CreateImplicit(S.Context, typeString);
66 auto *versioned = SwiftVersionedAdditionAttr::CreateImplicit(
67 S.Context, metadata.Version, typeAttr, metadata.IsReplacement);
68 decl->addAttr(versioned);
69 } else {
70 if (!metadata.IsActive)
71 return;
72 S.ApplyAPINotesType(decl, typeString);
73 }
74}
75
76/// Apply nullability to the given declaration.
77static void applyNullability(Sema &S, Decl *decl, NullabilityKind nullability,
78 VersionedInfoMetadata metadata) {
79 // Version-independent APINotes add "nullability" annotations
80 // with a versioned attribute for the client to select and apply.
82 SwiftNullabilityAttr::Kind attrNullabilityKind;
83 switch (nullability) {
85 attrNullabilityKind = SwiftNullabilityAttr::Kind::NonNull;
86 break;
88 attrNullabilityKind = SwiftNullabilityAttr::Kind::Nullable;
89 break;
91 attrNullabilityKind = SwiftNullabilityAttr::Kind::Unspecified;
92 break;
94 attrNullabilityKind = SwiftNullabilityAttr::Kind::NullableResult;
95 break;
96 }
97 auto *nullabilityAttr =
98 SwiftNullabilityAttr::CreateImplicit(S.Context, attrNullabilityKind);
99 auto *versioned = SwiftVersionedAdditionAttr::CreateImplicit(
100 S.Context, metadata.Version, nullabilityAttr, metadata.IsReplacement);
101 decl->addAttr(versioned);
102 return;
103 } else {
104 if (!metadata.IsActive)
105 return;
106
107 S.ApplyNullability(decl, nullability);
108 }
109}
110
111/// Copy a string into ASTContext-allocated memory.
112static StringRef ASTAllocateString(ASTContext &Ctx, StringRef String) {
113 void *mem = Ctx.Allocate(String.size(), alignof(char *));
114 memcpy(mem, String.data(), String.size());
115 return StringRef(static_cast<char *>(mem), String.size());
116}
117
122 /*Spelling*/ 0, /*IsAlignas*/ false,
123 /*IsRegularKeywordAttribute*/ false});
124}
125
126namespace {
127template <typename A> struct AttrKindFor {};
128
129#define ATTR(X) \
130 template <> struct AttrKindFor<X##Attr> { \
131 static const attr::Kind value = attr::X; \
132 };
133#include "clang/Basic/AttrList.inc"
134
135/// Handle an attribute introduced by API notes.
136///
137/// \param IsAddition Whether we should add a new attribute
138/// (otherwise, we might remove an existing attribute).
139/// \param CreateAttr Create the new attribute to be added.
140template <typename A>
141void handleAPINotedAttribute(
142 Sema &S, Decl *D, bool IsAddition, VersionedInfoMetadata Metadata,
143 llvm::function_ref<A *()> CreateAttr,
144 llvm::function_ref<Decl::attr_iterator(const Decl *)> GetExistingAttr) {
145 if (Metadata.IsActive) {
146 auto Existing = GetExistingAttr(D);
147 if (Existing != D->attr_end()) {
148 // Remove the existing attribute, and treat it as a superseded
149 // non-versioned attribute.
150 auto *Versioned = SwiftVersionedAdditionAttr::CreateImplicit(
151 S.Context, Metadata.Version, *Existing, /*IsReplacedByActive*/ true);
152
153 D->getAttrs().erase(Existing);
154 D->addAttr(Versioned);
155 }
156
157 // If we're supposed to add a new attribute, do so.
158 if (IsAddition) {
159 if (auto Attr = CreateAttr())
160 D->addAttr(Attr);
161 }
162
163 return;
164 }
165 if (IsAddition) {
166 if (auto Attr = CreateAttr()) {
167 auto *Versioned = SwiftVersionedAdditionAttr::CreateImplicit(
168 S.Context, Metadata.Version, Attr,
169 /*IsReplacedByActive*/ Metadata.IsReplacement);
170 D->addAttr(Versioned);
171 }
172 } else {
173 // FIXME: This isn't preserving enough information for things like
174 // availability, where we're trying to remove a /specific/ kind of
175 // attribute.
176 auto *Versioned = SwiftVersionedRemovalAttr::CreateImplicit(
177 S.Context, Metadata.Version, AttrKindFor<A>::value,
178 /*IsReplacedByActive*/ Metadata.IsReplacement);
179 D->addAttr(Versioned);
180 }
181}
182
183template <typename A>
184void handleAPINotedAttribute(Sema &S, Decl *D, bool ShouldAddAttribute,
185 VersionedInfoMetadata Metadata,
186 llvm::function_ref<A *()> CreateAttr) {
187 handleAPINotedAttribute<A>(
188 S, D, ShouldAddAttribute, Metadata, CreateAttr, [](const Decl *D) {
189 return llvm::find_if(D->attrs(),
190 [](const Attr *Next) { return isa<A>(Next); });
191 });
192}
193} // namespace
194
195template <typename A>
197 bool ShouldAddAttribute,
198 VersionedInfoMetadata Metadata) {
199 // The template argument has a default to make the "removal" case more
200 // concise; it doesn't matter /which/ attribute is being removed.
201 handleAPINotedAttribute<A>(
202 S, D, ShouldAddAttribute, Metadata,
203 [&] { return new (S.Context) A(S.Context, getPlaceholderAttrInfo()); },
204 [](const Decl *D) -> Decl::attr_iterator {
205 return llvm::find_if(D->attrs(), [](const Attr *Next) -> bool {
206 return isa<CFReturnsRetainedAttr>(Next) ||
207 isa<CFReturnsNotRetainedAttr>(Next) ||
208 isa<NSReturnsRetainedAttr>(Next) ||
209 isa<NSReturnsNotRetainedAttr>(Next) ||
210 isa<CFAuditedTransferAttr>(Next);
211 });
212 });
213}
214
216 Sema &S, Decl *D, VersionedInfoMetadata Metadata,
217 std::optional<api_notes::RetainCountConventionKind> Convention) {
218 if (!Convention)
219 return;
220 switch (*Convention) {
222 if (isa<FunctionDecl>(D)) {
224 S, D, /*shouldAddAttribute*/ true, Metadata);
225 } else {
227 S, D, /*shouldAddAttribute*/ false, Metadata);
228 }
229 break;
232 S, D, /*shouldAddAttribute*/ true, Metadata);
233 break;
236 S, D, /*shouldAddAttribute*/ true, Metadata);
237 break;
240 S, D, /*shouldAddAttribute*/ true, Metadata);
241 break;
244 S, D, /*shouldAddAttribute*/ true, Metadata);
245 break;
246 }
247}
248
249static void ProcessAPINotes(Sema &S, Decl *D,
250 const api_notes::CommonEntityInfo &Info,
251 VersionedInfoMetadata Metadata) {
252 // Availability
253 if (Info.Unavailable) {
254 handleAPINotedAttribute<UnavailableAttr>(S, D, true, Metadata, [&] {
255 return new (S.Context)
256 UnavailableAttr(S.Context, getPlaceholderAttrInfo(),
258 });
259 }
260
261 if (Info.UnavailableInSwift) {
262 handleAPINotedAttribute<AvailabilityAttr>(
263 S, D, true, Metadata,
264 [&] {
265 return new (S.Context) AvailabilityAttr(
267 &S.Context.Idents.get("swift"), VersionTuple(), VersionTuple(),
268 VersionTuple(),
269 /*Unavailable=*/true,
271 /*Strict=*/false,
272 /*Replacement=*/StringRef(),
273 /*Priority=*/Sema::AP_Explicit,
274 /*Environment=*/nullptr);
275 },
276 [](const Decl *D) {
277 return llvm::find_if(D->attrs(), [](const Attr *next) -> bool {
278 if (const auto *AA = dyn_cast<AvailabilityAttr>(next))
279 if (const auto *II = AA->getPlatform())
280 return II->isStr("swift");
281 return false;
282 });
283 });
284 }
285
286 // swift_private
287 if (auto SwiftPrivate = Info.isSwiftPrivate()) {
288 handleAPINotedAttribute<SwiftPrivateAttr>(
289 S, D, *SwiftPrivate, Metadata, [&] {
290 return new (S.Context)
291 SwiftPrivateAttr(S.Context, getPlaceholderAttrInfo());
292 });
293 }
294
295 // swift_safety
296 if (auto SafetyKind = Info.getSwiftSafety()) {
298 handleAPINotedAttribute<SwiftAttrAttr>(
299 S, D, Addition, Metadata,
300 [&] {
301 return SwiftAttrAttr::Create(
303 ? "safe"
304 : "unsafe");
305 },
306 [](const Decl *D) {
307 return llvm::find_if(D->attrs(), [](const Attr *attr) {
308 if (const auto *swiftAttr = dyn_cast<SwiftAttrAttr>(attr)) {
309 if (swiftAttr->getAttribute() == "safe" ||
310 swiftAttr->getAttribute() == "unsafe")
311 return true;
312 }
313 return false;
314 });
315 });
316 }
317
318 // swift_name
319 if (!Info.SwiftName.empty()) {
320 handleAPINotedAttribute<SwiftNameAttr>(
321 S, D, true, Metadata, [&]() -> SwiftNameAttr * {
322 AttributeFactory AF{};
323 AttributePool AP{AF};
324 auto &C = S.getASTContext();
325 ParsedAttr *SNA = AP.create(
326 &C.Idents.get("swift_name"), SourceRange(), AttributeScopeInfo(),
327 nullptr, nullptr, nullptr, ParsedAttr::Form::GNU());
328
329 if (!S.Swift().DiagnoseName(D, Info.SwiftName, D->getLocation(), *SNA,
330 /*IsAsync=*/false))
331 return nullptr;
332
333 return new (S.Context)
334 SwiftNameAttr(S.Context, getPlaceholderAttrInfo(),
335 ASTAllocateString(S.Context, Info.SwiftName));
336 });
337 }
338}
339
340static void ProcessAPINotes(Sema &S, Decl *D,
341 const api_notes::CommonTypeInfo &Info,
342 VersionedInfoMetadata Metadata) {
343 // swift_bridge
344 if (auto SwiftBridge = Info.getSwiftBridge()) {
345 handleAPINotedAttribute<SwiftBridgeAttr>(
346 S, D, !SwiftBridge->empty(), Metadata, [&] {
347 return new (S.Context)
348 SwiftBridgeAttr(S.Context, getPlaceholderAttrInfo(),
349 ASTAllocateString(S.Context, *SwiftBridge));
350 });
351 }
352
353 // ns_error_domain
354 if (auto NSErrorDomain = Info.getNSErrorDomain()) {
355 handleAPINotedAttribute<NSErrorDomainAttr>(
356 S, D, !NSErrorDomain->empty(), Metadata, [&] {
357 return new (S.Context)
358 NSErrorDomainAttr(S.Context, getPlaceholderAttrInfo(),
359 &S.Context.Idents.get(*NSErrorDomain));
360 });
361 }
362
363 if (auto ConformsTo = Info.getSwiftConformance())
364 D->addAttr(
365 SwiftAttrAttr::Create(S.Context, "conforms_to:" + ConformsTo.value()));
366
367 ProcessAPINotes(S, D, static_cast<const api_notes::CommonEntityInfo &>(Info),
368 Metadata);
369}
370
371/// Check that the replacement type provided by API notes is reasonable.
372///
373/// This is a very weak form of ABI check.
375 QualType OrigType,
376 QualType ReplacementType) {
377 if (S.Context.getTypeSize(OrigType) !=
378 S.Context.getTypeSize(ReplacementType)) {
379 S.Diag(Loc, diag::err_incompatible_replacement_type)
380 << ReplacementType << OrigType;
381 return true;
382 }
383
384 return false;
385}
386
387void Sema::ApplyAPINotesType(Decl *D, StringRef TypeString) {
388 if (!TypeString.empty() && ParseTypeFromStringCallback) {
389 auto ParsedType = ParseTypeFromStringCallback(TypeString, "<API Notes>",
390 D->getLocation());
391 if (ParsedType.isUsable()) {
393 auto TypeInfo = Context.getTrivialTypeSourceInfo(Type, D->getLocation());
394 if (auto Var = dyn_cast<VarDecl>(D)) {
395 // Make adjustments to parameter types.
396 if (isa<ParmVarDecl>(Var)) {
398 Type, D->getLocation(), TypeInfo);
399 Type = Context.getAdjustedParameterType(Type);
400 }
401
402 if (!checkAPINotesReplacementType(*this, Var->getLocation(),
403 Var->getType(), Type)) {
404 Var->setType(Type);
405 Var->setTypeSourceInfo(TypeInfo);
406 }
407 } else if (auto property = dyn_cast<ObjCPropertyDecl>(D)) {
408 if (!checkAPINotesReplacementType(*this, property->getLocation(),
409 property->getType(), Type)) {
410 property->setType(Type, TypeInfo);
411 }
412 } else if (auto field = dyn_cast<FieldDecl>(D)) {
413 if (!checkAPINotesReplacementType(*this, field->getLocation(),
414 field->getType(), Type)) {
415 field->setType(Type);
416 field->setTypeSourceInfo(TypeInfo);
417 }
418 } else {
419 llvm_unreachable("API notes allowed a type on an unknown declaration");
420 }
421 }
422 }
423}
424
426 auto GetModified =
427 [&](class Decl *D, QualType QT,
428 NullabilityKind Nullability) -> std::optional<QualType> {
429 QualType Original = QT;
432 /*OverrideExisting=*/true);
433 return (QT.getTypePtr() != Original.getTypePtr()) ? std::optional(QT)
434 : std::nullopt;
435 };
436
437 if (auto Function = dyn_cast<FunctionDecl>(D)) {
438 if (auto Modified =
439 GetModified(D, Function->getReturnType(), Nullability)) {
440 const FunctionType *FnType = Function->getType()->castAs<FunctionType>();
441 if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(FnType))
442 Function->setType(Context.getFunctionType(
443 *Modified, proto->getParamTypes(), proto->getExtProtoInfo()));
444 else
445 Function->setType(
446 Context.getFunctionNoProtoType(*Modified, FnType->getExtInfo()));
447 }
448 } else if (auto Method = dyn_cast<ObjCMethodDecl>(D)) {
449 if (auto Modified = GetModified(D, Method->getReturnType(), Nullability)) {
450 Method->setReturnType(*Modified);
451
452 // Make it a context-sensitive keyword if we can.
453 if (!isIndirectPointerType(*Modified))
454 Method->setObjCDeclQualifier(Decl::ObjCDeclQualifier(
455 Method->getObjCDeclQualifier() | Decl::OBJC_TQ_CSNullability));
456 }
457 } else if (auto Value = dyn_cast<ValueDecl>(D)) {
458 if (auto Modified = GetModified(D, Value->getType(), Nullability)) {
459 Value->setType(*Modified);
460
461 // Make it a context-sensitive keyword if we can.
462 if (auto Parm = dyn_cast<ParmVarDecl>(D)) {
463 if (Parm->isObjCMethodParameter() && !isIndirectPointerType(*Modified))
464 Parm->setObjCDeclQualifier(Decl::ObjCDeclQualifier(
465 Parm->getObjCDeclQualifier() | Decl::OBJC_TQ_CSNullability));
466 }
467 }
468 } else if (auto Property = dyn_cast<ObjCPropertyDecl>(D)) {
469 if (auto Modified = GetModified(D, Property->getType(), Nullability)) {
470 Property->setType(*Modified, Property->getTypeSourceInfo());
471
472 // Make it a property attribute if we can.
473 if (!isIndirectPointerType(*Modified))
474 Property->setPropertyAttributes(
476 }
477 }
478}
479
480/// Process API notes for a variable or property.
481static void ProcessAPINotes(Sema &S, Decl *D,
482 const api_notes::VariableInfo &Info,
483 VersionedInfoMetadata Metadata) {
484 // Type override.
485 applyAPINotesType(S, D, Info.getType(), Metadata);
486
487 // Nullability.
488 if (auto Nullability = Info.getNullability())
489 applyNullability(S, D, *Nullability, Metadata);
490
491 // Handle common entity information.
492 ProcessAPINotes(S, D, static_cast<const api_notes::CommonEntityInfo &>(Info),
493 Metadata);
494}
495
496/// Process API notes for a parameter.
498 const api_notes::ParamInfo &Info,
499 VersionedInfoMetadata Metadata) {
500 // noescape
501 if (auto NoEscape = Info.isNoEscape())
502 handleAPINotedAttribute<NoEscapeAttr>(S, D, *NoEscape, Metadata, [&] {
503 return new (S.Context) NoEscapeAttr(S.Context, getPlaceholderAttrInfo());
504 });
505
506 if (auto Lifetimebound = Info.isLifetimebound())
507 handleAPINotedAttribute<LifetimeBoundAttr>(
508 S, D, *Lifetimebound, Metadata, [&] {
509 return new (S.Context)
510 LifetimeBoundAttr(S.Context, getPlaceholderAttrInfo());
511 });
512
513 // Retain count convention
516
517 // Handle common entity information.
518 ProcessAPINotes(S, D, static_cast<const api_notes::VariableInfo &>(Info),
519 Metadata);
520}
521
522/// Process API notes for a global variable.
523static void ProcessAPINotes(Sema &S, VarDecl *D,
525 VersionedInfoMetadata metadata) {
526 // Handle common entity information.
527 ProcessAPINotes(S, D, static_cast<const api_notes::VariableInfo &>(Info),
528 metadata);
529}
530
531/// Process API notes for a C field.
532static void ProcessAPINotes(Sema &S, FieldDecl *D,
533 const api_notes::FieldInfo &Info,
534 VersionedInfoMetadata metadata) {
535 // Handle common entity information.
536 ProcessAPINotes(S, D, static_cast<const api_notes::VariableInfo &>(Info),
537 metadata);
538}
539
540/// Process API notes for an Objective-C property.
542 const api_notes::ObjCPropertyInfo &Info,
543 VersionedInfoMetadata Metadata) {
544 // Handle common entity information.
545 ProcessAPINotes(S, D, static_cast<const api_notes::VariableInfo &>(Info),
546 Metadata);
547
548 if (auto AsAccessors = Info.getSwiftImportAsAccessors()) {
549 handleAPINotedAttribute<SwiftImportPropertyAsAccessorsAttr>(
550 S, D, *AsAccessors, Metadata, [&] {
551 return new (S.Context) SwiftImportPropertyAsAccessorsAttr(
553 });
554 }
555}
556
557namespace {
558typedef llvm::PointerUnion<FunctionDecl *, ObjCMethodDecl *> FunctionOrMethod;
559}
560
561/// Process API notes for a function or method.
562static void ProcessAPINotes(Sema &S, FunctionOrMethod AnyFunc,
563 const api_notes::FunctionInfo &Info,
564 VersionedInfoMetadata Metadata) {
565 // Find the declaration itself.
566 FunctionDecl *FD = dyn_cast<FunctionDecl *>(AnyFunc);
567 Decl *D = FD;
568 ObjCMethodDecl *MD = nullptr;
569 if (!D) {
570 MD = cast<ObjCMethodDecl *>(AnyFunc);
571 D = MD;
572 }
573
574 assert((FD || MD) && "Expecting Function or ObjCMethod");
575
576 // Nullability of return type.
577 if (Info.NullabilityAudited)
578 applyNullability(S, D, Info.getReturnTypeInfo(), Metadata);
579
580 // Parameters.
581 unsigned NumParams = FD ? FD->getNumParams() : MD->param_size();
582
583 bool AnyTypeChanged = false;
584 for (unsigned I = 0; I != NumParams; ++I) {
585 ParmVarDecl *Param = FD ? FD->getParamDecl(I) : MD->param_begin()[I];
586 QualType ParamTypeBefore = Param->getType();
587
588 if (I < Info.Params.size())
589 ProcessAPINotes(S, Param, Info.Params[I], Metadata);
590
591 // Nullability.
592 if (Info.NullabilityAudited)
593 applyNullability(S, Param, Info.getParamTypeInfo(I), Metadata);
594
595 if (ParamTypeBefore.getAsOpaquePtr() != Param->getType().getAsOpaquePtr())
596 AnyTypeChanged = true;
597 }
598
599 // returns_(un)retained
600 if (!Info.SwiftReturnOwnership.empty())
601 D->addAttr(SwiftAttrAttr::Create(S.Context,
602 "returns_" + Info.SwiftReturnOwnership));
603
604 // Result type override.
605 QualType OverriddenResultType;
606 if (Metadata.IsActive && !Info.ResultType.empty() &&
609 Info.ResultType, "<API Notes>", D->getLocation());
610 if (ParsedType.isUsable()) {
611 QualType ResultType = Sema::GetTypeFromParser(ParsedType.get());
612
613 if (MD) {
615 MD->getReturnType(), ResultType)) {
616 auto ResultTypeInfo =
617 S.Context.getTrivialTypeSourceInfo(ResultType, D->getLocation());
618 MD->setReturnType(ResultType);
619 MD->setReturnTypeSourceInfo(ResultTypeInfo);
620 }
622 S, FD->getLocation(), FD->getReturnType(), ResultType)) {
623 OverriddenResultType = ResultType;
624 AnyTypeChanged = true;
625 }
626 }
627 }
628
629 // If the result type or any of the parameter types changed for a function
630 // declaration, we have to rebuild the type.
631 if (FD && AnyTypeChanged) {
632 if (const auto *fnProtoType = FD->getType()->getAs<FunctionProtoType>()) {
633 if (OverriddenResultType.isNull())
634 OverriddenResultType = fnProtoType->getReturnType();
635
636 SmallVector<QualType, 4> ParamTypes;
637 for (auto Param : FD->parameters())
638 ParamTypes.push_back(Param->getType());
639
640 FD->setType(S.Context.getFunctionType(OverriddenResultType, ParamTypes,
641 fnProtoType->getExtProtoInfo()));
642 } else if (!OverriddenResultType.isNull()) {
643 const auto *FnNoProtoType = FD->getType()->castAs<FunctionNoProtoType>();
645 OverriddenResultType, FnNoProtoType->getExtInfo()));
646 }
647 }
648
649 // Retain count convention
652
653 // Handle common entity information.
654 ProcessAPINotes(S, D, static_cast<const api_notes::CommonEntityInfo &>(Info),
655 Metadata);
656}
657
658/// Process API notes for a C++ method.
659static void ProcessAPINotes(Sema &S, CXXMethodDecl *Method,
660 const api_notes::CXXMethodInfo &Info,
661 VersionedInfoMetadata Metadata) {
662 if (Info.This && Info.This->isLifetimebound() &&
664 auto MethodType = Method->getType();
665 auto *attr = ::new (S.Context)
666 LifetimeBoundAttr(S.Context, getPlaceholderAttrInfo());
667 QualType AttributedType =
668 S.Context.getAttributedType(attr, MethodType, MethodType);
669 TypeLocBuilder TLB;
670 TLB.pushFullCopy(Method->getTypeSourceInfo()->getTypeLoc());
671 AttributedTypeLoc TyLoc = TLB.push<AttributedTypeLoc>(AttributedType);
672 TyLoc.setAttr(attr);
673 Method->setType(AttributedType);
674 Method->setTypeSourceInfo(TLB.getTypeSourceInfo(S.Context, AttributedType));
675 }
676
677 ProcessAPINotes(S, (FunctionOrMethod)Method, Info, Metadata);
678}
679
680/// Process API notes for a global function.
683 VersionedInfoMetadata Metadata) {
684 // Handle common function information.
685 ProcessAPINotes(S, FunctionOrMethod(D),
686 static_cast<const api_notes::FunctionInfo &>(Info), Metadata);
687}
688
689/// Process API notes for an enumerator.
691 const api_notes::EnumConstantInfo &Info,
692 VersionedInfoMetadata Metadata) {
693 // Handle common information.
694 ProcessAPINotes(S, D, static_cast<const api_notes::CommonEntityInfo &>(Info),
695 Metadata);
696}
697
698/// Process API notes for an Objective-C method.
700 const api_notes::ObjCMethodInfo &Info,
701 VersionedInfoMetadata Metadata) {
702 // Designated initializers.
703 if (Info.DesignatedInit) {
704 handleAPINotedAttribute<ObjCDesignatedInitializerAttr>(
705 S, D, true, Metadata, [&] {
706 if (ObjCInterfaceDecl *IFace = D->getClassInterface())
707 IFace->setHasDesignatedInitializers();
708
709 return new (S.Context) ObjCDesignatedInitializerAttr(
711 });
712 }
713
714 // Handle common function information.
715 ProcessAPINotes(S, FunctionOrMethod(D),
716 static_cast<const api_notes::FunctionInfo &>(Info), Metadata);
717}
718
719/// Process API notes for a tag.
720static void ProcessAPINotes(Sema &S, TagDecl *D, const api_notes::TagInfo &Info,
721 VersionedInfoMetadata Metadata) {
722 if (auto ImportAs = Info.SwiftImportAs)
723 D->addAttr(SwiftAttrAttr::Create(S.Context, "import_" + ImportAs.value()));
724
725 if (auto RetainOp = Info.SwiftRetainOp)
726 D->addAttr(SwiftAttrAttr::Create(S.Context, "retain:" + RetainOp.value()));
727
728 if (auto ReleaseOp = Info.SwiftReleaseOp)
729 D->addAttr(
730 SwiftAttrAttr::Create(S.Context, "release:" + ReleaseOp.value()));
731 if (auto DestroyOp = Info.SwiftDestroyOp)
732 D->addAttr(
733 SwiftAttrAttr::Create(S.Context, "destroy:" + DestroyOp.value()));
734 if (auto DefaultOwnership = Info.SwiftDefaultOwnership)
735 D->addAttr(SwiftAttrAttr::Create(
736 S.Context, "returned_as_" + DefaultOwnership.value() + "_by_default"));
737
738 if (auto Copyable = Info.isSwiftCopyable()) {
739 if (!*Copyable)
740 D->addAttr(SwiftAttrAttr::Create(S.Context, "~Copyable"));
741 }
742
743 if (auto Escapable = Info.isSwiftEscapable()) {
744 D->addAttr(SwiftAttrAttr::Create(S.Context,
745 *Escapable ? "Escapable" : "~Escapable"));
746 }
747
748 if (auto Extensibility = Info.EnumExtensibility) {
750 bool ShouldAddAttribute = (*Extensibility != EnumExtensibilityKind::None);
751 handleAPINotedAttribute<EnumExtensibilityAttr>(
752 S, D, ShouldAddAttribute, Metadata, [&] {
753 EnumExtensibilityAttr::Kind kind;
754 switch (*Extensibility) {
755 case EnumExtensibilityKind::None:
756 llvm_unreachable("remove only");
757 case EnumExtensibilityKind::Open:
758 kind = EnumExtensibilityAttr::Open;
759 break;
760 case EnumExtensibilityKind::Closed:
761 kind = EnumExtensibilityAttr::Closed;
762 break;
763 }
764 return new (S.Context)
765 EnumExtensibilityAttr(S.Context, getPlaceholderAttrInfo(), kind);
766 });
767 }
768
769 if (auto FlagEnum = Info.isFlagEnum()) {
770 handleAPINotedAttribute<FlagEnumAttr>(S, D, *FlagEnum, Metadata, [&] {
771 return new (S.Context) FlagEnumAttr(S.Context, getPlaceholderAttrInfo());
772 });
773 }
774
775 // Handle common type information.
776 ProcessAPINotes(S, D, static_cast<const api_notes::CommonTypeInfo &>(Info),
777 Metadata);
778}
779
780/// Process API notes for a typedef.
782 const api_notes::TypedefInfo &Info,
783 VersionedInfoMetadata Metadata) {
784 // swift_wrapper
785 using SwiftWrapperKind = api_notes::SwiftNewTypeKind;
786
787 if (auto SwiftWrapper = Info.SwiftWrapper) {
788 handleAPINotedAttribute<SwiftNewTypeAttr>(
789 S, D, *SwiftWrapper != SwiftWrapperKind::None, Metadata, [&] {
790 SwiftNewTypeAttr::NewtypeKind Kind;
791 switch (*SwiftWrapper) {
792 case SwiftWrapperKind::None:
793 llvm_unreachable("Shouldn't build an attribute");
794
795 case SwiftWrapperKind::Struct:
796 Kind = SwiftNewTypeAttr::NK_Struct;
797 break;
798
799 case SwiftWrapperKind::Enum:
800 Kind = SwiftNewTypeAttr::NK_Enum;
801 break;
802 }
803 AttributeCommonInfo SyntaxInfo{
804 SourceRange(),
805 AttributeCommonInfo::AT_SwiftNewType,
806 {AttributeCommonInfo::AS_GNU, SwiftNewTypeAttr::GNU_swift_wrapper,
807 /*IsAlignas*/ false, /*IsRegularKeywordAttribute*/ false}};
808 return new (S.Context) SwiftNewTypeAttr(S.Context, SyntaxInfo, Kind);
809 });
810 }
811
812 // Handle common type information.
813 ProcessAPINotes(S, D, static_cast<const api_notes::CommonTypeInfo &>(Info),
814 Metadata);
815}
816
817/// Process API notes for an Objective-C class or protocol.
819 const api_notes::ContextInfo &Info,
820 VersionedInfoMetadata Metadata) {
821 // Handle common type information.
822 ProcessAPINotes(S, D, static_cast<const api_notes::CommonTypeInfo &>(Info),
823 Metadata);
824}
825
826/// Process API notes for an Objective-C class.
828 const api_notes::ContextInfo &Info,
829 VersionedInfoMetadata Metadata) {
830 if (auto AsNonGeneric = Info.getSwiftImportAsNonGeneric()) {
831 handleAPINotedAttribute<SwiftImportAsNonGenericAttr>(
832 S, D, *AsNonGeneric, Metadata, [&] {
833 return new (S.Context)
834 SwiftImportAsNonGenericAttr(S.Context, getPlaceholderAttrInfo());
835 });
836 }
837
838 if (auto ObjcMembers = Info.getSwiftObjCMembers()) {
839 handleAPINotedAttribute<SwiftObjCMembersAttr>(
840 S, D, *ObjcMembers, Metadata, [&] {
841 return new (S.Context)
842 SwiftObjCMembersAttr(S.Context, getPlaceholderAttrInfo());
843 });
844 }
845
846 // Handle information common to Objective-C classes and protocols.
847 ProcessAPINotes(S, static_cast<clang::ObjCContainerDecl *>(D), Info,
848 Metadata);
849}
850
851/// If we're applying API notes with an active, non-default version, and the
852/// versioned API notes have a SwiftName but the declaration normally wouldn't
853/// have one, add a removal attribute to make it clear that the new SwiftName
854/// attribute only applies to the active version of \p D, not to all versions.
855///
856/// This must be run \em before processing API notes for \p D, because otherwise
857/// any existing SwiftName attribute will have been packaged up in a
858/// SwiftVersionedAdditionAttr.
859template <typename SpecificInfo>
861 Sema &S, Decl *D,
863 if (D->hasAttr<SwiftNameAttr>())
864 return;
865 if (!Info.getSelected())
866 return;
867
868 // Is the active slice versioned, and does it set a Swift name?
869 VersionTuple SelectedVersion;
870 SpecificInfo SelectedInfoSlice;
871 std::tie(SelectedVersion, SelectedInfoSlice) = Info[*Info.getSelected()];
872 if (SelectedVersion.empty())
873 return;
874 if (SelectedInfoSlice.SwiftName.empty())
875 return;
876
877 // Does the unversioned slice /not/ set a Swift name?
878 for (const auto &VersionAndInfoSlice : Info) {
879 if (!VersionAndInfoSlice.first.empty())
880 continue;
881 if (!VersionAndInfoSlice.second.SwiftName.empty())
882 return;
883 }
884
885 // Then explicitly call that out with a removal attribute.
886 VersionedInfoMetadata DummyFutureMetadata(
887 SelectedVersion, IsActive_t::Inactive, IsSubstitution_t::Replacement);
888 handleAPINotedAttribute<SwiftNameAttr>(
889 S, D, /*add*/ false, DummyFutureMetadata, []() -> SwiftNameAttr * {
890 llvm_unreachable("should not try to add an attribute here");
891 });
892}
893
894/// Processes all versions of versioned API notes.
895///
896/// Just dispatches to the various ProcessAPINotes functions in this file.
897template <typename SpecificDecl, typename SpecificInfo>
899 Sema &S, SpecificDecl *D,
901
904
905 unsigned Selected = Info.getSelected().value_or(Info.size());
906
907 VersionTuple Version;
908 SpecificInfo InfoSlice;
909 for (unsigned i = 0, e = Info.size(); i != e; ++i) {
910 std::tie(Version, InfoSlice) = Info[i];
911 auto Active = (i == Selected) ? IsActive_t::Active : IsActive_t::Inactive;
912 auto Replacement = IsSubstitution_t::Original;
913
914 // When collecting all APINotes as version-independent,
915 // capture all as inactive and defer to the client to select the
916 // right one.
918 Active = IsActive_t::Inactive;
919 Replacement = IsSubstitution_t::Original;
920 } else if (Active == IsActive_t::Inactive && Version.empty()) {
921 Replacement = IsSubstitution_t::Replacement;
922 Version = Info[Selected].first;
923 }
924
925 ProcessAPINotes(S, D, InfoSlice,
926 VersionedInfoMetadata(Version, Active, Replacement));
927 }
928}
929
930static std::optional<api_notes::Context>
932 if (auto NamespaceContext = dyn_cast<NamespaceDecl>(DC)) {
933 for (auto Reader : APINotes.findAPINotes(NamespaceContext->getLocation())) {
934 // Retrieve the context ID for the parent namespace of the decl.
935 std::stack<NamespaceDecl *> NamespaceStack;
936 {
937 for (auto CurrentNamespace = NamespaceContext; CurrentNamespace;
938 CurrentNamespace =
939 dyn_cast<NamespaceDecl>(CurrentNamespace->getParent())) {
940 if (!CurrentNamespace->isInlineNamespace())
941 NamespaceStack.push(CurrentNamespace);
942 }
943 }
944 std::optional<api_notes::ContextID> NamespaceID;
945 while (!NamespaceStack.empty()) {
946 auto CurrentNamespace = NamespaceStack.top();
947 NamespaceStack.pop();
948 NamespaceID =
949 Reader->lookupNamespaceID(CurrentNamespace->getName(), NamespaceID);
950 if (!NamespaceID)
951 return std::nullopt;
952 }
953 if (NamespaceID)
954 return api_notes::Context(*NamespaceID,
956 }
957 }
958 return std::nullopt;
959}
960
961static std::optional<api_notes::Context>
963 assert(DC && "tag context must not be null");
964 for (auto Reader : APINotes.findAPINotes(DC->getLocation())) {
965 // Retrieve the context ID for the parent tag of the decl.
966 std::stack<TagDecl *> TagStack;
967 {
968 for (auto CurrentTag = DC; CurrentTag;
969 CurrentTag = dyn_cast<TagDecl>(CurrentTag->getParent()))
970 TagStack.push(CurrentTag);
971 }
972 assert(!TagStack.empty());
973 std::optional<api_notes::Context> Ctx =
974 UnwindNamespaceContext(TagStack.top()->getDeclContext(), APINotes);
975 while (!TagStack.empty()) {
976 auto CurrentTag = TagStack.top();
977 TagStack.pop();
978 auto CtxID = Reader->lookupTagID(CurrentTag->getName(), Ctx);
979 if (!CtxID)
980 return std::nullopt;
982 }
983 return Ctx;
984 }
985 return std::nullopt;
986}
987
988/// Process API notes that are associated with this declaration, mapping them
989/// to attributes as appropriate.
991 if (!D)
992 return;
993
994 auto *DC = D->getDeclContext();
995 // Globals.
996 if (DC->isFileContext() || DC->isNamespace() ||
997 DC->getDeclKind() == Decl::LinkageSpec) {
998 std::optional<api_notes::Context> APINotesContext =
1000 // Global variables.
1001 if (auto VD = dyn_cast<VarDecl>(D)) {
1002 for (auto Reader : APINotes.findAPINotes(D->getLocation())) {
1003 auto Info =
1004 Reader->lookupGlobalVariable(VD->getName(), APINotesContext);
1005 ProcessVersionedAPINotes(*this, VD, Info);
1006 }
1007
1008 return;
1009 }
1010
1011 // Global functions.
1012 if (auto FD = dyn_cast<FunctionDecl>(D)) {
1013 if (FD->getDeclName().isIdentifier()) {
1014 for (auto Reader : APINotes.findAPINotes(D->getLocation())) {
1015 auto Info =
1016 Reader->lookupGlobalFunction(FD->getName(), APINotesContext);
1017 ProcessVersionedAPINotes(*this, FD, Info);
1018 }
1019 }
1020
1021 return;
1022 }
1023
1024 // Objective-C classes.
1025 if (auto Class = dyn_cast<ObjCInterfaceDecl>(D)) {
1026 for (auto Reader : APINotes.findAPINotes(D->getLocation())) {
1027 auto Info = Reader->lookupObjCClassInfo(Class->getName());
1028 ProcessVersionedAPINotes(*this, Class, Info);
1029 }
1030
1031 return;
1032 }
1033
1034 // Objective-C protocols.
1035 if (auto Protocol = dyn_cast<ObjCProtocolDecl>(D)) {
1036 for (auto Reader : APINotes.findAPINotes(D->getLocation())) {
1037 auto Info = Reader->lookupObjCProtocolInfo(Protocol->getName());
1038 ProcessVersionedAPINotes(*this, Protocol, Info);
1039 }
1040
1041 return;
1042 }
1043
1044 // Tags
1045 if (auto Tag = dyn_cast<TagDecl>(D)) {
1046 // Determine the name of the entity to search for. If this is an
1047 // anonymous tag that gets its linked name from a typedef, look for the
1048 // typedef name. This allows tag-specific information to be added
1049 // to the declaration.
1050 std::string LookupName;
1051 if (auto typedefName = Tag->getTypedefNameForAnonDecl())
1052 LookupName = typedefName->getName().str();
1053 else
1054 LookupName = Tag->getName().str();
1055
1056 // Use the source location to discern if this Tag is an OPTIONS macro.
1057 // For now we would like to limit this trick of looking up the APINote tag
1058 // using the EnumDecl's QualType in the case where the enum is anonymous.
1059 // This is only being used to support APINotes lookup for C++
1060 // NS/CF_OPTIONS when C++-Interop is enabled.
1061 std::string MacroName =
1062 LookupName.empty() && Tag->getOuterLocStart().isMacroID()
1064 Tag->getOuterLocStart(),
1065 Tag->getASTContext().getSourceManager(), LangOpts)
1066 .str()
1067 : "";
1068
1069 if (LookupName.empty() && isa<clang::EnumDecl>(Tag) &&
1070 (MacroName == "CF_OPTIONS" || MacroName == "NS_OPTIONS" ||
1071 MacroName == "OBJC_OPTIONS" || MacroName == "SWIFT_OPTIONS")) {
1072
1073 clang::QualType T = llvm::cast<clang::EnumDecl>(Tag)->getIntegerType();
1075 T.split(), getASTContext().getPrintingPolicy());
1076 }
1077
1078 for (auto Reader : APINotes.findAPINotes(D->getLocation())) {
1079 if (auto ParentTag = dyn_cast<TagDecl>(Tag->getDeclContext()))
1080 APINotesContext = UnwindTagContext(ParentTag, APINotes);
1081 auto Info = Reader->lookupTag(LookupName, APINotesContext);
1082 ProcessVersionedAPINotes(*this, Tag, Info);
1083 }
1084
1085 return;
1086 }
1087
1088 // Typedefs
1089 if (auto Typedef = dyn_cast<TypedefNameDecl>(D)) {
1090 for (auto Reader : APINotes.findAPINotes(D->getLocation())) {
1091 auto Info = Reader->lookupTypedef(Typedef->getName(), APINotesContext);
1092 ProcessVersionedAPINotes(*this, Typedef, Info);
1093 }
1094
1095 return;
1096 }
1097 }
1098
1099 // Enumerators.
1100 if (DC->getRedeclContext()->isFileContext() ||
1101 DC->getRedeclContext()->isExternCContext()) {
1102 if (auto EnumConstant = dyn_cast<EnumConstantDecl>(D)) {
1103 for (auto Reader : APINotes.findAPINotes(D->getLocation())) {
1104 auto Info = Reader->lookupEnumConstant(EnumConstant->getName());
1105 ProcessVersionedAPINotes(*this, EnumConstant, Info);
1106 }
1107
1108 return;
1109 }
1110 }
1111
1112 if (auto ObjCContainer = dyn_cast<ObjCContainerDecl>(DC)) {
1113 // Location function that looks up an Objective-C context.
1114 auto GetContext = [&](api_notes::APINotesReader *Reader)
1115 -> std::optional<api_notes::ContextID> {
1116 if (auto Protocol = dyn_cast<ObjCProtocolDecl>(ObjCContainer)) {
1117 if (auto Found = Reader->lookupObjCProtocolID(Protocol->getName()))
1118 return *Found;
1119
1120 return std::nullopt;
1121 }
1122
1123 if (auto Impl = dyn_cast<ObjCCategoryImplDecl>(ObjCContainer)) {
1124 if (auto Cat = Impl->getCategoryDecl())
1125 ObjCContainer = Cat->getClassInterface();
1126 else
1127 return std::nullopt;
1128 }
1129
1130 if (auto Category = dyn_cast<ObjCCategoryDecl>(ObjCContainer)) {
1131 if (Category->getClassInterface())
1132 ObjCContainer = Category->getClassInterface();
1133 else
1134 return std::nullopt;
1135 }
1136
1137 if (auto Impl = dyn_cast<ObjCImplDecl>(ObjCContainer)) {
1138 if (Impl->getClassInterface())
1139 ObjCContainer = Impl->getClassInterface();
1140 else
1141 return std::nullopt;
1142 }
1143
1144 if (auto Class = dyn_cast<ObjCInterfaceDecl>(ObjCContainer)) {
1145 if (auto Found = Reader->lookupObjCClassID(Class->getName()))
1146 return *Found;
1147
1148 return std::nullopt;
1149 }
1150
1151 return std::nullopt;
1152 };
1153
1154 // Objective-C methods.
1155 if (auto Method = dyn_cast<ObjCMethodDecl>(D)) {
1156 for (auto Reader : APINotes.findAPINotes(D->getLocation())) {
1157 if (auto Context = GetContext(Reader)) {
1158 // Map the selector.
1159 Selector Sel = Method->getSelector();
1160 SmallVector<StringRef, 2> SelPieces;
1161 if (Sel.isUnarySelector()) {
1162 SelPieces.push_back(Sel.getNameForSlot(0));
1163 } else {
1164 for (unsigned i = 0, n = Sel.getNumArgs(); i != n; ++i)
1165 SelPieces.push_back(Sel.getNameForSlot(i));
1166 }
1167
1168 api_notes::ObjCSelectorRef SelectorRef;
1169 SelectorRef.NumArgs = Sel.getNumArgs();
1170 SelectorRef.Identifiers = SelPieces;
1171
1172 auto Info = Reader->lookupObjCMethod(*Context, SelectorRef,
1173 Method->isInstanceMethod());
1174 ProcessVersionedAPINotes(*this, Method, Info);
1175 }
1176 }
1177 }
1178
1179 // Objective-C properties.
1180 if (auto Property = dyn_cast<ObjCPropertyDecl>(D)) {
1181 for (auto Reader : APINotes.findAPINotes(D->getLocation())) {
1182 if (auto Context = GetContext(Reader)) {
1183 bool isInstanceProperty =
1184 (Property->getPropertyAttributesAsWritten() &
1186 auto Info = Reader->lookupObjCProperty(*Context, Property->getName(),
1187 isInstanceProperty);
1188 ProcessVersionedAPINotes(*this, Property, Info);
1189 }
1190 }
1191
1192 return;
1193 }
1194 }
1195
1196 if (auto TagContext = dyn_cast<TagDecl>(DC)) {
1197 if (auto CXXMethod = dyn_cast<CXXMethodDecl>(D)) {
1198 if (!isa<CXXConstructorDecl>(CXXMethod) &&
1199 !isa<CXXDestructorDecl>(CXXMethod) &&
1200 !isa<CXXConversionDecl>(CXXMethod)) {
1201 for (auto Reader : APINotes.findAPINotes(D->getLocation())) {
1202 if (auto Context = UnwindTagContext(TagContext, APINotes)) {
1203 std::string MethodName;
1204 if (CXXMethod->isOverloadedOperator())
1205 MethodName =
1206 std::string("operator") +
1207 getOperatorSpelling(CXXMethod->getOverloadedOperator());
1208 else
1209 MethodName = CXXMethod->getName();
1210
1211 auto Info = Reader->lookupCXXMethod(Context->id, MethodName);
1212 ProcessVersionedAPINotes(*this, CXXMethod, Info);
1213 }
1214 }
1215 }
1216 }
1217
1218 if (auto Field = dyn_cast<FieldDecl>(D)) {
1219 if (!Field->isUnnamedBitField() && !Field->isAnonymousStructOrUnion()) {
1220 for (auto Reader : APINotes.findAPINotes(D->getLocation())) {
1221 if (auto Context = UnwindTagContext(TagContext, APINotes)) {
1222 auto Info = Reader->lookupField(Context->id, Field->getName());
1223 ProcessVersionedAPINotes(*this, Field, Info);
1224 }
1225 }
1226 }
1227 }
1228
1229 if (auto Tag = dyn_cast<TagDecl>(D)) {
1230 for (auto Reader : APINotes.findAPINotes(D->getLocation())) {
1231 if (auto Context = UnwindTagContext(TagContext, APINotes)) {
1232 auto Info = Reader->lookupTag(Tag->getName(), Context);
1233 ProcessVersionedAPINotes(*this, Tag, Info);
1234 }
1235 }
1236 }
1237 }
1238}
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
FormatToken * Next
The next token in the unwrapped line.
*collection of selector each with an associated kind and an ordered *collection of selectors A selector has a kind
static std::optional< api_notes::Context > UnwindNamespaceContext(DeclContext *DC, api_notes::APINotesManager &APINotes)
static void ProcessVersionedAPINotes(Sema &S, SpecificDecl *D, const api_notes::APINotesReader::VersionedInfo< SpecificInfo > Info)
Processes all versions of versioned API notes.
static bool checkAPINotesReplacementType(Sema &S, SourceLocation Loc, QualType OrigType, QualType ReplacementType)
Check that the replacement type provided by API notes is reasonable.
static std::optional< api_notes::Context > UnwindTagContext(TagDecl *DC, api_notes::APINotesManager &APINotes)
static StringRef ASTAllocateString(ASTContext &Ctx, StringRef String)
Copy a string into ASTContext-allocated memory.
static void applyAPINotesType(Sema &S, Decl *decl, StringRef typeString, VersionedInfoMetadata metadata)
static void handleAPINotedRetainCountConvention(Sema &S, Decl *D, VersionedInfoMetadata Metadata, std::optional< api_notes::RetainCountConventionKind > Convention)
static void handleAPINotedRetainCountAttribute(Sema &S, Decl *D, bool ShouldAddAttribute, VersionedInfoMetadata Metadata)
static AttributeCommonInfo getPlaceholderAttrInfo()
static void ProcessAPINotes(Sema &S, Decl *D, const api_notes::CommonEntityInfo &Info, VersionedInfoMetadata Metadata)
static void applyNullability(Sema &S, Decl *decl, NullabilityKind nullability, VersionedInfoMetadata metadata)
Apply nullability to the given declaration.
static void maybeAttachUnversionedSwiftName(Sema &S, Decl *D, const api_notes::APINotesReader::VersionedInfo< SpecificInfo > Info)
If we're applying API notes with an active, non-default version, and the versioned API notes have a S...
static bool isIndirectPointerType(QualType Type)
Determine whether this is a multi-level pointer type.
This file declares semantic analysis for Objective-C.
This file declares semantic analysis functions specific to Swift.
Defines the clang::SourceLocation class and associated facilities.
Defines the clang::TypeLoc interface and its subclasses.
__DEVICE__ void * memcpy(void *__a, const void *__b, size_t __c)
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:226
QualType getAttributedType(attr::Kind attrKind, QualType modifiedType, QualType equivalentType, const Attr *attr=nullptr) const
QualType getFunctionNoProtoType(QualType ResultTy, const FunctionType::ExtInfo &Info) const
Return a K&R style C function type like 'int()'.
IdentifierTable & Idents
Definition ASTContext.h:797
TypeSourceInfo * getTrivialTypeSourceInfo(QualType T, SourceLocation Loc=SourceLocation()) const
Allocate a TypeSourceInfo where all locations have been initialized to a given location,...
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
void * Allocate(size_t Size, unsigned Align=8) const
Definition ASTContext.h:871
QualType getFunctionType(QualType ResultTy, ArrayRef< QualType > Args, const FunctionProtoType::ExtProtoInfo &EPI) const
Return a normal function type with a typed argument list.
Attr - This represents one attribute.
Definition Attr.h:46
A factory, from which one makes pools, from which one creates individual attributes which are dealloc...
Definition ParsedAttr.h:622
ParsedAttr * create(IdentifierInfo *attrName, SourceRange attrRange, AttributeScopeInfo scope, ArgsUnion *args, unsigned numArgs, ParsedAttr::Form form, SourceLocation ellipsisLoc=SourceLocation())
Definition ParsedAttr.h:735
Type source information for an attributed type.
Definition TypeLoc.h:1008
void setAttr(const Attr *A)
Definition TypeLoc.h:1034
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2136
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition DeclBase.h:1449
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
void addAttr(Attr *A)
attr_iterator attr_end() const
Definition DeclBase.h:542
AttrVec::const_iterator attr_iterator
Definition DeclBase.h:532
ObjCDeclQualifier
ObjCDeclQualifier - 'Qualifiers' written next to the return and parameter types in method declaration...
Definition DeclBase.h:198
@ OBJC_TQ_CSNullability
The nullability qualifier is set when the nullability of the result or parameter was expressed via a ...
Definition DeclBase.h:210
SourceLocation getLocation() const
Definition DeclBase.h:439
DeclContext * getDeclContext()
Definition DeclBase.h:448
attr_range attrs() const
Definition DeclBase.h:535
AttrVec & getAttrs()
Definition DeclBase.h:524
bool hasAttr() const
Definition DeclBase.h:577
An instance of this object exists for each enum constant that is defined.
Definition Decl.h:3423
Represents a member of a struct/union/class.
Definition Decl.h:3160
Represents a function declaration or definition.
Definition Decl.h:2000
const ParmVarDecl * getParamDecl(unsigned i) const
Definition Decl.h:2797
QualType getReturnType() const
Definition Decl.h:2845
ArrayRef< ParmVarDecl * > parameters() const
Definition Decl.h:2774
unsigned getNumParams() const
Return the number of parameters this function must have based on its FunctionType.
Definition Decl.cpp:3827
Represents a K&R-style 'int foo()' function, which has no information available about its arguments.
Definition TypeBase.h:4893
Represents a prototype with parameter type info, e.g.
Definition TypeBase.h:5315
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition TypeBase.h:4511
ExtInfo getExtInfo() const
Definition TypeBase.h:4867
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
static StringRef getImmediateMacroName(SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts)
Retrieve the name of the immediate macro expansion.
Definition Lexer.cpp:1066
ObjCContainerDecl - Represents a container for method declarations.
Definition DeclObjC.h:948
Represents an ObjC class declaration.
Definition DeclObjC.h:1154
ObjCMethodDecl - Represents an instance or class method declaration.
Definition DeclObjC.h:140
unsigned param_size() const
Definition DeclObjC.h:347
void setReturnTypeSourceInfo(TypeSourceInfo *TInfo)
Definition DeclObjC.h:344
param_const_iterator param_begin() const
Definition DeclObjC.h:354
void setReturnType(QualType T)
Definition DeclObjC.h:330
QualType getReturnType() const
Definition DeclObjC.h:329
ObjCInterfaceDecl * getClassInterface()
Represents one property declaration in an Objective-C interface.
Definition DeclObjC.h:731
Represents a parameter to a function.
Definition Decl.h:1790
ParsedAttr - Represents a syntactic attribute.
Definition ParsedAttr.h:119
A (possibly-)qualified type.
Definition TypeBase.h:937
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition TypeBase.h:1004
std::string getAsString() const
void * getAsOpaquePtr() const
Definition TypeBase.h:984
Smart pointer class that efficiently represents Objective-C method names.
StringRef getNameForSlot(unsigned argIndex) const
Retrieve the name at a given position in the selector.
bool isUnarySelector() const
unsigned getNumArgs() const
SemaDiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID)
Emit a diagnostic.
Definition SemaBase.cpp:61
QualType AdjustParameterTypeForObjCAutoRefCount(QualType T, SourceLocation NameLoc, TypeSourceInfo *TSInfo)
bool DiagnoseName(Decl *D, StringRef Name, SourceLocation Loc, const ParsedAttr &AL, bool IsAsync)
Do a check to make sure Name looks like a legal argument for the swift_name attribute applied to decl...
Sema - This implements semantic analysis and AST building for C.
Definition Sema.h:868
ASTContext & Context
Definition Sema.h:1300
SemaObjC & ObjC()
Definition Sema.h:1510
bool captureSwiftVersionIndependentAPINotes()
Whether APINotes should be gathered for all applicable Swift language versions, without being applied...
Definition Sema.h:1661
ASTContext & getASTContext() const
Definition Sema.h:939
PrintingPolicy getPrintingPolicy() const
Retrieve a suitable printing policy for diagnostics.
Definition Sema.h:1204
api_notes::APINotesManager APINotes
Definition Sema.h:1304
const LangOptions & LangOpts
Definition Sema.h:1298
SemaSwift & Swift()
Definition Sema.h:1555
std::function< TypeResult(StringRef, StringRef, SourceLocation)> ParseTypeFromStringCallback
Callback to the parser to parse a type expressed as a string.
Definition Sema.h:1353
void ApplyNullability(Decl *D, NullabilityKind Nullability)
Apply the 'Nullability:' annotation to the specified declaration.
bool CheckImplicitNullabilityTypeSpecifier(QualType &Type, NullabilityKind Nullability, SourceLocation DiagLoc, bool AllowArrayTypes, bool OverrideExisting)
Check whether a nullability type specifier can be added to the given type through some means not writ...
@ AP_Explicit
The availability attribute was specified explicitly next to the declaration.
Definition Sema.h:4854
void ApplyAPINotesType(Decl *D, StringRef TypeString)
Apply the 'Type:' annotation to the specified declaration.
void ProcessAPINotes(Decl *D)
Map any API notes provided for this declaration to attributes on the declaration.
bool LookupName(LookupResult &R, Scope *S, bool AllowBuiltinCreation=false, bool ForceNoCPlusPlus=false)
Perform unqualified name lookup starting from a given scope.
static QualType GetTypeFromParser(ParsedType Ty, TypeSourceInfo **TInfo=nullptr)
Encodes a location in the source.
A trivial tuple used to represent a source range.
Represents the declaration of a struct/union/class/enum.
Definition Decl.h:3717
TyLocType push(QualType T)
Pushes space for a new TypeLoc of the given type.
void pushFullCopy(TypeLoc L)
Pushes a copy of the given TypeLoc onto this builder.
TypeSourceInfo * getTypeSourceInfo(ASTContext &Context, QualType T)
Creates a TypeSourceInfo for the given type.
The base class of the type hierarchy.
Definition TypeBase.h:1839
const T * castAs() const
Member-template castAs<specific type>.
Definition TypeBase.h:9285
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition Type.cpp:753
bool isMemberPointerType() const
Definition TypeBase.h:8706
bool isObjCObjectPointerType() const
Definition TypeBase.h:8804
bool isAnyPointerType() const
Definition TypeBase.h:8633
const T * getAs() const
Member-template getAs<specific type>'.
Definition TypeBase.h:9218
Base class for declarations which introduce a typedef-name.
Definition Decl.h:3562
void setType(QualType newType)
Definition Decl.h:724
QualType getType() const
Definition Decl.h:723
QualType getType() const
Definition Value.cpp:237
Represents a variable declaration or definition.
Definition Decl.h:926
The API notes manager helps find API notes associated with declarations.
llvm::SmallVector< APINotesReader *, 2 > findAPINotes(SourceLocation Loc)
Find the API notes readers that correspond to the given source location.
Captures the completed versioned information for a particular part of API notes, including both unver...
unsigned size() const
Return the number of versioned results we know about.
std::optional< unsigned > getSelected() const
Retrieve the selected index in the result set.
A class that reads API notes data from a binary file that was written by the APINotesWriter.
Describes API notes data for a C++ method.
Definition Types.h:734
std::optional< ParamInfo > This
Definition Types.h:738
Describes API notes data for any entity.
Definition Types.h:54
unsigned UnavailableInSwift
Whether this entity is marked unavailable in Swift.
Definition Types.h:65
unsigned Unavailable
Whether this entity is marked unavailable.
Definition Types.h:61
std::string UnavailableMsg
Message to use when this entity is unavailable.
Definition Types.h:57
std::optional< SwiftSafetyKind > getSwiftSafety() const
Definition Types.h:100
std::optional< bool > isSwiftPrivate() const
Definition Types.h:90
Describes API notes for types.
Definition Types.h:159
std::optional< std::string > getSwiftConformance() const
Definition Types.h:195
const std::optional< std::string > & getSwiftBridge() const
Definition Types.h:174
const std::optional< std::string > & getNSErrorDomain() const
Definition Types.h:182
Describes API notes data for an Objective-C class or protocol or a C++ namespace.
Definition Types.h:235
std::optional< bool > getSwiftImportAsNonGeneric() const
Definition Types.h:285
std::optional< bool > getSwiftObjCMembers() const
Definition Types.h:295
Describes API notes data for an enumerator.
Definition Types.h:752
Describes API notes data for a C/C++ record field.
Definition Types.h:728
API notes for a function or method.
Definition Types.h:551
std::string SwiftReturnOwnership
Ownership convention for return value.
Definition Types.h:584
std::optional< RetainCountConventionKind > getRetainCountConvention() const
Definition Types.h:631
std::vector< ParamInfo > Params
The function parameters.
Definition Types.h:587
NullabilityKind getReturnTypeInfo() const
Definition Types.h:629
NullabilityKind getParamTypeInfo(unsigned index) const
Definition Types.h:625
std::string ResultType
The result type of this function, as a C type.
Definition Types.h:581
unsigned NullabilityAudited
Whether the signature has been audited with respect to nullability.
Definition Types.h:565
Describes API notes data for a global function.
Definition Types.h:722
Describes API notes data for a global variable.
Definition Types.h:716
Describes API notes data for an Objective-C method.
Definition Types.h:675
unsigned DesignatedInit
Whether this is a designated initializer of its class.
Definition Types.h:679
Describes API notes data for an Objective-C property.
Definition Types.h:399
std::optional< bool > getSwiftImportAsAccessors() const
Definition Types.h:409
Describes a function or method parameter.
Definition Types.h:457
std::optional< bool > isNoEscape() const
Definition Types.h:485
std::optional< bool > isLifetimebound() const
Definition Types.h:493
std::optional< RetainCountConventionKind > getRetainCountConvention() const
Definition Types.h:502
Describes API notes data for a tag.
Definition Types.h:758
std::optional< std::string > SwiftReleaseOp
Definition Types.h:777
std::optional< std::string > SwiftRetainOp
Definition Types.h:776
std::optional< std::string > SwiftImportAs
Definition Types.h:775
std::optional< std::string > SwiftDefaultOwnership
Definition Types.h:779
std::optional< EnumExtensibilityKind > EnumExtensibility
Definition Types.h:781
std::optional< std::string > SwiftDestroyOp
Definition Types.h:778
std::optional< bool > isFlagEnum() const
Definition Types.h:788
std::optional< bool > isSwiftCopyable() const
Definition Types.h:798
std::optional< bool > isSwiftEscapable() const
Definition Types.h:807
Describes API notes data for a typedef.
Definition Types.h:869
std::optional< SwiftNewTypeKind > SwiftWrapper
Definition Types.h:871
API notes for a variable/property.
Definition Types.h:342
std::optional< NullabilityKind > getNullability() const
Definition Types.h:358
const std::string & getType() const
Definition Types.h:369
SwiftNewTypeKind
The kind of a swift_wrapper/swift_newtype.
Definition Types.h:43
EnumExtensibilityKind
The payload for an enum_extensibility attribute.
Definition Types.h:36
const internal::VariadicAllOfMatcher< Decl > decl
Matches declarations.
bool implicitObjectParamIsLifetimeBound(const FunctionDecl *FD)
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
Definition Address.h:330
NullabilityKind
Describes the nullability of a particular type.
Definition Specifiers.h:348
@ Nullable
Values of this type can be null.
Definition Specifiers.h:352
@ Unspecified
Whether values of this type can be null is (explicitly) unspecified.
Definition Specifiers.h:357
@ NonNull
Values of this type can never be null.
Definition Specifiers.h:350
@ Property
The type of a property.
Definition TypeBase.h:911
const char * getOperatorSpelling(OverloadedOperatorKind Operator)
Retrieve the spelling of the given overloaded operator, without the preceding "operator" keyword.
U cast(CodeGen::Address addr)
Definition Address.h:327
OpaquePtr< QualType > ParsedType
An opaque type for threading parsed type information through the parser.
Definition Ownership.h:230
@ Class
The "class" keyword introduces the elaborated-type-specifier.
Definition TypeBase.h:5925
A temporary reference to an Objective-C selector, suitable for referencing selector data on the stack...
Definition Types.h:928
llvm::ArrayRef< llvm::StringRef > Identifiers
Definition Types.h:930