Container of value parameter is always a callable

This commit is contained in:
Alexander Udalov
2015-07-07 16:31:56 +03:00
parent 636b63a8c5
commit 9b832d4b87
9 changed files with 39 additions and 40 deletions
@@ -46,6 +46,7 @@ import org.jetbrains.kotlin.codegen.state.JetTypeMapper;
import org.jetbrains.kotlin.codegen.when.SwitchCodegen; import org.jetbrains.kotlin.codegen.when.SwitchCodegen;
import org.jetbrains.kotlin.codegen.when.SwitchCodegenUtil; import org.jetbrains.kotlin.codegen.when.SwitchCodegenUtil;
import org.jetbrains.kotlin.descriptors.*; import org.jetbrains.kotlin.descriptors.*;
import org.jetbrains.kotlin.descriptors.impl.ScriptCodeDescriptor;
import org.jetbrains.kotlin.diagnostics.DiagnosticUtils; import org.jetbrains.kotlin.diagnostics.DiagnosticUtils;
import org.jetbrains.kotlin.diagnostics.Errors; import org.jetbrains.kotlin.diagnostics.Errors;
import org.jetbrains.kotlin.lexer.JetTokens; import org.jetbrains.kotlin.lexer.JetTokens;
@@ -2036,11 +2037,14 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
return localOrCaptured; return localOrCaptured;
} }
if (descriptor instanceof ValueParameterDescriptor && descriptor.getContainingDeclaration() instanceof ScriptDescriptor) { DeclarationDescriptor container = descriptor.getContainingDeclaration();
ScriptDescriptor scriptDescriptor = (ScriptDescriptor) descriptor.getContainingDeclaration(); if (descriptor instanceof ValueParameterDescriptor && container instanceof ScriptCodeDescriptor) {
ScriptCodeDescriptor scriptCodeDescriptor = (ScriptCodeDescriptor) container;
ScriptDescriptor scriptDescriptor = (ScriptDescriptor) scriptCodeDescriptor.getContainingDeclaration();
Type scriptClassType = asmTypeForScriptDescriptor(bindingContext, scriptDescriptor); Type scriptClassType = asmTypeForScriptDescriptor(bindingContext, scriptDescriptor);
ValueParameterDescriptor valueParameterDescriptor = (ValueParameterDescriptor) descriptor; ValueParameterDescriptor valueParameterDescriptor = (ValueParameterDescriptor) descriptor;
ClassDescriptor scriptClass = bindingContext.get(CLASS_FOR_SCRIPT, scriptDescriptor); ClassDescriptor scriptClass = bindingContext.get(CLASS_FOR_SCRIPT, scriptDescriptor);
//noinspection ConstantConditions
StackValue script = StackValue.thisOrOuter(this, scriptClass, false, false); StackValue script = StackValue.thisOrOuter(this, scriptClass, false, false);
Type fieldType = typeMapper.mapType(valueParameterDescriptor); Type fieldType = typeMapper.mapType(valueParameterDescriptor);
return StackValue.field(fieldType, scriptClassType, valueParameterDescriptor.getName().getIdentifier(), false, script, return StackValue.field(fieldType, scriptClassType, valueParameterDescriptor.getName().getIdentifier(), false, script,
@@ -345,18 +345,7 @@ public class DescriptorResolver {
@NotNull @NotNull
public ValueParameterDescriptorImpl resolveValueParameterDescriptor( public ValueParameterDescriptorImpl resolveValueParameterDescriptor(
JetScope scope, DeclarationDescriptor declarationDescriptor, JetScope scope, FunctionDescriptor owner, JetParameter valueParameter, int index, JetType type, BindingTrace trace
JetParameter valueParameter, int index, JetType type, BindingTrace trace
) {
return resolveValueParameterDescriptor(declarationDescriptor, valueParameter, index, type, trace,
annotationResolver.resolveAnnotationsWithoutArguments(scope, valueParameter.getModifierList(), trace));
}
@NotNull
private ValueParameterDescriptorImpl resolveValueParameterDescriptor(
DeclarationDescriptor declarationDescriptor,
JetParameter valueParameter, int index, JetType type, BindingTrace trace,
Annotations annotations
) { ) {
JetType varargElementType = null; JetType varargElementType = null;
JetType variableType = type; JetType variableType = type;
@@ -365,10 +354,10 @@ public class DescriptorResolver {
variableType = getVarargParameterType(type); variableType = getVarargParameterType(type);
} }
ValueParameterDescriptorImpl valueParameterDescriptor = new ValueParameterDescriptorImpl( ValueParameterDescriptorImpl valueParameterDescriptor = new ValueParameterDescriptorImpl(
declarationDescriptor, owner,
null, null,
index, index,
annotations, annotationResolver.resolveAnnotationsWithoutArguments(scope, valueParameter.getModifierList(), trace),
JetPsiUtil.safeName(valueParameter.getName()), JetPsiUtil.safeName(valueParameter.getName()),
variableType, variableType,
valueParameter.hasDefaultValue(), valueParameter.hasDefaultValue(),
@@ -22,6 +22,7 @@ import org.jetbrains.kotlin.descriptors.ScriptDescriptor;
import org.jetbrains.kotlin.descriptors.SourceElement; import org.jetbrains.kotlin.descriptors.SourceElement;
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor; import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor;
import org.jetbrains.kotlin.descriptors.annotations.Annotations; import org.jetbrains.kotlin.descriptors.annotations.Annotations;
import org.jetbrains.kotlin.descriptors.impl.ScriptCodeDescriptor;
import org.jetbrains.kotlin.descriptors.impl.ValueParameterDescriptorImpl; import org.jetbrains.kotlin.descriptors.impl.ValueParameterDescriptorImpl;
import org.jetbrains.kotlin.parsing.JetScriptDefinition; import org.jetbrains.kotlin.parsing.JetScriptDefinition;
import org.jetbrains.kotlin.parsing.JetScriptDefinitionProvider; import org.jetbrains.kotlin.parsing.JetScriptDefinitionProvider;
@@ -35,7 +36,7 @@ public final class ScriptParameterResolver {
@NotNull @NotNull
public static List<ValueParameterDescriptor> resolveScriptParameters( public static List<ValueParameterDescriptor> resolveScriptParameters(
@NotNull JetScript declaration, @NotNull JetScript declaration,
@NotNull ScriptDescriptor scriptDescriptor @NotNull ScriptCodeDescriptor scriptCodeDescriptor
) { ) {
List<ValueParameterDescriptor> valueParameters = Lists.newArrayList(); List<ValueParameterDescriptor> valueParameters = Lists.newArrayList();
@@ -44,23 +45,16 @@ public final class ScriptParameterResolver {
int index = 0; int index = 0;
for (AnalyzerScriptParameter scriptParameter : scriptDefinition.getScriptParameters()) { for (AnalyzerScriptParameter scriptParameter : scriptDefinition.getScriptParameters()) {
ValueParameterDescriptor parameter = resolveScriptParameter(scriptParameter, index, scriptDescriptor); ValueParameterDescriptor parameter = new ValueParameterDescriptorImpl(
scriptCodeDescriptor, null, index, Annotations.EMPTY, scriptParameter.getName(),
scriptParameter.getType(), false, null, SourceElement.NO_SOURCE
);
valueParameters.add(parameter); valueParameters.add(parameter);
++index; ++index;
} }
return valueParameters; return valueParameters;
} }
@NotNull
private static ValueParameterDescriptor resolveScriptParameter(
@NotNull AnalyzerScriptParameter scriptParameter,
int index,
@NotNull ScriptDescriptor script
) {
return new ValueParameterDescriptorImpl(script, null, index, Annotations.EMPTY, scriptParameter.getName(),
scriptParameter.getType(), false, null, SourceElement.NO_SOURCE);
}
private ScriptParameterResolver() { private ScriptParameterResolver() {
} }
} }
@@ -148,8 +148,7 @@ object DynamicCallableDescriptors {
) )
} }
private fun createValueParameters(owner: DeclarationDescriptor, call: Call): List<ValueParameterDescriptor> { private fun createValueParameters(owner: FunctionDescriptor, call: Call): List<ValueParameterDescriptor> {
val parameters = ArrayList<ValueParameterDescriptor>() val parameters = ArrayList<ValueParameterDescriptor>()
fun addParameter(arg : ValueArgument, outType: JetType, varargElementType: JetType?) { fun addParameter(arg : ValueArgument, outType: JetType, varargElementType: JetType?) {
@@ -69,7 +69,7 @@ public class LazyScriptDescriptor(
val result = ScriptCodeDescriptor(this) val result = ScriptCodeDescriptor(this)
result.initialize( result.initialize(
implicitReceiver, implicitReceiver,
ScriptParameterResolver.resolveScriptParameters(jetScript, this), ScriptParameterResolver.resolveScriptParameters(jetScript, result),
DeferredType.create(resolveSession.getStorageManager(), resolveSession.getTrace()) { DeferredType.create(resolveSession.getStorageManager(), resolveSession.getTrace()) {
scriptBodyResolver.resolveScriptReturnType(jetScript, this, resolveSession.getTrace()) scriptBodyResolver.resolveScriptReturnType(jetScript, this, resolveSession.getTrace())
} }
@@ -16,8 +16,7 @@
package org.jetbrains.kotlin.load.java.descriptors package org.jetbrains.kotlin.load.java.descriptors
import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
import org.jetbrains.kotlin.descriptors.impl.ValueParameterDescriptorImpl import org.jetbrains.kotlin.descriptors.impl.ValueParameterDescriptorImpl
import org.jetbrains.kotlin.resolve.descriptorUtil.module import org.jetbrains.kotlin.resolve.descriptorUtil.module
@@ -26,7 +25,7 @@ import org.jetbrains.kotlin.types.JetType
fun createEnhancedValueParameters( fun createEnhancedValueParameters(
enhancedTypes: Collection<JetType>, enhancedTypes: Collection<JetType>,
oldValueParameters: Collection<ValueParameterDescriptor>, oldValueParameters: Collection<ValueParameterDescriptor>,
newOwner: DeclarationDescriptor newOwner: CallableDescriptor
): List<ValueParameterDescriptor> { ): List<ValueParameterDescriptor> {
assert(enhancedTypes.size() == oldValueParameters.size()) { assert(enhancedTypes.size() == oldValueParameters.size()) {
"Different value parameters sizes: Enhanced = ${enhancedTypes.size()}, Old = ${oldValueParameters.size()}" "Different value parameters sizes: Enhanced = ${enhancedTypes.size()}, Old = ${oldValueParameters.size()}"
@@ -24,6 +24,10 @@ import org.jetbrains.kotlin.types.JetType;
import java.util.Set; import java.util.Set;
public interface ValueParameterDescriptor extends VariableDescriptor, ParameterDescriptor { public interface ValueParameterDescriptor extends VariableDescriptor, ParameterDescriptor {
@NotNull
@Override
CallableDescriptor getContainingDeclaration();
/** /**
* Returns the 0-based index of the value parameter in the parameter list of its containing function. * Returns the 0-based index of the value parameter in the parameter list of its containing function.
* *
@@ -53,7 +57,7 @@ public interface ValueParameterDescriptor extends VariableDescriptor, ParameterD
ValueParameterDescriptor getOriginal(); ValueParameterDescriptor getOriginal();
@NotNull @NotNull
ValueParameterDescriptor copy(@NotNull DeclarationDescriptor newOwner, @NotNull Name newName); ValueParameterDescriptor copy(@NotNull CallableDescriptor newOwner, @NotNull Name newName);
/** /**
* Parameter p1 overrides p2 iff * Parameter p1 overrides p2 iff
@@ -39,7 +39,7 @@ public class ValueParameterDescriptorImpl extends VariableDescriptorImpl impleme
private final Set<? extends ValueParameterDescriptor> readOnlyOverriddenDescriptors = Collections.unmodifiableSet(overriddenDescriptors); private final Set<? extends ValueParameterDescriptor> readOnlyOverriddenDescriptors = Collections.unmodifiableSet(overriddenDescriptors);
public ValueParameterDescriptorImpl( public ValueParameterDescriptorImpl(
@NotNull DeclarationDescriptor containingDeclaration, @NotNull CallableDescriptor containingDeclaration,
@Nullable ValueParameterDescriptor original, @Nullable ValueParameterDescriptor original,
int index, int index,
@NotNull Annotations annotations, @NotNull Annotations annotations,
@@ -56,6 +56,12 @@ public class ValueParameterDescriptorImpl extends VariableDescriptorImpl impleme
this.varargElementType = varargElementType; this.varargElementType = varargElementType;
} }
@NotNull
@Override
public CallableDescriptor getContainingDeclaration() {
return (CallableDescriptor) super.getContainingDeclaration();
}
public void setType(@NotNull JetType type) { public void setType(@NotNull JetType type) {
setOutType(type); setOutType(type);
} }
@@ -124,8 +130,11 @@ public class ValueParameterDescriptorImpl extends VariableDescriptorImpl impleme
@NotNull @NotNull
@Override @Override
public ValueParameterDescriptor copy(@NotNull DeclarationDescriptor newOwner, @NotNull Name newName) { public ValueParameterDescriptor copy(@NotNull CallableDescriptor newOwner, @NotNull Name newName) {
return new ValueParameterDescriptorImpl(newOwner, null, index, getAnnotations(), newName, getType(), declaresDefaultValue(), varargElementType, SourceElement.NO_SOURCE); return new ValueParameterDescriptorImpl(
newOwner, null, index, getAnnotations(), newName, getType(), declaresDefaultValue(), varargElementType,
SourceElement.NO_SOURCE
);
} }
@NotNull @NotNull
@@ -170,11 +170,12 @@ public class MemberDeserializer(private val c: DeserializationContext) {
} }
private fun valueParameters(callable: Callable, kind: AnnotatedCallableKind): List<ValueParameterDescriptor> { private fun valueParameters(callable: Callable, kind: AnnotatedCallableKind): List<ValueParameterDescriptor> {
val containerOfCallable = c.containingDeclaration.getContainingDeclaration()!!.asProtoContainer() val callableDescriptor = c.containingDeclaration as CallableDescriptor
val containerOfCallable = callableDescriptor.getContainingDeclaration().asProtoContainer()
return callable.getValueParameterList().mapIndexed { i, proto -> return callable.getValueParameterList().mapIndexed { i, proto ->
ValueParameterDescriptorImpl( ValueParameterDescriptorImpl(
c.containingDeclaration, null, i, callableDescriptor, null, i,
getParameterAnnotations(containerOfCallable, callable, kind, proto), getParameterAnnotations(containerOfCallable, callable, kind, proto),
c.nameResolver.getName(proto.getName()), c.nameResolver.getName(proto.getName()),
c.typeDeserializer.type(proto.getType()), c.typeDeserializer.type(proto.getType()),