21#include "llvm/ADT/StringSet.h"
22#include "llvm/Support/SourceMgr.h"
23#include "llvm/Support/VersionTuple.h"
24#include "llvm/Support/YAMLTraits.h"
29using namespace api_notes;
32enum class APIAvailability {
41template <>
struct ScalarEnumerationTraits<APIAvailability> {
43 IO.enumCase(AA,
"none", APIAvailability::None);
44 IO.enumCase(AA,
"nonswift", APIAvailability::NonSwift);
45 IO.enumCase(AA,
"available", APIAvailability::Available);
52enum class MethodKind {
60template <>
struct ScalarEnumerationTraits<MethodKind> {
62 IO.enumCase(MK,
"Class", MethodKind::Class);
63 IO.enumCase(MK,
"Instance", MethodKind::Instance);
72 std::optional<bool> NoEscape =
false;
74 std::optional<RetainCountConventionKind> RetainCountConvention;
78typedef std::vector<Param> ParamsSeq;
81LLVM_YAML_IS_SEQUENCE_VECTOR(Param)
88 IO.enumCase(NK,
"Nonnull", NullabilityKind::NonNull);
89 IO.enumCase(NK,
"Optional", NullabilityKind::Nullable);
90 IO.enumCase(NK,
"Unspecified", NullabilityKind::Unspecified);
91 IO.enumCase(NK,
"NullableResult", NullabilityKind::NullableResult);
94 IO.enumCase(NK,
"Scalar", NullabilityKind::Unspecified);
97 IO.enumCase(NK,
"N", NullabilityKind::NonNull);
98 IO.enumCase(NK,
"O", NullabilityKind::Nullable);
99 IO.enumCase(NK,
"U", NullabilityKind::Unspecified);
100 IO.enumCase(NK,
"S", NullabilityKind::Unspecified);
106 IO.enumCase(RCCK,
"none", RetainCountConventionKind::None);
107 IO.enumCase(RCCK,
"CFReturnsRetained",
108 RetainCountConventionKind::CFReturnsRetained);
109 IO.enumCase(RCCK,
"CFReturnsNotRetained",
110 RetainCountConventionKind::CFReturnsNotRetained);
111 IO.enumCase(RCCK,
"NSReturnsRetained",
112 RetainCountConventionKind::NSReturnsRetained);
113 IO.enumCase(RCCK,
"NSReturnsNotRetained",
114 RetainCountConventionKind::NSReturnsNotRetained);
118template <>
struct MappingTraits<Param> {
120 IO.mapRequired(
"Position",
P.Position);
121 IO.mapOptional(
"Nullability",
P.Nullability, std::nullopt);
122 IO.mapOptional(
"RetainCountConvention",
P.RetainCountConvention);
123 IO.mapOptional(
"NoEscape",
P.NoEscape);
124 IO.mapOptional(
"Type",
P.Type, StringRef(
""));
131typedef std::vector<NullabilityKind> NullabilitySeq;
133struct AvailabilityItem {
134 APIAvailability Mode = APIAvailability::Available;
139enum class FactoryAsInitKind {
153 std::optional<NullabilityKind> NullabilityOfRet;
154 std::optional<RetainCountConventionKind> RetainCountConvention;
155 AvailabilityItem Availability;
156 std::optional<bool> SwiftPrivate;
158 FactoryAsInitKind FactoryAsInit = FactoryAsInitKind::Infer;
159 bool DesignatedInit =
false;
161 StringRef ResultType;
164typedef std::vector<Method> MethodsSeq;
167LLVM_YAML_IS_SEQUENCE_VECTOR(Method)
171template <>
struct ScalarEnumerationTraits<FactoryAsInitKind> {
173 IO.enumCase(FIK,
"A", FactoryAsInitKind::Infer);
174 IO.enumCase(FIK,
"C", FactoryAsInitKind::AsClassMethod);
175 IO.enumCase(FIK,
"I", FactoryAsInitKind::AsInitializer);
179template <>
struct MappingTraits<Method> {
181 IO.mapRequired(
"Selector", M.Selector);
182 IO.mapRequired(
"MethodKind", M.Kind);
183 IO.mapOptional(
"Parameters", M.Params);
184 IO.mapOptional(
"Nullability", M.Nullability);
185 IO.mapOptional(
"NullabilityOfRet", M.NullabilityOfRet, std::nullopt);
186 IO.mapOptional(
"RetainCountConvention", M.RetainCountConvention);
187 IO.mapOptional(
"Availability", M.Availability.Mode,
188 APIAvailability::Available);
189 IO.mapOptional(
"AvailabilityMsg", M.Availability.Msg, StringRef(
""));
190 IO.mapOptional(
"SwiftPrivate", M.SwiftPrivate);
191 IO.mapOptional(
"SwiftName", M.SwiftName, StringRef(
""));
192 IO.mapOptional(
"FactoryAsInit", M.FactoryAsInit, FactoryAsInitKind::Infer);
193 IO.mapOptional(
"DesignatedInit", M.DesignatedInit,
false);
194 IO.mapOptional(
"Required", M.Required,
false);
195 IO.mapOptional(
"ResultType", M.ResultType, StringRef(
""));
204 std::optional<MethodKind>
Kind;
206 AvailabilityItem Availability;
207 std::optional<bool> SwiftPrivate;
209 std::optional<bool> SwiftImportAsAccessors;
213typedef std::vector<Property> PropertiesSeq;
216LLVM_YAML_IS_SEQUENCE_VECTOR(Property)
222 IO.mapRequired(
"Name",
P.Name);
223 IO.mapOptional(
"PropertyKind",
P.Kind);
224 IO.mapOptional(
"Nullability",
P.Nullability, std::nullopt);
225 IO.mapOptional(
"Availability",
P.Availability.Mode,
226 APIAvailability::Available);
227 IO.mapOptional(
"AvailabilityMsg",
P.Availability.Msg, StringRef(
""));
228 IO.mapOptional(
"SwiftPrivate",
P.SwiftPrivate);
229 IO.mapOptional(
"SwiftName",
P.SwiftName, StringRef(
""));
230 IO.mapOptional(
"SwiftImportAsAccessors",
P.SwiftImportAsAccessors);
231 IO.mapOptional(
"Type",
P.Type, StringRef(
""));
240 bool AuditedForNullability =
false;
241 AvailabilityItem Availability;
242 std::optional<bool> SwiftPrivate;
244 std::optional<StringRef> SwiftBridge;
245 std::optional<StringRef> NSErrorDomain;
246 std::optional<bool> SwiftImportAsNonGeneric;
247 std::optional<bool> SwiftObjCMembers;
249 PropertiesSeq Properties;
252typedef std::vector<Class> ClassesSeq;
255LLVM_YAML_IS_SEQUENCE_VECTOR(Class)
259template <>
struct MappingTraits<
Class> {
261 IO.mapRequired(
"Name",
C.Name);
262 IO.mapOptional(
"AuditedForNullability",
C.AuditedForNullability,
false);
263 IO.mapOptional(
"Availability",
C.Availability.Mode,
264 APIAvailability::Available);
265 IO.mapOptional(
"AvailabilityMsg",
C.Availability.Msg, StringRef(
""));
266 IO.mapOptional(
"SwiftPrivate",
C.SwiftPrivate);
267 IO.mapOptional(
"SwiftName",
C.SwiftName, StringRef(
""));
268 IO.mapOptional(
"SwiftBridge",
C.SwiftBridge);
269 IO.mapOptional(
"NSErrorDomain",
C.NSErrorDomain);
270 IO.mapOptional(
"SwiftImportAsNonGeneric",
C.SwiftImportAsNonGeneric);
271 IO.mapOptional(
"SwiftObjCMembers",
C.SwiftObjCMembers);
272 IO.mapOptional(
"Methods",
C.Methods);
273 IO.mapOptional(
"Properties",
C.Properties);
284 std::optional<NullabilityKind> NullabilityOfRet;
285 std::optional<api_notes::RetainCountConventionKind> RetainCountConvention;
286 AvailabilityItem Availability;
287 std::optional<bool> SwiftPrivate;
290 StringRef ResultType;
293typedef std::vector<Function> FunctionsSeq;
296LLVM_YAML_IS_SEQUENCE_VECTOR(Function)
302 IO.mapRequired(
"Name", F.Name);
303 IO.mapOptional(
"Parameters", F.Params);
304 IO.mapOptional(
"Nullability", F.Nullability);
305 IO.mapOptional(
"NullabilityOfRet", F.NullabilityOfRet, std::nullopt);
306 IO.mapOptional(
"RetainCountConvention", F.RetainCountConvention);
307 IO.mapOptional(
"Availability", F.Availability.Mode,
308 APIAvailability::Available);
309 IO.mapOptional(
"AvailabilityMsg", F.Availability.Msg, StringRef(
""));
310 IO.mapOptional(
"SwiftPrivate", F.SwiftPrivate);
311 IO.mapOptional(
"SwiftName", F.SwiftName, StringRef(
""));
312 IO.mapOptional(
"ResultType", F.ResultType, StringRef(
""));
319struct GlobalVariable {
322 AvailabilityItem Availability;
323 std::optional<bool> SwiftPrivate;
328typedef std::vector<GlobalVariable> GlobalVariablesSeq;
331LLVM_YAML_IS_SEQUENCE_VECTOR(GlobalVariable)
335template <>
struct MappingTraits<GlobalVariable> {
336 static void mapping(IO &IO, GlobalVariable &GV) {
337 IO.mapRequired(
"Name", GV.Name);
338 IO.mapOptional(
"Nullability", GV.Nullability, std::nullopt);
339 IO.mapOptional(
"Availability", GV.Availability.Mode,
340 APIAvailability::Available);
341 IO.mapOptional(
"AvailabilityMsg", GV.Availability.Msg, StringRef(
""));
342 IO.mapOptional(
"SwiftPrivate", GV.SwiftPrivate);
343 IO.mapOptional(
"SwiftName", GV.SwiftName, StringRef(
""));
344 IO.mapOptional(
"Type", GV.Type, StringRef(
""));
353 AvailabilityItem Availability;
354 std::optional<bool> SwiftPrivate;
358typedef std::vector<EnumConstant> EnumConstantsSeq;
361LLVM_YAML_IS_SEQUENCE_VECTOR(EnumConstant)
365template <>
struct MappingTraits<EnumConstant> {
366 static void mapping(IO &IO, EnumConstant &EC) {
367 IO.mapRequired(
"Name", EC.Name);
368 IO.mapOptional(
"Availability", EC.Availability.Mode,
369 APIAvailability::Available);
370 IO.mapOptional(
"AvailabilityMsg", EC.Availability.Msg, StringRef(
""));
371 IO.mapOptional(
"SwiftPrivate", EC.SwiftPrivate);
372 IO.mapOptional(
"SwiftName", EC.SwiftName, StringRef(
""));
380enum class EnumConvenienceAliasKind {
394template <>
struct ScalarEnumerationTraits<EnumConvenienceAliasKind> {
396 IO.enumCase(ECAK,
"none", EnumConvenienceAliasKind::None);
397 IO.enumCase(ECAK,
"CFEnum", EnumConvenienceAliasKind::CFEnum);
398 IO.enumCase(ECAK,
"NSEnum", EnumConvenienceAliasKind::CFEnum);
399 IO.enumCase(ECAK,
"CFOptions", EnumConvenienceAliasKind::CFOptions);
400 IO.enumCase(ECAK,
"NSOptions", EnumConvenienceAliasKind::CFOptions);
401 IO.enumCase(ECAK,
"CFClosedEnum", EnumConvenienceAliasKind::CFClosedEnum);
402 IO.enumCase(ECAK,
"NSClosedEnum", EnumConvenienceAliasKind::CFClosedEnum);
412 AvailabilityItem Availability;
413 std::optional<bool> SwiftPrivate;
418typedef std::vector<Field> FieldsSeq;
421LLVM_YAML_IS_SEQUENCE_VECTOR(Field)
425template <>
struct MappingTraits<Field> {
427 IO.mapRequired(
"Name", F.Name);
428 IO.mapOptional(
"Nullability", F.Nullability, std::nullopt);
429 IO.mapOptional(
"Availability", F.Availability.Mode,
430 APIAvailability::Available);
431 IO.mapOptional(
"AvailabilityMsg", F.Availability.Msg, StringRef(
""));
432 IO.mapOptional(
"SwiftPrivate", F.SwiftPrivate);
433 IO.mapOptional(
"SwiftName", F.SwiftName, StringRef(
""));
434 IO.mapOptional(
"Type", F.Type, StringRef(
""));
442typedef std::vector<Tag> TagsSeq;
446 AvailabilityItem Availability;
448 std::optional<bool> SwiftPrivate;
449 std::optional<StringRef> SwiftBridge;
450 std::optional<StringRef> NSErrorDomain;
451 std::optional<std::string> SwiftImportAs;
452 std::optional<std::string> SwiftRetainOp;
453 std::optional<std::string> SwiftReleaseOp;
454 std::optional<std::string> SwiftConformance;
455 std::optional<EnumExtensibilityKind> EnumExtensibility;
456 std::optional<bool> FlagEnum;
457 std::optional<EnumConvenienceAliasKind> EnumConvenienceKind;
458 std::optional<bool> SwiftCopyable;
459 FunctionsSeq Methods;
468LLVM_YAML_IS_SEQUENCE_VECTOR(Tag)
474 IO.enumCase(EEK,
"none", EnumExtensibilityKind::None);
475 IO.enumCase(EEK,
"open", EnumExtensibilityKind::Open);
476 IO.enumCase(EEK,
"closed", EnumExtensibilityKind::Closed);
480template <>
struct MappingTraits<
Tag> {
482 IO.mapRequired(
"Name",
T.Name);
483 IO.mapOptional(
"Availability",
T.Availability.Mode,
484 APIAvailability::Available);
485 IO.mapOptional(
"AvailabilityMsg",
T.Availability.Msg, StringRef(
""));
486 IO.mapOptional(
"SwiftPrivate",
T.SwiftPrivate);
487 IO.mapOptional(
"SwiftName",
T.SwiftName, StringRef(
""));
488 IO.mapOptional(
"SwiftBridge",
T.SwiftBridge);
489 IO.mapOptional(
"NSErrorDomain",
T.NSErrorDomain);
490 IO.mapOptional(
"SwiftImportAs",
T.SwiftImportAs);
491 IO.mapOptional(
"SwiftReleaseOp",
T.SwiftReleaseOp);
492 IO.mapOptional(
"SwiftRetainOp",
T.SwiftRetainOp);
493 IO.mapOptional(
"SwiftConformsTo",
T.SwiftConformance);
494 IO.mapOptional(
"EnumExtensibility",
T.EnumExtensibility);
495 IO.mapOptional(
"FlagEnum",
T.FlagEnum);
496 IO.mapOptional(
"EnumKind",
T.EnumConvenienceKind);
497 IO.mapOptional(
"SwiftCopyable",
T.SwiftCopyable);
498 IO.mapOptional(
"Methods",
T.Methods);
499 IO.mapOptional(
"Fields",
T.Fields);
500 IO.mapOptional(
"Tags",
T.Tags);
509 AvailabilityItem Availability;
511 std::optional<bool> SwiftPrivate;
512 std::optional<StringRef> SwiftBridge;
513 std::optional<StringRef> NSErrorDomain;
514 std::optional<SwiftNewTypeKind> SwiftType;
517typedef std::vector<Typedef> TypedefsSeq;
520LLVM_YAML_IS_SEQUENCE_VECTOR(Typedef)
526 IO.enumCase(SWK,
"none", SwiftNewTypeKind::None);
527 IO.enumCase(SWK,
"struct", SwiftNewTypeKind::Struct);
528 IO.enumCase(SWK,
"enum", SwiftNewTypeKind::Enum);
532template <>
struct MappingTraits<Typedef> {
534 IO.mapRequired(
"Name",
T.Name);
535 IO.mapOptional(
"Availability",
T.Availability.Mode,
536 APIAvailability::Available);
537 IO.mapOptional(
"AvailabilityMsg",
T.Availability.Msg, StringRef(
""));
538 IO.mapOptional(
"SwiftPrivate",
T.SwiftPrivate);
539 IO.mapOptional(
"SwiftName",
T.SwiftName, StringRef(
""));
540 IO.mapOptional(
"SwiftBridge",
T.SwiftBridge);
541 IO.mapOptional(
"NSErrorDomain",
T.NSErrorDomain);
542 IO.mapOptional(
"SwiftWrapper",
T.SwiftType);
550typedef std::vector<Namespace> NamespacesSeq;
552struct TopLevelItems {
554 ClassesSeq Protocols;
555 FunctionsSeq Functions;
556 GlobalVariablesSeq Globals;
557 EnumConstantsSeq EnumConstants;
559 TypedefsSeq Typedefs;
560 NamespacesSeq Namespaces;
567 IO.mapOptional(
"Classes", TLI.Classes);
568 IO.mapOptional(
"Protocols", TLI.Protocols);
569 IO.mapOptional(
"Functions", TLI.Functions);
570 IO.mapOptional(
"Globals", TLI.Globals);
571 IO.mapOptional(
"Enumerators", TLI.EnumConstants);
572 IO.mapOptional(
"Tags", TLI.Tags);
573 IO.mapOptional(
"Typedefs", TLI.Typedefs);
574 IO.mapOptional(
"Namespaces", TLI.Namespaces);
582 AvailabilityItem Availability;
584 std::optional<bool> SwiftPrivate;
589LLVM_YAML_IS_SEQUENCE_VECTOR(Namespace)
595 IO.mapRequired(
"Name",
T.Name);
596 IO.mapOptional(
"Availability",
T.Availability.Mode,
597 APIAvailability::Available);
598 IO.mapOptional(
"AvailabilityMsg",
T.Availability.Msg, StringRef(
""));
599 IO.mapOptional(
"SwiftPrivate",
T.SwiftPrivate);
600 IO.mapOptional(
"SwiftName",
T.SwiftName, StringRef(
""));
609 VersionTuple Version;
613typedef std::vector<Versioned> VersionedSeq;
616LLVM_YAML_IS_SEQUENCE_VECTOR(Versioned)
620template <>
struct MappingTraits<Versioned> {
622 IO.mapRequired(
"Version",
V.Version);
632 AvailabilityItem Availability;
633 TopLevelItems TopLevel;
634 VersionedSeq SwiftVersions;
636 std::optional<bool> SwiftInferImportAsMember;
638#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
639 LLVM_DUMP_METHOD
void dump() ;
646template <>
struct MappingTraits<
Module> {
648 IO.mapRequired(
"Name", M.
Name);
649 IO.mapOptional(
"Availability", M.Availability.Mode,
650 APIAvailability::Available);
651 IO.mapOptional(
"AvailabilityMsg", M.Availability.Msg, StringRef(
""));
652 IO.mapOptional(
"SwiftInferImportAsMember", M.SwiftInferImportAsMember);
654 IO.mapOptional(
"SwiftVersions", M.SwiftVersions);
660#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
661LLVM_DUMP_METHOD
void Module::dump() {
662 llvm::yaml::Output OS(llvm::errs());
668bool parseAPINotes(StringRef YI,
Module &M, llvm::SourceMgr::DiagHandlerTy
Diag,
670 llvm::yaml::Input IS(YI,
nullptr,
Diag, DiagContext);
672 return static_cast<bool>(IS.error());
677 llvm::raw_ostream &OS) {
679 if (parseAPINotes(YI, M,
nullptr,
nullptr))
682 llvm::yaml::Output YOS(OS);
689using namespace api_notes;
694 llvm::raw_ostream &OS;
695 llvm::SourceMgr::DiagHandlerTy DiagHandler;
696 void *DiagHandlerCtxt;
700 bool emitError(llvm::Twine Message) {
702 llvm::SMDiagnostic(
"", llvm::SourceMgr::DK_Error, Message.str()),
710 llvm::raw_ostream &OS,
711 llvm::SourceMgr::DiagHandlerTy DiagHandler,
712 void *DiagHandlerCtxt)
713 : M(TheModule), Writer(TheModule.Name, SourceFile), OS(OS),
714 DiagHandler(DiagHandler), DiagHandlerCtxt(DiagHandlerCtxt),
715 ErrorOccured(
false) {}
717 void convertAvailability(
const AvailabilityItem &Availability,
720 CEI.
Unavailable = (Availability.Mode == APIAvailability::None);
725 if (!Availability.Msg.empty())
726 emitError(llvm::Twine(
"availability message for available API '") +
727 APIName +
"' will not be used");
731 void convertParams(
const ParamsSeq &Params,
FunctionInfo &OutInfo) {
732 for (
const auto &
P : Params) {
739 if (OutInfo.
Params.size() <=
P.Position)
740 OutInfo.
Params.resize(
P.Position + 1);
741 OutInfo.
Params[
P.Position] |= PI;
745 void convertNullability(
const NullabilitySeq &Nullability,
746 std::optional<NullabilityKind> ReturnNullability,
749 emitError(llvm::Twine(
"nullability info for '") + APIName +
754 bool audited =
false;
755 unsigned int idx = 1;
756 for (
const auto &N : Nullability)
758 audited =
Nullability.size() > 0 || ReturnNullability;
760 OutInfo.
addTypeInfo(0, ReturnNullability ? *ReturnNullability
761 : NullabilityKind::NonNull);
769 template <
typename T>
772 convertAvailability(Common.Availability, Info, APIName);
774 Info.
SwiftName = std::string(Common.SwiftName);
778 template <
typename T>
781 convertCommonEntity(Common, Info, APIName);
782 if (Common.SwiftBridge)
788 void convertMethod(
const Method &M,
ContextID ClassID, StringRef ClassName,
789 VersionTuple SwiftVersion) {
791 convertCommonEntity(M, MI, M.Selector);
794 bool takesArguments = M.Selector.ends_with(
":");
798 M.Selector.split(Args,
":", -1,
false);
799 if (!takesArguments && Args.size() > 1) {
800 emitError(
"selector '" + M.Selector +
"' is missing a ':' at the end");
806 Selector.NumArgs = !takesArguments ? 0 : Args.size();
812 if (M.FactoryAsInit != FactoryAsInitKind::Infer)
813 emitError(
"'FactoryAsInit' is no longer valid; use 'SwiftName' instead");
818 convertParams(M.Params, MI);
821 convertNullability(M.Nullability, M.NullabilityOfRet, MI, M.Selector);
830 template <
typename T>
832 convertAvailability(Entity.Availability, VI, Entity.Name);
834 VI.
SwiftName = std::string(Entity.SwiftName);
835 if (Entity.Nullability)
837 VI.
setType(std::string(Entity.Type));
840 void convertContext(std::optional<ContextID> ParentContextID,
const Class &
C,
844 convertCommonType(
C, CI,
C.Name);
846 if (
C.AuditedForNullability)
848 if (
C.SwiftImportAsNonGeneric)
850 if (
C.SwiftObjCMembers)
857 llvm::StringMap<std::pair<bool, bool>> KnownMethods;
858 for (
const auto &method :
C.Methods) {
862 : KnownMethods[method.Selector].second;
864 emitError(llvm::Twine(
"duplicate definition of method '") +
865 (IsInstanceMethod ?
"-" :
"+") +
"[" +
C.Name +
" " +
866 method.Selector +
"]'");
871 convertMethod(method, CtxID,
C.Name, SwiftVersion);
875 llvm::StringSet<> KnownInstanceProperties;
876 llvm::StringSet<> KnownClassProperties;
877 for (
const auto &Property :
C.Properties) {
880 !KnownInstanceProperties.insert(
Property.Name).second) {
881 emitError(llvm::Twine(
"duplicate definition of instance property '") +
887 !KnownClassProperties.insert(
Property.Name).second) {
888 emitError(llvm::Twine(
"duplicate definition of class property '") +
895 convertVariable(Property, PI);
896 if (
Property.SwiftImportAsAccessors)
902 *
Property.Kind == MethodKind::Instance, PI,
911 void convertNamespaceContext(std::optional<ContextID> ParentContextID,
912 const Namespace &TheNamespace,
913 VersionTuple SwiftVersion) {
916 convertCommonEntity(TheNamespace, CI, TheNamespace.Name);
919 Writer.
addContext(ParentContextID, TheNamespace.Name,
920 ContextKind::Namespace, CI, SwiftVersion);
922 convertTopLevelItems(
Context(CtxID, ContextKind::Namespace),
923 TheNamespace.Items, SwiftVersion);
926 void convertFunction(
const Function &Function,
FunctionInfo &FI) {
937 void convertTagContext(std::optional<Context> ParentContext,
const Tag &
T,
938 VersionTuple SwiftVersion) {
940 std::optional<ContextID> ParentContextID =
941 ParentContext ? std::optional<ContextID>(ParentContext->id)
943 convertCommonType(
T, TI,
T.Name);
945 if ((
T.SwiftRetainOp ||
T.SwiftReleaseOp) && !
T.SwiftImportAs) {
946 emitError(llvm::Twine(
"should declare SwiftImportAs to use "
947 "SwiftRetainOp and SwiftReleaseOp (for ") +
951 if (
T.SwiftReleaseOp.has_value() !=
T.SwiftRetainOp.has_value()) {
952 emitError(llvm::Twine(
"should declare both SwiftReleaseOp and "
953 "SwiftRetainOp (for ") +
962 if (
T.SwiftReleaseOp)
964 if (
T.SwiftConformance)
970 if (
T.EnumConvenienceKind) {
971 if (
T.EnumExtensibility) {
973 llvm::Twine(
"cannot mix EnumKind and EnumExtensibility (for ") +
978 emitError(llvm::Twine(
"cannot mix EnumKind and FlagEnum (for ") +
982 switch (*
T.EnumConvenienceKind) {
983 case EnumConvenienceAliasKind::None:
987 case EnumConvenienceAliasKind::CFEnum:
991 case EnumConvenienceAliasKind::CFOptions:
995 case EnumConvenienceAliasKind::CFClosedEnum:
1005 Writer.
addTag(ParentContext,
T.Name, TI, SwiftVersion);
1008 auto TagCtxID = Writer.
addContext(ParentContextID,
T.Name, ContextKind::Tag,
1010 Context TagCtx(TagCtxID, ContextKind::Tag);
1012 for (
const auto &Field :
T.Fields) {
1014 convertVariable(Field, FI);
1018 for (
const auto &CXXMethod :
T.Methods) {
1020 convertFunction(CXXMethod, MI);
1021 Writer.
addCXXMethod(TagCtxID, CXXMethod.Name, MI, SwiftVersion);
1025 for (
const auto &Tag :
T.Tags)
1026 convertTagContext(TagCtx, Tag, SwiftVersion);
1029 void convertTopLevelItems(std::optional<Context> Ctx,
1030 const TopLevelItems &TLItems,
1031 VersionTuple SwiftVersion) {
1032 std::optional<ContextID> CtxID =
1033 Ctx ? std::optional(Ctx->id) :
std::nullopt;
1036 llvm::StringSet<> KnownClasses;
1037 for (
const auto &Class : TLItems.Classes) {
1039 if (!KnownClasses.insert(
Class.Name).second) {
1040 emitError(llvm::Twine(
"multiple definitions of class '") +
Class.Name +
1045 convertContext(CtxID, Class, ContextKind::ObjCClass, SwiftVersion);
1049 llvm::StringSet<> KnownProtocols;
1050 for (
const auto &Protocol : TLItems.Protocols) {
1052 if (!KnownProtocols.insert(
Protocol.Name).second) {
1053 emitError(llvm::Twine(
"multiple definitions of protocol '") +
1058 convertContext(CtxID, Protocol, ContextKind::ObjCProtocol, SwiftVersion);
1062 llvm::StringSet<> KnownNamespaces;
1063 for (
const auto &Namespace : TLItems.Namespaces) {
1065 if (!KnownNamespaces.insert(
Namespace.Name).second) {
1066 emitError(llvm::Twine(
"multiple definitions of namespace '") +
1071 convertNamespaceContext(CtxID, Namespace, SwiftVersion);
1075 llvm::StringSet<> KnownGlobals;
1076 for (
const auto &
Global : TLItems.Globals) {
1078 if (!KnownGlobals.insert(
Global.Name).second) {
1079 emitError(llvm::Twine(
"multiple definitions of global variable '") +
1085 convertVariable(
Global, GVI);
1090 llvm::StringSet<> KnownFunctions;
1091 for (
const auto &Function : TLItems.Functions) {
1093 if (!KnownFunctions.insert(
Function.Name).second) {
1094 emitError(llvm::Twine(
"multiple definitions of global function '") +
1100 convertFunction(Function, GFI);
1105 llvm::StringSet<> KnownEnumConstants;
1106 for (
const auto &EnumConstant : TLItems.EnumConstants) {
1108 if (!KnownEnumConstants.insert(
EnumConstant.Name).second) {
1109 emitError(llvm::Twine(
"multiple definitions of enumerator '") +
1122 llvm::StringSet<> KnownTags;
1123 for (
const auto &Tag : TLItems.Tags) {
1125 if (!KnownTags.insert(
Tag.Name).second) {
1126 emitError(llvm::Twine(
"multiple definitions of tag '") +
Tag.Name +
1131 convertTagContext(Ctx, Tag, SwiftVersion);
1135 llvm::StringSet<> KnownTypedefs;
1136 for (
const auto &Typedef : TLItems.Typedefs) {
1138 if (!KnownTypedefs.insert(Typedef.Name).second) {
1139 emitError(llvm::Twine(
"multiple definitions of typedef '") +
1140 Typedef.Name +
"'");
1145 convertCommonType(Typedef, TInfo, Typedef.Name);
1148 Writer.
addTypedef(Ctx, Typedef.Name, TInfo, SwiftVersion);
1152 bool convertModule() {
1154 convertTopLevelItems( std::nullopt, M.TopLevel,
1158 for (
const auto &Versioned : M.SwiftVersions)
1159 convertTopLevelItems( std::nullopt, Versioned.Items,
1165 return ErrorOccured;
1171 llvm::raw_ostream &OS,
1172 llvm::SourceMgr::DiagHandlerTy DiagHandler,
1173 void *DiagHandlerCtxt) {
1174 YAMLConverter
C(M, SourceFile, OS, DiagHandler, DiagHandlerCtxt);
1175 return C.convertModule();
1180 Diag.print(
nullptr, llvm::errs());
1185 llvm::raw_ostream &OS,
1186 llvm::SourceMgr::DiagHandlerTy DiagHandler,
1187 void *DiagHandlerCtxt) {
1193 if (parseAPINotes(YAMLInput, TheModule, DiagHandler, DiagHandlerCtxt))
1196 return compile(TheModule, SourceFile, OS, DiagHandler, DiagHandlerCtxt);
static void printDiagnostic(const llvm::SMDiagnostic &Diag, void *Context)
Simple diagnostic handler that prints diagnostics to standard error.
static bool compile(const Module &M, const FileEntry *SourceFile, llvm::raw_ostream &OS, llvm::SourceMgr::DiagHandlerTy DiagHandler, void *DiagHandlerCtxt)
enum clang::sema::@1655::IndirectLocalPathEntry::EntryKind Kind
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
static DiagnosticBuilder Diag(DiagnosticsEngine *Diags, const LangOptions &Features, FullSourceLoc TokLoc, const char *TokBegin, const char *TokRangeBegin, const char *TokRangeEnd, unsigned DiagID)
Produce a diagnostic highlighting some portion of a literal.
Defines various enumerations that describe declaration and type specifiers.
Cached information about one file (either on disk or in the virtual file system).
Describes a module or submodule.
std::string Name
The name of this module.
void dump() const
Dump the contents of this module to the given output stream.
Smart pointer class that efficiently represents Objective-C method names.
The base class of the type hierarchy.
A class that writes API notes data to a binary representation that can be read by the APINotesReader.
void addObjCMethod(ContextID CtxID, ObjCSelectorRef Selector, bool IsInstanceMethod, const ObjCMethodInfo &Info, llvm::VersionTuple SwiftVersion)
Add information about a specific Objective-C method.
void addEnumConstant(llvm::StringRef Name, const EnumConstantInfo &Info, llvm::VersionTuple SwiftVersion)
Add information about an enumerator.
ContextID addContext(std::optional< ContextID > ParentCtxID, llvm::StringRef Name, ContextKind Kind, const ContextInfo &Info, llvm::VersionTuple SwiftVersion)
Add information about a specific Objective-C class or protocol or a C++ namespace.
void addGlobalFunction(std::optional< Context > Ctx, llvm::StringRef Name, const GlobalFunctionInfo &Info, llvm::VersionTuple SwiftVersion)
Add information about a global function.
void addObjCProperty(ContextID CtxID, llvm::StringRef Name, bool IsInstanceProperty, const ObjCPropertyInfo &Info, llvm::VersionTuple SwiftVersion)
Add information about a specific Objective-C property.
void addField(ContextID CtxID, llvm::StringRef Name, const FieldInfo &Info, llvm::VersionTuple SwiftVersion)
Add information about a specific C record field.
void addGlobalVariable(std::optional< Context > Ctx, llvm::StringRef Name, const GlobalVariableInfo &Info, llvm::VersionTuple SwiftVersion)
Add information about a global variable.
void addTypedef(std::optional< Context > Ctx, llvm::StringRef Name, const TypedefInfo &Info, llvm::VersionTuple SwiftVersion)
Add information about a typedef.
void writeToStream(llvm::raw_ostream &OS)
void addCXXMethod(ContextID CtxID, llvm::StringRef Name, const CXXMethodInfo &Info, llvm::VersionTuple SwiftVersion)
Add information about a specific C++ method.
void addTag(std::optional< Context > Ctx, llvm::StringRef Name, const TagInfo &Info, llvm::VersionTuple SwiftVersion)
Add information about a tag (struct/union/enum/C++ class).
Describes API notes data for a C++ method.
Describes API notes data for any entity.
unsigned UnavailableInSwift
Whether this entity is marked unavailable in Swift.
unsigned Unavailable
Whether this entity is marked unavailable.
std::string SwiftName
Swift name of this entity.
void setSwiftPrivate(std::optional< bool > Private)
std::string UnavailableMsg
Message to use when this entity is unavailable.
Describes API notes for types.
void setNSErrorDomain(const std::optional< std::string > &Domain)
void setSwiftBridge(std::optional< std::string > SwiftType)
Opaque context ID used to refer to an Objective-C class or protocol or a C++ namespace.
Describes API notes data for an Objective-C class or protocol or a C++ namespace.
void setDefaultNullability(NullabilityKind Kind)
Set the default nullability for properties and methods of this class.
void setSwiftObjCMembers(std::optional< bool > Value)
void setSwiftImportAsNonGeneric(std::optional< bool > Value)
Describes API notes data for an enumerator.
Describes API notes data for a C/C++ record field.
API notes for a function or method.
void addTypeInfo(unsigned index, NullabilityKind kind)
void setRetainCountConvention(std::optional< RetainCountConventionKind > Value)
std::vector< ParamInfo > Params
The function parameters.
unsigned NumAdjustedNullable
Number of types whose nullability is encoded with the NullabilityPayload.
std::string ResultType
The result type of this function, as a C type.
static unsigned getMaxNullabilityIndex()
unsigned NullabilityAudited
Whether the signature has been audited with respect to nullability.
Describes API notes data for a global function.
Describes API notes data for a global variable.
Describes API notes data for an Objective-C method.
unsigned DesignatedInit
Whether this is a designated initializer of its class.
unsigned RequiredInit
Whether this is a required initializer.
Describes API notes data for an Objective-C property.
void setSwiftImportAsAccessors(std::optional< bool > Value)
Describes a function or method parameter.
void setNoEscape(std::optional< bool > Value)
void setRetainCountConvention(std::optional< RetainCountConventionKind > Value)
Describes API notes data for a tag.
std::optional< std::string > SwiftReleaseOp
std::optional< std::string > SwiftRetainOp
std::optional< std::string > SwiftImportAs
std::optional< EnumExtensibilityKind > EnumExtensibility
void setSwiftCopyable(std::optional< bool > Value)
void setFlagEnum(std::optional< bool > Value)
std::optional< std::string > SwiftConformance
The Swift protocol that this type should be automatically conformed to.
Describes API notes data for a typedef.
std::optional< SwiftNewTypeKind > SwiftWrapper
API notes for a variable/property.
void setNullabilityAudited(NullabilityKind kind)
void setType(const std::string &type)
RetainCountConventionKind
bool compileAPINotes(llvm::StringRef YAMLInput, const FileEntry *SourceFile, llvm::raw_ostream &OS, llvm::SourceMgr::DiagHandlerTy DiagHandler=nullptr, void *DiagHandlerCtxt=nullptr)
Converts API notes from YAML format to binary format.
SwiftNewTypeKind
The kind of a swift_wrapper/swift_newtype.
EnumExtensibilityKind
The payload for an enum_extensibility attribute.
bool parseAndDumpAPINotes(llvm::StringRef YI, llvm::raw_ostream &OS)
Parses the APINotes YAML content and writes the representation back to the specified stream.
The JSON file list parser is used to communicate input to InstallAPI.
NullabilityKind
Describes the nullability of a particular type.
@ Property
The type of a property.
const FunctionProtoType * T
@ Class
The "class" keyword introduces the elaborated-type-specifier.
static void mapTopLevelItems(IO &IO, TopLevelItems &TLI)
Diagnostic wrappers for TextAPI types for error reporting.
A temporary reference to an Objective-C selector, suitable for referencing selector data on the stack...
static void mapping(IO &IO, Class &C)
static void mapping(IO &IO, EnumConstant &EC)
static void mapping(IO &IO, Field &F)
static void mapping(IO &IO, Function &F)
static void mapping(IO &IO, GlobalVariable &GV)
static void mapping(IO &IO, Method &M)
static void mapping(IO &IO, Module &M)
static void mapping(IO &IO, Namespace &T)
static void mapping(IO &IO, Param &P)
static void mapping(IO &IO, Property &P)
static void mapping(IO &IO, Tag &T)
static void mapping(IO &IO, Typedef &T)
static void mapping(IO &IO, Versioned &V)
static void enumeration(IO &IO, APIAvailability &AA)
static void enumeration(IO &IO, EnumConvenienceAliasKind &ECAK)
static void enumeration(IO &IO, EnumExtensibilityKind &EEK)
static void enumeration(IO &IO, FactoryAsInitKind &FIK)
static void enumeration(IO &IO, MethodKind &MK)
static void enumeration(IO &IO, NullabilityKind &NK)
static void enumeration(IO &IO, RetainCountConventionKind &RCCK)
static void enumeration(IO &IO, SwiftNewTypeKind &SWK)