zend_attributes.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) Zend Technologies Ltd. (http://www.zend.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.00 of the Zend license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.zend.com/license/2_00.txt. |
  11. | If you did not receive a copy of the Zend license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@zend.com so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Benjamin Eberlei <kontakt@beberlei.de> |
  16. | Martin Schröder <m.schroeder2007@gmail.com> |
  17. +----------------------------------------------------------------------+
  18. */
  19. #include "zend.h"
  20. #include "zend_API.h"
  21. #include "zend_attributes.h"
  22. #include "zend_attributes_arginfo.h"
  23. #include "zend_smart_str.h"
  24. ZEND_API zend_class_entry *zend_ce_attribute;
  25. ZEND_API zend_class_entry *zend_ce_return_type_will_change_attribute;
  26. static HashTable internal_attributes;
  27. void validate_attribute(zend_attribute *attr, uint32_t target, zend_class_entry *scope)
  28. {
  29. // TODO: More proper signature validation: Too many args, incorrect arg names.
  30. if (attr->argc > 0) {
  31. zval flags;
  32. /* As this is run in the middle of compilation, fetch the attribute value without
  33. * specifying a scope. The class is not fully linked yet, and we may seen an
  34. * inconsistent state. */
  35. if (FAILURE == zend_get_attribute_value(&flags, attr, 0, NULL)) {
  36. return;
  37. }
  38. if (Z_TYPE(flags) != IS_LONG) {
  39. zend_error_noreturn(E_ERROR,
  40. "Attribute::__construct(): Argument #1 ($flags) must be of type int, %s given",
  41. zend_zval_type_name(&flags)
  42. );
  43. }
  44. if (Z_LVAL(flags) & ~ZEND_ATTRIBUTE_FLAGS) {
  45. zend_error_noreturn(E_ERROR, "Invalid attribute flags specified");
  46. }
  47. zval_ptr_dtor(&flags);
  48. }
  49. }
  50. ZEND_METHOD(Attribute, __construct)
  51. {
  52. zend_long flags = ZEND_ATTRIBUTE_TARGET_ALL;
  53. ZEND_PARSE_PARAMETERS_START(0, 1)
  54. Z_PARAM_OPTIONAL
  55. Z_PARAM_LONG(flags)
  56. ZEND_PARSE_PARAMETERS_END();
  57. ZVAL_LONG(OBJ_PROP_NUM(Z_OBJ_P(ZEND_THIS), 0), flags);
  58. }
  59. ZEND_METHOD(ReturnTypeWillChange, __construct)
  60. {
  61. ZEND_PARSE_PARAMETERS_NONE();
  62. }
  63. static zend_attribute *get_attribute(HashTable *attributes, zend_string *lcname, uint32_t offset)
  64. {
  65. if (attributes) {
  66. zend_attribute *attr;
  67. ZEND_HASH_FOREACH_PTR(attributes, attr) {
  68. if (attr->offset == offset && zend_string_equals(attr->lcname, lcname)) {
  69. return attr;
  70. }
  71. } ZEND_HASH_FOREACH_END();
  72. }
  73. return NULL;
  74. }
  75. static zend_attribute *get_attribute_str(HashTable *attributes, const char *str, size_t len, uint32_t offset)
  76. {
  77. if (attributes) {
  78. zend_attribute *attr;
  79. ZEND_HASH_FOREACH_PTR(attributes, attr) {
  80. if (attr->offset == offset && ZSTR_LEN(attr->lcname) == len) {
  81. if (0 == memcmp(ZSTR_VAL(attr->lcname), str, len)) {
  82. return attr;
  83. }
  84. }
  85. } ZEND_HASH_FOREACH_END();
  86. }
  87. return NULL;
  88. }
  89. ZEND_API zend_attribute *zend_get_attribute(HashTable *attributes, zend_string *lcname)
  90. {
  91. return get_attribute(attributes, lcname, 0);
  92. }
  93. ZEND_API zend_attribute *zend_get_attribute_str(HashTable *attributes, const char *str, size_t len)
  94. {
  95. return get_attribute_str(attributes, str, len, 0);
  96. }
  97. ZEND_API zend_attribute *zend_get_parameter_attribute(HashTable *attributes, zend_string *lcname, uint32_t offset)
  98. {
  99. return get_attribute(attributes, lcname, offset + 1);
  100. }
  101. ZEND_API zend_attribute *zend_get_parameter_attribute_str(HashTable *attributes, const char *str, size_t len, uint32_t offset)
  102. {
  103. return get_attribute_str(attributes, str, len, offset + 1);
  104. }
  105. ZEND_API zend_result zend_get_attribute_value(zval *ret, zend_attribute *attr, uint32_t i, zend_class_entry *scope)
  106. {
  107. if (i >= attr->argc) {
  108. return FAILURE;
  109. }
  110. ZVAL_COPY_OR_DUP(ret, &attr->args[i].value);
  111. if (Z_TYPE_P(ret) == IS_CONSTANT_AST) {
  112. if (SUCCESS != zval_update_constant_ex(ret, scope)) {
  113. zval_ptr_dtor(ret);
  114. return FAILURE;
  115. }
  116. }
  117. return SUCCESS;
  118. }
  119. static const char *target_names[] = {
  120. "class",
  121. "function",
  122. "method",
  123. "property",
  124. "class constant",
  125. "parameter"
  126. };
  127. ZEND_API zend_string *zend_get_attribute_target_names(uint32_t flags)
  128. {
  129. smart_str str = { 0 };
  130. for (uint32_t i = 0; i < (sizeof(target_names) / sizeof(char *)); i++) {
  131. if (flags & (1 << i)) {
  132. if (smart_str_get_len(&str)) {
  133. smart_str_appends(&str, ", ");
  134. }
  135. smart_str_appends(&str, target_names[i]);
  136. }
  137. }
  138. return smart_str_extract(&str);
  139. }
  140. ZEND_API bool zend_is_attribute_repeated(HashTable *attributes, zend_attribute *attr)
  141. {
  142. zend_attribute *other;
  143. ZEND_HASH_FOREACH_PTR(attributes, other) {
  144. if (other != attr && other->offset == attr->offset) {
  145. if (zend_string_equals(other->lcname, attr->lcname)) {
  146. return 1;
  147. }
  148. }
  149. } ZEND_HASH_FOREACH_END();
  150. return 0;
  151. }
  152. static void attr_free(zval *v)
  153. {
  154. zend_attribute *attr = Z_PTR_P(v);
  155. bool persistent = attr->flags & ZEND_ATTRIBUTE_PERSISTENT;
  156. zend_string_release(attr->name);
  157. zend_string_release(attr->lcname);
  158. for (uint32_t i = 0; i < attr->argc; i++) {
  159. if (attr->args[i].name) {
  160. zend_string_release(attr->args[i].name);
  161. }
  162. if (persistent) {
  163. zval_internal_ptr_dtor(&attr->args[i].value);
  164. } else {
  165. zval_ptr_dtor(&attr->args[i].value);
  166. }
  167. }
  168. pefree(attr, persistent);
  169. }
  170. ZEND_API zend_attribute *zend_add_attribute(HashTable **attributes, zend_string *name, uint32_t argc, uint32_t flags, uint32_t offset, uint32_t lineno)
  171. {
  172. bool persistent = flags & ZEND_ATTRIBUTE_PERSISTENT;
  173. if (*attributes == NULL) {
  174. *attributes = pemalloc(sizeof(HashTable), persistent);
  175. zend_hash_init(*attributes, 8, NULL, attr_free, persistent);
  176. }
  177. zend_attribute *attr = pemalloc(ZEND_ATTRIBUTE_SIZE(argc), persistent);
  178. if (persistent == ((GC_FLAGS(name) & IS_STR_PERSISTENT) != 0)) {
  179. attr->name = zend_string_copy(name);
  180. } else {
  181. attr->name = zend_string_dup(name, persistent);
  182. }
  183. attr->lcname = zend_string_tolower_ex(attr->name, persistent);
  184. attr->flags = flags;
  185. attr->lineno = lineno;
  186. attr->offset = offset;
  187. attr->argc = argc;
  188. /* Initialize arguments to avoid partial initialization in case of fatal errors. */
  189. for (uint32_t i = 0; i < argc; i++) {
  190. attr->args[i].name = NULL;
  191. ZVAL_UNDEF(&attr->args[i].value);
  192. }
  193. zend_hash_next_index_insert_ptr(*attributes, attr);
  194. return attr;
  195. }
  196. static void free_internal_attribute(zval *v)
  197. {
  198. pefree(Z_PTR_P(v), 1);
  199. }
  200. ZEND_API zend_internal_attribute *zend_internal_attribute_register(zend_class_entry *ce, uint32_t flags)
  201. {
  202. zend_internal_attribute *internal_attr;
  203. if (ce->type != ZEND_INTERNAL_CLASS) {
  204. zend_error_noreturn(E_ERROR, "Only internal classes can be registered as compiler attribute");
  205. }
  206. internal_attr = pemalloc(sizeof(zend_internal_attribute), 1);
  207. internal_attr->ce = ce;
  208. internal_attr->flags = flags;
  209. internal_attr->validator = NULL;
  210. zend_string *lcname = zend_string_tolower_ex(ce->name, 1);
  211. zend_hash_update_ptr(&internal_attributes, lcname, internal_attr);
  212. zend_attribute *attr = zend_add_class_attribute(ce, zend_ce_attribute->name, 1);
  213. ZVAL_LONG(&attr->args[0].value, flags);
  214. zend_string_release(lcname);
  215. return internal_attr;
  216. }
  217. ZEND_API zend_internal_attribute *zend_internal_attribute_get(zend_string *lcname)
  218. {
  219. return zend_hash_find_ptr(&internal_attributes, lcname);
  220. }
  221. void zend_register_attribute_ce(void)
  222. {
  223. zend_internal_attribute *attr;
  224. zend_hash_init(&internal_attributes, 8, NULL, free_internal_attribute, 1);
  225. zend_ce_attribute = register_class_Attribute();
  226. attr = zend_internal_attribute_register(zend_ce_attribute, ZEND_ATTRIBUTE_TARGET_CLASS);
  227. attr->validator = validate_attribute;
  228. zend_declare_class_constant_long(zend_ce_attribute, ZEND_STRL("TARGET_CLASS"), ZEND_ATTRIBUTE_TARGET_CLASS);
  229. zend_declare_class_constant_long(zend_ce_attribute, ZEND_STRL("TARGET_FUNCTION"), ZEND_ATTRIBUTE_TARGET_FUNCTION);
  230. zend_declare_class_constant_long(zend_ce_attribute, ZEND_STRL("TARGET_METHOD"), ZEND_ATTRIBUTE_TARGET_METHOD);
  231. zend_declare_class_constant_long(zend_ce_attribute, ZEND_STRL("TARGET_PROPERTY"), ZEND_ATTRIBUTE_TARGET_PROPERTY);
  232. zend_declare_class_constant_long(zend_ce_attribute, ZEND_STRL("TARGET_CLASS_CONSTANT"), ZEND_ATTRIBUTE_TARGET_CLASS_CONST);
  233. zend_declare_class_constant_long(zend_ce_attribute, ZEND_STRL("TARGET_PARAMETER"), ZEND_ATTRIBUTE_TARGET_PARAMETER);
  234. zend_declare_class_constant_long(zend_ce_attribute, ZEND_STRL("TARGET_ALL"), ZEND_ATTRIBUTE_TARGET_ALL);
  235. zend_declare_class_constant_long(zend_ce_attribute, ZEND_STRL("IS_REPEATABLE"), ZEND_ATTRIBUTE_IS_REPEATABLE);
  236. zend_ce_return_type_will_change_attribute = register_class_ReturnTypeWillChange();
  237. zend_internal_attribute_register(zend_ce_return_type_will_change_attribute, ZEND_ATTRIBUTE_TARGET_METHOD);
  238. }
  239. void zend_attributes_shutdown(void)
  240. {
  241. zend_hash_destroy(&internal_attributes);
  242. }