2019-01-12 20:27:35 +00:00
|
|
|
//! Many kinds of items or constructs can have generic parameters: functions,
|
|
|
|
//! structs, impls, traits, etc. This module provides a common HIR for these
|
|
|
|
//! generic parameters. See also the `Generics` type and the `generics_of` query
|
|
|
|
//! in rustc.
|
|
|
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2019-07-04 20:05:17 +00:00
|
|
|
use ra_syntax::ast::{self, DefaultTypeParamOwner, NameOwner, TypeBoundsOwner, TypeParamsOwner};
|
2019-01-12 20:27:35 +00:00
|
|
|
|
2019-02-24 20:36:49 +00:00
|
|
|
use crate::{
|
2019-07-04 20:05:17 +00:00
|
|
|
db::{AstDatabase, DefDatabase, HirDatabase},
|
2019-07-07 21:29:38 +00:00
|
|
|
name::SELF_TYPE,
|
2019-07-04 20:05:17 +00:00
|
|
|
path::Path,
|
2019-08-13 21:09:08 +00:00
|
|
|
type_ref::{TypeBound, TypeRef},
|
2019-07-06 15:43:13 +00:00
|
|
|
AdtDef, AsName, Container, Enum, EnumVariant, Function, HasSource, ImplBlock, Name, Struct,
|
|
|
|
Trait, TypeAlias, Union,
|
2019-02-24 20:36:49 +00:00
|
|
|
};
|
2019-01-12 20:27:35 +00:00
|
|
|
|
|
|
|
/// Data about a generic parameter (to a function, struct, impl, ...).
|
|
|
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
|
|
|
pub struct GenericParam {
|
2019-03-23 07:53:48 +00:00
|
|
|
// FIXME: give generic params proper IDs
|
2019-01-12 20:27:35 +00:00
|
|
|
pub(crate) idx: u32,
|
|
|
|
pub(crate) name: Name,
|
2019-05-19 09:44:29 +00:00
|
|
|
pub(crate) default: Option<Path>,
|
2019-01-12 20:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Data about the generic parameters of a function, struct, impl, etc.
|
|
|
|
#[derive(Clone, PartialEq, Eq, Debug, Default)]
|
2019-01-19 17:58:04 +00:00
|
|
|
pub struct GenericParams {
|
2019-02-16 21:06:23 +00:00
|
|
|
pub(crate) parent_params: Option<Arc<GenericParams>>,
|
2019-01-12 20:27:35 +00:00
|
|
|
pub(crate) params: Vec<GenericParam>,
|
2019-04-21 12:21:58 +00:00
|
|
|
pub(crate) where_predicates: Vec<WherePredicate>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A single predicate from a where clause, i.e. `where Type: Trait`. Combined
|
|
|
|
/// where clauses like `where T: Foo + Bar` are turned into multiple of these.
|
2019-08-13 21:09:08 +00:00
|
|
|
/// It might still result in multiple actual predicates though, because of
|
|
|
|
/// associated type bindings like `Iterator<Item = u32>`.
|
2019-04-21 12:21:58 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
|
|
|
pub struct WherePredicate {
|
2019-05-05 12:21:00 +00:00
|
|
|
pub(crate) type_ref: TypeRef,
|
2019-08-13 21:09:08 +00:00
|
|
|
pub(crate) bound: TypeBound,
|
2019-01-12 20:27:35 +00:00
|
|
|
}
|
|
|
|
|
2019-04-14 09:15:11 +00:00
|
|
|
// FIXME: consts can have type parameters from their parents (i.e. associated consts of traits)
|
2019-01-24 12:28:50 +00:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
|
|
|
|
pub enum GenericDef {
|
|
|
|
Function(Function),
|
2019-09-12 21:10:16 +00:00
|
|
|
AdtDef(AdtDef),
|
2019-01-24 22:31:32 +00:00
|
|
|
Trait(Trait),
|
2019-02-24 20:36:49 +00:00
|
|
|
TypeAlias(TypeAlias),
|
2019-02-16 20:19:24 +00:00
|
|
|
ImplBlock(ImplBlock),
|
2019-07-06 15:43:13 +00:00
|
|
|
// enum variants cannot have generics themselves, but their parent enums
|
|
|
|
// can, and this makes some code easier to write
|
|
|
|
EnumVariant(EnumVariant),
|
2019-01-24 12:28:50 +00:00
|
|
|
}
|
2019-09-12 21:10:16 +00:00
|
|
|
impl_froms!(GenericDef: Function, AdtDef, Trait, TypeAlias, ImplBlock, EnumVariant);
|
|
|
|
|
|
|
|
impl From<Struct> for GenericDef {
|
|
|
|
fn from(it: Struct) -> GenericDef {
|
|
|
|
GenericDef::AdtDef(AdtDef::Struct(it))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Enum> for GenericDef {
|
|
|
|
fn from(it: Enum) -> GenericDef {
|
|
|
|
GenericDef::AdtDef(AdtDef::Enum(it))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Union> for GenericDef {
|
|
|
|
fn from(it: Union) -> GenericDef {
|
|
|
|
GenericDef::AdtDef(AdtDef::Union(it))
|
|
|
|
}
|
|
|
|
}
|
2019-01-24 12:28:50 +00:00
|
|
|
|
2019-01-19 17:58:04 +00:00
|
|
|
impl GenericParams {
|
2019-01-24 12:28:50 +00:00
|
|
|
pub(crate) fn generic_params_query(
|
2019-06-01 18:17:57 +00:00
|
|
|
db: &(impl DefDatabase + AstDatabase),
|
2019-01-24 12:28:50 +00:00
|
|
|
def: GenericDef,
|
|
|
|
) -> Arc<GenericParams> {
|
2019-01-19 17:58:04 +00:00
|
|
|
let mut generics = GenericParams::default();
|
2019-02-16 21:06:23 +00:00
|
|
|
let parent = match def {
|
2019-04-14 09:15:11 +00:00
|
|
|
GenericDef::Function(it) => it.container(db).map(GenericDef::from),
|
|
|
|
GenericDef::TypeAlias(it) => it.container(db).map(GenericDef::from),
|
2019-07-06 15:43:13 +00:00
|
|
|
GenericDef::EnumVariant(it) => Some(it.parent_enum(db).into()),
|
2019-09-12 21:10:16 +00:00
|
|
|
GenericDef::AdtDef(_) | GenericDef::Trait(_) => None,
|
2019-02-16 21:06:23 +00:00
|
|
|
GenericDef::ImplBlock(_) => None,
|
|
|
|
};
|
2019-03-31 18:02:16 +00:00
|
|
|
generics.parent_params = parent.map(|p| db.generic_params(p));
|
2019-02-16 21:06:23 +00:00
|
|
|
let start = generics.parent_params.as_ref().map(|p| p.params.len()).unwrap_or(0) as u32;
|
2019-08-22 12:28:08 +00:00
|
|
|
// FIXME: add `: Sized` bound for everything except for `Self` in traits
|
2019-01-24 12:28:50 +00:00
|
|
|
match def {
|
2019-07-19 07:43:01 +00:00
|
|
|
GenericDef::Function(it) => generics.fill(&it.source(db).ast, start),
|
2019-09-12 21:10:16 +00:00
|
|
|
GenericDef::AdtDef(AdtDef::Struct(it)) => generics.fill(&it.source(db).ast, start),
|
|
|
|
GenericDef::AdtDef(AdtDef::Union(it)) => generics.fill(&it.source(db).ast, start),
|
|
|
|
GenericDef::AdtDef(AdtDef::Enum(it)) => generics.fill(&it.source(db).ast, start),
|
2019-03-26 22:07:26 +00:00
|
|
|
GenericDef::Trait(it) => {
|
|
|
|
// traits get the Self type as an implicit first type parameter
|
2019-07-07 22:09:35 +00:00
|
|
|
generics.params.push(GenericParam { idx: start, name: SELF_TYPE, default: None });
|
2019-07-19 07:43:01 +00:00
|
|
|
generics.fill(&it.source(db).ast, start + 1);
|
2019-09-07 12:31:43 +00:00
|
|
|
// add super traits as bounds on Self
|
|
|
|
// i.e., trait Foo: Bar is equivalent to trait Foo where Self: Bar
|
|
|
|
let self_param = TypeRef::Path(SELF_TYPE.into());
|
|
|
|
generics.fill_bounds(&it.source(db).ast, self_param);
|
2019-03-26 22:07:26 +00:00
|
|
|
}
|
2019-07-19 07:43:01 +00:00
|
|
|
GenericDef::TypeAlias(it) => generics.fill(&it.source(db).ast, start),
|
2019-08-22 12:28:08 +00:00
|
|
|
// Note that we don't add `Self` here: in `impl`s, `Self` is not a
|
|
|
|
// type-parameter, but rather is a type-alias for impl's target
|
2019-09-07 12:31:43 +00:00
|
|
|
// type, so this is handled by the resolver.
|
2019-07-19 07:43:01 +00:00
|
|
|
GenericDef::ImplBlock(it) => generics.fill(&it.source(db).ast, start),
|
2019-07-06 15:43:13 +00:00
|
|
|
GenericDef::EnumVariant(_) => {}
|
2019-01-12 20:27:35 +00:00
|
|
|
}
|
2019-01-24 12:28:50 +00:00
|
|
|
|
2019-01-12 20:27:35 +00:00
|
|
|
Arc::new(generics)
|
|
|
|
}
|
|
|
|
|
2019-02-16 21:06:23 +00:00
|
|
|
fn fill(&mut self, node: &impl TypeParamsOwner, start: u32) {
|
2019-01-24 15:56:38 +00:00
|
|
|
if let Some(params) = node.type_param_list() {
|
2019-02-16 21:06:23 +00:00
|
|
|
self.fill_params(params, start)
|
2019-01-24 15:56:38 +00:00
|
|
|
}
|
2019-04-21 12:21:58 +00:00
|
|
|
if let Some(where_clause) = node.where_clause() {
|
|
|
|
self.fill_where_predicates(where_clause);
|
|
|
|
}
|
2019-01-24 15:56:38 +00:00
|
|
|
}
|
|
|
|
|
2019-09-07 12:31:43 +00:00
|
|
|
fn fill_bounds(&mut self, node: &impl ast::TypeBoundsOwner, type_ref: TypeRef) {
|
|
|
|
for bound in
|
|
|
|
node.type_bound_list().iter().flat_map(|type_bound_list| type_bound_list.bounds())
|
|
|
|
{
|
|
|
|
self.add_where_predicate_from_bound(bound, type_ref.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-19 07:43:01 +00:00
|
|
|
fn fill_params(&mut self, params: ast::TypeParamList, start: u32) {
|
2019-01-24 12:28:50 +00:00
|
|
|
for (idx, type_param) in params.type_params().enumerate() {
|
2019-07-19 07:43:01 +00:00
|
|
|
let name = type_param.name().map_or_else(Name::missing, |it| it.as_name());
|
2019-05-19 09:44:29 +00:00
|
|
|
let default = type_param.default_type().and_then(|t| t.path()).and_then(Path::from_ast);
|
|
|
|
|
|
|
|
let param = GenericParam { idx: idx as u32 + start, name: name.clone(), default };
|
2019-01-24 12:28:50 +00:00
|
|
|
self.params.push(param);
|
2019-05-11 14:49:55 +00:00
|
|
|
|
|
|
|
let type_ref = TypeRef::Path(name.into());
|
2019-09-07 12:31:43 +00:00
|
|
|
self.fill_bounds(&type_param, type_ref);
|
2019-01-24 12:28:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-19 07:43:01 +00:00
|
|
|
fn fill_where_predicates(&mut self, where_clause: ast::WhereClause) {
|
2019-04-21 12:21:58 +00:00
|
|
|
for pred in where_clause.predicates() {
|
|
|
|
let type_ref = match pred.type_ref() {
|
|
|
|
Some(type_ref) => type_ref,
|
|
|
|
None => continue,
|
|
|
|
};
|
2019-05-11 14:49:55 +00:00
|
|
|
let type_ref = TypeRef::from_ast(type_ref);
|
2019-04-21 12:21:58 +00:00
|
|
|
for bound in pred.type_bound_list().iter().flat_map(|l| l.bounds()) {
|
2019-05-11 14:49:55 +00:00
|
|
|
self.add_where_predicate_from_bound(bound, type_ref.clone());
|
2019-04-21 12:21:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-19 07:43:01 +00:00
|
|
|
fn add_where_predicate_from_bound(&mut self, bound: ast::TypeBound, type_ref: TypeRef) {
|
2019-08-22 12:28:08 +00:00
|
|
|
if bound.has_question_mark() {
|
|
|
|
// FIXME: remove this bound
|
|
|
|
return;
|
|
|
|
}
|
2019-08-13 21:09:08 +00:00
|
|
|
let bound = TypeBound::from_ast(bound);
|
|
|
|
self.where_predicates.push(WherePredicate { type_ref, bound });
|
2019-05-11 14:49:55 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 20:27:35 +00:00
|
|
|
pub(crate) fn find_by_name(&self, name: &Name) -> Option<&GenericParam> {
|
|
|
|
self.params.iter().find(|p| &p.name == name)
|
|
|
|
}
|
2019-02-16 21:06:23 +00:00
|
|
|
|
|
|
|
pub fn count_parent_params(&self) -> usize {
|
|
|
|
self.parent_params.as_ref().map(|p| p.count_params_including_parent()).unwrap_or(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn count_params_including_parent(&self) -> usize {
|
|
|
|
let parent_count = self.count_parent_params();
|
|
|
|
parent_count + self.params.len()
|
|
|
|
}
|
2019-02-20 21:36:54 +00:00
|
|
|
|
|
|
|
fn for_each_param<'a>(&'a self, f: &mut impl FnMut(&'a GenericParam)) {
|
|
|
|
if let Some(parent) = &self.parent_params {
|
|
|
|
parent.for_each_param(f);
|
|
|
|
}
|
|
|
|
self.params.iter().for_each(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn params_including_parent(&self) -> Vec<&GenericParam> {
|
|
|
|
let mut vec = Vec::with_capacity(self.count_params_including_parent());
|
|
|
|
self.for_each_param(&mut |p| vec.push(p));
|
|
|
|
vec
|
|
|
|
}
|
2019-01-12 20:27:35 +00:00
|
|
|
}
|
2019-04-14 09:15:11 +00:00
|
|
|
|
2019-05-05 12:21:00 +00:00
|
|
|
impl GenericDef {
|
|
|
|
pub(crate) fn resolver(&self, db: &impl HirDatabase) -> crate::Resolver {
|
|
|
|
match self {
|
|
|
|
GenericDef::Function(inner) => inner.resolver(db),
|
2019-09-12 21:10:16 +00:00
|
|
|
GenericDef::AdtDef(adt) => adt.resolver(db),
|
2019-05-05 12:21:00 +00:00
|
|
|
GenericDef::Trait(inner) => inner.resolver(db),
|
|
|
|
GenericDef::TypeAlias(inner) => inner.resolver(db),
|
|
|
|
GenericDef::ImplBlock(inner) => inner.resolver(db),
|
2019-07-06 15:43:13 +00:00
|
|
|
GenericDef::EnumVariant(inner) => inner.parent_enum(db).resolver(db),
|
2019-05-05 12:21:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-14 09:15:11 +00:00
|
|
|
impl From<Container> for GenericDef {
|
|
|
|
fn from(c: Container) -> Self {
|
|
|
|
match c {
|
|
|
|
Container::Trait(trait_) => trait_.into(),
|
|
|
|
Container::ImplBlock(impl_block) => impl_block.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-14 11:07:45 +00:00
|
|
|
|
2019-05-20 22:02:29 +00:00
|
|
|
pub trait HasGenericParams: Copy {
|
2019-04-14 11:07:45 +00:00
|
|
|
fn generic_params(self, db: &impl DefDatabase) -> Arc<GenericParams>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> HasGenericParams for T
|
|
|
|
where
|
2019-05-20 22:02:29 +00:00
|
|
|
T: Into<GenericDef> + Copy,
|
2019-04-14 11:07:45 +00:00
|
|
|
{
|
|
|
|
fn generic_params(self, db: &impl DefDatabase) -> Arc<GenericParams> {
|
|
|
|
db.generic_params(self.into())
|
|
|
|
}
|
|
|
|
}
|