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};
19 builtin_type::{BuiltinFloat, BuiltinInt, BuiltinUint},
21 path::{GenericArgs, Path},
22 type_ref::{Mutability, Rawness, TypeRef},
26 pub use syntax::ast::{ArithOp, BinaryOp, CmpOp, LogicOp, Ordering, RangeOp, UnaryOp};
28 pub type ExprId = Idx<Expr>;
29 pub(crate) fn dummy_expr_id() -> ExprId {
30 ExprId::from_raw(RawIdx::from(!0))
33 pub type PatId = Idx<Pat>;
35 #[derive(Debug, Clone, Eq, PartialEq)]
39 pub type LabelId = Idx<Label>;
41 #[derive(Debug, Clone, Eq, PartialEq)]
47 Int(i128, Option<BuiltinInt>),
48 Uint(u128, Option<BuiltinUint>),
49 Float(u64, Option<BuiltinFloat>), // FIXME: f64 is not Eq
52 #[derive(Debug, Clone, Eq, PartialEq)]
54 /// This is produced if the syntax tree does not have a required expression piece.
60 else_branch: Option<ExprId>,
64 statements: Vec<Statement>,
66 label: Option<LabelId>,
70 label: Option<LabelId>,
75 label: Option<LabelId>,
81 label: Option<LabelId>,
91 generic_args: Option<Box<GenericArgs>>,
101 expr: Option<ExprId>,
105 expr: Option<ExprId>,
108 expr: Option<ExprId>,
111 path: Option<Box<Path>>,
112 fields: Vec<RecordLitField>,
113 spread: Option<ExprId>,
136 type_ref: Interned<TypeRef>,
141 mutability: Mutability,
153 op: Option<BinaryOp>,
166 arg_types: Vec<Option<Interned<TypeRef>>>,
167 ret_type: Option<Interned<TypeRef>>,
183 #[derive(Debug, Clone, Eq, PartialEq)]
185 ElementList(Vec<ExprId>),
186 Repeat { initializer: ExprId, repeat: ExprId },
189 #[derive(Debug, Clone, Eq, PartialEq)]
190 pub struct MatchArm {
192 pub guard: Option<MatchGuard>,
196 #[derive(Debug, Clone, Eq, PartialEq)]
197 pub enum MatchGuard {
200 IfLet { pat: PatId, expr: ExprId },
203 #[derive(Debug, Clone, Eq, PartialEq)]
204 pub struct RecordLitField {
209 #[derive(Debug, Clone, Eq, PartialEq)]
213 type_ref: Option<Interned<TypeRef>>,
214 initializer: Option<ExprId>,
215 else_branch: Option<ExprId>,
224 pub fn walk_child_exprs(&self, mut f: impl FnMut(ExprId)) {
228 Expr::If { condition, then_branch, else_branch } => {
231 if let Some(else_branch) = else_branch {
235 Expr::Block { statements, tail, .. } => {
236 for stmt in statements {
238 Statement::Let { initializer, .. } => {
239 if let Some(expr) = initializer {
243 Statement::Expr { expr: expression, .. } => f(*expression),
246 if let Some(expr) = tail {
250 Expr::TryBlock { body }
251 | Expr::Unsafe { body }
252 | Expr::Async { body }
253 | Expr::Const { body } => f(*body),
254 Expr::Loop { body, .. } => f(*body),
255 Expr::While { condition, body, .. } => {
259 Expr::For { iterable, body, .. } => {
263 Expr::Call { callee, args } => {
269 Expr::MethodCall { receiver, args, .. } => {
275 Expr::Match { expr, arms } => {
281 Expr::Continue { .. } => {}
282 Expr::Break { expr, .. } | Expr::Return { expr } | Expr::Yield { expr } => {
283 if let Some(expr) = expr {
287 Expr::RecordLit { fields, spread, .. } => {
288 for field in fields {
291 if let Some(expr) = spread {
295 Expr::Lambda { body, .. } => {
298 Expr::BinaryOp { lhs, rhs, .. } => {
302 Expr::Range { lhs, rhs, .. } => {
303 if let Some(lhs) = rhs {
306 if let Some(rhs) = lhs {
310 Expr::Index { base, index } => {
314 Expr::Field { expr, .. }
315 | Expr::Await { expr }
317 | Expr::Cast { expr, .. }
318 | Expr::Ref { expr, .. }
319 | Expr::UnaryOp { expr, .. }
320 | Expr::Box { expr } => {
323 Expr::Tuple { exprs } => {
328 Expr::Array(a) => match a {
329 Array::ElementList(exprs) => {
334 Array::Repeat { initializer, repeat } => {
339 Expr::MacroStmts { tail } => f(*tail),
340 Expr::Literal(_) => {}
345 /// Explicit binding annotations given in the HIR for a binding. Note
346 /// that this is not the final binding *mode* that we infer after type
348 #[derive(Clone, PartialEq, Eq, Debug, Copy)]
349 pub enum BindingAnnotation {
350 /// No binding annotation given: this means that the final binding mode
351 /// will depend on whether we have skipped through a `&` reference
352 /// when matching. For example, the `x` in `Some(x)` will have binding
353 /// mode `None`; if you do `let Some(x) = &Some(22)`, it will
354 /// ultimately be inferred to be by-reference.
357 /// Annotated with `mut x` -- could be either ref or not, similar to `None`.
360 /// Annotated as `ref`, like `ref x`
363 /// Annotated as `ref mut x`.
367 impl BindingAnnotation {
368 pub fn new(is_mutable: bool, is_ref: bool) -> Self {
369 match (is_mutable, is_ref) {
370 (true, true) => BindingAnnotation::RefMut,
371 (false, true) => BindingAnnotation::Ref,
372 (true, false) => BindingAnnotation::Mutable,
373 (false, false) => BindingAnnotation::Unannotated,
378 #[derive(Debug, Clone, Eq, PartialEq)]
379 pub struct RecordFieldPat {
384 /// Close relative to rustc's hir::PatKind
385 #[derive(Debug, Clone, Eq, PartialEq)]
389 Tuple { args: Vec<PatId>, ellipsis: Option<usize> },
391 Record { path: Option<Box<Path>>, args: Vec<RecordFieldPat>, ellipsis: bool },
392 Range { start: ExprId, end: ExprId },
393 Slice { prefix: Vec<PatId>, slice: Option<PatId>, suffix: Vec<PatId> },
396 Bind { mode: BindingAnnotation, name: Name, subpat: Option<PatId> },
397 TupleStruct { path: Option<Box<Path>>, args: Vec<PatId>, ellipsis: Option<usize> },
398 Ref { pat: PatId, mutability: Mutability },
399 Box { inner: PatId },
404 pub fn walk_child_pats(&self, mut f: impl FnMut(PatId)) {
409 | Pat::ConstBlock(..)
412 Pat::Bind { subpat, .. } => {
413 subpat.iter().copied().for_each(f);
415 Pat::Or(args) | Pat::Tuple { args, .. } | Pat::TupleStruct { args, .. } => {
416 args.iter().copied().for_each(f);
418 Pat::Ref { pat, .. } => f(*pat),
419 Pat::Slice { prefix, slice, suffix } => {
420 let total_iter = prefix.iter().chain(slice.iter()).chain(suffix.iter());
421 total_iter.copied().for_each(f);
423 Pat::Record { args, .. } => {
424 args.iter().map(|f| f.pat).for_each(f);
426 Pat::Box { inner } => f(*inner),