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

changeset 2027
4932bb04c4b8
parent 0
959103a6100f
equal deleted inserted replaced
2026:03c26c60499c 2027:4932bb04c4b8
1 /*
2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.tools.javac.jvm;
27
28 import java.util.Map;
29 import java.util.Map.Entry;
30 import java.util.WeakHashMap;
31
32 import com.sun.tools.javac.code.Symbol.MethodSymbol;
33 import com.sun.tools.javac.code.Symbol.VarSymbol;
34 import com.sun.tools.javac.tree.JCTree;
35 import com.sun.tools.javac.util.Context;
36 import com.sun.tools.javac.util.List;
37
38 /** This class contains a one to many relation between a tree and a set of variables.
39 * The relation implies that the given tree closes the DA (definite assignment)
40 * range for the set of variables.
41 *
42 * <p><b>This is NOT part of any supported API.
43 * If you write code that depends on this, you do so at your own risk.
44 * This code and its internal interfaces are subject to change or
45 * deletion without notice.</b>
46 */
47 public class LVTRanges {
48 /** The context key for the LVT ranges. */
49 protected static final Context.Key<LVTRanges> lvtRangesKey = new Context.Key<>();
50
51 /** Get the LVTRanges instance for this context. */
52 public static LVTRanges instance(Context context) {
53 LVTRanges instance = context.get(lvtRangesKey);
54 if (instance == null) {
55 instance = new LVTRanges(context);
56 }
57 return instance;
58 }
59
60 private static final long serialVersionUID = 1812267524140424433L;
61
62 protected Context context;
63
64 protected Map<MethodSymbol, Map<JCTree, List<VarSymbol>>>
65 aliveRangeClosingTrees = new WeakHashMap<>();
66
67 public LVTRanges(Context context) {
68 this.context = context;
69 context.put(lvtRangesKey, this);
70 }
71
72 public List<VarSymbol> getVars(MethodSymbol method, JCTree tree) {
73 Map<JCTree, List<VarSymbol>> varMap = aliveRangeClosingTrees.get(method);
74 return (varMap != null) ? varMap.get(tree) : null;
75 }
76
77 public boolean containsKey(MethodSymbol method, JCTree tree) {
78 Map<JCTree, List<VarSymbol>> varMap = aliveRangeClosingTrees.get(method);
79 if (varMap == null) {
80 return false;
81 }
82 return varMap.containsKey(tree);
83 }
84
85 public void setEntry(MethodSymbol method, JCTree tree, List<VarSymbol> vars) {
86 Map<JCTree, List<VarSymbol>> varMap = aliveRangeClosingTrees.get(method);
87 if (varMap != null) {
88 varMap.put(tree, vars);
89 } else {
90 varMap = new WeakHashMap<>();
91 varMap.put(tree, vars);
92 aliveRangeClosingTrees.put(method, varMap);
93 }
94 }
95
96 public List<VarSymbol> removeEntry(MethodSymbol method, JCTree tree) {
97 Map<JCTree, List<VarSymbol>> varMap = aliveRangeClosingTrees.get(method);
98 if (varMap != null) {
99 List<VarSymbol> result = varMap.remove(tree);
100 if (varMap.isEmpty()) {
101 aliveRangeClosingTrees.remove(method);
102 }
103 return result;
104 }
105 return null;
106 }
107
108 /* This method should be used for debugging LVT related issues.
109 */
110 @Override
111 public String toString() {
112 String result = "";
113 for (Entry<MethodSymbol, Map<JCTree, List<VarSymbol>>> mainEntry: aliveRangeClosingTrees.entrySet()) {
114 result += "Method: \n" + mainEntry.getKey().flatName() + "\n";
115 int i = 1;
116 for (Entry<JCTree, List<VarSymbol>> treeEntry: mainEntry.getValue().entrySet()) {
117 result += " Tree " + i + ": \n" + treeEntry.getKey().toString() + "\n";
118 result += " Variables closed:\n";
119 for (VarSymbol var: treeEntry.getValue()) {
120 result += " " + var.toString();
121 }
122 result += "\n";
123 i++;
124 }
125 }
126 return result;
127 }
128
129 }

mercurial