Ticket #275: java-jvm-7.patch
File java-jvm-7.patch, 161.2 KB (added by hopscc, 12 years ago) |
---|
-
Source/BackEndObjC/ObjcBackEnd.cobra
47 47 #m = ObjCModule(filename, verbosity) 48 48 return m 49 49 50 get baseExeName as String is override 51 """Resulting executable file name for compiled file(s) sans extension.""" 52 return .compiler.clrBaseExeFileName 53 54 get fullExeName as String is override 55 """Resulting full executable file name (pathname+extn) for compiled file(s).""" 56 return .compiler.clrFullExeFileName 57 50 58 def setupRunProcess(baseExe as String, fullExe as String) as Process is override 51 59 p = Process() 52 p.startInfo.fileName = baseExe60 p.startInfo.fileName = fullExe 53 61 return p 54 62 55 63 def setDefaultUseDirectives(ns as NameSpace) is override … … 130 138 131 139 #def handleNameSpaceNameCollision(ns as NameSpace, token as IToken, name as String) as NameSpace is override 132 140 141 def getEnumeratorMember(box as Box) as IMember? is override 142 """ Find iterator method member in given Box.""" 143 return nil #box.getIteratorMemberObjc 133 144 145 def getEnumeratorMemberType(box as Box, rt as IType) as IType? is override 146 """Return type from lookup of iterator 'next' field in given Box.""" 147 return nil # box.getIteratorNextTypeObjc(rt) 148 149 def validatePrintDestType(t as IType, s as Stmt) is override 150 """Validate that the given Type is a valid destination for a print statement redirection.""" 151 pass 152 153 134 154 class GenerateObjcCodePhase inherits Phase 135 155 136 156 cue init(c as Compiler) -
Source/Compiler.cobra
55 55 Novell 56 56 57 57 58 class BackEnd is abstract59 """60 Holder for items specific to the backEnd implementation61 """62 63 var _tagToTypeName = Dictionary<of String, String>()64 """ Map from Typename tags used in compiler to backend specific qualified Type name"""65 66 cue init(compiler as Compiler)67 base.init68 _name = ''69 _runTimeLibFileName = 'rtl'70 _runTimeLibNativeSourceFileName = 'rtlSrc.ext'71 __compiler = compiler72 73 get name from var as String74 75 get cobraRuntimeLibFileName from _runTimeLibFileName as String76 """Name of the Cobra RunTime Library file for this backend."""77 78 get runTimeLibNativeSourceFileName from _runTimeLibNativeSourceFileName as String79 """Name of the backEnd source file containing any native code support for Cobra.Core."""80 81 get compiler from __compiler as Compiler82 83 get tagToTypeName from var84 """ Map from TypeName tags used in compiler to backend specific qualified TypeName"""85 86 def resolveTypeTag(qualifiedNameOrTag as String) as String87 if qualifiedNameOrTag.contains('.')88 qualifiedName = qualifiedNameOrTag89 else90 qualifiedName = .tagToTypeName[qualifiedNameOrTag] # type tag to backend qualifiedName91 return qualifiedName92 93 def makePhases(phases as IList<of Phase>) is abstract94 """95 Given a list of core phases for compilation complete it with additions (or even removals and96 rearrangements if necessary) for this given backend.97 """98 99 def getRecommendedBuiltInType(parentNameSpace as NameSpace?, name as String) as String? is abstract100 """101 If parentNameSpace is the main system namespace and name is a backEnd builtin type102 return the Cobra equivalent language type name (or nil).103 """104 105 def computeOutName as String is abstract106 """107 Return the binary file output name for compilation of files for this backend.108 """109 110 def genNativeModule(filename as String, verbosity as int) as Module? is abstract111 """112 Check if a filename is a Native module and if so generate and return the Native module type for it113 otherwise return nil.114 """115 116 def setupRunProcess(baseExe as String, fullExe as String) as Process is abstract117 """118 Create and initialise the process to run the compiled program post compilation119 setup varies on the backend (.Clr/Jvm) and platform.120 """121 122 def setDefaultUseDirectives(ns as NameSpace) is abstract123 """124 Set the default Use directives into the given Namespace (usually topNamespace) for this backend.125 """126 127 def fixLibExtension(libRef as String) as String is abstract128 """129 Augment given lib reference string with backend extension if not already have one.130 """131 132 def loadLibReference(reference as String) as String? is abstract133 """134 Load the given library reference file using the current backend paradigms.135 Returns an error message, or nil if the reference was loaded correctly.136 """137 138 def readSystemTypes is abstract139 """ Read and Load System Types for backend for Compiler to make available"""140 141 def fixMemberSigs is abstract142 """143 Most backends dont natively support nilablility, this marks/fixes the common backend144 class signatures that should be marked nilable.145 """146 147 def installNativeMethods(box as Box, nativeType as NativeType) is abstract148 """149 Setup so that static methods (on Primitives and some others) get installed/treated as normal methods150 on the instance.151 """152 153 def isRunnableFile( fullExeFileName as String) as bool is abstract154 """Test if given filename is an executable ( vs a library or something else) """155 156 # Native Types access157 def cobraNameForNativeBoxName(name as String) as String is abstract158 """159 Returns name from backend library entries converted to cobra naming form.160 """161 162 get objectTypeProxy as AbstractTypeProxy is abstract163 """BE TypeProxy for root of back end Object hierarchy."""164 165 get typeTypeProxy as AbstractTypeProxy is abstract166 """BE TypeProxy for BE notion of a class describing a Type."""167 168 def nativeTypeProxy(type as NativeType) as NativeTypeProxy is abstract169 """170 Return a Proxy placeHolder for a BackEnd Native Type.171 """172 173 def nativeType(type as dynamic) as NativeType is abstract174 """175 Return a Native Type wrapped so we can use it without explicitly knowing what it is anywhere176 else but the providing back end. Used by the backends to generate Types from Libraries.177 """178 179 def nativeTypeByName(qualifiedName as String) as NativeType is abstract180 """181 Return a Native Type corresponding to the fully qualified name.182 Abstract Type Literal tags used in the compiler directly can also be obtained through this183 otherwise the qualified names are expected to conform to the platform back end naming.184 """185 186 def prepSystemObjectClass(box as Box) is abstract187 """Setup additional or convenience members on the System Object class. """188 189 def scanGenericArgs(box as Box) is abstract190 """Scan a loaded Dll (generic) type and translate from native any generic Args."""191 192 def scanNativeType(box as Box) is abstract193 """Scan a loaded Dll type and convert its native Type info to Cobras form """194 195 def scanNativeType(edcl as EnumDecl) is abstract196 """Scan a loaded Dll Enum type and convert its native info to Cobras form """197 198 def setUnderlyingType(edcl as EnumDecl) is abstract199 """Set underlying Storage Type of the value of an Enum."""200 201 def determineExtnNativeType(extn as Extension, nativeType as NativeType) as NativeType202 """203 The real extended type is the type of the first argument of any method.204 Find and make that type from the param list.205 """206 throw Exception('Need implementation of determineExtnNativeType for backend to determine type for an extension')207 208 def handleNameSpaceNameCollision(ns as NameSpace, token as IToken, name as String) as NameSpace209 """210 What to do if a namespace name collides with an existing symbol.211 Some backends disallow this situation so its an error.212 Others keep namespace and symbol tables separate so its perfectly213 allowable.214 """215 throw Exception('In [ns.name] there is a already non-namespace declaration named "[name]".')216 217 218 class FakeBackEnd inherits BackEnd219 """ Stub BackEnd for tests. """220 221 cue init(compiler as Compiler)222 base.init(compiler)223 _name = 'c#-clr(basic)'224 _runTimeLibFileName = 'cobraRTL.libext'225 _runTimeLibNativeSourceFileName = 'cobraRTL.ext'226 227 _tagToTypeName = {228 'Object': 'System.Object',229 'Type' : 'System.Type',230 }231 232 def makePhases(phases as IList<of Phase>) is override233 pass234 235 def getRecommendedBuiltInType(parentNameSpace as NameSpace?, name as String) as String? is override236 return nil237 238 def computeOutName as String is override239 return 'FakeOut'240 241 def genNativeModule(filename as String, verbosity as int) as Module? is override242 return nil243 244 def setupRunProcess(baseExe as String, fullExe as String) as Process is override245 p = Process()246 p.startInfo.fileName = 'echo'247 p.startInfo.arguments = 'basic BackEnd process - [baseExe]'248 return p249 250 def setDefaultUseDirectives(ns as NameSpace) is override251 pass252 253 def fixLibExtension(libRef as String) as String is override254 if not libRef.endsWith('.libext')255 libRef += '.libext'256 return libRef257 258 def loadLibReference(reference as String) as String? is override259 return nil260 261 def readSystemTypes is override262 pass263 264 def fixMemberSigs is override265 pass266 267 def installNativeMethods(box as Box, nativeType as NativeType) is override268 pass269 270 def isRunnableFile(fullExeFileName as String) as bool is override271 return true272 273 # Native Type access274 def cobraNameForNativeBoxName(name as String) as String is override275 return name + '_BBE'276 277 def prepSystemObjectClass(box as Box) is override278 pass279 280 def scanGenericArgs(box as Box) is override281 pass282 283 def scanNativeType(box as Box) is override284 pass285 286 def scanNativeType(edcl as EnumDecl) is override287 pass288 289 def setUnderlyingType(edcl as EnumDecl) is override290 pass291 292 # Types293 get objectTypeProxy as AbstractTypeProxy is override294 return ClrTypeProxy(Object) # for testing295 296 get typeTypeProxy as AbstractTypeProxy is override297 return ClrTypeProxy(Type )# for testing298 299 def nativeTypeProxy(type as NativeType) as NativeTypeProxy is override300 return ClrTypeProxy(type) #TODO: fix this to something non BE specific Tmp301 302 def nativeType(type) as NativeType is override303 return ClrNativeType(type) #TODO: fix this to something non BE specific Tmp304 305 def nativeTypeByName(qualifiedName as String) as NativeType is override306 return ClrNativeType(System.Object) #TODO: fix this to something non BE specific Tmp307 308 309 58 class Compiler implements ITypeProvider, IWarningRecorder, IErrorRecorder, ICompilerForNodes is partial 310 59 """ 311 60 General notes: … … 346 95 var _intermediateFileNames as List<of String> 347 96 var _loadedReferences as List<of String> 348 97 349 350 98 # caches 351 99 var _primitiveToITypeCache as IDictionary<of dynamic, IType>? # Key is backend type for a Type ( e.g System.Type) 352 100 … … 405 153 Returns a cache mapping BackEnd types to their corresponding ITypes. 406 154 Populated and used by the backend TypeProxy implementations ( {Clr,Jvm,...}TypeProxy) 407 155 """ 156 408 157 set primitiveCache as IDictionary<of dynamic, IType> 409 158 assert value.count <> 0 410 159 _primitiveToITypeCache = value … … 502 251 pro linesCompiled from var as int 503 252 pro nodesCompiled from var as int 504 253 pro tokensCompiled from var as int 254 505 255 256 get baseExeFileName as String 257 """Returns the executable file name sans extension.""" 258 return .backEnd.baseExeName 259 260 get fullExeFileName as String 261 """Returns the executable file name path with extension.""" 262 return .backEnd.fullExeName 263 506 264 def recordError(error as SourceException) 507 265 """ 508 266 Node calls this to record errors. … … 1335 1093 1336 1094 def setOfType as Class 1337 1095 return _libraryClass('Set<of>') 1338 1096 1097 def keyValuePairOfType as Box 1098 return _libraryBox('KeyValuePair<of,>') 1099 1339 1100 def libraryType(qualifiedNameOrTag as String) as IType 1340 1101 """ 1341 1102 Implemented for ITypeProvider, but use the more specific methods such as .stringType instead. … … 1366 1127 thing as IContainer? = nil 1367 1128 for name in names 1368 1129 possible = (thing ? ns).declForName(name) 1369 #print name1370 1130 # dbg 1371 #if not possible 1372 # trace thing ? ns 1373 # ((thing ? ns) to NameSpace).dumpDeclsNameKeys 1131 if Utils.isDevMachine and not possible 1132 ctnr = (thing ? ns) 1133 print 'NAME_LOOKUP_FAILURE: Cannot find "[name]" in namespace/Container "[ctnr.name]"' 1134 trace name, ctnr 1135 (ctnr to NameSpace).dumpDeclsNameKeys 1374 1136 assert possible, name 1375 1137 if possible inherits IContainer 1376 1138 thing = possible … … 1409 1171 #special type proxies for common Types - Object and Type 1410 1172 def objectTypeProxy as AbstractTypeProxy 1411 1173 """ 1412 Type proxy for BEnotion of a class for the root of Object hierarchy1174 Type proxy for backEnd notion of a class for the root of Object hierarchy 1413 1175 e.g. Object in dotNet 1414 1176 """ 1415 #return ClrTypeProxy(Object)1416 1177 return .backEnd.objectTypeProxy 1417 1178 1418 1179 def typeTypeProxy as AbstractTypeProxy 1419 1180 """ 1420 Type proxy for BEnotion of a class for a Type1181 Type proxy for backEnd notion of a class for a Type 1421 1182 e.g. System.Type in dotNet, java.lang.class in Java 1422 1183 """ 1423 #return ClrTypeProxy(Type)1424 1184 return .backEnd.typeTypeProxy 1425 1185 1426 1186 def nativeType(qualifiedName as String) as NativeType -
Source/Expr.cobra
1019 1019 else 1020 1020 arg.recordError('Cannot find "[arg.name]" in enumeration "[.name]"') 1021 1021 1022 defn = _type to EnumDecl 1023 if _args.count > 1 and not defn.isUsedAsSet 1024 defn.isUsedAsSet = true 1025 _type = defn 1022 1026 1023 1027 class ForExpr inherits Expr is partial 1024 1028 -
Source/Boxes.cobra
600 600 # TODO: can a for loop go through an IEnumerator<of>? 601 601 # if .isConstructed and .genericDef is .compiler.enumeratorOfType 602 602 # return .genericParams[0] 603 getEnum as IMember? 604 if .declForName('getEnumerator') is nil 605 # Comes up for IList<of T> which has multiple 'getEnumerator' methods in ancestor interfaces 606 for member in .allMembersForName('getEnumerator') 607 if member inherits Method and member.parentBox.isGeneric 608 getEnum = member 609 break 610 if getEnum is nil 611 getEnum = .symbolForName('getEnumerator', true) 603 getEnum = .backEnd.getEnumeratorMember(this) # Enumerator/Iterator 612 604 if getEnum 613 605 assert getEnum.didBindInt 606 # CLR specific 614 607 # can have two getEnumerators -- one generic and the other not. favor the generic one 615 608 if getEnum inherits MemberOverload 616 609 for member in getEnum.members … … 619 612 # implementing IEnumerable<of T> which requires two `getEnumerator` members 620 613 getEnum = member 621 614 break 622 rt = getEnum.resultType615 rt as IType? = getEnum.resultType 623 616 if rt.isDynamicOrPassThrough 624 617 return rt 625 618 rt = rt.nonNil # nilable is not a concern; unwrap it 626 if rt inherits Box and (rt to Box).isGeneric 627 # 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 628 rt = rt.memberForName('current').resultType 629 return rt 630 else 631 if rt.isDescendantOf(.compiler.dictEnumeratorType) 632 return rt.memberForName('entry').resultType 633 if rt.isDescendantOf(.compiler.enumeratorType) 634 rt = rt.memberForName('current').resultType 635 if rt.nonNil.isSystemObjectClass 636 # can we do better with indexer returnType? (e.g. MatchCollection) 637 indexer = .symbolForName(r'[]', true) 638 if indexer, rt = indexer.resultType 639 return rt 640 else 641 throw FallThroughException({'rt': rt, 'this': this, 'getEnum': getEnum}) 619 rt = .backEnd.getEnumeratorMemberType(this, rt to !) # type for method returning enumerator/iterator item 620 return rt 642 621 return nil 643 622 644 623 def isEquatableTo(t as IType) as bool 645 624 r = base.isEquatableTo(t) 646 625 if not r and t inherits CharType and this is .compiler.stringType # TODO: hacky? could this be determined by looking for a static == operator overload? … … 1103 1082 return t 1104 1083 1105 1084 def __constructTypeFor(typeArgs as List<of IType>) as Box 1106 """ Creates the constructed type. Does not issue any .bindInh, .bindInt, or _prepFoo. """1085 """ Creates the constructed generic instance type. Does not issue any .bindInh, .bindInt, or _prepFoo. """ 1107 1086 t = .memberwiseClone to Box 1108 1087 assert t is not this 1109 1088 argNames = (for type in typeArgs get type.name).join(',') -
Source/Container.cobra
160 160 _declsByNameCI[decl.name.toLower] = decl 161 161 else 162 162 _declsByNameCI.add(decl.name.toLower, decl) 163 163 164 def rmDecl(decl as TMember) 165 """ 166 Drop an existing Decl. 167 This is for some platforms with different naming/collision rules than C#/.Net. 168 We have detected a collision and choose to handle it by clearing the existing decl 169 """ 170 require 171 decl.name.length 172 .declsInOrder.contains(decl) 173 .declForName(decl.name) is not nil 174 ensure 175 not .declsInOrder.contains(decl) 176 .declForName(decl.name) is nil 177 .declForNameCI(decl.name) is nil 178 .declsInOrder.count == old .declsInOrder.count - 1 179 body 180 _declsInOrder.remove(decl) 181 _declsByName.remove(decl.name) 182 _declsByNameCI.remove(decl.name.toLower) 183 164 184 def declForName(name as String) as TMember? 165 185 require 166 186 name.length -
Source/BinaryOpExpr.cobra
808 808 base._bindImp 809 809 _type = .compiler.boolType 810 810 if not .hasError 811 if .right.type.innerType is nil811 if not .right.type inherits EnumDecl and .right.type.innerType is nil 812 812 .recordError('The right-hand expression for "in" is of type "[.right.type.name]" which contains nothing.') 813 813 else if .left.type.nonNil inherits CharType and .right.type is .compiler.stringType 814 814 # special case because there is no String.contains that takes a char … … 852 852 if contains inherits Method 853 853 if contains.params.count == 1 and _isAssignableTo(whatType, contains.params[0].type) 854 854 return true 855 if containerType inherits EnumDecl and _isAssignableTo(whatType, containerType), return true 855 856 return false 856 857 857 858 def _isAssignableTo(a as IType, b as IType) as bool -
Source/Node.cobra
1226 1226 def exceptionType as Class 1227 1227 def idictionaryType as Box 1228 1228 def idictionaryOfType as Box 1229 def keyValuePairOfType as Box 1229 1230 def ilistType as Box 1230 1231 def ilistOfType as Box 1231 1232 def listOfType as Class -
Source/BackEnd.cobra
1 """ 2 Abstract Base class for defining items that have different implementations 3 for each back end. 4 5 Used in Compiler, 6 Backends defined/implemented in BackEnd<BE>/<be>BackEnd 7 e.g. BackEndJvm/JvmBackEnd.cobra, BackendClr/ClrBackEnd 8 Still subject to change. 9 """ 10 11 use System.Diagnostics 12 13 class BackEnd is abstract 14 """ 15 Holder for items and actions specific to the backEnd implementation 16 """ 17 18 var _tagToTypeName = Dictionary<of String, String>() 19 """ Map from Typename tags used in compiler to backend specific qualified Type name""" 20 21 get name from _name as String 22 """Name of this backend: format is 'language-platform' e.g 'c#-clr'.""" 23 24 get cobraRuntimeLibFileName from _runTimeLibFileName as String 25 """Name of the Cobra RunTime Library file for this backend.""" 26 27 get runTimeLibNativeSourceFileName from _runTimeLibNativeSourceFileName as String 28 """Name of the backEnd source file containing any native code support for Cobra.Core.""" 29 30 get compiler from __compiler as Compiler 31 32 33 cue init(compiler as Compiler) 34 base.init 35 _name = '' 36 # these two are unused defaults, overidden in concrete classes 37 _runTimeLibFileName = 'rtl' 38 _runTimeLibNativeSourceFileName = 'rtlSrc.ext' 39 __compiler = compiler 40 41 get tagToTypeName from var 42 """ Map from TypeName tags used in compiler to backend specific qualified TypeName""" 43 44 def resolveTypeTag(qualifiedNameOrTag as String) as String 45 """Turn qualified type name or a type tag into backEnd className.""" 46 if qualifiedNameOrTag.contains('.') 47 qualifiedName = qualifiedNameOrTag 48 else 49 qualifiedName = .tagToTypeName[qualifiedNameOrTag] # type tag to backend qualifiedName 50 return qualifiedName 51 52 def makePhases(phases as IList<of Phase>) is abstract 53 """ 54 Given a list of core phases for compilation complete it with additions (or even removals and 55 rearrangements if necessary) for this given backend. 56 """ 57 58 def getRecommendedBuiltInType(parentNameSpace as NameSpace?, name as String) as String? is abstract 59 """ 60 If parentNameSpace is the main system namespace and name is a backEnd builtin type 61 return the Cobra equivalent language type name (or nil). 62 """ 63 64 def computeOutName as String is abstract 65 """ 66 Return the binary file output name for compilation of files for this backend. 67 """ 68 69 get baseExeName as String 70 """Resulting Executable file name for compiled file(s) sans extension.""" 71 return '' 72 73 get fullExeName as String 74 """Resulting executable file name (pathname+extn) for compiled file(s).""" 75 return '' 76 77 def genNativeModule(filename as String, verbosity as int) as Module? is abstract 78 """ 79 Check if a filename is a Native module and if so generate and return the Native module type 80 for it otherwise return nil. 81 """ 82 83 def setupRunProcess(baseExe as String, fullExe as String) as Process is abstract 84 """ 85 Create and initialise the process to run the compiled program post compilation 86 setup varies on the backend (.Clr/Jvm) and platform. 87 """ 88 89 def setDefaultUseDirectives(ns as NameSpace) is abstract 90 """ 91 Set the default Use directives into the given Namespace (usually topNamespace) for this backend. 92 """ 93 94 def fixLibExtension(libRef as String) as String is abstract 95 """ 96 Augment given lib reference string with backend extension if not already have one. 97 """ 98 99 def loadLibReference(reference as String) as String? is abstract 100 """ 101 Load the given library reference file using the current backend paradigms. 102 Returns an error message, or nil if the reference was loaded correctly. 103 """ 104 105 def readSystemTypes is abstract 106 """ Read and Load the System Types for this backend for the compiler to make available.""" 107 108 def fixMemberSigs is abstract 109 """ 110 Most backends dont natively support nilablility, this marks/fixes the common backend 111 class signatures that should be marked nilable. 112 """ 113 114 def installNativeMethods(box as Box, nativeType as NativeType) is abstract 115 """ 116 Setup so that static methods (on Primitives and some others) get installed/treated as 117 normal methods on the instance. 118 """ 119 120 def isRunnableFile( fullExeFileName as String) as bool is abstract 121 """Test if given filename is an executable ( vs a library or something else) """ 122 123 # Native Types access 124 def cobraNameForNativeBoxName(name as String) as String is abstract 125 """ 126 Returns name from backend library entries converted to cobra naming form. 127 """ 128 129 get objectTypeProxy as AbstractTypeProxy is abstract 130 """Return backEnd TypeProxy for root of back end Object hierarchy.""" 131 132 get typeTypeProxy as AbstractTypeProxy is abstract 133 """Return backEnd TypeProxy for BackEnd notion of a class describing a Type.""" 134 135 def nativeTypeProxy(type as NativeType) as NativeTypeProxy is abstract 136 """ 137 Return a proxy placeHolder for a BackEnd Native Type. 138 """ 139 140 def nativeType(type as dynamic) as NativeType is abstract 141 """ 142 Return a Native Type wrapped so we can use it without explicitly knowing what it is anywhere 143 else but the providing back end. Used by the backends to generate Types from Libraries. 144 """ 145 146 def nativeTypeByName(qualifiedName as String) as NativeType is abstract 147 """ 148 Return a Native Type corresponding to the fully qualified name. 149 Abstract Type literal tags used in the compiler directly can also be obtained through this 150 otherwise the qualified names are expected to conform to the platform back end naming. 151 """ 152 153 def prepSystemObjectClass(box as Box) is abstract 154 """Setup additional or convenience members on the System Object class. """ 155 156 def scanGenericArgs(box as Box) is abstract 157 """Scan a loaded Dll (generic) type and translate from native any generic Args.""" 158 159 def scanNativeType(box as Box) is abstract 160 """Scan a loaded Dll type and convert its native Type info to Cobras form """ 161 162 def scanNativeType(edcl as EnumDecl) is abstract 163 """Scan a loaded Dll Enum type and convert its native info to Cobras form """ 164 165 def setUnderlyingType(edcl as EnumDecl) is abstract 166 """Set underlying Storage Type of the value of an Enum.""" 167 168 def determineExtnNativeType(extn as Extension, nativeType as NativeType) as NativeType 169 """ 170 The real extended type is the type of the first argument of any method. 171 Find and make that type from the param list. 172 """ 173 throw Exception('Need implementation of determineExtnNativeType for backend to determine type for an extension') 174 175 def handleNameSpaceNameCollision(ns as NameSpace, token as IToken, name as String) as NameSpace 176 """ 177 What to do if a namespace name collides with an existing symbol. 178 Some backends disallow this situation so its an error. 179 Others keep namespace and symbol tables separate so its perfectly allowable. 180 """ 181 throw Exception('In [ns.name] there is a already non-namespace declaration named "[name]".') 182 183 def getEnumeratorMember(box as Box) as IMember? is abstract 184 """Find enumerator/iterator method member in given Box.""" 185 186 def getEnumeratorMemberType(box as Box, rt as IType) as IType? is abstract 187 """ 188 Return the type from lookup of enumerator/iterator 'next-item' field for 189 provided enumerator/iterator (rt) from iterator for given Box. 190 """ 191 192 def validatePrintDestType(t as IType, s as Stmt) is abstract 193 """ 194 Validate the given Type as a valid destination for a print statement redirection. 195 If not record an error message against the stmt node. 196 """ 197 198 199 class FakeBackEnd inherits BackEnd 200 """ Stub BackEnd for tests. """ 201 202 cue init(compiler as Compiler) 203 base.init(compiler) 204 _name = 'c#-clr(test)' 205 _runTimeLibFileName = 'cobraRTL.libext' 206 _runTimeLibNativeSourceFileName = 'cobraRTL.ext' 207 208 _tagToTypeName = { 209 'Object': 'System.Object', 210 'Type' : 'System.Type', 211 } 212 213 def makePhases(phases as IList<of Phase>) is override 214 pass 215 216 def getRecommendedBuiltInType(parentNameSpace as NameSpace?, name as String) as String? is override 217 return nil 218 219 def computeOutName as String is override 220 return 'FakeOut' 221 222 def genNativeModule(filename as String, verbosity as int) as Module? is override 223 return nil 224 225 def setupRunProcess(baseExe as String, fullExe as String) as Process is override 226 p = Process() 227 p.startInfo.fileName = 'echo' 228 p.startInfo.arguments = 'basic BackEnd process - [fullExe]' 229 return p 230 231 def setDefaultUseDirectives(ns as NameSpace) is override 232 pass 233 234 def fixLibExtension(libRef as String) as String is override 235 if not libRef.endsWith('.libext') 236 libRef += '.libext' 237 return libRef 238 239 def loadLibReference(reference as String) as String? is override 240 return nil 241 242 def readSystemTypes is override 243 pass 244 245 def fixMemberSigs is override 246 pass 247 248 def installNativeMethods(box as Box, nativeType as NativeType) is override 249 pass 250 251 def isRunnableFile(fullExeFileName as String) as bool is override 252 return true 253 254 # Native Type access 255 def cobraNameForNativeBoxName(name as String) as String is override 256 return name + '_BBE' 257 258 def prepSystemObjectClass(box as Box) is override 259 pass 260 261 def scanGenericArgs(box as Box) is override 262 pass 263 264 def scanNativeType(box as Box) is override 265 pass 266 267 def scanNativeType(edcl as EnumDecl) is override 268 pass 269 270 def setUnderlyingType(edcl as EnumDecl) is override 271 pass 272 273 # Types 274 get objectTypeProxy as AbstractTypeProxy is override 275 return ClrTypeProxy(Object) # for testing 276 277 get typeTypeProxy as AbstractTypeProxy is override 278 return ClrTypeProxy(Type )# for testing 279 280 def nativeTypeProxy(type as NativeType) as NativeTypeProxy is override 281 return ClrTypeProxy(type) #TODO: fix this to something non BE specific Tmp 282 283 def nativeType(type) as NativeType is override 284 return ClrNativeType(type) #TODO: fix this to something non BE specific Tmp 285 286 def nativeTypeByName(qualifiedName as String) as NativeType is override 287 return ClrNativeType(System.Object) #TODO: fix this to something non BE specific Tmp 288 289 def getEnumeratorMember(box as Box) as IMember? is override 290 return nil 291 292 def getEnumeratorMemberType(box as Box, rt as IType) as IType? is override 293 return nil 294 295 def validatePrintDestType(t as IType, s as Stmt) is override 296 pass -
Source/BackEndClr/SharpGenerator.cobra
10 10 11 11 class Compiler is partial 12 12 13 # executable filenames 13 14 var _baseExeFileName as String = '' 14 15 var _fullExeFileName as String = '' 15 16 16 17 get clrBaseExeFileName from _baseExeFileName 18 get clrFullExeFileName from _fullExeFileName 19 17 20 # The next two properties are set in GenerateSharpCodePhase 18 21 19 22 pro nativeCompiler from var = '' … … 84 87 85 88 var _didWriteSharpInfoClass as bool 86 89 87 get baseExeFileName from var88 """89 Returns the exe file name sans extension.90 """91 92 get fullExeFileName from var93 """94 Returns the exe file name with extension.95 """96 97 90 var _platform as PlatformEnum? 98 91 99 92 get platform as PlatformEnum … … 103 96 return _platform to ! 104 97 105 98 def computeOutNameSharp as String 106 ensure result == .fullExeFileName99 ensure result == _fullExeFileName 107 100 outName = '' 108 101 if .options.boolValue('test') 109 102 outName = _modules.last.sharpFileName … … 240 233 241 234 _deleteIntermediateFiles 242 235 243 _copyCore (outName)236 _copyCoreClr(outName) 244 237 245 def _copyCore (outName as String)238 def _copyCoreClr(outName as String) 246 239 if not .options.boolValue('copy-core'), return 247 240 sourceDir = Path.getDirectoryName(Assembly.getEntryAssembly.location) ? '' 248 241 outDir = Path.getDirectoryName(outName) ? '' … … 286 279 throw StopCompilation(this) 287 280 _cobraSharpProxy.reset 288 281 289 backEndOptions.add('[optChar]out:[ .fullExeFileName]')282 backEndOptions.add('[optChar]out:[_fullExeFileName]') 290 283 291 284 args = List<of String>(backEndOptions) 292 285 args.addRange(sharpFileNameList) … … 318 311 pass 319 312 else if File.exists(cscPath+'.exe') 320 313 cscPath += '.exe' 321 backEndOptions += ' "[optChar]out:[ .fullExeFileName]"'314 backEndOptions += ' "[optChar]out:[_fullExeFileName]"' 322 315 p = System.Diagnostics.Process() 323 316 p.startInfo.fileName = cscPath 324 317 sharpFileNames = (for fileName in sharpFileNameList get '"' + fileName + '"').join(' ') … … 343 336 cp.compilerOptions = backEndOptions 344 337 cp.referencedAssemblies.add('System.dll') 345 338 cp.generateExecutable = .options.getDefault('target', 'exe') in ['exe', 'winexe'] 346 cp.outputAssembly = .fullExeFileName339 cp.outputAssembly = _fullExeFileName 347 340 if _verbosity >= 2 348 341 print 'Code Provider =', provider 349 342 print ' compilerOptions =', backEndOptions 350 343 print ' referencedAssemblies =', cp.referencedAssemblies 351 344 print ' generateExecutable =', cp.generateExecutable 352 print ' outputAssembly =', .fullExeFileName345 print ' outputAssembly =', _fullExeFileName 353 346 print ' sharpFileNameList =', sharpFileNameList 354 347 cr = provider.compileAssemblyFromFile(cp, sharpFileNameList.toArray) 355 348 if false … … 4720 4713 sw.write(')') 4721 4714 if v, sw.write('/* A */') 4722 4715 else 4723 .writeInCheck (sw, false, v)4716 .writeInCheckSharp(sw, false, v) 4724 4717 if v, sw.write('/* B */') 4725 4718 sw.write(')') 4726 4719 else … … 4746 4739 if v, sw.write('/* D */') 4747 4740 sw.write(' })') 4748 4741 else 4749 .writeInCheck (sw, parens, v)4742 .writeInCheckSharp(sw, parens, v) 4750 4743 if v, sw.write('/* E */') 4751 4744 4752 def writeInCheck (sw as CurlyWriter, parens as bool, v as bool)4745 def writeInCheckSharp(sw as CurlyWriter, parens as bool, v as bool) 4753 4746 if .containsExpr 4754 4747 .containsExpr.writeSharpDef(sw, parens) 4755 4748 if v, sw.write('/* F */') -
Source/BackEndClr/ScanClrType.cobra
457 457 meth = Method(TokenFix.empty, TokenFix.empty, this, 'getType', List<of Param>(), .compiler.typeTypeProxy, nil, ['shared'], AttributeList(), 'Returns the Type instance that defines this type.') 458 458 meth.sharedMethodBacking = 'typeof' 459 459 overload.addMember(meth) 460 460 461 # Enumeration lookup 462 def getEnumeratorMemberClr as IMember? 463 """ 464 Step1 of 2 step lookup for Enumerator type - return getEnumeratorMember for current box 465 """ 466 getEnum as IMember? 467 if .declForName('getEnumerator') is nil 468 # Comes up for IList<of T> which has multiple 'getEnumerator' methods in ancestor interfaces 469 for member in .allMembersForName('getEnumerator') 470 if member inherits Method and member.parentBox.isGeneric 471 getEnum = member 472 break 473 if getEnum is nil 474 getEnum = .symbolForName('getEnumerator', true) 475 if getEnum 476 assert getEnum.didBindInt 477 # can have two getEnumerators -- one generic and the other not. favor the generic one 478 if getEnum inherits MemberOverload 479 for member in getEnum.members 480 if member inherits Method 481 if member.resultType <> .compiler.objectType 482 # implementing IEnumerable<of T> which requires two `getEnumerator` members 483 getEnum = member 484 break 485 return getEnum 486 487 def getEnumeratorMemberTypeClr(rt as IType) as IType? 488 """ 489 Step2 of 2 step lookup for Enumerator type - return the type for Enumerator member for current box 490 rt is the resultType from getEnumerator lookup above - i.e the Enumerator for this Box 491 """ 492 if rt inherits Box and (rt to Box).isGeneric 493 # 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 494 return rt.memberForName('current').resultType 495 else 496 if rt.isDescendantOf(.compiler.dictEnumeratorType) 497 return rt.memberForName('entry').resultType 498 if rt.isDescendantOf(.compiler.enumeratorType) 499 rt = rt.memberForName('current').resultType 500 if rt.nonNil.isSystemObjectClass 501 # can we do better with indexer returnType? (e.g. MatchCollection) 502 indexer = .symbolForName(r'[]', true) 503 if indexer, rt = indexer.resultType 504 return rt 505 else 506 throw FallThroughException({'rt': rt, 'this': this }) 507 return nil 508 509 # dll scanning 461 510 def scanNativeTypeClr 462 511 """ 463 512 Subclasses should invoke base and then invoke the various _scanFoo methods that are appropriate for them. -
Source/BackEndClr/ClrBackEnd.cobra
36 36 'IDictionary': 'System.Collections.IDictionary', 37 37 'IDictionary<of,>': 'System.Collections.Generic.IDictionary<of,>', 38 38 'Dictionary<of,>' : 'System.Collections.Generic.Dictionary<of,>', 39 'KeyValuePair<of,>':'System.Collections.Generic.KeyValuePair<of,>', 39 40 'Set<of>': 'Cobra.Core.Set<of>', 40 41 41 42 'bool' : 'System.Boolean', … … 86 87 m = SharpModule(filename, verbosity) 87 88 return m 88 89 90 get baseExeName as String is override 91 """Resulting executable file name for compiled file(s) sans extension.""" 92 return .compiler.clrBaseExeFileName 93 94 get fullExeName as String is override 95 """Resulting full executable file name (pathname+extn) for compiled file(s).""" 96 return .compiler.clrFullExeFileName 97 89 98 def setupRunProcess(baseExe as String, fullExe as String) as Process is override 90 99 p = Process() 91 100 branch .compiler.platform … … 114 123 115 124 def fixLibExtension(libRef as String) as String is override 116 125 """ 117 Augment given lib reference string with backend extension if not already have one126 Augment the given library filename reference with backend extension if not already have one. 118 127 """ 119 128 or require libRef.length 120 129 and ensure result.endsWith('.dll') or result.endsWith('.exe') … … 143 152 .compiler.clrReadAssembly(a) 144 153 else 145 154 # TODO: support targeting a specific CLR version, but not below 2.0 146 #.compiler.readAssembly(Assembly.load('mscorlib.dll') to !)147 155 .compiler.clrReadAssembly(Assembly.load('mscorlib.dll') to !) 148 156 149 157 # TODO: .readAssembly(Assembly.loadFrom('System.dll') to !) 150 158 # gives: Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.dll' or one of its dependencies. The system cannot find the file specified. 151 159 152 160 t = System.Diagnostics.Process.getType 153 #.compiler.readAssembly(t.assembly) # System.dll154 161 .compiler.clrReadAssembly(t.assembly) # System.dll 155 162 156 163 def fixMemberSigs is override … … 179 186 qualifiedName = .resolveTypeTag(qualifiedNameOrTag) 180 187 return ClrNativeType(.compiler.clrTypeByName(qualifiedName)) 181 188 /# 182 These are the type tags used directly by the compilercode.189 These are the type tags used directly by cobra code. 183 190 184 191 bool 'System.Boolean' 185 192 char 'System.Char' … … 222 229 """Clr doesnt allow colliding names.""" 223 230 throw Exception('In [ns.name] there is a already non-namespace declaration named "[name]".') 224 231 225 226 232 def getEnumeratorMember(box as Box) as IMember? is override 233 """ Find enumerator method member in given Box.""" 234 return box.getEnumeratorMemberClr 235 236 def getEnumeratorMemberType(box as Box, enumResultType as IType) as IType? is override 237 """Return type from lookup of enumerator 'current' field from in given Box.""" 238 return box.getEnumeratorMemberTypeClr(enumResultType) 239 240 def validatePrintDestType(t as IType, n as Stmt) is override 241 """ 242 Check that the given Type is a valid destination for a print statement redirection. 243 If not record an error message 244 """ 245 if not t.isDescendantOf(.compiler.libraryType('System.IO.TextWriter')) 246 # actually only needs a write/writeLine call 247 n.recordError('Invalid destination of type "[t.name]" for "print". Use a TextWriter or subclass thereof instead.') 248 227 249 class GenerateSharpCodePhase inherits Phase 228 250 229 251 cue init(c as Compiler) -
Source/files-to-compile.text
5 5 6 6 CommandLine 7 7 TestifyRunner 8 BackEnd 8 9 Compiler 9 10 10 11 KeywordSpecs -
Source/BackEndJvm/JvmJarSig.cobra
36 36 """ 37 37 Remap unique virtualised java class name to an actual existing Java class name. 38 38 """ 39 39 40 def reset 41 """Clear all static caches""" 42 JarSig.classByNameCache.clear 43 40 44 def lookupClassByCobraName(fullCobraName as String) as JavaClassType 41 45 """ 42 46 This is intended to be the only way to access the JarSig classCache contents from outside this file. 43 47 """ 44 48 assert fullCobraName[0].isUpper 45 49 #fullCobraName = .virtualNameRemaps.get(fullCobraName, fullCobraName) 46 parts = fullCobraName.split('.') 47 for i in 0: parts.length-1 48 parts[i] = parts[i][0].toLower.toString + parts[i][1:] 49 fullName = parts.join('.') 50 fullName = _convertCobraName(fullCobraName) 50 51 #print 'Lookup "[fullCobraName]" as "[fullName]"' 51 52 if not JarSig.classByNameCache.containsKey(fullName) # dbg 52 53 print 'Class [fullName] not in JarSig ClassName cache' 54 #for k in JarSig.classByNameCache.keys, print k 53 55 throw Exception('Cannot find class [fullName]') 54 56 return JarSig.classByNameCache[fullName] 57 58 def _convertCobraName(fullCobraName as String) as String 59 #if fullCobraName.contains('<') # genericInstDef Aaa.Bbb.C<Jjj.Uuu.C> 60 # genParts = fullCobraName.split('<') 61 # for i in 0 : genParts.length 62 # genParts[i] = _xvertName(genParts[i]) 63 # fullName = genParts.join('<') 64 #else 65 # fullName = _xvertName(fullCobraName) 66 #return fullName 67 # 68 #def _xvertName(fullCobraName as String) as String 69 #"""Turn names of form Aaa.Bbb.Class to aaa.bbb.Class. Cobra form to java.""" 70 parts = fullCobraName.split('.') 71 for i in 0: parts.length-1 72 parts[i] = parts[i][0].toLower.toString + parts[i][1:] 73 fullName = parts.join('.') 74 return fullName 55 75 56 76 def lookupClass(fullName as String) as JavaClassType 57 77 """ … … 636 656 idxrProp = _genAProp('[.canonicalName]_synthesizedIdxr', getMethod, setMethod, true) 637 657 _props.add(idxrProp) 638 658 _indexer = idxrProp 639 sb = StringBuilder('= ')640 if getMethod641 sb.append(getterName)642 if setMethod, sb.append(',')643 if setMethod644 sb.append(setterName)645 print 'dbg: [.canonicalName] indexerMethods [sb.toString] '659 #sb = StringBuilder('= ') 660 #if getMethod 661 # sb.append(getterName) 662 # if setMethod, sb.append(',') 663 #if setMethod 664 # sb.append(setterName) 665 #print 'dbg: [.canonicalName] [.getHashCode] indexerMethods [sb.toString] ' 646 666 #else 647 667 # print 'dbg: No IndexerMethods [getterName]/[setterName] on [.canonicalName]' 648 668 #/ 669 649 670 def _lookForMethods( getterName as String, setterName as String) as List<of JavaMethodInfo?> 650 671 getter as JavaMethodInfo? = nil 651 672 setter as JavaMethodInfo? = nil … … 653 674 if m.name == getterName, getter = m 654 675 if m.name == setterName, setter = m 655 676 return [getter, setter] 656 677 678 def lookupIndexer as JavaFieldInfo? 679 """ 680 Search this class and superclass chain for an indexer. Return first hit or nil. 681 Used to map indexing '[]' cobra code to java method calls. 682 """ 683 #depth=0 684 #print depth, .getHashCode, .canonicalName, .superclass 685 idxr = .indexer 686 if idxr 687 return idxr 688 super = .getSuperClass # otherwise try superclass chain 689 while super 690 idxr = super.indexer 691 if idxr 692 break 693 super = super.getSuperClass 694 return idxr 657 695 658 696 cue init(name as String, pkg as String, type as JavaType, super as String, ifcs as List<of String>, modifiers as List<of String> ) 659 697 """Create a normal Class or GenericClass Defn. MyClass or MyClass<T>""" … … 880 918 #return JvmNativeType(_type.getSuperClass) 881 919 if JarSig.classByNameCache.containsKey(.superclass) 882 920 return JarSig.lookupClass(.superclass) 921 else if .superclass.contains('`') # isGeneric 922 key = .superclass.before('`') 923 return JarSig.lookupClass(key) 883 924 else 884 925 return JarSig.lookupClass('java.lang.Object') 885 926 # e.g. Abstract base classes with default access - java.lang.AbstractStringBuilder … … 1029 1070 assert false, 'dump Indexers NYI' 1030 1071 1031 1072 class JavaMemberInfo 1073 """Baseclass for java fields and properties, constructors and methods""" 1032 1074 var name = '' 1033 1075 var _attributes as List<of String>? 1034 1076 var modifiers = List<of String>() # public, default (=protected+pkg), protected, private … … 1061 1103 return .attributes.contains('Override') 1062 1104 1063 1105 get isNonNullable 1106 if .isFinal 1107 return true # arbitrarily declare final members non nilable till get attributes working. 1064 1108 return .attributes.contains('NotNull') or .attributes.contains('NonNull') or .attributes.contains('Nonnull') 1065 1109 #for name in .attributes # in java these are annotations, - via pkgSig treated as strings 1066 1110 # if name.endsWith == 'NotNull' or name.endsWith == 'NonNull' or name.endsWith == 'Nonnull' … … 1114 1158 else if .isReadable, prefix = 'get' 1115 1159 else if .isWritable, prefix = 'set' 1116 1160 1117 sb = StringBuilder('[prefix] 1161 sb = StringBuilder('[prefix].[.name]') 1118 1162 if .typeName.length, sb.append(' as [.type]') 1119 1163 if .modifiers.count 1120 1164 sb.append(' is ') -
Source/BackEndJvm/ScanJvmType.cobra
44 44 if not sigFilePath 45 45 # eventually may also search CLASSPATH 46 46 if rv, print 'Returning false for __jvmLoadReference("[reference]").' 47 return 'to-do: what to put here? sigFilePath not found?'47 return 'to-do: what to put here? sigFilePath "[sigFilePath]" not found?' 48 48 49 49 if rv, print 'Will read jarsig: [sigFilePath]' 50 50 try … … 98 98 99 99 def javaReadJar(jSig as JarSig) 100 100 """ 101 Reads the contents of a jar (actually JarSig) file so that they are accessible to the program. 101 Reads the contents of a jar (actually JarSig) file and make the symbols accessible 102 to the compiler for the program being compiled. 102 103 """ 103 104 if jSig.name in _didReadJars, return 104 105 _didReadJars.add(jSig.name) … … 113 114 try 114 115 _modules.add(module) 115 116 for jvmType in jSig.getExportedTypes 116 if jvmType.isNested # or jvmType.declaringType117 if jvmType.isNested # or jvmType.declaringType 117 118 nestedTypes.add(jvmType) 118 119 continue 119 120 typeNamespace = jvmType.packageAsCobra #Capitalized … … 183 184 184 185 _fix('Java.Lang.Object', 'toString getClass clone') 185 186 # ^ regarding .toString, not technically true, but common enough and life is too painful when the return type is nilable 186 #_fix('java.lang.System', 'out') #stdout187 _fix('Java.Lang.System', 'in out err lineSeparator') 187 188 _fix('Java.Lang.String', r'[] length charAt concat remove replace replaceAll replaceFirst substring toLowerCase toUpperCase trim format') 188 189 #_fix('System.Type', 'assembly name toString') 189 190 # namespace can return nil if the Type is a generic parameter … … 225 226 #_fix('Java.Util.AbstractMap.SimpleEntry<of,>', r'getKey getValue') 226 227 #_fix('Java.Util.AbstractMap.SimpleImmutableEntry<of,>', r'getKey getValue') 227 228 _fix('Java.Util.Properties', r'getProperty') 229 _fix('Java.Util.Calendar', 'getInstance getTimeZone') 228 230 229 231 #_fix('System.IO.File', 'create createText open openRead openText openWrite readAllBytes readAllLines readAllText') 230 232 _fix('Java.Io.File', 'create createText open openRead openText openWrite readAllBytes readAllLines readAllText getPath getAbsolutePath') … … 292 294 293 295 def installJvmNativeMethods(box as Box, nativeType as NativeType) 294 296 """Install entries for native static methods on box instances.""" 295 pass 297 pass #TODO support native static methods on box instances 296 298 #print '++ TODO scanJvmType.installJvmNativeMethods' 297 299 /# 298 300 meths = List<of Method>() … … 352 354 #overload.addMember(meth) 353 355 .addDecl(meth) 354 356 357 358 def getIteratorMemberJvm as IMember? 359 """ 360 Step1 of 2 step lookup for iterator/enumerator type - return iterator Member for current box 361 """ 362 getEnum as IMember? 363 if .declForName('iterator') is not nil # Collections 364 getEnum = .symbolForName('iterator', true) 365 return getEnum 366 367 if .declForName('entrySet') is not nil # Map 368 es = .symbolForName('entrySet', true) 369 est = es.resultType.nonNil to Box 370 getEnum = est.symbolForName('iterator', true) 371 # could look for Enumerable here also 372 373 return getEnum 374 375 def getIteratorNextTypeJvm(rt as IType) as IType? 376 """ 377 Step2 of 2 step lookup for iterator/Enumerator type - return the type for iterator.next member for current box 378 rt is the resultType from iterator lookup above - i.e the Iterator for this Box 379 """ 380 if rt inherits Box and (rt to Box).isGeneric 381 rType = rt.memberForName('next').resultType 382 else 383 throw FallThroughException('getEnumeratorMemberType - rt not genericBox [rt]') 384 return rType 385 355 386 def scanNativeTypeJvm 356 387 """ 357 388 Subclasses should invoke base and then invoke the various _scanXXX methods that are appropriate for them. … … 550 581 break 551 582 if skip, continue 552 583 name = Utils.cobraNameForNativeMemberName(methInfo.name) 584 other = .declForName(name) 553 585 genericParams = List<of IType>() 554 586 for genArg in methInfo.getGenericArguments 555 587 genericParams.add(GenericParam(JvmNativeType(genArg))) … … 570 602 overload = MemberOverload(other) 571 603 .registerOverload(overload to !) 572 604 else 573 print 'Unexpected overload for name:', name, other 574 throw FallThroughException([this, method, other]) 605 if not other inherits BoxVar 606 print 'Unexpected overload (existing named decl) for method "[name]":', other 607 throw FallThroughException([this, method, other]) 608 609 # here we have a field name (BoxVar) shadowing a method name. 610 # In java this is unambiguous cos all methods are called with () suffix. 611 # In cobra we have to do something else... 612 # currently will choose to drop ( make inaccessible) the field... 613 # ...stupid field begone 614 print 'DUP field "[name]" is shadowing the method "[name]" on [.name],' 615 print ' Dropping the field. It will not be accessible from cobra code' 616 .rmDecl(other) 617 575 618 if overload 576 619 overload.addMember(method) 577 620 else … … 653 696 def _jvmMemberTypeProxy(jvmType as JavaClassType?, notNull as bool) as ITypeProxy 654 697 """ 655 698 Returns a type proxy for a member type such as a parameter type. 656 In Java, reference types are nilable by default, but you can pass `true` for `notNull` to indicate there was a NotNull Annotation in the jar metadata. 699 In Java, reference types are nilable by default, but you can pass `true` for `notNull` to 700 indicate there was a NotNull Annotation in the jar metadata. 657 701 """ 658 702 if jvmType is nil 659 703 return .compiler.voidType … … 695 739 def _fixNestedNilables 696 740 if .parentBox # nested 697 741 query = '[.parentBox.qualifiedName].[.name]' 698 print 'fixNest', query742 #print 'fixNest', query 699 743 memberNames = _nestedUnNil.get(query, '') 700 744 if memberNames.length 701 745 .parentBox.membersToUnNil = memberNames … … 789 833 def scanNativeTypeJvm 790 834 # TODO: read attribs 791 835 _needScanNativeType = false 792 _storageType = .compiler.anyIntType # tmp ?? 836 _storageType = .compiler.anyIntType # Not relevant ?? 837 838 #TODO fake up support for values() and valueOf() and getValue() synthesized in codegen 839 # - or not - how much of C#/Cobra enum capability to support?? 793 840 794 841 /# Enable when have Enum and JavaEnumType in PkgSig and JarSig 795 842 jvmType = (_nativeType to JvmNativeType).backEndType 796 843 assert jvmType inherits JavaEnumType # temporary 797 844 for name, value in jvmType.getConstantValues # list of names and values 798 intValue = value to int 799 member = EnumMember(name, intValue) 845 member = EnumMember(name, value) 800 846 member.enumDecl = this 801 847 .addDecl(member) 802 848 #/ 803 849 804 /#805 isByte = Enum.getUnderlyingType(jvmType).name == 'Byte'806 is64 = Enum.getUnderlyingType(jvmType).name == 'Int64'807 isU32 = Enum.getUnderlyingType(jvmType).name == 'UInt32'808 isU64 = Enum.getUnderlyingType(jvmType).name == 'UInt64'809 values = Enum.getValues(jvmType)810 i = 0811 for name in Enum.getNames(jvmType)812 value = values.getValue(i)813 # CC: lameness follows814 if isByte815 intValue = int.parse((value to uint8).toString)816 else if is64817 try818 intValue = int.parse((value to int64).toString)819 catch OverflowException820 intValue = 999 # CC: omg. but probably doesn't affect anything. we're reading the DLL here, not creating one821 else if isU32822 try823 intValue = int.parse((value to uint32).toString)824 catch OverflowException825 intValue = 2147483647826 else if isU64827 try828 intValue = int.parse((value to uint64).toString)829 catch OverflowException830 intValue = 2147483647831 else832 intValue = value to int833 member = EnumMember(name, intValue)834 member.enumDecl = this835 .addDecl(member)836 i += 1837 850 #/ 838 851 839 852 -
Source/BackEndJvm/JvmBackEnd.cobra
17 17 'Object': 'Java.Lang.Object', 18 18 'Type' : 'Java.Lang.Class<of>', # java.lang.Type is a marker interface 19 19 'String' : 'Java.Lang.String', 20 'Exception' : 'Java.Lang.RuntimeException', 20 #'Exception' : 'Java.Lang.RuntimeException', 21 'Exception' : 'Java.Lang.Exception', 21 22 22 'Delegate' : 'Cobra.Core.Delegate', 23 'Delegate' : 'Cobra.Core.DelegateO', 24 #'Delegate<of>' : 'Cobra.Core.Delegate<of>', 23 25 'Attribute' : 'Java.Lang.Annotation.Annotation', 24 26 'ICloneable': 'Java.Lang.Cloneable', 25 27 'IEnumerable': 'Java.Lang.Iterable', 26 28 'IEnumerator': 'Java.Lang.Iterator', 27 29 'IEnumerable<of>' : 'Java.Lang.Iterable<of>', 28 30 'IEnumerator<of>' : 'Java.Lang.Iterator<of>', 29 'IDictionaryEnumerator' : 'Java.Lang.Iterator', 30 #'IDictionaryEnumerator' : 'System.Collections.IDictionaryEnumerator', 31 'ICollection': 'Java.Util.Collection', # non generic collection interface 31 'IDictionaryEnumerator' : 'Java.Lang.Iterator', # non generic Dictionary enumer interface - Non existant 32 'ICollection': 'Java.Util.Collection', # non generic collection interface Non existant in java 32 33 'ICollection<of>': 'Java.Util.Collection<of>', 33 34 'IList' : 'Java.Util.List<of Object>', # non generic List interface 34 35 'IList<of>' : 'Java.Util.List<of>', 35 36 'List<of>': 'Java.Util.ArrayList<of>', 36 'IDictionary': 'Java.Util.Map<of Object,Object>', # Non Generic Map/Dict interface 37 #'IDictionary': 'Java.Util.Map<of Object,Object>', # Non Generic Map/Dict interface 38 'IDictionary': 'Java.Util.Map<of,>', # Non Generic Map/Dict interface 37 39 'IDictionary<of,>': 'Java.Util.Map<of,>', 38 40 'Dictionary<of,>' : 'Java.Util.HashMap<of,>', 41 'KeyValuePair<of,>':'Java.Util.Map<of,>.Entry<of,>', 39 42 'Set<of>': 'Java.Lang.HashSet<of>', 40 43 41 44 'bool' : 'Java.Lang.Boolean', # boolean … … 100 103 m = JavaModule(filename, verbosity) 101 104 return m 102 105 106 get baseExeName as String is override 107 """Resulting Executable file name for compiled file(s) sans extension.""" 108 return .compiler.javaMainClass 109 110 get fullExeName as String is override 111 """Resulting executable file name (pathname+extn) for compiled file(s).""" 112 return .compiler.fullJarFileName 113 103 114 def setupRunProcess(baseExe as String, fullExe as String) as Process is override 115 """ 116 Setup compiled java process to run assuming using an executable jar file. 117 All info needed should be already set in jarfile manifest - i.e main entry point 118 and any classpath dependencies 119 """ 104 120 p = Process() 105 121 p.startInfo.fileName = 'java' 122 # below is same as 123 #args = ' -jar [.compiler.fullJarFileName]' 124 args = ' -jar [fullExe]' 125 126 p.startInfo.arguments = args + ' ' 127 #print p.startInfo.fileName, p.startInfo.arguments 128 return p 129 130 def setupRunProcessClass(baseExe as String, fullExe as String) as Process #is override 131 """ 132 Setup running a java class file(s), using CobraCore.jar and assuming jarfiles 133 needed set in CLASSPATH env var ('.') 134 Superseded by setupRunProcess above. 135 """ 136 p = Process() 137 p.startInfo.fileName = 'java' 106 138 clPath = Path.getDirectoryName(CobraCore.exePath) to ! 107 139 clPath = Path.combine(clPath, 'CobraCore.jar') 108 140 classpath = Environment.getEnvironmentVariable('CLASSPATH') … … 115 147 p.startInfo.arguments = args + ' ' 116 148 #print p.startInfo.fileName, p.startInfo.arguments 117 149 return p 118 150 119 151 def setDefaultUseDirectives(ns as NameSpace) is override 120 152 # java packages available by default 121 153 useToken = Token('(implicit)', 1, 1, 1, 'USE', 'use', nil) … … 153 185 154 186 def readSystemTypes is override 155 187 # Initially we will rely on a external java tool to have been run to produce a precis 156 # file in a known place (same as cobra compiler perhaps) containing exported types for188 # file in a known place (same as cobra compiler) containing exported types for 157 189 # items in a jar file. 158 190 159 191 # JarSig is a holder for a jarfile or package extended with info on contained classes. 160 192 # javaReadJar loads info on contained classes into Cobra 161 193 # rt.jar is only one we need 194 JarSig.reset 162 195 .compiler.javaReadJar(JarSig('rt.jar')) 163 196 164 197 def fixMemberSigs is override … … 178 211 179 212 # Types 180 213 get objectTypeProxy as AbstractTypeProxy is override 181 """Type proxy for BEroot of Object hierarchy."""214 """Type proxy for backEnd root of Object hierarchy.""" 182 215 if not _objectTypeProxy 183 216 _objectTypeProxy = JvmTypeProxy(JarSig.lookupClassByCobraName('Java.Lang.Object')) 184 217 return _objectTypeProxy to ! 185 218 186 219 get typeTypeProxy as AbstractTypeProxy is override 187 """Type proxy for B Enotion of a class describing a Type."""220 """Type proxy for BackEnd notion of a class describing a Type.""" 188 221 if not _typeTypeProxy 189 222 _typeTypeProxy = JvmTypeProxy(JarSig.lookupClassByCobraName('Java.Lang.Class')) 190 223 return _typeTypeProxy to ! … … 196 229 return JvmNativeType(type) 197 230 198 231 def nativeTypeByName(qualifiedNameOrTag as String) as NativeType is override 199 # mapqualified name of .net aliased types to java equivalents232 # map the qualified name of .net aliased types to java equivalents 200 233 # bool, char, decimal, single, double, sbyte, int{16,32,64}, byte, uint{16,32,64}, 201 234 qualifiedName = .resolveTypeTag(qualifiedNameOrTag) 202 235 return JvmNativeType(.compiler.jvmTypeByName(qualifiedName)) … … 235 268 #print 'Any use of the namespace in cobra code must be changed accordingly.' 236 269 237 270 #print 'Cobra name collision in namespace "[ns.name]" for child namespace "[name]" and an existing declaration. Use name "[name]NS" for java namespace/package "[name]" .' 238 print ' In namespace "[ns.name]" java namespace "[name]" renamed in cobra to "[name]NS". (cobra name collision)'271 print 'Java "[ns.name].[name]" renamed in cobra to "[ns.name].[name]NS". (cobra name collision)' 239 272 name = name+'NS' 240 273 return ns.getOrMakeNameSpaceNamed(token, name) # recursive 241 274 … … 254 287 255 288 def setUnderlyingType(edcl as EnumDecl) is override 256 289 edcl.setUnderlyingTypeJvm 290 291 def getEnumeratorMember(box as Box) as IMember? is override 292 """ Find iterator method member in given Box.""" 293 return box.getIteratorMemberJvm 257 294 295 def getEnumeratorMemberType(box as Box, rt as IType) as IType? is override 296 """Return type from lookup of iterator 'next' field in given Box.""" 297 return box.getIteratorNextTypeJvm(rt) 298 299 def validatePrintDestType(t as IType, n as Stmt) is override 300 """ 301 Check that the given Type is a valid destination for a print statement redirection. 302 If not record an error message 303 """ 304 t = t.nonNil 305 if t.isDescendantOf(.compiler.libraryType('Java.Io.Writer')) 306 return # commonly PrintWriter or StringWriter 307 if t.isDescendantOf(.compiler.libraryType('Java.Io.PrintStream')) 308 return # for System.out 309 n.recordError('Invalid destination of type "[t.name]" for "print". Use a Java.Io.Writer or subclass thereof (PrintWriter, StringWriter) instead.') 310 311 258 312 class GenerateJavaCodePhase inherits Phase 259 313 260 314 cue init(c as Compiler) -
Source/BackEndJvm/JavaGenerator.cobra
11 11 ## 12 12 13 13 class Compiler is partial 14 var _javaMainClass as String = '' 15 var _fullJarFileName as String = '' 16 17 get javaMainClass from var 18 get fullJarFileName from var 19 14 20 15 21 # run prior to compilation to determine an output filename for the java source 16 # for testing for need for compilation.22 # for testing, for need for compilation. 17 23 # Filename used may be determined later post AST generation using the first classname in file 18 24 # see 'fixup output filename' in compileJava below 19 25 def computeOutNameJava as String … … 25 31 if outName == '', outName = .defaultOutName to ! 26 32 if outName.endsWith('.java') or outName.endsWith('.JAVA'), outName = outName[:-5] 27 33 if outName.endsWith('.cobra') or outName.endsWith('.COBRA'), outName = outName[:-6] 28 _ baseExeFileName= outName34 _javaMainClass = outName 29 35 30 outName = Utils.forceExtension(outName, '.class') 31 _fullExeFileName = outName 36 #outName = Utils.forceExtension(outName, '.class') #v1 37 outName = Utils.forceExtension(outName, '.jar') 38 _fullJarFileName = outName 32 39 return outName 33 40 34 41 def writeJava 35 42 Node.setCompiler(this) 36 43 try 37 #pass38 44 _moduleFileName_to_curlyToCobraLineNum = Dictionary<of String, Dictionary<of int, int>>() 39 45 for _curModule in _modules 40 46 if not _curModule.fileName.endsWith('SystemInterfaces.cobra') 41 47 javaToCobraLineNum = _curModule.writeJavaDef 42 _moduleFileName_to_curlyToCobraLineNum[_curModule.fileName] = javaToCobraLineNum 43 _moduleFileName_to_curlyToCobraLineNum[Path.getFullPath(_curModule.fileName)] = javaToCobraLineNum 48 .storeJavaFileInfo(_curModule.fileName, javaToCobraLineNum) 44 49 finally 45 50 Node.setCompiler(nil) 46 51 52 def storeJavaFileInfo(fileName as String, javaToCobraLineNum as Dictionary<of int, int>?) 53 # called above and fm Namespace.writeJavaDef after new class file 54 _moduleFileName_to_curlyToCobraLineNum[fileName] = javaToCobraLineNum 55 _moduleFileName_to_curlyToCobraLineNum[Path.getFullPath(fileName)] = javaToCobraLineNum 56 47 57 def writeJavaTestInvocation 48 58 # TODO 49 59 print 'TODO: writeJavaTestInvocation' … … 59 69 options = .options 60 70 61 71 # locate the Java compiler 62 # TODO: This just does files compilation, augment to also generate manifest and executable jarfile63 72 # TODO: augment to use Java library compiler when Java native 64 73 compilerPath = options.getDefault('native-compiler', 'auto') to String 65 74 if compilerPath <> 'auto' … … 71 80 print 'Cannot find compiler specified by -native-compiler argument: [compilerPath]' 72 81 throw StopCompilation(this) 73 82 else 74 # TODO use a javatool - wrap javac and jarfile gen for multiple files75 # javaTool -m MainClass -o outFile file-list76 83 compilerPath = if(Utils.isRunningOnUnix, 'javac', 'javac.exe') 77 84 78 85 optChar = '-' … … 110 117 nativeArgs = nativeArgs[1:-1] 111 118 backEndOptions += ' ' + nativeArgs 112 119 113 #sep = if(CobraCore.isRunningOnUnix, ':', ';') # after next snapshot 114 cpSep = ';' 115 if (Environment.osVersion.platform to int) in [4, 6, 128] # http://www.mono-project.com/FAQ:_Technical 116 cpSep = ':' 120 cpSep = if(CobraCore.isRunningOnUnix, ':', ';') 117 121 118 122 libclassPath = '' 119 123 for libPath in .options.getStringList('library-directory') … … 123 127 # explicitly add reference to it until we get setup to install a copy in extdirs 124 128 ccPath = Path.getDirectoryName(CobraCore.exePath) to ! 125 129 ccPath = Path.combine(ccPath, 'CobraCore.jar') 126 backEndOptions += ' -classpath "[ccPath][libclassPath]"' 130 classPath = '[ccPath][libclassPath]' 131 backEndOptions += ' -classpath "[classPath]"' 127 132 128 # TODO: add libPaths/classpaths for referenced namespaces133 # TODO: add libPaths/classpaths for any referenced namespaces 129 134 130 135 # .java files fm first class name in each module 131 javaFileName List = List<of String>()136 javaFileNameSet = Set<of String>() 132 137 for module in _modules[1:] 133 138 if module.javaFileName.length 134 javaFileNameList.add(module.javaFileName) 135 139 javaFileNameSet.add(module.javaFileName) 140 #javaFileNameList = _intermediateFileNames 141 for name in _intermediateFileNames 142 if name.length, javaFileNameSet.add(name) 143 javaFileNameList = List<of String>(javaFileNameSet) 144 145 #trace javaFileNameList, _intermediateFileNames 136 146 # fixup javaMain pkg path all lower case 137 147 # fixup output filename (classfile) to be in sync with Java filename 138 148 javaMain = _getFixedMainType 139 149 if outName <> javaFileNameList[0] 140 150 outName = javaFileNameList[0] 141 #_baseExeFileName = Path.changeExtension(outName, nil) to ! # remove any extn 142 _baseExeFileName = javaMain 151 _javaMainClass = javaMain 143 152 outName = Utils.forceExtension(outName, '.class') 144 _fullExeFileName = outName 153 154 if .defaultOutName 155 _fullJarFileName = Utils.forceExtension(.defaultOutName to !, '.jar') 145 156 146 157 # compilation command 147 158 if _verbosity … … 150 161 print 'compiler =', compilerPath 151 162 print ' compilerOptions =', backEndOptions 152 163 print ' nativeArgs =', nativeArgs 153 print ' outName =', .fullExeFileName164 print ' fullExeFileName=', _fullJarFileName 154 165 print ' baseExeFileName =', .baseExeFileName 155 166 print ' javaFileNameList =', javaFileNameList 156 157 output = _execAndCapture(compilerPath, backEndOptions, javaFileNameList) 158 _parseJavaCompilerOutput(output) 159 167 print ' defaultOutName =', .defaultOutName 168 169 #output = _execAndCapture(compilerPath, backEndOptions, javaFileNameList) 170 #_parseJavaCompilerOutput(output) 171 _javaTool(compilerPath, backEndOptions, classPath, javaFileNameList) 172 160 173 if .errors.count 161 174 _exitFromErrors 162 175 163 176 _deleteIntermediateFiles 177 _copyCoreJvm(outName) 178 179 def _copyCoreJvm(outName as String) 180 if not .options.boolValue('copy-core'), return 181 sourceDir = Path.getDirectoryName(Assembly.getEntryAssembly.location) ? '' 182 # above should be something non platform dependent (COBRA_HOME ?) 183 outDir = Path.getDirectoryName(outName) ? '' 184 _copyFile(sourceDir, outDir, 'Cobra.Core.jar') 185 #if .options.getDefault('debug', '') not in ['', '-', '0', 0, false] 186 # copy any debug files 187 # TODO: probably also need to adjust Manifest File Class-Path for cobra RTL 164 188 165 # Better would be options as optionList as List<of String> but ftm we're stuck with space joined strings 166 # coming into caller 189 def _javaTool(javacPath as String, options as String, classPath as String, fileNameList as List<of String>) 190 """ 191 javatool - wrap executing javac and executable jarfile generation for single or 192 multiple files 193 """ 194 # clean directory for java compiled cobraClasses 195 classDir = 'cobraClasses' 196 if Directory.exists(classDir) 197 Directory.delete(classDir, true) 198 Directory.createDirectory(classDir) 199 200 # put compiled classes in own subdir 201 options = options.replace('-d .', '-d [classDir]') 202 203 # run java compiler on java srcfiles 204 output = _execAndCapture(javacPath, options, fileNameList) 205 _parseJavaCompilerOutput(output) 206 if _errors.count, return 207 208 # Make manifest file - entry for Class-Path: <classPath> 209 cpSep = if(CobraCore.isRunningOnUnix, ':', ';') 210 manFileName = 'in.mf' 211 mfClassPath = classPath.replace(cpSep, ' ') # classpath entries space separated 212 mfClassPath = mfClassPath.replace('C:',' ') # cant handle drive designators 213 mfText = 'Class-Path: [mfClassPath] [Environment.newLine]' 214 try 215 File.writeAllLines(manFileName, [mfText].toArray) 216 catch exc as Exception 217 .warning(CobraWarning(manFileName, nil, 'Cannot write manifest file due to: [exc.message] ([exc.getType.name]).')) 218 219 # make jarfile 220 _intermediateFileNames.add(manFileName) 221 #outName = Utils.forceExtension(.defaultOutName to !, '.jar') 222 outName = _fullJarFileName 223 mainClass = _javaMainClass 224 #print 'jar cfem [outName] [.baseExeFileName] [manFileName] -C [classDir] .' 225 output = _execAndCapture('jar', 'cfem [outName] [mainClass] [manFileName] -C [classDir]', ['.']) 226 #print output # TODO parse this for errs 227 228 # Better would be options as optionList as List<of String> but ftm we're stuck with space 229 # joined strings coming into caller 167 230 # TODO: move this to CobraCore to avoid .NET dependency 168 231 def _execAndCapture(binaryPath as String, options as String, fileNameList as List<of String>) as String 169 232 p = System.Diagnostics.Process() … … 173 236 p.startInfo.arguments = '[options] [fileNames]' 174 237 if _verbosity >= 2 175 238 print '[p.startInfo.fileName] [p.startInfo.arguments]' 176 output = p.runAndCaptureAllOutput 239 try 240 output = p.runAndCaptureAllOutput 241 catch exc as System.ComponentModel.Win32Exception 242 print 'Error: Compiler file [p.startInfo.fileName] not found in path' 243 throw exc 177 244 # TODO: check p.exitCode, especially if output is empty 178 245 return output 179 246 … … 204 271 javaMain = '[javaPkg].[exeParts.last]' 205 272 #trace .mainMethodTypeName, javaMain 206 273 return javaMain 274 275 #hoisted from CobraModule 276 def newJavaFile( cobraModuleName as String, javaFileName as String) as CurlyWriter 277 #print 'newJavaFile [javaFileName]' 278 file = File.createText(javaFileName) 279 sw = CurlyWriter(file, CurlyLineNumberTreatment.Comment) 280 .addIntermediateFile(javaFileName) 281 sw.start(cobraModuleName) 282 sw.write('// [javaFileName] from [cobraModuleName]\n') 283 sw.write('// Generated by Cobra-[CommandLine.versionString]\n') 284 sw.write('// on [DateTime.now]\n') 285 # TODO: list op sys 286 sw.write('\n') 287 return sw 288 289 207 290 ## 208 291 ## Node 209 292 ## … … 328 411 return classFileName 329 412 330 413 def writeJavaDef as Dictionary<of int, int>? is override 331 file = File.createText(.javaFileName) 332 using sw = CurlyWriter(file, CurlyLineNumberTreatment.Comment) 333 .compiler.addIntermediateFile(_javaFileName) 334 sw.start(.fileName) 335 sw.write('// [_javaFileName] from [_fileName]\n') 336 sw.write('// Generated by Cobra-[CommandLine.versionString]\n') 337 sw.write('// on [DateTime.now]\n') 338 # TODO: list op sys 339 sw.write('\n') 340 414 using sw = (.compiler to Compiler).newJavaFile(.fileName, .javaFileName) 341 415 if .isMainWrapper 342 416 .synthesizeJavaMainWrapper(sw) 343 417 else 344 418 .topNameSpace.writeJavaDef(sw) 345 419 d = sw.curlyToCobraLineNum 346 420 return d 347 421 348 422 def writeJavaTestInvocation(sw as CurlyWriter) is override 349 423 # TODO: .topNameSpace.writeJavaTestInvocation(sw) 350 424 pass … … 422 496 return .javaRef 423 497 424 498 get _computeJavaRef as String 425 if .parent 426 s = .parent.javaRef 499 if .parent 500 if .parent.javaRef <> 'global' # no global (default) namespace in java 501 s = .parent.javaRef # ?'global'? 502 s += '.' 427 503 s += .javaName 428 504 else 429 505 s = .javaName … … 480 556 481 557 def writeJavaDef(sw as CurlyWriter) 482 558 """ 483 Write the Java code for this member de claration to the given CurlyWriter.559 Write the Java code for this member definition/declaration to the given CurlyWriter. 484 560 """ 485 561 486 562 def writeJavaTestInvocation(sw as CurlyWriter) … … 523 599 def writeJavaDef(sw as CurlyWriter) is override 524 600 assert not .isUnified 525 601 base.writeJavaDef(sw) 602 _writeNameSpaceHeading(sw) 603 604 # Top Level classes + Interfaces in a Namespace 605 d0 = _declsInOrder[0] 606 firstclass = true 607 for decl in _declsInOrder 608 isNames = List<of String>() 609 if decl inherits Box 610 .compiler.boxStack.push(decl) 611 isNames.add(decl.defaultAccessLevel) 612 isNames.addRange(decl.isNames) 613 if firstclass or decl == d0 or not 'public' in isNames 614 decl.writeJavaDef(sw) 615 firstclass = if(decl inherits EnumDecl, true, false) 616 else 617 # java - compile error if public classes are not in their own file - a pox on whoever made this unsuppressable 618 javaFileName = decl.name + '.java' 619 # make it look like this class was made in its own cobra module 620 # TODO insert Module name in new Java file header 621 cobraModName = decl.name + '.cobra' 622 #trace javaFileName, cobraModName 623 624 compiler =(.compiler to Compiler) 625 using sw = compiler.newJavaFile(cobraModName, javaFileName) 626 _writeNameSpaceHeading(sw) 627 decl.writeJavaDef(sw) 628 javaToCobraLineNum = sw.curlyToCobraLineNum 629 compiler.storeJavaFileInfo(cobraModName, javaToCobraLineNum) 630 631 if decl inherits Box, .compiler.boxStack.pop 632 633 def _writeNameSpaceHeading(sw as CurlyWriter) 526 634 if not .isRoot 527 javaName = .name.toLower # java idiom is all lowercase for packages528 sw.write('package [ javaName][.javaSuffix]; \n\n')635 pkgName = .name.toLower # java idiom is all lowercase for packages 636 sw.write('package [pkgName][.javaSuffix]; \n\n') 529 637 for ud in _useDirectives # imports 530 638 ud.writeJavaDef(sw) 531 for decl in _declsInOrder 532 if decl inherits Box 533 .compiler.boxStack.push(decl) 534 decl.writeJavaDef(sw) 535 if decl inherits Box 536 .compiler.boxStack.pop 537 # TODO 538 #if not .isRoot 539 # sw.dedentAndWrite('} // namespace [.name]\n') 540 639 541 640 def writeJavaTestInvocation(sw as CurlyWriter) is override 542 641 for decl in _declsInOrder 543 642 decl.writeJavaTestInvocation(sw) … … 590 689 get javaNameComponent as String 591 690 """ 592 691 Returns a string that refers to this type and is suitable for embedding in a larger 593 identifier (meaning there will be no punct ion, spaces or Java comments).692 identifier (meaning there will be no punctuation, spaces or Java comments). 594 693 """ 595 694 596 695 get javaRef as String … … 778 877 ## Enums 779 878 ## 780 879 781 # TODO 880 class EnumDecl is partial 881 882 get javaRef as String is override 883 if .isUsedAsSet, return 'java.util.EnumSet<[base.javaRef]>' 884 return base.javaRef 885 886 get javaRefBase as String 887 return base.javaRef 782 888 889 get javaInit as String is override 890 if .isUsedAsSet, return 'java.util.EnumSet.noneOf([base.javaRef].class)' 891 return base.javaRef + '.' + _declsInOrder[0].name 783 892 893 def writeJavaDef(sw as CurlyWriter) is override 894 base.writeJavaDef(sw) 895 if 'extern' in _isNames 896 return 897 for attr in .attributes 898 attr.writeJavaDef(sw) 899 if not _isNames.count, _isNames.add('internal') # to get nothing 900 .writeJavaIsNames(sw) 901 sw.write('enum [_name]') 902 /# we dont support storageType in java 903 storageType = _storageType ... 904 #/ 905 sw.write(' {\n') 906 sw.indent 907 sep = '' 908 i = 0 909 for em as EnumMember in .declsInOrder 910 sw.write(sep) 911 sw.write(em.name) 912 if em.value 913 sw.write('([em.value])') 914 complex = true 915 i += 1 916 sep = ',\n' 917 sw.write(';\n') 918 if complex # Make something like .Net enum - specific int values 919 sw.writeLine 920 sw.writeLine('private static int defValue = 0;') 921 sw.writeLine('private static void setDef(int b){defValue = b;}') 922 sw.writeLine('private static int getDef(){return defValue;}') 923 sw.writeLine('private int value;') 924 sw.writeLine('private [_name](int value) {this.value = value; setDef(value+1);}') 925 sw.writeLine('private [_name]() {this.value = getDef(); setDef(this.value+1);}') 926 sw.writeLine('public int getValue() {return this.value;}') 927 sw.dedent 928 sw.write('}\n\n') 929 930 931 class EnumMember is partial 932 933 def writeJavaTestInvocation(sw as CurlyWriter) 934 pass 935 936 get javaRef as String is override 937 return _enumDecl.javaRefBase + '.' + .javaName 938 939 784 940 ## 785 941 ## Boxes 786 942 ## … … 831 987 #trace .parentNameSpace.javaQualifier 832 988 javaRef = .parentNameSpace.javaQualifier + javaRef 833 989 else if .parentBox 834 trace .parentBox.javaRef990 #trace .parentBox.javaRef 835 991 javaRef = .parentBox.javaRef + '.' + javaRef 836 #trace java 992 #trace javaRef 837 993 return javaRef 838 994 839 995 def writeJavaDef(sw as CurlyWriter) … … 1077 1233 1078 1234 def writeJavaConstraint(sw as CurlyWriter) 1079 1235 if _constraints.count 1080 # TODO 1236 # TODO map cobra constraints to Java syntax 1237 # TODO before that try and comprehend java syntax for generic constraints 1081 1238 sw.write(' where [_name] : ') 1082 1239 sep = '' 1083 1240 for constraint in _constraints … … 1113 1270 1114 1271 class Extension is partial 1115 1272 1116 pass # TODO STUB 1273 pass # TODO STUB: Implement extensions 1117 1274 1118 1275 1119 1276 ## … … 1521 1678 if .isCompilerGenerated, return 1522 1679 if param.type.isReference and not param.type inherits NilableType and not param.isOut 1523 1680 if .compiler.options.boolValue('include-nil-checks') 1524 sw.write('if (cobra.core.CobraCore._willCheckNil && [param.javaName]==null) throw new System.ArgumentNullException("[param.name]");\n') 1681 sw.write('if (cobra.core.CobraCore._willCheckNil)\n') 1682 sw.indent 1683 sw.write('cobra.core.CobraImp.checkNotNull([param.javaName], "ParameterGuard Fail: non nilable parameter `[param.javaName]` is null" );\n') 1684 sw.dedent 1685 #sw.write('if (cobra.core.CobraCore._willCheckNil && [param.javaName]==null) throw new NullPointerException("Non nilable parameter named [param.name] is Null");\n') 1525 1686 1526 1687 def writeJavaDSTHead(sw as CurlyWriter) 1527 1688 if not .canHaveDetailedStackTrace … … 1802 1963 #.writeJavaIsNames(sw) 1803 1964 sw.write(' [_returnType.javaRef] this') 1804 1965 .writeJavaParams(sw, r'[]') 1805 #.write SharpBody(sw)1966 #.writeJavaBody(sw) 1806 1967 # TODO: finish correctly (method with special attribute??? 1807 1968 1808 1969 class MemberOverload is partial … … 1939 2100 sw.write(');\n') 1940 2101 sw.dedent 1941 2102 1942 /#1943 # use java asserts initially at least1944 sw.write(')\n')1945 sw.indent1946 sw.write('assert ')1947 _expr.writeJavaDef(sw)1948 #_expr.writeJavaBreakdown(sw)1949 #sw.write('[.javaThis]')1950 sw.write(' : ')1951 sw.write('String.format("assertion %s FAILED", ')1952 #sw.write('"assertion " + ')1953 tmpsw = CurlyWriter(StringWriter(), sw.curlyLineNumberTreatment)1954 _expr.writeJavaDef(tmpsw)1955 #sw.write(tmpsw.toString.replace('"', r'\"'))1956 sw.write(Utils.javaStringLiteralFor(tmpsw.toString))1957 #sw.write('" + " FAILED "')1958 sw.write(')')1959 if _info1960 sw.write(' + "info="+')1961 _info.writeJavaDef(sw)1962 sw.write(';\n')1963 sw.dedent1964 #/1965 1966 2103 class BlockStmt is partial 1967 2104 1968 2105 def writeJavaDef(sw as CurlyWriter) … … 2074 2211 sw.write('boolean [gotRightExceptionVarName] = false;\n') 2075 2212 sw.write('try ') 2076 2213 _block.writeJavaDef(sw) 2077 sw.writeAndIndent('catch ([exceptionTypeRef] ) {\n')2214 sw.writeAndIndent('catch ([exceptionTypeRef] _expExc ) {\n') 2078 2215 sw.write('// exactly what is expected\n') 2079 2216 sw.write('[gotRightExceptionVarName] = true;\n') 2080 2217 sw.dedentAndWrite('}\n') 2081 2218 if _exceptionType inherits Box and not (_exceptionType to Box).isJvmSystemExceptionClass 2082 wrongExceptionVarName = '_lh_expect_[_varNumber]'2083 assert gotRightExceptionVarName <> wrongExceptionVarName2084 sw.writeAndIndent('catch (java.lang.Throwable [ wrongExceptionVarName]) {\n')2085 sw.write('throw new cobra.core.ExpectException( typeof([exceptionTypeRef]), [wrongExceptionVarName]);\n')2219 gotWrongExceptionVarName = '_lh_expect_[_varNumber]' 2220 assert gotRightExceptionVarName <> gotWrongExceptionVarName 2221 sw.writeAndIndent('catch (java.lang.Throwable [gotWrongExceptionVarName]) {\n') 2222 sw.write('throw new cobra.core.ExpectException([gotWrongExceptionVarName].getClass(), [gotWrongExceptionVarName]);\n') 2086 2223 sw.dedentAndWrite('}\n') 2087 sw.write('if (![gotRightExceptionVarName]) throw new cobra.core.ExpectException([exceptionTypeRef]. getClass(), null);\n')2224 sw.write('if (![gotRightExceptionVarName]) throw new cobra.core.ExpectException([exceptionTypeRef].class, null);\n') 2088 2225 2089 2226 2090 2227 class ForStmt is partial … … 2251 2388 # in a C# foreach, (1) you must declare a new variable, (2) you cannot reassign it and (3) you cannot access it after the loop 2252 2389 # these constraints don't exist in Cobra 2253 2390 helperName = '_lh_for_[_var.name]_[_varNumber]' 2254 sw.write('for each ([_var.type.javaRef] [helperName] in')2391 sw.write('for/*each*/ ([_var.type.javaRef] [helperName] : ') 2255 2392 2256 2393 # TODO: the javaRef of a type is not qualified and there is no using System.Collections; 2257 2394 # _what.writeJavaDefInContext(sw, false) … … 2320 2457 2321 2458 def writeJavaDef(sw as CurlyWriter) 2322 2459 base.writeJavaDef(sw) 2323 if _destination # java Writer 2460 if _destination # java Writer or PrintStream 2461 methodName = if(_stop, 'printStop', 'printLine') 2462 sw.write('cobra.core.CobraImp.[methodName](') 2324 2463 _destination.writeJavaDef(sw, true) 2325 methodName = if(_stop, 'write', 'writeLine') 2326 sw.write('.[methodName](') 2464 sw.write(', ') 2465 2466 # if destination as one type or with constant available methods in each type 2467 # i.e if we only allowed destination to be a PrintStream or a PrintWriter... 2468 #_destination.writeJavaDef(sw, true) 2469 #methodName = if(_stop, 'print', 'println') 2470 #sw.write('.[methodName](') 2327 2471 else 2328 2472 methodName = if(_stop, 'printStop', 'printLine') 2329 2473 sw.write('cobra.core.CobraImp.[methodName](') … … 2429 2573 sw.write('[sep][Utils.javaStringLiteralFor(expr.toCobraSource)][sep]') 2430 2574 expr.writeJavaDef(sw, false) 2431 2575 sw.write(');\n') 2576 2577 2578 class TryStmt 2579 is partial 2580 2581 def writeJavaDef(sw as CurlyWriter) 2582 base.writeJavaDef(sw) 2583 if _successBlock 2584 # Java also has no "success" (or in Python, "else") block for the "try" statement 2585 # so it has to be simulated 2586 helperName = '_lh_success_[_varNumber]' 2587 sw.write('boolean [helperName] = true;\n') 2588 if _finallyBlock 2589 sw.write('try {\n') 2590 sw.indent 2591 sw.write('try') 2592 _tryBlock.writeJavaDef(sw, false) 2593 sw.dedent 2594 sw.write('}\n') 2595 if _catchBlocks.count 2596 .writeCatchBlocksJava(sw, helperName) 2597 2598 sw.write('finally /*Success block*/{ if ([helperName])') 2599 _successBlock.writeJavaDef(sw) 2600 sw.write('}\n') 2601 2602 if _finallyBlock 2603 sw.dedent 2604 sw.write('}\n') 2605 sw.write('finally') 2606 _finallyBlock.writeJavaDef(sw) 2607 else 2608 sw.write('try') 2609 _tryBlock.writeJavaDef(sw) 2610 .writeCatchBlocksJava(sw, nil) 2611 if _finallyBlock 2612 sw.write('finally') 2613 _finallyBlock.writeJavaDef(sw) 2614 2615 # All exceptions caught will be either 2616 # checked exceptions wrapped in a (RuntimeException based) CheckedExceptionWrapper 2617 # or other RunTimeException based Exceptions. 2618 # Generate code to rethrow these and can then just use the original cobra catchblocks as-is 2619 def writeCatchBlocksJava(sw as CurlyWriter, localSuccessVar as String?) 2620 sw.write('catch (Exception _exc0) {\n') 2621 sw.indent 2622 sw.write('try {\n') 2623 sw.write(' if (_exc0 instanceof cobra.core.CheckedExceptionWrapper) {\n') 2624 sw.write(' cobra.core.CheckedExceptionWrapper cxw = (cobra.core.CheckedExceptionWrapper)_exc0;\n') 2625 sw.write(' throw cxw.getCause(); //:codeGen: catchblock Header\n') 2626 sw.write(' }\n') 2627 sw.write(' else throw (RuntimeException)_exc0;}\n') 2628 2629 sw.write('//-- cobra-code catch blocks\n') 2630 _writeCatchBlocksJava(sw, localSuccessVar) # cobra code catch blocks 2631 sw.write('//-- end cobra-code catch blocks\n') 2632 2633 # fallthrough catch clause to propagate uncaught exceptions 2634 hasException = false 2635 for cb in _catchBlocks 2636 if not cb.type or (cb.type and cb.type == .compiler.exceptionType) 2637 hasException = true 2638 break 2639 if not hasException 2640 sw.write('catch (Exception _exc1){\n') 2641 sw.write(' if (_exc1 instanceof cobra.core.CheckedExceptionWrapper){\n') 2642 sw.write(' throw (cobra.core.CheckedExceptionWrapper) _exc0; //rethrow as uncaught\n') 2643 sw.write(' }\n') 2644 sw.write(' else throw (RuntimeException)_exc1;\n}\n') 2645 2646 sw.dedent 2647 sw.write('}\n') 2648 2649 # This is original writeCatchBlocksJava code generating the cobra catch blocks. 2650 # It originally worked alongside writeJavaDef0 in ThrowStmt below 2651 def _writeCatchBlocksJava(sw as CurlyWriter, localSuccessVar as String?) 2652 for cb in _catchBlocks 2653 sw.write('catch (') 2654 if cb.type 2655 sw.write(' [cb.type.javaRef]') 2656 else # no catchAll in Java language so catch Exception 2657 sw.write(' java.lang.Exception') 2658 helperName = if(cb.varName, cb.sharpHelperName, '_anyExc' ) 2659 # using sharpHelperName above is correct(same as sharp) - value set in Statements:CatchBlock.bindInt 2660 # should be renamed to be non BE specific tho' 2661 cb.javaRethrowName = helperName to ! # stash name for possible rethrow 2662 sw.write(' [helperName]') 2663 sw.write(')') 2664 sb = StringBuilder() 2665 if localSuccessVar, sb.append('[localSuccessVar] = false;\n') 2666 if .compiler.hasDetailedStackTraceOption, sb.append('CobraCoreInternal.CobraImp.HandledException();\n') 2667 if cb.varName 2668 trackLocals = .compiler.willTrackLocals 2669 if trackLocals, sb.append('CobraCoreInternal.CobraImp.SetLocal("[cb.varName]", ') 2670 sb.append('[cb.javaVarName] = [helperName]') 2671 if trackLocals, sb.append(')') 2672 sb.append(';\n') 2673 cb.block.writeJavaDef(sw, sb.toString) 2674 2675 2676 2677 class CatchBlock 2678 is partial 2679 2680 get javaVarName as String 2681 return _var.javaName 2682 2683 var javaRethrowName as String? 2684 2685 class ThrowStmt is partial 2686 /# 2687 # This version tries to hide cobra mapping of Exceptions to RuntimeExceptions from coders 2688 # We allow checked Exceptions (cobra coded) but when thrown codegen encloses them in a runtimeException: 2689 # In catch clauses we internally unwrap, examine and handle exceptions as coded 2690 # 2691 # Feeds into and is related to the (YTBD) determination of what to do with calls to methods throwing 2692 # checked exceptions - 2693 # Ideally codegen would automatically wrap calls generating a checked exception in a RuntimeException 2694 # generating wrapper and remainder handling would be treated as above. 2695 # There is the possibility of a lotta repeated duplicated line by line code generated for cases 2696 # that a coder writing same would put together in a block using same try-catch... 2697 # e.g. multiple sequential file io calls all generating IOException say.. 2698 # so as a convenience we may also want to provide some level of user specification 2699 # (compiler directive maybe) that specifies the enclosure limits of what should be wrapped in the 2700 # automatic code generated exception trapping boilerplate.. 2701 # @jvm-wcx <ExceptionTYPE> 2702 # .. lines of code that may gen such an Exception 2703 # 2704 # As currently implemented we could just detect such calls (not explicitly wrapped) and emit a 2705 # diagnostic (actually rely on javac to do it for us) suggesting that the cobra code must explicitly 2706 # trap/handle/declare checked exceptions and generate RuntimeException from it. 2707 # 2708 # This explicitly pushes all the necessary handling onto the user but cruelly exposes our non support for 2709 # conveniently remapping checked exceptions... 2710 # also it makes for ugly code (and obscures the code control flow). 2711 # (see comment below) 2712 #/ 2713 def writeJavaDef(sw as CurlyWriter) 2714 base.writeJavaDef(sw) 2715 sw.write('throw ') 2716 if _expr 2717 if _expr.type.isDynamic, sw.write('((java.lang.RuntimeException)') 2718 #throw Runtime Exceptions as is 2719 if _expr.type.isDescendantOf(.compiler.libraryType('Java.Lang.RuntimeException') to Box) 2720 _expr.writeJavaDef(sw, false) 2721 else 2722 # Checked exceptions get wrapped in a runtime Exc wrapper 2723 sw.write('new cobra.core.CheckedExceptionWrapper("Wrapping checked exception [_expr.type.name]",') 2724 _expr.writeJavaDef(sw, false) 2725 sw.write(')') 2726 if _expr.type.isDynamic, sw.write(')') 2727 else # rethrow 2728 # Java doesnt support an unadorned throw in a CatchBlock (rethrow), needs an explicit exception 2729 # look in enclosing block and if its a catchblock use its stored name (javaVarName) 2730 if .superNode and .superNode.superNode inherits CatchBlock 2731 rethrow = (.superNode.superNode to CatchBlock).javaRethrowName 2732 sw.write('cobra.core.CobraImp.reThrow(') 2733 sw.write(rethrow) 2734 sw.write(')') 2735 2736 sw.write(';\n') 2737 2738 /# 2739 # This original version of throws codegen was used along with JvmBackEnd 2740 # _tagToTypeName = {... 2741 # 'Exception' : 'Java.Lang.RuntimeException', 2742 # ...} 2743 # makes the base Exceptionclass (compiler.ExceptionType) to be set to RuntimeException. 2744 # 2745 # This forces dvlpr written code to use only RuntimeException based exceptions explicitly 2746 # and also forces the explicit trapping of all methods throwing checked exceptions 2747 #(to handle or convert to a wrapping RuntimeException) which does obscure the code layout - just like java... 2748 # We dont provide a way to specify Exceptions thrown on a method definition so the net effect is to 2749 # force cobra code to be written to conform (explicitly) to our forcing of all java Exceptions to 2750 # unchecked which may or may not be seen to be a good thing..... 2751 # but its extra boilerplate cruft in cobra code, ugly and inconvenient to do 2752 #/ 2753 def writeJavaDef0(sw as CurlyWriter) 2754 base.writeJavaDef(sw) 2755 sw.write('throw ') 2756 if _expr 2757 if _expr.type.isDynamic 2758 sw.write('((java.lang.RuntimeException)') 2759 _expr.writeJavaDef(sw, false) 2760 if _expr.type.isDynamic 2761 sw.write(')') 2762 else # rethrow 2763 # Java doesnt support an unadorned throw in a CatchBlock (rethrow), needs an explicit exception 2764 # look in enclosing block and if catchblock use its stored name ( javaVarName) 2765 if .superNode and .superNode.superNode inherits CatchBlock 2766 rethrow = (.superNode.superNode to CatchBlock).javaRethrowName 2767 sw.write(rethrow) 2768 2769 sw.write(';\n') 2770 2432 2771 2433 2772 2434 2773 class UsingStmt … … 2698 3037 class EnumCallExpr is partial 2699 3038 2700 3039 def writeJavaDef(sw as CurlyWriter, parens as bool) is override 2701 if parens, sw.write('(') 3040 isSet = _args.count > 1 3041 if parens 3042 if isSet, sw.write('java.util.EnumSet.of') 3043 sw.write('(') 2702 3044 if _args.count == 0 2703 sw.write(' default([_definition.javaRef])')3045 sw.write('/*default([_definition.javaRef])*/') 2704 3046 else 2705 3047 sep = '' 2706 3048 for member in _members 2707 3049 sw.write(sep) 2708 3050 sw.write(member.javaRef) 2709 sep = ' |'3051 sep = ',' 2710 3052 if parens, sw.write(')') 2711 3053 2712 3054 def writeJavaBreakdownItems(sw as CurlyWriter) … … 2887 3229 handled = true 2888 3230 if not handled 2889 3231 _target.writeJavaDef(sw) 2890 2891 ntIdxr = _lookupReadIndexer 3232 ntIdxr = _lookupIndexer 2892 3233 if ntIdxr 2893 assert ntIdxr.getter 2894 assert ntIdxr.getter.name.length 3234 assert ntIdxr.getter # getter for indexer exists 3235 assert ntIdxr.getter.name.length # and has a name set 2895 3236 _writeIndexer(sw, '.[ntIdxr.getter.name](', ')') 2896 3237 else 3238 # fallback to treat as Array Type 2897 3239 _writeIndexer(sw, r'[', r']') 2898 3240 2899 3241 if parens, sw.write(')') 2900 3242 2901 def _lookup ReadIndexer as JavaFieldInfo?3243 def _lookupIndexer as JavaFieldInfo? 2902 3244 nt = JvmNativeType.nativeType(_target.type.nonNil) 2903 #print 'dbg', _target.type.nonNil, nt 3245 if not nt # _target.type is genericInstance 3246 tt = _target.type.nonNil to Box 3247 genDef = tt.genericDef 3248 nt = JvmNativeType.nativeType(genDef) 3249 #print 'dbg-ReadIdxr', _target.type.nonNil,'\n+ ', nt 2904 3250 if nt 2905 ntIdxr = nt.indexer 3251 ntIdxr = nt.lookupIndexer 3252 #print 'ntIdxr: [ntIdxr]' 2906 3253 if ntIdxr and ntIdxr.isReadable 2907 3254 return ntIdxr 2908 3255 #TODO: check for Indexer attribute for a Cobra user written version 2909 3256 return nil 2910 3257 2911 3258 def _writeIndexer(sw as CurlyWriter, prefix as String, suffix as String) 2912 3259 sw.write(prefix) 2913 3260 _writeIdxArgs(sw) … … 3237 3584 3238 3585 get asJava as String is override 3239 3586 return _name 3587 3588 class SequenceLit 3589 is partial 3240 3590 3591 def writeJavaBreakdownItems(sw as CurlyWriter) is override # CC: axe is override 3592 base.writeJavaBreakdownItems(sw) 3593 sw.write(', +1') 3594 for expr in _exprs 3595 expr.writeJavaBreakdownItems(sw) 3596 sw.write(', -1') 3597 3598 class ListLit 3599 is partial 3600 3601 def writeJavaDef(sw as CurlyWriter, parens as bool) is override 3602 innerType = (_type to Box).genericParams[0] 3603 sw.write('cobra.core.CobraImp.makeList/*<[innerType.javaRef]>*/(') 3604 if _exprs.count 3605 sep = '' 3606 for expr in _exprs 3607 sw.write(sep) 3608 expr.writeJavaDef(sw, false) 3609 sep = ', ' 3610 sw.write(')') 3611 3612 3613 class ArrayLit 3614 is partial 3615 3616 def writeJavaDef(sw as CurlyWriter, parens as bool) is override 3617 sw.write('new [_type.javaRef] { ') 3618 if _exprs.count 3619 sep = '' 3620 for expr in _exprs 3621 sw.write(sep) 3622 expr.writeJavaDef(sw, false) 3623 sep = ', ' 3624 sw.write(' }') 3625 3626 3627 class SetLit 3628 is partial 3629 3630 def writeJavaDef(sw as CurlyWriter, parens as bool) is override 3631 innerType = (_type to Box).genericParams[0] 3632 sw.write('cobra.core.CobraImp.makeSet/*<[innerType.javaRef]>*/(') 3633 if _exprs.count 3634 sw.write(', ') 3635 sep = '' 3636 for expr in _exprs 3637 sw.write(sep) 3638 expr.writeJavaDef(sw, false) 3639 sep = ', ' 3640 sw.write(')') 3641 3642 3241 3643 class ToNilableOrNotExpr 3242 3644 is partial 3243 3645 … … 3338 3740 if _right inherits TryCatchExpr # expand trycatch and do assignment within the expansion 3339 3741 _right.writeJavaDef(sw) 3340 3742 return 3743 #trace _right 3341 3744 3342 3745 # TODO: 3343 3746 # if trackLocal: … … 3867 4270 _left.writeJavaBreakdownItems(sw) 3868 4271 3869 4272 4273 class InExpr is partial 4274 """ v in container""" 4275 4276 def writeJavaDef(sw as CurlyWriter, parens as bool) is override 4277 v = true 4278 branch _op 4279 on 'NOTIN', sw.write('!') 4280 on 'IN', pass 4281 else, throw FallThroughException(_op) 4282 if .needsNilCheck 4283 # Constraints: 4284 # (1) Don't evaluate the left expression twice 4285 # (2) Don't evaluate the right expression if the left is nil 4286 # In general, don't cause side effects from extra evaluation. 4287 if .left inherits IdentifierExpr and (.left to IdentifierExpr).definition inherits IVar 4288 # If the left is a var, there is no danger in double evaluation causing side effects. 4289 # java: (someVar==null ? false : <check>) 4290 sw.write('(') 4291 .left.writeJavaDef(sw, false) 4292 sw.write('==null ? false : ') 4293 # may have to cast int? to int, for example 4294 if .left.type inherits NilableType _ 4295 and not .left.type.nonNil.isReference _ 4296 and .containsExpr 4297 # well this last condition doesn't work so well due to generic args like List.Contains(T item) 4298 # and not (((.containsExpr to DotExpr).dotRight.memberDefinition to Method).params[0].type inherits NilableType) 4299 4300 # then need cast 4301 assert .left.type inherits NilableType 4302 .right.writeJavaDef(sw, false) 4303 sw.write('.contains(([.left.type.nonNil.javaRef])') 4304 .left.writeJavaDef(sw) 4305 sw.write(')') 4306 if v, sw.write('/* A */') 4307 else 4308 .writeInCheckJava(sw, false, v) 4309 if v, sw.write('/* B */') 4310 sw.write(')') 4311 else 4312 # cobra: left in right 4313 # java: cobra.core.CobraImp.inWithNullCheck(<left>, delegate(__lh_value) { return <right>.Contains(__lh_value); }) 4314 # use: static public bool inWithNullCheck<T>(T a, Predicate<T> predicate) 4315 4316 # cobra: left in right 4317 # java: cobra.core.CobraImp.in(left, delegate() { return right; }) 4318 sw.write('cobra.core/*CobraCoreInternal*/.CobraImp.inWithNullCheck(') 4319 .left.writeJavaDef(sw, false) 4320 sw.write(', delegate([.left.type.javaRef] __lh_value_[.serialNum]) { return ') 4321 if .containsExpr 4322 # Do not write the .containsExpr here because it's argument to .contains in the 4323 # full left expression. Instead, we want to pass the argument passed to the delegate. 4324 .right.writeJavaDef(sw, false) 4325 sw.write('.contains(([.left.type.nonNil.javaRef]) __lh_value_[.serialNum]);') 4326 if v, sw.write('/* C */') 4327 else 4328 # No .containsExpr means we use CobraImp.In(a, b) for the delegate 4329 sw.write('cobra.core/*CobraCoreInternal*/.CobraImp.in(__lh_value_[.serialNum], ') 4330 .right.writeJavaDef(sw, false) 4331 if v, sw.write('/* D */') 4332 sw.write(' })') 4333 else 4334 .writeInCheckJava(sw, parens, v) 4335 if v, sw.write('/* E */') 4336 4337 def writeInCheckJava(sw as CurlyWriter, parens as bool, v as bool) 4338 if .containsExpr 4339 .containsExpr.writeJavaDef(sw, parens) 4340 if v, sw.write('/* F */') 4341 else 4342 sw.write('cobra.core/*CobraCoreInternal*/.CobraImp.in(') 4343 .left.writeJavaDef(sw, false) 4344 sw.write(',') 4345 .right.writeJavaDef(sw, false) 4346 sw.write(')') 4347 if v, sw.write('/* G */') 4348 3870 4349 class InheritsExpr 3871 4350 is partial 3872 4351 … … 3875 4354 #sw.write(' instanceof ') 3876 4355 #_right.writeJavaDef(sw, false) 3877 4356 4357 # have to handle primitives ( bool, char, byte,short, int, long,float, double) specially 4358 3878 4359 # Compiler generates error with instanceof for non intersecting instance and Type so 3879 4360 # instead use runtime instance lookup 3880 4361 # instead of 'e instanceof T' becomes 'T.class.isInstance(e)' 3881 _right.writeJavaDef(sw, false) 3882 #sw.write('.class.isInstance(') 4362 # Have to map primitives to wrappers cos autoboxing makes it wrong 4363 if _right inherits TypeExpr # primitives 4364 ctjref = _right.containedType.javaRef 4365 if ctjref == 'int', ctjref = 'Integer.class' 4366 if ctjref == 'char', ctjref = 'Character.class' 4367 if ctjref == 'short', ctjref = 'Short.class' 4368 if ctjref == 'long', ctjref = 'Long.class' 4369 if ctjref == 'boolean', ctjref = 'Boolean.class' 4370 if ctjref == 'byte', ctjref = 'Byte.class' 4371 if ctjref == 'float', ctjref = 'Float.class' 4372 if ctjref == 'double', ctjref = 'Double.class' 4373 sw.write(ctjref) 4374 isPrim = true 4375 4376 if not isPrim 4377 _right.writeJavaDef(sw, false) 3883 4378 sw.write('.isInstance(') 3884 4379 _left.writeJavaDef(sw) 3885 4380 sw.write(' )') 3886 4381 3887 3888 4382 class ToExpr is partial 3889 4383 3890 4384 /# def needsContextCast as bool is override … … 4168 4662 def writeJavaTestInvocation(sw as CurlyWriter) 4169 4663 pass 4170 4664 4171 class EnumMember is partial4172 4173 def writeJavaTestInvocation(sw as CurlyWriter)4174 pass4175 4176 4665 class Extension is partial 4177 4666 4178 4667 get javaKeyWord as String is override -
Source/TestifyRunner.cobra
281 281 if baseName.startsWith('.') or baseName.startsWith('_') 282 282 # examples: .svn, _svn. Also, _ is a nice way to temporarily exclude a directory if possible 283 283 return 284 if _useConfigPath(dirName), return 285 _testifyDir(dirName, nil) 286 287 def _useConfigPath(dirName as String) as bool 284 288 configPath = Path.combine(dirName, 'testify.kv') 285 289 if File.exists(configPath) 286 290 dirOptions = Utils.readKeyValues(configPath) … … 288 292 argSets = dirOptions['args'].split(@[c'|']) 289 293 for argSet in argSets 290 294 _testifyDir(dirName, argSet.trim) 291 return 292 _testifyDir(dirName, nil)293 295 return true 296 return false 297 294 298 def _testifyDir(dirName as String, args as String?) 295 willCopyLibs = true # not CobraCore.isRunningMono # On Mono, we use MONO_PATH instead of coping the Cobra.Core.dll around296 libNames = ['Cobra.Core.dll']297 299 _extraTestifyArgs = args 298 300 _statusWriter.writeLine(if(args, '[dirName] with args: [args]', '[dirName]')) 299 301 _statusWriter.indent 300 302 try 301 303 print 'Running tests in [dirName]' 302 304 saveDir = Environment.currentDirectory 303 if willCopyLibs 304 # _cleanDir will remove these later 305 for libName in libNames 306 # Ideally, the file operations below would not be wrapped with try...catch and a 307 # 'warning' because if the current library files cannot be copied into the test 308 # directory, then the test is not valid. However, Windows has problems with 309 # deleting and/or copying over Cobra.Core.dll right after it has been used. This 310 # comes up on Tests\340-contracts whose testify.kv config file specifies two 311 # runs of the directory. 312 targetPath = Path.combine(dirName, libName) 313 if File.exists(targetPath) 314 if Utils.isRunningOnUnix 315 File.delete(targetPath) 316 else 317 # Windows is just too damn picky... 318 try 319 File.delete(targetPath) 320 catch UnauthorizedAccessException 321 print 'testify warning: cannot delete:', targetPath 322 if libName == 'Cobra.Core.dll' or File.exists(libName) 323 if Utils.isRunningOnUnix 324 File.copy(Path.combine(saveDir, libName), targetPath) 325 else 326 try 327 File.copy(Path.combine(saveDir, libName), targetPath, true) 328 catch IOException 329 print 'testify warning: cannot copy or copy over:', targetPath 305 _copyLibs(saveDir, dirName) 330 306 Directory.setCurrentDirectory(dirName) 331 307 try 332 setUpScript = if(Utils.isRunningOnUnix, 'testify-set-up', 'testify-set-up.bat') 333 if File.exists(setUpScript) 334 print '* * * * Running [setUpScript]' 335 process = Process() 336 process.startInfo.fileName = setUpScript 337 setUpOutput = process.runAndCaptureAllOutput 338 if process.exitCode <> 0 339 print 'TESTIFY WARNING: Script [setUpScript] exited with error code [process.exitCode]' 340 print 'begin output' 341 print setUpOutput.trim 342 print 'end output' 308 _runSetupScript 343 309 paths = List<of String>(Directory.getFiles('.')) 344 310 paths.addRange(Directory.getDirectories('.') to passthrough) 345 311 paths.sort … … 359 325 finally 360 326 _statusWriter.dedent 361 327 _testifyFlush 328 329 def _copyLibs(saveDir as String, dirName as String) 330 """ 331 Copy Cobra Libs etc to sources directory if platform needs it. 332 _cleanDir will remove these later 333 """ 334 if _runtimePlatform(nil) <> 'clr' # other platforms use a Path instead of dll in cwd 335 return 336 # if CobraCore.isRunningMono # On Mono, we use MONO_PATH instead of copying the Cobra.Core.dll around 337 # return 338 libNames = ['Cobra.Core.dll'] 339 for libName in libNames 340 # Ideally, the file operations below would not be wrapped with try...catch and a 341 # 'warning' because if the current library files cannot be copied into the test 342 # directory, then the test is not valid. However, Windows has problems with 343 # deleting and/or copying over Cobra.Core.dll right after it has been used. This 344 # comes up on Tests\340-contracts whose testify.kv config file specifies two 345 # runs of the directory. 346 targetPath = Path.combine(dirName, libName) 347 if File.exists(targetPath) 348 if Utils.isRunningOnUnix 349 File.delete(targetPath) 350 else 351 # Windows is just too damn picky... 352 try 353 File.delete(targetPath) 354 catch UnauthorizedAccessException 355 print 'testify warning: cannot delete:', targetPath 356 if libName == 'Cobra.Core.dll' or File.exists(libName) 357 if Utils.isRunningOnUnix 358 File.copy(Path.combine(saveDir, libName), targetPath) 359 else 360 try 361 File.copy(Path.combine(saveDir, libName), targetPath, true) 362 catch IOException 363 print 'testify warning: cannot copy or copy over:', targetPath 362 364 365 def _runtimePlatform(options as OptionValues?) as String 366 if not options, options = .testifyOptions 367 rtp = if(options['back-end']=='none', CobraCore.runtimePlatform, options['back-end'] to String) 368 return rtp 369 370 def _runSetupScript 371 setUpScript = if(Utils.isRunningOnUnix, 'testify-set-up', 'testify-set-up.bat') 372 if File.exists(setUpScript) 373 print '* * * * Running [setUpScript]' 374 process = Process() 375 process.startInfo.fileName = setUpScript 376 setUpOutput = process.runAndCaptureAllOutput 377 if process.exitCode <> 0 378 print 'TESTIFY WARNING: Script [setUpScript] exited with error code [process.exitCode]' 379 print 'begin output' 380 print setUpOutput.trim 381 print 'end output' 382 363 383 def testifyFile(baseName as String) as int 364 384 save = _failureCount 365 385 _firstAttempt = true … … 423 443 if inLineMessages.count > 0 # hasInlineMessages 424 444 return _testifyInlineMessages(inLineMessages, expectingError, 425 445 compilerVerbosity, [baseName], options, verbose ) 426 446 427 447 if firstLineInsensitive.startsWith('#.error.') 428 448 # deprecated: put errors on the lines where they occur. the "hasInlineMessages" code above will detect them. 429 449 # Note that errors that are only detected by the backend C# compiler are not detectable by testify … … 471 491 continue 472 492 473 493 if firstLineInsensitive.startsWith('#.require.') 474 rtPlatform = if(options['back-end']=='none', CobraCore.runtimePlatform, options['back-end'])494 rtPlatform = _runtimePlatform(options) 475 495 what = firstLineInsensitive[10:] 476 496 branch what 477 497 on 'mono' … … 714 734 715 735 def _testifyStd(compilerVerbosity as int, fileNames as IList<of String>, 716 736 options as OptionValues, verbose as bool) as int 717 c = Compiler(compilerVerbosity, _cachedTestifyModules, commandLineArgParser=_cl.argParser)718 737 try 738 c = Compiler(compilerVerbosity, _cachedTestifyModules, commandLineArgParser=_cl.argParser) 719 739 c.testifyFilesNamed(fileNames, options, _resultsWriter to !, verbose) 720 740 catch StopCompilation 721 741 .failed … … 768 788 769 789 print .bar 770 790 771 _cachedTestifyModules = for mod in c.modules where mod inherits AssemblyModule get mod791 #_cachedTestifyModules = for mod in c.modules where mod inherits AssemblyModule get mod 772 792 773 793 return 1 774 794 -
Source/Enums.cobra
10 10 var _storageType as IType? 11 11 var _nativeType as NativeType? 12 12 var _needScanNativeType as bool 13 13 var _isUsedAsSet = false # flag if multi member initted 14 14 15 cue init(parent as IParentSpace?, token as IToken, idToken as IToken, name as String, isNames as List<of String>, attribs as AttributeList, storageTypeNode as ITypeProxy?, docString as String, enumMembers as List<of EnumMember>) 15 16 base.init(parent, token, name, isNames, docString) 16 17 _idToken = idToken … … 27 28 _nativeType = nativeType 28 29 assert .compiler 29 30 .backEnd.setUnderlyingType(this) # sets _storageTypeNode 30 #_storageTypeNode = ClrTypeProxy(Enum.getUnderlyingType((_nativeType to ClrNativeType).backEndType)) # TODO: fix native31 31 _needScanNativeType = true 32 32 33 33 def _scanNativeType … … 84 84 get typeForReceiver as IType is override 85 85 return this 86 86 87 pro isUsedAsSet from var 88 87 89 def _bindInt is override 88 90 base._bindInt 89 91 for attrib in .attributes, attrib.bindInt … … 119 121 body 120 122 _parentBox = newBox 121 123 122 124 123 125 class EnumMember 124 126 is partial 125 127 inherits NamedNode 126 128 implements IMember 127 129 """ 128 130 Holds the name and (optionally) value of a member of an EnumDecl. 131 One of these is what an enum instance will be set to. 129 132 """ 130 133 131 134 var _value as int? -
Source/Statements.cobra
667 667 if isStreamOfKeyValue, kvRHS = ['key', 'value'] 668 668 if _var.type inherits Box 669 669 if not isStreamOfKeyValue 670 isStreamOfKeyValue = (_var.type to Box).isIndirectConstructionOf(.compiler.libraryType('System.Collections.Generic.KeyValuePair<of,>') to Box) 670 #isStreamOfKeyValue = (_var.type to Box).isIndirectConstructionOf(.compiler.libraryType('System.Collections.Generic.KeyValuePair<of,>') to Box) # compiler.KeyValuePairOfType 671 isStreamOfKeyValue = (_var.type to Box).isIndirectConstructionOf(.compiler.keyValuePairOfType) 671 672 if isStreamOfKeyValue, kvRHS = ['key', 'value'] 672 673 if not isStreamOfKeyValue 673 674 isStreamOfKeyValue = (_var.type to Box).isDescendantOf(.compiler.libraryType('Cobra.Core.BasePair') to Box) … … 976 977 base._bindImp 977 978 if .destination 978 979 .destination.bindImp 979 t = .destination.type 980 if not t.isDynamicOrPassThrough and not t.isDescendantOf(.compiler.libraryType('System.IO.TextWriter')) # TODO-BACKEND981 . recordError('Invalid destination of type "[t.name]" for "print". Use a TextWriter or subclass thereof instead.')980 t = .destination.type 981 if not t.isDynamicOrPassThrough 982 .compiler.backEnd.validatePrintDestType(t to !, this) 982 983 983 984 984 985 class PrintStmt inherits AbstractPrintStmt is partial … … 1403 1404 else if _typeNode 1404 1405 _typeNode.bindImp 1405 1406 _type = _typeNode.realType # CC: combine with above: _type = _typeNode.bindImp.realType 1406 _sharpHelperName = '_lh_catch_[.compiler.curBox.makeNextPrivateSerialNumber]' 1407 _sharpHelperName = '_lh_catch_[.compiler.curBox.makeNextPrivateSerialNumber]' # TODO rename to HelperName or beHelperName - common to both backends 1407 1408 _block.bindImp 1408 1409 1409 1410 … … 1455 1456 if _expr.definition inherits BoxEvent 1456 1457 reason = 'it is an event, not an exception. Either throw an exception or use "raise" on the event instead of "throw"' 1457 1458 else 1458 reason = 'it does not inherit from Exception'1459 reason = 'it does not inherit from [exceptionType.name] (Exception)' 1459 1460 .throwError('Cannot throw "[_expr.type.name]" because [reason].') 1460 1461 .compiler.curCodeMember.hasThrowStmt = true 1461 1462 … … 1807 1808 if isPair 1808 1809 memberNames = ['a', 'b'] 1809 1810 else if not isPair 1810 isPair = (_source.type to Box).isIndirectConstructionOf(.compiler. libraryType('System.Collections.Generic.KeyValuePair<of,>') to Box)1811 isPair = (_source.type to Box).isIndirectConstructionOf(.compiler.keyValuePairOfType) 1811 1812 if isPair, memberNames = ['key', 'value'] 1812 1813 1813 1814 if isPair -
Source/Cobra.Core/Java/CobraCore.java
18 18 19 19 public class CobraCore { 20 20 21 // Release info 22 public static final String versionDescription = "svn-post-0.8.0(jvm)"; 23 //textual description of the version such as "X.Y.Z" or "svn-post-X.Y.Z". 24 public static final boolean isOfficialRelease = false; 25 public static final int version = CobraCore.toVersionNumber(0,8,0); // Version# - Version(0, 8, 0) 26 public static final int releaseNum = 25; 27 21 28 public static Boolean _willCheckInvariant = true; 22 29 public static Boolean _willCheckRequire = true; 23 30 public static Boolean _willCheckEnsure = true; … … 47 54 System.exit(exitCode); 48 55 } 49 56 50 public static String newLine = System.getProperty("line.separator");57 public static final String newLine = System.getProperty("line.separator"); 51 58 //""" Returns the newline for the current environment/platform. """ 52 59 53 60 // Property StringMaker techStringMaker … … 76 83 """ 77 84 Run all Cobra `test` sections in all assemblies using reflection to locate them. 78 85 """ 79 if CobraImp.showTestProgress, listener = Cobra. Lang.Test.TextWriterListener(Console.out)80 else, listener = Cobra. Lang.Test.TextWriterOnlyOnFailureListener(Console.out)81 tr = Cobra. Lang.Test.TestRunner(listener)86 if CobraImp.showTestProgress, listener = Cobra.Core.Test.TextWriterListener(Console.out) 87 else, listener = Cobra.Core.Test.TextWriterOnlyOnFailureListener(Console.out) 88 tr = Cobra.Core.Test.TestRunner(listener) 82 89 tr.runAllTests 83 90 if listener.testFailures, CobraCore.exit(1) 84 91 */ … … 103 110 // TODO: CobraImp.printLine( " See also: http://cobra-language.com/docs/debugging"); 104 111 CobraImp.printLine(); 105 112 } 113 114 public static final int toVersionNumber( int major, int minor, int rev) { 115 assert major >= 0 && major < 100 : "Major between 0 and 99"; 116 assert minor >= 0 && minor < 100 : "Minor between 0 and 99"; 117 assert rev >= 0 && rev < 100 : "Rev between 0 and 99"; 118 return major*1000 + minor*100 + rev; 119 } 106 120 } 107 121 -
Source/Cobra.Core/Java/CheckedExceptionWrapper.java
1 /* 2 * A Runtime Exception wrapping a Checked Exception. 3 * Compiler generates internally in 'throws' codegen and unwrapped in catch clauses codegen 4 * See JavaGenerator.cobra ThrowStmt.writeJavaDef and tryStmt.writeCatchBlocksJava 5 * 6 * Basically a Marker class for RuntimeException. 7 */ 8 9 package cobra.core; 10 11 12 public class CheckedExceptionWrapper extends RuntimeException { 13 14 public CheckedExceptionWrapper(String msg, Exception cause) { 15 super(msg, cause); 16 } 17 18 public Exception getCause() { 19 return (Exception) super.getCause(); 20 } 21 } 22 23 -
Source/Cobra.Core/Java/Delegate.java
8 8 * call myDelegate(arg0,...) -> d.invoke(arg0,...) 9 9 * (or no args: myDelegate() -> d.invoke() ) 10 10 * 11 * Doesnt support Generics.12 11 * This Implementation genericised on ReturnType 13 12 */ 14 13 … … 19 18 import java.util.*; 20 19 import java.lang.reflect.*; 21 20 21 22 22 public class Delegate<T> { 23 23 Class<T> returnType; // method returnType 24 24 Class<?>[] paramTypes; // Types of the method parameters … … 209 209 } 210 210 } 211 211 212 213 212 class TestDelegate { 214 213 215 214 public static void main(String... args) { -
Source/Cobra.Core/Java/mkjarAll
8 8 9 9 [ -d classes ] || mkdir classes 10 10 [ -d classes/cobra/lang ] && rm -rf classes/cobra/lang/* 11 EXCEPTIONS="AssertException.java InvariantException.java RequireException.java EnsureException.java NonNilCastException.java DynamicOperationException.java "12 javac -d classes SourceSite.java CobraImp.java CobraCore.java Delegate.java ${EXCEPTIONS} CobraDirectString.java11 EXCEPTIONS="AssertException.java InvariantException.java RequireException.java EnsureException.java NonNilCastException.java DynamicOperationException.java ExpectException.java CheckedExceptionWrapper.java" 12 javac -d classes SourceSite.java CobraImp.java CobraCore.java Delegate.java DelegateO.java Pair.java ${EXCEPTIONS} CobraDirectString.java 13 13 [ $? == 0 ] || exit 14 14 15 15 jar cvf ${CORE_RTL} -C classes . 16 16 [ $? == 0 ] || exit 17 17 cp ${CORE_RTL} ../.. 18 18 19 if [ ! -e PkgSig.class ]; then 19 #if [ ! -e PkgSig.class ]; then 20 # echo 'building PkgSig app' 21 # javac -cp . PkgSig.java 22 if [ ! -e PkgSig.jar ]; then 20 23 echo 'building PkgSig app' 21 javac -cp . PkgSig.java 24 PSIGDIR=pkgsig-classes 25 [ -d $PSIGDIR ] || mkdir $PSIGDIR 26 javac -cp . -d $PSIGDIR PkgSig.java 27 jar cvfe PkgSig.jar PkgSig -C $PSIGDIR . 22 28 fi 23 29 30 export CLASSPATH='PkgSig.jar' 24 31 echo 'Gen pkgSig file for '${CORE_RTL} 25 java -cp " .;${CORE_RTL}" PkgSig -j ${CORE_RTL} > ${CORE_RTL}.sig32 java -cp "${CLASSPATH};${CORE_RTL}" PkgSig -j ${CORE_RTL} > ${CORE_RTL}.sig 26 33 27 34 echo 'Gen pkgSig file for rt.jar' 28 java -cp .PkgSig -jrt > rt.jar.sig35 java PkgSig -jrt > rt.jar.sig 29 36 echo 'Gen pkgSig file for java.lang' 30 java -cp .PkgSig java.lang > java.lang.sig37 java PkgSig java.lang > java.lang.sig 31 38 echo 'Gen pkgSig file for java.util' 32 java -cp .PkgSig java.util > java.util.sig39 java PkgSig java.util > java.util.sig 33 40 34 echo 'copy .sig files to compiler directory'35 cp ${CORE_RTL}.sig rt.jar.sig java.util.sig java.lang.sig ../..41 #echo 'copy .sig files to compiler directory' 42 #cp ${CORE_RTL}.sig rt.jar.sig java.util.sig java.lang.sig ../.. -
Source/Cobra.Core/Java/read-me.text
14 14 compiler. Then we'll have a cobra to java compiler on the jvm. 15 15 16 16 Supporting that is this directory which provides 17 - stub code for a java cobra RTL (CobraLang.jar) 18 - A tool to generate descriptions for java library classes (Pkgsig files) 19 - a script to build and copy the various supporting pieces the java backend needs to the compiler Source directory. 17 - stub code for a java cobra RTL (CobraCore.jar) 18 - A tool (PkgSig.jar) to generate descriptions for java library classes (Pkgsig files) 19 - a script (mkjarAll) to build and copy the various supporting pieces the java backend needs to 20 the compiler Source directory. 20 21 21 22 22 The base java version supported is currently 1.6. 23 The base java version supported is currently 1.7. 24 (I started with 1.6, It may still work to some extent with that java version ) 23 25 This one specifically 24 java version "1. 6.0_24"25 Java(TM) SE Runtime Environment (build 1. 6.0_24-b07)26 Java HotSpot(TM) Client VM (build 19.1-b02, mixed mode, sharing)26 java version "1.7.0_04" 27 Java(TM) SE Runtime Environment (build 1.7.0_04-b22) 28 Java HotSpot(TM) Client VM (build 23.0-b21, mixed mode, sharing) 27 29 28 This will move up to at least 1.7 as the port/code generation progresses29 and will probably stabilise on 1.8eventually.30 This will move up to 1.8 as the port/code generation progresses and 1.8 gets more useful 31 capabilities. I will probably stabilise on 1.8 as the base eventually. 30 32 31 33 = Caveats = 32 34 33 35 This backend is under development 34 - for any but the most trivial uses it is effectivelybroken.35 There are largeamounts of (java) code generation missing.36 It spewsdebug/diagnostic/conversational/status info all the time.36 - for any but the most trivial uses it is broken. 37 There are amounts of (java) code generation missing. 38 It will possibly spew debug/diagnostic/conversational/status info all the time. 37 39 There are bugs. 38 40 There are missing capabilities. 39 41 There are horrible bits … … 48 50 This is a source modification - no installation support. 49 51 Run the compiler out of the cobra source directory 50 52 51 I'm developing this in Windows (XP) - it may or may not be working under mono 52 on any other platform. 53 I'm now developing this in Windows 7 (I started in Windows XP but a System crash caused a 54 correction of that) 55 - it may or may not be working under mono on any other platform. 53 56 54 This is (the/an) initial devt snapshot. 55 Its working(ish) but rough, 57 This is a devt snapshot. Its working(ish) but rough, 56 58 Things will change 57 59 58 60 - What To Do - … … 61 63 happens, good for you. 62 64 This is what you will need to do 63 65 64 1) If necessary 65 Apply the patches providing the java backend to the Source tree 66 - get the modified compiler subsequently rebuilt 67 68 As on Nov 2011 (and touch wood) all the jvm patches posted have been 69 applied to the code repository source so it should be enough to pull down the latest compiler source and build it. 66 1) As of Nov 2011 (and touch wood) all the jvm patches posted have been 67 applied to the code repository source so it should be enough to pull down the latest 68 compiler source and build it. 70 69 71 Patchshould be on the cobra website http://cobra-language.com70 Any unapplied patches should be on the cobra website http://cobra-language.com 72 71 Ticket 275: More support for java backend 73 72 http://cobra-language.com/trac/cobra/ticket/275 74 73 75 2) Ensure ja va and javac executables (1.6or later) are in your PATH and work.74 2) Ensure jar, java and javac executables (1.7 or later) are in your PATH and work. 76 75 77 3) Go into this dir and run ./mk JarAll76 3) Go into this dir and run ./mkjarAll 78 77 ( its a bash script - you will need bash or to port the script, 79 or run the command contents manually) - this builds the cobra RTL and 80 package signature tool and uses that to create the class description info and copies 81 them to the right places in the cobra source tree) 78 or run the command contents manually) - this compiles the (minimal) cobra RTL 79 ( java source to a jarfile) and builds the package signature tool and uses that 80 to create the class description info (.sig files) and copies 81 them to the right places (Source dir alongside cobra.exe) 82 in the cobra source tree) 83 82 84 4) find a test cobra script and compile it 85 a good smoke test is Tests/100-basic/001.cobra 83 86 84 87 - How to build and run a cobra program generating a java app - 85 88 … … 103 106 104 107 105 108 - Status - 109 110 Theres a more complete timeline tracking the uploaded patches on the ticket 111 Ticket 275: More support for java backend 112 http://cobra-language.com/trac/cobra/ticket/275 113 106 114 Jul-2011 - We can build and run simple (single file) programs. 107 115 (Up to about 032-* of the files in Tests/100-*. 108 116 Maybe more but thats what I've tried so far) … … 112 120 (Up to about 080-* of the files in Tests/100-*) 113 121 Have properties and Indexers, Casting, reading Commandline ... 114 122 123 Mar-2012 - Minor cleanups and Testify compiler testcase harness working 124 125 10-Aug-2012 try and sync a major push for a cobra release. 126 Fix testcase regressions noted from testify runs 127 - fix issue with multiple internal Testify invocations 128 - fix incorrect conversion cobra to java name forms in multiple namespace generics 129 - get indexing ([]) working mapping to indexer getters/setters 130 - type inference in foreach and support iterators (java vs enumerators in CLR) 131 - In pkgSig correct some bad name handling for nested classes in rt.jar 132 - extend java RTL some more support codegen 133 Support try/catch and throw (minimally) 134 Modify this to correctly autowrap thrown checked Exceptions into runtimeExceptions and 135 unwrap, catch or propagate correctly in catch clauses. 136 Methods throwing checked Exceptions still need to be explicitly wrapped 137 138 Pull backEnd Abstract base classs out to its own file 139 140 Start enum support 141 (Up to about 301-* of the files in Tests/100-*) 142 143 Sep-2012 144 Complete enum support (Tests/100-*/302-334a*). 145 setup so enums built using the ctor syntax with multiple initial values( Enum(val1,val2) internally 146 use ( in java) and EnumSet. Codegen will break if you try and treat this as a sigle enum value and a multivalue set. 147 148 Add inbuilt support for testing for an enum value membership in a enum variable (set) using 149 'value in enumSet' 150 see test (310j-* and 334a-*) 151 152 Modify build so creates an executable jar file from classfiles(and runs it). 153 This means all classpath dependencies must be listed correctly in the manifest file 154 (see JavaGenerator.cobra _javaTool) 155 Correct handling for multiple cobra files and cobra file containing multiple classes. 156 157 Fixup codegen for print and print redirection. Add necessary RTL support 158 159 BackEnd KeyValuePair Type, support Pair (since compiler now uses it) 160 161 162 TODO 163 load annotations into .sig files and use for things like nullability 164 PkgSig.java and handling in JarSig.cobra 165 166 Support native static methods on box instances 167 installJvmNativeMethods in ScanJvmType 168 169 Handling ( wrap and convert) calls to methods in java libs generating 170 non RuntimeExceptions. 171 115 172 - Other - 116 173 117 174 Post issues/questions/comments/etc if you have them to Cobra Programming Language Forum -
Source/Cobra.Core/Java/DynamicOperationException.java
29 29 protected Object _obj; 30 30 protected String _name; 31 31 Class _type; 32 Exception _exc; 32 33 33 34 public UnknownMemberException(Object obj, String name, Class type) { this(obj, name, type, null);} 34 35 public UnknownMemberException(Object obj, String name, Class type, Exception cause) … … 39 40 this._obj = obj; 40 41 this._name = name; 41 42 this._type = type; 43 this._exc = cause; 42 44 } 43 45 } 44 46 47 48 class CannotInTypeException extends UnknownMemberException 49 { 50 public CannotInTypeException(Object obj, String name, Class type) 51 { 52 super(obj, name, type, null); 53 } 54 55 public CannotInTypeException(Object obj, String name, Class type, Exception innerExc) 56 { 57 super(obj, name, type, innerExc); 58 } 59 } 60 61 45 62 /* 46 63 * Missing, unreadable Member Exceptions 47 64 * / … … 78 95 base.init(obj, name, type, innerExc) 79 96 80 97 81 class CannotInTypeException inherits UnknownMemberException82 98 83 # CC: axe init()s84 85 cue init(obj as Object, name as String, type as Type)86 .init(obj, name, type, nil)87 88 cue init(obj as Object, name as String, type as Type, innerExc as Exception?)89 base.init(obj, name, type, innerExc)90 91 92 99 class CannotCompareException inherits DynamicOperationException 93 100 94 101 var _a -
Source/Cobra.Core/Java/PkgSig.java
799 799 String tStr = t.toString(); 800 800 //System.out.printf("### %-50s \n", tStr); 801 801 tStr = tStr.replace("java.util.Map.java.util.Map", "java.util.Map"); // EntrySets screwed for some reason 802 802 tStr = tStr.replace("java.nio.file.WatchEvent.java.nio.file.WatchEvent", "java.nio.file.WatchEvent"); 803 tStr = tStr.replace("java.nio.file.DirectoryStream.java.nio.file.DirectoryStream", "java.nio.file.DirectoryStream"); 804 803 805 tStr = tStr.replace("class ", ""); // sometimes puts on leading 'type ' 804 806 tStr = tStr.replace("interface ", ""); 805 807 // possibly others - enum ? -
Source/Cobra.Core/Java/mkjar
7 7 8 8 [ -d classes ] || mkdir classes 9 9 [ -d classes/cobra/lang ] && rm -rf classes/cobra/lang/* 10 EXCEPTIONS="AssertException.java InvariantException.java RequireException.java EnsureException.java NonNilCastException.java DynamicOperationException.java "11 javac - d classes SourceSite.java CobraImp.java CobraCore.java Delegate.java ${EXCEPTIONS} CobraDirectString.java10 EXCEPTIONS="AssertException.java InvariantException.java RequireException.java EnsureException.java NonNilCastException.java DynamicOperationException.java ExpectException.java CheckedExceptionWrapper.java" 11 javac -Xlint:unchecked -d classes SourceSite.java CobraImp.java CobraCore.java Delegate.java DelegateO.java BasePair.java Pair.java ${EXCEPTIONS} CobraDirectString.java 12 12 [ $? == 0 ] || exit 13 13 14 14 jar cvf ${CORE_RTL} -C classes . 15 15 [ $? == 0 ] || exit 16 16 cp ${CORE_RTL} ../.. 17 17 18 #echo 'pkgSig for CobraCore.jar'19 java -cp " .;${CORE_RTL}" PkgSig -j ${CORE_RTL} > ${CORE_RTL}.sig18 echo 'Gen pkgSig for CobraCore.jar' 19 java -cp "PkgSig.jar;${CORE_RTL}" PkgSig -j ${CORE_RTL} > ${CORE_RTL}.sig 20 20 cp ${CORE_RTL}.sig ../.. 21 21 22 22 -
Source/Cobra.Core/Java/SourceSite.java
8 8 9 9 public class SourceSite { 10 10 /* 11 * See Cobra. Lang.SourceSite.cobra11 * See Cobra.Core.SourceSite.cobra 12 12 * 13 13 * A SourceSite instance captures the source location of a statement or expression, as in the 14 14 * filename, line number, declaring class name, method name and run-time object. The Cobra -
Source/Cobra.Core/Java/mkjarOnly
7 7 8 8 [ -d classes ] || mkdir classes 9 9 [ -d classes/cobra/lang ] && rm -rf classes/cobra/lang/* 10 EXCEPTIONS="AssertException.java InvariantException.java RequireException.java EnsureException.java NonNilCastException.java DynamicOperationException.java "11 javac - d classes SourceSite.java CobraImp.java CobraCore.java Delegate.java ${EXCEPTIONS} CobraDirectString.java10 EXCEPTIONS="AssertException.java InvariantException.java RequireException.java EnsureException.java NonNilCastException.java DynamicOperationException.java ExpectException.java CheckedExceptionWrapper.java" 11 javac -Xlint:unchecked -d classes SourceSite.java CobraImp.java CobraCore.java Delegate.java DelegateO.java Pair.java ${EXCEPTIONS} CobraDirectString.java 12 12 [ $? == 0 ] || exit 13 13 jar cvf ${CORE_RTL} -C classes . 14 14 [ $? == 0 ] || exit 15 15 cp ${CORE_RTL} ../.. 16 16 17 17 #echo 'pkgSig for '${CORE_RTL} 18 #java -cp " .;${CORE_RTL}" PkgSig -j ${CORE_RTL} > ${CORE_RTL}.sig18 #java -cp "PkgSig.jar;${CORE_RTL}" PkgSig -j ${CORE_RTL} > ${CORE_RTL}.sig 19 19 #cp ${CORE_RTL}.sig ../.. 20 20 21 21 -
Source/Cobra.Core/Java/NonNilCastException.java
39 39 40 40 /* ## Misc exceptions 41 41 42 class ExpectException inherits Exception42 class ExpectException extends RuntimeException 43 43 44 44 cue init(expectedExceptionType as Type, actualException as Exception?) 45 45 base.init -
Source/Cobra.Core/Java/DelegateO.java
1 /* 2 * Non genericised Delegate Impl (to be same as .Net) 3 * Uses genericised version code 4 * Will need codegen support for casting invoke type 5 */ 6 7 package cobra.core; 8 9 import java.lang.*; 10 import java.io.*; 11 import java.util.*; 12 13 14 // Tmp placeholder 15 public class DelegateO { 16 Delegate<Object> delTgt; 17 Class returnType; 18 19 public DelegateO(Class returnType, Class... paramTypes) { 20 //public Delegate(Class... paramTypes) { 21 this.delTgt = new Delegate<Object>(Object.class, paramTypes); 22 this.returnType = returnType; 23 } 24 25 public void init(Object target, String methodName){ 26 this.delTgt.init(target, methodName); 27 } 28 29 public void init(Class targetClass, String methodName){ 30 this.delTgt.init(targetClass, methodName); 31 } 32 public Object invoke(Object... arguments) { 33 return delTgt.invoke(arguments); 34 } 35 36 public void add(Object target, String methodName){ 37 delTgt.add(target, methodName); 38 } 39 40 public void remove(Object target, String methodName){ 41 delTgt.remove(target, methodName); 42 } 43 44 public void clear(){ 45 delTgt.clear(); 46 } 47 } 48 -
Source/Cobra.Core/Java/Pair.java
1 /* 2 * Pair.java 3 * 4 * Provides simple pair class hierarchy to satisfy RT requirements for compiler 5 * Rough translation based on cobra implementation for .Net 6 */ 7 8 package cobra.core; 9 import java.util.*; 10 11 public class Pair<TA extends Comparable<Object>, TB extends Comparable<Object>> extends BasePair 12 implements Iterable, Comparator<Pair<TA, TB>> { 13 14 final TA a; 15 final TB b; 16 17 /* test 18 p = Pair<of int, int>(1, 2) 19 assert p.a == 1 and p.b == 2 20 assert p[0] == 1 and p[1] == 2 21 assert p.toString == 'Pair(1, 2)' 22 expect IndexOutOfRangeException, assert p[2] == 3 23 */ 24 public Pair(TA a, TB b) { 25 super(); 26 this.a = a; 27 this.b = b; 28 } 29 30 public Object getItem(int i) { 31 if (i == 0) 32 return a; 33 if (i == 1) 34 return b; 35 throw new IndexOutOfBoundsException("Index " + i + " is out of range for a pair (0-1)."); 36 } 37 38 @Override 39 public String toString() { 40 String sa = CobraCore.toTechString(this.a); 41 String sb = CobraCore.toTechString(this.b); 42 return String.format("%s (%s, %s)", this.getClass().getName(), sa, sb); 43 } 44 45 //Comparator 46 public int compare(Pair<TA, TB> t, Pair<TA, TB> p) { 47 /* test 48 assert Pair<of String, int>('a', 3) < Pair<of String, int>('b', 4) 49 assert Pair<of int, int>(2, 3) > Pair<of int, int>(2, 2) 50 assert Pair<of String?, String?>(nil, 'a') < Pair<of String?, String?>(nil, 'b') 51 assert Pair<of int?, int?>(nil, nil) < Pair<of int?, int?>(1, 1) 52 */ 53 int cmp = _compare(t.a, p.a); 54 if (cmp == 0) 55 cmp = _compare(t.b, p.b); 56 return cmp; 57 } 58 59 public boolean equals(Pair<TA, TB> p) { 60 /*test 61 assert Pair<String>('a', 'b').equals(Pair<String>('a', 'b')) 62 assert Pair<String>('a', 'b').equals(Pair<String>('a', 'c')) 63 */ 64 return p.a == this.a && p.b == this.b; 65 } 66 67 //Comparable 68 public int compareTo(Pair<TA, TB> obj) { 69 if (obj == null) return 1; 70 if (obj == this) return 0; 71 if (obj.getClass() == this.getClass() ) { 72 int comp_a = this.a.compareTo(obj.a); 73 if (comp_a ==0 ) 74 return this.b.compareTo(obj.b); 75 return comp_a; 76 } 77 return -1; 78 } 79 80 @Override 81 @SuppressWarnings("unchecked") 82 public boolean equals(Object obj) { 83 /*test 84 assert Pair<String, String>('a', 'b') == Pair<String, String>('a', 'b') 85 assert Pair<String, String>('a', 'b') <> Pair<String, String>('a', 'c') 86 */ 87 if (obj == null) return false; 88 if (obj == this) return true; 89 if (obj.getClass() == this.getClass() ) { 90 Pair<TA,TB> p = (Pair<TA,TB>) obj; 91 return p.a == this.a && p.b == this.b; 92 } 93 return false; 94 } 95 96 97 @Override 98 public int hashCode() { 99 /*test 100 d = { 101 Pair<of int, String>(1, 'one'): 'one', 102 Pair<of int, int>(2, 2): 'two', 103 Pair<of dynamic, dynamic>(nil, nil): 'nil', 104 } 105 assert d[Pair<of int, String>(1, 'one')] == 'one' 106 assert d[Pair<of int, int>(2, 2)] == 'two' 107 assert d[Pair<of dynamic, dynamic>(nil, nil)] == 'nil' 108 */ 109 // same as CobraCore.HashCodeUtils.combine( (this.a == null) ? 0 : this.a.hashCode(), 110 // (this.b == null) ? 0 : this.b.hashCode() ); 111 int ha = ((this.a == null) ? 0 : this.a.hashCode()); 112 int hb = ((this.b == null) ? 0 : this.b.hashCode()); 113 int hc = 37; 114 hc = hc * 23 + ha; 115 hc = hc * 23 + hb; 116 return hc; 117 } 118 119 public Iterator<?> iterator() { 120 /*test 121 assert (for s in Pair<of String, String>('a', 'b') get s) == ['a', 'b'] 122 */ 123 return new PairIterator(); 124 } 125 126 class PairIterator implements Iterator { 127 private int ofst =0; 128 129 public boolean hasNext() { 130 return (this.ofst < 2); 131 } 132 133 public Object next() { 134 Object ret; 135 switch (this.ofst) { 136 case 0: ret= a; break; 137 case 1: ret = b; break; 138 default: 139 throw new NoSuchElementException("Pair: no element after .a and .b") ; 140 } 141 this.ofst++; 142 return ret; 143 144 } 145 146 public void remove() { 147 throw new UnsupportedOperationException("Pair: no remove operation"); 148 } 149 150 } 151 152 } -
Source/Cobra.Core/Java/CobraImp.java
2 2 * CobraImp.java 3 3 * Native java support code RTL 4 4 * 5 * Currently only stubbed, minimal native code impl. 6 * see also Cobra.Lang/NativeExtern.cobra 5 * Currently only stubbed, minimal native code implementation. 7 6 */ 8 7 package cobra.core; 9 8 10 9 import java.lang.*; 11 10 import java.util.*; 11 import java.io.PrintStream; 12 import java.io.PrintWriter; 13 import java.io.Writer; 14 import java.io.IOException; 12 15 13 16 14 17 public class CobraImp { … … 22 25 return sb.toString(); 23 26 } 24 27 public String makeString(String s) { 28 if (s == null) return "nil"; 25 29 return s; 26 30 } 27 31 public String makeString(int i) { … … 34 38 35 39 public String makeString(Object... args) { 36 40 StringBuilder sb = new StringBuilder(); 37 for (Object arg : args) 38 sb.append(arg.toString()); 41 for (Object arg : args) { 42 if (arg == null) 43 sb.append("nil"); 44 else 45 sb.append(arg.toString()); 46 } 39 47 return sb.toString(); 40 48 } 41 49 } 50 42 51 //static public StringMaker _printStringMaker = new StringMaker(); 43 52 static public SimpleStringMaker _printStringMaker = new SimpleStringMaker(); 44 static public SimpleStringMaker _techStringMaker = new SimpleStringMaker(); 45 53 static public SimpleStringMaker _techStringMaker = new SimpleStringMaker(); 54 55 //static private Deque<PrintStream> _printToStack; 56 // above is Stack<PrintStream> TODO: support as PrintWriter 57 static private Deque<Writer> _printToStack; 58 static private PrintWriter outPW; 59 static private boolean _prtErr = false; 46 60 static { 47 // _printToStack = new Stack<TextWriter>(); 48 // PushPrintTo(Console.Out); 61 _printToStack = new ArrayDeque<Writer>(); 62 outPW = new PrintWriter(System.out, true); 63 pushPrintTo(outPW); 49 64 // _printStringMaker = new PrintStringMaker(); 50 65 // _techStringMaker = new TechStringMaker(); 51 66 // PromoteNumerics = NumericTypeInfo.PromoteNumerics; 52 67 } 53 68 69 static public Writer getPrintDestination() { 70 return _printToStack.peek(); 71 } 72 73 // Convenience for System.out 74 static public void pushPrintTo(PrintStream ps) { 75 Writer wr = new PrintWriter(ps, true); 76 pushPrintTo(wr); 77 } 78 79 static public void pushPrintTo(Writer wr) { 80 _printToStack.push(wr); 81 } 82 83 static public void popPrintTo() { 84 _printToStack.pop(); 85 } 86 87 static public void reset() { 88 _printToStack.clear(); 89 pushPrintTo(outPW); 90 } 91 92 static public void reset(Writer printDestination) { 93 _printToStack.clear(); 94 pushPrintTo(printDestination); 95 } 96 54 97 static public void printLine() { 55 //_printToStack.Peek().WriteLine(); 56 System.out.println(); 98 printLine(""); 99 //_printToStack.peek().println(); 100 //System.out.println(); 57 101 } 58 102 103 // Convenience for System.out 104 static public void printLine(PrintStream ps, String s) { 105 Writer wr = new PrintWriter(ps, true); 106 printLine(wr, s); 107 } 108 59 109 static public void printLine(String s) { 60 // _printToStack.Peek().WriteLine(s); 61 System.out.println(s); 110 printLine(_printToStack.peek(), s); 62 111 } 112 static public void printLine(Writer wr, String s) { 113 // We do this manually rather than using a PrintWriter so we can support use of a StringWriter 114 // which (of course) doesnt support the useful things like print and println... 115 try { 116 wr.write(s); 117 wr.write(System.lineSeparator()); 118 wr.flush(); 119 } 120 catch(IOException ioe) { 121 _prtErr = true; 122 } 123 //_printToStack.peek().println(s); 124 //System.out.println(s); 125 } 63 126 64 127 static public void printStop() { 65 128 } 66 129 130 // Convenience for System.out 131 static public void printStop(PrintStream ps, String s) { 132 Writer wr = new PrintWriter(ps, true); 133 printStop(wr, s); 134 } 135 67 136 static public void printStop(String s) { 68 // _printToStack.Peek().Write(s); 69 System.out.print(s); 137 printStop(_printToStack.peek(), s); 70 138 } 139 140 static public void printStop(Writer wr, String s) { 141 try { 142 wr.write(s); 143 } 144 catch(IOException ioe) { 145 _prtErr = true; 146 } 147 148 //_printToStack.peek().print(s); 149 //System.out.print(s); 150 } 151 /* 152 * Check if a Print error (IOException on print) has ocurred. 153 */ 154 static public boolean chkPrtError() { 155 return _prtErr; 156 } 71 157 158 /* 159 * Clear any print error setting, return the value that was set. 160 */ 161 static public boolean clearPrtError() { 162 boolean r = _prtErr; 163 _prtErr = false; 164 return r; 165 } 166 167 168 72 169 static public String makeString(String[] args) { 73 170 return _printStringMaker.makeString(args); 74 171 } … … 77 174 return s; 78 175 } 79 176 177 /* 178 * Convenience ctors for literal Lists, Sets and Dicts 179 */ 80 180 @SuppressWarnings("unchecked") 81 181 static public <T> List<T> makeList(T... args) { 82 return new ArrayList<T>(Arrays.asList(args)); 182 //return new ArrayList<T>(Arrays.asList(args)); 183 return Arrays.asList(args); 83 184 } 84 185 85 186 /* … … 106 207 return d; 107 208 } 108 209 210 211 212 static public <T> T checkNotNull(T value, String msg) 213 { 214 // used for generic checking for non null arg, throws NPE if fails 215 // msg expected to contain info and name of item being checked. 216 if (/*cobra.core.CobraCore._willCheckNil && */ value == null) { 217 throw new NullPointerException(msg); 218 } 219 return value; 220 } 109 221 110 222 static public <T> T checkNonNil(Object obj, String sourceCode, T value, /* SourceSite */ String fileName, int lineNum, String className, String memberName, Object thiss) 111 223 { … … 285 397 286 398 static public boolean referenceEquals(Object o, Object o1) { return o == o1; } 287 399 400 static public boolean is(Object a, Object b) { 401 return CobraImp.referenceEquals(a, b); 402 } 403 404 static public boolean isNot(Object a, Object b) { 405 return !CobraImp.referenceEquals(a, b); 406 } 407 408 static public boolean is(Enum a, Enum b) { 409 if (a==null) return b==null; 410 if (b==null) return false; 411 // return a.equals(b); 412 return a == b; 413 } 288 414 415 /* 416 * ........ Support for inclusion testing: 'a in b' ................... 417 */ 418 419 static public boolean in(char a, String b) { 420 return b.indexOf(a) != -1; 421 } 422 423 static public boolean in(String a, String b) { 424 return b.indexOf(a) != -1; 425 } 426 /* static public boolean in(Object a, Collection b) { 427 return b.contains(a); 428 } 429 430 static public boolean in(Object a, Iterable b) { 431 for (Object item : b) { 432 if (item.equals(a) /*equals(item, a)* /) 433 return true; 434 } 435 return false; 436 } 437 */ 438 439 // Covers collections including enums via EnumSet 440 static public <T> boolean in(T a, Collection<T> b) { 441 return b.contains(a); 442 } 443 444 static public <T> boolean in(T a, Iterable<T> b) { 445 for (T item : b) { 446 if (item.equals(a) /* Equals(item, a) */) 447 return true; 448 } 449 return false; 450 } 451 452 static public <T> boolean in(T a, Map<T,?> b) { 453 return b.containsKey(a); 454 } 455 456 /* LATER static public <T> bool InWithNullCheck(T a, Predicate<T> predicate) { 457 if (a == null) return false; 458 return predicate(a); 459 } 460 */ 461 static private boolean _noNestedIn = false; 462 463 static public boolean in(Object a, Object b) { 464 if (_noNestedIn) 465 throw new RuntimeException(String.format("_noNestedIn a=0$s, a.getClass=%1$s, b=%2$s, b.getClass=%3$s", 466 a, a==null ? "" : a.getClass().getName(), 467 b, b==null ? "" : b.getClass().getName() ) ); 468 _noNestedIn = true; 469 try { 470 if (b instanceof Collection) { // probably the most common case, check has to come before Iterable 471 return in(a, (Collection<?>)b); 472 } else if (b instanceof String) { 473 if (a instanceof String) { 474 return in((String)a, (String)b); 475 } if (a instanceof Character) { 476 return in( ((Character)a).charValue(), (String)b); 477 } else { 478 throw new CannotInTypeException(a, "a of `a in b`", a.getClass()); 479 } 480 } else if (b instanceof Iterable) { 481 return in(a, (Iterable<?>)b); 482 } else if (b instanceof Map) { 483 return in(a, (Map<?,?>)b); 484 } else if (b instanceof Enum) { 485 throw new RuntimeException("Cannnot test inclusion in an Enum, should be using EnumSet"); 486 } else { 487 throw new CannotInTypeException(b, "b of `a in b`", b.getClass()); 488 } 489 } finally { 490 _noNestedIn = false; 491 } 492 } 493 494 // support for throws codegen and checked exception wrapping 495 static public RuntimeException reThrow(Exception e) { 496 if (e instanceof RuntimeException) return (RuntimeException)e; 497 return new CheckedExceptionWrapper("Wrapping rethrown exception", e); 498 } 289 499 } 290 500 -
Source/Cobra.Core/Native.cs
326 326 return false; 327 327 } 328 328 329 static bool In(Object a, Enum b) { 330 Type et = b.GetType(); 331 if (! Enum.IsDefined(et, a)) 332 return false; 333 int v = Convert.ToInt32((Enum)a); 334 int v1 = Convert.ToInt32(b); 335 return (v1 & v) == v; 336 } 337 329 338 static public bool In<T>(T a, IList<T> b) { 330 339 return b.Contains(a); 331 340 } … … 364 373 return In(a, (IEnumerable)b); 365 374 } else if (b is IDictionary) { 366 375 return In(a, (IDictionary)b); 376 } else if (b is Enum) { 377 return In(a, (Enum)b); 367 378 } else { 368 379 throw new CannotInTypeException(b, "b of `a in b`", b.GetType()); 369 380 } -
Tests/100-basics/307-enums.cobra
1 #.require. clr 1 2 enum Medium 2 3 of uint64 3 4 air -
Tests/100-basics/902-access.cobra
1 #.require. clr 2 @platform clr 3 1 4 # access System 2 5 class Test 3 6 -
Tests/100-basics/900j-access.cobra
1 #.require. jvm 2 @platform jvm 3 class Test 4 5 def main is shared 6 assert CobraCore.releaseNum > 0 7 assert CobraCore.version > CobraCore.toVersionNumber(0,0,0) 8 #print CobraCore.commandLineArgs.size 9 assert CobraCore.commandLineArgs.size == 1 -
Tests/100-basics/902j-access.cobra
1 #.require. jvm 2 @platform jvm 3 4 # access System 5 class Test 6 7 def main 8 is shared 9 10 s as Java.Lang.Object = 'aoeu' 11 s = 'qwerty' 12 CobraCore.noOp(s) -
Tests/100-basics/140-break.cobra
1 #.require. clr 2 1 3 namespace Test 2 4 3 5 class Test -
Tests/100-basics/140j-break.cobra
1 #.require. jvm 2 @platform jvm 3 4 namespace Test 5 6 class Test 7 8 def main 9 is shared 10 11 x as int = 0 12 while x<10 13 if true, break 14 x += 1 # without 'if true' above java unreachable statement 15 assert x==0 16 17 x = 1 18 for i as int in 1 : 10 19 if true, break 20 x = i 21 assert x==1 22 23 s as String = 'a' 24 #l =['a', 'b'] # works 25 l = ArrayList<of String>() 26 l.add('a') 27 l.add('done') 28 for arg as String in l 29 CobraCore.noOp(arg) 30 if true, break 31 s = 'b' 32 assert s=='a' -
Tests/100-basics/150j-continue.cobra
1 #.require. jvm 2 # Modified for jvm for use of commandLine args and unreachable statement errors fm BackEnd 3 # on statements after a unconditional continue. 4 5 namespace Test 6 7 class Test 8 9 def main 10 is shared 11 12 count as int = 0 13 x as int = 0 14 while x<10 15 x += 1 16 if true, continue 17 count += 1 #unreachable statement 18 assert x==10 19 assert count==0 20 21 x = 1 22 for i as int in 1 : 10 23 x = i 24 if true, continue 25 count += 1 #unreachable statement 26 assert x==9 27 assert count==0 28 29 s as String = 'a' 30 for arg as String in CobraCore.commandLineArgs 31 CobraCore.noOp(arg) 32 s = 'b' 33 if true, continue 34 count += 1 #unreachable statement 35 assert s=='b' 36 assert count==0 -
Tests/100-basics/700j-print.cobra
1 #.require. jvm 2 @platform jvm 3 4 class Test 5 6 def main 7 is shared 8 print 1 9 print 1, 2, 3 10 print 1 stop 11 print 2 stop 12 print 3 13 print to System.out, 1, 2, 3 14 print to System.out 15 print 123 16 17 sw as StringWriter = StringWriter() 18 print to sw, 1, 2 19 assert sw.toString.trim == '1 2' 20 assert sw.toString <> sw.toString.trim 21 22 sw = StringWriter() 23 print to sw, 1, 2 stop 24 assert sw.toString == '1 2' 25 assert sw.toString == sw.toString.trim 26 27 sw = StringWriter() 28 print to sw 29 print 'sw.' 30 assert sw.toString == 'sw.'+ System.lineSeparator -
Tests/100-basics/200-try-raise.cobra
8 8 x as int = 0 9 9 10 10 try 11 throw Exception('') 12 x = 1 13 catch 14 pass 15 finally 16 x = 2 17 assert x==2 18 19 try 20 x = 1 21 throw Exception('') 22 x = 2 23 catch 24 pass 25 assert x==1 26 27 28 try 29 x = 0 30 throw Exception('') 31 catch e as Exception 32 x = 1 33 assert x==1 34 35 36 try 37 x = 0 38 throw Exception('') 39 catch nre as NullReferenceException 11 if true 12 throw Exception('') 13 x = 1 # unreachable code without if above (jvm) 14 catch nre as NonNilCastException # Platform independent exception 40 15 CobraCore.noOp(nre) 41 16 x = 1 42 17 catch -
Tests/100-basics/150-continue.cobra
1 #.require. clr 1 2 namespace Test 2 3 3 4 class Test -
Tests/100-basics/080j-inherits.cobra
17 17 assert 5 inherits int # .warning. always 18 18 assert 5 inherits Object # .warning. always 19 19 assert not (5 inherits String) # .warning. never 20 assert 5 inherits Integer # .warning. always20 assert 5 inherits Integer # .warning. never 21 21 22 22 assert true inherits bool # .warning. always 23 23 assert true inherits Object # .warning. always -
Tests/100-basics/900-access.cobra
1 #.require. clr 2 @platform clr 1 3 class Test 2 4 3 5 def main is shared -
Tests/100-basics/310j-enums-combine.cobra
1 enum Color 2 Red = 1 3 Green = 2 4 Blue = 4 5 6 class Test 7 8 def main is shared 9 c = Color(Red, Green) # in java BE c is actually an EnumSet 10 print c 11 .takeColor(Color(Red, Green)) 12 # TODO: Should have a qualified example. System.SomeEnum(MemberA, MemberB) and System.Something.SomeEnum(MemberA, MemberB) 13 14 # testing enum membership with 'in' operator 15 assert Color.Red in c 16 assert Color.Green in c 17 assert Color.Blue not in c 18 assert not Color.Blue in c 19 20 def takeColor(c as Color) is shared 21 pass -
Tests/100-basics/901j-access.cobra
1 #.require. jvm 2 @platform jvm 3 # access System 4 class Test 5 6 def main 7 is shared 8 9 nl as String = System.lineSeparator 10 CobraCore.noOp(nl) 11 12 nl1 = CobraCore.newLine 13 assert nl1 == System.lineSeparator -
Tests/100-basics/903j-use-structs.cobra
1 #.require. jvm 2 @platform jvm 3 4 namespace Test 5 class Test 6 def main 7 is shared 8 9 #now as Date = Date() #DateTime.now 10 #assert now.year > 2000 11 now as Calendar = Calendar.getInstance 12 assert now.get(Calendar.YEAR) > 2000 -
Tests/100-basics/200j-try-raise.cobra
1 #.require. jvm 2 # mods for unreachable code and using RuntimeException rather than Exception 3 namespace Test 4 5 class Test 6 7 def main 8 is shared 9 10 x as int = 0 11 12 try 13 if true, throw RuntimeException('') 14 x = 1 # unreachable code 15 catch nre as NonNilCastException # Cobra RTL exception 16 CobraCore.noOp(nre) 17 x = 1 18 catch 19 x = 2 20 assert x==2 21 22 try 23 x = 0 24 throw RuntimeException('') 25 catch RuntimeException # no variable 26 x = 1 27 assert x==1 28 29 loc as int 30 count as int 31 32 # rethrow an exception 33 try 34 loc = 1 35 try 36 loc = 2 37 throw RuntimeException('aoeu') 38 catch 39 loc = 3 40 throw 41 catch e as RuntimeException 42 loc = 4 43 assert e.message=='aoeu' 44 assert loc==4 45 46 # test that the "success" block executes when no exception is thrown 47 try 48 loc = 1 49 count = 1 50 catch 51 loc = 2 52 count += 1 53 success 54 loc = 3 55 count += 1 56 assert loc==3 57 assert count==2 58 59 60 # test the the "catch" block does not execute when no exception is throwd 61 try 62 loc = 1 63 count = 1 64 catch 65 loc = 2 66 count += 1 67 success 68 loc = 3 69 count += 1 70 finally 71 loc = 4 72 count += 1 73 assert loc==4 74 assert count==3 75 76 77 # test that the "success" block does not execute when an exception is throwd 78 try 79 loc = 1 80 count = 1 81 throw RuntimeException('') 82 catch 83 loc = 2 84 count += 1 85 success 86 loc = 3 87 count += 1 88 assert loc==2 89 assert count==2 90 91 92 # just for the heck of it 93 try 94 pass 95 catch 96 pass 97 success 98 pass 99 finally 100 pass 101 102 # this actually failed code gen at one point 103 try 104 pass 105 success 106 pass 107 finally 108 pass 109 110 # test that `success` and `finally` execute even with a `return` in the try part 111 .reset 112 .returnInTry 113 assert not _didCatch and _didSuccess and _didFinally 114 115 # test that `finally` executes even with an exception 116 .reset 117 expect RuntimeException, .throwInSuccess 118 assert not _didCatch and _didSuccess and _didFinally 119 120 var _didCatch as bool is shared 121 var _didSuccess as bool is shared 122 var _didFinally as bool is shared 123 124 def reset is shared 125 _didCatch = _didSuccess = _didFinally = false 126 127 def returnInTry is shared 128 try 129 return # test that a return statement here doesn't side-step `success` or `finally` 130 catch 131 _didCatch = true 132 throw 133 success 134 _didSuccess = true 135 finally 136 _didFinally = true 137 138 def throwInSuccess is shared 139 try 140 pass 141 success 142 _didSuccess = true 143 throw Exception('aoeu') # test that an exception here doesn't side-step `finally` 144 finally 145 _didFinally = true -
Tests/100-basics/334a-enums-comma.cobra
1 # comma sep syntax, platform independant inclusion test 2 enum Color 3 Red = 1, Green = 2, 4 Blue = 4 5 6 class Test 7 8 def main is shared 9 c = Color(Red, Green) 10 print c 11 .takeColor(Color(Red, Green)) 12 # TODO: Should have a qualified example. System.SomeEnum(MemberA, MemberB) and System.Something.SomeEnum(MemberA, MemberB) 13 14 # testing enum membership with 'in' 15 assert Color.Red in c 16 assert Color.Green in c 17 assert Color.Blue not in c 18 assert not Color.Blue in c 19 20 def takeColor(c as Color) is shared 21 pass -
Tests/100-basics/700-print.cobra
1 #.require. clr 2 @platform clr 3 1 4 class Test 2 5 3 6 def main -
Tests/100-basics/310-enums-combine.cobra
1 #.require. clr 2 @platform clr 3 1 4 enum Color 2 5 Red = 1 3 6 Green = 2 -
Tests/100-basics/901-access.cobra
1 #.require. clr 2 @platform clr 1 3 # access System 2 4 class Test 3 5 -
Tests/100-basics/903-use-structs.cobra
1 #.require. clr 2 @platform clr 3 1 4 namespace Test 2 5 class Test 3 6 def main -
Tests/100-basics/300-enums.cobra
1 #.require. clr 2 @platform clr 3 # This test of enums as value types is specific to the .Net environment 4 # other platforms treat enums differently. 1 5 enum Colors 2 6 Red 3 7 Green -
Tests/100-basics/334-enums-comma.cobra
1 # comma sep syntax 1 #.require. clr 2 @platform clr 3 # comma seperated syntax 2 4 enum Color 3 5 Red = 1, Green = 2, 4 6 Blue = 4 … … 8 10 def main is shared 9 11 c = Color(Red, Green) 10 12 print c 11 print c to int 13 print c to int # non portable - assumes Enums castable to int 12 14 .takeColor(Color(Red, Green)) 13 15 # TODO: Should have a qualified example. System.SomeEnum(MemberA, MemberB) and System.Something.SomeEnum(MemberA, MemberB) 14 16 15 # testing enum membership with bitwise-and 17 # testing enum membership with bitwise-and (also int centric) 16 18 assert c & Color.Red == Color.Red 17 19 assert c & Color.Green == Color.Green 18 20 assert c & Color.Blue <> Color.Blue