@@ -31,68 +31,123 @@ func initMathBig(_ *gogen.Package, conf *gogen.Config, big gogen.PkgRef) {
31
31
conf .UntypedBigFloat = big .Ref ("UntypedBigfloat" ).Type ().(* types.Named )
32
32
}
33
33
34
- func initBuiltinFns (builtin * types.Package , scope * types.Scope , pkg gogen.PkgRef , fns []string ) {
35
- for _ , fn := range fns {
36
- fnTitle := string (fn [0 ]- 'a' + 'A' ) + fn [1 :]
37
- scope .Insert (gogen .NewOverloadFunc (token .NoPos , builtin , fn , pkg .Ref (fnTitle )))
34
+ type Builtin struct {
35
+ types.Object
36
+ name string
37
+ pkg string
38
+ sym string
39
+ }
40
+
41
+ func (t * Builtin ) Parent () * types.Scope {
42
+ return Universe
43
+ }
44
+ func (t * Builtin ) Pos () token.Pos {
45
+ return token .NoPos
46
+ }
47
+ func (t * Builtin ) Pkg () * types.Package {
48
+ return nil
49
+ }
50
+ func (t * Builtin ) Name () string {
51
+ return t .name
52
+ }
53
+ func (t * Builtin ) Type () types.Type {
54
+ return types .Typ [types .Invalid ]
55
+ }
56
+ func (t * Builtin ) Exported () bool {
57
+ return false
58
+ }
59
+ func (t * Builtin ) Id () string {
60
+ return "_." + t .name
61
+ }
62
+ func (t * Builtin ) String () string {
63
+ return "builtin " + t .name
64
+ }
65
+ func (t * Builtin ) Sym () string {
66
+ return t .pkg + "." + t .sym
67
+ }
68
+
69
+ var (
70
+ Universe * types.Scope
71
+ )
72
+
73
+ var builtinDefs = [... ]struct {
74
+ name string
75
+ pkg string
76
+ sym string
77
+ }{
78
+ {"bigint" , "github.com/goplus/gop/builtin/ng" , "" },
79
+ {"bigrat" , "github.com/goplus/gop/builtin/ng" , "" },
80
+ {"bigfloat" , "github.com/goplus/gop/builtin/ng" , "" },
81
+ {"int128" , "github.com/goplus/gop/builtin/ng" , "" },
82
+ {"uint128" , "github.com/goplus/gop/builtin/ng" , "" },
83
+ {"lines" , "github.com/goplus/gop/builtin/iox" , "" },
84
+ {"blines" , "github.com/goplus/gop/builtin/iox" , "BLines" },
85
+ {"newRange" , "github.com/goplus/gop/builtin" , "NewRange__0" },
86
+ {"echo" , "fmt" , "Println" },
87
+ {"print" , "fmt" , "" },
88
+ {"println" , "fmt" , "" },
89
+ {"printf" , "fmt" , "" },
90
+ {"errorf" , "fmt" , "" },
91
+ {"fprint" , "fmt" , "" },
92
+ {"fprintln" , "fmt" , "" },
93
+ {"sprint" , "fmt" , "" },
94
+ {"sprintln" , "fmt" , "" },
95
+ {"sprintf" , "fmt" , "" },
96
+ {"open" , "os" , "" },
97
+ {"create" , "os" , "" },
98
+ {"type" , "reflect" , "TypeOf" },
99
+ }
100
+
101
+ type defSym struct {
102
+ name string
103
+ sym string
104
+ }
105
+
106
+ var (
107
+ builtinSym map [string ][]defSym
108
+ )
109
+
110
+ func init () {
111
+ Universe = types .NewScope (nil , 0 , 0 , "universe" )
112
+ builtinSym = make (map [string ][]defSym )
113
+ for _ , def := range builtinDefs {
114
+ if def .sym == "" {
115
+ def .sym = string (def .name [0 ]- ('a' - 'A' )) + def .name [1 :]
116
+ }
117
+ builtinSym [def .pkg ] = append (builtinSym [def .pkg ], defSym {name : def .name , sym : def .sym })
118
+ obj := & Builtin {name : def .name , pkg : def .pkg , sym : def .sym }
119
+ Universe .Insert (obj )
38
120
}
39
121
}
40
122
41
- func initBuiltin (_ * gogen.Package , builtin * types.Package , os , fmt , ng , iox , buil , reflect gogen. PkgRef ) {
123
+ func initBuiltin (pkg * gogen.Package , builtin * types.Package ) {
42
124
scope := builtin .Scope ()
43
- if ng .Types != nil {
44
- typs := []string {"bigint" , "bigrat" , "bigfloat" }
45
- for _ , typ := range typs {
46
- name := string (typ [0 ]- ('a' - 'A' )) + typ [1 :]
47
- scope .Insert (types .NewTypeName (token .NoPos , builtin , typ , ng .Ref (name ).Type ()))
125
+ for im , defs := range builtinSym {
126
+ if p := pkg .TryImport (im ); p .Types != nil {
127
+ for _ , def := range defs {
128
+ obj := p .Ref (def .sym )
129
+ if _ , ok := obj .Type ().(* types.Named ); ok {
130
+ scope .Insert (types .NewTypeName (token .NoPos , builtin , def .name , obj .Type ()))
131
+ } else {
132
+ scope .Insert (gogen .NewOverloadFunc (token .NoPos , builtin , def .name , obj ))
133
+ }
134
+ }
48
135
}
49
- scope .Insert (types .NewTypeName (token .NoPos , builtin , "uint128" , ng .Ref ("Uint128" ).Type ()))
50
- scope .Insert (types .NewTypeName (token .NoPos , builtin , "int128" , ng .Ref ("Int128" ).Type ()))
51
- }
52
- if fmt .Types != nil {
53
- scope .Insert (gogen .NewOverloadFunc (token .NoPos , builtin , "echo" , fmt .Ref ("Println" )))
54
- initBuiltinFns (builtin , scope , fmt , []string {
55
- "print" , "println" , "printf" , "errorf" ,
56
- "fprint" , "fprintln" , "fprintf" ,
57
- "sprint" , "sprintln" , "sprintf" ,
58
- })
59
- }
60
- if os .Types != nil {
61
- initBuiltinFns (builtin , scope , os , []string {
62
- "open" , "create" ,
63
- })
64
- }
65
- if iox .Types != nil {
66
- initBuiltinFns (builtin , scope , iox , []string {
67
- "lines" ,
68
- })
69
- scope .Insert (gogen .NewOverloadFunc (token .NoPos , builtin , "blines" , iox .Ref ("BLines" )))
70
- }
71
- if reflect .Types != nil {
72
- scope .Insert (gogen .NewOverloadFunc (token .NoPos , builtin , "type" , reflect .Ref ("TypeOf" )))
73
- }
74
- if buil .Types != nil {
75
- scope .Insert (gogen .NewOverloadFunc (token .NoPos , builtin , "newRange" , buil .Ref ("NewRange__0" )))
76
136
}
77
137
scope .Insert (types .NewTypeName (token .NoPos , builtin , "any" , gogen .TyEmptyInterface ))
78
138
}
79
139
80
140
func newBuiltinDefault (pkg * gogen.Package , conf * gogen.Config ) * types.Package {
81
141
builtin := types .NewPackage ("" , "" )
82
- fmt := pkg .TryImport ("fmt" )
83
- os := pkg .TryImport ("os" )
84
- reflect := pkg .TryImport ("reflect" )
85
- buil := pkg .TryImport ("github.com/goplus/gop/builtin" )
86
142
ng := pkg .TryImport ("github.com/goplus/gop/builtin/ng" )
87
- iox := pkg .TryImport ("github.com/goplus/gop/builtin/iox" )
88
143
strx := pkg .TryImport ("github.com/qiniu/x/stringutil" )
89
144
stringslice := pkg .TryImport ("github.com/goplus/gop/builtin/stringslice" )
90
145
pkg .TryImport ("strconv" )
91
146
pkg .TryImport ("strings" )
92
147
if ng .Types != nil {
93
148
initMathBig (pkg , conf , ng )
94
149
}
95
- initBuiltin (pkg , builtin , os , fmt , ng , iox , buil , reflect )
150
+ initBuiltin (pkg , builtin )
96
151
gogen .InitBuiltin (pkg , builtin , conf )
97
152
if strx .Types != nil {
98
153
ti := pkg .BuiltinTI (types .Typ [types .String ])
0 commit comments