1 //! This module describes hir-level representation of expressions.
3 //! This representation is:
5 //! 1. Identity-based. Each expression has an `id`, so we can distinguish
6 //! between different `1` in `1 + 1`.
7 //! 2. Independent of syntax. Though syntactic provenance information can be
8 //! attached separately via id-based side map.
9 //! 3. Unresolved. Paths are stored as sequences of names, and not as defs the
11 //! 4. Desugared. There's no `if let`.
13 //! See also a neighboring `body` module.
15 use hir_expand::name::Name;
16 use la_arena::{Idx, RawIdx};
17 use syntax::ast::RangeOp;
20 builtin_type::{BuiltinFloat, BuiltinInt},
21 path::{GenericArgs, Path},
22 type_ref::{Mutability, Rawness, TypeRef},
25 pub type ExprId = Idx<Expr>;
26 pub(crate) fn dummy_expr_id() -> ExprId {
27 ExprId::from_raw(RawIdx::from(!0))
30 pub type PatId = Idx<Pat>;
32 #[derive(Debug, Clone, Eq, PartialEq)]
36 pub type LabelId = Idx<Label>;
38 #[derive(Debug, Clone, Eq, PartialEq)]
44 Int(u64, Option<BuiltinInt>),
45 Float(u64, Option<BuiltinFloat>), // FIXME: f64 is not Eq
48 #[derive(Debug, Clone, Eq, PartialEq)]
50 /// This is produced if the syntax tree does not have a required expression piece.
56 else_branch: Option<ExprId>,
59 statements: Vec<Statement>,
61 label: Option<LabelId>,
65 label: Option<LabelId>,
70 label: Option<LabelId>,
76 label: Option<LabelId>,
86 generic_args: Option<GenericArgs>,
100 expr: Option<ExprId>,
103 expr: Option<ExprId>,
107 fields: Vec<RecordLitField>,
108 spread: Option<ExprId>,
136 mutability: Mutability,
148 op: Option<BinaryOp>,
161 arg_types: Vec<Option<TypeRef>>,
162 ret_type: Option<TypeRef>,
175 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
180 Assignment { op: Option<ArithOp> },
183 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
189 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
191 Eq { negated: bool },
192 Ord { ordering: Ordering, strict: bool },
195 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
201 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
215 pub use syntax::ast::PrefixOp as UnaryOp;
216 #[derive(Debug, Clone, Eq, PartialEq)]
218 ElementList(Vec<ExprId>),
219 Repeat { initializer: ExprId, repeat: ExprId },
222 #[derive(Debug, Clone, Eq, PartialEq)]
223 pub struct MatchArm {
225 pub guard: Option<ExprId>,
229 #[derive(Debug, Clone, Eq, PartialEq)]
230 pub struct RecordLitField {
235 #[derive(Debug, Clone, Eq, PartialEq)]
237 Let { pat: PatId, type_ref: Option<TypeRef>, initializer: Option<ExprId> },
242 pub fn walk_child_exprs(&self, mut f: impl FnMut(ExprId)) {
246 Expr::If { condition, then_branch, else_branch } => {
249 if let Some(else_branch) = else_branch {
253 Expr::Block { statements, tail, .. } => {
254 for stmt in statements {
256 Statement::Let { initializer, .. } => {
257 if let Some(expr) = initializer {
261 Statement::Expr(e) => f(*e),
264 if let Some(expr) = tail {
268 Expr::TryBlock { body }
269 | Expr::Unsafe { body }
270 | Expr::Async { body }
271 | Expr::Const { body } => f(*body),
272 Expr::Loop { body, .. } => f(*body),
273 Expr::While { condition, body, .. } => {
277 Expr::For { iterable, body, .. } => {
281 Expr::Call { callee, args } => {
287 Expr::MethodCall { receiver, args, .. } => {
293 Expr::Match { expr, arms } => {
299 Expr::Continue { .. } => {}
300 Expr::Break { expr, .. } | Expr::Return { expr } | Expr::Yield { expr } => {
301 if let Some(expr) = expr {
305 Expr::RecordLit { fields, spread, .. } => {
306 for field in fields {
309 if let Some(expr) = spread {
313 Expr::Lambda { body, .. } => {
316 Expr::BinaryOp { lhs, rhs, .. } => {
320 Expr::Range { lhs, rhs, .. } => {
321 if let Some(lhs) = rhs {
324 if let Some(rhs) = lhs {
328 Expr::Index { base, index } => {
332 Expr::Field { expr, .. }
333 | Expr::Await { expr }
335 | Expr::Cast { expr, .. }
336 | Expr::Ref { expr, .. }
337 | Expr::UnaryOp { expr, .. }
338 | Expr::Box { expr } => {
341 Expr::Tuple { exprs } => {
346 Expr::Array(a) => match a {
347 Array::ElementList(exprs) => {
352 Array::Repeat { initializer, repeat } => {
357 Expr::Literal(_) => {}
362 /// Explicit binding annotations given in the HIR for a binding. Note
363 /// that this is not the final binding *mode* that we infer after type
365 #[derive(Clone, PartialEq, Eq, Debug, Copy)]
366 pub enum BindingAnnotation {
367 /// No binding annotation given: this means that the final binding mode
368 /// will depend on whether we have skipped through a `&` reference
369 /// when matching. For example, the `x` in `Some(x)` will have binding
370 /// mode `None`; if you do `let Some(x) = &Some(22)`, it will
371 /// ultimately be inferred to be by-reference.
374 /// Annotated with `mut x` -- could be either ref or not, similar to `None`.
377 /// Annotated as `ref`, like `ref x`
380 /// Annotated as `ref mut x`.
384 impl BindingAnnotation {
385 pub fn new(is_mutable: bool, is_ref: bool) -> Self {
386 match (is_mutable, is_ref) {
387 (true, true) => BindingAnnotation::RefMut,
388 (false, true) => BindingAnnotation::Ref,
389 (true, false) => BindingAnnotation::Mutable,
390 (false, false) => BindingAnnotation::Unannotated,
395 #[derive(Debug, Clone, Eq, PartialEq)]
396 pub struct RecordFieldPat {
401 /// Close relative to rustc's hir::PatKind
402 #[derive(Debug, Clone, Eq, PartialEq)]
406 Tuple { args: Vec<PatId>, ellipsis: Option<usize> },
408 Record { path: Option<Path>, args: Vec<RecordFieldPat>, ellipsis: bool },
409 Range { start: ExprId, end: ExprId },
410 Slice { prefix: Vec<PatId>, slice: Option<PatId>, suffix: Vec<PatId> },
413 Bind { mode: BindingAnnotation, name: Name, subpat: Option<PatId> },
414 TupleStruct { path: Option<Path>, args: Vec<PatId>, ellipsis: Option<usize> },
415 Ref { pat: PatId, mutability: Mutability },
416 Box { inner: PatId },
421 pub fn walk_child_pats(&self, mut f: impl FnMut(PatId)) {
426 | Pat::ConstBlock(..)
429 Pat::Bind { subpat, .. } => {
430 subpat.iter().copied().for_each(f);
432 Pat::Or(args) | Pat::Tuple { args, .. } | Pat::TupleStruct { args, .. } => {
433 args.iter().copied().for_each(f);
435 Pat::Ref { pat, .. } => f(*pat),
436 Pat::Slice { prefix, slice, suffix } => {
437 let total_iter = prefix.iter().chain(slice.iter()).chain(suffix.iter());
438 total_iter.copied().for_each(f);
440 Pat::Record { args, .. } => {
441 args.iter().map(|f| f.pat).for_each(f);
443 Pat::Box { inner } => f(*inner),