1use{
2 std::cell::Cell,
3 crate::{
4 makepad_live_compiler::{
5 TokenSpan
6 },
7 shader_ast::*,
8 }
9};
10
11
12#[derive(Clone)]
13pub struct ConstGatherer<'a> {
14 pub fn_def: &'a FnDef,
15}
16
17impl<'a> ConstGatherer<'a> {
18 pub fn const_gather_expr(&self, expr: &Expr) {
19 match expr.const_val.borrow().as_ref().unwrap() {
22 Some(Val::Vec4(val)) => {
23 expr.const_index.set(Some(
24 self.fn_def.const_table.borrow().as_ref().unwrap().len(),
25 ));
26 self.write_span(&expr.span, 4);
27 self.write_f32(val.x);
28 self.write_f32(val.y);
29 self.write_f32(val.z);
30 self.write_f32(val.w);
31 return;
32 }
33 Some(Val::Float(val)) => {
34 expr.const_index.set(Some(
35 self.fn_def.const_table.borrow().as_ref().unwrap().len(),
36 ));
37 self.write_span(&expr.span, 1);
38 self.write_f32(*val);
39 return;
40 }
41 _ => {},
42 }
43
44 match expr.kind {
45 ExprKind::Cond {
46 span,
47 ref expr,
48 ref expr_if_true,
49 ref expr_if_false,
50 } => self.const_gather_cond_expr(span, expr, expr_if_true, expr_if_false),
51 ExprKind::Bin {
52 span,
53 op,
54 ref left_expr,
55 ref right_expr,
56 } => self.const_gather_bin_expr(span, op, left_expr, right_expr),
57 ExprKind::Un { span, op, ref expr } => self.const_gather_un_expr(span, op, expr),
58 ExprKind::MethodCall {
59 ref arg_exprs,
60 ..
61 } => self.const_gather_all_call_expr(arg_exprs),
62 ExprKind::PlainCall {
63 ref arg_exprs,
64 ..
65 } => self.const_gather_all_call_expr(arg_exprs),
66 ExprKind::BuiltinCall {
67 ref arg_exprs,
68 ..
69 } => self.const_gather_all_call_expr(arg_exprs),
70 ExprKind::ClosureDef(_) => (),
75 ExprKind::ConsCall {
76 ref arg_exprs,
77 ..
78 } => self.const_gather_all_call_expr(arg_exprs),
79 ExprKind::Field {
80 span,
81 ref expr,
82 field_ident,
83 } => self.const_gather_field_expr(span, expr, field_ident),
84 ExprKind::Index {
85 span,
86 ref expr,
87 ref index_expr,
88 } => self.const_gather_index_expr(span, expr, index_expr),
89 ExprKind::Var {
90 span,
91 ref kind,
92 ..
93 } => self.const_gather_var_expr(span, kind),
94 ExprKind::StructCons{
95 struct_ptr,
96 span,
97 ref args
98 } => self.const_gather_struct_cons(struct_ptr, span, args),
99 ExprKind::Lit { span, lit } => self.const_gather_lit_expr(span, lit),
100 }
101 }
102
103 fn const_gather_cond_expr(
104 &self,
105 _span: TokenSpan,
106 expr: &Expr,
107 expr_if_true: &Expr,
108 expr_if_false: &Expr,
109 ) {
110 self.const_gather_expr(expr);
111 self.const_gather_expr(expr_if_true);
112 self.const_gather_expr(expr_if_false);
113 }
114
115 #[allow(clippy::float_cmp)]
116 fn const_gather_bin_expr(&self, _span: TokenSpan, _op: BinOp, left_expr: &Expr, right_expr: &Expr) {
117 self.const_gather_expr(left_expr);
118 self.const_gather_expr(right_expr);
119 }
120
121 fn const_gather_un_expr(&self, _span: TokenSpan, _op: UnOp, expr: &Expr) {
122 self.const_gather_expr(expr);
123 }
124
125 fn const_gather_field_expr(&self, _span: TokenSpan, expr: &Expr, _field_ident: Ident) {
126 self.const_gather_expr(expr);
127 }
128
129 fn const_gather_index_expr(&self, _span: TokenSpan, expr: &Expr, _index_expr: &Expr) {
130 self.const_gather_expr(expr);
131 }
132
133 fn const_gather_all_call_expr(&self, arg_exprs: &[Expr]) {
134 for arg_expr in arg_exprs {
135 self.const_gather_expr(arg_expr);
136 }
137 }
138
139 fn const_gather_var_expr(&self, _span: TokenSpan, _kind: &Cell<Option<VarKind>>) {}
140
141 fn const_gather_lit_expr(&self, _span: TokenSpan, _lit: Lit) {}
142
143 fn const_gather_struct_cons(
144 &self,
145 _struct_ptr: StructPtr,
146 _span: TokenSpan,
147 args: &Vec<(Ident,Expr)>,
148 ) {
149 for arg in args{
150 self.const_gather_expr(&arg.1);
151 }
152 }
153
154 fn write_span(&self, span: &TokenSpan, slots:usize) {
155 let index = self.fn_def.const_table.borrow().as_ref().unwrap().len();
156 self.fn_def
157 .const_table_spans
158 .borrow_mut()
159 .as_mut()
160 .unwrap()
161 .push(ConstTableSpan{
162 token_id: span.token_id,
163 offset: index,
164 slots
165 });
166 }
167
168 fn write_f32(&self, val: f32) {
169 self.fn_def
170 .const_table
171 .borrow_mut()
172 .as_mut()
173 .unwrap()
174 .push(val);
175 }
176}