Permalink
Browse files

Make the class into an integer type.

Less data on the wire. Less parsing. Easier comparions.
  • Loading branch information...
1 parent 44ccc4b commit 8ce369591f51f0b00a5248f9a1dfd50e691b75f6 @nsf committed Feb 26, 2012
Showing with 69 additions and 63 deletions.
  1. +10 −17 autocompletecontext.go
  2. +2 −2 autocompletefile.go
  3. +49 −36 decl.go
  4. +1 −1 declcache.go
  5. +5 −5 gocode.go
  6. +1 −1 package.go
  7. +1 −1 server.go
View
@@ -25,7 +25,7 @@ import (
type candidate struct {
Name string
Type string
- Class string
+ Class decl_class
}
type out_buffers struct {
@@ -50,7 +50,7 @@ func (b *out_buffers) Len() int {
func (b *out_buffers) Less(i, j int) bool {
x := b.candidates[i]
y := b.candidates[j]
- if x.Class[0] == y.Class[0] {
+ if x.Class == y.Class {
return x.Name < y.Name
}
return x.Class < y.Class
@@ -60,10 +60,10 @@ func (b *out_buffers) Swap(i, j int) {
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"
- c2 := class != -1 && !match_class(int(decl.class), class)
- c3 := class == -1 && !strings.HasPrefix(name, p)
+ c2 := class != decl_invalid && decl.class != class
+ c3 := class == decl_invalid && !strings.HasPrefix(name, p)
c4 := !decl.matches()
c5 := !check_type_expr(decl.typ)
@@ -75,12 +75,12 @@ func (b *out_buffers) append_decl(p, name string, decl *decl, class int) {
b.candidates = append(b.candidates, candidate{
Name: name,
Type: b.tmpbuf.String(),
- Class: decl.class_name(),
+ Class: decl.class,
})
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 {
return
}
@@ -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
//
@@ -219,7 +212,7 @@ func (c *auto_complete_context) apropos(file []byte, filename string, cursor int
partial := 0
da := c.deduce_decl(file, cursor)
if da != nil {
- class := -1
+ class := decl_invalid
switch da.partial {
case "const":
class = decl_const
@@ -471,10 +464,10 @@ func check_type_expr(e ast.Expr) bool {
type decl_slice []*decl
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].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) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
View
@@ -115,7 +115,7 @@ func (f *auto_complete_file) process_decl(decl ast.Decl) {
f.scope, prevscope = advance_scope(f.scope)
}
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)
if d == nil {
@@ -335,7 +335,7 @@ func (f *auto_complete_file) process_assign_stmt(a *ast.AssignStmt) {
pack := decl_pack{names, nil, a.Rhs}
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)
if d == nil {
continue
View
85 decl.go
@@ -12,36 +12,54 @@ import (
)
// decl.class
+type decl_class int16
+
const (
- decl_const = iota
- decl_var
- decl_type
+ decl_invalid = decl_class(-1 + iota)
+
+ // these are in a sorted order
+ decl_const
decl_func
decl_package
+ decl_type
+ decl_var
// this one serves as a temporary type for those methods that were
// declared before their actual owner
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
+type decl_flags int16
+
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
// its type is inferred in a special way
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
//
@@ -55,8 +73,8 @@ type decl struct {
// '$i_%d' for anonymous interface types
name string
typ ast.Expr
- class int16
- flags int16
+ class decl_class
+ flags decl_flags
// functions for interface type, fields+methods for struct type
children map[string]*decl
@@ -94,7 +112,7 @@ func ast_decl_type(d ast.Decl) ast.Expr {
return nil
}
-func ast_decl_class(d ast.Decl) int {
+func ast_decl_class(d ast.Decl) decl_class {
switch t := d.(type) {
case *ast.GenDecl:
switch t.Tok {
@@ -109,7 +127,6 @@ func ast_decl_class(d ast.Decl) int {
return decl_func
}
panic("unreachable")
- return 0
}
func ast_decl_convertable(d ast.Decl) bool {
@@ -125,7 +142,7 @@ func ast_decl_convertable(d ast.Decl) bool {
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
for _, field := range f.List {
count += len(field.Names)
@@ -144,8 +161,8 @@ func ast_field_list_to_decls(f *ast.FieldList, class int, flags int, scope *scop
d := &decl{
name: name.Name,
typ: field.Type,
- class: int16(class),
- flags: int16(flags),
+ class: class,
+ flags: flags,
scope: scope,
value_index: -1,
}
@@ -161,8 +178,8 @@ func ast_field_list_to_decls(f *ast.FieldList, class int, flags int, scope *scop
d := &decl{
name: tp.name,
typ: field.Type,
- class: int16(class),
- flags: int16(flags),
+ class: class,
+ flags: flags,
scope: scope,
value_index: -1,
}
@@ -206,7 +223,7 @@ func ast_type_to_embedded(ty ast.Expr) []ast.Expr {
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) {
case *ast.StructType:
return ast_field_list_to_decls(t.Fields, decl_var, flags, scope)
@@ -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 {
return nil
}
@@ -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.name = name
- d.class = int16(class)
- d.flags = int16(flags)
+ d.class = class
+ d.flags = flags
d.typ = typ
d.value = v
d.value_index = vi
@@ -277,10 +294,10 @@ func new_decl_full(name string, class, flags int, typ, v ast.Expr, vi int, s *sc
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.name = name
- decl.class = int16(class)
+ decl.class = class
decl.value_index = -1
decl.scope = scope
return decl
@@ -348,10 +365,6 @@ func (other *decl) deep_copy() *decl {
return d
}
-func (d *decl) class_name() string {
- return g_decl_class_to_string[d.class]
-}
-
func (d *decl) expand_or_replace(other *decl) {
// expand only if it's a methods stub, otherwise simply copy
if d.class != decl_methods_stub && other.class != decl_methods_stub {
@@ -608,7 +621,7 @@ func range_predicate(v ast.Expr) bool {
}
type anonymous_typer struct {
- flags int
+ flags decl_flags
scope *scope
}
@@ -643,7 +656,7 @@ func (a *anonymous_typer) Visit(node ast.Node) ast.Visitor {
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}
ast.Walk(&v, node)
}
@@ -1206,7 +1219,7 @@ func (f *decl_pack) value_index(i int) (v ast.Expr, vi int) {
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 there is a type, we don't care about value, just return the type
// and zero value.
View
@@ -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) {
class := ast_decl_class(data.decl)
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)
if d == nil {
View
@@ -57,7 +57,7 @@ func (*nice_formatter) write_candidates(candidates []candidate, num int) {
fmt.Printf("Found %d candidates:\n", len(candidates))
for _, c := range candidates {
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"):])
}
fmt.Printf(" %s\n", abbr)
@@ -83,15 +83,15 @@ func (*vim_formatter) write_candidates(candidates []candidate, num int) {
}
word := c.Name
- if c.Class == "func" {
+ if c.Class == decl_func {
word += "("
if strings.HasPrefix(c.Type, "func()") {
word += ")"
}
}
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"):])
}
fmt.Printf("{'word': '%s', 'abbr': '%s'}", word, abbr)
@@ -107,8 +107,8 @@ type emacs_formatter struct{}
func (*emacs_formatter) write_candidates(candidates []candidate, num int) {
for _, c := range candidates {
- hint := c.Class + " " + c.Type
- if c.Class == "func" {
+ hint := c.Class.String() + " " + c.Type
+ if c.Class == decl_func {
hint = c.Type
}
fmt.Printf("%s,,%s\n", c.Name, hint)
View
@@ -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) {
class := ast_decl_class(data.decl)
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)
if d == nil {
View
@@ -112,7 +112,7 @@ func server_auto_complete(file []byte, filename string, cursor int) (c []candida
if err := recover(); err != nil {
print_backtrace(err)
c = []candidate{
- {"PANIC", "PANIC", "PANIC"},
+ {"PANIC", "PANIC", decl_invalid},
}
// drop cache

0 comments on commit 8ce3695

Please sign in to comment.