xref: /PHP-8.0/Zend/zend_attributes.h (revision 5686c16d)
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 
20 #ifndef ZEND_ATTRIBUTES_H
21 #define ZEND_ATTRIBUTES_H
22 
23 #define ZEND_ATTRIBUTE_TARGET_CLASS			(1<<0)
24 #define ZEND_ATTRIBUTE_TARGET_FUNCTION		(1<<1)
25 #define ZEND_ATTRIBUTE_TARGET_METHOD		(1<<2)
26 #define ZEND_ATTRIBUTE_TARGET_PROPERTY		(1<<3)
27 #define ZEND_ATTRIBUTE_TARGET_CLASS_CONST	(1<<4)
28 #define ZEND_ATTRIBUTE_TARGET_PARAMETER		(1<<5)
29 #define ZEND_ATTRIBUTE_TARGET_ALL			((1<<6) - 1)
30 #define ZEND_ATTRIBUTE_IS_REPEATABLE		(1<<6)
31 #define ZEND_ATTRIBUTE_FLAGS				((1<<7) - 1)
32 
33 /* Flags for zend_attribute.flags */
34 #define ZEND_ATTRIBUTE_PERSISTENT   (1<<0)
35 #define ZEND_ATTRIBUTE_STRICT_TYPES (1<<1)
36 
37 #define ZEND_ATTRIBUTE_SIZE(argc) \
38 	(sizeof(zend_attribute) + sizeof(zend_attribute_arg) * (argc) - sizeof(zend_attribute_arg))
39 
40 BEGIN_EXTERN_C()
41 
42 extern ZEND_API zend_class_entry *zend_ce_attribute;
43 
44 typedef struct {
45 	zend_string *name;
46 	zval value;
47 } zend_attribute_arg;
48 
49 typedef struct _zend_attribute {
50 	zend_string *name;
51 	zend_string *lcname;
52 	uint32_t flags;
53 	uint32_t lineno;
54 	/* Parameter offsets start at 1, everything else uses 0. */
55 	uint32_t offset;
56 	uint32_t argc;
57 	zend_attribute_arg args[1];
58 } zend_attribute;
59 
60 typedef struct _zend_internal_attribute {
61 	zend_class_entry *ce;
62 	uint32_t flags;
63 	void (*validator)(zend_attribute *attr, uint32_t target, zend_class_entry *scope);
64 } zend_internal_attribute;
65 
66 ZEND_API zend_attribute *zend_get_attribute(HashTable *attributes, zend_string *lcname);
67 ZEND_API zend_attribute *zend_get_attribute_str(HashTable *attributes, const char *str, size_t len);
68 
69 ZEND_API zend_attribute *zend_get_parameter_attribute(HashTable *attributes, zend_string *lcname, uint32_t offset);
70 ZEND_API zend_attribute *zend_get_parameter_attribute_str(HashTable *attributes, const char *str, size_t len, uint32_t offset);
71 
72 ZEND_API zend_result zend_get_attribute_value(zval *ret, zend_attribute *attr, uint32_t i, zend_class_entry *scope);
73 
74 ZEND_API zend_string *zend_get_attribute_target_names(uint32_t targets);
75 ZEND_API zend_bool zend_is_attribute_repeated(HashTable *attributes, zend_attribute *attr);
76 
77 ZEND_API zend_internal_attribute *zend_internal_attribute_register(zend_class_entry *ce, uint32_t flags);
78 ZEND_API zend_internal_attribute *zend_internal_attribute_get(zend_string *lcname);
79 
80 ZEND_API zend_attribute *zend_add_attribute(
81 		HashTable **attributes, zend_string *name, uint32_t argc,
82 		uint32_t flags, uint32_t offset, uint32_t lineno);
83 
END_EXTERN_C()84 END_EXTERN_C()
85 
86 static zend_always_inline zend_attribute *zend_add_class_attribute(zend_class_entry *ce, zend_string *name, uint32_t argc)
87 {
88 	uint32_t flags = ce->type != ZEND_USER_CLASS ? ZEND_ATTRIBUTE_PERSISTENT : 0;
89 	return zend_add_attribute(&ce->attributes, name, argc, flags, 0, 0);
90 }
91 
zend_add_function_attribute(zend_function * func,zend_string * name,uint32_t argc)92 static zend_always_inline zend_attribute *zend_add_function_attribute(zend_function *func, zend_string *name, uint32_t argc)
93 {
94 	uint32_t flags = func->common.type != ZEND_USER_FUNCTION ? ZEND_ATTRIBUTE_PERSISTENT : 0;
95 	return zend_add_attribute(&func->common.attributes, name, argc, flags, 0, 0);
96 }
97 
zend_add_parameter_attribute(zend_function * func,uint32_t offset,zend_string * name,uint32_t argc)98 static zend_always_inline zend_attribute *zend_add_parameter_attribute(zend_function *func, uint32_t offset, zend_string *name, uint32_t argc)
99 {
100 	uint32_t flags = func->common.type != ZEND_USER_FUNCTION ? ZEND_ATTRIBUTE_PERSISTENT : 0;
101 	return zend_add_attribute(&func->common.attributes, name, argc, flags, offset + 1, 0);
102 }
103 
zend_add_property_attribute(zend_class_entry * ce,zend_property_info * info,zend_string * name,uint32_t argc)104 static zend_always_inline zend_attribute *zend_add_property_attribute(zend_class_entry *ce, zend_property_info *info, zend_string *name, uint32_t argc)
105 {
106 	uint32_t flags = ce->type != ZEND_USER_CLASS ? ZEND_ATTRIBUTE_PERSISTENT : 0;
107 	return zend_add_attribute(&info->attributes, name, argc, flags, 0, 0);
108 }
109 
zend_add_class_constant_attribute(zend_class_entry * ce,zend_class_constant * c,zend_string * name,uint32_t argc)110 static zend_always_inline zend_attribute *zend_add_class_constant_attribute(zend_class_entry *ce, zend_class_constant *c, zend_string *name, uint32_t argc)
111 {
112 	uint32_t flags = ce->type != ZEND_USER_CLASS ? ZEND_ATTRIBUTE_PERSISTENT : 0;
113 	return zend_add_attribute(&c->attributes, name, argc, flags, 0, 0);
114 }
115 
116 void zend_register_attribute_ce(void);
117 void zend_attributes_shutdown(void);
118 
119 #endif
120