TypePHP 编译器 https://swoole.com/aot/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

279 lines
6.2 KiB

<?php
/**
* This file is part of TypePHP.
*
* @link https://www.swoole.com/
* @contact service@swoole.com
*/
#[Attribute(Attribute::TARGET_CLASS)]
final readonly class Native
{
}
#[Attribute(Attribute::TARGET_CLASS)]
final readonly class MethodsFor
{
public function __construct(public string $target)
{
}
}
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_FUNCTION | Attribute::TARGET_METHOD)]
final readonly class NoExport
{
}
/**
* Expose a statically typed function through a WASI 0.2 component interface.
*
* This is a compile-time attribute. It is never instantiated by the PHP
* runtime and therefore adds no reflection or dispatch overhead.
*/
#[Attribute(Attribute::TARGET_FUNCTION)]
final readonly class WasmExport
{
public function __construct(public ?string $name = null)
{
}
}
#[Attribute(Attribute::TARGET_PROPERTY)]
final readonly class Getter
{
}
#[Attribute(Attribute::TARGET_PROPERTY)]
final readonly class Setter
{
}
#[Attribute(Attribute::TARGET_PROPERTY)]
final readonly class With
{
}
#[Attribute(Attribute::TARGET_CLASS)]
final readonly class Printer
{
public function __construct(public ?array $fields = null)
{
}
}
#[Attribute(Attribute::TARGET_CLASS)]
final readonly class Arrayable
{
public function __construct(public ?array $fields = null)
{
}
}
#[Attribute(Attribute::TARGET_PARAMETER)]
final readonly class NotNull
{
}
#[Attribute(Attribute::TARGET_PARAMETER)]
final readonly class NotEmpty
{
}
#[Attribute(Attribute::TARGET_PARAMETER)]
final readonly class Validate
{
public function __construct(
public int $filter,
public int|array $options = 0,
public ?string $message = null,
) {
}
}
#[Attribute(Attribute::TARGET_FUNCTION | Attribute::TARGET_METHOD)]
final readonly class MustUse
{
}
#[Attribute(Attribute::TARGET_METHOD | Attribute::TARGET_PARAMETER)]
final readonly class Immutable
{
}
#[Attribute(Attribute::TARGET_FUNCTION | Attribute::TARGET_METHOD)]
final readonly class Hot
{
}
#[Attribute(Attribute::TARGET_FUNCTION | Attribute::TARGET_METHOD)]
final readonly class Cold
{
}
#[Attribute(Attribute::TARGET_PROPERTY)]
final readonly class Constructor
{
}
#[Attribute(Attribute::TARGET_PARAMETER | Attribute::TARGET_PROPERTY)]
final readonly class StdArray
{
public function __construct(string $valueType, int|array $sizeOrDimensions) {}
}
#[Attribute(Attribute::TARGET_PARAMETER | Attribute::TARGET_PROPERTY)]
final readonly class StdVector
{
public function __construct(string $valueType) {}
}
#[Attribute(Attribute::TARGET_PARAMETER | Attribute::TARGET_PROPERTY)]
final readonly class StdMap
{
public function __construct(string $keyType, string $valueType) {}
}
#[Attribute(Attribute::TARGET_PARAMETER | Attribute::TARGET_PROPERTY)]
final readonly class StdOrderedMap
{
public function __construct(string $keyType, string $valueType) {}
}
#[Attribute(Attribute::TARGET_PARAMETER | Attribute::TARGET_PROPERTY)]
final readonly class StdList
{
public function __construct(string $valueType) {}
}
#[Attribute(Attribute::TARGET_PARAMETER | Attribute::TARGET_PROPERTY)]
final readonly class StdDict
{
public function __construct(string $keyType, string $valueType) {}
}
/**
* Public compile-time type symbols shared by MethodsFor providers and std containers.
* This root class is deliberately distinct from the compiler-internal TypePhp\Type.
*/
final class Type
{
public const string Int = 'int';
public const string Float = 'float';
public const string Bool = 'bool';
public const string BigInt = 'bigint';
public const string BigFloat = 'bigfloat';
public const string Decimal = 'decimal';
public const string String = 'string';
public const string Str = 'string';
public const string Array = 'array';
public const string Object = 'object';
public const string Any = 'any';
public const string Stream = 'stream';
public const string Box = 'box';
}
/** Compiler directive that opts inferred integer locals into Variant storage. */
class varint_types
{
}
class std
{
public static function int(mixed $value): int
{
return intval($value);
}
public static function float(mixed $value): float
{
return floatval($value);
}
public static function bool(mixed $value): bool
{
return boolval($value);
}
public static function bigInt(mixed $value): mixed
{
return $value;
}
public static function decimal(mixed $value): mixed
{
return $value;
}
public static function bigFloat(mixed $value): mixed
{
return $value;
}
public static function any(mixed $value): mixed
{
return $value;
}
public static function object(mixed $value, string $class): object
{
if (!is_object($value)) {
throw new TypeError(sprintf(
'std::object(): Argument #1 ($value) must be an object, %s given',
get_debug_type($value),
));
}
if (!$value instanceof $class) {
throw new TypeError(sprintf(
'std::object(): Argument #1 ($value) must be an instance of %s, %s given',
$class,
$value::class,
));
}
return $value;
}
public static function &ref(mixed &$var): mixed
{
return $var;
}
public static function expected(mixed $condition): bool
{
return (bool) $condition;
}
public static function unexpected(mixed $condition): bool
{
return (bool) $condition;
}
public static function array(mixed $typeOrValues, ?int $size = null): array
{
return [];
}
public static function orderedMap(mixed $keyTypeOrValues, mixed $valueType = null): array
{
return [];
}
public static function map(mixed $keyTypeOrValues, mixed $valueType = null): array
{
return [];
}
public static function vector(mixed $valueTypeOrValues, ?int $size = null): array
{
return [];
}
public static function list(mixed $valueTypeOrValues): array
{
return [];
}
public static function dict(mixed $keyTypeOrValues, mixed $valueType = null): array
{
return [];
}
}