]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/generate.h
Populate the interface block map in VariableResolver
[libs/gl.git] / source / glsl / generate.h
1 #ifndef MSP_GL_SL_GENERATE_H_
2 #define MSP_GL_SL_GENERATE_H_
3
4 #include <map>
5 #include <set>
6 #include <string>
7 #include <vector>
8 #include "visitor.h"
9
10 namespace Msp {
11 namespace GL {
12 namespace SL {
13
14 class DeclarationCombiner: private TraversingVisitor
15 {
16 private:
17         std::map<std::string, std::vector<FunctionDeclaration *> > functions;
18         std::map<std::string, VariableDeclaration *> variables;
19         std::set<Node *> nodes_to_remove;
20
21 public:
22         void apply(Stage &);
23
24 private:
25         virtual void visit(Block &);
26         virtual void visit(FunctionDeclaration &);
27         virtual void visit(VariableDeclaration &);
28 };
29
30 class BlockResolver: private TraversingVisitor
31 {
32 public:
33         void apply(Stage &s) { s.content.visit(*this); }
34
35 private:
36         virtual void enter(Block &);
37 };
38
39 class VariableResolver: private TraversingVisitor
40 {
41 private:
42         Block *builtins;
43         std::map<std::string, VariableDeclaration *> *members;
44         RefPtr<InterfaceBlockReference> iface_ref;
45         std::string block_interface;
46         bool record_target;
47         VariableDeclaration *assignment_target;
48         bool self_referencing;
49
50 public:
51         VariableResolver();
52
53         void apply(Stage &);
54
55 private:
56         Block *next_block(Block &);
57
58         virtual void enter(Block &);
59         virtual void visit(VariableReference &);
60         virtual void visit(InterfaceBlockReference &);
61         virtual void visit(MemberAccess &);
62         virtual void visit(BinaryExpression &);
63         virtual void visit(Assignment &);
64         virtual void visit(StructDeclaration &);
65         virtual void visit(VariableDeclaration &);
66         virtual void visit(InterfaceBlock &);
67 };
68
69 class FunctionResolver: private TraversingVisitor
70 {
71 private:
72         std::map<std::string, std::vector<FunctionDeclaration *> > functions;
73
74 public:
75         void apply(Stage &s) { s.content.visit(*this); }
76
77 private:
78         virtual void visit(FunctionCall &);
79         virtual void visit(FunctionDeclaration &);
80 };
81
82 class InterfaceGenerator: private TraversingVisitor
83 {
84 private:
85         Stage *stage;
86         std::string in_prefix;
87         std::string out_prefix;
88         bool function_scope;
89         InterfaceBlock *iface_block;
90         bool copy_block;
91         Block *iface_target_block;
92         NodeList<Statement>::iterator iface_insert_point;
93         NodeList<Statement>::iterator assignment_insert_point;
94         std::set<Node *> nodes_to_remove;
95
96 public:
97         InterfaceGenerator();
98
99         void apply(Stage &);
100
101 private:
102         static std::string get_out_prefix(Stage::Type);
103         std::string change_prefix(const std::string &, const std::string &) const;
104         virtual void visit(Block &);
105         bool generate_interface(VariableDeclaration &, const std::string &, const std::string &);
106         bool generate_interface(InterfaceBlock &);
107         ExpressionStatement &insert_assignment(const std::string &, Expression *);
108         virtual void visit(VariableReference &);
109         virtual void visit(VariableDeclaration &);
110         virtual void visit(InterfaceBlock &);
111         virtual void visit(FunctionDeclaration &);
112         virtual void visit(Passthrough &);
113 };
114
115 class DeclarationReorderer: private TraversingVisitor
116 {
117 private:
118         enum DeclarationKind
119         {
120                 NO_DECLARATION,
121                 LAYOUT,
122                 STRUCT,
123                 VARIABLE,
124                 FUNCTION
125         };
126
127         DeclarationKind kind;
128         std::set<Node *> ordered_funcs;
129         std::set<Node *> needed_funcs;
130
131 public:
132         DeclarationReorderer();
133
134         void apply(Stage &s) { s.content.visit(*this); }
135
136 private:
137         virtual void visit(Block &);
138         virtual void visit(FunctionCall &);
139         virtual void visit(InterfaceLayout &) { kind = LAYOUT; }
140         virtual void visit(StructDeclaration &) { kind = STRUCT; }
141         virtual void visit(VariableDeclaration &);
142         virtual void visit(InterfaceBlock &) { kind = VARIABLE; }
143         virtual void visit(FunctionDeclaration &);
144 };
145
146 } // namespace SL
147 } // namespace GL
148 } // namespace Msp
149
150 #endif