Wiki

Changeset 2304

Show
Ignore:
Timestamp:
03/08/10 06:19:46 (2 years ago)
Author:
Chuck.Esterbrook
Message:

Code cleanup. In CobraType?.memberForName, assert that the type .didBindInh and .didBindInt. Also, create all nilable types through .typeProvider.nilableType(t) and cache.

Location:
cobra/trunk/Source
Files:
8 modified

Legend:

Unmodified
Added
Removed
  • cobra/trunk/Source/BinaryOpExpr.cobra

    r2297 r2304  
    962962        base._bindImp 
    963963        if not _type inherits NilableType 
    964             _type = NilableType(_type).bindAll to NilableType  # CC: axe cast when 'as this' is supported 
     964            _type = .typeProvider.nilableType(_type to !) 
    965965        # warn about redundancy 
    966966        if not .left.hasError and not .right.hasError 
  • cobra/trunk/Source/Compiler.cobra

    r2277 r2304  
    483483    var _unspecifiedType as UnspecifiedType? 
    484484    var _voidType as VoidType? 
     485    var _nilableTypes as Dictionary<of INode, NilableType>? 
    485486    var _variTypes as Dictionary<of INode, VariType>? 
    486487    var _nilableDynamicType as NilableType? 
     
    597598        return _voidType to ! 
    598599 
     600    def nilableType(type as IType) as NilableType 
     601        if type inherits NilableType, return type 
     602        if _nilableTypes is nil 
     603            _nilableTypes = Dictionary<of INode, NilableType>() 
     604        if _nilableTypes.containsKey(type) 
     605            return _nilableTypes[type] 
     606        else 
     607            _nilableTypes[type] = nt = NilableType(type) 
     608            nt.bindInh 
     609            nt.bindInt 
     610            return nt 
     611 
    599612    def variType(type as IType) as VariType 
    600613        if _variTypes is nil 
     
    615628 
    616629    ## More type stuff 
    617  
    618     def nilableType(t as IType) as NilableType 
    619         """ 
    620         Returns a NilableType wrapper for t, unless t is already a NilableType in which case 
    621         it is returned directly. 
    622         """ 
    623         if t inherits NilableType 
    624             return t 
    625         else 
    626             return NilableType(t).bindAll to NilableType  # CC: axe cast after "as this" 
    627630 
    628631    def readSystemTypes 
  • cobra/trunk/Source/Container.cobra

    r2299 r2304  
    220220            return type.greatestCommonDenominatorWith(this) 
    221221        if type inherits NilType 
    222             return NilableType(this) 
     222            return .typeProvider.nilableType(this) 
    223223        if .isDescendantOf(type) 
    224224            return type 
  • cobra/trunk/Source/Expr.cobra

    r2297 r2304  
    30393039    def _bindImp 
    30403040        base._bindImp 
    3041         if _expr.type inherits NilableType 
    3042             _type = _expr.type 
    3043             .compiler.warning(this, 'The given expression is already nilable so "to ?" is redundant. You can remove it.')  # TODO: needs test case 
    3044         else 
    3045             _type = NilableType(_expr.type) 
     3041        if not _expr.hasError 
     3042            assert _expr.type 
     3043            if _expr.type inherits NilableType 
     3044                _type = _expr.type 
     3045                .compiler.warning(this, 'The given expression is already nilable so "to ?" is redundant. You can remove it.')  # TODO: needs test case 
     3046            else 
     3047                _type = .typeProvider.nilableType(_expr.type to !) 
    30463048 
    30473049 
  • cobra/trunk/Source/Phases/BindInheritancePhase.cobra

    r2277 r2304  
    1717        c.objectType.bindInh 
    1818        c.stringType.bindInh 
     19        c.dynamicType.bindInh 
     20        c.passThroughType.bindInh 
    1921        for mod in c.modules.clone 
    2022            c.curModule = mod 
  • cobra/trunk/Source/Phases/BindInterfacePhase.cobra

    r2277 r2304  
    1818        c.objectType.bindInt 
    1919        c.stringType.bindInt 
     20        c.dynamicType.bindInt 
     21        c.passThroughType.bindInt 
    2022        for mod in c.modules.clone 
    2123            c.curModule = mod 
  • cobra/trunk/Source/TypeProxies.cobra

    r2277 r2304  
    8484 
    8585    get realType as IType is override 
    86         return NilableType(_innerType.realType) 
     86        return .typeProvider.nilableType(_innerType.realType) 
    8787 
    8888 
     
    267267                    if member.qualifiedName == 'System.Nullable<of>' 
    268268                        assert args.count == 1 
    269                         member = NilableType(args[0]) 
     269                        member = .typeProvider.nilableType(args[0]) 
    270270                    else 
    271271                        member = (member to Box).constructedTypeFor(args) 
     
    472472 
    473473    def _resolveType as IType is override 
    474         return ArrayType(_typeId.realType) 
     474        t = ArrayType(_typeId.realType) 
     475        t.bindInh 
     476        t.bindInt 
     477        return t 
    475478 
    476479 
     
    485488 
    486489    def _resolveType as IType is override 
    487         return NilableType(_typeId.realType) 
     490        return .typeProvider.nilableType(_typeId.realType) 
    488491 
    489492    get name as String is override 
     
    493496        m = container.memberForName(.name[:-1]) 
    494497        if m inherits IType 
    495             return NilableType(m) 
     498            return .typeProvider.nilableType(m) 
    496499        else 
    497500            return m 
  • cobra/trunk/Source/Types.cobra

    r2240 r2304  
    103103    get voidType as VoidType 
    104104 
    105     # TODO: def nilableType ... just like .variType. cache and get "is" testing 
     105    def nilableType(t as IType) as NilableType 
    106106 
    107107    def variType(type as IType) as VariType 
     
    133133    var _unspecifiedType    as UnspecifiedType? 
    134134    var _voidType           as VoidType? 
     135    var _nilableTypes       as Dictionary<of IType, NilableType>? 
    135136    var _variTypes          as Dictionary<of IType, VariType>? 
    136137    var _defaultType        as IType? 
     
    231232        return _voidType to ! 
    232233 
     234    def nilableType(type as IType) as NilableType 
     235        if type inherits NilableType, return type 
     236        if _nilableTypes is nil 
     237            _nilableTypes = Dictionary<of IType, NilableType>() 
     238        if _nilableTypes.containsKey(type) 
     239            return _nilableTypes[type] 
     240        else 
     241            _nilableTypes[type] = nt = NilableType(type) 
     242            nt.bindInh 
     243            nt.bindInt 
     244            return nt 
     245 
    233246    def variType(type as IType) as VariType 
    234247        if _variTypes is nil 
     
    247260    def defaultType as IType 
    248261        if _defaultType is nil 
    249             _defaultType = NilableType(.dynamicType) 
     262            _defaultType = .nilableType(.dynamicType) 
    250263        return _defaultType to ! 
    251264 
     
    698711            return type.greatestCommonDenominatorWith(this) 
    699712        if type inherits NilType 
    700             return NilableType(this) 
     713            return .typeProvider.nilableType(this) 
    701714        if .isDescendantOf(type) 
    702715            return type 
     
    757770        Returns a named member of System.Object by default. 
    758771        """ 
     772        assert .didBindInh and .didBindInt 
    759773        objectClass = .compiler.objectType 
    760774        return objectClass.memberForName(name) 
     
    10641078        if this == type 
    10651079            return this 
    1066         if type inherits NilableType 
    1067             return NilableType(this) 
    1068         if type inherits NilType 
    1069             return NilableType(this) 
     1080        if type inherits NilableType or type inherits NilType 
     1081            return .typeProvider.nilableType(this) 
    10701082        return this 
    10711083 
     
    13231335 
    13241336class NilableType inherits WrappedType is partial 
     1337    """ 
     1338    Rather than instantiating this type directly, use: 
     1339     
     1340        .typeProvider.nilableType(t) 
     1341 
     1342    Or if the code block already belongs to a type provider: 
     1343     
     1344        .nilableType(t) 
     1345    """ 
    13251346 
    13261347    cue init(t as IType?) 
     
    13731394 
    13741395    def memberForName(name as String) as IMember? is override 
     1396        _bindBasics 
    13751397        return _wrappedType.memberForName(name) 
    13761398 
     
    13841406                return this 
    13851407            else 
    1386                 return NilableType(_wrappedType.greatestCommonDenominatorWith(type.nonNil)) 
    1387         return NilableType(_wrappedType.greatestCommonDenominatorWith(type)) 
     1408                return .typeProvider.nilableType(_wrappedType.greatestCommonDenominatorWith(type.nonNil)) 
     1409        return .typeProvider.nilableType(_wrappedType.greatestCommonDenominatorWith(type)) 
    13881410 
    13891411    def suggestionsForBadMemberName(name as String) as List<of String> is override 
     
    14191441            return type 
    14201442        else 
    1421             return NilableType(type) 
     1443            return .typeProvider.nilableType(type) 
    14221444 
    14231445# TODO: 
     
    16231645 
    16241646    get innerType as IType? is override 
    1625         return _wrappedType.innerType  # TODO: 2008-11-22, I think this should just be _wrappedType 
     1647        return _wrappedType.innerType  # TODO: 2008-11-22, I think this should just be _wrappedType; 2010-03-07: or abstract and make subclasses specify 
    16261648 
    16271649    get suffix as String is abstract 
    1628      
     1650 
     1651    def memberForName(name as String) as IMember? is override 
     1652        _bindBasics 
     1653        return base.memberForName(name) 
     1654 
    16291655    def secondaryConstructedTypeFor(box as Box, gpToType as Dictionary<of GenericParam, IType>) as IType is override 
    16301656        thisType = .getType 
    16311657        return thisType(_wrappedType.secondaryConstructedTypeFor(box, gpToType)) 
     1658 
     1659    def _bindBasics 
     1660        .bindInh 
     1661        .bindInt 
    16321662 
    16331663