Changeset 3921 for trunk/ProjectFortress

Show
Ignore:
Timestamp:
07/06/09 08:26:38 (5 months ago)
Author:
jrhil47
Message:

[type environments] Still rewriting type environments. Fixed the constraint-related bug that David emailed about.

Location:
trunk/ProjectFortress/src/com/sun/fortress
Files:
8 modified

Legend:

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

    r3894 r3921  
    234234            } else { 
    235235                TypeEnv typeEnv = typeCheckEnv(component, env); 
    236                 ConstraintUtil.useScalaFormulas(); 
    237236                STypeChecker typeChecker = STypeCheckerFactory.make(component, traitTable, typeEnv, typeAnalyzer); 
    238237                component_ast = typeChecker.typeCheck(component_ast); 
     
    276275                                               boolean postInference) { 
    277276        TypeEnv typeEnv = typeCheckEnv(component, env); 
    278         ConstraintUtil.useJavaFormulas(); 
    279277        TypeChecker typeChecker = new TypeChecker(traitTable, typeEnv, 
    280278                                                  component, postInference); 
  • trunk/ProjectFortress/src/com/sun/fortress/compiler/index/Functional.java

    r3915 r3921  
    7373    /** 
    7474     * Evaluate the thunk to get a return type. Then replace the thunk with one 
    75      * that simply gets back the previously evaluated return type. 
     75     * that simply gets back the previously evaluated return type. After type 
     76     * checking, the result of this method will always be Some(type). 
    7677     */ 
    7778    public Option<Type> getReturnType() { 
  • trunk/ProjectFortress/src/com/sun/fortress/compiler/typechecker/constraints/ConstraintUtil.java

    r3813 r3921  
    1818package com.sun.fortress.compiler.typechecker.constraints; 
    1919 
     20import com.sun.fortress.Shell; 
    2021import com.sun.fortress.compiler.typechecker.constraints.JavaConstraintUtil; 
    2122import com.sun.fortress.scala_src.typechecker.ScalaConstraintUtil; 
     
    2930public class ConstraintUtil { 
    3031 
    31         private static boolean java = true; 
    32  
    33         public static void useJavaFormulas(){ 
    34                 java = true; 
    35         } 
    36  
    37         public static void useScalaFormulas(){ 
    38                 java = false; 
    39         } 
    40  
    41         public static void setJava(Boolean _java){ 
    42                 java = _java; 
    43         } 
    44  
    4532        public static ConstraintFormula trueFormula() { 
    46                 if(java) 
     33                if(!Shell.getScala()) 
    4734                        return JavaConstraintUtil.trueFormula(); 
    4835                else 
     
    5037        } 
    5138        public static ConstraintFormula falseFormula() { 
    52                 if(java) 
     39                if(!Shell.getScala()) 
    5340                        return JavaConstraintUtil.falseFormula(); 
    5441                else 
     
    5744 
    5845        public static ConstraintFormula upperBound(_InferenceVarType ivar, Type type, SubtypeHistory h) { 
    59                 if(java) 
     46                if(!Shell.getScala()) 
    6047                        return JavaConstraintUtil.upperBound(ivar,type,h); 
    6148                else 
     
    6451 
    6552        public static ConstraintFormula lowerBound(_InferenceVarType ivar, Type type, SubtypeHistory h) { 
    66                 if(java) 
     53                if(!Shell.getScala()) 
    6754                        return JavaConstraintUtil.lowerBound(ivar,type,h); 
    6855                else 
     
    8067 
    8168        public static ConstraintFormula fromBoolean(Boolean bool){ 
    82                 if(java) 
     69                if(!Shell.getScala()) 
    8370                        return JavaConstraintUtil.fromBoolean(bool); 
    8471                else 
  • trunk/ProjectFortress/src/com/sun/fortress/scala_src/typechecker/STypeChecker.scala

    r3902 r3921  
    226226   * Return the conditions for subtype <: supertype to hold. 
    227227   */ 
    228   protected def checkSubtype(subtype: Type, supertype: Type): ScalaConstraint = 
    229     analyzer.subtype(subtype, supertype).asInstanceOf[ScalaConstraint] 
     228  protected def checkSubtype(subtype: Type, supertype: Type): ScalaConstraint = { 
     229    val constraint = analyzer.subtype(subtype, supertype)  
     230    if (!constraint.isInstanceOf[ScalaConstraint]) { 
     231      bug("Not a ScalaConstraint.") 
     232    } 
     233    constraint.asInstanceOf[ScalaConstraint] 
     234  } 
    230235 
    231236  protected def equivalentTypes(t1: Type, t2: Type): Boolean = 
  • trunk/ProjectFortress/src/com/sun/fortress/scala_src/typechecker/staticenv/KindEnv.scala

    r3915 r3921  
    3535  type EnvBinding = KindBinding 
    3636 
    37   /** Extend me with the immediate bindings of the given node. */ 
    38   def extendWith(node: Any): KindEnv = 
    39     new NestedKindEnv(this, KindEnv.extractEnvBindings(node)) 
    40  
    4137  /** 
    4238   * Get the type of the given variable name, if it is bound. The resulting 
     
    6359 */ 
    6460class NestedKindEnv protected (protected val parent: KindEnv, 
    65                                _bindings: Collection[KindBinding]) 
     61                               _bindings: Iterable[KindBinding]) 
    6662    extends KindEnv with NestedStaticEnv[StaticParam] { 
    6763     
     
    8076  type EnvBinding = KindBinding 
    8177   
    82   /** New kind environment with empty parent and the node's bindings. */ 
    83   def make(node: Any): KindEnv = 
    84     new NestedKindEnv(EmptyKindEnv, extractEnvBindings(node)) 
     78  def empty():KindEnv = EmptyKindEnv 
    8579   
    8680  /** Extract out the bindings in node. */ 
    87   protected def extractEnvBindings(node: Any) : Collection[KindBinding] = null 
     81  protected def extractEnvBindings(node: Node) : Iterable[KindBinding] = Nil 
     82  protected def extractEnvBindings(node: Any) : Iterable[KindBinding] = Nil 
    8883} 
    8984 
  • trunk/ProjectFortress/src/com/sun/fortress/scala_src/typechecker/staticenv/STypeEnv.scala

    r3915 r3921  
    3636   
    3737  /** Extend me with the immediate bindings of the given node. */ 
    38   def extendWith(node: Any): STypeEnv = 
    39     new NestedSTypeEnv(this, STypeEnv.extractEnvBindings(node)) 
    4038   
    4139  /** Same as `lookup`. */ 
     
    5351 */ 
    5452class NestedSTypeEnv protected (protected val parent: STypeEnv, 
    55                                 _bindings: Collection[TypeBinding]) 
     53                                _bindings: Iterable[TypeBinding]) 
    5654    extends STypeEnv with NestedStaticEnv[Type] { 
    5755     
     
    6260 
    6361/** Companion module for STypeEnv. */ 
    64 object STypeEnv extends StaticEnvCompanion[Type] with STypeEnvExtraction { 
     62object STypeEnv extends StaticEnvCompanion[Type] 
     63    with STypeEnvExtraction { 
    6564   
    6665  /** My type. */ 
     
    7069  type EnvBinding = TypeBinding 
    7170   
    72   /** New type environment with empty parent and the node's bindings. */ 
    73   def make(node: Any): STypeEnv = 
    74     new NestedSTypeEnv(EmptySTypeEnv, extractEnvBindings(node)) 
     71  def empty(): STypeEnv = EmptySTypeEnv  
     72   
    7573} 
    7674 
  • trunk/ProjectFortress/src/com/sun/fortress/scala_src/typechecker/staticenv/STypeEnvExtraction.scala

    r3915 r3921  
    3333import edu.rice.cs.plt.collect.Relation 
    3434 
    35 trait STypeEnvExtraction { self: STypeEnv.type => 
     35 
     36/** 
     37 * Provides the functionality for extracting type bindings from nodes and 
     38 * indices to create type environments. 
     39 */ 
     40trait STypeEnvExtraction  { self: STypeEnv.type => 
    3641   
    3742  /** Extract out the bindings in node. */ 
    38   def extractEnvBindings(node: Any): Collection[TypeBinding] = { 
    39     def recur(node: Any) = extractEnvBindings(node) 
    40     (node match { 
    41        
     43  def extractEnvBindings(node: Node): Iterable[TypeBinding] = { 
     44    def recur(node: Node) = extractEnvBindings(node) 
     45    (node match{ 
    4246      case SBinding(_, name, mods, Some(typ)) => 
    4347        TypeBinding(name, typ, mods, false) 
     
    5054      case SLValue(_, name, mods, Some(typ), mutable) => 
    5155        TypeBinding(name, typ, mods, mutable) 
    52        
     56         
    5357      case SLocalVarDecl(_, _, lValues, _) => lValues.map(recur) 
    54        
    55       case v:DeclaredVariable => recur(v.ast) 
    56        
    57       // Type erasure makes us put all JMap cases in one. 
    58       case m:JMap[_, _] => toMap(m).flatMap(xv => 
    59         xv match { 
    60            
    61           // Match Map[Id, Variable] 
    62           case (x:Id, v:DeclaredVariable) => recur(v) 
    63           case (x:Id, v:SingletonVariable) => 
    64             recur(NF.makeLValue(x, v.declaringTrait)) 
    65           case (x:Id, v:ParamVariable) => recur(v.ast) 
    66            
    67           // Bind object names to their types. 
    68           case (x:Id, v:ObjectTraitIndex) => 
    69             val decl = v.ast 
    70             val params = toOption(NU.getParams(decl)) 
    71             val sparams = NU.getStaticParams(decl) 
    72             val objType = params match { 
    73               case None if sparams.isEmpty => 
    74                 // Just a single trait type. 
    75                 NF.makeTraitType(x) 
    76               case None => 
    77                 // A generic trait type. 
    78                 NF.makeGenericSingletonType(x, sparams) 
    79               case Some(params) if sparams.isEmpty => 
    80                 // Arrow type for basic constructor. 
    81                 NF.makeArrowType(NU.getSpan(x), 
    82                                  STypesUtil.makeDomainType(toList(params)), 
    83                                  NF.makeTraitType(x)) 
    84               case Some(params) => 
    85                 // Generic arrow type for constructor. 
    86                 val sargs = toList(sparams).map(STypesUtil.staticParamToArg) 
    87                 NF.makeArrowType(NU.getSpan(decl), 
    88                                  false, 
    89                                  STypesUtil.makeDomainType(toList(params)), 
    90                                  NF.makeTraitType(x, toJavaList(sargs)), 
    91                                  NF.emptyEffect, // TODO: Change this? 
    92                                  sparams, 
    93                                  NU.getWhereClause(decl)) 
    94             } 
    95             List(TypeBinding(x, objType, Modifiers.None, false)) 
    96         }) 
    97        
    98       // Matches all, but must be [IdOrOpOrAnonymousName, ? <: Functional] 
    99       case r:Relation[IdOrOpOrAnonymousName, Functional] => 
    100         val fnNames = toSet(r.firstSet) 
    101          
    102         // For each name, intersect together all of its overloading types. 
    103         fnNames.flatMap(x => x match { 
    104            
    105           // Make sure this is actually a name. 
    106           case x: IdOrOpOrAnonymousName =>  
    107             val fns: Set[Functional] = toSet(r.matchFirst(x).asInstanceOf[JSet[Functional]]) 
    108             val oTypes = 
    109               fns.map(STypesUtil.makeArrowFromFunctional(_).asInstanceOf[Type]) 
    110             val fnType = NF.makeIntersectionType(oTypes) 
    111             Some(TypeBinding(x, fnType, Modifiers.None, false)) 
    112              
    113 //          case _ => None 
    114         }) 
    115        
    116       case xs:Iterable[_] => xs.flatMap(recur) 
    117        
     58 
    11859      case _ => Nil 
    119        
    120     }) match { // Guarantee that a Collection is returned. 
    121       case bs:Collection[TypeBinding] => bs 
     60    }) match{ 
     61      case bs:Iterable[TypeBinding] => bs 
    12262      case b:TypeBinding => List(b) 
    12363    } 
    12464  } 
     65   
     66//  def extractEnvBindings(m: JMap[Id, Variable]): Iterable[TypeBinding] = toMap(m).flatMap(xv =>{  
     67//    xv match{ 
     68//      case (x:Id, v:DeclaredVariable) => extractEnvBindings(v.ast) 
     69//      case (x:Id, v:SingletonVariable) => 
     70//        extractEnvBindings(NF.makeLValue(x, v.declaringTrait)) 
     71//      case (x:Id, v:ParamVariable) =>  extractEnvBindings(v.ast) 
     72//    } 
     73//  }) 
     74   
     75//  def extractEnvBindings(m: JMap[Id, TypeConsIndex]): Iterable[TypeBinding] = toMap(m).flatMap(xv => { 
     76//    xv match { 
     77//      // Bind object names to their types. 
     78//      case (x:Id, v:ObjectTraitIndex) => 
     79//        val decl = v.ast 
     80//        val params = toOption(NU.getParams(decl)) 
     81//        val sparams = NU.getStaticParams(decl) 
     82//        val objType = params match { 
     83//          case None if sparams.isEmpty => 
     84//            // Just a single trait type. 
     85//            NF.makeTraitType(x) 
     86//          case None => 
     87//            // A generic trait type. 
     88//            NF.makeGenericSingletonType(x, sparams) 
     89//          case Some(params) if sparams.isEmpty => 
     90//            // Arrow type for basic constructor. 
     91//            NF.makeArrowType(NU.getSpan(x), 
     92//                             STypesUtil.makeDomainType(toList(params)), 
     93//                             NF.makeTraitType(x)) 
     94//          case Some(params) => 
     95//            // Generic arrow type for constructor. 
     96//            val sargs = toList(sparams).map(STypesUtil.staticParamToArg) 
     97//            NF.makeArrowType(NU.getSpan(decl), 
     98//                             false, 
     99//                             STypesUtil.makeDomainType(toList(params)), 
     100//                             NF.makeTraitType(x, toJavaList(sargs)), 
     101//                             NF.emptyEffect, // TODO: Change this? 
     102//                             sparams, 
     103//                             NU.getWhereClause(decl)) 
     104//        } 
     105//        List(TypeBinding(x, objType, Modifiers.None, false)) 
     106//    } 
     107//  }) 
     108   
     109  def extractEnvBindings(r: Relation[IdOrOpOrAnonymousName, Functional]): Iterable[TypeBinding] = { 
     110    val fnNames = toSet(r.firstSet)      
     111    // For each name, intersect together all of its overloading types. 
     112    fnNames.flatMap(x => { 
     113        val fns: Set[Functional] = toSet(r.matchFirst(x).asInstanceOf[JSet[Functional]]) 
     114        val oTypes = 
     115          fns.map(STypesUtil.makeArrowFromFunctional(_).asInstanceOf[Type]) 
     116        val fnType = NF.makeIntersectionType(oTypes) 
     117        Some(TypeBinding(x, fnType, Modifiers.None, false)) 
     118    }) 
     119  } 
     120   
     121  def make(comp: CompilationUnitIndex): STypeEnv = { 
     122    EmptySTypeEnv//.extendWith(comp.functions) 
     123     // .extendWith(comp.variables) 
     124      //.extendWith(comp.typeConses) 
     125  } 
    125126} 
  • trunk/ProjectFortress/src/com/sun/fortress/scala_src/typechecker/staticenv/StaticEnv.scala

    r3915 r3921  
    1818package com.sun.fortress.scala_src.typechecker.staticenv 
    1919 
     20import _root_.java.util.{Map => JMap} 
    2021import com.sun.fortress.nodes.Name 
     22import com.sun.fortress.nodes.Node 
    2123import com.sun.fortress.nodes.Type 
     24import edu.rice.cs.plt.collect.Relation 
    2225 
    2326/** 
     
    2528 * variable names to some value. Each static environment contains  
    2629 */ 
    27 trait StaticEnv[T] extends Collection[StaticBinding[T]] { 
     30trait StaticEnv[T] extends Iterable[StaticBinding[T]] { 
    2831   
    2932  /** Define Env as the type of the implementing class. */ 
     
    4144   *         `node` combined. 
    4245   */ 
    43   def extendWith(node: Any): Env 
     46 
    4447   
    4548  /** 
     
    6164   */ 
    6265  def getType(x: Name): Option[Type] 
     66   
     67  /** Not in Iterable, but specify size. */ 
     68  def size: Int 
    6369   
    6470  /** Make the type on `Collection.elements` more specific. */ 
     
    123129  /** 
    124130   * Extracts all the _immediate_ bindings for this kind of environment from the 
    125    * given node. If `node` is a collection of nodes, then this method returns 
    126    * the concatenation of all bindings found therein. Any bindings located 
    127    * further inside the node will not be extracted. 
     131   * given node. Any bindings located further inside the node will not be 
     132   * extracted. 
    128133   *  
    129    * @param node A node or collection of nodes in which to extract bindings. 
     134   * @param node A node in which to extract bindings. 
    130135   * @return A collection of all the bindings extracted in the given node. 
    131136   */ 
    132   protected def extractEnvBindings(node: Any): Collection[EnvBinding] 
     137  protected def extractEnvBindings(node: Node): Iterable[EnvBinding] 
     138   
     139  /** 
     140   * Extracts all the _immediate_ bindings for this kind of environment from the 
     141   * given nodes. Any bindings located further inside the nodes will not be 
     142   * extracted. This overloading simply flatMaps the other overloading for 
     143   * convenience. 
     144   *  
     145   * @param nodes A collection of nodes in which to extract bindings. 
     146   * @return A collection of all the bindings extracted in the given node. 
     147   */ 
     148  protected def extractEnvBindings(nodes: Iterable[Node]) 
     149                                   : Iterable[EnvBinding] = 
     150    nodes.flatMap(extractEnvBindings) 
    133151   
    134152  /** 
     
    139157   * @return A new instance of Env containing these bindings. 
    140158   */ 
    141   def make(node: Any): Env 
     159  def empty(): Env 
     160   
    142161} 
    143162