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<Box<GenericArgs>>,
103 expr: Option<ExprId>,
106 expr: Option<ExprId>,
109 path: Option<Box<Path>>,
110 fields: Vec<RecordLitField>,
111 spread: Option<ExprId>,
134 type_ref: Box<TypeRef>,
139 mutability: Mutability,
151 op: Option<BinaryOp>,
164 arg_types: Vec<Option<TypeRef>>,
165 ret_type: Option<Box<TypeRef>>,
181 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
186 Assignment { op: Option<ArithOp> },
189 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
195 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
197 Eq { negated: bool },
198 Ord { ordering: Ordering, strict: bool },
201 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
207 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
221 pub use syntax::ast::PrefixOp as UnaryOp;
222 #[derive(Debug, Clone, Eq, PartialEq)]
224 ElementList(Vec<ExprId>),
225 Repeat { initializer: ExprId, repeat: ExprId },
228 #[derive(Debug, Clone, Eq, PartialEq)]
229 pub struct MatchArm {
231 pub guard: Option<ExprId>,
235 #[derive(Debug, Clone, Eq, PartialEq)]
236 pub struct RecordLitField {
241 #[derive(Debug, Clone, Eq, PartialEq)]
243 Let { pat: PatId, type_ref: Option<TypeRef>, initializer: Option<ExprId> },
248 pub fn walk_child_exprs(&self, mut f: impl FnMut(ExprId)) {
252 Expr::If { condition, then_branch, else_branch } => {
255 if let Some(else_branch) = else_branch {
259 Expr::Block { statements, tail, .. } => {
260 for stmt in statements {
262 Statement::Let { initializer, .. } => {
263 if let Some(expr) = initializer {
267 Statement::Expr(e) => f(*e),
270 if let Some(expr) = tail {
274 Expr::TryBlock { body }
275 | Expr::Unsafe { body }
276 | Expr::Async { body }
277 | Expr::Const { body } => f(*body),
278 Expr::Loop { body, .. } => f(*body),
279 Expr::While { condition, body, .. } => {
283 Expr::For { iterable, body, .. } => {
287 Expr::Call { callee, args } => {
293 Expr::MethodCall { receiver, args, .. } => {
299 Expr::Match { expr, arms } => {
305 Expr::Continue { .. } => {}
306 Expr::Break { expr, .. } | Expr::Return { expr } | Expr::Yield { expr } => {
307 if let Some(expr) = expr {
311 Expr::RecordLit { fields, spread, .. } => {
312 for field in fields {
315 if let Some(expr) = spread {
319 Expr::Lambda { body, .. } => {
322 Expr::BinaryOp { lhs, rhs, .. } => {
326 Expr::Range { lhs, rhs, .. } => {
327 if let Some(lhs) = rhs {
330 if let Some(rhs) = lhs {
334 Expr::Index { base, index } => {
338 Expr::Field { expr, .. }
339 | Expr::Await { expr }
341 | Expr::Cast { expr, .. }
342 | Expr::Ref { expr, .. }
343 | Expr::UnaryOp { expr, .. }
344 | Expr::Box { expr } => {
347 Expr::Tuple { exprs } => {
352 Expr::Array(a) => match a {
353 Array::ElementList(exprs) => {
358 Array::Repeat { initializer, repeat } => {
363 Expr::MacroStmts { tail } => f(*tail),
364 Expr::Literal(_) => {}
369 /// Explicit binding annotations given in the HIR for a binding. Note
370 /// that this is not the final binding *mode* that we infer after type
372 #[derive(Clone, PartialEq, Eq, Debug, Copy)]
373 pub enum BindingAnnotation {
374 /// No binding annotation given: this means that the final binding mode
375 /// will depend on whether we have skipped through a `&` reference
376 /// when matching. For example, the `x` in `Some(x)` will have binding
377 /// mode `None`; if you do `let Some(x) = &Some(22)`, it will
378 /// ultimately be inferred to be by-reference.
381 /// Annotated with `mut x` -- could be either ref or not, similar to `None`.
384 /// Annotated as `ref`, like `ref x`
387 /// Annotated as `ref mut x`.
391 impl BindingAnnotation {
392 pub fn new(is_mutable: bool, is_ref: bool) -> Self {
393 match (is_mutable, is_ref) {
394 (true, true) => BindingAnnotation::RefMut,
395 (false, true) => BindingAnnotation::Ref,
396 (true, false) => BindingAnnotation::Mutable,
397 (false, false) => BindingAnnotation::Unannotated,
402 #[derive(Debug, Clone, Eq, PartialEq)]
403 pub struct RecordFieldPat {
408 /// Close relative to rustc's hir::PatKind
409 #[derive(Debug, Clone, Eq, PartialEq)]
413 Tuple { args: Vec<PatId>, ellipsis: Option<usize> },
415 Record { path: Option<Box<Path>>, args: Vec<RecordFieldPat>, ellipsis: bool },
416 Range { start: ExprId, end: ExprId },
417 Slice { prefix: Vec<PatId>, slice: Option<PatId>, suffix: Vec<PatId> },
420 Bind { mode: BindingAnnotation, name: Name, subpat: Option<PatId> },
421 TupleStruct { path: Option<Box<Path>>, args: Vec<PatId>, ellipsis: Option<usize> },
422 Ref { pat: PatId, mutability: Mutability },
423 Box { inner: PatId },
428 pub fn walk_child_pats(&self, mut f: impl FnMut(PatId)) {
433 | Pat::ConstBlock(..)
436 Pat::Bind { subpat, .. } => {
437 subpat.iter().copied().for_each(f);
439 Pat::Or(args) | Pat::Tuple { args, .. } | Pat::TupleStruct { args, .. } => {
440 args.iter().copied().for_each(f);
442 Pat::Ref { pat, .. } => f(*pat),
443 Pat::Slice { prefix, slice, suffix } => {
444 let total_iter = prefix.iter().chain(slice.iter()).chain(suffix.iter());
445 total_iter.copied().for_each(f);
447 Pat::Record { args, .. } => {
448 args.iter().map(|f| f.pat).for_each(f);
450 Pat::Box { inner } => f(*inner),