1414#define SWIFT_IDE_APIDIGESTERDATA_H
1515
1616#include " swift/Basic/LLVM.h"
17+ #include " llvm/ADT/ArrayRef.h"
1718#include " llvm/ADT/StringRef.h"
19+ #include " llvm/ADT/StringSwitch.h"
1820#include " llvm/Support/raw_ostream.h"
1921
2022namespace swift {
@@ -27,26 +29,42 @@ enum class SDKNodeKind: uint8_t {
2729#include " DigesterEnums.def"
2830};
2931
32+ SDKNodeKind parseSDKNodeKind (StringRef Content);
33+
3034enum class NodeAnnotation : uint8_t {
3135#define NODE_ANNOTATION (NAME ) NAME,
3236#include " DigesterEnums.def"
3337};
3438
39+ NodeAnnotation parseSDKNodeAnnotation (StringRef Content);
40+
41+ enum class APIDiffItemKind : uint8_t {
42+ #define DIFF_ITEM_KIND (NAME ) ADK_##NAME,
43+ #include " DigesterEnums.def"
44+ };
45+
3546// Redefine << so that we can output the name of the annotation kind.
3647raw_ostream &operator <<(raw_ostream &Out, const NodeAnnotation Value);
3748
3849// Redefine << so that we can output the name of the node kind.
3950raw_ostream &operator <<(raw_ostream &Out, const SDKNodeKind Value);
4051
41- // DiffItem describes how an element in SDK evolves in a way that migrator can
42- // read conveniently. Each DiffItem corresponds to one JSON element and contains
52+ struct APIDiffItem {
53+ virtual void streamDef (llvm::raw_ostream &S) const = 0;
54+ virtual APIDiffItemKind getKind () const = 0;
55+ virtual StringRef getKey () const = 0;
56+ virtual ~APIDiffItem () = default ;
57+ };
58+
59+ // CommonDiffItem describes how an element in SDK evolves in a way that migrator can
60+ // read conveniently. Each CommonDiffItem corresponds to one JSON element and contains
4361// sub fields explaining how migrator can assist client code to cope with such
4462// SDK change. For instance, the following first JSON element describes an unwrap
4563// optional change in the first parameter of function "c:@F@CTTextTabGetOptions".
4664// Similarly, the second JSON element describes a type parameter down cast in the
4765// second parameter of function "c:objc(cs)NSXMLDocument(im)insertChildren:atIndex:".
4866// We keep both usrs because in the future this may support auto-rename.
49- struct DiffItem {
67+ struct CommonDiffItem : public APIDiffItem {
5068 SDKNodeKind NodeKind;
5169 NodeAnnotation DiffKind;
5270 StringRef ChildIndex;
@@ -56,15 +74,21 @@ struct DiffItem {
5674 StringRef RightComment;
5775 StringRef ModuleName;
5876
59- DiffItem (SDKNodeKind NodeKind, NodeAnnotation DiffKind, StringRef ChildIndex,
60- StringRef LeftUsr, StringRef RightUsr, StringRef LeftComment,
61- StringRef RightComment, StringRef ModuleName);
77+ CommonDiffItem (SDKNodeKind NodeKind, NodeAnnotation DiffKind,
78+ StringRef ChildIndex, StringRef LeftUsr, StringRef RightUsr,
79+ StringRef LeftComment, StringRef RightComment,
80+ StringRef ModuleName);
6281
6382 static StringRef head ();
64- bool operator <(DiffItem Other) const ;
83+ bool operator <(CommonDiffItem Other) const ;
84+ static bool classof (const APIDiffItem *D);
6585 static void describe (llvm::raw_ostream &os);
6686 static void undef (llvm::raw_ostream &os);
67- void streamDef (llvm::raw_ostream &S) const ;
87+ void streamDef (llvm::raw_ostream &S) const override ;
88+ StringRef getKey () const override { return LeftUsr; }
89+ APIDiffItemKind getKind () const override {
90+ return APIDiffItemKind::ADK_CommonDiffItem;
91+ }
6892};
6993
7094
@@ -149,46 +173,95 @@ struct DiffItem {
149173// myColor.components
150174//
151175//
152- struct TypeMemberDiffItem {
176+ struct TypeMemberDiffItem : public APIDiffItem {
153177 StringRef usr;
154178 StringRef newTypeName;
155179 StringRef newPrintedName;
156180 Optional<uint8_t > selfIndex;
157181 StringRef oldPrintedName;
158182
183+ TypeMemberDiffItem (StringRef usr, StringRef newTypeName,
184+ StringRef newPrintedName, Optional<uint8_t > selfIndex,
185+ StringRef oldPrintedName) : usr(usr),
186+ newTypeName (newTypeName), newPrintedName(newPrintedName),
187+ selfIndex(selfIndex), oldPrintedName(oldPrintedName) {}
159188 static StringRef head ();
160189 static void describe (llvm::raw_ostream &os);
161190 static void undef (llvm::raw_ostream &os);
162- void streamDef (llvm::raw_ostream &os) const ;
191+ void streamDef (llvm::raw_ostream &os) const override ;
163192 bool operator <(TypeMemberDiffItem Other) const ;
193+ static bool classof (const APIDiffItem *D);
194+ StringRef getKey () const override { return usr; }
195+ APIDiffItemKind getKind () const override {
196+ return APIDiffItemKind::ADK_TypeMemberDiffItem;
197+ }
164198};
165199
166- struct NoEscapeFuncParam {
200+ struct NoEscapeFuncParam : public APIDiffItem {
167201 StringRef Usr;
168202 unsigned Index;
169203
170204 NoEscapeFuncParam (StringRef Usr, unsigned Index) : Usr(Usr), Index(Index) {}
171205 static StringRef head ();
172206 static void describe (llvm::raw_ostream &os);
173207 static void undef (llvm::raw_ostream &os);
174- void streamDef (llvm::raw_ostream &os) const ;
208+ void streamDef (llvm::raw_ostream &os) const override ;
175209 bool operator <(NoEscapeFuncParam Other) const ;
210+ static bool classof (const APIDiffItem *D);
211+ StringRef getKey () const override { return Usr; }
212+ APIDiffItemKind getKind () const override {
213+ return APIDiffItemKind::ADK_NoEscapeFuncParam;
214+ }
176215};
177216
178217// / This info is about functions meet the following criteria:
179218// / - This function is a member function of a type.
180219// / - This function is overloaded.
181- struct OverloadedFuncInfo {
220+ struct OverloadedFuncInfo : public APIDiffItem {
182221 StringRef Usr;
183222
184223 OverloadedFuncInfo (StringRef Usr) : Usr(Usr) {}
185224 static StringRef head ();
186225 static void describe (llvm::raw_ostream &os);
187226 static void undef (llvm::raw_ostream &os);
188- void streamDef (llvm::raw_ostream &os) const ;
227+ void streamDef (llvm::raw_ostream &os) const override ;
189228 bool operator <(OverloadedFuncInfo Other) const ;
229+ static bool classof (const APIDiffItem *D);
230+ StringRef getKey () const override { return Usr; }
231+ APIDiffItemKind getKind () const override {
232+ return APIDiffItemKind::ADK_OverloadedFuncInfo;
233+ }
234+ };
235+
236+ // / APIDiffItem store is the interface that migrator should communicates with;
237+ // / Given a key, usually the usr of the system entity under migration, the store
238+ // / should return a slice of related changes in the same format of
239+ // / swift-api-digester. This struct also handles the serialization and
240+ // / deserialization of all kinds of API diff items declared above.
241+ struct APIDiffItemStore {
242+ struct Implementation ;
243+ Implementation &Impl;
244+ static void serialize (llvm::raw_ostream &os, ArrayRef<APIDiffItem*> Items);
245+ APIDiffItemStore ();
246+ ~APIDiffItemStore ();
247+ ArrayRef<APIDiffItem*> getDiffItems (StringRef Key) const ;
248+ ArrayRef<APIDiffItem*> getAllDiffItems () const ;
249+
250+ // / Add a path of a JSON file dumped from swift-api-digester that contains
251+ // / API changes we care about. Calling this can be heavy since the procedure
252+ // / will parse and index the data inside of the given file.
253+ void addStorePath (StringRef Path);
190254};
191255}
192256}
257+ namespace json {
258+ template <>
259+ struct ScalarEnumerationTraits <ide::api::SDKNodeKind> {
260+ static void enumeration (Output &out, ide::api::SDKNodeKind &value) {
261+ #define NODE_KIND (X ) out.enumCase(value, #X, ide::api::SDKNodeKind::X);
262+ #include " swift/IDE/DigesterEnums.def"
263+ }
264+ };
265+ }
193266}
194267#endif // SWIFT_IDE_APIDIGESTERDATA_H
0 commit comments