]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/reflect.h
c2d7aa0483bd36e4411a55c2d5325158dfae1b92
[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(Literal &);
31         virtual void visit(VariableReference &);
32         virtual void visit(BasicTypeDeclaration &);
33         virtual void visit(ImageTypeDeclaration &);
34         virtual void visit(StructDeclaration &);
35         virtual void visit(VariableDeclaration &);
36 };
37
38 /** Determines the number of interface locations required by a variable. */
39 class LocationCounter: private NodeVisitor
40 {
41 private:
42         unsigned r_count;
43
44 public:
45         LocationCounter();
46
47         unsigned apply(VariableDeclaration &v) { v.visit(*this); return r_count; }
48
49 private:
50         virtual void visit(BasicTypeDeclaration &);
51         virtual void visit(ImageTypeDeclaration &);
52         virtual void visit(StructDeclaration &);
53         virtual void visit(VariableDeclaration &);
54 };
55
56 /** Determines the size and alignment of a variable, in bytes. */
57 class MemoryRequirementsCalculator: private NodeVisitor
58 {
59 public:
60         struct Result
61         {
62                 unsigned size;
63                 unsigned alignment;
64
65                 Result(unsigned s, unsigned a): size(s), alignment(a) { }
66         };
67 private:
68         unsigned r_size;
69         unsigned r_alignment;
70         int r_offset;
71
72 public:
73         Result apply(VariableDeclaration &v) { v.visit(*this); return Result(r_size, r_alignment); }
74
75 private:
76         virtual void visit(BasicTypeDeclaration &);
77         virtual void visit(StructDeclaration &);
78         virtual void visit(VariableDeclaration &);
79 };
80
81 /** Collects dependencies of a function.  This includes global variables,
82 interface blocks, other functions and types. */
83 class DependencyCollector: private TraversingVisitor
84 {
85 private:
86         std::set<Node *> dependencies;
87         std::set<Node *> locals;
88         std::set<FunctionDeclaration *> visited_functions;
89
90 public:
91         std::set<Node *> apply(FunctionDeclaration &);
92
93 private:
94         virtual void visit(VariableReference &);
95         virtual void visit(InterfaceBlockReference &);
96         virtual void visit(FunctionCall &);
97         virtual void visit(VariableDeclaration &);
98         virtual void visit(FunctionDeclaration &);
99 };
100
101 } // namespace SL
102 } // namespace GL
103 } // namespace Msp
104
105 #endif