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.k2js.analyze;
018
019 import com.google.common.collect.ImmutableList;
020 import org.jetbrains.annotations.NotNull;
021 import org.jetbrains.annotations.Nullable;
022 import org.jetbrains.jet.lang.DefaultModuleConfiguration;
023 import org.jetbrains.jet.lang.ModuleConfiguration;
024 import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
025 import org.jetbrains.jet.lang.resolve.BindingContext;
026 import org.jetbrains.jet.lang.resolve.BindingTrace;
027 import org.jetbrains.jet.lang.resolve.DescriptorUtils;
028 import org.jetbrains.jet.lang.resolve.ImportPath;
029 import org.jetbrains.jet.lang.resolve.name.FqName;
030 import org.jetbrains.jet.lang.resolve.name.FqNameUnsafe;
031 import org.jetbrains.jet.lang.resolve.scopes.WritableScope;
032 import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
033
034 import java.util.List;
035
036 import static org.jetbrains.jet.lang.resolve.DescriptorUtils.isRootNamespace;
037
038 public class JsConfiguration implements ModuleConfiguration {
039
040 @NotNull
041 public static final List<ImportPath> DEFAULT_IMPORT_PATHS = ImmutableList.of(
042 new ImportPath("js.*"),
043 new ImportPath("java.lang.*"),
044 new ImportPath(KotlinBuiltIns.getInstance().getBuiltInsPackageFqName(), true),
045 new ImportPath("kotlin.*"));
046
047 /*
048 * Adds a possibility to inject some preanalyzed files to speed up tests.
049 */
050 @Nullable
051 private final BindingContext preanalyzedContext;
052
053 JsConfiguration(@Nullable BindingContext preanalyzedContext) {
054 this.preanalyzedContext = preanalyzedContext;
055 }
056
057 @Override
058 public void extendNamespaceScope(@NotNull BindingTrace trace, @NotNull NamespaceDescriptor namespaceDescriptor,
059 @NotNull WritableScope namespaceMemberScope) {
060 DefaultModuleConfiguration.INSTANCE
061 .extendNamespaceScope(trace, namespaceDescriptor, namespaceMemberScope);
062
063 // Extend root namespace with standard classes
064 if (namespaceDescriptor.getFqName().shortNameOrSpecial().equals(FqNameUnsafe.ROOT_NAME)) {
065 namespaceMemberScope.importScope(KotlinBuiltIns.getInstance().getBuiltInsScope());
066 }
067
068 if (hasPreanalyzedContextForTests()) {
069 extendScopeWithPreAnalyzedContextForTests(namespaceDescriptor, namespaceMemberScope);
070 }
071 }
072
073 private boolean hasPreanalyzedContextForTests() {
074 return preanalyzedContext != null;
075 }
076
077 /*NOTE: this code is wrong. Check it if you have tests failing for frontend reasons*/
078 @SuppressWarnings("ConstantConditions")
079 private void extendScopeWithPreAnalyzedContextForTests(@NotNull NamespaceDescriptor namespaceDescriptor,
080 @NotNull WritableScope namespaceMemberScope) {
081 if (isNamespaceImportedByDefault(namespaceDescriptor) || isRootNamespace(namespaceDescriptor)) {
082 FqName descriptorName = DescriptorUtils.getFQName(namespaceDescriptor).toSafe();
083 NamespaceDescriptor alreadyAnalyzedNamespace = preanalyzedContext.get(BindingContext.FQNAME_TO_NAMESPACE_DESCRIPTOR, descriptorName);
084 namespaceMemberScope.importScope(alreadyAnalyzedNamespace.getMemberScope());
085 }
086 }
087
088 private static boolean isNamespaceImportedByDefault(@NotNull NamespaceDescriptor namespaceDescriptor) {
089 for (ImportPath path : DEFAULT_IMPORT_PATHS) {
090 if (path.fqnPart().equals(DescriptorUtils.getFQName(namespaceDescriptor).toSafe())) {
091 return true;
092 }
093 }
094 return false;
095 }
096 }