Remove ParentCodegenAware

Inline its fields to subclasses, fix parent codegen nullability
This commit is contained in:
Alexander Udalov
2015-01-14 18:59:45 +03:00
parent 2b438a286f
commit c23352d81b
8 changed files with 51 additions and 83 deletions
@@ -40,16 +40,16 @@ public abstract class ClassBodyCodegen extends MemberCodegen<JetClassOrObject> {
protected final ClassDescriptor descriptor;
protected ClassBodyCodegen(
@NotNull JetClassOrObject aClass,
@NotNull JetClassOrObject myClass,
@NotNull ClassContext context,
@NotNull ClassBuilder v,
@NotNull GenerationState state,
@Nullable MemberCodegen<?> parentCodegen
) {
super(state, parentCodegen, context, aClass, v);
myClass = aClass;
kind = context.getContextKind();
descriptor = bindingContext.get(BindingContext.CLASS, aClass);
super(state, parentCodegen, context, myClass, v);
this.myClass = myClass;
this.kind = context.getContextKind();
this.descriptor = bindingContext.get(BindingContext.CLASS, myClass);
}
@Override
@@ -93,7 +93,7 @@ public abstract class ClassBodyCodegen extends MemberCodegen<JetClassOrObject> {
genFunctionOrProperty(declaration);
}
else if (declaration instanceof JetClassOrObject) {
if (declaration instanceof JetEnumEntry && !enumEntryNeedSubclass(state.getBindingContext(), (JetEnumEntry) declaration)) {
if (declaration instanceof JetEnumEntry && !enumEntryNeedSubclass(bindingContext, (JetEnumEntry) declaration)) {
return;
}
@@ -108,7 +108,7 @@ public abstract class ClassBodyCodegen extends MemberCodegen<JetClassOrObject> {
boolean isAnnotation = origin instanceof JetClass && ((JetClass) origin).isAnnotation();
for (JetParameter p : getPrimaryConstructorParameters()) {
if (p.hasValOrVarNode()) {
PropertyDescriptor propertyDescriptor = state.getBindingContext().get(BindingContext.PRIMARY_CONSTRUCTOR_PARAMETER, p);
PropertyDescriptor propertyDescriptor = bindingContext.get(BindingContext.PRIMARY_CONSTRUCTOR_PARAMETER, p);
if (propertyDescriptor != null) {
if (!isAnnotation) {
propertyCodegen.generatePrimaryConstructorProperty(p, propertyDescriptor);
@@ -76,7 +76,7 @@ public class ClosureCodegen extends MemberCodegen<JetElement> {
@NotNull ClassContext context,
@NotNull KotlinSyntheticClass.Kind syntheticClassKind,
@NotNull FunctionGenerationStrategy strategy,
@Nullable MemberCodegen<?> parentCodegen,
@NotNull MemberCodegen<?> parentCodegen,
@NotNull ClassBuilder classBuilder,
@NotNull Type asmType
) {
@@ -79,20 +79,26 @@ import static org.jetbrains.kotlin.resolve.jvm.diagnostics.DiagnosticsPackage.Ot
import static org.jetbrains.kotlin.resolve.jvm.diagnostics.DiagnosticsPackage.Synthetic;
import static org.jetbrains.org.objectweb.asm.Opcodes.*;
public class FunctionCodegen extends ParentCodegenAware {
public class FunctionCodegen {
public final GenerationState state;
private final JetTypeMapper typeMapper;
private final BindingContext bindingContext;
private final CodegenContext owner;
private final ClassBuilder v;
private final MemberCodegen<?> memberCodegen;
public FunctionCodegen(
@NotNull CodegenContext owner,
@NotNull ClassBuilder v,
@NotNull GenerationState state,
MemberCodegen<?> parentCodegen
@NotNull MemberCodegen<?> memberCodegen
) {
super(state, parentCodegen);
this.owner = owner;
this.v = v;
this.state = state;
this.typeMapper = state.getTypeMapper();
this.bindingContext = state.getBindingContext();
this.memberCodegen = memberCodegen;
}
public void gen(@NotNull JetNamedFunction function) {
@@ -166,8 +172,8 @@ public class FunctionCodegen extends ParentCodegenAware {
boolean staticInClassObject = AnnotationsPackage.isPlatformStaticInClassObject(functionDescriptor);
if (staticInClassObject) {
MemberCodegen<?> codegen = getParentCodegen().getParentCodegen();
((ImplementationBodyCodegen) codegen).addAdditionalTask(new PlatformStaticGenerator(functionDescriptor, origin, state));
ImplementationBodyCodegen parentBodyCodegen = (ImplementationBodyCodegen) memberCodegen.getParentCodegen();
parentBodyCodegen.addAdditionalTask(new PlatformStaticGenerator(functionDescriptor, origin, state));
}
if (state.getClassBuilderMode() == ClassBuilderMode.LIGHT_CLASSES || isAbstractMethod(functionDescriptor, methodContextKind)) {
@@ -186,14 +192,14 @@ public class FunctionCodegen extends ParentCodegenAware {
}
if (!isNative) {
generateMethodBody(mv, functionDescriptor, methodContext, jvmSignature, strategy, getParentCodegen());
generateMethodBody(mv, functionDescriptor, methodContext, jvmSignature, strategy, memberCodegen);
}
else if (staticInClassObject) {
// native platformStatic foo() in class object should delegate to the static native function moved to the outer class
mv.visitCode();
FunctionDescriptor staticFunctionDescriptor = PlatformStaticGenerator.createStaticFunctionDescriptor(functionDescriptor);
JvmMethodSignature jvmMethodSignature =
typeMapper.mapSignature(getParentCodegen().getContext().accessibleFunctionDescriptor(staticFunctionDescriptor));
typeMapper.mapSignature(memberCodegen.getContext().accessibleFunctionDescriptor(staticFunctionDescriptor));
Type owningType = typeMapper.mapClass((ClassifierDescriptor) staticFunctionDescriptor.getContainingDeclaration());
generateDelegateToMethodBody(false, mv, jvmMethodSignature.getAsmMethod(), owningType.getInternalName());
}
@@ -642,7 +648,7 @@ public class FunctionCodegen extends ParentCodegenAware {
@Nullable JetNamedFunction function
) {
mv.visitCode();
generateDefaultImplBody(methodContext, signature, functionDescriptor, isStatic, mv, loadStrategy, function, getParentCodegen(), state);
generateDefaultImplBody(methodContext, signature, functionDescriptor, isStatic, mv, loadStrategy, function, memberCodegen, state);
endVisit(mv, "default method", callableDescriptorToDeclaration(functionDescriptor));
}
@@ -190,7 +190,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
if (isEnum) {
for (JetDeclaration declaration : myClass.getDeclarations()) {
if (declaration instanceof JetEnumEntry) {
if (enumEntryNeedSubclass(state.getBindingContext(), (JetEnumEntry) declaration)) {
if (enumEntryNeedSubclass(bindingContext, (JetEnumEntry) declaration)) {
access &= ~ACC_FINAL;
}
}
@@ -1166,7 +1166,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
}
if (isClassObjectWithBackingFieldsInOuter(descriptor)) {
final ImplementationBodyCodegen parentCodegen = getParentBodyCodegen(this);
final ImplementationBodyCodegen parentCodegen = (ImplementationBodyCodegen) getParentCodegen();
//generate OBJECT$
parentCodegen.generateClassObjectInitializer(descriptor);
generateInitializers(new Function0<ExpressionCodegen>() {
@@ -1248,7 +1248,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
result.addField((JetDelegatorByExpressionSpecifier) specifier, propertyDescriptor);
}
else {
JetType expressionType = state.getBindingContext().get(BindingContext.EXPRESSION_TYPE, expression);
JetType expressionType = bindingContext.get(BindingContext.EXPRESSION_TYPE, expression);
Type asmType =
expressionType != null ? typeMapper.mapType(expressionType) : typeMapper.mapType(getSuperClass(specifier));
result.addField((JetDelegatorByExpressionSpecifier) specifier, asmType, "$delegate_" + n);
@@ -1599,7 +1599,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
iv.aconst(enumConstant.getName());
iv.iconst(ordinal);
if (delegationSpecifiers.size() == 1 && !enumEntryNeedSubclass(state.getBindingContext(), enumConstant)) {
if (delegationSpecifiers.size() == 1 && !enumEntryNeedSubclass(bindingContext, enumConstant)) {
JetDelegationSpecifier specifier = delegationSpecifiers.get(0);
if (!(specifier instanceof JetDelegatorToSuperCall)) {
throw new UnsupportedOperationException("unsupported type of enum constant initializer: " + specifier);
@@ -1626,7 +1626,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
DelegationFieldsInfo.Field field = delegationFieldsInfo.getInfo((JetDelegatorByExpressionSpecifier) specifier);
generateDelegateField(field);
JetExpression delegateExpression = ((JetDelegatorByExpressionSpecifier) specifier).getDelegateExpression();
JetType delegateExpressionType = state.getBindingContext().get(BindingContext.EXPRESSION_TYPE, delegateExpression);
JetType delegateExpressionType = bindingContext.get(BindingContext.EXPRESSION_TYPE, delegateExpression);
generateDelegates(getSuperClass(specifier), delegateExpressionType, field);
}
}
@@ -194,14 +194,6 @@ public class JvmCodegenUtil {
return file != null && CodeFragmentUtilPackage.getSuppressDiagnosticsInDebugMode(file);
}
@NotNull
public static ImplementationBodyCodegen getParentBodyCodegen(@Nullable MemberCodegen<?> classBodyCodegen) {
assert classBodyCodegen != null && classBodyCodegen.getParentCodegen() instanceof ImplementationBodyCodegen
: "Class object should have appropriate parent BodyCodegen";
return (ImplementationBodyCodegen) classBodyCodegen.getParentCodegen();
}
@Nullable
public static ClassDescriptor getDispatchReceiverParameterForConstructorCall(
@NotNull ConstructorDescriptor descriptor,
@@ -27,6 +27,7 @@ import org.jetbrains.kotlin.codegen.inline.InlineCodegenUtil;
import org.jetbrains.kotlin.codegen.inline.NameGenerator;
import org.jetbrains.kotlin.codegen.inline.ReifiedTypeParametersUsages;
import org.jetbrains.kotlin.codegen.state.GenerationState;
import org.jetbrains.kotlin.codegen.state.JetTypeMapper;
import org.jetbrains.kotlin.descriptors.*;
import org.jetbrains.kotlin.descriptors.annotations.Annotations;
import org.jetbrains.kotlin.descriptors.impl.SimpleFunctionDescriptorImpl;
@@ -61,16 +62,20 @@ import static org.jetbrains.kotlin.resolve.jvm.diagnostics.DiagnosticsPackage.Tr
import static org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmDeclarationOrigin.NO_ORIGIN;
import static org.jetbrains.org.objectweb.asm.Opcodes.*;
public abstract class MemberCodegen<T extends JetElement/* TODO: & JetDeclarationContainer*/> extends ParentCodegenAware {
public abstract class MemberCodegen<T extends JetElement/* TODO: & JetDeclarationContainer*/> {
protected final GenerationState state;
protected final T element;
protected final FieldOwnerContext context;
protected final ClassBuilder v;
protected final FunctionCodegen functionCodegen;
protected final PropertyCodegen propertyCodegen;
protected final JetTypeMapper typeMapper;
protected final BindingContext bindingContext;
private final MemberCodegen<?> parentCodegen;
private final ReifiedTypeParametersUsages reifiedTypeParametersUsages = new ReifiedTypeParametersUsages();
protected ExpressionCodegen clInit;
private NameGenerator inlineNameGenerator;
private final ReifiedTypeParametersUsages reifiedTypeParametersUsages = new ReifiedTypeParametersUsages();
public MemberCodegen(
@NotNull GenerationState state,
@@ -79,12 +84,15 @@ public abstract class MemberCodegen<T extends JetElement/* TODO: & JetDeclaratio
T element,
@NotNull ClassBuilder builder
) {
super(state, parentCodegen);
this.state = state;
this.typeMapper = state.getTypeMapper();
this.bindingContext = state.getBindingContext();
this.element = element;
this.context = context;
this.v = builder;
this.functionCodegen = new FunctionCodegen(context, v, state, this);
this.propertyCodegen = new PropertyCodegen(context, v, functionCodegen, this);
this.parentCodegen = parentCodegen;
}
protected MemberCodegen(@NotNull MemberCodegen<T> wrapped) {
@@ -249,8 +257,9 @@ public abstract class MemberCodegen<T extends JetElement/* TODO: & JetDeclaratio
bindingContext.get(BindingContext.DELEGATED_PROPERTY_PD_RESOLVED_CALL, propertyDescriptor);
if (pdResolvedCall != null) {
int index = PropertyCodegen.indexOfDelegatedProperty(property);
StackValue lastValue = PropertyCodegen.invokeDelegatedPropertyConventionMethod(propertyDescriptor, codegen,
state.getTypeMapper(), pdResolvedCall, index, 0);
StackValue lastValue = PropertyCodegen.invokeDelegatedPropertyConventionMethod(
propertyDescriptor, codegen, typeMapper, pdResolvedCall, index, 0
);
lastValue.put(Type.VOID_TYPE, codegen.v);
}
}
@@ -390,4 +399,8 @@ public abstract class MemberCodegen<T extends JetElement/* TODO: & JetDeclaratio
public ReifiedTypeParametersUsages getReifiedTypeParametersUsages() {
return reifiedTypeParametersUsages;
}
public MemberCodegen<?> getParentCodegen() {
return parentCodegen;
}
}
@@ -1,42 +0,0 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.codegen;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.codegen.state.GenerationState;
import org.jetbrains.kotlin.codegen.state.JetTypeMapper;
import org.jetbrains.kotlin.resolve.BindingContext;
public class ParentCodegenAware {
protected final GenerationState state;
protected final JetTypeMapper typeMapper;
protected final BindingContext bindingContext;
private final MemberCodegen<?> parentCodegen;
public ParentCodegenAware(@NotNull GenerationState state, @Nullable MemberCodegen<?> parentCodegen) {
this.state = state;
this.typeMapper = state.getTypeMapper();
this.bindingContext = state.getBindingContext();
this.parentCodegen = parentCodegen;
}
@Nullable
public MemberCodegen<?> getParentCodegen() {
return parentCodegen;
}
}
@@ -46,7 +46,6 @@ import org.jetbrains.org.objectweb.asm.commons.Method;
import java.util.List;
import static org.jetbrains.kotlin.codegen.AsmUtil.*;
import static org.jetbrains.kotlin.codegen.JvmCodegenUtil.getParentBodyCodegen;
import static org.jetbrains.kotlin.codegen.JvmCodegenUtil.isInterface;
import static org.jetbrains.kotlin.codegen.JvmSerializationBindings.*;
import static org.jetbrains.kotlin.resolve.DescriptorUtils.isClassObject;
@@ -62,14 +61,14 @@ public class PropertyCodegen {
private final JetTypeMapper typeMapper;
private final BindingContext bindingContext;
private final FieldOwnerContext context;
private final MemberCodegen<?> classBodyCodegen;
private final MemberCodegen<?> memberCodegen;
private final OwnerKind kind;
public PropertyCodegen(
@NotNull FieldOwnerContext context,
@NotNull ClassBuilder v,
@NotNull FunctionCodegen functionCodegen,
@Nullable MemberCodegen<?> classBodyCodegen
@NotNull MemberCodegen<?> memberCodegen
) {
this.state = functionCodegen.state;
this.v = v;
@@ -77,7 +76,7 @@ public class PropertyCodegen {
this.typeMapper = state.getTypeMapper();
this.bindingContext = state.getBindingContext();
this.context = context;
this.classBodyCodegen = classBodyCodegen;
this.memberCodegen = memberCodegen;
this.kind = context.getContextKind();
}
@@ -254,7 +253,7 @@ public class PropertyCodegen {
if (AsmUtil.isInstancePropertyWithStaticBackingField(propertyDescriptor) ) {
modifiers |= ACC_STATIC | getVisibilityForSpecialPropertyBackingField(propertyDescriptor, isDelegate);
if (AsmUtil.isPropertyWithBackingFieldInOuterClass(propertyDescriptor)) {
ImplementationBodyCodegen codegen = getParentBodyCodegen(classBodyCodegen);
ImplementationBodyCodegen codegen = (ImplementationBodyCodegen) memberCodegen.getParentCodegen();
builder = codegen.v;
backingFieldContext = codegen.context;
v.getSerializationBindings().put(STATIC_FIELD_IN_OUTER_CLASS, propertyDescriptor);
@@ -265,7 +264,7 @@ public class PropertyCodegen {
}
if (AsmUtil.isPropertyWithBackingFieldCopyInOuterClass(propertyDescriptor)) {
ImplementationBodyCodegen parentBodyCodegen = getParentBodyCodegen(classBodyCodegen);
ImplementationBodyCodegen parentBodyCodegen = (ImplementationBodyCodegen) memberCodegen.getParentCodegen();
parentBodyCodegen.addClassObjectPropertyToCopy(propertyDescriptor, defaultValue);
}