script: make top level declarations class members

This commit is contained in:
Stepan Koltsov
2012-06-05 22:56:26 +04:00
parent 877ecf83ad
commit f27147f752
21 changed files with 308 additions and 33 deletions
@@ -17,10 +17,14 @@
package org.jetbrains.jet.lang.descriptors;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
import org.jetbrains.jet.lang.types.JetType;
import java.util.Collections;
import java.util.List;
/**
* @author Stepan Koltsov
@@ -32,6 +36,13 @@ public class ScriptCodeDescriptor extends FunctionDescriptorImpl {
setVisibility(Visibilities.LOCAL);
}
public void initialize(
@NotNull ReceiverDescriptor expectedThisObject,
@NotNull List<ValueParameterDescriptor> valueParameters,
@NotNull JetType returnType) {
super.initialize(null, expectedThisObject, Collections.<TypeParameterDescriptor>emptyList(), valueParameters, returnType, Modality.FINAL, Visibilities.LOCAL);
}
@Override
protected FunctionDescriptorImpl createSubstitutedCopy(DeclarationDescriptor newOwner, boolean preserveOriginal, Kind kind) {
throw new IllegalStateException("no need to copy script code descriptor");
@@ -20,6 +20,8 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ScriptReceiver;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.TypeSubstitutor;
@@ -36,6 +38,7 @@ public class ScriptDescriptor extends DeclarationDescriptorImpl {
private List<ValueParameterDescriptor> valueParameters;
private final ScriptCodeDescriptor scriptCodeDescriptor = new ScriptCodeDescriptor(this);
private final ReceiverDescriptor implicitReceiver = new ScriptReceiver(this);
public ScriptDescriptor(@Nullable DeclarationDescriptor containingDeclaration) {
super(containingDeclaration, Collections.<AnnotationDescriptor>emptyList(), NAME);
@@ -44,6 +47,7 @@ public class ScriptDescriptor extends DeclarationDescriptorImpl {
public void initialize(@NotNull JetType returnType, @NotNull List<ValueParameterDescriptor> valueParameters) {
this.returnType = returnType;
this.valueParameters = valueParameters;
scriptCodeDescriptor.initialize(implicitReceiver, valueParameters, returnType);
}
@NotNull
@@ -61,6 +65,11 @@ public class ScriptDescriptor extends DeclarationDescriptorImpl {
return scriptCodeDescriptor;
}
@NotNull
public ReceiverDescriptor getImplicitReceiver() {
return implicitReceiver;
}
@Override
public DeclarationDescriptor substitute(TypeSubstitutor substitutor) {
throw new IllegalStateException("nothing to substitute in script");
@@ -71,4 +71,8 @@ public abstract class JetNamedDeclaration extends JetDeclaration implements PsiN
PsiElement identifier = getNameIdentifier();
return identifier != null ? identifier.getTextRange().getStartOffset() : getTextRange().getStartOffset();
}
public boolean isScriptDeclaration() {
return getParent() != null && getParent().getParent() instanceof JetScript;
}
}
@@ -41,6 +41,11 @@ public class JetScript extends JetDeclaration {
return PsiTreeUtil.getChildrenOfTypeAsList(this, JetImportDirective.class);
}
@NotNull
public List<JetDeclaration> getDeclarations() {
return PsiTreeUtil.getChildrenOfTypeAsList(getBlockExpression(), JetDeclaration.class);
}
@Override
public void accept(@NotNull JetVisitorVoid visitor) {
visitor.visitScript(this);
@@ -489,11 +489,32 @@ public class DescriptorResolver {
@NotNull
public VariableDescriptor resolveLocalVariableDescriptor(DeclarationDescriptor containingDeclaration, JetScope scope, JetProperty property, DataFlowInfo dataFlowInfo, BindingTrace trace) {
VariableDescriptorImpl variableDescriptor = resolveLocalVariableDescriptorWithType(containingDeclaration, property, null, trace);
if (property.isScriptDeclaration()) {
PropertyDescriptor propertyDescriptor = new PropertyDescriptor(
containingDeclaration,
annotationResolver.createAnnotationStubs(property.getModifierList(), trace),
Modality.FINAL,
Visibilities.INTERNAL,
property.isVar(),
false,
JetPsiUtil.safeName(property.getName()),
CallableMemberDescriptor.Kind.DECLARATION
);
JetType type = getVariableType(scope, property, dataFlowInfo, false, trace); // For a local variable the type must not be deferred
variableDescriptor.setOutType(type);
return variableDescriptor;
JetType type = getVariableType(scope, property, dataFlowInfo, false, trace); // For a local variable the type must not be deferred
propertyDescriptor.setType(type, Collections.<TypeParameterDescriptor>emptyList(), scope.getImplicitReceiver(), (JetType) null);
trace.record(BindingContext.VARIABLE, property, propertyDescriptor);
return propertyDescriptor;
}
else {
VariableDescriptorImpl variableDescriptor = resolveLocalVariableDescriptorWithType(containingDeclaration, property, null, trace);
JetType type = getVariableType(scope, property, dataFlowInfo, false, trace); // For a local variable the type must not be deferred
variableDescriptor.setOutType(type);
return variableDescriptor;
}
}
@NotNull
@@ -108,6 +108,10 @@ public class DescriptorUtils {
ClassDescriptor classDescriptor = (ClassDescriptor) containingDeclaration;
return classDescriptor.getImplicitReceiver();
}
else if (containingDeclaration instanceof ScriptDescriptor) {
ScriptDescriptor scriptDescriptor = (ScriptDescriptor) containingDeclaration;
return scriptDescriptor.getImplicitReceiver();
}
return NO_RECEIVER;
}
@@ -29,6 +29,7 @@ import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.resolve.scopes.RedeclarationHandler;
import org.jetbrains.jet.lang.resolve.scopes.WritableScope;
import org.jetbrains.jet.lang.resolve.scopes.WritableScopeImpl;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ScriptReceiver;
import org.jetbrains.jet.lang.types.DependencyClassByQualifiedNameResolver;
import org.jetbrains.jet.lang.types.ErrorUtils;
import org.jetbrains.jet.lang.types.JetType;
@@ -155,6 +156,8 @@ public class ScriptResolver {
List<ValueParameterDescriptor> valueParameters = Lists.newArrayList();
scope.setImplicitReceiver(descriptor.getImplicitReceiver());
int index = 0;
for (AnalyzerScriptParameter scriptParameter : topDownAnalysisParameters.getScriptParameters()) {
ValueParameterDescriptor parameter = resolveScriptParameter(scriptParameter, index, descriptor);
@@ -61,6 +61,11 @@ public class AutoCastUtils {
return castThis(dataFlowInfo, receiver);
}
@Override
public List<ReceiverDescriptor> visitScriptReceiver(ScriptReceiver receiver, Object data) {
return Collections.emptyList();
}
@Override
public List<ReceiverDescriptor> visitExpressionReceiver(ExpressionReceiver receiver, Object data) {
// JetExpression expression = receiver.getExpression();
@@ -39,4 +39,8 @@ public class ReceiverDescriptorVisitor<R, D> {
public R visitClassReceiver(ClassReceiver receiver, D data) {
return null;
}
public R visitScriptReceiver(ScriptReceiver receiver, D data) {
return null;
}
}
@@ -0,0 +1,60 @@
/*
* 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.resolve.scopes.receivers;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.descriptors.ScriptDescriptor;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.lang.JetStandardClasses;
/**
* @author Stepan Koltsov
*/
public class ScriptReceiver implements ThisReceiverDescriptor {
@NotNull
private final ScriptDescriptor scriptDescriptor;
public ScriptReceiver(@NotNull ScriptDescriptor scriptDescriptor) {
this.scriptDescriptor = scriptDescriptor;
}
@NotNull
@Override
public DeclarationDescriptor getDeclarationDescriptor() {
return scriptDescriptor;
}
@NotNull
@Override
public JetType getType() {
// not sure
return JetStandardClasses.getAnyType();
}
@Override
public boolean exists() {
return true;
}
@Override
public <R, D> R accept(@NotNull ReceiverDescriptorVisitor<R, D> visitor, D data) {
return visitor.visitScriptReceiver(this, data);
}
}