Skip to content

Rfc: inner class3 #4

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

Open
wants to merge 65 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 62 commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
bee945b
add new token
withinboredom Mar 8, 2025
3d05dd7
create the new grammar
withinboredom Mar 8, 2025
a815b8b
add new ast pieces
withinboredom Mar 8, 2025
978be06
handle modifiers
withinboredom Mar 8, 2025
466f6c8
handle compiling class declarations
withinboredom Mar 8, 2025
cbfc5bc
add initial implementation;
withinboredom Mar 8, 2025
d9e9ccf
get return types working
withinboredom Mar 9, 2025
aa6d3e9
make ::class work
withinboredom Mar 9, 2025
bb94414
fix failing test
withinboredom Mar 9, 2025
b2581f4
add another class
withinboredom Mar 9, 2025
787dd8e
add more tests to validate scope resolution keywords
withinboredom Mar 9, 2025
05225e3
update reflection
withinboredom Mar 9, 2025
6e8031c
enable visibility
withinboredom Mar 9, 2025
d4a4d32
add tests for autoloading
withinboredom Mar 9, 2025
b42316c
properly persist classes in opcache
withinboredom Mar 9, 2025
cbd51f7
handle return type visibility enforcement
withinboredom Mar 9, 2025
a86c26e
fix static member access
withinboredom Mar 11, 2025
269a403
move tests
withinboredom Mar 13, 2025
a624033
add more tests and fix access modifiers
withinboredom Mar 13, 2025
e8d7570
add support for lexical scope
withinboredom Mar 13, 2025
2e1d287
handle type visibility
withinboredom Mar 14, 2025
d33cd2f
fix opcache
withinboredom Mar 14, 2025
a7364af
fix test
withinboredom Mar 14, 2025
63a0882
refine some error messages
withinboredom Mar 14, 2025
aafbda4
add type check to return type
withinboredom Mar 14, 2025
0966e30
fix tests
withinboredom Mar 14, 2025
6774e61
temporarily fix this test -- might want to undo this
withinboredom Mar 14, 2025
9297a01
clean up a bit and check readonly
withinboredom Mar 14, 2025
441eb93
handle visibility of methods
withinboredom Mar 14, 2025
bcee5ab
just do not cache -- more trouble than it is worth right now
withinboredom Mar 14, 2025
4b847cd
properly handle lexical scope
withinboredom Mar 14, 2025
5a5e44b
handle long names
withinboredom Mar 14, 2025
f24caa3
handle more visibility
withinboredom Mar 14, 2025
20b2699
handle constructor visibility
withinboredom Mar 15, 2025
e4d3e62
prevent static access
withinboredom Mar 15, 2025
609ce36
remove T_INNER_REF
withinboredom Mar 24, 2025
b2bb823
allow inner classes to use \\ now
withinboredom Mar 24, 2025
e17d677
fix prototype issue
withinboredom Mar 24, 2025
63745a5
defer compiling inner classes
withinboredom Mar 24, 2025
7a16a09
handle nesting classes in functions
withinboredom Mar 24, 2025
1f2ddb2
update tests
withinboredom Mar 25, 2025
4f441cb
do not elide return checks for inner classes
withinboredom Mar 25, 2025
d9ae53b
do visibility check during instantiation
withinboredom Mar 25, 2025
864c4c6
replace wrong token
withinboredom Mar 25, 2025
c342b5d
remove inner class AST
withinboredom Mar 25, 2025
6c28874
defer compiling inner classes
withinboredom Mar 25, 2025
a92ba63
allow looking up classes in scopes
withinboredom Mar 25, 2025
bbc0fed
fix isInnerClass
withinboredom Mar 25, 2025
ce0db15
add build artifacts
withinboredom Mar 25, 2025
e716cc9
Revert "defer compiling inner classes"
withinboredom Mar 25, 2025
a3abaff
update and fix tests
withinboredom Mar 25, 2025
28a5466
handle namespaces properly
withinboredom Mar 25, 2025
ab0dd57
add to windows build
withinboredom Mar 25, 2025
7ca157c
fix memory leak
withinboredom Mar 25, 2025
416bde2
fixup last commit
withinboredom Mar 25, 2025
1143b6c
ensure global scope does not have a lexical scope
withinboredom Mar 25, 2025
6f10bda
ensure we do not escape class scope boundaries
withinboredom Mar 25, 2025
226adc4
move namespace destruction to end of process
withinboredom Mar 25, 2025
4ac9b82
remove dangling grammar
withinboredom Mar 25, 2025
0ccd215
make a copy of the empty string
withinboredom Mar 25, 2025
9579c43
ensure namespaces are not used after free
withinboredom Mar 25, 2025
0a68147
tighten up namespaces
withinboredom Mar 25, 2025
a65d578
finally fix the elusive memory leak
withinboredom Mar 25, 2025
7275681
stupid errors
withinboredom Mar 26, 2025
13f130e
destroy namespaces after executor shutdown
withinboredom Mar 26, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Zend/Optimizer/dfa_pass.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ static inline bool can_elide_list_type(
zend_string *lcname = zend_string_tolower(ZEND_TYPE_NAME(*single_type));
zend_class_entry *ce = zend_optimizer_get_class_entry(script, op_array, lcname);
zend_string_release(lcname);
bool result = ce && safe_instanceof(use_info->ce, ce);
bool result = ce && !ce->required_scope && safe_instanceof(use_info->ce, ce);
if (result == !is_intersection) {
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion Zend/tests/errmsg/errmsg_027.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ class test {
echo "Done\n";
?>
--EXPECTF--
Fatal error: Class declarations may not be nested in %s on line %d
Fatal error: Class declarations may not be declared inside functions in %s on line %d
5 changes: 5 additions & 0 deletions Zend/zend.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
*/

#include "zend.h"

#include <zend_namespaces.h>

#include "zend_extensions.h"
#include "zend_modules.h"
#include "zend_constants.h"
Expand Down Expand Up @@ -1228,6 +1231,8 @@ void zend_shutdown(void) /* {{{ */
zend_unload_modules();

zend_optimizer_shutdown();

zend_destroy_namespaces();
startup_done = false;
}
/* }}} */
Expand Down
4 changes: 4 additions & 0 deletions Zend/zend.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ struct _zend_class_entry {
HashTable properties_info;
HashTable constants_table;

zend_class_entry *required_scope;
zend_class_entry *lexical_scope;
char required_scope_absolute;

ZEND_MAP_PTR_DEF(zend_class_mutable_data*, mutable_data);
zend_inheritance_cache_entry *inheritance_cache;

Expand Down
24 changes: 24 additions & 0 deletions Zend/zend_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -1816,6 +1816,30 @@ static zend_always_inline zend_result _object_and_properties_init(zval *arg, zen
return FAILURE;
}

if (class_type->required_scope) {
const zend_class_entry *scope = zend_get_executed_scope();
if (UNEXPECTED(scope == NULL)) {
zend_throw_error(NULL, "Cannot instantiate class %s from the global scope", ZSTR_VAL(class_type->name));
ZVAL_NULL(arg);
Z_OBJ_P(arg) = NULL;
return FAILURE;
}

if (class_type->required_scope_absolute) {
if (scope != class_type->required_scope && scope->lexical_scope != class_type->required_scope) {
zend_throw_error(NULL, "Cannot instantiate private class %s from %s", ZSTR_VAL(class_type->name), ZSTR_VAL(scope->name));
ZVAL_NULL(arg);
Z_OBJ_P(arg) = NULL;
return FAILURE;
}
} else if (!instanceof_function(scope, class_type->required_scope) && !instanceof_function(scope->lexical_scope, class_type->required_scope)) {
zend_throw_error(NULL, "Cannot instantiate protected class %s from %s", ZSTR_VAL(class_type->name), ZSTR_VAL(scope->name));
ZVAL_NULL(arg);
Z_OBJ_P(arg) = NULL;
return FAILURE;
}
}

if (UNEXPECTED(!(class_type->ce_flags & ZEND_ACC_CONSTANTS_UPDATED))) {
if (UNEXPECTED(zend_update_class_constants(class_type) != SUCCESS)) {
ZVAL_NULL(arg);
Expand Down
1 change: 1 addition & 0 deletions Zend/zend_ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ enum _zend_ast_kind {
ZEND_AST_YIELD,
ZEND_AST_COALESCE,
ZEND_AST_ASSIGN_COALESCE,
ZEND_AST_INNER_CLASS,

ZEND_AST_STATIC,
ZEND_AST_WHILE,
Expand Down
Loading
Loading