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