]> git.tdb.fi Git - libs/gl.git/commitdiff
Assign a result type to all expressions
authorMikko Rasa <tdb@tdb.fi>
Fri, 5 Mar 2021 22:46:43 +0000 (00:46 +0200)
committerMikko Rasa <tdb@tdb.fi>
Fri, 5 Mar 2021 23:01:38 +0000 (01:01 +0200)
This requires some additional work for function overloads and swizzles.

14 files changed:
source/glsl/compiler.cpp
source/glsl/debug.cpp
source/glsl/debug.h
source/glsl/generate.cpp
source/glsl/generate.h
source/glsl/optimize.cpp
source/glsl/optimize.h
source/glsl/syntax.cpp
source/glsl/syntax.h
source/glsl/validate.cpp
source/glsl/validate.h
tests/glsl/binary_operators.glsl [new file with mode: 0644]
tests/glsl/invalid_expressions.glsl [new file with mode: 0644]
tests/glsl/unary_operators.glsl [new file with mode: 0644]

index 9083ece3fb019681a33974a4ed46c41b15bfb296..66a487fbccd4b7a63f2575295ecc03fcbfcddf5a 100644 (file)
@@ -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<Diagnostic>::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
index d49b9a5f142e8a09b29bcc304e7c29056b87f412..8a76744a1a90bbeeeac5366489ab978c017c93ee 100644 (file)
@@ -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>
 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();
index b377b84488a5cd897645f63ad728d80337262858..b2de993da648e39a06a8f8f93d03c112b596cd39 100644 (file)
@@ -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>
        typename T::const_iterator increment(typename T::const_iterator &, const T &);
index c9803acc3b2afd5698b29f66c8d69a110ddfc18c..770fb460d657196cf7781fd0ea9b9553a2728b21 100644 (file)
@@ -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<BasicTypeDeclaration *>(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<BasicTypeDeclaration *>(from.base_type);
+               BasicTypeDeclaration *to_base = dynamic_cast<BasicTypeDeclaration *>(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<BasicTypeDeclaration *>::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<BasicTypeDeclaration *>::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<Expression> &expr, BasicTypeDeclaration &type)
+{
+       RefPtr<FunctionCall> 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<Expression> &expr, BasicTypeDeclaration &elem_type)
+{
+       if(BasicTypeDeclaration *expr_type = dynamic_cast<BasicTypeDeclaration *>(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<bool>())
+               literal.type = find_type(BasicTypeDeclaration::BOOL, 1);
+       else if(literal.value.check_type<int>())
+               literal.type = find_type(BasicTypeDeclaration::INT, 32);
+       else if(literal.value.check_type<float>())
+               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<BasicTypeDeclaration *>(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<BasicTypeDeclaration *>(binary.left->type);
+       BasicTypeDeclaration *basic_right = dynamic_cast<BasicTypeDeclaration *>(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<string, TypeDeclaration *>::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<BasicTypeDeclaration *>(var.type_declaration);
+       BasicTypeDeclaration *init_basic = dynamic_cast<BasicTypeDeclaration *>(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;
index 63f1b0770ba435b287157eef975514b7e41cab9d..ddf2b5f629cb0a8aadedd76dd0eab5041dfe1fa4 100644 (file)
@@ -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<BasicTypeDeclaration *> 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<Expression> &, BasicTypeDeclaration &);
+       bool convert_to_element(RefPtr<Expression> &, 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
 {
index f4626f3dfde4a30b91658d5e981b458fad9ab7c5..1aad8d1b311adcecf27904b8e29efb49892d98a6 100644 (file)
@@ -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)
index 92e19c8c9be08a8a649586929c0755607e558fab..455a2d84a52d1ac82c7b769b3b419e3bab962537 100644 (file)
@@ -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 &);
index c8058c4fe9a228b102cbe4792daa83dc3f921f70..d7eec3da4bf680cbf4125d0cb747feccd54af38d 100644 (file)
@@ -94,7 +94,9 @@ void Block::visit(NodeVisitor &visitor)
 
 
 Expression::Expression():
-       oper(0)
+       oper(0),
+       type(0),
+       lvalue(false)
 { }
 
 
index ae05c02e4379c429207418fdadac16ba81b3d011..2c832c149e2467ba346867eb453cc8850b7a11a6 100644 (file)
@@ -102,7 +102,7 @@ template<typename T>
 class NodeArray: public NodeContainer<std::vector<RefPtr<T> > >
 { };
 
-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;
index e6f0c653e45f4ad9e62c108084867dc4374c11c9..676dd9e808792867458d8c59d03a62d47ceb8e88 100644 (file)
@@ -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
index 3efa51419d6e6c765f93839c3ed7b76218e0bfe0..36b702e01fbb395a58c8c8a2f6c6916a83669429 100644 (file)
@@ -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 (file)
index 0000000..06c367f
--- /dev/null
@@ -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 || 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||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 (file)
index 0000000..cf27c9a
--- /dev/null
@@ -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:
+<test>:4: Initializing a variable of type 'int' with an expression of type 'float'
+<test>:5: Initializing a variable of type 'bool' with an expression of type 'int'
+<test>:6: Assignment to variable of type 'int' from expression of type 'bool'
+<test>:7: Initializing a variable of type 'float' with an expression of type 'bool'
+<test>:8: No matching operator '+' found for 'int' and 'bool'
+<test>:11: No matching operator '*' found for 'mat3x2' and 'mat3x2'
+<test>: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 (file)
index 0000000..a04d24f
--- /dev/null
@@ -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];
+}
+*/