Skip to content

Add SymbolRelation class with roles and symbols #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions Sources/IndexStoreDB/IndexStoreDB.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ public final class IndexStoreDB {

}

public final class SymbolRelation {
let value: indexstoredb_symbol_relation_t

public lazy var roles: SymbolRole = SymbolRole(rawValue: indexstoredb_symbol_relation_get_roles(value))
public lazy var symbol: Symbol = Symbol(indexstoredb_symbol_relation_get_symbol(value))

public init(_ value: indexstoredb_symbol_relation_t) {
self.value = value
}
}

public struct SymbolRole: OptionSet {

public var rawValue: UInt64
Expand Down Expand Up @@ -148,12 +159,45 @@ public struct SymbolRole: OptionSet {
}
}

public enum IndexSymbolKind {
case unknown
case module
case namespace
case namespaceAlias
case macro
case `enum`
case `struct`
case `class`
case `protocol`
case `extension`
case union
case `typealias`
case function
case variable
case field
case enumConstant
case instanceMethod
case classMethod
case staticMethod
case instanceProperty
case classProperty
case staticProperty
case constructor
case destructor
case conversionFunction
case parameter
case using

case commentTag
}

public final class Symbol {

let value: indexstoredb_symbol_t

public lazy var usr: String = String(cString: indexstoredb_symbol_usr(value))
public lazy var name: String = String(cString: indexstoredb_symbol_name(value))
public lazy var kind: IndexSymbolKind = getSymbolKind(from: indexstoredb_symbol_kind(value))

init(_ value: indexstoredb_symbol_t) {
self.value = value
Expand All @@ -162,6 +206,70 @@ public final class Symbol {
deinit {
indexstoredb_release(value)
}

func getSymbolKind(from cSymbolKind: indexstoredb_symbol_kind_t) -> IndexSymbolKind {
switch cSymbolKind {
case INDEXSTOREDB_SYMBOL_KIND_UNKNOWN:
return .unknown
case INDEXSTOREDB_SYMBOL_KIND_MODULE:
return .module
case INDEXSTOREDB_SYMBOL_KIND_NAMESPACE:
return .namespace
case INDEXSTOREDB_SYMBOL_KIND_NAMESPACEALIAS:
return .namespaceAlias
case INDEXSTOREDB_SYMBOL_KIND_MACRO:
return .macro
case INDEXSTOREDB_SYMBOL_KIND_ENUM:
return .enum
case INDEXSTOREDB_SYMBOL_KIND_STRUCT:
return .struct
case INDEXSTOREDB_SYMBOL_KIND_CLASS:
return .class
case INDEXSTOREDB_SYMBOL_KIND_PROTOCOL:
return .protocol
case INDEXSTOREDB_SYMBOL_KIND_EXTENSION:
return .extension
case INDEXSTOREDB_SYMBOL_KIND_UNION:
return .union
case INDEXSTOREDB_SYMBOL_KIND_TYPEALIAS:
return .typealias
case INDEXSTOREDB_SYMBOL_KIND_FUNCTION:
return .function
case INDEXSTOREDB_SYMBOL_KIND_VARIABLE:
return .variable
case INDEXSTOREDB_SYMBOL_KIND_FIELD:
return .field
case INDEXSTOREDB_SYMBOL_KIND_ENUMCONSTANT:
return .enumConstant
case INDEXSTOREDB_SYMBOL_KIND_INSTANCEMETHOD:
return .instanceMethod
case INDEXSTOREDB_SYMBOL_KIND_CLASSMETHOD:
return .classMethod
case INDEXSTOREDB_SYMBOL_KIND_STATICMETHOD:
return .staticMethod
case INDEXSTOREDB_SYMBOL_KIND_INSTANCEPROPERTY:
return .instanceProperty
case INDEXSTOREDB_SYMBOL_KIND_CLASSPROPERTY:
return .classProperty
case INDEXSTOREDB_SYMBOL_KIND_STATICPROPERTY:
return .staticProperty
case INDEXSTOREDB_SYMBOL_KIND_CONSTRUCTOR:
return .constructor
case INDEXSTOREDB_SYMBOL_KIND_DESTRUCTOR:
return .destructor
case INDEXSTOREDB_SYMBOL_KIND_CONVERSIONFUNCTION:
return .conversionFunction
case INDEXSTOREDB_SYMBOL_KIND_PARAMETER:
return .parameter
case INDEXSTOREDB_SYMBOL_KIND_USING:
return .using

case INDEXSTOREDB_SYMBOL_KIND_COMMENTTAG:
return .commentTag
default:
return .unknown
}
}
}

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

init(_ value: indexstoredb_symbol_occurrence_t) {
self.value = value
Expand All @@ -200,6 +309,23 @@ public final class SymbolOccurrence {
deinit {
indexstoredb_release(value)
}

private func getRelations() -> [SymbolRelation] {
var relations: [SymbolRelation] = []
forEachRelation{relation in
relations.append(relation)
return true
}
return relations
}

@discardableResult public func forEachRelation(
body: @escaping (SymbolRelation) -> Bool
) -> Bool {
return indexstoredb_symbol_occurrence_relations(value){ relation in
body(SymbolRelation(relation))
}
}
}

public protocol IndexStoreLibraryProvider {
Expand Down
57 changes: 57 additions & 0 deletions include/CIndexStoreDB/CIndexStoreDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ typedef indexstoredb_object_t indexstoredb_symbol_occurrence_t;

typedef void *indexstoredb_error_t;
typedef void *indexstoredb_symbol_location_t;
typedef void *indexstoredb_symbol_relation_t;

typedef enum {
INDEXSTOREDB_SYMBOL_ROLE_DECLARATION = 1 << 0,
Expand Down Expand Up @@ -79,6 +80,38 @@ typedef enum {
INDEXSTOREDB_SYMBOL_ROLE_CANONICAL = 1 << 63,
} indexstoredb_symbol_role_t;

typedef enum {
INDEXSTOREDB_SYMBOL_KIND_UNKNOWN = 0,
INDEXSTOREDB_SYMBOL_KIND_MODULE = 1,
INDEXSTOREDB_SYMBOL_KIND_NAMESPACE = 2,
INDEXSTOREDB_SYMBOL_KIND_NAMESPACEALIAS = 3,
INDEXSTOREDB_SYMBOL_KIND_MACRO = 4,
INDEXSTOREDB_SYMBOL_KIND_ENUM = 5,
INDEXSTOREDB_SYMBOL_KIND_STRUCT = 6,
INDEXSTOREDB_SYMBOL_KIND_CLASS = 7,
INDEXSTOREDB_SYMBOL_KIND_PROTOCOL = 8,
INDEXSTOREDB_SYMBOL_KIND_EXTENSION = 9,
INDEXSTOREDB_SYMBOL_KIND_UNION = 10,
INDEXSTOREDB_SYMBOL_KIND_TYPEALIAS = 11,
INDEXSTOREDB_SYMBOL_KIND_FUNCTION = 12,
INDEXSTOREDB_SYMBOL_KIND_VARIABLE = 13,
INDEXSTOREDB_SYMBOL_KIND_FIELD = 14,
INDEXSTOREDB_SYMBOL_KIND_ENUMCONSTANT = 15,
INDEXSTOREDB_SYMBOL_KIND_INSTANCEMETHOD = 16,
INDEXSTOREDB_SYMBOL_KIND_CLASSMETHOD = 17,
INDEXSTOREDB_SYMBOL_KIND_STATICMETHOD = 18,
INDEXSTOREDB_SYMBOL_KIND_INSTANCEPROPERTY = 19,
INDEXSTOREDB_SYMBOL_KIND_CLASSPROPERTY = 20,
INDEXSTOREDB_SYMBOL_KIND_STATICPROPERTY = 21,
INDEXSTOREDB_SYMBOL_KIND_CONSTRUCTOR = 22,
INDEXSTOREDB_SYMBOL_KIND_DESTRUCTOR = 23,
INDEXSTOREDB_SYMBOL_KIND_CONVERSIONFUNCTION = 24,
INDEXSTOREDB_SYMBOL_KIND_PARAMETER = 25,
INDEXSTOREDB_SYMBOL_KIND_USING = 26,

INDEXSTOREDB_SYMBOL_KIND_COMMENTTAG = 1000,
} indexstoredb_symbol_kind_t;

/// Returns true on success.
typedef _Nullable indexstoredb_indexstore_library_t(^indexstore_library_provider_t)(const char * _Nonnull);

Expand Down Expand Up @@ -201,6 +234,30 @@ indexstoredb_index_canonical_symbol_occurences_containing_pattern(
bool ignoreCase,
_Nonnull indexstoredb_symbol_occurrence_receiver_t receiver);

/// Gets the set of roles of the passed in symbol relation
/// @param relation A symbol relation
INDEXSTOREDB_PUBLIC uint64_t
indexstoredb_symbol_relation_get_roles(_Nonnull indexstoredb_symbol_relation_t);

/// Gets the symbol associated with the passed in relation
/// @param relation A symbol relation
INDEXSTOREDB_PUBLIC _Nonnull indexstoredb_symbol_t
indexstoredb_symbol_relation_get_symbol(_Nonnull indexstoredb_symbol_relation_t);

/// Loops through each relation that a passed in symbol has, and performs the passed in function.
/// The relations are owned by the occurrence and shall not be used after the occurrence is freed.
/// @param occurrence The symbol occurrence that whose relations should be found.
/// @param applier The function that should be performed on each symbol relation.
/// The function should return a boolean indicating whether the looping should continue.
INDEXSTOREDB_PUBLIC bool
indexstoredb_symbol_occurrence_relations(_Nonnull indexstoredb_symbol_occurrence_t,
bool(^ _Nonnull applier)(indexstoredb_symbol_relation_t _Nonnull ));

/// Get the SymbolKind of a Symbol
/// @param symbol The symbol whose kind should be found.
INDEXSTOREDB_PUBLIC indexstoredb_symbol_kind_t
indexstoredb_symbol_kind(_Nonnull indexstoredb_symbol_t);

INDEXSTOREDB_END_DECLS

#endif
31 changes: 31 additions & 0 deletions lib/CIndexStoreDB/CIndexStoreDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ indexstoredb_symbol_name(indexstoredb_symbol_t symbol) {
return obj->value->getName().c_str();
}

indexstoredb_symbol_kind_t
indexstoredb_symbol_kind(indexstoredb_symbol_t symbol) {
auto symbolObj = (IndexStoreDBObject<std::shared_ptr<Symbol>> *)symbol;
return (indexstoredb_symbol_kind_t) symbolObj->value->getSymbolKind();
}

bool
indexstoredb_index_symbol_names(indexstoredb_index_t index, indexstoredb_symbol_name_receiver receiver) {
auto obj = (IndexStoreDBObject<std::shared_ptr<IndexSystem>> *)index;
Expand Down Expand Up @@ -193,6 +199,31 @@ indexstoredb_symbol_occurrence_symbol(indexstoredb_symbol_occurrence_t occur) {
return make_object(obj->value->getSymbol());
}

uint64_t
indexstoredb_symbol_relation_get_roles(indexstoredb_symbol_relation_t relation) {
auto relationObj = (IndexStoreDBObject<SymbolRelation> *)relation;
return relationObj->value.getRoles().toRaw();
}

indexstoredb_symbol_t
indexstoredb_symbol_relation_get_symbol(indexstoredb_symbol_relation_t relation) {
auto relationObj = (IndexStoreDBObject<SymbolRelation> *)relation;
return make_object(relationObj->value.getSymbol());
}

bool
indexstoredb_symbol_occurrence_relations(indexstoredb_symbol_occurrence_t occurrence,
bool(^applier)(indexstoredb_symbol_relation_t)) {
auto occurrenceObj = (IndexStoreDBObject<SymbolOccurrenceRef> *)occurrence;
ArrayRef<SymbolRelation> relations = occurrenceObj->value->getRelations();
for (SymbolRelation rel : relations) {
if(!applier(make_object(rel))) {
return false;
}
}
return true;
}

uint64_t
indexstoredb_symbol_occurrence_roles(indexstoredb_symbol_occurrence_t occur) {
auto obj = (IndexStoreDBObject<SymbolOccurrenceRef> *)occur;
Expand Down