From 8a3d3d21bd22e2e7c1391308ca0f7952f9821ea5 Mon Sep 17 00:00:00 2001 From: Stepan Koltsov Date: Fri, 8 Jun 2012 04:19:23 +0400 Subject: [PATCH] better script class name --- .../jet/codegen/ClosureAnnotator.java | 67 +++++++++++++++---- .../jet/codegen/ExpressionCodegen.java | 8 ++- .../jet/codegen/GenerationState.java | 7 ++ .../jetbrains/jet/codegen/JetTypeMapper.java | 4 +- .../jetbrains/jet/codegen/ScriptCodegen.java | 17 +++-- .../compiler/KotlinToJVMBytecodeCompiler.java | 2 +- .../jet/cli/jvm/repl/ReplInterpreter.java | 3 +- .../jet/lang/psi/JetNamedDeclaration.java | 13 +++- .../jet/codegen/CodegenTestCase.java | 2 +- 9 files changed, 97 insertions(+), 26 deletions(-) diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/ClosureAnnotator.java b/compiler/backend/src/org/jetbrains/jet/codegen/ClosureAnnotator.java index 707b824cece..ef5926695f1 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/ClosureAnnotator.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/ClosureAnnotator.java @@ -42,6 +42,8 @@ public class ClosureAnnotator { private final Map classNamesForAnonymousClasses = new HashMap(); private final Map classNamesForClassDescriptor = new HashMap(); private final Map anonymousSubclassesCount = new HashMap(); + private final Map classNameForScript = new HashMap(); + private final Set scriptClassNames = new HashSet(); private final Map classesForFunctions = new HashMap(); private final Map enclosing = new HashMap(); @@ -86,27 +88,66 @@ public class ClosureAnnotator { return classDescriptor; } + public void registerClassNameForScript(@NotNull ScriptDescriptor scriptDescriptor, @NotNull JvmClassName className) { + JvmClassName oldName = classNameForScript.put(scriptDescriptor, className); + if (oldName != null) { + throw new IllegalStateException("Rewrite at key " + scriptDescriptor + " for name"); + } + + if (!scriptClassNames.add(className)) { + throw new IllegalStateException("More than one script has class name " + className); + } + + ClassDescriptorImpl classDescriptor = new ClassDescriptorImpl( + scriptDescriptor, + Collections.emptyList(), + Name.special("")); + recordName(classDescriptor, className); + classDescriptor.initialize( + false, + Collections.emptyList(), + Collections.singletonList(JetStandardClasses.getAnyType()), + JetScope.EMPTY, + Collections.emptySet(), + null); + + ClassDescriptorImpl oldDescriptor = classesForFunctions.put(scriptDescriptor, classDescriptor); + if (oldDescriptor != null) { + throw new IllegalStateException("Rewrite at key " + scriptDescriptor + " for class"); + } + } + + public void registerClassNameForScript(@NotNull JetScript jetScript, @NotNull JvmClassName className) { + ScriptDescriptor descriptor = bindingContext.get(BindingContext.SCRIPT, jetScript); + if (descriptor == null) { + throw new IllegalStateException("Descriptor is not found for PSI " + jetScript); + } + registerClassNameForScript(descriptor, className); + } + @NotNull public ClassDescriptor classDescriptorForScrpitDescriptor(@NotNull ScriptDescriptor scriptDescriptor) { ClassDescriptorImpl classDescriptor = classesForFunctions.get(scriptDescriptor); if (classDescriptor == null) { - classDescriptor = new ClassDescriptorImpl( - scriptDescriptor, - Collections.emptyList(), - Name.special("