2 #include <msp/core/algorithm.h>
3 #include <msp/core/raii.h>
4 #include <msp/strings/format.h>
5 #include <msp/strings/utils.h>
15 void Validator::diagnose(Node &node, Node &provoking_node, Diagnostic::Severity severity, const string &message)
18 diag.severity = severity;
19 diag.source = node.source;
20 diag.line = node.line;
21 diag.provoking_source = provoking_node.source;
22 diag.provoking_line = provoking_node.line;
23 diag.message = message;
24 stage->diagnostics.push_back(diag);
26 last_provoker = &provoking_node;
29 void Validator::add_info(Node &node, const string &message)
32 throw logic_error("Tried to add info without a previous provoker");
33 diagnose(node, *last_provoker, Diagnostic::INFO, message);
37 void DeclarationValidator::apply(Stage &s, const Features &f)
41 s.content.visit(*this);
43 Node *global_err_node = 0;
44 auto i = s.functions.find("main()");
45 if(i!=s.functions.end())
46 global_err_node = i->second;
49 for(auto j=s.content.body.begin(); (!global_err_node && j!=s.content.body.end()); ++j)
51 global_err_node = j->get();
54 if(s.type==Stage::GEOMETRY)
56 if(!have_input_primitive)
57 error(*global_err_node, "No primitive type qualifier found on input");
58 if(!have_output_primitive)
59 error(*global_err_node, "No primitive type qualifier found on output");
60 if(!have_output_vertex_count)
61 error(*global_err_node, "No vertex count qualifier found on output");
63 else if(s.type==Stage::COMPUTE)
65 if(!have_workgroup_size)
66 error(*global_err_node, "No workgroup size qualifier found");
70 const char *DeclarationValidator::describe_variable(ScopeType scope)
74 case GLOBAL: return "global variable";
75 case STRUCT: return "struct member";
76 case INTERFACE_BLOCK: return "interface block member";
77 case FUNCTION_PARAM: return "function parameter";
78 case FUNCTION: return "local variable";
79 default: return "variable";
83 void DeclarationValidator::visit(Layout &layout)
85 bool push_constant = false;
87 for(const Layout::Qualifier &q: layout.qualifiers)
92 if(q.name=="location")
93 allowed = (variable && scope==GLOBAL);
94 else if(q.name=="binding" || q.name=="set")
98 if(q.name=="set" && features.target_api!=VULKAN)
100 error(layout, "Layout qualifier 'set' not allowed when targeting OpenGL");
106 const TypeDeclaration *base_type = get_ultimate_base_type(variable->type_declaration);
107 bool uniform = (variable->interface=="uniform");
108 allowed = (scope==GLOBAL && uniform && dynamic_cast<const ImageTypeDeclaration *>(base_type));
109 err_descr = (uniform ? "variable of non-opaque type" : "non-uniform variable");
113 allowed = (iface_block->interface=="uniform");
114 err_descr = "non-uniform interface block";
117 else if(q.name=="constant_id")
119 allowed = (variable && scope==GLOBAL);
122 if(!variable->constant)
125 err_descr = "non-constant variable";
129 BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(variable->type_declaration);
130 if(!basic || basic->kind<BasicTypeDeclaration::BOOL || basic->kind>BasicTypeDeclaration::INT)
133 err_descr = format("variable of type '%s'",
134 (variable->type_declaration ? variable->type_declaration->name : variable->type));
139 else if(q.name=="offset")
140 allowed = (variable && scope==INTERFACE_BLOCK && iface_block->interface=="uniform");
141 else if(q.name=="align")
142 allowed = (scope==INTERFACE_BLOCK && iface_block->interface=="uniform");
143 else if(q.name=="points")
145 allowed = (stage->type==Stage::GEOMETRY && iface_layout && (iface_layout->interface=="in" || iface_layout->interface=="out"));
149 if(iface_layout->interface=="in")
150 have_input_primitive = true;
151 else if(iface_layout->interface=="out")
152 have_output_primitive = true;
155 else if(q.name=="lines" || q.name=="lines_adjacency" || q.name=="triangles" || q.name=="triangles_adjacency")
157 allowed = (stage->type==Stage::GEOMETRY && iface_layout && iface_layout->interface=="in");
160 have_input_primitive = true;
162 else if(q.name=="line_strip" || q.name=="triangle_strip")
164 allowed = (stage->type==Stage::GEOMETRY && iface_layout && iface_layout->interface=="out");
167 have_output_primitive = true;
169 else if(q.name=="invocations")
170 allowed = (stage->type==Stage::GEOMETRY && iface_layout && iface_layout->interface=="in");
171 else if(q.name=="max_vertices")
173 allowed = (stage->type==Stage::GEOMETRY && iface_layout && iface_layout->interface=="out");
175 have_output_vertex_count = true;
177 else if(q.name=="std140" || q.name=="std430")
179 allowed = (iface_block && !variable && iface_block->interface=="uniform");
182 else if(q.name=="column_major" || q.name=="row_major")
184 allowed = (variable && scope==INTERFACE_BLOCK);
187 BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(variable->type_declaration);
188 while(basic && basic->kind==BasicTypeDeclaration::ARRAY)
189 basic = dynamic_cast<BasicTypeDeclaration *>(basic->base_type);
190 allowed = (basic && basic->kind==BasicTypeDeclaration::MATRIX);
191 err_descr = "non-matrix variable";
194 else if(q.name=="push_constant")
196 push_constant = true;
197 allowed = (iface_block && !variable && iface_block->interface=="uniform");
200 else if(q.name=="local_size_x" || q.name=="local_size_y" || q.name=="local_size_z")
202 allowed = (stage->type==Stage::COMPUTE && iface_layout && iface_layout->interface=="in");
204 have_workgroup_size = true;
206 else if(q.name=="rgba32f" || q.name=="rgba16f" || q.name=="rg32f" || q.name=="rg16f" || q.name=="r32f" || q.name=="r16f" ||
207 q.name=="rgba16" || q.name=="rgba8" || q.name=="rg16" || q.name=="rg8" || q.name=="r16" || q.name=="r8" ||
208 q.name=="rgba16_snorm" || q.name=="rgba8_snorm" || q.name=="rg16_snorm" || q.name=="rg8_snorm" || q.name=="r16_snorm" || q.name=="r8_snorm")
214 const TypeDeclaration *base_type = get_ultimate_base_type(variable->type_declaration);
215 const ImageTypeDeclaration *image = dynamic_cast<const ImageTypeDeclaration *>(base_type);
216 allowed = (image && !image->sampled);
217 err_descr = (image ? "sampled image" : "non-image variable");
223 if(err_descr.empty())
226 err_descr = describe_variable(scope);
228 err_descr = "interface block";
229 else if(iface_layout)
230 err_descr = format("interface '%s'", iface_layout->interface);
232 err_descr = "unknown declaration";
234 error(layout, format("Layout qualifier '%s' not allowed on %s", q.name, err_descr));
236 else if(value && !q.has_value)
237 error(layout, format("Layout qualifier '%s' requires a value", q.name));
238 else if(!value && q.has_value)
239 error(layout, format("Layout qualifier '%s' does not allow a value", q.name));
242 if(push_constant && binding)
243 error(layout, "Layout qualifier 'push_constant' not allowed together with 'binding' or 'set'");
246 void DeclarationValidator::visit(InterfaceLayout &layout)
248 SetForScope<InterfaceLayout *> set_layout(iface_layout, &layout);
249 TraversingVisitor::visit(layout);
252 void DeclarationValidator::visit(BasicTypeDeclaration &type)
254 BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type);
255 BasicTypeDeclaration::Kind base_kind = (basic_base ? basic_base->kind : BasicTypeDeclaration::VOID);
257 if(type.kind==BasicTypeDeclaration::VECTOR && (base_kind<BasicTypeDeclaration::BOOL || base_kind>BasicTypeDeclaration::FLOAT))
258 error(type, format("Invalid base type '%s' for vector type '%s'", type.base, type.name));
259 else if(type.kind==BasicTypeDeclaration::MATRIX && base_kind!=BasicTypeDeclaration::VECTOR)
260 error(type, format("Invalid base type '%s' for matrix type '%s'", type.base, type.name));
261 else if(type.kind==BasicTypeDeclaration::ARRAY && basic_base && base_kind==BasicTypeDeclaration::VOID)
262 error(type, format("Invalid base type '%s' for array type '%s'", type.base, type.name));
265 void DeclarationValidator::visit(ImageTypeDeclaration &type)
267 BasicTypeDeclaration::Kind base_kind = BasicTypeDeclaration::VOID;
268 if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
269 base_kind = basic_base->kind;
270 if(base_kind!=BasicTypeDeclaration::INT && base_kind!=BasicTypeDeclaration::FLOAT)
271 error(type, format("Invalid base type '%s' for image type '%s'", type.base, type.name));
274 void DeclarationValidator::visit(StructDeclaration &strct)
276 SetForScope<ScopeType> set_scope(scope, (strct.block_name.empty() ? STRUCT : INTERFACE_BLOCK));
277 SetForScope<VariableDeclaration *> set_iface(iface_block, strct.block_declaration);
278 TraversingVisitor::visit(strct);
281 void DeclarationValidator::visit(VariableDeclaration &var)
283 SetForScope<VariableDeclaration *> set_var((var.block_declaration ? iface_block : variable), &var);
285 const char *descr = describe_variable(scope);
289 if(scope!=GLOBAL && scope!=INTERFACE_BLOCK)
290 error(var, format("Layout qualifier not allowed on %s", descr));
292 var.layout->visit(*this);
297 if(scope==STRUCT || scope==INTERFACE_BLOCK)
298 error(var, format("Constant qualifier not allowed on %s", descr));
299 if(!var.init_expression)
300 error(var, "Constant variable must have an initializer");
303 if(!var.interpolation.empty() || !var.sampling.empty())
305 if(var.interface!="in" && stage->type==Stage::VERTEX)
306 error(var, "Interpolation qualifier not allowed on vertex input");
307 else if(var.interface!="out" && stage->type==Stage::FRAGMENT)
308 error(var, "Interpolation qualifier not allowed on fragment output");
309 else if((var.interface!="in" && var.interface!="out") || (scope==FUNCTION_PARAM || scope==FUNCTION))
310 error(var, "Interpolation qualifier not allowed on non-interpolated variable");
313 if(!var.interface.empty())
315 if(iface_block && var.interface!=iface_block->interface)
316 error(var, format("Mismatched interface qualifier '%s' inside '%s' block", var.interface, iface_block->interface));
317 else if(scope==STRUCT || scope==FUNCTION)
318 error(var, format("Interface qualifier not allowed on %s", descr));
319 else if(scope==GLOBAL && var.interface=="uniform" && !var.block_declaration && features.target_api==VULKAN)
321 if(!dynamic_cast<const ImageTypeDeclaration *>(get_ultimate_base_type(var.type_declaration)))
322 error(var, "Interface qualifier 'uniform' not allowed on non-opaque variable in global scope");
326 TypeDeclaration *type = var.type_declaration;
327 BasicTypeDeclaration::Kind kind = BasicTypeDeclaration::ALIAS;
328 while(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(type))
331 type = basic->base_type;
333 if(dynamic_cast<ImageTypeDeclaration *>(type))
335 if(scope!=GLOBAL && scope!=FUNCTION_PARAM)
336 error(var, format("Type '%s' not allowed on %s", type->name, descr));
337 else if(scope==GLOBAL && var.interface!="uniform")
338 error(var, format("Type '%s' only allowed with uniform interface", type->name));
340 else if(var.block_declaration)
342 if(stage->type==Stage::VERTEX && var.interface=="in")
343 error(var, "Interface block not allowed on vertex shader input");
344 else if(stage->type==Stage::FRAGMENT && var.interface=="out")
345 error(var, "Interface block not allowed on fragment shader output");
347 else if(kind==BasicTypeDeclaration::VOID)
348 error(var, "Type 'void' not allowed on variable");
349 else if(kind==BasicTypeDeclaration::BOOL && var.source!=BUILTIN_SOURCE)
351 if(scope==INTERFACE_BLOCK)
352 error(var, "Type 'bool' not allowed in an interface block");
353 else if(!var.interface.empty())
354 error(var, "Type 'bool' not allowed on interface variable");
357 if(var.array && !var.array_size)
358 error(var, "Array must have a size");
360 if(var.init_expression)
362 if(scope==GLOBAL && !var.constant)
363 error(var, format("Initializer not allowed on non-constant %s", descr));
364 else if(scope!=GLOBAL && scope!=FUNCTION)
365 error(var, format("Initializer not allowed on %s", descr));
367 var.init_expression->visit(*this);
371 void DeclarationValidator::visit(FunctionDeclaration &func)
373 SetForScope<ScopeType> set_scope(scope, FUNCTION_PARAM);
374 for(const RefPtr<VariableDeclaration> &p: func.parameters)
377 func.body.visit(*this);
381 void IdentifierValidator::multiple_definition(const string &name, Statement &statement, Statement &previous)
383 error(statement, format("Multiple definition of %s", name));
384 add_info(previous, "Previous definition is here");
387 Statement *IdentifierValidator::find_definition(const string &name)
389 BlockDeclarationMap *decls = &declarations[current_block];
390 auto i = decls->find(name);
391 if(i==decls->end() && anonymous_block)
393 decls = &declarations[current_block->parent];
394 i = decls->find(name);
396 return (i!=decls->end() ? i->second : 0);
399 void IdentifierValidator::check_definition(const string &name, Statement &statement)
401 if(Statement *previous = find_definition(name))
402 multiple_definition(format("'%s'", name), statement, *previous);
404 record_definition(name, statement);
407 void IdentifierValidator::record_definition(const string &name, Statement &statement)
409 declarations[current_block].insert(make_pair(name, &statement));
411 declarations[current_block->parent].insert(make_pair(name, &statement));
414 void IdentifierValidator::visit(TypeDeclaration &type)
416 if(type.source!=INTERNAL_SOURCE)
417 check_definition(type.name, type);
420 void IdentifierValidator::visit(StructDeclaration &strct)
422 if(strct.block_name.empty())
423 check_definition(strct.name, strct);
424 TraversingVisitor::visit(strct);
427 void IdentifierValidator::visit(VariableDeclaration &var)
429 if(var.block_declaration)
431 string key = format("%s %s", var.interface, var.block_declaration->block_name);
432 auto i = interface_blocks.find(key);
433 if(i!=interface_blocks.end())
434 multiple_definition(format("interface block '%s %s'", var.interface, var.block_declaration->block_name), var, *i->second);
436 interface_blocks.insert(make_pair(key, &var));
438 if(Statement *previous = find_definition(var.block_declaration->block_name))
440 const VariableDeclaration *prev_var = dynamic_cast<const VariableDeclaration *>(previous);
441 if(!prev_var || !prev_var->block_declaration)
442 multiple_definition(format("'%s'", var.block_declaration->block_name), var, *previous);
445 record_definition(var.block_declaration->block_name, var);
447 if(var.name.find(' ')!=string::npos)
449 // Inject anonymous interface block members into the global scope
450 for(const auto &kvp: var.block_declaration->members.variables)
451 check_definition(kvp.first, *kvp.second);
455 if(var.name.find(' ')==string::npos)
456 check_definition(var.name, var);
458 TraversingVisitor::visit(var);
461 void IdentifierValidator::visit(FunctionDeclaration &func)
463 string key = func.name+func.signature;
464 auto i = overloaded_functions.find(key);
465 if(i==overloaded_functions.end())
466 overloaded_functions.insert(make_pair(key, &func));
467 else if(func.return_type_declaration && i->second->return_type_declaration!=func.return_type_declaration)
469 error(func, format("Conflicting return type '%s' for function '%s'", func.return_type_declaration->name, func.name));
470 if(i->second->return_type_declaration)
471 add_info(*i->second, format("Previously declared as returning '%s'", i->second->return_type_declaration->name));
474 if(Statement *previous = find_definition(func.name))
476 if(!dynamic_cast<FunctionDeclaration *>(previous))
477 multiple_definition(format("'%s'", func.name), func, *previous);
480 record_definition(func.name, func);
482 if(func.definition==&func)
483 check_definition(func.name+func.signature, func);
485 TraversingVisitor::visit(func);
489 void ReferenceValidator::visit(BasicTypeDeclaration &type)
491 if(!type.base.empty() && !type.base_type)
492 error(type, format("Use of undeclared type '%s'", type.base));
495 void ReferenceValidator::visit(ImageTypeDeclaration &type)
497 if(!type.base.empty() && !type.base_type)
498 error(type, format("Use of undeclared type '%s'", type.base));
501 void ReferenceValidator::visit(VariableReference &var)
504 error(var, format("Use of undeclared variable '%s'", var.name));
505 else if(stage->type!=Stage::VERTEX && var.declaration->interface=="in" && var.name.compare(0, 3, "gl_") && !var.declaration->linked_declaration)
506 error(var, format("Use of unlinked input %s '%s'", (var.declaration->block_declaration ? "block" : "variable"), var.name));
509 void ReferenceValidator::visit(MemberAccess &memacc)
511 if(memacc.left->type && !memacc.declaration)
512 error(memacc, format("Use of undeclared member '%s'", memacc.member));
513 TraversingVisitor::visit(memacc);
516 void ReferenceValidator::visit(FunctionCall &call)
518 if((!call.constructor && !call.declaration) || (call.constructor && !call.type))
520 bool have_declaration = call.constructor;
521 if(!call.constructor)
523 auto i = stage->functions.lower_bound(call.name);
524 have_declaration = (i!=stage->functions.end() && i->second->name==call.name);
529 bool valid_types = true;
531 for(auto j=call.arguments.begin(); (valid_types && j!=call.arguments.end()); ++j)
534 append(signature, ", ", (*j)->type->name);
540 error(call, format("No matching %s found for '%s(%s)'", (call.constructor ? "constructor" : "overload"), call.name, signature));
543 error(call, format("Call to undeclared function '%s'", call.name));
545 TraversingVisitor::visit(call);
548 void ReferenceValidator::visit(VariableDeclaration &var)
550 if(!var.type_declaration)
551 error(var, format("Use of undeclared type '%s'", var.type));
552 TraversingVisitor::visit(var);
555 void ReferenceValidator::visit(FunctionDeclaration &func)
557 if(!func.return_type_declaration)
558 error(func, format("Use of undeclared type '%s'", func.return_type));
559 TraversingVisitor::visit(func);
563 void ExpressionValidator::visit(VariableReference &var)
565 if(var.declaration && constant_expression)
567 if(!var.declaration->constant)
569 const char *kind = (var.declaration->block_declaration ? "interface block" : "non-constant variable");
570 error(var, format("Reference to %s '%s' in a constant expression", kind, var.name));
572 else if(var.declaration->layout && constant_expression==FIXED_CONSTANT)
574 if(has_layout_qualifier(var.declaration->layout.get(), "constant_id"))
575 error(var, format("Reference to specialization constant '%s' in a fixed constant expression", var.name));
580 void ExpressionValidator::visit(Swizzle &swizzle)
583 if(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(swizzle.left->type))
585 if(basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT)
587 else if(basic->kind==BasicTypeDeclaration::VECTOR)
593 static const char component_names[] = { 'x', 'y', 'z', 'w', 'r', 'g', 'b', 'a', 's', 't', 'p', 'q' };
595 for(unsigned i=0; i<swizzle.count; ++i)
597 unsigned component_flavour = (std::find(component_names, component_names+12, swizzle.component_group[i])-component_names)/4;
599 flavour = component_flavour;
600 else if(flavour>=0 && component_flavour!=static_cast<unsigned>(flavour))
602 error(swizzle, format("Flavour of swizzle component '%c' is inconsistent with '%c'",
603 swizzle.component_group[i], swizzle.component_group[0]));
607 if(swizzle.components[i]>=size)
608 error(swizzle, format("Access to component '%c' which is not present in '%s'",
609 swizzle.component_group[i], swizzle.left->type->name));
612 else if(swizzle.left->type)
613 error(swizzle, format("Swizzle applied to '%s' which is neither a scalar nor a vector", swizzle.left->type->name));
615 TraversingVisitor::visit(swizzle);
618 void ExpressionValidator::visit(UnaryExpression &unary)
620 if(unary.expression->type)
623 error(unary, format("No matching operator '%s' found for '%s'", unary.oper->token, unary.expression->type->name));
624 else if(unary.oper->token[1]=='+' || unary.oper->token[1]=='-')
626 if(constant_expression)
627 error(unary, format("Use of '%s' in a constant expression", unary.oper->token));
628 else if(!unary.expression->lvalue)
629 error(unary, format("Operand of '%s' is not an lvalue", unary.oper->token));
632 TraversingVisitor::visit(unary);
635 void ExpressionValidator::visit(BinaryExpression &binary)
637 if(!binary.type && binary.left->type && binary.right->type)
639 if(binary.oper->token[0]=='[')
640 error(binary, format("Can't index element of '%s' with '%s'",
641 binary.left->type->name, binary.right->type->name));
643 error(binary, format("No matching operator '%s' found for '%s' and '%s'",
644 binary.oper->token, binary.left->type->name, binary.right->type->name));
646 TraversingVisitor::visit(binary);
649 void ExpressionValidator::visit(Assignment &assign)
651 if(assign.left->type)
653 if(constant_expression)
654 error(assign, "Assignment in constant expression");
655 else if(!assign.left->lvalue)
656 error(assign, "Target of assignment is not an lvalue");
657 if(assign.right->type)
659 if(assign.oper->token[0]!='=')
662 error(assign, format("No matching operator '%s' found for '%s' and '%s'",
663 string(assign.oper->token, strlen(assign.oper->token)-1), assign.left->type->name, assign.right->type->name));
665 else if(assign.left->type!=assign.right->type)
666 error(assign, format("Assignment to variable of type '%s' from expression of incompatible type '%s'",
667 assign.left->type->name, assign.right->type->name));
670 TraversingVisitor::visit(assign);
673 void ExpressionValidator::visit(TernaryExpression &ternary)
675 if(ternary.condition->type)
677 BasicTypeDeclaration *basic_cond = dynamic_cast<BasicTypeDeclaration *>(ternary.condition->type);
678 if(!basic_cond || basic_cond->kind!=BasicTypeDeclaration::BOOL)
679 error(ternary, "Ternary operator condition is not a boolean");
680 else if(!ternary.type && ternary.true_expr->type && ternary.false_expr->type)
681 error(ternary, format("Ternary operator has incompatible types '%s' and '%s'",
682 ternary.true_expr->type->name, ternary.false_expr->type->name));
684 TraversingVisitor::visit(ternary);
687 void ExpressionValidator::visit(StructDeclaration &strct)
689 SetFlag set_struct(in_struct);
690 TraversingVisitor::visit(strct);
693 void ExpressionValidator::visit(VariableDeclaration &var)
695 if(var.init_expression && var.init_expression->type && var.type_declaration && var.init_expression->type!=var.type_declaration)
696 error(var, format("Initializing a variable of type '%s' with an expression of incompatible type '%s'",
697 var.type_declaration->name, var.init_expression->type->name));
700 var.layout->visit(*this);
701 if(var.init_expression)
703 ConstantKind const_kind = (!var.constant ? NOT_CONSTANT :
704 has_layout_qualifier(var.layout.get(), "constant_id") ? FIXED_CONSTANT : SPEC_CONSTANT);
706 SetForScope<ConstantKind> set_const(constant_expression, const_kind);
707 TraversingVisitor::visit(var.init_expression);
711 SetForScope<ConstantKind> set_const(constant_expression, (in_struct || !var.interface.empty() ? FIXED_CONSTANT : SPEC_CONSTANT));
712 TraversingVisitor::visit(var.array_size);
716 void ExpressionValidator::visit(FunctionDeclaration &func)
718 SetForScope<FunctionDeclaration *> set_func(current_function, &func);
719 TraversingVisitor::visit(func);
722 void ExpressionValidator::visit(Conditional &cond)
724 if(cond.condition->type)
726 BasicTypeDeclaration *basic_cond = dynamic_cast<BasicTypeDeclaration *>(cond.condition->type);
727 if(!basic_cond || basic_cond->kind!=BasicTypeDeclaration::BOOL)
728 error(cond, "Condition is not a boolean");
730 TraversingVisitor::visit(cond);
733 void ExpressionValidator::visit(Iteration &iter)
735 if(iter.condition->type)
737 BasicTypeDeclaration *basic_cond = dynamic_cast<BasicTypeDeclaration *>(iter.condition->type);
738 if(!basic_cond || basic_cond->kind!=BasicTypeDeclaration::BOOL)
739 error(iter, "Loop condition is not a boolean");
741 TraversingVisitor::visit(iter);
744 void ExpressionValidator::visit(Return &ret)
746 if(current_function && current_function->return_type_declaration)
748 TypeDeclaration *return_type = current_function->return_type_declaration;
749 BasicTypeDeclaration *basic_return = dynamic_cast<BasicTypeDeclaration *>(return_type);
752 if(ret.expression->type && ret.expression->type!=return_type)
753 error(ret, format("Return expression type '%s' is incompatible with declared return type '%s'",
754 ret.expression->type->name, return_type->name));
756 else if(!basic_return || basic_return->kind!=BasicTypeDeclaration::VOID)
757 error(ret, "Return statement without an expression in a function not returning 'void'");
760 TraversingVisitor::visit(ret);
764 void FlowControlValidator::visit(Block &block)
766 for(const RefPtr<Statement> &s: block.body)
770 diagnose(*s, Diagnostic::WARN, "Unreachable code detected");
777 void FlowControlValidator::visit(FunctionDeclaration &func)
779 func.body.visit(*this);
781 if(func.definition==&func && func.return_type_declaration)
783 const BasicTypeDeclaration *basic_ret = dynamic_cast<const BasicTypeDeclaration *>(func.return_type_declaration);
784 if(reachable && (!basic_ret || basic_ret->kind!=BasicTypeDeclaration::VOID))
785 error(func, "Missing return statement at the end of a function not returning 'void'");
790 void FlowControlValidator::visit(Conditional &cond)
792 cond.body.visit(*this);
793 bool reachable_if_true = reachable;
795 cond.else_body.visit(*this);
796 reachable |= reachable_if_true;
799 void FlowControlValidator::visit(Iteration &iter)
801 iter.body.visit(*this);
806 void StageInterfaceValidator::visit(VariableDeclaration &var)
808 int location = get_layout_value(var.layout.get(), "location");
809 if(var.interface=="in" && var.linked_declaration)
811 const Layout *linked_layout = var.linked_declaration->layout.get();
812 int linked_location = get_layout_value(linked_layout, "location");
813 if(linked_location!=location)
815 error(var, format("Mismatched location %d for 'in %s'", location, var.name));
816 add_info(*var.linked_declaration, format("Linked to 'out %s' with location %d",
817 var.linked_declaration->name, linked_location));
819 if(var.type_declaration && var.linked_declaration->type_declaration)
821 TypeDeclaration *type = var.type_declaration;
822 if(stage->type==Stage::GEOMETRY)
824 if(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(type))
825 if(basic->kind==BasicTypeDeclaration::ARRAY && basic->base_type)
826 type = basic->base_type;
828 if(!TypeComparer().apply(*type, *var.linked_declaration->type_declaration))
830 error(var, format("Mismatched type '%s' for 'in %s'", type->name, var.name));
831 add_info(*var.linked_declaration, format("Linked to 'out %s' with type '%s'",
832 var.linked_declaration->name, var.linked_declaration->type_declaration->name));
837 if(location>=0 && !var.interface.empty())
839 map<unsigned, VariableDeclaration *> &used = used_locations[var.interface];
841 unsigned loc_count = LocationCounter().apply(var);
842 for(unsigned i=0; i<loc_count; ++i)
844 auto j = used.find(location+i);
847 error(var, format("Overlapping location %d for '%s %s'", location+i, var.interface, var.name));
848 add_info(*j->second, format("Previously used here for '%s %s'", j->second->interface, j->second->name));
851 used[location+i] = &var;
857 void GlobalInterfaceValidator::apply(Module &module)
859 for(Stage &s: module.stages)
862 s.content.visit(*this);
866 void GlobalInterfaceValidator::check_uniform(const Uniform &uni)
868 auto i = used_names.find(uni.name);
869 if(i!=used_names.end())
871 if(uni.location>=0 && i->second->location>=0 && i->second->location!=uni.location)
873 error(*uni.node, format("Mismatched location %d for uniform '%s'", uni.location, uni.name));
874 add_info(*i->second->node, format("Previously declared here with location %d", i->second->location));
876 if(i->second->desc_set!=uni.desc_set)
878 error(*uni.node, format("Mismatched descriptor set %d for uniform '%s'", uni.desc_set, uni.name));
879 add_info(*i->second->node, format("Previously declared here with descriptor set %d", i->second->desc_set));
881 if(uni.bind_point>=0 && i->second->bind_point>=0 && i->second->bind_point!=uni.bind_point)
883 error(*uni.node, format("Mismatched binding %d for uniform '%s'", uni.bind_point, uni.name));
884 add_info(*i->second->node, format("Previously declared here with binding %d", i->second->bind_point));
886 if(uni.type && i->second->type && !TypeComparer().apply(*uni.type, *i->second->type))
888 string type_name = (dynamic_cast<const StructDeclaration *>(uni.type) ?
889 "structure" : format("type '%s'", uni.type->name));
890 error(*uni.node, format("Mismatched %s for uniform '%s'", type_name, uni.name));
892 string message = "Previously declared here";
893 if(!dynamic_cast<const StructDeclaration *>(i->second->type))
894 message += format(" with type '%s'", i->second->type->name);
895 add_info(*i->second->node, message);
899 used_names.insert(make_pair(uni.name, &uni));
903 auto j = used_locations.find(uni.location);
904 if(j!=used_locations.end())
906 if(j->second->name!=uni.name)
908 error(*uni.node, format("Overlapping location %d for '%s'", uni.location, uni.name));
909 add_info(*j->second->node, format("Previously used here for '%s'", j->second->name));
914 for(unsigned k=0; k<uni.loc_count; ++k)
915 used_locations.insert(make_pair(uni.location+k, &uni));
919 if(uni.bind_point>=0)
921 map<unsigned, const Uniform *> &used = used_bindings[uni.desc_set];
922 auto j = used.find(uni.bind_point);
925 if(j->second->name!=uni.name)
927 error(*uni.node, format("Overlapping binding %d for '%s'", uni.bind_point, uni.name));
928 add_info(*j->second->node, format("Previously used here for '%s'", j->second->name));
932 used.insert(make_pair(uni.bind_point, &uni));
936 void GlobalInterfaceValidator::visit(VariableDeclaration &var)
938 if(var.interface=="uniform")
942 uni.type = var.type_declaration;
943 uni.name = (var.block_declaration ? var.block_declaration->block_name : var.name);
946 if(!var.block_declaration)
948 uni.location = get_layout_value(var.layout.get(), "location");
949 uni.loc_count = LocationCounter().apply(var);
951 uni.desc_set = get_layout_value(var.layout.get(), "set", 0);
952 uni.bind_point = get_layout_value(var.layout.get(), "binding");
955 uniforms.push_back(uni);
956 check_uniform(uniforms.back());