Top level functions inside module should be invoked using package$src class

This commit is contained in:
Natalia.Ukhorskaya
2013-03-04 18:59:27 +04:00
parent 4d51572482
commit 02c8989b2f
23 changed files with 273 additions and 32 deletions
@@ -270,6 +270,14 @@ public class CodegenUtil {
&& context.getContextKind() != OwnerKind.TRAIT_IMPL);
}
public static boolean isCallInsideSameModuleAsDeclared(CallableMemberDescriptor declarationDescriptor, CodegenContext context) {
if (context == CodegenContext.STATIC) {
return true;
}
DeclarationDescriptor contextDescriptor = context.getContextDescriptor();
return DescriptorUtils.isInSameModule(declarationDescriptor, contextDescriptor);
}
public static boolean hasAbstractMembers(@NotNull ClassDescriptor classDescriptor) {
return ContainerUtil.exists(classDescriptor.getDefaultType().getMemberScope().getAllDescriptors(),
new Condition<DeclarationDescriptor>() {
@@ -1721,6 +1721,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
PropertyDescriptor initialDescriptor = propertyDescriptor;
propertyDescriptor = initialDescriptor.getOriginal();
boolean isInsideClass = isCallInsideSameClassAsDeclared(propertyDescriptor, context);
boolean isInsideModule = isCallInsideSameModuleAsDeclared(propertyDescriptor, context);
boolean isExtensionProperty = propertyDescriptor.getReceiverParameter() != null;
Method getter = null;
Method setter = null;
@@ -1794,7 +1795,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
boolean isInterface;
if (isStatic) {
isInterface = overridesTrait;
owner = ownerParam = typeMapper.getOwner(propertyDescriptor, contextKind());
owner = ownerParam = typeMapper.getOwner(propertyDescriptor, contextKind(), isInsideModule);
getterInvokeOpcode = INVOKESTATIC;
setterInvokeOpcode = INVOKESTATIC;
@@ -1807,7 +1808,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
getterInvokeOpcode = getOpcodeForPropertyDescriptorWithoutAccessor(propertyDescriptor);
}
else {
callableGetter = typeMapper.mapToCallableMethod(propertyDescriptor.getGetter(), isSuper, isInsideClass, contextKind());
callableGetter = typeMapper.mapToCallableMethod(propertyDescriptor.getGetter(), isSuper, isInsideClass, isInsideModule, contextKind());
getterInvokeOpcode = callableGetter.getInvokeOpcode();
}
@@ -1815,13 +1816,13 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
setterInvokeOpcode = getOpcodeForPropertyDescriptorWithoutAccessor(propertyDescriptor);
}
else {
callableSetter = typeMapper.mapToCallableMethod(propertyDescriptor.getSetter(), isSuper, isInsideClass, contextKind());
callableSetter = typeMapper.mapToCallableMethod(propertyDescriptor.getSetter(), isSuper, isInsideClass, isInsideModule, contextKind());
setterInvokeOpcode = callableSetter.getInvokeOpcode();
}
CallableMethod callableMethod = callableGetter != null ? callableGetter : callableSetter;
if (callableMethod == null) {
owner = ownerParam = typeMapper.getOwner(propertyDescriptor, contextKind());
owner = ownerParam = typeMapper.getOwner(propertyDescriptor, contextKind(), isInsideModule);
}
else {
owner = isFakeOverride && !overridesTrait && !isInterface(initialDescriptor.getContainingDeclaration())
@@ -2034,7 +2035,10 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
callableMethod = typeMapper.asCallableMethod(invoke);
}
else {
callableMethod = typeMapper.mapToCallableMethod(fd, superCall, isCallInsideSameClassAsDeclared(fd, context), OwnerKind.IMPLEMENTATION);
callableMethod = typeMapper.mapToCallableMethod(fd, superCall,
isCallInsideSameClassAsDeclared(fd, context),
isCallInsideSameModuleAsDeclared(fd, context),
OwnerKind.IMPLEMENTATION);
}
return callableMethod;
}
@@ -3105,6 +3109,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
descriptor,
false,
isCallInsideSameClassAsDeclared(descriptor, context),
isCallInsideSameModuleAsDeclared(descriptor, context),
OwnerKind.IMPLEMENTATION);
invokeMethodWithArguments(callableMethod, expression, StackValue.none());
return type;
@@ -3207,6 +3212,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
operationDescriptor,
false,
isCallInsideSameClassAsDeclared(operationDescriptor, context),
isCallInsideSameModuleAsDeclared(operationDescriptor, context),
OwnerKind.IMPLEMENTATION);
boolean isGetter = accessor.getSignature().getAsmMethod().getName().equals("get");
@@ -76,6 +76,7 @@ public class FunctionCodegen extends GenerationStateAware {
functionDescriptor,
false,
isCallInsideSameClassAsDeclared(functionDescriptor, owner),
isCallInsideSameModuleAsDeclared(functionDescriptor, owner),
owner.getContextKind()).getSignature();
generateMethod(f, method, true, null, functionDescriptor);
}
@@ -345,7 +346,7 @@ public class FunctionCodegen extends GenerationStateAware {
MethodVisitor mv
) {
if (jvmSignature == null) {
jvmSignature = state.getTypeMapper().mapToCallableMethod(functionDescriptor, false, false, OwnerKind.IMPLEMENTATION).getSignature();
jvmSignature = state.getTypeMapper().mapToCallableMethod(functionDescriptor, false, false, false, OwnerKind.IMPLEMENTATION).getSignature();
}
List<ValueParameterDescriptor> paramDescrs = functionDescriptor.getValueParameters();
@@ -791,7 +791,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
typeMapper.mapSignature(original.getName(), original).getAsmMethod();
Type[] argTypes = method.getArgumentTypes();
String owner = typeMapper.getOwner(original, OwnerKind.IMPLEMENTATION).getInternalName();
String owner = typeMapper.getOwner(original, OwnerKind.IMPLEMENTATION, isCallInsideSameModuleAsDeclared(original, context)).getInternalName();
MethodVisitor mv = v.newMethod(null, ACC_BRIDGE | ACC_SYNTHETIC | ACC_STATIC, bridge.getName().getName(),
method.getDescriptor(), null, null);
if (state.getClassBuilderMode() == ClassBuilderMode.STUBS) {
@@ -849,12 +849,13 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
iv.load(0, OBJECT_TYPE);
boolean hasBackingField = Boolean.TRUE.equals(bindingContext.get(BindingContext.BACKING_FIELD_REQUIRED, original));
boolean isInsideModule = isCallInsideSameModuleAsDeclared(original, context);
if (original.getVisibility() == Visibilities.PRIVATE && hasBackingField) {
iv.getfield(typeMapper.getOwner(original, OwnerKind.IMPLEMENTATION).getInternalName(), original.getName().getName(),
iv.getfield(typeMapper.getOwner(original, OwnerKind.IMPLEMENTATION, isInsideModule).getInternalName(), original.getName().getName(),
originalMethod.getReturnType().getDescriptor());
}
else {
iv.invokespecial(typeMapper.getOwner(original, OwnerKind.IMPLEMENTATION).getInternalName(),
iv.invokespecial(typeMapper.getOwner(original, OwnerKind.IMPLEMENTATION, isInsideModule).getInternalName(),
originalMethod.getName(), originalMethod.getDescriptor());
}
@@ -891,12 +892,13 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
//noinspection AssignmentToForLoopParameter
reg += argType.getSize();
}
boolean isInsideModule = isCallInsideSameModuleAsDeclared(original, context);
if (original.getVisibility() == Visibilities.PRIVATE && original.getModality() == Modality.FINAL) {
iv.putfield(typeMapper.getOwner(original, OwnerKind.IMPLEMENTATION).getInternalName(), original.getName().getName(),
iv.putfield(typeMapper.getOwner(original, OwnerKind.IMPLEMENTATION, isInsideModule).getInternalName(), original.getName().getName(),
originalMethod.getArgumentTypes()[0].getDescriptor());
}
else {
iv.invokespecial(typeMapper.getOwner(original, OwnerKind.IMPLEMENTATION).getInternalName(),
iv.invokespecial(typeMapper.getOwner(original, OwnerKind.IMPLEMENTATION, isInsideModule).getInternalName(),
originalMethod.getName(), originalMethod.getDescriptor());
}
@@ -1354,6 +1356,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
inheritedFun,
false,
isCallInsideSameClassAsDeclared(inheritedFun, context),
isCallInsideSameModuleAsDeclared(inheritedFun, context),
OwnerKind.IMPLEMENTATION).getSignature();
JetMethodAnnotationWriter aw = JetMethodAnnotationWriter.visitAnnotation(mv);
int kotlinFlags = getFlagsForVisibility(fun.getVisibility());
@@ -1588,7 +1591,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
}
codegen.gen(initializer, type);
// @todo write directly to the field. Fix test excloset.jet::test6
JvmClassName owner = typeMapper.getOwner(propertyDescriptor, OwnerKind.IMPLEMENTATION);
JvmClassName owner = typeMapper.getOwner(propertyDescriptor, OwnerKind.IMPLEMENTATION, isCallInsideSameModuleAsDeclared(propertyDescriptor, codegen.context));
Type propType = typeMapper.mapType(jetType);
StackValue.property(propertyDescriptor, owner, owner,
propType, false, false, false, null, null, 0, 0, state).store(propType, iv);
@@ -212,7 +212,7 @@ public class PropertyCodegen extends GenerationStateAware {
iv.visitFieldInsn(
kind == OwnerKind.NAMESPACE ? GETSTATIC : GETFIELD,
typeMapper.getOwner(propertyDescriptor, kind).getInternalName(),
typeMapper.getOwner(propertyDescriptor, kind, isCallInsideSameModuleAsDeclared(propertyDescriptor, context)).getInternalName(),
propertyDescriptor.getName().getName(),
type.getDescriptor());
iv.areturn(type);
@@ -296,7 +296,7 @@ public class PropertyCodegen extends GenerationStateAware {
}
iv.load(paramCode, type);
iv.visitFieldInsn(kind == OwnerKind.NAMESPACE ? PUTSTATIC : PUTFIELD,
typeMapper.getOwner(propertyDescriptor, kind).getInternalName(),
typeMapper.getOwner(propertyDescriptor, kind, isCallInsideSameModuleAsDeclared(propertyDescriptor, context)).getInternalName(),
propertyDescriptor.getName().getName(),
type.getDescriptor());
@@ -62,12 +62,12 @@ public class JetTypeMapper extends BindingTraceAware {
}
@NotNull
public JvmClassName getOwner(DeclarationDescriptor descriptor, OwnerKind kind) {
public JvmClassName getOwner(DeclarationDescriptor descriptor, OwnerKind kind, boolean isInsideModule) {
JetTypeMapperMode mapTypeMode = ownerKindToMapTypeMode(kind);
DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration();
if (containingDeclaration instanceof NamespaceDescriptor) {
return jvmClassNameForNamespace((NamespaceDescriptor) containingDeclaration, descriptor);
return jvmClassNameForNamespace((NamespaceDescriptor) containingDeclaration, descriptor, isInsideModule);
}
else if (containingDeclaration instanceof ClassDescriptor) {
ClassDescriptor classDescriptor = (ClassDescriptor) containingDeclaration;
@@ -122,7 +122,11 @@ public class JetTypeMapper extends BindingTraceAware {
}
@NotNull
private JvmClassName jvmClassNameForNamespace(@NotNull NamespaceDescriptor namespace, @NotNull DeclarationDescriptor descriptor) {
private JvmClassName jvmClassNameForNamespace(
@NotNull NamespaceDescriptor namespace,
@NotNull DeclarationDescriptor descriptor,
boolean insideModule
) {
StringBuilder r = new StringBuilder();
@@ -147,15 +151,10 @@ public class JetTypeMapper extends BindingTraceAware {
r.append("/");
}
if (descriptor instanceof PropertyDescriptor) {
JetFile file = BindingContextUtils.getContainingFile(bindingContext, descriptor);
if (file != null) {
String internalName = NamespaceCodegen.getNamespacePartInternalName(file);
r.append(internalName.substring(r.length()));
}
else {
r.append(PackageClassUtils.getPackageClassName(namespace.getFqName()));
}
JetFile file = BindingContextUtils.getContainingFile(bindingContext, descriptor);
if (insideModule && file != null) {
String internalName = NamespaceCodegen.getNamespacePartInternalName(file);
r.append(internalName.substring(r.length()));
}
else {
r.append(PackageClassUtils.getPackageClassName(namespace.getFqName()));
@@ -450,6 +449,7 @@ public class JetTypeMapper extends BindingTraceAware {
@NotNull FunctionDescriptor functionDescriptor,
boolean superCall,
boolean isInsideClass,
boolean isInsideModule,
OwnerKind kind
) {
final DeclarationDescriptor functionParent = functionDescriptor.getOriginal().getContainingDeclaration();
@@ -464,7 +464,7 @@ public class JetTypeMapper extends BindingTraceAware {
JvmClassName thisClass;
if (functionParent instanceof NamespaceDescriptor) {
assert !superCall;
owner = jvmClassNameForNamespace((NamespaceDescriptor) functionParent, functionDescriptor);
owner = jvmClassNameForNamespace((NamespaceDescriptor) functionParent, functionDescriptor, isInsideModule);
ownerForDefaultImpl = ownerForDefaultParam = owner;
invokeOpcode = INVOKESTATIC;
thisClass = null;