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 | 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()
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)
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
|