ReplaceCompileUnits.java revision 1070:34d55faf0b3a
11590Srgrimes/* 21590Srgrimes * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 31590Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 41590Srgrimes * 51590Srgrimes * This code is free software; you can redistribute it and/or modify it 61590Srgrimes * under the terms of the GNU General Public License version 2 only, as 71590Srgrimes * published by the Free Software Foundation. Oracle designates this 81590Srgrimes * particular file as subject to the "Classpath" exception as provided 91590Srgrimes * by Oracle in the LICENSE file that accompanied this code. 101590Srgrimes * 111590Srgrimes * This code is distributed in the hope that it will be useful, but WITHOUT 121590Srgrimes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 131590Srgrimes * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 141590Srgrimes * version 2 for more details (a copy is included in the LICENSE file that 151590Srgrimes * accompanied this code). 161590Srgrimes * 171590Srgrimes * You should have received a copy of the GNU General Public License version 181590Srgrimes * 2 along with this work; if not, write to the Free Software Foundation, 191590Srgrimes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 201590Srgrimes * 211590Srgrimes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 221590Srgrimes * or visit www.oracle.com if you need additional information or have any 231590Srgrimes * questions. 241590Srgrimes */ 251590Srgrimes 261590Srgrimespackage jdk.nashorn.internal.codegen; 271590Srgrimes 281590Srgrimesimport java.util.ArrayList; 291590Srgrimesimport java.util.List; 301590Srgrimesimport jdk.nashorn.internal.ir.CompileUnitHolder; 311590Srgrimesimport jdk.nashorn.internal.ir.FunctionNode; 321590Srgrimesimport jdk.nashorn.internal.ir.FunctionNode.CompilationState; 331590Srgrimesimport jdk.nashorn.internal.ir.LexicalContext; 341590Srgrimesimport jdk.nashorn.internal.ir.LiteralNode; 3528693Scharnierimport jdk.nashorn.internal.ir.LiteralNode.ArrayLiteralNode; 361590Srgrimesimport jdk.nashorn.internal.ir.LiteralNode.ArrayLiteralNode.ArrayUnit; 371590Srgrimesimport jdk.nashorn.internal.ir.Node; 38123441Sbdeimport jdk.nashorn.internal.ir.visitor.NodeVisitor; 391590Srgrimes 40123441Sbde/** 411590Srgrimes * Base class for a node visitor that replaces {@link CompileUnit}s in {@link CompileUnitHolder}s. 42123441Sbde */ 43123441Sbdeabstract class ReplaceCompileUnits extends NodeVisitor<LexicalContext> { 4428693Scharnier ReplaceCompileUnits() { 451590Srgrimes super(new LexicalContext()); 46123441Sbde } 47123441Sbde 48123441Sbde /** 491590Srgrimes * Override to provide a replacement for an old compile unit. 501590Srgrimes * @param oldUnit the old compile unit to replace 511590Srgrimes * @return the compile unit's replacement. 5212811Sbde */ 531590Srgrimes abstract CompileUnit getReplacement(final CompileUnit oldUnit); 541590Srgrimes 551590Srgrimes CompileUnit getExistingReplacement(final CompileUnitHolder node) { 561590Srgrimes final CompileUnit oldUnit = node.getCompileUnit(); 571590Srgrimes assert oldUnit != null; 58111008Sphk 591590Srgrimes final CompileUnit newUnit = getReplacement(oldUnit); 6012804Speter assert newUnit != null; 6112811Sbde 6212811Sbde return newUnit; 6312811Sbde } 6428693Scharnier 6587690Smarkm @Override 6628693Scharnier public Node leaveFunctionNode(final FunctionNode node) { 6728693Scharnier return node.setCompileUnit(lc, getExistingReplacement(node)).setState(lc, CompilationState.COMPILE_UNITS_REUSED); 6828693Scharnier } 6928693Scharnier 70148413Srwatson @Override 711590Srgrimes public Node leaveLiteralNode(final LiteralNode<?> node) { 7228693Scharnier if (node instanceof ArrayLiteralNode) { 731590Srgrimes final ArrayLiteralNode aln = (ArrayLiteralNode)node; 741590Srgrimes if (aln.getUnits() == null) { 751590Srgrimes return node; 7630180Sdima } 7728693Scharnier final List<ArrayUnit> newArrayUnits = new ArrayList<>(); 7828693Scharnier for (final ArrayUnit au : aln.getUnits()) { 79174573Speter newArrayUnits.add(new ArrayUnit(getExistingReplacement(au), au.getLo(), au.getHi())); 801590Srgrimes } 8187690Smarkm return aln.setUnits(lc, newArrayUnits); 8287690Smarkm } 8387690Smarkm return node; 84181881Sjhb } 851590Srgrimes} 86181881Sjhb