clang 24.0.0git
APINotesReader.h
Go to the documentation of this file.
1//===--- APINotesReader.h - API Notes 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// This file defines the \c APINotesReader class that reads source API notes
10// data providing additional information about source code as a separate input,
11// such as the non-nil/nilable annotations for method parameters.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_APINOTES_READER_H
16#define LLVM_CLANG_APINOTES_READER_H
17
19#include "llvm/ADT/ArrayRef.h"
20#include "llvm/ADT/SmallVector.h"
21#include "llvm/Support/Error.h"
22#include "llvm/Support/MemoryBuffer.h"
23#include "llvm/Support/VersionTuple.h"
24#include <memory>
25#include <optional>
26#include <string>
27
28namespace clang {
29namespace api_notes {
30
31/// A class that reads API notes data from a binary file that was written by
32/// the \c APINotesWriter.
33class APINotesReader {
34 class Implementation;
35 std::unique_ptr<Implementation> Implementation;
36
37 APINotesReader(llvm::MemoryBuffer *InputBuffer,
38 llvm::VersionTuple SwiftVersion, llvm::Error &Err);
39
40public:
41 /// Create a new API notes reader from the given memory buffer, which
42 /// contains the contents of a binary API notes file.
43 ///
44 /// \returns the new API notes reader, or an error if one occurred.
46 Create(std::unique_ptr<llvm::MemoryBuffer> InputBuffer,
47 llvm::VersionTuple SwiftVersion);
48
50
51 APINotesReader(const APINotesReader &) = delete;
52 APINotesReader &operator=(const APINotesReader &) = delete;
53
54 /// Captures the completed versioned information for a particular part of
55 /// API notes, including both unversioned API notes and each versioned API
56 /// note for that particular entity.
57 template <typename T> class VersionedInfo {
58 /// The complete set of results.
60
61 /// The index of the result that is the "selected" set based on the desired
62 /// Swift version, or null if nothing matched.
63 std::optional<unsigned> Selected;
64
65 public:
66 /// Form an empty set of versioned information.
67 VersionedInfo(std::nullopt_t) : Selected(std::nullopt) {}
68
69 /// Form a versioned info set given the desired version and a set of
70 /// results.
72 llvm::VersionTuple Version,
73 llvm::SmallVector<std::pair<llvm::VersionTuple, T>, 1> Results);
74
75 /// Retrieve the selected index in the result set.
76 std::optional<unsigned> getSelected() const { return Selected; }
77
78 /// Return the number of versioned results we know about.
79 unsigned size() const { return Results.size(); }
80
81 /// Access all versioned results.
82 const std::pair<llvm::VersionTuple, T> *begin() const {
83 assert(!Results.empty());
84 return Results.begin();
85 }
86 const std::pair<llvm::VersionTuple, T> *end() const {
87 return Results.end();
88 }
89
90 /// Access a specific versioned result.
91 const std::pair<llvm::VersionTuple, T> &operator[](unsigned index) const {
92 assert(index < Results.size());
93 return Results[index];
94 }
95 };
96
97 /// Look for the context ID of the given Objective-C class.
98 ///
99 /// \param Name The name of the class we're looking for.
100 ///
101 /// \returns The ID, if known.
102 std::optional<ContextID> lookupObjCClassID(llvm::StringRef Name);
103
104 /// Look for information regarding the given Objective-C class.
105 ///
106 /// \param Name The name of the class we're looking for.
107 ///
108 /// \returns The information about the class, if known.
109 VersionedInfo<ContextInfo> lookupObjCClassInfo(llvm::StringRef Name);
110
111 /// Look for the context ID of the given Objective-C protocol.
112 ///
113 /// \param Name The name of the protocol we're looking for.
114 ///
115 /// \returns The ID of the protocol, if known.
116 std::optional<ContextID> lookupObjCProtocolID(llvm::StringRef Name);
117
118 /// Look for information regarding the given Objective-C protocol.
119 ///
120 /// \param Name The name of the protocol we're looking for.
121 ///
122 /// \returns The information about the protocol, if known.
123 VersionedInfo<ContextInfo> lookupObjCProtocolInfo(llvm::StringRef Name);
124
125 /// Look for information regarding the given Objective-C property in
126 /// the given context.
127 ///
128 /// \param CtxID The ID that references the context we are looking for.
129 /// \param Name The name of the property we're looking for.
130 /// \param IsInstance Whether we are looking for an instance property (vs.
131 /// a class property).
132 ///
133 /// \returns Information about the property, if known.
134 VersionedInfo<ObjCPropertyInfo>
135 lookupObjCProperty(ContextID CtxID, llvm::StringRef Name, bool IsInstance);
136
137 /// Look for information regarding the given Objective-C method in
138 /// the given context.
139 ///
140 /// \param CtxID The ID that references the context we are looking for.
141 /// \param Selector The selector naming the method we're looking for.
142 /// \param IsInstanceMethod Whether we are looking for an instance method.
143 ///
144 /// \returns Information about the method, if known.
145 VersionedInfo<ObjCMethodInfo> lookupObjCMethod(ContextID CtxID,
147 bool IsInstanceMethod);
148
149 /// Look for information regarding the given field of a C struct.
150 ///
151 /// \param Name The name of the field.
152 ///
153 /// \returns information about the field, if known.
154 VersionedInfo<FieldInfo> lookupField(ContextID CtxID, llvm::StringRef Name);
155
156 /// Look for information regarding the given C++ method in the given C++ tag
157 /// context.
158 ///
159 /// \param CtxID The ID that references the parent context, i.e. a C++ tag.
160 /// \param Name The name of the C++ method we're looking for.
161 ///
162 /// \returns Information about the method, if known.
163 VersionedInfo<CXXMethodInfo> lookupCXXMethod(ContextID CtxID,
164 llvm::StringRef Name);
165
166 /// Look for information regarding the given C++ method with an exact
167 /// parameter selector. An empty parameter list uses an exact zero-parameter
168 /// key, and a non-empty list uses an exact ordered parameter key.
169 VersionedInfo<CXXMethodInfo>
170 lookupCXXMethod(ContextID CtxID, llvm::StringRef Name,
171 llvm::ArrayRef<std::string> Parameters);
172
173 /// Build the selector key for the given C++ method.
174 std::optional<APINotesFunctionSelectorKey>
175 getCXXMethodSelectorKey(ContextID CtxID, llvm::StringRef Name);
176
177 /// Build the selector key for the given C++ method with an exact parameter
178 /// selector.
179 std::optional<APINotesFunctionSelectorKey>
180 getCXXMethodSelectorKey(ContextID CtxID, llvm::StringRef Name,
181 llvm::ArrayRef<std::string> Parameters);
182
183 /// Look for information regarding the given global variable.
184 ///
185 /// \param Name The name of the global variable.
186 ///
187 /// \returns information about the global variable, if known.
188 VersionedInfo<GlobalVariableInfo>
189 lookupGlobalVariable(llvm::StringRef Name,
190 std::optional<Context> Ctx = std::nullopt);
191
192 /// Look for information regarding the given global function.
193 ///
194 /// \param Name The name of the global function.
195 ///
196 /// \returns information about the global function, if known.
197 VersionedInfo<GlobalFunctionInfo>
198 lookupGlobalFunction(llvm::StringRef Name,
199 std::optional<Context> Ctx = std::nullopt);
200
201 /// Look for information regarding the given global function with an exact
202 /// parameter selector. An empty parameter list uses an exact zero-parameter
203 /// key, and a non-empty list uses an exact ordered parameter key.
204 VersionedInfo<GlobalFunctionInfo>
205 lookupGlobalFunction(llvm::StringRef Name,
207 std::optional<Context> Ctx = std::nullopt);
208
209 /// Build the selector key for the given global function.
210 std::optional<APINotesFunctionSelectorKey>
211 getGlobalFunctionSelectorKey(llvm::StringRef Name,
212 std::optional<Context> Ctx = std::nullopt);
213
214 /// Build the selector key for the given global function with an exact
215 /// parameter selector.
216 std::optional<APINotesFunctionSelectorKey>
217 getGlobalFunctionSelectorKey(llvm::StringRef Name,
219 std::optional<Context> Ctx = std::nullopt);
220
221 /// Collect exact parameter selector keys stored by this reader.
224
225 /// Reconstruct parameter selector strings for a stored exact selector key.
226 std::optional<llvm::SmallVector<std::string, 4>>
228 const APINotesFunctionSelectorKey &Key);
229
230 /// Look for information regarding the given enumerator.
231 ///
232 /// \param Name The name of the enumerator.
233 ///
234 /// \returns information about the enumerator, if known.
235 VersionedInfo<EnumConstantInfo> lookupEnumConstant(llvm::StringRef Name);
236
237 /// Look for the context ID of the given C++ tag.
238 ///
239 /// \param Name The name of the tag we're looking for.
240 /// \param ParentCtx The context in which this tag is declared, e.g. a C++
241 /// namespace.
242 ///
243 /// \returns The ID, if known.
244 std::optional<ContextID>
245 lookupTagID(llvm::StringRef Name,
246 std::optional<Context> ParentCtx = std::nullopt);
247
248 /// Look for information regarding the given tag
249 /// (struct/union/enum/C++ class).
250 ///
251 /// \param Name The name of the tag.
252 ///
253 /// \returns information about the tag, if known.
254 VersionedInfo<TagInfo> lookupTag(llvm::StringRef Name,
255 std::optional<Context> Ctx = std::nullopt);
256
257 /// Look for information regarding the given typedef.
258 ///
259 /// \param Name The name of the typedef.
260 ///
261 /// \returns information about the typedef, if known.
262 VersionedInfo<TypedefInfo>
263 lookupTypedef(llvm::StringRef Name,
264 std::optional<Context> Ctx = std::nullopt);
265
266 /// Look for the context ID of the given C++ namespace.
267 ///
268 /// \param Name The name of the class we're looking for.
269 ///
270 /// \returns The ID, if known.
271 std::optional<ContextID>
272 lookupNamespaceID(llvm::StringRef Name,
273 std::optional<ContextID> ParentNamespaceID = std::nullopt);
274
275private:
276 VersionedInfo<CXXMethodInfo> lookupCXXMethodImpl(ContextID CtxID,
277 llvm::StringRef Name);
278 template <typename ParameterT>
279 VersionedInfo<CXXMethodInfo>
280 lookupCXXMethodImpl(ContextID CtxID, llvm::StringRef Name,
281 llvm::ArrayRef<ParameterT> Parameters);
282
283 VersionedInfo<GlobalFunctionInfo>
284 lookupGlobalFunctionImpl(llvm::StringRef Name, std::optional<Context> Ctx);
285 template <typename ParameterT>
286 VersionedInfo<GlobalFunctionInfo>
287 lookupGlobalFunctionImpl(llvm::StringRef Name,
289 std::optional<Context> Ctx);
290};
291
292} // end namespace api_notes
293} // end namespace clang
294
295#endif // LLVM_CLANG_APINOTES_READER_H
Smart pointer class that efficiently represents Objective-C method names.
VersionedInfo(std::nullopt_t)
Form an empty set of versioned information.
const std::pair< llvm::VersionTuple, T > & operator[](unsigned index) const
Access a specific versioned result.
const std::pair< llvm::VersionTuple, T > * end() const
unsigned size() const
Return the number of versioned results we know about.
std::optional< unsigned > getSelected() const
Retrieve the selected index in the result set.
const std::pair< llvm::VersionTuple, T > * begin() const
Access all versioned results.
VersionedInfo< ContextInfo > lookupObjCClassInfo(llvm::StringRef Name)
Look for information regarding the given Objective-C class.
VersionedInfo< FieldInfo > lookupField(ContextID CtxID, llvm::StringRef Name)
Look for information regarding the given field of a C struct.
VersionedInfo< CXXMethodInfo > lookupCXXMethod(ContextID CtxID, llvm::StringRef Name)
Look for information regarding the given C++ method in the given C++ tag context.
VersionedInfo< TagInfo > lookupTag(llvm::StringRef Name, std::optional< Context > Ctx=std::nullopt)
Look for information regarding the given tag (struct/union/enum/C++ class).
VersionedInfo< GlobalFunctionInfo > lookupGlobalFunction(llvm::StringRef Name, std::optional< Context > Ctx=std::nullopt)
Look for information regarding the given global function.
VersionedInfo< ObjCPropertyInfo > lookupObjCProperty(ContextID CtxID, llvm::StringRef Name, bool IsInstance)
Look for information regarding the given Objective-C property in the given context.
VersionedInfo< ObjCMethodInfo > lookupObjCMethod(ContextID CtxID, ObjCSelectorRef Selector, bool IsInstanceMethod)
Look for information regarding the given Objective-C method in the given context.
std::optional< APINotesFunctionSelectorKey > getGlobalFunctionSelectorKey(llvm::StringRef Name, std::optional< Context > Ctx=std::nullopt)
Build the selector key for the given global function.
VersionedInfo< GlobalVariableInfo > lookupGlobalVariable(llvm::StringRef Name, std::optional< Context > Ctx=std::nullopt)
Look for information regarding the given global variable.
std::optional< APINotesFunctionSelectorKey > getCXXMethodSelectorKey(ContextID CtxID, llvm::StringRef Name)
Build the selector key for the given C++ method.
std::optional< ContextID > lookupNamespaceID(llvm::StringRef Name, std::optional< ContextID > ParentNamespaceID=std::nullopt)
Look for the context ID of the given C++ namespace.
std::optional< ContextID > lookupTagID(llvm::StringRef Name, std::optional< Context > ParentCtx=std::nullopt)
Look for the context ID of the given C++ tag.
VersionedInfo< TypedefInfo > lookupTypedef(llvm::StringRef Name, std::optional< Context > Ctx=std::nullopt)
Look for information regarding the given typedef.
std::optional< ContextID > lookupObjCClassID(llvm::StringRef Name)
Look for the context ID of the given Objective-C class.
std::optional< llvm::SmallVector< std::string, 4 > > getParameterSelectorSpellingsForDiagnostics(const APINotesFunctionSelectorKey &Key)
Reconstruct parameter selector strings for a stored exact selector key.
APINotesReader & operator=(const APINotesReader &)=delete
VersionedInfo< ContextInfo > lookupObjCProtocolInfo(llvm::StringRef Name)
Look for information regarding the given Objective-C protocol.
void collectExactFunctionParameterSelectors(llvm::SmallVectorImpl< APINotesFunctionSelectorKey > &Selectors)
Collect exact parameter selector keys stored by this reader.
std::optional< ContextID > lookupObjCProtocolID(llvm::StringRef Name)
Look for the context ID of the given Objective-C protocol.
VersionedInfo< EnumConstantInfo > lookupEnumConstant(llvm::StringRef Name)
Look for information regarding the given enumerator.
APINotesReader(const APINotesReader &)=delete
Opaque context ID used to refer to an Objective-C class or protocol or a C++ namespace.
Definition Types.h:988
The JSON file list parser is used to communicate input to InstallAPI.
@ Create
'create' clause, allowed on Compute and Combined constructs, plus 'data', 'enter data',...
Stable reader-facing identity for an API notes function selector entry.
Definition Types.h:1068
A temporary reference to an Objective-C selector, suitable for referencing selector data on the stack...
Definition Types.h:1092