From 50ab5ca2babc8d9592903da6072a13b381ed6656 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 6 Mar 2021 00:46:43 +0200 Subject: [PATCH] Assign a result type to all expressions This requires some additional work for function overloads and swizzles. --- source/glsl/compiler.cpp | 3 + source/glsl/debug.cpp | 18 +- source/glsl/debug.h | 1 + source/glsl/generate.cpp | 343 ++++++++++++++++++++++++++++ source/glsl/generate.h | 42 ++++ source/glsl/optimize.cpp | 23 ++ source/glsl/optimize.h | 4 + source/glsl/syntax.cpp | 4 +- source/glsl/syntax.h | 5 +- source/glsl/validate.cpp | 39 ++++ source/glsl/validate.h | 12 + tests/glsl/binary_operators.glsl | 50 ++++ tests/glsl/invalid_expressions.glsl | 23 ++ tests/glsl/unary_operators.glsl | 28 +++ 14 files changed, 587 insertions(+), 8 deletions(-) create mode 100644 tests/glsl/binary_operators.glsl create mode 100644 tests/glsl/invalid_expressions.glsl create mode 100644 tests/glsl/unary_operators.glsl diff --git a/source/glsl/compiler.cpp b/source/glsl/compiler.cpp index 9083ece3..66a487fb 100644 --- a/source/glsl/compiler.cpp +++ b/source/glsl/compiler.cpp @@ -250,6 +250,7 @@ void Compiler::generate(Stage &stage, Mode mode) InterfaceGenerator().apply(stage); TypeResolver().apply(stage); VariableResolver().apply(stage); + ExpressionResolver().apply(stage); FunctionResolver().apply(stage); ConstantSpecializer().apply(stage, (mode==PROGRAM && specialized ? &spec_values : 0)); @@ -262,6 +263,7 @@ bool Compiler::validate(Stage &stage) TypeValidator().apply(stage); DeclarationValidator().apply(stage); ReferenceValidator().apply(stage); + ExpressionValidator().apply(stage); for(vector::const_iterator i=stage.diagnostics.begin(); i!=stage.diagnostics.end(); ++i) if(i->severity==Diagnostic::ERR) @@ -281,6 +283,7 @@ Compiler::OptimizeResult Compiler::optimize(Stage &stage) TypeResolver().apply(stage); VariableResolver().apply(stage); FunctionResolver().apply(stage); + ExpressionResolver().apply(stage); } /* Removing variables or functions may cause things from the previous stage diff --git a/source/glsl/debug.cpp b/source/glsl/debug.cpp index d49b9a5f..8a76744a 100644 --- a/source/glsl/debug.cpp +++ b/source/glsl/debug.cpp @@ -82,6 +82,11 @@ unsigned DumpTree::get_label(const Node &node) return label; } +string DumpTree::format_type(TypeDeclaration *type) +{ + return (type ? type->name : "?"); +} + template typename T::const_iterator DumpTree::increment(typename T::const_iterator &iter, const T &container) { @@ -119,12 +124,12 @@ void DumpTree::visit(Block &block) void DumpTree::visit(Literal &literal) { - append(format("Literal: %s", literal.token)); + append(format("Literal: %s -> %s", literal.token, format_type(literal.type))); } void DumpTree::visit(ParenthesizedExpression &parexpr) { - annotated_branch("(expr)", *parexpr.expression); + annotated_branch(format("(expr) -> %s", format_type(parexpr.type)), *parexpr.expression); } void DumpTree::visit(VariableReference &var) @@ -132,7 +137,7 @@ void DumpTree::visit(VariableReference &var) string text; if(var.declaration) text += format("%%%d ", get_label(*var.declaration)); - text += format("%s (var)", var.name); + text += format("%s (var) -> %s", var.name, format_type(var.type)); append(text); } @@ -150,19 +155,19 @@ void DumpTree::visit(MemberAccess &memacc) string text = "Member access:"; if(memacc.declaration) text += format(" %%%d", get_label(*memacc.declaration)); - text += format(" .%s", memacc.member); + text += format(" .%s -> %s", memacc.member, format_type(memacc.type)); annotated_branch(text, *memacc.left); } void DumpTree::visit(UnaryExpression &unary) { - string text = format("Unary: %s, %sfix", unary.oper->token, (unary.oper->type==Operator::PREFIX ? "pre" : "post")); + string text = format("Unary: %s, %sfix -> %s", unary.oper->token, (unary.oper->type==Operator::PREFIX ? "pre" : "post"), format_type(unary.type)); annotated_branch(text, *unary.expression); } void DumpTree::visit(BinaryExpression &binary) { - append(format("Binary: %s", (binary.oper->token[0]=='[' ? "[]" : binary.oper->token))); + append(format("Binary: %s -> %s", (binary.oper->token[0]=='[' ? "[]" : binary.oper->token), format_type(binary.type))); begin_sub(); binary.left->visit(*this); last_branch(); @@ -190,6 +195,7 @@ void DumpTree::visit(FunctionCall &call) head += call.name; if(call.constructor) head += " (constructor)"; + head += format(" -> %s", format_type(call.type)); append(head); begin_sub(); diff --git a/source/glsl/debug.h b/source/glsl/debug.h index b377b844..b2de993d 100644 --- a/source/glsl/debug.h +++ b/source/glsl/debug.h @@ -36,6 +36,7 @@ private: void end_sub(); void annotated_branch(const std::string &, Node &); unsigned get_label(const Node &); + std::string format_type(TypeDeclaration *); template typename T::const_iterator increment(typename T::const_iterator &, const T &); diff --git a/source/glsl/generate.cpp b/source/glsl/generate.cpp index c9803acc..770fb460 100644 --- a/source/glsl/generate.cpp +++ b/source/glsl/generate.cpp @@ -416,6 +416,349 @@ void VariableResolver::visit(InterfaceBlock &iface) } +bool ExpressionResolver::is_scalar(BasicTypeDeclaration &type) +{ + return (type.kind==BasicTypeDeclaration::INT || type.kind==BasicTypeDeclaration::FLOAT); +} + +bool ExpressionResolver::is_vector_or_matrix(BasicTypeDeclaration &type) +{ + return (type.kind==BasicTypeDeclaration::VECTOR || type.kind==BasicTypeDeclaration::MATRIX); +} + +BasicTypeDeclaration *ExpressionResolver::get_element_type(BasicTypeDeclaration &type) +{ + if(is_vector_or_matrix(type) || type.kind==BasicTypeDeclaration::ARRAY) + { + BasicTypeDeclaration *basic_base = dynamic_cast(type.base_type); + return (basic_base ? get_element_type(*basic_base) : 0); + } + else + return &type; +} + +bool ExpressionResolver::can_convert(BasicTypeDeclaration &from, BasicTypeDeclaration &to) +{ + if(from.kind==BasicTypeDeclaration::INT && to.kind==BasicTypeDeclaration::FLOAT) + return from.size<=to.size; + else if(from.kind!=to.kind) + return false; + else if((from.kind==BasicTypeDeclaration::VECTOR || from.kind==BasicTypeDeclaration::MATRIX) && from.size==to.size) + { + BasicTypeDeclaration *from_base = dynamic_cast(from.base_type); + BasicTypeDeclaration *to_base = dynamic_cast(to.base_type); + return (from_base && to_base && can_convert(*from_base, *to_base)); + } + else + return false; +} + +ExpressionResolver::Compatibility ExpressionResolver::get_compatibility(BasicTypeDeclaration &left, BasicTypeDeclaration &right) +{ + if(&left==&right) + return SAME_TYPE; + else if(can_convert(left, right)) + return LEFT_CONVERTIBLE; + else if(can_convert(right, left)) + return RIGHT_CONVERTIBLE; + else + return NOT_COMPATIBLE; +} + +BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration::Kind kind, unsigned size) +{ + for(vector::const_iterator i=basic_types.begin(); i!=basic_types.end(); ++i) + if((*i)->kind==kind && (*i)->size==size) + return *i; + return 0; +} + +BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration &elem_type, BasicTypeDeclaration::Kind kind, unsigned size) +{ + for(vector::const_iterator i=basic_types.begin(); i!=basic_types.end(); ++i) + if(get_element_type(**i)==&elem_type && (*i)->kind==kind && (*i)->size==size) + return *i; + return 0; +} + +void ExpressionResolver::convert_to(RefPtr &expr, BasicTypeDeclaration &type) +{ + RefPtr call = new FunctionCall; + call->name = type.name; + call->constructor = true; + call->arguments.push_back(0); + call->arguments.back() = expr; + call->type = &type; + expr = call; +} + +bool ExpressionResolver::convert_to_element(RefPtr &expr, BasicTypeDeclaration &elem_type) +{ + if(BasicTypeDeclaration *expr_type = dynamic_cast(expr->type)) + { + BasicTypeDeclaration *to_type = &elem_type; + if(is_vector_or_matrix(*expr_type)) + to_type = find_type(elem_type, expr_type->kind, expr_type->size); + if(to_type) + { + convert_to(expr, *to_type); + return true; + } + } + + return false; +} + +void ExpressionResolver::visit(Literal &literal) +{ + if(literal.value.check_type()) + literal.type = find_type(BasicTypeDeclaration::BOOL, 1); + else if(literal.value.check_type()) + literal.type = find_type(BasicTypeDeclaration::INT, 32); + else if(literal.value.check_type()) + literal.type = find_type(BasicTypeDeclaration::FLOAT, 32); +} + +void ExpressionResolver::visit(ParenthesizedExpression &parexpr) +{ + TraversingVisitor::visit(parexpr); + + parexpr.type = parexpr.expression->type; + parexpr.lvalue = parexpr.expression->lvalue; +} + +void ExpressionResolver::visit(VariableReference &var) +{ + if(var.declaration) + var.type = var.declaration->type_declaration; + var.lvalue = true; +} + +void ExpressionResolver::visit(InterfaceBlockReference &iface) +{ + iface.lvalue = true; +} + +void ExpressionResolver::visit(MemberAccess &memacc) +{ + TraversingVisitor::visit(memacc); + + if(memacc.declaration) + memacc.type = memacc.declaration->type_declaration; + memacc.lvalue = memacc.left->lvalue; +} + +void ExpressionResolver::visit(UnaryExpression &unary) +{ + TraversingVisitor::visit(unary); + + BasicTypeDeclaration *basic = dynamic_cast(unary.expression->type); + if(!basic) + return; + + char oper = unary.oper->token[0]; + if(oper=='!') + { + if(basic->kind==BasicTypeDeclaration::BOOL) + unary.type = basic; + } + else if(oper=='~') + { + if(basic->kind==BasicTypeDeclaration::INT) + unary.type = basic; + } + else if(oper=='+' || oper=='-') + { + BasicTypeDeclaration *elem = get_element_type(*basic); + if(elem && is_scalar(*elem)) + unary.type = basic; + } + unary.lvalue = unary.expression->lvalue; +} + +void ExpressionResolver::visit(BinaryExpression &binary) +{ + TraversingVisitor::visit(binary); + + /* Binary operators are only defined for basic types (not for image or + structure types). */ + BasicTypeDeclaration *basic_left = dynamic_cast(binary.left->type); + BasicTypeDeclaration *basic_right = dynamic_cast(binary.right->type); + if(!basic_left || !basic_right) + return; + + binary.lvalue = false; + + char oper = binary.oper->token[0]; + if(oper=='[') + { + /* Subscripting operates on vectors, matrices and arrays, and the right + operand must be an integer. */ + if((!is_vector_or_matrix(*basic_left) && basic_left->kind!=BasicTypeDeclaration::ARRAY) || basic_right->kind!=BasicTypeDeclaration::INT) + return; + + binary.type = basic_left->base_type; + binary.lvalue = binary.left->lvalue; + return; + } + else if(basic_left->kind==BasicTypeDeclaration::ARRAY || basic_right->kind==BasicTypeDeclaration::ARRAY) + // No other binary operator can be used with arrays. + return; + + BasicTypeDeclaration *elem_left = get_element_type(*basic_left); + BasicTypeDeclaration *elem_right = get_element_type(*basic_right); + if(!elem_left || !elem_right) + return; + + Compatibility compat = get_compatibility(*basic_left, *basic_right); + Compatibility elem_compat = get_compatibility(*elem_left, *elem_right); + if(elem_compat==NOT_COMPATIBLE) + return; + + char oper2 = binary.oper->token[1]; + if((oper=='<' && oper2!='<') || (oper=='>' && oper2!='>')) + { + /* Relational operators compare two scalar integer or floating-point + values. */ + if(!is_scalar(*elem_left) || !is_scalar(*elem_right) || compat==NOT_COMPATIBLE) + return; + + binary.type = find_type(BasicTypeDeclaration::BOOL, 1); + } + else if((oper=='=' || oper=='!') && oper2=='=') + { + // Equality comparison can be done on any compatible types. + if(compat==NOT_COMPATIBLE) + return; + + binary.type = find_type(BasicTypeDeclaration::BOOL, 1); + } + else if(oper2=='&' || oper2=='|' || oper2=='^') + { + // Logical operators can only be applied to booleans. + if(basic_left->kind!=BasicTypeDeclaration::BOOL || basic_right->kind!=BasicTypeDeclaration::BOOL) + return; + + binary.type = basic_left; + } + else if((oper=='&' || oper=='|' || oper=='^' || oper=='%') && !oper2) + { + // Bitwise operators and modulo can only be applied to integers. + if(basic_left->kind!=BasicTypeDeclaration::INT || basic_right->kind!=BasicTypeDeclaration::INT) + return; + + binary.type = (compat==LEFT_CONVERTIBLE ? basic_right : basic_left); + } + else if((oper=='<' || oper=='>') && oper2==oper) + { + // Shifts only apply to integers. + if(basic_left->kind!=BasicTypeDeclaration::INT || basic_right->kind!=BasicTypeDeclaration::INT) + return; + + binary.type = basic_left; + } + else if(oper=='+' || oper=='-' || oper=='*' || oper=='/') + { + // Arithmetic operators require scalar elements. + if(!is_scalar(*elem_left) || !is_scalar(*elem_right)) + return; + + if(oper=='*' && is_vector_or_matrix(*basic_left) && is_vector_or_matrix(*basic_right) && + (basic_left->kind==BasicTypeDeclaration::MATRIX || basic_right->kind==BasicTypeDeclaration::MATRIX)) + { + /* Multiplication has special rules when at least one operand is a + matrix and the other is a vector or a matrix. */ + unsigned left_columns = basic_left->size&0xFFFF; + unsigned right_rows = basic_right->size; + if(basic_right->kind==BasicTypeDeclaration::MATRIX) + right_rows >>= 16; + if(left_columns!=right_rows) + return; + + BasicTypeDeclaration *elem_result = (elem_compat==LEFT_CONVERTIBLE ? elem_right : elem_left); + + if(basic_left->kind==BasicTypeDeclaration::VECTOR) + binary.type = find_type(*elem_result, BasicTypeDeclaration::VECTOR, basic_right->size&0xFFFF); + else if(basic_right->kind==BasicTypeDeclaration::VECTOR) + binary.type = find_type(*elem_result, BasicTypeDeclaration::VECTOR, basic_left->size>>16); + else + binary.type = find_type(*elem_result, BasicTypeDeclaration::MATRIX, (basic_left->size&0xFFFF0000)|(basic_right->size&0xFFFF)); + } + else if(compat==NOT_COMPATIBLE) + { + // Arithmetic between scalars and matrices or vectors is supported. + if(is_scalar(*basic_left) && is_vector_or_matrix(*basic_right)) + binary.type = (elem_compat==RIGHT_CONVERTIBLE ? find_type(*elem_left, basic_right->kind, basic_right->size) : basic_right); + else if(is_vector_or_matrix(*basic_left) && is_scalar(*basic_right)) + binary.type = (elem_compat==LEFT_CONVERTIBLE ? find_type(*elem_right, basic_left->kind, basic_left->size) : basic_left); + else + return; + } + else if(compat==LEFT_CONVERTIBLE) + binary.type = basic_right; + else + binary.type = basic_left; + } + else + return; + + bool converted = true; + if(compat==LEFT_CONVERTIBLE) + convert_to(binary.left, *basic_right); + else if(compat==RIGHT_CONVERTIBLE) + convert_to(binary.right, *basic_left); + else if(elem_compat==LEFT_CONVERTIBLE) + converted = convert_to_element(binary.left, *elem_right); + else if(elem_compat==RIGHT_CONVERTIBLE) + converted = convert_to_element(binary.right, *elem_left); + + if(!converted) + binary.type = 0; +} + +void ExpressionResolver::visit(Assignment &assign) +{ + TraversingVisitor::visit(assign); + assign.type = assign.left->type; + assign.lvalue = true; +} + +void ExpressionResolver::visit(FunctionCall &call) +{ + TraversingVisitor::visit(call); + + if(call.declaration) + call.type = call.declaration->return_type_declaration; + else if(call.constructor) + { + map::const_iterator i=stage->types.find(call.name); + call.type = (i!=stage->types.end() ? i->second : 0); + } + call.lvalue = false; +} + +void ExpressionResolver::visit(BasicTypeDeclaration &type) +{ + basic_types.push_back(&type); +} + +void ExpressionResolver::visit(VariableDeclaration &var) +{ + TraversingVisitor::visit(var); + if(!var.init_expression) + return; + + BasicTypeDeclaration *var_basic = dynamic_cast(var.type_declaration); + BasicTypeDeclaration *init_basic = dynamic_cast(var.init_expression->type); + if(!var_basic || !init_basic) + return; + + Compatibility compat = get_compatibility(*var_basic, *init_basic); + if(compat==RIGHT_CONVERTIBLE) + convert_to(var.init_expression, *var_basic); +} + + void FunctionResolver::apply(Stage &s) { stage = &s; diff --git a/source/glsl/generate.h b/source/glsl/generate.h index 63f1b077..ddf2b5f6 100644 --- a/source/glsl/generate.h +++ b/source/glsl/generate.h @@ -110,6 +110,48 @@ private: virtual void visit(InterfaceBlock &); }; +/** Resolves types and lvalueness of expressions. */ +class ExpressionResolver: private TraversingVisitor +{ +private: + enum Compatibility + { + NOT_COMPATIBLE, + LEFT_CONVERTIBLE, + RIGHT_CONVERTIBLE, + SAME_TYPE + }; + + Stage *stage; + std::vector basic_types; + +public: + void apply(Stage &s) { stage = &s; s.content.visit(*this); } + +private: + static bool is_scalar(BasicTypeDeclaration &); + static bool is_vector_or_matrix(BasicTypeDeclaration &); + static BasicTypeDeclaration *get_element_type(BasicTypeDeclaration &); + static bool can_convert(BasicTypeDeclaration &, BasicTypeDeclaration &); + static Compatibility get_compatibility(BasicTypeDeclaration &, BasicTypeDeclaration &); + BasicTypeDeclaration *find_type(BasicTypeDeclaration::Kind, unsigned); + BasicTypeDeclaration *find_type(BasicTypeDeclaration &, BasicTypeDeclaration::Kind, unsigned); + void convert_to(RefPtr &, BasicTypeDeclaration &); + bool convert_to_element(RefPtr &, BasicTypeDeclaration &); + + virtual void visit(Literal &); + virtual void visit(ParenthesizedExpression &); + virtual void visit(VariableReference &); + virtual void visit(InterfaceBlockReference &); + virtual void visit(MemberAccess &); + virtual void visit(UnaryExpression &); + virtual void visit(BinaryExpression &); + virtual void visit(Assignment &); + virtual void visit(FunctionCall &); + virtual void visit(BasicTypeDeclaration &); + virtual void visit(VariableDeclaration &); +}; + /** Resolves function declarations and calls. */ class FunctionResolver: private TraversingVisitor { diff --git a/source/glsl/optimize.cpp b/source/glsl/optimize.cpp index f4626f3d..1aad8d1b 100644 --- a/source/glsl/optimize.cpp +++ b/source/glsl/optimize.cpp @@ -629,6 +629,29 @@ bool UnusedTypeRemover::apply(Stage &stage) return !unused_nodes.empty(); } +void UnusedTypeRemover::visit(Literal &literal) +{ + unused_nodes.erase(literal.type); +} + +void UnusedTypeRemover::visit(UnaryExpression &unary) +{ + unused_nodes.erase(unary.type); + TraversingVisitor::visit(unary); +} + +void UnusedTypeRemover::visit(BinaryExpression &binary) +{ + unused_nodes.erase(binary.type); + TraversingVisitor::visit(binary); +} + +void UnusedTypeRemover::visit(FunctionCall &call) +{ + unused_nodes.erase(call.type); + TraversingVisitor::visit(call); +} + void UnusedTypeRemover::visit(BasicTypeDeclaration &type) { if(type.base_type) diff --git a/source/glsl/optimize.h b/source/glsl/optimize.h index 92e19c8c..455a2d84 100644 --- a/source/glsl/optimize.h +++ b/source/glsl/optimize.h @@ -175,6 +175,10 @@ public: bool apply(Stage &); private: + virtual void visit(Literal &); + virtual void visit(UnaryExpression &); + virtual void visit(BinaryExpression &); + virtual void visit(FunctionCall &); virtual void visit(BasicTypeDeclaration &); virtual void visit(ImageTypeDeclaration &); virtual void visit(StructDeclaration &); diff --git a/source/glsl/syntax.cpp b/source/glsl/syntax.cpp index c8058c4f..d7eec3da 100644 --- a/source/glsl/syntax.cpp +++ b/source/glsl/syntax.cpp @@ -94,7 +94,9 @@ void Block::visit(NodeVisitor &visitor) Expression::Expression(): - oper(0) + oper(0), + type(0), + lvalue(false) { } diff --git a/source/glsl/syntax.h b/source/glsl/syntax.h index ae05c02e..2c832c14 100644 --- a/source/glsl/syntax.h +++ b/source/glsl/syntax.h @@ -102,7 +102,7 @@ template class NodeArray: public NodeContainer > > { }; -struct StructDeclaration; +struct TypeDeclaration; struct VariableDeclaration; struct InterfaceBlock; struct FunctionDeclaration; @@ -131,6 +131,9 @@ struct Expression: Node { const Operator *oper; + TypeDeclaration *type; + bool lvalue; + Expression(); virtual Expression *clone() const = 0; diff --git a/source/glsl/validate.cpp b/source/glsl/validate.cpp index e6f0c653..676dd9e8 100644 --- a/source/glsl/validate.cpp +++ b/source/glsl/validate.cpp @@ -208,6 +208,45 @@ void ReferenceValidator::visit(FunctionDeclaration &func) TraversingVisitor::visit(func); } + +void ExpressionValidator::visit(UnaryExpression &unary) +{ + if(unary.expression->type) + { + if(!unary.type) + error(unary, format("No matching operator '%s' found for '%s'", unary.oper->token, unary.expression->type->name)); + else if((unary.oper->token[1]=='+' || unary.oper->token[1]=='-') && !unary.expression->lvalue) + error(unary, format("Operand of '%s' is not an lvalue", unary.oper->token)); + } + TraversingVisitor::visit(unary); +} + +void ExpressionValidator::visit(BinaryExpression &binary) +{ + if(!binary.type && binary.left->type && binary.right->type) + error(binary, format("No matching operator '%s' found for '%s' and '%s'", + binary.oper->token, binary.left->type->name, binary.right->type->name)); + TraversingVisitor::visit(binary); +} + +void ExpressionValidator::visit(Assignment &assign) +{ + if(assign.left->type && !assign.left->lvalue) + error(assign, "Target of assignment is not an lvalue"); + if(assign.left->type && assign.right->type && assign.left->type!=assign.right->type) + error(assign, format("Assignment to variable of type '%s' from expression of type '%s'", + assign.left->type->name, assign.right->type->name)); + TraversingVisitor::visit(assign); +} + +void ExpressionValidator::visit(VariableDeclaration &var) +{ + if(var.init_expression && var.init_expression->type && var.type_declaration && var.init_expression->type!=var.type_declaration) + error(var, format("Initializing a variable of type '%s' with an expression of type '%s'", + var.type_declaration->name, var.init_expression->type->name)); + TraversingVisitor::visit(var); +} + } // namespace SL } // namespace GL } // namespace Msp diff --git a/source/glsl/validate.h b/source/glsl/validate.h index 3efa5141..36b702e0 100644 --- a/source/glsl/validate.h +++ b/source/glsl/validate.h @@ -81,6 +81,18 @@ private: virtual void visit(FunctionDeclaration &); }; +class ExpressionValidator: private Validator +{ +public: + void apply(Stage &s) { stage = &s; s.content.visit(*this); } + +private: + virtual void visit(UnaryExpression &); + virtual void visit(BinaryExpression &); + virtual void visit(Assignment &); + virtual void visit(VariableDeclaration &); +}; + } // namespace SL } // namespace GL } // namespace Msp diff --git a/tests/glsl/binary_operators.glsl b/tests/glsl/binary_operators.glsl new file mode 100644 index 00000000..06c367f2 --- /dev/null +++ b/tests/glsl/binary_operators.glsl @@ -0,0 +1,50 @@ +#pragma MSP stage(vertex) +void main() +{ + int i = 0; + i = i-3; + float f = 0; + f = (f+i)*(f/i); + bool b = i=f; + b = b && i==f; + + int j = 1; + i = i|j; + j = j<<(i%5); + b = b || i!=j; + + mat4x2 m1; + mat2x4 m2; + mat4 m3 = m2*m1*5; + vec4 v1 = vec4(1.0); + vec4 v2; + v2 = m3*v1; + vec2 v3; + v3 = v1*m2+v2.xy; + + if(b) + ++v3; +} + +/* Expected output: vertex +void main() +{ + int i = 0; + i = i-3; + float f = float(0); + f = (f+float(i))*(f/float(i)); + bool b = float(i)=f; + b = b&&float(i)==f; + int j = 1; + i = i|1; + j = j<<(i%5); + b = b||i!=j; + mat4x2 m1; + mat2x4 m2; + vec4 v1 = vec4(1.0); + vec2 v3; + v3 = v1*m2+(m2*m1*float(5)*v1).xy; + if(b) + ++v3; +} +*/ diff --git a/tests/glsl/invalid_expressions.glsl b/tests/glsl/invalid_expressions.glsl new file mode 100644 index 00000000..cf27c9a5 --- /dev/null +++ b/tests/glsl/invalid_expressions.glsl @@ -0,0 +1,23 @@ +#pragma MSP stage(vertex) +void main() +{ + int i = 1.0; + bool b = 0; + i = b; + float f = b; + i+b; + mat3x2 m; + vec3 v; + m*m; + v*m; +} + +/* Expected error: +:4: Initializing a variable of type 'int' with an expression of type 'float' +:5: Initializing a variable of type 'bool' with an expression of type 'int' +:6: Assignment to variable of type 'int' from expression of type 'bool' +:7: Initializing a variable of type 'float' with an expression of type 'bool' +:8: No matching operator '+' found for 'int' and 'bool' +:11: No matching operator '*' found for 'mat3x2' and 'mat3x2' +:12: No matching operator '*' found for 'vec3' and 'mat3x2' +*/ diff --git a/tests/glsl/unary_operators.glsl b/tests/glsl/unary_operators.glsl new file mode 100644 index 00000000..a04d24f7 --- /dev/null +++ b/tests/glsl/unary_operators.glsl @@ -0,0 +1,28 @@ +#pragma MSP stage(vertex) +void main() +{ + int i = 0; + i = -~i; + ++i; + --i; + i++; + i--; + bool b = false; + b = !b; + int arr[3]; + ++arr[0]; +} + +/* Expected output: vertex +void main() +{ + int i = 0; + i = -~i; + ++i; + --i; + i++; + i--; + int arr[3]; + ++arr[0]; +} +*/ -- 2.43.0