]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/reflect.cpp
Rewrite type comparisons as a visitor
[libs/gl.git] / source / glsl / reflect.cpp
index 151735ab18f3790d51b94bbf61da68726ce56a39..93d64e5fdb62aac1087d080efb37a0687fd99901 100644 (file)
@@ -6,6 +6,115 @@ namespace Msp {
 namespace GL {
 namespace SL {
 
+unsigned TypeComparer::next_tag = 1;
+
+TypeComparer::TypeComparer():
+       first(0),
+       second(0),
+       first_tag(0),
+       r_result(false)
+{ }
+
+void TypeComparer::compare(Node &node1, Node &node2)
+{
+       if(&node1==&node2)
+               r_result = true;
+       else
+       {
+               second = &node2;
+               node1.visit(*this);
+       }
+}
+
+template<typename T>
+T *TypeComparer::multi_visit(T &node)
+{
+       static unsigned tag = next_tag++;
+
+       if(second)
+       {
+               Node *s = second;
+               first = &node;
+               first_tag = tag;
+               second = 0;
+               s->visit(*this);
+       }
+       else if(!first || tag!=first_tag)
+               r_result = false;
+       else
+       {
+               T *f = static_cast<T *>(first);
+               first = 0;
+               return f;
+       }
+
+       return 0;
+}
+
+void TypeComparer::visit(BasicTypeDeclaration &basic)
+{
+       if(BasicTypeDeclaration *basic1 = multi_visit(basic))
+       {
+               if(basic1->kind!=basic.kind || basic1->size!=basic.size)
+                       r_result = false;
+               else if(basic1->base_type && basic.base_type)
+                       compare(*basic1->base_type, *basic.base_type);
+               else
+                       r_result = (!basic1->base_type && !basic.base_type);
+       }
+}
+
+void TypeComparer::visit(ImageTypeDeclaration &image)
+{
+       if(ImageTypeDeclaration *image1 = multi_visit(image))
+       {
+               if(image1->dimensions!=image.dimensions || image1->array!=image.array)
+                       r_result = false;
+               else if(image1->sampled!=image.sampled || image1->shadow!=image.shadow)
+                       r_result = false;
+               else if(image1->base_type && image.base_type)
+                       compare(*image1->base_type, *image.base_type);
+               else
+                       r_result = (!image1->base_type && !image.base_type);
+       }
+}
+
+void TypeComparer::visit(StructDeclaration &strct)
+{
+       if(StructDeclaration *strct1 = multi_visit(strct))
+       {
+               if(strct1->members.body.size()!=strct.members.body.size())
+                       r_result = false;
+               else
+               {
+                       r_result = true;
+                       NodeList<Statement>::const_iterator i = strct1->members.body.begin();
+                       NodeList<Statement>::const_iterator j = strct.members.body.begin();
+                       for(; (r_result && i!=strct1->members.body.end()); ++i, ++j)
+                               compare(**i, **j);
+               }
+       }
+}
+
+void TypeComparer::visit(VariableDeclaration &var)
+{
+       if(VariableDeclaration *var1 = multi_visit(var))
+       {
+               if(var1->name!=var.name || var1->array!=var.array)
+                       r_result = false;
+               else if(!var1->type_declaration || !var.type_declaration)
+                       r_result = false;
+               else
+               {
+                       // TODO Compare array sizes
+                       if(var1->type_declaration!=var.type_declaration)
+                               compare(*var1->type_declaration, *var.type_declaration);
+                       // TODO Compare layout qualifiers for interface block members
+               }
+       }
+}
+
+
 LocationCounter::LocationCounter():
        r_count(0)
 { }