From 8222011874d6177e1e24f0750518271d6213758c Mon Sep 17 00:00:00 2001 From: Stepan Koltsov Date: Wed, 23 Nov 2011 17:08:47 +0400 Subject: [PATCH] +-JDK and -NOSTDLIB options FullJetPsiCheckerTest has -JDK option QuickJetPsiCheckerTest has +JDK option FullJetPsiCheckerTest = QuickJetPsiCheckerTest with +JDK QuickJetPsiCheckerTest = FullJetPsiCheckerTest with -JDK --- .../jet/codegen/GenerationState.java | 4 +- .../jet/compiler/CompileSession.java | 2 +- .../jet/lang/resolve/java/AnalyzerFacade.java | 2 +- .../jet/lang/JetSemanticServices.java | 6 ++- .../jet/lang/resolve/AnalyzingUtils.java | 10 ++-- .../jet/lang/types/JetStandardLibrary.java | 50 ++++++++++++++----- .../checkerWithErrorTypes/full/Casts.jet | 4 +- .../jet/checkers/CheckerTestUtilTest.java | 2 +- .../jet/checkers/FullJetPsiCheckerTest.java | 11 +++- .../jet/checkers/QuickJetPsiCheckerTest.java | 8 ++- .../jetbrains/jet/resolve/JetResolveTest.java | 2 +- .../jet/plugin/compiler/JetCompiler.java | 2 +- .../jet/plugin/java/JavaElementFinder.java | 2 +- 13 files changed, 77 insertions(+), 28 deletions(-) diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/GenerationState.java b/compiler/backend/src/org/jetbrains/jet/codegen/GenerationState.java index 56b432cf2c9..c106eeaed70 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/GenerationState.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/GenerationState.java @@ -30,7 +30,7 @@ public class GenerationState { public GenerationState(Project project, ClassBuilderFactory builderFactory) { this.project = project; - this.standardLibrary = JetStandardLibrary.getJetStandardLibrary(project); + this.standardLibrary = JetStandardLibrary.getJetStandardLibrary(project, true); this.factory = new ClassFileFactory(builderFactory, this); this.intrinsics = new IntrinsicMethods(project, standardLibrary); } @@ -82,7 +82,7 @@ public class GenerationState { public void compile(JetFile psiFile) { final JetNamespace namespace = psiFile.getRootNamespace(); - final BindingContext bindingContext = AnalyzingUtils.getInstance(JavaDefaultImports.JAVA_DEFAULT_IMPORTS).analyzeNamespace(namespace, JetControlFlowDataTraceFactory.EMPTY); + final BindingContext bindingContext = AnalyzingUtils.getInstance(JavaDefaultImports.JAVA_DEFAULT_IMPORTS, true).analyzeNamespace(namespace, JetControlFlowDataTraceFactory.EMPTY); AnalyzingUtils.throwExceptionOnErrors(bindingContext); compileCorrectNamespaces(bindingContext, Collections.singletonList(namespace)); // NamespaceCodegen codegen = forNamespace(namespace); diff --git a/compiler/backend/src/org/jetbrains/jet/compiler/CompileSession.java b/compiler/backend/src/org/jetbrains/jet/compiler/CompileSession.java index e90fda9eb8a..43f4661b726 100644 --- a/compiler/backend/src/org/jetbrains/jet/compiler/CompileSession.java +++ b/compiler/backend/src/org/jetbrains/jet/compiler/CompileSession.java @@ -84,7 +84,7 @@ public class CompileSession { } return false; } - final AnalyzingUtils instance = AnalyzingUtils.getInstance(JavaDefaultImports.JAVA_DEFAULT_IMPORTS); + final AnalyzingUtils instance = AnalyzingUtils.getInstance(JavaDefaultImports.JAVA_DEFAULT_IMPORTS, true); List allNamespaces = new ArrayList(mySourceFileNamespaces); allNamespaces.addAll(myLibrarySourceFileNamespaces); myBindingContext = instance.analyzeNamespaces(myEnvironment.getProject(), allNamespaces, Predicates.alwaysTrue(), JetControlFlowDataTraceFactory.EMPTY); diff --git a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/AnalyzerFacade.java b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/AnalyzerFacade.java index 6ad38396927..9be40fce997 100644 --- a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/AnalyzerFacade.java +++ b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/AnalyzerFacade.java @@ -34,7 +34,7 @@ public class AnalyzerFacade { } }; - private static final AnalyzingUtils ANALYZING_UTILS = AnalyzingUtils.getInstance(JavaDefaultImports.JAVA_DEFAULT_IMPORTS); + private static final AnalyzingUtils ANALYZING_UTILS = AnalyzingUtils.getInstance(JavaDefaultImports.JAVA_DEFAULT_IMPORTS, true); private final static Key> BINDING_CONTEXT = Key.create("BINDING_CONTEXT"); private static final Object lock = new Object(); diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/JetSemanticServices.java b/compiler/frontend/src/org/jetbrains/jet/lang/JetSemanticServices.java index 5a73728db37..c666125cd19 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/JetSemanticServices.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/JetSemanticServices.java @@ -17,7 +17,11 @@ public class JetSemanticServices { } public static JetSemanticServices createSemanticServices(Project project) { - return new JetSemanticServices(JetStandardLibrary.getJetStandardLibrary(project)); + return createSemanticServices(project, true); + } + + public static JetSemanticServices createSemanticServices(Project project, boolean loadFullLibrary) { + return new JetSemanticServices(JetStandardLibrary.getJetStandardLibrary(project, loadFullLibrary)); } private final JetStandardLibrary standardLibrary; diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/AnalyzingUtils.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/AnalyzingUtils.java index 483a5673739..2eb18914627 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/AnalyzingUtils.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/AnalyzingUtils.java @@ -28,8 +28,8 @@ import java.util.*; * @author abreslav */ public class AnalyzingUtils { - public static AnalyzingUtils getInstance(@NotNull ImportingStrategy importingStrategy) { - return new AnalyzingUtils(importingStrategy); + public static AnalyzingUtils getInstance(@NotNull ImportingStrategy importingStrategy, boolean loadStdlib) { + return new AnalyzingUtils(importingStrategy, loadStdlib); } public static void checkForSyntacticErrors(@NotNull PsiElement root) { @@ -53,9 +53,11 @@ public class AnalyzingUtils { } private final ImportingStrategy importingStrategy; + private final boolean loadStdlib; - private AnalyzingUtils(ImportingStrategy importingStrategy) { + private AnalyzingUtils(ImportingStrategy importingStrategy, boolean loadStdlib) { this.importingStrategy = importingStrategy; + this.loadStdlib = loadStdlib; } public BindingContext analyzeNamespace(@NotNull JetNamespace namespace, @NotNull JetControlFlowDataTraceFactory flowDataTraceFactory) { @@ -71,7 +73,7 @@ public class AnalyzingUtils { @NotNull Predicate filesToAnalyzeCompletely, @NotNull JetControlFlowDataTraceFactory flowDataTraceFactory) { BindingTraceContext bindingTraceContext = new BindingTraceContext(); - JetSemanticServices semanticServices = JetSemanticServices.createSemanticServices(project); + JetSemanticServices semanticServices = JetSemanticServices.createSemanticServices(project, loadStdlib); JetScope libraryScope = semanticServices.getStandardLibrary().getLibraryScope(); ModuleDescriptor owner = new ModuleDescriptor(""); diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/JetStandardLibrary.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/JetStandardLibrary.java index 62923713125..c90e935e043 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/types/JetStandardLibrary.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/JetStandardLibrary.java @@ -29,16 +29,24 @@ import java.util.Set; public class JetStandardLibrary { // TODO : consider releasing this memory - private static JetStandardLibrary cachedLibrary = null; + private static JetStandardLibrary cachedFullLibrary = null; + private static JetStandardLibrary cachedQuickLibrary = null; // private static final Map standardLibraryCache = new HashMap(); // TODO : double checked locking synchronized - public static JetStandardLibrary getJetStandardLibrary(@NotNull Project project) { - if (cachedLibrary == null) { - cachedLibrary = new JetStandardLibrary(project); + public static JetStandardLibrary getJetStandardLibrary(@NotNull Project project, boolean loadFullLibrary) { + if (loadFullLibrary) { + if (cachedFullLibrary == null) { + cachedFullLibrary = new JetStandardLibrary(project, true); + } + return cachedFullLibrary; + } else { + if (cachedQuickLibrary == null) { + cachedQuickLibrary = new JetStandardLibrary(project, false); + } + return cachedQuickLibrary; } - return cachedLibrary; // JetStandardLibrary standardLibrary = standardLibraryCache.get(project); // if (standardLibrary == null) { // standardLibrary = new JetStandardLibrary(project); @@ -47,6 +55,12 @@ public class JetStandardLibrary { // return standardLibrary; } + public static JetStandardLibrary getJetStandardLibrary(@NotNull Project project) { + return getJetStandardLibrary(project, true); + } + + private final boolean loadFullLibrary; + private JetScope libraryScope; private ClassDescriptor numberClass; @@ -122,20 +136,27 @@ public class JetStandardLibrary { private NamespaceDescriptor typeInfoNamespace; private Set typeInfoFunction; - private JetStandardLibrary(@NotNull Project project) { - // TODO : review - InputStream stream = JetStandardClasses.class.getClassLoader().getResourceAsStream("jet/Library.jet"); + private JetStandardLibrary(@NotNull Project project, boolean loadFullLibrary) { + this.loadFullLibrary = loadFullLibrary; + try { - //noinspection IOResourceOpenedButNotSafelyClosed - JetFile file = (JetFile) PsiFileFactory.getInstance(project).createFileFromText("Library.jet", - JetFileType.INSTANCE, FileUtil.loadTextAndClose(new InputStreamReader(stream))); + JetFile file = null; + if (loadFullLibrary) { + // TODO : review + InputStream stream = JetStandardClasses.class.getClassLoader().getResourceAsStream("jet/Library.jet"); + //noinspection IOResourceOpenedButNotSafelyClosed + file = (JetFile) PsiFileFactory.getInstance(project).createFileFromText("Library.jet", + JetFileType.INSTANCE, FileUtil.loadTextAndClose(new InputStreamReader(stream))); + } JetSemanticServices bootstrappingSemanticServices = JetSemanticServices.createSemanticServices(this); BindingTraceContext bindingTraceContext = new BindingTraceContext(); WritableScopeImpl writableScope = new WritableScopeImpl(JetStandardClasses.STANDARD_CLASSES, JetStandardClasses.STANDARD_CLASSES_NAMESPACE, RedeclarationHandler.THROW_EXCEPTION).setDebugName("Root bootstrap scope"); // this.libraryScope = bootstrappingTDA.process(JetStandardClasses.STANDARD_CLASSES, file.getRootNamespace().getDeclarations()); // bootstrappingTDA.process(writableScope, JetStandardClasses.STANDARD_CLASSES_NAMESPACE, file.getRootNamespace().getDeclarations()); - TopDownAnalyzer.processStandardLibraryNamespace(bootstrappingSemanticServices, bindingTraceContext, writableScope, JetStandardClasses.STANDARD_CLASSES_NAMESPACE, file.getRootNamespace()); + if (loadFullLibrary) { + TopDownAnalyzer.processStandardLibraryNamespace(bootstrappingSemanticServices, bindingTraceContext, writableScope, JetStandardClasses.STANDARD_CLASSES_NAMESPACE, file.getRootNamespace()); + } // this.libraryScope = JetStandardClasses.STANDARD_CLASSES_NAMESPACE.getMemberScope(); AnalyzingUtils.throwExceptionOnErrors(bindingTraceContext.getBindingContext()); @@ -153,6 +174,11 @@ public class JetStandardLibrary { private void initStdClasses() { if(libraryScope == null) { this.libraryScope = JetStandardClasses.STANDARD_CLASSES_NAMESPACE.getMemberScope(); + + if (!loadFullLibrary) { + return; + } + this.numberClass = (ClassDescriptor) libraryScope.getClassifier("Number"); this.byteClass = (ClassDescriptor) libraryScope.getClassifier("Byte"); this.charClass = (ClassDescriptor) libraryScope.getClassifier("Char"); diff --git a/compiler/testData/checkerWithErrorTypes/full/Casts.jet b/compiler/testData/checkerWithErrorTypes/full/Casts.jet index 163619fd4d0..e34a90b945a 100644 --- a/compiler/testData/checkerWithErrorTypes/full/Casts.jet +++ b/compiler/testData/checkerWithErrorTypes/full/Casts.jet @@ -1,3 +1,5 @@ +// -JDK + fun test() : Unit { var x : Int? = 0 var y : Int = 0 @@ -13,4 +15,4 @@ fun test() : Unit { x as? Int? : Int? y as? Int? : Int? () -} \ No newline at end of file +} diff --git a/compiler/tests/org/jetbrains/jet/checkers/CheckerTestUtilTest.java b/compiler/tests/org/jetbrains/jet/checkers/CheckerTestUtilTest.java index 06ffed74d04..8663cc4d9fc 100644 --- a/compiler/tests/org/jetbrains/jet/checkers/CheckerTestUtilTest.java +++ b/compiler/tests/org/jetbrains/jet/checkers/CheckerTestUtilTest.java @@ -81,7 +81,7 @@ public class CheckerTestUtilTest extends JetLiteFixture { } public void test(PsiFile psiFile) { - BindingContext bindingContext = AnalyzerFacade.analyzeFileWithCache(AnalyzingUtils.getInstance(ImportingStrategy.NONE), (JetFile) psiFile, AnalyzerFacade.SINGLE_DECLARATION_PROVIDER); + BindingContext bindingContext = AnalyzerFacade.analyzeFileWithCache(AnalyzingUtils.getInstance(ImportingStrategy.NONE, true), (JetFile) psiFile, AnalyzerFacade.SINGLE_DECLARATION_PROVIDER); String expectedText = CheckerTestUtil.addDiagnosticMarkersToText(psiFile, bindingContext).toString(); List diagnosedRanges = Lists.newArrayList(); diff --git a/compiler/tests/org/jetbrains/jet/checkers/FullJetPsiCheckerTest.java b/compiler/tests/org/jetbrains/jet/checkers/FullJetPsiCheckerTest.java index 986f6501d10..5b4646d207e 100644 --- a/compiler/tests/org/jetbrains/jet/checkers/FullJetPsiCheckerTest.java +++ b/compiler/tests/org/jetbrains/jet/checkers/FullJetPsiCheckerTest.java @@ -8,8 +8,11 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.JetLiteFixture; import org.jetbrains.jet.JetTestCaseBuilder; import org.jetbrains.jet.lang.diagnostics.DiagnosticUtils; +import org.jetbrains.jet.lang.resolve.AnalyzingUtils; import org.jetbrains.jet.lang.resolve.BindingContext; +import org.jetbrains.jet.lang.resolve.ImportingStrategy; import org.jetbrains.jet.lang.resolve.java.AnalyzerFacade; +import org.jetbrains.jet.lang.resolve.java.JavaDefaultImports; import java.util.List; @@ -42,7 +45,13 @@ public class FullJetPsiCheckerTest extends JetLiteFixture { String clearText = CheckerTestUtil.parseDiagnosedRanges(expectedText, diagnosedRanges); myFile = createPsiFile(myName, clearText); - BindingContext bindingContext = AnalyzerFacade.analyzeFileWithCache(myFile, AnalyzerFacade.SINGLE_DECLARATION_PROVIDER); + + boolean loadStdlib = !expectedText.contains("-STDLIB"); + boolean importJdk = !expectedText.contains("-JDK"); + + ImportingStrategy importingStrategy = importJdk ? JavaDefaultImports.JAVA_DEFAULT_IMPORTS : ImportingStrategy.NONE; + + BindingContext bindingContext = AnalyzerFacade.analyzeFileWithCache(AnalyzingUtils.getInstance(importingStrategy, loadStdlib), myFile, AnalyzerFacade.SINGLE_DECLARATION_PROVIDER); CheckerTestUtil.diagnosticsDiff(diagnosedRanges, bindingContext.getDiagnostics(), new CheckerTestUtil.DiagnosticDiffCallbacks() { @Override diff --git a/compiler/tests/org/jetbrains/jet/checkers/QuickJetPsiCheckerTest.java b/compiler/tests/org/jetbrains/jet/checkers/QuickJetPsiCheckerTest.java index d6ead64cdf1..a69f04a21c7 100644 --- a/compiler/tests/org/jetbrains/jet/checkers/QuickJetPsiCheckerTest.java +++ b/compiler/tests/org/jetbrains/jet/checkers/QuickJetPsiCheckerTest.java @@ -13,6 +13,7 @@ import org.jetbrains.jet.lang.resolve.AnalyzingUtils; import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.ImportingStrategy; import org.jetbrains.jet.lang.resolve.java.AnalyzerFacade; +import org.jetbrains.jet.lang.resolve.java.JavaDefaultImports; import java.util.List; @@ -40,7 +41,12 @@ public class QuickJetPsiCheckerTest extends JetLiteFixture { createAndCheckPsiFile(name, clearText); JetFile jetFile = (JetFile) myFile; - BindingContext bindingContext = AnalyzerFacade.analyzeFileWithCache(AnalyzingUtils.getInstance(ImportingStrategy.NONE), jetFile, AnalyzerFacade.SINGLE_DECLARATION_PROVIDER); + boolean loadStdlib = !expectedText.contains("-STDLIB"); + boolean importJdk = expectedText.contains("+JDK"); + + ImportingStrategy importingStrategy = importJdk ? JavaDefaultImports.JAVA_DEFAULT_IMPORTS : ImportingStrategy.NONE; + + BindingContext bindingContext = AnalyzerFacade.analyzeFileWithCache(AnalyzingUtils.getInstance(importingStrategy, loadStdlib), jetFile, AnalyzerFacade.SINGLE_DECLARATION_PROVIDER); CheckerTestUtil.diagnosticsDiff(diagnosedRanges, bindingContext.getDiagnostics(), new CheckerTestUtil.DiagnosticDiffCallbacks() { @Override diff --git a/compiler/tests/org/jetbrains/jet/resolve/JetResolveTest.java b/compiler/tests/org/jetbrains/jet/resolve/JetResolveTest.java index ca206ae88c0..f4bf68958a1 100644 --- a/compiler/tests/org/jetbrains/jet/resolve/JetResolveTest.java +++ b/compiler/tests/org/jetbrains/jet/resolve/JetResolveTest.java @@ -44,7 +44,7 @@ public class JetResolveTest extends ExtensibleResolveTestCase { @Override protected ExpectedResolveData getExpectedResolveData() { Project project = getProject(); - JetStandardLibrary lib = JetStandardLibrary.getJetStandardLibrary(project); + JetStandardLibrary lib = JetStandardLibrary.getJetStandardLibrary(project, true); Map nameToDescriptor = new HashMap(); nameToDescriptor.put("std::Int.plus(Int)", standardFunction(lib.getInt(), "plus", lib.getIntType())); FunctionDescriptor descriptorForGet = standardFunction(lib.getArray(), Collections.singletonList(new TypeProjection(lib.getIntType())), "get", lib.getIntType()); diff --git a/idea/src/org/jetbrains/jet/plugin/compiler/JetCompiler.java b/idea/src/org/jetbrains/jet/plugin/compiler/JetCompiler.java index 0a16cb56f58..0a34294d80f 100644 --- a/idea/src/org/jetbrains/jet/plugin/compiler/JetCompiler.java +++ b/idea/src/org/jetbrains/jet/plugin/compiler/JetCompiler.java @@ -80,7 +80,7 @@ public class JetCompiler implements TranslatingCompiler { } } - BindingContext bindingContext = AnalyzingUtils.getInstance(JavaDefaultImports.JAVA_DEFAULT_IMPORTS).analyzeNamespaces( + BindingContext bindingContext = AnalyzingUtils.getInstance(JavaDefaultImports.JAVA_DEFAULT_IMPORTS, true).analyzeNamespaces( compileContext.getProject(), namespaces, Predicates.alwaysTrue(), JetControlFlowDataTraceFactory.EMPTY); diff --git a/idea/src/org/jetbrains/jet/plugin/java/JavaElementFinder.java b/idea/src/org/jetbrains/jet/plugin/java/JavaElementFinder.java index 7cfa0fe1b86..1967f70b22e 100644 --- a/idea/src/org/jetbrains/jet/plugin/java/JavaElementFinder.java +++ b/idea/src/org/jetbrains/jet/plugin/java/JavaElementFinder.java @@ -211,7 +211,7 @@ public class JavaElementFinder extends PsiElementFinder { if (dirty.size() > 0) { - final BindingContext context = AnalyzingUtils.getInstance(JavaDefaultImports.JAVA_DEFAULT_IMPORTS).shallowAnalyzeFiles(dirty); + final BindingContext context = AnalyzingUtils.getInstance(JavaDefaultImports.JAVA_DEFAULT_IMPORTS, true).shallowAnalyzeFiles(dirty); state.compileCorrectNamespaces(context, AnalyzingUtils.collectRootNamespaces(dirty)); state.getFactory().files(); }