/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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.codehaus.groovy.control;

import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyRuntimeException;
import groovy.transform.CompilationUnitAware;
import org.codehaus.groovy.GroovyBugError;
import org.codehaus.groovy.ast.AnnotationNode;
import org.codehaus.groovy.ast.ClassHelper;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.CompileUnit;
import org.codehaus.groovy.ast.GroovyClassVisitor;
import org.codehaus.groovy.ast.InnerClassNode;
import org.codehaus.groovy.ast.ModuleNode;
import org.codehaus.groovy.classgen.AsmClassGenerator;
import org.codehaus.groovy.classgen.ClassCompletionVerifier;
import org.codehaus.groovy.classgen.EnumCompletionVisitor;
import org.codehaus.groovy.classgen.EnumVisitor;
import org.codehaus.groovy.classgen.ExtendedVerifier;
import org.codehaus.groovy.classgen.GeneratorContext;
import org.codehaus.groovy.classgen.InnerClassCompletionVisitor;
import org.codehaus.groovy.classgen.InnerClassVisitor;
import org.codehaus.groovy.classgen.Verifier;
import org.codehaus.groovy.control.customizers.CompilationCustomizer;
import org.codehaus.groovy.control.io.InputStreamReaderSource;
import org.codehaus.groovy.control.io.ReaderSource;
import org.codehaus.groovy.control.messages.ExceptionMessage;
import org.codehaus.groovy.control.messages.Message;
import org.codehaus.groovy.syntax.RuntimeParserException;
import org.codehaus.groovy.syntax.SyntaxException;
import org.codehaus.groovy.tools.GroovyClass;
import org.codehaus.groovy.transform.ASTTransformationVisitor;
import org.codehaus.groovy.transform.AnnotationCollectorTransform;
import org.codehaus.groovy.transform.trait.TraitComposer;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.security.CodeSource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Queue;
import java.util.Set;

import static org.codehaus.groovy.transform.sc.StaticCompilationMetadataKeys.DYNAMIC_OUTER_NODE_CALLBACK;

/**
 * The CompilationUnit collects all compilation data as it is generated by the compiler system.
 * You can use this object to add additional source units to the compilation, or force the
 * compilation to be run again (to affect only the deltas).
 * <p>
 * You can also add PhaseOperations to this compilation using the addPhaseOperation method.
 * This is commonly used when you want to wire a new AST Transformation into the compilation.
 */
public class CompilationUnit extends ProcessingUnit {

    /** The overall AST for this CompilationUnit. */
    protected CompileUnit ast; // TODO: Switch to private and access through getAST().

    /** The source units from which this unit is built. */
    protected Map<String, SourceUnit> sources = new LinkedHashMap<>();
    protected Queue<SourceUnit> queuedSources = new LinkedList<>();

    /** The classes generated during classgen. */
    private List<GroovyClass> generatedClasses = new ArrayList<>();

    private Deque<PhaseOperation>[] phaseOperations;
    private Deque<PhaseOperation>[] newPhaseOperations;
    {
        final int n = Phases.ALL + 1;
        phaseOperations = new Deque[n];
        newPhaseOperations = new Deque[n];
        for (int i = 0; i < n; i += 1) {
            phaseOperations[i] = new LinkedList<>();
            newPhaseOperations[i] = new LinkedList<>();
        }
    }

    /** If set, outputs a little more information during compilation when errors occur. */
    protected boolean debug;
    /** True after the first {@link #configure(CompilerConfiguration)} operation. */
    protected boolean configured;

    /** A callback called during the {@code classgen} phase of compilation */
    protected ClassgenCallback classgenCallback;
    /** A callback for use during {@link #compile()} */
    protected ProgressCallback progressCallback;

    protected ClassNodeResolver classNodeResolver = new ClassNodeResolver();
    protected ResolveVisitor resolveVisitor = new ResolveVisitor(this);

    /** The AST transformations state data. */
    protected ASTTransformationsContext astTransformationsContext;

    private Set<javax.tools.JavaFileObject> javaCompilationUnitSet = new HashSet<>();

    /**
     * Initializes the CompilationUnit with defaults.
     */
    public CompilationUnit() {
        this(null, null, null);
    }

    /**
     * Initializes the CompilationUnit with defaults except for class loader.
     */
    public CompilationUnit(final GroovyClassLoader loader) {
        this(null, null, loader);
    }

    /**
     * Initializes the CompilationUnit with no security considerations.
     */
    public CompilationUnit(final CompilerConfiguration configuration) {
        this(configuration, null, null);
    }

    /**
     * Initializes the CompilationUnit with a CodeSource for controlling
     * security stuff and a class loader for loading classes.
     */
    public CompilationUnit(final CompilerConfiguration configuration, final CodeSource codeSource, final GroovyClassLoader loader) {
        this(configuration, codeSource, loader, null);
    }

    /**
     * Initializes the CompilationUnit with a CodeSource for controlling
     * security stuff, a class loader for loading classes, and a class
     * loader for loading AST transformations.
     * <p>
     * <b>Note</b>: The transform loader must be able to load compiler classes.
     * That means {@link #classLoader} must be at last a parent to {@code transformLoader}.
     * The other loader has no such constraint.
     *
     * @param transformLoader - the loader for transforms
     * @param loader          - loader used to resolve classes against during compilation
     * @param codeSource      - security setting for the compilation
     * @param configuration   - compilation configuration
     */
    public CompilationUnit(final CompilerConfiguration configuration, final CodeSource codeSource,
                           final GroovyClassLoader loader, final GroovyClassLoader transformLoader) {
        super(configuration, loader, null);

        this.astTransformationsContext = new ASTTransformationsContext(this, transformLoader);
        this.ast = new CompileUnit(getClassLoader(), codeSource, getConfiguration());

        addPhaseOperations();
        applyCompilationCustomizers();
    }

