001 /*
002 * Copyright 2010-2013 JetBrains s.r.o.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.jetbrains.jet.lang.descriptors;
018
019 import org.jetbrains.annotations.NotNull;
020 import org.jetbrains.jet.lang.descriptors.annotations.Annotations;
021 import org.jetbrains.jet.lang.descriptors.impl.*;
022 import org.jetbrains.jet.lang.resolve.name.Name;
023 import org.jetbrains.jet.lang.resolve.scopes.JetScope;
024 import org.jetbrains.jet.lang.resolve.scopes.RedeclarationHandler;
025 import org.jetbrains.jet.lang.resolve.scopes.WritableScope;
026 import org.jetbrains.jet.lang.resolve.scopes.WritableScopeImpl;
027 import org.jetbrains.jet.lang.resolve.scopes.receivers.ScriptReceiver;
028 import org.jetbrains.jet.lang.types.JetType;
029 import org.jetbrains.jet.lang.types.TypeSubstitutor;
030 import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
031
032 import java.util.Collections;
033 import java.util.List;
034
035 // SCRIPT: Script declaration descriptor
036 public class ScriptDescriptorImpl extends DeclarationDescriptorNonRootImpl implements ScriptDescriptor {
037
038 private final int priority;
039
040 private List<ValueParameterDescriptor> valueParameters;
041
042 private final ScriptCodeDescriptor scriptCodeDescriptor = new ScriptCodeDescriptor(this);
043 private final ReceiverParameterDescriptor implicitReceiver = new ReceiverParameterDescriptorImpl(this,
044 // Putting Any here makes no sense,
045 // it is simply copied from someplace else
046 // during a refactoring
047 KotlinBuiltIns.getInstance().getAnyType(),
048 new ScriptReceiver(this));
049
050 private final MutableClassDescriptor classDescriptor;
051
052 private final WritableScopeImpl classScope;
053 private WritableScope scopeForBodyResolution;
054 private PropertyDescriptor scriptResultProperty;
055
056 public ScriptDescriptorImpl(
057 @NotNull DeclarationDescriptor containingDeclaration,
058 int priority,
059 @NotNull JetScope scriptScope,
060 @NotNull Name className
061 ) {
062 super(containingDeclaration, Annotations.EMPTY, NAME);
063 this.priority = priority;
064
065 classDescriptor = new MutableClassDescriptor(containingDeclaration, scriptScope, ClassKind.CLASS, false, className);
066 classDescriptor.addSupertype(KotlinBuiltIns.getInstance().getAnyType());
067 classDescriptor.setModality(Modality.FINAL);
068 classDescriptor.setVisibility(Visibilities.PUBLIC);
069 classDescriptor.setTypeParameterDescriptors(Collections.<TypeParameterDescriptor>emptyList());
070
071 classScope = new WritableScopeImpl(JetScope.EMPTY, classDescriptor, RedeclarationHandler.DO_NOTHING, "script members");
072 classScope.changeLockLevel(WritableScope.LockLevel.BOTH);
073 classDescriptor.setScopeForMemberLookup(classScope);
074 classDescriptor.createTypeConstructor();
075 }
076
077 public void initialize(
078 @NotNull JetType returnType,
079 @NotNull List<? extends PropertyDescriptorImpl> properties,
080 @NotNull List<? extends FunctionDescriptor> functions
081 ) {
082 assert valueParameters != null : "setValueParameters() must be called before this method";
083 scriptCodeDescriptor.initialize(implicitReceiver, valueParameters, returnType);
084
085 scriptResultProperty = createScriptResultProperty(this);
086 classScope.addPropertyDescriptor(scriptResultProperty);
087
088 for (PropertyDescriptorImpl property : properties) {
089 classScope.addPropertyDescriptor(property);
090 }
091
092 for (FunctionDescriptor function : functions) {
093 classScope.addFunctionDescriptor(function);
094 }
095 }
096
097 @NotNull
098 public static PropertyDescriptor createScriptResultProperty(@NotNull ScriptDescriptor scriptDescriptor) {
099 PropertyDescriptorImpl propertyDescriptor = PropertyDescriptorImpl.create(scriptDescriptor.getClassDescriptor(),
100 Annotations.EMPTY,
101 Modality.FINAL,
102 Visibilities.PUBLIC,
103 false,
104 Name.identifier(LAST_EXPRESSION_VALUE_FIELD_NAME),
105 CallableMemberDescriptor.Kind.DECLARATION);
106 JetType returnType = scriptDescriptor.getScriptCodeDescriptor().getReturnType();
107 assert returnType != null : "Return type not initialized for " + scriptDescriptor;
108 propertyDescriptor.setType(
109 returnType,
110 Collections.<TypeParameterDescriptor>emptyList(),
111 scriptDescriptor.getThisAsReceiverParameter(),
112 ReceiverParameterDescriptor.NO_RECEIVER_PARAMETER);
113 propertyDescriptor.initialize(null, null);
114 return propertyDescriptor;
115 }
116
117 @NotNull
118 @Override
119 public PropertyDescriptor getScriptResultProperty() {
120 return scriptResultProperty;
121 }
122
123 @Override
124 public int getPriority() {
125 return priority;
126 }
127
128 @Override
129 @NotNull
130 public ScriptCodeDescriptor getScriptCodeDescriptor() {
131 return scriptCodeDescriptor;
132 }
133
134 @Override
135 @NotNull
136 public ReceiverParameterDescriptor getThisAsReceiverParameter() {
137 return implicitReceiver;
138 }
139
140 @Override
141 public DeclarationDescriptor substitute(@NotNull TypeSubstitutor substitutor) {
142 throw new IllegalStateException("nothing to substitute in script");
143 }
144
145 @Override
146 public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) {
147 return visitor.visitScriptDescriptor(this, data);
148 }
149
150 public void setValueParameters(@NotNull List<ValueParameterDescriptor> valueParameters) {
151 this.valueParameters = valueParameters;
152
153 ConstructorDescriptorImpl constructorDescriptor = createConstructor(this, valueParameters);
154 constructorDescriptor.setReturnType(classDescriptor.getDefaultType());
155 classDescriptor.getConstructors().add(constructorDescriptor);
156 classDescriptor.setPrimaryConstructor(constructorDescriptor);
157
158 for (ValueParameterDescriptor valueParameter : valueParameters) {
159 classScope.addPropertyDescriptor(createPropertyFromScriptParameter(this, valueParameter));
160 }
161 }
162
163 @NotNull
164 public static ConstructorDescriptorImpl createConstructor(
165 @NotNull ScriptDescriptor scriptDescriptor, @NotNull List<ValueParameterDescriptor> valueParameters
166 ) {
167 return ConstructorDescriptorImpl.create(scriptDescriptor.getClassDescriptor(), Annotations.EMPTY, true)
168 .initialize(
169 Collections.<TypeParameterDescriptor>emptyList(),
170 valueParameters,
171 Visibilities.PUBLIC,
172 false
173 );
174 }
175
176 @NotNull
177 public static PropertyDescriptor createPropertyFromScriptParameter(
178 @NotNull ScriptDescriptor scriptDescriptor,
179 @NotNull ValueParameterDescriptor parameter
180 ) {
181 PropertyDescriptorImpl propertyDescriptor = PropertyDescriptorImpl.create(scriptDescriptor.getClassDescriptor(),
182 Annotations.EMPTY,
183 Modality.FINAL,
184 Visibilities.PUBLIC,
185 false,
186 parameter.getName(),
187 CallableMemberDescriptor.Kind.DECLARATION);
188 propertyDescriptor.setType(
189 parameter.getType(),
190 Collections.<TypeParameterDescriptor>emptyList(),
191 scriptDescriptor.getThisAsReceiverParameter(), ReceiverParameterDescriptor.NO_RECEIVER_PARAMETER);
192 propertyDescriptor.initialize(null, null);
193 return propertyDescriptor;
194 }
195
196 @Override
197 @NotNull
198 public ClassDescriptor getClassDescriptor() {
199 return classDescriptor;
200 }
201
202 @Override
203 @NotNull
204 public WritableScope getScopeForBodyResolution() {
205 return scopeForBodyResolution;
206 }
207
208 public void setScopeForBodyResolution(@NotNull WritableScope scopeForBodyResolution) {
209 assert this.scopeForBodyResolution == null : "Scope for body resolution already set for " + this;
210 this.scopeForBodyResolution = scopeForBodyResolution;
211 }
212 }