protospec_build/asg/expression/
mod.rs1use super::*;
2
3mod binary;
4pub use binary::*;
5
6mod unary;
7pub use unary::*;
8
9mod cast;
10pub use cast::*;
11
12mod array_index;
13pub use array_index::*;
14
15mod enum_access;
16pub use enum_access::*;
17
18mod ternary;
19pub use ternary::*;
20
21mod call;
22pub use call::*;
23
24mod const_int;
25pub use const_int::*;
26
27mod int;
28pub use int::*;
29
30mod member;
31pub use member::*;
32
33pub trait AsgExpression {
34 fn get_type(&self) -> Option<Type>;
35}
36
37#[derive(PartialEq, Clone, Debug)]
38pub enum Expression {
39 Binary(BinaryExpression),
40 Unary(UnaryExpression),
41 Cast(CastExpression),
42 ArrayIndex(ArrayIndexExpression),
43 EnumAccess(EnumAccessExpression),
44 Int(Int),
45 ConstRef(Arc<Const>),
46 InputRef(Arc<Input>),
47 FieldRef(Arc<Field>),
48 Str(ast::Str),
49 Ternary(TernaryExpression),
50 Bool(bool),
51 Call(CallExpression),
52 Member(MemberExpression),
53}
54
55impl AsgExpression for Expression {
56 fn get_type(&self) -> Option<Type> {
57 use Expression::*;
58 match self {
59 Binary(e) => e.get_type(),
60 Unary(e) => e.get_type(),
61 Cast(e) => e.get_type(),
62 ArrayIndex(e) => e.get_type(),
63 EnumAccess(e) => e.get_type(),
64 Int(e) => Some(Type::Scalar(e.type_)),
65 ConstRef(e) => e.get_type(),
66 InputRef(e) => e.get_type(),
67 FieldRef(e) => e.get_type(),
68 Str(e) => Some(Type::Array(Box::new(ArrayType {
69 element: Arc::new(Field {
70 name: "$string".to_string(),
71 arguments: RefCell::new(vec![]),
72 span: e.span,
73 type_: RefCell::new(Type::Scalar(ScalarType::U8)),
74 condition: RefCell::new(None),
75 transforms: RefCell::new(vec![]),
76 toplevel: false,
77 is_auto: Cell::new(false),
78 is_maybe_cyclical: Cell::new(false),
79 is_pad: Cell::new(false),
80 }),
81 length: LengthConstraint {
82 expandable: true,
83 value: Some(Expression::Int(self::Int {
84 value: ConstInt::U64(e.content.len() as u64),
85 type_: ScalarType::U64,
86 span: e.span,
87 })),
88 },
89 }))),
90 Ternary(e) => e.get_type(),
91 Bool(_) => Some(Type::Bool),
92 Call(ffi) => ffi.get_type(),
93 Member(e) => e.get_type(),
94 }
95 }
96}
97
98impl fmt::Display for Expression {
99 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
100 write!(f, "..")
101 }
114}
115
116impl From<Int> for Expression {
117 fn from(from: Int) -> Self {
118 Expression::Int(from)
119 }
120}
121
122impl From<u64> for Expression {
123 fn from(from: u64) -> Self {
124 Expression::Int(from.into())
125 }
126}