    private void addPhaseOperations() {
        addPhaseOperation(SourceUnit::parse, Phases.PARSING);

        addPhaseOperation(source -> {
            source.convert();
            // add module to compile unit
            getAST().addModule(source.getAST());
            Optional.ofNullable(getProgressCallback())
                .ifPresent(callback -> callback.call(source, getPhase()));
        }, Phases.CONVERSION);

        addPhaseOperation((final SourceUnit source, final GeneratorContext context, final ClassNode classNode) -> {
            GroovyClassVisitor visitor = new EnumVisitor(this, source);
            visitor.visitClass(classNode);
        }, Phases.CONVERSION);
        addPhaseOperation((final SourceUnit source, final GeneratorContext context, final ClassNode classNode) -> {
            GroovyClassVisitor visitor = new PlaceholderVisitor(this, source);
            visitor.visitClass(classNode);
        }, Phases.CONVERSION);

        addPhaseOperation(source -> {
            try {
                resolveVisitor.phase = 1; // resolve head of each class
                resolveVisitor.setClassNodeResolver(classNodeResolver);
                for (ClassNode classNode : source.getAST().getClasses()) {
                    resolveVisitor.startResolving(classNode, source);
                }
            } finally {
                resolveVisitor.phase = 0;
            }
        }, Phases.SEMANTIC_ANALYSIS);

        addPhaseOperation(source -> {
            try {
                resolveVisitor.phase = 2; // resolve body of each class
                for (ClassNode classNode : source.getAST().getClasses()) {
                    resolveVisitor.startResolving(classNode, source);
                }
            } finally {
                resolveVisitor.phase = 0;
            }
        }, Phases.SEMANTIC_ANALYSIS);

        addPhaseOperation((final SourceUnit source, final GeneratorContext context, final ClassNode classNode) -> {
            GroovyClassVisitor visitor = new StaticImportVisitor(classNode, source);
            visitor.visitClass(classNode);
        }, Phases.SEMANTIC_ANALYSIS);

        addPhaseOperation((final SourceUnit source, final GeneratorContext context, final ClassNode classNode) -> {
            GroovyClassVisitor visitor = new InnerClassVisitor(this, source);
            visitor.visitClass(classNode);
        }, Phases.SEMANTIC_ANALYSIS);

        addPhaseOperation((final SourceUnit source, final GeneratorContext context, final ClassNode classNode) -> {
            if (!classNode.isSynthetic()) {
                GroovyClassVisitor visitor = new GenericsVisitor(source);
                visitor.visitClass(classNode);
            }
        }, Phases.SEMANTIC_ANALYSIS);

        addPhaseOperation((final SourceUnit source, final GeneratorContext context, final ClassNode classNode) -> {
            AnnotationCollectorTransform.ClassChanger xformer = new AnnotationCollectorTransform.ClassChanger();
            xformer.transformClass(classNode);
        }, Phases.SEMANTIC_ANALYSIS);

        addPhaseOperation((final SourceUnit source, final GeneratorContext context, final ClassNode classNode) -> {
            TraitComposer.doExtendTraits(classNode, source, this);
        }, Phases.CANONICALIZATION);

        addPhaseOperation(source -> {
            List<ClassNode> classes = source.getAST().getClasses();
            for (ClassNode node : classes) {
                CompileUnit cu = node.getCompileUnit();
                for (Iterator<String> it = cu.getClassesToCompile().keySet().iterator(); it.hasNext(); ) {
                    String name = it.next();
                    StringBuilder message = new StringBuilder("Compilation incomplete: expected to find the class ")
                            .append(name)
                            .append(" in ")
                            .append(source.getName())
                            .append(", but the file ");
                    if (classes.isEmpty()) {
                        message.append("seems not to contain any classes");
                    } else {
                        message.append("contains the classes: ");
                        boolean first = true;
                        for (ClassNode cn : classes) {
                            if (first) {
                                first = false;
                            } else {
                                message.append(", ");
                            }
                            message.append(cn.getName());
                        }
                    }

                    getErrorCollector().addErrorAndContinue(Message.create(message.toString(), this));

                    it.remove();
                }
            }
        }, Phases.CANONICALIZATION);

        addPhaseOperation(source -> {
            for (ClassNode cn : source.getAST().getClasses()) {
                // GROOVY-10540: add GroovyObject before STC and classgen
                if (!cn.isInterface() && !cn.isDerivedFromGroovyObject()) {
                    boolean cs = false, pojo = false, trait = false;
                    for (AnnotationNode an : cn.getAnnotations()) {
                        switch (an.getClassNode().getName()) {
                        case "groovy.transform.CompileStatic":
                            cs = true; break;
                        case "groovy.transform.stc.POJO":
                            pojo = true; break;
                        case "groovy.transform.Trait":
                            trait = true; break;
                        }
                    }
                    if (!(cs && pojo) && !trait)
                        cn.addInterface(ClassHelper.GROOVY_OBJECT_TYPE);
                }
            }
        }, Phases.INSTRUCTION_SELECTION);

        addPhaseOperation(verification, Phases.CLASS_GENERATION);

        addPhaseOperation(classgen, Phases.CLASS_GENERATION);

        addPhaseOperation((final SourceUnit source) -> {
            // GROOVY-10687: nest members include closure and anonymous inner classes
            // materialized while bytecode is generated, so the NestMembers attribute
            // of each nest host is completed once all of its classes are generated
            for (ClassNode classNode : source.getAST().getClasses()) {
                if (classNode.getOuterClass() == null && classNode.getInnerClasses().hasNext()) {
                    addNestMembers(classNode);
                }
            }
        }, Phases.CLASS_GENERATION);

        addPhaseOperation(groovyClass -> {
            String name = groovyClass.getName().replace('.', File.separatorChar) + ".class";
            File path = new File(getConfiguration().getTargetDirectory(), name);

            // ensure the path is ready for the file
            File directory = path.getParentFile();
            if (directory != null && !directory.exists()) {
                directory.mkdirs();
            }

            // create the file and write out the data
            try (FileOutputStream stream = new FileOutputStream(path)) {
                stream.write(groovyClass.getBytes());
            } catch (IOException e) {
                getErrorCollector().addError(Message.create(e.getMessage(), this));
            }
        });

        ASTTransformationVisitor.addPhaseOperations(this);

        // post-transform operations:

        addPhaseOperation((final SourceUnit source, final GeneratorContext context, final ClassNode classNode) -> {
            StaticVerifier verifier = new StaticVerifier();
            verifier.visitClass(classNode, source);
        }, Phases.SEMANTIC_ANALYSIS);

        addPhaseOperation((final SourceUnit source, final GeneratorContext context, final ClassNode classNode) -> {
            GroovyClassVisitor visitor = new InnerClassCompletionVisitor(this, source);
            visitor.visitClass(classNode);

            visitor = new EnumCompletionVisitor(this, source);
            visitor.visitClass(classNode);
        }, Phases.CANONICALIZATION);

        addPhaseOperation((final SourceUnit source, final GeneratorContext context, final ClassNode classNode) -> {
            Object callback = classNode.getNodeMetaData(DYNAMIC_OUTER_NODE_CALLBACK);
            if (callback instanceof IPrimaryClassNodeOperation) {
                ((IPrimaryClassNodeOperation) callback).call(source, context, classNode);
                classNode.removeNodeMetaData(DYNAMIC_OUTER_NODE_CALLBACK);
            }
        }, Phases.INSTRUCTION_SELECTION);
    }

