diff --git a/crates/hir-def/src/db.rs b/crates/hir-def/src/db.rs index bf6cc1dcadec..598a850898bb 100644 --- a/crates/hir-def/src/db.rs +++ b/crates/hir-def/src/db.rs @@ -10,12 +10,12 @@ use triomphe::Arc; use crate::{ attr::{Attrs, AttrsWithOwner}, - body::{scope::ExprScopes, Body, BodySourceMap}, data::{ adt::{EnumData, EnumVariantData, StructData, VariantData}, ConstData, ExternCrateDeclData, FunctionData, ImplData, Macro2Data, MacroRulesData, ProcMacroData, StaticData, TraitAliasData, TraitData, TypeAliasData, }, + expr_store::{scope::ExprScopes, Body, BodySourceMap}, generics::GenericParams, import_map::ImportMap, item_tree::{AttrOwner, ItemTree, ItemTreeSourceMaps}, diff --git a/crates/hir-def/src/body.rs b/crates/hir-def/src/expr_store.rs similarity index 72% rename from crates/hir-def/src/body.rs rename to crates/hir-def/src/expr_store.rs index de4392493063..9df6eaade757 100644 --- a/crates/hir-def/src/body.rs +++ b/crates/hir-def/src/expr_store.rs @@ -1,18 +1,19 @@ -//! Defines `Body`: a lowered representation of bodies of functions, statics and +//! Defines `ExpressionStore`: a lowered representation of functions, statics and //! consts. +mod body; mod lower; mod pretty; pub mod scope; + #[cfg(test)] mod tests; use std::ops::{Deref, Index}; -use base_db::CrateId; use cfg::{CfgExpr, CfgOptions}; use either::Either; use hir_expand::{name::Name, ExpandError, InFile}; -use la_arena::{Arena, ArenaMap, Idx, RawIdx}; +use la_arena::{Arena, ArenaMap}; use rustc_hash::FxHashMap; use smallvec::SmallVec; use span::{Edition, MacroFileId, SyntaxContextData}; @@ -22,19 +23,18 @@ use tt::TextRange; use crate::{ db::DefDatabase, - expander::Expander, hir::{ - dummy_expr_id, Array, AsmOperand, Binding, BindingId, Expr, ExprId, ExprOrPatId, Label, - LabelId, Pat, PatId, RecordFieldPat, Statement, + Array, AsmOperand, Binding, BindingId, Expr, ExprId, ExprOrPatId, Label, LabelId, Pat, + PatId, RecordFieldPat, Statement, }, - item_tree::AttrOwner, nameres::DefMap, path::{ModPath, Path}, - src::HasSource, type_ref::{TypeRef, TypeRefId, TypesMap, TypesSourceMap}, - BlockId, DefWithBodyId, HasModule, Lookup, SyntheticSyntax, + BlockId, DefWithBodyId, Lookup, SyntheticSyntax, }; +pub use self::body::{Body, BodySourceMap}; + /// A wrapper around [`span::SyntaxContextId`] that is intended only for comparisons. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct HygieneId(span::SyntaxContextId); @@ -58,9 +58,29 @@ impl HygieneId { } } -/// The body of an item (function, const etc.). +pub type ExprPtr = AstPtr; +pub type ExprSource = InFile; + +pub type PatPtr = AstPtr; +pub type PatSource = InFile; + +pub type LabelPtr = AstPtr; +pub type LabelSource = InFile; + +pub type FieldPtr = AstPtr; +pub type FieldSource = InFile; + +pub type PatFieldPtr = AstPtr>; +pub type PatFieldSource = InFile; + +pub type ExprOrPatPtr = AstPtr>; +pub type ExprOrPatSource = InFile; + +pub type SelfParamPtr = AstPtr; +pub type MacroCallPtr = AstPtr; + #[derive(Debug, Eq, PartialEq)] -pub struct Body { +pub struct ExpressionStore { pub exprs: Arena, pub pats: Arena, pub bindings: Arena, @@ -68,19 +88,9 @@ pub struct Body { /// Id of the closure/coroutine that owns the corresponding binding. If a binding is owned by the /// top level expression, it will not be listed in here. pub binding_owners: FxHashMap, - /// The patterns for the function's parameters. While the parameter types are - /// part of the function signature, the patterns are not (they don't change - /// the external type of the function). - /// - /// If this `Body` is for the body of a constant, this will just be - /// empty. - pub params: Box<[PatId]>, - pub self_param: Option, - /// The `ExprId` of the actual body expression. - pub body_expr: ExprId, pub types: TypesMap, - /// Block expressions in this body that may contain inner items. - block_scopes: Vec, + /// Block expressions in this store that may contain inner items. + block_scopes: Box<[BlockId]>, /// A map from binding to its hygiene ID. /// @@ -92,44 +102,13 @@ pub struct Body { binding_hygiene: FxHashMap, /// A map from an variable usages to their hygiene ID. /// - /// Expressions that can be recorded here are single segment path, although not all single segments path refer + /// Expressions (and destructuing patterns) that can be recorded here are single segment path, although not all single segments path refer /// to variables and have hygiene (some refer to items, we don't know at this stage). - expr_hygiene: FxHashMap, - /// A map from a destructuring assignment possible variable usages to their hygiene ID. - pat_hygiene: FxHashMap, + ident_hygiene: FxHashMap, } -pub type ExprPtr = AstPtr; -pub type ExprSource = InFile; - -pub type PatPtr = AstPtr; -pub type PatSource = InFile; - -pub type LabelPtr = AstPtr; -pub type LabelSource = InFile; - -pub type FieldPtr = AstPtr; -pub type FieldSource = InFile; - -pub type PatFieldPtr = AstPtr>; -pub type PatFieldSource = InFile; - -pub type ExprOrPatPtr = AstPtr>; -pub type ExprOrPatSource = InFile; - -/// An item body together with the mapping from syntax nodes to HIR expression -/// IDs. This is needed to go from e.g. a position in a file to the HIR -/// expression containing it; but for type inference etc., we want to operate on -/// a structure that is agnostic to the actual positions of expressions in the -/// file, so that we don't recompute types whenever some whitespace is typed. -/// -/// One complication here is that, due to macro expansion, a single `Body` might -/// be spread across several files. So, for each ExprId and PatId, we record -/// both the HirFileId and the position inside the file. However, we only store -/// AST -> ExprId mapping for non-macro files, as it is not clear how to handle -/// this properly for macros. -#[derive(Default, Debug, Eq, PartialEq)] -pub struct BodySourceMap { +#[derive(Debug, Eq, PartialEq, Default)] +pub struct ExpressionStoreSourceMap { // AST expressions can create patterns in destructuring assignments. Therefore, `ExprSource` can also map // to `PatId`, and `PatId` can also map to `ExprSource` (the other way around is unaffected). expr_map: FxHashMap, @@ -141,7 +120,6 @@ pub struct BodySourceMap { label_map: FxHashMap, label_map_back: ArenaMap, - self_param: Option>>, binding_definitions: FxHashMap>, /// We don't create explicit nodes for record fields (`S { record_field: 92 }`). @@ -153,11 +131,25 @@ pub struct BodySourceMap { template_map: Option>, - expansions: FxHashMap>, MacroFileId>, + expansions: FxHashMap, MacroFileId>, - /// Diagnostics accumulated during body lowering. These contain `AstPtr`s and so are stored in + /// Diagnostics accumulated during lowering. These contain `AstPtr`s and so are stored in /// the source map (since they're just as volatile). - diagnostics: Vec, + diagnostics: Vec, +} + +/// The body of an item (function, const etc.). +#[derive(Debug, Eq, PartialEq, Default)] +pub struct ExpressionStoreBuilder { + pub exprs: Arena, + pub pats: Arena, + pub bindings: Arena, + pub labels: Arena