src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/util/MethodGenerator.java
changeset 55658 a35a210e6c0f
parent 48409 5ab69533994b
equal deleted inserted replaced
55657:107ebf94ddcc 55658:a35a210e6c0f
     1 /*
     1 /*
     2  * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
     3  */
     3  */
     4 /*
     4 /*
     5  * Licensed to the Apache Software Foundation (ASF) under one or more
     5  * Licensed to the Apache Software Foundation (ASF) under one or more
     6  * contributor license agreements.  See the NOTICE file distributed with
     6  * contributor license agreements.  See the NOTICE file distributed with
     7  * this work for additional information regarding copyright ownership.
     7  * this work for additional information regarding copyright ownership.
    73 import java.util.Stack;
    73 import java.util.Stack;
    74 
    74 
    75 /**
    75 /**
    76  * @author Jacek Ambroziak
    76  * @author Jacek Ambroziak
    77  * @author Santiago Pericas-Geertsen
    77  * @author Santiago Pericas-Geertsen
    78  * @LastModified: Nov 2017
    78  * @LastModified: July 2019
    79  */
    79  */
    80 public class MethodGenerator extends MethodGen
    80 public class MethodGenerator extends MethodGen
    81     implements com.sun.org.apache.xalan.internal.xsltc.compiler.Constants {
    81     implements com.sun.org.apache.xalan.internal.xsltc.compiler.Constants {
    82     protected static final int INVALID_INDEX   = -1;
    82     protected static final int INVALID_INDEX   = -1;
    83 
    83 
   205 
   205 
   206         index = cpg.addInterfaceMethodref(NODE_ITERATOR, NEXT, NEXT_SIG);
   206         index = cpg.addInterfaceMethodref(NODE_ITERATOR, NEXT, NEXT_SIG);
   207         _nextNode = new INVOKEINTERFACE(index, 1);
   207         _nextNode = new INVOKEINTERFACE(index, 1);
   208 
   208 
   209         _slotAllocator = new SlotAllocator();
   209         _slotAllocator = new SlotAllocator();
   210         _slotAllocator.initialize(getLocalVariableRegistry().getLocals(false));
   210         _slotAllocator.initialize(getLocalVariableRegistry().getLocals());
   211         _allocatorInit = true;
   211         _allocatorInit = true;
   212     }
   212     }
   213 
   213 
   214     /**
   214     /**
   215      * Allocates a local variable. If the slot allocator has already been
   215      * Allocates a local variable. If the slot allocator has already been
   443                         sameNameList.remove(i);
   443                         sameNameList.remove(i);
   444                         break;
   444                         break;
   445                     }
   445                     }
   446                 }
   446                 }
   447             } else {
   447             } else {
   448                 _nameToLVGMap.remove(lvg);
   448                 _nameToLVGMap.remove(lvg.getName());
   449             }
   449             }
   450         }
   450         }
   451 
   451 
   452         /**
   452         /**
   453          * <p>Given the name of a variable, finds a {@link LocalVariableGen}
   453          * <p>Given the name of a variable, finds a {@link LocalVariableGen}
   478 
   478 
   479             return lvg;
   479             return lvg;
   480         }
   480         }
   481 
   481 
   482         /**
   482         /**
   483          * <p>Gets all {@link LocalVariableGen} objects for this method.</p>
   483          * Gets all {@link LocalVariableGen} objects.
   484          * <p>When the <code>includeRemoved</code> argument has the value
   484          * This method replaces {@link MethodGen#getLocalVariables()} which has
   485          * <code>false</code>, this method replaces uses of
       
   486          * {@link MethodGen#getLocalVariables()} which has
       
   487          * a side-effect of setting the start and end range for any
   485          * a side-effect of setting the start and end range for any
   488          * <code>LocalVariableGen</code> if either was <code>null</code>.  That
   486          * {@code LocalVariableGen} if either was {@code null}.  That
   489          * side-effect causes problems for outlining of code in XSLTC.
   487          * side-effect causes problems for outlining of code in XSLTC.
   490          * @param includeRemoved Specifies whether all local variables ever
   488          *
   491          * declared should be returned (<code>true</code>) or only those not
   489          * @return an array of {@code LocalVariableGen} containing all the
   492          * removed (<code>false</code>)
       
   493          * @return an array of <code>LocalVariableGen</code> containing all the
       
   494          * local variables
   490          * local variables
   495          */
   491          */
   496         @SuppressWarnings("unchecked")
   492         @SuppressWarnings("unchecked")
   497         protected LocalVariableGen[] getLocals(boolean includeRemoved) {
   493         private LocalVariableGen[] getLocals() {
   498             LocalVariableGen[] locals = null;
   494             LocalVariableGen[] locals = null;
   499             List<LocalVariableGen> allVarsEverDeclared = new ArrayList<>();
   495             List<LocalVariableGen> allVarsEverDeclared = new ArrayList<>();
   500 
   496 
   501             if (includeRemoved) {
   497             for (Map.Entry<String, Object> nameVarsPair : _nameToLVGMap.entrySet()) {
   502                 int slotCount = allVarsEverDeclared.size();
   498                 Object vars = nameVarsPair.getValue();
   503 
   499                 if (vars != null) {
   504                 for (int i = 0; i < slotCount; i++) {
   500                     if (vars instanceof ArrayList) {
   505                     Object slotEntries = _variables.get(i);
   501                         List<LocalVariableGen> varsList =
   506                     if (slotEntries != null) {
   502                                 (List<LocalVariableGen>) vars;
   507                         if (slotEntries instanceof ArrayList) {
   503                         for (int i = 0; i < varsList.size(); i++) {
   508                             List<LocalVariableGen> slotList =
   504                             allVarsEverDeclared.add(varsList.get(i));
   509                                     (List<LocalVariableGen>)slotEntries;
       
   510 
       
   511                             for (int j = 0; j < slotList.size(); j++) {
       
   512                                 allVarsEverDeclared.add(slotList.get(i));
       
   513                             }
       
   514                         } else {
       
   515                             allVarsEverDeclared.add((LocalVariableGen)slotEntries);
       
   516                         }
   505                         }
   517                     }
   506                     } else {
   518                 }
   507                         allVarsEverDeclared.add((LocalVariableGen)vars);
   519             } else {
       
   520                 for (Map.Entry<String, Object> nameVarsPair : _nameToLVGMap.entrySet()) {
       
   521                     Object vars = nameVarsPair.getValue();
       
   522                     if (vars != null) {
       
   523                         if (vars instanceof ArrayList) {
       
   524                             List<LocalVariableGen> varsList =
       
   525                                     (List<LocalVariableGen>) vars;
       
   526                             for (int i = 0; i < varsList.size(); i++) {
       
   527                                 allVarsEverDeclared.add(varsList.get(i));
       
   528                             }
       
   529                         } else {
       
   530                             allVarsEverDeclared.add((LocalVariableGen)vars);
       
   531                         }
       
   532                     }
   508                     }
   533                 }
   509                 }
   534             }
   510             }
   535 
   511 
   536             locals = new LocalVariableGen[allVarsEverDeclared.size()];
   512             locals = new LocalVariableGen[allVarsEverDeclared.size()];