17
17
#define LLVM_CLANG_API_NOTES_READER_H
18
18
19
19
#include " clang/APINotes/Types.h"
20
+ #include " clang/Basic/VersionTuple.h"
20
21
#include " llvm/ADT/Optional.h"
21
22
#include " llvm/Support/MemoryBuffer.h"
22
23
#include < memory>
@@ -32,22 +33,24 @@ class APINotesReader {
32
33
Implementation &Impl;
33
34
34
35
APINotesReader (llvm::MemoryBuffer *inputBuffer, bool ownsInputBuffer,
35
- bool &failed);
36
+ VersionTuple swiftVersion, bool &failed);
36
37
37
38
public:
38
39
// / Create a new API notes reader from the given member buffer, which
39
40
// / contains the contents of a binary API notes file.
40
41
// /
41
42
// / \returns the new API notes reader, or null if an error occurred.
42
43
static std::unique_ptr<APINotesReader>
43
- get (std::unique_ptr<llvm::MemoryBuffer> inputBuffer);
44
+ get (std::unique_ptr<llvm::MemoryBuffer> inputBuffer,
45
+ VersionTuple swiftVersion);
44
46
45
47
// / Create a new API notes reader from the given member buffer, which
46
48
// / contains the contents of a binary API notes file.
47
49
// /
48
50
// / \returns the new API notes reader, or null if an error occurred.
49
51
static std::unique_ptr<APINotesReader>
50
- getUnmanaged (llvm::MemoryBuffer *inputBuffer);
52
+ getUnmanaged (llvm::MemoryBuffer *inputBuffer,
53
+ VersionTuple swiftVersion);
51
54
52
55
~APINotesReader ();
53
56
@@ -65,21 +68,83 @@ class APINotesReader {
65
68
// / Retrieve the module options
66
69
ModuleOptions getModuleOptions () const ;
67
70
71
+ // / Captures the completed versioned information for a particular part of
72
+ // / API notes, including both unversioned API notes and each versioned API
73
+ // / note for that particular entity.
74
+ template <typename T>
75
+ class VersionedInfo {
76
+ // / The complete set of results.
77
+ SmallVector<std::pair<VersionTuple, T>, 1 > Results;
78
+
79
+ // / The index of the result that is the "selected" set based on the desired
80
+ // / Swift version, or \c Results.size() if nothing matched.
81
+ unsigned Selected;
82
+
83
+ public:
84
+ // / Form an empty set of versioned information.
85
+ VersionedInfo (llvm::NoneType) : Selected(0 ) { }
86
+
87
+ // / Form a versioned info set given the desired version and a set of
88
+ // / results.
89
+ VersionedInfo (VersionTuple version,
90
+ SmallVector<std::pair<VersionTuple, T>, 1 > results);
91
+
92
+ // / Determine whether there is a result that should be applied directly
93
+ // / to the AST.
94
+ explicit operator bool () const { return Selected != size (); }
95
+
96
+ // / Retrieve the information to apply directly to the AST.
97
+ const T& operator *() const {
98
+ assert (*this && " No result to apply directly" );
99
+ return (*this )[Selected].second ;
100
+ }
101
+
102
+ // / Retrieve the selected index in the result set.
103
+ Optional<unsigned > getSelected () const {
104
+ if (Selected == Results.size ()) return None;
105
+ return Selected;
106
+ }
107
+
108
+ // / Return the number of versioned results we know about.
109
+ unsigned size () const { return Results.size (); }
110
+
111
+ // / Access all versioned results.
112
+ const std::pair<VersionTuple, T> *begin () const { return Results.begin (); }
113
+ const std::pair<VersionTuple, T> *end () const { return Results.end (); }
114
+
115
+ // / Access a specific versioned result.
116
+ const std::pair<VersionTuple, T> &operator [](unsigned index) const {
117
+ return Results[index];
118
+ }
119
+ };
120
+
121
+ // / Look for the context ID of the given Objective-C class.
122
+ // /
123
+ // / \param name The name of the class we're looking for.
124
+ // /
125
+ // / \returns The ID, if known.
126
+ Optional<ContextID> lookupObjCClassID (StringRef name);
127
+
68
128
// / Look for information regarding the given Objective-C class.
69
129
// /
70
130
// / \param name The name of the class we're looking for.
71
131
// /
72
- // / \returns The ID and information about the class, if known.
73
- Optional<std::pair<ContextID, ObjCContextInfo>>
74
- lookupObjCClass (StringRef name);
132
+ // / \returns The information about the class, if known.
133
+ VersionedInfo<ObjCContextInfo> lookupObjCClassInfo (StringRef name);
134
+
135
+ // / Look for the context ID of the given Objective-C protocol.
136
+ // /
137
+ // / \param name The name of the protocol we're looking for.
138
+ // /
139
+ // / \returns The ID of the protocol, if known.
140
+ Optional<ContextID> lookupObjCProtocolID (StringRef name);
75
141
76
142
// / Look for information regarding the given Objective-C protocol.
77
143
// /
78
144
// / \param name The name of the protocol we're looking for.
79
145
// /
80
- // / \returns The ID and information about the protocol, if known.
81
- Optional<std::pair<ContextID, ObjCContextInfo>>
82
- lookupObjCProtocol (StringRef name);
146
+ // / \returns The information about the protocol, if known.
147
+ VersionedInfo<ObjCContextInfo> lookupObjCProtocolInfo (StringRef name);
83
148
84
149
// / Look for information regarding the given Objective-C property in
85
150
// / the given context.
@@ -88,11 +153,12 @@ class APINotesReader {
88
153
// / \param name The name of the property we're looking for.
89
154
// / \param isInstance Whether we are looking for an instance property (vs.
90
155
// / a class property).
156
+ // / \param swiftVersion The Swift version to filter for, if any.
91
157
// /
92
158
// / \returns Information about the property, if known.
93
- Optional <ObjCPropertyInfo> lookupObjCProperty (ContextID contextID,
94
- StringRef name,
95
- bool isInstance);
159
+ VersionedInfo <ObjCPropertyInfo> lookupObjCProperty (ContextID contextID,
160
+ StringRef name,
161
+ bool isInstance);
96
162
97
163
// / Look for information regarding the given Objective-C method in
98
164
// / the given context.
@@ -102,45 +168,45 @@ class APINotesReader {
102
168
// / \param isInstanceMethod Whether we are looking for an instance method.
103
169
// /
104
170
// / \returns Information about the method, if known.
105
- Optional <ObjCMethodInfo> lookupObjCMethod (ContextID contextID,
106
- ObjCSelectorRef selector,
107
- bool isInstanceMethod);
171
+ VersionedInfo <ObjCMethodInfo> lookupObjCMethod (ContextID contextID,
172
+ ObjCSelectorRef selector,
173
+ bool isInstanceMethod);
108
174
109
175
// / Look for information regarding the given global variable.
110
176
// /
111
177
// / \param name The name of the global variable.
112
178
// /
113
179
// / \returns information about the global variable, if known.
114
- Optional <GlobalVariableInfo> lookupGlobalVariable (StringRef name);
180
+ VersionedInfo <GlobalVariableInfo> lookupGlobalVariable (StringRef name);
115
181
116
182
// / Look for information regarding the given global function.
117
183
// /
118
184
// / \param name The name of the global function.
119
185
// /
120
186
// / \returns information about the global function, if known.
121
- Optional <GlobalFunctionInfo> lookupGlobalFunction (StringRef name);
187
+ VersionedInfo <GlobalFunctionInfo> lookupGlobalFunction (StringRef name);
122
188
123
189
// / Look for information regarding the given enumerator.
124
190
// /
125
191
// / \param name The name of the enumerator.
126
192
// /
127
193
// / \returns information about the enumerator, if known.
128
- Optional <EnumConstantInfo> lookupEnumConstant (StringRef name);
194
+ VersionedInfo <EnumConstantInfo> lookupEnumConstant (StringRef name);
129
195
130
196
// / Look for information regarding the given tag
131
197
// / (struct/union/enum/C++ class).
132
198
// /
133
199
// / \param name The name of the tag.
134
200
// /
135
201
// / \returns information about the tag, if known.
136
- Optional <TagInfo> lookupTag (StringRef name);
202
+ VersionedInfo <TagInfo> lookupTag (StringRef name);
137
203
138
204
// / Look for information regarding the given typedef.
139
205
// /
140
206
// / \param name The name of the typedef.
141
207
// /
142
208
// / \returns information about the typedef, if known.
143
- Optional <TypedefInfo> lookupTypedef (StringRef name);
209
+ VersionedInfo <TypedefInfo> lookupTypedef (StringRef name);
144
210
145
211
// / Visitor used when walking the contents of the API notes file.
146
212
class Visitor {
@@ -149,39 +215,48 @@ class APINotesReader {
149
215
150
216
// / Visit an Objective-C class.
151
217
virtual void visitObjCClass (ContextID contextID, StringRef name,
152
- const ObjCContextInfo &info);
218
+ const ObjCContextInfo &info,
219
+ VersionTuple swiftVersion);
153
220
154
221
// / Visit an Objective-C protocol.
155
222
virtual void visitObjCProtocol (ContextID contextID, StringRef name,
156
- const ObjCContextInfo &info);
223
+ const ObjCContextInfo &info,
224
+ VersionTuple swiftVersion);
157
225
158
226
// / Visit an Objective-C method.
159
227
virtual void visitObjCMethod (ContextID contextID, StringRef selector,
160
228
bool isInstanceMethod,
161
- const ObjCMethodInfo &info);
229
+ const ObjCMethodInfo &info,
230
+ VersionTuple swiftVersion);
162
231
163
232
// / Visit an Objective-C property.
164
233
virtual void visitObjCProperty (ContextID contextID, StringRef name,
165
234
bool isInstance,
166
- const ObjCPropertyInfo &info);
235
+ const ObjCPropertyInfo &info,
236
+ VersionTuple swiftVersion);
167
237
168
238
// / Visit a global variable.
169
239
virtual void visitGlobalVariable (StringRef name,
170
- const GlobalVariableInfo &info);
240
+ const GlobalVariableInfo &info,
241
+ VersionTuple swiftVersion);
171
242
172
243
// / Visit a global function.
173
244
virtual void visitGlobalFunction (StringRef name,
174
- const GlobalFunctionInfo &info);
245
+ const GlobalFunctionInfo &info,
246
+ VersionTuple swiftVersion);
175
247
176
248
// / Visit an enumerator.
177
249
virtual void visitEnumConstant (StringRef name,
178
- const EnumConstantInfo &info);
250
+ const EnumConstantInfo &info,
251
+ VersionTuple swiftVersion);
179
252
180
253
// / Visit a tag.
181
- virtual void visitTag (StringRef name, const TagInfo &info);
254
+ virtual void visitTag (StringRef name, const TagInfo &info,
255
+ VersionTuple swiftVersion);
182
256
183
257
// / Visit a typedef.
184
- virtual void visitTypedef (StringRef name, const TypedefInfo &info);
258
+ virtual void visitTypedef (StringRef name, const TypedefInfo &info,
259
+ VersionTuple swiftVersion);
185
260
};
186
261
187
262
// / Visit the contents of the API notes file, passing each entity to the
0 commit comments