clang 23.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/Support/Error.h"
21#include "llvm/Support/MemoryBuffer.h"
22#include "llvm/Support/VersionTuple.h"
23#include <memory>
24#include <optional>
25#include <string>
26
27namespace clang {
28namespace api_notes {
29
30/// A class that reads API notes data from a binary file that was written by
31/// the \c APINotesWriter.
32class APINotesReader {
33 class Implementation;
34 std::unique_ptr<Implementation> Implementation;
35
36 APINotesReader(llvm::MemoryBuffer *InputBuffer,
37 llvm::VersionTuple SwiftVersion, llvm::Error &Err);
38
39public:
40 /// Create a new API notes reader from the given memory buffer, which
41 /// contains the contents of a binary API notes file.
42 ///
43 /// \returns the new API notes reader, or an error if one occurred.
45 Create(std::unique_ptr<llvm::MemoryBuffer> InputBuffer,
46 llvm::VersionTuple SwiftVersion);
47
49
50 APINotesReader(const APINotesReader &) = delete;
51 APINotesReader &operator=(const APINotesReader &) = delete;
52
53 /// Captures the completed versioned information for a particular part of
54 /// API notes, including both unversioned API notes and each versioned API
55 /// note for that particular entity.
56 template <typename T> class VersionedInfo {
57 /// The complete set of results.
59
60 /// The index of the result that is the "selected" set based on the desired
61 /// Swift version, or null if nothing matched.
62 std::optional<unsigned> Selected;
63
64 public:
65 /// Form an empty set of versioned information.
66 VersionedInfo(std::nullopt_t) : Selected(std::nullopt) {}
67
68 /// Form a versioned info set given the desired version and a set of
69 /// results.
71 llvm::VersionTuple Version,
72 llvm::SmallVector<std::pair<llvm::VersionTuple, T>, 1> Results);
73
74 /// Retrieve the selected index in the result set.
75 std::optional<unsigned> getSelected() const { return Selected; }
76
77 /// Return the number of versioned results we know about.
78 unsigned size() const { return Results.size(); }
79
80 /// Access all versioned results.
81 const std::pair<llvm::VersionTuple, T> *begin() const {
82 assert(!Results.empty());
83 return Results.begin();
84 }
85 const std::pair<llvm::VersionTuple, T> *end() const {
86 return Results.end();
87 }
88
89 /// Access a specific versioned result.
90 const std::pair<llvm::VersionTuple, T> &operator[](unsigned index) const {
91 assert(index < Results.size());
92 return Results[index];
93 }
94 };
95
96 /// Look for the context ID of the given Objective-C class.
97 ///
98 /// \param Name The name of the class we're looking for.
99 ///
100 /// \returns The ID, if known.
101 std::optional<ContextID> lookupObjCClassID(llvm::StringRef Name);
102
103 /// Look for information regarding the given Objective-C class.
104 ///
105 /// \param Name The name of the class we're looking for.
106 ///
107 /// \returns The information about the class, if known.
108 VersionedInfo<ContextInfo> lookupObjCClassInfo(llvm::StringRef Name);
109
110 /// Look for the context ID of the given Objective-C protocol.
111 ///
112 /// \param Name The name of the protocol we're looking for.
113 ///
114 /// \returns The ID of the protocol, if known.
115 std::optional<ContextID> lookupObjCProtocolID(llvm::StringRef Name);
116
117 /// Look for information regarding the given Objective-C protocol.
118 ///
119 /// \param Name The name of the protocol we're looking for.
120 ///
121 /// \returns The information about the protocol, if known.
122 VersionedInfo<ContextInfo> lookupObjCProtocolInfo(llvm::StringRef Name);
123
124 /// Look for information regarding the given Objective-C property in
125 /// the given context.
126 ///
127 /// \param CtxID The ID that references the context we are looking for.
128 /// \param Name The name of the property we're looking for.
129 /// \param IsInstance Whether we are looking for an instance property (vs.
130 /// a class property).
131 ///
132 /// \returns Information about the property, if known.
133 VersionedInfo<ObjCPropertyInfo>
134 lookupObjCProperty(ContextID CtxID, llvm::StringRef Name, bool IsInstance);
135
136 /// Look for information regarding the given Objective-C method in
137 /// the given context.
138 ///
139 /// \param CtxID The ID that references the context we are looking for.
140 /// \param Selector The selector naming the method we're looking for.
141 /// \param IsInstanceMethod Whether we are looking for an instance method.
142 ///
143 /// \returns Information about the method, if known.
144 VersionedInfo<ObjCMethodInfo> lookupObjCMethod(ContextID CtxID,
146 bool IsInstanceMethod);
147
148 /// Look for information regarding the given field of a C struct.
149 ///
150 /// \param Name The name of the field.
151 ///
152 /// \returns information about the field, if known.
153 VersionedInfo<FieldInfo> lookupField(ContextID CtxID, llvm::StringRef Name);
154
155 /// Look for information regarding the given C++ method in the given C++ tag
156 /// context.
157 ///
158 /// \param CtxID The ID that references the parent context, i.e. a C++ tag.
159 /// \param Name The name of the C++ method we're looking for.
160 ///
161 /// \returns Information about the method, if known.
162 VersionedInfo<CXXMethodInfo> lookupCXXMethod(ContextID CtxID,
163 llvm::StringRef Name);
164
165 /// Look for information regarding the given C++ method with an exact
166 /// parameter selector. An empty parameter list uses an exact zero-parameter
167 /// key, and a non-empty list uses an exact ordered parameter key.
168 VersionedInfo<CXXMethodInfo>
169 lookupCXXMethod(ContextID CtxID, llvm::StringRef Name,
170 llvm::ArrayRef<std::string> Parameters);
171
172 /// Look for information regarding the given global variable.
173 ///
174 /// \param Name The name of the global variable.
175 ///
176 /// \returns information about the global variable, if known.
177 VersionedInfo<GlobalVariableInfo>
178 lookupGlobalVariable(llvm::StringRef Name,
179 std::optional<Context> Ctx = std::nullopt);
180
181 /// Look for information regarding the given global function.
182 ///
183 /// \param Name The name of the global function.
184 ///
185 /// \returns information about the global function, if known.
186 VersionedInfo<GlobalFunctionInfo>
187 lookupGlobalFunction(llvm::StringRef Name,
188 std::optional<Context> Ctx = std::nullopt);
189
190 /// Look for information regarding the given global function with an exact
191 /// parameter selector. An empty parameter list uses an exact zero-parameter
192 /// key, and a non-empty list uses an exact ordered parameter key.
193 VersionedInfo<GlobalFunctionInfo>
194 lookupGlobalFunction(llvm::StringRef Name,
196 std::optional<Context> Ctx = std::nullopt);
197
198 /// Look for information regarding the given enumerator.
199 ///
200 /// \param Name The name of the enumerator.
201 ///
202 /// \returns information about the enumerator, if known.
203 VersionedInfo<EnumConstantInfo> lookupEnumConstant(llvm::StringRef Name);
204
205 /// Look for the context ID of the given C++ tag.
206 ///
207 /// \param Name The name of the tag we're looking for.
208 /// \param ParentCtx The context in which this tag is declared, e.g. a C++
209 /// namespace.
210 ///
211 /// \returns The ID, if known.
212 std::optional<ContextID>
213 lookupTagID(llvm::StringRef Name,
214 std::optional<Context> ParentCtx = std::nullopt);
215
216 /// Look for information regarding the given tag
217 /// (struct/union/enum/C++ class).
218 ///
219 /// \param Name The name of the tag.
220 ///
221 /// \returns information about the tag, if known.
222 VersionedInfo<TagInfo> lookupTag(llvm::StringRef Name,
223 std::optional<Context> Ctx = std::nullopt);
224
225 /// Look for information regarding the given typedef.
226 ///
227 /// \param Name The name of the typedef.
228 ///
229 /// \returns information about the typedef, if known.
230 VersionedInfo<TypedefInfo>
231 lookupTypedef(llvm::StringRef Name,
232 std::optional<Context> Ctx = std::nullopt);
233
234 /// Look for the context ID of the given C++ namespace.
235 ///
236 /// \param Name The name of the class we're looking for.
237 ///
238 /// \returns The ID, if known.
239 std::optional<ContextID>
240 lookupNamespaceID(llvm::StringRef Name,
241 std::optional<ContextID> ParentNamespaceID = std::nullopt);
242
243private:
244 VersionedInfo<CXXMethodInfo> lookupCXXMethodImpl(ContextID CtxID,
245 llvm::StringRef Name);
246 template <typename ParameterT>
247 VersionedInfo<CXXMethodInfo>
248 lookupCXXMethodImpl(ContextID CtxID, llvm::StringRef Name,
249 llvm::ArrayRef<ParameterT> Parameters);
250
251 VersionedInfo<GlobalFunctionInfo>
252 lookupGlobalFunctionImpl(llvm::StringRef Name, std::optional<Context> Ctx);
253 template <typename ParameterT>
254 VersionedInfo<GlobalFunctionInfo>
255 lookupGlobalFunctionImpl(llvm::StringRef Name,
257 std::optional<Context> Ctx);
258};
259
260} // end namespace api_notes
261} // end namespace clang
262
263#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.
VersionedInfo< GlobalVariableInfo > lookupGlobalVariable(llvm::StringRef Name, std::optional< Context > Ctx=std::nullopt)
Look for information regarding the given global variable.
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.
APINotesReader & operator=(const APINotesReader &)=delete
VersionedInfo< ContextInfo > lookupObjCProtocolInfo(llvm::StringRef Name)
Look for information regarding the given Objective-C protocol.
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:970
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',...
A temporary reference to an Objective-C selector, suitable for referencing selector data on the stack...
Definition Types.h:997