Skip to content

Commit 44340c4

Browse files
committed
Move AST attribute functions to zend_ast.c
1 parent b737879 commit 44340c4

File tree

5 files changed

+70
-69
lines changed

5 files changed

+70
-69
lines changed

Zend/zend_ast.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2203,3 +2203,65 @@ ZEND_API void zend_ast_convert_attributes(zval *ret, HashTable *attributes, zend
22032203
}
22042204
}
22052205

2206+
void zend_ast_add_attribute(zend_ast *name, zend_ast *value) /* {{{ */
2207+
{
2208+
zval *val, tmp;
2209+
zend_string *key;
2210+
2211+
znode class_node;
2212+
2213+
zval *zv = zend_ast_get_zval(name);
2214+
key = Z_STR_P(zv);
2215+
2216+
if (!CG(attributes)) {
2217+
ALLOC_HASHTABLE(CG(attributes));
2218+
zend_hash_init(CG(attributes), 8, NULL, ZVAL_PTR_DTOR, 0);
2219+
}
2220+
2221+
ZVAL_NULL(&tmp);
2222+
val = zend_hash_add(CG(attributes), key, &tmp);
2223+
2224+
if (!val) {
2225+
zend_error_noreturn(E_COMPILE_ERROR, "Redeclared attribute %s", ZSTR_VAL(key));
2226+
}
2227+
2228+
if (value) {
2229+
if (value->kind == ZEND_AST_ZVAL) {
2230+
zval *zv = zend_ast_get_zval(value);
2231+
2232+
if (Z_TYPE_P(zv) == IS_ARRAY) {
2233+
ZVAL_COPY_VALUE(val, zv);
2234+
} else {
2235+
array_init(val);
2236+
zend_hash_next_index_insert_new(Z_ARRVAL_P(val), zv);
2237+
}
2238+
} else {
2239+
zend_const_expr_to_zval(&tmp, value);
2240+
array_init(val);
2241+
zend_hash_next_index_insert_new(Z_ARRVAL_P(val), &tmp);
2242+
}
2243+
} else {
2244+
array_init(val);
2245+
}
2246+
}
2247+
/* }}} */
2248+
2249+
zend_ast *zend_ast_add_attribute_value(zend_ast *list_ast, zend_ast *val_ast) /* {{{ */
2250+
{
2251+
zval *list, *val, arr, tmp;
2252+
2253+
if (list_ast->kind == ZEND_AST_ZVAL) {
2254+
list = zend_ast_get_zval(list_ast);
2255+
if (Z_TYPE_P(list) != IS_ARRAY) {
2256+
array_init(&arr);
2257+
zend_hash_next_index_insert_new(Z_ARRVAL(arr), list);
2258+
ZVAL_ARR(list, Z_ARR(arr));
2259+
}
2260+
2261+
val = zend_ast_get_zval(val_ast);
2262+
zend_hash_next_index_insert_new(Z_ARRVAL_P(list), val);
2263+
}
2264+
2265+
return list_ast;
2266+
}
2267+
/* }}} */

Zend/zend_ast.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,4 +353,7 @@ static zend_always_inline zend_ast *zend_ast_list_rtrim(zend_ast *ast) {
353353
}
354354
return ast;
355355
}
356+
357+
void zend_ast_add_attribute(zend_ast *name, zend_ast *value);
358+
zend_ast *zend_ast_add_attribute_value(zend_ast *list_ast, zend_ast *val_ast);
356359
#endif

Zend/zend_compile.c

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -9283,66 +9283,4 @@ void zend_eval_const_expr(zend_ast **ast_ptr) /* {{{ */
92839283
}
92849284
/* }}} */
92859285

