1 #ifndef MSP_GL_SL_GENERATE_H_
2 #define MSP_GL_SL_GENERATE_H_
14 /** Combines multiple declarations of the same identifier into one. */
15 class DeclarationCombiner: private TraversingVisitor
18 std::map<std::string, std::vector<FunctionDeclaration *> > functions;
19 std::map<std::string, VariableDeclaration *> variables;
20 std::set<Node *> nodes_to_remove;
26 virtual void visit(Block &);
27 virtual void visit(VariableDeclaration &);
28 virtual void visit(FunctionDeclaration &) { }
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
37 const std::map<std::string, int> *values;
40 ConstantSpecializer();
42 void apply(Stage &, const std::map<std::string, int> *);
45 virtual void visit(VariableDeclaration &);
48 /** Forms links between nested blocks in the syntax tree. */
49 class BlockHierarchyResolver: private TraversingVisitor
52 void apply(Stage &s) { s.content.visit(*this); }
55 virtual void enter(Block &);
58 /** Resolves types of variables and base types of other types. */
59 class TypeResolver: private TraversingVisitor
70 virtual void visit(BasicTypeDeclaration &);
71 virtual void visit(ImageTypeDeclaration &);
72 virtual void visit(StructDeclaration &);
73 virtual void visit(VariableDeclaration &);
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
82 std::map<std::string, VariableDeclaration *> *r_members;
83 RefPtr<InterfaceBlockReference> r_iface_ref;
84 std::string block_interface;
86 bool r_self_referencing;
87 VariableDeclaration *r_assignment_target;
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 &);
107 /** Resolves function declarations and calls. */
108 class FunctionResolver: private TraversingVisitor
112 std::map<std::string, std::vector<FunctionDeclaration *> > declarations;
118 virtual void visit(FunctionCall &);
119 virtual void visit(FunctionDeclaration &);
122 /** Materializes implicitly declared interfaces.
124 Out variable declarations inside functions are moved to the global scope.
126 Passthrough statements are processed, generating out variables to match in
127 variables and copying values.
129 Unresolved variables are looked up in the previous stage's out variables. */
130 class InterfaceGenerator: private TraversingVisitor
134 std::string in_prefix;
135 std::string out_prefix;
137 InterfaceBlock *iface_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;
145 InterfaceGenerator();
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 &);