1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Railt package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Railt\SDL\Frontend\IR; |
11
|
|
|
|
12
|
|
|
use Railt\Io\PositionInterface; |
13
|
|
|
use Railt\Io\Readable; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class JoinableOpcode |
17
|
|
|
*/ |
18
|
|
|
class JoinedOpcode extends Opcode implements PositionInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var int |
22
|
|
|
*/ |
23
|
|
|
private $id; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Readable |
27
|
|
|
*/ |
28
|
|
|
private $file; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var int |
32
|
|
|
*/ |
33
|
|
|
private $offset; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var \Closure |
37
|
|
|
*/ |
38
|
|
|
private $description; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* JoinableOpcode constructor. |
42
|
|
|
* @param OpcodeInterface|self $opcode |
43
|
|
|
* @param int $id |
44
|
|
|
* @param Readable $file |
45
|
|
|
* @param int $offset |
46
|
|
|
*/ |
47
|
|
|
public function __construct(OpcodeInterface $opcode, int $id, Readable $file, int $offset = 0) |
48
|
|
|
{ |
49
|
|
|
parent::__construct($opcode->getOperation(), ...$opcode->operands); |
|
|
|
|
50
|
|
|
|
51
|
|
|
$this->id = $id; |
52
|
|
|
$this->file = $file; |
53
|
|
|
$this->offset = $offset; |
54
|
|
|
|
55
|
|
|
$this->description = function () use ($opcode): string { |
56
|
|
|
return \trim((string)(new \ReflectionObject($opcode))->getDocComment(), " \t\n\r\0\x0B/*"); |
57
|
|
|
}; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return int |
62
|
|
|
*/ |
63
|
|
|
public function getOffset(): int |
64
|
|
|
{ |
65
|
|
|
return $this->offset; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
public function getDescription(): string |
72
|
|
|
{ |
73
|
|
|
return \preg_replace_callback('/\$(\d+)/iu', function (array $m): string { |
74
|
|
|
return $this->operandToString($this->operands[(int)$m[1]] ?? null); |
75
|
|
|
}, ($this->description)()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return Readable |
80
|
|
|
*/ |
81
|
|
|
public function getFile(): Readable |
82
|
|
|
{ |
83
|
|
|
return $this->file; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return PositionInterface |
88
|
|
|
*/ |
89
|
|
|
private function getPosition(): PositionInterface |
90
|
|
|
{ |
91
|
|
|
return $this->file->getPosition($this->offset); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return int |
96
|
|
|
*/ |
97
|
|
|
public function getLine(): int |
98
|
|
|
{ |
99
|
|
|
return $this->getPosition()->getLine(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return int |
104
|
|
|
*/ |
105
|
|
|
public function getColumn(): int |
106
|
|
|
{ |
107
|
|
|
return $this->getPosition()->getColumn(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return int |
112
|
|
|
*/ |
113
|
|
|
public function getId(): int |
114
|
|
|
{ |
115
|
|
|
return $this->id; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
public function __toString(): string |
122
|
|
|
{ |
123
|
|
|
return \vsprintf('%4s | %-80s %s:%d', [ |
124
|
|
|
'#' . $this->getId(), |
125
|
|
|
parent::__toString(), |
126
|
|
|
$this->getFile()->getPathname(), |
127
|
|
|
$this->getLine(), |
128
|
|
|
]); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: