Change symbol id from size_t to RawId (uint32_t currently)

This commit is contained in:
Fangrui Song 2018-02-03 10:33:22 -08:00
parent f266cb7b2a
commit 6933870962
5 changed files with 35 additions and 35 deletions

View File

@ -1464,12 +1464,12 @@ void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
IndexFuncId parent_func_id = IndexFuncId parent_func_id =
db->ToFuncId(decl->semanticContainer->cursor); db->ToFuncId(decl->semanticContainer->cursor);
var->def.parent_kind = SymbolKind::Func; var->def.parent_kind = SymbolKind::Func;
var->def.parent_id = size_t(parent_func_id); var->def.parent_id = Id<void>(parent_func_id);
} else if (IsTypeDefinition(decl->semanticContainer)) { } else if (IsTypeDefinition(decl->semanticContainer)) {
IndexTypeId parent_type_id = IndexTypeId parent_type_id =
db->ToTypeId(decl->semanticContainer->cursor); db->ToTypeId(decl->semanticContainer->cursor);
var->def.parent_kind = SymbolKind::Type; var->def.parent_kind = SymbolKind::Type;
var->def.parent_id = size_t(parent_type_id); var->def.parent_id = Id<void>(parent_type_id);
db->Resolve(parent_type_id)->def.vars.push_back(var_id); db->Resolve(parent_type_id)->def.vars.push_back(var_id);
} }
} }

View File

