Skip to content

Commit 9331cc1

Browse files
committed
Add Flat and Ast builders
1 parent f8b2a85 commit 9331cc1

10 files changed

Lines changed: 163 additions & 44 deletions

File tree

tools/FileRefExtractor/ActionOp.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
interface ActionOp
2222
{
23-
public function build(TLContext $tl): array;
23+
public function build(TLContext $tl): void;
2424

2525
public function normalize(array $stack, string $current): ?self;
2626
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of MadelineProto.
7+
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
8+
* MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9+
* See the GNU Affero General Public License for more details.
10+
* You should have received a copy of the GNU General Public License along with MadelineProto.
11+
* If not, see <http://www.gnu.org/licenses/>.
12+
*
13+
* @author Daniil Gentili <daniil@daniil.it>
14+
* @copyright 2016-2025 Daniil Gentili <daniil@daniil.it>
15+
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
16+
* @link https://docs.madelineproto.xyz MadelineProto documentation
17+
*/
18+
19+
namespace danog\MadelineProto\FileRefExtractor;
20+
21+
interface BuildMode
22+
{
23+
public function cleanup(): void;
24+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of MadelineProto.
7+
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
8+
* MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9+
* See the GNU Affero General Public License for more details.
10+
* You should have received a copy of the GNU General Public License along with MadelineProto.
11+
* If not, see <http://www.gnu.org/licenses/>.
12+
*
13+
* @author Daniil Gentili <daniil@daniil.it>
14+
* @copyright 2016-2025 Daniil Gentili <daniil@daniil.it>
15+
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
16+
* @link https://docs.madelineproto.xyz MadelineProto documentation
17+
*/
18+
19+
namespace danog\MadelineProto\FileRefExtractor\BuildMode;
20+
21+
use danog\MadelineProto\FileRefExtractor\BuildMode;
22+
23+
final class Ast implements BuildMode
24+
{
25+
public array $output = [];
26+
public ?string $needsMethod = null;
27+
28+
public function cleanup(): void
29+
{
30+
$this->needsMethod = null;
31+
}
32+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of MadelineProto.
7+
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
8+
* MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9+
* See the GNU Affero General Public License for more details.
10+
* You should have received a copy of the GNU General Public License along with MadelineProto.
11+
* If not, see <http://www.gnu.org/licenses/>.
12+
*
13+
* @author Daniil Gentili <daniil@daniil.it>
14+
* @copyright 2016-2025 Daniil Gentili <daniil@daniil.it>
15+
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
16+
* @link https://docs.madelineproto.xyz MadelineProto documentation
17+
*/
18+
19+
namespace danog\MadelineProto\FileRefExtractor\BuildMode;
20+
21+
use danog\MadelineProto\FileRefExtractor\BuildMode;
22+
23+
final class Flat implements BuildMode
24+
{
25+
public array $actionsPre = [];
26+
public array $actionsPost = [];
27+
public array $backrefs = [];
28+
public string $contextName;
29+
30+
public function cleanup(): void
31+
{
32+
$this->actionsPre = [];
33+
$this->actionsPost = [];
34+
}
35+
}

tools/FileRefExtractor/Ops/CallOp.php

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
namespace danog\MadelineProto\FileRefExtractor\Ops;
2020

2121
use danog\MadelineProto\FileRefExtractor\ActionOp;
22+
use danog\MadelineProto\FileRefExtractor\BuildMode\Ast;
23+
use danog\MadelineProto\FileRefExtractor\BuildMode\Flat;
2224
use danog\MadelineProto\FileRefExtractor\TLContext;
2325
use danog\MadelineProto\FileRefExtractor\TypedOp;
2426
use Webmozart\Assert\Assert;
@@ -64,48 +66,64 @@ public static function simple(string $method, string $constructor, array $args):
6466
return new CallOp($method, $final);
6567
}
6668

67-
public function build(TLContext $tl): array
69+
public function build(TLContext $tl): void
6870
{
6971
$final = [];
7072
$tl->validateParams($this->method, false, $this->args);
73+
$types = [];
7174
foreach ($this->args as $from => $to) {
7275
$final[$from] = $to->build($tl);
73-
}
74-
$hasBackref = (bool) $tl->tl->backrefs;
75-
if (!$hasBackref) {
76-
$tl->tl->backrefs[$this->method] = true;
76+
$types[$from] = $to->getType($tl);
7777
}
7878

79-
foreach ($tl->tl->backrefs as $cons => $_) {
80-
$tl->tl->actionsPre[$cons] ??= [];
81-
array_unshift($tl->tl->actionsPre[$cons], [
82-
'op' => 'pushContext',
83-
'ctx' => $tl->contextName,
84-
]);
79+
$out = $tl->buildMode;
80+
if ($out instanceof Flat) {
81+
foreach ($out->backrefs as $cons => $type) {
82+
$out->actionsPre[$cons] ??= [];
83+
array_unshift($out->actionsPre[$cons], [
84+
'op' => 'pushContext',
85+
'ctx' => $out->contextName,
86+
]);
8587

86-
$tl->tl->actionsPost[$cons] ??= [];
87-
array_push($tl->tl->actionsPost[$cons], [
88-
'op' => 'popAndProcessContext',
89-
'ctx' => $tl->contextName,
90-
]);
91-
}
88+
$out->actionsPost[$cons] ??= [];
89+
array_push($out->actionsPost[$cons], [
90+
'op' => 'processContext',
91+
'ctx' => $out->contextName,
92+
'method' => $this->method,
93+
'args' => $final,
94+
]);
95+
array_push($out->actionsPost[$cons], [
96+
'op' => 'popContext',
97+
'ctx' => $out->contextName,
98+
]);
99+
}
92100

93-
if ($hasBackref) {
94-
$tl->tl->actionsPost[$cons][] = [
101+
$out->actionsPost[$cons][] = [
95102
'op' => 'processContext',
96-
'ctx' => $tl->contextName,
103+
'ctx' => $out->contextName,
104+
'method' => $this->method,
105+
'args' => $final,
97106
];
98-
$tl->tl->actionsPost[$cons][] = [
99-
'op' => 'deleteContextEntries',
100-
'ctx' => $tl->contextName,
101-
'entries' => array_keys($final),
107+
if ($hasBackref) {
108+
$out->actionsPost[$cons][] = [
109+
'op' => 'deleteContextEntries',
110+
'ctx' => $out->contextName,
111+
'entries' => array_keys($final),
112+
];
113+
} else {
114+
$out->actionsPost[$cons][] = [
115+
'op' => 'popContext',
116+
'ctx' => $out->contextName,
117+
];
118+
}
119+
} else {
120+
assert($out instanceof Ast);
121+
$out->output[$tl->position][] = [
122+
'op' => 'call',
123+
'method' => $this->method,
124+
'args' => $final,
125+
'needsMethod' => $out->needsMethod
102126
];
103127
}
104-
105-
return [
106-
'op' => 'call',
107-
'method' => $this->method,
108-
'args' => $final,
109-
];
110128
}
111129
}

tools/FileRefExtractor/Ops/CopyMethodCallOp.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
namespace danog\MadelineProto\FileRefExtractor\Ops;
2020

2121
use danog\MadelineProto\FileRefExtractor\ActionOp;
22+
use danog\MadelineProto\FileRefExtractor\BuildMode;
23+
use danog\MadelineProto\FileRefExtractor\BuildMode\Ast;
2224
use danog\MadelineProto\FileRefExtractor\TLContext;
2325
use Webmozart\Assert\Assert;
2426

@@ -35,10 +37,18 @@ public function normalize(array $stack, string $current): ?\danog\MadelineProto\
3537
return $this;
3638
}
3739

38-
public function build(TLContext $tl): array
40+
public function build(TLContext $tl): void
3941
{
4042
Assert::eq($tl->position, $this->method, "Current constructor {$tl->position} does not match expected method {$this->method}");
4143
$tl->tl->tl->getMethods()->findByMethod($this->method)['type']; // Validate type
42-
return ['op' => 'copyMethodCall', 'method' => $this->method];
44+
$out = $tl->buildMode;
45+
if ($out instanceof Ast) {
46+
$out->output[$this->method][] = [
47+
'op' => 'copyMethodCall',
48+
'ctx' => $out->contextName,
49+
'method' => $this->method,
50+
];
51+
} else {
52+
}
4353
}
4454
}

tools/FileRefExtractor/Ops/ExtractFromHereOp.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
use danog\MadelineProto\FileRefExtractor\FieldExtractorOp;
2222
use danog\MadelineProto\FileRefExtractor\TLContext;
23-
use danog\MadelineProto\FileRefExtractor\TypedOp;
2423
use Webmozart\Assert\Assert;
2524

2625
final readonly class ExtractFromHereOp implements FieldExtractorOp
@@ -29,7 +28,7 @@ public function __construct(
2928
/** @var string[] */
3029
public readonly array $path,
3130
public readonly bool $isFlag = false,
32-
public readonly ?TypedOp $ifEmptyFlag = null,
31+
public readonly ?FieldExtractorOp $ifEmptyFlag = null,
3332
) {
3433
if ($ifEmptyFlag !== null) {
3534
Assert::true($isFlag);
@@ -68,7 +67,7 @@ public function build(TLContext $tl): array
6867
{
6968
// Validate
7069
$this->getType($tl);
71-
70+
7271
return [
7372
'op' => 'extractFromHere',
7473
'isFlag' => $this->isFlag,

tools/FileRefExtractor/Ops/ExtractFromMethodCallOp.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
namespace danog\MadelineProto\FileRefExtractor\Ops;
2020

21+
use danog\MadelineProto\FileRefExtractor\BuildMode\Ast;
22+
use danog\MadelineProto\FileRefExtractor\BuildMode\Flat;
2123
use danog\MadelineProto\FileRefExtractor\FieldExtractorOp;
2224
use danog\MadelineProto\FileRefExtractor\TLContext;
2325
use danog\MadelineProto\FileRefExtractor\TypedOp;
@@ -69,7 +71,12 @@ public function extend(string ...$path): self
6971
public function build(TLContext $tl): array
7072
{
7173
// Validate
72-
$this->getType($tl);
74+
$t = $this->getType($tl);
75+
if ($tl->buildMode instanceof Flat) {
76+
} elseif ($tl->buildMode instanceof Ast) {
77+
Assert::eq($tl->buildMode->needsMethod ?? $this->path[0], $this->path[0]);
78+
$tl->buildMode->needsMethod = $this->path[0];
79+
}
7380
return [
7481
'op' => 'extractFromMethodCall',
7582
'isFlag' => $this->isFlag,

tools/FileRefExtractor/TLContext.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
{
2727
public function __construct(
2828
public TLWrapper $tl,
29-
public string $contextName,
29+
public BuildMode $buildMode,
3030
public string $position,
3131
public bool $normalized = false,
3232
) {

tools/FileRefExtractor/TLWrapper.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,8 @@ final class TLWrapper
2626
private readonly array $constructorsOfType;
2727
private readonly array $methodsOfType;
2828

29-
public array $actionsPre = [];
30-
public array $actionsPost = [];
31-
public array $backrefs = [];
32-
3329
public function __construct(
3430
public readonly TLInterface $tl,
35-
public readonly ?string $position = null,
36-
public readonly bool $normalized = false,
3731
) {
3832
$constructorsOfType = [];
3933
$methodsOfType = [];

0 commit comments

Comments
 (0)