Simplify query.h

This commit is contained in:
Fangrui Song 2018-02-03 18:16:30 -08:00
parent 15dd552610
commit 0a721ff247
2 changed files with 58 additions and 113 deletions

View File

@ -458,100 +458,6 @@ QueryLocation IdMap::ToQuery(IndexFunc::Declaration decl) const {
return QueryLocation(primary_file, decl.spelling);
}
optional<QueryLocation> IdMap::ToQuery(optional<Range> range) const {
if (!range)
return nullopt;
return ToQuery(range.value());
}
optional<QueryTypeId> IdMap::ToQuery(optional<IndexTypeId> id) const {
if (!id)
return nullopt;
return ToQuery(id.value());
}
optional<QueryFuncId> IdMap::ToQuery(optional<IndexFuncId> id) const {
if (!id)
return nullopt;
return ToQuery(id.value());
}
optional<QueryVarId> IdMap::ToQuery(optional<IndexVarId> id) const {
if (!id)
return nullopt;
return ToQuery(id.value());
}
optional<WithGen<QueryTypeId>> IdMap::ToQuery(optional<IndexTypeId> id, int) const {
if (!id)
return nullopt;
return ToQuery(id.value(), 0);
}
optional<WithGen<QueryFuncId>> IdMap::ToQuery(optional<IndexFuncId> id, int) const {
if (!id)
return nullopt;
return ToQuery(id.value(), 0);
}
optional<WithGen<QueryVarId>> IdMap::ToQuery(optional<IndexVarId> id, int) const {
if (!id)
return nullopt;
return ToQuery(id.value(), 0);
}
optional<QueryFuncRef> IdMap::ToQuery(optional<IndexFuncRef> ref) const {
if (!ref)
return nullopt;
return ToQuery(ref.value());
}
optional<QueryLocation> IdMap::ToQuery(
optional<IndexFunc::Declaration> decl) const {
if (!decl)
return nullopt;
return ToQuery(decl.value());
}
template <typename In, typename Out>
std::vector<Out> ToQueryTransform(const IdMap& id_map,
const std::vector<In>& input) {
std::vector<Out> result;
result.reserve(input.size());
for (const In& in : input)
result.push_back(id_map.ToQuery(in));
return result;
}
template <typename In, typename Out>
std::vector<WithGen<Out>> ToQueryTransformG(const IdMap& id_map,
const std::vector<In>& input) {
std::vector<WithGen<Out>> result;
result.reserve(input.size());
for (const In& in : input)
result.push_back(id_map.ToQuery(in, 0));
return result;
}
std::vector<QueryLocation> IdMap::ToQuery(std::vector<Range> ranges) const {
return ToQueryTransform<Range, QueryLocation>(*this, ranges);
}
std::vector<QueryTypeId> IdMap::ToQuery(std::vector<IndexTypeId> ids) const {
return ToQueryTransform<IndexTypeId, QueryTypeId>(*this, ids);
}
std::vector<QueryFuncId> IdMap::ToQuery(std::vector<IndexFuncId> ids) const {
return ToQueryTransform<IndexFuncId, QueryFuncId>(*this, ids);
}
std::vector<QueryVarId> IdMap::ToQuery(std::vector<IndexVarId> ids) const {
return ToQueryTransform<IndexVarId, QueryVarId>(*this, ids);
}
std::vector<WithGen<QueryTypeId>> IdMap::ToQuery(std::vector<IndexTypeId> ids, int) const {
return ToQueryTransformG<IndexTypeId, QueryTypeId>(*this, ids);
}
std::vector<WithGen<QueryFuncId>> IdMap::ToQuery(std::vector<IndexFuncId> ids, int) const {
return ToQueryTransformG<IndexFuncId, QueryFuncId>(*this, ids);
}
std::vector<WithGen<QueryVarId>> IdMap::ToQuery(std::vector<IndexVarId> ids, int) const {
return ToQueryTransformG<IndexVarId, QueryVarId>(*this, ids);
}
std::vector<QueryFuncRef> IdMap::ToQuery(std::vector<IndexFuncRef> refs) const {
return ToQueryTransform<IndexFuncRef, QueryFuncRef>(*this, refs);
}
std::vector<QueryLocation> IdMap::ToQuery(
std::vector<IndexFunc::Declaration> decls) const {
return ToQueryTransform<IndexFunc::Declaration, QueryLocation>(*this, decls);
}
SymbolIdx IdMap::ToSymbol(IndexTypeId id) const {
return SymbolIdx(SymbolKind::Type, ToQuery(id).id);
}