9286-
void zend_add_attribute(zend_ast *name, zend_ast *value) /* {{{ */
9287-
{
9288-
zval *val, tmp;
9289-
zend_string *key;
9290-
9291-
znode class_node;
9292-
9293-
zend_compile_class_ref(&class_node, name, ZEND_FETCH_CLASS_EXCEPTION);
9294-
key = Z_STR(class_node.u.constant);
9295-
9296-
if (!CG(attributes)) {
9297-
ALLOC_HASHTABLE(CG(attributes));
9298-
zend_hash_init(CG(attributes), 8, NULL, ZVAL_PTR_DTOR, 0);
9299-
}
9300-
9301-
ZVAL_NULL(&tmp);
9302-
val = zend_hash_add(CG(attributes), key, &tmp);
9303-
9304-
if (!val) {
9305-
zend_error_noreturn(E_COMPILE_ERROR, "Redeclared attribute %s", ZSTR_VAL(key));
9306-
}
9307-
9308-
if (value) {
9309-
if (value->kind == ZEND_AST_ZVAL) {
9310-
zval *zv = zend_ast_get_zval(value);
9311-
9312-
if (Z_TYPE_P(zv) == IS_ARRAY) {
9313-
ZVAL_COPY_VALUE(val, zv);
9314-
} else {
9315-
array_init(val);
9316-
zend_hash_next_index_insert_new(Z_ARRVAL_P(val), zv);
9317-
}
9318-
} else {
9319-
zend_const_expr_to_zval(&tmp, value);
9320-
array_init(val);
9321-
zend_hash_next_index_insert_new(Z_ARRVAL_P(val), &tmp);
9322-
}
9323-
} else {
9324-
array_init(val);
9325-
}
9326-
}
9327-
/* }}} */
9328-
9329-
zend_ast *zend_add_attribute_value(zend_ast *list_ast, zend_ast *val_ast) /* {{{ */
9330-
{
9331-
zval *list, *val, arr, tmp;
9332-
9333-
if (list_ast->kind == ZEND_AST_ZVAL) {
9334-
list = zend_ast_get_zval(list_ast);
9335-
if (Z_TYPE_P(list) != IS_ARRAY) {
9336-
array_init(&arr);
9337-
zend_hash_next_index_insert_new(Z_ARRVAL(arr), list);
9338-
ZVAL_ARR(list, Z_ARR(arr));
9339-
}
9340-
9341-
val = zend_ast_get_zval(val_ast);
9342-
zend_hash_next_index_insert_new(Z_ARRVAL_P(list), val);
9343-
}
9344-
9345-
return list_ast;
9346-
}
9347-
/* }}} */
93489286

Zend/zend_compile.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -749,8 +749,6 @@ zend_ast *zend_negate_num_string(zend_ast *ast);
749749
uint32_t zend_add_class_modifier(uint32_t flags, uint32_t new_flag);
750750
uint32_t zend_add_member_modifier(uint32_t flags, uint32_t new_flag);
751751
zend_bool zend_handle_encoding_declaration(zend_ast *ast);
752-
void zend_add_attribute(zend_ast *name, zend_ast *value);
753-
zend_ast *zend_add_attribute_value(zend_ast *list_ast, zend_ast *val_ast);
754752

755753
/* parser-driven code generators */
756754
void zend_do_free(znode *op1);

Zend/zend_language_parser.y

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -316,14 +316,14 @@ name:
316316

317317
attribute_values:
318318
expr { $$ = $1; }
319-
| attribute_values ',' expr { $$ = zend_add_attribute_value($1, $3); }
319+
| attribute_values ',' expr { $$ = zend_ast_add_attribute_value($1, $3); }
320320
;
321321

322322
attribute_list:
323-
class_name_reference { zend_add_attribute($1, NULL); }
324-
| class_name_reference '(' attribute_values ')' { zend_add_attribute($1, $3); }
325-
| attribute_list ',' class_name_reference { zend_add_attribute($3, NULL); }
326-
| attribute_list ',' class_name_reference '(' attribute_values ')' { zend_add_attribute($3, $5); }
323+
class_name_reference { zend_ast_add_attribute($1, NULL); }
324+
| class_name_reference '(' attribute_values ')' { zend_ast_add_attribute($1, $3); }
325+
| attribute_list ',' class_name_reference { zend_ast_add_attribute($3, NULL); }
326+
| attribute_list ',' class_name_reference '(' attribute_values ')' { zend_ast_add_attribute($3, $5); }
327327
;
328328

329329
attribute:

0 commit comments

Comments
 (0)