Skip to content

Commit fe4f059

Browse files
committed
internal: prepare to merge hir::BinaryOp and ast::BinOp
1 parent 6df00f8 commit fe4f059

File tree

4 files changed

+97
-97
lines changed

4 files changed

+97
-97
lines changed

crates/hir_def/src/body/lower.rs

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ use crate::{
2727
builtin_type::{BuiltinFloat, BuiltinInt, BuiltinUint},
2828
db::DefDatabase,
2929
expr::{
30-
dummy_expr_id, ArithOp, Array, BinaryOp, BindingAnnotation, CmpOp, Expr, ExprId, Label,
31-
LabelId, Literal, LogicOp, MatchArm, MatchGuard, Ordering, Pat, PatId, RecordFieldPat,
32-
RecordLitField, Statement,
30+
dummy_expr_id, Array, BinaryOp, BindingAnnotation, Expr, ExprId, Label, LabelId, Literal,
31+
MatchArm, MatchGuard, Pat, PatId, RecordFieldPat, RecordLitField, Statement,
3332
},
3433
intern::Interned,
3534
item_scope::BuiltinShadowMode,
@@ -954,50 +953,6 @@ impl ExprCollector<'_> {
954953
}
955954
}
956955

957-
impl From<ast::BinOp> for BinaryOp {
958-
fn from(ast_op: ast::BinOp) -> Self {
959-
match ast_op {
960-
ast::BinOp::BooleanOr => BinaryOp::LogicOp(LogicOp::Or),
961-
ast::BinOp::BooleanAnd => BinaryOp::LogicOp(LogicOp::And),
962-
ast::BinOp::EqualityTest => BinaryOp::CmpOp(CmpOp::Eq { negated: false }),
963-
ast::BinOp::NegatedEqualityTest => BinaryOp::CmpOp(CmpOp::Eq { negated: true }),
964-
ast::BinOp::LesserEqualTest => {
965-
BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Less, strict: false })
966-
}
967-
ast::BinOp::GreaterEqualTest => {
968-
BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Greater, strict: false })
969-
}
970-
ast::BinOp::LesserTest => {
971-
BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Less, strict: true })
972-
}
973-
ast::BinOp::GreaterTest => {
974-
BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Greater, strict: true })
975-
}
976-
ast::BinOp::Addition => BinaryOp::ArithOp(ArithOp::Add),
977-
ast::BinOp::Multiplication => BinaryOp::ArithOp(ArithOp::Mul),
978-
ast::BinOp::Subtraction => BinaryOp::ArithOp(ArithOp::Sub),
979-
ast::BinOp::Division => BinaryOp::ArithOp(ArithOp::Div),
980-
ast::BinOp::Remainder => BinaryOp::ArithOp(ArithOp::Rem),
981-
ast::BinOp::LeftShift => BinaryOp::ArithOp(ArithOp::Shl),
982-
ast::BinOp::RightShift => BinaryOp::ArithOp(ArithOp::Shr),
983-
ast::BinOp::BitwiseXor => BinaryOp::ArithOp(ArithOp::BitXor),
984-
ast::BinOp::BitwiseOr => BinaryOp::ArithOp(ArithOp::BitOr),
985-
ast::BinOp::BitwiseAnd => BinaryOp::ArithOp(ArithOp::BitAnd),
986-
ast::BinOp::Assignment => BinaryOp::Assignment { op: None },
987-
ast::BinOp::AddAssign => BinaryOp::Assignment { op: Some(ArithOp::Add) },
988-
ast::BinOp::DivAssign => BinaryOp::Assignment { op: Some(ArithOp::Div) },
989-
ast::BinOp::MulAssign => BinaryOp::Assignment { op: Some(ArithOp::Mul) },
990-
ast::BinOp::RemAssign => BinaryOp::Assignment { op: Some(ArithOp::Rem) },
991-
ast::BinOp::ShlAssign => BinaryOp::Assignment { op: Some(ArithOp::Shl) },
992-
ast::BinOp::ShrAssign => BinaryOp::Assignment { op: Some(ArithOp::Shr) },
993-
ast::BinOp::SubAssign => BinaryOp::Assignment { op: Some(ArithOp::Sub) },
994-
ast::BinOp::BitOrAssign => BinaryOp::Assignment { op: Some(ArithOp::BitOr) },
995-
ast::BinOp::BitAndAssign => BinaryOp::Assignment { op: Some(ArithOp::BitAnd) },
996-
ast::BinOp::BitXorAssign => BinaryOp::Assignment { op: Some(ArithOp::BitXor) },
997-
}
998-
}
999-
}
1000-
1001956
impl From<ast::LiteralKind> for Literal {
1002957
fn from(ast_lit_kind: ast::LiteralKind) -> Self {
1003958
match ast_lit_kind {

crates/hir_def/src/expr.rs

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
1515
use hir_expand::name::Name;
1616
use la_arena::{Idx, RawIdx};
17-
use syntax::ast::RangeOp;
1817

1918
use crate::{
2019
builtin_type::{BuiltinFloat, BuiltinInt, BuiltinUint},
@@ -24,6 +23,8 @@ use crate::{
2423
BlockId,
2524
};
2625

26+
pub use syntax::ast::{ArithOp, BinaryOp, CmpOp, LogicOp, Ordering, RangeOp, UnaryOp};
27+
2728
pub type ExprId = Idx<Expr>;
2829
pub(crate) fn dummy_expr_id() -> ExprId {
2930
ExprId::from_raw(RawIdx::from(!0))
@@ -179,47 +180,6 @@ pub enum Expr {
179180
Literal(Literal),
180181
}
181182

182-
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
183-
pub enum BinaryOp {
184-
LogicOp(LogicOp),
185-
ArithOp(ArithOp),
186-
CmpOp(CmpOp),
187-
Assignment { op: Option<ArithOp> },
188-
}
189-
190-
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
191-
pub enum LogicOp {
192-
And,
193-
Or,
194-
}
195-
196-
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
197-
pub enum CmpOp {
198-
Eq { negated: bool },
199-
Ord { ordering: Ordering, strict: bool },
200-
}
201-
202-
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
203-
pub enum Ordering {
204-
Less,
205-
Greater,
206-
}
207-
208-
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
209-
pub enum ArithOp {
210-
Add,
211-
Mul,
212-
Sub,
213-
Div,
214-
Rem,
215-
Shl,
216-
Shr,
217-
BitXor,
218-
BitOr,
219-
BitAnd,
220-
}
221-
222-
pub use syntax::ast::UnaryOp;
223183
#[derive(Debug, Clone, Eq, PartialEq)]
224184
pub enum Array {
225185
ElementList(Vec<ExprId>),

crates/syntax/src/ast.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ pub use self::{
2424
AttrKind, AttrsOwnerNode, FieldKind, Macro, NameLike, NameOrNameRef, PathSegmentKind,
2525
SelfParamKind, SlicePatComponents, StructKind, TypeBoundKind, VisibilityKind,
2626
},
27-
operators::{RangeOp, UnaryOp},
27+
operators::{ArithOp, BinaryOp, CmpOp, LogicOp, RangeOp, UnaryOp, Ordering},
2828
token_ext::{
29-
CommentKind, CommentPlacement, CommentShape, HasFormatSpecifier, IsString, QuoteOffsets,
30-
Radix,
29+
CommentKind, CommentPlacement, CommentShape, FormatSpecifier, HasFormatSpecifier, IsString,
30+
QuoteOffsets, Radix,
3131
},
3232
traits::{
3333
ArgListOwner, AttrsOwner, CommentIter, DocCommentsOwner, GenericParamsOwner, LoopBodyOwner,

crates/syntax/src/ast/operators.rs

Lines changed: 90 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
2+
pub enum RangeOp {
3+
/// `..`
4+
Exclusive,
5+
/// `..=`
6+
Inclusive,
7+
}
8+
19
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
210
pub enum UnaryOp {
311
/// The `*` operator for dereferencing
@@ -9,9 +17,86 @@ pub enum UnaryOp {
917
}
1018

1119
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
12-
pub enum RangeOp {
13-
/// `..`
14-
Exclusive,
15-
/// `..=`
16-
Inclusive,
20+
pub enum BinaryOp {
21+
LogicOp(LogicOp),
22+
ArithOp(ArithOp),
23+
CmpOp(CmpOp),
24+
Assignment { op: Option<ArithOp> },
25+
}
26+
27+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
28+
pub enum LogicOp {
29+
And,
30+
Or,
31+
}
32+
33+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
34+
pub enum CmpOp {
35+
Eq { negated: bool },
36+
Ord { ordering: Ordering, strict: bool },
37+
}
38+
39+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
40+
pub enum Ordering {
41+
Less,
42+
Greater,
43+
}
44+
45+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
46+
pub enum ArithOp {
47+
Add,
48+
Mul,
49+
Sub,
50+
Div,
51+
Rem,
52+
Shl,
53+
Shr,
54+
BitXor,
55+
BitOr,
56+
BitAnd,
57+
}
58+
59+
use crate::ast;
60+
impl From<ast::BinOp> for BinaryOp {
61+
fn from(ast_op: ast::BinOp) -> Self {
62+
match ast_op {
63+
ast::BinOp::BooleanOr => BinaryOp::LogicOp(LogicOp::Or),
64+
ast::BinOp::BooleanAnd => BinaryOp::LogicOp(LogicOp::And),
65+
ast::BinOp::EqualityTest => BinaryOp::CmpOp(CmpOp::Eq { negated: false }),
66+
ast::BinOp::NegatedEqualityTest => BinaryOp::CmpOp(CmpOp::Eq { negated: true }),
67+
ast::BinOp::LesserEqualTest => {
68+
BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Less, strict: false })
69+
}
70+
ast::BinOp::GreaterEqualTest => {
71+
BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Greater, strict: false })
72+
}
73+
ast::BinOp::LesserTest => {
74+
BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Less, strict: true })
75+
}
76+
ast::BinOp::GreaterTest => {
77+
BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Greater, strict: true })
78+
}
79+
ast::BinOp::Addition => BinaryOp::ArithOp(ArithOp::Add),
80+
ast::BinOp::Multiplication => BinaryOp::ArithOp(ArithOp::Mul),
81+
ast::BinOp::Subtraction => BinaryOp::ArithOp(ArithOp::Sub),
82+
ast::BinOp::Division => BinaryOp::ArithOp(ArithOp::Div),
83+
ast::BinOp::Remainder => BinaryOp::ArithOp(ArithOp::Rem),
84+
ast::BinOp::LeftShift => BinaryOp::ArithOp(ArithOp::Shl),
85+
ast::BinOp::RightShift => BinaryOp::ArithOp(ArithOp::Shr),
86+
ast::BinOp::BitwiseXor => BinaryOp::ArithOp(ArithOp::BitXor),
87+
ast::BinOp::BitwiseOr => BinaryOp::ArithOp(ArithOp::BitOr),
88+
ast::BinOp::BitwiseAnd => BinaryOp::ArithOp(ArithOp::BitAnd),
89+
ast::BinOp::Assignment => BinaryOp::Assignment { op: None },
90+
ast::BinOp::AddAssign => BinaryOp::Assignment { op: Some(ArithOp::Add) },
91+
ast::BinOp::DivAssign => BinaryOp::Assignment { op: Some(ArithOp::Div) },
92+
ast::BinOp::MulAssign => BinaryOp::Assignment { op: Some(ArithOp::Mul) },
93+
ast::BinOp::RemAssign => BinaryOp::Assignment { op: Some(ArithOp::Rem) },
94+
ast::BinOp::ShlAssign => BinaryOp::Assignment { op: Some(ArithOp::Shl) },
95+
ast::BinOp::ShrAssign => BinaryOp::Assignment { op: Some(ArithOp::Shr) },
96+
ast::BinOp::SubAssign => BinaryOp::Assignment { op: Some(ArithOp::Sub) },
97+
ast::BinOp::BitOrAssign => BinaryOp::Assignment { op: Some(ArithOp::BitOr) },
98+
ast::BinOp::BitAndAssign => BinaryOp::Assignment { op: Some(ArithOp::BitAnd) },
99+
ast::BinOp::BitXorAssign => BinaryOp::Assignment { op: Some(ArithOp::BitXor) },
100+
}
101+
}
17102
}

0 commit comments

Comments
 (0)