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.
2.1 KiB
2.1 KiB
函数与类生成
本章聚焦两个最频繁的发射动作:genFunctionWrapper()(函数/方法 → C++ 函数)与 genClassWrapper()(类/接口/枚举 → C++ 类与 zend_class_entry 注册)。
Sources: src/Translator.php
函数生成
genFunctionDeclarations() 为每个 FunctionDef 生成前向声明,规则包括:
- 方法:首参加
php::Object &this_;trait 父类场景追加zend_class_entry *trait_parent_ce; - 变参:声明为
php::Array并附默认参数表达式; - 多返回:在
typephp_multi_return命名空间再生成一份返回元组版本(hasMultiReturn()); - 热/冷路径:
hot/cold加TYPEPHP_HOT_ATTRIBUTE/TYPEPHP_COLD_ATTRIBUTE; - 导入/导出:跨库函数用
TYPEPHP_<LIB>_IMPORT宏,库导出函数用TYPEPHP_<NAME>_API。
Sources: src/Translator.php
类生成
genClassWrapper() 为每个 ClassDef / InterfaceDef 生成:
- C++ 类结构与属性布局;
- 静态属性默认值(
php::setStaticProperty); create_object钩子与属性处理器安装(genClassPropertyInit());- 数组常量(
genClassArrayConstants())。
属性初始化分两种情况(src/Translator.php):
- 标量/常量默认值:包成
php::Var写入属性表; - 数组默认值:用
ArrayInitPlan生成构造表达式,避免重复求值。
Sources: src/Translator.php · src/Preprocessor.php
类注册排序
genClassCeList() 用 marcj/topsort 按继承依赖拓扑排序生成 zend_class_entry 注册顺序,确保父类先于子类注册;内置类/接口(不在符号表中)直接加入排序而不参与依赖解析。
Sources: src/Translator.php