js.frontend new module
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="module" module-name="frontend" />
|
||||
<orderEntry type="library" scope="PROVIDED" name="intellij-core" level="project" />
|
||||
<orderEntry type="module" module-name="util" />
|
||||
<orderEntry type="module" module-name="js.dart-ast" exported="" />
|
||||
</component>
|
||||
</module>
|
||||
|
||||
@@ -0,0 +1,279 @@
|
||||
/*
|
||||
* Copyright 2010-2014 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.jet.di;
|
||||
|
||||
import com.intellij.openapi.project.Project;
|
||||
import org.jetbrains.jet.context.GlobalContext;
|
||||
import org.jetbrains.jet.storage.StorageManager;
|
||||
import org.jetbrains.jet.lang.resolve.BindingTrace;
|
||||
import org.jetbrains.jet.lang.descriptors.ModuleDescriptor;
|
||||
import org.jetbrains.jet.lang.PlatformToKotlinClassMap;
|
||||
import org.jetbrains.jet.lang.resolve.TopDownAnalyzer;
|
||||
import org.jetbrains.jet.lang.resolve.LazyTopDownAnalyzer;
|
||||
import org.jetbrains.jet.lang.resolve.MutablePackageFragmentProvider;
|
||||
import org.jetbrains.jet.lang.resolve.BodyResolver;
|
||||
import org.jetbrains.jet.lang.resolve.AnnotationResolver;
|
||||
import org.jetbrains.jet.lang.resolve.calls.CallResolver;
|
||||
import org.jetbrains.jet.lang.resolve.calls.ArgumentTypeResolver;
|
||||
import org.jetbrains.jet.lang.types.expressions.ExpressionTypingServices;
|
||||
import org.jetbrains.jet.lang.types.expressions.ExpressionTypingComponents;
|
||||
import org.jetbrains.jet.lang.types.expressions.ControlStructureTypingUtils;
|
||||
import org.jetbrains.jet.lang.types.expressions.ExpressionTypingUtils;
|
||||
import org.jetbrains.jet.lang.types.expressions.ForLoopConventionsChecker;
|
||||
import org.jetbrains.jet.lang.reflect.ReflectionTypes;
|
||||
import org.jetbrains.jet.lang.resolve.calls.CallExpressionResolver;
|
||||
import org.jetbrains.jet.lang.resolve.DescriptorResolver;
|
||||
import org.jetbrains.jet.lang.resolve.DelegatedPropertyResolver;
|
||||
import org.jetbrains.jet.lang.resolve.TypeResolver;
|
||||
import org.jetbrains.jet.lang.resolve.QualifiedExpressionResolver;
|
||||
import org.jetbrains.jet.lang.resolve.calls.CallResolverExtensionProvider;
|
||||
import org.jetbrains.jet.lang.resolve.calls.CallCompleter;
|
||||
import org.jetbrains.jet.lang.resolve.calls.CandidateResolver;
|
||||
import org.jetbrains.jet.lang.resolve.ControlFlowAnalyzer;
|
||||
import org.jetbrains.jet.lang.resolve.DeclarationsChecker;
|
||||
import org.jetbrains.jet.lang.resolve.FunctionAnalyzerExtension;
|
||||
import org.jetbrains.jet.lang.resolve.ScriptBodyResolver;
|
||||
import org.jetbrains.jet.lang.resolve.DeclarationResolver;
|
||||
import org.jetbrains.jet.lang.resolve.ImportsResolver;
|
||||
import org.jetbrains.jet.lang.psi.JetImportsFactory;
|
||||
import org.jetbrains.jet.lang.resolve.OverloadResolver;
|
||||
import org.jetbrains.jet.lang.resolve.OverrideResolver;
|
||||
import org.jetbrains.jet.lang.resolve.TypeHierarchyResolver;
|
||||
import org.jetbrains.jet.lang.resolve.ScriptHeaderResolver;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import javax.annotation.PreDestroy;
|
||||
|
||||
/* This file is generated by org.jetbrains.jet.generators.injectors.InjectorsPackage. DO NOT EDIT! */
|
||||
@SuppressWarnings("ALL")
|
||||
public class InjectorForTopDownAnalyzerForJs {
|
||||
|
||||
private final Project project;
|
||||
private final GlobalContext globalContext;
|
||||
private final StorageManager storageManager;
|
||||
private final BindingTrace bindingTrace;
|
||||
private final ModuleDescriptor moduleDescriptor;
|
||||
private final PlatformToKotlinClassMap platformToKotlinClassMap;
|
||||
private final TopDownAnalyzer topDownAnalyzer;
|
||||
private final LazyTopDownAnalyzer lazyTopDownAnalyzer;
|
||||
private final MutablePackageFragmentProvider mutablePackageFragmentProvider;
|
||||
private final BodyResolver bodyResolver;
|
||||
private final AnnotationResolver annotationResolver;
|
||||
private final CallResolver callResolver;
|
||||
private final ArgumentTypeResolver argumentTypeResolver;
|
||||
private final ExpressionTypingServices expressionTypingServices;
|
||||
private final ExpressionTypingComponents expressionTypingComponents;
|
||||
private final ControlStructureTypingUtils controlStructureTypingUtils;
|
||||
private final ExpressionTypingUtils expressionTypingUtils;
|
||||
private final ForLoopConventionsChecker forLoopConventionsChecker;
|
||||
private final ReflectionTypes reflectionTypes;
|
||||
private final CallExpressionResolver callExpressionResolver;
|
||||
private final DescriptorResolver descriptorResolver;
|
||||
private final DelegatedPropertyResolver delegatedPropertyResolver;
|
||||
private final TypeResolver typeResolver;
|
||||
private final QualifiedExpressionResolver qualifiedExpressionResolver;
|
||||
private final CallResolverExtensionProvider callResolverExtensionProvider;
|
||||
private final CallCompleter callCompleter;
|
||||
private final CandidateResolver candidateResolver;
|
||||
private final ControlFlowAnalyzer controlFlowAnalyzer;
|
||||
private final DeclarationsChecker declarationsChecker;
|
||||
private final FunctionAnalyzerExtension functionAnalyzerExtension;
|
||||
private final ScriptBodyResolver scriptBodyResolver;
|
||||
private final DeclarationResolver declarationResolver;
|
||||
private final ImportsResolver importsResolver;
|
||||
private final JetImportsFactory jetImportsFactory;
|
||||
private final OverloadResolver overloadResolver;
|
||||
private final OverrideResolver overrideResolver;
|
||||
private final TypeHierarchyResolver typeHierarchyResolver;
|
||||
private final ScriptHeaderResolver scriptHeaderResolver;
|
||||
|
||||
public InjectorForTopDownAnalyzerForJs(
|
||||
@NotNull Project project,
|
||||
@NotNull GlobalContext globalContext,
|
||||
@NotNull BindingTrace bindingTrace,
|
||||
@NotNull ModuleDescriptor moduleDescriptor
|
||||
) {
|
||||
this.project = project;
|
||||
this.globalContext = globalContext;
|
||||
this.storageManager = globalContext.getStorageManager();
|
||||
this.bindingTrace = bindingTrace;
|
||||
this.moduleDescriptor = moduleDescriptor;
|
||||
this.platformToKotlinClassMap = moduleDescriptor.getPlatformToKotlinClassMap();
|
||||
this.topDownAnalyzer = new TopDownAnalyzer();
|
||||
this.lazyTopDownAnalyzer = new LazyTopDownAnalyzer();
|
||||
this.mutablePackageFragmentProvider = new MutablePackageFragmentProvider(getModuleDescriptor());
|
||||
this.bodyResolver = new BodyResolver();
|
||||
this.annotationResolver = new AnnotationResolver();
|
||||
this.callResolver = new CallResolver();
|
||||
this.argumentTypeResolver = new ArgumentTypeResolver();
|
||||
this.expressionTypingComponents = new ExpressionTypingComponents();
|
||||
this.expressionTypingServices = new ExpressionTypingServices(expressionTypingComponents);
|
||||
this.controlStructureTypingUtils = new ControlStructureTypingUtils(expressionTypingServices);
|
||||
this.expressionTypingUtils = new ExpressionTypingUtils(expressionTypingServices, callResolver);
|
||||
this.forLoopConventionsChecker = new ForLoopConventionsChecker();
|
||||
this.reflectionTypes = new ReflectionTypes(getModuleDescriptor());
|
||||
this.callExpressionResolver = new CallExpressionResolver();
|
||||
this.descriptorResolver = new DescriptorResolver();
|
||||
this.delegatedPropertyResolver = new DelegatedPropertyResolver();
|
||||
this.typeResolver = new TypeResolver();
|
||||
this.qualifiedExpressionResolver = new QualifiedExpressionResolver();
|
||||
this.callResolverExtensionProvider = new CallResolverExtensionProvider();
|
||||
this.candidateResolver = new CandidateResolver();
|
||||
this.callCompleter = new CallCompleter(argumentTypeResolver, candidateResolver);
|
||||
this.controlFlowAnalyzer = new ControlFlowAnalyzer();
|
||||
this.declarationsChecker = new DeclarationsChecker();
|
||||
this.functionAnalyzerExtension = new FunctionAnalyzerExtension();
|
||||
this.scriptBodyResolver = new ScriptBodyResolver();
|
||||
this.declarationResolver = new DeclarationResolver();
|
||||
this.importsResolver = new ImportsResolver();
|
||||
this.jetImportsFactory = new JetImportsFactory();
|
||||
this.overloadResolver = new OverloadResolver();
|
||||
this.overrideResolver = new OverrideResolver();
|
||||
this.typeHierarchyResolver = new TypeHierarchyResolver();
|
||||
this.scriptHeaderResolver = new ScriptHeaderResolver();
|
||||
|
||||
this.topDownAnalyzer.setBodyResolver(bodyResolver);
|
||||
this.topDownAnalyzer.setDeclarationResolver(declarationResolver);
|
||||
this.topDownAnalyzer.setLazyTopDownAnalyzer(lazyTopDownAnalyzer);
|
||||
this.topDownAnalyzer.setModuleDescriptor(moduleDescriptor);
|
||||
this.topDownAnalyzer.setOverloadResolver(overloadResolver);
|
||||
this.topDownAnalyzer.setOverrideResolver(overrideResolver);
|
||||
this.topDownAnalyzer.setPackageFragmentProvider(mutablePackageFragmentProvider);
|
||||
this.topDownAnalyzer.setProject(project);
|
||||
this.topDownAnalyzer.setTrace(bindingTrace);
|
||||
this.topDownAnalyzer.setTypeHierarchyResolver(typeHierarchyResolver);
|
||||
|
||||
this.lazyTopDownAnalyzer.setBodyResolver(bodyResolver);
|
||||
this.lazyTopDownAnalyzer.setDeclarationResolver(declarationResolver);
|
||||
this.lazyTopDownAnalyzer.setModuleDescriptor(moduleDescriptor);
|
||||
this.lazyTopDownAnalyzer.setOverloadResolver(overloadResolver);
|
||||
this.lazyTopDownAnalyzer.setOverrideResolver(overrideResolver);
|
||||
this.lazyTopDownAnalyzer.setTrace(bindingTrace);
|
||||
|
||||
bodyResolver.setAnnotationResolver(annotationResolver);
|
||||
bodyResolver.setCallResolver(callResolver);
|
||||
bodyResolver.setControlFlowAnalyzer(controlFlowAnalyzer);
|
||||
bodyResolver.setDeclarationsChecker(declarationsChecker);
|
||||
bodyResolver.setDelegatedPropertyResolver(delegatedPropertyResolver);
|
||||
bodyResolver.setExpressionTypingServices(expressionTypingServices);
|
||||
bodyResolver.setFunctionAnalyzerExtension(functionAnalyzerExtension);
|
||||
bodyResolver.setScriptBodyResolverResolver(scriptBodyResolver);
|
||||
bodyResolver.setTrace(bindingTrace);
|
||||
|
||||
annotationResolver.setCallResolver(callResolver);
|
||||
annotationResolver.setStorageManager(storageManager);
|
||||
annotationResolver.setTypeResolver(typeResolver);
|
||||
|
||||
callResolver.setArgumentTypeResolver(argumentTypeResolver);
|
||||
callResolver.setCallCompleter(callCompleter);
|
||||
callResolver.setCandidateResolver(candidateResolver);
|
||||
callResolver.setExpressionTypingServices(expressionTypingServices);
|
||||
callResolver.setTypeResolver(typeResolver);
|
||||
|
||||
argumentTypeResolver.setExpressionTypingServices(expressionTypingServices);
|
||||
argumentTypeResolver.setTypeResolver(typeResolver);
|
||||
|
||||
expressionTypingServices.setAnnotationResolver(annotationResolver);
|
||||
expressionTypingServices.setCallExpressionResolver(callExpressionResolver);
|
||||
expressionTypingServices.setCallResolver(callResolver);
|
||||
expressionTypingServices.setDescriptorResolver(descriptorResolver);
|
||||
expressionTypingServices.setExtensionProvider(callResolverExtensionProvider);
|
||||
expressionTypingServices.setProject(project);
|
||||
expressionTypingServices.setTypeResolver(typeResolver);
|
||||
|
||||
expressionTypingComponents.setCallResolver(callResolver);
|
||||
expressionTypingComponents.setControlStructureTypingUtils(controlStructureTypingUtils);
|
||||
expressionTypingComponents.setExpressionTypingServices(expressionTypingServices);
|
||||
expressionTypingComponents.setExpressionTypingUtils(expressionTypingUtils);
|
||||
expressionTypingComponents.setForLoopConventionsChecker(forLoopConventionsChecker);
|
||||
expressionTypingComponents.setGlobalContext(globalContext);
|
||||
expressionTypingComponents.setPlatformToKotlinClassMap(platformToKotlinClassMap);
|
||||
expressionTypingComponents.setReflectionTypes(reflectionTypes);
|
||||
|
||||
forLoopConventionsChecker.setExpressionTypingServices(expressionTypingServices);
|
||||
forLoopConventionsChecker.setExpressionTypingUtils(expressionTypingUtils);
|
||||
forLoopConventionsChecker.setProject(project);
|
||||
|
||||
callExpressionResolver.setExpressionTypingServices(expressionTypingServices);
|
||||
|
||||
descriptorResolver.setAnnotationResolver(annotationResolver);
|
||||
descriptorResolver.setDelegatedPropertyResolver(delegatedPropertyResolver);
|
||||
descriptorResolver.setExpressionTypingServices(expressionTypingServices);
|
||||
descriptorResolver.setStorageManager(storageManager);
|
||||
descriptorResolver.setTypeResolver(typeResolver);
|
||||
|
||||
delegatedPropertyResolver.setCallResolver(callResolver);
|
||||
delegatedPropertyResolver.setExpressionTypingServices(expressionTypingServices);
|
||||
|
||||
typeResolver.setAnnotationResolver(annotationResolver);
|
||||
typeResolver.setModuleDescriptor(moduleDescriptor);
|
||||
typeResolver.setQualifiedExpressionResolver(qualifiedExpressionResolver);
|
||||
|
||||
candidateResolver.setArgumentTypeResolver(argumentTypeResolver);
|
||||
|
||||
controlFlowAnalyzer.setTrace(bindingTrace);
|
||||
|
||||
declarationsChecker.setDescriptorResolver(descriptorResolver);
|
||||
declarationsChecker.setTrace(bindingTrace);
|
||||
|
||||
functionAnalyzerExtension.setTrace(bindingTrace);
|
||||
|
||||
scriptBodyResolver.setExpressionTypingServices(expressionTypingServices);
|
||||
|
||||
declarationResolver.setAnnotationResolver(annotationResolver);
|
||||
declarationResolver.setDescriptorResolver(descriptorResolver);
|
||||
declarationResolver.setImportsResolver(importsResolver);
|
||||
declarationResolver.setTrace(bindingTrace);
|
||||
|
||||
importsResolver.setImportsFactory(jetImportsFactory);
|
||||
importsResolver.setModuleDescriptor(moduleDescriptor);
|
||||
importsResolver.setQualifiedExpressionResolver(qualifiedExpressionResolver);
|
||||
importsResolver.setTrace(bindingTrace);
|
||||
|
||||
jetImportsFactory.setProject(project);
|
||||
|
||||
overloadResolver.setTrace(bindingTrace);
|
||||
|
||||
overrideResolver.setTrace(bindingTrace);
|
||||
|
||||
typeHierarchyResolver.setDescriptorResolver(descriptorResolver);
|
||||
typeHierarchyResolver.setImportsResolver(importsResolver);
|
||||
typeHierarchyResolver.setPackageFragmentProvider(mutablePackageFragmentProvider);
|
||||
typeHierarchyResolver.setScriptHeaderResolver(scriptHeaderResolver);
|
||||
typeHierarchyResolver.setTrace(bindingTrace);
|
||||
|
||||
scriptHeaderResolver.setPackageFragmentProvider(mutablePackageFragmentProvider);
|
||||
scriptHeaderResolver.setTrace(bindingTrace);
|
||||
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void destroy() {
|
||||
}
|
||||
|
||||
public ModuleDescriptor getModuleDescriptor() {
|
||||
return this.moduleDescriptor;
|
||||
}
|
||||
|
||||
public TopDownAnalyzer getTopDownAnalyzer() {
|
||||
return this.topDownAnalyzer;
|
||||
}
|
||||
|
||||
public LazyTopDownAnalyzer getLazyTopDownAnalyzer() {
|
||||
return this.lazyTopDownAnalyzer;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
/*
|
||||
* Copyright 2010-2014 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.k2js.analyze;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.base.Predicates;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.analyzer.AnalyzeExhaust;
|
||||
import org.jetbrains.jet.context.ContextPackage;
|
||||
import org.jetbrains.jet.context.GlobalContextImpl;
|
||||
import org.jetbrains.jet.di.InjectorForLazyResolve;
|
||||
import org.jetbrains.jet.di.InjectorForTopDownAnalyzerForJs;
|
||||
import org.jetbrains.jet.lang.PlatformToKotlinClassMap;
|
||||
import org.jetbrains.jet.lang.descriptors.ModuleDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.impl.ModuleDescriptorImpl;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.jet.lang.resolve.*;
|
||||
import org.jetbrains.jet.lang.resolve.lazy.ResolveSession;
|
||||
import org.jetbrains.jet.lang.resolve.lazy.declarations.DeclarationProviderFactory;
|
||||
import org.jetbrains.jet.lang.resolve.lazy.declarations.DeclarationProviderFactoryService;
|
||||
import org.jetbrains.jet.lang.resolve.name.Name;
|
||||
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
|
||||
import org.jetbrains.k2js.config.Config;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
public final class AnalyzerFacadeForJS {
|
||||
public static final List<ImportPath> DEFAULT_IMPORTS = ImmutableList.of(
|
||||
new ImportPath("js.*"),
|
||||
new ImportPath("java.lang.*"),
|
||||
new ImportPath("kotlin.*")
|
||||
);
|
||||
|
||||
private AnalyzerFacadeForJS() {
|
||||
}
|
||||
|
||||
//NOTE: web demo related method
|
||||
@SuppressWarnings("UnusedDeclaration")
|
||||
@NotNull
|
||||
public static BindingContext analyzeFiles(@NotNull Collection<JetFile> files, @NotNull Config config) {
|
||||
return analyzeFiles(files, Predicates.<PsiFile>alwaysTrue(), config).getBindingContext();
|
||||
}
|
||||
|
||||
//TODO: refactor
|
||||
@NotNull
|
||||
public static AnalyzeExhaust analyzeFiles(
|
||||
@NotNull Collection<JetFile> files,
|
||||
@NotNull Predicate<PsiFile> filesToAnalyzeCompletely,
|
||||
@NotNull Config config
|
||||
) {
|
||||
Project project = config.getProject();
|
||||
|
||||
ModuleDescriptorImpl owner = createJsModule("<module>");
|
||||
|
||||
Predicate<PsiFile> completely = Predicates.and(notLibFiles(config.getLibFiles()), filesToAnalyzeCompletely);
|
||||
|
||||
GlobalContextImpl globalContext = ContextPackage.GlobalContext();
|
||||
TopDownAnalysisParameters topDownAnalysisParameters = TopDownAnalysisParameters.create(
|
||||
globalContext.getStorageManager(), globalContext.getExceptionTracker(), completely, false, false);
|
||||
|
||||
owner.addDependencyOnModule(owner);
|
||||
owner.addDependencyOnModule(KotlinBuiltIns.getInstance().getBuiltInsModule());
|
||||
ModuleDescriptor libraryModule = config.getLibraryModule();
|
||||
if (libraryModule != null) {
|
||||
owner.addDependencyOnModule((ModuleDescriptorImpl) libraryModule); // "import" analyzed library module
|
||||
}
|
||||
owner.seal();
|
||||
|
||||
BindingContext libraryContext = config.getLibraryContext();
|
||||
BindingTrace trace = libraryContext == null
|
||||
? new BindingTraceContext()
|
||||
: new DelegatingBindingTrace(libraryContext, "trace with preanalyzed library");
|
||||
InjectorForTopDownAnalyzerForJs injector = new InjectorForTopDownAnalyzerForJs(project, topDownAnalysisParameters, trace, owner);
|
||||
try {
|
||||
Collection<JetFile> allFiles = libraryModule != null ?
|
||||
files :
|
||||
Config.withJsLibAdded(files, config);
|
||||
injector.getTopDownAnalyzer().analyzeFiles(topDownAnalysisParameters, allFiles);
|
||||
return AnalyzeExhaust.success(trace.getBindingContext(), owner);
|
||||
}
|
||||
finally {
|
||||
injector.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
public static void checkForErrors(@NotNull Collection<JetFile> allFiles, @NotNull BindingContext bindingContext) {
|
||||
AnalyzingUtils.throwExceptionOnErrors(bindingContext);
|
||||
for (JetFile file : allFiles) {
|
||||
AnalyzingUtils.checkForSyntacticErrors(file);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static Predicate<PsiFile> notLibFiles(@NotNull final List<JetFile> jsLibFiles) {
|
||||
return new Predicate<PsiFile>() {
|
||||
@Override
|
||||
public boolean apply(@Nullable PsiFile file) {
|
||||
assert file instanceof JetFile;
|
||||
@SuppressWarnings("UnnecessaryLocalVariable") boolean notLibFile = !jsLibFiles.contains(file);
|
||||
return notLibFile;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static ResolveSession getLazyResolveSession(
|
||||
@NotNull Collection<JetFile> syntheticFiles,
|
||||
@NotNull GlobalSearchScope filesScope,
|
||||
@NotNull Config config
|
||||
) {
|
||||
GlobalContextImpl globalContext = ContextPackage.GlobalContext();
|
||||
DeclarationProviderFactory declarationProviderFactory = DeclarationProviderFactoryService.OBJECT$
|
||||
.createDeclarationProviderFactory(
|
||||
config.getProject(),
|
||||
globalContext.getStorageManager(),
|
||||
//TODO: lib files are not really synthetic
|
||||
Config.withJsLibAdded(syntheticFiles, config),
|
||||
filesScope
|
||||
);
|
||||
ModuleDescriptorImpl module = createJsModule("<lazy module>");
|
||||
module.addDependencyOnModule(module);
|
||||
module.addDependencyOnModule(KotlinBuiltIns.getInstance().getBuiltInsModule());
|
||||
module.seal();
|
||||
|
||||
ResolveSession session = new InjectorForLazyResolve(
|
||||
config.getProject(),
|
||||
globalContext,
|
||||
module,
|
||||
declarationProviderFactory,
|
||||
new BindingTraceContext()).getResolveSession();
|
||||
module.initialize(session.getPackageFragmentProvider());
|
||||
return session;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static ModuleDescriptorImpl createJsModule(@NotNull String name) {
|
||||
return new ModuleDescriptorImpl(Name.special(name), DEFAULT_IMPORTS, PlatformToKotlinClassMap.EMPTY);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,226 @@
|
||||
/*
|
||||
* Copyright 2010-2013 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.k2js.config;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.descriptors.ModuleDescriptor;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Base class representing a configuration of translator.
|
||||
*/
|
||||
public abstract class Config {
|
||||
//NOTE: a hacky solution to be able to rerun code samples with lib loaded only once: used by tests and web demo
|
||||
@NotNull
|
||||
public static final String REWRITABLE_MODULE_NAME = "JS_TESTS";
|
||||
|
||||
@NotNull
|
||||
public static Config getEmptyConfig(@NotNull Project project, @NotNull EcmaVersion ecmaVersion) {
|
||||
return new Config(project, "main", ecmaVersion) {
|
||||
@NotNull
|
||||
@Override
|
||||
protected List<JetFile> generateLibFiles() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
//NOTE: used by mvn build
|
||||
@SuppressWarnings("UnusedDeclaration")
|
||||
@NotNull
|
||||
public static Config getEmptyConfig(@NotNull Project project) {
|
||||
return getEmptyConfig(project, EcmaVersion.defaultVersion());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static final List<String> LIB_FILES_WITH_DECLARATIONS = Arrays.asList(
|
||||
"/core/annotations.kt",
|
||||
"/core/core.kt",
|
||||
"/core/date.kt",
|
||||
"/core/dom.kt",
|
||||
"/core/javaio.kt",
|
||||
"/core/javalang.kt",
|
||||
"/core/javautil.kt",
|
||||
"/core/javautilCollections.kt",
|
||||
"/core/json.kt",
|
||||
"/core/kotlin.kt",
|
||||
"/core/math.kt",
|
||||
"/core/string.kt",
|
||||
"/core/htmlDom.kt",
|
||||
"/html5/canvas.kt",
|
||||
"/jquery/common.kt",
|
||||
"/jquery/ui.kt",
|
||||
"/junit/core.kt",
|
||||
"/qunit/core.kt",
|
||||
"/stdlib/browser.kt"
|
||||
);
|
||||
|
||||
@NotNull
|
||||
public static final List<String> LIB_FILES_WITH_CODE = Arrays.asList(
|
||||
"/stdlib/TuplesCode.kt",
|
||||
"/core/javautilCollectionsCode.kt"
|
||||
);
|
||||
|
||||
@NotNull
|
||||
public static final List<String> LIB_FILE_NAMES = Lists.newArrayList();
|
||||
|
||||
static {
|
||||
LIB_FILE_NAMES.addAll(LIB_FILES_WITH_DECLARATIONS);
|
||||
LIB_FILE_NAMES.addAll(LIB_FILES_WITH_CODE);
|
||||
}
|
||||
|
||||
/**
|
||||
* the library files which depend on the STDLIB files to be able to compile
|
||||
*/
|
||||
@NotNull
|
||||
public static final List<String> LIB_FILE_NAMES_DEPENDENT_ON_STDLIB = Arrays.asList(
|
||||
"/core/stringsCode.kt",
|
||||
"/stdlib/domCode.kt",
|
||||
"/core/javautilCode.kt",
|
||||
"/stdlib/jutilCode.kt",
|
||||
"/stdlib/testCode.kt"
|
||||
);
|
||||
|
||||
public static final String LIBRARIES_LOCATION = "js/js.libraries/src";
|
||||
public static final String REFLECTION_LIB_LOCATION = "core/reflection/src/kotlin/reflect";
|
||||
|
||||
/**
|
||||
* The file names in the standard library to compile
|
||||
*/
|
||||
@NotNull
|
||||
public static final List<String> STDLIB_FILE_NAMES = Arrays.asList(
|
||||
"/kotlin/Preconditions.kt",
|
||||
"/kotlin/Functions.kt",
|
||||
"/kotlin/collections/JUtil.kt",
|
||||
"/kotlin/collections/Iterators.kt",
|
||||
"/kotlin/collections/Arrays.kt",
|
||||
"/kotlin/collections/Maps.kt",
|
||||
"/kotlin/collections/Exceptions.kt",
|
||||
"/kotlin/collections/MutableCollections.kt",
|
||||
"/kotlin/collections/Stream.kt",
|
||||
"/kotlin/collections/AbstractIterator.kt",
|
||||
"/generated/_Aggregates.kt",
|
||||
"/generated/_Arrays.kt",
|
||||
"/generated/_DownTo.kt",
|
||||
"/generated/_Elements.kt",
|
||||
"/generated/_Filtering.kt",
|
||||
"/generated/_Generators.kt",
|
||||
"/generated/_Guards.kt",
|
||||
"/generated/_Mapping.kt",
|
||||
"/generated/_Numeric.kt",
|
||||
"/generated/_Ordering.kt",
|
||||
"/generated/_Snapshots.kt",
|
||||
"/generated/_Strings.kt",
|
||||
"/generated/_Streams.kt",
|
||||
"/kotlin/Standard.kt",
|
||||
"/kotlin/Ranges.kt",
|
||||
"/kotlin/Numbers.kt",
|
||||
"/kotlin/text/Strings.kt",
|
||||
"/kotlin/text/StringBuilder.kt",
|
||||
"/kotlin/dom/Dom.kt",
|
||||
"/kotlin/test/Test.kt"
|
||||
);
|
||||
|
||||
/**
|
||||
* The location of the stdlib sources
|
||||
*/
|
||||
public static final String STDLIB_LOCATION = "libraries/stdlib/src";
|
||||
|
||||
@NotNull
|
||||
private final Project project;
|
||||
@Nullable
|
||||
private List<JetFile> libFiles = null;
|
||||
@NotNull
|
||||
private final EcmaVersion target;
|
||||
|
||||
@NotNull
|
||||
private final String moduleId;
|
||||
|
||||
private final boolean sourcemap;
|
||||
|
||||
public Config(@NotNull Project project, @NotNull String moduleId, @NotNull EcmaVersion ecmaVersion) {
|
||||
this(project, moduleId, ecmaVersion, false);
|
||||
}
|
||||
|
||||
public Config(@NotNull Project project, @NotNull String moduleId, @NotNull EcmaVersion ecmaVersion, boolean sourcemap) {
|
||||
this.project = project;
|
||||
this.target = ecmaVersion;
|
||||
this.moduleId = moduleId;
|
||||
this.sourcemap = sourcemap;
|
||||
}
|
||||
|
||||
public boolean isSourcemap() {
|
||||
return sourcemap;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Project getProject() {
|
||||
return project;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public EcmaVersion getTarget() {
|
||||
return target;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getModuleId() {
|
||||
return moduleId;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected abstract List<JetFile> generateLibFiles();
|
||||
|
||||
@NotNull
|
||||
public final List<JetFile> getLibFiles() {
|
||||
if (libFiles == null) {
|
||||
libFiles = generateLibFiles();
|
||||
}
|
||||
return libFiles;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public BindingContext getLibraryContext() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public ModuleDescriptor getLibraryModule() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Collection<JetFile> withJsLibAdded(@NotNull Collection<JetFile> files, @NotNull Config config) {
|
||||
Collection<JetFile> allFiles = Lists.newArrayList();
|
||||
allFiles.addAll(files);
|
||||
allFiles.addAll(config.getLibFiles());
|
||||
return allFiles;
|
||||
}
|
||||
|
||||
public boolean isTestConfig() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright 2010-2013 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.k2js.config;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public enum EcmaVersion {
|
||||
v3, v5;
|
||||
|
||||
@NotNull
|
||||
public static EcmaVersion defaultVersion() {
|
||||
return v5;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
/*
|
||||
* Copyright 2010-2013 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.k2js.config;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Key;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.openapi.vfs.*;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.PsiManager;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
import static org.jetbrains.jet.lang.psi.PsiPackage.JetPsiFactory;
|
||||
|
||||
public class LibrarySourcesConfig extends Config {
|
||||
@NotNull
|
||||
public static final Key<String> EXTERNAL_MODULE_NAME = Key.create("externalModule");
|
||||
@NotNull
|
||||
public static final String UNKNOWN_EXTERNAL_MODULE_NAME = "<unknown>";
|
||||
|
||||
@NotNull
|
||||
private static final Logger LOG = Logger.getInstance("#org.jetbrains.k2js.config.LibrarySourcesConfig");
|
||||
|
||||
@NotNull
|
||||
private final List<String> files;
|
||||
|
||||
public LibrarySourcesConfig(
|
||||
@NotNull Project project,
|
||||
@NotNull String moduleId,
|
||||
@NotNull List<String> files,
|
||||
@NotNull EcmaVersion ecmaVersion,
|
||||
boolean sourcemap
|
||||
) {
|
||||
super(project, moduleId, ecmaVersion, sourcemap);
|
||||
this.files = files;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected List<JetFile> generateLibFiles() {
|
||||
if (files.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
List<JetFile> jetFiles = new ArrayList<JetFile>();
|
||||
String moduleName = UNKNOWN_EXTERNAL_MODULE_NAME;
|
||||
VirtualFileSystem fileSystem = VirtualFileManager.getInstance().getFileSystem(StandardFileSystems.FILE_PROTOCOL);
|
||||
PsiManager psiManager = PsiManager.getInstance(getProject());
|
||||
|
||||
for (String path : files) {
|
||||
if (path.charAt(0) == '@') {
|
||||
moduleName = path.substring(1);
|
||||
}
|
||||
else if (path.endsWith(".jar") || path.endsWith(".zip")) {
|
||||
try {
|
||||
jetFiles.addAll(readZip(path));
|
||||
}
|
||||
catch (IOException e) {
|
||||
LOG.error(e);
|
||||
}
|
||||
}
|
||||
else {
|
||||
VirtualFile file = fileSystem.findFileByPath(path);
|
||||
if (file == null) {
|
||||
LOG.error("File '" + path + "not found.'");
|
||||
}
|
||||
else if (file.isDirectory()) {
|
||||
JetFileCollector jetFileCollector = new JetFileCollector(jetFiles, moduleName, psiManager);
|
||||
VfsUtilCore.visitChildrenRecursively(file, jetFileCollector);
|
||||
}
|
||||
else {
|
||||
jetFiles.add(getJetFileByVirtualFile(file, moduleName, psiManager));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return jetFiles;
|
||||
}
|
||||
|
||||
private List<JetFile> readZip(String file) throws IOException {
|
||||
ZipFile zipFile = new ZipFile(file);
|
||||
try {
|
||||
return traverseArchive(zipFile);
|
||||
}
|
||||
finally {
|
||||
zipFile.close();
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private List<JetFile> traverseArchive(@NotNull ZipFile file) throws IOException {
|
||||
List<JetFile> result = Lists.newArrayList();
|
||||
Enumeration<? extends ZipEntry> zipEntries = file.entries();
|
||||
while (zipEntries.hasMoreElements()) {
|
||||
ZipEntry entry = zipEntries.nextElement();
|
||||
if (!entry.isDirectory() && entry.getName().endsWith(".kt")) {
|
||||
InputStream stream = file.getInputStream(entry);
|
||||
String text = StringUtil.convertLineSeparators(FileUtil.loadTextAndClose(stream));
|
||||
JetFile jetFile = JetPsiFactory(getProject()).createFile(entry.getName(), text);
|
||||
jetFile.putUserData(EXTERNAL_MODULE_NAME, UNKNOWN_EXTERNAL_MODULE_NAME);
|
||||
result.add(jetFile);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static JetFile getJetFileByVirtualFile(VirtualFile file, String moduleName, PsiManager psiManager) {
|
||||
PsiFile psiFile = psiManager.findFile(file);
|
||||
assert psiFile != null;
|
||||
psiFile.putUserData(EXTERNAL_MODULE_NAME, moduleName);
|
||||
return (JetFile) psiFile;
|
||||
}
|
||||
|
||||
private static class JetFileCollector extends VirtualFileVisitor {
|
||||
private final List<JetFile> jetFiles;
|
||||
private final String moduleName;
|
||||
private final PsiManager psiManager;
|
||||
|
||||
private JetFileCollector(List<JetFile> files, String name, PsiManager manager) {
|
||||
moduleName = name;
|
||||
psiManager = manager;
|
||||
jetFiles = files;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visitFile(@NotNull VirtualFile file) {
|
||||
if (file.getName().endsWith(".kt")) {
|
||||
jetFiles.add(getJetFileByVirtualFile(file, moduleName, psiManager));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user