    private void applyCompilationCustomizers() {
        for (CompilationCustomizer customizer : getConfiguration().getCompilationCustomizers()) {
            if (customizer instanceof CompilationUnitAware) {
                ((CompilationUnitAware) customizer).setCompilationUnit(this);
            }
            addPhaseOperation(customizer, customizer.getPhase().getPhaseNumber());
        }
    }

    /**
     * Adds a Groovy class operation to the output phase.
     *
     * @param op the operation to run for generated classes
     */
    public void addPhaseOperation(final IGroovyClassOperation op) {
        phaseOperations[Phases.OUTPUT].addFirst(op);
    }

    /**
     * Adds a source-unit operation to the supplied phase.
     *
     * @param op the operation to add
     * @param phase the phase in which to run it
     */
    public void addPhaseOperation(final ISourceUnitOperation op, final int phase) {
        validatePhase(phase);
        phaseOperations[phase].add(op);
    }

    /**
     * Adds a primary-class operation to the supplied phase.
     *
     * @param op the operation to add
     * @param phase the phase in which to run it
     */
    public void addPhaseOperation(final IPrimaryClassNodeOperation op, final int phase) {
        validatePhase(phase);
        phaseOperations[phase].add(op);
    }

    /**
     * Adds a primary-class operation at the front of the supplied phase queue.
     *
     * @param op the operation to add
     * @param phase the phase in which to run it
     */
    public void addFirstPhaseOperation(final IPrimaryClassNodeOperation op, final int phase) {
        validatePhase(phase);
        phaseOperations[phase].addFirst(op);
    }

    /**
     * Adds a source-unit operation that becomes active only after the current phase queue drains.
     *
     * @param op the operation to add
     * @param phase the phase in which to run it
     */
    public void addNewPhaseOperation(final ISourceUnitOperation op, final int phase) {
        validatePhase(phase);
        newPhaseOperations[phase].add(op);
    }

    private static void validatePhase(final int phase) {
        if (phase < 1 || phase > Phases.ALL) {
            throw new IllegalArgumentException("phase " + phase + " is unknown");
        }
    }

    /**
     * Configures its debugging mode and classloader classpath from a given compiler configuration.
     * This cannot be done more than once due to limitations in {@link java.net.URLClassLoader URLClassLoader}.
     */
    @Override
    public void configure(final CompilerConfiguration configuration) {
        super.configure(configuration);
        this.debug = getConfiguration().getDebug();
        this.configured = true;
    }

    /**
     * Returns the CompileUnit that roots our AST.
     */
    public CompileUnit getAST() {
        return this.ast;
    }

    /**
     * Get the GroovyClasses generated by compile().
     */
    public List<GroovyClass> getClasses() {
        return generatedClasses;
    }

    /**
     * Convenience routine to get the first ClassNode, for
     * when you are sure there is only one.
     */
    public ClassNode getFirstClassNode() {
        return getAST().getModules().get(0).getClasses().get(0);
    }

    /**
     * Convenience routine to get the named ClassNode.
     */
    public ClassNode getClassNode(final String name) {
        ClassNode[] result = new ClassNode[1];
        IPrimaryClassNodeOperation handler = (source, context, classNode) -> {
            if (classNode.getName().equals(name)) {
                result[0] = classNode;
            }
        };
        try {
            handler.doPhaseOperation(this);
        } catch (CompilationFailedException e) {
            if (debug) e.printStackTrace();
        }
        return result[0];
    }

    /**
     * @return the AST transformations current context
     */
    public ASTTransformationsContext getASTTransformationsContext() {
        return astTransformationsContext;
    }

    /**
     * Returns the resolver used for on-demand class lookup.
     *
     * @return the active class-node resolver
     */
    public ClassNodeResolver getClassNodeResolver() {
        return classNodeResolver;
    }

