diff --git a/Zend/zend_exceptions.c b/Zend/zend_exceptions.c index e93526689d15a..0026860ef8ca6 100644 --- a/Zend/zend_exceptions.c +++ b/Zend/zend_exceptions.c @@ -53,14 +53,21 @@ static zend_object_handlers default_exception_handlers; /* {{{ zend_implement_throwable */ static int zend_implement_throwable(zend_class_entry *interface, zend_class_entry *class_type) { - if (instanceof_function(class_type, zend_ce_exception) || instanceof_function(class_type, zend_ce_error)) { + /* zend_ce_exception and zend_ce_error may not be initialized yet when this is caleld (e.g when + * implementing Throwable for Exception itself). Perform a manual inheritance check. */ + zend_class_entry *root = class_type; + while (root->parent) { + root = root->parent; + } + if (zend_string_equals_literal(root->name, "Exception") + || zend_string_equals_literal(root->name, "Error")) { return SUCCESS; } - zend_error_noreturn(E_ERROR, "Class %s cannot implement interface %s, extend %s or %s instead", + + zend_error_noreturn(E_ERROR, + "Class %s cannot implement interface %s, extend Exception or Error instead", ZSTR_VAL(class_type->name), - ZSTR_VAL(interface->name), - ZSTR_VAL(zend_ce_exception->name), - ZSTR_VAL(zend_ce_error->name)); + ZSTR_VAL(interface->name)); return FAILURE; } /* }}} */ @@ -740,87 +747,50 @@ ZEND_METHOD(Exception, __toString) } /* }}} */ -static void declare_exception_properties(zend_class_entry *ce) -{ - zval val; - - zend_declare_property_string(ce, "message", sizeof("message")-1, "", ZEND_ACC_PROTECTED); - zend_declare_property_string(ce, "string", sizeof("string")-1, "", ZEND_ACC_PRIVATE); - zend_declare_property_long(ce, "code", sizeof("code")-1, 0, ZEND_ACC_PROTECTED); - zend_declare_property_null(ce, "file", sizeof("file")-1, ZEND_ACC_PROTECTED); - zend_declare_property_null(ce, "line", sizeof("line")-1, ZEND_ACC_PROTECTED); - - ZVAL_EMPTY_ARRAY(&val); - zend_declare_typed_property( - ce, ZSTR_KNOWN(ZEND_STR_TRACE), &val, ZEND_ACC_PRIVATE, NULL, - (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_ARRAY)); - - ZVAL_NULL(&val); - zend_declare_typed_property( - ce, ZSTR_KNOWN(ZEND_STR_PREVIOUS), &val, ZEND_ACC_PRIVATE, NULL, - (zend_type) ZEND_TYPE_INIT_CE(zend_ce_throwable, /* allow_null */ 1, 0)); -} - void zend_register_default_exception(void) /* {{{ */ { - zend_class_entry ce; - - REGISTER_MAGIC_INTERFACE(throwable, Throwable); - zend_class_implements(zend_ce_throwable, 1, zend_ce_stringable); + zend_ce_throwable = register_class_Throwable(zend_ce_stringable); + zend_ce_throwable->interface_gets_implemented = zend_implement_throwable; memcpy(&default_exception_handlers, &std_object_handlers, sizeof(zend_object_handlers)); default_exception_handlers.clone_obj = NULL; - INIT_CLASS_ENTRY(ce, "Exception", class_Exception_methods); - zend_ce_exception = zend_register_internal_class_ex(&ce, NULL); + zend_ce_exception = register_class_Exception(zend_ce_throwable); zend_ce_exception->create_object = zend_default_exception_new; - zend_class_implements(zend_ce_exception, 1, zend_ce_throwable); - declare_exception_properties(zend_ce_exception); - INIT_CLASS_ENTRY(ce, "ErrorException", class_ErrorException_methods); - zend_ce_error_exception = zend_register_internal_class_ex(&ce, zend_ce_exception); + zend_ce_error_exception = register_class_ErrorException(zend_ce_exception); zend_ce_error_exception->create_object = zend_error_exception_new; + /* Declared manually because it uses constant E_ERROR. */ zend_declare_property_long(zend_ce_error_exception, "severity", sizeof("severity")-1, E_ERROR, ZEND_ACC_PROTECTED); - INIT_CLASS_ENTRY(ce, "Error", class_Error_methods); - zend_ce_error = zend_register_internal_class_ex(&ce, NULL); + zend_ce_error = register_class_Error(zend_ce_throwable); zend_ce_error->create_object = zend_default_exception_new; - zend_class_implements(zend_ce_error, 1, zend_ce_throwable); - declare_exception_properties(zend_ce_error); - INIT_CLASS_ENTRY(ce, "CompileError", class_CompileError_methods); - zend_ce_compile_error = zend_register_internal_class_ex(&ce, zend_ce_error); + zend_ce_compile_error = register_class_CompileError(zend_ce_error); zend_ce_compile_error->create_object = zend_default_exception_new; - INIT_CLASS_ENTRY(ce, "ParseError", class_ParseError_methods); - zend_ce_parse_error = zend_register_internal_class_ex(&ce, zend_ce_compile_error); + zend_ce_parse_error = register_class_ParseError(zend_ce_compile_error); zend_ce_parse_error->create_object = zend_default_exception_new; - INIT_CLASS_ENTRY(ce, "TypeError", class_TypeError_methods); - zend_ce_type_error = zend_register_internal_class_ex(&ce, zend_ce_error); + zend_ce_type_error = register_class_TypeError(zend_ce_error); zend_ce_type_error->create_object = zend_default_exception_new; - INIT_CLASS_ENTRY(ce, "ArgumentCountError", class_ArgumentCountError_methods); - zend_ce_argument_count_error = zend_register_internal_class_ex(&ce, zend_ce_type_error); + zend_ce_argument_count_error = register_class_ArgumentCountError(zend_ce_type_error); zend_ce_argument_count_error->create_object = zend_default_exception_new; - INIT_CLASS_ENTRY(ce, "ValueError", class_ValueError_methods); - zend_ce_value_error = zend_register_internal_class_ex(&ce, zend_ce_error); + zend_ce_value_error = register_class_ValueError(zend_ce_error); zend_ce_value_error->create_object = zend_default_exception_new; - INIT_CLASS_ENTRY(ce, "ArithmeticError", class_ArithmeticError_methods); - zend_ce_arithmetic_error = zend_register_internal_class_ex(&ce, zend_ce_error); + zend_ce_arithmetic_error = register_class_ArithmeticError(zend_ce_error); zend_ce_arithmetic_error->create_object = zend_default_exception_new; - INIT_CLASS_ENTRY(ce, "DivisionByZeroError", class_DivisionByZeroError_methods); - zend_ce_division_by_zero_error = zend_register_internal_class_ex(&ce, zend_ce_arithmetic_error); + zend_ce_division_by_zero_error = register_class_DivisionByZeroError(zend_ce_arithmetic_error); zend_ce_division_by_zero_error->create_object = zend_default_exception_new; - INIT_CLASS_ENTRY(zend_ce_unwind_exit, "UnwindExit", NULL); - - INIT_CLASS_ENTRY(ce, "UnhandledMatchError", NULL); - zend_ce_unhandled_match_error = zend_register_internal_class_ex(&ce, zend_ce_error); + zend_ce_unhandled_match_error = register_class_UnhandledMatchError(zend_ce_error); zend_ce_unhandled_match_error->create_object = zend_default_exception_new; + + INIT_CLASS_ENTRY(zend_ce_unwind_exit, "UnwindExit", NULL); } /* }}} */ diff --git a/Zend/zend_exceptions.stub.php b/Zend/zend_exceptions.stub.php index 11c1d500d3075..89dff788c09c6 100644 --- a/Zend/zend_exceptions.stub.php +++ b/Zend/zend_exceptions.stub.php @@ -1,6 +1,9 @@ ce_flags |= ZEND_ACC_FINAL | ZEND_ACC_NO_DYNAMIC_PROPERTIES; + zend_ce_generator = register_class_Generator(zend_ce_iterator); zend_ce_generator->create_object = zend_generator_create; zend_ce_generator->serialize = zend_class_serialize_deny; zend_ce_generator->unserialize = zend_class_unserialize_deny; - /* get_iterator has to be assigned *after* implementing the interface */ - zend_class_implements(zend_ce_generator, 1, zend_ce_iterator); zend_ce_generator->get_iterator = zend_generator_get_iterator; memcpy(&zend_generator_handlers, &std_object_handlers, sizeof(zend_object_handlers)); @@ -1132,7 +1126,6 @@ void zend_register_generator_ce(void) /* {{{ */ zend_generator_handlers.clone_obj = NULL; zend_generator_handlers.get_constructor = zend_generator_get_constructor; - INIT_CLASS_ENTRY(ce, "ClosedGeneratorException", NULL); - zend_ce_ClosedGeneratorException = zend_register_internal_class_ex(&ce, zend_ce_exception); + zend_ce_ClosedGeneratorException = register_class_ClosedGeneratorException(zend_ce_exception); } /* }}} */ diff --git a/Zend/zend_generators.stub.php b/Zend/zend_generators.stub.php index f2e47616bad1d..879fdb3642733 100644 --- a/Zend/zend_generators.stub.php +++ b/Zend/zend_generators.stub.php @@ -1,7 +1,11 @@ ce_flags |= ZEND_ACC_FINAL|ZEND_ACC_NO_DYNAMIC_PROPERTIES; + zend_class_implements(class_entry, 1, class_entry_Iterator); + + return class_entry; +} + +zend_class_entry *register_class_ClosedGeneratorException(zend_class_entry *class_entry_Exception) +{ + zend_class_entry ce, *class_entry; + + INIT_CLASS_ENTRY(ce, "ClosedGeneratorException", class_ClosedGeneratorException_methods); + class_entry = zend_register_internal_class_ex(&ce, class_entry_Exception); + + return class_entry; +} + diff --git a/build/gen_stub.php b/build/gen_stub.php index 319446ef41183..8653b6f3c5662 100755 --- a/build/gen_stub.php +++ b/build/gen_stub.php @@ -2,11 +2,13 @@ forceRegeneration || $stubHash !== $oldStubHash) && file_put_contents($arginfoFile, $arginfoCode)) { echo "Saved $arginfoFile\n"; @@ -63,11 +66,15 @@ function processStubFile(string $stubFile, Context $context): ?FileInfo { foreach ($fileInfo->getAllFuncInfos() as $funcInfo) { $funcInfo->discardInfoForOldPhpVersions(); } + foreach ($fileInfo->getAllPropertyInfos() as $propertyInfo) { + $propertyInfo->discardInfoForOldPhpVersions(); + } + $arginfoCode = generateArgInfoCode($fileInfo, $stubHash); if (($context->forceRegeneration || $stubHash !== $oldStubHash) && file_put_contents($legacyFile, $arginfoCode)) { echo "Saved $legacyFile\n"; } - } + } return $fileInfo; } catch (Exception $e) { @@ -293,7 +300,7 @@ public function tryToSimpleType(): ?SimpleType { return null; } - public function toArginfoType(): ?ArginfoType { + public function toArginfoType(): ArginfoType { $classTypes = []; $builtinTypes = []; foreach ($this->types as $type) { @@ -457,6 +464,24 @@ public function getDefaultValueAsMethodSynopsisString(): ?string { } } +class PropertyName { + /** @var Name */ + public $class; + /** @var string */ + public $property; + + public function __construct(Name $class, string $property) + { + $this->class = $class; + $this->property = $property; + } + + public function __toString() + { + return $this->class->toString() . "::$" . $this->property; + } +} + interface FunctionOrMethodName { public function getDeclaration(): string; public function getArgInfoName(): string; @@ -804,6 +829,14 @@ public function getFunctionEntry(): string { } } + public function discardInfoForOldPhpVersions(): void { + $this->return->type = null; + foreach ($this->args as $arg) { + $arg->type = null; + $arg->defaultValue = null; + } + } + private function getFlagsAsArginfoString(): string { $flags = "ZEND_ACC_PUBLIC"; @@ -940,14 +973,6 @@ public function getMethodSynopsisElement(array $funcMap, array $aliasMap, DOMDoc 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'); @@ -965,16 +990,306 @@ private function appendMethodSynopsisTypeToElement(DOMDocument $doc, DOMElement } } +class PropertyInfo +{ + /** @var PropertyName */ + public $name; + /** @var int */ + public $flags; + /** @var Type|null */ + public $type; + /** @var Expr|null */ + public $defaultValue; + + public function __construct(PropertyName $name, int $flags, ?Type $type, ?Expr $defaultValue) + { + $this->name = $name; + $this->flags = $flags; + $this->type = $type; + $this->defaultValue = $defaultValue; + } + + public function discardInfoForOldPhpVersions(): void { + $this->type = null; + } + + public function getDeclaration(): string { + $code = "\n"; + + $propertyName = $this->name->property; + + $defaultValueConstant = false; + if ($this->defaultValue === null) { + $defaultValue = null; + $defaultValueType = "undefined"; + } else { + $evaluator = new ConstExprEvaluator( + function (Expr $expr) use (&$defaultValueConstant) { + if ($expr instanceof Expr\ConstFetch) { + $defaultValueConstant = true; + return null; + } + + throw new Exception("Property $this->name has an unsupported default value"); + } + ); + $defaultValue = $evaluator->evaluateDirectly($this->defaultValue); + $defaultValueType = gettype($defaultValue); + } + + if ($defaultValueConstant) { + echo "Skipping code generation for property $this->name, because it has a constant default value\n"; + return ""; + } + + $typeCode = ""; + if ($this->type) { + $arginfoType = $this->type->toArginfoType(); + if ($arginfoType->hasClassType()) { + $simpleType = $this->type->tryToSimpleType(); + + $className = $arginfoType->classTypes[0]->name; + $code .= " zend_string *property_{$propertyName}_class_{$className} = zend_string_init(\"$className\", sizeof(\"$className\")-1, 1);\n"; + if ($simpleType) { + $typeCode = "(zend_type) ZEND_TYPE_INIT_CLASS(property_{$propertyName}_class_{$className}, " . ((int) $this->type->isNullable()) . ", 0)"; + } elseif (count($arginfoType->classTypes) === 1) { + $typeCode = "(zend_type) ZEND_TYPE_INIT_CLASS(property_{$propertyName}_class_{$className}, 0, " . $arginfoType->toTypeMask() . ")"; + } else { + throw new Exception("Property $this->name has an unsupported union type"); + } + } else { + $typeCode = "(zend_type) ZEND_TYPE_INIT_MASK(" . $arginfoType->toTypeMask() . ")"; + } + } + + $code .= $this->initializeValue($defaultValueType, $defaultValue, $this->type !== null); + + $code .= "\tzend_string *property_{$propertyName}_name = zend_string_init(\"$propertyName\", sizeof(\"$propertyName\") - 1, 1);\n"; + $nameCode = "property_{$propertyName}_name"; + + if ($this->type !== null) { + $code .= "\tzend_declare_typed_property(class_entry, $nameCode, &property_{$propertyName}_default_value, " . $this->getFlagsAsString() . ", NULL, $typeCode);\n"; + } else { + $code .= "\tzend_declare_property_ex(class_entry, $nameCode, &property_{$propertyName}_default_value, " . $this->getFlagsAsString() . ", NULL);\n"; + } + $code .= "\tzend_string_release(property_{$propertyName}_name);\n"; + + return $code; + } + + /** + * @param mixed $value + */ + private function initializeValue(string $type, $value, bool $isTyped): string + { + $name = $this->name->property; + $zvalName = "property_{$name}_default_value"; + + $code = "\tzval $zvalName;\n"; + + switch ($type) { + case "undefined": + if ($isTyped) { + $code .= "\tZVAL_UNDEF(&$zvalName);\n"; + } else { + $code .= "\tZVAL_NULL(&$zvalName);\n"; + } + break; + + case "NULL": + $code .= "\tZVAL_NULL(&$zvalName);\n"; + break; + + case "boolean": + $code .= "\tZVAL_BOOL(&$zvalName, " . ((int) $value) . ");\n"; + break; + + case "integer": + $code .= "\tZVAL_LONG(&$zvalName, $value);\n"; + break; + + case "double": + $code .= "\tZVAL_DOUBLE(&$zvalName, $value);\n"; + break; + + case "string": + if (empty($value)) { + $code .= "\tZVAL_EMPTY_STRING(&$zvalName);\n"; + } else { + $code .= "\tZVAL_STRING(&$zvalName, \"$value\");\n"; + } + break; + + case "array": + if (empty($value)) { + $code .= "\tZVAL_EMPTY_ARRAY(&$zvalName);\n"; + } else { + throw new Exception("Unimplemented property default value"); + } + break; + + default: + throw new Exception("Invalid property default value"); + } + + return $code; + } + + private function getFlagsAsString(): 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"; + } + + return $flags; + } +} + class ClassInfo { /** @var Name */ public $name; + /** @var int */ + public $flags; + /** @var string */ + public $type; + /** @var string|null */ + public $alias; + /** @var bool */ + public $isDeprecated; + /** @var bool */ + public $isStrictProperties; + /** @var Name[] */ + public $extends; + /** @var Name[] */ + public $implements; + /** @var PropertyInfo[] */ + public $propertyInfos; /** @var FuncInfo[] */ public $funcInfos; - public function __construct(Name $name, array $funcInfos) { + /** + * @param Name[] $extends + * @param Name[] $implements + * @param PropertyInfo[] $propertyInfos + * @param FuncInfo[] $funcInfos + */ + public function __construct( + Name $name, + int $flags, + string $type, + ?string $alias, + bool $isDeprecated, + bool $isStrictProperties, + array $extends, + array $implements, + array $propertyInfos, + array $funcInfos + ) { $this->name = $name; + $this->flags = $flags; + $this->type = $type; + $this->alias = $alias; + $this->isDeprecated = $isDeprecated; + $this->isStrictProperties = $isStrictProperties; + $this->extends = $extends; + $this->implements = $implements; + $this->propertyInfos = $propertyInfos; $this->funcInfos = $funcInfos; } + + public function getRegistration(): string + { + $params = []; + foreach ($this->extends as $extends) { + $params[] = "zend_class_entry *class_entry_" . implode("_", $extends->parts); + } + foreach ($this->implements as $implements) { + $params[] = "zend_class_entry *class_entry_" . implode("_", $implements->parts); + } + + $escapedName = implode("_", $this->name->parts); + + $code = "zend_class_entry *register_class_$escapedName(" . implode(", ", $params) . ")\n"; + + $code .= "{\n"; + $code .= "\tzend_class_entry ce, *class_entry;\n\n"; + if (count($this->name->parts) > 1) { + $className = $this->name->getLast(); + $namespace = $this->name->slice(0, -1); + + $code .= "\tINIT_NS_CLASS_ENTRY(ce, \"$namespace\", \"$className\", class_{$escapedName}_methods);\n"; + } else { + $code .= "\tINIT_CLASS_ENTRY(ce, \"$this->name\", class_{$escapedName}_methods);\n"; + } + + if ($this->type === "class" || $this->type === "trait") { + $code .= "\tclass_entry = zend_register_internal_class_ex(&ce, " . (isset($this->extends[0]) ? "class_entry_" . str_replace("\\", "_", $this->extends[0]->toString()) : "NULL") . ");\n"; + } else { + $code .= "\tclass_entry = zend_register_internal_interface(&ce);\n"; + } + if ($this->getFlagsAsString()) { + $code .= "\tclass_entry->ce_flags |= " . $this->getFlagsAsString() . ";\n"; + } + + $implements = array_map( + function (Name $item) { + return "class_entry_" . implode("_", $item->parts); + }, + $this->type === "interface" ? $this->extends : $this->implements + ); + + if (!empty($implements)) { + $code .= "\tzend_class_implements(class_entry, " . count($implements) . ", " . implode(", ", $implements) . ");\n"; + } + + if ($this->alias) { + $code .= "\tzend_register_class_alias(\"" . str_replace("\\", "_", $this->alias) . "\", class_entry);\n"; + } + + foreach ($this->propertyInfos as $property) { + $code .= $property->getDeclaration(); + } + + $code .= "\n\treturn class_entry;\n"; + + $code .= "}\n\n"; + + return $code; + } + + private function getFlagsAsString(): string + { + $flags = []; + + if ($this->type === "trait") { + $flags[] = "ZEND_ACC_TRAIT"; + } + + 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"; + } + + if ($this->isStrictProperties) { + $flags[] = "ZEND_ACC_NO_DYNAMIC_PROPERTIES"; + } + + return implode("|", $flags); + } } class FileInfo { @@ -988,6 +1303,8 @@ class FileInfo { public $declarationPrefix = ""; /** @var bool */ public $generateLegacyArginfo = false; + /** @var bool */ + public $generateClassEntries = false; /** * @return iterable @@ -998,6 +1315,15 @@ public function getAllFuncInfos(): iterable { yield from $classInfo->funcInfos; } } + + /** + * @return iterable + */ + public function getAllPropertyInfos(): iterable { + foreach ($this->classInfos as $classInfo) { + yield from $classInfo->propertyInfos; + } + } } class DocCommentTag { @@ -1209,6 +1535,98 @@ function parseFunctionLike( } } +function parseProperty( + Name $class, + int $flags, + Stmt\PropertyProperty $property, + ?Node $type, + ?DocComment $comment +): PropertyInfo { + $docType = false; + + if ($comment) { + $tags = parseDocComment($comment); + foreach ($tags as $tag) { + if ($tag->name === 'var') { + $docType = true; + } + } + } + + $propertyType = $type ? Type::fromNode($type) : null; + if ($propertyType === null && !$docType) { + throw new Exception("Missing type for property $class::\$$property->name"); + } + + if ($property->default instanceof Expr\ConstFetch && + $property->default->name->toLowerString() === "null" && + $propertyType && !$propertyType->isNullable() + ) { + $simpleType = $propertyType->tryToSimpleType(); + if ($simpleType === null) { + throw new Exception( + "Property $class::\$$property->name has null default, but is not nullable"); + } + } + + return new PropertyInfo( + new PropertyName($class, $property->name->__toString()), + $flags, + $propertyType, + $property->default + ); +} + +/** + * @param PropertyInfo[] $properties + * @param FuncInfo[] $methods + */ +function parseClass(Name $name, Stmt\ClassLike $class, array $properties, array $methods): ClassInfo { + $flags = $class instanceof Class_ ? $class->flags : 0; + $comment = $class->getDocComment(); + $alias = null; + $isDeprecated = false; + $isStrictProperties = false; + + if ($comment) { + $tags = parseDocComment($comment); + foreach ($tags as $tag) { + if ($tag->name === 'alias') { + $alias = $tag->getValue(); + } else if ($tag->name === 'deprecated') { + $isDeprecated = true; + } else if ($tag->name === 'strict-properties') { + $isStrictProperties = true; + } + } + } + + $extends = []; + $implements = []; + + if ($class instanceof Class_) { + if ($class->extends) { + $extends[] = $class->extends; + } + $implements = $class->implements; + } elseif ($class instanceof Interface_) { + $extends = $class->extends; + } + + return new ClassInfo( + $name, + $flags, + $class instanceof Class_ ? "class" : ($class instanceof Interface_ ? "interface" : "trait"), + $alias, + $isDeprecated, + $isStrictProperties, + $extends, + $implements, + $properties, + $methods + ); +} + function handlePreprocessorConditions(array &$conds, Stmt $stmt): ?string { foreach ($stmt->getComments() as $comment) { $text = trim($comment->getText()); @@ -1281,6 +1699,7 @@ function handleStatements(FileInfo $fileInfo, array $stmts, PrettyPrinterAbstrac if ($stmt instanceof Stmt\ClassLike) { $className = $stmt->namespacedName; + $propertyInfos = []; $methodInfos = []; foreach ($stmt->stmts as $classStmt) { $cond = handlePreprocessorConditions($conds, $classStmt); @@ -1288,7 +1707,7 @@ function handleStatements(FileInfo $fileInfo, array $stmts, PrettyPrinterAbstrac continue; } - if (!$classStmt instanceof Stmt\ClassMethod) { + if (!$classStmt instanceof Stmt\ClassMethod && !$classStmt instanceof Stmt\Property) { throw new Exception("Not implemented {$classStmt->getType()}"); } @@ -1303,20 +1722,32 @@ function handleStatements(FileInfo $fileInfo, array $stmts, PrettyPrinterAbstrac } if (!($flags & Class_::VISIBILITY_MODIFIER_MASK)) { - throw new Exception("Method visibility modifier is required"); + throw new Exception("Visibility modifier is required"); } - $methodInfos[] = parseFunctionLike( - $prettyPrinter, - new MethodName($className, $classStmt->name->toString()), - $classFlags, - $flags, - $classStmt, - $cond - ); + if ($classStmt instanceof Stmt\Property) { + foreach ($classStmt->props as $property) { + $propertyInfos[] = parseProperty( + $className, + $flags, + $property, + $classStmt->type, + $classStmt->getDocComment() + ); + } + } else if ($classStmt instanceof Stmt\ClassMethod) { + $methodInfos[] = parseFunctionLike( + $prettyPrinter, + new MethodName($className, $classStmt->name->toString()), + $classFlags, + $flags, + $classStmt, + $cond + ); + } } - $fileInfo->classInfos[] = new ClassInfo($className, $methodInfos); + $fileInfo->classInfos[] = parseClass($className, $stmt, $propertyInfos, $methodInfos); continue; } @@ -1348,10 +1779,16 @@ protected function pName_FullyQualified(Name\FullyQualified $node) { $fileInfo->declarationPrefix = $tag->value ? $tag->value . " " : ""; } else if ($tag->name === 'generate-legacy-arginfo') { $fileInfo->generateLegacyArginfo = true; + } else if ($tag->name === 'generate-class-entries') { + $fileInfo->generateClassEntries = true; } } } + if ($fileInfo->generateClassEntries && !$fileInfo->generateFunctionEntries) { + throw new Exception("Function entry generation must be enabled when generating class entries"); + } + handleStatements($fileInfo, $stmts, $prettyPrinter); return $fileInfo; } @@ -1533,6 +1970,20 @@ function (FuncInfo $funcInfo) use($fileInfo, &$generatedFunctionDeclarations) { } } + if ($fileInfo->generateClassEntries) { + $code .= generateClassEntryCode($fileInfo); + } + + return $code; +} + +function generateClassEntryCode(FileInfo $fileInfo): string { + $code = "\n"; + + foreach ($fileInfo->classInfos as $class) { + $code .= $class->getRegistration(); + } + return $code; } diff --git a/ext/curl/curl_file.c b/ext/curl/curl_file.c index aad162604a1f4..a81239a60aba2 100644 --- a/ext/curl/curl_file.c +++ b/ext/curl/curl_file.c @@ -122,12 +122,7 @@ ZEND_METHOD(CURLFile, setPostFilename) void curlfile_register_class(void) { - zend_class_entry ce; - INIT_CLASS_ENTRY( ce, "CURLFile", class_CURLFile_methods ); - curl_CURLFile_class = zend_register_internal_class(&ce); + curl_CURLFile_class = register_class_CURLFile(); curl_CURLFile_class->serialize = zend_class_serialize_deny; curl_CURLFile_class->unserialize = zend_class_unserialize_deny; - zend_declare_property_string(curl_CURLFile_class, "name", sizeof("name")-1, "", ZEND_ACC_PUBLIC); - zend_declare_property_string(curl_CURLFile_class, "mime", sizeof("mime")-1, "", ZEND_ACC_PUBLIC); - zend_declare_property_string(curl_CURLFile_class, "postname", sizeof("postname")-1, "", ZEND_ACC_PUBLIC); } diff --git a/ext/curl/curl_file.stub.php b/ext/curl/curl_file.stub.php index 28a218c69836f..7b0c83aa2f9aa 100644 --- a/ext/curl/curl_file.stub.php +++ b/ext/curl/curl_file.stub.php @@ -1,9 +1,19 @@ create_object = IntlIterator_object_create; IntlIterator_ce_ptr->get_iterator = IntlIterator_get_iterator; - zend_class_implements(IntlIterator_ce_ptr, 1, - zend_ce_iterator); memcpy(&IntlIterator_handlers, &std_object_handlers, sizeof IntlIterator_handlers); diff --git a/ext/intl/resourcebundle/resourcebundle.stub.php b/ext/intl/resourcebundle/resourcebundle.stub.php index fccd65e3df4f6..3585bf76e832f 100644 --- a/ext/intl/resourcebundle/resourcebundle.stub.php +++ b/ext/intl/resourcebundle/resourcebundle.stub.php @@ -1,6 +1,9 @@ create_object = ResourceBundle_object_create; + ResourceBundle_ce_ptr->get_iterator = resourcebundle_get_iterator; ResourceBundle_object_handlers = std_object_handlers; ResourceBundle_object_handlers.offset = XtOffsetOf(ResourceBundle_object, zend); @@ -380,7 +375,5 @@ void resourcebundle_register_class( void ) ResourceBundle_object_handlers.free_obj = ResourceBundle_object_free; ResourceBundle_object_handlers.read_dimension = resourcebundle_array_get; ResourceBundle_object_handlers.count_elements = resourcebundle_array_count; - - zend_class_implements(ResourceBundle_ce_ptr, 2, zend_ce_aggregate, zend_ce_countable); } /* }}} */ diff --git a/ext/intl/transliterator/transliterator.stub.php b/ext/intl/transliterator/transliterator.stub.php index c14a4cf0c060f..d9e29bd073797 100644 --- a/ext/intl/transliterator/transliterator.stub.php +++ b/ext/intl/transliterator/transliterator.stub.php @@ -1,9 +1,15 @@ create_object = Transliterator_object_create; + memcpy( &Transliterator_handlers, &std_object_handlers, sizeof Transliterator_handlers ); Transliterator_handlers.offset = XtOffsetOf(Transliterator_object, zo); Transliterator_handlers.free_obj = Transliterator_objects_free; Transliterator_handlers.clone_obj = Transliterator_clone_obj; @@ -273,17 +269,6 @@ void transliterator_register_Transliterator_class( void ) Transliterator_handlers.read_property = Transliterator_read_property; Transliterator_handlers.write_property = Transliterator_write_property; - /* Declare 'Transliterator' class properties */ - if( !Transliterator_ce_ptr ) - { - zend_error( E_ERROR, - "Transliterator: attempt to create properties " - "on a non-registered class." ); - return; - } - zend_declare_property_null( Transliterator_ce_ptr, - "id", sizeof( "id" ) - 1, ZEND_ACC_PUBLIC ); - /* constants are declared in transliterator_register_constants, called from MINIT */ } diff --git a/ext/libxml/libxml.c b/ext/libxml/libxml.c index b3ce853ef1fd9..088af307132a9 100644 --- a/ext/libxml/libxml.c +++ b/ext/libxml/libxml.c @@ -738,8 +738,6 @@ PHP_LIBXML_API void php_libxml_switch_context(zval *context, zval *oldcontext) static PHP_MINIT_FUNCTION(libxml) { - zend_class_entry ce; - php_libxml_initialize(); REGISTER_LONG_CONSTANT("LIBXML_VERSION", LIBXML_VERSION, CONST_CS | CONST_PERSISTENT); @@ -787,35 +785,7 @@ static PHP_MINIT_FUNCTION(libxml) REGISTER_LONG_CONSTANT("LIBXML_ERR_ERROR", XML_ERR_ERROR, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("LIBXML_ERR_FATAL", XML_ERR_FATAL, CONST_CS | CONST_PERSISTENT); - INIT_CLASS_ENTRY(ce, "LibXMLError", NULL); - libxmlerror_class_entry = zend_register_internal_class(&ce); - - zval default_val; - zend_string *name; - ZVAL_UNDEF(&default_val); - - name = zend_string_init("level", sizeof("level")-1, 1); - zend_declare_typed_property( - libxmlerror_class_entry, name, &default_val, ZEND_ACC_PUBLIC, NULL, - (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_LONG)); - zend_string_release(name); - zend_declare_typed_property( - libxmlerror_class_entry, ZSTR_KNOWN(ZEND_STR_CODE), &default_val, ZEND_ACC_PUBLIC, NULL, - (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_LONG)); - name = zend_string_init("column", sizeof("column")-1, 1); - zend_declare_typed_property( - libxmlerror_class_entry, name, &default_val, ZEND_ACC_PUBLIC, NULL, - (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_LONG)); - zend_string_release(name); - zend_declare_typed_property( - libxmlerror_class_entry, ZSTR_KNOWN(ZEND_STR_MESSAGE), &default_val, ZEND_ACC_PUBLIC, NULL, - (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_STRING)); - zend_declare_typed_property( - libxmlerror_class_entry, ZSTR_KNOWN(ZEND_STR_FILE), &default_val, ZEND_ACC_PUBLIC, NULL, - (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_STRING)); - zend_declare_typed_property( - libxmlerror_class_entry, ZSTR_KNOWN(ZEND_STR_LINE), &default_val, ZEND_ACC_PUBLIC, NULL, - (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_LONG)); + libxmlerror_class_entry = register_class_LibXMLError(); if (sapi_module.name) { static const char * const supported_sapis[] = { diff --git a/ext/libxml/libxml.stub.php b/ext/libxml/libxml.stub.php index 12685bd8ceea6..b20d681b96358 100644 --- a/ext/libxml/libxml.stub.php +++ b/ext/libxml/libxml.stub.php @@ -1,6 +1,19 @@ get_iterator = pdo_stmt_iter_get; pdo_dbstmt_ce->create_object = pdo_dbstmt_new; pdo_dbstmt_ce->serialize = zend_class_serialize_deny; pdo_dbstmt_ce->unserialize = zend_class_unserialize_deny; - zend_class_implements(pdo_dbstmt_ce, 1, zend_ce_aggregate); - zend_declare_property_null(pdo_dbstmt_ce, "queryString", sizeof("queryString")-1, ZEND_ACC_PUBLIC); memcpy(&pdo_dbstmt_object_handlers, &std_object_handlers, sizeof(zend_object_handlers)); pdo_dbstmt_object_handlers.offset = XtOffsetOf(pdo_stmt_t, std); @@ -2525,9 +2520,7 @@ void pdo_stmt_init(void) pdo_dbstmt_object_handlers.compare = dbstmt_compare; pdo_dbstmt_object_handlers.clone_obj = NULL; - INIT_CLASS_ENTRY(ce, "PDORow", class_PDORow_methods); - pdo_row_ce = zend_register_internal_class(&ce); - pdo_row_ce->ce_flags |= ZEND_ACC_FINAL; /* when removing this a lot of handlers need to be redone */ + pdo_row_ce = register_class_PDORow(); pdo_row_ce->create_object = pdo_row_new; pdo_row_ce->serialize = zend_class_serialize_deny; pdo_row_ce->unserialize = zend_class_unserialize_deny; diff --git a/ext/pdo/pdo_stmt.stub.php b/ext/pdo/pdo_stmt.stub.php index ab3ab6a42fe95..dbc8aa86352c1 100644 --- a/ext/pdo/pdo_stmt.stub.php +++ b/ext/pdo/pdo_stmt.stub.php @@ -1,9 +1,15 @@ ce_flags |= ZEND_ACC_FINAL; + + return class_entry; +} + diff --git a/ext/simplexml/simplexml.c b/ext/simplexml/simplexml.c index 32899e576c67a..dc74967ededbc 100644 --- a/ext/simplexml/simplexml.c +++ b/ext/simplexml/simplexml.c @@ -2682,14 +2682,9 @@ ZEND_GET_MODULE(simplexml) /* {{{ PHP_MINIT_FUNCTION(simplexml) */ PHP_MINIT_FUNCTION(simplexml) { - zend_class_entry ce; - - INIT_CLASS_ENTRY(ce, "SimpleXMLElement", class_SimpleXMLElement_methods); - sxe_class_entry = zend_register_internal_class(&ce); + sxe_class_entry = register_class_SimpleXMLElement(zend_ce_stringable, zend_ce_countable, spl_ce_RecursiveIterator); sxe_class_entry->create_object = sxe_object_new; sxe_class_entry->get_iterator = php_sxe_get_iterator; - zend_class_implements(sxe_class_entry, 3, - zend_ce_countable, zend_ce_stringable, spl_ce_RecursiveIterator); memcpy(&sxe_object_handlers, &std_object_handlers, sizeof(zend_object_handlers)); sxe_object_handlers.offset = XtOffsetOf(php_sxe_object, zo); @@ -2719,8 +2714,7 @@ PHP_MINIT_FUNCTION(simplexml) /* TODO: Why do we have two variables for this? */ ce_SimpleXMLElement = sxe_class_entry; - INIT_CLASS_ENTRY(ce, "SimpleXMLIterator", NULL); - ce_SimpleXMLIterator = zend_register_internal_class_ex(&ce, ce_SimpleXMLElement); + ce_SimpleXMLIterator = register_class_SimpleXMLIterator(ce_SimpleXMLElement); php_libxml_register_export(sxe_class_entry, simplexml_export_node); diff --git a/ext/simplexml/simplexml.stub.php b/ext/simplexml/simplexml.stub.php index 7d56de88e9fec..da562db7e4c94 100644 --- a/ext/simplexml/simplexml.stub.php +++ b/ext/simplexml/simplexml.stub.php @@ -1,6 +1,9 @@ create_object = xml_parser_create_object; - xml_parser_ce->ce_flags |= ZEND_ACC_FINAL | ZEND_ACC_NO_DYNAMIC_PROPERTIES; xml_parser_ce->serialize = zend_class_serialize_deny; xml_parser_ce->unserialize = zend_class_unserialize_deny; diff --git a/ext/xml/xml.stub.php b/ext/xml/xml.stub.php index 6dc10d58b5b0e..8167c68e86db2 100644 --- a/ext/xml/xml.stub.php +++ b/ext/xml/xml.stub.php @@ -1,6 +1,9 @@ ce_flags |= ZEND_ACC_FINAL|ZEND_ACC_NO_DYNAMIC_PROPERTIES; + + return class_entry; +} + diff --git a/ext/zend_test/test.c b/ext/zend_test/test.c index c44fdb481f354..1b9ba4ce5956e 100644 --- a/ext/zend_test/test.c +++ b/ext/zend_test/test.c @@ -346,40 +346,13 @@ static void custom_zend_execute_ex(zend_execute_data *execute_data) PHP_MINIT_FUNCTION(zend_test) { - zend_class_entry class_entry; - - INIT_CLASS_ENTRY(class_entry, "_ZendTestInterface", NULL); - zend_test_interface = zend_register_internal_interface(&class_entry); + zend_test_interface = register_class__ZendTestInterface(); zend_declare_class_constant_long(zend_test_interface, ZEND_STRL("DUMMY"), 0); - INIT_CLASS_ENTRY(class_entry, "_ZendTestClass", class__ZendTestClass_methods); - zend_test_class = zend_register_internal_class(&class_entry); - zend_class_implements(zend_test_class, 1, zend_test_interface); + + zend_test_class = register_class__ZendTestClass(zend_test_interface); zend_test_class->create_object = zend_test_class_new; zend_test_class->get_static_method = zend_test_class_static_method_get; - zend_declare_property_null(zend_test_class, "_StaticProp", sizeof("_StaticProp") - 1, ZEND_ACC_STATIC); - - { - zend_string *name = zend_string_init("intProp", sizeof("intProp") - 1, 1); - zval val; - ZVAL_LONG(&val, 123); - zend_declare_typed_property( - zend_test_class, name, &val, ZEND_ACC_PUBLIC, NULL, - (zend_type) ZEND_TYPE_INIT_CODE(IS_LONG, 0, 0)); - zend_string_release(name); - } - - { - zend_string *name = zend_string_init("classProp", sizeof("classProp") - 1, 1); - zend_string *class_name = zend_string_init("stdClass", sizeof("stdClass") - 1, 1); - zval val; - ZVAL_NULL(&val); - zend_declare_typed_property( - zend_test_class, name, &val, ZEND_ACC_PUBLIC, NULL, - (zend_type) ZEND_TYPE_INIT_CLASS(class_name, 1, 0)); - zend_string_release(name); - } - { zend_string *name = zend_string_init("classUnionProp", sizeof("classUnionProp") - 1, 1); zend_string *class_name1 = zend_string_init("stdClass", sizeof("stdClass") - 1, 1); @@ -395,35 +368,16 @@ PHP_MINIT_FUNCTION(zend_test) zend_string_release(name); } - { - zend_string *name = zend_string_init("staticIntProp", sizeof("staticIntProp") - 1, 1); - zval val; - ZVAL_LONG(&val, 123); - zend_declare_typed_property( - zend_test_class, name, &val, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC, NULL, - (zend_type) ZEND_TYPE_INIT_CODE(IS_LONG, 0, 0)); - zend_string_release(name); - } - - INIT_CLASS_ENTRY(class_entry, "_ZendTestChildClass", NULL); - zend_test_child_class = zend_register_internal_class_ex(&class_entry, zend_test_class); + zend_test_child_class = register_class__ZendTestChildClass(zend_test_class); memcpy(&zend_test_class_handlers, &std_object_handlers, sizeof(zend_object_handlers)); zend_test_class_handlers.get_method = zend_test_class_method_get; - INIT_CLASS_ENTRY(class_entry, "_ZendTestTrait", class__ZendTestTrait_methods); - zend_test_trait = zend_register_internal_class(&class_entry); - zend_test_trait->ce_flags |= ZEND_ACC_TRAIT; - zend_declare_property_null(zend_test_trait, "testProp", sizeof("testProp")-1, ZEND_ACC_PUBLIC); - - zend_register_class_alias("_ZendTestClassAlias", zend_test_class); + zend_test_trait = register_class__ZendTestTrait(); REGISTER_LONG_CONSTANT("ZEND_TEST_DEPRECATED", 42, CONST_PERSISTENT | CONST_DEPRECATED); - INIT_CLASS_ENTRY(class_entry, "ZendTestAttribute", NULL); - zend_test_attribute = zend_register_internal_class(&class_entry); - zend_test_attribute->ce_flags |= ZEND_ACC_FINAL; - + zend_test_attribute = register_class_ZendTestAttribute(); { zend_internal_attribute *attr = zend_internal_attribute_register(zend_test_attribute, ZEND_ATTRIBUTE_TARGET_ALL); attr->validator = zend_attribute_validate_zendtestattribute; diff --git a/ext/zend_test/test.stub.php b/ext/zend_test/test.stub.php index 91ff78e113e0a..e72094028d125 100644 --- a/ext/zend_test/test.stub.php +++ b/ext/zend_test/test.stub.php @@ -1,10 +1,26 @@ ce_flags |= ZEND_ACC_TRAIT; + + zval property_testProp_default_value; + ZVAL_NULL(&property_testProp_default_value); + zend_string *property_testProp_name = zend_string_init("testProp", sizeof("testProp") - 1, 1); + zend_declare_property_ex(class_entry, property_testProp_name, &property_testProp_default_value, ZEND_ACC_PUBLIC, NULL); + zend_string_release(property_testProp_name); + + return class_entry; +} + +zend_class_entry *register_class_ZendTestAttribute() +{ + zend_class_entry ce, *class_entry; + + INIT_CLASS_ENTRY(ce, "ZendTestAttribute", class_ZendTestAttribute_methods); + class_entry = zend_register_internal_class_ex(&ce, NULL); + class_entry->ce_flags |= ZEND_ACC_FINAL; + + return class_entry; +} + +zend_class_entry *register_class_ZendTestNS_Foo() +{ + zend_class_entry ce, *class_entry; + + INIT_NS_CLASS_ENTRY(ce, "ZendTestNS", "Foo", class_ZendTestNS_Foo_methods); + class_entry = zend_register_internal_class_ex(&ce, NULL); + + return class_entry; +} + +zend_class_entry *register_class_ZendTestNS2_Foo() +{ + zend_class_entry ce, *class_entry; + + INIT_NS_CLASS_ENTRY(ce, "ZendTestNS2", "Foo", class_ZendTestNS2_Foo_methods); + class_entry = zend_register_internal_class_ex(&ce, NULL); + + return class_entry; +} + diff --git a/ext/zip/php_zip.stub.php b/ext/zip/php_zip.stub.php index 5ae4731ae25d5..ff7b9d99fbe9f 100644 --- a/ext/zip/php_zip.stub.php +++ b/ext/zip/php_zip.stub.php @@ -66,6 +66,9 @@ function zip_entry_compressionmethod($zip_entry): string|false {} class ZipArchive { + /** @var string|null */ + public $name; + /** @return bool|int */ public function open(string $filename, int $flags = 0) {} diff --git a/ext/zip/php_zip_arginfo.h b/ext/zip/php_zip_arginfo.h index ba72e52aa8b6b..104fac05a0b56 100644 --- a/ext/zip/php_zip_arginfo.h +++ b/ext/zip/php_zip_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 095084d2a2df557398191af5dd6c8bea6bb7c338 */ + * Stub hash: a5b8a10dbaeddf5d0d1ac751ff64a5d7ff235562 */ ZEND_BEGIN_ARG_INFO_EX(arginfo_zip_open, 0, 0, 1) ZEND_ARG_TYPE_INFO(0, filename, IS_STRING, 0)