Changeset 1768

Show
Ignore:
Timestamp:
11/16/08 00:26:47 (8 weeks ago)
Author:
Chuck.Esterbrook
Message:

Code cleanup.
Introduce IType.nonNil which makes coding more succinct.

Location:
cobra/trunk/Source
Files:
7 modified

Legend:

Unmodified
Added
Removed
  • cobra/trunk/Source/BackEndClr/SharpGenerator.cobra

    r1767 r1768  
    33743374                                        .writeSharpArgs(sw) 
    33753375                                        sw.write(')') 
    3376                                 else if expr.type.isDescendantOf(.compiler.delegateType) 
    3377                                         isMethodSig = true 
    3378                                 else if expr.type inherits NilableType and (expr.type to NilableType).theWrappedType.isDescendantOf(.compiler.delegateType) 
     3376                                else if expr.type.nonNil.isDescendantOf(.compiler.delegateType) 
    33793377                                        isMethodSig = true 
    33803378                                else if expr.type.isSystemTypeClass or _type.isDynamic 
     
    33833381                                        assert false, expr 
    33843382                        else if expr inherits IndexExpr 
    3385                                 if expr.type.isDescendantOf(.compiler.delegateType) 
     3383                                if expr.type.nonNil.isDescendantOf(.compiler.delegateType) 
    33863384                                        isMethodSig = true 
    33873385                                else if expr.type.isSystemTypeClass or _type.isDynamic 
     
    38683866        def writeSharpDef(sw as SharpWriter, parens as bool) is override 
    38693867                type = _expr.type 
    3870                 if type inherits NilableType and not (type to NilableType).theWrappedType.isReference 
     3868                if type inherits NilableType and not type.nonNil.isReference 
    38713869                        # ex: (x).Value 
    38723870                        # ex: (obj.foo).Value 
     
    44104408                right = .right 
    44114409                t = right.definition to IType 
    4412                 if t inherits NilableType, t = t.theWrappedType 
     4410                t = t.nonNil 
    44134411                typeSharpRef = t.sharpRef 
    44144412                if t.isReference 
  • cobra/trunk/Source/BinaryOpExpr.cobra

    r1767 r1768  
    296296                tstring = .compiler.stringType 
    297297                cannotMix = false 
    298                 leftType = _left.type 
    299                 if leftType inherits NilableType 
    300                         leftType = leftType.theWrappedType 
    301                 rightType = _right.type 
    302                 if rightType inherits NilableType 
    303                         rightType = rightType.theWrappedType 
    304  
    305                 tgcd = leftType.greatestCommonDenominatorWith(rightType to !) 
     298                leftType = _left.type.nonNil 
     299                rightType = _right.type.nonNil 
     300 
     301                tgcd = leftType.greatestCommonDenominatorWith(rightType) 
    306302 
    307303                if leftType.isDescendantOf(tint) 
     
    383379                        # TODO: error 
    384380                        pass 
    385                 else if .left.type.isDescendantOf(.compiler.delegateType) 
     381                else if .left.type.nonNil.isDescendantOf(.compiler.delegateType) 
    386382                        leftSource = .left.toCobraSource 
    387383                        rightSource = .right.toCobraSource 
     
    459455        def _bindImp # TODO: this is all messed up. does this get called? TODO: this is missing, so its not being run. resolve with base class: is override 
    460456                base._bindImp 
    461                 lt = _left.type 
    462                 if lt inherits NilableType 
    463                         lt = lt.theWrappedType 
    464                 rt = _right.type 
    465                 if rt inherits NilableType 
    466                         rt = rt.theWrappedType 
     457                lt = _left.type.nonNil 
     458                rt = _right.type.nonNil 
    467459                if lt is rt 
    468460                        assert _type == lt or (_type is .compiler.decimalType and _op=='SLASH') 
     
    731723                                if leftType 
    732724                                        okay = true 
    733                                         if leftType inherits DynamicType or (leftType inherits NilableType and (leftType to NilableType).theWrappedType inherits DynamicType) 
     725                                        if leftType.isDynamic 
    734726                                                pass 
    735727                                        else if leftType.isDescendantOf(rightType) 
     
    738730                                                # ex: someShape inherits Circle 
    739731                                                pass 
    740                                         else if leftType inherits NilableType and not rightType inherits NilableType and rightType.isStrictDescendantOf((leftType to NilableType).theWrappedType) 
     732                                        else if leftType inherits NilableType and not rightType inherits NilableType and rightType.isStrictDescendantOf(leftType.nonNil) 
    741733                                                # ex: someNilableShape inherits Circle 
    742734                                                pass 
    743                                         else if leftType inherits NilableType and not rightType inherits NilableType and (leftType to NilableType).theWrappedType.isDescendantOf(rightType) 
     735                                        else if leftType inherits NilableType and not rightType inherits NilableType and leftType.nonNil.isDescendantOf(rightType) 
    744736                                                # ex: someNilableCircle inherits Shape 
    745737                                                .compiler.warning(this, 'The expression will always be of type "[.right.toCobraSource]" when not nil. You can just check for not nil by removing "inherits [.right.toCobraSource]".') 
     
    823815                if rightType 
    824816                        if _left.type inherits NilableType and not rightType inherits NilableType 
    825                                 if (_left.type to NilableType).theWrappedType is rightType 
     817                                if _left.type.nonNil == rightType 
    826818                                        .compiler.warning(this, 'The given expression is already a "[_rightTypeExpr.toCobraSource]", but nilable. You can just use "to !".') 
    827819                        else if not _left.type inherits NilableType and rightType inherits NilableType 
    828                                 if (rightType to NilableType).theWrappedType == _left.type 
     820                                if rightType.nonNil == _left.type 
    829821                                        .compiler.warning(this, 'The given expression is already a "[_left.type.name]", but not nilable. You can just use "to ?".') 
    830822                        else if _left.type == rightType 
     
    868860                else if _left.type inherits NilableType and not _right.type inherits NilableType 
    869861                        # x ? y   ...where x can be nil, but y cannot 
    870                         _type = (_left.type to NilableType).theWrappedType  # TODO: should be greatest common denominator between the two 
     862                        _type = _left.type.nonNil  # TODO: should be greatest common denominator between the two 
    871863                else 
    872864                        # the catch all case 
     
    894886#               else if _right.type inherits NilableType and not _right.type inherits NilableType 
    895887#                       # x ? y   ...where x can be nil, but y cannot 
    896 #                       _type = _left.type.theWrappedType  # TODO: should be greatest common denominator between the two 
     888#                       _type = _left.type.nonNil  # TODO: should be greatest common denominator between the two 
    897889                else 
    898890                        # the catch all case 
  • cobra/trunk/Source/Boxes.cobra

    r1718 r1768  
    426426                                                        break 
    427427                        rt = getEnum.resultType 
    428                         if rt is .compiler.passThroughType 
     428                        if rt.isDynamicOrPassThrough 
    429429                                return rt 
    430                         if rt.isDynamic 
    431                                 return rt 
    432                         if rt inherits NilableType 
    433                                 # nilable is not a concern; unwrap it: 
    434                                 rt = rt.theWrappedType 
     430                        rt = rt.nonNil  # nilable is not a concern; unwrap it 
    435431                        if rt inherits Box and (rt to Box).isGeneric 
    436432                                # don't take the first argument of the result type -- that won't work for a nested type in a generic class, like ValueCollection, which gets the generic params of its parents 
     
    688684                r = base.isAssignableTo(type) 
    689685                if not r 
    690                         if type inherits NilableType 
    691                                 type = type.theWrappedType 
     686                        type = type.nonNil 
    692687                        if type inherits PrimitiveType 
    693688                                if type.systemAliasType == this 
  • cobra/trunk/Source/Container.cobra

    r1656 r1768  
    253253                                if not type.didBindInt  # TODO: shouldn't need this 
    254254                                        type.bindInt 
    255                                 return .isAssignableTo(type.theWrappedType to passthrough)  # CC: weird cast 
     255                                return .isAssignableTo(type.theWrappedType to passthrough)  # CC: weird cast. bug. has to do with if-inherits and invoking the same method 
    256256                        result_ = .isDescendantOf(type) # CC: rename to result 
    257257                        return result_ 
     
    259259        # CC: duplicated from Type 
    260260        def isComparableTo(t as IType) as bool 
     261                t = t.nonNil 
    261262                if t inherits NilableType 
    262263                        t = t.theWrappedType 
     
    269270        # CC: duplicated from Type 
    270271        def isEquatableTo(t as IType) as bool 
    271                 if t inherits NilableType 
    272                         t = t.theWrappedType 
     272                t = t.nonNil 
    273273                if .isAssignableTo(t) or t.isAssignableTo(this)  # CC: to ! or assert above with nil flow analysis 
    274274                        return true 
    275275                return false 
     276 
     277        # CC: duplicated from Type 
     278        get nonNil as IType 
     279                return this 
    276280 
    277281        def isDescendantOf(type as IType) as bool 
  • cobra/trunk/Source/Expr.cobra

    r1767 r1768  
    609609                                        else if arg.canBeAssignedTo(param.type) 
    610610                                                score += 10 
    611                                         else if arg.type inherits NilableType and (arg.type to NilableType).theWrappedType.isAssignableTo(param.type)  
     611                                        else if arg.type.nonNil.isAssignableTo(param.type)  
    612612                                                # Cobra's code and data flow analysis sometimes leaves us with a nilable type that's not actually nil anymore 
    613613                                                # due to an assignment, possibly wrapped in an if statement. Eventually this will be corrected, but for now 
     
    706706                                        print '<> param.type =', param.type 
    707707                                        print '<> param.ifInheritsStack.count =', param.ifInheritsStack.count 
    708                                 if arg.type inherits NilableType and not param.type inherits NilableType and (arg.type to NilableType).theWrappedType.isAssignableTo(param.type) 
     708                                if arg.type inherits NilableType and not param.type inherits NilableType and arg.type.nonNil.isAssignableTo(param.type) 
    709709                                        .throwError('Argument [i+1] of method "[_name]" expects a non-nilable type ([param.type.name]), but the call is supplying a nilable type ([arg.type.name]).') 
    710710                                else 
     
    11671167                                        print '<> param = ' stop 
    11681168                                        param.writeDeepString 
    1169                                 if arg.type inherits NilableType and not param.type inherits NilableType and (arg.type to NilableType).theWrappedType.isAssignableTo(param.type) 
     1169                                if arg.type inherits NilableType and not param.type inherits NilableType and arg.type.nonNil.isAssignableTo(param.type) 
    11701170                                        .throwError('Argument [i+1] of indexer expects a non-nilable type ([param.type.name]), but the call is supplying a nilable type ([arg.type.name]).') 
    11711171                                else 
     
    15671567                if _type is nil 
    15681568                        assert expr.type is not nil 
    1569                         exprType = expr.type 
    1570                         if exprType inherits NilableType 
    1571                                 exprType = exprType.theWrappedType 
     1569                        exprType = expr.type.nonNil 
    15721570                        if exprType inherits MethodSig 
    15731571                                _type = exprType.returnType 
     
    19351933                        _expr.bindImp 
    19361934                else if type inherits NilableType 
    1937                         if type.theWrappedType inherits DynamicType 
     1935                        if type.nonNil inherits DynamicType 
    19381936                                _treatment = Treatment.InvokeRuntime 
    19391937                        else 
     
    28832881                base._bindImp 
    28842882                if (et = _expr.type) inherits NilableType 
    2885                         _type = et.theWrappedType 
     2883                        _type = et.nonNil 
    28862884                else 
    28872885                        .compiler.warning(this, 'The given expression is already non-nilable so "to !" is redundant. You can remove it.') # TODO: needs test case 
  • cobra/trunk/Source/Members.cobra

    r1767 r1768  
    287287                        return true 
    288288                if (a inherits NilableType) <> (b inherits NilableType) 
    289                         if a inherits NilableType, a = a.theWrappedType 
    290                         if b inherits NilableType, b = b.theWrappedType 
    291                         return a == b 
     289                        return a.nonNil == b.nonNil 
    292290                return false 
    293291 
     
    374372                        _handlerType = _handlerTypeProxy.realType 
    375373                # TODO: error check that _handlerType is a delegate 
    376                 # _handlerType.isDescendantOf(.compiler.delegateType) 
     374                # _handlerType.nonNil.isDescendantOf(.compiler.delegateType) 
    377375 
    378376 
  • cobra/trunk/Source/Types.cobra

    r1737 r1768  
    323323                return this is not t and t.isSystemObjectClass 
    324324 
     325        get nonNil as IType 
     326                return this 
     327 
    325328        ## Methods 
    326329 
     
    512515                """ 
    513516                Returns true if this type can be compared to the given type via `is`, `is not`, `==` and `<>`. 
     517                """ 
     518 
     519        get nonNil as IType 
     520                """ 
     521                Returns the non-nil version of the receiver. 
     522                This is normally just the receiver itself, except in the case of NilableType. 
    514523                """ 
    515524 
     
    616625 
    617626        def isComparableTo(t as IType) as bool 
    618                 if t inherits NilableType 
    619                         t = t.theWrappedType 
     627                t = t.nonNil 
    620628                compareTo = .memberForName('compareTo') 
    621629                if compareTo and compareTo.isMethod and compareTo.resultType is .compiler.intType and t.isDescendantOf(this) 
     
    627635 
    628636        def isEquatableTo(t as IType) as bool 
    629                 if t inherits NilableType 
    630                         t = t.theWrappedType 
     637                t = t.nonNil 
    631638                if .isAssignableTo(t) or t.isAssignableTo(this) 
    632639                        return true 
    633640                return false 
     641 
     642        get nonNil as IType 
     643                return this 
    634644 
    635645        get requiresThis as bool 
     
    13191329                if _wrappedType.isDynamic, return true 
    13201330                if type inherits PassThroughType, return true 
    1321                 if type inherits NilableType, return _wrappedType.isAssignableTo(type.theWrappedType) 
     1331                if type inherits NilableType, return _wrappedType.isAssignableTo(type.nonNil) 
    13221332                return false 
    13231333 
     
    13361346                else 
    13371347                        return _wrappedType.isEquatableTo(b) 
     1348 
     1349        get nonNil as IType is override 
     1350                return _wrappedType 
    13381351 
    13391352        def memberForName(name as String) as IMember? is override 
     
    13461359                        return this 
    13471360                if type inherits NilableType 
    1348                         if _wrappedType is type.theWrappedType 
     1361                        if _wrappedType is type.nonNil 
    13491362                                return this 
    13501363                        else 
    1351                                 return NilableType(_wrappedType.greatestCommonDenominatorWith(type.theWrappedType)) 
     1364                                return NilableType(_wrappedType.greatestCommonDenominatorWith(type.nonNil)) 
    13521365                return NilableType(_wrappedType.greatestCommonDenominatorWith(type)) 
    13531366 
     
    13671380 
    13681381        def isAssignableTo(type as IType) as bool is override 
    1369                 if this is type 
    1370                         return true 
    1371                 if type is .compiler.passThroughType 
    1372                         return true 
    1373                 if type inherits NilableType 
    1374                         return true 
     1382                if this is type, return true 
     1383                if type is .compiler.passThroughType, return true 
     1384                if type inherits NilableType, return true 
    13751385                return false 
    13761386 
     
    16231633 
    16241634        def isNilableAndDescendantOf(otherType as IType) as bool 
    1625                 return this inherits NilableType and ((this to NilableType).theWrappedType).isDescendantOf(otherType) 
     1635                return this inherits NilableType and .nonNil.isDescendantOf(otherType) 
    16261636 
    16271637        def isSequenceLike as bool 
     
    16331643                type = this 
    16341644                if type inherits NilableType 
    1635                         return type.theWrappedType.isSequenceLike 
     1645                        return type.nonNil.isSequenceLike 
    16361646                # TODO: check for ISliceable which would have a .getSlice 
    16371647                tp = Node.getCompiler  # 'tp' for 'type provider' 
     
    16671677                type = this 
    16681678                if type inherits NilableType 
    1669                         return type.theWrappedType.isDictionaryLike 
     1679                        return type.nonNil.isDictionaryLike 
    16701680                tp = Node.getCompiler  # 'tp' for 'type provider' # TODO? expand ITypeProvider to cover idictionaryType, dictionaryOfType, idictionaryOfType 
    16711681                if type.isDescendantOf(tp.idictionaryType)