|
| 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.transformers; |
| 20 | + |
| 21 | +import org.codehaus.groovy.ast.ClassNode; |
| 22 | +import org.codehaus.groovy.ast.FieldNode; |
| 23 | +import org.codehaus.groovy.ast.expr.Expression; |
| 24 | +import org.codehaus.groovy.ast.expr.PropertyExpression; |
| 25 | +import org.codehaus.groovy.ast.expr.VariableExpression; |
| 26 | +import org.codehaus.groovy.transform.sc.StaticCompilationMetadataKeys; |
| 27 | +import org.codehaus.groovy.transform.stc.StaticTypesMarker; |
| 28 | + |
| 29 | +import static org.codehaus.groovy.ast.tools.GeneralUtils.*; |
| 30 | + |
| 31 | +/** |
| 32 | + * Transformer for VariableExpression the bytecode backend wouldn't be able to |
| 33 | + * handle otherwise. |
| 34 | + */ |
| 35 | +public class VariableExpressionTransformer { |
| 36 | + |
| 37 | + public Expression transformVariableExpression(VariableExpression expr) { |
| 38 | + Expression trn = tryTransformDelegateToProperty(expr); |
| 39 | + if (trn != null) { |
| 40 | + return trn; |
| 41 | + } |
| 42 | + trn = tryTransformPrivateFieldAccess(expr); |
| 43 | + if (trn != null) { |
| 44 | + return trn; |
| 45 | + } |
| 46 | + return expr; |
| 47 | + } |
| 48 | + |
| 49 | + private static Expression tryTransformDelegateToProperty(VariableExpression expr) { |
| 50 | + // we need to transform variable expressions that go to a delegate |
| 51 | + // to a property expression, as ACG would lose the information in |
| 52 | + // processClassVariable before it reaches any makeCall, that could handle it |
| 53 | + Object val = expr.getNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER); |
| 54 | + if (val == null || val.equals(expr.getName())) return null; |
| 55 | + |
| 56 | + // TODO handle the owner and delegate cases better for nested scenarios and potentially remove the need for the implicit this case |
| 57 | + VariableExpression receiver = new VariableExpression("owner".equals(val) ? (String) val : "delegate".equals(val) ? (String) val : "this"); |
| 58 | + // GROOVY-9136 -- object expression should not overlap source range of property; property stands in for original variable expression |
| 59 | + receiver.setLineNumber(expr.getLineNumber()); |
| 60 | + receiver.setColumnNumber(expr.getColumnNumber()); |
| 61 | + |
| 62 | + PropertyExpression pexp = new PropertyExpression(receiver, expr.getName()); |
| 63 | + pexp.getProperty().setSourcePosition(expr); |
| 64 | + pexp.copyNodeMetaData(expr); |
| 65 | + pexp.setImplicitThis(true); |
| 66 | + |
| 67 | + ClassNode owner = expr.getNodeMetaData(StaticCompilationMetadataKeys.PROPERTY_OWNER); |
| 68 | + if (owner != null) { |
| 69 | + receiver.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, owner); |
| 70 | + receiver.putNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER, val); |
| 71 | + } |
| 72 | + pexp.removeNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER); |
| 73 | + |
| 74 | + return pexp; |
| 75 | + } |
| 76 | + |
| 77 | + private static Expression tryTransformPrivateFieldAccess(VariableExpression expr) { |
| 78 | + FieldNode field = expr.getNodeMetaData(StaticTypesMarker.PV_FIELDS_ACCESS); |
| 79 | + if (field == null) { |
| 80 | + field = expr.getNodeMetaData(StaticTypesMarker.PV_FIELDS_MUTATION); |
| 81 | + } |
| 82 | + if (field != null) { |
| 83 | + // access to a private field from a section of code that normally doesn't have access to it, like a |
| 84 | + // closure or an inner class |
| 85 | + /* GRECLIPSE edit -- GROOVY-6610 |
| 86 | + VariableExpression receiver = new VariableExpression("this"); |
| 87 | + PropertyExpression pexp = new PropertyExpression( |
| 88 | + receiver, |
| 89 | + expr.getName() |
| 90 | + ); |
| 91 | + pexp.setImplicitThis(true); |
| 92 | + pexp.getProperty().setSourcePosition(expr); |
| 93 | + */ |
| 94 | + PropertyExpression pexp = !field.isStatic() ? thisPropX(true, expr.getName()) |
| 95 | + : (PropertyExpression) propX(classX(field.getDeclaringClass()), expr.getName()); |
| 96 | + pexp.getProperty().setSourcePosition(expr); |
| 97 | + // GRECLIPSE end |
| 98 | + // put the receiver inferred type so that the class writer knows that it will have to call a bridge method |
| 99 | + pexp.getObjectExpression().putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, field.getDeclaringClass()); |
| 100 | + // add inferred type information |
| 101 | + pexp.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, field.getOriginType()); |
| 102 | + return pexp; |
| 103 | + } |
| 104 | + return null; |
| 105 | + } |
| 106 | +} |
0 commit comments