glsl2: Clone methods return the type of the thing being cloned
[mesa.git] / src / glsl / ir_clone.cpp
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include <string.h>
25 #include "ir.h"
26 #include "glsl_types.h"
27 extern "C" {
28 #include "hash_table.h"
29 }
30
31 /**
32 * Duplicate an IR variable
33 *
34 * \note
35 * This will probably be made \c virtual and moved to the base class
36 * eventually.
37 */
38 ir_variable *
39 ir_variable::clone(struct hash_table *ht) const
40 {
41 void *ctx = talloc_parent(this);
42 ir_variable *var = new(ctx) ir_variable(type, name);
43
44 var->max_array_access = this->max_array_access;
45 var->read_only = this->read_only;
46 var->centroid = this->centroid;
47 var->invariant = this->invariant;
48 var->mode = this->mode;
49 var->interpolation = this->interpolation;
50
51 if (ht) {
52 hash_table_insert(ht, var, (void *)const_cast<ir_variable *>(this));
53 }
54
55 return var;
56 }
57
58 ir_swizzle *
59 ir_swizzle::clone(struct hash_table *ht) const
60 {
61 void *ctx = talloc_parent(this);
62 return new(ctx) ir_swizzle((ir_rvalue *)this->val->clone(ht), this->mask);
63 }
64
65 ir_return *
66 ir_return::clone(struct hash_table *ht) const
67 {
68 void *ctx = talloc_parent(this);
69 ir_rvalue *new_value = NULL;
70
71 if (this->value)
72 new_value = (ir_rvalue *)this->value->clone(ht);
73
74 return new(ctx) ir_return(new_value);
75 }
76
77 ir_discard *
78 ir_discard::clone(struct hash_table *ht) const
79 {
80 void *ctx = talloc_parent(this);
81 ir_rvalue *new_condition = NULL;
82
83 if (this->condition != NULL)
84 new_condition = (ir_rvalue *) this->condition->clone(ht);
85
86 return new(ctx) ir_discard(new_condition);
87 }
88
89 ir_loop_jump *
90 ir_loop_jump::clone(struct hash_table *ht) const
91 {
92 void *ctx = talloc_parent(this);
93 (void)ht;
94
95 return new(ctx) ir_loop_jump(this->mode);
96 }
97
98 ir_if *
99 ir_if::clone(struct hash_table *ht) const
100 {
101 void *ctx = talloc_parent(this);
102 ir_if *new_if = new(ctx) ir_if((ir_rvalue *)this->condition->clone(ht));
103
104 foreach_iter(exec_list_iterator, iter, this->then_instructions) {
105 ir_instruction *ir = (ir_instruction *)iter.get();
106 new_if->then_instructions.push_tail(ir->clone(ht));
107 }
108
109 foreach_iter(exec_list_iterator, iter, this->else_instructions) {
110 ir_instruction *ir = (ir_instruction *)iter.get();
111 new_if->else_instructions.push_tail(ir->clone(ht));
112 }
113
114 return new_if;
115 }
116
117 ir_loop *
118 ir_loop::clone(struct hash_table *ht) const
119 {
120 void *ctx = talloc_parent(this);
121 ir_loop *new_loop = new(ctx) ir_loop();
122
123 if (this->from)
124 new_loop->from = (ir_rvalue *)this->from->clone(ht);
125 if (this->to)
126 new_loop->to = (ir_rvalue *)this->to->clone(ht);
127 if (this->increment)
128 new_loop->increment = (ir_rvalue *)this->increment->clone(ht);
129 new_loop->counter = counter;
130
131 foreach_iter(exec_list_iterator, iter, this->body_instructions) {
132 ir_instruction *ir = (ir_instruction *)iter.get();
133 new_loop->body_instructions.push_tail(ir->clone(ht));
134 }
135
136 return new_loop;
137 }
138
139 ir_call *
140 ir_call::clone(struct hash_table *ht) const
141 {
142 void *ctx = talloc_parent(this);
143 exec_list new_parameters;
144
145 foreach_iter(exec_list_iterator, iter, this->actual_parameters) {
146 ir_instruction *ir = (ir_instruction *)iter.get();
147 new_parameters.push_tail(ir->clone(ht));
148 }
149
150 return new(ctx) ir_call(this->callee, &new_parameters);
151 }
152
153 ir_expression *
154 ir_expression::clone(struct hash_table *ht) const
155 {
156 void *ctx = talloc_parent(this);
157 ir_rvalue *op[2] = {NULL, NULL};
158 unsigned int i;
159
160 for (i = 0; i < get_num_operands(); i++) {
161 op[i] = (ir_rvalue *)this->operands[i]->clone(ht);
162 }
163
164 return new(ctx) ir_expression(this->operation, this->type, op[0], op[1]);
165 }
166
167 ir_dereference_variable *
168 ir_dereference_variable::clone(struct hash_table *ht) const
169 {
170 void *ctx = talloc_parent(this);
171 ir_variable *new_var;
172
173 if (ht) {
174 new_var = (ir_variable *)hash_table_find(ht, this->var);
175 if (!new_var)
176 new_var = this->var;
177 } else {
178 new_var = this->var;
179 }
180
181 return new(ctx) ir_dereference_variable(new_var);
182 }
183
184 ir_dereference_array *
185 ir_dereference_array::clone(struct hash_table *ht) const
186 {
187 void *ctx = talloc_parent(this);
188 return new(ctx) ir_dereference_array((ir_rvalue *)this->array->clone(ht),
189 (ir_rvalue *)this->array_index->clone(ht));
190 }
191
192 ir_dereference_record *
193 ir_dereference_record::clone(struct hash_table *ht) const
194 {
195 void *ctx = talloc_parent(this);
196 return new(ctx) ir_dereference_record((ir_rvalue *)this->record->clone(ht),
197 this->field);
198 }
199
200 ir_texture *
201 ir_texture::clone(struct hash_table *ht) const
202 {
203 void *ctx = talloc_parent(this);
204 ir_texture *new_tex = new(ctx) ir_texture(this->op);
205
206 new_tex->sampler = (ir_dereference *)this->sampler->clone(ht);
207 new_tex->coordinate = (ir_rvalue *)this->coordinate->clone(ht);
208 if (this->projector)
209 new_tex->projector = (ir_rvalue *)this->projector->clone(ht);
210 if (this->shadow_comparitor) {
211 new_tex->shadow_comparitor =
212 (ir_rvalue *)this->shadow_comparitor->clone(ht);
213 }
214
215 for (int i = 0; i < 3; i++)
216 new_tex->offsets[i] = this->offsets[i];
217
218 switch (this->op) {
219 case ir_tex:
220 break;
221 case ir_txb:
222 new_tex->lod_info.bias = (ir_rvalue *)this->lod_info.bias->clone(ht);
223 break;
224 case ir_txl:
225 case ir_txf:
226 new_tex->lod_info.lod = (ir_rvalue *)this->lod_info.lod->clone(ht);
227 break;
228 case ir_txd:
229 new_tex->lod_info.grad.dPdx =
230 (ir_rvalue *)this->lod_info.grad.dPdx->clone(ht);
231 new_tex->lod_info.grad.dPdy =
232 (ir_rvalue *)this->lod_info.grad.dPdy->clone(ht);
233 break;
234 }
235
236 return new_tex;
237 }
238
239 ir_assignment *
240 ir_assignment::clone(struct hash_table *ht) const
241 {
242 ir_rvalue *new_condition = NULL;
243
244 if (this->condition)
245 new_condition = (ir_rvalue *)this->condition->clone(ht);
246
247 void *ctx = talloc_parent(this);
248 return new(ctx) ir_assignment((ir_rvalue *)this->lhs->clone(ht),
249 (ir_rvalue *)this->rhs->clone(ht),
250 new_condition);
251 }
252
253 ir_function *
254 ir_function::clone(struct hash_table *ht) const
255 {
256 (void)ht;
257 /* FINISHME */
258 abort();
259 }
260
261 ir_function_signature *
262 ir_function_signature::clone(struct hash_table *ht) const
263 {
264 (void)ht;
265 /* FINISHME */
266 abort();
267 }
268
269 ir_constant *
270 ir_constant::clone(struct hash_table *ht) const
271 {
272 void *ctx = talloc_parent(this);
273 (void)ht;
274
275 switch (this->type->base_type) {
276 case GLSL_TYPE_UINT:
277 case GLSL_TYPE_INT:
278 case GLSL_TYPE_FLOAT:
279 case GLSL_TYPE_BOOL:
280 return new(ctx) ir_constant(this->type, &this->value);
281
282 case GLSL_TYPE_STRUCT: {
283 ir_constant *c = new(ctx) ir_constant;
284
285 c->type = this->type;
286 for (exec_node *node = this->components.head
287 ; !node->is_tail_sentinal()
288 ; node = node->next) {
289 ir_constant *const orig = (ir_constant *) node;
290
291 c->components.push_tail(orig->clone(NULL));
292 }
293
294 return c;
295 }
296
297 default:
298 assert(!"Should not get here."); break;
299 return NULL;
300 }
301 }