<commit_before>/*     ____ ____  ____ ____  ______                                     *\
**    / __// __ \/ __// __ \/ ____/    SOcos COmpiles Scala             **
**  __\_ \/ /_/ / /__/ /_/ /\_ \       (c) 2002, LAMP/EPFL              **
** /_____/\____/\___/\____/____/                                        **
\*                                                                      */

// $Id$

package scalac.transformer;

import scalac.*;
import scalac.util.*;
import scalac.parser.*;
import scalac.symtab.*;
import java.util.ArrayList;

public class LambdaLiftPhase extends Phase implements Kinds, Modifiers {

    /** Initializes this instance. */
    public LambdaLiftPhase(Global global, PhaseDescriptor descriptor) {
        super(global, descriptor);
    }

    /** Applies this phase to the given compilation unit. */
    public void apply(CompilationUnit unit) {
        new LambdaLift(global, this).apply(unit);
    }

    public Type transformInfo(Symbol sym, Type tp) {
	/*
        if (global.debug)
            global.log("transform info for " + sym + ":" + tp + sym.locationString());
	*/
        Type tp1 = tp;
        if (sym != Symbol.NONE) {
            switch (tp) {
            case MethodType(_, _):
            case PolyType(_, _):
                tp1 = transform(tp, sym);
                break;
            default:
                if (sym.kind == CLASS)
                    tp1 = transform(tp, sym);
                else
                    tp1 = transform(tp, sym.owner());
            }
        }
        if ((sym.flags & Modifiers.CAPTURED) != 0)
            return global.definitions.REF_TYPE(tp1);
        else return tp1;
    }

    /** Add proxies as type arguments for propagated type parameters.
     */
    Type transform(Type tp, Symbol owner) {
        return transformTypeMap.setOwner(owner).apply(tp);
    }

    /** MapOnlyTypes => All symbols are mapped to themselves.
     */
    private class TransformTypeMap extends Type.MapOnlyTypes {
        Symbol owner;
//      ArrayList/*<Symbol>*/ excluded = new ArrayList();
        Type.Map setOwner(Symbol owner) { this.owner = owner; return this; }

        public Type apply(Type tp) {
            switch (tp) {
            case TypeRef(Type pre, Symbol sym, Type[] targs):
                if (sym.kind == CLASS) {
                    switch (pre) {
                    case NoPrefix:
                        pre = sym.owner().enclClass().thisType();
                        tp = Type.typeRef(pre, sym, targs);
                    }
                }
                switch (pre) {
                case NoPrefix:
                    if (LambdaLift.isLocal(sym, owner)) {
                        assert targs.length == 0;
                        return proxy(sym, owner).type();
                    }
                    break;
                case ThisType(_):
                    if (sym.kind == CLASS &&
			sym.primaryConstructor().isUpdatedAt(LambdaLiftPhase.this)) {
                        Symbol[] tparams = sym.primaryConstructor().nextInfo().typeParams();
                        int i = tparams.length;
                        while (i > 0 && (tparams[i-1].flags & SYNTHETIC) != 0)
                            i--;
                        if (i < tparams.length) {
                            if (global.debug)
                                global.log("adding proxies for " + sym + ": " + ArrayApply.toString(tparams));

                            Type[] targs1 = new Type[tparams.length];
                            System.arraycopy(map(targs), 0, targs1, 0, targs.length);
                            while (i < tparams.length) {
                                targs1[i] = proxy(tparams[i], owner).type();
                                i++;
                            }
                            return Type.typeRef(pre, sym, targs1);
                        }
                    }
                }
                break;
/*
            case PolyType(Symbol[] tparams, _):
                if (tparams.length != 0) {
                    int len = excluded.size();
                    for (int i = 0; i < tparams.length; i++)
                        excluded.add(tparams[i]);
                    Type tp1 = map(tp);
                    for (int i = 0; i < tparams.length; i++)
                        excluded.remove(excluded.size() - 1);
                    return tp1;
                }
*/
            }
            return map(tp);
        }
    }

    private TransformTypeMap transformTypeMap = new TransformTypeMap();

    /** Return closest enclosing (type)parameter that has same name as `fv',
     *  or `fv' itself if this is the closest definition.
     */
    Symbol proxy(Symbol fv, Symbol owner) {
        if (global.debug)
            global.log("proxy " + fv + " of " + fv.owner() + " in " + LambdaLift.enclFun(owner));
        Symbol o = owner;
        while (o.kind != NONE) {
            if (global.debug)
                global.log("looking in " +  LambdaLift.enclFun(o) + " " +
                    ArrayApply.toString(o.typeParams()));
            Symbol fowner = LambdaLift.enclFun(o);
            if (fowner.isMethod()) {
                if (LambdaLift.enclFun(fv.owner()) == fowner) return fv;
                Type ft = (fowner.isUpdatedAt(this)) ? fowner.nextType()
                    : fowner.type();
                Symbol[] ownerparams = fv.isType() ? ft.typeParams()
                    : ft.firstParams();
                for (int i = 0; i < ownerparams.length; i++) {
                    if (ownerparams[i].name == fv.name)
                        return ownerparams[i];
                }
            }
            assert o.owner() != o;
            o = o.owner();
        }
        return fv;
        //throw new ApplicationError("proxy " + fv + " in " + owner);
    }

}<commit_msg>- replaced some tests on symbol flags.<commit_after>/*     ____ ____  ____ ____  ______                                     *\
**    / __// __ \/ __// __ \/ ____/    SOcos COmpiles Scala             **
**  __\_ \/ /_/ / /__/ /_/ /\_ \       (c) 2002-2005, LAMP/EPFL         **
** /_____/\____/\___/\____/____/                                        **
\*                                                                      */

