diff --git a/src/main/php/lang/ast/CodeGen.class.php b/src/main/php/lang/ast/CodeGen.class.php index 439a1957..8066878a 100755 --- a/src/main/php/lang/ast/CodeGen.class.php +++ b/src/main/php/lang/ast/CodeGen.class.php @@ -1,5 +1,7 @@ scope[0] ?? null; + + if ('self' === $type || 'static' === $type) { + return new Declaration($enclosing->type); + } else if ('parent' === $type) { + return $enclosing->type->parent ? $this->lookup($enclosing->type->parent->literal()) : null; + } + + foreach ($this->scope as $scope) { + if ($scope->type->name && $type === $scope->type->name->literal()) { + return new Declaration($scope->type); + } + } + + if (class_exists($type) || interface_exists($type) || trait_exists($type) || enum_exists($type)) { + return new Reflection($type); + } else { + return new Incomplete($type); + } + } } \ No newline at end of file diff --git a/src/main/php/lang/ast/emit/Declaration.class.php b/src/main/php/lang/ast/emit/Declaration.class.php index f3d58a3a..82240a57 100755 --- a/src/main/php/lang/ast/emit/Declaration.class.php +++ b/src/main/php/lang/ast/emit/Declaration.class.php @@ -3,17 +3,13 @@ use lang\ast\nodes\{EnumCase, Property}; class Declaration extends Type { - private $type, $result; + private $type; static function __static() { } - /** - * @param lang.ast.nodes.TypeDeclaration $type - * @param lang.ast.Result $result - */ - public function __construct($type, $result) { + /** @param lang.ast.nodes.TypeDeclaration $type */ + public function __construct($type) { $this->type= $type; - $this->result= $result; } /** @return string */ diff --git a/src/main/php/lang/ast/emit/GeneratedCode.class.php b/src/main/php/lang/ast/emit/GeneratedCode.class.php index 9c1716af..e02acbe9 100755 --- a/src/main/php/lang/ast/emit/GeneratedCode.class.php +++ b/src/main/php/lang/ast/emit/GeneratedCode.class.php @@ -62,28 +62,11 @@ public function temp() { /** * Looks up a given type * + * @deprecated Use `CodeGen::lookup()` instead! * @param string $type * @return lang.ast.emit.Type */ public function lookup($type) { - $enclosing= $this->codegen->scope[0] ?? null; - - if ('self' === $type || 'static' === $type) { - return new Declaration($enclosing->type, $this); - } else if ('parent' === $type) { - return $enclosing->type->parent ? $this->lookup($enclosing->type->parent->literal()) : null; - } - - foreach ($this->codegen->scope as $scope) { - if ($scope->type->name && $type === $scope->type->name->literal()) { - return new Declaration($scope->type, $this); - } - } - - if (class_exists($type) || interface_exists($type) || trait_exists($type) || enum_exists($type)) { - return new Reflection($type); - } else { - return new Incomplete($type); - } + return $this->codegen->lookup($type); } } \ No newline at end of file diff --git a/src/main/php/lang/ast/emit/PHP.class.php b/src/main/php/lang/ast/emit/PHP.class.php index 0ddb234a..1f6ffb34 100755 --- a/src/main/php/lang/ast/emit/PHP.class.php +++ b/src/main/php/lang/ast/emit/PHP.class.php @@ -70,7 +70,7 @@ protected function isConstant($result, $node) { return ( $node->member instanceof Literal && is_string($node->type) && - !$result->lookup($node->type)->rewriteEnumCase($node->member->expression) + !$result->codegen->lookup($node->type)->rewriteEnumCase($node->member->expression) ); } else if ($node instanceof BinaryExpression) { return $this->isConstant($result, $node->left) && $this->isConstant($result, $node->right); @@ -1064,7 +1064,7 @@ protected function emitScope($result, $scope) { $scope->member instanceof Literal && is_string($scope->type) && 'class' !== $scope->member->expression && - $result->lookup($scope->type)->rewriteEnumCase($scope->member->expression) + $result->codegen->lookup($scope->type)->rewriteEnumCase($scope->member->expression) ) { $result->out->write('$'.$scope->member->expression); } else { diff --git a/src/test/php/lang/ast/unittest/emit/GeneratedCodeTest.class.php b/src/test/php/lang/ast/unittest/emit/GeneratedCodeTest.class.php index 87a9ff3a..a5fc9402 100755 --- a/src/test/php/lang/ast/unittest/emit/GeneratedCodeTest.class.php +++ b/src/test/php/lang/ast/unittest/emit/GeneratedCodeTest.class.php @@ -49,7 +49,7 @@ public function lookup_self() { $r= new GeneratedCode(new MemoryOutputStream()); $context= $r->codegen->enter(new InType(new ClassDeclaration([], new IsValue('\\T'), null, [], [], null, null, 1))); - Assert::equals(new Declaration($context->type, $r), $r->lookup('self')); + Assert::equals(new Declaration($context->type), $r->lookup('self')); } #[Test] @@ -73,7 +73,7 @@ public function lookup_named() { $r= new GeneratedCode(new MemoryOutputStream()); $context= $r->codegen->enter(new InType(new ClassDeclaration([], new IsValue('\\T'), null, [], [], null, null, 1))); - Assert::equals(new Declaration($context->type, $r), $r->lookup('\\T')); + Assert::equals(new Declaration($context->type), $r->lookup('\\T')); } #[Test]