1 #include <msp/core/algorithm.h>
2 #include <msp/core/maputils.h>
12 const Operator Operator::operators[] =
14 { "[", "]", 2, BINARY, LEFT_TO_RIGHT },
15 { "(", ")", 2, POSTFIX, LEFT_TO_RIGHT },
16 { ".", { }, 2, POSTFIX, LEFT_TO_RIGHT },
17 { "++", { }, 2, POSTFIX, LEFT_TO_RIGHT },
18 { "--", { }, 2, POSTFIX, LEFT_TO_RIGHT },
19 { "++", { }, 3, PREFIX, RIGHT_TO_LEFT },
20 { "--", { }, 3, PREFIX, RIGHT_TO_LEFT },
21 { "+", { }, 3, PREFIX, RIGHT_TO_LEFT },
22 { "-", { }, 3, PREFIX, RIGHT_TO_LEFT },
23 { "~", { }, 3, PREFIX, RIGHT_TO_LEFT },
24 { "!", { }, 3, PREFIX, RIGHT_TO_LEFT },
25 { "*", { }, 4, BINARY, ASSOCIATIVE },
26 { "/", { }, 4, BINARY, LEFT_TO_RIGHT },
27 { "%", { }, 4, BINARY, LEFT_TO_RIGHT },
28 { "+", { }, 5, BINARY, ASSOCIATIVE },
29 { "-", { }, 5, BINARY, LEFT_TO_RIGHT },
30 { "<<", { }, 6, BINARY, LEFT_TO_RIGHT },
31 { ">>", { }, 6, BINARY, LEFT_TO_RIGHT },
32 { "<", { }, 7, BINARY, LEFT_TO_RIGHT },
33 { ">", { }, 7, BINARY, LEFT_TO_RIGHT },
34 { "<=", { }, 7, BINARY, LEFT_TO_RIGHT },
35 { ">=", { }, 7, BINARY, LEFT_TO_RIGHT },
36 { "==", { }, 8, BINARY, LEFT_TO_RIGHT },
37 { "!=", { }, 8, BINARY, LEFT_TO_RIGHT },
38 { "&", { }, 9, BINARY, ASSOCIATIVE },
39 { "^", { }, 10, BINARY, ASSOCIATIVE },
40 { "|", { }, 11, BINARY, ASSOCIATIVE },
41 { "&&", { }, 12, BINARY, ASSOCIATIVE },
42 { "^^", { }, 13, BINARY, ASSOCIATIVE },
43 { "||", { }, 14, BINARY, ASSOCIATIVE },
44 { "?", ":", 15, TERNARY, RIGHT_TO_LEFT },
45 { "=", { }, 16, BINARY, RIGHT_TO_LEFT },
46 { "+=", { }, 16, BINARY, RIGHT_TO_LEFT },
47 { "-=", { }, 16, BINARY, RIGHT_TO_LEFT },
48 { "*=", { }, 16, BINARY, RIGHT_TO_LEFT },
49 { "/=", { }, 16, BINARY, RIGHT_TO_LEFT },
50 { "%=", { }, 16, BINARY, RIGHT_TO_LEFT },
51 { "<<=", { }, 16, BINARY, RIGHT_TO_LEFT },
52 { ">>=", { }, 16, BINARY, RIGHT_TO_LEFT },
53 { "&=", { }, 16, BINARY, RIGHT_TO_LEFT },
54 { "^=", { }, 16, BINARY, RIGHT_TO_LEFT },
55 { "|=", { }, 16, BINARY, RIGHT_TO_LEFT },
56 { ",", { }, 17, BINARY, LEFT_TO_RIGHT },
57 { { 0 }, { }, 18, NO_OPERATOR, LEFT_TO_RIGHT }
60 const Operator &Operator::get_operator(const string &token, Type type)
62 for(const Operator *i=operators; i->type; ++i)
63 if(i->type==type && i->token==token)
65 throw key_error(token);
70 NodeContainer<C>::NodeContainer(const NodeContainer &c):
78 Block::Block(const Block &other):
81 use_braces(other.use_braces),
85 void Block::visit(NodeVisitor &visitor)
91 void Literal::visit(NodeVisitor &visitor)
97 VariableReference::VariableReference(const VariableReference &other):
102 void VariableReference::visit(NodeVisitor &visitor)
104 visitor.visit(*this);
108 InterfaceBlockReference::InterfaceBlockReference(const InterfaceBlockReference &other):
113 void InterfaceBlockReference::visit(NodeVisitor &visitor)
115 visitor.visit(*this);
119 MemberAccess::MemberAccess(const MemberAccess &other):
123 // Do not copy declaration
126 void MemberAccess::visit(NodeVisitor &visitor)
128 visitor.visit(*this);
132 void Swizzle::visit(NodeVisitor &visitor)
134 visitor.visit(*this);
138 void UnaryExpression::visit(NodeVisitor &visitor)
140 visitor.visit(*this);
144 void BinaryExpression::visit(NodeVisitor &visitor)
146 visitor.visit(*this);
150 Assignment::Assignment(const Assignment &other):
151 BinaryExpression(other),
152 self_referencing(other.self_referencing)
153 // Do not copy target
156 void Assignment::visit(NodeVisitor &visitor)
158 visitor.visit(*this);
162 bool Assignment::Target::operator<(const Target &other) const
164 if(declaration!=other.declaration)
165 return declaration<other.declaration;
166 for(unsigned i=0; (i<7 && i<chain_len && i<other.chain_len); ++i)
167 if(chain[i]!=other.chain[i])
168 return chain[i]<other.chain[i];
169 return chain_len<other.chain_len;
173 void TernaryExpression::visit(NodeVisitor &visitor)
175 visitor.visit(*this);
179 FunctionCall::FunctionCall(const FunctionCall &other):
182 constructor(other.constructor),
183 arguments(other.arguments)
184 // Do not copy declaration
187 void FunctionCall::visit(NodeVisitor &visitor)
189 visitor.visit(*this);
193 void ExpressionStatement::visit(NodeVisitor &visitor)
195 visitor.visit(*this);
199 void Import::visit(NodeVisitor &visitor)
201 visitor.visit(*this);
205 void Precision::visit(NodeVisitor &visitor)
207 visitor.visit(*this);
211 void Layout::visit(NodeVisitor &visitor)
213 visitor.visit(*this);
217 void InterfaceLayout::visit(NodeVisitor &visitor)
219 visitor.visit(*this);
223 BasicTypeDeclaration::BasicTypeDeclaration(const BasicTypeDeclaration &other):
224 TypeDeclaration(other),
229 // Do not copy base type
232 void BasicTypeDeclaration::visit(NodeVisitor &visitor)
234 visitor.visit(*this);
238 void ImageTypeDeclaration::visit(NodeVisitor &visitor)
240 visitor.visit(*this);
244 StructDeclaration::StructDeclaration()
246 members.use_braces = true;
249 StructDeclaration::StructDeclaration(const StructDeclaration &other):
250 TypeDeclaration(other),
251 members(other.members)
252 // Do not copy interface block
255 StructDeclaration::~StructDeclaration()
257 if(interface_block && interface_block->struct_declaration==this)
258 interface_block->struct_declaration = 0;
261 void StructDeclaration::visit(NodeVisitor &visitor)
263 visitor.visit(*this);
267 VariableDeclaration::VariableDeclaration(const VariableDeclaration &other):
269 layout(other.layout),
270 constant(other.constant),
271 sampling(other.sampling),
272 interpolation(other.interpolation),
273 interface(other.interface),
274 precision(other.precision),
278 array_size(other.array_size),
279 init_expression(other.init_expression)
280 // Do not copy type and linked declarations
283 VariableDeclaration::~VariableDeclaration()
285 if(linked_declaration && linked_declaration->linked_declaration==this)
286 linked_declaration->linked_declaration = 0;
289 void VariableDeclaration::visit(NodeVisitor &visitor)
291 visitor.visit(*this);
295 InterfaceBlock::InterfaceBlock(const InterfaceBlock &other):
297 layout(other.layout),
298 interface(other.interface),
299 block_name(other.block_name),
300 members(other.members),
301 instance_name(other.instance_name),
303 // Do not copy pointers to other nodes
306 InterfaceBlock::~InterfaceBlock()
308 if(linked_block && linked_block->linked_block==this)
309 linked_block->linked_block = 0;
310 if(struct_declaration && struct_declaration->interface_block==this)
311 struct_declaration->interface_block = 0;
314 void InterfaceBlock::visit(NodeVisitor &visitor)
316 visitor.visit(*this);
320 FunctionDeclaration::FunctionDeclaration(const FunctionDeclaration &other):
322 return_type(other.return_type),
324 parameters(other.parameters),
325 virtua(other.virtua),
326 overrd(other.overrd),
328 signature(other.signature),
329 definition(other.definition==&other ? this : 0)
330 // Do not copy return type declaration
333 void FunctionDeclaration::visit(NodeVisitor &visitor)
335 visitor.visit(*this);
339 void Conditional::visit(NodeVisitor &visitor)
341 visitor.visit(*this);
345 void Iteration::visit(NodeVisitor &visitor)
347 visitor.visit(*this);
351 void Passthrough::visit(NodeVisitor &visitor)
353 visitor.visit(*this);
357 void Return::visit(NodeVisitor &visitor)
359 visitor.visit(*this);
363 void Jump::visit(NodeVisitor &visitor)
365 visitor.visit(*this);
369 Stage::Stage(Stage::Type t):
373 const char *Stage::get_stage_name(Type type)
375 static const char *const names[] = { "shared", "vertex", "geometry", "fragment" };
381 shared(Stage::SHARED)
385 string get_unused_variable_name(const Block &block, const string &base)
390 unsigned base_size = name.size();
394 for(const Block *b=█ (unused && b); b=b->parent)
395 unused = !b->variables.count(name);
399 name.erase(base_size);
400 name += format("_%d", number);
405 int get_layout_value(const Layout &layout, const string &name, int def_value)
407 auto i = find_member(layout.qualifiers, name, &Layout::Qualifier::name);
408 return (i!=layout.qualifiers.end() ? i->value : def_value);
411 void add_to_chain(Assignment::Target &target, Assignment::Target::ChainType type, unsigned index)
413 if(target.chain_len<7)
414 target.chain[target.chain_len] = type | min<unsigned>(index, 0x3F);
418 bool targets_overlap(const Assignment::Target &target1, const Assignment::Target &target2)
420 bool overlap = (target1.declaration==target2.declaration);
421 for(unsigned i=0; (overlap && i<target1.chain_len && i<target2.chain_len); ++i)
423 Assignment::Target::ChainType type1 = static_cast<Assignment::Target::ChainType>(target1.chain[i]&0xC0);
424 Assignment::Target::ChainType type2 = static_cast<Assignment::Target::ChainType>(target2.chain[i]&0xC0);
425 unsigned index1 = target1.chain[i]&0x3F;
426 unsigned index2 = target2.chain[i]&0x3F;
427 if(type1==Assignment::Target::SWIZZLE || type2==Assignment::Target::SWIZZLE)
429 if(type1==Assignment::Target::SWIZZLE && type2==Assignment::Target::SWIZZLE)
430 overlap = index1&index2;
431 else if(type1==Assignment::Target::ARRAY && index1<4)
432 overlap = index2&(1<<index1);
433 else if(type2==Assignment::Target::ARRAY && index2<4)
434 overlap = index1&(1<<index2);
435 // Treat other combinations as overlapping (shouldn't happen)
438 overlap = (type1==type2 && (index1==index2 || index1==0x3F || index2==0x3F));