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("