@ -36,20 +36,22 @@ struct IndexVar;
enum class SymbolKind : uint8_t { Invalid, File, Type, Func, Var }; enum class SymbolKind : uint8_t { Invalid, File, Type, Func, Var };
MAKE_REFLECT_TYPE_PROXY(SymbolKind); MAKE_REFLECT_TYPE_PROXY(SymbolKind);
using RawId = uint32_t;
template <typename T> template <typename T>
struct Id { struct Id {
size_t id; RawId id;
// Invalid id. // Invalid id.
Id() : id(-1) {} Id() : id(-1) {}
explicit Id(size_t id) : id(id) {} explicit Id(RawId id) : id(id) {}
template <typename U> template <typename U>
explicit Id(Id<U> o) : id(o.id) {} 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 RawId() const { return id; }
bool HasValue() const { return id != size_t(-1); } bool HasValue() const { return id != RawId(-1); }
bool operator==(const Id<T>& other) const { return id == other.id; } bool operator==(const Id<T>& other) const { return id == other.id; }
@ -99,7 +101,7 @@ struct IndexFuncRef {
IndexFuncRef(IndexFuncId id, Range loc, bool is_implicit) IndexFuncRef(IndexFuncId id, Range loc, bool is_implicit)
: id(id), loc(loc), is_implicit(is_implicit) {} : id(id), loc(loc), is_implicit(is_implicit) {}
IndexFuncRef(Range loc, bool 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) { inline bool operator==(const IndexFuncRef& other) {
return id == other.id && loc == other.loc && return id == other.id && loc == other.loc &&
@ -109,12 +111,11 @@ struct IndexFuncRef {
return !(*this == other); return !(*this == other);
} }
inline bool operator<(const IndexFuncRef& other) const { inline bool operator<(const IndexFuncRef& other) const {
if (id < other.id) if (id != other.id)
return true; return id < other.id;
if (id == other.id && loc < other.loc) if (loc != other.loc)
return true; return loc < other.loc;
return id == other.id && loc == other.loc && return is_implicit < other.is_implicit;
is_implicit < other.is_implicit;
} }
}; };
@ -144,12 +145,11 @@ inline void Reflect(Writer& visitor, IndexFuncRef& value) {
if (value.is_implicit) if (value.is_implicit)
s += "~"; s += "~";
// id.id is unsigned, special case 0 value // id.id is unsigned, special case -1 value
if (value.id.id == static_cast<size_t>(-1)) { if (value.id.HasValue())
s += "-1";
} else {
s += std::to_string(value.id.id); s += std::to_string(value.id.id);
} else
s += "-1";
s += "@" + value.loc.ToString(); s += "@" + value.loc.ToString();
visitor.String(s.c_str()); visitor.String(s.c_str());
@ -403,7 +403,7 @@ struct VarDefDefinitionData {
Maybe<TypeId> variable_type; Maybe<TypeId> variable_type;
// Function/type which declares this one. // Function/type which declares this one.
size_t parent_id = size_t(-1); Maybe<Id<void>> parent_id;
int16_t short_name_offset = 0; int16_t short_name_offset = 0;
int16_t short_name_size = 0; int16_t short_name_size = 0;
SymbolKind parent_kind = SymbolKind::Invalid; SymbolKind parent_kind = SymbolKind::Invalid;

View File

@ -54,7 +54,7 @@ bool FindFileOrFail(QueryDatabase* db,
} }
if (out_file_id) 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) != bool indexing = project->absolute_path_to_entry_index_.find(absolute_path) !=
project->absolute_path_to_entry_index_.end(); project->absolute_path_to_entry_index_.end();

View File

@ -304,7 +304,7 @@ Maybe<QueryFileId> GetQueryFileIdFromPath(QueryDatabase* query_db,
if (!create_if_missing) if (!create_if_missing)
return {}; 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->usr_to_file[normalized_path] = QueryFileId(idx);
query_db->files.push_back(QueryFile(path)); query_db->files.push_back(QueryFile(path));
return QueryFileId(idx); return QueryFileId(idx);
@ -319,7 +319,7 @@ Maybe<QueryTypeId> GetQueryTypeIdFromUsr(QueryDatabase* query_db,
if (!create_if_missing) if (!create_if_missing)
return {}; return {};
size_t idx = query_db->types.size(); RawId idx = query_db->types.size();
query_db->usr_to_type[usr] = QueryTypeId(idx); query_db->usr_to_type[usr] = QueryTypeId(idx);
query_db->types.push_back(QueryType(usr)); query_db->types.push_back(QueryType(usr));
return QueryTypeId(idx); return QueryTypeId(idx);
@ -334,7 +334,7 @@ Maybe<QueryFuncId> GetQueryFuncIdFromUsr(QueryDatabase* query_db,
if (!create_if_missing) if (!create_if_missing)
return {}; return {};
size_t idx = query_db->funcs.size(); RawId idx = query_db->funcs.size();
query_db->usr_to_func[usr] = QueryFuncId(idx); query_db->usr_to_func[usr] = QueryFuncId(idx);
query_db->funcs.push_back(QueryFunc(usr)); query_db->funcs.push_back(QueryFunc(usr));
return QueryFuncId(idx); return QueryFuncId(idx);
@ -349,7 +349,7 @@ Maybe<QueryVarId> GetQueryVarIdFromUsr(QueryDatabase* query_db,
if (!create_if_missing) if (!create_if_missing)
return {}; return {};
size_t idx = query_db->vars.size(); RawId idx = query_db->vars.size();
query_db->usr_to_var[usr] = QueryVarId(idx); query_db->usr_to_var[usr] = QueryVarId(idx);
query_db->vars.push_back(QueryVar(usr)); query_db->vars.push_back(QueryVar(usr));
return QueryVarId(idx); return QueryVarId(idx);
@ -956,15 +956,15 @@ void QueryDatabase::ImportOrUpdate(
void QueryDatabase::UpdateSymbols(Maybe<Id<void>>* symbol_idx, void QueryDatabase::UpdateSymbols(Maybe<Id<void>>* symbol_idx,
SymbolKind kind, SymbolKind kind,
size_t idx) { RawId idx) {
if (!symbol_idx->has_value()) { if (!symbol_idx->has_value()) {
*symbol_idx = Id<void>(symbols.size()); *symbol_idx = Id<void>(symbols.size());
symbols.push_back(SymbolIdx(kind, idx)); symbols.push_back(SymbolIdx(kind, idx));
} }
} }
std::string_view QueryDatabase::GetSymbolDetailedName(size_t symbol_idx) const { std::string_view QueryDatabase::GetSymbolDetailedName(RawId symbol_idx) const {
size_t idx = symbols[symbol_idx].idx; RawId idx = symbols[symbol_idx].idx;
switch (symbols[symbol_idx].kind) { switch (symbols[symbol_idx].kind) {
default: default:
break; break;
@ -988,8 +988,8 @@ std::string_view QueryDatabase::GetSymbolDetailedName(size_t symbol_idx) const {
return ""; return "";
} }
std::string_view QueryDatabase::GetSymbolShortName(size_t symbol_idx) const { std::string_view QueryDatabase::GetSymbolShortName(RawId symbol_idx) const {
size_t idx = symbols[symbol_idx].idx; RawId idx = symbols[symbol_idx].idx;
switch (symbols[symbol_idx].kind) { switch (symbols[symbol_idx].kind) {
default: default:
break; break;

View File

@ -61,12 +61,12 @@ struct hash<::SymbolKind> {
struct SymbolIdx { struct SymbolIdx {
SymbolKind kind; SymbolKind kind;
size_t idx; RawId idx;
SymbolIdx() SymbolIdx()
: kind(SymbolKind::Invalid), : kind(SymbolKind::Invalid),
idx((size_t)-1) {} // Default ctor needed by stdlib. Do not use. idx(RawId(-1)) {} // Default ctor needed by stdlib. Do not use.
SymbolIdx(SymbolKind kind, uint64_t idx) : kind(kind), idx(idx) {} SymbolIdx(SymbolKind kind, RawId idx) : kind(kind), idx(idx) {}
bool operator==(const SymbolIdx& that) const { bool operator==(const SymbolIdx& that) const {
return kind == that.kind && idx == that.idx; return kind == that.kind && idx == that.idx;
@ -386,9 +386,9 @@ 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(Maybe<Id<void>>* symbol_idx, SymbolKind kind, size_t idx); void UpdateSymbols(Maybe<Id<void>>* symbol_idx, SymbolKind kind, RawId idx);
std::string_view GetSymbolDetailedName(size_t symbol_idx) const; std::string_view GetSymbolDetailedName(RawId symbol_idx) const;
std::string_view GetSymbolShortName(size_t symbol_idx) const; std::string_view GetSymbolShortName(RawId symbol_idx) const;
// Query the indexing structure to look up symbol id for given Usr. // Query the indexing structure to look up symbol id for given Usr.
Maybe<QueryFileId> GetQueryFileIdFromPath(const std::string& path); Maybe<QueryFileId> GetQueryFileIdFromPath(const std::string& path);