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},
22 path::{GenericArgs, Path},
23 type_ref::{Mutability, Rawness, TypeRef},
27 pub type ExprId = Idx<Expr>;
28 pub(crate) fn dummy_expr_id() -> ExprId {
29 ExprId::from_raw(RawIdx::from(!0))
32 pub type PatId = Idx<Pat>;
34 #[derive(Debug, Clone, Eq, PartialEq)]
38 pub type LabelId = Idx<Label>;
40 #[derive(Debug, Clone, Eq, PartialEq)]
46 Int(i128, Option<BuiltinInt>),
47 Uint(u128, Option<BuiltinUint>),
48 Float(u64, Option<BuiltinFloat>), // FIXME: f64 is not Eq
51 #[derive(Debug, Clone, Eq, PartialEq)]
53 /// This is produced if the syntax tree does not have a required expression piece.
59 else_branch: Option<ExprId>,
63 statements: Vec<Statement>,
65 label: Option<LabelId>,
69 label: Option<LabelId>,
74 label: Option<LabelId>,
80 label: Option<LabelId>,
90 generic_args: Option<Box<GenericArgs>>,
100 expr: Option<ExprId>,
104 expr: Option<ExprId>,
107 expr: Option<ExprId>,
110 path: Option<Box<Path>>,
111 fields: Vec<RecordLitField>,
112 spread: Option<ExprId>,
135 type_ref: Interned<TypeRef>,
140 mutability: Mutability,
152 op: Option<BinaryOp>,
165 arg_types: Vec<Option<Interned<TypeRef>>>,
166 ret_type: Option<Interned<TypeRef>>,
182 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
187 Assignment { op: Option<ArithOp> },
190 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
196 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
198 Eq { negated: bool },
199 Ord { ordering: Ordering, strict: bool },
202 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
208 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
222 pub use syntax::ast::PrefixOp as UnaryOp;
223 #[derive(Debug, Clone, Eq, PartialEq)]
225 ElementList(Vec<ExprId>),
226 Repeat { initializer: ExprId, repeat: ExprId },
229 #[derive(Debug, Clone, Eq, PartialEq)]
230 pub struct MatchArm {
232 pub guard: Option<ExprId>,
236 #[derive(Debug, Clone, Eq, PartialEq)]
237 pub struct RecordLitField {
242 #[derive(Debug, Clone, Eq, PartialEq)]
244 Let { pat: PatId, type_ref: Option<Interned<TypeRef>>, initializer: Option<ExprId> },
245 Expr { expr: ExprId, has_semi: bool },
249 pub fn walk_child_exprs(&self, mut f: impl FnMut(ExprId)) {
253 Expr::If { condition, then_branch, else_branch } => {
256 if let Some(else_branch) = else_branch {
260 Expr::Block { statements, tail, .. } => {
261 for stmt in statements {
263 Statement::Let { initializer, .. } => {
264 if let Some(expr) = initializer {
268 Statement::Expr { expr: expression, .. } => f(*expression),
271 if let Some(expr) = tail {
275 Expr::TryBlock { body }
276 | Expr::Unsafe { body }
277 | Expr::Async { body }
278 | Expr::Const { body } => f(*body),
279 Expr::Loop { body, .. } => f(*body),
280 Expr::While { condition, body, .. } => {
284 Expr::For { iterable, body, .. } => {
288 Expr::Call { callee, args } => {
294 Expr::MethodCall { receiver, args, .. } => {
300 Expr::Match { expr, arms } => {
306 Expr::Continue { .. } => {}
307 Expr::Break { expr, .. } | Expr::Return { expr } | Expr::Yield { expr } => {
308 if let Some(expr) = expr {
312 Expr::RecordLit { fields, spread, .. } => {
313 for field in fields {
316 if let Some(expr) = spread {
320 Expr::Lambda { body, .. } => {
323 Expr::BinaryOp { lhs, rhs, .. } => {
327 Expr::Range { lhs, rhs, .. } => {
328 if let Some(lhs) = rhs {
331 if let Some(rhs) = lhs {
335 Expr::Index { base, index } => {
339 Expr::Field { expr, .. }
340 | Expr::Await { expr }
342 | Expr::Cast { expr, .. }
343 | Expr::Ref { expr, .. }
344 | Expr::UnaryOp { expr, .. }
345 | Expr::Box { expr } => {
348 Expr::Tuple { exprs } => {
353 Expr::Array(a) => match a {
354 Array::ElementList(exprs) => {
359 Array::Repeat { initializer, repeat } => {
364 Expr::MacroStmts { tail } => f(*tail),
365 Expr::Literal(_) => {}
370 /// Explicit binding annotations given in the HIR for a binding. Note
371 /// that this is not the final binding *mode* that we infer after type
373 #[derive(Clone, PartialEq, Eq, Debug, Copy)]
374 pub enum BindingAnnotation {
375 /// No binding annotation given: this means that the final binding mode
376 /// will depend on whether we have skipped through a `&` reference
377 /// when matching. For example, the `x` in `Some(x)` will have binding
378 /// mode `None`; if you do `let Some(x) = &Some(22)`, it will
379 /// ultimately be inferred to be by-reference.
382 /// Annotated with `mut x` -- could be either ref or not, similar to `None`.
385 /// Annotated as `ref`, like `ref x`
388 /// Annotated as `ref mut x`.
392 impl BindingAnnotation {
393 pub fn new(is_mutable: bool, is_ref: bool) -> Self {
394 match (is_mutable, is_ref) {
395 (true, true) => BindingAnnotation::RefMut,
396 (false, true) => BindingAnnotation::Ref,
397 (true, false) => BindingAnnotation::Mutable,
398 (false, false) => BindingAnnotation::Unannotated,
403 #[derive(Debug, Clone, Eq, PartialEq)]
404 pub struct RecordFieldPat {
409 /// Close relative to rustc's hir::PatKind
410 #[derive(Debug, Clone, Eq, PartialEq)]
414 Tuple { args: Vec<PatId>, ellipsis: Option<usize> },
416 Record { path: Option<Box<Path>>, args: Vec<RecordFieldPat>, ellipsis: bool },
417 Range { start: ExprId, end: ExprId },
418 Slice { prefix: Vec<PatId>, slice: Option<PatId>, suffix: Vec<PatId> },
421 Bind { mode: BindingAnnotation, name: Name, subpat: Option<PatId> },
422 TupleStruct { path: Option<Box<Path>>, args: Vec<PatId>, ellipsis: Option<usize> },
423 Ref { pat: PatId, mutability: Mutability },
424 Box { inner: PatId },
429 pub fn walk_child_pats(&self, mut f: impl FnMut(PatId)) {
434 | Pat::ConstBlock(..)
437 Pat::Bind { subpat, .. } => {
438 subpat.iter().copied().for_each(f);
440 Pat::Or(args) | Pat::Tuple { args, .. } | Pat::TupleStruct { args, .. } => {
441 args.iter().copied().for_each(f);
443 Pat::Ref { pat, .. } => f(*pat),
444 Pat::Slice { prefix, slice, suffix } => {
445 let total_iter = prefix.iter().chain(slice.iter()).chain(suffix.iter());
446 total_iter.copied().for_each(f);
448 Pat::Record { args, .. } => {
449 args.iter().map(|f| f.pat).for_each(f);
451 Pat::Box { inner } => f(*inner),