// Return false if the inference is illegal. Currently this only happens if the var is reassigned // to incompatible // type inside the loop. public boolean add(VariableExpression ve, ClassNode type) { if (defVars == null) defVars = FHashMap.emptyMap; if (ve.getAccessedVariable() != null) { defVars = defVars.put(ve.getAccessedVariable(), type); // dumpMap(ve); } else { boolean done = false; for (Map.Entry<Variable, ClassNode> variable : defVars.entrySet()) { if (variable.getKey().getName().equals(ve.getName())) { defVars = defVars.put(variable.getKey(), type); // dumpMap(ve); done = true; break; } } if (!done) { defVars = defVars.put(ve, type); // dumpMap(ve); } } if (parentScopeInference != null && parentScopeInference.defVars != null) { final ClassNode oldType = parentScopeInference.defVars.get(ve.getAccessedVariable()); if (oldType != null) { if (!TypeUtil.isDirectlyAssignableFrom(oldType, type)) { return false; } } } return true; }
public void jumpFrom(LocalVarInferenceTypes cur) { if (!visited) { if (defVars == null) { // we are 1st time here - just init if (cur.defVars != null) defVars = cur.defVars; else defVars = FHashMap.emptyMap; } else { // we were here already, so we need to merge if (cur.defVars != null) for (Map.Entry<Variable, ClassNode> e : cur.defVars.entrySet()) { final ClassNode oldType = defVars.get(e.getKey()); if (oldType != null) { final ClassNode newType = TypeUtil.commonType(oldType, e.getValue()); if (newType != oldType) defVars = defVars.put(e.getKey(), newType); } } } } else { // jump back - all is checked, nothing too be done at this point. } }