From ae999f9c92217d7cfcdad99bea06b588797faaca Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Thu, 1 Feb 2018 22:47:22 -0800 Subject: [PATCH] size_t Query*::symbol_idx -> Maybe> --- src/indexer.h | 2 ++ src/query.cc | 18 +++++++++--------- src/query.h | 10 +++++----- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/indexer.h b/src/indexer.h index 4db78fbd..5c54be0f 100644 --- a/src/indexer.h +++ b/src/indexer.h @@ -42,6 +42,8 @@ struct Id { Id() : id(-1) {} // Needed for containers and Maybe. Do not use directly. explicit Id(size_t id) : id(id) {} + template + explicit Id(Id o) : id(o.id) {} // Needed for google::dense_hash_map. explicit operator size_t() const { return id; } diff --git a/src/query.cc b/src/query.cc index 29e3001a..bde0ef88 100644 --- a/src/query.cc +++ b/src/query.cc @@ -795,8 +795,8 @@ void QueryDatabase::RemoveUsrs(SymbolKind usr_kind, case SymbolKind::Type: { for (const Usr& usr : to_remove) { QueryType& type = types[usr_to_type[usr].id]; - if (type.symbol_idx != size_t(-1)) - symbols[type.symbol_idx].kind = SymbolKind::Invalid; + if (type.symbol_idx) + symbols[type.symbol_idx->id].kind = SymbolKind::Invalid; type.def = nullopt; } break; @@ -804,8 +804,8 @@ void QueryDatabase::RemoveUsrs(SymbolKind usr_kind, case SymbolKind::Func: { for (const Usr& usr : to_remove) { QueryFunc& func = funcs[usr_to_func[usr].id]; - if (func.symbol_idx != size_t(-1)) - symbols[func.symbol_idx].kind = SymbolKind::Invalid; + if (func.symbol_idx) + symbols[func.symbol_idx->id].kind = SymbolKind::Invalid; func.def = nullopt; } break; @@ -813,8 +813,8 @@ void QueryDatabase::RemoveUsrs(SymbolKind usr_kind, case SymbolKind::Var: { for (const Usr& usr : to_remove) { QueryVar& var = vars[usr_to_var[usr].id]; - if (var.symbol_idx != size_t(-1)) - symbols[var.symbol_idx].kind = SymbolKind::Invalid; + if (var.symbol_idx) + symbols[var.symbol_idx->id].kind = SymbolKind::Invalid; var.def = nullopt; } break; @@ -954,11 +954,11 @@ void QueryDatabase::ImportOrUpdate( } } -void QueryDatabase::UpdateSymbols(size_t* symbol_idx, +void QueryDatabase::UpdateSymbols(Maybe>* symbol_idx, SymbolKind kind, size_t idx) { - if (*symbol_idx == -1) { - *symbol_idx = symbols.size(); + if (!symbol_idx->has_value()) { + *symbol_idx = Id(symbols.size()); symbols.push_back(SymbolIdx(kind, idx)); } } diff --git a/src/query.h b/src/query.h index 1e9efca8..babd1cad 100644 --- a/src/query.h +++ b/src/query.h @@ -213,7 +213,7 @@ struct QueryFile { using DefUpdate = WithFileContent; optional def; - size_t symbol_idx = (size_t)-1; + Maybe> symbol_idx; explicit QueryFile(const std::string& path) { def = Def(); @@ -243,7 +243,7 @@ struct QueryType { std::vector derived; std::vector instances; std::vector uses; - size_t symbol_idx = (size_t)-1; + Maybe> symbol_idx; explicit QueryType(const Usr& usr) : usr(usr) {} }; @@ -264,7 +264,7 @@ struct QueryFunc { std::vector declarations; std::vector derived; std::vector callers; - size_t symbol_idx = (size_t)-1; + Maybe> symbol_idx; explicit QueryFunc(const Usr& usr) : usr(usr) {} }; @@ -280,7 +280,7 @@ struct QueryVar { optional def; std::vector declarations; std::vector uses; - size_t symbol_idx = (size_t)-1; + Maybe> symbol_idx; explicit QueryVar(const Usr& usr) : usr(usr) {} }; @@ -386,7 +386,7 @@ struct QueryDatabase { void ImportOrUpdate(const std::vector& updates); void ImportOrUpdate(const std::vector& updates); void ImportOrUpdate(const std::vector& updates); - void UpdateSymbols(size_t* symbol_idx, SymbolKind kind, size_t idx); + void UpdateSymbols(Maybe>* symbol_idx, SymbolKind kind, size_t idx); std::string_view GetSymbolDetailedName(size_t symbol_idx) const; std::string_view GetSymbolShortName(size_t symbol_idx) const;