Changeset 1719

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

Code cleanup.

Location:
cobra/trunk/Source
Files:
2 modified

Legend:

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

    r1718 r1719  
    832832                                return false 
    833833 
    834         def readAssembly(ass as Assembly) 
    835                 .readAssembly(ass, false) 
    836  
    837         def readAssembly(ass as Assembly, skipCobra as bool) 
    838                 """ 
    839                 Reads the contents of an assembly (.DLL) so that they are accessible to the program. 
    840                 In other words, this method reads libraries. 
    841                 """ 
    842                 verbosity = .verbosity 
    843                 if verbosity 
    844                         print 'Reading assembly:  [ass] at [ass.location]'  # extra space lines up with 'Loading reference:' 
    845                 namespaceQualNameToNameSpaceObject = Dictionary<of String, NameSpace>() 
    846                 module = AssemblyModule(ass, .globalNS) 
    847                 _curModule = module 
    848                 try 
    849                         _modules.add(module) 
    850                         skipCobra = false 
    851                         for type in ass.getExportedTypes 
    852                                 if skipCobra and type.namespace and (type.namespace == 'Cobra' or type.namespace.startsWith('Cobra.')) 
    853                                         # this is a huge hack to get around the repetition of the Cobra run-time support which gets embedded in both programs and libraries 
    854                                         # TODO: this should go away 
    855                                         continue 
    856 # delme or at least don't run on .NET 
    857 #                               if '__CompilerGenerated' in type.name  # TODO: does that work in .NET? 
    858 #                                       continue 
    859                                 if type.isNested or type.declaringType 
    860                                         # these will be scanned by Box._scanNestedTypes 
    861                                         # print '### skipping [type.name] in [type.namespace]. isNested=[type.isNested], declaringType=[type.declaringType]' 
    862                                         continue 
    863                                 typeNamespace = type.namespace 
    864                                 if typeNamespace is nil or typeNamespace.length == 0 
    865                                         # happens for classes etc. that are not declared in a namespace 
    866                                         curNameSpace = module.topNameSpace 
    867                                 else 
    868                                         namespaceName = typeNamespace to ! 
    869                                         if namespaceQualNameToNameSpaceObject.containsKey(namespaceName) 
    870                                                 curNameSpace = namespaceQualNameToNameSpaceObject[namespaceName] 
    871                                         else 
    872                                                 curNameSpace = module.topNameSpace 
    873                                                 for name in typeNamespace.split(c'.') 
    874                                                         curNameSpace = curNameSpace.getOrMakeNameSpaceNamed(Token.empty, name) 
    875                                                         assert not curNameSpace.isUnified 
    876                                                 namespaceQualNameToNameSpaceObject[namespaceName] = curNameSpace 
    877                                 if verbosity >= 4 
    878                                         print '  Reading type [type.name] in namespace "[namespaceName]"' 
    879                                 clrType = ClrNativeType(type) 
    880                                 if type.isClass 
    881                                         if type.name.startsWith('Extend_') and Utils.countChars(type.name, c'_') >= 2 
    882                                                 curNameSpace.addDecl(Extension(clrType)) 
    883                                         else 
    884                                                 curNameSpace.addDecl(Class(clrType)) 
    885                                 else if type.isInterface 
    886                                         curNameSpace.addDecl(Interface(clrType)) 
    887                                 else if type.isEnum 
    888                                         curNameSpace.addDecl(EnumDecl(curNameSpace, clrType, List<of String>(), ''))  # TODO: isNames; docString? 
    889                                 else if type.isValueType 
    890                                         curNameSpace.addDecl(Struct(clrType)) 
    891                                 else if type.isAnsiClass 
    892                                         # The Enum class is an example that returns false for .isClass but true for .isAnsiClass 
    893                                         curNameSpace.addDecl(Class(clrType)) 
    894                                 else 
    895                                         throw FallThroughException(type) 
    896                 finally 
    897                         _curModule = nil 
    898  
    899         def fixNilableMemberSigs 
    900                 # TODO: this really needs to go in a separate file that the compiler reads each time 
    901  
    902                 # TODO: look to see if what the Spec# team put together can be leveraged instead of recreating all this work! 
    903                 # fix up member sigs regarding nilable 
    904                 # hard coded below. TODO: read from a Cobra config file 
    905                 _fix('System.Object', 'toString getType memberwiseClone') 
    906                         # ^ regarding .toString, not technically true, but common enough and life is too painful when the return type is nilable 
    907                 _fix('System.Console', 'out') 
    908                 _fix('System.String', 'padLeft padRight replace substring toLower toUpper trim') 
    909                 _fix('System.Type', 'assembly name toString') 
    910                         # namespace can return nil if the Type is a generic parameter 
    911                 _fix('System.Environment', 'commandLine currentDirectory newLine version') 
    912                 _fix('System.Exception', 'message') 
    913                 _fix('System.Collections.Generic.IEnumerable<of>', r'getEnumerator') 
    914                 _fix('System.Collections.Generic.IList<of>', r'[] getRange toArray') 
    915                 _fix('System.Collections.Generic.List<of>', r'[]') 
    916                 _fix('System.Collections.Generic.IDictionary<of,>', r'[] keys values') 
    917                 _fix('System.Collections.Generic.Dictionary<of,>', r'[]') 
    918                 _fix('System.Collections.Generic.KeyValuePair<of,>', r'key value') 
    919                 _fix('System.IO.File', 'create createText open openRead openText openWrite readAllBytes readAllLines readAllText') 
    920                 _fix('System.IO.FileSystemInfo', 'name fullName') 
    921                 _fix('System.IO.TextWriter', 'newLine') 
    922                 _fix('System.IO.Path', 'combine getFullPath') 
    923                         # getDirectoryName does return String? 
    924                         # getFileName does return String? 
    925                         # TODO: Add something like CobraUtils.getDirectoryName and .getFileName that return String instead 
    926                 # args: System.IO.Path.combine(arg1 as String, arg2 as String) as String 
    927                 _fix('System.Text.StringBuilder', 'toString') 
    928                 _fix('System.Text.RegularExpressions.Regex', 'match replace') 
    929                 _fix('System.Diagnostics.Process', 'processName') 
    930                 _fix('System.Reflection.Assembly', 'getEntryAssembly getExecutingAssembly location') 
    931                 _fix('System.Reflection.MemberInfo', 'name') 
    932                 _fix('System.Reflection.FieldInfo', 'fieldType') 
    933                 _fix('System.Reflection.ParameterInfo', 'parameterType') 
    934                 _fix('System.Reflection.PropertyInfo', 'propertyType') 
    935  
    936                 # TODO: I don't think sigs outside the "standard lib" can be specified 
    937                 # HttpUtility.htmlEncode htmlDecode urlEncode urlDecode 
    938  
    939                 # TODO: shouldn't need the following. see comment in _fixSubs 
    940                 _fix('System.IO.StringWriter', 'toString') 
    941  
    942         def _fix(className as String, memberNames as String) 
    943                 type = .libraryType(className) to ? 
    944                 if type is nil 
    945                         print 'WARNING: Cannot find [className].'  # TODO: make a real warning 
    946                 else if type inherits Box 
    947                         type.membersToUnNil = memberNames 
    948                 else 
    949                         print 'WARNING: Cannot fix [className] which is not a class/struct/interface. (type=[type])'  # TODO: make a real warning 
    950  
    951834        ## 
    952835        ## Binding 
  • cobra/trunk/Source/ScanClrType.cobra

    r1718 r1719  
     1class Compiler 
     2        is partial 
     3 
     4        def readAssembly(ass as Assembly) 
     5                .readAssembly(ass, false) 
     6 
     7        def readAssembly(ass as Assembly, skipCobra as bool) 
     8                """ 
     9                Reads the contents of an assembly (.DLL) so that they are accessible to the program. 
     10                In other words, this method reads libraries. 
     11                """ 
     12                verbosity = .verbosity 
     13                if verbosity 
     14                        print 'Reading assembly:  [ass] at [ass.location]'  # extra space lines up with 'Loading reference:' 
     15                namespaceQualNameToNameSpaceObject = Dictionary<of String, NameSpace>() 
     16                module = AssemblyModule(ass, .globalNS) 
     17                _curModule = module 
     18                try 
     19                        _modules.add(module) 
     20                        skipCobra = false 
     21                        for type in ass.getExportedTypes 
     22                                if skipCobra and type.namespace and (type.namespace == 'Cobra' or type.namespace.startsWith('Cobra.')) 
     23                                        # this is a huge hack to get around the repetition of the Cobra run-time support which gets embedded in both programs and libraries 
     24                                        # TODO: this should go away 
     25                                        continue 
     26# delme or at least don't run on .NET 
     27#                               if '__CompilerGenerated' in type.name  # TODO: does that work in .NET? 
     28#                                       continue 
     29                                if type.isNested or type.declaringType 
     30                                        # these will be scanned by Box._scanNestedTypes 
     31                                        # print '### skipping [type.name] in [type.namespace]. isNested=[type.isNested], declaringType=[type.declaringType]' 
     32                                        continue 
     33                                typeNamespace = type.namespace 
     34                                if typeNamespace is nil or typeNamespace.length == 0 
     35                                        # happens for classes etc. that are not declared in a namespace 
     36                                        curNameSpace = module.topNameSpace 
     37                                else 
     38                                        namespaceName = typeNamespace to ! 
     39                                        if namespaceQualNameToNameSpaceObject.containsKey(namespaceName) 
     40                                                curNameSpace = namespaceQualNameToNameSpaceObject[namespaceName] 
     41                                        else 
     42                                                curNameSpace = module.topNameSpace 
     43                                                for name in typeNamespace.split(c'.') 
     44                                                        curNameSpace = curNameSpace.getOrMakeNameSpaceNamed(Token.empty, name) 
     45                                                        assert not curNameSpace.isUnified 
     46                                                namespaceQualNameToNameSpaceObject[namespaceName] = curNameSpace 
     47                                if verbosity >= 4 
     48                                        print '  Reading type [type.name] in namespace "[namespaceName]"' 
     49                                clrType = ClrNativeType(type) 
     50                                if type.isClass 
     51                                        if type.name.startsWith('Extend_') and Utils.countChars(type.name, c'_') >= 2 
     52                                                curNameSpace.addDecl(Extension(clrType)) 
     53                                        else 
     54                                                curNameSpace.addDecl(Class(clrType)) 
     55                                else if type.isInterface 
     56                                        curNameSpace.addDecl(Interface(clrType)) 
     57                                else if type.isEnum 
     58                                        curNameSpace.addDecl(EnumDecl(curNameSpace, clrType, List<of String>(), ''))  # TODO: isNames; docString? 
     59                                else if type.isValueType 
     60                                        curNameSpace.addDecl(Struct(clrType)) 
     61                                else if type.isAnsiClass 
     62                                        # The Enum class is an example that returns false for .isClass but true for .isAnsiClass 
     63                                        curNameSpace.addDecl(Class(clrType)) 
     64                                else 
     65                                        throw FallThroughException(type) 
     66                finally 
     67                        _curModule = nil 
     68 
     69        def fixNilableMemberSigs 
     70                # TODO: this really needs to go in a separate file that the compiler reads each time 
     71 
     72                # TODO: look to see if what the Spec# team put together can be leveraged instead of recreating all this work! 
     73                # fix up member sigs regarding nilable 
     74                # hard coded below. TODO: read from a Cobra config file 
     75                _fix('System.Object', 'toString getType memberwiseClone') 
     76                        # ^ regarding .toString, not technically true, but common enough and life is too painful when the return type is nilable 
     77                _fix('System.Console', 'out') 
     78                _fix('System.String', 'padLeft padRight replace substring toLower toUpper trim') 
     79                _fix('System.Type', 'assembly name toString') 
     80                        # namespace can return nil if the Type is a generic parameter 
     81                _fix('System.Environment', 'commandLine currentDirectory newLine version') 
     82                _fix('System.Exception', 'message') 
     83                _fix('System.Collections.Generic.IEnumerable<of>', r'getEnumerator') 
     84                _fix('System.Collections.Generic.IList<of>', r'[] getRange toArray') 
     85                _fix('System.Collections.Generic.List<of>', r'[]') 
     86                _fix('System.Collections.Generic.IDictionary<of,>', r'[] keys values') 
     87                _fix('System.Collections.Generic.Dictionary<of,>', r'[]') 
     88                _fix('System.Collections.Generic.KeyValuePair<of,>', r'key value') 
     89                _fix('System.IO.File', 'create createText open openRead openText openWrite readAllBytes readAllLines readAllText') 
     90                _fix('System.IO.FileSystemInfo', 'name fullName') 
     91                _fix('System.IO.TextWriter', 'newLine') 
     92                _fix('System.IO.Path', 'combine getFullPath') 
     93                        # getDirectoryName does return String? 
     94                        # getFileName does return String? 
     95                        # TODO: Add something like CobraUtils.getDirectoryName and .getFileName that return String instead 
     96                # args: System.IO.Path.combine(arg1 as String, arg2 as String) as String 
     97                _fix('System.Text.StringBuilder', 'toString') 
     98                _fix('System.Text.RegularExpressions.Regex', 'match replace') 
     99                _fix('System.Diagnostics.Process', 'processName') 
     100                _fix('System.Reflection.Assembly', 'getEntryAssembly getExecutingAssembly location') 
     101                _fix('System.Reflection.MemberInfo', 'name') 
     102                _fix('System.Reflection.FieldInfo', 'fieldType') 
     103                _fix('System.Reflection.ParameterInfo', 'parameterType') 
     104                _fix('System.Reflection.PropertyInfo', 'propertyType') 
     105 
     106                # TODO: I don't think sigs outside the "standard lib" can be specified 
     107                # HttpUtility.htmlEncode htmlDecode urlEncode urlDecode 
     108 
     109                # TODO: shouldn't need the following. see comment in _fixSubs 
     110                _fix('System.IO.StringWriter', 'toString') 
     111 
     112        def _fix(className as String, memberNames as String) 
     113                type = .libraryType(className) to ? 
     114                if type is nil 
     115                        print 'WARNING: Cannot find [className].'  # TODO: make a real warning 
     116                else if type inherits Box 
     117                        type.membersToUnNil = memberNames 
     118                else 
     119                        print 'WARNING: Cannot fix [className] which is not a class/struct/interface. (type=[type])'  # TODO: make a real warning 
     120 
     121 
    1122class Box 
    2123        is partial