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 variable references. Variable references which match the name
59 of an interface block are turned into interface block references. */
60 class VariableResolver: private TraversingVisitor
64 std::map<std::string, VariableDeclaration *> *members;
65 RefPtr<InterfaceBlockReference> iface_ref;
66 std::string block_interface;
68 VariableDeclaration *assignment_target;
69 bool self_referencing;
77 virtual void enter(Block &);
78 virtual void visit(VariableReference &);
79 virtual void visit(InterfaceBlockReference &);
80 virtual void visit(MemberAccess &);
81 virtual void visit(BinaryExpression &);
82 virtual void visit(Assignment &);
83 virtual void visit(StructDeclaration &);
84 virtual void visit(VariableDeclaration &);
85 virtual void visit(InterfaceBlock &);
88 /** Resolves function declarations and calls. */
89 class FunctionResolver: private TraversingVisitor
93 std::map<std::string, std::vector<FunctionDeclaration *> > declarations;
99 virtual void visit(FunctionCall &);
100 virtual void visit(FunctionDeclaration &);
103 /** Materializes implicitly declared interfaces.
105 Out variable declarations inside functions are moved to the global scope.
107 Passthrough statements are processed, generating out variables to match in
108 variables and copying values.
110 Unresolved variables are looked up in the previous stage's out variables. */
111 class InterfaceGenerator: private TraversingVisitor
115 std::string in_prefix;
116 std::string out_prefix;
118 InterfaceBlock *iface_block;
120 Block *iface_target_block;
121 NodeList<Statement>::iterator iface_insert_point;
122 NodeList<Statement>::iterator assignment_insert_point;
123 std::set<Node *> nodes_to_remove;
126 InterfaceGenerator();
131 static std::string get_out_prefix(Stage::Type);
132 std::string change_prefix(const std::string &, const std::string &) const;
133 virtual void visit(Block &);
134 bool generate_interface(VariableDeclaration &, const std::string &, const std::string &);
135 bool generate_interface(InterfaceBlock &);
136 ExpressionStatement &insert_assignment(const std::string &, Expression *);
137 virtual void visit(VariableReference &);
138 virtual void visit(VariableDeclaration &);
139 virtual void visit(InterfaceBlock &);
140 virtual void visit(FunctionDeclaration &);
141 virtual void visit(Passthrough &);