bind: accept implicit self parameters in all methods

This is the ObjC equiivalent to CL 30276. It expands support for
implicit `self` parameters to every exported method.

Change-Id: Iff8a956b38448213866a93dc02ca59cac592feef
Reviewed-on: https://go-review.googlesource.com/30277
Reviewed-by: David Crawshaw <crawshaw@golang.org>
This commit is contained in:
Elias Naur
2016-10-17 10:37:36 +00:00
parent 1c49d29d1c
commit 4cb3e7f634
3 changed files with 35 additions and 43 deletions
+21 -27
View File
@@ -405,7 +405,7 @@ type funcSummary struct {
ret string
sig *types.Signature
params, retParams []paramInfo
hasthis bool
hasself bool
}
type paramInfo struct {
@@ -429,31 +429,25 @@ func (g *ObjcGen) funcSummary(oinf *objcClassInfo, obj *types.Func) *funcSummary
}
params := sig.Params()
first := 0
if om != nil {
// Check the implicit this argument and the argument count of the overridden method
excess := params.Len() - len(om.Params)
if excess < 0 {
g.errorf("method %s has fewer arguments than the method it overrides", obj.Name())
} else if excess > 1 {
g.errorf("overriding method %s has more arguments than the method it overrides", obj.Name())
} else if excess == 1 {
s.hasthis = true
first = 1
t := params.At(0).Type()
if !isObjcType(t) {
g.errorf("the `this` argument to method %s is not a ObjC type", obj.Name())
return s
} else {
ot := g.wrapMap[t.(*types.Named).Obj().Name()]
found := false
for _, sup := range oinf.supers {
if ot == sup {
found = true
break
if oinf != nil {
if params.Len() > 0 {
v := params.At(0)
if v.Name() == "self" {
t := v.Type()
if isObjcType(t) {
s.hasself = true
first = 1
ot := g.wrapMap[t.(*types.Named).Obj().Name()]
found := false
for _, sup := range oinf.supers {
if ot == sup {
found = true
break
}
}
if !found {
g.errorf("the type %s of the `this` argument to method %s is not a super class of the enclosing struct", ot.Name, obj.Name())
}
}
if !found {
g.errorf("the type %s of the `this` argument to method %s is not a super class of the enclosing struct", ot.Name, obj.Name())
}
}
}
@@ -740,7 +734,7 @@ func (g *ObjcGen) genRead(toName, fromName string, t types.Type, mode varMode) {
func (g *ObjcGen) genFunc(s *funcSummary, objName string) {
if objName != "" {
g.Printf("int32_t refnum = go_seq_go_to_refnum(self._ref);\n")
if s.hasthis {
if s.hasself {
g.genRefWrite("self")
}
}
@@ -759,7 +753,7 @@ func (g *ObjcGen) genFunc(s *funcSummary, objName string) {
g.Printf("proxy%s_%s_%s(", g.pkgPrefix, objName, s.goname)
if objName != "" {
g.Printf("refnum")
if s.hasthis {
if s.hasself {
g.Printf(", _self")
}
}
+4 -4
View File
@@ -62,9 +62,9 @@
- (void)testClass {
GoObjcpkgGoNSDate *d = [[GoObjcpkgGoNSDate alloc] init];
NSString *desc = [d description]; // Also stores this
XCTAssertEqual(d, [d this], "GoNSDate this not identical");
XCTAssertEqual(GoObjcpkgHash, [d hash], "GoNSDate this not identical");
NSString *desc = [d description];
XCTAssertEqual(d, [d getSelf], "GoNSDate self not identical");
XCTAssertEqual(GoObjcpkgHash, [d hash], "GoNSDate hash not identical");
XCTAssertTrue([desc isEqualToString:GoObjcpkgDescriptionStr], "GoNSDate description mismatch: %@", desc);
GoObjcpkgGoUIResponder *resp = [[GoObjcpkgGoUIResponder alloc] init];
[resp pressesBegan:nil withEvent:nil];
@@ -79,7 +79,7 @@
};
NSString *superDesc = ((NSString *(*)(struct objc_super*, SEL))objc_msgSendSuper)(&_super, @selector(description));
XCTAssertTrue([superDesc isEqualToString:[o description]], "GoNSObject description mismatch");
[o setUseThis:TRUE];
[o setUseSelf:TRUE];
XCTAssertTrue([GoObjcpkgDescriptionStr isEqualToString:[o description]], "GoNSObject description mismatch");
}
+10 -12
View File
@@ -16,24 +16,22 @@ const (
type GoNSDate struct {
Foundation.NSDate
this Foundation.NSDate
}
func (d *GoNSDate) Hash(this Foundation.NSDate) int {
func (d *GoNSDate) Hash(self Foundation.NSDate) int {
return Hash
}
func (d *GoNSDate) Description(this Foundation.NSDate) string {
// Test this call
if h := this.Hash(); h != Hash {
func (d *GoNSDate) Description(self Foundation.NSDate) string {
// Test self call
if h := self.Hash(); h != Hash {
panic("hash mismatch")
}
d.this = this
return DescriptionStr
}
func (d *GoNSDate) This() Foundation.NSDate {
return d.this
func (d *GoNSDate) GetSelf(self Foundation.NSDate) Foundation.NSDate {
return self
}
func NewGoNSDate() *GoNSDate {
@@ -43,14 +41,14 @@ func NewGoNSDate() *GoNSDate {
type GoNSObject struct {
C Foundation.NSObjectC // The class
P Foundation.NSObjectP // The protocol
UseThis bool
UseSelf bool
}
func (o *GoNSObject) Description(this Foundation.NSObjectC) string {
if o.UseThis {
func (o *GoNSObject) Description(self Foundation.NSObjectC) string {
if o.UseSelf {
return DescriptionStr
} else {
return this.Super().Description()
return self.Super().Description()
}
}