@Override public ReferenceCollection getReferences(Var var) { if (!var.isGlobal()) { return null; } return refMap.get(var.getName()); }
/** * Resets global var reference map with the new provide map. * * @param globalRefMap The reference map result of a {@link ReferenceCollectingCallback} pass * collected from the whole AST. */ private void resetGlobalVarReferences(Map<Var, ReferenceCollection> globalRefMap) { refMap = new HashMap<>(); for (Entry<Var, ReferenceCollection> entry : globalRefMap.entrySet()) { Var var = entry.getKey(); if (var.isGlobal()) { refMap.put(var.getName(), entry.getValue()); } } }
/** * Updates the internal reference map based on the provided parameters. If {@code scriptRoot} is * not SCRIPT, it basically replaces the internal map with the new one, otherwise it replaces all * the information associated to the given script. * * @param refMapPatch The reference map result of a {@link ReferenceCollectingCallback} pass which * might be collected from the whole AST or just a sub-tree associated to a SCRIPT node. * @param root AST sub-tree root on which reference collection was done. */ void updateGlobalVarReferences(Map<Var, ReferenceCollection> refMapPatch, Node root) { if (refMap == null || !root.isScript()) { resetGlobalVarReferences(refMapPatch); return; } InputId inputId = root.getInputId(); Preconditions.checkNotNull(inputId); // Note there are two assumptions here (i) the order of compiler inputs // has not changed and (ii) all references are in the order they appear // in AST (this is enforced in ReferenceCollectionCallback). removeScriptReferences(inputId); for (Entry<Var, ReferenceCollection> entry : refMapPatch.entrySet()) { Var var = entry.getKey(); if (var.isGlobal()) { replaceReferences(var.getName(), inputId, entry.getValue()); } } }