Changeset 1718

Show
Ignore:
Timestamp:
11/01/08 04:12:37 (2 months ago)
Author:
Chuck.Esterbrook
Message:

Code cleanup.
Related to cleaning up access to native types,
in anticipation of other backends.

Rename ClrType? to ClrNativeType? to reflect its inheritance and help distinguish it from other types such System.Type, IntegerType?, etc.
Require that subclasses of NativeType? override .equals and .getHashCode.
Replace various uses of System.Type with NativeType?.

Location:
cobra/trunk/Source
Files:
8 modified

Legend:

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

    r1710 r1718  
    979979                base.init(TokenFix.empty, ClrTypeProxy.cobraNameForSharpBoxName(nativeType.name), List<of IType>(), List<of String>(), AttributeList(), List<of ITypeProxy>(), nil) 
    980980                if nativeType.baseType 
    981                         _baseNode = ClrTypeProxy(nativeType.baseType to ClrType)  # TODO: fix native. Change to NativeTypeProxy 
     981                        _baseNode = ClrTypeProxy(nativeType.baseType to ClrNativeType)  # TODO: fix native. Change to NativeTypeProxy 
    982982                _initNativeType(nativeType) 
    983983 
     
    14631463                # the real extended type is not this type, but the type of the first argument of any method 
    14641464                for methInfo in .clrType.getMethods  # TODO: fix native. break this out to a method 
    1465                         nativeType = ClrType(methInfo.getParameters[0].parameterType) 
     1465                        nativeType = ClrNativeType(methInfo.getParameters[0].parameterType) 
    14661466                        break 
    14671467                _extendedBoxProxy = ClrTypeProxy(nativeType) 
  • cobra/trunk/Source/ClrType.cobra

    r1671 r1718  
    1 class ClrType 
     1class ClrNativeType 
    22        inherits NativeType 
    33 
     
    77                _type = type 
    88 
    9         get theClrType as Type 
     9        def equals(other as Object) as bool is override 
     10                if other is this 
     11                        return true 
     12                if other inherits ClrNativeType 
     13                        return .clrType == other.clrType 
     14                else 
     15                        return false 
     16 
     17        def getHashCode as int is override 
     18                return .clrType.getHashCode 
     19 
     20        get clrType as Type 
    1021                return _type 
    1122 
     
    2031 
    2132        get baseType as NativeType? is override 
    22                 return if(_type.baseType, ClrType(_type.baseType), nil) 
     33                return if(_type.baseType, ClrNativeType(_type.baseType), nil) 
    2334 
    2435 
  • cobra/trunk/Source/Compiler.cobra

    r1710 r1718  
    877877                                if verbosity >= 4 
    878878                                        print '  Reading type [type.name] in namespace "[namespaceName]"' 
    879                                 clrType = ClrType(type) 
     879                                clrType = ClrNativeType(type) 
    880880                                if type.isClass 
    881881                                        if type.name.startsWith('Extend_') and Utils.countChars(type.name, c'_') >= 2 
     
    13361336                        return .codeMemberStack.peek.findLocal(name) 
    13371337 
    1338         def clrType(qualifiedName as String) as System.Type 
     1338        def nativeType(qualifiedName as String) as NativeType 
     1339                return ClrNativeType(_clrType(qualifiedName)) 
     1340 
     1341        def _clrType(qualifiedName as String) as System.Type 
    13391342                # eventually this method will be key in targeting different versions of the CLR 
    13401343                # suppose you are on .NET 3.0 and want to target .NET 2.0 
  • cobra/trunk/Source/Enums.cobra

    r1703 r1718  
    2424                _attribs = AttributeList() 
    2525                _nativeType = nativeType 
    26                 _storageTypeNode = ClrTypeProxy(Enum.getUnderlyingType((_nativeType to ClrType).theClrType))  # TODO: fix native 
     26                _storageTypeNode = ClrTypeProxy(Enum.getUnderlyingType((_nativeType to ClrNativeType).clrType))  # TODO: fix native 
    2727                _needScanNativeType = true 
    2828 
     
    3030                # TODO: read attribs 
    3131                _needScanNativeType = false 
    32                 clrType = (_nativeType to ClrType).theClrType # TODO: fix native 
     32                clrType = (_nativeType to ClrNativeType).clrType # TODO: fix native 
    3333                isByte  = Enum.getUnderlyingType(clrType).name == 'Byte' 
    3434                is64    = Enum.getUnderlyingType(clrType).name == 'Int64' 
  • cobra/trunk/Source/ScanClrType.cobra

    r1671 r1718  
    77                Throws exception rather than return nil. 
    88                """ 
    9                 return (.nativeType to ClrType).theClrType 
     9                return (.nativeType to ClrNativeType).clrType 
    1010 
    1111        def _scanNativeType 
     
    3030                if .clrType.isGenericType 
    3131                        for genArg in .clrType.getGenericArguments 
    32                                 _genericParams.add(GenericParam(ClrType(genArg))) 
     32                                _genericParams.add(GenericParam(ClrNativeType(genArg))) 
    3333 
    3434        def _scanImplements 
     
    3939        def _scanNestedTypes 
    4040                for type in .clrType.getNestedTypes 
    41                         clrType = ClrType(type) 
     41                        clrType = ClrNativeType(type) 
    4242                        if type.isClass 
    4343                                .addDecl(Class(clrType)) 
     
    200200                        genericParams = List<of IType>() 
    201201                        for genArg in methInfo.getGenericArguments 
    202                                 genericParams.add(GenericParam(ClrType(genArg))) 
     202                                genericParams.add(GenericParam(ClrNativeType(genArg))) 
    203203                        params = _scanParams(methInfo.getParameters) 
    204204                        isNames = _isNamesForMethodInfo(methInfo) 
  • cobra/trunk/Source/SharpGenerator.cobra

    r1716 r1718  
    727727 
    728728        get sharpName as String is override 
    729                 return _clrType.name 
     729                return _nativeType.name 
    730730 
    731731 
     
    734734 
    735735        get sharpName as String is override 
    736                 return _clrType.name 
     736                return _nativeType.name 
    737737 
    738738 
  • cobra/trunk/Source/TypeProxies.cobra

    r1681 r1718  
    141141 
    142142        def init(nativeType as NativeType)  # TODO: HACK 
    143                 .init((nativeType to ClrType).theClrType) 
     143                .init((nativeType to ClrNativeType).clrType) 
    144144 
    145145        def init(clrType as Type) 
     
    165165                        for bt in .compiler.basicTypes 
    166166                                if bt.systemAliasProxy 
    167                                         key = .compiler.clrType((bt.systemAliasProxy to LibraryTypeProxy).qualifiedName) 
     167                                        key = (.compiler.nativeType((bt.systemAliasProxy to LibraryTypeProxy).qualifiedName) to ClrNativeType).clrType # TODO: cleanup 
    168168                                        clrPrimitiveToIType[key] = bt 
    169169                        assert clrPrimitiveToIType.count == 0 or clrPrimitiveToIType.count > 9 
     
    201201                # generic parameters 
    202202                if clrType.isGenericParameter 
    203                         return GenericParam(ClrType(clrType)) 
     203                        return GenericParam(ClrNativeType(clrType)) 
    204204 
    205205                # compute type name 
  • cobra/trunk/Source/Types.cobra

    r1707 r1718  
    4444                """ 
    4545 
     46        def equals(other as Object) as bool is override 
     47                throw Exception('Subclasses must override .equals') 
     48 
     49        def getHashCode as int is override 
     50                throw Exception('Subclasses must override .getHashCode') 
     51 
    4652 
    4753interface ITypeProvider 
     
    99105        def libraryType(qualifiedName as String) as IType 
    100106 
    101         def clrType(qualifiedName as String) as System.Type 
     107        def nativeType(qualifiedName as String) as NativeType 
    102108 
    103109        def defaultType as IType 
     
    230236 
    231237        def libraryType(qualifiedName as String) as IType 
    232                 clrType = $sharp('Type.GetType(qualifiedName)') to Type? 
    233                 return BasicLibraryType(clrType) 
    234  
    235         def clrType(qualifiedName as String) as System.Type 
    236                 return sharp'Type.GetType(qualifiedName)' to System.Type 
     238                return BasicLibraryType(.nativeType(qualifiedName)) 
     239 
     240        def nativeType(qualifiedName as String) as NativeType 
     241                return ClrNativeType(sharp'Type.GetType(qualifiedName)' to System.Type) 
    237242 
    238243        def defaultType as IType 
     
    254259                 
    255260                def systemObjectType as BasicLibraryType 
    256                         return BasicLibraryType($sharp('typeof(System.Object)')) 
    257  
    258         var _clrType as Type 
    259  
    260         def init(clrType as Type) 
    261                 _clrType = clrType 
     261                        return BasicLibraryType(ClrNativeType(sharp'typeof(System.Object)')) 
     262 
     263        var _nativeType as NativeType 
     264 
     265        def init(nativeType as NativeType) 
     266                _nativeType = nativeType 
    262267 
    263268        def toString as String is override 
    264                 return '[.getType.name]([.clrType])' 
     269                return '[.getType.name]([.nativeType])' 
    265270 
    266271        def equals(other as Object?) as bool is override 
    267272                if other 
    268273                        if other inherits BasicLibraryType 
    269                                 return .clrType == other.clrType 
     274                                return .nativeType == other.nativeType 
    270275                        else 
    271276                                return false 
     
    274279 
    275280        def getHashCode as int is override 
    276                 return _clrType.getHashCode 
    277  
    278         get clrType from var 
     281                return _nativeType.getHashCode 
     282 
     283        get nativeType from var 
    279284 
    280285        ## IType Properties 
     
    295300 
    296301        get isSystemObjectClass as bool 
    297                 return _clrType.name == 'Object' 
     302                return _nativeType.name == 'Object' 
    298303 
    299304        get isSystemTypeClass as bool 
    300                 return _clrType.name == 'Type' 
     305                return _nativeType.name == 'Type' 
    301306 
    302307        def isAssignableTo(t as IType) as bool 
     
    313318 
    314319        get isReference as bool 
    315                 return not _clrType.isValueType 
     320                return not _nativeType.isValueType 
    316321 
    317322        def isStrictDescendantOf(t as IType) as bool 
     
    413418         
    414419        get name as String 
    415                 return .clrType.name 
     420                return .nativeType.name 
    416421 
    417422        get typeForIdentifier as IType 
     
    788793        var _systemAliasType as IType? 
    789794 
    790         var _clrType as Type? 
     795        var _nativeType as NativeType? 
    791796 
    792797        var _box as Box? 
     
    799804                base.init(superType) 
    800805 
    801         get clrType from var 
     806        get nativeType from var 
    802807 
    803808        get systemAliasProxy from var 
     
    821826                #       _systemAliasType.bindInt 
    822827 
    823                 if .compiler and _clrType 
     828                if .compiler and _nativeType 
    824829                        meths = List<of Method>() 
    825                         _installNativeMethodsFrom(_clrType to !, meths) 
    826                         _installNativeMethodsFrom(System.Math.getType, meths) 
     830                        _installNativeMethodsFrom(_nativeType to !, meths) 
     831                        _installNativeMethodsFrom(ClrNativeType(System.Math.getType), meths) 
    827832                        _box.bindInt 
    828833                        if false 
     
    843848                                                print '    [meth.cobraSourceSignature]' 
    844849 
    845         def _installNativeMethodsFrom(clrType as Type, meths as List<of Method>) 
     850        def _installNativeMethodsFrom(nativeType as NativeType, meths as List<of Method>) 
     851                argClrType = (nativeType to ClrNativeType).clrType 
     852                thisClrType = (_nativeType to ClrNativeType).clrType 
    846853                box = _box 
    847854                # print 
    848855                # print '** [.name], clrType' 
    849                 isSameNativeType = clrType == _clrType 
    850                 for methInfo in clrType.getMethods(BindingFlags(DeclaredOnly, Static, Instance, Public)) 
     856                isSameNativeType = argClrType == thisClrType 
     857                for methInfo in argClrType.getMethods(BindingFlags(DeclaredOnly, Static, Instance, Public)) 
    851858                        if methInfo.isSpecialName, continue 
    852859                        # print 
     
    862869                                        first = false 
    863870                                        if methInfo.isStatic 
    864                                                 if methInfo.getParameters[0].parameterType == _clrType 
     871                                                if methInfo.getParameters[0].parameterType == thisClrType 
    865872                                                        # When the first arg of the shared method is the same type, 
    866873                                                        # then make an instance method that can be used directly on values of the type. 
     
    882889                        meth = Method(Token.empty, box, name, newParams, ClrTypeProxy(methInfo.returnType), nil, modifiers, AttributeList(), '') 
    883890                        if methInfo.isStatic 
    884                                 meth.sharedMethodBacking = clrType.name + '.' + methInfo.name 
     891                                meth.sharedMethodBacking = argClrType.name + '.' + methInfo.name 
    885892                                if 'shared' in modifiers, meth.sharedMethodBackingIsAlias = true 
    886893 
     
    938945        def init 
    939946                base.init 
    940                 _clrType = .typeProvider.clrType('System.Boolean') 
     947                _nativeType = .typeProvider.nativeType('System.Boolean') 
    941948                _systemAliasProxy = LibraryTypeProxy('System.Boolean') 
    942949 
     
    951958        def init 
    952959                base.init 
    953                 _clrType = .typeProvider.clrType('System.Char') 
     960                _nativeType = .typeProvider.nativeType('System.Char') 
    954961                _systemAliasProxy = LibraryTypeProxy('System.Char') 
    955962 
     
    975982        def init 
    976983                base.init 
    977                 _clrType = .typeProvider.clrType('System.Decimal') 
     984                _nativeType = .typeProvider.nativeType('System.Decimal') 
    978985                _systemAliasProxy = LibraryTypeProxy('System.Decimal') 
    979986 
     
    10751082                        _size = size 
    10761083                        branch size 
    1077                                 on 32, _clrType = .typeProvider.clrType('System.Single') 
    1078                                 on 64, _clrType = .typeProvider.clrType('System.Double') 
     1084                                on 32, _nativeType = .typeProvider.nativeType('System.Single') 
     1085                                on 64, _nativeType = .typeProvider.nativeType('System.Double') 
    10791086                                else, throw FallThroughException(size) 
    1080                         _systemAliasProxy = LibraryTypeProxy(_clrType.fullName) 
     1087                        _systemAliasProxy = LibraryTypeProxy(_nativeType.fullName) 
    10811088 
    10821089        def addMinFields 
     
    11881195                        if signed 
    11891196                                branch size 
    1190                                         on  8, _clrType = .typeProvider.clrType('System.SByte') 
    1191                                         on 16, _clrType = .typeProvider.clrType('System.Int16') 
    1192                                         on 32, _clrType = .typeProvider.clrType('System.Int32') 
    1193                                         on 64, _clrType = .typeProvider.clrType('System.Int64') 
     1197                                        on  8, _nativeType = .typeProvider.nativeType('System.SByte') 
     1198                                        on 16, _nativeType = .typeProvider.nativeType('System.Int16') 
     1199                                        on 32, _nativeType = .typeProvider.nativeType('System.Int32') 
     1200                                        on 64, _nativeType = .typeProvider.nativeType('System.Int64') 
    11941201                                        else: throw FallThroughException(size) 
    11951202                        else 
    11961203                                branch size 
    1197                                         on  8, _clrType = .typeProvider.clrType('System.Byte') 
    1198                                         on 16, _clrType = .typeProvider.clrType('System.UInt16') 
    1199                                         on 32, _clrType = .typeProvider.clrType('System.UInt32') 
    1200                                         on 64, _clrType = .typeProvider.clrType('System.UInt64') 
     1204                                        on  8, _nativeType = .typeProvider.nativeType('System.Byte') 
     1205                                        on 16, _nativeType = .typeProvider.nativeType('System.UInt16') 
     1206                                        on 32, _nativeType = .typeProvider.nativeType('System.UInt32') 
     1207                                        on 64, _nativeType = .typeProvider.nativeType('System.UInt64') 
    12011208                                        else, throw FallThroughException(size) 
    1202                         _systemAliasProxy = LibraryTypeProxy(_clrType.fullName) 
     1209                        _systemAliasProxy = LibraryTypeProxy(_nativeType.fullName) 
    12031210 
    12041211        def addMinFields