diff --git a/src/indexer.h b/src/indexer.h index 5c54be0f..295c0a99 100644 --- a/src/indexer.h +++ b/src/indexer.h @@ -40,7 +40,8 @@ template struct Id { size_t id; - Id() : id(-1) {} // Needed for containers and Maybe. Do not use directly. + // Invalid id. + Id() : id(-1) {} explicit Id(size_t id) : id(id) {} template explicit Id(Id o) : id(o.id) {} diff --git a/src/language_server_api.cc b/src/language_server_api.cc index b4d07a40..d8889f24 100644 --- a/src/language_server_api.cc +++ b/src/language_server_api.cc @@ -124,7 +124,7 @@ optional MessageRegistry::ReadMessageFromStdin( optional content = ReadJsonRpcContentFrom(&ReadCharFromStdinBlocking); if (!content) { - LOG_S(FATAL) << "Failed to read JsonRpc input; exiting"; + LOG_S(ERROR) << "Failed to read JsonRpc input; exiting"; exit(1); } diff --git a/src/maybe.h b/src/maybe.h index 0b1d2712..b05fabbf 100644 --- a/src/maybe.h +++ b/src/maybe.h @@ -11,6 +11,7 @@ class Maybe { public: constexpr Maybe() = default; Maybe(const Maybe&) = default; + Maybe(std::nullopt_t) {} Maybe(const T& x) : storage(x) {} Maybe(T&& x) : storage(std::forward(x)) {} diff --git a/src/messages/cquery_call_tree.cc b/src/messages/cquery_call_tree.cc index 245a3798..7055ffb4 100644 --- a/src/messages/cquery_call_tree.cc +++ b/src/messages/cquery_call_tree.cc @@ -106,7 +106,7 @@ std::vector BuildExpandCallTree( // std::endl; return; //} - if (caller.has_id()) { + if (caller.HasValue()) { QueryFunc& call_func = db->funcs[caller.id_.id]; if (!call_func.def) return; @@ -189,10 +189,10 @@ struct CqueryCallTreeExpandHandler out.id = request->id; // FIXME - optional func_id = + Maybe func_id = db->GetQueryFuncIdFromUsr(std::stoull(request->params.usr)); if (func_id) - out.result = BuildExpandCallTree(db, working_files, func_id.value()); + out.result = BuildExpandCallTree(db, working_files, *func_id); QueueManager::WriteStdout(IpcId::CqueryCallTreeExpand, out); } diff --git a/src/query.cc b/src/query.cc index bde0ef88..665c1a64 100644 --- a/src/query.cc +++ b/src/query.cc @@ -294,9 +294,9 @@ QueryFile::DefUpdate BuildFileDefUpdate(const IdMap& id_map, const IndexFile& in return QueryFile::DefUpdate(def, indexed.file_contents); } -inline optional GetQueryFileIdFromPath(QueryDatabase* query_db, - const std::string& path, - bool create_if_missing) { +Maybe GetQueryFileIdFromPath(QueryDatabase* query_db, + const std::string& path, + bool create_if_missing) { NormalizedPath normalized_path(path); auto it = query_db->usr_to_file.find(normalized_path); if (it != query_db->usr_to_file.end()) @@ -310,9 +310,9 @@ inline optional GetQueryFileIdFromPath(QueryDatabase* query_db, return QueryFileId(idx); } -inline optional GetQueryTypeIdFromUsr(QueryDatabase* query_db, - Usr usr, - bool create_if_missing) { +Maybe GetQueryTypeIdFromUsr(QueryDatabase* query_db, + Usr usr, + bool create_if_missing) { auto it = query_db->usr_to_type.find(usr); if (it != query_db->usr_to_type.end()) return QueryTypeId(it->second.id); @@ -325,9 +325,9 @@ inline optional GetQueryTypeIdFromUsr(QueryDatabase* query_db, return QueryTypeId(idx); } -inline optional GetQueryFuncIdFromUsr(QueryDatabase* query_db, - Usr usr, - bool create_if_missing) { +Maybe GetQueryFuncIdFromUsr(QueryDatabase* query_db, + Usr usr, + bool create_if_missing) { auto it = query_db->usr_to_func.find(usr); if (it != query_db->usr_to_func.end()) return QueryFuncId(it->second.id); @@ -340,9 +340,9 @@ inline optional GetQueryFuncIdFromUsr(QueryDatabase* query_db, return QueryFuncId(idx); } -inline optional GetQueryVarIdFromUsr(QueryDatabase* query_db, - Usr usr, - bool create_if_missing) { +Maybe GetQueryVarIdFromUsr(QueryDatabase* query_db, + Usr usr, + bool create_if_missing) { auto it = query_db->usr_to_var.find(usr); if (it != query_db->usr_to_var.end()) return QueryVarId(it->second.id); @@ -362,20 +362,20 @@ bool Maybe::has_value() const { return storage.range.start.line >= 0; } -optional QueryDatabase::GetQueryFileIdFromPath( +Maybe QueryDatabase::GetQueryFileIdFromPath( const std::string& path) { return ::GetQueryFileIdFromPath(this, path, false); } -optional QueryDatabase::GetQueryTypeIdFromUsr(Usr usr) { +Maybe QueryDatabase::GetQueryTypeIdFromUsr(Usr usr) { return ::GetQueryTypeIdFromUsr(this, usr, false); } -optional QueryDatabase::GetQueryFuncIdFromUsr(Usr usr) { +Maybe QueryDatabase::GetQueryFuncIdFromUsr(Usr usr) { return ::GetQueryFuncIdFromUsr(this, usr, false); } -optional QueryDatabase::GetQueryVarIdFromUsr(Usr usr) { +Maybe QueryDatabase::GetQueryVarIdFromUsr(Usr usr) { return ::GetQueryVarIdFromUsr(this, usr, false); } @@ -383,22 +383,22 @@ IdMap::IdMap(QueryDatabase* query_db, const IdCache& local_ids) : local_ids(local_ids) { // LOG_S(INFO) << "Creating IdMap for " << local_ids.primary_file; primary_file = - GetQueryFileIdFromPath(query_db, local_ids.primary_file, true).value(); + *GetQueryFileIdFromPath(query_db, local_ids.primary_file, true); cached_type_ids_.resize(local_ids.type_id_to_usr.size()); for (const auto& entry : local_ids.type_id_to_usr) cached_type_ids_[entry.first] = - GetQueryTypeIdFromUsr(query_db, entry.second, true).value(); + *GetQueryTypeIdFromUsr(query_db, entry.second, true); cached_func_ids_.resize(local_ids.func_id_to_usr.size()); for (const auto& entry : local_ids.func_id_to_usr) cached_func_ids_[entry.first] = - GetQueryFuncIdFromUsr(query_db, entry.second, true).value(); + *GetQueryFuncIdFromUsr(query_db, entry.second, true); cached_var_ids_.resize(local_ids.var_id_to_usr.size()); for (const auto& entry : local_ids.var_id_to_usr) cached_var_ids_[entry.first] = - GetQueryVarIdFromUsr(query_db, entry.second, true).value(); + *GetQueryVarIdFromUsr(query_db, entry.second, true); } QueryLocation IdMap::ToQuery(Range range) const { @@ -409,8 +409,8 @@ QueryTypeId IdMap::ToQuery(IndexTypeId id) const { return QueryTypeId(cached_type_ids_.find(id)->second); } QueryFuncId IdMap::ToQuery(IndexFuncId id) const { - if (id.id == -1) - return QueryFuncId((size_t)-1); + if (id == IndexFuncId()) + return QueryFuncId(); assert(cached_func_ids_.find(id) != cached_func_ids_.end()); return QueryFuncId(cached_func_ids_.find(id)->second); } diff --git a/src/query.h b/src/query.h index babd1cad..60df5141 100644 --- a/src/query.h +++ b/src/query.h @@ -108,7 +108,7 @@ struct QueryFuncRef { QueryLocation loc; bool is_implicit = false; - bool has_id() const { return id_.id != static_cast(-1); } + bool HasValue() const { return id_.HasValue(); } QueryFuncRef() {} // Do not use, needed for reflect. QueryFuncRef(QueryFuncId id, QueryLocation loc, bool is_implicit) @@ -391,10 +391,10 @@ struct QueryDatabase { std::string_view GetSymbolShortName(size_t symbol_idx) const; // Query the indexing structure to look up symbol id for given Usr. - optional GetQueryFileIdFromPath(const std::string& path); - optional GetQueryTypeIdFromUsr(Usr usr); - optional GetQueryFuncIdFromUsr(Usr usr); - optional GetQueryVarIdFromUsr(Usr usr); + Maybe GetQueryFileIdFromPath(const std::string& path); + Maybe GetQueryTypeIdFromUsr(Usr usr); + Maybe GetQueryFuncIdFromUsr(Usr usr); + Maybe GetQueryVarIdFromUsr(Usr usr); }; struct IdMap {