1 #include <msp/core/raii.h>
2 #include <msp/strings/format.h>
11 Validator::Validator():
15 void Validator::diagnose(Node &node, Diagnostic::Severity severity, const string &message)
18 diag.severity = severity;
19 diag.source = node.source;
20 diag.line = node.line;
21 diag.message = message;
22 stage->diagnostics.push_back(diag);
26 TypeValidator::TypeValidator():
30 void TypeValidator::visit(BasicTypeDeclaration &type)
32 if(type.kind==BasicTypeDeclaration::VECTOR)
34 BasicTypeDeclaration::Kind base_kind = BasicTypeDeclaration::VOID;
35 if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
36 base_kind = basic_base->kind;
37 if(base_kind!=BasicTypeDeclaration::BOOL && base_kind!=BasicTypeDeclaration::INT && base_kind!=BasicTypeDeclaration::FLOAT)
38 error(type, format("Invalid base type '%s' for vector type '%s'", type.base, type.name));
40 else if(type.kind==BasicTypeDeclaration::ARRAY)
42 if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
43 if(basic_base->kind==BasicTypeDeclaration::VOID)
44 error(type, format("Invalid base type '%s' for array type '%s'", type.base, type.name));
48 void TypeValidator::visit(ImageTypeDeclaration &type)
50 BasicTypeDeclaration::Kind base_kind = BasicTypeDeclaration::VOID;
51 if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
52 base_kind = basic_base->kind;
53 if(base_kind!=BasicTypeDeclaration::INT && base_kind!=BasicTypeDeclaration::FLOAT)
54 error(type, format("Invalid base type '%s' for image type '%s'", type.base, type.name));
57 void TypeValidator::visit(StructDeclaration &strct)
59 SetFlag set_struct(in_struct);
60 TraversingVisitor::visit(strct);
63 void TypeValidator::visit(VariableDeclaration &var)
68 error(var, "Struct members can't have layouts");
69 if(var.init_expression)
70 error(var, "Struct members can't have initializers");
73 TraversingVisitor::visit(var);
77 DeclarationValidator::DeclarationValidator():
78 anonymous_block(false)
81 void DeclarationValidator::multiple_definition(const string &name, Statement &statement, Statement &previous)
83 error(statement, format("Multiple definition of %s", name));
84 diagnose(previous, Diagnostic::INFO, "Previous definition is here");
87 Statement *DeclarationValidator::find_definition(const string &name)
89 BlockDeclarationMap *decls = &declarations[current_block];
90 BlockDeclarationMap::const_iterator i = decls->find(name);
91 if(i==decls->end() && anonymous_block)
93 decls = &declarations[current_block->parent];
94 i = decls->find(name);
96 return (i!=decls->end() ? i->second : 0);
99 void DeclarationValidator::check_definition(const string &name, Statement &statement)
101 if(Statement *previous = find_definition(name))
102 multiple_definition(format("'%s'", name), statement, *previous);
104 record_definition(name, statement);
107 void DeclarationValidator::record_definition(const string &name, Statement &statement)
109 declarations[current_block].insert(make_pair(name, &statement));
111 declarations[current_block->parent].insert(make_pair(name, &statement));
114 void DeclarationValidator::visit(TypeDeclaration &type)
116 check_definition(type.name, type);
119 void DeclarationValidator::visit(StructDeclaration &strct)
121 check_definition(strct.name, strct);
122 TraversingVisitor::visit(strct);
125 void DeclarationValidator::visit(VariableDeclaration &var)
127 check_definition(var.name, var);
128 TraversingVisitor::visit(var);
131 void DeclarationValidator::visit(InterfaceBlock &iface)
133 string key = iface.interface+iface.name;
134 map<string, InterfaceBlock *>::const_iterator i = interface_blocks.find(key);
135 if(i!=interface_blocks.end())
136 multiple_definition(format("interface block '%s %s'", iface.interface, iface.name), iface, *i->second);
138 interface_blocks.insert(make_pair(key, &iface));
140 if(Statement *previous = find_definition(iface.name))
142 if(!dynamic_cast<InterfaceBlock *>(previous))
143 multiple_definition(format("'%s'", iface.name), iface, *previous);
146 record_definition(iface.name, iface);
148 if(!iface.instance_name.empty())
149 check_definition(iface.instance_name, iface);
151 SetFlag set_anon(anonymous_block, iface.instance_name.empty());
152 TraversingVisitor::visit(iface);
155 void DeclarationValidator::visit(FunctionDeclaration &func)
157 if(Statement *previous = find_definition(func.name))
158 if(!dynamic_cast<FunctionDeclaration *>(previous))
159 multiple_definition(format("'%s'", func.name), func, *previous);
161 if(func.definition==&func)
162 check_definition(func.name, func);
163 TraversingVisitor::visit(func);
167 void ReferenceValidator::visit(BasicTypeDeclaration &type)
169 if(!type.base.empty() && !type.base_type)
170 error(type, format("Use of undeclared type '%s'", type.base));
173 void ReferenceValidator::visit(ImageTypeDeclaration &type)
175 if(!type.base.empty() && !type.base_type)
176 error(type, format("Use of undeclared type '%s'", type.base));
179 void ReferenceValidator::visit(VariableReference &var)
182 error(var, format("Use of undeclared variable '%s'", var.name));
183 else if(stage->type!=Stage::VERTEX && var.declaration->interface=="in" && !var.declaration->linked_declaration)
184 error(var, format("Use of unlinked input variable '%s'", var.name));
187 void ReferenceValidator::visit(InterfaceBlockReference &iface)
189 /* An interface block reference without a declaration should be impossible
190 since references are generated based on existing declarations. */
191 if(!iface.declaration)
192 error(iface, format("Use of undeclared interface block '%s'", iface.name));
193 else if(stage->type!=Stage::VERTEX && iface.declaration->interface=="in" && !iface.declaration->linked_block)
194 error(iface, format("Use of unlinked input block '%s'", iface.name));
197 void ReferenceValidator::visit(VariableDeclaration &var)
199 if(!var.type_declaration)
200 error(var, format("Use of undeclared type '%s'", var.type));
201 TraversingVisitor::visit(var);
204 void ReferenceValidator::visit(FunctionDeclaration &func)
206 if(!func.return_type_declaration)
207 error(func, format("Use of undeclared type '%s'", func.return_type));
208 TraversingVisitor::visit(func);
212 void ExpressionValidator::visit(UnaryExpression &unary)
214 if(unary.expression->type)
217 error(unary, format("No matching operator '%s' found for '%s'", unary.oper->token, unary.expression->type->name));
218 else if((unary.oper->token[1]=='+' || unary.oper->token[1]=='-') && !unary.expression->lvalue)
219 error(unary, format("Operand of '%s' is not an lvalue", unary.oper->token));
221 TraversingVisitor::visit(unary);
224 void ExpressionValidator::visit(BinaryExpression &binary)
226 if(!binary.type && binary.left->type && binary.right->type)
227 error(binary, format("No matching operator '%s' found for '%s' and '%s'",
228 binary.oper->token, binary.left->type->name, binary.right->type->name));
229 TraversingVisitor::visit(binary);
232 void ExpressionValidator::visit(Assignment &assign)
234 if(assign.left->type && !assign.left->lvalue)
235 error(assign, "Target of assignment is not an lvalue");
236 if(assign.left->type && assign.right->type && assign.left->type!=assign.right->type)
237 error(assign, format("Assignment to variable of type '%s' from expression of type '%s'",
238 assign.left->type->name, assign.right->type->name));
239 TraversingVisitor::visit(assign);
242 void ExpressionValidator::visit(VariableDeclaration &var)
244 if(var.init_expression && var.init_expression->type && var.type_declaration && var.init_expression->type!=var.type_declaration)
245 error(var, format("Initializing a variable of type '%s' with an expression of type '%s'",
246 var.type_declaration->name, var.init_expression->type->name));
247 TraversingVisitor::visit(var);