aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/expr.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/expr.rs')
-rw-r--r--crates/ra_hir/src/expr.rs101
1 files changed, 98 insertions, 3 deletions
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs
index f33676655..a16561d11 100644
--- a/crates/ra_hir/src/expr.rs
+++ b/crates/ra_hir/src/expr.rs
@@ -245,6 +245,10 @@ pub enum Expr {
245 rhs: ExprId, 245 rhs: ExprId,
246 op: Option<BinaryOp>, 246 op: Option<BinaryOp>,
247 }, 247 },
248 Index {
249 base: ExprId,
250 index: ExprId,
251 },
248 Lambda { 252 Lambda {
249 args: Vec<PatId>, 253 args: Vec<PatId>,
250 arg_types: Vec<Option<TypeRef>>, 254 arg_types: Vec<Option<TypeRef>>,
@@ -257,7 +261,46 @@ pub enum Expr {
257 Literal(Literal), 261 Literal(Literal),
258} 262}
259 263
260pub use ra_syntax::ast::BinOp as BinaryOp; 264#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
265pub enum BinaryOp {
266 LogicOp(LogicOp),
267 ArithOp(ArithOp),
268 CmpOp(CmpOp),
269 Assignment { op: Option<ArithOp> },
270}
271
272#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
273pub enum LogicOp {
274 And,
275 Or,
276}
277
278#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
279pub enum CmpOp {
280 Eq { negated: bool },
281 Ord { ordering: Ordering, strict: bool },
282}
283
284#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
285pub enum Ordering {
286 Less,
287 Greater,
288}
289
290#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
291pub enum ArithOp {
292 Add,
293 Mul,
294 Sub,
295 Div,
296 Rem,
297 Shl,
298 Shr,
299 BitXor,
300 BitOr,
301 BitAnd,
302}
303
261pub use ra_syntax::ast::PrefixOp as UnaryOp; 304pub use ra_syntax::ast::PrefixOp as UnaryOp;
262#[derive(Debug, Clone, Eq, PartialEq)] 305#[derive(Debug, Clone, Eq, PartialEq)]
263pub enum Array { 306pub enum Array {
@@ -360,6 +403,10 @@ impl Expr {
360 f(*lhs); 403 f(*lhs);
361 f(*rhs); 404 f(*rhs);
362 } 405 }
406 Expr::Index { base, index } => {
407 f(*base);
408 f(*index);
409 }
363 Expr::Field { expr, .. } 410 Expr::Field { expr, .. }
364 | Expr::Await { expr } 411 | Expr::Await { expr }
365 | Expr::Try { expr } 412 | Expr::Try { expr }
@@ -791,7 +838,7 @@ where
791 ast::ExprKind::BinExpr(e) => { 838 ast::ExprKind::BinExpr(e) => {
792 let lhs = self.collect_expr_opt(e.lhs()); 839 let lhs = self.collect_expr_opt(e.lhs());
793 let rhs = self.collect_expr_opt(e.rhs()); 840 let rhs = self.collect_expr_opt(e.rhs());
794 let op = e.op_kind(); 841 let op = e.op_kind().map(BinaryOp::from);
795 self.alloc_expr(Expr::BinaryOp { lhs, rhs, op }, syntax_ptr) 842 self.alloc_expr(Expr::BinaryOp { lhs, rhs, op }, syntax_ptr)
796 } 843 }
797 ast::ExprKind::TupleExpr(e) => { 844 ast::ExprKind::TupleExpr(e) => {
@@ -848,10 +895,14 @@ where
848 }; 895 };
849 self.alloc_expr(Expr::Literal(lit), syntax_ptr) 896 self.alloc_expr(Expr::Literal(lit), syntax_ptr)
850 } 897 }
898 ast::ExprKind::IndexExpr(e) => {
899 let base = self.collect_expr_opt(e.base());
900 let index = self.collect_expr_opt(e.index());
901 self.alloc_expr(Expr::Index { base, index }, syntax_ptr)
902 }
851 903
852 // FIXME implement HIR for these: 904 // FIXME implement HIR for these:
853 ast::ExprKind::Label(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), 905 ast::ExprKind::Label(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
854 ast::ExprKind::IndexExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
855 ast::ExprKind::RangeExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), 906 ast::ExprKind::RangeExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
856 ast::ExprKind::MacroCall(e) => { 907 ast::ExprKind::MacroCall(e) => {
857 let ast_id = self 908 let ast_id = self
@@ -1038,6 +1089,50 @@ where
1038 } 1089 }
1039} 1090}
1040 1091
1092impl From<ast::BinOp> for BinaryOp {
1093 fn from(ast_op: ast::BinOp) -> Self {
1094 match ast_op {
1095 ast::BinOp::BooleanOr => BinaryOp::LogicOp(LogicOp::Or),
1096 ast::BinOp::BooleanAnd => BinaryOp::LogicOp(LogicOp::And),
1097 ast::BinOp::EqualityTest => BinaryOp::CmpOp(CmpOp::Eq { negated: false }),
1098 ast::BinOp::NegatedEqualityTest => BinaryOp::CmpOp(CmpOp::Eq { negated: true }),
1099 ast::BinOp::LesserEqualTest => {
1100 BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Less, strict: false })
1101 }
1102 ast::BinOp::GreaterEqualTest => {
1103 BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Greater, strict: false })
1104 }
1105 ast::BinOp::LesserTest => {
1106 BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Less, strict: true })
1107 }
1108 ast::BinOp::GreaterTest => {
1109 BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Greater, strict: true })
1110 }
1111 ast::BinOp::Addition => BinaryOp::ArithOp(ArithOp::Add),
1112 ast::BinOp::Multiplication => BinaryOp::ArithOp(ArithOp::Mul),
1113 ast::BinOp::Subtraction => BinaryOp::ArithOp(ArithOp::Sub),
1114 ast::BinOp::Division => BinaryOp::ArithOp(ArithOp::Div),
1115 ast::BinOp::Remainder => BinaryOp::ArithOp(ArithOp::Rem),
1116 ast::BinOp::LeftShift => BinaryOp::ArithOp(ArithOp::Shl),
1117 ast::BinOp::RightShift => BinaryOp::ArithOp(ArithOp::Shr),
1118 ast::BinOp::BitwiseXor => BinaryOp::ArithOp(ArithOp::BitXor),
1119 ast::BinOp::BitwiseOr => BinaryOp::ArithOp(ArithOp::BitOr),
1120 ast::BinOp::BitwiseAnd => BinaryOp::ArithOp(ArithOp::BitAnd),
1121 ast::BinOp::Assignment => BinaryOp::Assignment { op: None },
1122 ast::BinOp::AddAssign => BinaryOp::Assignment { op: Some(ArithOp::Add) },
1123 ast::BinOp::DivAssign => BinaryOp::Assignment { op: Some(ArithOp::Div) },
1124 ast::BinOp::MulAssign => BinaryOp::Assignment { op: Some(ArithOp::Mul) },
1125 ast::BinOp::RemAssign => BinaryOp::Assignment { op: Some(ArithOp::Rem) },
1126 ast::BinOp::ShlAssign => BinaryOp::Assignment { op: Some(ArithOp::Shl) },
1127 ast::BinOp::ShrAssign => BinaryOp::Assignment { op: Some(ArithOp::Shr) },
1128 ast::BinOp::SubAssign => BinaryOp::Assignment { op: Some(ArithOp::Sub) },
1129 ast::BinOp::BitOrAssign => BinaryOp::Assignment { op: Some(ArithOp::BitOr) },
1130 ast::BinOp::BitAndAssign => BinaryOp::Assignment { op: Some(ArithOp::BitAnd) },
1131 ast::BinOp::BitXorAssign => BinaryOp::Assignment { op: Some(ArithOp::BitXor) },
1132 }
1133 }
1134}
1135
1041pub(crate) fn body_with_source_map_query( 1136pub(crate) fn body_with_source_map_query(
1042 db: &impl HirDatabase, 1137 db: &impl HirDatabase,
1043 def: DefWithBody, 1138 def: DefWithBody,