diff options
Diffstat (limited to 'sources/scalac')
-rw-r--r-- | sources/scalac/ast/TreeGen.java | 1014 | ||||
-rw-r--r-- | sources/scalac/transformer/AddAccessors.java | 16 | ||||
-rw-r--r-- | sources/scalac/transformer/AddConstructors.java | 21 | ||||
-rw-r--r-- | sources/scalac/transformer/AddInterfaces.java | 14 | ||||
-rw-r--r-- | sources/scalac/transformer/Erasure.java | 8 | ||||
-rw-r--r-- | sources/scalac/transformer/ExpandMixins.java | 2 | ||||
-rw-r--r-- | sources/scalac/transformer/ExpandMixinsPhase.java | 2 | ||||
-rw-r--r-- | sources/scalac/transformer/ExplicitOuterClassesPhase.java | 16 | ||||
-rw-r--r-- | sources/scalac/transformer/LambdaLift.java | 11 | ||||
-rw-r--r-- | sources/scalac/transformer/UnCurry.java | 2 | ||||
-rw-r--r-- | sources/scalac/transformer/matching/AlgebraicMatcher.java | 5 | ||||
-rw-r--r-- | sources/scalac/transformer/matching/Autom2Scala.java | 8 | ||||
-rw-r--r-- | sources/scalac/transformer/matching/CodeFactory.java | 22 | ||||
-rw-r--r-- | sources/scalac/transformer/matching/LeftTracerInScala.java | 4 | ||||
-rw-r--r-- | sources/scalac/transformer/matching/PatternMatcher.java | 2 | ||||
-rw-r--r-- | sources/scalac/typechecker/RefCheck.java | 24 |
16 files changed, 582 insertions, 589 deletions
diff --git a/sources/scalac/ast/TreeGen.java b/sources/scalac/ast/TreeGen.java index 62c5fbf6c8..ac99d4f58b 100644 --- a/sources/scalac/ast/TreeGen.java +++ b/sources/scalac/ast/TreeGen.java @@ -8,268 +8,293 @@ package scalac.ast; -import java.io.*; -import java.util.*; import scalac.*; import scalac.symtab.*; -import scalac.typechecker.Infer; import scalac.util.*; import Tree.*; -/** A helper class for building trees +/** + * This class provides method to build attributed trees. * - * @author Martin Odersky, Christine Roeckl - * @version 1.0 + * @author Martin Odersky, Christine Roeckl + * @version 1.0 */ public class TreeGen implements Kinds, Modifiers, TypeTags { - /********************************************************************************/ - /********************************************************************************/ - /** VARIABLES **/ + //######################################################################## + // Private Fields - /** the global environment - */ - protected final Global global; + /** The global environment */ + private final Global global; - /** the global definitions - */ - protected final Definitions definitions; + /** The global definitions */ + private final Definitions definitions; - /** the tree factory - */ + /** The tree factory */ public final TreeFactory make; - /** the type inferencer - */ - final Infer infer; + //######################################################################## + // Public Constructors - /************************************************************************/ - /************************************************************************/ - /** CONSTRUCTORS **/ + /** Initializes this instance. */ + public TreeGen(Global global) { + this(global, global.make); + } + /** Initializes this instance. */ public TreeGen(Global global, TreeFactory make) { this.global = global; this.definitions = global.definitions; this.make = make; - this.infer = new Infer(global, this, make); } - public TreeGen(Global global) { - this(global, global.make); + //######################################################################## + // Public Methods - Building types + + /** Builds type references corresponding to given symbols. */ + public Tree[] mkTypeRefs(int pos, Symbol[] syms) { + if (syms.length == 0) return Tree.EMPTY_ARRAY; + Tree[] trees = new Tree[syms.length]; + for (int i = 0; i < trees.length; i++) + trees[i] = mkTypeRef(pos, syms[i]); + return trees; } - /*************************************************************************/ - /*************************************************************************/ - /** METHODS **/ + /** Builds a type reference corresponding to given symbol. */ + public Tree mkTypeRef(int pos, Symbol sym) { + assert sym.kind == TYPE: Debug.show(sym); + sym.flags |= ACCESSED; + return mkType(pos, sym.nextType()); + } - public Type deref(Type tp) { - switch (tp) { - case PolyType(Symbol[] tparams, Type restp): - if (tparams.length == 0) return restp; - } - return tp; + /** Builds trees corresponding to given types. */ + public Tree[] mkTypes(int pos, Type[] types) { + if (types.length == 0) return Tree.EMPTY_ARRAY; + Tree[] trees = new Tree[types.length]; + for (int i = 0; i < trees.length; i++) + trees[i] = mkType(pos, types[i]); + return trees; } - /** Create a dummy symbol to be used for templates. - */ - public Symbol localDummy(int pos, Symbol owner) { - return new TermSymbol(pos, Names.LOCAL(owner), owner, 0) - .setInfo(Type.NoType); + /** Builds a tree corresponding to given type. */ + public Tree mkType(int pos, Type type) { + return TypeTerm(pos, type); } - public Tree mkStable(Tree tree) { - Symbol sym = tree.symbol(); - if (sym.isStable()) { - switch (tree) { - case Ident(_): - tree.setType(Type.singleType(sym.owner().thisType(), sym)); - break; - case Select(Tree qual, _): - if (qual.type.isStable()) - tree.setType(Type.singleType(qual.type, sym)); - } - } - return tree; + /** Builds a TypeTerm node corresponding to given type. */ + public TypeTerm TypeTerm(int pos, Type type) { + TypeTerm tree = make.TypeTerm(pos); + tree.setType(type); + return tree; } - public Tree mkRef(int pos, Type pre, Symbol sym) { - if (pre.isSameAs(Type.localThisType) || pre.symbol().isRoot()) - return Ident(pos, sym); - else - return Select(pos, mkStableId(pos, pre), sym); + //######################################################################## + // Public Methods - Building constants + + /** Builds a unit literal. */ + public Tree mkUnitLit(int pos) { + return make.Block(pos, Tree.EMPTY_ARRAY). + setType(definitions.UNIT_TYPE); } - public Tree mkRef(int pos, Symbol sym) { - return mkRef(pos, sym.owner().thisType(), sym); + /** Builds a boolean literal. */ + public Tree mkBooleanLit(int pos, boolean value) { + return make.Literal(pos, value ? Boolean.TRUE : Boolean.FALSE). + setType(definitions.BOOLEAN_TYPE); } - /** Build and attribute stable identifier tree corresponding to given prefix. - */ - public Tree mkStableId(int pos, Type pre) { - switch (pre.expandModuleThis()) { - case ThisType(Symbol sym): - return This(pos, sym); - case SingleType(Type pre1, Symbol sym): - return mkStable(mkRef(pos, pre1, sym)); - default: - throw new ApplicationError(pre); - } + /** Builds a byte literal. */ + public Tree mkByteLit(int pos, byte value) { + return make.Literal(pos, new Byte(value)). + setType(definitions.BYTE_TYPE); } - /** Build and attribute ident nodes with given symbols. - */ - public Tree[] mkIdents(int pos, Symbol[] syms) { - if (syms.length == 0) return Tree.EMPTY_ARRAY; - Tree[] trees = new Tree[syms.length]; - for (int i = 0; i < trees.length; i++) - trees[i] = Ident(pos, syms[i]); - return trees; + /** Builds a short literal. */ + public Tree mkShortLit(int pos, short value) { + return make.Literal(pos, new Short(value)). + setType(definitions.SHORT_TYPE); } - /** Build and attribute type idents with given symbols. - */ - public Tree[] mkTypeIdents(int pos, Symbol[] syms) { - if (syms.length == 0) return Tree.EMPTY_ARRAY; - Tree[] trees = new Tree[syms.length]; - for (int i = 0; i < trees.length; i++) - trees[i] = mkTypeIdent(pos, syms[i]); - return trees; + /** Builds a character literal. */ + public Tree mkCharLit(int pos, char value) { + return make.Literal(pos, new Character(value)). + setType(definitions.CHAR_TYPE); } - /** Build and attribute type ident with given symbol. - */ - public Tree mkTypeIdent(int pos, Symbol sym) { - assert sym.kind == TYPE: Debug.show(sym); - sym.flags |= ACCESSED; - return mkType(pos, sym.nextType()); + /** Builds an integer literal */ + public Tree mkIntLit(int pos, int value) { + return make.Literal(pos, new Integer(value)). + setType(definitions.INT_TYPE); } - /** Build and attribute tree corresponding to given type. - */ - public Tree mkType(int pos, Type type) { - return TypeTerm(pos, type); + /** Builds a long literal. */ + public Tree mkLongLit(int pos, long value) { + return make.Literal(pos, new Long(value)). + setType(definitions.LONG_TYPE); } - /** Build and attribute tree array corresponding to given type array. - */ - public Tree[] mkTypes(int pos, Type[] types) { - Tree[] res = new Tree[types.length]; - for (int i = 0; i < types.length; i++) { - res[i] = mkType(pos, types[i]); - } - return res; + /** Builds a float literal. */ + public Tree mkFloatLit(int pos, float value) { + return make.Literal(pos, new Float(value)). + setType(definitions.FLOAT_TYPE); } - /** Build and attribute tree corresponding to given type. - */ - public Tree TypeTerm(int pos, Type type) { - return make.TypeTerm(pos).setType(type); + /** Builds a double literal. */ + public Tree mkDoubleLit(int pos, double value) { + return make.Literal(pos, new Double(value)). + setType(definitions.DOUBLE_TYPE); } - /** Build and attribute tree corresponding to symbol's declaration. - */ - public Tree mkDef(int pos, Symbol sym) { - switch (sym.kind) { - case ERROR: - return make.Bad(pos, Symbol.ERROR).setType(Type.ErrorType); - case TYPE: - return AbsTypeDef(pos, sym); - case ALIAS: - return AliasTypeDef(pos, sym); - case VAL: - if (sym.isMethod()) return DefDef(pos, sym, Tree.Empty); - else return ValDef(pos, sym, Tree.Empty); - default: - throw new ApplicationError(); - } + /** Builds a string literal. */ + public Tree mkStringLit(int pos, String value) { + return make.Literal(pos, value).setType(definitions.JAVA_STRING_TYPE); } - /** Build and attribute tree array corresponding to given symbol's declarations. - */ - public Tree[] mkDefs(int pos, Symbol[] syms) { - Tree[] res = new Tree[syms.length]; - for (int i = 0; i < syms.length; i++) { - res[i] = mkDef(pos, syms[i]); + /** Builds a null literal. */ + public Tree mkNullLit(int pos) { + return Ident(pos, definitions.NULL); + } + + /** Builds a zero literal. */ + public Tree mkZeroLit(int pos) { + return Ident(pos, definitions.ZERO); + } + + /** Builds a default zero value according to given type tag. */ + public Tree mkDefaultValue(int pos, int tag) { + switch (tag) { + case UNIT : return mkUnitLit(pos); + case BOOLEAN: return mkBooleanLit(pos, false); + case BYTE : return mkByteLit(pos, (byte)0); + case SHORT : return mkShortLit(pos, (short)0); + case CHAR : return mkCharLit(pos, '\0'); + case INT : return mkIntLit(pos, 0); + case LONG : return mkLongLit(pos, 0l); + case FLOAT : return mkFloatLit(pos, 0f); + case DOUBLE : return mkDoubleLit(pos, 0d); + default : throw Debug.abort("unknown type tag: " + tag); } - return res; } - /** Build a boolean constant tree. - */ - public Tree mkBooleanLit(int pos, boolean bool) { - return make.Literal(pos, bool ? Boolean.TRUE : Boolean.FALSE). - setType(definitions.BOOLEAN_TYPE); + /** Builds a default zero value according to given type. */ + public Tree mkDefaultValue(int pos, Type type) { + if (type.isSubType(definitions.ANYREF_TYPE)) return mkNullLit(pos); + switch (type.unbox()) { + case UnboxedType(int tag): return mkDefaultValue(pos, tag); + } + return mkZeroLit(pos); } - /** Build a string literal - */ - public Tree mkStringLit(int pos, String str) { - return make.Literal(pos, str).setType(definitions.JAVA_STRING_TYPE); + //######################################################################## + // Public Methods - Building references + + /** Builds references corresponding to given symbols. */ + public Tree[] mkRefs(int pos, Symbol[] syms) { + if (syms.length == 0) return Tree.EMPTY_ARRAY; + Tree[] trees = new Tree[syms.length]; + for (int i = 0; i < trees.length; i++) + trees[i] = mkRef(pos, syms[i]); + return trees; } - /** Build an integer literal - */ - public Tree mkIntLit(int pos, int value) { - return make.Literal(pos, new Integer(value)).setType(definitions.INT_TYPE); + /** Builds a reference corresponding to given symbol. */ + public Tree mkRef(int pos, Symbol sym) { + return mkRef(pos, sym.owner().thisType(), sym); } - /** Build a default zero value according to type - */ - public Tree mkDefaultValue(int pos, Type tp) { - if (tp.isSubType(definitions.ANYREF_TYPE)) { - return Ident(pos, definitions.NULL); - } else { - switch (tp.unbox()) { - case UnboxedType(BOOLEAN): - return mkBooleanLit(pos, false); - case UnboxedType(BYTE): - case UnboxedType(SHORT): - case UnboxedType(CHAR): - case UnboxedType(INT): - return mkIntLit(pos, 0); - case UnboxedType(LONG): - return make.Literal(pos, new Long(0)).setType(definitions.LONG_TYPE); - case UnboxedType(FLOAT): - return make.Literal(pos, new Float(0)).setType(definitions.FLOAT_TYPE); - case UnboxedType(DOUBLE): - return make.Literal(pos, new Double(0)).setType(definitions.DOUBLE_TYPE); - case UnboxedType(UNIT): - return Block(pos, Tree.EMPTY_ARRAY); - default: - return Ident(pos, definitions.ZERO); - } - } + /** Builds a reference corresponding to given prefix & symbol. */ + public Tree mkRef(int pos, Type pre, Symbol sym) { + if (pre.isSameAs(Type.localThisType) || pre.symbol().isRoot()) + return Ident(pos, sym); + else + return Select(pos, mkStableId(pos, pre), sym); } + /** Builds a reference corresponding to given stable prefix. */ + public Tree mkStableId(int pos, Type pre) { + switch (pre.expandModuleThis()) { + case ThisType(Symbol sym): + return This(pos, sym); + case SingleType(Type pre1, Symbol sym): + return mkRef(pos, pre1, sym); + default: + throw Debug.abort("illegal case", pre); + } + } - /** Build a call to a primary constructor. - */ - public Tree mkPrimaryConstr(int pos, Type type) { - return mkPrimaryConstr(pos, type, Tree.EMPTY_ARRAY); + /** Builds a This node corresponding to given class. */ + public This This(int pos, Symbol clazz) { + assert clazz.isClass(): Debug.show(clazz); + This tree = make.This(pos, clazz); + global.nextPhase(); + tree.setType(clazz.thisType()); + global.prevPhase(); + return tree; } - public Tree mkPrimaryConstr(int pos, Type type, Tree[] args) { - switch (type) { - case TypeRef(Type prefix, Symbol clazz, Type[] targs): - global.nextPhase(); - Symbol constr = clazz.primaryConstructor(); - global.prevPhase(); - return mkApply(mkRef(pos, prefix, constr), targs, args); - default: - throw Debug.abort("invalid type", type); - } + /** Builds a Super node corresponding to given class. */ + public Super Super(int pos, Symbol clazz) { + assert clazz.isClass(): Debug.show(clazz); + Super tree = make.Super(pos, clazz, TypeNames.EMPTY); + global.nextPhase(); + tree.setType(clazz.thisType()); + global.prevPhase(); + return tree; } - /** Build an array of calls to primary constructors. + /** Builds an Ident node corresponding to given symbol. */ + public Ident Ident(int pos, Symbol sym) { + assert sym.isTerm(): Debug.show(sym); + sym.flags |= ACCESSED; + Ident tree = make.Ident(pos, sym); + global.nextPhase(); + if (sym.isStable()) + tree.setType(Type.singleType(sym.owner().thisType(), sym)); + else + tree.setType(sym.type()); + global.prevPhase(); + return tree; + } + + /** + * Builds a Select node corresponding to given symbol selected + * from given qualifier. + */ + public Select Select(int pos, Tree qual, Symbol sym) { + assert sym.isTerm(): Debug.show(sym); + sym.flags |= ACCESSED | SELECTOR; + Select tree = make.Select(pos, sym, qual); + global.nextPhase(); + if (sym.isStable() && qual.type.isStable()) + tree.setType(Type.singleType(qual.type, sym)); + else + tree.setType(qual.type.memberType(sym)); + global.prevPhase(); + return tree; + } + public Select Select(Tree qual, Symbol sym) { + return Select(qual.pos, qual, sym); + } + + //######################################################################## + // Public Methods - Building applications + + /** + * Builds calls to primary constructors of given types with given + * value arguments. */ - public Tree[] mkPrimaryConstrs(int pos, Type[] types, Tree[][] args) { - assert types.length == args.length: Debug.show(types, " -- ", args); + public Tree[] mkPrimaryConstrs(int pos, Type[] types, Tree[][] vargs) { + assert types.length == vargs.length: Debug.show(types, " -- ", vargs); Tree[] trees = new Tree[types.length]; for (int i = 0; i < trees.length; i++) - trees[i] = mkPrimaryConstr(pos, types[i], args[i]); + trees[i] = mkPrimaryConstr(pos, types[i], vargs[i]); return trees; } + /** + * Builds calls to primary constructors of given types with no + * value arguments. + */ public Tree[] mkPrimaryConstrs(int pos, Type[] types) { Tree[] trees = new Tree[types.length]; for (int i = 0; i < trees.length; i++) @@ -277,396 +302,393 @@ public class TreeGen implements Kinds, Modifiers, TypeTags { return trees; } - - /** Build parameter sections corresponding to type. + /** + * Builds a call to the primary constructor of given type with + * given value arguments. Missing type arguments are extracted + * from the given type. */ - public ValDef[][] mkParams(Type type) { + public Tree mkPrimaryConstr(int pos, Type type, Tree[] vargs) { switch (type) { - case PolyType(Symbol[] tparams, Type restype): - return mkParams(restype); - case MethodType(Symbol[] vparams, Type restype): - ValDef[] params1 = mkParams(vparams); - ValDef[][] paramss = mkParams(restype); - if (paramss.length == 0) { - return new ValDef[][]{params1}; - } else { - ValDef[][] paramss1 = new ValDef[paramss.length + 1][]; - paramss1[0] = params1; - System.arraycopy(paramss, 0, paramss1, 1, paramss.length); - return paramss1; - } - default: - return new ValDef[][]{}; - } + case TypeRef(Type pre, Symbol clazz, Type[] targs): + return mkPrimaryConstr(pos, pre, clazz, targs, vargs); + default: + throw Debug.abort("invalid type", type); + } } - /** Build parameter section corresponding to given array of symbols . + /** + * Builds a call to the primary constructor of given type with no + * value arguments. Missing type arguments are extracted from the + * given type. */ - public ValDef[] mkParams(Symbol[] symbols) { - ValDef[] res = new ValDef[symbols.length]; - for (int i = 0; i < symbols.length; i++) { - res[i] = mkParam(symbols[i]); - } - return res; + public Tree mkPrimaryConstr(int pos, Type type) { + return mkPrimaryConstr(pos, type, Tree.EMPTY_ARRAY); } - /** Build parameter corresponding to given symbol . + /** + * Builds a call to the primary constructor of given class with + * given type and value arguments. */ - public ValDef mkParam(Symbol sym) { - return ValDef(sym.pos, sym, Tree.Empty); + public Tree mkPrimaryConstr(int pos, Type pre, Symbol clazz, Type[] targs, + Tree[] vargs) + { + global.nextPhase(); + Symbol constr = clazz.primaryConstructor(); + global.prevPhase(); + return mkApply(mkRef(pos, constr), targs, vargs); } - - /** Build type parameter section corresponding to given array of symbols . - */ - public AbsTypeDef[] mkTypeParams(Symbol[] symbols) { - AbsTypeDef[] res = new AbsTypeDef[symbols.length]; - for (int i = 0; i < symbols.length; i++) { - res[i] = mkTypeParam(symbols[i]); - } - return res; + public Tree mkPrimaryConstr(int pos, Symbol clazz,Type[]targs,Tree[]vargs){ + return mkPrimaryConstr(pos,clazz.owner().thisType(),clazz,targs,vargs); } - /** Build type parameter corresponding to given symbol . + /** + * Builds a call to the primary constructor of given class with + * given type arguments and no value arguments. */ - public AbsTypeDef mkTypeParam(Symbol sym) { - return AbsTypeDef(sym.pos, sym); + public Tree mkPrimaryConstr(int pos, Type pre, Symbol clazz, Type[] targs){ + return mkPrimaryConstr(pos, pre, clazz, targs, Tree.EMPTY_ARRAY); + } + public Tree mkPrimaryConstr(int pos, Symbol clazz, Type[] targs) { + return mkPrimaryConstr(pos, clazz.owner().thisType(), clazz, targs); } - /** Build abstract type definition corresponding to given symbol . + /** + * Builds a call to the primary constructor of given class with no + * type and value arguments. */ - public AbsTypeDef AbsTypeDef(int pos, Symbol sym) { - Global.instance.nextPhase(); - Type symtype = sym.info(); - Global.instance.prevPhase(); - AbsTypeDef res = make.AbsTypeDef( - pos, sym, TypeTerm(pos, symtype), TypeTerm(pos, sym.loBound())); - res.setType(definitions.UNIT_TYPE); - return res; + public Tree mkPrimaryConstr(int pos, Type pre, Symbol clazz) { + return mkPrimaryConstr(pos, pre, clazz, Type.EMPTY_ARRAY); } - - public AbsTypeDef AbsTypeDef(Symbol sym) { - return AbsTypeDef(sym.pos, sym); + public Tree mkPrimaryConstr(int pos, Symbol clazz) { + return mkPrimaryConstr(pos, clazz.owner().thisType(), clazz); } - /** Build type definition corresponding to given symbol . - */ - public AliasTypeDef AliasTypeDef(int pos, Symbol sym) { - Global.instance.nextPhase(); - Type symtype = sym.info(); - Global.instance.prevPhase(); - AliasTypeDef res = make.AliasTypeDef( - pos, - sym, - mkTypeParams(sym.typeParams()), - TypeTerm(pos, symtype)); - res.setType(definitions.UNIT_TYPE); - return res; + /** Builds an application with given function and arguments. */ + public Tree mkApply(int pos, Tree fn, Type[] targs, Tree[] vargs) { + if (targs.length != 0) fn = TypeApply(pos, fn, mkTypes(pos, targs)); + return Apply(pos, fn, vargs); } - - public AliasTypeDef AliasTypeDef(Symbol sym) { - return AliasTypeDef(sym.pos, sym); + public Tree mkApply(Tree fn, Type[] targs, Tree[] vargs) { + return mkApply(fn.pos, fn, targs, vargs); } - - /** Build and attribute block with given statements, starting - * at given position. The type is the type of the last - * statement in the block. - */ - public Tree Block(int pos, Tree[] stats) { - Type tp = (stats.length == 0) ? definitions.UNIT_TYPE - : stats[stats.length - 1].type; - return make.Block(pos, stats).setType(tp); + public Tree mkApply(int pos, Tree fn, Tree[] targs, Tree[] vargs) { + if (targs.length != 0) fn = TypeApply(pos, fn, targs); + return Apply(pos, fn, vargs); + } + public Tree mkApply(Tree fn, Tree[] targs, Tree[] vargs) { + return mkApply(fn.pos, fn, targs, vargs); } - /** Build and attribute non-empty block with given statements. + /** + * Builds an application with given function and type arguments + * and with no value arguments. */ - public Tree Block(Tree[] stats) { - return Block(stats[0].pos, stats); + public Tree mkApply(int pos, Tree fn, Type[] targs) { + return mkApply(pos, fn, targs, Tree.EMPTY_ARRAY); } - - public Tree Typed(Tree tree, Type tp) { - return make.Typed(tree.pos, tree, TypeTerm(tree.pos, tp)).setType(tp); + public Tree mkApply(Tree fn, Type[] targs) { + return mkApply(fn.pos, fn, targs); } - - /** Build and attribute the assignment lhs = rhs - */ - public Tree Assign(int pos, Tree lhs, Tree rhs) { - return make.Assign(pos, lhs, rhs).setType(definitions.UNIT_TYPE); + public Tree mkApply(int pos, Tree fn, Tree[] targs) { + return mkApply(pos, fn, targs, Tree.EMPTY_ARRAY); } - - public Tree Assign(Tree lhs, Tree rhs) { - return Assign(lhs.pos, lhs, rhs); + public Tree mkApply(Tree fn, Tree[] targs) { + return mkApply(fn.pos, fn, targs); } - /** Build and attribute new B, given constructor expression B. - */ - public Tree New(int pos, Tree constr) { - Symbol local = localDummy(pos, Symbol.NONE); - Template templ = make.Template( - pos, local, new Tree[]{constr}, Tree.EMPTY_ARRAY); - templ.setType(constr.type); - return make.New(pos, templ).setType(constr.type); + /** Builds a TypeApply node with given function and arguments. */ + public TypeApply TypeApply(int pos, Tree fn, Tree[] targs) { + switch (fn.type) { + case PolyType(Symbol[] tparams, Type result): + TypeApply tree = make.TypeApply(pos, fn, targs); + assert tparams.length == targs.length: tree; + global.nextPhase(); + tree.setType(result.subst(tparams, Tree.typeOf(targs))); + global.prevPhase(); + return tree; + default: + throw Debug.abort("illegal case", fn.type); + } } - - public Tree New(Tree constr) { - return New(constr.pos, constr); + public TypeApply TypeApply(Tree fn, Tree[] targs) { + return TypeApply(fn.pos, fn, targs); } - - /** Build an allocation new P.C[TARGS](ARGS) - * given a (singleton) type P, class C, type arguments TARGS and arguments ARGS - */ - public Tree New(int pos, Type pre, Symbol clazz, - Type[] targs, Tree[] args) { - Tree constr = mkRef(pos, pre, clazz.primaryConstructor()); - return New(mkApply(constr, mkTypes(pos, targs), args)); + /** Builds an Apply node with given function and arguments. */ + public Apply Apply(int pos, Tree fn, Tree[] vargs) { + switch (fn.type) { + case Type.MethodType(Symbol[] vparams, Type result): + Apply tree = make.Apply(pos, fn, vargs); + // !!! assert vparams.length == vargs.length: tree + " --- " + Debug.show(vparams) + " --- " + Debug.show(vargs); + tree.setType(result); + return tree; + default: + throw Debug.abort("illegal case", fn); + } } - - /** Build a monomorphic allocation new P.C(ARGS) - * given a prefix P, class C and arguments ARGS - */ - public Tree New(int pos, Type pre, Symbol clazz, Tree[] args) { - return New(pos, pre, clazz, Type.EMPTY_ARRAY, args); + public Apply Apply(Tree fn, Tree[] vargs) { + return Apply(fn.pos, fn, vargs); } - /** Build application with given function, type args and value - * args. - */ - public Tree mkApply(int pos, Tree fn, Type[] targs, Tree[] args) { - if (targs.length != 0) fn = TypeApply(pos, fn, mkTypes(pos, targs)); - return Apply(pos, fn, args); + /** Builds an Apply node with given function and no arguments. */ + public Apply Apply(int pos, Tree fn) { + return Apply(pos, fn, Tree.EMPTY_ARRAY); } - - public Tree mkApply(Tree fn, Type[] targs, Tree[] args) { - return mkApply(fn.pos, fn, targs, args); + public Apply Apply(Tree fn) { + return Apply(fn.pos, fn); } - public Tree mkApply(int pos, Tree fn, Tree[] targs, Tree[] args) { - if (targs.length != 0) fn = TypeApply(pos, fn, targs); - return Apply(pos, fn, args); - } + //######################################################################## + // Public Methods - Building expressions - public Tree mkApply(Tree fn, Tree[] targs, Tree[] args) { - return mkApply(fn.pos, fn, targs, args); + /** Builds a cast with given value and type. */ + public Tree mkAsInstanceOf(int pos, Tree value, Type type) { + return mkApply(pos, Select(value, definitions.AS), new Type[] {type}); } - - /** Build and attribute application node with given function - * and argument trees. - */ - public Tree Apply(int pos, Tree fn, Tree[] args) { - try { - switch (fn.type) { - case Type.OverloadedType(Symbol[] alts, Type[] alttypes): - global.nextPhase(); - infer.methodAlternative(fn, alts, alttypes, - Tree.typeOf(args), Type.AnyType); - global.prevPhase(); - } - switch (fn.type) { - case Type.MethodType(Symbol[] vparams, Type restpe): - return make.Apply(pos, fn, args).setType(restpe); - } - } catch (Type.Error ex) { - } - throw new ApplicationError("method type required", fn.type); + public Tree mkAsInstanceOf(Tree value, Type type) { + return mkAsInstanceOf(value.pos, value, type); } - public Tree Apply(Tree fn, Tree[] args) { - return Apply(fn.pos, fn, args); + /** Builds a Block node with given statements. */ + public Tree Block(int pos, Tree[] stats) { + Block tree = make.Block(pos, stats); + tree.setType(stats.length == 0 + ? definitions.UNIT_TYPE + : stats[stats.length - 1].type); + return tree; } - /** Build and attribute type application node with given function - * and argument trees. - */ - public Tree TypeApply(int pos, Tree fn, Tree[] args) { - try { - switch (fn.type) { - case Type.OverloadedType(Symbol[] alts, Type[] alttypes): - global.nextPhase(); - infer.polyAlternative(fn, alts, alttypes, args.length); - global.prevPhase(); - } - switch (fn.type) { - case Type.PolyType(Symbol[] tparams, Type restpe): - global.nextPhase(); - restpe = restpe.subst(tparams, Tree.typeOf(args)); - global.prevPhase(); - return make.TypeApply(pos, fn, args).setType(restpe); - } - } catch (Type.Error ex) { - } - throw new ApplicationError("poly type required", fn.type); + /** Builds a Block node with given non-empty statements list. */ + public Tree Block(Tree[] stats) { + return Block(stats[0].pos, stats); } - public Tree TypeApply(Tree fn, Tree[] args) { - return TypeApply(fn.pos, fn, args); + /** Builds an Assign node corresponding to "<lhs> = <rhs>". */ + public Assign Assign(int pos, Tree lhs, Tree rhs) { + Assign tree = make.Assign(pos, lhs, rhs); + tree.setType(definitions.UNIT_TYPE); + return tree; } - - public Tree If(int pos, Tree cond, Tree thenpart, Tree elsepart) { - return - make.If(pos, cond, thenpart, elsepart).setType(thenpart.type); + public Assign Assign(Tree lhs, Tree rhs) { + return Assign(lhs.pos, lhs, rhs); } - public Tree If(Tree cond, Tree thenpart, Tree elsepart) { + /** Builds an If node with given condition and branches. */ + public If If(int pos, Tree cond, Tree thenpart, Tree elsepart) { + If tree = make.If(pos, cond, thenpart, elsepart); + global.nextPhase(); + if (thenpart.type.isSameAs(elsepart.type)) + tree.setType(thenpart.type); + else + tree.setType(Type.lub(new Type[] {thenpart.type, elsepart.type})); + global.prevPhase(); + return tree; + } + public If If(Tree cond, Tree thenpart, Tree elsepart) { return If(cond.pos, cond, thenpart, elsepart); } - /** Build and applied type node with given function - * and argument trees. - public Tree AppliedType(int pos, Tree fn, Tree[] args) { - return make.AppliedType(pos, fn, args) - .setType(Type.appliedType(fn.type, Tree.typeOf(args))); + /** Builds a New node corresponding to "new <constr>". */ + public Tree New(int pos, Tree constr) { + Symbol local = localDummy(pos, Symbol.NONE); // !!! + Template templ = make.Template( + pos, local, new Tree[]{constr}, Tree.EMPTY_ARRAY); // !!! + templ.setType(constr.type); + New tree = make.New(pos, templ); + tree.setType(constr.type); + return tree; } - - public Tree AppliedType(Tree fn, Tree[] args) { - return AppliedType(fn.pos, fn, args); + public Tree New(Tree constr) { + return New(constr.pos, constr); } - */ - /** Build and attribute select node of given symbol. - * It is assumed that the prefix is not empty. - */ - public Tree Select(int pos, Tree qual, Symbol sym) { - assert sym.kind != NONE; - Global.instance.nextPhase(); - Type symtype = qual.type.memberType(sym); - Global.instance.prevPhase(); - sym.flags |= ACCESSED | SELECTOR; - return make.Select(pos, sym, qual).setType(deref(symtype)); + /** Builds a Typed nodes with given value and type. */ + public Typed Typed(int pos, Tree value, Type type) { + Typed tree = make.Typed(pos, value, TypeTerm(pos, type)); + tree.setType(type); + return tree; } - - public Tree Select(Tree qual, Symbol sym) { - return Select(qual.pos, qual, sym); + public Typed Typed(Tree value, Type type) { + return Typed(value.pos, value, type); } - public Tree Select(Tree qual, Name name) { - Symbol sym = qual.type.lookup(name); - assert (sym.kind != NONE && sym != Symbol.ERROR) : name + " from " + qual.type; - return Select(qual, sym); - } + //######################################################################## + // Public Methods - Building definitions - /** Build and attribute ident node with given symbol. - */ - public Tree Ident(int pos, Symbol sym) { - assert sym.isTerm(): Debug.show(sym); - sym.flags |= ACCESSED; - return make.Ident(pos, sym).setType(deref(sym.nextType())); + /** Builds the type parameter section of given symbol. */ + public AbsTypeDef[] mkTypeParamsOf(Symbol sym) { + Symbol[] tparams = sym.nextTypeParams(); + AbsTypeDef[] trees = new AbsTypeDef[tparams.length]; + for (int i = 0; i < tparams.length; i++) + trees[i] = mkTypeParam(tparams[i]); + return trees; } - public Tree Ident(Symbol sym) { - return Ident(sym.pos, sym); + /** Builds the value parameter section of given symbol. */ + public ValDef[][] mkParamsOf(Symbol sym) { + global.nextPhase(); + if (sym.isClass()) sym = sym.primaryConstructor(); + Type type = sym.type(); + global.prevPhase(); + ValDef[][] treess = Tree.ValDef_EMPTY_ARRAY_ARRAY; + while (true) { + switch (type) { + case PolyType(_, Type result): + type = result; + continue; + case MethodType(Symbol[] vparams, Type result): + ValDef[] trees = new ValDef[vparams.length]; + for (int i = 0; i < vparams.length; i++) + trees[i] = mkParam(vparams[i]); + ValDef[][] array = new ValDef[treess.length + 1][]; + for (int i = 0; i < treess.length; i++) array[i] = treess[i]; + array[treess.length] = trees; + treess = array; + type = result; + continue; + default: + return treess; + } + } } - /** Build and attribute this node with given symbol. - */ - public Tree This(int pos, Symbol sym) { - return make.This(pos, sym).setType(sym.thisType()); + /** Builds the type parameter corresponding to given symbol. */ + public AbsTypeDef mkTypeParam(Symbol sym) { + return AbsTypeDef(sym); } - /** Build and attribute super node with given type. - */ - public Tree Super(int pos, Symbol sym) { - return make.Super(pos, sym, TypeNames.EMPTY).setType(sym.thisType()); + /** Builds the value parameter corresponding to given symbol. */ + public ValDef mkParam(Symbol sym) { + return ValDef(sym, Tree.Empty); } - /** Build and attribute value/variable/let definition node whose signature - * corresponds to given symbol and which has given rhs. - */ - public ValDef ValDef(int pos, Symbol sym, Tree rhs) { + /** Builds a definition for given interface with given body. */ + public Tree mkInterfaceDef(Symbol clazz, Tree[] body) { Global.instance.nextPhase(); - Type symtype = sym.type(); + clazz.info(); // needed until isInterface() triggers flag updates + assert clazz.isInterface(): Debug.show(clazz); + Type[] parents = clazz.parents(); Global.instance.prevPhase(); - ValDef res = make.ValDef(pos, sym, TypeTerm(pos, symtype), rhs); - res.setType(definitions.UNIT_TYPE); - return res; + return ClassDef_(clazz, mkPrimaryConstrs(clazz.pos, parents), body); } - public ValDef ValDef(Symbol sym, Tree rhs) { - return ValDef(sym.pos, sym, rhs); + /** Builds a ClassDef node for given class with given template. */ + public ClassDef ClassDef(Symbol clazz, Template template) { + ClassDef tree = make.ClassDef( + clazz.pos, + clazz, + mkTypeParamsOf(clazz), + mkParamsOf(clazz), + Tree.Empty, + template); + tree.setType(definitions.UNIT_TYPE); + return tree; } - /** Build and attribute value/variable/let definition node whose signature - * corresponds to given symbol and which has given body. + /** + * Builds a ClassDef node for given class with given parent + * constructors, local symbol and body. */ - public Tree DefDef(int pos, Symbol sym, Tree body) { - Global.instance.nextPhase(); - Type symtype = sym.type(); - Global.instance.prevPhase(); - return make.DefDef(pos, - sym, - mkTypeParams(symtype.typeParams()), - mkParams(symtype), - TypeTerm(pos, symtype.resultType()), - body) - .setType(definitions.UNIT_TYPE); + public ClassDef ClassDef(Symbol clazz, Tree[] constrs, Symbol local, + Tree[] body) + { + Template templ = make.Template(local.pos, local, constrs, body); + templ.setType(clazz.nextInfo()); // !!! + return ClassDef(clazz, templ); } - public Tree DefDef(Symbol sym, Tree rhs) { - return DefDef(sym.pos, sym, rhs); + /** Builds a ValDef node for given symbol and with given rhs. */ + public ValDef ValDef(Symbol sym, Tree rhs) { + ValDef tree = make.ValDef( + sym.pos, + sym, + TypeTerm(sym.pos, sym.nextType()), + rhs); + tree.setType(definitions.UNIT_TYPE); + return tree; + } + + /** Builds a DefDef node for given symbol with given body. */ + public DefDef DefDef(Symbol sym, Tree body) { + DefDef tree = make.DefDef( + sym.pos, + sym, + mkTypeParamsOf(sym), + mkParamsOf(sym), + TypeTerm(sym.pos, sym.nextType().resultType()), + body); + tree.setType(definitions.UNIT_TYPE); + return tree; + } + + /** Builds an AbsTypeDef node for given symbol. */ + public AbsTypeDef AbsTypeDef(Symbol sym) { + AbsTypeDef tree = make.AbsTypeDef( + sym.pos, + sym, + TypeTerm(sym.pos, sym.nextInfo()), + TypeTerm(sym.pos, sym.loBound())); + tree.setType(definitions.UNIT_TYPE); + return tree; } - /** Generate class definition from class symbol, and template. - */ - public Tree ClassDef(int pos, Symbol clazz, Template template) { - Global.instance.nextPhase(); - Type constrtype = clazz.primaryConstructor().info(); - Global.instance.prevPhase(); - return make.ClassDef( - pos, - clazz, - mkTypeParams(constrtype.typeParams()), - mkParams(constrtype), - Tree.Empty, - template) - .setType(definitions.UNIT_TYPE); + /** Builds an AliasTypeDef node for given symbol. */ + public AliasTypeDef AliasTypeDef(Symbol sym) { + AliasTypeDef tree = make.AliasTypeDef( + sym.pos, + sym, + mkTypeParamsOf(sym), + TypeTerm(sym.pos, sym.nextInfo())); + tree.setType(definitions.UNIT_TYPE); + return tree; } - public Tree ClassDef(Symbol clazz, Template template) { - return ClassDef(clazz.pos, clazz, template); - } + //######################################################################## + //######################################################################## + //######################################################################## - /** Generate class definition from class symbol, parent constructors, and body. - */ - public Tree ClassDef(int pos, Symbol clazz, Tree[] constrs, Symbol local, Tree[] body) { - Global.instance.nextPhase(); - Type clazzinfo = clazz.info(); - Global.instance.prevPhase(); - switch (clazzinfo) { - case CompoundType(Type[] parents, Scope members): - Template templ = make.Template(pos, local, constrs, body); - templ.setType(clazzinfo); - return ClassDef(pos, clazz, templ); - default: - throw Debug.abort("illegal case", clazzinfo); - } - } + //######################################################################## + // !!! to add - public Tree ClassDef(int pos, Symbol clazz, Tree[] constrs, Tree[] body) { - return ClassDef(pos, clazz, constrs, localDummy(pos, clazz), body); + /** Builds a Template node with given symbol, parents and body. */ + public Template Template(int pos, Symbol local, Tree[]parents, Tree[]body){ + Template tree = make.Template(pos, local, parents, body); + tree.setType(Type.NoType); + return tree; } - - public Tree ClassDef(Symbol clazz, Tree[] constrs, Symbol local, Tree[] body) { - return ClassDef(clazz.pos, clazz, constrs, local, body); + public Template Template(Symbol local, Tree[] parents, Tree[] body) { + return Template(local.pos, local, parents, body); } - public Tree ClassDef(Symbol clazz, Tree[] constrs, Tree[] body) { - return ClassDef(clazz.pos, clazz, constrs, body); + + //######################################################################## + // !!! to remove + + public ClassDef ClassDef_(Symbol clazz, Tree[] constrs, Tree[] body) { + return ClassDef(clazz, constrs, localDummy(clazz.pos, clazz), body); } - /** Generate class definition from interface symbol */ - public Tree mkInterfaceDef(Symbol clazz, Tree[] body) { - Global.instance.nextPhase(); - Type[] parents = clazz.parents(); - assert clazz.isInterface(): Debug.show(clazz); - Global.instance.prevPhase(); - return ClassDef(clazz, mkPrimaryConstrs(clazz.pos, parents), body); + public Tree Select__(Tree qual, Name name) { + Symbol sym = qual.type.lookup(name); + assert (sym.kind != NONE && sym != Symbol.ERROR) : name + " from " + qual.type; + return Select(qual, sym); } + //######################################################################## + // !!! not yet reviewed + + /** Create a dummy symbol to be used for templates. + */ + public Symbol localDummy(int pos, Symbol owner) { + return new TermSymbol(pos, Names.LOCAL(owner), owner, 0) + .setInfo(Type.NoType); + } /** Build the expansion of (() => expr) */ - public Tree mkUnitFunction(Tree expr, Type tp, Symbol owner) { - return mkFunction(expr.pos, Tree.ValDef_EMPTY_ARRAY, expr, tp, owner); + public Tree mkUnitFunction(Tree expr, Type type, Symbol owner) { + return mkFunction(expr.pos, Tree.ValDef_EMPTY_ARRAY, expr, type, owner); } /** Build the expansion of ((vparams_1, ..., vparams_n) => body) @@ -712,9 +734,9 @@ public class TreeGen implements Kinds, Modifiers, TypeTags { changeOwner(body, owner, applyMeth); Tree[] parentTrees = mkPrimaryConstrs(pos, parentTypes); Tree[] memberTrees = { DefDef(applyMeth, body) }; - Tree classDef = ClassDef(clazz, parentTrees, memberTrees); - Tree alloc = New(pos, Type.localThisType, clazz, Tree.EMPTY_ARRAY) - .setType(parentTypes[1]); + Tree classDef = ClassDef_(clazz, parentTrees, memberTrees); + Tree alloc = New(pos, mkPrimaryConstr(pos, clazz)) + .setType(parentTypes[1]); // !!! return Block(new Tree[]{classDef, alloc}); } @@ -735,9 +757,9 @@ public class TreeGen implements Kinds, Modifiers, TypeTags { pattype, restype, clazz, owner), makeVisitorMethod(pos, Names.isDefinedAt, isDefinedAtVisitor, pattype, definitions.BOOLEAN_TYPE, clazz, owner)}; - Tree classDef = ClassDef(clazz, parentTrees, memberTrees); - Tree alloc = New(pos, Type.localThisType, clazz, Tree.EMPTY_ARRAY) - .setType(parentTypes[1]); + Tree classDef = ClassDef_(clazz, parentTrees, memberTrees); + Tree alloc = New(pos, mkPrimaryConstr(pos, clazz)) + .setType(parentTypes[1]); // !!! return Block(new Tree[]{classDef, alloc}); } //where @@ -752,7 +774,7 @@ public class TreeGen implements Kinds, Modifiers, TypeTags { changeOwner(visitor, prevOwner, meth); Tree body = mkApply( - Select(Ident(param), definitions.MATCH), + Select(Ident(pos, param), definitions.MATCH), new Tree[]{mkType(pos, pattype), mkType(pos, restype)}, new Tree[]{visitor}); return DefDef(meth, body); @@ -779,14 +801,14 @@ public class TreeGen implements Kinds, Modifiers, TypeTags { */ public Tree postfixApply(Tree obj, Tree fn, Symbol owner) { if (TreeInfo.isPureExpr(obj) || TreeInfo.isPureExpr(fn)) { - return Apply(Select(fn, Names.apply), new Tree[]{obj}); + return Apply(Select__(fn, Names.apply), new Tree[]{obj}); } else { Name tmpname = global.freshNameCreator.newName("tmp", '$'); Symbol tmp = new TermSymbol( obj.pos, tmpname, owner, SYNTHETIC | FINAL) .setInfo(obj.type); Tree tmpdef = ValDef(tmp, obj); - Tree expr = postfixApply(Ident(tmp), fn, owner); + Tree expr = postfixApply(Ident(obj.pos, tmp), fn, owner); return Block(new Tree[]{tmpdef, expr}); } } diff --git a/sources/scalac/transformer/AddAccessors.java b/sources/scalac/transformer/AddAccessors.java index e2dc69b7f1..14bf9c610a 100644 --- a/sources/scalac/transformer/AddAccessors.java +++ b/sources/scalac/transformer/AddAccessors.java @@ -44,8 +44,8 @@ public class AddAccessors extends Transformer { accessor = new TermSymbol(sym.pos, sym.name, sym.classOwner(), - Modifiers.STABLE - | Modifiers.ACCESSOR + /* !!! Modifiers.STABLE + | */ Modifiers.ACCESSOR | Modifiers.PRIVATE); accessor.setType(Type.MethodType(Symbol.EMPTY_ARRAY, sym.type())); accessorMap.put(sym, accessor); @@ -82,18 +82,19 @@ public class AddAccessors extends Transformer { for (int i = 0; i < params.length; ++i) { Symbol paramSym = params[i].symbol(); if (accessorMap.containsKey(paramSym)) { + int pos = paramSym.pos; Symbol accessorSym = (Symbol)accessorMap.get(paramSym); - Symbol valSym = new TermSymbol(paramSym.pos, + Symbol valSym = new TermSymbol(pos, valName(paramSym), clsSym, Modifiers.PRIVATE); valSym.setType(paramSym.type()); - newBody.addFirst(gen.DefDef(accessorSym, gen.Ident(valSym))); + newBody.addFirst(gen.DefDef(accessorSym, gen.Ident(pos, valSym))); newMembers.enter(accessorSym); - newBody.addFirst(gen.ValDef(valSym, gen.Ident(paramSym))); + newBody.addFirst(gen.ValDef(valSym, gen.Ident(pos, paramSym))); newMembers.enter(valSym); } } @@ -120,8 +121,7 @@ public class AddAccessors extends Transformer { Symbol sym = tree.symbol(); assert sym.kind != Kinds.NONE : tree; if (sym.owner().isPrimaryConstructor()) - return gen.Apply(gen.Select(transform(qualifier), accessor(sym)), - Tree.EMPTY_ARRAY); + return gen.Apply(gen.Select(tree.pos, transform(qualifier), accessor(sym))); else return copy.Select(tree, sym, transform(qualifier)); } @@ -131,7 +131,7 @@ public class AddAccessors extends Transformer { if (inClassContext && sym.name.isTermName() && sym.owner().isPrimaryConstructor()) - return gen.Apply(gen.Ident(accessor(sym)), Tree.EMPTY_ARRAY); + return gen.Apply(gen.Ident(tree.pos, accessor(sym))); else return copy.Ident(tree, sym); } diff --git a/sources/scalac/transformer/AddConstructors.java b/sources/scalac/transformer/AddConstructors.java index 8fa3987b81..e8799ca51b 100644 --- a/sources/scalac/transformer/AddConstructors.java +++ b/sources/scalac/transformer/AddConstructors.java @@ -120,8 +120,7 @@ public class AddConstructors extends Transformer { public Tree transform(Tree tree) { final Symbol treeSym = tree.symbol(); switch (tree) { - case ClassDef(_, _, _, ValDef[][] vparams, _, //: - Template(Tree[] baseClasses, Tree[] body)): + case ClassDef(_, _, _, ValDef[][] vparams, _, Template impl): assert treeSym.name.isTypeName(); @@ -154,8 +153,8 @@ public class AddConstructors extends Transformer { Debug.show(constrSym.owner()) + "\n\texpected: " + Debug.show(treeSym); - for (int i = 0; i < body.length; i++) { - Tree t = body[i]; + for (int i = 0; i < impl.body.length; i++) { + Tree t = impl.body[i]; if (t.definesSymbol()) { Symbol sym = t.symbol(); switch (t) { @@ -191,24 +190,22 @@ public class AddConstructors extends Transformer { classScope.enterOrOverload(sym); } else { // move class-level expressions into the constructor - constrBody2.add(transform(body[i])); + constrBody2.add(transform(impl.body[i])); } } // inline the call to the super constructor if ( !forINT || !treeSym.parents()[0].symbol().isJava()) { - switch (baseClasses[0]) { + switch (impl.parents[0]) { case Apply(Tree fun, Tree[] args): - int pos = baseClasses[0].pos; + int pos = impl.parents[0].pos; Tree superConstr = gen.Select (gen.Super(pos, treeSym), getConstructor(fun.symbol())); constrBody.add(gen.Apply(superConstr, transform(args))); break; default: - new scalac.ast.printer.TextTreePrinter(). - print(baseClasses[0]).println().end(); - assert false; + throw Debug.abort("illegal case", impl.parents[0]); } } @@ -248,7 +245,7 @@ public class AddConstructors extends Transformer { toArray(new Tree[constrBody.size()])): (Tree) constrBody.get(0); - classBody.add(gen.DefDef(tree.pos, constrSym, constrTree)); + classBody.add(gen.DefDef(constrSym, constrTree)); // strip off the class constructor from parameters switch (treeSym.primaryConstructor().info()) { @@ -269,7 +266,7 @@ public class AddConstructors extends Transformer { for (int i = 0; i < newBody.length - 1; i ++) newBody[i] = transform(newBody[i]); - return gen.ClassDef(classSym, baseClasses, newBody); + return gen.ClassDef(classSym, impl.parents, impl.symbol(), newBody); // Substitute the constructor into the 'new' expressions case New(Template(Tree[] baseClasses, _)): diff --git a/sources/scalac/transformer/AddInterfaces.java b/sources/scalac/transformer/AddInterfaces.java index 287cd7e6b4..15747cce48 100644 --- a/sources/scalac/transformer/AddInterfaces.java +++ b/sources/scalac/transformer/AddInterfaces.java @@ -183,7 +183,7 @@ class AddInterfaces extends Transformer { Map memMap = phase.getClassMemberMap(realClsSym); assert memMap != null : Debug.show(clsSym) + " " + Debug.show(realClsSym); - return gen.Select(qualifier, (Symbol)memMap.get(sym)); + return gen.Select(tree.pos, qualifier, (Symbol)memMap.get(sym)); } else return super.transform(tree); } else { @@ -197,13 +197,7 @@ class AddInterfaces extends Transformer { Type qualifierType = qualifier.type().bound(); if (phase.needInterface(qualifierType.symbol())) { Type castType = qualifierType.baseType(owner); - qualifier = - gen.Apply( - gen.TypeApply( - gen.Select(qualifier, defs.AS), - new Tree[] { - gen.mkType(tree.pos, castType)}), - Tree.EMPTY_ARRAY); + qualifier = gen.mkAsInstanceOf(qualifier, castType); } } return copy.Select(tree, sym, qualifier); @@ -223,13 +217,13 @@ class AddInterfaces extends Transformer { : Debug.show(clsSym) + " " + Debug.show(realClsSym); assert memMap.containsKey(sym) : Debug.show(sym) + " not in " + memMap; - return gen.Ident((Symbol)memMap.get(sym)); + return gen.Ident(tree.pos, (Symbol)memMap.get(sym)); } else return super.transform(tree); } else if (typeSubst != null) { Symbol newSym = (Symbol)typeSubst.lookupSymbol(tree.symbol()); if (newSym != null) - return gen.Ident(newSym); + return gen.Ident(tree.pos, newSym); else return super.transform(tree); } else { diff --git a/sources/scalac/transformer/Erasure.java b/sources/scalac/transformer/Erasure.java index e65e3c4a16..7f62094608 100644 --- a/sources/scalac/transformer/Erasure.java +++ b/sources/scalac/transformer/Erasure.java @@ -283,9 +283,9 @@ public class Erasure extends Transformer implements Modifiers { assert params1.length == symparams.length; Tree[] args = new Tree[params1.length]; for (int i = 0; i < args.length; i++) { - args[i] = cast(gen.Ident(params1[i]), symparams[i].type().erasure()); + args[i] = cast(gen.Ident(sym.pos, params1[i]), symparams[i].type().erasure()); } - Tree fwd = make.Apply(sym.pos, gen.Ident(sym).setType(symtype), args) + Tree fwd = make.Apply(sym.pos, gen.Ident(sym.pos, sym).setType(symtype), args) .setType(symrestp); bridges.append(gen.DefDef(bridgeSym, coerce(fwd, restp))); return; @@ -518,7 +518,7 @@ public class Erasure extends Transformer implements Modifiers { case LabelDef(Name name, Tree.Ident[] params,Tree body): Tree.Ident[] new_params = new Tree.Ident[params.length]; for (int i = 0; i < params.length; i++) { - new_params[i] = (Tree.Ident)gen.Ident(params[i].symbol()); + new_params[i] = (Tree.Ident)gen.Ident(params[i].pos, params[i].symbol()); } return copy.LabelDef(tree, new_params, transform(body)).setType(owntype); @@ -595,7 +595,7 @@ public class Erasure extends Transformer implements Modifiers { Tree tree1; switch (tree) { case Ident(Name name): - if (name == Names.ZERO) tree1 = gen.Ident(definitions.NULL); + if (name == Names.ZERO) tree1 = gen.mkNullLit(tree.pos); else tree1 = tree; break; case Select(Tree qual, _): diff --git a/sources/scalac/transformer/ExpandMixins.java b/sources/scalac/transformer/ExpandMixins.java index 8d761ca249..48d81ec4c3 100644 --- a/sources/scalac/transformer/ExpandMixins.java +++ b/sources/scalac/transformer/ExpandMixins.java @@ -157,7 +157,7 @@ public class ClassExpander { for (int i = 0; i < params.length; i++) { Symbol member = map.lookupSymbol(params[i]); member.setType(map.apply(member.type())); - body.append(gen.ValDef(args[i].pos, member, args[i])); + body.append(gen.ValDef(member, args[i])); } } diff --git a/sources/scalac/transformer/ExpandMixinsPhase.java b/sources/scalac/transformer/ExpandMixinsPhase.java index ba0f43bd25..0b5e34fc96 100644 --- a/sources/scalac/transformer/ExpandMixinsPhase.java +++ b/sources/scalac/transformer/ExpandMixinsPhase.java @@ -151,7 +151,7 @@ public class ExpandMixinsPhase extends Phase { case ClassDef(_, _, _, _, _, _): Symbol clasz = tree.symbol(); if (clasz.isInterface()) return super.transform(tree); - return gen.ClassDef(tree.pos,clasz,getExpandedTemplate(clasz)); + return gen.ClassDef(clasz, getExpandedTemplate(clasz)); case PackageDef(_, _): return super.transform(tree); case Template(_, _): diff --git a/sources/scalac/transformer/ExplicitOuterClassesPhase.java b/sources/scalac/transformer/ExplicitOuterClassesPhase.java index 3812e1d708..81ba49cf1c 100644 --- a/sources/scalac/transformer/ExplicitOuterClassesPhase.java +++ b/sources/scalac/transformer/ExplicitOuterClassesPhase.java @@ -250,7 +250,7 @@ public class ExplicitOuterClassesPhase extends Phase { context = context.getConstructorContext(symbol); rhs = transform(rhs); context = backup; - return gen.DefDef(tree.pos, symbol, rhs); + return gen.DefDef(symbol, rhs); case This(_): return genOuterRef(tree.pos, tree.symbol()); @@ -262,13 +262,13 @@ public class ExplicitOuterClassesPhase extends Phase { // !!! A this node is missing here. This should // never happen if all trees were correct. Tree qualifier = genOuterRef(tree.pos, owner); - return gen.mkStable(gen.Select(qualifier, symbol)); + return gen.Select(qualifier, symbol); } if (!owner.isConstructor()) break; Symbol clasz = owner.constructorClass(); if (clasz == context.clasz) break; Tree qualifier = genOuterRef(tree.pos, clasz); - return gen.mkStable(gen.Select(qualifier, symbol)); + return gen.Select(qualifier, symbol); case Select(Super(_, _), _): Tree qualifier = ((Tree.Select)tree).qualifier; @@ -276,7 +276,7 @@ public class ExplicitOuterClassesPhase extends Phase { if (clasz == context.clasz) break; Symbol symbol = getSuperMethod(clasz, tree.symbol()); qualifier = genOuterRef(qualifier.pos, clasz); - return gen.mkStable(gen.Select(tree.pos, qualifier, symbol)); + return gen.Select(tree.pos, qualifier, symbol); case Apply(Tree vfun, Tree[] vargs): switch (vfun) { @@ -351,8 +351,8 @@ public class ExplicitOuterClassesPhase extends Phase { Symbol method = (Symbol)entry.getKey(); Symbol forward = (Symbol)entry.getValue(); int pos = forward.pos; - Tree[] targs = gen.mkTypeIdents(pos, nextTypeParams(forward)); - Tree[] vargs = gen.mkIdents(pos, nextValueParams(forward)); + Tree[] targs = gen.mkTypeRefs(pos, nextTypeParams(forward)); + Tree[] vargs = gen.mkRefs(pos, nextValueParams(forward)); Tree fun = gen.Select(gen.Super(pos, context.clasz), method); trees[i] = gen.DefDef(forward, gen.mkApply(fun, targs, vargs)); } @@ -362,11 +362,11 @@ public class ExplicitOuterClassesPhase extends Phase { /** Returns a tree referencing the given outer class. */ private Tree genOuterRef(int pos, Symbol clasz) { if (context.clasz == clasz) return gen.This(pos, clasz); - Tree tree = gen.mkStable(gen.Ident(pos, context.link)); + Tree tree = gen.Ident(pos, context.link); for (Context context = this.context.outer;;context =context.outer){ assert context != null: Debug.show(clasz); if (context.clasz == clasz) return tree; - tree = gen.mkStable(gen.Select(tree, context.link)); + tree = gen.Select(tree, context.link); } } diff --git a/sources/scalac/transformer/LambdaLift.java b/sources/scalac/transformer/LambdaLift.java index af1881ea7d..f3d19647c6 100644 --- a/sources/scalac/transformer/LambdaLift.java +++ b/sources/scalac/transformer/LambdaLift.java @@ -398,14 +398,9 @@ public class LambdaLift extends OwnerTransformer if ((sym.flags & CAPTURED) != 0) { assert sym.isLocal(); Type boxedType = sym.nextType(); - Type unboxedType = boxedType.typeArgs()[0]; tpe1 = gen.mkType(tpe.pos, boxedType); rhs1 = gen.New( - rhs.pos, - definitions.SCALA_TYPE, - boxedType.symbol(), - new Type[]{unboxedType}, - new Tree[]{rhs1}); + gen.mkPrimaryConstr(rhs.pos, boxedType, new Tree[]{rhs1})); } return copy.ValDef(tree, sym, tpe1, rhs1); @@ -451,7 +446,7 @@ public class LambdaLift extends OwnerTransformer if (tree1 instanceof Ident) ((Ident)tree1).name = sym.name; else ((Select)tree1).selector = sym.name; } - if ((sym.flags & CAPTURED) != 0) return gen.Select(tree1, Names.elem); + if ((sym.flags & CAPTURED) != 0) return gen.Select__(tree1, Names.elem); else return tree1; default: @@ -621,7 +616,7 @@ public class LambdaLift extends OwnerTransformer for (int i = 0; i < fparams.length; i++) { Symbol farg = descr.proxy(fparams[i], currentOwner); args1[args.length + i] = - types ? gen.mkTypeIdent(pos, farg) : gen.Ident(pos, farg); + types ? gen.mkTypeRef(pos, farg) : gen.Ident(pos, farg); } return args1; } else { diff --git a/sources/scalac/transformer/UnCurry.java b/sources/scalac/transformer/UnCurry.java index 6294631ce5..02df321e64 100644 --- a/sources/scalac/transformer/UnCurry.java +++ b/sources/scalac/transformer/UnCurry.java @@ -147,7 +147,7 @@ public class UnCurry extends OwnerTransformer if (tree1.symbol().isDefParameter()) { tree1.type = global.definitions.functionType( Type.EMPTY_ARRAY, tree1.type.widen()); - return gen.Apply(gen.Select(tree1, Names.apply), new Tree[0]); + return gen.Apply(gen.Select__(tree1, Names.apply), new Tree[0]); } else { return tree1; } diff --git a/sources/scalac/transformer/matching/AlgebraicMatcher.java b/sources/scalac/transformer/matching/AlgebraicMatcher.java index ea07730a1a..ea74702a99 100644 --- a/sources/scalac/transformer/matching/AlgebraicMatcher.java +++ b/sources/scalac/transformer/matching/AlgebraicMatcher.java @@ -358,9 +358,8 @@ public class AlgebraicMatcher extends PatternMatcher { public Tree toTree() { TreeList ts = new TreeList(); - ts.append( gen.ValDef(_m.pos, root.symbol(), _m.selector )); - ts.append( gen.ValDef(_m.pos, - resultVar, + ts.append( gen.ValDef(root.symbol(), _m.selector )); + ts.append( gen.ValDef(resultVar, gen.mkDefaultValue(_m.pos, resultVar.info()) )); ts.append( cf.If( toTree(root.and), gen.Ident( _m.pos, resultVar ), diff --git a/sources/scalac/transformer/matching/Autom2Scala.java b/sources/scalac/transformer/matching/Autom2Scala.java index abe5a6b35a..adcd929e43 100644 --- a/sources/scalac/transformer/matching/Autom2Scala.java +++ b/sources/scalac/transformer/matching/Autom2Scala.java @@ -154,11 +154,9 @@ public class Autom2Scala { // overridden in TracerInScala Tree loadCurrentElem( Tree body ) { return cf.Block( Position.FIRSTPOS, new Tree[] { - cf.gen.ValDef( Position.FIRSTPOS, - this.hasnSym, + cf.gen.ValDef( this.hasnSym, cf._hasNext( _iter() ) ), - cf.gen.ValDef( Position.FIRSTPOS, - this.curSym, + cf.gen.ValDef( this.curSym, cf.If( _ref( hasnSym ),//cf._hasNext( _iter() ), cf._next( _iter() ), cf.ignoreValue( curSym.type() ))), @@ -182,7 +180,7 @@ public class Autom2Scala { /** creates an int variable */ Tree _intvar( Symbol sym, Tree init ) { - return gen.ValDef( pos, sym, init ); + return gen.ValDef( sym, init ); } // the caller needs to set the type ! diff --git a/sources/scalac/transformer/matching/CodeFactory.java b/sources/scalac/transformer/matching/CodeFactory.java index 96c3cd66ee..9800ff4eb1 100644 --- a/sources/scalac/transformer/matching/CodeFactory.java +++ b/sources/scalac/transformer/matching/CodeFactory.java @@ -281,21 +281,21 @@ class CodeFactory extends PatternTool { } Tree newSeqNil( Type tpe ) { - return gen.Select(gen.Ident(pos, defs.SCALA), Names.Nil/*seqNilSym()*/); + return gen.Select__(gen.Ident(pos, defs.SCALA), Names.Nil/*seqNilSym()*/); } // EXPERIMENTAL Tree newRef( Tree init ) { //System.out.println( "hello:"+refSym().type() ); - return gen.New( pos, defs.SCALA_TYPE, refSym(), - new Type[] { init.type() }, - new Tree[] { init } ); + return gen.New(gen.mkPrimaryConstr(pos, refSym(), + new Type[] { init.type() }, + new Tree[] { init } )); } Tree newSeqCons( Tree head, Tree tail ) { - return gen.New( pos, defs.SCALA_TYPE, seqConsSym(), - new Type[] { head.type() }, - new Tree[] { head, tail }); + return gen.New(gen.mkPrimaryConstr(pos, seqConsSym(), + new Type[] { head.type() }, + new Tree[] { head, tail })); } /** returns A for T <: Sequence[ A ] @@ -440,7 +440,7 @@ class CodeFactory extends PatternTool { } return make.Apply( tree.pos, - gen.Select(tree, NOT_N), + gen.Select__(tree, NOT_N), Tree.EMPTY_ARRAY).setType(defs.BOOLEAN_TYPE); } @@ -579,9 +579,9 @@ class CodeFactory extends PatternTool { } Tree newPair( Tree left, Tree right ) { - return gen.New( pos, defs.SCALA_TYPE, tuple2Sym(), - new Type[] { left.type(), right.type() }, - new Tree[] { left, right }); + return gen.New(gen.mkPrimaryConstr(pos, tuple2Sym(), + new Type[] { left.type(), right.type() }, + new Tree[] { left, right })); } diff --git a/sources/scalac/transformer/matching/LeftTracerInScala.java b/sources/scalac/transformer/matching/LeftTracerInScala.java index c79fa113e3..cb58f4b196 100644 --- a/sources/scalac/transformer/matching/LeftTracerInScala.java +++ b/sources/scalac/transformer/matching/LeftTracerInScala.java @@ -213,14 +213,14 @@ public class LeftTracerInScala extends TracerInScala { .setType( _accumType( elementType ) ) ; // `val acc = SeqNil[ elementType ]' init accumulator - v.add( gen.ValDef( pos, emptyAccSym, emptyAcc) ); + v.add( gen.ValDef( emptyAccSym, emptyAcc) ); Tree run = callFun( new Tree[] { gen.Ident( pos, emptyAccSym ), cf.newIterator( selector, selector.type() ), cf.Int( 0 ) }); - run = gen.ValDef( Position.FIRSTPOS, resultSym, run ); + run = gen.ValDef( resultSym, run ); v.add( run ); diff --git a/sources/scalac/transformer/matching/PatternMatcher.java b/sources/scalac/transformer/matching/PatternMatcher.java index ca196c37e6..1ee3b2574f 100644 --- a/sources/scalac/transformer/matching/PatternMatcher.java +++ b/sources/scalac/transformer/matching/PatternMatcher.java @@ -902,7 +902,7 @@ public class PatternMatcher extends PatternTool { cases = cases.next; } return cf.Switch( - gen.Apply(gen.Select(selector.duplicate(), Names.tag), new Tree[0]), + gen.Apply(gen.Select__(selector.duplicate(), Names.tag), new Tree[0]), tags, bodies, (defaultCase == null) ? gen.mkBooleanLit(selector.pos, false) diff --git a/sources/scalac/typechecker/RefCheck.java b/sources/scalac/typechecker/RefCheck.java index b2e394458a..da3bef4476 100644 --- a/sources/scalac/typechecker/RefCheck.java +++ b/sources/scalac/typechecker/RefCheck.java @@ -455,20 +455,8 @@ public class RefCheck extends Transformer implements Modifiers, Kinds { private Tree[] transformModule(Tree tree, int mods, Name name, Tree tpe, Tree.Template templ) { Symbol sym = tree.symbol(); - Tree cdef = make.ClassDef( - tree.pos, - mods | FINAL | MODUL, - name.toTypeName(), - Tree.AbsTypeDef_EMPTY_ARRAY, - Tree.ValDef_EMPTY_ARRAY_ARRAY, - Tree.Empty, - templ) - .setSymbol(sym.moduleClass()).setType(tree.type); - Tree alloc = gen.New( - tree.pos, - sym.type().prefix(), - sym.moduleClass(), - Tree.EMPTY_ARRAY); + Tree cdef = gen.ClassDef(sym.moduleClass(), templ); + Tree alloc = gen.New(gen.mkPrimaryConstr(tree.pos, sym.moduleClass())); if (sym.isGlobalModule()) { Tree vdef = gen.ValDef(sym, alloc); return new Tree[]{cdef, vdef}; @@ -506,7 +494,7 @@ public class RefCheck extends Transformer implements Modifiers, Kinds { m_eq.setInfo( Type.MethodType(new Symbol[] {m_eqarg}, defs.UNIT_TYPE)); Tree m_eqdef = gen.DefDef(m_eq, - gen.Assign(gen.mkRef(tree.pos, mvar), gen.Ident(m_eqarg))); + gen.Assign(gen.mkRef(tree.pos, mvar), gen.Ident(tree.pos, m_eqarg))); sym.owner().members().enterOrOverload(m_eq); return new Tree[]{cdef, vdef, ddef, m_eqdef}; @@ -597,7 +585,7 @@ public class RefCheck extends Transformer implements Modifiers, Kinds { new Tree[]{gen.mkStringLit(clazz.pos, str)}); } } - return gen.DefDef(clazz.pos, toStringSym, body); + return gen.DefDef(toStringSym, body); } private Tree equalsMethod(ClassSymbol clazz) { @@ -675,7 +663,7 @@ public class RefCheck extends Transformer implements Modifiers, Kinds { clazz.isSubClass(defs.OBJECT_CLASS) ? OVERRIDE : 0) .setInfo(Type.MethodType(Symbol.EMPTY_ARRAY, defs.INT_TYPE)); clazz.info().members().enter(tagSym); - return gen.DefDef(clazz.pos, tagSym, gen.mkIntLit(clazz.pos, clazz.tag())); + return gen.DefDef(tagSym, gen.mkIntLit(clazz.pos, clazz.tag())); } private Tree hashCodeMethod(ClassSymbol clazz) { @@ -712,7 +700,7 @@ public class RefCheck extends Transformer implements Modifiers, Kinds { addMethod), new Tree[]{operand}); } - return gen.DefDef(clazz.pos, hashCodeSym, body); + return gen.DefDef(hashCodeSym, body); } // where |