changes: generated annotated code but it still causes type errors + re-formatting...
[IRC.git] / Robust / src / Analysis / SSJava / FlowDownCheck.java
index 2e879b18b0a18f32133f4bd509129704b16894f9..3d2459662bfe54f1be6895cd14a97d5fd91035b2 100644 (file)
@@ -332,7 +332,7 @@ public class FlowDownCheck {
     }
 
     // second, check return location annotation
-    if (!md.getReturnType().isVoid()) {
+    if (!md.getReturnType().isVoid() && !ssjava.getMethodContainingSSJavaLoop().equals(md)) {
       if (!hasReturnLocDeclaration) {
         // if developer does not define method lattice
         // search return location in the method default lattice
@@ -351,17 +351,24 @@ public class FlowDownCheck {
 
     }
 
-    if (!md.getReturnType().isVoid()) {
+    if (!md.getReturnType().isVoid() && !ssjava.getMethodContainingSSJavaLoop().equals(md)) {
       MethodLattice<String> methodLattice = ssjava.getMethodLattice(md);
       String thisLocId = methodLattice.getThisLoc();
-      if ((!md.isStatic()) && thisLocId == null) {
-        throw new Error("Method '" + md + "' does not have the definition of 'this' location at "
-            + md.getClassDesc().getSourceFileName());
+      if ((!md.isStatic())) {
+
+        if (thisLocId == null) {
+          throw new Error("Method '" + md + "' does not have the definition of 'this' location at "
+              + md.getClassDesc().getSourceFileName());
+        }
+
+        CompositeLocation thisLoc = new CompositeLocation(new Location(md, thisLocId));
+        paramList.add(0, thisLoc);
+
       }
-      CompositeLocation thisLoc = new CompositeLocation(new Location(md, thisLocId));
-      paramList.add(0, thisLoc);
+
       md2ReturnLocGen.put(md, new ReturnLocGenerator(md2ReturnLoc.get(md), md, paramList, md
           + " of " + cd.getSourceFileName()));
+
     }
 
     // fourth, check declarations inside of method
@@ -545,8 +552,14 @@ public class FlowDownCheck {
   private CompositeLocation checkLocationFromReturnNode(MethodDescriptor md, SymbolTable nametable,
       ReturnNode rn, CompositeLocation constraint) {
 
+    if (ssjava.getMethodContainingSSJavaLoop().equals(md)) {
+      return new CompositeLocation();
+    }
+
     ExpressionNode returnExp = rn.getReturnExpression();
 
+    CompositeLocation declaredReturnLoc = md2ReturnLoc.get(md);
+
     CompositeLocation returnValueLoc;
     if (returnExp != null) {
       returnValueLoc =
@@ -568,9 +581,40 @@ public class FlowDownCheck {
       // generateErrorMessage(md.getClassDesc(), rn));
       // }
 
+      if (constraint != null) {
+
+        // Set<CompositeLocation> inputGLB = new HashSet<CompositeLocation>();
+        // inputGLB.add(returnValueLoc);
+        // inputGLB.add(constraint);
+        // returnValueLoc =
+        // CompositeLattice.calculateGLB(inputGLB,
+        // generateErrorMessage(md.getClassDesc(), rn));
+
+        // if (!returnValueLoc.get(returnValueLoc.getSize() - 1).isTop()) {
+        // if (!CompositeLattice.isGreaterThan(constraint, returnValueLoc,
+        // generateErrorMessage(md.getClassDesc(), rn))) {
+        // System.out.println("returnValueLoc.get(returnValueLoc.getSize() - 1).isTop()="
+        // + returnValueLoc.get(returnValueLoc.getSize() - 1).isTop());
+        // throw new Error("The value flow from " + constraint + " to " +
+        // returnValueLoc
+        // + " does not respect location hierarchy on the assignment " +
+        // rn.printNode(0)
+        // + " at " + md.getClassDesc().getSourceFileName() + "::" +
+        // rn.getNumLine());
+        // }
+        // }
+
+        if (!CompositeLattice.isGreaterThan(constraint, declaredReturnLoc,
+            generateErrorMessage(md.getClassDesc(), rn))) {
+          throw new Error("The value flow from " + constraint + " to " + declaredReturnLoc
+              + " does not respect location hierarchy on the assignment " + rn.printNode(0)
+              + " at " + md.getClassDesc().getSourceFileName() + "::" + rn.getNumLine());
+        }
+
+      }
+
       // check if return value is equal or higher than RETRUNLOC of method
       // declaration annotation
-      CompositeLocation declaredReturnLoc = md2ReturnLoc.get(md);
 
       int compareResult =
           CompositeLattice.compare(returnValueLoc, declaredReturnLoc, false,
@@ -663,13 +707,14 @@ public class FlowDownCheck {
   private CompositeLocation checkLocationFromIfStatementNode(MethodDescriptor md,
       SymbolTable nametable, IfStatementNode isn, CompositeLocation constraint) {
 
+    System.out.println("\n\ncheckLocationFromIfStatementNode=" + isn.printNode(0));
     CompositeLocation condLoc =
         checkLocationFromExpressionNode(md, nametable, isn.getCondition(), new CompositeLocation(),
             constraint, false);
 
-    // addLocationType(isn.getCondition().getType(), condLoc);
-
+    System.out.println("-######old constraint=" + constraint);
     constraint = generateNewConstraint(constraint, condLoc);
+    System.out.println("-######new constraint=" + constraint);
     checkLocationFromBlockNode(md, nametable, isn.getTrueBlock(), constraint);
 
     if (isn.getFalseBlock() != null) {
@@ -869,24 +914,25 @@ public class FlowDownCheck {
     // System.out.println("# false=" + tn.getFalseExpr().printNode(0) + " Loc="
     // + falseLoc);
 
-    // check if condLoc is higher than trueLoc & falseLoc
-    if (!trueLoc.get(0).isTop()
-        && !CompositeLattice.isGreaterThan(condLoc, trueLoc, generateErrorMessage(cd, tn))) {
-      throw new Error(
-          "The location of the condition expression is lower than the true expression at "
-              + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
-    }
-
-    if (!falseLoc.get(0).isTop()
-        && !CompositeLattice.isGreaterThan(condLoc, falseLoc,
-            generateErrorMessage(cd, tn.getCond()))) {
-      throw new Error(
-          "The location of the condition expression is lower than the false expression at "
-              + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
-    }
+    // we don't need to check that condLoc is higher than trueLoc & falseLoc
+    // if (!trueLoc.get(0).isTop()
+    // && !CompositeLattice.isGreaterThan(condLoc, trueLoc, generateErrorMessage(cd, tn))) {
+    // throw new Error(
+    // "The location of the condition expression is lower than the true expression at "
+    // + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
+    // }
+    //
+    // if (!falseLoc.get(0).isTop()
+    // && !CompositeLattice.isGreaterThan(condLoc, falseLoc,
+    // generateErrorMessage(cd, tn.getCond()))) {
+    // throw new Error(
+    // "The location of the condition expression is lower than the false expression at "
+    // + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
+    // }
 
     // then, return glb of trueLoc & falseLoc
     Set<CompositeLocation> glbInputSet = new HashSet<CompositeLocation>();
+    glbInputSet.add(condLoc);
     glbInputSet.add(trueLoc);
     glbInputSet.add(falseLoc);
 
@@ -903,7 +949,7 @@ public class FlowDownCheck {
       CompositeLocation constraint) {
 
     ClassDescriptor cd = md.getClassDesc();
-    MethodDescriptor calleeMD = min.getMethod();
+    MethodDescriptor calleeMethodDesc = min.getMethod();
 
     NameDescriptor baseName = min.getBaseName();
     boolean isSystemout = false;
@@ -911,8 +957,9 @@ public class FlowDownCheck {
       isSystemout = baseName.getSymbol().equals("System.out");
     }
 
-    if (!ssjava.isSSJavaUtil(calleeMD.getClassDesc()) && !ssjava.isTrustMethod(calleeMD)
-        && !calleeMD.getModifiers().isNative() && !isSystemout) {
+    if (!ssjava.isSSJavaUtil(calleeMethodDesc.getClassDesc())
+        && !ssjava.isTrustMethod(calleeMethodDesc) && !calleeMethodDesc.getModifiers().isNative()
+        && !isSystemout) {
 
       CompositeLocation baseLocation = null;
       if (min.getExpression() != null) {
@@ -940,11 +987,14 @@ public class FlowDownCheck {
       // setup the location list of caller's arguments
       List<CompositeLocation> callerArgList = new ArrayList<CompositeLocation>();
 
+      // setup the location list of callee's parameters
+      MethodLattice<String> calleeLattice = ssjava.getMethodLattice(calleeMethodDesc);
+      List<CompositeLocation> calleeParamList = new ArrayList<CompositeLocation>();
+
       if (min.numArgs() > 0) {
-        // setup caller args set
-        // first, add caller's base(this) location
-        callerArgList.add(baseLocation);
-        // second, add caller's arguments
+        if (!calleeMethodDesc.isStatic()) {
+          callerArgList.add(baseLocation);
+        }
         for (int i = 0; i < min.numArgs(); i++) {
           ExpressionNode en = min.getArg(i);
           CompositeLocation callerArgLoc =
@@ -952,21 +1002,18 @@ public class FlowDownCheck {
                   constraint, false);
           callerArgList.add(callerArgLoc);
         }
-      }
 
-      // setup the location list of callee's parameters
-      MethodDescriptor calleemd = min.getMethod();
-      MethodLattice<String> calleeLattice = ssjava.getMethodLattice(calleemd);
-      CompositeLocation calleeThisLoc =
-          new CompositeLocation(new Location(calleemd, calleeLattice.getThisLoc()));
-      List<CompositeLocation> calleeParamList = new ArrayList<CompositeLocation>();
-      // first, add callee's this location
-      calleeParamList.add(calleeThisLoc);
-      // second, add callee's parameters
-      for (int i = 0; i < calleemd.numParameters(); i++) {
-        VarDescriptor calleevd = (VarDescriptor) calleemd.getParameter(i);
-        CompositeLocation calleeLoc = d2loc.get(calleevd);
-        calleeParamList.add(calleeLoc);
+        if (!calleeMethodDesc.isStatic()) {
+          CompositeLocation calleeThisLoc =
+              new CompositeLocation(new Location(calleeMethodDesc, calleeLattice.getThisLoc()));
+          calleeParamList.add(calleeThisLoc);
+        }
+
+        for (int i = 0; i < calleeMethodDesc.numParameters(); i++) {
+          VarDescriptor calleevd = (VarDescriptor) calleeMethodDesc.getParameter(i);
+          CompositeLocation calleeLoc = d2loc.get(calleevd);
+          calleeParamList.add(calleeLoc);
+        }
       }
 
       if (constraint != null) {
@@ -975,35 +1022,50 @@ public class FlowDownCheck {
         // annotation that declares the program counter that is higher than
         // corresponding parameter
 
-        CompositeLocation calleePCLOC = ssjava.getPCLocation(calleemd);
+        CompositeLocation calleePCLOC = ssjava.getPCLocation(calleeMethodDesc);
 
         for (int idx = 0; idx < callerArgList.size(); idx++) {
           CompositeLocation argLocation = callerArgList.get(idx);
 
-          int compareResult =
-              CompositeLattice
-                  .compare(argLocation, constraint, true, generateErrorMessage(cd, min));
+          // if the PC location is lower than an argument location
+          // then, need to check that the callee's the initial location for the PC is also lower
+          // than the corresponding parameter location
 
-          // need to check that param is higher than PCLOC
-          if (compareResult == ComparisonResult.GREATER) {
+          if (!argLocation.get(0).isTop()
+              && CompositeLattice.compare(argLocation, constraint, true,
+                  generateErrorMessage(cd, min)) == ComparisonResult.GREATER) {
 
             CompositeLocation paramLocation = calleeParamList.get(idx);
 
             int paramCompareResult =
-                CompositeLattice.compare(calleePCLOC, paramLocation, true,
+                CompositeLattice.compare(paramLocation, calleePCLOC, true,
                     generateErrorMessage(cd, min));
-
-            if (paramCompareResult == ComparisonResult.GREATER) {
+            // CompositeLattice.compare(calleePCLOC, paramLocation, true,
+            // generateErrorMessage(cd, min));
+
+            System.out.println("-CHECKPCLOC:");
+            System.out.println("---ARG LOC="
+                + argLocation
+                + " callerPCLOC="
+                + constraint
+                + " result="
+                + CompositeLattice.compare(argLocation, constraint, true,
+                    generateErrorMessage(cd, min)));
+            System.out.println("---PARAM LOC=" + paramLocation + " calleePCLOC=" + calleePCLOC
+                + " paramCompareResult=" + paramCompareResult);
+
+            if (!(paramLocation.get(0).equals(calleePCLOC.get(0)) && calleePCLOC.getSize() > 1)
+                && paramCompareResult == ComparisonResult.LESS) {
               throw new Error(
-                  "The program counter location "
-                      + constraint
-                      + " is lower than the argument(idx="
+                  "The argument(idx="
                       + idx
                       + ") location "
                       + argLocation
+                      + "is higher than the caller program counter location "
+                      + constraint
                       + ". Need to specify that the initial PC location of the callee, which is currently set to "
-                      + calleePCLOC + ", is lower than " + paramLocation + " in the method "
-                      + calleeMD.getSymbol() + ":" + min.getNumLine());
+                      + calleePCLOC + ", is not higher than " + paramLocation + " in the method "
+                      + calleeMethodDesc.getSymbol() + ":" + min.getNumLine());
             }
 
           }
@@ -1014,14 +1076,17 @@ public class FlowDownCheck {
 
       checkCalleeConstraints(md, nametable, min, baseLocation, constraint);
 
-      // checkCallerArgumentLocationConstraints(md, nametable, min,
-      // baseLocation, constraint);
+      checkCallerArgumentLocationConstraints(md, nametable, min, baseLocation, constraint);
 
       if (!min.getMethod().getReturnType().isVoid()) {
         // If method has a return value, compute the highest possible return
         // location in the caller's perspective
         CompositeLocation ceilingLoc =
             computeCeilingLocationForCaller(md, nametable, min, baseLocation, constraint);
+
+        if (ceilingLoc == null) {
+          return new CompositeLocation(Location.createTopLocation(md));
+        }
         return ceilingLoc;
       }
     }
@@ -1058,6 +1123,10 @@ public class FlowDownCheck {
 
     MethodDescriptor calleemd = min.getMethod();
 
+    if (calleemd.isStatic()) {
+      return;
+    }
+
     List<CompositeLocation> callerArgList = new ArrayList<CompositeLocation>();
     List<CompositeLocation> calleeParamList = new ArrayList<CompositeLocation>();
 
@@ -1105,7 +1174,16 @@ public class FlowDownCheck {
                   generateErrorMessage(md.getClassDesc(), min));
         }
 
-        if (!CompositeLattice.isGreaterThan(callerArgLoc, paramLocation, errorMsg)) {
+        Location argLastLoc = callerArgLoc.get(callerArgLoc.getSize() - 1);
+        Location paramLastLoc = paramLocation.get(paramLocation.getSize() - 1);
+
+        if (argLastLoc.equals(paramLastLoc) && ssjava.isSharedLocation(argLastLoc)
+            && ssjava.isSharedLocation(paramLastLoc)) {
+          continue;
+        }
+
+        // if (!CompositeLattice.isGreaterThan(callerArgLoc, paramLocation, errorMsg)) {
+        if (CompositeLattice.compare(callerArgLoc, paramLocation, true, errorMsg) == ComparisonResult.LESS) {
           throw new Error("Caller argument '" + min.getArg(i).printNode(0) + " : " + callerArgLoc
               + "' should be higher than corresponding callee's parameter : " + paramLocation
               + " at " + errorMsg);
@@ -1141,7 +1219,9 @@ public class FlowDownCheck {
     List<CompositeLocation> argList = new ArrayList<CompositeLocation>();
 
     // by default, method has a THIS parameter
-    argList.add(baseLocation);
+    if (!min.getMethod().isStatic()) {
+      argList.add(baseLocation);
+    }
 
     for (int i = 0; i < min.numArgs(); i++) {
       ExpressionNode en = min.getArg(i);
@@ -1151,8 +1231,7 @@ public class FlowDownCheck {
       argList.add(callerArg);
     }
 
-    // System.out.println("\n## computeReturnLocation=" + min.getMethod() +
-    // " argList=" + argList);
+    // System.out.println("\n## computeReturnLocation=" + min.getMethod() + " argList=" + argList);
     CompositeLocation ceilLoc = md2ReturnLocGen.get(min.getMethod()).computeReturnLocation(argList);
     // System.out.println("## ReturnLocation=" + ceilLoc);
 
@@ -1163,13 +1242,12 @@ public class FlowDownCheck {
   private void checkCalleeConstraints(MethodDescriptor md, SymbolTable nametable,
       MethodInvokeNode min, CompositeLocation callerBaseLoc, CompositeLocation constraint) {
 
-    // System.out.println("checkCalleeConstraints=" + min.printNode(0));
-
     MethodDescriptor calleemd = min.getMethod();
 
     MethodLattice<String> calleeLattice = ssjava.getMethodLattice(calleemd);
-    CompositeLocation calleeThisLoc =
-        new CompositeLocation(new Location(calleemd, calleeLattice.getThisLoc()));
+
+    System.out.println("checkCalleeConstraints=" + calleemd + " calleeLattice.getThisLoc()="
+        + calleeLattice.getThisLoc());
 
     List<CompositeLocation> callerArgList = new ArrayList<CompositeLocation>();
     List<CompositeLocation> calleeParamList = new ArrayList<CompositeLocation>();
@@ -1180,7 +1258,8 @@ public class FlowDownCheck {
 
       // setup caller args set
       // first, add caller's base(this) location
-      callerArgList.add(callerBaseLoc);
+      if (!calleemd.isStatic())
+        callerArgList.add(callerBaseLoc);
       // second, add caller's arguments
       for (int i = 0; i < min.numArgs(); i++) {
         ExpressionNode en = min.getArg(i);
@@ -1192,7 +1271,11 @@ public class FlowDownCheck {
 
       // setup callee params set
       // first, add callee's this location
-      calleeParamList.add(calleeThisLoc);
+      if (!calleemd.isStatic()) {
+        CompositeLocation calleeThisLoc =
+            new CompositeLocation(new Location(calleemd, calleeLattice.getThisLoc()));
+        calleeParamList.add(calleeThisLoc);
+      }
       // second, add callee's parameters
       for (int i = 0; i < calleemd.numParameters(); i++) {
         VarDescriptor calleevd = (VarDescriptor) calleemd.getParameter(i);
@@ -1247,16 +1330,24 @@ public class FlowDownCheck {
 
               String paramName1, paramName2;
 
-              if (i == 0) {
-                paramName1 = "'THIS'";
+              if (!calleemd.isStatic()) {
+                if (i == 0) {
+                  paramName1 = "'THIS'";
+                } else {
+                  paramName1 = "'parameter " + calleemd.getParamName(i - 1) + "'";
+                }
               } else {
-                paramName1 = "'parameter " + calleemd.getParamName(i - 1) + "'";
+                paramName1 = "'parameter " + calleemd.getParamName(i) + "'";
               }
 
-              if (j == 0) {
-                paramName2 = "'THIS'";
+              if (!calleemd.isStatic()) {
+                if (j == 0 && !calleemd.isStatic()) {
+                  paramName2 = "'THIS'";
+                } else {
+                  paramName2 = "'parameter " + calleemd.getParamName(j - 1) + "'";
+                }
               } else {
-                paramName2 = "'parameter " + calleemd.getParamName(j - 1) + "'";
+                paramName2 = "'parameter " + calleemd.getParamName(j) + "'";
               }
 
               throw new Error(
@@ -1275,18 +1366,19 @@ public class FlowDownCheck {
 
   private CompositeLocation checkLocationFromArrayAccessNode(MethodDescriptor md,
       SymbolTable nametable, ArrayAccessNode aan, CompositeLocation constraint, boolean isLHS) {
-
+    System.out.println("aan=" + aan.printNode(0) + "  line#=" + aan.getNumLine());
     ClassDescriptor cd = md.getClassDesc();
-
+    System.out.println("aan.getExpression()=" + aan.getExpression().getClass());
     CompositeLocation arrayLoc =
         checkLocationFromExpressionNode(md, nametable, aan.getExpression(),
             new CompositeLocation(), constraint, isLHS);
-
+    System.out.println("HERE?");
     // addTypeLocation(aan.getExpression().getType(), arrayLoc);
     CompositeLocation indexLoc =
         checkLocationFromExpressionNode(md, nametable, aan.getIndex(), new CompositeLocation(),
             constraint, isLHS);
     // addTypeLocation(aan.getIndex().getType(), indexLoc);
+    System.out.println("HERE2?");
 
     if (isLHS) {
       if (!CompositeLattice.isGreaterThan(indexLoc, arrayLoc, generateErrorMessage(cd, aan))) {
@@ -1298,7 +1390,11 @@ public class FlowDownCheck {
       Set<CompositeLocation> inputGLB = new HashSet<CompositeLocation>();
       inputGLB.add(arrayLoc);
       inputGLB.add(indexLoc);
-      return CompositeLattice.calculateGLB(inputGLB, generateErrorMessage(cd, aan));
+      System.out.println("arrayLoc=" + arrayLoc + "   indexLoc=" + indexLoc);
+      CompositeLocation comp =
+          CompositeLattice.calculateGLB(inputGLB, generateErrorMessage(cd, aan));
+      System.out.println("---aan=" + aan.printNode(0) + "  compLoc=" + comp);
+      return comp;
     }
 
   }
@@ -1330,12 +1426,9 @@ public class FlowDownCheck {
       // addTypeLocation(on.getRight().getType(), rightLoc);
     }
 
-    // System.out.println("\n# OP NODE=" + on.printNode(0));
-    // System.out.println("# left loc=" + leftLoc + " from " +
-    // on.getLeft().getClass());
+    // System.out.println("# left loc=" + leftLoc + " from " + on.getLeft().getClass());
     // if (on.getRight() != null) {
-    // System.out.println("# right loc=" + rightLoc + " from " +
-    // on.getRight().getClass());
+    // System.out.println("# right loc=" + rightLoc + " from " + on.getRight().getClass());
     // }
 
     Operation op = on.getOp();
@@ -1397,6 +1490,7 @@ public class FlowDownCheck {
   private CompositeLocation checkLocationFromNameNode(MethodDescriptor md, SymbolTable nametable,
       NameNode nn, CompositeLocation loc, CompositeLocation constraint) {
 
+    // System.out.println("checkLocationFromNameNode nn=" + nn.printNode(0));
     NameDescriptor nd = nn.getName();
     if (nd.getBase() != null) {
       loc =
@@ -1428,6 +1522,7 @@ public class FlowDownCheck {
       } else if (d instanceof FieldDescriptor) {
         // the type of field descriptor has a location!
         FieldDescriptor fd = (FieldDescriptor) d;
+        // System.out.println("fd=" + fd);
         if (fd.isStatic()) {
           if (fd.isFinal()) {
             // if it is 'static final', the location has TOP since no one can
@@ -1435,6 +1530,7 @@ public class FlowDownCheck {
             loc.addLocation(Location.createTopLocation(md));
             return loc;
           } else {
+
             // if 'static', the location has pre-assigned global loc
             MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
             String globalLocId = localLattice.getGlobalLoc();
@@ -1442,7 +1538,7 @@ public class FlowDownCheck {
               throw new Error("Global location element is not defined in the method " + md);
             }
             Location globalLoc = new Location(md, globalLocId);
-
+            System.out.println("static=" + globalLoc);
             loc.addLocation(globalLoc);
           }
         } else {
@@ -1474,6 +1570,7 @@ public class FlowDownCheck {
 
       }
     }
+
     return loc;
   }
 
@@ -1500,26 +1597,29 @@ public class FlowDownCheck {
       }
     }
 
+    Set<CompositeLocation> inputGLB = new HashSet<CompositeLocation>();
     if (left instanceof ArrayAccessNode) {
       ArrayAccessNode aan = (ArrayAccessNode) left;
-      left = aan.getExpression();
+      CompositeLocation indexLoc =
+          checkLocationFromExpressionNode(md, nametable, aan.getIndex(), loc, constraint, false);
+      inputGLB.add(indexLoc);
     }
 
     loc = checkLocationFromExpressionNode(md, nametable, left, loc, constraint, false);
-    // System.out.println("### checkLocationFromFieldAccessNode=" +
-    // fan.printNode(0));
-    // System.out.println("### left=" + left.printNode(0));
 
     if (!left.getType().isPrimitive()) {
 
-      if (fd.getSymbol().equals("length")) {
+      if (!fd.getSymbol().equals("length")) {
         // array.length access, return the location of the array
-        return loc;
+        Location fieldLoc = getFieldLocation(fd);
+        loc.addLocation(fieldLoc);
       }
 
-      Location fieldLoc = getFieldLocation(fd);
-      loc.addLocation(fieldLoc);
     }
+
+    inputGLB.add(loc);
+    loc = CompositeLattice.calculateGLB(inputGLB, generateErrorMessage(md.getClassDesc(), fan));
+
     return loc;
   }
 
@@ -1564,7 +1664,7 @@ public class FlowDownCheck {
   private CompositeLocation checkLocationFromAssignmentNode(MethodDescriptor md,
       SymbolTable nametable, AssignmentNode an, CompositeLocation loc, CompositeLocation constraint) {
 
-    // System.out.println("\n# ASSIGNMENTNODE=" + an.printNode(0));
+    System.out.println("\n\ncheckLocationFromAssignmentNode=" + an.printNode(0));
 
     ClassDescriptor cd = md.getClassDesc();
 
@@ -1593,20 +1693,31 @@ public class FlowDownCheck {
           checkLocationFromExpressionNode(md, nametable, an.getSrc(), new CompositeLocation(),
               constraint, false);
 
-      srcLocation = rhsLocation;
+      if (an.getOperation().getOp() >= 2 && an.getOperation().getOp() <= 12) {
+        // if assignment contains OP+EQ operator, need to merge location types
+        // of LHS & RHS into the RHS
+        Set<CompositeLocation> srcGLBSet = new HashSet<CompositeLocation>();
+        srcGLBSet.add(rhsLocation);
+        srcGLBSet.add(destLocation);
+        srcLocation = CompositeLattice.calculateGLB(srcGLBSet, generateErrorMessage(cd, an));
+      } else {
+        srcLocation = rhsLocation;
+      }
 
-      // if (!rhsLocation.get(rhsLocation.getSize() - 1).isTop()) {
       if (constraint != null) {
-        inputGLBSet.add(rhsLocation);
-        inputGLBSet.add(constraint);
-        srcLocation = CompositeLattice.calculateGLB(inputGLBSet, generateErrorMessage(cd, an));
+
+        if (!CompositeLattice.isGreaterThan(constraint, destLocation, generateErrorMessage(cd, an))) {
+          throw new Error("The value flow from " + constraint + " to " + destLocation
+              + " does not respect location hierarchy on the assignment " + an.printNode(0)
+              + " at " + cd.getSourceFileName() + "::" + an.getNumLine());
+        }
+        // inputGLBSet.add(srcLocation);
+        // inputGLBSet.add(constraint);
+        // srcLocation = CompositeLattice.calculateGLB(inputGLBSet,
+        // generateErrorMessage(cd, an));
       }
-      // }
 
-      // System.out.println("dstLocation=" + destLocation);
-      // System.out.println("rhsLocation=" + rhsLocation);
-      // System.out.println("srcLocation=" + srcLocation);
-      // System.out.println("constraint=" + constraint);
+      System.out.println("src=" + srcLocation + "  dest=" + destLocation + "  const=" + constraint);
 
       if (!CompositeLattice.isGreaterThan(srcLocation, destLocation, generateErrorMessage(cd, an))) {
 
@@ -1637,17 +1748,21 @@ public class FlowDownCheck {
                   constraint, false);
 
       if (constraint != null) {
-        inputGLBSet.add(rhsLocation);
-        inputGLBSet.add(constraint);
-        srcLocation = CompositeLattice.calculateGLB(inputGLBSet, generateErrorMessage(cd, an));
+
+        if (!CompositeLattice.isGreaterThan(constraint, destLocation, generateErrorMessage(cd, an))) {
+          throw new Error("The value flow from " + constraint + " to " + destLocation
+              + " does not respect location hierarchy on the assignment " + an.printNode(0)
+              + " at " + cd.getSourceFileName() + "::" + an.getNumLine());
+        }
+        // inputGLBSet.add(rhsLocation);
+        // inputGLBSet.add(constraint);
+        // srcLocation = CompositeLattice.calculateGLB(inputGLBSet,
+        // generateErrorMessage(cd, an));
+        srcLocation = rhsLocation;
       } else {
         srcLocation = rhsLocation;
       }
 
-      // System.out.println("srcLocation=" + srcLocation);
-      // System.out.println("rhsLocation=" + rhsLocation);
-      // System.out.println("constraint=" + constraint);
-
       if (!CompositeLattice.isGreaterThan(srcLocation, destLocation, generateErrorMessage(cd, an))) {
 
         if (srcLocation.equals(destLocation)) {
@@ -2010,7 +2125,11 @@ public class FlowDownCheck {
         } else if (lattice.isGreaterThan(loc1.getLocIdentifier(), loc2.getLocIdentifier())) {
           return ComparisonResult.GREATER;
         } else {
-          return ComparisonResult.LESS;
+          if (lattice.isComparable(loc1.getLocIdentifier(), loc2.getLocIdentifier())) {
+            return ComparisonResult.LESS;
+          } else {
+            return ComparisonResult.INCOMPARABLE;
+          }
         }
 
       }
@@ -2037,7 +2156,7 @@ public class FlowDownCheck {
 
     public static CompositeLocation calculateGLB(Set<CompositeLocation> inputSet, String errMsg) {
 
-      // System.out.println("Calculating GLB=" + inputSet);
+      System.out.println("Calculating GLB=" + inputSet);
       CompositeLocation glbCompLoc = new CompositeLocation();
 
       // calculate GLB of the first(priority) element
@@ -2050,6 +2169,7 @@ public class FlowDownCheck {
       // composite location
 
       int maxTupleSize = 0;
+      int minTupleSize = 0;
       CompositeLocation maxCompLoc = null;
 
       Location prevPriorityLoc = null;
@@ -2059,6 +2179,9 @@ public class FlowDownCheck {
           maxTupleSize = compLoc.getSize();
           maxCompLoc = compLoc;
         }
+        if (minTupleSize == 0 || compLoc.getSize() < minTupleSize) {
+          minTupleSize = compLoc.getSize();
+        }
         Location priorityLoc = compLoc.get(0);
         String priorityLocId = priorityLoc.getLocIdentifier();
         priorityLocIdentifierSet.add(priorityLocId);
@@ -2086,7 +2209,6 @@ public class FlowDownCheck {
 
       SSJavaLattice<String> locOrder = getLatticeByDescriptor(priorityDescriptor);
       String glbOfPriorityLoc = locOrder.getGLB(priorityLocIdentifierSet);
-
       glbCompLoc.addLocation(new Location(priorityDescriptor, glbOfPriorityLoc));
       Set<CompositeLocation> compSet = locId2CompLocSet.get(glbOfPriorityLoc);
 
@@ -2097,7 +2219,8 @@ public class FlowDownCheck {
 
         // in this case, do not take care about delta
         // CompositeLocation inputComp = inputSet.iterator().next();
-        for (int i = 1; i < maxTupleSize; i++) {
+        // for (int i = 1; i < maxTupleSize; i++) {
+        for (int i = 1; i < minTupleSize; i++) {
           glbCompLoc.addLocation(Location.createTopLocation(maxCompLoc.get(i).getDescriptor()));
         }
       } else {
@@ -2147,6 +2270,7 @@ public class FlowDownCheck {
           }
 
           if (innerGLBInput.size() > 0) {
+            // System.out.println("######innerGLBInput=" + innerGLBInput);
             CompositeLocation innerGLB = CompositeLattice.calculateGLB(innerGLBInput, errMsg);
             for (int idx = 0; idx < innerGLB.getSize(); idx++) {
               glbCompLoc.addLocation(innerGLB.get(idx));
@@ -2169,7 +2293,7 @@ public class FlowDownCheck {
         }
       }
 
-      // System.out.println("GLB=" + glbCompLoc);
+      System.out.println("GLB=" + glbCompLoc);
       return glbCompLoc;
 
     }
@@ -2348,6 +2472,9 @@ class ReturnLocGenerator {
       // compute GLB of arguments subset that are same or higher than return
       // location
       if (inputGLB.isEmpty()) {
+        if (args.size() == 0) {
+          return null;
+        }
         CompositeLocation rtr =
             new CompositeLocation(Location.createTopLocation(args.get(0).get(0).getDescriptor()));
         return rtr;