001    /*
002     * Copyright 2010-2015 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.kotlin.resolve;
018    
019    import com.google.common.collect.Lists;
020    import org.jetbrains.annotations.NotNull;
021    import org.jetbrains.kotlin.descriptors.ScriptDescriptor;
022    import org.jetbrains.kotlin.descriptors.SourceElement;
023    import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor;
024    import org.jetbrains.kotlin.descriptors.annotations.Annotations;
025    import org.jetbrains.kotlin.descriptors.impl.ValueParameterDescriptorImpl;
026    import org.jetbrains.kotlin.parsing.JetScriptDefinition;
027    import org.jetbrains.kotlin.parsing.JetScriptDefinitionProvider;
028    import org.jetbrains.kotlin.psi.JetFile;
029    import org.jetbrains.kotlin.psi.JetScript;
030    
031    import java.util.List;
032    
033    // SCRIPT: Resolve script parameters
034    public final class ScriptParameterResolver {
035        @NotNull
036        public static List<ValueParameterDescriptor> resolveScriptParameters(
037                @NotNull JetScript declaration,
038                @NotNull ScriptDescriptor scriptDescriptor
039        ) {
040            List<ValueParameterDescriptor> valueParameters = Lists.newArrayList();
041    
042            JetFile file = declaration.getContainingJetFile();
043            JetScriptDefinition scriptDefinition = JetScriptDefinitionProvider.getInstance(file.getProject()).findScriptDefinition(file);
044    
045            int index = 0;
046            for (AnalyzerScriptParameter scriptParameter : scriptDefinition.getScriptParameters()) {
047                ValueParameterDescriptor parameter = resolveScriptParameter(scriptParameter, index, scriptDescriptor);
048                valueParameters.add(parameter);
049                ++index;
050            }
051            return valueParameters;
052        }
053    
054        @NotNull
055        private static ValueParameterDescriptor resolveScriptParameter(
056                @NotNull AnalyzerScriptParameter scriptParameter,
057                int index,
058                @NotNull ScriptDescriptor script
059        ) {
060            return new ValueParameterDescriptorImpl(script, null, index, Annotations.EMPTY, scriptParameter.getName(),
061                                                    scriptParameter.getType(), false, null, SourceElement.NO_SOURCE);
062        }
063    
064        private ScriptParameterResolver() {
065        }
066    }