summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNikita Popov <nikita.ppv@gmail.com>2019-11-08 15:38:00 +0100
committerNikita Popov <nikita.ppv@gmail.com>2019-11-08 17:32:18 +0100
commit9bbbc9e7e9ee1e3941065b32daccee389d61e785 (patch)
treeb147b80400f44f4a25224851a95dba330b517b73
parent70b4bc963c559aba8d036a47804c8b21ca41a668 (diff)
downloadphp-git-9bbbc9e7e9ee1e3941065b32daccee389d61e785.tar.gz
Add support for union types in stubs
This is the MVP for supporting union types in PHP stubs. Return types with only builtin types work, which is the part we mainly need. Closes GH-4895.
-rw-r--r--Zend/zend_API.h4
-rwxr-xr-xext/standard/basic_functions.stub.php33
-rwxr-xr-xext/standard/basic_functions_arginfo.h32
-rwxr-xr-xscripts/dev/gen_stub.php185
4 files changed, 180 insertions, 74 deletions
diff --git a/Zend/zend_API.h b/Zend/zend_API.h
index ba6b4ed439..85c2d495a6 100644
--- a/Zend/zend_API.h
+++ b/Zend/zend_API.h
@@ -124,6 +124,10 @@ typedef struct _zend_fcall_info_cache {
#define ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO(name, class_name, allow_null) \
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(name, 0, -1, class_name, allow_null)
+#define ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(name, return_reference, required_num_args, type) \
+ static const zend_internal_arg_info name[] = { \
+ { (const char*)(zend_uintptr_t)(required_num_args), ZEND_TYPE_INIT_MASK(type | _ZEND_ARG_INFO_FLAGS(return_reference, 0)) },
+
#define ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(name, return_reference, required_num_args, type, allow_null) \
static const zend_internal_arg_info name[] = { \
{ (const char*)(zend_uintptr_t)(required_num_args), ZEND_TYPE_INIT_CODE(type, allow_null, _ZEND_ARG_INFO_FLAGS(return_reference, 0)) },
diff --git a/ext/standard/basic_functions.stub.php b/ext/standard/basic_functions.stub.php
index bb72ed2ff9..d693b87b90 100755
--- a/ext/standard/basic_functions.stub.php
+++ b/ext/standard/basic_functions.stub.php
@@ -22,19 +22,15 @@ function ob_end_flush(): bool {}
function ob_end_clean(): bool {}
-/** @return string|false */
-function ob_get_flush() {}
+function ob_get_flush(): string|false {}
-/** @return string|false */
-function ob_get_clean() {}
+function ob_get_clean(): string|false {}
-/** @return string|false */
-function ob_get_contents() {}
+function ob_get_contents(): string|false {}
function ob_get_level(): int {}
-/** @return int|false */
-function ob_get_length() {}
+function ob_get_length(): int|false {}
function ob_list_handlers(): array {}
@@ -649,26 +645,19 @@ function dirname(string $path, int $levels = 1): string {}
/** @return array|string */
function pathinfo(string $path, int $options = UNKNOWN) {}
-/** @return string|false */
-function stristr(string $haystack, string $needle, bool $before_needle = false) {}
+function stristr(string $haystack, string $needle, bool $before_needle = false): string|false {}
-/** @return string|false */
-function strstr(string $haystack, string $needle, bool $before_needle = false) {}
+function strstr(string $haystack, string $needle, bool $before_needle = false): string|false {}
-/** @return int|false */
-function strpos(string $haystack, string $needle, int $offset = 0) {}
+function strpos(string $haystack, string $needle, int $offset = 0): int|false {}
-/** @return int|false */
-function stripos(string $haystack, string $needle, int $offset = 0) {}
+function stripos(string $haystack, string $needle, int $offset = 0): int|false {}
-/** @return int|false */
-function strrpos(string $haystack, string $needle, int $offset = 0) {}
+function strrpos(string $haystack, string $needle, int $offset = 0): int|false {}
-/** @return int|false */
-function strripos(string $haystack, string $needle, int $offset = 0) {}
+function strripos(string $haystack, string $needle, int $offset = 0): int|false {}
-/** @return string|false */
-function strrchr(string $haystack, string $needle) {}
+function strrchr(string $haystack, string $needle): string|false {}
function chunk_split(string $str, int $chunklen = 76, string $ending = "\r\n"): string {}
diff --git a/ext/standard/basic_functions_arginfo.h b/ext/standard/basic_functions_arginfo.h
index 4e0b672ed0..2a54583871 100755
--- a/ext/standard/basic_functions_arginfo.h
+++ b/ext/standard/basic_functions_arginfo.h
@@ -23,7 +23,7 @@ ZEND_END_ARG_INFO()
#define arginfo_ob_end_clean arginfo_ob_flush
-ZEND_BEGIN_ARG_INFO_EX(arginfo_ob_get_flush, 0, 0, 0)
+ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_ob_get_flush, 0, 0, MAY_BE_STRING|MAY_BE_FALSE)
ZEND_END_ARG_INFO()
#define arginfo_ob_get_clean arginfo_ob_get_flush
@@ -33,7 +33,8 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_ob_get_level, 0, 0, IS_LONG, 0)
ZEND_END_ARG_INFO()
-#define arginfo_ob_get_length arginfo_ob_get_flush
+ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_ob_get_length, 0, 0, MAY_BE_LONG|MAY_BE_FALSE)
+ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_ob_list_handlers, 0, 0, IS_ARRAY, 0)
ZEND_END_ARG_INFO()
@@ -495,7 +496,8 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_set_include_path, 0, 0, 1)
ZEND_ARG_TYPE_INFO(0, include_path, IS_STRING, 0)
ZEND_END_ARG_INFO()
-#define arginfo_get_include_path arginfo_ob_get_flush
+ZEND_BEGIN_ARG_INFO_EX(arginfo_get_include_path, 0, 0, 0)
+ZEND_END_ARG_INFO()
#define arginfo_restore_include_path arginfo_flush
@@ -648,7 +650,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_dns_get_mx, 0, 2, _IS_BOOL, 0)
ZEND_END_ARG_INFO()
#endif
-#define arginfo_net_get_interfaces arginfo_ob_get_flush
+#define arginfo_net_get_interfaces arginfo_get_include_path
#if HAVE_FTOK
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_ftok, 0, 2, IS_LONG, 0)
@@ -674,13 +676,13 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_md5_file, 0, 0, 1)
ZEND_ARG_TYPE_INFO(0, raw_output, _IS_BOOL, 0)
ZEND_END_ARG_INFO()
-#define arginfo_getmyuid arginfo_ob_get_flush
+#define arginfo_getmyuid arginfo_get_include_path
-#define arginfo_getmygid arginfo_ob_get_flush
+#define arginfo_getmygid arginfo_get_include_path
-#define arginfo_getmypid arginfo_ob_get_flush
+#define arginfo_getmypid arginfo_get_include_path
-#define arginfo_getmyinode arginfo_ob_get_flush
+#define arginfo_getmyinode arginfo_get_include_path
#define arginfo_getlastmod arginfo_ob_get_level
@@ -873,7 +875,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_pathinfo, 0, 0, 1)
ZEND_ARG_TYPE_INFO(0, options, IS_LONG, 0)
ZEND_END_ARG_INFO()
-ZEND_BEGIN_ARG_INFO_EX(arginfo_stristr, 0, 0, 2)
+ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_stristr, 0, 2, MAY_BE_STRING|MAY_BE_FALSE)
ZEND_ARG_TYPE_INFO(0, haystack, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, needle, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, before_needle, _IS_BOOL, 0)
@@ -881,7 +883,7 @@ ZEND_END_ARG_INFO()
#define arginfo_strstr arginfo_stristr
-ZEND_BEGIN_ARG_INFO_EX(arginfo_strpos, 0, 0, 2)
+ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_strpos, 0, 2, MAY_BE_LONG|MAY_BE_FALSE)
ZEND_ARG_TYPE_INFO(0, haystack, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, needle, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, offset, IS_LONG, 0)
@@ -893,7 +895,7 @@ ZEND_END_ARG_INFO()
#define arginfo_strripos arginfo_strpos
-ZEND_BEGIN_ARG_INFO_EX(arginfo_strrchr, 0, 0, 2)
+ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_strrchr, 0, 2, MAY_BE_STRING|MAY_BE_FALSE)
ZEND_ARG_TYPE_INFO(0, haystack, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, needle, IS_STRING, 0)
ZEND_END_ARG_INFO()
@@ -1112,7 +1114,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_chroot, 0, 1, _IS_BOOL, 0)
ZEND_END_ARG_INFO()
#endif
-#define arginfo_getcwd arginfo_ob_get_flush
+#define arginfo_getcwd arginfo_get_include_path
#define arginfo_rewinddir arginfo_closedir
@@ -1239,15 +1241,15 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_phpcredits, 0, 0, _IS_BOOL, 0)
ZEND_ARG_TYPE_INFO(0, flag, IS_LONG, 0)
ZEND_END_ARG_INFO()
-#define arginfo_php_sapi_name arginfo_ob_get_flush
+#define arginfo_php_sapi_name arginfo_get_include_path
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_php_uname, 0, 0, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, mode, IS_STRING, 0)
ZEND_END_ARG_INFO()
-#define arginfo_php_ini_scanned_files arginfo_ob_get_flush
+#define arginfo_php_ini_scanned_files arginfo_get_include_path
-#define arginfo_php_ini_loaded_file arginfo_ob_get_flush
+#define arginfo_php_ini_loaded_file arginfo_get_include_path
ZEND_BEGIN_ARG_INFO_EX(arginfo_iptcembed, 0, 0, 2)
ZEND_ARG_TYPE_INFO(0, iptcdata, IS_STRING, 0)
diff --git a/scripts/dev/gen_stub.php b/scripts/dev/gen_stub.php
index 4bf29fc440..2aed71af61 100755
--- a/scripts/dev/gen_stub.php
+++ b/scripts/dev/gen_stub.php
@@ -55,35 +55,32 @@ function processStubFile(string $stubFile) {
}
}
-class Type {
+class SimpleType {
/** @var string */
public $name;
/** @var bool */
public $isBuiltin;
- /** @var bool */
- public $isNullable;
- public function __construct(string $name, bool $isBuiltin, bool $isNullable = false) {
+ public function __construct(string $name, bool $isBuiltin) {
$this->name = $name;
$this->isBuiltin = $isBuiltin;
- $this->isNullable = $isNullable;
}
public static function fromNode(Node $node) {
- if ($node instanceof Node\NullableType) {
- $type = self::fromNode($node->type);
- return new Type($type->name, $type->isBuiltin, true);
- }
if ($node instanceof Node\Name) {
assert($node->isFullyQualified());
- return new Type($node->toString(), false);
+ return new SimpleType($node->toString(), false);
}
if ($node instanceof Node\Identifier) {
- return new Type($node->toString(), true);
+ return new SimpleType($node->toString(), true);
}
throw new Exception("Unexpected node type");
}
+ public function isNull() {
+ return $this->isBuiltin && $this->name === 'null';
+ }
+
public function toTypeCode() {
assert($this->isBuiltin);
switch (strtolower($this->name)) {
@@ -108,14 +105,111 @@ class Type {
}
}
+ public function toTypeMask() {
+ assert($this->isBuiltin);
+ switch (strtolower($this->name)) {
+ 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";
+ default:
+ throw new Exception("Not implemented: $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) {
+ 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),
+ new SimpleType('null', true),
+ ]);
+ }
+ return new Type([SimpleType::fromNode($node)]);
+ }
+
+ public function isNullable(): bool {
+ foreach ($this->types as $type) {
+ if ($type->isNull()) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public function isBuiltinOnly(): bool {
+ foreach ($this->types as $type) {
+ if (!$type->isBuiltin) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ 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 toTypeMask(): string {
+ return implode('|', array_map(function(SimpleType $type) {
+ return $type->toTypeMask();
+ }, $this->types));
+ }
+
public static function equals(?Type $a, ?Type $b): bool {
if ($a === null || $b === null) {
return $a === $b;
}
- return $a->name === $b->name
- && $a->isBuiltin === $b->isBuiltin
- && $a->isNullable === $b->isNullable;
+ 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;
}
}
@@ -369,20 +463,31 @@ function parseStubFile(string $fileName) {
function funcInfoToCode(FuncInfo $funcInfo): string {
$code = '';
- if ($funcInfo->return->type) {
- $returnType = $funcInfo->return->type;
- if ($returnType->isBuiltin) {
+ $returnType = $funcInfo->return->type;
+ if ($returnType !== null) {
+ $simpleReturnType = $returnType->tryToSimpleType();
+ if ($simpleReturnType !== null) {
+ if ($simpleReturnType->isBuiltin) {
+ $code .= sprintf(
+ "ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_%s, %d, %d, %s, %d)\n",
+ $funcInfo->name, $funcInfo->return->byRef, $funcInfo->numRequiredArgs,
+ $simpleReturnType->toTypeCode(), $returnType->isNullable()
+ );
+ } else {
+ $code .= sprintf(
+ "ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_%s, %d, %d, %s, %d)\n",
+ $funcInfo->name, $funcInfo->return->byRef, $funcInfo->numRequiredArgs,
+ str_replace('\\', '\\\\', $simpleReturnType->name), $returnType->isNullable()
+ );
+ }
+ } else if ($returnType->isBuiltinOnly()) {
$code .= sprintf(
- "ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_%s, %d, %d, %s, %d)\n",
+ "ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_%s, %d, %d, %s)\n",
$funcInfo->name, $funcInfo->return->byRef, $funcInfo->numRequiredArgs,
- $returnType->toTypeCode(), $returnType->isNullable
+ $returnType->toTypeMask()
);
} else {
- $code .= sprintf(
- "ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_%s, %d, %d, %s, %d)\n",
- $funcInfo->name, $funcInfo->return->byRef, $funcInfo->numRequiredArgs,
- str_replace('\\', '\\\\', $returnType->name), $returnType->isNullable
- );
+ throw new Exception('Unimplemented');
}
} else {
$code .= sprintf(
@@ -393,19 +498,25 @@ function funcInfoToCode(FuncInfo $funcInfo): string {
foreach ($funcInfo->args as $argInfo) {
$argKind = $argInfo->isVariadic ? "ARG_VARIADIC" : "ARG";
- if ($argInfo->type) {
- if ($argInfo->type->isBuiltin) {
- $code .= sprintf(
- "\tZEND_%s_TYPE_INFO(%s, %s, %s, %d)\n",
- $argKind, $argInfo->getSendByString(), $argInfo->name,
- $argInfo->type->toTypeCode(), $argInfo->type->isNullable
- );
+ $argType = $argInfo->type;
+ if ($argType !== null) {
+ $simpleArgType = $argType->tryToSimpleType();
+ if ($simpleArgType !== null) {
+ if ($simpleArgType->isBuiltin) {
+ $code .= sprintf(
+ "\tZEND_%s_TYPE_INFO(%s, %s, %s, %d)\n",
+ $argKind, $argInfo->getSendByString(), $argInfo->name,
+ $simpleArgType->toTypeCode(), $argType->isNullable()
+ );
+ } else {
+ $code .= sprintf(
+ "\tZEND_%s_OBJ_INFO(%s, %s, %s, %d)\n",
+ $argKind, $argInfo->getSendByString(), $argInfo->name,
+ str_replace('\\', '\\\\', $simpleArgType->name), $argType->isNullable()
+ );
+ }
} else {
- $code .= sprintf(
- "\tZEND_%s_OBJ_INFO(%s, %s, %s, %d)\n",
- $argKind, $argInfo->getSendByString(), $argInfo->name,
- str_replace('\\', '\\\\', $argInfo->type->name), $argInfo->type->isNullable
- );
+ throw new Exception('Unimplemented');
}
} else {
$code .= sprintf(
@@ -456,7 +567,7 @@ function generateArginfoCode(array $funcInfos): string {
}
function initPhpParser() {
- $version = "4.2.2";
+ $version = "4.3.0";
$phpParserDir = __DIR__ . "/PHP-Parser-$version";
if (!is_dir($phpParserDir)) {
$cwd = getcwd();