optional<...> GetQuery*IdFromUsr -> Maybe

This commit is contained in:
Fangrui Song 2018-02-01 23:10:37 -08:00
parent ae999f9c92
commit e42f6b6191
6 changed files with 34 additions and 32 deletions

View File

@ -40,7 +40,8 @@ template <typename T>
struct Id {
size_t id;
Id() : id(-1) {} // Needed for containers and Maybe<Id>. Do not use directly.
// Invalid id.
Id() : id(-1) {}
explicit Id(size_t id) : id(id) {}
template <typename U>
explicit Id(Id<U> o) : id(o.id) {}

View File

@ -124,7 +124,7 @@ optional<std::string> MessageRegistry::ReadMessageFromStdin(
optional<std::string> 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);
}

View File

@ -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<T>(x)) {}

View File

@ -106,7 +106,7 @@ std::vector<Out_CqueryCallTree::CallEntry> 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<QueryFuncId> func_id =
Maybe<QueryFuncId> 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);
}

View File

@ -294,9 +294,9 @@ QueryFile::DefUpdate BuildFileDefUpdate(const IdMap& id_map, const IndexFile& in
return QueryFile::DefUpdate(def, indexed.file_contents);
}
inline optional<QueryFileId> GetQueryFileIdFromPath(QueryDatabase* query_db,
const std::string& path,
bool create_if_missing) {
Maybe<QueryFileId> 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<QueryFileId> GetQueryFileIdFromPath(QueryDatabase* query_db,
return QueryFileId(idx);
}
inline optional<QueryTypeId> GetQueryTypeIdFromUsr(QueryDatabase* query_db,
Usr usr,
bool create_if_missing) {
Maybe<QueryTypeId> 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<QueryTypeId> GetQueryTypeIdFromUsr(QueryDatabase* query_db,
return QueryTypeId(idx);
}
inline optional<QueryFuncId> GetQueryFuncIdFromUsr(QueryDatabase* query_db,
Usr usr,
bool create_if_missing) {
Maybe<QueryFuncId> 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<QueryFuncId> GetQueryFuncIdFromUsr(QueryDatabase* query_db,
return QueryFuncId(idx);
}
inline optional<QueryVarId> GetQueryVarIdFromUsr(QueryDatabase* query_db,
Usr usr,
bool create_if_missing) {
Maybe<QueryVarId> 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<QueryLocation>::has_value() const {
return storage.range.start.line >= 0;
}
optional<QueryFileId> QueryDatabase::GetQueryFileIdFromPath(
Maybe<QueryFileId> QueryDatabase::GetQueryFileIdFromPath(
const std::string& path) {
return ::GetQueryFileIdFromPath(this, path, false);
}
optional<QueryTypeId> QueryDatabase::GetQueryTypeIdFromUsr(Usr usr) {
Maybe<QueryTypeId> QueryDatabase::GetQueryTypeIdFromUsr(Usr usr) {
return ::GetQueryTypeIdFromUsr(this, usr, false);
}
optional<QueryFuncId> QueryDatabase::GetQueryFuncIdFromUsr(Usr usr) {
Maybe<QueryFuncId> QueryDatabase::GetQueryFuncIdFromUsr(Usr usr) {
return ::GetQueryFuncIdFromUsr(this, usr, false);
}
optional<QueryVarId> QueryDatabase::GetQueryVarIdFromUsr(Usr usr) {
Maybe<QueryVarId> 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);
}

View File

@ -108,7 +108,7 @@ struct QueryFuncRef {
QueryLocation loc;
bool is_implicit = false;
bool has_id() const { return id_.id != static_cast<size_t>(-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<QueryFileId> GetQueryFileIdFromPath(const std::string& path);
optional<QueryTypeId> GetQueryTypeIdFromUsr(Usr usr);
optional<QueryFuncId> GetQueryFuncIdFromUsr(Usr usr);
optional<QueryVarId> GetQueryVarIdFromUsr(Usr usr);
Maybe<QueryFileId> GetQueryFileIdFromPath(const std::string& path);
Maybe<QueryTypeId> GetQueryTypeIdFromUsr(Usr usr);
Maybe<QueryFuncId> GetQueryFuncIdFromUsr(Usr usr);
Maybe<QueryVarId> GetQueryVarIdFromUsr(Usr usr);
};
struct IdMap {