001 /*
002 * Copyright 2010-2014 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.base.Predicate;
020 import com.google.common.base.Predicates;
021 import com.google.common.collect.ImmutableList;
022 import com.intellij.openapi.project.Project;
023 import com.intellij.psi.PsiFile;
024 import com.intellij.psi.search.GlobalSearchScope;
025 import org.jetbrains.annotations.NotNull;
026 import org.jetbrains.annotations.Nullable;
027 import org.jetbrains.jet.analyzer.AnalyzeExhaust;
028 import org.jetbrains.jet.context.ContextPackage;
029 import org.jetbrains.jet.context.GlobalContextImpl;
030 import org.jetbrains.jet.di.InjectorForLazyResolve;
031 import org.jetbrains.jet.di.InjectorForTopDownAnalyzerForJs;
032 import org.jetbrains.jet.lang.PlatformToKotlinClassMap;
033 import org.jetbrains.jet.lang.descriptors.DependencyKind;
034 import org.jetbrains.jet.lang.descriptors.ModuleDescriptor;
035 import org.jetbrains.jet.lang.descriptors.impl.ModuleDescriptorImpl;
036 import org.jetbrains.jet.lang.psi.JetFile;
037 import org.jetbrains.jet.lang.resolve.*;
038 import org.jetbrains.jet.lang.resolve.lazy.ResolveSession;
039 import org.jetbrains.jet.lang.resolve.lazy.declarations.DeclarationProviderFactory;
040 import org.jetbrains.jet.lang.resolve.lazy.declarations.DeclarationProviderFactoryService;
041 import org.jetbrains.jet.lang.resolve.name.Name;
042 import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
043 import org.jetbrains.k2js.config.Config;
044
045 import java.util.Collection;
046 import java.util.List;
047
048 public final class AnalyzerFacadeForJS {
049 public static final List<ImportPath> DEFAULT_IMPORTS = ImmutableList.of(
050 new ImportPath("js.*"),
051 new ImportPath("java.lang.*"),
052 new ImportPath("kotlin.*")
053 );
054
055 private AnalyzerFacadeForJS() {
056 }
057
058 //NOTE: web demo related method
059 @SuppressWarnings("UnusedDeclaration")
060 @NotNull
061 public static BindingContext analyzeFiles(@NotNull Collection<JetFile> files, @NotNull Config config) {
062 return analyzeFiles(files, Predicates.<PsiFile>alwaysTrue(), config).getBindingContext();
063 }
064
065 //TODO: refactor
066 @NotNull
067 public static AnalyzeExhaust analyzeFiles(
068 @NotNull Collection<JetFile> files,
069 @NotNull Predicate<PsiFile> filesToAnalyzeCompletely,
070 @NotNull Config config
071 ) {
072 Project project = config.getProject();
073
074 ModuleDescriptorImpl owner = createJsModule("<module>");
075
076 Predicate<PsiFile> completely = Predicates.and(notLibFiles(config.getLibFiles()), filesToAnalyzeCompletely);
077
078 GlobalContextImpl globalContext = ContextPackage.GlobalContext();
079 TopDownAnalysisParameters topDownAnalysisParameters = TopDownAnalysisParameters.create(
080 globalContext.getStorageManager(), globalContext.getExceptionTracker(), completely, false, false);
081
082 ModuleDescriptor libraryModule = config.getLibraryModule();
083 if (libraryModule != null) {
084 owner.addFragmentProvider(DependencyKind.BINARIES, libraryModule.getPackageFragmentProvider()); // "import" analyzed library module
085 }
086
087 BindingContext libraryContext = config.getLibraryContext();
088 BindingTrace trace = libraryContext == null
089 ? new BindingTraceContext()
090 : new DelegatingBindingTrace(libraryContext, "trace with preanalyzed library");
091 InjectorForTopDownAnalyzerForJs injector = new InjectorForTopDownAnalyzerForJs(project, topDownAnalysisParameters, trace, owner);
092 try {
093 Collection<JetFile> allFiles = libraryModule != null ?
094 files :
095 Config.withJsLibAdded(files, config);
096 injector.getTopDownAnalyzer().analyzeFiles(topDownAnalysisParameters, allFiles);
097 return AnalyzeExhaust.success(trace.getBindingContext(), owner);
098 }
099 finally {
100 injector.destroy();
101 }
102 }
103
104 public static void checkForErrors(@NotNull Collection<JetFile> allFiles, @NotNull BindingContext bindingContext) {
105 AnalyzingUtils.throwExceptionOnErrors(bindingContext);
106 for (JetFile file : allFiles) {
107 AnalyzingUtils.checkForSyntacticErrors(file);
108 }
109 }
110
111 @NotNull
112 private static Predicate<PsiFile> notLibFiles(@NotNull final List<JetFile> jsLibFiles) {
113 return new Predicate<PsiFile>() {
114 @Override
115 public boolean apply(@Nullable PsiFile file) {
116 assert file instanceof JetFile;
117 @SuppressWarnings("UnnecessaryLocalVariable") boolean notLibFile = !jsLibFiles.contains(file);
118 return notLibFile;
119 }
120 };
121 }
122
123 @NotNull
124 public static ResolveSession getLazyResolveSession(
125 @NotNull Collection<JetFile> syntheticFiles,
126 @NotNull GlobalSearchScope filesScope,
127 @NotNull Config config
128 ) {
129 GlobalContextImpl globalContext = ContextPackage.GlobalContext();
130 DeclarationProviderFactory declarationProviderFactory = DeclarationProviderFactoryService.OBJECT$
131 .createDeclarationProviderFactory(
132 config.getProject(),
133 globalContext.getStorageManager(),
134 //TODO: lib files are not really synthetic
135 Config.withJsLibAdded(syntheticFiles, config),
136 filesScope
137 );
138 ModuleDescriptorImpl module = createJsModule("<lazy module>");
139 module.addFragmentProvider(DependencyKind.BUILT_INS, KotlinBuiltIns.getInstance().getBuiltInsModule().getPackageFragmentProvider());
140
141 return new InjectorForLazyResolve(
142 config.getProject(),
143 globalContext,
144 module,
145 declarationProviderFactory,
146 new BindingTraceContext()).getResolveSession();
147 }
148
149 @NotNull
150 private static ModuleDescriptorImpl createJsModule(@NotNull String name) {
151 return new ModuleDescriptorImpl(Name.special(name), DEFAULT_IMPORTS, PlatformToKotlinClassMap.EMPTY);
152 }
153
154 }