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 org.jetbrains.annotations.NotNull;
020 import org.jetbrains.kotlin.context.ContextKt;
021 import org.jetbrains.kotlin.context.ModuleContext;
022 import org.jetbrains.kotlin.context.MutableModuleContext;
023 import org.jetbrains.kotlin.descriptors.PackageFragmentProvider;
024 import org.jetbrains.kotlin.descriptors.impl.ModuleDescriptorImpl;
025 import org.jetbrains.kotlin.frontend.js.di.InjectionKt;
026 import org.jetbrains.kotlin.js.analyzer.JsAnalysisResult;
027 import org.jetbrains.kotlin.js.config.Config;
028 import org.jetbrains.kotlin.js.resolve.JsPlatform;
029 import org.jetbrains.kotlin.name.Name;
030 import org.jetbrains.kotlin.psi.KtFile;
031 import org.jetbrains.kotlin.resolve.*;
032 import org.jetbrains.kotlin.resolve.lazy.declarations.FileBasedDeclarationProviderFactory;
033
034 import java.util.ArrayList;
035 import java.util.Collection;
036 import java.util.Collections;
037 import java.util.List;
038
039 public final class TopDownAnalyzerFacadeForJS {
040 private TopDownAnalyzerFacadeForJS() {
041 }
042
043 @NotNull
044 public static JsAnalysisResult analyzeFiles(
045 @NotNull Collection<KtFile> files,
046 @NotNull Config config
047 ) {
048 BindingTrace trace = new BindingTraceContext();
049
050 MutableModuleContext newModuleContext = ContextKt.ContextForNewModule(
051 config.getProject(), Name.special("<" + config.getModuleId() + ">"), JsPlatform.INSTANCE$
052 );
053 newModuleContext.setDependencies(computeDependencies(newModuleContext.getModule(), config));
054 return analyzeFilesWithGivenTrace(files, trace, newModuleContext, config);
055 }
056
057 @NotNull
058 private static List<ModuleDescriptorImpl> computeDependencies(ModuleDescriptorImpl module, @NotNull Config config) {
059 List<ModuleDescriptorImpl> allDependencies = new ArrayList<ModuleDescriptorImpl>();
060 allDependencies.add(module);
061 allDependencies.addAll(config.getModuleDescriptors());
062 allDependencies.add(JsPlatform.INSTANCE$.getBuiltIns().getBuiltInsModule());
063 return allDependencies;
064 }
065
066 @NotNull
067 public static JsAnalysisResult analyzeFilesWithGivenTrace(
068 @NotNull Collection<KtFile> files,
069 @NotNull BindingTrace trace,
070 @NotNull ModuleContext moduleContext,
071 @NotNull Config config
072 ) {
073 Collection<KtFile> allFiles = Config.withJsLibAdded(files, config);
074
075 LazyTopDownAnalyzerForTopLevel analyzerForJs = InjectionKt.createTopDownAnalyzerForJs(
076 moduleContext, trace,
077 new FileBasedDeclarationProviderFactory(moduleContext.getStorageManager(), allFiles)
078 );
079 analyzerForJs.analyzeFiles(TopDownAnalysisMode.TopLevelDeclarations, files, Collections.<PackageFragmentProvider>emptyList());
080 return JsAnalysisResult.success(trace, moduleContext.getModule());
081 }
082
083 public static void checkForErrors(@NotNull Collection<KtFile> allFiles, @NotNull BindingContext bindingContext) {
084 AnalyzingUtils.throwExceptionOnErrors(bindingContext);
085 for (KtFile file : allFiles) {
086 AnalyzingUtils.checkForSyntacticErrors(file);
087 }
088 }
089 }