Skip to content
This repository has been archived by the owner on Oct 27, 2021. It is now read-only.

Commit

Permalink
suggest: type descriptions for builtin functions
Browse files Browse the repository at this point in the history
Provide type descriptions for builtin types based on pseudo-package
builtin's descriptions. Not currently enabled though because of
"proposeBuiltins = false" in (*candidateCollector).appendObject.
  • Loading branch information
mdempsky committed May 18, 2016
1 parent 7a99f7e commit 787cfae
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
46 changes: 35 additions & 11 deletions suggest/candidate.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ func classifyObject(obj types.Object) string {
return "const"
case *types.Func:
return "func"
case *types.Nil:
return "const"
case *types.PkgName:
return "package"
case *types.TypeName:
Expand Down Expand Up @@ -112,11 +114,7 @@ func (b *candidateCollector) asCandidate(obj types.Object) Candidate {
typStr = "struct"
default:
if _, isBuiltin := obj.(*types.Builtin); isBuiltin {
if obj.Pkg().Path() == "unsafe" {
typStr = "func(any) uintptr"
} else {
panic("TODO: unhandled builtin")
}
typStr = builtinTypes[obj.Name()]
} else if t != nil {
typStr = types.TypeString(t, b.qualify)
}
Expand All @@ -129,6 +127,30 @@ func (b *candidateCollector) asCandidate(obj types.Object) Candidate {
}
}

var builtinTypes = map[string]string{
// Universe.
"append": "func(slice []Type, elems ..Type) []Type",
"cap": "func(v Type) int",
"close": "func(c chan<- Type)",
"complex": "func(real FloatType, imag FloatType) ComplexType",
"copy": "func(dst []Type, src []Type) int",
"delete": "func(m map[Key]Type, key Key)",
"imag": "func(c ComplexType) FloatType",
"len": "func(v Type) int",
"make": "func(Type, size IntegerType) Type",
"new": "func(Type) *Type",
"panic": "func(v interface{})",
"print": "func(args ...Type)",
"println": "func(args ...Type)",
"real": "func(c ComplexType) FloatType",
"recover": "func() interface{}",

// Package unsafe.
"Alignof": "func(x Type) uintptr",
"Sizeof": "func(x Type) uintptr",
"Offsetof": "func(x Type) uintptr",
}

func (b *candidateCollector) qualify(pkg *types.Package) string {
if pkg == b.localpkg {
return ""
Expand All @@ -140,12 +162,14 @@ func (b *candidateCollector) appendObject(obj types.Object) {
// TODO(mdempsky): Change this to true.
const proposeBuiltins = false

if !proposeBuiltins && obj.Pkg() == nil && obj.Name() != "Error" {
return
}

if obj.Pkg() != nil && obj.Pkg() != b.localpkg && !obj.Exported() {
return
if obj.Pkg() != b.localpkg {
if obj.Parent() == types.Universe {
if !proposeBuiltins {
return
}
} else if !obj.Exported() {
return
}
}

// TODO(mdempsky): Reconsider this functionality.
Expand Down
6 changes: 3 additions & 3 deletions suggest/testdata/test.0025/out.expected
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Found 4 candidates:
func Alignof(any) uintptr
func Offsetof(any) uintptr
func Sizeof(any) uintptr
func Alignof(x Type) uintptr
func Offsetof(x Type) uintptr
func Sizeof(x Type) uintptr
type Pointer unsafe.Pointer

0 comments on commit 787cfae

Please sign in to comment.