mirror of
				https://github.com/MaskRay/ccls.git
				synced 2025-11-03 22:04:24 +00:00 
			
		
		
		
	Simplify query.h
This commit is contained in:
		
							parent
							
								
									15dd552610
								
							
						
					
					
						commit
						0a721ff247
					
				
							
								
								
									
										94
									
								
								src/query.cc
									
									
									
									
									
								
							
							
						
						
									
										94
									
								
								src/query.cc
									
									
									
									
									
								
							@ -458,100 +458,6 @@ QueryLocation IdMap::ToQuery(IndexFunc::Declaration decl) const {
 | 
				
			|||||||
  return QueryLocation(primary_file, decl.spelling);
 | 
					  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 {
 | 
					SymbolIdx IdMap::ToSymbol(IndexTypeId id) const {
 | 
				
			||||||
  return SymbolIdx(SymbolKind::Type, ToQuery(id).id);
 | 
					  return SymbolIdx(SymbolKind::Type, ToQuery(id).id);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										77
									
								
								src/query.h
									
									
									
									
									
								
							
							
						
						
									
										77
									
								
								src/query.h
									
									
									
									
									
								
							@ -442,6 +442,24 @@ struct QueryDatabase {
 | 
				
			|||||||
  Maybe<QueryVarId> GetQueryVarIdFromUsr(Usr usr);
 | 
					  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 {
 | 
					struct IdMap {
 | 
				
			||||||
  const IdCache& local_ids;
 | 
					  const IdCache& local_ids;
 | 
				
			||||||
  QueryFileId primary_file;
 | 
					  QueryFileId primary_file;
 | 
				
			||||||
@ -459,25 +477,46 @@ struct IdMap {
 | 
				
			|||||||
  WithGen<QueryVarId> ToQuery(IndexVarId id, int) const;
 | 
					  WithGen<QueryVarId> ToQuery(IndexVarId id, int) const;
 | 
				
			||||||
  QueryFuncRef ToQuery(IndexFuncRef ref) const;
 | 
					  QueryFuncRef ToQuery(IndexFuncRef ref) const;
 | 
				
			||||||
  QueryLocation ToQuery(IndexFunc::Declaration decl) const;
 | 
					  QueryLocation ToQuery(IndexFunc::Declaration decl) const;
 | 
				
			||||||
  optional<QueryLocation> ToQuery(optional<Range> range) const;
 | 
					  template <typename I>
 | 
				
			||||||
  optional<QueryTypeId> ToQuery(optional<IndexTypeId> id) const;
 | 
					  optional<typename IndexToQuery<I>::type> ToQuery(optional<I> id) const {
 | 
				
			||||||
  optional<QueryFuncId> ToQuery(optional<IndexFuncId> id) const;
 | 
					    if (!id)
 | 
				
			||||||
  optional<QueryVarId> ToQuery(optional<IndexVarId> id) const;
 | 
					      return nullopt;
 | 
				
			||||||
  optional<WithGen<QueryTypeId>> ToQuery(optional<IndexTypeId> id,int) const;
 | 
					    return ToQuery(*id);
 | 
				
			||||||
  optional<WithGen<QueryFuncId>> ToQuery(optional<IndexFuncId> id,int) const;
 | 
					  }
 | 
				
			||||||
  optional<WithGen<QueryVarId>> ToQuery(optional<IndexVarId> id,int) const;
 | 
					  template <typename I>
 | 
				
			||||||
  optional<QueryFuncRef> ToQuery(optional<IndexFuncRef> ref) const;
 | 
					  optional<WithGen<typename IndexToQuery<I>::type>> ToQuery(optional<I> id, int) const {
 | 
				
			||||||
  optional<QueryLocation> ToQuery(optional<IndexFunc::Declaration> decl) const;
 | 
					    if (!id)
 | 
				
			||||||
  std::vector<QueryLocation> ToQuery(std::vector<Range> ranges) const;
 | 
					      return nullopt;
 | 
				
			||||||
  std::vector<QueryTypeId> ToQuery(std::vector<IndexTypeId> ids) const;
 | 
					    return ToQuery(*id, 0);
 | 
				
			||||||
  std::vector<QueryFuncId> ToQuery(std::vector<IndexFuncId> ids) const;
 | 
					  }
 | 
				
			||||||
  std::vector<QueryVarId> ToQuery(std::vector<IndexVarId> ids) const;
 | 
					  template <typename I>
 | 
				
			||||||
  std::vector<WithGen<QueryTypeId>> ToQuery(std::vector<IndexTypeId> ids,int) const;
 | 
					  Maybe<typename IndexToQuery<I>::type> ToQuery(Maybe<I> id) const {
 | 
				
			||||||
  std::vector<WithGen<QueryFuncId>> ToQuery(std::vector<IndexFuncId> ids,int) const;
 | 
					    if (!id)
 | 
				
			||||||
  std::vector<WithGen<QueryVarId>> ToQuery(std::vector<IndexVarId> ids,int) const;
 | 
					      return nullopt;
 | 
				
			||||||
  std::vector<QueryFuncRef> ToQuery(std::vector<IndexFuncRef> refs) const;
 | 
					    return ToQuery(*id);
 | 
				
			||||||
  std::vector<QueryLocation> ToQuery(
 | 
					  }
 | 
				
			||||||
      std::vector<IndexFunc::Declaration> decls) const;
 | 
					  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
 | 
					  // clang-format on
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  SymbolIdx ToSymbol(IndexTypeId id) const;
 | 
					  SymbolIdx ToSymbol(IndexTypeId id) const;
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user