From 95e7b5f6e4e24379f96f5964aabf313706a676c0 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Thu, 9 Apr 2026 17:45:06 +0800 Subject: [PATCH] =?UTF-8?q?refactor(compiler):=20=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E7=BC=96=E8=AF=91=E5=99=A8=E4=BB=A3=E7=A0=81=E7=BB=93=E6=9E=84?= =?UTF-8?q?=E5=B9=B6=E4=BF=AE=E5=A4=8D=E7=A9=BA=E6=8C=87=E9=92=88=E6=A3=80?= =?UTF-8?q?=E6=9F=A5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除编译器主函数中过早的函数声明和全局变量声明生成逻辑 - 将扩展源文件生成逻辑移至翻译器类的编译流程中 - 修改genExtension方法返回生成的文件路径而不是接收文件参数 - 修复预处理器中对类标志的空指针检查,避免访问未设置的属性 - 更新并行编译时的进度显示颜色从蓝色调整为浅蓝色 - 重新组织源文件编译顺序,确保扩展模块文件正确加入编译列表 --- bin/compiler.php | 16 ---------------- src/Php/Preprocessor.php | 2 +- src/Php/Translator.php | 29 ++++++++++++++++------------- 3 files changed, 17 insertions(+), 30 deletions(-) diff --git a/bin/compiler.php b/bin/compiler.php index e9faa681..08c3f00a 100755 --- a/bin/compiler.php +++ b/bin/compiler.php @@ -58,22 +58,6 @@ if (empty($sourceFiles)) { $translator->stop("No valid source file found"); } -// 生成所有函数声明、全局变量声明 -$translator->genFunctionDeclaration($translator->getIncludeDir() . '/php_func_decl.h'); -$translator->genExternGlobalVars($translator->getIncludeDir() . '/php_global_var_decl.h'); - -// 生成所有全局变量源文件 -$extensionSourceFile = $translator->getBuildDir() . '/extension.cc'; -$translator->genExtension($extensionSourceFile); -$sourceFiles[] = $extensionSourceFile; - -// 添加 main.cc 文件 -if ($translator->getBuildMode() == 'bin') { - $sourceFiles[] = ROOT_PATH . '/src/cpp/main.cc'; - $sourceFiles[] = ROOT_PATH . '/src/cpp/php_cli_process_title.c'; - $sourceFiles[] = ROOT_PATH . '/src/cpp/ps_title.c'; -} - // 编译所有 C++ 文件 $objectFiles = $translator->compile($sourceFiles); diff --git a/src/Php/Preprocessor.php b/src/Php/Preprocessor.php index 5bd33994..0a5b9841 100644 --- a/src/Php/Preprocessor.php +++ b/src/Php/Preprocessor.php @@ -572,7 +572,7 @@ class Preprocessor extends CompilerBase $this->checkRequiredArgNum($name, $this->methodDef, $v); $this->classDef->addMethod($this->methodDef); } else { - if (!($class->flags & Modifiers::ABSTRACT)) { + if (isset($class->flags) and !($class->flags & Modifiers::ABSTRACT)) { $this->fatalError($v, "Class {$this->class} cannot override non-abstract method {$v->name}"); } if ($this->method === '__construct') { diff --git a/src/Php/Translator.php b/src/Php/Translator.php index 23330978..b196a6cb 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -241,7 +241,7 @@ class Translator extends Preprocessor $this->writeFile($file, $code); } - public function genExtension(string $file): void + public function genExtension(): string { if ($this->buildMode == 'bin') { if (!$this->hasFunction('main')) { @@ -249,12 +249,14 @@ class Translator extends Preprocessor exit(1); } } + $file = $this->getBuildDir() . '/extension-' . $this->getModuleName() . '.cc'; $this->localHeaders = $this->argInfoHeaderFiles; $this->genClassCeList(); $code = $this->render('extension.cc.php'); $this->writeFile($file, $code); $this->formatCppCode($file); $this->localHeaders = []; + return $file; } public function getModuleName(): string @@ -317,17 +319,18 @@ class Translator extends Preprocessor $objectFiles = []; $job = $this->maxJob; - // 如果只有一个文件或 job 为 1,则串行编译 - if (count($sourceFiles) <= 1 || $job <= 1) { - foreach ($sourceFiles as $cppFile) { - $objectFile = $this->getObjectFile($cppFile); - $this->compileFile($cppFile, $objectFile); - if (!is_file($objectFile)) { - throw new \Exception('compile error'); - } - $objectFiles[] = $objectFile; - } - return $objectFiles; + // 生成所有函数声明、全局变量声明的头文件 + $this->genFunctionDeclaration($this->getIncludeDir() . '/php_func_decl.h'); + $this->genExternGlobalVars($this->getIncludeDir() . '/php_global_var_decl.h'); + + // 生成扩展模块的源文件 + $sourceFiles[] =$this->genExtension(); + + // embed 需要 main 函数,以及 cli 的内置函数定义 + if ($this->getBuildMode() == 'bin') { + $sourceFiles[] = __DIR__ . '/../cpp/main.cc'; + $sourceFiles[] = __DIR__ . '/../cpp/php_cli_process_title.c'; + $sourceFiles[] = __DIR__ . '/../cpp/ps_title.c'; } // 并行编译 @@ -338,7 +341,7 @@ class Translator extends Preprocessor $compiledCount = 0; $failedFiles = []; - $this->climate->blue("Starting parallel compilation with {$job} jobs for {$totalFiles} files"); + $this->climate->lightBlue("Starting parallel compilation with {$job} jobs for {$totalFiles} files"); while ($compiledCount < $totalFiles) { // 启动新进程,直到达到最大并发数