void BlockHierarchyResolver::enter(Block &block)
{
+ r_any_resolved |= (current_block!=block.parent);
block.parent = current_block;
}
TypeResolver::TypeResolver():
- stage(0)
+ stage(0),
+ r_any_resolved(false)
{ }
-void TypeResolver::apply(Stage &s)
+bool TypeResolver::apply(Stage &s)
{
stage = &s;
s.types.clear();
+ r_any_resolved = false;
s.content.visit(*this);
+ return r_any_resolved;
+}
+
+TypeDeclaration *TypeResolver::get_or_create_array_type(TypeDeclaration &type)
+{
+ map<TypeDeclaration *, TypeDeclaration *>::iterator i = array_types.find(&type);
+ if(i!=array_types.end())
+ return i->second;
+
+ BasicTypeDeclaration *array = new BasicTypeDeclaration;
+ array->source = BUILTIN_SOURCE;
+ array->name = type.name+"[]";
+ array->kind = BasicTypeDeclaration::ARRAY;
+ array->base = type.name;
+ array->base_type = &type;
+ stage->content.body.insert(type_insert_point, array);
+ array_types[&type] = array;
+ return array;
}
-TypeDeclaration *TypeResolver::resolve_type(const string &name)
+void TypeResolver::resolve_type(TypeDeclaration *&type, const string &name, bool array)
{
+ TypeDeclaration *resolved = 0;
map<string, TypeDeclaration *>::iterator i = stage->types.find(name);
if(i!=stage->types.end())
{
map<TypeDeclaration *, TypeDeclaration *>::iterator j = alias_map.find(i->second);
- return (j!=alias_map.end() ? j->second : i->second);
+ resolved = (j!=alias_map.end() ? j->second : i->second);
}
- else
- return 0;
+
+ if(resolved && array)
+ resolved = get_or_create_array_type(*resolved);
+
+ r_any_resolved |= (resolved!=type);
+ type=resolved;
}
void TypeResolver::visit(Block &block)
void TypeResolver::visit(BasicTypeDeclaration &type)
{
- type.base_type = resolve_type(type.base);
+ resolve_type(type.base_type, type.base, false);
if(type.kind==BasicTypeDeclaration::VECTOR && type.base_type)
if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
void TypeResolver::visit(ImageTypeDeclaration &type)
{
- type.base_type = resolve_type(type.base);
+ resolve_type(type.base_type, type.base, false);
stage->types.insert(make_pair(type.name, &type));
}
void TypeResolver::visit(VariableDeclaration &var)
{
- TypeDeclaration *type = resolve_type(var.type);
- if(var.array && type)
- {
- map<TypeDeclaration *, TypeDeclaration *>::iterator i = array_types.find(type);
- if(i==array_types.end())
- {
- BasicTypeDeclaration *array = new BasicTypeDeclaration;
- array->source = BUILTIN_SOURCE;
- array->name = type->name+"[]";
- array->kind = BasicTypeDeclaration::ARRAY;
- array->base = type->name;
- array->base_type = type;
- stage->content.body.insert(type_insert_point, array);
- array->visit(*this);
- type = array;
- }
- else
- type = i->second;
- }
- var.type_declaration = type;
+ resolve_type(var.type_declaration, var.type, var.array);
}
void TypeResolver::visit(FunctionDeclaration &func)
{
- func.return_type_declaration = resolve_type(func.return_type);
+ resolve_type(func.return_type_declaration, func.return_type, false);
TraversingVisitor::visit(func);
}
resolve(unary, basic, unary.expression->lvalue);
}
-void ExpressionResolver::visit(BinaryExpression &binary)
+void ExpressionResolver::visit(BinaryExpression &binary, bool assign)
{
- TraversingVisitor::visit(binary);
-
/* Binary operators are only defined for basic types (not for image or
structure types). */
BasicTypeDeclaration *basic_left = dynamic_cast<BasicTypeDeclaration *>(binary.left->type);
Compatibility elem_compat = get_compatibility(*elem_left, *elem_right);
if(elem_compat==NOT_COMPATIBLE)
return;
+ if(assign && (compat==LEFT_CONVERTIBLE || elem_compat==LEFT_CONVERTIBLE))
+ return;
TypeDeclaration *type = 0;
char oper2 = binary.oper->token[1];
}
else if((oper=='<' || oper=='>') && oper2==oper)
{
- // Shifts only apply to integers.
- if(basic_left->kind!=BasicTypeDeclaration::INT || basic_right->kind!=BasicTypeDeclaration::INT)
+ // Shifts apply to integer scalars and vectors, with some restrictions.
+ if(elem_left->kind!=BasicTypeDeclaration::INT || elem_right->kind!=BasicTypeDeclaration::INT)
+ return;
+ unsigned left_size = (basic_left->kind==BasicTypeDeclaration::INT ? 1 : basic_left->kind==BasicTypeDeclaration::VECTOR ? basic_left->size : 0);
+ unsigned right_size = (basic_right->kind==BasicTypeDeclaration::INT ? 1 : basic_right->kind==BasicTypeDeclaration::VECTOR ? basic_right->size : 0);
+ if(!left_size || (left_size==1 && right_size!=1) || (left_size>1 && right_size!=1 && right_size!=left_size))
return;
type = basic_left;
+ // Don't perform conversion even if the operands are of different sizes.
+ compat = SAME_TYPE;
}
else if(oper=='+' || oper=='-' || oper=='*' || oper=='/')
{
else
return;
+ if(assign && type!=basic_left)
+ return;
+
bool converted = true;
if(compat==LEFT_CONVERTIBLE)
convert_to(binary.left, *basic_right);
if(!converted)
type = 0;
- resolve(binary, type, false);
+ resolve(binary, type, assign);
+}
+
+void ExpressionResolver::visit(BinaryExpression &binary)
+{
+ TraversingVisitor::visit(binary);
+ visit(binary, false);
}
void ExpressionResolver::visit(Assignment &assign)
{
TraversingVisitor::visit(assign);
+
+ if(assign.oper->token[0]!='=')
+ return visit(assign, true);
+ else if(assign.left->type!=assign.right->type)
+ {
+ BasicTypeDeclaration *basic_left = dynamic_cast<BasicTypeDeclaration *>(assign.left->type);
+ BasicTypeDeclaration *basic_right = dynamic_cast<BasicTypeDeclaration *>(assign.right->type);
+ if(!basic_left || !basic_right)
+ return;
+
+ Compatibility compat = get_compatibility(*basic_left, *basic_right);
+ if(compat==RIGHT_CONVERTIBLE)
+ convert_to(assign.right, *basic_left);
+ else if(compat!=SAME_TYPE)
+ return;
+ }
+
resolve(assign, assign.left->type, true);
}
}
-void FunctionResolver::apply(Stage &s)
+bool FunctionResolver::apply(Stage &s)
{
stage = &s;
s.functions.clear();
+ r_any_resolved = false;
s.content.visit(*this);
+ return r_any_resolved;
}
void FunctionResolver::visit(FunctionCall &call)