From 65c3b7dab0370fd704395c0e53ac69484481ee72 Mon Sep 17 00:00:00 2001 From: Alex Tkachman Date: Wed, 29 Aug 2012 21:14:12 +0300 Subject: [PATCH] subclasses of CodegenContext moved to upper level --- .../jet/codegen/ExpressionCodegen.java | 16 +- .../jet/codegen/FunctionCodegen.java | 9 +- .../codegen/ImplementationBodyCodegen.java | 11 +- .../jetbrains/jet/codegen/ScriptCodegen.java | 5 +- .../context/AnonymousClassContext.java | 53 ++++ .../jet/codegen/context/ClassContext.java | 48 ++++ .../jet/codegen/context/ClosureContext.java | 64 +++++ .../jet/codegen/context/CodegenContext.java | 268 +----------------- .../codegen/context/ConstructorContext.java | 52 ++++ .../jet/codegen/context/MethodContext.java | 70 +++++ .../jet/codegen/context/NamespaceContext.java | 39 +++ .../jet/codegen/context/ReceiverContext.java | 46 +++ .../jet/codegen/context/RootContext.java | 85 ++++++ .../jet/codegen/context/ScriptContext.java | 53 ++++ 14 files changed, 531 insertions(+), 288 deletions(-) create mode 100644 compiler/backend/src/org/jetbrains/jet/codegen/context/AnonymousClassContext.java create mode 100644 compiler/backend/src/org/jetbrains/jet/codegen/context/ClassContext.java create mode 100644 compiler/backend/src/org/jetbrains/jet/codegen/context/ClosureContext.java create mode 100644 compiler/backend/src/org/jetbrains/jet/codegen/context/ConstructorContext.java create mode 100644 compiler/backend/src/org/jetbrains/jet/codegen/context/MethodContext.java create mode 100644 compiler/backend/src/org/jetbrains/jet/codegen/context/NamespaceContext.java create mode 100644 compiler/backend/src/org/jetbrains/jet/codegen/context/ReceiverContext.java create mode 100644 compiler/backend/src/org/jetbrains/jet/codegen/context/RootContext.java create mode 100644 compiler/backend/src/org/jetbrains/jet/codegen/context/ScriptContext.java diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java index 8d9b4062847..65578e8b712 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java @@ -1491,7 +1491,7 @@ public class ExpressionCodegen extends JetVisitor implem boolean isInsideClass = !isFakeOverride && (((containingDeclaration == null && !context.hasThisDescriptor() || context.hasThisDescriptor() && containingDeclaration == context.getThisDescriptor()) || - (context.getParentContext() instanceof CodegenContext.NamespaceContext) && + (context.getParentContext() instanceof NamespaceContext) && context.getParentContext().getContextDescriptor() == containingDeclaration) && contextKind() != OwnerKind.TRAIT_IMPL); Method getter = null; @@ -1673,7 +1673,7 @@ public class ExpressionCodegen extends JetVisitor implem if (enclosed != context.getThisDescriptor()) { CodegenContext c = context; //noinspection ConstantConditions - while (!(c instanceof CodegenContext.ClassContext) || + while (!(c instanceof ClassContext) || !DescriptorUtils.isSubclass(c.getThisDescriptor(), enclosed)) { c = c.getParentContext(); assert c != null; @@ -1900,12 +1900,12 @@ public class ExpressionCodegen extends JetVisitor implem CodegenContext cur = context; StackValue result = StackValue.local(0, OBJECT_TYPE); while (cur != null) { - if (cur instanceof CodegenContext.MethodContext && !(cur instanceof CodegenContext.ConstructorContext)) { + if (cur instanceof MethodContext && !(cur instanceof ConstructorContext)) { cur = cur.getParentContext(); } - if (cur instanceof CodegenContext.ScriptContext) { - CodegenContext.ScriptContext scriptContext = (CodegenContext.ScriptContext) cur; + if (cur instanceof ScriptContext) { + ScriptContext scriptContext = (ScriptContext) cur; JvmClassName currentScriptClassName = classNameForScriptDescriptor(state.getBindingContext(), @@ -1927,7 +1927,7 @@ public class ExpressionCodegen extends JetVisitor implem assert cur != null; result = cur.getOuterExpression(result, false); - if (cur instanceof CodegenContext.ConstructorContext) { + if (cur instanceof ConstructorContext) { cur = cur.getParentContext(); } assert cur != null; @@ -1945,7 +1945,7 @@ public class ExpressionCodegen extends JetVisitor implem Type type = asmType(calleeContainingClass.getDefaultType()); StackValue result = StackValue.local(0, type); while (cur != null) { - if (cur instanceof CodegenContext.MethodContext && !(cur instanceof CodegenContext.ConstructorContext)) { + if (cur instanceof MethodContext && !(cur instanceof ConstructorContext)) { cur = cur.getParentContext(); } @@ -1963,7 +1963,7 @@ public class ExpressionCodegen extends JetVisitor implem result = cur.getOuterExpression(result, false); - if (cur instanceof CodegenContext.ConstructorContext) { + if (cur instanceof ConstructorContext) { cur = cur.getParentContext(); } assert cur != null; diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/FunctionCodegen.java b/compiler/backend/src/org/jetbrains/jet/codegen/FunctionCodegen.java index bc2708dcb92..6837d3762bd 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/FunctionCodegen.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/FunctionCodegen.java @@ -26,6 +26,7 @@ import org.jetbrains.asm4.Type; import org.jetbrains.asm4.commons.InstructionAdapter; import org.jetbrains.asm4.commons.Method; import org.jetbrains.jet.codegen.context.CodegenContext; +import org.jetbrains.jet.codegen.context.MethodContext; import org.jetbrains.jet.codegen.signature.JvmMethodSignature; import org.jetbrains.jet.codegen.signature.kotlin.JetMethodAnnotationWriter; import org.jetbrains.jet.codegen.signature.kotlin.JetValueParameterAnnotationWriter; @@ -80,7 +81,7 @@ public class FunctionCodegen { @Nullable String propertyTypeSignature, FunctionDescriptor functionDescriptor ) { - CodegenContext.MethodContext funContext = owner.intoFunction(functionDescriptor); + MethodContext funContext = owner.intoFunction(functionDescriptor); final JetExpression bodyExpression = f.getBodyExpression(); generatedMethod(bodyExpression, jvmMethod, needJetAnnotations, propertyTypeSignature, funContext, functionDescriptor, f); @@ -90,7 +91,7 @@ public class FunctionCodegen { JetExpression bodyExpressions, JvmMethodSignature jvmSignature, boolean needJetAnnotations, @Nullable String propertyTypeSignature, - CodegenContext.MethodContext context, + MethodContext context, FunctionDescriptor functionDescriptor, JetDeclarationWithBody fun ) { @@ -422,7 +423,7 @@ public class FunctionCodegen { } static void generateDefaultIfNeeded( - CodegenContext.MethodContext owner, + MethodContext owner, GenerationState state, ClassBuilder v, Method jvmSignature, @@ -482,7 +483,7 @@ public class FunctionCodegen { } private static void generateDefaultImpl( - CodegenContext.MethodContext owner, + MethodContext owner, GenerationState state, Method jvmSignature, FunctionDescriptor functionDescriptor, diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/ImplementationBodyCodegen.java b/compiler/backend/src/org/jetbrains/jet/codegen/ImplementationBodyCodegen.java index 83d0e4cde4a..e22505b2828 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/ImplementationBodyCodegen.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/ImplementationBodyCodegen.java @@ -26,10 +26,7 @@ import org.jetbrains.asm4.MethodVisitor; import org.jetbrains.asm4.Type; import org.jetbrains.asm4.commons.InstructionAdapter; import org.jetbrains.asm4.commons.Method; -import org.jetbrains.jet.codegen.context.CalculatedClosure; -import org.jetbrains.jet.codegen.context.CodegenBinding; -import org.jetbrains.jet.codegen.context.CodegenContext; -import org.jetbrains.jet.codegen.context.MutableClosure; +import org.jetbrains.jet.codegen.context.*; import org.jetbrains.jet.codegen.signature.*; import org.jetbrains.jet.codegen.signature.kotlin.JetMethodAnnotationWriter; import org.jetbrains.jet.codegen.signature.kotlin.JetValueParameterAnnotationWriter; @@ -560,7 +557,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen { ConstructorDescriptor constructorDescriptor = bindingContext.get(BindingContext.CONSTRUCTOR, myClass); - final CodegenContext.ConstructorContext constructorContext = context.intoConstructor(constructorDescriptor); + final ConstructorContext constructorContext = context.intoConstructor(constructorDescriptor); lookupConstructorExpressionsInClosureIfPresent(constructorContext); @@ -599,7 +596,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen { private void generatePrimiryConstructorImpl( ConstructorDescriptor constructorDescriptor, - CodegenContext.ConstructorContext constructorContext, + ConstructorContext constructorContext, JvmMethodSignature constructorMethod, CallableMethod callableMethod, boolean hasThis0, @@ -793,7 +790,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen { generateDelegates(superClass, delegateContext, field); } - private void lookupConstructorExpressionsInClosureIfPresent(final CodegenContext.ConstructorContext constructorContext) { + private void lookupConstructorExpressionsInClosureIfPresent(final ConstructorContext constructorContext) { final JetVisitorVoid visitor = new JetVisitorVoid() { @Override public void visitJetElement(JetElement e) { diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/ScriptCodegen.java b/compiler/backend/src/org/jetbrains/jet/codegen/ScriptCodegen.java index e723c679994..7921e0a8351 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/ScriptCodegen.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/ScriptCodegen.java @@ -23,6 +23,7 @@ import org.jetbrains.asm4.MethodVisitor; import org.jetbrains.asm4.Type; import org.jetbrains.asm4.commons.InstructionAdapter; import org.jetbrains.jet.codegen.context.CodegenContext; +import org.jetbrains.jet.codegen.context.ScriptContext; import org.jetbrains.jet.codegen.signature.JvmMethodSignature; import org.jetbrains.jet.lang.descriptors.ClassDescriptor; import org.jetbrains.jet.lang.descriptors.ScriptDescriptor; @@ -94,8 +95,8 @@ public class ScriptCodegen { ClassDescriptor classDescriptorForScript = bindingContext.get(CLASS_FOR_FUNCTION, scriptDescriptor); assert classDescriptorForScript != null; - CodegenContext.ScriptContext context = - (CodegenContext.ScriptContext) CodegenContext.STATIC + ScriptContext context = + (ScriptContext) CodegenContext.STATIC .intoScript(scriptDescriptor, classDescriptorForScript); JvmClassName className = bindingContext.get(FQN, classDescriptorForScript); diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/context/AnonymousClassContext.java b/compiler/backend/src/org/jetbrains/jet/codegen/context/AnonymousClassContext.java new file mode 100644 index 00000000000..99e6366722c --- /dev/null +++ b/compiler/backend/src/org/jetbrains/jet/codegen/context/AnonymousClassContext.java @@ -0,0 +1,53 @@ +/* + * Copyright 2010-2012 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.jet.codegen.context; + +import org.jetbrains.jet.codegen.JetTypeMapper; +import org.jetbrains.jet.codegen.OwnerKind; +import org.jetbrains.jet.lang.descriptors.ClassDescriptor; + +import static org.jetbrains.jet.codegen.context.CodegenBinding.CLOSURE; + +/** + * @author alex.tkachman +*/ +class AnonymousClassContext extends CodegenContext { + public AnonymousClassContext( + JetTypeMapper typeMapper, + ClassDescriptor contextDescriptor, + OwnerKind contextKind, + CodegenContext parentContext, + LocalLookup localLookup + ) { + //noinspection SuspiciousMethodCalls + super(contextDescriptor, contextKind, parentContext, (MutableClosure) typeMapper.getBindingContext().get(CLOSURE, + contextDescriptor), + contextDescriptor, + localLookup); + initOuterExpression(typeMapper, contextDescriptor); + } + + @Override + public boolean isStatic() { + return false; + } + + @Override + public String toString() { + return "Anonymous: " + getThisDescriptor(); + } +} diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/context/ClassContext.java b/compiler/backend/src/org/jetbrains/jet/codegen/context/ClassContext.java new file mode 100644 index 00000000000..463fc61ba6c --- /dev/null +++ b/compiler/backend/src/org/jetbrains/jet/codegen/context/ClassContext.java @@ -0,0 +1,48 @@ +/* + * Copyright 2010-2012 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.jet.codegen.context; + +import org.jetbrains.jet.codegen.JetTypeMapper; +import org.jetbrains.jet.codegen.OwnerKind; +import org.jetbrains.jet.lang.descriptors.ClassDescriptor; + +import static org.jetbrains.jet.codegen.context.CodegenBinding.CLOSURE; + +/** + * @author alex.tkachman +*/ +public class ClassContext extends CodegenContext { + public ClassContext( + JetTypeMapper typeMapper, + ClassDescriptor contextDescriptor, + OwnerKind contextKind, + CodegenContext parentContext, + LocalLookup localLookup + ) { + //noinspection SuspiciousMethodCalls + super(contextDescriptor, contextKind, parentContext, (MutableClosure) typeMapper.getBindingContext().get(CLOSURE, + contextDescriptor), + contextDescriptor, + localLookup); + initOuterExpression(typeMapper, contextDescriptor); + } + + @Override + public boolean isStatic() { + return false; + } +} diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/context/ClosureContext.java b/compiler/backend/src/org/jetbrains/jet/codegen/context/ClosureContext.java new file mode 100644 index 00000000000..d99f79d7df2 --- /dev/null +++ b/compiler/backend/src/org/jetbrains/jet/codegen/context/ClosureContext.java @@ -0,0 +1,64 @@ +/* + * Copyright 2010-2012 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.jet.codegen.context; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.codegen.JetTypeMapper; +import org.jetbrains.jet.codegen.OwnerKind; +import org.jetbrains.jet.lang.descriptors.ClassDescriptor; +import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; +import org.jetbrains.jet.lang.descriptors.FunctionDescriptor; + +import static org.jetbrains.jet.codegen.context.CodegenBinding.CLOSURE; + +/** + * @author alex.tkachman +*/ +class ClosureContext extends ReceiverContext { + private final ClassDescriptor classDescriptor; + + public ClosureContext( + JetTypeMapper typeMapper, + FunctionDescriptor contextDescriptor, + ClassDescriptor classDescriptor, + CodegenContext parentContext, + LocalLookup localLookup + ) { + //noinspection SuspiciousMethodCalls + super(contextDescriptor, OwnerKind.IMPLEMENTATION, parentContext, + (MutableClosure) typeMapper.getBindingContext().get(CLOSURE, classDescriptor), classDescriptor, localLookup); + this.classDescriptor = classDescriptor; + + initOuterExpression(typeMapper, classDescriptor); + } + + @NotNull + @Override + public DeclarationDescriptor getContextDescriptor() { + return classDescriptor; + } + + @Override + public boolean isStatic() { + return false; + } + + @Override + public String toString() { + return "Closure: " + classDescriptor; + } +} diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/context/CodegenContext.java b/compiler/backend/src/org/jetbrains/jet/codegen/context/CodegenContext.java index 18031570ac6..c5bd9599ee0 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/context/CodegenContext.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/context/CodegenContext.java @@ -22,17 +22,13 @@ import org.jetbrains.asm4.Type; import org.jetbrains.jet.codegen.*; import org.jetbrains.jet.lang.descriptors.*; import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor; -import org.jetbrains.jet.lang.resolve.name.Name; -import org.jetbrains.jet.lang.types.TypeSubstitutor; import java.util.Collections; import java.util.HashMap; -import java.util.List; import java.util.Map; import static org.jetbrains.jet.codegen.AsmTypeConstants.*; import static org.jetbrains.jet.codegen.context.CodegenBinding.CLASS_FOR_FUNCTION; -import static org.jetbrains.jet.codegen.context.CodegenBinding.CLOSURE; import static org.jetbrains.jet.codegen.context.CodegenBinding.FQN; /* @@ -41,19 +37,8 @@ import static org.jetbrains.jet.codegen.context.CodegenBinding.FQN; */ public abstract class CodegenContext { - public static final CodegenContext STATIC = - new CodegenContext(new FakeDescriptorForStaticContext(), OwnerKind.NAMESPACE, null, null, null, null) { - @Override - public boolean isStatic() { - return true; - } + public static final CodegenContext STATIC = new RootContext(); - @Override - public String toString() { - return "ROOT"; - } - }; - private static final StackValue local1 = StackValue.local(1, OBJECT_TYPE); @NotNull private final DeclarationDescriptor contextDescriptor; @@ -298,255 +283,4 @@ public abstract class CodegenContext { public Map getAccessors() { return accessors == null ? Collections.emptyMap() : accessors; } - - private static class FakeDescriptorForStaticContext implements DeclarationDescriptor { - - @NotNull - @Override - public DeclarationDescriptor getOriginal() { - throw new IllegalStateException(); - } - - @Override - public DeclarationDescriptor getContainingDeclaration() { - throw new IllegalStateException(); - } - - @Override - public DeclarationDescriptor substitute(TypeSubstitutor substitutor) { - throw new IllegalStateException(); - } - - @Override - public R accept(DeclarationDescriptorVisitor visitor, D data) { - throw new IllegalStateException(); - } - - @Override - public void acceptVoid(DeclarationDescriptorVisitor visitor) { - throw new IllegalStateException(); - } - - @Override - public List getAnnotations() { - throw new IllegalStateException(); - } - - @NotNull - @Override - public Name getName() { - throw new IllegalStateException(); - } - } - - private abstract static class ReceiverContext extends CodegenContext { - final CallableDescriptor receiverDescriptor; - - public ReceiverContext( - CallableDescriptor contextDescriptor, - OwnerKind contextKind, - CodegenContext parentContext, - @Nullable MutableClosure closure, - ClassDescriptor thisDescriptor, - @Nullable LocalLookup localLookup - ) { - super(contextDescriptor, contextKind, parentContext, closure, thisDescriptor, localLookup); - receiverDescriptor = contextDescriptor.getReceiverParameter().exists() ? contextDescriptor : null; - } - - @Override - public CallableDescriptor getReceiverDescriptor() { - return receiverDescriptor; - } - } - - public static class MethodContext extends ReceiverContext { - public MethodContext( - @NotNull FunctionDescriptor contextType, - OwnerKind contextKind, - CodegenContext parentContext - ) { - super(contextType instanceof PropertyAccessorDescriptor - ? ((PropertyAccessorDescriptor) contextType).getCorrespondingProperty() - : contextType, contextKind, parentContext, null, - parentContext.hasThisDescriptor() ? parentContext.getThisDescriptor() : null, null); - } - - @Override - public StackValue lookupInContext(DeclarationDescriptor d, @Nullable StackValue result, GenerationState state, boolean ignoreNoOuter) { - if (getContextDescriptor() == d) { - return StackValue.local(0, AsmTypeConstants.OBJECT_TYPE); - } - - //noinspection ConstantConditions - return getParentContext().lookupInContext(d, result, state, ignoreNoOuter); - } - - @Override - public boolean isStatic() { - //noinspection ConstantConditions - return getParentContext().isStatic(); - } - - @Override - public StackValue getOuterExpression(StackValue prefix, boolean ignoreNoOuter) { - //noinspection ConstantConditions - return getParentContext().getOuterExpression(prefix, false); - } - - @Override - public String toString() { - return "Method: " + getContextDescriptor(); - } - } - - public static class ConstructorContext extends MethodContext { - public ConstructorContext( - ConstructorDescriptor contextDescriptor, - OwnerKind kind, - CodegenContext parent - ) { - super(contextDescriptor, kind, parent); - - final ClassDescriptor type = getEnclosingClass(); - outerExpression = type != null ? local1 : null; - } - - @Override - public StackValue getOuterExpression(StackValue prefix, boolean ignoreNoOuter) { - return outerExpression; - } - - @Override - public String toString() { - return "Constructor: " + getContextDescriptor().getName(); - } - } - - public static class ScriptContext extends CodegenContext { - @NotNull - private final ScriptDescriptor scriptDescriptor; - - public ScriptContext( - @NotNull ScriptDescriptor scriptDescriptor, - @NotNull ClassDescriptor contextDescriptor, - @NotNull OwnerKind contextKind, - @Nullable CodegenContext parentContext, - @Nullable MutableClosure closure - ) { - super(contextDescriptor, contextKind, parentContext, closure, contextDescriptor, null); - - this.scriptDescriptor = scriptDescriptor; - } - - @NotNull - public ScriptDescriptor getScriptDescriptor() { - return scriptDescriptor; - } - - @Override - public boolean isStatic() { - return true; - } - } - - public static class ClassContext extends CodegenContext { - public ClassContext( - JetTypeMapper typeMapper, - ClassDescriptor contextDescriptor, - OwnerKind contextKind, - CodegenContext parentContext, - LocalLookup localLookup - ) { - //noinspection SuspiciousMethodCalls - super(contextDescriptor, contextKind, parentContext, (MutableClosure) typeMapper.getBindingContext().get(CLOSURE, - contextDescriptor), - contextDescriptor, - localLookup); - initOuterExpression(typeMapper, contextDescriptor); - } - - @Override - public boolean isStatic() { - return false; - } - } - - private static class AnonymousClassContext extends CodegenContext { - public AnonymousClassContext( - JetTypeMapper typeMapper, - ClassDescriptor contextDescriptor, - OwnerKind contextKind, - CodegenContext parentContext, - LocalLookup localLookup - ) { - //noinspection SuspiciousMethodCalls - super(contextDescriptor, contextKind, parentContext, (MutableClosure) typeMapper.getBindingContext().get(CLOSURE, - contextDescriptor), - contextDescriptor, - localLookup); - initOuterExpression(typeMapper, contextDescriptor); - } - - @Override - public boolean isStatic() { - return false; - } - - @Override - public String toString() { - return "Anonymous: " + getThisDescriptor(); - } - } - - private static class ClosureContext extends ReceiverContext { - private final ClassDescriptor classDescriptor; - - public ClosureContext( - JetTypeMapper typeMapper, - FunctionDescriptor contextDescriptor, - ClassDescriptor classDescriptor, - CodegenContext parentContext, - LocalLookup localLookup - ) { - //noinspection SuspiciousMethodCalls - super(contextDescriptor, OwnerKind.IMPLEMENTATION, parentContext, - (MutableClosure) typeMapper.getBindingContext().get(CLOSURE, classDescriptor), classDescriptor, localLookup); - this.classDescriptor = classDescriptor; - - initOuterExpression(typeMapper, classDescriptor); - } - - @NotNull - @Override - public DeclarationDescriptor getContextDescriptor() { - return classDescriptor; - } - - @Override - public boolean isStatic() { - return false; - } - - @Override - public String toString() { - return "Closure: " + classDescriptor; - } - } - - public static class NamespaceContext extends CodegenContext { - public NamespaceContext(NamespaceDescriptor contextDescriptor, CodegenContext parent, OwnerKind kind) { - super(contextDescriptor, kind != null ? kind : OwnerKind.NAMESPACE, parent, null, null, null); - } - - @Override - public boolean isStatic() { - return true; - } - - @Override - public String toString() { - return "Namespace: " + getContextDescriptor().getName(); - } - } } diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/context/ConstructorContext.java b/compiler/backend/src/org/jetbrains/jet/codegen/context/ConstructorContext.java new file mode 100644 index 00000000000..4c3dd6603c2 --- /dev/null +++ b/compiler/backend/src/org/jetbrains/jet/codegen/context/ConstructorContext.java @@ -0,0 +1,52 @@ +/* + * Copyright 2010-2012 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.jet.codegen.context; + +import org.jetbrains.jet.codegen.OwnerKind; +import org.jetbrains.jet.codegen.StackValue; +import org.jetbrains.jet.lang.descriptors.ClassDescriptor; +import org.jetbrains.jet.lang.descriptors.ConstructorDescriptor; + +import static org.jetbrains.jet.codegen.AsmTypeConstants.OBJECT_TYPE; + +/** + * @author alex.tkachman +*/ +public class ConstructorContext extends MethodContext { + private static final StackValue local1 = StackValue.local(1, OBJECT_TYPE); + + public ConstructorContext( + ConstructorDescriptor contextDescriptor, + OwnerKind kind, + CodegenContext parent + ) { + super(contextDescriptor, kind, parent); + + final ClassDescriptor type = getEnclosingClass(); + outerExpression = type != null ? local1 : null; + } + + @Override + public StackValue getOuterExpression(StackValue prefix, boolean ignoreNoOuter) { + return outerExpression; + } + + @Override + public String toString() { + return "Constructor: " + getContextDescriptor().getName(); + } +} diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/context/MethodContext.java b/compiler/backend/src/org/jetbrains/jet/codegen/context/MethodContext.java new file mode 100644 index 00000000000..4eaf526c2dd --- /dev/null +++ b/compiler/backend/src/org/jetbrains/jet/codegen/context/MethodContext.java @@ -0,0 +1,70 @@ +/* + * Copyright 2010-2012 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.jet.codegen.context; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.codegen.AsmTypeConstants; +import org.jetbrains.jet.codegen.GenerationState; +import org.jetbrains.jet.codegen.OwnerKind; +import org.jetbrains.jet.codegen.StackValue; +import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; +import org.jetbrains.jet.lang.descriptors.FunctionDescriptor; +import org.jetbrains.jet.lang.descriptors.PropertyAccessorDescriptor; + +/** + * @author alex.tkachman +*/ +public class MethodContext extends ReceiverContext { + public MethodContext( + @NotNull FunctionDescriptor contextType, + OwnerKind contextKind, + CodegenContext parentContext + ) { + super(contextType instanceof PropertyAccessorDescriptor + ? ((PropertyAccessorDescriptor) contextType).getCorrespondingProperty() + : contextType, contextKind, parentContext, null, + parentContext.hasThisDescriptor() ? parentContext.getThisDescriptor() : null, null); + } + + @Override + public StackValue lookupInContext(DeclarationDescriptor d, @Nullable StackValue result, GenerationState state, boolean ignoreNoOuter) { + if (getContextDescriptor() == d) { + return StackValue.local(0, AsmTypeConstants.OBJECT_TYPE); + } + + //noinspection ConstantConditions + return getParentContext().lookupInContext(d, result, state, ignoreNoOuter); + } + + @Override + public boolean isStatic() { + //noinspection ConstantConditions + return getParentContext().isStatic(); + } + + @Override + public StackValue getOuterExpression(StackValue prefix, boolean ignoreNoOuter) { + //noinspection ConstantConditions + return getParentContext().getOuterExpression(prefix, false); + } + + @Override + public String toString() { + return "Method: " + getContextDescriptor(); + } +} diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/context/NamespaceContext.java b/compiler/backend/src/org/jetbrains/jet/codegen/context/NamespaceContext.java new file mode 100644 index 00000000000..74edc400c7d --- /dev/null +++ b/compiler/backend/src/org/jetbrains/jet/codegen/context/NamespaceContext.java @@ -0,0 +1,39 @@ +/* + * Copyright 2010-2012 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.jet.codegen.context; + +import org.jetbrains.jet.codegen.OwnerKind; +import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor; + +/** + * @author alex.tkachman +*/ +public class NamespaceContext extends CodegenContext { + public NamespaceContext(NamespaceDescriptor contextDescriptor, CodegenContext parent, OwnerKind kind) { + super(contextDescriptor, kind != null ? kind : OwnerKind.NAMESPACE, parent, null, null, null); + } + + @Override + public boolean isStatic() { + return true; + } + + @Override + public String toString() { + return "Namespace: " + getContextDescriptor().getName(); + } +} diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/context/ReceiverContext.java b/compiler/backend/src/org/jetbrains/jet/codegen/context/ReceiverContext.java new file mode 100644 index 00000000000..ef3fff7f685 --- /dev/null +++ b/compiler/backend/src/org/jetbrains/jet/codegen/context/ReceiverContext.java @@ -0,0 +1,46 @@ +/* + * Copyright 2010-2012 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.jet.codegen.context; + +import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.codegen.OwnerKind; +import org.jetbrains.jet.lang.descriptors.CallableDescriptor; +import org.jetbrains.jet.lang.descriptors.ClassDescriptor; + +/** + * @author alex.tkachman +*/ +abstract class ReceiverContext extends CodegenContext { + final CallableDescriptor receiverDescriptor; + + public ReceiverContext( + CallableDescriptor contextDescriptor, + OwnerKind contextKind, + CodegenContext parentContext, + @Nullable MutableClosure closure, + ClassDescriptor thisDescriptor, + @Nullable LocalLookup localLookup + ) { + super(contextDescriptor, contextKind, parentContext, closure, thisDescriptor, localLookup); + receiverDescriptor = contextDescriptor.getReceiverParameter().exists() ? contextDescriptor : null; + } + + @Override + public CallableDescriptor getReceiverDescriptor() { + return receiverDescriptor; + } +} diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/context/RootContext.java b/compiler/backend/src/org/jetbrains/jet/codegen/context/RootContext.java new file mode 100644 index 00000000000..2beee98e672 --- /dev/null +++ b/compiler/backend/src/org/jetbrains/jet/codegen/context/RootContext.java @@ -0,0 +1,85 @@ +/* + * Copyright 2010-2012 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.jet.codegen.context; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.codegen.OwnerKind; +import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; +import org.jetbrains.jet.lang.descriptors.DeclarationDescriptorVisitor; +import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor; +import org.jetbrains.jet.lang.resolve.name.Name; +import org.jetbrains.jet.lang.types.TypeSubstitutor; + +import java.util.List; + +/** + * @author alex.tkachman +*/ +class RootContext extends CodegenContext { + public RootContext() { + super(new FakeDescriptor(), OwnerKind.NAMESPACE, null, null, null, null); + } + + @Override + public boolean isStatic() { + return true; + } + + @Override + public String toString() { + return "ROOT"; + } + + private static class FakeDescriptor implements DeclarationDescriptor { + @NotNull + @Override + public DeclarationDescriptor getOriginal() { + throw new IllegalStateException(); + } + + @Override + public DeclarationDescriptor getContainingDeclaration() { + throw new IllegalStateException(); + } + + @Override + public DeclarationDescriptor substitute(TypeSubstitutor substitutor) { + throw new IllegalStateException(); + } + + @Override + public R accept(DeclarationDescriptorVisitor visitor, D data) { + throw new IllegalStateException(); + } + + @Override + public void acceptVoid(DeclarationDescriptorVisitor visitor) { + throw new IllegalStateException(); + } + + @Override + public List getAnnotations() { + throw new IllegalStateException(); + } + + @NotNull + @Override + public Name getName() { + throw new IllegalStateException(); + } + } +} diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/context/ScriptContext.java b/compiler/backend/src/org/jetbrains/jet/codegen/context/ScriptContext.java new file mode 100644 index 00000000000..b402aef58e6 --- /dev/null +++ b/compiler/backend/src/org/jetbrains/jet/codegen/context/ScriptContext.java @@ -0,0 +1,53 @@ +/* + * Copyright 2010-2012 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.jet.codegen.context; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.codegen.OwnerKind; +import org.jetbrains.jet.lang.descriptors.ClassDescriptor; +import org.jetbrains.jet.lang.descriptors.ScriptDescriptor; + +/** + * @author alex.tkachman +*/ +public class ScriptContext extends CodegenContext { + @NotNull + private final ScriptDescriptor scriptDescriptor; + + public ScriptContext( + @NotNull ScriptDescriptor scriptDescriptor, + @NotNull ClassDescriptor contextDescriptor, + @NotNull OwnerKind contextKind, + @Nullable CodegenContext parentContext, + @Nullable MutableClosure closure + ) { + super(contextDescriptor, contextKind, parentContext, closure, contextDescriptor, null); + + this.scriptDescriptor = scriptDescriptor; + } + + @NotNull + public ScriptDescriptor getScriptDescriptor() { + return scriptDescriptor; + } + + @Override + public boolean isStatic() { + return true; + } +}