ScriptDescriptorImpl and related non-lazy code removed

This commit is contained in:
Andrey Breslav
2015-01-23 20:04:58 +03:00
parent 7d1a826778
commit 32a3b58f14
6 changed files with 102 additions and 328 deletions
@@ -275,7 +275,7 @@ public class ReplInterpreter {
trace.clearDiagnostics();
//noinspection ConstantConditions
psiFile.getScript().putUserData(ScriptHeaderResolver.PRIORITY_KEY, lineNumber);
psiFile.getScript().putUserData(ScriptPriorities.PRIORITY_KEY, lineNumber);
ScriptDescriptor scriptDescriptor = doAnalyze(psiFile, errorCollector);
if (scriptDescriptor == null) {
@@ -1,214 +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.descriptors.impl;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
import org.jetbrains.kotlin.descriptors.*;
import org.jetbrains.kotlin.descriptors.annotations.Annotations;
import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.resolve.scopes.JetScope;
import org.jetbrains.kotlin.resolve.scopes.RedeclarationHandler;
import org.jetbrains.kotlin.resolve.scopes.WritableScope;
import org.jetbrains.kotlin.resolve.scopes.WritableScopeImpl;
import org.jetbrains.kotlin.resolve.scopes.receivers.ScriptReceiver;
import org.jetbrains.kotlin.types.JetType;
import org.jetbrains.kotlin.types.TypeSubstitutor;
import java.util.Collections;
import java.util.List;
// SCRIPT: Script declaration descriptor
public class ScriptDescriptorImpl extends DeclarationDescriptorNonRootImpl implements ScriptDescriptor {
private final int priority;
private List<ValueParameterDescriptor> valueParameters;
private final ScriptCodeDescriptor scriptCodeDescriptor = new ScriptCodeDescriptor(this);
private final ReceiverParameterDescriptor implicitReceiver = new ReceiverParameterDescriptorImpl(this,
// Putting Any here makes no sense,
// it is simply copied from someplace else
// during a refactoring
KotlinBuiltIns.getInstance().getAnyType(),
new ScriptReceiver(this));
private final MutableClassDescriptor classDescriptor;
private final WritableScopeImpl classScope;
private WritableScope scopeForBodyResolution;
private PropertyDescriptor scriptResultProperty;
public ScriptDescriptorImpl(
@NotNull DeclarationDescriptor containingDeclaration,
int priority,
@NotNull JetScope scriptScope,
@NotNull Name className,
@NotNull SourceElement source
) {
super(containingDeclaration, Annotations.EMPTY, NAME, source);
this.priority = priority;
classDescriptor = new MutableClassDescriptor(containingDeclaration, scriptScope, ClassKind.CLASS,
false, className, SourceElement.NO_SOURCE);
classDescriptor.addSupertype(KotlinBuiltIns.getInstance().getAnyType());
classDescriptor.setModality(Modality.FINAL);
classDescriptor.setVisibility(Visibilities.PUBLIC);
classDescriptor.setTypeParameterDescriptors(Collections.<TypeParameterDescriptor>emptyList());
classScope = new WritableScopeImpl(JetScope.Empty.INSTANCE$, classDescriptor, RedeclarationHandler.DO_NOTHING, "script members");
classScope.changeLockLevel(WritableScope.LockLevel.BOTH);
classDescriptor.setScopeForMemberLookup(classScope);
classDescriptor.createTypeConstructor();
}
public void initialize(
@NotNull JetType returnType,
@NotNull List<? extends PropertyDescriptorImpl> properties,
@NotNull List<? extends FunctionDescriptor> functions
) {
assert valueParameters != null : "setValueParameters() must be called before this method";
scriptCodeDescriptor.initialize(implicitReceiver, valueParameters, returnType);
scriptResultProperty = createScriptResultProperty(this);
classScope.addPropertyDescriptor(scriptResultProperty);
for (PropertyDescriptorImpl property : properties) {
classScope.addPropertyDescriptor(property);
}
for (FunctionDescriptor function : functions) {
classScope.addFunctionDescriptor(function);
}
}
@NotNull
public static PropertyDescriptor createScriptResultProperty(@NotNull ScriptDescriptor scriptDescriptor) {
PropertyDescriptorImpl propertyDescriptor = PropertyDescriptorImpl.create(scriptDescriptor.getClassDescriptor(),
Annotations.EMPTY,
Modality.FINAL,
Visibilities.PUBLIC,
false,
Name.identifier(LAST_EXPRESSION_VALUE_FIELD_NAME),
CallableMemberDescriptor.Kind.DECLARATION,
SourceElement.NO_SOURCE);
JetType returnType = scriptDescriptor.getScriptCodeDescriptor().getReturnType();
assert returnType != null : "Return type not initialized for " + scriptDescriptor;
propertyDescriptor.setType(
returnType,
Collections.<TypeParameterDescriptor>emptyList(),
scriptDescriptor.getThisAsReceiverParameter(),
ReceiverParameterDescriptor.NO_RECEIVER_PARAMETER);
propertyDescriptor.initialize(null, null);
return propertyDescriptor;
}
@NotNull
@Override
public PropertyDescriptor getScriptResultProperty() {
return scriptResultProperty;
}
@Override
public int getPriority() {
return priority;
}
@Override
@NotNull
public ScriptCodeDescriptor getScriptCodeDescriptor() {
return scriptCodeDescriptor;
}
@Override
@NotNull
public ReceiverParameterDescriptor getThisAsReceiverParameter() {
return implicitReceiver;
}
@Override
public DeclarationDescriptor substitute(@NotNull TypeSubstitutor substitutor) {
throw new IllegalStateException("nothing to substitute in script");
}
@Override
public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) {
return visitor.visitScriptDescriptor(this, data);
}
public void setValueParameters(@NotNull List<ValueParameterDescriptor> valueParameters) {
this.valueParameters = valueParameters;
ConstructorDescriptorImpl constructorDescriptor = createConstructor(this, valueParameters);
constructorDescriptor.setReturnType(classDescriptor.getDefaultType());
classDescriptor.getConstructors().add(constructorDescriptor);
classDescriptor.setPrimaryConstructor(constructorDescriptor);
for (ValueParameterDescriptor valueParameter : valueParameters) {
classScope.addPropertyDescriptor(createPropertyFromScriptParameter(this, valueParameter));
}
}
@NotNull
public static ConstructorDescriptorImpl createConstructor(
@NotNull ScriptDescriptor scriptDescriptor, @NotNull List<ValueParameterDescriptor> valueParameters
) {
return ConstructorDescriptorImpl
.create(scriptDescriptor.getClassDescriptor(), Annotations.EMPTY, true, SourceElement.NO_SOURCE)
.initialize(Collections.<TypeParameterDescriptor>emptyList(), valueParameters, Visibilities.PUBLIC);
}
@NotNull
public static PropertyDescriptor createPropertyFromScriptParameter(
@NotNull ScriptDescriptor scriptDescriptor,
@NotNull ValueParameterDescriptor parameter
) {
PropertyDescriptorImpl propertyDescriptor = PropertyDescriptorImpl.create(
scriptDescriptor.getClassDescriptor(),
Annotations.EMPTY,
Modality.FINAL,
Visibilities.PUBLIC,
false,
parameter.getName(),
CallableMemberDescriptor.Kind.DECLARATION,
SourceElement.NO_SOURCE
);
propertyDescriptor.setType(
parameter.getType(),
Collections.<TypeParameterDescriptor>emptyList(),
scriptDescriptor.getThisAsReceiverParameter(), ReceiverParameterDescriptor.NO_RECEIVER_PARAMETER);
propertyDescriptor.initialize(null, null);
return propertyDescriptor;
}
@Override
@NotNull
public ClassDescriptor getClassDescriptor() {
return classDescriptor;
}
@Override
@NotNull
public WritableScope getScopeForBodyResolution() {
return scopeForBodyResolution;
}
public void setScopeForBodyResolution(@NotNull WritableScope scopeForBodyResolution) {
assert this.scopeForBodyResolution == null : "Scope for body resolution already set for " + this;
this.scopeForBodyResolution = scopeForBodyResolution;
}
}
@@ -1,108 +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.resolve;
import com.intellij.openapi.util.Key;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor;
import org.jetbrains.kotlin.descriptors.ScriptDescriptor;
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor;
import org.jetbrains.kotlin.descriptors.impl.ScriptDescriptorImpl;
import org.jetbrains.kotlin.name.FqName;
import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.psi.JetFile;
import org.jetbrains.kotlin.psi.JetScript;
import org.jetbrains.kotlin.resolve.scopes.RedeclarationHandler;
import org.jetbrains.kotlin.resolve.scopes.WritableScope;
import org.jetbrains.kotlin.resolve.scopes.WritableScopeImpl;
import javax.inject.Inject;
import java.util.List;
import java.util.Map;
import static org.jetbrains.kotlin.resolve.source.SourcePackage.toSourceElement;
// SCRIPT: Resolve declarations in scripts
public class ScriptHeaderResolver {
public static final Key<Integer> PRIORITY_KEY = Key.create(JetScript.class.getName() + ".priority");
@NotNull
private MutablePackageFragmentProvider packageFragmentProvider;
@NotNull
private BindingTrace trace;
@Inject
public void setPackageFragmentProvider(@NotNull MutablePackageFragmentProvider packageFragmentProvider) {
this.packageFragmentProvider = packageFragmentProvider;
}
@Inject
public void setTrace(@NotNull BindingTrace trace) {
this.trace = trace;
}
public void processScriptHierarchy(@NotNull TopDownAnalysisContext c, @NotNull JetScript script, @NotNull WritableScope outerScope) {
JetFile file = script.getContainingJetFile();
FqName fqName = file.getPackageFqName();
PackageFragmentDescriptor ns = packageFragmentProvider.getOrCreateFragment(fqName);
Integer priority = getScriptPriority(script);
FqName nameForScript = ScriptNameUtil.classNameForScript(script);
Name className = nameForScript.shortName();
ScriptDescriptorImpl scriptDescriptor = new ScriptDescriptorImpl(ns, priority, outerScope, className, toSourceElement(script));
WritableScopeImpl scriptScope = new WritableScopeImpl(outerScope, scriptDescriptor, RedeclarationHandler.DO_NOTHING, "script");
scriptScope.changeLockLevel(WritableScope.LockLevel.BOTH);
scriptDescriptor.setScopeForBodyResolution(scriptScope);
c.getScripts().put(script, scriptDescriptor);
trace.record(BindingContext.SCRIPT, script, scriptDescriptor);
outerScope.addClassifierDescriptor(scriptDescriptor.getClassDescriptor());
}
public static int getScriptPriority(@NotNull JetScript script) {
Integer priority = script.getUserData(PRIORITY_KEY);
return priority == null ? 0 : priority;
}
public static void resolveScriptDeclarations(@NotNull TopDownAnalysisContext c) {
for (Map.Entry<JetScript, ScriptDescriptor> e : c.getScripts().entrySet()) {
JetScript declaration = e.getKey();
ScriptDescriptorImpl descriptor = (ScriptDescriptorImpl) e.getValue();
List<ValueParameterDescriptor> valueParameters = ScriptParameterResolver.resolveScriptParameters(
declaration,
descriptor
);
descriptor.setValueParameters(valueParameters);
WritableScope scope = descriptor.getScopeForBodyResolution();
scope.setImplicitReceiver(descriptor.getThisAsReceiverParameter());
for (ValueParameterDescriptor valueParameterDescriptor : valueParameters) {
scope.addVariableDescriptor(valueParameterDescriptor);
}
}
}
}
@@ -0,0 +1,32 @@
/*
* 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.resolve;
import com.intellij.openapi.util.Key;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.psi.JetScript;
// SCRIPT: Resolve declarations in scripts
public class ScriptPriorities {
public static final Key<Integer> PRIORITY_KEY = Key.create(JetScript.class.getName() + ".priority");
public static int getScriptPriority(@NotNull JetScript script) {
Integer priority = script.getUserData(PRIORITY_KEY);
return priority == null ? 0 : priority;
}
}
@@ -171,7 +171,7 @@ public class ResolveSession implements KotlinCodeAnalyzer, LazyClassContext {
ResolveSession.this,
scriptBodyResolver,
script,
ScriptHeaderResolver.getScriptPriority(script)
ScriptPriorities.getScriptPriority(script)
);
}
}
@@ -17,7 +17,6 @@
package org.jetbrains.kotlin.resolve.lazy.descriptors
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.impl.ScriptDescriptorImpl
import org.jetbrains.kotlin.resolve.BindingTrace
import org.jetbrains.kotlin.resolve.lazy.ResolveSession
import org.jetbrains.kotlin.resolve.lazy.data.JetScriptInfo
@@ -25,6 +24,11 @@ import org.jetbrains.kotlin.resolve.lazy.declarations.ClassMemberDeclarationProv
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.storage.NotNullLazyValue
import org.jetbrains.kotlin.utils.toReadOnlyList
import org.jetbrains.kotlin.descriptors.impl.PropertyDescriptorImpl
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.types.JetType
import java.util.Collections
import org.jetbrains.kotlin.descriptors.impl.ConstructorDescriptorImpl
// SCRIPT: Members of a script class
public class LazyScriptClassMemberScope protected(
@@ -36,7 +40,7 @@ public class LazyScriptClassMemberScope protected(
private val scriptResultProperty: NotNullLazyValue<PropertyDescriptor> = resolveSession.getStorageManager().createLazyValue {
val scriptInfo = declarationProvider.getOwnerInfo() as JetScriptInfo
ScriptDescriptorImpl.createScriptResultProperty(resolveSession.getScriptDescriptor(scriptInfo.script))
createScriptResultProperty(resolveSession.getScriptDescriptor(scriptInfo.script))
}
override fun computeExtraDescriptors(): Collection<DeclarationDescriptor> {
@@ -66,7 +70,7 @@ public class LazyScriptClassMemberScope protected(
for (valueParameterDescriptor in primaryConstructor.getValueParameters()) {
if (name == valueParameterDescriptor.getName()) {
result.add(ScriptDescriptorImpl.createPropertyFromScriptParameter(resolveSession.getScriptDescriptor(scriptInfo.script), valueParameterDescriptor))
result.add(createPropertyFromScriptParameter(resolveSession.getScriptDescriptor(scriptInfo.script), valueParameterDescriptor))
}
}
}
@@ -74,8 +78,68 @@ public class LazyScriptClassMemberScope protected(
override fun resolvePrimaryConstructor(): ConstructorDescriptor? {
val scriptInfo = declarationProvider.getOwnerInfo() as JetScriptInfo
val scriptDescriptor = resolveSession.getScriptDescriptor(scriptInfo.script)
val constructor = ScriptDescriptorImpl.createConstructor(scriptDescriptor, scriptDescriptor.getScriptCodeDescriptor().getValueParameters())
val constructor = createConstructor(scriptDescriptor, scriptDescriptor.getScriptCodeDescriptor().getValueParameters())
setDeferredReturnType(constructor)
return constructor
}
private fun createScriptResultProperty(scriptDescriptor: ScriptDescriptor): PropertyDescriptor {
val propertyDescriptor = PropertyDescriptorImpl.create(
scriptDescriptor.getClassDescriptor(),
Annotations.EMPTY,
Modality.FINAL,
Visibilities.PUBLIC,
false,
Name.identifier(ScriptDescriptor.LAST_EXPRESSION_VALUE_FIELD_NAME),
CallableMemberDescriptor.Kind.DECLARATION,
SourceElement.NO_SOURCE
)
val returnType = scriptDescriptor.getScriptCodeDescriptor().getReturnType()
assert(returnType != null) { "Return type not initialized for " + scriptDescriptor }
propertyDescriptor.setType(
returnType,
listOf<TypeParameterDescriptor>(),
scriptDescriptor.getThisAsReceiverParameter(),
ReceiverParameterDescriptor.NO_RECEIVER_PARAMETER
)
propertyDescriptor.initialize(null, null)
return propertyDescriptor
}
private fun createConstructor(scriptDescriptor: ScriptDescriptor, valueParameters: List<ValueParameterDescriptor>): ConstructorDescriptorImpl {
return ConstructorDescriptorImpl.create(
scriptDescriptor.getClassDescriptor(),
Annotations.EMPTY,
true,
SourceElement.NO_SOURCE
).initialize(
listOf(),
valueParameters,
Visibilities.PUBLIC
)
}
private fun createPropertyFromScriptParameter(scriptDescriptor: ScriptDescriptor, parameter: ValueParameterDescriptor): PropertyDescriptor {
val propertyDescriptor = PropertyDescriptorImpl.create(
scriptDescriptor.getClassDescriptor(),
Annotations.EMPTY,
Modality.FINAL,
Visibilities.PUBLIC,
false,
parameter.getName(),
CallableMemberDescriptor.Kind.DECLARATION,
SourceElement.NO_SOURCE
)
propertyDescriptor.setType(
parameter.getType(),
listOf(),
scriptDescriptor.getThisAsReceiverParameter(),
ReceiverParameterDescriptor.NO_RECEIVER_PARAMETER
)
propertyDescriptor.initialize(null, null)
return propertyDescriptor
}
}