-
Notifications
You must be signed in to change notification settings - Fork 8k
Generate C enums from internal enums, introduce Z_PARAM_ENUM() #20917
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
TimWolla
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some first comments. Will take another look once rebased after the merge of #20915.
|
|
||
| static zend_always_inline zend_long zend_enum_fetch_case_id(zend_object *zobj) | ||
| { | ||
| ZEND_ASSERT(zobj->ce->ce_flags & ZEND_ACC_ENUM); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This assert is redundant with the one in zend_enum_obj_from_obj(). I also don't see how it could help with codegen.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I though about this when adding this assert, but if we consider functions as opaque APIs, we don't know that zend_enum_obj_from_obj() has redundant asserts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is fair, but in this case it is probably reasonable to expect zend_enum_obj_from_obj() to do the verification (if necessary), since that's the purpose of the function, especially since a failed assert is always a programmer error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No particularly strong feelings either way, though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The _decl files should be listed in .gitattributes as linguist-generated -diff.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. I've added **/*_decl.h, but this could accidentally match unrelated files. I'm hesitating to use another name that would be less likely to match unrelated files, such as {$ext}_arginfo_decl.h.
0b7ca66 to
adfa289
Compare
|
Nice approach. How will ADTs be handled? |
|
@iluuu1994 yes this should work. Alternatively it's possible to use |
| # include "php.h" | ||
| # include "php_random_csprng.h" | ||
| # include "php_random_uint128.h" | ||
| # include "random_decl.h" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the generated headername maybe be prefixed with php_ ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The file name is generated from the stub file name. In this case the stub file is named random.stub.php, so we generate random_arginfo.h and random_decl.h. Some extensions have a stub file whose name is prefixed with php_, but most don't.
The compiler should now be able to tell us if we forget one case
8614167 to
40036ff
Compare
TimWolla
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looked at everything, except ext/dom (cc @ndossche) and gen_stub.php.
Requesting changes due to the ext/uri bug I noticed. The others are primarily suggestions to clean this up as much as possible.
ext/uri/php_uri.c
Outdated
| { | ||
| zend_object *that_object; | ||
| zend_object *comparison_mode = NULL; | ||
| zend_enum_Uri_UriComparisonMode comparison_mode = ZEND_ENUM_Uri_UriComparisonMode_IncludeFragment; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the wrong default:
php-src/ext/uri/php_uri.stub.php
Line 84 in 62c94b6
| public function equals(\Uri\Rfc3986\Uri $uri, \Uri\UriComparisonMode $comparisonMode = \Uri\UriComparisonMode::ExcludeFragment): bool {} |
I'm confused why this wasn't caught by tests, perhaps no test tests the default value, but only the explicit parameter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, during one of the last review round to the equals() tests (#20391 (review)), all the default value tests were removed IIRC. I noticed it at that time, but I would have never expected that they would catch a bug this fast.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch!
Zend/zend_enum.c
Outdated
| zend_object *zend_enum_new(zval *result, zend_class_entry *ce, int case_id, zend_string *case_name, zval *backing_value_zv) | ||
| { | ||
| zend_object *zobj = zend_objects_new(ce); | ||
| zend_enum_obj *intern = zend_object_alloc(sizeof(zend_enum_obj), ce); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| zend_enum_obj *intern = zend_object_alloc(sizeof(zend_enum_obj), ce); | |
| zend_enum_obj *intern = zend_object_alloc(sizeof(*intern), ce); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also prefer this style, but from what I've seen there is no consensus on this. I've applied the suggestion anyway.
3f54e91 to
2bb15f6
Compare
Update
gen_stubs.phpto generate C enums from internal enums. Enum values can be compared to the result ofzend_enum_fetch_case_id(zend_object*).The generated enums are added to separate files named
{$extensionName}_decl.h(one for each extension declaring some enums), so that it's possible to include these from anywhere._arginfo.hfiles would generate warnings if we tried to include them in a compilation unit that doesn't call theregister_{$class}functions, for instance.Introduce
Z_PARAM_ENUM()(similarly to #20898).cc @TimWolla