1 //! This module describes hir-level representation of expressions.
3 //! This representaion 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 arena::{Idx, RawId};
16 use hir_expand::name::Name;
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(RawId::from(!0))
30 pub type PatId = Idx<Pat>;
32 #[derive(Debug, Clone, Eq, PartialEq)]
38 Int(u64, Option<BuiltinInt>),
39 Float(u64, Option<BuiltinFloat>), // FIXME: f64 is not Eq
42 #[derive(Debug, Clone, Eq, PartialEq)]
44 /// This is produced if the syntax tree does not have a required expression piece.
50 else_branch: Option<ExprId>,
53 statements: Vec<Statement>,
80 generic_args: Option<GenericArgs>,
98 fields: Vec<RecordLitField>,
99 spread: Option<ExprId>,
124 mutability: Mutability,
136 op: Option<BinaryOp>,
149 arg_types: Vec<Option<TypeRef>>,
150 ret_type: Option<TypeRef>,
163 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
168 Assignment { op: Option<ArithOp> },
171 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
177 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
179 Eq { negated: bool },
180 Ord { ordering: Ordering, strict: bool },
183 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
189 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
203 pub use syntax::ast::PrefixOp as UnaryOp;
204 #[derive(Debug, Clone, Eq, PartialEq)]
206 ElementList(Vec<ExprId>),
207 Repeat { initializer: ExprId, repeat: ExprId },
210 #[derive(Debug, Clone, Eq, PartialEq)]
211 pub struct MatchArm {
213 pub guard: Option<ExprId>,
217 #[derive(Debug, Clone, Eq, PartialEq)]
218 pub struct RecordLitField {
223 #[derive(Debug, Clone, Eq, PartialEq)]
225 Let { pat: PatId, type_ref: Option<TypeRef>, initializer: Option<ExprId> },
230 pub fn walk_child_exprs(&self, mut f: impl FnMut(ExprId)) {
234 Expr::If { condition, then_branch, else_branch } => {
237 if let Some(else_branch) = else_branch {
241 Expr::Block { statements, tail, .. } => {
242 for stmt in statements {
244 Statement::Let { initializer, .. } => {
245 if let Some(expr) = initializer {
249 Statement::Expr(e) => f(*e),
252 if let Some(expr) = tail {
256 Expr::TryBlock { body } | Expr::Unsafe { body } | Expr::Async { body } => f(*body),
257 Expr::Loop { body, .. } => f(*body),
258 Expr::While { condition, body, .. } => {
262 Expr::For { iterable, body, .. } => {
266 Expr::Call { callee, args } => {
272 Expr::MethodCall { receiver, args, .. } => {
278 Expr::Match { expr, arms } => {
284 Expr::Continue { .. } => {}
285 Expr::Break { expr, .. } | Expr::Return { expr } => {
286 if let Some(expr) = expr {
290 Expr::RecordLit { fields, spread, .. } => {
291 for field in fields {
294 if let Some(expr) = spread {
298 Expr::Lambda { body, .. } => {
301 Expr::BinaryOp { lhs, rhs, .. } => {
305 Expr::Range { lhs, rhs, .. } => {
306 if let Some(lhs) = rhs {
309 if let Some(rhs) = lhs {
313 Expr::Index { base, index } => {
317 Expr::Field { expr, .. }
318 | Expr::Await { expr }
320 | Expr::Cast { expr, .. }
321 | Expr::Ref { expr, .. }
322 | Expr::UnaryOp { expr, .. }
323 | Expr::Box { expr } => {
326 Expr::Tuple { exprs } => {
331 Expr::Array(a) => match a {
332 Array::ElementList(exprs) => {
337 Array::Repeat { initializer, repeat } => {
342 Expr::Literal(_) => {}
347 /// Explicit binding annotations given in the HIR for a binding. Note
348 /// that this is not the final binding *mode* that we infer after type
350 #[derive(Clone, PartialEq, Eq, Debug, Copy)]
351 pub enum BindingAnnotation {
352 /// No binding annotation given: this means that the final binding mode
353 /// will depend on whether we have skipped through a `&` reference
354 /// when matching. For example, the `x` in `Some(x)` will have binding
355 /// mode `None`; if you do `let Some(x) = &Some(22)`, it will
356 /// ultimately be inferred to be by-reference.
359 /// Annotated with `mut x` -- could be either ref or not, similar to `None`.
362 /// Annotated as `ref`, like `ref x`
365 /// Annotated as `ref mut x`.
369 impl BindingAnnotation {
370 pub fn new(is_mutable: bool, is_ref: bool) -> Self {
371 match (is_mutable, is_ref) {
372 (true, true) => BindingAnnotation::RefMut,
373 (false, true) => BindingAnnotation::Ref,
374 (true, false) => BindingAnnotation::Mutable,
375 (false, false) => BindingAnnotation::Unannotated,
380 #[derive(Debug, Clone, Eq, PartialEq)]
381 pub struct RecordFieldPat {
386 /// Close relative to rustc's hir::PatKind
387 #[derive(Debug, Clone, Eq, PartialEq)]
391 Tuple { args: Vec<PatId>, ellipsis: Option<usize> },
393 Record { path: Option<Path>, args: Vec<RecordFieldPat>, ellipsis: bool },
394 Range { start: ExprId, end: ExprId },
395 Slice { prefix: Vec<PatId>, slice: Option<PatId>, suffix: Vec<PatId> },
398 Bind { mode: BindingAnnotation, name: Name, subpat: Option<PatId> },
399 TupleStruct { path: Option<Path>, args: Vec<PatId>, ellipsis: Option<usize> },
400 Ref { pat: PatId, mutability: Mutability },
401 Box { inner: PatId },
405 pub fn walk_child_pats(&self, mut f: impl FnMut(PatId)) {
407 Pat::Range { .. } | Pat::Lit(..) | Pat::Path(..) | Pat::Wild | Pat::Missing => {}
408 Pat::Bind { subpat, .. } => {
409 subpat.iter().copied().for_each(f);
411 Pat::Or(args) | Pat::Tuple { args, .. } | Pat::TupleStruct { args, .. } => {
412 args.iter().copied().for_each(f);
414 Pat::Ref { pat, .. } => f(*pat),
415 Pat::Slice { prefix, slice, suffix } => {
416 let total_iter = prefix.iter().chain(slice.iter()).chain(suffix.iter());
417 total_iter.copied().for_each(f);
419 Pat::Record { args, .. } => {
420 args.iter().map(|f| f.pat).for_each(f);
422 Pat::Box { inner } => f(*inner),