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    // SCRIPT: script as field owner context
029    public class ScriptContext extends FieldOwnerContext<ClassDescriptor> {
030        private final ScriptDescriptor scriptDescriptor;
031        private final List<ScriptDescriptor> earlierScripts;
032    
033        public ScriptContext(
034                @NotNull ScriptDescriptor scriptDescriptor,
035                @NotNull List<ScriptDescriptor> earlierScripts,
036                @NotNull ClassDescriptor contextDescriptor,
037                @NotNull OwnerKind contextKind,
038                @Nullable CodegenContext parentContext,
039                @Nullable MutableClosure closure
040        ) {
041            super(contextDescriptor, contextKind, parentContext, closure, contextDescriptor, null);
042            this.scriptDescriptor = scriptDescriptor;
043            this.earlierScripts = earlierScripts;
044        }
045    
046        @NotNull
047        public ScriptDescriptor getScriptDescriptor() {
048            return scriptDescriptor;
049        }
050    
051        @NotNull
052        public List<ScriptDescriptor> getEarlierScripts() {
053            return earlierScripts;
054        }
055    
056        @NotNull
057        public String getScriptFieldName(@NotNull ScriptDescriptor scriptDescriptor) {
058            int index = earlierScripts.indexOf(scriptDescriptor);
059            if (index < 0) {
060                throw new IllegalStateException("Unregistered script: " + scriptDescriptor);
061            }
062            return "script$" + (index + 1);
063        }
064    
065        @Override
066        public boolean isStatic() {
067            return true;
068        }
069    }