Skip to content

Commit

Permalink
Make the class into an integer type.
Browse files Browse the repository at this point in the history
Less data on the wire. Less parsing. Easier comparions.
  • Loading branch information
nsf committed Feb 26, 2012
1 parent 44ccc4b commit 8ce3695
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 63 deletions.
27 changes: 10 additions & 17 deletions autocompletecontext.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
type candidate struct { type candidate struct {
Name string Name string
Type string Type string
Class string Class decl_class
} }


type out_buffers struct { type out_buffers struct {
Expand All @@ -50,7 +50,7 @@ func (b *out_buffers) Len() int {
func (b *out_buffers) Less(i, j int) bool { func (b *out_buffers) Less(i, j int) bool {
x := b.candidates[i] x := b.candidates[i]
y := b.candidates[j] y := b.candidates[j]
if x.Class[0] == y.Class[0] { if x.Class == y.Class {
return x.Name < y.Name return x.Name < y.Name
} }
return x.Class < y.Class return x.Class < y.Class
Expand All @@ -60,10 +60,10 @@ func (b *out_buffers) Swap(i, j int) {
b.candidates[i], b.candidates[j] = b.candidates[j], b.candidates[i] b.candidates[i], b.candidates[j] = b.candidates[j], b.candidates[i]
} }


func (b *out_buffers) append_decl(p, name string, decl *decl, class int) { func (b *out_buffers) append_decl(p, name string, decl *decl, class decl_class) {
c1 := !g_config.ProposeBuiltins && decl.scope == g_universe_scope && decl.name != "Error" c1 := !g_config.ProposeBuiltins && decl.scope == g_universe_scope && decl.name != "Error"
c2 := class != -1 && !match_class(int(decl.class), class) c2 := class != decl_invalid && decl.class != class
c3 := class == -1 && !strings.HasPrefix(name, p) c3 := class == decl_invalid && !strings.HasPrefix(name, p)
c4 := !decl.matches() c4 := !decl.matches()
c5 := !check_type_expr(decl.typ) c5 := !check_type_expr(decl.typ)


Expand All @@ -75,12 +75,12 @@ func (b *out_buffers) append_decl(p, name string, decl *decl, class int) {
b.candidates = append(b.candidates, candidate{ b.candidates = append(b.candidates, candidate{
Name: name, Name: name,
Type: b.tmpbuf.String(), Type: b.tmpbuf.String(),
Class: decl.class_name(), Class: decl.class,
}) })
b.tmpbuf.Reset() b.tmpbuf.Reset()
} }


func (b *out_buffers) append_embedded(p string, decl *decl, class int) { func (b *out_buffers) append_embedded(p string, decl *decl, class decl_class) {
if decl.embedded == nil { if decl.embedded == nil {
return return
} }
Expand Down Expand Up @@ -117,13 +117,6 @@ func (b *out_buffers) append_embedded(p string, decl *decl, class int) {
} }
} }


func match_class(declclass int, class int) bool {
if class == declclass {
return true
}
return false
}

//------------------------------------------------------------------------- //-------------------------------------------------------------------------
// auto_complete_context // auto_complete_context
// //
Expand Down Expand Up @@ -219,7 +212,7 @@ func (c *auto_complete_context) apropos(file []byte, filename string, cursor int
partial := 0 partial := 0
da := c.deduce_decl(file, cursor) da := c.deduce_decl(file, cursor)
if da != nil { if da != nil {
class := -1 class := decl_invalid
switch da.partial { switch da.partial {
case "const": case "const":
class = decl_const class = decl_const
Expand Down Expand Up @@ -471,10 +464,10 @@ func check_type_expr(e ast.Expr) bool {
type decl_slice []*decl type decl_slice []*decl


func (s decl_slice) Less(i, j int) bool { func (s decl_slice) Less(i, j int) bool {
if s[i].class_name()[0] == s[j].class_name()[0] { if s[i].class != s[j].class {
return s[i].name < s[j].name return s[i].name < s[j].name
} }
return s[i].class_name() < s[j].class_name() return s[i].class < s[j].class
} }
func (s decl_slice) Len() int { return len(s) } func (s decl_slice) Len() int { return len(s) }
func (s decl_slice) Swap(i, j int) { s[i], s[j] = s[j], s[i] } func (s decl_slice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
Expand Down
4 changes: 2 additions & 2 deletions autocompletefile.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (f *auto_complete_file) process_decl(decl ast.Decl) {
f.scope, prevscope = advance_scope(f.scope) f.scope, prevscope = advance_scope(f.scope)
} }
for i, name := range data.names { for i, name := range data.names {
typ, v, vi := data.type_value_index(i, 0) typ, v, vi := data.type_value_index(i)


d := new_decl_full(name.Name, class, 0, typ, v, vi, prevscope) d := new_decl_full(name.Name, class, 0, typ, v, vi, prevscope)
if d == nil { if d == nil {
Expand Down Expand Up @@ -335,7 +335,7 @@ func (f *auto_complete_file) process_assign_stmt(a *ast.AssignStmt) {


pack := decl_pack{names, nil, a.Rhs} pack := decl_pack{names, nil, a.Rhs}
for i, name := range pack.names { for i, name := range pack.names {
typ, v, vi := pack.type_value_index(i, 0) typ, v, vi := pack.type_value_index(i)
d := new_decl_var(name.Name, typ, v, vi, prevscope) d := new_decl_var(name.Name, typ, v, vi, prevscope)
if d == nil { if d == nil {
continue continue
Expand Down
85 changes: 49 additions & 36 deletions decl.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,36 +12,54 @@ import (
) )


// decl.class // decl.class
type decl_class int16

const ( const (
decl_const = iota decl_invalid = decl_class(-1 + iota)
decl_var
decl_type // these are in a sorted order
decl_const
decl_func decl_func
decl_package decl_package
decl_type
decl_var


// this one serves as a temporary type for those methods that were // this one serves as a temporary type for those methods that were
// declared before their actual owner // declared before their actual owner
decl_methods_stub decl_methods_stub
) )


func (this decl_class) String() string {
switch this {
case decl_invalid:
return "PANIC"
case decl_const:
return "const"
case decl_func:
return "func"
case decl_package:
return "package"
case decl_type:
return "type"
case decl_var:
return "var"
case decl_methods_stub:
return "IF YOU SEE THIS, REPORT A BUG" // :D
}
panic("unreachable")
}

// decl.flags // decl.flags
type decl_flags int16

const ( const (
decl_foreign = 1 << iota // imported from another package decl_foreign = decl_flags(1 << iota) // imported from another package


// means that the decl is a part of the range statement // means that the decl is a part of the range statement
// its type is inferred in a special way // its type is inferred in a special way
decl_rangevar decl_rangevar
) )


var g_decl_class_to_string = [...]string{
decl_const: "const",
decl_var: "var",
decl_type: "type",
decl_func: "func",
decl_package: "package",
decl_methods_stub: "IF YOU SEE THIS, REPORT A BUG", // :D
}

//------------------------------------------------------------------------- //-------------------------------------------------------------------------
// decl // decl
// //
Expand All @@ -55,8 +73,8 @@ type decl struct {
// '$i_%d' for anonymous interface types // '$i_%d' for anonymous interface types
name string name string
typ ast.Expr typ ast.Expr
class int16 class decl_class
flags int16 flags decl_flags


// functions for interface type, fields+methods for struct type // functions for interface type, fields+methods for struct type
children map[string]*decl children map[string]*decl
Expand Down Expand Up @@ -94,7 +112,7 @@ func ast_decl_type(d ast.Decl) ast.Expr {
return nil return nil
} }


func ast_decl_class(d ast.Decl) int { func ast_decl_class(d ast.Decl) decl_class {
switch t := d.(type) { switch t := d.(type) {
case *ast.GenDecl: case *ast.GenDecl:
switch t.Tok { switch t.Tok {
Expand All @@ -109,7 +127,6 @@ func ast_decl_class(d ast.Decl) int {
return decl_func return decl_func
} }
panic("unreachable") panic("unreachable")
return 0
} }


func ast_decl_convertable(d ast.Decl) bool { func ast_decl_convertable(d ast.Decl) bool {
Expand All @@ -125,7 +142,7 @@ func ast_decl_convertable(d ast.Decl) bool {
return false return false
} }


func ast_field_list_to_decls(f *ast.FieldList, class int, flags int, scope *scope) map[string]*decl { func ast_field_list_to_decls(f *ast.FieldList, class decl_class, flags decl_flags, scope *scope) map[string]*decl {
count := 0 count := 0
for _, field := range f.List { for _, field := range f.List {
count += len(field.Names) count += len(field.Names)
Expand All @@ -144,8 +161,8 @@ func ast_field_list_to_decls(f *ast.FieldList, class int, flags int, scope *scop
d := &decl{ d := &decl{
name: name.Name, name: name.Name,
typ: field.Type, typ: field.Type,
class: int16(class), class: class,
flags: int16(flags), flags: flags,
scope: scope, scope: scope,
value_index: -1, value_index: -1,
} }
Expand All @@ -161,8 +178,8 @@ func ast_field_list_to_decls(f *ast.FieldList, class int, flags int, scope *scop
d := &decl{ d := &decl{
name: tp.name, name: tp.name,
typ: field.Type, typ: field.Type,
class: int16(class), class: class,
flags: int16(flags), flags: flags,
scope: scope, scope: scope,
value_index: -1, value_index: -1,
} }
Expand Down Expand Up @@ -206,7 +223,7 @@ func ast_type_to_embedded(ty ast.Expr) []ast.Expr {
return nil return nil
} }


func ast_type_to_children(ty ast.Expr, flags int, scope *scope) map[string]*decl { func ast_type_to_children(ty ast.Expr, flags decl_flags, scope *scope) map[string]*decl {
switch t := ty.(type) { switch t := ty.(type) {
case *ast.StructType: case *ast.StructType:
return ast_field_list_to_decls(t.Fields, decl_var, flags, scope) return ast_field_list_to_decls(t.Fields, decl_var, flags, scope)
Expand Down Expand Up @@ -239,7 +256,7 @@ var g_anon_gen anonymous_id_gen


//------------------------------------------------------------------------- //-------------------------------------------------------------------------


func check_for_anon_type(t ast.Expr, flags int, s *scope) ast.Expr { func check_for_anon_type(t ast.Expr, flags decl_flags, s *scope) ast.Expr {
if t == nil { if t == nil {
return nil return nil
} }
Expand All @@ -263,11 +280,11 @@ func check_for_anon_type(t ast.Expr, flags int, s *scope) ast.Expr {


//------------------------------------------------------------------------- //-------------------------------------------------------------------------


func new_decl_full(name string, class, flags int, typ, v ast.Expr, vi int, s *scope) *decl { func new_decl_full(name string, class decl_class, flags decl_flags, typ, v ast.Expr, vi int, s *scope) *decl {
d := new(decl) d := new(decl)
d.name = name d.name = name
d.class = int16(class) d.class = class
d.flags = int16(flags) d.flags = flags
d.typ = typ d.typ = typ
d.value = v d.value = v
d.value_index = vi d.value_index = vi
Expand All @@ -277,10 +294,10 @@ func new_decl_full(name string, class, flags int, typ, v ast.Expr, vi int, s *sc
return d return d
} }


func new_decl(name string, class int, scope *scope) *decl { func new_decl(name string, class decl_class, scope *scope) *decl {
decl := new(decl) decl := new(decl)
decl.name = name decl.name = name
decl.class = int16(class) decl.class = class
decl.value_index = -1 decl.value_index = -1
decl.scope = scope decl.scope = scope
return decl return decl
Expand Down Expand Up @@ -348,10 +365,6 @@ func (other *decl) deep_copy() *decl {
return d return d
} }


func (d *decl) class_name() string {
return g_decl_class_to_string[d.class]
}

func (d *decl) expand_or_replace(other *decl) { func (d *decl) expand_or_replace(other *decl) {
// expand only if it's a methods stub, otherwise simply copy // expand only if it's a methods stub, otherwise simply copy
if d.class != decl_methods_stub && other.class != decl_methods_stub { if d.class != decl_methods_stub && other.class != decl_methods_stub {
Expand Down Expand Up @@ -608,7 +621,7 @@ func range_predicate(v ast.Expr) bool {
} }


type anonymous_typer struct { type anonymous_typer struct {
flags int flags decl_flags
scope *scope scope *scope
} }


Expand Down Expand Up @@ -643,7 +656,7 @@ func (a *anonymous_typer) Visit(node ast.Node) ast.Visitor {
return a return a
} }


func anonymify_ast(node ast.Node, flags int, scope *scope) { func anonymify_ast(node ast.Node, flags decl_flags, scope *scope) {
v := anonymous_typer{flags, scope} v := anonymous_typer{flags, scope}
ast.Walk(&v, node) ast.Walk(&v, node)
} }
Expand Down Expand Up @@ -1206,7 +1219,7 @@ func (f *decl_pack) value_index(i int) (v ast.Expr, vi int) {
return return
} }


func (f *decl_pack) type_value_index(i, flags int) (ast.Expr, ast.Expr, int) { func (f *decl_pack) type_value_index(i int) (ast.Expr, ast.Expr, int) {
if f.typ != nil { if f.typ != nil {
// If there is a type, we don't care about value, just return the type // If there is a type, we don't care about value, just return the type
// and zero value. // and zero value.
Expand Down
2 changes: 1 addition & 1 deletion declcache.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func append_to_top_decls(decls map[string]*decl, decl ast.Decl, scope *scope) {
foreach_decl(decl, func(data *foreach_decl_struct) { foreach_decl(decl, func(data *foreach_decl_struct) {
class := ast_decl_class(data.decl) class := ast_decl_class(data.decl)
for i, name := range data.names { for i, name := range data.names {
typ, v, vi := data.type_value_index(i, 0) typ, v, vi := data.type_value_index(i)


d := new_decl_full(name.Name, class, 0, typ, v, vi, scope) d := new_decl_full(name.Name, class, 0, typ, v, vi, scope)
if d == nil { if d == nil {
Expand Down
10 changes: 5 additions & 5 deletions gocode.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (*nice_formatter) write_candidates(candidates []candidate, num int) {
fmt.Printf("Found %d candidates:\n", len(candidates)) fmt.Printf("Found %d candidates:\n", len(candidates))
for _, c := range candidates { for _, c := range candidates {
abbr := fmt.Sprintf("%s %s %s", c.Class, c.Name, c.Type) abbr := fmt.Sprintf("%s %s %s", c.Class, c.Name, c.Type)
if c.Class == "func" { if c.Class == decl_func {
abbr = fmt.Sprintf("%s %s%s", c.Class, c.Name, c.Type[len("func"):]) abbr = fmt.Sprintf("%s %s%s", c.Class, c.Name, c.Type[len("func"):])
} }
fmt.Printf(" %s\n", abbr) fmt.Printf(" %s\n", abbr)
Expand All @@ -83,15 +83,15 @@ func (*vim_formatter) write_candidates(candidates []candidate, num int) {
} }


word := c.Name word := c.Name
if c.Class == "func" { if c.Class == decl_func {
word += "(" word += "("
if strings.HasPrefix(c.Type, "func()") { if strings.HasPrefix(c.Type, "func()") {
word += ")" word += ")"
} }
} }


abbr := fmt.Sprintf("%s %s %s", c.Class, c.Name, c.Type) abbr := fmt.Sprintf("%s %s %s", c.Class, c.Name, c.Type)
if c.Class == "func" { if c.Class == decl_func {
abbr = fmt.Sprintf("%s %s%s", c.Class, c.Name, c.Type[len("func"):]) abbr = fmt.Sprintf("%s %s%s", c.Class, c.Name, c.Type[len("func"):])
} }
fmt.Printf("{'word': '%s', 'abbr': '%s'}", word, abbr) fmt.Printf("{'word': '%s', 'abbr': '%s'}", word, abbr)
Expand All @@ -107,8 +107,8 @@ type emacs_formatter struct{}


func (*emacs_formatter) write_candidates(candidates []candidate, num int) { func (*emacs_formatter) write_candidates(candidates []candidate, num int) {
for _, c := range candidates { for _, c := range candidates {
hint := c.Class + " " + c.Type hint := c.Class.String() + " " + c.Type
if c.Class == "func" { if c.Class == decl_func {
hint = c.Type hint = c.Type
} }
fmt.Printf("%s,,%s\n", c.Name, hint) fmt.Printf("%s,,%s\n", c.Name, hint)
Expand Down
2 changes: 1 addition & 1 deletion package.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func add_ast_decl_to_package(pkg *decl, decl ast.Decl, scope *scope) {
foreach_decl(decl, func(data *foreach_decl_struct) { foreach_decl(decl, func(data *foreach_decl_struct) {
class := ast_decl_class(data.decl) class := ast_decl_class(data.decl)
for i, name := range data.names { for i, name := range data.names {
typ, v, vi := data.type_value_index(i, decl_foreign) typ, v, vi := data.type_value_index(i)


d := new_decl_full(name.Name, class, decl_foreign, typ, v, vi, scope) d := new_decl_full(name.Name, class, decl_foreign, typ, v, vi, scope)
if d == nil { if d == nil {
Expand Down
2 changes: 1 addition & 1 deletion server.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func server_auto_complete(file []byte, filename string, cursor int) (c []candida
if err := recover(); err != nil { if err := recover(); err != nil {
print_backtrace(err) print_backtrace(err)
c = []candidate{ c = []candidate{
{"PANIC", "PANIC", "PANIC"}, {"PANIC", "PANIC", decl_invalid},
} }


// drop cache // drop cache
Expand Down

0 comments on commit 8ce3695

Please sign in to comment.