diff --git a/src/indexer.cc b/src/indexer.cc index 913416f3..56d78211 100644 --- a/src/indexer.cc +++ b/src/indexer.cc @@ -1464,12 +1464,12 @@ void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) { IndexFuncId parent_func_id = db->ToFuncId(decl->semanticContainer->cursor); var->def.parent_kind = SymbolKind::Func; - var->def.parent_id = size_t(parent_func_id); + var->def.parent_id = Id(parent_func_id); } else if (IsTypeDefinition(decl->semanticContainer)) { IndexTypeId parent_type_id = db->ToTypeId(decl->semanticContainer->cursor); var->def.parent_kind = SymbolKind::Type; - var->def.parent_id = size_t(parent_type_id); + var->def.parent_id = Id(parent_type_id); db->Resolve(parent_type_id)->def.vars.push_back(var_id); } } diff --git a/src/indexer.h b/src/indexer.h index 295c0a99..37b1dd70 100644 --- a/src/indexer.h +++ b/src/indexer.h @@ -36,20 +36,22 @@ struct IndexVar; enum class SymbolKind : uint8_t { Invalid, File, Type, Func, Var }; MAKE_REFLECT_TYPE_PROXY(SymbolKind); +using RawId = uint32_t; + template struct Id { - size_t id; + RawId id; // Invalid id. Id() : id(-1) {} - explicit Id(size_t id) : id(id) {} + explicit Id(RawId id) : id(id) {} template explicit Id(Id o) : id(o.id) {} // Needed for google::dense_hash_map. - explicit operator size_t() const { return id; } + explicit operator RawId() const { return id; } - bool HasValue() const { return id != size_t(-1); } + bool HasValue() const { return id != RawId(-1); } bool operator==(const Id& other) const { return id == other.id; } @@ -99,7 +101,7 @@ struct IndexFuncRef { IndexFuncRef(IndexFuncId id, Range loc, bool is_implicit) : id(id), loc(loc), is_implicit(is_implicit) {} IndexFuncRef(Range loc, bool is_implicit) - : id(IndexFuncId((size_t)-1)), loc(loc), is_implicit(is_implicit) {} + : loc(loc), is_implicit(is_implicit) {} inline bool operator==(const IndexFuncRef& other) { return id == other.id && loc == other.loc && @@ -109,12 +111,11 @@ struct IndexFuncRef { return !(*this == other); } inline bool operator<(const IndexFuncRef& other) const { - if (id < other.id) - return true; - if (id == other.id && loc < other.loc) - return true; - return id == other.id && loc == other.loc && - is_implicit < other.is_implicit; + if (id != other.id) + return id < other.id; + if (loc != other.loc) + return loc < other.loc; + return is_implicit < other.is_implicit; } }; @@ -144,12 +145,11 @@ inline void Reflect(Writer& visitor, IndexFuncRef& value) { if (value.is_implicit) s += "~"; - // id.id is unsigned, special case 0 value - if (value.id.id == static_cast(-1)) { - s += "-1"; - } else { + // id.id is unsigned, special case -1 value + if (value.id.HasValue()) s += std::to_string(value.id.id); - } + else + s += "-1"; s += "@" + value.loc.ToString(); visitor.String(s.c_str()); @@ -403,7 +403,7 @@ struct VarDefDefinitionData { Maybe variable_type; // Function/type which declares this one. - size_t parent_id = size_t(-1); + Maybe> parent_id; int16_t short_name_offset = 0; int16_t short_name_size = 0; SymbolKind parent_kind = SymbolKind::Invalid; diff --git a/src/message_handler.cc b/src/message_handler.cc index 0fc409fd..95c2bcc2 100644 --- a/src/message_handler.cc +++ b/src/message_handler.cc @@ -54,7 +54,7 @@ bool FindFileOrFail(QueryDatabase* db, } if (out_file_id) - *out_file_id = QueryFileId((size_t)-1); + *out_file_id = QueryFileId(); bool indexing = project->absolute_path_to_entry_index_.find(absolute_path) != project->absolute_path_to_entry_index_.end(); diff --git a/src/query.cc b/src/query.cc index 665c1a64..b681b1fc 100644 --- a/src/query.cc +++ b/src/query.cc @@ -304,7 +304,7 @@ Maybe GetQueryFileIdFromPath(QueryDatabase* query_db, if (!create_if_missing) return {}; - size_t idx = query_db->files.size(); + RawId idx = query_db->files.size(); query_db->usr_to_file[normalized_path] = QueryFileId(idx); query_db->files.push_back(QueryFile(path)); return QueryFileId(idx); @@ -319,7 +319,7 @@ Maybe GetQueryTypeIdFromUsr(QueryDatabase* query_db, if (!create_if_missing) return {}; - size_t idx = query_db->types.size(); + RawId idx = query_db->types.size(); query_db->usr_to_type[usr] = QueryTypeId(idx); query_db->types.push_back(QueryType(usr)); return QueryTypeId(idx); @@ -334,7 +334,7 @@ Maybe GetQueryFuncIdFromUsr(QueryDatabase* query_db, if (!create_if_missing) return {}; - size_t idx = query_db->funcs.size(); + RawId idx = query_db->funcs.size(); query_db->usr_to_func[usr] = QueryFuncId(idx); query_db->funcs.push_back(QueryFunc(usr)); return QueryFuncId(idx); @@ -349,7 +349,7 @@ Maybe GetQueryVarIdFromUsr(QueryDatabase* query_db, if (!create_if_missing) return {}; - size_t idx = query_db->vars.size(); + RawId idx = query_db->vars.size(); query_db->usr_to_var[usr] = QueryVarId(idx); query_db->vars.push_back(QueryVar(usr)); return QueryVarId(idx); @@ -956,15 +956,15 @@ void QueryDatabase::ImportOrUpdate( void QueryDatabase::UpdateSymbols(Maybe>* symbol_idx, SymbolKind kind, - size_t idx) { + RawId idx) { if (!symbol_idx->has_value()) { *symbol_idx = Id(symbols.size()); symbols.push_back(SymbolIdx(kind, idx)); } } -std::string_view QueryDatabase::GetSymbolDetailedName(size_t symbol_idx) const { - size_t idx = symbols[symbol_idx].idx; +std::string_view QueryDatabase::GetSymbolDetailedName(RawId symbol_idx) const { + RawId idx = symbols[symbol_idx].idx; switch (symbols[symbol_idx].kind) { default: break; @@ -988,8 +988,8 @@ std::string_view QueryDatabase::GetSymbolDetailedName(size_t symbol_idx) const { return ""; } -std::string_view QueryDatabase::GetSymbolShortName(size_t symbol_idx) const { - size_t idx = symbols[symbol_idx].idx; +std::string_view QueryDatabase::GetSymbolShortName(RawId symbol_idx) const { + RawId idx = symbols[symbol_idx].idx; switch (symbols[symbol_idx].kind) { default: break; diff --git a/src/query.h b/src/query.h index 60df5141..91c008f2 100644 --- a/src/query.h +++ b/src/query.h @@ -61,12 +61,12 @@ struct hash<::SymbolKind> { struct SymbolIdx { SymbolKind kind; - size_t idx; + RawId idx; SymbolIdx() : kind(SymbolKind::Invalid), - idx((size_t)-1) {} // Default ctor needed by stdlib. Do not use. - SymbolIdx(SymbolKind kind, uint64_t idx) : kind(kind), idx(idx) {} + idx(RawId(-1)) {} // Default ctor needed by stdlib. Do not use. + SymbolIdx(SymbolKind kind, RawId idx) : kind(kind), idx(idx) {} bool operator==(const SymbolIdx& that) const { return kind == that.kind && idx == that.idx; @@ -386,9 +386,9 @@ struct QueryDatabase { void ImportOrUpdate(const std::vector& updates); void ImportOrUpdate(const std::vector& updates); void ImportOrUpdate(const std::vector& updates); - 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; + void UpdateSymbols(Maybe>* symbol_idx, SymbolKind kind, RawId idx); + std::string_view GetSymbolDetailedName(RawId symbol_idx) const; + std::string_view GetSymbolShortName(RawId symbol_idx) const; // Query the indexing structure to look up symbol id for given Usr. Maybe GetQueryFileIdFromPath(const std::string& path);