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.codegen.context;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.annotations.Nullable;
021    import org.jetbrains.kotlin.codegen.OwnerKind;
022    import org.jetbrains.kotlin.codegen.binding.MutableClosure;
023    import org.jetbrains.kotlin.descriptors.ClassDescriptor;
024    import org.jetbrains.kotlin.descriptors.ScriptDescriptor;
025    
026    import java.util.List;
027    
028    public class ScriptContext extends FieldOwnerContext<ClassDescriptor> {
029        private final ScriptDescriptor scriptDescriptor;
030        private final List<ScriptDescriptor> earlierScripts;
031    
032        public ScriptContext(
033                @NotNull ScriptDescriptor scriptDescriptor,
034                @NotNull List<ScriptDescriptor> earlierScripts,
035                @NotNull ClassDescriptor contextDescriptor,
036                @NotNull OwnerKind contextKind,
037                @Nullable CodegenContext parentContext,
038                @Nullable MutableClosure closure
039        ) {
040            super(contextDescriptor, contextKind, parentContext, closure, contextDescriptor, null);
041            this.scriptDescriptor = scriptDescriptor;
042            this.earlierScripts = earlierScripts;
043        }
044    
045        @NotNull
046        public ScriptDescriptor getScriptDescriptor() {
047            return scriptDescriptor;
048        }
049    
050        @NotNull
051        public List<ScriptDescriptor> getEarlierScripts() {
052            return earlierScripts;
053        }
054    
055        @NotNull
056        public String getScriptFieldName(@NotNull ScriptDescriptor scriptDescriptor) {
057            int index = earlierScripts.indexOf(scriptDescriptor);
058            if (index < 0) {
059                throw new IllegalStateException("Unregistered script: " + scriptDescriptor);
060            }
061            return "script$" + (index + 1);
062        }
063    
064        @Override
065        public String toString() {
066            return "Script: " + getContextDescriptor().getName().asString();
067        }
068    }