Wiki

Changeset 2310

Show
Ignore:
Timestamp:
03/11/10 09:47:16 (2 years ago)
Author:
Chuck.Esterbrook
Message:

Taught the compiler that arrays implement ICloneable, ICollection<of> and IEnumerable<of>. Extensions methods are picked up so someArray.toList now works.

Location:
cobra/trunk
Files:
1 added
4 modified

Legend:

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

    r2304 r2310  
    782782            else if not _canContain(.right.type to !, .left.type to !) 
    783783                .recordError('The left expression ([.left.toCobraSource]) cannot be "in" the right-hand expression because the left type is "[.left.type.name]" and the right-hand expression contains type "[.right.type.innerType.name]".') 
     784            else if _right.type inherits ArrayType 
     785                pass 
    784786            else if not _right.type.isDynamic 
    785787                # try to use right.contains(left) 
  • cobra/trunk/Source/Members.cobra

    r2299 r2310  
    756756 
    757757    def unNilReturnType is override 
    758         assert _returnType is nil 
    759         if _returnTypeNode inherits NilableTypeProxy 
     758        if _returnType 
     759            if _returnType inherits NilableType 
     760                _returnType = _returnType.nonNil 
     761        else if _returnTypeNode inherits NilableTypeProxy 
    760762            _returnTypeNode = _returnTypeNode.innerTypeProxy 
    761763         
     
    12741276 
    12751277    def unNilReturnType is override 
    1276         assert _returnType is nil 
    1277         if _returnTypeNode inherits NilableTypeProxy 
     1278        if _returnType 
     1279            if _returnType inherits NilableType 
     1280                _returnType = _returnType.nonNil 
     1281        else if _returnTypeNode inherits NilableTypeProxy 
    12781282            _returnTypeNode = _returnTypeNode.innerTypeProxy 
    12791283 
  • cobra/trunk/Source/Types.cobra

    r2304 r2310  
    673673    def isEquatableTo(t as IType) as bool 
    674674        t = t.nonNil 
    675         if this == t 
    676             return true 
    677         if .isAssignableTo(t) or t.isAssignableTo(this) 
    678             return true 
     675        if this == t, return true 
     676        if .isAssignableTo(t) or t.isAssignableTo(this), return true 
    679677        return false 
    680678 
    681679    get nonNil as IType 
     680        ensure not result inherits NilableType 
    682681        return this 
    683682 
     
    770769        Returns a named member of System.Object by default. 
    771770        """ 
    772         assert .didBindInh and .didBindInt 
     771        assert .didBindInh and .didBindInt  # don't `require` because subclass overrides may check and fix before invoking base 
    773772        objectClass = .compiler.objectType 
    774773        return objectClass.memberForName(name) 
     
    969968        member = base.memberForName(name) 
    970969        if member is nil 
    971             member = _box.symbolForName(name, true) 
     970            member = _box.symbolForName(name, true)  # TODO: should that be .memberForName instead of .symbolForName? 
    972971        return member 
    973972 
    974973    def isAssignableTo(type as IType) as bool 
    975974        r = base.isAssignableTo(type) 
    976         if not r 
    977             r = _systemAliasType == type 
     975        if not r, r = _systemAliasType == type 
    978976        return r 
    979977 
     
    985983 
    986984    def isComparableTo(t as IType) as bool 
    987         if this is t 
    988             return true 
    989         else 
    990             return base.isComparableTo(t) 
     985        return this is t or base.isComparableTo(t) 
    991986 
    992987 
     
    13431338     
    13441339        .nilableType(t) 
     1340         
     1341    Note that NilableType will not wrap another NilableType. Also, .typeProvider.nilableType(t) 
     1342    silently handles this so in practice you don't have to guard against it. 
    13451343    """ 
    13461344 
    13471345    cue init(t as IType?) 
     1346        require not t inherits NilableType 
    13481347        base.init(t) 
    13491348 
     
    14091408        return .typeProvider.nilableType(_wrappedType.greatestCommonDenominatorWith(type)) 
    14101409 
     1410    def secondaryConstructedTypeFor(box as Box, gpToType as Dictionary<of GenericParam, IType>) as IType is override 
     1411        wt = _wrappedType.secondaryConstructedTypeFor(box, gpToType) 
     1412        if not wt inherits NilableType 
     1413            thisType = .getType 
     1414            return thisType(wt) 
     1415        else 
     1416            return wt 
     1417 
    14111418    def suggestionsForBadMemberName(name as String) as List<of String> is override 
    14121419        return _wrappedType.suggestionsForBadMemberName(name) 
     
    16831690            Node.typeProvider = saveTP 
    16841691 
    1685     var _box as Box? 
    1686  
    16871692    cue init(wrappedType as IType?) 
    16881693        base.init(wrappedType) 
     
    16971702        return true 
    16981703 
     1704    get box from var as Box? 
     1705 
    16991706    get name as String is override 
    17001707        return _wrappedType.name + r'[]' 
     
    17041711 
    17051712    def isAssignableTo(type as IType) as bool is override 
    1706         if base.isAssignableTo(type) 
    1707             return true 
     1713        if base.isAssignableTo(type), return true 
    17081714        if type inherits ArrayType 
    1709             if _wrappedType == type.theWrappedType 
    1710                 return true 
     1715            if _wrappedType == type.theWrappedType, return true 
    17111716            # covariance with arrays 
    1712             if _wrappedType.isAssignableTo(type.theWrappedType) 
    1713                 return true 
     1717            if _wrappedType.isAssignableTo(type.theWrappedType), return true 
    17141718        if type inherits StreamType 
    1715             if .isAssignableTo(type.box to !) 
    1716                 return true 
     1719            if .isAssignableTo(type.box to !), return true 
    17171720            # covariance with streams 
    1718             if _wrappedType.isAssignableTo(type.innerType to !) 
    1719                 return true  
    1720         return false 
     1721            if _wrappedType.isAssignableTo(type.innerType to !), return true 
     1722        if _box.isAssignableTo(type), return true 
     1723        return false 
     1724 
     1725    def isDescendantOf(type as IType) as bool 
     1726        return base.isDescendantOf(type) or .box.isDescendantOf(type) 
     1727 
     1728    def memberForName(name as String) as IMember? is override 
     1729        m = base.memberForName(name) 
     1730        if m is nil, m = .box.memberForName(name) 
     1731        # TODO: dup'ed from StreamType: 
     1732        if m is nil and _box and .compiler and .compiler.nodeStack.count and (.compiler.nodeStack.peek to Node).isBindingImp 
     1733            # look for an extension member, but it has to be accessible according to namespaces 
     1734            if .compiler.nameSpaceStack.count 
     1735                # don't need to go through the whole namespace stack because the namespace will check its parent 
     1736                m = .compiler.curNameSpace.extensionMemberFor(_box to !, name) 
     1737        return m 
     1738 
     1739    def suggestionsForBadMemberName(name as String) as List<of String> is override 
     1740        return .box.suggestionsForBadMemberName(name) 
    17211741 
    17221742    def _bindInh 
    17231743        base._bindInh 
    1724         _superType = .compiler.enumerableOfType.constructedTypeFor([.theWrappedType]) 
    1725  
    1726     def memberForName(name as String) as IMember? 
    1727         member = base.memberForName(name) 
    1728         if member is nil 
    1729             if _box is nil 
    1730                 # TODO: make members based on System.Array 
    1731                 _box = Class(Token.empty, Token.empty, '[.getType.name]_[.serialNum]', List<of IType>(), List<of String>(), AttributeList(), nil, List<of ITypeProxy>(), List<of ITypeProxy>(), nil) 
    1732                 indexer = Indexer(Token.empty, Token.empty, _box, r'[]', [Param(Token('', 1, 1, 1, 'ID', 'index', nil), .compiler.intType)], _wrappedType, List<of String>(), AttributeList(), '') 
    1733                 _box.addDecl(indexer) 
    1734                 lengthProp = Property(Token.empty, Token.empty, _box, 'length', .compiler.intType, List<of String>(), AttributeList(), '') 
    1735                 _box.addDecl(lengthProp) 
    1736                 _box.bindAll 
    1737             member = _box.symbolForName(name, true) 
    1738         return member 
     1744        if .compiler  # will be nil during unit tests 
     1745            iclone = .compiler.libraryType('System.ICloneable') 
     1746            icoll = (.compiler.libraryType('System.Collections.Generic.ICollection<of>') to Box).constructedTypeFor([.theWrappedType]) 
     1747            ienum = .compiler.enumerableOfType.constructedTypeFor([.theWrappedType]) 
     1748            # TODO: ilist = .compiler.ilistOfType.constructedTypeFor([.theWrappedType]) 
     1749            interfaces = [iclone to ITypeProxy, icoll, ienum]  # TODO:, ilist]  -- problems with cobra -ert:yes hello and extension method String.split 
     1750            _box = Class(Token.empty, Token.empty, '[.getType.name]_[.serialNum]', List<of IType>(), List<of String>(), AttributeList(), nil, interfaces, List<of ITypeProxy>(), nil) 
     1751            # TODO: make members based on System.Array 
     1752            indexer = Indexer(Token.empty, Token.empty, _box, r'[]', [Param(Token('', 1, 1, 1, 'ID', 'index', nil), .compiler.intType)], _wrappedType, List<of String>(), AttributeList(), '') 
     1753            _box.addDecl(indexer) 
     1754            lengthProp = Property(Token.empty, Token.empty, _box, 'length', .compiler.intType, List<of String>(), AttributeList(), '') 
     1755            _box.addDecl(lengthProp) 
     1756            _box.bindInh 
     1757            _superType = _box 
     1758 
     1759    def _bindInt 
     1760        base._bindInt 
     1761        _box.bindInt 
     1762     
     1763    def _bindImp 
     1764        base._bindImp 
     1765        _box.bindImp 
    17391766 
    17401767 
  • cobra/trunk/Tests/700-command-line/112-run-hello.cobra

    r1886 r2310  
    3939 
    4040            assert p.exitCode == 0 
    41             assert output.trim == 'Hello' 
     41            assert output.trim == 'Hello', args 
    4242 
    4343        def flush