diff options
author | buraq <buraq@epfl.ch> | 2003-10-30 13:03:16 +0000 |
---|---|---|
committer | buraq <buraq@epfl.ch> | 2003-10-30 13:03:16 +0000 |
commit | 4430371143989edfefef7ccac71dcdf2759074d0 (patch) | |
tree | c57a10248594c06c7e0f55b80040f0f4cf8bd7af /sources | |
parent | 7bfe816d3d8112828ba2ef80625e665255532621 (diff) | |
download | scala-4430371143989edfefef7ccac71dcdf2759074d0.tar.gz scala-4430371143989edfefef7ccac71dcdf2759074d0.tar.bz2 scala-4430371143989edfefef7ccac71dcdf2759074d0.zip |
desugarizing of x:Typed in patterns
Diffstat (limited to 'sources')
-rw-r--r-- | sources/scalac/typechecker/Analyzer.java | 59 | ||||
-rw-r--r-- | sources/scalac/typechecker/DeSugarize.java | 30 |
2 files changed, 62 insertions, 27 deletions
diff --git a/sources/scalac/typechecker/Analyzer.java b/sources/scalac/typechecker/Analyzer.java index 2d569a8425..5d96732ad0 100644 --- a/sources/scalac/typechecker/Analyzer.java +++ b/sources/scalac/typechecker/Analyzer.java @@ -2000,27 +2000,30 @@ public class Analyzer extends Transformer implements Modifiers, Kinds { .setType( pt ); case Bind( Name name, Tree body ): - Symbol vble = new TermSymbol(tree.pos, - name, - context.owner, - 0x00000000 ).setType( pt ); - vble = enterInScope( vble ); - //System.out.println("Bind("+name+",...) enters in scope:"+vble.fullNameString()); - - patternVars.put( vble, new Boolean( this.inAlternative )); - //System.out.println("case Bind.. put symbol vble="+vble+" in scope and patternVars."); - - body = transform( body ); - //assert body.type != null; - if( TreeInfo.isSequenceValued( body ) ) { - vble.setType( definitions.LIST_TYPE(pt) ); - } else { - vble.setType( body.type ); + Symbol vble = null; + if( name != Names.PATTERN_WILDCARD ) { + vble = new TermSymbol(tree.pos, + name, + context.owner, + 0x00000000 ).setType( pt ); + vble = enterInScope( vble ); + //System.out.println("Bind("+name+",...) enters in scope:"+vble.fullNameString()); + + patternVars.put( vble, new Boolean( this.inAlternative )); + //System.out.println("case Bind.. put symbol vble="+vble+" in scope and patternVars."); } - - return copy.Bind( tree, name, body ) - .setSymbol( vble ).setType( body.type ); - + body = transform( body ); + if( name != Names.PATTERN_WILDCARD ) { + //assert body.type != null; + if( TreeInfo.isSequenceValued( body ) ) { + vble.setType( definitions.listType(pt) ); + } else { + vble.setType( body.type ); + } + return copy.Bind( tree, name, body ) + .setSymbol( vble ).setType( body.type ); + } + return body; case Visitor(Tree.CaseDef[] cases): if (pt.symbol().isSubClass(definitions.PARTIALFUNCTION_CLASS)) { Type pft = pt.baseType(definitions.PARTIALFUNCTION_CLASS); @@ -2166,6 +2169,15 @@ public class Analyzer extends Transformer implements Modifiers, Kinds { } case Typed(Tree expr, Tree tpe): + switch( expr ) { + case Ident(Name n): + if ((n!=Names.PATTERN_WILDCARD) + &&((mode & PATTERNmode) != 0)) { + return transform( desugarize.TypedPattern( (Typed) tree ), + mode, + pt ); + } + } Tree expr1; Tree tpe1; switch (tpe) { @@ -2178,8 +2190,8 @@ public class Analyzer extends Transformer implements Modifiers, Kinds { : Type.ErrorType; tpe1 = tpe.setType(elemtp); break; - default: - tpe1 = transform(tpe, TYPEmode); + default: + tpe1 = transform(tpe, TYPEmode); expr1 = transform(expr, mode & baseModes, tpe1.type); } return copy.Typed(tree, expr1, tpe1).setType(tpe1.type); @@ -2470,7 +2482,8 @@ public class Analyzer extends Transformer implements Modifiers, Kinds { context.owner, 0).setType( pt ); - if((( mode & SEQUENCEmode) != 0)&&( name != Names.PATTERN_WILDCARD )) { + //if((( mode & SEQUENCEmode) != 0)&&( name != Names.PATTERN_WILDCARD )) { + if( name != Names.PATTERN_WILDCARD ) { // x => x @ _ in sequence patterns tree = desugarize.IdentPattern( tree ); } diff --git a/sources/scalac/typechecker/DeSugarize.java b/sources/scalac/typechecker/DeSugarize.java index 4b9b5864af..9f0a9a98c7 100644 --- a/sources/scalac/typechecker/DeSugarize.java +++ b/sources/scalac/typechecker/DeSugarize.java @@ -535,8 +535,7 @@ public class DeSugarize implements Kinds, Modifiers { } } - /** x => x @ _ - * only within sequence patterns, for now. BE + /** in patterns x => x @ _ * precondition: name != '_' * post: returns *unattributed* Bind tree */ @@ -544,15 +543,38 @@ public class DeSugarize implements Kinds, Modifiers { public Tree IdentPattern( Tree tree ) { switch( tree ) { case Ident( Name name ): - if( name == Names.PATTERN_WILDCARD ) throw new ApplicationError("nothing to desugarize"); + if( name == Names.PATTERN_WILDCARD ) + throw new ApplicationError("nothing to desugarize"); return make.Bind( tree.pos, name, - gen.Ident( tree.pos, global.definitions.PATTERN_WILDCARD )).setType( tree.type ); + gen.Ident( tree.pos, + global.definitions.PATTERN_WILDCARD )) + .setType( tree.type ); default: throw new ApplicationError("ident expected"); } } + /** in patterns x:T => x @ _ : T + * pre: t is a typed variable. + * post: returns *unattributed* Bind tree + */ + + public Tree TypedPattern( Tree.Typed t ) { + switch( t ) { + case Typed(Ident(Name name), Tree tpe): + return make.Bind(t.pos, + name, + make.Typed(t.pos, + gen.Ident( t.pos, + global.definitions.PATTERN_WILDCARD ), + tpe)); + default: + throw new ApplicationError("unexpected Typed node"); + } + } + + /** f, (syms_1)...(syms_n)T ==> f(ps_1)...(ps_n) */ Tree toApply(Tree tree, Type type) { |