]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/generate.h
Have generate_interface return the generated statement
[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 variable references.  Variable references which match the name
59 of an interface block are turned into interface block references. */
60 class VariableResolver: private TraversingVisitor
61 {
62 private:
63         Stage *stage;
64         std::map<std::string, VariableDeclaration *> *r_members;
65         RefPtr<InterfaceBlockReference> r_iface_ref;
66         std::string block_interface;
67         bool record_target;
68         bool r_self_referencing;
69         VariableDeclaration *r_assignment_target;
70
71 public:
72         VariableResolver();
73
74         void apply(Stage &);
75
76 private:
77         virtual void enter(Block &);
78         virtual void visit(VariableReference &);
79         virtual void visit(InterfaceBlockReference &);
80         virtual void visit(MemberAccess &);
81         virtual void visit(UnaryExpression &);
82         virtual void visit(BinaryExpression &);
83         virtual void visit(Assignment &);
84         virtual void visit(FunctionCall &);
85         virtual void visit(StructDeclaration &);
86         virtual void visit(VariableDeclaration &);
87         virtual void visit(InterfaceBlock &);
88 };
89
90 /** Resolves function declarations and calls. */
91 class FunctionResolver: private TraversingVisitor
92 {
93 private:
94         Stage *stage;
95         std::map<std::string, std::vector<FunctionDeclaration *> > declarations;
96
97 public:
98         void apply(Stage &);
99
100 private:
101         virtual void visit(FunctionCall &);
102         virtual void visit(FunctionDeclaration &);
103 };
104
105 /** Materializes implicitly declared interfaces.
106
107 Out variable declarations inside functions are moved to the global scope.
108
109 Passthrough statements are processed, generating out variables to match in
110 variables and copying values.
111
112 Unresolved variables are looked up in the previous stage's out variables. */
113 class InterfaceGenerator: private TraversingVisitor
114 {
115 private:
116         Stage *stage;
117         std::string in_prefix;
118         std::string out_prefix;
119         bool function_scope;
120         InterfaceBlock *iface_block;
121         bool copy_block;
122         Block *iface_target_block;
123         NodeList<Statement>::iterator iface_insert_point;
124         NodeList<Statement>::iterator assignment_insert_point;
125         std::set<Node *> nodes_to_remove;
126
127 public:
128         InterfaceGenerator();
129
130         void apply(Stage &);
131
132 private:
133         static std::string get_out_prefix(Stage::Type);
134         std::string change_prefix(const std::string &, const std::string &) const;
135         virtual void visit(Block &);
136         VariableDeclaration *generate_interface(VariableDeclaration &, const std::string &, const std::string &);
137         InterfaceBlock *generate_interface(InterfaceBlock &);
138         ExpressionStatement &insert_assignment(const std::string &, Expression *);
139         virtual void visit(VariableReference &);
140         virtual void visit(VariableDeclaration &);
141         virtual void visit(InterfaceBlock &);
142         virtual void visit(FunctionDeclaration &);
143         virtual void visit(Passthrough &);
144 };
145
146 } // namespace SL
147 } // namespace GL
148 } // namespace Msp
149
150 #endif