    /**
     * Replaces the resolver used for on-demand class lookup.
     *
     * @param classNodeResolver the resolver to install
     */
    public void setClassNodeResolver(final ClassNodeResolver classNodeResolver) {
        this.classNodeResolver = classNodeResolver;
    }

    /**
     * Returns the queued Java compilation units used for joint compilation.
     *
     * @return the tracked Java compilation units
     */
    public Set<javax.tools.JavaFileObject> getJavaCompilationUnitSet() {
        return javaCompilationUnitSet;
    }

    /**
     * Adds Java compilation units to the joint-compilation set.
     *
     * @param javaCompilationUnitSet the Java units to add
     */
    public void addJavaCompilationUnits(final Set<javax.tools.JavaFileObject> javaCompilationUnitSet) {
        this.javaCompilationUnitSet.addAll(javaCompilationUnitSet);
    }

    /**
     * Returns the class loader for loading AST transformations.
     */
    public GroovyClassLoader getTransformLoader() {
        return Optional.ofNullable(getASTTransformationsContext().getTransformLoader()).orElseGet(this::getClassLoader);
    }

    //---------------------------------------------------------------------------
    // SOURCE CREATION

    /**
     * Adds a set of file paths to the unit.
     */
    public void addSources(final String[] paths) {
        for (String path : paths) {
            addSource(new File(path));
        }
    }

    /**
     * Adds a set of source files to the unit.
     */
    public void addSources(final File[] files) {
        for (File file : files) {
            addSource(file);
        }
    }

    /**
     * Adds a source file to the unit.
     */
    public SourceUnit addSource(final File file) {
        return addSource(new SourceUnit(file, getConfiguration(), getClassLoader(), getErrorCollector()));
    }

    /**
     * Adds a source file to the unit.
     */
    public SourceUnit addSource(final URL url) {
        return addSource(new SourceUnit(url, getConfiguration(), getClassLoader(), getErrorCollector()));
    }

    /**
     * Adds a InputStream source to the unit.
     */
    public SourceUnit addSource(final String name, final InputStream stream) {
        ReaderSource source = new InputStreamReaderSource(stream, getConfiguration());
        return addSource(new SourceUnit(name, source, getConfiguration(), getClassLoader(), getErrorCollector()));
    }

    /**
     * Adds an in-memory script source to this compilation unit.
     *
     * @param name the source name
     * @param scriptText the script text
     * @return the created source unit
     */
    public SourceUnit addSource(final String name, final String scriptText) {
        return addSource(new SourceUnit(name, scriptText, getConfiguration(), getClassLoader(), getErrorCollector()));
    }

    /**
     * Adds a SourceUnit to the unit.
     */
    public SourceUnit addSource(final SourceUnit source) {
        String name = source.getName();
        source.setClassLoader(getClassLoader());
        for (SourceUnit su : queuedSources) {
            if (name.equals(su.getName())) return su;
        }
        queuedSources.add(source);
        return source;
    }

    /**
     * Returns an iterator on the unit's SourceUnits.
     */
    public Iterator<SourceUnit> iterator() {
        return new Iterator<SourceUnit>() {
            private Iterator<String> nameIterator = sources.keySet().iterator();
            @Override
            public boolean hasNext() {
                return nameIterator.hasNext();
            }
            @Override
            public SourceUnit next() {
                String name = nameIterator.next();
                return sources.get(name);
            }
            @Override
            public void remove() {
                throw new UnsupportedOperationException();
            }
        };
    }

    /**
     * Adds a ClassNode directly to the unit (i.e. without source).
     * WARNING: the source is needed for error reporting, using
     * this method without setting a SourceUnit will cause
     * NullPointerExceptions
     */
    public void addClassNode(final ClassNode node) {
        ModuleNode module = new ModuleNode(getAST());
        getAST().addModule(module);
        module.addClass(node);
    }

    //---------------------------------------------------------------------------
    // EXTERNAL CALLBACKS

    /**
     * A callback interface you can use during the {@code classgen}
     * phase of compilation as the compiler traverses the ClassNode tree.
     * You will be called-back for each primary and inner class.
     * Use setClassgenCallback() before running compile() to set your callback.
     */
    @FunctionalInterface
    public interface ClassgenCallback {
        /**
         * Called after bytecode has been generated for a class.
         *
         * @param writer the ASM visitor used to emit the class
         * @param node the generated class node
         * @throws CompilationFailedException if callback processing fails
         */
        void call(ClassVisitor writer, ClassNode node) throws CompilationFailedException;
    }

    /**
     * Returns the callback invoked after class generation.
     *
     * @return the class-generation callback, or {@code null}
     */
    public ClassgenCallback getClassgenCallback() {
        return classgenCallback;
    }

    /**
     * Sets a ClassgenCallback.  You can have only one, and setting
     * it to {@code null} removes any existing setting.
     */
    public void setClassgenCallback(final ClassgenCallback visitor) {
        this.classgenCallback = visitor;
    }

    /**
     * A callback interface you can use to get a callback after every
     * unit of the compile process.  You will be called-back with a
     * ProcessingUnit and a phase indicator.  Use setProgressCallback()
     * before running compile() to set your callback.
     */
    @FunctionalInterface
    public interface ProgressCallback {
        /**
         * Called after a compilation phase finishes for a processing unit.
         *
         * @param context the processing unit that advanced
         * @param phase the phase that just completed
         * @throws CompilationFailedException if callback processing fails
         */
        void call(ProcessingUnit context, int phase) throws CompilationFailedException;
    }

    /**
     * Returns the callback invoked after each phase transition.
     *
     * @return the progress callback, or {@code null}
     */
    public ProgressCallback getProgressCallback() {
        return progressCallback;
    }

