2018-01-18 07:14:29 +00:00
|
|
|
#include "message_handler.h"
|
2018-05-28 00:50:02 +00:00
|
|
|
#include "pipeline.hh"
|
2018-01-18 07:14:29 +00:00
|
|
|
#include "query_utils.h"
|
2018-05-28 00:50:02 +00:00
|
|
|
using namespace ccls;
|
2018-01-18 07:14:29 +00:00
|
|
|
|
2018-07-09 06:31:40 +00:00
|
|
|
#include <clang/AST/Type.h>
|
|
|
|
using namespace clang;
|
|
|
|
|
2018-05-05 22:29:17 +00:00
|
|
|
#include <unordered_set>
|
|
|
|
|
2018-01-18 07:14:29 +00:00
|
|
|
namespace {
|
2018-03-31 03:16:33 +00:00
|
|
|
MethodType kMethodType = "$ccls/memberHierarchy";
|
2018-03-22 04:05:25 +00:00
|
|
|
|
2018-03-31 03:16:33 +00:00
|
|
|
struct In_CclsMemberHierarchy : public RequestInMessage {
|
2018-03-22 04:05:25 +00:00
|
|
|
MethodType GetMethodType() const override { return kMethodType; }
|
|
|
|
|
2018-02-25 17:23:38 +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 17:23:38 +00:00
|
|
|
lsTextDocumentIdentifier textDocument;
|
|
|
|
lsPosition position;
|
2018-02-28 05:56:40 +00:00
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
// Type
|
|
|
|
Usr usr;
|
|
|
|
std::string id;
|
2018-02-28 05:56:40 +00:00
|
|
|
|
2018-04-06 00:00:07 +00:00
|
|
|
bool qualified = 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
|
|
|
|
2018-03-31 03:16:33 +00:00
|
|
|
MAKE_REFLECT_STRUCT(In_CclsMemberHierarchy::Params,
|
2018-02-25 17:23:38 +00:00
|
|
|
textDocument,
|
|
|
|
position,
|
2018-02-28 05:56:40 +00:00
|
|
|
id,
|
2018-04-06 00:00:07 +00:00
|
|
|
qualified,
|
2018-02-25 17:23:38 +00:00
|
|
|
levels);
|
2018-03-31 03:16:33 +00:00
|
|
|
MAKE_REFLECT_STRUCT(In_CclsMemberHierarchy, id, params);
|
|
|
|
REGISTER_IN_MESSAGE(In_CclsMemberHierarchy);
|
2018-01-18 07:14:29 +00:00
|
|
|
|
2018-03-31 03:16:33 +00:00
|
|
|
struct Out_CclsMemberHierarchy
|
|
|
|
: public lsOutMessage<Out_CclsMemberHierarchy> {
|
2018-01-18 07:14:29 +00:00
|
|
|
struct Entry {
|
2018-04-30 04:49:03 +00:00
|
|
|
Usr usr;
|
|
|
|
std::string 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-03-31 03:16:33 +00:00
|
|
|
std::optional<Entry> result;
|
2018-01-18 07:14:29 +00:00
|
|
|
};
|
2018-03-31 03:16:33 +00:00
|
|
|
MAKE_REFLECT_STRUCT(Out_CclsMemberHierarchy::Entry,
|
2018-02-25 17:23:38 +00:00
|
|
|
id,
|
|
|
|
name,
|
2018-02-26 00:05:03 +00:00
|
|
|
fieldName,
|
2018-02-25 17:23:38 +00:00
|
|
|
location,
|
|
|
|
numChildren,
|
|
|
|
children);
|
2018-05-08 03:59:08 +00:00
|
|
|
MAKE_REFLECT_STRUCT_MANDATORY_OPTIONAL(Out_CclsMemberHierarchy,
|
|
|
|
jsonrpc,
|
|
|
|
id,
|
|
|
|
result);
|
2018-01-18 07:14:29 +00:00
|
|
|
|
2018-02-27 04:31:08 +00:00
|
|
|
bool Expand(MessageHandler* m,
|
2018-03-31 03:16:33 +00:00
|
|
|
Out_CclsMemberHierarchy::Entry* entry,
|
2018-04-06 00:00:07 +00:00
|
|
|
bool qualified,
|
2018-02-27 04:31:08 +00:00
|
|
|
int levels);
|
|
|
|
|
|
|
|
// Add a field to |entry| which is a Func/Type.
|
|
|
|
void DoField(MessageHandler* m,
|
2018-03-31 03:16:33 +00:00
|
|
|
Out_CclsMemberHierarchy::Entry* entry,
|
2018-02-27 04:31:08 +00:00
|
|
|
const QueryVar& var,
|
2018-05-29 00:03:14 +00:00
|
|
|
int64_t offset,
|
2018-04-06 00:00:07 +00:00
|
|
|
bool qualified,
|
2018-02-27 04:31:08 +00:00
|
|
|
int levels) {
|
|
|
|
const QueryVar::Def* def1 = var.AnyDef();
|
|
|
|
if (!def1)
|
|
|
|
return;
|
2018-03-31 03:16:33 +00:00
|
|
|
Out_CclsMemberHierarchy::Entry entry1;
|
2018-05-29 00:03:14 +00:00
|
|
|
// With multiple inheritance, the offset is incorrect.
|
|
|
|
if (offset >= 0) {
|
|
|
|
if (offset / 8 < 10)
|
|
|
|
entry1.fieldName += ' ';
|
|
|
|
entry1.fieldName += std::to_string(offset / 8);
|
|
|
|
if (offset % 8) {
|
|
|
|
entry1.fieldName += '.';
|
|
|
|
entry1.fieldName += std::to_string(offset % 8);
|
|
|
|
}
|
|
|
|
entry1.fieldName += ' ';
|
|
|
|
}
|
2018-04-06 00:00:07 +00:00
|
|
|
if (qualified)
|
2018-05-29 00:03:14 +00:00
|
|
|
entry1.fieldName += def1->detailed_name;
|
2018-06-08 04:53:41 +00:00
|
|
|
else {
|
|
|
|
entry1.fieldName +=
|
|
|
|
std::string_view(def1->detailed_name).substr(0, def1->qual_name_offset);
|
|
|
|
entry1.fieldName += def1->Name(false);
|
|
|
|
}
|
2018-02-27 04:31:08 +00:00
|
|
|
if (def1->spell) {
|
2018-03-31 03:16:33 +00:00
|
|
|
if (std::optional<lsLocation> loc =
|
2018-03-20 02:51:42 +00:00
|
|
|
GetLsLocation(m->db, m->working_files, *def1->spell))
|
2018-02-27 04:31:08 +00:00
|
|
|
entry1.location = *loc;
|
|
|
|
}
|
|
|
|
if (def1->type) {
|
2018-04-30 04:49:03 +00:00
|
|
|
entry1.id = std::to_string(def1->type);
|
|
|
|
entry1.usr = def1->type;
|
2018-04-06 00:00:07 +00:00
|
|
|
if (Expand(m, &entry1, qualified, levels))
|
2018-02-27 04:31:08 +00:00
|
|
|
entry->children.push_back(std::move(entry1));
|
|
|
|
} else {
|
2018-04-30 04:49:03 +00:00
|
|
|
entry1.id = "0";
|
|
|
|
entry1.usr = 0;
|
2018-02-27 04:31:08 +00:00
|
|
|
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-03-31 03:16:33 +00:00
|
|
|
Out_CclsMemberHierarchy::Entry* entry,
|
2018-04-06 00:00:07 +00:00
|
|
|
bool qualified,
|
2018-02-25 23:24:51 +00:00
|
|
|
int levels) {
|
2018-07-09 06:31:40 +00:00
|
|
|
if (0 < entry->usr && entry->usr <= BuiltinType::LastKind) {
|
2018-07-15 07:45:51 +00:00
|
|
|
entry->name = ClangBuiltinTypeName(int(entry->usr));
|
2018-02-26 07:14:03 +00:00
|
|
|
return true;
|
2018-02-25 23:24:51 +00:00
|
|
|
}
|
2018-04-30 04:49:03 +00:00
|
|
|
const QueryType& type = m->db->Type(entry->usr);
|
|
|
|
const QueryType::Def* def = type.AnyDef();
|
|
|
|
// builtin types have no declaration and empty |qualified|.
|
2018-02-27 01:23:45 +00:00
|
|
|
if (!def)
|
2018-02-26 03:10:02 +00:00
|
|
|
return false;
|
2018-04-06 00:00:07 +00:00
|
|
|
entry->name = def->Name(qualified);
|
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-05-30 06:56:14 +00:00
|
|
|
EachDefinedType(m->db, def->bases, [&](QueryType& type1) {
|
2018-02-26 06:55:17 +00:00
|
|
|
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) {
|
2018-04-30 04:49:03 +00:00
|
|
|
const QueryType::Def* def1 = m->db->Type(def->alias_of).AnyDef();
|
2018-03-31 03:16:33 +00:00
|
|
|
Out_CclsMemberHierarchy::Entry entry1;
|
2018-04-30 04:49:03 +00:00
|
|
|
entry1.id = std::to_string(def->alias_of);
|
|
|
|
entry1.usr = def->alias_of;
|
2018-02-27 04:31:08 +00:00
|
|
|
if (def1 && def1->spell) {
|
|
|
|
// The declaration of target type.
|
2018-03-31 03:16:33 +00:00
|
|
|
if (std::optional<lsLocation> loc =
|
2018-03-20 02:51:42 +00:00
|
|
|
GetLsLocation(m->db, m->working_files, *def1->spell))
|
2018-02-27 04:31:08 +00:00
|
|
|
entry1.location = *loc;
|
|
|
|
} else if (def->spell) {
|
|
|
|
// Builtin types have no declaration but the typedef declaration
|
|
|
|
// itself is useful.
|
2018-03-31 03:16:33 +00:00
|
|
|
if (std::optional<lsLocation> loc =
|
2018-02-27 04:31:08 +00:00
|
|
|
GetLsLocation(m->db, m->working_files, *def->spell))
|
|
|
|
entry1.location = *loc;
|
2018-02-26 06:55:17 +00:00
|
|
|
}
|
2018-04-06 00:00:07 +00:00
|
|
|
if (def1 && qualified)
|
2018-02-27 04:31:08 +00:00
|
|
|
entry1.fieldName = def1->detailed_name;
|
2018-04-06 00:00:07 +00:00
|
|
|
if (Expand(m, &entry1, qualified, levels - 1)) {
|
2018-02-27 01:23:45 +00:00
|
|
|
// For builtin types |name| is set.
|
2018-04-06 00:00:07 +00:00
|
|
|
if (entry1.fieldName.empty())
|
2018-02-27 01:23:45 +00:00
|
|
|
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 {
|
2018-05-29 00:03:14 +00:00
|
|
|
for (auto it : def->vars) {
|
2018-05-30 06:56:14 +00:00
|
|
|
QueryVar& var = m->db->Var(it.first);
|
2018-05-29 00:03:14 +00:00
|
|
|
if (!var.def.empty())
|
|
|
|
DoField(m, entry, var, it.second, qualified, 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-03-31 03:16:33 +00:00
|
|
|
struct Handler_CclsMemberHierarchy
|
|
|
|
: BaseMessageHandler<In_CclsMemberHierarchy> {
|
2018-03-22 04:05:25 +00:00
|
|
|
MethodType GetMethodType() const override { return kMethodType; }
|
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
std::optional<Out_CclsMemberHierarchy::Entry> BuildInitial(SymbolKind kind,
|
|
|
|
Usr root_usr,
|
|
|
|
bool qualified,
|
|
|
|
int levels) {
|
|
|
|
switch (kind) {
|
|
|
|
default:
|
2018-02-27 04:31:08 +00:00
|
|
|
return {};
|
2018-04-30 04:49:03 +00:00
|
|
|
case SymbolKind::Func: {
|
|
|
|
const auto* def = db->Func(root_usr).AnyDef();
|
|
|
|
if (!def)
|
|
|
|
return {};
|
2018-02-27 04:31:08 +00:00
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
Out_CclsMemberHierarchy::Entry entry;
|
|
|
|
// Not type, |id| is invalid.
|
2018-07-21 06:27:47 +00:00
|
|
|
entry.name = def->Name(qualified);
|
2018-04-30 04:49:03 +00:00
|
|
|
if (def->spell) {
|
|
|
|
if (std::optional<lsLocation> loc =
|
|
|
|
GetLsLocation(db, working_files, *def->spell))
|
|
|
|
entry.location = *loc;
|
|
|
|
}
|
2018-05-30 06:56:14 +00:00
|
|
|
EachDefinedVar(db, def->vars, [&](QueryVar& var) {
|
2018-05-29 00:03:14 +00:00
|
|
|
DoField(this, &entry, var, -1, qualified, levels - 1);
|
2018-04-30 04:49:03 +00:00
|
|
|
});
|
|
|
|
return entry;
|
2018-02-27 04:31:08 +00:00
|
|
|
}
|
2018-04-30 04:49:03 +00:00
|
|
|
case SymbolKind::Type: {
|
|
|
|
const auto* def = db->Type(root_usr).AnyDef();
|
|
|
|
if (!def)
|
|
|
|
return {};
|
2018-02-27 04:31:08 +00:00
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
Out_CclsMemberHierarchy::Entry entry;
|
|
|
|
entry.id = std::to_string(root_usr);
|
|
|
|
entry.usr = root_usr;
|
|
|
|
if (def->spell) {
|
|
|
|
if (std::optional<lsLocation> loc =
|
|
|
|
GetLsLocation(db, working_files, *def->spell))
|
|
|
|
entry.location = *loc;
|
|
|
|
}
|
|
|
|
Expand(this, &entry, qualified, levels);
|
|
|
|
return entry;
|
|
|
|
}
|
2018-02-26 02:45:46 +00:00
|
|
|
}
|
2018-02-25 17:23:38 +00:00
|
|
|
}
|
2018-01-18 07:14:29 +00:00
|
|
|
|
2018-03-31 03:16:33 +00:00
|
|
|
void Run(In_CclsMemberHierarchy* request) override {
|
2018-04-08 17:03:50 +00:00
|
|
|
auto& params = request->params;
|
2018-03-31 03:16:33 +00:00
|
|
|
Out_CclsMemberHierarchy out;
|
2018-01-18 07:14:29 +00:00
|
|
|
out.id = request->id;
|
|
|
|
|
2018-04-30 04:49:03 +00:00
|
|
|
if (params.id.size()) {
|
|
|
|
try {
|
|
|
|
params.usr = std::stoull(params.id);
|
|
|
|
} catch (...) {
|
|
|
|
return;
|
|
|
|
}
|
2018-03-31 03:16:33 +00:00
|
|
|
Out_CclsMemberHierarchy::Entry entry;
|
2018-04-30 04:49:03 +00:00
|
|
|
entry.id = std::to_string(params.usr);
|
|
|
|
entry.usr = params.usr;
|
2018-02-26 00:05:03 +00:00
|
|
|
// entry.name is empty as it is known by the client.
|
2018-05-30 06:56:14 +00:00
|
|
|
if (db->HasType(entry.usr) &&
|
2018-04-06 00:00:07 +00:00
|
|
|
Expand(this, &entry, params.qualified, params.levels))
|
2018-02-26 03:10:02 +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;
|
2018-04-08 17:03:50 +00:00
|
|
|
WorkingFile* wfile =
|
2018-03-20 02:51:42 +00:00
|
|
|
working_files->GetFileByFilename(file->def->path);
|
2018-02-28 05:56:40 +00:00
|
|
|
for (SymbolRef sym :
|
2018-04-08 17:03:50 +00:00
|
|
|
FindSymbolsAtLocation(wfile, file, params.position)) {
|
2018-02-28 05:56:40 +00:00
|
|
|
switch (sym.kind) {
|
2018-03-20 02:51:42 +00:00
|
|
|
case SymbolKind::Func:
|
|
|
|
case SymbolKind::Type:
|
2018-04-30 04:49:03 +00:00
|
|
|
out.result = BuildInitial(sym.kind, sym.usr, params.qualified,
|
2018-03-20 02:51:42 +00:00
|
|
|
params.levels);
|
|
|
|
break;
|
|
|
|
case SymbolKind::Var: {
|
|
|
|
const QueryVar::Def* def = db->GetVar(sym).AnyDef();
|
|
|
|
if (def && def->type)
|
2018-04-30 04:49:03 +00:00
|
|
|
out.result = BuildInitial(SymbolKind::Type, def->type,
|
2018-04-06 00:00:07 +00:00
|
|
|
params.qualified, params.levels);
|
2018-03-20 02:51:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
continue;
|
2018-02-28 05:56:40 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-02-25 17:23:38 +00:00
|
|
|
}
|
2018-01-18 07:14:29 +00:00
|
|
|
|
2018-05-28 00:50:02 +00:00
|
|
|
pipeline::WriteStdout(kMethodType, out);
|
2018-01-18 07:14:29 +00:00
|
|
|
}
|
|
|
|
};
|
2018-03-31 03:16:33 +00:00
|
|
|
REGISTER_MESSAGE_HANDLER(Handler_CclsMemberHierarchy);
|
2018-02-28 05:56:40 +00:00
|
|
|
|
2018-01-18 07:14:29 +00:00
|
|
|
} // namespace
|