3 #include <msp/core/raii.h>
4 #include <msp/strings/format.h>
13 Validator::Validator():
18 void Validator::diagnose(Node &node, Node &provoking_node, Diagnostic::Severity severity, const string &message)
21 diag.severity = severity;
22 diag.source = node.source;
23 diag.line = node.line;
24 diag.provoking_source = provoking_node.source;
25 diag.provoking_line = provoking_node.line;
26 diag.message = message;
27 stage->diagnostics.push_back(diag);
29 last_provoker = &provoking_node;
32 void Validator::add_info(Node &node, const string &message)
35 throw logic_error("Tried to add info without a previous provoker");
36 diagnose(node, *last_provoker, Diagnostic::INFO, message);
40 TypeValidator::TypeValidator():
44 void TypeValidator::visit(BasicTypeDeclaration &type)
46 if(type.kind==BasicTypeDeclaration::VECTOR)
48 BasicTypeDeclaration::Kind base_kind = BasicTypeDeclaration::VOID;
49 if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
50 base_kind = basic_base->kind;
51 if(base_kind!=BasicTypeDeclaration::BOOL && base_kind!=BasicTypeDeclaration::INT && base_kind!=BasicTypeDeclaration::FLOAT)
52 error(type, format("Invalid base type '%s' for vector type '%s'", type.base, type.name));
54 else if(type.kind==BasicTypeDeclaration::ARRAY)
56 if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
57 if(basic_base->kind==BasicTypeDeclaration::VOID)
58 error(type, format("Invalid base type '%s' for array type '%s'", type.base, type.name));
62 void TypeValidator::visit(ImageTypeDeclaration &type)
64 BasicTypeDeclaration::Kind base_kind = BasicTypeDeclaration::VOID;
65 if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
66 base_kind = basic_base->kind;
67 if(base_kind!=BasicTypeDeclaration::INT && base_kind!=BasicTypeDeclaration::FLOAT)
68 error(type, format("Invalid base type '%s' for image type '%s'", type.base, type.name));
71 void TypeValidator::visit(StructDeclaration &strct)
73 SetFlag set_struct(in_struct);
74 TraversingVisitor::visit(strct);
77 void TypeValidator::visit(VariableDeclaration &var)
82 error(var, "Struct members can't have layouts");
83 if(var.init_expression)
84 error(var, "Struct members can't have initializers");
87 TraversingVisitor::visit(var);
91 DeclarationValidator::DeclarationValidator():
92 anonymous_block(false)
95 void DeclarationValidator::multiple_definition(const string &name, Statement &statement, Statement &previous)
97 error(statement, format("Multiple definition of %s", name));
98 add_info(previous, "Previous definition is here");
101 Statement *DeclarationValidator::find_definition(const string &name)
103 BlockDeclarationMap *decls = &declarations[current_block];
104 BlockDeclarationMap::const_iterator i = decls->find(name);
105 if(i==decls->end() && anonymous_block)
107 decls = &declarations[current_block->parent];
108 i = decls->find(name);
110 return (i!=decls->end() ? i->second : 0);
113 void DeclarationValidator::check_definition(const string &name, Statement &statement)
115 if(Statement *previous = find_definition(name))
116 multiple_definition(format("'%s'", name), statement, *previous);
118 record_definition(name, statement);
121 void DeclarationValidator::record_definition(const string &name, Statement &statement)
123 declarations[current_block].insert(make_pair(name, &statement));
125 declarations[current_block->parent].insert(make_pair(name, &statement));
128 void DeclarationValidator::visit(TypeDeclaration &type)
130 check_definition(type.name, type);
133 void DeclarationValidator::visit(StructDeclaration &strct)
135 check_definition(strct.name, strct);
136 TraversingVisitor::visit(strct);
139 void DeclarationValidator::visit(VariableDeclaration &var)
141 check_definition(var.name, var);
142 TraversingVisitor::visit(var);
145 void DeclarationValidator::visit(InterfaceBlock &iface)
147 string key = iface.interface+iface.name;
148 map<string, InterfaceBlock *>::const_iterator i = interface_blocks.find(key);
149 if(i!=interface_blocks.end())
150 multiple_definition(format("interface block '%s %s'", iface.interface, iface.name), iface, *i->second);
152 interface_blocks.insert(make_pair(key, &iface));
154 if(Statement *previous = find_definition(iface.name))
156 if(!dynamic_cast<InterfaceBlock *>(previous))
157 multiple_definition(format("'%s'", iface.name), iface, *previous);
160 record_definition(iface.name, iface);
162 if(!iface.instance_name.empty())
163 check_definition(iface.instance_name, iface);
165 if(iface.instance_name.empty() && iface.struct_declaration)
167 // Inject anonymous interface block members into the global scope
168 const map<string, VariableDeclaration *> &iface_vars = iface.struct_declaration->members.variables;
169 for(map<string, VariableDeclaration *>::const_iterator j=iface_vars.begin(); j!=iface_vars.end(); ++j)
170 check_definition(j->first, *j->second);
174 void DeclarationValidator::visit(FunctionDeclaration &func)
176 if(Statement *previous = find_definition(func.name))
178 FunctionDeclaration *prev_func = dynamic_cast<FunctionDeclaration *>(previous);
179 if(prev_func && prev_func->definition==&func)
180 declarations[current_block][func.name] = &func;
182 multiple_definition(format("'%s'", func.name), func, *previous);
185 record_definition(func.name, func);
187 TraversingVisitor::visit(func);
191 void ReferenceValidator::visit(BasicTypeDeclaration &type)
193 if(!type.base.empty() && !type.base_type)
194 error(type, format("Use of undeclared type '%s'", type.base));
197 void ReferenceValidator::visit(ImageTypeDeclaration &type)
199 if(!type.base.empty() && !type.base_type)
200 error(type, format("Use of undeclared type '%s'", type.base));
203 void ReferenceValidator::visit(VariableReference &var)
206 error(var, format("Use of undeclared variable '%s'", var.name));
207 else if(stage->type!=Stage::VERTEX && var.declaration->interface=="in" && !var.declaration->linked_declaration)
208 error(var, format("Use of unlinked input variable '%s'", var.name));
211 void ReferenceValidator::visit(MemberAccess &memacc)
213 if(memacc.left->type && !memacc.declaration)
214 error(memacc, format("Use of undeclared member '%s'", memacc.member));
215 TraversingVisitor::visit(memacc);
218 void ReferenceValidator::visit(InterfaceBlockReference &iface)
220 /* An interface block reference without a declaration should be impossible
221 since references are generated based on existing declarations. */
222 if(!iface.declaration)
223 error(iface, format("Use of undeclared interface block '%s'", iface.name));
224 else if(stage->type!=Stage::VERTEX && iface.declaration->interface=="in" && !iface.declaration->linked_block)
225 error(iface, format("Use of unlinked input block '%s'", iface.name));
228 void ReferenceValidator::visit(VariableDeclaration &var)
230 if(!var.type_declaration)
231 error(var, format("Use of undeclared type '%s'", var.type));
232 TraversingVisitor::visit(var);
235 void ReferenceValidator::visit(InterfaceBlock &iface)
237 if(!iface.struct_declaration)
238 error(iface, format("Interface block '%s %s' lacks a struct declaration", iface.interface, iface.name));
239 TraversingVisitor::visit(iface);
242 void ReferenceValidator::visit(FunctionDeclaration &func)
244 if(!func.return_type_declaration)
245 error(func, format("Use of undeclared type '%s'", func.return_type));
246 TraversingVisitor::visit(func);
250 void ExpressionValidator::visit(Swizzle &swizzle)
253 if(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(swizzle.left->type))
255 if(basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT)
257 else if(basic->kind==BasicTypeDeclaration::VECTOR)
263 static const char component_names[] = { 'x', 'y', 'z', 'w', 'r', 'g', 'b', 'a', 's', 't', 'p', 'q' };
265 for(unsigned i=0; i<swizzle.count; ++i)
267 unsigned component_flavour = (find(component_names, component_names+12, swizzle.component_group[i])-component_names)/4;
269 flavour = component_flavour;
270 else if(flavour>=0 && component_flavour!=static_cast<unsigned>(flavour))
272 error(swizzle, format("Flavour of swizzle component '%c' is inconsistent with '%c'",
273 swizzle.component_group[i], swizzle.component_group[0]));
277 if(swizzle.components[i]>=size)
278 error(swizzle, format("Access to component '%c' which is not present in '%s'",
279 swizzle.component_group[i], swizzle.left->type->name));
282 else if(swizzle.left->type)
283 error(swizzle, format("Swizzle applied to '%s' which is neither a scalar nor a vector", swizzle.left->type->name));
285 TraversingVisitor::visit(swizzle);
288 void ExpressionValidator::visit(UnaryExpression &unary)
290 if(unary.expression->type)
293 error(unary, format("No matching operator '%s' found for '%s'", unary.oper->token, unary.expression->type->name));
294 else if((unary.oper->token[1]=='+' || unary.oper->token[1]=='-') && !unary.expression->lvalue)
295 error(unary, format("Operand of '%s' is not an lvalue", unary.oper->token));
297 TraversingVisitor::visit(unary);
300 void ExpressionValidator::visit(BinaryExpression &binary)
302 if(!binary.type && binary.left->type && binary.right->type)
303 error(binary, format("No matching operator '%s' found for '%s' and '%s'",
304 binary.oper->token, binary.left->type->name, binary.right->type->name));
305 TraversingVisitor::visit(binary);
308 void ExpressionValidator::visit(Assignment &assign)
310 if(assign.left->type)
312 if(!assign.left->lvalue)
313 error(assign, "Target of assignment is not an lvalue");
314 if(assign.right->type)
316 if(assign.oper->token[0]!='=')
319 error(assign, format("No matching operator '%s' found for '%s' and '%s'",
320 string(assign.oper->token, strlen(assign.oper->token)-1), assign.left->type->name, assign.right->type->name));
322 else if(assign.left->type!=assign.right->type)
323 error(assign, format("Assignment to variable of type '%s' from expression of incompatible type '%s'",
324 assign.left->type->name, assign.right->type->name));
327 TraversingVisitor::visit(assign);
330 void ExpressionValidator::visit(VariableDeclaration &var)
332 if(var.init_expression && var.init_expression->type && var.type_declaration && var.init_expression->type!=var.type_declaration)
333 error(var, format("Initializing a variable of type '%s' with an expression of incompatible type '%s'",
334 var.type_declaration->name, var.init_expression->type->name));
335 TraversingVisitor::visit(var);