From 86287ef999b64f5678fa1b1f31d1cf63e807b1a7 Mon Sep 17 00:00:00 2001
From: kishida <naokikishida@gmail.com>
Date: Sat, 5 Sep 2015 21:20:09 +0900
Subject: [PATCH] Add parenthesis only when their needed as same as
 BinaryOperator.

---
 .../aparapi/internal/writer/BlockWriter.java  | 51 +++++++++++--------
 1 file changed, 31 insertions(+), 20 deletions(-)

diff --git a/com.amd.aparapi/src/java/com/amd/aparapi/internal/writer/BlockWriter.java b/com.amd.aparapi/src/java/com/amd/aparapi/internal/writer/BlockWriter.java
index 82031e2e..613e9401 100644
--- a/com.amd.aparapi/src/java/com/amd/aparapi/internal/writer/BlockWriter.java
+++ b/com.amd.aparapi/src/java/com/amd/aparapi/internal/writer/BlockWriter.java
@@ -364,14 +364,19 @@ public abstract class BlockWriter{
 
    public void writeInstruction(Instruction _instruction) throws CodeGenException {
       if (_instruction instanceof CompositeIfElseInstruction) {
-         write("(");
+         boolean needParenthesis = isNeedParenthesis(_instruction);
+         if(needParenthesis){
+             write("(");
+         }
          write("(");
          final Instruction lhs = writeConditional(((CompositeInstruction) _instruction).getBranchSet());
          write(")?");
          writeInstruction(lhs);
          write(":");
          writeInstruction(lhs.getNextExpr().getNextExpr());
-         write(")");
+         if(needParenthesis){
+             write(")");
+         }
       } else if (_instruction instanceof CompositeInstruction) {
          writeComposite((CompositeInstruction) _instruction);
 
@@ -558,24 +563,7 @@ public abstract class BlockWriter{
       } else if (_instruction instanceof BinaryOperator) {
          final BinaryOperator binaryInstruction = (BinaryOperator) _instruction;
          final Instruction parent = binaryInstruction.getParentExpr();
-         boolean needsParenthesis = true;
-
-         if (parent instanceof AssignToLocalVariable) {
-            needsParenthesis = false;
-         } else if (parent instanceof AssignToField) {
-            needsParenthesis = false;
-         } else if (parent instanceof AssignToArrayElement) {
-            needsParenthesis = false;
-         } else {
-            /**
-                        if (parent instanceof BinaryOperator) {
-                           BinaryOperator parentBinaryOperator = (BinaryOperator) parent;
-                           if (parentBinaryOperator.getOperator().ordinal() > binaryInstruction.getOperator().ordinal()) {
-                              needsParenthesis = false;
-                           }
-                        }
-            **/
-         }
+         boolean needsParenthesis = isNeedParenthesis(binaryInstruction);
 
          if (needsParenthesis) {
             write("(");
@@ -731,6 +719,29 @@ public abstract class BlockWriter{
 
    }
 
+   private boolean isNeedParenthesis(Instruction instruction){
+        final Instruction parent = instruction.getParentExpr();
+        boolean needsParenthesis = true;
+
+        if (parent instanceof AssignToLocalVariable) {
+           needsParenthesis = false;
+        } else if (parent instanceof AssignToField) {
+           needsParenthesis = false;
+        } else if (parent instanceof AssignToArrayElement) {
+           needsParenthesis = false;
+        } else {
+           /**
+                       if (parent instanceof BinaryOperator) {
+                          BinaryOperator parentBinaryOperator = (BinaryOperator) parent;
+                          if (parentBinaryOperator.getOperator().ordinal() > binaryInstruction.getOperator().ordinal()) {
+                             needsParenthesis = false;
+                          }
+                       }
+           **/
+        }
+        return needsParenthesis;
+   }
+
    private boolean isMultiDimensionalArray(NameAndTypeEntry nameAndTypeEntry) {
       return nameAndTypeEntry.getDescriptorUTF8Entry().getUTF8().startsWith("[[");
    }
-- 
GitLab