]> git.lizzy.rs Git - rust.git/blobdiff - crates/hir_def/src/expr.rs
internal: Replace Vec with Box in hir Expr
[rust.git] / crates / hir_def / src / expr.rs
index 0c3b410802fe5be48e42dda6272620f579b9771c..587a5ce46d4a3e33cde8a8e815c39f43e260d5cd 100644 (file)
@@ -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<Expr>;
 pub(crate) fn dummy_expr_id() -> ExprId {
     ExprId::from_raw(RawIdx::from(!0))
@@ -43,8 +44,8 @@ pub enum Literal {
     ByteString(Vec<u8>),
     Char(char),
     Bool(bool),
-    Int(u64, Option<BuiltinInt>),
-    Uint(u64, Option<BuiltinUint>),
+    Int(i128, Option<BuiltinInt>),
+    Uint(u128, Option<BuiltinUint>),
     Float(u64, Option<BuiltinFloat>), // FIXME: f64 is not Eq
 }
 
@@ -60,7 +61,7 @@ pub enum Expr {
     },
     Block {
         id: BlockId,
-        statements: Vec<Statement>,
+        statements: Box<[Statement]>,
         tail: Option<ExprId>,
         label: Option<LabelId>,
     },
@@ -81,17 +82,17 @@ pub enum Expr {
     },
     Call {
         callee: ExprId,
-        args: Vec<ExprId>,
+        args: Box<[ExprId]>,
     },
     MethodCall {
         receiver: ExprId,
         method_name: Name,
-        args: Vec<ExprId>,
+        args: Box<[ExprId]>,
         generic_args: Option<Box<GenericArgs>>,
     },
     Match {
         expr: ExprId,
-        arms: Vec<MatchArm>,
+        arms: Box<[MatchArm]>,
     },
     Continue {
         label: Option<Name>,
@@ -108,7 +109,7 @@ pub enum Expr {
     },
     RecordLit {
         path: Option<Box<Path>>,
-        fields: Vec<RecordLitField>,
+        fields: Box<[RecordLitField]>,
         spread: Option<ExprId>,
     },
     Field {
@@ -161,13 +162,13 @@ pub enum Expr {
         index: ExprId,
     },
     Lambda {
-        args: Vec<PatId>,
-        arg_types: Vec<Option<Interned<TypeRef>>>,
+        args: Box<[PatId]>,
+        arg_types: Box<[Option<Interned<TypeRef>>]>,
         ret_type: Option<Interned<TypeRef>>,
         body: ExprId,
     },
     Tuple {
-        exprs: Vec<ExprId>,
+        exprs: Box<[ExprId]>,
     },
     Unsafe {
         body: ExprId,
@@ -179,47 +180,6 @@ pub enum Expr {
     Literal(Literal),
 }
 
-#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
-pub enum BinaryOp {
-    LogicOp(LogicOp),
-    ArithOp(ArithOp),
-    CmpOp(CmpOp),
-    Assignment { op: Option<ArithOp> },
-}
-
-#[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<ExprId>),
@@ -229,10 +189,17 @@ pub enum Array {
 #[derive(Debug, Clone, Eq, PartialEq)]
 pub struct MatchArm {
     pub pat: PatId,
-    pub guard: Option<ExprId>,
+    pub guard: Option<MatchGuard>,
     pub expr: ExprId,
 }
 
+#[derive(Debug, Clone, Eq, PartialEq)]
+pub enum MatchGuard {
+    If { expr: ExprId },
+
+    IfLet { pat: PatId, expr: ExprId },
+}
+
 #[derive(Debug, Clone, Eq, PartialEq)]
 pub struct RecordLitField {
     pub name: Name,
@@ -241,8 +208,16 @@ pub struct RecordLitField {
 
 #[derive(Debug, Clone, Eq, PartialEq)]
 pub enum Statement {
-    Let { pat: PatId, type_ref: Option<Interned<TypeRef>>, initializer: Option<ExprId> },
-    Expr { expr: ExprId, has_semi: bool },
+    Let {
+        pat: PatId,
+        type_ref: Option<Interned<TypeRef>>,
+        initializer: Option<ExprId>,
+        else_branch: Option<ExprId>,
+    },
+    Expr {
+        expr: ExprId,
+        has_semi: bool,
+    },
 }
 
 impl Expr {
@@ -258,7 +233,7 @@ pub fn walk_child_exprs(&self, mut f: impl FnMut(ExprId)) {
                 }
             }
             Expr::Block { statements, tail, .. } => {
-                for stmt in statements {
+                for stmt in statements.iter() {
                     match stmt {
                         Statement::Let { initializer, .. } => {
                             if let Some(expr) = initializer {
@@ -287,19 +262,19 @@ pub fn walk_child_exprs(&self, mut f: impl FnMut(ExprId)) {
             }
             Expr::Call { callee, args } => {
                 f(*callee);
-                for arg in args {
+                for arg in args.iter() {
                     f(*arg);
                 }
             }
             Expr::MethodCall { receiver, args, .. } => {
                 f(*receiver);
-                for arg in args {
+                for arg in args.iter() {
                     f(*arg);
                 }
             }
             Expr::Match { expr, arms } => {
                 f(*expr);
-                for arm in arms {
+                for arm in arms.iter() {
                     f(arm.expr);
                 }
             }
@@ -310,7 +285,7 @@ pub fn walk_child_exprs(&self, mut f: impl FnMut(ExprId)) {
                 }
             }
             Expr::RecordLit { fields, spread, .. } => {
-                for field in fields {
+                for field in fields.iter() {
                     f(field.expr);
                 }
                 if let Some(expr) = spread {
@@ -346,7 +321,7 @@ pub fn walk_child_exprs(&self, mut f: impl FnMut(ExprId)) {
                 f(*expr);
             }
             Expr::Tuple { exprs } => {
-                for expr in exprs {
+                for expr in exprs.iter() {
                     f(*expr);
                 }
             }