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.frontend.js.di.DiPackage;
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.annotation.*"),
047 new ImportPath("kotlin.js.*")
048 );
049
050 public static ModuleParameters JS_MODULE_PARAMETERS = new ModuleParameters() {
051 @NotNull
052 @Override
053 public List<ImportPath> getDefaultImports() {
054 return DEFAULT_IMPORTS;
055 }
056
057 @NotNull
058 @Override
059 public PlatformToKotlinClassMap getPlatformToKotlinClassMap() {
060 return PlatformToKotlinClassMap.EMPTY;
061 }
062 };
063
064 private TopDownAnalyzerFacadeForJS() {
065 }
066
067 @NotNull
068 public static JsAnalysisResult analyzeFiles(
069 @NotNull Collection<JetFile> files,
070 @NotNull Config config
071 ) {
072 BindingTrace trace = new BindingTraceContext();
073
074 MutableModuleContext newModuleContext = ContextPackage.ContextForNewModule(
075 config.getProject(), Name.special("<" + config.getModuleId() + ">"), JS_MODULE_PARAMETERS
076 );
077 newModuleContext.setDependencies(computeDependencies(newModuleContext.getModule(), config));
078 return analyzeFilesWithGivenTrace(files, trace, newModuleContext, config);
079 }
080
081 @NotNull
082 private static List<ModuleDescriptorImpl> computeDependencies(ModuleDescriptorImpl module, @NotNull Config config) {
083 List<ModuleDescriptorImpl> allDependencies = new ArrayList<ModuleDescriptorImpl>();
084 allDependencies.add(module);
085 allDependencies.addAll(config.getModuleDescriptors());
086 allDependencies.add(KotlinBuiltIns.getInstance().getBuiltInsModule());
087 return allDependencies;
088 }
089
090 @NotNull
091 public static JsAnalysisResult analyzeFilesWithGivenTrace(
092 @NotNull Collection<JetFile> files,
093 @NotNull BindingTrace trace,
094 @NotNull ModuleContext moduleContext,
095 @NotNull Config config
096 ) {
097 Collection<JetFile> allFiles = Config.withJsLibAdded(files, config);
098
099 LazyTopDownAnalyzerForTopLevel analyzerForJs = DiPackage.createTopDownAnalyzerForJs(
100 moduleContext, trace,
101 new FileBasedDeclarationProviderFactory(moduleContext.getStorageManager(), allFiles)
102 );
103 analyzerForJs.analyzeFiles(TopDownAnalysisMode.TopLevelDeclarations, files, Collections.<PackageFragmentProvider>emptyList());
104 return JsAnalysisResult.success(trace, moduleContext.getModule());
105 }
106
107 public static void checkForErrors(@NotNull Collection<JetFile> allFiles, @NotNull BindingContext bindingContext) {
108 AnalyzingUtils.throwExceptionOnErrors(bindingContext);
109 for (JetFile file : allFiles) {
110 AnalyzingUtils.checkForSyntacticErrors(file);
111 }
112 }
113 }