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.annotations.Nullable;
021    import org.jetbrains.jet.lang.ModuleConfiguration;
022    import org.jetbrains.jet.lang.PlatformToKotlinClassMap;
023    import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
024    import org.jetbrains.jet.lang.descriptors.impl.DeclarationDescriptorImpl;
025    import org.jetbrains.jet.lang.descriptors.impl.NamespaceDescriptorImpl;
026    import org.jetbrains.jet.lang.resolve.ImportPath;
027    import org.jetbrains.jet.lang.resolve.name.FqName;
028    import org.jetbrains.jet.lang.resolve.name.Name;
029    import org.jetbrains.jet.lang.types.TypeSubstitutor;
030    
031    import java.util.Collections;
032    import java.util.List;
033    
034    public class ModuleDescriptorImpl extends DeclarationDescriptorImpl implements ModuleDescriptor {
035        private NamespaceDescriptor rootNamepsace;
036        private ModuleConfiguration moduleConfiguration;
037        private final List<ImportPath> defaultImports;
038        private final PlatformToKotlinClassMap platformToKotlinClassMap;
039    
040        public ModuleDescriptorImpl(
041                @NotNull Name name,
042                @NotNull List<ImportPath> defaultImports,
043                @NotNull PlatformToKotlinClassMap platformToKotlinClassMap
044        ) {
045            super(Collections.<AnnotationDescriptor>emptyList(), name);
046            if (!name.isSpecial()) {
047                throw new IllegalArgumentException("module name must be special: " + name);
048            }
049            this.defaultImports = defaultImports;
050            this.platformToKotlinClassMap = platformToKotlinClassMap;
051        }
052    
053        public void setRootNamespace(@NotNull NamespaceDescriptor rootNs) {
054            if (this.rootNamepsace != null) {
055                throw new IllegalStateException("setRootNamespace() is called twice");
056            }
057            this.rootNamepsace = rootNs;
058        }
059    
060        @Override
061        @Nullable
062        public DeclarationDescriptor getContainingDeclaration() {
063            return null;
064        }
065    
066        @Nullable
067        @Override
068        public NamespaceDescriptor getNamespace(@NotNull FqName fqName) {
069            if (fqName.isRoot()) return rootNamepsace;
070            NamespaceDescriptor current = rootNamepsace;
071            for (Name simpleName : fqName.pathSegments()) {
072                current = current.getMemberScope().getNamespace(simpleName);
073                if (current == null) return null;
074            }
075            return current;
076        }
077    
078        @NotNull
079        @Override
080        public ModuleConfiguration getModuleConfiguration() {
081            return moduleConfiguration;
082        }
083    
084        @NotNull
085        public ModuleDescriptorImpl setModuleConfiguration(@NotNull ModuleConfiguration moduleConfiguration) {
086            assert this.moduleConfiguration == null : "Trying to set module configuration twice for " + this;
087            this.moduleConfiguration = moduleConfiguration;
088            return this;
089        }
090    
091        @NotNull
092        @Override
093        public List<ImportPath> getDefaultImports() {
094            return defaultImports;
095        }
096    
097        @NotNull
098        @Override
099        public PlatformToKotlinClassMap getPlatformToKotlinClassMap() {
100            return platformToKotlinClassMap;
101        }
102    
103        public NamespaceDescriptorImpl getRootNamespaceDescriptorImpl() {
104            return (NamespaceDescriptorImpl) rootNamepsace;
105        }
106    
107        @NotNull
108        @Override
109        public ModuleDescriptor substitute(@NotNull TypeSubstitutor substitutor) {
110            return this;
111        }
112    
113        @Override
114        public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) {
115            return visitor.visitModuleDeclaration(this, data);
116        }
117    
118    
119        @Override
120        public void addNamespace(@NotNull NamespaceDescriptor namespaceDescriptor) {
121            if (namespaceDescriptor.getContainingDeclaration() != this) {
122                throw new IllegalStateException();
123            }
124            setRootNamespace(namespaceDescriptor);
125        }
126    
127    }