Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BuiltinTI.AddMethods(...BuiltinMethod) #464

Merged
merged 1 commit into from
Jan 12, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions builtin.go
Original file line number Diff line number Diff line change
@@ -1242,19 +1242,19 @@ var (

type bmExargs = []interface{}

type builtinMethod struct {
name string
fn types.Object
eargs bmExargs
type BuiltinMethod struct {
Name string
Fn types.Object
Exargs []interface{}
}

func (p *builtinMethod) Results() *types.Tuple {
return p.fn.Type().(*types.Signature).Results()
func (p *BuiltinMethod) Results() *types.Tuple {
return p.Fn.Type().(*types.Signature).Results()
}

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

type BuiltinTI struct {
typ types.Type
methods []*builtinMethod
methods []*BuiltinMethod
}

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

func (p *BuiltinTI) numMethods() int {
return len(p.methods)
}

func (p *BuiltinTI) method(i int) *builtinMethod {
func (p *BuiltinTI) method(i int) *BuiltinMethod {
return p.methods[i]
}

func (p *BuiltinTI) lookupByName(name string) mthdSignature {
for i, n := 0, p.numMethods(); i < n; i++ {
method := p.method(i)
if method.name == name {
if method.Name == name {
return method
}
}
@@ -1323,7 +1323,7 @@ func initBuiltinTIs(pkg *Package) {
if iox := pkg.TryImport(ioxPkg); iox.isValid() {
ioxTI = &BuiltinTI{
typ: os.Ref("File").Type(),
methods: []*builtinMethod{
methods: []*BuiltinMethod{
{"Gop_Enum", iox.Ref("EnumLines"), nil},
},
}
@@ -1334,33 +1334,33 @@ func initBuiltinTIs(pkg *Package) {
if strconv.isValid() {
float64TI = &BuiltinTI{
typ: types.Typ[types.Float64],
methods: []*builtinMethod{
methods: []*BuiltinMethod{
{"String", strconv.Ref("FormatFloat"), bmExargs{'g', -1, 64}},
},
}
intTI = &BuiltinTI{
typ: types.Typ[types.Int],
methods: []*builtinMethod{
methods: []*BuiltinMethod{
{"String", strconv.Ref("Itoa"), nil},
},
}
int64TI = &BuiltinTI{
typ: types.Typ[types.Int64],
methods: []*builtinMethod{
methods: []*BuiltinMethod{
{"String", strconv.Ref("FormatInt"), bmExargs{10}},
},
}
uint64TI = &BuiltinTI{
typ: types.Typ[types.Uint64],
methods: []*builtinMethod{
methods: []*BuiltinMethod{
{"String", strconv.Ref("FormatUint"), bmExargs{10}},
},
}
}
if strings.isValid() && strconv.isValid() {
stringTI = &BuiltinTI{
typ: types.Typ[types.String],
methods: []*builtinMethod{
methods: []*BuiltinMethod{
{"Len", btoLen, nil},
{"Count", strings.Ref("Count"), nil},
{"Int", strconv.Ref("Atoi"), nil},
@@ -1406,7 +1406,7 @@ func initBuiltinTIs(pkg *Package) {
if strings.isValid() {
stringSliceTI = &BuiltinTI{
typ: types.NewSlice(types.Typ[types.String]),
methods: []*builtinMethod{
methods: []*BuiltinMethod{
{"Len", btoLen, nil},
{"Cap", btoCap, nil},
{"Join", strings.Ref("Join"), nil},
@@ -1423,20 +1423,20 @@ func initBuiltinTIs(pkg *Package) {
stringSliceTI,
{
typ: tySlice,
methods: []*builtinMethod{
methods: []*BuiltinMethod{
{"Len", btoLen, nil},
{"Cap", btoCap, nil},
},
},
{
typ: tyMap,
methods: []*builtinMethod{
methods: []*BuiltinMethod{
{"Len", btoLen, nil},
},
},
{
typ: tyChan,
methods: []*builtinMethod{
methods: []*BuiltinMethod{
{"Len", btoLen, nil},
},
},
10 changes: 5 additions & 5 deletions codebuild.go
Original file line number Diff line number Diff line change
@@ -1776,7 +1776,7 @@ func (p *CodeBuilder) btiMethod(
if o != nil {
for i, n := 0, o.numMethods(); i < n; i++ {
method := o.method(i)
v := method.name
v := method.Name
if v == name || (flag > 0 && v == aliasName) {
autoprop := flag == MemberFlagAutoProperty && v == aliasName
this := p.stk.Pop()
@@ -1785,14 +1785,14 @@ func (p *CodeBuilder) btiMethod(
Fun: ast.NewIdent(fn),
Args: []ast.Expr{this.Val},
}
this.Type = &btiMethodType{Type: o.typ, eargs: method.eargs}
this.Type = &btiMethodType{Type: o.typ, eargs: method.Exargs}
} else {
this.Type = &btiMethodType{Type: this.Type, eargs: method.eargs}
this.Type = &btiMethodType{Type: this.Type, eargs: method.Exargs}
}
p.Val(method.fn, src)
p.Val(method.Fn, src)
p.stk.Push(this)
if p.rec != nil {
p.rec.Member(src, method.fn)
p.rec.Member(src, method.Fn)
}
if autoprop {
p.CallWith(0, 0, src)
4 changes: 3 additions & 1 deletion gop_test.go
Original file line number Diff line number Diff line change
@@ -40,7 +40,9 @@ func newGopBuiltinDefault(pkg *gogen.Package, conf *gogen.Config) *types.Package
gogen.InitBuiltin(pkg, builtin, conf)
initGopBuiltin(b, conf)
tiStr := pkg.BuiltinTI(types.Typ[types.String])
tiStr.AddMethod("Capitalize", b.Ref("Capitalize"))
tiStr.AddMethods(
&gogen.BuiltinMethod{Name: "Capitalize", Fn: b.Ref("Capitalize")},
)
return builtin
}