Skip to content

Commit

Permalink
[gencommon] Add type arg support (#43)
Browse files Browse the repository at this point in the history
### Background

→ gencommon didn't know about generic types.

### Changes

- Make it pass along the type parameters of generic types.
- Populate a `TypeArgNames` field in `Param` to make it easier to use
the type args.

### Testing

- Tested in place

---------

Co-authored-by: Gavin Shriver <[email protected]>
  • Loading branch information
bhollis and drshriveer authored Mar 14, 2024
1 parent 594dc48 commit 6a0f360
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
39 changes: 26 additions & 13 deletions gencommon/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,37 @@ func (ih *ImportHandler) ExtractTypeRef(typ types.Type) string {
case *types.Named:
pkg := t.Obj().Pkg()
typeName := t.Obj().Name()
if pkg == nil || pkg.Path() == ih.PInfo.PkgPath {
return typeName
alias := ""

// If we need an import, find it and use the proper alias
if pkg != nil && pkg.Path() != ih.PInfo.PkgPath {
// first check if we have a mapping for the package:
i, ok := ih.imports[pkg.Path()]
if ok {
i.inUse = true
} else {
i = &ImportDesc{
Alias: pkg.Name(),
PkgPath: pkg.Path(),
inUse: true,
}
ih.imports[i.PkgPath] = i
}
alias = i.Alias + "."
}

// first check if we have a mapping for the package:
i, ok := ih.imports[pkg.Path()]
if ok {
i.inUse = true
} else {
i = &ImportDesc{
Alias: pkg.Name(),
PkgPath: pkg.Path(),
inUse: true,
// Recurse into type arguments for generic types
targs := t.TypeArgs()
if targs != nil {
typeArgNames := make([]string, targs.Len())
for i := 0; i < targs.Len(); i++ {
typeArg := targs.At(i)
typeArgNames[i] = ih.ExtractTypeRef(typeArg)
}
ih.imports[i.PkgPath] = i
return fmt.Sprintf("%s%s[%s]", alias, typeName, strings.Join(typeArgNames, ", "))
}

return fmt.Sprintf("%s.%s", i.Alias, typeName)
return fmt.Sprintf("%s%s", alias, typeName)

default:
// *types.Interface is usually handled here too.
Expand Down
23 changes: 18 additions & 5 deletions gencommon/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ func ParamsFromSignatureTuple(ih *ImportHandler, tuple *types.Tuple, variadic bo
// Comments: nil, // FIXME: need AST
}

// Fill in type args if it's a generic type.
t := v.Type()
if n, ok := t.(*types.Pointer); ok { // unwrap pointer
t = n.Elem()
}
if n, ok := t.(*types.Named); ok && n.TypeArgs() != nil {
p.TypeArgNames = make([]string, n.TypeArgs().Len())
for i := 0; i < n.TypeArgs().Len(); i++ {
p.TypeArgNames[i] = ih.ExtractTypeRef(n.TypeArgs().At(i))
}
}

// ...Variadic's seem to be very forced into the language
// They exist at a signature level, but not lower.
// Lower, the type just resolves to a slice, so we need to trim that out.
Expand Down Expand Up @@ -131,11 +143,12 @@ func (ps Params) ensureNames(paramDeduper map[string]int, isOutput bool) {

// Param has information about a single parameter.
type Param struct {
ActualType types.Type
TypeRef string
Name string
Comments Comments
Variadic bool
ActualType types.Type
TypeRef string
Name string
Comments Comments
Variadic bool
TypeArgNames []string
}

// Declaration returns a name and type.
Expand Down

0 comments on commit 6a0f360

Please sign in to comment.