Skip to content

Commit 15a961b

Browse files
authored
Merge pull request #30 from literalpie/symbol-relation
Add SymbolRelation class with roles and symbols
2 parents e615203 + 31dadcd commit 15a961b

File tree

3 files changed

+214
-0
lines changed

3 files changed

+214
-0
lines changed

Sources/IndexStoreDB/IndexStoreDB.swift

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,17 @@ public final class IndexStoreDB {
109109

110110
}
111111

112+
public final class SymbolRelation {
113+
let value: indexstoredb_symbol_relation_t
114+
115+
public lazy var roles: SymbolRole = SymbolRole(rawValue: indexstoredb_symbol_relation_get_roles(value))
116+
public lazy var symbol: Symbol = Symbol(indexstoredb_symbol_relation_get_symbol(value))
117+
118+
public init(_ value: indexstoredb_symbol_relation_t) {
119+
self.value = value
120+
}
121+
}
122+
112123
public struct SymbolRole: OptionSet {
113124

114125
public var rawValue: UInt64
@@ -148,12 +159,45 @@ public struct SymbolRole: OptionSet {
148159
}
149160
}
150161

162+
public enum IndexSymbolKind {
163+
case unknown
164+
case module
165+
case namespace
166+
case namespaceAlias
167+
case macro
168+
case `enum`
169+
case `struct`
170+
case `class`
171+
case `protocol`
172+
case `extension`
173+
case union
174+
case `typealias`
175+
case function
176+
case variable
177+
case field
178+
case enumConstant
179+
case instanceMethod
180+
case classMethod
181+
case staticMethod
182+
case instanceProperty
183+
case classProperty
184+
case staticProperty
185+
case constructor
186+
case destructor
187+
case conversionFunction
188+
case parameter
189+
case using
190+
191+
case commentTag
192+
}
193+
151194
public final class Symbol {
152195

153196
let value: indexstoredb_symbol_t
154197

155198
public lazy var usr: String = String(cString: indexstoredb_symbol_usr(value))
156199
public lazy var name: String = String(cString: indexstoredb_symbol_name(value))
200+
public lazy var kind: IndexSymbolKind = getSymbolKind(from: indexstoredb_symbol_kind(value))
157201

158202
init(_ value: indexstoredb_symbol_t) {
159203
self.value = value
@@ -162,6 +206,70 @@ public final class Symbol {
162206
deinit {
163207
indexstoredb_release(value)
164208
}
209+
210+
func getSymbolKind(from cSymbolKind: indexstoredb_symbol_kind_t) -> IndexSymbolKind {
211+
switch cSymbolKind {
212+
case INDEXSTOREDB_SYMBOL_KIND_UNKNOWN:
213+
return .unknown
214+
case INDEXSTOREDB_SYMBOL_KIND_MODULE:
215+
return .module
216+
case INDEXSTOREDB_SYMBOL_KIND_NAMESPACE:
217+
return .namespace
218+
case INDEXSTOREDB_SYMBOL_KIND_NAMESPACEALIAS:
219+
return .namespaceAlias
220+
case INDEXSTOREDB_SYMBOL_KIND_MACRO:
221+
return .macro
222+
case INDEXSTOREDB_SYMBOL_KIND_ENUM:
223+
return .enum
224+
case INDEXSTOREDB_SYMBOL_KIND_STRUCT:
225+
return .struct
226+
case INDEXSTOREDB_SYMBOL_KIND_CLASS:
227+
return .class
228+
case INDEXSTOREDB_SYMBOL_KIND_PROTOCOL:
229+
return .protocol
230+
case INDEXSTOREDB_SYMBOL_KIND_EXTENSION:
231+
return .extension
232+
case INDEXSTOREDB_SYMBOL_KIND_UNION:
233+
return .union
234+
case INDEXSTOREDB_SYMBOL_KIND_TYPEALIAS:
235+
return .typealias
236+
case INDEXSTOREDB_SYMBOL_KIND_FUNCTION:
237+
return .function
238+
case INDEXSTOREDB_SYMBOL_KIND_VARIABLE:
239+
return .variable
240+
case INDEXSTOREDB_SYMBOL_KIND_FIELD:
241+
return .field
242+
case INDEXSTOREDB_SYMBOL_KIND_ENUMCONSTANT:
243+
return .enumConstant
244+
case INDEXSTOREDB_SYMBOL_KIND_INSTANCEMETHOD:
245+
return .instanceMethod
246+
case INDEXSTOREDB_SYMBOL_KIND_CLASSMETHOD:
247+
return .classMethod
248+
case INDEXSTOREDB_SYMBOL_KIND_STATICMETHOD:
249+
return .staticMethod
250+
case INDEXSTOREDB_SYMBOL_KIND_INSTANCEPROPERTY:
251+
return .instanceProperty
252+
case INDEXSTOREDB_SYMBOL_KIND_CLASSPROPERTY:
253+
return .classProperty
254+
case INDEXSTOREDB_SYMBOL_KIND_STATICPROPERTY:
255+
return .staticProperty
256+
case INDEXSTOREDB_SYMBOL_KIND_CONSTRUCTOR:
257+
return .constructor
258+
case INDEXSTOREDB_SYMBOL_KIND_DESTRUCTOR:
259+
return .destructor
260+
case INDEXSTOREDB_SYMBOL_KIND_CONVERSIONFUNCTION:
261+
return .conversionFunction
262+
case INDEXSTOREDB_SYMBOL_KIND_PARAMETER:
263+
return .parameter
264+
case INDEXSTOREDB_SYMBOL_KIND_USING:
265+
return .using
266+
267+
case INDEXSTOREDB_SYMBOL_KIND_COMMENTTAG:
268+
return .commentTag
269+
default:
270+
return .unknown
271+
}
272+
}
165273
}
166274

167275
public struct SymbolLocation {
@@ -192,6 +300,7 @@ public final class SymbolOccurrence {
192300
public lazy var symbol: Symbol = Symbol(indexstoredb_symbol_occurrence_symbol(value))
193301
public lazy var roles: SymbolRole = SymbolRole(rawValue: indexstoredb_symbol_occurrence_roles(value))
194302
public lazy var location: SymbolLocation = SymbolLocation(indexstoredb_symbol_occurrence_location(value))
303+
public lazy var relations: [SymbolRelation] = getRelations()
195304

196305
init(_ value: indexstoredb_symbol_occurrence_t) {
197306
self.value = value
@@ -200,6 +309,23 @@ public final class SymbolOccurrence {
200309
deinit {
201310
indexstoredb_release(value)
202311
}
312+
313+
private func getRelations() -> [SymbolRelation] {
314+
var relations: [SymbolRelation] = []
315+
forEachRelation{relation in
316+
relations.append(relation)
317+
return true
318+
}
319+
return relations
320+
}
321+
322+
@discardableResult public func forEachRelation(
323+
body: @escaping (SymbolRelation) -> Bool
324+
) -> Bool {
325+
return indexstoredb_symbol_occurrence_relations(value){ relation in
326+
body(SymbolRelation(relation))
327+
}
328+
}
203329
}
204330

205331
public protocol IndexStoreLibraryProvider {

include/CIndexStoreDB/CIndexStoreDB.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ typedef indexstoredb_object_t indexstoredb_symbol_occurrence_t;
5252

5353
typedef void *indexstoredb_error_t;
5454
typedef void *indexstoredb_symbol_location_t;
55+
typedef void *indexstoredb_symbol_relation_t;
5556

5657
typedef enum {
5758
INDEXSTOREDB_SYMBOL_ROLE_DECLARATION = 1 << 0,
@@ -79,6 +80,38 @@ typedef enum {
7980
INDEXSTOREDB_SYMBOL_ROLE_CANONICAL = 1 << 63,
8081
} indexstoredb_symbol_role_t;
8182

83+
typedef enum {
84+
INDEXSTOREDB_SYMBOL_KIND_UNKNOWN = 0,
85+
INDEXSTOREDB_SYMBOL_KIND_MODULE = 1,
86+
INDEXSTOREDB_SYMBOL_KIND_NAMESPACE = 2,
87+
INDEXSTOREDB_SYMBOL_KIND_NAMESPACEALIAS = 3,
88+
INDEXSTOREDB_SYMBOL_KIND_MACRO = 4,
89+
INDEXSTOREDB_SYMBOL_KIND_ENUM = 5,
90+
INDEXSTOREDB_SYMBOL_KIND_STRUCT = 6,
91+
INDEXSTOREDB_SYMBOL_KIND_CLASS = 7,
92+
INDEXSTOREDB_SYMBOL_KIND_PROTOCOL = 8,
93+
INDEXSTOREDB_SYMBOL_KIND_EXTENSION = 9,
94+
INDEXSTOREDB_SYMBOL_KIND_UNION = 10,
95+
INDEXSTOREDB_SYMBOL_KIND_TYPEALIAS = 11,
96+
INDEXSTOREDB_SYMBOL_KIND_FUNCTION = 12,
97+
INDEXSTOREDB_SYMBOL_KIND_VARIABLE = 13,
98+
INDEXSTOREDB_SYMBOL_KIND_FIELD = 14,
99+
INDEXSTOREDB_SYMBOL_KIND_ENUMCONSTANT = 15,
100+
INDEXSTOREDB_SYMBOL_KIND_INSTANCEMETHOD = 16,
101+
INDEXSTOREDB_SYMBOL_KIND_CLASSMETHOD = 17,
102+
INDEXSTOREDB_SYMBOL_KIND_STATICMETHOD = 18,
103+
INDEXSTOREDB_SYMBOL_KIND_INSTANCEPROPERTY = 19,
104+
INDEXSTOREDB_SYMBOL_KIND_CLASSPROPERTY = 20,
105+
INDEXSTOREDB_SYMBOL_KIND_STATICPROPERTY = 21,
106+
INDEXSTOREDB_SYMBOL_KIND_CONSTRUCTOR = 22,
107+
INDEXSTOREDB_SYMBOL_KIND_DESTRUCTOR = 23,
108+
INDEXSTOREDB_SYMBOL_KIND_CONVERSIONFUNCTION = 24,
109+
INDEXSTOREDB_SYMBOL_KIND_PARAMETER = 25,
110+
INDEXSTOREDB_SYMBOL_KIND_USING = 26,
111+
112+
INDEXSTOREDB_SYMBOL_KIND_COMMENTTAG = 1000,
113+
} indexstoredb_symbol_kind_t;
114+
82115
/// Returns true on success.
83116
typedef _Nullable indexstoredb_indexstore_library_t(^indexstore_library_provider_t)(const char * _Nonnull);
84117

@@ -201,6 +234,30 @@ indexstoredb_index_canonical_symbol_occurences_containing_pattern(
201234
bool ignoreCase,
202235
_Nonnull indexstoredb_symbol_occurrence_receiver_t receiver);
203236

237+
/// Gets the set of roles of the passed in symbol relation
238+
/// @param relation A symbol relation
239+
INDEXSTOREDB_PUBLIC uint64_t
240+
indexstoredb_symbol_relation_get_roles(_Nonnull indexstoredb_symbol_relation_t);
241+
242+
/// Gets the symbol associated with the passed in relation
243+
/// @param relation A symbol relation
244+
INDEXSTOREDB_PUBLIC _Nonnull indexstoredb_symbol_t
245+
indexstoredb_symbol_relation_get_symbol(_Nonnull indexstoredb_symbol_relation_t);
246+
247+
/// Loops through each relation that a passed in symbol has, and performs the passed in function.
248+
/// The relations are owned by the occurrence and shall not be used after the occurrence is freed.
249+
/// @param occurrence The symbol occurrence that whose relations should be found.
250+
/// @param applier The function that should be performed on each symbol relation.
251+
/// The function should return a boolean indicating whether the looping should continue.
252+
INDEXSTOREDB_PUBLIC bool
253+
indexstoredb_symbol_occurrence_relations(_Nonnull indexstoredb_symbol_occurrence_t,
254+
bool(^ _Nonnull applier)(indexstoredb_symbol_relation_t _Nonnull ));
255+
256+
/// Get the SymbolKind of a Symbol
257+
/// @param symbol The symbol whose kind should be found.
258+
INDEXSTOREDB_PUBLIC indexstoredb_symbol_kind_t
259+
indexstoredb_symbol_kind(_Nonnull indexstoredb_symbol_t);
260+
204261
INDEXSTOREDB_END_DECLS
205262

206263
#endif

lib/CIndexStoreDB/CIndexStoreDB.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ indexstoredb_symbol_name(indexstoredb_symbol_t symbol) {
144144
return obj->value->getName().c_str();
145145
}
146146

147+
indexstoredb_symbol_kind_t
148+
indexstoredb_symbol_kind(indexstoredb_symbol_t symbol) {
149+
auto symbolObj = (IndexStoreDBObject<std::shared_ptr<Symbol>> *)symbol;
150+
return (indexstoredb_symbol_kind_t) symbolObj->value->getSymbolKind();
151+
}
152+
147153
bool
148154
indexstoredb_index_symbol_names(indexstoredb_index_t index, indexstoredb_symbol_name_receiver receiver) {
149155
auto obj = (IndexStoreDBObject<std::shared_ptr<IndexSystem>> *)index;
@@ -193,6 +199,31 @@ indexstoredb_symbol_occurrence_symbol(indexstoredb_symbol_occurrence_t occur) {
193199
return make_object(obj->value->getSymbol());
194200
}
195201

202+
uint64_t
203+
indexstoredb_symbol_relation_get_roles(indexstoredb_symbol_relation_t relation) {
204+
auto relationObj = (IndexStoreDBObject<SymbolRelation> *)relation;
205+
return relationObj->value.getRoles().toRaw();
206+
}
207+
208+
indexstoredb_symbol_t
209+
indexstoredb_symbol_relation_get_symbol(indexstoredb_symbol_relation_t relation) {
210+
auto relationObj = (IndexStoreDBObject<SymbolRelation> *)relation;
211+
return make_object(relationObj->value.getSymbol());
212+
}
213+
214+
bool
215+
indexstoredb_symbol_occurrence_relations(indexstoredb_symbol_occurrence_t occurrence,
216+
bool(^applier)(indexstoredb_symbol_relation_t)) {
217+
auto occurrenceObj = (IndexStoreDBObject<SymbolOccurrenceRef> *)occurrence;
218+
ArrayRef<SymbolRelation> relations = occurrenceObj->value->getRelations();
219+
for (SymbolRelation rel : relations) {
220+
if(!applier(make_object(rel))) {
221+
return false;
222+
}
223+
}
224+
return true;
225+
}
226+
196227
uint64_t
197228
indexstoredb_symbol_occurrence_roles(indexstoredb_symbol_occurrence_t occur) {
198229
auto obj = (IndexStoreDBObject<SymbolOccurrenceRef> *)occur;

0 commit comments

Comments
 (0)