    /**
     * Sets a ProgressCallback.  You can have only one, and setting
     * it to {@code null} removes any existing setting.
     */
    public void setProgressCallback(final ProgressCallback callback) {
        this.progressCallback = callback;
    }

    //---------------------------------------------------------------------------
    // ACTIONS

    /**
     * Compiles the compilation unit from sources through the configured target
     * phase; a synonym for {@code compile(Phases.ALL)} unless an earlier phase
     * is set via {@link CompilerConfiguration#setTargetPhase(int)} (GROOVY-12204).
     */
    public void compile() throws CompilationFailedException {
        compile(configuration.getTargetPhase());
    }

    /**
     * Compiles the compilation unit from sources.
     */
    public void compile(int throughPhase) throws CompilationFailedException {
        // to support incremental compilation, always restart the compiler;
        // individual passes are responsible for not re-processing old code
        gotoPhase(Phases.INITIALIZATION);
        throughPhase = Math.min(throughPhase, Phases.ALL);

        while (throughPhase >= phase && phase <= Phases.ALL) {
            if (phase == Phases.CONVERSION) {
                (sources.size() > 1 && Boolean.TRUE.equals(configuration.getOptimizationOptions().get(CompilerConfiguration.PARALLEL_PARSE))
                        ? sources.values().parallelStream() : sources.values().stream()
                ).forEach(SourceUnit::buildAST);
            }
            try {
                processPhaseOperations(phase);
            } catch (ResolveVisitor.Interrupt x) {
                assert !queuedSources.isEmpty();
            }
            if (dequeued()) continue; // bring new sources into phase

            // Grab processing may have brought in new AST transforms into various phases, process them as well
            processNewPhaseOperations(phase);

            Optional.ofNullable(getProgressCallback())
                .ifPresent(callback -> callback.call(this, phase));
            completePhase();
            mark();

            gotoPhase(phase + 1);

            if (phase == Phases.CLASS_GENERATION) {
                getAST().getModules().forEach(ModuleNode::sortClasses);
            }
        }

        getErrorCollector().failIfErrors();
    }

    private void processPhaseOperations(final int phase) {
        for (PhaseOperation op : phaseOperations[phase]) {
            op.doPhaseOperation(this);
        }
    }

    private void processNewPhaseOperations(final int phase) {
        recordPhaseOpsInAllOtherPhases(phase);
        Deque<PhaseOperation> currentPhaseNewOps = newPhaseOperations[phase];
        while (!currentPhaseNewOps.isEmpty()) {
            PhaseOperation operation = currentPhaseNewOps.removeFirst();
            // push this operation to master list and then process it
            phaseOperations[phase].add(operation);
            operation.doPhaseOperation(this);
            // if this operation has brought in more phase ops for ast transforms, keep recording them
            // in master list of other phases and keep processing them for this phase
            recordPhaseOpsInAllOtherPhases(phase);
            currentPhaseNewOps = newPhaseOperations[phase];
        }
    }

    private void recordPhaseOpsInAllOtherPhases(final int phase) {
        // apart from current phase, push new operations for every other phase in the master phase ops list
        for (int ph = Phases.INITIALIZATION; ph <= Phases.ALL; ph += 1) {
            if (ph != phase && !newPhaseOperations[ph].isEmpty()) {
                phaseOperations[ph].addAll(newPhaseOperations[ph]);
                newPhaseOperations[ph].clear();
            }
        }
    }

    /**
     * Dequeues any source units added through addSource and resets the compiler
     * phase to initialization.
     * <p>
     * Note: this does not mean a file is recompiled. If a SourceUnit has already
     * passed a phase it is skipped until a higher phase is reached.
     *
     * @return true if there was a queued source
     * @throws CompilationFailedException
     */
    protected boolean dequeued() throws CompilationFailedException {
        if (!queuedSources.isEmpty()) { SourceUnit unit;
            while ((unit = queuedSources.poll()) != null) {
                sources.put(unit.getName(), unit);
            }
            gotoPhase(Phases.INITIALIZATION);
            return true;
        }
        return false;
    }

    private final IPrimaryClassNodeOperation verification = new IPrimaryClassNodeOperation() {
        @Override
        public void call(final SourceUnit source, final GeneratorContext context, final ClassNode classNode) throws CompilationFailedException {
            new OptimizerVisitor(CompilationUnit.this).visitClass(classNode, source); // GROOVY-4272: repositioned from static import visitor

            GroovyClassVisitor visitor = new Verifier();
            try {
                visitor.visitClass(classNode);
            } catch (RuntimeParserException rpe) {
                getErrorCollector().addError(new SyntaxException(rpe.getMessage(), rpe.getNode()), source);
            }

            visitor = new LabelVerifier(source);
            visitor.visitClass(classNode);

            visitor = new InstanceOfVerifier() {
                @Override
                protected SourceUnit getSourceUnit() {
                    return source;
                }
            };
            visitor.visitClass(classNode);

            visitor = new ClassCompletionVerifier(source);
            visitor.visitClass(classNode);

            visitor = new ExtendedVerifier(source);
            visitor.visitClass(classNode);

            // because the class may be generated even if an error was found
            // and that class may have an invalid format we fail here if needed
            getErrorCollector().failIfErrors();
        }

        @Override
        public boolean needSortedInput() {
            return true;
        }
    };

