]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/reflect.h
Rewrite type comparisons as a visitor
[libs/gl.git] / source / glsl / reflect.h
1 #ifndef MSP_GL_SL_REFLECT_H_
2 #define MSP_GL_SL_REFLECT_H_
3
4 #include "visitor.h"
5
6 namespace Msp {
7 namespace GL {
8 namespace SL {
9
10 /** Compares two types for equality.  Struct types are compared recursively. */
11 class TypeComparer: private NodeVisitor
12 {
13 private:
14         Node *first;
15         Node *second;
16         unsigned first_tag;
17         bool r_result;
18
19         static unsigned next_tag;
20
21 public:
22         TypeComparer();
23
24         bool apply(TypeDeclaration &t1, TypeDeclaration &t2) { compare(t1, t2); return r_result; }
25
26 private:
27         void compare(Node &, Node &);
28         template<typename T>
29         T *multi_visit(T &);
30         virtual void visit(BasicTypeDeclaration &);
31         virtual void visit(ImageTypeDeclaration &);
32         virtual void visit(StructDeclaration &);
33         virtual void visit(VariableDeclaration &);
34 };
35
36 /** Determines the number of interface locations required by a variable. */
37 class LocationCounter: private NodeVisitor
38 {
39 private:
40         unsigned r_count;
41
42 public:
43         LocationCounter();
44
45         unsigned apply(VariableDeclaration &v) { v.visit(*this); return r_count; }
46
47 private:
48         virtual void visit(BasicTypeDeclaration &);
49         virtual void visit(ImageTypeDeclaration &);
50         virtual void visit(StructDeclaration &);
51         virtual void visit(VariableDeclaration &);
52 };
53
54 /** Determines the size and alignment of a variable, in bytes. */
55 class MemoryRequirementsCalculator: private NodeVisitor
56 {
57 public:
58         struct Result
59         {
60                 unsigned size;
61                 unsigned alignment;
62
63                 Result(unsigned s, unsigned a): size(s), alignment(a) { }
64         };
65 private:
66         unsigned r_size;
67         unsigned r_alignment;
68         int r_offset;
69
70 public:
71         Result apply(VariableDeclaration &v) { v.visit(*this); return Result(r_size, r_alignment); }
72
73 private:
74         virtual void visit(BasicTypeDeclaration &);
75         virtual void visit(StructDeclaration &);
76         virtual void visit(VariableDeclaration &);
77 };
78
79 /** Collects dependencies of a function.  This includes global variables,
80 interface blocks, other functions and types. */
81 class DependencyCollector: private TraversingVisitor
82 {
83 private:
84         std::set<Node *> dependencies;
85         std::set<Node *> locals;
86
87 public:
88         std::set<Node *> apply(FunctionDeclaration &);
89
90 private:
91         virtual void visit(VariableReference &);
92         virtual void visit(InterfaceBlockReference &);
93         virtual void visit(FunctionCall &);
94         virtual void visit(VariableDeclaration &);
95 };
96
97 } // namespace SL
98 } // namespace GL
99 } // namespace Msp
100
101 #endif