Skip to content

Commit 4fcb998

Browse files
authored
Merge pull request #463 from xushiwei/q
pkg.GetBuiltinTI
2 parents 61c6bd1 + 634a3be commit 4fcb998

File tree

4 files changed

+45
-23
lines changed

4 files changed

+45
-23
lines changed

builtin.go

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,22 +1270,26 @@ type mthdSignature interface {
12701270
Params() *types.Tuple
12711271
}
12721272

1273-
type builtinTI struct {
1273+
type BuiltinTI struct {
12741274
typ types.Type
12751275
methods []*builtinMethod
12761276
}
12771277

1278-
func (p *builtinTI) NumMethods() int {
1278+
func (p *BuiltinTI) AddMethod(name string, fn types.Object, eargs ...interface{}) {
1279+
p.methods = append(p.methods, &builtinMethod{name, fn, eargs})
1280+
}
1281+
1282+
func (p *BuiltinTI) numMethods() int {
12791283
return len(p.methods)
12801284
}
12811285

1282-
func (p *builtinTI) Method(i int) *builtinMethod {
1286+
func (p *BuiltinTI) method(i int) *builtinMethod {
12831287
return p.methods[i]
12841288
}
12851289

1286-
func (p *builtinTI) lookupByName(name string) mthdSignature {
1287-
for i, n := 0, p.NumMethods(); i < n; i++ {
1288-
method := p.Method(i)
1290+
func (p *BuiltinTI) lookupByName(name string) mthdSignature {
1291+
for i, n := 0, p.numMethods(); i < n; i++ {
1292+
method := p.method(i)
12891293
if method.name == name {
12901294
return method
12911295
}
@@ -1301,8 +1305,8 @@ var (
13011305

13021306
func initBuiltinTIs(pkg *Package) {
13031307
var (
1304-
float64TI, intTI, int64TI, uint64TI *builtinTI
1305-
ioxTI, stringTI, stringSliceTI *builtinTI
1308+
float64TI, intTI, int64TI, uint64TI *BuiltinTI
1309+
ioxTI, stringTI, stringSliceTI *BuiltinTI
13061310
)
13071311
btiMap := new(typeutil.Map)
13081312
strconv := pkg.TryImport("strconv")
@@ -1317,7 +1321,7 @@ func initBuiltinTIs(pkg *Package) {
13171321
if ioxPkg != "" {
13181322
if os := pkg.TryImport("os"); os.isValid() {
13191323
if iox := pkg.TryImport(ioxPkg); iox.isValid() {
1320-
ioxTI = &builtinTI{
1324+
ioxTI = &BuiltinTI{
13211325
typ: os.Ref("File").Type(),
13221326
methods: []*builtinMethod{
13231327
{"Gop_Enum", iox.Ref("EnumLines"), nil},
@@ -1328,33 +1332,33 @@ func initBuiltinTIs(pkg *Package) {
13281332
}
13291333
}
13301334
if strconv.isValid() {
1331-
float64TI = &builtinTI{
1335+
float64TI = &BuiltinTI{
13321336
typ: types.Typ[types.Float64],
13331337
methods: []*builtinMethod{
13341338
{"String", strconv.Ref("FormatFloat"), bmExargs{'g', -1, 64}},
13351339
},
13361340
}
1337-
intTI = &builtinTI{
1341+
intTI = &BuiltinTI{
13381342
typ: types.Typ[types.Int],
13391343
methods: []*builtinMethod{
13401344
{"String", strconv.Ref("Itoa"), nil},
13411345
},
13421346
}
1343-
int64TI = &builtinTI{
1347+
int64TI = &BuiltinTI{
13441348
typ: types.Typ[types.Int64],
13451349
methods: []*builtinMethod{
13461350
{"String", strconv.Ref("FormatInt"), bmExargs{10}},
13471351
},
13481352
}
1349-
uint64TI = &builtinTI{
1353+
uint64TI = &BuiltinTI{
13501354
typ: types.Typ[types.Uint64],
13511355
methods: []*builtinMethod{
13521356
{"String", strconv.Ref("FormatUint"), bmExargs{10}},
13531357
},
13541358
}
13551359
}
13561360
if strings.isValid() && strconv.isValid() {
1357-
stringTI = &builtinTI{
1361+
stringTI = &BuiltinTI{
13581362
typ: types.Typ[types.String],
13591363
methods: []*builtinMethod{
13601364
{"Len", btoLen, nil},
@@ -1400,7 +1404,7 @@ func initBuiltinTIs(pkg *Package) {
14001404
}
14011405
}
14021406
if strings.isValid() {
1403-
stringSliceTI = &builtinTI{
1407+
stringSliceTI = &BuiltinTI{
14041408
typ: types.NewSlice(types.Typ[types.String]),
14051409
methods: []*builtinMethod{
14061410
{"Len", btoLen, nil},
@@ -1409,7 +1413,7 @@ func initBuiltinTIs(pkg *Package) {
14091413
},
14101414
}
14111415
}
1412-
tis := []*builtinTI{
1416+
tis := []*BuiltinTI{
14131417
ioxTI,
14141418
float64TI,
14151419
intTI,
@@ -1445,7 +1449,7 @@ func initBuiltinTIs(pkg *Package) {
14451449
pkg.cb.btiMap = btiMap
14461450
}
14471451

1448-
func (p *CodeBuilder) getBuiltinTI(typ types.Type) *builtinTI {
1452+
func (p *CodeBuilder) getBuiltinTI(typ types.Type) *BuiltinTI {
14491453
switch t := typ.(type) {
14501454
case *types.Basic:
14511455
typ = types.Default(typ)
@@ -1459,9 +1463,15 @@ func (p *CodeBuilder) getBuiltinTI(typ types.Type) *builtinTI {
14591463
typ = tyChan
14601464
}
14611465
if bti := p.btiMap.At(typ); bti != nil {
1462-
return bti.(*builtinTI)
1466+
return bti.(*BuiltinTI)
14631467
}
14641468
return nil
14651469
}
14661470

14671471
// ----------------------------------------------------------------------------
1472+
1473+
func (p *Package) BuiltinTI(typ types.Type) *BuiltinTI {
1474+
return p.cb.getBuiltinTI(typ)
1475+
}
1476+
1477+
// ----------------------------------------------------------------------------

codebuild.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1772,10 +1772,10 @@ func isTypeConvert(otyp, typ types.Type) (string, bool) {
17721772
}
17731773

17741774
func (p *CodeBuilder) btiMethod(
1775-
o *builtinTI, name, aliasName string, flag MemberFlag, src ast.Node) MemberKind {
1775+
o *BuiltinTI, name, aliasName string, flag MemberFlag, src ast.Node) MemberKind {
17761776
if o != nil {
1777-
for i, n := 0, o.NumMethods(); i < n; i++ {
1778-
method := o.Method(i)
1777+
for i, n := 0, o.numMethods(); i < n; i++ {
1778+
method := o.method(i)
17791779
v := method.name
17801780
if v == name || (flag > 0 && v == aliasName) {
17811781
autoprop := flag == MemberFlagAutoProperty && v == aliasName

gop_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@ func initGopBuiltin(big gogen.PkgRef, conf *gogen.Config) {
3232

3333
func newGopBuiltinDefault(pkg *gogen.Package, conf *gogen.Config) *types.Package {
3434
fmt := pkg.Import("fmt")
35-
big := pkg.Import("github.com/goplus/gogen/internal/builtin")
35+
b := pkg.Import("github.com/goplus/gogen/internal/builtin")
3636
builtin := types.NewPackage("", "")
3737
if builtin.Scope().Insert(gogen.NewOverloadFunc(token.NoPos, builtin, "println", fmt.Ref("Println"))) != nil {
3838
panic("println exists")
3939
}
4040
gogen.InitBuiltin(pkg, builtin, conf)
41-
initGopBuiltin(big, conf)
41+
initGopBuiltin(b, conf)
42+
tiStr := pkg.BuiltinTI(types.Typ[types.String])
43+
tiStr.AddMethod("Capitalize", b.Ref("Capitalize"))
4244
return builtin
4345
}
4446

internal/builtin/strings.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package builtin
2+
3+
import "strings"
4+
5+
func Capitalize(str string) string {
6+
if str == "" {
7+
return ""
8+
}
9+
return strings.ToUpper(str[0:1]) + str[1:]
10+
}

0 commit comments

Comments
 (0)