2 #include <msp/core/raii.h>
3 #include <msp/strings/format.h>
12 Validator::Validator():
16 void Validator::diagnose(Node &node, Diagnostic::Severity severity, const string &message)
19 diag.severity = severity;
20 diag.source = node.source;
21 diag.line = node.line;
22 diag.message = message;
23 stage->diagnostics.push_back(diag);
27 TypeValidator::TypeValidator():
31 void TypeValidator::visit(BasicTypeDeclaration &type)
33 if(type.kind==BasicTypeDeclaration::VECTOR)
35 BasicTypeDeclaration::Kind base_kind = BasicTypeDeclaration::VOID;
36 if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
37 base_kind = basic_base->kind;
38 if(base_kind!=BasicTypeDeclaration::BOOL && base_kind!=BasicTypeDeclaration::INT && base_kind!=BasicTypeDeclaration::FLOAT)
39 error(type, format("Invalid base type '%s' for vector type '%s'", type.base, type.name));
41 else if(type.kind==BasicTypeDeclaration::ARRAY)
43 if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
44 if(basic_base->kind==BasicTypeDeclaration::VOID)
45 error(type, format("Invalid base type '%s' for array type '%s'", type.base, type.name));
49 void TypeValidator::visit(ImageTypeDeclaration &type)
51 BasicTypeDeclaration::Kind base_kind = BasicTypeDeclaration::VOID;
52 if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
53 base_kind = basic_base->kind;
54 if(base_kind!=BasicTypeDeclaration::INT && base_kind!=BasicTypeDeclaration::FLOAT)
55 error(type, format("Invalid base type '%s' for image type '%s'", type.base, type.name));
58 void TypeValidator::visit(StructDeclaration &strct)
60 SetFlag set_struct(in_struct);
61 TraversingVisitor::visit(strct);
64 void TypeValidator::visit(VariableDeclaration &var)
69 error(var, "Struct members can't have layouts");
70 if(var.init_expression)
71 error(var, "Struct members can't have initializers");
74 TraversingVisitor::visit(var);
78 DeclarationValidator::DeclarationValidator():
79 anonymous_block(false)
82 void DeclarationValidator::multiple_definition(const string &name, Statement &statement, Statement &previous)
84 error(statement, format("Multiple definition of %s", name));
85 diagnose(previous, Diagnostic::INFO, "Previous definition is here");
88 Statement *DeclarationValidator::find_definition(const string &name)
90 BlockDeclarationMap *decls = &declarations[current_block];
91 BlockDeclarationMap::const_iterator i = decls->find(name);
92 if(i==decls->end() && anonymous_block)
94 decls = &declarations[current_block->parent];
95 i = decls->find(name);
97 return (i!=decls->end() ? i->second : 0);
100 void DeclarationValidator::check_definition(const string &name, Statement &statement)
102 if(Statement *previous = find_definition(name))
103 multiple_definition(format("'%s'", name), statement, *previous);
105 record_definition(name, statement);
108 void DeclarationValidator::record_definition(const string &name, Statement &statement)
110 declarations[current_block].insert(make_pair(name, &statement));
112 declarations[current_block->parent].insert(make_pair(name, &statement));
115 void DeclarationValidator::visit(TypeDeclaration &type)
117 check_definition(type.name, type);
120 void DeclarationValidator::visit(StructDeclaration &strct)
122 check_definition(strct.name, strct);
123 TraversingVisitor::visit(strct);
126 void DeclarationValidator::visit(VariableDeclaration &var)
128 check_definition(var.name, var);
129 TraversingVisitor::visit(var);
132 void DeclarationValidator::visit(InterfaceBlock &iface)
134 string key = iface.interface+iface.name;
135 map<string, InterfaceBlock *>::const_iterator i = interface_blocks.find(key);
136 if(i!=interface_blocks.end())
137 multiple_definition(format("interface block '%s %s'", iface.interface, iface.name), iface, *i->second);
139 interface_blocks.insert(make_pair(key, &iface));
141 if(Statement *previous = find_definition(iface.name))
143 if(!dynamic_cast<InterfaceBlock *>(previous))
144 multiple_definition(format("'%s'", iface.name), iface, *previous);
147 record_definition(iface.name, iface);
149 if(!iface.instance_name.empty())
150 check_definition(iface.instance_name, iface);
152 SetFlag set_anon(anonymous_block, iface.instance_name.empty());
153 TraversingVisitor::visit(iface);
156 void DeclarationValidator::visit(FunctionDeclaration &func)
158 if(Statement *previous = find_definition(func.name))
159 if(!dynamic_cast<FunctionDeclaration *>(previous))
160 multiple_definition(format("'%s'", func.name), func, *previous);
162 if(func.definition==&func)
163 check_definition(func.name, func);
164 TraversingVisitor::visit(func);
168 void ReferenceValidator::visit(BasicTypeDeclaration &type)
170 if(!type.base.empty() && !type.base_type)
171 error(type, format("Use of undeclared type '%s'", type.base));
174 void ReferenceValidator::visit(ImageTypeDeclaration &type)
176 if(!type.base.empty() && !type.base_type)
177 error(type, format("Use of undeclared type '%s'", type.base));
180 void ReferenceValidator::visit(VariableReference &var)
183 error(var, format("Use of undeclared variable '%s'", var.name));
184 else if(stage->type!=Stage::VERTEX && var.declaration->interface=="in" && !var.declaration->linked_declaration)
185 error(var, format("Use of unlinked input variable '%s'", var.name));
188 void ReferenceValidator::visit(InterfaceBlockReference &iface)
190 /* An interface block reference without a declaration should be impossible
191 since references are generated based on existing declarations. */
192 if(!iface.declaration)
193 error(iface, format("Use of undeclared interface block '%s'", iface.name));
194 else if(stage->type!=Stage::VERTEX && iface.declaration->interface=="in" && !iface.declaration->linked_block)
195 error(iface, format("Use of unlinked input block '%s'", iface.name));
198 void ReferenceValidator::visit(VariableDeclaration &var)
200 if(!var.type_declaration)
201 error(var, format("Use of undeclared type '%s'", var.type));
202 TraversingVisitor::visit(var);
205 void ReferenceValidator::visit(FunctionDeclaration &func)
207 if(!func.return_type_declaration)
208 error(func, format("Use of undeclared type '%s'", func.return_type));
209 TraversingVisitor::visit(func);
213 void ExpressionValidator::visit(UnaryExpression &unary)
215 if(unary.expression->type)
218 error(unary, format("No matching operator '%s' found for '%s'", unary.oper->token, unary.expression->type->name));
219 else if((unary.oper->token[1]=='+' || unary.oper->token[1]=='-') && !unary.expression->lvalue)
220 error(unary, format("Operand of '%s' is not an lvalue", unary.oper->token));
222 TraversingVisitor::visit(unary);
225 void ExpressionValidator::visit(BinaryExpression &binary)
227 if(!binary.type && binary.left->type && binary.right->type)
228 error(binary, format("No matching operator '%s' found for '%s' and '%s'",
229 binary.oper->token, binary.left->type->name, binary.right->type->name));
230 TraversingVisitor::visit(binary);
233 void ExpressionValidator::visit(Assignment &assign)
235 if(assign.left->type)
237 if(!assign.left->lvalue)
238 error(assign, "Target of assignment is not an lvalue");
239 if(assign.right->type)
241 if(assign.oper->token[0]!='=')
244 error(assign, format("No matching operator '%s' found for '%s' and '%s'",
245 string(assign.oper->token, strlen(assign.oper->token)-1), assign.left->type->name, assign.right->type->name));
247 else if(assign.left->type!=assign.right->type)
248 error(assign, format("Assignment to variable of type '%s' from expression of incompatible type '%s'",
249 assign.left->type->name, assign.right->type->name));
252 TraversingVisitor::visit(assign);
255 void ExpressionValidator::visit(VariableDeclaration &var)
257 if(var.init_expression && var.init_expression->type && var.type_declaration && var.init_expression->type!=var.type_declaration)
258 error(var, format("Initializing a variable of type '%s' with an expression of incompatible type '%s'",
259 var.type_declaration->name, var.init_expression->type->name));
260 TraversingVisitor::visit(var);