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