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))
160 FunctionDeclaration *prev_func = dynamic_cast<FunctionDeclaration *>(previous);
161 if(prev_func && prev_func->definition==&func)
162 declarations[current_block][func.name] = &func;
164 multiple_definition(format("'%s'", func.name), func, *previous);
167 record_definition(func.name, func);
169 TraversingVisitor::visit(func);
173 void ReferenceValidator::visit(BasicTypeDeclaration &type)
175 if(!type.base.empty() && !type.base_type)
176 error(type, format("Use of undeclared type '%s'", type.base));
179 void ReferenceValidator::visit(ImageTypeDeclaration &type)
181 if(!type.base.empty() && !type.base_type)
182 error(type, format("Use of undeclared type '%s'", type.base));
185 void ReferenceValidator::visit(VariableReference &var)
188 error(var, format("Use of undeclared variable '%s'", var.name));
189 else if(stage->type!=Stage::VERTEX && var.declaration->interface=="in" && !var.declaration->linked_declaration)
190 error(var, format("Use of unlinked input variable '%s'", var.name));
193 void ReferenceValidator::visit(InterfaceBlockReference &iface)
195 /* An interface block reference without a declaration should be impossible
196 since references are generated based on existing declarations. */
197 if(!iface.declaration)
198 error(iface, format("Use of undeclared interface block '%s'", iface.name));
199 else if(stage->type!=Stage::VERTEX && iface.declaration->interface=="in" && !iface.declaration->linked_block)
200 error(iface, format("Use of unlinked input block '%s'", iface.name));
203 void ReferenceValidator::visit(VariableDeclaration &var)
205 if(!var.type_declaration)
206 error(var, format("Use of undeclared type '%s'", var.type));
207 TraversingVisitor::visit(var);
210 void ReferenceValidator::visit(FunctionDeclaration &func)
212 if(!func.return_type_declaration)
213 error(func, format("Use of undeclared type '%s'", func.return_type));
214 TraversingVisitor::visit(func);
218 void ExpressionValidator::visit(UnaryExpression &unary)
220 if(unary.expression->type)
223 error(unary, format("No matching operator '%s' found for '%s'", unary.oper->token, unary.expression->type->name));
224 else if((unary.oper->token[1]=='+' || unary.oper->token[1]=='-') && !unary.expression->lvalue)
225 error(unary, format("Operand of '%s' is not an lvalue", unary.oper->token));
227 TraversingVisitor::visit(unary);
230 void ExpressionValidator::visit(BinaryExpression &binary)
232 if(!binary.type && binary.left->type && binary.right->type)
233 error(binary, format("No matching operator '%s' found for '%s' and '%s'",
234 binary.oper->token, binary.left->type->name, binary.right->type->name));
235 TraversingVisitor::visit(binary);
238 void ExpressionValidator::visit(Assignment &assign)
240 if(assign.left->type)
242 if(!assign.left->lvalue)
243 error(assign, "Target of assignment is not an lvalue");
244 if(assign.right->type)
246 if(assign.oper->token[0]!='=')
249 error(assign, format("No matching operator '%s' found for '%s' and '%s'",
250 string(assign.oper->token, strlen(assign.oper->token)-1), assign.left->type->name, assign.right->type->name));
252 else if(assign.left->type!=assign.right->type)
253 error(assign, format("Assignment to variable of type '%s' from expression of incompatible type '%s'",
254 assign.left->type->name, assign.right->type->name));
257 TraversingVisitor::visit(assign);
260 void ExpressionValidator::visit(VariableDeclaration &var)
262 if(var.init_expression && var.init_expression->type && var.type_declaration && var.init_expression->type!=var.type_declaration)
263 error(var, format("Initializing a variable of type '%s' with an expression of incompatible type '%s'",
264 var.type_declaration->name, var.init_expression->type->name));
265 TraversingVisitor::visit(var);