    /**
     * Generates bytecode for a single {@code ClassNode}.
     */
    private final IPrimaryClassNodeOperation classgen = new IPrimaryClassNodeOperation() {
        @Override
        public void call(final SourceUnit source, final GeneratorContext context, final ClassNode classNode) throws CompilationFailedException {
            //
            // Prep the generator machinery
            //
            ClassVisitor classVisitor = createClassVisitor();

            String sourceName = (source == null ? classNode.getModule().getDescription() : source.getName());
            // only show the file name and its extension like javac does in its stacktraces rather than the full path
            // also takes care of both \ and / depending on the host compiling environment
            if (sourceName != null) {
                sourceName = sourceName.substring(Math.max(sourceName.lastIndexOf('\\'), sourceName.lastIndexOf('/')) + 1);
            }

            //
            // Run the generation and create the class (if required)
            //
            AsmClassGenerator visitor = new AsmClassGenerator(source, context, classVisitor, sourceName);
            visitor.visitClass(classNode);

            byte[] bytes = ((ClassWriter) classVisitor).toByteArray();
            getClasses().add(new GroovyClass(classNode.getName(), bytes));

            //
            // Handle any callback that's been set
            //
            if (classgenCallback != null) {
                classgenCallback.call(classVisitor, classNode);
            }

            //
            // Recurse for generated classes
            //
            Deque<ClassNode> innerClasses = visitor.getInnerClasses();
            while (!innerClasses.isEmpty()) {
                ClassNode innerClass = innerClasses.removeFirst();
                verification.call(source, context, innerClass);
                classgen.call(source, context, innerClass);
            }
        }

        @Override
        public boolean needSortedInput() {
            return true;
        }
    };

    /**
     * Creates the ASM class visitor used for bytecode generation.
     *
     * @return the class visitor used to emit bytecode
     */
    protected ClassVisitor createClassVisitor() {
        return new CachingClassWriter();
    }

    /**
     * Per-class {@link ClassWriter} that memoizes {@link #getCommonSuperClass}.
     * {@code COMPUTE_FRAMES} repeats the same internal-name pairs at merge points;
     * the writer (and its maps) live for one generated class and are then discarded.
     */
    private final class CachingClassWriter extends ClassWriter {
        private final Map<String, ClassNode> classNodeByInternalName = new HashMap<>(8);
        private final Map<String, Map<String, String>> commonSuperByPair = new HashMap<>(8);

        private CachingClassWriter() {
            super(COMPUTE_MAXS | COMPUTE_FRAMES);
        }

        @Override
        protected String getCommonSuperClass(final String type1, final String type2) {
            Map<String, String> bySecond = commonSuperByPair.get(type1);
            if (bySecond != null) {
                String cached = bySecond.get(type2);
                if (cached != null) {
                    return cached;
                }
            }
            ClassNode class1 = getClassNode(type1);
            ClassNode class2 = getClassNode(type2);
            if (class1 == null || class2 == null) {
                throw new GroovyBugError("Unable to determine common super class of " + type1 + " and " + type2);
            }
            ClassNode commonNode = getCommonSuperClassNode(class1, class2);
            String common;
            if (commonNode == class1) {
                common = type1;
            } else if (commonNode == class2) {
                common = type2;
            } else if (ClassHelper.isObjectType(commonNode)) {
                common = "java/lang/Object";
            } else {
                common = commonNode.getName().replace('.', '/');
            }
            commonSuperByPair.computeIfAbsent(type1, k -> new HashMap<>(4)).put(type2, common);
            commonSuperByPair.computeIfAbsent(type2, k -> new HashMap<>(4)).put(type1, common);
            return common;
        }

        private ClassNode getClassNode(final String internalName) {
            ClassNode cached = classNodeByInternalName.get(internalName);
            if (cached != null) {
                return cached;
            }
            String name = internalName.replace('/', '.');
            // try classes under compilation
            CompileUnit cu = getAST();
            ClassNode cn = cu.getClass(name);
            if (cn == null) {
                // try inner classes
                cn = cu.getGeneratedInnerClass(name);
            }
            if (cn == null) {
                ClassNodeResolver.LookupResult lookupResult = getClassNodeResolver().resolveName(name, CompilationUnit.this);
                cn = lookupResult == null ? null : lookupResult.getClassNode();
            }
            if (cn != null) {
                classNodeByInternalName.put(internalName, cn);
            }
            return cn;
        }

        private ClassNode getCommonSuperClassNode(ClassNode c, ClassNode d) {
            // adapted from ClassWriter code
            if (c.isDerivedFrom(d)) return d;
            if (d.isDerivedFrom(c)) return c;
            if (c.isInterface() || d.isInterface()) return ClassHelper.OBJECT_TYPE;
            do {
                c = c.getSuperClass();
            } while (c != null && !d.isDerivedFrom(c));
            return c == null ? ClassHelper.OBJECT_TYPE : c;
        }
    }

    //---------------------------------------------------------------------------
    // PHASE HANDLING

    /**
     * Updates the phase marker on all sources.
     */
    protected void mark() throws CompilationFailedException {
        ISourceUnitOperation mark = (final SourceUnit source) -> {
            if (source.phase < phase) {
                source.gotoPhase(phase);
            }
            if (source.phase == phase && phaseComplete && !source.phaseComplete) {
                source.completePhase();
            }
        };
        mark.doPhaseOperation(this);
    }

    //---------------------------------------------------------------------------
    // LOOP SIMPLIFICATION FOR SourceUnit OPERATIONS

    private interface PhaseOperation {
        void doPhaseOperation(CompilationUnit unit);
    }

    /**
     * Performs an operation for each source unit in the compilation.
     */
    @FunctionalInterface
    public interface ISourceUnitOperation extends PhaseOperation {
        /**
         * Performs the operation for one source unit.
         *
         * @param source the source unit to process
         * @throws CompilationFailedException if processing fails
         */
        void call(SourceUnit source) throws CompilationFailedException;

