Mon, 14 Nov 2011 15:11:10 -0800
7106166: (javac) re-factor EndPos parser
Reviewed-by: jjg
1.1 --- a/src/share/classes/com/sun/tools/javac/api/JavacTrees.java Mon Nov 14 08:09:47 2011 -0800 1.2 +++ b/src/share/classes/com/sun/tools/javac/api/JavacTrees.java Mon Nov 14 15:11:10 2011 -0800 1.3 @@ -59,6 +59,7 @@ 1.4 import com.sun.tools.javac.comp.MemberEnter; 1.5 import com.sun.tools.javac.comp.Resolve; 1.6 import com.sun.tools.javac.model.JavacElements; 1.7 +import com.sun.tools.javac.parser.EndPosTable; 1.8 import com.sun.tools.javac.processing.JavacProcessingEnvironment; 1.9 import com.sun.tools.javac.tree.JCTree.*; 1.10 import com.sun.tools.javac.tree.JCTree; 1.11 @@ -140,8 +141,8 @@ 1.12 } 1.13 1.14 public long getEndPosition(CompilationUnitTree file, Tree tree) { 1.15 - Map<JCTree,Integer> endPositions = ((JCCompilationUnit) file).endPositions; 1.16 - return TreeInfo.getEndPos((JCTree) tree, endPositions); 1.17 + EndPosTable endPosTable = ((JCCompilationUnit) file).endPositions; 1.18 + return TreeInfo.getEndPos((JCTree) tree, endPosTable); 1.19 } 1.20 }; 1.21 }
2.1 --- a/src/share/classes/com/sun/tools/javac/comp/Lower.java Mon Nov 14 08:09:47 2011 -0800 2.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Lower.java Mon Nov 14 15:11:10 2011 -0800 2.3 @@ -40,6 +40,7 @@ 2.4 import com.sun.tools.javac.code.Type.*; 2.5 2.6 import com.sun.tools.javac.jvm.Target; 2.7 +import com.sun.tools.javac.parser.EndPosTable; 2.8 2.9 import static com.sun.tools.javac.code.Flags.*; 2.10 import static com.sun.tools.javac.code.Flags.BLOCK; 2.11 @@ -127,7 +128,7 @@ 2.12 2.13 /** A hash table mapping syntax trees to their ending source positions. 2.14 */ 2.15 - Map<JCTree, Integer> endPositions; 2.16 + EndPosTable endPosTable; 2.17 2.18 /************************************************************************** 2.19 * Global mappings 2.20 @@ -2195,9 +2196,8 @@ 2.21 } else { 2.22 make_at(tree.pos()); 2.23 T result = super.translate(tree); 2.24 - if (endPositions != null && result != tree) { 2.25 - Integer endPos = endPositions.remove(tree); 2.26 - if (endPos != null) endPositions.put(result, endPos); 2.27 + if (endPosTable != null && result != tree) { 2.28 + endPosTable.replaceTree(tree, result); 2.29 } 2.30 return result; 2.31 } 2.32 @@ -3675,7 +3675,7 @@ 2.33 try { 2.34 attrEnv = env; 2.35 this.make = make; 2.36 - endPositions = env.toplevel.endPositions; 2.37 + endPosTable = env.toplevel.endPositions; 2.38 currentClass = null; 2.39 currentMethodDef = null; 2.40 outermostClassDef = (cdef.hasTag(CLASSDEF)) ? (JCClassDecl)cdef : null; 2.41 @@ -3704,7 +3704,7 @@ 2.42 // note that recursive invocations of this method fail hard 2.43 attrEnv = null; 2.44 this.make = null; 2.45 - endPositions = null; 2.46 + endPosTable = null; 2.47 currentClass = null; 2.48 currentMethodDef = null; 2.49 outermostClassDef = null;
3.1 --- a/src/share/classes/com/sun/tools/javac/jvm/CRTable.java Mon Nov 14 08:09:47 2011 -0800 3.2 +++ b/src/share/classes/com/sun/tools/javac/jvm/CRTable.java Mon Nov 14 15:11:10 2011 -0800 3.3 @@ -31,6 +31,7 @@ 3.4 import com.sun.tools.javac.util.*; 3.5 import com.sun.tools.javac.util.List; 3.6 import com.sun.tools.javac.tree.JCTree.*; 3.7 +import com.sun.tools.javac.parser.EndPosTable; 3.8 3.9 /** This class contains the CharacterRangeTable for some method 3.10 * and the hashtable for mapping trees or lists of trees to their 3.11 @@ -54,9 +55,9 @@ 3.12 */ 3.13 private Map<Object,SourceRange> positions = new HashMap<Object,SourceRange>(); 3.14 3.15 - /** The hashtable for ending positions stored in the parser. 3.16 + /** The object for ending positions stored in the parser. 3.17 */ 3.18 - private Map<JCTree, Integer> endPositions; 3.19 + private EndPosTable endPosTable; 3.20 3.21 /** The tree of the method this table is intended for. 3.22 * We should traverse this tree to get source ranges. 3.23 @@ -65,9 +66,9 @@ 3.24 3.25 /** Constructor 3.26 */ 3.27 - public CRTable(JCTree.JCMethodDecl tree, Map<JCTree, Integer> endPositions) { 3.28 + public CRTable(JCTree.JCMethodDecl tree, EndPosTable endPosTable) { 3.29 this.methodTree = tree; 3.30 - this.endPositions = endPositions; 3.31 + this.endPosTable = endPosTable; 3.32 } 3.33 3.34 /** Create a new CRTEntry and add it to the entries. 3.35 @@ -534,10 +535,7 @@ 3.36 if (tree == null) return Position.NOPOS; 3.37 if (tree.hasTag(JCTree.Tag.BLOCK)) 3.38 return ((JCBlock) tree).endpos; 3.39 - Integer endpos = endPositions.get(tree); 3.40 - if (endpos != null) 3.41 - return endpos.intValue(); 3.42 - return Position.NOPOS; 3.43 + return endPosTable.getEndPos(tree); 3.44 } 3.45 } 3.46
4.1 --- a/src/share/classes/com/sun/tools/javac/jvm/Gen.java Mon Nov 14 08:09:47 2011 -0800 4.2 +++ b/src/share/classes/com/sun/tools/javac/jvm/Gen.java Mon Nov 14 15:11:10 2011 -0800 4.3 @@ -26,8 +26,6 @@ 4.4 package com.sun.tools.javac.jvm; 4.5 import java.util.*; 4.6 4.7 -import javax.lang.model.element.ElementKind; 4.8 - 4.9 import com.sun.tools.javac.util.*; 4.10 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; 4.11 import com.sun.tools.javac.util.List; 4.12 @@ -39,6 +37,7 @@ 4.13 import com.sun.tools.javac.code.Type.*; 4.14 import com.sun.tools.javac.jvm.Code.*; 4.15 import com.sun.tools.javac.jvm.Items.*; 4.16 +import com.sun.tools.javac.parser.EndPosTable; 4.17 import com.sun.tools.javac.tree.JCTree.*; 4.18 4.19 import static com.sun.tools.javac.code.Flags.*; 4.20 @@ -197,9 +196,10 @@ 4.21 */ 4.22 private int nerrs = 0; 4.23 4.24 - /** A hash table mapping syntax trees to their ending source positions. 4.25 + /** An object containing mappings of syntax trees to their 4.26 + * ending source positions. 4.27 */ 4.28 - private Map<JCTree, Integer> endPositions; 4.29 + EndPosTable endPosTable; 4.30 4.31 /** Generate code to load an integer constant. 4.32 * @param n The integer to be loaded. 4.33 @@ -482,20 +482,14 @@ 4.34 JCStatement init = make.at(vdef.pos()). 4.35 Assignment(sym, vdef.init); 4.36 initCode.append(init); 4.37 - if (endPositions != null) { 4.38 - Integer endPos = endPositions.remove(vdef); 4.39 - if (endPos != null) endPositions.put(init, endPos); 4.40 - } 4.41 + endPosTable.replaceTree(vdef, init); 4.42 } else if (sym.getConstValue() == null) { 4.43 // Initialize class (static) variables only if 4.44 // they are not compile-time constants. 4.45 JCStatement init = make.at(vdef.pos). 4.46 Assignment(sym, vdef.init); 4.47 clinitCode.append(init); 4.48 - if (endPositions != null) { 4.49 - Integer endPos = endPositions.remove(vdef); 4.50 - if (endPos != null) endPositions.put(init, endPos); 4.51 - } 4.52 + endPosTable.replaceTree(vdef, init); 4.53 } else { 4.54 checkStringConstant(vdef.init.pos(), sym.getConstValue()); 4.55 } 4.56 @@ -2217,7 +2211,7 @@ 4.57 attrEnv = env; 4.58 ClassSymbol c = cdef.sym; 4.59 this.toplevel = env.toplevel; 4.60 - this.endPositions = toplevel.endPositions; 4.61 + this.endPosTable = toplevel.endPositions; 4.62 // If this is a class definition requiring Miranda methods, 4.63 // add them. 4.64 if (generateIproxies && 4.65 @@ -2253,7 +2247,7 @@ 4.66 attrEnv = null; 4.67 this.env = null; 4.68 toplevel = null; 4.69 - endPositions = null; 4.70 + endPosTable = null; 4.71 nerrs = 0; 4.72 } 4.73 }
5.1 --- a/src/share/classes/com/sun/tools/javac/parser/EndPosParser.java Mon Nov 14 08:09:47 2011 -0800 5.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 5.3 @@ -1,102 +0,0 @@ 5.4 -/* 5.5 - * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved. 5.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5.7 - * 5.8 - * This code is free software; you can redistribute it and/or modify it 5.9 - * under the terms of the GNU General Public License version 2 only, as 5.10 - * published by the Free Software Foundation. Oracle designates this 5.11 - * particular file as subject to the "Classpath" exception as provided 5.12 - * by Oracle in the LICENSE file that accompanied this code. 5.13 - * 5.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 5.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 5.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 5.17 - * version 2 for more details (a copy is included in the LICENSE file that 5.18 - * accompanied this code). 5.19 - * 5.20 - * You should have received a copy of the GNU General Public License version 5.21 - * 2 along with this work; if not, write to the Free Software Foundation, 5.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 5.23 - * 5.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 5.25 - * or visit www.oracle.com if you need additional information or have any 5.26 - * questions. 5.27 - */ 5.28 - 5.29 -package com.sun.tools.javac.parser; 5.30 - 5.31 -import java.util.Map; 5.32 -import java.util.HashMap; 5.33 -import com.sun.tools.javac.tree.JCTree; 5.34 -import com.sun.tools.javac.tree.TreeInfo; 5.35 - 5.36 -import static com.sun.tools.javac.tree.JCTree.*; 5.37 - 5.38 -/** 5.39 - * This class is similar to Parser except that it stores ending 5.40 - * positions for the tree nodes. 5.41 - * 5.42 - * <p><b>This is NOT part of any supported API. 5.43 - * If you write code that depends on this, you do so at your own risk. 5.44 - * This code and its internal interfaces are subject to change or 5.45 - * deletion without notice.</b></p> 5.46 - */ 5.47 -public class EndPosParser extends JavacParser { 5.48 - 5.49 - public EndPosParser(ParserFactory fac, Lexer S, boolean keepDocComments, boolean keepLineMap) { 5.50 - super(fac, S, keepDocComments, keepLineMap); 5.51 - this.S = S; 5.52 - endPositions = new HashMap<JCTree,Integer>(); 5.53 - } 5.54 - 5.55 - private Lexer S; 5.56 - 5.57 - /** A hashtable to store ending positions 5.58 - * of source ranges indexed by the tree nodes. 5.59 - * Defined only if option flag genEndPos is set. 5.60 - */ 5.61 - Map<JCTree, Integer> endPositions; 5.62 - 5.63 - /** {@inheritDoc} */ 5.64 - @Override 5.65 - protected void storeEnd(JCTree tree, int endpos) { 5.66 - int errorEndPos = getErrorEndPos(); 5.67 - endPositions.put(tree, errorEndPos > endpos ? errorEndPos : endpos); 5.68 - } 5.69 - 5.70 - /** {@inheritDoc} */ 5.71 - @Override 5.72 - protected <T extends JCTree> T to(T t) { 5.73 - storeEnd(t, token.endPos); 5.74 - return t; 5.75 - } 5.76 - 5.77 - /** {@inheritDoc} */ 5.78 - @Override 5.79 - protected <T extends JCTree> T toP(T t) { 5.80 - storeEnd(t, S.prevToken().endPos); 5.81 - return t; 5.82 - } 5.83 - 5.84 - @Override 5.85 - public JCCompilationUnit parseCompilationUnit() { 5.86 - JCCompilationUnit t = super.parseCompilationUnit(); 5.87 - t.endPositions = endPositions; 5.88 - return t; 5.89 - } 5.90 - 5.91 - /** {@inheritDoc} */ 5.92 - @Override 5.93 - JCExpression parExpression() { 5.94 - int pos = token.pos; 5.95 - JCExpression t = super.parExpression(); 5.96 - return toP(F.at(pos).Parens(t)); 5.97 - } 5.98 - 5.99 - /** {@inheritDoc} */ 5.100 - @Override 5.101 - public int getEndPos(JCTree tree) { 5.102 - return TreeInfo.getEndPos(tree, endPositions); 5.103 - } 5.104 - 5.105 -}
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/src/share/classes/com/sun/tools/javac/parser/EndPosTable.java Mon Nov 14 15:11:10 2011 -0800 6.3 @@ -0,0 +1,56 @@ 6.4 +/* 6.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. 6.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 6.7 + * 6.8 + * This code is free software; you can redistribute it and/or modify it 6.9 + * under the terms of the GNU General Public License version 2 only, as 6.10 + * published by the Free Software Foundation. Oracle designates this 6.11 + * particular file as subject to the "Classpath" exception as provided 6.12 + * by Oracle in the LICENSE file that accompanied this code. 6.13 + * 6.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 6.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 6.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 6.17 + * version 2 for more details (a copy is included in the LICENSE file that 6.18 + * accompanied this code). 6.19 + * 6.20 + * You should have received a copy of the GNU General Public License version 6.21 + * 2 along with this work; if not, write to the Free Software Foundation, 6.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 6.23 + * 6.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 6.25 + * or visit www.oracle.com if you need additional information or have any 6.26 + * questions. 6.27 + */ 6.28 +package com.sun.tools.javac.parser; 6.29 + 6.30 +import com.sun.tools.javac.tree.JCTree; 6.31 + 6.32 +/** 6.33 + * Specifies the methods to access a mappings of syntax trees to end positions. 6.34 + * <p><b>This is NOT part of any supported API. 6.35 + * If you write code that depends on this, you do so at your own 6.36 + * risk. This code and its internal interfaces are subject to change 6.37 + * or deletion without notice.</b></p> 6.38 + */ 6.39 +public interface EndPosTable { 6.40 + 6.41 + /** 6.42 + * This method will return the end position of a given tree, otherwise a 6.43 + * Positions.NOPOS will be returned. 6.44 + * @param tree JCTree 6.45 + * @return position of the source tree or Positions.NOPOS for non-existent mapping 6.46 + */ 6.47 + public int getEndPos(JCTree tree); 6.48 + 6.49 + /** 6.50 + * Give an old tree and a new tree, the old tree will be replaced with 6.51 + * the new tree, the position of the new tree will be that of the old 6.52 + * tree. 6.53 + * not exist. 6.54 + * @param oldtree a JCTree to be replaced 6.55 + * @param newtree a JCTree to be replaced with 6.56 + * @return position of the old tree or Positions.NOPOS for non-existent mapping 6.57 + */ 6.58 + public int replaceTree(JCTree oldtree, JCTree newtree); 6.59 +}
7.1 --- a/src/share/classes/com/sun/tools/javac/parser/JavacParser.java Mon Nov 14 08:09:47 2011 -0800 7.2 +++ b/src/share/classes/com/sun/tools/javac/parser/JavacParser.java Mon Nov 14 15:11:10 2011 -0800 7.3 @@ -83,12 +83,16 @@ 7.4 /** The name table. */ 7.5 private Names names; 7.6 7.7 + /** End position mappings container */ 7.8 + private final AbstractEndPosTable endPosTable; 7.9 + 7.10 /** Construct a parser from a given scanner, tree factory and log. 7.11 */ 7.12 protected JavacParser(ParserFactory fac, 7.13 Lexer S, 7.14 boolean keepDocComments, 7.15 - boolean keepLineMap) { 7.16 + boolean keepLineMap, 7.17 + boolean keepEndPositions) { 7.18 this.S = S; 7.19 nextToken(); // prime the pump 7.20 this.F = fac.F; 7.21 @@ -110,8 +114,14 @@ 7.22 docComments = keepDocComments ? new HashMap<JCTree,String>() : null; 7.23 this.keepLineMap = keepLineMap; 7.24 this.errorTree = F.Erroneous(); 7.25 + endPosTable = newEndPosTable(keepEndPositions); 7.26 } 7.27 7.28 + protected AbstractEndPosTable newEndPosTable(boolean keepEndPositions) { 7.29 + return keepEndPositions 7.30 + ? new SimpleEndPosTable() 7.31 + : new EmptyEndPosTable(); 7.32 + } 7.33 /** Switch: Should generics be recognized? 7.34 */ 7.35 boolean allowGenerics; 7.36 @@ -389,37 +399,21 @@ 7.37 7.38 /* -------- source positions ------- */ 7.39 7.40 - private int errorEndPos = -1; 7.41 - 7.42 private void setErrorEndPos(int errPos) { 7.43 - if (errPos > errorEndPos) 7.44 - errorEndPos = errPos; 7.45 + endPosTable.setErrorEndPos(errPos); 7.46 } 7.47 7.48 - protected int getErrorEndPos() { 7.49 - return errorEndPos; 7.50 + private void storeEnd(JCTree tree, int endpos) { 7.51 + endPosTable.storeEnd(tree, endpos); 7.52 } 7.53 7.54 - /** 7.55 - * Store ending position for a tree. 7.56 - * @param tree The tree. 7.57 - * @param endpos The ending position to associate with the tree. 7.58 - */ 7.59 - protected void storeEnd(JCTree tree, int endpos) {} 7.60 + private <T extends JCTree> T to(T t) { 7.61 + return endPosTable.to(t); 7.62 + } 7.63 7.64 - /** 7.65 - * Store ending position for a tree. The ending position should 7.66 - * be the ending position of the current token. 7.67 - * @param t The tree. 7.68 - */ 7.69 - protected <T extends JCTree> T to(T t) { return t; } 7.70 - 7.71 - /** 7.72 - * Store ending position for a tree. The ending position should 7.73 - * be greater of the ending position of the previous token and errorEndPos. 7.74 - * @param t The tree. 7.75 - */ 7.76 - protected <T extends JCTree> T toP(T t) { return t; } 7.77 + private <T extends JCTree> T toP(T t) { 7.78 + return endPosTable.toP(t); 7.79 + } 7.80 7.81 /** Get the start position for a tree node. The start position is 7.82 * defined to be the position of the first character of the first 7.83 @@ -439,7 +433,7 @@ 7.84 * @param tree The tree node 7.85 */ 7.86 public int getEndPos(JCTree tree) { 7.87 - return Position.NOPOS; 7.88 + return endPosTable.getEndPos(tree); 7.89 } 7.90 7.91 7.92 @@ -1362,7 +1356,7 @@ 7.93 int pos = token.pos; 7.94 nextToken(); 7.95 accept(CLASS); 7.96 - if (token.pos == errorEndPos) { 7.97 + if (token.pos == endPosTable.errorEndPos) { 7.98 // error recovery 7.99 Name name = null; 7.100 if (token.kind == IDENTIFIER) { 7.101 @@ -1542,10 +1536,11 @@ 7.102 /** ParExpression = "(" Expression ")" 7.103 */ 7.104 JCExpression parExpression() { 7.105 + int pos = token.pos; 7.106 accept(LPAREN); 7.107 JCExpression t = parseExpression(); 7.108 accept(RPAREN); 7.109 - return t; 7.110 + return toP(F.at(pos).Parens(t)); 7.111 } 7.112 7.113 /** Block = "{" BlockStatements "}" 7.114 @@ -1661,7 +1656,7 @@ 7.115 // error recovery 7.116 if (token.pos == lastErrPos) 7.117 return stats.toList(); 7.118 - if (token.pos <= errorEndPos) { 7.119 + if (token.pos <= endPosTable.errorEndPos) { 7.120 skip(false, true, true, true); 7.121 lastErrPos = token.pos; 7.122 } 7.123 @@ -2270,7 +2265,7 @@ 7.124 boolean checkForImports = true; 7.125 boolean firstTypeDecl = true; 7.126 while (token.kind != EOF) { 7.127 - if (token.pos <= errorEndPos) { 7.128 + if (token.pos <= endPosTable.errorEndPos) { 7.129 // error recovery 7.130 skip(checkForImports, false, false, false); 7.131 if (token.kind == EOF) 7.132 @@ -2304,6 +2299,7 @@ 7.133 toplevel.docComments = docComments; 7.134 if (keepLineMap) 7.135 toplevel.lineMap = S.getLineMap(); 7.136 + toplevel.endPositions = this.endPosTable; 7.137 return toplevel; 7.138 } 7.139 7.140 @@ -2494,7 +2490,7 @@ 7.141 while (token.kind != RBRACE && token.kind != EOF) { 7.142 defs.appendList(classOrInterfaceBodyDeclaration(enumName, 7.143 false)); 7.144 - if (token.pos <= errorEndPos) { 7.145 + if (token.pos <= endPosTable.errorEndPos) { 7.146 // error recovery 7.147 skip(false, true, true, false); 7.148 } 7.149 @@ -2556,7 +2552,7 @@ 7.150 */ 7.151 List<JCTree> classOrInterfaceBody(Name className, boolean isInterface) { 7.152 accept(LBRACE); 7.153 - if (token.pos <= errorEndPos) { 7.154 + if (token.pos <= endPosTable.errorEndPos) { 7.155 // error recovery 7.156 skip(false, true, false, false); 7.157 if (token.kind == LBRACE) 7.158 @@ -2565,7 +2561,7 @@ 7.159 ListBuffer<JCTree> defs = new ListBuffer<JCTree>(); 7.160 while (token.kind != RBRACE && token.kind != EOF) { 7.161 defs.appendList(classOrInterfaceBodyDeclaration(className, isInterface)); 7.162 - if (token.pos <= errorEndPos) { 7.163 + if (token.pos <= endPosTable.errorEndPos) { 7.164 // error recovery 7.165 skip(false, true, true, false); 7.166 } 7.167 @@ -2697,7 +2693,7 @@ 7.168 defaultValue = null; 7.169 } 7.170 accept(SEMI); 7.171 - if (token.pos <= errorEndPos) { 7.172 + if (token.pos <= endPosTable.errorEndPos) { 7.173 // error recovery 7.174 skip(false, true, false, false); 7.175 if (token.kind == LBRACE) { 7.176 @@ -3028,4 +3024,112 @@ 7.177 allowTWR = true; 7.178 } 7.179 } 7.180 + 7.181 + /* 7.182 + * a functional source tree and end position mappings 7.183 + */ 7.184 + protected class SimpleEndPosTable extends AbstractEndPosTable { 7.185 + 7.186 + private final Map<JCTree, Integer> endPosMap; 7.187 + 7.188 + SimpleEndPosTable() { 7.189 + endPosMap = new HashMap<JCTree, Integer>(); 7.190 + } 7.191 + 7.192 + protected void storeEnd(JCTree tree, int endpos) { 7.193 + endPosMap.put(tree, errorEndPos > endpos ? errorEndPos : endpos); 7.194 + } 7.195 + 7.196 + protected <T extends JCTree> T to(T t) { 7.197 + storeEnd(t, token.endPos); 7.198 + return t; 7.199 + } 7.200 + 7.201 + protected <T extends JCTree> T toP(T t) { 7.202 + storeEnd(t, S.prevToken().endPos); 7.203 + return t; 7.204 + } 7.205 + 7.206 + public int getEndPos(JCTree tree) { 7.207 + Integer value = endPosMap.get(tree); 7.208 + return (value == null) ? Position.NOPOS : value; 7.209 + } 7.210 + 7.211 + public int replaceTree(JCTree oldTree, JCTree newTree) { 7.212 + Integer pos = endPosMap.remove(oldTree); 7.213 + if (pos != null) { 7.214 + endPosMap.put(newTree, pos); 7.215 + return pos; 7.216 + } 7.217 + return Position.NOPOS; 7.218 + } 7.219 + } 7.220 + 7.221 + /* 7.222 + * a default skeletal implementation without any mapping overhead. 7.223 + */ 7.224 + protected class EmptyEndPosTable extends AbstractEndPosTable { 7.225 + 7.226 + protected void storeEnd(JCTree tree, int endpos) { /* empty */ } 7.227 + 7.228 + protected <T extends JCTree> T to(T t) { 7.229 + return t; 7.230 + } 7.231 + 7.232 + protected <T extends JCTree> T toP(T t) { 7.233 + return t; 7.234 + } 7.235 + 7.236 + public int getEndPos(JCTree tree) { 7.237 + return Position.NOPOS; 7.238 + } 7.239 + 7.240 + public int replaceTree(JCTree oldTree, JCTree newTree) { 7.241 + return Position.NOPOS; 7.242 + } 7.243 + 7.244 + } 7.245 + 7.246 + protected abstract class AbstractEndPosTable implements EndPosTable { 7.247 + 7.248 + /** 7.249 + * Store the last error position. 7.250 + */ 7.251 + protected int errorEndPos; 7.252 + 7.253 + /** 7.254 + * Store ending position for a tree, the value of which is the greater 7.255 + * of last error position and the given ending position. 7.256 + * @param tree The tree. 7.257 + * @param endpos The ending position to associate with the tree. 7.258 + */ 7.259 + protected abstract void storeEnd(JCTree tree, int endpos); 7.260 + 7.261 + /** 7.262 + * Store current token's ending position for a tree, the value of which 7.263 + * will be the greater of last error position and the ending position of 7.264 + * the current token. 7.265 + * @param t The tree. 7.266 + */ 7.267 + protected abstract <T extends JCTree> T to(T t); 7.268 + 7.269 + /** 7.270 + * Store current token's ending position for a tree, the value of which 7.271 + * will be the greater of last error position and the ending position of 7.272 + * the previous token. 7.273 + * @param t The tree. 7.274 + */ 7.275 + protected abstract <T extends JCTree> T toP(T t); 7.276 + 7.277 + /** 7.278 + * Set the error position during the parsing phases, the value of which 7.279 + * will be set only if it is greater than the last stored error position. 7.280 + * @param errPos The error position 7.281 + */ 7.282 + protected void setErrorEndPos(int errPos) { 7.283 + if (errPos > errorEndPos) { 7.284 + errorEndPos = errPos; 7.285 + } 7.286 + } 7.287 + } 7.288 }
8.1 --- a/src/share/classes/com/sun/tools/javac/parser/ParserFactory.java Mon Nov 14 08:09:47 2011 -0800 8.2 +++ b/src/share/classes/com/sun/tools/javac/parser/ParserFactory.java Mon Nov 14 15:11:10 2011 -0800 8.3 @@ -1,5 +1,5 @@ 8.4 /* 8.5 - * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved. 8.6 + * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved. 8.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 8.8 * 8.9 * This code is free software; you can redistribute it and/or modify it 8.10 @@ -75,10 +75,6 @@ 8.11 8.12 public Parser newParser(CharSequence input, boolean keepDocComments, boolean keepEndPos, boolean keepLineMap) { 8.13 Lexer lexer = scannerFactory.newScanner(input, keepDocComments); 8.14 - if (keepEndPos) { 8.15 - return new EndPosParser(this, lexer, keepDocComments, keepLineMap); 8.16 - } else { 8.17 - return new JavacParser(this, lexer, keepDocComments, keepLineMap); 8.18 - } 8.19 + return new JavacParser(this, lexer, keepDocComments, keepLineMap, keepEndPos); 8.20 } 8.21 }
9.1 --- a/src/share/classes/com/sun/tools/javac/tree/JCTree.java Mon Nov 14 08:09:47 2011 -0800 9.2 +++ b/src/share/classes/com/sun/tools/javac/tree/JCTree.java Mon Nov 14 15:11:10 2011 -0800 9.3 @@ -39,6 +39,7 @@ 9.4 import com.sun.tools.javac.code.*; 9.5 import com.sun.tools.javac.code.Scope.*; 9.6 import com.sun.tools.javac.code.Symbol.*; 9.7 +import com.sun.tools.javac.parser.EndPosTable; 9.8 import com.sun.source.tree.*; 9.9 9.10 import static com.sun.tools.javac.code.BoundKind.*; 9.11 @@ -450,7 +451,7 @@ 9.12 } 9.13 9.14 // for default DiagnosticPosition 9.15 - public int getEndPosition(Map<JCTree, Integer> endPosTable) { 9.16 + public int getEndPosition(EndPosTable endPosTable) { 9.17 return TreeInfo.getEndPos(this, endPosTable); 9.18 } 9.19 9.20 @@ -467,7 +468,7 @@ 9.21 * @param docComments A hashtable that stores all documentation comments 9.22 * indexed by the tree nodes they refer to. 9.23 * defined only if option -s is set. 9.24 - * @param endPositions A hashtable that stores ending positions of source 9.25 + * @param endPositions An object encapsulating ending positions of source 9.26 * ranges indexed by the tree nodes they belong to. 9.27 * Defined only if option -Xjcov is set. 9.28 */ 9.29 @@ -481,7 +482,7 @@ 9.30 public StarImportScope starImportScope; 9.31 public Position.LineMap lineMap = null; 9.32 public Map<JCTree, String> docComments = null; 9.33 - public Map<JCTree, Integer> endPositions = null; 9.34 + public EndPosTable endPositions = null; 9.35 protected JCCompilationUnit(List<JCAnnotation> packageAnnotations, 9.36 JCExpression pid, 9.37 List<JCTree> defs,
10.1 --- a/src/share/classes/com/sun/tools/javac/tree/TreeInfo.java Mon Nov 14 08:09:47 2011 -0800 10.2 +++ b/src/share/classes/com/sun/tools/javac/tree/TreeInfo.java Mon Nov 14 15:11:10 2011 -0800 10.3 @@ -28,10 +28,10 @@ 10.4 import com.sun.source.tree.Tree; 10.5 import com.sun.tools.javac.comp.AttrContext; 10.6 import com.sun.tools.javac.comp.Env; 10.7 -import java.util.Map; 10.8 import com.sun.tools.javac.util.*; 10.9 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; 10.10 import com.sun.tools.javac.code.*; 10.11 +import com.sun.tools.javac.parser.EndPosTable; 10.12 import com.sun.tools.javac.tree.JCTree.*; 10.13 10.14 import static com.sun.tools.javac.code.Flags.*; 10.15 @@ -346,17 +346,17 @@ 10.16 10.17 /** The end position of given tree, given a table of end positions generated by the parser 10.18 */ 10.19 - public static int getEndPos(JCTree tree, Map<JCTree, Integer> endPositions) { 10.20 + public static int getEndPos(JCTree tree, EndPosTable endPosTable) { 10.21 if (tree == null) 10.22 return Position.NOPOS; 10.23 10.24 - if (endPositions == null) { 10.25 + if (endPosTable == null) { 10.26 // fall back on limited info in the tree 10.27 return endPos(tree); 10.28 } 10.29 10.30 - Integer mapPos = endPositions.get(tree); 10.31 - if (mapPos != null) 10.32 + int mapPos = endPosTable.getEndPos(tree); 10.33 + if (mapPos != Position.NOPOS) 10.34 return mapPos; 10.35 10.36 switch(tree.getTag()) { 10.37 @@ -364,7 +364,7 @@ 10.38 case SL_ASG: case SR_ASG: case USR_ASG: 10.39 case PLUS_ASG: case MINUS_ASG: case MUL_ASG: 10.40 case DIV_ASG: case MOD_ASG: 10.41 - return getEndPos(((JCAssignOp) tree).rhs, endPositions); 10.42 + return getEndPos(((JCAssignOp) tree).rhs, endPosTable); 10.43 case OR: case AND: case BITOR: 10.44 case BITXOR: case BITAND: case EQ: 10.45 case NE: case LT: case GT: 10.46 @@ -372,62 +372,62 @@ 10.47 case SR: case USR: case PLUS: 10.48 case MINUS: case MUL: case DIV: 10.49 case MOD: 10.50 - return getEndPos(((JCBinary) tree).rhs, endPositions); 10.51 + return getEndPos(((JCBinary) tree).rhs, endPosTable); 10.52 case CASE: 10.53 - return getEndPos(((JCCase) tree).stats.last(), endPositions); 10.54 + return getEndPos(((JCCase) tree).stats.last(), endPosTable); 10.55 case CATCH: 10.56 - return getEndPos(((JCCatch) tree).body, endPositions); 10.57 + return getEndPos(((JCCatch) tree).body, endPosTable); 10.58 case CONDEXPR: 10.59 - return getEndPos(((JCConditional) tree).falsepart, endPositions); 10.60 + return getEndPos(((JCConditional) tree).falsepart, endPosTable); 10.61 case FORLOOP: 10.62 - return getEndPos(((JCForLoop) tree).body, endPositions); 10.63 + return getEndPos(((JCForLoop) tree).body, endPosTable); 10.64 case FOREACHLOOP: 10.65 - return getEndPos(((JCEnhancedForLoop) tree).body, endPositions); 10.66 + return getEndPos(((JCEnhancedForLoop) tree).body, endPosTable); 10.67 case IF: { 10.68 JCIf node = (JCIf)tree; 10.69 if (node.elsepart == null) { 10.70 - return getEndPos(node.thenpart, endPositions); 10.71 + return getEndPos(node.thenpart, endPosTable); 10.72 } else { 10.73 - return getEndPos(node.elsepart, endPositions); 10.74 + return getEndPos(node.elsepart, endPosTable); 10.75 } 10.76 } 10.77 case LABELLED: 10.78 - return getEndPos(((JCLabeledStatement) tree).body, endPositions); 10.79 + return getEndPos(((JCLabeledStatement) tree).body, endPosTable); 10.80 case MODIFIERS: 10.81 - return getEndPos(((JCModifiers) tree).annotations.last(), endPositions); 10.82 + return getEndPos(((JCModifiers) tree).annotations.last(), endPosTable); 10.83 case SYNCHRONIZED: 10.84 - return getEndPos(((JCSynchronized) tree).body, endPositions); 10.85 + return getEndPos(((JCSynchronized) tree).body, endPosTable); 10.86 case TOPLEVEL: 10.87 - return getEndPos(((JCCompilationUnit) tree).defs.last(), endPositions); 10.88 + return getEndPos(((JCCompilationUnit) tree).defs.last(), endPosTable); 10.89 case TRY: { 10.90 JCTry node = (JCTry)tree; 10.91 if (node.finalizer != null) { 10.92 - return getEndPos(node.finalizer, endPositions); 10.93 + return getEndPos(node.finalizer, endPosTable); 10.94 } else if (!node.catchers.isEmpty()) { 10.95 - return getEndPos(node.catchers.last(), endPositions); 10.96 + return getEndPos(node.catchers.last(), endPosTable); 10.97 } else { 10.98 - return getEndPos(node.body, endPositions); 10.99 + return getEndPos(node.body, endPosTable); 10.100 } 10.101 } 10.102 case WILDCARD: 10.103 - return getEndPos(((JCWildcard) tree).inner, endPositions); 10.104 + return getEndPos(((JCWildcard) tree).inner, endPosTable); 10.105 case TYPECAST: 10.106 - return getEndPos(((JCTypeCast) tree).expr, endPositions); 10.107 + return getEndPos(((JCTypeCast) tree).expr, endPosTable); 10.108 case TYPETEST: 10.109 - return getEndPos(((JCInstanceOf) tree).clazz, endPositions); 10.110 + return getEndPos(((JCInstanceOf) tree).clazz, endPosTable); 10.111 case POS: 10.112 case NEG: 10.113 case NOT: 10.114 case COMPL: 10.115 case PREINC: 10.116 case PREDEC: 10.117 - return getEndPos(((JCUnary) tree).arg, endPositions); 10.118 + return getEndPos(((JCUnary) tree).arg, endPosTable); 10.119 case WHILELOOP: 10.120 - return getEndPos(((JCWhileLoop) tree).body, endPositions); 10.121 + return getEndPos(((JCWhileLoop) tree).body, endPosTable); 10.122 case ERRONEOUS: { 10.123 JCErroneous node = (JCErroneous)tree; 10.124 if (node.errs != null && node.errs.nonEmpty()) 10.125 - return getEndPos(node.errs.last(), endPositions); 10.126 + return getEndPos(node.errs.last(), endPosTable); 10.127 } 10.128 } 10.129 return Position.NOPOS; 10.130 @@ -444,7 +444,7 @@ 10.131 public JCTree getTree() { return tree; } 10.132 public int getStartPosition() { return TreeInfo.getStartPos(tree); } 10.133 public int getPreferredPosition() { return endPos; } 10.134 - public int getEndPosition(Map<JCTree, Integer> endPosTable) { 10.135 + public int getEndPosition(EndPosTable endPosTable) { 10.136 return TreeInfo.getEndPos(tree, endPosTable); 10.137 } 10.138 };
11.1 --- a/src/share/classes/com/sun/tools/javac/util/DiagnosticSource.java Mon Nov 14 08:09:47 2011 -0800 11.2 +++ b/src/share/classes/com/sun/tools/javac/util/DiagnosticSource.java Mon Nov 14 15:11:10 2011 -0800 11.3 @@ -1,5 +1,5 @@ 11.4 /* 11.5 - * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved. 11.6 + * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved. 11.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 11.8 * 11.9 * This code is free software; you can redistribute it and/or modify it 11.10 @@ -32,6 +32,7 @@ 11.11 import javax.tools.JavaFileObject; 11.12 11.13 import com.sun.tools.javac.file.JavacFileManager; 11.14 +import com.sun.tools.javac.parser.EndPosTable; 11.15 import com.sun.tools.javac.tree.JCTree; 11.16 11.17 import static com.sun.tools.javac.util.LayoutCharacters.*; 11.18 @@ -128,11 +129,11 @@ 11.19 } 11.20 } 11.21 11.22 - public Map<JCTree, Integer> getEndPosTable() { 11.23 + public EndPosTable getEndPosTable() { 11.24 return endPosTable; 11.25 } 11.26 11.27 - public void setEndPosTable(Map<JCTree, Integer> t) { 11.28 + public void setEndPosTable(EndPosTable t) { 11.29 if (endPosTable != null && endPosTable != t) 11.30 throw new IllegalStateException("endPosTable already set"); 11.31 endPosTable = t; 11.32 @@ -199,7 +200,7 @@ 11.33 /** The underlying file object. */ 11.34 protected JavaFileObject fileObject; 11.35 11.36 - protected Map<JCTree, Integer> endPosTable; 11.37 + protected EndPosTable endPosTable; 11.38 11.39 /** A soft reference to the content of the file object. */ 11.40 protected SoftReference<char[]> refBuf;
12.1 --- a/src/share/classes/com/sun/tools/javac/util/JCDiagnostic.java Mon Nov 14 08:09:47 2011 -0800 12.2 +++ b/src/share/classes/com/sun/tools/javac/util/JCDiagnostic.java Mon Nov 14 15:11:10 2011 -0800 12.3 @@ -35,6 +35,7 @@ 12.4 12.5 import com.sun.tools.javac.api.DiagnosticFormatter; 12.6 import com.sun.tools.javac.code.Lint.LintCategory; 12.7 +import com.sun.tools.javac.parser.EndPosTable; 12.8 import com.sun.tools.javac.tree.JCTree; 12.9 12.10 import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticType.*; 12.11 @@ -313,7 +314,7 @@ 12.12 /** If there is a tree node, and if endPositions are available, get 12.13 * the end position of the tree node. Otherwise, just returns the 12.14 * same as getPreferredPosition(). */ 12.15 - int getEndPosition(Map<JCTree, Integer> endPosTable); 12.16 + int getEndPosition(EndPosTable endPosTable); 12.17 } 12.18 12.19 /** 12.20 @@ -337,7 +338,7 @@ 12.21 return pos; 12.22 } 12.23 12.24 - public int getEndPosition(Map<JCTree, Integer> endPosTable) { 12.25 + public int getEndPosition(EndPosTable endPosTable) { 12.26 return pos; 12.27 } 12.28
13.1 --- a/src/share/classes/com/sun/tools/javac/util/Log.java Mon Nov 14 08:09:47 2011 -0800 13.2 +++ b/src/share/classes/com/sun/tools/javac/util/Log.java Mon Nov 14 15:11:10 2011 -0800 13.3 @@ -38,6 +38,7 @@ 13.4 13.5 import com.sun.tools.javac.api.DiagnosticFormatter; 13.6 import com.sun.tools.javac.main.OptionName; 13.7 +import com.sun.tools.javac.parser.EndPosTable; 13.8 import com.sun.tools.javac.tree.JCTree; 13.9 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; 13.10 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType; 13.11 @@ -250,9 +251,9 @@ 13.12 return diagListener != null; 13.13 } 13.14 13.15 - public void setEndPosTable(JavaFileObject name, Map<JCTree, Integer> table) { 13.16 + public void setEndPosTable(JavaFileObject name, EndPosTable endPosTable) { 13.17 name.getClass(); // null check 13.18 - getSource(name).setEndPosTable(table); 13.19 + getSource(name).setEndPosTable(endPosTable); 13.20 } 13.21 13.22 /** Return current sourcefile.
14.1 --- a/test/tools/javac/6304921/TestLog.java Mon Nov 14 08:09:47 2011 -0800 14.2 +++ b/test/tools/javac/6304921/TestLog.java Mon Nov 14 15:11:10 2011 -0800 14.3 @@ -1,5 +1,5 @@ 14.4 /* 14.5 - * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 14.6 + * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved. 14.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 14.8 * 14.9 * This code is free software; you can redistribute it and/or modify it 14.10 @@ -33,6 +33,7 @@ 14.11 import javax.tools.JavaFileObject; 14.12 import javax.tools.SimpleJavaFileObject; 14.13 import com.sun.tools.javac.file.JavacFileManager; 14.14 +import com.sun.tools.javac.parser.EndPosTable; 14.15 import com.sun.tools.javac.parser.Parser; 14.16 import com.sun.tools.javac.parser.ParserFactory; 14.17 import com.sun.tools.javac.tree.JCTree; 14.18 @@ -97,9 +98,9 @@ 14.19 } 14.20 14.21 private static class LogTester extends TreeScanner { 14.22 - LogTester(Log log, java.util.Map<JCTree, Integer> endPositions) { 14.23 + LogTester(Log log, EndPosTable endPosTable) { 14.24 this.log = log; 14.25 - this.endPositions = endPositions; 14.26 + this.endPosTable = endPosTable; 14.27 } 14.28 14.29 public void visitIf(JCTree.JCIf tree) { 14.30 @@ -117,7 +118,7 @@ 14.31 } 14.32 14.33 private Log log; 14.34 - private java.util.Map<JCTree, Integer> endPositions; 14.35 + private EndPosTable endPosTable; 14.36 } 14.37 14.38 private static class StringJavaFileObject extends SimpleJavaFileObject {
15.1 --- a/test/tools/javac/failover/CheckAttributedTree.java Mon Nov 14 08:09:47 2011 -0800 15.2 +++ b/test/tools/javac/failover/CheckAttributedTree.java Mon Nov 14 15:11:10 2011 -0800 15.3 @@ -55,12 +55,8 @@ 15.4 import java.io.PrintWriter; 15.5 import java.io.StringWriter; 15.6 import java.lang.reflect.Field; 15.7 -import java.lang.reflect.Modifier; 15.8 -import java.nio.charset.Charset; 15.9 import java.util.ArrayList; 15.10 -import java.util.HashMap; 15.11 import java.util.List; 15.12 -import java.util.Map; 15.13 import javax.tools.Diagnostic; 15.14 import javax.tools.DiagnosticListener; 15.15 import javax.tools.JavaFileObject; 15.16 @@ -72,8 +68,8 @@ 15.17 import com.sun.tools.javac.api.JavacTool; 15.18 import com.sun.tools.javac.code.Symbol; 15.19 import com.sun.tools.javac.code.Type; 15.20 +import com.sun.tools.javac.parser.EndPosTable; 15.21 import com.sun.tools.javac.tree.JCTree; 15.22 -import com.sun.tools.javac.tree.JCTree.JCClassDecl; 15.23 import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; 15.24 import com.sun.tools.javac.tree.JCTree.JCImport; 15.25 import com.sun.tools.javac.tree.TreeInfo; 15.26 @@ -421,7 +417,7 @@ 15.27 } 15.28 15.29 JavaFileObject sourcefile; 15.30 - Map<JCTree, Integer> endPosTable; 15.31 + EndPosTable endPosTable; 15.32 Info encl; 15.33 } 15.34 15.35 @@ -437,7 +433,7 @@ 15.36 end = Integer.MAX_VALUE; 15.37 } 15.38 15.39 - Info(JCTree tree, Map<JCTree, Integer> endPosTable) { 15.40 + Info(JCTree tree, EndPosTable endPosTable) { 15.41 this.tree = tree; 15.42 tag = tree.getTag(); 15.43 start = TreeInfo.getStartPos(tree);
16.1 --- a/test/tools/javac/tree/TreePosTest.java Mon Nov 14 08:09:47 2011 -0800 16.2 +++ b/test/tools/javac/tree/TreePosTest.java Mon Nov 14 15:11:10 2011 -0800 16.3 @@ -73,6 +73,7 @@ 16.4 import com.sun.source.util.JavacTask; 16.5 import com.sun.tools.javac.api.JavacTool; 16.6 import com.sun.tools.javac.code.Flags; 16.7 +import com.sun.tools.javac.parser.EndPosTable; 16.8 import com.sun.tools.javac.tree.JCTree; 16.9 import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; 16.10 import com.sun.tools.javac.tree.JCTree.JCNewClass; 16.11 @@ -435,7 +436,7 @@ 16.12 } 16.13 16.14 JavaFileObject sourcefile; 16.15 - Map<JCTree, Integer> endPosTable; 16.16 + EndPosTable endPosTable; 16.17 Info encl; 16.18 16.19 } 16.20 @@ -452,7 +453,7 @@ 16.21 end = Integer.MAX_VALUE; 16.22 } 16.23 16.24 - Info(JCTree tree, Map<JCTree, Integer> endPosTable) { 16.25 + Info(JCTree tree, EndPosTable endPosTable) { 16.26 this.tree = tree; 16.27 tag = tree.getTag(); 16.28 start = TreeInfo.getStartPos(tree);