X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=crates%2Fhir_def%2Fsrc%2Fexpr.rs;h=4dca8238880d9376833bf406c65369e8550737fe;hb=0b53744f2d7e0694cd7207cca632fd6de1dc5bff;hp=b4ad984bd90295e10fa14c1a5481438cd0552dd2;hpb=2ace128dd4c9c2e5d59d21402da53654acb0c7e4;p=rust.git diff --git a/crates/hir_def/src/expr.rs b/crates/hir_def/src/expr.rs index b4ad984bd90..4dca8238880 100644 --- a/crates/hir_def/src/expr.rs +++ b/crates/hir_def/src/expr.rs @@ -14,7 +14,6 @@ use hir_expand::name::Name; use la_arena::{Idx, RawIdx}; -use syntax::ast::RangeOp; use crate::{ builtin_type::{BuiltinFloat, BuiltinInt, BuiltinUint}, @@ -24,6 +23,8 @@ 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(RawIdx::from(!0)) @@ -39,12 +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), - Uint(u64, Option), + Int(i128, Option), + Uint(u128, Option), Float(u64, Option), // FIXME: f64 is not Eq } @@ -58,9 +59,13 @@ pub enum Expr { then_branch: ExprId, else_branch: Option, }, + Let { + pat: PatId, + expr: ExprId, + }, Block { id: BlockId, - statements: Vec, + statements: Box<[Statement]>, tail: Option, label: Option, }, @@ -81,17 +86,17 @@ pub enum Expr { }, Call { callee: ExprId, - args: Vec, + args: Box<[ExprId]>, }, MethodCall { receiver: ExprId, method_name: Name, - args: Vec, + args: Box<[ExprId]>, generic_args: Option>, }, Match { expr: ExprId, - arms: Vec, + arms: Box<[MatchArm]>, }, Continue { label: Option, @@ -108,7 +113,7 @@ pub enum Expr { }, RecordLit { path: Option>, - fields: Vec, + fields: Box<[RecordLitField]>, spread: Option, }, Field { @@ -161,13 +166,13 @@ pub enum Expr { index: ExprId, }, Lambda { - args: Vec, - arg_types: Vec>>, + args: Box<[PatId]>, + arg_types: Box<[Option>]>, ret_type: Option>, body: ExprId, }, Tuple { - exprs: Vec, + exprs: Box<[ExprId]>, }, Unsafe { body: ExprId, @@ -179,50 +184,9 @@ pub enum Expr { 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 }, } @@ -241,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 { @@ -253,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 } @@ -287,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 } | Expr::Yield { expr } => { - if let Some(expr) = expr { - f(*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, .. } => { @@ -325,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 } => { @@ -345,17 +314,9 @@ 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) @@ -411,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 }, + 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),