Auto merge of #15677 - Veykril:param-ast-id, r=Veykril

Allocate ast ids for parameters

Since these can have attributes attached to them, we'll need this sooner or later (sooner being me tinkering with the token map right now)
This commit is contained in:
bors 2023-09-28 11:21:15 +00:00
commit b3f45745ea
5 changed files with 34 additions and 20 deletions

View File

@ -15,9 +15,7 @@ use crate::{
attr::Attrs, attr::Attrs,
db::DefDatabase, db::DefDatabase,
expander::{Expander, Mark}, expander::{Expander, Mark},
item_tree::{ item_tree::{self, AssocItem, FnFlags, ItemTree, ItemTreeId, MacroCall, ModItem, TreeId},
self, AssocItem, FnFlags, ItemTree, ItemTreeId, MacroCall, ModItem, Param, TreeId,
},
macro_call_as_call_id, macro_id_to_def_id, macro_call_as_call_id, macro_id_to_def_id,
nameres::{ nameres::{
attr_resolution::ResolvedAttr, attr_resolution::ResolvedAttr,
@ -69,7 +67,7 @@ impl FunctionData {
let is_varargs = enabled_params let is_varargs = enabled_params
.clone() .clone()
.next_back() .next_back()
.map_or(false, |param| matches!(item_tree[param], Param::Varargs)); .map_or(false, |param| item_tree[param].type_ref.is_none());
let mut flags = func.flags; let mut flags = func.flags;
if is_varargs { if is_varargs {
@ -105,10 +103,7 @@ impl FunctionData {
name: func.name.clone(), name: func.name.clone(),
params: enabled_params params: enabled_params
.clone() .clone()
.filter_map(|id| match &item_tree[id] { .filter_map(|id| item_tree[id].type_ref.clone())
Param::Normal(ty) => Some(ty.clone()),
Param::Varargs => None,
})
.collect(), .collect(),
ret_type: func.ret_type.clone(), ret_type: func.ret_type.clone(),
attrs: item_tree.attrs(db, krate, ModItem::from(loc.id.value).into()), attrs: item_tree.attrs(db, krate, ModItem::from(loc.id.value).into()),

View File

@ -613,10 +613,17 @@ pub struct Function {
pub(crate) flags: FnFlags, pub(crate) flags: FnFlags,
} }
#[derive(Debug, Clone, Eq, PartialEq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub enum Param { pub struct Param {
Normal(Interned<TypeRef>), /// This is [`None`] for varargs
Varargs, pub type_ref: Option<Interned<TypeRef>>,
pub ast_id: ParamAstId,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ParamAstId {
Param(FileAstId<ast::Param>),
SelfParam(FileAstId<ast::SelfParam>),
} }
bitflags::bitflags! { bitflags::bitflags! {

View File

@ -295,8 +295,12 @@ impl<'a> Ctx<'a> {
} }
} }
}; };
let ty = Interned::new(self_type); let type_ref = Interned::new(self_type);
let idx = self.data().params.alloc(Param::Normal(ty)); let ast_id = self.source_ast_id_map.ast_id(&self_param);
let idx = self.data().params.alloc(Param {
type_ref: Some(type_ref),
ast_id: ParamAstId::SelfParam(ast_id),
});
self.add_attrs( self.add_attrs(
idx.into(), idx.into(),
RawAttrs::new(self.db.upcast(), &self_param, self.hygiene()), RawAttrs::new(self.db.upcast(), &self_param, self.hygiene()),
@ -305,11 +309,19 @@ impl<'a> Ctx<'a> {
} }
for param in param_list.params() { for param in param_list.params() {
let idx = match param.dotdotdot_token() { let idx = match param.dotdotdot_token() {
Some(_) => self.data().params.alloc(Param::Varargs), Some(_) => {
let ast_id = self.source_ast_id_map.ast_id(&param);
self.data()
.params
.alloc(Param { type_ref: None, ast_id: ParamAstId::Param(ast_id) })
}
None => { None => {
let type_ref = TypeRef::from_ast_opt(&self.body_ctx, param.ty()); let type_ref = TypeRef::from_ast_opt(&self.body_ctx, param.ty());
let ty = Interned::new(type_ref); let ty = Interned::new(type_ref);
self.data().params.alloc(Param::Normal(ty)) let ast_id = self.source_ast_id_map.ast_id(&param);
self.data()
.params
.alloc(Param { type_ref: Some(ty), ast_id: ParamAstId::Param(ast_id) })
} }
}; };
self.add_attrs(idx.into(), RawAttrs::new(self.db.upcast(), &param, self.hygiene())); self.add_attrs(idx.into(), RawAttrs::new(self.db.upcast(), &param, self.hygiene()));

View File

@ -261,15 +261,15 @@ impl Printer<'_> {
self.indented(|this| { self.indented(|this| {
for param in params.clone() { for param in params.clone() {
this.print_attrs_of(param, "\n"); this.print_attrs_of(param, "\n");
match &this.tree[param] { match &this.tree[param].type_ref {
Param::Normal(ty) => { Some(ty) => {
if flags.contains(FnFlags::HAS_SELF_PARAM) { if flags.contains(FnFlags::HAS_SELF_PARAM) {
w!(this, "self: "); w!(this, "self: ");
} }
this.print_type_ref(ty); this.print_type_ref(ty);
wln!(this, ","); wln!(this, ",");
} }
Param::Varargs => { None => {
wln!(this, "..."); wln!(this, "...");
} }
}; };

View File

@ -99,7 +99,7 @@ register_ast_id_node! {
TraitAlias, TraitAlias,
TypeAlias, TypeAlias,
Use, Use,
AssocItem, BlockExpr, Variant, RecordField, TupleField, ConstArg AssocItem, BlockExpr, Variant, RecordField, TupleField, ConstArg, Param, SelfParam
} }
/// Maps items' `SyntaxNode`s to `ErasedFileAstId`s and back. /// Maps items' `SyntaxNode`s to `ErasedFileAstId`s and back.