add PCLOC annotations. all three benchmarks are type-checked now.
[IRC.git] / Robust / src / Analysis / SSJava / FlowDownCheck.java
index 0a1752c65f86896b1f66b0b8e6be2e12cea86abc..a16a6e9ff784ad5da0624a5a3d7a0c2e4d79e138 100644 (file)
@@ -11,7 +11,6 @@ import java.util.Set;
 import java.util.StringTokenizer;
 import java.util.Vector;
 
-
 import Analysis.SSJava.FlowDownCheck.ComparisonResult;
 import Analysis.SSJava.FlowDownCheck.CompositeLattice;
 import IR.AnnotationDescriptor;
@@ -24,7 +23,9 @@ import IR.Operation;
 import IR.State;
 import IR.SymbolTable;
 import IR.TypeDescriptor;
+import IR.TypeExtension;
 import IR.VarDescriptor;
+import IR.Flat.FlatNode;
 import IR.Tree.ArrayAccessNode;
 import IR.Tree.AssignmentNode;
 import IR.Tree.BlockExpressionNode;
@@ -137,7 +138,7 @@ public class FlowDownCheck {
       toanalyzeList.addAll(classtable.getValueSet());
       Collections.sort(toanalyzeList, new Comparator<ClassDescriptor>() {
         public int compare(ClassDescriptor o1, ClassDescriptor o2) {
-          return o1.getClassName().compareTo(o2.getClassName());
+          return o1.getClassName().compareToIgnoreCase(o2.getClassName());
         }
       });
     } else {
@@ -154,7 +155,7 @@ public class FlowDownCheck {
       toanalyzeMethodList.addAll(methodtable.getValueSet());
       Collections.sort(toanalyzeMethodList, new Comparator<MethodDescriptor>() {
         public int compare(MethodDescriptor o1, MethodDescriptor o2) {
-          return o1.getSymbol().compareTo(o2.getSymbol());
+          return o1.getSymbol().compareToIgnoreCase(o2.getSymbol());
         }
       });
     } else {
@@ -222,7 +223,11 @@ public class FlowDownCheck {
       while (!toAnalyzeMethodIsEmpty()) {
         MethodDescriptor md = toAnalyzeMethodNext();
         if (ssjava.needTobeAnnotated(md)) {
-          checkMethodBody(cd, md);
+          if (state.SSJAVADEBUG) {
+            System.out.println("SSJAVA: Checking Flow-down Rules: " + md);
+          }
+          CompositeLocation calleePCLOC = ssjava.getPCLocation(md);
+          checkMethodBody(cd, md, calleePCLOC);
         }
       }
     }
@@ -292,58 +297,77 @@ public class FlowDownCheck {
   private void checkDeclarationInMethodBody(ClassDescriptor cd, MethodDescriptor md) {
     BlockNode bn = state.getMethodBody(md);
 
-    if (ssjava.needTobeAnnotated(md)) {
-      // first, check annotations on method declaration
-
-      // parsing returnloc annotation
-      Vector<AnnotationDescriptor> methodAnnotations = md.getModifiers().getAnnotations();
-      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
-            String returnLocDeclaration = an.getValue();
-            CompositeLocation returnLocComp =
-                parseLocationDeclaration(md, null, returnLocDeclaration);
-            md2ReturnLoc.put(md, returnLocComp);
-          }
-        }
-        if (!md.getReturnType().isVoid() && !md2ReturnLoc.containsKey(md)) {
-          throw new Error("Return location is not specified for the method " + md + " at "
-              + cd.getSourceFileName());
+    // first, check annotations on method parameters
+    List<CompositeLocation> paramList = new ArrayList<CompositeLocation>();
+    for (int i = 0; i < md.numParameters(); i++) {
+      // process annotations on method parameters
+      VarDescriptor vd = (VarDescriptor) md.getParameter(i);
+      assignLocationOfVarDescriptor(vd, md, md.getParameterTable(), null);
+      paramList.add(d2loc.get(vd));
+    }
+    Vector<AnnotationDescriptor> methodAnnotations = md.getModifiers().getAnnotations();
+
+    CompositeLocation returnLocComp = null;
+
+    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)) {
+          // this case, developer explicitly defines method lattice
+          String returnLocDeclaration = an.getValue();
+          returnLocComp = parseLocationDeclaration(md, null, returnLocDeclaration);
+          hasReturnLocDeclaration = true;
+        } else if (an.getMarker().equals(ssjava.THISLOC)) {
+          String thisLoc = an.getValue();
+          ssjava.getMethodLattice(md).setThisLoc(thisLoc);
+        } else if (an.getMarker().equals(ssjava.GLOBALLOC)) {
+          String globalLoc = an.getValue();
+          ssjava.getMethodLattice(md).setGlobalLoc(globalLoc);
+        } else if (an.getMarker().equals(ssjava.PCLOC)) {
+          String pcLocDeclaration = an.getValue();
+          ssjava.setPCLocation(md, parseLocationDeclaration(md, null, pcLocDeclaration));
         }
       }
+    }
 
