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.resolve;
018    
019    import com.intellij.openapi.util.Key;
020    import org.jetbrains.annotations.NotNull;
021    import org.jetbrains.jet.lang.descriptors.PackageFragmentDescriptor;
022    import org.jetbrains.jet.lang.descriptors.ScriptDescriptor;
023    import org.jetbrains.jet.lang.descriptors.ScriptDescriptorImpl;
024    import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor;
025    import org.jetbrains.jet.lang.psi.JetFile;
026    import org.jetbrains.jet.lang.psi.JetScript;
027    import org.jetbrains.jet.lang.resolve.name.FqName;
028    import org.jetbrains.jet.lang.resolve.name.Name;
029    import org.jetbrains.jet.lang.resolve.scopes.RedeclarationHandler;
030    import org.jetbrains.jet.lang.resolve.scopes.WritableScope;
031    import org.jetbrains.jet.lang.resolve.scopes.WritableScopeImpl;
032    
033    import javax.inject.Inject;
034    import java.util.List;
035    import java.util.Map;
036    
037    // SCRIPT: Resolve declarations in scripts
038    public class ScriptHeaderResolver {
039    
040        public static final Key<Integer> PRIORITY_KEY = Key.create(JetScript.class.getName() + ".priority");
041    
042        @NotNull
043        private MutablePackageFragmentProvider packageFragmentProvider;
044        @NotNull
045        private ScriptParameterResolver scriptParameterResolver;
046        @NotNull
047        private BindingTrace trace;
048    
049        @Inject
050        public void setPackageFragmentProvider(@NotNull MutablePackageFragmentProvider packageFragmentProvider) {
051            this.packageFragmentProvider = packageFragmentProvider;
052        }
053    
054        @Inject
055        public void setScriptParameterResolver(@NotNull ScriptParameterResolver scriptParameterResolver) {
056            this.scriptParameterResolver = scriptParameterResolver;
057        }
058    
059        @Inject
060        public void setTrace(@NotNull BindingTrace trace) {
061            this.trace = trace;
062        }
063    
064    
065    
066        public void processScriptHierarchy(@NotNull TopDownAnalysisContext c, @NotNull JetScript script, @NotNull WritableScope outerScope) {
067            JetFile file = script.getContainingJetFile();
068            FqName fqName = file.getPackageFqName();
069            PackageFragmentDescriptor ns = packageFragmentProvider.getOrCreateFragment(fqName);
070    
071            Integer priority = getScriptPriority(script);
072    
073            FqName nameForScript = ScriptNameUtil.classNameForScript(script);
074            Name className = nameForScript.shortName();
075            ScriptDescriptorImpl scriptDescriptor = new ScriptDescriptorImpl(ns, priority, outerScope, className);
076    
077            WritableScopeImpl scriptScope = new WritableScopeImpl(outerScope, scriptDescriptor, RedeclarationHandler.DO_NOTHING, "script");
078            scriptScope.changeLockLevel(WritableScope.LockLevel.BOTH);
079            scriptDescriptor.setScopeForBodyResolution(scriptScope);
080    
081            c.getScripts().put(script, scriptDescriptor);
082    
083            trace.record(BindingContext.SCRIPT, script, scriptDescriptor);
084    
085            outerScope.addClassifierDescriptor(scriptDescriptor.getClassDescriptor());
086        }
087    
088        public static int getScriptPriority(@NotNull JetScript script) {
089            Integer priority = script.getUserData(PRIORITY_KEY);
090            return priority == null ? 0 : priority;
091        }
092    
093        public void resolveScriptDeclarations(@NotNull TopDownAnalysisContext c) {
094            for (Map.Entry<JetScript, ScriptDescriptor> e : c.getScripts().entrySet()) {
095                JetScript declaration = e.getKey();
096                ScriptDescriptorImpl descriptor = (ScriptDescriptorImpl) e.getValue();
097    
098                List<ValueParameterDescriptor> valueParameters = scriptParameterResolver.resolveScriptParameters(
099                        declaration,
100                        descriptor
101                );
102    
103                descriptor.setValueParameters(valueParameters);
104    
105                WritableScope scope = descriptor.getScopeForBodyResolution();
106                scope.setImplicitReceiver(descriptor.getThisAsReceiverParameter());
107                for (ValueParameterDescriptor valueParameterDescriptor : valueParameters) {
108                    scope.addVariableDescriptor(valueParameterDescriptor);
109                }
110            }
111        }
112    
113    }