]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/resolve.cpp
Use extended alignment in SPIR-V struct layout when necessary
[libs/gl.git] / source / glsl / resolve.cpp
1 #include <msp/core/algorithm.h>
2 #include <msp/core/raii.h>
3 #include <msp/strings/utils.h>
4 #include "reflect.h"
5 #include "resolve.h"
6
7 using namespace std;
8
9 namespace Msp {
10 namespace GL {
11 namespace SL {
12
13 void BlockHierarchyResolver::enter(Block &block)
14 {
15         r_any_resolved |= (current_block!=block.parent);
16         block.parent = current_block;
17 }
18
19
20 bool TypeResolver::apply(Stage &s)
21 {
22         stage = &s;
23         s.types.clear();
24         r_any_resolved = false;
25         s.content.visit(*this);
26         return r_any_resolved;
27 }
28
29 TypeDeclaration *TypeResolver::get_or_create_array_type(TypeDeclaration &type)
30 {
31         bool extended_alignment = iface_block;
32         auto key = make_pair(&type, extended_alignment);
33         auto i = array_types.find(key);
34         if(i!=array_types.end())
35                 return i->second;
36
37         BasicTypeDeclaration *array = new BasicTypeDeclaration;
38         array->source = INTERNAL_SOURCE;
39         array->name = type.name+"[]";
40         array->kind = BasicTypeDeclaration::ARRAY;
41         array->extended_alignment = extended_alignment;
42         array->base = type.name;
43         array->base_type = &type;
44         stage->content.body.insert(type_insert_point, array);
45         array_types[key] = array;
46         return array;
47 }
48
49 void TypeResolver::resolve_type(TypeDeclaration *&type, const string &name, bool array)
50 {
51         TypeDeclaration *resolved = 0;
52         auto i = stage->types.find(name);
53         if(i!=stage->types.end())
54         {
55                 auto j = alias_map.find(i->second);
56                 resolved = (j!=alias_map.end() ? j->second : i->second);
57         }
58
59         if(resolved && array)
60                 resolved = get_or_create_array_type(*resolved);
61
62         r_any_resolved |= (resolved!=type);
63         type=resolved;
64 }
65
66 void TypeResolver::visit(Block &block)
67 {
68         for(auto i=block.body.begin(); i!=block.body.end(); ++i)
69         {
70                 if(!block.parent)
71                         type_insert_point = i;
72                 (*i)->visit(*this);
73         }
74 }
75
76 void TypeResolver::visit(BasicTypeDeclaration &type)
77 {
78         resolve_type(type.base_type, type.base, false);
79
80         if(type.kind==BasicTypeDeclaration::VECTOR && type.base_type)
81                 if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
82                         if(basic_base->kind==BasicTypeDeclaration::VECTOR)
83                         {
84                                 type.kind = BasicTypeDeclaration::MATRIX;
85                                 /* A matrix's base type is its column vector type.  This will put
86                                 the column vector's size, i.e. the matrix's row count, in the high
87                                 half of the size. */
88                                 type.size |= basic_base->size<<16;
89                         }
90
91         if(type.kind==BasicTypeDeclaration::ALIAS && type.base_type)
92                 alias_map[&type] = type.base_type;
93         else if(type.kind==BasicTypeDeclaration::ARRAY && type.base_type)
94                 array_types[make_pair(type.base_type, type.extended_alignment)] = &type;
95
96         stage->types.insert(make_pair(type.name, &type));
97 }
98
99 void TypeResolver::visit(ImageTypeDeclaration &type)
100 {
101         resolve_type(type.base_type, type.base, false);
102         stage->types.insert(make_pair(type.name, &type));
103 }
104
105 void TypeResolver::visit(StructDeclaration &strct)
106 {
107         stage->types.insert(make_pair(strct.name, &strct));
108         SetForScope<InterfaceBlock *> set_iface(iface_block, strct.interface_block);
109         TraversingVisitor::visit(strct);
110 }
111
112 void TypeResolver::visit(VariableDeclaration &var)
113 {
114         resolve_type(var.type_declaration, var.type, var.array);
115         if(iface_block)
116         {
117                 if(var.interface==iface_block->interface)
118                         var.interface.clear();
119                 if(StructDeclaration *strct = dynamic_cast<StructDeclaration *>(var.type_declaration))
120                         strct->extended_alignment = true;
121         }
122 }
123
124 void TypeResolver::visit(InterfaceBlock &iface)
125 {
126         if(iface.members)
127         {
128                 SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
129                 iface.members->visit(*this);
130
131                 StructDeclaration *strct = new StructDeclaration;
132                 strct->source = INTERNAL_SOURCE;
133                 strct->name = format("_%s_%s", iface.interface, iface.block_name);
134                 strct->members.body.splice(strct->members.body.begin(), iface.members->body);
135                 strct->extended_alignment = true;
136                 stage->content.body.insert(type_insert_point, strct);
137                 stage->types.insert(make_pair(strct->name, strct));
138
139                 iface.members = 0;
140                 strct->interface_block = &iface;
141                 iface.struct_declaration = strct;
142         }
143
144         TypeDeclaration *type = iface.struct_declaration;
145         if(type && iface.array)
146                 type = get_or_create_array_type(*type);
147         r_any_resolved = (type!=iface.type_declaration);
148         iface.type_declaration = type;
149 }
150
151 void TypeResolver::visit(FunctionDeclaration &func)
152 {
153         resolve_type(func.return_type_declaration, func.return_type, false);
154         TraversingVisitor::visit(func);
155 }
156
157
158 bool VariableResolver::apply(Stage &s)
159 {
160         stage = &s;
161         s.interface_blocks.clear();
162         r_any_resolved = false;
163         s.content.visit(*this);
164         for(Statement *b: redeclared_builtins)
165                 b->source = GENERATED_SOURCE;
166         NodeRemover().apply(s, nodes_to_remove);
167         return r_any_resolved;
168 }
169
170 void VariableResolver::enter(Block &block)
171 {
172         block.variables.clear();
173 }
174
175 void VariableResolver::visit(RefPtr<Expression> &expr)
176 {
177         r_replacement_expr = 0;
178         expr->visit(*this);
179         if(r_replacement_expr)
180         {
181                 expr = r_replacement_expr;
182                 /* Don't record assignment target when doing a replacement, because chain
183                 information won't be correct. */
184                 r_assignment_target.declaration = 0;
185                 r_any_resolved = true;
186         }
187         r_replacement_expr = 0;
188 }
189
190 void VariableResolver::check_assignment_target(Statement *declaration)
191 {
192         if(record_target)
193         {
194                 if(r_assignment_target.declaration)
195                 {
196                         /* More than one reference found in assignment target.  Unable to
197                         determine what the primary target is. */
198                         record_target = false;
199                         r_assignment_target.declaration = 0;
200                 }
201                 else
202                         r_assignment_target.declaration = declaration;
203         }
204         // TODO This check is overly broad and may prevent some optimizations.
205         else if(declaration && declaration==r_assignment_target.declaration)
206                 r_self_referencing = true;
207 }
208
209 void VariableResolver::visit(VariableReference &var)
210 {
211         VariableDeclaration *declaration = 0;
212
213         /* Look for variable declarations in the block hierarchy first.  Interface
214         blocks are always defined in the top level so we can't accidentally skip
215         one. */
216         for(Block *block=current_block; (!declaration && block); block=block->parent)
217         {
218                 auto i = block->variables.find(var.name);
219                 if(i!=block->variables.end())
220                         declaration = i->second;
221         }
222
223         if(!declaration)
224         {
225                 const map<string, InterfaceBlock *> &blocks = stage->interface_blocks;
226                 auto i = blocks.find(var.name);
227                 if(i==blocks.end())
228                 {
229                         // Look for the variable in anonymous interface blocks.
230                         for(i=blocks.begin(); i!=blocks.end(); ++i)
231                                 if(i->second->instance_name.empty() && i->second->struct_declaration)
232                                         if(i->second->struct_declaration->members.variables.count(var.name))
233                                                 break;
234                 }
235
236                 if(i!=blocks.end())
237                 {
238                         /* The name refers to either an interface block with an instance name
239                         or a variable declared inside an anonymous interface block.  Prepare
240                         new syntax tree nodes accordingly. */
241                         InterfaceBlockReference *iface_ref = new InterfaceBlockReference;
242                         iface_ref->source = var.source;
243                         iface_ref->line = var.line;
244                         iface_ref->declaration = i->second;
245
246                         if(i->second->instance_name.empty())
247                         {
248                                 iface_ref->name = format("%s %s", i->second->interface, i->second->block_name);
249
250                                 MemberAccess *memacc = new MemberAccess;
251                                 memacc->source = var.source;
252                                 memacc->line = var.line;
253                                 memacc->left = iface_ref;
254                                 memacc->member = var.name;
255
256                                 r_replacement_expr = memacc;
257                         }
258                         else
259                         {
260                                 iface_ref->name = var.name;
261                                 r_replacement_expr = iface_ref;
262                         }
263                 }
264         }
265
266         r_any_resolved |= (declaration!=var.declaration);
267         var.declaration = declaration;
268
269         check_assignment_target(var.declaration);
270 }
271
272 void VariableResolver::visit(InterfaceBlockReference &iface)
273 {
274         auto i = stage->interface_blocks.find(iface.name);
275         InterfaceBlock *declaration = (i!=stage->interface_blocks.end() ? i->second : 0);
276         r_any_resolved |= (declaration!=iface.declaration);
277         iface.declaration = declaration;
278
279         check_assignment_target(iface.declaration);
280 }
281
282 void VariableResolver::visit(MemberAccess &memacc)
283 {
284         TraversingVisitor::visit(memacc);
285
286         VariableDeclaration *declaration = 0;
287         int index = -1;
288         if(StructDeclaration *strct = dynamic_cast<StructDeclaration *>(memacc.left->type))
289         {
290                 auto i = strct->members.variables.find(memacc.member);
291                 if(i!=strct->members.variables.end())
292                 {
293                         declaration = i->second;
294                         index = 0;
295                         for(auto j=strct->members.body.begin(); (j!=strct->members.body.end() && j->get()!=i->second); ++j)
296                                 ++index;
297
298                         if(record_target)
299                                 add_to_chain(r_assignment_target, Assignment::Target::MEMBER, index);
300                 }
301         }
302         else if(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(memacc.left->type))
303         {
304                 bool scalar_swizzle = ((basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT) && memacc.member.size()==1);
305                 bool vector_swizzle = (basic->kind==BasicTypeDeclaration::VECTOR && memacc.member.size()<=4);
306                 if(scalar_swizzle || vector_swizzle)
307                 {
308                         static const char component_names[] = { 'x', 'r', 's', 'y', 'g', 't', 'z', 'b', 'p', 'w', 'a', 'q' };
309
310                         bool ok = true;
311                         uint8_t components[4] = { };
312                         for(unsigned i=0; (ok && i<memacc.member.size()); ++i)
313                                 ok = ((components[i] = (std::find(component_names, component_names+12, memacc.member[i])-component_names)/3) < 4);
314
315                         if(ok)
316                         {
317                                 Swizzle *swizzle = new Swizzle;
318                                 swizzle->source = memacc.source;
319                                 swizzle->line = memacc.line;
320                                 swizzle->oper = memacc.oper;
321                                 swizzle->left = memacc.left;
322                                 swizzle->component_group = memacc.member;
323                                 swizzle->count = memacc.member.size();
324                                 copy(components, components+memacc.member.size(), swizzle->components);
325                                 r_replacement_expr = swizzle;
326                         }
327                 }
328         }
329
330         r_any_resolved |= (declaration!=memacc.declaration || index!=memacc.index);
331         memacc.declaration = declaration;
332         memacc.index = index;
333 }
334
335 void VariableResolver::visit(Swizzle &swizzle)
336 {
337         TraversingVisitor::visit(swizzle);
338
339         if(record_target)
340         {
341                 unsigned mask = 0;
342                 for(unsigned i=0; i<swizzle.count; ++i)
343                         mask |= 1<<swizzle.components[i];
344                 add_to_chain(r_assignment_target, Assignment::Target::SWIZZLE, mask);
345         }
346 }
347
348 void VariableResolver::visit(BinaryExpression &binary)
349 {
350         if(binary.oper->token[0]=='[')
351         {
352                 {
353                         /* The subscript expression is not a part of the primary assignment
354                         target. */
355                         SetFlag set(record_target, false);
356                         visit(binary.right);
357                 }
358                 visit(binary.left);
359
360                 if(record_target)
361                 {
362                         unsigned index = 0x3F;
363                         if(Literal *literal_subscript = dynamic_cast<Literal *>(binary.right.get()))
364                                 if(literal_subscript->value.check_type<int>())
365                                         index = literal_subscript->value.value<int>();
366                         add_to_chain(r_assignment_target, Assignment::Target::ARRAY, index);
367                 }
368         }
369         else
370                 TraversingVisitor::visit(binary);
371 }
372
373 void VariableResolver::visit(Assignment &assign)
374 {
375         {
376                 SetFlag set(record_target);
377                 r_assignment_target = Assignment::Target();
378                 visit(assign.left);
379                 r_any_resolved |= (r_assignment_target<assign.target || assign.target<r_assignment_target);
380                 assign.target = r_assignment_target;
381         }
382
383         r_self_referencing = false;
384         visit(assign.right);
385         assign.self_referencing = (r_self_referencing || assign.oper->token[0]!='=');
386 }
387
388 void VariableResolver::merge_layouts(Layout &to_layout, const Layout &from_layout)
389 {
390         for(const Layout::Qualifier &q: from_layout.qualifiers)
391         {
392                 auto i = find_member(to_layout.qualifiers, q.name, &Layout::Qualifier::name);
393                 if(i!=to_layout.qualifiers.end())
394                 {
395                         i->has_value = q.value;
396                         i->value = q.value;
397                 }
398                 else
399                         to_layout.qualifiers.push_back(q);
400         }
401 }
402
403 void VariableResolver::redeclare_builtin(VariableDeclaration &existing, VariableDeclaration &var)
404 {
405         if(var.layout)
406         {
407                 if(existing.layout)
408                         merge_layouts(*existing.layout, *var.layout);
409                 else
410                         existing.layout = var.layout;
411         }
412         if(var.array_size)
413                 existing.array_size = var.array_size;
414
415         redeclared_builtins.push_back(&existing);
416 }
417
418 void VariableResolver::visit(VariableDeclaration &var)
419 {
420         TraversingVisitor::visit(var);
421
422         auto i = current_block->variables.find(var.name);
423         VariableDeclaration *existing = 0;
424         InterfaceBlock *block = 0;
425         if(i!=current_block->variables.end())
426                 existing = i->second;
427         else if(!current_block->parent)
428         {
429                 const map<string, InterfaceBlock *> &blocks = stage->interface_blocks;
430                 for(auto j=blocks.begin(); j!=blocks.end(); ++j)
431                         if(j->second->instance_name.empty() && j->second->struct_declaration)
432                         {
433                                 map<string, VariableDeclaration *> &block_vars = j->second->struct_declaration->members.variables;
434                                 i = block_vars.find(var.name);
435                                 if(i!=block_vars.end())
436                                 {
437                                         existing = i->second;
438                                         block = j->second;
439                                         break;
440                                 }
441                         }
442         }
443
444         if(!existing)
445                 current_block->variables.insert(make_pair(var.name, &var));
446         else if(!current_block->parent && (block ? block->interface : existing->interface)==var.interface && existing->type==var.type && existing->array==var.array)
447         {
448                 if(existing->source==BUILTIN_SOURCE)
449                 {
450                         redeclare_builtin(*existing, var);
451
452                         if(block)
453                         {
454                                 redeclared_builtins.push_back(block);
455                                 for(const auto &kvp: block->struct_declaration->members.variables)
456                                         redeclared_builtins.push_back(kvp.second);
457                         }
458
459                         nodes_to_remove.insert(&var);
460                         r_any_resolved = true;
461                 }
462                 else if(existing->array && !existing->array_size && !var.layout && !var.init_expression)
463                 {
464                         existing->array_size = var.array_size;
465                         nodes_to_remove.insert(&var);
466                         r_any_resolved = true;
467                 }
468         }
469 }
470
471 void VariableResolver::visit(InterfaceBlock &iface)
472 {
473         string key = format("%s %s", iface.interface, iface.block_name);
474         auto i = stage->interface_blocks.find(key);
475         if(i!=stage->interface_blocks.end())
476         {
477                 if(i->second->source==BUILTIN_SOURCE && iface.struct_declaration && i->second->struct_declaration)
478                 {
479                         const map<string, VariableDeclaration *> &vars = iface.struct_declaration->members.variables;
480                         const map<string, VariableDeclaration *> &existing_vars = i->second->struct_declaration->members.variables;
481
482                         bool found_all = true;
483                         for(const auto &kvp: vars)
484                         {
485                                 auto j = existing_vars.find(kvp.first);
486                                 if(j!=existing_vars.end() && j->second->type==kvp.second->type && j->second->array==kvp.second->array)
487                                         redeclare_builtin(*j->second, *kvp.second);
488                                 else
489                                         found_all = false;
490                         }
491
492                         if(found_all)
493                         {
494                                 redeclared_builtins.push_back(i->second);
495                                 nodes_to_remove.insert(&iface);
496                                 nodes_to_remove.insert(iface.struct_declaration);
497                         }
498                 }
499         }
500         else
501         {
502                 /* Block names can be reused in different interfaces.  Prepend the interface
503                 to the name to avoid conflicts. */
504                 stage->interface_blocks.insert(make_pair(key, &iface));
505                 if(!iface.instance_name.empty())
506                         stage->interface_blocks.insert(make_pair(iface.instance_name, &iface));
507         }
508
509         TraversingVisitor::visit(iface);
510 }
511
512
513 bool ExpressionResolver::apply(Stage &s)
514 {
515         stage = &s;
516         r_any_resolved = false;
517         s.content.visit(*this);
518         return r_any_resolved;
519 }
520
521 ExpressionResolver::Compatibility ExpressionResolver::get_compatibility(BasicTypeDeclaration &left, BasicTypeDeclaration &right)
522 {
523         if(&left==&right)
524                 return SAME_TYPE;
525         else if(can_convert(left, right))
526                 return LEFT_CONVERTIBLE;
527         else if(can_convert(right, left))
528                 return RIGHT_CONVERTIBLE;
529         else
530                 return NOT_COMPATIBLE;
531 }
532
533 BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration::Kind kind, unsigned size, bool sign)
534 {
535         auto i = find_if(basic_types,
536                 [kind, size, sign](const BasicTypeDeclaration *t){ return t->kind==kind && t->size==size && t->sign==sign; });
537         return (i!=basic_types.end() ? *i : 0);
538 }
539
540 BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration &elem_type, BasicTypeDeclaration::Kind kind, unsigned size)
541 {
542         auto i = find_if(basic_types,
543                 [&elem_type, kind, size](BasicTypeDeclaration *t){ return get_element_type(*t)==&elem_type && t->kind==kind && t->size==size; });
544         return (i!=basic_types.end() ? *i : 0);
545 }
546
547 void ExpressionResolver::convert_to(RefPtr<Expression> &expr, BasicTypeDeclaration &type)
548 {
549         RefPtr<FunctionCall> call = new FunctionCall;
550         call->name = type.name;
551         call->constructor = true;
552         call->arguments.push_back_nocopy(expr);
553         call->type = &type;
554         expr = call;
555 }
556
557 bool ExpressionResolver::convert_to_element(RefPtr<Expression> &expr, BasicTypeDeclaration &elem_type)
558 {
559         if(BasicTypeDeclaration *expr_basic = dynamic_cast<BasicTypeDeclaration *>(expr->type))
560         {
561                 BasicTypeDeclaration *to_type = &elem_type;
562                 if(is_vector_or_matrix(*expr_basic))
563                         to_type = find_type(elem_type, expr_basic->kind, expr_basic->size);
564                 if(to_type)
565                 {
566                         convert_to(expr, *to_type);
567                         return true;
568                 }
569         }
570
571         return false;
572 }
573
574 bool ExpressionResolver::truncate_vector(RefPtr<Expression> &expr, unsigned size)
575 {
576         if(BasicTypeDeclaration *expr_basic = dynamic_cast<BasicTypeDeclaration *>(expr->type))
577                 if(BasicTypeDeclaration *expr_elem = get_element_type(*expr_basic))
578                 {
579                         RefPtr<Swizzle> swizzle = new Swizzle;
580                         swizzle->left = expr;
581                         swizzle->oper = &Operator::get_operator(".", Operator::POSTFIX);
582                         swizzle->component_group = string("xyzw", size);
583                         swizzle->count = size;
584                         for(unsigned i=0; i<size; ++i)
585                                 swizzle->components[i] = i;
586                         if(size==1)
587                                 swizzle->type = expr_elem;
588                         else
589                                 swizzle->type = find_type(*expr_elem, BasicTypeDeclaration::VECTOR, size);
590                         expr = swizzle;
591
592                         return true;
593                 }
594
595         return false;
596 }
597
598 void ExpressionResolver::resolve(Expression &expr, TypeDeclaration *type, bool lvalue)
599 {
600         r_any_resolved |= (type!=expr.type || lvalue!=expr.lvalue);
601         expr.type = type;
602         expr.lvalue = lvalue;
603 }
604
605 void ExpressionResolver::visit(Block &block)
606 {
607         SetForScope<Block *> set_block(current_block, &block);
608         for(auto i=block.body.begin(); i!=block.body.end(); ++i)
609         {
610                 insert_point = i;
611                 (*i)->visit(*this);
612         }
613 }
614
615 void ExpressionResolver::visit(Literal &literal)
616 {
617         if(literal.value.check_type<bool>())
618                 resolve(literal, find_type(BasicTypeDeclaration::BOOL, 1), false);
619         else if(literal.value.check_type<int>())
620                 resolve(literal, find_type(BasicTypeDeclaration::INT, 32, true), false);
621         else if(literal.value.check_type<unsigned>())
622                 resolve(literal, find_type(BasicTypeDeclaration::INT, 32, false), false);
623         else if(literal.value.check_type<float>())
624                 resolve(literal, find_type(BasicTypeDeclaration::FLOAT, 32), false);
625 }
626
627 void ExpressionResolver::visit(VariableReference &var)
628 {
629         if(var.declaration)
630                 resolve(var, var.declaration->type_declaration, true);
631 }
632
633 void ExpressionResolver::visit(InterfaceBlockReference &iface)
634 {
635         if(iface.declaration)
636                 resolve(iface, iface.declaration->type_declaration, true);
637 }
638
639 void ExpressionResolver::visit(MemberAccess &memacc)
640 {
641         TraversingVisitor::visit(memacc);
642
643         if(memacc.declaration)
644                 resolve(memacc, memacc.declaration->type_declaration, memacc.left->lvalue);
645 }
646
647 void ExpressionResolver::visit(Swizzle &swizzle)
648 {
649         TraversingVisitor::visit(swizzle);
650
651         if(BasicTypeDeclaration *left_basic = dynamic_cast<BasicTypeDeclaration *>(swizzle.left->type))
652         {
653                 BasicTypeDeclaration *left_elem = get_element_type(*left_basic);
654                 if(swizzle.count==1)
655                         resolve(swizzle, left_elem, swizzle.left->lvalue);
656                 else if(left_basic->kind==BasicTypeDeclaration::VECTOR && left_elem)
657                         resolve(swizzle, find_type(*left_elem, left_basic->kind, swizzle.count), swizzle.left->lvalue);
658         }
659 }
660
661 void ExpressionResolver::visit(UnaryExpression &unary)
662 {
663         TraversingVisitor::visit(unary);
664
665         BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(unary.expression->type);
666         if(!basic)
667                 return;
668
669         char oper = unary.oper->token[0];
670         if(oper=='!')
671         {
672                 if(basic->kind!=BasicTypeDeclaration::BOOL)
673                         return;
674         }
675         else if(oper=='~')
676         {
677                 if(basic->kind!=BasicTypeDeclaration::INT)
678                         return;
679         }
680         else if(oper=='+' || oper=='-')
681         {
682                 BasicTypeDeclaration *elem = get_element_type(*basic);
683                 if(!elem || !is_scalar(*elem))
684                         return;
685         }
686         resolve(unary, basic, unary.expression->lvalue);
687 }
688
689 void ExpressionResolver::visit(BinaryExpression &binary, bool assign)
690 {
691         /* Binary operators are only defined for basic types (not for image or
692         structure types). */
693         BasicTypeDeclaration *basic_left = dynamic_cast<BasicTypeDeclaration *>(binary.left->type);
694         BasicTypeDeclaration *basic_right = dynamic_cast<BasicTypeDeclaration *>(binary.right->type);
695         if(!basic_left || !basic_right)
696                 return;
697
698         char oper = binary.oper->token[0];
699         if(oper=='[')
700         {
701                 /* Subscripting operates on vectors, matrices and arrays, and the right
702                 operand must be an integer. */
703                 if((!is_vector_or_matrix(*basic_left) && basic_left->kind!=BasicTypeDeclaration::ARRAY) || basic_right->kind!=BasicTypeDeclaration::INT)
704                         return;
705
706                 resolve(binary, basic_left->base_type, binary.left->lvalue);
707                 return;
708         }
709         else if(basic_left->kind==BasicTypeDeclaration::ARRAY || basic_right->kind==BasicTypeDeclaration::ARRAY)
710                 // No other binary operator can be used with arrays.
711                 return;
712
713         BasicTypeDeclaration *elem_left = get_element_type(*basic_left);
714         BasicTypeDeclaration *elem_right = get_element_type(*basic_right);
715         if(!elem_left || !elem_right)
716                 return;
717
718         Compatibility compat = get_compatibility(*basic_left, *basic_right);
719         Compatibility elem_compat = get_compatibility(*elem_left, *elem_right);
720         if(elem_compat==NOT_COMPATIBLE)
721                 return;
722         if(assign && (compat==LEFT_CONVERTIBLE || elem_compat==LEFT_CONVERTIBLE))
723                 return;
724
725         TypeDeclaration *type = 0;
726         char oper2 = binary.oper->token[1];
727         if((oper=='<' && oper2!='<') || (oper=='>' && oper2!='>'))
728         {
729                 /* Relational operators compare two scalar integer or floating-point
730                 values. */
731                 if(!is_scalar(*elem_left) || !is_scalar(*elem_right) || compat==NOT_COMPATIBLE)
732                         return;
733
734                 type = find_type(BasicTypeDeclaration::BOOL, 1);
735         }
736         else if((oper=='=' || oper=='!') && oper2=='=')
737         {
738                 // Equality comparison can be done on any compatible types.
739                 if(compat==NOT_COMPATIBLE)
740                         return;
741
742                 type = find_type(BasicTypeDeclaration::BOOL, 1);
743         }
744         else if(oper2=='&' || oper2=='|' || oper2=='^')
745         {
746                 // Logical operators can only be applied to booleans.
747                 if(basic_left->kind!=BasicTypeDeclaration::BOOL || basic_right->kind!=BasicTypeDeclaration::BOOL)
748                         return;
749
750                 type = basic_left;
751         }
752         else if((oper=='&' || oper=='|' || oper=='^' || oper=='%') && !oper2)
753         {
754                 // Bitwise operators and modulo can only be applied to integers.
755                 if(basic_left->kind!=BasicTypeDeclaration::INT || basic_right->kind!=BasicTypeDeclaration::INT)
756                         return;
757
758                 type = (compat==LEFT_CONVERTIBLE ? basic_right : basic_left);
759         }
760         else if((oper=='<' || oper=='>') && oper2==oper)
761         {
762                 // Shifts apply to integer scalars and vectors, with some restrictions.
763                 if(elem_left->kind!=BasicTypeDeclaration::INT || elem_right->kind!=BasicTypeDeclaration::INT)
764                         return;
765                 unsigned left_size = (basic_left->kind==BasicTypeDeclaration::INT ? 1 : basic_left->kind==BasicTypeDeclaration::VECTOR ? basic_left->size : 0);
766                 unsigned right_size = (basic_right->kind==BasicTypeDeclaration::INT ? 1 : basic_right->kind==BasicTypeDeclaration::VECTOR ? basic_right->size : 0);
767                 if(!left_size || (left_size==1 && right_size!=1) || (left_size>1 && right_size!=1 && right_size!=left_size))
768                         return;
769
770                 /* If the left operand is a vector and right is scalar, convert the right
771                 operand to a vector too. */
772                 if(left_size>1 && right_size==1)
773                 {
774                         BasicTypeDeclaration *vec_right = find_type(*elem_right, basic_left->kind, basic_left->size);
775                         if(!vec_right)
776                                 return;
777
778                         convert_to(binary.right, *vec_right);
779                 }
780
781                 type = basic_left;
782                 // Don't perform conversion even if the operands are of different sizes.
783                 compat = SAME_TYPE;
784         }
785         else if(oper=='+' || oper=='-' || oper=='*' || oper=='/')
786         {
787                 // Arithmetic operators require scalar elements.
788                 if(!is_scalar(*elem_left) || !is_scalar(*elem_right))
789                         return;
790
791                 if(oper=='*' && is_vector_or_matrix(*basic_left) && is_vector_or_matrix(*basic_right) &&
792                         (basic_left->kind==BasicTypeDeclaration::MATRIX || basic_right->kind==BasicTypeDeclaration::MATRIX))
793                 {
794                         /* Multiplication has special rules when at least one operand is a
795                         matrix and the other is a vector or a matrix. */
796                         unsigned left_columns = basic_left->size&0xFFFF;
797                         unsigned right_rows = basic_right->size;
798                         if(basic_right->kind==BasicTypeDeclaration::MATRIX)
799                                 right_rows >>= 16;
800                         if(left_columns!=right_rows)
801                                 return;
802
803                         BasicTypeDeclaration *elem_result = (elem_compat==LEFT_CONVERTIBLE ? elem_right : elem_left);
804
805                         if(basic_left->kind==BasicTypeDeclaration::VECTOR)
806                                 type = find_type(*elem_result, BasicTypeDeclaration::VECTOR, basic_right->size&0xFFFF);
807                         else if(basic_right->kind==BasicTypeDeclaration::VECTOR)
808                                 type = find_type(*elem_result, BasicTypeDeclaration::VECTOR, basic_left->size>>16);
809                         else
810                                 type = find_type(*elem_result, BasicTypeDeclaration::MATRIX, (basic_left->size&0xFFFF0000)|(basic_right->size&0xFFFF));
811                 }
812                 else if(compat==NOT_COMPATIBLE)
813                 {
814                         // Arithmetic between scalars and matrices or vectors is supported.
815                         if(is_scalar(*basic_left) && is_vector_or_matrix(*basic_right))
816                                 type = (elem_compat==RIGHT_CONVERTIBLE ? find_type(*elem_left, basic_right->kind, basic_right->size) : basic_right);
817                         else if(is_vector_or_matrix(*basic_left) && is_scalar(*basic_right))
818                                 type = (elem_compat==LEFT_CONVERTIBLE ? find_type(*elem_right, basic_left->kind, basic_left->size) : basic_left);
819                         else
820                                 return;
821                 }
822                 else if(compat==LEFT_CONVERTIBLE)
823                         type = basic_right;
824                 else
825                         type = basic_left;
826         }
827         else
828                 return;
829
830         if(assign && type!=basic_left)
831                 return;
832
833         bool converted = true;
834         if(compat==LEFT_CONVERTIBLE)
835                 convert_to(binary.left, *basic_right);
836         else if(compat==RIGHT_CONVERTIBLE)
837                 convert_to(binary.right, *basic_left);
838         else if(elem_compat==LEFT_CONVERTIBLE)
839                 converted = convert_to_element(binary.left, *elem_right);
840         else if(elem_compat==RIGHT_CONVERTIBLE)
841                 converted = convert_to_element(binary.right, *elem_left);
842
843         if(!converted)
844                 type = 0;
845
846         resolve(binary, type, assign);
847 }
848
849 void ExpressionResolver::visit(BinaryExpression &binary)
850 {
851         TraversingVisitor::visit(binary);
852         visit(binary, false);
853 }
854
855 void ExpressionResolver::visit(Assignment &assign)
856 {
857         TraversingVisitor::visit(assign);
858
859         if(assign.oper->token[0]!='=')
860                 return visit(assign, true);
861         else if(assign.left->type!=assign.right->type)
862         {
863                 BasicTypeDeclaration *basic_left = dynamic_cast<BasicTypeDeclaration *>(assign.left->type);
864                 BasicTypeDeclaration *basic_right = dynamic_cast<BasicTypeDeclaration *>(assign.right->type);
865                 if(!basic_left || !basic_right)
866                         return;
867
868                 Compatibility compat = get_compatibility(*basic_left, *basic_right);
869                 if(compat==RIGHT_CONVERTIBLE)
870                         convert_to(assign.right, *basic_left);
871                 else if(compat!=SAME_TYPE)
872                         return;
873         }
874
875         resolve(assign, assign.left->type, true);
876 }
877
878 void ExpressionResolver::visit(TernaryExpression &ternary)
879 {
880         TraversingVisitor::visit(ternary);
881
882         BasicTypeDeclaration *basic_cond = dynamic_cast<BasicTypeDeclaration *>(ternary.condition->type);
883         if(!basic_cond || basic_cond->kind!=BasicTypeDeclaration::BOOL)
884                 return;
885
886         TypeDeclaration *type = 0;
887         if(ternary.true_expr->type==ternary.false_expr->type)
888                 type = ternary.true_expr->type;
889         else
890         {
891                 BasicTypeDeclaration *basic_true = dynamic_cast<BasicTypeDeclaration *>(ternary.true_expr->type);
892                 BasicTypeDeclaration *basic_false = dynamic_cast<BasicTypeDeclaration *>(ternary.false_expr->type);
893                 if(!basic_true || !basic_false)
894                         return;
895
896                 Compatibility compat = get_compatibility(*basic_true, *basic_false);
897                 if(compat==NOT_COMPATIBLE)
898                         return;
899
900                 type = (compat==LEFT_CONVERTIBLE ? basic_true : basic_false);
901
902                 if(compat==LEFT_CONVERTIBLE)
903                         convert_to(ternary.true_expr, *basic_false);
904                 else if(compat==RIGHT_CONVERTIBLE)
905                         convert_to(ternary.false_expr, *basic_true);
906         }
907
908         resolve(ternary, type, false);
909 }
910
911 void ExpressionResolver::visit_constructor(FunctionCall &call)
912 {
913         if(call.arguments.empty())
914                 return;
915
916         auto i = stage->types.find(call.name);
917         if(i==stage->types.end())
918                 return;
919         else if(call.arguments.size()==1 && i->second==call.arguments[0]->type)
920                 ;
921         else if(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(i->second))
922         {
923                 BasicTypeDeclaration *elem = get_element_type(*basic);
924                 if(!elem)
925                         return;
926
927                 vector<ArgumentInfo> args;
928                 args.reserve(call.arguments.size());
929                 unsigned arg_component_total = 0;
930                 bool has_matrices = false;
931                 for(const RefPtr<Expression> &a: call.arguments)
932                 {
933                         ArgumentInfo info;
934                         if(!(info.type=dynamic_cast<BasicTypeDeclaration *>(a->type)))
935                                 return;
936                         if(is_scalar(*info.type) || info.type->kind==BasicTypeDeclaration::BOOL)
937                                 info.component_count = 1;
938                         else if(info.type->kind==BasicTypeDeclaration::VECTOR)
939                                 info.component_count = info.type->size;
940                         else if(info.type->kind==BasicTypeDeclaration::MATRIX)
941                         {
942                                 info.component_count = (info.type->size>>16)*(info.type->size&0xFFFF);
943                                 has_matrices = true;
944                         }
945                         else
946                                 return;
947                         arg_component_total += info.component_count;
948                         args.push_back(info);
949                 }
950
951                 bool convert_args = false;
952                 if((is_scalar(*basic) || basic->kind==BasicTypeDeclaration::BOOL) && call.arguments.size()==1 && !has_matrices)
953                 {
954                         if(arg_component_total>1)
955                                 truncate_vector(call.arguments.front(), 1);
956
957                         /* Single-element type constructors never need to convert their
958                         arguments because the constructor *is* the conversion. */
959                 }
960                 else if(basic->kind==BasicTypeDeclaration::VECTOR && !has_matrices)
961                 {
962                         /* Vector constructors need either a single scalar argument or
963                         enough components to fill out the vector. */
964                         if(arg_component_total!=1 && arg_component_total<basic->size)
965                                 return;
966
967                         /* A vector of same size can be converted directly.  For other
968                         combinations the individual arguments need to be converted. */
969                         if(call.arguments.size()==1)
970                         {
971                                 if(arg_component_total==1)
972                                         convert_args = true;
973                                 else if(arg_component_total>basic->size)
974                                         truncate_vector(call.arguments.front(), basic->size);
975                         }
976                         else if(arg_component_total==basic->size)
977                                 convert_args = true;
978                         else
979                                 return;
980                 }
981                 else if(basic->kind==BasicTypeDeclaration::MATRIX)
982                 {
983                         unsigned column_count = basic->size&0xFFFF;
984                         unsigned row_count = basic->size>>16;
985
986                         vector<RefPtr<Expression> > columns;
987                         columns.reserve(column_count);
988                         bool changed_columns = false;
989
990                         if(call.arguments.size()==1)
991                         {
992                                 /* A matrix can be constructed from a single element or another
993                                 matrix of sufficient size. */
994                                 if(arg_component_total==1)
995                                         convert_args = true;
996                                 else if(args.front().type->kind==BasicTypeDeclaration::MATRIX)
997                                 {
998                                         unsigned arg_columns = args.front().type->size&0xFFFF;
999                                         unsigned arg_rows = args.front().type->size>>16;
1000                                         if(arg_columns<column_count || arg_rows<row_count)
1001                                                 return;
1002
1003                                         /* Always generate a temporary here and let the optimization
1004                                         stage inline it if that's reasonable. */
1005                                         RefPtr<VariableDeclaration> temporary = new VariableDeclaration;
1006                                         temporary->type = args.front().type->name;
1007                                         temporary->name = get_unused_variable_name(*current_block, "_temp");
1008                                         temporary->init_expression = call.arguments.front();
1009                                         current_block->body.insert(insert_point, temporary);
1010
1011                                         // Create expressions to build each column.
1012                                         for(unsigned j=0; j<column_count; ++j)
1013                                         {
1014                                                 RefPtr<VariableReference> ref = new VariableReference;
1015                                                 ref->name = temporary->name;
1016
1017                                                 RefPtr<Literal> index = new Literal;
1018                                                 index->token = lexical_cast<string>(j);
1019                                                 index->value = static_cast<int>(j);
1020
1021                                                 RefPtr<BinaryExpression> subscript = new BinaryExpression;
1022                                                 subscript->left = ref;
1023                                                 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
1024                                                 subscript->right = index;
1025                                                 subscript->type = args.front().type->base_type;
1026
1027                                                 columns.push_back(subscript);
1028                                                 if(arg_rows>row_count)
1029                                                         truncate_vector(columns.back(), row_count);
1030                                         }
1031
1032                                         changed_columns = true;
1033                                 }
1034                                 else
1035                                         return;
1036                         }
1037                         else if(arg_component_total==column_count*row_count && !has_matrices)
1038                         {
1039                                 /* Construct a matrix from individual components in column-major
1040                                 order.  Arguments must align at column boundaries. */
1041                                 vector<RefPtr<Expression> > column_args;
1042                                 column_args.reserve(row_count);
1043                                 unsigned column_component_count = 0;
1044
1045                                 for(unsigned j=0; j<call.arguments.size(); ++j)
1046                                 {
1047                                         const ArgumentInfo &info = args[j];
1048                                         if(!column_component_count && info.type->kind==BasicTypeDeclaration::VECTOR && info.component_count==row_count)
1049                                                 // A vector filling the entire column can be used as is.
1050                                                 columns.push_back(call.arguments[j]);
1051                                         else
1052                                         {
1053                                                 column_args.push_back(call.arguments[j]);
1054                                                 column_component_count += info.component_count;
1055                                                 if(column_component_count==row_count)
1056                                                 {
1057                                                         /* The column has filled up.  Create a vector constructor
1058                                                         for it.*/
1059                                                         RefPtr<FunctionCall> column_call = new FunctionCall;
1060                                                         column_call->name = basic->base_type->name;
1061                                                         column_call->constructor = true;
1062                                                         column_call->arguments.resize(column_args.size());
1063                                                         copy(column_args.begin(), column_args.end(), column_call->arguments.begin());
1064                                                         column_call->type = basic->base_type;
1065                                                         visit_constructor(*column_call);
1066                                                         columns.push_back(column_call);
1067
1068                                                         column_args.clear();
1069                                                         column_component_count = 0;
1070                                                 }
1071                                                 else if(column_component_count>row_count)
1072                                                         // Argument alignment mismatch.
1073                                                         return;
1074
1075                                                 changed_columns = true;
1076                                         }
1077                                 }
1078                         }
1079                         else
1080                                 return;
1081
1082                         if(changed_columns)
1083                         {
1084                                 call.arguments.resize(column_count);
1085                                 copy(columns.begin(), columns.end(), call.arguments.begin());
1086
1087                                 /* Let VariableResolver process the new nodes and finish
1088                                 resolving the constructor on the next pass. */
1089                                 r_any_resolved = true;
1090                                 return;
1091                         }
1092                 }
1093                 else
1094                         return;
1095
1096                 if(convert_args)
1097                 {
1098                         // The argument list may have changed so can't rely on args.
1099                         for(RefPtr<Expression> &a: call.arguments)
1100                                 if(BasicTypeDeclaration *basic_arg = dynamic_cast<BasicTypeDeclaration *>(a->type))
1101                                 {
1102                                         BasicTypeDeclaration *elem_arg = get_element_type(*basic_arg);
1103                                         if(elem_arg!=elem)
1104                                                 convert_to_element(a, *elem);
1105                                 }
1106                 }
1107         }
1108         else if(StructDeclaration *strct = dynamic_cast<StructDeclaration *>(i->second))
1109         {
1110                 if(call.arguments.size()!=strct->members.body.size())
1111                         return;
1112
1113                 auto j = call.arguments.begin();
1114                 for(const RefPtr<Statement> &s: strct->members.body)
1115                 {
1116                         if(VariableDeclaration *var = dynamic_cast<VariableDeclaration *>(s.get()))
1117                         {
1118                                 if(!(*j)->type || (*j)->type!=var->type_declaration)
1119                                         return;
1120                         }
1121                         else
1122                                 return;
1123                         ++j;
1124                 }
1125         }
1126
1127         resolve(call, i->second, false);
1128 }
1129
1130 void ExpressionResolver::visit(FunctionCall &call)
1131 {
1132         TraversingVisitor::visit(call);
1133
1134         if(call.declaration)
1135                 resolve(call, call.declaration->return_type_declaration, false);
1136         else if(call.constructor)
1137                 visit_constructor(call);
1138 }
1139
1140 void ExpressionResolver::visit(BasicTypeDeclaration &type)
1141 {
1142         basic_types.push_back(&type);
1143 }
1144
1145 void ExpressionResolver::visit(VariableDeclaration &var)
1146 {
1147         TraversingVisitor::visit(var);
1148         if(!var.init_expression)
1149                 return;
1150
1151         BasicTypeDeclaration *var_basic = dynamic_cast<BasicTypeDeclaration *>(var.type_declaration);
1152         BasicTypeDeclaration *init_basic = dynamic_cast<BasicTypeDeclaration *>(var.init_expression->type);
1153         if(!var_basic || !init_basic)
1154                 return;
1155
1156         Compatibility compat = get_compatibility(*var_basic, *init_basic);
1157         if(compat==RIGHT_CONVERTIBLE)
1158                 convert_to(var.init_expression, *var_basic);
1159 }
1160
1161 void ExpressionResolver::visit(FunctionDeclaration &func)
1162 {
1163         SetForScope<const FunctionDeclaration *> set_func(current_function, &func);
1164         TraversingVisitor::visit(func);
1165 }
1166
1167 void ExpressionResolver::visit(Return &ret)
1168 {
1169         TraversingVisitor::visit(ret);
1170         if(!current_function || !ret.expression)
1171                 return;
1172
1173         BasicTypeDeclaration *ret_basic = dynamic_cast<BasicTypeDeclaration *>(current_function->return_type_declaration);
1174         BasicTypeDeclaration *expr_basic = dynamic_cast<BasicTypeDeclaration *>(ret.expression->type);
1175         if(!ret_basic || !expr_basic)
1176                 return;
1177
1178         Compatibility compat = get_compatibility(*ret_basic, *expr_basic);
1179         if(compat==RIGHT_CONVERTIBLE)
1180                 convert_to(ret.expression, *ret_basic);
1181 }
1182
1183
1184 bool FunctionResolver::apply(Stage &s)
1185 {
1186         stage = &s;
1187         s.functions.clear();
1188         r_any_resolved = false;
1189         s.content.visit(*this);
1190         return r_any_resolved;
1191 }
1192
1193 bool FunctionResolver::can_convert_arguments(const FunctionCall &call, const FunctionDeclaration &decl)
1194 {
1195         if(decl.parameters.size()!=call.arguments.size())
1196                 return false;
1197
1198         for(unsigned j=0; j<call.arguments.size(); ++j)
1199         {
1200                 const TypeDeclaration *arg_type = call.arguments[j]->type;
1201                 const TypeDeclaration *param_type = decl.parameters[j]->type_declaration;
1202                 if(arg_type==param_type)
1203                         continue;
1204
1205                 const BasicTypeDeclaration *arg_basic = dynamic_cast<const BasicTypeDeclaration *>(arg_type);
1206                 const BasicTypeDeclaration *param_basic = dynamic_cast<const BasicTypeDeclaration *>(param_type);
1207                 if(arg_basic && param_basic && can_convert(*arg_basic, *param_basic))
1208                         continue;
1209
1210                 return false;
1211         }
1212
1213         return true;
1214 }
1215
1216 void FunctionResolver::visit(FunctionCall &call)
1217 {
1218         FunctionDeclaration *declaration = 0;
1219         if(stage->types.count(call.name))
1220                 call.constructor = true;
1221         else
1222         {
1223                 string arg_types;
1224                 bool has_signature = true;
1225                 for(auto i=call.arguments.begin(); (has_signature && i!=call.arguments.end()); ++i)
1226                 {
1227                         if((*i)->type)
1228                                 append(arg_types, ",", (*i)->type->name);
1229                         else
1230                                 has_signature = false;
1231                 }
1232
1233                 if(has_signature)
1234                 {
1235                         auto i = stage->functions.find(format("%s(%s)", call.name, arg_types));
1236                         declaration = (i!=stage->functions.end() ? i->second : 0);
1237
1238                         if(!declaration)
1239                         {
1240                                 for(i=stage->functions.lower_bound(call.name+"("); (i!=stage->functions.end() && i->second->name==call.name); ++i)
1241                                         if(can_convert_arguments(call, *i->second))
1242                                         {
1243                                                 if(declaration)
1244                                                 {
1245                                                         declaration = 0;
1246                                                         break;
1247                                                 }
1248                                                 else
1249                                                         declaration = i->second;
1250                                         }
1251                         }
1252                 }
1253         }
1254
1255         r_any_resolved |= (declaration!=call.declaration);
1256         call.declaration = declaration;
1257
1258         TraversingVisitor::visit(call);
1259 }
1260
1261 void FunctionResolver::visit(FunctionDeclaration &func)
1262 {
1263         if(func.signature.empty())
1264         {
1265                 string param_types;
1266                 for(const RefPtr<VariableDeclaration> &p: func.parameters)
1267                 {
1268                         if(p->type_declaration)
1269                                 append(param_types, ",", p->type_declaration->name);
1270                         else
1271                                 return;
1272                 }
1273                 func.signature = format("(%s)", param_types);
1274                 r_any_resolved = true;
1275         }
1276
1277         string key = func.name+func.signature;
1278         FunctionDeclaration *&stage_decl = stage->functions[key];
1279         vector<FunctionDeclaration *> &decls = declarations[key];
1280         if(func.definition==&func)
1281         {
1282                 if(stage_decl && stage_decl->definition)
1283                 {
1284                         if(!func.overrd)
1285                                 stage->diagnostics.push_back(Diagnostic(Diagnostic::WARN, func.source, func.line,
1286                                         format("Overriding function '%s' without the override keyword is deprecated", key)));
1287                         if(!stage_decl->definition->virtua)
1288                                 stage->diagnostics.push_back(Diagnostic(Diagnostic::WARN, func.source, func.line,
1289                                         format("Overriding function '%s' not declared as virtual is deprecated", key)));
1290                 }
1291                 stage_decl = &func;
1292
1293                 // Set all previous declarations to use this definition.
1294                 for(FunctionDeclaration *f: decls)
1295                 {
1296                         r_any_resolved |= (func.definition!=f->definition);
1297                         f->definition = func.definition;
1298                         f->body.body.clear();
1299                 }
1300         }
1301         else
1302         {
1303                 FunctionDeclaration *definition = (stage_decl ? stage_decl->definition : 0);
1304                 r_any_resolved |= (definition!=func.definition);
1305                 func.definition = definition;
1306
1307                 if(!stage_decl)
1308                         stage_decl = &func;
1309         }
1310         decls.push_back(&func);
1311
1312         TraversingVisitor::visit(func);
1313 }
1314
1315 } // namespace SL
1316 } // namespace GL
1317 } // namespace Msp