Script refactoring, frontend: Treat scripts as classes (as opposed to function bodies)

ScriptDescriptor implements ClassDescriptor
Scripts are accessible in IDE via stub index
Replace special treatment of scripts with generic mechanism in several cases
Scripts to longer generate 'rv' property for storing result value (changes in repl required)
This commit is contained in:
Pavel V. Talanov
2015-11-17 15:09:01 +03:00
parent aebcebe8ca
commit efa4bf1d74
27 changed files with 198 additions and 580 deletions
@@ -17,24 +17,11 @@
package org.jetbrains.kotlin.descriptors;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.descriptors.impl.ScriptCodeDescriptor;
import org.jetbrains.kotlin.name.Name;
public interface ScriptDescriptor extends DeclarationDescriptorNonRoot {
String LAST_EXPRESSION_VALUE_FIELD_NAME = "rv";
Name NAME = Name.special("<script>");
public interface ScriptDescriptor extends ClassDescriptor {
int getPriority();
@NotNull
ScriptCodeDescriptor getScriptCodeDescriptor();
@NotNull
ReceiverParameterDescriptor getThisAsReceiverParameter();
@NotNull
ClassDescriptor getClassDescriptor();
@NotNull
PropertyDescriptor getScriptResultProperty();
@Override
ConstructorDescriptor getUnsubstitutedPrimaryConstructor();
}
@@ -65,7 +65,7 @@ public class DeclarationDescriptorVisitorEmptyBodies<R, D> implements Declaratio
@Override
public R visitScriptDescriptor(ScriptDescriptor scriptDescriptor, D data) {
return visitDeclarationDescriptor(scriptDescriptor, data);
return visitClassDescriptor(scriptDescriptor, data);
}
@Override
@@ -1,75 +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.annotations.Nullable;
import org.jetbrains.kotlin.descriptors.*;
import org.jetbrains.kotlin.descriptors.annotations.Annotations;
import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.types.KotlinType;
import java.util.Collections;
import java.util.List;
public class ScriptCodeDescriptor extends FunctionDescriptorImpl {
public ScriptCodeDescriptor(@NotNull ScriptDescriptor containingDeclaration) {
super(containingDeclaration, null, Annotations.Companion.getEMPTY(), Name.special("<script-code>"), Kind.DECLARATION, SourceElement.NO_SOURCE);
}
public void initialize(
@NotNull ReceiverParameterDescriptor dispatchReceiverParameter,
@NotNull List<ValueParameterDescriptor> valueParameters,
@NotNull KotlinType returnType) {
super.initialize(null, dispatchReceiverParameter, Collections.<TypeParameterDescriptor>emptyList(), valueParameters, returnType,
Modality.FINAL, Visibilities.INTERNAL);
}
@NotNull
@Override
protected FunctionDescriptorImpl createSubstitutedCopy(
@NotNull DeclarationDescriptor newOwner,
@Nullable FunctionDescriptor original,
@NotNull Kind kind,
@Nullable Name newName,
boolean preserveSource
) {
throw new IllegalStateException("no need to copy script code descriptor");
}
@NotNull
@Override
public FunctionDescriptor copy(DeclarationDescriptor newOwner, Modality modality, Visibility visibility, Kind kind, boolean copyOverrides) {
throw new IllegalStateException("no need to copy script code descriptor");
}
@Override
public boolean isExternal() {
return false;
}
@Override
public boolean isInline() {
return false;
}
@Override
public boolean isTailrec() {
return false;
}
}
@@ -872,10 +872,6 @@ internal class DescriptorRendererImpl(
/* OTHER */
private fun renderModuleOrScript(moduleOrScript: DeclarationDescriptor, builder: StringBuilder) {
renderName(moduleOrScript, builder)
}
private fun renderPackageView(packageView: PackageViewDescriptor, builder: StringBuilder) {
builder.append(renderKeyword("package")).append(" ")
builder.append(renderFqName(packageView.fqName.toUnsafe()))
@@ -962,11 +958,11 @@ internal class DescriptorRendererImpl(
}
override fun visitModuleDeclaration(descriptor: ModuleDescriptor, builder: StringBuilder) {
renderModuleOrScript(descriptor, builder)
renderName(descriptor, builder)
}
override fun visitScriptDescriptor(scriptDescriptor: ScriptDescriptor, builder: StringBuilder) {
renderModuleOrScript(scriptDescriptor, builder)
visitClassDescriptor(scriptDescriptor, builder)
}
override fun visitClassDescriptor(descriptor: ClassDescriptor, builder: StringBuilder) {