// $Id$

package scalac.transformer;

//import java.util.ArrayList;

import scalac.*;
import scalac.parser.*;
import scalac.symtab.*;
import scalac.util.ArrayApply;


public class LambdaLiftPhase extends Phase implements Kinds, Modifiers {

    /** Initializes this instance. */
    public LambdaLiftPhase(Global global, PhaseDescriptor descriptor) {
        super(global, descriptor);
    }

    /** Applies this phase to the given compilation unit.
     *
     *  @param unit
     */
    public void apply(CompilationUnit unit) {
        new LambdaLift(global, this).apply(unit);
    }

    /** ...
     *
     *  @param sym
     *  @param tp
     *  @return
     */
    public Type transformInfo(Symbol sym, Type tp) {
	/*
        if (global.debug)
            global.log("transform info for " + sym + ":" + tp + sym.locationString());
	*/
        Type tp1 = tp;
        if (sym != Symbol.NONE) {
            switch (tp) {
            case MethodType(_, _):
            case PolyType(_, _):
                tp1 = transform(tp, sym);
                break;
            default:
                if (sym.kind == CLASS)
                    tp1 = transform(tp, sym);
                else
                    tp1 = transform(tp, sym.owner());
            }
        }
        if (sym.isCaptured())
            return global.definitions.REF_TYPE(tp1);
        else return tp1;
    }

    /** Add proxies as type arguments for propagated type parameters.
     *
     *  @param tp
     *  @param owner
     *  @return
     */
    Type transform(Type tp, Symbol owner) {
        return transformTypeMap.setOwner(owner).apply(tp);
    }

    /** MapOnlyTypes => All symbols are mapped to themselves.
     */
    private class TransformTypeMap extends Type.MapOnlyTypes {
        Symbol owner;
//      ArrayList/*<Symbol>*/ excluded = new ArrayList();
        Type.Map setOwner(Symbol owner) { this.owner = owner; return this; }

        public Type apply(Type tp) {
            switch (tp) {
            case TypeRef(Type pre, Symbol sym, Type[] targs):
                if (sym.kind == CLASS) {
                    switch (pre) {
                    case NoPrefix:
                        pre = sym.owner().enclClass().thisType();
                        tp = Type.typeRef(pre, sym, targs);
                    }
                }
                switch (pre) {
                case NoPrefix:
                    if (LambdaLift.isLocal(sym, owner)) {
                        assert targs.length == 0;
                        return proxy(sym, owner).type();
                    }
                    break;
                case ThisType(_):
                    if (sym.kind == CLASS &&
			sym.primaryConstructor().isUpdatedAt(LambdaLiftPhase.this)) {
                        Symbol[] tparams = sym.primaryConstructor().nextInfo().typeParams();
                        int i = tparams.length;
                        while (i > 0 && tparams[i-1].isSynthetic())
                            i--;
                        if (i < tparams.length) {
                            if (global.debug)
                                global.log("adding proxies for " + sym +
                                           ": " + ArrayApply.toString(tparams));

                            Type[] targs1 = new Type[tparams.length];
                            System.arraycopy(map(targs), 0, targs1, 0, targs.length);
                            while (i < tparams.length) {
                                targs1[i] = proxy(tparams[i], owner).type();
                                i++;
                            }
                            return Type.typeRef(pre, sym, targs1);
                        }
                    }
                }
                break;
/*
            case PolyType(Symbol[] tparams, _):
                if (tparams.length != 0) {
                    int len = excluded.size();
                    for (int i = 0; i < tparams.length; i++)
                        excluded.add(tparams[i]);
                    Type tp1 = map(tp);
                    for (int i = 0; i < tparams.length; i++)
                        excluded.remove(excluded.size() - 1);
                    return tp1;
                }
*/
            }
            return map(tp);
        }
    }

    private TransformTypeMap transformTypeMap = new TransformTypeMap();

    /** Return closest enclosing (type)parameter that has same name as `fv',
     *  or `fv' itself if this is the closest definition.
     *
     *  @param fv
     *  @param owner
     *  @return
     */
    Symbol proxy(Symbol fv, Symbol owner) {
        if (global.debug)
            global.log("proxy " + fv + " of " + fv.owner() +
                       " in " + LambdaLift.enclFun(owner));
        Symbol o = owner;
        while (o.kind != NONE) {
            if (global.debug)
                global.log("looking in " +  LambdaLift.enclFun(o) + " " +
                    ArrayApply.toString(o.typeParams()));
            Symbol fowner = LambdaLift.enclFun(o);
            if (fowner.isMethod()) {
                if (LambdaLift.enclFun(fv.owner()) == fowner) return fv;
                Type ft = (fowner.isUpdatedAt(this)) ? fowner.nextType()
                    : fowner.type();
                Symbol[] ownerparams = fv.isType() ? ft.typeParams()
                    : ft.firstParams();
                for (int i = 0; i < ownerparams.length; i++) {
                    if (ownerparams[i].name == fv.name)
                        return ownerparams[i];
                }
            }
            assert o.owner() != o;
            o = o.owner();
        }
        return fv;
        //throw new ApplicationError("proxy " + fv + " in " + owner);
    }

}
{'fraction_non_alphanumeric': 0.11769127965252255, 'fraction_numerical': 0.004844637487470765, 'mean_word_length': 1.1812716341774459, 'pattern_counts': {'":': 4, '<': 15, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2486948', 'n_tokens_mistral': 3299, 'n_tokens_neox': 3118, 'n_words': 741}