-      List<CompositeLocation> paramList = new ArrayList<CompositeLocation>();
-
-      boolean hasReturnValue = (!md.getReturnType().isVoid());
-      if (hasReturnValue) {
-        MethodLattice<String> methodLattice = ssjava.getMethodLattice(md);
-        String thisLocId = methodLattice.getThisLoc();
-        if (thisLocId == null) {
-          throw new Error("Method '" + md + "' does not have the definition of 'this' location at "
-              + md.getClassDesc().getSourceFileName());
+    // second, check return location annotation
+    if (!md.getReturnType().isVoid()) {
+      if (!hasReturnLocDeclaration) {
+        // if developer does not define method lattice
+        // search return location in the method default lattice
+        String rtrStr = ssjava.getMethodLattice(md).getReturnLoc();
+        if (rtrStr != null) {
+          returnLocComp = new CompositeLocation(new Location(md, rtrStr));
         }
-        CompositeLocation thisLoc = new CompositeLocation(new Location(md, thisLocId));
-        paramList.add(thisLoc);
       }
 
-      for (int i = 0; i < md.numParameters(); i++) {
-        // process annotations on method parameters
-        VarDescriptor vd = (VarDescriptor) md.getParameter(i);
-        assignLocationOfVarDescriptor(vd, md, md.getParameterTable(), bn);
-        if (hasReturnValue) {
-          paramList.add(d2loc.get(vd));
-        }
+      if (returnLocComp == null) {
+        throw new Error("Return location is not specified for the method " + md + " at "
+            + cd.getSourceFileName());
       }
 
-      if (hasReturnValue) {
-        md2ReturnLocGen.put(md, new ReturnLocGenerator(md2ReturnLoc.get(md), paramList,
-            generateErrorMessage(cd, null)));
+      md2ReturnLoc.put(md, returnLocComp);
+
+    }
+
+    if (!md.getReturnType().isVoid()) {
+      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());
       }
-      checkDeclarationInBlockNode(md, md.getParameterTable(), bn);
+      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
+
+    checkDeclarationInBlockNode(md, md.getParameterTable(), bn);
+
   }
 
   private void checkDeclarationInBlockNode(MethodDescriptor md, SymbolTable nametable, BlockNode bn) {
@@ -422,9 +446,10 @@ public class FlowDownCheck {
     checkDeclarationInBlockNode(md, nametable, ln.getBody());
   }
 
-  private void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
+  private void checkMethodBody(ClassDescriptor cd, MethodDescriptor md,
+      CompositeLocation constraints) {
     BlockNode bn = state.getMethodBody(md);
-    checkLocationFromBlockNode(md, md.getParameterTable(), bn, null);
+    checkLocationFromBlockNode(md, md.getParameterTable(), bn, constraints);
   }
 
   private String generateErrorMessage(ClassDescriptor cd, TreeNode tn) {
@@ -528,12 +553,27 @@ public class FlowDownCheck {
           checkLocationFromExpressionNode(md, nametable, returnExp, new CompositeLocation(),
               constraint, false);
 
+      // System.out.println("# RETURN VALUE LOC=" + returnValueLoc +
+      // " with constraint=" + constraint);
+
+      // TODO: do we need to check here?
+      // if this return statement is inside branch, return value has an implicit
+      // flow from conditional location
+      // if (constraint != null) {
+      // Set<CompositeLocation> inputGLB = new HashSet<CompositeLocation>();
+      // inputGLB.add(returnValueLoc);
+      // inputGLB.add(constraint);
+      // returnValueLoc =
+      // CompositeLattice.calculateGLB(inputGLB,
+      // generateErrorMessage(md.getClassDesc(), rn));
+      // }
+
       // 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,
+          CompositeLattice.compare(returnValueLoc, declaredReturnLoc, false,
               generateErrorMessage(md.getClassDesc(), rn));
 
       if (compareResult == ComparisonResult.LESS || compareResult == ComparisonResult.INCOMPARABLE) {
@@ -563,7 +603,7 @@ public class FlowDownCheck {
       CompositeLocation condLoc =
           checkLocationFromExpressionNode(md, nametable, ln.getCondition(),
               new CompositeLocation(), constraint, false);
-      addLocationType(ln.getCondition().getType(), (condLoc));
+      // addLocationType(ln.getCondition().getType(), (condLoc));
 
       constraint = generateNewConstraint(constraint, condLoc);
       checkLocationFromBlockNode(md, nametable, ln.getBody(), constraint);
@@ -574,12 +614,18 @@ public class FlowDownCheck {
       // check 'for loop' case
       BlockNode bn = ln.getInitializer();
       bn.getVarTable().setParent(nametable);
+      // need to check initialization node
+      // checkLocationFromBlockNode(md, bn.getVarTable(), bn, constraint);
+      for (int i = 0; i < bn.size(); i++) {
+        BlockStatementNode bsn = bn.get(i);
+        checkLocationFromBlockStatementNode(md, bn.getVarTable(), bsn, constraint);
+      }
 
       // calculate glb location of condition and update statements
       CompositeLocation condLoc =
           checkLocationFromExpressionNode(md, bn.getVarTable(), ln.getCondition(),
               new CompositeLocation(), constraint, false);
-      addLocationType(ln.getCondition().getType(), condLoc);
+      // addLocationType(ln.getCondition().getType(), condLoc);
 
       constraint = generateNewConstraint(constraint, condLoc);
 
@@ -609,7 +655,7 @@ public class FlowDownCheck {
       Set<CompositeLocation> inputSet = new HashSet<CompositeLocation>();
       inputSet.add(currentCon);
       inputSet.add(newCon);
-      return CompositeLattice.calculateGLB(inputSet);
+      return CompositeLattice.calculateGLB(inputSet, "");
     }
 
   }
@@ -621,7 +667,7 @@ public class FlowDownCheck {
         checkLocationFromExpressionNode(md, nametable, isn.getCondition(), new CompositeLocation(),
             constraint, false);
 
-    addLocationType(isn.getCondition().getType(), condLoc);
+    // addLocationType(isn.getCondition().getType(), condLoc);
 
     constraint = generateNewConstraint(constraint, condLoc);
     checkLocationFromBlockNode(md, nametable, isn.getTrueBlock(), constraint);
@@ -633,6 +679,32 @@ public class FlowDownCheck {
     return new CompositeLocation();
   }
 
+  private void checkOwnership(MethodDescriptor md, TreeNode tn, ExpressionNode srcExpNode) {
+
+    if (srcExpNode.kind() == Kind.NameNode || srcExpNode.kind() == Kind.FieldAccessNode) {
+      if (srcExpNode.getType().isPtr() && !srcExpNode.getType().isNull()) {
+        // first, check the linear type
+        // RHS reference should be owned by the current method
+        FieldDescriptor fd = getFieldDescriptorFromExpressionNode(srcExpNode);
+        boolean isOwned;
+        if (fd == null) {
+          // local var case
+          isOwned = ((SSJavaType) srcExpNode.getType().getExtension()).isOwned();
+        } else {
+          // field case
+          isOwned = ssjava.isOwnedByMethod(md, fd);
+        }
+        if (!isOwned) {
+          throw new Error(
+              "It is not allowed to create the reference alias from the reference not owned by the method at "
+                  + generateErrorMessage(md.getClassDesc(), tn));
+        }
+
+      }
+    }
+
+  }
+
   private CompositeLocation checkLocationFromDeclarationNode(MethodDescriptor md,
       SymbolTable nametable, DeclarationNode dn, CompositeLocation constraint) {
 
@@ -641,12 +713,16 @@ public class FlowDownCheck {
     CompositeLocation destLoc = d2loc.get(vd);
 
     if (dn.getExpression() != null) {
+
+      checkOwnership(md, dn, dn.getExpression());
+
       CompositeLocation expressionLoc =
           checkLocationFromExpressionNode(md, nametable, dn.getExpression(),
               new CompositeLocation(), constraint, false);
       // addTypeLocation(dn.getExpression().getType(), expressionLoc);
 
       if (expressionLoc != null) {
+
         // checking location order
         if (!CompositeLattice.isGreaterThan(expressionLoc, destLoc,
             generateErrorMessage(md.getClassDesc(), dn))) {
@@ -667,11 +743,12 @@ public class FlowDownCheck {
 
   private void checkDeclarationInSubBlockNode(MethodDescriptor md, SymbolTable nametable,
       SubBlockNode sbn) {
-    checkDeclarationInBlockNode(md, nametable.getParent(), sbn.getBlockNode());
+    checkDeclarationInBlockNode(md, nametable, sbn.getBlockNode());
   }
 
   private CompositeLocation checkLocationFromBlockExpressionNode(MethodDescriptor md,
       SymbolTable nametable, BlockExpressionNode ben, CompositeLocation constraint) {
+
     CompositeLocation compLoc =
         checkLocationFromExpressionNode(md, nametable, ben.getExpression(), null, constraint, false);
     // addTypeLocation(ben.getExpression().getType(), compLoc);
@@ -771,20 +848,27 @@ public class FlowDownCheck {
     CompositeLocation condLoc =
         checkLocationFromExpressionNode(md, nametable, tn.getCond(), new CompositeLocation(),
             constraint, false);
-    addLocationType(tn.getCond().getType(), condLoc);
+    // addLocationType(tn.getCond().getType(), condLoc);
     CompositeLocation trueLoc =
         checkLocationFromExpressionNode(md, nametable, tn.getTrueExpr(), new CompositeLocation(),
             constraint, false);
-    addLocationType(tn.getTrueExpr().getType(), trueLoc);
+    // addLocationType(tn.getTrueExpr().getType(), trueLoc);
     CompositeLocation falseLoc =
         checkLocationFromExpressionNode(md, nametable, tn.getFalseExpr(), new CompositeLocation(),
             constraint, false);
-    addLocationType(tn.getFalseExpr().getType(), falseLoc);
+    // addLocationType(tn.getFalseExpr().getType(), falseLoc);
 
     // locations from true/false branches can be TOP when there are only literal
     // values
     // in this case, we don't need to check flow down rule!
 
+    // System.out.println("\n#tertiary cond=" + tn.getCond().printNode(0) +
+    // " Loc=" + condLoc);
+    // System.out.println("# true=" + tn.getTrueExpr().printNode(0) + " Loc=" +
+    // trueLoc);
+    // 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))) {
@@ -797,7 +881,7 @@ public class FlowDownCheck {
         && !CompositeLattice.isGreaterThan(condLoc, falseLoc,
             generateErrorMessage(cd, tn.getCond()))) {
       throw new Error(
-          "The location of the condition expression is lower than the true expression at "
+          "The location of the condition expression is lower than the false expression at "
               + cd.getSourceFileName() + ":" + tn.getCond().getNumLine());
     }
 
@@ -806,35 +890,248 @@ public class FlowDownCheck {
     glbInputSet.add(trueLoc);
     glbInputSet.add(falseLoc);
 
-    return CompositeLattice.calculateGLB(glbInputSet);
+    if (glbInputSet.size() == 1) {
+      return trueLoc;
+    } else {
+      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);
+    ClassDescriptor cd = md.getClassDesc();
+    MethodDescriptor calleeMethodDesc = min.getMethod();
 
-    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));
+    NameDescriptor baseName = min.getBaseName();
+    boolean isSystemout = false;
+    if (baseName != null) {
+      isSystemout = baseName.getSymbol().equals("System.out");
+    }
+
+    if (!ssjava.isSSJavaUtil(calleeMethodDesc.getClassDesc())
+        && !ssjava.isTrustMethod(calleeMethodDesc) && !calleeMethodDesc.getModifiers().isNative()
+        && !isSystemout) {
+
+      CompositeLocation baseLocation = null;
+      if (min.getExpression() != null) {
+        baseLocation =
+            checkLocationFromExpressionNode(md, nametable, min.getExpression(),
+                new CompositeLocation(), constraint, false);
+      } else {
+        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));
+        }
+      }
+
+      // System.out.println("\n#checkLocationFromMethodInvokeNode=" +
+      // min.printNode(0)
+      // + " baseLocation=" + baseLocation + " constraint=" + constraint);
+
+      // 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) {
+        if (!calleeMethodDesc.isStatic()) {
+          callerArgList.add(baseLocation);
+        }
+        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);
+        }
+
+        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) {
+        // check whether the PC location is lower than one of the
+        // argument locations. If it is lower, the callee has to have @PCLOC
+        // annotation that declares the program counter that is higher than
+        // corresponding parameter
+
+        CompositeLocation calleePCLOC = ssjava.getPCLocation(calleeMethodDesc);
+
+        for (int idx = 0; idx < callerArgList.size(); idx++) {
+          CompositeLocation argLocation = callerArgList.get(idx);
+
+          // need to check that param is higher than PCLOC
+          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,
+                    generateErrorMessage(cd, min));
+
+            if (paramCompareResult == ComparisonResult.GREATER) {
+              throw new Error(
+                  "The program counter location "
+                      + constraint
+                      + " is lower than the argument(idx="
+                      + idx
+                      + ") location "
+                      + argLocation
+                      + ". Need to specify that the initial PC location of the callee, which is currently set to "
+                      + calleePCLOC + ", is lower than " + paramLocation + " in the method "
+                      + calleeMethodDesc.getSymbol() + ":" + min.getNumLine());
+            }
+
+          }
+
+        }
+
+      }
+
+      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
+        CompositeLocation ceilingLoc =
+            computeCeilingLocationForCaller(md, nametable, min, baseLocation, constraint);
+        return ceilingLoc;
+      }
     }
 
-    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);
-      return ceilingLoc;
+    return new CompositeLocation(Location.createTopLocation(md));
+
+  }
+
+  private CompositeLocation translateCallerLocToCalleeLoc(MethodDescriptor calleeMD,
+      CompositeLocation calleeBaseLoc, CompositeLocation constraint) {
+
+    CompositeLocation calleeConstraint = new CompositeLocation();
+
+    // if (constraint.startsWith(calleeBaseLoc)) {
+    // if the first part of constraint loc is matched with callee base loc
+    Location thisLoc = new Location(calleeMD, ssjava.getMethodLattice(calleeMD).getThisLoc());
+    calleeConstraint.addLocation(thisLoc);
+    for (int i = calleeBaseLoc.getSize(); i < constraint.getSize(); i++) {
+      calleeConstraint.addLocation(constraint.get(i));
     }
 
-    return new CompositeLocation();
+    // }
+
+    return calleeConstraint;
+  }
+
+  private void checkCallerArgumentLocationConstraints(MethodDescriptor md, SymbolTable nametable,
+      MethodInvokeNode min, CompositeLocation callerBaseLoc, 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);
+
+    // System.out.println("checkCallerArgumentLocationConstraints=" +
+    // min.printNode(0));
+    // System.out.println("base location=" + callerBaseLoc + " constraint=" +
+    // constraint);
+
+    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 callerArgLoc = callerArgList.get(i);
+
+        CompositeLocation paramLocation =
+            translateCalleeParamLocToCaller(md, calleeParamLoc, callerBaseLoc, errorMsg);
+
+        Set<CompositeLocation> inputGLBSet = new HashSet<CompositeLocation>();
+        if (constraint != null) {
+          inputGLBSet.add(callerArgLoc);
+          inputGLBSet.add(constraint);
+          callerArgLoc =
+              CompositeLattice.calculateGLB(inputGLBSet,
+                  generateErrorMessage(md.getClassDesc(), min));
+        }
+
+        if (!CompositeLattice.isGreaterThan(callerArgLoc, paramLocation, errorMsg)) {
+          throw new Error("Caller argument '" + min.getArg(i).printNode(0) + " : " + callerArgLoc
+              + "' should be higher than corresponding callee's parameter : " + paramLocation
+              + " at " + errorMsg);
+        }
+
+      }
+    }
+
+  }
+
+  private CompositeLocation translateCalleeParamLocToCaller(MethodDescriptor md,
+      CompositeLocation calleeParamLoc, CompositeLocation callerBaseLocation, String errorMsg) {
+
+    CompositeLocation translate = new CompositeLocation();
+
+    for (int i = 0; i < callerBaseLocation.getSize(); i++) {
+      translate.addLocation(callerBaseLocation.get(i));
+    }
 
+    for (int i = 1; i < calleeParamLoc.getSize(); i++) {
+      translate.addLocation(calleeParamLoc.get(i));
+    }
+
+    // System.out.println("TRANSLATED=" + translate + " from calleeParamLoc=" +
+    // calleeParamLoc);
+
+    return translate;
   }
 
   private CompositeLocation computeCeilingLocationForCaller(MethodDescriptor md,
@@ -853,65 +1150,122 @@ public class FlowDownCheck {
       argList.add(callerArg);
     }
 
-    System.out.println("\n## computeReturnLocation=" + min.getMethod() + " argList=" + argList);
-    CompositeLocation compLoc = md2ReturnLocGen.get(min.getMethod()).computeReturnLocation(argList);
-    DeltaLocation delta = new DeltaLocation(compLoc, 1);
-    System.out.println("##computeReturnLocation=" + delta);
+    // System.out.println("\n## computeReturnLocation=" + min.getMethod() +
+    // " argList=" + argList);
+    CompositeLocation ceilLoc = md2ReturnLocGen.get(min.getMethod()).computeReturnLocation(argList);
+    // System.out.println("## ReturnLocation=" + ceilLoc);
 
-    return delta;
+    return ceilLoc;
 
   }
 
   private void checkCalleeConstraints(MethodDescriptor md, SymbolTable nametable,
-      MethodInvokeNode min, CompositeLocation constraint) {
+      MethodInvokeNode min, CompositeLocation callerBaseLoc, CompositeLocation constraint) {
+
+    // System.out.println("checkCalleeConstraints=" + min.printNode(0));
 
-    if (min.numArgs() > 1) {
+    MethodDescriptor calleemd = min.getMethod();
+
+    MethodLattice<String> calleeLattice = ssjava.getMethodLattice(calleemd);
+    CompositeLocation calleeThisLoc =
+        new CompositeLocation(new Location(calleemd, calleeLattice.getThisLoc()));
+
+    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);
+        // System.out.println("calleevd=" + calleevd + " loc=" + calleeLoc);
+        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;
+            }
+
+            // System.out.println("calleeLoc1=" + calleeLoc1);
+            // System.out.println("calleeLoc2=" + calleeLoc2 +
+            // "calleeParamList=" + calleeParamList);
+
+            int callerResult =
+                CompositeLattice.compare(callerLoc1, callerLoc2, true,
+                    generateErrorMessage(md.getClassDesc(), min));
+            // System.out.println("callerResult=" + callerResult);
+            int calleeResult =
+                CompositeLattice.compare(calleeLoc1, calleeLoc2, true,
+                    generateErrorMessage(md.getClassDesc(), min));
+            // System.out.println("calleeResult=" + calleeResult);
+
+            if (callerResult == ComparisonResult.EQUAL) {
+              if (ssjava.isSharedLocation(callerLoc1.get(callerLoc1.getSize() - 1))
+                  && ssjava.isSharedLocation(callerLoc2.get(callerLoc2.getSize() - 1))) {
+                // if both of them are shared locations, promote them to
+                // "GREATER relation"
+                callerResult = ComparisonResult.GREATER;
+              }
+            }
+
+            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());
             }
           }
