1 //! Contains basic data about various HIR declarations.
5 use hir_expand::{name::Name, InFile};
12 item_tree::{AssocItem, ItemTreeId, ModItem},
13 type_ref::{TypeBound, TypeRef},
14 visibility::RawVisibility,
15 AssocContainerId, AssocItemId, ConstId, ConstLoc, FunctionId, FunctionLoc, HasModule, ImplId,
16 Intern, Lookup, ModuleId, StaticId, TraitId, TypeAliasId, TypeAliasLoc,
19 #[derive(Debug, Clone, PartialEq, Eq)]
20 pub struct FunctionData {
22 pub params: Vec<TypeRef>,
23 pub ret_type: TypeRef,
25 /// True if the first param is `self`. This is relevant to decide whether this
26 /// can be called as a method.
27 pub has_self_param: bool,
32 pub visibility: RawVisibility,
36 pub(crate) fn fn_data_query(db: &dyn DefDatabase, func: FunctionId) -> Arc<FunctionData> {
37 let loc = func.lookup(db);
38 let krate = loc.container.module(db).krate;
39 let item_tree = db.item_tree(loc.id.file_id);
40 let func = &item_tree[loc.id.value];
42 Arc::new(FunctionData {
43 name: func.name.clone(),
44 params: func.params.iter().map(|id| item_tree[*id].clone()).collect(),
45 ret_type: item_tree[func.ret_type].clone(),
46 attrs: item_tree.attrs(db, krate, ModItem::from(loc.id.value).into()).clone(),
47 has_self_param: func.has_self_param,
48 has_body: func.has_body,
49 is_unsafe: func.is_unsafe,
50 is_varargs: func.is_varargs,
51 is_extern: func.is_extern,
52 visibility: item_tree[func.visibility].clone(),
57 #[derive(Debug, Clone, PartialEq, Eq)]
58 pub struct TypeAliasData {
60 pub type_ref: Option<TypeRef>,
61 pub visibility: RawVisibility,
63 /// Bounds restricting the type alias itself (eg. `type Ty: Bound;` in a trait or impl).
64 pub bounds: Vec<TypeBound>,
68 pub(crate) fn type_alias_data_query(
71 ) -> Arc<TypeAliasData> {
72 let loc = typ.lookup(db);
73 let item_tree = db.item_tree(loc.id.file_id);
74 let typ = &item_tree[loc.id.value];
76 Arc::new(TypeAliasData {
77 name: typ.name.clone(),
78 type_ref: typ.type_ref.map(|id| item_tree[id].clone()),
79 visibility: item_tree[typ.visibility].clone(),
80 is_extern: typ.is_extern,
81 bounds: typ.bounds.to_vec(),
86 #[derive(Debug, Clone, PartialEq, Eq)]
87 pub struct TraitData {
89 pub items: Vec<(Name, AssocItemId)>,
94 pub(crate) fn trait_data_query(db: &dyn DefDatabase, tr: TraitId) -> Arc<TraitData> {
95 let tr_loc = tr.lookup(db);
96 let item_tree = db.item_tree(tr_loc.id.file_id);
97 let tr_def = &item_tree[tr_loc.id.value];
98 let name = tr_def.name.clone();
99 let auto = tr_def.auto;
100 let module_id = tr_loc.container.module(db);
101 let container = AssocContainerId::TraitId(tr);
102 let mut expander = Expander::new(db, tr_loc.id.file_id, module_id);
104 let items = collect_items(
108 tr_def.items.iter().copied(),
114 Arc::new(TraitData { name, items, auto })
117 pub fn associated_types(&self) -> impl Iterator<Item = TypeAliasId> + '_ {
118 self.items.iter().filter_map(|(_name, item)| match item {
119 AssocItemId::TypeAliasId(t) => Some(*t),
124 pub fn associated_type_by_name(&self, name: &Name) -> Option<TypeAliasId> {
125 self.items.iter().find_map(|(item_name, item)| match item {
126 AssocItemId::TypeAliasId(t) if item_name == name => Some(*t),
132 #[derive(Debug, Clone, PartialEq, Eq)]
133 pub struct ImplData {
134 pub target_trait: Option<TypeRef>,
135 pub target_type: TypeRef,
136 pub items: Vec<AssocItemId>,
137 pub is_negative: bool,
141 pub(crate) fn impl_data_query(db: &dyn DefDatabase, id: ImplId) -> Arc<ImplData> {
142 let _p = profile::span("impl_data_query");
143 let impl_loc = id.lookup(db);
145 let item_tree = db.item_tree(impl_loc.id.file_id);
146 let impl_def = &item_tree[impl_loc.id.value];
147 let target_trait = impl_def.target_trait.map(|id| item_tree[id].clone());
148 let target_type = item_tree[impl_def.target_type].clone();
149 let is_negative = impl_def.is_negative;
150 let module_id = impl_loc.container.module(db);
151 let container = AssocContainerId::ImplId(id);
152 let mut expander = Expander::new(db, impl_loc.id.file_id, module_id);
154 let items = collect_items(
158 impl_def.items.iter().copied(),
163 let items = items.into_iter().map(|(_, item)| item).collect();
165 Arc::new(ImplData { target_trait, target_type, items, is_negative })
169 #[derive(Debug, Clone, PartialEq, Eq)]
170 pub struct ConstData {
171 /// const _: () = ();
172 pub name: Option<Name>,
173 pub type_ref: TypeRef,
174 pub visibility: RawVisibility,
178 pub(crate) fn const_data_query(db: &dyn DefDatabase, konst: ConstId) -> Arc<ConstData> {
179 let loc = konst.lookup(db);
180 let item_tree = db.item_tree(loc.id.file_id);
181 let konst = &item_tree[loc.id.value];
184 name: konst.name.clone(),
185 type_ref: item_tree[konst.type_ref].clone(),
186 visibility: item_tree[konst.visibility].clone(),
191 #[derive(Debug, Clone, PartialEq, Eq)]
192 pub struct StaticData {
193 pub name: Option<Name>,
194 pub type_ref: TypeRef,
195 pub visibility: RawVisibility,
201 pub(crate) fn static_data_query(db: &dyn DefDatabase, konst: StaticId) -> Arc<StaticData> {
202 let node = konst.lookup(db);
203 let item_tree = db.item_tree(node.id.file_id);
204 let statik = &item_tree[node.id.value];
206 Arc::new(StaticData {
207 name: Some(statik.name.clone()),
208 type_ref: item_tree[statik.type_ref].clone(),
209 visibility: item_tree[statik.visibility].clone(),
210 mutable: statik.mutable,
211 is_extern: statik.is_extern,
217 db: &dyn DefDatabase,
219 expander: &mut Expander,
220 assoc_items: impl Iterator<Item = AssocItem>,
221 file_id: crate::HirFileId,
222 container: AssocContainerId,
224 ) -> Vec<(Name, AssocItemId)> {
229 let item_tree = db.item_tree(file_id);
230 let cfg_options = db.crate_graph()[module.krate].cfg_options.clone();
232 let mut items = Vec::new();
233 for item in assoc_items {
235 AssocItem::Function(id) => {
236 let item = &item_tree[id];
237 let attrs = item_tree.attrs(db, module.krate, ModItem::from(id).into());
238 if !attrs.is_cfg_enabled(&cfg_options) {
241 let def = FunctionLoc { container, id: ItemTreeId::new(file_id, id) }.intern(db);
242 items.push((item.name.clone(), def.into()));
245 AssocItem::Const(id) => {
246 let item = &item_tree[id];
247 let name = match item.name.clone() {
251 let def = ConstLoc { container, id: ItemTreeId::new(file_id, id) }.intern(db);
252 items.push((name, def.into()));
254 AssocItem::TypeAlias(id) => {
255 let item = &item_tree[id];
256 let def = TypeAliasLoc { container, id: ItemTreeId::new(file_id, id) }.intern(db);
257 items.push((item.name.clone(), def.into()));
259 AssocItem::MacroCall(call) => {
260 let call = &item_tree[call];
261 let ast_id_map = db.ast_id_map(file_id);
262 let root = db.parse_or_expand(file_id).unwrap();
263 let call = ast_id_map.get(call.ast_id).to_node(&root);
265 if let Some((mark, mac)) = expander.enter_expand(db, call).value {
266 let src: InFile<ast::MacroItems> = expander.to_source(mac);
267 let item_tree = db.item_tree(src.file_id);
269 item_tree.top_level_items().iter().filter_map(ModItem::as_assoc_item);
270 items.extend(collect_items(
280 expander.exit(db, mark);