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