ccls/src/query.h

185 lines
5.3 KiB
C
Raw Normal View History

2017-02-23 09:23:23 +00:00
#pragma once
2017-02-26 08:11:47 +00:00
#include "indexer.h"
#include "serializer.h"
2017-02-26 08:11:47 +00:00
#include <llvm/ADT/DenseMap.h>
#include <llvm/ADT/SmallVector.h>
2017-04-17 20:40:50 +00:00
struct QueryFile;
struct QueryType;
struct QueryFunc;
struct QueryVar;
struct DB;
template <typename T>
struct WithFileContent {
T value;
std::string file_content;
2018-02-22 07:34:32 +00:00
WithFileContent(const T& value, const std::string& file_content)
: value(value), file_content(file_content) {}
};
struct QueryFile {
struct Def {
std::string path;
2018-02-15 06:41:07 +00:00
std::vector<std::string> args;
2018-05-05 03:40:52 +00:00
LanguageId language;
2017-05-21 03:46:15 +00:00
// Includes in the file.
std::vector<IndexInclude> includes;
// Outline of the file (ie, for code lens).
std::vector<SymbolRef> outline;
// Every symbol found in the file (ie, for goto definition)
std::vector<SymbolRef> all_symbols;
// Parts of the file which are disabled.
std::vector<Range> inactive_regions;
2018-03-31 03:16:33 +00:00
// Used by |$ccls/freshenIndex|.
std::vector<std::string> dependencies;
};
2017-02-27 07:23:43 +00:00
using DefUpdate = WithFileContent<Def>;
2017-02-27 07:23:43 +00:00
int id = -1;
2018-03-31 03:16:33 +00:00
std::optional<Def> def;
2017-02-26 08:11:47 +00:00
};
2017-03-14 08:33:39 +00:00
template <typename Q, typename QDef>
2018-02-22 06:34:34 +00:00
struct QueryEntity {
using Def = QDef;
2018-02-22 06:34:34 +00:00
Def* AnyDef() {
Def* ret = nullptr;
for (auto& i : static_cast<Q*>(this)->def) {
ret = &i;
if (i.spell)
break;
}
return ret;
}
2018-02-22 07:34:32 +00:00
const Def* AnyDef() const { return const_cast<QueryEntity*>(this)->AnyDef(); }
2018-02-22 06:34:34 +00:00
};
2018-05-04 04:20:10 +00:00
using UseUpdate =
std::unordered_map<Usr, std::pair<std::vector<Use>, std::vector<Use>>>;
using UsrUpdate =
std::unordered_map<Usr, std::pair<std::vector<Usr>, std::vector<Usr>>>;
2017-02-27 02:03:14 +00:00
struct QueryFunc : QueryEntity<QueryFunc, FuncDef> {
2018-01-13 08:10:39 +00:00
Usr usr;
llvm::SmallVector<Def, 1> def;
std::vector<Use> declarations;
std::vector<Use> uses;
std::vector<Usr> derived;
2017-02-26 08:11:47 +00:00
};
2017-03-14 08:33:39 +00:00
struct QueryType : QueryEntity<QueryType, TypeDef> {
2018-01-13 08:10:39 +00:00
Usr usr;
llvm::SmallVector<Def, 1> def;
std::vector<Use> declarations;
std::vector<Use> uses;
std::vector<Usr> derived;
std::vector<Usr> instances;
2017-02-26 08:11:47 +00:00
};
2017-03-14 08:33:39 +00:00
struct QueryVar : QueryEntity<QueryVar, VarDef> {
Usr usr;
llvm::SmallVector<Def, 1> def;
std::vector<Use> declarations;
std::vector<Use> uses;
2017-02-26 08:11:47 +00:00
};
2017-03-14 08:33:39 +00:00
2017-02-27 07:23:43 +00:00
struct IndexUpdate {
// Creates a new IndexUpdate based on the delta from previous to current. If
// no delta computation should be done just pass null for previous.
static IndexUpdate CreateDelta(IndexFile* previous,
2017-09-22 01:14:57 +00:00
IndexFile* current);
2018-05-08 15:56:20 +00:00
int file_id;
// Dummy one to refresh all semantic highlight.
bool refresh = false;
2017-02-27 07:23:43 +00:00
// File updates.
std::optional<std::string> files_removed;
std::optional<QueryFile::DefUpdate> files_def_update;
// Function updates.
int funcs_hint;
std::vector<Usr> funcs_removed;
2018-05-04 04:20:10 +00:00
std::vector<std::pair<Usr, QueryFunc::Def>> funcs_def_update;
UseUpdate funcs_declarations;
UseUpdate funcs_uses;
UsrUpdate funcs_derived;
2017-02-27 07:23:43 +00:00
// Type updates.
int types_hint;
2017-02-27 07:23:43 +00:00
std::vector<Usr> types_removed;
2018-05-04 04:20:10 +00:00
std::vector<std::pair<Usr, QueryType::Def>> types_def_update;
UseUpdate types_declarations;
UseUpdate types_uses;
UsrUpdate types_derived;
UsrUpdate types_instances;
2017-02-27 07:23:43 +00:00
// Variable updates.
int vars_hint;
std::vector<Usr> vars_removed;
2018-05-04 04:20:10 +00:00
std::vector<std::pair<Usr, QueryVar::Def>> vars_def_update;
UseUpdate vars_declarations;
UseUpdate vars_uses;
2017-02-27 07:23:43 +00:00
};
template <typename Q>
struct EntityToIndex {
using argument_type = const Q&;
llvm::DenseMap<Usr, unsigned> m;
unsigned operator()(const Q& entity) const {
return m[entity.usr];
}
};
struct WrappedUsr {
Usr usr;
};
template <>
struct llvm::DenseMapInfo<WrappedUsr> {
static inline WrappedUsr getEmptyKey() { return {0}; }
static inline WrappedUsr getTombstoneKey() { return {~0ULL}; }
static unsigned getHashValue(WrappedUsr w) { return w.usr; }
static bool isEqual(WrappedUsr l, WrappedUsr r) { return l.usr == r.usr; }
};
2017-02-27 07:23:43 +00:00
// The query database is heavily optimized for fast queries. It is stored
// in-memory.
struct DB {
std::vector<QueryFile> files;
llvm::StringMap<int> name2file_id;
llvm::DenseMap<WrappedUsr, int> func_usr, type_usr, var_usr;
std::vector<QueryFunc> funcs;
std::vector<QueryType> types;
std::vector<QueryVar> vars;
2017-02-27 07:23:43 +00:00
// Marks the given Usrs as invalid.
void RemoveUsrs(SymbolKind usr_kind, const std::vector<Usr>& to_remove);
void RemoveUsrs(SymbolKind usr_kind, int file_id, const std::vector<Usr>& to_remove);
2017-02-27 07:23:43 +00:00
// Insert the contents of |update| into |db|.
void ApplyIndexUpdate(IndexUpdate* update);
int Update(QueryFile::DefUpdate&& u);
2018-05-04 04:20:10 +00:00
void Update(int file_id, std::vector<std::pair<Usr, QueryType::Def>>&& us);
void Update(int file_id, std::vector<std::pair<Usr, QueryFunc::Def>>&& us);
void Update(int file_id, std::vector<std::pair<Usr, QueryVar::Def>>&& us);
std::string_view GetSymbolName(SymbolIdx sym, bool qualified);
2017-04-07 08:01:58 +00:00
bool HasFunc(Usr usr) const { return func_usr.count({usr}); }
bool HasType(Usr usr) const { return type_usr.count({usr}); }
bool HasVar(Usr usr) const { return var_usr.count({usr}); }
2017-04-08 07:52:57 +00:00
QueryFunc& Func(Usr usr) { return funcs[func_usr[{usr}]]; }
QueryType& Type(Usr usr) { return types[type_usr[{usr}]]; }
QueryVar& Var(Usr usr) { return vars[var_usr[{usr}]]; }
QueryFile& GetFile(SymbolIdx ref) { return files[ref.usr]; }
QueryFunc& GetFunc(SymbolIdx ref) { return Func(ref.usr); }
QueryType& GetType(SymbolIdx ref) { return Type(ref.usr); }
QueryVar& GetVar(SymbolIdx ref) { return Var(ref.usr); }
2017-04-07 08:01:58 +00:00
};