src/share/classes/com/sun/tools/javac/jvm/LVTRanges.java

changeset 2027
4932bb04c4b8
parent 0
959103a6100f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javac/jvm/LVTRanges.java	Sat Sep 14 19:04:47 2013 +0100
     1.3 @@ -0,0 +1,129 @@
     1.4 +/*
     1.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.javac.jvm;
    1.30 +
    1.31 +import java.util.Map;
    1.32 +import java.util.Map.Entry;
    1.33 +import java.util.WeakHashMap;
    1.34 +
    1.35 +import com.sun.tools.javac.code.Symbol.MethodSymbol;
    1.36 +import com.sun.tools.javac.code.Symbol.VarSymbol;
    1.37 +import com.sun.tools.javac.tree.JCTree;
    1.38 +import com.sun.tools.javac.util.Context;
    1.39 +import com.sun.tools.javac.util.List;
    1.40 +
    1.41 +/** This class contains a one to many relation between a tree and a set of variables.
    1.42 + *  The relation implies that the given tree closes the DA (definite assignment)
    1.43 + *  range for the set of variables.
    1.44 + *
    1.45 + *  <p><b>This is NOT part of any supported API.
    1.46 + *  If you write code that depends on this, you do so at your own risk.
    1.47 + *  This code and its internal interfaces are subject to change or
    1.48 + *  deletion without notice.</b>
    1.49 + */
    1.50 +public class LVTRanges {
    1.51 +    /** The context key for the LVT ranges. */
    1.52 +    protected static final Context.Key<LVTRanges> lvtRangesKey = new Context.Key<>();
    1.53 +
    1.54 +    /** Get the LVTRanges instance for this context. */
    1.55 +    public static LVTRanges instance(Context context) {
    1.56 +        LVTRanges instance = context.get(lvtRangesKey);
    1.57 +        if (instance == null) {
    1.58 +            instance = new LVTRanges(context);
    1.59 +        }
    1.60 +        return instance;
    1.61 +    }
    1.62 +
    1.63 +    private static final long serialVersionUID = 1812267524140424433L;
    1.64 +
    1.65 +    protected Context context;
    1.66 +
    1.67 +    protected Map<MethodSymbol, Map<JCTree, List<VarSymbol>>>
    1.68 +            aliveRangeClosingTrees = new WeakHashMap<>();
    1.69 +
    1.70 +    public LVTRanges(Context context) {
    1.71 +        this.context = context;
    1.72 +        context.put(lvtRangesKey, this);
    1.73 +    }
    1.74 +
    1.75 +    public List<VarSymbol> getVars(MethodSymbol method, JCTree tree) {
    1.76 +        Map<JCTree, List<VarSymbol>> varMap = aliveRangeClosingTrees.get(method);
    1.77 +        return (varMap != null) ? varMap.get(tree) : null;
    1.78 +    }
    1.79 +
    1.80 +    public boolean containsKey(MethodSymbol method, JCTree tree) {
    1.81 +        Map<JCTree, List<VarSymbol>> varMap = aliveRangeClosingTrees.get(method);
    1.82 +        if (varMap == null) {
    1.83 +            return false;
    1.84 +        }
    1.85 +        return varMap.containsKey(tree);
    1.86 +    }
    1.87 +
    1.88 +    public void setEntry(MethodSymbol method, JCTree tree, List<VarSymbol> vars) {
    1.89 +        Map<JCTree, List<VarSymbol>> varMap = aliveRangeClosingTrees.get(method);
    1.90 +        if (varMap != null) {
    1.91 +            varMap.put(tree, vars);
    1.92 +        } else {
    1.93 +            varMap = new WeakHashMap<>();
    1.94 +            varMap.put(tree, vars);
    1.95 +            aliveRangeClosingTrees.put(method, varMap);
    1.96 +        }
    1.97 +    }
    1.98 +
    1.99 +    public List<VarSymbol> removeEntry(MethodSymbol method, JCTree tree) {
   1.100 +        Map<JCTree, List<VarSymbol>> varMap = aliveRangeClosingTrees.get(method);
   1.101 +        if (varMap != null) {
   1.102 +            List<VarSymbol> result = varMap.remove(tree);
   1.103 +            if (varMap.isEmpty()) {
   1.104 +                aliveRangeClosingTrees.remove(method);
   1.105 +            }
   1.106 +            return result;
   1.107 +        }
   1.108 +        return null;
   1.109 +    }
   1.110 +
   1.111 +    /* This method should be used for debugging LVT related issues.
   1.112 +     */
   1.113 +    @Override
   1.114 +    public String toString() {
   1.115 +        String result = "";
   1.116 +        for (Entry<MethodSymbol, Map<JCTree, List<VarSymbol>>> mainEntry: aliveRangeClosingTrees.entrySet()) {
   1.117 +            result += "Method: \n" + mainEntry.getKey().flatName() + "\n";
   1.118 +            int i = 1;
   1.119 +            for (Entry<JCTree, List<VarSymbol>> treeEntry: mainEntry.getValue().entrySet()) {
   1.120 +                result += "    Tree " + i + ": \n" + treeEntry.getKey().toString() + "\n";
   1.121 +                result += "        Variables closed:\n";
   1.122 +                for (VarSymbol var: treeEntry.getValue()) {
   1.123 +                    result += "            " + var.toString();
   1.124 +                }
   1.125 +                result += "\n";
   1.126 +                i++;
   1.127 +            }
   1.128 +        }
   1.129 +        return result;
   1.130 +    }
   1.131 +
   1.132 +}

mercurial