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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542 | def normalize_function_signature(
self,
prototype: astx.FunctionPrototype,
*,
definition: astx.FunctionDef | None = None,
validate_ffi: bool = True,
validate_main: bool = True,
) -> FunctionSignature:
"""
title: Normalize and validate one semantic function signature.
parameters:
prototype:
type: astx.FunctionPrototype
definition:
type: astx.FunctionDef | None
validate_ffi:
type: bool
validate_main:
type: bool
returns:
type: FunctionSignature
"""
seen_parameter_names: set[str] = set()
seen_default_parameter = False
for argument in prototype.args.nodes:
if argument.name in seen_parameter_names:
self.context.diagnostics.add(
f"Function '{prototype.name}' repeats parameter "
f"'{argument.name}'",
node=argument,
code=DiagnosticCodes.SEMANTIC_DUPLICATE_DECLARATION,
)
continue
seen_parameter_names.add(argument.name)
if self._argument_has_default(argument):
seen_default_parameter = True
continue
if not seen_default_parameter:
continue
self.context.diagnostics.add(
f"Function '{prototype.name}' parameter '{argument.name}' "
"without a default cannot follow a parameter with a "
"default value",
node=argument,
code=DiagnosticCodes.SEMANTIC_CALL_ARITY,
)
is_extern = self._prototype_is_extern(prototype)
is_variadic = self._prototype_is_variadic(prototype)
symbol_name = self._prototype_symbol_name(prototype)
required_runtime_features = normalize_runtime_features(
prototype,
diagnostics=self.context.diagnostics,
)
calling_convention = self._prototype_calling_convention(
prototype,
is_extern=is_extern,
)
if definition is not None and is_extern:
self.context.diagnostics.add(
f"Extern function '{prototype.name}' cannot define a body",
node=definition,
code=DiagnosticCodes.FFI_INVALID_SIGNATURE,
)
if is_variadic and not is_extern:
self.context.diagnostics.add(
f"Function '{prototype.name}' may be variadic only when "
"declared extern",
node=prototype,
code=DiagnosticCodes.FFI_INVALID_SIGNATURE,
)
if is_extern and calling_convention is not CallingConvention.C:
self.context.diagnostics.add(
f"Extern function '{prototype.name}' must use calling "
"convention 'c'",
node=prototype,
code=DiagnosticCodes.FFI_INVALID_SIGNATURE,
)
if (
not is_extern
and calling_convention is not CallingConvention.IRX_DEFAULT
):
self.context.diagnostics.add(
f"IRx-defined function '{prototype.name}' must use calling "
"convention 'irx_default'",
node=prototype,
code=DiagnosticCodes.FFI_INVALID_SIGNATURE,
)
if not is_extern and symbol_name != prototype.name:
self.context.diagnostics.add(
f"Function '{prototype.name}' may override symbol_name only "
"for extern declarations",
node=prototype,
code=DiagnosticCodes.FFI_INVALID_SIGNATURE,
)
if not is_extern and required_runtime_features:
self.context.diagnostics.add(
f"Function '{prototype.name}' may declare runtime features "
"only when declared extern",
node=prototype,
code=DiagnosticCodes.RUNTIME_FEATURE_UNKNOWN,
)
signature = self.factory.make_function_signature(
prototype,
calling_convention=calling_convention,
is_variadic=is_variadic,
is_extern=is_extern,
symbol_name=symbol_name,
required_runtime_features=required_runtime_features,
)
if validate_ffi and signature.is_extern:
ffi = build_ffi_callable_info(
self.context,
signature=signature,
prototype=prototype,
)
if ffi is not None:
signature = replace(signature, ffi=ffi)
if validate_main and signature.name == MAIN_FUNCTION_NAME:
if signature.is_extern:
self.context.diagnostics.add(
"Function 'main' cannot be extern",
node=prototype,
code=DiagnosticCodes.FFI_INVALID_SIGNATURE,
)
if signature.is_variadic:
self.context.diagnostics.add(
"Function 'main' must not be variadic",
node=prototype,
code=DiagnosticCodes.FFI_INVALID_SIGNATURE,
)
if len(signature.parameters) != 0:
self.context.diagnostics.add(
"Function 'main' must not declare parameters",
node=prototype,
code=DiagnosticCodes.FFI_INVALID_SIGNATURE,
)
if (
signature.calling_convention
is not CallingConvention.IRX_DEFAULT
):
self.context.diagnostics.add(
"Function 'main' must use calling convention "
"'irx_default'",
node=prototype,
code=DiagnosticCodes.FFI_INVALID_SIGNATURE,
)
if not same_type(signature.return_type, astx.Int32()):
self.context.diagnostics.add(
"Function 'main' must return Int32",
node=prototype,
code=DiagnosticCodes.FFI_INVALID_SIGNATURE,
)
return signature
|