-        }
 
+        }
       }
 
     }
@@ -926,6 +1280,7 @@ public class FlowDownCheck {
     CompositeLocation arrayLoc =
         checkLocationFromExpressionNode(md, nametable, aan.getExpression(),
             new CompositeLocation(), constraint, isLHS);
+
     // addTypeLocation(aan.getExpression().getType(), arrayLoc);
     CompositeLocation indexLoc =
         checkLocationFromExpressionNode(md, nametable, aan.getIndex(), new CompositeLocation(),
@@ -942,7 +1297,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));
     }
 
   }
@@ -974,11 +1329,13 @@ 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());
-    if (on.getRight() != null) {
-      System.out.println("# right loc=" + rightLoc + " from " + on.getRight().getClass());
-    }
+    // System.out.println("\n# OP NODE=" + on.printNode(0));
+    // System.out.println("# left loc=" + leftLoc + " from " +
+    // on.getLeft().getClass());
+    // if (on.getRight() != null) {
+    // System.out.println("# right loc=" + rightLoc + " from " +
+    // on.getRight().getClass());
+    // }
 
     Operation op = on.getOp();
 
@@ -1015,7 +1372,8 @@ 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));
       return glbCompLoc;
 
     default:
@@ -1055,6 +1413,7 @@ public class FlowDownCheck {
         Location locElement = new Location(md, thisLocId);
         loc.addLocation(locElement);
         return loc;
+
       }
 
       Descriptor d = (Descriptor) nametable.get(varname);
