diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/ScriptCodeDescriptor.java b/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/ScriptCodeDescriptor.java new file mode 100644 index 00000000000..9d374209cac --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/ScriptCodeDescriptor.java @@ -0,0 +1,45 @@ +/* + * 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.lang.descriptors; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor; +import org.jetbrains.jet.lang.resolve.name.Name; + +import java.util.Collections; + +/** + * @author Stepan Koltsov + */ +public class ScriptCodeDescriptor extends FunctionDescriptorImpl { + + public ScriptCodeDescriptor(@NotNull ScriptDescriptor containingDeclaration) { + super(containingDeclaration, Collections.emptyList(), Name.special(""), Kind.DECLARATION); + setVisibility(Visibilities.LOCAL); + } + + @Override + protected FunctionDescriptorImpl createSubstitutedCopy(DeclarationDescriptor newOwner, boolean preserveOriginal, Kind kind) { + throw new IllegalStateException("no need to copy script code descriptor"); + } + + @NotNull + @Override + public FunctionDescriptor copy(DeclarationDescriptor newOwner, boolean makeNonAbstract, boolean makeInvisible, Kind kind, boolean copyOverrides) { + throw new IllegalStateException("no need to copy script code descriptor"); + } +} diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/ScriptDescriptor.java b/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/ScriptDescriptor.java index 15b7abd2419..d468145bb89 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/ScriptDescriptor.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/descriptors/ScriptDescriptor.java @@ -35,6 +35,8 @@ public class ScriptDescriptor extends DeclarationDescriptorImpl { private JetType returnType; private List valueParameters; + private final ScriptCodeDescriptor scriptCodeDescriptor = new ScriptCodeDescriptor(this); + public ScriptDescriptor(@Nullable DeclarationDescriptor containingDeclaration) { super(containingDeclaration, Collections.emptyList(), NAME); } @@ -54,6 +56,11 @@ public class ScriptDescriptor extends DeclarationDescriptorImpl { return valueParameters; } + @NotNull + public ScriptCodeDescriptor getScriptCodeDescriptor() { + return scriptCodeDescriptor; + } + @Override public DeclarationDescriptor substitute(TypeSubstitutor substitutor) { throw new IllegalStateException("nothing to substitute in script"); diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/ExpressionTypingServices.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/ExpressionTypingServices.java index b4254229bc2..582247093d4 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/ExpressionTypingServices.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/ExpressionTypingServices.java @@ -25,6 +25,7 @@ import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; import org.jetbrains.jet.lang.descriptors.FunctionDescriptor; import org.jetbrains.jet.lang.descriptors.FunctionDescriptorUtil; +import org.jetbrains.jet.lang.descriptors.ScriptDescriptor; import org.jetbrains.jet.lang.descriptors.VariableDescriptor; import org.jetbrains.jet.lang.diagnostics.Diagnostic; import org.jetbrains.jet.lang.psi.*; @@ -193,6 +194,13 @@ public class ExpressionTypingServices { } DeclarationDescriptor containingDescriptor = outerScope.getContainingDeclaration(); + if (containingDescriptor instanceof ScriptDescriptor) { + if (!(expression.getParent() instanceof JetScript)) { + // top level script declarations should have ScriptDescriptor parent + // and lower level script declarations should be ScriptCodeDescriptor parent + containingDescriptor = ((ScriptDescriptor) containingDescriptor).getScriptCodeDescriptor(); + } + } WritableScope scope = new WritableScopeImpl(outerScope, containingDescriptor, new TraceBasedRedeclarationHandler(context.trace)).setDebugName("getBlockReturnedType"); scope.changeLockLevel(WritableScope.LockLevel.BOTH); return getBlockReturnedTypeWithWritableScope(scope, block, coercionStrategyForLastExpression, context, trace);