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 |
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()]; |