| 1 | //===-- APINotesYAMLCompiler.cpp - API Notes YAML Format Reader -*- C++ -*-===// |
| 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 | // The types defined locally are designed to represent the YAML state, which |
| 10 | // adds an additional bit of state: e.g. a tri-state boolean attribute (yes, no, |
| 11 | // not applied) becomes a tri-state boolean + present. As a result, while these |
| 12 | // enumerations appear to be redefining constants from the attributes table |
| 13 | // data, they are distinct. |
| 14 | // |
| 15 | |
| 16 | #include "clang/APINotes/APINotesYAMLCompiler.h" |
| 17 | #include "clang/APINotes/APINotesWriter.h" |
| 18 | #include "clang/APINotes/Types.h" |
| 19 | #include "clang/Basic/LLVM.h" |
| 20 | #include "clang/Basic/Specifiers.h" |
| 21 | #include "llvm/ADT/StringSet.h" |
| 22 | #include "llvm/Support/SourceMgr.h" |
| 23 | #include "llvm/Support/VersionTuple.h" |
| 24 | #include "llvm/Support/YAMLTraits.h" |
| 25 | #include <optional> |
| 26 | #include <type_traits> |
| 27 | #include <vector> |
| 28 | |
| 29 | using namespace clang; |
| 30 | using namespace api_notes; |
| 31 | |
| 32 | namespace llvm { |
| 33 | namespace yaml { |
| 34 | template <> struct ScalarEnumerationTraits<SwiftSafetyKind> { |
| 35 | static void enumeration(IO &IO, SwiftSafetyKind &SK) { |
| 36 | IO.enumCase(Val&: SK, Str: "unspecified" , ConstVal: SwiftSafetyKind::Unspecified); |
| 37 | IO.enumCase(Val&: SK, Str: "safe" , ConstVal: SwiftSafetyKind::Safe); |
| 38 | IO.enumCase(Val&: SK, Str: "unsafe" , ConstVal: SwiftSafetyKind::Unsafe); |
| 39 | } |
| 40 | }; |
| 41 | } // namespace yaml |
| 42 | } // namespace llvm |
| 43 | |
| 44 | namespace { |
| 45 | enum class APIAvailability { |
| 46 | Available = 0, |
| 47 | None, |
| 48 | NonSwift, |
| 49 | }; |
| 50 | } // namespace |
| 51 | |
| 52 | namespace llvm { |
| 53 | namespace yaml { |
| 54 | template <> struct ScalarEnumerationTraits<APIAvailability> { |
| 55 | static void enumeration(IO &IO, APIAvailability &AA) { |
| 56 | IO.enumCase(Val&: AA, Str: "none" , ConstVal: APIAvailability::None); |
| 57 | IO.enumCase(Val&: AA, Str: "nonswift" , ConstVal: APIAvailability::NonSwift); |
| 58 | IO.enumCase(Val&: AA, Str: "available" , ConstVal: APIAvailability::Available); |
| 59 | } |
| 60 | }; |
| 61 | } // namespace yaml |
| 62 | } // namespace llvm |
| 63 | |
| 64 | namespace { |
| 65 | enum class MethodKind { |
| 66 | Class, |
| 67 | Instance, |
| 68 | }; |
| 69 | } // namespace |
| 70 | |
| 71 | namespace llvm { |
| 72 | namespace yaml { |
| 73 | template <> struct ScalarEnumerationTraits<MethodKind> { |
| 74 | static void enumeration(IO &IO, MethodKind &MK) { |
| 75 | IO.enumCase(Val&: MK, Str: "Class" , ConstVal: MethodKind::Class); |
| 76 | IO.enumCase(Val&: MK, Str: "Instance" , ConstVal: MethodKind::Instance); |
| 77 | } |
| 78 | }; |
| 79 | } // namespace yaml |
| 80 | } // namespace llvm |
| 81 | |
| 82 | namespace { |
| 83 | struct Param { |
| 84 | int Position; |
| 85 | std::optional<bool> NoEscape = false; |
| 86 | std::optional<bool> Lifetimebound = false; |
| 87 | std::optional<NullabilityKind> Nullability; |
| 88 | std::optional<RetainCountConventionKind> RetainCountConvention; |
| 89 | StringRef Type; |
| 90 | }; |
| 91 | |
| 92 | typedef std::vector<Param> ParamsSeq; |
| 93 | } // namespace |
| 94 | |
| 95 | LLVM_YAML_IS_SEQUENCE_VECTOR(Param) |
| 96 | LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(NullabilityKind) |
| 97 | |
| 98 | namespace llvm { |
| 99 | namespace yaml { |
| 100 | template <> struct ScalarEnumerationTraits<NullabilityKind> { |
| 101 | static void enumeration(IO &IO, NullabilityKind &NK) { |
| 102 | IO.enumCase(Val&: NK, Str: "Nonnull" , ConstVal: NullabilityKind::NonNull); |
| 103 | IO.enumCase(Val&: NK, Str: "Optional" , ConstVal: NullabilityKind::Nullable); |
| 104 | IO.enumCase(Val&: NK, Str: "Unspecified" , ConstVal: NullabilityKind::Unspecified); |
| 105 | IO.enumCase(Val&: NK, Str: "NullableResult" , ConstVal: NullabilityKind::NullableResult); |
| 106 | // TODO: Mapping this to it's own value would allow for better cross |
| 107 | // checking. Also the default should be Unknown. |
| 108 | IO.enumCase(Val&: NK, Str: "Scalar" , ConstVal: NullabilityKind::Unspecified); |
| 109 | |
| 110 | // Aliases for compatibility with existing APINotes. |
| 111 | IO.enumCase(Val&: NK, Str: "N" , ConstVal: NullabilityKind::NonNull); |
| 112 | IO.enumCase(Val&: NK, Str: "O" , ConstVal: NullabilityKind::Nullable); |
| 113 | IO.enumCase(Val&: NK, Str: "U" , ConstVal: NullabilityKind::Unspecified); |
| 114 | IO.enumCase(Val&: NK, Str: "S" , ConstVal: NullabilityKind::Unspecified); |
| 115 | } |
| 116 | }; |
| 117 | |
| 118 | template <> struct ScalarEnumerationTraits<RetainCountConventionKind> { |
| 119 | static void enumeration(IO &IO, RetainCountConventionKind &RCCK) { |
| 120 | IO.enumCase(Val&: RCCK, Str: "none" , ConstVal: RetainCountConventionKind::None); |
| 121 | IO.enumCase(Val&: RCCK, Str: "CFReturnsRetained" , |
| 122 | ConstVal: RetainCountConventionKind::CFReturnsRetained); |
| 123 | IO.enumCase(Val&: RCCK, Str: "CFReturnsNotRetained" , |
| 124 | ConstVal: RetainCountConventionKind::CFReturnsNotRetained); |
| 125 | IO.enumCase(Val&: RCCK, Str: "NSReturnsRetained" , |
| 126 | ConstVal: RetainCountConventionKind::NSReturnsRetained); |
| 127 | IO.enumCase(Val&: RCCK, Str: "NSReturnsNotRetained" , |
| 128 | ConstVal: RetainCountConventionKind::NSReturnsNotRetained); |
| 129 | } |
| 130 | }; |
| 131 | |
| 132 | template <> struct MappingTraits<Param> { |
| 133 | static void mapping(IO &IO, Param &P) { |
| 134 | IO.mapRequired(Key: "Position" , Val&: P.Position); |
| 135 | IO.mapOptional(Key: "Nullability" , Val&: P.Nullability, Default: std::nullopt); |
| 136 | IO.mapOptional(Key: "RetainCountConvention" , Val&: P.RetainCountConvention); |
| 137 | IO.mapOptional(Key: "NoEscape" , Val&: P.NoEscape); |
| 138 | IO.mapOptional(Key: "Lifetimebound" , Val&: P.Lifetimebound); |
| 139 | IO.mapOptional(Key: "Type" , Val&: P.Type, Default: StringRef("" )); |
| 140 | } |
| 141 | }; |
| 142 | } // namespace yaml |
| 143 | } // namespace llvm |
| 144 | |
| 145 | namespace { |
| 146 | typedef std::vector<NullabilityKind> NullabilitySeq; |
| 147 | |
| 148 | struct AvailabilityItem { |
| 149 | APIAvailability Mode = APIAvailability::Available; |
| 150 | StringRef Msg; |
| 151 | }; |
| 152 | |
| 153 | /// Old attribute deprecated in favor of SwiftName. |
| 154 | enum class FactoryAsInitKind { |
| 155 | /// Infer based on name and type (the default). |
| 156 | Infer, |
| 157 | /// Treat as a class method. |
| 158 | AsClassMethod, |
| 159 | /// Treat as an initializer. |
| 160 | AsInitializer, |
| 161 | }; |
| 162 | |
| 163 | struct Method { |
| 164 | StringRef Selector; |
| 165 | MethodKind Kind; |
| 166 | ParamsSeq Params; |
| 167 | NullabilitySeq Nullability; |
| 168 | std::optional<NullabilityKind> NullabilityOfRet; |
| 169 | std::optional<RetainCountConventionKind> RetainCountConvention; |
| 170 | AvailabilityItem Availability; |
| 171 | std::optional<bool> SwiftPrivate; |
| 172 | StringRef SwiftName; |
| 173 | FactoryAsInitKind FactoryAsInit = FactoryAsInitKind::Infer; |
| 174 | bool DesignatedInit = false; |
| 175 | bool Required = false; |
| 176 | StringRef ResultType; |
| 177 | StringRef SwiftReturnOwnership; |
| 178 | SwiftSafetyKind SafetyKind = SwiftSafetyKind::None; |
| 179 | }; |
| 180 | |
| 181 | typedef std::vector<Method> MethodsSeq; |
| 182 | } // namespace |
| 183 | |
| 184 | LLVM_YAML_IS_SEQUENCE_VECTOR(Method) |
| 185 | |
| 186 | namespace llvm { |
| 187 | namespace yaml { |
| 188 | template <> struct ScalarEnumerationTraits<FactoryAsInitKind> { |
| 189 | static void enumeration(IO &IO, FactoryAsInitKind &FIK) { |
| 190 | IO.enumCase(Val&: FIK, Str: "A" , ConstVal: FactoryAsInitKind::Infer); |
| 191 | IO.enumCase(Val&: FIK, Str: "C" , ConstVal: FactoryAsInitKind::AsClassMethod); |
| 192 | IO.enumCase(Val&: FIK, Str: "I" , ConstVal: FactoryAsInitKind::AsInitializer); |
| 193 | } |
| 194 | }; |
| 195 | |
| 196 | template <> struct MappingTraits<Method> { |
| 197 | static void mapping(IO &IO, Method &M) { |
| 198 | IO.mapRequired(Key: "Selector" , Val&: M.Selector); |
| 199 | IO.mapRequired(Key: "MethodKind" , Val&: M.Kind); |
| 200 | IO.mapOptional(Key: "Parameters" , Val&: M.Params); |
| 201 | IO.mapOptional(Key: "Nullability" , Val&: M.Nullability); |
| 202 | IO.mapOptional(Key: "NullabilityOfRet" , Val&: M.NullabilityOfRet, Default: std::nullopt); |
| 203 | IO.mapOptional(Key: "RetainCountConvention" , Val&: M.RetainCountConvention); |
| 204 | IO.mapOptional(Key: "Availability" , Val&: M.Availability.Mode, |
| 205 | Default: APIAvailability::Available); |
| 206 | IO.mapOptional(Key: "AvailabilityMsg" , Val&: M.Availability.Msg, Default: StringRef("" )); |
| 207 | IO.mapOptional(Key: "SwiftPrivate" , Val&: M.SwiftPrivate); |
| 208 | IO.mapOptional(Key: "SwiftName" , Val&: M.SwiftName, Default: StringRef("" )); |
| 209 | IO.mapOptional(Key: "FactoryAsInit" , Val&: M.FactoryAsInit, Default: FactoryAsInitKind::Infer); |
| 210 | IO.mapOptional(Key: "DesignatedInit" , Val&: M.DesignatedInit, Default: false); |
| 211 | IO.mapOptional(Key: "Required" , Val&: M.Required, Default: false); |
| 212 | IO.mapOptional(Key: "ResultType" , Val&: M.ResultType, Default: StringRef("" )); |
| 213 | IO.mapOptional(Key: "SwiftReturnOwnership" , Val&: M.SwiftReturnOwnership, |
| 214 | Default: StringRef("" )); |
| 215 | IO.mapOptional(Key: "SwiftSafety" , Val&: M.SafetyKind, Default: SwiftSafetyKind::None); |
| 216 | } |
| 217 | }; |
| 218 | } // namespace yaml |
| 219 | } // namespace llvm |
| 220 | |
| 221 | namespace { |
| 222 | struct Property { |
| 223 | StringRef Name; |
| 224 | std::optional<MethodKind> Kind; |
| 225 | std::optional<NullabilityKind> Nullability; |
| 226 | AvailabilityItem Availability; |
| 227 | std::optional<bool> SwiftPrivate; |
| 228 | StringRef SwiftName; |
| 229 | std::optional<bool> SwiftImportAsAccessors; |
| 230 | StringRef Type; |
| 231 | SwiftSafetyKind SafetyKind = SwiftSafetyKind::None; |
| 232 | }; |
| 233 | |
| 234 | typedef std::vector<Property> PropertiesSeq; |
| 235 | } // namespace |
| 236 | |
| 237 | LLVM_YAML_IS_SEQUENCE_VECTOR(Property) |
| 238 | |
| 239 | namespace llvm { |
| 240 | namespace yaml { |
| 241 | template <> struct MappingTraits<Property> { |
| 242 | static void mapping(IO &IO, Property &P) { |
| 243 | IO.mapRequired(Key: "Name" , Val&: P.Name); |
| 244 | IO.mapOptional(Key: "PropertyKind" , Val&: P.Kind); |
| 245 | IO.mapOptional(Key: "Nullability" , Val&: P.Nullability, Default: std::nullopt); |
| 246 | IO.mapOptional(Key: "Availability" , Val&: P.Availability.Mode, |
| 247 | Default: APIAvailability::Available); |
| 248 | IO.mapOptional(Key: "AvailabilityMsg" , Val&: P.Availability.Msg, Default: StringRef("" )); |
| 249 | IO.mapOptional(Key: "SwiftPrivate" , Val&: P.SwiftPrivate); |
| 250 | IO.mapOptional(Key: "SwiftName" , Val&: P.SwiftName, Default: StringRef("" )); |
| 251 | IO.mapOptional(Key: "SwiftImportAsAccessors" , Val&: P.SwiftImportAsAccessors); |
| 252 | IO.mapOptional(Key: "Type" , Val&: P.Type, Default: StringRef("" )); |
| 253 | IO.mapOptional(Key: "SwiftSafety" , Val&: P.SafetyKind, Default: SwiftSafetyKind::None); |
| 254 | } |
| 255 | }; |
| 256 | } // namespace yaml |
| 257 | } // namespace llvm |
| 258 | |
| 259 | namespace { |
| 260 | struct Class { |
| 261 | StringRef Name; |
| 262 | bool AuditedForNullability = false; |
| 263 | AvailabilityItem Availability; |
| 264 | std::optional<bool> SwiftPrivate; |
| 265 | StringRef SwiftName; |
| 266 | std::optional<StringRef> SwiftBridge; |
| 267 | std::optional<StringRef> NSErrorDomain; |
| 268 | std::optional<bool> SwiftImportAsNonGeneric; |
| 269 | std::optional<bool> SwiftObjCMembers; |
| 270 | std::optional<std::string> SwiftConformance; |
| 271 | MethodsSeq Methods; |
| 272 | PropertiesSeq Properties; |
| 273 | SwiftSafetyKind SafetyKind = SwiftSafetyKind::None; |
| 274 | }; |
| 275 | |
| 276 | typedef std::vector<Class> ClassesSeq; |
| 277 | } // namespace |
| 278 | |
| 279 | LLVM_YAML_IS_SEQUENCE_VECTOR(Class) |
| 280 | |
| 281 | namespace llvm { |
| 282 | namespace yaml { |
| 283 | template <> struct MappingTraits<Class> { |
| 284 | static void mapping(IO &IO, Class &C) { |
| 285 | IO.mapRequired(Key: "Name" , Val&: C.Name); |
| 286 | IO.mapOptional(Key: "AuditedForNullability" , Val&: C.AuditedForNullability, Default: false); |
| 287 | IO.mapOptional(Key: "Availability" , Val&: C.Availability.Mode, |
| 288 | Default: APIAvailability::Available); |
| 289 | IO.mapOptional(Key: "AvailabilityMsg" , Val&: C.Availability.Msg, Default: StringRef("" )); |
| 290 | IO.mapOptional(Key: "SwiftPrivate" , Val&: C.SwiftPrivate); |
| 291 | IO.mapOptional(Key: "SwiftName" , Val&: C.SwiftName, Default: StringRef("" )); |
| 292 | IO.mapOptional(Key: "SwiftBridge" , Val&: C.SwiftBridge); |
| 293 | IO.mapOptional(Key: "NSErrorDomain" , Val&: C.NSErrorDomain); |
| 294 | IO.mapOptional(Key: "SwiftImportAsNonGeneric" , Val&: C.SwiftImportAsNonGeneric); |
| 295 | IO.mapOptional(Key: "SwiftObjCMembers" , Val&: C.SwiftObjCMembers); |
| 296 | IO.mapOptional(Key: "SwiftConformsTo" , Val&: C.SwiftConformance); |
| 297 | IO.mapOptional(Key: "Methods" , Val&: C.Methods); |
| 298 | IO.mapOptional(Key: "Properties" , Val&: C.Properties); |
| 299 | IO.mapOptional(Key: "SwiftSafety" , Val&: C.SafetyKind, Default: SwiftSafetyKind::None); |
| 300 | } |
| 301 | }; |
| 302 | } // namespace yaml |
| 303 | } // namespace llvm |
| 304 | |
| 305 | namespace { |
| 306 | struct Function { |
| 307 | StringRef Name; |
| 308 | ParamsSeq Params; |
| 309 | NullabilitySeq Nullability; |
| 310 | std::optional<NullabilityKind> NullabilityOfRet; |
| 311 | std::optional<api_notes::RetainCountConventionKind> RetainCountConvention; |
| 312 | AvailabilityItem Availability; |
| 313 | std::optional<bool> SwiftPrivate; |
| 314 | StringRef SwiftName; |
| 315 | StringRef Type; |
| 316 | StringRef ResultType; |
| 317 | StringRef SwiftReturnOwnership; |
| 318 | SwiftSafetyKind SafetyKind = SwiftSafetyKind::None; |
| 319 | }; |
| 320 | |
| 321 | typedef std::vector<Function> FunctionsSeq; |
| 322 | } // namespace |
| 323 | |
| 324 | LLVM_YAML_IS_SEQUENCE_VECTOR(Function) |
| 325 | |
| 326 | namespace llvm { |
| 327 | namespace yaml { |
| 328 | template <> struct MappingTraits<Function> { |
| 329 | static void mapping(IO &IO, Function &F) { |
| 330 | IO.mapRequired(Key: "Name" , Val&: F.Name); |
| 331 | IO.mapOptional(Key: "Parameters" , Val&: F.Params); |
| 332 | IO.mapOptional(Key: "Nullability" , Val&: F.Nullability); |
| 333 | IO.mapOptional(Key: "NullabilityOfRet" , Val&: F.NullabilityOfRet, Default: std::nullopt); |
| 334 | IO.mapOptional(Key: "RetainCountConvention" , Val&: F.RetainCountConvention); |
| 335 | IO.mapOptional(Key: "Availability" , Val&: F.Availability.Mode, |
| 336 | Default: APIAvailability::Available); |
| 337 | IO.mapOptional(Key: "AvailabilityMsg" , Val&: F.Availability.Msg, Default: StringRef("" )); |
| 338 | IO.mapOptional(Key: "SwiftPrivate" , Val&: F.SwiftPrivate); |
| 339 | IO.mapOptional(Key: "SwiftName" , Val&: F.SwiftName, Default: StringRef("" )); |
| 340 | IO.mapOptional(Key: "ResultType" , Val&: F.ResultType, Default: StringRef("" )); |
| 341 | IO.mapOptional(Key: "SwiftReturnOwnership" , Val&: F.SwiftReturnOwnership, |
| 342 | Default: StringRef("" )); |
| 343 | IO.mapOptional(Key: "SwiftSafety" , Val&: F.SafetyKind, Default: SwiftSafetyKind::None); |
| 344 | } |
| 345 | }; |
| 346 | } // namespace yaml |
| 347 | } // namespace llvm |
| 348 | |
| 349 | namespace { |
| 350 | struct GlobalVariable { |
| 351 | StringRef Name; |
| 352 | std::optional<NullabilityKind> Nullability; |
| 353 | AvailabilityItem Availability; |
| 354 | std::optional<bool> SwiftPrivate; |
| 355 | StringRef SwiftName; |
| 356 | StringRef Type; |
| 357 | SwiftSafetyKind SafetyKind = SwiftSafetyKind::None; |
| 358 | }; |
| 359 | |
| 360 | typedef std::vector<GlobalVariable> GlobalVariablesSeq; |
| 361 | } // namespace |
| 362 | |
| 363 | LLVM_YAML_IS_SEQUENCE_VECTOR(GlobalVariable) |
| 364 | |
| 365 | namespace llvm { |
| 366 | namespace yaml { |
| 367 | template <> struct MappingTraits<GlobalVariable> { |
| 368 | static void mapping(IO &IO, GlobalVariable &GV) { |
| 369 | IO.mapRequired(Key: "Name" , Val&: GV.Name); |
| 370 | IO.mapOptional(Key: "Nullability" , Val&: GV.Nullability, Default: std::nullopt); |
| 371 | IO.mapOptional(Key: "Availability" , Val&: GV.Availability.Mode, |
| 372 | Default: APIAvailability::Available); |
| 373 | IO.mapOptional(Key: "AvailabilityMsg" , Val&: GV.Availability.Msg, Default: StringRef("" )); |
| 374 | IO.mapOptional(Key: "SwiftPrivate" , Val&: GV.SwiftPrivate); |
| 375 | IO.mapOptional(Key: "SwiftName" , Val&: GV.SwiftName, Default: StringRef("" )); |
| 376 | IO.mapOptional(Key: "Type" , Val&: GV.Type, Default: StringRef("" )); |
| 377 | IO.mapOptional(Key: "SwiftSafety" , Val&: GV.SafetyKind, Default: SwiftSafetyKind::None); |
| 378 | } |
| 379 | }; |
| 380 | } // namespace yaml |
| 381 | } // namespace llvm |
| 382 | |
| 383 | namespace { |
| 384 | struct EnumConstant { |
| 385 | StringRef Name; |
| 386 | AvailabilityItem Availability; |
| 387 | std::optional<bool> SwiftPrivate; |
| 388 | StringRef SwiftName; |
| 389 | SwiftSafetyKind SafetyKind = SwiftSafetyKind::None; |
| 390 | }; |
| 391 | |
| 392 | typedef std::vector<EnumConstant> EnumConstantsSeq; |
| 393 | } // namespace |
| 394 | |
| 395 | LLVM_YAML_IS_SEQUENCE_VECTOR(EnumConstant) |
| 396 | |
| 397 | namespace llvm { |
| 398 | namespace yaml { |
| 399 | template <> struct MappingTraits<EnumConstant> { |
| 400 | static void mapping(IO &IO, EnumConstant &EC) { |
| 401 | IO.mapRequired(Key: "Name" , Val&: EC.Name); |
| 402 | IO.mapOptional(Key: "Availability" , Val&: EC.Availability.Mode, |
| 403 | Default: APIAvailability::Available); |
| 404 | IO.mapOptional(Key: "AvailabilityMsg" , Val&: EC.Availability.Msg, Default: StringRef("" )); |
| 405 | IO.mapOptional(Key: "SwiftPrivate" , Val&: EC.SwiftPrivate); |
| 406 | IO.mapOptional(Key: "SwiftName" , Val&: EC.SwiftName, Default: StringRef("" )); |
| 407 | IO.mapOptional(Key: "SwiftSafety" , Val&: EC.SafetyKind, Default: SwiftSafetyKind::None); |
| 408 | } |
| 409 | }; |
| 410 | } // namespace yaml |
| 411 | } // namespace llvm |
| 412 | |
| 413 | namespace { |
| 414 | /// Syntactic sugar for EnumExtensibility and FlagEnum |
| 415 | enum class EnumConvenienceAliasKind { |
| 416 | /// EnumExtensibility: none, FlagEnum: false |
| 417 | None, |
| 418 | /// EnumExtensibility: open, FlagEnum: false |
| 419 | CFEnum, |
| 420 | /// EnumExtensibility: open, FlagEnum: true |
| 421 | CFOptions, |
| 422 | /// EnumExtensibility: closed, FlagEnum: false |
| 423 | CFClosedEnum |
| 424 | }; |
| 425 | } // namespace |
| 426 | |
| 427 | namespace llvm { |
| 428 | namespace yaml { |
| 429 | template <> struct ScalarEnumerationTraits<EnumConvenienceAliasKind> { |
| 430 | static void enumeration(IO &IO, EnumConvenienceAliasKind &ECAK) { |
| 431 | IO.enumCase(Val&: ECAK, Str: "none" , ConstVal: EnumConvenienceAliasKind::None); |
| 432 | IO.enumCase(Val&: ECAK, Str: "CFEnum" , ConstVal: EnumConvenienceAliasKind::CFEnum); |
| 433 | IO.enumCase(Val&: ECAK, Str: "NSEnum" , ConstVal: EnumConvenienceAliasKind::CFEnum); |
| 434 | IO.enumCase(Val&: ECAK, Str: "CFOptions" , ConstVal: EnumConvenienceAliasKind::CFOptions); |
| 435 | IO.enumCase(Val&: ECAK, Str: "NSOptions" , ConstVal: EnumConvenienceAliasKind::CFOptions); |
| 436 | IO.enumCase(Val&: ECAK, Str: "CFClosedEnum" , ConstVal: EnumConvenienceAliasKind::CFClosedEnum); |
| 437 | IO.enumCase(Val&: ECAK, Str: "NSClosedEnum" , ConstVal: EnumConvenienceAliasKind::CFClosedEnum); |
| 438 | } |
| 439 | }; |
| 440 | } // namespace yaml |
| 441 | } // namespace llvm |
| 442 | |
| 443 | namespace { |
| 444 | struct Field { |
| 445 | StringRef Name; |
| 446 | std::optional<NullabilityKind> Nullability; |
| 447 | AvailabilityItem Availability; |
| 448 | std::optional<bool> SwiftPrivate; |
| 449 | StringRef SwiftName; |
| 450 | StringRef Type; |
| 451 | SwiftSafetyKind SafetyKind = SwiftSafetyKind::None; |
| 452 | }; |
| 453 | |
| 454 | typedef std::vector<Field> FieldsSeq; |
| 455 | } // namespace |
| 456 | |
| 457 | LLVM_YAML_IS_SEQUENCE_VECTOR(Field) |
| 458 | |
| 459 | namespace llvm { |
| 460 | namespace yaml { |
| 461 | template <> struct MappingTraits<Field> { |
| 462 | static void mapping(IO &IO, Field &F) { |
| 463 | IO.mapRequired(Key: "Name" , Val&: F.Name); |
| 464 | IO.mapOptional(Key: "Nullability" , Val&: F.Nullability, Default: std::nullopt); |
| 465 | IO.mapOptional(Key: "Availability" , Val&: F.Availability.Mode, |
| 466 | Default: APIAvailability::Available); |
| 467 | IO.mapOptional(Key: "AvailabilityMsg" , Val&: F.Availability.Msg, Default: StringRef("" )); |
| 468 | IO.mapOptional(Key: "SwiftPrivate" , Val&: F.SwiftPrivate); |
| 469 | IO.mapOptional(Key: "SwiftName" , Val&: F.SwiftName, Default: StringRef("" )); |
| 470 | IO.mapOptional(Key: "Type" , Val&: F.Type, Default: StringRef("" )); |
| 471 | IO.mapOptional(Key: "SwiftSafety" , Val&: F.SafetyKind, Default: SwiftSafetyKind::None); |
| 472 | } |
| 473 | }; |
| 474 | } // namespace yaml |
| 475 | } // namespace llvm |
| 476 | |
| 477 | namespace { |
| 478 | struct Tag; |
| 479 | typedef std::vector<Tag> TagsSeq; |
| 480 | |
| 481 | struct Tag { |
| 482 | StringRef Name; |
| 483 | AvailabilityItem Availability; |
| 484 | StringRef SwiftName; |
| 485 | std::optional<bool> SwiftPrivate; |
| 486 | std::optional<StringRef> SwiftBridge; |
| 487 | std::optional<StringRef> NSErrorDomain; |
| 488 | std::optional<std::string> SwiftImportAs; |
| 489 | std::optional<std::string> SwiftRetainOp; |
| 490 | std::optional<std::string> SwiftReleaseOp; |
| 491 | std::optional<std::string> SwiftDestroyOp; |
| 492 | std::optional<std::string> SwiftDefaultOwnership; |
| 493 | std::optional<std::string> SwiftConformance; |
| 494 | std::optional<EnumExtensibilityKind> EnumExtensibility; |
| 495 | std::optional<bool> FlagEnum; |
| 496 | std::optional<EnumConvenienceAliasKind> EnumConvenienceKind; |
| 497 | std::optional<bool> SwiftCopyable; |
| 498 | std::optional<bool> SwiftEscapable; |
| 499 | SwiftSafetyKind SafetyKind = SwiftSafetyKind::None; |
| 500 | FunctionsSeq Methods; |
| 501 | FieldsSeq Fields; |
| 502 | |
| 503 | /// Tags that are declared within the current tag. Only the tags that have |
| 504 | /// corresponding API Notes will be listed. |
| 505 | TagsSeq Tags; |
| 506 | }; |
| 507 | } // namespace |
| 508 | |
| 509 | LLVM_YAML_IS_SEQUENCE_VECTOR(Tag) |
| 510 | |
| 511 | namespace llvm { |
| 512 | namespace yaml { |
| 513 | template <> struct ScalarEnumerationTraits<EnumExtensibilityKind> { |
| 514 | static void enumeration(IO &IO, EnumExtensibilityKind &EEK) { |
| 515 | IO.enumCase(Val&: EEK, Str: "none" , ConstVal: EnumExtensibilityKind::None); |
| 516 | IO.enumCase(Val&: EEK, Str: "open" , ConstVal: EnumExtensibilityKind::Open); |
| 517 | IO.enumCase(Val&: EEK, Str: "closed" , ConstVal: EnumExtensibilityKind::Closed); |
| 518 | } |
| 519 | }; |
| 520 | |
| 521 | template <> struct MappingTraits<Tag> { |
| 522 | static void mapping(IO &IO, Tag &T) { |
| 523 | IO.mapRequired(Key: "Name" , Val&: T.Name); |
| 524 | IO.mapOptional(Key: "Availability" , Val&: T.Availability.Mode, |
| 525 | Default: APIAvailability::Available); |
| 526 | IO.mapOptional(Key: "AvailabilityMsg" , Val&: T.Availability.Msg, Default: StringRef("" )); |
| 527 | IO.mapOptional(Key: "SwiftPrivate" , Val&: T.SwiftPrivate); |
| 528 | IO.mapOptional(Key: "SwiftName" , Val&: T.SwiftName, Default: StringRef("" )); |
| 529 | IO.mapOptional(Key: "SwiftBridge" , Val&: T.SwiftBridge); |
| 530 | IO.mapOptional(Key: "NSErrorDomain" , Val&: T.NSErrorDomain); |
| 531 | IO.mapOptional(Key: "SwiftImportAs" , Val&: T.SwiftImportAs); |
| 532 | IO.mapOptional(Key: "SwiftReleaseOp" , Val&: T.SwiftReleaseOp); |
| 533 | IO.mapOptional(Key: "SwiftRetainOp" , Val&: T.SwiftRetainOp); |
| 534 | IO.mapOptional(Key: "SwiftDestroyOp" , Val&: T.SwiftDestroyOp); |
| 535 | IO.mapOptional(Key: "SwiftDefaultOwnership" , Val&: T.SwiftDefaultOwnership); |
| 536 | IO.mapOptional(Key: "SwiftConformsTo" , Val&: T.SwiftConformance); |
| 537 | IO.mapOptional(Key: "EnumExtensibility" , Val&: T.EnumExtensibility); |
| 538 | IO.mapOptional(Key: "FlagEnum" , Val&: T.FlagEnum); |
| 539 | IO.mapOptional(Key: "EnumKind" , Val&: T.EnumConvenienceKind); |
| 540 | IO.mapOptional(Key: "SwiftCopyable" , Val&: T.SwiftCopyable); |
| 541 | IO.mapOptional(Key: "SwiftEscapable" , Val&: T.SwiftEscapable); |
| 542 | IO.mapOptional(Key: "Methods" , Val&: T.Methods); |
| 543 | IO.mapOptional(Key: "Fields" , Val&: T.Fields); |
| 544 | IO.mapOptional(Key: "Tags" , Val&: T.Tags); |
| 545 | IO.mapOptional(Key: "SwiftSafety" , Val&: T.SafetyKind, Default: SwiftSafetyKind::None); |
| 546 | } |
| 547 | }; |
| 548 | } // namespace yaml |
| 549 | } // namespace llvm |
| 550 | |
| 551 | namespace { |
| 552 | struct Typedef { |
| 553 | StringRef Name; |
| 554 | AvailabilityItem Availability; |
| 555 | StringRef SwiftName; |
| 556 | std::optional<bool> SwiftPrivate; |
| 557 | std::optional<StringRef> SwiftBridge; |
| 558 | std::optional<StringRef> NSErrorDomain; |
| 559 | std::optional<SwiftNewTypeKind> SwiftType; |
| 560 | std::optional<std::string> SwiftConformance; |
| 561 | const SwiftSafetyKind SafetyKind = SwiftSafetyKind::None; |
| 562 | }; |
| 563 | |
| 564 | typedef std::vector<Typedef> TypedefsSeq; |
| 565 | } // namespace |
| 566 | |
| 567 | LLVM_YAML_IS_SEQUENCE_VECTOR(Typedef) |
| 568 | |
| 569 | namespace llvm { |
| 570 | namespace yaml { |
| 571 | template <> struct ScalarEnumerationTraits<SwiftNewTypeKind> { |
| 572 | static void enumeration(IO &IO, SwiftNewTypeKind &SWK) { |
| 573 | IO.enumCase(Val&: SWK, Str: "none" , ConstVal: SwiftNewTypeKind::None); |
| 574 | IO.enumCase(Val&: SWK, Str: "struct" , ConstVal: SwiftNewTypeKind::Struct); |
| 575 | IO.enumCase(Val&: SWK, Str: "enum" , ConstVal: SwiftNewTypeKind::Enum); |
| 576 | } |
| 577 | }; |
| 578 | |
| 579 | template <> struct MappingTraits<Typedef> { |
| 580 | static void mapping(IO &IO, Typedef &T) { |
| 581 | IO.mapRequired(Key: "Name" , Val&: T.Name); |
| 582 | IO.mapOptional(Key: "Availability" , Val&: T.Availability.Mode, |
| 583 | Default: APIAvailability::Available); |
| 584 | IO.mapOptional(Key: "AvailabilityMsg" , Val&: T.Availability.Msg, Default: StringRef("" )); |
| 585 | IO.mapOptional(Key: "SwiftPrivate" , Val&: T.SwiftPrivate); |
| 586 | IO.mapOptional(Key: "SwiftName" , Val&: T.SwiftName, Default: StringRef("" )); |
| 587 | IO.mapOptional(Key: "SwiftBridge" , Val&: T.SwiftBridge); |
| 588 | IO.mapOptional(Key: "NSErrorDomain" , Val&: T.NSErrorDomain); |
| 589 | IO.mapOptional(Key: "SwiftWrapper" , Val&: T.SwiftType); |
| 590 | IO.mapOptional(Key: "SwiftConformsTo" , Val&: T.SwiftConformance); |
| 591 | } |
| 592 | }; |
| 593 | } // namespace yaml |
| 594 | } // namespace llvm |
| 595 | |
| 596 | namespace { |
| 597 | struct Namespace; |
| 598 | typedef std::vector<Namespace> NamespacesSeq; |
| 599 | |
| 600 | struct TopLevelItems { |
| 601 | ClassesSeq Classes; |
| 602 | ClassesSeq Protocols; |
| 603 | FunctionsSeq Functions; |
| 604 | GlobalVariablesSeq Globals; |
| 605 | EnumConstantsSeq EnumConstants; |
| 606 | TagsSeq Tags; |
| 607 | TypedefsSeq Typedefs; |
| 608 | NamespacesSeq Namespaces; |
| 609 | }; |
| 610 | } // namespace |
| 611 | |
| 612 | namespace llvm { |
| 613 | namespace yaml { |
| 614 | static void mapTopLevelItems(IO &IO, TopLevelItems &TLI) { |
| 615 | IO.mapOptional(Key: "Classes" , Val&: TLI.Classes); |
| 616 | IO.mapOptional(Key: "Protocols" , Val&: TLI.Protocols); |
| 617 | IO.mapOptional(Key: "Functions" , Val&: TLI.Functions); |
| 618 | IO.mapOptional(Key: "Globals" , Val&: TLI.Globals); |
| 619 | IO.mapOptional(Key: "Enumerators" , Val&: TLI.EnumConstants); |
| 620 | IO.mapOptional(Key: "Tags" , Val&: TLI.Tags); |
| 621 | IO.mapOptional(Key: "Typedefs" , Val&: TLI.Typedefs); |
| 622 | IO.mapOptional(Key: "Namespaces" , Val&: TLI.Namespaces); |
| 623 | } |
| 624 | } // namespace yaml |
| 625 | } // namespace llvm |
| 626 | |
| 627 | namespace { |
| 628 | struct Namespace { |
| 629 | StringRef Name; |
| 630 | AvailabilityItem Availability; |
| 631 | StringRef SwiftName; |
| 632 | std::optional<bool> SwiftPrivate; |
| 633 | TopLevelItems Items; |
| 634 | const SwiftSafetyKind SafetyKind = SwiftSafetyKind::None; |
| 635 | }; |
| 636 | } // namespace |
| 637 | |
| 638 | LLVM_YAML_IS_SEQUENCE_VECTOR(Namespace) |
| 639 | |
| 640 | namespace llvm { |
| 641 | namespace yaml { |
| 642 | template <> struct MappingTraits<Namespace> { |
| 643 | static void mapping(IO &IO, Namespace &T) { |
| 644 | IO.mapRequired(Key: "Name" , Val&: T.Name); |
| 645 | IO.mapOptional(Key: "Availability" , Val&: T.Availability.Mode, |
| 646 | Default: APIAvailability::Available); |
| 647 | IO.mapOptional(Key: "AvailabilityMsg" , Val&: T.Availability.Msg, Default: StringRef("" )); |
| 648 | IO.mapOptional(Key: "SwiftPrivate" , Val&: T.SwiftPrivate); |
| 649 | IO.mapOptional(Key: "SwiftName" , Val&: T.SwiftName, Default: StringRef("" )); |
| 650 | mapTopLevelItems(IO, TLI&: T.Items); |
| 651 | } |
| 652 | }; |
| 653 | } // namespace yaml |
| 654 | } // namespace llvm |
| 655 | |
| 656 | namespace { |
| 657 | struct Versioned { |
| 658 | VersionTuple Version; |
| 659 | TopLevelItems Items; |
| 660 | }; |
| 661 | |
| 662 | typedef std::vector<Versioned> VersionedSeq; |
| 663 | } // namespace |
| 664 | |
| 665 | LLVM_YAML_IS_SEQUENCE_VECTOR(Versioned) |
| 666 | |
| 667 | namespace llvm { |
| 668 | namespace yaml { |
| 669 | template <> struct MappingTraits<Versioned> { |
| 670 | static void mapping(IO &IO, Versioned &V) { |
| 671 | IO.mapRequired(Key: "Version" , Val&: V.Version); |
| 672 | mapTopLevelItems(IO, TLI&: V.Items); |
| 673 | } |
| 674 | }; |
| 675 | } // namespace yaml |
| 676 | } // namespace llvm |
| 677 | |
| 678 | namespace { |
| 679 | struct Module { |
| 680 | StringRef Name; |
| 681 | AvailabilityItem Availability; |
| 682 | TopLevelItems TopLevel; |
| 683 | VersionedSeq SwiftVersions; |
| 684 | |
| 685 | std::optional<bool> SwiftInferImportAsMember; |
| 686 | |
| 687 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 688 | LLVM_DUMP_METHOD void dump() /*const*/; |
| 689 | #endif |
| 690 | }; |
| 691 | } // namespace |
| 692 | |
| 693 | namespace llvm { |
| 694 | namespace yaml { |
| 695 | template <> struct MappingTraits<Module> { |
| 696 | static void mapping(IO &IO, Module &M) { |
| 697 | IO.mapRequired(Key: "Name" , Val&: M.Name); |
| 698 | IO.mapOptional(Key: "Availability" , Val&: M.Availability.Mode, |
| 699 | Default: APIAvailability::Available); |
| 700 | IO.mapOptional(Key: "AvailabilityMsg" , Val&: M.Availability.Msg, Default: StringRef("" )); |
| 701 | IO.mapOptional(Key: "SwiftInferImportAsMember" , Val&: M.SwiftInferImportAsMember); |
| 702 | mapTopLevelItems(IO, TLI&: M.TopLevel); |
| 703 | IO.mapOptional(Key: "SwiftVersions" , Val&: M.SwiftVersions); |
| 704 | } |
| 705 | }; |
| 706 | } // namespace yaml |
| 707 | } // namespace llvm |
| 708 | |
| 709 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 710 | LLVM_DUMP_METHOD void Module::dump() { |
| 711 | llvm::yaml::Output OS(llvm::errs()); |
| 712 | OS << *this; |
| 713 | } |
| 714 | #endif |
| 715 | |
| 716 | namespace { |
| 717 | bool parseAPINotes(StringRef YI, Module &M, llvm::SourceMgr::DiagHandlerTy Diag, |
| 718 | void *DiagContext) { |
| 719 | llvm::yaml::Input IS(YI, nullptr, Diag, DiagContext); |
| 720 | IS >> M; |
| 721 | return static_cast<bool>(IS.error()); |
| 722 | } |
| 723 | } // namespace |
| 724 | |
| 725 | bool clang::api_notes::parseAndDumpAPINotes(StringRef YI, |
| 726 | llvm::raw_ostream &OS) { |
| 727 | Module M; |
| 728 | if (parseAPINotes(YI, M, Diag: nullptr, DiagContext: nullptr)) |
| 729 | return true; |
| 730 | |
| 731 | llvm::yaml::Output YOS(OS); |
| 732 | YOS << M; |
| 733 | |
| 734 | return false; |
| 735 | } |
| 736 | |
| 737 | namespace { |
| 738 | using namespace api_notes; |
| 739 | |
| 740 | class YAMLConverter { |
| 741 | const Module &M; |
| 742 | APINotesWriter Writer; |
| 743 | llvm::raw_ostream &OS; |
| 744 | llvm::SourceMgr::DiagHandlerTy DiagHandler; |
| 745 | void *DiagHandlerCtxt; |
| 746 | bool ErrorOccurred; |
| 747 | |
| 748 | /// Emit a diagnostic |
| 749 | bool emitError(llvm::Twine Message) { |
| 750 | DiagHandler( |
| 751 | llvm::SMDiagnostic("" , llvm::SourceMgr::DK_Error, Message.str()), |
| 752 | DiagHandlerCtxt); |
| 753 | ErrorOccurred = true; |
| 754 | return true; |
| 755 | } |
| 756 | |
| 757 | public: |
| 758 | YAMLConverter(const Module &TheModule, const FileEntry *SourceFile, |
| 759 | llvm::raw_ostream &OS, |
| 760 | llvm::SourceMgr::DiagHandlerTy DiagHandler, |
| 761 | void *DiagHandlerCtxt) |
| 762 | : M(TheModule), Writer(TheModule.Name, SourceFile), OS(OS), |
| 763 | DiagHandler(DiagHandler), DiagHandlerCtxt(DiagHandlerCtxt), |
| 764 | ErrorOccurred(false) {} |
| 765 | |
| 766 | void convertAvailability(const AvailabilityItem &Availability, |
| 767 | CommonEntityInfo &CEI, llvm::StringRef APIName) { |
| 768 | // Populate the unavailability information. |
| 769 | CEI.Unavailable = (Availability.Mode == APIAvailability::None); |
| 770 | CEI.UnavailableInSwift = (Availability.Mode == APIAvailability::NonSwift); |
| 771 | if (CEI.Unavailable || CEI.UnavailableInSwift) { |
| 772 | CEI.UnavailableMsg = std::string(Availability.Msg); |
| 773 | } else { |
| 774 | if (!Availability.Msg.empty()) |
| 775 | emitError(Message: llvm::Twine("availability message for available API '" ) + |
| 776 | APIName + "' will not be used" ); |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | void convertParams(const ParamsSeq &Params, FunctionInfo &OutInfo, |
| 781 | std::optional<ParamInfo> &thisOrSelf) { |
| 782 | for (const auto &P : Params) { |
| 783 | ParamInfo PI; |
| 784 | if (P.Nullability) |
| 785 | PI.setNullabilityAudited(*P.Nullability); |
| 786 | PI.setNoEscape(P.NoEscape); |
| 787 | PI.setLifetimebound(P.Lifetimebound); |
| 788 | PI.setType(std::string(P.Type)); |
| 789 | PI.setRetainCountConvention(P.RetainCountConvention); |
| 790 | if (static_cast<int>(OutInfo.Params.size()) <= P.Position) |
| 791 | OutInfo.Params.resize(new_size: P.Position + 1); |
| 792 | if (P.Position == -1) |
| 793 | thisOrSelf = PI; |
| 794 | else if (P.Position >= 0) |
| 795 | OutInfo.Params[P.Position] |= PI; |
| 796 | else |
| 797 | emitError(Message: "invalid parameter position " + llvm::itostr(X: P.Position)); |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | void convertNullability(const NullabilitySeq &Nullability, |
| 802 | std::optional<NullabilityKind> ReturnNullability, |
| 803 | FunctionInfo &OutInfo, llvm::StringRef APIName) { |
| 804 | if (Nullability.size() > FunctionInfo::getMaxNullabilityIndex()) { |
| 805 | emitError(Message: llvm::Twine("nullability info for '" ) + APIName + |
| 806 | "' does not fit" ); |
| 807 | return; |
| 808 | } |
| 809 | |
| 810 | bool audited = false; |
| 811 | unsigned int idx = 1; |
| 812 | for (const auto &N : Nullability) |
| 813 | OutInfo.addTypeInfo(index: idx++, kind: N); |
| 814 | audited = Nullability.size() > 0 || ReturnNullability; |
| 815 | if (audited) |
| 816 | OutInfo.addTypeInfo(index: 0, |
| 817 | kind: ReturnNullability.value_or(u: NullabilityKind::NonNull)); |
| 818 | if (!audited) |
| 819 | return; |
| 820 | OutInfo.NullabilityAudited = audited; |
| 821 | OutInfo.NumAdjustedNullable = idx; |
| 822 | } |
| 823 | |
| 824 | /// Convert the common parts of an entity from YAML. |
| 825 | template <typename T> |
| 826 | void convertCommonEntity(const T &Common, CommonEntityInfo &Info, |
| 827 | StringRef APIName) { |
| 828 | convertAvailability(Availability: Common.Availability, CEI&: Info, APIName); |
| 829 | Info.setSwiftPrivate(Common.SwiftPrivate); |
| 830 | if (Common.SafetyKind != SwiftSafetyKind::None) |
| 831 | Info.setSwiftSafety(Common.SafetyKind); |
| 832 | Info.SwiftName = std::string(Common.SwiftName); |
| 833 | } |
| 834 | |
| 835 | /// Convert the common parts of a type entity from YAML. |
| 836 | template <typename T> |
| 837 | void convertCommonType(const T &Common, CommonTypeInfo &Info, |
| 838 | StringRef APIName) { |
| 839 | convertCommonEntity(Common, Info, APIName); |
| 840 | if (Common.SwiftBridge) |
| 841 | Info.setSwiftBridge(std::string(*Common.SwiftBridge)); |
| 842 | Info.setNSErrorDomain(Common.NSErrorDomain); |
| 843 | if (auto conformance = Common.SwiftConformance) |
| 844 | Info.setSwiftConformance(conformance); |
| 845 | } |
| 846 | |
| 847 | // Translate from Method into ObjCMethodInfo and write it out. |
| 848 | void convertMethod(const Method &M, ContextID ClassID, StringRef ClassName, |
| 849 | VersionTuple SwiftVersion) { |
| 850 | ObjCMethodInfo MI; |
| 851 | convertCommonEntity(Common: M, Info&: MI, APIName: M.Selector); |
| 852 | |
| 853 | // Check if the selector ends with ':' to determine if it takes arguments. |
| 854 | bool takesArguments = M.Selector.ends_with(Suffix: ":" ); |
| 855 | |
| 856 | // Split the selector into pieces. |
| 857 | llvm::SmallVector<StringRef, 4> Args; |
| 858 | M.Selector.split(A&: Args, Separator: ":" , /*MaxSplit*/ -1, /*KeepEmpty*/ false); |
| 859 | if (!takesArguments && Args.size() > 1) { |
| 860 | emitError(Message: "selector '" + M.Selector + "' is missing a ':' at the end" ); |
| 861 | return; |
| 862 | } |
| 863 | |
| 864 | // Construct ObjCSelectorRef. |
| 865 | api_notes::ObjCSelectorRef Selector; |
| 866 | Selector.NumArgs = !takesArguments ? 0 : Args.size(); |
| 867 | Selector.Identifiers = Args; |
| 868 | |
| 869 | // Translate the initializer info. |
| 870 | MI.DesignatedInit = M.DesignatedInit; |
| 871 | MI.RequiredInit = M.Required; |
| 872 | if (M.FactoryAsInit != FactoryAsInitKind::Infer) |
| 873 | emitError(Message: "'FactoryAsInit' is no longer valid; use 'SwiftName' instead" ); |
| 874 | |
| 875 | MI.ResultType = std::string(M.ResultType); |
| 876 | MI.SwiftReturnOwnership = std::string(M.SwiftReturnOwnership); |
| 877 | |
| 878 | // Translate parameter information. |
| 879 | convertParams(Params: M.Params, OutInfo&: MI, thisOrSelf&: MI.Self); |
| 880 | |
| 881 | // Translate nullability info. |
| 882 | convertNullability(Nullability: M.Nullability, ReturnNullability: M.NullabilityOfRet, OutInfo&: MI, APIName: M.Selector); |
| 883 | |
| 884 | MI.setRetainCountConvention(M.RetainCountConvention); |
| 885 | |
| 886 | // Write it. |
| 887 | Writer.addObjCMethod(CtxID: ClassID, Selector, IsInstanceMethod: M.Kind == MethodKind::Instance, Info: MI, |
| 888 | SwiftVersion); |
| 889 | } |
| 890 | |
| 891 | template <typename T> |
| 892 | void convertVariable(const T &Entity, VariableInfo &VI) { |
| 893 | convertAvailability(Availability: Entity.Availability, CEI&: VI, APIName: Entity.Name); |
| 894 | VI.setSwiftPrivate(Entity.SwiftPrivate); |
| 895 | VI.SwiftName = std::string(Entity.SwiftName); |
| 896 | if (Entity.Nullability) |
| 897 | VI.setNullabilityAudited(*Entity.Nullability); |
| 898 | VI.setType(std::string(Entity.Type)); |
| 899 | } |
| 900 | |
| 901 | void convertContext(std::optional<ContextID> ParentContextID, const Class &C, |
| 902 | ContextKind Kind, VersionTuple SwiftVersion) { |
| 903 | // Write the class. |
| 904 | ContextInfo CI; |
| 905 | convertCommonType(Common: C, Info&: CI, APIName: C.Name); |
| 906 | |
| 907 | if (C.AuditedForNullability) |
| 908 | CI.setDefaultNullability(NullabilityKind::NonNull); |
| 909 | if (C.SwiftImportAsNonGeneric) |
| 910 | CI.setSwiftImportAsNonGeneric(*C.SwiftImportAsNonGeneric); |
| 911 | if (C.SwiftObjCMembers) |
| 912 | CI.setSwiftObjCMembers(*C.SwiftObjCMembers); |
| 913 | |
| 914 | ContextID CtxID = |
| 915 | Writer.addContext(ParentCtxID: ParentContextID, Name: C.Name, Kind, Info: CI, SwiftVersion); |
| 916 | |
| 917 | // Write all methods. |
| 918 | llvm::StringMap<std::pair<bool, bool>> KnownMethods; |
| 919 | for (const auto &method : C.Methods) { |
| 920 | // Check for duplicate method definitions. |
| 921 | bool IsInstanceMethod = method.Kind == MethodKind::Instance; |
| 922 | bool &Known = IsInstanceMethod ? KnownMethods[method.Selector].first |
| 923 | : KnownMethods[method.Selector].second; |
| 924 | if (Known) { |
| 925 | emitError(Message: llvm::Twine("duplicate definition of method '" ) + |
| 926 | (IsInstanceMethod ? "-" : "+" ) + "[" + C.Name + " " + |
| 927 | method.Selector + "]'" ); |
| 928 | continue; |
| 929 | } |
| 930 | Known = true; |
| 931 | |
| 932 | convertMethod(M: method, ClassID: CtxID, ClassName: C.Name, SwiftVersion); |
| 933 | } |
| 934 | |
| 935 | // Write all properties. |
| 936 | llvm::StringSet<> KnownInstanceProperties; |
| 937 | llvm::StringSet<> KnownClassProperties; |
| 938 | for (const auto &Property : C.Properties) { |
| 939 | // Check for duplicate property definitions. |
| 940 | if ((!Property.Kind || *Property.Kind == MethodKind::Instance) && |
| 941 | !KnownInstanceProperties.insert(key: Property.Name).second) { |
| 942 | emitError(Message: llvm::Twine("duplicate definition of instance property '" ) + |
| 943 | C.Name + "." + Property.Name + "'" ); |
| 944 | continue; |
| 945 | } |
| 946 | |
| 947 | if ((!Property.Kind || *Property.Kind == MethodKind::Class) && |
| 948 | !KnownClassProperties.insert(key: Property.Name).second) { |
| 949 | emitError(Message: llvm::Twine("duplicate definition of class property '" ) + |
| 950 | C.Name + "." + Property.Name + "'" ); |
| 951 | continue; |
| 952 | } |
| 953 | |
| 954 | // Translate from Property into ObjCPropertyInfo. |
| 955 | ObjCPropertyInfo PI; |
| 956 | convertVariable(Entity: Property, VI&: PI); |
| 957 | if (Property.SwiftImportAsAccessors) |
| 958 | PI.setSwiftImportAsAccessors(*Property.SwiftImportAsAccessors); |
| 959 | |
| 960 | // Add both instance and class properties with this name. |
| 961 | if (Property.Kind) { |
| 962 | Writer.addObjCProperty(CtxID, Name: Property.Name, |
| 963 | IsInstanceProperty: *Property.Kind == MethodKind::Instance, Info: PI, |
| 964 | SwiftVersion); |
| 965 | } else { |
| 966 | Writer.addObjCProperty(CtxID, Name: Property.Name, IsInstanceProperty: true, Info: PI, SwiftVersion); |
| 967 | Writer.addObjCProperty(CtxID, Name: Property.Name, IsInstanceProperty: false, Info: PI, SwiftVersion); |
| 968 | } |
| 969 | } |
| 970 | } |
| 971 | |
| 972 | void convertNamespaceContext(std::optional<ContextID> ParentContextID, |
| 973 | const Namespace &TheNamespace, |
| 974 | VersionTuple SwiftVersion) { |
| 975 | // Write the namespace. |
| 976 | ContextInfo CI; |
| 977 | convertCommonEntity(Common: TheNamespace, Info&: CI, APIName: TheNamespace.Name); |
| 978 | |
| 979 | ContextID CtxID = |
| 980 | Writer.addContext(ParentCtxID: ParentContextID, Name: TheNamespace.Name, |
| 981 | Kind: ContextKind::Namespace, Info: CI, SwiftVersion); |
| 982 | |
| 983 | convertTopLevelItems(Ctx: Context(CtxID, ContextKind::Namespace), |
| 984 | TLItems: TheNamespace.Items, SwiftVersion); |
| 985 | } |
| 986 | |
| 987 | template <typename FuncOrMethodInfo> |
| 988 | void convertFunction(const Function &Function, FuncOrMethodInfo &FI) { |
| 989 | convertAvailability(Availability: Function.Availability, CEI&: FI, APIName: Function.Name); |
| 990 | FI.setSwiftPrivate(Function.SwiftPrivate); |
| 991 | if (Function.SafetyKind != SwiftSafetyKind::None) |
| 992 | FI.setSwiftSafety(Function.SafetyKind); |
| 993 | FI.SwiftName = std::string(Function.SwiftName); |
| 994 | std::optional<ParamInfo> This; |
| 995 | convertParams(Params: Function.Params, OutInfo&: FI, thisOrSelf&: This); |
| 996 | if constexpr (std::is_same_v<FuncOrMethodInfo, CXXMethodInfo>) |
| 997 | FI.This = This; |
| 998 | else if (This) |
| 999 | emitError(Message: "implicit instance parameter is only permitted on C++ and " |
| 1000 | "Objective-C methods" ); |
| 1001 | convertNullability(Nullability: Function.Nullability, ReturnNullability: Function.NullabilityOfRet, OutInfo&: FI, |
| 1002 | APIName: Function.Name); |
| 1003 | FI.ResultType = std::string(Function.ResultType); |
| 1004 | FI.SwiftReturnOwnership = std::string(Function.SwiftReturnOwnership); |
| 1005 | FI.setRetainCountConvention(Function.RetainCountConvention); |
| 1006 | } |
| 1007 | |
| 1008 | void convertTagContext(std::optional<Context> ParentContext, const Tag &T, |
| 1009 | VersionTuple SwiftVersion) { |
| 1010 | TagInfo TI; |
| 1011 | std::optional<ContextID> ParentContextID = |
| 1012 | ParentContext ? std::optional<ContextID>(ParentContext->id) |
| 1013 | : std::nullopt; |
| 1014 | convertCommonType(Common: T, Info&: TI, APIName: T.Name); |
| 1015 | |
| 1016 | if ((T.SwiftRetainOp || T.SwiftReleaseOp) && !T.SwiftImportAs) { |
| 1017 | emitError(Message: llvm::Twine("should declare SwiftImportAs to use " |
| 1018 | "SwiftRetainOp and SwiftReleaseOp (for " ) + |
| 1019 | T.Name + ")" ); |
| 1020 | return; |
| 1021 | } |
| 1022 | if (T.SwiftReleaseOp.has_value() != T.SwiftRetainOp.has_value()) { |
| 1023 | emitError(Message: llvm::Twine("should declare both SwiftReleaseOp and " |
| 1024 | "SwiftRetainOp (for " ) + |
| 1025 | T.Name + ")" ); |
| 1026 | return; |
| 1027 | } |
| 1028 | |
| 1029 | if (T.SwiftImportAs) |
| 1030 | TI.SwiftImportAs = T.SwiftImportAs; |
| 1031 | if (T.SwiftRetainOp) |
| 1032 | TI.SwiftRetainOp = T.SwiftRetainOp; |
| 1033 | if (T.SwiftReleaseOp) |
| 1034 | TI.SwiftReleaseOp = T.SwiftReleaseOp; |
| 1035 | if (T.SwiftDestroyOp) |
| 1036 | TI.SwiftDestroyOp = T.SwiftDestroyOp; |
| 1037 | if (T.SwiftDefaultOwnership) |
| 1038 | TI.SwiftDefaultOwnership = T.SwiftDefaultOwnership; |
| 1039 | |
| 1040 | if (T.SwiftCopyable) |
| 1041 | TI.setSwiftCopyable(T.SwiftCopyable); |
| 1042 | if (T.SwiftEscapable) |
| 1043 | TI.setSwiftEscapable(T.SwiftEscapable); |
| 1044 | |
| 1045 | if (T.EnumConvenienceKind) { |
| 1046 | if (T.EnumExtensibility) { |
| 1047 | emitError( |
| 1048 | Message: llvm::Twine("cannot mix EnumKind and EnumExtensibility (for " ) + |
| 1049 | T.Name + ")" ); |
| 1050 | return; |
| 1051 | } |
| 1052 | if (T.FlagEnum) { |
| 1053 | emitError(Message: llvm::Twine("cannot mix EnumKind and FlagEnum (for " ) + |
| 1054 | T.Name + ")" ); |
| 1055 | return; |
| 1056 | } |
| 1057 | switch (*T.EnumConvenienceKind) { |
| 1058 | case EnumConvenienceAliasKind::None: |
| 1059 | TI.EnumExtensibility = EnumExtensibilityKind::None; |
| 1060 | TI.setFlagEnum(false); |
| 1061 | break; |
| 1062 | case EnumConvenienceAliasKind::CFEnum: |
| 1063 | TI.EnumExtensibility = EnumExtensibilityKind::Open; |
| 1064 | TI.setFlagEnum(false); |
| 1065 | break; |
| 1066 | case EnumConvenienceAliasKind::CFOptions: |
| 1067 | TI.EnumExtensibility = EnumExtensibilityKind::Open; |
| 1068 | TI.setFlagEnum(true); |
| 1069 | break; |
| 1070 | case EnumConvenienceAliasKind::CFClosedEnum: |
| 1071 | TI.EnumExtensibility = EnumExtensibilityKind::Closed; |
| 1072 | TI.setFlagEnum(false); |
| 1073 | break; |
| 1074 | } |
| 1075 | } else { |
| 1076 | TI.EnumExtensibility = T.EnumExtensibility; |
| 1077 | TI.setFlagEnum(T.FlagEnum); |
| 1078 | } |
| 1079 | |
| 1080 | Writer.addTag(Ctx: ParentContext, Name: T.Name, Info: TI, SwiftVersion); |
| 1081 | |
| 1082 | ContextInfo CI; |
| 1083 | auto TagCtxID = Writer.addContext(ParentCtxID: ParentContextID, Name: T.Name, Kind: ContextKind::Tag, |
| 1084 | Info: CI, SwiftVersion); |
| 1085 | Context TagCtx(TagCtxID, ContextKind::Tag); |
| 1086 | |
| 1087 | for (const auto &Field : T.Fields) { |
| 1088 | FieldInfo FI; |
| 1089 | convertVariable(Entity: Field, VI&: FI); |
| 1090 | Writer.addField(CtxID: TagCtxID, Name: Field.Name, Info: FI, SwiftVersion); |
| 1091 | } |
| 1092 | |
| 1093 | for (const auto &CXXMethod : T.Methods) { |
| 1094 | CXXMethodInfo MI; |
| 1095 | convertFunction(Function: CXXMethod, FI&: MI); |
| 1096 | Writer.addCXXMethod(CtxID: TagCtxID, Name: CXXMethod.Name, Info: MI, SwiftVersion); |
| 1097 | } |
| 1098 | |
| 1099 | // Convert nested tags. |
| 1100 | for (const auto &Tag : T.Tags) |
| 1101 | convertTagContext(ParentContext: TagCtx, T: Tag, SwiftVersion); |
| 1102 | } |
| 1103 | |
| 1104 | void convertTopLevelItems(std::optional<Context> Ctx, |
| 1105 | const TopLevelItems &TLItems, |
| 1106 | VersionTuple SwiftVersion) { |
| 1107 | std::optional<ContextID> CtxID = |
| 1108 | Ctx ? std::optional(Ctx->id) : std::nullopt; |
| 1109 | |
| 1110 | // Write all classes. |
| 1111 | llvm::StringSet<> KnownClasses; |
| 1112 | for (const auto &Class : TLItems.Classes) { |
| 1113 | // Check for duplicate class definitions. |
| 1114 | if (!KnownClasses.insert(key: Class.Name).second) { |
| 1115 | emitError(Message: llvm::Twine("multiple definitions of class '" ) + Class.Name + |
| 1116 | "'" ); |
| 1117 | continue; |
| 1118 | } |
| 1119 | |
| 1120 | convertContext(ParentContextID: CtxID, C: Class, Kind: ContextKind::ObjCClass, SwiftVersion); |
| 1121 | } |
| 1122 | |
| 1123 | // Write all protocols. |
| 1124 | llvm::StringSet<> KnownProtocols; |
| 1125 | for (const auto &Protocol : TLItems.Protocols) { |
| 1126 | // Check for duplicate protocol definitions. |
| 1127 | if (!KnownProtocols.insert(key: Protocol.Name).second) { |
| 1128 | emitError(Message: llvm::Twine("multiple definitions of protocol '" ) + |
| 1129 | Protocol.Name + "'" ); |
| 1130 | continue; |
| 1131 | } |
| 1132 | |
| 1133 | convertContext(ParentContextID: CtxID, C: Protocol, Kind: ContextKind::ObjCProtocol, SwiftVersion); |
| 1134 | } |
| 1135 | |
| 1136 | // Write all namespaces. |
| 1137 | llvm::StringSet<> KnownNamespaces; |
| 1138 | for (const auto &Namespace : TLItems.Namespaces) { |
| 1139 | // Check for duplicate namespace definitions. |
| 1140 | if (!KnownNamespaces.insert(key: Namespace.Name).second) { |
| 1141 | emitError(Message: llvm::Twine("multiple definitions of namespace '" ) + |
| 1142 | Namespace.Name + "'" ); |
| 1143 | continue; |
| 1144 | } |
| 1145 | |
| 1146 | convertNamespaceContext(ParentContextID: CtxID, TheNamespace: Namespace, SwiftVersion); |
| 1147 | } |
| 1148 | |
| 1149 | // Write all global variables. |
| 1150 | llvm::StringSet<> KnownGlobals; |
| 1151 | for (const auto &Global : TLItems.Globals) { |
| 1152 | // Check for duplicate global variables. |
| 1153 | if (!KnownGlobals.insert(key: Global.Name).second) { |
| 1154 | emitError(Message: llvm::Twine("multiple definitions of global variable '" ) + |
| 1155 | Global.Name + "'" ); |
| 1156 | continue; |
| 1157 | } |
| 1158 | |
| 1159 | GlobalVariableInfo GVI; |
| 1160 | convertVariable(Entity: Global, VI&: GVI); |
| 1161 | Writer.addGlobalVariable(Ctx, Name: Global.Name, Info: GVI, SwiftVersion); |
| 1162 | } |
| 1163 | |
| 1164 | // Write all global functions. |
| 1165 | llvm::StringSet<> KnownFunctions; |
| 1166 | for (const auto &Function : TLItems.Functions) { |
| 1167 | // Check for duplicate global functions. |
| 1168 | if (!KnownFunctions.insert(key: Function.Name).second) { |
| 1169 | emitError(Message: llvm::Twine("multiple definitions of global function '" ) + |
| 1170 | Function.Name + "'" ); |
| 1171 | continue; |
| 1172 | } |
| 1173 | |
| 1174 | GlobalFunctionInfo GFI; |
| 1175 | convertFunction(Function, FI&: GFI); |
| 1176 | Writer.addGlobalFunction(Ctx, Name: Function.Name, Info: GFI, SwiftVersion); |
| 1177 | } |
| 1178 | |
| 1179 | // Write all enumerators. |
| 1180 | llvm::StringSet<> KnownEnumConstants; |
| 1181 | for (const auto &EnumConstant : TLItems.EnumConstants) { |
| 1182 | // Check for duplicate enumerators |
| 1183 | if (!KnownEnumConstants.insert(key: EnumConstant.Name).second) { |
| 1184 | emitError(Message: llvm::Twine("multiple definitions of enumerator '" ) + |
| 1185 | EnumConstant.Name + "'" ); |
| 1186 | continue; |
| 1187 | } |
| 1188 | |
| 1189 | EnumConstantInfo ECI; |
| 1190 | convertAvailability(Availability: EnumConstant.Availability, CEI&: ECI, APIName: EnumConstant.Name); |
| 1191 | ECI.setSwiftPrivate(EnumConstant.SwiftPrivate); |
| 1192 | ECI.SwiftName = std::string(EnumConstant.SwiftName); |
| 1193 | Writer.addEnumConstant(Name: EnumConstant.Name, Info: ECI, SwiftVersion); |
| 1194 | } |
| 1195 | |
| 1196 | // Write all tags. |
| 1197 | llvm::StringSet<> KnownTags; |
| 1198 | for (const auto &Tag : TLItems.Tags) { |
| 1199 | // Check for duplicate tag definitions. |
| 1200 | if (!KnownTags.insert(key: Tag.Name).second) { |
| 1201 | emitError(Message: llvm::Twine("multiple definitions of tag '" ) + Tag.Name + |
| 1202 | "'" ); |
| 1203 | continue; |
| 1204 | } |
| 1205 | |
| 1206 | convertTagContext(ParentContext: Ctx, T: Tag, SwiftVersion); |
| 1207 | } |
| 1208 | |
| 1209 | // Write all typedefs. |
| 1210 | llvm::StringSet<> KnownTypedefs; |
| 1211 | for (const auto &Typedef : TLItems.Typedefs) { |
| 1212 | // Check for duplicate typedef definitions. |
| 1213 | if (!KnownTypedefs.insert(key: Typedef.Name).second) { |
| 1214 | emitError(Message: llvm::Twine("multiple definitions of typedef '" ) + |
| 1215 | Typedef.Name + "'" ); |
| 1216 | continue; |
| 1217 | } |
| 1218 | |
| 1219 | TypedefInfo TInfo; |
| 1220 | convertCommonType(Common: Typedef, Info&: TInfo, APIName: Typedef.Name); |
| 1221 | TInfo.SwiftWrapper = Typedef.SwiftType; |
| 1222 | |
| 1223 | Writer.addTypedef(Ctx, Name: Typedef.Name, Info: TInfo, SwiftVersion); |
| 1224 | } |
| 1225 | } |
| 1226 | |
| 1227 | bool convertModule() { |
| 1228 | // Write the top-level items. |
| 1229 | convertTopLevelItems(/* context */ Ctx: std::nullopt, TLItems: M.TopLevel, |
| 1230 | SwiftVersion: VersionTuple()); |
| 1231 | |
| 1232 | // Convert the versioned information. |
| 1233 | for (const auto &Versioned : M.SwiftVersions) |
| 1234 | convertTopLevelItems(/* context */ Ctx: std::nullopt, TLItems: Versioned.Items, |
| 1235 | SwiftVersion: Versioned.Version); |
| 1236 | |
| 1237 | if (!ErrorOccurred) |
| 1238 | Writer.writeToStream(OS); |
| 1239 | |
| 1240 | return ErrorOccurred; |
| 1241 | } |
| 1242 | }; |
| 1243 | } // namespace |
| 1244 | |
| 1245 | static bool compile(const Module &M, const FileEntry *SourceFile, |
| 1246 | llvm::raw_ostream &OS, |
| 1247 | llvm::SourceMgr::DiagHandlerTy DiagHandler, |
| 1248 | void *DiagHandlerCtxt) { |
| 1249 | YAMLConverter C(M, SourceFile, OS, DiagHandler, DiagHandlerCtxt); |
| 1250 | return C.convertModule(); |
| 1251 | } |
| 1252 | |
| 1253 | /// Simple diagnostic handler that prints diagnostics to standard error. |
| 1254 | static void printDiagnostic(const llvm::SMDiagnostic &Diag, void *Context) { |
| 1255 | Diag.print(ProgName: nullptr, S&: llvm::errs()); |
| 1256 | } |
| 1257 | |
| 1258 | bool api_notes::compileAPINotes(StringRef YAMLInput, |
| 1259 | const FileEntry *SourceFile, |
| 1260 | llvm::raw_ostream &OS, |
| 1261 | llvm::SourceMgr::DiagHandlerTy DiagHandler, |
| 1262 | void *DiagHandlerCtxt) { |
| 1263 | Module TheModule; |
| 1264 | |
| 1265 | if (!DiagHandler) |
| 1266 | DiagHandler = &printDiagnostic; |
| 1267 | |
| 1268 | if (parseAPINotes(YI: YAMLInput, M&: TheModule, Diag: DiagHandler, DiagContext: DiagHandlerCtxt)) |
| 1269 | return true; |
| 1270 | |
| 1271 | return compile(M: TheModule, SourceFile, OS, DiagHandler, DiagHandlerCtxt); |
| 1272 | } |
| 1273 | |