View File

@ -442,6 +442,24 @@ struct QueryDatabase {
Maybe<QueryVarId> GetQueryVarIdFromUsr(Usr usr);
};
template <typename I>
struct IndexToQuery;
// clang-format off
template <> struct IndexToQuery<IndexFuncId> { using type = QueryFuncId; };
template <> struct IndexToQuery<IndexTypeId> { using type = QueryTypeId; };
template <> struct IndexToQuery<IndexVarId> { using type = QueryVarId; };
template <> struct IndexToQuery<IndexFuncRef> { using type = QueryFuncRef; };
template <> struct IndexToQuery<Range> { using type = QueryLocation; };
template <> struct IndexToQuery<IndexFunc::Declaration> { using type = QueryLocation; };
template <typename I> struct IndexToQuery<optional<I>> {
using type = optional<typename IndexToQuery<I>::type>;
};
template <typename I> struct IndexToQuery<std::vector<I>> {
using type = std::vector<typename IndexToQuery<I>::type>;
};
// clang-format on
struct IdMap {
const IdCache& local_ids;
QueryFileId primary_file;
@ -459,25 +477,46 @@ struct IdMap {
WithGen<QueryVarId> ToQuery(IndexVarId id, int) const;
QueryFuncRef ToQuery(IndexFuncRef ref) const;
QueryLocation ToQuery(IndexFunc::Declaration decl) const;
optional<QueryLocation> ToQuery(optional<Range> range) const;
optional<QueryTypeId> ToQuery(optional<IndexTypeId> id) const;
optional<QueryFuncId> ToQuery(optional<IndexFuncId> id) const;
optional<QueryVarId> ToQuery(optional<IndexVarId> id) const;
optional<WithGen<QueryTypeId>> ToQuery(optional<IndexTypeId> id,int) const;
optional<WithGen<QueryFuncId>> ToQuery(optional<IndexFuncId> id,int) const;
optional<WithGen<QueryVarId>> ToQuery(optional<IndexVarId> id,int) const;
optional<QueryFuncRef> ToQuery(optional<IndexFuncRef> ref) const;
optional<QueryLocation> ToQuery(optional<IndexFunc::Declaration> decl) const;
std::vector<QueryLocation> ToQuery(std::vector<Range> ranges) const;
std::vector<QueryTypeId> ToQuery(std::vector<IndexTypeId> ids) const;
std::vector<QueryFuncId> ToQuery(std::vector<IndexFuncId> ids) const;
std::vector<QueryVarId> ToQuery(std::vector<IndexVarId> ids) const;
std::vector<WithGen<QueryTypeId>> ToQuery(std::vector<IndexTypeId> ids,int) const;
std::vector<WithGen<QueryFuncId>> ToQuery(std::vector<IndexFuncId> ids,int) const;
std::vector<WithGen<QueryVarId>> ToQuery(std::vector<IndexVarId> ids,int) const;
std::vector<QueryFuncRef> ToQuery(std::vector<IndexFuncRef> refs) const;
std::vector<QueryLocation> ToQuery(
std::vector<IndexFunc::Declaration> decls) const;
template <typename I>
optional<typename IndexToQuery<I>::type> ToQuery(optional<I> id) const {
if (!id)
return nullopt;
return ToQuery(*id);
}
template <typename I>
optional<WithGen<typename IndexToQuery<I>::type>> ToQuery(optional<I> id, int) const {
if (!id)
return nullopt;
return ToQuery(*id, 0);
}
template <typename I>
Maybe<typename IndexToQuery<I>::type> ToQuery(Maybe<I> id) const {
if (!id)
return nullopt;
return ToQuery(*id);
}
template <typename I>
Maybe<WithGen<typename IndexToQuery<I>::type>> ToQuery(Maybe<I> id, int) const {
if (!id)
return nullopt;
return ToQuery(*id, 0);
}
template <typename I>
std::vector<typename IndexToQuery<I>::type> ToQuery(const std::vector<I>& a) const {
std::vector<typename IndexToQuery<I>::type> ret;
ret.reserve(a.size());
for (auto& x : a)
ret.push_back(ToQuery(x));
return ret;
}
template <typename I>
std::vector<WithGen<typename IndexToQuery<I>::type>> ToQuery(std::vector<I> a, int) const {
std::vector<WithGen<typename IndexToQuery<I>::type>> ret;
ret.reserve(a.size());
for (auto& x : a)
ret.push_back(ToQuery(x, 0));
return ret;
}
// clang-format on
SymbolIdx ToSymbol(IndexTypeId id) const;