@@ -1064,7 +1423,7 @@ public class FlowDownCheck {
         VarDescriptor vd = (VarDescriptor) d;
         // localLoc = d2loc.get(vd);
         // the type of var descriptor has a composite location!
-        loc = ((CompositeLocation) vd.getType().getExtension()).clone();
+        loc = ((SSJavaType) vd.getType().getExtension()).getCompLoc().clone();
       } else if (d instanceof FieldDescriptor) {
         // the type of field descriptor has a location!
         FieldDescriptor fd = (FieldDescriptor) d;
@@ -1096,22 +1455,21 @@ 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
+        // access static field
         FieldDescriptor fd = nn.getField();
-        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));
+
+        Location fieldLoc = (Location) fd.getType().getExtension();
+        loc.addLocation(fieldLoc);
+
+        return loc;
 
       }
     }
@@ -1141,17 +1499,35 @@ public class FlowDownCheck {
       }
     }
 
+    if (left instanceof ArrayAccessNode) {
+      ArrayAccessNode aan = (ArrayAccessNode) left;
+      left = aan.getExpression();
+    }
+
     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")) {
+        // array.length access, return the location of the array
+        return loc;
+      }
+
       Location fieldLoc = getFieldLocation(fd);
       loc.addLocation(fieldLoc);
     }
