X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=crates%2Fhir_def%2Fsrc%2Fexpr.rs;h=4dca8238880d9376833bf406c65369e8550737fe;hb=0b53744f2d7e0694cd7207cca632fd6de1dc5bff;hp=76f5721e58786424ecf0b368ed5c654cb8f8b732;hpb=8c33ffecc1feea732f2ea18b3701c145adc73928;p=rust.git diff --git a/crates/hir_def/src/expr.rs b/crates/hir_def/src/expr.rs index 76f5721e587..4dca8238880 100644 --- a/crates/hir_def/src/expr.rs +++ b/crates/hir_def/src/expr.rs @@ -12,19 +12,22 @@ //! //! See also a neighboring `body` module. -use arena::{Idx, RawId}; use hir_expand::name::Name; -use syntax::ast::RangeOp; +use la_arena::{Idx, RawIdx}; use crate::{ - builtin_type::{BuiltinFloat, BuiltinInt}, + builtin_type::{BuiltinFloat, BuiltinInt, BuiltinUint}, + intern::Interned, path::{GenericArgs, Path}, type_ref::{Mutability, Rawness, TypeRef}, + BlockId, }; +pub use syntax::ast::{ArithOp, BinaryOp, CmpOp, LogicOp, Ordering, RangeOp, UnaryOp}; + pub type ExprId = Idx; pub(crate) fn dummy_expr_id() -> ExprId { - ExprId::from_raw(RawId::from(!0)) + ExprId::from_raw(RawIdx::from(!0)) } pub type PatId = Idx; @@ -37,11 +40,12 @@ pub struct Label { #[derive(Debug, Clone, Eq, PartialEq)] pub enum Literal { - String(String), - ByteString(Vec), + String(Box), + ByteString(Box<[u8]>), Char(char), Bool(bool), - Int(u64, Option), + Int(i128, Option), + Uint(u128, Option), Float(u64, Option), // FIXME: f64 is not Eq } @@ -55,8 +59,13 @@ pub enum Expr { then_branch: ExprId, else_branch: Option, }, + Let { + pat: PatId, + expr: ExprId, + }, Block { - statements: Vec, + id: BlockId, + statements: Box<[Statement]>, tail: Option, label: Option, }, @@ -77,17 +86,17 @@ pub enum Expr { }, Call { callee: ExprId, - args: Vec, + args: Box<[ExprId]>, }, MethodCall { receiver: ExprId, method_name: Name, - args: Vec, - generic_args: Option, + args: Box<[ExprId]>, + generic_args: Option>, }, Match { expr: ExprId, - arms: Vec, + arms: Box<[MatchArm]>, }, Continue { label: Option, @@ -99,9 +108,12 @@ pub enum Expr { Return { expr: Option, }, + Yield { + expr: Option, + }, RecordLit { - path: Option, - fields: Vec, + path: Option>, + fields: Box<[RecordLitField]>, spread: Option, }, Field { @@ -125,7 +137,7 @@ pub enum Expr { }, Cast { expr: ExprId, - type_ref: TypeRef, + type_ref: Interned, }, Ref { expr: ExprId, @@ -154,65 +166,27 @@ pub enum Expr { index: ExprId, }, Lambda { - args: Vec, - arg_types: Vec>, - ret_type: Option, + args: Box<[PatId]>, + arg_types: Box<[Option>]>, + ret_type: Option>, body: ExprId, }, Tuple { - exprs: Vec, + exprs: Box<[ExprId]>, }, Unsafe { body: ExprId, }, + MacroStmts { + tail: ExprId, + }, Array(Array), Literal(Literal), } -#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] -pub enum BinaryOp { - LogicOp(LogicOp), - ArithOp(ArithOp), - CmpOp(CmpOp), - Assignment { op: Option }, -} - -#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] -pub enum LogicOp { - And, - Or, -} - -#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] -pub enum CmpOp { - Eq { negated: bool }, - Ord { ordering: Ordering, strict: bool }, -} - -#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] -pub enum Ordering { - Less, - Greater, -} - -#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] -pub enum ArithOp { - Add, - Mul, - Sub, - Div, - Rem, - Shl, - Shr, - BitXor, - BitOr, - BitAnd, -} - -pub use syntax::ast::PrefixOp as UnaryOp; #[derive(Debug, Clone, Eq, PartialEq)] pub enum Array { - ElementList(Vec), + ElementList(Box<[ExprId]>), Repeat { initializer: ExprId, repeat: ExprId }, } @@ -231,8 +205,16 @@ pub struct RecordLitField { #[derive(Debug, Clone, Eq, PartialEq)] pub enum Statement { - Let { pat: PatId, type_ref: Option, initializer: Option }, - Expr(ExprId), + Let { + pat: PatId, + type_ref: Option>, + initializer: Option, + else_branch: Option, + }, + Expr { + expr: ExprId, + has_semi: bool, + }, } impl Expr { @@ -243,23 +225,26 @@ pub fn walk_child_exprs(&self, mut f: impl FnMut(ExprId)) { Expr::If { condition, then_branch, else_branch } => { f(*condition); f(*then_branch); - if let Some(else_branch) = else_branch { - f(*else_branch); + if let &Some(else_branch) = else_branch { + f(else_branch); } } + Expr::Let { expr, .. } => { + f(*expr); + } Expr::Block { statements, tail, .. } => { - for stmt in statements { + for stmt in statements.iter() { match stmt { Statement::Let { initializer, .. } => { - if let Some(expr) = initializer { - f(*expr); + if let &Some(expr) = initializer { + f(expr); } } - Statement::Expr(e) => f(*e), + Statement::Expr { expr: expression, .. } => f(*expression), } } - if let Some(expr) = tail { - f(*expr); + if let &Some(expr) = tail { + f(expr); } } Expr::TryBlock { body } @@ -277,34 +262,28 @@ pub fn walk_child_exprs(&self, mut f: impl FnMut(ExprId)) { } Expr::Call { callee, args } => { f(*callee); - for arg in args { - f(*arg); - } + args.iter().copied().for_each(f); } Expr::MethodCall { receiver, args, .. } => { f(*receiver); - for arg in args { - f(*arg); - } + args.iter().copied().for_each(f); } Expr::Match { expr, arms } => { f(*expr); - for arm in arms { - f(arm.expr); - } + arms.iter().map(|arm| arm.expr).for_each(f); } Expr::Continue { .. } => {} - Expr::Break { expr, .. } | Expr::Return { expr } => { - if let Some(expr) = expr { - f(*expr); + Expr::Break { expr, .. } | Expr::Return { expr } | Expr::Yield { expr } => { + if let &Some(expr) = expr { + f(expr); } } Expr::RecordLit { fields, spread, .. } => { - for field in fields { + for field in fields.iter() { f(field.expr); } - if let Some(expr) = spread { - f(*expr); + if let &Some(expr) = spread { + f(expr); } } Expr::Lambda { body, .. } => { @@ -315,11 +294,11 @@ pub fn walk_child_exprs(&self, mut f: impl FnMut(ExprId)) { f(*rhs); } Expr::Range { lhs, rhs, .. } => { - if let Some(lhs) = rhs { - f(*lhs); + if let &Some(lhs) = rhs { + f(lhs); } - if let Some(rhs) = lhs { - f(*rhs); + if let &Some(rhs) = lhs { + f(rhs); } } Expr::Index { base, index } => { @@ -335,22 +314,15 @@ pub fn walk_child_exprs(&self, mut f: impl FnMut(ExprId)) { | Expr::Box { expr } => { f(*expr); } - Expr::Tuple { exprs } => { - for expr in exprs { - f(*expr); - } - } + Expr::Tuple { exprs } => exprs.iter().copied().for_each(f), Expr::Array(a) => match a { - Array::ElementList(exprs) => { - for expr in exprs { - f(*expr); - } - } + Array::ElementList(exprs) => exprs.iter().copied().for_each(f), Array::Repeat { initializer, repeat } => { f(*initializer); f(*repeat) } }, + Expr::MacroStmts { tail } => f(*tail), Expr::Literal(_) => {} } } @@ -400,15 +372,15 @@ pub struct RecordFieldPat { pub enum Pat { Missing, Wild, - Tuple { args: Vec, ellipsis: Option }, - Or(Vec), - Record { path: Option, args: Vec, ellipsis: bool }, + Tuple { args: Box<[PatId]>, ellipsis: Option }, + Or(Box<[PatId]>), + Record { path: Option>, args: Box<[RecordFieldPat]>, ellipsis: bool }, Range { start: ExprId, end: ExprId }, - Slice { prefix: Vec, slice: Option, suffix: Vec }, - Path(Path), + Slice { prefix: Box<[PatId]>, slice: Option, suffix: Box<[PatId]> }, + Path(Box), Lit(ExprId), Bind { mode: BindingAnnotation, name: Name, subpat: Option }, - TupleStruct { path: Option, args: Vec, ellipsis: Option }, + TupleStruct { path: Option>, args: Box<[PatId]>, ellipsis: Option }, Ref { pat: PatId, mutability: Mutability }, Box { inner: PatId }, ConstBlock(ExprId),