1 //! Defines `Body`: a lowered representation of bodies of functions, statics and
6 use std::{mem, ops::Index, sync::Arc};
8 use arena::{map::ArenaMap, Arena};
11 use drop_bomb::DropBomb;
13 use hir_expand::{ast_id_map::AstIdMap, hygiene::Hygiene, AstId, HirFileId, InFile, MacroDefId};
14 use rustc_hash::FxHashMap;
15 use syntax::{ast, AstNode, AstPtr};
18 pub(crate) use lower::LowerCtx;
23 expr::{Expr, ExprId, Pat, PatId},
24 item_scope::BuiltinShadowMode,
25 item_scope::ItemScope,
27 path::{ModPath, Path},
29 AsMacroCall, DefWithBodyId, HasModule, Lookup, ModuleId,
32 /// A subset of Expander that only deals with cfg attributes. We only need it to
33 /// avoid cyclic queries in crate def map during enum processing.
34 pub(crate) struct CfgExpander {
35 cfg_options: CfgOptions,
39 pub(crate) struct Expander {
40 cfg_expander: CfgExpander,
41 crate_def_map: Arc<CrateDefMap>,
42 current_file_id: HirFileId,
43 ast_id_map: Arc<AstIdMap>,
45 recursion_limit: usize,
49 const EXPANSION_RECURSION_LIMIT: usize = 32;
52 const EXPANSION_RECURSION_LIMIT: usize = 128;
57 current_file_id: HirFileId,
60 let hygiene = Hygiene::new(db.upcast(), current_file_id);
61 let cfg_options = db.crate_graph()[krate].cfg_options.clone();
62 CfgExpander { cfg_options, hygiene }
65 pub(crate) fn parse_attrs(&self, owner: &dyn ast::AttrsOwner) -> Attrs {
66 Attrs::new(owner, &self.hygiene)
69 pub(crate) fn is_cfg_enabled(&self, owner: &dyn ast::AttrsOwner) -> bool {
70 let attrs = self.parse_attrs(owner);
71 attrs.is_cfg_enabled(&self.cfg_options)
78 current_file_id: HirFileId,
81 let cfg_expander = CfgExpander::new(db, current_file_id, module.krate);
82 let crate_def_map = db.crate_def_map(module.krate);
83 let ast_id_map = db.ast_id_map(current_file_id);
94 pub(crate) fn enter_expand<T: ast::AstNode>(
97 local_scope: Option<&ItemScope>,
98 macro_call: ast::MacroCall,
99 ) -> Option<(Mark, T)> {
100 self.recursion_limit += 1;
101 if self.recursion_limit > EXPANSION_RECURSION_LIMIT {
102 mark::hit!(your_stack_belongs_to_me);
106 let macro_call = InFile::new(self.current_file_id, ¯o_call);
108 if let Some(call_id) = macro_call.as_call_id(db, self.crate_def_map.krate, |path| {
109 if let Some(local_scope) = local_scope {
110 if let Some(def) = path.as_ident().and_then(|n| local_scope.get_legacy_macro(n)) {
114 self.resolve_path_as_macro(db, &path)
116 let file_id = call_id.as_file();
117 if let Some(node) = db.parse_or_expand(file_id) {
118 if let Some(expr) = T::cast(node) {
119 log::debug!("macro expansion {:#?}", expr.syntax());
122 file_id: self.current_file_id,
123 ast_id_map: mem::take(&mut self.ast_id_map),
124 bomb: DropBomb::new("expansion mark dropped"),
126 self.cfg_expander.hygiene = Hygiene::new(db.upcast(), file_id);
127 self.current_file_id = file_id;
128 self.ast_id_map = db.ast_id_map(file_id);
129 return Some((mark, expr));
134 // FIXME: Instead of just dropping the error from expansion
139 pub(crate) fn exit(&mut self, db: &dyn DefDatabase, mut mark: Mark) {
140 self.cfg_expander.hygiene = Hygiene::new(db.upcast(), mark.file_id);
141 self.current_file_id = mark.file_id;
142 self.ast_id_map = mem::take(&mut mark.ast_id_map);
143 self.recursion_limit -= 1;
147 pub(crate) fn to_source<T>(&self, value: T) -> InFile<T> {
148 InFile { file_id: self.current_file_id, value }
151 pub(crate) fn is_cfg_enabled(&self, owner: &dyn ast::AttrsOwner) -> bool {
152 self.cfg_expander.is_cfg_enabled(owner)
155 fn parse_path(&mut self, path: ast::Path) -> Option<Path> {
156 Path::from_src(path, &self.cfg_expander.hygiene)
159 fn resolve_path_as_macro(&self, db: &dyn DefDatabase, path: &ModPath) -> Option<MacroDefId> {
161 .resolve_path(db, self.module.local_id, path, BuiltinShadowMode::Other)
166 fn ast_id<N: AstNode>(&self, item: &N) -> AstId<N> {
167 let file_local_id = self.ast_id_map.ast_id(item);
168 AstId::new(self.current_file_id, file_local_id)
172 pub(crate) struct Mark {
174 ast_id_map: Arc<AstIdMap>,
178 /// The body of an item (function, const etc.).
179 #[derive(Debug, Eq, PartialEq)]
181 pub exprs: Arena<Expr>,
182 pub pats: Arena<Pat>,
183 /// The patterns for the function's parameters. While the parameter types are
184 /// part of the function signature, the patterns are not (they don't change
185 /// the external type of the function).
187 /// If this `Body` is for the body of a constant, this will just be
189 pub params: Vec<PatId>,
190 /// The `ExprId` of the actual body expression.
191 pub body_expr: ExprId,
192 pub item_scope: ItemScope,
195 pub type ExprPtr = AstPtr<ast::Expr>;
196 pub type ExprSource = InFile<ExprPtr>;
198 pub type PatPtr = Either<AstPtr<ast::Pat>, AstPtr<ast::SelfParam>>;
199 pub type PatSource = InFile<PatPtr>;
201 /// An item body together with the mapping from syntax nodes to HIR expression
202 /// IDs. This is needed to go from e.g. a position in a file to the HIR
203 /// expression containing it; but for type inference etc., we want to operate on
204 /// a structure that is agnostic to the actual positions of expressions in the
205 /// file, so that we don't recompute types whenever some whitespace is typed.
207 /// One complication here is that, due to macro expansion, a single `Body` might
208 /// be spread across several files. So, for each ExprId and PatId, we record
209 /// both the HirFileId and the position inside the file. However, we only store
210 /// AST -> ExprId mapping for non-macro files, as it is not clear how to handle
211 /// this properly for macros.
212 #[derive(Default, Debug, Eq, PartialEq)]
213 pub struct BodySourceMap {
214 expr_map: FxHashMap<ExprSource, ExprId>,
215 expr_map_back: ArenaMap<ExprId, Result<ExprSource, SyntheticSyntax>>,
216 pat_map: FxHashMap<PatSource, PatId>,
217 pat_map_back: ArenaMap<PatId, Result<PatSource, SyntheticSyntax>>,
218 field_map: FxHashMap<(ExprId, usize), InFile<AstPtr<ast::RecordExprField>>>,
219 expansions: FxHashMap<InFile<AstPtr<ast::MacroCall>>, HirFileId>,
222 #[derive(Default, Debug, Eq, PartialEq, Clone, Copy)]
223 pub struct SyntheticSyntax;
226 pub(crate) fn body_with_source_map_query(
227 db: &dyn DefDatabase,
229 ) -> (Arc<Body>, Arc<BodySourceMap>) {
230 let _p = profile::span("body_with_source_map_query");
231 let mut params = None;
233 let (file_id, module, body) = match def {
234 DefWithBodyId::FunctionId(f) => {
235 let f = f.lookup(db);
236 let src = f.source(db);
237 params = src.value.param_list();
238 (src.file_id, f.module(db), src.value.body().map(ast::Expr::from))
240 DefWithBodyId::ConstId(c) => {
241 let c = c.lookup(db);
242 let src = c.source(db);
243 (src.file_id, c.module(db), src.value.body())
245 DefWithBodyId::StaticId(s) => {
246 let s = s.lookup(db);
247 let src = s.source(db);
248 (src.file_id, s.module(db), src.value.body())
251 let expander = Expander::new(db, file_id, module);
252 let (body, source_map) = Body::new(db, def, expander, params, body);
253 (Arc::new(body), Arc::new(source_map))
256 pub(crate) fn body_query(db: &dyn DefDatabase, def: DefWithBodyId) -> Arc<Body> {
257 db.body_with_source_map(def).0
261 db: &dyn DefDatabase,
264 params: Option<ast::ParamList>,
265 body: Option<ast::Expr>,
266 ) -> (Body, BodySourceMap) {
267 lower::lower(db, def, expander, params, body)
271 impl Index<ExprId> for Body {
274 fn index(&self, expr: ExprId) -> &Expr {
279 impl Index<PatId> for Body {
282 fn index(&self, pat: PatId) -> &Pat {
288 pub fn expr_syntax(&self, expr: ExprId) -> Result<ExprSource, SyntheticSyntax> {
289 self.expr_map_back[expr].clone()
292 pub fn node_expr(&self, node: InFile<&ast::Expr>) -> Option<ExprId> {
293 let src = node.map(|it| AstPtr::new(it));
294 self.expr_map.get(&src).cloned()
297 pub fn node_macro_file(&self, node: InFile<&ast::MacroCall>) -> Option<HirFileId> {
298 let src = node.map(|it| AstPtr::new(it));
299 self.expansions.get(&src).cloned()
302 pub fn pat_syntax(&self, pat: PatId) -> Result<PatSource, SyntheticSyntax> {
303 self.pat_map_back[pat].clone()
306 pub fn node_pat(&self, node: InFile<&ast::Pat>) -> Option<PatId> {
307 let src = node.map(|it| Either::Left(AstPtr::new(it)));
308 self.pat_map.get(&src).cloned()
311 pub fn node_self_param(&self, node: InFile<&ast::SelfParam>) -> Option<PatId> {
312 let src = node.map(|it| Either::Right(AstPtr::new(it)));
313 self.pat_map.get(&src).cloned()
316 pub fn field_syntax(&self, expr: ExprId, field: usize) -> InFile<AstPtr<ast::RecordExprField>> {
317 self.field_map[&(expr, field)].clone()
323 use base_db::{fixture::WithFixture, SourceDatabase};
324 use test_utils::mark;
326 use crate::ModuleDefId;
330 fn lower(ra_fixture: &str) -> Arc<Body> {
331 let (db, file_id) = crate::test_db::TestDB::with_single_file(ra_fixture);
333 let krate = db.crate_graph().iter().next().unwrap();
334 let def_map = db.crate_def_map(krate);
335 let module = def_map.modules_for_file(file_id).next().unwrap();
336 let module = &def_map[module];
337 let fn_def = match module.scope.declarations().next().unwrap() {
338 ModuleDefId::FunctionId(it) => it,
342 db.body(fn_def.into())
346 fn your_stack_belongs_to_me() {
347 mark::check!(your_stack_belongs_to_me);
350 macro_rules! n_nuple {
353 (n_nuple!($($rest)*)None,)
356 fn main() { n_nuple!(1,2,3); }