-
     return loc;
   }
 
   private Location getFieldLocation(FieldDescriptor fd) {
 
+    // System.out.println("### getFieldLocation=" + fd);
+    // System.out.println("### fd.getType().getExtension()=" +
+    // fd.getType().getExtension());
+
     Location fieldLoc = (Location) fd.getType().getExtension();
 
     // handle the case that method annotation checking skips checking field
@@ -1164,10 +1540,30 @@ public class FlowDownCheck {
 
   }
 
+  private FieldDescriptor getFieldDescriptorFromExpressionNode(ExpressionNode en) {
+
+    if (en.kind() == Kind.NameNode) {
+      NameNode nn = (NameNode) en;
+      if (nn.getField() != null) {
+        return nn.getField();
+      }
+
+      if (nn.getName() != null && nn.getName().getBase() != null) {
+        return getFieldDescriptorFromExpressionNode(nn.getExpression());
+      }
+
+    } else if (en.kind() == Kind.FieldAccessNode) {
+      FieldAccessNode fan = (FieldAccessNode) en;
+      return fan.getField();
+    }
+
+    return null;
+  }
+
   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# ASSIGNMENTNODE=" + an.printNode(0));
 
     ClassDescriptor cd = md.getClassDesc();
 
@@ -1189,26 +1585,48 @@ public class FlowDownCheck {
     CompositeLocation srcLocation;
 
     if (!postinc) {
+
+      checkOwnership(md, an, an.getSrc());
+
       rhsLocation =
           checkLocationFromExpressionNode(md, nametable, an.getSrc(), new CompositeLocation(),
               constraint, false);
 
-      System.out.println("dstLocation=" + destLocation);
-      System.out.println("rhsLocation=" + rhsLocation);
-      System.out.println("constraint=" + constraint);
+      srcLocation = rhsLocation;
 
+      // if (!rhsLocation.get(rhsLocation.getSize() - 1).isTop()) {
       if (constraint != null) {
         inputGLBSet.add(rhsLocation);
         inputGLBSet.add(constraint);
-        srcLocation = CompositeLattice.calculateGLB(inputGLBSet);
-      } else {
-        srcLocation = rhsLocation;
+        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);
 
       if (!CompositeLattice.isGreaterThan(srcLocation, destLocation, generateErrorMessage(cd, an))) {
+
+        String context = "";
+        if (constraint != null) {
+          context = " and the current context constraint is " + constraint;
+        }
+
         throw new Error("The value flow from " + srcLocation + " to " + destLocation
-            + " does not respect location hierarchy on the assignment " + an.printNode(0) + " at "
-            + cd.getSourceFileName() + "::" + an.getNumLine());
+            + " does not respect location hierarchy on the assignment " + an.printNode(0) + context
+            + " at " + cd.getSourceFileName() + "::" + an.getNumLine());
+      }
+
+      if (srcLocation.equals(destLocation)) {
+        // keep it for definitely written analysis
+        Set<FlatNode> flatNodeSet = ssjava.getBuildFlat().getFlatNodeSet(an);
+        for (Iterator iterator = flatNodeSet.iterator(); iterator.hasNext();) {
+          FlatNode fn = (FlatNode) iterator.next();
+          ssjava.addSameHeightWriteFlatNode(fn);
+        }
+
       }
 
     } else {
@@ -1220,15 +1638,36 @@ 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 "
-            + cd.getSourceFileName() + "::" + an.getNumLine());
+
+        if (srcLocation.equals(destLocation)) {
+          throw new Error("Location " + srcLocation
+              + " is not allowed to have the value flow that moves within the same location at '"
+              + an.printNode(0) + "' of " + cd.getSourceFileName() + "::" + an.getNumLine());
+        } else {
+          throw new Error("The value flow from " + srcLocation + " to " + destLocation
+              + " does not respect location hierarchy on the assignment " + an.printNode(0)
+              + " at " + cd.getSourceFileName() + "::" + an.getNumLine());
+        }
+
+      }
+
+      if (srcLocation.equals(destLocation)) {
+        // keep it for definitely written analysis
+        Set<FlatNode> flatNodeSet = ssjava.getBuildFlat().getFlatNodeSet(an);
+        for (Iterator iterator = flatNodeSet.iterator(); iterator.hasNext();) {
+          FlatNode fn = (FlatNode) iterator.next();
+          ssjava.addSameHeightWriteFlatNode(fn);
+        }
       }
 
     }
