this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

pkg: use more go/types logic in code generation

Don't chain callCtxtGetter with adtKind, which caused callCtxtGetter
to have some logic which only existed for the sake of adtKind.
This does mean the two funcs now repeat a few Go types like cue.Value,
but we continue to integrate some more principled logic on go/types.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I82772807fe2e4668329aa9c6047adf73fc17aedd
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1201377
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>

+43 -29
+43 -29
pkg/gen.go
··· 303 303 return nil 304 304 } 305 305 306 - var errorType = types.Universe.Lookup("error").Type() 306 + var ( 307 + typeError = types.Universe.Lookup("error").Type() 308 + typeByte = types.Universe.Lookup("byte").Type() 309 + ) 307 310 308 311 func (g *generator) genFunc(fn *types.Func) { 309 312 g.nonConcrete = false ··· 313 316 } 314 317 params := sign.Params() 315 318 results := sign.Results() 316 - if results == nil || (results.Len() != 1 && results.At(1).Type() != errorType) { 319 + if results == nil || (results.Len() != 1 && results.At(1).Type() != typeError) { 317 320 fmt.Printf("Dropped func %s.%s: must have one return value or a value and an error %v\n", g.cuePkgPath, fn.Name(), sign) 318 321 return 319 322 } ··· 375 378 switch typ := typ.(type) { 376 379 case *types.Basic: 377 380 return strings.Title(typ.String()) // "int" turns into "Int" 378 - case *types.Map: 379 - return "Struct" 380 381 case *types.Slice: 381 382 switch typ.Elem().String() { 382 383 case "byte": ··· 407 408 return "Schema" 408 409 case "io.Reader": 409 410 return "Reader" 410 - case "error": 411 - return "Bottom" // for [generator.adtKind] 412 - 413 - // Some builtin functions return custom types, like [cuelang.org/go/pkg/time.Split]. 414 - // TODO: we can simplify this once the CUE API declarations in ./pkg/... 415 - // use CUE function signatures to validate their parameters and results. 416 - case "*cuelang.org/go/pkg/time.Parts": 417 - return "Struct" 418 411 } 419 - log.Fatal("unknown Go type: ", typ.String()) 412 + log.Fatal("callCtxtGetter: unhandled Go type ", typ.String()) 420 413 return "" 421 414 } 422 415 ··· 424 417 // a [cuelang.org/go/internal/core/adt.Kind] value for the given type. 425 418 func (g *generator) adtKind(typ types.Type) string { 426 419 // TODO: detect list and structs types for return values. 427 - switch name := g.callCtxtGetter(typ); name { 428 - case "Bottom", "Bool", "String", "Struct", "Int", "List": 429 - return "adt." + name + "Kind" 430 - case "Int8", "Int16", "Int32", "Rune", "Int64", 431 - "Uint", "Byte", "Uint8", "Uint16", "Uint32", "Uint64", 432 - "BigInt": 433 - return "adt.IntKind" 434 - case "Float64", "BigFloat", "Decimal": 435 - return "adt.NumberKind" 436 - case "DecimalList", "StringList", "CueList": 420 + switch typ := typ.(type) { 421 + case *types.Slice: 422 + if typ.Elem() == typeByte { 423 + return "adt.BytesKind | adt.StringKind" 424 + } 437 425 return "adt.ListKind" 438 - case "Bytes", "Reader": 426 + case *types.Map: 427 + return "adt.StructKind" 428 + case *types.Basic: 429 + if typ.Info()&types.IsInteger != 0 { 430 + return "adt.IntKind" 431 + } 432 + if typ.Kind() == types.Float64 { 433 + return "adt.NumberKind" 434 + } 435 + return "adt." + strings.Title(typ.String()) + "Kind" // "bool" turns into "adt.BoolKind" 436 + } 437 + switch typ.String() { 438 + case "error": 439 + return "adt.BottomKind" 440 + case "io.Reader": 439 441 return "adt.BytesKind | adt.StringKind" 440 - case "Value", "Schema": 441 - // Must use the CallCtxt.Value method for these types and resolve manually. 442 + case "cuelang.org/go/internal/pkg.Struct": 443 + return "adt.StructKind" 444 + case "cuelang.org/go/internal/pkg.List": 445 + return "adt.ListKind" 446 + case "*math/big.Int": 447 + return "adt.IntKind" 448 + case "*cuelang.org/go/internal.Decimal", "*math/big.Float": 449 + return "adt.NumberKind" 450 + case "cuelang.org/go/cue.Value", "cuelang.org/go/cue/ast.Expr", "cuelang.org/go/internal/pkg.Schema": 442 451 return "adt.TopKind" // TODO: can be more precise 443 - default: 444 - log.Fatal("unknown CallCtxt type: ", name) 445 - return "" 452 + 453 + // Some builtin functions return custom types, like [cuelang.org/go/pkg/time.Split]. 454 + // TODO: we can simplify this once the CUE API declarations in ./pkg/... 455 + // use CUE function signatures to validate their parameters and results. 456 + case "*cuelang.org/go/pkg/time.Parts": 457 + return "adt.StructKind" 446 458 } 459 + log.Fatal("adtKind: unhandled Go type ", typ.String()) 460 + return "" 447 461 } 448 462 449 463 var errNoCUEFiles = errors.New("no CUE files in directory")