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, BuiltinUint},
21 path::{GenericArgs, Path},
22 type_ref::{Mutability, Rawness, TypeRef},
26 pub type ExprId = Idx<Expr>;
27 pub(crate) fn dummy_expr_id() -> ExprId {
28 ExprId::from_raw(RawIdx::from(!0))
31 pub type PatId = Idx<Pat>;
33 #[derive(Debug, Clone, Eq, PartialEq)]
37 pub type LabelId = Idx<Label>;
39 #[derive(Debug, Clone, Eq, PartialEq)]
45 Int(u64, Option<BuiltinInt>),
46 Uint(u64, Option<BuiltinUint>),
47 Float(u64, Option<BuiltinFloat>), // FIXME: f64 is not Eq
50 #[derive(Debug, Clone, Eq, PartialEq)]
52 /// This is produced if the syntax tree does not have a required expression piece.
58 else_branch: Option<ExprId>,
62 statements: Vec<Statement>,
64 label: Option<LabelId>,
68 label: Option<LabelId>,
73 label: Option<LabelId>,
79 label: Option<LabelId>,
89 generic_args: Option<GenericArgs>,
103 expr: Option<ExprId>,
106 expr: Option<ExprId>,
110 fields: Vec<RecordLitField>,
111 spread: Option<ExprId>,
139 mutability: Mutability,
151 op: Option<BinaryOp>,
164 arg_types: Vec<Option<TypeRef>>,
165 ret_type: Option<TypeRef>,
178 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
183 Assignment { op: Option<ArithOp> },
186 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
192 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
194 Eq { negated: bool },
195 Ord { ordering: Ordering, strict: bool },
198 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
204 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
218 pub use syntax::ast::PrefixOp as UnaryOp;
219 #[derive(Debug, Clone, Eq, PartialEq)]
221 ElementList(Vec<ExprId>),
222 Repeat { initializer: ExprId, repeat: ExprId },
225 #[derive(Debug, Clone, Eq, PartialEq)]
226 pub struct MatchArm {
228 pub guard: Option<ExprId>,
232 #[derive(Debug, Clone, Eq, PartialEq)]
233 pub struct RecordLitField {
238 #[derive(Debug, Clone, Eq, PartialEq)]
240 Let { pat: PatId, type_ref: Option<TypeRef>, initializer: Option<ExprId> },
245 pub fn walk_child_exprs(&self, mut f: impl FnMut(ExprId)) {
249 Expr::If { condition, then_branch, else_branch } => {
252 if let Some(else_branch) = else_branch {
256 Expr::Block { statements, tail, .. } => {
257 for stmt in statements {
259 Statement::Let { initializer, .. } => {
260 if let Some(expr) = initializer {
264 Statement::Expr(e) => f(*e),
267 if let Some(expr) = tail {
271 Expr::TryBlock { body }
272 | Expr::Unsafe { body }
273 | Expr::Async { body }
274 | Expr::Const { body } => f(*body),
275 Expr::Loop { body, .. } => f(*body),
276 Expr::While { condition, body, .. } => {
280 Expr::For { iterable, body, .. } => {
284 Expr::Call { callee, args } => {
290 Expr::MethodCall { receiver, args, .. } => {
296 Expr::Match { expr, arms } => {
302 Expr::Continue { .. } => {}
303 Expr::Break { expr, .. } | Expr::Return { expr } | Expr::Yield { expr } => {
304 if let Some(expr) = expr {
308 Expr::RecordLit { fields, spread, .. } => {
309 for field in fields {
312 if let Some(expr) = spread {
316 Expr::Lambda { body, .. } => {
319 Expr::BinaryOp { lhs, rhs, .. } => {
323 Expr::Range { lhs, rhs, .. } => {
324 if let Some(lhs) = rhs {
327 if let Some(rhs) = lhs {
331 Expr::Index { base, index } => {
335 Expr::Field { expr, .. }
336 | Expr::Await { expr }
338 | Expr::Cast { expr, .. }
339 | Expr::Ref { expr, .. }
340 | Expr::UnaryOp { expr, .. }
341 | Expr::Box { expr } => {
344 Expr::Tuple { exprs } => {
349 Expr::Array(a) => match a {
350 Array::ElementList(exprs) => {
355 Array::Repeat { initializer, repeat } => {
360 Expr::Literal(_) => {}
365 /// Explicit binding annotations given in the HIR for a binding. Note
366 /// that this is not the final binding *mode* that we infer after type
368 #[derive(Clone, PartialEq, Eq, Debug, Copy)]
369 pub enum BindingAnnotation {
370 /// No binding annotation given: this means that the final binding mode
371 /// will depend on whether we have skipped through a `&` reference
372 /// when matching. For example, the `x` in `Some(x)` will have binding
373 /// mode `None`; if you do `let Some(x) = &Some(22)`, it will
374 /// ultimately be inferred to be by-reference.
377 /// Annotated with `mut x` -- could be either ref or not, similar to `None`.
380 /// Annotated as `ref`, like `ref x`
383 /// Annotated as `ref mut x`.
387 impl BindingAnnotation {
388 pub fn new(is_mutable: bool, is_ref: bool) -> Self {
389 match (is_mutable, is_ref) {
390 (true, true) => BindingAnnotation::RefMut,
391 (false, true) => BindingAnnotation::Ref,
392 (true, false) => BindingAnnotation::Mutable,
393 (false, false) => BindingAnnotation::Unannotated,
398 #[derive(Debug, Clone, Eq, PartialEq)]
399 pub struct RecordFieldPat {
404 /// Close relative to rustc's hir::PatKind
405 #[derive(Debug, Clone, Eq, PartialEq)]
409 Tuple { args: Vec<PatId>, ellipsis: Option<usize> },
411 Record { path: Option<Path>, args: Vec<RecordFieldPat>, ellipsis: bool },
412 Range { start: ExprId, end: ExprId },
413 Slice { prefix: Vec<PatId>, slice: Option<PatId>, suffix: Vec<PatId> },
416 Bind { mode: BindingAnnotation, name: Name, subpat: Option<PatId> },
417 TupleStruct { path: Option<Path>, args: Vec<PatId>, ellipsis: Option<usize> },
418 Ref { pat: PatId, mutability: Mutability },
419 Box { inner: PatId },
424 pub fn walk_child_pats(&self, mut f: impl FnMut(PatId)) {
429 | Pat::ConstBlock(..)
432 Pat::Bind { subpat, .. } => {
433 subpat.iter().copied().for_each(f);
435 Pat::Or(args) | Pat::Tuple { args, .. } | Pat::TupleStruct { args, .. } => {
436 args.iter().copied().for_each(f);
438 Pat::Ref { pat, .. } => f(*pat),
439 Pat::Slice { prefix, slice, suffix } => {
440 let total_iter = prefix.iter().chain(slice.iter()).chain(suffix.iter());
441 total_iter.copied().for_each(f);
443 Pat::Record { args, .. } => {
444 args.iter().map(|f| f.pat).for_each(f);
446 Pat::Box { inner } => f(*inner),