@@ -1244,37 +1683,41 @@ public class FlowDownCheck {
 
     // currently enforce every variable to have corresponding location
     if (annotationVec.size() == 0) {
-      throw new Error("Location is not assigned to variable " + vd.getSymbol() + " in the method "
-          + md.getSymbol() + " of the class " + cd.getSymbol());
+      throw new Error("Location is not assigned to variable '" + vd.getSymbol()
+          + "' in the method '" + md + "' of the class " + cd.getSymbol() + " at "
+          + generateErrorMessage(cd, n));
     }
 
-    if (annotationVec.size() > 1) { // variable can have at most one location
-      throw new Error(vd.getSymbol() + " has more than one location.");
-    }
+    int locDecCount = 0;
+    for (int i = 0; i < annotationVec.size(); i++) {
+      AnnotationDescriptor ad = annotationVec.elementAt(i);
 
-    AnnotationDescriptor ad = annotationVec.elementAt(0);
+      if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
 
-    if (ad.getType() == AnnotationDescriptor.SINGLE_ANNOTATION) {
+        if (ad.getMarker().equals(SSJavaAnalysis.LOC)) {
+          locDecCount++;
+          if (locDecCount > 1) {// variable can have at most one location
+            throw new Error(vd.getSymbol() + " has more than one location declaration.");
+          }
+          String locDec = ad.getValue(); // check if location is defined
 
-      if (ad.getMarker().equals(SSJavaAnalysis.LOC)) {
-        String locDec = ad.getValue(); // check if location is defined
+          if (locDec.startsWith(SSJavaAnalysis.DELTA)) {
+            DeltaLocation deltaLoc = parseDeltaDeclaration(md, n, locDec);
+            d2loc.put(vd, deltaLoc);
+            addLocationType(vd.getType(), deltaLoc);
+          } else {
+            CompositeLocation compLoc = parseLocationDeclaration(md, n, locDec);
 
-        if (locDec.startsWith(SSJavaAnalysis.DELTA)) {
-          DeltaLocation deltaLoc = parseDeltaDeclaration(md, n, locDec);
-          d2loc.put(vd, deltaLoc);
-          addLocationType(vd.getType(), deltaLoc);
-        } else {
-          CompositeLocation compLoc = parseLocationDeclaration(md, n, locDec);
+            Location lastElement = compLoc.get(compLoc.getSize() - 1);
+            if (ssjava.isSharedLocation(lastElement)) {
+              ssjava.mapSharedLocation2Descriptor(lastElement, vd);
+            }
 
-          Location lastElement = compLoc.get(compLoc.getSize() - 1);
-          if (ssjava.isSharedLocation(lastElement)) {
-            ssjava.mapSharedLocation2Descriptor(lastElement, vd);
+            d2loc.put(vd, compLoc);
+            addLocationType(vd.getType(), compLoc);
           }
 
-          d2loc.put(vd, compLoc);
-          addLocationType(vd.getType(), compLoc);
         }
-
       }
     }
 
@@ -1297,9 +1740,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);
 
@@ -1309,6 +1753,8 @@ 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);
     }
@@ -1346,26 +1792,20 @@ public class FlowDownCheck {
     if (localLattice == null || (!localLattice.containsKey(localLocId))) {
       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;
@@ -1437,7 +1877,15 @@ public class FlowDownCheck {
 
   private void addLocationType(TypeDescriptor type, CompositeLocation loc) {
     if (type != null) {
-      type.setExtension(loc);
+      TypeExtension te = type.getExtension();
+      SSJavaType ssType;
+      if (te != null) {
+        ssType = (SSJavaType) te;
+        ssType.setCompLoc(loc);
+      } else {
+        ssType = new SSJavaType(loc);
+        type.setExtension(ssType);
+      }
     }
   }
 
@@ -1451,8 +1899,9 @@ 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;
@@ -1467,10 +1916,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);
+      // System.out.println("compare=" + loc1 + " " + loc2);
+      int baseCompareResult = compareBaseLocationSet(loc1, loc2, false, ignore, msg);
 
       if (baseCompareResult == ComparisonResult.EQUAL) {
         return compareDelta(loc1, loc2);
@@ -1502,7 +1952,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;
@@ -1512,93 +1962,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);
-            }
+            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);
-            }
-          }
-
-        } 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);
           }
         }
