]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/generate.cpp
Implement the ternary operator in GLSL
[libs/gl.git] / source / glsl / generate.cpp
index ab487a44c470bca769af032b9ce2966c7a0df5d2..e5855e94cacb701c4ab96bec16324335fcccf910 100644 (file)
@@ -872,6 +872,36 @@ void ExpressionResolver::visit(Assignment &assign)
        resolve(assign, assign.left->type, true);
 }
 
+void ExpressionResolver::visit(TernaryExpression &ternary)
+{
+       TraversingVisitor::visit(ternary);
+
+       BasicTypeDeclaration *basic_cond = dynamic_cast<BasicTypeDeclaration *>(ternary.condition->type);
+       if(!basic_cond || basic_cond->kind!=BasicTypeDeclaration::BOOL)
+               return;
+
+       TypeDeclaration *type = 0;
+       if(ternary.true_expr->type==ternary.false_expr->type)
+               type = ternary.true_expr->type;
+       else
+       {
+               BasicTypeDeclaration *basic_true = dynamic_cast<BasicTypeDeclaration *>(ternary.true_expr->type);
+               BasicTypeDeclaration *basic_false = dynamic_cast<BasicTypeDeclaration *>(ternary.false_expr->type);
+               Compatibility compat = get_compatibility(*basic_true, *basic_false);
+               if(compat==NOT_COMPATIBLE)
+                       return;
+
+               type = (compat==LEFT_CONVERTIBLE ? basic_true : basic_false);
+
+               if(compat==LEFT_CONVERTIBLE)
+                       convert_to(ternary.true_expr, *basic_false);
+               else if(compat==RIGHT_CONVERTIBLE)
+                       convert_to(ternary.false_expr, *basic_true);
+       }
+
+       resolve(ternary, type, false);
+}
+
 void ExpressionResolver::visit(FunctionCall &call)
 {
        TraversingVisitor::visit(call);