Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ when any `.xphp` source changes. No separate `xphp:compile` step is needed.
generated `XPHP\Generated\…` namespace. Write `\App\Finder\SourceInterface`, not a
`use`d `SourceInterface`.
- **You don't need a call site.** A generic that's only injected is never written as
`new Foo<Bar>()` anywhere, yet monomorphization is usage-driven. The bundle bridges
`new Foo::<Bar>()` anywhere, yet monomorphization is usage-driven. The bundle bridges
this by synthesizing a throwaway instantiation for each declared binding at compile
time (transparent; cleaned up afterwards). *(Stopgap: a future xphp release may
accept specialization roots directly.)*
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
},
"require": {
"php": "^8.4.0",
"xphp-lang/xphp": "^0.1",
"symfony/config": "^8.0",
"symfony/console": "^8.0",
"symfony/dependency-injection": "^8.0",
"symfony/http-kernel": "^8.0"
"symfony/http-kernel": "^8.0",
"xphp-lang/xphp": "^v0.2.0"
},
"require-dev": {
"symfony/framework-bundle": "^8.0",
Expand Down
2 changes: 1 addition & 1 deletion demo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ src/
├── Command/DemoCommand.php # handwritten PHP — calls into the compiled Catalog
├── Models/Plastic.php # handwritten PHP — left untouched by the build
├── Containers/Box.xphp # generic template class Box<T>
├── Demo/Catalog.xphp # uses new Box<Plastic>() (so it must be .xphp)
├── Demo/Catalog.xphp # uses new Box::<Plastic>() (so it must be .xphp)
└── Kernel.php
```

Expand Down
8 changes: 4 additions & 4 deletions demo/src/Demo/Catalog.xphp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use App\Containers\Box;
use App\Models\Plastic;

/**
* Exercises the generic. Because it instantiates `new Box<Plastic>()`, this file
* Exercises the generic. Because it instantiates `new Box::<Plastic>()`, this file
* must be .xphp — the compiler rewrites those call sites to the concrete
* XPHP\Generated\App\Containers\Box\T_<hash> classes. The public API
* (`colors()`, `boxClass()`) is plain PHP, so handwritten code can call it
Expand All @@ -21,10 +21,10 @@ final class Catalog
*/
public function colors(): array
{
$red = new Box<Plastic>();
$red = new Box::<Plastic>();
$red->set(new Plastic('red'));

$blue = new Box<Plastic>();
$blue = new Box::<Plastic>();
$blue->set(new Plastic('blue'));

return [$red->get()->color, $blue->get()->color];
Expand All @@ -36,7 +36,7 @@ final class Catalog
*/
public function boxClass(): string
{
$box = new Box<Plastic>();
$box = new Box::<Plastic>();

return $box::class;
}
Expand Down
25 changes: 22 additions & 3 deletions src/Compiler/XphpCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ private function runCompiler(): string
* Monomorphization is usage-driven: the transpiler only emits a `T_<hash>`
* specialization where source actually instantiates `Foo<Bar>`. A generic that is
* only declared as a DI service (and injected via an interface) is never written
* as `new Foo<Bar>()` anywhere, so its specialization would never be generated.
* as `new Foo::<Bar>()` anywhere, so its specialization would never be generated.
*
* As a stopgap we synthesize a throwaway `.xphp` file containing one
* `new Template<Args>();` per declared root, dropped into the source set so the
* `new Template::<Args>();` per declared root, dropped into the source set so the
* transpiler treats it as a usage. The rewritten copy that lands in the target dir
* is dead code (it is never autoloaded — the file declares no class) and is removed
* by {@see removeRootsArtifacts()} along with the source file.
Expand All @@ -127,12 +127,31 @@ private function writeRootsFile(): void

$lines = ['<?php', '', '// Auto-generated by xphp-symfony-bundle; safe to delete.'];
foreach ($this->roots as $root) {
$lines[] = sprintf('new %s();', $root);
$lines[] = sprintf('new %s();', self::toTurbofish($root));
}

file_put_contents($dir . \DIRECTORY_SEPARATOR . 'roots.xphp', implode("\n", $lines) . "\n");
}

/**
* Turn a canonical generic-instantiation expression (`Foo\Bar<Baz>`) into the
* call-site turbofish form xphp expects (`Foo\Bar::<Baz>`). xphp >= 0.2 requires
* the `::<…>` operator at instantiation sites — bare `new Foo<Baz>()` is now a
* parse error (`Foo < Baz` is ambiguous with comparison). Only the outermost
* `<` is the call site; nested type arguments keep their plain `<…>` form. A root
* with no type arguments becomes an empty turbofish (`Foo::<>`) so an
* all-defaulted template still triggers specialization.
*/
private static function toTurbofish(string $canonical): string
{
$pos = strpos($canonical, '<');
if ($pos === false) {
return $canonical . '::<>';
}

return substr($canonical, 0, $pos) . '::' . substr($canonical, $pos);
}

private function removeRootsArtifacts(): void
{
if ($this->roots === []) {
Expand Down
Loading