        /**
         * A loop driver for applying operations to all SourceUnits.
         * Automatically skips units that have already been processed
         * through the current phase.
         */
        @Override
        default void doPhaseOperation(final CompilationUnit unit) throws CompilationFailedException {
            for (SourceUnit source : unit.sources.values()) {
                if (source.phase < unit.phase || (source.phase == unit.phase && !source.phaseComplete)) {
                    try {
                        this.call(source);
                    } catch (CompilationFailedException e) {
                        throw e;
                    } catch (Exception e) {
                        GroovyBugError gbe = new GroovyBugError(e);
                        unit.changeBugText(gbe, source);
                        throw gbe;
                    } catch (GroovyBugError e) {
                        unit.changeBugText(e, source);
                        throw e;
                    }
                }
            }
            unit.getErrorCollector().failIfErrors();
        }
    }

    //---------------------------------------------------------------------------
    // LOOP SIMPLIFICATION FOR PRIMARY ClassNode OPERATIONS

    /**
     * Performs an operation for each primary class node in the compilation.
     */
    @FunctionalInterface
    public interface IPrimaryClassNodeOperation extends PhaseOperation {
        /**
         * Performs the operation for one primary class node.
         *
         * @param source the source unit owning the class, or {@code null} when the class node was
         *        supplied directly via {@link CompilationUnit#addClassNode(ClassNode)} and so has no
         *        originating source. Implementations must tolerate {@code null} here: an unguarded
         *        dereference is reported as a {@link org.codehaus.groovy.GroovyBugError} wrapping the
         *        {@code NullPointerException}, which reads as a compiler bug rather than as a fault
         *        in the implementation.
         * @param context the current generator context
         * @param classNode the class node to process
         * @throws CompilationFailedException if processing fails
         */
        void call(SourceUnit source, GeneratorContext context, ClassNode classNode) throws CompilationFailedException;

        /**
         * A loop driver for applying operations to all primary ClassNodes in
         * our AST.  Automatically skips units that have already been processed
         * through the current phase.
         */
        @Override
        default void doPhaseOperation(final CompilationUnit unit) throws CompilationFailedException {
            for (ClassNode classNode : unit.getPrimaryClassNodes(this.needSortedInput())) {
                SourceUnit context = null;
                try {
                    context = classNode.getModule().getContext();
                    if (context == null || context.phase < unit.phase || (context.phase == unit.phase && !context.phaseComplete)) {
                        int offset = 1;
                        for (Iterator<InnerClassNode> it = classNode.getInnerClasses(); it.hasNext(); ) {
                            it.next();
                            offset += 1;
                        }
                        this.call(context, new GeneratorContext(unit.getAST(), offset), classNode);
                    }
                } catch (CompilationFailedException e) {
                    // fall through
                } catch (NullPointerException npe) {
                    GroovyBugError gbe = new GroovyBugError("unexpected NullPointerException", npe);
                    unit.changeBugText(gbe, context);
                    throw gbe;
                } catch (GroovyBugError e) {
                    unit.changeBugText(e, context);
                    throw e;
                } catch (Exception | LinkageError e) {
                    ErrorCollector errorCollector = null;
                    // check for a nested compilation exception
                    for (Throwable t = e.getCause(); t != e && t != null; t = t.getCause()) {
                        if (t instanceof MultipleCompilationErrorsException) {
                            errorCollector = ((MultipleCompilationErrorsException) t).getErrorCollector();
                            break;
                        }
                    }

                    if (errorCollector != null) {
                        unit.getErrorCollector().addCollectorContents(errorCollector);
                    } else {
                        if (e instanceof GroovyRuntimeException gre) {
                            context = Optional.ofNullable(gre.getModule()).map(ModuleNode::getContext).orElse(context);
                        }
                        if (context != null) {
                            if (e instanceof SyntaxException) {
                                unit.getErrorCollector().addError((SyntaxException) e, context);
                            } else if (e.getCause() instanceof SyntaxException) {
                                unit.getErrorCollector().addError((SyntaxException) e.getCause(), context);
                            } else {
                                unit.getErrorCollector().addException(e instanceof Exception ? (Exception) e : new RuntimeException(e), context);
                            }
                        } else {
                            unit.getErrorCollector().addError(new ExceptionMessage(e instanceof Exception ? (Exception) e : new RuntimeException(e), unit.debug, unit));
                        }
                    }
                }
            }
            unit.getErrorCollector().failIfErrors();
        }

        /**
         * Indicates whether primary classes should be sorted before visiting.
         *
         * @return {@code true} if sorted input is required
         */
        default boolean needSortedInput() {
            return false;
        }
    }

    /**
     * Performs an operation for each generated {@link GroovyClass}.
     */
    @FunctionalInterface
    public interface IGroovyClassOperation extends PhaseOperation {
        /**
         * Performs the operation for one generated class.
         *
         * @param groovyClass the generated class to process
         * @throws CompilationFailedException if processing fails
         */
        void call(GroovyClass groovyClass) throws CompilationFailedException;

        /**
         * Applies this operation to all generated classes in the compilation unit.
         *
         * @param unit the compilation unit supplying generated classes
         * @throws CompilationFailedException if processing fails
         */
        @Override
        default void doPhaseOperation(final CompilationUnit unit) throws CompilationFailedException {
            if (unit.phase != Phases.OUTPUT && !(unit.phase == Phases.CLASS_GENERATION && unit.phaseComplete)) {
                throw new GroovyBugError("CompilationUnit not ready for output(). Current phase=" + unit.getPhaseDescription());
            }

            for (GroovyClass groovyClass : unit.getClasses()) {
                try {
                    this.call(groovyClass);
                } catch (CompilationFailedException e) {
                    // fall through
                } catch (NullPointerException npe) {
                    throw npe;
                } catch (GroovyBugError e) {
                    unit.changeBugText(e, null);
                    throw e;
                } catch (Exception e) {
                    throw new GroovyBugError(e);
                }
            }
            unit.getErrorCollector().failIfErrors();
        }
    }

