Changeset 2740

Show
Ignore:
Timestamp:
08/25/08 07:40:29 (15 months ago)
Author:
angelee
Message:

[desugarer] implemented static type parameters captured by object
expression; added more test cases for mutable variables captured by object
expression.

Location:
trunk/ProjectFortress
Files:
1 added
5 modified

Legend:

Unmodified
Added
Removed
  • trunk/ProjectFortress/src/com/sun/fortress/compiler/desugarer/FreeNameCollection.java

    r2732 r2740  
    2121import java.util.List; 
    2222 
     23import com.sun.fortress.compiler.typechecker.TypeEnv; 
    2324import com.sun.fortress.exceptions.DesugarerError; 
     25import com.sun.fortress.nodes.BoolParam; 
    2426import com.sun.fortress.nodes.BoolRef; 
    2527import com.sun.fortress.nodes.DimRef; 
    2628import com.sun.fortress.nodes.FnRef; 
     29import com.sun.fortress.nodes.IntParam; 
    2730import com.sun.fortress.nodes.IntRef; 
     31import com.sun.fortress.nodes.NatParam; 
    2832import com.sun.fortress.nodes.OpRef; 
     33import com.sun.fortress.nodes.StaticParam; 
    2934import com.sun.fortress.nodes.Type; 
    3035import com.sun.fortress.nodes.UnitRef; 
     
    113118     * Hence, remove these redundant references from the freeVarRefs. 
    114119     */ 
    115     public void removeStaticRefsFromFreeVarRefs() { 
     120    public void removeStaticRefsFromFreeVarRefs(TypeEnv typeEnv) { 
    116121        // need to use a new list; can't just remove the redundant var from 
    117122        // the old list, otherwise we get a ConcurrentModificationException. 
     
    119124 
    120125        for(VarRef var : freeVarRefs) { 
    121             BoolRef b = new BoolRef( var.getVar() ); 
    122             IntRef i = new IntRef( var.getVar() ); 
    123             if( freeBoolRefs.contains(b) == false &&  
    124                 freeIntRefs.contains(i) == false ) { 
     126            Option<StaticParam> spOp = typeEnv.staticParam( var.getVar() ); 
     127            if( spOp.isNone() ) { // it's not a static param 
    125128                newFreeVarRefs.add(var); 
     129            } else if( spOp.unwrap() instanceof BoolParam ) { 
     130                this.add( new BoolRef(var.getSpan(), var.getVar()) );  
     131            } else if( spOp.unwrap() instanceof IntParam ) { 
     132                this.add( new IntRef(var.getSpan(), var.getVar()) );  
     133            } else if( spOp.unwrap() instanceof NatParam ) { 
     134                this.add( new IntRef(var.getSpan(), var.getVar()) );  
     135            } else { 
     136                throw new DesugarerError( "Unexpected Static Param type " + 
     137                    "found: " + spOp.unwrap() ); 
    126138            } 
    127139        } 
  • trunk/ProjectFortress/src/com/sun/fortress/compiler/desugarer/FreeNameCollector.java

    r2732 r2740  
    8282     *     We need this info so that we don't lose the extends clauses on the  
    8383     *     TypeParam when we make the StaticParam list for the lifted ObjExpr  
     84     * 
     85     * Don't need this now that we are passing all static params 
     86    private Map<Pair<Id,Id>, TypeParam> staticArgToTypeParam; 
    8487     */  
    85     private Map<Pair<Id,Id>, TypeParam> staticArgToTypeParam; 
    8688 
    8789         
     
    104106        this.declSiteToVarRefs =  
    105107            new HashMap<Pair<String,Span>, List<Pair<VarRef,Node>>>(); 
    106         this.staticArgToTypeParam = new HashMap<Pair<Id,Id>, TypeParam>(); 
     108        // this.staticArgToTypeParam = new HashMap<Pair<Id,Id>, TypeParam>(); 
    107109         
    108110                this.enclosingTraitDecl = null;  
     
    119121    } 
    120122 
     123    /*  
    121124    public Map<Pair<Id,Id>, TypeParam> getStaticArgToTypeParam() { 
    122125        return staticArgToTypeParam; 
    123     } 
     126    } */ 
    124127 
    125128        @Override 
     
    228231        scopeStack.pop(); 
    229232 
     233        TypeEnv objExprTypeEnv = typeCheckerOutput.getTypeEnv(that); 
     234 
    230235        if( objExprStack.isEmpty() &&  
    231236            (enclosingObjectDecl != null || enclosingTraitDecl != null) ) { 
    232237            // use the "self" id to get the right type of the 
    233238            // enclosing object / trait decl 
    234             Option<Type> type =  
    235                 typeCheckerOutput.getTypeEnv(that).type( new Id("self") ); 
     239            Option<Type> type = objExprTypeEnv.type( new Id("self") ); 
    236240            freeNames.setEnclosingSelfType(type); 
    237241        } 
     
    241245        // captured in two different list and is redundant.  Remove them so 
    242246        // we don't get a name collision in the lifted ObjectDecl.  
    243         freeNames.removeStaticRefsFromFreeVarRefs(); 
     247        freeNames.removeStaticRefsFromFreeVarRefs(objExprTypeEnv); 
    244248 
    245249        List<VarRef> mutableVars =  
     
    476480                    freeNames.add(that); 
    477481 
     482            /* 
     483             * Don't need to do this now that we are passing all static 
     484             * params 
    478485            ObjectExpr innerMostObjExpr = objExprStack.peek(); 
    479486                TypeEnv objExprTypeEnv =  
     
    501508            } else { 
    502509                staticArgToTypeParam.put( key, (TypeParam) spOp.unwrap() ); 
    503             } 
     510            } */ 
    504511                } 
    505512 
     
    616623                    DEBUG_LEVEL, "its span is: ", topLevelNode.getSpan()); 
    617624                 
    618                 // FIXME: change if going back to Pair<Node,Span> key 
    619         // FIXME: change when the type checking ppl are done w/ getTypeEnv 
    620625                TypeEnv topLevelEnv = typeCheckerOutput.getTypeEnv(topLevelNode); 
    621626 
  • trunk/ProjectFortress/src/com/sun/fortress/compiler/desugarer/ObjectExpressionVisitor.java

    r2732 r2740  
    100100     *     We need this info so that we don't lose the extends clauses on the  
    101101     *     TypeParam when we make the StaticParam list for the lifted ObjExpr  
     102     * 
     103     * This is no longer needed now that we are passing all static params 
     104    private Map<Pair<Id,Id>, TypeParam> staticArgToTypeParam; 
    102105     */ 
    103     private Map<Pair<Id,Id>, TypeParam> staticArgToTypeParam; 
    104106 
    105107 
     
    147149        objExprToFreeNames = freeNameCollector.getObjExprToFreeNames(); 
    148150        declSiteToVarRefs  = freeNameCollector.getDeclSiteToVarRefs(); 
    149         staticArgToTypeParam = freeNameCollector.getStaticArgToTypeParam(); 
     151        // staticArgToTypeParam = freeNameCollector.getStaticArgToTypeParam(); 
    150152 
    151153        // No object expression found in this component. We are done. 
     
    290292        if( rewriteList != null ) { 
    291293            String uniqueSuffix = ""; 
    292             if( enclosingObjectDecl != null ) { 
    293                 uniqueSuffix += enclosingObjectDecl.getName().getText(); 
     294            Id enclosingId = getEnclosingTraitObjectName(); 
     295 
     296            if( enclosingId != null ) { 
     297                uniqueSuffix += enclosingId.getText(); 
    294298            } 
    295299            uniqueSuffix += nextUniqueId();  
     
    394398        VarRef enclosingSelf = null; 
    395399 
    396         List<StaticArg> staticArgs = makeStaticArgsToLiftedObj(freeNames); 
     400        List<StaticArg> staticArgs =  
     401                makeStaticArgsToLiftedObj(objExpr, freeNames); 
    397402 
    398403        /* Now make the call to construct the lifted object */ 
     
    404409        if (freeMethodRefs != null && freeMethodRefs.size() != 0) { 
    405410            enclosingSelf = ExprFactory.makeVarRef(span, "self"); 
    406         } 
     411        }  
    407412 
    408413        List<Expr> exprs = makeArgsForCallToLiftedObj(objExpr, 
     
    416421    } 
    417422 
    418     private List<StaticArg>  
    419     makeStaticArgsToLiftedObj(FreeNameCollection freeNames) { 
     423    /*  
     424     * Turns out that we do need pass all the static params from the outer 
     425     * enclosing Trait/ObjectDecl, but not just the ones being referenced,  
     426     * because otherwise some varRef captured by object expression may have  
     427     * types that's not recognized at top level.  Argh.  The old code is  
     428     * commented out and left at the end of the file. 
     429     */  
     430    private List<StaticArg> makeStaticArgsToLiftedObj(ObjectExpr target,  
     431                                            FreeNameCollection freeNames) { 
    420432        List<BoolRef> boolRefs = freeNames.getFreeBoolRefs(); 
    421433        List<IntRef> intRefs = freeNames.getFreeIntRefs(); 
    422434        List<VarType> varTypes = freeNames.getFreeVarTypes(); 
     435        List<StaticParam> sParams = null; 
    423436        List<StaticArg> args = new LinkedList<StaticArg>(); 
    424  
    425         for(BoolRef boolRef : boolRefs) { 
    426             args.add( new BoolArg(boolRef.getSpan(), boolRef) );  
    427         } 
    428  
    429         for(IntRef intRef : intRefs) { 
    430             args.add( new IntArg(intRef.getSpan(), intRef) ); 
    431         } 
    432  
    433         for(VarType varType: varTypes) { 
    434             args.add( new TypeArg(varType.getSpan(), varType) ); 
     437             
     438        if( enclosingTraitDecl != null ) { 
     439            sParams = enclosingTraitDecl.getStaticParams(); 
     440        } else if( enclosingObjectDecl != null ) { 
     441            sParams = enclosingObjectDecl.getStaticParams(); 
     442        } else { 
     443            if( boolRefs.isEmpty() == false ||  // sanity check 
     444                intRefs.isEmpty() == false || varTypes.isEmpty() == false ) {    
     445                throw new DesugarerError( target.getSpan(),  
     446                        "Found refences to static params outside " +  
     447                        "of Trait/ObjectDecl!"); 
     448            } 
     449        } 
     450 
     451        if( sParams != null) { 
     452            for(StaticParam sp : sParams) { 
     453                args.add( makeStaticArgFromStaticParam(sp) );  
     454            } 
    435455        } 
    436456 
     
    447467        List<Expr> exprs = new LinkedList<Expr>(); 
    448468 
    449         // FIXME: Need to handle mutated vars 
    450469        if(freeVarRefs != null) { 
    451470            for(VarRef var : freeVarRefs) { 
     
    504523 
    505524        List<StaticParam> staticParams = 
    506                 makeStaticParamsForLiftedObj(freeNames); 
     525                makeStaticParamsForLiftedObj(target, freeNames); 
    507526 
    508527        if( freeMethodRefs.isEmpty() == false ) { 
     
    531550    } 
    532551 
    533     private List<StaticParam> 
    534     makeStaticParamsForLiftedObj(FreeNameCollection freeNames) { 
     552    /*  
     553     * Turns out that we do need pass all the static params from the outer 
     554     * enclosing Trait/ObjectDecl, but not just the ones being referenced,  
     555     * because otherwise some varRef captured by object expression may have  
     556     * types that's not recognized at top level.  Argh.  The old code is  
     557     * commented out and left at the end of the file. 
     558     */  
     559    private List<StaticParam> makeStaticParamsForLiftedObj(ObjectExpr target,  
     560                                                FreeNameCollection freeNames) { 
    535561        List<BoolRef> boolRefs = freeNames.getFreeBoolRefs(); 
    536562        List<IntRef> intRefs = freeNames.getFreeIntRefs(); 
    537563        List<VarType> varTypes = freeNames.getFreeVarTypes(); 
    538         List<StaticParam> sParams = new LinkedList<StaticParam>(); 
    539  
    540         for(BoolRef boolRef : boolRefs) { 
    541             sParams.add( new BoolParam(boolRef.getSpan(), boolRef.getName()) ); 
    542         } 
    543         for(IntRef intRef : intRefs) { 
    544             sParams.add( new IntParam(intRef.getSpan(), intRef.getName()) ); 
    545         } 
    546         for(VarType varType : varTypes) { 
    547             Id enclosingId = null; 
    548             if(enclosingTraitDecl != null){ 
    549                 enclosingId = enclosingTraitDecl.getName(); 
    550             } else if(enclosingObjectDecl != null) { 
    551                 enclosingId = enclosingObjectDecl.getName(); 
    552             } else { 
    553                 throw new DesugarerError( varType.getSpan(), "VarType " + 
    554                         varType + " found outside of Trait/ObjectDecl!" ); 
    555             } 
    556  
    557             Pair<Id,Id> key = new Pair<Id,Id>( enclosingId, varType.getName() ); 
    558             TypeParam declSite = staticArgToTypeParam.get(key); 
    559          
    560             if(declSite == null) { 
    561                throw new DesugarerError( varType.getSpan(), "Cannot find the " 
    562                             + "decl site of VarType " + varType + " in " +  
    563                             "staticArgToTypeParam map!"); 
    564             } 
    565  
    566             sParams.add( new TypeParam(varType.getSpan(), varType.getName(),  
    567                                        declSite.getExtendsClause(),  
    568                                        declSite.isAbsorbs()) ); 
    569         } 
    570          
    571         return sParams; 
     564        List<StaticParam> sParamsCopy = new LinkedList<StaticParam>(); 
     565        List<StaticParam> sParams = null; 
     566 
     567        if( enclosingTraitDecl != null ) { 
     568            sParams = enclosingTraitDecl.getStaticParams(); 
     569        } else if( enclosingObjectDecl != null ) { 
     570            sParams = enclosingObjectDecl.getStaticParams(); 
     571        } else { 
     572            if( boolRefs.isEmpty() == false ||  // sanity check 
     573                intRefs.isEmpty() == false || varTypes.isEmpty() == false ) {    
     574                throw new DesugarerError( target.getSpan(),  
     575                        "Found refences to static params outside " +  
     576                        "of Trait/ObjectDecl!"); 
     577            } 
     578        } 
     579 
     580        if(sParams != null) { 
     581            sParamsCopy.addAll(sParams); 
     582        } 
     583 
     584        return sParamsCopy; 
    572585    } 
    573586 
     
    576589                           FreeNameCollection freeNames, 
    577590                           NormalParam enclosingSelfParam) { 
    578         // TODO: need to figure out shadowed self via FnRef 
    579         // need to box any var that's mutabl 
    580591 
    581592        Option<Type> type = null; 
     
    585596        List<FnRef> freeFnRefs = freeNames.getFreeFnRefs(); 
    586597 
    587         // FIXME: Need to handle mutated vars 
    588598        if(freeVarRefs != null) { 
    589599            for(VarRef var : freeVarRefs) { 
     
    634644 
    635645        // Just sanity check 
    636         if(enclosingTraitDecl == null && enclosingObjectDecl == null) { 
     646        if( getEnclosingTraitObjectName() == null ) { 
    637647            throw new DesugarerError("No enclosing trait or object " + 
    638648                        "decl found when a dotted method is referenced."); 
     
    668678 
    669679    // small helper methods 
     680    private StaticArg makeStaticArgFromStaticParam(StaticParam sParam) { 
     681        Span span = sParam.getSpan(); 
     682 
     683        if( sParam instanceof BoolParam ) { 
     684            BoolRef boolRef = new BoolRef(span, ((BoolParam) sParam).getName()); 
     685            return new BoolArg(span, boolRef); 
     686        } else if( sParam instanceof IntParam ) { 
     687            IntRef intRef = new IntRef(span, ((IntParam) sParam).getName()); 
     688            return new IntArg(span, intRef); 
     689        } else if( sParam instanceof NatParam ) { 
     690            IntRef intRef = new IntRef(span, ((NatParam) sParam).getName()); 
     691            return new IntArg(span, intRef); 
     692        } else if( sParam instanceof TypeParam ) { 
     693            VarType varType = new VarType(span, ((TypeParam) sParam).getName()); 
     694            return new TypeArg(span, varType); 
     695        } else { 
     696            throw new DesugarerError( 
     697                "Unexpected type of Static Param found: " + sParam); 
     698        } 
     699    } 
     700  
     701    private Id getEnclosingTraitObjectName() { 
     702        Id enclosingId = null; 
     703        if(enclosingTraitDecl != null){ 
     704            enclosingId = enclosingTraitDecl.getName(); 
     705        } else if(enclosingObjectDecl != null) { 
     706            enclosingId = enclosingObjectDecl.getName(); 
     707        }  
     708 
     709        return enclosingId; 
     710    } 
     711 
    670712    private VarRef makeVarRefFromNormalParam(NormalParam param) { 
    671713        VarRef varRef = ExprFactory.makeVarRef( param.getSpan(), 
     
    685727    } 
    686728 
     729 
     730/************ 
     731 * Some code that I like to keep around - may be useful one day. 
     732 ************/ 
     733 
     734    /* 
     735     * The old code that make static args to pass to the lifted object expr 
     736     * based on what BoolRefs and IntRefs are captured 
     737     *  
     738    private List<StaticArg>  
     739    makeStaticArgsToLiftedObj(FreeNameCollection freeNames) { 
     740        List<BoolRef> boolRefs = freeNames.getFreeBoolRefs(); 
     741        List<IntRef> intRefs = freeNames.getFreeIntRefs(); 
     742        List<VarType> varTypes = freeNames.getFreeVarTypes(); 
     743        List<StaticArg> args = new LinkedList<StaticArg>(); 
     744 
     745        for(BoolRef boolRef : boolRefs) { 
     746            args.add( new BoolArg(boolRef.getSpan(), boolRef) );  
     747        } 
     748 
     749        for(IntRef intRef : intRefs) { 
     750            args.add( new IntArg(intRef.getSpan(), intRef) ); 
     751        } 
     752 
     753        for(VarType varType: varTypes) { 
     754            args.add( new TypeArg(varType.getSpan(), varType) ); 
     755        } 
     756 
     757        return args; 
     758    } */ 
     759 
     760    /* 
     761     * The old code that make static type params for the lifted object expr 
     762     * based on what BoolRefs and IntRefs are captured 
     763     *  
     764    private List<StaticParam> 
     765    makeStaticParamsForLiftedObj(FreeNameCollection freeNames) { 
     766        List<BoolRef> boolRefs = freeNames.getFreeBoolRefs(); 
     767        List<IntRef> intRefs = freeNames.getFreeIntRefs(); 
     768        List<VarType> varTypes = freeNames.getFreeVarTypes(); 
     769        List<StaticParam> sParams = new LinkedList<StaticParam>(); 
     770 
     771        for(BoolRef boolRef : boolRefs) { 
     772            sParams.add( new BoolParam(boolRef.getSpan(), boolRef.getName()) ); 
     773        } 
     774        for(IntRef intRef : intRefs) { 
     775            sParams.add( new IntParam(intRef.getSpan(), intRef.getName()) ); 
     776        } 
     777        for(VarType varType : varTypes) { 
     778            Id enclosingId = getEnclosingTraitDeclName(); 
     779   
     780            if(enclosingId == null) { 
     781                throw new DesugarerError( varType.getSpan(), "VarType " + 
     782                        varType + " found outside of Trait/ObjectDecl!" ); 
     783            } 
     784 
     785            Pair<Id,Id> key = new Pair<Id,Id>( enclosingId, varType.getName() ); 
     786            TypeParam declSite = staticArgToTypeParam.get(key); 
     787         
     788            if(declSite == null) { 
     789               throw new DesugarerError( varType.getSpan(), "Cannot find the " 
     790                            + "decl site of VarType " + varType + " in " +  
     791                            "staticArgToTypeParam map!"); 
     792            } 
     793 
     794            sParams.add( new TypeParam(varType.getSpan(), varType.getName(),  
     795                                       declSite.getExtendsClause(),  
     796                                       declSite.isAbsorbs()) ); 
     797        } 
     798         
     799        return sParams; 
     800    } */ 
    687801 
    688802//     private ObjectDecl  
  • trunk/ProjectFortress/src/com/sun/fortress/compiler/desugarer/ObjectExpressionVisitorJUTest.java

    r2732 r2740  
    5050        runFile("objectCC_mutVar1.fss"); 
    5151        runFile("objectCC_mutVar2.fss"); 
     52        runFile("objectCC_mutable.fss"); 
    5253    } 
    5354 
  • trunk/ProjectFortress/tests/objectCC_staticParams.fss

    r2736 r2740  
    1 (******************************************************************************* 
    2     Copyright 2008 Sun Microsystems, Inc., 
    3     4150 Network Circle, Santa Clara, California 95054, U.S.A. 
    4     All rights reserved. 
    5  
    6     U.S. Government Rights - Commercial software. 
    7     Government users are subject to the Sun Microsystems, Inc. standard 
    8     license agreement and applicable provisions of the FAR and its supplements. 
    9  
    10     Use is subject to license terms. 
    11  
    12     This distribution may include materials developed by third parties. 
    13  
    14     Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered 
    15     trademarks of Sun Microsystems, Inc. in the U.S. and other countries. 
    16  ******************************************************************************) 
    17  
    181component objectCC_staticParams 
    192export Executable 
     
    269end 
    2710 
    28 object O[\X extends String, bool b, nat n\](v:ZZ32, s:X) 
     11trait Y  
     12end 
     13 
     14object YObj extends Y 
     15end 
     16 
     17object O[\X extends String, bool b, nat n\](var v:ZZ32, s:X) 
    2918    foo():ZZ32 = 3 
    3019    (* After we move checking of method calls to be after 
     
    3322                x():ZZ32 = foo() 
    3423                y():ZZ32 = quack() 
    35                 z():ZZ32 = do 
    36                     _ = O[\X, b, n\](v, s) 
     24                z():ZZ32 = do  
     25                    _ = O[\X, b, 3\](v, s) 
    3726                    if b then n else (n-1) end 
    3827                end 
     
    4332end 
    4433 
     34(* Referring to var with static type param, when the static type is not 
     35   explitictly referred in object expression body; if the closure conversion 
     36   works correctly, the unparse output should type check *) 
     37object O2[\X extends String, Z, ZObj extends Z\](var v:ZZ32, s:X, s2:ZObj) 
     38    foo():ZZ32 = 3 
     39    bar():T[\X\] = object extends T[\X\] 
     40                x():ZZ32 = foo() 
     41                y():ZZ32 = quack() 
     42                z() = s2 
     43                a():X = s 
     44            end 
     45    quack():ZZ32 = self.v 
     46    name():X = s 
     47end 
     48 
    4549run(args:String...):() = do 
    4650    o:O[\String, true, 7\] = O[\String, true, 7\](4, "Hello"); 
    47     println o.v 
    48     println o.bar().x() 
    49     println o.bar().z() 
    50     println o.bar().a() 
     51    o.v := o.v + 1 
     52    t:T[\String\] = o.bar() 
     53    assert( o.foo(), t.x(), "t.x() failed." ) 
     54    assert( o.quack(), t.y(), "t.y() failed." ) 
     55    assert( 7, t.z(), "t.z() failed." ) 
     56    assert( o.s, t.a(), "t.a() failed." ) 
    5157end 
    5258 
    5359end 
     60