@@ -1615,7 +2002,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;
@@ -1630,8 +2017,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;
@@ -1641,7 +2034,7 @@ 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);
       CompositeLocation glbCompLoc = new CompositeLocation();
@@ -1658,6 +2051,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) {
@@ -1679,10 +2073,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);
@@ -1717,7 +2115,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++) {
@@ -1735,26 +2132,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));
+            }
+            if (innerCompLoc.getSize() > 0) {
+              innerGLBInput.add(innerCompLoc);
             }
-            // calculate glb for the current lattice
+          }
 
-            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
@@ -1773,6 +2168,7 @@ public class FlowDownCheck {
         }
       }
 
+      // System.out.println("GLB=" + glbCompLoc);
       return glbCompLoc;
 
     }
@@ -1795,6 +2191,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 {
@@ -1815,50 +2282,79 @@ 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 || type == PARAMISSAME) {
+          // return loc is equal to or lower than param
+          inputGLB.add(argLoc);
+        }
+      }
+
+      // compute GLB of arguments subset that are same or higher than return
+      // location
+      if (inputGLB.isEmpty()) {
+        CompositeLocation rtr =
+            new CompositeLocation(Location.createTopLocation(args.get(0).get(0).getDescriptor()));
+        return rtr;
+      } else {
+        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;
   }
 }