Changeset 1719
- Timestamp:
- 11/01/08 04:31:00 (2 months ago)
- Location:
- cobra/trunk/Source
- Files:
-
- 2 modified
-
Compiler.cobra (modified) (1 diff)
-
ScanClrType.cobra (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
cobra/trunk/Source/Compiler.cobra
r1718 r1719 832 832 return false 833 833 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 = .verbosity843 if verbosity844 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 = module848 try849 _modules.add(module)850 skipCobra = false851 for type in ass.getExportedTypes852 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 libraries854 # TODO: this should go away855 continue856 # delme or at least don't run on .NET857 # if '__CompilerGenerated' in type.name # TODO: does that work in .NET?858 # continue859 if type.isNested or type.declaringType860 # these will be scanned by Box._scanNestedTypes861 # print '### skipping [type.name] in [type.namespace]. isNested=[type.isNested], declaringType=[type.declaringType]'862 continue863 typeNamespace = type.namespace864 if typeNamespace is nil or typeNamespace.length == 0865 # happens for classes etc. that are not declared in a namespace866 curNameSpace = module.topNameSpace867 else868 namespaceName = typeNamespace to !869 if namespaceQualNameToNameSpaceObject.containsKey(namespaceName)870 curNameSpace = namespaceQualNameToNameSpaceObject[namespaceName]871 else872 curNameSpace = module.topNameSpace873 for name in typeNamespace.split(c'.')874 curNameSpace = curNameSpace.getOrMakeNameSpaceNamed(Token.empty, name)875 assert not curNameSpace.isUnified876 namespaceQualNameToNameSpaceObject[namespaceName] = curNameSpace877 if verbosity >= 4878 print ' Reading type [type.name] in namespace "[namespaceName]"'879 clrType = ClrNativeType(type)880 if type.isClass881 if type.name.startsWith('Extend_') and Utils.countChars(type.name, c'_') >= 2882 curNameSpace.addDecl(Extension(clrType))883 else884 curNameSpace.addDecl(Class(clrType))885 else if type.isInterface886 curNameSpace.addDecl(Interface(clrType))887 else if type.isEnum888 curNameSpace.addDecl(EnumDecl(curNameSpace, clrType, List<of String>(), '')) # TODO: isNames; docString?889 else if type.isValueType890 curNameSpace.addDecl(Struct(clrType))891 else if type.isAnsiClass892 # The Enum class is an example that returns false for .isClass but true for .isAnsiClass893 curNameSpace.addDecl(Class(clrType))894 else895 throw FallThroughException(type)896 finally897 _curModule = nil898 899 def fixNilableMemberSigs900 # TODO: this really needs to go in a separate file that the compiler reads each time901 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 nilable904 # hard coded below. TODO: read from a Cobra config file905 _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 nilable907 _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 parameter911 _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 instead926 # args: System.IO.Path.combine(arg1 as String, arg2 as String) as String927 _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 specified937 # HttpUtility.htmlEncode htmlDecode urlEncode urlDecode938 939 # TODO: shouldn't need the following. see comment in _fixSubs940 _fix('System.IO.StringWriter', 'toString')941 942 def _fix(className as String, memberNames as String)943 type = .libraryType(className) to ?944 if type is nil945 print 'WARNING: Cannot find [className].' # TODO: make a real warning946 else if type inherits Box947 type.membersToUnNil = memberNames948 else949 print 'WARNING: Cannot fix [className] which is not a class/struct/interface. (type=[type])' # TODO: make a real warning950 951 834 ## 952 835 ## Binding -
cobra/trunk/Source/ScanClrType.cobra
r1718 r1719 1 class 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 1 122 class Box 2 123 is partial
