From 8ba6023e7aa181b4e7e3206caf29c0550315f5b4 Mon Sep 17 00:00:00 2001 From: paltherr Date: Sat, 6 Mar 2004 15:50:23 +0000 Subject: - Generalized the use of static types/trees in ... - Generalized the use of static types/trees in static contextes. Now all this-types should refer to an enclosing class and all Tree.Select should refer a non-static class member. --- sources/scala/tools/scalai/ExpressionCompiler.java | 4 +- sources/scalac/ast/TreeGen.java | 20 +++--- sources/scalac/atree/ATreeFromSTree.java | 19 +++-- sources/scalac/backend/jvm/GenJVM.java | 84 +++++++--------------- sources/scalac/checkers/TreeChecker.java | 5 +- sources/scalac/symtab/Definitions.java | 72 ++++++++----------- .../scalac/symtab/classfile/JavaTypeCreator.java | 2 +- .../transformer/ExplicitOuterClassesPhase.java | 2 + sources/scalac/transformer/TailCallPhase.java | 3 +- 9 files changed, 80 insertions(+), 131 deletions(-) (limited to 'sources') diff --git a/sources/scala/tools/scalai/ExpressionCompiler.java b/sources/scala/tools/scalai/ExpressionCompiler.java index 0983b56339..cfeebfd1a3 100644 --- a/sources/scala/tools/scalai/ExpressionCompiler.java +++ b/sources/scala/tools/scalai/ExpressionCompiler.java @@ -216,10 +216,10 @@ public class ExpressionCompiler { return Code.Self; case Select(Tree expr, _): - return tree.symbol().isStatic() ? Code.Null : compute(expr); + return compute(expr); case Ident(_): - return Code.Self; + return Code.Null; default: throw Debug.abort("illegal tree", tree); diff --git a/sources/scalac/ast/TreeGen.java b/sources/scalac/ast/TreeGen.java index 1367311374..77bc1f2600 100644 --- a/sources/scalac/ast/TreeGen.java +++ b/sources/scalac/ast/TreeGen.java @@ -254,17 +254,15 @@ public class TreeGen implements Kinds, Modifiers, TypeTags { case NoPrefix: return Ident(pos, symbol); case ThisType(Symbol clasz): - if (clasz.isRoot()) return Ident(pos, symbol); // !!! remove ? - if (clasz.isPackage()) return mkRef(pos, mkGlobalRef(pos, clasz.module()), symbol); // !!! return mkRef(pos, This(pos, clasz), symbol); case SingleType(Type prefix, Symbol member): - Tree tree = mkRef(pos, prefix, member); - switch (tree.type()) { + Tree qualifier = mkRef(pos, prefix, member); + switch (qualifier.type()) { case MethodType(Symbol[] params, _): - assert params.length == 0: tree.type(); - tree = Apply(pos, tree); + assert params.length == 0: qualifier.type(); + qualifier = Apply(pos, qualifier); } - return mkRef(pos, tree, symbol); + return mkRef(pos, qualifier, symbol); default: throw Debug.abort("illegal case", stable); } @@ -279,10 +277,10 @@ public class TreeGen implements Kinds, Modifiers, TypeTags { /** Builds a global reference to given symbol. */ public Tree mkGlobalRef(int pos, Symbol symbol) { assert symbol.isTerm(): Debug.show(symbol); - Symbol owner = symbol.owner(); - if (owner.isRoot()) return Ident(pos, symbol); - assert owner.isModuleClass(): Debug.show(symbol); - return mkRef(pos, mkGlobalRef(pos, owner.module()), symbol); + global.nextPhase(); + Type prefix = symbol.owner().staticPrefix(); + global.prevPhase(); + return mkRef(pos, prefix, symbol); } /** Builds a This node corresponding to given class. */ diff --git a/sources/scalac/atree/ATreeFromSTree.java b/sources/scalac/atree/ATreeFromSTree.java index 6c83e5e6a8..c67ac19e93 100644 --- a/sources/scalac/atree/ATreeFromSTree.java +++ b/sources/scalac/atree/ATreeFromSTree.java @@ -295,13 +295,14 @@ public class ATreeFromSTree { switch (tree) { case Select(Tree qualifier, _): - if (symbol.isJava() && symbol.owner().isModuleClass()) - return AFunction.Method(make.Void, symbol, AInvokeStyle.StaticClass); // !!! qualifier is ignored ! - ACode object = expression(qualifier); - return AFunction.Method(object, symbol, invokeStyle(qualifier)); + AInvokeStyle style = invokeStyle(qualifier); + return AFunction.Method(expression(qualifier), symbol, style); case Ident(_): - return AFunction.Method(make.Void, symbol, AInvokeStyle.New); + AInvokeStyle style = symbol.isInitializer() + ? AInvokeStyle.New + : AInvokeStyle.StaticClass; + return AFunction.Method(make.Void, symbol, style); default: throw Debug.abort("illegal case", tree); @@ -327,15 +328,13 @@ public class ATreeFromSTree { switch (tree) { case Select(Tree qualifier, _): - if (symbol.isModule()) - return ALocation.Module(symbol); // !!! qualifier is ignored ! - if (symbol.isJava() && symbol.owner().isModuleClass()) - return ALocation.Field(make.Void, symbol, true); // !!! qualifier is ignored ! return ALocation.Field(expression(qualifier), symbol, false); case Ident(_): if (symbol.isModule()) return ALocation.Module(symbol); - return ALocation.Local(symbol, symbol.isParameter()); + return symbol.owner().isClass() + ? ALocation.Field(make.Void, symbol, true) + : ALocation.Local(symbol, symbol.isParameter()); default: throw Debug.abort("illegal case", tree); diff --git a/sources/scalac/backend/jvm/GenJVM.java b/sources/scalac/backend/jvm/GenJVM.java index 1f7e08754b..1ee1c87222 100644 --- a/sources/scalac/backend/jvm/GenJVM.java +++ b/sources/scalac/backend/jvm/GenJVM.java @@ -274,7 +274,7 @@ class GenJVM { } break; case Apply(TypeApply(Tree fun, Tree[] args), _): { - genLoadQualifier(ctx, fun, true); + genLoadQualifier(ctx, fun); JType type = typeStoJ(args[0].type); if (fun.symbol() == defs.ANY_IS) { @@ -423,7 +423,7 @@ class GenJVM { boolean isStatic = isStaticMember(funSym); if (!isStatic) - genLoadQualifier(ctx, fun, !isConstrCall); + genLoadQualifier(ctx, fun); for (int i = 0; i < args.length; ++i) genLoad(ctx, args[i], argTypes[i]); @@ -456,13 +456,19 @@ class GenJVM { } } break; - case Ident(Name name): { + case Ident(_): JType type = typeStoJ(sym.info()); - if (sym.isModule()) - generatedType = genLoadModule(ctx, sym); - else if (sym.owner().isClass()) { - ctx.code.emitALOAD_0(); - ctx.code.emitGETFIELD(ctx.clazz.getName(), name.toString(), type); + if (sym.isModule()) { + String javaSymName = javaName(sym.moduleClass()); + ctx.code.emitGETSTATIC(javaSymName, + MODULE_INSTANCE_FIELD_NAME, + type); + generatedType = type; + } else if (sym.owner().isClass()) { + JType fieldType = typeStoJ(sym.info()); + String className = javaName(sym.owner()); + String fieldName = sym.name.toString(); + ctx.code.emitGETSTATIC(className, fieldName, fieldType); generatedType = type; } else { assert ctx.locals.containsKey(sym) @@ -471,25 +477,16 @@ class GenJVM { ctx.code.emitLOAD(index, type); generatedType = type; } - } break; + break; - case Select(Tree qualifier, Name selector): { - sym.info(); - if (sym.isModule()) - generatedType = genLoadModule(ctx, sym); - else { - JType fieldType = typeStoJ(sym.info()); - String className = javaName(sym.owner()); - String fieldName = selector.toString(); - if (isStaticMember(sym)) - ctx.code.emitGETSTATIC(className, fieldName, fieldType); - else { - genLoadQualifier(ctx, tree, true); - ctx.code.emitGETFIELD(className, fieldName, fieldType); - } - generatedType = fieldType; - } - } break; + case Select(Tree qualifier, _): + JType fieldType = typeStoJ(sym.info()); + String className = javaName(sym.owner()); + String fieldName = sym.name.toString(); + genLoadQualifier(ctx, tree); + ctx.code.emitGETFIELD(className, fieldName, fieldType); + generatedType = fieldType; + break; case Assign(Tree lhs, Tree rhs): { genStorePrologue(ctx, lhs); @@ -582,33 +579,14 @@ class GenJVM { return expectedType; } - /** - * Generate code to load the module represented by the given - * symbol. - */ - protected JType genLoadModule(Context ctx, Symbol sym) { - String javaSymName = javaName(sym.moduleClass()); - JType type = typeStoJ(sym.info()); - if (javaSymName.equals(ctx.clazz.getName())) - ctx.code.emitALOAD_0(); - else - ctx.code.emitGETSTATIC(javaSymName, - MODULE_INSTANCE_FIELD_NAME, - type); - - return type; - } - /** * Generate code to load the qualifier of the given tree, which * can be implicitely "this". */ - protected void genLoadQualifier(Context ctx, Tree tree, boolean implicitThis) + protected void genLoadQualifier(Context ctx, Tree tree) throws JCode.OffsetTooBigException { switch (tree) { case Ident(_): - if (implicitThis) - ctx.code.emitALOAD_0(); break; case Select(Tree qualifier, _): genLoad(ctx, qualifier, JAVA_LANG_OBJECT_T); @@ -675,19 +653,7 @@ class GenJVM { */ protected void genStorePrologue(Context ctx, Tree tree) throws JCode.OffsetTooBigException { - Symbol sym = tree.symbol(); - switch (tree) { - case Ident(_): - if (sym.owner().isClass()) - ctx.code.emitALOAD_0(); - break; - case Select(Tree qualifier, _): - if (!isStaticMember(sym)) - genLoadQualifier(ctx, tree, true); - break; - default: - throw global.fail("unexpected left-hand side", tree); - } + genLoadQualifier(ctx, tree); } /** diff --git a/sources/scalac/checkers/TreeChecker.java b/sources/scalac/checkers/TreeChecker.java index 9f48678cda..b409775629 100644 --- a/sources/scalac/checkers/TreeChecker.java +++ b/sources/scalac/checkers/TreeChecker.java @@ -342,6 +342,7 @@ public class TreeChecker { return selection(tree); case Ident(_): + if (symbol.owner().isStaticOwner()) return true; if (symbol.isInitializer()) return true; assert labels.contains(symbol): show(tree); assert symbol.owner() == currentMember(): show(tree); @@ -363,7 +364,7 @@ public class TreeChecker { return selection(tree); case Ident(_): - if (symbol.isModule()) return true; + if (symbol.owner().isStaticOwner()) return true; assert vvars.contains(symbol): show(tree); assert symbol.owner() == currentMember(): show(tree); return true; @@ -384,7 +385,7 @@ public class TreeChecker { Symbol symbol = tree.symbol(); assert symbol != null && symbol.isTerm(): show(tree); Symbol owner = symbol.owner(); - assert owner.isClassType(): show(tree); + assert owner.isClass() && !owner.isStaticOwner(): show(tree); assert qualifier.type().baseType(owner) != Type.NoType: show(tree); return expression(qualifier, qualifier.type()); diff --git a/sources/scalac/symtab/Definitions.java b/sources/scalac/symtab/Definitions.java index b418116b6b..c3a8a7679a 100644 --- a/sources/scalac/symtab/Definitions.java +++ b/sources/scalac/symtab/Definitions.java @@ -42,38 +42,38 @@ public class Definitions { /** The scala.Any class */ public final Symbol ANY_CLASS; - public final Type ANY_TYPE() {return ANY_CLASS.type();} + public final Type ANY_TYPE() {return ANY_CLASS.staticType();} /** The scala.AnyVal class */ public final Symbol ANYVAL_CLASS; - public final Type ANYVAL_TYPE() {return ANYVAL_CLASS.type();} + public final Type ANYVAL_TYPE() {return ANYVAL_CLASS.staticType();} /** The scala.AnyRef class */ public final Symbol ANYREF_CLASS; - public final Type ANYREF_TYPE() {return ANYREF_CLASS.type();} + public final Type ANYREF_TYPE() {return ANYREF_CLASS.staticType();} /** The scala.AllRef class */ public final Symbol ALLREF_CLASS; - public final Type ALLREF_TYPE() {return ALLREF_CLASS.type();} + public final Type ALLREF_TYPE() {return ALLREF_CLASS.staticType();} /** The scala.All class */ public final Symbol ALL_CLASS; - public final Type ALL_TYPE() {return ALL_CLASS.type();} + public final Type ALL_TYPE() {return ALL_CLASS.staticType();} //######################################################################## // Public Fields & Methods - Java classes /** The java.lang.Object class */ public final Symbol OBJECT_CLASS; - public final Type OBJECT_TYPE() {return OBJECT_CLASS.type();} + public final Type OBJECT_TYPE() {return OBJECT_CLASS.staticType();} /** The java.lang.String class */ public final Symbol STRING_CLASS; - public final Type STRING_TYPE() {return STRING_CLASS.type();} + public final Type STRING_TYPE() {return STRING_CLASS.staticType();} /** The java.lang.Throwable class */ public final Symbol THROWABLE_CLASS; - public final Type THROWABLE_TYPE() {return THROWABLE_CLASS.type();} + public final Type THROWABLE_TYPE() {return THROWABLE_CLASS.staticType();} //######################################################################## // Public Fields & Methods - Scala value classes @@ -137,12 +137,12 @@ public class Definitions { /** The scala.ScalaObject class */ public final Symbol SCALAOBJECT_CLASS; - public final Type SCALAOBJECT_TYPE() {return SCALAOBJECT_CLASS.type();} + public final Type SCALAOBJECT_TYPE() {return SCALAOBJECT_CLASS.staticType();} /** The scala.Ref class */ public final Symbol REF_CLASS; public final Type REF_TYPE(Type element) { - return getType(REF_CLASS, element); + return REF_CLASS.staticType(element); } /** The scala.TupleX classes */ @@ -150,7 +150,7 @@ public class Definitions { public final Symbol[] TUPLE_CLASS = new Symbol[TUPLE_COUNT]; public final Type TUPLE_TYPE(Type[] args) { assert 0 < args.length && args.length < TUPLE_COUNT: args.length; - return getType(TUPLE_CLASS[args.length], args); + return TUPLE_CLASS[args.length].staticType(args); } /** The scala.FunctionX classes */ @@ -162,37 +162,37 @@ public class Definitions { throw new Type.Error("function has too many arguments; limit = " + (FUNCTION_COUNT-1)); args = Type.cloneArray(args, 1); args[args.length - 1] = result; - return getType(FUNCTION_CLASS[args.length - 1], args); + return FUNCTION_CLASS[args.length - 1].staticType(args); } /** The scala.PartialFunction class */ public final Symbol PARTIALFUNCTION_CLASS; public final Type PARTIALFUNCTION_TYPE(Type argument, Type result) { - return getType(PARTIALFUNCTION_CLASS, new Type[] { argument, result }); + return PARTIALFUNCTION_CLASS.staticType(argument, result); } /** The scala.Iterable class */ public final Symbol ITERABLE_CLASS; public final Type ITERABLE_TYPE(Type element) { - return getType(ITERABLE_CLASS, element); + return ITERABLE_CLASS.staticType(element); } /** The scala.Iterator class */ public final Symbol ITERATOR_CLASS; public final Type ITERATOR_TYPE(Type element) { - return getType(ITERATOR_CLASS, element); + return ITERATOR_CLASS.staticType(element); } /** The scala.Seq class */ public final Symbol SEQ_CLASS; public final Type SEQ_TYPE(Type element) { - return getType(SEQ_CLASS, element); + return SEQ_CLASS.staticType(element); } /** The scala.List class */ public final Symbol LIST_CLASS; public final Type LIST_TYPE(Type element) { - return getType(LIST_CLASS, element); + return LIST_CLASS.staticType(element); } /** The scala.Nil module */ @@ -201,7 +201,7 @@ public class Definitions { /** The scala.:: class */ public final Symbol CONS_CLASS; public final Type CONS_TYPE(Type element) { - return getType(CONS_CLASS, element); + return CONS_CLASS.staticType(element); } /** The scala.Array class */ @@ -521,17 +521,17 @@ public class Definitions { initClass(ALL_CLASS, new Type[]{ANY_TYPE()}); // create type symbols - UNIT_TYPE = newTypeSymbol(Names.Unit , UNIT_CLASS.type ()); - BOOLEAN_TYPE = newTypeSymbol(Names.Boolean, BOOLEAN_CLASS.type()); - BYTE_TYPE = newTypeSymbol(Names.Byte , BYTE_CLASS.type ()); - SHORT_TYPE = newTypeSymbol(Names.Short , SHORT_CLASS.type ()); - CHAR_TYPE = newTypeSymbol(Names.Char , CHAR_CLASS.type ()); - INT_TYPE = newTypeSymbol(Names.Int , INT_CLASS.type ()); - LONG_TYPE = newTypeSymbol(Names.Long , LONG_CLASS.type ()); - FLOAT_TYPE = newTypeSymbol(Names.Float , FLOAT_CLASS.type ()); - DOUBLE_TYPE = newTypeSymbol(Names.Double , DOUBLE_CLASS.type ()); + UNIT_TYPE = newTypeSymbol(Names.Unit ,UNIT_CLASS .staticType()); + BOOLEAN_TYPE = newTypeSymbol(Names.Boolean,BOOLEAN_CLASS.staticType()); + BYTE_TYPE = newTypeSymbol(Names.Byte ,BYTE_CLASS .staticType()); + SHORT_TYPE = newTypeSymbol(Names.Short ,SHORT_CLASS .staticType()); + CHAR_TYPE = newTypeSymbol(Names.Char ,CHAR_CLASS .staticType()); + INT_TYPE = newTypeSymbol(Names.Int ,INT_CLASS .staticType()); + LONG_TYPE = newTypeSymbol(Names.Long ,LONG_CLASS .staticType()); + FLOAT_TYPE = newTypeSymbol(Names.Float ,FLOAT_CLASS .staticType()); + DOUBLE_TYPE = newTypeSymbol(Names.Double ,DOUBLE_CLASS .staticType()); ARRAY_TYPE = newTypeSymbol(Names.Array , - Type.appliedType(ARRAY_CLASS.type(), new Type[]{ANYREF_TYPE()})); + ARRAY_CLASS.staticType(new Type[]{ANYREF_TYPE()})); // add members to scala.Any ANY_EQ = newTerm(ANY_CLASS, Names.eq , 0); @@ -651,11 +651,6 @@ public class Definitions { return sym; } - /** Returns the type of the class with the given fullname. */ - public Type getType(Name fullname) { - return getClass(fullname).type(); - } - //######################################################################## // Private Methods @@ -754,16 +749,5 @@ public class Definitions { throw Debug.abort(Debug.show(clasz,"."+name+" - ",vargs," -> ",alts)); } - - /** Returns the type of given class applied to given type argument. */ - private Type getType(Symbol clasz, Type arg) { - return getType(clasz, new Type[] { arg }); - } - - /** Returns the type of given class applied to given type arguments. */ - private Type getType(Symbol clasz, Type[] args) { - return Type.appliedType(clasz.type(), args); - } - //######################################################################## } diff --git a/sources/scalac/symtab/classfile/JavaTypeCreator.java b/sources/scalac/symtab/classfile/JavaTypeCreator.java index e81f828ca6..d5128efcb3 100644 --- a/sources/scalac/symtab/classfile/JavaTypeCreator.java +++ b/sources/scalac/symtab/classfile/JavaTypeCreator.java @@ -104,7 +104,7 @@ public class JavaTypeCreator implements JavaTypeFactory { } public Type classType(Symbol clasz) { - return clasz.typeConstructor(); + return clasz.staticType(); } public Type arrayType(Type elemtpe) { diff --git a/sources/scalac/transformer/ExplicitOuterClassesPhase.java b/sources/scalac/transformer/ExplicitOuterClassesPhase.java index e1e4293919..5ddda74792 100644 --- a/sources/scalac/transformer/ExplicitOuterClassesPhase.java +++ b/sources/scalac/transformer/ExplicitOuterClassesPhase.java @@ -397,6 +397,8 @@ public class ExplicitOuterClassesPhase extends Phase { case Select(Tree qualifier, _): Symbol symbol = tree.symbol(); + if (symbol.owner().isStaticOwner()) // !!! qualifier ignored + return gen.mkGlobalRef(tree.pos, symbol); switch (qualifier) { case Super(_, _): Symbol clasz = qualifier.symbol(); diff --git a/sources/scalac/transformer/TailCallPhase.java b/sources/scalac/transformer/TailCallPhase.java index a170671880..80d86166fd 100644 --- a/sources/scalac/transformer/TailCallPhase.java +++ b/sources/scalac/transformer/TailCallPhase.java @@ -162,8 +162,7 @@ public class TailCallPhase extends Phase { if (!isReferenceToThis(qual, method.owner())) return tree; return gen.Apply(tree.pos, gen.Ident(qual.pos, label), vargs); case Ident(_): - assert fun.symbol().isLabel(); - return tree; + return tree; // !!! or is this a tail call? default: throw Debug.abort("illegal case", fun); } -- cgit v1.2.3