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,
30 pub visibility: RawVisibility,
34 pub(crate) fn fn_data_query(db: &dyn DefDatabase, func: FunctionId) -> Arc<FunctionData> {
35 let loc = func.lookup(db);
36 let item_tree = db.item_tree(loc.id.file_id);
37 let func = &item_tree[loc.id.value];
39 Arc::new(FunctionData {
40 name: func.name.clone(),
41 params: func.params.to_vec(),
42 ret_type: func.ret_type.clone(),
43 attrs: item_tree.attrs(ModItem::from(loc.id.value).into()).clone(),
44 has_self_param: func.has_self_param,
45 is_unsafe: func.is_unsafe,
46 is_varargs: func.is_varargs,
47 visibility: item_tree[func.visibility].clone(),
52 #[derive(Debug, Clone, PartialEq, Eq)]
53 pub struct TypeAliasData {
55 pub type_ref: Option<TypeRef>,
56 pub visibility: RawVisibility,
57 /// Bounds restricting the type alias itself (eg. `type Ty: Bound;` in a trait or impl).
58 pub bounds: Vec<TypeBound>,
62 pub(crate) fn type_alias_data_query(
65 ) -> Arc<TypeAliasData> {
66 let loc = typ.lookup(db);
67 let item_tree = db.item_tree(loc.id.file_id);
68 let typ = &item_tree[loc.id.value];
70 Arc::new(TypeAliasData {
71 name: typ.name.clone(),
72 type_ref: typ.type_ref.clone(),
73 visibility: item_tree[typ.visibility].clone(),
74 bounds: typ.bounds.to_vec(),
79 #[derive(Debug, Clone, PartialEq, Eq)]
80 pub struct TraitData {
82 pub items: Vec<(Name, AssocItemId)>,
87 pub(crate) fn trait_data_query(db: &dyn DefDatabase, tr: TraitId) -> Arc<TraitData> {
88 let tr_loc = tr.lookup(db);
89 let item_tree = db.item_tree(tr_loc.id.file_id);
90 let tr_def = &item_tree[tr_loc.id.value];
91 let name = tr_def.name.clone();
92 let auto = tr_def.auto;
93 let module_id = tr_loc.container.module(db);
94 let container = AssocContainerId::TraitId(tr);
95 let mut expander = Expander::new(db, tr_loc.id.file_id, module_id);
97 let items = collect_items(
101 tr_def.items.iter().copied(),
107 Arc::new(TraitData { name, items, auto })
110 pub fn associated_types(&self) -> impl Iterator<Item = TypeAliasId> + '_ {
111 self.items.iter().filter_map(|(_name, item)| match item {
112 AssocItemId::TypeAliasId(t) => Some(*t),
117 pub fn associated_type_by_name(&self, name: &Name) -> Option<TypeAliasId> {
118 self.items.iter().find_map(|(item_name, item)| match item {
119 AssocItemId::TypeAliasId(t) if item_name == name => Some(*t),
125 #[derive(Debug, Clone, PartialEq, Eq)]
126 pub struct ImplData {
127 pub target_trait: Option<TypeRef>,
128 pub target_type: TypeRef,
129 pub items: Vec<AssocItemId>,
130 pub is_negative: bool,
134 pub(crate) fn impl_data_query(db: &dyn DefDatabase, id: ImplId) -> Arc<ImplData> {
135 let _p = profile::span("impl_data_query");
136 let impl_loc = id.lookup(db);
138 let item_tree = db.item_tree(impl_loc.id.file_id);
139 let impl_def = &item_tree[impl_loc.id.value];
140 let target_trait = impl_def.target_trait.clone();
141 let target_type = impl_def.target_type.clone();
142 let is_negative = impl_def.is_negative;
143 let module_id = impl_loc.container.module(db);
144 let container = AssocContainerId::ImplId(id);
145 let mut expander = Expander::new(db, impl_loc.id.file_id, module_id);
147 let items = collect_items(
151 impl_def.items.iter().copied(),
156 let items = items.into_iter().map(|(_, item)| item).collect();
158 Arc::new(ImplData { target_trait, target_type, items, is_negative })
162 #[derive(Debug, Clone, PartialEq, Eq)]
163 pub struct ConstData {
164 /// const _: () = ();
165 pub name: Option<Name>,
166 pub type_ref: TypeRef,
167 pub visibility: RawVisibility,
171 pub(crate) fn const_data_query(db: &dyn DefDatabase, konst: ConstId) -> Arc<ConstData> {
172 let loc = konst.lookup(db);
173 let item_tree = db.item_tree(loc.id.file_id);
174 let konst = &item_tree[loc.id.value];
177 name: konst.name.clone(),
178 type_ref: konst.type_ref.clone(),
179 visibility: item_tree[konst.visibility].clone(),
184 #[derive(Debug, Clone, PartialEq, Eq)]
185 pub struct StaticData {
186 pub name: Option<Name>,
187 pub type_ref: TypeRef,
188 pub visibility: RawVisibility,
193 pub(crate) fn static_data_query(db: &dyn DefDatabase, konst: StaticId) -> Arc<StaticData> {
194 let node = konst.lookup(db);
195 let item_tree = db.item_tree(node.id.file_id);
196 let statik = &item_tree[node.id.value];
198 Arc::new(StaticData {
199 name: Some(statik.name.clone()),
200 type_ref: statik.type_ref.clone(),
201 visibility: item_tree[statik.visibility].clone(),
202 mutable: statik.mutable,
208 db: &dyn DefDatabase,
210 expander: &mut Expander,
211 assoc_items: impl Iterator<Item = AssocItem>,
212 file_id: crate::HirFileId,
213 container: AssocContainerId,
215 ) -> Vec<(Name, AssocItemId)> {
220 let item_tree = db.item_tree(file_id);
221 let cfg_options = db.crate_graph()[module.krate].cfg_options.clone();
223 let mut items = Vec::new();
224 for item in assoc_items {
226 AssocItem::Function(id) => {
227 let item = &item_tree[id];
228 let attrs = item_tree.attrs(ModItem::from(id).into());
229 if !attrs.is_cfg_enabled(&cfg_options) {
232 let def = FunctionLoc { container, id: ItemTreeId::new(file_id, id) }.intern(db);
233 items.push((item.name.clone(), def.into()));
236 AssocItem::Const(id) => {
237 let item = &item_tree[id];
238 let name = match item.name.clone() {
242 let def = ConstLoc { container, id: ItemTreeId::new(file_id, id) }.intern(db);
243 items.push((name, def.into()));
245 AssocItem::TypeAlias(id) => {
246 let item = &item_tree[id];
247 let def = TypeAliasLoc { container, id: ItemTreeId::new(file_id, id) }.intern(db);
248 items.push((item.name.clone(), def.into()));
250 AssocItem::MacroCall(call) => {
251 let call = &item_tree[call];
252 let ast_id_map = db.ast_id_map(file_id);
253 let root = db.parse_or_expand(file_id).unwrap();
254 let call = ast_id_map.get(call.ast_id).to_node(&root);
256 if let Some((mark, mac)) = expander.enter_expand(db, None, call) {
257 let src: InFile<ast::MacroItems> = expander.to_source(mac);
258 let item_tree = db.item_tree(src.file_id);
260 item_tree.top_level_items().iter().filter_map(ModItem::as_assoc_item);
261 items.extend(collect_items(
271 expander.exit(db, mark);