Esempio n. 1
0
def solve_constraints( vars, constraints, basic):
    """Solve type constraints.

    Return lower bound for each type variable or None if the variable could
    not be solved.
    """
    # Collect a list of constraints for each type variable.
    cmap = {}
    for con in constraints:
        a = cmap.get(con.type_var, [])
        a.append(con)
        cmap[con.type_var] = a
    
    res = []

    # Solve each type variable separately.
    for tvar in vars:
        bottom = None
        top = None
        
        # Process each contraint separely, and calculate the lower and upper
        # bounds based on constraints. Note that we assume that the contraint
        # targets do not have contraint references.
        for c in cmap.get(tvar, []):
            if c.op == SUPERTYPE_OF:
                if bottom is None:
                    bottom = c.target
                else:
                    bottom = join_types(bottom, c.target, basic)
            else:
                if top is None:
                    top = c.target
                else:
                    top = meet_types(top, c.target, basic)
        
        if top is None:
            if isinstance(bottom, Void):
                top = Void()
            else:
                top = basic.object
        
        if bottom is None:
            if isinstance(top, Void):
                bottom = Void()
            else:
                bottom = NoneTyp()
        
        if isinstance(top, Any) or isinstance(bottom, Any):
            top = Any()
            bottom = Any()
        
        # Pick the most specific type if it satisfies the constraints.
        if (not top or not bottom or is_subtype(bottom, top)) and (
                not isinstance(top, ErrorType) and
                not isinstance(bottom, ErrorType)):
            res.append(bottom)
        else:
            res.append(None)
    
    return res
Esempio n. 2
0
 def test_simple_type_objects(self):
     t1 = self.type_callable(self.fx.a, self.fx.a)
     t2 = self.type_callable(self.fx.b, self.fx.b)
     
     self.assert_join(t1, t1, t1)
     assert_true(join_types(t1, t1, self.fx.basic).is_type_obj())
     
     self.assert_join(t1, t2, self.fx.std_type)
     self.assert_join(t1, self.fx.std_type, self.fx.std_type)
     self.assert_join(self.fx.std_type, self.fx.std_type, self.fx.std_type)
Esempio n. 3
0
 def assert_simple_join(self, s, t, join):
     result = join_types(s, t, self.fx.basic)
     actual = str(result)
     expected = str(join)
     assert_equal(actual, expected,
                  'join({}, {}) == {{}} ({{}} expected)'.format(s, t))
     if not isinstance(s, ErrorType) and not isinstance(result, ErrorType):
         assert_true(is_subtype(s, result),
                     '{} not subtype of {}'.format(s, result))
     if not isinstance(t, ErrorType) and not isinstance(result, ErrorType):
         assert_true(is_subtype(t, result),
                     '{} not subtype of {}'.format(t, result))
Esempio n. 4
0
 Typ[] res = []
 
 # Solve each type variable separately.
 for tvar in vars:
     Typ bottom = None
     Typ top = None
     
     # Process each contraint separely, and calculate the lower and upper
     # bounds based on constraints. Note that we assume that the contraint
     # targets do not have contraint references.
     for c in cmap.get(tvar, []):
         if c.op == SUPERTYPE_OF:
             if bottom is None:
                 bottom = c.target
             else:
                 bottom = join_types(bottom, c.target, basic)
         else:
             if top is None:
                 top = c.target
             else:
                 top = meet_types(top, c.target, basic)
     
     if top is None:
         if isinstance(bottom, Void):
             top = Void()
         else:
             top = basic.object
     
     if bottom is None:
         if isinstance(top, Void):
             bottom = Void()