size_t Query*::symbol_idx -> Maybe<Id<void>>

This commit is contained in:
Fangrui Song 2018-02-01 22:47:22 -08:00
parent 3c6f3f661f
commit ae999f9c92
3 changed files with 16 additions and 14 deletions

View File

@ -42,6 +42,8 @@ struct Id {
Id() : id(-1) {} // Needed for containers and Maybe<Id>. Do not use directly. Id() : id(-1) {} // Needed for containers and Maybe<Id>. Do not use directly.
explicit Id(size_t id) : id(id) {} explicit Id(size_t id) : id(id) {}
template <typename U>
explicit Id(Id<U> o) : id(o.id) {}
// Needed for google::dense_hash_map. // Needed for google::dense_hash_map.
explicit operator size_t() const { return id; } explicit operator size_t() const { return id; }

View File

@ -795,8 +795,8 @@ void QueryDatabase::RemoveUsrs(SymbolKind usr_kind,
case SymbolKind::Type: { case SymbolKind::Type: {
for (const Usr& usr : to_remove) { for (const Usr& usr : to_remove) {
QueryType& type = types[usr_to_type[usr].id]; QueryType& type = types[usr_to_type[usr].id];
if (type.symbol_idx != size_t(-1)) if (type.symbol_idx)
symbols[type.symbol_idx].kind = SymbolKind::Invalid; symbols[type.symbol_idx->id].kind = SymbolKind::Invalid;
type.def = nullopt; type.def = nullopt;
} }
break; break;
@ -804,8 +804,8 @@ void QueryDatabase::RemoveUsrs(SymbolKind usr_kind,
case SymbolKind::Func: { case SymbolKind::Func: {
for (const Usr& usr : to_remove) { for (const Usr& usr : to_remove) {
QueryFunc& func = funcs[usr_to_func[usr].id]; QueryFunc& func = funcs[usr_to_func[usr].id];
if (func.symbol_idx != size_t(-1)) if (func.symbol_idx)
symbols[func.symbol_idx].kind = SymbolKind::Invalid; symbols[func.symbol_idx->id].kind = SymbolKind::Invalid;
func.def = nullopt; func.def = nullopt;
} }
break; break;
@ -813,8 +813,8 @@ void QueryDatabase::RemoveUsrs(SymbolKind usr_kind,
case SymbolKind::Var: { case SymbolKind::Var: {
for (const Usr& usr : to_remove) { for (const Usr& usr : to_remove) {
QueryVar& var = vars[usr_to_var[usr].id]; QueryVar& var = vars[usr_to_var[usr].id];
if (var.symbol_idx != size_t(-1)) if (var.symbol_idx)
symbols[var.symbol_idx].kind = SymbolKind::Invalid; symbols[var.symbol_idx->id].kind = SymbolKind::Invalid;
var.def = nullopt; var.def = nullopt;
} }
break; break;
@ -954,11 +954,11 @@ void QueryDatabase::ImportOrUpdate(
} }
} }
void QueryDatabase::UpdateSymbols(size_t* symbol_idx, void QueryDatabase::UpdateSymbols(Maybe<Id<void>>* symbol_idx,
SymbolKind kind, SymbolKind kind,
size_t idx) { size_t idx) {
if (*symbol_idx == -1) { if (!symbol_idx->has_value()) {
*symbol_idx = symbols.size(); *symbol_idx = Id<void>(symbols.size());
symbols.push_back(SymbolIdx(kind, idx)); symbols.push_back(SymbolIdx(kind, idx));
} }
} }

View File

@ -213,7 +213,7 @@ struct QueryFile {
using DefUpdate = WithFileContent<Def>; using DefUpdate = WithFileContent<Def>;
optional<Def> def; optional<Def> def;
size_t symbol_idx = (size_t)-1; Maybe<Id<void>> symbol_idx;
explicit QueryFile(const std::string& path) { explicit QueryFile(const std::string& path) {
def = Def(); def = Def();
@ -243,7 +243,7 @@ struct QueryType {
std::vector<QueryTypeId> derived; std::vector<QueryTypeId> derived;
std::vector<QueryVarId> instances; std::vector<QueryVarId> instances;
std::vector<QueryLocation> uses; std::vector<QueryLocation> uses;
size_t symbol_idx = (size_t)-1; Maybe<Id<void>> symbol_idx;
explicit QueryType(const Usr& usr) : usr(usr) {} explicit QueryType(const Usr& usr) : usr(usr) {}
}; };
@ -264,7 +264,7 @@ struct QueryFunc {
std::vector<QueryLocation> declarations; std::vector<QueryLocation> declarations;
std::vector<QueryFuncId> derived; std::vector<QueryFuncId> derived;
std::vector<QueryFuncRef> callers; std::vector<QueryFuncRef> callers;
size_t symbol_idx = (size_t)-1; Maybe<Id<void>> symbol_idx;
explicit QueryFunc(const Usr& usr) : usr(usr) {} explicit QueryFunc(const Usr& usr) : usr(usr) {}
}; };
@ -280,7 +280,7 @@ struct QueryVar {
optional<Def> def; optional<Def> def;
std::vector<QueryLocation> declarations; std::vector<QueryLocation> declarations;
std::vector<QueryLocation> uses; std::vector<QueryLocation> uses;
size_t symbol_idx = (size_t)-1; Maybe<Id<void>> symbol_idx;
explicit QueryVar(const Usr& usr) : usr(usr) {} explicit QueryVar(const Usr& usr) : usr(usr) {}
}; };
@ -386,7 +386,7 @@ struct QueryDatabase {
void ImportOrUpdate(const std::vector<QueryType::DefUpdate>& updates); void ImportOrUpdate(const std::vector<QueryType::DefUpdate>& updates);
void ImportOrUpdate(const std::vector<QueryFunc::DefUpdate>& updates); void ImportOrUpdate(const std::vector<QueryFunc::DefUpdate>& updates);
void ImportOrUpdate(const std::vector<QueryVar::DefUpdate>& updates); void ImportOrUpdate(const std::vector<QueryVar::DefUpdate>& updates);
void UpdateSymbols(size_t* symbol_idx, SymbolKind kind, size_t idx); void UpdateSymbols(Maybe<Id<void>>* symbol_idx, SymbolKind kind, size_t idx);
std::string_view GetSymbolDetailedName(size_t symbol_idx) const; std::string_view GetSymbolDetailedName(size_t symbol_idx) const;
std::string_view GetSymbolShortName(size_t symbol_idx) const; std::string_view GetSymbolShortName(size_t symbol_idx) const;