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
122 std::vector<BasicTypeDeclaration *> basic_types;
126 ExpressionResolver();
131 static bool is_scalar(BasicTypeDeclaration &);
132 static bool is_vector_or_matrix(BasicTypeDeclaration &);
133 static BasicTypeDeclaration *get_element_type(BasicTypeDeclaration &);
134 static bool can_convert(BasicTypeDeclaration &, BasicTypeDeclaration &);
135 static Compatibility get_compatibility(BasicTypeDeclaration &, BasicTypeDeclaration &);
136 BasicTypeDeclaration *find_type(BasicTypeDeclaration::Kind, unsigned);
137 BasicTypeDeclaration *find_type(BasicTypeDeclaration &, BasicTypeDeclaration::Kind, unsigned);
138 void convert_to(RefPtr<Expression> &, BasicTypeDeclaration &);
139 bool convert_to_element(RefPtr<Expression> &, BasicTypeDeclaration &);
140 void resolve(Expression &, TypeDeclaration *, bool);
142 virtual void visit(Literal &);
143 virtual void visit(VariableReference &);
144 virtual void visit(InterfaceBlockReference &);
145 virtual void visit(MemberAccess &);
146 virtual void visit(Swizzle &);
147 virtual void visit(UnaryExpression &);
148 void visit(BinaryExpression &, bool);
149 virtual void visit(BinaryExpression &);
150 virtual void visit(Assignment &);
151 virtual void visit(TernaryExpression &);
152 virtual void visit(FunctionCall &);
153 virtual void visit(BasicTypeDeclaration &);
154 virtual void visit(VariableDeclaration &);
157 /** Resolves function declarations and calls. */
158 class FunctionResolver: private TraversingVisitor
162 std::map<std::string, std::vector<FunctionDeclaration *> > declarations;
169 virtual void visit(FunctionCall &);
170 virtual void visit(FunctionDeclaration &);
173 /** Materializes implicitly declared interfaces.
175 Out variable declarations inside functions are moved to the global scope.
177 Passthrough statements are processed, generating out variables to match in
178 variables and copying values.
180 Unresolved variables are looked up in the previous stage's out variables. */
181 class InterfaceGenerator: private TraversingVisitor
185 std::string in_prefix;
186 std::string out_prefix;
189 std::vector<VariableDeclaration *> declared_inputs;
190 Block *iface_target_block;
191 NodeList<Statement>::iterator iface_insert_point;
192 NodeList<Statement>::iterator assignment_insert_point;
193 std::set<Node *> nodes_to_remove;
196 InterfaceGenerator();
201 static std::string get_out_prefix(Stage::Type);
202 std::string change_prefix(const std::string &, const std::string &) const;
203 virtual void visit(Block &);
204 VariableDeclaration *generate_interface(VariableDeclaration &, const std::string &, const std::string &);
205 InterfaceBlock *generate_interface(InterfaceBlock &);
206 ExpressionStatement &insert_assignment(const std::string &, Expression *);
207 virtual void visit(VariableReference &);
208 virtual void visit(VariableDeclaration &);
209 virtual void visit(InterfaceBlock &);
210 virtual void visit(FunctionDeclaration &);
211 virtual void visit(Passthrough &);