Skip to content

Commit dc8cddf

Browse files
authored
Merge pull request #464 from xushiwei/q
BuiltinTI.AddMethods(...BuiltinMethod)
2 parents 4fcb998 + 85cf3f5 commit dc8cddf

File tree

3 files changed

+32
-30
lines changed

3 files changed

+32
-30
lines changed

builtin.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,19 +1242,19 @@ var (
12421242

12431243
type bmExargs = []interface{}
12441244

1245-
type builtinMethod struct {
1246-
name string
1247-
fn types.Object
1248-
eargs bmExargs
1245+
type BuiltinMethod struct {
1246+
Name string
1247+
Fn types.Object
1248+
Exargs []interface{}
12491249
}
12501250

1251-
func (p *builtinMethod) Results() *types.Tuple {
1252-
return p.fn.Type().(*types.Signature).Results()
1251+
func (p *BuiltinMethod) Results() *types.Tuple {
1252+
return p.Fn.Type().(*types.Signature).Results()
12531253
}
12541254

1255-
func (p *builtinMethod) Params() *types.Tuple {
1256-
params := p.fn.Type().(*types.Signature).Params()
1257-
n := params.Len() - len(p.eargs) - 1
1255+
func (p *BuiltinMethod) Params() *types.Tuple {
1256+
params := p.Fn.Type().(*types.Signature).Params()
1257+
n := params.Len() - len(p.Exargs) - 1
12581258
if n <= 0 {
12591259
return nil
12601260
}
@@ -1272,25 +1272,25 @@ type mthdSignature interface {
12721272

12731273
type BuiltinTI struct {
12741274
typ types.Type
1275-
methods []*builtinMethod
1275+
methods []*BuiltinMethod
12761276
}
12771277

1278-
func (p *BuiltinTI) AddMethod(name string, fn types.Object, eargs ...interface{}) {
1279-
p.methods = append(p.methods, &builtinMethod{name, fn, eargs})
1278+
func (p *BuiltinTI) AddMethods(mthds ...*BuiltinMethod) {
1279+
p.methods = append(p.methods, mthds...)
12801280
}
12811281

12821282
func (p *BuiltinTI) numMethods() int {
12831283
return len(p.methods)
12841284
}
12851285

1286-
func (p *BuiltinTI) method(i int) *builtinMethod {
1286+
func (p *BuiltinTI) method(i int) *BuiltinMethod {
12871287
return p.methods[i]
12881288
}
12891289

12901290
func (p *BuiltinTI) lookupByName(name string) mthdSignature {
12911291
for i, n := 0, p.numMethods(); i < n; i++ {
12921292
method := p.method(i)
1293-
if method.name == name {
1293+
if method.Name == name {
12941294
return method
12951295
}
12961296
}
@@ -1323,7 +1323,7 @@ func initBuiltinTIs(pkg *Package) {
13231323
if iox := pkg.TryImport(ioxPkg); iox.isValid() {
13241324
ioxTI = &BuiltinTI{
13251325
typ: os.Ref("File").Type(),
1326-
methods: []*builtinMethod{
1326+
methods: []*BuiltinMethod{
13271327
{"Gop_Enum", iox.Ref("EnumLines"), nil},
13281328
},
13291329
}
@@ -1334,33 +1334,33 @@ func initBuiltinTIs(pkg *Package) {
13341334
if strconv.isValid() {
13351335
float64TI = &BuiltinTI{
13361336
typ: types.Typ[types.Float64],
1337-
methods: []*builtinMethod{
1337+
methods: []*BuiltinMethod{
13381338
{"String", strconv.Ref("FormatFloat"), bmExargs{'g', -1, 64}},
13391339
},
13401340
}
13411341
intTI = &BuiltinTI{
13421342
typ: types.Typ[types.Int],
1343-
methods: []*builtinMethod{
1343+
methods: []*BuiltinMethod{
13441344
{"String", strconv.Ref("Itoa"), nil},
13451345
},
13461346
}
13471347
int64TI = &BuiltinTI{
13481348
typ: types.Typ[types.Int64],
1349-
methods: []*builtinMethod{
1349+
methods: []*BuiltinMethod{
13501350
{"String", strconv.Ref("FormatInt"), bmExargs{10}},
13511351
},
13521352
}
13531353
uint64TI = &BuiltinTI{
13541354
typ: types.Typ[types.Uint64],
1355-
methods: []*builtinMethod{
1355+
methods: []*BuiltinMethod{
13561356
{"String", strconv.Ref("FormatUint"), bmExargs{10}},
13571357
},
13581358
}
13591359
}
13601360
if strings.isValid() && strconv.isValid() {
13611361
stringTI = &BuiltinTI{
13621362
typ: types.Typ[types.String],
1363-
methods: []*builtinMethod{
1363+
methods: []*BuiltinMethod{
13641364
{"Len", btoLen, nil},
13651365
{"Count", strings.Ref("Count"), nil},
13661366
{"Int", strconv.Ref("Atoi"), nil},
@@ -1406,7 +1406,7 @@ func initBuiltinTIs(pkg *Package) {
14061406
if strings.isValid() {
14071407
stringSliceTI = &BuiltinTI{
14081408
typ: types.NewSlice(types.Typ[types.String]),
1409-
methods: []*builtinMethod{
1409+
methods: []*BuiltinMethod{
14101410
{"Len", btoLen, nil},
14111411
{"Cap", btoCap, nil},
14121412
{"Join", strings.Ref("Join"), nil},
@@ -1423,20 +1423,20 @@ func initBuiltinTIs(pkg *Package) {
14231423
stringSliceTI,
14241424
{
14251425
typ: tySlice,
1426-
methods: []*builtinMethod{
1426+
methods: []*BuiltinMethod{
14271427
{"Len", btoLen, nil},
14281428
{"Cap", btoCap, nil},
14291429
},
14301430
},
14311431
{
14321432
typ: tyMap,
1433-
methods: []*builtinMethod{
1433+
methods: []*BuiltinMethod{
14341434
{"Len", btoLen, nil},
14351435
},
14361436
},
14371437
{
14381438
typ: tyChan,
1439-
methods: []*builtinMethod{
1439+
methods: []*BuiltinMethod{
14401440
{"Len", btoLen, nil},
14411441
},
14421442
},

codebuild.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,7 +1776,7 @@ func (p *CodeBuilder) btiMethod(
17761776
if o != nil {
17771777
for i, n := 0, o.numMethods(); i < n; i++ {
17781778
method := o.method(i)
1779-
v := method.name
1779+
v := method.Name
17801780
if v == name || (flag > 0 && v == aliasName) {
17811781
autoprop := flag == MemberFlagAutoProperty && v == aliasName
17821782
this := p.stk.Pop()
@@ -1785,14 +1785,14 @@ func (p *CodeBuilder) btiMethod(
17851785
Fun: ast.NewIdent(fn),
17861786
Args: []ast.Expr{this.Val},
17871787
}
1788-
this.Type = &btiMethodType{Type: o.typ, eargs: method.eargs}
1788+
this.Type = &btiMethodType{Type: o.typ, eargs: method.Exargs}
17891789
} else {
1790-
this.Type = &btiMethodType{Type: this.Type, eargs: method.eargs}
1790+
this.Type = &btiMethodType{Type: this.Type, eargs: method.Exargs}
17911791
}
1792-
p.Val(method.fn, src)
1792+
p.Val(method.Fn, src)
17931793
p.stk.Push(this)
17941794
if p.rec != nil {
1795-
p.rec.Member(src, method.fn)
1795+
p.rec.Member(src, method.Fn)
17961796
}
17971797
if autoprop {
17981798
p.CallWith(0, 0, src)

gop_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ func newGopBuiltinDefault(pkg *gogen.Package, conf *gogen.Config) *types.Package
4040
gogen.InitBuiltin(pkg, builtin, conf)
4141
initGopBuiltin(b, conf)
4242
tiStr := pkg.BuiltinTI(types.Typ[types.String])
43-
tiStr.AddMethod("Capitalize", b.Ref("Capitalize"))
43+
tiStr.AddMethods(
44+
&gogen.BuiltinMethod{Name: "Capitalize", Fn: b.Ref("Capitalize")},
45+
)
4446
return builtin
4547
}
4648

0 commit comments

Comments
 (0)