1 /*
  2  * Copyright (c) 2001, 2019, 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.*;
 29 
 30 import com.sun.tools.javac.resources.CompilerProperties.Warnings;
 31 import com.sun.tools.javac.tree.*;
 32 import com.sun.tools.javac.util.*;
 33 import com.sun.tools.javac.util.List;
 34 import com.sun.tools.javac.tree.JCTree.*;
 35 import com.sun.tools.javac.tree.EndPosTable;
 36 import com.sun.tools.javac.tree.JCTree.JCSwitchExpression;
 37 
 38 /** This class contains the CharacterRangeTable for some method
 39  *  and the hashtable for mapping trees or lists of trees to their
 40  *  ending positions.
 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 CRTable
 48 implements CRTFlags {
 49 
 50     private final boolean crtDebug = false;
 51 
 52     /** The list of CRTable entries.
 53      */
 54     private ListBuffer<CRTEntry> entries = new ListBuffer<>();
 55 
 56     /** The hashtable for source positions.
 57      */
 58     private Map<Object,SourceRange> positions = new HashMap<>();
 59 
 60     /** The object for ending positions stored in the parser.
 61      */
 62     private EndPosTable endPosTable;
 63 
 64     /** The tree of the method this table is intended for.
 65      *  We should traverse this tree to get source ranges.
 66      */
 67     JCTree.JCMethodDecl methodTree;
 68 
 69     /** Constructor
 70      */
 71     public CRTable(JCTree.JCMethodDecl tree, EndPosTable endPosTable) {
 72         this.methodTree = tree;
 73         this.endPosTable = endPosTable;
 74     }
 75 
 76     /** Create a new CRTEntry and add it to the entries.
 77      *  @param tree     The tree or the list of trees for which
 78      *                  we are storing the code pointers.
 79      *  @param flags    The set of flags designating type of the entry.
 80      *  @param startPc  The starting code position.
 81      *  @param endPc    The ending code position.
 82      */
 83     public void put(Object tree, int flags, int startPc, int endPc) {
 84         entries.append(new CRTEntry(tree, flags, startPc, endPc));
 85     }
 86 
 87     /** Compute source positions and write CRT to the databuf.
 88      *  @param databuf  The buffer to write bytecodes to.
 89      */
 90     public int writeCRT(ByteBuffer databuf, Position.LineMap lineMap, Log log) {
 91 
 92         int crtEntries = 0;
 93 
 94         // compute source positions for the method
 95         new SourceComputer().csp(methodTree);
 96 
 97         for (List<CRTEntry> l = entries.toList(); l.nonEmpty(); l = l.tail) {
 98 
 99             CRTEntry entry = l.head;
100 
101             // eliminate entries that do not produce bytecodes:
102             // for example, empty blocks and statements
103             if (entry.startPc == entry.endPc)
104                 continue;
105 
106             SourceRange pos = positions.get(entry.tree);
107             Assert.checkNonNull(pos, "CRT: tree source positions are undefined");
108             if ((pos.startPos == Position.NOPOS) || (pos.endPos == Position.NOPOS))
109                 continue;
110 
111             if (crtDebug) {
112                 System.out.println("Tree: " + entry.tree + ", type:" + getTypes(entry.flags));
113                 System.out.print("Start: pos = " + pos.startPos + ", pc = " + entry.startPc);
114             }
115 
116             // encode startPos into line/column representation
117             int startPos = encodePosition(pos.startPos, lineMap, log);
118             if (startPos == Position.NOPOS)
119                 continue;
120 
121             if (crtDebug) {
122                 System.out.print("End:   pos = " + pos.endPos + ", pc = " + (entry.endPc - 1));
123             }
124 
125             // encode endPos into line/column representation
126             int endPos = encodePosition(pos.endPos, lineMap, log);
127             if (endPos == Position.NOPOS)
128                 continue;
129 
130             // write attribute
131             databuf.appendChar(entry.startPc);
132             // 'endPc - 1' because endPc actually points to start of the next command
133             databuf.appendChar(entry.endPc - 1);
134             databuf.appendInt(startPos);
135             databuf.appendInt(endPos);
136             databuf.appendChar(entry.flags);
137 
138             crtEntries++;
139         }
140 
141         return crtEntries;
142     }
143 
144     /** Return the number of the entries.
145      */
146     public int length() {
147         return entries.length();
148     }
149 
150     /** Return string describing flags enabled.
151      */
152     private String getTypes(int flags) {
153         String types = "";
154         if ((flags & CRT_STATEMENT)       != 0) types += " CRT_STATEMENT";
155         if ((flags & CRT_BLOCK)           != 0) types += " CRT_BLOCK";
156         if ((flags & CRT_ASSIGNMENT)      != 0) types += " CRT_ASSIGNMENT";
157         if ((flags & CRT_FLOW_CONTROLLER) != 0) types += " CRT_FLOW_CONTROLLER";
158         if ((flags & CRT_FLOW_TARGET)     != 0) types += " CRT_FLOW_TARGET";
159         if ((flags & CRT_INVOKE)          != 0) types += " CRT_INVOKE";
160         if ((flags & CRT_CREATE)          != 0) types += " CRT_CREATE";
161         if ((flags & CRT_BRANCH_TRUE)     != 0) types += " CRT_BRANCH_TRUE";
162         if ((flags & CRT_BRANCH_FALSE)    != 0) types += " CRT_BRANCH_FALSE";
163         return types;
164     }
165 
166     /** Source file positions in CRT are integers in the format:
167      *  {@literal line-number << LINESHIFT + column-number }
168      */
169      private int encodePosition(int pos, Position.LineMap lineMap, Log log) {
170          int line = lineMap.getLineNumber(pos);
171          int col = lineMap.getColumnNumber(pos);
172          int new_pos = Position.encodePosition(line, col);
173          if (crtDebug) {
174              System.out.println(", line = " + line + ", column = " + col +
175                                 ", new_pos = " + new_pos);
176          }
177          if (new_pos == Position.NOPOS)
178              log.warning(pos, Warnings.PositionOverflow(line));
179 
180         return new_pos;
181      }
182 
183 /* ************************************************************************
184  * Traversal methods
185  *************************************************************************/
186 
187     /**
188      *  This class contains methods to compute source positions for trees.
189      *  Extends Tree.Visitor to traverse the abstract syntax tree.
190      */
191     class SourceComputer extends JCTree.Visitor {
192 
193         /** The result of the tree traversal methods.
194          */
195         SourceRange result;
196 
197         /** Visitor method: compute source positions for a single node.
198          */
199         public SourceRange csp(JCTree tree) {
200             if (tree == null) return null;
201             tree.accept(this);
202             if (result != null) {
203                 positions.put(tree, result);
204             }
205             return result;
206         }
207 
208         /** Visitor method: compute source positions for a list of nodes.
209          */
210         public SourceRange csp(List<? extends JCTree> trees) {
211             if ((trees == null) || !(trees.nonEmpty())) return null;
212             SourceRange list_sr = new SourceRange();
213             for (List<? extends JCTree> l = trees; l.nonEmpty(); l = l.tail) {
214                 list_sr.mergeWith(csp(l.head));
215             }
216             positions.put(trees, list_sr);
217             return list_sr;
218         }
219 
220         /**  Visitor method: compute source positions for
221          *    a list of case blocks of switch statements.
222          */
223         public SourceRange cspCases(List<JCCase> trees) {
224             if ((trees == null) || !(trees.nonEmpty())) return null;
225             SourceRange list_sr = new SourceRange();
226             for (List<JCCase> l = trees; l.nonEmpty(); l = l.tail) {
227                 list_sr.mergeWith(csp(l.head));
228             }
229             positions.put(trees, list_sr);
230             return list_sr;
231         }
232 
233         /**  Visitor method: compute source positions for
234          *   a list of catch clauses in try statements.
235          */
236         public SourceRange cspCatchers(List<JCCatch> trees) {
237             if ((trees == null) || !(trees.nonEmpty())) return null;
238             SourceRange list_sr = new SourceRange();
239             for (List<JCCatch> l = trees; l.nonEmpty(); l = l.tail) {
240                 list_sr.mergeWith(csp(l.head));
241             }
242             positions.put(trees, list_sr);
243             return list_sr;
244         }
245 
246         public void visitMethodDef(JCMethodDecl tree) {
247             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
248             sr.mergeWith(csp(tree.body));
249             result = sr;
250         }
251 
252         public void visitVarDef(JCVariableDecl tree) {
253             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
254             csp(tree.vartype);
255             sr.mergeWith(csp(tree.init));
256             result = sr;
257         }
258 
259         public void visitSkip(JCSkip tree) {
260             // endPos is the same as startPos for the empty statement
261             SourceRange sr = new SourceRange(startPos(tree), startPos(tree));
262             result = sr;
263         }
264 
265         public void visitBlock(JCBlock tree) {
266             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
267             csp(tree.stats);    // doesn't compare because block's ending position is defined
268             result = sr;
269         }
270 
271         public void visitDoLoop(JCDoWhileLoop tree) {
272             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
273             sr.mergeWith(csp(tree.body));
274             sr.mergeWith(csp(tree.cond));
275             result = sr;
276         }
277 
278         public void visitWhileLoop(JCWhileLoop tree) {
279             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
280             sr.mergeWith(csp(tree.cond));
281             sr.mergeWith(csp(tree.body));
282             result = sr;
283         }
284 
285         public void visitForLoop(JCForLoop tree) {
286             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
287             sr.mergeWith(csp(tree.init));
288             sr.mergeWith(csp(tree.cond));
289             sr.mergeWith(csp(tree.step));
290             sr.mergeWith(csp(tree.body));
291             result = sr;
292         }
293 
294         public void visitForeachLoop(JCEnhancedForLoop tree) {
295             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
296             sr.mergeWith(csp(tree.var));
297             sr.mergeWith(csp(tree.expr));
298             sr.mergeWith(csp(tree.body));
299             result = sr;
300         }
301 
302         public void visitLabelled(JCLabeledStatement tree) {
303             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
304             sr.mergeWith(csp(tree.body));
305             result = sr;
306         }
307 
308         public void visitSwitch(JCSwitch tree) {
309             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
310             sr.mergeWith(csp(tree.selector));
311             sr.mergeWith(cspCases(tree.cases));
312             result = sr;
313         }
314 
315         @Override
316         public void visitSwitchExpression(JCSwitchExpression tree) {
317             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
318             sr.mergeWith(csp(tree.selector));
319             sr.mergeWith(cspCases(tree.cases));
320             result = sr;
321         }
322 
323         public void visitCase(JCCase tree) {
324             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
325             sr.mergeWith(csp(tree.labels));
326             sr.mergeWith(csp(tree.stats));
327             result = sr;
328         }
329 
330         @Override
331         public void visitDefaultCaseLabel(JCTree.JCDefaultCaseLabel that) {
332             result = null;
333         }
334 
335         public void visitSynchronized(JCSynchronized tree) {
336             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
337             sr.mergeWith(csp(tree.lock));
338             sr.mergeWith(csp(tree.body));
339             result = sr;
340         }
341 
342         public void visitTry(JCTry tree) {
343             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
344             sr.mergeWith(csp(tree.resources));
345             sr.mergeWith(csp(tree.body));
346             sr.mergeWith(cspCatchers(tree.catchers));
347             sr.mergeWith(csp(tree.finalizer));
348             result = sr;
349         }
350 
351         public void visitCatch(JCCatch tree) {
352             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
353             sr.mergeWith(csp(tree.param));
354             sr.mergeWith(csp(tree.body));
355             result = sr;
356         }
357 
358         public void visitConditional(JCConditional tree) {
359             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
360             sr.mergeWith(csp(tree.cond));
361             sr.mergeWith(csp(tree.truepart));
362             sr.mergeWith(csp(tree.falsepart));
363             result = sr;
364         }
365 
366         public void visitIf(JCIf tree) {
367             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
368             sr.mergeWith(csp(tree.cond));
369             sr.mergeWith(csp(tree.thenpart));
370             sr.mergeWith(csp(tree.elsepart));
371             result = sr;
372         }
373 
374         public void visitExec(JCExpressionStatement tree) {
375             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
376             sr.mergeWith(csp(tree.expr));
377             result = sr;
378         }
379 
380         public void visitBreak(JCBreak tree) {
381             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
382             result = sr;
383         }
384 
385         public void visitYield(JCYield tree) {
386             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
387             sr.mergeWith(csp(tree.value));
388             result = sr;
389         }
390 
391         public void visitContinue(JCContinue tree) {
392             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
393             result = sr;
394         }
395 
396         public void visitReturn(JCReturn tree) {
397             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
398             sr.mergeWith(csp(tree.expr));
399             result = sr;
400         }
401 
402         public void visitThrow(JCThrow tree) {
403             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
404             sr.mergeWith(csp(tree.expr));
405             result = sr;
406         }
407 
408         public void visitAssert(JCAssert tree) {
409             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
410             sr.mergeWith(csp(tree.cond));
411             sr.mergeWith(csp(tree.detail));
412             result = sr;
413         }
414 
415         public void visitApply(JCMethodInvocation tree) {
416             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
417             sr.mergeWith(csp(tree.meth));
418             sr.mergeWith(csp(tree.args));
419             result = sr;
420         }
421 
422         public void visitNewClass(JCNewClass tree) {
423             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
424             sr.mergeWith(csp(tree.encl));
425             sr.mergeWith(csp(tree.clazz));
426             sr.mergeWith(csp(tree.args));
427             sr.mergeWith(csp(tree.def));
428             result = sr;
429         }
430 
431         public void visitNewArray(JCNewArray tree) {
432             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
433             sr.mergeWith(csp(tree.elemtype));
434             sr.mergeWith(csp(tree.dims));
435             sr.mergeWith(csp(tree.elems));
436             result = sr;
437         }
438 
439         public void visitParens(JCParens tree) {
440             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
441             sr.mergeWith(csp(tree.expr));
442             result = sr;
443         }
444 
445         public void visitAssign(JCAssign tree) {
446             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
447             sr.mergeWith(csp(tree.lhs));
448             sr.mergeWith(csp(tree.rhs));
449             result = sr;
450         }
451 
452         public void visitAssignop(JCAssignOp tree) {
453             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
454             sr.mergeWith(csp(tree.lhs));
455             sr.mergeWith(csp(tree.rhs));
456             result = sr;
457         }
458 
459         public void visitUnary(JCUnary tree) {
460             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
461             sr.mergeWith(csp(tree.arg));
462             result = sr;
463         }
464 
465         public void visitBinary(JCBinary tree) {
466             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
467             sr.mergeWith(csp(tree.lhs));
468             sr.mergeWith(csp(tree.rhs));
469             result = sr;
470         }
471 
472         public void visitTypeCast(JCTypeCast tree) {
473             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
474             sr.mergeWith(csp(tree.clazz));
475             sr.mergeWith(csp(tree.expr));
476             result = sr;
477         }
478 
479         public void visitTypeTest(JCInstanceOf tree) {
480             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
481             sr.mergeWith(csp(tree.expr));
482             sr.mergeWith(csp(tree.pattern));
483             result = sr;
484         }
485 
486         public void visitIndexed(JCArrayAccess tree) {
487             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
488             sr.mergeWith(csp(tree.indexed));
489             sr.mergeWith(csp(tree.index));
490             result = sr;
491         }
492 
493         public void visitSelect(JCFieldAccess tree) {
494             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
495             sr.mergeWith(csp(tree.selected));
496             result = sr;
497         }
498 
499         public void visitIdent(JCIdent tree) {
500             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
501             result = sr;
502         }
503 
504         public void visitLiteral(JCLiteral tree) {
505             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
506             result = sr;
507         }
508 
509         public void visitTypeIdent(JCPrimitiveTypeTree tree) {
510             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
511             result = sr;
512         }
513 
514         public void visitTypeArray(JCArrayTypeTree tree) {
515             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
516             sr.mergeWith(csp(tree.elemtype));
517             result = sr;
518         }
519 
520         public void visitTypeApply(JCTypeApply tree) {
521             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
522             sr.mergeWith(csp(tree.clazz));
523             sr.mergeWith(csp(tree.arguments));
524             result = sr;
525         }
526 
527         @Override
528         public void visitLetExpr(LetExpr tree) {
529             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
530             sr.mergeWith(csp(tree.defs));
531             sr.mergeWith(csp(tree.expr));
532             result = sr;
533         }
534 
535         public void visitTypeParameter(JCTypeParameter tree) {
536             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
537             sr.mergeWith(csp(tree.bounds));
538             result = sr;
539         }
540 
541         @Override
542         public void visitTypeUnion(JCTypeUnion tree) {
543             SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
544             sr.mergeWith(csp(tree.alternatives));
545             result = sr;
546         }
547 
548         public void visitWildcard(JCWildcard tree) {
549             result = null;
550         }
551 
552         public void visitErroneous(JCErroneous tree) {
553             result = null;
554         }
555 
556         public void visitTree(JCTree tree) {
557             Assert.error();
558         }
559 
560         /** The start position of given tree.
561          */
562         public int startPos(JCTree tree) {
563             if (tree == null) return Position.NOPOS;
564             return TreeInfo.getStartPos(tree);
565         }
566 
567         /** The end position of given tree, if it has
568          *  defined endpos, NOPOS otherwise.
569          */
570         public int endPos(JCTree tree) {
571             if (tree == null) return Position.NOPOS;
572             return TreeInfo.getEndPos(tree, endPosTable);
573         }
574     }
575 
576     /** This class contains a CharacterRangeTableEntry.
577      */
578     static class CRTEntry {
579 
580         /** A tree or a list of trees to obtain source positions.
581          */
582         Object tree;
583 
584         /** The flags described in the CharacterRangeTable spec.
585          */
586         int flags;
587 
588         /** The starting code position of this entry.
589          */
590         int startPc;
591 
592         /** The ending code position of this entry.
593          */
594         int endPc;
595 
596         /** Constructor */
597         CRTEntry(Object tree, int flags, int startPc, int endPc) {
598             this.tree = tree;
599             this.flags = flags;
600             this.startPc = startPc;
601             this.endPc = endPc;
602         }
603     }
604 
605 
606     /** This class contains source positions
607      *  for some tree or list of trees.
608      */
609     static class SourceRange {
610 
611         /** The starting source position.
612          */
613         int startPos;
614 
615         /** The ending source position.
616          */
617         int endPos;
618 
619         /** Constructor */
620         SourceRange() {
621             startPos = Position.NOPOS;
622             endPos = Position.NOPOS;
623         }
624 
625         /** Constructor */
626         SourceRange(int startPos, int endPos) {
627             this.startPos = startPos;
628             this.endPos = endPos;
629         }
630 
631         /** Compare the starting and the ending positions
632          *  of the source range and combines them assigning
633          *  the widest range to this.
634          */
635         SourceRange mergeWith(SourceRange sr) {
636             if (sr == null) return this;
637             if (startPos == Position.NOPOS)
638                 startPos = sr.startPos;
639             else if (sr.startPos != Position.NOPOS)
640                 startPos = (startPos < sr.startPos ? startPos : sr.startPos);
641             if (endPos == Position.NOPOS)
642                 endPos = sr.endPos;
643             else if (sr.endPos != Position.NOPOS)
644                 endPos = (endPos > sr.endPos ? endPos : sr.endPos);
645             return this;
646         }
647     }
648 
649 }