1 #ifndef MSP_GL_SL_GENERATE_H_
2 #define MSP_GL_SL_GENERATE_H_
14 /** Manipulates specialization constants. If values are specified, turns
15 specialization constants into normal constants. Without values assigns
16 automatic constant_ids to specialization constants. */
17 class ConstantSpecializer: private TraversingVisitor
20 const std::map<std::string, int> *values;
23 ConstantSpecializer();
25 void apply(Stage &, const std::map<std::string, int> *);
28 virtual void visit(VariableDeclaration &);
31 /** Forms links between nested blocks in the syntax tree. */
32 class BlockHierarchyResolver: private TraversingVisitor
38 BlockHierarchyResolver(): r_any_resolved(false) { }
40 bool apply(Stage &s) { r_any_resolved = false; s.content.visit(*this); return r_any_resolved; }
43 virtual void enter(Block &);
46 /** Resolves types of variables and base types of other types. */
47 class TypeResolver: private TraversingVisitor
51 std::map<TypeDeclaration *, TypeDeclaration *> alias_map;
52 std::map<TypeDeclaration *, TypeDeclaration *> array_types;
53 NodeList<Statement>::iterator type_insert_point;
54 InterfaceBlock *iface_block;
63 TypeDeclaration *get_or_create_array_type(TypeDeclaration &);
64 void resolve_type(TypeDeclaration *&, const std::string &, bool);
65 virtual void visit(Block &);
66 virtual void visit(BasicTypeDeclaration &);
67 virtual void visit(ImageTypeDeclaration &);
68 virtual void visit(StructDeclaration &);
69 virtual void visit(VariableDeclaration &);
70 virtual void visit(InterfaceBlock &);
71 virtual void visit(FunctionDeclaration &);
74 /** Resolves variable references. Variable references which match the name
75 of an interface block are turned into interface block references. */
76 class VariableResolver: private TraversingVisitor
80 RefPtr<Expression> r_replacement_expr;
83 bool r_self_referencing;
84 Assignment::Target r_assignment_target;
85 std::vector<VariableDeclaration *> redeclared_builtins;
86 std::set<Node *> nodes_to_remove;
94 virtual void enter(Block &);
95 virtual void visit(RefPtr<Expression> &);
96 void check_assignment_target(Statement *);
97 virtual void visit(VariableReference &);
98 virtual void visit(InterfaceBlockReference &);
99 void add_to_chain(Assignment::Target::ChainType, unsigned);
100 virtual void visit(MemberAccess &);
101 virtual void visit(Swizzle &);
102 virtual void visit(BinaryExpression &);
103 virtual void visit(Assignment &);
104 void merge_layouts(Layout &, const Layout &);
105 virtual void visit(VariableDeclaration &);
106 virtual void visit(InterfaceBlock &);
109 /** Resolves types and lvalueness of expressions. */
110 class ExpressionResolver: private TraversingVisitor
123 BasicTypeDeclaration *type;
124 unsigned component_count;
128 std::vector<BasicTypeDeclaration *> basic_types;
129 NodeList<Statement>::iterator insert_point;
133 ExpressionResolver();
138 static bool is_scalar(BasicTypeDeclaration &);
139 static bool is_vector_or_matrix(BasicTypeDeclaration &);
140 static BasicTypeDeclaration *get_element_type(BasicTypeDeclaration &);
141 static bool can_convert(BasicTypeDeclaration &, BasicTypeDeclaration &);
142 static Compatibility get_compatibility(BasicTypeDeclaration &, BasicTypeDeclaration &);
143 BasicTypeDeclaration *find_type(BasicTypeDeclaration::Kind, unsigned);
144 BasicTypeDeclaration *find_type(BasicTypeDeclaration &, BasicTypeDeclaration::Kind, unsigned);
145 void convert_to(RefPtr<Expression> &, BasicTypeDeclaration &);
146 bool convert_to_element(RefPtr<Expression> &, BasicTypeDeclaration &);
147 bool truncate_vector(RefPtr<Expression> &, unsigned);
148 void resolve(Expression &, TypeDeclaration *, bool);
150 virtual void visit(Block &);
151 virtual void visit(Literal &);
152 virtual void visit(VariableReference &);
153 virtual void visit(InterfaceBlockReference &);
154 virtual void visit(MemberAccess &);
155 virtual void visit(Swizzle &);
156 virtual void visit(UnaryExpression &);
157 void visit(BinaryExpression &, bool);
158 virtual void visit(BinaryExpression &);
159 virtual void visit(Assignment &);
160 virtual void visit(TernaryExpression &);
161 void visit_constructor(FunctionCall &);
162 virtual void visit(FunctionCall &);
163 virtual void visit(BasicTypeDeclaration &);
164 virtual void visit(VariableDeclaration &);
167 /** Resolves function declarations and calls. */
168 class FunctionResolver: private TraversingVisitor
172 std::map<std::string, std::vector<FunctionDeclaration *> > declarations;
179 virtual void visit(FunctionCall &);
180 virtual void visit(FunctionDeclaration &);
183 /** Materializes implicitly declared interfaces.
185 Out variable declarations inside functions are moved to the global scope.
187 Passthrough statements are processed, generating out variables to match in
188 variables and copying values.
190 Unresolved variables are looked up in the previous stage's out variables. */
191 class InterfaceGenerator: private TraversingVisitor
195 std::string in_prefix;
196 std::string out_prefix;
199 std::vector<VariableDeclaration *> declared_inputs;
200 Block *iface_target_block;
201 NodeList<Statement>::iterator iface_insert_point;
202 NodeList<Statement>::iterator assignment_insert_point;
203 std::set<Node *> nodes_to_remove;
206 InterfaceGenerator();
211 static std::string get_out_prefix(Stage::Type);
212 std::string change_prefix(const std::string &, const std::string &) const;
213 virtual void visit(Block &);
214 VariableDeclaration *generate_interface(VariableDeclaration &, const std::string &, const std::string &);
215 InterfaceBlock *generate_interface(InterfaceBlock &);
216 ExpressionStatement &insert_assignment(const std::string &, Expression *);
217 virtual void visit(VariableReference &);
218 virtual void visit(VariableDeclaration &);
219 virtual void visit(InterfaceBlock &);
220 virtual void visit(FunctionDeclaration &);
221 virtual void visit(Passthrough &);