aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/expr.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-08-17 15:42:41 +0100
committerAleksey Kladov <[email protected]>2019-08-17 15:42:41 +0100
commit7e5a186c1fe585aac95019addc963bf74cb112ae (patch)
tree3ece58abec006c824f38e8470f67229bc7c6acdd /crates/ra_hir/src/expr.rs
parent8919aa8065c31d55050a6bfe10b574fc71bcec09 (diff)
Introduce separate hir::BinaryOp
Unlike ast::BinOp, it has significantly more structure to it, so it's easier to, say, handle all assignment-like operations in the same way.
Diffstat (limited to 'crates/ra_hir/src/expr.rs')
-rw-r--r--crates/ra_hir/src/expr.rs77
1 files changed, 75 insertions, 2 deletions
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs
index f33676655..ea3fa4417 100644
--- a/crates/ra_hir/src/expr.rs
+++ b/crates/ra_hir/src/expr.rs
@@ -257,7 +257,44 @@ pub enum Expr {
257 Literal(Literal), 257 Literal(Literal),
258} 258}
259 259
260pub use ra_syntax::ast::BinOp as BinaryOp; 260#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
261pub enum BinaryOp {
262 LogicOp(LogicOp),
263 ArithOp(ArithOp),
264 CmpOp(CmpOp),
265 Assignment { op: Option<ArithOp> },
266}
267
268#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
269pub enum LogicOp {
270 And,
271 Or,
272}
273
274#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
275pub enum CmpOp {
276 Equal,
277 NotEqual,
278 Less,
279 LessOrEqual,
280 Greater,
281 GreaterOrEqual,
282}
283
284#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
285pub enum ArithOp {
286 Add,
287 Mul,
288 Sub,
289 Div,
290 Rem,
291 Shl,
292 Shr,
293 BitXor,
294 BitOr,
295 BitAnd,
296}
297
261pub use ra_syntax::ast::PrefixOp as UnaryOp; 298pub use ra_syntax::ast::PrefixOp as UnaryOp;
262#[derive(Debug, Clone, Eq, PartialEq)] 299#[derive(Debug, Clone, Eq, PartialEq)]
263pub enum Array { 300pub enum Array {
@@ -791,7 +828,7 @@ where
791 ast::ExprKind::BinExpr(e) => { 828 ast::ExprKind::BinExpr(e) => {
792 let lhs = self.collect_expr_opt(e.lhs()); 829 let lhs = self.collect_expr_opt(e.lhs());
793 let rhs = self.collect_expr_opt(e.rhs()); 830 let rhs = self.collect_expr_opt(e.rhs());
794 let op = e.op_kind(); 831 let op = e.op_kind().map(BinaryOp::from);
795 self.alloc_expr(Expr::BinaryOp { lhs, rhs, op }, syntax_ptr) 832 self.alloc_expr(Expr::BinaryOp { lhs, rhs, op }, syntax_ptr)
796 } 833 }
797 ast::ExprKind::TupleExpr(e) => { 834 ast::ExprKind::TupleExpr(e) => {
@@ -1038,6 +1075,42 @@ where
1038 } 1075 }
1039} 1076}
1040 1077
1078impl From<ast::BinOp> for BinaryOp {
1079 fn from(ast_op: ast::BinOp) -> Self {
1080 match ast_op {
1081 ast::BinOp::BooleanOr => BinaryOp::LogicOp(LogicOp::Or),
1082 ast::BinOp::BooleanAnd => BinaryOp::LogicOp(LogicOp::And),
1083 ast::BinOp::EqualityTest => BinaryOp::CmpOp(CmpOp::Equal),
1084 ast::BinOp::NegatedEqualityTest => BinaryOp::CmpOp(CmpOp::NotEqual),
1085 ast::BinOp::LesserEqualTest => BinaryOp::CmpOp(CmpOp::LessOrEqual),
1086 ast::BinOp::GreaterEqualTest => BinaryOp::CmpOp(CmpOp::GreaterOrEqual),
1087 ast::BinOp::LesserTest => BinaryOp::CmpOp(CmpOp::Less),
1088 ast::BinOp::GreaterTest => BinaryOp::CmpOp(CmpOp::Greater),
1089 ast::BinOp::Addition => BinaryOp::ArithOp(ArithOp::Add),
1090 ast::BinOp::Multiplication => BinaryOp::ArithOp(ArithOp::Mul),
1091 ast::BinOp::Subtraction => BinaryOp::ArithOp(ArithOp::Sub),
1092 ast::BinOp::Division => BinaryOp::ArithOp(ArithOp::Div),
1093 ast::BinOp::Remainder => BinaryOp::ArithOp(ArithOp::Rem),
1094 ast::BinOp::LeftShift => BinaryOp::ArithOp(ArithOp::Shl),
1095 ast::BinOp::RightShift => BinaryOp::ArithOp(ArithOp::Shr),
1096 ast::BinOp::BitwiseXor => BinaryOp::ArithOp(ArithOp::BitXor),
1097 ast::BinOp::BitwiseOr => BinaryOp::ArithOp(ArithOp::BitOr),
1098 ast::BinOp::BitwiseAnd => BinaryOp::ArithOp(ArithOp::BitAnd),
1099 ast::BinOp::Assignment => BinaryOp::Assignment { op: None },
1100 ast::BinOp::AddAssign => BinaryOp::Assignment { op: Some(ArithOp::Add) },
1101 ast::BinOp::DivAssign => BinaryOp::Assignment { op: Some(ArithOp::Div) },
1102 ast::BinOp::MulAssign => BinaryOp::Assignment { op: Some(ArithOp::Mul) },
1103 ast::BinOp::RemAssign => BinaryOp::Assignment { op: Some(ArithOp::Rem) },
1104 ast::BinOp::ShlAssign => BinaryOp::Assignment { op: Some(ArithOp::Shl) },
1105 ast::BinOp::ShrAssign => BinaryOp::Assignment { op: Some(ArithOp::Shr) },
1106 ast::BinOp::SubAssign => BinaryOp::Assignment { op: Some(ArithOp::Sub) },
1107 ast::BinOp::BitOrAssign => BinaryOp::Assignment { op: Some(ArithOp::BitOr) },
1108 ast::BinOp::BitAndAssign => BinaryOp::Assignment { op: Some(ArithOp::BitAnd) },
1109 ast::BinOp::BitXorAssign => BinaryOp::Assignment { op: Some(ArithOp::BitXor) },
1110 }
1111 }
1112}
1113
1041pub(crate) fn body_with_source_map_query( 1114pub(crate) fn body_with_source_map_query(
1042 db: &impl HirDatabase, 1115 db: &impl HirDatabase,
1043 def: DefWithBody, 1116 def: DefWithBody,