1 /* 2 * Copyright (c) 2010, 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 jdk.nashorn.internal.ir; 27 28 import java.util.ArrayDeque; 29 import java.util.ArrayList; 30 import java.util.Deque; 31 import java.util.List; 32 import java.util.ListIterator; 33 34 /** 35 * This is a subclass of lexical context used for filling 36 * blocks (and function nodes) with statements. When popping 37 * a block from the lexical context, any statements that have 38 * been generated in it are commited to the block. This saves 39 * unnecessary object mutations and lexical context replacement 40 */ 41 public class BlockLexicalContext extends LexicalContext { 42 /** statement stack, each block on the lexical context maintains one of these, which is 43 * committed to the block on pop */ 44 private Deque<List<Statement>> sstack = new ArrayDeque<>(); 45 46 /** Last non debug statement emitted in this context */ 47 protected Statement lastStatement; 48 49 @Override 50 public <T extends LexicalContextNode> T push(final T node) { 51 T pushed = super.push(node); 52 if (node instanceof Block) { 53 sstack.push(new ArrayList<Statement>()); 54 } 55 return pushed; 56 } 57 58 /** 59 * Get the statement list from the stack, possibly filtered 60 * @return statement list 61 */ 62 protected List<Statement> popStatements() { 63 return sstack.pop(); 64 } 65 66 @Override 67 public <T extends LexicalContextNode> T pop(final T node) { 68 T expected = node; 69 if (node instanceof Block) { 70 final List<Statement> newStatements = popStatements(); 71 expected = (T)((Block)node).setStatements(this, newStatements); 72 if (!sstack.isEmpty()) { 73 lastStatement = lastStatement(sstack.peek()); 74 } 75 } 76 return super.pop(expected); 77 } 78 79 /** 80 * Append a statement to the block being generated 81 * @param statement statement to add 82 */ 83 public void appendStatement(final Statement statement) { 84 assert statement != null; 85 sstack.peek().add(statement); 86 lastStatement = statement; 87 } 88 89 /** 90 * Prepend a statement to the block being generated 91 * @param statement statement to prepend 92 * @return the prepended statement 93 */ 94 public Node prependStatement(final Statement statement) { 95 assert statement != null; 96 sstack.peek().add(0, statement); 97 return statement; 98 } 99 100 /** 101 * Get the last statement that was emitted into a block 102 * @return the last statement emitted 103 */ 104 public Statement getLastStatement() { 105 return lastStatement; 106 } 107 108 private static Statement lastStatement(final List<Statement> statements) { 109 for (final ListIterator<Statement> iter = statements.listIterator(statements.size()); iter.hasPrevious(); ) { 110 final Statement node = iter.previous(); 111 return node; 112 } 113 return null; 114 } 115 }