PageRenderTime 111ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/Zend/zend_compile.c

http://github.com/php/php-src
C | 9277 lines | 8026 code | 924 blank | 327 comment | 1618 complexity | a142d13b4798cf62d9d1e736f262e637 MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) Zend Technologies Ltd. (http://www.zend.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.00 of the Zend license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.zend.com/license/2_00.txt. |
  11. | If you did not receive a copy of the Zend license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@zend.com so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Andi Gutmans <andi@php.net> |
  16. | Zeev Suraski <zeev@php.net> |
  17. | Nikita Popov <nikic@php.net> |
  18. +----------------------------------------------------------------------+
  19. */
  20. #include <zend_language_parser.h>
  21. #include "zend.h"
  22. #include "zend_compile.h"
  23. #include "zend_constants.h"
  24. #include "zend_llist.h"
  25. #include "zend_API.h"
  26. #include "zend_exceptions.h"
  27. #include "zend_interfaces.h"
  28. #include "zend_virtual_cwd.h"
  29. #include "zend_multibyte.h"
  30. #include "zend_language_scanner.h"
  31. #include "zend_inheritance.h"
  32. #include "zend_vm.h"
  33. #define SET_NODE(target, src) do { \
  34. target ## _type = (src)->op_type; \
  35. if ((src)->op_type == IS_CONST) { \
  36. target.constant = zend_add_literal(&(src)->u.constant); \
  37. } else { \
  38. target = (src)->u.op; \
  39. } \
  40. } while (0)
  41. #define GET_NODE(target, src) do { \
  42. (target)->op_type = src ## _type; \
  43. if ((target)->op_type == IS_CONST) { \
  44. ZVAL_COPY_VALUE(&(target)->u.constant, CT_CONSTANT(src)); \
  45. } else { \
  46. (target)->u.op = src; \
  47. } \
  48. } while (0)
  49. #define FC(member) (CG(file_context).member)
  50. typedef struct _zend_loop_var {
  51. zend_uchar opcode;
  52. zend_uchar var_type;
  53. uint32_t var_num;
  54. uint32_t try_catch_offset;
  55. } zend_loop_var;
  56. static inline uint32_t zend_alloc_cache_slots(unsigned count) {
  57. if (count == 0) {
  58. return (uint32_t) -1;
  59. }
  60. zend_op_array *op_array = CG(active_op_array);
  61. uint32_t ret = op_array->cache_size;
  62. op_array->cache_size += count * sizeof(void*);
  63. return ret;
  64. }
  65. static inline uint32_t zend_alloc_cache_slot(void) {
  66. return zend_alloc_cache_slots(1);
  67. }
  68. ZEND_API zend_op_array *(*zend_compile_file)(zend_file_handle *file_handle, int type);
  69. ZEND_API zend_op_array *(*zend_compile_string)(zval *source_string, const char *filename);
  70. #ifndef ZTS
  71. ZEND_API zend_compiler_globals compiler_globals;
  72. ZEND_API zend_executor_globals executor_globals;
  73. #endif
  74. static zend_op *zend_emit_op(znode *result, zend_uchar opcode, znode *op1, znode *op2);
  75. static zend_bool zend_try_ct_eval_array(zval *result, zend_ast *ast);
  76. static void init_op(zend_op *op)
  77. {
  78. MAKE_NOP(op);
  79. op->extended_value = 0;
  80. op->lineno = CG(zend_lineno);
  81. }
  82. static zend_always_inline uint32_t get_next_op_number(void)
  83. {
  84. return CG(active_op_array)->last;
  85. }
  86. static zend_op *get_next_op(void)
  87. {
  88. zend_op_array *op_array = CG(active_op_array);
  89. uint32_t next_op_num = op_array->last++;
  90. zend_op *next_op;
  91. if (UNEXPECTED(next_op_num >= CG(context).opcodes_size)) {
  92. CG(context).opcodes_size *= 4;
  93. op_array->opcodes = erealloc(op_array->opcodes, CG(context).opcodes_size * sizeof(zend_op));
  94. }
  95. next_op = &(op_array->opcodes[next_op_num]);
  96. init_op(next_op);
  97. return next_op;
  98. }
  99. static zend_brk_cont_element *get_next_brk_cont_element(void)
  100. {
  101. CG(context).last_brk_cont++;
  102. CG(context).brk_cont_array = erealloc(CG(context).brk_cont_array, sizeof(zend_brk_cont_element) * CG(context).last_brk_cont);
  103. return &CG(context).brk_cont_array[CG(context).last_brk_cont-1];
  104. }
  105. static void zend_destroy_property_info_internal(zval *zv) /* {{{ */
  106. {
  107. zend_property_info *property_info = Z_PTR_P(zv);
  108. zend_string_release(property_info->name);
  109. zend_type_release(property_info->type, /* persistent */ 1);
  110. free(property_info);
  111. }
  112. /* }}} */
  113. static zend_string *zend_build_runtime_definition_key(zend_string *name, uint32_t start_lineno) /* {{{ */
  114. {
  115. zend_string *filename = CG(active_op_array)->filename;
  116. zend_string *result = zend_strpprintf(0, "%c%s%s:%" PRIu32 "$%" PRIx32,
  117. '\0', ZSTR_VAL(name), ZSTR_VAL(filename), start_lineno, CG(rtd_key_counter)++);
  118. return zend_new_interned_string(result);
  119. }
  120. /* }}} */
  121. static zend_bool zend_get_unqualified_name(const zend_string *name, const char **result, size_t *result_len) /* {{{ */
  122. {
  123. const char *ns_separator = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
  124. if (ns_separator != NULL) {
  125. *result = ns_separator + 1;
  126. *result_len = ZSTR_VAL(name) + ZSTR_LEN(name) - *result;
  127. return 1;
  128. }
  129. return 0;
  130. }
  131. /* }}} */
  132. struct reserved_class_name {
  133. const char *name;
  134. size_t len;
  135. };
  136. static const struct reserved_class_name reserved_class_names[] = {
  137. {ZEND_STRL("bool")},
  138. {ZEND_STRL("false")},
  139. {ZEND_STRL("float")},
  140. {ZEND_STRL("int")},
  141. {ZEND_STRL("null")},
  142. {ZEND_STRL("parent")},
  143. {ZEND_STRL("self")},
  144. {ZEND_STRL("static")},
  145. {ZEND_STRL("string")},
  146. {ZEND_STRL("true")},
  147. {ZEND_STRL("void")},
  148. {ZEND_STRL("iterable")},
  149. {ZEND_STRL("object")},
  150. {NULL, 0}
  151. };
  152. static zend_bool zend_is_reserved_class_name(const zend_string *name) /* {{{ */
  153. {
  154. const struct reserved_class_name *reserved = reserved_class_names;
  155. const char *uqname = ZSTR_VAL(name);
  156. size_t uqname_len = ZSTR_LEN(name);
  157. zend_get_unqualified_name(name, &uqname, &uqname_len);
  158. for (; reserved->name; ++reserved) {
  159. if (uqname_len == reserved->len
  160. && zend_binary_strcasecmp(uqname, uqname_len, reserved->name, reserved->len) == 0
  161. ) {
  162. return 1;
  163. }
  164. }
  165. return 0;
  166. }
  167. /* }}} */
  168. void zend_assert_valid_class_name(const zend_string *name) /* {{{ */
  169. {
  170. if (zend_is_reserved_class_name(name)) {
  171. zend_error_noreturn(E_COMPILE_ERROR,
  172. "Cannot use '%s' as class name as it is reserved", ZSTR_VAL(name));
  173. }
  174. }
  175. /* }}} */
  176. typedef struct _builtin_type_info {
  177. const char* name;
  178. const size_t name_len;
  179. const zend_uchar type;
  180. } builtin_type_info;
  181. static const builtin_type_info builtin_types[] = {
  182. {ZEND_STRL("null"), IS_NULL},
  183. {ZEND_STRL("false"), IS_FALSE},
  184. {ZEND_STRL("int"), IS_LONG},
  185. {ZEND_STRL("float"), IS_DOUBLE},
  186. {ZEND_STRL("string"), IS_STRING},
  187. {ZEND_STRL("bool"), _IS_BOOL},
  188. {ZEND_STRL("void"), IS_VOID},
  189. {ZEND_STRL("iterable"), IS_ITERABLE},
  190. {ZEND_STRL("object"), IS_OBJECT},
  191. {NULL, 0, IS_UNDEF}
  192. };
  193. typedef struct {
  194. const char *name;
  195. size_t name_len;
  196. const char *correct_name;
  197. } confusable_type_info;
  198. static const confusable_type_info confusable_types[] = {
  199. {ZEND_STRL("boolean"), "bool"},
  200. {ZEND_STRL("integer"), "int"},
  201. {ZEND_STRL("double"), "float"},
  202. {ZEND_STRL("resource"), NULL},
  203. {NULL, 0, NULL},
  204. };
  205. static zend_always_inline zend_uchar zend_lookup_builtin_type_by_name(const zend_string *name) /* {{{ */
  206. {
  207. const builtin_type_info *info = &builtin_types[0];
  208. for (; info->name; ++info) {
  209. if (ZSTR_LEN(name) == info->name_len
  210. && zend_binary_strcasecmp(ZSTR_VAL(name), ZSTR_LEN(name), info->name, info->name_len) == 0
  211. ) {
  212. return info->type;
  213. }
  214. }
  215. return 0;
  216. }
  217. /* }}} */
  218. static zend_always_inline zend_bool zend_is_confusable_type(const zend_string *name, const char **correct_name) /* {{{ */
  219. {
  220. const confusable_type_info *info = confusable_types;
  221. /* Intentionally using case-sensitive comparison here, because "integer" is likely intended
  222. * as a scalar type, while "Integer" is likely a class type. */
  223. for (; info->name; ++info) {
  224. if (ZSTR_LEN(name) == info->name_len
  225. && memcmp(ZSTR_VAL(name), info->name, info->name_len) == 0
  226. ) {
  227. *correct_name = info->correct_name;
  228. return 1;
  229. }
  230. }
  231. return 0;
  232. }
  233. /* }}} */
  234. static zend_bool zend_is_not_imported(zend_string *name) {
  235. /* Assuming "name" is unqualified here. */
  236. return !FC(imports) || zend_hash_find_ptr_lc(FC(imports), name) == NULL;
  237. }
  238. void zend_oparray_context_begin(zend_oparray_context *prev_context) /* {{{ */
  239. {
  240. *prev_context = CG(context);
  241. CG(context).opcodes_size = INITIAL_OP_ARRAY_SIZE;
  242. CG(context).vars_size = 0;
  243. CG(context).literals_size = 0;
  244. CG(context).fast_call_var = -1;
  245. CG(context).try_catch_offset = -1;
  246. CG(context).current_brk_cont = -1;
  247. CG(context).last_brk_cont = 0;
  248. CG(context).brk_cont_array = NULL;
  249. CG(context).labels = NULL;
  250. }
  251. /* }}} */
  252. void zend_oparray_context_end(zend_oparray_context *prev_context) /* {{{ */
  253. {
  254. if (CG(context).brk_cont_array) {
  255. efree(CG(context).brk_cont_array);
  256. CG(context).brk_cont_array = NULL;
  257. }
  258. if (CG(context).labels) {
  259. zend_hash_destroy(CG(context).labels);
  260. FREE_HASHTABLE(CG(context).labels);
  261. CG(context).labels = NULL;
  262. }
  263. CG(context) = *prev_context;
  264. }
  265. /* }}} */
  266. static void zend_reset_import_tables(void) /* {{{ */
  267. {
  268. if (FC(imports)) {
  269. zend_hash_destroy(FC(imports));
  270. efree(FC(imports));
  271. FC(imports) = NULL;
  272. }
  273. if (FC(imports_function)) {
  274. zend_hash_destroy(FC(imports_function));
  275. efree(FC(imports_function));
  276. FC(imports_function) = NULL;
  277. }
  278. if (FC(imports_const)) {
  279. zend_hash_destroy(FC(imports_const));
  280. efree(FC(imports_const));
  281. FC(imports_const) = NULL;
  282. }
  283. }
  284. /* }}} */
  285. static void zend_end_namespace(void) /* {{{ */ {
  286. FC(in_namespace) = 0;
  287. zend_reset_import_tables();
  288. if (FC(current_namespace)) {
  289. zend_string_release_ex(FC(current_namespace), 0);
  290. FC(current_namespace) = NULL;
  291. }
  292. }
  293. /* }}} */
  294. void zend_file_context_begin(zend_file_context *prev_context) /* {{{ */
  295. {
  296. *prev_context = CG(file_context);
  297. FC(imports) = NULL;
  298. FC(imports_function) = NULL;
  299. FC(imports_const) = NULL;
  300. FC(current_namespace) = NULL;
  301. FC(in_namespace) = 0;
  302. FC(has_bracketed_namespaces) = 0;
  303. FC(declarables).ticks = 0;
  304. zend_hash_init(&FC(seen_symbols), 8, NULL, NULL, 0);
  305. }
  306. /* }}} */
  307. void zend_file_context_end(zend_file_context *prev_context) /* {{{ */
  308. {
  309. zend_end_namespace();
  310. zend_hash_destroy(&FC(seen_symbols));
  311. CG(file_context) = *prev_context;
  312. }
  313. /* }}} */
  314. void zend_init_compiler_data_structures(void) /* {{{ */
  315. {
  316. zend_stack_init(&CG(loop_var_stack), sizeof(zend_loop_var));
  317. zend_stack_init(&CG(delayed_oplines_stack), sizeof(zend_op));
  318. CG(active_class_entry) = NULL;
  319. CG(in_compilation) = 0;
  320. CG(skip_shebang) = 0;
  321. CG(encoding_declared) = 0;
  322. CG(memoized_exprs) = NULL;
  323. CG(memoize_mode) = 0;
  324. }
  325. /* }}} */
  326. static void zend_register_seen_symbol(zend_string *name, uint32_t kind) {
  327. zval *zv = zend_hash_find(&FC(seen_symbols), name);
  328. if (zv) {
  329. Z_LVAL_P(zv) |= kind;
  330. } else {
  331. zval tmp;
  332. ZVAL_LONG(&tmp, kind);
  333. zend_hash_add_new(&FC(seen_symbols), name, &tmp);
  334. }
  335. }
  336. static zend_bool zend_have_seen_symbol(zend_string *name, uint32_t kind) {
  337. zval *zv = zend_hash_find(&FC(seen_symbols), name);
  338. return zv && (Z_LVAL_P(zv) & kind) != 0;
  339. }
  340. ZEND_API void file_handle_dtor(zend_file_handle *fh) /* {{{ */
  341. {
  342. zend_file_handle_dtor(fh);
  343. }
  344. /* }}} */
  345. void init_compiler(void) /* {{{ */
  346. {
  347. CG(arena) = zend_arena_create(64 * 1024);
  348. CG(active_op_array) = NULL;
  349. memset(&CG(context), 0, sizeof(CG(context)));
  350. zend_init_compiler_data_structures();
  351. zend_init_rsrc_list();
  352. zend_hash_init(&CG(filenames_table), 8, NULL, ZVAL_PTR_DTOR, 0);
  353. zend_llist_init(&CG(open_files), sizeof(zend_file_handle), (void (*)(void *)) file_handle_dtor, 0);
  354. CG(unclean_shutdown) = 0;
  355. CG(delayed_variance_obligations) = NULL;
  356. CG(delayed_autoloads) = NULL;
  357. }
  358. /* }}} */
  359. void shutdown_compiler(void) /* {{{ */
  360. {
  361. zend_stack_destroy(&CG(loop_var_stack));
  362. zend_stack_destroy(&CG(delayed_oplines_stack));
  363. zend_hash_destroy(&CG(filenames_table));
  364. zend_arena_destroy(CG(arena));
  365. if (CG(delayed_variance_obligations)) {
  366. zend_hash_destroy(CG(delayed_variance_obligations));
  367. FREE_HASHTABLE(CG(delayed_variance_obligations));
  368. CG(delayed_variance_obligations) = NULL;
  369. }
  370. if (CG(delayed_autoloads)) {
  371. zend_hash_destroy(CG(delayed_autoloads));
  372. FREE_HASHTABLE(CG(delayed_autoloads));
  373. CG(delayed_autoloads) = NULL;
  374. }
  375. }
  376. /* }}} */
  377. ZEND_API zend_string *zend_set_compiled_filename(zend_string *new_compiled_filename) /* {{{ */
  378. {
  379. zval *p, rv;
  380. if ((p = zend_hash_find(&CG(filenames_table), new_compiled_filename))) {
  381. ZEND_ASSERT(Z_TYPE_P(p) == IS_STRING);
  382. CG(compiled_filename) = Z_STR_P(p);
  383. return Z_STR_P(p);
  384. }
  385. new_compiled_filename = zend_new_interned_string(zend_string_copy(new_compiled_filename));
  386. ZVAL_STR(&rv, new_compiled_filename);
  387. zend_hash_add_new(&CG(filenames_table), new_compiled_filename, &rv);
  388. CG(compiled_filename) = new_compiled_filename;
  389. return new_compiled_filename;
  390. }
  391. /* }}} */
  392. ZEND_API void zend_restore_compiled_filename(zend_string *original_compiled_filename) /* {{{ */
  393. {
  394. CG(compiled_filename) = original_compiled_filename;
  395. }
  396. /* }}} */
  397. ZEND_API zend_string *zend_get_compiled_filename(void) /* {{{ */
  398. {
  399. return CG(compiled_filename);
  400. }
  401. /* }}} */
  402. ZEND_API int zend_get_compiled_lineno(void) /* {{{ */
  403. {
  404. return CG(zend_lineno);
  405. }
  406. /* }}} */
  407. ZEND_API zend_bool zend_is_compiling(void) /* {{{ */
  408. {
  409. return CG(in_compilation);
  410. }
  411. /* }}} */
  412. static zend_always_inline uint32_t get_temporary_variable(void) /* {{{ */
  413. {
  414. return (uint32_t)CG(active_op_array)->T++;
  415. }
  416. /* }}} */
  417. static int lookup_cv(zend_string *name) /* {{{ */{
  418. zend_op_array *op_array = CG(active_op_array);
  419. int i = 0;
  420. zend_ulong hash_value = zend_string_hash_val(name);
  421. while (i < op_array->last_var) {
  422. if (ZSTR_H(op_array->vars[i]) == hash_value
  423. && zend_string_equals(op_array->vars[i], name)) {
  424. return EX_NUM_TO_VAR(i);
  425. }
  426. i++;
  427. }
  428. i = op_array->last_var;
  429. op_array->last_var++;
  430. if (op_array->last_var > CG(context).vars_size) {
  431. CG(context).vars_size += 16; /* FIXME */
  432. op_array->vars = erealloc(op_array->vars, CG(context).vars_size * sizeof(zend_string*));
  433. }
  434. op_array->vars[i] = zend_string_copy(name);
  435. return EX_NUM_TO_VAR(i);
  436. }
  437. /* }}} */
  438. static inline zend_string *zval_make_interned_string(zval *zv) /* {{{ */
  439. {
  440. ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
  441. Z_STR_P(zv) = zend_new_interned_string(Z_STR_P(zv));
  442. if (ZSTR_IS_INTERNED(Z_STR_P(zv))) {
  443. Z_TYPE_FLAGS_P(zv) = 0;
  444. }
  445. return Z_STR_P(zv);
  446. }
  447. /* Common part of zend_add_literal and zend_append_individual_literal */
  448. static inline void zend_insert_literal(zend_op_array *op_array, zval *zv, int literal_position) /* {{{ */
  449. {
  450. zval *lit = CT_CONSTANT_EX(op_array, literal_position);
  451. if (Z_TYPE_P(zv) == IS_STRING) {
  452. zval_make_interned_string(zv);
  453. }
  454. ZVAL_COPY_VALUE(lit, zv);
  455. Z_EXTRA_P(lit) = 0;
  456. }
  457. /* }}} */
  458. /* Is used while compiling a function, using the context to keep track
  459. of an approximate size to avoid to relocate to often.
  460. Literals are truncated to actual size in the second compiler pass (pass_two()). */
  461. static int zend_add_literal(zval *zv) /* {{{ */
  462. {
  463. zend_op_array *op_array = CG(active_op_array);
  464. int i = op_array->last_literal;
  465. op_array->last_literal++;
  466. if (i >= CG(context).literals_size) {
  467. while (i >= CG(context).literals_size) {
  468. CG(context).literals_size += 16; /* FIXME */
  469. }
  470. op_array->literals = (zval*)erealloc(op_array->literals, CG(context).literals_size * sizeof(zval));
  471. }
  472. zend_insert_literal(op_array, zv, i);
  473. return i;
  474. }
  475. /* }}} */
  476. static inline int zend_add_literal_string(zend_string **str) /* {{{ */
  477. {
  478. int ret;
  479. zval zv;
  480. ZVAL_STR(&zv, *str);
  481. ret = zend_add_literal(&zv);
  482. *str = Z_STR(zv);
  483. return ret;
  484. }
  485. /* }}} */
  486. static int zend_add_func_name_literal(zend_string *name) /* {{{ */
  487. {
  488. /* Original name */
  489. int ret = zend_add_literal_string(&name);
  490. /* Lowercased name */
  491. zend_string *lc_name = zend_string_tolower(name);
  492. zend_add_literal_string(&lc_name);
  493. return ret;
  494. }
  495. /* }}} */
  496. static int zend_add_ns_func_name_literal(zend_string *name) /* {{{ */
  497. {
  498. const char *unqualified_name;
  499. size_t unqualified_name_len;
  500. /* Original name */
  501. int ret = zend_add_literal_string(&name);
  502. /* Lowercased name */
  503. zend_string *lc_name = zend_string_tolower(name);
  504. zend_add_literal_string(&lc_name);
  505. /* Lowercased unqualfied name */
  506. if (zend_get_unqualified_name(name, &unqualified_name, &unqualified_name_len)) {
  507. lc_name = zend_string_alloc(unqualified_name_len, 0);
  508. zend_str_tolower_copy(ZSTR_VAL(lc_name), unqualified_name, unqualified_name_len);
  509. zend_add_literal_string(&lc_name);
  510. }
  511. return ret;
  512. }
  513. /* }}} */
  514. static int zend_add_class_name_literal(zend_string *name) /* {{{ */
  515. {
  516. /* Original name */
  517. int ret = zend_add_literal_string(&name);
  518. /* Lowercased name */
  519. zend_string *lc_name = zend_string_tolower(name);
  520. zend_add_literal_string(&lc_name);
  521. return ret;
  522. }
  523. /* }}} */
  524. static int zend_add_const_name_literal(zend_string *name, zend_bool unqualified) /* {{{ */
  525. {
  526. zend_string *tmp_name;
  527. int ret = zend_add_literal_string(&name);
  528. size_t ns_len = 0, after_ns_len = ZSTR_LEN(name);
  529. const char *after_ns = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
  530. if (after_ns) {
  531. after_ns += 1;
  532. ns_len = after_ns - ZSTR_VAL(name) - 1;
  533. after_ns_len = ZSTR_LEN(name) - ns_len - 1;
  534. /* lowercased namespace name & original constant name */
  535. tmp_name = zend_string_init(ZSTR_VAL(name), ZSTR_LEN(name), 0);
  536. zend_str_tolower(ZSTR_VAL(tmp_name), ns_len);
  537. zend_add_literal_string(&tmp_name);
  538. if (!unqualified) {
  539. return ret;
  540. }
  541. } else {
  542. after_ns = ZSTR_VAL(name);
  543. }
  544. /* original unqualified constant name */
  545. tmp_name = zend_string_init(after_ns, after_ns_len, 0);
  546. zend_add_literal_string(&tmp_name);
  547. return ret;
  548. }
  549. /* }}} */
  550. #define LITERAL_STR(op, str) do { \
  551. zval _c; \
  552. ZVAL_STR(&_c, str); \
  553. op.constant = zend_add_literal(&_c); \
  554. } while (0)
  555. void zend_stop_lexing(void)
  556. {
  557. if (LANG_SCNG(on_event)) {
  558. LANG_SCNG(on_event)(ON_STOP, END, 0, LANG_SCNG(on_event_context));
  559. }
  560. LANG_SCNG(yy_cursor) = LANG_SCNG(yy_limit);
  561. }
  562. static inline void zend_begin_loop(
  563. zend_uchar free_opcode, const znode *loop_var, zend_bool is_switch) /* {{{ */
  564. {
  565. zend_brk_cont_element *brk_cont_element;
  566. int parent = CG(context).current_brk_cont;
  567. zend_loop_var info = {0};
  568. CG(context).current_brk_cont = CG(context).last_brk_cont;
  569. brk_cont_element = get_next_brk_cont_element();
  570. brk_cont_element->parent = parent;
  571. brk_cont_element->is_switch = is_switch;
  572. if (loop_var && (loop_var->op_type & (IS_VAR|IS_TMP_VAR))) {
  573. uint32_t start = get_next_op_number();
  574. info.opcode = free_opcode;
  575. info.var_type = loop_var->op_type;
  576. info.var_num = loop_var->u.op.var;
  577. brk_cont_element->start = start;
  578. } else {
  579. info.opcode = ZEND_NOP;
  580. /* The start field is used to free temporary variables in case of exceptions.
  581. * We won't try to free something of we don't have loop variable. */
  582. brk_cont_element->start = -1;
  583. }
  584. zend_stack_push(&CG(loop_var_stack), &info);
  585. }
  586. /* }}} */
  587. static inline void zend_end_loop(int cont_addr, const znode *var_node) /* {{{ */
  588. {
  589. uint32_t end = get_next_op_number();
  590. zend_brk_cont_element *brk_cont_element
  591. = &CG(context).brk_cont_array[CG(context).current_brk_cont];
  592. brk_cont_element->cont = cont_addr;
  593. brk_cont_element->brk = end;
  594. CG(context).current_brk_cont = brk_cont_element->parent;
  595. zend_stack_del_top(&CG(loop_var_stack));
  596. }
  597. /* }}} */
  598. void zend_do_free(znode *op1) /* {{{ */
  599. {
  600. if (op1->op_type == IS_TMP_VAR) {
  601. zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
  602. while (opline->opcode == ZEND_END_SILENCE ||
  603. opline->opcode == ZEND_OP_DATA) {
  604. opline--;
  605. }
  606. if (opline->result_type == IS_TMP_VAR && opline->result.var == op1->u.op.var) {
  607. switch (opline->opcode) {
  608. case ZEND_BOOL:
  609. case ZEND_BOOL_NOT:
  610. /* boolean resuls don't have to be freed */
  611. return;
  612. case ZEND_POST_INC_STATIC_PROP:
  613. case ZEND_POST_DEC_STATIC_PROP:
  614. case ZEND_POST_INC_OBJ:
  615. case ZEND_POST_DEC_OBJ:
  616. case ZEND_POST_INC:
  617. case ZEND_POST_DEC:
  618. /* convert $i++ to ++$i */
  619. opline->opcode -= 2;
  620. opline->result_type = IS_UNUSED;
  621. return;
  622. case ZEND_ASSIGN:
  623. case ZEND_ASSIGN_DIM:
  624. case ZEND_ASSIGN_OBJ:
  625. case ZEND_ASSIGN_STATIC_PROP:
  626. case ZEND_ASSIGN_OP:
  627. case ZEND_ASSIGN_DIM_OP:
  628. case ZEND_ASSIGN_OBJ_OP:
  629. case ZEND_ASSIGN_STATIC_PROP_OP:
  630. case ZEND_PRE_INC_STATIC_PROP:
  631. case ZEND_PRE_DEC_STATIC_PROP:
  632. case ZEND_PRE_INC_OBJ:
  633. case ZEND_PRE_DEC_OBJ:
  634. case ZEND_PRE_INC:
  635. case ZEND_PRE_DEC:
  636. opline->result_type = IS_UNUSED;
  637. return;
  638. }
  639. }
  640. zend_emit_op(NULL, ZEND_FREE, op1, NULL);
  641. } else if (op1->op_type == IS_VAR) {
  642. zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
  643. while (opline->opcode == ZEND_END_SILENCE ||
  644. opline->opcode == ZEND_EXT_FCALL_END ||
  645. opline->opcode == ZEND_OP_DATA) {
  646. opline--;
  647. }
  648. if (opline->result_type == IS_VAR
  649. && opline->result.var == op1->u.op.var) {
  650. if (opline->opcode == ZEND_FETCH_THIS) {
  651. opline->opcode = ZEND_NOP;
  652. opline->result_type = IS_UNUSED;
  653. } else {
  654. opline->result_type = IS_UNUSED;
  655. }
  656. } else {
  657. while (opline >= CG(active_op_array)->opcodes) {
  658. if ((opline->opcode == ZEND_FETCH_LIST_R ||
  659. opline->opcode == ZEND_FETCH_LIST_W) &&
  660. opline->op1_type == IS_VAR &&
  661. opline->op1.var == op1->u.op.var) {
  662. zend_emit_op(NULL, ZEND_FREE, op1, NULL);
  663. return;
  664. }
  665. if (opline->result_type == IS_VAR
  666. && opline->result.var == op1->u.op.var) {
  667. if (opline->opcode == ZEND_NEW) {
  668. zend_emit_op(NULL, ZEND_FREE, op1, NULL);
  669. }
  670. break;
  671. }
  672. opline--;
  673. }
  674. }
  675. } else if (op1->op_type == IS_CONST) {
  676. /* Destroy value without using GC: When opcache moves arrays into SHM it will
  677. * free the zend_array structure, so references to it from outside the op array
  678. * become invalid. GC would cause such a reference in the root buffer. */
  679. zval_ptr_dtor_nogc(&op1->u.constant);
  680. }
  681. }
  682. /* }}} */
  683. uint32_t zend_add_class_modifier(uint32_t flags, uint32_t new_flag) /* {{{ */
  684. {
  685. uint32_t new_flags = flags | new_flag;
  686. if ((flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flag & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) {
  687. zend_throw_exception(zend_ce_compile_error,
  688. "Multiple abstract modifiers are not allowed", 0);
  689. return 0;
  690. }
  691. if ((flags & ZEND_ACC_FINAL) && (new_flag & ZEND_ACC_FINAL)) {
  692. zend_throw_exception(zend_ce_compile_error, "Multiple final modifiers are not allowed", 0);
  693. return 0;
  694. }
  695. if ((new_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flags & ZEND_ACC_FINAL)) {
  696. zend_throw_exception(zend_ce_compile_error,
  697. "Cannot use the final modifier on an abstract class", 0);
  698. return 0;
  699. }
  700. return new_flags;
  701. }
  702. /* }}} */
  703. uint32_t zend_add_member_modifier(uint32_t flags, uint32_t new_flag) /* {{{ */
  704. {
  705. uint32_t new_flags = flags | new_flag;
  706. if ((flags & ZEND_ACC_PPP_MASK) && (new_flag & ZEND_ACC_PPP_MASK)) {
  707. zend_throw_exception(zend_ce_compile_error,
  708. "Multiple access type modifiers are not allowed", 0);
  709. return 0;
  710. }
  711. if ((flags & ZEND_ACC_ABSTRACT) && (new_flag & ZEND_ACC_ABSTRACT)) {
  712. zend_throw_exception(zend_ce_compile_error, "Multiple abstract modifiers are not allowed", 0);
  713. return 0;
  714. }
  715. if ((flags & ZEND_ACC_STATIC) && (new_flag & ZEND_ACC_STATIC)) {
  716. zend_throw_exception(zend_ce_compile_error, "Multiple static modifiers are not allowed", 0);
  717. return 0;
  718. }
  719. if ((flags & ZEND_ACC_FINAL) && (new_flag & ZEND_ACC_FINAL)) {
  720. zend_throw_exception(zend_ce_compile_error, "Multiple final modifiers are not allowed", 0);
  721. return 0;
  722. }
  723. if ((new_flags & ZEND_ACC_ABSTRACT) && (new_flags & ZEND_ACC_FINAL)) {
  724. zend_throw_exception(zend_ce_compile_error,
  725. "Cannot use the final modifier on an abstract class member", 0);
  726. return 0;
  727. }
  728. return new_flags;
  729. }
  730. /* }}} */
  731. ZEND_API zend_string *zend_create_member_string(zend_string *class_name, zend_string *member_name) {
  732. return zend_string_concat3(
  733. ZSTR_VAL(class_name), ZSTR_LEN(class_name),
  734. "::", sizeof("::") - 1,
  735. ZSTR_VAL(member_name), ZSTR_LEN(member_name));
  736. }
  737. zend_string *zend_concat_names(char *name1, size_t name1_len, char *name2, size_t name2_len) {
  738. return zend_string_concat3(name1, name1_len, "\\", 1, name2, name2_len);
  739. }
  740. zend_string *zend_prefix_with_ns(zend_string *name) {
  741. if (FC(current_namespace)) {
  742. zend_string *ns = FC(current_namespace);
  743. return zend_concat_names(ZSTR_VAL(ns), ZSTR_LEN(ns), ZSTR_VAL(name), ZSTR_LEN(name));
  744. } else {
  745. return zend_string_copy(name);
  746. }
  747. }
  748. zend_string *zend_resolve_non_class_name(
  749. zend_string *name, uint32_t type, zend_bool *is_fully_qualified,
  750. zend_bool case_sensitive, HashTable *current_import_sub
  751. ) {
  752. char *compound;
  753. *is_fully_qualified = 0;
  754. if (ZSTR_VAL(name)[0] == '\\') {
  755. /* Remove \ prefix (only relevant if this is a string rather than a label) */
  756. *is_fully_qualified = 1;
  757. return zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0);
  758. }
  759. if (type == ZEND_NAME_FQ) {
  760. *is_fully_qualified = 1;
  761. return zend_string_copy(name);
  762. }
  763. if (type == ZEND_NAME_RELATIVE) {
  764. *is_fully_qualified = 1;
  765. return zend_prefix_with_ns(name);
  766. }
  767. if (current_import_sub) {
  768. /* If an unqualified name is a function/const alias, replace it. */
  769. zend_string *import_name;
  770. if (case_sensitive) {
  771. import_name = zend_hash_find_ptr(current_import_sub, name);
  772. } else {
  773. import_name = zend_hash_find_ptr_lc(current_import_sub, name);
  774. }
  775. if (import_name) {
  776. *is_fully_qualified = 1;
  777. return zend_string_copy(import_name);
  778. }
  779. }
  780. compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
  781. if (compound) {
  782. *is_fully_qualified = 1;
  783. }
  784. if (compound && FC(imports)) {
  785. /* If the first part of a qualified name is an alias, substitute it. */
  786. size_t len = compound - ZSTR_VAL(name);
  787. zend_string *import_name = zend_hash_str_find_ptr_lc(FC(imports), ZSTR_VAL(name), len);
  788. if (import_name) {
  789. return zend_concat_names(
  790. ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1);
  791. }
  792. }
  793. return zend_prefix_with_ns(name);
  794. }
  795. /* }}} */
  796. zend_string *zend_resolve_function_name(zend_string *name, uint32_t type, zend_bool *is_fully_qualified) /* {{{ */
  797. {
  798. return zend_resolve_non_class_name(
  799. name, type, is_fully_qualified, 0, FC(imports_function));
  800. }
  801. /* }}} */
  802. zend_string *zend_resolve_const_name(zend_string *name, uint32_t type, zend_bool *is_fully_qualified) /* {{{ */ {
  803. return zend_resolve_non_class_name(
  804. name, type, is_fully_qualified, 1, FC(imports_const));
  805. }
  806. /* }}} */
  807. zend_string *zend_resolve_class_name(zend_string *name, uint32_t type) /* {{{ */
  808. {
  809. char *compound;
  810. if (type == ZEND_NAME_RELATIVE) {
  811. return zend_prefix_with_ns(name);
  812. }
  813. if (type == ZEND_NAME_FQ || ZSTR_VAL(name)[0] == '\\') {
  814. /* Remove \ prefix (only relevant if this is a string rather than a label) */
  815. if (ZSTR_VAL(name)[0] == '\\') {
  816. name = zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0);
  817. } else {
  818. zend_string_addref(name);
  819. }
  820. /* Ensure that \self, \parent and \static are not used */
  821. if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) {
  822. zend_error_noreturn(E_COMPILE_ERROR, "'\\%s' is an invalid class name", ZSTR_VAL(name));
  823. }
  824. return name;
  825. }
  826. if (FC(imports)) {
  827. compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
  828. if (compound) {
  829. /* If the first part of a qualified name is an alias, substitute it. */
  830. size_t len = compound - ZSTR_VAL(name);
  831. zend_string *import_name =
  832. zend_hash_str_find_ptr_lc(FC(imports), ZSTR_VAL(name), len);
  833. if (import_name) {
  834. return zend_concat_names(
  835. ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1);
  836. }
  837. } else {
  838. /* If an unqualified name is an alias, replace it. */
  839. zend_string *import_name
  840. = zend_hash_find_ptr_lc(FC(imports), name);
  841. if (import_name) {
  842. return zend_string_copy(import_name);
  843. }
  844. }
  845. }
  846. /* If not fully qualified and not an alias, prepend the current namespace */
  847. return zend_prefix_with_ns(name);
  848. }
  849. /* }}} */
  850. zend_string *zend_resolve_class_name_ast(zend_ast *ast) /* {{{ */
  851. {
  852. zval *class_name = zend_ast_get_zval(ast);
  853. if (Z_TYPE_P(class_name) != IS_STRING) {
  854. zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
  855. }
  856. return zend_resolve_class_name(Z_STR_P(class_name), ast->attr);
  857. }
  858. /* }}} */
  859. static void label_ptr_dtor(zval *zv) /* {{{ */
  860. {
  861. efree_size(Z_PTR_P(zv), sizeof(zend_label));
  862. }
  863. /* }}} */
  864. static void str_dtor(zval *zv) /* {{{ */ {
  865. zend_string_release_ex(Z_STR_P(zv), 0);
  866. }
  867. /* }}} */
  868. static zend_bool zend_is_call(zend_ast *ast);
  869. static uint32_t zend_add_try_element(uint32_t try_op) /* {{{ */
  870. {
  871. zend_op_array *op_array = CG(active_op_array);
  872. uint32_t try_catch_offset = op_array->last_try_catch++;
  873. zend_try_catch_element *elem;
  874. op_array->try_catch_array = safe_erealloc(
  875. op_array->try_catch_array, sizeof(zend_try_catch_element), op_array->last_try_catch, 0);
  876. elem = &op_array->try_catch_array[try_catch_offset];
  877. elem->try_op = try_op;
  878. elem->catch_op = 0;
  879. elem->finally_op = 0;
  880. elem->finally_end = 0;
  881. return try_catch_offset;
  882. }
  883. /* }}} */
  884. ZEND_API void function_add_ref(zend_function *function) /* {{{ */
  885. {
  886. if (function->type == ZEND_USER_FUNCTION) {
  887. zend_op_array *op_array = &function->op_array;
  888. if (op_array->refcount) {
  889. (*op_array->refcount)++;
  890. }
  891. if (op_array->static_variables
  892. && !(GC_FLAGS(op_array->static_variables) & IS_ARRAY_IMMUTABLE)) {
  893. GC_ADDREF(op_array->static_variables);
  894. }
  895. if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) {
  896. ZEND_ASSERT(op_array->fn_flags & ZEND_ACC_PRELOADED);
  897. ZEND_MAP_PTR_NEW(op_array->run_time_cache);
  898. ZEND_MAP_PTR_NEW(op_array->static_variables_ptr);
  899. } else {
  900. ZEND_MAP_PTR_INIT(op_array->static_variables_ptr, &op_array->static_variables);
  901. ZEND_MAP_PTR_INIT(op_array->run_time_cache, zend_arena_alloc(&CG(arena), sizeof(void*)));
  902. ZEND_MAP_PTR_SET(op_array->run_time_cache, NULL);
  903. }
  904. }
  905. if (function->common.function_name) {
  906. zend_string_addref(function->common.function_name);
  907. }
  908. }
  909. /* }}} */
  910. static zend_never_inline ZEND_COLD ZEND_NORETURN void do_bind_function_error(zend_string *lcname, zend_op_array *op_array, zend_bool compile_time) /* {{{ */
  911. {
  912. zval *zv = zend_hash_find_ex(compile_time ? CG(function_table) : EG(function_table), lcname, 1);
  913. int error_level = compile_time ? E_COMPILE_ERROR : E_ERROR;
  914. zend_function *old_function;
  915. ZEND_ASSERT(zv != NULL);
  916. old_function = (zend_function*)Z_PTR_P(zv);
  917. if (old_function->type == ZEND_USER_FUNCTION
  918. && old_function->op_array.last > 0) {
  919. zend_error_noreturn(error_level, "Cannot redeclare %s() (previously declared in %s:%d)",
  920. op_array ? ZSTR_VAL(op_array->function_name) : ZSTR_VAL(old_function->common.function_name),
  921. ZSTR_VAL(old_function->op_array.filename),
  922. old_function->op_array.opcodes[0].lineno);
  923. } else {
  924. zend_error_noreturn(error_level, "Cannot redeclare %s()",
  925. op_array ? ZSTR_VAL(op_array->function_name) : ZSTR_VAL(old_function->common.function_name));
  926. }
  927. }
  928. ZEND_API int do_bind_function(zval *lcname) /* {{{ */
  929. {
  930. zend_function *function;
  931. zval *rtd_key, *zv;
  932. rtd_key = lcname + 1;
  933. zv = zend_hash_find_ex(EG(function_table), Z_STR_P(rtd_key), 1);
  934. if (UNEXPECTED(!zv)) {
  935. do_bind_function_error(Z_STR_P(lcname), NULL, 0);
  936. return FAILURE;
  937. }
  938. function = (zend_function*)Z_PTR_P(zv);
  939. zv = zend_hash_set_bucket_key(EG(function_table), (Bucket*)zv, Z_STR_P(lcname));
  940. if (UNEXPECTED(!zv)) {
  941. do_bind_function_error(Z_STR_P(lcname), &function->op_array, 0);
  942. return FAILURE;
  943. }
  944. return SUCCESS;
  945. }
  946. /* }}} */
  947. ZEND_API int do_bind_class(zval *lcname, zend_string *lc_parent_name) /* {{{ */
  948. {
  949. zend_class_entry *ce;
  950. zval *rtd_key, *zv;
  951. rtd_key = lcname + 1;
  952. zv = zend_hash_find_ex(EG(class_table), Z_STR_P(rtd_key), 1);
  953. if (UNEXPECTED(!zv)) {
  954. ce = zend_hash_find_ptr(EG(class_table), Z_STR_P(lcname));
  955. if (ce) {
  956. zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare %s %s, because the name is already in use", zend_get_object_type(ce), ZSTR_VAL(ce->name));
  957. return FAILURE;
  958. } else {
  959. do {
  960. ZEND_ASSERT(EG(current_execute_data)->func->op_array.fn_flags & ZEND_ACC_PRELOADED);
  961. if (zend_preload_autoload
  962. && zend_preload_autoload(EG(current_execute_data)->func->op_array.filename) == SUCCESS) {
  963. zv = zend_hash_find_ex(EG(class_table), Z_STR_P(rtd_key), 1);
  964. if (EXPECTED(zv != NULL)) {
  965. break;
  966. }
  967. }
  968. zend_error_noreturn(E_ERROR, "Class %s wasn't preloaded", Z_STRVAL_P(lcname));
  969. return FAILURE;
  970. } while (0);
  971. }
  972. }
  973. /* Register the derived class */
  974. ce = (zend_class_entry*)Z_PTR_P(zv);
  975. zv = zend_hash_set_bucket_key(EG(class_table), (Bucket*)zv, Z_STR_P(lcname));
  976. if (UNEXPECTED(!zv)) {
  977. zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare %s %s, because the name is already in use", zend_get_object_type(ce), ZSTR_VAL(ce->name));
  978. return FAILURE;
  979. }
  980. if (zend_do_link_class(ce, lc_parent_name) == FAILURE) {
  981. /* Reload bucket pointer, the hash table may have been reallocated */
  982. zv = zend_hash_find(EG(class_table), Z_STR_P(lcname));
  983. zend_hash_set_bucket_key(EG(class_table), (Bucket *) zv, Z_STR_P(rtd_key));
  984. return FAILURE;
  985. }
  986. return SUCCESS;
  987. }
  988. /* }}} */
  989. static zend_string *add_type_string(zend_string *type, zend_string *new_type) {
  990. zend_string *result;
  991. if (type == NULL) {
  992. return zend_string_copy(new_type);
  993. }
  994. result = zend_string_concat3(
  995. ZSTR_VAL(type), ZSTR_LEN(type), "|", 1, ZSTR_VAL(new_type), ZSTR_LEN(new_type));
  996. zend_string_release(type);
  997. return result;
  998. }
  999. static zend_string *resolve_class_name(zend_string *name, zend_class_entry *scope) {
  1000. if (scope) {
  1001. if (zend_string_equals_literal_ci(name, "self")) {
  1002. name = scope->name;
  1003. } else if (zend_string_equals_literal_ci(name, "parent") && scope->parent) {
  1004. name = scope->parent->name;
  1005. }
  1006. }
  1007. return name;
  1008. }
  1009. zend_string *zend_type_to_string_resolved(zend_type type, zend_class_entry *scope) {
  1010. zend_string *str = NULL;
  1011. if (ZEND_TYPE_HAS_LIST(type)) {
  1012. zend_type *list_type;
  1013. ZEND_TYPE_LIST_FOREACH(ZEND_TYPE_LIST(type), list_type) {
  1014. if (ZEND_TYPE_HAS_CE(*list_type)) {
  1015. str = add_type_string(str, ZEND_TYPE_CE(*list_type)->name);
  1016. } else {
  1017. str = add_type_string(str, resolve_class_name(ZEND_TYPE_NAME(*list_type), scope));
  1018. }
  1019. } ZEND_TYPE_LIST_FOREACH_END();
  1020. } else if (ZEND_TYPE_HAS_NAME(type)) {
  1021. str = zend_string_copy(resolve_class_name(ZEND_TYPE_NAME(type), scope));
  1022. } else if (ZEND_TYPE_HAS_CE(type)) {
  1023. str = zend_string_copy(ZEND_TYPE_CE(type)->name);
  1024. }
  1025. uint32_t type_mask = ZEND_TYPE_FULL_MASK(type);
  1026. if (type_mask & MAY_BE_STATIC) {
  1027. zend_string *name = ZSTR_KNOWN(ZEND_STR_STATIC);
  1028. if (scope) {
  1029. zend_class_entry *called_scope = zend_get_called_scope(EG(current_execute_data));
  1030. if (called_scope) {
  1031. name = called_scope->name;
  1032. }
  1033. }
  1034. str = add_type_string(str, name);
  1035. }
  1036. if (type_mask & MAY_BE_CALLABLE) {
  1037. str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_CALLABLE));
  1038. }
  1039. if (type_mask & MAY_BE_ITERABLE) {
  1040. str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_ITERABLE));
  1041. }
  1042. if (type_mask & MAY_BE_OBJECT) {
  1043. str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_OBJECT));
  1044. }
  1045. if (type_mask & MAY_BE_ARRAY) {
  1046. str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_ARRAY));
  1047. }
  1048. if (type_mask & MAY_BE_STRING) {
  1049. str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_STRING));
  1050. }
  1051. if (type_mask & MAY_BE_LONG) {
  1052. str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_INT));
  1053. }
  1054. if (type_mask & MAY_BE_DOUBLE) {
  1055. str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_FLOAT));
  1056. }
  1057. if ((type_mask & MAY_BE_BOOL) == MAY_BE_BOOL) {
  1058. str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_BOOL));
  1059. } else if (type_mask & MAY_BE_FALSE) {
  1060. str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_FALSE));
  1061. }
  1062. if (type_mask & MAY_BE_VOID) {
  1063. str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_VOID));
  1064. }
  1065. if (type_mask & MAY_BE_NULL) {
  1066. zend_bool is_union = !str || memchr(ZSTR_VAL(str), '|', ZSTR_LEN(str)) != NULL;
  1067. if (!is_union) {
  1068. zend_string *nullable_str = zend_string_concat2("?", 1, ZSTR_VAL(str), ZSTR_LEN(str));
  1069. zend_string_release(str);
  1070. return nullable_str;
  1071. }
  1072. str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_NULL_LOWERCASE));
  1073. }
  1074. return str;
  1075. }
  1076. ZEND_API zend_string *zend_type_to_string(zend_type type) {
  1077. return zend_type_to_string_resolved(type, NULL);
  1078. }
  1079. static zend_bool is_generator_compatible_class_type(zend_string *name) {
  1080. return zend_string_equals_literal_ci(name, "Traversable")
  1081. || zend_string_equals_literal_ci(name, "Iterator")
  1082. || zend_string_equals_literal_ci(name, "Generator");
  1083. }
  1084. static void zend_mark_function_as_generator() /* {{{ */
  1085. {
  1086. if (!CG(active_op_array)->function_name) {
  1087. zend_error_noreturn(E_COMPILE_ERROR,
  1088. "The \"yield\" expression can only be used inside a function");
  1089. }
  1090. if (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
  1091. zend_type return_type = CG(active_op_array)->arg_info[-1].type;
  1092. zend_bool valid_type = (ZEND_TYPE_FULL_MASK(return_type) & MAY_BE_ITERABLE) != 0;
  1093. if (!valid_type) {
  1094. zend_type *single_type;
  1095. ZEND_TYPE_FOREACH(return_type, single_type) {
  1096. if (ZEND_TYPE_HAS_NAME(*single_type)
  1097. && is_generator_compatible_class_type(ZEND_TYPE_NAME(*single_type))) {
  1098. valid_type = 1;
  1099. break;
  1100. }
  1101. } ZEND_TYPE_FOREACH_END();
  1102. }
  1103. if (!valid_type) {
  1104. zend_string *str = zend_type_to_string(return_type);
  1105. zend_error_noreturn(E_COMPILE_ERROR,
  1106. "Generators may only declare a return type containing " \
  1107. "Generator, Iterator, Traversable, or iterable, %s is not permitted",
  1108. ZSTR_VAL(str));
  1109. }
  1110. }
  1111. CG(active_op_array)->fn_flags |= ZEND_ACC_GENERATOR;
  1112. }
  1113. /* }}} */
  1114. ZEND_API uint32_t zend_build_delayed_early_binding_list(const zend_op_array *op_array) /* {{{ */
  1115. {
  1116. if (op_array->fn_flags & ZEND_ACC_EARLY_BINDING) {
  1117. uint32_t first_early_binding_opline = (uint32_t)-1;
  1118. uint32_t *prev_opline_num = &first_early_binding_opline;
  1119. zend_op *opline = op_array->opcodes;
  1120. zend_op *end = opline + op_array->last;
  1121. while (opline < end) {
  1122. if (opline->opcode == ZEND_DECLARE_CLASS_DELAYED) {
  1123. *prev_opline_num = opline - op_array->opcodes;
  1124. prev_opline_num = &opline->result.opline_num;
  1125. }
  1126. ++opline;
  1127. }
  1128. *prev_opline_num = -1;
  1129. return first_early_binding_opline;
  1130. }
  1131. return (uint32_t)-1;
  1132. }
  1133. /* }}} */
  1134. ZEND_API void zend_do_delayed_early_binding(zend_op_array *op_array, uint32_t first_early_binding_opline) /* {{{ */
  1135. {
  1136. if (first_early_binding_opline != (uint32_t)-1) {
  1137. zend_bool orig_in_compilation = CG(in_compilation);
  1138. uint32_t opline_num = first_early_binding_opline;
  1139. void **run_time_cache;
  1140. if (!ZEND_MAP_PTR(op_array->run_time_cache)) {
  1141. void *ptr;
  1142. ZEND_ASSERT(op_array->fn_flags & ZEND_ACC_HEAP_RT_CACHE);
  1143. ptr = emalloc(op_array->cache_size + sizeof(void*));
  1144. ZEND_MAP_PTR_INIT(op_array->run_time_cache, ptr);
  1145. ptr = (char*)ptr + sizeof(void*);
  1146. ZEND_MAP_PTR_SET(op_array->run_time_cache, ptr);
  1147. memset(ptr, 0, op_array->cache_size);
  1148. }
  1149. run_time_cache = RUN_TIME_CACHE(op_array);
  1150. CG(in_compilation) = 1;
  1151. while (opline_num != (uint32_t)-1) {
  1152. const zend_op *opline = &op_array->opcodes[opline_num];
  1153. zval *lcname = RT_CONSTANT(opline, opline->op1);
  1154. zval *zv = zend_hash_find_ex(EG(class_table), Z_STR_P(lcname + 1), 1);
  1155. if (zv) {
  1156. zend_class_entry *ce = Z_CE_P(zv);
  1157. zend_string *lc_parent_name = Z_STR_P(RT_CONSTANT(opline, opline->op2));
  1158. zend_class_entry *parent_ce = zend_hash_find_ex_ptr(EG(class_table), lc_parent_name, 1);
  1159. if (parent_ce) {
  1160. if (zend_try_early_bind(ce, parent_ce, Z_STR_P(lcname), zv)) {
  1161. /* Store in run-time cache */
  1162. ((void**)((char*)run_time_cache + opline->extended_value))[0] = ce;
  1163. }
  1164. }
  1165. }
  1166. opline_num = op_array->opcodes[opline_num].result.opline_num;
  1167. }
  1168. CG(in_compilation) = orig_in_compilation;
  1169. }
  1170. }
  1171. /* }}} */
  1172. ZEND_API zend_string *zend_mangle_property_name(const char *src1, size_t src1_length, const char *src2, size_t src2_length, int internal) /* {{{ */
  1173. {
  1174. size_t prop_name_length = 1 + src1_length + 1 + src2_length;
  1175. zend_string *prop_name = zend_string_alloc(prop_name_length, internal);
  1176. ZSTR_VAL(prop_name)[0] = '\0';
  1177. memcpy(ZSTR_VAL(prop_name) + 1, src1, src1_length+1);
  1178. memcpy(ZSTR_VAL(prop_name) + 1 + src1_length + 1, src2, src2_length+1);
  1179. return prop_name;
  1180. }
  1181. /* }}} */
  1182. static zend_always_inline size_t zend_strnlen(const char* s, size_t maxlen) /* {{{ */
  1183. {
  1184. size_t len = 0;
  1185. while (*s++ && maxlen--) len++;
  1186. return len;
  1187. }
  1188. /* }}} */
  1189. ZEND_API int zend_unmangle_property_name_ex(const zend_string *name, const char **class_name, const char **prop_name, size_t *prop_len) /* {{{ */
  1190. {
  1191. size_t class_name_len;
  1192. size_t anonclass_src_len;
  1193. *class_name = NULL;
  1194. if (!ZSTR_LEN(name) || ZSTR_VAL(name)[0] != '\0') {
  1195. *prop_name = ZSTR_VAL(name);
  1196. if (prop_len) {
  1197. *prop_len = ZSTR_LEN(name);
  1198. }
  1199. return SUCCESS;
  1200. }
  1201. if (ZSTR_LEN(name) < 3 || ZSTR_VAL(name)[1] == '\0') {
  1202. zend_error(E_NOTICE, "Illegal member variable name");
  1203. *prop_name = ZSTR_VAL(name);
  1204. if (prop_len) {
  1205. *prop_len = ZSTR_LEN(name);
  1206. }
  1207. return FAILURE;
  1208. }
  1209. class_name_len = zend_strnlen(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 2);
  1210. if (class_name_len >= ZSTR_LEN(name) - 2 || ZSTR_VAL(name)[class_name_len + 1] != '\0') {
  1211. zend_error(E_NOTICE, "Corrupt member variable name");
  1212. *prop_name = ZSTR_VAL(name);
  1213. if (prop_len) {
  1214. *prop_len = ZSTR_LEN(name);
  1215. }
  1216. return FAILURE;
  1217. }
  1218. *class_name = ZSTR_VAL(name) + 1;
  1219. anonclass_src_len = zend_strnlen(*class_name + class_name_len + 1, ZSTR_LEN(name) - class_name_len - 2);
  1220. if (class_name_len + anonclass_src_len + 2 != ZSTR_LEN(name)) {
  1221. class_name_len += anonclass_src_len + 1;
  1222. }
  1223. *prop_name = ZSTR_VAL(name) + class_name_len + 2;
  1224. if (prop_len) {
  1225. *prop_len = ZSTR_LEN(name) - class_name_len - 2;
  1226. }
  1227. return SUCCESS;
  1228. }
  1229. /* }}} */
  1230. static zend_bool can_ct_eval_const(zend_constant *c) {
  1231. if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
  1232. return 0;
  1233. }
  1234. if ((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)
  1235. && !(CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION)
  1236. && !((ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE)
  1237. && (CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE))) {
  1238. return 1;
  1239. }
  1240. if (Z_TYPE(c->value) < IS_OBJECT
  1241. && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
  1242. return 1;
  1243. }
  1244. return 0;
  1245. }
  1246. static zend_bool zend_try_ct_eval_const(zval *zv, zend_string *name, zend_bool is_fully_qualified) /* {{{ */
  1247. {
  1248. zend_constant *c = zend_hash_find_ptr(EG(zend_constants), name);
  1249. if (c && can_ct_eval_const(c)) {
  1250. ZVAL_COPY_OR_DUP(zv, &c->value);
  1251. return 1;
  1252. }
  1253. {
  1254. /* Substitute true, false and null (including unqualified usage in namespaces) */
  1255. const char *lookup_name = ZSTR_VAL(name);
  1256. size_t lookup_len = ZSTR_LEN(name);
  1257. if (!is_fully_qualified) {
  1258. zend_get_unqualified_name(name, &lookup_name, &lookup_len);
  1259. }
  1260. if ((c = zend_get_special_const(lookup_name, lookup_len))) {
  1261. ZVAL_COPY_VALUE(zv, &c->value);
  1262. return 1;
  1263. }
  1264. return 0;
  1265. }
  1266. }
  1267. /* }}} */
  1268. static inline zend_bool zend_is_scope_known() /* {{{ */
  1269. {
  1270. if (CG(active_op_array)->fn_flags & ZEND_ACC_CLOSURE) {
  1271. /* Closures can be rebound to a different scope */
  1272. return 0;
  1273. }
  1274. if (!CG(active_class_entry)) {
  1275. /* The scope is known if we're in a free function (no scope), but not if we're in
  1276. * a file/eval (which inherits including/eval'ing scope). */
  1277. return CG(active_op_array)->function_name != NULL;
  1278. }
  1279. /* For traits self etc refers to the using class, not the trait itself */
  1280. return (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) == 0;
  1281. }
  1282. /* }}} */
  1283. static inline zend_bool class_name_refers_to_active_ce(zend_string *class_name, uint32_t fetch_type) /* {{{ */
  1284. {
  1285. if (!CG(active_class_entry)) {
  1286. return 0;
  1287. }
  1288. if (fetch_type == ZEND_FETCH_CLASS_SELF && zend_is_scope_known()) {
  1289. return 1;
  1290. }
  1291. return fetch_type == ZEND_FETCH_CLASS_DEFAULT
  1292. && zend_string_equals_ci(class_name, CG(active_class_entry)->name);
  1293. }
  1294. /* }}} */
  1295. uint32_t zend_get_class_fetch_type(zend_string *name) /* {{{ */
  1296. {
  1297. if (zend_string_equals_literal_ci(name, "self")) {
  1298. return ZEND_FETCH_CLASS_SELF;
  1299. } else if (zend_string_equals_literal_ci(name, "parent")) {
  1300. return ZEND_FETCH_CLASS_PARENT;
  1301. } else if (zend_string_equals_literal_ci(name, "static")) {
  1302. return ZEND_FETCH_CLASS_STATIC;
  1303. } else {
  1304. return ZEND_FETCH_CLASS_DEFAULT;
  1305. }
  1306. }
  1307. /* }}} */
  1308. static uint32_t zend_get_class_fetch_type_ast(zend_ast *name_ast) /* {{{ */
  1309. {
  1310. /* Fully qualified names are always default refs */
  1311. if (name_ast->attr == ZEND_NAME_FQ) {
  1312. return ZEND_FETCH_CLASS_DEFAULT;
  1313. }
  1314. return zend_get_class_fetch_type(zend_ast_get_str(name_ast));
  1315. }
  1316. /* }}} */
  1317. static zend_string *zend_resolve_const_class_name_reference(zend_ast *ast, const char *type)
  1318. {
  1319. zend_string *class_name = zend_ast_get_str(ast);
  1320. if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type_ast(ast)) {
  1321. zend_error_noreturn(E_COMPILE_ERROR,
  1322. "Cannot use '%s' as %s, as it is reserved",
  1323. ZSTR_VAL(class_name), type);
  1324. }
  1325. return zend_resolve_class_name(class_name, ast->attr);
  1326. }
  1327. static void zend_ensure_valid_class_fetch_type(uint32_t fetch_type) /* {{{ */
  1328. {
  1329. if (fetch_type != ZEND_FETCH_CLASS_DEFAULT && zend_is_scope_known()) {
  1330. zend_class_entry *ce = CG(active_class_entry);
  1331. if (!ce) {
  1332. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"%s\" when no class scope is active",
  1333. fetch_type == ZEND_FETCH_CLASS_SELF ? "self" :
  1334. fetch_type == ZEND_FETCH_CLASS_PARENT ? "parent" : "static");
  1335. } else if (fetch_type == ZEND_FETCH_CLASS_PARENT && !ce->parent_name) {
  1336. zend_error_noreturn(E_COMPILE_ERROR,
  1337. "Cannot use \"parent\" when current class scope has no parent");
  1338. }
  1339. }
  1340. }
  1341. /* }}} */
  1342. static zend_bool zend_try_compile_const_expr_resolve_class_name(zval *zv, zend_ast *class_ast) /* {{{ */
  1343. {
  1344. uint32_t fetch_type;
  1345. zval *class_name;
  1346. if (class_ast->kind != ZEND_AST_ZVAL) {
  1347. return 0;
  1348. }
  1349. class_name = zend_ast_get_zval(class_ast);
  1350. if (Z_TYPE_P(class_name) != IS_STRING) {
  1351. zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
  1352. }
  1353. fetch_type = zend_get_class_fetch_type(Z_STR_P(class_name));
  1354. zend_ensure_valid_class_fetch_type(fetch_type);
  1355. switch (fetch_type) {
  1356. case ZEND_FETCH_CLASS_SELF:
  1357. if (CG(active_class_entry) && zend_is_scope_known()) {
  1358. ZVAL_STR_COPY(zv, CG(active_class_entry)->name);
  1359. return 1;
  1360. }
  1361. return 0;
  1362. case ZEND_FETCH_CLASS_PARENT:
  1363. if (CG(active_class_entry) && CG(active_class_entry)->parent_name
  1364. && zend_is_scope_known()) {
  1365. ZVAL_STR_COPY(zv, CG(active_class_entry)->parent_name);
  1366. return 1;
  1367. }
  1368. return 0;
  1369. case ZEND_FETCH_CLASS_STATIC:
  1370. return 0;
  1371. case ZEND_FETCH_CLASS_DEFAULT:
  1372. ZVAL_STR(zv, zend_resolve_class_name_ast(class_ast));
  1373. return 1;
  1374. EMPTY_SWITCH_DEFAULT_CASE()
  1375. }
  1376. }
  1377. /* }}} */
  1378. /* We don't use zend_verify_const_access because we need to deal with unlinked classes. */
  1379. static zend_bool zend_verify_ct_const_access(zend_class_constant *c, zend_class_entry *scope)
  1380. {
  1381. if (Z_ACCESS_FLAGS(c->value) & ZEND_ACC_PUBLIC) {
  1382. return 1;
  1383. } else if (Z_ACCESS_FLAGS(c->value) & ZEND_ACC_PRIVATE) {
  1384. return c->ce == scope;
  1385. } else {
  1386. zend_class_entry *ce = c->ce;
  1387. while (1) {
  1388. if (ce == scope) {
  1389. return 1;
  1390. }
  1391. if (!ce->parent) {
  1392. break;
  1393. }
  1394. if (ce->ce_flags & ZEND_ACC_RESOLVED_PARENT) {
  1395. ce = ce->parent;
  1396. } else {
  1397. ce = zend_hash_find_ptr_lc(CG(class_table), ce->parent_name);
  1398. if (!ce) {
  1399. break;
  1400. }
  1401. }
  1402. }
  1403. /* Reverse case cannot be true during compilation */
  1404. return 0;
  1405. }
  1406. }
  1407. static zend_bool zend_try_ct_eval_class_const(zval *zv, zend_string *class_name, zend_string *name) /* {{{ */
  1408. {
  1409. uint32_t fetch_type = zend_get_class_fetch_type(class_name);
  1410. zend_class_constant *cc;
  1411. zval *c;
  1412. if (class_name_refers_to_active_ce(class_name, fetch_type)) {
  1413. cc = zend_hash_find_ptr(&CG(active_class_entry)->constants_table, name);
  1414. } else if (fetch_type == ZEND_FETCH_CLASS_DEFAULT && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
  1415. zend_class_entry *ce = zend_hash_find_ptr_lc(CG(class_table), class_name);
  1416. if (ce) {
  1417. cc = zend_hash_find_ptr(&ce->constants_table, name);
  1418. } else {
  1419. return 0;
  1420. }
  1421. } else {
  1422. return 0;
  1423. }
  1424. if (CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION) {
  1425. return 0;
  1426. }
  1427. if (!cc || !zend_verify_ct_const_access(cc, CG(active_class_entry))) {
  1428. return 0;
  1429. }
  1430. c = &cc->value;
  1431. /* Substitute case-sensitive (or lowercase) persistent class constants */
  1432. if (Z_TYPE_P(c) < IS_OBJECT) {
  1433. ZVAL_COPY_OR_DUP(zv, c);
  1434. return 1;
  1435. }
  1436. return 0;
  1437. }
  1438. /* }}} */
  1439. static void zend_add_to_list(void *result, void *item) /* {{{ */
  1440. {
  1441. void** list = *(void**)result;
  1442. size_t n = 0;
  1443. if (list) {
  1444. while (list[n]) {
  1445. n++;
  1446. }
  1447. }
  1448. list = erealloc(list, sizeof(void*) * (n+2));
  1449. list[n] = item;
  1450. list[n+1] = NULL;
  1451. *(void**)result = list;
  1452. }
  1453. /* }}} */
  1454. void zend_do_extended_stmt(void) /* {{{ */
  1455. {
  1456. zend_op *opline;
  1457. if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT)) {
  1458. return;
  1459. }
  1460. opline = get_next_op();
  1461. opline->opcode = ZEND_EXT_STMT;
  1462. }
  1463. /* }}} */
  1464. void zend_do_extended_fcall_begin(void) /* {{{ */
  1465. {
  1466. zend_op *opline;
  1467. if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_FCALL)) {
  1468. return;
  1469. }
  1470. opline = get_next_op();
  1471. opline->opcode = ZEND_EXT_FCALL_BEGIN;
  1472. }
  1473. /* }}} */
  1474. void zend_do_extended_fcall_end(void) /* {{{ */
  1475. {
  1476. zend_op *opline;
  1477. if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_FCALL)) {
  1478. return;
  1479. }
  1480. opline = get_next_op();
  1481. opline->opcode = ZEND_EXT_FCALL_END;
  1482. }
  1483. /* }}} */
  1484. zend_bool zend_is_auto_global_str(char *name, size_t len) /* {{{ */ {
  1485. zend_auto_global *auto_global;
  1486. if ((auto_global = zend_hash_str_find_ptr(CG(auto_globals), name, len)) != NULL) {
  1487. if (auto_global->armed) {
  1488. auto_global->armed = auto_global->auto_global_callback(auto_global->name);
  1489. }
  1490. return 1;
  1491. }
  1492. return 0;
  1493. }
  1494. /* }}} */
  1495. zend_bool zend_is_auto_global(zend_string *name) /* {{{ */
  1496. {
  1497. zend_auto_global *auto_global;
  1498. if ((auto_global = zend_hash_find_ptr(CG(auto_globals), name)) != NULL) {
  1499. if (auto_global->armed) {
  1500. auto_global->armed = auto_global->auto_global_callback(auto_global->name);
  1501. }
  1502. return 1;
  1503. }
  1504. return 0;
  1505. }
  1506. /* }}} */
  1507. int zend_register_auto_global(zend_string *name, zend_bool jit, zend_auto_global_callback auto_global_callback) /* {{{ */
  1508. {
  1509. zend_auto_global auto_global;
  1510. int retval;
  1511. auto_global.name = name;
  1512. auto_global.auto_global_callback = auto_global_callback;
  1513. auto_global.jit = jit;
  1514. retval = zend_hash_add_mem(CG(auto_globals), auto_global.name, &auto_global, sizeof(zend_auto_global)) != NULL ? SUCCESS : FAILURE;
  1515. return retval;
  1516. }
  1517. /* }}} */
  1518. ZEND_API void zend_activate_auto_globals(void) /* {{{ */
  1519. {
  1520. zend_auto_global *auto_global;
  1521. ZEND_HASH_FOREACH_PTR(CG(auto_globals), auto_global) {
  1522. if (auto_global->jit) {
  1523. auto_global->armed = 1;
  1524. } else if (auto_global->auto_global_callback) {
  1525. auto_global->armed = auto_global->auto_global_callback(auto_global->name);
  1526. } else {
  1527. auto_global->armed = 0;
  1528. }
  1529. } ZEND_HASH_FOREACH_END();
  1530. }
  1531. /* }}} */
  1532. int ZEND_FASTCALL zendlex(zend_parser_stack_elem *elem) /* {{{ */
  1533. {
  1534. zval zv;
  1535. int ret;
  1536. if (CG(increment_lineno)) {
  1537. CG(zend_lineno)++;
  1538. CG(increment_lineno) = 0;
  1539. }
  1540. ret = lex_scan(&zv, elem);
  1541. ZEND_ASSERT(!EG(exception) || ret == T_ERROR);
  1542. return ret;
  1543. }
  1544. /* }}} */
  1545. ZEND_API void zend_initialize_class_data(zend_class_entry *ce, zend_bool nullify_handlers) /* {{{ */
  1546. {
  1547. zend_bool persistent_hashes = ce->type == ZEND_INTERNAL_CLASS;
  1548. ce->refcount = 1;
  1549. ce->ce_flags = ZEND_ACC_CONSTANTS_UPDATED;
  1550. if (CG(compiler_options) & ZEND_COMPILE_GUARDS) {
  1551. ce->ce_flags |= ZEND_ACC_USE_GUARDS;
  1552. }
  1553. ce->default_properties_table = NULL;
  1554. ce->default_static_members_table = NULL;
  1555. zend_hash_init_ex(&ce->properties_info, 8, NULL, (persistent_hashes ? zend_destroy_property_info_internal : NULL), persistent_hashes, 0);
  1556. zend_hash_init_ex(&ce->constants_table, 8, NULL, NULL, persistent_hashes, 0);
  1557. zend_hash_init_ex(&ce->function_table, 8, NULL, ZEND_FUNCTION_DTOR, persistent_hashes, 0);
  1558. if (ce->type == ZEND_INTERNAL_CLASS) {
  1559. ZEND_MAP_PTR_INIT(ce->static_members_table, NULL);
  1560. } else {
  1561. ZEND_MAP_PTR_INIT(ce->static_members_table, &ce->default_static_members_table);
  1562. ce->info.user.doc_comment = NULL;
  1563. }
  1564. ce->default_properties_count = 0;
  1565. ce->default_static_members_count = 0;
  1566. ce->properties_info_table = NULL;
  1567. if (nullify_handlers) {
  1568. ce->constructor = NULL;
  1569. ce->destructor = NULL;
  1570. ce->clone = NULL;
  1571. ce->__get = NULL;
  1572. ce->__set = NULL;
  1573. ce->__unset = NULL;
  1574. ce->__isset = NULL;
  1575. ce->__call = NULL;
  1576. ce->__callstatic = NULL;
  1577. ce->__tostring = NULL;
  1578. ce->create_object = NULL;
  1579. ce->get_iterator = NULL;
  1580. ce->iterator_funcs_ptr = NULL;
  1581. ce->get_static_method = NULL;
  1582. ce->parent = NULL;
  1583. ce->parent_name = NULL;
  1584. ce->num_interfaces = 0;
  1585. ce->interfaces = NULL;
  1586. ce->num_traits = 0;
  1587. ce->trait_names = NULL;
  1588. ce->trait_aliases = NULL;
  1589. ce->trait_precedences = NULL;
  1590. ce->serialize = NULL;
  1591. ce->unserialize = NULL;
  1592. ce->serialize_func = NULL;
  1593. ce->unserialize_func = NULL;
  1594. ce->__debugInfo = NULL;
  1595. if (ce->type == ZEND_INTERNAL_CLASS) {
  1596. ce->info.internal.module = NULL;
  1597. ce->info.internal.builtin_functions = NULL;
  1598. }
  1599. }
  1600. }
  1601. /* }}} */
  1602. ZEND_API zend_string *zend_get_compiled_variable_name(const zend_op_array *op_array, uint32_t var) /* {{{ */
  1603. {
  1604. return op_array->vars[EX_VAR_TO_NUM(var)];
  1605. }
  1606. /* }}} */
  1607. zend_ast *zend_ast_append_str(zend_ast *left_ast, zend_ast *right_ast) /* {{{ */
  1608. {
  1609. zval *left_zv = zend_ast_get_zval(left_ast);
  1610. zend_string *left = Z_STR_P(left_zv);
  1611. zend_string *right = zend_ast_get_str(right_ast);
  1612. zend_string *result;
  1613. size_t left_len = ZSTR_LEN(left);
  1614. size_t len = left_len + ZSTR_LEN(right) + 1; /* left\right */
  1615. result = zend_string_extend(left, len, 0);
  1616. ZSTR_VAL(result)[left_len] = '\\';
  1617. memcpy(&ZSTR_VAL(result)[left_len + 1], ZSTR_VAL(right), ZSTR_LEN(right));
  1618. ZSTR_VAL(result)[len] = '\0';
  1619. zend_string_release_ex(right, 0);
  1620. ZVAL_STR(left_zv, result);
  1621. return left_ast;
  1622. }
  1623. /* }}} */
  1624. zend_ast *zend_negate_num_string(zend_ast *ast) /* {{{ */
  1625. {
  1626. zval *zv = zend_ast_get_zval(ast);
  1627. if (Z_TYPE_P(zv) == IS_LONG) {
  1628. if (Z_LVAL_P(zv) == 0) {
  1629. ZVAL_NEW_STR(zv, zend_string_init("-0", sizeof("-0")-1, 0));
  1630. } else {
  1631. ZEND_ASSERT(Z_LVAL_P(zv) > 0);
  1632. Z_LVAL_P(zv) *= -1;
  1633. }
  1634. } else if (Z_TYPE_P(zv) == IS_STRING) {
  1635. size_t orig_len = Z_STRLEN_P(zv);
  1636. Z_STR_P(zv) = zend_string_extend(Z_STR_P(zv), orig_len + 1, 0);
  1637. memmove(Z_STRVAL_P(zv) + 1, Z_STRVAL_P(zv), orig_len + 1);
  1638. Z_STRVAL_P(zv)[0] = '-';
  1639. } else {
  1640. ZEND_ASSERT(0);
  1641. }
  1642. return ast;
  1643. }
  1644. /* }}} */
  1645. void zend_verify_namespace(void) /* {{{ */
  1646. {
  1647. if (FC(has_bracketed_namespaces) && !FC(in_namespace)) {
  1648. zend_error_noreturn(E_COMPILE_ERROR, "No code may exist outside of namespace {}");
  1649. }
  1650. }
  1651. /* }}} */
  1652. /* {{{ zend_dirname
  1653. Returns directory name component of path */
  1654. ZEND_API size_t zend_dirname(char *path, size_t len)
  1655. {
  1656. register char *end = path + len - 1;
  1657. unsigned int len_adjust = 0;
  1658. #ifdef ZEND_WIN32
  1659. /* Note that on Win32 CWD is per drive (heritage from CP/M).
  1660. * This means dirname("c:foo") maps to "c:." or "c:" - which means CWD on C: drive.
  1661. */
  1662. if ((2 <= len) && isalpha((int)((unsigned char *)path)[0]) && (':' == path[1])) {
  1663. /* Skip over the drive spec (if any) so as not to change */
  1664. path += 2;
  1665. len_adjust += 2;
  1666. if (2 == len) {
  1667. /* Return "c:" on Win32 for dirname("c:").
  1668. * It would be more consistent to return "c:."
  1669. * but that would require making the string *longer*.
  1670. */
  1671. return len;
  1672. }
  1673. }
  1674. #endif
  1675. if (len == 0) {
  1676. /* Illegal use of this function */
  1677. return 0;
  1678. }
  1679. /* Strip trailing slashes */
  1680. while (end >= path && IS_SLASH_P(end)) {
  1681. end--;
  1682. }
  1683. if (end < path) {
  1684. /* The path only contained slashes */
  1685. path[0] = DEFAULT_SLASH;
  1686. path[1] = '\0';
  1687. return 1 + len_adjust;
  1688. }
  1689. /* Strip filename */
  1690. while (end >= path && !IS_SLASH_P(end)) {
  1691. end--;
  1692. }
  1693. if (end < path) {
  1694. /* No slash found, therefore return '.' */
  1695. path[0] = '.';
  1696. path[1] = '\0';
  1697. return 1 + len_adjust;
  1698. }
  1699. /* Strip slashes which came before the file name */
  1700. while (end >= path && IS_SLASH_P(end)) {
  1701. end--;
  1702. }
  1703. if (end < path) {
  1704. path[0] = DEFAULT_SLASH;
  1705. path[1] = '\0';
  1706. return 1 + len_adjust;
  1707. }
  1708. *(end+1) = '\0';
  1709. return (size_t)(end + 1 - path) + len_adjust;
  1710. }
  1711. /* }}} */
  1712. static void zend_adjust_for_fetch_type(zend_op *opline, znode *result, uint32_t type) /* {{{ */
  1713. {
  1714. zend_uchar factor = (opline->opcode == ZEND_FETCH_STATIC_PROP_R) ? 1 : 3;
  1715. switch (type) {
  1716. case BP_VAR_R:
  1717. opline->result_type = IS_TMP_VAR;
  1718. result->op_type = IS_TMP_VAR;
  1719. return;
  1720. case BP_VAR_W:
  1721. opline->opcode += 1 * factor;
  1722. return;
  1723. case BP_VAR_RW:
  1724. opline->opcode += 2 * factor;
  1725. return;
  1726. case BP_VAR_IS:
  1727. opline->result_type = IS_TMP_VAR;
  1728. result->op_type = IS_TMP_VAR;
  1729. opline->opcode += 3 * factor;
  1730. return;
  1731. case BP_VAR_FUNC_ARG:
  1732. opline->opcode += 4 * factor;
  1733. return;
  1734. case BP_VAR_UNSET:
  1735. opline->opcode += 5 * factor;
  1736. return;
  1737. EMPTY_SWITCH_DEFAULT_CASE()
  1738. }
  1739. }
  1740. /* }}} */
  1741. static inline void zend_make_var_result(znode *result, zend_op *opline) /* {{{ */
  1742. {
  1743. opline->result_type = IS_VAR;
  1744. opline->result.var = get_temporary_variable();
  1745. GET_NODE(result, opline->result);
  1746. }
  1747. /* }}} */
  1748. static inline void zend_make_tmp_result(znode *result, zend_op *opline) /* {{{ */
  1749. {
  1750. opline->result_type = IS_TMP_VAR;
  1751. opline->result.var = get_temporary_variable();
  1752. GET_NODE(result, opline->result);
  1753. }
  1754. /* }}} */
  1755. static zend_op *zend_emit_op(znode *result, zend_uchar opcode, znode *op1, znode *op2) /* {{{ */
  1756. {
  1757. zend_op *opline = get_next_op();
  1758. opline->opcode = opcode;
  1759. if (op1 != NULL) {
  1760. SET_NODE(opline->op1, op1);
  1761. }
  1762. if (op2 != NULL) {
  1763. SET_NODE(opline->op2, op2);
  1764. }
  1765. if (result) {
  1766. zend_make_var_result(result, opline);
  1767. }
  1768. return opline;
  1769. }
  1770. /* }}} */
  1771. static zend_op *zend_emit_op_tmp(znode *result, zend_uchar opcode, znode *op1, znode *op2) /* {{{ */
  1772. {
  1773. zend_op *opline = get_next_op();
  1774. opline->opcode = opcode;
  1775. if (op1 != NULL) {
  1776. SET_NODE(opline->op1, op1);
  1777. }
  1778. if (op2 != NULL) {
  1779. SET_NODE(opline->op2, op2);
  1780. }
  1781. if (result) {
  1782. zend_make_tmp_result(result, opline);
  1783. }
  1784. return opline;
  1785. }
  1786. /* }}} */
  1787. static void zend_emit_tick(void) /* {{{ */
  1788. {
  1789. zend_op *opline;
  1790. /* This prevents a double TICK generated by the parser statement of "declare()" */
  1791. if (CG(active_op_array)->last && CG(active_op_array)->opcodes[CG(active_op_array)->last - 1].opcode == ZEND_TICKS) {
  1792. return;
  1793. }
  1794. opline = get_next_op();
  1795. opline->opcode = ZEND_TICKS;
  1796. opline->extended_value = FC(declarables).ticks;
  1797. }
  1798. /* }}} */
  1799. static inline zend_op *zend_emit_op_data(znode *value) /* {{{ */
  1800. {
  1801. return zend_emit_op(NULL, ZEND_OP_DATA, value, NULL);
  1802. }
  1803. /* }}} */
  1804. static inline uint32_t zend_emit_jump(uint32_t opnum_target) /* {{{ */
  1805. {
  1806. uint32_t opnum = get_next_op_number();
  1807. zend_op *opline = zend_emit_op(NULL, ZEND_JMP, NULL, NULL);
  1808. opline->op1.opline_num = opnum_target;
  1809. return opnum;
  1810. }
  1811. /* }}} */
  1812. ZEND_API int zend_is_smart_branch(const zend_op *opline) /* {{{ */
  1813. {
  1814. switch (opline->opcode) {
  1815. case ZEND_IS_IDENTICAL:
  1816. case ZEND_IS_NOT_IDENTICAL:
  1817. case ZEND_IS_EQUAL:
  1818. case ZEND_IS_NOT_EQUAL:
  1819. case ZEND_IS_SMALLER:
  1820. case ZEND_IS_SMALLER_OR_EQUAL:
  1821. case ZEND_CASE:
  1822. case ZEND_ISSET_ISEMPTY_CV:
  1823. case ZEND_ISSET_ISEMPTY_VAR:
  1824. case ZEND_ISSET_ISEMPTY_DIM_OBJ:
  1825. case ZEND_ISSET_ISEMPTY_PROP_OBJ:
  1826. case ZEND_ISSET_ISEMPTY_STATIC_PROP:
  1827. case ZEND_INSTANCEOF:
  1828. case ZEND_TYPE_CHECK:
  1829. case ZEND_DEFINED:
  1830. case ZEND_IN_ARRAY:
  1831. case ZEND_ARRAY_KEY_EXISTS:
  1832. return 1;
  1833. default:
  1834. return 0;
  1835. }
  1836. }
  1837. /* }}} */
  1838. static inline uint32_t zend_emit_cond_jump(zend_uchar opcode, znode *cond, uint32_t opnum_target) /* {{{ */
  1839. {
  1840. uint32_t opnum = get_next_op_number();
  1841. zend_op *opline;
  1842. if (cond->op_type == IS_TMP_VAR && opnum > 0) {
  1843. opline = CG(active_op_array)->opcodes + opnum - 1;
  1844. if (opline->result_type == IS_TMP_VAR
  1845. && opline->result.var == cond->u.op.var
  1846. && zend_is_smart_branch(opline)) {
  1847. if (opcode == ZEND_JMPZ) {
  1848. opline->result_type = IS_TMP_VAR | IS_SMART_BRANCH_JMPZ;
  1849. } else {
  1850. ZEND_ASSERT(opcode == ZEND_JMPNZ);
  1851. opline->result_type = IS_TMP_VAR | IS_SMART_BRANCH_JMPNZ;
  1852. }
  1853. }
  1854. }
  1855. opline = zend_emit_op(NULL, opcode, cond, NULL);
  1856. opline->op2.opline_num = opnum_target;
  1857. return opnum;
  1858. }
  1859. /* }}} */
  1860. static inline void zend_update_jump_target(uint32_t opnum_jump, uint32_t opnum_target) /* {{{ */
  1861. {
  1862. zend_op *opline = &CG(active_op_array)->opcodes[opnum_jump];
  1863. switch (opline->opcode) {
  1864. case ZEND_JMP:
  1865. opline->op1.opline_num = opnum_target;
  1866. break;
  1867. case ZEND_JMPZ:
  1868. case ZEND_JMPNZ:
  1869. case ZEND_JMPZ_EX:
  1870. case ZEND_JMPNZ_EX:
  1871. case ZEND_JMP_SET:
  1872. case ZEND_COALESCE:
  1873. opline->op2.opline_num = opnum_target;
  1874. break;
  1875. EMPTY_SWITCH_DEFAULT_CASE()
  1876. }
  1877. }
  1878. /* }}} */
  1879. static inline void zend_update_jump_target_to_next(uint32_t opnum_jump) /* {{{ */
  1880. {
  1881. zend_update_jump_target(opnum_jump, get_next_op_number());
  1882. }
  1883. /* }}} */
  1884. static inline zend_op *zend_delayed_emit_op(znode *result, zend_uchar opcode, znode *op1, znode *op2) /* {{{ */
  1885. {
  1886. zend_op tmp_opline;
  1887. init_op(&tmp_opline);
  1888. tmp_opline.opcode = opcode;
  1889. if (op1 != NULL) {
  1890. SET_NODE(tmp_opline.op1, op1);
  1891. }
  1892. if (op2 != NULL) {
  1893. SET_NODE(tmp_opline.op2, op2);
  1894. }
  1895. if (result) {
  1896. zend_make_var_result(result, &tmp_opline);
  1897. }
  1898. zend_stack_push(&CG(delayed_oplines_stack), &tmp_opline);
  1899. return zend_stack_top(&CG(delayed_oplines_stack));
  1900. }
  1901. /* }}} */
  1902. static inline uint32_t zend_delayed_compile_begin(void) /* {{{ */
  1903. {
  1904. return zend_stack_count(&CG(delayed_oplines_stack));
  1905. }
  1906. /* }}} */
  1907. static zend_op *zend_delayed_compile_end(uint32_t offset) /* {{{ */
  1908. {
  1909. zend_op *opline = NULL, *oplines = zend_stack_base(&CG(delayed_oplines_stack));
  1910. uint32_t i, count = zend_stack_count(&CG(delayed_oplines_stack));
  1911. ZEND_ASSERT(count >= offset);
  1912. for (i = offset; i < count; ++i) {
  1913. opline = get_next_op();
  1914. memcpy(opline, &oplines[i], sizeof(zend_op));
  1915. }
  1916. CG(delayed_oplines_stack).top = offset;
  1917. return opline;
  1918. }
  1919. /* }}} */
  1920. #define ZEND_MEMOIZE_NONE 0
  1921. #define ZEND_MEMOIZE_COMPILE 1
  1922. #define ZEND_MEMOIZE_FETCH 2
  1923. static void zend_compile_memoized_expr(znode *result, zend_ast *expr) /* {{{ */
  1924. {
  1925. int memoize_mode = CG(memoize_mode);
  1926. if (memoize_mode == ZEND_MEMOIZE_COMPILE) {
  1927. znode memoized_result;
  1928. /* Go through normal compilation */
  1929. CG(memoize_mode) = ZEND_MEMOIZE_NONE;
  1930. zend_compile_expr(result, expr);
  1931. CG(memoize_mode) = ZEND_MEMOIZE_COMPILE;
  1932. if (result->op_type == IS_VAR) {
  1933. zend_emit_op(&memoized_result, ZEND_COPY_TMP, result, NULL);
  1934. } else if (result->op_type == IS_TMP_VAR) {
  1935. zend_emit_op_tmp(&memoized_result, ZEND_COPY_TMP, result, NULL);
  1936. } else {
  1937. if (result->op_type == IS_CONST) {
  1938. Z_TRY_ADDREF(result->u.constant);
  1939. }
  1940. memoized_result = *result;
  1941. }
  1942. zend_hash_index_update_mem(
  1943. CG(memoized_exprs), (uintptr_t) expr, &memoized_result, sizeof(znode));
  1944. } else if (memoize_mode == ZEND_MEMOIZE_FETCH) {
  1945. znode *memoized_result = zend_hash_index_find_ptr(CG(memoized_exprs), (uintptr_t) expr);
  1946. *result = *memoized_result;
  1947. if (result->op_type == IS_CONST) {
  1948. Z_TRY_ADDREF(result->u.constant);
  1949. }
  1950. } else {
  1951. ZEND_ASSERT(0);
  1952. }
  1953. }
  1954. /* }}} */
  1955. static size_t zend_type_get_num_classes(zend_type type) {
  1956. if (!ZEND_TYPE_HAS_CLASS(type)) {
  1957. return 0;
  1958. }
  1959. if (ZEND_TYPE_HAS_LIST(type)) {
  1960. return ZEND_TYPE_LIST(type)->num_types;
  1961. }
  1962. return 1;
  1963. }
  1964. static void zend_emit_return_type_check(
  1965. znode *expr, zend_arg_info *return_info, zend_bool implicit) /* {{{ */
  1966. {
  1967. zend_type type = return_info->type;
  1968. if (ZEND_TYPE_IS_SET(type)) {
  1969. zend_op *opline;
  1970. /* `return ...;` is illegal in a void function (but `return;` isn't) */
  1971. if (ZEND_TYPE_CONTAINS_CODE(type, IS_VOID)) {
  1972. if (expr) {
  1973. if (expr->op_type == IS_CONST && Z_TYPE(expr->u.constant) == IS_NULL) {
  1974. zend_error_noreturn(E_COMPILE_ERROR,
  1975. "A void function must not return a value "
  1976. "(did you mean \"return;\" instead of \"return null;\"?)");
  1977. } else {
  1978. zend_error_noreturn(E_COMPILE_ERROR, "A void function must not return a value");
  1979. }
  1980. }
  1981. /* we don't need run-time check */
  1982. return;
  1983. }
  1984. if (!expr && !implicit) {
  1985. if (ZEND_TYPE_ALLOW_NULL(type)) {
  1986. zend_error_noreturn(E_COMPILE_ERROR,
  1987. "A function with return type must return a value "
  1988. "(did you mean \"return null;\" instead of \"return;\"?)");
  1989. } else {
  1990. zend_error_noreturn(E_COMPILE_ERROR,
  1991. "A function with return type must return a value");
  1992. }
  1993. }
  1994. if (expr && expr->op_type == IS_CONST) {
  1995. if (ZEND_TYPE_CONTAINS_CODE(type, Z_TYPE(expr->u.constant))) {
  1996. /* we don't need run-time check */
  1997. return;
  1998. }
  1999. }
  2000. opline = zend_emit_op(NULL, ZEND_VERIFY_RETURN_TYPE, expr, NULL);
  2001. if (expr && expr->op_type == IS_CONST) {
  2002. opline->result_type = expr->op_type = IS_TMP_VAR;
  2003. opline->result.var = expr->u.op.var = get_temporary_variable();
  2004. }
  2005. opline->op2.num = zend_alloc_cache_slots(zend_type_get_num_classes(return_info->type));
  2006. }
  2007. }
  2008. /* }}} */
  2009. void zend_emit_final_return(int return_one) /* {{{ */
  2010. {
  2011. znode zn;
  2012. zend_op *ret;
  2013. zend_bool returns_reference = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
  2014. if (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE
  2015. && !(CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR)) {
  2016. zend_emit_return_type_check(NULL, CG(active_op_array)->arg_info - 1, 1);
  2017. }
  2018. zn.op_type = IS_CONST;
  2019. if (return_one) {
  2020. ZVAL_LONG(&zn.u.constant, 1);
  2021. } else {
  2022. ZVAL_NULL(&zn.u.constant);
  2023. }
  2024. ret = zend_emit_op(NULL, returns_reference ? ZEND_RETURN_BY_REF : ZEND_RETURN, &zn, NULL);
  2025. ret->extended_value = -1;
  2026. }
  2027. /* }}} */
  2028. static inline zend_bool zend_is_variable(zend_ast *ast) /* {{{ */
  2029. {
  2030. return ast->kind == ZEND_AST_VAR || ast->kind == ZEND_AST_DIM
  2031. || ast->kind == ZEND_AST_PROP || ast->kind == ZEND_AST_STATIC_PROP;
  2032. }
  2033. /* }}} */
  2034. static inline zend_bool zend_is_call(zend_ast *ast) /* {{{ */
  2035. {
  2036. return ast->kind == ZEND_AST_CALL
  2037. || ast->kind == ZEND_AST_METHOD_CALL
  2038. || ast->kind == ZEND_AST_STATIC_CALL;
  2039. }
  2040. /* }}} */
  2041. static inline zend_bool zend_is_variable_or_call(zend_ast *ast) /* {{{ */
  2042. {
  2043. return zend_is_variable(ast) || zend_is_call(ast);
  2044. }
  2045. /* }}} */
  2046. static inline zend_bool zend_is_unticked_stmt(zend_ast *ast) /* {{{ */
  2047. {
  2048. return ast->kind == ZEND_AST_STMT_LIST || ast->kind == ZEND_AST_LABEL
  2049. || ast->kind == ZEND_AST_PROP_DECL || ast->kind == ZEND_AST_CLASS_CONST_DECL
  2050. || ast->kind == ZEND_AST_USE_TRAIT || ast->kind == ZEND_AST_METHOD;
  2051. }
  2052. /* }}} */
  2053. static inline zend_bool zend_can_write_to_variable(zend_ast *ast) /* {{{ */
  2054. {
  2055. while (ast->kind == ZEND_AST_DIM || ast->kind == ZEND_AST_PROP) {
  2056. ast = ast->child[0];
  2057. }
  2058. return zend_is_variable_or_call(ast);
  2059. }
  2060. /* }}} */
  2061. static inline zend_bool zend_is_const_default_class_ref(zend_ast *name_ast) /* {{{ */
  2062. {
  2063. if (name_ast->kind != ZEND_AST_ZVAL) {
  2064. return 0;
  2065. }
  2066. return ZEND_FETCH_CLASS_DEFAULT == zend_get_class_fetch_type_ast(name_ast);
  2067. }
  2068. /* }}} */
  2069. static inline void zend_handle_numeric_op(znode *node) /* {{{ */
  2070. {
  2071. if (node->op_type == IS_CONST && Z_TYPE(node->u.constant) == IS_STRING) {
  2072. zend_ulong index;
  2073. if (ZEND_HANDLE_NUMERIC(Z_STR(node->u.constant), index)) {
  2074. zval_ptr_dtor(&node->u.constant);
  2075. ZVAL_LONG(&node->u.constant, index);
  2076. }
  2077. }
  2078. }
  2079. /* }}} */
  2080. static inline void zend_handle_numeric_dim(zend_op *opline, znode *dim_node) /* {{{ */
  2081. {
  2082. if (Z_TYPE(dim_node->u.constant) == IS_STRING) {
  2083. zend_ulong index;
  2084. if (ZEND_HANDLE_NUMERIC(Z_STR(dim_node->u.constant), index)) {
  2085. /* For numeric indexes we also keep the original value to use by ArrayAccess
  2086. * See bug #63217
  2087. */
  2088. int c = zend_add_literal(&dim_node->u.constant);
  2089. ZEND_ASSERT(opline->op2.constant + 1 == c);
  2090. ZVAL_LONG(CT_CONSTANT(opline->op2), index);
  2091. Z_EXTRA_P(CT_CONSTANT(opline->op2)) = ZEND_EXTRA_VALUE;
  2092. return;
  2093. }
  2094. }
  2095. }
  2096. /* }}} */
  2097. static inline void zend_set_class_name_op1(zend_op *opline, znode *class_node) /* {{{ */
  2098. {
  2099. if (class_node->op_type == IS_CONST) {
  2100. opline->op1_type = IS_CONST;
  2101. opline->op1.constant = zend_add_class_name_literal(
  2102. Z_STR(class_node->u.constant));
  2103. } else {
  2104. SET_NODE(opline->op1, class_node);
  2105. }
  2106. }
  2107. /* }}} */
  2108. static void zend_compile_class_ref(znode *result, zend_ast *name_ast, uint32_t fetch_flags) /* {{{ */
  2109. {
  2110. uint32_t fetch_type;
  2111. if (name_ast->kind != ZEND_AST_ZVAL) {
  2112. znode name_node;
  2113. zend_compile_expr(&name_node, name_ast);
  2114. if (name_node.op_type == IS_CONST) {
  2115. zend_string *name;
  2116. if (Z_TYPE(name_node.u.constant) != IS_STRING) {
  2117. zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
  2118. }
  2119. name = Z_STR(name_node.u.constant);
  2120. fetch_type = zend_get_class_fetch_type(name);
  2121. if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) {
  2122. result->op_type = IS_CONST;
  2123. ZVAL_STR(&result->u.constant, zend_resolve_class_name(name, ZEND_NAME_FQ));
  2124. } else {
  2125. zend_ensure_valid_class_fetch_type(fetch_type);
  2126. result->op_type = IS_UNUSED;
  2127. result->u.op.num = fetch_type | fetch_flags;
  2128. }
  2129. zend_string_release_ex(name, 0);
  2130. } else {
  2131. zend_op *opline = zend_emit_op(result, ZEND_FETCH_CLASS, NULL, &name_node);
  2132. opline->op1.num = ZEND_FETCH_CLASS_DEFAULT | fetch_flags;
  2133. }
  2134. return;
  2135. }
  2136. /* Fully qualified names are always default refs */
  2137. if (name_ast->attr == ZEND_NAME_FQ) {
  2138. result->op_type = IS_CONST;
  2139. ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast));
  2140. return;
  2141. }
  2142. fetch_type = zend_get_class_fetch_type(zend_ast_get_str(name_ast));
  2143. if (ZEND_FETCH_CLASS_DEFAULT == fetch_type) {
  2144. result->op_type = IS_CONST;
  2145. ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast));
  2146. } else {
  2147. zend_ensure_valid_class_fetch_type(fetch_type);
  2148. result->op_type = IS_UNUSED;
  2149. result->u.op.num = fetch_type | fetch_flags;
  2150. }
  2151. }
  2152. /* }}} */
  2153. static int zend_try_compile_cv(znode *result, zend_ast *ast) /* {{{ */
  2154. {
  2155. zend_ast *name_ast = ast->child[0];
  2156. if (name_ast->kind == ZEND_AST_ZVAL) {
  2157. zval *zv = zend_ast_get_zval(name_ast);
  2158. zend_string *name;
  2159. if (EXPECTED(Z_TYPE_P(zv) == IS_STRING)) {
  2160. name = zval_make_interned_string(zv);
  2161. } else {
  2162. name = zend_new_interned_string(zval_get_string_func(zv));
  2163. }
  2164. if (zend_is_auto_global(name)) {
  2165. return FAILURE;
  2166. }
  2167. result->op_type = IS_CV;
  2168. result->u.op.var = lookup_cv(name);
  2169. if (UNEXPECTED(Z_TYPE_P(zv) != IS_STRING)) {
  2170. zend_string_release_ex(name, 0);
  2171. }
  2172. return SUCCESS;
  2173. }
  2174. return FAILURE;
  2175. }
  2176. /* }}} */
  2177. static zend_op *zend_compile_simple_var_no_cv(znode *result, zend_ast *ast, uint32_t type, int delayed) /* {{{ */
  2178. {
  2179. zend_ast *name_ast = ast->child[0];
  2180. znode name_node;
  2181. zend_op *opline;
  2182. zend_compile_expr(&name_node, name_ast);
  2183. if (name_node.op_type == IS_CONST) {
  2184. convert_to_string(&name_node.u.constant);
  2185. }
  2186. if (delayed) {
  2187. opline = zend_delayed_emit_op(result, ZEND_FETCH_R, &name_node, NULL);
  2188. } else {
  2189. opline = zend_emit_op(result, ZEND_FETCH_R, &name_node, NULL);
  2190. }
  2191. if (name_node.op_type == IS_CONST &&
  2192. zend_is_auto_global(Z_STR(name_node.u.constant))) {
  2193. opline->extended_value = ZEND_FETCH_GLOBAL;
  2194. } else {
  2195. opline->extended_value = ZEND_FETCH_LOCAL;
  2196. }
  2197. zend_adjust_for_fetch_type(opline, result, type);
  2198. return opline;
  2199. }
  2200. /* }}} */
  2201. static zend_bool is_this_fetch(zend_ast *ast) /* {{{ */
  2202. {
  2203. if (ast->kind == ZEND_AST_VAR && ast->child[0]->kind == ZEND_AST_ZVAL) {
  2204. zval *name = zend_ast_get_zval(ast->child[0]);
  2205. return Z_TYPE_P(name) == IS_STRING && zend_string_equals_literal(Z_STR_P(name), "this");
  2206. }
  2207. return 0;
  2208. }
  2209. /* }}} */
  2210. static zend_bool this_guaranteed_exists() /* {{{ */
  2211. {
  2212. zend_op_array *op_array = CG(active_op_array);
  2213. /* Instance methods always have a $this.
  2214. * This also includes closures that have a scope and use $this. */
  2215. return op_array->scope != NULL
  2216. && (op_array->fn_flags & ZEND_ACC_STATIC) == 0;
  2217. }
  2218. /* }}} */
  2219. static zend_op *zend_compile_simple_var(znode *result, zend_ast *ast, uint32_t type, int delayed) /* {{{ */
  2220. {
  2221. if (is_this_fetch(ast)) {
  2222. zend_op *opline = zend_emit_op(result, ZEND_FETCH_THIS, NULL, NULL);
  2223. if ((type == BP_VAR_R) || (type == BP_VAR_IS)) {
  2224. opline->result_type = IS_TMP_VAR;
  2225. result->op_type = IS_TMP_VAR;
  2226. }
  2227. CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
  2228. return opline;
  2229. } else if (zend_try_compile_cv(result, ast) == FAILURE) {
  2230. return zend_compile_simple_var_no_cv(result, ast, type, delayed);
  2231. }
  2232. return NULL;
  2233. }
  2234. /* }}} */
  2235. static void zend_separate_if_call_and_write(znode *node, zend_ast *ast, uint32_t type) /* {{{ */
  2236. {
  2237. if (type != BP_VAR_R && type != BP_VAR_IS && zend_is_call(ast)) {
  2238. if (node->op_type == IS_VAR) {
  2239. zend_op *opline = zend_emit_op(NULL, ZEND_SEPARATE, node, NULL);
  2240. opline->result_type = IS_VAR;
  2241. opline->result.var = opline->op1.var;
  2242. } else {
  2243. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use result of built-in function in write context");
  2244. }
  2245. }
  2246. }
  2247. /* }}} */
  2248. zend_op *zend_delayed_compile_var(znode *result, zend_ast *ast, uint32_t type, zend_bool by_ref);
  2249. void zend_compile_assign(znode *result, zend_ast *ast);
  2250. static inline void zend_emit_assign_znode(zend_ast *var_ast, znode *value_node) /* {{{ */
  2251. {
  2252. znode dummy_node;
  2253. zend_ast *assign_ast = zend_ast_create(ZEND_AST_ASSIGN, var_ast,
  2254. zend_ast_create_znode(value_node));
  2255. zend_compile_assign(&dummy_node, assign_ast);
  2256. zend_do_free(&dummy_node);
  2257. }
  2258. /* }}} */
  2259. static zend_op *zend_delayed_compile_dim(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
  2260. {
  2261. if (ast->attr == ZEND_DIM_ALTERNATIVE_SYNTAX) {
  2262. zend_error(E_DEPRECATED, "Array and string offset access syntax with curly braces is deprecated");
  2263. }
  2264. zend_ast *var_ast = ast->child[0];
  2265. zend_ast *dim_ast = ast->child[1];
  2266. zend_op *opline;
  2267. znode var_node, dim_node;
  2268. opline = zend_delayed_compile_var(&var_node, var_ast, type, 0);
  2269. if (opline && type == BP_VAR_W && (opline->opcode == ZEND_FETCH_STATIC_PROP_W || opline->opcode == ZEND_FETCH_OBJ_W)) {
  2270. opline->extended_value |= ZEND_FETCH_DIM_WRITE;
  2271. }
  2272. zend_separate_if_call_and_write(&var_node, var_ast, type);
  2273. if (dim_ast == NULL) {
  2274. if (type == BP_VAR_R || type == BP_VAR_IS) {
  2275. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
  2276. }
  2277. if (type == BP_VAR_UNSET) {
  2278. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for unsetting");
  2279. }
  2280. dim_node.op_type = IS_UNUSED;
  2281. } else {
  2282. zend_compile_expr(&dim_node, dim_ast);
  2283. }
  2284. opline = zend_delayed_emit_op(result, ZEND_FETCH_DIM_R, &var_node, &dim_node);
  2285. zend_adjust_for_fetch_type(opline, result, type);
  2286. if (dim_node.op_type == IS_CONST) {
  2287. zend_handle_numeric_dim(opline, &dim_node);
  2288. }
  2289. return opline;
  2290. }
  2291. /* }}} */
  2292. static zend_op *zend_compile_dim(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
  2293. {
  2294. uint32_t offset = zend_delayed_compile_begin();
  2295. zend_delayed_compile_dim(result, ast, type);
  2296. return zend_delayed_compile_end(offset);
  2297. }
  2298. /* }}} */
  2299. static zend_op *zend_delayed_compile_prop(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
  2300. {
  2301. zend_ast *obj_ast = ast->child[0];
  2302. zend_ast *prop_ast = ast->child[1];
  2303. znode obj_node, prop_node;
  2304. zend_op *opline;
  2305. if (is_this_fetch(obj_ast)) {
  2306. if (this_guaranteed_exists()) {
  2307. obj_node.op_type = IS_UNUSED;
  2308. } else {
  2309. zend_emit_op(&obj_node, ZEND_FETCH_THIS, NULL, NULL);
  2310. }
  2311. CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
  2312. } else {
  2313. opline = zend_delayed_compile_var(&obj_node, obj_ast, type, 0);
  2314. zend_separate_if_call_and_write(&obj_node, obj_ast, type);
  2315. }
  2316. zend_compile_expr(&prop_node, prop_ast);
  2317. opline = zend_delayed_emit_op(result, ZEND_FETCH_OBJ_R, &obj_node, &prop_node);
  2318. if (opline->op2_type == IS_CONST) {
  2319. convert_to_string(CT_CONSTANT(opline->op2));
  2320. opline->extended_value = zend_alloc_cache_slots(3);
  2321. }
  2322. zend_adjust_for_fetch_type(opline, result, type);
  2323. return opline;
  2324. }
  2325. /* }}} */
  2326. static zend_op *zend_compile_prop(znode *result, zend_ast *ast, uint32_t type, int by_ref) /* {{{ */
  2327. {
  2328. uint32_t offset = zend_delayed_compile_begin();
  2329. zend_op *opline = zend_delayed_compile_prop(result, ast, type);
  2330. if (by_ref) { /* shared with cache_slot */
  2331. opline->extended_value |= ZEND_FETCH_REF;
  2332. }
  2333. return zend_delayed_compile_end(offset);
  2334. }
  2335. /* }}} */
  2336. zend_op *zend_compile_static_prop(znode *result, zend_ast *ast, uint32_t type, int by_ref, int delayed) /* {{{ */
  2337. {
  2338. zend_ast *class_ast = ast->child[0];
  2339. zend_ast *prop_ast = ast->child[1];
  2340. znode class_node, prop_node;
  2341. zend_op *opline;
  2342. zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
  2343. zend_compile_expr(&prop_node, prop_ast);
  2344. if (delayed) {
  2345. opline = zend_delayed_emit_op(result, ZEND_FETCH_STATIC_PROP_R, &prop_node, NULL);
  2346. } else {
  2347. opline = zend_emit_op(result, ZEND_FETCH_STATIC_PROP_R, &prop_node, NULL);
  2348. }
  2349. if (opline->op1_type == IS_CONST) {
  2350. convert_to_string(CT_CONSTANT(opline->op1));
  2351. opline->extended_value = zend_alloc_cache_slots(3);
  2352. }
  2353. if (class_node.op_type == IS_CONST) {
  2354. opline->op2_type = IS_CONST;
  2355. opline->op2.constant = zend_add_class_name_literal(
  2356. Z_STR(class_node.u.constant));
  2357. if (opline->op1_type != IS_CONST) {
  2358. opline->extended_value = zend_alloc_cache_slot();
  2359. }
  2360. } else {
  2361. SET_NODE(opline->op2, &class_node);
  2362. }
  2363. if (by_ref && (type == BP_VAR_W || type == BP_VAR_FUNC_ARG)) { /* shared with cache_slot */
  2364. opline->extended_value |= ZEND_FETCH_REF;
  2365. }
  2366. zend_adjust_for_fetch_type(opline, result, type);
  2367. return opline;
  2368. }
  2369. /* }}} */
  2370. static void zend_verify_list_assign_target(zend_ast *var_ast, zend_bool old_style) /* {{{ */ {
  2371. if (var_ast->kind == ZEND_AST_ARRAY) {
  2372. if (var_ast->attr == ZEND_ARRAY_SYNTAX_LONG) {
  2373. zend_error_noreturn(E_COMPILE_ERROR, "Cannot assign to array(), use [] instead");
  2374. }
  2375. if (old_style != var_ast->attr) {
  2376. zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix [] and list()");
  2377. }
  2378. } else if (!zend_can_write_to_variable(var_ast)) {
  2379. zend_error_noreturn(E_COMPILE_ERROR, "Assignments can only happen to writable values");
  2380. }
  2381. }
  2382. /* }}} */
  2383. static inline void zend_emit_assign_ref_znode(zend_ast *var_ast, znode *value_node);
  2384. /* Propagate refs used on leaf elements to the surrounding list() structures. */
  2385. static zend_bool zend_propagate_list_refs(zend_ast *ast) { /* {{{ */
  2386. zend_ast_list *list = zend_ast_get_list(ast);
  2387. zend_bool has_refs = 0;
  2388. uint32_t i;
  2389. for (i = 0; i < list->children; ++i) {
  2390. zend_ast *elem_ast = list->child[i];
  2391. if (elem_ast) {
  2392. zend_ast *var_ast = elem_ast->child[0];
  2393. if (var_ast->kind == ZEND_AST_ARRAY) {
  2394. elem_ast->attr = zend_propagate_list_refs(var_ast);
  2395. }
  2396. has_refs |= elem_ast->attr;
  2397. }
  2398. }
  2399. return has_refs;
  2400. }
  2401. /* }}} */
  2402. static void zend_compile_list_assign(
  2403. znode *result, zend_ast *ast, znode *expr_node, zend_bool old_style) /* {{{ */
  2404. {
  2405. zend_ast_list *list = zend_ast_get_list(ast);
  2406. uint32_t i;
  2407. zend_bool has_elems = 0;
  2408. zend_bool is_keyed =
  2409. list->children > 0 && list->child[0] != NULL && list->child[0]->child[1] != NULL;
  2410. if (list->children && expr_node->op_type == IS_CONST && Z_TYPE(expr_node->u.constant) == IS_STRING) {
  2411. zval_make_interned_string(&expr_node->u.constant);
  2412. }
  2413. for (i = 0; i < list->children; ++i) {
  2414. zend_ast *elem_ast = list->child[i];
  2415. zend_ast *var_ast, *key_ast;
  2416. znode fetch_result, dim_node;
  2417. zend_op *opline;
  2418. if (elem_ast == NULL) {
  2419. if (is_keyed) {
  2420. zend_error(E_COMPILE_ERROR,
  2421. "Cannot use empty array entries in keyed array assignment");
  2422. } else {
  2423. continue;
  2424. }
  2425. }
  2426. if (elem_ast->kind == ZEND_AST_UNPACK) {
  2427. zend_error(E_COMPILE_ERROR,
  2428. "Spread operator is not supported in assignments");
  2429. }
  2430. var_ast = elem_ast->child[0];
  2431. key_ast = elem_ast->child[1];
  2432. has_elems = 1;
  2433. if (is_keyed) {
  2434. if (key_ast == NULL) {
  2435. zend_error(E_COMPILE_ERROR,
  2436. "Cannot mix keyed and unkeyed array entries in assignments");
  2437. }
  2438. zend_compile_expr(&dim_node, key_ast);
  2439. } else {
  2440. if (key_ast != NULL) {
  2441. zend_error(E_COMPILE_ERROR,
  2442. "Cannot mix keyed and unkeyed array entries in assignments");
  2443. }
  2444. dim_node.op_type = IS_CONST;
  2445. ZVAL_LONG(&dim_node.u.constant, i);
  2446. }
  2447. if (expr_node->op_type == IS_CONST) {
  2448. Z_TRY_ADDREF(expr_node->u.constant);
  2449. }
  2450. zend_verify_list_assign_target(var_ast, old_style);
  2451. opline = zend_emit_op(&fetch_result,
  2452. elem_ast->attr ? (expr_node->op_type == IS_CV ? ZEND_FETCH_DIM_W : ZEND_FETCH_LIST_W) : ZEND_FETCH_LIST_R, expr_node, &dim_node);
  2453. if (dim_node.op_type == IS_CONST) {
  2454. zend_handle_numeric_dim(opline, &dim_node);
  2455. }
  2456. if (var_ast->kind == ZEND_AST_ARRAY) {
  2457. if (elem_ast->attr) {
  2458. zend_emit_op(&fetch_result, ZEND_MAKE_REF, &fetch_result, NULL);
  2459. }
  2460. zend_compile_list_assign(NULL, var_ast, &fetch_result, var_ast->attr);
  2461. } else if (elem_ast->attr) {
  2462. zend_emit_assign_ref_znode(var_ast, &fetch_result);
  2463. } else {
  2464. zend_emit_assign_znode(var_ast, &fetch_result);
  2465. }
  2466. }
  2467. if (has_elems == 0) {
  2468. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use empty list");
  2469. }
  2470. if (result) {
  2471. *result = *expr_node;
  2472. } else {
  2473. zend_do_free(expr_node);
  2474. }
  2475. }
  2476. /* }}} */
  2477. static void zend_ensure_writable_variable(const zend_ast *ast) /* {{{ */
  2478. {
  2479. if (ast->kind == ZEND_AST_CALL) {
  2480. zend_error_noreturn(E_COMPILE_ERROR, "Can't use function return value in write context");
  2481. }
  2482. if (ast->kind == ZEND_AST_METHOD_CALL || ast->kind == ZEND_AST_STATIC_CALL) {
  2483. zend_error_noreturn(E_COMPILE_ERROR, "Can't use method return value in write context");
  2484. }
  2485. }
  2486. /* }}} */
  2487. /* Detects $a... = $a pattern */
  2488. zend_bool zend_is_assign_to_self(zend_ast *var_ast, zend_ast *expr_ast) /* {{{ */
  2489. {
  2490. if (expr_ast->kind != ZEND_AST_VAR || expr_ast->child[0]->kind != ZEND_AST_ZVAL) {
  2491. return 0;
  2492. }
  2493. while (zend_is_variable(var_ast) && var_ast->kind != ZEND_AST_VAR) {
  2494. var_ast = var_ast->child[0];
  2495. }
  2496. if (var_ast->kind != ZEND_AST_VAR || var_ast->child[0]->kind != ZEND_AST_ZVAL) {
  2497. return 0;
  2498. }
  2499. {
  2500. zend_string *name1 = zval_get_string(zend_ast_get_zval(var_ast->child[0]));
  2501. zend_string *name2 = zval_get_string(zend_ast_get_zval(expr_ast->child[0]));
  2502. zend_bool result = zend_string_equals(name1, name2);
  2503. zend_string_release_ex(name1, 0);
  2504. zend_string_release_ex(name2, 0);
  2505. return result;
  2506. }
  2507. }
  2508. /* }}} */
  2509. void zend_compile_assign(znode *result, zend_ast *ast) /* {{{ */
  2510. {
  2511. zend_ast *var_ast = ast->child[0];
  2512. zend_ast *expr_ast = ast->child[1];
  2513. znode var_node, expr_node;
  2514. zend_op *opline;
  2515. uint32_t offset;
  2516. if (is_this_fetch(var_ast)) {
  2517. zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
  2518. }
  2519. zend_ensure_writable_variable(var_ast);
  2520. switch (var_ast->kind) {
  2521. case ZEND_AST_VAR:
  2522. offset = zend_delayed_compile_begin();
  2523. zend_delayed_compile_var(&var_node, var_ast, BP_VAR_W, 0);
  2524. zend_compile_expr(&expr_node, expr_ast);
  2525. zend_delayed_compile_end(offset);
  2526. zend_emit_op_tmp(result, ZEND_ASSIGN, &var_node, &expr_node);
  2527. return;
  2528. case ZEND_AST_STATIC_PROP:
  2529. offset = zend_delayed_compile_begin();
  2530. zend_delayed_compile_var(result, var_ast, BP_VAR_W, 0);
  2531. zend_compile_expr(&expr_node, expr_ast);
  2532. opline = zend_delayed_compile_end(offset);
  2533. opline->opcode = ZEND_ASSIGN_STATIC_PROP;
  2534. opline->result_type = IS_TMP_VAR;
  2535. result->op_type = IS_TMP_VAR;
  2536. zend_emit_op_data(&expr_node);
  2537. return;
  2538. case ZEND_AST_DIM:
  2539. offset = zend_delayed_compile_begin();
  2540. zend_delayed_compile_dim(result, var_ast, BP_VAR_W);
  2541. if (zend_is_assign_to_self(var_ast, expr_ast)
  2542. && !is_this_fetch(expr_ast)) {
  2543. /* $a[0] = $a should evaluate the right $a first */
  2544. znode cv_node;
  2545. if (zend_try_compile_cv(&cv_node, expr_ast) == FAILURE) {
  2546. zend_compile_simple_var_no_cv(&expr_node, expr_ast, BP_VAR_R, 0);
  2547. } else {
  2548. zend_emit_op_tmp(&expr_node, ZEND_QM_ASSIGN, &cv_node, NULL);
  2549. }
  2550. } else {
  2551. zend_compile_expr(&expr_node, expr_ast);
  2552. }
  2553. opline = zend_delayed_compile_end(offset);
  2554. opline->opcode = ZEND_ASSIGN_DIM;
  2555. opline->result_type = IS_TMP_VAR;
  2556. result->op_type = IS_TMP_VAR;
  2557. opline = zend_emit_op_data(&expr_node);
  2558. return;
  2559. case ZEND_AST_PROP:
  2560. offset = zend_delayed_compile_begin();
  2561. zend_delayed_compile_prop(result, var_ast, BP_VAR_W);
  2562. zend_compile_expr(&expr_node, expr_ast);
  2563. opline = zend_delayed_compile_end(offset);
  2564. opline->opcode = ZEND_ASSIGN_OBJ;
  2565. opline->result_type = IS_TMP_VAR;
  2566. result->op_type = IS_TMP_VAR;
  2567. zend_emit_op_data(&expr_node);
  2568. return;
  2569. case ZEND_AST_ARRAY:
  2570. if (zend_propagate_list_refs(var_ast)) {
  2571. if (!zend_is_variable_or_call(expr_ast)) {
  2572. zend_error_noreturn(E_COMPILE_ERROR,
  2573. "Cannot assign reference to non referencable value");
  2574. }
  2575. zend_compile_var(&expr_node, expr_ast, BP_VAR_W, 1);
  2576. /* MAKE_REF is usually not necessary for CVs. However, if there are
  2577. * self-assignments, this forces the RHS to evaluate first. */
  2578. zend_emit_op(&expr_node, ZEND_MAKE_REF, &expr_node, NULL);
  2579. } else {
  2580. if (expr_ast->kind == ZEND_AST_VAR) {
  2581. /* list($a, $b) = $a should evaluate the right $a first */
  2582. znode cv_node;
  2583. if (zend_try_compile_cv(&cv_node, expr_ast) == FAILURE) {
  2584. zend_compile_simple_var_no_cv(&expr_node, expr_ast, BP_VAR_R, 0);
  2585. } else {
  2586. zend_emit_op_tmp(&expr_node, ZEND_QM_ASSIGN, &cv_node, NULL);
  2587. }
  2588. } else {
  2589. zend_compile_expr(&expr_node, expr_ast);
  2590. }
  2591. }
  2592. zend_compile_list_assign(result, var_ast, &expr_node, var_ast->attr);
  2593. return;
  2594. EMPTY_SWITCH_DEFAULT_CASE();
  2595. }
  2596. }
  2597. /* }}} */
  2598. void zend_compile_assign_ref(znode *result, zend_ast *ast) /* {{{ */
  2599. {
  2600. zend_ast *target_ast = ast->child[0];
  2601. zend_ast *source_ast = ast->child[1];
  2602. znode target_node, source_node;
  2603. zend_op *opline;
  2604. uint32_t offset, flags;
  2605. if (is_this_fetch(target_ast)) {
  2606. zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
  2607. }
  2608. zend_ensure_writable_variable(target_ast);
  2609. offset = zend_delayed_compile_begin();
  2610. zend_delayed_compile_var(&target_node, target_ast, BP_VAR_W, 1);
  2611. zend_compile_var(&source_node, source_ast, BP_VAR_W, 1);
  2612. if ((target_ast->kind != ZEND_AST_VAR
  2613. || target_ast->child[0]->kind != ZEND_AST_ZVAL)
  2614. && source_node.op_type != IS_CV) {
  2615. /* Both LHS and RHS expressions may modify the same data structure,
  2616. * and the modification during RHS evaluation may dangle the pointer
  2617. * to the result of the LHS evaluation.
  2618. * Use MAKE_REF instruction to replace direct pointer with REFERENCE.
  2619. * See: Bug #71539
  2620. */
  2621. zend_emit_op(&source_node, ZEND_MAKE_REF, &source_node, NULL);
  2622. }
  2623. opline = zend_delayed_compile_end(offset);
  2624. if (source_node.op_type != IS_VAR && zend_is_call(source_ast)) {
  2625. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use result of built-in function in write context");
  2626. }
  2627. flags = zend_is_call(source_ast) ? ZEND_RETURNS_FUNCTION : 0;
  2628. if (opline && opline->opcode == ZEND_FETCH_OBJ_W) {
  2629. opline->opcode = ZEND_ASSIGN_OBJ_REF;
  2630. opline->extended_value &= ~ZEND_FETCH_REF;
  2631. opline->extended_value |= flags;
  2632. zend_emit_op_data(&source_node);
  2633. if (result != NULL) {
  2634. *result = target_node;
  2635. }
  2636. } else if (opline && opline->opcode == ZEND_FETCH_STATIC_PROP_W) {
  2637. opline->opcode = ZEND_ASSIGN_STATIC_PROP_REF;
  2638. opline->extended_value &= ~ZEND_FETCH_REF;
  2639. opline->extended_value |= flags;
  2640. zend_emit_op_data(&source_node);
  2641. if (result != NULL) {
  2642. *result = target_node;
  2643. }
  2644. } else {
  2645. opline = zend_emit_op(result, ZEND_ASSIGN_REF, &target_node, &source_node);
  2646. opline->extended_value = flags;
  2647. }
  2648. }
  2649. /* }}} */
  2650. static inline void zend_emit_assign_ref_znode(zend_ast *var_ast, znode *value_node) /* {{{ */
  2651. {
  2652. zend_ast *assign_ast = zend_ast_create(ZEND_AST_ASSIGN_REF, var_ast,
  2653. zend_ast_create_znode(value_node));
  2654. zend_compile_assign_ref(NULL, assign_ast);
  2655. }
  2656. /* }}} */
  2657. void zend_compile_compound_assign(znode *result, zend_ast *ast) /* {{{ */
  2658. {
  2659. zend_ast *var_ast = ast->child[0];
  2660. zend_ast *expr_ast = ast->child[1];
  2661. uint32_t opcode = ast->attr;
  2662. znode var_node, expr_node;
  2663. zend_op *opline;
  2664. uint32_t offset, cache_slot;
  2665. zend_ensure_writable_variable(var_ast);
  2666. switch (var_ast->kind) {
  2667. case ZEND_AST_VAR:
  2668. offset = zend_delayed_compile_begin();
  2669. zend_delayed_compile_var(&var_node, var_ast, BP_VAR_RW, 0);
  2670. zend_compile_expr(&expr_node, expr_ast);
  2671. zend_delayed_compile_end(offset);
  2672. opline = zend_emit_op_tmp(result, ZEND_ASSIGN_OP, &var_node, &expr_node);
  2673. opline->extended_value = opcode;
  2674. return;
  2675. case ZEND_AST_STATIC_PROP:
  2676. offset = zend_delayed_compile_begin();
  2677. zend_delayed_compile_var(result, var_ast, BP_VAR_RW, 0);
  2678. zend_compile_expr(&expr_node, expr_ast);
  2679. opline = zend_delayed_compile_end(offset);
  2680. cache_slot = opline->extended_value;
  2681. opline->opcode = ZEND_ASSIGN_STATIC_PROP_OP;
  2682. opline->extended_value = opcode;
  2683. opline->result_type = IS_TMP_VAR;
  2684. result->op_type = IS_TMP_VAR;
  2685. opline = zend_emit_op_data(&expr_node);
  2686. opline->extended_value = cache_slot;
  2687. return;
  2688. case ZEND_AST_DIM:
  2689. offset = zend_delayed_compile_begin();
  2690. zend_delayed_compile_dim(result, var_ast, BP_VAR_RW);
  2691. zend_compile_expr(&expr_node, expr_ast);
  2692. opline = zend_delayed_compile_end(offset);
  2693. opline->opcode = ZEND_ASSIGN_DIM_OP;
  2694. opline->extended_value = opcode;
  2695. opline->result_type = IS_TMP_VAR;
  2696. result->op_type = IS_TMP_VAR;
  2697. zend_emit_op_data(&expr_node);
  2698. return;
  2699. case ZEND_AST_PROP:
  2700. offset = zend_delayed_compile_begin();
  2701. zend_delayed_compile_prop(result, var_ast, BP_VAR_RW);
  2702. zend_compile_expr(&expr_node, expr_ast);
  2703. opline = zend_delayed_compile_end(offset);
  2704. cache_slot = opline->extended_value;
  2705. opline->opcode = ZEND_ASSIGN_OBJ_OP;
  2706. opline->extended_value = opcode;
  2707. opline->result_type = IS_TMP_VAR;
  2708. result->op_type = IS_TMP_VAR;
  2709. opline = zend_emit_op_data(&expr_node);
  2710. opline->extended_value = cache_slot;
  2711. return;
  2712. EMPTY_SWITCH_DEFAULT_CASE()
  2713. }
  2714. }
  2715. /* }}} */
  2716. uint32_t zend_compile_args(zend_ast *ast, zend_function *fbc) /* {{{ */
  2717. {
  2718. zend_ast_list *args = zend_ast_get_list(ast);
  2719. uint32_t i;
  2720. zend_bool uses_arg_unpack = 0;
  2721. uint32_t arg_count = 0; /* number of arguments not including unpacks */
  2722. for (i = 0; i < args->children; ++i) {
  2723. zend_ast *arg = args->child[i];
  2724. uint32_t arg_num = i + 1;
  2725. znode arg_node;
  2726. zend_op *opline;
  2727. zend_uchar opcode;
  2728. if (arg->kind == ZEND_AST_UNPACK) {
  2729. uses_arg_unpack = 1;
  2730. fbc = NULL;
  2731. zend_compile_expr(&arg_node, arg->child[0]);
  2732. opline = zend_emit_op(NULL, ZEND_SEND_UNPACK, &arg_node, NULL);
  2733. opline->op2.num = arg_count;
  2734. opline->result.var = EX_NUM_TO_VAR(arg_count - 1);
  2735. continue;
  2736. }
  2737. if (uses_arg_unpack) {
  2738. zend_error_noreturn(E_COMPILE_ERROR,
  2739. "Cannot use positional argument after argument unpacking");
  2740. }
  2741. arg_count++;
  2742. if (zend_is_call(arg)) {
  2743. zend_compile_var(&arg_node, arg, BP_VAR_R, 0);
  2744. if (arg_node.op_type & (IS_CONST|IS_TMP_VAR)) {
  2745. /* Function call was converted into builtin instruction */
  2746. opcode = ZEND_SEND_VAL;
  2747. } else {
  2748. if (fbc) {
  2749. if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
  2750. opcode = ZEND_SEND_VAR_NO_REF;
  2751. } else if (ARG_MAY_BE_SENT_BY_REF(fbc, arg_num)) {
  2752. opcode = ZEND_SEND_VAL;
  2753. } else {
  2754. opcode = ZEND_SEND_VAR;
  2755. }
  2756. } else {
  2757. opcode = ZEND_SEND_VAR_NO_REF_EX;
  2758. }
  2759. }
  2760. } else if (zend_is_variable(arg)) {
  2761. if (fbc) {
  2762. if (ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) {
  2763. zend_compile_var(&arg_node, arg, BP_VAR_W, 1);
  2764. opcode = ZEND_SEND_REF;
  2765. } else {
  2766. zend_compile_var(&arg_node, arg, BP_VAR_R, 0);
  2767. opcode = (arg_node.op_type == IS_TMP_VAR) ? ZEND_SEND_VAL : ZEND_SEND_VAR;
  2768. }
  2769. } else {
  2770. do {
  2771. if (arg->kind == ZEND_AST_VAR) {
  2772. CG(zend_lineno) = zend_ast_get_lineno(ast);
  2773. if (is_this_fetch(arg)) {
  2774. zend_emit_op(&arg_node, ZEND_FETCH_THIS, NULL, NULL);
  2775. opcode = ZEND_SEND_VAR_EX;
  2776. CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
  2777. break;
  2778. } else if (zend_try_compile_cv(&arg_node, arg) == SUCCESS) {
  2779. opcode = ZEND_SEND_VAR_EX;
  2780. break;
  2781. }
  2782. }
  2783. opline = zend_emit_op(NULL, ZEND_CHECK_FUNC_ARG, NULL, NULL);
  2784. opline->op2.num = arg_num;
  2785. zend_compile_var(&arg_node, arg, BP_VAR_FUNC_ARG, 1);
  2786. opcode = ZEND_SEND_FUNC_ARG;
  2787. } while (0);
  2788. }
  2789. } else {
  2790. zend_compile_expr(&arg_node, arg);
  2791. if (arg_node.op_type == IS_VAR) {
  2792. /* pass ++$a or something similar */
  2793. if (fbc) {
  2794. if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
  2795. opcode = ZEND_SEND_VAR_NO_REF;
  2796. } else if (ARG_MAY_BE_SENT_BY_REF(fbc, arg_num)) {
  2797. opcode = ZEND_SEND_VAL;
  2798. } else {
  2799. opcode = ZEND_SEND_VAR;
  2800. }
  2801. } else {
  2802. opcode = ZEND_SEND_VAR_NO_REF_EX;
  2803. }
  2804. } else if (arg_node.op_type == IS_CV) {
  2805. if (fbc) {
  2806. if (ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) {
  2807. opcode = ZEND_SEND_REF;
  2808. } else {
  2809. opcode = ZEND_SEND_VAR;
  2810. }
  2811. } else {
  2812. opcode = ZEND_SEND_VAR_EX;
  2813. }
  2814. } else {
  2815. /* Delay "Only variables can be passed by reference" error to execution */
  2816. if (fbc && !ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
  2817. opcode = ZEND_SEND_VAL;
  2818. } else {
  2819. opcode = ZEND_SEND_VAL_EX;
  2820. }
  2821. }
  2822. }
  2823. opline = zend_emit_op(NULL, opcode, &arg_node, NULL);
  2824. opline->op2.opline_num = arg_num;
  2825. opline->result.var = EX_NUM_TO_VAR(arg_num - 1);
  2826. }
  2827. return arg_count;
  2828. }
  2829. /* }}} */
  2830. ZEND_API zend_uchar zend_get_call_op(const zend_op *init_op, zend_function *fbc) /* {{{ */
  2831. {
  2832. if (fbc) {
  2833. if (fbc->type == ZEND_INTERNAL_FUNCTION && !(CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS)) {
  2834. if (init_op->opcode == ZEND_INIT_FCALL && !zend_execute_internal) {
  2835. if (!(fbc->common.fn_flags & (ZEND_ACC_ABSTRACT|ZEND_ACC_DEPRECATED))) {
  2836. return ZEND_DO_ICALL;
  2837. } else {
  2838. return ZEND_DO_FCALL_BY_NAME;
  2839. }
  2840. }
  2841. } else if (!(CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS)){
  2842. if (zend_execute_ex == execute_ex && !(fbc->common.fn_flags & ZEND_ACC_ABSTRACT)) {
  2843. return ZEND_DO_UCALL;
  2844. }
  2845. }
  2846. } else if (zend_execute_ex == execute_ex &&
  2847. !zend_execute_internal &&
  2848. (init_op->opcode == ZEND_INIT_FCALL_BY_NAME ||
  2849. init_op->opcode == ZEND_INIT_NS_FCALL_BY_NAME)) {
  2850. return ZEND_DO_FCALL_BY_NAME;
  2851. }
  2852. return ZEND_DO_FCALL;
  2853. }
  2854. /* }}} */
  2855. void zend_compile_call_common(znode *result, zend_ast *args_ast, zend_function *fbc) /* {{{ */
  2856. {
  2857. zend_op *opline;
  2858. uint32_t opnum_init = get_next_op_number() - 1;
  2859. uint32_t arg_count;
  2860. arg_count = zend_compile_args(args_ast, fbc);
  2861. zend_do_extended_fcall_begin();
  2862. opline = &CG(active_op_array)->opcodes[opnum_init];
  2863. opline->extended_value = arg_count;
  2864. if (opline->opcode == ZEND_INIT_FCALL) {
  2865. opline->op1.num = zend_vm_calc_used_stack(arg_count, fbc);
  2866. }
  2867. opline = zend_emit_op(result, zend_get_call_op(opline, fbc), NULL, NULL);
  2868. zend_do_extended_fcall_end();
  2869. }
  2870. /* }}} */
  2871. zend_bool zend_compile_function_name(znode *name_node, zend_ast *name_ast) /* {{{ */
  2872. {
  2873. zend_string *orig_name = zend_ast_get_str(name_ast);
  2874. zend_bool is_fully_qualified;
  2875. name_node->op_type = IS_CONST;
  2876. ZVAL_STR(&name_node->u.constant, zend_resolve_function_name(
  2877. orig_name, name_ast->attr, &is_fully_qualified));
  2878. return !is_fully_qualified && FC(current_namespace);
  2879. }
  2880. /* }}} */
  2881. void zend_compile_ns_call(znode *result, znode *name_node, zend_ast *args_ast) /* {{{ */
  2882. {
  2883. zend_op *opline = get_next_op();
  2884. opline->opcode = ZEND_INIT_NS_FCALL_BY_NAME;
  2885. opline->op2_type = IS_CONST;
  2886. opline->op2.constant = zend_add_ns_func_name_literal(
  2887. Z_STR(name_node->u.constant));
  2888. opline->result.num = zend_alloc_cache_slot();
  2889. zend_compile_call_common(result, args_ast, NULL);
  2890. }
  2891. /* }}} */
  2892. void zend_compile_dynamic_call(znode *result, znode *name_node, zend_ast *args_ast) /* {{{ */
  2893. {
  2894. if (name_node->op_type == IS_CONST && Z_TYPE(name_node->u.constant) == IS_STRING) {
  2895. const char *colon;
  2896. zend_string *str = Z_STR(name_node->u.constant);
  2897. if ((colon = zend_memrchr(ZSTR_VAL(str), ':', ZSTR_LEN(str))) != NULL && colon > ZSTR_VAL(str) && *(colon - 1) == ':') {
  2898. zend_string *class = zend_string_init(ZSTR_VAL(str), colon - ZSTR_VAL(str) - 1, 0);
  2899. zend_string *method = zend_string_init(colon + 1, ZSTR_LEN(str) - (colon - ZSTR_VAL(str)) - 1, 0);
  2900. zend_op *opline = get_next_op();
  2901. opline->opcode = ZEND_INIT_STATIC_METHOD_CALL;
  2902. opline->op1_type = IS_CONST;
  2903. opline->op1.constant = zend_add_class_name_literal(class);
  2904. opline->op2_type = IS_CONST;
  2905. opline->op2.constant = zend_add_func_name_literal(method);
  2906. /* 2 slots, for class and method */
  2907. opline->result.num = zend_alloc_cache_slots(2);
  2908. zval_ptr_dtor(&name_node->u.constant);
  2909. } else {
  2910. zend_op *opline = get_next_op();
  2911. opline->opcode = ZEND_INIT_FCALL_BY_NAME;
  2912. opline->op2_type = IS_CONST;
  2913. opline->op2.constant = zend_add_func_name_literal(str);
  2914. opline->result.num = zend_alloc_cache_slot();
  2915. }
  2916. } else {
  2917. zend_emit_op(NULL, ZEND_INIT_DYNAMIC_CALL, NULL, name_node);
  2918. }
  2919. zend_compile_call_common(result, args_ast, NULL);
  2920. }
  2921. /* }}} */
  2922. static inline zend_bool zend_args_contain_unpack(zend_ast_list *args) /* {{{ */
  2923. {
  2924. uint32_t i;
  2925. for (i = 0; i < args->children; ++i) {
  2926. if (args->child[i]->kind == ZEND_AST_UNPACK) {
  2927. return 1;
  2928. }
  2929. }
  2930. return 0;
  2931. }
  2932. /* }}} */
  2933. int zend_compile_func_strlen(znode *result, zend_ast_list *args) /* {{{ */
  2934. {
  2935. znode arg_node;
  2936. if (args->children != 1) {
  2937. return FAILURE;
  2938. }
  2939. zend_compile_expr(&arg_node, args->child[0]);
  2940. if (arg_node.op_type == IS_CONST && Z_TYPE(arg_node.u.constant) == IS_STRING) {
  2941. result->op_type = IS_CONST;
  2942. ZVAL_LONG(&result->u.constant, Z_STRLEN(arg_node.u.constant));
  2943. zval_ptr_dtor_str(&arg_node.u.constant);
  2944. } else {
  2945. zend_emit_op_tmp(result, ZEND_STRLEN, &arg_node, NULL);
  2946. }
  2947. return SUCCESS;
  2948. }
  2949. /* }}} */
  2950. int zend_compile_func_typecheck(znode *result, zend_ast_list *args, uint32_t type) /* {{{ */
  2951. {
  2952. znode arg_node;
  2953. zend_op *opline;
  2954. if (args->children != 1) {
  2955. return FAILURE;
  2956. }
  2957. zend_compile_expr(&arg_node, args->child[0]);
  2958. opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &arg_node, NULL);
  2959. if (type != _IS_BOOL) {
  2960. opline->extended_value = (1 << type);
  2961. } else {
  2962. opline->extended_value = (1 << IS_FALSE) | (1 << IS_TRUE);
  2963. }
  2964. return SUCCESS;
  2965. }
  2966. /* }}} */
  2967. static int zend_compile_func_is_scalar(znode *result, zend_ast_list *args) /* {{{ */
  2968. {
  2969. znode arg_node;
  2970. zend_op *opline;
  2971. if (args->children != 1) {
  2972. return FAILURE;
  2973. }
  2974. zend_compile_expr(&arg_node, args->child[0]);
  2975. opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &arg_node, NULL);
  2976. opline->extended_value = (1 << IS_FALSE | 1 << IS_TRUE | 1 << IS_DOUBLE | 1 << IS_LONG | 1 << IS_STRING);
  2977. return SUCCESS;
  2978. }
  2979. int zend_compile_func_cast(znode *result, zend_ast_list *args, uint32_t type) /* {{{ */
  2980. {
  2981. znode arg_node;
  2982. zend_op *opline;
  2983. if (args->children != 1) {
  2984. return FAILURE;
  2985. }
  2986. zend_compile_expr(&arg_node, args->child[0]);
  2987. if (type == _IS_BOOL) {
  2988. opline = zend_emit_op_tmp(result, ZEND_BOOL, &arg_node, NULL);
  2989. } else {
  2990. opline = zend_emit_op_tmp(result, ZEND_CAST, &arg_node, NULL);
  2991. opline->extended_value = type;
  2992. }
  2993. return SUCCESS;
  2994. }
  2995. /* }}} */
  2996. int zend_compile_func_defined(znode *result, zend_ast_list *args) /* {{{ */
  2997. {
  2998. zend_string *name;
  2999. zend_op *opline;
  3000. if (args->children != 1 || args->child[0]->kind != ZEND_AST_ZVAL) {
  3001. return FAILURE;
  3002. }
  3003. name = zval_get_string(zend_ast_get_zval(args->child[0]));
  3004. if (zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name)) || zend_memrchr(ZSTR_VAL(name), ':', ZSTR_LEN(name))) {
  3005. zend_string_release_ex(name, 0);
  3006. return FAILURE;
  3007. }
  3008. if (zend_try_ct_eval_const(&result->u.constant, name, 0)) {
  3009. zend_string_release_ex(name, 0);
  3010. zval_ptr_dtor(&result->u.constant);
  3011. ZVAL_TRUE(&result->u.constant);
  3012. result->op_type = IS_CONST;
  3013. return SUCCESS;
  3014. }
  3015. opline = zend_emit_op_tmp(result, ZEND_DEFINED, NULL, NULL);
  3016. opline->op1_type = IS_CONST;
  3017. LITERAL_STR(opline->op1, name);
  3018. opline->extended_value = zend_alloc_cache_slot();
  3019. return SUCCESS;
  3020. }
  3021. /* }}} */
  3022. int zend_compile_func_chr(znode *result, zend_ast_list *args) /* {{{ */
  3023. {
  3024. if (args->children == 1 &&
  3025. args->child[0]->kind == ZEND_AST_ZVAL &&
  3026. Z_TYPE_P(zend_ast_get_zval(args->child[0])) == IS_LONG) {
  3027. zend_long c = Z_LVAL_P(zend_ast_get_zval(args->child[0])) & 0xff;
  3028. result->op_type = IS_CONST;
  3029. ZVAL_INTERNED_STR(&result->u.constant, ZSTR_CHAR(c));
  3030. return SUCCESS;
  3031. } else {
  3032. return FAILURE;
  3033. }
  3034. }
  3035. /* }}} */
  3036. int zend_compile_func_ord(znode *result, zend_ast_list *args) /* {{{ */
  3037. {
  3038. if (args->children == 1 &&
  3039. args->child[0]->kind == ZEND_AST_ZVAL &&
  3040. Z_TYPE_P(zend_ast_get_zval(args->child[0])) == IS_STRING) {
  3041. result->op_type = IS_CONST;
  3042. ZVAL_LONG(&result->u.constant, (unsigned char)Z_STRVAL_P(zend_ast_get_zval(args->child[0]))[0]);
  3043. return SUCCESS;
  3044. } else {
  3045. return FAILURE;
  3046. }
  3047. }
  3048. /* }}} */
  3049. /* We can only calculate the stack size for functions that have been fully compiled, otherwise
  3050. * additional CV or TMP slots may still be added. This prevents the use of INIT_FCALL for
  3051. * directly or indirectly recursive function calls. */
  3052. static zend_bool fbc_is_finalized(zend_function *fbc) {
  3053. return !ZEND_USER_CODE(fbc->type) || (fbc->common.fn_flags & ZEND_ACC_DONE_PASS_TWO);
  3054. }
  3055. static int zend_try_compile_ct_bound_init_user_func(zend_ast *name_ast, uint32_t num_args) /* {{{ */
  3056. {
  3057. zend_string *name, *lcname;
  3058. zend_function *fbc;
  3059. zend_op *opline;
  3060. if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) {
  3061. return FAILURE;
  3062. }
  3063. name = zend_ast_get_str(name_ast);
  3064. lcname = zend_string_tolower(name);
  3065. fbc = zend_hash_find_ptr(CG(function_table), lcname);
  3066. if (!fbc || !fbc_is_finalized(fbc)
  3067. || (fbc->type == ZEND_INTERNAL_FUNCTION && (CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS))
  3068. || (fbc->type == ZEND_USER_FUNCTION && (CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS))
  3069. || (fbc->type == ZEND_USER_FUNCTION && (CG(compiler_options) & ZEND_COMPILE_IGNORE_OTHER_FILES) && fbc->op_array.filename != CG(active_op_array)->filename)
  3070. ) {
  3071. zend_string_release_ex(lcname, 0);
  3072. return FAILURE;
  3073. }
  3074. opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, NULL);
  3075. opline->extended_value = num_args;
  3076. opline->op1.num = zend_vm_calc_used_stack(num_args, fbc);
  3077. opline->op2_type = IS_CONST;
  3078. LITERAL_STR(opline->op2, lcname);
  3079. opline->result.num = zend_alloc_cache_slot();
  3080. return SUCCESS;
  3081. }
  3082. /* }}} */
  3083. static void zend_compile_init_user_func(zend_ast *name_ast, uint32_t num_args, zend_string *orig_func_name) /* {{{ */
  3084. {
  3085. zend_op *opline;
  3086. znode name_node;
  3087. if (zend_try_compile_ct_bound_init_user_func(name_ast, num_args) == SUCCESS) {
  3088. return;
  3089. }
  3090. zend_compile_expr(&name_node, name_ast);
  3091. opline = zend_emit_op(NULL, ZEND_INIT_USER_CALL, NULL, &name_node);
  3092. opline->op1_type = IS_CONST;
  3093. LITERAL_STR(opline->op1, zend_string_copy(orig_func_name));
  3094. opline->extended_value = num_args;
  3095. }
  3096. /* }}} */
  3097. /* cufa = call_user_func_array */
  3098. int zend_compile_func_cufa(znode *result, zend_ast_list *args, zend_string *lcname) /* {{{ */
  3099. {
  3100. znode arg_node;
  3101. if (args->children != 2) {
  3102. return FAILURE;
  3103. }
  3104. zend_compile_init_user_func(args->child[0], 0, lcname);
  3105. if (args->child[1]->kind == ZEND_AST_CALL
  3106. && args->child[1]->child[0]->kind == ZEND_AST_ZVAL
  3107. && Z_TYPE_P(zend_ast_get_zval(args->child[1]->child[0])) == IS_STRING
  3108. && args->child[1]->child[1]->kind == ZEND_AST_ARG_LIST) {
  3109. zend_string *orig_name = zend_ast_get_str(args->child[1]->child[0]);
  3110. zend_ast_list *list = zend_ast_get_list(args->child[1]->child[1]);
  3111. zend_bool is_fully_qualified;
  3112. zend_string *name = zend_resolve_function_name(orig_name, args->child[1]->child[0]->attr, &is_fully_qualified);
  3113. if (zend_string_equals_literal_ci(name, "array_slice")
  3114. && list->children == 3
  3115. && list->child[1]->kind == ZEND_AST_ZVAL) {
  3116. zval *zv = zend_ast_get_zval(list->child[1]);
  3117. if (Z_TYPE_P(zv) == IS_LONG
  3118. && Z_LVAL_P(zv) >= 0
  3119. && Z_LVAL_P(zv) <= 0x7fffffff) {
  3120. zend_op *opline;
  3121. znode len_node;
  3122. zend_compile_expr(&arg_node, list->child[0]);
  3123. zend_compile_expr(&len_node, list->child[2]);
  3124. opline = zend_emit_op(NULL, ZEND_SEND_ARRAY, &arg_node, &len_node);
  3125. opline->extended_value = Z_LVAL_P(zv);
  3126. zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
  3127. zend_string_release_ex(name, 0);
  3128. return SUCCESS;
  3129. }
  3130. }
  3131. zend_string_release_ex(name, 0);
  3132. }
  3133. zend_compile_expr(&arg_node, args->child[1]);
  3134. zend_emit_op(NULL, ZEND_SEND_ARRAY, &arg_node, NULL);
  3135. zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
  3136. return SUCCESS;
  3137. }
  3138. /* }}} */
  3139. /* cuf = call_user_func */
  3140. int zend_compile_func_cuf(znode *result, zend_ast_list *args, zend_string *lcname) /* {{{ */
  3141. {
  3142. uint32_t i;
  3143. if (args->children < 1) {
  3144. return FAILURE;
  3145. }
  3146. zend_compile_init_user_func(args->child[0], args->children - 1, lcname);
  3147. for (i = 1; i < args->children; ++i) {
  3148. zend_ast *arg_ast = args->child[i];
  3149. znode arg_node;
  3150. zend_op *opline;
  3151. zend_compile_expr(&arg_node, arg_ast);
  3152. opline = zend_emit_op(NULL, ZEND_SEND_USER, &arg_node, NULL);
  3153. opline->op2.num = i;
  3154. opline->result.var = EX_NUM_TO_VAR(i - 1);
  3155. }
  3156. zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
  3157. return SUCCESS;
  3158. }
  3159. /* }}} */
  3160. static void zend_compile_assert(znode *result, zend_ast_list *args, zend_string *name, zend_function *fbc) /* {{{ */
  3161. {
  3162. if (EG(assertions) >= 0) {
  3163. znode name_node;
  3164. zend_op *opline;
  3165. uint32_t check_op_number = get_next_op_number();
  3166. zend_emit_op(NULL, ZEND_ASSERT_CHECK, NULL, NULL);
  3167. if (fbc && fbc_is_finalized(fbc)) {
  3168. name_node.op_type = IS_CONST;
  3169. ZVAL_STR_COPY(&name_node.u.constant, name);
  3170. opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node);
  3171. } else {
  3172. opline = zend_emit_op(NULL, ZEND_INIT_NS_FCALL_BY_NAME, NULL, NULL);
  3173. opline->op2_type = IS_CONST;
  3174. opline->op2.constant = zend_add_ns_func_name_literal(name);
  3175. }
  3176. opline->result.num = zend_alloc_cache_slot();
  3177. if (args->children == 1 &&
  3178. (args->child[0]->kind != ZEND_AST_ZVAL ||
  3179. Z_TYPE_P(zend_ast_get_zval(args->child[0])) != IS_STRING)) {
  3180. /* add "assert(condition) as assertion message */
  3181. zend_ast_list_add((zend_ast*)args,
  3182. zend_ast_create_zval_from_str(
  3183. zend_ast_export("assert(", args->child[0], ")")));
  3184. }
  3185. zend_compile_call_common(result, (zend_ast*)args, fbc);
  3186. opline = &CG(active_op_array)->opcodes[check_op_number];
  3187. opline->op2.opline_num = get_next_op_number();
  3188. SET_NODE(opline->result, result);
  3189. } else {
  3190. if (!fbc) {
  3191. zend_string_release_ex(name, 0);
  3192. }
  3193. result->op_type = IS_CONST;
  3194. ZVAL_TRUE(&result->u.constant);
  3195. }
  3196. }
  3197. /* }}} */
  3198. static int zend_compile_func_in_array(znode *result, zend_ast_list *args) /* {{{ */
  3199. {
  3200. zend_bool strict = 0;
  3201. znode array, needly;
  3202. zend_op *opline;
  3203. if (args->children == 3) {
  3204. if (args->child[2]->kind == ZEND_AST_ZVAL) {
  3205. strict = zend_is_true(zend_ast_get_zval(args->child[2]));
  3206. } else if (args->child[2]->kind == ZEND_AST_CONST) {
  3207. zval value;
  3208. zend_ast *name_ast = args->child[2]->child[0];
  3209. zend_bool is_fully_qualified;
  3210. zend_string *resolved_name = zend_resolve_const_name(
  3211. zend_ast_get_str(name_ast), name_ast->attr, &is_fully_qualified);
  3212. if (!zend_try_ct_eval_const(&value, resolved_name, is_fully_qualified)) {
  3213. zend_string_release_ex(resolved_name, 0);
  3214. return FAILURE;
  3215. }
  3216. zend_string_release_ex(resolved_name, 0);
  3217. strict = zend_is_true(&value);
  3218. zval_ptr_dtor(&value);
  3219. } else {
  3220. return FAILURE;
  3221. }
  3222. } else if (args->children != 2) {
  3223. return FAILURE;
  3224. }
  3225. if (args->child[1]->kind != ZEND_AST_ARRAY
  3226. || !zend_try_ct_eval_array(&array.u.constant, args->child[1])) {
  3227. return FAILURE;
  3228. }
  3229. if (zend_hash_num_elements(Z_ARRVAL(array.u.constant)) > 0) {
  3230. zend_bool ok = 1;
  3231. zval *val, tmp;
  3232. HashTable *src = Z_ARRVAL(array.u.constant);
  3233. HashTable *dst = zend_new_array(zend_hash_num_elements(src));
  3234. ZVAL_TRUE(&tmp);
  3235. if (strict) {
  3236. ZEND_HASH_FOREACH_VAL(src, val) {
  3237. if (Z_TYPE_P(val) == IS_STRING) {
  3238. zend_hash_add(dst, Z_STR_P(val), &tmp);
  3239. } else if (Z_TYPE_P(val) == IS_LONG) {
  3240. zend_hash_index_add(dst, Z_LVAL_P(val), &tmp);
  3241. } else {
  3242. zend_array_destroy(dst);
  3243. ok = 0;
  3244. break;
  3245. }
  3246. } ZEND_HASH_FOREACH_END();
  3247. } else {
  3248. ZEND_HASH_FOREACH_VAL(src, val) {
  3249. if (Z_TYPE_P(val) != IS_STRING
  3250. || is_numeric_string(Z_STRVAL_P(val), Z_STRLEN_P(val), NULL, NULL, 0)) {
  3251. zend_array_destroy(dst);
  3252. ok = 0;
  3253. break;
  3254. }
  3255. zend_hash_add(dst, Z_STR_P(val), &tmp);
  3256. } ZEND_HASH_FOREACH_END();
  3257. }
  3258. zend_array_destroy(src);
  3259. if (!ok) {
  3260. return FAILURE;
  3261. }
  3262. Z_ARRVAL(array.u.constant) = dst;
  3263. }
  3264. array.op_type = IS_CONST;
  3265. zend_compile_expr(&needly, args->child[0]);
  3266. opline = zend_emit_op_tmp(result, ZEND_IN_ARRAY, &needly, &array);
  3267. opline->extended_value = strict;
  3268. return SUCCESS;
  3269. }
  3270. /* }}} */
  3271. int zend_compile_func_count(znode *result, zend_ast_list *args, zend_string *lcname) /* {{{ */
  3272. {
  3273. znode arg_node;
  3274. zend_op *opline;
  3275. if (args->children != 1) {
  3276. return FAILURE;
  3277. }
  3278. zend_compile_expr(&arg_node, args->child[0]);
  3279. opline = zend_emit_op_tmp(result, ZEND_COUNT, &arg_node, NULL);
  3280. opline->extended_value = zend_string_equals_literal(lcname, "sizeof");
  3281. return SUCCESS;
  3282. }
  3283. /* }}} */
  3284. int zend_compile_func_get_class(znode *result, zend_ast_list *args) /* {{{ */
  3285. {
  3286. if (args->children == 0) {
  3287. zend_emit_op_tmp(result, ZEND_GET_CLASS, NULL, NULL);
  3288. } else {
  3289. znode arg_node;
  3290. if (args->children != 1) {
  3291. return FAILURE;
  3292. }
  3293. zend_compile_expr(&arg_node, args->child[0]);
  3294. zend_emit_op_tmp(result, ZEND_GET_CLASS, &arg_node, NULL);
  3295. }
  3296. return SUCCESS;
  3297. }
  3298. /* }}} */
  3299. int zend_compile_func_get_called_class(znode *result, zend_ast_list *args) /* {{{ */
  3300. {
  3301. if (args->children != 0) {
  3302. return FAILURE;
  3303. }
  3304. zend_emit_op_tmp(result, ZEND_GET_CALLED_CLASS, NULL, NULL);
  3305. return SUCCESS;
  3306. }
  3307. /* }}} */
  3308. int zend_compile_func_gettype(znode *result, zend_ast_list *args) /* {{{ */
  3309. {
  3310. znode arg_node;
  3311. if (args->children != 1) {
  3312. return FAILURE;
  3313. }
  3314. zend_compile_expr(&arg_node, args->child[0]);
  3315. zend_emit_op_tmp(result, ZEND_GET_TYPE, &arg_node, NULL);
  3316. return SUCCESS;
  3317. }
  3318. /* }}} */
  3319. int zend_compile_func_num_args(znode *result, zend_ast_list *args) /* {{{ */
  3320. {
  3321. if (CG(active_op_array)->function_name && args->children == 0) {
  3322. zend_emit_op_tmp(result, ZEND_FUNC_NUM_ARGS, NULL, NULL);
  3323. return SUCCESS;
  3324. } else {
  3325. return FAILURE;
  3326. }
  3327. }
  3328. /* }}} */
  3329. int zend_compile_func_get_args(znode *result, zend_ast_list *args) /* {{{ */
  3330. {
  3331. if (CG(active_op_array)->function_name && args->children == 0) {
  3332. zend_emit_op_tmp(result, ZEND_FUNC_GET_ARGS, NULL, NULL);
  3333. return SUCCESS;
  3334. } else {
  3335. return FAILURE;
  3336. }
  3337. }
  3338. /* }}} */
  3339. int zend_compile_func_array_key_exists(znode *result, zend_ast_list *args) /* {{{ */
  3340. {
  3341. znode subject, needle;
  3342. if (args->children != 2) {
  3343. return FAILURE;
  3344. }
  3345. zend_compile_expr(&needle, args->child[0]);
  3346. zend_compile_expr(&subject, args->child[1]);
  3347. zend_emit_op_tmp(result, ZEND_ARRAY_KEY_EXISTS, &needle, &subject);
  3348. return SUCCESS;
  3349. }
  3350. /* }}} */
  3351. int zend_compile_func_array_slice(znode *result, zend_ast_list *args) /* {{{ */
  3352. {
  3353. if (CG(active_op_array)->function_name
  3354. && args->children == 2
  3355. && args->child[0]->kind == ZEND_AST_CALL
  3356. && args->child[0]->child[0]->kind == ZEND_AST_ZVAL
  3357. && Z_TYPE_P(zend_ast_get_zval(args->child[0]->child[0])) == IS_STRING
  3358. && args->child[0]->child[1]->kind == ZEND_AST_ARG_LIST
  3359. && args->child[1]->kind == ZEND_AST_ZVAL) {
  3360. zend_string *orig_name = zend_ast_get_str(args->child[0]->child[0]);
  3361. zend_bool is_fully_qualified;
  3362. zend_string *name = zend_resolve_function_name(orig_name, args->child[0]->child[0]->attr, &is_fully_qualified);
  3363. zend_ast_list *list = zend_ast_get_list(args->child[0]->child[1]);
  3364. zval *zv = zend_ast_get_zval(args->child[1]);
  3365. znode first;
  3366. if (zend_string_equals_literal_ci(name, "func_get_args")
  3367. && list->children == 0
  3368. && Z_TYPE_P(zv) == IS_LONG
  3369. && Z_LVAL_P(zv) >= 0) {
  3370. first.op_type = IS_CONST;
  3371. ZVAL_LONG(&first.u.constant, Z_LVAL_P(zv));
  3372. zend_emit_op_tmp(result, ZEND_FUNC_GET_ARGS, &first, NULL);
  3373. zend_string_release_ex(name, 0);
  3374. return SUCCESS;
  3375. }
  3376. zend_string_release_ex(name, 0);
  3377. }
  3378. return FAILURE;
  3379. }
  3380. /* }}} */
  3381. int zend_try_compile_special_func(znode *result, zend_string *lcname, zend_ast_list *args, zend_function *fbc, uint32_t type) /* {{{ */
  3382. {
  3383. if (CG(compiler_options) & ZEND_COMPILE_NO_BUILTINS) {
  3384. return FAILURE;
  3385. }
  3386. if (zend_args_contain_unpack(args)) {
  3387. return FAILURE;
  3388. }
  3389. if (zend_string_equals_literal(lcname, "strlen")) {
  3390. return zend_compile_func_strlen(result, args);
  3391. } else if (zend_string_equals_literal(lcname, "is_null")) {
  3392. return zend_compile_func_typecheck(result, args, IS_NULL);
  3393. } else if (zend_string_equals_literal(lcname, "is_bool")) {
  3394. return zend_compile_func_typecheck(result, args, _IS_BOOL);
  3395. } else if (zend_string_equals_literal(lcname, "is_long")
  3396. || zend_string_equals_literal(lcname, "is_int")
  3397. || zend_string_equals_literal(lcname, "is_integer")
  3398. ) {
  3399. return zend_compile_func_typecheck(result, args, IS_LONG);
  3400. } else if (zend_string_equals_literal(lcname, "is_float")
  3401. || zend_string_equals_literal(lcname, "is_double")
  3402. ) {
  3403. return zend_compile_func_typecheck(result, args, IS_DOUBLE);
  3404. } else if (zend_string_equals_literal(lcname, "is_string")) {
  3405. return zend_compile_func_typecheck(result, args, IS_STRING);
  3406. } else if (zend_string_equals_literal(lcname, "is_array")) {
  3407. return zend_compile_func_typecheck(result, args, IS_ARRAY);
  3408. } else if (zend_string_equals_literal(lcname, "is_object")) {
  3409. return zend_compile_func_typecheck(result, args, IS_OBJECT);
  3410. } else if (zend_string_equals_literal(lcname, "is_resource")) {
  3411. return zend_compile_func_typecheck(result, args, IS_RESOURCE);
  3412. } else if (zend_string_equals_literal(lcname, "is_scalar")) {
  3413. return zend_compile_func_is_scalar(result, args);
  3414. } else if (zend_string_equals_literal(lcname, "boolval")) {
  3415. return zend_compile_func_cast(result, args, _IS_BOOL);
  3416. } else if (zend_string_equals_literal(lcname, "intval")) {
  3417. return zend_compile_func_cast(result, args, IS_LONG);
  3418. } else if (zend_string_equals_literal(lcname, "floatval")
  3419. || zend_string_equals_literal(lcname, "doubleval")
  3420. ) {
  3421. return zend_compile_func_cast(result, args, IS_DOUBLE);
  3422. } else if (zend_string_equals_literal(lcname, "strval")) {
  3423. return zend_compile_func_cast(result, args, IS_STRING);
  3424. } else if (zend_string_equals_literal(lcname, "defined")) {
  3425. return zend_compile_func_defined(result, args);
  3426. } else if (zend_string_equals_literal(lcname, "chr") && type == BP_VAR_R) {
  3427. return zend_compile_func_chr(result, args);
  3428. } else if (zend_string_equals_literal(lcname, "ord") && type == BP_VAR_R) {
  3429. return zend_compile_func_ord(result, args);
  3430. } else if (zend_string_equals_literal(lcname, "call_user_func_array")) {
  3431. return zend_compile_func_cufa(result, args, lcname);
  3432. } else if (zend_string_equals_literal(lcname, "call_user_func")) {
  3433. return zend_compile_func_cuf(result, args, lcname);
  3434. } else if (zend_string_equals_literal(lcname, "in_array")) {
  3435. return zend_compile_func_in_array(result, args);
  3436. } else if (zend_string_equals_literal(lcname, "count")
  3437. || zend_string_equals_literal(lcname, "sizeof")) {
  3438. return zend_compile_func_count(result, args, lcname);
  3439. } else if (zend_string_equals_literal(lcname, "get_class")) {
  3440. return zend_compile_func_get_class(result, args);
  3441. } else if (zend_string_equals_literal(lcname, "get_called_class")) {
  3442. return zend_compile_func_get_called_class(result, args);
  3443. } else if (zend_string_equals_literal(lcname, "gettype")) {
  3444. return zend_compile_func_gettype(result, args);
  3445. } else if (zend_string_equals_literal(lcname, "func_num_args")) {
  3446. return zend_compile_func_num_args(result, args);
  3447. } else if (zend_string_equals_literal(lcname, "func_get_args")) {
  3448. return zend_compile_func_get_args(result, args);
  3449. } else if (zend_string_equals_literal(lcname, "array_slice")) {
  3450. return zend_compile_func_array_slice(result, args);
  3451. } else if (zend_string_equals_literal(lcname, "array_key_exists")) {
  3452. return zend_compile_func_array_key_exists(result, args);
  3453. } else {
  3454. return FAILURE;
  3455. }
  3456. }
  3457. /* }}} */
  3458. void zend_compile_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
  3459. {
  3460. zend_ast *name_ast = ast->child[0];
  3461. zend_ast *args_ast = ast->child[1];
  3462. znode name_node;
  3463. if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) {
  3464. zend_compile_expr(&name_node, name_ast);
  3465. zend_compile_dynamic_call(result, &name_node, args_ast);
  3466. return;
  3467. }
  3468. {
  3469. zend_bool runtime_resolution = zend_compile_function_name(&name_node, name_ast);
  3470. if (runtime_resolution) {
  3471. if (zend_string_equals_literal_ci(zend_ast_get_str(name_ast), "assert")) {
  3472. zend_compile_assert(result, zend_ast_get_list(args_ast), Z_STR(name_node.u.constant), NULL);
  3473. } else {
  3474. zend_compile_ns_call(result, &name_node, args_ast);
  3475. }
  3476. return;
  3477. }
  3478. }
  3479. {
  3480. zval *name = &name_node.u.constant;
  3481. zend_string *lcname;
  3482. zend_function *fbc;
  3483. zend_op *opline;
  3484. lcname = zend_string_tolower(Z_STR_P(name));
  3485. fbc = zend_hash_find_ptr(CG(function_table), lcname);
  3486. /* Special assert() handling should apply independently of compiler flags. */
  3487. if (fbc && zend_string_equals_literal(lcname, "assert")) {
  3488. zend_compile_assert(result, zend_ast_get_list(args_ast), lcname, fbc);
  3489. zend_string_release(lcname);
  3490. zval_ptr_dtor(&name_node.u.constant);
  3491. return;
  3492. }
  3493. if (!fbc || !fbc_is_finalized(fbc)
  3494. || (fbc->type == ZEND_INTERNAL_FUNCTION && (CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS))
  3495. || (fbc->type == ZEND_USER_FUNCTION && (CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS))
  3496. || (fbc->type == ZEND_USER_FUNCTION && (CG(compiler_options) & ZEND_COMPILE_IGNORE_OTHER_FILES) && fbc->op_array.filename != CG(active_op_array)->filename)
  3497. ) {
  3498. zend_string_release_ex(lcname, 0);
  3499. zend_compile_dynamic_call(result, &name_node, args_ast);
  3500. return;
  3501. }
  3502. if (zend_try_compile_special_func(result, lcname,
  3503. zend_ast_get_list(args_ast), fbc, type) == SUCCESS
  3504. ) {
  3505. zend_string_release_ex(lcname, 0);
  3506. zval_ptr_dtor(&name_node.u.constant);
  3507. return;
  3508. }
  3509. zval_ptr_dtor(&name_node.u.constant);
  3510. ZVAL_NEW_STR(&name_node.u.constant, lcname);
  3511. opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node);
  3512. opline->result.num = zend_alloc_cache_slot();
  3513. zend_compile_call_common(result, args_ast, fbc);
  3514. }
  3515. }
  3516. /* }}} */
  3517. void zend_compile_method_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
  3518. {
  3519. zend_ast *obj_ast = ast->child[0];
  3520. zend_ast *method_ast = ast->child[1];
  3521. zend_ast *args_ast = ast->child[2];
  3522. znode obj_node, method_node;
  3523. zend_op *opline;
  3524. zend_function *fbc = NULL;
  3525. if (is_this_fetch(obj_ast)) {
  3526. if (this_guaranteed_exists()) {
  3527. obj_node.op_type = IS_UNUSED;
  3528. } else {
  3529. zend_emit_op(&obj_node, ZEND_FETCH_THIS, NULL, NULL);
  3530. }
  3531. CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
  3532. } else {
  3533. zend_compile_expr(&obj_node, obj_ast);
  3534. }
  3535. zend_compile_expr(&method_node, method_ast);
  3536. opline = zend_emit_op(NULL, ZEND_INIT_METHOD_CALL, &obj_node, NULL);
  3537. if (method_node.op_type == IS_CONST) {
  3538. if (Z_TYPE(method_node.u.constant) != IS_STRING) {
  3539. zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string");
  3540. }
  3541. opline->op2_type = IS_CONST;
  3542. opline->op2.constant = zend_add_func_name_literal(
  3543. Z_STR(method_node.u.constant));
  3544. opline->result.num = zend_alloc_cache_slots(2);
  3545. } else {
  3546. SET_NODE(opline->op2, &method_node);
  3547. }
  3548. /* Check if this calls a known method on $this */
  3549. if (opline->op1_type == IS_UNUSED && opline->op2_type == IS_CONST &&
  3550. CG(active_class_entry) && zend_is_scope_known()) {
  3551. zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op2) + 1);
  3552. fbc = zend_hash_find_ptr(&CG(active_class_entry)->function_table, lcname);
  3553. /* We only know the exact method that is being called if it is either private or final.
  3554. * Otherwise an overriding method in a child class may be called. */
  3555. if (fbc && !(fbc->common.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_FINAL))) {
  3556. fbc = NULL;
  3557. }
  3558. }
  3559. zend_compile_call_common(result, args_ast, fbc);
  3560. }
  3561. /* }}} */
  3562. static zend_bool zend_is_constructor(zend_string *name) /* {{{ */
  3563. {
  3564. return zend_string_equals_literal_ci(name, ZEND_CONSTRUCTOR_FUNC_NAME);
  3565. }
  3566. /* }}} */
  3567. static zend_function *zend_get_compatible_func_or_null(zend_class_entry *ce, zend_string *lcname) /* {{{ */
  3568. {
  3569. zend_function *fbc = zend_hash_find_ptr(&ce->function_table, lcname);
  3570. if (!fbc || (fbc->common.fn_flags & ZEND_ACC_PUBLIC) || ce == CG(active_class_entry)) {
  3571. return fbc;
  3572. }
  3573. if (!(fbc->common.fn_flags & ZEND_ACC_PRIVATE)
  3574. && (fbc->common.scope->ce_flags & ZEND_ACC_LINKED)
  3575. && (!CG(active_class_entry) || (CG(active_class_entry)->ce_flags & ZEND_ACC_LINKED))
  3576. && zend_check_protected(zend_get_function_root_class(fbc), CG(active_class_entry))) {
  3577. return fbc;
  3578. }
  3579. return NULL;
  3580. }
  3581. /* }}} */
  3582. void zend_compile_static_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
  3583. {
  3584. zend_ast *class_ast = ast->child[0];
  3585. zend_ast *method_ast = ast->child[1];
  3586. zend_ast *args_ast = ast->child[2];
  3587. znode class_node, method_node;
  3588. zend_op *opline;
  3589. zend_function *fbc = NULL;
  3590. zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
  3591. zend_compile_expr(&method_node, method_ast);
  3592. if (method_node.op_type == IS_CONST) {
  3593. zval *name = &method_node.u.constant;
  3594. if (Z_TYPE_P(name) != IS_STRING) {
  3595. zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string");
  3596. }
  3597. if (zend_is_constructor(Z_STR_P(name))) {
  3598. zval_ptr_dtor(name);
  3599. method_node.op_type = IS_UNUSED;
  3600. }
  3601. }
  3602. opline = get_next_op();
  3603. opline->opcode = ZEND_INIT_STATIC_METHOD_CALL;
  3604. zend_set_class_name_op1(opline, &class_node);
  3605. if (method_node.op_type == IS_CONST) {
  3606. opline->op2_type = IS_CONST;
  3607. opline->op2.constant = zend_add_func_name_literal(
  3608. Z_STR(method_node.u.constant));
  3609. opline->result.num = zend_alloc_cache_slots(2);
  3610. } else {
  3611. if (opline->op1_type == IS_CONST) {
  3612. opline->result.num = zend_alloc_cache_slot();
  3613. }
  3614. SET_NODE(opline->op2, &method_node);
  3615. }
  3616. /* Check if we already know which method we're calling */
  3617. if (opline->op2_type == IS_CONST) {
  3618. zend_class_entry *ce = NULL;
  3619. if (opline->op1_type == IS_CONST) {
  3620. zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op1) + 1);
  3621. ce = zend_hash_find_ptr(CG(class_table), lcname);
  3622. if (!ce && CG(active_class_entry)
  3623. && zend_string_equals_ci(CG(active_class_entry)->name, lcname)) {
  3624. ce = CG(active_class_entry);
  3625. }
  3626. } else if (opline->op1_type == IS_UNUSED
  3627. && (opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF
  3628. && zend_is_scope_known()) {
  3629. ce = CG(active_class_entry);
  3630. }
  3631. if (ce) {
  3632. zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op2) + 1);
  3633. fbc = zend_get_compatible_func_or_null(ce, lcname);
  3634. }
  3635. }
  3636. zend_compile_call_common(result, args_ast, fbc);
  3637. }
  3638. /* }}} */
  3639. void zend_compile_class_decl(znode *result, zend_ast *ast, zend_bool toplevel);
  3640. void zend_compile_new(znode *result, zend_ast *ast) /* {{{ */
  3641. {
  3642. zend_ast *class_ast = ast->child[0];
  3643. zend_ast *args_ast = ast->child[1];
  3644. znode class_node, ctor_result;
  3645. zend_op *opline;
  3646. if (class_ast->kind == ZEND_AST_CLASS) {
  3647. /* anon class declaration */
  3648. zend_compile_class_decl(&class_node, class_ast, 0);
  3649. } else {
  3650. zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
  3651. }
  3652. opline = zend_emit_op(result, ZEND_NEW, NULL, NULL);
  3653. if (class_node.op_type == IS_CONST) {
  3654. opline->op1_type = IS_CONST;
  3655. opline->op1.constant = zend_add_class_name_literal(
  3656. Z_STR(class_node.u.constant));
  3657. opline->op2.num = zend_alloc_cache_slot();
  3658. } else {
  3659. SET_NODE(opline->op1, &class_node);
  3660. }
  3661. zend_compile_call_common(&ctor_result, args_ast, NULL);
  3662. zend_do_free(&ctor_result);
  3663. }
  3664. /* }}} */
  3665. void zend_compile_clone(znode *result, zend_ast *ast) /* {{{ */
  3666. {
  3667. zend_ast *obj_ast = ast->child[0];
  3668. znode obj_node;
  3669. zend_compile_expr(&obj_node, obj_ast);
  3670. zend_emit_op_tmp(result, ZEND_CLONE, &obj_node, NULL);
  3671. }
  3672. /* }}} */
  3673. void zend_compile_global_var(zend_ast *ast) /* {{{ */
  3674. {
  3675. zend_ast *var_ast = ast->child[0];
  3676. zend_ast *name_ast = var_ast->child[0];
  3677. znode name_node, result;
  3678. zend_compile_expr(&name_node, name_ast);
  3679. if (name_node.op_type == IS_CONST) {
  3680. convert_to_string(&name_node.u.constant);
  3681. }
  3682. if (is_this_fetch(var_ast)) {
  3683. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as global variable");
  3684. } else if (zend_try_compile_cv(&result, var_ast) == SUCCESS) {
  3685. zend_op *opline = zend_emit_op(NULL, ZEND_BIND_GLOBAL, &result, &name_node);
  3686. opline->extended_value = zend_alloc_cache_slot();
  3687. } else {
  3688. /* name_ast should be evaluated only. FETCH_GLOBAL_LOCK instructs FETCH_W
  3689. * to not free the name_node operand, so it can be reused in the following
  3690. * ASSIGN_REF, which then frees it. */
  3691. zend_op *opline = zend_emit_op(&result, ZEND_FETCH_W, &name_node, NULL);
  3692. opline->extended_value = ZEND_FETCH_GLOBAL_LOCK;
  3693. if (name_node.op_type == IS_CONST) {
  3694. zend_string_addref(Z_STR(name_node.u.constant));
  3695. }
  3696. zend_emit_assign_ref_znode(
  3697. zend_ast_create(ZEND_AST_VAR, zend_ast_create_znode(&name_node)),
  3698. &result
  3699. );
  3700. }
  3701. }
  3702. /* }}} */
  3703. static void zend_compile_static_var_common(zend_string *var_name, zval *value, uint32_t mode) /* {{{ */
  3704. {
  3705. zend_op *opline;
  3706. if (!CG(active_op_array)->static_variables) {
  3707. if (CG(active_op_array)->scope) {
  3708. CG(active_op_array)->scope->ce_flags |= ZEND_HAS_STATIC_IN_METHODS;
  3709. }
  3710. CG(active_op_array)->static_variables = zend_new_array(8);
  3711. }
  3712. value = zend_hash_update(CG(active_op_array)->static_variables, var_name, value);
  3713. if (zend_string_equals_literal(var_name, "this")) {
  3714. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as static variable");
  3715. }
  3716. opline = zend_emit_op(NULL, ZEND_BIND_STATIC, NULL, NULL);
  3717. opline->op1_type = IS_CV;
  3718. opline->op1.var = lookup_cv(var_name);
  3719. opline->extended_value = (uint32_t)((char*)value - (char*)CG(active_op_array)->static_variables->arData) | mode;
  3720. }
  3721. /* }}} */
  3722. void zend_compile_static_var(zend_ast *ast) /* {{{ */
  3723. {
  3724. zend_ast *var_ast = ast->child[0];
  3725. zend_ast *value_ast = ast->child[1];
  3726. zval value_zv;
  3727. if (value_ast) {
  3728. zend_const_expr_to_zval(&value_zv, value_ast);
  3729. } else {
  3730. ZVAL_NULL(&value_zv);
  3731. }
  3732. zend_compile_static_var_common(zend_ast_get_str(var_ast), &value_zv, ZEND_BIND_REF);
  3733. }
  3734. /* }}} */
  3735. void zend_compile_unset(zend_ast *ast) /* {{{ */
  3736. {
  3737. zend_ast *var_ast = ast->child[0];
  3738. znode var_node;
  3739. zend_op *opline;
  3740. zend_ensure_writable_variable(var_ast);
  3741. switch (var_ast->kind) {
  3742. case ZEND_AST_VAR:
  3743. if (is_this_fetch(var_ast)) {
  3744. zend_error_noreturn(E_COMPILE_ERROR, "Cannot unset $this");
  3745. } else if (zend_try_compile_cv(&var_node, var_ast) == SUCCESS) {
  3746. opline = zend_emit_op(NULL, ZEND_UNSET_CV, &var_node, NULL);
  3747. } else {
  3748. opline = zend_compile_simple_var_no_cv(NULL, var_ast, BP_VAR_UNSET, 0);
  3749. opline->opcode = ZEND_UNSET_VAR;
  3750. }
  3751. return;
  3752. case ZEND_AST_DIM:
  3753. opline = zend_compile_dim(NULL, var_ast, BP_VAR_UNSET);
  3754. opline->opcode = ZEND_UNSET_DIM;
  3755. return;
  3756. case ZEND_AST_PROP:
  3757. opline = zend_compile_prop(NULL, var_ast, BP_VAR_UNSET, 0);
  3758. opline->opcode = ZEND_UNSET_OBJ;
  3759. return;
  3760. case ZEND_AST_STATIC_PROP:
  3761. opline = zend_compile_static_prop(NULL, var_ast, BP_VAR_UNSET, 0, 0);
  3762. opline->opcode = ZEND_UNSET_STATIC_PROP;
  3763. return;
  3764. EMPTY_SWITCH_DEFAULT_CASE()
  3765. }
  3766. }
  3767. /* }}} */
  3768. static int zend_handle_loops_and_finally_ex(zend_long depth, znode *return_value) /* {{{ */
  3769. {
  3770. zend_loop_var *base;
  3771. zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack));
  3772. if (!loop_var) {
  3773. return 1;
  3774. }
  3775. base = zend_stack_base(&CG(loop_var_stack));
  3776. for (; loop_var >= base; loop_var--) {
  3777. if (loop_var->opcode == ZEND_FAST_CALL) {
  3778. zend_op *opline = get_next_op();
  3779. opline->opcode = ZEND_FAST_CALL;
  3780. opline->result_type = IS_TMP_VAR;
  3781. opline->result.var = loop_var->var_num;
  3782. if (return_value) {
  3783. SET_NODE(opline->op2, return_value);
  3784. }
  3785. opline->op1.num = loop_var->try_catch_offset;
  3786. } else if (loop_var->opcode == ZEND_DISCARD_EXCEPTION) {
  3787. zend_op *opline = get_next_op();
  3788. opline->opcode = ZEND_DISCARD_EXCEPTION;
  3789. opline->op1_type = IS_TMP_VAR;
  3790. opline->op1.var = loop_var->var_num;
  3791. } else if (loop_var->opcode == ZEND_RETURN) {
  3792. /* Stack separator */
  3793. break;
  3794. } else if (depth <= 1) {
  3795. return 1;
  3796. } else if (loop_var->opcode == ZEND_NOP) {
  3797. /* Loop doesn't have freeable variable */
  3798. depth--;
  3799. } else {
  3800. zend_op *opline;
  3801. ZEND_ASSERT(loop_var->var_type & (IS_VAR|IS_TMP_VAR));
  3802. opline = get_next_op();
  3803. opline->opcode = loop_var->opcode;
  3804. opline->op1_type = loop_var->var_type;
  3805. opline->op1.var = loop_var->var_num;
  3806. opline->extended_value = ZEND_FREE_ON_RETURN;
  3807. depth--;
  3808. }
  3809. }
  3810. return (depth == 0);
  3811. }
  3812. /* }}} */
  3813. static int zend_handle_loops_and_finally(znode *return_value) /* {{{ */
  3814. {
  3815. return zend_handle_loops_and_finally_ex(zend_stack_count(&CG(loop_var_stack)) + 1, return_value);
  3816. }
  3817. /* }}} */
  3818. static int zend_has_finally_ex(zend_long depth) /* {{{ */
  3819. {
  3820. zend_loop_var *base;
  3821. zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack));
  3822. if (!loop_var) {
  3823. return 0;
  3824. }
  3825. base = zend_stack_base(&CG(loop_var_stack));
  3826. for (; loop_var >= base; loop_var--) {
  3827. if (loop_var->opcode == ZEND_FAST_CALL) {
  3828. return 1;
  3829. } else if (loop_var->opcode == ZEND_DISCARD_EXCEPTION) {
  3830. } else if (loop_var->opcode == ZEND_RETURN) {
  3831. /* Stack separator */
  3832. return 0;
  3833. } else if (depth <= 1) {
  3834. return 0;
  3835. } else {
  3836. depth--;
  3837. }
  3838. }
  3839. return 0;
  3840. }
  3841. /* }}} */
  3842. static int zend_has_finally(void) /* {{{ */
  3843. {
  3844. return zend_has_finally_ex(zend_stack_count(&CG(loop_var_stack)) + 1);
  3845. }
  3846. /* }}} */
  3847. void zend_compile_return(zend_ast *ast) /* {{{ */
  3848. {
  3849. zend_ast *expr_ast = ast->child[0];
  3850. zend_bool is_generator = (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) != 0;
  3851. zend_bool by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
  3852. znode expr_node;
  3853. zend_op *opline;
  3854. if (is_generator) {
  3855. /* For generators the by-ref flag refers to yields, not returns */
  3856. by_ref = 0;
  3857. }
  3858. if (!expr_ast) {
  3859. expr_node.op_type = IS_CONST;
  3860. ZVAL_NULL(&expr_node.u.constant);
  3861. } else if (by_ref && zend_is_variable(expr_ast)) {
  3862. zend_compile_var(&expr_node, expr_ast, BP_VAR_W, 1);
  3863. } else {
  3864. zend_compile_expr(&expr_node, expr_ast);
  3865. }
  3866. if ((CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)
  3867. && (expr_node.op_type == IS_CV || (by_ref && expr_node.op_type == IS_VAR))
  3868. && zend_has_finally()) {
  3869. /* Copy return value into temporary VAR to avoid modification in finally code */
  3870. if (by_ref) {
  3871. zend_emit_op(&expr_node, ZEND_MAKE_REF, &expr_node, NULL);
  3872. } else {
  3873. zend_emit_op_tmp(&expr_node, ZEND_QM_ASSIGN, &expr_node, NULL);
  3874. }
  3875. }
  3876. /* Generator return types are handled separately */
  3877. if (!is_generator && CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
  3878. zend_emit_return_type_check(
  3879. expr_ast ? &expr_node : NULL, CG(active_op_array)->arg_info - 1, 0);
  3880. }
  3881. zend_handle_loops_and_finally((expr_node.op_type & (IS_TMP_VAR | IS_VAR)) ? &expr_node : NULL);
  3882. opline = zend_emit_op(NULL, by_ref ? ZEND_RETURN_BY_REF : ZEND_RETURN,
  3883. &expr_node, NULL);
  3884. if (by_ref && expr_ast) {
  3885. if (zend_is_call(expr_ast)) {
  3886. opline->extended_value = ZEND_RETURNS_FUNCTION;
  3887. } else if (!zend_is_variable(expr_ast)) {
  3888. opline->extended_value = ZEND_RETURNS_VALUE;
  3889. }
  3890. }
  3891. }
  3892. /* }}} */
  3893. void zend_compile_echo(zend_ast *ast) /* {{{ */
  3894. {
  3895. zend_op *opline;
  3896. zend_ast *expr_ast = ast->child[0];
  3897. znode expr_node;
  3898. zend_compile_expr(&expr_node, expr_ast);
  3899. opline = zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL);
  3900. opline->extended_value = 0;
  3901. }
  3902. /* }}} */
  3903. void zend_compile_throw(znode *result, zend_ast *ast) /* {{{ */
  3904. {
  3905. zend_ast *expr_ast = ast->child[0];
  3906. znode expr_node;
  3907. zend_compile_expr(&expr_node, expr_ast);
  3908. zend_op *opline = zend_emit_op(NULL, ZEND_THROW, &expr_node, NULL);
  3909. if (result) {
  3910. /* Mark this as an "expression throw" for opcache. */
  3911. opline->extended_value = ZEND_THROW_IS_EXPR;
  3912. result->op_type = IS_CONST;
  3913. ZVAL_BOOL(&result->u.constant, 1);
  3914. }
  3915. }
  3916. /* }}} */
  3917. void zend_compile_break_continue(zend_ast *ast) /* {{{ */
  3918. {
  3919. zend_ast *depth_ast = ast->child[0];
  3920. zend_op *opline;
  3921. zend_long depth;
  3922. ZEND_ASSERT(ast->kind == ZEND_AST_BREAK || ast->kind == ZEND_AST_CONTINUE);
  3923. if (depth_ast) {
  3924. zval *depth_zv;
  3925. if (depth_ast->kind != ZEND_AST_ZVAL) {
  3926. zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator with non-integer operand "
  3927. "is no longer supported", ast->kind == ZEND_AST_BREAK ? "break" : "continue");
  3928. }
  3929. depth_zv = zend_ast_get_zval(depth_ast);
  3930. if (Z_TYPE_P(depth_zv) != IS_LONG || Z_LVAL_P(depth_zv) < 1) {
  3931. zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator accepts only positive integers",
  3932. ast->kind == ZEND_AST_BREAK ? "break" : "continue");
  3933. }
  3934. depth = Z_LVAL_P(depth_zv);
  3935. } else {
  3936. depth = 1;
  3937. }
  3938. if (CG(context).current_brk_cont == -1) {
  3939. zend_error_noreturn(E_COMPILE_ERROR, "'%s' not in the 'loop' or 'switch' context",
  3940. ast->kind == ZEND_AST_BREAK ? "break" : "continue");
  3941. } else {
  3942. if (!zend_handle_loops_and_finally_ex(depth, NULL)) {
  3943. zend_error_noreturn(E_COMPILE_ERROR, "Cannot '%s' " ZEND_LONG_FMT " level%s",
  3944. ast->kind == ZEND_AST_BREAK ? "break" : "continue",
  3945. depth, depth == 1 ? "" : "s");
  3946. }
  3947. }
  3948. if (ast->kind == ZEND_AST_CONTINUE) {
  3949. int d, cur = CG(context).current_brk_cont;
  3950. for (d = depth - 1; d > 0; d--) {
  3951. cur = CG(context).brk_cont_array[cur].parent;
  3952. ZEND_ASSERT(cur != -1);
  3953. }
  3954. if (CG(context).brk_cont_array[cur].is_switch) {
  3955. if (depth == 1) {
  3956. zend_error(E_WARNING,
  3957. "\"continue\" targeting switch is equivalent to \"break\". " \
  3958. "Did you mean to use \"continue " ZEND_LONG_FMT "\"?",
  3959. depth + 1);
  3960. } else {
  3961. zend_error(E_WARNING,
  3962. "\"continue " ZEND_LONG_FMT "\" targeting switch is equivalent to \"break " ZEND_LONG_FMT "\". " \
  3963. "Did you mean to use \"continue " ZEND_LONG_FMT "\"?",
  3964. depth, depth, depth + 1);
  3965. }
  3966. }
  3967. }
  3968. opline = zend_emit_op(NULL, ast->kind == ZEND_AST_BREAK ? ZEND_BRK : ZEND_CONT, NULL, NULL);
  3969. opline->op1.num = CG(context).current_brk_cont;
  3970. opline->op2.num = depth;
  3971. }
  3972. /* }}} */
  3973. void zend_resolve_goto_label(zend_op_array *op_array, zend_op *opline) /* {{{ */
  3974. {
  3975. zend_label *dest;
  3976. int current, remove_oplines = opline->op1.num;
  3977. zval *label;
  3978. uint32_t opnum = opline - op_array->opcodes;
  3979. label = CT_CONSTANT_EX(op_array, opline->op2.constant);
  3980. if (CG(context).labels == NULL ||
  3981. (dest = zend_hash_find_ptr(CG(context).labels, Z_STR_P(label))) == NULL
  3982. ) {
  3983. CG(in_compilation) = 1;
  3984. CG(active_op_array) = op_array;
  3985. CG(zend_lineno) = opline->lineno;
  3986. zend_error_noreturn(E_COMPILE_ERROR, "'goto' to undefined label '%s'", Z_STRVAL_P(label));
  3987. }
  3988. zval_ptr_dtor_str(label);
  3989. ZVAL_NULL(label);
  3990. current = opline->extended_value;
  3991. for (; current != dest->brk_cont; current = CG(context).brk_cont_array[current].parent) {
  3992. if (current == -1) {
  3993. CG(in_compilation) = 1;
  3994. CG(active_op_array) = op_array;
  3995. CG(zend_lineno) = opline->lineno;
  3996. zend_error_noreturn(E_COMPILE_ERROR, "'goto' into loop or switch statement is disallowed");
  3997. }
  3998. if (CG(context).brk_cont_array[current].start >= 0) {
  3999. remove_oplines--;
  4000. }
  4001. }
  4002. for (current = 0; current < op_array->last_try_catch; ++current) {
  4003. zend_try_catch_element *elem = &op_array->try_catch_array[current];
  4004. if (elem->try_op > opnum) {
  4005. break;
  4006. }
  4007. if (elem->finally_op && opnum < elem->finally_op - 1
  4008. && (dest->opline_num > elem->finally_end || dest->opline_num < elem->try_op)
  4009. ) {
  4010. remove_oplines--;
  4011. }
  4012. }
  4013. opline->opcode = ZEND_JMP;
  4014. opline->op1.opline_num = dest->opline_num;
  4015. opline->extended_value = 0;
  4016. SET_UNUSED(opline->op1);
  4017. SET_UNUSED(opline->op2);
  4018. SET_UNUSED(opline->result);
  4019. ZEND_ASSERT(remove_oplines >= 0);
  4020. while (remove_oplines--) {
  4021. opline--;
  4022. MAKE_NOP(opline);
  4023. ZEND_VM_SET_OPCODE_HANDLER(opline);
  4024. }
  4025. }
  4026. /* }}} */
  4027. void zend_compile_goto(zend_ast *ast) /* {{{ */
  4028. {
  4029. zend_ast *label_ast = ast->child[0];
  4030. znode label_node;
  4031. zend_op *opline;
  4032. uint32_t opnum_start = get_next_op_number();
  4033. zend_compile_expr(&label_node, label_ast);
  4034. /* Label resolution and unwinding adjustments happen in pass two. */
  4035. zend_handle_loops_and_finally(NULL);
  4036. opline = zend_emit_op(NULL, ZEND_GOTO, NULL, &label_node);
  4037. opline->op1.num = get_next_op_number() - opnum_start - 1;
  4038. opline->extended_value = CG(context).current_brk_cont;
  4039. }
  4040. /* }}} */
  4041. void zend_compile_label(zend_ast *ast) /* {{{ */
  4042. {
  4043. zend_string *label = zend_ast_get_str(ast->child[0]);
  4044. zend_label dest;
  4045. if (!CG(context).labels) {
  4046. ALLOC_HASHTABLE(CG(context).labels);
  4047. zend_hash_init(CG(context).labels, 8, NULL, label_ptr_dtor, 0);
  4048. }
  4049. dest.brk_cont = CG(context).current_brk_cont;
  4050. dest.opline_num = get_next_op_number();
  4051. if (!zend_hash_add_mem(CG(context).labels, label, &dest, sizeof(zend_label))) {
  4052. zend_error_noreturn(E_COMPILE_ERROR, "Label '%s' already defined", ZSTR_VAL(label));
  4053. }
  4054. }
  4055. /* }}} */
  4056. void zend_compile_while(zend_ast *ast) /* {{{ */
  4057. {
  4058. zend_ast *cond_ast = ast->child[0];
  4059. zend_ast *stmt_ast = ast->child[1];
  4060. znode cond_node;
  4061. uint32_t opnum_start, opnum_jmp, opnum_cond;
  4062. opnum_jmp = zend_emit_jump(0);
  4063. zend_begin_loop(ZEND_NOP, NULL, 0);
  4064. opnum_start = get_next_op_number();
  4065. zend_compile_stmt(stmt_ast);
  4066. opnum_cond = get_next_op_number();
  4067. zend_update_jump_target(opnum_jmp, opnum_cond);
  4068. zend_compile_expr(&cond_node, cond_ast);
  4069. zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start);
  4070. zend_end_loop(opnum_cond, NULL);
  4071. }
  4072. /* }}} */
  4073. void zend_compile_do_while(zend_ast *ast) /* {{{ */
  4074. {
  4075. zend_ast *stmt_ast = ast->child[0];
  4076. zend_ast *cond_ast = ast->child[1];
  4077. znode cond_node;
  4078. uint32_t opnum_start, opnum_cond;
  4079. zend_begin_loop(ZEND_NOP, NULL, 0);
  4080. opnum_start = get_next_op_number();
  4081. zend_compile_stmt(stmt_ast);
  4082. opnum_cond = get_next_op_number();
  4083. zend_compile_expr(&cond_node, cond_ast);
  4084. zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start);
  4085. zend_end_loop(opnum_cond, NULL);
  4086. }
  4087. /* }}} */
  4088. void zend_compile_expr_list(znode *result, zend_ast *ast) /* {{{ */
  4089. {
  4090. zend_ast_list *list;
  4091. uint32_t i;
  4092. result->op_type = IS_CONST;
  4093. ZVAL_TRUE(&result->u.constant);
  4094. if (!ast) {
  4095. return;
  4096. }
  4097. list = zend_ast_get_list(ast);
  4098. for (i = 0; i < list->children; ++i) {
  4099. zend_ast *expr_ast = list->child[i];
  4100. zend_do_free(result);
  4101. zend_compile_expr(result, expr_ast);
  4102. }
  4103. }
  4104. /* }}} */
  4105. void zend_compile_for(zend_ast *ast) /* {{{ */
  4106. {
  4107. zend_ast *init_ast = ast->child[0];
  4108. zend_ast *cond_ast = ast->child[1];
  4109. zend_ast *loop_ast = ast->child[2];
  4110. zend_ast *stmt_ast = ast->child[3];
  4111. znode result;
  4112. uint32_t opnum_start, opnum_jmp, opnum_loop;
  4113. zend_compile_expr_list(&result, init_ast);
  4114. zend_do_free(&result);
  4115. opnum_jmp = zend_emit_jump(0);
  4116. zend_begin_loop(ZEND_NOP, NULL, 0);
  4117. opnum_start = get_next_op_number();
  4118. zend_compile_stmt(stmt_ast);
  4119. opnum_loop = get_next_op_number();
  4120. zend_compile_expr_list(&result, loop_ast);
  4121. zend_do_free(&result);
  4122. zend_update_jump_target_to_next(opnum_jmp);
  4123. zend_compile_expr_list(&result, cond_ast);
  4124. zend_do_extended_stmt();
  4125. zend_emit_cond_jump(ZEND_JMPNZ, &result, opnum_start);
  4126. zend_end_loop(opnum_loop, NULL);
  4127. }
  4128. /* }}} */
  4129. void zend_compile_foreach(zend_ast *ast) /* {{{ */
  4130. {
  4131. zend_ast *expr_ast = ast->child[0];
  4132. zend_ast *value_ast = ast->child[1];
  4133. zend_ast *key_ast = ast->child[2];
  4134. zend_ast *stmt_ast = ast->child[3];
  4135. zend_bool by_ref = value_ast->kind == ZEND_AST_REF;
  4136. zend_bool is_variable = zend_is_variable(expr_ast) && zend_can_write_to_variable(expr_ast);
  4137. znode expr_node, reset_node, value_node, key_node;
  4138. zend_op *opline;
  4139. uint32_t opnum_reset, opnum_fetch;
  4140. if (key_ast) {
  4141. if (key_ast->kind == ZEND_AST_REF) {
  4142. zend_error_noreturn(E_COMPILE_ERROR, "Key element cannot be a reference");
  4143. }
  4144. if (key_ast->kind == ZEND_AST_ARRAY) {
  4145. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use list as key element");
  4146. }
  4147. }
  4148. if (by_ref) {
  4149. value_ast = value_ast->child[0];
  4150. }
  4151. if (value_ast->kind == ZEND_AST_ARRAY && zend_propagate_list_refs(value_ast)) {
  4152. by_ref = 1;
  4153. }
  4154. if (by_ref && is_variable) {
  4155. zend_compile_var(&expr_node, expr_ast, BP_VAR_W, 1);
  4156. } else {
  4157. zend_compile_expr(&expr_node, expr_ast);
  4158. }
  4159. if (by_ref) {
  4160. zend_separate_if_call_and_write(&expr_node, expr_ast, BP_VAR_W);
  4161. }
  4162. opnum_reset = get_next_op_number();
  4163. opline = zend_emit_op(&reset_node, by_ref ? ZEND_FE_RESET_RW : ZEND_FE_RESET_R, &expr_node, NULL);
  4164. zend_begin_loop(ZEND_FE_FREE, &reset_node, 0);
  4165. opnum_fetch = get_next_op_number();
  4166. opline = zend_emit_op(NULL, by_ref ? ZEND_FE_FETCH_RW : ZEND_FE_FETCH_R, &reset_node, NULL);
  4167. if (is_this_fetch(value_ast)) {
  4168. zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
  4169. } else if (value_ast->kind == ZEND_AST_VAR &&
  4170. zend_try_compile_cv(&value_node, value_ast) == SUCCESS) {
  4171. SET_NODE(opline->op2, &value_node);
  4172. } else {
  4173. opline->op2_type = IS_VAR;
  4174. opline->op2.var = get_temporary_variable();
  4175. GET_NODE(&value_node, opline->op2);
  4176. if (value_ast->kind == ZEND_AST_ARRAY) {
  4177. zend_compile_list_assign(NULL, value_ast, &value_node, value_ast->attr);
  4178. } else if (by_ref) {
  4179. zend_emit_assign_ref_znode(value_ast, &value_node);
  4180. } else {
  4181. zend_emit_assign_znode(value_ast, &value_node);
  4182. }
  4183. }
  4184. if (key_ast) {
  4185. opline = &CG(active_op_array)->opcodes[opnum_fetch];
  4186. zend_make_tmp_result(&key_node, opline);
  4187. zend_emit_assign_znode(key_ast, &key_node);
  4188. }
  4189. zend_compile_stmt(stmt_ast);
  4190. /* Place JMP and FE_FREE on the line where foreach starts. It would be
  4191. * better to use the end line, but this information is not available
  4192. * currently. */
  4193. CG(zend_lineno) = ast->lineno;
  4194. zend_emit_jump(opnum_fetch);
  4195. opline = &CG(active_op_array)->opcodes[opnum_reset];
  4196. opline->op2.opline_num = get_next_op_number();
  4197. opline = &CG(active_op_array)->opcodes[opnum_fetch];
  4198. opline->extended_value = get_next_op_number();
  4199. zend_end_loop(opnum_fetch, &reset_node);
  4200. opline = zend_emit_op(NULL, ZEND_FE_FREE, &reset_node, NULL);
  4201. }
  4202. /* }}} */
  4203. void zend_compile_if(zend_ast *ast) /* {{{ */
  4204. {
  4205. zend_ast_list *list = zend_ast_get_list(ast);
  4206. uint32_t i;
  4207. uint32_t *jmp_opnums = NULL;
  4208. if (list->children > 1) {
  4209. jmp_opnums = safe_emalloc(sizeof(uint32_t), list->children - 1, 0);
  4210. }
  4211. for (i = 0; i < list->children; ++i) {
  4212. zend_ast *elem_ast = list->child[i];
  4213. zend_ast *cond_ast = elem_ast->child[0];
  4214. zend_ast *stmt_ast = elem_ast->child[1];
  4215. if (cond_ast) {
  4216. znode cond_node;
  4217. uint32_t opnum_jmpz;
  4218. zend_compile_expr(&cond_node, cond_ast);
  4219. opnum_jmpz = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
  4220. zend_compile_stmt(stmt_ast);
  4221. if (i != list->children - 1) {
  4222. jmp_opnums[i] = zend_emit_jump(0);
  4223. }
  4224. zend_update_jump_target_to_next(opnum_jmpz);
  4225. } else {
  4226. /* "else" can only occur as last element. */
  4227. ZEND_ASSERT(i == list->children - 1);
  4228. zend_compile_stmt(stmt_ast);
  4229. }
  4230. }
  4231. if (list->children > 1) {
  4232. for (i = 0; i < list->children - 1; ++i) {
  4233. zend_update_jump_target_to_next(jmp_opnums[i]);
  4234. }
  4235. efree(jmp_opnums);
  4236. }
  4237. }
  4238. /* }}} */
  4239. static zend_uchar determine_switch_jumptable_type(zend_ast_list *cases) {
  4240. uint32_t i;
  4241. zend_uchar common_type = IS_UNDEF;
  4242. for (i = 0; i < cases->children; i++) {
  4243. zend_ast *case_ast = cases->child[i];
  4244. zend_ast **cond_ast = &case_ast->child[0];
  4245. zval *cond_zv;
  4246. if (!case_ast->child[0]) {
  4247. /* Skip default clause */
  4248. continue;
  4249. }
  4250. zend_eval_const_expr(cond_ast);
  4251. if ((*cond_ast)->kind != ZEND_AST_ZVAL) {
  4252. /* Non-constant case */
  4253. return IS_UNDEF;
  4254. }
  4255. cond_zv = zend_ast_get_zval(case_ast->child[0]);
  4256. if (Z_TYPE_P(cond_zv) != IS_LONG && Z_TYPE_P(cond_zv) != IS_STRING) {
  4257. /* We only optimize switched on integers and strings */
  4258. return IS_UNDEF;
  4259. }
  4260. if (common_type == IS_UNDEF) {
  4261. common_type = Z_TYPE_P(cond_zv);
  4262. } else if (common_type != Z_TYPE_P(cond_zv)) {
  4263. /* Non-uniform case types */
  4264. return IS_UNDEF;
  4265. }
  4266. if (Z_TYPE_P(cond_zv) == IS_STRING
  4267. && is_numeric_string(Z_STRVAL_P(cond_zv), Z_STRLEN_P(cond_zv), NULL, NULL, 0)) {
  4268. /* Numeric strings cannot be compared with a simple hash lookup */
  4269. return IS_UNDEF;
  4270. }
  4271. }
  4272. return common_type;
  4273. }
  4274. static zend_bool should_use_jumptable(zend_ast_list *cases, zend_uchar jumptable_type) {
  4275. if (CG(compiler_options) & ZEND_COMPILE_NO_JUMPTABLES) {
  4276. return 0;
  4277. }
  4278. /* Thresholds are chosen based on when the average switch time for equidistributed
  4279. * input becomes smaller when using the jumptable optimization. */
  4280. if (jumptable_type == IS_LONG) {
  4281. return cases->children >= 5;
  4282. } else {
  4283. ZEND_ASSERT(jumptable_type == IS_STRING);
  4284. return cases->children >= 2;
  4285. }
  4286. }
  4287. void zend_compile_switch(zend_ast *ast) /* {{{ */
  4288. {
  4289. zend_ast *expr_ast = ast->child[0];
  4290. zend_ast_list *cases = zend_ast_get_list(ast->child[1]);
  4291. uint32_t i;
  4292. zend_bool has_default_case = 0;
  4293. znode expr_node, case_node;
  4294. zend_op *opline;
  4295. uint32_t *jmpnz_opnums, opnum_default_jmp, opnum_switch = (uint32_t)-1;
  4296. zend_uchar jumptable_type;
  4297. HashTable *jumptable = NULL;
  4298. zend_compile_expr(&expr_node, expr_ast);
  4299. zend_begin_loop(ZEND_FREE, &expr_node, 1);
  4300. case_node.op_type = IS_TMP_VAR;
  4301. case_node.u.op.var = get_temporary_variable();
  4302. jumptable_type = determine_switch_jumptable_type(cases);
  4303. if (jumptable_type != IS_UNDEF && should_use_jumptable(cases, jumptable_type)) {
  4304. znode jumptable_op;
  4305. ALLOC_HASHTABLE(jumptable);
  4306. zend_hash_init(jumptable, cases->children, NULL, NULL, 0);
  4307. jumptable_op.op_type = IS_CONST;
  4308. ZVAL_ARR(&jumptable_op.u.constant, jumptable);
  4309. opline = zend_emit_op(NULL,
  4310. jumptable_type == IS_LONG ? ZEND_SWITCH_LONG : ZEND_SWITCH_STRING,
  4311. &expr_node, &jumptable_op);
  4312. if (opline->op1_type == IS_CONST) {
  4313. Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
  4314. }
  4315. opnum_switch = opline - CG(active_op_array)->opcodes;
  4316. }
  4317. jmpnz_opnums = safe_emalloc(sizeof(uint32_t), cases->children, 0);
  4318. for (i = 0; i < cases->children; ++i) {
  4319. zend_ast *case_ast = cases->child[i];
  4320. zend_ast *cond_ast = case_ast->child[0];
  4321. znode cond_node;
  4322. if (!cond_ast) {
  4323. if (has_default_case) {
  4324. CG(zend_lineno) = case_ast->lineno;
  4325. zend_error_noreturn(E_COMPILE_ERROR,
  4326. "Switch statements may only contain one default clause");
  4327. }
  4328. has_default_case = 1;
  4329. continue;
  4330. }
  4331. zend_compile_expr(&cond_node, cond_ast);
  4332. if (expr_node.op_type == IS_CONST
  4333. && Z_TYPE(expr_node.u.constant) == IS_FALSE) {
  4334. jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
  4335. } else if (expr_node.op_type == IS_CONST
  4336. && Z_TYPE(expr_node.u.constant) == IS_TRUE) {
  4337. jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, 0);
  4338. } else {
  4339. opline = zend_emit_op(NULL,
  4340. (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) ? ZEND_CASE : ZEND_IS_EQUAL,
  4341. &expr_node, &cond_node);
  4342. SET_NODE(opline->result, &case_node);
  4343. if (opline->op1_type == IS_CONST) {
  4344. Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
  4345. }
  4346. jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &case_node, 0);
  4347. }
  4348. }
  4349. opnum_default_jmp = zend_emit_jump(0);
  4350. for (i = 0; i < cases->children; ++i) {
  4351. zend_ast *case_ast = cases->child[i];
  4352. zend_ast *cond_ast = case_ast->child[0];
  4353. zend_ast *stmt_ast = case_ast->child[1];
  4354. if (cond_ast) {
  4355. zend_update_jump_target_to_next(jmpnz_opnums[i]);
  4356. if (jumptable) {
  4357. zval *cond_zv = zend_ast_get_zval(cond_ast);
  4358. zval jmp_target;
  4359. ZVAL_LONG(&jmp_target, get_next_op_number());
  4360. ZEND_ASSERT(Z_TYPE_P(cond_zv) == jumptable_type);
  4361. if (Z_TYPE_P(cond_zv) == IS_LONG) {
  4362. zend_hash_index_add(jumptable, Z_LVAL_P(cond_zv), &jmp_target);
  4363. } else {
  4364. ZEND_ASSERT(Z_TYPE_P(cond_zv) == IS_STRING);
  4365. zend_hash_add(jumptable, Z_STR_P(cond_zv), &jmp_target);
  4366. }
  4367. }
  4368. } else {
  4369. zend_update_jump_target_to_next(opnum_default_jmp);
  4370. if (jumptable) {
  4371. ZEND_ASSERT(opnum_switch != (uint32_t)-1);
  4372. opline = &CG(active_op_array)->opcodes[opnum_switch];
  4373. opline->extended_value = get_next_op_number();
  4374. }
  4375. }
  4376. zend_compile_stmt(stmt_ast);
  4377. }
  4378. if (!has_default_case) {
  4379. zend_update_jump_target_to_next(opnum_default_jmp);
  4380. if (jumptable) {
  4381. opline = &CG(active_op_array)->opcodes[opnum_switch];
  4382. opline->extended_value = get_next_op_number();
  4383. }
  4384. }
  4385. zend_end_loop(get_next_op_number(), &expr_node);
  4386. if (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) {
  4387. opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL);
  4388. opline->extended_value = ZEND_FREE_SWITCH;
  4389. } else if (expr_node.op_type == IS_CONST) {
  4390. zval_ptr_dtor_nogc(&expr_node.u.constant);
  4391. }
  4392. efree(jmpnz_opnums);
  4393. }
  4394. /* }}} */
  4395. void zend_compile_try(zend_ast *ast) /* {{{ */
  4396. {
  4397. zend_ast *try_ast = ast->child[0];
  4398. zend_ast_list *catches = zend_ast_get_list(ast->child[1]);
  4399. zend_ast *finally_ast = ast->child[2];
  4400. uint32_t i, j;
  4401. zend_op *opline;
  4402. uint32_t try_catch_offset;
  4403. uint32_t *jmp_opnums = safe_emalloc(sizeof(uint32_t), catches->children, 0);
  4404. uint32_t orig_fast_call_var = CG(context).fast_call_var;
  4405. uint32_t orig_try_catch_offset = CG(context).try_catch_offset;
  4406. if (catches->children == 0 && !finally_ast) {
  4407. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use try without catch or finally");
  4408. }
  4409. /* label: try { } must not be equal to try { label: } */
  4410. if (CG(context).labels) {
  4411. zend_label *label;
  4412. ZEND_HASH_REVERSE_FOREACH_PTR(CG(context).labels, label) {
  4413. if (label->opline_num == get_next_op_number()) {
  4414. zend_emit_op(NULL, ZEND_NOP, NULL, NULL);
  4415. }
  4416. break;
  4417. } ZEND_HASH_FOREACH_END();
  4418. }
  4419. try_catch_offset = zend_add_try_element(get_next_op_number());
  4420. if (finally_ast) {
  4421. zend_loop_var fast_call;
  4422. if (!(CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)) {
  4423. CG(active_op_array)->fn_flags |= ZEND_ACC_HAS_FINALLY_BLOCK;
  4424. }
  4425. CG(context).fast_call_var = get_temporary_variable();
  4426. /* Push FAST_CALL on unwind stack */
  4427. fast_call.opcode = ZEND_FAST_CALL;
  4428. fast_call.var_type = IS_TMP_VAR;
  4429. fast_call.var_num = CG(context).fast_call_var;
  4430. fast_call.try_catch_offset = try_catch_offset;
  4431. zend_stack_push(&CG(loop_var_stack), &fast_call);
  4432. }
  4433. CG(context).try_catch_offset = try_catch_offset;
  4434. zend_compile_stmt(try_ast);
  4435. if (catches->children != 0) {
  4436. jmp_opnums[0] = zend_emit_jump(0);
  4437. }
  4438. for (i = 0; i < catches->children; ++i) {
  4439. zend_ast *catch_ast = catches->child[i];
  4440. zend_ast_list *classes = zend_ast_get_list(catch_ast->child[0]);
  4441. zend_ast *var_ast = catch_ast->child[1];
  4442. zend_ast *stmt_ast = catch_ast->child[2];
  4443. zend_string *var_name = zval_make_interned_string(zend_ast_get_zval(var_ast));
  4444. zend_bool is_last_catch = (i + 1 == catches->children);
  4445. uint32_t *jmp_multicatch = safe_emalloc(sizeof(uint32_t), classes->children - 1, 0);
  4446. uint32_t opnum_catch = (uint32_t)-1;
  4447. CG(zend_lineno) = catch_ast->lineno;
  4448. for (j = 0; j < classes->children; j++) {
  4449. zend_ast *class_ast = classes->child[j];
  4450. zend_bool is_last_class = (j + 1 == classes->children);
  4451. if (!zend_is_const_default_class_ref(class_ast)) {
  4452. zend_error_noreturn(E_COMPILE_ERROR, "Bad class name in the catch statement");
  4453. }
  4454. opnum_catch = get_next_op_number();
  4455. if (i == 0 && j == 0) {
  4456. CG(active_op_array)->try_catch_array[try_catch_offset].catch_op = opnum_catch;
  4457. }
  4458. opline = get_next_op();
  4459. opline->opcode = ZEND_CATCH;
  4460. opline->op1_type = IS_CONST;
  4461. opline->op1.constant = zend_add_class_name_literal(
  4462. zend_resolve_class_name_ast(class_ast));
  4463. opline->extended_value = zend_alloc_cache_slot();
  4464. if (zend_string_equals_literal(var_name, "this")) {
  4465. zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
  4466. }
  4467. opline->result_type = IS_CV;
  4468. opline->result.var = lookup_cv(var_name);
  4469. if (is_last_catch && is_last_class) {
  4470. opline->extended_value |= ZEND_LAST_CATCH;
  4471. }
  4472. if (!is_last_class) {
  4473. jmp_multicatch[j] = zend_emit_jump(0);
  4474. opline = &CG(active_op_array)->opcodes[opnum_catch];
  4475. opline->op2.opline_num = get_next_op_number();
  4476. }
  4477. }
  4478. for (j = 0; j < classes->children - 1; j++) {
  4479. zend_update_jump_target_to_next(jmp_multicatch[j]);
  4480. }
  4481. efree(jmp_multicatch);
  4482. zend_compile_stmt(stmt_ast);
  4483. if (!is_last_catch) {
  4484. jmp_opnums[i + 1] = zend_emit_jump(0);
  4485. }
  4486. ZEND_ASSERT(opnum_catch != (uint32_t)-1 && "Should have at least one class");
  4487. opline = &CG(active_op_array)->opcodes[opnum_catch];
  4488. if (!is_last_catch) {
  4489. opline->op2.opline_num = get_next_op_number();
  4490. }
  4491. }
  4492. for (i = 0; i < catches->children; ++i) {
  4493. zend_update_jump_target_to_next(jmp_opnums[i]);
  4494. }
  4495. if (finally_ast) {
  4496. zend_loop_var discard_exception;
  4497. uint32_t opnum_jmp = get_next_op_number() + 1;
  4498. /* Pop FAST_CALL from unwind stack */
  4499. zend_stack_del_top(&CG(loop_var_stack));
  4500. /* Push DISCARD_EXCEPTION on unwind stack */
  4501. discard_exception.opcode = ZEND_DISCARD_EXCEPTION;
  4502. discard_exception.var_type = IS_TMP_VAR;
  4503. discard_exception.var_num = CG(context).fast_call_var;
  4504. zend_stack_push(&CG(loop_var_stack), &discard_exception);
  4505. CG(zend_lineno) = finally_ast->lineno;
  4506. opline = zend_emit_op(NULL, ZEND_FAST_CALL, NULL, NULL);
  4507. opline->op1.num = try_catch_offset;
  4508. opline->result_type = IS_TMP_VAR;
  4509. opline->result.var = CG(context).fast_call_var;
  4510. zend_emit_op(NULL, ZEND_JMP, NULL, NULL);
  4511. zend_compile_stmt(finally_ast);
  4512. CG(active_op_array)->try_catch_array[try_catch_offset].finally_op = opnum_jmp + 1;
  4513. CG(active_op_array)->try_catch_array[try_catch_offset].finally_end
  4514. = get_next_op_number();
  4515. opline = zend_emit_op(NULL, ZEND_FAST_RET, NULL, NULL);
  4516. opline->op1_type = IS_TMP_VAR;
  4517. opline->op1.var = CG(context).fast_call_var;
  4518. opline->op2.num = orig_try_catch_offset;
  4519. zend_update_jump_target_to_next(opnum_jmp);
  4520. CG(context).fast_call_var = orig_fast_call_var;
  4521. /* Pop DISCARD_EXCEPTION from unwind stack */
  4522. zend_stack_del_top(&CG(loop_var_stack));
  4523. }
  4524. CG(context).try_catch_offset = orig_try_catch_offset;
  4525. efree(jmp_opnums);
  4526. }
  4527. /* }}} */
  4528. /* Encoding declarations must already be handled during parsing */
  4529. zend_bool zend_handle_encoding_declaration(zend_ast *ast) /* {{{ */
  4530. {
  4531. zend_ast_list *declares = zend_ast_get_list(ast);
  4532. uint32_t i;
  4533. for (i = 0; i < declares->children; ++i) {
  4534. zend_ast *declare_ast = declares->child[i];
  4535. zend_ast *name_ast = declare_ast->child[0];
  4536. zend_ast *value_ast = declare_ast->child[1];
  4537. zend_string *name = zend_ast_get_str(name_ast);
  4538. if (zend_string_equals_literal_ci(name, "encoding")) {
  4539. if (value_ast->kind != ZEND_AST_ZVAL) {
  4540. zend_throw_exception(zend_ce_compile_error, "Encoding must be a literal", 0);
  4541. return 0;
  4542. }
  4543. if (CG(multibyte)) {
  4544. zend_string *encoding_name = zval_get_string(zend_ast_get_zval(value_ast));
  4545. const zend_encoding *new_encoding, *old_encoding;
  4546. zend_encoding_filter old_input_filter;
  4547. CG(encoding_declared) = 1;
  4548. new_encoding = zend_multibyte_fetch_encoding(ZSTR_VAL(encoding_name));
  4549. if (!new_encoding) {
  4550. zend_error(E_COMPILE_WARNING, "Unsupported encoding [%s]", ZSTR_VAL(encoding_name));
  4551. } else {
  4552. old_input_filter = LANG_SCNG(input_filter);
  4553. old_encoding = LANG_SCNG(script_encoding);
  4554. zend_multibyte_set_filter(new_encoding);
  4555. /* need to re-scan if input filter changed */
  4556. if (old_input_filter != LANG_SCNG(input_filter) ||
  4557. (old_input_filter && new_encoding != old_encoding)) {
  4558. zend_multibyte_yyinput_again(old_input_filter, old_encoding);
  4559. }
  4560. }
  4561. zend_string_release_ex(encoding_name, 0);
  4562. } else {
  4563. zend_error(E_COMPILE_WARNING, "declare(encoding=...) ignored because "
  4564. "Zend multibyte feature is turned off by settings");
  4565. }
  4566. }
  4567. }
  4568. return 1;
  4569. }
  4570. /* }}} */
  4571. static int zend_declare_is_first_statement(zend_ast *ast) /* {{{ */
  4572. {
  4573. uint32_t i = 0;
  4574. zend_ast_list *file_ast = zend_ast_get_list(CG(ast));
  4575. /* Check to see if this declare is preceded only by declare statements */
  4576. while (i < file_ast->children) {
  4577. if (file_ast->child[i] == ast) {
  4578. return SUCCESS;
  4579. } else if (file_ast->child[i] == NULL) {
  4580. /* Empty statements are not allowed prior to a declare */
  4581. return FAILURE;
  4582. } else if (file_ast->child[i]->kind != ZEND_AST_DECLARE) {
  4583. /* declares can only be preceded by other declares */
  4584. return FAILURE;
  4585. }
  4586. i++;
  4587. }
  4588. return FAILURE;
  4589. }
  4590. /* }}} */
  4591. void zend_compile_declare(zend_ast *ast) /* {{{ */
  4592. {
  4593. zend_ast_list *declares = zend_ast_get_list(ast->child[0]);
  4594. zend_ast *stmt_ast = ast->child[1];
  4595. zend_declarables orig_declarables = FC(declarables);
  4596. uint32_t i;
  4597. for (i = 0; i < declares->children; ++i) {
  4598. zend_ast *declare_ast = declares->child[i];
  4599. zend_ast *name_ast = declare_ast->child[0];
  4600. zend_ast *value_ast = declare_ast->child[1];
  4601. zend_string *name = zend_ast_get_str(name_ast);
  4602. if (value_ast->kind != ZEND_AST_ZVAL) {
  4603. zend_error_noreturn(E_COMPILE_ERROR, "declare(%s) value must be a literal", ZSTR_VAL(name));
  4604. }
  4605. if (zend_string_equals_literal_ci(name, "ticks")) {
  4606. zval value_zv;
  4607. zend_const_expr_to_zval(&value_zv, value_ast);
  4608. FC(declarables).ticks = zval_get_long(&value_zv);
  4609. zval_ptr_dtor_nogc(&value_zv);
  4610. } else if (zend_string_equals_literal_ci(name, "encoding")) {
  4611. if (FAILURE == zend_declare_is_first_statement(ast)) {
  4612. zend_error_noreturn(E_COMPILE_ERROR, "Encoding declaration pragma must be "
  4613. "the very first statement in the script");
  4614. }
  4615. } else if (zend_string_equals_literal_ci(name, "strict_types")) {
  4616. zval value_zv;
  4617. if (FAILURE == zend_declare_is_first_statement(ast)) {
  4618. zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must be "
  4619. "the very first statement in the script");
  4620. }
  4621. if (ast->child[1] != NULL) {
  4622. zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must not "
  4623. "use block mode");
  4624. }
  4625. zend_const_expr_to_zval(&value_zv, value_ast);
  4626. if (Z_TYPE(value_zv) != IS_LONG || (Z_LVAL(value_zv) != 0 && Z_LVAL(value_zv) != 1)) {
  4627. zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must have 0 or 1 as its value");
  4628. }
  4629. if (Z_LVAL(value_zv) == 1) {
  4630. CG(active_op_array)->fn_flags |= ZEND_ACC_STRICT_TYPES;
  4631. }
  4632. } else {
  4633. zend_error(E_COMPILE_WARNING, "Unsupported declare '%s'", ZSTR_VAL(name));
  4634. }
  4635. }
  4636. if (stmt_ast) {
  4637. zend_compile_stmt(stmt_ast);
  4638. FC(declarables) = orig_declarables;
  4639. }
  4640. }
  4641. /* }}} */
  4642. void zend_compile_stmt_list(zend_ast *ast) /* {{{ */
  4643. {
  4644. zend_ast_list *list = zend_ast_get_list(ast);
  4645. uint32_t i;
  4646. for (i = 0; i < list->children; ++i) {
  4647. zend_compile_stmt(list->child[i]);
  4648. }
  4649. }
  4650. /* }}} */
  4651. ZEND_API void zend_set_function_arg_flags(zend_function *func) /* {{{ */
  4652. {
  4653. uint32_t i, n;
  4654. func->common.arg_flags[0] = 0;
  4655. func->common.arg_flags[1] = 0;
  4656. func->common.arg_flags[2] = 0;
  4657. if (func->common.arg_info) {
  4658. n = MIN(func->common.num_args, MAX_ARG_FLAG_NUM);
  4659. i = 0;
  4660. while (i < n) {
  4661. ZEND_SET_ARG_FLAG(func, i + 1, ZEND_ARG_SEND_MODE(&func->common.arg_info[i]));
  4662. i++;
  4663. }
  4664. if (UNEXPECTED(func->common.fn_flags & ZEND_ACC_VARIADIC && ZEND_ARG_SEND_MODE(&func->common.arg_info[i]))) {
  4665. uint32_t pass_by_reference = ZEND_ARG_SEND_MODE(&func->common.arg_info[i]);
  4666. while (i < MAX_ARG_FLAG_NUM) {
  4667. ZEND_SET_ARG_FLAG(func, i + 1, pass_by_reference);
  4668. i++;
  4669. }
  4670. }
  4671. }
  4672. }
  4673. /* }}} */
  4674. static zend_type zend_compile_single_typename(zend_ast *ast)
  4675. {
  4676. ZEND_ASSERT(!(ast->attr & ZEND_TYPE_NULLABLE));
  4677. if (ast->kind == ZEND_AST_TYPE) {
  4678. if (ast->attr == IS_STATIC && !CG(active_class_entry) && zend_is_scope_known()) {
  4679. zend_error_noreturn(E_COMPILE_ERROR,
  4680. "Cannot use \"static\" when no class scope is active");
  4681. }
  4682. return (zend_type) ZEND_TYPE_INIT_CODE(ast->attr, 0, 0);
  4683. } else {
  4684. zend_string *class_name = zend_ast_get_str(ast);
  4685. zend_uchar type_code = zend_lookup_builtin_type_by_name(class_name);
  4686. if (type_code != 0) {
  4687. if ((ast->attr & ZEND_NAME_NOT_FQ) != ZEND_NAME_NOT_FQ) {
  4688. zend_error_noreturn(E_COMPILE_ERROR,
  4689. "Type declaration '%s' must be unqualified",
  4690. ZSTR_VAL(zend_string_tolower(class_name)));
  4691. }
  4692. return (zend_type) ZEND_TYPE_INIT_CODE(type_code, 0, 0);
  4693. } else {
  4694. const char *correct_name;
  4695. zend_string *orig_name = zend_ast_get_str(ast);
  4696. uint32_t fetch_type = zend_get_class_fetch_type_ast(ast);
  4697. if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) {
  4698. class_name = zend_resolve_class_name_ast(ast);
  4699. zend_assert_valid_class_name(class_name);
  4700. } else {
  4701. zend_ensure_valid_class_fetch_type(fetch_type);
  4702. zend_string_addref(class_name);
  4703. }
  4704. if (ast->attr == ZEND_NAME_NOT_FQ
  4705. && zend_is_confusable_type(orig_name, &correct_name)
  4706. && zend_is_not_imported(orig_name)) {
  4707. const char *extra =
  4708. FC(current_namespace) ? " or import the class with \"use\"" : "";
  4709. if (correct_name) {
  4710. zend_error(E_COMPILE_WARNING,
  4711. "\"%s\" will be interpreted as a class name. Did you mean \"%s\"? "
  4712. "Write \"\\%s\"%s to suppress this warning",
  4713. ZSTR_VAL(orig_name), correct_name, ZSTR_VAL(class_name), extra);
  4714. } else {
  4715. zend_error(E_COMPILE_WARNING,
  4716. "\"%s\" is not a supported builtin type "
  4717. "and will be interpreted as a class name. "
  4718. "Write \"\\%s\"%s to suppress this warning",
  4719. ZSTR_VAL(orig_name), ZSTR_VAL(class_name), extra);
  4720. }
  4721. }
  4722. return (zend_type) ZEND_TYPE_INIT_CLASS(class_name, 0, 0);
  4723. }
  4724. }
  4725. }
  4726. static zend_bool zend_type_contains_traversable(zend_type type) {
  4727. zend_type *single_type;
  4728. ZEND_TYPE_FOREACH(type, single_type) {
  4729. if (ZEND_TYPE_HAS_NAME(*single_type)
  4730. && zend_string_equals_literal_ci(ZEND_TYPE_NAME(*single_type), "Traversable")) {
  4731. return 1;
  4732. }
  4733. } ZEND_TYPE_FOREACH_END();
  4734. return 0;
  4735. }
  4736. // TODO: Ideally we'd canonicalize "iterable" into "array|Traversable" and essentially
  4737. // treat it as a built-in type alias.
  4738. static zend_type zend_compile_typename(
  4739. zend_ast *ast, zend_bool force_allow_null, zend_bool use_arena) /* {{{ */
  4740. {
  4741. zend_bool allow_null = force_allow_null;
  4742. zend_ast_attr orig_ast_attr = ast->attr;
  4743. zend_type type = ZEND_TYPE_INIT_NONE(0);
  4744. if (ast->attr & ZEND_TYPE_NULLABLE) {
  4745. allow_null = 1;
  4746. ast->attr &= ~ZEND_TYPE_NULLABLE;
  4747. }
  4748. if (ast->kind == ZEND_AST_TYPE_UNION) {
  4749. zend_ast_list *list = zend_ast_get_list(ast);
  4750. for (uint32_t i = 0; i < list->children; i++) {
  4751. zend_ast *type_ast = list->child[i];
  4752. zend_type single_type = zend_compile_single_typename(type_ast);
  4753. uint32_t type_mask_overlap =
  4754. ZEND_TYPE_PURE_MASK(type) & ZEND_TYPE_PURE_MASK(single_type);
  4755. if (type_mask_overlap) {
  4756. zend_type overlap_type = ZEND_TYPE_INIT_MASK(type_mask_overlap);
  4757. zend_string *overlap_type_str = zend_type_to_string(overlap_type);
  4758. zend_error_noreturn(E_COMPILE_ERROR,
  4759. "Duplicate type %s is redundant", ZSTR_VAL(overlap_type_str));
  4760. }
  4761. ZEND_TYPE_FULL_MASK(type) |= ZEND_TYPE_PURE_MASK(single_type);
  4762. ZEND_TYPE_FULL_MASK(single_type) &= ~_ZEND_TYPE_MAY_BE_MASK;
  4763. if (ZEND_TYPE_HAS_CLASS(single_type)) {
  4764. if (!ZEND_TYPE_HAS_CLASS(type)) {
  4765. /* The first class type can be stored directly as the type ptr payload. */
  4766. ZEND_TYPE_SET_PTR(type, ZEND_TYPE_NAME(single_type));
  4767. ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_NAME_BIT;
  4768. } else {
  4769. zend_type_list *list;
  4770. if (ZEND_TYPE_HAS_LIST(type)) {
  4771. /* Add name to existing name list. */
  4772. zend_type_list *old_list = ZEND_TYPE_LIST(type);
  4773. if (use_arena) {
  4774. // TODO: Add a zend_arena_realloc API?
  4775. list = zend_arena_alloc(
  4776. &CG(arena), ZEND_TYPE_LIST_SIZE(old_list->num_types + 1));
  4777. memcpy(list, old_list, ZEND_TYPE_LIST_SIZE(old_list->num_types));
  4778. } else {
  4779. list = erealloc(old_list, ZEND_TYPE_LIST_SIZE(old_list->num_types + 1));
  4780. }
  4781. } else {
  4782. /* Switch from single name to name list. */
  4783. size_t size = ZEND_TYPE_LIST_SIZE(2);
  4784. list = use_arena ? zend_arena_alloc(&CG(arena), size) : emalloc(size);
  4785. list->num_types = 1;
  4786. list->types[0] = type;
  4787. ZEND_TYPE_FULL_MASK(list->types[0]) &= ~_ZEND_TYPE_MAY_BE_MASK;
  4788. }
  4789. list->types[list->num_types++] = single_type;
  4790. ZEND_TYPE_SET_LIST(type, list);
  4791. if (use_arena) {
  4792. ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT;
  4793. }
  4794. /* Check for trivially redundant class types */
  4795. for (size_t i = 0; i < list->num_types - 1; i++) {
  4796. if (zend_string_equals_ci(
  4797. ZEND_TYPE_NAME(list->types[i]), ZEND_TYPE_NAME(single_type))) {
  4798. zend_string *single_type_str = zend_type_to_string(single_type);
  4799. zend_error_noreturn(E_COMPILE_ERROR,
  4800. "Duplicate type %s is redundant", ZSTR_VAL(single_type_str));
  4801. }
  4802. }
  4803. }
  4804. }
  4805. }
  4806. } else {
  4807. type = zend_compile_single_typename(ast);
  4808. }
  4809. if (allow_null) {
  4810. ZEND_TYPE_FULL_MASK(type) |= MAY_BE_NULL;
  4811. }
  4812. uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
  4813. if ((type_mask & (MAY_BE_ARRAY|MAY_BE_ITERABLE)) == (MAY_BE_ARRAY|MAY_BE_ITERABLE)) {
  4814. zend_string *type_str = zend_type_to_string(type);
  4815. zend_error_noreturn(E_COMPILE_ERROR,
  4816. "Type %s contains both iterable and array, which is redundant", ZSTR_VAL(type_str));
  4817. }
  4818. if ((type_mask & MAY_BE_ITERABLE) && zend_type_contains_traversable(type)) {
  4819. zend_string *type_str = zend_type_to_string(type);
  4820. zend_error_noreturn(E_COMPILE_ERROR,
  4821. "Type %s contains both iterable and Traversable, which is redundant",
  4822. ZSTR_VAL(type_str));
  4823. }
  4824. if ((type_mask & MAY_BE_OBJECT) && (ZEND_TYPE_HAS_CLASS(type) || (type_mask & MAY_BE_STATIC))) {
  4825. zend_string *type_str = zend_type_to_string(type);
  4826. zend_error_noreturn(E_COMPILE_ERROR,
  4827. "Type %s contains both object and a class type, which is redundant",
  4828. ZSTR_VAL(type_str));
  4829. }
  4830. if ((type_mask & MAY_BE_VOID) && (ZEND_TYPE_HAS_CLASS(type) || type_mask != MAY_BE_VOID)) {
  4831. zend_error_noreturn(E_COMPILE_ERROR, "Void can only be used as a standalone type");
  4832. }
  4833. if ((type_mask & (MAY_BE_NULL|MAY_BE_FALSE))
  4834. && !ZEND_TYPE_HAS_CLASS(type) && !(type_mask & ~(MAY_BE_NULL|MAY_BE_FALSE))) {
  4835. if (type_mask == MAY_BE_NULL) {
  4836. zend_error_noreturn(E_COMPILE_ERROR, "Null can not be used as a standalone type");
  4837. } else {
  4838. zend_error_noreturn(E_COMPILE_ERROR, "False can not be used as a standalone type");
  4839. }
  4840. }
  4841. ast->attr = orig_ast_attr;
  4842. return type;
  4843. }
  4844. /* }}} */
  4845. /* May convert value from int to float. */
  4846. static zend_bool zend_is_valid_default_value(zend_type type, zval *value)
  4847. {
  4848. ZEND_ASSERT(ZEND_TYPE_IS_SET(type));
  4849. if (ZEND_TYPE_CONTAINS_CODE(type, Z_TYPE_P(value))) {
  4850. return 1;
  4851. }
  4852. if ((ZEND_TYPE_FULL_MASK(type) & MAY_BE_DOUBLE) && Z_TYPE_P(value) == IS_LONG) {
  4853. /* Integers are allowed as initializers for floating-point values. */
  4854. convert_to_double(value);
  4855. return 1;
  4856. }
  4857. if ((ZEND_TYPE_FULL_MASK(type) & MAY_BE_ITERABLE) && Z_TYPE_P(value) == IS_ARRAY) {
  4858. return 1;
  4859. }
  4860. return 0;
  4861. }
  4862. void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, uint32_t fallback_return_type) /* {{{ */
  4863. {
  4864. zend_ast_list *list = zend_ast_get_list(ast);
  4865. uint32_t i;
  4866. zend_op_array *op_array = CG(active_op_array);
  4867. zend_arg_info *arg_infos;
  4868. zend_string *optional_param = NULL;
  4869. if (return_type_ast || fallback_return_type) {
  4870. /* Use op_array->arg_info[-1] for return type */
  4871. arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children + 1, 0);
  4872. arg_infos->name = NULL;
  4873. if (return_type_ast) {
  4874. arg_infos->type = zend_compile_typename(
  4875. return_type_ast, /* force_allow_null */ 0, /* use_arena */ 0);
  4876. ZEND_TYPE_FULL_MASK(arg_infos->type) |= _ZEND_ARG_INFO_FLAGS(
  4877. (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0, /* is_variadic */ 0);
  4878. } else {
  4879. arg_infos->type = (zend_type) ZEND_TYPE_INIT_CODE(fallback_return_type, 0, 0);
  4880. }
  4881. arg_infos++;
  4882. op_array->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE;
  4883. } else {
  4884. if (list->children == 0) {
  4885. return;
  4886. }
  4887. arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children, 0);
  4888. }
  4889. for (i = 0; i < list->children; ++i) {
  4890. zend_ast *param_ast = list->child[i];
  4891. zend_ast *type_ast = param_ast->child[0];
  4892. zend_ast *var_ast = param_ast->child[1];
  4893. zend_ast *default_ast = param_ast->child[2];
  4894. zend_string *name = zval_make_interned_string(zend_ast_get_zval(var_ast));
  4895. zend_bool is_ref = (param_ast->attr & ZEND_PARAM_REF) != 0;
  4896. zend_bool is_variadic = (param_ast->attr & ZEND_PARAM_VARIADIC) != 0;
  4897. znode var_node, default_node;
  4898. zend_uchar opcode;
  4899. zend_op *opline;
  4900. zend_arg_info *arg_info;
  4901. if (zend_is_auto_global(name)) {
  4902. zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign auto-global variable %s",
  4903. ZSTR_VAL(name));
  4904. }
  4905. var_node.op_type = IS_CV;
  4906. var_node.u.op.var = lookup_cv(name);
  4907. if (EX_VAR_TO_NUM(var_node.u.op.var) != i) {
  4908. zend_error_noreturn(E_COMPILE_ERROR, "Redefinition of parameter $%s",
  4909. ZSTR_VAL(name));
  4910. } else if (zend_string_equals_literal(name, "this")) {
  4911. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as parameter");
  4912. }
  4913. if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
  4914. zend_error_noreturn(E_COMPILE_ERROR, "Only the last parameter can be variadic");
  4915. }
  4916. if (is_variadic) {
  4917. opcode = ZEND_RECV_VARIADIC;
  4918. default_node.op_type = IS_UNUSED;
  4919. op_array->fn_flags |= ZEND_ACC_VARIADIC;
  4920. if (default_ast) {
  4921. zend_error_noreturn(E_COMPILE_ERROR,
  4922. "Variadic parameter cannot have a default value");
  4923. }
  4924. } else if (default_ast) {
  4925. /* we cannot substitute constants here or it will break ReflectionParameter::getDefaultValueConstantName() and ReflectionParameter::isDefaultValueConstant() */
  4926. uint32_t cops = CG(compiler_options);
  4927. CG(compiler_options) |= ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION | ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION;
  4928. opcode = ZEND_RECV_INIT;
  4929. default_node.op_type = IS_CONST;
  4930. zend_const_expr_to_zval(&default_node.u.constant, default_ast);
  4931. CG(compiler_options) = cops;
  4932. if (!optional_param) {
  4933. /* Ignore parameters of the form "Type $param = null".
  4934. * This is the PHP 5 style way of writing "?Type $param", so allow it for now. */
  4935. zend_bool is_implicit_nullable =
  4936. type_ast && Z_TYPE(default_node.u.constant) == IS_NULL;
  4937. if (!is_implicit_nullable) {
  4938. optional_param = name;
  4939. }
  4940. }
  4941. } else {
  4942. opcode = ZEND_RECV;
  4943. default_node.op_type = IS_UNUSED;
  4944. op_array->required_num_args = i + 1;
  4945. if (optional_param) {
  4946. zend_error(E_DEPRECATED, "Required parameter $%s follows optional parameter $%s",
  4947. ZSTR_VAL(name), ZSTR_VAL(optional_param));
  4948. }
  4949. }
  4950. arg_info = &arg_infos[i];
  4951. arg_info->name = zend_string_copy(name);
  4952. arg_info->type = (zend_type) ZEND_TYPE_INIT_NONE(0);
  4953. if (type_ast) {
  4954. uint32_t default_type = default_ast ? Z_TYPE(default_node.u.constant) : IS_UNDEF;
  4955. op_array->fn_flags |= ZEND_ACC_HAS_TYPE_HINTS;
  4956. arg_info->type = zend_compile_typename(
  4957. type_ast, default_type == IS_NULL, /* use_arena */ 0);
  4958. if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_VOID) {
  4959. zend_error_noreturn(E_COMPILE_ERROR, "void cannot be used as a parameter type");
  4960. }
  4961. if (default_type > IS_NULL && default_type != IS_CONSTANT_AST
  4962. && !zend_is_valid_default_value(arg_info->type, &default_node.u.constant)) {
  4963. zend_string *type_str = zend_type_to_string(arg_info->type);
  4964. zend_error_noreturn(E_COMPILE_ERROR,
  4965. "Cannot use %s as default value for parameter $%s of type %s",
  4966. zend_get_type_by_const(default_type),
  4967. ZSTR_VAL(name), ZSTR_VAL(type_str));
  4968. }
  4969. }
  4970. opline = zend_emit_op(NULL, opcode, NULL, &default_node);
  4971. SET_NODE(opline->result, &var_node);
  4972. opline->op1.num = i + 1;
  4973. if (type_ast) {
  4974. /* Allocate cache slot to speed-up run-time class resolution */
  4975. opline->extended_value =
  4976. zend_alloc_cache_slots(zend_type_get_num_classes(arg_info->type));
  4977. }
  4978. ZEND_TYPE_FULL_MASK(arg_info->type) |= _ZEND_ARG_INFO_FLAGS(is_ref, is_variadic);
  4979. if (opcode == ZEND_RECV) {
  4980. opline->op2.num = type_ast ?
  4981. ZEND_TYPE_FULL_MASK(arg_info->type) : MAY_BE_ANY;
  4982. }
  4983. }
  4984. /* These are assigned at the end to avoid uninitialized memory in case of an error */
  4985. op_array->num_args = list->children;
  4986. op_array->arg_info = arg_infos;
  4987. /* Don't count the variadic argument */
  4988. if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
  4989. op_array->num_args--;
  4990. }
  4991. zend_set_function_arg_flags((zend_function*)op_array);
  4992. }
  4993. /* }}} */
  4994. static void zend_compile_closure_binding(znode *closure, zend_op_array *op_array, zend_ast *uses_ast) /* {{{ */
  4995. {
  4996. zend_ast_list *list = zend_ast_get_list(uses_ast);
  4997. uint32_t i;
  4998. if (!list->children) {
  4999. return;
  5000. }
  5001. if (!op_array->static_variables) {
  5002. op_array->static_variables = zend_new_array(8);
  5003. }
  5004. for (i = 0; i < list->children; ++i) {
  5005. zend_ast *var_name_ast = list->child[i];
  5006. zend_string *var_name = zval_make_interned_string(zend_ast_get_zval(var_name_ast));
  5007. uint32_t mode = var_name_ast->attr;
  5008. zend_op *opline;
  5009. zval *value;
  5010. if (zend_string_equals_literal(var_name, "this")) {
  5011. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as lexical variable");
  5012. }
  5013. if (zend_is_auto_global(var_name)) {
  5014. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use auto-global as lexical variable");
  5015. }
  5016. value = zend_hash_add(op_array->static_variables, var_name, &EG(uninitialized_zval));
  5017. if (!value) {
  5018. zend_error_noreturn(E_COMPILE_ERROR,
  5019. "Cannot use variable $%s twice", ZSTR_VAL(var_name));
  5020. }
  5021. CG(zend_lineno) = zend_ast_get_lineno(var_name_ast);
  5022. opline = zend_emit_op(NULL, ZEND_BIND_LEXICAL, closure, NULL);
  5023. opline->op2_type = IS_CV;
  5024. opline->op2.var = lookup_cv(var_name);
  5025. opline->extended_value =
  5026. (uint32_t)((char*)value - (char*)op_array->static_variables->arData) | mode;
  5027. }
  5028. }
  5029. /* }}} */
  5030. typedef struct {
  5031. HashTable uses;
  5032. zend_bool varvars_used;
  5033. } closure_info;
  5034. static void find_implicit_binds_recursively(closure_info *info, zend_ast *ast) {
  5035. if (!ast) {
  5036. return;
  5037. }
  5038. if (ast->kind == ZEND_AST_VAR) {
  5039. zend_ast *name_ast = ast->child[0];
  5040. if (name_ast->kind == ZEND_AST_ZVAL && Z_TYPE_P(zend_ast_get_zval(name_ast)) == IS_STRING) {
  5041. zend_string *name = zend_ast_get_str(name_ast);
  5042. if (zend_is_auto_global(name)) {
  5043. /* These is no need to explicitly import auto-globals. */
  5044. return;
  5045. }
  5046. if (zend_string_equals_literal(name, "this")) {
  5047. /* $this does not need to be explicitly imported. */
  5048. return;
  5049. }
  5050. zend_hash_add_empty_element(&info->uses, name);
  5051. } else {
  5052. info->varvars_used = 1;
  5053. find_implicit_binds_recursively(info, name_ast);
  5054. }
  5055. } else if (zend_ast_is_list(ast)) {
  5056. zend_ast_list *list = zend_ast_get_list(ast);
  5057. uint32_t i;
  5058. for (i = 0; i < list->children; i++) {
  5059. find_implicit_binds_recursively(info, list->child[i]);
  5060. }
  5061. } else if (ast->kind == ZEND_AST_CLOSURE) {
  5062. /* For normal closures add the use() list. */
  5063. zend_ast_decl *closure_ast = (zend_ast_decl *) ast;
  5064. zend_ast *uses_ast = closure_ast->child[1];
  5065. if (uses_ast) {
  5066. zend_ast_list *uses_list = zend_ast_get_list(uses_ast);
  5067. uint32_t i;
  5068. for (i = 0; i < uses_list->children; i++) {
  5069. zend_hash_add_empty_element(&info->uses, zend_ast_get_str(uses_list->child[i]));
  5070. }
  5071. }
  5072. } else if (ast->kind == ZEND_AST_ARROW_FUNC) {
  5073. /* For arrow functions recursively check the expression. */
  5074. zend_ast_decl *closure_ast = (zend_ast_decl *) ast;
  5075. find_implicit_binds_recursively(info, closure_ast->child[2]);
  5076. } else if (!zend_ast_is_special(ast)) {
  5077. uint32_t i, children = zend_ast_get_num_children(ast);
  5078. for (i = 0; i < children; i++) {
  5079. find_implicit_binds_recursively(info, ast->child[i]);
  5080. }
  5081. }
  5082. }
  5083. static void find_implicit_binds(closure_info *info, zend_ast *params_ast, zend_ast *stmt_ast)
  5084. {
  5085. zend_ast_list *param_list = zend_ast_get_list(params_ast);
  5086. uint32_t i;
  5087. zend_hash_init(&info->uses, param_list->children, NULL, NULL, 0);
  5088. find_implicit_binds_recursively(info, stmt_ast);
  5089. /* Remove variables that are parameters */
  5090. for (i = 0; i < param_list->children; i++) {
  5091. zend_ast *param_ast = param_list->child[i];
  5092. zend_hash_del(&info->uses, zend_ast_get_str(param_ast->child[1]));
  5093. }
  5094. }
  5095. static void compile_implicit_lexical_binds(
  5096. closure_info *info, znode *closure, zend_op_array *op_array)
  5097. {
  5098. zend_string *var_name;
  5099. zend_op *opline;
  5100. /* TODO We might want to use a special binding mode if varvars_used is set. */
  5101. if (zend_hash_num_elements(&info->uses) == 0) {
  5102. return;
  5103. }
  5104. if (!op_array->static_variables) {
  5105. op_array->static_variables = zend_new_array(8);
  5106. }
  5107. ZEND_HASH_FOREACH_STR_KEY(&info->uses, var_name)
  5108. zval *value = zend_hash_add(
  5109. op_array->static_variables, var_name, &EG(uninitialized_zval));
  5110. uint32_t offset = (uint32_t)((char*)value - (char*)op_array->static_variables->arData);
  5111. opline = zend_emit_op(NULL, ZEND_BIND_LEXICAL, closure, NULL);
  5112. opline->op2_type = IS_CV;
  5113. opline->op2.var = lookup_cv(var_name);
  5114. opline->extended_value = offset | ZEND_BIND_IMPLICIT;
  5115. ZEND_HASH_FOREACH_END();
  5116. }
  5117. static void zend_compile_closure_uses(zend_ast *ast) /* {{{ */
  5118. {
  5119. zend_op_array *op_array = CG(active_op_array);
  5120. zend_ast_list *list = zend_ast_get_list(ast);
  5121. uint32_t i;
  5122. for (i = 0; i < list->children; ++i) {
  5123. zend_ast *var_ast = list->child[i];
  5124. zend_string *var_name = zend_ast_get_str(var_ast);
  5125. zval zv;
  5126. ZVAL_NULL(&zv);
  5127. {
  5128. int i;
  5129. for (i = 0; i < op_array->last_var; i++) {
  5130. if (zend_string_equals(op_array->vars[i], var_name)) {
  5131. zend_error_noreturn(E_COMPILE_ERROR,
  5132. "Cannot use lexical variable $%s as a parameter name", ZSTR_VAL(var_name));
  5133. }
  5134. }
  5135. }
  5136. CG(zend_lineno) = zend_ast_get_lineno(var_ast);
  5137. zend_compile_static_var_common(var_name, &zv, var_ast->attr ? ZEND_BIND_REF : 0);
  5138. }
  5139. }
  5140. /* }}} */
  5141. static void zend_compile_implicit_closure_uses(closure_info *info)
  5142. {
  5143. zend_string *var_name;
  5144. ZEND_HASH_FOREACH_STR_KEY(&info->uses, var_name)
  5145. zval zv;
  5146. ZVAL_NULL(&zv);
  5147. zend_compile_static_var_common(var_name, &zv, ZEND_BIND_IMPLICIT);
  5148. ZEND_HASH_FOREACH_END();
  5149. }
  5150. static void zend_check_magic_method_attr(uint32_t attr, zend_class_entry *ce, const char* method, zend_bool is_static) /* {{{ */
  5151. {
  5152. if (!(attr & ZEND_ACC_PUBLIC)) {
  5153. zend_error(E_WARNING,
  5154. "The magic method %s::%s() must have public visibility",
  5155. ZSTR_VAL(ce->name), method);
  5156. }
  5157. if (is_static) {
  5158. if (!(attr & ZEND_ACC_STATIC)) {
  5159. zend_error(E_WARNING,
  5160. "The magic method %s::%s() must be static",
  5161. ZSTR_VAL(ce->name), method);
  5162. }
  5163. } else if (attr & ZEND_ACC_STATIC) {
  5164. zend_error(E_WARNING,
  5165. "The magic method %s::%s() cannot be static",
  5166. ZSTR_VAL(ce->name), method);
  5167. }
  5168. }
  5169. /* }}} */
  5170. static void add_stringable_interface(zend_class_entry *ce) {
  5171. for (uint32_t i = 0; i < ce->num_interfaces; i++) {
  5172. if (zend_string_equals_literal(ce->interface_names[i].lc_name, "stringable")) {
  5173. /* Interface already explicitly implemented */
  5174. return;
  5175. }
  5176. }
  5177. ce->num_interfaces++;
  5178. ce->interface_names =
  5179. erealloc(ce->interface_names, sizeof(zend_class_name) * ce->num_interfaces);
  5180. // TODO: Add known interned strings instead?
  5181. ce->interface_names[ce->num_interfaces - 1].name =
  5182. zend_string_init("Stringable", sizeof("Stringable") - 1, 0);
  5183. ce->interface_names[ce->num_interfaces - 1].lc_name =
  5184. zend_string_init("stringable", sizeof("stringable") - 1, 0);
  5185. }
  5186. zend_string *zend_begin_method_decl(zend_op_array *op_array, zend_string *name, zend_bool has_body) /* {{{ */
  5187. {
  5188. zend_class_entry *ce = CG(active_class_entry);
  5189. zend_bool in_interface = (ce->ce_flags & ZEND_ACC_INTERFACE) != 0;
  5190. uint32_t fn_flags = op_array->fn_flags;
  5191. zend_string *lcname;
  5192. if (in_interface) {
  5193. if (!(fn_flags & ZEND_ACC_PUBLIC) || (fn_flags & (ZEND_ACC_FINAL|ZEND_ACC_ABSTRACT))) {
  5194. zend_error_noreturn(E_COMPILE_ERROR, "Access type for interface method "
  5195. "%s::%s() must be omitted", ZSTR_VAL(ce->name), ZSTR_VAL(name));
  5196. }
  5197. op_array->fn_flags |= ZEND_ACC_ABSTRACT;
  5198. }
  5199. if (op_array->fn_flags & ZEND_ACC_ABSTRACT) {
  5200. if ((op_array->fn_flags & ZEND_ACC_PRIVATE) && !(ce->ce_flags & ZEND_ACC_TRAIT)) {
  5201. zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot be declared private",
  5202. in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name));
  5203. }
  5204. if (has_body) {
  5205. zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot contain body",
  5206. in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name));
  5207. }
  5208. ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
  5209. } else if (!has_body) {
  5210. zend_error_noreturn(E_COMPILE_ERROR, "Non-abstract method %s::%s() must contain body",
  5211. ZSTR_VAL(ce->name), ZSTR_VAL(name));
  5212. }
  5213. op_array->scope = ce;
  5214. op_array->function_name = zend_string_copy(name);
  5215. lcname = zend_string_tolower(name);
  5216. lcname = zend_new_interned_string(lcname);
  5217. if (zend_hash_add_ptr(&ce->function_table, lcname, op_array) == NULL) {
  5218. zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::%s()",
  5219. ZSTR_VAL(ce->name), ZSTR_VAL(name));
  5220. }
  5221. if (zend_string_equals_literal(lcname, "serialize")) {
  5222. ce->serialize_func = (zend_function *) op_array;
  5223. } else if (zend_string_equals_literal(lcname, "unserialize")) {
  5224. ce->unserialize_func = (zend_function *) op_array;
  5225. } else if (ZSTR_VAL(lcname)[0] != '_' || ZSTR_VAL(lcname)[1] != '_') {
  5226. /* pass */
  5227. } else if (zend_string_equals_literal(lcname, ZEND_CONSTRUCTOR_FUNC_NAME)) {
  5228. ce->constructor = (zend_function *) op_array;
  5229. } else if (zend_string_equals_literal(lcname, ZEND_DESTRUCTOR_FUNC_NAME)) {
  5230. ce->destructor = (zend_function *) op_array;
  5231. } else if (zend_string_equals_literal(lcname, ZEND_CLONE_FUNC_NAME)) {
  5232. ce->clone = (zend_function *) op_array;
  5233. } else if (zend_string_equals_literal(lcname, ZEND_CALL_FUNC_NAME)) {
  5234. zend_check_magic_method_attr(fn_flags, ce, "__call", 0);
  5235. ce->__call = (zend_function *) op_array;
  5236. } else if (zend_string_equals_literal(lcname, ZEND_CALLSTATIC_FUNC_NAME)) {
  5237. zend_check_magic_method_attr(fn_flags, ce, "__callStatic", 1);
  5238. ce->__callstatic = (zend_function *) op_array;
  5239. } else if (zend_string_equals_literal(lcname, ZEND_GET_FUNC_NAME)) {
  5240. zend_check_magic_method_attr(fn_flags, ce, "__get", 0);
  5241. ce->__get = (zend_function *) op_array;
  5242. ce->ce_flags |= ZEND_ACC_USE_GUARDS;
  5243. } else if (zend_string_equals_literal(lcname, ZEND_SET_FUNC_NAME)) {
  5244. zend_check_magic_method_attr(fn_flags, ce, "__set", 0);
  5245. ce->__set = (zend_function *) op_array;
  5246. ce->ce_flags |= ZEND_ACC_USE_GUARDS;
  5247. } else if (zend_string_equals_literal(lcname, ZEND_UNSET_FUNC_NAME)) {
  5248. zend_check_magic_method_attr(fn_flags, ce, "__unset", 0);
  5249. ce->__unset = (zend_function *) op_array;
  5250. ce->ce_flags |= ZEND_ACC_USE_GUARDS;
  5251. } else if (zend_string_equals_literal(lcname, ZEND_ISSET_FUNC_NAME)) {
  5252. zend_check_magic_method_attr(fn_flags, ce, "__isset", 0);
  5253. ce->__isset = (zend_function *) op_array;
  5254. ce->ce_flags |= ZEND_ACC_USE_GUARDS;
  5255. } else if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME)) {
  5256. zend_check_magic_method_attr(fn_flags, ce, "__toString", 0);
  5257. ce->__tostring = (zend_function *) op_array;
  5258. add_stringable_interface(ce);
  5259. } else if (zend_string_equals_literal(lcname, ZEND_INVOKE_FUNC_NAME)) {
  5260. zend_check_magic_method_attr(fn_flags, ce, "__invoke", 0);
  5261. } else if (zend_string_equals_literal(lcname, ZEND_DEBUGINFO_FUNC_NAME)) {
  5262. zend_check_magic_method_attr(fn_flags, ce, "__debugInfo", 0);
  5263. ce->__debugInfo = (zend_function *) op_array;
  5264. } else if (zend_string_equals_literal(lcname, "__serialize")) {
  5265. zend_check_magic_method_attr(fn_flags, ce, "__serialize", 0);
  5266. } else if (zend_string_equals_literal(lcname, "__unserialize")) {
  5267. zend_check_magic_method_attr(fn_flags, ce, "__unserialize", 0);
  5268. } else if (zend_string_equals_literal(lcname, "__set_state")) {
  5269. zend_check_magic_method_attr(fn_flags, ce, "__set_state", 1);
  5270. }
  5271. return lcname;
  5272. }
  5273. /* }}} */
  5274. static void zend_begin_func_decl(znode *result, zend_op_array *op_array, zend_ast_decl *decl, zend_bool toplevel) /* {{{ */
  5275. {
  5276. zend_string *unqualified_name, *name, *lcname, *key;
  5277. zend_op *opline;
  5278. unqualified_name = decl->name;
  5279. op_array->function_name = name = zend_prefix_with_ns(unqualified_name);
  5280. lcname = zend_string_tolower(name);
  5281. if (FC(imports_function)) {
  5282. zend_string *import_name =
  5283. zend_hash_find_ptr_lc(FC(imports_function), unqualified_name);
  5284. if (import_name && !zend_string_equals_ci(lcname, import_name)) {
  5285. zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare function %s "
  5286. "because the name is already in use", ZSTR_VAL(name));
  5287. }
  5288. }
  5289. if (zend_string_equals_literal(lcname, "__autoload")) {
  5290. zend_error_noreturn(E_COMPILE_ERROR,
  5291. "__autoload() is no longer supported, use spl_autoload_register() instead");
  5292. }
  5293. if (zend_string_equals_literal_ci(unqualified_name, "assert")) {
  5294. zend_error(E_COMPILE_ERROR,
  5295. "Defining a custom assert() function is not allowed, "
  5296. "as the function has special semantics");
  5297. }
  5298. zend_register_seen_symbol(lcname, ZEND_SYMBOL_FUNCTION);
  5299. if (toplevel) {
  5300. if (UNEXPECTED(zend_hash_add_ptr(CG(function_table), lcname, op_array) == NULL)) {
  5301. do_bind_function_error(lcname, op_array, 1);
  5302. }
  5303. zend_string_release_ex(lcname, 0);
  5304. return;
  5305. }
  5306. key = zend_build_runtime_definition_key(lcname, decl->start_lineno);
  5307. if (!zend_hash_add_ptr(CG(function_table), key, op_array)) {
  5308. zend_error_noreturn(E_ERROR,
  5309. "Runtime definition key collision for function %s. This is a bug", ZSTR_VAL(name));
  5310. }
  5311. if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
  5312. opline = zend_emit_op_tmp(result, ZEND_DECLARE_LAMBDA_FUNCTION, NULL, NULL);
  5313. opline->extended_value = zend_alloc_cache_slot();
  5314. opline->op1_type = IS_CONST;
  5315. LITERAL_STR(opline->op1, key);
  5316. } else {
  5317. opline = get_next_op();
  5318. opline->opcode = ZEND_DECLARE_FUNCTION;
  5319. opline->op1_type = IS_CONST;
  5320. LITERAL_STR(opline->op1, zend_string_copy(lcname));
  5321. /* RTD key is placed after lcname literal in op1 */
  5322. zend_add_literal_string(&key);
  5323. }
  5324. zend_string_release_ex(lcname, 0);
  5325. }
  5326. /* }}} */
  5327. void zend_compile_func_decl(znode *result, zend_ast *ast, zend_bool toplevel) /* {{{ */
  5328. {
  5329. zend_ast_decl *decl = (zend_ast_decl *) ast;
  5330. zend_ast *params_ast = decl->child[0];
  5331. zend_ast *uses_ast = decl->child[1];
  5332. zend_ast *stmt_ast = decl->child[2];
  5333. zend_ast *return_type_ast = decl->child[3];
  5334. zend_bool is_method = decl->kind == ZEND_AST_METHOD;
  5335. zend_string *method_lcname;
  5336. zend_class_entry *orig_class_entry = CG(active_class_entry);
  5337. zend_op_array *orig_op_array = CG(active_op_array);
  5338. zend_op_array *op_array = zend_arena_alloc(&CG(arena), sizeof(zend_op_array));
  5339. zend_oparray_context orig_oparray_context;
  5340. closure_info info;
  5341. memset(&info, 0, sizeof(closure_info));
  5342. init_op_array(op_array, ZEND_USER_FUNCTION, INITIAL_OP_ARRAY_SIZE);
  5343. if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) {
  5344. op_array->fn_flags |= ZEND_ACC_PRELOADED;
  5345. ZEND_MAP_PTR_NEW(op_array->run_time_cache);
  5346. ZEND_MAP_PTR_NEW(op_array->static_variables_ptr);
  5347. } else {
  5348. ZEND_MAP_PTR_INIT(op_array->run_time_cache, zend_arena_alloc(&CG(arena), sizeof(void*)));
  5349. ZEND_MAP_PTR_SET(op_array->run_time_cache, NULL);
  5350. }
  5351. op_array->fn_flags |= (orig_op_array->fn_flags & ZEND_ACC_STRICT_TYPES);
  5352. op_array->fn_flags |= decl->flags;
  5353. op_array->line_start = decl->start_lineno;
  5354. op_array->line_end = decl->end_lineno;
  5355. if (decl->doc_comment) {
  5356. op_array->doc_comment = zend_string_copy(decl->doc_comment);
  5357. }
  5358. if (decl->kind == ZEND_AST_CLOSURE || decl->kind == ZEND_AST_ARROW_FUNC) {
  5359. op_array->fn_flags |= ZEND_ACC_CLOSURE;
  5360. }
  5361. if (is_method) {
  5362. zend_bool has_body = stmt_ast != NULL;
  5363. method_lcname = zend_begin_method_decl(op_array, decl->name, has_body);
  5364. } else {
  5365. zend_begin_func_decl(result, op_array, decl, toplevel);
  5366. if (decl->kind == ZEND_AST_ARROW_FUNC) {
  5367. find_implicit_binds(&info, params_ast, stmt_ast);
  5368. compile_implicit_lexical_binds(&info, result, op_array);
  5369. } else if (uses_ast) {
  5370. zend_compile_closure_binding(result, op_array, uses_ast);
  5371. }
  5372. }
  5373. CG(active_op_array) = op_array;
  5374. /* Do not leak the class scope into free standing functions, even if they are dynamically
  5375. * defined inside a class method. This is necessary for correct handling of magic constants.
  5376. * For example __CLASS__ should always be "" inside a free standing function. */
  5377. if (decl->kind == ZEND_AST_FUNC_DECL) {
  5378. CG(active_class_entry) = NULL;
  5379. }
  5380. if (toplevel) {
  5381. op_array->fn_flags |= ZEND_ACC_TOP_LEVEL;
  5382. }
  5383. zend_oparray_context_begin(&orig_oparray_context);
  5384. if (CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT) {
  5385. zend_op *opline_ext = zend_emit_op(NULL, ZEND_EXT_NOP, NULL, NULL);
  5386. opline_ext->lineno = decl->start_lineno;
  5387. }
  5388. {
  5389. /* Push a separator to the loop variable stack */
  5390. zend_loop_var dummy_var;
  5391. dummy_var.opcode = ZEND_RETURN;
  5392. zend_stack_push(&CG(loop_var_stack), (void *) &dummy_var);
  5393. }
  5394. zend_compile_params(params_ast, return_type_ast,
  5395. is_method && zend_string_equals_literal_ci(decl->name, "__toString") ? IS_STRING : 0);
  5396. if (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) {
  5397. zend_mark_function_as_generator();
  5398. zend_emit_op(NULL, ZEND_GENERATOR_CREATE, NULL, NULL);
  5399. }
  5400. if (decl->kind == ZEND_AST_ARROW_FUNC) {
  5401. zend_compile_implicit_closure_uses(&info);
  5402. zend_hash_destroy(&info.uses);
  5403. } else if (uses_ast) {
  5404. zend_compile_closure_uses(uses_ast);
  5405. }
  5406. zend_compile_stmt(stmt_ast);
  5407. if (is_method) {
  5408. zend_check_magic_method_implementation(
  5409. CG(active_class_entry), (zend_function *) op_array, method_lcname, E_COMPILE_ERROR);
  5410. zend_string_release_ex(method_lcname, 0);
  5411. }
  5412. /* put the implicit return on the really last line */
  5413. CG(zend_lineno) = decl->end_lineno;
  5414. zend_do_extended_stmt();
  5415. zend_emit_final_return(0);
  5416. pass_two(CG(active_op_array));
  5417. zend_oparray_context_end(&orig_oparray_context);
  5418. /* Pop the loop variable stack separator */
  5419. zend_stack_del_top(&CG(loop_var_stack));
  5420. CG(active_op_array) = orig_op_array;
  5421. CG(active_class_entry) = orig_class_entry;
  5422. }
  5423. /* }}} */
  5424. void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast, uint32_t flags) /* {{{ */
  5425. {
  5426. zend_ast_list *list = zend_ast_get_list(ast);
  5427. zend_class_entry *ce = CG(active_class_entry);
  5428. uint32_t i, children = list->children;
  5429. if (ce->ce_flags & ZEND_ACC_INTERFACE) {
  5430. zend_error_noreturn(E_COMPILE_ERROR, "Interfaces may not include member variables");
  5431. }
  5432. if (flags & ZEND_ACC_ABSTRACT) {
  5433. zend_error_noreturn(E_COMPILE_ERROR, "Properties cannot be declared abstract");
  5434. }
  5435. for (i = 0; i < children; ++i) {
  5436. zend_ast *prop_ast = list->child[i];
  5437. zend_ast *name_ast = prop_ast->child[0];
  5438. zend_ast *value_ast = prop_ast->child[1];
  5439. zend_ast *doc_comment_ast = prop_ast->child[2];
  5440. zend_string *name = zval_make_interned_string(zend_ast_get_zval(name_ast));
  5441. zend_string *doc_comment = NULL;
  5442. zval value_zv;
  5443. zend_type type = ZEND_TYPE_INIT_NONE(0);
  5444. if (type_ast) {
  5445. type = zend_compile_typename(type_ast, /* force_allow_null */ 0, /* use_arena */ 1);
  5446. if (ZEND_TYPE_FULL_MASK(type) & (MAY_BE_VOID|MAY_BE_CALLABLE)) {
  5447. zend_string *str = zend_type_to_string(type);
  5448. zend_error_noreturn(E_COMPILE_ERROR,
  5449. "Property %s::$%s cannot have type %s",
  5450. ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(str));
  5451. }
  5452. }
  5453. /* Doc comment has been appended as last element in ZEND_AST_PROP_ELEM ast */
  5454. if (doc_comment_ast) {
  5455. doc_comment = zend_string_copy(zend_ast_get_str(doc_comment_ast));
  5456. }
  5457. if (flags & ZEND_ACC_FINAL) {
  5458. zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare property %s::$%s final, "
  5459. "the final modifier is allowed only for methods and classes",
  5460. ZSTR_VAL(ce->name), ZSTR_VAL(name));
  5461. }
  5462. if (zend_hash_exists(&ce->properties_info, name)) {
  5463. zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::$%s",
  5464. ZSTR_VAL(ce->name), ZSTR_VAL(name));
  5465. }
  5466. if (value_ast) {
  5467. zend_const_expr_to_zval(&value_zv, value_ast);
  5468. if (ZEND_TYPE_IS_SET(type) && !Z_CONSTANT(value_zv)
  5469. && !zend_is_valid_default_value(type, &value_zv)) {
  5470. zend_string *str = zend_type_to_string(type);
  5471. if (Z_TYPE(value_zv) == IS_NULL) {
  5472. zend_error_noreturn(E_COMPILE_ERROR,
  5473. "Default value for property of type %s may not be null. "
  5474. "Use the nullable type ?%s to allow null default value",
  5475. ZSTR_VAL(str), ZSTR_VAL(str));
  5476. } else {
  5477. zend_error_noreturn(E_COMPILE_ERROR,
  5478. "Cannot use %s as default value for property %s::$%s of type %s",
  5479. zend_get_type_by_const(Z_TYPE(value_zv)),
  5480. ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(str));
  5481. }
  5482. }
  5483. } else if (!ZEND_TYPE_IS_SET(type)) {
  5484. ZVAL_NULL(&value_zv);
  5485. } else {
  5486. ZVAL_UNDEF(&value_zv);
  5487. }
  5488. zend_declare_typed_property(ce, name, &value_zv, flags, doc_comment, type);
  5489. }
  5490. }
  5491. /* }}} */
  5492. void zend_compile_prop_group(zend_ast *list) /* {{{ */
  5493. {
  5494. zend_ast *type_ast = list->child[0];
  5495. zend_ast *prop_ast = list->child[1];
  5496. zend_compile_prop_decl(prop_ast, type_ast, list->attr);
  5497. }
  5498. /* }}} */
  5499. static void zend_check_const_and_trait_alias_attr(uint32_t attr, const char* entity) /* {{{ */
  5500. {
  5501. if (attr & ZEND_ACC_STATIC) {
  5502. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use 'static' as %s modifier", entity);
  5503. } else if (attr & ZEND_ACC_ABSTRACT) {
  5504. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use 'abstract' as %s modifier", entity);
  5505. } else if (attr & ZEND_ACC_FINAL) {
  5506. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use 'final' as %s modifier", entity);
  5507. }
  5508. }
  5509. /* }}} */
  5510. void zend_compile_class_const_decl(zend_ast *ast) /* {{{ */
  5511. {
  5512. zend_ast_list *list = zend_ast_get_list(ast);
  5513. zend_class_entry *ce = CG(active_class_entry);
  5514. uint32_t i;
  5515. if ((ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
  5516. zend_error_noreturn(E_COMPILE_ERROR, "Traits cannot have constants");
  5517. return;
  5518. }
  5519. for (i = 0; i < list->children; ++i) {
  5520. zend_ast *const_ast = list->child[i];
  5521. zend_ast *name_ast = const_ast->child[0];
  5522. zend_ast *value_ast = const_ast->child[1];
  5523. zend_ast *doc_comment_ast = const_ast->child[2];
  5524. zend_string *name = zval_make_interned_string(zend_ast_get_zval(name_ast));
  5525. zend_string *doc_comment = doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL;
  5526. zval value_zv;
  5527. if (UNEXPECTED(ast->attr & (ZEND_ACC_STATIC|ZEND_ACC_ABSTRACT|ZEND_ACC_FINAL))) {
  5528. zend_check_const_and_trait_alias_attr(ast->attr, "constant");
  5529. }
  5530. zend_const_expr_to_zval(&value_zv, value_ast);
  5531. zend_declare_class_constant_ex(ce, name, &value_zv, ast->attr, doc_comment);
  5532. }
  5533. }
  5534. /* }}} */
  5535. static void zend_compile_method_ref(zend_ast *ast, zend_trait_method_reference *method_ref) /* {{{ */
  5536. {
  5537. zend_ast *class_ast = ast->child[0];
  5538. zend_ast *method_ast = ast->child[1];
  5539. method_ref->method_name = zend_string_copy(zend_ast_get_str(method_ast));
  5540. if (class_ast) {
  5541. method_ref->class_name = zend_resolve_const_class_name_reference(class_ast, "trait name");
  5542. } else {
  5543. method_ref->class_name = NULL;
  5544. }
  5545. }
  5546. /* }}} */
  5547. static void zend_compile_trait_precedence(zend_ast *ast) /* {{{ */
  5548. {
  5549. zend_ast *method_ref_ast = ast->child[0];
  5550. zend_ast *insteadof_ast = ast->child[1];
  5551. zend_ast_list *insteadof_list = zend_ast_get_list(insteadof_ast);
  5552. uint32_t i;
  5553. zend_trait_precedence *precedence = emalloc(sizeof(zend_trait_precedence) + (insteadof_list->children - 1) * sizeof(zend_string*));
  5554. zend_compile_method_ref(method_ref_ast, &precedence->trait_method);
  5555. precedence->num_excludes = insteadof_list->children;
  5556. for (i = 0; i < insteadof_list->children; ++i) {
  5557. zend_ast *name_ast = insteadof_list->child[i];
  5558. precedence->exclude_class_names[i] =
  5559. zend_resolve_const_class_name_reference(name_ast, "trait name");
  5560. }
  5561. zend_add_to_list(&CG(active_class_entry)->trait_precedences, precedence);
  5562. }
  5563. /* }}} */
  5564. static void zend_compile_trait_alias(zend_ast *ast) /* {{{ */
  5565. {
  5566. zend_ast *method_ref_ast = ast->child[0];
  5567. zend_ast *alias_ast = ast->child[1];
  5568. uint32_t modifiers = ast->attr;
  5569. zend_trait_alias *alias;
  5570. zend_check_const_and_trait_alias_attr(modifiers, "method");
  5571. alias = emalloc(sizeof(zend_trait_alias));
  5572. zend_compile_method_ref(method_ref_ast, &alias->trait_method);
  5573. alias->modifiers = modifiers;
  5574. if (alias_ast) {
  5575. alias->alias = zend_string_copy(zend_ast_get_str(alias_ast));
  5576. } else {
  5577. alias->alias = NULL;
  5578. }
  5579. zend_add_to_list(&CG(active_class_entry)->trait_aliases, alias);
  5580. }
  5581. /* }}} */
  5582. void zend_compile_use_trait(zend_ast *ast) /* {{{ */
  5583. {
  5584. zend_ast_list *traits = zend_ast_get_list(ast->child[0]);
  5585. zend_ast_list *adaptations = ast->child[1] ? zend_ast_get_list(ast->child[1]) : NULL;
  5586. zend_class_entry *ce = CG(active_class_entry);
  5587. uint32_t i;
  5588. ce->trait_names = erealloc(ce->trait_names, sizeof(zend_class_name) * (ce->num_traits + traits->children));
  5589. for (i = 0; i < traits->children; ++i) {
  5590. zend_ast *trait_ast = traits->child[i];
  5591. if (ce->ce_flags & ZEND_ACC_INTERFACE) {
  5592. zend_string *name = zend_ast_get_str(trait_ast);
  5593. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use traits inside of interfaces. "
  5594. "%s is used in %s", ZSTR_VAL(name), ZSTR_VAL(ce->name));
  5595. }
  5596. ce->trait_names[ce->num_traits].name =
  5597. zend_resolve_const_class_name_reference(trait_ast, "trait name");
  5598. ce->trait_names[ce->num_traits].lc_name = zend_string_tolower(ce->trait_names[ce->num_traits].name);
  5599. ce->num_traits++;
  5600. }
  5601. if (!adaptations) {
  5602. return;
  5603. }
  5604. for (i = 0; i < adaptations->children; ++i) {
  5605. zend_ast *adaptation_ast = adaptations->child[i];
  5606. switch (adaptation_ast->kind) {
  5607. case ZEND_AST_TRAIT_PRECEDENCE:
  5608. zend_compile_trait_precedence(adaptation_ast);
  5609. break;
  5610. case ZEND_AST_TRAIT_ALIAS:
  5611. zend_compile_trait_alias(adaptation_ast);
  5612. break;
  5613. EMPTY_SWITCH_DEFAULT_CASE()
  5614. }
  5615. }
  5616. }
  5617. /* }}} */
  5618. void zend_compile_implements(zend_ast *ast) /* {{{ */
  5619. {
  5620. zend_ast_list *list = zend_ast_get_list(ast);
  5621. zend_class_entry *ce = CG(active_class_entry);
  5622. zend_class_name *interface_names;
  5623. uint32_t i;
  5624. interface_names = emalloc(sizeof(zend_class_name) * list->children);
  5625. for (i = 0; i < list->children; ++i) {
  5626. zend_ast *class_ast = list->child[i];
  5627. interface_names[i].name =
  5628. zend_resolve_const_class_name_reference(class_ast, "interface name");
  5629. interface_names[i].lc_name = zend_string_tolower(interface_names[i].name);
  5630. }
  5631. ce->num_interfaces = list->children;
  5632. ce->interface_names = interface_names;
  5633. }
  5634. /* }}} */
  5635. static zend_string *zend_generate_anon_class_name(zend_ast_decl *decl)
  5636. {
  5637. zend_string *filename = CG(active_op_array)->filename;
  5638. uint32_t start_lineno = decl->start_lineno;
  5639. /* Use parent or first interface as prefix. */
  5640. zend_string *prefix = ZSTR_KNOWN(ZEND_STR_CLASS);
  5641. if (decl->child[0]) {
  5642. prefix = zend_resolve_const_class_name_reference(decl->child[0], "class name");
  5643. } else if (decl->child[1]) {
  5644. zend_ast_list *list = zend_ast_get_list(decl->child[1]);
  5645. prefix = zend_resolve_const_class_name_reference(list->child[0], "interface name");
  5646. }
  5647. zend_string *result = zend_strpprintf(0, "%s@anonymous%c%s:%" PRIu32 "$%" PRIx32,
  5648. ZSTR_VAL(prefix), '\0', ZSTR_VAL(filename), start_lineno, CG(rtd_key_counter)++);
  5649. zend_string_release(prefix);
  5650. return zend_new_interned_string(result);
  5651. }
  5652. void zend_compile_class_decl(znode *result, zend_ast *ast, zend_bool toplevel) /* {{{ */
  5653. {
  5654. zend_ast_decl *decl = (zend_ast_decl *) ast;
  5655. zend_ast *extends_ast = decl->child[0];
  5656. zend_ast *implements_ast = decl->child[1];
  5657. zend_ast *stmt_ast = decl->child[2];
  5658. zend_string *name, *lcname;
  5659. zend_class_entry *ce = zend_arena_alloc(&CG(arena), sizeof(zend_class_entry));
  5660. zend_op *opline;
  5661. zend_class_entry *original_ce = CG(active_class_entry);
  5662. if (EXPECTED((decl->flags & ZEND_ACC_ANON_CLASS) == 0)) {
  5663. zend_string *unqualified_name = decl->name;
  5664. if (CG(active_class_entry)) {
  5665. zend_error_noreturn(E_COMPILE_ERROR, "Class declarations may not be nested");
  5666. }
  5667. zend_assert_valid_class_name(unqualified_name);
  5668. name = zend_prefix_with_ns(unqualified_name);
  5669. name = zend_new_interned_string(name);
  5670. lcname = zend_string_tolower(name);
  5671. if (FC(imports)) {
  5672. zend_string *import_name =
  5673. zend_hash_find_ptr_lc(FC(imports), unqualified_name);
  5674. if (import_name && !zend_string_equals_ci(lcname, import_name)) {
  5675. zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare class %s "
  5676. "because the name is already in use", ZSTR_VAL(name));
  5677. }
  5678. }
  5679. zend_register_seen_symbol(lcname, ZEND_SYMBOL_CLASS);
  5680. } else {
  5681. name = zend_generate_anon_class_name(decl);
  5682. lcname = zend_string_tolower(name);
  5683. }
  5684. lcname = zend_new_interned_string(lcname);
  5685. ce->type = ZEND_USER_CLASS;
  5686. ce->name = name;
  5687. zend_initialize_class_data(ce, 1);
  5688. if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) {
  5689. ce->ce_flags |= ZEND_ACC_PRELOADED;
  5690. ZEND_MAP_PTR_NEW(ce->static_members_table);
  5691. }
  5692. ce->ce_flags |= decl->flags;
  5693. ce->info.user.filename = zend_get_compiled_filename();
  5694. ce->info.user.line_start = decl->start_lineno;
  5695. ce->info.user.line_end = decl->end_lineno;
  5696. if (decl->doc_comment) {
  5697. ce->info.user.doc_comment = zend_string_copy(decl->doc_comment);
  5698. }
  5699. if (UNEXPECTED((decl->flags & ZEND_ACC_ANON_CLASS))) {
  5700. /* Serialization is not supported for anonymous classes */
  5701. ce->serialize = zend_class_serialize_deny;
  5702. ce->unserialize = zend_class_unserialize_deny;
  5703. }
  5704. if (extends_ast) {
  5705. ce->parent_name =
  5706. zend_resolve_const_class_name_reference(extends_ast, "class name");
  5707. }
  5708. CG(active_class_entry) = ce;
  5709. if (implements_ast) {
  5710. zend_compile_implements(implements_ast);
  5711. }
  5712. zend_compile_stmt(stmt_ast);
  5713. /* Reset lineno for final opcodes and errors */
  5714. CG(zend_lineno) = ast->lineno;
  5715. if (ce->constructor) {
  5716. ce->constructor->common.fn_flags |= ZEND_ACC_CTOR;
  5717. if (ce->constructor->common.fn_flags & ZEND_ACC_STATIC) {
  5718. zend_error_noreturn(E_COMPILE_ERROR, "Constructor %s::%s() cannot be static",
  5719. ZSTR_VAL(ce->name), ZSTR_VAL(ce->constructor->common.function_name));
  5720. }
  5721. if (ce->constructor->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
  5722. zend_error_noreturn(E_COMPILE_ERROR,
  5723. "Constructor %s::%s() cannot declare a return type",
  5724. ZSTR_VAL(ce->name), ZSTR_VAL(ce->constructor->common.function_name));
  5725. }
  5726. }
  5727. if (ce->destructor) {
  5728. if (ce->destructor->common.fn_flags & ZEND_ACC_STATIC) {
  5729. zend_error_noreturn(E_COMPILE_ERROR, "Destructor %s::%s() cannot be static",
  5730. ZSTR_VAL(ce->name), ZSTR_VAL(ce->destructor->common.function_name));
  5731. } else if (ce->destructor->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
  5732. zend_error_noreturn(E_COMPILE_ERROR,
  5733. "Destructor %s::%s() cannot declare a return type",
  5734. ZSTR_VAL(ce->name), ZSTR_VAL(ce->destructor->common.function_name));
  5735. }
  5736. }
  5737. if (ce->clone) {
  5738. if (ce->clone->common.fn_flags & ZEND_ACC_STATIC) {
  5739. zend_error_noreturn(E_COMPILE_ERROR, "Clone method %s::%s() cannot be static",
  5740. ZSTR_VAL(ce->name), ZSTR_VAL(ce->clone->common.function_name));
  5741. } else if (ce->clone->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
  5742. zend_error_noreturn(E_COMPILE_ERROR,
  5743. "Clone method %s::%s() cannot declare a return type",
  5744. ZSTR_VAL(ce->name), ZSTR_VAL(ce->clone->common.function_name));
  5745. }
  5746. }
  5747. if ((ce->ce_flags & (ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) == ZEND_ACC_IMPLICIT_ABSTRACT_CLASS) {
  5748. zend_verify_abstract_class(ce);
  5749. }
  5750. CG(active_class_entry) = original_ce;
  5751. if (toplevel) {
  5752. ce->ce_flags |= ZEND_ACC_TOP_LEVEL;
  5753. }
  5754. if (toplevel
  5755. /* We currently don't early-bind classes that implement interfaces or use traits */
  5756. && !ce->num_interfaces && !ce->num_traits
  5757. && !(CG(compiler_options) & ZEND_COMPILE_PRELOAD)) {
  5758. if (extends_ast) {
  5759. zend_class_entry *parent_ce = zend_lookup_class_ex(
  5760. ce->parent_name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD);
  5761. if (parent_ce
  5762. && ((parent_ce->type != ZEND_INTERNAL_CLASS) || !(CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_CLASSES))
  5763. && ((parent_ce->type != ZEND_USER_CLASS) || !(CG(compiler_options) & ZEND_COMPILE_IGNORE_OTHER_FILES) || (parent_ce->info.user.filename == ce->info.user.filename))) {
  5764. CG(zend_lineno) = decl->end_lineno;
  5765. if (zend_try_early_bind(ce, parent_ce, lcname, NULL)) {
  5766. CG(zend_lineno) = ast->lineno;
  5767. zend_string_release(lcname);
  5768. return;
  5769. }
  5770. CG(zend_lineno) = ast->lineno;
  5771. }
  5772. } else if (EXPECTED(zend_hash_add_ptr(CG(class_table), lcname, ce) != NULL)) {
  5773. zend_string_release(lcname);
  5774. zend_build_properties_info_table(ce);
  5775. ce->ce_flags |= ZEND_ACC_LINKED;
  5776. return;
  5777. }
  5778. }
  5779. opline = get_next_op();
  5780. if (ce->parent_name) {
  5781. /* Lowercased parent name */
  5782. zend_string *lc_parent_name = zend_string_tolower(ce->parent_name);
  5783. opline->op2_type = IS_CONST;
  5784. LITERAL_STR(opline->op2, lc_parent_name);
  5785. }
  5786. opline->op1_type = IS_CONST;
  5787. LITERAL_STR(opline->op1, lcname);
  5788. if (decl->flags & ZEND_ACC_ANON_CLASS) {
  5789. opline->opcode = ZEND_DECLARE_ANON_CLASS;
  5790. opline->extended_value = zend_alloc_cache_slot();
  5791. zend_make_var_result(result, opline);
  5792. if (!zend_hash_add_ptr(CG(class_table), lcname, ce)) {
  5793. zend_error_noreturn(E_ERROR,
  5794. "Runtime definition key collision for %s. This is a bug", ZSTR_VAL(name));
  5795. }
  5796. } else {
  5797. zend_string *key = zend_build_runtime_definition_key(lcname, decl->start_lineno);
  5798. /* RTD key is placed after lcname literal in op1 */
  5799. zend_add_literal_string(&key);
  5800. if (!zend_hash_add_ptr(CG(class_table), key, ce)) {
  5801. zend_error_noreturn(E_ERROR,
  5802. "Runtime definition key collision for class %s. This is a bug", ZSTR_VAL(name));
  5803. }
  5804. opline->opcode = ZEND_DECLARE_CLASS;
  5805. if (extends_ast && toplevel
  5806. && (CG(compiler_options) & ZEND_COMPILE_DELAYED_BINDING)
  5807. /* We currently don't early-bind classes that implement interfaces or use traits */
  5808. && !ce->num_interfaces && !ce->num_traits
  5809. ) {
  5810. CG(active_op_array)->fn_flags |= ZEND_ACC_EARLY_BINDING;
  5811. opline->opcode = ZEND_DECLARE_CLASS_DELAYED;
  5812. opline->extended_value = zend_alloc_cache_slot();
  5813. opline->result_type = IS_UNUSED;
  5814. opline->result.opline_num = -1;
  5815. }
  5816. }
  5817. }
  5818. /* }}} */
  5819. static HashTable *zend_get_import_ht(uint32_t type) /* {{{ */
  5820. {
  5821. switch (type) {
  5822. case ZEND_SYMBOL_CLASS:
  5823. if (!FC(imports)) {
  5824. FC(imports) = emalloc(sizeof(HashTable));
  5825. zend_hash_init(FC(imports), 8, NULL, str_dtor, 0);
  5826. }
  5827. return FC(imports);
  5828. case ZEND_SYMBOL_FUNCTION:
  5829. if (!FC(imports_function)) {
  5830. FC(imports_function) = emalloc(sizeof(HashTable));
  5831. zend_hash_init(FC(imports_function), 8, NULL, str_dtor, 0);
  5832. }
  5833. return FC(imports_function);
  5834. case ZEND_SYMBOL_CONST:
  5835. if (!FC(imports_const)) {
  5836. FC(imports_const) = emalloc(sizeof(HashTable));
  5837. zend_hash_init(FC(imports_const), 8, NULL, str_dtor, 0);
  5838. }
  5839. return FC(imports_const);
  5840. EMPTY_SWITCH_DEFAULT_CASE()
  5841. }
  5842. return NULL;
  5843. }
  5844. /* }}} */
  5845. static char *zend_get_use_type_str(uint32_t type) /* {{{ */
  5846. {
  5847. switch (type) {
  5848. case ZEND_SYMBOL_CLASS:
  5849. return "";
  5850. case ZEND_SYMBOL_FUNCTION:
  5851. return " function";
  5852. case ZEND_SYMBOL_CONST:
  5853. return " const";
  5854. EMPTY_SWITCH_DEFAULT_CASE()
  5855. }
  5856. return " unknown";
  5857. }
  5858. /* }}} */
  5859. static void zend_check_already_in_use(uint32_t type, zend_string *old_name, zend_string *new_name, zend_string *check_name) /* {{{ */
  5860. {
  5861. if (zend_string_equals_ci(old_name, check_name)) {
  5862. return;
  5863. }
  5864. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name "
  5865. "is already in use", zend_get_use_type_str(type), ZSTR_VAL(old_name), ZSTR_VAL(new_name));
  5866. }
  5867. /* }}} */
  5868. void zend_compile_use(zend_ast *ast) /* {{{ */
  5869. {
  5870. zend_ast_list *list = zend_ast_get_list(ast);
  5871. uint32_t i;
  5872. zend_string *current_ns = FC(current_namespace);
  5873. uint32_t type = ast->attr;
  5874. HashTable *current_import = zend_get_import_ht(type);
  5875. zend_bool case_sensitive = type == ZEND_SYMBOL_CONST;
  5876. for (i = 0; i < list->children; ++i) {
  5877. zend_ast *use_ast = list->child[i];
  5878. zend_ast *old_name_ast = use_ast->child[0];
  5879. zend_ast *new_name_ast = use_ast->child[1];
  5880. zend_string *old_name = zend_ast_get_str(old_name_ast);
  5881. zend_string *new_name, *lookup_name;
  5882. if (new_name_ast) {
  5883. new_name = zend_string_copy(zend_ast_get_str(new_name_ast));
  5884. } else {
  5885. const char *unqualified_name;
  5886. size_t unqualified_name_len;
  5887. if (zend_get_unqualified_name(old_name, &unqualified_name, &unqualified_name_len)) {
  5888. /* The form "use A\B" is equivalent to "use A\B as B" */
  5889. new_name = zend_string_init(unqualified_name, unqualified_name_len, 0);
  5890. } else {
  5891. new_name = zend_string_copy(old_name);
  5892. if (!current_ns) {
  5893. zend_error(E_WARNING, "The use statement with non-compound name '%s' "
  5894. "has no effect", ZSTR_VAL(new_name));
  5895. }
  5896. }
  5897. }
  5898. if (case_sensitive) {
  5899. lookup_name = zend_string_copy(new_name);
  5900. } else {
  5901. lookup_name = zend_string_tolower(new_name);
  5902. }
  5903. if (type == ZEND_SYMBOL_CLASS && zend_is_reserved_class_name(new_name)) {
  5904. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use %s as %s because '%s' "
  5905. "is a special class name", ZSTR_VAL(old_name), ZSTR_VAL(new_name), ZSTR_VAL(new_name));
  5906. }
  5907. if (current_ns) {
  5908. zend_string *ns_name = zend_string_alloc(ZSTR_LEN(current_ns) + 1 + ZSTR_LEN(new_name), 0);
  5909. zend_str_tolower_copy(ZSTR_VAL(ns_name), ZSTR_VAL(current_ns), ZSTR_LEN(current_ns));
  5910. ZSTR_VAL(ns_name)[ZSTR_LEN(current_ns)] = '\\';
  5911. memcpy(ZSTR_VAL(ns_name) + ZSTR_LEN(current_ns) + 1, ZSTR_VAL(lookup_name), ZSTR_LEN(lookup_name) + 1);
  5912. if (zend_have_seen_symbol(ns_name, type)) {
  5913. zend_check_already_in_use(type, old_name, new_name, ns_name);
  5914. }
  5915. zend_string_efree(ns_name);
  5916. } else if (zend_have_seen_symbol(lookup_name, type)) {
  5917. zend_check_already_in_use(type, old_name, new_name, lookup_name);
  5918. }
  5919. zend_string_addref(old_name);
  5920. old_name = zend_new_interned_string(old_name);
  5921. if (!zend_hash_add_ptr(current_import, lookup_name, old_name)) {
  5922. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name "
  5923. "is already in use", zend_get_use_type_str(type), ZSTR_VAL(old_name), ZSTR_VAL(new_name));
  5924. }
  5925. zend_string_release_ex(lookup_name, 0);
  5926. zend_string_release_ex(new_name, 0);
  5927. }
  5928. }
  5929. /* }}} */
  5930. void zend_compile_group_use(zend_ast *ast) /* {{{ */
  5931. {
  5932. uint32_t i;
  5933. zend_string *ns = zend_ast_get_str(ast->child[0]);
  5934. zend_ast_list *list = zend_ast_get_list(ast->child[1]);
  5935. for (i = 0; i < list->children; i++) {
  5936. zend_ast *inline_use, *use = list->child[i];
  5937. zval *name_zval = zend_ast_get_zval(use->child[0]);
  5938. zend_string *name = Z_STR_P(name_zval);
  5939. zend_string *compound_ns = zend_concat_names(ZSTR_VAL(ns), ZSTR_LEN(ns), ZSTR_VAL(name), ZSTR_LEN(name));
  5940. zend_string_release_ex(name, 0);
  5941. ZVAL_STR(name_zval, compound_ns);
  5942. inline_use = zend_ast_create_list(1, ZEND_AST_USE, use);
  5943. inline_use->attr = ast->attr ? ast->attr : use->attr;
  5944. zend_compile_use(inline_use);
  5945. }
  5946. }
  5947. /* }}} */
  5948. void zend_compile_const_decl(zend_ast *ast) /* {{{ */
  5949. {
  5950. zend_ast_list *list = zend_ast_get_list(ast);
  5951. uint32_t i;
  5952. for (i = 0; i < list->children; ++i) {
  5953. zend_ast *const_ast = list->child[i];
  5954. zend_ast *name_ast = const_ast->child[0];
  5955. zend_ast *value_ast = const_ast->child[1];
  5956. zend_string *unqualified_name = zend_ast_get_str(name_ast);
  5957. zend_string *name;
  5958. znode name_node, value_node;
  5959. zval *value_zv = &value_node.u.constant;
  5960. value_node.op_type = IS_CONST;
  5961. zend_const_expr_to_zval(value_zv, value_ast);
  5962. if (zend_get_special_const(ZSTR_VAL(unqualified_name), ZSTR_LEN(unqualified_name))) {
  5963. zend_error_noreturn(E_COMPILE_ERROR,
  5964. "Cannot redeclare constant '%s'", ZSTR_VAL(unqualified_name));
  5965. }
  5966. name = zend_prefix_with_ns(unqualified_name);
  5967. name = zend_new_interned_string(name);
  5968. if (FC(imports_const)) {
  5969. zend_string *import_name = zend_hash_find_ptr(FC(imports_const), unqualified_name);
  5970. if (import_name && !zend_string_equals(import_name, name)) {
  5971. zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare const %s because "
  5972. "the name is already in use", ZSTR_VAL(name));
  5973. }
  5974. }
  5975. name_node.op_type = IS_CONST;
  5976. ZVAL_STR(&name_node.u.constant, name);
  5977. zend_emit_op(NULL, ZEND_DECLARE_CONST, &name_node, &value_node);
  5978. zend_register_seen_symbol(name, ZEND_SYMBOL_CONST);
  5979. }
  5980. }
  5981. /* }}}*/
  5982. void zend_compile_namespace(zend_ast *ast) /* {{{ */
  5983. {
  5984. zend_ast *name_ast = ast->child[0];
  5985. zend_ast *stmt_ast = ast->child[1];
  5986. zend_string *name;
  5987. zend_bool with_bracket = stmt_ast != NULL;
  5988. /* handle mixed syntax declaration or nested namespaces */
  5989. if (!FC(has_bracketed_namespaces)) {
  5990. if (FC(current_namespace)) {
  5991. /* previous namespace declarations were unbracketed */
  5992. if (with_bracket) {
  5993. zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations "
  5994. "with unbracketed namespace declarations");
  5995. }
  5996. }
  5997. } else {
  5998. /* previous namespace declarations were bracketed */
  5999. if (!with_bracket) {
  6000. zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations "
  6001. "with unbracketed namespace declarations");
  6002. } else if (FC(current_namespace) || FC(in_namespace)) {
  6003. zend_error_noreturn(E_COMPILE_ERROR, "Namespace declarations cannot be nested");
  6004. }
  6005. }
  6006. if (((!with_bracket && !FC(current_namespace))
  6007. || (with_bracket && !FC(has_bracketed_namespaces))) && CG(active_op_array)->last > 0
  6008. ) {
  6009. /* ignore ZEND_EXT_STMT and ZEND_TICKS */
  6010. uint32_t num = CG(active_op_array)->last;
  6011. while (num > 0 &&
  6012. (CG(active_op_array)->opcodes[num-1].opcode == ZEND_EXT_STMT ||
  6013. CG(active_op_array)->opcodes[num-1].opcode == ZEND_TICKS)) {
  6014. --num;
  6015. }
  6016. if (num > 0) {
  6017. zend_error_noreturn(E_COMPILE_ERROR, "Namespace declaration statement has to be "
  6018. "the very first statement or after any declare call in the script");
  6019. }
  6020. }
  6021. if (FC(current_namespace)) {
  6022. zend_string_release_ex(FC(current_namespace), 0);
  6023. }
  6024. if (name_ast) {
  6025. name = zend_ast_get_str(name_ast);
  6026. if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) {
  6027. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use '%s' as namespace name", ZSTR_VAL(name));
  6028. }
  6029. FC(current_namespace) = zend_string_copy(name);
  6030. } else {
  6031. FC(current_namespace) = NULL;
  6032. }
  6033. zend_reset_import_tables();
  6034. FC(in_namespace) = 1;
  6035. if (with_bracket) {
  6036. FC(has_bracketed_namespaces) = 1;
  6037. }
  6038. if (stmt_ast) {
  6039. zend_compile_top_stmt(stmt_ast);
  6040. zend_end_namespace();
  6041. }
  6042. }
  6043. /* }}} */
  6044. void zend_compile_halt_compiler(zend_ast *ast) /* {{{ */
  6045. {
  6046. zend_ast *offset_ast = ast->child[0];
  6047. zend_long offset = Z_LVAL_P(zend_ast_get_zval(offset_ast));
  6048. zend_string *filename, *name;
  6049. const char const_name[] = "__COMPILER_HALT_OFFSET__";
  6050. if (FC(has_bracketed_namespaces) && FC(in_namespace)) {
  6051. zend_error_noreturn(E_COMPILE_ERROR,
  6052. "__HALT_COMPILER() can only be used from the outermost scope");
  6053. }
  6054. filename = zend_get_compiled_filename();
  6055. name = zend_mangle_property_name(const_name, sizeof(const_name) - 1,
  6056. ZSTR_VAL(filename), ZSTR_LEN(filename), 0);
  6057. zend_register_long_constant(ZSTR_VAL(name), ZSTR_LEN(name), offset, CONST_CS, 0);
  6058. zend_string_release_ex(name, 0);
  6059. }
  6060. /* }}} */
  6061. static zend_bool zend_try_ct_eval_magic_const(zval *zv, zend_ast *ast) /* {{{ */
  6062. {
  6063. zend_op_array *op_array = CG(active_op_array);
  6064. zend_class_entry *ce = CG(active_class_entry);
  6065. switch (ast->attr) {
  6066. case T_LINE:
  6067. ZVAL_LONG(zv, ast->lineno);
  6068. break;
  6069. case T_FILE:
  6070. ZVAL_STR_COPY(zv, CG(compiled_filename));
  6071. break;
  6072. case T_DIR:
  6073. {
  6074. zend_string *filename = CG(compiled_filename);
  6075. zend_string *dirname = zend_string_init(ZSTR_VAL(filename), ZSTR_LEN(filename), 0);
  6076. #ifdef ZEND_WIN32
  6077. ZSTR_LEN(dirname) = php_win32_ioutil_dirname(ZSTR_VAL(dirname), ZSTR_LEN(dirname));
  6078. #else
  6079. ZSTR_LEN(dirname) = zend_dirname(ZSTR_VAL(dirname), ZSTR_LEN(dirname));
  6080. #endif
  6081. if (strcmp(ZSTR_VAL(dirname), ".") == 0) {
  6082. dirname = zend_string_extend(dirname, MAXPATHLEN, 0);
  6083. #if HAVE_GETCWD
  6084. ZEND_IGNORE_VALUE(VCWD_GETCWD(ZSTR_VAL(dirname), MAXPATHLEN));
  6085. #elif HAVE_GETWD
  6086. ZEND_IGNORE_VALUE(VCWD_GETWD(ZSTR_VAL(dirname)));
  6087. #endif
  6088. ZSTR_LEN(dirname) = strlen(ZSTR_VAL(dirname));
  6089. }
  6090. ZVAL_STR(zv, dirname);
  6091. break;
  6092. }
  6093. case T_FUNC_C:
  6094. if (op_array && op_array->function_name) {
  6095. ZVAL_STR_COPY(zv, op_array->function_name);
  6096. } else {
  6097. ZVAL_EMPTY_STRING(zv);
  6098. }
  6099. break;
  6100. case T_METHOD_C:
  6101. /* Detect whether we are directly inside a class (e.g. a class constant) and treat
  6102. * this as not being inside a function. */
  6103. if (op_array && ce && !op_array->scope && !(op_array->fn_flags & ZEND_ACC_CLOSURE)) {
  6104. op_array = NULL;
  6105. }
  6106. if (op_array && op_array->function_name) {
  6107. if (op_array->scope) {
  6108. ZVAL_NEW_STR(zv,
  6109. zend_create_member_string(op_array->scope->name, op_array->function_name));
  6110. } else {
  6111. ZVAL_STR_COPY(zv, op_array->function_name);
  6112. }
  6113. } else {
  6114. ZVAL_EMPTY_STRING(zv);
  6115. }
  6116. break;
  6117. case T_CLASS_C:
  6118. if (ce) {
  6119. if ((ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
  6120. return 0;
  6121. } else {
  6122. ZVAL_STR_COPY(zv, ce->name);
  6123. }
  6124. } else {
  6125. ZVAL_EMPTY_STRING(zv);
  6126. }
  6127. break;
  6128. case T_TRAIT_C:
  6129. if (ce && (ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
  6130. ZVAL_STR_COPY(zv, ce->name);
  6131. } else {
  6132. ZVAL_EMPTY_STRING(zv);
  6133. }
  6134. break;
  6135. case T_NS_C:
  6136. if (FC(current_namespace)) {
  6137. ZVAL_STR_COPY(zv, FC(current_namespace));
  6138. } else {
  6139. ZVAL_EMPTY_STRING(zv);
  6140. }
  6141. break;
  6142. EMPTY_SWITCH_DEFAULT_CASE()
  6143. }
  6144. return 1;
  6145. }
  6146. /* }}} */
  6147. ZEND_API zend_bool zend_binary_op_produces_error(uint32_t opcode, zval *op1, zval *op2) /* {{{ */
  6148. {
  6149. if ((opcode == ZEND_CONCAT || opcode == ZEND_FAST_CONCAT)) {
  6150. /* Array to string warning. */
  6151. return Z_TYPE_P(op1) == IS_ARRAY || Z_TYPE_P(op2) == IS_ARRAY;
  6152. }
  6153. if (!(opcode == ZEND_ADD || opcode == ZEND_SUB || opcode == ZEND_MUL || opcode == ZEND_DIV
  6154. || opcode == ZEND_POW || opcode == ZEND_MOD || opcode == ZEND_SL || opcode == ZEND_SR
  6155. || opcode == ZEND_BW_OR || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR)) {
  6156. /* Only the numeric operations throw errors. */
  6157. return 0;
  6158. }
  6159. if (Z_TYPE_P(op1) == IS_ARRAY || Z_TYPE_P(op2) == IS_ARRAY) {
  6160. if (opcode == ZEND_ADD && Z_TYPE_P(op1) == IS_ARRAY && Z_TYPE_P(op2) == IS_ARRAY) {
  6161. /* Adding two arrays is allowed. */
  6162. return 0;
  6163. }
  6164. /* Numeric operators throw when one of the operands is an array. */
  6165. return 1;
  6166. }
  6167. /* While basic arithmetic operators always produce numeric string errors,
  6168. * bitwise operators don't produce errors if both operands are strings */
  6169. if ((opcode == ZEND_BW_OR || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR)
  6170. && Z_TYPE_P(op1) == IS_STRING && Z_TYPE_P(op2) == IS_STRING) {
  6171. return 0;
  6172. }
  6173. if (Z_TYPE_P(op1) == IS_STRING
  6174. && !is_numeric_string(Z_STRVAL_P(op1), Z_STRLEN_P(op1), NULL, NULL, 0)) {
  6175. return 1;
  6176. }
  6177. if (Z_TYPE_P(op2) == IS_STRING
  6178. && !is_numeric_string(Z_STRVAL_P(op2), Z_STRLEN_P(op2), NULL, NULL, 0)) {
  6179. return 1;
  6180. }
  6181. if ((opcode == ZEND_MOD && zval_get_long(op2) == 0)
  6182. || (opcode == ZEND_DIV && zval_get_double(op2) == 0.0)) {
  6183. /* Division by zero throws an error. */
  6184. return 1;
  6185. }
  6186. if ((opcode == ZEND_SL || opcode == ZEND_SR) && zval_get_long(op2) < 0) {
  6187. /* Shift by negative number throws an error. */
  6188. return 1;
  6189. }
  6190. return 0;
  6191. }
  6192. /* }}} */
  6193. static inline zend_bool zend_try_ct_eval_binary_op(zval *result, uint32_t opcode, zval *op1, zval *op2) /* {{{ */
  6194. {
  6195. if (zend_binary_op_produces_error(opcode, op1, op2)) {
  6196. return 0;
  6197. }
  6198. binary_op_type fn = get_binary_op(opcode);
  6199. fn(result, op1, op2);
  6200. return 1;
  6201. }
  6202. /* }}} */
  6203. static inline void zend_ct_eval_unary_op(zval *result, uint32_t opcode, zval *op) /* {{{ */
  6204. {
  6205. unary_op_type fn = get_unary_op(opcode);
  6206. fn(result, op);
  6207. }
  6208. /* }}} */
  6209. static inline zend_bool zend_try_ct_eval_unary_pm(zval *result, zend_ast_kind kind, zval *op) /* {{{ */
  6210. {
  6211. zval left;
  6212. ZVAL_LONG(&left, (kind == ZEND_AST_UNARY_PLUS) ? 1 : -1);
  6213. return zend_try_ct_eval_binary_op(result, ZEND_MUL, &left, op);
  6214. }
  6215. /* }}} */
  6216. static inline void zend_ct_eval_greater(zval *result, zend_ast_kind kind, zval *op1, zval *op2) /* {{{ */
  6217. {
  6218. binary_op_type fn = kind == ZEND_AST_GREATER
  6219. ? is_smaller_function : is_smaller_or_equal_function;
  6220. fn(result, op2, op1);
  6221. }
  6222. /* }}} */
  6223. static zend_bool zend_try_ct_eval_array(zval *result, zend_ast *ast) /* {{{ */
  6224. {
  6225. zend_ast_list *list = zend_ast_get_list(ast);
  6226. zend_ast *last_elem_ast = NULL;
  6227. uint32_t i;
  6228. zend_bool is_constant = 1;
  6229. if (ast->attr == ZEND_ARRAY_SYNTAX_LIST) {
  6230. zend_error(E_COMPILE_ERROR, "Cannot use list() as standalone expression");
  6231. }
  6232. /* First ensure that *all* child nodes are constant and by-val */
  6233. for (i = 0; i < list->children; ++i) {
  6234. zend_ast *elem_ast = list->child[i];
  6235. if (elem_ast == NULL) {
  6236. /* Report error at line of last non-empty element */
  6237. if (last_elem_ast) {
  6238. CG(zend_lineno) = zend_ast_get_lineno(last_elem_ast);
  6239. }
  6240. zend_error(E_COMPILE_ERROR, "Cannot use empty array elements in arrays");
  6241. }
  6242. if (elem_ast->kind != ZEND_AST_UNPACK) {
  6243. zend_eval_const_expr(&elem_ast->child[0]);
  6244. zend_eval_const_expr(&elem_ast->child[1]);
  6245. if (elem_ast->attr /* by_ref */ || elem_ast->child[0]->kind != ZEND_AST_ZVAL
  6246. || (elem_ast->child[1] && elem_ast->child[1]->kind != ZEND_AST_ZVAL)
  6247. ) {
  6248. is_constant = 0;
  6249. }
  6250. } else {
  6251. zend_eval_const_expr(&elem_ast->child[0]);
  6252. if (elem_ast->child[0]->kind != ZEND_AST_ZVAL) {
  6253. is_constant = 0;
  6254. }
  6255. }
  6256. last_elem_ast = elem_ast;
  6257. }
  6258. if (!is_constant) {
  6259. return 0;
  6260. }
  6261. if (!list->children) {
  6262. ZVAL_EMPTY_ARRAY(result);
  6263. return 1;
  6264. }
  6265. array_init_size(result, list->children);
  6266. for (i = 0; i < list->children; ++i) {
  6267. zend_ast *elem_ast = list->child[i];
  6268. zend_ast *value_ast = elem_ast->child[0];
  6269. zend_ast *key_ast;
  6270. zval *value = zend_ast_get_zval(value_ast);
  6271. if (elem_ast->kind == ZEND_AST_UNPACK) {
  6272. if (Z_TYPE_P(value) == IS_ARRAY) {
  6273. HashTable *ht = Z_ARRVAL_P(value);
  6274. zval *val;
  6275. zend_string *key;
  6276. ZEND_HASH_FOREACH_STR_KEY_VAL(ht, key, val) {
  6277. if (key) {
  6278. zend_error_noreturn(E_COMPILE_ERROR, "Cannot unpack array with string keys");
  6279. }
  6280. if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), val)) {
  6281. zval_ptr_dtor(result);
  6282. return 0;
  6283. }
  6284. Z_TRY_ADDREF_P(val);
  6285. } ZEND_HASH_FOREACH_END();
  6286. continue;
  6287. } else {
  6288. zend_error_noreturn(E_COMPILE_ERROR, "Only arrays and Traversables can be unpacked");
  6289. }
  6290. }
  6291. Z_TRY_ADDREF_P(value);
  6292. key_ast = elem_ast->child[1];
  6293. if (key_ast) {
  6294. zval *key = zend_ast_get_zval(key_ast);
  6295. switch (Z_TYPE_P(key)) {
  6296. case IS_LONG:
  6297. zend_hash_index_update(Z_ARRVAL_P(result), Z_LVAL_P(key), value);
  6298. break;
  6299. case IS_STRING:
  6300. zend_symtable_update(Z_ARRVAL_P(result), Z_STR_P(key), value);
  6301. break;
  6302. case IS_DOUBLE:
  6303. zend_hash_index_update(Z_ARRVAL_P(result),
  6304. zend_dval_to_lval(Z_DVAL_P(key)), value);
  6305. break;
  6306. case IS_FALSE:
  6307. zend_hash_index_update(Z_ARRVAL_P(result), 0, value);
  6308. break;
  6309. case IS_TRUE:
  6310. zend_hash_index_update(Z_ARRVAL_P(result), 1, value);
  6311. break;
  6312. case IS_NULL:
  6313. zend_hash_update(Z_ARRVAL_P(result), ZSTR_EMPTY_ALLOC(), value);
  6314. break;
  6315. default:
  6316. zend_error_noreturn(E_COMPILE_ERROR, "Illegal offset type");
  6317. break;
  6318. }
  6319. } else if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), value)) {
  6320. zval_ptr_dtor_nogc(value);
  6321. zval_ptr_dtor(result);
  6322. return 0;
  6323. }
  6324. }
  6325. return 1;
  6326. }
  6327. /* }}} */
  6328. void zend_compile_binary_op(znode *result, zend_ast *ast) /* {{{ */
  6329. {
  6330. zend_ast *left_ast = ast->child[0];
  6331. zend_ast *right_ast = ast->child[1];
  6332. uint32_t opcode = ast->attr;
  6333. znode left_node, right_node;
  6334. zend_compile_expr(&left_node, left_ast);
  6335. zend_compile_expr(&right_node, right_ast);
  6336. if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
  6337. if (zend_try_ct_eval_binary_op(&result->u.constant, opcode,
  6338. &left_node.u.constant, &right_node.u.constant)
  6339. ) {
  6340. result->op_type = IS_CONST;
  6341. zval_ptr_dtor(&left_node.u.constant);
  6342. zval_ptr_dtor(&right_node.u.constant);
  6343. return;
  6344. }
  6345. }
  6346. do {
  6347. if (opcode == ZEND_IS_EQUAL || opcode == ZEND_IS_NOT_EQUAL) {
  6348. if (left_node.op_type == IS_CONST) {
  6349. if (Z_TYPE(left_node.u.constant) == IS_FALSE) {
  6350. opcode = (opcode == ZEND_IS_NOT_EQUAL) ? ZEND_BOOL : ZEND_BOOL_NOT;
  6351. zend_emit_op_tmp(result, opcode, &right_node, NULL);
  6352. break;
  6353. } else if (Z_TYPE(left_node.u.constant) == IS_TRUE) {
  6354. opcode = (opcode == ZEND_IS_EQUAL) ? ZEND_BOOL : ZEND_BOOL_NOT;
  6355. zend_emit_op_tmp(result, opcode, &right_node, NULL);
  6356. break;
  6357. }
  6358. } else if (right_node.op_type == IS_CONST) {
  6359. if (Z_TYPE(right_node.u.constant) == IS_FALSE) {
  6360. opcode = (opcode == ZEND_IS_NOT_EQUAL) ? ZEND_BOOL : ZEND_BOOL_NOT;
  6361. zend_emit_op_tmp(result, opcode, &left_node, NULL);
  6362. break;
  6363. } else if (Z_TYPE(right_node.u.constant) == IS_TRUE) {
  6364. opcode = (opcode == ZEND_IS_EQUAL) ? ZEND_BOOL : ZEND_BOOL_NOT;
  6365. zend_emit_op_tmp(result, opcode, &left_node, NULL);
  6366. break;
  6367. }
  6368. }
  6369. } else if (opcode == ZEND_IS_IDENTICAL || opcode == ZEND_IS_NOT_IDENTICAL) {
  6370. /* convert $x === null to is_null($x) (i.e. ZEND_TYPE_CHECK opcode). Do the same thing for false/true. (covers IS_NULL, IS_FALSE, and IS_TRUE) */
  6371. if (left_node.op_type == IS_CONST) {
  6372. if (Z_TYPE(left_node.u.constant) <= IS_TRUE && Z_TYPE(left_node.u.constant) >= IS_NULL) {
  6373. zend_op *opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &right_node, NULL);
  6374. opline->extended_value =
  6375. (opcode == ZEND_IS_IDENTICAL) ?
  6376. (1 << Z_TYPE(left_node.u.constant)) :
  6377. (MAY_BE_ANY - (1 << Z_TYPE(left_node.u.constant)));
  6378. return;
  6379. }
  6380. } else if (right_node.op_type == IS_CONST) {
  6381. if (Z_TYPE(right_node.u.constant) <= IS_TRUE && Z_TYPE(right_node.u.constant) >= IS_NULL) {
  6382. zend_op *opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &left_node, NULL);
  6383. opline->extended_value =
  6384. (opcode == ZEND_IS_IDENTICAL) ?
  6385. (1 << Z_TYPE(right_node.u.constant)) :
  6386. (MAY_BE_ANY - (1 << Z_TYPE(right_node.u.constant)));
  6387. return;
  6388. }
  6389. }
  6390. } else if (opcode == ZEND_CONCAT) {
  6391. /* convert constant operands to strings at compile-time */
  6392. if (left_node.op_type == IS_CONST) {
  6393. if (Z_TYPE(left_node.u.constant) == IS_ARRAY) {
  6394. zend_emit_op_tmp(&left_node, ZEND_CAST, &left_node, NULL)->extended_value = IS_STRING;
  6395. } else {
  6396. convert_to_string(&left_node.u.constant);
  6397. }
  6398. }
  6399. if (right_node.op_type == IS_CONST) {
  6400. if (Z_TYPE(right_node.u.constant) == IS_ARRAY) {
  6401. zend_emit_op_tmp(&right_node, ZEND_CAST, &right_node, NULL)->extended_value = IS_STRING;
  6402. } else {
  6403. convert_to_string(&right_node.u.constant);
  6404. }
  6405. }
  6406. if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
  6407. opcode = ZEND_FAST_CONCAT;
  6408. }
  6409. }
  6410. zend_emit_op_tmp(result, opcode, &left_node, &right_node);
  6411. } while (0);
  6412. }
  6413. /* }}} */
  6414. /* We do not use zend_compile_binary_op for this because we want to retain the left-to-right
  6415. * evaluation order. */
  6416. void zend_compile_greater(znode *result, zend_ast *ast) /* {{{ */
  6417. {
  6418. zend_ast *left_ast = ast->child[0];
  6419. zend_ast *right_ast = ast->child[1];
  6420. znode left_node, right_node;
  6421. ZEND_ASSERT(ast->kind == ZEND_AST_GREATER || ast->kind == ZEND_AST_GREATER_EQUAL);
  6422. zend_compile_expr(&left_node, left_ast);
  6423. zend_compile_expr(&right_node, right_ast);
  6424. if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
  6425. result->op_type = IS_CONST;
  6426. zend_ct_eval_greater(&result->u.constant, ast->kind,
  6427. &left_node.u.constant, &right_node.u.constant);
  6428. zval_ptr_dtor(&left_node.u.constant);
  6429. zval_ptr_dtor(&right_node.u.constant);
  6430. return;
  6431. }
  6432. zend_emit_op_tmp(result,
  6433. ast->kind == ZEND_AST_GREATER ? ZEND_IS_SMALLER : ZEND_IS_SMALLER_OR_EQUAL,
  6434. &right_node, &left_node);
  6435. }
  6436. /* }}} */
  6437. void zend_compile_unary_op(znode *result, zend_ast *ast) /* {{{ */
  6438. {
  6439. zend_ast *expr_ast = ast->child[0];
  6440. uint32_t opcode = ast->attr;
  6441. znode expr_node;
  6442. zend_compile_expr(&expr_node, expr_ast);
  6443. if (expr_node.op_type == IS_CONST) {
  6444. result->op_type = IS_CONST;
  6445. zend_ct_eval_unary_op(&result->u.constant, opcode,
  6446. &expr_node.u.constant);
  6447. zval_ptr_dtor(&expr_node.u.constant);
  6448. return;
  6449. }
  6450. zend_emit_op_tmp(result, opcode, &expr_node, NULL);
  6451. }
  6452. /* }}} */
  6453. void zend_compile_unary_pm(znode *result, zend_ast *ast) /* {{{ */
  6454. {
  6455. zend_ast *expr_ast = ast->child[0];
  6456. znode expr_node;
  6457. znode lefthand_node;
  6458. ZEND_ASSERT(ast->kind == ZEND_AST_UNARY_PLUS || ast->kind == ZEND_AST_UNARY_MINUS);
  6459. zend_compile_expr(&expr_node, expr_ast);
  6460. if (expr_node.op_type == IS_CONST
  6461. && zend_try_ct_eval_unary_pm(&result->u.constant, ast->kind, &expr_node.u.constant)) {
  6462. result->op_type = IS_CONST;
  6463. zval_ptr_dtor(&expr_node.u.constant);
  6464. return;
  6465. }
  6466. lefthand_node.op_type = IS_CONST;
  6467. ZVAL_LONG(&lefthand_node.u.constant, (ast->kind == ZEND_AST_UNARY_PLUS) ? 1 : -1);
  6468. zend_emit_op_tmp(result, ZEND_MUL, &lefthand_node, &expr_node);
  6469. }
  6470. /* }}} */
  6471. void zend_compile_short_circuiting(znode *result, zend_ast *ast) /* {{{ */
  6472. {
  6473. zend_ast *left_ast = ast->child[0];
  6474. zend_ast *right_ast = ast->child[1];
  6475. znode left_node, right_node;
  6476. zend_op *opline_jmpz, *opline_bool;
  6477. uint32_t opnum_jmpz;
  6478. ZEND_ASSERT(ast->kind == ZEND_AST_AND || ast->kind == ZEND_AST_OR);
  6479. zend_compile_expr(&left_node, left_ast);
  6480. if (left_node.op_type == IS_CONST) {
  6481. if ((ast->kind == ZEND_AST_AND && !zend_is_true(&left_node.u.constant))
  6482. || (ast->kind == ZEND_AST_OR && zend_is_true(&left_node.u.constant))) {
  6483. result->op_type = IS_CONST;
  6484. ZVAL_BOOL(&result->u.constant, zend_is_true(&left_node.u.constant));
  6485. } else {
  6486. zend_compile_expr(&right_node, right_ast);
  6487. if (right_node.op_type == IS_CONST) {
  6488. result->op_type = IS_CONST;
  6489. ZVAL_BOOL(&result->u.constant, zend_is_true(&right_node.u.constant));
  6490. zval_ptr_dtor(&right_node.u.constant);
  6491. } else {
  6492. zend_emit_op_tmp(result, ZEND_BOOL, &right_node, NULL);
  6493. }
  6494. }
  6495. zval_ptr_dtor(&left_node.u.constant);
  6496. return;
  6497. }
  6498. opnum_jmpz = get_next_op_number();
  6499. opline_jmpz = zend_emit_op(NULL, ast->kind == ZEND_AST_AND ? ZEND_JMPZ_EX : ZEND_JMPNZ_EX,
  6500. &left_node, NULL);
  6501. if (left_node.op_type == IS_TMP_VAR) {
  6502. SET_NODE(opline_jmpz->result, &left_node);
  6503. GET_NODE(result, opline_jmpz->result);
  6504. } else {
  6505. zend_make_tmp_result(result, opline_jmpz);
  6506. }
  6507. zend_compile_expr(&right_node, right_ast);
  6508. opline_bool = zend_emit_op(NULL, ZEND_BOOL, &right_node, NULL);
  6509. SET_NODE(opline_bool->result, result);
  6510. zend_update_jump_target_to_next(opnum_jmpz);
  6511. }
  6512. /* }}} */
  6513. void zend_compile_post_incdec(znode *result, zend_ast *ast) /* {{{ */
  6514. {
  6515. zend_ast *var_ast = ast->child[0];
  6516. ZEND_ASSERT(ast->kind == ZEND_AST_POST_INC || ast->kind == ZEND_AST_POST_DEC);
  6517. zend_ensure_writable_variable(var_ast);
  6518. if (var_ast->kind == ZEND_AST_PROP) {
  6519. zend_op *opline = zend_compile_prop(NULL, var_ast, BP_VAR_RW, 0);
  6520. opline->opcode = ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC_OBJ : ZEND_POST_DEC_OBJ;
  6521. zend_make_tmp_result(result, opline);
  6522. } else if (var_ast->kind == ZEND_AST_STATIC_PROP) {
  6523. zend_op *opline = zend_compile_static_prop(NULL, var_ast, BP_VAR_RW, 0, 0);
  6524. opline->opcode = ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC_STATIC_PROP : ZEND_POST_DEC_STATIC_PROP;
  6525. zend_make_tmp_result(result, opline);
  6526. } else {
  6527. znode var_node;
  6528. zend_compile_var(&var_node, var_ast, BP_VAR_RW, 0);
  6529. zend_emit_op_tmp(result, ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC : ZEND_POST_DEC,
  6530. &var_node, NULL);
  6531. }
  6532. }
  6533. /* }}} */
  6534. void zend_compile_pre_incdec(znode *result, zend_ast *ast) /* {{{ */
  6535. {
  6536. zend_ast *var_ast = ast->child[0];
  6537. ZEND_ASSERT(ast->kind == ZEND_AST_PRE_INC || ast->kind == ZEND_AST_PRE_DEC);
  6538. zend_ensure_writable_variable(var_ast);
  6539. if (var_ast->kind == ZEND_AST_PROP) {
  6540. zend_op *opline = zend_compile_prop(result, var_ast, BP_VAR_RW, 0);
  6541. opline->opcode = ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC_OBJ : ZEND_PRE_DEC_OBJ;
  6542. opline->result_type = IS_TMP_VAR;
  6543. result->op_type = IS_TMP_VAR;
  6544. } else if (var_ast->kind == ZEND_AST_STATIC_PROP) {
  6545. zend_op *opline = zend_compile_static_prop(result, var_ast, BP_VAR_RW, 0, 0);
  6546. opline->opcode = ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC_STATIC_PROP : ZEND_PRE_DEC_STATIC_PROP;
  6547. opline->result_type = IS_TMP_VAR;
  6548. result->op_type = IS_TMP_VAR;
  6549. } else {
  6550. znode var_node;
  6551. zend_compile_var(&var_node, var_ast, BP_VAR_RW, 0);
  6552. zend_emit_op_tmp(result, ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC : ZEND_PRE_DEC,
  6553. &var_node, NULL);
  6554. }
  6555. }
  6556. /* }}} */
  6557. void zend_compile_cast(znode *result, zend_ast *ast) /* {{{ */
  6558. {
  6559. zend_ast *expr_ast = ast->child[0];
  6560. znode expr_node;
  6561. zend_op *opline;
  6562. zend_compile_expr(&expr_node, expr_ast);
  6563. if (ast->attr == _IS_BOOL) {
  6564. opline = zend_emit_op_tmp(result, ZEND_BOOL, &expr_node, NULL);
  6565. } else if (ast->attr == IS_NULL) {
  6566. zend_error(E_COMPILE_ERROR, "The (unset) cast is no longer supported");
  6567. } else {
  6568. opline = zend_emit_op_tmp(result, ZEND_CAST, &expr_node, NULL);
  6569. opline->extended_value = ast->attr;
  6570. }
  6571. }
  6572. /* }}} */
  6573. static void zend_compile_shorthand_conditional(znode *result, zend_ast *ast) /* {{{ */
  6574. {
  6575. zend_ast *cond_ast = ast->child[0];
  6576. zend_ast *false_ast = ast->child[2];
  6577. znode cond_node, false_node;
  6578. zend_op *opline_qm_assign;
  6579. uint32_t opnum_jmp_set;
  6580. ZEND_ASSERT(ast->child[1] == NULL);
  6581. zend_compile_expr(&cond_node, cond_ast);
  6582. opnum_jmp_set = get_next_op_number();
  6583. zend_emit_op_tmp(result, ZEND_JMP_SET, &cond_node, NULL);
  6584. zend_compile_expr(&false_node, false_ast);
  6585. opline_qm_assign = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &false_node, NULL);
  6586. SET_NODE(opline_qm_assign->result, result);
  6587. zend_update_jump_target_to_next(opnum_jmp_set);
  6588. }
  6589. /* }}} */
  6590. void zend_compile_conditional(znode *result, zend_ast *ast) /* {{{ */
  6591. {
  6592. zend_ast *cond_ast = ast->child[0];
  6593. zend_ast *true_ast = ast->child[1];
  6594. zend_ast *false_ast = ast->child[2];
  6595. znode cond_node, true_node, false_node;
  6596. zend_op *opline_qm_assign2;
  6597. uint32_t opnum_jmpz, opnum_jmp;
  6598. if (cond_ast->kind == ZEND_AST_CONDITIONAL
  6599. && cond_ast->attr != ZEND_PARENTHESIZED_CONDITIONAL) {
  6600. if (cond_ast->child[1]) {
  6601. if (true_ast) {
  6602. zend_error(E_DEPRECATED,
  6603. "Unparenthesized `a ? b : c ? d : e` is deprecated. "
  6604. "Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)`");
  6605. } else {
  6606. zend_error(E_DEPRECATED,
  6607. "Unparenthesized `a ? b : c ?: d` is deprecated. "
  6608. "Use either `(a ? b : c) ?: d` or `a ? b : (c ?: d)`");
  6609. }
  6610. } else {
  6611. if (true_ast) {
  6612. zend_error(E_DEPRECATED,
  6613. "Unparenthesized `a ?: b ? c : d` is deprecated. "
  6614. "Use either `(a ?: b) ? c : d` or `a ?: (b ? c : d)`");
  6615. } else {
  6616. /* This case is harmless: (a ?: b) ?: c always produces the same result
  6617. * as a ?: (b ?: c). */
  6618. }
  6619. }
  6620. }
  6621. if (!true_ast) {
  6622. zend_compile_shorthand_conditional(result, ast);
  6623. return;
  6624. }
  6625. zend_compile_expr(&cond_node, cond_ast);
  6626. opnum_jmpz = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
  6627. zend_compile_expr(&true_node, true_ast);
  6628. zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &true_node, NULL);
  6629. opnum_jmp = zend_emit_jump(0);
  6630. zend_update_jump_target_to_next(opnum_jmpz);
  6631. zend_compile_expr(&false_node, false_ast);
  6632. opline_qm_assign2 = zend_emit_op(NULL, ZEND_QM_ASSIGN, &false_node, NULL);
  6633. SET_NODE(opline_qm_assign2->result, result);
  6634. zend_update_jump_target_to_next(opnum_jmp);
  6635. }
  6636. /* }}} */
  6637. void zend_compile_coalesce(znode *result, zend_ast *ast) /* {{{ */
  6638. {
  6639. zend_ast *expr_ast = ast->child[0];
  6640. zend_ast *default_ast = ast->child[1];
  6641. znode expr_node, default_node;
  6642. zend_op *opline;
  6643. uint32_t opnum;
  6644. zend_compile_var(&expr_node, expr_ast, BP_VAR_IS, 0);
  6645. opnum = get_next_op_number();
  6646. zend_emit_op_tmp(result, ZEND_COALESCE, &expr_node, NULL);
  6647. zend_compile_expr(&default_node, default_ast);
  6648. opline = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &default_node, NULL);
  6649. SET_NODE(opline->result, result);
  6650. opline = &CG(active_op_array)->opcodes[opnum];
  6651. opline->op2.opline_num = get_next_op_number();
  6652. }
  6653. /* }}} */
  6654. static void znode_dtor(zval *zv) {
  6655. znode *node = Z_PTR_P(zv);
  6656. if (node->op_type == IS_CONST) {
  6657. zval_ptr_dtor_nogc(&node->u.constant);
  6658. }
  6659. efree(node);
  6660. }
  6661. void zend_compile_assign_coalesce(znode *result, zend_ast *ast) /* {{{ */
  6662. {
  6663. zend_ast *var_ast = ast->child[0];
  6664. zend_ast *default_ast = ast->child[1];
  6665. znode var_node_is, var_node_w, default_node, assign_node, *node;
  6666. zend_op *opline;
  6667. uint32_t coalesce_opnum;
  6668. zend_bool need_frees = 0;
  6669. /* Remember expressions compiled during the initial BP_VAR_IS lookup,
  6670. * to avoid double-evaluation when we compile again with BP_VAR_W. */
  6671. HashTable *orig_memoized_exprs = CG(memoized_exprs);
  6672. int orig_memoize_mode = CG(memoize_mode);
  6673. zend_ensure_writable_variable(var_ast);
  6674. if (is_this_fetch(var_ast)) {
  6675. zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
  6676. }
  6677. ALLOC_HASHTABLE(CG(memoized_exprs));
  6678. zend_hash_init(CG(memoized_exprs), 0, NULL, znode_dtor, 0);
  6679. CG(memoize_mode) = ZEND_MEMOIZE_COMPILE;
  6680. zend_compile_var(&var_node_is, var_ast, BP_VAR_IS, 0);
  6681. coalesce_opnum = get_next_op_number();
  6682. zend_emit_op_tmp(result, ZEND_COALESCE, &var_node_is, NULL);
  6683. CG(memoize_mode) = ZEND_MEMOIZE_NONE;
  6684. zend_compile_expr(&default_node, default_ast);
  6685. CG(memoize_mode) = ZEND_MEMOIZE_FETCH;
  6686. zend_compile_var(&var_node_w, var_ast, BP_VAR_W, 0);
  6687. /* Reproduce some of the zend_compile_assign() opcode fixup logic here. */
  6688. opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
  6689. switch (var_ast->kind) {
  6690. case ZEND_AST_VAR:
  6691. zend_emit_op_tmp(&assign_node, ZEND_ASSIGN, &var_node_w, &default_node);
  6692. break;
  6693. case ZEND_AST_STATIC_PROP:
  6694. opline->opcode = ZEND_ASSIGN_STATIC_PROP;
  6695. opline->result_type = IS_TMP_VAR;
  6696. var_node_w.op_type = IS_TMP_VAR;
  6697. zend_emit_op_data(&default_node);
  6698. assign_node = var_node_w;
  6699. break;
  6700. case ZEND_AST_DIM:
  6701. opline->opcode = ZEND_ASSIGN_DIM;
  6702. opline->result_type = IS_TMP_VAR;
  6703. var_node_w.op_type = IS_TMP_VAR;
  6704. zend_emit_op_data(&default_node);
  6705. assign_node = var_node_w;
  6706. break;
  6707. case ZEND_AST_PROP:
  6708. opline->opcode = ZEND_ASSIGN_OBJ;
  6709. opline->result_type = IS_TMP_VAR;
  6710. var_node_w.op_type = IS_TMP_VAR;
  6711. zend_emit_op_data(&default_node);
  6712. assign_node = var_node_w;
  6713. break;
  6714. EMPTY_SWITCH_DEFAULT_CASE();
  6715. }
  6716. opline = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &assign_node, NULL);
  6717. SET_NODE(opline->result, result);
  6718. ZEND_HASH_FOREACH_PTR(CG(memoized_exprs), node) {
  6719. if (node->op_type == IS_TMP_VAR || node->op_type == IS_VAR) {
  6720. need_frees = 1;
  6721. break;
  6722. }
  6723. } ZEND_HASH_FOREACH_END();
  6724. /* Free DUPed expressions if there are any */
  6725. if (need_frees) {
  6726. uint32_t jump_opnum = zend_emit_jump(0);
  6727. zend_update_jump_target_to_next(coalesce_opnum);
  6728. ZEND_HASH_FOREACH_PTR(CG(memoized_exprs), node) {
  6729. if (node->op_type == IS_TMP_VAR || node->op_type == IS_VAR) {
  6730. zend_emit_op(NULL, ZEND_FREE, node, NULL);
  6731. }
  6732. } ZEND_HASH_FOREACH_END();
  6733. zend_update_jump_target_to_next(jump_opnum);
  6734. } else {
  6735. zend_update_jump_target_to_next(coalesce_opnum);
  6736. }
  6737. zend_hash_destroy(CG(memoized_exprs));
  6738. FREE_HASHTABLE(CG(memoized_exprs));
  6739. CG(memoized_exprs) = orig_memoized_exprs;
  6740. CG(memoize_mode) = orig_memoize_mode;
  6741. }
  6742. /* }}} */
  6743. void zend_compile_print(znode *result, zend_ast *ast) /* {{{ */
  6744. {
  6745. zend_op *opline;
  6746. zend_ast *expr_ast = ast->child[0];
  6747. znode expr_node;
  6748. zend_compile_expr(&expr_node, expr_ast);
  6749. opline = zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL);
  6750. opline->extended_value = 1;
  6751. result->op_type = IS_CONST;
  6752. ZVAL_LONG(&result->u.constant, 1);
  6753. }
  6754. /* }}} */
  6755. void zend_compile_exit(znode *result, zend_ast *ast) /* {{{ */
  6756. {
  6757. zend_ast *expr_ast = ast->child[0];
  6758. if (expr_ast) {
  6759. znode expr_node;
  6760. zend_compile_expr(&expr_node, expr_ast);
  6761. zend_emit_op(NULL, ZEND_EXIT, &expr_node, NULL);
  6762. } else {
  6763. zend_emit_op(NULL, ZEND_EXIT, NULL, NULL);
  6764. }
  6765. result->op_type = IS_CONST;
  6766. ZVAL_BOOL(&result->u.constant, 1);
  6767. }
  6768. /* }}} */
  6769. void zend_compile_yield(znode *result, zend_ast *ast) /* {{{ */
  6770. {
  6771. zend_ast *value_ast = ast->child[0];
  6772. zend_ast *key_ast = ast->child[1];
  6773. znode value_node, key_node;
  6774. znode *value_node_ptr = NULL, *key_node_ptr = NULL;
  6775. zend_op *opline;
  6776. zend_bool returns_by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
  6777. zend_mark_function_as_generator();
  6778. if (key_ast) {
  6779. zend_compile_expr(&key_node, key_ast);
  6780. key_node_ptr = &key_node;
  6781. }
  6782. if (value_ast) {
  6783. if (returns_by_ref && zend_is_variable(value_ast)) {
  6784. zend_compile_var(&value_node, value_ast, BP_VAR_W, 1);
  6785. } else {
  6786. zend_compile_expr(&value_node, value_ast);
  6787. }
  6788. value_node_ptr = &value_node;
  6789. }
  6790. opline = zend_emit_op(result, ZEND_YIELD, value_node_ptr, key_node_ptr);
  6791. if (value_ast && returns_by_ref && zend_is_call(value_ast)) {
  6792. opline->extended_value = ZEND_RETURNS_FUNCTION;
  6793. }
  6794. }
  6795. /* }}} */
  6796. void zend_compile_yield_from(znode *result, zend_ast *ast) /* {{{ */
  6797. {
  6798. zend_ast *expr_ast = ast->child[0];
  6799. znode expr_node;
  6800. zend_mark_function_as_generator();
  6801. if (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) {
  6802. zend_error_noreturn(E_COMPILE_ERROR,
  6803. "Cannot use \"yield from\" inside a by-reference generator");
  6804. }
  6805. zend_compile_expr(&expr_node, expr_ast);
  6806. zend_emit_op_tmp(result, ZEND_YIELD_FROM, &expr_node, NULL);
  6807. }
  6808. /* }}} */
  6809. void zend_compile_instanceof(znode *result, zend_ast *ast) /* {{{ */
  6810. {
  6811. zend_ast *obj_ast = ast->child[0];
  6812. zend_ast *class_ast = ast->child[1];
  6813. znode obj_node, class_node;
  6814. zend_op *opline;
  6815. zend_compile_expr(&obj_node, obj_ast);
  6816. if (obj_node.op_type == IS_CONST) {
  6817. zend_do_free(&obj_node);
  6818. result->op_type = IS_CONST;
  6819. ZVAL_FALSE(&result->u.constant);
  6820. return;
  6821. }
  6822. zend_compile_class_ref(&class_node, class_ast,
  6823. ZEND_FETCH_CLASS_NO_AUTOLOAD | ZEND_FETCH_CLASS_EXCEPTION);
  6824. opline = zend_emit_op_tmp(result, ZEND_INSTANCEOF, &obj_node, NULL);
  6825. if (class_node.op_type == IS_CONST) {
  6826. opline->op2_type = IS_CONST;
  6827. opline->op2.constant = zend_add_class_name_literal(
  6828. Z_STR(class_node.u.constant));
  6829. opline->extended_value = zend_alloc_cache_slot();
  6830. } else {
  6831. SET_NODE(opline->op2, &class_node);
  6832. }
  6833. }
  6834. /* }}} */
  6835. void zend_compile_include_or_eval(znode *result, zend_ast *ast) /* {{{ */
  6836. {
  6837. zend_ast *expr_ast = ast->child[0];
  6838. znode expr_node;
  6839. zend_op *opline;
  6840. zend_do_extended_fcall_begin();
  6841. zend_compile_expr(&expr_node, expr_ast);
  6842. opline = zend_emit_op(result, ZEND_INCLUDE_OR_EVAL, &expr_node, NULL);
  6843. opline->extended_value = ast->attr;
  6844. zend_do_extended_fcall_end();
  6845. }
  6846. /* }}} */
  6847. void zend_compile_isset_or_empty(znode *result, zend_ast *ast) /* {{{ */
  6848. {
  6849. zend_ast *var_ast = ast->child[0];
  6850. znode var_node;
  6851. zend_op *opline = NULL;
  6852. ZEND_ASSERT(ast->kind == ZEND_AST_ISSET || ast->kind == ZEND_AST_EMPTY);
  6853. if (!zend_is_variable(var_ast)) {
  6854. if (ast->kind == ZEND_AST_EMPTY) {
  6855. /* empty(expr) can be transformed to !expr */
  6856. zend_ast *not_ast = zend_ast_create_ex(ZEND_AST_UNARY_OP, ZEND_BOOL_NOT, var_ast);
  6857. zend_compile_expr(result, not_ast);
  6858. return;
  6859. } else {
  6860. zend_error_noreturn(E_COMPILE_ERROR,
  6861. "Cannot use isset() on the result of an expression "
  6862. "(you can use \"null !== expression\" instead)");
  6863. }
  6864. }
  6865. switch (var_ast->kind) {
  6866. case ZEND_AST_VAR:
  6867. if (is_this_fetch(var_ast)) {
  6868. opline = zend_emit_op(result, ZEND_ISSET_ISEMPTY_THIS, NULL, NULL);
  6869. CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
  6870. } else if (zend_try_compile_cv(&var_node, var_ast) == SUCCESS) {
  6871. opline = zend_emit_op(result, ZEND_ISSET_ISEMPTY_CV, &var_node, NULL);
  6872. } else {
  6873. opline = zend_compile_simple_var_no_cv(result, var_ast, BP_VAR_IS, 0);
  6874. opline->opcode = ZEND_ISSET_ISEMPTY_VAR;
  6875. }
  6876. break;
  6877. case ZEND_AST_DIM:
  6878. opline = zend_compile_dim(result, var_ast, BP_VAR_IS);
  6879. opline->opcode = ZEND_ISSET_ISEMPTY_DIM_OBJ;
  6880. break;
  6881. case ZEND_AST_PROP:
  6882. opline = zend_compile_prop(result, var_ast, BP_VAR_IS, 0);
  6883. opline->opcode = ZEND_ISSET_ISEMPTY_PROP_OBJ;
  6884. break;
  6885. case ZEND_AST_STATIC_PROP:
  6886. opline = zend_compile_static_prop(result, var_ast, BP_VAR_IS, 0, 0);
  6887. opline->opcode = ZEND_ISSET_ISEMPTY_STATIC_PROP;
  6888. break;
  6889. EMPTY_SWITCH_DEFAULT_CASE()
  6890. }
  6891. result->op_type = opline->result_type = IS_TMP_VAR;
  6892. if (!(ast->kind == ZEND_AST_ISSET)) {
  6893. opline->extended_value |= ZEND_ISEMPTY;
  6894. }
  6895. }
  6896. /* }}} */
  6897. void zend_compile_silence(znode *result, zend_ast *ast) /* {{{ */
  6898. {
  6899. zend_ast *expr_ast = ast->child[0];
  6900. znode silence_node;
  6901. zend_emit_op_tmp(&silence_node, ZEND_BEGIN_SILENCE, NULL, NULL);
  6902. if (expr_ast->kind == ZEND_AST_VAR) {
  6903. /* For @$var we need to force a FETCH instruction, otherwise the CV access will
  6904. * happen outside the silenced section. */
  6905. zend_compile_simple_var_no_cv(result, expr_ast, BP_VAR_R, 0 );
  6906. } else {
  6907. zend_compile_expr(result, expr_ast);
  6908. }
  6909. zend_emit_op(NULL, ZEND_END_SILENCE, &silence_node, NULL);
  6910. }
  6911. /* }}} */
  6912. void zend_compile_shell_exec(znode *result, zend_ast *ast) /* {{{ */
  6913. {
  6914. zend_ast *expr_ast = ast->child[0];
  6915. zval fn_name;
  6916. zend_ast *name_ast, *args_ast, *call_ast;
  6917. ZVAL_STRING(&fn_name, "shell_exec");
  6918. name_ast = zend_ast_create_zval(&fn_name);
  6919. args_ast = zend_ast_create_list(1, ZEND_AST_ARG_LIST, expr_ast);
  6920. call_ast = zend_ast_create(ZEND_AST_CALL, name_ast, args_ast);
  6921. zend_compile_expr(result, call_ast);
  6922. zval_ptr_dtor(&fn_name);
  6923. }
  6924. /* }}} */
  6925. void zend_compile_array(znode *result, zend_ast *ast) /* {{{ */
  6926. {
  6927. zend_ast_list *list = zend_ast_get_list(ast);
  6928. zend_op *opline;
  6929. uint32_t i, opnum_init = -1;
  6930. zend_bool packed = 1;
  6931. if (zend_try_ct_eval_array(&result->u.constant, ast)) {
  6932. result->op_type = IS_CONST;
  6933. return;
  6934. }
  6935. /* Empty arrays are handled at compile-time */
  6936. ZEND_ASSERT(list->children > 0);
  6937. for (i = 0; i < list->children; ++i) {
  6938. zend_ast *elem_ast = list->child[i];
  6939. zend_ast *value_ast, *key_ast;
  6940. zend_bool by_ref;
  6941. znode value_node, key_node, *key_node_ptr = NULL;
  6942. if (elem_ast == NULL) {
  6943. zend_error(E_COMPILE_ERROR, "Cannot use empty array elements in arrays");
  6944. }
  6945. value_ast = elem_ast->child[0];
  6946. if (elem_ast->kind == ZEND_AST_UNPACK) {
  6947. zend_compile_expr(&value_node, value_ast);
  6948. if (i == 0) {
  6949. opnum_init = get_next_op_number();
  6950. opline = zend_emit_op_tmp(result, ZEND_INIT_ARRAY, NULL, NULL);
  6951. }
  6952. opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_UNPACK, &value_node, NULL);
  6953. SET_NODE(opline->result, result);
  6954. continue;
  6955. }
  6956. key_ast = elem_ast->child[1];
  6957. by_ref = elem_ast->attr;
  6958. if (key_ast) {
  6959. zend_compile_expr(&key_node, key_ast);
  6960. zend_handle_numeric_op(&key_node);
  6961. key_node_ptr = &key_node;
  6962. }
  6963. if (by_ref) {
  6964. zend_ensure_writable_variable(value_ast);
  6965. zend_compile_var(&value_node, value_ast, BP_VAR_W, 1);
  6966. } else {
  6967. zend_compile_expr(&value_node, value_ast);
  6968. }
  6969. if (i == 0) {
  6970. opnum_init = get_next_op_number();
  6971. opline = zend_emit_op_tmp(result, ZEND_INIT_ARRAY, &value_node, key_node_ptr);
  6972. opline->extended_value = list->children << ZEND_ARRAY_SIZE_SHIFT;
  6973. } else {
  6974. opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_ELEMENT,
  6975. &value_node, key_node_ptr);
  6976. SET_NODE(opline->result, result);
  6977. }
  6978. opline->extended_value |= by_ref;
  6979. if (key_ast && key_node.op_type == IS_CONST && Z_TYPE(key_node.u.constant) == IS_STRING) {
  6980. packed = 0;
  6981. }
  6982. }
  6983. /* Add a flag to INIT_ARRAY if we know this array cannot be packed */
  6984. if (!packed) {
  6985. ZEND_ASSERT(opnum_init != (uint32_t)-1);
  6986. opline = &CG(active_op_array)->opcodes[opnum_init];
  6987. opline->extended_value |= ZEND_ARRAY_NOT_PACKED;
  6988. }
  6989. }
  6990. /* }}} */
  6991. void zend_compile_const(znode *result, zend_ast *ast) /* {{{ */
  6992. {
  6993. zend_ast *name_ast = ast->child[0];
  6994. zend_op *opline;
  6995. zend_bool is_fully_qualified;
  6996. zend_string *orig_name = zend_ast_get_str(name_ast);
  6997. zend_string *resolved_name = zend_resolve_const_name(orig_name, name_ast->attr, &is_fully_qualified);
  6998. if (zend_string_equals_literal(resolved_name, "__COMPILER_HALT_OFFSET__") || (name_ast->attr != ZEND_NAME_RELATIVE && zend_string_equals_literal(orig_name, "__COMPILER_HALT_OFFSET__"))) {
  6999. zend_ast *last = CG(ast);
  7000. while (last && last->kind == ZEND_AST_STMT_LIST) {
  7001. zend_ast_list *list = zend_ast_get_list(last);
  7002. if (list->children == 0) {
  7003. break;
  7004. }
  7005. last = list->child[list->children-1];
  7006. }
  7007. if (last && last->kind == ZEND_AST_HALT_COMPILER) {
  7008. result->op_type = IS_CONST;
  7009. ZVAL_LONG(&result->u.constant, Z_LVAL_P(zend_ast_get_zval(last->child[0])));
  7010. zend_string_release_ex(resolved_name, 0);
  7011. return;
  7012. }
  7013. }
  7014. if (zend_try_ct_eval_const(&result->u.constant, resolved_name, is_fully_qualified)) {
  7015. result->op_type = IS_CONST;
  7016. zend_string_release_ex(resolved_name, 0);
  7017. return;
  7018. }
  7019. opline = zend_emit_op_tmp(result, ZEND_FETCH_CONSTANT, NULL, NULL);
  7020. opline->op2_type = IS_CONST;
  7021. if (is_fully_qualified || !FC(current_namespace)) {
  7022. opline->op2.constant = zend_add_const_name_literal(
  7023. resolved_name, 0);
  7024. } else {
  7025. opline->op1.num = IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE;
  7026. opline->op2.constant = zend_add_const_name_literal(
  7027. resolved_name, 1);
  7028. }
  7029. opline->extended_value = zend_alloc_cache_slot();
  7030. }
  7031. /* }}} */
  7032. void zend_compile_class_const(znode *result, zend_ast *ast) /* {{{ */
  7033. {
  7034. zend_ast *class_ast = ast->child[0];
  7035. zend_ast *const_ast = ast->child[1];
  7036. znode class_node, const_node;
  7037. zend_op *opline;
  7038. zend_eval_const_expr(&ast->child[0]);
  7039. zend_eval_const_expr(&ast->child[1]);
  7040. class_ast = ast->child[0];
  7041. const_ast = ast->child[1];
  7042. if (class_ast->kind == ZEND_AST_ZVAL) {
  7043. zend_string *resolved_name;
  7044. resolved_name = zend_resolve_class_name_ast(class_ast);
  7045. if (const_ast->kind == ZEND_AST_ZVAL && zend_try_ct_eval_class_const(&result->u.constant, resolved_name, zend_ast_get_str(const_ast))) {
  7046. result->op_type = IS_CONST;
  7047. zend_string_release_ex(resolved_name, 0);
  7048. return;
  7049. }
  7050. zend_string_release_ex(resolved_name, 0);
  7051. }
  7052. zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
  7053. zend_compile_expr(&const_node, const_ast);
  7054. opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_CONSTANT, NULL, &const_node);
  7055. zend_set_class_name_op1(opline, &class_node);
  7056. opline->extended_value = zend_alloc_cache_slots(2);
  7057. }
  7058. /* }}} */
  7059. void zend_compile_class_name(znode *result, zend_ast *ast) /* {{{ */
  7060. {
  7061. zend_ast *class_ast = ast->child[0];
  7062. if (zend_try_compile_const_expr_resolve_class_name(&result->u.constant, class_ast)) {
  7063. result->op_type = IS_CONST;
  7064. return;
  7065. }
  7066. if (class_ast->kind == ZEND_AST_ZVAL) {
  7067. zend_op *opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL);
  7068. opline->op1.num = zend_get_class_fetch_type(zend_ast_get_str(class_ast));
  7069. } else {
  7070. znode expr_node;
  7071. zend_compile_expr(&expr_node, class_ast);
  7072. if (expr_node.op_type == IS_CONST) {
  7073. /* Unlikely case that happen if class_ast is constant folded.
  7074. * Handle it here, to avoid needing a CONST specialization in the VM. */
  7075. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use ::class on value of type %s",
  7076. zend_get_type_by_const(Z_TYPE(expr_node.u.constant)));
  7077. }
  7078. zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, &expr_node, NULL);
  7079. }
  7080. }
  7081. /* }}} */
  7082. static zend_op *zend_compile_rope_add_ex(zend_op *opline, znode *result, uint32_t num, znode *elem_node) /* {{{ */
  7083. {
  7084. if (num == 0) {
  7085. result->op_type = IS_TMP_VAR;
  7086. result->u.op.var = -1;
  7087. opline->opcode = ZEND_ROPE_INIT;
  7088. } else {
  7089. opline->opcode = ZEND_ROPE_ADD;
  7090. SET_NODE(opline->op1, result);
  7091. }
  7092. SET_NODE(opline->op2, elem_node);
  7093. SET_NODE(opline->result, result);
  7094. opline->extended_value = num;
  7095. return opline;
  7096. }
  7097. /* }}} */
  7098. static zend_op *zend_compile_rope_add(znode *result, uint32_t num, znode *elem_node) /* {{{ */
  7099. {
  7100. zend_op *opline = get_next_op();
  7101. if (num == 0) {
  7102. result->op_type = IS_TMP_VAR;
  7103. result->u.op.var = -1;
  7104. opline->opcode = ZEND_ROPE_INIT;
  7105. } else {
  7106. opline->opcode = ZEND_ROPE_ADD;
  7107. SET_NODE(opline->op1, result);
  7108. }
  7109. SET_NODE(opline->op2, elem_node);
  7110. SET_NODE(opline->result, result);
  7111. opline->extended_value = num;
  7112. return opline;
  7113. }
  7114. /* }}} */
  7115. static void zend_compile_encaps_list(znode *result, zend_ast *ast) /* {{{ */
  7116. {
  7117. uint32_t i, j;
  7118. uint32_t rope_init_lineno = -1;
  7119. zend_op *opline = NULL, *init_opline;
  7120. znode elem_node, last_const_node;
  7121. zend_ast_list *list = zend_ast_get_list(ast);
  7122. uint32_t reserved_op_number = -1;
  7123. ZEND_ASSERT(list->children > 0);
  7124. j = 0;
  7125. last_const_node.op_type = IS_UNUSED;
  7126. for (i = 0; i < list->children; i++) {
  7127. zend_compile_expr(&elem_node, list->child[i]);
  7128. if (elem_node.op_type == IS_CONST) {
  7129. convert_to_string(&elem_node.u.constant);
  7130. if (Z_STRLEN(elem_node.u.constant) == 0) {
  7131. zval_ptr_dtor(&elem_node.u.constant);
  7132. } else if (last_const_node.op_type == IS_CONST) {
  7133. concat_function(&last_const_node.u.constant, &last_const_node.u.constant, &elem_node.u.constant);
  7134. zval_ptr_dtor(&elem_node.u.constant);
  7135. } else {
  7136. last_const_node.op_type = IS_CONST;
  7137. ZVAL_COPY_VALUE(&last_const_node.u.constant, &elem_node.u.constant);
  7138. /* Reserve place for ZEND_ROPE_ADD instruction */
  7139. reserved_op_number = get_next_op_number();
  7140. opline = get_next_op();
  7141. opline->opcode = ZEND_NOP;
  7142. }
  7143. continue;
  7144. } else {
  7145. if (j == 0) {
  7146. if (last_const_node.op_type == IS_CONST) {
  7147. rope_init_lineno = reserved_op_number;
  7148. } else {
  7149. rope_init_lineno = get_next_op_number();
  7150. }
  7151. }
  7152. if (last_const_node.op_type == IS_CONST) {
  7153. opline = &CG(active_op_array)->opcodes[reserved_op_number];
  7154. zend_compile_rope_add_ex(opline, result, j++, &last_const_node);
  7155. last_const_node.op_type = IS_UNUSED;
  7156. }
  7157. opline = zend_compile_rope_add(result, j++, &elem_node);
  7158. }
  7159. }
  7160. if (j == 0) {
  7161. result->op_type = IS_CONST;
  7162. if (last_const_node.op_type == IS_CONST) {
  7163. ZVAL_COPY_VALUE(&result->u.constant, &last_const_node.u.constant);
  7164. } else {
  7165. ZVAL_EMPTY_STRING(&result->u.constant);
  7166. /* empty string */
  7167. }
  7168. CG(active_op_array)->last = reserved_op_number - 1;
  7169. return;
  7170. } else if (last_const_node.op_type == IS_CONST) {
  7171. opline = &CG(active_op_array)->opcodes[reserved_op_number];
  7172. opline = zend_compile_rope_add_ex(opline, result, j++, &last_const_node);
  7173. }
  7174. init_opline = CG(active_op_array)->opcodes + rope_init_lineno;
  7175. if (j == 1) {
  7176. if (opline->op2_type == IS_CONST) {
  7177. GET_NODE(result, opline->op2);
  7178. MAKE_NOP(opline);
  7179. } else {
  7180. opline->opcode = ZEND_CAST;
  7181. opline->extended_value = IS_STRING;
  7182. opline->op1_type = opline->op2_type;
  7183. opline->op1 = opline->op2;
  7184. SET_UNUSED(opline->op2);
  7185. zend_make_tmp_result(result, opline);
  7186. }
  7187. } else if (j == 2) {
  7188. opline->opcode = ZEND_FAST_CONCAT;
  7189. opline->extended_value = 0;
  7190. opline->op1_type = init_opline->op2_type;
  7191. opline->op1 = init_opline->op2;
  7192. zend_make_tmp_result(result, opline);
  7193. MAKE_NOP(init_opline);
  7194. } else {
  7195. uint32_t var;
  7196. init_opline->extended_value = j;
  7197. opline->opcode = ZEND_ROPE_END;
  7198. zend_make_tmp_result(result, opline);
  7199. var = opline->op1.var = get_temporary_variable();
  7200. /* Allocates the necessary number of zval slots to keep the rope */
  7201. i = ((j * sizeof(zend_string*)) + (sizeof(zval) - 1)) / sizeof(zval);
  7202. while (i > 1) {
  7203. get_temporary_variable();
  7204. i--;
  7205. }
  7206. /* Update all the previous opcodes to use the same variable */
  7207. while (opline != init_opline) {
  7208. opline--;
  7209. if (opline->opcode == ZEND_ROPE_ADD &&
  7210. opline->result.var == (uint32_t)-1) {
  7211. opline->op1.var = var;
  7212. opline->result.var = var;
  7213. } else if (opline->opcode == ZEND_ROPE_INIT &&
  7214. opline->result.var == (uint32_t)-1) {
  7215. opline->result.var = var;
  7216. }
  7217. }
  7218. }
  7219. }
  7220. /* }}} */
  7221. void zend_compile_magic_const(znode *result, zend_ast *ast) /* {{{ */
  7222. {
  7223. zend_op *opline;
  7224. if (zend_try_ct_eval_magic_const(&result->u.constant, ast)) {
  7225. result->op_type = IS_CONST;
  7226. return;
  7227. }
  7228. ZEND_ASSERT(ast->attr == T_CLASS_C &&
  7229. CG(active_class_entry) &&
  7230. (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) != 0);
  7231. opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL);
  7232. opline->op1.num = ZEND_FETCH_CLASS_SELF;
  7233. }
  7234. /* }}} */
  7235. zend_bool zend_is_allowed_in_const_expr(zend_ast_kind kind) /* {{{ */
  7236. {
  7237. return kind == ZEND_AST_ZVAL || kind == ZEND_AST_BINARY_OP
  7238. || kind == ZEND_AST_GREATER || kind == ZEND_AST_GREATER_EQUAL
  7239. || kind == ZEND_AST_AND || kind == ZEND_AST_OR
  7240. || kind == ZEND_AST_UNARY_OP
  7241. || kind == ZEND_AST_UNARY_PLUS || kind == ZEND_AST_UNARY_MINUS
  7242. || kind == ZEND_AST_CONDITIONAL || kind == ZEND_AST_DIM
  7243. || kind == ZEND_AST_ARRAY || kind == ZEND_AST_ARRAY_ELEM
  7244. || kind == ZEND_AST_UNPACK
  7245. || kind == ZEND_AST_CONST || kind == ZEND_AST_CLASS_CONST
  7246. || kind == ZEND_AST_CLASS_NAME
  7247. || kind == ZEND_AST_MAGIC_CONST || kind == ZEND_AST_COALESCE;
  7248. }
  7249. /* }}} */
  7250. void zend_compile_const_expr_class_const(zend_ast **ast_ptr) /* {{{ */
  7251. {
  7252. zend_ast *ast = *ast_ptr;
  7253. zend_ast *class_ast = ast->child[0];
  7254. zend_ast *const_ast = ast->child[1];
  7255. zend_string *class_name;
  7256. zend_string *const_name = zend_ast_get_str(const_ast);
  7257. zend_string *name;
  7258. int fetch_type;
  7259. if (class_ast->kind != ZEND_AST_ZVAL) {
  7260. zend_error_noreturn(E_COMPILE_ERROR,
  7261. "Dynamic class names are not allowed in compile-time class constant references");
  7262. }
  7263. class_name = zend_ast_get_str(class_ast);
  7264. fetch_type = zend_get_class_fetch_type(class_name);
  7265. if (ZEND_FETCH_CLASS_STATIC == fetch_type) {
  7266. zend_error_noreturn(E_COMPILE_ERROR,
  7267. "\"static::\" is not allowed in compile-time constants");
  7268. }
  7269. if (ZEND_FETCH_CLASS_DEFAULT == fetch_type) {
  7270. class_name = zend_resolve_class_name_ast(class_ast);
  7271. } else {
  7272. zend_string_addref(class_name);
  7273. }
  7274. name = zend_create_member_string(class_name, const_name);
  7275. zend_ast_destroy(ast);
  7276. zend_string_release_ex(class_name, 0);
  7277. *ast_ptr = zend_ast_create_constant(name, fetch_type | ZEND_FETCH_CLASS_EXCEPTION);
  7278. }
  7279. /* }}} */
  7280. void zend_compile_const_expr_class_name(zend_ast **ast_ptr) /* {{{ */
  7281. {
  7282. zend_ast *ast = *ast_ptr;
  7283. zend_ast *class_ast = ast->child[0];
  7284. if (class_ast->kind != ZEND_AST_ZVAL) {
  7285. zend_error_noreturn(E_COMPILE_ERROR,
  7286. "(expression)::class cannot be used in constant expressions");
  7287. }
  7288. zend_string *class_name = zend_ast_get_str(class_ast);
  7289. uint32_t fetch_type = zend_get_class_fetch_type(class_name);
  7290. switch (fetch_type) {
  7291. case ZEND_FETCH_CLASS_SELF:
  7292. case ZEND_FETCH_CLASS_PARENT:
  7293. /* For the const-eval representation store the fetch type instead of the name. */
  7294. zend_string_release(class_name);
  7295. ast->child[0] = NULL;
  7296. ast->attr = fetch_type;
  7297. return;
  7298. case ZEND_FETCH_CLASS_STATIC:
  7299. zend_error_noreturn(E_COMPILE_ERROR,
  7300. "static::class cannot be used for compile-time class name resolution");
  7301. return;
  7302. EMPTY_SWITCH_DEFAULT_CASE()
  7303. }
  7304. }
  7305. void zend_compile_const_expr_const(zend_ast **ast_ptr) /* {{{ */
  7306. {
  7307. zend_ast *ast = *ast_ptr;
  7308. zend_ast *name_ast = ast->child[0];
  7309. zend_string *orig_name = zend_ast_get_str(name_ast);
  7310. zend_bool is_fully_qualified;
  7311. zval result;
  7312. zend_string *resolved_name;
  7313. resolved_name = zend_resolve_const_name(
  7314. orig_name, name_ast->attr, &is_fully_qualified);
  7315. if (zend_try_ct_eval_const(&result, resolved_name, is_fully_qualified)) {
  7316. zend_string_release_ex(resolved_name, 0);
  7317. zend_ast_destroy(ast);
  7318. *ast_ptr = zend_ast_create_zval(&result);
  7319. return;
  7320. }
  7321. zend_ast_destroy(ast);
  7322. *ast_ptr = zend_ast_create_constant(resolved_name,
  7323. !is_fully_qualified && FC(current_namespace) ? IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE : 0);
  7324. }
  7325. /* }}} */
  7326. void zend_compile_const_expr_magic_const(zend_ast **ast_ptr) /* {{{ */
  7327. {
  7328. zend_ast *ast = *ast_ptr;
  7329. /* Other cases already resolved by constant folding */
  7330. ZEND_ASSERT(ast->attr == T_CLASS_C);
  7331. zend_ast_destroy(ast);
  7332. *ast_ptr = zend_ast_create(ZEND_AST_CONSTANT_CLASS);
  7333. }
  7334. /* }}} */
  7335. void zend_compile_const_expr(zend_ast **ast_ptr) /* {{{ */
  7336. {
  7337. zend_ast *ast = *ast_ptr;
  7338. if (ast == NULL || ast->kind == ZEND_AST_ZVAL) {
  7339. return;
  7340. }
  7341. if (!zend_is_allowed_in_const_expr(ast->kind)) {
  7342. zend_error_noreturn(E_COMPILE_ERROR, "Constant expression contains invalid operations");
  7343. }
  7344. switch (ast->kind) {
  7345. case ZEND_AST_CLASS_CONST:
  7346. zend_compile_const_expr_class_const(ast_ptr);
  7347. break;
  7348. case ZEND_AST_CLASS_NAME:
  7349. zend_compile_const_expr_class_name(ast_ptr);
  7350. break;
  7351. case ZEND_AST_CONST:
  7352. zend_compile_const_expr_const(ast_ptr);
  7353. break;
  7354. case ZEND_AST_MAGIC_CONST:
  7355. zend_compile_const_expr_magic_const(ast_ptr);
  7356. break;
  7357. default:
  7358. zend_ast_apply(ast, zend_compile_const_expr);
  7359. break;
  7360. }
  7361. }
  7362. /* }}} */
  7363. void zend_const_expr_to_zval(zval *result, zend_ast *ast) /* {{{ */
  7364. {
  7365. zend_ast *orig_ast = ast;
  7366. zend_eval_const_expr(&ast);
  7367. zend_compile_const_expr(&ast);
  7368. if (ast->kind == ZEND_AST_ZVAL) {
  7369. ZVAL_COPY_VALUE(result, zend_ast_get_zval(ast));
  7370. } else {
  7371. ZVAL_AST(result, zend_ast_copy(ast));
  7372. /* destroy the ast here, it might have been replaced */
  7373. zend_ast_destroy(ast);
  7374. }
  7375. /* Kill this branch of the original AST, as it was already destroyed.
  7376. * It would be nice to find a better solution to this problem in the
  7377. * future. */
  7378. orig_ast->kind = 0;
  7379. }
  7380. /* }}} */
  7381. /* Same as compile_stmt, but with early binding */
  7382. void zend_compile_top_stmt(zend_ast *ast) /* {{{ */
  7383. {
  7384. if (!ast) {
  7385. return;
  7386. }
  7387. if (ast->kind == ZEND_AST_STMT_LIST) {
  7388. zend_ast_list *list = zend_ast_get_list(ast);
  7389. uint32_t i;
  7390. for (i = 0; i < list->children; ++i) {
  7391. zend_compile_top_stmt(list->child[i]);
  7392. }
  7393. return;
  7394. }
  7395. if (ast->kind == ZEND_AST_FUNC_DECL) {
  7396. CG(zend_lineno) = ast->lineno;
  7397. zend_compile_func_decl(NULL, ast, 1);
  7398. CG(zend_lineno) = ((zend_ast_decl *) ast)->end_lineno;
  7399. } else if (ast->kind == ZEND_AST_CLASS) {
  7400. CG(zend_lineno) = ast->lineno;
  7401. zend_compile_class_decl(NULL, ast, 1);
  7402. CG(zend_lineno) = ((zend_ast_decl *) ast)->end_lineno;
  7403. } else {
  7404. zend_compile_stmt(ast);
  7405. }
  7406. if (ast->kind != ZEND_AST_NAMESPACE && ast->kind != ZEND_AST_HALT_COMPILER) {
  7407. zend_verify_namespace();
  7408. }
  7409. }
  7410. /* }}} */
  7411. void zend_compile_stmt(zend_ast *ast) /* {{{ */
  7412. {
  7413. if (!ast) {
  7414. return;
  7415. }
  7416. CG(zend_lineno) = ast->lineno;
  7417. if ((CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT) && !zend_is_unticked_stmt(ast)) {
  7418. zend_do_extended_stmt();
  7419. }
  7420. switch (ast->kind) {
  7421. case ZEND_AST_STMT_LIST:
  7422. zend_compile_stmt_list(ast);
  7423. break;
  7424. case ZEND_AST_GLOBAL:
  7425. zend_compile_global_var(ast);
  7426. break;
  7427. case ZEND_AST_STATIC:
  7428. zend_compile_static_var(ast);
  7429. break;
  7430. case ZEND_AST_UNSET:
  7431. zend_compile_unset(ast);
  7432. break;
  7433. case ZEND_AST_RETURN:
  7434. zend_compile_return(ast);
  7435. break;
  7436. case ZEND_AST_ECHO:
  7437. zend_compile_echo(ast);
  7438. break;
  7439. case ZEND_AST_BREAK:
  7440. case ZEND_AST_CONTINUE:
  7441. zend_compile_break_continue(ast);
  7442. break;
  7443. case ZEND_AST_GOTO:
  7444. zend_compile_goto(ast);
  7445. break;
  7446. case ZEND_AST_LABEL:
  7447. zend_compile_label(ast);
  7448. break;
  7449. case ZEND_AST_WHILE:
  7450. zend_compile_while(ast);
  7451. break;
  7452. case ZEND_AST_DO_WHILE:
  7453. zend_compile_do_while(ast);
  7454. break;
  7455. case ZEND_AST_FOR:
  7456. zend_compile_for(ast);
  7457. break;
  7458. case ZEND_AST_FOREACH:
  7459. zend_compile_foreach(ast);
  7460. break;
  7461. case ZEND_AST_IF:
  7462. zend_compile_if(ast);
  7463. break;
  7464. case ZEND_AST_SWITCH:
  7465. zend_compile_switch(ast);
  7466. break;
  7467. case ZEND_AST_TRY:
  7468. zend_compile_try(ast);
  7469. break;
  7470. case ZEND_AST_DECLARE:
  7471. zend_compile_declare(ast);
  7472. break;
  7473. case ZEND_AST_FUNC_DECL:
  7474. case ZEND_AST_METHOD:
  7475. zend_compile_func_decl(NULL, ast, 0);
  7476. break;
  7477. case ZEND_AST_PROP_GROUP:
  7478. zend_compile_prop_group(ast);
  7479. break;
  7480. case ZEND_AST_CLASS_CONST_DECL:
  7481. zend_compile_class_const_decl(ast);
  7482. break;
  7483. case ZEND_AST_USE_TRAIT:
  7484. zend_compile_use_trait(ast);
  7485. break;
  7486. case ZEND_AST_CLASS:
  7487. zend_compile_class_decl(NULL, ast, 0);
  7488. break;
  7489. case ZEND_AST_GROUP_USE:
  7490. zend_compile_group_use(ast);
  7491. break;
  7492. case ZEND_AST_USE:
  7493. zend_compile_use(ast);
  7494. break;
  7495. case ZEND_AST_CONST_DECL:
  7496. zend_compile_const_decl(ast);
  7497. break;
  7498. case ZEND_AST_NAMESPACE:
  7499. zend_compile_namespace(ast);
  7500. break;
  7501. case ZEND_AST_HALT_COMPILER:
  7502. zend_compile_halt_compiler(ast);
  7503. break;
  7504. case ZEND_AST_THROW:
  7505. zend_compile_throw(NULL, ast);
  7506. break;
  7507. default:
  7508. {
  7509. znode result;
  7510. zend_compile_expr(&result, ast);
  7511. zend_do_free(&result);
  7512. }
  7513. }
  7514. if (FC(declarables).ticks && !zend_is_unticked_stmt(ast)) {
  7515. zend_emit_tick();
  7516. }
  7517. }
  7518. /* }}} */
  7519. void zend_compile_expr(znode *result, zend_ast *ast) /* {{{ */
  7520. {
  7521. /* CG(zend_lineno) = ast->lineno; */
  7522. CG(zend_lineno) = zend_ast_get_lineno(ast);
  7523. if (CG(memoize_mode) != ZEND_MEMOIZE_NONE) {
  7524. zend_compile_memoized_expr(result, ast);
  7525. return;
  7526. }
  7527. switch (ast->kind) {
  7528. case ZEND_AST_ZVAL:
  7529. ZVAL_COPY(&result->u.constant, zend_ast_get_zval(ast));
  7530. result->op_type = IS_CONST;
  7531. return;
  7532. case ZEND_AST_ZNODE:
  7533. *result = *zend_ast_get_znode(ast);
  7534. return;
  7535. case ZEND_AST_VAR:
  7536. case ZEND_AST_DIM:
  7537. case ZEND_AST_PROP:
  7538. case ZEND_AST_STATIC_PROP:
  7539. case ZEND_AST_CALL:
  7540. case ZEND_AST_METHOD_CALL:
  7541. case ZEND_AST_STATIC_CALL:
  7542. zend_compile_var(result, ast, BP_VAR_R, 0);
  7543. return;
  7544. case ZEND_AST_ASSIGN:
  7545. zend_compile_assign(result, ast);
  7546. return;
  7547. case ZEND_AST_ASSIGN_REF:
  7548. zend_compile_assign_ref(result, ast);
  7549. return;
  7550. case ZEND_AST_NEW:
  7551. zend_compile_new(result, ast);
  7552. return;
  7553. case ZEND_AST_CLONE:
  7554. zend_compile_clone(result, ast);
  7555. return;
  7556. case ZEND_AST_ASSIGN_OP:
  7557. zend_compile_compound_assign(result, ast);
  7558. return;
  7559. case ZEND_AST_BINARY_OP:
  7560. zend_compile_binary_op(result, ast);
  7561. return;
  7562. case ZEND_AST_GREATER:
  7563. case ZEND_AST_GREATER_EQUAL:
  7564. zend_compile_greater(result, ast);
  7565. return;
  7566. case ZEND_AST_UNARY_OP:
  7567. zend_compile_unary_op(result, ast);
  7568. return;
  7569. case ZEND_AST_UNARY_PLUS:
  7570. case ZEND_AST_UNARY_MINUS:
  7571. zend_compile_unary_pm(result, ast);
  7572. return;
  7573. case ZEND_AST_AND:
  7574. case ZEND_AST_OR:
  7575. zend_compile_short_circuiting(result, ast);
  7576. return;
  7577. case ZEND_AST_POST_INC:
  7578. case ZEND_AST_POST_DEC:
  7579. zend_compile_post_incdec(result, ast);
  7580. return;
  7581. case ZEND_AST_PRE_INC:
  7582. case ZEND_AST_PRE_DEC:
  7583. zend_compile_pre_incdec(result, ast);
  7584. return;
  7585. case ZEND_AST_CAST:
  7586. zend_compile_cast(result, ast);
  7587. return;
  7588. case ZEND_AST_CONDITIONAL:
  7589. zend_compile_conditional(result, ast);
  7590. return;
  7591. case ZEND_AST_COALESCE:
  7592. zend_compile_coalesce(result, ast);
  7593. return;
  7594. case ZEND_AST_ASSIGN_COALESCE:
  7595. zend_compile_assign_coalesce(result, ast);
  7596. return;
  7597. case ZEND_AST_PRINT:
  7598. zend_compile_print(result, ast);
  7599. return;
  7600. case ZEND_AST_EXIT:
  7601. zend_compile_exit(result, ast);
  7602. return;
  7603. case ZEND_AST_YIELD:
  7604. zend_compile_yield(result, ast);
  7605. return;
  7606. case ZEND_AST_YIELD_FROM:
  7607. zend_compile_yield_from(result, ast);
  7608. return;
  7609. case ZEND_AST_INSTANCEOF:
  7610. zend_compile_instanceof(result, ast);
  7611. return;
  7612. case ZEND_AST_INCLUDE_OR_EVAL:
  7613. zend_compile_include_or_eval(result, ast);
  7614. return;
  7615. case ZEND_AST_ISSET:
  7616. case ZEND_AST_EMPTY:
  7617. zend_compile_isset_or_empty(result, ast);
  7618. return;
  7619. case ZEND_AST_SILENCE:
  7620. zend_compile_silence(result, ast);
  7621. return;
  7622. case ZEND_AST_SHELL_EXEC:
  7623. zend_compile_shell_exec(result, ast);
  7624. return;
  7625. case ZEND_AST_ARRAY:
  7626. zend_compile_array(result, ast);
  7627. return;
  7628. case ZEND_AST_CONST:
  7629. zend_compile_const(result, ast);
  7630. return;
  7631. case ZEND_AST_CLASS_CONST:
  7632. zend_compile_class_const(result, ast);
  7633. return;
  7634. case ZEND_AST_CLASS_NAME:
  7635. zend_compile_class_name(result, ast);
  7636. return;
  7637. case ZEND_AST_ENCAPS_LIST:
  7638. zend_compile_encaps_list(result, ast);
  7639. return;
  7640. case ZEND_AST_MAGIC_CONST:
  7641. zend_compile_magic_const(result, ast);
  7642. return;
  7643. case ZEND_AST_CLOSURE:
  7644. case ZEND_AST_ARROW_FUNC:
  7645. zend_compile_func_decl(result, ast, 0);
  7646. return;
  7647. case ZEND_AST_THROW:
  7648. zend_compile_throw(result, ast);
  7649. return;
  7650. default:
  7651. ZEND_ASSERT(0 /* not supported */);
  7652. }
  7653. }
  7654. /* }}} */
  7655. zend_op *zend_compile_var(znode *result, zend_ast *ast, uint32_t type, int by_ref) /* {{{ */
  7656. {
  7657. CG(zend_lineno) = zend_ast_get_lineno(ast);
  7658. switch (ast->kind) {
  7659. case ZEND_AST_VAR:
  7660. return zend_compile_simple_var(result, ast, type, 0);
  7661. case ZEND_AST_DIM:
  7662. return zend_compile_dim(result, ast, type);
  7663. case ZEND_AST_PROP:
  7664. return zend_compile_prop(result, ast, type, by_ref);
  7665. case ZEND_AST_STATIC_PROP:
  7666. return zend_compile_static_prop(result, ast, type, by_ref, 0);
  7667. case ZEND_AST_CALL:
  7668. zend_compile_call(result, ast, type);
  7669. return NULL;
  7670. case ZEND_AST_METHOD_CALL:
  7671. zend_compile_method_call(result, ast, type);
  7672. return NULL;
  7673. case ZEND_AST_STATIC_CALL:
  7674. zend_compile_static_call(result, ast, type);
  7675. return NULL;
  7676. case ZEND_AST_ZNODE:
  7677. *result = *zend_ast_get_znode(ast);
  7678. return NULL;
  7679. default:
  7680. if (type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET) {
  7681. zend_error_noreturn(E_COMPILE_ERROR,
  7682. "Cannot use temporary expression in write context");
  7683. }
  7684. zend_compile_expr(result, ast);
  7685. return NULL;
  7686. }
  7687. }
  7688. /* }}} */
  7689. zend_op *zend_delayed_compile_var(znode *result, zend_ast *ast, uint32_t type, zend_bool by_ref) /* {{{ */
  7690. {
  7691. switch (ast->kind) {
  7692. case ZEND_AST_VAR:
  7693. return zend_compile_simple_var(result, ast, type, 1);
  7694. case ZEND_AST_DIM:
  7695. return zend_delayed_compile_dim(result, ast, type);
  7696. case ZEND_AST_PROP:
  7697. {
  7698. zend_op *opline = zend_delayed_compile_prop(result, ast, type);
  7699. if (by_ref) {
  7700. opline->extended_value |= ZEND_FETCH_REF;
  7701. }
  7702. return opline;
  7703. }
  7704. case ZEND_AST_STATIC_PROP:
  7705. return zend_compile_static_prop(result, ast, type, by_ref, 1);
  7706. default:
  7707. return zend_compile_var(result, ast, type, 0);
  7708. }
  7709. }
  7710. /* }}} */
  7711. void zend_eval_const_expr(zend_ast **ast_ptr) /* {{{ */
  7712. {
  7713. zend_ast *ast = *ast_ptr;
  7714. zval result;
  7715. if (!ast) {
  7716. return;
  7717. }
  7718. switch (ast->kind) {
  7719. case ZEND_AST_BINARY_OP:
  7720. zend_eval_const_expr(&ast->child[0]);
  7721. zend_eval_const_expr(&ast->child[1]);
  7722. if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
  7723. return;
  7724. }
  7725. if (!zend_try_ct_eval_binary_op(&result, ast->attr,
  7726. zend_ast_get_zval(ast->child[0]), zend_ast_get_zval(ast->child[1]))
  7727. ) {
  7728. return;
  7729. }
  7730. break;
  7731. case ZEND_AST_GREATER:
  7732. case ZEND_AST_GREATER_EQUAL:
  7733. zend_eval_const_expr(&ast->child[0]);
  7734. zend_eval_const_expr(&ast->child[1]);
  7735. if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
  7736. return;
  7737. }
  7738. zend_ct_eval_greater(&result, ast->kind,
  7739. zend_ast_get_zval(ast->child[0]), zend_ast_get_zval(ast->child[1]));
  7740. break;
  7741. case ZEND_AST_AND:
  7742. case ZEND_AST_OR:
  7743. {
  7744. zend_bool child0_is_true, child1_is_true;
  7745. zend_eval_const_expr(&ast->child[0]);
  7746. zend_eval_const_expr(&ast->child[1]);
  7747. if (ast->child[0]->kind != ZEND_AST_ZVAL) {
  7748. return;
  7749. }
  7750. child0_is_true = zend_is_true(zend_ast_get_zval(ast->child[0]));
  7751. if (child0_is_true == (ast->kind == ZEND_AST_OR)) {
  7752. ZVAL_BOOL(&result, ast->kind == ZEND_AST_OR);
  7753. break;
  7754. }
  7755. if (ast->child[1]->kind != ZEND_AST_ZVAL) {
  7756. return;
  7757. }
  7758. child1_is_true = zend_is_true(zend_ast_get_zval(ast->child[1]));
  7759. if (ast->kind == ZEND_AST_OR) {
  7760. ZVAL_BOOL(&result, child0_is_true || child1_is_true);
  7761. } else {
  7762. ZVAL_BOOL(&result, child0_is_true && child1_is_true);
  7763. }
  7764. break;
  7765. }
  7766. case ZEND_AST_UNARY_OP:
  7767. zend_eval_const_expr(&ast->child[0]);
  7768. if (ast->child[0]->kind != ZEND_AST_ZVAL) {
  7769. return;
  7770. }
  7771. zend_ct_eval_unary_op(&result, ast->attr, zend_ast_get_zval(ast->child[0]));
  7772. break;
  7773. case ZEND_AST_UNARY_PLUS:
  7774. case ZEND_AST_UNARY_MINUS:
  7775. zend_eval_const_expr(&ast->child[0]);
  7776. if (ast->child[0]->kind != ZEND_AST_ZVAL) {
  7777. return;
  7778. }
  7779. if (!zend_try_ct_eval_unary_pm(&result, ast->kind, zend_ast_get_zval(ast->child[0]))) {
  7780. return;
  7781. }
  7782. break;
  7783. case ZEND_AST_COALESCE:
  7784. /* Set isset fetch indicator here, opcache disallows runtime altering of the AST */
  7785. if (ast->child[0]->kind == ZEND_AST_DIM) {
  7786. ast->child[0]->attr |= ZEND_DIM_IS;
  7787. }
  7788. zend_eval_const_expr(&ast->child[0]);
  7789. if (ast->child[0]->kind != ZEND_AST_ZVAL) {
  7790. /* ensure everything was compile-time evaluated at least once */
  7791. zend_eval_const_expr(&ast->child[1]);
  7792. return;
  7793. }
  7794. if (Z_TYPE_P(zend_ast_get_zval(ast->child[0])) == IS_NULL) {
  7795. zend_eval_const_expr(&ast->child[1]);
  7796. *ast_ptr = ast->child[1];
  7797. ast->child[1] = NULL;
  7798. zend_ast_destroy(ast);
  7799. } else {
  7800. *ast_ptr = ast->child[0];
  7801. ast->child[0] = NULL;
  7802. zend_ast_destroy(ast);
  7803. }
  7804. return;
  7805. case ZEND_AST_CONDITIONAL:
  7806. {
  7807. zend_ast **child, *child_ast;
  7808. zend_eval_const_expr(&ast->child[0]);
  7809. if (ast->child[0]->kind != ZEND_AST_ZVAL) {
  7810. /* ensure everything was compile-time evaluated at least once */
  7811. if (ast->child[1]) {
  7812. zend_eval_const_expr(&ast->child[1]);
  7813. }
  7814. zend_eval_const_expr(&ast->child[2]);
  7815. return;
  7816. }
  7817. child = &ast->child[2 - zend_is_true(zend_ast_get_zval(ast->child[0]))];
  7818. if (*child == NULL) {
  7819. child--;
  7820. }
  7821. child_ast = *child;
  7822. *child = NULL;
  7823. zend_ast_destroy(ast);
  7824. *ast_ptr = child_ast;
  7825. zend_eval_const_expr(ast_ptr);
  7826. return;
  7827. }
  7828. case ZEND_AST_DIM:
  7829. {
  7830. /* constant expression should be always read context ... */
  7831. zval *container, *dim;
  7832. if (ast->child[1] == NULL) {
  7833. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
  7834. }
  7835. if (ast->attr & ZEND_DIM_ALTERNATIVE_SYNTAX) {
  7836. ast->attr &= ~ZEND_DIM_ALTERNATIVE_SYNTAX; /* remove flag to avoid duplicate warning */
  7837. zend_error(E_DEPRECATED, "Array and string offset access syntax with curly braces is deprecated");
  7838. }
  7839. /* Set isset fetch indicator here, opcache disallows runtime altering of the AST */
  7840. if (ast->attr & ZEND_DIM_IS && ast->child[0]->kind == ZEND_AST_DIM) {
  7841. ast->child[0]->attr |= ZEND_DIM_IS;
  7842. }
  7843. zend_eval_const_expr(&ast->child[0]);
  7844. zend_eval_const_expr(&ast->child[1]);
  7845. if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
  7846. return;
  7847. }
  7848. container = zend_ast_get_zval(ast->child[0]);
  7849. dim = zend_ast_get_zval(ast->child[1]);
  7850. if (Z_TYPE_P(container) == IS_ARRAY) {
  7851. zval *el;
  7852. if (Z_TYPE_P(dim) == IS_LONG) {
  7853. el = zend_hash_index_find(Z_ARR_P(container), Z_LVAL_P(dim));
  7854. if (el) {
  7855. ZVAL_COPY(&result, el);
  7856. } else {
  7857. return;
  7858. }
  7859. } else if (Z_TYPE_P(dim) == IS_STRING) {
  7860. el = zend_symtable_find(Z_ARR_P(container), Z_STR_P(dim));
  7861. if (el) {
  7862. ZVAL_COPY(&result, el);
  7863. } else {
  7864. return;
  7865. }
  7866. } else {
  7867. return; /* warning... handle at runtime */
  7868. }
  7869. } else if (Z_TYPE_P(container) == IS_STRING) {
  7870. zend_long offset;
  7871. zend_uchar c;
  7872. if (Z_TYPE_P(dim) == IS_LONG) {
  7873. offset = Z_LVAL_P(dim);
  7874. } else if (Z_TYPE_P(dim) != IS_STRING || is_numeric_string(Z_STRVAL_P(dim), Z_STRLEN_P(dim), &offset, NULL, 1) != IS_LONG) {
  7875. return;
  7876. }
  7877. if (offset < 0 || (size_t)offset >= Z_STRLEN_P(container)) {
  7878. return;
  7879. }
  7880. c = (zend_uchar) Z_STRVAL_P(container)[offset];
  7881. ZVAL_INTERNED_STR(&result, ZSTR_CHAR(c));
  7882. } else if (Z_TYPE_P(container) <= IS_FALSE) {
  7883. ZVAL_NULL(&result);
  7884. } else {
  7885. return;
  7886. }
  7887. break;
  7888. }
  7889. case ZEND_AST_ARRAY:
  7890. if (!zend_try_ct_eval_array(&result, ast)) {
  7891. return;
  7892. }
  7893. break;
  7894. case ZEND_AST_MAGIC_CONST:
  7895. if (!zend_try_ct_eval_magic_const(&result, ast)) {
  7896. return;
  7897. }
  7898. break;
  7899. case ZEND_AST_CONST:
  7900. {
  7901. zend_ast *name_ast = ast->child[0];
  7902. zend_bool is_fully_qualified;
  7903. zend_string *resolved_name = zend_resolve_const_name(
  7904. zend_ast_get_str(name_ast), name_ast->attr, &is_fully_qualified);
  7905. if (!zend_try_ct_eval_const(&result, resolved_name, is_fully_qualified)) {
  7906. zend_string_release_ex(resolved_name, 0);
  7907. return;
  7908. }
  7909. zend_string_release_ex(resolved_name, 0);
  7910. break;
  7911. }
  7912. case ZEND_AST_CLASS_CONST:
  7913. {
  7914. zend_ast *class_ast;
  7915. zend_ast *name_ast;
  7916. zend_string *resolved_name;
  7917. zend_eval_const_expr(&ast->child[0]);
  7918. zend_eval_const_expr(&ast->child[1]);
  7919. class_ast = ast->child[0];
  7920. name_ast = ast->child[1];
  7921. if (class_ast->kind != ZEND_AST_ZVAL || name_ast->kind != ZEND_AST_ZVAL) {
  7922. return;
  7923. }
  7924. resolved_name = zend_resolve_class_name_ast(class_ast);
  7925. if (!zend_try_ct_eval_class_const(&result, resolved_name, zend_ast_get_str(name_ast))) {
  7926. zend_string_release_ex(resolved_name, 0);
  7927. return;
  7928. }
  7929. zend_string_release_ex(resolved_name, 0);
  7930. break;
  7931. }
  7932. case ZEND_AST_CLASS_NAME:
  7933. {
  7934. zend_ast *class_ast = ast->child[0];
  7935. if (!zend_try_compile_const_expr_resolve_class_name(&result, class_ast)) {
  7936. return;
  7937. }
  7938. break;
  7939. }
  7940. default:
  7941. return;
  7942. }
  7943. zend_ast_destroy(ast);
  7944. *ast_ptr = zend_ast_create_zval(&result);
  7945. }
  7946. /* }}} */