changes.
[IRC.git] / Robust / src / Analysis / SSJava / FlowDownCheck.java
index f1133415d9817e6530828c539c62e8c8a3111704..e2dc738475c1005fe4f14568f4c8109f369e6994 100644 (file)
@@ -300,35 +300,39 @@ public class FlowDownCheck {
       assignLocationOfVarDescriptor(vd, md, md.getParameterTable(), bn);
       paramList.add(d2loc.get(vd));
     }
+    Vector<AnnotationDescriptor> methodAnnotations = md.getModifiers().getAnnotations();
 
     // second, check return location annotation
     if (!md.getReturnType().isVoid()) {
       CompositeLocation returnLocComp = null;
-      Vector<AnnotationDescriptor> methodAnnotations = md.getModifiers().getAnnotations();
+
+      boolean hasReturnLocDeclaration = false;
       if (methodAnnotations != null) {
         for (int i = 0; i < methodAnnotations.size(); i++) {
           AnnotationDescriptor an = methodAnnotations.elementAt(i);
           if (an.getMarker().equals(ssjava.RETURNLOC)) {
-            // developer explicitly defines method lattice
+            // this case, developer explicitly defines method lattice
             String returnLocDeclaration = an.getValue();
             returnLocComp = parseLocationDeclaration(md, null, returnLocDeclaration);
-            md2ReturnLoc.put(md, returnLocComp);
+            hasReturnLocDeclaration = true;
           }
         }
       }
-      if (returnLocComp == null) {
-        MethodLattice<String> methodDefaultLattice = ssjava.getMethodDefaultLattice(cd);
-        if (methodDefaultLattice.getReturnLoc() != null) {
-          returnLocComp = parseLocationDeclaration(md, null, methodDefaultLattice.getReturnLoc());
-          md2ReturnLoc.put(md, returnLocComp);
-        }
+
+      if (!hasReturnLocDeclaration) {
+        // if developer does not define method lattice
+        // search return location in the method default lattice
+        String rtrStr = ssjava.getMethodLattice(md).getReturnLoc();
+        returnLocComp = new CompositeLocation(new Location(md, rtrStr));
       }
 
-      if (!md2ReturnLoc.containsKey(md)) {
+      if (returnLocComp == null) {
         throw new Error("Return location is not specified for the method " + md + " at "
             + cd.getSourceFileName());
       }
 
+      md2ReturnLoc.put(md, returnLocComp);
+
       // check this location
       MethodLattice<String> methodLattice = ssjava.getMethodLattice(md);
       String thisLocId = methodLattice.getThisLoc();
@@ -339,11 +343,13 @@ public class FlowDownCheck {
       CompositeLocation thisLoc = new CompositeLocation(new Location(md, thisLocId));
       paramList.add(0, thisLoc);
 
-      md2ReturnLocGen.put(md, new ReturnLocGenerator(md2ReturnLoc.get(md), paramList,
-          generateErrorMessage(cd, null)));
+      System.out.println("### ReturnLocGenerator=" + md);
+      System.out.println("### md2ReturnLoc.get(md)=" + md2ReturnLoc.get(md));
+      md2ReturnLocGen.put(md, new ReturnLocGenerator(md2ReturnLoc.get(md), md, paramList, md
+          + " of " + cd.getSourceFileName()));
     }
 
-    // third, check declarations inside of method
+    // fourth, check declarations inside of method
 
     checkDeclarationInBlockNode(md, md.getParameterTable(), bn);
 
@@ -537,7 +543,8 @@ public class FlowDownCheck {
         Set<CompositeLocation> inputGLB = new HashSet<CompositeLocation>();
         inputGLB.add(returnValueLoc);
         inputGLB.add(constraint);
-        returnValueLoc = CompositeLattice.calculateGLB(inputGLB);
+        returnValueLoc =
+            CompositeLattice.calculateGLB(inputGLB, generateErrorMessage(md.getClassDesc(), rn));
       }
 
       // check if return value is equal or higher than RETRUNLOC of method
@@ -545,7 +552,7 @@ public class FlowDownCheck {
       CompositeLocation declaredReturnLoc = md2ReturnLoc.get(md);
 
       int compareResult =
-          CompositeLattice.compare(returnValueLoc, declaredReturnLoc,
+          CompositeLattice.compare(returnValueLoc, declaredReturnLoc, false,
               generateErrorMessage(md.getClassDesc(), rn));
 
       if (compareResult == ComparisonResult.LESS || compareResult == ComparisonResult.INCOMPARABLE) {
@@ -621,7 +628,7 @@ public class FlowDownCheck {
       Set<CompositeLocation> inputSet = new HashSet<CompositeLocation>();
       inputSet.add(currentCon);
       inputSet.add(newCon);
-      return CompositeLattice.calculateGLB(inputSet);
+      return CompositeLattice.calculateGLB(inputSet, "");
     }
 
   }
@@ -818,25 +825,37 @@ public class FlowDownCheck {
     glbInputSet.add(trueLoc);
     glbInputSet.add(falseLoc);
 
-    return CompositeLattice.calculateGLB(glbInputSet);
+    return CompositeLattice.calculateGLB(glbInputSet, generateErrorMessage(cd, tn));
   }
 
   private CompositeLocation checkLocationFromMethodInvokeNode(MethodDescriptor md,
       SymbolTable nametable, MethodInvokeNode min, CompositeLocation loc,
       CompositeLocation constraint) {
 
-    checkCalleeConstraints(md, nametable, min, constraint);
-
     CompositeLocation baseLocation = null;
     if (min.getExpression() != null) {
       baseLocation =
           checkLocationFromExpressionNode(md, nametable, min.getExpression(),
               new CompositeLocation(), constraint, false);
     } else {
-      String thisLocId = ssjava.getMethodLattice(md).getThisLoc();
-      baseLocation = new CompositeLocation(new Location(md, thisLocId));
+
+      if (min.getMethod().isStatic()) {
+        String globalLocId = ssjava.getMethodLattice(md).getGlobalLoc();
+        if (globalLocId == null) {
+          throw new Error("Method lattice does not define global variable location at "
+              + generateErrorMessage(md.getClassDesc(), min));
+        }
+        baseLocation = new CompositeLocation(new Location(md, globalLocId));
+      } else {
+        String thisLocId = ssjava.getMethodLattice(md).getThisLoc();
+        baseLocation = new CompositeLocation(new Location(md, thisLocId));
+      }
     }
 
+    checkCalleeConstraints(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
@@ -849,6 +868,86 @@ public class FlowDownCheck {
 
   }
 
+  private void checkCallerArgumentLocationConstraints(MethodDescriptor md, SymbolTable nametable,
+      MethodInvokeNode min, CompositeLocation baseLoc, CompositeLocation constraint) {
+    // if parameter location consists of THIS and FIELD location,
+    // caller should pass an argument that is comparable to the declared
+    // parameter location
+    // and is not lower than the declared parameter location in the field
+    // lattice.
+
+    MethodDescriptor calleemd = min.getMethod();
+
+    List<CompositeLocation> callerArgList = new ArrayList<CompositeLocation>();
+    List<CompositeLocation> calleeParamList = new ArrayList<CompositeLocation>();
+
+    MethodLattice<String> calleeLattice = ssjava.getMethodLattice(calleemd);
+    Location calleeThisLoc = new Location(calleemd, calleeLattice.getThisLoc());
+
+    for (int i = 0; i < min.numArgs(); i++) {
+      ExpressionNode en = min.getArg(i);
+      CompositeLocation callerArgLoc =
+          checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation(), constraint,
+              false);
+      callerArgList.add(callerArgLoc);
+    }
+
+    // setup callee params set
+    for (int i = 0; i < calleemd.numParameters(); i++) {
+      VarDescriptor calleevd = (VarDescriptor) calleemd.getParameter(i);
+      CompositeLocation calleeLoc = d2loc.get(calleevd);
+      calleeParamList.add(calleeLoc);
+    }
+
+    String errorMsg = generateErrorMessage(md.getClassDesc(), min);
+
+    for (int i = 0; i < calleeParamList.size(); i++) {
+      CompositeLocation calleeParamLoc = calleeParamList.get(i);
+      if (calleeParamLoc.get(0).equals(calleeThisLoc) && calleeParamLoc.getSize() > 1) {
+        // callee parameter location has field information
+        CompositeLocation argLocation =
+            translateCallerLocToCallee(md, calleeThisLoc, callerArgList.get(i),errorMsg);
+
+        if (!CompositeLattice.isGreaterThan(argLocation, calleeParamLoc, errorMsg)) {
+          throw new Error("Caller argument '" + min.getArg(i).printNode(0)
+              + "' should be higher than corresponding callee's parameter at " + errorMsg);
+        }
+
+      }
+    }
+
+  }
+
+  private CompositeLocation translateCallerLocToCallee(MethodDescriptor md, Location calleeThisLoc,
+      CompositeLocation callerArgLoc,String errorMsg) {
+
+    ClassDescriptor calleeClassDesc = md.getClassDesc();
+    CompositeLocation translate = new CompositeLocation();
+
+    int startIdx = 0;
+    for (int i = 0; i < callerArgLoc.getSize(); i++) {
+      if (callerArgLoc.get(i).getDescriptor().equals(calleeClassDesc)) {
+        startIdx = i;
+      }
+    }
+
+    if (startIdx == 0) {
+      // caller arg location doesn't have field information
+      throw new Error("Caller argument location " + callerArgLoc
+          + " does not contain field information while callee has ordering constraints on field at "+errorMsg);
+    }
+
+    translate.addLocation(calleeThisLoc);
+
+    for (int i = startIdx + 1; i < callerArgLoc.getSize(); i++) {
+      translate.addLocation(callerArgLoc.get(i));
+    }
+
+    System.out.println("TRANSLATED=" + translate + " from callerArgLoc=" + callerArgLoc);
+
+    return translate;
+  }
+
   private CompositeLocation computeCeilingLocationForCaller(MethodDescriptor md,
       SymbolTable nametable, MethodInvokeNode min, CompositeLocation baseLocation,
       CompositeLocation constraint) {
@@ -875,55 +974,94 @@ public class FlowDownCheck {
   }
 
   private void checkCalleeConstraints(MethodDescriptor md, SymbolTable nametable,
-      MethodInvokeNode min, CompositeLocation constraint) {
+      MethodInvokeNode min, CompositeLocation callerBaseLoc, CompositeLocation constraint) {
+
+    MethodDescriptor calleemd = min.getMethod();
+
+    MethodLattice<String> calleeLattice = ssjava.getMethodLattice(calleemd);
+    CompositeLocation calleeThisLoc =
+        new CompositeLocation(new Location(calleemd, calleeLattice.getThisLoc()));
 
-    if (min.numArgs() > 1) {
+    List<CompositeLocation> callerArgList = new ArrayList<CompositeLocation>();
+    List<CompositeLocation> calleeParamList = new ArrayList<CompositeLocation>();
+
+    if (min.numArgs() > 0) {
       // caller needs to guarantee that it passes arguments in regarding to
       // callee's hierarchy
+
+      // setup caller args set
+      // first, add caller's base(this) location
+      callerArgList.add(callerBaseLoc);
+      // second, add caller's arguments
       for (int i = 0; i < min.numArgs(); i++) {
         ExpressionNode en = min.getArg(i);
-        CompositeLocation callerArg1 =
+        CompositeLocation callerArgLoc =
             checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation(), constraint,
                 false);
+        callerArgList.add(callerArgLoc);
+      }
+
+      // setup callee params set
+      // 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);
+      }
+
+      // here, check if ordering relations among caller's args respect
+      // ordering relations in-between callee's args
+      CHECK: for (int i = 0; i < calleeParamList.size(); i++) {
+        CompositeLocation calleeLoc1 = calleeParamList.get(i);
+        CompositeLocation callerLoc1 = callerArgList.get(i);
+
+        for (int j = 0; j < calleeParamList.size(); j++) {
+          if (i != j) {
+            CompositeLocation calleeLoc2 = calleeParamList.get(j);
+            CompositeLocation callerLoc2 = callerArgList.get(j);
 
-        VarDescriptor calleevd = (VarDescriptor) min.getMethod().getParameter(i);
-        CompositeLocation calleeLoc1 = d2loc.get(calleevd);
-
-        if (!callerArg1.get(0).isTop()) {
-          // here, check if ordering relations among caller's args respect
-          // ordering relations in-between callee's args
-          for (int currentIdx = 0; currentIdx < min.numArgs(); currentIdx++) {
-            if (currentIdx != i) { // skip itself
-              ExpressionNode argExp = min.getArg(currentIdx);
-
-              CompositeLocation callerArg2 =
-                  checkLocationFromExpressionNode(md, nametable, argExp, new CompositeLocation(),
-                      constraint, false);
-
-              VarDescriptor calleevd2 = (VarDescriptor) min.getMethod().getParameter(currentIdx);
-              CompositeLocation calleeLoc2 = d2loc.get(calleevd2);
-
-              int callerResult =
-                  CompositeLattice.compare(callerArg1, callerArg2,
-                      generateErrorMessage(md.getClassDesc(), min));
-              int calleeResult =
-                  CompositeLattice.compare(calleeLoc1, calleeLoc2,
-                      generateErrorMessage(md.getClassDesc(), min));
-
-              if (calleeResult == ComparisonResult.GREATER
-                  && callerResult != ComparisonResult.GREATER) {
-                // If calleeLoc1 is higher than calleeLoc2
-                // then, caller should have same ordering relation in-bet
-                // callerLoc1 & callerLoc2
-
-                throw new Error("Caller doesn't respect ordering relations among method arguments:"
-                    + md.getClassDesc().getSourceFileName() + ":" + min.getNumLine());
+            if (callerLoc1.get(callerLoc1.getSize() - 1).isTop()
+                || callerLoc2.get(callerLoc2.getSize() - 1).isTop()) {
+              continue CHECK;
+            }
+
+            int callerResult =
+                CompositeLattice.compare(callerLoc1, callerLoc2, true,
+                    generateErrorMessage(md.getClassDesc(), min));
+            int calleeResult =
+                CompositeLattice.compare(calleeLoc1, calleeLoc2, true,
+                    generateErrorMessage(md.getClassDesc(), min));
+
+            if (calleeResult == ComparisonResult.GREATER
+                && callerResult != ComparisonResult.GREATER) {
+              // If calleeLoc1 is higher than calleeLoc2
+              // then, caller should have same ordering relation in-bet
+              // callerLoc1 & callerLoc2
+
+              String paramName1, paramName2;
+
+              if (i == 0) {
+                paramName1 = "'THIS'";
+              } else {
+                paramName1 = "'parameter " + calleemd.getParamName(i - 1) + "'";
               }
 
+              if (j == 0) {
+                paramName2 = "'THIS'";
+              } else {
+                paramName2 = "'parameter " + calleemd.getParamName(j - 1) + "'";
+              }
+
+              throw new Error(
+                  "Caller doesn't respect an ordering relation among method arguments: callee expects that "
+                      + paramName1 + " should be higher than " + paramName2 + " in " + calleemd
+                      + " at " + md.getClassDesc().getSourceFileName() + ":" + min.getNumLine());
             }
           }
-        }
 
+        }
       }
 
     }
@@ -954,7 +1092,7 @@ public class FlowDownCheck {
       Set<CompositeLocation> inputGLB = new HashSet<CompositeLocation>();
       inputGLB.add(arrayLoc);
       inputGLB.add(indexLoc);
-      return CompositeLattice.calculateGLB(inputGLB);
+      return CompositeLattice.calculateGLB(inputGLB, generateErrorMessage(cd, aan));
     }
 
   }
@@ -1027,7 +1165,9 @@ public class FlowDownCheck {
       Set<CompositeLocation> inputSet = new HashSet<CompositeLocation>();
       inputSet.add(leftLoc);
       inputSet.add(rightLoc);
-      CompositeLocation glbCompLoc = CompositeLattice.calculateGLB(inputSet);
+      CompositeLocation glbCompLoc =
+          CompositeLattice.calculateGLB(inputSet, generateErrorMessage(cd, on));
+      System.out.println("# glbCompLoc=" + glbCompLoc);
       return glbCompLoc;
 
     default:
@@ -1067,6 +1207,7 @@ public class FlowDownCheck {
         Location locElement = new Location(md, thisLocId);
         loc.addLocation(locElement);
         return loc;
+
       }
 
       Descriptor d = (Descriptor) nametable.get(varname);
@@ -1108,22 +1249,17 @@ public class FlowDownCheck {
         Location fieldLoc = (Location) fd.getType().getExtension();
         loc.addLocation(fieldLoc);
       } else if (d == null) {
-
-        // check if the var is a static field of the class
-        FieldDescriptor fd = nn.getField();
+        // access static field
         ClassDescriptor cd = nn.getClassDesc();
 
-        if (fd != null && cd != null) {
-
-          if (fd.isStatic() && fd.isFinal()) {
-            loc.addLocation(Location.createTopLocation(md));
-            return loc;
-          } else {
-            MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
-            Location fieldLoc = new Location(md, localLattice.getThisLoc());
-            loc.addLocation(fieldLoc);
-          }
+        MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
+        String globalLocId = localLattice.getGlobalLoc();
+        if (globalLocId == null) {
+          throw new Error("Method lattice does not define global variable location at "
+              + generateErrorMessage(md.getClassDesc(), nn));
         }
+        loc.addLocation(new Location(md, globalLocId));
+        return loc;
 
       }
     }
@@ -1209,12 +1345,14 @@ public class FlowDownCheck {
       System.out.println("rhsLocation=" + rhsLocation);
       System.out.println("constraint=" + constraint);
 
-      if (constraint != null) {
-        inputGLBSet.add(rhsLocation);
-        inputGLBSet.add(constraint);
-        srcLocation = CompositeLattice.calculateGLB(inputGLBSet);
-      } else {
-        srcLocation = rhsLocation;
+      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(srcLocation, destLocation, generateErrorMessage(cd, an))) {
@@ -1232,11 +1370,15 @@ public class FlowDownCheck {
       if (constraint != null) {
         inputGLBSet.add(rhsLocation);
         inputGLBSet.add(constraint);
-        srcLocation = CompositeLattice.calculateGLB(inputGLBSet);
+        srcLocation = CompositeLattice.calculateGLB(inputGLBSet, generateErrorMessage(cd, an));
       } 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))) {
         throw new Error("Location " + destLocation
             + " is not allowed to have the value flow that moves within the same location at "
@@ -1309,9 +1451,10 @@ public class FlowDownCheck {
     return deltaLoc;
   }
 
-  private Location parseFieldLocDeclaraton(String decl, String msg) {
+  private Location parseFieldLocDeclaraton(String decl, String msg) throws Exception {
 
     int idx = decl.indexOf(".");
+
     String className = decl.substring(0, idx);
     String fieldName = decl.substring(idx + 1);
 
@@ -1321,6 +1464,7 @@ public class FlowDownCheck {
     Descriptor d = state.getClassSymbolTable().get(className);
 
     if (d == null) {
+      System.out.println("state.getClassSymbolTable()=" + state.getClassSymbolTable());
       throw new Error("The class in the location declaration '" + decl + "' does not exist at "
           + msg);
     }
@@ -1356,28 +1500,23 @@ public class FlowDownCheck {
     SSJavaLattice<String> localLattice = CompositeLattice.getLatticeByDescriptor(md);
     Location localLoc = new Location(md, localLocId);
     if (localLattice == null || (!localLattice.containsKey(localLocId))) {
+      System.out.println("locDec=" + locDec);
       throw new Error("Location " + localLocId
           + " is not defined in the local variable lattice at "
-          + md.getClassDesc().getSourceFileName() + "::" + (n != null ? n.getNumLine() : "") + ".");
+          + md.getClassDesc().getSourceFileName() + "::" + (n != null ? n.getNumLine() : md) + ".");
     }
     compLoc.addLocation(localLoc);
 
     for (int i = 1; i < locIdList.size(); i++) {
       String locName = locIdList.get(i);
-
-      Location fieldLoc =
-          parseFieldLocDeclaraton(locName, generateErrorMessage(md.getClassDesc(), n));
-      // ClassDescriptor cd = fieldLocName2cd.get(locName);
-      // SSJavaLattice<String> fieldLattice =
-      // CompositeLattice.getLatticeByDescriptor(cd);
-      //
-      // if (fieldLattice == null || (!fieldLattice.containsKey(locName))) {
-      // throw new Error("Location " + locName +
-      // " is not defined in the field lattice at "
-      // + cd.getSourceFileName() + ".");
-      // }
-      // Location fieldLoc = new Location(cd, locName);
-      compLoc.addLocation(fieldLoc);
+      try {
+        Location fieldLoc =
+            parseFieldLocDeclaraton(locName, generateErrorMessage(md.getClassDesc(), n));
+        compLoc.addLocation(fieldLoc);
+      } catch (Exception e) {
+        throw new Error("The location declaration '" + locName + "' is wrong  at "
+            + generateErrorMessage(md.getClassDesc(), n));
+      }
     }
 
     return compLoc;
@@ -1463,8 +1602,8 @@ public class FlowDownCheck {
 
     public static boolean isGreaterThan(CompositeLocation loc1, CompositeLocation loc2, String msg) {
 
-      System.out.println("isGreaterThan=" + loc1 + " " + loc2 + " msg=" + msg);
-      int baseCompareResult = compareBaseLocationSet(loc1, loc2, true, msg);
+      System.out.println("\nisGreaterThan=" + loc1 + " " + loc2 + " msg=" + msg);
+      int baseCompareResult = compareBaseLocationSet(loc1, loc2, true, false, msg);
       if (baseCompareResult == ComparisonResult.EQUAL) {
         if (compareDelta(loc1, loc2) == ComparisonResult.GREATER) {
           return true;
@@ -1479,10 +1618,11 @@ public class FlowDownCheck {
 
     }
 
-    public static int compare(CompositeLocation loc1, CompositeLocation loc2, String msg) {
+    public static int compare(CompositeLocation loc1, CompositeLocation loc2, boolean ignore,
+        String msg) {
 
       System.out.println("compare=" + loc1 + " " + loc2);
-      int baseCompareResult = compareBaseLocationSet(loc1, loc2, false, msg);
+      int baseCompareResult = compareBaseLocationSet(loc1, loc2, false, ignore, msg);
 
       if (baseCompareResult == ComparisonResult.EQUAL) {
         return compareDelta(loc1, loc2);
@@ -1514,7 +1654,7 @@ public class FlowDownCheck {
     }
 
     private static int compareBaseLocationSet(CompositeLocation compLoc1,
-        CompositeLocation compLoc2, boolean awareSharedLoc, String msg) {
+        CompositeLocation compLoc2, boolean awareSharedLoc, boolean ignore, String msg) {
 
       // if compLoc1 is greater than compLoc2, return true
       // else return false;
@@ -1524,93 +1664,30 @@ public class FlowDownCheck {
       for (int i = 0; i < compLoc1.getSize(); i++) {
         Location loc1 = compLoc1.get(i);
         if (i >= compLoc2.getSize()) {
-          throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
-              + " because they are not comparable.");
-        }
-        Location loc2 = compLoc2.get(i);
-
-        Descriptor d1 = loc1.getDescriptor();
-        Descriptor d2 = loc2.getDescriptor();
-
-        Descriptor descriptor;
-
-        if (d1 instanceof ClassDescriptor && d2 instanceof ClassDescriptor) {
-
-          if (d1.equals(d2)) {
-            descriptor = d1;
+          if (ignore) {
+            return ComparisonResult.INCOMPARABLE;
           } else {
-            // identifying which one is parent class
-            Set<Descriptor> d1SubClassesSet = ssjava.tu.getSubClasses((ClassDescriptor) d1);
-            Set<Descriptor> d2SubClassesSet = ssjava.tu.getSubClasses((ClassDescriptor) d2);
-
-            if (d1 == null && d2 == null) {
-              throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
-                  + " because they are not comparable at " + msg);
-            } else if (d1SubClassesSet != null && d1SubClassesSet.contains(d2)) {
-              descriptor = d1;
-            } else if (d2SubClassesSet != null && d2SubClassesSet.contains(d1)) {
-              descriptor = d2;
-            } else {
-              throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
-                  + " because they are not comparable at " + msg);
-            }
-          }
-
-        } else if (d1 instanceof MethodDescriptor && d2 instanceof MethodDescriptor) {
-
-          if (d1.equals(d2)) {
-            descriptor = d1;
-          } else {
-
-            // identifying which one is parent class
-            MethodDescriptor md1 = (MethodDescriptor) d1;
-            MethodDescriptor md2 = (MethodDescriptor) d2;
-
-            if (!md1.matches(md2)) {
-              throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
-                  + " because they are not comparable at " + msg);
-            }
-
-            Set<Descriptor> d1SubClassesSet =
-                ssjava.tu.getSubClasses(((MethodDescriptor) d1).getClassDesc());
-            Set<Descriptor> d2SubClassesSet =
-                ssjava.tu.getSubClasses(((MethodDescriptor) d2).getClassDesc());
-
-            if (d1 == null && d2 == null) {
-              throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
-                  + " because they are not comparable at " + msg);
-            } else if (d1 != null && d1SubClassesSet.contains(d2)) {
-              descriptor = d1;
-            } else if (d2 != null && d2SubClassesSet.contains(d1)) {
-              descriptor = d2;
-            } else {
-              throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
-                  + " because they are not comparable at " + msg);
-            }
+            throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
+                + " because they are not comparable at " + msg);
           }
-
-        } else {
-          throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
-              + " because they are not comparable at " + msg);
         }
+        Location loc2 = compLoc2.get(i);
 
-        // SSJavaLattice<String> lattice1 = getLatticeByDescriptor(d1);
-        // SSJavaLattice<String> lattice2 = getLatticeByDescriptor(d2);
-
+        Descriptor descriptor = getCommonParentDescriptor(loc1, loc2, msg);
         SSJavaLattice<String> lattice = getLatticeByDescriptor(descriptor);
 
-        // check if the spin location is appeared only at the end of the
+        // check if the shared location is appeared only at the end of the
         // composite location
-        if (lattice.getSpinLocSet().contains(loc1.getLocIdentifier())) {
+        if (lattice.getSharedLocSet().contains(loc1.getLocIdentifier())) {
           if (i != (compLoc1.getSize() - 1)) {
             throw new Error("The shared location " + loc1.getLocIdentifier()
                 + " cannot be appeared in the middle of composite location at" + msg);
           }
         }
 
-        if (lattice.getSpinLocSet().contains(loc2.getLocIdentifier())) {
+        if (lattice.getSharedLocSet().contains(loc2.getLocIdentifier())) {
           if (i != (compLoc2.getSize() - 1)) {
-            throw new Error("The spin location " + loc2.getLocIdentifier()
+            throw new Error("The shared location " + loc2.getLocIdentifier()
                 + " cannot be appeared in the middle of composite location at " + msg);
           }
         }
@@ -1627,7 +1704,7 @@ public class FlowDownCheck {
           // note that the spinning location only can be appeared in the last
           // part of the composite location
           if (awareSharedLoc && numOfTie == compLoc1.getSize()
-              && lattice.getSpinLocSet().contains(loc1.getLocIdentifier())) {
+              && lattice.getSharedLocSet().contains(loc1.getLocIdentifier())) {
             return ComparisonResult.GREATER;
           }
           continue;
@@ -1642,8 +1719,14 @@ public class FlowDownCheck {
       if (numOfTie == compLoc1.getSize()) {
 
         if (numOfTie != compLoc2.getSize()) {
-          throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
-              + " because they are not comparable at " + msg);
+
+          if (ignore) {
+            return ComparisonResult.INCOMPARABLE;
+          } else {
+            throw new Error("Failed to compare two locations of " + compLoc1 + " and " + compLoc2
+                + " because they are not comparable at " + msg);
+          }
+
         }
 
         return ComparisonResult.EQUAL;
@@ -1653,9 +1736,9 @@ public class FlowDownCheck {
 
     }
 
-    public static CompositeLocation calculateGLB(Set<CompositeLocation> inputSet) {
+    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
@@ -1670,6 +1753,7 @@ public class FlowDownCheck {
       int maxTupleSize = 0;
       CompositeLocation maxCompLoc = null;
 
+      Location prevPriorityLoc = null;
       for (Iterator iterator = inputSet.iterator(); iterator.hasNext();) {
         CompositeLocation compLoc = (CompositeLocation) iterator.next();
         if (compLoc.getSize() > maxTupleSize) {
@@ -1691,10 +1775,14 @@ public class FlowDownCheck {
         // check if priority location are coming from the same lattice
         if (priorityDescriptor == null) {
           priorityDescriptor = priorityLoc.getDescriptor();
-        } else if (!priorityDescriptor.equals(priorityLoc.getDescriptor())) {
-          throw new Error("Failed to calculate GLB of " + inputSet
-              + " because they are from different lattices.");
+        } else {
+          priorityDescriptor = getCommonParentDescriptor(priorityLoc, prevPriorityLoc, errMsg);
         }
+        prevPriorityLoc = priorityLoc;
+        // else if (!priorityDescriptor.equals(priorityLoc.getDescriptor())) {
+        // throw new Error("Failed to calculate GLB of " + inputSet
+        // + " because they are from different lattices.");
+        // }
       }
 
       SSJavaLattice<String> locOrder = getLatticeByDescriptor(priorityDescriptor);
@@ -1729,7 +1817,6 @@ public class FlowDownCheck {
         }
 
         if (compSet.size() == 1) {
-
           // if GLB(x1,x2)==x1 or x2 : GLB case 2,3
           CompositeLocation comp = compSet.iterator().next();
           for (int i = 1; i < comp.getSize(); i++) {
@@ -1747,26 +1834,24 @@ public class FlowDownCheck {
           // if more than one location shares the same priority GLB
           // need to calculate the rest of GLB loc
 
-          // int compositeLocSize = compSet.iterator().next().getSize();
-          int compositeLocSize = maxFromCompSet.getSize();
-
-          Set<String> glbInputSet = new HashSet<String>();
-          Descriptor currentD = null;
-          for (int i = 1; i < compositeLocSize; i++) {
-            for (Iterator iterator = compSet.iterator(); iterator.hasNext();) {
-              CompositeLocation compositeLocation = (CompositeLocation) iterator.next();
-              if (compositeLocation.getSize() > i) {
-                Location currentLoc = compositeLocation.get(i);
-                currentD = currentLoc.getDescriptor();
-                // making set of the current location sharing the same idx
-                glbInputSet.add(currentLoc.getLocIdentifier());
-              }
+          // setup input set starting from the second tuple item
+          Set<CompositeLocation> innerGLBInput = new HashSet<CompositeLocation>();
+          for (Iterator iterator = compSet.iterator(); iterator.hasNext();) {
+            CompositeLocation compLoc = (CompositeLocation) iterator.next();
+            CompositeLocation innerCompLoc = new CompositeLocation();
+            for (int idx = 1; idx < compLoc.getSize(); idx++) {
+              innerCompLoc.addLocation(compLoc.get(idx));
             }
-            // calculate glb for the current lattice
+            if (innerCompLoc.getSize() > 0) {
+              innerGLBInput.add(innerCompLoc);
+            }
+          }
 
-            SSJavaLattice<String> currentLattice = getLatticeByDescriptor(currentD);
-            String currentGLBLocId = currentLattice.getGLB(glbInputSet);
-            glbCompLoc.addLocation(new Location(currentD, currentGLBLocId));
+          if (innerGLBInput.size() > 0) {
+            CompositeLocation innerGLB = CompositeLattice.calculateGLB(innerGLBInput, errMsg);
+            for (int idx = 0; idx < innerGLB.getSize(); idx++) {
+              glbCompLoc.addLocation(innerGLB.get(idx));
+            }
           }
 
           // if input location corresponding to glb is a delta, need to apply
@@ -1785,6 +1870,7 @@ public class FlowDownCheck {
         }
       }
 
+      System.out.println("GLB=" + glbCompLoc);
       return glbCompLoc;
 
     }
@@ -1807,6 +1893,77 @@ public class FlowDownCheck {
       return lattice;
     }
 
+    static Descriptor getCommonParentDescriptor(Location loc1, Location loc2, String msg) {
+
+      Descriptor d1 = loc1.getDescriptor();
+      Descriptor d2 = loc2.getDescriptor();
+
+      Descriptor descriptor;
+
+      if (d1 instanceof ClassDescriptor && d2 instanceof ClassDescriptor) {
+
+        if (d1.equals(d2)) {
+          descriptor = d1;
+        } else {
+          // identifying which one is parent class
+          Set<Descriptor> d1SubClassesSet = ssjava.tu.getSubClasses((ClassDescriptor) d1);
+          Set<Descriptor> d2SubClassesSet = ssjava.tu.getSubClasses((ClassDescriptor) d2);
+
+          if (d1 == null && d2 == null) {
+            throw new Error("Failed to compare two locations of " + loc1 + " and " + loc2
+                + " because they are not comparable at " + msg);
+          } else if (d1SubClassesSet != null && d1SubClassesSet.contains(d2)) {
+            descriptor = d1;
+          } else if (d2SubClassesSet != null && d2SubClassesSet.contains(d1)) {
+            descriptor = d2;
+          } else {
+            throw new Error("Failed to compare two locations of " + loc1 + " and " + loc2
+                + " because they are not comparable at " + msg);
+          }
+        }
+
+      } else if (d1 instanceof MethodDescriptor && d2 instanceof MethodDescriptor) {
+
+        if (d1.equals(d2)) {
+          descriptor = d1;
+        } else {
+
+          // identifying which one is parent class
+          MethodDescriptor md1 = (MethodDescriptor) d1;
+          MethodDescriptor md2 = (MethodDescriptor) d2;
+
+          if (!md1.matches(md2)) {
+            throw new Error("Failed to compare two locations of " + loc1 + " and " + loc2
+                + " because they are not comparable at " + msg);
+          }
+
+          Set<Descriptor> d1SubClassesSet =
+              ssjava.tu.getSubClasses(((MethodDescriptor) d1).getClassDesc());
+          Set<Descriptor> d2SubClassesSet =
+              ssjava.tu.getSubClasses(((MethodDescriptor) d2).getClassDesc());
+
+          if (d1 == null && d2 == null) {
+            throw new Error("Failed to compare two locations of " + loc1 + " and " + loc2
+                + " because they are not comparable at " + msg);
+          } else if (d1 != null && d1SubClassesSet.contains(d2)) {
+            descriptor = d1;
+          } else if (d2 != null && d2SubClassesSet.contains(d1)) {
+            descriptor = d2;
+          } else {
+            throw new Error("Failed to compare two locations of " + loc1 + " and " + loc2
+                + " because they are not comparable at " + msg);
+          }
+        }
+
+      } else {
+        throw new Error("Failed to compare two locations of " + loc1 + " and " + loc2
+            + " because they are not comparable at " + msg);
+      }
+
+      return descriptor;
+
+    }
+
   }
 
   class ComparisonResult {
@@ -1827,50 +1984,77 @@ class ReturnLocGenerator {
   public static final int PARAMISSAME = 1;
   public static final int IGNORE = 2;
 
-  Hashtable<Integer, Integer> paramIdx2paramType;
+  private Hashtable<Integer, Integer> paramIdx2paramType;
 
-  public ReturnLocGenerator(CompositeLocation returnLoc, List<CompositeLocation> params, String msg) {
-    // creating mappings
-    paramIdx2paramType = new Hashtable<Integer, Integer>();
-    for (int i = 0; i < params.size(); i++) {
-      CompositeLocation param = params.get(i);
-      int compareResult = CompositeLattice.compare(param, returnLoc, msg);
+  private CompositeLocation declaredReturnLoc = null;
 
-      int type;
-      if (compareResult == ComparisonResult.GREATER) {
-        type = 0;
-      } else if (compareResult == ComparisonResult.EQUAL) {
-        type = 1;
-      } else {
-        type = 2;
+  public ReturnLocGenerator(CompositeLocation returnLoc, MethodDescriptor md,
+      List<CompositeLocation> params, String msg) {
+
+    CompositeLocation thisLoc = params.get(0);
+    if (returnLoc.get(0).equals(thisLoc.get(0)) && returnLoc.getSize() > 1) {
+      // if the declared return location consists of THIS and field location,
+      // return location for the caller's side has to have same field element
+      this.declaredReturnLoc = returnLoc;
+    } else {
+      // creating mappings
+      paramIdx2paramType = new Hashtable<Integer, Integer>();
+      for (int i = 0; i < params.size(); i++) {
+        CompositeLocation param = params.get(i);
+        int compareResult = CompositeLattice.compare(param, returnLoc, true, msg);
+
+        int type;
+        if (compareResult == ComparisonResult.GREATER) {
+          type = 0;
+        } else if (compareResult == ComparisonResult.EQUAL) {
+          type = 1;
+        } else {
+          type = 2;
+        }
+        paramIdx2paramType.put(new Integer(i), new Integer(type));
       }
-      paramIdx2paramType.put(new Integer(i), new Integer(type));
     }
 
   }
 
   public CompositeLocation computeReturnLocation(List<CompositeLocation> args) {
 
-    // compute the highest possible location in caller's side
-    assert paramIdx2paramType.keySet().size() == args.size();
-
-    Set<CompositeLocation> inputGLB = new HashSet<CompositeLocation>();
-    for (int i = 0; i < args.size(); i++) {
-      int type = (paramIdx2paramType.get(new Integer(i))).intValue();
-      CompositeLocation argLoc = args.get(i);
-      if (type == PARAMISHIGHER) {
-        // return loc is lower than param
-        DeltaLocation delta = new DeltaLocation(argLoc, 1);
-        inputGLB.add(delta);
-      } else if (type == PARAMISSAME) {
-        // return loc is equal or lower than param
-        inputGLB.add(argLoc);
+    if (declaredReturnLoc != null) {
+      // when developer specify that the return value is [THIS,field]
+      // needs to translate to the caller's location
+      CompositeLocation callerLoc = new CompositeLocation();
+      CompositeLocation callerBaseLocation = args.get(0);
+
+      for (int i = 0; i < callerBaseLocation.getSize(); i++) {
+        callerLoc.addLocation(callerBaseLocation.get(i));
+      }
+      for (int i = 1; i < declaredReturnLoc.getSize(); i++) {
+        callerLoc.addLocation(declaredReturnLoc.get(i));
+      }
+      return callerLoc;
+    } else {
+      // compute the highest possible location in caller's side
+      assert paramIdx2paramType.keySet().size() == args.size();
+
+      Set<CompositeLocation> inputGLB = new HashSet<CompositeLocation>();
+      for (int i = 0; i < args.size(); i++) {
+        int type = (paramIdx2paramType.get(new Integer(i))).intValue();
+        CompositeLocation argLoc = args.get(i);
+        if (type == PARAMISHIGHER) {
+          // return loc is lower than param
+          DeltaLocation delta = new DeltaLocation(argLoc, 1);
+          inputGLB.add(delta);
+        } else if (type == PARAMISSAME) {
+          // return loc is equal or lower than param
+          inputGLB.add(argLoc);
+        }
       }
+
+      // compute GLB of arguments subset that are same or higher than return
+      // location
+      CompositeLocation glb = CompositeLattice.calculateGLB(inputGLB, "");
+      return glb;
     }
 
-    // compute GLB of arguments subset that are same or higher than return
-    // location
-    CompositeLocation glb = CompositeLattice.calculateGLB(inputGLB);
-    return glb;
   }
 }