xref: /llvm-project/clang/lib/Sema/SemaAPINotes.cpp (revision 1e3e199ed9f214594e358eb0c7892cdedc703f7a)
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 "CheckExprLifetime.h"
14 #include "TypeLocBuilder.h"
15 #include "clang/APINotes/APINotesReader.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"
20 #include "clang/Basic/SourceLocation.h"
21 #include "clang/Lex/Lexer.h"
22 #include "clang/Sema/SemaObjC.h"
23 #include "clang/Sema/SemaSwift.h"
24 #include <stack>
25 
26 using namespace clang;
27 
28 namespace {
29 enum class IsActive_t : bool { Inactive, Active };
30 enum class IsSubstitution_t : bool { Original, Replacement };
31 
32 struct VersionedInfoMetadata {
33   /// An empty version refers to unversioned metadata.
34   VersionTuple Version;
35   unsigned IsActive : 1;
36   unsigned IsReplacement : 1;
37 
38   VersionedInfoMetadata(VersionTuple Version, IsActive_t Active,
39                         IsSubstitution_t Replacement)
40       : Version(Version), IsActive(Active == IsActive_t::Active),
41         IsReplacement(Replacement == IsSubstitution_t::Replacement) {}
42 };
43 } // end anonymous namespace
44 
45 /// Determine whether this is a multi-level pointer type.
46 static bool isIndirectPointerType(QualType Type) {
47   QualType Pointee = Type->getPointeeType();
48   if (Pointee.isNull())
49     return false;
50 
51   return Pointee->isAnyPointerType() || Pointee->isObjCObjectPointerType() ||
52          Pointee->isMemberPointerType();
53 }
54 
55 /// Apply nullability to the given declaration.
56 static void applyNullability(Sema &S, Decl *D, NullabilityKind Nullability,
57                              VersionedInfoMetadata Metadata) {
58   if (!Metadata.IsActive)
59     return;
60 
61   auto GetModified =
62       [&](Decl *D, QualType QT,
63           NullabilityKind Nullability) -> std::optional<QualType> {
64     QualType Original = QT;
65     S.CheckImplicitNullabilityTypeSpecifier(QT, Nullability, D->getLocation(),
66                                             isa<ParmVarDecl>(D),
67                                             /*OverrideExisting=*/true);
68     return (QT.getTypePtr() != Original.getTypePtr()) ? std::optional(QT)
69                                                       : std::nullopt;
70   };
71 
72   if (auto Function = dyn_cast<FunctionDecl>(D)) {
73     if (auto Modified =
74             GetModified(D, Function->getReturnType(), Nullability)) {
75       const FunctionType *FnType = Function->getType()->castAs<FunctionType>();
76       if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(FnType))
77         Function->setType(S.Context.getFunctionType(
78             *Modified, proto->getParamTypes(), proto->getExtProtoInfo()));
79       else
80         Function->setType(
81             S.Context.getFunctionNoProtoType(*Modified, FnType->getExtInfo()));
82     }
83   } else if (auto Method = dyn_cast<ObjCMethodDecl>(D)) {
84     if (auto Modified = GetModified(D, Method->getReturnType(), Nullability)) {
85       Method->setReturnType(*Modified);
86 
87       // Make it a context-sensitive keyword if we can.
88       if (!isIndirectPointerType(*Modified))
89         Method->setObjCDeclQualifier(Decl::ObjCDeclQualifier(
90             Method->getObjCDeclQualifier() | Decl::OBJC_TQ_CSNullability));
91     }
92   } else if (auto Value = dyn_cast<ValueDecl>(D)) {
93     if (auto Modified = GetModified(D, Value->getType(), Nullability)) {
94       Value->setType(*Modified);
95 
96       // Make it a context-sensitive keyword if we can.
97       if (auto Parm = dyn_cast<ParmVarDecl>(D)) {
98         if (Parm->isObjCMethodParameter() && !isIndirectPointerType(*Modified))
99           Parm->setObjCDeclQualifier(Decl::ObjCDeclQualifier(
100               Parm->getObjCDeclQualifier() | Decl::OBJC_TQ_CSNullability));
101       }
102     }
103   } else if (auto Property = dyn_cast<ObjCPropertyDecl>(D)) {
104     if (auto Modified = GetModified(D, Property->getType(), Nullability)) {
105       Property->setType(*Modified, Property->getTypeSourceInfo());
106 
107       // Make it a property attribute if we can.
108       if (!isIndirectPointerType(*Modified))
109         Property->setPropertyAttributes(
110             ObjCPropertyAttribute::kind_null_resettable);
111     }
112   }
113 }
114 
115 /// Copy a string into ASTContext-allocated memory.
116 static StringRef ASTAllocateString(ASTContext &Ctx, StringRef String) {
117   void *mem = Ctx.Allocate(String.size(), alignof(char *));
118   memcpy(mem, String.data(), String.size());
119   return StringRef(static_cast<char *>(mem), String.size());
120 }
121 
122 static AttributeCommonInfo getPlaceholderAttrInfo() {
123   return AttributeCommonInfo(SourceRange(),
124                              AttributeCommonInfo::UnknownAttribute,
125                              {AttributeCommonInfo::AS_GNU,
126                               /*Spelling*/ 0, /*IsAlignas*/ false,
127                               /*IsRegularKeywordAttribute*/ false});
128 }
129 
130 namespace {
131 template <typename A> struct AttrKindFor {};
132 
133 #define ATTR(X)                                                                \
134   template <> struct AttrKindFor<X##Attr> {                                    \
135     static const attr::Kind value = attr::X;                                   \
136   };
137 #include "clang/Basic/AttrList.inc"
138 
139 /// Handle an attribute introduced by API notes.
140 ///
141 /// \param IsAddition Whether we should add a new attribute
142 /// (otherwise, we might remove an existing attribute).
143 /// \param CreateAttr Create the new attribute to be added.
144 template <typename A>
145 void handleAPINotedAttribute(
146     Sema &S, Decl *D, bool IsAddition, VersionedInfoMetadata Metadata,
147     llvm::function_ref<A *()> CreateAttr,
148     llvm::function_ref<Decl::attr_iterator(const Decl *)> GetExistingAttr) {
149   if (Metadata.IsActive) {
150     auto Existing = GetExistingAttr(D);
151     if (Existing != D->attr_end()) {
152       // Remove the existing attribute, and treat it as a superseded
153       // non-versioned attribute.
154       auto *Versioned = SwiftVersionedAdditionAttr::CreateImplicit(
155           S.Context, Metadata.Version, *Existing, /*IsReplacedByActive*/ true);
156 
157       D->getAttrs().erase(Existing);
158       D->addAttr(Versioned);
159     }
160 
161     // If we're supposed to add a new attribute, do so.
162     if (IsAddition) {
163       if (auto Attr = CreateAttr())
164         D->addAttr(Attr);
165     }
166 
167     return;
168   }
169   if (IsAddition) {
170     if (auto Attr = CreateAttr()) {
171       auto *Versioned = SwiftVersionedAdditionAttr::CreateImplicit(
172           S.Context, Metadata.Version, Attr,
173           /*IsReplacedByActive*/ Metadata.IsReplacement);
174       D->addAttr(Versioned);
175     }
176   } else {
177     // FIXME: This isn't preserving enough information for things like
178     // availability, where we're trying to remove a /specific/ kind of
179     // attribute.
180     auto *Versioned = SwiftVersionedRemovalAttr::CreateImplicit(
181         S.Context, Metadata.Version, AttrKindFor<A>::value,
182         /*IsReplacedByActive*/ Metadata.IsReplacement);
183     D->addAttr(Versioned);
184   }
185 }
186 
187 template <typename A>
188 void handleAPINotedAttribute(Sema &S, Decl *D, bool ShouldAddAttribute,
189                              VersionedInfoMetadata Metadata,
190                              llvm::function_ref<A *()> CreateAttr) {
191   handleAPINotedAttribute<A>(
192       S, D, ShouldAddAttribute, Metadata, CreateAttr, [](const Decl *D) {
193         return llvm::find_if(D->attrs(),
194                              [](const Attr *Next) { return isa<A>(Next); });
195       });
196 }
197 } // namespace
198 
199 template <typename A>
200 static void handleAPINotedRetainCountAttribute(Sema &S, Decl *D,
201                                                bool ShouldAddAttribute,
202                                                VersionedInfoMetadata Metadata) {
203   // The template argument has a default to make the "removal" case more
204   // concise; it doesn't matter /which/ attribute is being removed.
205   handleAPINotedAttribute<A>(
206       S, D, ShouldAddAttribute, Metadata,
207       [&] { return new (S.Context) A(S.Context, getPlaceholderAttrInfo()); },
208       [](const Decl *D) -> Decl::attr_iterator {
209         return llvm::find_if(D->attrs(), [](const Attr *Next) -> bool {
210           return isa<CFReturnsRetainedAttr>(Next) ||
211                  isa<CFReturnsNotRetainedAttr>(Next) ||
212                  isa<NSReturnsRetainedAttr>(Next) ||
213                  isa<NSReturnsNotRetainedAttr>(Next) ||
214                  isa<CFAuditedTransferAttr>(Next);
215         });
216       });
217 }
218 
219 static void handleAPINotedRetainCountConvention(
220     Sema &S, Decl *D, VersionedInfoMetadata Metadata,
221     std::optional<api_notes::RetainCountConventionKind> Convention) {
222   if (!Convention)
223     return;
224   switch (*Convention) {
225   case api_notes::RetainCountConventionKind::None:
226     if (isa<FunctionDecl>(D)) {
227       handleAPINotedRetainCountAttribute<CFUnknownTransferAttr>(
228           S, D, /*shouldAddAttribute*/ true, Metadata);
229     } else {
230       handleAPINotedRetainCountAttribute<CFReturnsRetainedAttr>(
231           S, D, /*shouldAddAttribute*/ false, Metadata);
232     }
233     break;
234   case api_notes::RetainCountConventionKind::CFReturnsRetained:
235     handleAPINotedRetainCountAttribute<CFReturnsRetainedAttr>(
236         S, D, /*shouldAddAttribute*/ true, Metadata);
237     break;
238   case api_notes::RetainCountConventionKind::CFReturnsNotRetained:
239     handleAPINotedRetainCountAttribute<CFReturnsNotRetainedAttr>(
240         S, D, /*shouldAddAttribute*/ true, Metadata);
241     break;
242   case api_notes::RetainCountConventionKind::NSReturnsRetained:
243     handleAPINotedRetainCountAttribute<NSReturnsRetainedAttr>(
244         S, D, /*shouldAddAttribute*/ true, Metadata);
245     break;
246   case api_notes::RetainCountConventionKind::NSReturnsNotRetained:
247     handleAPINotedRetainCountAttribute<NSReturnsNotRetainedAttr>(
248         S, D, /*shouldAddAttribute*/ true, Metadata);
249     break;
250   }
251 }
252 
253 static void ProcessAPINotes(Sema &S, Decl *D,
254                             const api_notes::CommonEntityInfo &Info,
255                             VersionedInfoMetadata Metadata) {
256   // Availability
257   if (Info.Unavailable) {
258     handleAPINotedAttribute<UnavailableAttr>(S, D, true, Metadata, [&] {
259       return new (S.Context)
260           UnavailableAttr(S.Context, getPlaceholderAttrInfo(),
261                           ASTAllocateString(S.Context, Info.UnavailableMsg));
262     });
263   }
264 
265   if (Info.UnavailableInSwift) {
266     handleAPINotedAttribute<AvailabilityAttr>(
267         S, D, true, Metadata,
268         [&] {
269           return new (S.Context) AvailabilityAttr(
270               S.Context, getPlaceholderAttrInfo(),
271               &S.Context.Idents.get("swift"), VersionTuple(), VersionTuple(),
272               VersionTuple(),
273               /*Unavailable=*/true,
274               ASTAllocateString(S.Context, Info.UnavailableMsg),
275               /*Strict=*/false,
276               /*Replacement=*/StringRef(),
277               /*Priority=*/Sema::AP_Explicit,
278               /*Environment=*/nullptr);
279         },
280         [](const Decl *D) {
281           return llvm::find_if(D->attrs(), [](const Attr *next) -> bool {
282             if (const auto *AA = dyn_cast<AvailabilityAttr>(next))
283               if (const auto *II = AA->getPlatform())
284                 return II->isStr("swift");
285             return false;
286           });
287         });
288   }
289 
290   // swift_private
291   if (auto SwiftPrivate = Info.isSwiftPrivate()) {
292     handleAPINotedAttribute<SwiftPrivateAttr>(
293         S, D, *SwiftPrivate, Metadata, [&] {
294           return new (S.Context)
295               SwiftPrivateAttr(S.Context, getPlaceholderAttrInfo());
296         });
297   }
298 
299   // swift_name
300   if (!Info.SwiftName.empty()) {
301     handleAPINotedAttribute<SwiftNameAttr>(
302         S, D, true, Metadata, [&]() -> SwiftNameAttr * {
303           AttributeFactory AF{};
304           AttributePool AP{AF};
305           auto &C = S.getASTContext();
306           ParsedAttr *SNA =
307               AP.create(&C.Idents.get("swift_name"), SourceRange(), nullptr,
308                         SourceLocation(), nullptr, nullptr, nullptr,
309                         ParsedAttr::Form::GNU());
310 
311           if (!S.Swift().DiagnoseName(D, Info.SwiftName, D->getLocation(), *SNA,
312                                       /*IsAsync=*/false))
313             return nullptr;
314 
315           return new (S.Context)
316               SwiftNameAttr(S.Context, getPlaceholderAttrInfo(),
317                             ASTAllocateString(S.Context, Info.SwiftName));
318         });
319   }
320 }
321 
322 static void ProcessAPINotes(Sema &S, Decl *D,
323                             const api_notes::CommonTypeInfo &Info,
324                             VersionedInfoMetadata Metadata) {
325   // swift_bridge
326   if (auto SwiftBridge = Info.getSwiftBridge()) {
327     handleAPINotedAttribute<SwiftBridgeAttr>(
328         S, D, !SwiftBridge->empty(), Metadata, [&] {
329           return new (S.Context)
330               SwiftBridgeAttr(S.Context, getPlaceholderAttrInfo(),
331                               ASTAllocateString(S.Context, *SwiftBridge));
332         });
333   }
334 
335   // ns_error_domain
336   if (auto NSErrorDomain = Info.getNSErrorDomain()) {
337     handleAPINotedAttribute<NSErrorDomainAttr>(
338         S, D, !NSErrorDomain->empty(), Metadata, [&] {
339           return new (S.Context)
340               NSErrorDomainAttr(S.Context, getPlaceholderAttrInfo(),
341                                 &S.Context.Idents.get(*NSErrorDomain));
342         });
343   }
344 
345   ProcessAPINotes(S, D, static_cast<const api_notes::CommonEntityInfo &>(Info),
346                   Metadata);
347 }
348 
349 /// Check that the replacement type provided by API notes is reasonable.
350 ///
351 /// This is a very weak form of ABI check.
352 static bool checkAPINotesReplacementType(Sema &S, SourceLocation Loc,
353                                          QualType OrigType,
354                                          QualType ReplacementType) {
355   if (S.Context.getTypeSize(OrigType) !=
356       S.Context.getTypeSize(ReplacementType)) {
357     S.Diag(Loc, diag::err_incompatible_replacement_type)
358         << ReplacementType << OrigType;
359     return true;
360   }
361 
362   return false;
363 }
364 
365 /// Process API notes for a variable or property.
366 static void ProcessAPINotes(Sema &S, Decl *D,
367                             const api_notes::VariableInfo &Info,
368                             VersionedInfoMetadata Metadata) {
369   // Type override.
370   if (Metadata.IsActive && !Info.getType().empty() &&
371       S.ParseTypeFromStringCallback) {
372     auto ParsedType = S.ParseTypeFromStringCallback(
373         Info.getType(), "<API Notes>", D->getLocation());
374     if (ParsedType.isUsable()) {
375       QualType Type = Sema::GetTypeFromParser(ParsedType.get());
376       auto TypeInfo =
377           S.Context.getTrivialTypeSourceInfo(Type, D->getLocation());
378 
379       if (auto Var = dyn_cast<VarDecl>(D)) {
380         // Make adjustments to parameter types.
381         if (isa<ParmVarDecl>(Var)) {
382           Type = S.ObjC().AdjustParameterTypeForObjCAutoRefCount(
383               Type, D->getLocation(), TypeInfo);
384           Type = S.Context.getAdjustedParameterType(Type);
385         }
386 
387         if (!checkAPINotesReplacementType(S, Var->getLocation(), Var->getType(),
388                                           Type)) {
389           Var->setType(Type);
390           Var->setTypeSourceInfo(TypeInfo);
391         }
392       } else if (auto Property = dyn_cast<ObjCPropertyDecl>(D)) {
393         if (!checkAPINotesReplacementType(S, Property->getLocation(),
394                                           Property->getType(), Type))
395           Property->setType(Type, TypeInfo);
396 
397       } else
398         llvm_unreachable("API notes allowed a type on an unknown declaration");
399     }
400   }
401 
402   // Nullability.
403   if (auto Nullability = Info.getNullability())
404     applyNullability(S, D, *Nullability, Metadata);
405 
406   // Handle common entity information.
407   ProcessAPINotes(S, D, static_cast<const api_notes::CommonEntityInfo &>(Info),
408                   Metadata);
409 }
410 
411 /// Process API notes for a parameter.
412 static void ProcessAPINotes(Sema &S, ParmVarDecl *D,
413                             const api_notes::ParamInfo &Info,
414                             VersionedInfoMetadata Metadata) {
415   // noescape
416   if (auto NoEscape = Info.isNoEscape())
417     handleAPINotedAttribute<NoEscapeAttr>(S, D, *NoEscape, Metadata, [&] {
418       return new (S.Context) NoEscapeAttr(S.Context, getPlaceholderAttrInfo());
419     });
420 
421   if (auto Lifetimebound = Info.isLifetimebound())
422     handleAPINotedAttribute<LifetimeBoundAttr>(
423         S, D, *Lifetimebound, Metadata, [&] {
424           return new (S.Context)
425               LifetimeBoundAttr(S.Context, getPlaceholderAttrInfo());
426         });
427 
428   // Retain count convention
429   handleAPINotedRetainCountConvention(S, D, Metadata,
430                                       Info.getRetainCountConvention());
431 
432   // Handle common entity information.
433   ProcessAPINotes(S, D, static_cast<const api_notes::VariableInfo &>(Info),
434                   Metadata);
435 }
436 
437 /// Process API notes for a global variable.
438 static void ProcessAPINotes(Sema &S, VarDecl *D,
439                             const api_notes::GlobalVariableInfo &Info,
440                             VersionedInfoMetadata metadata) {
441   // Handle common entity information.
442   ProcessAPINotes(S, D, static_cast<const api_notes::VariableInfo &>(Info),
443                   metadata);
444 }
445 
446 /// Process API notes for a C field.
447 static void ProcessAPINotes(Sema &S, FieldDecl *D,
448                             const api_notes::FieldInfo &Info,
449                             VersionedInfoMetadata metadata) {
450   // Handle common entity information.
451   ProcessAPINotes(S, D, static_cast<const api_notes::VariableInfo &>(Info),
452                   metadata);
453 }
454 
455 /// Process API notes for an Objective-C property.
456 static void ProcessAPINotes(Sema &S, ObjCPropertyDecl *D,
457                             const api_notes::ObjCPropertyInfo &Info,
458                             VersionedInfoMetadata Metadata) {
459   // Handle common entity information.
460   ProcessAPINotes(S, D, static_cast<const api_notes::VariableInfo &>(Info),
461                   Metadata);
462 
463   if (auto AsAccessors = Info.getSwiftImportAsAccessors()) {
464     handleAPINotedAttribute<SwiftImportPropertyAsAccessorsAttr>(
465         S, D, *AsAccessors, Metadata, [&] {
466           return new (S.Context) SwiftImportPropertyAsAccessorsAttr(
467               S.Context, getPlaceholderAttrInfo());
468         });
469   }
470 }
471 
472 namespace {
473 typedef llvm::PointerUnion<FunctionDecl *, ObjCMethodDecl *> FunctionOrMethod;
474 }
475 
476 /// Process API notes for a function or method.
477 static void ProcessAPINotes(Sema &S, FunctionOrMethod AnyFunc,
478                             const api_notes::FunctionInfo &Info,
479                             VersionedInfoMetadata Metadata) {
480   // Find the declaration itself.
481   FunctionDecl *FD = AnyFunc.dyn_cast<FunctionDecl *>();
482   Decl *D = FD;
483   ObjCMethodDecl *MD = nullptr;
484   if (!D) {
485     MD = cast<ObjCMethodDecl *>(AnyFunc);
486     D = MD;
487   }
488 
489   assert((FD || MD) && "Expecting Function or ObjCMethod");
490 
491   // Nullability of return type.
492   if (Info.NullabilityAudited)
493     applyNullability(S, D, Info.getReturnTypeInfo(), Metadata);
494 
495   // Parameters.
496   unsigned NumParams = FD ? FD->getNumParams() : MD->param_size();
497 
498   bool AnyTypeChanged = false;
499   for (unsigned I = 0; I != NumParams; ++I) {
500     ParmVarDecl *Param = FD ? FD->getParamDecl(I) : MD->param_begin()[I];
501     QualType ParamTypeBefore = Param->getType();
502 
503     if (I < Info.Params.size())
504       ProcessAPINotes(S, Param, Info.Params[I], Metadata);
505 
506     // Nullability.
507     if (Info.NullabilityAudited)
508       applyNullability(S, Param, Info.getParamTypeInfo(I), Metadata);
509 
510     if (ParamTypeBefore.getAsOpaquePtr() != Param->getType().getAsOpaquePtr())
511       AnyTypeChanged = true;
512   }
513 
514   // Result type override.
515   QualType OverriddenResultType;
516   if (Metadata.IsActive && !Info.ResultType.empty() &&
517       S.ParseTypeFromStringCallback) {
518     auto ParsedType = S.ParseTypeFromStringCallback(
519         Info.ResultType, "<API Notes>", D->getLocation());
520     if (ParsedType.isUsable()) {
521       QualType ResultType = Sema::GetTypeFromParser(ParsedType.get());
522 
523       if (MD) {
524         if (!checkAPINotesReplacementType(S, D->getLocation(),
525                                           MD->getReturnType(), ResultType)) {
526           auto ResultTypeInfo =
527               S.Context.getTrivialTypeSourceInfo(ResultType, D->getLocation());
528           MD->setReturnType(ResultType);
529           MD->setReturnTypeSourceInfo(ResultTypeInfo);
530         }
531       } else if (!checkAPINotesReplacementType(
532                      S, FD->getLocation(), FD->getReturnType(), ResultType)) {
533         OverriddenResultType = ResultType;
534         AnyTypeChanged = true;
535       }
536     }
537   }
538 
539   // If the result type or any of the parameter types changed for a function
540   // declaration, we have to rebuild the type.
541   if (FD && AnyTypeChanged) {
542     if (const auto *fnProtoType = FD->getType()->getAs<FunctionProtoType>()) {
543       if (OverriddenResultType.isNull())
544         OverriddenResultType = fnProtoType->getReturnType();
545 
546       SmallVector<QualType, 4> ParamTypes;
547       for (auto Param : FD->parameters())
548         ParamTypes.push_back(Param->getType());
549 
550       FD->setType(S.Context.getFunctionType(OverriddenResultType, ParamTypes,
551                                             fnProtoType->getExtProtoInfo()));
552     } else if (!OverriddenResultType.isNull()) {
553       const auto *FnNoProtoType = FD->getType()->castAs<FunctionNoProtoType>();
554       FD->setType(S.Context.getFunctionNoProtoType(
555           OverriddenResultType, FnNoProtoType->getExtInfo()));
556     }
557   }
558 
559   // Retain count convention
560   handleAPINotedRetainCountConvention(S, D, Metadata,
561                                       Info.getRetainCountConvention());
562 
563   // Handle common entity information.
564   ProcessAPINotes(S, D, static_cast<const api_notes::CommonEntityInfo &>(Info),
565                   Metadata);
566 }
567 
568 /// Process API notes for a C++ method.
569 static void ProcessAPINotes(Sema &S, CXXMethodDecl *Method,
570                             const api_notes::CXXMethodInfo &Info,
571                             VersionedInfoMetadata Metadata) {
572   if (Info.This && Info.This->isLifetimebound() &&
573       !sema::implicitObjectParamIsLifetimeBound(Method)) {
574     auto MethodType = Method->getType();
575     auto *attr = ::new (S.Context)
576         LifetimeBoundAttr(S.Context, getPlaceholderAttrInfo());
577     QualType AttributedType =
578         S.Context.getAttributedType(attr, MethodType, MethodType);
579     TypeLocBuilder TLB;
580     TLB.pushFullCopy(Method->getTypeSourceInfo()->getTypeLoc());
581     AttributedTypeLoc TyLoc = TLB.push<AttributedTypeLoc>(AttributedType);
582     TyLoc.setAttr(attr);
583     Method->setType(AttributedType);
584     Method->setTypeSourceInfo(TLB.getTypeSourceInfo(S.Context, AttributedType));
585   }
586 
587   ProcessAPINotes(S, (FunctionOrMethod)Method, Info, Metadata);
588 }
589 
590 /// Process API notes for a global function.
591 static void ProcessAPINotes(Sema &S, FunctionDecl *D,
592                             const api_notes::GlobalFunctionInfo &Info,
593                             VersionedInfoMetadata Metadata) {
594   // Handle common function information.
595   ProcessAPINotes(S, FunctionOrMethod(D),
596                   static_cast<const api_notes::FunctionInfo &>(Info), Metadata);
597 }
598 
599 /// Process API notes for an enumerator.
600 static void ProcessAPINotes(Sema &S, EnumConstantDecl *D,
601                             const api_notes::EnumConstantInfo &Info,
602                             VersionedInfoMetadata Metadata) {
603   // Handle common information.
604   ProcessAPINotes(S, D, static_cast<const api_notes::CommonEntityInfo &>(Info),
605                   Metadata);
606 }
607 
608 /// Process API notes for an Objective-C method.
609 static void ProcessAPINotes(Sema &S, ObjCMethodDecl *D,
610                             const api_notes::ObjCMethodInfo &Info,
611                             VersionedInfoMetadata Metadata) {
612   // Designated initializers.
613   if (Info.DesignatedInit) {
614     handleAPINotedAttribute<ObjCDesignatedInitializerAttr>(
615         S, D, true, Metadata, [&] {
616           if (ObjCInterfaceDecl *IFace = D->getClassInterface())
617             IFace->setHasDesignatedInitializers();
618 
619           return new (S.Context) ObjCDesignatedInitializerAttr(
620               S.Context, getPlaceholderAttrInfo());
621         });
622   }
623 
624   // Handle common function information.
625   ProcessAPINotes(S, FunctionOrMethod(D),
626                   static_cast<const api_notes::FunctionInfo &>(Info), Metadata);
627 }
628 
629 /// Process API notes for a tag.
630 static void ProcessAPINotes(Sema &S, TagDecl *D, const api_notes::TagInfo &Info,
631                             VersionedInfoMetadata Metadata) {
632   if (auto ImportAs = Info.SwiftImportAs)
633     D->addAttr(SwiftAttrAttr::Create(S.Context, "import_" + ImportAs.value()));
634 
635   if (auto RetainOp = Info.SwiftRetainOp)
636     D->addAttr(SwiftAttrAttr::Create(S.Context, "retain:" + RetainOp.value()));
637 
638   if (auto ReleaseOp = Info.SwiftReleaseOp)
639     D->addAttr(
640         SwiftAttrAttr::Create(S.Context, "release:" + ReleaseOp.value()));
641 
642   if (auto ConformsTo = Info.SwiftConformance)
643     D->addAttr(
644         SwiftAttrAttr::Create(S.Context, "conforms_to:" + ConformsTo.value()));
645 
646   if (auto Copyable = Info.isSwiftCopyable()) {
647     if (!*Copyable)
648       D->addAttr(SwiftAttrAttr::Create(S.Context, "~Copyable"));
649   }
650 
651   if (auto Escapable = Info.isSwiftEscapable()) {
652     D->addAttr(SwiftAttrAttr::Create(S.Context,
653                                      *Escapable ? "Escapable" : "~Escapable"));
654   }
655 
656   if (auto Extensibility = Info.EnumExtensibility) {
657     using api_notes::EnumExtensibilityKind;
658     bool ShouldAddAttribute = (*Extensibility != EnumExtensibilityKind::None);
659     handleAPINotedAttribute<EnumExtensibilityAttr>(
660         S, D, ShouldAddAttribute, Metadata, [&] {
661           EnumExtensibilityAttr::Kind kind;
662           switch (*Extensibility) {
663           case EnumExtensibilityKind::None:
664             llvm_unreachable("remove only");
665           case EnumExtensibilityKind::Open:
666             kind = EnumExtensibilityAttr::Open;
667             break;
668           case EnumExtensibilityKind::Closed:
669             kind = EnumExtensibilityAttr::Closed;
670             break;
671           }
672           return new (S.Context)
673               EnumExtensibilityAttr(S.Context, getPlaceholderAttrInfo(), kind);
674         });
675   }
676 
677   if (auto FlagEnum = Info.isFlagEnum()) {
678     handleAPINotedAttribute<FlagEnumAttr>(S, D, *FlagEnum, Metadata, [&] {
679       return new (S.Context) FlagEnumAttr(S.Context, getPlaceholderAttrInfo());
680     });
681   }
682 
683   // Handle common type information.
684   ProcessAPINotes(S, D, static_cast<const api_notes::CommonTypeInfo &>(Info),
685                   Metadata);
686 }
687 
688 /// Process API notes for a typedef.
689 static void ProcessAPINotes(Sema &S, TypedefNameDecl *D,
690                             const api_notes::TypedefInfo &Info,
691                             VersionedInfoMetadata Metadata) {
692   // swift_wrapper
693   using SwiftWrapperKind = api_notes::SwiftNewTypeKind;
694 
695   if (auto SwiftWrapper = Info.SwiftWrapper) {
696     handleAPINotedAttribute<SwiftNewTypeAttr>(
697         S, D, *SwiftWrapper != SwiftWrapperKind::None, Metadata, [&] {
698           SwiftNewTypeAttr::NewtypeKind Kind;
699           switch (*SwiftWrapper) {
700           case SwiftWrapperKind::None:
701             llvm_unreachable("Shouldn't build an attribute");
702 
703           case SwiftWrapperKind::Struct:
704             Kind = SwiftNewTypeAttr::NK_Struct;
705             break;
706 
707           case SwiftWrapperKind::Enum:
708             Kind = SwiftNewTypeAttr::NK_Enum;
709             break;
710           }
711           AttributeCommonInfo SyntaxInfo{
712               SourceRange(),
713               AttributeCommonInfo::AT_SwiftNewType,
714               {AttributeCommonInfo::AS_GNU, SwiftNewTypeAttr::GNU_swift_wrapper,
715                /*IsAlignas*/ false, /*IsRegularKeywordAttribute*/ false}};
716           return new (S.Context) SwiftNewTypeAttr(S.Context, SyntaxInfo, Kind);
717         });
718   }
719 
720   // Handle common type information.
721   ProcessAPINotes(S, D, static_cast<const api_notes::CommonTypeInfo &>(Info),
722                   Metadata);
723 }
724 
725 /// Process API notes for an Objective-C class or protocol.
726 static void ProcessAPINotes(Sema &S, ObjCContainerDecl *D,
727                             const api_notes::ContextInfo &Info,
728                             VersionedInfoMetadata Metadata) {
729   // Handle common type information.
730   ProcessAPINotes(S, D, static_cast<const api_notes::CommonTypeInfo &>(Info),
731                   Metadata);
732 }
733 
734 /// Process API notes for an Objective-C class.
735 static void ProcessAPINotes(Sema &S, ObjCInterfaceDecl *D,
736                             const api_notes::ContextInfo &Info,
737                             VersionedInfoMetadata Metadata) {
738   if (auto AsNonGeneric = Info.getSwiftImportAsNonGeneric()) {
739     handleAPINotedAttribute<SwiftImportAsNonGenericAttr>(
740         S, D, *AsNonGeneric, Metadata, [&] {
741           return new (S.Context)
742               SwiftImportAsNonGenericAttr(S.Context, getPlaceholderAttrInfo());
743         });
744   }
745 
746   if (auto ObjcMembers = Info.getSwiftObjCMembers()) {
747     handleAPINotedAttribute<SwiftObjCMembersAttr>(
748         S, D, *ObjcMembers, Metadata, [&] {
749           return new (S.Context)
750               SwiftObjCMembersAttr(S.Context, getPlaceholderAttrInfo());
751         });
752   }
753 
754   // Handle information common to Objective-C classes and protocols.
755   ProcessAPINotes(S, static_cast<clang::ObjCContainerDecl *>(D), Info,
756                   Metadata);
757 }
758 
759 /// If we're applying API notes with an active, non-default version, and the
760 /// versioned API notes have a SwiftName but the declaration normally wouldn't
761 /// have one, add a removal attribute to make it clear that the new SwiftName
762 /// attribute only applies to the active version of \p D, not to all versions.
763 ///
764 /// This must be run \em before processing API notes for \p D, because otherwise
765 /// any existing SwiftName attribute will have been packaged up in a
766 /// SwiftVersionedAdditionAttr.
767 template <typename SpecificInfo>
768 static void maybeAttachUnversionedSwiftName(
769     Sema &S, Decl *D,
770     const api_notes::APINotesReader::VersionedInfo<SpecificInfo> Info) {
771   if (D->hasAttr<SwiftNameAttr>())
772     return;
773   if (!Info.getSelected())
774     return;
775 
776   // Is the active slice versioned, and does it set a Swift name?
777   VersionTuple SelectedVersion;
778   SpecificInfo SelectedInfoSlice;
779   std::tie(SelectedVersion, SelectedInfoSlice) = Info[*Info.getSelected()];
780   if (SelectedVersion.empty())
781     return;
782   if (SelectedInfoSlice.SwiftName.empty())
783     return;
784 
785   // Does the unversioned slice /not/ set a Swift name?
786   for (const auto &VersionAndInfoSlice : Info) {
787     if (!VersionAndInfoSlice.first.empty())
788       continue;
789     if (!VersionAndInfoSlice.second.SwiftName.empty())
790       return;
791   }
792 
793   // Then explicitly call that out with a removal attribute.
794   VersionedInfoMetadata DummyFutureMetadata(
795       SelectedVersion, IsActive_t::Inactive, IsSubstitution_t::Replacement);
796   handleAPINotedAttribute<SwiftNameAttr>(
797       S, D, /*add*/ false, DummyFutureMetadata, []() -> SwiftNameAttr * {
798         llvm_unreachable("should not try to add an attribute here");
799       });
800 }
801 
802 /// Processes all versions of versioned API notes.
803 ///
804 /// Just dispatches to the various ProcessAPINotes functions in this file.
805 template <typename SpecificDecl, typename SpecificInfo>
806 static void ProcessVersionedAPINotes(
807     Sema &S, SpecificDecl *D,
808     const api_notes::APINotesReader::VersionedInfo<SpecificInfo> Info) {
809 
810   maybeAttachUnversionedSwiftName(S, D, Info);
811 
812   unsigned Selected = Info.getSelected().value_or(Info.size());
813 
814   VersionTuple Version;
815   SpecificInfo InfoSlice;
816   for (unsigned i = 0, e = Info.size(); i != e; ++i) {
817     std::tie(Version, InfoSlice) = Info[i];
818     auto Active = (i == Selected) ? IsActive_t::Active : IsActive_t::Inactive;
819     auto Replacement = IsSubstitution_t::Original;
820     if (Active == IsActive_t::Inactive && Version.empty()) {
821       Replacement = IsSubstitution_t::Replacement;
822       Version = Info[Selected].first;
823     }
824     ProcessAPINotes(S, D, InfoSlice,
825                     VersionedInfoMetadata(Version, Active, Replacement));
826   }
827 }
828 
829 static std::optional<api_notes::Context>
830 UnwindNamespaceContext(DeclContext *DC, api_notes::APINotesManager &APINotes) {
831   if (auto NamespaceContext = dyn_cast<NamespaceDecl>(DC)) {
832     for (auto Reader : APINotes.findAPINotes(NamespaceContext->getLocation())) {
833       // Retrieve the context ID for the parent namespace of the decl.
834       std::stack<NamespaceDecl *> NamespaceStack;
835       {
836         for (auto CurrentNamespace = NamespaceContext; CurrentNamespace;
837              CurrentNamespace =
838                  dyn_cast<NamespaceDecl>(CurrentNamespace->getParent())) {
839           if (!CurrentNamespace->isInlineNamespace())
840             NamespaceStack.push(CurrentNamespace);
841         }
842       }
843       std::optional<api_notes::ContextID> NamespaceID;
844       while (!NamespaceStack.empty()) {
845         auto CurrentNamespace = NamespaceStack.top();
846         NamespaceStack.pop();
847         NamespaceID =
848             Reader->lookupNamespaceID(CurrentNamespace->getName(), NamespaceID);
849         if (!NamespaceID)
850           return std::nullopt;
851       }
852       if (NamespaceID)
853         return api_notes::Context(*NamespaceID,
854                                   api_notes::ContextKind::Namespace);
855     }
856   }
857   return std::nullopt;
858 }
859 
860 static std::optional<api_notes::Context>
861 UnwindTagContext(TagDecl *DC, api_notes::APINotesManager &APINotes) {
862   assert(DC && "tag context must not be null");
863   for (auto Reader : APINotes.findAPINotes(DC->getLocation())) {
864     // Retrieve the context ID for the parent tag of the decl.
865     std::stack<TagDecl *> TagStack;
866     {
867       for (auto CurrentTag = DC; CurrentTag;
868            CurrentTag = dyn_cast<TagDecl>(CurrentTag->getParent()))
869         TagStack.push(CurrentTag);
870     }
871     assert(!TagStack.empty());
872     std::optional<api_notes::Context> Ctx =
873         UnwindNamespaceContext(TagStack.top()->getDeclContext(), APINotes);
874     while (!TagStack.empty()) {
875       auto CurrentTag = TagStack.top();
876       TagStack.pop();
877       auto CtxID = Reader->lookupTagID(CurrentTag->getName(), Ctx);
878       if (!CtxID)
879         return std::nullopt;
880       Ctx = api_notes::Context(*CtxID, api_notes::ContextKind::Tag);
881     }
882     return Ctx;
883   }
884   return std::nullopt;
885 }
886 
887 /// Process API notes that are associated with this declaration, mapping them
888 /// to attributes as appropriate.
889 void Sema::ProcessAPINotes(Decl *D) {
890   if (!D)
891     return;
892 
893   auto *DC = D->getDeclContext();
894   // Globals.
895   if (DC->isFileContext() || DC->isNamespace() || DC->isExternCContext() ||
896       DC->isExternCXXContext()) {
897     std::optional<api_notes::Context> APINotesContext =
898         UnwindNamespaceContext(DC, APINotes);
899     // Global variables.
900     if (auto VD = dyn_cast<VarDecl>(D)) {
901       for (auto Reader : APINotes.findAPINotes(D->getLocation())) {
902         auto Info =
903             Reader->lookupGlobalVariable(VD->getName(), APINotesContext);
904         ProcessVersionedAPINotes(*this, VD, Info);
905       }
906 
907       return;
908     }
909 
910     // Global functions.
911     if (auto FD = dyn_cast<FunctionDecl>(D)) {
912       if (FD->getDeclName().isIdentifier()) {
913         for (auto Reader : APINotes.findAPINotes(D->getLocation())) {
914           auto Info =
915               Reader->lookupGlobalFunction(FD->getName(), APINotesContext);
916           ProcessVersionedAPINotes(*this, FD, Info);
917         }
918       }
919 
920       return;
921     }
922 
923     // Objective-C classes.
924     if (auto Class = dyn_cast<ObjCInterfaceDecl>(D)) {
925       for (auto Reader : APINotes.findAPINotes(D->getLocation())) {
926         auto Info = Reader->lookupObjCClassInfo(Class->getName());
927         ProcessVersionedAPINotes(*this, Class, Info);
928       }
929 
930       return;
931     }
932 
933     // Objective-C protocols.
934     if (auto Protocol = dyn_cast<ObjCProtocolDecl>(D)) {
935       for (auto Reader : APINotes.findAPINotes(D->getLocation())) {
936         auto Info = Reader->lookupObjCProtocolInfo(Protocol->getName());
937         ProcessVersionedAPINotes(*this, Protocol, Info);
938       }
939 
940       return;
941     }
942 
943     // Tags
944     if (auto Tag = dyn_cast<TagDecl>(D)) {
945       // Determine the name of the entity to search for. If this is an
946       // anonymous tag that gets its linked name from a typedef, look for the
947       // typedef name. This allows tag-specific information to be added
948       // to the declaration.
949       std::string LookupName;
950       if (auto typedefName = Tag->getTypedefNameForAnonDecl())
951         LookupName = typedefName->getName().str();
952       else
953         LookupName = Tag->getName().str();
954 
955       // Use the source location to discern if this Tag is an OPTIONS macro.
956       // For now we would like to limit this trick of looking up the APINote tag
957       // using the EnumDecl's QualType in the case where the enum is anonymous.
958       // This is only being used to support APINotes lookup for C++
959       // NS/CF_OPTIONS when C++-Interop is enabled.
960       std::string MacroName =
961           LookupName.empty() && Tag->getOuterLocStart().isMacroID()
962               ? clang::Lexer::getImmediateMacroName(
963                     Tag->getOuterLocStart(),
964                     Tag->getASTContext().getSourceManager(), LangOpts)
965                     .str()
966               : "";
967 
968       if (LookupName.empty() && isa<clang::EnumDecl>(Tag) &&
969           (MacroName == "CF_OPTIONS" || MacroName == "NS_OPTIONS" ||
970            MacroName == "OBJC_OPTIONS" || MacroName == "SWIFT_OPTIONS")) {
971 
972         clang::QualType T = llvm::cast<clang::EnumDecl>(Tag)->getIntegerType();
973         LookupName = clang::QualType::getAsString(
974             T.split(), getASTContext().getPrintingPolicy());
975       }
976 
977       for (auto Reader : APINotes.findAPINotes(D->getLocation())) {
978         if (auto ParentTag = dyn_cast<TagDecl>(Tag->getDeclContext()))
979           APINotesContext = UnwindTagContext(ParentTag, APINotes);
980         auto Info = Reader->lookupTag(LookupName, APINotesContext);
981         ProcessVersionedAPINotes(*this, Tag, Info);
982       }
983 
984       return;
985     }
986 
987     // Typedefs
988     if (auto Typedef = dyn_cast<TypedefNameDecl>(D)) {
989       for (auto Reader : APINotes.findAPINotes(D->getLocation())) {
990         auto Info = Reader->lookupTypedef(Typedef->getName(), APINotesContext);
991         ProcessVersionedAPINotes(*this, Typedef, Info);
992       }
993 
994       return;
995     }
996   }
997 
998   // Enumerators.
999   if (DC->getRedeclContext()->isFileContext() ||
1000       DC->getRedeclContext()->isExternCContext()) {
1001     if (auto EnumConstant = dyn_cast<EnumConstantDecl>(D)) {
1002       for (auto Reader : APINotes.findAPINotes(D->getLocation())) {
1003         auto Info = Reader->lookupEnumConstant(EnumConstant->getName());
1004         ProcessVersionedAPINotes(*this, EnumConstant, Info);
1005       }
1006 
1007       return;
1008     }
1009   }
1010 
1011   if (auto ObjCContainer = dyn_cast<ObjCContainerDecl>(DC)) {
1012     // Location function that looks up an Objective-C context.
1013     auto GetContext = [&](api_notes::APINotesReader *Reader)
1014         -> std::optional<api_notes::ContextID> {
1015       if (auto Protocol = dyn_cast<ObjCProtocolDecl>(ObjCContainer)) {
1016         if (auto Found = Reader->lookupObjCProtocolID(Protocol->getName()))
1017           return *Found;
1018 
1019         return std::nullopt;
1020       }
1021 
1022       if (auto Impl = dyn_cast<ObjCCategoryImplDecl>(ObjCContainer)) {
1023         if (auto Cat = Impl->getCategoryDecl())
1024           ObjCContainer = Cat->getClassInterface();
1025         else
1026           return std::nullopt;
1027       }
1028 
1029       if (auto Category = dyn_cast<ObjCCategoryDecl>(ObjCContainer)) {
1030         if (Category->getClassInterface())
1031           ObjCContainer = Category->getClassInterface();
1032         else
1033           return std::nullopt;
1034       }
1035 
1036       if (auto Impl = dyn_cast<ObjCImplDecl>(ObjCContainer)) {
1037         if (Impl->getClassInterface())
1038           ObjCContainer = Impl->getClassInterface();
1039         else
1040           return std::nullopt;
1041       }
1042 
1043       if (auto Class = dyn_cast<ObjCInterfaceDecl>(ObjCContainer)) {
1044         if (auto Found = Reader->lookupObjCClassID(Class->getName()))
1045           return *Found;
1046 
1047         return std::nullopt;
1048       }
1049 
1050       return std::nullopt;
1051     };
1052 
1053     // Objective-C methods.
1054     if (auto Method = dyn_cast<ObjCMethodDecl>(D)) {
1055       for (auto Reader : APINotes.findAPINotes(D->getLocation())) {
1056         if (auto Context = GetContext(Reader)) {
1057           // Map the selector.
1058           Selector Sel = Method->getSelector();
1059           SmallVector<StringRef, 2> SelPieces;
1060           if (Sel.isUnarySelector()) {
1061             SelPieces.push_back(Sel.getNameForSlot(0));
1062           } else {
1063             for (unsigned i = 0, n = Sel.getNumArgs(); i != n; ++i)
1064               SelPieces.push_back(Sel.getNameForSlot(i));
1065           }
1066 
1067           api_notes::ObjCSelectorRef SelectorRef;
1068           SelectorRef.NumArgs = Sel.getNumArgs();
1069           SelectorRef.Identifiers = SelPieces;
1070 
1071           auto Info = Reader->lookupObjCMethod(*Context, SelectorRef,
1072                                                Method->isInstanceMethod());
1073           ProcessVersionedAPINotes(*this, Method, Info);
1074         }
1075       }
1076     }
1077 
1078     // Objective-C properties.
1079     if (auto Property = dyn_cast<ObjCPropertyDecl>(D)) {
1080       for (auto Reader : APINotes.findAPINotes(D->getLocation())) {
1081         if (auto Context = GetContext(Reader)) {
1082           bool isInstanceProperty =
1083               (Property->getPropertyAttributesAsWritten() &
1084                ObjCPropertyAttribute::kind_class) == 0;
1085           auto Info = Reader->lookupObjCProperty(*Context, Property->getName(),
1086                                                  isInstanceProperty);
1087           ProcessVersionedAPINotes(*this, Property, Info);
1088         }
1089       }
1090 
1091       return;
1092     }
1093   }
1094 
1095   if (auto TagContext = dyn_cast<TagDecl>(DC)) {
1096     if (auto CXXMethod = dyn_cast<CXXMethodDecl>(D)) {
1097       if (!isa<CXXConstructorDecl>(CXXMethod) &&
1098           !isa<CXXDestructorDecl>(CXXMethod) &&
1099           !isa<CXXConversionDecl>(CXXMethod) &&
1100           !CXXMethod->isOverloadedOperator()) {
1101         for (auto Reader : APINotes.findAPINotes(D->getLocation())) {
1102           if (auto Context = UnwindTagContext(TagContext, APINotes)) {
1103             auto Info =
1104                 Reader->lookupCXXMethod(Context->id, CXXMethod->getName());
1105             ProcessVersionedAPINotes(*this, CXXMethod, Info);
1106           }
1107         }
1108       }
1109     }
1110 
1111     if (auto Field = dyn_cast<FieldDecl>(D)) {
1112       if (!Field->isUnnamedBitField() && !Field->isAnonymousStructOrUnion()) {
1113         for (auto Reader : APINotes.findAPINotes(D->getLocation())) {
1114           if (auto Context = UnwindTagContext(TagContext, APINotes)) {
1115             auto Info = Reader->lookupField(Context->id, Field->getName());
1116             ProcessVersionedAPINotes(*this, Field, Info);
1117           }
1118         }
1119       }
1120     }
1121 
1122     if (auto Tag = dyn_cast<TagDecl>(D)) {
1123       for (auto Reader : APINotes.findAPINotes(D->getLocation())) {
1124         if (auto Context = UnwindTagContext(TagContext, APINotes)) {
1125           auto Info = Reader->lookupTag(Tag->getName(), Context);
1126           ProcessVersionedAPINotes(*this, Tag, Info);
1127         }
1128       }
1129     }
1130   }
1131 }
1132