Repository navigation
Expand file tree
/
Copy pathcall.go
More file actions
495 lines (475 loc) 路 14.3 KB
/
Copy pathcall.go
File metadata and controls
495 lines (475 loc) 路 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
package analysis
import (
"cmp"
"fmt"
"maps"
"slices"
"strconv"
"strings"
"github.com/ProCode-Software/klar/internal/ast"
"github.com/ProCode-Software/klar/internal/klarerrs"
"github.com/ProCode-Software/klar/internal/ranges"
)
func (c *Checker) checkCallExpr(expr *ast.CallExpression, t *Expr) {
lhs := t.NewChild()
if lhsExpr, ok := expr.Callee.(*ast.Symbol); ok {
// Allow types for initializers
c.checkSymbolExpr(lhsExpr, true, lhs)
if t.Type == nil {
t.Type = InvalidType
}
c.Module.Info.Expressions[expr] = t
} else {
c.checkExpr(expr.Callee, lhs)
}
if lhs.Kind() == InvalidType {
t.Type = InvalidType
return
}
canCall := true
switch fn := UnderlyingTypeName(lhs.Type, true).(type) {
case *Overload, *Lambda, *TypeName, *EnumFunction:
case *Function:
if isTODO(fn) {
t.Root.mode |= todoExpr
}
// TODO: This is temporary and will be removed when generic inference
// is implemented
if isCloneBuiltin(fn) && len(expr.Args) >= 1 {
p1 := c.checkExprFrom(expr.Args[0].Value, t)
t.Type = p1.Type
return
}
case *UntypedInit:
if fn.kind == KindEnum && fn.Params == nil {
enum := expr.Callee.(*ast.EnumLiteral)
calledInit := &UntypedInit{kind: KindEnum, Node: enum, Params: expr.Args}
t.Type = calledInit
c.queue(func() { c.checkEnumParams(expr, t) }, true)
return // Won't check params now
}
canCall = false
default:
canCall = false
if obj, ok := lhs.Type.(*Object); ok && obj.IsTypeName() {
if _, ok := obj.TypeName().Type.(*TypeAlias); ok {
// Allow all type aliases to be called for type casts. There isn't
// always an expression syntax for every type (such as unions), so
// if the user aliases them, they can cast.
// type MyUnion = String | Int
// union := MyUnion("hello")
canCall = true
}
}
}
if !canCall {
// Not a function (or initializer)
err := klarerrs.Node(klarerrs.ErrNotAFunction, expr.Callee)
// If the user tries to call an enum item that doesn't take parameters,
// show a different error
if ei, ok := Underlying(lhs.Type).(*EnumRef); ok {
err.Code = klarerrs.ErrEnumItemNoParams
err.Label = fmt.Sprintf("Can't pass parameters to %s.%s", ei.Enum.Name, ei.Name)
err.Name = ei.Name
} else {
typ := quoteAka(lhs.Type)
err.Label = "This callee has type " + typ + " and can't be called"
err.Name = typ
}
c.fileError(err, t.Context.File)
t.Type = InvalidType
}
c.checkCallArgs(lhs.Type, expr.Args, expr.Parens, t)
}
func (c *Checker) checkCallArgs(
lhs Type, args []*ast.CallParam, parens ranges.Range, t *Expr,
) (overload Type) {
if c.debug() {
c.logger.push(fmt.Sprintf(
"call to %s at %s:%s", lhs, c.Module.ResolveFile(t.FileID()), parens,
))
defer c.logger.pop()
}
var name string
if obj, ok := lhs.(*Object); ok {
name = obj.Name
}
switch fn := UnderlyingTypeName(lhs, true).(type) {
case *TypeName, *Map, *List: // List/map cast
t.Type = lhs
// For type casts `T(v)`, `v` must be compatible with `T`.
//
// Or, if `v` is an implementation of `T` - `V(t)`, `V?` is returned.
// `t` must be a union, tag, or interface. If `t` is an optional or Result,
// show a hint that there's a more idiomatic way to check.
//
// If we have `T(t)`, show an error that the cast is redundant.
und := fn
if tn, ok := fn.(*TypeName); ok {
und = tn.Type
}
// Builtin initializer
var isBuiltin bool
switch und2 := und.(type) {
case Kind:
isBuiltin = true
und = builtinModule.Context.Lookup(und2.String()).TypeName().Type
if u, ok := und.(*bootstrapType); ok {
und = u.asDeclared
}
case *bootstrapType:
isBuiltin = true
und = und2.asDeclared
}
var castParam Type
// Call might be an initializer. Structs, enums, and builtins can be
// initialized in other ways, not just casts.
switch und := Underlying(und).(type) {
case *Struct:
ps := c.inferCallParams(args, t)
// Custom initializers have priority over default initializers
if ok := c.tryCheckInitializer(und.Initializers, ps, args, parens, t); ok {
// t.Type was set by tryCheckInitializer
return
}
// Now try using a default initializer
// Users can't access default initializers for builtins, except Error
if !isBuiltin || name == "Error" {
if ok := c.checkDefaultStructInit(und, args, ps, t.FileID()); ok {
t.Type = lhs // Default initializer is never fallable
return
}
}
castParam = ps.params[0]
case *Enum:
ps := c.inferCallParams(args, t)
if ok := c.tryCheckInitializer(und.Initializers, ps, args, parens, t); ok {
// TODO: Preserve type name given in LHS
return
}
// TODO: If we support raw-value initializers or flag enums, attempt
// to check for that.
if ok := c.checkDefaultEnumInit(und, args, ps, t); ok {
return
}
// TODO: Check arity count like below
castParam = ps.params[0]
default:
if !c.checkArity(args, Arity{1, 1}, len(args), parens, t.FileID()) {
return
}
castParam = c.checkExprFrom(args[0].Value, t).Type
}
// Otherwise, it's a type cast
if !c.checkArity(args, Arity{1, 1}, len(args), parens, t.FileID()) {
return // There must be exactly 1 parameter
}
c.checkTypeCast(lhs, castParam, args[0].Value, t)
case *EnumFunction:
t.Type = lhs
case *Lambda:
c.checkLambdaParams(fn, args, parens, t)
case *Function:
t.Type = fn.Return
pset := c.inferCallParams(args, t)
ov, _, err := c.resolveOverload(fn.Overloads, pset, false)
if err != nil {
err.Range = parens
c.fileError(err, t.FileID())
// ov still isn't nil
}
c.checkOverloadParams(ov, pset, args, parens, t)
case *Overload:
t.Type = fn.Return
// c.checkOverloadParams(fn, )
case *UntypedInit: // Not resolved yet
t.Type = InvalidType
case Kind:
default:
panic(fmt.Sprintf(
"checkCallArgs: unhandled LHS type after being handled by checkCallExpr: %T (underlying: %T)",
lhs, fn,
))
}
return lhs // TODO
}
func (c *Checker) checkArity(
args []*ast.CallParam, arity Arity, got int, parens ranges.Range, fid FileID,
) bool {
if arity.InRange(got) {
return true
}
err := arityError(args, arity, got, parens)
c.fileError(err, fid)
return false
}
func arityError(args []*ast.CallParam, arity Arity, got int, parens ranges.Range) *klarerrs.Error {
err := klarerrs.Range(klarerrs.ErrWrongParamCount, parens)
err.SetParam("got", got)
expParam := strconv.Itoa(arity.MinParams)
switch {
case arity.MaxParams == -1:
expParam += "+" // Expected 2+ params
case arity.MinParams != arity.MaxParams:
expParam += "-" + strconv.Itoa(arity.MaxParams) // Expected 2-4 params
}
err.SetParam("expected", expParam)
if got < arity.MinParams {
err.SetParam("notEnough", true)
}
switch got {
case 0:
err.Label = "No parameters were provided"
case 1:
err.Label = "1 parameter was provided"
default:
err.Label = strconv.Itoa(got) + " parameters were provided"
}
return err
}
func (c *Checker) checkLambdaParams(fn *Lambda, args []*ast.CallParam, parens ranges.Range, t *Expr) {
t.Type = fn.Return
arity := fn.Arity()
if !c.checkArity(args, arity, len(args), parens, t.FileID()) {
return
}
for i := 0; i < len(args); i++ {
var (
arg = args[i]
isVariadic = fn.Variadic && i >= len(fn.Params)
expType = fn.Params[min(i, len(fn.Params)-1)]
)
if isVariadic {
expType = expType.(*List).Elem
}
// Allowed if tuple or in variadic param
if rest, ok := arg.Value.(*ast.RestExpression); ok {
var (
rhs = c.checkExprFrom(rest.Expression, t.NewChild())
rhsRang = rest.Expression.GetRange()
itemType Type
)
switch rhs.Kind() {
case KindTuple:
tup := As[*Tuple](rhs.Type)
switch {
case tup.Len() < 2:
c.fileError(makeTupleSpreadCountError(rest, tup.Len(), false), t.FileID())
case isVariadic:
commonType, err := canSpreadTupleIntoList(tup)
if err != nil {
err.Range = rhsRang
c.fileError(err, t.FileID())
break
}
if !Compatible(commonType, expType) {
c.fileError(typeMismatch(expType, commonType, rhsRang), t.FileID())
}
case arity.MaxParams != -1 && i+tup.Len() > arity.MaxParams:
// Tuple has more parameters than the function accepts
// TODO: Error
default:
for j, item := range tup.Items {
expType := fn.Params[min(i+j, len(fn.Params)-1)]
if !Compatible(item, expType) {
c.fileError(typeMismatch(expType, item, rhsRang), t.FileID())
}
}
i += tup.Len()
}
case KindList:
itemType = As[*List](rhs.Type).Elem
fallthrough
case StringType:
if !isVariadic {
c.fileError(misplacedListRestError(rhs.Kind(), rest), t.FileID())
}
itemType = cmp.Or[Type](itemType, StringType)
if !Compatible(itemType, expType) {
c.fileError(typeMismatch(expType, itemType, rhsRang), t.FileID())
}
default:
c.fileError(invalidRestTypeError(rhs.Type, rest.Expression), t.FileID())
}
continue
}
e := c.checkExpr(arg.Value, t.NewChild().withHint(expType))
if !Compatible(e.Type, expType) {
err := typeMismatch(expType, e.Type, arg.Range)
// If [T] is passed to a parameter ...T, show a hint
// TODO: Probably also show a hint for tuples and strings
if list, ok := Underlying(e.Type).(*List); ok &&
isVariadic && Compatible(list.Elem, expType) {
hintWithDiff(
err, "Did you mean to spread this list?",
klarerrs.AddedString{Pos: arg.Range.End, String: "..."},
)
}
c.fileError(err, t.FileID())
}
}
}
func (c *Checker) checkEnumParams(expr *ast.CallExpression, t *Expr) {
}
func (c *Checker) checkTypeCast(
target, param Type, arg ast.Expression, t *Expr) {
}
// Returned parameters are untyped
func (c *Checker) inferCallParams(params []*ast.CallParam, callExpr *Expr) (ps paramSet) {
for _, arg := range params {
var typ Type
if rest, ok := arg.Value.(*ast.RestExpression); ok {
// TODO: If the param is labelled, a rest is only allowed if the
// param is variadic
inner := c.checkExprFrom(rest.Expression, callExpr)
switch inner.Kind() {
case KindTuple:
tup := As[*Tuple](inner.Type)
ps.params = append(ps.params, tup.Items...)
for range tup.Items {
ps.nodeMap = append(ps.nodeMap, rest.Expression)
}
continue
case KindList:
typ = As[*List](inner.Type).Elem
case StringType:
typ = StringType
default:
c.fileError(invalidRestTypeError(inner.Type, rest.Expression), callExpr.FileID())
typ = inner.Type
}
} else {
typ = c.checkExprFrom(arg.Value, callExpr).Type
}
// TODO: Labelled param followed by unlabelled params is considered variadic
if arg.Label != nil {
if ps.labelled == nil {
ps.labelled = make(map[string]Type)
}
ps.labelled[arg.Label.Name] = typ
} else {
ps.params = append(ps.params, typ)
ps.nodeMap = append(ps.nodeMap, arg.Value)
}
}
return ps
}
func (c *Checker) checkOverloadParams(
o *Overload, ps paramSet, params []*ast.CallParam, parens ranges.Range, t *Expr,
) {
t.Type = o.Return
// Arity check. We're not directly calling [Checker.checkArity] so we can
// offer hints if positional params were used instead of labelled ones.
if !o.Arity.InRange(len(ps.params)) {
c.overloadArityError(o, ps, params, parens, t)
return
}
checkParam := func(expVar *Variable, got Type, getNode func() ast.Expression) {
exp := expVar.Type
isVariadic := expVar.Object.Flags.Has(VariadicParam)
if isVariadic {
exp = exp.(*List).Elem
}
if !Compatible(got, exp) {
err := typeMismatch(exp, got, getNode().GetRange())
c.fileError(err, t.FileID())
}
// Set the inferred types for lambdas and shorthands
// TODO: This isn't a perfect solution. If these types are a list element,
// they won't be set.
switch Underlying(got).(type) {
case *UntypedLambda, *UntypedInit:
if e := c.Module.Info.Expressions[getNode()]; e != nil {
e.Type = exp
}
}
}
// Positional params
for i, got := range ps.params {
expVar := o.Params[min(i, len(o.Params)-1)]
checkParam(expVar, got, func() ast.Expression { return ps.nodeMap[i] })
}
// Labelled params
orderedLabels := orderedLabelledParams(params)
var labelledNodes map[string]*ast.CallParam // Lazily initialized
getLabelledNodes := func() map[string]*ast.CallParam {
if labelledNodes == nil {
labelledNodes = makeLabelledParamMap(params)
}
return labelledNodes
}
for _, name := range orderedLabels {
got := ps.labelled[name]
expVar, ok := o.labelMap[name]
if !ok {
// Labelled param doesn't exist
labelNode := getLabelledNodes()[name].Label
err := klarerrs.Node(klarerrs.ErrParamLabelUndefined, labelNode)
err.Desc = "These params are defined: " + strings.Join(
slices.Sorted(maps.Keys(o.labelMap)), ", ",
)
c.fileError(err, t.FileID())
continue
}
checkParam(expVar, got, func() ast.Expression {
return getLabelledNodes()[name].Value
})
}
}
func orderedLabelledParams(nodes []*ast.CallParam) (labels []string) {
for _, node := range nodes {
if node.Label != nil {
labels = append(labels, node.Label.Name)
}
}
return
}
func makeLabelledParamMap(params []*ast.CallParam) map[string]*ast.CallParam {
m := make(map[string]*ast.CallParam)
for _, node := range params {
if node.Label != nil {
// Respecified labels already checked at parse-time
m[node.Label.Name] = node
}
}
return m
}
// Try to show a helpful message if the user provided a labelled
// parameter as positional
func (c *Checker) overloadArityError(
o *Overload, ps paramSet, params []*ast.CallParam, parens ranges.Range, t *Expr,
) {
defaultError := func() {
// If there's no special hint we can give, use the generic message
c.checkArity(params, o.Arity, len(ps.params), parens, t.FileID())
}
// To show a different message, the user has to provide all required
// positional params (may be 0)
if len(ps.params) <= len(o.Params) {
defaultError()
return
}
var hadAltError bool
for i, posParam := range ps.params[len(o.Params):] {
// Labelled params must be in order to show the special message
if i >= len(o.LabelledParams) {
break
}
labelled := o.LabelledParams[i]
if !Compatible(posParam, labelled.Variable.Type) {
continue
}
hadAltError = true
node := ps.nodeMap[len(o.Params)+i]
err := klarerrs.Node(klarerrs.ErrMissingParamLabel, node)
err.Name = labelled.Label
err.Label = fmt.Sprintf("Missing parameter label '%s:'", labelled.Label)
hintWithDiff(err, "Add the missing label", klarerrs.AddedString{
Pos: node.GetRange().Start, String: labelled.Label + ": ",
})
c.fileError(err, t.FileID())
}
if !hadAltError {
defaultError()
}
}