2018-02-25 22:53:57 +00:00
|
|
|
#include "message_handler.h"
|
|
|
|
#include "query_utils.h"
|
|
|
|
#include "queue_manager.h"
|
|
|
|
|
|
|
|
#include <loguru.hpp>
|
|
|
|
|
|
|
|
namespace {
|
2018-03-22 04:05:25 +00:00
|
|
|
|
|
|
|
MethodType kMethodType = "$cquery/callHierarchy";
|
|
|
|
|
2018-03-20 02:51:42 +00:00
|
|
|
enum class CallType : uint8_t {
|
|
|
|
Direct = 0,
|
|
|
|
Base = 1,
|
|
|
|
Derived = 2,
|
|
|
|
All = 1 | 2
|
|
|
|
};
|
2018-02-26 00:05:03 +00:00
|
|
|
MAKE_REFLECT_TYPE_PROXY(CallType);
|
|
|
|
|
|
|
|
bool operator&(CallType lhs, CallType rhs) {
|
|
|
|
return uint8_t(lhs) & uint8_t(rhs);
|
|
|
|
}
|
|
|
|
|
2018-03-22 05:01:21 +00:00
|
|
|
struct In_CqueryCallHierarchy : public RequestInMessage {
|
2018-03-22 04:05:25 +00:00
|
|
|
MethodType GetMethodType() const override { return kMethodType; }
|
|
|
|
|
2018-02-25 22:53:57 +00:00
|
|
|
struct Params {
|
2018-02-28 05:56:40 +00:00
|
|
|
// If id is specified, expand a node; otherwise textDocument+position should
|
|
|
|
// be specified for building the root and |levels| of nodes below.
|
2018-02-25 22:53:57 +00:00
|
|
|
lsTextDocumentIdentifier textDocument;
|
|
|
|
lsPosition position;
|
2018-02-28 05:56:40 +00:00
|
|
|
|
|
|
|
Maybe<QueryFuncId> id;
|
|
|
|
|
2018-02-26 00:05:03 +00:00
|
|
|
// true: callee tree (functions called by this function); false: caller tree
|
|
|
|
// (where this function is called)
|
2018-02-25 22:53:57 +00:00
|
|
|
bool callee = false;
|
2018-02-26 00:05:03 +00:00
|
|
|
// Base: include base functions; All: include both base and derived
|
|
|
|
// functions.
|
2018-02-28 07:08:39 +00:00
|
|
|
CallType callType = CallType::All;
|
2018-02-25 23:24:51 +00:00
|
|
|
bool detailedName = false;
|
2018-02-25 22:53:57 +00:00
|
|
|
int levels = 1;
|
|
|
|
};
|
|
|
|
Params params;
|
2018-03-22 04:05:25 +00:00
|
|
|
|
2018-02-25 22:53:57 +00:00
|
|
|
};
|
2018-03-22 04:05:25 +00:00
|
|
|
MAKE_REFLECT_STRUCT(In_CqueryCallHierarchy::Params,
|
2018-02-25 22:53:57 +00:00
|
|
|
textDocument,
|
|
|
|
position,
|
2018-02-25 23:24:51 +00:00
|
|
|
id,
|
|
|
|
callee,
|
2018-02-26 00:05:03 +00:00
|
|
|
callType,
|
2018-02-25 23:24:51 +00:00
|
|
|
detailedName,
|
|
|
|
levels);
|
2018-03-22 04:05:25 +00:00
|
|
|
MAKE_REFLECT_STRUCT(In_CqueryCallHierarchy, id, params);
|
|
|
|
REGISTER_IN_MESSAGE(In_CqueryCallHierarchy);
|
2018-02-25 22:53:57 +00:00
|
|
|
|
|
|
|
struct Out_CqueryCallHierarchy : public lsOutMessage<Out_CqueryCallHierarchy> {
|
|
|
|
struct Entry {
|
|
|
|
QueryFuncId id;
|
|
|
|
std::string_view name;
|
|
|
|
lsLocation location;
|
|
|
|
CallType callType = CallType::Direct;
|
|
|
|
int numChildren;
|
|
|
|
// Empty if the |levels| limit is reached.
|
|
|
|
std::vector<Entry> children;
|
|
|
|
};
|
|
|
|
|
|
|
|
lsRequestId id;
|
|
|
|
optional<Entry> result;
|
|
|
|
};
|
|
|
|
MAKE_REFLECT_STRUCT(Out_CqueryCallHierarchy::Entry,
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
location,
|
|
|
|
callType,
|
|
|
|
numChildren,
|
|
|
|
children);
|
|
|
|
MAKE_REFLECT_STRUCT(Out_CqueryCallHierarchy, jsonrpc, id, result);
|
|
|
|
|
2018-02-26 03:10:02 +00:00
|
|
|
bool Expand(MessageHandler* m,
|
2018-02-25 22:53:57 +00:00
|
|
|
Out_CqueryCallHierarchy::Entry* entry,
|
|
|
|
bool callee,
|
2018-02-26 00:05:03 +00:00
|
|
|
CallType call_type,
|
2018-02-25 23:24:51 +00:00
|
|
|
bool detailed_name,
|
2018-02-25 22:53:57 +00:00
|
|
|
int levels) {
|
|
|
|
const QueryFunc& func = m->db->funcs[entry->id.id];
|
|
|
|
const QueryFunc::Def* def = func.AnyDef();
|
|
|
|
entry->numChildren = 0;
|
|
|
|
if (!def)
|
2018-02-26 03:10:02 +00:00
|
|
|
return false;
|
2018-02-25 22:53:57 +00:00
|
|
|
auto handle = [&](Use use, CallType call_type) {
|
2018-02-26 00:05:03 +00:00
|
|
|
entry->numChildren++;
|
|
|
|
if (levels > 0) {
|
2018-02-26 03:10:02 +00:00
|
|
|
Out_CqueryCallHierarchy::Entry entry1;
|
|
|
|
entry1.id = QueryFuncId(use.id);
|
2018-03-01 01:53:43 +00:00
|
|
|
if (auto loc = GetLsLocation(m->db, m->working_files, use))
|
|
|
|
entry1.location = *loc;
|
2018-02-26 03:10:02 +00:00
|
|
|
entry1.callType = call_type;
|
|
|
|
if (Expand(m, &entry1, callee, call_type, detailed_name, levels - 1))
|
|
|
|
entry->children.push_back(std::move(entry1));
|
2018-02-25 22:53:57 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
auto handle_uses = [&](const QueryFunc& func, CallType call_type) {
|
|
|
|
if (callee) {
|
|
|
|
if (const auto* def = func.AnyDef())
|
|
|
|
for (SymbolRef ref : def->callees)
|
|
|
|
if (ref.kind == SymbolKind::Func)
|
2018-03-20 02:51:42 +00:00
|
|
|
handle(Use(ref.range, ref.id, ref.kind, ref.role, def->file),
|
|
|
|
call_type);
|
2018-02-25 22:53:57 +00:00
|
|
|
} else {
|
|
|
|
for (Use use : func.uses)
|
|
|
|
if (use.kind == SymbolKind::Func)
|
|
|
|
handle(use, call_type);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::unordered_set<Usr> seen;
|
2018-02-27 01:23:45 +00:00
|
|
|
seen.insert(func.usr);
|
2018-02-25 22:53:57 +00:00
|
|
|
std::vector<const QueryFunc*> stack;
|
2018-02-26 03:10:02 +00:00
|
|
|
if (detailed_name)
|
|
|
|
entry->name = def->detailed_name;
|
|
|
|
else
|
|
|
|
entry->name = def->ShortName();
|
2018-02-25 22:53:57 +00:00
|
|
|
handle_uses(func, CallType::Direct);
|
|
|
|
|
|
|
|
// Callers/callees of base functions.
|
2018-02-26 00:05:03 +00:00
|
|
|
if (call_type & CallType::Base) {
|
|
|
|
stack.push_back(&func);
|
|
|
|
while (stack.size()) {
|
|
|
|
const QueryFunc& func1 = *stack.back();
|
|
|
|
stack.pop_back();
|
|
|
|
if (auto* def1 = func1.AnyDef()) {
|
2018-02-26 02:45:46 +00:00
|
|
|
EachDefinedEntity(m->db->funcs, def1->bases, [&](QueryFunc& func2) {
|
2018-02-26 00:05:03 +00:00
|
|
|
if (!seen.count(func2.usr)) {
|
|
|
|
seen.insert(func2.usr);
|
|
|
|
stack.push_back(&func2);
|
|
|
|
handle_uses(func2, CallType::Base);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Callers/callees of derived functions.
|
|
|
|
if (call_type & CallType::Derived) {
|
|
|
|
stack.push_back(&func);
|
|
|
|
while (stack.size()) {
|
|
|
|
const QueryFunc& func1 = *stack.back();
|
|
|
|
stack.pop_back();
|
|
|
|
EachDefinedEntity(m->db->funcs, func1.derived, [&](QueryFunc& func2) {
|
|
|
|
if (!seen.count(func2.usr)) {
|
2018-02-25 22:53:57 +00:00
|
|
|
seen.insert(func2.usr);
|
|
|
|
stack.push_back(&func2);
|
2018-02-26 00:05:03 +00:00
|
|
|
handle_uses(func2, CallType::Derived);
|
2018-02-25 22:53:57 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2018-02-26 03:10:02 +00:00
|
|
|
return true;
|
2018-02-25 22:53:57 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 04:05:25 +00:00
|
|
|
struct Handler_CqueryCallHierarchy
|
|
|
|
: BaseMessageHandler<In_CqueryCallHierarchy> {
|
|
|
|
MethodType GetMethodType() const override { return kMethodType; }
|
|
|
|
|
2018-02-25 22:53:57 +00:00
|
|
|
optional<Out_CqueryCallHierarchy::Entry> BuildInitial(QueryFuncId root_id,
|
|
|
|
bool callee,
|
2018-02-26 00:05:03 +00:00
|
|
|
CallType call_type,
|
2018-02-25 23:24:51 +00:00
|
|
|
bool detailed_name,
|
2018-02-25 22:53:57 +00:00
|
|
|
int levels) {
|
|
|
|
const auto* def = db->funcs[root_id.id].AnyDef();
|
|
|
|
if (!def)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
Out_CqueryCallHierarchy::Entry entry;
|
|
|
|
entry.id = root_id;
|
2018-02-26 03:10:02 +00:00
|
|
|
entry.callType = CallType::Direct;
|
2018-03-01 01:53:43 +00:00
|
|
|
if (def->spell) {
|
|
|
|
if (optional<lsLocation> loc =
|
2018-03-20 02:51:42 +00:00
|
|
|
GetLsLocation(db, working_files, *def->spell))
|
2018-03-01 01:53:43 +00:00
|
|
|
entry.location = *loc;
|
|
|
|
}
|
2018-02-26 00:05:03 +00:00
|
|
|
Expand(this, &entry, callee, call_type, detailed_name, levels);
|
2018-02-25 22:53:57 +00:00
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
2018-03-22 04:05:25 +00:00
|
|
|
void Run(In_CqueryCallHierarchy* request) override {
|
2018-02-25 22:53:57 +00:00
|
|
|
const auto& params = request->params;
|
|
|
|
Out_CqueryCallHierarchy out;
|
|
|
|
out.id = request->id;
|
|
|
|
|
|
|
|
if (params.id) {
|
|
|
|
Out_CqueryCallHierarchy::Entry entry;
|
|
|
|
entry.id = *params.id;
|
2018-02-26 03:10:02 +00:00
|
|
|
entry.callType = CallType::Direct;
|
2018-02-25 22:53:57 +00:00
|
|
|
if (entry.id.id < db->funcs.size())
|
2018-02-26 00:05:03 +00:00
|
|
|
Expand(this, &entry, params.callee, params.callType,
|
|
|
|
params.detailedName, params.levels);
|
2018-02-25 22:53:57 +00:00
|
|
|
out.result = std::move(entry);
|
2018-02-28 05:56:40 +00:00
|
|
|
} else {
|
|
|
|
QueryFile* file;
|
|
|
|
if (!FindFileOrFail(db, project, request->id,
|
|
|
|
params.textDocument.uri.GetPath(), &file))
|
|
|
|
return;
|
|
|
|
WorkingFile* working_file =
|
|
|
|
working_files->GetFileByFilename(file->def->path);
|
|
|
|
for (SymbolRef sym :
|
2018-03-20 02:51:42 +00:00
|
|
|
FindSymbolsAtLocation(working_file, file, params.position)) {
|
2018-02-28 05:56:40 +00:00
|
|
|
if (sym.kind == SymbolKind::Func) {
|
|
|
|
out.result =
|
|
|
|
BuildInitial(QueryFuncId(sym.id), params.callee, params.callType,
|
2018-03-20 02:51:42 +00:00
|
|
|
params.detailedName, params.levels);
|
2018-02-28 05:56:40 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-02-25 22:53:57 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 04:05:25 +00:00
|
|
|
QueueManager::WriteStdout(kMethodType, out);
|
2018-02-25 22:53:57 +00:00
|
|
|
}
|
|
|
|
};
|
2018-03-22 04:05:25 +00:00
|
|
|
REGISTER_MESSAGE_HANDLER(Handler_CqueryCallHierarchy);
|
2018-02-28 05:56:40 +00:00
|
|
|
|
2018-02-25 22:53:57 +00:00
|
|
|
} // namespace
|