Move top level properties from package class to package$src

This commit is contained in:
Natalia.Ukhorskaya
2013-02-28 18:25:46 +04:00
parent 4bfdeffd0b
commit d501e8821c
7 changed files with 80 additions and 65 deletions
@@ -314,7 +314,7 @@ public class FunctionCodegen extends GenerationStateAware {
}
}
private void generateStaticDelegateMethodBody(
public static void generateStaticDelegateMethodBody(
@NotNull MethodVisitor mv,
@NotNull Method asmMethod,
@NotNull OwnerKind.StaticDelegateKind dk
@@ -109,10 +109,6 @@ public class NamespaceCodegen extends MemberCodegen {
}
assert v.isActivated() == shouldGenerateNSClass(files) : "Different algorithms for generating namespace class and for heuristics";
if (hasNonConstantPropertyInitializers()) {
generateStaticInitializers();
}
}
private void generate(JetFile file) {
@@ -120,11 +116,7 @@ public class NamespaceCodegen extends MemberCodegen {
assert descriptor != null : "No namespace found for file " + file + " declared package: " + file.getPackageName();
int countOfDeclarationsInSrcClass = 0;
for (JetDeclaration declaration : file.getDeclarations()) {
if (declaration instanceof JetProperty) {
final CodegenContext context = CodegenContext.STATIC.intoNamespace(descriptor);
genFunctionOrProperty(context, (JetTypeParameterListOwner) declaration, v.getClassBuilder());
}
else if (declaration instanceof JetNamedFunction) {
if (declaration instanceof JetProperty || declaration instanceof JetNamedFunction) {
countOfDeclarationsInSrcClass++;
}
else if (declaration instanceof JetClassOrObject) {
@@ -154,7 +146,7 @@ public class NamespaceCodegen extends MemberCodegen {
builder.visitSource(file.getName(), null);
for (JetDeclaration declaration : file.getDeclarations()) {
if (declaration instanceof JetNamedFunction) {
if (declaration instanceof JetNamedFunction || declaration instanceof JetProperty) {
{
final CodegenContext context =
CodegenContext.STATIC.intoNamespace(descriptor);
@@ -168,6 +160,10 @@ public class NamespaceCodegen extends MemberCodegen {
}
}
if (hasNonConstantPropertyInitializers()) {
generateStaticInitializers(builder, file);
}
builder.done();
}
}
@@ -210,41 +206,32 @@ public class NamespaceCodegen extends MemberCodegen {
}
}
private void generateStaticInitializers() {
final JetFile namespace = files.iterator().next(); // @todo: hack
private void generateStaticInitializers(@NotNull ClassBuilder builder, @NotNull JetFile file) {
MethodVisitor mv = builder.newMethod(file, ACC_PUBLIC | ACC_STATIC, "<clinit>", "()V", null, null);
if (state.getClassBuilderMode() == ClassBuilderMode.FULL) {
mv.visitCode();
v.addOptionalDeclaration(new ClassBuilderOnDemand.ClassBuilderCallback() {
@Override
public void doSomething(@NotNull ClassBuilder v) {
MethodVisitor mv = v.newMethod(namespace, ACC_PUBLIC | ACC_STATIC, "<clinit>", "()V", null, null);
if (state.getClassBuilderMode() == ClassBuilderMode.FULL) {
mv.visitCode();
FrameMap frameMap = new FrameMap();
ExpressionCodegen codegen = new ExpressionCodegen(mv, frameMap, Type.VOID_TYPE, CodegenContext.STATIC, state);
FrameMap frameMap = new FrameMap();
ExpressionCodegen codegen = new ExpressionCodegen(mv, frameMap, Type.VOID_TYPE, CodegenContext.STATIC, state);
for (JetFile file : files) {
for (JetDeclaration declaration : file.getDeclarations()) {
if (declaration instanceof JetProperty) {
final JetExpression initializer = ((JetProperty) declaration).getInitializer();
if (initializer != null && !(initializer instanceof JetConstantExpression)) {
final PropertyDescriptor descriptor =
(PropertyDescriptor) state.getBindingContext().get(BindingContext.VARIABLE, declaration);
assert descriptor != null;
codegen.genToJVMStack(initializer);
StackValue.Property propValue = codegen.intermediateValueForProperty(descriptor, true, null);
propValue.store(propValue.type, new InstructionAdapter(mv));
}
}
}
for (JetDeclaration declaration : file.getDeclarations()) {
if (declaration instanceof JetProperty) {
final JetExpression initializer = ((JetProperty) declaration).getInitializer();
if (initializer != null && !(initializer instanceof JetConstantExpression)) {
final PropertyDescriptor descriptor =
(PropertyDescriptor) state.getBindingContext().get(BindingContext.VARIABLE, declaration);
assert descriptor != null;
codegen.genToJVMStack(initializer);
StackValue.Property propValue = codegen.intermediateValueForProperty(descriptor, true, null);
propValue.store(propValue.type, new InstructionAdapter(mv));
}
mv.visitInsn(RETURN);
FunctionCodegen.endVisit(mv, "static initializer for namespace", namespace);
mv.visitEnd();
}
}
});
mv.visitInsn(RETURN);
FunctionCodegen.endVisit(mv, "static initializer for namespace", file);
mv.visitEnd();
}
}
private boolean hasNonConstantPropertyInitializers() {
@@ -66,10 +66,10 @@ public class PropertyCodegen extends GenerationStateAware {
throw new UnsupportedOperationException("expect a property to have a property descriptor");
}
PropertyDescriptor propertyDescriptor = (PropertyDescriptor) descriptor;
assert kind == OwnerKind.NAMESPACE || kind == OwnerKind.IMPLEMENTATION || kind == OwnerKind.TRAIT_IMPL
assert kind instanceof OwnerKind.StaticDelegateKind || kind == OwnerKind.NAMESPACE || kind == OwnerKind.IMPLEMENTATION || kind == OwnerKind.TRAIT_IMPL
: "Generating property with a wrong kind (" + kind + "): " + descriptor;
if (kind != OwnerKind.TRAIT_IMPL) {
if (kind != OwnerKind.TRAIT_IMPL && !(kind instanceof OwnerKind.StaticDelegateKind)) {
generateBackingField(p, propertyDescriptor);
}
generateGetter(p, propertyDescriptor);
@@ -166,7 +166,7 @@ public class PropertyCodegen extends GenerationStateAware {
return;
}
if (kind == OwnerKind.NAMESPACE) {
if (kind == OwnerKind.NAMESPACE || kind instanceof OwnerKind.StaticDelegateKind) {
flags |= ACC_STATIC;
}
@@ -200,6 +200,9 @@ public class PropertyCodegen extends GenerationStateAware {
if (state.getClassBuilderMode() == ClassBuilderMode.STUBS) {
genStubThrow(mv);
}
else if (kind instanceof OwnerKind.StaticDelegateKind) {
FunctionCodegen.generateStaticDelegateMethodBody(mv, jvmMethodSignature.getAsmMethod(), (OwnerKind.StaticDelegateKind) kind);
}
else {
InstructionAdapter iv = new InstructionAdapter(mv);
if (kind != OwnerKind.NAMESPACE) {
@@ -246,7 +249,7 @@ public class PropertyCodegen extends GenerationStateAware {
return;
}
if (kind == OwnerKind.NAMESPACE) {
if (kind == OwnerKind.NAMESPACE || kind instanceof OwnerKind.StaticDelegateKind) {
flags |= ACC_STATIC;
}
@@ -276,6 +279,9 @@ public class PropertyCodegen extends GenerationStateAware {
if (state.getClassBuilderMode() == ClassBuilderMode.STUBS) {
genStubThrow(mv);
}
else if (kind instanceof OwnerKind.StaticDelegateKind) {
FunctionCodegen.generateStaticDelegateMethodBody(mv, jvmMethodSignature.getAsmMethod(), (OwnerKind.StaticDelegateKind) kind);
}
else {
InstructionAdapter iv = new InstructionAdapter(mv);
final Type type = typeMapper.mapType(propertyDescriptor);
@@ -29,6 +29,7 @@ import org.jetbrains.jet.codegen.signature.*;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.psi.JetDelegatorToSuperCall;
import org.jetbrains.jet.lang.psi.JetElement;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BindingContextUtils;
import org.jetbrains.jet.lang.resolve.BindingTrace;
@@ -66,7 +67,7 @@ public class JetTypeMapper extends BindingTraceAware {
DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration();
if (containingDeclaration instanceof NamespaceDescriptor) {
return jvmClassNameForNamespace((NamespaceDescriptor) containingDeclaration);
return jvmClassNameForNamespace((NamespaceDescriptor) containingDeclaration, descriptor);
}
else if (containingDeclaration instanceof ClassDescriptor) {
ClassDescriptor classDescriptor = (ClassDescriptor) containingDeclaration;
@@ -121,7 +122,7 @@ public class JetTypeMapper extends BindingTraceAware {
}
@NotNull
private JvmClassName jvmClassNameForNamespace(@NotNull NamespaceDescriptor namespace) {
private JvmClassName jvmClassNameForNamespace(@NotNull NamespaceDescriptor namespace, @NotNull DeclarationDescriptor descriptor) {
StringBuilder r = new StringBuilder();
@@ -145,7 +146,20 @@ public class JetTypeMapper extends BindingTraceAware {
if (r.length() > 0) {
r.append("/");
}
r.append(PackageClassUtils.getPackageClassName(namespace.getFqName()));
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()));
}
}
else {
r.append(PackageClassUtils.getPackageClassName(namespace.getFqName()));
}
}
if (r.length() == 0) {
@@ -450,7 +464,7 @@ public class JetTypeMapper extends BindingTraceAware {
JvmClassName thisClass;
if (functionParent instanceof NamespaceDescriptor) {
assert !superCall;
owner = jvmClassNameForNamespace((NamespaceDescriptor) functionParent);
owner = jvmClassNameForNamespace((NamespaceDescriptor) functionParent, functionDescriptor);
ownerForDefaultImpl = ownerForDefaultParam = owner;
invokeOpcode = INVOKESTATIC;
thisClass = null;