clang 20.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/StringRef.h"
15#include <climits>
16#include <optional>
17#include <vector>
18
19namespace llvm {
20class raw_ostream;
21} // namespace llvm
22
23namespace clang {
24namespace api_notes {
26 None,
31};
32
33/// The payload for an enum_extensibility attribute. This is a tri-state rather
34/// than just a boolean because the presence of the attribute indicates
35/// auditing.
37 None,
38 Open,
39 Closed,
40};
41
42/// The kind of a swift_wrapper/swift_newtype.
43enum class SwiftNewTypeKind {
44 None,
45 Struct,
46 Enum,
47};
48
49/// Describes API notes data for any entity.
50///
51/// This is used as the base of all API notes.
53public:
54 /// Message to use when this entity is unavailable.
55 std::string UnavailableMsg;
56
57 /// Whether this entity is marked unavailable.
58 LLVM_PREFERRED_TYPE(bool)
60
61 /// Whether this entity is marked unavailable in Swift.
62 LLVM_PREFERRED_TYPE(bool)
63 unsigned UnavailableInSwift : 1;
64
65private:
66 /// Whether SwiftPrivate was specified.
67 LLVM_PREFERRED_TYPE(bool)
68 unsigned SwiftPrivateSpecified : 1;
69
70 /// Whether this entity is considered "private" to a Swift overlay.
71 LLVM_PREFERRED_TYPE(bool)
72 unsigned SwiftPrivate : 1;
73
74public:
75 /// Swift name of this entity.
76 std::string SwiftName;
77
79 : Unavailable(0), UnavailableInSwift(0), SwiftPrivateSpecified(0),
80 SwiftPrivate(0) {}
81
82 std::optional<bool> isSwiftPrivate() const {
83 return SwiftPrivateSpecified ? std::optional<bool>(SwiftPrivate)
84 : std::nullopt;
85 }
86
87 void setSwiftPrivate(std::optional<bool> Private) {
88 SwiftPrivateSpecified = Private.has_value();
89 SwiftPrivate = Private.value_or(0);
90 }
91
92 friend bool operator==(const CommonEntityInfo &, const CommonEntityInfo &);
93
95 // Merge unavailability.
96 if (RHS.Unavailable) {
97 Unavailable = true;
98 if (UnavailableMsg.empty())
100 }
101
102 if (RHS.UnavailableInSwift) {
103 UnavailableInSwift = true;
104 if (UnavailableMsg.empty())
106 }
107
108 if (!SwiftPrivateSpecified)
110
111 if (SwiftName.empty())
112 SwiftName = RHS.SwiftName;
113
114 return *this;
115 }
116
117 LLVM_DUMP_METHOD void dump(llvm::raw_ostream &OS) const;
118};
119
120inline bool operator==(const CommonEntityInfo &LHS,
121 const CommonEntityInfo &RHS) {
122 return LHS.UnavailableMsg == RHS.UnavailableMsg &&
123 LHS.Unavailable == RHS.Unavailable &&
125 LHS.SwiftPrivateSpecified == RHS.SwiftPrivateSpecified &&
126 LHS.SwiftPrivate == RHS.SwiftPrivate && LHS.SwiftName == RHS.SwiftName;
127}
128
129inline bool operator!=(const CommonEntityInfo &LHS,
130 const CommonEntityInfo &RHS) {
131 return !(LHS == RHS);
132}
133
134/// Describes API notes for types.
136 /// The Swift type to which a given type is bridged.
137 ///
138 /// Reflects the swift_bridge attribute.
139 std::optional<std::string> SwiftBridge;
140
141 /// The NS error domain for this type.
142 std::optional<std::string> NSErrorDomain;
143
144public:
146
147 const std::optional<std::string> &getSwiftBridge() const {
148 return SwiftBridge;
149 }
150
151 void setSwiftBridge(std::optional<std::string> SwiftType) {
152 SwiftBridge = SwiftType;
153 }
154
155 const std::optional<std::string> &getNSErrorDomain() const {
156 return NSErrorDomain;
157 }
158
159 void setNSErrorDomain(const std::optional<std::string> &Domain) {
160 NSErrorDomain = Domain;
161 }
162
163 void setNSErrorDomain(const std::optional<llvm::StringRef> &Domain) {
164 NSErrorDomain = Domain ? std::optional<std::string>(std::string(*Domain))
165 : std::nullopt;
166 }
167
168 friend bool operator==(const CommonTypeInfo &, const CommonTypeInfo &);
169
171 // Merge inherited info.
172 static_cast<CommonEntityInfo &>(*this) |= RHS;
173
174 if (!SwiftBridge)
176 if (!NSErrorDomain)
178
179 return *this;
180 }
181
182 LLVM_DUMP_METHOD void dump(llvm::raw_ostream &OS) const;
183};
184
185inline bool operator==(const CommonTypeInfo &LHS, const CommonTypeInfo &RHS) {
186 return static_cast<const CommonEntityInfo &>(LHS) == RHS &&
187 LHS.SwiftBridge == RHS.SwiftBridge &&
188 LHS.NSErrorDomain == RHS.NSErrorDomain;
189}
190
191inline bool operator!=(const CommonTypeInfo &LHS, const CommonTypeInfo &RHS) {
192 return !(LHS == RHS);
193}
194
195/// Describes API notes data for an Objective-C class or protocol or a C++
196/// namespace.
198 /// Whether this class has a default nullability.
199 LLVM_PREFERRED_TYPE(bool)
200 unsigned HasDefaultNullability : 1;
201
202 /// The default nullability.
203 LLVM_PREFERRED_TYPE(NullabilityKind)
204 unsigned DefaultNullability : 2;
205
206 /// Whether this class has designated initializers recorded.
207 LLVM_PREFERRED_TYPE(bool)
208 unsigned HasDesignatedInits : 1;
209
210 LLVM_PREFERRED_TYPE(bool)
211 unsigned SwiftImportAsNonGenericSpecified : 1;
212 LLVM_PREFERRED_TYPE(bool)
213 unsigned SwiftImportAsNonGeneric : 1;
214
215 LLVM_PREFERRED_TYPE(bool)
216 unsigned SwiftObjCMembersSpecified : 1;
217 LLVM_PREFERRED_TYPE(bool)
218 unsigned SwiftObjCMembers : 1;
219
220public:
222 : HasDefaultNullability(0), DefaultNullability(0), HasDesignatedInits(0),
223 SwiftImportAsNonGenericSpecified(false), SwiftImportAsNonGeneric(false),
224 SwiftObjCMembersSpecified(false), SwiftObjCMembers(false) {}
225
226 /// Determine the default nullability for properties and methods of this
227 /// class.
228 ///
229 /// Returns the default nullability, if implied, or std::nullopt if there is
230 /// none.
231 std::optional<NullabilityKind> getDefaultNullability() const {
232 return HasDefaultNullability
233 ? std::optional<NullabilityKind>(
234 static_cast<NullabilityKind>(DefaultNullability))
235 : std::nullopt;
236 }
237
238 /// Set the default nullability for properties and methods of this class.
240 HasDefaultNullability = true;
241 DefaultNullability = static_cast<unsigned>(Kind);
242 }
243
244 bool hasDesignatedInits() const { return HasDesignatedInits; }
245 void setHasDesignatedInits(bool Value) { HasDesignatedInits = Value; }
246
247 std::optional<bool> getSwiftImportAsNonGeneric() const {