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,
|
|
|
|
type_ref::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.
|
|
|
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
|
|
|
pub struct WherePredicate {
|
2019-05-05 12:21:00 +00:00
|
|
|
pub(crate) type_ref: TypeRef,
|
|
|
|
pub(crate) trait_ref: Path,
|
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-01-24 14:54:18 +00:00
|
|
|
Struct(Struct),
|
2019-05-23 17:18:47 +00:00
|
|
|
Union(Union),
|
2019-01-24 15:56:38 +00:00
|
|
|
Enum(Enum),
|
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-07-06 15:43:13 +00:00
|
|
|
impl_froms!(GenericDef: Function, Struct, Union, Enum, Trait, TypeAlias, ImplBlock, EnumVariant);
|
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-05-23 17:18:47 +00:00
|
|
|
GenericDef::Struct(_)
|
|
|
|
| GenericDef::Union(_)
|
|
|
|
| GenericDef::Enum(_)
|
|
|
|
| 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-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),
|
|
|
|
GenericDef::Struct(it) => generics.fill(&it.source(db).ast, start),
|
|
|
|
GenericDef::Union(it) => generics.fill(&it.source(db).ast, start),
|
|
|
|
GenericDef::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-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),
|
|
|
|
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-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());
|
|
|
|
for bound in type_param
|
|
|
|
.type_bound_list()
|
|
|
|
.iter()
|
|
|
|
.flat_map(|type_bound_list| type_bound_list.bounds())
|
|
|
|
{
|
|
|
|
self.add_where_predicate_from_bound(bound, type_ref.clone());
|
|
|
|
}
|
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-05-11 14:49:55 +00:00
|
|
|
let path = bound
|
|
|
|
.type_ref()
|
|
|
|
.and_then(|tr| match tr.kind() {
|
|
|
|
ast::TypeRefKind::PathType(path) => path.path(),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.and_then(Path::from_ast);
|
|
|
|
let path = match path {
|
|
|
|
Some(p) => p,
|
|
|
|
None => return,
|
|
|
|
};
|
|
|
|
self.where_predicates.push(WherePredicate { type_ref, trait_ref: path });
|
|
|
|
}
|
|
|
|
|
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),
|
|
|
|
GenericDef::Struct(inner) => inner.resolver(db),
|
2019-05-23 17:18:47 +00:00
|
|
|
GenericDef::Union(inner) => inner.resolver(db),
|
2019-05-05 12:21:00 +00:00
|
|
|
GenericDef::Enum(inner) => inner.resolver(db),
|
|
|
|
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-04-20 10:34:36 +00:00
|
|
|
impl From<crate::adt::AdtDef> for GenericDef {
|
|
|
|
fn from(adt: crate::adt::AdtDef) -> Self {
|
|
|
|
match adt {
|
|
|
|
AdtDef::Struct(s) => s.into(),
|
2019-05-23 17:18:47 +00:00
|
|
|
AdtDef::Union(u) => u.into(),
|
2019-04-20 10:34:36 +00:00
|
|
|
AdtDef::Enum(e) => e.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|