PageRenderTime 60ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/ext/reflection/php_reflection.c

http://github.com/php/php-src
C | 6370 lines | 4715 code | 900 blank | 755 comment | 1196 complexity | 65db7fe1e3d541645c5f50f6d0594b66 MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | http://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Timm Friebe <thekid@thekid.de> |
  14. | George Schlossnagle <george@omniti.com> |
  15. | Andrei Zmievski <andrei@gravitonic.com> |
  16. | Marcus Boerger <helly@php.net> |
  17. | Johannes Schlueter <johannes@php.net> |
  18. +----------------------------------------------------------------------+
  19. */
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include "php.h"
  24. #include "php_ini.h"
  25. #include "php_reflection.h"
  26. #include "ext/standard/info.h"
  27. #include "ext/standard/sha1.h"
  28. #include "ext/standard/php_random.h"
  29. #include "zend.h"
  30. #include "zend_API.h"
  31. #include "zend_exceptions.h"
  32. #include "zend_operators.h"
  33. #include "zend_constants.h"
  34. #include "zend_ini.h"
  35. #include "zend_interfaces.h"
  36. #include "zend_closures.h"
  37. #include "zend_generators.h"
  38. #include "zend_extensions.h"
  39. #include "zend_builtin_functions.h"
  40. #include "zend_smart_str.h"
  41. #include "php_reflection_arginfo.h"
  42. /* Key used to avoid leaking addresses in ReflectionProperty::getId() */
  43. #define REFLECTION_KEY_LEN 16
  44. ZEND_BEGIN_MODULE_GLOBALS(reflection)
  45. zend_bool key_initialized;
  46. unsigned char key[REFLECTION_KEY_LEN];
  47. ZEND_END_MODULE_GLOBALS(reflection)
  48. ZEND_DECLARE_MODULE_GLOBALS(reflection)
  49. #define REFLECTION_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(reflection, v)
  50. static zend_always_inline zval *reflection_prop_name(zval *object) {
  51. /* $name is always in the first property slot. */
  52. ZEND_ASSERT(Z_OBJCE_P(object)->default_properties_count >= 1);
  53. return &Z_OBJ_P(object)->properties_table[0];
  54. }
  55. static zend_always_inline zval *reflection_prop_class(zval *object) {
  56. /* $class is always in the second property slot. */
  57. ZEND_ASSERT(Z_OBJCE_P(object)->default_properties_count >= 2);
  58. return &Z_OBJ_P(object)->properties_table[1];
  59. }
  60. /* Class entry pointers */
  61. PHPAPI zend_class_entry *reflector_ptr;
  62. PHPAPI zend_class_entry *reflection_exception_ptr;
  63. PHPAPI zend_class_entry *reflection_ptr;
  64. PHPAPI zend_class_entry *reflection_function_abstract_ptr;
  65. PHPAPI zend_class_entry *reflection_function_ptr;
  66. PHPAPI zend_class_entry *reflection_generator_ptr;
  67. PHPAPI zend_class_entry *reflection_parameter_ptr;
  68. PHPAPI zend_class_entry *reflection_type_ptr;
  69. PHPAPI zend_class_entry *reflection_named_type_ptr;
  70. PHPAPI zend_class_entry *reflection_union_type_ptr;
  71. PHPAPI zend_class_entry *reflection_class_ptr;
  72. PHPAPI zend_class_entry *reflection_object_ptr;
  73. PHPAPI zend_class_entry *reflection_method_ptr;
  74. PHPAPI zend_class_entry *reflection_property_ptr;
  75. PHPAPI zend_class_entry *reflection_class_constant_ptr;
  76. PHPAPI zend_class_entry *reflection_extension_ptr;
  77. PHPAPI zend_class_entry *reflection_zend_extension_ptr;
  78. PHPAPI zend_class_entry *reflection_reference_ptr;
  79. /* Exception throwing macro */
  80. #define _DO_THROW(msg) \
  81. zend_throw_exception(reflection_exception_ptr, msg, 0);
  82. #define GET_REFLECTION_OBJECT() do { \
  83. intern = Z_REFLECTION_P(ZEND_THIS); \
  84. if (intern->ptr == NULL) { \
  85. if (EG(exception) && EG(exception)->ce == reflection_exception_ptr) { \
  86. RETURN_THROWS(); \
  87. } \
  88. zend_throw_error(NULL, "Internal error: Failed to retrieve the reflection object"); \
  89. RETURN_THROWS(); \
  90. } \
  91. } while (0)
  92. #define GET_REFLECTION_OBJECT_PTR(target) do { \
  93. GET_REFLECTION_OBJECT(); \
  94. target = intern->ptr; \
  95. } while (0)
  96. /* Class constants */
  97. #define REGISTER_REFLECTION_CLASS_CONST_LONG(class_name, const_name, value) \
  98. zend_declare_class_constant_long(reflection_ ## class_name ## _ptr, const_name, sizeof(const_name)-1, (zend_long)value);
  99. /* {{{ Object structure */
  100. /* Struct for properties */
  101. typedef struct _property_reference {
  102. zend_property_info *prop;
  103. zend_string *unmangled_name;
  104. } property_reference;
  105. /* Struct for parameters */
  106. typedef struct _parameter_reference {
  107. uint32_t offset;
  108. zend_bool required;
  109. struct _zend_arg_info *arg_info;
  110. zend_function *fptr;
  111. } parameter_reference;
  112. /* Struct for type hints */
  113. typedef struct _type_reference {
  114. zend_type type;
  115. /* Whether to use backwards compatible null representation */
  116. zend_bool legacy_behavior;
  117. } type_reference;
  118. typedef enum {
  119. REF_TYPE_OTHER, /* Must be 0 */
  120. REF_TYPE_FUNCTION,
  121. REF_TYPE_GENERATOR,
  122. REF_TYPE_PARAMETER,
  123. REF_TYPE_TYPE,
  124. REF_TYPE_PROPERTY,
  125. REF_TYPE_CLASS_CONSTANT
  126. } reflection_type_t;
  127. /* Struct for reflection objects */
  128. typedef struct {
  129. zval obj;
  130. void *ptr;
  131. zend_class_entry *ce;
  132. reflection_type_t ref_type;
  133. unsigned int ignore_visibility:1;
  134. zend_object zo;
  135. } reflection_object;
  136. static inline reflection_object *reflection_object_from_obj(zend_object *obj) {
  137. return (reflection_object*)((char*)(obj) - XtOffsetOf(reflection_object, zo));
  138. }
  139. #define Z_REFLECTION_P(zv) reflection_object_from_obj(Z_OBJ_P((zv)))
  140. /* }}} */
  141. static zend_object_handlers reflection_object_handlers;
  142. static zend_always_inline uint32_t prop_get_flags(property_reference *ref) {
  143. return ref->prop ? ref->prop->flags : ZEND_ACC_PUBLIC;
  144. }
  145. static inline zend_bool is_closure_invoke(zend_class_entry *ce, zend_string *lcname) {
  146. return ce == zend_ce_closure
  147. && zend_string_equals_literal(lcname, ZEND_INVOKE_FUNC_NAME);
  148. }
  149. static void _default_get_name(zval *object, zval *return_value) /* {{{ */
  150. {
  151. zval *name = reflection_prop_name(object);
  152. if (Z_ISUNDEF_P(name)) {
  153. RETURN_FALSE;
  154. }
  155. ZVAL_COPY(return_value, name);
  156. }
  157. /* }}} */
  158. static zend_function *_copy_function(zend_function *fptr) /* {{{ */
  159. {
  160. if (fptr
  161. && (fptr->internal_function.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE))
  162. {
  163. zend_function *copy_fptr;
  164. copy_fptr = emalloc(sizeof(zend_function));
  165. memcpy(copy_fptr, fptr, sizeof(zend_function));
  166. copy_fptr->internal_function.function_name = zend_string_copy(fptr->internal_function.function_name);
  167. return copy_fptr;
  168. } else {
  169. /* no copy needed */
  170. return fptr;
  171. }
  172. }
  173. /* }}} */
  174. static void _free_function(zend_function *fptr) /* {{{ */
  175. {
  176. if (fptr
  177. && (fptr->internal_function.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE))
  178. {
  179. zend_string_release_ex(fptr->internal_function.function_name, 0);
  180. zend_free_trampoline(fptr);
  181. }
  182. }
  183. /* }}} */
  184. static void reflection_free_objects_storage(zend_object *object) /* {{{ */
  185. {
  186. reflection_object *intern = reflection_object_from_obj(object);
  187. parameter_reference *reference;
  188. property_reference *prop_reference;
  189. if (intern->ptr) {
  190. switch (intern->ref_type) {
  191. case REF_TYPE_PARAMETER:
  192. reference = (parameter_reference*)intern->ptr;
  193. _free_function(reference->fptr);
  194. efree(intern->ptr);
  195. break;
  196. case REF_TYPE_TYPE:
  197. {
  198. type_reference *type_ref = intern->ptr;
  199. if (ZEND_TYPE_HAS_NAME(type_ref->type)) {
  200. zend_string_release(ZEND_TYPE_NAME(type_ref->type));
  201. }
  202. efree(type_ref);
  203. break;
  204. }
  205. case REF_TYPE_FUNCTION:
  206. _free_function(intern->ptr);
  207. break;
  208. case REF_TYPE_PROPERTY:
  209. prop_reference = (property_reference*)intern->ptr;
  210. zend_string_release_ex(prop_reference->unmangled_name, 0);
  211. efree(intern->ptr);
  212. break;
  213. case REF_TYPE_GENERATOR:
  214. case REF_TYPE_CLASS_CONSTANT:
  215. case REF_TYPE_OTHER:
  216. break;
  217. }
  218. }
  219. intern->ptr = NULL;
  220. zval_ptr_dtor(&intern->obj);
  221. zend_object_std_dtor(object);
  222. }
  223. /* }}} */
  224. static HashTable *reflection_get_gc(zend_object *obj, zval **gc_data, int *gc_data_count) /* {{{ */
  225. {
  226. reflection_object *intern = reflection_object_from_obj(obj);
  227. *gc_data = &intern->obj;
  228. *gc_data_count = 1;
  229. return zend_std_get_properties(obj);
  230. }
  231. /* }}} */
  232. static zend_object *reflection_objects_new(zend_class_entry *class_type) /* {{{ */
  233. {
  234. reflection_object *intern = zend_object_alloc(sizeof(reflection_object), class_type);
  235. zend_object_std_init(&intern->zo, class_type);
  236. object_properties_init(&intern->zo, class_type);
  237. intern->zo.handlers = &reflection_object_handlers;
  238. return &intern->zo;
  239. }
  240. /* }}} */
  241. static zval *reflection_instantiate(zend_class_entry *pce, zval *object) /* {{{ */
  242. {
  243. object_init_ex(object, pce);
  244. return object;
  245. }
  246. /* }}} */
  247. static void _const_string(smart_str *str, char *name, zval *value, char *indent);
  248. static void _function_string(smart_str *str, zend_function *fptr, zend_class_entry *scope, char* indent);
  249. static void _property_string(smart_str *str, zend_property_info *prop, const char *prop_name, char* indent);
  250. static void _class_const_string(smart_str *str, char *name, zend_class_constant *c, char* indent);
  251. static void _class_string(smart_str *str, zend_class_entry *ce, zval *obj, char *indent);
  252. static void _extension_string(smart_str *str, zend_module_entry *module, char *indent);
  253. static void _zend_extension_string(smart_str *str, zend_extension *extension, char *indent);
  254. /* {{{ _class_string */
  255. static void _class_string(smart_str *str, zend_class_entry *ce, zval *obj, char *indent)
  256. {
  257. int count, count_static_props = 0, count_static_funcs = 0, count_shadow_props = 0;
  258. zend_string *sub_indent = strpprintf(0, "%s ", indent);
  259. /* TBD: Repair indenting of doc comment (or is this to be done in the parser?) */
  260. if (ce->type == ZEND_USER_CLASS && ce->info.user.doc_comment) {
  261. smart_str_append_printf(str, "%s%s", indent, ZSTR_VAL(ce->info.user.doc_comment));
  262. smart_str_appendc(str, '\n');
  263. }
  264. if (obj && Z_TYPE_P(obj) == IS_OBJECT) {
  265. smart_str_append_printf(str, "%sObject of class [ ", indent);
  266. } else {
  267. char *kind = "Class";
  268. if (ce->ce_flags & ZEND_ACC_INTERFACE) {
  269. kind = "Interface";
  270. } else if (ce->ce_flags & ZEND_ACC_TRAIT) {
  271. kind = "Trait";
  272. }
  273. smart_str_append_printf(str, "%s%s [ ", indent, kind);
  274. }
  275. smart_str_append_printf(str, (ce->type == ZEND_USER_CLASS) ? "<user" : "<internal");
  276. if (ce->type == ZEND_INTERNAL_CLASS && ce->info.internal.module) {
  277. smart_str_append_printf(str, ":%s", ce->info.internal.module->name);
  278. }
  279. smart_str_append_printf(str, "> ");
  280. if (ce->get_iterator != NULL) {
  281. smart_str_append_printf(str, "<iterateable> ");
  282. }
  283. if (ce->ce_flags & ZEND_ACC_INTERFACE) {
  284. smart_str_append_printf(str, "interface ");
  285. } else if (ce->ce_flags & ZEND_ACC_TRAIT) {
  286. smart_str_append_printf(str, "trait ");
  287. } else {
  288. if (ce->ce_flags & (ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) {
  289. smart_str_append_printf(str, "abstract ");
  290. }
  291. if (ce->ce_flags & ZEND_ACC_FINAL) {
  292. smart_str_append_printf(str, "final ");
  293. }
  294. smart_str_append_printf(str, "class ");
  295. }
  296. smart_str_append_printf(str, "%s", ZSTR_VAL(ce->name));
  297. if (ce->parent) {
  298. smart_str_append_printf(str, " extends %s", ZSTR_VAL(ce->parent->name));
  299. }
  300. if (ce->num_interfaces) {
  301. uint32_t i;
  302. ZEND_ASSERT(ce->ce_flags & ZEND_ACC_LINKED);
  303. if (ce->ce_flags & ZEND_ACC_INTERFACE) {
  304. smart_str_append_printf(str, " extends %s", ZSTR_VAL(ce->interfaces[0]->name));
  305. } else {
  306. smart_str_append_printf(str, " implements %s", ZSTR_VAL(ce->interfaces[0]->name));
  307. }
  308. for (i = 1; i < ce->num_interfaces; ++i) {
  309. smart_str_append_printf(str, ", %s", ZSTR_VAL(ce->interfaces[i]->name));
  310. }
  311. }
  312. smart_str_append_printf(str, " ] {\n");
  313. /* The information where a class is declared is only available for user classes */
  314. if (ce->type == ZEND_USER_CLASS) {
  315. smart_str_append_printf(str, "%s @@ %s %d-%d\n", indent, ZSTR_VAL(ce->info.user.filename),
  316. ce->info.user.line_start, ce->info.user.line_end);
  317. }
  318. /* Constants */
  319. smart_str_append_printf(str, "\n");
  320. count = zend_hash_num_elements(&ce->constants_table);
  321. smart_str_append_printf(str, "%s - Constants [%d] {\n", indent, count);
  322. if (count > 0) {
  323. zend_string *key;
  324. zend_class_constant *c;
  325. ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->constants_table, key, c) {
  326. _class_const_string(str, ZSTR_VAL(key), c, ZSTR_VAL(sub_indent));
  327. if (UNEXPECTED(EG(exception))) {
  328. return;
  329. }
  330. } ZEND_HASH_FOREACH_END();
  331. }
  332. smart_str_append_printf(str, "%s }\n", indent);
  333. /* Static properties */
  334. /* counting static properties */
  335. count = zend_hash_num_elements(&ce->properties_info);
  336. if (count > 0) {
  337. zend_property_info *prop;
  338. ZEND_HASH_FOREACH_PTR(&ce->properties_info, prop) {
  339. if ((prop->flags & ZEND_ACC_PRIVATE) && prop->ce != ce) {
  340. count_shadow_props++;
  341. } else if (prop->flags & ZEND_ACC_STATIC) {
  342. count_static_props++;
  343. }
  344. } ZEND_HASH_FOREACH_END();
  345. }
  346. /* static properties */
  347. smart_str_append_printf(str, "\n%s - Static properties [%d] {\n", indent, count_static_props);
  348. if (count_static_props > 0) {
  349. zend_property_info *prop;
  350. ZEND_HASH_FOREACH_PTR(&ce->properties_info, prop) {
  351. if ((prop->flags & ZEND_ACC_STATIC) && (!(prop->flags & ZEND_ACC_PRIVATE) || prop->ce == ce)) {
  352. _property_string(str, prop, NULL, ZSTR_VAL(sub_indent));
  353. }
  354. } ZEND_HASH_FOREACH_END();
  355. }
  356. smart_str_append_printf(str, "%s }\n", indent);
  357. /* Static methods */
  358. /* counting static methods */
  359. count = zend_hash_num_elements(&ce->function_table);
  360. if (count > 0) {
  361. zend_function *mptr;
  362. ZEND_HASH_FOREACH_PTR(&ce->function_table, mptr) {
  363. if (mptr->common.fn_flags & ZEND_ACC_STATIC
  364. && ((mptr->common.fn_flags & ZEND_ACC_PRIVATE) == 0 || mptr->common.scope == ce))
  365. {
  366. count_static_funcs++;
  367. }
  368. } ZEND_HASH_FOREACH_END();
  369. }
  370. /* static methods */
  371. smart_str_append_printf(str, "\n%s - Static methods [%d] {", indent, count_static_funcs);
  372. if (count_static_funcs > 0) {
  373. zend_function *mptr;
  374. ZEND_HASH_FOREACH_PTR(&ce->function_table, mptr) {
  375. if (mptr->common.fn_flags & ZEND_ACC_STATIC
  376. && ((mptr->common.fn_flags & ZEND_ACC_PRIVATE) == 0 || mptr->common.scope == ce))
  377. {
  378. smart_str_append_printf(str, "\n");
  379. _function_string(str, mptr, ce, ZSTR_VAL(sub_indent));
  380. }
  381. } ZEND_HASH_FOREACH_END();
  382. } else {
  383. smart_str_append_printf(str, "\n");
  384. }
  385. smart_str_append_printf(str, "%s }\n", indent);
  386. /* Default/Implicit properties */
  387. count = zend_hash_num_elements(&ce->properties_info) - count_static_props - count_shadow_props;
  388. smart_str_append_printf(str, "\n%s - Properties [%d] {\n", indent, count);
  389. if (count > 0) {
  390. zend_property_info *prop;
  391. ZEND_HASH_FOREACH_PTR(&ce->properties_info, prop) {
  392. if (!(prop->flags & ZEND_ACC_STATIC)
  393. && (!(prop->flags & ZEND_ACC_PRIVATE) || prop->ce == ce)) {
  394. _property_string(str, prop, NULL, ZSTR_VAL(sub_indent));
  395. }
  396. } ZEND_HASH_FOREACH_END();
  397. }
  398. smart_str_append_printf(str, "%s }\n", indent);
  399. if (obj && Z_TYPE_P(obj) == IS_OBJECT) {
  400. HashTable *properties = Z_OBJ_HT_P(obj)->get_properties(Z_OBJ_P(obj));
  401. zend_string *prop_name;
  402. smart_str prop_str = {0};
  403. count = 0;
  404. if (properties && zend_hash_num_elements(properties)) {
  405. ZEND_HASH_FOREACH_STR_KEY(properties, prop_name) {
  406. if (prop_name && ZSTR_LEN(prop_name) && ZSTR_VAL(prop_name)[0]) { /* skip all private and protected properties */
  407. if (!zend_hash_exists(&ce->properties_info, prop_name)) {
  408. count++;
  409. _property_string(&prop_str, NULL, ZSTR_VAL(prop_name), ZSTR_VAL(sub_indent));
  410. }
  411. }
  412. } ZEND_HASH_FOREACH_END();
  413. }
  414. smart_str_append_printf(str, "\n%s - Dynamic properties [%d] {\n", indent, count);
  415. smart_str_append_smart_str(str, &prop_str);
  416. smart_str_append_printf(str, "%s }\n", indent);
  417. smart_str_free(&prop_str);
  418. }
  419. /* Non static methods */
  420. count = zend_hash_num_elements(&ce->function_table) - count_static_funcs;
  421. if (count > 0) {
  422. zend_function *mptr;
  423. smart_str method_str = {0};
  424. count = 0;
  425. ZEND_HASH_FOREACH_PTR(&ce->function_table, mptr) {
  426. if ((mptr->common.fn_flags & ZEND_ACC_STATIC) == 0
  427. && ((mptr->common.fn_flags & ZEND_ACC_PRIVATE) == 0 || mptr->common.scope == ce))
  428. {
  429. zend_function *closure;
  430. /* see if this is a closure */
  431. if (obj && is_closure_invoke(ce, mptr->common.function_name)
  432. && (closure = zend_get_closure_invoke_method(Z_OBJ_P(obj))) != NULL)
  433. {
  434. mptr = closure;
  435. } else {
  436. closure = NULL;
  437. }
  438. smart_str_appendc(&method_str, '\n');
  439. _function_string(&method_str, mptr, ce, ZSTR_VAL(sub_indent));
  440. count++;
  441. _free_function(closure);
  442. }
  443. } ZEND_HASH_FOREACH_END();
  444. smart_str_append_printf(str, "\n%s - Methods [%d] {", indent, count);
  445. smart_str_append_smart_str(str, &method_str);
  446. if (!count) {
  447. smart_str_append_printf(str, "\n");
  448. }
  449. smart_str_free(&method_str);
  450. } else {
  451. smart_str_append_printf(str, "\n%s - Methods [0] {\n", indent);
  452. }
  453. smart_str_append_printf(str, "%s }\n", indent);
  454. smart_str_append_printf(str, "%s}\n", indent);
  455. zend_string_release_ex(sub_indent, 0);
  456. }
  457. /* }}} */
  458. /* {{{ _const_string */
  459. static void _const_string(smart_str *str, char *name, zval *value, char *indent)
  460. {
  461. const char *type = zend_zval_type_name(value);
  462. if (Z_TYPE_P(value) == IS_ARRAY) {
  463. smart_str_append_printf(str, "%s Constant [ %s %s ] { Array }\n",
  464. indent, type, name);
  465. } else if (Z_TYPE_P(value) == IS_STRING) {
  466. smart_str_append_printf(str, "%s Constant [ %s %s ] { %s }\n",
  467. indent, type, name, Z_STRVAL_P(value));
  468. } else {
  469. zend_string *tmp_value_str;
  470. zend_string *value_str = zval_get_tmp_string(value, &tmp_value_str);
  471. smart_str_append_printf(str, "%s Constant [ %s %s ] { %s }\n",
  472. indent, type, name, ZSTR_VAL(value_str));
  473. zend_tmp_string_release(tmp_value_str);
  474. }
  475. }
  476. /* }}} */
  477. /* {{{ _class_const_string */
  478. static void _class_const_string(smart_str *str, char *name, zend_class_constant *c, char *indent)
  479. {
  480. char *visibility = zend_visibility_string(Z_ACCESS_FLAGS(c->value));
  481. const char *type;
  482. zval_update_constant_ex(&c->value, c->ce);
  483. type = zend_zval_type_name(&c->value);
  484. if (Z_TYPE(c->value) == IS_ARRAY) {
  485. smart_str_append_printf(str, "%sConstant [ %s %s %s ] { Array }\n",
  486. indent, visibility, type, name);
  487. } else {
  488. zend_string *tmp_value_str;
  489. zend_string *value_str = zval_get_tmp_string(&c->value, &tmp_value_str);
  490. smart_str_append_printf(str, "%sConstant [ %s %s %s ] { %s }\n",
  491. indent, visibility, type, name, ZSTR_VAL(value_str));
  492. zend_tmp_string_release(tmp_value_str);
  493. }
  494. }
  495. /* }}} */
  496. static zend_op *get_recv_op(zend_op_array *op_array, uint32_t offset)
  497. {
  498. zend_op *op = op_array->opcodes;
  499. zend_op *end = op + op_array->last;
  500. ++offset;
  501. while (op < end) {
  502. if ((op->opcode == ZEND_RECV || op->opcode == ZEND_RECV_INIT
  503. || op->opcode == ZEND_RECV_VARIADIC) && op->op1.num == offset)
  504. {
  505. return op;
  506. }
  507. ++op;
  508. }
  509. ZEND_ASSERT(0 && "Failed to find op");
  510. return NULL;
  511. }
  512. static zval *get_default_from_recv(zend_op_array *op_array, uint32_t offset) {
  513. zend_op *recv = get_recv_op(op_array, offset);
  514. if (!recv || recv->opcode != ZEND_RECV_INIT) {
  515. return NULL;
  516. }
  517. return RT_CONSTANT(recv, recv->op2);
  518. }
  519. static int format_default_value(smart_str *str, zval *value, zend_class_entry *scope) {
  520. zval zv;
  521. ZVAL_COPY(&zv, value);
  522. if (UNEXPECTED(zval_update_constant_ex(&zv, scope) == FAILURE)) {
  523. zval_ptr_dtor(&zv);
  524. return FAILURE;
  525. }
  526. if (Z_TYPE(zv) == IS_TRUE) {
  527. smart_str_appends(str, "true");
  528. } else if (Z_TYPE(zv) == IS_FALSE) {
  529. smart_str_appends(str, "false");
  530. } else if (Z_TYPE(zv) == IS_NULL) {
  531. smart_str_appends(str, "NULL");
  532. } else if (Z_TYPE(zv) == IS_STRING) {
  533. smart_str_appendc(str, '\'');
  534. smart_str_appendl(str, Z_STRVAL(zv), MIN(Z_STRLEN(zv), 15));
  535. if (Z_STRLEN(zv) > 15) {
  536. smart_str_appends(str, "...");
  537. }
  538. smart_str_appendc(str, '\'');
  539. } else if (Z_TYPE(zv) == IS_ARRAY) {
  540. smart_str_appends(str, "Array");
  541. } else {
  542. zend_string *tmp_zv_str;
  543. zend_string *zv_str = zval_get_tmp_string(&zv, &tmp_zv_str);
  544. smart_str_append(str, zv_str);
  545. zend_tmp_string_release(tmp_zv_str);
  546. }
  547. zval_ptr_dtor(&zv);
  548. return SUCCESS;
  549. }
  550. static inline zend_bool has_internal_arg_info(const zend_function *fptr) {
  551. return fptr->type == ZEND_INTERNAL_FUNCTION
  552. && !(fptr->common.fn_flags & ZEND_ACC_USER_ARG_INFO);
  553. }
  554. /* {{{ _parameter_string */
  555. static void _parameter_string(smart_str *str, zend_function *fptr, struct _zend_arg_info *arg_info, uint32_t offset, zend_bool required, char* indent)
  556. {
  557. smart_str_append_printf(str, "Parameter #%d [ ", offset);
  558. if (!required) {
  559. smart_str_append_printf(str, "<optional> ");
  560. } else {
  561. smart_str_append_printf(str, "<required> ");
  562. }
  563. if (ZEND_TYPE_IS_SET(arg_info->type)) {
  564. zend_string *type_str = zend_type_to_string(arg_info->type);
  565. smart_str_append(str, type_str);
  566. smart_str_appendc(str, ' ');
  567. zend_string_release(type_str);
  568. }
  569. if (ZEND_ARG_SEND_MODE(arg_info)) {
  570. smart_str_appendc(str, '&');
  571. }
  572. if (ZEND_ARG_IS_VARIADIC(arg_info)) {
  573. smart_str_appends(str, "...");
  574. }
  575. smart_str_append_printf(str, "$%s", has_internal_arg_info(fptr)
  576. ? ((zend_internal_arg_info*)arg_info)->name : ZSTR_VAL(arg_info->name));
  577. if (!required) {
  578. if (fptr->type == ZEND_INTERNAL_FUNCTION) {
  579. smart_str_appends(str, " = ");
  580. if (((zend_internal_arg_info*)arg_info)->default_value) {
  581. smart_str_appends(str, ((zend_internal_arg_info*)arg_info)->default_value);
  582. } else {
  583. smart_str_appends(str, "<default>");
  584. }
  585. } else {
  586. zval *default_value = get_default_from_recv((zend_op_array*)fptr, offset);
  587. if (default_value) {
  588. smart_str_appends(str, " = ");
  589. if (format_default_value(str, default_value, fptr->common.scope) == FAILURE) {
  590. return;
  591. }
  592. }
  593. }
  594. }
  595. smart_str_appends(str, " ]");
  596. }
  597. /* }}} */
  598. /* {{{ _function_parameter_string */
  599. static void _function_parameter_string(smart_str *str, zend_function *fptr, char* indent)
  600. {
  601. struct _zend_arg_info *arg_info = fptr->common.arg_info;
  602. uint32_t i, num_args, num_required = fptr->common.required_num_args;
  603. if (!arg_info) {
  604. return;
  605. }
  606. num_args = fptr->common.num_args;
  607. if (fptr->common.fn_flags & ZEND_ACC_VARIADIC) {
  608. num_args++;
  609. }
  610. smart_str_appendc(str, '\n');
  611. smart_str_append_printf(str, "%s- Parameters [%d] {\n", indent, num_args);
  612. for (i = 0; i < num_args; i++) {
  613. smart_str_append_printf(str, "%s ", indent);
  614. _parameter_string(str, fptr, arg_info, i, i < num_required, indent);
  615. smart_str_appendc(str, '\n');
  616. arg_info++;
  617. }
  618. smart_str_append_printf(str, "%s}\n", indent);
  619. }
  620. /* }}} */
  621. /* {{{ _function_closure_string */
  622. static void _function_closure_string(smart_str *str, zend_function *fptr, char* indent)
  623. {
  624. uint32_t i, count;
  625. zend_string *key;
  626. HashTable *static_variables;
  627. if (fptr->type != ZEND_USER_FUNCTION || !fptr->op_array.static_variables) {
  628. return;
  629. }
  630. static_variables = ZEND_MAP_PTR_GET(fptr->op_array.static_variables_ptr);
  631. count = zend_hash_num_elements(static_variables);
  632. if (!count) {
  633. return;
  634. }
  635. smart_str_append_printf(str, "\n");
  636. smart_str_append_printf(str, "%s- Bound Variables [%d] {\n", indent, zend_hash_num_elements(static_variables));
  637. i = 0;
  638. ZEND_HASH_FOREACH_STR_KEY(static_variables, key) {
  639. smart_str_append_printf(str, "%s Variable #%d [ $%s ]\n", indent, i++, ZSTR_VAL(key));
  640. } ZEND_HASH_FOREACH_END();
  641. smart_str_append_printf(str, "%s}\n", indent);
  642. }
  643. /* }}} */
  644. /* {{{ _function_string */
  645. static void _function_string(smart_str *str, zend_function *fptr, zend_class_entry *scope, char* indent)
  646. {
  647. smart_str param_indent = {0};
  648. zend_function *overwrites;
  649. zend_string *lc_name;
  650. /* TBD: Repair indenting of doc comment (or is this to be done in the parser?)
  651. * What's "wrong" is that any whitespace before the doc comment start is
  652. * swallowed, leading to an unaligned comment.
  653. */
  654. if (fptr->type == ZEND_USER_FUNCTION && fptr->op_array.doc_comment) {
  655. smart_str_append_printf(str, "%s%s\n", indent, ZSTR_VAL(fptr->op_array.doc_comment));
  656. }
  657. smart_str_appendl(str, indent, strlen(indent));
  658. smart_str_append_printf(str, fptr->common.fn_flags & ZEND_ACC_CLOSURE ? "Closure [ " : (fptr->common.scope ? "Method [ " : "Function [ "));
  659. smart_str_append_printf(str, (fptr->type == ZEND_USER_FUNCTION) ? "<user" : "<internal");
  660. if (fptr->common.fn_flags & ZEND_ACC_DEPRECATED) {
  661. smart_str_appends(str, ", deprecated");
  662. }
  663. if (fptr->type == ZEND_INTERNAL_FUNCTION && ((zend_internal_function*)fptr)->module) {
  664. smart_str_append_printf(str, ":%s", ((zend_internal_function*)fptr)->module->name);
  665. }
  666. if (scope && fptr->common.scope) {
  667. if (fptr->common.scope != scope) {
  668. smart_str_append_printf(str, ", inherits %s", ZSTR_VAL(fptr->common.scope->name));
  669. } else if (fptr->common.scope->parent) {
  670. lc_name = zend_string_tolower(fptr->common.function_name);
  671. if ((overwrites = zend_hash_find_ptr(&fptr->common.scope->parent->function_table, lc_name)) != NULL) {
  672. if (fptr->common.scope != overwrites->common.scope) {
  673. smart_str_append_printf(str, ", overwrites %s", ZSTR_VAL(overwrites->common.scope->name));
  674. }
  675. }
  676. zend_string_release_ex(lc_name, 0);
  677. }
  678. }
  679. if (fptr->common.prototype && fptr->common.prototype->common.scope) {
  680. smart_str_append_printf(str, ", prototype %s", ZSTR_VAL(fptr->common.prototype->common.scope->name));
  681. }
  682. if (fptr->common.fn_flags & ZEND_ACC_CTOR) {
  683. smart_str_appends(str, ", ctor");
  684. }
  685. smart_str_appends(str, "> ");
  686. if (fptr->common.fn_flags & ZEND_ACC_ABSTRACT) {
  687. smart_str_appends(str, "abstract ");
  688. }
  689. if (fptr->common.fn_flags & ZEND_ACC_FINAL) {
  690. smart_str_appends(str, "final ");
  691. }
  692. if (fptr->common.fn_flags & ZEND_ACC_STATIC) {
  693. smart_str_appends(str, "static ");
  694. }
  695. if (fptr->common.scope) {
  696. /* These are mutually exclusive */
  697. switch (fptr->common.fn_flags & ZEND_ACC_PPP_MASK) {
  698. case ZEND_ACC_PUBLIC:
  699. smart_str_appends(str, "public ");
  700. break;
  701. case ZEND_ACC_PRIVATE:
  702. smart_str_appends(str, "private ");
  703. break;
  704. case ZEND_ACC_PROTECTED:
  705. smart_str_appends(str, "protected ");
  706. break;
  707. default:
  708. smart_str_appends(str, "<visibility error> ");
  709. break;
  710. }
  711. smart_str_appends(str, "method ");
  712. } else {
  713. smart_str_appends(str, "function ");
  714. }
  715. if (fptr->op_array.fn_flags & ZEND_ACC_RETURN_REFERENCE) {
  716. smart_str_appendc(str, '&');
  717. }
  718. smart_str_append_printf(str, "%s ] {\n", ZSTR_VAL(fptr->common.function_name));
  719. /* The information where a function is declared is only available for user classes */
  720. if (fptr->type == ZEND_USER_FUNCTION) {
  721. smart_str_append_printf(str, "%s @@ %s %d - %d\n", indent,
  722. ZSTR_VAL(fptr->op_array.filename),
  723. fptr->op_array.line_start,
  724. fptr->op_array.line_end);
  725. }
  726. smart_str_append_printf(&param_indent, "%s ", indent);
  727. smart_str_0(&param_indent);
  728. if (fptr->common.fn_flags & ZEND_ACC_CLOSURE) {
  729. _function_closure_string(str, fptr, ZSTR_VAL(param_indent.s));
  730. }
  731. _function_parameter_string(str, fptr, ZSTR_VAL(param_indent.s));
  732. smart_str_free(&param_indent);
  733. if (fptr->op_array.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
  734. smart_str_append_printf(str, " %s- Return [ ", indent);
  735. if (ZEND_TYPE_IS_SET(fptr->common.arg_info[-1].type)) {
  736. zend_string *type_str = zend_type_to_string(fptr->common.arg_info[-1].type);
  737. smart_str_append_printf(str, "%s ", ZSTR_VAL(type_str));
  738. zend_string_release(type_str);
  739. }
  740. smart_str_appends(str, "]\n");
  741. }
  742. smart_str_append_printf(str, "%s}\n", indent);
  743. }
  744. /* }}} */
  745. static zval *property_get_default(zend_property_info *prop_info) {
  746. zend_class_entry *ce = prop_info->ce;
  747. if (prop_info->flags & ZEND_ACC_STATIC) {
  748. zval *prop = &ce->default_static_members_table[prop_info->offset];
  749. ZVAL_DEINDIRECT(prop);
  750. return prop;
  751. } else {
  752. return &ce->default_properties_table[OBJ_PROP_TO_NUM(prop_info->offset)];
  753. }
  754. }
  755. /* {{{ _property_string */
  756. static void _property_string(smart_str *str, zend_property_info *prop, const char *prop_name, char* indent)
  757. {
  758. smart_str_append_printf(str, "%sProperty [ ", indent);
  759. if (!prop) {
  760. smart_str_append_printf(str, "<dynamic> public $%s", prop_name);
  761. } else {
  762. /* These are mutually exclusive */
  763. switch (prop->flags & ZEND_ACC_PPP_MASK) {
  764. case ZEND_ACC_PUBLIC:
  765. smart_str_appends(str, "public ");
  766. break;
  767. case ZEND_ACC_PRIVATE:
  768. smart_str_appends(str, "private ");
  769. break;
  770. case ZEND_ACC_PROTECTED:
  771. smart_str_appends(str, "protected ");
  772. break;
  773. }
  774. if (prop->flags & ZEND_ACC_STATIC) {
  775. smart_str_appends(str, "static ");
  776. }
  777. if (ZEND_TYPE_IS_SET(prop->type)) {
  778. zend_string *type_str = zend_type_to_string(prop->type);
  779. smart_str_append(str, type_str);
  780. smart_str_appendc(str, ' ');
  781. zend_string_release(type_str);
  782. }
  783. if (!prop_name) {
  784. const char *class_name;
  785. zend_unmangle_property_name(prop->name, &class_name, &prop_name);
  786. }
  787. smart_str_append_printf(str, "$%s", prop_name);
  788. zval *default_value = property_get_default(prop);
  789. if (!Z_ISUNDEF_P(default_value)) {
  790. smart_str_appends(str, " = ");
  791. if (format_default_value(str, default_value, prop->ce) == FAILURE) {
  792. return;
  793. }
  794. }
  795. }
  796. smart_str_appends(str, " ]\n");
  797. }
  798. /* }}} */
  799. static void _extension_ini_string(zend_ini_entry *ini_entry, smart_str *str, char *indent, int number) /* {{{ */
  800. {
  801. char *comma = "";
  802. if (number == ini_entry->module_number) {
  803. smart_str_append_printf(str, " %sEntry [ %s <", indent, ZSTR_VAL(ini_entry->name));
  804. if (ini_entry->modifiable == ZEND_INI_ALL) {
  805. smart_str_appends(str, "ALL");
  806. } else {
  807. if (ini_entry->modifiable & ZEND_INI_USER) {
  808. smart_str_appends(str, "USER");
  809. comma = ",";
  810. }
  811. if (ini_entry->modifiable & ZEND_INI_PERDIR) {
  812. smart_str_append_printf(str, "%sPERDIR", comma);
  813. comma = ",";
  814. }
  815. if (ini_entry->modifiable & ZEND_INI_SYSTEM) {
  816. smart_str_append_printf(str, "%sSYSTEM", comma);
  817. }
  818. }
  819. smart_str_appends(str, "> ]\n");
  820. smart_str_append_printf(str, " %s Current = '%s'\n", indent, ini_entry->value ? ZSTR_VAL(ini_entry->value) : "");
  821. if (ini_entry->modified) {
  822. smart_str_append_printf(str, " %s Default = '%s'\n", indent, ini_entry->orig_value ? ZSTR_VAL(ini_entry->orig_value) : "");
  823. }
  824. smart_str_append_printf(str, " %s}\n", indent);
  825. }
  826. }
  827. /* }}} */
  828. static void _extension_class_string(zend_class_entry *ce, zend_string *key, smart_str *str, char *indent, zend_module_entry *module, int *num_classes) /* {{{ */
  829. {
  830. if (ce->type == ZEND_INTERNAL_CLASS && ce->info.internal.module && !strcasecmp(ce->info.internal.module->name, module->name)) {
  831. /* dump class if it is not an alias */
  832. if (zend_string_equals_ci(ce->name, key)) {
  833. smart_str_append_printf(str, "\n");
  834. _class_string(str, ce, NULL, indent);
  835. (*num_classes)++;
  836. }
  837. }
  838. }
  839. /* }}} */
  840. static void _extension_string(smart_str *str, zend_module_entry *module, char *indent) /* {{{ */
  841. {
  842. smart_str_append_printf(str, "%sExtension [ ", indent);
  843. if (module->type == MODULE_PERSISTENT) {
  844. smart_str_appends(str, "<persistent>");
  845. }
  846. if (module->type == MODULE_TEMPORARY) {
  847. smart_str_appends(str, "<temporary>" );
  848. }
  849. smart_str_append_printf(str, " extension #%d %s version %s ] {\n",
  850. module->module_number, module->name,
  851. (module->version == NO_VERSION_YET) ? "<no_version>" : module->version);
  852. if (module->deps) {
  853. const zend_module_dep* dep = module->deps;
  854. smart_str_appends(str, "\n - Dependencies {\n");
  855. while(dep->name) {
  856. smart_str_append_printf(str, "%s Dependency [ %s (", indent, dep->name);
  857. switch(dep->type) {
  858. case MODULE_DEP_REQUIRED:
  859. smart_str_appends(str, "Required");
  860. break;
  861. case MODULE_DEP_CONFLICTS:
  862. smart_str_appends(str, "Conflicts");
  863. break;
  864. case MODULE_DEP_OPTIONAL:
  865. smart_str_appends(str, "Optional");
  866. break;
  867. default:
  868. smart_str_appends(str, "Error"); /* shouldn't happen */
  869. break;
  870. }
  871. if (dep->rel) {
  872. smart_str_append_printf(str, " %s", dep->rel);
  873. }
  874. if (dep->version) {
  875. smart_str_append_printf(str, " %s", dep->version);
  876. }
  877. smart_str_appends(str, ") ]\n");
  878. dep++;
  879. }
  880. smart_str_append_printf(str, "%s }\n", indent);
  881. }
  882. {
  883. smart_str str_ini = {0};
  884. zend_ini_entry *ini_entry;
  885. ZEND_HASH_FOREACH_PTR(EG(ini_directives), ini_entry) {
  886. _extension_ini_string(ini_entry, &str_ini, indent, module->module_number);
  887. } ZEND_HASH_FOREACH_END();
  888. if (smart_str_get_len(&str_ini) > 0) {
  889. smart_str_append_printf(str, "\n - INI {\n");
  890. smart_str_append_smart_str(str, &str_ini);
  891. smart_str_append_printf(str, "%s }\n", indent);
  892. }
  893. smart_str_free(&str_ini);
  894. }
  895. {
  896. smart_str str_constants = {0};
  897. zend_constant *constant;
  898. int num_constants = 0;
  899. ZEND_HASH_FOREACH_PTR(EG(zend_constants), constant) {
  900. if (ZEND_CONSTANT_MODULE_NUMBER(constant) == module->module_number) {
  901. _const_string(&str_constants, ZSTR_VAL(constant->name), &constant->value, indent);
  902. num_constants++;
  903. }
  904. } ZEND_HASH_FOREACH_END();
  905. if (num_constants) {
  906. smart_str_append_printf(str, "\n - Constants [%d] {\n", num_constants);
  907. smart_str_append_smart_str(str, &str_constants);
  908. smart_str_append_printf(str, "%s }\n", indent);
  909. }
  910. smart_str_free(&str_constants);
  911. }
  912. {
  913. zend_function *fptr;
  914. int first = 1;
  915. ZEND_HASH_FOREACH_PTR(CG(function_table), fptr) {
  916. if (fptr->common.type==ZEND_INTERNAL_FUNCTION
  917. && fptr->internal_function.module == module) {
  918. if (first) {
  919. smart_str_append_printf(str, "\n - Functions {\n");
  920. first = 0;
  921. }
  922. _function_string(str, fptr, NULL, " ");
  923. }
  924. } ZEND_HASH_FOREACH_END();
  925. if (!first) {
  926. smart_str_append_printf(str, "%s }\n", indent);
  927. }
  928. }
  929. {
  930. zend_string *sub_indent = strpprintf(0, "%s ", indent);
  931. smart_str str_classes = {0};
  932. zend_string *key;
  933. zend_class_entry *ce;
  934. int num_classes = 0;
  935. ZEND_HASH_FOREACH_STR_KEY_PTR(EG(class_table), key, ce) {
  936. _extension_class_string(ce, key, &str_classes, ZSTR_VAL(sub_indent), module, &num_classes);
  937. } ZEND_HASH_FOREACH_END();
  938. if (num_classes) {
  939. smart_str_append_printf(str, "\n - Classes [%d] {", num_classes);
  940. smart_str_append_smart_str(str, &str_classes);
  941. smart_str_append_printf(str, "%s }\n", indent);
  942. }
  943. smart_str_free(&str_classes);
  944. zend_string_release_ex(sub_indent, 0);
  945. }
  946. smart_str_append_printf(str, "%s}\n", indent);
  947. }
  948. /* }}} */
  949. static void _zend_extension_string(smart_str *str, zend_extension *extension, char *indent) /* {{{ */
  950. {
  951. smart_str_append_printf(str, "%sZend Extension [ %s ", indent, extension->name);
  952. if (extension->version) {
  953. smart_str_append_printf(str, "%s ", extension->version);
  954. }
  955. if (extension->copyright) {
  956. smart_str_append_printf(str, "%s ", extension->copyright);
  957. }
  958. if (extension->author) {
  959. smart_str_append_printf(str, "by %s ", extension->author);
  960. }
  961. if (extension->URL) {
  962. smart_str_append_printf(str, "<%s> ", extension->URL);
  963. }
  964. smart_str_appends(str, "]\n");
  965. }
  966. /* }}} */
  967. /* {{{ _function_check_flag */
  968. static void _function_check_flag(INTERNAL_FUNCTION_PARAMETERS, int mask)
  969. {
  970. reflection_object *intern;
  971. zend_function *mptr;
  972. if (zend_parse_parameters_none() == FAILURE) {
  973. RETURN_THROWS();
  974. }
  975. GET_REFLECTION_OBJECT_PTR(mptr);
  976. RETURN_BOOL(mptr->common.fn_flags & mask);
  977. }
  978. /* }}} */
  979. /* {{{ zend_reflection_class_factory */
  980. PHPAPI void zend_reflection_class_factory(zend_class_entry *ce, zval *object)
  981. {
  982. reflection_object *intern;
  983. reflection_instantiate(reflection_class_ptr, object);
  984. intern = Z_REFLECTION_P(object);
  985. intern->ptr = ce;
  986. intern->ref_type = REF_TYPE_OTHER;
  987. intern->ce = ce;
  988. ZVAL_STR_COPY(reflection_prop_name(object), ce->name);
  989. }
  990. /* }}} */
  991. /* {{{ reflection_extension_factory */
  992. static void reflection_extension_factory(zval *object, const char *name_str)
  993. {
  994. reflection_object *intern;
  995. size_t name_len = strlen(name_str);
  996. zend_string *lcname;
  997. struct _zend_module_entry *module;
  998. lcname = zend_string_alloc(name_len, 0);
  999. zend_str_tolower_copy(ZSTR_VAL(lcname), name_str, name_len);
  1000. module = zend_hash_find_ptr(&module_registry, lcname);
  1001. zend_string_efree(lcname);
  1002. if (!module) {
  1003. return;
  1004. }
  1005. reflection_instantiate(reflection_extension_ptr, object);
  1006. intern = Z_REFLECTION_P(object);
  1007. intern->ptr = module;
  1008. intern->ref_type = REF_TYPE_OTHER;
  1009. intern->ce = NULL;
  1010. ZVAL_STRINGL(reflection_prop_name(object), module->name, name_len);
  1011. }
  1012. /* }}} */
  1013. /* {{{ reflection_parameter_factory */
  1014. static void reflection_parameter_factory(zend_function *fptr, zval *closure_object, struct _zend_arg_info *arg_info, uint32_t offset, zend_bool required, zval *object)
  1015. {
  1016. reflection_object *intern;
  1017. parameter_reference *reference;
  1018. zval *prop_name;
  1019. reflection_instantiate(reflection_parameter_ptr, object);
  1020. intern = Z_REFLECTION_P(object);
  1021. reference = (parameter_reference*) emalloc(sizeof(parameter_reference));
  1022. reference->arg_info = arg_info;
  1023. reference->offset = offset;
  1024. reference->required = required;
  1025. reference->fptr = fptr;
  1026. intern->ptr = reference;
  1027. intern->ref_type = REF_TYPE_PARAMETER;
  1028. intern->ce = fptr->common.scope;
  1029. if (closure_object) {
  1030. Z_ADDREF_P(closure_object);
  1031. ZVAL_OBJ(&intern->obj, Z_OBJ_P(closure_object));
  1032. }
  1033. prop_name = reflection_prop_name(object);
  1034. if (has_internal_arg_info(fptr)) {
  1035. ZVAL_STRING(prop_name, ((zend_internal_arg_info*)arg_info)->name);
  1036. } else {
  1037. ZVAL_STR_COPY(prop_name, arg_info->name);
  1038. }
  1039. }
  1040. /* }}} */
  1041. /* For backwards compatibility reasons, we need to return T|null style unions
  1042. * as a ReflectionNamedType. Here we determine what counts as a union type and
  1043. * what doesn't. */
  1044. static zend_bool is_union_type(zend_type type) {
  1045. if (ZEND_TYPE_HAS_LIST(type)) {
  1046. return 1;
  1047. }
  1048. uint32_t type_mask_without_null = ZEND_TYPE_PURE_MASK_WITHOUT_NULL(type);
  1049. if (ZEND_TYPE_HAS_CLASS(type)) {
  1050. return type_mask_without_null != 0;
  1051. }
  1052. if (type_mask_without_null == MAY_BE_BOOL) {
  1053. return 0;
  1054. }
  1055. /* Check that only one bit is set. */
  1056. return (type_mask_without_null & (type_mask_without_null - 1)) != 0;
  1057. }
  1058. /* {{{ reflection_type_factory */
  1059. static void reflection_type_factory(zend_type type, zval *object, zend_bool legacy_behavior)
  1060. {
  1061. reflection_object *intern;
  1062. type_reference *reference;
  1063. zend_bool is_union = is_union_type(type);
  1064. reflection_instantiate(
  1065. is_union ? reflection_union_type_ptr : reflection_named_type_ptr, object);
  1066. intern = Z_REFLECTION_P(object);
  1067. reference = (type_reference*) emalloc(sizeof(type_reference));
  1068. reference->type = type;
  1069. reference->legacy_behavior = legacy_behavior && !is_union;
  1070. intern->ptr = reference;
  1071. intern->ref_type = REF_TYPE_TYPE;
  1072. /* Property types may be resolved during the lifetime of the ReflectionType.
  1073. * If we reference a string, make sure it doesn't get released. However, only
  1074. * do this for the top-level type, as resolutions inside type lists will be
  1075. * fully visible to us (we'd have to do a fully copy of the type if we wanted
  1076. * to prevent that). */
  1077. if (ZEND_TYPE_HAS_NAME(type)) {
  1078. zend_string_addref(ZEND_TYPE_NAME(type));
  1079. }
  1080. }
  1081. /* }}} */
  1082. /* {{{ reflection_function_factory */
  1083. static void reflection_function_factory(zend_function *function, zval *closure_object, zval *object)
  1084. {
  1085. reflection_object *intern;
  1086. reflection_instantiate(reflection_function_ptr, object);
  1087. intern = Z_REFLECTION_P(object);
  1088. intern->ptr = function;
  1089. intern->ref_type = REF_TYPE_FUNCTION;
  1090. intern->ce = NULL;
  1091. if (closure_object) {
  1092. Z_ADDREF_P(closure_object);
  1093. ZVAL_OBJ(&intern->obj, Z_OBJ_P(closure_object));
  1094. }
  1095. ZVAL_STR_COPY(reflection_prop_name(object), function->common.function_name);
  1096. }
  1097. /* }}} */
  1098. /* {{{ reflection_method_factory */
  1099. static void reflection_method_factory(zend_class_entry *ce, zend_function *method, zval *closure_object, zval *object)
  1100. {
  1101. reflection_object *intern;
  1102. reflection_instantiate(reflection_method_ptr, object);
  1103. intern = Z_REFLECTION_P(object);
  1104. intern->ptr = method;
  1105. intern->ref_type = REF_TYPE_FUNCTION;
  1106. intern->ce = ce;
  1107. if (closure_object) {
  1108. Z_ADDREF_P(closure_object);
  1109. ZVAL_OBJ(&intern->obj, Z_OBJ_P(closure_object));
  1110. }
  1111. ZVAL_STR_COPY(reflection_prop_name(object), method->common.function_name);
  1112. ZVAL_STR_COPY(reflection_prop_class(object), method->common.scope->name);
  1113. }
  1114. /* }}} */
  1115. /* {{{ reflection_property_factory */
  1116. static void reflection_property_factory(zend_class_entry *ce, zend_string *name, zend_property_info *prop, zval *object)
  1117. {
  1118. reflection_object *intern;
  1119. property_reference *reference;
  1120. reflection_instantiate(reflection_property_ptr, object);
  1121. intern = Z_REFLECTION_P(object);
  1122. reference = (property_reference*) emalloc(sizeof(property_reference));
  1123. reference->prop = prop;
  1124. reference->unmangled_name = zend_string_copy(name);
  1125. intern->ptr = reference;
  1126. intern->ref_type = REF_TYPE_PROPERTY;
  1127. intern->ce = ce;
  1128. intern->ignore_visibility = 0;
  1129. ZVAL_STR_COPY(reflection_prop_name(object), name);
  1130. ZVAL_STR_COPY(reflection_prop_class(object), prop ? prop->ce->name : ce->name);
  1131. }
  1132. /* }}} */
  1133. static void reflection_property_factory_str(zend_class_entry *ce, const char *name_str, size_t name_len, zend_property_info *prop, zval *object)
  1134. {
  1135. zend_string *name = zend_string_init(name_str, name_len, 0);
  1136. reflection_property_factory(ce, name, prop, object);
  1137. zend_string_release(name);
  1138. }
  1139. /* {{{ reflection_class_constant_factory */
  1140. static void reflection_class_constant_factory(zend_string *name_str, zend_class_constant *constant, zval *object)
  1141. {
  1142. reflection_object *intern;
  1143. reflection_instantiate(reflection_class_constant_ptr, object);
  1144. intern = Z_REFLECTION_P(object);
  1145. intern->ptr = constant;
  1146. intern->ref_type = REF_TYPE_CLASS_CONSTANT;
  1147. intern->ce = constant->ce;
  1148. intern->ignore_visibility = 0;
  1149. ZVAL_STR_COPY(reflection_prop_name(object), name_str);
  1150. ZVAL_STR_COPY(reflection_prop_class(object), constant->ce->name);
  1151. }
  1152. /* }}} */
  1153. static int get_parameter_default(zval *result, parameter_reference *param) {
  1154. if (param->fptr->type == ZEND_INTERNAL_FUNCTION) {
  1155. return zend_get_default_from_internal_arg_info(
  1156. result, (zend_internal_arg_info *) param->arg_info);
  1157. } else {
  1158. zval *default_value = get_default_from_recv((zend_op_array *) param->fptr, param->offset);
  1159. if (!default_value) {
  1160. return FAILURE;
  1161. }
  1162. ZVAL_COPY(result, default_value);
  1163. return SUCCESS;
  1164. }
  1165. }
  1166. /* {{{ Preventing __clone from being called */
  1167. ZEND_METHOD(ReflectionClass, __clone)
  1168. {
  1169. /* Should never be executable */
  1170. _DO_THROW("Cannot clone object using __clone()");
  1171. }
  1172. /* }}} */
  1173. /* {{{ proto public static array Reflection::getModifierNames(int modifiers)
  1174. Returns an array of modifier names */
  1175. ZEND_METHOD(Reflection, getModifierNames)
  1176. {
  1177. zend_long modifiers;
  1178. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &modifiers) == FAILURE) {
  1179. RETURN_THROWS();
  1180. }
  1181. array_init(return_value);
  1182. if (modifiers & (ZEND_ACC_ABSTRACT | ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) {
  1183. add_next_index_stringl(return_value, "abstract", sizeof("abstract")-1);
  1184. }
  1185. if (modifiers & ZEND_ACC_FINAL) {
  1186. add_next_index_stringl(return_value, "final", sizeof("final")-1);
  1187. }
  1188. /* These are mutually exclusive */
  1189. switch (modifiers & ZEND_ACC_PPP_MASK) {
  1190. case ZEND_ACC_PUBLIC:
  1191. add_next_index_stringl(return_value, "public", sizeof("public")-1);
  1192. break;
  1193. case ZEND_ACC_PRIVATE:
  1194. add_next_index_stringl(return_value, "private", sizeof("private")-1);
  1195. break;
  1196. case ZEND_ACC_PROTECTED:
  1197. add_next_index_stringl(return_value, "protected", sizeof("protected")-1);
  1198. break;
  1199. }
  1200. if (modifiers & ZEND_ACC_STATIC) {
  1201. add_next_index_stringl(return_value, "static", sizeof("static")-1);
  1202. }
  1203. }
  1204. /* }}} */
  1205. /* {{{ proto public void ReflectionFunction::__construct(string name)
  1206. Constructor. Throws an Exception in case the given function does not exist */
  1207. ZEND_METHOD(ReflectionFunction, __construct)
  1208. {
  1209. zval *object;
  1210. zval *closure = NULL;
  1211. reflection_object *intern;
  1212. zend_function *fptr;
  1213. zend_string *fname, *lcname;
  1214. object = ZEND_THIS;
  1215. intern = Z_REFLECTION_P(object);
  1216. if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "O", &closure, zend_ce_closure) == SUCCESS) {
  1217. fptr = (zend_function*)zend_get_closure_method_def(closure);
  1218. Z_ADDREF_P(closure);
  1219. } else {
  1220. ALLOCA_FLAG(use_heap)
  1221. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &fname) == FAILURE) {
  1222. RETURN_THROWS();
  1223. }
  1224. if (UNEXPECTED(ZSTR_VAL(fname)[0] == '\\')) {
  1225. /* Ignore leading "\" */
  1226. ZSTR_ALLOCA_ALLOC(lcname, ZSTR_LEN(fname) - 1, use_heap);
  1227. zend_str_tolower_copy(ZSTR_VAL(lcname), ZSTR_VAL(fname) + 1, ZSTR_LEN(fname) - 1);
  1228. fptr = zend_fetch_function(lcname);
  1229. ZSTR_ALLOCA_FREE(lcname, use_heap);
  1230. } else {
  1231. lcname = zend_string_tolower(fname);
  1232. fptr = zend_fetch_function(lcname);
  1233. zend_string_release(lcname);
  1234. }
  1235. if (fptr == NULL) {
  1236. zend_throw_exception_ex(reflection_exception_ptr, 0,
  1237. "Function %s() does not exist", ZSTR_VAL(fname));
  1238. RETURN_THROWS();
  1239. }
  1240. }
  1241. ZVAL_STR_COPY(reflection_prop_name(object), fptr->common.function_name);
  1242. intern->ptr = fptr;
  1243. intern->ref_type = REF_TYPE_FUNCTION;
  1244. if (closure) {
  1245. ZVAL_OBJ(&intern->obj, Z_OBJ_P(closure));
  1246. } else {
  1247. ZVAL_UNDEF(&intern->obj);
  1248. }
  1249. intern->ce = NULL;
  1250. }
  1251. /* }}} */
  1252. /* {{{ proto public string ReflectionFunction::__toString()
  1253. Returns a string representation */
  1254. ZEND_METHOD(ReflectionFunction, __toString)
  1255. {
  1256. reflection_object *intern;
  1257. zend_function *fptr;
  1258. smart_str str = {0};
  1259. if (zend_parse_parameters_none() == FAILURE) {
  1260. RETURN_THROWS();
  1261. }
  1262. GET_REFLECTION_OBJECT_PTR(fptr);
  1263. _function_string(&str, fptr, intern->ce, "");
  1264. RETURN_STR(smart_str_extract(&str));
  1265. }
  1266. /* }}} */
  1267. /* {{{ proto public string ReflectionFunction::getName()
  1268. Returns this function's name */
  1269. ZEND_METHOD(ReflectionFunctionAbstract, getName)
  1270. {
  1271. reflection_object *intern;
  1272. zend_function *fptr;
  1273. if (zend_parse_parameters_none() == FAILURE) {
  1274. RETURN_THROWS();
  1275. }
  1276. GET_REFLECTION_OBJECT_PTR(fptr);
  1277. RETURN_STR_COPY(fptr->common.function_name);
  1278. }
  1279. /* }}} */
  1280. /* {{{ proto public bool ReflectionFunction::isClosure()
  1281. Returns whether this is a closure */
  1282. ZEND_METHOD(ReflectionFunctionAbstract, isClosure)
  1283. {
  1284. reflection_object *intern;
  1285. zend_function *fptr;
  1286. if (zend_parse_parameters_none() == FAILURE) {
  1287. RETURN_THROWS();
  1288. }
  1289. GET_REFLECTION_OBJECT_PTR(fptr);
  1290. RETURN_BOOL(fptr->common.fn_flags & ZEND_ACC_CLOSURE);
  1291. }
  1292. /* }}} */
  1293. /* {{{ proto public bool ReflectionFunction::getClosureThis()
  1294. Returns this pointer bound to closure */
  1295. ZEND_METHOD(ReflectionFunctionAbstract, getClosureThis)
  1296. {
  1297. reflection_object *intern;
  1298. zval* closure_this;
  1299. if (zend_parse_parameters_none() == FAILURE) {
  1300. RETURN_THROWS();
  1301. }
  1302. GET_REFLECTION_OBJECT();
  1303. if (!Z_ISUNDEF(intern->obj)) {
  1304. closure_this = zend_get_closure_this_ptr(&intern->obj);
  1305. if (!Z_ISUNDEF_P(closure_this)) {
  1306. Z_ADDREF_P(closure_this);
  1307. ZVAL_OBJ(return_value, Z_OBJ_P(closure_this));
  1308. }
  1309. }
  1310. }
  1311. /* }}} */
  1312. /* {{{ proto public ReflectionClass ReflectionFunction::getClosureScopeClass()
  1313. Returns the scope associated to the closure */
  1314. ZEND_METHOD(ReflectionFunctionAbstract, getClosureScopeClass)
  1315. {
  1316. reflection_object *intern;
  1317. const zend_function *closure_func;
  1318. if (zend_parse_parameters_none() == FAILURE) {
  1319. RETURN_THROWS();
  1320. }
  1321. GET_REFLECTION_OBJECT();
  1322. if (!Z_ISUNDEF(intern->obj)) {
  1323. closure_func = zend_get_closure_method_def(&intern->obj);
  1324. if (closure_func && closure_func->common.scope) {
  1325. zend_reflection_class_factory(closure_func->common.scope, return_value);
  1326. }
  1327. }
  1328. }
  1329. /* }}} */
  1330. /* {{{ proto public mixed ReflectionFunction::getClosure()
  1331. Returns a dynamically created closure for the function */
  1332. ZEND_METHOD(ReflectionFunction, getClosure)
  1333. {
  1334. reflection_object *intern;
  1335. zend_function *fptr;
  1336. if (zend_parse_parameters_none() == FAILURE) {
  1337. RETURN_THROWS();
  1338. }
  1339. GET_REFLECTION_OBJECT_PTR(fptr);
  1340. if (!Z_ISUNDEF(intern->obj)) {
  1341. /* Closures are immutable objects */
  1342. Z_ADDREF(intern->obj);
  1343. ZVAL_OBJ(return_value, Z_OBJ(intern->obj));
  1344. } else {
  1345. zend_create_fake_closure(return_value, fptr, NULL, NULL, NULL);
  1346. }
  1347. }
  1348. /* }}} */
  1349. /* {{{ proto public bool ReflectionFunction::isInternal()
  1350. Returns whether this is an internal function */
  1351. ZEND_METHOD(ReflectionFunctionAbstract, isInternal)
  1352. {
  1353. reflection_object *intern;
  1354. zend_function *fptr;
  1355. if (zend_parse_parameters_none() == FAILURE) {
  1356. RETURN_THROWS();
  1357. }
  1358. GET_REFLECTION_OBJECT_PTR(fptr);
  1359. RETURN_BOOL(fptr->type == ZEND_INTERNAL_FUNCTION);
  1360. }
  1361. /* }}} */
  1362. /* {{{ proto public bool ReflectionFunction::isUserDefined()
  1363. Returns whether this is a user-defined function */
  1364. ZEND_METHOD(ReflectionFunctionAbstract, isUserDefined)
  1365. {
  1366. reflection_object *intern;
  1367. zend_function *fptr;
  1368. if (zend_parse_parameters_none() == FAILURE) {
  1369. RETURN_THROWS();
  1370. }
  1371. GET_REFLECTION_OBJECT_PTR(fptr);
  1372. RETURN_BOOL(fptr->type == ZEND_USER_FUNCTION);
  1373. }
  1374. /* }}} */
  1375. /* {{{ proto public bool ReflectionFunction::isDisabled()
  1376. Returns whether this function has been disabled or not */
  1377. ZEND_METHOD(ReflectionFunction, isDisabled)
  1378. {
  1379. if (zend_parse_parameters_none() == FAILURE) {
  1380. RETURN_THROWS();
  1381. }
  1382. /* A disabled function cannot be queried using Reflection. */
  1383. RETURN_FALSE;
  1384. }
  1385. /* }}} */
  1386. /* {{{ proto public string ReflectionFunction::getFileName()
  1387. Returns the filename of the file this function was declared in */
  1388. ZEND_METHOD(ReflectionFunctionAbstract, getFileName)
  1389. {
  1390. reflection_object *intern;
  1391. zend_function *fptr;
  1392. if (zend_parse_parameters_none() == FAILURE) {
  1393. RETURN_THROWS();
  1394. }
  1395. GET_REFLECTION_OBJECT_PTR(fptr);
  1396. if (fptr->type == ZEND_USER_FUNCTION) {
  1397. RETURN_STR_COPY(fptr->op_array.filename);
  1398. }
  1399. RETURN_FALSE;
  1400. }
  1401. /* }}} */
  1402. /* {{{ proto public int ReflectionFunction::getStartLine()
  1403. Returns the line this function's declaration starts at */
  1404. ZEND_METHOD(ReflectionFunctionAbstract, getStartLine)
  1405. {
  1406. reflection_object *intern;
  1407. zend_function *fptr;
  1408. if (zend_parse_parameters_none() == FAILURE) {
  1409. RETURN_THROWS();
  1410. }
  1411. GET_REFLECTION_OBJECT_PTR(fptr);
  1412. if (fptr->type == ZEND_USER_FUNCTION) {
  1413. RETURN_LONG(fptr->op_array.line_start);
  1414. }
  1415. RETURN_FALSE;
  1416. }
  1417. /* }}} */
  1418. /* {{{ proto public int ReflectionFunction::getEndLine()
  1419. Returns the line this function's declaration ends at */
  1420. ZEND_METHOD(ReflectionFunctionAbstract, getEndLine)
  1421. {
  1422. reflection_object *intern;
  1423. zend_function *fptr;
  1424. if (zend_parse_parameters_none() == FAILURE)

Large files files are truncated, but you can click here to view the full file