-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisitor.c
159 lines (139 loc) · 4.76 KB
/
visitor.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
* Copyright (c) anno Domini nostri Jesu Christi MMXVI-MMXXIV John Boehr & contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include <stdbool.h>
#include "Zend/zend_API.h"
#include "Zend/zend_exceptions.h"
#include "main/php.h"
#include "main/php_streams.h"
#include "php_vyrtue.h"
#include "visitor.h"
static const struct vyrtue_visitor_array EMPTY_VISITOR_ARRAY = {
.size = 0,
.length = 0,
};
VYRTUE_PUBLIC
void vyrtue_register_attribute_visitor(const char *visitor_name, zend_string *attribute_name, vyrtue_ast_callback enter, vyrtue_ast_callback leave)
{
struct vyrtue_visitor visitor = {
.name = visitor_name,
.enter = enter,
.leave = leave,
};
struct vyrtue_visitor_array *arr = zend_hash_find_ptr(&VYRTUE_G(attribute_visitors), attribute_name);
if (!arr) {
size_t size = sizeof(*arr) + sizeof(arr->data[0]);
arr = pemalloc(size, 1);
memset(arr, 0, size);
arr->size = size;
} else {
size_t size = sizeof(*arr) + sizeof(arr->data[0]) * arr->length + 1;
arr = perealloc(arr, size, 1);
arr->size = size;
}
arr->data[arr->length] = visitor;
arr->length++;
zend_hash_update_ptr(&VYRTUE_G(attribute_visitors), attribute_name, arr);
}
VYRTUE_PUBLIC
void vyrtue_register_function_visitor(const char *visitor_name, zend_string *function_name, vyrtue_ast_callback enter, vyrtue_ast_callback leave)
{
struct vyrtue_visitor visitor = {
.name = visitor_name,
.enter = enter,
.leave = leave,
};
struct vyrtue_visitor_array *arr = zend_hash_find_ptr(&VYRTUE_G(function_visitors), function_name);
if (!arr) {
size_t size = sizeof(*arr) + sizeof(arr->data[0]);
arr = pemalloc(size, 1);
memset(arr, 0, size);
arr->size = size;
} else {
size_t size = sizeof(*arr) + sizeof(arr->data[0]) * arr->length + 1;
arr = perealloc(arr, size, 1);
arr->size = size;
}
arr->data[arr->length] = visitor;
arr->length++;
zend_hash_update_ptr(&VYRTUE_G(function_visitors), function_name, arr);
}
VYRTUE_PUBLIC
void vyrtue_register_kind_visitor(const char *visitor_name, enum _zend_ast_kind kind, vyrtue_ast_callback enter, vyrtue_ast_callback leave)
{
struct vyrtue_visitor visitor = {
.name = visitor_name,
.enter = enter,
.leave = leave,
};
struct vyrtue_visitor_array *arr = zend_hash_index_find_ptr(&VYRTUE_G(kind_visitors), (zend_ulong) kind);
if (!arr) {
size_t size = sizeof(*arr) + sizeof(arr->data[0]);
arr = pemalloc(size, 1);
memset(arr, 0, size);
arr->size = size;
} else {
size_t size = sizeof(*arr) + sizeof(arr->data[0]) * arr->length + 1;
arr = perealloc(arr, size, 1);
arr->size = size;
}
arr->data[arr->length] = visitor;
arr->length++;
zend_hash_index_update_ptr(&VYRTUE_G(kind_visitors), (zend_ulong) kind, arr);
}
VYRTUE_PUBLIC
VYRTUE_ATTR_NONNULL_ALL
VYRTUE_ATTR_RETURNS_NONNULL
VYRTUE_ATTR_WARN_UNUSED_RESULT
const struct vyrtue_visitor_array *vyrtue_get_attribute_visitors(zend_string *function_name)
{
const struct vyrtue_visitor_array *arr = zend_hash_find_ptr(&VYRTUE_G(attribute_visitors), function_name);
if (arr) {
return arr;
} else {
return &EMPTY_VISITOR_ARRAY;
}
}
VYRTUE_PUBLIC
VYRTUE_ATTR_NONNULL_ALL
VYRTUE_ATTR_RETURNS_NONNULL
VYRTUE_ATTR_WARN_UNUSED_RESULT
const struct vyrtue_visitor_array *vyrtue_get_function_visitors(zend_string *function_name)
{
const struct vyrtue_visitor_array *arr = zend_hash_find_ptr(&VYRTUE_G(function_visitors), function_name);
if (arr) {
return arr;
} else {
return &EMPTY_VISITOR_ARRAY;
}
}
VYRTUE_PUBLIC
VYRTUE_ATTR_NONNULL_ALL
VYRTUE_ATTR_RETURNS_NONNULL
VYRTUE_ATTR_WARN_UNUSED_RESULT
const struct vyrtue_visitor_array *vyrtue_get_kind_visitors(enum _zend_ast_kind kind)
{
const struct vyrtue_visitor_array *arr = zend_hash_index_find_ptr(&VYRTUE_G(kind_visitors), (zend_ulong) kind);
if (arr) {
return arr;
} else {
return &EMPTY_VISITOR_ARRAY;
}
}