Skip to content

Emit meta data for class constants #163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 17, 2023
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"php" : ">=7.0.0"
},
"require-dev" : {
"xp-framework/reflection": "dev-feature/typed_class_constants as 2.11.0",
"xp-framework/test": "^1.0"
},
"bin": ["bin/xp.xp-framework.compiler.compile", "bin/xp.xp-framework.compiler.ast"],
Expand Down
8 changes: 8 additions & 0 deletions src/main/php/lang/ast/emit/OmitConstModifiers.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
trait OmitConstModifiers {

protected function emitConst($result, $const) {
$result->codegen->scope[0]->meta[self::CONSTANT][$const->name]= [
DETAIL_RETURNS => $const->type ? $const->type->name() : 'var',
DETAIL_ANNOTATIONS => $const->annotations,
DETAIL_COMMENT => $const->comment,
DETAIL_TARGET_ANNO => [],
DETAIL_ARGUMENTS => []
];

$result->out->write('const '.$const->name.'=');
$this->emitOne($result, $const->expression);
$result->out->write(';');
Expand Down
9 changes: 9 additions & 0 deletions src/main/php/lang/ast/emit/PHP.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
abstract class PHP extends Emitter {
const PROPERTY = 0;
const METHOD = 1;
const CONSTANT = 2;

protected $literals= [];

Expand Down Expand Up @@ -562,6 +563,14 @@ protected function emitUse($result, $use) {
}

protected function emitConst($result, $const) {
$result->codegen->scope[0]->meta[self::CONSTANT][$const->name]= [
DETAIL_RETURNS => $const->type ? $const->type->name() : 'var',
DETAIL_ANNOTATIONS => $const->annotations,
DETAIL_COMMENT => $const->comment,
DETAIL_TARGET_ANNO => [],
DETAIL_ARGUMENTS => []
];

$const->comment && $this->emitOne($result, $const->comment);
$const->annotations && $this->emitOne($result, $const->annotations);
$result->at($const->declared)->out->write(implode(' ', $const->modifiers).' const '.$const->name.'=');
Expand Down
14 changes: 6 additions & 8 deletions src/test/php/lang/ast/unittest/emit/MembersTest.class.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php namespace lang\ast\unittest\emit;

use lang\ArrayType;
use lang\{ArrayType, Primitive, Reflection};
use test\{Assert, Ignore, Test, Values};

class MembersTest extends EmittingTest {
Expand Down Expand Up @@ -59,15 +59,13 @@ public function run() {

#[Test]
public function typed_class_constant() {
$r= $this->run('class <T> {
$t= Reflection::type($this->type('class <T> {
private const string MEMBER = "Test";
}'));
$const= $t->constant('MEMBER');

public function run() {
return self::MEMBER;
}
}');

Assert::equals('Test', $r);
Assert::equals('Test', $const->value());
Assert::equals(Primitive::$STRING, $const->constraint()->type());
}

#[Test, Values(['$this->$member', '$this->{$member}', '$this->{strtoupper($member)}'])]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php namespace lang\ast\unittest\emit;

use lang\ast\emit\{OmitConstModifiers, PHP};
use lang\ast\nodes\{Constant, Literal};
use lang\ast\types\IsLiteral;
use test\{Assert, Test};
use lang\ast\nodes\{Constant, ClassDeclaration, Literal};
use lang\ast\types\{IsLiteral, IsValue};
use test\{Assert, Before, Test};

class OmitConstModifiersTest extends EmitterTraitTest {
private $type;

/** @return lang.ast.Emitter */
protected function fixture() {
Expand All @@ -14,19 +15,20 @@ protected function fixture() {
};
}

#[Before]
public function type() {
$this->type= new ClassDeclaration([], new IsValue('\\T'), null, [], [], null, null, 1);
}

#[Test]
public function omits_type() {
Assert::equals(
'const TEST="test";',
$this->emit(new Constant([], 'TEST', new IsLiteral('string'), new Literal('"test"')))
);
$const= new Constant([], 'TEST', new IsLiteral('string'), new Literal('"test"'));
Assert::equals('const TEST="test";', $this->emit($const, [$this->type]));
}

#[Test]
public function omits_modifier() {
Assert::equals(
'const TEST="test";',
$this->emit(new Constant(['private'], 'TEST', new IsLiteral('string'), new Literal('"test"')))
);
$const= new Constant(['private'], 'TEST', new IsLiteral('string'), new Literal('"test"'));
Assert::equals('const TEST="test";', $this->emit($const, [$this->type]));
}
}