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)]
44 ByteString(Box<[u8]>),
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>,
68 statements: Box<[Statement]>,
70 label: Option<LabelId>,
74 label: Option<LabelId>,
79 label: Option<LabelId>,
85 label: Option<LabelId>,
95 generic_args: Option<Box<GenericArgs>>,
99 arms: Box<[MatchArm]>,
105 expr: Option<ExprId>,
109 expr: Option<ExprId>,
112 expr: Option<ExprId>,
115 path: Option<Box<Path>>,
116 fields: Box<[RecordLitField]>,
117 spread: Option<ExprId>,
140 type_ref: Interned<TypeRef>,
145 mutability: Mutability,
157 op: Option<BinaryOp>,
170 arg_types: Box<[Option<Interned<TypeRef>>]>,
171 ret_type: Option<Interned<TypeRef>>,
175 exprs: Box<[ExprId]>,
187 #[derive(Debug, Clone, Eq, PartialEq)]
189 ElementList(Box<[ExprId]>),
190 Repeat { initializer: ExprId, repeat: ExprId },
193 #[derive(Debug, Clone, Eq, PartialEq)]
194 pub struct MatchArm {
196 pub guard: Option<ExprId>,
200 #[derive(Debug, Clone, Eq, PartialEq)]
201 pub struct RecordLitField {
206 #[derive(Debug, Clone, Eq, PartialEq)]
210 type_ref: Option<Interned<TypeRef>>,
211 initializer: Option<ExprId>,
212 else_branch: Option<ExprId>,
221 pub fn walk_child_exprs(&self, mut f: impl FnMut(ExprId)) {
225 Expr::If { condition, then_branch, else_branch } => {
228 if let &Some(else_branch) = else_branch {
232 Expr::Let { expr, .. } => {
235 Expr::Block { statements, tail, .. } => {
236 for stmt in statements.iter() {
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 } => {
265 args.iter().copied().for_each(f);
267 Expr::MethodCall { receiver, args, .. } => {
269 args.iter().copied().for_each(f);
271 Expr::Match { expr, arms } => {
273 arms.iter().map(|arm| arm.expr).for_each(f);
275 Expr::Continue { .. } => {}
276 Expr::Break { expr, .. } | Expr::Return { expr } | Expr::Yield { expr } => {
277 if let &Some(expr) = expr {
281 Expr::RecordLit { fields, spread, .. } => {
282 for field in fields.iter() {
285 if let &Some(expr) = spread {
289 Expr::Lambda { body, .. } => {
292 Expr::BinaryOp { lhs, rhs, .. } => {
296 Expr::Range { lhs, rhs, .. } => {
297 if let &Some(lhs) = rhs {
300 if let &Some(rhs) = lhs {
304 Expr::Index { base, index } => {
308 Expr::Field { expr, .. }
309 | Expr::Await { expr }
311 | Expr::Cast { expr, .. }
312 | Expr::Ref { expr, .. }
313 | Expr::UnaryOp { expr, .. }
314 | Expr::Box { expr } => {
317 Expr::Tuple { exprs } => exprs.iter().copied().for_each(f),
318 Expr::Array(a) => match a {
319 Array::ElementList(exprs) => exprs.iter().copied().for_each(f),
320 Array::Repeat { initializer, repeat } => {
325 Expr::MacroStmts { tail } => f(*tail),
326 Expr::Literal(_) => {}
331 /// Explicit binding annotations given in the HIR for a binding. Note
332 /// that this is not the final binding *mode* that we infer after type
334 #[derive(Clone, PartialEq, Eq, Debug, Copy)]
335 pub enum BindingAnnotation {
336 /// No binding annotation given: this means that the final binding mode
337 /// will depend on whether we have skipped through a `&` reference
338 /// when matching. For example, the `x` in `Some(x)` will have binding
339 /// mode `None`; if you do `let Some(x) = &Some(22)`, it will
340 /// ultimately be inferred to be by-reference.
343 /// Annotated with `mut x` -- could be either ref or not, similar to `None`.
346 /// Annotated as `ref`, like `ref x`
349 /// Annotated as `ref mut x`.
353 impl BindingAnnotation {
354 pub fn new(is_mutable: bool, is_ref: bool) -> Self {
355 match (is_mutable, is_ref) {
356 (true, true) => BindingAnnotation::RefMut,
357 (false, true) => BindingAnnotation::Ref,
358 (true, false) => BindingAnnotation::Mutable,
359 (false, false) => BindingAnnotation::Unannotated,
364 #[derive(Debug, Clone, Eq, PartialEq)]
365 pub struct RecordFieldPat {
370 /// Close relative to rustc's hir::PatKind
371 #[derive(Debug, Clone, Eq, PartialEq)]
375 Tuple { args: Box<[PatId]>, ellipsis: Option<usize> },
377 Record { path: Option<Box<Path>>, args: Box<[RecordFieldPat]>, ellipsis: bool },
378 Range { start: ExprId, end: ExprId },
379 Slice { prefix: Box<[PatId]>, slice: Option<PatId>, suffix: Box<[PatId]> },
382 Bind { mode: BindingAnnotation, name: Name, subpat: Option<PatId> },
383 TupleStruct { path: Option<Box<Path>>, args: Box<[PatId]>, ellipsis: Option<usize> },
384 Ref { pat: PatId, mutability: Mutability },
385 Box { inner: PatId },
390 pub fn walk_child_pats(&self, mut f: impl FnMut(PatId)) {
395 | Pat::ConstBlock(..)
398 Pat::Bind { subpat, .. } => {
399 subpat.iter().copied().for_each(f);
401 Pat::Or(args) | Pat::Tuple { args, .. } | Pat::TupleStruct { args, .. } => {
402 args.iter().copied().for_each(f);
404 Pat::Ref { pat, .. } => f(*pat),
405 Pat::Slice { prefix, slice, suffix } => {
406 let total_iter = prefix.iter().chain(slice.iter()).chain(suffix.iter());
407 total_iter.copied().for_each(f);
409 Pat::Record { args, .. } => {
410 args.iter().map(|f| f.pat).for_each(f);
412 Pat::Box { inner } => f(*inner),