]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/generate.h
Give declaration nodes to all GLSL types.
[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 /** Combines multiple declarations of the same identifier into one. */
15 class DeclarationCombiner: private TraversingVisitor
16 {
17 private:
18         std::map<std::string, std::vector<FunctionDeclaration *> > functions;
19         std::map<std::string, VariableDeclaration *> variables;
20         std::set<Node *> nodes_to_remove;
21
22 public:
23         void apply(Stage &);
24
25 private:
26         virtual void visit(Block &);
27         virtual void visit(VariableDeclaration &);
28         virtual void visit(FunctionDeclaration &) { }
29 };
30
31 /** Manipulates specialization constants.  If values are specified, turns
32 specialization constants into normal constants.  Without values assigns
33 automatic constant_ids to specialization constants. */
34 class ConstantSpecializer: private TraversingVisitor
35 {
36 private:
37         const std::map<std::string, int> *values;
38
39 public:
40         ConstantSpecializer();
41
42         void apply(Stage &, const std::map<std::string, int> *);
43
44 private:
45         virtual void visit(VariableDeclaration &);
46 };
47
48 /** Forms links between nested blocks in the syntax tree. */
49 class BlockHierarchyResolver: private TraversingVisitor
50 {
51 public:
52         void apply(Stage &s) { s.content.visit(*this); }
53
54 private:
55         virtual void enter(Block &);
56 };
57
58 /** Resolves types of variables and base types of other types. */
59 class TypeResolver: private TraversingVisitor
60 {
61 private:
62         Stage *stage;
63
64 public:
65         TypeResolver();
66
67         void apply(Stage &);
68
69 private:
70         virtual void visit(BasicTypeDeclaration &);
71         virtual void visit(ImageTypeDeclaration &);
72         virtual void visit(StructDeclaration &);
73         virtual void visit(VariableDeclaration &);
74 };
75
76 /** Resolves variable references.  Variable references which match the name
77 of an interface block are turned into interface block references. */
78 class VariableResolver: private TraversingVisitor
79 {
80 private:
81         Stage *stage;
82         std::map<std::string, VariableDeclaration *> *r_members;
83         RefPtr<InterfaceBlockReference> r_iface_ref;
84         std::string block_interface;
85         bool record_target;
86         bool r_self_referencing;
87         VariableDeclaration *r_assignment_target;
88
89 public:
90         VariableResolver();
91
92         void apply(Stage &);
93
94 private:
95         virtual void enter(Block &);
96         virtual void visit(VariableReference &);
97         virtual void visit(InterfaceBlockReference &);
98         virtual void visit(MemberAccess &);
99         virtual void visit(UnaryExpression &);
100         virtual void visit(BinaryExpression &);
101         virtual void visit(Assignment &);
102         virtual void visit(FunctionCall &);
103         virtual void visit(VariableDeclaration &);
104         virtual void visit(InterfaceBlock &);
105 };
106
107 /** Resolves function declarations and calls. */
108 class FunctionResolver: private TraversingVisitor
109 {
110 private:
111         Stage *stage;
112         std::map<std::string, std::vector<FunctionDeclaration *> > declarations;
113
114 public:
115         void apply(Stage &);
116
117 private:
118         virtual void visit(FunctionCall &);
119         virtual void visit(FunctionDeclaration &);
120 };
121
122 /** Materializes implicitly declared interfaces.
123
124 Out variable declarations inside functions are moved to the global scope.
125
126 Passthrough statements are processed, generating out variables to match in
127 variables and copying values.
128
129 Unresolved variables are looked up in the previous stage's out variables. */
130 class InterfaceGenerator: private TraversingVisitor
131 {
132 private:
133         Stage *stage;
134         std::string in_prefix;
135         std::string out_prefix;
136         bool function_scope;
137         InterfaceBlock *iface_block;
138         bool copy_block;
139         Block *iface_target_block;
140         NodeList<Statement>::iterator iface_insert_point;
141         NodeList<Statement>::iterator assignment_insert_point;
142         std::set<Node *> nodes_to_remove;
143
144 public:
145         InterfaceGenerator();
146
147         void apply(Stage &);
148
149 private:
150         static std::string get_out_prefix(Stage::Type);
151         std::string change_prefix(const std::string &, const std::string &) const;
152         virtual void visit(Block &);
153         VariableDeclaration *generate_interface(VariableDeclaration &, const std::string &, const std::string &);
154         InterfaceBlock *generate_interface(InterfaceBlock &);
155         ExpressionStatement &insert_assignment(const std::string &, Expression *);
156         virtual void visit(VariableReference &);
157         virtual void visit(VariableDeclaration &);
158         virtual void visit(InterfaceBlock &);
159         virtual void visit(FunctionDeclaration &);
160         virtual void visit(Passthrough &);
161 };
162
163 } // namespace SL
164 } // namespace GL
165 } // namespace Msp
166
167 #endif