clang 24.0.0git
Types.h
Go to the documentation of this file.
1//===-- Types.h - API Notes Data Types --------------------------*- 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#ifndef LLVM_CLANG_APINOTES_TYPES_H
10#define LLVM_CLANG_APINOTES_TYPES_H
11
13#include "llvm/ADT/ArrayRef.h"
14#include "llvm/ADT/DenseMapInfo.h"
15#include "llvm/ADT/Hashing.h"
16#include "llvm/ADT/PointerEmbeddedInt.h"
17#include "llvm/ADT/SmallVector.h"
18#include "llvm/ADT/StringExtras.h"
19#include "llvm/ADT/StringRef.h"
20#include "llvm/Support/raw_ostream.h"
21#include <climits>
22#include <optional>
23#include <string>
24#include <vector>
25
26namespace llvm {
27class raw_ostream;
28} // namespace llvm
29
30namespace clang {
31namespace api_notes {
32
33template <typename RangeT>
34std::string formatAPINotesParameterSelector(RangeT &&Parameters) {
35 std::string Result;
36 llvm::raw_string_ostream OS(Result);
37 OS << "[";
38 llvm::interleaveComma(Parameters, OS);
39 OS << "]";
40 return Result;
41}
42
50
51/// The payload for an enum_extensibility attribute. This is a tri-state rather
52/// than just a boolean because the presence of the attribute indicates
53/// auditing.
59
60/// The kind of a swift_wrapper/swift_newtype.
66
68
69/// Describes API notes data for any entity.
70///
71/// This is used as the base of all API notes.
73public:
74 /// Message to use when this entity is unavailable.
75 std::string UnavailableMsg;
76
77 /// Whether this entity is marked unavailable.
78 LLVM_PREFERRED_TYPE(bool)
80
81 /// Whether this entity is marked unavailable in Swift.
82 LLVM_PREFERRED_TYPE(bool)
83 unsigned UnavailableInSwift : 1;
84
85private:
86 /// Whether SwiftPrivate was specified.
87 LLVM_PREFERRED_TYPE(bool)
88 unsigned SwiftPrivateSpecified : 1;
89
90 /// Whether this entity is considered "private" to a Swift overlay.
91 LLVM_PREFERRED_TYPE(bool)
92 unsigned SwiftPrivate : 1;
93
94 LLVM_PREFERRED_TYPE(bool)
95 unsigned SwiftSafetyAudited : 1;
96
97 LLVM_PREFERRED_TYPE(SwiftSafetyKind)
98 unsigned SwiftSafety : 2;
99
100public:
101 /// Swift name of this entity.
102 std::string SwiftName;
103
105 : Unavailable(0), UnavailableInSwift(0), SwiftPrivateSpecified(0),
106 SwiftPrivate(0), SwiftSafetyAudited(0), SwiftSafety(0) {}
107
108 std::optional<bool> isSwiftPrivate() const {
109 return SwiftPrivateSpecified ? std::optional<bool>(SwiftPrivate)
110 : std::nullopt;
111 }
112
113 void setSwiftPrivate(std::optional<bool> Private) {
114 SwiftPrivateSpecified = Private.has_value();
115 SwiftPrivate = Private.value_or(0);
116 }
117
118 std::optional<SwiftSafetyKind> getSwiftSafety() const {
119 return SwiftSafetyAudited ? std::optional<SwiftSafetyKind>(
120 static_cast<SwiftSafetyKind>(SwiftSafety))
121 : std::nullopt;
122 }
123
125 SwiftSafetyAudited = 1;
126 SwiftSafety = static_cast<unsigned>(Safety);
127 }
128
129 friend bool operator==(const CommonEntityInfo &, const CommonEntityInfo &);
130
132 // Merge unavailability.
133 if (RHS.Unavailable) {
134 Unavailable = true;
135 if (UnavailableMsg.empty())
137 }
138
139 if (RHS.UnavailableInSwift) {
140 UnavailableInSwift = true;
141 if (UnavailableMsg.empty())
143 }
144
145 if (!SwiftPrivateSpecified)
147
148 if (!SwiftSafetyAudited && RHS.SwiftSafetyAudited)
150
151 if (SwiftName.empty())
152 SwiftName = RHS.SwiftName;
153
154 return *this;
155 }
156
157 LLVM_DUMP_METHOD void dump(llvm::raw_ostream &OS) const;
158};
159
160inline bool operator==(const CommonEntityInfo &LHS,
161 const CommonEntityInfo &RHS) {
162 return LHS.UnavailableMsg == RHS.UnavailableMsg &&
163 LHS.Unavailable == RHS.Unavailable &&
165 LHS.SwiftPrivateSpecified == RHS.SwiftPrivateSpecified &&
166 LHS.SwiftPrivate == RHS.SwiftPrivate &&
167 LHS.SwiftSafetyAudited == RHS.SwiftSafetyAudited &&
168 LHS.SwiftSafety == RHS.SwiftSafety && LHS.SwiftName == RHS.SwiftName;
169}
170
171inline bool operator!=(const CommonEntityInfo &LHS,
172 const CommonEntityInfo &RHS) {
173 return !(LHS == RHS);
174}
175
176/// Describes API notes for types.
178 /// The Swift type to which a given type is bridged.
179 ///
180 /// Reflects the swift_bridge attribute.
181 std::optional<std::string> SwiftBridge;
182
183 /// The NS error domain for this type.
184 std::optional<std::string> NSErrorDomain;
185
186 /// The Swift protocol that this type should be automatically conformed to.
187 std::optional<std::string> SwiftConformance;
188
189public:
191
192 const std::optional<std::string> &getSwiftBridge() const {
193 return SwiftBridge;
194 }
195
196 void setSwiftBridge(std::optional<std::string> SwiftType) {
197 SwiftBridge = SwiftType;
198 }
199
200 const std::optional<std::string> &getNSErrorDomain() const {
201 return NSErrorDomain;
202 }
203
204 void setNSErrorDomain(const std::optional<std::string> &Domain) {
205 NSErrorDomain = Domain;
206 }
207
208 void setNSErrorDomain(const std::optional<llvm::StringRef> &Domain) {
209 NSErrorDomain = Domain ? std::optional<std::string>(std::string(*Domain))
210 : std::nullopt;
211 }
212
213 std::optional<std::string> getSwiftConformance() const {
214 return SwiftConformance;
215 }
216
217 void setSwiftConformance(std::optional<std::string> conformance) {
218 SwiftConformance = conformance;
219 }
220
221 friend bool operator==(const CommonTypeInfo &, const CommonTypeInfo &);
222
224 // Merge inherited info.
225 static_cast<CommonEntityInfo &>(*this) |= RHS;
226
227 if (!SwiftBridge)
229 if (!NSErrorDomain)
231 if (SwiftConformance)
233
234 return *this;
235 }
236
237 LLVM_DUMP_METHOD void dump(llvm::raw_ostream &OS) const;
238};
239
240inline bool operator==(const CommonTypeInfo &LHS, const CommonTypeInfo &RHS) {
241 return static_cast<const CommonEntityInfo &>(LHS) == RHS &&
242 LHS.SwiftBridge == RHS.SwiftBridge &&
243 LHS.NSErrorDomain == RHS.NSErrorDomain &&
244 LHS.SwiftConformance == RHS.SwiftConformance;
245}
246
247inline bool operator!=(const CommonTypeInfo &LHS, const CommonTypeInfo &RHS) {
248 return !(LHS == RHS);
249}
250
251/// Describes API notes data for an Objective-C class or protocol or a C++
252/// namespace.
254 /// The default nullability, if any.
255 LLVM_PREFERRED_TYPE(NullabilityKindOrNone)
256 unsigned DefaultNullabilityOrNone : 3;
257
258 /// Whether this class has designated initializers recorded.
259 LLVM_PREFERRED_TYPE(bool)
260 unsigned HasDesignatedInits : 1;
261
262 LLVM_PREFERRED_TYPE(bool)
263 unsigned SwiftImportAsNonGenericSpecified : 1;
264 LLVM_PREFERRED_TYPE(bool)
265 unsigned SwiftImportAsNonGeneric : 1;
266
267 LLVM_PREFERRED_TYPE(bool)
268 unsigned SwiftObjCMembersSpecified : 1;
269 LLVM_PREFERRED_TYPE(bool)
270 unsigned SwiftObjCMembers : 1;
271
272public:
274 : DefaultNullabilityOrNone(0), HasDesignatedInits(0),
275 SwiftImportAsNonGenericSpecified(false), SwiftImportAsNonGeneric(false),
276 SwiftObjCMembersSpecified(false), SwiftObjCMembers(false) {}
277
278 /// Determine the default nullability for properties and methods of this
279 /// class.
280 ///
281 /// Returns the default nullability, if implied, or std::nullopt if there is
282 /// none.
287
288 /// Set the default nullability for properties and methods of this class.
290 DefaultNullabilityOrNone =
292 }
293
294 bool hasDesignatedInits() const { return HasDesignatedInits; }
295 void setHasDesignatedInits(bool Value) { HasDesignatedInits = Value; }
296
297 std::optional<bool> getSwiftImportAsNonGeneric() const {
298 return SwiftImportAsNonGenericSpecified
299 ? std::optional<bool>(SwiftImportAsNonGeneric)
300 : std::nullopt;
301 }
302 void setSwiftImportAsNonGeneric(std::optional<bool> Value) {
303 SwiftImportAsNonGenericSpecified = Value.has_value();
304 SwiftImportAsNonGeneric = Value.value_or(false);
305 }
306
307 std::optional<bool> getSwiftObjCMembers() const {
308 return SwiftObjCMembersSpecified ? std::optional<bool>(SwiftObjCMembers)
309 : std::nullopt;
310 }
311 void setSwiftObjCMembers(std::optional<bool> Value) {
312 SwiftObjCMembersSpecified = Value.has_value();
313 SwiftObjCMembers = Value.value_or(false);
314 }
315
316 friend bool operator==(const ContextInfo &, const ContextInfo &);
317
319 // Merge inherited info.
320 static_cast<CommonTypeInfo &>(*this) |= RHS;
321
322 // Merge nullability.
324 if (auto Nullability = RHS.getDefaultNullability())
325 setDefaultNullability(*Nullability);
326
327 if (!SwiftImportAsNonGenericSpecified)
329
330 if (!SwiftObjCMembersSpecified)
332
333 HasDesignatedInits |= RHS.HasDesignatedInits;
334
335 return *this;
336 }
337
338 LLVM_DUMP_METHOD void dump(llvm::raw_ostream &OS);
339};
340
341inline bool operator==(const ContextInfo &LHS, const ContextInfo &RHS) {
342 return static_cast<const CommonTypeInfo &>(LHS) == RHS &&
344 LHS.HasDesignatedInits == RHS.HasDesignatedInits &&
347}
348
349inline bool operator!=(const ContextInfo &LHS, const ContextInfo &RHS) {
350 return !(LHS == RHS);
351}
352
354public:
362
363private:
364 /// The kind of bounds safety for this property. Only valid if the bounds
365 /// safety has been audited.
366 LLVM_PREFERRED_TYPE(BoundsSafetyKind)
367 unsigned Kind : 3;
368
369 /// Whether the bounds safety kind has been audited.
370 LLVM_PREFERRED_TYPE(bool)
371 unsigned KindAudited : 1;
372
373 /// The pointer indirection level at which the bounds annotation applies.
374 /// Only valid if LevelAudited is set.
375 unsigned Level : 3;
376
377 /// Whether the pointer indirection level has been specified.
378 LLVM_PREFERRED_TYPE(bool)
379 unsigned LevelAudited : 1;
380
381public:
382 std::string ExternalBounds;
383
385 : Kind(0), KindAudited(false), Level(0), LevelAudited(false),
386 ExternalBounds("") {}
387
388 std::optional<BoundsSafetyKind> getKind() const {
389 return KindAudited ? std::optional<BoundsSafetyKind>(
390 static_cast<BoundsSafetyKind>(Kind))
391 : std::nullopt;
392 }
393
395 KindAudited = true;
396 Kind = static_cast<unsigned>(kind);
397 }
398
399 std::optional<unsigned> getLevel() const {
400 return LevelAudited ? std::optional<unsigned>(Level) : std::nullopt;
401 }
402
403 void setLevelAudited(unsigned level) {
404 LevelAudited = true;
405 Level = level;
406 }
407
408 friend bool operator==(const BoundsSafetyInfo &, const BoundsSafetyInfo &);
409
410 LLVM_DUMP_METHOD void dump(llvm::raw_ostream &OS) const;
411};
412
413inline bool operator==(const BoundsSafetyInfo &LHS,
414 const BoundsSafetyInfo &RHS) {
415 return LHS.KindAudited == RHS.KindAudited && LHS.Kind == RHS.Kind &&
416 LHS.LevelAudited == RHS.LevelAudited && LHS.Level == RHS.Level &&
418}
419
420inline bool operator!=(const BoundsSafetyInfo &LHS,
421 const BoundsSafetyInfo &RHS) {
422 return !(LHS == RHS);
423}
424
425/// API notes for a variable/property.
427 /// The kind of nullability for this property, if the nullability
428 /// has been audited.
429 LLVM_PREFERRED_TYPE(NullabilityKindOrNone)
430 unsigned NullabilityOrNone : 3;
431
432 /// The C type of the variable, as a string.
433 std::string Type;
434
435public:
436 VariableInfo() : NullabilityOrNone(0) {}
437
441
445
446 const std::string &getType() const { return Type; }
447 void setType(const std::string &type) { Type = type; }
448
449 friend bool operator==(const VariableInfo &, const VariableInfo &);
450
452 static_cast<CommonEntityInfo &>(*this) |= RHS;
453
454 if (!getNullability() && RHS.getNullability())
456 if (Type.empty())
457 Type = RHS.Type;
458
459 return *this;
460 }
461
462 LLVM_DUMP_METHOD void dump(llvm::raw_ostream &OS) const;
463};
464
465inline bool operator==(const VariableInfo &LHS, const VariableInfo &RHS) {
466 return static_cast<const CommonEntityInfo &>(LHS) == RHS &&
467 LHS.NullabilityOrNone == RHS.NullabilityOrNone && LHS.Type == RHS.Type;
468}
469
470inline bool operator!=(const VariableInfo &LHS, const VariableInfo &RHS) {
471 return !(LHS == RHS);
472}
473
474/// Describes API notes data for an Objective-C property.
476 LLVM_PREFERRED_TYPE(bool)
477 unsigned SwiftImportAsAccessorsSpecified : 1;
478 LLVM_PREFERRED_TYPE(bool)
479 unsigned SwiftImportAsAccessors : 1;
480
481public:
483 : SwiftImportAsAccessorsSpecified(false), SwiftImportAsAccessors(false) {}
484
485 std::optional<bool> getSwiftImportAsAccessors() const {
486 return SwiftImportAsAccessorsSpecified
487 ? std::optional<bool>(SwiftImportAsAccessors)
488 : std::nullopt;
489 }
490 void setSwiftImportAsAccessors(std::optional<bool> Value) {
491 SwiftImportAsAccessorsSpecified = Value.has_value();
492 SwiftImportAsAccessors = Value.value_or(false);
493 }
494
495 friend bool operator==(const ObjCPropertyInfo &, const ObjCPropertyInfo &);
496
497 /// Merge class-wide information into the given property.
499 static_cast<CommonEntityInfo &>(*this) |= RHS;
500
501 // Merge nullability.
502 if (!getNullability())
503 if (auto Nullable = RHS.getDefaultNullability())
505
506 return *this;
507 }
508
510 static_cast<VariableInfo &>(*this) |= RHS;
511
512 if (!SwiftImportAsAccessorsSpecified)
514
515 return *this;
516 }
517
518 LLVM_DUMP_METHOD void dump(llvm::raw_ostream &OS) const;
519};
520
521inline bool operator==(const ObjCPropertyInfo &LHS,
522 const ObjCPropertyInfo &RHS) {
523 return static_cast<const VariableInfo &>(LHS) == RHS &&
525}
526
527inline bool operator!=(const ObjCPropertyInfo &LHS,
528 const ObjCPropertyInfo &RHS) {
529 return !(LHS == RHS);
530}
531
532/// Describes a function or method parameter.
533class ParamInfo : public VariableInfo {
534 /// Whether noescape was specified.
535 LLVM_PREFERRED_TYPE(bool)
536 unsigned NoEscapeSpecified : 1;
537
538 /// Whether the this parameter has the 'noescape' attribute.
539 LLVM_PREFERRED_TYPE(bool)
540 unsigned NoEscape : 1;
541
542 /// Whether lifetimebound was specified.
543 LLVM_PREFERRED_TYPE(bool)
544 unsigned LifetimeboundSpecified : 1;
545
546 /// Whether the this parameter has the 'lifetimebound' attribute.
547 LLVM_PREFERRED_TYPE(bool)
548 unsigned Lifetimebound : 1;
549
550 /// A biased RetainCountConventionKind, where 0 means "unspecified".
551 ///
552 /// Only relevant for out-parameters.
553 unsigned RawRetainCountConvention : 3;
554
555public:
556 std::optional<BoundsSafetyInfo> BoundsSafety;
557
559 : NoEscapeSpecified(false), NoEscape(false),
560 LifetimeboundSpecified(false), Lifetimebound(false),
561 RawRetainCountConvention(), BoundsSafety(std::nullopt) {}
562
563 std::optional<bool> isNoEscape() const {
564 return NoEscapeSpecified ? std::optional<bool>(NoEscape) : std::nullopt;
565 }
566 void setNoEscape(std::optional<bool> Value) {
567 NoEscapeSpecified = Value.has_value();
568 NoEscape = Value.value_or(false);
569 }
570
571 std::optional<bool> isLifetimebound() const {
572 return LifetimeboundSpecified ? std::optional<bool>(Lifetimebound)
573 : std::nullopt;
574 }
575 void setLifetimebound(std::optional<bool> Value) {
576 LifetimeboundSpecified = Value.has_value();
577 Lifetimebound = Value.value_or(false);
578 }
579
580 std::optional<RetainCountConventionKind> getRetainCountConvention() const {
581 if (!RawRetainCountConvention)
582 return std::nullopt;
583 return static_cast<RetainCountConventionKind>(RawRetainCountConvention - 1);
584 }
585 void
586 setRetainCountConvention(std::optional<RetainCountConventionKind> Value) {
587 RawRetainCountConvention = Value ? static_cast<unsigned>(*Value) + 1 : 0;
588 assert(getRetainCountConvention() == Value && "bitfield too small");
589 }
590
592 static_cast<VariableInfo &>(*this) |= RHS;
593
594 if (!NoEscapeSpecified && RHS.NoEscapeSpecified) {
595 NoEscapeSpecified = true;
596 NoEscape = RHS.NoEscape;
597 }
598
599 if (!LifetimeboundSpecified && RHS.LifetimeboundSpecified) {
600 LifetimeboundSpecified = true;
601 Lifetimebound = RHS.Lifetimebound;
602 }
603
604 if (!RawRetainCountConvention)
605 RawRetainCountConvention = RHS.RawRetainCountConvention;
606
607 if (!BoundsSafety)
609
610 return *this;
611 }
612
613 friend bool operator==(const ParamInfo &, const ParamInfo &);
614
615 LLVM_DUMP_METHOD void dump(llvm::raw_ostream &OS) const;
616};
617
618inline bool operator==(const ParamInfo &LHS, const ParamInfo &RHS) {
619 return static_cast<const VariableInfo &>(LHS) == RHS &&
620 LHS.NoEscapeSpecified == RHS.NoEscapeSpecified &&
621 LHS.NoEscape == RHS.NoEscape &&
622 LHS.LifetimeboundSpecified == RHS.LifetimeboundSpecified &&
623 LHS.Lifetimebound == RHS.Lifetimebound &&
624 LHS.RawRetainCountConvention == RHS.RawRetainCountConvention &&
625 LHS.BoundsSafety == RHS.BoundsSafety;
626}
627
628inline bool operator!=(const ParamInfo &LHS, const ParamInfo &RHS) {
629 return !(LHS == RHS);
630}
631
632/// API notes for a function or method.
634private:
635 static constexpr const uint64_t NullabilityKindMask = 0x3;
636 static constexpr const unsigned NullabilityKindSize = 2;
637
638 static constexpr const unsigned ReturnInfoIndex = 0;
639
640public:
641 // If yes, we consider all types to be non-nullable unless otherwise noted.
642 // If this flag is not set, the pointer types are considered to have
643 // unknown nullability.
644
645 /// Whether the signature has been audited with respect to nullability.
646 LLVM_PREFERRED_TYPE(bool)
648
649 /// Number of types whose nullability is encoded with the NullabilityPayload.
651
652 /// A biased RetainCountConventionKind, where 0 means "unspecified".
654
655 /// Whether the function has the [[clang::unsafe_buffer_usage]] attribute
656 LLVM_PREFERRED_TYPE(bool)
657 unsigned UnsafeBufferUsage : 1;
658
659 // NullabilityKindSize bits are used to encode the nullability. The info
660 // about the return type is stored at position 0, followed by the nullability
661 // of the parameters.
662
663 /// Stores the nullability of the return type and the parameters.
664 uint64_t NullabilityPayload = 0;
665
666 /// The result type of this function, as a C type.
668
669 /// Ownership convention for return value
671
672 /// The function parameters.
674
678
679 static unsigned getMaxNullabilityIndex() {
680 return ((sizeof(NullabilityPayload) * CHAR_BIT) / NullabilityKindSize);
681 }
682
684 assert(index <= getMaxNullabilityIndex());
685 assert(static_cast<unsigned>(kind) < NullabilityKindMask);
686
687 NullabilityAudited = true;
688 if (NumAdjustedNullable < index + 1)
690
691 // Mask the bits.
693 ~(NullabilityKindMask << (index * NullabilityKindSize));
694
695 // Set the value.
696 unsigned kindValue = (static_cast<unsigned>(kind))
697 << (index * NullabilityKindSize);
698 NullabilityPayload |= kindValue;
699 }
700
701 /// Adds the return type info.
703 addTypeInfo(ReturnInfoIndex, kind);
704 }
705
706 /// Adds the parameter type info.
708 addTypeInfo(index + 1, kind);
709 }
710
712 return getTypeInfo(index + 1);
713 }
714
715 NullabilityKind getReturnTypeInfo() const { return getTypeInfo(0); }
716
717 std::optional<RetainCountConventionKind> getRetainCountConvention() const {
719 return std::nullopt;
721 }
722 void
723 setRetainCountConvention(std::optional<RetainCountConventionKind> Value) {
724 RawRetainCountConvention = Value ? static_cast<unsigned>(*Value) + 1 : 0;
725 assert(getRetainCountConvention() == Value && "bitfield too small");
726 }
727
728 friend bool operator==(const FunctionInfo &, const FunctionInfo &);
729
730private:
731 NullabilityKind getTypeInfo(unsigned index) const {
732 assert(NullabilityAudited &&
733 "Checking the type adjustment on non-audited method.");
734
735 // If we don't have info about this parameter, return the default.
738 auto nullability = NullabilityPayload >> (index * NullabilityKindSize);
739 return static_cast<NullabilityKind>(nullability & NullabilityKindMask);
740 }
741
742public:
743 LLVM_DUMP_METHOD void dump(llvm::raw_ostream &OS) const;
744};
745
746inline bool operator==(const FunctionInfo &LHS, const FunctionInfo &RHS) {
747 return static_cast<const CommonEntityInfo &>(LHS) == RHS &&
752 LHS.ResultType == RHS.ResultType && LHS.Params == RHS.Params &&
755}
756
757inline bool operator!=(const FunctionInfo &LHS, const FunctionInfo &RHS) {
758 return !(LHS == RHS);
759}
760
761/// Describes API notes data for an Objective-C method.
763public:
764 /// Whether this is a designated initializer of its class.
765 LLVM_PREFERRED_TYPE(bool)
767
768 /// Whether this is a required initializer.
769 LLVM_PREFERRED_TYPE(bool)
770 unsigned RequiredInit : 1;
771
772 std::optional<ParamInfo> Self;
773
775
776 friend bool operator==(const ObjCMethodInfo &, const ObjCMethodInfo &);
777
779 // Merge Nullability.
780 if (!NullabilityAudited) {
781 if (auto Nullable = RHS.getDefaultNullability()) {
782 NullabilityAudited = true;
784 }
785 }
786 return *this;
787 }
788
789 LLVM_DUMP_METHOD void dump(llvm::raw_ostream &OS);
790};
791
792inline bool operator==(const ObjCMethodInfo &LHS, const ObjCMethodInfo &RHS) {
793 return static_cast<const FunctionInfo &>(LHS) == RHS &&
794 LHS.DesignatedInit == RHS.DesignatedInit &&
795 LHS.RequiredInit == RHS.RequiredInit && LHS.Self == RHS.Self;
796}
797
798inline bool operator!=(const ObjCMethodInfo &LHS, const ObjCMethodInfo &RHS) {
799 return !(LHS == RHS);
800}
801
802/// Describes API notes data for a global variable.
804public:
806};
807
808/// Describes API notes data for a global function.
810public:
812};
813
814/// Describes API notes data for a C/C++ record field.
815class FieldInfo : public VariableInfo {
816public:
818};
819
820/// Describes API notes data for a C++ method.
822public:
824
825 std::optional<ParamInfo> This;
826
827 LLVM_DUMP_METHOD void dump(llvm::raw_ostream &OS);
828};
829
830inline bool operator==(const CXXMethodInfo &LHS, const CXXMethodInfo &RHS) {
831 return static_cast<const FunctionInfo &>(LHS) == RHS && LHS.This == RHS.This;
832}
833
834inline bool operator!=(const CXXMethodInfo &LHS, const CXXMethodInfo &RHS) {
835 return !(LHS == RHS);
836}
837
838/// Describes API notes data for an enumerator.
840public:
842};
843
844/// Describes API notes data for a tag.
845class TagInfo : public CommonTypeInfo {
846 LLVM_PREFERRED_TYPE(bool)
847 unsigned HasFlagEnum : 1;
848 LLVM_PREFERRED_TYPE(bool)
849 unsigned IsFlagEnum : 1;
850
851 LLVM_PREFERRED_TYPE(bool)
852 unsigned SwiftCopyableSpecified : 1;
853 LLVM_PREFERRED_TYPE(bool)
854 unsigned SwiftCopyable : 1;
855
856 LLVM_PREFERRED_TYPE(bool)
857 unsigned SwiftEscapableSpecified : 1;
858 LLVM_PREFERRED_TYPE(bool)
859 unsigned SwiftEscapable : 1;
860
861public:
862 std::optional<std::string> SwiftImportAs;
863 std::optional<std::string> SwiftRetainOp;
864 std::optional<std::string> SwiftReleaseOp;
865 std::optional<std::string> SwiftDestroyOp;
866 std::optional<std::string> SwiftDefaultOwnership;
867
868 std::optional<EnumExtensibilityKind> EnumExtensibility;
869
871 : HasFlagEnum(0), IsFlagEnum(0), SwiftCopyableSpecified(false),
872 SwiftCopyable(false), SwiftEscapableSpecified(false),
873 SwiftEscapable(false) {}
874
875 std::optional<bool> isFlagEnum() const {
876 if (HasFlagEnum)
877 return IsFlagEnum;
878 return std::nullopt;
879 }
880 void setFlagEnum(std::optional<bool> Value) {
881 HasFlagEnum = Value.has_value();
882 IsFlagEnum = Value.value_or(false);
883 }
884
885 std::optional<bool> isSwiftCopyable() const {
886 return SwiftCopyableSpecified ? std::optional<bool>(SwiftCopyable)
887 : std::nullopt;
888 }
889 void setSwiftCopyable(std::optional<bool> Value) {
890 SwiftCopyableSpecified = Value.has_value();
891 SwiftCopyable = Value.value_or(false);
892 }
893
894 std::optional<bool> isSwiftEscapable() const {
895 return SwiftEscapableSpecified ? std::optional<bool>(SwiftEscapable)
896 : std::nullopt;
897 }
898
899 void setSwiftEscapable(std::optional<bool> Value) {
900 SwiftEscapableSpecified = Value.has_value();
901 SwiftEscapable = Value.value_or(false);
902 }
903
905 static_cast<CommonTypeInfo &>(*this) |= RHS;
906
907 if (!SwiftImportAs)
909 if (!SwiftRetainOp)
911 if (!SwiftReleaseOp)
913 if (!SwiftDestroyOp)
917
918 if (!HasFlagEnum)
919 setFlagEnum(RHS.isFlagEnum());
920
923
924 if (!SwiftCopyableSpecified)
926
927 if (!SwiftEscapableSpecified)
929
930 return *this;
931 }
932
933 friend bool operator==(const TagInfo &, const TagInfo &);
934
935 LLVM_DUMP_METHOD void dump(llvm::raw_ostream &OS);
936};
937
938inline bool operator==(const TagInfo &LHS, const TagInfo &RHS) {
939 return static_cast<const CommonTypeInfo &>(LHS) == RHS &&
940 LHS.SwiftImportAs == RHS.SwiftImportAs &&
941 LHS.SwiftRetainOp == RHS.SwiftRetainOp &&
942 LHS.SwiftReleaseOp == RHS.SwiftReleaseOp &&
943 LHS.SwiftDestroyOp == RHS.SwiftDestroyOp &&
945 LHS.isFlagEnum() == RHS.isFlagEnum() &&
946 LHS.isSwiftCopyable() == RHS.isSwiftCopyable() &&
947 LHS.isSwiftEscapable() == RHS.isSwiftEscapable() &&
949}
950
951inline bool operator!=(const TagInfo &LHS, const TagInfo &RHS) {
952 return !(LHS == RHS);
953}
954
955/// Describes API notes data for a typedef.
957public:
958 std::optional<SwiftNewTypeKind> SwiftWrapper;
959
961
963 static_cast<CommonTypeInfo &>(*this) |= RHS;
964 if (!SwiftWrapper)
966 return *this;
967 }
968
969 friend bool operator==(const TypedefInfo &, const TypedefInfo &);
970
971 LLVM_DUMP_METHOD void dump(llvm::raw_ostream &OS) const;
972};
973
974inline bool operator==(const TypedefInfo &LHS, const TypedefInfo &RHS) {
975 return static_cast<const CommonTypeInfo &>(LHS) == RHS &&
976 LHS.SwiftWrapper == RHS.SwiftWrapper;
977}
978
979inline bool operator!=(const TypedefInfo &LHS, const TypedefInfo &RHS) {
980 return !(LHS == RHS);
981}
982
983/// The file extension used for the source representation of API notes.
984static const constexpr char SOURCE_APINOTES_EXTENSION[] = "apinotes";
985
986/// Opaque context ID used to refer to an Objective-C class or protocol or a C++
987/// namespace.
989public:
990 unsigned Value;
991
992 explicit ContextID(unsigned value) : Value(value) {}
993};
994
995enum class ContextKind : uint8_t {
999 Tag = 3,
1000};
1001
1008
1009using IdentifierID = llvm::PointerEmbeddedInt<unsigned, 31>;
1010
1011/// A key for a stored global-function or C++-method API notes entry.
1012///
1013/// The key is represented by the ID of its parent context, the declaration
1014/// name, and optional exact parameter types.
1018 std::optional<llvm::SmallVector<IdentifierID, 2>> parameterTypeIDs;
1019
1021
1022 FunctionTableKey(uint32_t ParentContextID, uint32_t NameID)
1023 : parentContextID(ParentContextID), nameID(NameID) {}
1024
1025 FunctionTableKey(uint32_t ParentContextID, uint32_t NameID,
1026 const llvm::SmallVectorImpl<IdentifierID> &ParameterTypeIDs)
1027 : parentContextID(ParentContextID), nameID(NameID) {
1028 parameterTypeIDs.emplace(ParameterTypeIDs.begin(), ParameterTypeIDs.end());
1029 }
1030
1031 FunctionTableKey(std::optional<Context> ParentCtx, IdentifierID NameID)
1032 : parentContextID(ParentCtx ? ParentCtx->id.Value
1033 : static_cast<uint32_t>(-1)),
1034 nameID(NameID) {}
1035
1036 FunctionTableKey(std::optional<Context> ParentCtx, IdentifierID NameID,
1037 const llvm::SmallVectorImpl<IdentifierID> &ParameterTypeIDs)
1038 : parentContextID(ParentCtx ? ParentCtx->id.Value
1039 : static_cast<uint32_t>(-1)),
1040 nameID(NameID) {
1041 parameterTypeIDs.emplace(ParameterTypeIDs.begin(), ParameterTypeIDs.end());
1042 }
1043
1044 llvm::hash_code hashValue() const {
1045 auto Hash = llvm::hash_combine(parentContextID, nameID,
1046 static_cast<bool>(parameterTypeIDs));
1047 if (parameterTypeIDs) {
1048 Hash = llvm::hash_combine(Hash, parameterTypeIDs->size());
1049 for (IdentifierID TypeID : *parameterTypeIDs)
1050 Hash = llvm::hash_combine(Hash, static_cast<unsigned>(TypeID));
1051 }
1052 return Hash;
1053 }
1054};
1055
1056inline bool operator==(const FunctionTableKey &LHS,
1057 const FunctionTableKey &RHS) {
1058 return LHS.parentContextID == RHS.parentContextID &&
1059 LHS.nameID == RHS.nameID &&
1061}
1062
1063/// Stable reader-facing identity for an API notes function selector entry.
1064///
1065/// The key keeps the serialized function table identity together with the table
1066/// kind. parentContextID names only the declaration context and does not say
1067/// whether the entry came from the global-function or C++-method table.
1070 bool IsCXXMethod = false;
1071
1073 return {FunctionTableKey(Key.parentContextID, Key.nameID), IsCXXMethod};
1074 }
1075
1076 llvm::hash_code hashValue() const {
1077 return llvm::hash_combine(Key.hashValue(), IsCXXMethod);
1078 }
1079};
1080
1082 const APINotesFunctionSelectorKey &RHS) {
1083 return LHS.Key == RHS.Key && LHS.IsCXXMethod == RHS.IsCXXMethod;
1084}
1085
1086/// A temporary reference to an Objective-C selector, suitable for
1087/// referencing selector data on the stack.
1088///
1089/// Instances of this struct do not store references to any of the
1090/// data they contain; it is up to the user to ensure that the data
1091/// referenced by the identifier list persists.
1096} // namespace api_notes
1097} // namespace clang
1098
1099namespace llvm {
1100template <> struct DenseMapInfo<clang::api_notes::FunctionTableKey> {
1102 return Key.hashValue();
1103 }
1104
1107 return LHS == RHS;
1108 }
1109};
1110
1111template <> struct DenseMapInfo<clang::api_notes::APINotesFunctionSelectorKey> {
1112 static unsigned
1116
1117 static bool
1122};
1123} // namespace llvm
1124
1125#endif
*collection of selector each with an associated kind and an ordered *collection of selectors A selector has a kind
Defines various enumerations that describe declaration and type specifiers.
std::optional< BoundsSafetyKind > getKind() const
Definition Types.h:388
LLVM_DUMP_METHOD void dump(llvm::raw_ostream &OS) const
std::optional< unsigned > getLevel() const
Definition Types.h:399
friend bool operator==(const BoundsSafetyInfo &, const BoundsSafetyInfo &)
Definition Types.h:413
void setKindAudited(BoundsSafetyKind kind)
Definition Types.h:394
void setLevelAudited(unsigned level)
Definition Types.h:403
Describes API notes data for a C++ method.
Definition Types.h:821
std::optional< ParamInfo > This
Definition Types.h:825
LLVM_DUMP_METHOD void dump(llvm::raw_ostream &OS)
Describes API notes data for any entity.
Definition Types.h:72
unsigned UnavailableInSwift
Whether this entity is marked unavailable in Swift.
Definition Types.h:83
unsigned Unavailable
Whether this entity is marked unavailable.
Definition Types.h:79
std::string SwiftName
Swift name of this entity.
Definition Types.h:102
void setSwiftSafety(SwiftSafetyKind Safety)
Definition Types.h:124
void setSwiftPrivate(std::optional< bool > Private)
Definition Types.h:113
friend bool operator==(const CommonEntityInfo &, const CommonEntityInfo &)
Definition Types.h:160
std::string UnavailableMsg
Message to use when this entity is unavailable.
Definition Types.h:75
std::optional< SwiftSafetyKind > getSwiftSafety() const
Definition Types.h:118
LLVM_DUMP_METHOD void dump(llvm::raw_ostream &OS) const
std::optional< bool > isSwiftPrivate() const
Definition Types.h:108
CommonEntityInfo & operator|=(const CommonEntityInfo &RHS)
Definition Types.h:131
Describes API notes for types.
Definition Types.h:177
void setNSErrorDomain(const std::optional< llvm::StringRef > &Domain)
Definition Types.h:208
friend bool operator==(const CommonTypeInfo &, const CommonTypeInfo &)
Definition Types.h:240
std::optional< std::string > getSwiftConformance() const
Definition Types.h:213
const std::optional< std::string > & getSwiftBridge() const
Definition Types.h:192
void setSwiftConformance(std::optional< std::string > conformance)
Definition Types.h:217
LLVM_DUMP_METHOD void dump(llvm::raw_ostream &OS) const
void setNSErrorDomain(const std::optional< std::string > &Domain)
Definition Types.h:204
const std::optional< std::string > & getNSErrorDomain() const
Definition Types.h:200
void setSwiftBridge(std::optional< std::string > SwiftType)
Definition Types.h:196
CommonTypeInfo & operator|=(const CommonTypeInfo &RHS)
Definition Types.h:223
Opaque context ID used to refer to an Objective-C class or protocol or a C++ namespace.
Definition Types.h:988
ContextID(unsigned value)
Definition Types.h:992
Describes API notes data for an Objective-C class or protocol or a C++ namespace.
Definition Types.h:253
std::optional< bool > getSwiftImportAsNonGeneric() const
Definition Types.h:297
LLVM_DUMP_METHOD void dump(llvm::raw_ostream &OS)
std::optional< bool > getSwiftObjCMembers() const
Definition Types.h:307
void setDefaultNullability(NullabilityKind Kind)
Set the default nullability for properties and methods of this class.
Definition Types.h:289
NullabilityKindOrNone getDefaultNullability() const
Determine the default nullability for properties and methods of this class.
Definition Types.h:283
void setSwiftObjCMembers(std::optional< bool > Value)
Definition Types.h:311
bool hasDesignatedInits() const
Definition Types.h:294
void setSwiftImportAsNonGeneric(std::optional< bool > Value)
Definition Types.h:302
void setHasDesignatedInits(bool Value)
Definition Types.h:295
friend bool operator==(const ContextInfo &, const ContextInfo &)
Definition Types.h:341
ContextInfo & operator|=(const ContextInfo &RHS)
Definition Types.h:318
API notes for a function or method.
Definition Types.h:633
std::string SwiftReturnOwnership
Ownership convention for return value.
Definition Types.h:670
void addTypeInfo(unsigned index, NullabilityKind kind)
Definition Types.h:683
uint64_t NullabilityPayload
Stores the nullability of the return type and the parameters.
Definition Types.h:664
std::optional< RetainCountConventionKind > getRetainCountConvention() const
Definition Types.h:717
LLVM_DUMP_METHOD void dump(llvm::raw_ostream &OS) const
void setRetainCountConvention(std::optional< RetainCountConventionKind > Value)
Definition Types.h:723
unsigned RawRetainCountConvention
A biased RetainCountConventionKind, where 0 means "unspecified".
Definition Types.h:653
std::vector< ParamInfo > Params
The function parameters.
Definition Types.h:673
NullabilityKind getReturnTypeInfo() const
Definition Types.h:715
friend bool operator==(const FunctionInfo &, const FunctionInfo &)
Definition Types.h:746
NullabilityKind getParamTypeInfo(unsigned index) const
Definition Types.h:711
unsigned NumAdjustedNullable
Number of types whose nullability is encoded with the NullabilityPayload.
Definition Types.h:650
std::string ResultType
The result type of this function, as a C type.
Definition Types.h:667
unsigned UnsafeBufferUsage
Whether the function has the [[clang::unsafe_buffer_usage]] attribute.
Definition Types.h:657
static unsigned getMaxNullabilityIndex()
Definition Types.h:679
void addReturnTypeInfo(NullabilityKind kind)
Adds the return type info.
Definition Types.h:702
unsigned NullabilityAudited
Whether the signature has been audited with respect to nullability.
Definition Types.h:647
void addParamTypeInfo(unsigned index, NullabilityKind kind)
Adds the parameter type info.
Definition Types.h:707
Describes API notes data for an Objective-C method.
Definition Types.h:762
unsigned DesignatedInit
Whether this is a designated initializer of its class.
Definition Types.h:766
friend bool operator==(const ObjCMethodInfo &, const ObjCMethodInfo &)
Definition Types.h:792
std::optional< ParamInfo > Self
Definition Types.h:772
ObjCMethodInfo & operator|=(const ContextInfo &RHS)
Definition Types.h:778
unsigned RequiredInit
Whether this is a required initializer.
Definition Types.h:770
LLVM_DUMP_METHOD void dump(llvm::raw_ostream &OS)
Describes API notes data for an Objective-C property.
Definition Types.h:475
LLVM_DUMP_METHOD void dump(llvm::raw_ostream &OS) const
void setSwiftImportAsAccessors(std::optional< bool > Value)
Definition Types.h:490
std::optional< bool > getSwiftImportAsAccessors() const
Definition Types.h:485
friend bool operator==(const ObjCPropertyInfo &, const ObjCPropertyInfo &)
Definition Types.h:521
ObjCPropertyInfo & operator|=(const ObjCPropertyInfo &RHS)
Definition Types.h:509
ObjCPropertyInfo & operator|=(const ContextInfo &RHS)
Merge class-wide information into the given property.
Definition Types.h:498
Describes a function or method parameter.
Definition Types.h:533
std::optional< BoundsSafetyInfo > BoundsSafety
Definition Types.h:556
void setNoEscape(std::optional< bool > Value)
Definition Types.h:566
std::optional< bool > isNoEscape() const
Definition Types.h:563
ParamInfo & operator|=(const ParamInfo &RHS)
Definition Types.h:591
LLVM_DUMP_METHOD void dump(llvm::raw_ostream &OS) const
std::optional< bool > isLifetimebound() const
Definition Types.h:571
friend bool operator==(const ParamInfo &, const ParamInfo &)
Definition Types.h:618
void setLifetimebound(std::optional< bool > Value)
Definition Types.h:575
std::optional< RetainCountConventionKind > getRetainCountConvention() const
Definition Types.h:580
void setRetainCountConvention(std::optional< RetainCountConventionKind > Value)
Definition Types.h:586
Describes API notes data for a tag.
Definition Types.h:845
std::optional< std::string > SwiftReleaseOp
Definition Types.h:864
std::optional< std::string > SwiftRetainOp
Definition Types.h:863
std::optional< std::string > SwiftImportAs
Definition Types.h:862
std::optional< std::string > SwiftDefaultOwnership
Definition Types.h:866
std::optional< EnumExtensibilityKind > EnumExtensibility
Definition Types.h:868
void setSwiftCopyable(std::optional< bool > Value)
Definition Types.h:889
std::optional< std::string > SwiftDestroyOp
Definition Types.h:865
void setSwiftEscapable(std::optional< bool > Value)
Definition Types.h:899
std::optional< bool > isFlagEnum() const
Definition Types.h:875
TagInfo & operator|=(const TagInfo &RHS)
Definition Types.h:904
std::optional< bool > isSwiftCopyable() const
Definition Types.h:885
LLVM_DUMP_METHOD void dump(llvm::raw_ostream &OS)
friend bool operator==(const TagInfo &, const TagInfo &)
Definition Types.h:938
std::optional< bool > isSwiftEscapable() const
Definition Types.h:894
void setFlagEnum(std::optional< bool > Value)
Definition Types.h:880
Describes API notes data for a typedef.
Definition Types.h:956
LLVM_DUMP_METHOD void dump(llvm::raw_ostream &OS) const
TypedefInfo & operator|=(const TypedefInfo &RHS)
Definition Types.h:962
friend bool operator==(const TypedefInfo &, const TypedefInfo &)
Definition Types.h:974
std::optional< SwiftNewTypeKind > SwiftWrapper
Definition Types.h:958
API notes for a variable/property.
Definition Types.h:426
void setNullabilityAudited(NullabilityKind kind)
Definition Types.h:442
void setType(const std::string &type)
Definition Types.h:447
LLVM_DUMP_METHOD void dump(llvm::raw_ostream &OS) const
VariableInfo & operator|=(const VariableInfo &RHS)
Definition Types.h:451
const std::string & getType() const
Definition Types.h:446
friend bool operator==(const VariableInfo &, const VariableInfo &)
Definition Types.h:465
NullabilityKindOrNone getNullability() const
Definition Types.h:438
#define CHAR_BIT
Definition limits.h:71
bool operator!=(const CommonEntityInfo &LHS, const CommonEntityInfo &RHS)
Definition Types.h:171
bool operator==(const CommonEntityInfo &LHS, const CommonEntityInfo &RHS)
Definition Types.h:160
RetainCountConventionKind
Definition Types.h:43
llvm::PointerEmbeddedInt< unsigned, 31 > IdentifierID
Definition Types.h:1009
SwiftNewTypeKind
The kind of a swift_wrapper/swift_newtype.
Definition Types.h:61
EnumExtensibilityKind
The payload for an enum_extensibility attribute.
Definition Types.h:54
std::string formatAPINotesParameterSelector(RangeT &&Parameters)
Definition Types.h:34
static const constexpr char SOURCE_APINOTES_EXTENSION[]
The file extension used for the source representation of API notes.
Definition Types.h:984
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
Top level wrappers for InstallAPI frontend operations.
NullabilityKind
Describes the nullability of a particular type.
Definition Specifiers.h:349
@ Nullable
Values of this type can be null.
Definition Specifiers.h:353
@ NonNull
Values of this type can never be null.
Definition Specifiers.h:351
@ Private
'private' clause, allowed on 'parallel', 'serial', 'loop', 'parallel loop', and 'serial loop' constru...
@ Result
The result type of a method or function.
Definition TypeBase.h:906
OptionalUnsigned< NullabilityKind > NullabilityKindOrNone
Definition Specifiers.h:365
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30
__packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 __packed_splat2 uint8_t
__packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 __packed_splat2 __packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 uint32_t
#define false
Definition stdbool.h:26
constexpr underlying_type toInternalRepresentation() const
static constexpr OptionalUnsigned fromInternalRepresentation(underlying_type Rep)
Stable reader-facing identity for an API notes function selector entry.
Definition Types.h:1068
APINotesFunctionSelectorKey getWithoutParameterSelector() const
Definition Types.h:1072
Context(ContextID id, ContextKind kind)
Definition Types.h:1006
A key for a stored global-function or C++-method API notes entry.
Definition Types.h:1015
FunctionTableKey(std::optional< Context > ParentCtx, IdentifierID NameID)
Definition Types.h:1031
std::optional< llvm::SmallVector< IdentifierID, 2 > > parameterTypeIDs
Definition Types.h:1018
llvm::hash_code hashValue() const
Definition Types.h:1044
FunctionTableKey(uint32_t ParentContextID, uint32_t NameID)
Definition Types.h:1022
FunctionTableKey(uint32_t ParentContextID, uint32_t NameID, const llvm::SmallVectorImpl< IdentifierID > &ParameterTypeIDs)
Definition Types.h:1025
FunctionTableKey(std::optional< Context > ParentCtx, IdentifierID NameID, const llvm::SmallVectorImpl< IdentifierID > &ParameterTypeIDs)
Definition Types.h:1036
A temporary reference to an Objective-C selector, suitable for referencing selector data on the stack...
Definition Types.h:1092
llvm::ArrayRef< llvm::StringRef > Identifiers
Definition Types.h:1094
static bool isEqual(const clang::api_notes::APINotesFunctionSelectorKey &LHS, const clang::api_notes::APINotesFunctionSelectorKey &RHS)
Definition Types.h:1118
static unsigned getHashValue(const clang::api_notes::APINotesFunctionSelectorKey &Key)
Definition Types.h:1113
static unsigned getHashValue(const clang::api_notes::FunctionTableKey &Key)
Definition Types.h:1101
static bool isEqual(const clang::api_notes::FunctionTableKey &LHS, const clang::api_notes::FunctionTableKey &RHS)
Definition Types.h:1105