Changeset 2740
- Timestamp:
- 08/25/08 07:40:29 (15 months ago)
- Location:
- trunk/ProjectFortress
- Files:
-
- 1 added
- 5 modified
-
src/com/sun/fortress/compiler/desugarer/FreeNameCollection.java (modified) (3 diffs)
-
src/com/sun/fortress/compiler/desugarer/FreeNameCollector.java (modified) (8 diffs)
-
src/com/sun/fortress/compiler/desugarer/ObjectExpressionVisitor.java (modified) (14 diffs)
-
src/com/sun/fortress/compiler/desugarer/ObjectExpressionVisitorJUTest.java (modified) (1 diff)
-
tests/objectCC_mutable.fss (added)
-
tests/objectCC_staticParams.fss (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/ProjectFortress/src/com/sun/fortress/compiler/desugarer/FreeNameCollection.java
r2732 r2740 21 21 import java.util.List; 22 22 23 import com.sun.fortress.compiler.typechecker.TypeEnv; 23 24 import com.sun.fortress.exceptions.DesugarerError; 25 import com.sun.fortress.nodes.BoolParam; 24 26 import com.sun.fortress.nodes.BoolRef; 25 27 import com.sun.fortress.nodes.DimRef; 26 28 import com.sun.fortress.nodes.FnRef; 29 import com.sun.fortress.nodes.IntParam; 27 30 import com.sun.fortress.nodes.IntRef; 31 import com.sun.fortress.nodes.NatParam; 28 32 import com.sun.fortress.nodes.OpRef; 33 import com.sun.fortress.nodes.StaticParam; 29 34 import com.sun.fortress.nodes.Type; 30 35 import com.sun.fortress.nodes.UnitRef; … … 113 118 * Hence, remove these redundant references from the freeVarRefs. 114 119 */ 115 public void removeStaticRefsFromFreeVarRefs( ) {120 public void removeStaticRefsFromFreeVarRefs(TypeEnv typeEnv) { 116 121 // need to use a new list; can't just remove the redundant var from 117 122 // the old list, otherwise we get a ConcurrentModificationException. … … 119 124 120 125 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 125 128 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() ); 126 138 } 127 139 } -
trunk/ProjectFortress/src/com/sun/fortress/compiler/desugarer/FreeNameCollector.java
r2732 r2740 82 82 * We need this info so that we don't lose the extends clauses on the 83 83 * 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; 84 87 */ 85 private Map<Pair<Id,Id>, TypeParam> staticArgToTypeParam;86 88 87 89 … … 104 106 this.declSiteToVarRefs = 105 107 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>(); 107 109 108 110 this.enclosingTraitDecl = null; … … 119 121 } 120 122 123 /* 121 124 public Map<Pair<Id,Id>, TypeParam> getStaticArgToTypeParam() { 122 125 return staticArgToTypeParam; 123 } 126 } */ 124 127 125 128 @Override … … 228 231 scopeStack.pop(); 229 232 233 TypeEnv objExprTypeEnv = typeCheckerOutput.getTypeEnv(that); 234 230 235 if( objExprStack.isEmpty() && 231 236 (enclosingObjectDecl != null || enclosingTraitDecl != null) ) { 232 237 // use the "self" id to get the right type of the 233 238 // 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") ); 236 240 freeNames.setEnclosingSelfType(type); 237 241 } … … 241 245 // captured in two different list and is redundant. Remove them so 242 246 // we don't get a name collision in the lifted ObjectDecl. 243 freeNames.removeStaticRefsFromFreeVarRefs( );247 freeNames.removeStaticRefsFromFreeVarRefs(objExprTypeEnv); 244 248 245 249 List<VarRef> mutableVars = … … 476 480 freeNames.add(that); 477 481 482 /* 483 * Don't need to do this now that we are passing all static 484 * params 478 485 ObjectExpr innerMostObjExpr = objExprStack.peek(); 479 486 TypeEnv objExprTypeEnv = … … 501 508 } else { 502 509 staticArgToTypeParam.put( key, (TypeParam) spOp.unwrap() ); 503 } 510 } */ 504 511 } 505 512 … … 616 623 DEBUG_LEVEL, "its span is: ", topLevelNode.getSpan()); 617 624 618 // FIXME: change if going back to Pair<Node,Span> key619 // FIXME: change when the type checking ppl are done w/ getTypeEnv620 625 TypeEnv topLevelEnv = typeCheckerOutput.getTypeEnv(topLevelNode); 621 626 -
trunk/ProjectFortress/src/com/sun/fortress/compiler/desugarer/ObjectExpressionVisitor.java
r2732 r2740 100 100 * We need this info so that we don't lose the extends clauses on the 101 101 * 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; 102 105 */ 103 private Map<Pair<Id,Id>, TypeParam> staticArgToTypeParam;104 106 105 107 … … 147 149 objExprToFreeNames = freeNameCollector.getObjExprToFreeNames(); 148 150 declSiteToVarRefs = freeNameCollector.getDeclSiteToVarRefs(); 149 staticArgToTypeParam = freeNameCollector.getStaticArgToTypeParam();151 // staticArgToTypeParam = freeNameCollector.getStaticArgToTypeParam(); 150 152 151 153 // No object expression found in this component. We are done. … … 290 292 if( rewriteList != null ) { 291 293 String uniqueSuffix = ""; 292 if( enclosingObjectDecl != null ) { 293 uniqueSuffix += enclosingObjectDecl.getName().getText(); 294 Id enclosingId = getEnclosingTraitObjectName(); 295 296 if( enclosingId != null ) { 297 uniqueSuffix += enclosingId.getText(); 294 298 } 295 299 uniqueSuffix += nextUniqueId(); … … 394 398 VarRef enclosingSelf = null; 395 399 396 List<StaticArg> staticArgs = makeStaticArgsToLiftedObj(freeNames); 400 List<StaticArg> staticArgs = 401 makeStaticArgsToLiftedObj(objExpr, freeNames); 397 402 398 403 /* Now make the call to construct the lifted object */ … … 404 409 if (freeMethodRefs != null && freeMethodRefs.size() != 0) { 405 410 enclosingSelf = ExprFactory.makeVarRef(span, "self"); 406 } 411 } 407 412 408 413 List<Expr> exprs = makeArgsForCallToLiftedObj(objExpr, … … 416 421 } 417 422 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) { 420 432 List<BoolRef> boolRefs = freeNames.getFreeBoolRefs(); 421 433 List<IntRef> intRefs = freeNames.getFreeIntRefs(); 422 434 List<VarType> varTypes = freeNames.getFreeVarTypes(); 435 List<StaticParam> sParams = null; 423 436 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 } 435 455 } 436 456 … … 447 467 List<Expr> exprs = new LinkedList<Expr>(); 448 468 449 // FIXME: Need to handle mutated vars450 469 if(freeVarRefs != null) { 451 470 for(VarRef var : freeVarRefs) { … … 504 523 505 524 List<StaticParam> staticParams = 506 makeStaticParamsForLiftedObj( freeNames);525 makeStaticParamsForLiftedObj(target, freeNames); 507 526 508 527 if( freeMethodRefs.isEmpty() == false ) { … … 531 550 } 532 551 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) { 535 561 List<BoolRef> boolRefs = freeNames.getFreeBoolRefs(); 536 562 List<IntRef> intRefs = freeNames.getFreeIntRefs(); 537 563 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; 572 585 } 573 586 … … 576 589 FreeNameCollection freeNames, 577 590 NormalParam enclosingSelfParam) { 578 // TODO: need to figure out shadowed self via FnRef579 // need to box any var that's mutabl580 591 581 592 Option<Type> type = null; … … 585 596 List<FnRef> freeFnRefs = freeNames.getFreeFnRefs(); 586 597 587 // FIXME: Need to handle mutated vars588 598 if(freeVarRefs != null) { 589 599 for(VarRef var : freeVarRefs) { … … 634 644 635 645 // Just sanity check 636 if( enclosingTraitDecl == null && enclosingObjectDecl == null) {646 if( getEnclosingTraitObjectName() == null ) { 637 647 throw new DesugarerError("No enclosing trait or object " + 638 648 "decl found when a dotted method is referenced."); … … 668 678 669 679 // 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 670 712 private VarRef makeVarRefFromNormalParam(NormalParam param) { 671 713 VarRef varRef = ExprFactory.makeVarRef( param.getSpan(), … … 685 727 } 686 728 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 } */ 687 801 688 802 // private ObjectDecl -
trunk/ProjectFortress/src/com/sun/fortress/compiler/desugarer/ObjectExpressionVisitorJUTest.java
r2732 r2740 50 50 runFile("objectCC_mutVar1.fss"); 51 51 runFile("objectCC_mutVar2.fss"); 52 runFile("objectCC_mutable.fss"); 52 53 } 53 54 -
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. standard8 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 registered15 trademarks of Sun Microsystems, Inc. in the U.S. and other countries.16 ******************************************************************************)17 18 1 component objectCC_staticParams 19 2 export Executable … … 26 9 end 27 10 28 object O[\X extends String, bool b, nat n\](v:ZZ32, s:X) 11 trait Y 12 end 13 14 object YObj extends Y 15 end 16 17 object O[\X extends String, bool b, nat n\](var v:ZZ32, s:X) 29 18 foo():ZZ32 = 3 30 19 (* After we move checking of method calls to be after … … 33 22 x():ZZ32 = foo() 34 23 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) 37 26 if b then n else (n-1) end 38 27 end … … 43 32 end 44 33 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 *) 37 object 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 47 end 48 45 49 run(args:String...):() = do 46 50 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." ) 51 57 end 52 58 53 59 end 60

