Changeset 1763
- Timestamp:
- 11/14/08 05:24:55 (8 weeks ago)
- Location:
- cobra/trunk/Source
- Files:
-
- 4 modified
-
BackEndClr/SharpGenerator.cobra (modified) (9 diffs)
-
BinaryOpExpr.cobra (modified) (12 diffs)
-
Expr.cobra (modified) (17 diffs)
-
Statements.cobra (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
cobra/trunk/Source/BackEndClr/SharpGenerator.cobra
r1754 r1763 3120 3120 if not sharpName.startsWith('typeof') 3121 3121 # maybe parentNameSpace could be moved up in the interface definitions 3122 pn = (.definition to dynamic).parentNameSpace3122 pn = .definition.parentNameSpace 3123 3123 if pn, sharpName = pn.sharpQualifier + sharpName 3124 3124 … … 3132 3132 get sharpName as String 3133 3133 assert .didBindImp 3134 assert . definition3134 assert .namedDefinition 3135 3135 assert not .type inherits UnspecifiedType 3136 3136 if .superNode inherits DotExpr 3137 3137 assert this is not (.superNode to DotExpr).right # should be a CallExpr or MemberExpr instead 3138 defi = . definition3138 defi = .namedDefinition 3139 3139 if .isTypeReference 3140 3140 # C# often requires typeof(Foo) instead of just plain Foo … … 3151 3151 require 3152 3152 .didBindImp 3153 . definition3153 .namedDefinition 3154 3154 body 3155 3155 if _definition inherits IVar … … 3160 3160 def writeSharpDefForBreakdown(sw as SharpWriter) 3161 3161 sharpName = .sharpName 3162 if .isKindOf(.compiler.typeType) and . definition inherits IType and 'typeof(' not in sharpName3162 if .isKindOf(.compiler.typeType) and .namedDefinition inherits IType and 'typeof(' not in sharpName 3163 3163 sw.write('typeof([.sharpName])') 3164 3164 else … … 3392 3392 sw.write(')') 3393 3393 else if isDynamic 3394 defi = (expr to dynamic).definition3394 defi = expr.definition 3395 3395 assert not defi inherits Box, expr # TODO: just curious 3396 3396 what = if(defi inherits IType, 'typeof([defi.sharpName])', defi.sharpName to String) … … 3974 3974 # handle the case where a type got backed up because of assignment inside of an if-inherits 3975 3975 if _backUpIfInheritsStack 3976 assert (_left to dynamic).definition inherits IVar3977 (_left to dynamic).definition.ifInheritsStack = Stack<of IType>(_backUpIfInheritsStack)3976 assert _left.definition inherits IVar 3977 _left.definition.ifInheritsStack = Stack<of IType>(_backUpIfInheritsStack) 3978 3978 3979 3979 … … 4327 4327 if _left inherits ThisLit 4328 4328 if _right inherits CallExpr 4329 defi = _right. definition4329 defi = _right.memberDefinition 4330 4330 if defi inherits IMember 4331 4331 writeThis = not defi.isShared … … 4335 4335 throw FallThroughException(defi) 4336 4336 else if _right inherits MemberExpr 4337 writeThis = not _right. definition.isShared4337 writeThis = not _right.memberDefinition.isShared 4338 4338 else if .curCodeMember inherits Initializer and _right inherits IDotRightExpr and (_right to IDotRightExpr).name == 'init' and _left inherits ThisOrBaseLit 4339 4339 # Well, in practice this doesn't really happen because Constructer.innerWriteSharpDef usurps the code gen for base calls … … 4416 4416 t = right.containedType to ! 4417 4417 else if right inherits IdentifierExpr 4418 t = right. definition to IType4418 t = right.namedDefinition to IType 4419 4419 else 4420 4420 throw FallThroughException(t) -
cobra/trunk/Source/BinaryOpExpr.cobra
r1762 r1763 73 73 var _left as Expr 74 74 var _right as Expr 75 var _definition as IMember?76 75 77 76 def init(opToken as IToken, op as String, left as Expr, right as Expr) … … 88 87 89 88 get right from var 90 91 pro definition from var92 89 93 90 get hasError as bool … … 161 158 .throwError('There is no type for the right hand side of "[.token.text]"[append] [sugg]') 162 159 163 def addRefFields164 base.addRefFields165 .addField('definition', _definition)166 167 160 def addSubFields 168 161 base.addSubFields … … 198 191 if _left inherits NameExpr 199 192 _trackLocal = _left.definition inherits AbstractLocalVar 200 if _trackLocal 201 _trackName = _left.definition.name 193 if _trackLocal, _trackName = _left.namedDefinition.name 202 194 203 195 … … 232 224 assert right.type 233 225 if left inherits IdentifierExpr 234 if left. definition is nil226 if left.namedDefinition is nil 235 227 # type inference 236 228 assert right.type … … 259 251 okay = false 260 252 if left inherits IdentifierExpr 261 if (defi = left. definition) inherits IVar253 if (defi = left.namedDefinition) inherits IVar 262 254 if defi.attemptAssignmentOf(right.type to !) 263 255 _backUpIfInheritsStack = List<of IType>(defi.ifInheritsStack) # will need this later for code gen … … 270 262 def checkAfterBindImp 271 263 base.checkAfterBindImp 272 if _left inherits IdentifierExpr 273 assert _left.definition 274 if _left.definition inherits Param 275 if _right inherits IdentifierExpr 276 if _right.definition inherits BoxVar 277 # the exception to this warning is when the statement is guarded by an if which references the _left.definition, as in: if param is nil; param = _param 278 # TODO: this has a test case that generates no warnings. so the cobra test cases should probably barf on unexpected warnings 279 isException = false 280 if .parent inherits BlockStmt and (.parent to BlockStmt).parent inherits IfStmt 281 ifStmt = (.parent to BlockStmt).parent to IfStmt 282 if (bop = ifStmt.cond) inherits BinaryOpExpr 283 if (bop.left inherits NameExpr) and (bop.left to NameExpr).definition is _left.definition 284 isException = true 285 if (bop.right inherits NameExpr) and (bop.right to NameExpr).definition is _right.definition 286 isException = true 287 if not isException 288 leftName = (_left.definition to Param).name 289 rightName = (_right.definition to BoxVar).name 290 .compiler.warning(this, 'Setting a parameter ("[leftName]") to a class variable ("[rightName]") is often a mistake. You may want to reverse the assignment.') 264 if not .hasError and _left.definition inherits Param 265 if _right.definition inherits BoxVar 266 # the exception to this warning is when the statement is guarded by an if which references the _left.definition, as in: if param is nil; param = _param 267 # TODO: this has a test case that generates no warnings. so the cobra test cases should probably barf on unexpected warnings 268 isException = false 269 if .parent inherits BlockStmt and (.parent to BlockStmt).parent inherits IfStmt 270 ifStmt = (.parent to BlockStmt).parent to IfStmt 271 if (bop = ifStmt.cond) inherits BinaryOpExpr 272 if bop.left.definition is _left.definition, isException = true 273 else if bop.right.definition is _right.definition, isException = true 274 if not isException 275 leftName = _left.definition.name 276 rightName = _right.definition.name 277 .compiler.warning(this, 'Setting a parameter ("[leftName]") to a class variable ("[rightName]") is often a mistake. You may want to reverse the assignment.') 291 278 292 279 … … 592 579 _dotRightExpr = right to IDotRightExpr 593 580 581 get definition is override 582 """ 583 The definition of a DotExpr is the definition of its .right which will be 584 a CallExpr or MemberExpr (via the interface IDotRightExpr). 585 """ 586 return .right.definition 587 594 588 get dotRight as IDotRightExpr 595 589 """ … … 618 612 else if not _left.hasError and not _right.hasError 619 613 if _left inherits IdentifierExpr # TODO: seems flawed. The error below should take place if left is a DotExpr 620 if _left. definition inherits Box # CC: combine with above if statement614 if _left.namedDefinition inherits Box # CC: combine with above if statement 621 615 if _dotRightExpr.member is not nil and not _dotRightExpr.member.isShared # when _dotRightExpr.member is nil, it's a nested type 622 616 if _dotRightExpr.name == 'init' … … 634 628 _transformTo(TypeExpr(.token, .definition to IType).bindImp) 635 629 else if (right = .right) inherits CallExpr 636 if right. definition implements IType630 if right.memberDefinition implements IType 637 631 # A.B.C() where C is a type such as a class or struct 638 te = TypeExpr(.token, right. definition to IType)632 te = TypeExpr(.token, right.memberDefinition to IType) 639 633 te.bindImp 640 634 pc = PostCallExpr(right.token, te, right.args) … … 668 662 # overridden to return the type this dotted expr represents in those cases when it does represent a type 669 663 assert .didBindImp 670 if _definition inherits IType671 return _definition664 if .definition inherits IType 665 return .definition 672 666 else 673 667 return nil … … 763 757 if not okay 764 758 .compiler.warning(this, 'The expression (of type "[leftType.name]") is never of type "[.right.toCobraSource]".') 765 if (right = .right) inherits IdentifierExpr # TODO: change to IPotentialTypeExpr and make a test case first 766 defi = right.definition 767 if defi 768 if defi inherits Class 769 if .op <> 'INHERITS' 770 .throwError('Use the "inherits" operator when testing objects against classes.') 771 else if defi inherits Struct 772 if .op <> 'INHERITS' 773 .throwError('Use the "inherits" operator when testing objects against structs.') 774 else if defi inherits Interface 775 pass 776 #if .op <> 'IMPLEMENTS' 777 # .throwError('Use the "implements" operator when testing objects against interfaces.') 778 else if defi inherits GenericParam 779 pass 780 else 781 .throwError('Cannot test "[.op.toLower]" against a [(defi to dynamic).englishName].') 759 defi = .right.definition 760 if defi inherits Class 761 if .op <> 'INHERITS', .throwError('Use the "inherits" operator when testing objects against classes.') 762 else if defi inherits Struct 763 if .op <> 'INHERITS', .throwError('Use the "inherits" operator when testing objects against structs.') 764 else if defi inherits Interface 765 # got annoying: 766 # if .op <> 'IMPLEMENTS', .throwError('Use the "implements" operator when testing objects against interfaces.') 767 pass 768 else if defi inherits GenericParam 769 pass 770 else if defi 771 .throwError('Cannot test "[.op.toLower]" against a [defi.englishName].') 782 772 783 773 -
cobra/trunk/Source/Expr.cobra
r1741 r1763 10 10 To see what really makes an expression, see the Expr class. 11 11 """ 12 13 get definition 14 """ 15 Returns the definition corresponding to this expression. 16 Dynamically typed. 17 Returns nil by default. 18 Not all expressions have definitions. 19 """ 20 12 21 def toCobraSource as String 13 22 … … 56 65 base.addRefFields 57 66 .addField('type', _type) 67 if .definition, .addField('definition', .definition) 58 68 59 69 pro argumentLabel from var … … 61 71 Indicates the label for an argument such as 'out' or 'inout'. 62 72 """ 73 74 get definition 75 return nil 63 76 64 77 get willChangeVar as bool … … 264 277 .addField('name', _name) 265 278 266 def addRefFields 267 base.addRefFields 268 .addField('definition', _definition) 269 270 get definition from var 279 get definition is override 280 return _definition 281 282 get namedDefinition from _definition 271 283 272 284 get name from var … … 274 286 def memberForName(name as String) as IMember? is override 275 287 or require 276 . definition288 .namedDefinition 277 289 body 278 290 return _definition.typeForReceiver.memberForName(name) … … 365 377 var _args as List<of Expr> 366 378 var _hasParens as bool 367 var _definition as INamedNode? 379 var _definition as INamedNode? # TODO: could tighten this down to IMember right?s 368 380 369 381 def init(token as IToken, name as String, args as List<of Expr>, hasParens as bool) … … 382 394 .addField('name', _name) 383 395 384 def addRefFields is override385 base.addRefFields386 .addField('definition', _definition)387 388 396 def addSubFields is override 389 397 base.addSubFields … … 414 422 has Subnodes 415 423 416 get definition from var 424 get definition is override 425 return _definition 426 427 get memberDefinition from _definition 417 428 418 429 get member as IMember? … … 484 495 assert _type, _definition 485 496 if .args.count == 0 and .hasParens 497 # TODO: 498 # if .definition inherits BoxMember and .definition.isMethod and not dotNode.isImplicit 486 499 if (.definition inherits Method or .definition inherits MemberOverload) and not dotNode.isImplicit 487 500 .compiler.warning(this, 'Unnecessary parentheses. You can remove them.') … … 741 754 has Subnodes 742 755 743 get definition from var 756 get definition is override 757 return _definition 758 759 get enumDefinition from _definition 744 760 745 761 get hasError as bool is override 746 if base.hasError 747 return true 748 for arg in _args 749 if arg.hasError 750 return true 762 if base.hasError, return true 763 for arg in _args, if arg.hasError, return true 751 764 return false 752 765 … … 828 841 if _nameExpr.definition 829 842 if _nameExpr.definition inherits IVar 830 _var = _nameExpr.definition to IVar843 _var = _nameExpr.definition 831 844 else 832 845 .throwError('Expecting a variable not a [_nameExpr.definition.getType.name].') # TODO: what's the best way to report what was found? … … 1071 1084 .addField('args', _args) 1072 1085 1073 get definition from var 1086 get definition is override 1087 return _definition 1088 1089 get memberDefinition from _definition 1074 1090 1075 1091 get target from var … … 1262 1278 .addField('isReference', _isReference) 1263 1279 1264 def addRefFields is override1265 base.addRefFields1266 .addField('definition', _definition)1267 1268 1280 get args as List<of Expr> 1269 1281 return List<of Expr>() 1270 1282 1271 get definition from var 1283 get definition is override 1284 return _definition 1285 1286 get memberDefinition from _definition 1272 1287 1273 1288 pro isReference from var … … 1329 1344 # TODO: there should be a subclass of BinaryOpExpr called DotExpr and it should do the following work and maybe even the work above. 1330 1345 # TODO: _receiverType = receiverType 1331 .binarySuperNode.definition = _definition # TODO: is this needed?1332 1346 .binarySuperNode.type = .type # the type of foo.bar is what bar returns. A MemberExpr is the "bar" part. 1333 1347 .binarySuperNode.receiverType = receiverType … … 1517 1531 expr = expr.bindImp 1518 1532 1519 if expr inherits IdentifierExpr 1520 if expr.definition inherits EnumDecl 1521 enumCall = EnumCallExpr(.token, expr.name, .args, expr.definition to EnumDecl).bindImp 1522 _type = enumCall.type to IType 1523 _transformTo(enumCall) 1524 return 1533 if expr.definition inherits EnumDecl 1534 enumCall = EnumCallExpr(.token, expr.toCobraSource, .args, expr.definition to EnumDecl).bindImp # CC: axe cast 1535 _type = enumCall.type to IType 1536 _transformTo(enumCall) 1537 return 1525 1538 1526 1539 for arg in .args … … 1678 1691 right = _expr.right 1679 1692 if right inherits MemberExpr 1680 if right.definition inherits AbstractMethod or right.definition inherits MemberOverload 1681 right.isReference = true 1682 else if right.definition is nil 1693 if right.definition is nil 1683 1694 # happens from error recovery such as: obj = BadClassName() ... ref obj.foo 1684 1695 pass 1696 else if right.definition inherits AbstractMethod or right.definition inherits MemberOverload 1697 right.isReference = true 1685 1698 else 1686 1699 .throwError('Only methods can be referenced, not [right.definition.englishName].') … … 1693 1706 pass 1694 1707 else 1695 .throwError('Only methods can be referenced, not [Utils.pluralize( (_expr.definition to dynamic).englishName)].')1708 .throwError('Only methods can be referenced, not [Utils.pluralize(_expr.definition.englishName)].') 1696 1709 else 1697 1710 .throwError('Unexpected reference. Refer to methods after `ref` or remove `ref`.') 1698 _type = .compiler.passThroughType # TODO: Set a real type such as . libraryType('System.Delegate')1711 _type = .compiler.passThroughType # TODO: Set a real type such as .compiler.delegateType 1699 1712 # TODO: need to do something more sophisticated like overriding: def canBeAssignedTo(type as IType) as bool 1700 1713 -
cobra/trunk/Source/Statements.cobra
r1752 r1763 88 88 ve.bindImp 89 89 assert ve.definition 90 varr = ve.definition to AbstractLocalVar90 varr = ve.definition 91 91 else 92 92 throw FallThroughException(ve) … … 385 385 if _varExpr.definition 386 386 if _varExpr.definition inherits IVar 387 _var = _varExpr.definition to IVar387 _var = _varExpr.definition 388 388 else 389 389 .throwError('Expecting a variable not a [_varExpr.definition.getType.name].') # TODO: what's the best way to report what was found? … … 677 677 left = cond.left 678 678 if left inherits IdentifierExpr 679 leftVar = left. definition679 leftVar = left.namedDefinition 680 680 if leftVar inherits IVar # if-inherits smarts only work on variables 681 681 # if x inherits Y ... … … 689 689 else if left inherits AssignExpr 690 690 if left.left inherits IdentifierExpr 691 leftVar = (left.left to IdentifierExpr).definition691 leftVar = left.left.definition 692 692 if leftVar inherits IVar 693 693 # if (x = expr) inherits Y ... … … 702 702 if cond inherits TruthExpr 703 703 if cond.expr inherits IdentifierExpr 704 leftVar = (cond.expr to IdentifierExpr).definition704 leftVar = cond.expr.definition 705 705 leftType = leftVar.typeForIdentifier 706 706 if leftType inherits NilableType or (leftType.isReference and leftType is not .compiler.objectType) … … 717 717 # TODO? "if not x is nil" "if nil is not x" 718 718 notNil = true 719 &nb
