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
55 BlockHierarchyResolver(): r_any_resolved(false) { }
57 bool apply(Stage &s) { r_any_resolved = false; s.content.visit(*this); return r_any_resolved; }
60 virtual void enter(Block &);
63 /** Resolves types of variables and base types of other types. */
64 class TypeResolver: private TraversingVisitor
68 std::map<TypeDeclaration *, TypeDeclaration *> alias_map;
69 std::map<TypeDeclaration *, TypeDeclaration *> array_types;
70 NodeList<Statement>::iterator type_insert_point;
71 InterfaceBlock *iface_block;
80 TypeDeclaration *get_or_create_array_type(TypeDeclaration &);
81 void resolve_type(TypeDeclaration *&, const std::string &, bool);
82 virtual void visit(Block &);
83 virtual void visit(BasicTypeDeclaration &);
84 virtual void visit(ImageTypeDeclaration &);
85 virtual void visit(StructDeclaration &);
86 virtual void visit(VariableDeclaration &);
87 virtual void visit(InterfaceBlock &);
88 virtual void visit(FunctionDeclaration &);
91 /** Resolves variable references. Variable references which match the name
92 of an interface block are turned into interface block references. */
93 class VariableResolver: private TraversingVisitor
97 RefPtr<Expression> r_replacement_expr;
100 bool r_self_referencing;
101 Assignment::Target r_assignment_target;
109 virtual void enter(Block &);
110 virtual void visit(RefPtr<Expression> &);
111 void check_assignment_target(Statement *);
112 virtual void visit(VariableReference &);
113 virtual void visit(InterfaceBlockReference &);
114 void add_to_chain(Assignment::Target::ChainType, unsigned);
115 virtual void visit(MemberAccess &);
116 virtual void visit(Swizzle &);
117 virtual void visit(BinaryExpression &);
118 virtual void visit(Assignment &);
119 virtual void visit(VariableDeclaration &);
120 virtual void visit(InterfaceBlock &);
123 /** Resolves types and lvalueness of expressions. */
124 class ExpressionResolver: private TraversingVisitor
136 std::vector<BasicTypeDeclaration *> basic_types;
140 ExpressionResolver();
145 static bool is_scalar(BasicTypeDeclaration &);
146 static bool is_vector_or_matrix(BasicTypeDeclaration &);
147 static BasicTypeDeclaration *get_element_type(BasicTypeDeclaration &);
148 static bool can_convert(BasicTypeDeclaration &, BasicTypeDeclaration &);
149 static Compatibility get_compatibility(BasicTypeDeclaration &, BasicTypeDeclaration &);
150 BasicTypeDeclaration *find_type(BasicTypeDeclaration::Kind, unsigned);
151 BasicTypeDeclaration *find_type(BasicTypeDeclaration &, BasicTypeDeclaration::Kind, unsigned);
152 void convert_to(RefPtr<Expression> &, BasicTypeDeclaration &);
153 bool convert_to_element(RefPtr<Expression> &, BasicTypeDeclaration &);
154 void resolve(Expression &, TypeDeclaration *, bool);
156 virtual void visit(Literal &);
157 virtual void visit(ParenthesizedExpression &);
158 virtual void visit(VariableReference &);
159 virtual void visit(InterfaceBlockReference &);
160 virtual void visit(MemberAccess &);
161 virtual void visit(Swizzle &);
162 virtual void visit(UnaryExpression &);
163 void visit(BinaryExpression &, bool);
164 virtual void visit(BinaryExpression &);
165 virtual void visit(Assignment &);
166 virtual void visit(FunctionCall &);
167 virtual void visit(BasicTypeDeclaration &);
168 virtual void visit(VariableDeclaration &);
171 /** Resolves function declarations and calls. */
172 class FunctionResolver: private TraversingVisitor
176 std::map<std::string, std::vector<FunctionDeclaration *> > declarations;
183 virtual void visit(FunctionCall &);
184 virtual void visit(FunctionDeclaration &);
187 /** Materializes implicitly declared interfaces.
189 Out variable declarations inside functions are moved to the global scope.
191 Passthrough statements are processed, generating out variables to match in
192 variables and copying values.
194 Unresolved variables are looked up in the previous stage's out variables. */
195 class InterfaceGenerator: private TraversingVisitor
199 std::string in_prefix;
200 std::string out_prefix;
203 Block *iface_target_block;
204 NodeList<Statement>::iterator iface_insert_point;
205 NodeList<Statement>::iterator assignment_insert_point;
206 std::set<Node *> nodes_to_remove;
209 InterfaceGenerator();
214 static std::string get_out_prefix(Stage::Type);
215 std::string change_prefix(const std::string &, const std::string &) const;
216 virtual void visit(Block &);
217 VariableDeclaration *generate_interface(VariableDeclaration &, const std::string &, const std::string &);
218 InterfaceBlock *generate_interface(InterfaceBlock &);
219 ExpressionStatement &insert_assignment(const std::string &, Expression *);
220 virtual void visit(VariableReference &);
221 virtual void visit(VariableDeclaration &);
222 virtual void visit(InterfaceBlock &);
223 virtual void visit(FunctionDeclaration &);
224 virtual void visit(Passthrough &);