diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.md index eab0308e..f74fc64b 100644 --- a/.github/copilot-instructions.md +++ b/.github/copilot-instructions.md @@ -9,7 +9,7 @@ TypePHP is an ahead-of-time compiler that translates PHP source into C++, then c 3. `compile()` chooses the platform/compiler backend, generates support sources and headers, and compiles sources, using `pcntl` parallelism when available. 4. `build()` links object files into the final executable or extension. -`src/Php/CompilerBase.php` contains most PHP-to-C++ translation logic and mixes in many traits for syntax handling and optimizations. `src/Php/Preprocessor.php` owns dependency discovery and file ordering. Platform-specific behavior lives under `src/Php/Platform/`, compiler backends under `src/Php/Backend/`, and metadata/state objects under `src/Php/Entity/` and `src/Php/Context/`. +`src/CompilerBase.php` contains most PHP-to-C++ translation logic and mixes in many traits for syntax handling and optimizations. `src/Preprocessor.php` owns dependency discovery and file ordering. Platform-specific behavior lives under `src/Platform/`, compiler backends under `src/Backend/`, and metadata/state objects under `src/Entity/` and `src/Context/`. ## Setup and build commands diff --git a/CLAUDE.md b/CLAUDE.md index 27501c8e..e9530b67 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -50,7 +50,7 @@ php run-tests.php tests/aot/arrays.phpt ### Translation Pipeline -The compiler follows a 4-stage pipeline, orchestrated by `src/Php/Translator.php` (the main entry point): +The compiler follows a 4-stage pipeline, orchestrated by `src/Translator.php` (the main entry point): 1. **prepare()** — Scan PHP files, collect symbol declarations and dependencies, topological-sort for compilation order 2. **convert()** — Parse PHP AST via `nikic/php-parser`, translate each node to C++ source code @@ -60,31 +60,31 @@ The compiler follows a 4-stage pipeline, orchestrated by `src/Php/Translator.php ### Class Hierarchy ``` -src/Php/CompilerBase (core PHP→C++ translation logic, indent/output/mode helpers) +src/CompilerBase.php (core PHP→C++ translation logic, indent/output/mode helpers) ├─ uses traits: AstNodeType, FuncCallOptimizer, ClosureGenerator, │ PlaceHolderGenerator, PropertyPromotion, MagicMethodDetector - └─ src/Php/Preprocessor (scanning, symbol tables, dependency sort, YAML config) - └─ src/Php/Translator (full pipeline: prepare→convert→compile→build) - └─ src/Php/CompilerTest (test-only subclass, used by PHPUnit tests) + └─ src/Preprocessor.php (scanning, symbol tables, dependency sort, YAML config) + └─ src/Translator.php (full pipeline: prepare→convert→compile→build) + └─ src/CompilerTest.php (test-only subclass, used by PHPUnit tests) ``` ### Key Components | Directory | Purpose | |-----------|---------| -| `src/Php/Entity/` | Data classes: `ClassDef`, `FunctionDef`, `MethodDef`, `PropertyDef`, `ConstantDef`, `InterfaceDef` | -| `src/Php/Generator/` | Codegen helpers: `ClosureGenerator`, `PlaceHolderGenerator`, `PropertyPromotion`, `Utils` | -| `src/Php/Backend/` | Compiler abstraction: `CompilerBackend` (abstract) → `Gcc`, `Clang`, `Msvc`. Factory pattern via `CompilerFactory` | -| `src/Php/Platform/` | OS abstraction: `PlatformBase` → `Linux`, `Macos`, `Windows`. Factory via `PlatformFactory` | -| `src/Php/Context/` | `ScopeContext` and `FunctionContext` for variable scoping and type tracking | -| `src/Php/Exception/` | `SyntaxError`, `Unsupported`, `DynamicCall`, `PlaceHolder`, `Skip`, `Redo`, `TestError` | -| `src/Php/Parser/` | Special-purpose parsers like `StdContainerParser` (C++ std container foreach support) | -| `src/Php/Reflection.php` | Static helpers wrapping PHP reflection (internal class/function detection) | -| `src/Php/Symbol.php` | Maps PHP operations to `phpx` C++ API symbol names | -| `src/Php/FileScanner.php` | Recursive file discovery with extension filtering (supports `.php`, `.cpp`, `.c`, `.s`, `.m`, `.mm`) | -| `src/Php/ArgInfo.php` | Generates C function argument info structures for internal function registration | -| `src/Php/Extractor.php` | Extracts interfaces from PHP classes | -| `src/Php/Visitor.php` | Base `NodeVisitorAbstract` extension (skeleton for custom AST visitors) | +| `src/Entity/` | Data classes: `ClassDef`, `FunctionDef`, `MethodDef`, `PropertyDef`, `ConstantDef`, `InterfaceDef` | +| `src/Generator/` | Codegen helpers: `ClosureGenerator`, `PlaceHolderGenerator`, `PropertyPromotion`, `Utils` | +| `src/Backend/` | Compiler abstraction: `CompilerBackend` (abstract) → `Gcc`, `Clang`, `Msvc`. Factory pattern via `CompilerFactory` | +| `src/Platform/` | OS abstraction: `PlatformBase` → `Linux`, `Macos`, `Windows`. Factory via `PlatformFactory` | +| `src/Context/` | `ScopeContext` and `FunctionContext` for variable scoping and type tracking | +| `src/Exception/` | `SyntaxError`, `Unsupported`, `DynamicCall`, `PlaceHolder`, `Skip`, `Redo`, `TestError` | +| `src/Parser/` | Special-purpose parsers like `StdContainerParser` (C++ std container foreach support) | +| `src/Reflection.php` | Static helpers wrapping PHP reflection (internal class/function detection) | +| `src/Symbol.php` | Maps PHP operations to `phpx` C++ API symbol names | +| `src/FileScanner.php` | Recursive file discovery with extension filtering (supports `.php`, `.cpp`, `.c`, `.s`, `.m`, `.mm`) | +| `src/ArgInfo.php` | Generates C function argument info structures for internal function registration | +| `src/Extractor.php` | Extracts interfaces from PHP classes | +| `src/Visitor.php` | Base `NodeVisitorAbstract` extension (skeleton for custom AST visitors) | ### Configuration @@ -100,4 +100,4 @@ Generated `.cc` and `.o` files land in `build/` directory. The compiled binary i - **PHPUnit tests** (`phpunit/src/`) — unit/integration tests for compiler internals. Bootstrap at `phpunit/bootstrap.php` defines a `BaseTest` class with an `exec()` helper that runs the compiler and expects a `TestError` exception containing a given string - **PHPT tests** (`tests/aot/`) — end-to-end tests using the standard PHPT format (`run-tests.php`). Each `.phpt` contains PHP source and expected output sections -When writing new compiler tests, use `CompilerTest::create(ROOT_PATH)` (in `src/Php/CompilerTest.php`) which sets `forTest = true` to enable test-specific behavior without writing files to disk. +When writing new compiler tests, use `CompilerTest::create(ROOT_PATH)` (in `src/CompilerTest.php`) which sets `forTest = true` to enable test-specific behavior without writing files to disk. diff --git a/composer.json b/composer.json index bf050b2a..3e3c0f83 100644 --- a/composer.json +++ b/composer.json @@ -15,10 +15,7 @@ }, "autoload": { "psr-4": { - "TypePhp\\": [ - "src/Php", - "src" - ] + "TypePhp\\": "src" } }, "scripts": { diff --git a/phpunit/TEST_RESULTS.md b/phpunit/TEST_RESULTS.md index 61ce3430..df2bbdf1 100644 --- a/phpunit/TEST_RESULTS.md +++ b/phpunit/TEST_RESULTS.md @@ -255,7 +255,7 @@ protected const string MODULE_NAME_PREFIX = 'app_'; ### 方案 B:临时修改代码 -修改 `src/Php/Translator.php`: +修改 `src/Translator.php`: ```php // 第 31-32 行 diff --git a/phpunit/src/Platform/PlatformTest.php b/phpunit/src/Platform/PlatformTest.php index 3e7dfa06..beb2b9c5 100644 --- a/phpunit/src/Platform/PlatformTest.php +++ b/phpunit/src/Platform/PlatformTest.php @@ -72,7 +72,7 @@ class PlatformTest extends TestCase { $platform = new Windows(); - $this->assertEquals('src\Php\Backend', $platform->normalizePath('src/Php/Backend')); + $this->assertEquals('src\Backend', $platform->normalizePath('src/Backend')); $this->assertEquals('C:\PHP\include', $platform->normalizePath('C:/PHP/include')); } diff --git a/phpunit/src/SsaAnalysisTest.php b/phpunit/src/SsaAnalysisTest.php index b6fa4bc8..2c731b39 100644 --- a/phpunit/src/SsaAnalysisTest.php +++ b/phpunit/src/SsaAnalysisTest.php @@ -32,7 +32,7 @@ class SsaAnalysisTest extends TestCase parent::setUp(); // SsaFlags, SsaVar, SsaBlock, etc. are defined in SsaBuilder.php // but PSR-4 expects each class in its own file. Load them early. - require_once __DIR__ . '/../../src/Php/Analysis/SsaBuilder.php'; + require_once __DIR__ . '/../../src/Analysis/SsaBuilder.php'; $this->tmpDir = sys_get_temp_dir() . '/ssa_test_' . uniqid(); mkdir($this->tmpDir, 0777, true); $this->compiler = CompilerTest::create($this->tmpDir); diff --git a/project.yml b/project.yml index 9d563036..18a7add3 100644 --- a/project.yml +++ b/project.yml @@ -30,9 +30,10 @@ resource: # manifest: app.manifest sources: - - ./src/Php - - ./src/functions.php - - ./src/gen_stub.php - - ./src/compiler.php + - ./src - vendor/nikic/php-parser/lib/PhpParser/NodeVisitor.php - vendor/nikic/php-parser/lib/PhpParser/NodeVisitorAbstract.php + +ignore: + - ./src/Assert.php + - ./src/polyfills.php diff --git a/src/Php/Analysis/SsaBuilder.php b/src/Analysis/SsaBuilder.php similarity index 100% rename from src/Php/Analysis/SsaBuilder.php rename to src/Analysis/SsaBuilder.php diff --git a/src/Php/ArgInfo.php b/src/ArgInfo.php similarity index 100% rename from src/Php/ArgInfo.php rename to src/ArgInfo.php diff --git a/src/Php/AstNodeType.php b/src/AstNodeType.php similarity index 100% rename from src/Php/AstNodeType.php rename to src/AstNodeType.php diff --git a/src/Php/Backend/Clang.php b/src/Backend/Clang.php similarity index 100% rename from src/Php/Backend/Clang.php rename to src/Backend/Clang.php diff --git a/src/Php/Backend/CompilerBackend.php b/src/Backend/CompilerBackend.php similarity index 100% rename from src/Php/Backend/CompilerBackend.php rename to src/Backend/CompilerBackend.php diff --git a/src/Php/Backend/CompilerFactory.php b/src/Backend/CompilerFactory.php similarity index 100% rename from src/Php/Backend/CompilerFactory.php rename to src/Backend/CompilerFactory.php diff --git a/src/Php/Backend/Gcc.php b/src/Backend/Gcc.php similarity index 100% rename from src/Php/Backend/Gcc.php rename to src/Backend/Gcc.php diff --git a/src/Php/Backend/GccLikeBackend.php b/src/Backend/GccLikeBackend.php similarity index 100% rename from src/Php/Backend/GccLikeBackend.php rename to src/Backend/GccLikeBackend.php diff --git a/src/Php/Backend/Msvc.php b/src/Backend/Msvc.php similarity index 100% rename from src/Php/Backend/Msvc.php rename to src/Backend/Msvc.php diff --git a/src/Php/CompilerBase.php b/src/CompilerBase.php similarity index 100% rename from src/Php/CompilerBase.php rename to src/CompilerBase.php diff --git a/src/Php/CompilerTest.php b/src/CompilerTest.php similarity index 100% rename from src/Php/CompilerTest.php rename to src/CompilerTest.php diff --git a/src/Php/Constants.php b/src/Constants.php similarity index 100% rename from src/Php/Constants.php rename to src/Constants.php diff --git a/src/Php/Context/FunctionContext.php b/src/Context/FunctionContext.php similarity index 100% rename from src/Php/Context/FunctionContext.php rename to src/Context/FunctionContext.php diff --git a/src/Php/Context/ScopeContext.php b/src/Context/ScopeContext.php similarity index 100% rename from src/Php/Context/ScopeContext.php rename to src/Context/ScopeContext.php diff --git a/src/Php/Entity/ArrayInitPlan.php b/src/Entity/ArrayInitPlan.php similarity index 100% rename from src/Php/Entity/ArrayInitPlan.php rename to src/Entity/ArrayInitPlan.php diff --git a/src/Php/Entity/ClassDef.php b/src/Entity/ClassDef.php similarity index 100% rename from src/Php/Entity/ClassDef.php rename to src/Entity/ClassDef.php diff --git a/src/Php/Entity/ClassLikeDef.php b/src/Entity/ClassLikeDef.php similarity index 100% rename from src/Php/Entity/ClassLikeDef.php rename to src/Entity/ClassLikeDef.php diff --git a/src/Php/Entity/ConstantDef.php b/src/Entity/ConstantDef.php similarity index 100% rename from src/Php/Entity/ConstantDef.php rename to src/Entity/ConstantDef.php diff --git a/src/Php/Entity/FunctionDef.php b/src/Entity/FunctionDef.php similarity index 100% rename from src/Php/Entity/FunctionDef.php rename to src/Entity/FunctionDef.php diff --git a/src/Php/Entity/InterfaceDef.php b/src/Entity/InterfaceDef.php similarity index 100% rename from src/Php/Entity/InterfaceDef.php rename to src/Entity/InterfaceDef.php diff --git a/src/Php/Entity/MethodDef.php b/src/Entity/MethodDef.php similarity index 100% rename from src/Php/Entity/MethodDef.php rename to src/Entity/MethodDef.php diff --git a/src/Php/Entity/PropertyDef.php b/src/Entity/PropertyDef.php similarity index 100% rename from src/Php/Entity/PropertyDef.php rename to src/Entity/PropertyDef.php diff --git a/src/Php/Exception/DynamicCall.php b/src/Exception/DynamicCall.php similarity index 100% rename from src/Php/Exception/DynamicCall.php rename to src/Exception/DynamicCall.php diff --git a/src/Php/Exception/PlaceHolder.php b/src/Exception/PlaceHolder.php similarity index 100% rename from src/Php/Exception/PlaceHolder.php rename to src/Exception/PlaceHolder.php diff --git a/src/Php/Exception/Redo.php b/src/Exception/Redo.php similarity index 100% rename from src/Php/Exception/Redo.php rename to src/Exception/Redo.php diff --git a/src/Php/Exception/Skip.php b/src/Exception/Skip.php similarity index 100% rename from src/Php/Exception/Skip.php rename to src/Exception/Skip.php diff --git a/src/Php/Exception/SyntaxError.php b/src/Exception/SyntaxError.php similarity index 100% rename from src/Php/Exception/SyntaxError.php rename to src/Exception/SyntaxError.php diff --git a/src/Php/Exception/TestError.php b/src/Exception/TestError.php similarity index 100% rename from src/Php/Exception/TestError.php rename to src/Exception/TestError.php diff --git a/src/Php/Exception/Unsupported.php b/src/Exception/Unsupported.php similarity index 100% rename from src/Php/Exception/Unsupported.php rename to src/Exception/Unsupported.php diff --git a/src/Php/Extractor.php b/src/Extractor.php similarity index 100% rename from src/Php/Extractor.php rename to src/Extractor.php diff --git a/src/Php/FileScanner.php b/src/FileScanner.php similarity index 100% rename from src/Php/FileScanner.php rename to src/FileScanner.php diff --git a/src/Php/Generator/AnonClassGenerator.php b/src/Generator/AnonClassGenerator.php similarity index 100% rename from src/Php/Generator/AnonClassGenerator.php rename to src/Generator/AnonClassGenerator.php diff --git a/src/Php/Generator/ClosureGenerator.php b/src/Generator/ClosureGenerator.php similarity index 100% rename from src/Php/Generator/ClosureGenerator.php rename to src/Generator/ClosureGenerator.php diff --git a/src/Php/Generator/PlaceHolderGenerator.php b/src/Generator/PlaceHolderGenerator.php similarity index 100% rename from src/Php/Generator/PlaceHolderGenerator.php rename to src/Generator/PlaceHolderGenerator.php diff --git a/src/Php/Generator/PropertyPromotion.php b/src/Generator/PropertyPromotion.php similarity index 100% rename from src/Php/Generator/PropertyPromotion.php rename to src/Generator/PropertyPromotion.php diff --git a/src/Php/Generator/ResourceFileGenerator.php b/src/Generator/ResourceFileGenerator.php similarity index 100% rename from src/Php/Generator/ResourceFileGenerator.php rename to src/Generator/ResourceFileGenerator.php diff --git a/src/Php/Generator/TypeCheckGenerator.php b/src/Generator/TypeCheckGenerator.php similarity index 100% rename from src/Php/Generator/TypeCheckGenerator.php rename to src/Generator/TypeCheckGenerator.php diff --git a/src/Php/Generator/Utils.php b/src/Generator/Utils.php similarity index 100% rename from src/Php/Generator/Utils.php rename to src/Generator/Utils.php diff --git a/src/Php/MagicMethodDetector.php b/src/MagicMethodDetector.php similarity index 100% rename from src/Php/MagicMethodDetector.php rename to src/MagicMethodDetector.php diff --git a/src/Php/Optimizer/FuncCallOptimizer.php b/src/Optimizer/FuncCallOptimizer.php similarity index 100% rename from src/Php/Optimizer/FuncCallOptimizer.php rename to src/Optimizer/FuncCallOptimizer.php diff --git a/src/Php/Optimizer/LoopVarOptimizer.php b/src/Optimizer/LoopVarOptimizer.php similarity index 100% rename from src/Php/Optimizer/LoopVarOptimizer.php rename to src/Optimizer/LoopVarOptimizer.php diff --git a/src/Php/Optimizer/SsaPropOptimizer.php b/src/Optimizer/SsaPropOptimizer.php similarity index 100% rename from src/Php/Optimizer/SsaPropOptimizer.php rename to src/Optimizer/SsaPropOptimizer.php diff --git a/src/Php/Optimizer/SsaTypeOptimizer.php b/src/Optimizer/SsaTypeOptimizer.php similarity index 100% rename from src/Php/Optimizer/SsaTypeOptimizer.php rename to src/Optimizer/SsaTypeOptimizer.php diff --git a/src/Php/Parser/AssignOpTrait.php b/src/Parser/AssignOpTrait.php similarity index 100% rename from src/Php/Parser/AssignOpTrait.php rename to src/Parser/AssignOpTrait.php diff --git a/src/Php/Parser/BinaryOpTrait.php b/src/Parser/BinaryOpTrait.php similarity index 100% rename from src/Php/Parser/BinaryOpTrait.php rename to src/Parser/BinaryOpTrait.php diff --git a/src/Php/Parser/StdContainerTrait.php b/src/Parser/StdContainerTrait.php similarity index 100% rename from src/Php/Parser/StdContainerTrait.php rename to src/Parser/StdContainerTrait.php diff --git a/src/Php/Parser/TypeConversionTrait.php b/src/Parser/TypeConversionTrait.php similarity index 100% rename from src/Php/Parser/TypeConversionTrait.php rename to src/Parser/TypeConversionTrait.php diff --git a/src/Php/Parser/TypeDetectionTrait.php b/src/Parser/TypeDetectionTrait.php similarity index 100% rename from src/Php/Parser/TypeDetectionTrait.php rename to src/Parser/TypeDetectionTrait.php diff --git a/src/Php/Platform/Linux.php b/src/Platform/Linux.php similarity index 100% rename from src/Php/Platform/Linux.php rename to src/Platform/Linux.php diff --git a/src/Php/Platform/Macos.php b/src/Platform/Macos.php similarity index 100% rename from src/Php/Platform/Macos.php rename to src/Platform/Macos.php diff --git a/src/Php/Platform/PlatformBase.php b/src/Platform/PlatformBase.php similarity index 100% rename from src/Php/Platform/PlatformBase.php rename to src/Platform/PlatformBase.php diff --git a/src/Php/Platform/PlatformFactory.php b/src/Platform/PlatformFactory.php similarity index 100% rename from src/Php/Platform/PlatformFactory.php rename to src/Platform/PlatformFactory.php diff --git a/src/Php/Platform/UnixPlatform.php b/src/Platform/UnixPlatform.php similarity index 100% rename from src/Php/Platform/UnixPlatform.php rename to src/Platform/UnixPlatform.php diff --git a/src/Php/Platform/Windows.php b/src/Platform/Windows.php similarity index 100% rename from src/Php/Platform/Windows.php rename to src/Platform/Windows.php diff --git a/src/Php/Preprocessor.php b/src/Preprocessor.php similarity index 100% rename from src/Php/Preprocessor.php rename to src/Preprocessor.php diff --git a/src/Php/Reflection.php b/src/Reflection.php similarity index 100% rename from src/Php/Reflection.php rename to src/Reflection.php diff --git a/src/Php/Resolver/InstancePropertyFetchTarget.php b/src/Resolver/InstancePropertyFetchTarget.php similarity index 100% rename from src/Php/Resolver/InstancePropertyFetchTarget.php rename to src/Resolver/InstancePropertyFetchTarget.php diff --git a/src/Php/Resolver/NativePropertyAccess.php b/src/Resolver/NativePropertyAccess.php similarity index 100% rename from src/Php/Resolver/NativePropertyAccess.php rename to src/Resolver/NativePropertyAccess.php diff --git a/src/Php/Resolver/PropertyAccessContext.php b/src/Resolver/PropertyAccessContext.php similarity index 100% rename from src/Php/Resolver/PropertyAccessContext.php rename to src/Resolver/PropertyAccessContext.php diff --git a/src/Php/Resolver/PropertyAccessResolver.php b/src/Resolver/PropertyAccessResolver.php similarity index 100% rename from src/Php/Resolver/PropertyAccessResolver.php rename to src/Resolver/PropertyAccessResolver.php diff --git a/src/Php/Resolver/PropertyAccessResult.php b/src/Resolver/PropertyAccessResult.php similarity index 100% rename from src/Php/Resolver/PropertyAccessResult.php rename to src/Resolver/PropertyAccessResult.php diff --git a/src/Php/Resolver/PropertyAssignTypeInfo.php b/src/Resolver/PropertyAssignTypeInfo.php similarity index 100% rename from src/Php/Resolver/PropertyAssignTypeInfo.php rename to src/Resolver/PropertyAssignTypeInfo.php diff --git a/src/Php/Resolver/PropertyWriteTarget.php b/src/Resolver/PropertyWriteTarget.php similarity index 100% rename from src/Php/Resolver/PropertyWriteTarget.php rename to src/Resolver/PropertyWriteTarget.php diff --git a/src/Php/Resolver/StaticPropertyFetchResolution.php b/src/Resolver/StaticPropertyFetchResolution.php similarity index 100% rename from src/Php/Resolver/StaticPropertyFetchResolution.php rename to src/Resolver/StaticPropertyFetchResolution.php diff --git a/src/Php/Resolver/StaticPropertyFetchTarget.php b/src/Resolver/StaticPropertyFetchTarget.php similarity index 100% rename from src/Php/Resolver/StaticPropertyFetchTarget.php rename to src/Resolver/StaticPropertyFetchTarget.php diff --git a/src/Php/Symbol.php b/src/Symbol.php similarity index 100% rename from src/Php/Symbol.php rename to src/Symbol.php diff --git a/src/Php/Translator.php b/src/Translator.php similarity index 100% rename from src/Php/Translator.php rename to src/Translator.php diff --git a/src/Php/UniversalMethodCall.php b/src/UniversalMethodCall.php similarity index 100% rename from src/Php/UniversalMethodCall.php rename to src/UniversalMethodCall.php diff --git a/src/Php/Visitor.php b/src/Visitor.php similarity index 100% rename from src/Php/Visitor.php rename to src/Visitor.php