PageRenderTime 81ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/Zend/zend_compile.c

http://github.com/infusion/PHP
C | 5454 lines | 4294 code | 754 blank | 406 comment | 1094 complexity | 79b2bba80d4838380d8d3483804f6547 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1998-2011 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@zend.com> |
  16. | Zeev Suraski <zeev@zend.com> |
  17. +----------------------------------------------------------------------+
  18. */
  19. /* $Id: zend_compile.c 308443 2011-02-17 23:24:50Z felipe $ */
  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 "tsrm_virtual_cwd.h"
  28. #ifdef ZEND_MULTIBYTE
  29. #include "zend_multibyte.h"
  30. #endif /* ZEND_MULTIBYTE */
  31. ZEND_API zend_op_array *(*zend_compile_file)(zend_file_handle *file_handle, int type TSRMLS_DC);
  32. ZEND_API zend_op_array *(*zend_compile_string)(zval *source_string, char *filename TSRMLS_DC);
  33. #ifndef ZTS
  34. ZEND_API zend_compiler_globals compiler_globals;
  35. ZEND_API zend_executor_globals executor_globals;
  36. #endif
  37. static void zend_duplicate_property_info(zend_property_info *property_info) /* {{{ */
  38. {
  39. property_info->name = estrndup(property_info->name, property_info->name_length);
  40. if (property_info->doc_comment) {
  41. property_info->doc_comment = estrndup(property_info->doc_comment, property_info->doc_comment_len);
  42. }
  43. }
  44. /* }}} */
  45. static void zend_duplicate_property_info_internal(zend_property_info *property_info) /* {{{ */
  46. {
  47. property_info->name = zend_strndup(property_info->name, property_info->name_length);
  48. }
  49. /* }}} */
  50. static void zend_destroy_property_info(zend_property_info *property_info) /* {{{ */
  51. {
  52. efree(property_info->name);
  53. if (property_info->doc_comment) {
  54. efree(property_info->doc_comment);
  55. }
  56. }
  57. /* }}} */
  58. static void zend_destroy_property_info_internal(zend_property_info *property_info) /* {{{ */
  59. {
  60. free(property_info->name);
  61. }
  62. /* }}} */
  63. static void build_runtime_defined_function_key(zval *result, const char *name, int name_length TSRMLS_DC) /* {{{ */
  64. {
  65. char char_pos_buf[32];
  66. uint char_pos_len;
  67. char *filename;
  68. char_pos_len = zend_sprintf(char_pos_buf, "%p", LANG_SCNG(yy_text));
  69. if (CG(active_op_array)->filename) {
  70. filename = CG(active_op_array)->filename;
  71. } else {
  72. filename = "-";
  73. }
  74. /* NULL, name length, filename length, last accepting char position length */
  75. result->value.str.len = 1+name_length+strlen(filename)+char_pos_len;
  76. #ifdef ZEND_MULTIBYTE
  77. /* must be binary safe */
  78. result->value.str.val = (char *) safe_emalloc(result->value.str.len, 1, 1);
  79. result->value.str.val[0] = '\0';
  80. sprintf(result->value.str.val+1, "%s%s%s", name, filename, char_pos_buf);
  81. #else
  82. zend_spprintf(&result->value.str.val, 0, "%c%s%s%s", '\0', name, filename, char_pos_buf);
  83. #endif /* ZEND_MULTIBYTE */
  84. result->type = IS_STRING;
  85. Z_SET_REFCOUNT_P(result, 1);
  86. }
  87. /* }}} */
  88. int zend_auto_global_arm(zend_auto_global *auto_global TSRMLS_DC) /* {{{ */
  89. {
  90. auto_global->armed = (auto_global->auto_global_callback ? 1 : 0);
  91. return 0;
  92. }
  93. /* }}} */
  94. ZEND_API int zend_auto_global_disable_jit(const char *varname, zend_uint varname_length TSRMLS_DC) /* {{{ */
  95. {
  96. zend_auto_global *auto_global;
  97. if (zend_hash_find(CG(auto_globals), varname, varname_length+1, (void **) &auto_global)==FAILURE) {
  98. return FAILURE;
  99. }
  100. auto_global->armed = 0;
  101. return SUCCESS;
  102. }
  103. /* }}} */
  104. static void init_compiler_declarables(TSRMLS_D) /* {{{ */
  105. {
  106. Z_TYPE(CG(declarables).ticks) = IS_LONG;
  107. Z_LVAL(CG(declarables).ticks) = 0;
  108. }
  109. /* }}} */
  110. void zend_init_compiler_data_structures(TSRMLS_D) /* {{{ */
  111. {
  112. zend_stack_init(&CG(bp_stack));
  113. zend_stack_init(&CG(function_call_stack));
  114. zend_stack_init(&CG(switch_cond_stack));
  115. zend_stack_init(&CG(foreach_copy_stack));
  116. zend_stack_init(&CG(object_stack));
  117. zend_stack_init(&CG(declare_stack));
  118. CG(active_class_entry) = NULL;
  119. zend_llist_init(&CG(list_llist), sizeof(list_llist_element), NULL, 0);
  120. zend_llist_init(&CG(dimension_llist), sizeof(int), NULL, 0);
  121. zend_stack_init(&CG(list_stack));
  122. CG(in_compilation) = 0;
  123. CG(start_lineno) = 0;
  124. CG(current_namespace) = NULL;
  125. CG(in_namespace) = 0;
  126. CG(has_bracketed_namespaces) = 0;
  127. CG(current_import) = NULL;
  128. init_compiler_declarables(TSRMLS_C);
  129. zend_hash_apply(CG(auto_globals), (apply_func_t) zend_auto_global_arm TSRMLS_CC);
  130. zend_stack_init(&CG(labels_stack));
  131. CG(labels) = NULL;
  132. #ifdef ZEND_MULTIBYTE
  133. CG(script_encoding_list) = NULL;
  134. CG(script_encoding_list_size) = 0;
  135. CG(internal_encoding) = NULL;
  136. CG(encoding_detector) = NULL;
  137. CG(encoding_converter) = NULL;
  138. CG(encoding_oddlen) = NULL;
  139. CG(encoding_declared) = 0;
  140. #endif /* ZEND_MULTIBYTE */
  141. }
  142. /* }}} */
  143. ZEND_API void file_handle_dtor(zend_file_handle *fh) /* {{{ */
  144. {
  145. TSRMLS_FETCH();
  146. zend_file_handle_dtor(fh TSRMLS_CC);
  147. }
  148. /* }}} */
  149. void init_compiler(TSRMLS_D) /* {{{ */
  150. {
  151. CG(active_op_array) = NULL;
  152. zend_init_compiler_data_structures(TSRMLS_C);
  153. zend_init_rsrc_list(TSRMLS_C);
  154. zend_hash_init(&CG(filenames_table), 5, NULL, (dtor_func_t) free_estring, 0);
  155. zend_llist_init(&CG(open_files), sizeof(zend_file_handle), (void (*)(void *)) file_handle_dtor, 0);
  156. CG(unclean_shutdown) = 0;
  157. }
  158. /* }}} */
  159. void shutdown_compiler(TSRMLS_D) /* {{{ */
  160. {
  161. zend_stack_destroy(&CG(bp_stack));
  162. zend_stack_destroy(&CG(function_call_stack));
  163. zend_stack_destroy(&CG(switch_cond_stack));
  164. zend_stack_destroy(&CG(foreach_copy_stack));
  165. zend_stack_destroy(&CG(object_stack));
  166. zend_stack_destroy(&CG(declare_stack));
  167. zend_stack_destroy(&CG(list_stack));
  168. zend_hash_destroy(&CG(filenames_table));
  169. zend_llist_destroy(&CG(open_files));
  170. zend_stack_destroy(&CG(labels_stack));
  171. #ifdef ZEND_MULTIBYTE
  172. if (CG(script_encoding_list)) {
  173. efree(CG(script_encoding_list));
  174. }
  175. #endif /* ZEND_MULTIBYTE */
  176. }
  177. /* }}} */
  178. ZEND_API char *zend_set_compiled_filename(const char *new_compiled_filename TSRMLS_DC) /* {{{ */
  179. {
  180. char **pp, *p;
  181. int length = strlen(new_compiled_filename);
  182. if (zend_hash_find(&CG(filenames_table), new_compiled_filename, length+1, (void **) &pp) == SUCCESS) {
  183. CG(compiled_filename) = *pp;
  184. return *pp;
  185. }
  186. p = estrndup(new_compiled_filename, length);
  187. zend_hash_update(&CG(filenames_table), new_compiled_filename, length+1, &p, sizeof(char *), (void **) &pp);
  188. CG(compiled_filename) = p;
  189. return p;
  190. }
  191. /* }}} */
  192. ZEND_API void zend_restore_compiled_filename(char *original_compiled_filename TSRMLS_DC) /* {{{ */
  193. {
  194. CG(compiled_filename) = original_compiled_filename;
  195. }
  196. /* }}} */
  197. ZEND_API char *zend_get_compiled_filename(TSRMLS_D) /* {{{ */
  198. {
  199. return CG(compiled_filename);
  200. }
  201. /* }}} */
  202. ZEND_API int zend_get_compiled_lineno(TSRMLS_D) /* {{{ */
  203. {
  204. return CG(zend_lineno);
  205. }
  206. /* }}} */
  207. ZEND_API zend_bool zend_is_compiling(TSRMLS_D) /* {{{ */
  208. {
  209. return CG(in_compilation);
  210. }
  211. /* }}} */
  212. static zend_uint get_temporary_variable(zend_op_array *op_array) /* {{{ */
  213. {
  214. return (op_array->T)++ * ZEND_MM_ALIGNED_SIZE(sizeof(temp_variable));
  215. }
  216. /* }}} */
  217. static int lookup_cv(zend_op_array *op_array, char* name, int name_len) /* {{{ */
  218. {
  219. int i = 0;
  220. ulong hash_value = zend_inline_hash_func(name, name_len+1);
  221. while (i < op_array->last_var) {
  222. if (op_array->vars[i].hash_value == hash_value &&
  223. op_array->vars[i].name_len == name_len &&
  224. strcmp(op_array->vars[i].name, name) == 0) {
  225. efree(name);
  226. return i;
  227. }
  228. i++;
  229. }
  230. i = op_array->last_var;
  231. op_array->last_var++;
  232. if (op_array->last_var > op_array->size_var) {
  233. op_array->size_var += 16; /* FIXME */
  234. op_array->vars = erealloc(op_array->vars, op_array->size_var*sizeof(zend_compiled_variable));
  235. }
  236. op_array->vars[i].name = name; /* estrndup(name, name_len); */
  237. op_array->vars[i].name_len = name_len;
  238. op_array->vars[i].hash_value = hash_value;
  239. return i;
  240. }
  241. /* }}} */
  242. void zend_do_binary_op(zend_uchar op, znode *result, const znode *op1, const znode *op2 TSRMLS_DC) /* {{{ */
  243. {
  244. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  245. opline->opcode = op;
  246. opline->result.op_type = IS_TMP_VAR;
  247. opline->result.u.var = get_temporary_variable(CG(active_op_array));
  248. opline->op1 = *op1;
  249. opline->op2 = *op2;
  250. *result = opline->result;
  251. }
  252. /* }}} */
  253. void zend_do_unary_op(zend_uchar op, znode *result, const znode *op1 TSRMLS_DC) /* {{{ */
  254. {
  255. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  256. opline->opcode = op;
  257. opline->result.op_type = IS_TMP_VAR;
  258. opline->result.u.var = get_temporary_variable(CG(active_op_array));
  259. opline->op1 = *op1;
  260. *result = opline->result;
  261. SET_UNUSED(opline->op2);
  262. }
  263. /* }}} */
  264. #define MAKE_NOP(opline) { opline->opcode = ZEND_NOP; memset(&opline->result,0,sizeof(znode)); memset(&opline->op1,0,sizeof(znode)); memset(&opline->op2,0,sizeof(znode)); opline->result.op_type=opline->op1.op_type=opline->op2.op_type=IS_UNUSED; }
  265. static void zend_do_op_data(zend_op *data_op, const znode *value TSRMLS_DC) /* {{{ */
  266. {
  267. data_op->opcode = ZEND_OP_DATA;
  268. data_op->op1 = *value;
  269. SET_UNUSED(data_op->op2);
  270. }
  271. /* }}} */
  272. void zend_do_binary_assign_op(zend_uchar op, znode *result, const znode *op1, const znode *op2 TSRMLS_DC) /* {{{ */
  273. {
  274. int last_op_number = get_next_op_number(CG(active_op_array));
  275. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  276. if (last_op_number > 0) {
  277. zend_op *last_op = &CG(active_op_array)->opcodes[last_op_number-1];
  278. switch (last_op->opcode) {
  279. case ZEND_FETCH_OBJ_RW:
  280. last_op->opcode = op;
  281. last_op->extended_value = ZEND_ASSIGN_OBJ;
  282. zend_do_op_data(opline, op2 TSRMLS_CC);
  283. SET_UNUSED(opline->result);
  284. *result = last_op->result;
  285. return;
  286. case ZEND_FETCH_DIM_RW:
  287. last_op->opcode = op;
  288. last_op->extended_value = ZEND_ASSIGN_DIM;
  289. zend_do_op_data(opline, op2 TSRMLS_CC);
  290. opline->op2.u.var = get_temporary_variable(CG(active_op_array));
  291. opline->op2.u.EA.type = 0;
  292. opline->op2.op_type = IS_VAR;
  293. SET_UNUSED(opline->result);
  294. *result = last_op->result;
  295. return;
  296. default:
  297. break;
  298. }
  299. }
  300. opline->opcode = op;
  301. opline->op1 = *op1;
  302. opline->op2 = *op2;
  303. opline->result.op_type = IS_VAR;
  304. opline->result.u.EA.type = 0;
  305. opline->result.u.var = get_temporary_variable(CG(active_op_array));
  306. *result = opline->result;
  307. }
  308. /* }}} */
  309. void fetch_simple_variable_ex(znode *result, znode *varname, int bp, zend_uchar op TSRMLS_DC) /* {{{ */
  310. {
  311. zend_op opline;
  312. zend_op *opline_ptr;
  313. zend_llist *fetch_list_ptr;
  314. if (varname->op_type == IS_CONST) {
  315. if (Z_TYPE(varname->u.constant) != IS_STRING) {
  316. convert_to_string(&varname->u.constant);
  317. }
  318. if (!zend_is_auto_global(varname->u.constant.value.str.val, varname->u.constant.value.str.len TSRMLS_CC) &&
  319. !(varname->u.constant.value.str.len == (sizeof("this")-1) &&
  320. !memcmp(varname->u.constant.value.str.val, "this", sizeof("this"))) &&
  321. (CG(active_op_array)->last == 0 ||
  322. CG(active_op_array)->opcodes[CG(active_op_array)->last-1].opcode != ZEND_BEGIN_SILENCE)) {
  323. result->op_type = IS_CV;
  324. result->u.var = lookup_cv(CG(active_op_array), varname->u.constant.value.str.val, varname->u.constant.value.str.len);
  325. result->u.EA.type = 0;
  326. varname->u.constant.value.str.val = CG(active_op_array)->vars[result->u.var].name;
  327. return;
  328. }
  329. }
  330. if (bp) {
  331. opline_ptr = &opline;
  332. init_op(opline_ptr TSRMLS_CC);
  333. } else {
  334. opline_ptr = get_next_op(CG(active_op_array) TSRMLS_CC);
  335. }
  336. opline_ptr->opcode = op;
  337. opline_ptr->result.op_type = IS_VAR;
  338. opline_ptr->result.u.EA.type = 0;
  339. opline_ptr->result.u.var = get_temporary_variable(CG(active_op_array));
  340. opline_ptr->op1 = *varname;
  341. *result = opline_ptr->result;
  342. SET_UNUSED(opline_ptr->op2);
  343. opline_ptr->op2.u.EA.type = ZEND_FETCH_LOCAL;
  344. if (varname->op_type == IS_CONST && varname->u.constant.type == IS_STRING) {
  345. if (zend_is_auto_global(varname->u.constant.value.str.val, varname->u.constant.value.str.len TSRMLS_CC)) {
  346. opline_ptr->op2.u.EA.type = ZEND_FETCH_GLOBAL;
  347. }
  348. }
  349. if (bp) {
  350. zend_stack_top(&CG(bp_stack), (void **) &fetch_list_ptr);
  351. zend_llist_add_element(fetch_list_ptr, opline_ptr);
  352. }
  353. }
  354. /* }}} */
  355. void fetch_simple_variable(znode *result, znode *varname, int bp TSRMLS_DC) /* {{{ */
  356. {
  357. /* the default mode must be Write, since fetch_simple_variable() is used to define function arguments */
  358. fetch_simple_variable_ex(result, varname, bp, ZEND_FETCH_W TSRMLS_CC);
  359. }
  360. /* }}} */
  361. void zend_do_fetch_static_member(znode *result, znode *class_name TSRMLS_DC) /* {{{ */
  362. {
  363. znode class_node;
  364. zend_llist *fetch_list_ptr;
  365. zend_llist_element *le;
  366. zend_op *opline_ptr;
  367. zend_op opline;
  368. zend_do_fetch_class(&class_node, class_name TSRMLS_CC);
  369. zend_stack_top(&CG(bp_stack), (void **) &fetch_list_ptr);
  370. if (result->op_type == IS_CV) {
  371. init_op(&opline TSRMLS_CC);
  372. opline.opcode = ZEND_FETCH_W;
  373. opline.result.op_type = IS_VAR;
  374. opline.result.u.EA.type = 0;
  375. opline.result.u.var = get_temporary_variable(CG(active_op_array));
  376. opline.op1.op_type = IS_CONST;
  377. opline.op1.u.constant.type = IS_STRING;
  378. opline.op1.u.constant.value.str.val = estrdup(CG(active_op_array)->vars[result->u.var].name);
  379. opline.op1.u.constant.value.str.len = CG(active_op_array)->vars[result->u.var].name_len;
  380. SET_UNUSED(opline.op2);
  381. opline.op2 = class_node;
  382. opline.op2.u.EA.type = ZEND_FETCH_STATIC_MEMBER;
  383. *result = opline.result;
  384. zend_llist_add_element(fetch_list_ptr, &opline);
  385. } else {
  386. le = fetch_list_ptr->head;
  387. opline_ptr = (zend_op *)le->data;
  388. if (opline_ptr->opcode != ZEND_FETCH_W && opline_ptr->op1.op_type == IS_CV) {
  389. init_op(&opline TSRMLS_CC);
  390. opline.opcode = ZEND_FETCH_W;
  391. opline.result.op_type = IS_VAR;
  392. opline.result.u.EA.type = 0;
  393. opline.result.u.var = get_temporary_variable(CG(active_op_array));
  394. opline.op1.op_type = IS_CONST;
  395. opline.op1.u.constant.type = IS_STRING;
  396. opline.op1.u.constant.value.str.val = estrdup(CG(active_op_array)->vars[opline_ptr->op1.u.var].name);
  397. opline.op1.u.constant.value.str.len = CG(active_op_array)->vars[opline_ptr->op1.u.var].name_len;
  398. SET_UNUSED(opline.op2);
  399. opline.op2 = class_node;
  400. opline.op2.u.EA.type = ZEND_FETCH_STATIC_MEMBER;
  401. opline_ptr->op1 = opline.result;
  402. zend_llist_prepend_element(fetch_list_ptr, &opline);
  403. } else {
  404. opline_ptr->op2 = class_node;
  405. opline_ptr->op2.u.EA.type = ZEND_FETCH_STATIC_MEMBER;
  406. }
  407. }
  408. }
  409. /* }}} */
  410. void fetch_array_begin(znode *result, znode *varname, znode *first_dim TSRMLS_DC) /* {{{ */
  411. {
  412. fetch_simple_variable(result, varname, 1 TSRMLS_CC);
  413. fetch_array_dim(result, result, first_dim TSRMLS_CC);
  414. }
  415. /* }}} */
  416. void fetch_array_dim(znode *result, const znode *parent, const znode *dim TSRMLS_DC) /* {{{ */
  417. {
  418. zend_op opline;
  419. zend_llist *fetch_list_ptr;
  420. init_op(&opline TSRMLS_CC);
  421. opline.opcode = ZEND_FETCH_DIM_W; /* the backpatching routine assumes W */
  422. opline.result.op_type = IS_VAR;
  423. opline.result.u.EA.type = 0;
  424. opline.result.u.var = get_temporary_variable(CG(active_op_array));
  425. opline.op1 = *parent;
  426. opline.op2 = *dim;
  427. opline.extended_value = ZEND_FETCH_STANDARD;
  428. *result = opline.result;
  429. zend_stack_top(&CG(bp_stack), (void **) &fetch_list_ptr);
  430. zend_llist_add_element(fetch_list_ptr, &opline);
  431. }
  432. /* }}} */
  433. void fetch_string_offset(znode *result, const znode *parent, const znode *offset TSRMLS_DC) /* {{{ */
  434. {
  435. fetch_array_dim(result, parent, offset TSRMLS_CC);
  436. }
  437. /* }}} */
  438. void zend_do_print(znode *result, const znode *arg TSRMLS_DC) /* {{{ */
  439. {
  440. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  441. opline->result.op_type = IS_TMP_VAR;
  442. opline->result.u.var = get_temporary_variable(CG(active_op_array));
  443. opline->opcode = ZEND_PRINT;
  444. opline->op1 = *arg;
  445. SET_UNUSED(opline->op2);
  446. *result = opline->result;
  447. }
  448. /* }}} */
  449. void zend_do_echo(const znode *arg TSRMLS_DC) /* {{{ */
  450. {
  451. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  452. opline->opcode = ZEND_ECHO;
  453. opline->op1 = *arg;
  454. SET_UNUSED(opline->op2);
  455. }
  456. /* }}} */
  457. void zend_do_abstract_method(const znode *function_name, znode *modifiers, const znode *body TSRMLS_DC) /* {{{ */
  458. {
  459. char *method_type;
  460. if (CG(active_class_entry)->ce_flags & ZEND_ACC_INTERFACE) {
  461. Z_LVAL(modifiers->u.constant) |= ZEND_ACC_ABSTRACT;
  462. method_type = "Interface";
  463. } else {
  464. method_type = "Abstract";
  465. }
  466. if (modifiers->u.constant.value.lval & ZEND_ACC_ABSTRACT) {
  467. if(modifiers->u.constant.value.lval & ZEND_ACC_PRIVATE) {
  468. zend_error(E_COMPILE_ERROR, "%s function %s::%s() cannot be declared private", method_type, CG(active_class_entry)->name, function_name->u.constant.value.str.val);
  469. }
  470. if (Z_LVAL(body->u.constant) == ZEND_ACC_ABSTRACT) {
  471. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  472. opline->opcode = ZEND_RAISE_ABSTRACT_ERROR;
  473. SET_UNUSED(opline->op1);
  474. SET_UNUSED(opline->op2);
  475. } else {
  476. /* we had code in the function body */
  477. zend_error(E_COMPILE_ERROR, "%s function %s::%s() cannot contain body", method_type, CG(active_class_entry)->name, function_name->u.constant.value.str.val);
  478. }
  479. } else {
  480. if (body->u.constant.value.lval == ZEND_ACC_ABSTRACT) {
  481. zend_error(E_COMPILE_ERROR, "Non-abstract method %s::%s() must contain body", CG(active_class_entry)->name, function_name->u.constant.value.str.val);
  482. }
  483. }
  484. }
  485. /* }}} */
  486. static zend_bool opline_is_fetch_this(const zend_op *opline TSRMLS_DC) /* {{{ */
  487. {
  488. if ((opline->opcode == ZEND_FETCH_W) && (opline->op1.op_type == IS_CONST)
  489. && (opline->op1.u.constant.type == IS_STRING)
  490. && (opline->op1.u.constant.value.str.len == (sizeof("this")-1))
  491. && !memcmp(opline->op1.u.constant.value.str.val, "this", sizeof("this"))) {
  492. return 1;
  493. } else {
  494. return 0;
  495. }
  496. }
  497. /* }}} */
  498. void zend_do_assign(znode *result, znode *variable, const znode *value TSRMLS_DC) /* {{{ */
  499. {
  500. int last_op_number;
  501. zend_op *opline;
  502. if (value->op_type == IS_CV) {
  503. zend_llist *fetch_list_ptr;
  504. zend_stack_top(&CG(bp_stack), (void **) &fetch_list_ptr);
  505. if (fetch_list_ptr && fetch_list_ptr->head) {
  506. opline = (zend_op *)fetch_list_ptr->head->data;
  507. if (opline->opcode == ZEND_FETCH_DIM_W &&
  508. opline->op1.op_type == IS_CV &&
  509. opline->op1.u.var == value->u.var) {
  510. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  511. opline->opcode = ZEND_FETCH_R;
  512. opline->result.op_type = IS_VAR;
  513. opline->result.u.EA.type = 0;
  514. opline->result.u.var = get_temporary_variable(CG(active_op_array));
  515. opline->op1.op_type = IS_CONST;
  516. ZVAL_STRINGL(&opline->op1.u.constant,
  517. CG(active_op_array)->vars[value->u.var].name,
  518. CG(active_op_array)->vars[value->u.var].name_len, 1);
  519. SET_UNUSED(opline->op2);
  520. opline->op2.u.EA.type = ZEND_FETCH_LOCAL;
  521. value = &opline->result;
  522. }
  523. }
  524. }
  525. zend_do_end_variable_parse(variable, BP_VAR_W, 0 TSRMLS_CC);
  526. last_op_number = get_next_op_number(CG(active_op_array));
  527. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  528. if (variable->op_type == IS_CV) {
  529. if (variable->u.var == CG(active_op_array)->this_var) {
  530. zend_error(E_COMPILE_ERROR, "Cannot re-assign $this");
  531. }
  532. } else if (variable->op_type == IS_VAR) {
  533. int n = 0;
  534. while (last_op_number - n > 0) {
  535. zend_op *last_op;
  536. last_op = &CG(active_op_array)->opcodes[last_op_number-n-1];
  537. if (last_op->result.op_type == IS_VAR &&
  538. last_op->result.u.var == variable->u.var) {
  539. if (last_op->opcode == ZEND_FETCH_OBJ_W) {
  540. if (n > 0) {
  541. int opline_no = (opline-CG(active_op_array)->opcodes)/sizeof(*opline);
  542. *opline = *last_op;
  543. MAKE_NOP(last_op);
  544. /* last_op = opline; */
  545. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  546. /* get_next_op can realloc, we need to move last_op */
  547. last_op = &CG(active_op_array)->opcodes[opline_no];
  548. }
  549. last_op->opcode = ZEND_ASSIGN_OBJ;
  550. zend_do_op_data(opline, value TSRMLS_CC);
  551. SET_UNUSED(opline->result);
  552. *result = last_op->result;
  553. return;
  554. } else if (last_op->opcode == ZEND_FETCH_DIM_W) {
  555. if (n > 0) {
  556. int opline_no = (opline-CG(active_op_array)->opcodes)/sizeof(*opline);
  557. *opline = *last_op;
  558. MAKE_NOP(last_op);
  559. /* last_op = opline; */
  560. /* TBFixed: this can realloc opcodes, leaving last_op pointing wrong */
  561. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  562. /* get_next_op can realloc, we need to move last_op */
  563. last_op = &CG(active_op_array)->opcodes[opline_no];
  564. }
  565. last_op->opcode = ZEND_ASSIGN_DIM;
  566. zend_do_op_data(opline, value TSRMLS_CC);
  567. opline->op2.u.var = get_temporary_variable(CG(active_op_array));
  568. opline->op2.u.EA.type = 0;
  569. opline->op2.op_type = IS_VAR;
  570. SET_UNUSED(opline->result);
  571. *result = last_op->result;
  572. return;
  573. } else if (opline_is_fetch_this(last_op TSRMLS_CC)) {
  574. zend_error(E_COMPILE_ERROR, "Cannot re-assign $this");
  575. } else {
  576. break;
  577. }
  578. }
  579. n++;
  580. }
  581. }
  582. opline->opcode = ZEND_ASSIGN;
  583. opline->op1 = *variable;
  584. opline->op2 = *value;
  585. opline->result.op_type = IS_VAR;
  586. opline->result.u.EA.type = 0;
  587. opline->result.u.var = get_temporary_variable(CG(active_op_array));
  588. *result = opline->result;
  589. }
  590. /* }}} */
  591. static inline zend_bool zend_is_function_or_method_call(const znode *variable) /* {{{ */
  592. {
  593. zend_uint type = variable->u.EA.type;
  594. return ((type & ZEND_PARSED_METHOD_CALL) || (type == ZEND_PARSED_FUNCTION_CALL));
  595. }
  596. /* }}} */
  597. void zend_do_assign_ref(znode *result, const znode *lvar, const znode *rvar TSRMLS_DC) /* {{{ */
  598. {
  599. zend_op *opline;
  600. if (lvar->op_type == IS_CV) {
  601. if (lvar->u.var == CG(active_op_array)->this_var) {
  602. zend_error(E_COMPILE_ERROR, "Cannot re-assign $this");
  603. }
  604. } else if (lvar->op_type == IS_VAR) {
  605. int last_op_number = get_next_op_number(CG(active_op_array));
  606. if (last_op_number > 0) {
  607. opline = &CG(active_op_array)->opcodes[last_op_number-1];
  608. if (opline_is_fetch_this(opline TSRMLS_CC)) {
  609. zend_error(E_COMPILE_ERROR, "Cannot re-assign $this");
  610. }
  611. }
  612. }
  613. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  614. opline->opcode = ZEND_ASSIGN_REF;
  615. if (zend_is_function_or_method_call(rvar)) {
  616. opline->extended_value = ZEND_RETURNS_FUNCTION;
  617. } else if (rvar->u.EA.type & ZEND_PARSED_NEW) {
  618. opline->extended_value = ZEND_RETURNS_NEW;
  619. } else {
  620. opline->extended_value = 0;
  621. }
  622. if (result) {
  623. opline->result.op_type = IS_VAR;
  624. opline->result.u.EA.type = 0;
  625. opline->result.u.var = get_temporary_variable(CG(active_op_array));
  626. *result = opline->result;
  627. } else {
  628. /* SET_UNUSED(opline->result); */
  629. opline->result.u.EA.type |= EXT_TYPE_UNUSED;
  630. }
  631. opline->op1 = *lvar;
  632. opline->op2 = *rvar;
  633. }
  634. /* }}} */
  635. static inline void do_begin_loop(TSRMLS_D) /* {{{ */
  636. {
  637. zend_brk_cont_element *brk_cont_element;
  638. int parent;
  639. parent = CG(active_op_array)->current_brk_cont;
  640. CG(active_op_array)->current_brk_cont = CG(active_op_array)->last_brk_cont;
  641. brk_cont_element = get_next_brk_cont_element(CG(active_op_array));
  642. brk_cont_element->start = get_next_op_number(CG(active_op_array));
  643. brk_cont_element->parent = parent;
  644. }
  645. /* }}} */
  646. static inline void do_end_loop(int cont_addr, int has_loop_var TSRMLS_DC) /* {{{ */
  647. {
  648. if (!has_loop_var) {
  649. /* The start fileld is used to free temporary variables in case of exceptions.
  650. * We won't try to free something of we don't have loop variable.
  651. */
  652. CG(active_op_array)->brk_cont_array[CG(active_op_array)->current_brk_cont].start = -1;
  653. }
  654. CG(active_op_array)->brk_cont_array[CG(active_op_array)->current_brk_cont].cont = cont_addr;
  655. CG(active_op_array)->brk_cont_array[CG(active_op_array)->current_brk_cont].brk = get_next_op_number(CG(active_op_array));
  656. CG(active_op_array)->current_brk_cont = CG(active_op_array)->brk_cont_array[CG(active_op_array)->current_brk_cont].parent;
  657. }
  658. /* }}} */
  659. void zend_do_while_cond(const znode *expr, znode *close_bracket_token TSRMLS_DC) /* {{{ */
  660. {
  661. int while_cond_op_number = get_next_op_number(CG(active_op_array));
  662. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  663. opline->opcode = ZEND_JMPZ;
  664. opline->op1 = *expr;
  665. close_bracket_token->u.opline_num = while_cond_op_number;
  666. SET_UNUSED(opline->op2);
  667. do_begin_loop(TSRMLS_C);
  668. INC_BPC(CG(active_op_array));
  669. }
  670. /* }}} */
  671. void zend_do_while_end(const znode *while_token, const znode *close_bracket_token TSRMLS_DC) /* {{{ */
  672. {
  673. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  674. /* add unconditional jump */
  675. opline->opcode = ZEND_JMP;
  676. opline->op1.u.opline_num = while_token->u.opline_num;
  677. SET_UNUSED(opline->op1);
  678. SET_UNUSED(opline->op2);
  679. /* update while's conditional jmp */
  680. CG(active_op_array)->opcodes[close_bracket_token->u.opline_num].op2.u.opline_num = get_next_op_number(CG(active_op_array));
  681. do_end_loop(while_token->u.opline_num, 0 TSRMLS_CC);
  682. DEC_BPC(CG(active_op_array));
  683. }
  684. /* }}} */
  685. void zend_do_for_cond(const znode *expr, znode *second_semicolon_token TSRMLS_DC) /* {{{ */
  686. {
  687. int for_cond_op_number = get_next_op_number(CG(active_op_array));
  688. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  689. opline->opcode = ZEND_JMPZNZ;
  690. opline->op1 = *expr; /* the conditional expression */
  691. second_semicolon_token->u.opline_num = for_cond_op_number;
  692. SET_UNUSED(opline->op2);
  693. }
  694. /* }}} */
  695. void zend_do_for_before_statement(const znode *cond_start, const znode *second_semicolon_token TSRMLS_DC) /* {{{ */
  696. {
  697. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  698. opline->opcode = ZEND_JMP;
  699. opline->op1.u.opline_num = cond_start->u.opline_num;
  700. CG(active_op_array)->opcodes[second_semicolon_token->u.opline_num].extended_value = get_next_op_number(CG(active_op_array));
  701. SET_UNUSED(opline->op1);
  702. SET_UNUSED(opline->op2);
  703. do_begin_loop(TSRMLS_C);
  704. INC_BPC(CG(active_op_array));
  705. }
  706. /* }}} */
  707. void zend_do_for_end(const znode *second_semicolon_token TSRMLS_DC) /* {{{ */
  708. {
  709. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  710. opline->opcode = ZEND_JMP;
  711. opline->op1.u.opline_num = second_semicolon_token->u.opline_num+1;
  712. CG(active_op_array)->opcodes[second_semicolon_token->u.opline_num].op2.u.opline_num = get_next_op_number(CG(active_op_array));
  713. SET_UNUSED(opline->op1);
  714. SET_UNUSED(opline->op2);
  715. do_end_loop(second_semicolon_token->u.opline_num+1, 0 TSRMLS_CC);
  716. DEC_BPC(CG(active_op_array));
  717. }
  718. /* }}} */
  719. void zend_do_pre_incdec(znode *result, const znode *op1, zend_uchar op TSRMLS_DC) /* {{{ */
  720. {
  721. int last_op_number = get_next_op_number(CG(active_op_array));
  722. zend_op *opline;
  723. if (last_op_number > 0) {
  724. zend_op *last_op = &CG(active_op_array)->opcodes[last_op_number-1];
  725. if (last_op->opcode == ZEND_FETCH_OBJ_RW) {
  726. last_op->opcode = (op==ZEND_PRE_INC)?ZEND_PRE_INC_OBJ:ZEND_PRE_DEC_OBJ;
  727. last_op->result.op_type = IS_VAR;
  728. last_op->result.u.EA.type = 0;
  729. last_op->result.u.var = get_temporary_variable(CG(active_op_array));
  730. *result = last_op->result;
  731. return;
  732. }
  733. }
  734. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  735. opline->opcode = op;
  736. opline->op1 = *op1;
  737. SET_UNUSED(opline->op2);
  738. opline->result.op_type = IS_VAR;
  739. opline->result.u.EA.type = 0;
  740. opline->result.u.var = get_temporary_variable(CG(active_op_array));
  741. *result = opline->result;
  742. }
  743. /* }}} */
  744. void zend_do_post_incdec(znode *result, const znode *op1, zend_uchar op TSRMLS_DC) /* {{{ */
  745. {
  746. int last_op_number = get_next_op_number(CG(active_op_array));
  747. zend_op *opline;
  748. if (last_op_number > 0) {
  749. zend_op *last_op = &CG(active_op_array)->opcodes[last_op_number-1];
  750. if (last_op->opcode == ZEND_FETCH_OBJ_RW) {
  751. last_op->opcode = (op==ZEND_POST_INC)?ZEND_POST_INC_OBJ:ZEND_POST_DEC_OBJ;
  752. last_op->result.op_type = IS_TMP_VAR;
  753. last_op->result.u.var = get_temporary_variable(CG(active_op_array));
  754. *result = last_op->result;
  755. return;
  756. }
  757. }
  758. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  759. opline->opcode = op;
  760. opline->op1 = *op1;
  761. SET_UNUSED(opline->op2);
  762. opline->result.op_type = IS_TMP_VAR;
  763. opline->result.u.var = get_temporary_variable(CG(active_op_array));
  764. *result = opline->result;
  765. }
  766. /* }}} */
  767. void zend_do_if_cond(const znode *cond, znode *closing_bracket_token TSRMLS_DC) /* {{{ */
  768. {
  769. int if_cond_op_number = get_next_op_number(CG(active_op_array));
  770. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  771. opline->opcode = ZEND_JMPZ;
  772. opline->op1 = *cond;
  773. closing_bracket_token->u.opline_num = if_cond_op_number;
  774. SET_UNUSED(opline->op2);
  775. INC_BPC(CG(active_op_array));
  776. }
  777. /* }}} */
  778. void zend_do_if_after_statement(const znode *closing_bracket_token, unsigned char initialize TSRMLS_DC) /* {{{ */
  779. {
  780. int if_end_op_number = get_next_op_number(CG(active_op_array));
  781. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  782. zend_llist *jmp_list_ptr;
  783. opline->opcode = ZEND_JMP;
  784. /* save for backpatching */
  785. if (initialize) {
  786. zend_llist jmp_list;
  787. zend_llist_init(&jmp_list, sizeof(int), NULL, 0);
  788. zend_stack_push(&CG(bp_stack), (void *) &jmp_list, sizeof(zend_llist));
  789. }
  790. zend_stack_top(&CG(bp_stack), (void **) &jmp_list_ptr);
  791. zend_llist_add_element(jmp_list_ptr, &if_end_op_number);
  792. CG(active_op_array)->opcodes[closing_bracket_token->u.opline_num].op2.u.opline_num = if_end_op_number+1;
  793. SET_UNUSED(opline->op1);
  794. SET_UNUSED(opline->op2);
  795. }
  796. /* }}} */
  797. void zend_do_if_end(TSRMLS_D) /* {{{ */
  798. {
  799. int next_op_number = get_next_op_number(CG(active_op_array));
  800. zend_llist *jmp_list_ptr;
  801. zend_llist_element *le;
  802. zend_stack_top(&CG(bp_stack), (void **) &jmp_list_ptr);
  803. for (le=jmp_list_ptr->head; le; le = le->next) {
  804. CG(active_op_array)->opcodes[*((int *) le->data)].op1.u.opline_num = next_op_number;
  805. }
  806. zend_llist_destroy(jmp_list_ptr);
  807. zend_stack_del_top(&CG(bp_stack));
  808. DEC_BPC(CG(active_op_array));
  809. }
  810. /* }}} */
  811. void zend_check_writable_variable(const znode *variable) /* {{{ */
  812. {
  813. zend_uint type = variable->u.EA.type;
  814. if (type & ZEND_PARSED_METHOD_CALL) {
  815. zend_error(E_COMPILE_ERROR, "Can't use method return value in write context");
  816. }
  817. if (type == ZEND_PARSED_FUNCTION_CALL) {
  818. zend_error(E_COMPILE_ERROR, "Can't use function return value in write context");
  819. }
  820. }
  821. /* }}} */
  822. void zend_do_begin_variable_parse(TSRMLS_D) /* {{{ */
  823. {
  824. zend_llist fetch_list;
  825. zend_llist_init(&fetch_list, sizeof(zend_op), NULL, 0);
  826. zend_stack_push(&CG(bp_stack), (void *) &fetch_list, sizeof(zend_llist));
  827. }
  828. /* }}} */
  829. void zend_do_end_variable_parse(znode *variable, int type, int arg_offset TSRMLS_DC) /* {{{ */
  830. {
  831. zend_llist *fetch_list_ptr;
  832. zend_llist_element *le;
  833. zend_op *opline = NULL;
  834. zend_op *opline_ptr;
  835. zend_uint this_var = -1;
  836. zend_stack_top(&CG(bp_stack), (void **) &fetch_list_ptr);
  837. le = fetch_list_ptr->head;
  838. /* TODO: $foo->x->y->z = 1 should fetch "x" and "y" for R or RW, not just W */
  839. if (le) {
  840. opline_ptr = (zend_op *)le->data;
  841. if (opline_is_fetch_this(opline_ptr TSRMLS_CC)) {
  842. /* convert to FETCH_?(this) into IS_CV */
  843. if (CG(active_op_array)->last == 0 ||
  844. CG(active_op_array)->opcodes[CG(active_op_array)->last-1].opcode != ZEND_BEGIN_SILENCE) {
  845. this_var = opline_ptr->result.u.var;
  846. if (CG(active_op_array)->this_var == -1) {
  847. CG(active_op_array)->this_var = lookup_cv(CG(active_op_array), Z_STRVAL(opline_ptr->op1.u.constant), Z_STRLEN(opline_ptr->op1.u.constant));
  848. } else {
  849. efree(Z_STRVAL(opline_ptr->op1.u.constant));
  850. }
  851. le = le->next;
  852. if (variable->op_type == IS_VAR &&
  853. variable->u.var == this_var) {
  854. variable->op_type = IS_CV;
  855. variable->u.var = CG(active_op_array)->this_var;
  856. }
  857. } else if (CG(active_op_array)->this_var == -1) {
  858. CG(active_op_array)->this_var = lookup_cv(CG(active_op_array), estrndup("this", sizeof("this")-1), sizeof("this")-1);
  859. }
  860. }
  861. while (le) {
  862. opline_ptr = (zend_op *)le->data;
  863. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  864. memcpy(opline, opline_ptr, sizeof(zend_op));
  865. if (opline->op1.op_type == IS_VAR &&
  866. opline->op1.u.var == this_var) {
  867. opline->op1.op_type = IS_CV;
  868. opline->op1.u.var = CG(active_op_array)->this_var;
  869. }
  870. switch (type) {
  871. case BP_VAR_R:
  872. if (opline->opcode == ZEND_FETCH_DIM_W && opline->op2.op_type == IS_UNUSED) {
  873. zend_error(E_COMPILE_ERROR, "Cannot use [] for reading");
  874. }
  875. opline->opcode -= 3;
  876. break;
  877. case BP_VAR_W:
  878. break;
  879. case BP_VAR_RW:
  880. opline->opcode += 3;
  881. break;
  882. case BP_VAR_IS:
  883. if (opline->opcode == ZEND_FETCH_DIM_W && opline->op2.op_type == IS_UNUSED) {
  884. zend_error(E_COMPILE_ERROR, "Cannot use [] for reading");
  885. }
  886. opline->opcode += 6; /* 3+3 */
  887. break;
  888. case BP_VAR_FUNC_ARG:
  889. opline->opcode += 9; /* 3+3+3 */
  890. opline->extended_value = arg_offset;
  891. break;
  892. case BP_VAR_UNSET:
  893. if (opline->opcode == ZEND_FETCH_DIM_W && opline->op2.op_type == IS_UNUSED) {
  894. zend_error(E_COMPILE_ERROR, "Cannot use [] for unsetting");
  895. }
  896. opline->opcode += 12; /* 3+3+3+3 */
  897. break;
  898. }
  899. le = le->next;
  900. }
  901. if (opline && type == BP_VAR_W && arg_offset) {
  902. opline->extended_value = ZEND_FETCH_MAKE_REF;
  903. }
  904. }
  905. zend_llist_destroy(fetch_list_ptr);
  906. zend_stack_del_top(&CG(bp_stack));
  907. }
  908. /* }}} */
  909. void zend_do_add_string(znode *result, const znode *op1, znode *op2 TSRMLS_DC) /* {{{ */
  910. {
  911. zend_op *opline;
  912. if (Z_STRLEN(op2->u.constant) > 1) {
  913. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  914. opline->opcode = ZEND_ADD_STRING;
  915. } else if (Z_STRLEN(op2->u.constant) == 1) {
  916. int ch = *Z_STRVAL(op2->u.constant);
  917. /* Free memory and use ZEND_ADD_CHAR in case of 1 character strings */
  918. efree(Z_STRVAL(op2->u.constant));
  919. ZVAL_LONG(&op2->u.constant, ch);
  920. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  921. opline->opcode = ZEND_ADD_CHAR;
  922. } else { /* String can be empty after a variable at the end of a heredoc */
  923. efree(Z_STRVAL(op2->u.constant));
  924. return;
  925. }
  926. if (op1) {
  927. opline->op1 = *op1;
  928. opline->result = *op1;
  929. } else {
  930. SET_UNUSED(opline->op1);
  931. opline->result.op_type = IS_TMP_VAR;
  932. opline->result.u.var = get_temporary_variable(CG(active_op_array));
  933. }
  934. opline->op2 = *op2;
  935. *result = opline->result;
  936. }
  937. /* }}} */
  938. void zend_do_add_variable(znode *result, const znode *op1, const znode *op2 TSRMLS_DC) /* {{{ */
  939. {
  940. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  941. opline->opcode = ZEND_ADD_VAR;
  942. if (op1) {
  943. opline->op1 = *op1;
  944. opline->result = *op1;
  945. } else {
  946. SET_UNUSED(opline->op1);
  947. opline->result.op_type = IS_TMP_VAR;
  948. opline->result.u.var = get_temporary_variable(CG(active_op_array));
  949. }
  950. opline->op2 = *op2;
  951. *result = opline->result;
  952. }
  953. /* }}} */
  954. void zend_do_free(znode *op1 TSRMLS_DC) /* {{{ */
  955. {
  956. if (op1->op_type==IS_TMP_VAR) {
  957. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  958. opline->opcode = ZEND_FREE;
  959. opline->op1 = *op1;
  960. SET_UNUSED(opline->op2);
  961. } else if (op1->op_type==IS_VAR) {
  962. zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
  963. while (opline->opcode == ZEND_END_SILENCE || opline->opcode == ZEND_EXT_FCALL_END || opline->opcode == ZEND_OP_DATA) {
  964. opline--;
  965. }
  966. if (opline->result.op_type == IS_VAR
  967. && opline->result.u.var == op1->u.var) {
  968. opline->result.u.EA.type |= EXT_TYPE_UNUSED;
  969. } else {
  970. while (opline>CG(active_op_array)->opcodes) {
  971. if (opline->opcode == ZEND_FETCH_DIM_R
  972. && opline->op1.op_type == IS_VAR
  973. && opline->op1.u.var == op1->u.var) {
  974. /* This should the end of a list() construct
  975. * Mark its result as unused
  976. */
  977. opline->extended_value = ZEND_FETCH_STANDARD;
  978. break;
  979. } else if (opline->result.op_type==IS_VAR
  980. && opline->result.u.var == op1->u.var) {
  981. if (opline->opcode == ZEND_NEW) {
  982. opline->result.u.EA.type |= EXT_TYPE_UNUSED;
  983. }
  984. break;
  985. }
  986. opline--;
  987. }
  988. }
  989. } else if (op1->op_type == IS_CONST) {
  990. zval_dtor(&op1->u.constant);
  991. }
  992. }
  993. /* }}} */
  994. int zend_do_verify_access_types(const znode *current_access_type, const znode *new_modifier) /* {{{ */
  995. {
  996. if ((Z_LVAL(current_access_type->u.constant) & ZEND_ACC_PPP_MASK)
  997. && (Z_LVAL(new_modifier->u.constant) & ZEND_ACC_PPP_MASK)) {
  998. zend_error(E_COMPILE_ERROR, "Multiple access type modifiers are not allowed");
  999. }
  1000. if ((Z_LVAL(current_access_type->u.constant) & ZEND_ACC_ABSTRACT)
  1001. && (Z_LVAL(new_modifier->u.constant) & ZEND_ACC_ABSTRACT)) {
  1002. zend_error(E_COMPILE_ERROR, "Multiple abstract modifiers are not allowed");
  1003. }
  1004. if ((Z_LVAL(current_access_type->u.constant) & ZEND_ACC_STATIC)
  1005. && (Z_LVAL(new_modifier->u.constant) & ZEND_ACC_STATIC)) {
  1006. zend_error(E_COMPILE_ERROR, "Multiple static modifiers are not allowed");
  1007. }
  1008. if ((Z_LVAL(current_access_type->u.constant) & ZEND_ACC_FINAL)
  1009. && (Z_LVAL(new_modifier->u.constant) & ZEND_ACC_FINAL)) {
  1010. zend_error(E_COMPILE_ERROR, "Multiple final modifiers are not allowed");
  1011. }
  1012. if (((Z_LVAL(current_access_type->u.constant) | Z_LVAL(new_modifier->u.constant)) & (ZEND_ACC_ABSTRACT | ZEND_ACC_FINAL)) == (ZEND_ACC_ABSTRACT | ZEND_ACC_FINAL)) {
  1013. zend_error(E_COMPILE_ERROR, "Cannot use the final modifier on an abstract class member");
  1014. }
  1015. return (Z_LVAL(current_access_type->u.constant) | Z_LVAL(new_modifier->u.constant));
  1016. }
  1017. /* }}} */
  1018. void zend_do_begin_function_declaration(znode *function_token, znode *function_name, int is_method, int return_reference, znode *fn_flags_znode TSRMLS_DC) /* {{{ */
  1019. {
  1020. zend_op_array op_array;
  1021. char *name = function_name->u.constant.value.str.val;
  1022. int name_len = function_name->u.constant.value.str.len;
  1023. int function_begin_line = function_token->u.opline_num;
  1024. zend_uint fn_flags;
  1025. char *lcname;
  1026. zend_bool orig_interactive;
  1027. ALLOCA_FLAG(use_heap)
  1028. if (is_method) {
  1029. if (CG(active_class_entry)->ce_flags & ZEND_ACC_INTERFACE) {
  1030. if ((Z_LVAL(fn_flags_znode->u.constant) & ~(ZEND_ACC_STATIC|ZEND_ACC_PUBLIC))) {
  1031. zend_error(E_COMPILE_ERROR, "Access type for interface method %s::%s() must be omitted", CG(active_class_entry)->name, function_name->u.constant.value.str.val);
  1032. }
  1033. Z_LVAL(fn_flags_znode->u.constant) |= ZEND_ACC_ABSTRACT; /* propagates to the rest of the parser */
  1034. }
  1035. fn_flags = Z_LVAL(fn_flags_znode->u.constant); /* must be done *after* the above check */
  1036. } else {
  1037. fn_flags = 0;
  1038. }
  1039. if ((fn_flags & ZEND_ACC_STATIC) && (fn_flags & ZEND_ACC_ABSTRACT) && !(CG(active_class_entry)->ce_flags & ZEND_ACC_INTERFACE)) {
  1040. zend_error(E_STRICT, "Static function %s%s%s() should not be abstract", is_method ? CG(active_class_entry)->name : "", is_method ? "::" : "", Z_STRVAL(function_name->u.constant));
  1041. }
  1042. function_token->u.op_array = CG(active_op_array);
  1043. lcname = zend_str_tolower_dup(name, name_len);
  1044. orig_interactive = CG(interactive);
  1045. CG(interactive) = 0;
  1046. init_op_array(&op_array, ZEND_USER_FUNCTION, INITIAL_OP_ARRAY_SIZE TSRMLS_CC);
  1047. CG(interactive) = orig_interactive;
  1048. op_array.function_name = name;
  1049. op_array.return_reference = return_reference;
  1050. op_array.fn_flags |= fn_flags;
  1051. op_array.pass_rest_by_reference = 0;
  1052. op_array.scope = is_method?CG(active_class_entry):NULL;
  1053. op_array.prototype = NULL;
  1054. op_array.line_start = zend_get_compiled_lineno(TSRMLS_C);
  1055. if (is_method) {
  1056. if (zend_hash_add(&CG(active_class_entry)->function_table, lcname, name_len+1, &op_array, sizeof(zend_op_array), (void **) &CG(active_op_array)) == FAILURE) {
  1057. zend_error(E_COMPILE_ERROR, "Cannot redeclare %s::%s()", CG(active_class_entry)->name, name);
  1058. }
  1059. if (fn_flags & ZEND_ACC_ABSTRACT) {
  1060. CG(active_class_entry)->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
  1061. }
  1062. if (!(fn_flags & ZEND_ACC_PPP_MASK)) {
  1063. fn_flags |= ZEND_ACC_PUBLIC;
  1064. }
  1065. if (CG(active_class_entry)->ce_flags & ZEND_ACC_INTERFACE) {
  1066. if ((name_len == sizeof(ZEND_CALL_FUNC_NAME)-1) && (!memcmp(lcname, ZEND_CALL_FUNC_NAME, sizeof(ZEND_CALL_FUNC_NAME)-1))) {
  1067. if (fn_flags & ((ZEND_ACC_PPP_MASK | ZEND_ACC_STATIC) ^ ZEND_ACC_PUBLIC)) {
  1068. zend_error(E_WARNING, "The magic method __call() must have public visibility and cannot be static");
  1069. }
  1070. } else if ((name_len == sizeof(ZEND_CALLSTATIC_FUNC_NAME)-1) && (!memcmp(lcname, ZEND_CALLSTATIC_FUNC_NAME, sizeof(ZEND_CALLSTATIC_FUNC_NAME)-1))) {
  1071. if ((fn_flags & (ZEND_ACC_PPP_MASK ^ ZEND_ACC_PUBLIC)) || (fn_flags & ZEND_ACC_STATIC) == 0) {
  1072. zend_error(E_WARNING, "The magic method __callStatic() must have public visibility and be static");
  1073. }
  1074. } else if ((name_len == sizeof(ZEND_GET_FUNC_NAME)-1) && (!memcmp(lcname, ZEND_GET_FUNC_NAME, sizeof(ZEND_GET_FUNC_NAME)-1))) {
  1075. if (fn_flags & ((ZEND_ACC_PPP_MASK | ZEND_ACC_STATIC) ^ ZEND_ACC_PUBLIC)) {
  1076. zend_error(E_WARNING, "The magic method __get() must have public visibility and cannot be static");
  1077. }
  1078. } else if ((name_len == sizeof(ZEND_SET_FUNC_NAME)-1) && (!memcmp(lcname, ZEND_SET_FUNC_NAME, sizeof(ZEND_SET_FUNC_NAME)-1))) {
  1079. if (fn_flags & ((ZEND_ACC_PPP_MASK | ZEND_ACC_STATIC) ^ ZEND_ACC_PUBLIC)) {
  1080. zend_error(E_WARNING, "The magic method __set() must have public visibility and cannot be static");
  1081. }
  1082. } else if ((name_len == sizeof(ZEND_UNSET_FUNC_NAME)-1) && (!memcmp(lcname, ZEND_UNSET_FUNC_NAME, sizeof(ZEND_UNSET_FUNC_NAME)-1))) {
  1083. if (fn_flags & ((ZEND_ACC_PPP_MASK | ZEND_ACC_STATIC) ^ ZEND_ACC_PUBLIC)) {
  1084. zend_error(E_WARNING, "The magic method __unset() must have public visibility and cannot be static");
  1085. }
  1086. } else if ((name_len == sizeof(ZEND_ISSET_FUNC_NAME)-1) && (!memcmp(lcname, ZEND_ISSET_FUNC_NAME, sizeof(ZEND_ISSET_FUNC_NAME)-1))) {
  1087. if (fn_flags & ((ZEND_ACC_PPP_MASK | ZEND_ACC_STATIC) ^ ZEND_ACC_PUBLIC)) {
  1088. zend_error(E_WARNING, "The magic method __isset() must have public visibility and cannot be static");
  1089. }
  1090. } else if ((name_len == sizeof(ZEND_TOSTRING_FUNC_NAME)-1) && (!memcmp(lcname, ZEND_TOSTRING_FUNC_NAME, sizeof(ZEND_TOSTRING_FUNC_NAME)-1))) {
  1091. if (fn_flags & ((ZEND_ACC_PPP_MASK | ZEND_ACC_STATIC) ^ ZEND_ACC_PUBLIC)) {
  1092. zend_error(E_WARNING, "The magic method __toString() must have public visibility and cannot be static");
  1093. }
  1094. }
  1095. } else {
  1096. char *class_lcname;
  1097. class_lcname = do_alloca(CG(active_class_entry)->name_length + 1, use_heap);
  1098. zend_str_tolower_copy(class_lcname, CG(active_class_entry)->name, CG(active_class_entry)->name_length);
  1099. /* Improve after RC: cache the lowercase class name */
  1100. if ((CG(active_class_entry)->name_length == name_len) && (!memcmp(class_lcname, lcname, name_len))) {
  1101. if (!CG(active_class_entry)->constructor) {
  1102. CG(active_class_entry)->constructor = (zend_function *) CG(active_op_array);
  1103. }
  1104. } else if ((name_len == sizeof(ZEND_CONSTRUCTOR_FUNC_NAME)-1) && (!memcmp(lcname, ZEND_CONSTRUCTOR_FUNC_NAME, sizeof(ZEND_CONSTRUCTOR_FUNC_NAME)))) {
  1105. if (CG(active_class_entry)->constructor) {
  1106. zend_error(E_STRICT, "Redefining already defined constructor for class %s", CG(active_class_entry)->name);
  1107. }
  1108. CG(active_class_entry)->constructor = (zend_function *) CG(active_op_array);
  1109. } else if ((name_len == sizeof(ZEND_DESTRUCTOR_FUNC_NAME)-1) && (!memcmp(lcname, ZEND_DESTRUCTOR_FUNC_NAME, sizeof(ZEND_DESTRUCTOR_FUNC_NAME)-1))) {
  1110. CG(active_class_entry)->destructor = (zend_function *) CG(active_op_array);
  1111. } else if ((name_len == sizeof(ZEND_CLONE_FUNC_NAME)-1) && (!memcmp(lcname, ZEND_CLONE_FUNC_NAME, sizeof(ZEND_CLONE_FUNC_NAME)-1))) {
  1112. CG(active_class_entry)->clone = (zend_function *) CG(active_op_array);
  1113. } else if ((name_len == sizeof(ZEND_CALL_FUNC_NAME)-1) && (!memcmp(lcname, ZEND_CALL_FUNC_NAME, sizeof(ZEND_CALL_FUNC_NAME)-1))) {
  1114. if (fn_flags & ((ZEND_ACC_PPP_MASK | ZEND_ACC_STATIC) ^ ZEND_ACC_PUBLIC)) {
  1115. zend_error(E_WARNING, "The magic method __call() must have public visibility and cannot be static");
  1116. }
  1117. CG(active_class_entry)->__call = (zend_function *) CG(active_op_array);
  1118. } else if ((name_len == sizeof(ZEND_CALLSTATIC_FUNC_NAME)-1) && (!memcmp(lcname, ZEND_CALLSTATIC_FUNC_NAME, sizeof(ZEND_CALLSTATIC_FUNC_NAME)-1))) {
  1119. if ((fn_flags & (ZEND_ACC_PPP_MASK ^ ZEND_ACC_PUBLIC)) || (fn_flags & ZEND_ACC_STATIC) == 0) {
  1120. zend_error(E_WARNING, "The magic method __callStatic() must have public visibility and be static");
  1121. }
  1122. CG(active_class_entry)->__callstatic = (zend_function *) CG(active_op_array);
  1123. } else if ((name_len == sizeof(ZEND_GET_FUNC_NAME)-1) && (!memcmp(lcname, ZEND_GET_FUNC_NAME, sizeof(ZEND_GET_FUNC_NAME)-1))) {
  1124. if (fn_flags & ((ZEND_ACC_PPP_MASK | ZEND_ACC_STATIC) ^ ZEND_ACC_PUBLIC)) {
  1125. zend_error(E_WARNING, "The magic method __get() must have public visibility and cannot be static");
  1126. }
  1127. CG(active_class_entry)->__get = (zend_function *) CG(active_op_array);
  1128. } else if ((name_len == sizeof(ZEND_SET_FUNC_NAME)-1) && (!memcmp(lcname, ZEND_SET_FUNC_NAME, sizeof(ZEND_SET_FUNC_NAME)-1))) {
  1129. if (fn_flags & ((ZEND_ACC_PPP_MASK | ZEND_ACC_STATIC) ^ ZEND_ACC_PUBLIC)) {
  1130. zend_error(E_WARNING, "The magic method __set() must have public visibility and cannot be static");
  1131. }
  1132. CG(active_class_entry)->__set = (zend_function *) CG(active_op_array);
  1133. } else if ((name_len == sizeof(ZEND_UNSET_FUNC_NAME)-1) && (!memcmp(lcname, ZEND_UNSET_FUNC_NAME, sizeof(ZEND_UNSET_FUNC_NAME)-1))) {
  1134. if (fn_flags & ((ZEND_ACC_PPP_MASK | ZEND_ACC_STATIC) ^ ZEND_ACC_PUBLIC)) {
  1135. zend_error(E_WARNING, "The magic method __unset() must have public visibility and cannot be static");
  1136. }
  1137. CG(active_class_entry)->__unset = (zend_function *) CG(active_op_array);
  1138. } else if ((name_len == sizeof(ZEND_ISSET_FUNC_NAME)-1) && (!memcmp(lcname, ZEND_ISSET_FUNC_NAME, sizeof(ZEND_ISSET_FUNC_NAME)-1))) {
  1139. if (fn_flags & ((ZEND_ACC_PPP_MASK | ZEND_ACC_STATIC) ^ ZEND_ACC_PUBLIC)) {
  1140. zend_error(E_WARNING, "The magic method __isset() must have public visibility and cannot be static");
  1141. }
  1142. CG(active_class_entry)->__isset = (zend_function *) CG(active_op_array);
  1143. } else if ((name_len == sizeof(ZEND_TOSTRING_FUNC_NAME)-1) && (!memcmp(lcname, ZEND_TOSTRING_FUNC_NAME, sizeof(ZEND_TOSTRING_FUNC_NAME)-1))) {
  1144. if (fn_flags & ((ZEND_ACC_PPP_MASK | ZEND_ACC_STATIC) ^ ZEND_ACC_PUBLIC)) {
  1145. zend_error(E_WARNING, "The magic method __toString() must have public visibility and cannot be static");
  1146. }
  1147. CG(active_class_entry)->__tostring = (zend_function *) CG(active_op_array);
  1148. } else if (!(fn_flags & ZEND_ACC_STATIC)) {
  1149. CG(active_op_array)->fn_flags |= ZEND_ACC_ALLOW_STATIC;
  1150. }
  1151. free_alloca(class_lcname, use_heap);
  1152. }
  1153. efree(lcname);
  1154. } else {
  1155. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  1156. if (CG(current_namespace)) {
  1157. /* Prefix function name with current namespcae name */
  1158. znode tmp;
  1159. tmp.u.constant = *CG(current_namespace);
  1160. zval_copy_ctor(&tmp.u.constant);
  1161. zend_do_build_namespace_name(&tmp, &tmp, function_name TSRMLS_CC);
  1162. op_array.function_name = Z_STRVAL(tmp.u.constant);
  1163. efree(lcname);
  1164. name_len = Z_STRLEN(tmp.u.constant);
  1165. lcname = zend_str_tolower_dup(Z_STRVAL(tmp.u.constant), name_len);
  1166. }
  1167. opline->opcode = ZEND_DECLARE_FUNCTION;
  1168. opline->op1.op_type = IS_CONST;
  1169. build_runtime_defined_function_key(&opline->op1.u.constant, lcname, name_len TSRMLS_CC);
  1170. opline->op2.op_type = IS_CONST;
  1171. opline->op2.u.constant.type = IS_STRING;
  1172. opline->op2.u.constant.value.str.val = lcname;
  1173. opline->op2.u.constant.value.str.len = name_len;
  1174. Z_SET_REFCOUNT(opline->op2.u.constant, 1);
  1175. opline->extended_value = ZEND_DECLARE_FUNCTION;
  1176. zend_hash_update(CG(function_table), opline->op1.u.constant.value.str.val, opline->op1.u.constant.value.str.len, &op_array, sizeof(zend_op_array), (void **) &CG(active_op_array));
  1177. }
  1178. if (CG(compiler_options) & ZEND_COMPILE_EXTENDED_INFO) {
  1179. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  1180. opline->opcode = ZEND_EXT_NOP;
  1181. opline->lineno = function_begin_line;
  1182. SET_UNUSED(opline->op1);
  1183. SET_UNUSED(opline->op2);
  1184. }
  1185. {
  1186. /* Push a seperator to the switch and foreach stacks */
  1187. zend_switch_entry switch_entry;
  1188. switch_entry.cond.op_type = IS_UNUSED;
  1189. switch_entry.default_case = 0;
  1190. switch_entry.control_var = 0;
  1191. zend_stack_push(&CG(switch_cond_stack), (void *) &switch_entry, sizeof(switch_entry));
  1192. {
  1193. /* Foreach stack separator */
  1194. zend_op dummy_opline;
  1195. dummy_opline.result.op_type = IS_UNUSED;
  1196. dummy_opline.op1.op_type = IS_UNUSED;
  1197. zend_stack_push(&CG(foreach_copy_stack), (void *) &dummy_opline, sizeof(zend_op));
  1198. }
  1199. }
  1200. if (CG(doc_comment)) {
  1201. CG(active_op_array)->doc_comment = CG(doc_comment);
  1202. CG(active_op_array)->doc_comment_len = CG(doc_comment_len);
  1203. CG(doc_comment) = NULL;
  1204. CG(doc_comment_len) = 0;
  1205. }
  1206. zend_stack_push(&CG(labels_stack), (void *) &CG(labels), sizeof(HashTable*));
  1207. CG(labels) = NULL;
  1208. }
  1209. /* }}} */
  1210. void zend_do_begin_lambda_function_declaration(znode *result, znode *function_token, int return_reference TSRMLS_DC) /* {{{ */
  1211. {
  1212. znode function_name;
  1213. zend_op_array *current_op_array = CG(active_op_array);
  1214. int current_op_number = get_next_op_number(CG(active_op_array));
  1215. zend_op *current_op;
  1216. function_name.op_type = IS_CONST;
  1217. ZVAL_STRINGL(&function_name.u.constant, "{closure}", sizeof("{closure}")-1, 1);
  1218. zend_do_begin_function_declaration(function_token, &function_name, 0, return_reference, NULL TSRMLS_CC);
  1219. result->op_type = IS_TMP_VAR;
  1220. result->u.var = get_temporary_variable(current_op_array);
  1221. current_op = &current_op_array->opcodes[current_op_number];
  1222. current_op->opcode = ZEND_DECLARE_LAMBDA_FUNCTION;
  1223. zval_dtor(&current_op->op2.u.constant);
  1224. ZVAL_LONG(&current_op->op2.u.constant, zend_hash_func(Z_STRVAL(current_op->op1.u.constant), Z_STRLEN(current_op->op1.u.constant)));
  1225. current_op->result = *result;
  1226. CG(active_op_array)->fn_flags |= ZEND_ACC_CLOSURE;
  1227. }
  1228. /* }}} */
  1229. void zend_do_handle_exception(TSRMLS_D) /* {{{ */
  1230. {
  1231. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  1232. opline->opcode = ZEND_HANDLE_EXCEPTION;
  1233. SET_UNUSED(opline->op1);
  1234. SET_UNUSED(opline->op2);
  1235. }
  1236. /* }}} */
  1237. void zend_do_end_function_declaration(const znode *function_token TSRMLS_DC) /* {{{ */
  1238. {
  1239. char lcname[16];
  1240. int name_len;
  1241. zend_do_extended_info(TSRMLS_C);
  1242. zend_do_return(NULL, 0 TSRMLS_CC);
  1243. pass_two(CG(active_op_array) TSRMLS_CC);
  1244. zend_release_labels(TSRMLS_C);
  1245. if (CG(active_class_entry)) {
  1246. zend_check_magic_method_implementation(CG(active_class_entry), (zend_function*)CG(active_op_array), E_COMPILE_ERROR TSRMLS_CC);
  1247. } else {
  1248. /* we don't care if the function name is longer, in fact lowercasing only
  1249. * the beginning of the name speeds up the check process */
  1250. name_len = strlen(CG(active_op_array)->function_name);
  1251. zend_str_tolower_copy(lcname, CG(active_op_array)->function_name, MIN(name_len, sizeof(lcname)-1));
  1252. lcname[sizeof(lcname)-1] = '\0'; /* zend_str_tolower_copy won't necessarily set the zero byte */
  1253. if (name_len == sizeof(ZEND_AUTOLOAD_FUNC_NAME) - 1 && !memcmp(lcname, ZEND_AUTOLOAD_FUNC_NAME, sizeof(ZEND_AUTOLOAD_FUNC_NAME)) && CG(active_op_array)->num_args != 1) {
  1254. zend_error(E_COMPILE_ERROR, "%s() must take exactly 1 argument", ZEND_AUTOLOAD_FUNC_NAME);
  1255. }
  1256. }
  1257. CG(active_op_array)->line_end = zend_get_compiled_lineno(TSRMLS_C);
  1258. CG(active_op_array) = function_token->u.op_array;
  1259. /* Pop the switch and foreach seperators */
  1260. zend_stack_del_top(&CG(switch_cond_stack));
  1261. zend_stack_del_top(&CG(foreach_copy_stack));
  1262. }
  1263. /* }}} */
  1264. void zend_do_receive_arg(zend_uchar op, const znode *var, const znode *offset, const znode *initialization, znode *class_type, const znode *varname, zend_uchar pass_by_reference TSRMLS_DC) /* {{{ */
  1265. {
  1266. zend_op *opline;
  1267. zend_arg_info *cur_arg_info;
  1268. if (class_type->op_type == IS_CONST &&
  1269. Z_TYPE(class_type->u.constant) == IS_STRING &&
  1270. Z_STRLEN(class_type->u.constant) == 0) {
  1271. /* Usage of namespace as class name not in namespace */
  1272. zval_dtor(&class_type->u.constant);
  1273. zend_error(E_COMPILE_ERROR, "Cannot use 'namespace' as a class name");
  1274. return;
  1275. }
  1276. if (var->op_type == IS_CV &&
  1277. var->u.var == CG(active_op_array)->this_var &&
  1278. (CG(active_op_array)->fn_flags & ZEND_ACC_STATIC) == 0) {
  1279. zend_error(E_COMPILE_ERROR, "Cannot re-assign $this");
  1280. } else if (var->op_type == IS_VAR &&
  1281. CG(active_op_array)->scope &&
  1282. ((CG(active_op_array)->fn_flags & ZEND_ACC_STATIC) == 0) &&
  1283. (Z_TYPE(varname->u.constant) == IS_STRING) &&
  1284. (Z_STRLEN(varname->u.constant) == sizeof("this")-1) &&
  1285. (memcmp(Z_STRVAL(varname->u.constant), "this", sizeof("this")) == 0)) {
  1286. zend_error(E_COMPILE_ERROR, "Cannot re-assign $this");
  1287. }
  1288. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  1289. CG(active_op_array)->num_args++;
  1290. opline->opcode = op;
  1291. opline->result = *var;
  1292. opline->op1 = *offset;
  1293. if (op == ZEND_RECV_INIT) {
  1294. opline->op2 = *initialization;
  1295. } else {
  1296. CG(active_op_array)->required_num_args = CG(active_op_array)->num_args;
  1297. SET_UNUSED(opline->op2);
  1298. }
  1299. CG(active_op_array)->arg_info = erealloc(CG(active_op_array)->arg_info, sizeof(zend_arg_info)*(CG(active_op_array)->num_args));
  1300. cur_arg_info = &CG(active_op_array)->arg_info[CG(active_op_array)->num_args-1];
  1301. cur_arg_info->name = estrndup(varname->u.constant.value.str.val, varname->u.constant.value.str.len);
  1302. cur_arg_info->name_len = varname->u.constant.value.str.len;
  1303. cur_arg_info->array_type_hint = 0;
  1304. cur_arg_info->allow_null = 1;
  1305. cur_arg_info->pass_by_reference = pass_by_reference;
  1306. cur_arg_info->class_name = NULL;
  1307. cur_arg_info->class_name_len = 0;
  1308. if (class_type->op_type != IS_UNUSED) {
  1309. cur_arg_info->allow_null = 0;
  1310. if (class_type->u.constant.type == IS_STRING) {
  1311. if (ZEND_FETCH_CLASS_DEFAULT == zend_get_class_fetch_type(Z_STRVAL(class_type->u.constant), Z_STRLEN(class_type->u.constant))) {
  1312. zend_resolve_class_name(class_type, &opline->extended_value, 1 TSRMLS_CC);
  1313. }
  1314. cur_arg_info->class_name = class_type->u.constant.value.str.val;
  1315. cur_arg_info->class_name_len = class_type->u.constant.value.str.len;
  1316. if (op == ZEND_RECV_INIT) {
  1317. if (Z_TYPE(initialization->u.constant) == IS_NULL || (Z_TYPE(initialization->u.constant) == IS_CONSTANT && !strcasecmp(Z_STRVAL(initialization->u.constant), "NULL"))) {
  1318. cur_arg_info->allow_null = 1;
  1319. } else {
  1320. zend_error(E_COMPILE_ERROR, "Default value for parameters with a class type hint can only be NULL");
  1321. }
  1322. }
  1323. } else {
  1324. cur_arg_info->array_type_hint = 1;
  1325. cur_arg_info->class_name = NULL;
  1326. cur_arg_info->class_name_len = 0;
  1327. if (op == ZEND_RECV_INIT) {
  1328. if (Z_TYPE(initialization->u.constant) == IS_NULL || (Z_TYPE(initialization->u.constant) == IS_CONSTANT && !strcasecmp(Z_STRVAL(initialization->u.constant), "NULL"))) {
  1329. cur_arg_info->allow_null = 1;
  1330. } else if (Z_TYPE(initialization->u.constant) != IS_ARRAY && Z_TYPE(initialization->u.constant) != IS_CONSTANT_ARRAY) {
  1331. zend_error(E_COMPILE_ERROR, "Default value for parameters with array type hint can only be an array or NULL");
  1332. }
  1333. }
  1334. }
  1335. }
  1336. opline->result.u.EA.type |= EXT_TYPE_UNUSED;
  1337. }
  1338. /* }}} */
  1339. int zend_do_begin_function_call(znode *function_name, zend_bool check_namespace TSRMLS_DC) /* {{{ */
  1340. {
  1341. zend_function *function;
  1342. char *lcname;
  1343. char *is_compound = memchr(Z_STRVAL(function_name->u.constant), '\\', Z_STRLEN(function_name->u.constant));
  1344. zend_resolve_non_class_name(function_name, check_namespace TSRMLS_CC);
  1345. if (check_namespace && CG(current_namespace) && !is_compound) {
  1346. /* We assume we call function from the current namespace
  1347. if it is not prefixed. */
  1348. /* In run-time PHP will check for function with full name and
  1349. internal function with short name */
  1350. zend_do_begin_dynamic_function_call(function_name, 1 TSRMLS_CC);
  1351. return 1;
  1352. }
  1353. lcname = zend_str_tolower_dup(function_name->u.constant.value.str.val, function_name->u.constant.value.str.len);
  1354. if ((zend_hash_find(CG(function_table), lcname, function_name->u.constant.value.str.len+1, (void **) &function)==FAILURE) ||
  1355. ((CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS) &&
  1356. (function->type == ZEND_INTERNAL_FUNCTION))) {
  1357. zend_do_begin_dynamic_function_call(function_name, 0 TSRMLS_CC);
  1358. efree(lcname);
  1359. return 1; /* Dynamic */
  1360. }
  1361. efree(function_name->u.constant.value.str.val);
  1362. function_name->u.constant.value.str.val = lcname;
  1363. zend_stack_push(&CG(function_call_stack), (void *) &function, sizeof(zend_function *));
  1364. zend_do_extended_fcall_begin(TSRMLS_C);
  1365. return 0;
  1366. }
  1367. /* }}} */
  1368. void zend_do_begin_method_call(znode *left_bracket TSRMLS_DC) /* {{{ */
  1369. {
  1370. zend_op *last_op;
  1371. int last_op_number;
  1372. unsigned char *ptr = NULL;
  1373. zend_do_end_variable_parse(left_bracket, BP_VAR_R, 0 TSRMLS_CC);
  1374. zend_do_begin_variable_parse(TSRMLS_C);
  1375. last_op_number = get_next_op_number(CG(active_op_array))-1;
  1376. last_op = &CG(active_op_array)->opcodes[last_op_number];
  1377. if ((last_op->op2.op_type == IS_CONST) && (last_op->op2.u.constant.type == IS_STRING) && (last_op->op2.u.constant.value.str.len == sizeof(ZEND_CLONE_FUNC_NAME)-1)
  1378. && !zend_binary_strcasecmp(last_op->op2.u.constant.value.str.val, last_op->op2.u.constant.value.str.len, ZEND_CLONE_FUNC_NAME, sizeof(ZEND_CLONE_FUNC_NAME)-1)) {
  1379. zend_error(E_COMPILE_ERROR, "Cannot call __clone() method on objects - use 'clone $obj' instead");
  1380. }
  1381. if (last_op->opcode == ZEND_FETCH_OBJ_R) {
  1382. last_op->opcode = ZEND_INIT_METHOD_CALL;
  1383. SET_UNUSED(last_op->result);
  1384. Z_LVAL(left_bracket->u.constant) = ZEND_INIT_FCALL_BY_NAME;
  1385. } else {
  1386. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  1387. opline->opcode = ZEND_INIT_FCALL_BY_NAME;
  1388. opline->op2 = *left_bracket;
  1389. if (opline->op2.op_type == IS_CONST) {
  1390. opline->op1.op_type = IS_CONST;
  1391. Z_TYPE(opline->op1.u.constant) = IS_STRING;
  1392. Z_STRVAL(opline->op1.u.constant) = zend_str_tolower_dup(Z_STRVAL(opline->op2.u.constant), Z_STRLEN(opline->op2.u.constant));
  1393. Z_STRLEN(opline->op1.u.constant) = Z_STRLEN(opline->op2.u.constant);
  1394. opline->extended_value = zend_hash_func(Z_STRVAL(opline->op1.u.constant), Z_STRLEN(opline->op1.u.constant) + 1);
  1395. } else {
  1396. opline->extended_value = 0;
  1397. SET_UNUSED(opline->op1);
  1398. }
  1399. }
  1400. zend_stack_push(&CG(function_call_stack), (void *) &ptr, sizeof(zend_function *));
  1401. zend_do_extended_fcall_begin(TSRMLS_C);
  1402. }
  1403. /* }}} */
  1404. void zend_do_clone(znode *result, const znode *expr TSRMLS_DC) /* {{{ */
  1405. {
  1406. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  1407. opline->opcode = ZEND_CLONE;
  1408. opline->op1 = *expr;
  1409. SET_UNUSED(opline->op2);
  1410. opline->result.op_type = IS_VAR;
  1411. opline->result.u.var = get_temporary_variable(CG(active_op_array));
  1412. *result = opline->result;
  1413. }
  1414. /* }}} */
  1415. void zend_do_begin_dynamic_function_call(znode *function_name, int ns_call TSRMLS_DC) /* {{{ */
  1416. {
  1417. unsigned char *ptr = NULL;
  1418. zend_op *opline, *opline2;
  1419. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  1420. if (ns_call) {
  1421. char *slash;
  1422. int prefix_len, name_len;
  1423. /* In run-time PHP will check for function with full name and
  1424. internal function with short name */
  1425. opline->opcode = ZEND_INIT_NS_FCALL_BY_NAME;
  1426. opline->op2 = *function_name;
  1427. opline->extended_value = 0;
  1428. opline->op1.op_type = IS_CONST;
  1429. Z_TYPE(opline->op1.u.constant) = IS_STRING;
  1430. Z_STRVAL(opline->op1.u.constant) = zend_str_tolower_dup(Z_STRVAL(opline->op2.u.constant), Z_STRLEN(opline->op2.u.constant));
  1431. Z_STRLEN(opline->op1.u.constant) = Z_STRLEN(opline->op2.u.constant);
  1432. opline->extended_value = zend_hash_func(Z_STRVAL(opline->op1.u.constant), Z_STRLEN(opline->op1.u.constant) + 1);
  1433. slash = zend_memrchr(Z_STRVAL(opline->op1.u.constant), '\\', Z_STRLEN(opline->op1.u.constant));
  1434. prefix_len = slash-Z_STRVAL(opline->op1.u.constant)+1;
  1435. name_len = Z_STRLEN(opline->op1.u.constant)-prefix_len;
  1436. opline2 = get_next_op(CG(active_op_array) TSRMLS_CC);
  1437. opline2->opcode = ZEND_OP_DATA;
  1438. opline2->op1.op_type = IS_CONST;
  1439. Z_TYPE(opline2->op1.u.constant) = IS_LONG;
  1440. if(!slash) {
  1441. zend_error(E_CORE_ERROR, "Namespaced name %s should contain slash", Z_STRVAL(opline->op1.u.constant));
  1442. }
  1443. /* this is the length of namespace prefix */
  1444. Z_LVAL(opline2->op1.u.constant) = prefix_len;
  1445. /* this is the hash of the non-prefixed part, lowercased */
  1446. opline2->extended_value = zend_hash_func(slash+1, name_len+1);
  1447. SET_UNUSED(opline2->op2);
  1448. } else {
  1449. opline->opcode = ZEND_INIT_FCALL_BY_NAME;
  1450. opline->op2 = *function_name;
  1451. if (opline->op2.op_type == IS_CONST) {
  1452. opline->op1.op_type = IS_CONST;
  1453. Z_TYPE(opline->op1.u.constant) = IS_STRING;
  1454. Z_STRVAL(opline->op1.u.constant) = zend_str_tolower_dup(Z_STRVAL(opline->op2.u.constant), Z_STRLEN(opline->op2.u.constant));
  1455. Z_STRLEN(opline->op1.u.constant) = Z_STRLEN(opline->op2.u.constant);
  1456. opline->extended_value = zend_hash_func(Z_STRVAL(opline->op1.u.constant), Z_STRLEN(opline->op1.u.constant) + 1);
  1457. } else {
  1458. opline->extended_value = 0;
  1459. SET_UNUSED(opline->op1);
  1460. }
  1461. }
  1462. zend_stack_push(&CG(function_call_stack), (void *) &ptr, sizeof(zend_function *));
  1463. zend_do_extended_fcall_begin(TSRMLS_C);
  1464. }
  1465. /* }}} */
  1466. void zend_resolve_non_class_name(znode *element_name, zend_bool check_namespace TSRMLS_DC) /* {{{ */
  1467. {
  1468. znode tmp;
  1469. int len;
  1470. zval **ns;
  1471. char *lcname, *compound = memchr(Z_STRVAL(element_name->u.constant), '\\', Z_STRLEN(element_name->u.constant));
  1472. if (Z_STRVAL(element_name->u.constant)[0] == '\\') {
  1473. /* name starts with \ so it is known and unambiguos, nothing to do here but shorten it */
  1474. memmove(Z_STRVAL(element_name->u.constant), Z_STRVAL(element_name->u.constant)+1, Z_STRLEN(element_name->u.constant));
  1475. --Z_STRLEN(element_name->u.constant);
  1476. return;
  1477. }
  1478. if(!check_namespace) {
  1479. return;
  1480. }
  1481. if (compound && CG(current_import)) {
  1482. len = compound - Z_STRVAL(element_name->u.constant);
  1483. lcname = zend_str_tolower_dup(Z_STRVAL(element_name->u.constant), len);
  1484. /* Check if first part of compound name is an import name */
  1485. if (zend_hash_find(CG(current_import), lcname, len+1, (void**)&ns) == SUCCESS) {
  1486. /* Substitute import name */
  1487. tmp.op_type = IS_CONST;
  1488. tmp.u.constant = **ns;
  1489. zval_copy_ctor(&tmp.u.constant);
  1490. len += 1;
  1491. Z_STRLEN(element_name->u.constant) -= len;
  1492. memmove(Z_STRVAL(element_name->u.constant), Z_STRVAL(element_name->u.constant)+len, Z_STRLEN(element_name->u.constant)+1);
  1493. zend_do_build_namespace_name(&tmp, &tmp, element_name TSRMLS_CC);
  1494. *element_name = tmp;
  1495. efree(lcname);
  1496. return;
  1497. }
  1498. efree(lcname);
  1499. }
  1500. if (CG(current_namespace)) {
  1501. tmp = *element_name;
  1502. Z_STRLEN(tmp.u.constant) = sizeof("\\")-1 + Z_STRLEN(element_name->u.constant) + Z_STRLEN_P(CG(current_namespace));
  1503. Z_STRVAL(tmp.u.constant) = (char *) emalloc(Z_STRLEN(tmp.u.constant)+1);
  1504. memcpy(Z_STRVAL(tmp.u.constant), Z_STRVAL_P(CG(current_namespace)), Z_STRLEN_P(CG(current_namespace)));
  1505. memcpy(&(Z_STRVAL(tmp.u.constant)[Z_STRLEN_P(CG(current_namespace))]), "\\", sizeof("\\")-1);
  1506. memcpy(&(Z_STRVAL(tmp.u.constant)[Z_STRLEN_P(CG(current_namespace)) + sizeof("\\")-1]), Z_STRVAL(element_name->u.constant), Z_STRLEN(element_name->u.constant)+1);
  1507. STR_FREE(Z_STRVAL(element_name->u.constant));
  1508. *element_name = tmp;
  1509. }
  1510. }
  1511. /* }}} */
  1512. void zend_resolve_class_name(znode *class_name, ulong *fetch_type, int check_ns_name TSRMLS_DC) /* {{{ */
  1513. {
  1514. char *compound;
  1515. char *lcname;
  1516. zval **ns;
  1517. znode tmp;
  1518. int len;
  1519. compound = memchr(Z_STRVAL(class_name->u.constant), '\\', Z_STRLEN(class_name->u.constant));
  1520. if (compound) {
  1521. /* This is a compound class name that contains namespace prefix */
  1522. if (Z_STRVAL(class_name->u.constant)[0] == '\\') {
  1523. /* The STRING name has "\" prefix */
  1524. Z_STRLEN(class_name->u.constant) -= 1;
  1525. memmove(Z_STRVAL(class_name->u.constant), Z_STRVAL(class_name->u.constant)+1, Z_STRLEN(class_name->u.constant)+1);
  1526. Z_STRVAL(class_name->u.constant) = erealloc(
  1527. Z_STRVAL(class_name->u.constant),
  1528. Z_STRLEN(class_name->u.constant) + 1);
  1529. if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(Z_STRVAL(class_name->u.constant), Z_STRLEN(class_name->u.constant))) {
  1530. zend_error(E_COMPILE_ERROR, "'\\%s' is an invalid class name", Z_STRVAL(class_name->u.constant));
  1531. }
  1532. } else {
  1533. if (CG(current_import)) {
  1534. len = compound - Z_STRVAL(class_name->u.constant);
  1535. lcname = zend_str_tolower_dup(Z_STRVAL(class_name->u.constant), len);
  1536. /* Check if first part of compound name is an import name */
  1537. if (zend_hash_find(CG(current_import), lcname, len+1, (void**)&ns) == SUCCESS) {
  1538. /* Substitute import name */
  1539. tmp.op_type = IS_CONST;
  1540. tmp.u.constant = **ns;
  1541. zval_copy_ctor(&tmp.u.constant);
  1542. len += 1;
  1543. Z_STRLEN(class_name->u.constant) -= len;
  1544. memmove(Z_STRVAL(class_name->u.constant), Z_STRVAL(class_name->u.constant)+len, Z_STRLEN(class_name->u.constant)+1);
  1545. zend_do_build_namespace_name(&tmp, &tmp, class_name TSRMLS_CC);
  1546. *class_name = tmp;
  1547. efree(lcname);
  1548. return;
  1549. }
  1550. efree(lcname);
  1551. }
  1552. /* Here name is not prefixed with \ and not imported */
  1553. if (CG(current_namespace)) {
  1554. tmp.op_type = IS_CONST;
  1555. tmp.u.constant = *CG(current_namespace);
  1556. zval_copy_ctor(&tmp.u.constant);
  1557. zend_do_build_namespace_name(&tmp, &tmp, class_name TSRMLS_CC);
  1558. *class_name = tmp;
  1559. }
  1560. }
  1561. } else if (CG(current_import) || CG(current_namespace)) {
  1562. /* this is a plain name (without \) */
  1563. lcname = zend_str_tolower_dup(Z_STRVAL(class_name->u.constant), Z_STRLEN(class_name->u.constant));
  1564. if (CG(current_import) &&
  1565. zend_hash_find(CG(current_import), lcname, Z_STRLEN(class_name->u.constant)+1, (void**)&ns) == SUCCESS) {
  1566. /* The given name is an import name. Substitute it. */
  1567. zval_dtor(&class_name->u.constant);
  1568. class_name->u.constant = **ns;
  1569. zval_copy_ctor(&class_name->u.constant);
  1570. } else if (CG(current_namespace)) {
  1571. /* plain name, no import - prepend current namespace to it */
  1572. tmp.op_type = IS_CONST;
  1573. tmp.u.constant = *CG(current_namespace);
  1574. zval_copy_ctor(&tmp.u.constant);
  1575. zend_do_build_namespace_name(&tmp, &tmp, class_name TSRMLS_CC);
  1576. *class_name = tmp;
  1577. }
  1578. efree(lcname);
  1579. }
  1580. }
  1581. /* }}} */
  1582. void zend_do_fetch_class(znode *result, znode *class_name TSRMLS_DC) /* {{{ */
  1583. {
  1584. long fetch_class_op_number;
  1585. zend_op *opline;
  1586. if (class_name->op_type == IS_CONST &&
  1587. Z_TYPE(class_name->u.constant) == IS_STRING &&
  1588. Z_STRLEN(class_name->u.constant) == 0) {
  1589. /* Usage of namespace as class name not in namespace */
  1590. zval_dtor(&class_name->u.constant);
  1591. zend_error(E_COMPILE_ERROR, "Cannot use 'namespace' as a class name");
  1592. return;
  1593. }
  1594. fetch_class_op_number = get_next_op_number(CG(active_op_array));
  1595. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  1596. opline->opcode = ZEND_FETCH_CLASS;
  1597. SET_UNUSED(opline->op1);
  1598. opline->extended_value = ZEND_FETCH_CLASS_GLOBAL;
  1599. CG(catch_begin) = fetch_class_op_number;
  1600. if (class_name->op_type == IS_CONST) {
  1601. int fetch_type;
  1602. fetch_type = zend_get_class_fetch_type(class_name->u.constant.value.str.val, class_name->u.constant.value.str.len);
  1603. switch (fetch_type) {
  1604. case ZEND_FETCH_CLASS_SELF:
  1605. case ZEND_FETCH_CLASS_PARENT:
  1606. case ZEND_FETCH_CLASS_STATIC:
  1607. SET_UNUSED(opline->op2);
  1608. opline->extended_value = fetch_type;
  1609. zval_dtor(&class_name->u.constant);
  1610. break;
  1611. default:
  1612. zend_resolve_class_name(class_name, &opline->extended_value, 0 TSRMLS_CC);
  1613. opline->op2 = *class_name;
  1614. break;
  1615. }
  1616. } else {
  1617. opline->op2 = *class_name;
  1618. }
  1619. opline->result.u.var = get_temporary_variable(CG(active_op_array));
  1620. opline->result.u.EA.type = opline->extended_value;
  1621. opline->result.op_type = IS_VAR; /* FIXME: Hack so that INIT_FCALL_BY_NAME still knows this is a class */
  1622. *result = opline->result;
  1623. }
  1624. /* }}} */
  1625. void zend_do_label(znode *label TSRMLS_DC) /* {{{ */
  1626. {
  1627. zend_op_array *oparray = CG(active_op_array);
  1628. zend_label dest;
  1629. if (!CG(labels)) {
  1630. ALLOC_HASHTABLE(CG(labels));
  1631. zend_hash_init(CG(labels), 4, NULL, NULL, 0);
  1632. }
  1633. dest.brk_cont = oparray->current_brk_cont;
  1634. dest.opline_num = get_next_op_number(oparray);
  1635. if (zend_hash_add(CG(labels), Z_STRVAL(label->u.constant), Z_STRLEN(label->u.constant) + 1, (void**)&dest, sizeof(zend_label), NULL) == FAILURE) {
  1636. zend_error(E_COMPILE_ERROR, "Label '%s' already defined", Z_STRVAL(label->u.constant));
  1637. }
  1638. /* Done with label now */
  1639. zval_dtor(&label->u.constant);
  1640. }
  1641. /* }}} */
  1642. void zend_resolve_goto_label(zend_op_array *op_array, zend_op *opline, int pass2 TSRMLS_DC) /* {{{ */
  1643. {
  1644. zend_label *dest;
  1645. long current, distance;
  1646. if (CG(labels) == NULL ||
  1647. zend_hash_find(CG(labels), Z_STRVAL(opline->op2.u.constant), Z_STRLEN(opline->op2.u.constant)+1, (void**)&dest) == FAILURE) {
  1648. if (pass2) {
  1649. CG(in_compilation) = 1;
  1650. CG(active_op_array) = op_array;
  1651. CG(zend_lineno) = opline->lineno;
  1652. zend_error(E_COMPILE_ERROR, "'goto' to undefined label '%s'", Z_STRVAL(opline->op2.u.constant));
  1653. } else {
  1654. /* Label is not defined. Delay to pass 2. */
  1655. INC_BPC(op_array);
  1656. return;
  1657. }
  1658. }
  1659. opline->op1.u.opline_num = dest->opline_num;
  1660. zval_dtor(&opline->op2.u.constant);
  1661. /* Check that we are not moving into loop or switch */
  1662. current = opline->extended_value;
  1663. for (distance = 0; current != dest->brk_cont; distance++) {
  1664. if (current == -1) {
  1665. if (pass2) {
  1666. CG(in_compilation) = 1;
  1667. CG(active_op_array) = op_array;
  1668. CG(zend_lineno) = opline->lineno;
  1669. }
  1670. zend_error(E_COMPILE_ERROR, "'goto' into loop or switch statement is disallowed");
  1671. }
  1672. current = op_array->brk_cont_array[current].parent;
  1673. }
  1674. if (distance == 0) {
  1675. /* Nothing to break out of, optimize to ZEND_JMP */
  1676. opline->opcode = ZEND_JMP;
  1677. opline->extended_value = 0;
  1678. SET_UNUSED(opline->op2);
  1679. } else {
  1680. /* Set real break distance */
  1681. ZVAL_LONG(&opline->op2.u.constant, distance);
  1682. }
  1683. if (pass2) {
  1684. DEC_BPC(op_array);
  1685. }
  1686. }
  1687. /* }}} */
  1688. void zend_do_goto(const znode *label TSRMLS_DC) /* {{{ */
  1689. {
  1690. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  1691. opline->opcode = ZEND_GOTO;
  1692. opline->extended_value = CG(active_op_array)->current_brk_cont;
  1693. SET_UNUSED(opline->op1);
  1694. opline->op2 = *label;
  1695. zend_resolve_goto_label(CG(active_op_array), opline, 0 TSRMLS_CC);
  1696. }
  1697. /* }}} */
  1698. void zend_release_labels(TSRMLS_D) /* {{{ */
  1699. {
  1700. if (CG(labels)) {
  1701. zend_hash_destroy(CG(labels));
  1702. FREE_HASHTABLE(CG(labels));
  1703. }
  1704. if (!zend_stack_is_empty(&CG(labels_stack))) {
  1705. HashTable **pht;
  1706. zend_stack_top(&CG(labels_stack), (void**)&pht);
  1707. CG(labels) = *pht;
  1708. zend_stack_del_top(&CG(labels_stack));
  1709. } else {
  1710. CG(labels) = NULL;
  1711. }
  1712. }
  1713. /* }}} */
  1714. void zend_do_build_full_name(znode *result, znode *prefix, znode *name, int is_class_member TSRMLS_DC) /* {{{ */
  1715. {
  1716. zend_uint length;
  1717. if (!result) {
  1718. result = prefix;
  1719. } else {
  1720. *result = *prefix;
  1721. }
  1722. if (is_class_member) {
  1723. length = sizeof("::")-1 + result->u.constant.value.str.len + name->u.constant.value.str.len;
  1724. result->u.constant.value.str.val = erealloc(result->u.constant.value.str.val, length+1);
  1725. memcpy(&result->u.constant.value.str.val[result->u.constant.value.str.len], "::", sizeof("::")-1);
  1726. memcpy(&result->u.constant.value.str.val[result->u.constant.value.str.len + sizeof("::")-1], name->u.constant.value.str.val, name->u.constant.value.str.len+1);
  1727. STR_FREE(name->u.constant.value.str.val);
  1728. result->u.constant.value.str.len = length;
  1729. } else {
  1730. length = sizeof("\\")-1 + result->u.constant.value.str.len + name->u.constant.value.str.len;
  1731. result->u.constant.value.str.val = erealloc(result->u.constant.value.str.val, length+1);
  1732. memcpy(&result->u.constant.value.str.val[result->u.constant.value.str.len], "\\", sizeof("\\")-1);
  1733. memcpy(&result->u.constant.value.str.val[result->u.constant.value.str.len + sizeof("\\")-1], name->u.constant.value.str.val, name->u.constant.value.str.len+1);
  1734. STR_FREE(name->u.constant.value.str.val);
  1735. result->u.constant.value.str.len = length;
  1736. }
  1737. }
  1738. /* }}} */
  1739. int zend_do_begin_class_member_function_call(znode *class_name, znode *method_name TSRMLS_DC) /* {{{ */
  1740. {
  1741. znode class_node;
  1742. unsigned char *ptr = NULL;
  1743. zend_op *opline;
  1744. ulong fetch_type = 0;
  1745. if (method_name->op_type == IS_CONST) {
  1746. char *lcname = zend_str_tolower_dup(Z_STRVAL(method_name->u.constant), Z_STRLEN(method_name->u.constant));
  1747. if ((sizeof(ZEND_CONSTRUCTOR_FUNC_NAME)-1) == Z_STRLEN(method_name->u.constant) &&
  1748. memcmp(lcname, ZEND_CONSTRUCTOR_FUNC_NAME, sizeof(ZEND_CONSTRUCTOR_FUNC_NAME)-1) == 0) {
  1749. zval_dtor(&method_name->u.constant);
  1750. SET_UNUSED(*method_name);
  1751. }
  1752. efree(lcname);
  1753. }
  1754. if (class_name->op_type == IS_CONST &&
  1755. ZEND_FETCH_CLASS_DEFAULT == zend_get_class_fetch_type(Z_STRVAL(class_name->u.constant), Z_STRLEN(class_name->u.constant))) {
  1756. fetch_type = ZEND_FETCH_CLASS_GLOBAL;
  1757. zend_resolve_class_name(class_name, &fetch_type, 1 TSRMLS_CC);
  1758. class_node = *class_name;
  1759. } else {
  1760. zend_do_fetch_class(&class_node, class_name TSRMLS_CC);
  1761. }
  1762. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  1763. opline->opcode = ZEND_INIT_STATIC_METHOD_CALL;
  1764. opline->op1 = class_node;
  1765. opline->op2 = *method_name;
  1766. zend_stack_push(&CG(function_call_stack), (void *) &ptr, sizeof(zend_function *));
  1767. zend_do_extended_fcall_begin(TSRMLS_C);
  1768. return 1; /* Dynamic */
  1769. }
  1770. /* }}} */
  1771. void zend_do_end_function_call(znode *function_name, znode *result, const znode *argument_list, int is_method, int is_dynamic_fcall TSRMLS_DC) /* {{{ */
  1772. {
  1773. zend_op *opline;
  1774. if (is_method && function_name && function_name->op_type == IS_UNUSED) {
  1775. /* clone */
  1776. if (Z_LVAL(argument_list->u.constant) != 0) {
  1777. zend_error(E_WARNING, "Clone method does not require arguments");
  1778. }
  1779. opline = &CG(active_op_array)->opcodes[Z_LVAL(function_name->u.constant)];
  1780. } else {
  1781. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  1782. if (!is_method && !is_dynamic_fcall && function_name->op_type==IS_CONST) {
  1783. opline->opcode = ZEND_DO_FCALL;
  1784. opline->op1 = *function_name;
  1785. ZVAL_LONG(&opline->op2.u.constant, zend_hash_func(Z_STRVAL(function_name->u.constant), Z_STRLEN(function_name->u.constant) + 1));
  1786. } else {
  1787. opline->opcode = ZEND_DO_FCALL_BY_NAME;
  1788. SET_UNUSED(opline->op1);
  1789. }
  1790. }
  1791. opline->result.u.var = get_temporary_variable(CG(active_op_array));
  1792. opline->result.op_type = IS_VAR;
  1793. *result = opline->result;
  1794. SET_UNUSED(opline->op2);
  1795. zend_stack_del_top(&CG(function_call_stack));
  1796. opline->extended_value = Z_LVAL(argument_list->u.constant);
  1797. }
  1798. /* }}} */
  1799. void zend_do_pass_param(znode *param, zend_uchar op, int offset TSRMLS_DC) /* {{{ */
  1800. {
  1801. zend_op *opline;
  1802. int original_op=op;
  1803. zend_function **function_ptr_ptr, *function_ptr;
  1804. int send_by_reference;
  1805. int send_function = 0;
  1806. zend_stack_top(&CG(function_call_stack), (void **) &function_ptr_ptr);
  1807. function_ptr = *function_ptr_ptr;
  1808. if (original_op == ZEND_SEND_REF) {
  1809. if (function_ptr &&
  1810. function_ptr->common.function_name &&
  1811. function_ptr->common.type == ZEND_USER_FUNCTION &&
  1812. !ARG_SHOULD_BE_SENT_BY_REF(function_ptr, (zend_uint) offset)) {
  1813. zend_error(E_DEPRECATED,
  1814. "Call-time pass-by-reference has been deprecated; "
  1815. "If you would like to pass it by reference, modify the declaration of %s(). "
  1816. "If you would like to enable call-time pass-by-reference, you can set "
  1817. "allow_call_time_pass_reference to true in your INI file", function_ptr->common.function_name);
  1818. } else {
  1819. zend_error(E_DEPRECATED, "Call-time pass-by-reference has been deprecated");
  1820. }
  1821. }
  1822. if (function_ptr) {
  1823. if (ARG_MAY_BE_SENT_BY_REF(function_ptr, (zend_uint) offset)) {
  1824. if (param->op_type & (IS_VAR|IS_CV)) {
  1825. send_by_reference = 1;
  1826. if (op == ZEND_SEND_VAR && zend_is_function_or_method_call(param)) {
  1827. /* Method call */
  1828. op = ZEND_SEND_VAR_NO_REF;
  1829. send_function = ZEND_ARG_SEND_FUNCTION | ZEND_ARG_SEND_SILENT;
  1830. }
  1831. } else {
  1832. op = ZEND_SEND_VAL;
  1833. send_by_reference = 0;
  1834. }
  1835. } else {
  1836. send_by_reference = ARG_SHOULD_BE_SENT_BY_REF(function_ptr, (zend_uint) offset) ? ZEND_ARG_SEND_BY_REF : 0;
  1837. }
  1838. } else {
  1839. send_by_reference = 0;
  1840. }
  1841. if (op == ZEND_SEND_VAR && zend_is_function_or_method_call(param)) {
  1842. /* Method call */
  1843. op = ZEND_SEND_VAR_NO_REF;
  1844. send_function = ZEND_ARG_SEND_FUNCTION;
  1845. } else if (op == ZEND_SEND_VAL && (param->op_type & (IS_VAR|IS_CV))) {
  1846. op = ZEND_SEND_VAR_NO_REF;
  1847. }
  1848. if (op!=ZEND_SEND_VAR_NO_REF && send_by_reference==ZEND_ARG_SEND_BY_REF) {
  1849. /* change to passing by reference */
  1850. switch (param->op_type) {
  1851. case IS_VAR:
  1852. case IS_CV:
  1853. op = ZEND_SEND_REF;
  1854. break;
  1855. default:
  1856. zend_error(E_COMPILE_ERROR, "Only variables can be passed by reference");
  1857. break;
  1858. }
  1859. }
  1860. if (original_op == ZEND_SEND_VAR) {
  1861. switch (op) {
  1862. case ZEND_SEND_VAR_NO_REF:
  1863. zend_do_end_variable_parse(param, BP_VAR_R, 0 TSRMLS_CC);
  1864. break;
  1865. case ZEND_SEND_VAR:
  1866. if (function_ptr) {
  1867. zend_do_end_variable_parse(param, BP_VAR_R, 0 TSRMLS_CC);
  1868. } else {
  1869. zend_do_end_variable_parse(param, BP_VAR_FUNC_ARG, offset TSRMLS_CC);
  1870. }
  1871. break;
  1872. case ZEND_SEND_REF:
  1873. zend_do_end_variable_parse(param, BP_VAR_W, 0 TSRMLS_CC);
  1874. break;
  1875. }
  1876. }
  1877. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  1878. if (op == ZEND_SEND_VAR_NO_REF) {
  1879. if (function_ptr) {
  1880. opline->extended_value = ZEND_ARG_COMPILE_TIME_BOUND | send_by_reference | send_function;
  1881. } else {
  1882. opline->extended_value = send_function;
  1883. }
  1884. } else {
  1885. if (function_ptr) {
  1886. opline->extended_value = ZEND_DO_FCALL;
  1887. } else {
  1888. opline->extended_value = ZEND_DO_FCALL_BY_NAME;
  1889. }
  1890. }
  1891. opline->opcode = op;
  1892. opline->op1 = *param;
  1893. opline->op2.u.opline_num = offset;
  1894. SET_UNUSED(opline->op2);
  1895. }
  1896. /* }}} */
  1897. static int generate_free_switch_expr(const zend_switch_entry *switch_entry TSRMLS_DC) /* {{{ */
  1898. {
  1899. zend_op *opline;
  1900. if (switch_entry->cond.op_type != IS_VAR && switch_entry->cond.op_type != IS_TMP_VAR) {
  1901. return (switch_entry->cond.op_type == IS_UNUSED);
  1902. }
  1903. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  1904. opline->opcode = (switch_entry->cond.op_type == IS_TMP_VAR) ? ZEND_FREE : ZEND_SWITCH_FREE;
  1905. opline->op1 = switch_entry->cond;
  1906. SET_UNUSED(opline->op2);
  1907. opline->extended_value = 0;
  1908. return 0;
  1909. }
  1910. /* }}} */
  1911. static int generate_free_foreach_copy(const zend_op *foreach_copy TSRMLS_DC) /* {{{ */
  1912. {
  1913. zend_op *opline;
  1914. /* If we reach the seperator then stop applying the stack */
  1915. if (foreach_copy->result.op_type == IS_UNUSED && foreach_copy->op1.op_type == IS_UNUSED) {
  1916. return 1;
  1917. }
  1918. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  1919. opline->opcode = (foreach_copy->result.op_type == IS_TMP_VAR) ? ZEND_FREE : ZEND_SWITCH_FREE;
  1920. opline->op1 = foreach_copy->result;
  1921. SET_UNUSED(opline->op2);
  1922. opline->extended_value = 1;
  1923. if (foreach_copy->op1.op_type != IS_UNUSED) {
  1924. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  1925. opline->opcode = (foreach_copy->op1.op_type == IS_TMP_VAR) ? ZEND_FREE : ZEND_SWITCH_FREE;
  1926. opline->op1 = foreach_copy->op1;
  1927. SET_UNUSED(opline->op2);
  1928. opline->extended_value = 0;
  1929. }
  1930. return 0;
  1931. }
  1932. /* }}} */
  1933. void zend_do_return(znode *expr, int do_end_vparse TSRMLS_DC) /* {{{ */
  1934. {
  1935. zend_op *opline;
  1936. int start_op_number, end_op_number;
  1937. if (do_end_vparse) {
  1938. if (CG(active_op_array)->return_reference && !zend_is_function_or_method_call(expr)) {
  1939. zend_do_end_variable_parse(expr, BP_VAR_W, 0 TSRMLS_CC);
  1940. } else {
  1941. zend_do_end_variable_parse(expr, BP_VAR_R, 0 TSRMLS_CC);
  1942. }
  1943. }
  1944. start_op_number = get_next_op_number(CG(active_op_array));
  1945. #ifdef ZTS
  1946. zend_stack_apply_with_argument(&CG(switch_cond_stack), ZEND_STACK_APPLY_TOPDOWN, (int (*)(void *element, void *)) generate_free_switch_expr TSRMLS_CC);
  1947. zend_stack_apply_with_argument(&CG(foreach_copy_stack), ZEND_STACK_APPLY_TOPDOWN, (int (*)(void *element, void *)) generate_free_foreach_copy TSRMLS_CC);
  1948. #else
  1949. zend_stack_apply(&CG(switch_cond_stack), ZEND_STACK_APPLY_TOPDOWN, (int (*)(void *element)) generate_free_switch_expr);
  1950. zend_stack_apply(&CG(foreach_copy_stack), ZEND_STACK_APPLY_TOPDOWN, (int (*)(void *element)) generate_free_foreach_copy);
  1951. #endif
  1952. end_op_number = get_next_op_number(CG(active_op_array));
  1953. while (start_op_number < end_op_number) {
  1954. CG(active_op_array)->opcodes[start_op_number].op1.u.EA.type = EXT_TYPE_FREE_ON_RETURN;
  1955. start_op_number++;
  1956. }
  1957. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  1958. opline->opcode = ZEND_RETURN;
  1959. if (expr) {
  1960. opline->op1 = *expr;
  1961. if (do_end_vparse && zend_is_function_or_method_call(expr)) {
  1962. opline->extended_value = ZEND_RETURNS_FUNCTION;
  1963. }
  1964. } else {
  1965. opline->op1.op_type = IS_CONST;
  1966. INIT_ZVAL(opline->op1.u.constant);
  1967. }
  1968. SET_UNUSED(opline->op2);
  1969. }
  1970. /* }}} */
  1971. static int zend_add_try_element(zend_uint try_op TSRMLS_DC) /* {{{ */
  1972. {
  1973. int try_catch_offset = CG(active_op_array)->last_try_catch++;
  1974. CG(active_op_array)->try_catch_array = erealloc(CG(active_op_array)->try_catch_array, sizeof(zend_try_catch_element)*CG(active_op_array)->last_try_catch);
  1975. CG(active_op_array)->try_catch_array[try_catch_offset].try_op = try_op;
  1976. return try_catch_offset;
  1977. }
  1978. /* }}} */
  1979. static void zend_add_catch_element(int offset, zend_uint catch_op TSRMLS_DC) /* {{{ */
  1980. {
  1981. CG(active_op_array)->try_catch_array[offset].catch_op = catch_op;
  1982. }
  1983. /* }}} */
  1984. void zend_do_first_catch(znode *open_parentheses TSRMLS_DC) /* {{{ */
  1985. {
  1986. open_parentheses->u.opline_num = get_next_op_number(CG(active_op_array));
  1987. }
  1988. /* }}} */
  1989. void zend_initialize_try_catch_element(const znode *try_token TSRMLS_DC) /* {{{ */
  1990. {
  1991. int jmp_op_number = get_next_op_number(CG(active_op_array));
  1992. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  1993. zend_llist jmp_list;
  1994. zend_llist *jmp_list_ptr;
  1995. opline->opcode = ZEND_JMP;
  1996. SET_UNUSED(opline->op1);
  1997. SET_UNUSED(opline->op2);
  1998. /* save for backpatching */
  1999. zend_llist_init(&jmp_list, sizeof(int), NULL, 0);
  2000. zend_stack_push(&CG(bp_stack), (void *) &jmp_list, sizeof(zend_llist));
  2001. zend_stack_top(&CG(bp_stack), (void **) &jmp_list_ptr);
  2002. zend_llist_add_element(jmp_list_ptr, &jmp_op_number);
  2003. zend_add_catch_element(try_token->u.opline_num, get_next_op_number(CG(active_op_array)) TSRMLS_CC);
  2004. }
  2005. /* }}} */
  2006. void zend_do_mark_last_catch(const znode *first_catch, const znode *last_additional_catch TSRMLS_DC) /* {{{ */
  2007. {
  2008. CG(active_op_array)->last--;
  2009. zend_do_if_end(TSRMLS_C);
  2010. if (last_additional_catch->u.opline_num == -1) {
  2011. CG(active_op_array)->opcodes[first_catch->u.opline_num].op1.u.EA.type = 1;
  2012. CG(active_op_array)->opcodes[first_catch->u.opline_num].extended_value = get_next_op_number(CG(active_op_array));
  2013. } else {
  2014. CG(active_op_array)->opcodes[last_additional_catch->u.opline_num].op1.u.EA.type = 1;
  2015. CG(active_op_array)->opcodes[last_additional_catch->u.opline_num].extended_value = get_next_op_number(CG(active_op_array));
  2016. }
  2017. DEC_BPC(CG(active_op_array));
  2018. }
  2019. /* }}} */
  2020. void zend_do_try(znode *try_token TSRMLS_DC) /* {{{ */
  2021. {
  2022. try_token->u.opline_num = zend_add_try_element(get_next_op_number(CG(active_op_array)) TSRMLS_CC);
  2023. INC_BPC(CG(active_op_array));
  2024. }
  2025. /* }}} */
  2026. void zend_do_begin_catch(znode *try_token, znode *class_name, const znode *catch_var, znode *first_catch TSRMLS_DC) /* {{{ */
  2027. {
  2028. long catch_op_number;
  2029. zend_op *opline;
  2030. znode catch_class;
  2031. zend_do_fetch_class(&catch_class, class_name TSRMLS_CC);
  2032. catch_op_number = get_next_op_number(CG(active_op_array));
  2033. if (catch_op_number > 0) {
  2034. opline = &CG(active_op_array)->opcodes[catch_op_number-1];
  2035. if (opline->opcode == ZEND_FETCH_CLASS) {
  2036. opline->extended_value |= ZEND_FETCH_CLASS_NO_AUTOLOAD;
  2037. }
  2038. }
  2039. if (first_catch) {
  2040. first_catch->u.opline_num = catch_op_number;
  2041. }
  2042. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  2043. opline->opcode = ZEND_CATCH;
  2044. opline->op1 = catch_class;
  2045. /* SET_UNUSED(opline->op1); */ /* FIXME: Define IS_CLASS or something like that */
  2046. opline->op2.op_type = IS_CV;
  2047. opline->op2.u.var = lookup_cv(CG(active_op_array), catch_var->u.constant.value.str.val, catch_var->u.constant.value.str.len);
  2048. opline->op2.u.EA.type = 0;
  2049. opline->op1.u.EA.type = 0; /* 1 means it's the last catch in the block */
  2050. try_token->u.opline_num = catch_op_number;
  2051. }
  2052. /* }}} */
  2053. void zend_do_end_catch(const znode *try_token TSRMLS_DC) /* {{{ */
  2054. {
  2055. int jmp_op_number = get_next_op_number(CG(active_op_array));
  2056. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  2057. zend_llist *jmp_list_ptr;
  2058. opline->opcode = ZEND_JMP;
  2059. SET_UNUSED(opline->op1);
  2060. SET_UNUSED(opline->op2);
  2061. /* save for backpatching */
  2062. zend_stack_top(&CG(bp_stack), (void **) &jmp_list_ptr);
  2063. zend_llist_add_element(jmp_list_ptr, &jmp_op_number);
  2064. CG(active_op_array)->opcodes[try_token->u.opline_num].extended_value = get_next_op_number(CG(active_op_array));
  2065. }
  2066. /* }}} */
  2067. void zend_do_throw(const znode *expr TSRMLS_DC) /* {{{ */
  2068. {
  2069. zend_op *opline;
  2070. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  2071. opline->opcode = ZEND_THROW;
  2072. opline->op1 = *expr;
  2073. SET_UNUSED(opline->op2);
  2074. }
  2075. /* }}} */
  2076. ZEND_API void function_add_ref(zend_function *function) /* {{{ */
  2077. {
  2078. if (function->type == ZEND_USER_FUNCTION) {
  2079. zend_op_array *op_array = &function->op_array;
  2080. (*op_array->refcount)++;
  2081. if (op_array->static_variables) {
  2082. HashTable *static_variables = op_array->static_variables;
  2083. zval *tmp_zval;
  2084. ALLOC_HASHTABLE(op_array->static_variables);
  2085. zend_hash_init(op_array->static_variables, zend_hash_num_elements(static_variables), NULL, ZVAL_PTR_DTOR, 0);
  2086. zend_hash_copy(op_array->static_variables, static_variables, (copy_ctor_func_t) zval_add_ref, (void *) &tmp_zval, sizeof(zval *));
  2087. }
  2088. }
  2089. }
  2090. /* }}} */
  2091. static void do_inherit_parent_constructor(zend_class_entry *ce) /* {{{ */
  2092. {
  2093. zend_function *function;
  2094. if (!ce->parent) {
  2095. return;
  2096. }
  2097. /* You cannot change create_object */
  2098. ce->create_object = ce->parent->create_object;
  2099. /* Inherit special functions if needed */
  2100. if (!ce->get_iterator) {
  2101. ce->get_iterator = ce->parent->get_iterator;
  2102. }
  2103. if (!ce->iterator_funcs.funcs) {
  2104. ce->iterator_funcs.funcs = ce->parent->iterator_funcs.funcs;
  2105. }
  2106. if (!ce->__get) {
  2107. ce->__get = ce->parent->__get;
  2108. }
  2109. if (!ce->__set) {
  2110. ce->__set = ce->parent->__set;
  2111. }
  2112. if (!ce->__unset) {
  2113. ce->__unset = ce->parent->__unset;
  2114. }
  2115. if (!ce->__isset) {
  2116. ce->__isset = ce->parent->__isset;
  2117. }
  2118. if (!ce->__call) {
  2119. ce->__call = ce->parent->__call;
  2120. }
  2121. if (!ce->__callstatic) {
  2122. ce->__callstatic = ce->parent->__callstatic;
  2123. }
  2124. if (!ce->__tostring) {
  2125. ce->__tostring = ce->parent->__tostring;
  2126. }
  2127. if (!ce->clone) {
  2128. ce->clone = ce->parent->clone;
  2129. }
  2130. if(!ce->serialize) {
  2131. ce->serialize = ce->parent->serialize;
  2132. }
  2133. if(!ce->unserialize) {
  2134. ce->unserialize = ce->parent->unserialize;
  2135. }
  2136. if (!ce->destructor) {
  2137. ce->destructor = ce->parent->destructor;
  2138. }
  2139. if (ce->constructor) {
  2140. if (ce->parent->constructor && ce->parent->constructor->common.fn_flags & ZEND_ACC_FINAL) {
  2141. zend_error(E_ERROR, "Cannot override final %s::%s() with %s::%s()",
  2142. ce->parent->name, ce->parent->constructor->common.function_name,
  2143. ce->name, ce->constructor->common.function_name
  2144. );
  2145. }
  2146. return;
  2147. }
  2148. if (zend_hash_find(&ce->parent->function_table, ZEND_CONSTRUCTOR_FUNC_NAME, sizeof(ZEND_CONSTRUCTOR_FUNC_NAME), (void **)&function)==SUCCESS) {
  2149. /* inherit parent's constructor */
  2150. zend_hash_update(&ce->function_table, ZEND_CONSTRUCTOR_FUNC_NAME, sizeof(ZEND_CONSTRUCTOR_FUNC_NAME), function, sizeof(zend_function), NULL);
  2151. function_add_ref(function);
  2152. } else {
  2153. /* Don't inherit the old style constructor if we already have the new style constructor */
  2154. char *lc_class_name;
  2155. char *lc_parent_class_name;
  2156. lc_class_name = zend_str_tolower_dup(ce->name, ce->name_length);
  2157. if (!zend_hash_exists(&ce->function_table, lc_class_name, ce->name_length+1)) {
  2158. lc_parent_class_name = zend_str_tolower_dup(ce->parent->name, ce->parent->name_length);
  2159. if (!zend_hash_exists(&ce->function_table, lc_parent_class_name, ce->parent->name_length+1) &&
  2160. zend_hash_find(&ce->parent->function_table, lc_parent_class_name, ce->parent->name_length+1, (void **)&function)==SUCCESS) {
  2161. if (function->common.fn_flags & ZEND_ACC_CTOR) {
  2162. /* inherit parent's constructor */
  2163. zend_hash_update(&ce->function_table, lc_parent_class_name, ce->parent->name_length+1, function, sizeof(zend_function), NULL);
  2164. function_add_ref(function);
  2165. }
  2166. }
  2167. efree(lc_parent_class_name);
  2168. }
  2169. efree(lc_class_name);
  2170. }
  2171. ce->constructor = ce->parent->constructor;
  2172. }
  2173. /* }}} */
  2174. char *zend_visibility_string(zend_uint fn_flags) /* {{{ */
  2175. {
  2176. if (fn_flags & ZEND_ACC_PRIVATE) {
  2177. return "private";
  2178. }
  2179. if (fn_flags & ZEND_ACC_PROTECTED) {
  2180. return "protected";
  2181. }
  2182. if (fn_flags & ZEND_ACC_PUBLIC) {
  2183. return "public";
  2184. }
  2185. return "";
  2186. }
  2187. /* }}} */
  2188. static void do_inherit_method(zend_function *function) /* {{{ */
  2189. {
  2190. /* The class entry of the derived function intentionally remains the same
  2191. * as that of the parent class. That allows us to know in which context
  2192. * we're running, and handle private method calls properly.
  2193. */
  2194. function_add_ref(function);
  2195. }
  2196. /* }}} */
  2197. static zend_bool zend_do_perform_implementation_check(const zend_function *fe, const zend_function *proto) /* {{{ */
  2198. {
  2199. zend_uint i;
  2200. /* If it's a user function then arg_info == NULL means we don't have any parameters but
  2201. * we still need to do the arg number checks. We are only willing to ignore this for internal
  2202. * functions because extensions don't always define arg_info.
  2203. */
  2204. if (!proto || (!proto->common.arg_info && proto->common.type != ZEND_USER_FUNCTION)) {
  2205. return 1;
  2206. }
  2207. /* Checks for constructors only if they are declared in an interface */
  2208. if ((fe->common.fn_flags & ZEND_ACC_CTOR) && (proto->common.scope->ce_flags & ZEND_ACC_INTERFACE) == 0) {
  2209. return 1;
  2210. }
  2211. /* check number of arguments */
  2212. if (proto->common.required_num_args < fe->common.required_num_args
  2213. || proto->common.num_args > fe->common.num_args) {
  2214. return 0;
  2215. }
  2216. if (fe->common.type != ZEND_USER_FUNCTION
  2217. && proto->common.pass_rest_by_reference
  2218. && !fe->common.pass_rest_by_reference) {
  2219. return 0;
  2220. }
  2221. /* by-ref constraints on return values are covariant */
  2222. if (proto->common.return_reference && !fe->common.return_reference) {
  2223. return 0;
  2224. }
  2225. for (i=0; i < proto->common.num_args; i++) {
  2226. if (ZEND_LOG_XOR(fe->common.arg_info[i].class_name, proto->common.arg_info[i].class_name)) {
  2227. /* Only one has a type hint and the other one doesn't */
  2228. return 0;
  2229. }
  2230. if (fe->common.arg_info[i].class_name
  2231. && strcasecmp(fe->common.arg_info[i].class_name, proto->common.arg_info[i].class_name)!=0) {
  2232. char *colon;
  2233. if (fe->common.type != ZEND_USER_FUNCTION ||
  2234. strchr(proto->common.arg_info[i].class_name, '\\') != NULL ||
  2235. (colon = zend_memrchr(fe->common.arg_info[i].class_name, '\\', fe->common.arg_info[i].class_name_len)) == NULL ||
  2236. strcasecmp(colon+1, proto->common.arg_info[i].class_name) != 0) {
  2237. return 0;
  2238. }
  2239. }
  2240. if (fe->common.arg_info[i].array_type_hint != proto->common.arg_info[i].array_type_hint) {
  2241. /* Only one has an array type hint and the other one doesn't */
  2242. return 0;
  2243. }
  2244. /* by-ref constraints on arguments are invariant */
  2245. if (fe->common.arg_info[i].pass_by_reference != proto->common.arg_info[i].pass_by_reference) {
  2246. return 0;
  2247. }
  2248. }
  2249. if (proto->common.pass_rest_by_reference) {
  2250. for (i=proto->common.num_args; i < fe->common.num_args; i++) {
  2251. if (!fe->common.arg_info[i].pass_by_reference) {
  2252. return 0;
  2253. }
  2254. }
  2255. }
  2256. return 1;
  2257. }
  2258. /* }}} */
  2259. static zend_bool do_inherit_method_check(HashTable *child_function_table, zend_function *parent, const zend_hash_key *hash_key, zend_class_entry *child_ce) /* {{{ */
  2260. {
  2261. zend_uint child_flags;
  2262. zend_uint parent_flags = parent->common.fn_flags;
  2263. zend_function *child;
  2264. TSRMLS_FETCH();
  2265. if (zend_hash_quick_find(child_function_table, hash_key->arKey, hash_key->nKeyLength, hash_key->h, (void **) &child)==FAILURE) {
  2266. if (parent_flags & (ZEND_ACC_ABSTRACT)) {
  2267. child_ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
  2268. }
  2269. return 1; /* method doesn't exist in child, copy from parent */
  2270. }
  2271. if (parent->common.fn_flags & ZEND_ACC_ABSTRACT
  2272. && parent->common.scope != (child->common.prototype ? child->common.prototype->common.scope : child->common.scope)
  2273. && child->common.fn_flags & (ZEND_ACC_ABSTRACT|ZEND_ACC_IMPLEMENTED_ABSTRACT)) {
  2274. zend_error(E_COMPILE_ERROR, "Can't inherit abstract function %s::%s() (previously declared abstract in %s)",
  2275. parent->common.scope->name,
  2276. child->common.function_name,
  2277. child->common.prototype ? child->common.prototype->common.scope->name : child->common.scope->name);
  2278. }
  2279. if (parent_flags & ZEND_ACC_FINAL) {
  2280. zend_error(E_COMPILE_ERROR, "Cannot override final method %s::%s()", ZEND_FN_SCOPE_NAME(parent), child->common.function_name);
  2281. }
  2282. child_flags = child->common.fn_flags;
  2283. /* You cannot change from static to non static and vice versa.
  2284. */
  2285. if ((child_flags & ZEND_ACC_STATIC) != (parent_flags & ZEND_ACC_STATIC)) {
  2286. if (child->common.fn_flags & ZEND_ACC_STATIC) {
  2287. zend_error(E_COMPILE_ERROR, "Cannot make non static method %s::%s() static in class %s", ZEND_FN_SCOPE_NAME(parent), child->common.function_name, ZEND_FN_SCOPE_NAME(child));
  2288. } else {
  2289. zend_error(E_COMPILE_ERROR, "Cannot make static method %s::%s() non static in class %s", ZEND_FN_SCOPE_NAME(parent), child->common.function_name, ZEND_FN_SCOPE_NAME(child));
  2290. }
  2291. }
  2292. /* Disallow making an inherited method abstract. */
  2293. if ((child_flags & ZEND_ACC_ABSTRACT) && !(parent_flags & ZEND_ACC_ABSTRACT)) {
  2294. zend_error(E_COMPILE_ERROR, "Cannot make non abstract method %s::%s() abstract in class %s", ZEND_FN_SCOPE_NAME(parent), child->common.function_name, ZEND_FN_SCOPE_NAME(child));
  2295. }
  2296. if (parent_flags & ZEND_ACC_CHANGED) {
  2297. child->common.fn_flags |= ZEND_ACC_CHANGED;
  2298. } else {
  2299. /* Prevent derived classes from restricting access that was available in parent classes
  2300. */
  2301. if ((child_flags & ZEND_ACC_PPP_MASK) > (parent_flags & ZEND_ACC_PPP_MASK)) {
  2302. zend_error(E_COMPILE_ERROR, "Access level to %s::%s() must be %s (as in class %s)%s", ZEND_FN_SCOPE_NAME(child), child->common.function_name, zend_visibility_string(parent_flags), ZEND_FN_SCOPE_NAME(parent), (parent_flags&ZEND_ACC_PUBLIC) ? "" : " or weaker");
  2303. } else if (((child_flags & ZEND_ACC_PPP_MASK) < (parent_flags & ZEND_ACC_PPP_MASK))
  2304. && ((parent_flags & ZEND_ACC_PPP_MASK) & ZEND_ACC_PRIVATE)) {
  2305. child->common.fn_flags |= ZEND_ACC_CHANGED;
  2306. }
  2307. }
  2308. if (parent_flags & ZEND_ACC_PRIVATE) {
  2309. child->common.prototype = NULL;
  2310. } else if (parent_flags & ZEND_ACC_ABSTRACT) {
  2311. child->common.fn_flags |= ZEND_ACC_IMPLEMENTED_ABSTRACT;
  2312. child->common.prototype = parent;
  2313. } else if (!(parent->common.fn_flags & ZEND_ACC_CTOR) || (parent->common.prototype && (parent->common.prototype->common.scope->ce_flags & ZEND_ACC_INTERFACE))) {
  2314. /* ctors only have a prototype if it comes from an interface */
  2315. child->common.prototype = parent->common.prototype ? parent->common.prototype : parent;
  2316. }
  2317. if (child->common.prototype && (child->common.prototype->common.fn_flags & ZEND_ACC_ABSTRACT)) {
  2318. if (!zend_do_perform_implementation_check(child, child->common.prototype)) {
  2319. zend_error(E_COMPILE_ERROR, "Declaration of %s::%s() must be compatible with that of %s::%s()", ZEND_FN_SCOPE_NAME(child), child->common.function_name, ZEND_FN_SCOPE_NAME(child->common.prototype), child->common.prototype->common.function_name);
  2320. }
  2321. } else if (EG(error_reporting) & E_STRICT || EG(user_error_handler)) { /* Check E_STRICT (or custom error handler) before the check so that we save some time */
  2322. if (!zend_do_perform_implementation_check(child, parent)) {
  2323. zend_error(E_STRICT, "Declaration of %s::%s() should be compatible with that of %s::%s()", ZEND_FN_SCOPE_NAME(child), child->common.function_name, ZEND_FN_SCOPE_NAME(parent), parent->common.function_name);
  2324. }
  2325. }
  2326. return 0;
  2327. }
  2328. /* }}} */
  2329. static zend_bool do_inherit_property_access_check(HashTable *target_ht, zend_property_info *parent_info, const zend_hash_key *hash_key, zend_class_entry *ce) /* {{{ */
  2330. {
  2331. zend_property_info *child_info;
  2332. zend_class_entry *parent_ce = ce->parent;
  2333. if (parent_info->flags & (ZEND_ACC_PRIVATE|ZEND_ACC_SHADOW)) {
  2334. if (zend_hash_quick_find(&ce->properties_info, hash_key->arKey, hash_key->nKeyLength, hash_key->h, (void **) &child_info)==SUCCESS) {
  2335. child_info->flags |= ZEND_ACC_CHANGED;
  2336. } else {
  2337. zend_hash_quick_update(&ce->properties_info, hash_key->arKey, hash_key->nKeyLength, hash_key->h, parent_info, sizeof(zend_property_info), (void **) &child_info);
  2338. if(ce->type & ZEND_INTERNAL_CLASS) {
  2339. zend_duplicate_property_info_internal(child_info);
  2340. } else {
  2341. zend_duplicate_property_info(child_info);
  2342. }
  2343. child_info->flags &= ~ZEND_ACC_PRIVATE; /* it's not private anymore */
  2344. child_info->flags |= ZEND_ACC_SHADOW; /* but it's a shadow of private */
  2345. }
  2346. return 0; /* don't copy access information to child */
  2347. }
  2348. if (zend_hash_quick_find(&ce->properties_info, hash_key->arKey, hash_key->nKeyLength, hash_key->h, (void **) &child_info)==SUCCESS) {
  2349. if ((parent_info->flags & ZEND_ACC_STATIC) != (child_info->flags & ZEND_ACC_STATIC)) {
  2350. zend_error(E_COMPILE_ERROR, "Cannot redeclare %s%s::$%s as %s%s::$%s",
  2351. (parent_info->flags & ZEND_ACC_STATIC) ? "static " : "non static ", parent_ce->name, hash_key->arKey,
  2352. (child_info->flags & ZEND_ACC_STATIC) ? "static " : "non static ", ce->name, hash_key->arKey);
  2353. }
  2354. if(parent_info->flags & ZEND_ACC_CHANGED) {
  2355. child_info->flags |= ZEND_ACC_CHANGED;
  2356. }
  2357. if ((child_info->flags & ZEND_ACC_PPP_MASK) > (parent_info->flags & ZEND_ACC_PPP_MASK)) {
  2358. zend_error(E_COMPILE_ERROR, "Access level to %s::$%s must be %s (as in class %s)%s", ce->name, hash_key->arKey, zend_visibility_string(parent_info->flags), parent_ce->name, (parent_info->flags&ZEND_ACC_PUBLIC) ? "" : " or weaker");
  2359. } else if (child_info->flags & ZEND_ACC_IMPLICIT_PUBLIC) {
  2360. if (!(parent_info->flags & ZEND_ACC_IMPLICIT_PUBLIC)) {
  2361. /* Explicitly copy the default value from the parent (if it has one) */
  2362. zval **pvalue;
  2363. if (zend_hash_quick_find(&parent_ce->default_properties, parent_info->name, parent_info->name_length+1, parent_info->h, (void **) &pvalue) == SUCCESS) {
  2364. Z_ADDREF_PP(pvalue);
  2365. zend_hash_quick_del(&ce->default_properties, child_info->name, child_info->name_length+1, parent_info->h);
  2366. zend_hash_quick_update(&ce->default_properties, parent_info->name, parent_info->name_length+1, parent_info->h, pvalue, sizeof(zval *), NULL);
  2367. }
  2368. }
  2369. return 1; /* Inherit from the parent */
  2370. } else if ((child_info->flags & ZEND_ACC_PUBLIC) && (parent_info->flags & ZEND_ACC_PROTECTED)) {
  2371. char *prot_name;
  2372. int prot_name_length;
  2373. zend_mangle_property_name(&prot_name, &prot_name_length, "*", 1, child_info->name, child_info->name_length, ce->type & ZEND_INTERNAL_CLASS);
  2374. if (child_info->flags & ZEND_ACC_STATIC) {
  2375. zval **prop;
  2376. HashTable *ht;
  2377. if (parent_ce->type != ce->type) {
  2378. /* User class extends internal class */
  2379. TSRMLS_FETCH();
  2380. ht = CE_STATIC_MEMBERS(parent_ce);
  2381. } else {
  2382. ht = &parent_ce->default_static_members;
  2383. }
  2384. if (zend_hash_find(ht, prot_name, prot_name_length+1, (void**)&prop) == SUCCESS) {
  2385. zend_hash_del(&ce->default_static_members, prot_name, prot_name_length+1);
  2386. }
  2387. } else {
  2388. zend_hash_del(&ce->default_properties, prot_name, prot_name_length+1);
  2389. }
  2390. pefree(prot_name, ce->type & ZEND_INTERNAL_CLASS);
  2391. }
  2392. return 0; /* Don't copy from parent */
  2393. } else {
  2394. return 1; /* Copy from parent */
  2395. }
  2396. }
  2397. /* }}} */
  2398. static inline void do_implement_interface(zend_class_entry *ce, zend_class_entry *iface TSRMLS_DC) /* {{{ */
  2399. {
  2400. if (!(ce->ce_flags & ZEND_ACC_INTERFACE) && iface->interface_gets_implemented && iface->interface_gets_implemented(iface, ce TSRMLS_CC) == FAILURE) {
  2401. zend_error(E_CORE_ERROR, "Class %s could not implement interface %s", ce->name, iface->name);
  2402. }
  2403. if (ce == iface) {
  2404. zend_error(E_ERROR, "Interface %s cannot implement itself", ce->name);
  2405. }
  2406. }
  2407. /* }}} */
  2408. ZEND_API void zend_do_inherit_interfaces(zend_class_entry *ce, const zend_class_entry *iface TSRMLS_DC) /* {{{ */
  2409. {
  2410. /* expects interface to be contained in ce's interface list already */
  2411. zend_uint i, ce_num, if_num = iface->num_interfaces;
  2412. zend_class_entry *entry;
  2413. if (if_num==0) {
  2414. return;
  2415. }
  2416. ce_num = ce->num_interfaces;
  2417. if (ce->type == ZEND_INTERNAL_CLASS) {
  2418. ce->interfaces = (zend_class_entry **) realloc(ce->interfaces, sizeof(zend_class_entry *) * (ce_num + if_num));
  2419. } else {
  2420. ce->interfaces = (zend_class_entry **) erealloc(ce->interfaces, sizeof(zend_class_entry *) * (ce_num + if_num));
  2421. }
  2422. /* Inherit the interfaces, only if they're not already inherited by the class */
  2423. while (if_num--) {
  2424. entry = iface->interfaces[if_num];
  2425. for (i = 0; i < ce_num; i++) {
  2426. if (ce->interfaces[i] == entry) {
  2427. break;
  2428. }
  2429. }
  2430. if (i == ce_num) {
  2431. ce->interfaces[ce->num_interfaces++] = entry;
  2432. }
  2433. }
  2434. /* and now call the implementing handlers */
  2435. while (ce_num < ce->num_interfaces) {
  2436. do_implement_interface(ce, ce->interfaces[ce_num++] TSRMLS_CC);
  2437. }
  2438. }
  2439. /* }}} */
  2440. static int inherit_static_prop(zval **p TSRMLS_DC, int num_args, va_list args, const zend_hash_key *key) /* {{{ */
  2441. {
  2442. HashTable *target = va_arg(args, HashTable*);
  2443. if (!zend_hash_quick_exists(target, key->arKey, key->nKeyLength, key->h)) {
  2444. SEPARATE_ZVAL_TO_MAKE_IS_REF(p);
  2445. if (zend_hash_quick_add(target, key->arKey, key->nKeyLength, key->h, p, sizeof(zval*), NULL) == SUCCESS) {
  2446. Z_ADDREF_PP(p);
  2447. }
  2448. }
  2449. return ZEND_HASH_APPLY_KEEP;
  2450. }
  2451. /* }}} */
  2452. #define zval_property_ctor(parent_ce, ce) \
  2453. ((copy_ctor_func_t) (((parent_ce)->type != (ce)->type) ? zval_shared_property_ctor : zval_add_ref))
  2454. ZEND_API void zend_do_inheritance(zend_class_entry *ce, zend_class_entry *parent_ce TSRMLS_DC) /* {{{ */
  2455. {
  2456. if ((ce->ce_flags & ZEND_ACC_INTERFACE)
  2457. && !(parent_ce->ce_flags & ZEND_ACC_INTERFACE)) {
  2458. zend_error(E_COMPILE_ERROR, "Interface %s may not inherit from class (%s)", ce->name, parent_ce->name);
  2459. }
  2460. if (parent_ce->ce_flags & ZEND_ACC_FINAL_CLASS) {
  2461. zend_error(E_COMPILE_ERROR, "Class %s may not inherit from final class (%s)", ce->name, parent_ce->name);
  2462. }
  2463. ce->parent = parent_ce;
  2464. /* Copy serialize/unserialize callbacks */
  2465. if (!ce->serialize) {
  2466. ce->serialize = parent_ce->serialize;
  2467. }
  2468. if (!ce->unserialize) {
  2469. ce->unserialize = parent_ce->unserialize;
  2470. }
  2471. /* Inherit interfaces */
  2472. zend_do_inherit_interfaces(ce, parent_ce TSRMLS_CC);
  2473. /* Inherit properties */
  2474. zend_hash_merge(&ce->default_properties, &parent_ce->default_properties, zval_property_ctor(parent_ce, ce), NULL, sizeof(zval *), 0);
  2475. if (parent_ce->type != ce->type) {
  2476. /* User class extends internal class */
  2477. zend_update_class_constants(parent_ce TSRMLS_CC);
  2478. zend_hash_apply_with_arguments(CE_STATIC_MEMBERS(parent_ce) TSRMLS_CC, (apply_func_args_t)inherit_static_prop, 1, &ce->default_static_members);
  2479. } else {
  2480. zend_hash_apply_with_arguments(&parent_ce->default_static_members TSRMLS_CC, (apply_func_args_t)inherit_static_prop, 1, &ce->default_static_members);
  2481. }
  2482. zend_hash_merge_ex(&ce->properties_info, &parent_ce->properties_info, (copy_ctor_func_t) (ce->type & ZEND_INTERNAL_CLASS ? zend_duplicate_property_info_internal : zend_duplicate_property_info), sizeof(zend_property_info), (merge_checker_func_t) do_inherit_property_access_check, ce);
  2483. zend_hash_merge(&ce->constants_table, &parent_ce->constants_table, zval_property_ctor(parent_ce, ce), NULL, sizeof(zval *), 0);
  2484. zend_hash_merge_ex(&ce->function_table, &parent_ce->function_table, (copy_ctor_func_t) do_inherit_method, sizeof(zend_function), (merge_checker_func_t) do_inherit_method_check, ce);
  2485. do_inherit_parent_constructor(ce);
  2486. if (ce->ce_flags & ZEND_ACC_IMPLICIT_ABSTRACT_CLASS && ce->type == ZEND_INTERNAL_CLASS) {
  2487. ce->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;
  2488. } else if (!(ce->ce_flags & ZEND_ACC_IMPLEMENT_INTERFACES)) {
  2489. /* The verification will be done in runtime by ZEND_VERIFY_ABSTRACT_CLASS */
  2490. zend_verify_abstract_class(ce TSRMLS_CC);
  2491. }
  2492. }
  2493. /* }}} */
  2494. static zend_bool do_inherit_constant_check(HashTable *child_constants_table, const zval **parent_constant, const zend_hash_key *hash_key, const zend_class_entry *iface) /* {{{ */
  2495. {
  2496. zval **old_constant;
  2497. if (zend_hash_quick_find(child_constants_table, hash_key->arKey, hash_key->nKeyLength, hash_key->h, (void**)&old_constant) == SUCCESS) {
  2498. if (*old_constant != *parent_constant) {
  2499. zend_error(E_COMPILE_ERROR, "Cannot inherit previously-inherited or override constant %s from interface %s", hash_key->arKey, iface->name);
  2500. }
  2501. return 0;
  2502. }
  2503. return 1;
  2504. }
  2505. /* }}} */
  2506. static int do_interface_constant_check(zval **val TSRMLS_DC, int num_args, va_list args, const zend_hash_key *key) /* {{{ */
  2507. {
  2508. zend_class_entry **iface = va_arg(args, zend_class_entry**);
  2509. do_inherit_constant_check(&(*iface)->constants_table, (const zval **) val, key, *iface);
  2510. return ZEND_HASH_APPLY_KEEP;
  2511. }
  2512. /* }}} */
  2513. ZEND_API void zend_do_implement_interface(zend_class_entry *ce, zend_class_entry *iface TSRMLS_DC) /* {{{ */
  2514. {
  2515. zend_uint i, ignore = 0;
  2516. zend_uint current_iface_num = ce->num_interfaces;
  2517. zend_uint parent_iface_num = ce->parent ? ce->parent->num_interfaces : 0;
  2518. for (i = 0; i < ce->num_interfaces; i++) {
  2519. if (ce->interfaces[i] == NULL) {
  2520. memmove(ce->interfaces + i, ce->interfaces + i + 1, sizeof(zend_class_entry*) * (--ce->num_interfaces - i));
  2521. i--;
  2522. } else if (ce->interfaces[i] == iface) {
  2523. if (i < parent_iface_num) {
  2524. ignore = 1;
  2525. } else {
  2526. zend_error(E_COMPILE_ERROR, "Class %s cannot implement previously implemented interface %s", ce->name, iface->name);
  2527. }
  2528. }
  2529. }
  2530. if (ignore) {
  2531. /* Check for attempt to redeclare interface constants */
  2532. zend_hash_apply_with_arguments(&ce->constants_table TSRMLS_CC, (apply_func_args_t) do_interface_constant_check, 1, &iface);
  2533. } else {
  2534. if (ce->num_interfaces >= current_iface_num) {
  2535. if (ce->type == ZEND_INTERNAL_CLASS) {
  2536. ce->interfaces = (zend_class_entry **) realloc(ce->interfaces, sizeof(zend_class_entry *) * (++current_iface_num));
  2537. } else {
  2538. ce->interfaces = (zend_class_entry **) erealloc(ce->interfaces, sizeof(zend_class_entry *) * (++current_iface_num));
  2539. }
  2540. }
  2541. ce->interfaces[ce->num_interfaces++] = iface;
  2542. zend_hash_merge_ex(&ce->constants_table, &iface->constants_table, (copy_ctor_func_t) zval_add_ref, sizeof(zval *), (merge_checker_func_t) do_inherit_constant_check, iface);
  2543. zend_hash_merge_ex(&ce->function_table, &iface->function_table, (copy_ctor_func_t) do_inherit_method, sizeof(zend_function), (merge_checker_func_t) do_inherit_method_check, ce);
  2544. do_implement_interface(ce, iface TSRMLS_CC);
  2545. zend_do_inherit_interfaces(ce, iface TSRMLS_CC);
  2546. }
  2547. }
  2548. /* }}} */
  2549. ZEND_API int do_bind_function(zend_op *opline, HashTable *function_table, zend_bool compile_time) /* {{{ */
  2550. {
  2551. zend_function *function;
  2552. zend_hash_find(function_table, opline->op1.u.constant.value.str.val, opline->op1.u.constant.value.str.len, (void *) &function);
  2553. if (zend_hash_add(function_table, opline->op2.u.constant.value.str.val, opline->op2.u.constant.value.str.len+1, function, sizeof(zend_function), NULL)==FAILURE) {
  2554. int error_level = compile_time ? E_COMPILE_ERROR : E_ERROR;
  2555. zend_function *old_function;
  2556. if (zend_hash_find(function_table, opline->op2.u.constant.value.str.val, opline->op2.u.constant.value.str.len+1, (void *) &old_function)==SUCCESS
  2557. && old_function->type == ZEND_USER_FUNCTION
  2558. && old_function->op_array.last > 0) {
  2559. zend_error(error_level, "Cannot redeclare %s() (previously declared in %s:%d)",
  2560. function->common.function_name,
  2561. old_function->op_array.filename,
  2562. old_function->op_array.opcodes[0].lineno);
  2563. } else {
  2564. zend_error(error_level, "Cannot redeclare %s()", function->common.function_name);
  2565. }
  2566. return FAILURE;
  2567. } else {
  2568. (*function->op_array.refcount)++;
  2569. function->op_array.static_variables = NULL; /* NULL out the unbound function */
  2570. return SUCCESS;
  2571. }
  2572. }
  2573. /* }}} */
  2574. ZEND_API zend_class_entry *do_bind_class(const zend_op *opline, HashTable *class_table, zend_bool compile_time TSRMLS_DC) /* {{{ */
  2575. {
  2576. zend_class_entry *ce, **pce;
  2577. if (zend_hash_find(class_table, opline->op1.u.constant.value.str.val, opline->op1.u.constant.value.str.len, (void **) &pce)==FAILURE) {
  2578. zend_error(E_COMPILE_ERROR, "Internal Zend error - Missing class information for %s", opline->op1.u.constant.value.str.val);
  2579. return NULL;
  2580. } else {
  2581. ce = *pce;
  2582. }
  2583. ce->refcount++;
  2584. if (zend_hash_add(class_table, opline->op2.u.constant.value.str.val, opline->op2.u.constant.value.str.len+1, &ce, sizeof(zend_class_entry *), NULL)==FAILURE) {
  2585. ce->refcount--;
  2586. if (!compile_time) {
  2587. /* If we're in compile time, in practice, it's quite possible
  2588. * that we'll never reach this class declaration at runtime,
  2589. * so we shut up about it. This allows the if (!defined('FOO')) { return; }
  2590. * approach to work.
  2591. */
  2592. zend_error(E_COMPILE_ERROR, "Cannot redeclare class %s", ce->name);
  2593. }
  2594. return NULL;
  2595. } else {
  2596. if (!(ce->ce_flags & (ZEND_ACC_INTERFACE|ZEND_ACC_IMPLEMENT_INTERFACES))) {
  2597. zend_verify_abstract_class(ce TSRMLS_CC);
  2598. }
  2599. return ce;
  2600. }
  2601. }
  2602. /* }}} */
  2603. ZEND_API zend_class_entry *do_bind_inherited_class(const zend_op *opline, HashTable *class_table, zend_class_entry *parent_ce, zend_bool compile_time TSRMLS_DC) /* {{{ */
  2604. {
  2605. zend_class_entry *ce, **pce;
  2606. int found_ce;
  2607. found_ce = zend_hash_find(class_table, opline->op1.u.constant.value.str.val, opline->op1.u.constant.value.str.len, (void **) &pce);
  2608. if (found_ce == FAILURE) {
  2609. if (!compile_time) {
  2610. /* If we're in compile time, in practice, it's quite possible
  2611. * that we'll never reach this class declaration at runtime,
  2612. * so we shut up about it. This allows the if (!defined('FOO')) { return; }
  2613. * approach to work.
  2614. */
  2615. zend_error(E_COMPILE_ERROR, "Cannot redeclare class %s", opline->op2.u.constant.value.str.val);
  2616. }
  2617. return NULL;
  2618. } else {
  2619. ce = *pce;
  2620. }
  2621. if (parent_ce->ce_flags & ZEND_ACC_INTERFACE) {
  2622. zend_error(E_COMPILE_ERROR, "Class %s cannot extend from interface %s", ce->name, parent_ce->name);
  2623. }
  2624. zend_do_inheritance(ce, parent_ce TSRMLS_CC);
  2625. ce->refcount++;
  2626. /* Register the derived class */
  2627. if (zend_hash_add(class_table, opline->op2.u.constant.value.str.val, opline->op2.u.constant.value.str.len+1, pce, sizeof(zend_class_entry *), NULL)==FAILURE) {
  2628. zend_error(E_COMPILE_ERROR, "Cannot redeclare class %s", ce->name);
  2629. }
  2630. return ce;
  2631. }
  2632. /* }}} */
  2633. void zend_do_early_binding(TSRMLS_D) /* {{{ */
  2634. {
  2635. zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
  2636. HashTable *table;
  2637. while (opline->opcode == ZEND_TICKS && opline > CG(active_op_array)->opcodes) {
  2638. opline--;
  2639. }
  2640. switch (opline->opcode) {
  2641. case ZEND_DECLARE_FUNCTION:
  2642. if (do_bind_function(opline, CG(function_table), 1) == FAILURE) {
  2643. return;
  2644. }
  2645. table = CG(function_table);
  2646. break;
  2647. case ZEND_DECLARE_CLASS:
  2648. if (do_bind_class(opline, CG(class_table), 1 TSRMLS_CC) == NULL) {
  2649. return;
  2650. }
  2651. table = CG(class_table);
  2652. break;
  2653. case ZEND_DECLARE_INHERITED_CLASS:
  2654. {
  2655. zend_op *fetch_class_opline = opline-1;
  2656. zval *parent_name = &fetch_class_opline->op2.u.constant;
  2657. zend_class_entry **pce;
  2658. if ((zend_lookup_class(Z_STRVAL_P(parent_name), Z_STRLEN_P(parent_name), &pce TSRMLS_CC) == FAILURE) ||
  2659. ((CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_CLASSES) &&
  2660. ((*pce)->type == ZEND_INTERNAL_CLASS))) {
  2661. if (CG(compiler_options) & ZEND_COMPILE_DELAYED_BINDING) {
  2662. zend_uint *opline_num = &CG(active_op_array)->early_binding;
  2663. while (*opline_num != -1) {
  2664. opline_num = &CG(active_op_array)->opcodes[*opline_num].result.u.opline_num;
  2665. }
  2666. *opline_num = opline - CG(active_op_array)->opcodes;
  2667. opline->opcode = ZEND_DECLARE_INHERITED_CLASS_DELAYED;
  2668. opline->result.op_type = IS_UNUSED;
  2669. opline->result.u.opline_num = -1;
  2670. }
  2671. return;
  2672. }
  2673. if (do_bind_inherited_class(opline, CG(class_table), *pce, 1 TSRMLS_CC) == NULL) {
  2674. return;
  2675. }
  2676. /* clear unnecessary ZEND_FETCH_CLASS opcode */
  2677. zval_dtor(&fetch_class_opline->op2.u.constant);
  2678. MAKE_NOP(fetch_class_opline);
  2679. table = CG(class_table);
  2680. break;
  2681. }
  2682. case ZEND_VERIFY_ABSTRACT_CLASS:
  2683. case ZEND_ADD_INTERFACE:
  2684. /* We currently don't early-bind classes that implement interfaces */
  2685. return;
  2686. default:
  2687. zend_error(E_COMPILE_ERROR, "Invalid binding type");
  2688. return;
  2689. }
  2690. zend_hash_del(table, opline->op1.u.constant.value.str.val, opline->op1.u.constant.value.str.len);
  2691. zval_dtor(&opline->op1.u.constant);
  2692. zval_dtor(&opline->op2.u.constant);
  2693. MAKE_NOP(opline);
  2694. }
  2695. /* }}} */
  2696. ZEND_API void zend_do_delayed_early_binding(const zend_op_array *op_array TSRMLS_DC) /* {{{ */
  2697. {
  2698. if (op_array->early_binding != -1) {
  2699. zend_bool orig_in_compilation = CG(in_compilation);
  2700. zend_uint opline_num = op_array->early_binding;
  2701. zend_class_entry **pce;
  2702. CG(in_compilation) = 1;
  2703. while (opline_num != -1) {
  2704. if (zend_lookup_class(Z_STRVAL(op_array->opcodes[opline_num-1].op2.u.constant), Z_STRLEN(op_array->opcodes[opline_num-1].op2.u.constant), &pce TSRMLS_CC) == SUCCESS) {
  2705. do_bind_inherited_class(&op_array->opcodes[opline_num], EG(class_table), *pce, 1 TSRMLS_CC);
  2706. }
  2707. opline_num = op_array->opcodes[opline_num].result.u.opline_num;
  2708. }
  2709. CG(in_compilation) = orig_in_compilation;
  2710. }
  2711. }
  2712. /* }}} */
  2713. void zend_do_boolean_or_begin(znode *expr1, znode *op_token TSRMLS_DC) /* {{{ */
  2714. {
  2715. int next_op_number = get_next_op_number(CG(active_op_array));
  2716. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  2717. opline->opcode = ZEND_JMPNZ_EX;
  2718. if (expr1->op_type == IS_TMP_VAR) {
  2719. opline->result = *expr1;
  2720. } else {
  2721. opline->result.u.var = get_temporary_variable(CG(active_op_array));
  2722. opline->result.op_type = IS_TMP_VAR;
  2723. }
  2724. opline->op1 = *expr1;
  2725. SET_UNUSED(opline->op2);
  2726. op_token->u.opline_num = next_op_number;
  2727. *expr1 = opline->result;
  2728. }
  2729. /* }}} */
  2730. void zend_do_boolean_or_end(znode *result, const znode *expr1, const znode *expr2, znode *op_token TSRMLS_DC) /* {{{ */
  2731. {
  2732. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  2733. *result = *expr1; /* we saved the original result in expr1 */
  2734. opline->opcode = ZEND_BOOL;
  2735. opline->result = *result;
  2736. opline->op1 = *expr2;
  2737. SET_UNUSED(opline->op2);
  2738. CG(active_op_array)->opcodes[op_token->u.opline_num].op2.u.opline_num = get_next_op_number(CG(active_op_array));
  2739. }
  2740. /* }}} */
  2741. void zend_do_boolean_and_begin(znode *expr1, znode *op_token TSRMLS_DC) /* {{{ */
  2742. {
  2743. int next_op_number = get_next_op_number(CG(active_op_array));
  2744. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  2745. opline->opcode = ZEND_JMPZ_EX;
  2746. if (expr1->op_type == IS_TMP_VAR) {
  2747. opline->result = *expr1;
  2748. } else {
  2749. opline->result.u.var = get_temporary_variable(CG(active_op_array));
  2750. opline->result.op_type = IS_TMP_VAR;
  2751. }
  2752. opline->op1 = *expr1;
  2753. SET_UNUSED(opline->op2);
  2754. op_token->u.opline_num = next_op_number;
  2755. *expr1 = opline->result;
  2756. }
  2757. /* }}} */
  2758. void zend_do_boolean_and_end(znode *result, const znode *expr1, const znode *expr2, const znode *op_token TSRMLS_DC) /* {{{ */
  2759. {
  2760. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  2761. *result = *expr1; /* we saved the original result in expr1 */
  2762. opline->opcode = ZEND_BOOL;
  2763. opline->result = *result;
  2764. opline->op1 = *expr2;
  2765. SET_UNUSED(opline->op2);
  2766. CG(active_op_array)->opcodes[op_token->u.opline_num].op2.u.opline_num = get_next_op_number(CG(active_op_array));
  2767. }
  2768. /* }}} */
  2769. void zend_do_do_while_begin(TSRMLS_D) /* {{{ */
  2770. {
  2771. do_begin_loop(TSRMLS_C);
  2772. INC_BPC(CG(active_op_array));
  2773. }
  2774. /* }}} */
  2775. void zend_do_do_while_end(const znode *do_token, const znode *expr_open_bracket, const znode *expr TSRMLS_DC) /* {{{ */
  2776. {
  2777. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  2778. opline->opcode = ZEND_JMPNZ;
  2779. opline->op1 = *expr;
  2780. opline->op2.u.opline_num = do_token->u.opline_num;
  2781. SET_UNUSED(opline->op2);
  2782. do_end_loop(expr_open_bracket->u.opline_num, 0 TSRMLS_CC);
  2783. DEC_BPC(CG(active_op_array));
  2784. }
  2785. /* }}} */
  2786. void zend_do_brk_cont(zend_uchar op, const znode *expr TSRMLS_DC) /* {{{ */
  2787. {
  2788. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  2789. opline->opcode = op;
  2790. opline->op1.u.opline_num = CG(active_op_array)->current_brk_cont;
  2791. SET_UNUSED(opline->op1);
  2792. if (expr) {
  2793. opline->op2 = *expr;
  2794. } else {
  2795. Z_TYPE(opline->op2.u.constant) = IS_LONG;
  2796. Z_LVAL(opline->op2.u.constant) = 1;
  2797. INIT_PZVAL(&opline->op2.u.constant);
  2798. opline->op2.op_type = IS_CONST;
  2799. }
  2800. }
  2801. /* }}} */
  2802. void zend_do_switch_cond(const znode *cond TSRMLS_DC) /* {{{ */
  2803. {
  2804. zend_switch_entry switch_entry;
  2805. switch_entry.cond = *cond;
  2806. switch_entry.default_case = -1;
  2807. switch_entry.control_var = -1;
  2808. zend_stack_push(&CG(switch_cond_stack), (void *) &switch_entry, sizeof(switch_entry));
  2809. do_begin_loop(TSRMLS_C);
  2810. INC_BPC(CG(active_op_array));
  2811. }
  2812. /* }}} */
  2813. void zend_do_switch_end(const znode *case_list TSRMLS_DC) /* {{{ */
  2814. {
  2815. zend_op *opline;
  2816. zend_switch_entry *switch_entry_ptr;
  2817. zend_stack_top(&CG(switch_cond_stack), (void **) &switch_entry_ptr);
  2818. /* add code to jmp to default case */
  2819. if (switch_entry_ptr->default_case != -1) {
  2820. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  2821. opline->opcode = ZEND_JMP;
  2822. SET_UNUSED(opline->op1);
  2823. SET_UNUSED(opline->op2);
  2824. opline->op1.u.opline_num = switch_entry_ptr->default_case;
  2825. }
  2826. if (case_list->op_type != IS_UNUSED) { /* non-empty switch */
  2827. int next_op_number = get_next_op_number(CG(active_op_array));
  2828. CG(active_op_array)->opcodes[case_list->u.opline_num].op1.u.opline_num = next_op_number;
  2829. }
  2830. /* remember break/continue loop information */
  2831. CG(active_op_array)->brk_cont_array[CG(active_op_array)->current_brk_cont].cont = CG(active_op_array)->brk_cont_array[CG(active_op_array)->current_brk_cont].brk = get_next_op_number(CG(active_op_array));
  2832. CG(active_op_array)->current_brk_cont = CG(active_op_array)->brk_cont_array[CG(active_op_array)->current_brk_cont].parent;
  2833. if (switch_entry_ptr->cond.op_type==IS_VAR || switch_entry_ptr->cond.op_type==IS_TMP_VAR) {
  2834. /* emit free for the switch condition*/
  2835. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  2836. opline->opcode = (switch_entry_ptr->cond.op_type == IS_TMP_VAR) ? ZEND_FREE : ZEND_SWITCH_FREE;
  2837. opline->op1 = switch_entry_ptr->cond;
  2838. SET_UNUSED(opline->op2);
  2839. }
  2840. if (switch_entry_ptr->cond.op_type == IS_CONST) {
  2841. zval_dtor(&switch_entry_ptr->cond.u.constant);
  2842. }
  2843. zend_stack_del_top(&CG(switch_cond_stack));
  2844. DEC_BPC(CG(active_op_array));
  2845. }
  2846. /* }}} */
  2847. void zend_do_case_before_statement(const znode *case_list, znode *case_token, const znode *case_expr TSRMLS_DC) /* {{{ */
  2848. {
  2849. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  2850. int next_op_number;
  2851. zend_switch_entry *switch_entry_ptr;
  2852. znode result;
  2853. zend_stack_top(&CG(switch_cond_stack), (void **) &switch_entry_ptr);
  2854. if (switch_entry_ptr->control_var == -1) {
  2855. switch_entry_ptr->control_var = get_temporary_variable(CG(active_op_array));
  2856. }
  2857. opline->opcode = ZEND_CASE;
  2858. opline->result.u.var = switch_entry_ptr->control_var;
  2859. opline->result.op_type = IS_TMP_VAR;
  2860. opline->op1 = switch_entry_ptr->cond;
  2861. opline->op2 = *case_expr;
  2862. if (opline->op1.op_type == IS_CONST) {
  2863. zval_copy_ctor(&opline->op1.u.constant);
  2864. }
  2865. result = opline->result;
  2866. next_op_number = get_next_op_number(CG(active_op_array));
  2867. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  2868. opline->opcode = ZEND_JMPZ;
  2869. opline->op1 = result;
  2870. SET_UNUSED(opline->op2);
  2871. case_token->u.opline_num = next_op_number;
  2872. if (case_list->op_type==IS_UNUSED) {
  2873. return;
  2874. }
  2875. next_op_number = get_next_op_number(CG(active_op_array));
  2876. CG(active_op_array)->opcodes[case_list->u.opline_num].op1.u.opline_num = next_op_number;
  2877. }
  2878. /* }}} */
  2879. void zend_do_case_after_statement(znode *result, const znode *case_token TSRMLS_DC) /* {{{ */
  2880. {
  2881. int next_op_number = get_next_op_number(CG(active_op_array));
  2882. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  2883. opline->opcode = ZEND_JMP;
  2884. SET_UNUSED(opline->op1);
  2885. SET_UNUSED(opline->op2);
  2886. result->u.opline_num = next_op_number;
  2887. switch (CG(active_op_array)->opcodes[case_token->u.opline_num].opcode) {
  2888. case ZEND_JMP:
  2889. CG(active_op_array)->opcodes[case_token->u.opline_num].op1.u.opline_num = get_next_op_number(CG(active_op_array));
  2890. break;
  2891. case ZEND_JMPZ:
  2892. CG(active_op_array)->opcodes[case_token->u.opline_num].op2.u.opline_num = get_next_op_number(CG(active_op_array));
  2893. break;
  2894. }
  2895. }
  2896. /* }}} */
  2897. void zend_do_default_before_statement(const znode *case_list, znode *default_token TSRMLS_DC) /* {{{ */
  2898. {
  2899. int next_op_number = get_next_op_number(CG(active_op_array));
  2900. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  2901. zend_switch_entry *switch_entry_ptr;
  2902. zend_stack_top(&CG(switch_cond_stack), (void **) &switch_entry_ptr);
  2903. opline->opcode = ZEND_JMP;
  2904. SET_UNUSED(opline->op1);
  2905. SET_UNUSED(opline->op2);
  2906. default_token->u.opline_num = next_op_number;
  2907. next_op_number = get_next_op_number(CG(active_op_array));
  2908. switch_entry_ptr->default_case = next_op_number;
  2909. if (case_list->op_type==IS_UNUSED) {
  2910. return;
  2911. }
  2912. CG(active_op_array)->opcodes[case_list->u.opline_num].op1.u.opline_num = next_op_number;
  2913. }
  2914. /* }}} */
  2915. void zend_do_begin_class_declaration(const znode *class_token, znode *class_name, const znode *parent_class_name TSRMLS_DC) /* {{{ */
  2916. {
  2917. zend_op *opline;
  2918. int doing_inheritance = 0;
  2919. zend_class_entry *new_class_entry;
  2920. char *lcname;
  2921. int error = 0;
  2922. zval **ns_name;
  2923. if (CG(active_class_entry)) {
  2924. zend_error(E_COMPILE_ERROR, "Class declarations may not be nested");
  2925. return;
  2926. }
  2927. lcname = zend_str_tolower_dup(class_name->u.constant.value.str.val, class_name->u.constant.value.str.len);
  2928. if (!(strcmp(lcname, "self") && strcmp(lcname, "parent"))) {
  2929. efree(lcname);
  2930. zend_error(E_COMPILE_ERROR, "Cannot use '%s' as class name as it is reserved", class_name->u.constant.value.str.val);
  2931. }
  2932. /* Class name must not conflict with import names */
  2933. if (CG(current_import) &&
  2934. zend_hash_find(CG(current_import), lcname, Z_STRLEN(class_name->u.constant)+1, (void**)&ns_name) == SUCCESS) {
  2935. error = 1;
  2936. }
  2937. if (CG(current_namespace)) {
  2938. /* Prefix class name with name of current namespace */
  2939. znode tmp;
  2940. tmp.u.constant = *CG(current_namespace);
  2941. zval_copy_ctor(&tmp.u.constant);
  2942. zend_do_build_namespace_name(&tmp, &tmp, class_name TSRMLS_CC);
  2943. class_name = &tmp;
  2944. efree(lcname);
  2945. lcname = zend_str_tolower_dup(Z_STRVAL(class_name->u.constant), Z_STRLEN(class_name->u.constant));
  2946. }
  2947. if (error) {
  2948. char *tmp = zend_str_tolower_dup(Z_STRVAL_PP(ns_name), Z_STRLEN_PP(ns_name));
  2949. if (Z_STRLEN_PP(ns_name) != Z_STRLEN(class_name->u.constant) ||
  2950. memcmp(tmp, lcname, Z_STRLEN(class_name->u.constant))) {
  2951. zend_error(E_COMPILE_ERROR, "Cannot declare class %s because the name is already in use", Z_STRVAL(class_name->u.constant));
  2952. }
  2953. efree(tmp);
  2954. }
  2955. new_class_entry = emalloc(sizeof(zend_class_entry));
  2956. new_class_entry->type = ZEND_USER_CLASS;
  2957. new_class_entry->name = class_name->u.constant.value.str.val;
  2958. new_class_entry->name_length = class_name->u.constant.value.str.len;
  2959. zend_initialize_class_data(new_class_entry, 1 TSRMLS_CC);
  2960. new_class_entry->filename = zend_get_compiled_filename(TSRMLS_C);
  2961. new_class_entry->line_start = class_token->u.opline_num;
  2962. new_class_entry->ce_flags |= class_token->u.EA.type;
  2963. if (parent_class_name && parent_class_name->op_type != IS_UNUSED) {
  2964. switch (parent_class_name->u.EA.type) {
  2965. case ZEND_FETCH_CLASS_SELF:
  2966. zend_error(E_COMPILE_ERROR, "Cannot use 'self' as class name as it is reserved");
  2967. break;
  2968. case ZEND_FETCH_CLASS_PARENT:
  2969. zend_error(E_COMPILE_ERROR, "Cannot use 'parent' as class name as it is reserved");
  2970. break;
  2971. case ZEND_FETCH_CLASS_STATIC:
  2972. zend_error(E_COMPILE_ERROR, "Cannot use 'static' as class name as it is reserved");
  2973. break;
  2974. default:
  2975. break;
  2976. }
  2977. doing_inheritance = 1;
  2978. }
  2979. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  2980. opline->op1.op_type = IS_CONST;
  2981. build_runtime_defined_function_key(&opline->op1.u.constant, lcname, new_class_entry->name_length TSRMLS_CC);
  2982. opline->op2.op_type = IS_CONST;
  2983. opline->op2.u.constant.type = IS_STRING;
  2984. Z_SET_REFCOUNT(opline->op2.u.constant, 1);
  2985. if (doing_inheritance) {
  2986. opline->extended_value = parent_class_name->u.var;
  2987. opline->opcode = ZEND_DECLARE_INHERITED_CLASS;
  2988. } else {
  2989. opline->opcode = ZEND_DECLARE_CLASS;
  2990. }
  2991. opline->op2.u.constant.value.str.val = lcname;
  2992. opline->op2.u.constant.value.str.len = new_class_entry->name_length;
  2993. zend_hash_update(CG(class_table), opline->op1.u.constant.value.str.val, opline->op1.u.constant.value.str.len, &new_class_entry, sizeof(zend_class_entry *), NULL);
  2994. CG(active_class_entry) = new_class_entry;
  2995. opline->result.u.var = get_temporary_variable(CG(active_op_array));
  2996. opline->result.op_type = IS_VAR;
  2997. CG(implementing_class) = opline->result;
  2998. if (CG(doc_comment)) {
  2999. CG(active_class_entry)->doc_comment = CG(doc_comment);
  3000. CG(active_class_entry)->doc_comment_len = CG(doc_comment_len);
  3001. CG(doc_comment) = NULL;
  3002. CG(doc_comment_len) = 0;
  3003. }
  3004. }
  3005. /* }}} */
  3006. static void do_verify_abstract_class(TSRMLS_D) /* {{{ */
  3007. {
  3008. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  3009. opline->opcode = ZEND_VERIFY_ABSTRACT_CLASS;
  3010. opline->op1 = CG(implementing_class);
  3011. SET_UNUSED(opline->op2);
  3012. }
  3013. /* }}} */
  3014. void zend_do_end_class_declaration(const znode *class_token, const znode *parent_token TSRMLS_DC) /* {{{ */
  3015. {
  3016. zend_class_entry *ce = CG(active_class_entry);
  3017. if (ce->constructor) {
  3018. ce->constructor->common.fn_flags |= ZEND_ACC_CTOR;
  3019. if (ce->constructor->common.fn_flags & ZEND_ACC_STATIC) {
  3020. zend_error(E_COMPILE_ERROR, "Constructor %s::%s() cannot be static", ce->name, ce->constructor->common.function_name);
  3021. }
  3022. }
  3023. if (ce->destructor) {
  3024. ce->destructor->common.fn_flags |= ZEND_ACC_DTOR;
  3025. if (ce->destructor->common.fn_flags & ZEND_ACC_STATIC) {
  3026. zend_error(E_COMPILE_ERROR, "Destructor %s::%s() cannot be static", ce->name, ce->destructor->common.function_name);
  3027. }
  3028. }
  3029. if (ce->clone) {
  3030. ce->clone->common.fn_flags |= ZEND_ACC_CLONE;
  3031. if (ce->clone->common.fn_flags & ZEND_ACC_STATIC) {
  3032. zend_error(E_COMPILE_ERROR, "Clone method %s::%s() cannot be static", ce->name, ce->clone->common.function_name);
  3033. }
  3034. }
  3035. ce->line_end = zend_get_compiled_lineno(TSRMLS_C);
  3036. if (!(ce->ce_flags & (ZEND_ACC_INTERFACE|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS))
  3037. && ((parent_token->op_type != IS_UNUSED) || (ce->num_interfaces > 0))) {
  3038. zend_verify_abstract_class(ce TSRMLS_CC);
  3039. if (ce->num_interfaces) {
  3040. do_verify_abstract_class(TSRMLS_C);
  3041. }
  3042. }
  3043. /* Inherit interfaces; reset number to zero, we need it for above check and
  3044. * will restore it during actual implementation.
  3045. * The ZEND_ACC_IMPLEMENT_INTERFACES flag disables double call to
  3046. * zend_verify_abstract_class() */
  3047. if (ce->num_interfaces > 0) {
  3048. ce->interfaces = NULL;
  3049. ce->num_interfaces = 0;
  3050. ce->ce_flags |= ZEND_ACC_IMPLEMENT_INTERFACES;
  3051. }
  3052. CG(active_class_entry) = NULL;
  3053. }
  3054. /* }}} */
  3055. void zend_do_implements_interface(znode *interface_name TSRMLS_DC) /* {{{ */
  3056. {
  3057. zend_op *opline;
  3058. switch (zend_get_class_fetch_type(Z_STRVAL(interface_name->u.constant), Z_STRLEN(interface_name->u.constant))) {
  3059. case ZEND_FETCH_CLASS_SELF:
  3060. case ZEND_FETCH_CLASS_PARENT:
  3061. case ZEND_FETCH_CLASS_STATIC:
  3062. zend_error(E_COMPILE_ERROR, "Cannot use '%s' as interface name as it is reserved", Z_STRVAL(interface_name->u.constant));
  3063. break;
  3064. default:
  3065. break;
  3066. }
  3067. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  3068. opline->opcode = ZEND_ADD_INTERFACE;
  3069. opline->op1 = CG(implementing_class);
  3070. zend_resolve_class_name(interface_name, &opline->extended_value, 0 TSRMLS_CC);
  3071. opline->extended_value = (opline->extended_value & ~ZEND_FETCH_CLASS_MASK) | ZEND_FETCH_CLASS_INTERFACE;
  3072. opline->op2 = *interface_name;
  3073. CG(active_class_entry)->num_interfaces++;
  3074. }
  3075. /* }}} */
  3076. ZEND_API void zend_mangle_property_name(char **dest, int *dest_length, const char *src1, int src1_length, const char *src2, int src2_length, int internal) /* {{{ */
  3077. {
  3078. char *prop_name;
  3079. int prop_name_length;
  3080. prop_name_length = 1 + src1_length + 1 + src2_length;
  3081. prop_name = pemalloc(prop_name_length + 1, internal);
  3082. prop_name[0] = '\0';
  3083. memcpy(prop_name + 1, src1, src1_length+1);
  3084. memcpy(prop_name + 1 + src1_length + 1, src2, src2_length+1);
  3085. *dest = prop_name;
  3086. *dest_length = prop_name_length;
  3087. }
  3088. /* }}} */
  3089. static int zend_strnlen(const char* s, int maxlen) /* {{{ */
  3090. {
  3091. int len = 0;
  3092. while (*s++ && maxlen--) len++;
  3093. return len;
  3094. }
  3095. /* }}} */
  3096. ZEND_API int zend_unmangle_property_name(char *mangled_property, int len, char **class_name, char **prop_name) /* {{{ */
  3097. {
  3098. int class_name_len;
  3099. *class_name = NULL;
  3100. if (mangled_property[0]!=0) {
  3101. *prop_name = mangled_property;
  3102. return SUCCESS;
  3103. }
  3104. if (len < 3 || mangled_property[1]==0) {
  3105. zend_error(E_NOTICE, "Illegal member variable name");
  3106. *prop_name = mangled_property;
  3107. return FAILURE;
  3108. }
  3109. class_name_len = zend_strnlen(mangled_property+1, --len - 1) + 1;
  3110. if (class_name_len >= len || mangled_property[class_name_len]!=0) {
  3111. zend_error(E_NOTICE, "Corrupt member variable name");
  3112. *prop_name = mangled_property;
  3113. return FAILURE;
  3114. }
  3115. *class_name = mangled_property+1;
  3116. *prop_name = (*class_name)+class_name_len;
  3117. return SUCCESS;
  3118. }
  3119. /* }}} */
  3120. void zend_do_declare_property(const znode *var_name, const znode *value, zend_uint access_type TSRMLS_DC) /* {{{ */
  3121. {
  3122. zval *property;
  3123. zend_property_info *existing_property_info;
  3124. char *comment = NULL;
  3125. int comment_len = 0;
  3126. if (CG(active_class_entry)->ce_flags & ZEND_ACC_INTERFACE) {
  3127. zend_error(E_COMPILE_ERROR, "Interfaces may not include member variables");
  3128. }
  3129. if (access_type & ZEND_ACC_ABSTRACT) {
  3130. zend_error(E_COMPILE_ERROR, "Properties cannot be declared abstract");
  3131. }
  3132. if (access_type & ZEND_ACC_FINAL) {
  3133. zend_error(E_COMPILE_ERROR, "Cannot declare property %s::$%s final, the final modifier is allowed only for methods and classes",
  3134. CG(active_class_entry)->name, var_name->u.constant.value.str.val);
  3135. }
  3136. if (zend_hash_find(&CG(active_class_entry)->properties_info, var_name->u.constant.value.str.val, var_name->u.constant.value.str.len+1, (void **) &existing_property_info)==SUCCESS) {
  3137. if (!(existing_property_info->flags & ZEND_ACC_IMPLICIT_PUBLIC)) {
  3138. zend_error(E_COMPILE_ERROR, "Cannot redeclare %s::$%s", CG(active_class_entry)->name, var_name->u.constant.value.str.val);
  3139. }
  3140. }
  3141. ALLOC_ZVAL(property);
  3142. if (value) {
  3143. *property = value->u.constant;
  3144. } else {
  3145. INIT_PZVAL(property);
  3146. Z_TYPE_P(property) = IS_NULL;
  3147. }
  3148. if (CG(doc_comment)) {
  3149. comment = CG(doc_comment);
  3150. comment_len = CG(doc_comment_len);
  3151. CG(doc_comment) = NULL;
  3152. CG(doc_comment_len) = 0;
  3153. }
  3154. zend_declare_property_ex(CG(active_class_entry), var_name->u.constant.value.str.val, var_name->u.constant.value.str.len, property, access_type, comment, comment_len TSRMLS_CC);
  3155. efree(var_name->u.constant.value.str.val);
  3156. }
  3157. /* }}} */
  3158. void zend_do_declare_class_constant(znode *var_name, const znode *value TSRMLS_DC) /* {{{ */
  3159. {
  3160. zval *property;
  3161. if(Z_TYPE(value->u.constant) == IS_CONSTANT_ARRAY) {
  3162. zend_error(E_COMPILE_ERROR, "Arrays are not allowed in class constants");
  3163. }
  3164. ALLOC_ZVAL(property);
  3165. *property = value->u.constant;
  3166. if (zend_hash_add(&CG(active_class_entry)->constants_table, var_name->u.constant.value.str.val, var_name->u.constant.value.str.len+1, &property, sizeof(zval *), NULL)==FAILURE) {
  3167. FREE_ZVAL(property);
  3168. zend_error(E_COMPILE_ERROR, "Cannot redefine class constant %s::%s", CG(active_class_entry)->name, var_name->u.constant.value.str.val);
  3169. }
  3170. FREE_PNODE(var_name);
  3171. if (CG(doc_comment)) {
  3172. efree(CG(doc_comment));
  3173. CG(doc_comment) = NULL;
  3174. CG(doc_comment_len) = 0;
  3175. }
  3176. }
  3177. /* }}} */
  3178. void zend_do_fetch_property(znode *result, znode *object, const znode *property TSRMLS_DC) /* {{{ */
  3179. {
  3180. zend_op opline;
  3181. zend_llist *fetch_list_ptr;
  3182. zend_stack_top(&CG(bp_stack), (void **) &fetch_list_ptr);
  3183. if (object->op_type == IS_CV) {
  3184. if (object->u.var == CG(active_op_array)->this_var) {
  3185. SET_UNUSED(*object); /* this means $this for objects */
  3186. }
  3187. } else if (fetch_list_ptr->count == 1) {
  3188. zend_llist_element *le = fetch_list_ptr->head;
  3189. zend_op *opline_ptr = (zend_op *) le->data;
  3190. if (opline_is_fetch_this(opline_ptr TSRMLS_CC)) {
  3191. efree(Z_STRVAL(opline_ptr->op1.u.constant));
  3192. SET_UNUSED(opline_ptr->op1); /* this means $this for objects */
  3193. opline_ptr->op2 = *property;
  3194. /* if it was usual fetch, we change it to object fetch */
  3195. switch (opline_ptr->opcode) {
  3196. case ZEND_FETCH_W:
  3197. opline_ptr->opcode = ZEND_FETCH_OBJ_W;
  3198. break;
  3199. case ZEND_FETCH_R:
  3200. opline_ptr->opcode = ZEND_FETCH_OBJ_R;
  3201. break;
  3202. case ZEND_FETCH_RW:
  3203. opline_ptr->opcode = ZEND_FETCH_OBJ_RW;
  3204. break;
  3205. case ZEND_FETCH_IS:
  3206. opline_ptr->opcode = ZEND_FETCH_OBJ_IS;
  3207. break;
  3208. case ZEND_FETCH_UNSET:
  3209. opline_ptr->opcode = ZEND_FETCH_OBJ_UNSET;
  3210. break;
  3211. case ZEND_FETCH_FUNC_ARG:
  3212. opline_ptr->opcode = ZEND_FETCH_OBJ_FUNC_ARG;
  3213. break;
  3214. }
  3215. *result = opline_ptr->result;
  3216. return;
  3217. }
  3218. }
  3219. init_op(&opline TSRMLS_CC);
  3220. opline.opcode = ZEND_FETCH_OBJ_W; /* the backpatching routine assumes W */
  3221. opline.result.op_type = IS_VAR;
  3222. opline.result.u.EA.type = 0;
  3223. opline.result.u.var = get_temporary_variable(CG(active_op_array));
  3224. opline.op1 = *object;
  3225. opline.op2 = *property;
  3226. *result = opline.result;
  3227. zend_llist_add_element(fetch_list_ptr, &opline);
  3228. }
  3229. /* }}} */
  3230. void zend_do_halt_compiler_register(TSRMLS_D) /* {{{ */
  3231. {
  3232. char *name, *cfilename;
  3233. char haltoff[] = "__COMPILER_HALT_OFFSET__";
  3234. int len, clen;
  3235. cfilename = zend_get_compiled_filename(TSRMLS_C);
  3236. clen = strlen(cfilename);
  3237. zend_mangle_property_name(&name, &len, haltoff, sizeof(haltoff) - 1, cfilename, clen, 0);
  3238. zend_register_long_constant(name, len+1, zend_get_scanned_file_offset(TSRMLS_C), CONST_CS, 0 TSRMLS_CC);
  3239. pefree(name, 0);
  3240. }
  3241. /* }}} */
  3242. void zend_do_declare_implicit_property(TSRMLS_D) /* {{{ */
  3243. {
  3244. /* Fixes bug #26182. Not sure why we needed to do this in the first place.
  3245. Has to be checked with Zeev.
  3246. */
  3247. #if ANDI_0
  3248. zend_op *opline_ptr;
  3249. zend_llist_element *le;
  3250. zend_llist *fetch_list_ptr;
  3251. zend_stack_top(&CG(bp_stack), (void **) &fetch_list_ptr);
  3252. if (fetch_list_ptr->count != 1) {
  3253. return;
  3254. }
  3255. le = fetch_list_ptr->head;
  3256. opline_ptr = (zend_op *) le->data;
  3257. if (opline_ptr->op1.op_type == IS_UNUSED
  3258. && CG(active_class_entry)
  3259. && opline_ptr->op2.op_type == IS_CONST
  3260. && !zend_hash_exists(&CG(active_class_entry)->properties_info, opline_ptr->op2.u.constant.value.str.val, opline_ptr->op2.u.constant.value.str.len+1)) {
  3261. znode property;
  3262. property = opline_ptr->op2;
  3263. property.u.constant.value.str.val = estrndup(opline_ptr->op2.u.constant.value.str.val, opline_ptr->op2.u.constant.value.str.len);
  3264. zend_do_declare_property(&property, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_IMPLICIT_PUBLIC TSRMLS_CC);
  3265. }
  3266. #endif
  3267. }
  3268. /* }}} */
  3269. void zend_do_push_object(const znode *object TSRMLS_DC) /* {{{ */
  3270. {
  3271. zend_stack_push(&CG(object_stack), object, sizeof(znode));
  3272. }
  3273. /* }}} */
  3274. void zend_do_pop_object(znode *object TSRMLS_DC) /* {{{ */
  3275. {
  3276. if (object) {
  3277. znode *tmp;
  3278. zend_stack_top(&CG(object_stack), (void **) &tmp);
  3279. *object = *tmp;
  3280. }
  3281. zend_stack_del_top(&CG(object_stack));
  3282. }
  3283. /* }}} */
  3284. void zend_do_begin_new_object(znode *new_token, znode *class_type TSRMLS_DC) /* {{{ */
  3285. {
  3286. zend_op *opline;
  3287. unsigned char *ptr = NULL;
  3288. new_token->u.opline_num = get_next_op_number(CG(active_op_array));
  3289. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  3290. opline->opcode = ZEND_NEW;
  3291. opline->result.op_type = IS_VAR;
  3292. opline->result.u.var = get_temporary_variable(CG(active_op_array));
  3293. opline->op1 = *class_type;
  3294. SET_UNUSED(opline->op2);
  3295. zend_stack_push(&CG(function_call_stack), (void *) &ptr, sizeof(unsigned char *));
  3296. }
  3297. /* }}} */
  3298. void zend_do_end_new_object(znode *result, const znode *new_token, const znode *argument_list TSRMLS_DC) /* {{{ */
  3299. {
  3300. znode ctor_result;
  3301. zend_do_end_function_call(NULL, &ctor_result, argument_list, 1, 0 TSRMLS_CC);
  3302. zend_do_free(&ctor_result TSRMLS_CC);
  3303. CG(active_op_array)->opcodes[new_token->u.opline_num].op2.u.opline_num = get_next_op_number(CG(active_op_array));
  3304. *result = CG(active_op_array)->opcodes[new_token->u.opline_num].result;
  3305. }
  3306. /* }}} */
  3307. static zend_constant* zend_get_ct_const(const zval *const_name, int all_internal_constants_substitution TSRMLS_DC) /* {{{ */
  3308. {
  3309. zend_constant *c = NULL;
  3310. if (Z_STRVAL_P(const_name)[0] == '\\') {
  3311. if (zend_hash_find(EG(zend_constants), Z_STRVAL_P(const_name)+1, Z_STRLEN_P(const_name), (void **) &c) == FAILURE) {
  3312. char *lookup_name = zend_str_tolower_dup(Z_STRVAL_P(const_name)+1, Z_STRLEN_P(const_name)-1);
  3313. if (zend_hash_find(EG(zend_constants), lookup_name, Z_STRLEN_P(const_name), (void **) &c)==SUCCESS) {
  3314. if ((c->flags & CONST_CT_SUBST) && !(c->flags & CONST_CS)) {
  3315. efree(lookup_name);
  3316. return c;
  3317. }
  3318. }
  3319. efree(lookup_name);
  3320. return NULL;
  3321. }
  3322. } else if (zend_hash_find(EG(zend_constants), Z_STRVAL_P(const_name), Z_STRLEN_P(const_name)+1, (void **) &c) == FAILURE) {
  3323. char *lookup_name = zend_str_tolower_dup(Z_STRVAL_P(const_name), Z_STRLEN_P(const_name));
  3324. if (zend_hash_find(EG(zend_constants), lookup_name, Z_STRLEN_P(const_name)+1, (void **) &c)==SUCCESS) {
  3325. if ((c->flags & CONST_CT_SUBST) && !(c->flags & CONST_CS)) {
  3326. efree(lookup_name);
  3327. return c;
  3328. }
  3329. }
  3330. efree(lookup_name);
  3331. return NULL;
  3332. }
  3333. if (c->flags & CONST_CT_SUBST) {
  3334. return c;
  3335. }
  3336. if (all_internal_constants_substitution &&
  3337. (c->flags & CONST_PERSISTENT) &&
  3338. !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION) &&
  3339. Z_TYPE(c->value) != IS_CONSTANT &&
  3340. Z_TYPE(c->value) != IS_CONSTANT_ARRAY) {
  3341. return c;
  3342. }
  3343. return NULL;
  3344. }
  3345. /* }}} */
  3346. static int zend_constant_ct_subst(znode *result, zval *const_name, int all_internal_constants_substitution TSRMLS_DC) /* {{{ */
  3347. {
  3348. zend_constant *c = zend_get_ct_const(const_name, all_internal_constants_substitution TSRMLS_CC);
  3349. if (c) {
  3350. zval_dtor(const_name);
  3351. result->op_type = IS_CONST;
  3352. result->u.constant = c->value;
  3353. zval_copy_ctor(&result->u.constant);
  3354. INIT_PZVAL(&result->u.constant);
  3355. return 1;
  3356. }
  3357. return 0;
  3358. }
  3359. /* }}} */
  3360. void zend_do_fetch_constant(znode *result, znode *constant_container, znode *constant_name, int mode, zend_bool check_namespace TSRMLS_DC) /* {{{ */
  3361. {
  3362. znode tmp;
  3363. zend_op *opline;
  3364. int type;
  3365. char *compound;
  3366. ulong fetch_type = 0;
  3367. if (constant_container) {
  3368. switch (mode) {
  3369. case ZEND_CT:
  3370. /* this is a class constant */
  3371. type = zend_get_class_fetch_type(Z_STRVAL(constant_container->u.constant), Z_STRLEN(constant_container->u.constant));
  3372. if (ZEND_FETCH_CLASS_STATIC == type) {
  3373. zend_error(E_ERROR, "\"static::\" is not allowed in compile-time constants");
  3374. } else if (ZEND_FETCH_CLASS_DEFAULT == type) {
  3375. zend_resolve_class_name(constant_container, &fetch_type, 1 TSRMLS_CC);
  3376. }
  3377. zend_do_build_full_name(NULL, constant_container, constant_name, 1 TSRMLS_CC);
  3378. *result = *constant_container;
  3379. result->u.constant.type = IS_CONSTANT | fetch_type;
  3380. break;
  3381. case ZEND_RT:
  3382. if (constant_container->op_type == IS_CONST &&
  3383. ZEND_FETCH_CLASS_DEFAULT == zend_get_class_fetch_type(Z_STRVAL(constant_container->u.constant), Z_STRLEN(constant_container->u.constant))) {
  3384. zend_resolve_class_name(constant_container, &fetch_type, 1 TSRMLS_CC);
  3385. } else {
  3386. zend_do_fetch_class(&tmp, constant_container TSRMLS_CC);
  3387. constant_container = &tmp;
  3388. }
  3389. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  3390. opline->opcode = ZEND_FETCH_CONSTANT;
  3391. opline->result.op_type = IS_TMP_VAR;
  3392. opline->result.u.var = get_temporary_variable(CG(active_op_array));
  3393. opline->op1 = *constant_container;
  3394. opline->op2 = *constant_name;
  3395. *result = opline->result;
  3396. break;
  3397. }
  3398. return;
  3399. }
  3400. /* namespace constant */
  3401. /* only one that did not contain \ from the start can be converted to string if unknown */
  3402. switch (mode) {
  3403. case ZEND_CT:
  3404. compound = memchr(Z_STRVAL(constant_name->u.constant), '\\', Z_STRLEN(constant_name->u.constant));
  3405. /* this is a namespace constant, or an unprefixed constant */
  3406. if (zend_constant_ct_subst(result, &constant_name->u.constant, 0 TSRMLS_CC)) {
  3407. break;
  3408. }
  3409. zend_resolve_non_class_name(constant_name, check_namespace TSRMLS_CC);
  3410. if(!compound) {
  3411. fetch_type |= IS_CONSTANT_UNQUALIFIED;
  3412. }
  3413. *result = *constant_name;
  3414. result->u.constant.type = IS_CONSTANT | fetch_type;
  3415. break;
  3416. case ZEND_RT:
  3417. compound = memchr(Z_STRVAL(constant_name->u.constant), '\\', Z_STRLEN(constant_name->u.constant));
  3418. zend_resolve_non_class_name(constant_name, check_namespace TSRMLS_CC);
  3419. if(zend_constant_ct_subst(result, &constant_name->u.constant, 1 TSRMLS_CC)) {
  3420. break;
  3421. }
  3422. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  3423. opline->opcode = ZEND_FETCH_CONSTANT;
  3424. opline->result.op_type = IS_TMP_VAR;
  3425. opline->result.u.var = get_temporary_variable(CG(active_op_array));
  3426. *result = opline->result;
  3427. SET_UNUSED(opline->op1);
  3428. if(compound) {
  3429. /* the name is unambiguous */
  3430. opline->extended_value = 0;
  3431. } else {
  3432. opline->extended_value = IS_CONSTANT_UNQUALIFIED;
  3433. }
  3434. opline->op2 = *constant_name;
  3435. break;
  3436. }
  3437. }
  3438. /* }}} */
  3439. void zend_do_shell_exec(znode *result, const znode *cmd TSRMLS_DC) /* {{{ */
  3440. {
  3441. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  3442. switch (cmd->op_type) {
  3443. case IS_CONST:
  3444. case IS_TMP_VAR:
  3445. opline->opcode = ZEND_SEND_VAL;
  3446. break;
  3447. default:
  3448. opline->opcode = ZEND_SEND_VAR;
  3449. break;
  3450. }
  3451. opline->op1 = *cmd;
  3452. opline->op2.u.opline_num = 0;
  3453. opline->extended_value = ZEND_DO_FCALL;
  3454. SET_UNUSED(opline->op2);
  3455. /* FIXME: exception support not added to this op2 */
  3456. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  3457. opline->opcode = ZEND_DO_FCALL;
  3458. opline->result.u.var = get_temporary_variable(CG(active_op_array));
  3459. opline->result.op_type = IS_VAR;
  3460. Z_STRVAL(opline->op1.u.constant) = estrndup("shell_exec", sizeof("shell_exec")-1);
  3461. Z_STRLEN(opline->op1.u.constant) = sizeof("shell_exec")-1;
  3462. INIT_PZVAL(&opline->op1.u.constant);
  3463. Z_TYPE(opline->op1.u.constant) = IS_STRING;
  3464. opline->op1.op_type = IS_CONST;
  3465. opline->extended_value = 1;
  3466. SET_UNUSED(opline->op2);
  3467. ZVAL_LONG(&opline->op2.u.constant, zend_hash_func("shell_exec", sizeof("shell_exec")));
  3468. *result = opline->result;
  3469. }
  3470. /* }}} */
  3471. void zend_do_init_array(znode *result, const znode *expr, const znode *offset, zend_bool is_ref TSRMLS_DC) /* {{{ */
  3472. {
  3473. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  3474. opline->opcode = ZEND_INIT_ARRAY;
  3475. opline->result.u.var = get_temporary_variable(CG(active_op_array));
  3476. opline->result.op_type = IS_TMP_VAR;
  3477. *result = opline->result;
  3478. if (expr) {
  3479. opline->op1 = *expr;
  3480. if (offset) {
  3481. opline->op2 = *offset;
  3482. } else {
  3483. SET_UNUSED(opline->op2);
  3484. }
  3485. } else {
  3486. SET_UNUSED(opline->op1);
  3487. SET_UNUSED(opline->op2);
  3488. }
  3489. opline->extended_value = is_ref;
  3490. }
  3491. /* }}} */
  3492. void zend_do_add_array_element(znode *result, const znode *expr, const znode *offset, zend_bool is_ref TSRMLS_DC) /* {{{ */
  3493. {
  3494. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  3495. opline->opcode = ZEND_ADD_ARRAY_ELEMENT;
  3496. opline->result = *result;
  3497. opline->op1 = *expr;
  3498. if (offset) {
  3499. opline->op2 = *offset;
  3500. } else {
  3501. SET_UNUSED(opline->op2);
  3502. }
  3503. opline->extended_value = is_ref;
  3504. }
  3505. /* }}} */
  3506. void zend_do_add_static_array_element(znode *result, znode *offset, const znode *expr) /* {{{ */
  3507. {
  3508. zval *element;
  3509. ALLOC_ZVAL(element);
  3510. *element = expr->u.constant;
  3511. if (offset) {
  3512. switch (offset->u.constant.type & IS_CONSTANT_TYPE_MASK) {
  3513. case IS_CONSTANT:
  3514. /* Ugly hack to denote that this value has a constant index */
  3515. Z_TYPE_P(element) |= IS_CONSTANT_INDEX;
  3516. Z_STRVAL(offset->u.constant) = erealloc(Z_STRVAL(offset->u.constant), Z_STRLEN(offset->u.constant)+3);
  3517. Z_STRVAL(offset->u.constant)[Z_STRLEN(offset->u.constant)+1] = Z_TYPE(offset->u.constant);
  3518. Z_STRVAL(offset->u.constant)[Z_STRLEN(offset->u.constant)+2] = 0;
  3519. zend_symtable_update(result->u.constant.value.ht, Z_STRVAL(offset->u.constant), Z_STRLEN(offset->u.constant)+3, &element, sizeof(zval *), NULL);
  3520. zval_dtor(&offset->u.constant);
  3521. break;
  3522. case IS_STRING:
  3523. zend_symtable_update(result->u.constant.value.ht, offset->u.constant.value.str.val, offset->u.constant.value.str.len+1, &element, sizeof(zval *), NULL);
  3524. zval_dtor(&offset->u.constant);
  3525. break;
  3526. case IS_NULL:
  3527. zend_symtable_update(Z_ARRVAL(result->u.constant), "", 1, &element, sizeof(zval *), NULL);
  3528. break;
  3529. case IS_LONG:
  3530. case IS_BOOL:
  3531. zend_hash_index_update(Z_ARRVAL(result->u.constant), Z_LVAL(offset->u.constant), &element, sizeof(zval *), NULL);
  3532. break;
  3533. case IS_DOUBLE:
  3534. zend_hash_index_update(Z_ARRVAL(result->u.constant), zend_dval_to_lval(Z_DVAL(offset->u.constant)), &element, sizeof(zval *), NULL);
  3535. break;
  3536. case IS_CONSTANT_ARRAY:
  3537. zend_error(E_ERROR, "Illegal offset type");
  3538. break;
  3539. }
  3540. } else {
  3541. zend_hash_next_index_insert(Z_ARRVAL(result->u.constant), &element, sizeof(zval *), NULL);
  3542. }
  3543. }
  3544. /* }}} */
  3545. void zend_do_add_list_element(const znode *element TSRMLS_DC) /* {{{ */
  3546. {
  3547. list_llist_element lle;
  3548. if (element) {
  3549. zend_check_writable_variable(element);
  3550. lle.var = *element;
  3551. zend_llist_copy(&lle.dimensions, &CG(dimension_llist));
  3552. zend_llist_prepend_element(&CG(list_llist), &lle);
  3553. }
  3554. (*((int *)CG(dimension_llist).tail->data))++;
  3555. }
  3556. /* }}} */
  3557. void zend_do_new_list_begin(TSRMLS_D) /* {{{ */
  3558. {
  3559. int current_dimension = 0;
  3560. zend_llist_add_element(&CG(dimension_llist), &current_dimension);
  3561. }
  3562. /* }}} */
  3563. void zend_do_new_list_end(TSRMLS_D) /* {{{ */
  3564. {
  3565. zend_llist_remove_tail(&CG(dimension_llist));
  3566. (*((int *)CG(dimension_llist).tail->data))++;
  3567. }
  3568. /* }}} */
  3569. void zend_do_list_init(TSRMLS_D) /* {{{ */
  3570. {
  3571. zend_stack_push(&CG(list_stack), &CG(list_llist), sizeof(zend_llist));
  3572. zend_stack_push(&CG(list_stack), &CG(dimension_llist), sizeof(zend_llist));
  3573. zend_llist_init(&CG(list_llist), sizeof(list_llist_element), NULL, 0);
  3574. zend_llist_init(&CG(dimension_llist), sizeof(int), NULL, 0);
  3575. zend_do_new_list_begin(TSRMLS_C);
  3576. }
  3577. /* }}} */
  3578. void zend_do_list_end(znode *result, znode *expr TSRMLS_DC) /* {{{ */
  3579. {
  3580. zend_llist_element *le;
  3581. zend_llist_element *dimension;
  3582. zend_op *opline;
  3583. znode last_container;
  3584. le = CG(list_llist).head;
  3585. while (le) {
  3586. zend_llist *tmp_dimension_llist = &((list_llist_element *)le->data)->dimensions;
  3587. dimension = tmp_dimension_llist->head;
  3588. while (dimension) {
  3589. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  3590. if (dimension == tmp_dimension_llist->head) { /* first */
  3591. last_container = *expr;
  3592. switch (expr->op_type) {
  3593. case IS_VAR:
  3594. case IS_CV:
  3595. opline->opcode = ZEND_FETCH_DIM_R;
  3596. break;
  3597. case IS_TMP_VAR:
  3598. opline->opcode = ZEND_FETCH_DIM_TMP_VAR;
  3599. break;
  3600. case IS_CONST: /* fetch_dim_tmp_var will handle this bogus fetch */
  3601. zval_copy_ctor(&expr->u.constant);
  3602. opline->opcode = ZEND_FETCH_DIM_TMP_VAR;
  3603. break;
  3604. }
  3605. opline->extended_value = ZEND_FETCH_ADD_LOCK;
  3606. } else {
  3607. opline->opcode = ZEND_FETCH_DIM_R;
  3608. }
  3609. opline->result.op_type = IS_VAR;
  3610. opline->result.u.EA.type = 0;
  3611. opline->result.u.var = get_temporary_variable(CG(active_op_array));
  3612. opline->op1 = last_container;
  3613. opline->op2.op_type = IS_CONST;
  3614. Z_TYPE(opline->op2.u.constant) = IS_LONG;
  3615. Z_LVAL(opline->op2.u.constant) = *((int *) dimension->data);
  3616. INIT_PZVAL(&opline->op2.u.constant);
  3617. last_container = opline->result;
  3618. dimension = dimension->next;
  3619. }
  3620. ((list_llist_element *) le->data)->value = last_container;
  3621. zend_llist_destroy(&((list_llist_element *) le->data)->dimensions);
  3622. zend_do_assign(result, &((list_llist_element *) le->data)->var, &((list_llist_element *) le->data)->value TSRMLS_CC);
  3623. zend_do_free(result TSRMLS_CC);
  3624. le = le->next;
  3625. }
  3626. zend_llist_destroy(&CG(dimension_llist));
  3627. zend_llist_destroy(&CG(list_llist));
  3628. *result = *expr;
  3629. {
  3630. zend_llist *p;
  3631. /* restore previous lists */
  3632. zend_stack_top(&CG(list_stack), (void **) &p);
  3633. CG(dimension_llist) = *p;
  3634. zend_stack_del_top(&CG(list_stack));
  3635. zend_stack_top(&CG(list_stack), (void **) &p);
  3636. CG(list_llist) = *p;
  3637. zend_stack_del_top(&CG(list_stack));
  3638. }
  3639. }
  3640. /* }}} */
  3641. void zend_do_fetch_static_variable(znode *varname, const znode *static_assignment, int fetch_type TSRMLS_DC) /* {{{ */
  3642. {
  3643. zval *tmp;
  3644. zend_op *opline;
  3645. znode lval;
  3646. znode result;
  3647. ALLOC_ZVAL(tmp);
  3648. if (static_assignment) {
  3649. *tmp = static_assignment->u.constant;
  3650. } else {
  3651. INIT_ZVAL(*tmp);
  3652. }
  3653. if (!CG(active_op_array)->static_variables) {
  3654. ALLOC_HASHTABLE(CG(active_op_array)->static_variables);
  3655. zend_hash_init(CG(active_op_array)->static_variables, 2, NULL, ZVAL_PTR_DTOR, 0);
  3656. }
  3657. zend_hash_update(CG(active_op_array)->static_variables, varname->u.constant.value.str.val, varname->u.constant.value.str.len+1, &tmp, sizeof(zval *), NULL);
  3658. if (varname->op_type == IS_CONST) {
  3659. if (Z_TYPE(varname->u.constant) != IS_STRING) {
  3660. convert_to_string(&varname->u.constant);
  3661. }
  3662. }
  3663. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  3664. opline->opcode = (fetch_type == ZEND_FETCH_LEXICAL) ? ZEND_FETCH_R : ZEND_FETCH_W; /* the default mode must be Write, since fetch_simple_variable() is used to define function arguments */
  3665. opline->result.op_type = IS_VAR;
  3666. opline->result.u.EA.type = 0;
  3667. opline->result.u.var = get_temporary_variable(CG(active_op_array));
  3668. opline->op1 = *varname;
  3669. SET_UNUSED(opline->op2);
  3670. opline->op2.u.EA.type = ZEND_FETCH_STATIC;
  3671. result = opline->result;
  3672. if (varname->op_type == IS_CONST) {
  3673. zval_copy_ctor(&varname->u.constant);
  3674. }
  3675. fetch_simple_variable(&lval, varname, 0 TSRMLS_CC); /* Relies on the fact that the default fetch is BP_VAR_W */
  3676. if (fetch_type == ZEND_FETCH_LEXICAL) {
  3677. znode dummy;
  3678. zend_do_begin_variable_parse(TSRMLS_C);
  3679. zend_do_assign(&dummy, &lval, &result TSRMLS_CC);
  3680. zend_do_free(&dummy TSRMLS_CC);
  3681. } else {
  3682. zend_do_assign_ref(NULL, &lval, &result TSRMLS_CC);
  3683. }
  3684. CG(active_op_array)->opcodes[CG(active_op_array)->last-1].result.u.EA.type |= EXT_TYPE_UNUSED;
  3685. /* zval_dtor(&varname->u.constant); */
  3686. }
  3687. /* }}} */
  3688. void zend_do_fetch_lexical_variable(znode *varname, zend_bool is_ref TSRMLS_DC) /* {{{ */
  3689. {
  3690. znode value;
  3691. if (Z_STRLEN(varname->u.constant) == sizeof("this") - 1 &&
  3692. memcmp(Z_STRVAL(varname->u.constant), "this", sizeof("this") - 1) == 0) {
  3693. zend_error(E_COMPILE_ERROR, "Cannot use $this as lexical variable");
  3694. return;
  3695. }
  3696. value.op_type = IS_CONST;
  3697. ZVAL_NULL(&value.u.constant);
  3698. Z_TYPE(value.u.constant) |= is_ref ? IS_LEXICAL_REF : IS_LEXICAL_VAR;
  3699. Z_SET_REFCOUNT_P(&value.u.constant, 1);
  3700. Z_UNSET_ISREF_P(&value.u.constant);
  3701. zend_do_fetch_static_variable(varname, &value, is_ref ? ZEND_FETCH_STATIC : ZEND_FETCH_LEXICAL TSRMLS_CC);
  3702. }
  3703. /* }}} */
  3704. void zend_do_fetch_global_variable(znode *varname, const znode *static_assignment, int fetch_type TSRMLS_DC) /* {{{ */
  3705. {
  3706. zend_op *opline;
  3707. znode lval;
  3708. znode result;
  3709. if (varname->op_type == IS_CONST) {
  3710. if (Z_TYPE(varname->u.constant) != IS_STRING) {
  3711. convert_to_string(&varname->u.constant);
  3712. }
  3713. }
  3714. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  3715. opline->opcode = ZEND_FETCH_W; /* the default mode must be Write, since fetch_simple_variable() is used to define function arguments */
  3716. opline->result.op_type = IS_VAR;
  3717. opline->result.u.EA.type = 0;
  3718. opline->result.u.var = get_temporary_variable(CG(active_op_array));
  3719. opline->op1 = *varname;
  3720. SET_UNUSED(opline->op2);
  3721. opline->op2.u.EA.type = fetch_type;
  3722. result = opline->result;
  3723. if (varname->op_type == IS_CONST) {
  3724. zval_copy_ctor(&varname->u.constant);
  3725. }
  3726. fetch_simple_variable(&lval, varname, 0 TSRMLS_CC); /* Relies on the fact that the default fetch is BP_VAR_W */
  3727. zend_do_assign_ref(NULL, &lval, &result TSRMLS_CC);
  3728. CG(active_op_array)->opcodes[CG(active_op_array)->last-1].result.u.EA.type |= EXT_TYPE_UNUSED;
  3729. }
  3730. /* }}} */
  3731. void zend_do_cast(znode *result, const znode *expr, int type TSRMLS_DC) /* {{{ */
  3732. {
  3733. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  3734. opline->opcode = ZEND_CAST;
  3735. opline->result.op_type = IS_TMP_VAR;
  3736. opline->result.u.var = get_temporary_variable(CG(active_op_array));
  3737. opline->op1 = *expr;
  3738. SET_UNUSED(opline->op2);
  3739. opline->extended_value = type;
  3740. *result = opline->result;
  3741. }
  3742. /* }}} */
  3743. void zend_do_include_or_eval(int type, znode *result, const znode *op1 TSRMLS_DC) /* {{{ */
  3744. {
  3745. zend_do_extended_fcall_begin(TSRMLS_C);
  3746. {
  3747. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  3748. opline->opcode = ZEND_INCLUDE_OR_EVAL;
  3749. opline->result.op_type = IS_VAR;
  3750. opline->result.u.var = get_temporary_variable(CG(active_op_array));
  3751. opline->op1 = *op1;
  3752. SET_UNUSED(opline->op2);
  3753. Z_LVAL(opline->op2.u.constant) = type;
  3754. *result = opline->result;
  3755. }
  3756. zend_do_extended_fcall_end(TSRMLS_C);
  3757. }
  3758. /* }}} */
  3759. void zend_do_indirect_references(znode *result, const znode *num_references, znode *variable TSRMLS_DC) /* {{{ */
  3760. {
  3761. int i;
  3762. zend_do_end_variable_parse(variable, BP_VAR_R, 0 TSRMLS_CC);
  3763. for (i=1; i<num_references->u.constant.value.lval; i++) {
  3764. fetch_simple_variable_ex(result, variable, 0, ZEND_FETCH_R TSRMLS_CC);
  3765. *variable = *result;
  3766. }
  3767. zend_do_begin_variable_parse(TSRMLS_C);
  3768. fetch_simple_variable(result, variable, 1 TSRMLS_CC);
  3769. /* there is a chance someone is accessing $this */
  3770. if (CG(active_op_array)->scope && CG(active_op_array)->this_var == -1) {
  3771. CG(active_op_array)->this_var = lookup_cv(CG(active_op_array), estrndup("this", sizeof("this")-1), sizeof("this")-1);
  3772. }
  3773. }
  3774. /* }}} */
  3775. void zend_do_unset(const znode *variable TSRMLS_DC) /* {{{ */
  3776. {
  3777. zend_op *last_op;
  3778. zend_check_writable_variable(variable);
  3779. if (variable->op_type == IS_CV) {
  3780. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  3781. opline->opcode = ZEND_UNSET_VAR;
  3782. opline->op1 = *variable;
  3783. SET_UNUSED(opline->op2);
  3784. opline->op2.u.EA.type = ZEND_FETCH_LOCAL;
  3785. SET_UNUSED(opline->result);
  3786. opline->extended_value = ZEND_QUICK_SET;
  3787. } else {
  3788. last_op = &CG(active_op_array)->opcodes[get_next_op_number(CG(active_op_array))-1];
  3789. switch (last_op->opcode) {
  3790. case ZEND_FETCH_UNSET:
  3791. last_op->opcode = ZEND_UNSET_VAR;
  3792. break;
  3793. case ZEND_FETCH_DIM_UNSET:
  3794. last_op->opcode = ZEND_UNSET_DIM;
  3795. break;
  3796. case ZEND_FETCH_OBJ_UNSET:
  3797. last_op->opcode = ZEND_UNSET_OBJ;
  3798. break;
  3799. }
  3800. }
  3801. }
  3802. /* }}} */
  3803. void zend_do_isset_or_isempty(int type, znode *result, znode *variable TSRMLS_DC) /* {{{ */
  3804. {
  3805. zend_op *last_op;
  3806. zend_do_end_variable_parse(variable, BP_VAR_IS, 0 TSRMLS_CC);
  3807. zend_check_writable_variable(variable);
  3808. if (variable->op_type == IS_CV) {
  3809. last_op = get_next_op(CG(active_op_array) TSRMLS_CC);
  3810. last_op->opcode = ZEND_ISSET_ISEMPTY_VAR;
  3811. last_op->op1 = *variable;
  3812. SET_UNUSED(last_op->op2);
  3813. last_op->op2.u.EA.type = ZEND_FETCH_LOCAL;
  3814. last_op->result.u.var = get_temporary_variable(CG(active_op_array));
  3815. last_op->extended_value = ZEND_QUICK_SET;
  3816. } else {
  3817. last_op = &CG(active_op_array)->opcodes[get_next_op_number(CG(active_op_array))-1];
  3818. switch (last_op->opcode) {
  3819. case ZEND_FETCH_IS:
  3820. last_op->opcode = ZEND_ISSET_ISEMPTY_VAR;
  3821. break;
  3822. case ZEND_FETCH_DIM_IS:
  3823. last_op->opcode = ZEND_ISSET_ISEMPTY_DIM_OBJ;
  3824. break;
  3825. case ZEND_FETCH_OBJ_IS:
  3826. last_op->opcode = ZEND_ISSET_ISEMPTY_PROP_OBJ;
  3827. break;
  3828. }
  3829. last_op->extended_value = 0;
  3830. }
  3831. last_op->result.op_type = IS_TMP_VAR;
  3832. last_op->extended_value |= type;
  3833. *result = last_op->result;
  3834. }
  3835. /* }}} */
  3836. void zend_do_sizeof(int type, znode *result, znode *arg TSRMLS_DC) {
  3837. if (arg->op_type == IS_CONST) {
  3838. if (type == ZEND_STRLEN) {
  3839. result->op_type = IS_CONST;
  3840. Z_TYPE(result->u.constant) = IS_LONG;
  3841. Z_LVAL(result->u.constant) = Z_STRLEN(arg->u.constant);
  3842. } else {
  3843. zend_error(E_COMPILE_ERROR, "count() expects an array, constant given");
  3844. }
  3845. } else {
  3846. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  3847. opline->result.op_type = IS_TMP_VAR;
  3848. opline->result.u.var = get_temporary_variable(CG(active_op_array));
  3849. opline->op1 = *arg;
  3850. opline->opcode = ZEND_SIZEOF;
  3851. opline->extended_value|= type;
  3852. SET_UNUSED(opline->op2);
  3853. *result = opline->result;
  3854. }
  3855. }
  3856. /* }}} */
  3857. void zend_do_ifset(znode *result, znode *variable TSRMLS_DC) {
  3858. fprintf(stderr, "IFSET: %i\n", variable->op_type);
  3859. }
  3860. /* }}} */
  3861. void zend_do_instanceof(znode *result, const znode *expr, const znode *class_znode, int type TSRMLS_DC) /* {{{ */
  3862. {
  3863. int last_op_number = get_next_op_number(CG(active_op_array));
  3864. zend_op *opline;
  3865. if (last_op_number > 0) {
  3866. opline = &CG(active_op_array)->opcodes[last_op_number-1];
  3867. if (opline->opcode == ZEND_FETCH_CLASS) {
  3868. opline->extended_value |= ZEND_FETCH_CLASS_NO_AUTOLOAD;
  3869. }
  3870. }
  3871. if (expr->op_type == IS_CONST) {
  3872. zend_error(E_COMPILE_ERROR, "instanceof expects an object instance, constant given");
  3873. }
  3874. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  3875. opline->opcode = ZEND_INSTANCEOF;
  3876. opline->result.op_type = IS_TMP_VAR;
  3877. opline->result.u.var = get_temporary_variable(CG(active_op_array));
  3878. opline->op1 = *expr;
  3879. opline->op2 = *class_znode;
  3880. *result = opline->result;
  3881. }
  3882. /* }}} */
  3883. void zend_do_foreach_begin(znode *foreach_token, znode *open_brackets_token, znode *array, znode *as_token, int variable TSRMLS_DC) /* {{{ */
  3884. {
  3885. zend_op *opline;
  3886. zend_bool is_variable;
  3887. zend_bool push_container = 0;
  3888. zend_op dummy_opline;
  3889. if (variable) {
  3890. if (zend_is_function_or_method_call(array)) {
  3891. is_variable = 0;
  3892. } else {
  3893. is_variable = 1;
  3894. }
  3895. /* save the location of FETCH_W instruction(s) */
  3896. open_brackets_token->u.opline_num = get_next_op_number(CG(active_op_array));
  3897. zend_do_end_variable_parse(array, BP_VAR_W, 0 TSRMLS_CC);
  3898. if (CG(active_op_array)->last > 0 &&
  3899. CG(active_op_array)->opcodes[CG(active_op_array)->last-1].opcode == ZEND_FETCH_OBJ_W) {
  3900. /* Only lock the container if we are fetching from a real container and not $this */
  3901. if (CG(active_op_array)->opcodes[CG(active_op_array)->last-1].op1.op_type == IS_VAR) {
  3902. CG(active_op_array)->opcodes[CG(active_op_array)->last-1].extended_value |= ZEND_FETCH_ADD_LOCK;
  3903. push_container = 1;
  3904. }
  3905. }
  3906. } else {
  3907. is_variable = 0;
  3908. open_brackets_token->u.opline_num = get_next_op_number(CG(active_op_array));
  3909. }
  3910. /* save the location of FE_RESET */
  3911. foreach_token->u.opline_num = get_next_op_number(CG(active_op_array));
  3912. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  3913. /* Preform array reset */
  3914. opline->opcode = ZEND_FE_RESET;
  3915. opline->result.op_type = IS_VAR;
  3916. opline->result.u.var = get_temporary_variable(CG(active_op_array));
  3917. opline->op1 = *array;
  3918. SET_UNUSED(opline->op2);
  3919. opline->extended_value = is_variable ? ZEND_FE_RESET_VARIABLE : 0;
  3920. dummy_opline.result = opline->result;
  3921. if (push_container) {
  3922. dummy_opline.op1 = CG(active_op_array)->opcodes[CG(active_op_array)->last-2].op1;
  3923. } else {
  3924. znode tmp;
  3925. tmp.op_type = IS_UNUSED;
  3926. dummy_opline.op1 = tmp;
  3927. }
  3928. zend_stack_push(&CG(foreach_copy_stack), (void *) &dummy_opline, sizeof(zend_op));
  3929. /* save the location of FE_FETCH */
  3930. as_token->u.opline_num = get_next_op_number(CG(active_op_array));
  3931. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  3932. opline->opcode = ZEND_FE_FETCH;
  3933. opline->result.op_type = IS_VAR;
  3934. opline->result.u.var = get_temporary_variable(CG(active_op_array));
  3935. opline->op1 = dummy_opline.result;
  3936. opline->extended_value = 0;
  3937. SET_UNUSED(opline->op2);
  3938. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  3939. opline->opcode = ZEND_OP_DATA;
  3940. SET_UNUSED(opline->op1);
  3941. SET_UNUSED(opline->op2);
  3942. SET_UNUSED(opline->result);
  3943. }
  3944. /* }}} */
  3945. void zend_do_foreach_cont(znode *foreach_token, const znode *open_brackets_token, const znode *as_token, znode *value, znode *key TSRMLS_DC) /* {{{ */
  3946. {
  3947. zend_op *opline;
  3948. znode dummy, value_node;
  3949. zend_bool assign_by_ref=0;
  3950. opline = &CG(active_op_array)->opcodes[as_token->u.opline_num];
  3951. if (key->op_type != IS_UNUSED) {
  3952. znode *tmp;
  3953. /* switch between the key and value... */
  3954. tmp = key;
  3955. key = value;
  3956. value = tmp;
  3957. /* Mark extended_value in case both key and value are being used */
  3958. opline->extended_value |= ZEND_FE_FETCH_WITH_KEY;
  3959. }
  3960. if ((key->op_type != IS_UNUSED) && (key->u.EA.type & ZEND_PARSED_REFERENCE_VARIABLE)) {
  3961. zend_error(E_COMPILE_ERROR, "Key element cannot be a reference");
  3962. }
  3963. if (value->u.EA.type & ZEND_PARSED_REFERENCE_VARIABLE) {
  3964. assign_by_ref = 1;
  3965. if (!(opline-1)->extended_value) {
  3966. zend_error(E_COMPILE_ERROR, "Cannot create references to elements of a temporary array expression");
  3967. }
  3968. /* Mark extended_value for assign-by-reference */
  3969. opline->extended_value |= ZEND_FE_FETCH_BYREF;
  3970. CG(active_op_array)->opcodes[foreach_token->u.opline_num].extended_value |= ZEND_FE_RESET_REFERENCE;
  3971. } else {
  3972. zend_op *foreach_copy;
  3973. zend_op *fetch = &CG(active_op_array)->opcodes[foreach_token->u.opline_num];
  3974. zend_op *end = &CG(active_op_array)->opcodes[open_brackets_token->u.opline_num];
  3975. /* Change "write context" into "read context" */
  3976. fetch->extended_value = 0; /* reset ZEND_FE_RESET_VARIABLE */
  3977. while (fetch != end) {
  3978. --fetch;
  3979. if (fetch->opcode == ZEND_FETCH_DIM_W && fetch->op2.op_type == IS_UNUSED) {
  3980. zend_error(E_COMPILE_ERROR, "Cannot use [] for reading");
  3981. }
  3982. fetch->opcode -= 3; /* FETCH_W -> FETCH_R */
  3983. }
  3984. /* prevent double SWITCH_FREE */
  3985. zend_stack_top(&CG(foreach_copy_stack), (void **) &foreach_copy);
  3986. foreach_copy->op1.op_type = IS_UNUSED;
  3987. }
  3988. value_node = opline->result;
  3989. if (assign_by_ref) {
  3990. zend_do_end_variable_parse(value, BP_VAR_W, 0 TSRMLS_CC);
  3991. /* Mark FE_FETCH as IS_VAR as it holds the data directly as a value */
  3992. zend_do_assign_ref(NULL, value, &value_node TSRMLS_CC);
  3993. } else {
  3994. zend_do_assign(&dummy, value, &value_node TSRMLS_CC);
  3995. zend_do_free(&dummy TSRMLS_CC);
  3996. }
  3997. if (key->op_type != IS_UNUSED) {
  3998. znode key_node;
  3999. opline = &CG(active_op_array)->opcodes[as_token->u.opline_num+1];
  4000. opline->result.op_type = IS_TMP_VAR;
  4001. opline->result.u.EA.type = 0;
  4002. opline->result.u.opline_num = get_temporary_variable(CG(active_op_array));
  4003. key_node = opline->result;
  4004. zend_do_assign(&dummy, key, &key_node TSRMLS_CC);
  4005. zend_do_free(&dummy TSRMLS_CC);
  4006. }
  4007. do_begin_loop(TSRMLS_C);
  4008. INC_BPC(CG(active_op_array));
  4009. }
  4010. /* }}} */
  4011. void zend_do_foreach_end(const znode *foreach_token, const znode *as_token TSRMLS_DC) /* {{{ */
  4012. {
  4013. zend_op *container_ptr;
  4014. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  4015. opline->opcode = ZEND_JMP;
  4016. opline->op1.u.opline_num = as_token->u.opline_num;
  4017. SET_UNUSED(opline->op1);
  4018. SET_UNUSED(opline->op2);
  4019. CG(active_op_array)->opcodes[foreach_token->u.opline_num].op2.u.opline_num = get_next_op_number(CG(active_op_array)); /* FE_RESET */
  4020. CG(active_op_array)->opcodes[as_token->u.opline_num].op2.u.opline_num = get_next_op_number(CG(active_op_array)); /* FE_FETCH */
  4021. do_end_loop(as_token->u.opline_num, 1 TSRMLS_CC);
  4022. zend_stack_top(&CG(foreach_copy_stack), (void **) &container_ptr);
  4023. generate_free_foreach_copy(container_ptr TSRMLS_CC);
  4024. zend_stack_del_top(&CG(foreach_copy_stack));
  4025. DEC_BPC(CG(active_op_array));
  4026. }
  4027. /* }}} */
  4028. void zend_do_declare_begin(TSRMLS_D) /* {{{ */
  4029. {
  4030. zend_stack_push(&CG(declare_stack), &CG(declarables), sizeof(zend_declarables));
  4031. }
  4032. /* }}} */
  4033. void zend_do_declare_stmt(znode *var, znode *val TSRMLS_DC) /* {{{ */
  4034. {
  4035. if (!zend_binary_strcasecmp(var->u.constant.value.str.val, var->u.constant.value.str.len, "ticks", sizeof("ticks")-1)) {
  4036. convert_to_long(&val->u.constant);
  4037. CG(declarables).ticks = val->u.constant;
  4038. #ifdef ZEND_MULTIBYTE
  4039. } else if (!zend_binary_strcasecmp(var->u.constant.value.str.val, var->u.constant.value.str.len, "encoding", sizeof("encoding")-1)) {
  4040. zend_encoding *new_encoding, *old_encoding;
  4041. zend_encoding_filter old_input_filter;
  4042. if ((Z_TYPE(val->u.constant) & IS_CONSTANT_TYPE_MASK) == IS_CONSTANT) {
  4043. zend_error(E_COMPILE_ERROR, "Cannot use constants as encoding");
  4044. }
  4045. /*
  4046. * Check that the pragma comes before any opcodes. If the compilation
  4047. * got as far as this, the previous portion of the script must have been
  4048. * parseable according to the .ini script_encoding setting. We still
  4049. * want to tell them to put declare() at the top.
  4050. */
  4051. {
  4052. int num = CG(active_op_array)->last;
  4053. /* ignore ZEND_EXT_STMT and ZEND_TICKS */
  4054. while (num > 0 &&
  4055. (CG(active_op_array)->opcodes[num-1].opcode == ZEND_EXT_STMT ||
  4056. CG(active_op_array)->opcodes[num-1].opcode == ZEND_TICKS)) {
  4057. --num;
  4058. }
  4059. if (num > 0) {
  4060. zend_error(E_COMPILE_ERROR, "Encoding declaration pragma must be the very first statement in the script");
  4061. }
  4062. }
  4063. CG(encoding_declared) = 1;
  4064. convert_to_string(&val->u.constant);
  4065. new_encoding = zend_multibyte_fetch_encoding(val->u.constant.value.str.val);
  4066. if (!new_encoding) {
  4067. zend_error(E_COMPILE_WARNING, "Unsupported encoding [%s]", val->u.constant.value.str.val);
  4068. } else {
  4069. old_input_filter = LANG_SCNG(input_filter);
  4070. old_encoding = LANG_SCNG(script_encoding);
  4071. zend_multibyte_set_filter(new_encoding TSRMLS_CC);
  4072. /* need to re-scan if input filter changed */
  4073. if (old_input_filter != LANG_SCNG(input_filter) ||
  4074. ((old_input_filter == zend_multibyte_script_encoding_filter) &&
  4075. (new_encoding != old_encoding))) {
  4076. zend_multibyte_yyinput_again(old_input_filter, old_encoding TSRMLS_CC);
  4077. }
  4078. }
  4079. efree(val->u.constant.value.str.val);
  4080. #else /* !ZEND_MULTIBYTE */
  4081. } else if (!zend_binary_strcasecmp(var->u.constant.value.str.val, var->u.constant.value.str.len, "encoding", sizeof("encoding")-1)) {
  4082. /* Do not generate any kind of warning for encoding declares */
  4083. /* zend_error(E_COMPILE_WARNING, "Declare encoding [%s] not supported", val->u.constant.value.str.val); */
  4084. zval_dtor(&val->u.constant);
  4085. #endif /* ZEND_MULTIBYTE */
  4086. } else {
  4087. zend_error(E_COMPILE_WARNING, "Unsupported declare '%s'", var->u.constant.value.str.val);
  4088. zval_dtor(&val->u.constant);
  4089. }
  4090. zval_dtor(&var->u.constant);
  4091. }
  4092. /* }}} */
  4093. void zend_do_declare_end(const znode *declare_token TSRMLS_DC) /* {{{ */
  4094. {
  4095. zend_declarables *declarables;
  4096. zend_stack_top(&CG(declare_stack), (void **) &declarables);
  4097. /* We should restore if there was more than (current - start) - (ticks?1:0) opcodes */
  4098. if ((get_next_op_number(CG(active_op_array)) - declare_token->u.opline_num) - ((Z_LVAL(CG(declarables).ticks))?1:0)) {
  4099. CG(declarables) = *declarables;
  4100. }
  4101. }
  4102. /* }}} */
  4103. void zend_do_exit(znode *result, const znode *message TSRMLS_DC) /* {{{ */
  4104. {
  4105. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  4106. opline->opcode = ZEND_EXIT;
  4107. opline->op1 = *message;
  4108. SET_UNUSED(opline->op2);
  4109. result->op_type = IS_CONST;
  4110. Z_TYPE(result->u.constant) = IS_BOOL;
  4111. Z_LVAL(result->u.constant) = 1;
  4112. }
  4113. /* }}} */
  4114. void zend_do_begin_silence(znode *strudel_token TSRMLS_DC) /* {{{ */
  4115. {
  4116. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  4117. opline->opcode = ZEND_BEGIN_SILENCE;
  4118. opline->result.op_type = IS_TMP_VAR;
  4119. opline->result.u.var = get_temporary_variable(CG(active_op_array));
  4120. SET_UNUSED(opline->op1);
  4121. SET_UNUSED(opline->op2);
  4122. *strudel_token = opline->result;
  4123. }
  4124. /* }}} */
  4125. void zend_do_end_silence(const znode *strudel_token TSRMLS_DC) /* {{{ */
  4126. {
  4127. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  4128. opline->opcode = ZEND_END_SILENCE;
  4129. opline->op1 = *strudel_token;
  4130. SET_UNUSED(opline->op2);
  4131. }
  4132. /* }}} */
  4133. void zend_do_jmp_set(const znode *value, znode *jmp_token, znode *colon_token TSRMLS_DC) /* {{{ */
  4134. {
  4135. int op_number = get_next_op_number(CG(active_op_array));
  4136. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  4137. opline->opcode = ZEND_JMP_SET;
  4138. opline->result.op_type = IS_TMP_VAR;
  4139. opline->result.u.var = get_temporary_variable(CG(active_op_array));
  4140. opline->op1 = *value;
  4141. SET_UNUSED(opline->op2);
  4142. *colon_token = opline->result;
  4143. jmp_token->u.opline_num = op_number;
  4144. INC_BPC(CG(active_op_array));
  4145. }
  4146. /* }}} */
  4147. void zend_do_jmp_set_else(znode *result, const znode *false_value, const znode *jmp_token, const znode *colon_token TSRMLS_DC) /* {{{ */
  4148. {
  4149. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  4150. opline->opcode = ZEND_QM_ASSIGN;
  4151. opline->extended_value = 0;
  4152. opline->result = *colon_token;
  4153. opline->op1 = *false_value;
  4154. SET_UNUSED(opline->op2);
  4155. *result = opline->result;
  4156. CG(active_op_array)->opcodes[jmp_token->u.opline_num].op2.u.opline_num = get_next_op_number(CG(active_op_array));
  4157. DEC_BPC(CG(active_op_array));
  4158. }
  4159. /* }}} */
  4160. void zend_do_begin_qm_op(const znode *cond, znode *qm_token TSRMLS_DC) /* {{{ */
  4161. {
  4162. int jmpz_op_number = get_next_op_number(CG(active_op_array));
  4163. zend_op *opline;
  4164. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  4165. opline->opcode = ZEND_JMPZ;
  4166. opline->op1 = *cond;
  4167. SET_UNUSED(opline->op2);
  4168. opline->op2.u.opline_num = jmpz_op_number;
  4169. *qm_token = opline->op2;
  4170. INC_BPC(CG(active_op_array));
  4171. }
  4172. /* }}} */
  4173. void zend_do_qm_true(const znode *true_value, znode *qm_token, znode *colon_token TSRMLS_DC) /* {{{ */
  4174. {
  4175. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  4176. CG(active_op_array)->opcodes[qm_token->u.opline_num].op2.u.opline_num = get_next_op_number(CG(active_op_array))+1; /* jmp over the ZEND_JMP */
  4177. opline->opcode = ZEND_QM_ASSIGN;
  4178. opline->result.op_type = IS_TMP_VAR;
  4179. opline->result.u.var = get_temporary_variable(CG(active_op_array));
  4180. opline->op1 = *true_value;
  4181. SET_UNUSED(opline->op2);
  4182. *qm_token = opline->result;
  4183. colon_token->u.opline_num = get_next_op_number(CG(active_op_array));
  4184. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  4185. opline->opcode = ZEND_JMP;
  4186. SET_UNUSED(opline->op1);
  4187. SET_UNUSED(opline->op2);
  4188. }
  4189. /* }}} */
  4190. void zend_do_qm_false(znode *result, const znode *false_value, const znode *qm_token, const znode *colon_token TSRMLS_DC) /* {{{ */
  4191. {
  4192. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  4193. opline->opcode = ZEND_QM_ASSIGN;
  4194. opline->result = *qm_token;
  4195. opline->op1 = *false_value;
  4196. SET_UNUSED(opline->op2);
  4197. CG(active_op_array)->opcodes[colon_token->u.opline_num].op1.u.opline_num = get_next_op_number(CG(active_op_array));
  4198. *result = opline->result;
  4199. DEC_BPC(CG(active_op_array));
  4200. }
  4201. /* }}} */
  4202. void zend_do_extended_info(TSRMLS_D) /* {{{ */
  4203. {
  4204. zend_op *opline;
  4205. if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_INFO)) {
  4206. return;
  4207. }
  4208. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  4209. opline->opcode = ZEND_EXT_STMT;
  4210. SET_UNUSED(opline->op1);
  4211. SET_UNUSED(opline->op2);
  4212. }
  4213. /* }}} */
  4214. void zend_do_extended_fcall_begin(TSRMLS_D) /* {{{ */
  4215. {
  4216. zend_op *opline;
  4217. if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_INFO)) {
  4218. return;
  4219. }
  4220. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  4221. opline->opcode = ZEND_EXT_FCALL_BEGIN;
  4222. SET_UNUSED(opline->op1);
  4223. SET_UNUSED(opline->op2);
  4224. }
  4225. /* }}} */
  4226. void zend_do_extended_fcall_end(TSRMLS_D) /* {{{ */
  4227. {
  4228. zend_op *opline;
  4229. if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_INFO)) {
  4230. return;
  4231. }
  4232. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  4233. opline->opcode = ZEND_EXT_FCALL_END;
  4234. SET_UNUSED(opline->op1);
  4235. SET_UNUSED(opline->op2);
  4236. }
  4237. /* }}} */
  4238. void zend_do_ticks(TSRMLS_D) /* {{{ */
  4239. {
  4240. if (Z_LVAL(CG(declarables).ticks)) {
  4241. zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  4242. opline->opcode = ZEND_TICKS;
  4243. opline->op1.u.constant = CG(declarables).ticks;
  4244. opline->op1.op_type = IS_CONST;
  4245. SET_UNUSED(opline->op2);
  4246. }
  4247. }
  4248. /* }}} */
  4249. void zend_auto_global_dtor(zend_auto_global *auto_global) /* {{{ */
  4250. {
  4251. free(auto_global->name);
  4252. }
  4253. /* }}} */
  4254. zend_bool zend_is_auto_global(const char *name, uint name_len TSRMLS_DC) /* {{{ */
  4255. {
  4256. zend_auto_global *auto_global;
  4257. if (zend_hash_find(CG(auto_globals), name, name_len+1, (void **) &auto_global)==SUCCESS) {
  4258. if (auto_global->armed) {
  4259. auto_global->armed = auto_global->auto_global_callback(auto_global->name, auto_global->name_len TSRMLS_CC);
  4260. }
  4261. return 1;
  4262. }
  4263. return 0;
  4264. }
  4265. /* }}} */
  4266. int zend_register_auto_global(const char *name, uint name_len, zend_auto_global_callback auto_global_callback TSRMLS_DC) /* {{{ */
  4267. {
  4268. zend_auto_global auto_global;
  4269. auto_global.name = zend_strndup(name, name_len);
  4270. auto_global.name_len = name_len;
  4271. auto_global.auto_global_callback = auto_global_callback;
  4272. return zend_hash_add(CG(auto_globals), name, name_len+1, &auto_global, sizeof(zend_auto_global), NULL);
  4273. }
  4274. /* }}} */
  4275. int zendlex(znode *zendlval TSRMLS_DC) /* {{{ */
  4276. {
  4277. int retval;
  4278. if (CG(increment_lineno)) {
  4279. CG(zend_lineno)++;
  4280. CG(increment_lineno) = 0;
  4281. }
  4282. again:
  4283. Z_TYPE(zendlval->u.constant) = IS_LONG;
  4284. retval = lex_scan(&zendlval->u.constant TSRMLS_CC);
  4285. switch (retval) {
  4286. case T_COMMENT:
  4287. case T_DOC_COMMENT:
  4288. case T_OPEN_TAG:
  4289. case T_WHITESPACE:
  4290. goto again;
  4291. case T_CLOSE_TAG:
  4292. if (LANG_SCNG(yy_text)[LANG_SCNG(yy_leng)-1] != '>') {
  4293. CG(increment_lineno) = 1;
  4294. }
  4295. if (CG(has_bracketed_namespaces) && !CG(in_namespace)) {
  4296. goto again;
  4297. }
  4298. retval = ';'; /* implicit ; */
  4299. break;
  4300. case T_OPEN_TAG_WITH_ECHO:
  4301. retval = T_ECHO;
  4302. break;
  4303. case T_END_HEREDOC:
  4304. efree(Z_STRVAL(zendlval->u.constant));
  4305. break;
  4306. }
  4307. INIT_PZVAL(&zendlval->u.constant);
  4308. zendlval->op_type = IS_CONST;
  4309. return retval;
  4310. }
  4311. /* }}} */
  4312. ZEND_API void zend_initialize_class_data(zend_class_entry *ce, zend_bool nullify_handlers TSRMLS_DC) /* {{{ */
  4313. {
  4314. zend_bool persistent_hashes = (ce->type == ZEND_INTERNAL_CLASS) ? 1 : 0;
  4315. dtor_func_t zval_ptr_dtor_func = ((persistent_hashes) ? ZVAL_INTERNAL_PTR_DTOR : ZVAL_PTR_DTOR);
  4316. ce->refcount = 1;
  4317. ce->constants_updated = 0;
  4318. ce->ce_flags = 0;
  4319. ce->doc_comment = NULL;
  4320. ce->doc_comment_len = 0;
  4321. zend_hash_init_ex(&ce->default_properties, 0, NULL, zval_ptr_dtor_func, persistent_hashes, 0);
  4322. zend_hash_init_ex(&ce->properties_info, 0, NULL, (dtor_func_t) (persistent_hashes ? zend_destroy_property_info_internal : zend_destroy_property_info), persistent_hashes, 0);
  4323. zend_hash_init_ex(&ce->default_static_members, 0, NULL, zval_ptr_dtor_func, persistent_hashes, 0);
  4324. zend_hash_init_ex(&ce->constants_table, 0, NULL, zval_ptr_dtor_func, persistent_hashes, 0);
  4325. zend_hash_init_ex(&ce->function_table, 0, NULL, ZEND_FUNCTION_DTOR, persistent_hashes, 0);
  4326. if (ce->type == ZEND_INTERNAL_CLASS) {
  4327. #ifdef ZTS
  4328. int n = zend_hash_num_elements(CG(class_table));
  4329. if (CG(static_members) && n >= CG(last_static_member)) {
  4330. /* Support for run-time declaration: dl() */
  4331. CG(last_static_member) = n+1;
  4332. CG(static_members) = realloc(CG(static_members), (n+1)*sizeof(HashTable*));
  4333. CG(static_members)[n] = NULL;
  4334. }
  4335. ce->static_members = (HashTable*)(zend_intptr_t)n;
  4336. #else
  4337. ce->static_members = NULL;
  4338. #endif
  4339. } else {
  4340. ce->static_members = &ce->default_static_members;
  4341. }
  4342. if (nullify_handlers) {
  4343. ce->constructor = NULL;
  4344. ce->destructor = NULL;
  4345. ce->clone = NULL;
  4346. ce->__get = NULL;
  4347. ce->__set = NULL;
  4348. ce->__unset = NULL;
  4349. ce->__isset = NULL;
  4350. ce->__call = NULL;
  4351. ce->__callstatic = NULL;
  4352. ce->__tostring = NULL;
  4353. ce->create_object = NULL;
  4354. ce->get_iterator = NULL;
  4355. ce->iterator_funcs.funcs = NULL;
  4356. ce->interface_gets_implemented = NULL;
  4357. ce->get_static_method = NULL;
  4358. ce->parent = NULL;
  4359. ce->num_interfaces = 0;
  4360. ce->interfaces = NULL;
  4361. ce->module = NULL;
  4362. ce->serialize = NULL;
  4363. ce->unserialize = NULL;
  4364. ce->serialize_func = NULL;
  4365. ce->unserialize_func = NULL;
  4366. ce->builtin_functions = NULL;
  4367. }
  4368. }
  4369. /* }}} */
  4370. int zend_get_class_fetch_type(const char *class_name, uint class_name_len) /* {{{ */
  4371. {
  4372. if ((class_name_len == sizeof("self")-1) &&
  4373. !memcmp(class_name, "self", sizeof("self")-1)) {
  4374. return ZEND_FETCH_CLASS_SELF;
  4375. } else if ((class_name_len == sizeof("parent")-1) &&
  4376. !memcmp(class_name, "parent", sizeof("parent")-1)) {
  4377. return ZEND_FETCH_CLASS_PARENT;
  4378. } else if ((class_name_len == sizeof("static")-1) &&
  4379. !memcmp(class_name, "static", sizeof("static")-1)) {
  4380. return ZEND_FETCH_CLASS_STATIC;
  4381. } else {
  4382. return ZEND_FETCH_CLASS_DEFAULT;
  4383. }
  4384. }
  4385. /* }}} */
  4386. ZEND_API char* zend_get_compiled_variable_name(const zend_op_array *op_array, zend_uint var, int* name_len) /* {{{ */
  4387. {
  4388. if (name_len) {
  4389. *name_len = op_array->vars[var].name_len;
  4390. }
  4391. return op_array->vars[var].name;
  4392. }
  4393. /* }}} */
  4394. void zend_do_build_namespace_name(znode *result, znode *prefix, znode *name TSRMLS_DC) /* {{{ */
  4395. {
  4396. if (prefix) {
  4397. *result = *prefix;
  4398. if (Z_TYPE(result->u.constant) == IS_STRING &&
  4399. Z_STRLEN(result->u.constant) == 0) {
  4400. /* namespace\ */
  4401. if (CG(current_namespace)) {
  4402. znode tmp;
  4403. zval_dtor(&result->u.constant);
  4404. tmp.op_type = IS_CONST;
  4405. tmp.u.constant = *CG(current_namespace);
  4406. zval_copy_ctor(&tmp.u.constant);
  4407. zend_do_build_namespace_name(result, NULL, &tmp TSRMLS_CC);
  4408. }
  4409. }
  4410. } else {
  4411. result->op_type = IS_CONST;
  4412. Z_TYPE(result->u.constant) = IS_STRING;
  4413. Z_STRVAL(result->u.constant) = NULL;
  4414. Z_STRLEN(result->u.constant) = 0;
  4415. }
  4416. /* prefix = result */
  4417. zend_do_build_full_name(NULL, result, name, 0 TSRMLS_CC);
  4418. }
  4419. /* }}} */
  4420. void zend_do_begin_namespace(const znode *name, zend_bool with_bracket TSRMLS_DC) /* {{{ */
  4421. {
  4422. char *lcname;
  4423. /* handle mixed syntax declaration or nested namespaces */
  4424. if (!CG(has_bracketed_namespaces)) {
  4425. if (CG(current_namespace)) {
  4426. /* previous namespace declarations were unbracketed */
  4427. if (with_bracket) {
  4428. zend_error(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations with unbracketed namespace declarations");
  4429. }
  4430. }
  4431. } else {
  4432. /* previous namespace declarations were bracketed */
  4433. if (!with_bracket) {
  4434. zend_error(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations with unbracketed namespace declarations");
  4435. } else if (CG(current_namespace) || CG(in_namespace)) {
  4436. zend_error(E_COMPILE_ERROR, "Namespace declarations cannot be nested");
  4437. }
  4438. }
  4439. if (((!with_bracket && !CG(current_namespace)) || (with_bracket && !CG(has_bracketed_namespaces))) && CG(active_op_array)->last > 0) {
  4440. /* ignore ZEND_EXT_STMT and ZEND_TICKS */
  4441. int num = CG(active_op_array)->last;
  4442. while (num > 0 &&
  4443. (CG(active_op_array)->opcodes[num-1].opcode == ZEND_EXT_STMT ||
  4444. CG(active_op_array)->opcodes[num-1].opcode == ZEND_TICKS)) {
  4445. --num;
  4446. }
  4447. if (num > 0) {
  4448. zend_error(E_COMPILE_ERROR, "Namespace declaration statement has to be the very first statement in the script");
  4449. }
  4450. }
  4451. CG(in_namespace) = 1;
  4452. if (with_bracket) {
  4453. CG(has_bracketed_namespaces) = 1;
  4454. }
  4455. if (name) {
  4456. lcname = zend_str_tolower_dup(Z_STRVAL(name->u.constant), Z_STRLEN(name->u.constant));
  4457. if (((Z_STRLEN(name->u.constant) == sizeof("self")-1) &&
  4458. !memcmp(lcname, "self", sizeof("self")-1)) ||
  4459. ((Z_STRLEN(name->u.constant) == sizeof("parent")-1) &&
  4460. !memcmp(lcname, "parent", sizeof("parent")-1))) {
  4461. zend_error(E_COMPILE_ERROR, "Cannot use '%s' as namespace name", Z_STRVAL(name->u.constant));
  4462. }
  4463. efree(lcname);
  4464. if (CG(current_namespace)) {
  4465. zval_dtor(CG(current_namespace));
  4466. } else {
  4467. ALLOC_ZVAL(CG(current_namespace));
  4468. }
  4469. *CG(current_namespace) = name->u.constant;
  4470. } else {
  4471. if (CG(current_namespace)) {
  4472. zval_dtor(CG(current_namespace));
  4473. FREE_ZVAL(CG(current_namespace));
  4474. CG(current_namespace) = NULL;
  4475. }
  4476. }
  4477. if (CG(current_import)) {
  4478. zend_hash_destroy(CG(current_import));
  4479. efree(CG(current_import));
  4480. CG(current_import) = NULL;
  4481. }
  4482. }
  4483. /* }}} */
  4484. void zend_do_use(znode *ns_name, znode *new_name, int is_global TSRMLS_DC) /* {{{ */
  4485. {
  4486. char *lcname;
  4487. zval *name, *ns, tmp;
  4488. zend_bool warn = 0;
  4489. zend_class_entry **pce;
  4490. if (!CG(current_import)) {
  4491. CG(current_import) = emalloc(sizeof(HashTable));
  4492. zend_hash_init(CG(current_import), 0, NULL, ZVAL_PTR_DTOR, 0);
  4493. }
  4494. ALLOC_ZVAL(ns);
  4495. *ns = ns_name->u.constant;
  4496. if (new_name) {
  4497. name = &new_name->u.constant;
  4498. } else {
  4499. char *p;
  4500. /* The form "use A\B" is eqivalent to "use A\B as B".
  4501. So we extract the last part of compound name to use as a new_name */
  4502. name = &tmp;
  4503. p = zend_memrchr(Z_STRVAL_P(ns), '\\', Z_STRLEN_P(ns));
  4504. if (p) {
  4505. ZVAL_STRING(name, p+1, 1);
  4506. } else {
  4507. *name = *ns;
  4508. zval_copy_ctor(name);
  4509. warn = !is_global && !CG(current_namespace);
  4510. }
  4511. }
  4512. lcname = zend_str_tolower_dup(Z_STRVAL_P(name), Z_STRLEN_P(name));
  4513. if (((Z_STRLEN_P(name) == sizeof("self")-1) &&
  4514. !memcmp(lcname, "self", sizeof("self")-1)) ||
  4515. ((Z_STRLEN_P(name) == sizeof("parent")-1) &&
  4516. !memcmp(lcname, "parent", sizeof("parent")-1))) {
  4517. zend_error(E_COMPILE_ERROR, "Cannot use %s as %s because '%s' is a special class name", Z_STRVAL_P(ns), Z_STRVAL_P(name), Z_STRVAL_P(name));
  4518. }
  4519. if (CG(current_namespace)) {
  4520. /* Prefix import name with current namespace name to avoid conflicts with classes */
  4521. char *c_ns_name = emalloc(Z_STRLEN_P(CG(current_namespace)) + 1 + Z_STRLEN_P(name) + 1);
  4522. zend_str_tolower_copy(c_ns_name, Z_STRVAL_P(CG(current_namespace)), Z_STRLEN_P(CG(current_namespace)));
  4523. c_ns_name[Z_STRLEN_P(CG(current_namespace))] = '\\';
  4524. memcpy(c_ns_name+Z_STRLEN_P(CG(current_namespace))+1, lcname, Z_STRLEN_P(name)+1);
  4525. if (zend_hash_exists(CG(class_table), c_ns_name, Z_STRLEN_P(CG(current_namespace)) + 1 + Z_STRLEN_P(name)+1)) {
  4526. char *tmp2 = zend_str_tolower_dup(Z_STRVAL_P(ns), Z_STRLEN_P(ns));
  4527. if (Z_STRLEN_P(ns) != Z_STRLEN_P(CG(current_namespace)) + 1 + Z_STRLEN_P(name) ||
  4528. memcmp(tmp2, c_ns_name, Z_STRLEN_P(ns))) {
  4529. zend_error(E_COMPILE_ERROR, "Cannot use %s as %s because the name is already in use", Z_STRVAL_P(ns), Z_STRVAL_P(name));
  4530. }
  4531. efree(tmp2);
  4532. }
  4533. efree(c_ns_name);
  4534. } else if (zend_hash_find(CG(class_table), lcname, Z_STRLEN_P(name)+1, (void**)&pce) == SUCCESS &&
  4535. (*pce)->type == ZEND_USER_CLASS &&
  4536. (*pce)->filename == CG(compiled_filename)) {
  4537. char *c_tmp = zend_str_tolower_dup(Z_STRVAL_P(ns), Z_STRLEN_P(ns));
  4538. if (Z_STRLEN_P(ns) != Z_STRLEN_P(name) ||
  4539. memcmp(c_tmp, lcname, Z_STRLEN_P(ns))) {
  4540. zend_error(E_COMPILE_ERROR, "Cannot use %s as %s because the name is already in use", Z_STRVAL_P(ns), Z_STRVAL_P(name));
  4541. }
  4542. efree(c_tmp);
  4543. }
  4544. if (zend_hash_add(CG(current_import), lcname, Z_STRLEN_P(name)+1, &ns, sizeof(zval*), NULL) != SUCCESS) {
  4545. zend_error(E_COMPILE_ERROR, "Cannot use %s as %s because the name is already in use", Z_STRVAL_P(ns), Z_STRVAL_P(name));
  4546. }
  4547. if (warn) {
  4548. zend_error(E_WARNING, "The use statement with non-compound name '%s' has no effect", Z_STRVAL_P(name));
  4549. }
  4550. efree(lcname);
  4551. zval_dtor(name);
  4552. }
  4553. /* }}} */
  4554. void zend_do_declare_constant(znode *name, znode *value TSRMLS_DC) /* {{{ */
  4555. {
  4556. zend_op *opline;
  4557. if(Z_TYPE(value->u.constant) == IS_CONSTANT_ARRAY) {
  4558. zend_error(E_COMPILE_ERROR, "Arrays are not allowed as constants");
  4559. }
  4560. if (zend_get_ct_const(&name->u.constant, 0 TSRMLS_CC)) {
  4561. zend_error(E_COMPILE_ERROR, "Cannot redeclare constant '%s'", Z_STRVAL(name->u.constant));
  4562. }
  4563. if (CG(current_namespace)) {
  4564. /* Prefix constant name with name of current namespace, lowercased */
  4565. znode tmp;
  4566. tmp.op_type = IS_CONST;
  4567. tmp.u.constant = *CG(current_namespace);
  4568. Z_STRVAL(tmp.u.constant) = zend_str_tolower_dup(Z_STRVAL(tmp.u.constant), Z_STRLEN(tmp.u.constant));
  4569. zend_do_build_namespace_name(&tmp, &tmp, name TSRMLS_CC);
  4570. *name = tmp;
  4571. }
  4572. opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  4573. opline->opcode = ZEND_DECLARE_CONST;
  4574. SET_UNUSED(opline->result);
  4575. opline->op1 = *name;
  4576. opline->op2 = *value;
  4577. }
  4578. /* }}} */
  4579. void zend_verify_namespace(TSRMLS_D) /* {{{ */
  4580. {
  4581. if (CG(has_bracketed_namespaces) && !CG(in_namespace)) {
  4582. zend_error(E_COMPILE_ERROR, "No code may exist outside of namespace {}");
  4583. }
  4584. }
  4585. /* }}} */
  4586. void zend_do_end_namespace(TSRMLS_D) /* {{{ */
  4587. {
  4588. CG(in_namespace) = 0;
  4589. if (CG(current_namespace)) {
  4590. zval_dtor(CG(current_namespace));
  4591. FREE_ZVAL(CG(current_namespace));
  4592. CG(current_namespace) = NULL;
  4593. }
  4594. if (CG(current_import)) {
  4595. zend_hash_destroy(CG(current_import));
  4596. efree(CG(current_import));
  4597. CG(current_import) = NULL;
  4598. }
  4599. }
  4600. /* }}} */
  4601. void zend_do_end_compilation(TSRMLS_D) /* {{{ */
  4602. {
  4603. CG(has_bracketed_namespaces) = 0;
  4604. zend_do_end_namespace(TSRMLS_C);
  4605. }
  4606. /* }}} */
  4607. /* {{{ zend_dirname
  4608. Returns directory name component of path */
  4609. ZEND_API size_t zend_dirname(char *path, size_t len)
  4610. {
  4611. register char *end = path + len - 1;
  4612. unsigned int len_adjust = 0;
  4613. #ifdef PHP_WIN32
  4614. /* Note that on Win32 CWD is per drive (heritage from CP/M).
  4615. * This means dirname("c:foo") maps to "c:." or "c:" - which means CWD on C: drive.
  4616. */
  4617. if ((2 <= len) && isalpha((int)((unsigned char *)path)[0]) && (':' == path[1])) {
  4618. /* Skip over the drive spec (if any) so as not to change */
  4619. path += 2;
  4620. len_adjust += 2;
  4621. if (2 == len) {
  4622. /* Return "c:" on Win32 for dirname("c:").
  4623. * It would be more consistent to return "c:."
  4624. * but that would require making the string *longer*.
  4625. */
  4626. return len;
  4627. }
  4628. }
  4629. #elif defined(NETWARE)
  4630. /*
  4631. * Find the first occurence of : from the left
  4632. * move the path pointer to the position just after :
  4633. * increment the len_adjust to the length of path till colon character(inclusive)
  4634. * If there is no character beyond : simple return len
  4635. */
  4636. char *colonpos = NULL;
  4637. colonpos = strchr(path, ':');
  4638. if (colonpos != NULL) {
  4639. len_adjust = ((colonpos - path) + 1);
  4640. path += len_adjust;
  4641. if (len_adjust == len) {
  4642. return len;
  4643. }
  4644. }
  4645. #endif
  4646. if (len == 0) {
  4647. /* Illegal use of this function */
  4648. return 0;
  4649. }
  4650. /* Strip trailing slashes */
  4651. while (end >= path && IS_SLASH_P(end)) {
  4652. end--;
  4653. }
  4654. if (end < path) {
  4655. /* The path only contained slashes */
  4656. path[0] = DEFAULT_SLASH;
  4657. path[1] = '\0';
  4658. return 1 + len_adjust;
  4659. }
  4660. /* Strip filename */
  4661. while (end >= path && !IS_SLASH_P(end)) {
  4662. end--;
  4663. }
  4664. if (end < path) {
  4665. /* No slash found, therefore return '.' */
  4666. #ifdef NETWARE
  4667. if (len_adjust == 0) {
  4668. path[0] = '.';
  4669. path[1] = '\0';
  4670. return 1; /* only one character */
  4671. } else {
  4672. path[0] = '\0';
  4673. return len_adjust;
  4674. }
  4675. #else
  4676. path[0] = '.';
  4677. path[1] = '\0';
  4678. return 1 + len_adjust;
  4679. #endif
  4680. }
  4681. /* Strip slashes which came before the file name */
  4682. while (end >= path && IS_SLASH_P(end)) {
  4683. end--;
  4684. }
  4685. if (end < path) {
  4686. path[0] = DEFAULT_SLASH;
  4687. path[1] = '\0';
  4688. return 1 + len_adjust;
  4689. }
  4690. *(end+1) = '\0';
  4691. return (size_t)(end + 1 - path) + len_adjust;
  4692. }
  4693. /* }}} */
  4694. /*
  4695. * Local variables:
  4696. * tab-width: 4
  4697. * c-basic-offset: 4
  4698. * indent-tabs-mode: t
  4699. * End:
  4700. */