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