|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.codehaus.groovy.transform.sc; |
| 20 | + |
| 21 | +import org.codehaus.groovy.ast.ClassNode; |
| 22 | +import org.codehaus.groovy.ast.GroovyCodeVisitor; |
| 23 | +import org.codehaus.groovy.ast.expr.Expression; |
| 24 | +import org.codehaus.groovy.ast.expr.ExpressionTransformer; |
| 25 | +import org.codehaus.groovy.classgen.AsmClassGenerator; |
| 26 | +import org.codehaus.groovy.classgen.asm.ExpressionAsVariableSlot; |
| 27 | +import org.codehaus.groovy.classgen.asm.WriterController; |
| 28 | + |
| 29 | +import static org.codehaus.groovy.transform.stc.StaticTypesMarker.INFERRED_TYPE; |
| 30 | + |
| 31 | +/** |
| 32 | + * A front-end class for {@link org.codehaus.groovy.classgen.asm.ExpressionAsVariableSlot} which |
| 33 | + * allows defining temporary variables loaded from variable slots directly at the AST level, |
| 34 | + * without any knowledge of {@link org.codehaus.groovy.classgen.AsmClassGenerator}. |
| 35 | + * |
| 36 | + * @since 2.4.0 |
| 37 | + */ |
| 38 | +public class TemporaryVariableExpression extends Expression { |
| 39 | + |
| 40 | + private final Expression expression; |
| 41 | + |
| 42 | + private ExpressionAsVariableSlot[] variable = {null}; |
| 43 | + |
| 44 | + public TemporaryVariableExpression(final Expression expression) { |
| 45 | + this.expression = expression; |
| 46 | + putNodeMetaData(INFERRED_TYPE, expression.getNodeMetaData(INFERRED_TYPE)); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public Expression transformExpression(final ExpressionTransformer transformer) { |
| 51 | + TemporaryVariableExpression result = new TemporaryVariableExpression(transformer.transform(expression)); |
| 52 | + result.copyNodeMetaData(this); |
| 53 | + result.variable = variable; |
| 54 | + return result; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void visit(final GroovyCodeVisitor visitor) { |
| 59 | + if (visitor instanceof AsmClassGenerator) { |
| 60 | + if (variable[0] == null) { |
| 61 | + WriterController controller = ((AsmClassGenerator) visitor).getController(); |
| 62 | + variable[0] = new ExpressionAsVariableSlot(controller, expression); |
| 63 | + } |
| 64 | + variable[0].visit(visitor); |
| 65 | + } else { |
| 66 | + expression.visit(visitor); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public void remove(final WriterController controller) { |
| 71 | + controller.getCompileStack().removeVar(variable[0].getIndex()); |
| 72 | + variable[0] = null; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public ClassNode getType() { |
| 77 | + return expression.getType(); |
| 78 | + } |
| 79 | +} |
0 commit comments