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.js.analyze;
018
019 import com.google.common.collect.ImmutableList;
020 import org.jetbrains.annotations.NotNull;
021 import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
022 import org.jetbrains.kotlin.context.ContextPackage;
023 import org.jetbrains.kotlin.context.ModuleContext;
024 import org.jetbrains.kotlin.context.MutableModuleContext;
025 import org.jetbrains.kotlin.descriptors.ModuleParameters;
026 import org.jetbrains.kotlin.descriptors.PackageFragmentProvider;
027 import org.jetbrains.kotlin.descriptors.impl.ModuleDescriptorImpl;
028 import org.jetbrains.kotlin.di.InjectorForTopDownAnalyzerForJs;
029 import org.jetbrains.kotlin.js.analyzer.JsAnalysisResult;
030 import org.jetbrains.kotlin.js.config.Config;
031 import org.jetbrains.kotlin.name.Name;
032 import org.jetbrains.kotlin.platform.PlatformToKotlinClassMap;
033 import org.jetbrains.kotlin.psi.JetFile;
034 import org.jetbrains.kotlin.resolve.*;
035 import org.jetbrains.kotlin.resolve.lazy.declarations.FileBasedDeclarationProviderFactory;
036
037 import java.util.ArrayList;
038 import java.util.Collection;
039 import java.util.Collections;
040 import java.util.List;
041
042 public final class TopDownAnalyzerFacadeForJS {
043 public static final List<ImportPath> DEFAULT_IMPORTS = ImmutableList.of(
044 new ImportPath("java.lang.*"),
045 new ImportPath("kotlin.*"),
046 new ImportPath("kotlin.js.*")
047 );
048
049 public static ModuleParameters JS_MODULE_PARAMETERS = new ModuleParameters() {
050 @NotNull
051 @Override
052 public List<ImportPath> getDefaultImports() {
053 return DEFAULT_IMPORTS;
054 }
055
056 @NotNull
057 @Override
058 public PlatformToKotlinClassMap getPlatformToKotlinClassMap() {
059 return PlatformToKotlinClassMap.EMPTY;
060 }
061 };
062
063 private TopDownAnalyzerFacadeForJS() {
064 }
065
066 @NotNull
067 public static JsAnalysisResult analyzeFiles(
068 @NotNull Collection<JetFile> files,
069 @NotNull Config config
070 ) {
071 BindingTrace trace = new BindingTraceContext();
072
073 MutableModuleContext newModuleContext = ContextPackage.ContextForNewModule(
074 config.getProject(), Name.special("<" + config.getModuleId() + ">"), JS_MODULE_PARAMETERS
075 );
076 newModuleContext.setDependencies(computeDependencies(newModuleContext.getModule(), config));
077 return analyzeFilesWithGivenTrace(files, trace, newModuleContext, config);
078 }
079
080 @NotNull
081 private static List<ModuleDescriptorImpl> computeDependencies(ModuleDescriptorImpl module, @NotNull Config config) {
082 List<ModuleDescriptorImpl> allDependencies = new ArrayList<ModuleDescriptorImpl>();
083 allDependencies.add(module);
084 allDependencies.addAll(config.getModuleDescriptors());
085 allDependencies.add(KotlinBuiltIns.getInstance().getBuiltInsModule());
086 return allDependencies;
087 }
088
089 @NotNull
090 public static JsAnalysisResult analyzeFilesWithGivenTrace(
091 @NotNull Collection<JetFile> files,
092 @NotNull BindingTrace trace,
093 @NotNull ModuleContext moduleContext,
094 @NotNull Config config
095 ) {
096 Collection<JetFile> allFiles = Config.withJsLibAdded(files, config);
097
098 InjectorForTopDownAnalyzerForJs injector = new InjectorForTopDownAnalyzerForJs(
099 moduleContext, trace, new FileBasedDeclarationProviderFactory(moduleContext.getStorageManager(), allFiles)
100 );
101 try {
102 injector.getLazyTopDownAnalyzerForTopLevel().analyzeFiles(TopDownAnalysisMode.TopLevelDeclarations, files,
103 Collections.<PackageFragmentProvider>emptyList());
104 return JsAnalysisResult.success(trace, moduleContext.getModule());
105 }
106 finally {
107 injector.destroy();
108 }
109 }
110
111 public static void checkForErrors(@NotNull Collection<JetFile> allFiles, @NotNull BindingContext bindingContext) {
112 AnalyzingUtils.throwExceptionOnErrors(bindingContext);
113 for (JetFile file : allFiles) {
114 AnalyzingUtils.checkForSyntacticErrors(file);
115 }
116 }
117 }