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>,
121 mutability: Mutability,
133 op: Option<BinaryOp>,
146 arg_types: Vec<Option<TypeRef>>,
147 ret_type: Option<TypeRef>,
160 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
165 Assignment { op: Option<ArithOp> },
168 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
174 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
176 Eq { negated: bool },
177 Ord { ordering: Ordering, strict: bool },
180 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
186 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
200 pub use syntax::ast::PrefixOp as UnaryOp;
201 #[derive(Debug, Clone, Eq, PartialEq)]
203 ElementList(Vec<ExprId>),
204 Repeat { initializer: ExprId, repeat: ExprId },
207 #[derive(Debug, Clone, Eq, PartialEq)]
208 pub struct MatchArm {
210 pub guard: Option<ExprId>,
214 #[derive(Debug, Clone, Eq, PartialEq)]
215 pub struct RecordLitField {
220 #[derive(Debug, Clone, Eq, PartialEq)]
222 Let { pat: PatId, type_ref: Option<TypeRef>, initializer: Option<ExprId> },
227 pub fn walk_child_exprs(&self, mut f: impl FnMut(ExprId)) {
231 Expr::If { condition, then_branch, else_branch } => {
234 if let Some(else_branch) = else_branch {
238 Expr::Block { statements, tail, .. } => {
239 for stmt in statements {
241 Statement::Let { initializer, .. } => {
242 if let Some(expr) = initializer {
246 Statement::Expr(e) => f(*e),
249 if let Some(expr) = tail {
253 Expr::TryBlock { body } | Expr::Unsafe { 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 } => {
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::Literal(_) => {}
344 /// Explicit binding annotations given in the HIR for a binding. Note
345 /// that this is not the final binding *mode* that we infer after type
347 #[derive(Clone, PartialEq, Eq, Debug, Copy)]
348 pub enum BindingAnnotation {
349 /// No binding annotation given: this means that the final binding mode
350 /// will depend on whether we have skipped through a `&` reference
351 /// when matching. For example, the `x` in `Some(x)` will have binding
352 /// mode `None`; if you do `let Some(x) = &Some(22)`, it will
353 /// ultimately be inferred to be by-reference.
356 /// Annotated with `mut x` -- could be either ref or not, similar to `None`.
359 /// Annotated as `ref`, like `ref x`
362 /// Annotated as `ref mut x`.
366 impl BindingAnnotation {
367 pub fn new(is_mutable: bool, is_ref: bool) -> Self {
368 match (is_mutable, is_ref) {
369 (true, true) => BindingAnnotation::RefMut,
370 (false, true) => BindingAnnotation::Ref,
371 (true, false) => BindingAnnotation::Mutable,
372 (false, false) => BindingAnnotation::Unannotated,
377 #[derive(Debug, Clone, Eq, PartialEq)]
378 pub struct RecordFieldPat {
383 /// Close relative to rustc's hir::PatKind
384 #[derive(Debug, Clone, Eq, PartialEq)]
388 Tuple { args: Vec<PatId>, ellipsis: Option<usize> },
390 Record { path: Option<Path>, args: Vec<RecordFieldPat>, ellipsis: bool },
391 Range { start: ExprId, end: ExprId },
392 Slice { prefix: Vec<PatId>, slice: Option<PatId>, suffix: Vec<PatId> },
395 Bind { mode: BindingAnnotation, name: Name, subpat: Option<PatId> },
396 TupleStruct { path: Option<Path>, args: Vec<PatId>, ellipsis: Option<usize> },
397 Ref { pat: PatId, mutability: Mutability },
398 Box { inner: PatId },
402 pub fn walk_child_pats(&self, mut f: impl FnMut(PatId)) {
404 Pat::Range { .. } | Pat::Lit(..) | Pat::Path(..) | Pat::Wild | Pat::Missing => {}
405 Pat::Bind { subpat, .. } => {
406 subpat.iter().copied().for_each(f);
408 Pat::Or(args) | Pat::Tuple { args, .. } | Pat::TupleStruct { args, .. } => {
409 args.iter().copied().for_each(f);
411 Pat::Ref { pat, .. } => f(*pat),
412 Pat::Slice { prefix, slice, suffix } => {
413 let total_iter = prefix.iter().chain(slice.iter()).chain(suffix.iter());
414 total_iter.copied().for_each(f);
416 Pat::Record { args, .. } => {
417 args.iter().map(|f| f.pat).for_each(f);
419 Pat::Box { inner } => f(*inner),