2018-01-18 07:14:29 +00:00
|
|
|
#include "message_handler.h"
|
|
|
|
#include "query_utils.h"
|
|
|
|
#include "queue_manager.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct Ipc_CqueryMemberHierarchyInitial
|
2018-01-19 09:01:56 +00:00
|
|
|
: public RequestMessage<Ipc_CqueryMemberHierarchyInitial> {
|
2018-01-18 07:14:29 +00:00
|
|
|
const static IpcId kIpcId = IpcId::CqueryMemberHierarchyInitial;
|
2018-02-25 17:23:38 +00:00
|
|
|
struct Params {
|
|
|
|
lsTextDocumentIdentifier textDocument;
|
|
|
|
lsPosition position;
|
2018-02-25 23:24:51 +00:00
|
|
|
bool detailedName = false;
|
2018-02-25 17:23:38 +00:00
|
|
|
int levels = 1;
|
|
|
|
};
|
|
|
|
Params params;
|
2018-01-18 07:14:29 +00:00
|
|
|
};
|
2018-02-25 17:23:38 +00:00
|
|
|
|
|
|
|
MAKE_REFLECT_STRUCT(Ipc_CqueryMemberHierarchyInitial::Params,
|
|
|
|
textDocument,
|
|
|
|
position,
|
2018-02-25 23:24:51 +00:00
|
|
|
detailedName,
|
2018-02-25 17:23:38 +00:00
|
|
|
levels);
|
2018-01-18 07:14:29 +00:00
|
|
|
MAKE_REFLECT_STRUCT(Ipc_CqueryMemberHierarchyInitial, id, params);
|
|
|
|
REGISTER_IPC_MESSAGE(Ipc_CqueryMemberHierarchyInitial);
|
|
|
|
|
|
|
|
struct Ipc_CqueryMemberHierarchyExpand
|
2018-01-19 09:01:56 +00:00
|
|
|
: public RequestMessage<Ipc_CqueryMemberHierarchyExpand> {
|
2018-01-18 07:14:29 +00:00
|
|
|
const static IpcId kIpcId = IpcId::CqueryMemberHierarchyExpand;
|
|
|
|
struct Params {
|
2018-02-25 17:23:38 +00:00
|
|
|
Maybe<QueryTypeId> id;
|
2018-02-25 23:24:51 +00:00
|
|
|
bool detailedName = false;
|
2018-02-25 17:23:38 +00:00
|
|
|
int levels = 1;
|
2018-01-18 08:09:13 +00:00
|
|
|
};
|
|
|
|
Params params;
|
2018-01-18 07:14:29 +00:00
|
|
|
};
|
2018-02-25 23:24:51 +00:00
|
|
|
MAKE_REFLECT_STRUCT(Ipc_CqueryMemberHierarchyExpand::Params, id, detailedName, levels);
|
2018-01-18 07:14:29 +00:00
|
|
|
MAKE_REFLECT_STRUCT(Ipc_CqueryMemberHierarchyExpand, id, params);
|
|
|
|
REGISTER_IPC_MESSAGE(Ipc_CqueryMemberHierarchyExpand);
|
|
|
|
|
|
|
|
struct Out_CqueryMemberHierarchy
|
|
|
|
: public lsOutMessage<Out_CqueryMemberHierarchy> {
|
|
|
|
struct Entry {
|
2018-02-25 17:23:38 +00:00
|
|
|
QueryTypeId id;
|
2018-01-31 08:35:04 +00:00
|
|
|
std::string_view name;
|
2018-02-26 01:03:24 +00:00
|
|
|
std::string fieldName;
|
2018-01-18 07:14:29 +00:00
|
|
|
lsLocation location;
|
2018-02-25 22:53:57 +00:00
|
|
|
// For unexpanded nodes, this is an upper bound because some entities may be
|
|
|
|
// undefined. If it is 0, there are no members.
|
2018-02-27 01:23:45 +00:00
|
|
|
int numChildren = 0;
|
2018-02-25 17:23:38 +00:00
|
|
|
// Empty if the |levels| limit is reached.
|
|
|
|
std::vector<Entry> children;
|
2018-01-18 07:14:29 +00:00
|
|
|
};
|
|
|
|
lsRequestId id;
|
2018-02-25 17:23:38 +00:00
|
|
|
optional<Entry> result;
|
2018-01-18 07:14:29 +00:00
|
|
|
};
|
2018-02-25 17:23:38 +00:00
|
|
|
MAKE_REFLECT_STRUCT(Out_CqueryMemberHierarchy::Entry,
|
|
|
|
id,
|
|
|
|
name,
|
2018-02-26 00:05:03 +00:00
|
|
|
fieldName,
|
2018-02-25 17:23:38 +00:00
|
|
|
location,
|
|
|
|
numChildren,
|
|
|
|
children);
|
2018-01-18 07:14:29 +00:00
|
|
|
MAKE_REFLECT_STRUCT(Out_CqueryMemberHierarchy, jsonrpc, id, result);
|
|
|
|
|
2018-02-27 04:31:08 +00:00
|
|
|
bool Expand(MessageHandler* m,
|
|
|
|
Out_CqueryMemberHierarchy::Entry* entry,
|
|
|
|
bool detailed_name,
|
|
|
|
int levels);
|
|
|
|
|
|
|
|
// Add a field to |entry| which is a Func/Type.
|
|
|
|
void DoField(MessageHandler* m,
|
|
|
|
Out_CqueryMemberHierarchy::Entry* entry,
|
|
|
|
const QueryVar& var,
|
|
|
|
bool detailed_name,
|
|
|
|
int levels) {
|
|
|
|
const QueryVar::Def* def1 = var.AnyDef();
|
|
|
|
if (!def1)
|
|
|
|
return;
|
|
|
|
Out_CqueryMemberHierarchy::Entry entry1;
|
|
|
|
if (detailed_name)
|
|
|
|
entry1.fieldName = def1->DetailedName(false);
|
|
|
|
else
|
|
|
|
entry1.fieldName = std::string(def1->ShortName());
|
|
|
|
if (def1->spell) {
|
|
|
|
if (optional<lsLocation> loc =
|
|
|
|
GetLsLocation(m->db, m->working_files, *def1->spell))
|
|
|
|
entry1.location = *loc;
|
|
|
|
}
|
|
|
|
if (def1->type) {
|
|
|
|
entry1.id = *def1->type;
|
|
|
|
if (Expand(m, &entry1, detailed_name, levels))
|
|
|
|
entry->children.push_back(std::move(entry1));
|
|
|
|
} else {
|
|
|
|
entry1.id = QueryTypeId();
|
|
|
|
entry->children.push_back(std::move(entry1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expand a type node by adding members recursively to it.
|
2018-02-26 03:10:02 +00:00
|
|
|
bool Expand(MessageHandler* m,
|
2018-02-25 23:24:51 +00:00
|
|
|
Out_CqueryMemberHierarchy::Entry* entry,
|
|
|
|
bool detailed_name,
|
|
|
|
int levels) {
|
|
|
|
const QueryType& type = m->db->types[entry->id.id];
|
|
|
|
const QueryType::Def* def = type.AnyDef();
|
2018-02-25 23:39:27 +00:00
|
|
|
// builtin types have no declaration and empty |detailed_name|.
|
2018-02-25 23:24:51 +00:00
|
|
|
if (CXType_FirstBuiltin <= type.usr && type.usr <= CXType_LastBuiltin) {
|
2018-02-27 01:23:45 +00:00
|
|
|
entry->name = ClangBuiltinTypeName(CXTypeKind(type.usr));
|
2018-02-26 07:14:03 +00:00
|
|
|
return true;
|
2018-02-25 23:24:51 +00:00
|
|
|
}
|
2018-02-27 01:23:45 +00:00
|
|
|
if (!def)
|
2018-02-26 03:10:02 +00:00
|
|
|
return false;
|
2018-02-25 23:39:27 +00:00
|
|
|
if (detailed_name)
|
|
|
|
entry->name = def->detailed_name;
|
|
|
|
else
|
|
|
|
entry->name = def->ShortName();
|
2018-02-26 08:07:01 +00:00
|
|
|
std::unordered_set<Usr> seen;
|
2018-02-25 17:23:38 +00:00
|
|
|
if (levels > 0) {
|
2018-02-26 06:55:17 +00:00
|
|
|
std::vector<const QueryType*> stack;
|
|
|
|
seen.insert(type.usr);
|
|
|
|
stack.push_back(&type);
|
|
|
|
while (stack.size()) {
|
2018-02-26 08:07:01 +00:00
|
|
|
const auto* def = stack.back()->AnyDef();
|
2018-02-26 06:55:17 +00:00
|
|
|
stack.pop_back();
|
2018-02-26 08:07:01 +00:00
|
|
|
if (def) {
|
2018-02-26 06:55:17 +00:00
|
|
|
EachDefinedEntity(m->db->types, def->bases, [&](QueryType& type1) {
|
|
|
|
if (!seen.count(type1.usr)) {
|
|
|
|
seen.insert(type1.usr);
|
|
|
|
stack.push_back(&type1);
|
|
|
|
}
|
|
|
|
});
|
2018-02-26 08:07:01 +00:00
|
|
|
if (def->alias_of) {
|
|
|
|
const QueryType::Def* def1 = m->db->types[def->alias_of->id].AnyDef();
|
2018-02-26 06:55:17 +00:00
|
|
|
Out_CqueryMemberHierarchy::Entry entry1;
|
2018-02-26 08:07:01 +00:00
|
|
|
entry1.id = *def->alias_of;
|
2018-02-27 04:31:08 +00:00
|
|
|
if (def1 && def1->spell) {
|
|
|
|
// The declaration of target type.
|
|
|
|
if (optional<lsLocation> loc =
|
|
|
|
GetLsLocation(m->db, m->working_files, *def1->spell))
|
|
|
|
entry1.location = *loc;
|
|
|
|
} else if (def->spell) {
|
|
|
|
// Builtin types have no declaration but the typedef declaration
|
|
|
|
// itself is useful.
|
|
|
|
if (optional<lsLocation> loc =
|
|
|
|
GetLsLocation(m->db, m->working_files, *def->spell))
|
|
|
|
entry1.location = *loc;
|
2018-02-26 06:55:17 +00:00
|
|
|
}
|
2018-02-27 04:31:08 +00:00
|
|
|
if (def1 && detailed_name)
|
|
|
|
entry1.fieldName = def1->detailed_name;
|
2018-02-27 01:23:45 +00:00
|
|
|
if (Expand(m, &entry1, detailed_name, levels - 1)) {
|
|
|
|
// For builtin types |name| is set.
|
|
|
|
if (detailed_name && entry1.fieldName.empty())
|
|
|
|
entry1.fieldName = std::string(entry1.name);
|
2018-02-26 06:55:17 +00:00
|
|
|
entry->children.push_back(std::move(entry1));
|
2018-02-27 01:23:45 +00:00
|
|
|
}
|
2018-02-26 08:07:01 +00:00
|
|
|
} else {
|
|
|
|
EachDefinedEntity(m->db->vars, def->vars, [&](QueryVar& var) {
|
2018-02-27 04:31:08 +00:00
|
|
|
DoField(m, entry, var, detailed_name, levels - 1);
|
2018-02-26 08:07:01 +00:00
|
|
|
});
|
|
|
|
}
|
2018-02-26 02:45:46 +00:00
|
|
|
}
|
2018-02-26 06:55:17 +00:00
|
|
|
}
|
2018-02-25 22:53:57 +00:00
|
|
|
entry->numChildren = int(entry->children.size());
|
2018-02-26 03:10:02 +00:00
|
|
|
} else
|
2018-02-26 08:07:01 +00:00
|
|
|
entry->numChildren = def->alias_of ? 1 : int(def->vars.size());
|
2018-02-26 03:10:02 +00:00
|
|
|
return true;
|
2018-01-18 07:14:29 +00:00
|
|
|
}
|
|
|
|
|
2018-02-25 17:23:38 +00:00
|
|
|
struct CqueryMemberHierarchyInitialHandler
|
|
|
|
: BaseMessageHandler<Ipc_CqueryMemberHierarchyInitial> {
|
2018-02-27 04:31:08 +00:00
|
|
|
optional<Out_CqueryMemberHierarchy::Entry> BuildInitial(QueryFuncId root_id,
|
|
|
|
bool detailed_name,
|
|
|
|
int levels) {
|
|
|
|
const auto* def = db->funcs[root_id.id].AnyDef();
|
|
|
|
if (!def)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
Out_CqueryMemberHierarchy::Entry entry;
|
|
|
|
// Not type, |id| is invalid.
|
|
|
|
if (detailed_name)
|
|
|
|
entry.name = def->DetailedName(false);
|
|
|
|
else
|
|
|
|
entry.name = std::string(def->ShortName());
|
|
|
|
if (def->spell) {
|
|
|
|
if (optional<lsLocation> loc =
|
|
|
|
GetLsLocation(db, working_files, *def->spell))
|
|
|
|
entry.location = *loc;
|
|
|
|
}
|
|
|
|
EachDefinedEntity(db->vars, def->vars, [&](QueryVar& var) {
|
|
|
|
DoField(this, &entry, var, detailed_name, levels - 1);
|
|
|
|
});
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
2018-02-25 17:23:38 +00:00
|
|
|
optional<Out_CqueryMemberHierarchy::Entry> BuildInitial(QueryTypeId root_id,
|
2018-02-25 23:24:51 +00:00
|
|
|
bool detailed_name,
|
2018-02-25 17:23:38 +00:00
|
|
|
int levels) {
|
|
|
|
const auto* def = db->types[root_id.id].AnyDef();
|
|
|
|
if (!def)
|
|
|
|
return {};
|
2018-01-18 07:14:29 +00:00
|
|
|
|
|
|
|
Out_CqueryMemberHierarchy::Entry entry;
|
2018-02-25 17:23:38 +00:00
|
|
|
entry.id = root_id;
|
2018-02-26 02:45:46 +00:00
|
|
|
if (def->spell) {
|
|
|
|
if (optional<lsLocation> loc =
|
|
|
|
GetLsLocation(db, working_files, *def->spell))
|
|
|
|
entry.location = *loc;
|
|
|
|
}
|
2018-02-25 23:24:51 +00:00
|
|
|
Expand(this, &entry, detailed_name, levels);
|
2018-02-25 17:23:38 +00:00
|
|
|
return entry;
|
|
|
|
}
|
2018-01-18 07:14:29 +00:00
|
|
|
|
|
|
|
void Run(Ipc_CqueryMemberHierarchyInitial* request) override {
|
|
|
|
QueryFile* file;
|
2018-02-25 23:24:51 +00:00
|
|
|
const auto& params = request->params;
|
2018-01-18 07:14:29 +00:00
|
|
|
if (!FindFileOrFail(db, project, request->id,
|
2018-02-25 23:24:51 +00:00
|
|
|
params.textDocument.uri.GetPath(), &file))
|
2018-01-18 07:14:29 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
WorkingFile* working_file =
|
|
|
|
working_files->GetFileByFilename(file->def->path);
|
|
|
|
Out_CqueryMemberHierarchy out;
|
|
|
|
out.id = request->id;
|
|
|
|
|
2018-02-25 23:24:51 +00:00
|
|
|
for (SymbolRef sym :
|
|
|
|
FindSymbolsAtLocation(working_file, file, params.position)) {
|
2018-02-27 04:31:08 +00:00
|
|
|
switch (sym.kind) {
|
|
|
|
case SymbolKind::Func:
|
|
|
|
out.result = BuildInitial(QueryFuncId(sym.id), params.detailedName,
|
|
|
|
params.levels);
|
|
|
|
break;
|
|
|
|
case SymbolKind::Type:
|
2018-02-25 23:24:51 +00:00
|
|
|
out.result = BuildInitial(QueryTypeId(sym.id), params.detailedName,
|
|
|
|
params.levels);
|
2018-01-18 07:14:29 +00:00
|
|
|
break;
|
2018-02-27 04:31:08 +00:00
|
|
|
case SymbolKind::Var: {
|
2018-02-18 05:52:14 +00:00
|
|
|
const QueryVar::Def* def = db->GetVar(sym).AnyDef();
|
|
|
|
if (def && def->type)
|
2018-02-25 23:24:51 +00:00
|
|
|
out.result = BuildInitial(QueryTypeId(*def->type),
|
|
|
|
params.detailedName, params.levels);
|
2018-01-18 07:14:29 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-02-27 04:31:08 +00:00
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
2018-01-18 07:14:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QueueManager::WriteStdout(IpcId::CqueryMemberHierarchyInitial, out);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
REGISTER_MESSAGE_HANDLER(CqueryMemberHierarchyInitialHandler);
|
|
|
|
|
|
|
|
struct CqueryMemberHierarchyExpandHandler
|
|
|
|
: BaseMessageHandler<Ipc_CqueryMemberHierarchyExpand> {
|
|
|
|
void Run(Ipc_CqueryMemberHierarchyExpand* request) override {
|
2018-02-25 23:24:51 +00:00
|
|
|
const auto& params = request->params;
|
2018-01-18 07:14:29 +00:00
|
|
|
Out_CqueryMemberHierarchy out;
|
|
|
|
out.id = request->id;
|
2018-02-25 23:24:51 +00:00
|
|
|
if (params.id) {
|
2018-02-25 17:23:38 +00:00
|
|
|
Out_CqueryMemberHierarchy::Entry entry;
|
|
|
|
entry.id = *request->params.id;
|
2018-02-26 00:05:03 +00:00
|
|
|
// entry.name is empty as it is known by the client.
|
2018-02-26 03:10:02 +00:00
|
|
|
if (entry.id.id < db->types.size() &&
|
|
|
|
Expand(this, &entry, params.detailedName, params.levels))
|
|
|
|
out.result = std::move(entry);
|
2018-02-25 17:23:38 +00:00
|
|
|
}
|
2018-01-18 07:14:29 +00:00
|
|
|
|
|
|
|
QueueManager::WriteStdout(IpcId::CqueryMemberHierarchyExpand, out);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
REGISTER_MESSAGE_HANDLER(CqueryMemberHierarchyExpandHandler);
|
|
|
|
} // namespace
|