    private static int getSuperClassCount(ClassNode classNode) {
        int count = 0;
        while (classNode != null) {
            count += 1;
            classNode = classNode.getSuperClass();
        }
        return count;
    }

    private static int getInterfacesCount(final ClassNode classNode) {
        int count = 1;
        for (ClassNode face : classNode.getInterfaces()) {
            count = Math.max(count, getInterfacesCount(face) + 1);
        }
        return count;
    }

    private void addNestMembers(final ClassNode host) {
        List<String> members = new ArrayList<>();
        collectNestMembers(host, members);
        for (int i = 0, n = generatedClasses.size(); i < n; i += 1) {
            GroovyClass groovyClass = generatedClasses.get(i);
            if (groovyClass.getName().equals(host.getName())) {
                generatedClasses.set(i, new GroovyClass(groovyClass.getName(), addNestMembers(groovyClass.getBytes(), members)));
                break;
            }
        }
    }

    private static void collectNestMembers(final ClassNode classNode, final List<String> names) {
        for (Iterator<InnerClassNode> it = classNode.getInnerClasses(); it.hasNext(); ) {
            ClassNode innerClass = it.next();
            names.add(innerClass.getName().replace('.', '/'));
            collectNestMembers(innerClass, names);
        }
    }

    private static byte[] addNestMembers(final byte[] bytes, final List<String> members) {
        ClassReader reader = new ClassReader(bytes);
        // share the constant pool so unmodified methods are copied as raw bytes
        // instead of being re-parsed and re-serialized (GROOVY-10687 hot path)
        ClassWriter writer = new ClassWriter(reader, 0);
        reader.accept(new ClassVisitor(CompilerConfiguration.ASM_API_VERSION, writer) {
            private final Set<String> seen = new LinkedHashSet<>();
            @Override
            public void visitNestMember(final String nestMember) {
                if (seen.add(nestMember)) super.visitNestMember(nestMember);
            }
            @Override
            public void visitEnd() {
                for (String member : members) {
                    if (seen.add(member)) super.visitNestMember(member);
                }
                super.visitEnd();
            }
        }, 0);
        return writer.toByteArray();
    }

    private List<ClassNode> getPrimaryClassNodes(final boolean sort) {
        List<ClassNode> classes = getAST().getClasses();

        if (sort && classes.size() > 1) {
            classes.sort(Comparator.comparingInt(cn -> {
                int count;
                if (cn.isInterface()) {
                    count = getInterfacesCount(cn);
                } else {
                    count = getSuperClassCount(cn) + 1000;
                }
                return count;
            }));
        }

        return classes;
    }

    private void changeBugText(final GroovyBugError e, final SourceUnit context) {
        e.setBugText("exception in phase '" + getPhaseDescription() + "' in source unit '" + (context != null ? context.getName() : "?") + "' " + e.getBugText());
    }

    //--------------------------------------------------------------------------

    /**
     * Adapts a deprecated groovy-class operation to the current API.
     *
     * @param op the operation to add
     */
    @Deprecated
    public void addPhaseOperation(final GroovyClassOperation op) {
        addPhaseOperation((IGroovyClassOperation) op);
    }

    /**
     * Adapts a deprecated source-unit operation to the current API.
     *
     * @param op the operation to add
     * @param phase the phase in which to run it
     */
    @Deprecated
    public void addPhaseOperation(final SourceUnitOperation op, final int phase) {
        addPhaseOperation((ISourceUnitOperation) op, phase);
    }

    /**
     * Adapts a deprecated primary-class operation to the current API.
     *
     * @param op the operation to add
     * @param phase the phase in which to run it
     */
    @Deprecated
    public void addPhaseOperation(final PrimaryClassNodeOperation op, final int phase) {
        addPhaseOperation((IPrimaryClassNodeOperation) op, phase);
    }

    /**
     * Adapts a deprecated primary-class operation to the front of a phase queue.
     *
     * @param op the operation to add
     * @param phase the phase in which to run it
     */
    @Deprecated
    public void addFirstPhaseOperation(final PrimaryClassNodeOperation op, final int phase) {
        addFirstPhaseOperation((IPrimaryClassNodeOperation) op, phase);
    }

    /**
     * Adapts a deprecated source-unit operation to the delayed phase queue.
     *
     * @param op the operation to add
     * @param phase the phase in which to run it
     */
    @Deprecated
    public void addNewPhaseOperation(final SourceUnitOperation op, final int phase) {
        addNewPhaseOperation((ISourceUnitOperation) op, phase);
    }

    /**
     * Applies a deprecated source-unit operation to all source units.
     *
     * @param op the operation to run
     * @throws CompilationFailedException if processing fails
     */
    @Deprecated
    public void applyToSourceUnits(final SourceUnitOperation op) throws CompilationFailedException {
        op.doPhaseOperation(this);
    }

    /**
     * Applies a deprecated primary-class operation to all primary class nodes.
     *
     * @param op the operation to run
     * @throws CompilationFailedException if processing fails
     */
    @Deprecated
    public void applyToPrimaryClassNodes(final PrimaryClassNodeOperation op) throws CompilationFailedException {
        op.doPhaseOperation(this);
    }

    /**
     * Compatibility base type for deprecated source-unit operations.
     */
    @Deprecated
    public abstract static class SourceUnitOperation implements ISourceUnitOperation {
    }

    /**
     * Compatibility base type for deprecated Groovy-class operations.
     */
    @Deprecated
    public abstract static class GroovyClassOperation implements IGroovyClassOperation {
    }

    /**
     * Compatibility base type for deprecated primary-class operations.
     */
    @Deprecated
    public abstract static class PrimaryClassNodeOperation implements IPrimaryClassNodeOperation {
    }
}
