#!/usr/bin/env php
getPathName();
if (preg_match('/\.stub\.php$/', $pathName)) {
$fileInfo = processStubFile($pathName, $context);
if ($fileInfo) {
$fileInfos[] = $fileInfo;
}
}
}
return $fileInfos;
}
function processStubFile(string $stubFile, Context $context): ?FileInfo {
try {
if (!file_exists($stubFile)) {
throw new Exception("File $stubFile does not exist");
}
$arginfoFile = str_replace('.stub.php', '_arginfo.h', $stubFile);
$legacyFile = str_replace('.stub.php', '_legacy_arginfo.h', $stubFile);
$stubCode = file_get_contents($stubFile);
$stubHash = computeStubHash($stubCode);
$oldStubHash = extractStubHash($arginfoFile);
if ($stubHash === $oldStubHash && !$context->forceParse) {
/* Stub file did not change, do not regenerate. */
return null;
}
initPhpParser();
$fileInfo = parseStubFile($stubCode);
$arginfoCode = generateArgInfoCode($fileInfo, $stubHash);
if (($context->forceRegeneration || $stubHash !== $oldStubHash) && file_put_contents($arginfoFile, $arginfoCode)) {
echo "Saved $arginfoFile\n";
}
if ($fileInfo->generateLegacyArginfo) {
foreach ($fileInfo->getAllFuncInfos() as $funcInfo) {
$funcInfo->discardInfoForOldPhpVersions();
}
$arginfoCode = generateArgInfoCode($fileInfo, $stubHash);
if (($context->forceRegeneration || $stubHash !== $oldStubHash) && file_put_contents($legacyFile, $arginfoCode)) {
echo "Saved $legacyFile\n";
}
}
return $fileInfo;
} catch (Exception $e) {
echo "In $stubFile:\n{$e->getMessage()}\n";
exit(1);
}
}
function computeStubHash(string $stubCode): string {
return sha1(str_replace("\r\n", "\n", $stubCode));
}
function extractStubHash(string $arginfoFile): ?string {
if (!file_exists($arginfoFile)) {
return null;
}
$arginfoCode = file_get_contents($arginfoFile);
if (!preg_match('/\* Stub hash: ([0-9a-f]+) \*/', $arginfoCode, $matches)) {
return null;
}
return $matches[1];
}
class Context {
/** @var bool */
public $forceParse = false;
/** @var bool */
public $forceRegeneration = false;
}
class SimpleType {
/** @var string */
public $name;
/** @var bool */
public $isBuiltin;
public function __construct(string $name, bool $isBuiltin) {
$this->name = $name;
$this->isBuiltin = $isBuiltin;
}
public static function fromNode(Node $node): SimpleType {
if ($node instanceof Node\Name) {
if ($node->toLowerString() === 'static') {
// PHP internally considers "static" a builtin type.
return new SimpleType($node->toString(), true);
}
if ($node->toLowerString() === 'self') {
throw new Exception('The exact class name must be used instead of "self"');
}
assert($node->isFullyQualified());
return new SimpleType($node->toString(), false);
}
if ($node instanceof Node\Identifier) {
return new SimpleType($node->toString(), true);
}
throw new Exception("Unexpected node type");
}
public static function fromPhpDoc(string $type): SimpleType
{
switch (strtolower($type)) {
case "void":
case "null":
case "false":
case "bool":
case "int":
case "float":
case "string":
case "array":
case "iterable":
case "object":
case "resource":
case "mixed":
case "static":
return new SimpleType(strtolower($type), true);
case "self":
throw new Exception('The exact class name must be used instead of "self"');
}
if (strpos($type, "[]") !== false) {
return new SimpleType("array", true);
}
return new SimpleType($type, false);
}
public static function null(): SimpleType
{
return new SimpleType("null", true);
}
public static function void(): SimpleType
{
return new SimpleType("void", true);
}
public function isNull(): bool {
return $this->isBuiltin && $this->name === 'null';
}
public function toTypeCode(): string {
assert($this->isBuiltin);
switch (strtolower($this->name)) {
case "bool":
return "_IS_BOOL";
case "int":
return "IS_LONG";
case "float":
return "IS_DOUBLE";
case "string":
return "IS_STRING";
case "array":
return "IS_ARRAY";
case "object":
return "IS_OBJECT";
case "void":
return "IS_VOID";
case "callable":
return "IS_CALLABLE";
case "iterable":
return "IS_ITERABLE";
case "mixed":
return "IS_MIXED";
case "static":
return "IS_STATIC";
default:
throw new Exception("Not implemented: $this->name");
}
}
public function toTypeMask() {
assert($this->isBuiltin);
switch (strtolower($this->name)) {
case "null":
return "MAY_BE_NULL";
case "false":
return "MAY_BE_FALSE";
case "bool":
return "MAY_BE_BOOL";
case "int":
return "MAY_BE_LONG";
case "float":
return "MAY_BE_DOUBLE";
case "string":
return "MAY_BE_STRING";
case "array":
return "MAY_BE_ARRAY";
case "object":
return "MAY_BE_OBJECT";
case "callable":
return "MAY_BE_CALLABLE";
case "mixed":
return "MAY_BE_ANY";
case "static":
return "MAY_BE_STATIC";
default:
throw new Exception("Not implemented: $this->name");
}
}
public function toEscapedName(): string {
return str_replace('\\', '\\\\', $this->name);
}
public function equals(SimpleType $other) {
return $this->name === $other->name
&& $this->isBuiltin === $other->isBuiltin;
}
}
class Type {
/** @var SimpleType[] $types */
public $types;
public function __construct(array $types) {
$this->types = $types;
}
public static function fromNode(Node $node): Type {
if ($node instanceof Node\UnionType) {
return new Type(array_map(['SimpleType', 'fromNode'], $node->types));
}
if ($node instanceof Node\NullableType) {
return new Type([
SimpleType::fromNode($node->type),
SimpleType::null(),
]);
}
return new Type([SimpleType::fromNode($node)]);
}
public static function fromPhpDoc(string $phpDocType) {
$types = explode("|", $phpDocType);
$simpleTypes = [];
foreach ($types as $type) {
$simpleTypes[] = SimpleType::fromPhpDoc($type);
}
return new Type($simpleTypes);
}
public function isNullable(): bool {
foreach ($this->types as $type) {
if ($type->isNull()) {
return true;
}
}
return false;
}
public function getWithoutNull(): Type {
return new Type(array_filter($this->types, function(SimpleType $type) {
return !$type->isNull();
}));
}
public function tryToSimpleType(): ?SimpleType {
$withoutNull = $this->getWithoutNull();
if (count($withoutNull->types) === 1) {
return $withoutNull->types[0];
}
return null;
}
public function toArginfoType(): ?ArginfoType {
$classTypes = [];
$builtinTypes = [];
foreach ($this->types as $type) {
if ($type->isBuiltin) {
$builtinTypes[] = $type;
} else {
$classTypes[] = $type;
}
}
return new ArginfoType($classTypes, $builtinTypes);
}
public static function equals(?Type $a, ?Type $b): bool {
if ($a === null || $b === null) {
return $a === $b;
}
if (count($a->types) !== count($b->types)) {
return false;
}
for ($i = 0; $i < count($a->types); $i++) {
if (!$a->types[$i]->equals($b->types[$i])) {
return false;
}
}
return true;
}
public function __toString() {
if ($this->types === null) {
return 'mixed';
}
return implode('|', array_map(
function ($type) { return $type->name; },
$this->types)
);
}
}
class ArginfoType {
/** @var ClassType[] $classTypes */
public $classTypes;
/** @var SimpleType[] $builtinTypes */
private $builtinTypes;
public function __construct(array $classTypes, array $builtinTypes) {
$this->classTypes = $classTypes;
$this->builtinTypes = $builtinTypes;
}
public function hasClassType(): bool {
return !empty($this->classTypes);
}
public function toClassTypeString(): string {
return implode('|', array_map(function(SimpleType $type) {
return $type->toEscapedName();
}, $this->classTypes));
}
public function toTypeMask(): string {
if (empty($this->builtinTypes)) {
return '0';
}
return implode('|', array_map(function(SimpleType $type) {
return $type->toTypeMask();
}, $this->builtinTypes));
}
}
class ArgInfo {
const SEND_BY_VAL = 0;
const SEND_BY_REF = 1;
const SEND_PREFER_REF = 2;
/** @var string */
public $name;
/** @var int */
public $sendBy;
/** @var bool */
public $isVariadic;
/** @var Type|null */
public $type;
/** @var Type|null */
public $phpDocType;
/** @var string|null */
public $defaultValue;
public function __construct(string $name, int $sendBy, bool $isVariadic, ?Type $type, ?Type $phpDocType, ?string $defaultValue) {
$this->name = $name;
$this->sendBy = $sendBy;
$this->isVariadic = $isVariadic;
$this->type = $type;
$this->phpDocType = $phpDocType;
$this->defaultValue = $defaultValue;
}
public function equals(ArgInfo $other): bool {
return $this->name === $other->name
&& $this->sendBy === $other->sendBy
&& $this->isVariadic === $other->isVariadic
&& Type::equals($this->type, $other->type)
&& $this->defaultValue === $other->defaultValue;
}
public function getSendByString(): string {
switch ($this->sendBy) {
case self::SEND_BY_VAL:
return "0";
case self::SEND_BY_REF:
return "1";
case self::SEND_PREFER_REF:
return "ZEND_SEND_PREFER_REF";
}
throw new Exception("Invalid sendBy value");
}
public function getMethodSynopsisType(): Type {
if ($this->type) {
return $this->type;
}
if ($this->phpDocType) {
return $this->phpDocType;
}
throw new Exception("A parameter must have a type");
}
public function hasProperDefaultValue(): bool {
return $this->defaultValue !== null && $this->defaultValue !== "UNKNOWN";
}
public function getDefaultValueAsArginfoString(): string {
if ($this->hasProperDefaultValue()) {
return '"' . addslashes($this->defaultValue) . '"';
}
return "NULL";
}
public function getDefaultValueAsMethodSynopsisString(): ?string {
if ($this->defaultValue === null) {
return null;
}
switch ($this->defaultValue) {
case 'UNKNOWN':
return null;
case 'false':
case 'true':
case 'null':
return "&{$this->defaultValue};";
}
return $this->defaultValue;
}
}
interface FunctionOrMethodName {
public function getDeclaration(): string;
public function getArgInfoName(): string;
public function getMethodSynopsisFilename(): string;
public function __toString(): string;
public function isMethod(): bool;
public function isConstructor(): bool;
public function isDestructor(): bool;
}
class FunctionName implements FunctionOrMethodName {
/** @var Name */
private $name;
public function __construct(Name $name) {
$this->name = $name;
}
public function getNamespace(): ?string {
if ($this->name->isQualified()) {
return $this->name->slice(0, -1)->toString();
}
return null;
}
public function getNonNamespacedName(): string {
if ($this->name->isQualified()) {
throw new Exception("Namespaced name not supported here");
}
return $this->name->toString();
}
public function getDeclarationName(): string {
return $this->name->getLast();
}
public function getDeclaration(): string {
return "ZEND_FUNCTION({$this->getDeclarationName()});\n";
}
public function getArgInfoName(): string {
$underscoreName = implode('_', $this->name->parts);
return "arginfo_$underscoreName";
}
public function getMethodSynopsisFilename(): string {
return implode('_', $this->name->parts);
}
public function __toString(): string {
return $this->name->toString();
}
public function isMethod(): bool {
return false;
}
public function isConstructor(): bool {
return false;
}
public function isDestructor(): bool {
return false;
}
}
class MethodName implements FunctionOrMethodName {
/** @var Name */
private $className;
/** @var string */
public $methodName;
public function __construct(Name $className, string $methodName) {
$this->className = $className;
$this->methodName = $methodName;
}
public function getDeclarationClassName(): string {
return implode('_', $this->className->parts);
}
public function getDeclaration(): string {
return "ZEND_METHOD({$this->getDeclarationClassName()}, $this->methodName);\n";
}
public function getArgInfoName(): string {
return "arginfo_class_{$this->getDeclarationClassName()}_{$this->methodName}";
}
public function getMethodSynopsisFilename(): string {
return $this->getDeclarationClassName() . "_{$this->methodName}";
}
public function __toString(): string {
return "$this->className::$this->methodName";
}
public function isMethod(): bool {
return true;
}
public function isConstructor(): bool {
return $this->methodName === "__construct";
}
public function isDestructor(): bool {
return $this->methodName === "__destruct";
}
}
class ReturnInfo {
/** @var bool */
public $byRef;
/** @var Type|null */
public $type;
/** @var Type|null */
public $phpDocType;
public function __construct(bool $byRef, ?Type $type, ?Type $phpDocType) {
$this->byRef = $byRef;
$this->type = $type;
$this->phpDocType = $phpDocType;
}
public function equals(ReturnInfo $other): bool {
return $this->byRef === $other->byRef
&& Type::equals($this->type, $other->type);
}
public function getMethodSynopsisType(): ?Type {
return $this->type ?? $this->phpDocType;
}
}
class FuncInfo {
/** @var FunctionOrMethodName */
public $name;
/** @var int */
public $classFlags;
/** @var int */
public $flags;
/** @var string|null */
public $aliasType;
/** @var FunctionName|null */
public $alias;
/** @var bool */
public $isDeprecated;
/** @var bool */
public $verify;
/** @var ArgInfo[] */
public $args;
/** @var ReturnInfo */
public $return;
/** @var int */
public $numRequiredArgs;
/** @var string|null */
public $cond;
public function __construct(
FunctionOrMethodName $name,
int $classFlags,
int $flags,
?string $aliasType,
?FunctionOrMethodName $alias,
bool $isDeprecated,
bool $verify,
array $args,
ReturnInfo $return,
int $numRequiredArgs,
?string $cond
) {
$this->name = $name;
$this->classFlags = $classFlags;
$this->flags = $flags;
$this->aliasType = $aliasType;
$this->alias = $alias;
$this->isDeprecated = $isDeprecated;
$this->verify = $verify;
$this->args = $args;
$this->return = $return;
$this->numRequiredArgs = $numRequiredArgs;
$this->cond = $cond;
}
public function isMethod(): bool
{
return $this->name->isMethod();
}
public function isFinalMethod(): bool
{
return ($this->flags & Class_::MODIFIER_FINAL) || ($this->classFlags & Class_::MODIFIER_FINAL);
}
public function isInstanceMethod(): bool
{
return !($this->flags & Class_::MODIFIER_STATIC) && $this->isMethod() && !$this->name->isConstructor();
}
/** @return string[] */
public function getModifierNames(): array
{
if (!$this->isMethod()) {
return [];
}
$result = [];
if ($this->flags & Class_::MODIFIER_FINAL) {
$result[] = "final";
} elseif ($this->flags & Class_::MODIFIER_ABSTRACT && $this->classFlags & ~Class_::MODIFIER_ABSTRACT) {
$result[] = "abstract";
}
if ($this->flags & Class_::MODIFIER_PROTECTED) {
$result[] = "protected";
} elseif ($this->flags & Class_::MODIFIER_PRIVATE) {
$result[] = "private";
} else {
$result[] = "public";
}
if ($this->flags & Class_::MODIFIER_STATIC) {
$result[] = "static";
}
return $result;
}
public function hasParamWithUnknownDefaultValue(): bool
{
foreach ($this->args as $arg) {
if ($arg->defaultValue && !$arg->hasProperDefaultValue()) {
return true;
}
}
return false;
}
public function equalsApartFromName(FuncInfo $other): bool {
if (count($this->args) !== count($other->args)) {
return false;
}
for ($i = 0; $i < count($this->args); $i++) {
if (!$this->args[$i]->equals($other->args[$i])) {
return false;
}
}
return $this->return->equals($other->return)
&& $this->numRequiredArgs === $other->numRequiredArgs
&& $this->cond === $other->cond;
}
public function getArgInfoName(): string {
return $this->name->getArgInfoName();
}
public function getDeclarationKey(): string
{
$name = $this->alias ?? $this->name;
return "$name|$this->cond";
}
public function getDeclaration(): ?string
{
if ($this->flags & Class_::MODIFIER_ABSTRACT) {
return null;
}
$name = $this->alias ?? $this->name;
return $name->getDeclaration();
}
public function getFunctionEntry(): string {
if ($this->name instanceof MethodName) {
if ($this->alias) {
if ($this->alias instanceof MethodName) {
return sprintf(
"\tZEND_MALIAS(%s, %s, %s, %s, %s)\n",
$this->alias->getDeclarationClassName(), $this->name->methodName,
$this->alias->methodName, $this->getArgInfoName(), $this->getFlagsAsArginfoString()
);
} else if ($this->alias instanceof FunctionName) {
return sprintf(
"\tZEND_ME_MAPPING(%s, %s, %s, %s)\n",
$this->name->methodName, $this->alias->getNonNamespacedName(),
$this->getArgInfoName(), $this->getFlagsAsArginfoString()
);
} else {
throw new Error("Cannot happen");
}
} else {
$declarationClassName = $this->name->getDeclarationClassName();
if ($this->flags & Class_::MODIFIER_ABSTRACT) {
return sprintf(
"\tZEND_ABSTRACT_ME_WITH_FLAGS(%s, %s, %s, %s)\n",
$declarationClassName, $this->name->methodName, $this->getArgInfoName(),
$this->getFlagsAsArginfoString()
);
}
return sprintf(
"\tZEND_ME(%s, %s, %s, %s)\n",
$declarationClassName, $this->name->methodName, $this->getArgInfoName(),
$this->getFlagsAsArginfoString()
);
}
} else if ($this->name instanceof FunctionName) {
$namespace = $this->name->getNamespace();
$declarationName = $this->name->getDeclarationName();
if ($this->alias && $this->isDeprecated) {
return sprintf(
"\tZEND_DEP_FALIAS(%s, %s, %s)\n",
$declarationName, $this->alias->getNonNamespacedName(), $this->getArgInfoName()
);
}
if ($this->alias) {
return sprintf(
"\tZEND_FALIAS(%s, %s, %s)\n",
$declarationName, $this->alias->getNonNamespacedName(), $this->getArgInfoName()
);
}
if ($this->isDeprecated) {
return sprintf(
"\tZEND_DEP_FE(%s, %s)\n", $declarationName, $this->getArgInfoName());
}
if ($namespace) {
// Render A\B as "A\\B" in C strings for namespaces
return sprintf(
"\tZEND_NS_FE(\"%s\", %s, %s)\n",
addslashes($namespace), $declarationName, $this->getArgInfoName());
} else {
return sprintf("\tZEND_FE(%s, %s)\n", $declarationName, $this->getArgInfoName());
}
} else {
throw new Error("Cannot happen");
}
}
private function getFlagsAsArginfoString(): string
{
$flags = "ZEND_ACC_PUBLIC";
if ($this->flags & Class_::MODIFIER_PROTECTED) {
$flags = "ZEND_ACC_PROTECTED";
} elseif ($this->flags & Class_::MODIFIER_PRIVATE) {
$flags = "ZEND_ACC_PRIVATE";
}
if ($this->flags & Class_::MODIFIER_STATIC) {
$flags .= "|ZEND_ACC_STATIC";
}
if ($this->flags & Class_::MODIFIER_FINAL) {
$flags .= "|ZEND_ACC_FINAL";
}
if ($this->flags & Class_::MODIFIER_ABSTRACT) {
$flags .= "|ZEND_ACC_ABSTRACT";
}
if ($this->isDeprecated) {
$flags .= "|ZEND_ACC_DEPRECATED";
}
return $flags;
}
/**
* @param FuncInfo[] $funcMap
* @param FuncInfo[] $aliasMap
* @throws Exception
*/
public function getMethodSynopsisDocument(array $funcMap, array $aliasMap): ?string {
$doc = new DOMDocument();
$doc->formatOutput = true;
$methodSynopsis = $this->getMethodSynopsisElement($funcMap, $aliasMap, $doc);
if (!$methodSynopsis) {
return null;
}
$doc->appendChild($methodSynopsis);
return $doc->saveXML();
}
/**
* @param FuncInfo[] $funcMap
* @param FuncInfo[] $aliasMap
* @throws Exception
*/
public function getMethodSynopsisElement(array $funcMap, array $aliasMap, DOMDocument $doc): ?DOMElement {
if ($this->hasParamWithUnknownDefaultValue()) {
return null;
}
if ($this->name->isConstructor()) {
$synopsisType = "constructorsynopsis";
} elseif ($this->name->isDestructor()) {
$synopsisType = "destructorsynopsis";
} else {
$synopsisType = "methodsynopsis";
}
$methodSynopsis = $doc->createElement($synopsisType);
$aliasedFunc = $this->aliasType === "alias" && isset($funcMap[$this->alias->__toString()]) ? $funcMap[$this->alias->__toString()] : null;
$aliasFunc = $aliasMap[$this->name->__toString()] ?? null;
if (($this->aliasType === "alias" && $aliasedFunc !== null && $aliasedFunc->isMethod() !== $this->isMethod()) ||
($aliasFunc !== null && $aliasFunc->isMethod() !== $this->isMethod())
) {
$role = $doc->createAttribute("role");
$role->value = $this->isMethod() ? "oop" : "procedural";
$methodSynopsis->appendChild($role);
}
$methodSynopsis->appendChild(new DOMText("\n "));
foreach ($this->getModifierNames() as $modifierString) {
$modifierElement = $doc->createElement('modifier', $modifierString);
$methodSynopsis->appendChild($modifierElement);
$methodSynopsis->appendChild(new DOMText(" "));
}
$returnType = $this->return->getMethodSynopsisType();
if ($returnType) {
$this->appendMethodSynopsisTypeToElement($doc, $methodSynopsis, $returnType);
}
$methodname = $doc->createElement('methodname', $this->name->__toString());
$methodSynopsis->appendChild($methodname);
if (empty($this->args)) {
$methodSynopsis->appendChild(new DOMText("\n "));
$void = $doc->createElement('void');
$methodSynopsis->appendChild($void);
} else {
foreach ($this->args as $arg) {
$methodSynopsis->appendChild(new DOMText("\n "));
$methodparam = $doc->createElement('methodparam');
if ($arg->defaultValue !== null) {
$methodparam->setAttribute("choice", "opt");
}
if ($arg->isVariadic) {
$methodparam->setAttribute("rep", "repeat");
}
$methodSynopsis->appendChild($methodparam);
$this->appendMethodSynopsisTypeToElement($doc, $methodparam, $arg->getMethodSynopsisType());
$parameter = $doc->createElement('parameter', $arg->name);
if ($arg->sendBy !== ArgInfo::SEND_BY_VAL) {
$parameter->setAttribute("role", "reference");
}
$methodparam->appendChild($parameter);
$defaultValue = $arg->getDefaultValueAsMethodSynopsisString();
if ($defaultValue !== null) {
$initializer = $doc->createElement('initializer');
if (preg_match('/^[a-zA-Z_][a-zA-Z_0-9]*$/', $defaultValue)) {
$constant = $doc->createElement('constant', $defaultValue);
$initializer->appendChild($constant);
} else {
$initializer->nodeValue = $defaultValue;
}
$methodparam->appendChild($initializer);
}
}
}
$methodSynopsis->appendChild(new DOMText("\n "));
return $methodSynopsis;
}
public function discardInfoForOldPhpVersions(): void {
$this->return->type = null;
foreach ($this->args as $arg) {
$arg->type = null;
$arg->defaultValue = null;
}
}
private function appendMethodSynopsisTypeToElement(DOMDocument $doc, DOMElement $elementToAppend, Type $type) {
if (count($type->types) > 1) {
$typeElement = $doc->createElement('type');
$typeElement->setAttribute("class", "union");
foreach ($type->types as $type) {
$unionTypeElement = $doc->createElement('type', $type->name);
$typeElement->appendChild($unionTypeElement);
}
} else {
$typeElement = $doc->createElement('type', $type->types[0]->name);
}
$elementToAppend->appendChild($typeElement);
}
}
class ClassInfo {
/** @var Name */
public $name;
/** @var FuncInfo[] */
public $funcInfos;
public function __construct(Name $name, array $funcInfos) {
$this->name = $name;
$this->funcInfos = $funcInfos;
}
}
class FileInfo {
/** @var FuncInfo[] */
public $funcInfos = [];
/** @var ClassInfo[] */
public $classInfos = [];
/** @var bool */
public $generateFunctionEntries = false;
/** @var string */
public $declarationPrefix = "";
/** @var bool */
public $generateLegacyArginfo = false;
/**
* @return iterable