📄 crtable.java
字号:
/* * Copyright 2001-2006 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Sun designates this * particular file as subject to the "Classpath" exception as provided * by Sun in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, * CA 95054 USA or visit www.sun.com if you need additional information or * have any questions. */package com.sun.tools.javac.jvm;import java.util.*;import com.sun.tools.javac.tree.*;import com.sun.tools.javac.util.*;import com.sun.tools.javac.util.List;import com.sun.tools.javac.tree.JCTree.*;/** This class contains the CharacterRangeTable for some method * and the hashtable for mapping trees or lists of trees to their * ending positions. * * <p><b>This is NOT part of any API supported by Sun Microsystems. If * you write code that depends on this, you do so at your own risk. * This code and its internal interfaces are subject to change or * deletion without notice.</b> */public class CRTableimplements CRTFlags { private final boolean crtDebug = false; /** The list of CRTable entries. */ private ListBuffer<CRTEntry> entries = new ListBuffer<CRTEntry>(); /** The hashtable for source positions. */ private Map<Object,SourceRange> positions = new HashMap<Object,SourceRange>(); /** The hashtable for ending positions stored in the parser. */ private Map<JCTree, Integer> endPositions; /** The tree of the method this table is intended for. * We should traverse this tree to get source ranges. */ JCTree.JCMethodDecl methodTree; /** Constructor */ public CRTable(JCTree.JCMethodDecl tree, Map<JCTree, Integer> endPositions) { this.methodTree = tree; this.endPositions = endPositions; } /** Create a new CRTEntry and add it to the entries. * @param tree The tree or the list of trees for which * we are storing the code pointers. * @param flags The set of flags designating type of the entry. * @param startPc The starting code position. * @param endPc The ending code position. */ public void put(Object tree, int flags, int startPc, int endPc) { entries.append(new CRTEntry(tree, flags, startPc, endPc)); } /** Compute source positions and write CRT to the databuf. * @param databuf The buffer to write bytecodes to. */ public int writeCRT(ByteBuffer databuf, Position.LineMap lineMap, Log log) { int crtEntries = 0; // compute source positions for the method new SourceComputer().csp(methodTree); for (List<CRTEntry> l = entries.toList(); l.nonEmpty(); l = l.tail) { CRTEntry entry = l.head; // eliminate entries that do not produce bytecodes: // for example, empty blocks and statements if (entry.startPc == entry.endPc) continue; SourceRange pos = positions.get(entry.tree); assert pos != null : "CRT: tree source positions are undefined"; if ((pos.startPos == Position.NOPOS) || (pos.endPos == Position.NOPOS)) continue; if (crtDebug) { System.out.println("Tree: " + entry.tree + ", type:" + getTypes(entry.flags)); System.out.print("Start: pos = " + pos.startPos + ", pc = " + entry.startPc); } // encode startPos into line/column representation int startPos = encodePosition(pos.startPos, lineMap, log); if (startPos == Position.NOPOS) continue; if (crtDebug) { System.out.print("End: pos = " + pos.endPos + ", pc = " + (entry.endPc - 1)); } // encode endPos into line/column representation int endPos = encodePosition(pos.endPos, lineMap, log); if (endPos == Position.NOPOS) continue; // write attribute databuf.appendChar(entry.startPc); // 'endPc - 1' because endPc actually points to start of the next command databuf.appendChar(entry.endPc - 1); databuf.appendInt(startPos); databuf.appendInt(endPos); databuf.appendChar(entry.flags); crtEntries++; } return crtEntries; } /** Return the number of the entries. */ public int length() { return entries.length(); } /** Return string describing flags enabled. */ private String getTypes(int flags) { String types = ""; if ((flags & CRT_STATEMENT) != 0) types += " CRT_STATEMENT"; if ((flags & CRT_BLOCK) != 0) types += " CRT_BLOCK"; if ((flags & CRT_ASSIGNMENT) != 0) types += " CRT_ASSIGNMENT"; if ((flags & CRT_FLOW_CONTROLLER) != 0) types += " CRT_FLOW_CONTROLLER"; if ((flags & CRT_FLOW_TARGET) != 0) types += " CRT_FLOW_TARGET"; if ((flags & CRT_INVOKE) != 0) types += " CRT_INVOKE"; if ((flags & CRT_CREATE) != 0) types += " CRT_CREATE"; if ((flags & CRT_BRANCH_TRUE) != 0) types += " CRT_BRANCH_TRUE"; if ((flags & CRT_BRANCH_FALSE) != 0) types += " CRT_BRANCH_FALSE"; return types; } /** Source file positions in CRT are integers in the format: * line-number << LINESHIFT + column-number */ private int encodePosition(int pos, Position.LineMap lineMap, Log log) { int line = lineMap.getLineNumber(pos); int col = lineMap.getColumnNumber(pos); int new_pos = Position.encodePosition(line, col); if (crtDebug) { System.out.println(", line = " + line + ", column = " + col + ", new_pos = " + new_pos); } if (new_pos == Position.NOPOS) log.warning(pos, "position.overflow", line); return new_pos; } /* ************************************************************************ * Traversal methods *************************************************************************/ /** * This class contains methods to compute source positions for trees. * Extends Tree.Visitor to traverse the abstract syntax tree. */ class SourceComputer extends JCTree.Visitor { /** The result of the tree traversal methods. */ SourceRange result; /** Visitor method: compute source positions for a single node. */ public SourceRange csp(JCTree tree) { if (tree == null) return null; tree.accept(this); if (result != null) { positions.put(tree, result); } return result; } /** Visitor method: compute source positions for a list of nodes. */ public SourceRange csp(List<? extends JCTree> trees) { if ((trees == null) || !(trees.nonEmpty())) return null; SourceRange list_sr = new SourceRange(); for (List<? extends JCTree> l = trees; l.nonEmpty(); l = l.tail) { list_sr.mergeWith(csp(l.head)); } positions.put(trees, list_sr); return list_sr; } /** Visitor method: compute source positions for * a list of case blocks of switch statements. */ public SourceRange cspCases(List<JCCase> trees) { if ((trees == null) || !(trees.nonEmpty())) return null; SourceRange list_sr = new SourceRange(); for (List<JCCase> l = trees; l.nonEmpty(); l = l.tail) { list_sr.mergeWith(csp(l.head)); } positions.put(trees, list_sr); return list_sr; } /** Visitor method: compute source positions for * a list of catch clauses in try statements. */ public SourceRange cspCatchers(List<JCCatch> trees) { if ((trees == null) || !(trees.nonEmpty())) return null; SourceRange list_sr = new SourceRange(); for (List<JCCatch> l = trees; l.nonEmpty(); l = l.tail) { list_sr.mergeWith(csp(l.head)); } positions.put(trees, list_sr); return list_sr; } public void visitMethodDef(JCMethodDecl tree) { SourceRange sr = new SourceRange(startPos(tree), endPos(tree)); sr.mergeWith(csp(tree.body)); result = sr; } public void visitVarDef(JCVariableDecl tree) { SourceRange sr = new SourceRange(startPos(tree), endPos(tree)); csp(tree.vartype); sr.mergeWith(csp(tree.init)); result = sr; } public void visitSkip(JCSkip tree) { // endPos is the same as startPos for the empty statement SourceRange sr = new SourceRange(startPos(tree), startPos(tree)); result = sr; } public void visitBlock(JCBlock tree) { SourceRange sr = new SourceRange(startPos(tree), endPos(tree)); csp(tree.stats); // doesn't compare because block's ending position is defined result = sr; } public void visitDoLoop(JCDoWhileLoop tree) { SourceRange sr = new SourceRange(startPos(tree), endPos(tree)); sr.mergeWith(csp(tree.body)); sr.mergeWith(csp(tree.cond)); result = sr; } public void visitWhileLoop(JCWhileLoop tree) { SourceRange sr = new SourceRange(startPos(tree), endPos(tree)); sr.mergeWith(csp(tree.cond)); sr.mergeWith(csp(tree.body)); result = sr; } public void visitForLoop(JCForLoop tree) { SourceRange sr = new SourceRange(startPos(tree), endPos(tree)); sr.mergeWith(csp(tree.init)); sr.mergeWith(csp(tree.cond)); sr.mergeWith(csp(tree.step)); sr.mergeWith(csp(tree.body)); result = sr; } public void visitForeachLoop(JCEnhancedForLoop tree) { SourceRange sr = new SourceRange(startPos(tree), endPos(tree)); sr.mergeWith(csp(tree.var)); sr.mergeWith(csp(tree.expr)); sr.mergeWith(csp(tree.body)); result = sr; } public void visitLabelled(JCLabeledStatement tree) { SourceRange sr = new SourceRange(startPos(tree), endPos(tree)); sr.mergeWith(csp(tree.body)); result = sr; } public void visitSwitch(JCSwitch tree) { SourceRange sr = new SourceRange(startPos(tree), endPos(tree)); sr.mergeWith(csp(tree.selector)); sr.mergeWith(cspCases(tree.cases)); result = sr;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -