diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplInterpreter.java b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplInterpreter.java index 7e3f4ef9b1d..13395a98f97 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplInterpreter.java +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplInterpreter.java @@ -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) { diff --git a/compiler/frontend/src/org/jetbrains/kotlin/descriptors/impl/ScriptDescriptorImpl.java b/compiler/frontend/src/org/jetbrains/kotlin/descriptors/impl/ScriptDescriptorImpl.java deleted file mode 100644 index 319e3f0de2c..00000000000 --- a/compiler/frontend/src/org/jetbrains/kotlin/descriptors/impl/ScriptDescriptorImpl.java +++ /dev/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 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.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 properties, - @NotNull List 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.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 accept(DeclarationDescriptorVisitor visitor, D data) { - return visitor.visitScriptDescriptor(this, data); - } - - public void setValueParameters(@NotNull List 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 valueParameters - ) { - return ConstructorDescriptorImpl - .create(scriptDescriptor.getClassDescriptor(), Annotations.EMPTY, true, SourceElement.NO_SOURCE) - .initialize(Collections.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.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; - } -} diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/ScriptHeaderResolver.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/ScriptHeaderResolver.java deleted file mode 100644 index c7278c1e72d..00000000000 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/ScriptHeaderResolver.java +++ /dev/null @@ -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 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 e : c.getScripts().entrySet()) { - JetScript declaration = e.getKey(); - ScriptDescriptorImpl descriptor = (ScriptDescriptorImpl) e.getValue(); - - List valueParameters = ScriptParameterResolver.resolveScriptParameters( - declaration, - descriptor - ); - - descriptor.setValueParameters(valueParameters); - - WritableScope scope = descriptor.getScopeForBodyResolution(); - scope.setImplicitReceiver(descriptor.getThisAsReceiverParameter()); - for (ValueParameterDescriptor valueParameterDescriptor : valueParameters) { - scope.addVariableDescriptor(valueParameterDescriptor); - } - } - } - -} diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/ScriptPriorities.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/ScriptPriorities.java new file mode 100644 index 00000000000..d332e5c4065 --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/ScriptPriorities.java @@ -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 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; + } +} diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/ResolveSession.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/ResolveSession.java index d51bc99845d..af0408bb40f 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/ResolveSession.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/ResolveSession.java @@ -171,7 +171,7 @@ public class ResolveSession implements KotlinCodeAnalyzer, LazyClassContext { ResolveSession.this, scriptBodyResolver, script, - ScriptHeaderResolver.getScriptPriority(script) + ScriptPriorities.getScriptPriority(script) ); } } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/LazyScriptClassMemberScope.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/LazyScriptClassMemberScope.kt index 973170de54e..3970448fbe2 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/LazyScriptClassMemberScope.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/LazyScriptClassMemberScope.kt @@ -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 = resolveSession.getStorageManager().createLazyValue { val scriptInfo = declarationProvider.getOwnerInfo() as JetScriptInfo - ScriptDescriptorImpl.createScriptResultProperty(resolveSession.getScriptDescriptor(scriptInfo.script)) + createScriptResultProperty(resolveSession.getScriptDescriptor(scriptInfo.script)) } override fun computeExtraDescriptors(): Collection { @@ -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(), + scriptDescriptor.getThisAsReceiverParameter(), + ReceiverParameterDescriptor.NO_RECEIVER_PARAMETER + ) + propertyDescriptor.initialize(null, null) + + return propertyDescriptor + } + + private fun createConstructor(scriptDescriptor: ScriptDescriptor, valueParameters: List): 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 + } }