basic support for builtins
[m6w6/ext-psi] / src / builtin.c
1 /*******************************************************************************
2 Copyright (c) 2018, Michael Wallner <mike@php.net>.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
18 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *******************************************************************************/
25
26 #include "php_psi_stdinc.h"
27
28 #include "php_psi.h"
29 #include "builtin.h"
30 #include "parser.h"
31 #include "cpp.h"
32
33 #include <stdarg.h>
34
35 HashTable psi_builtins;
36
37 static bool has_include(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res);
38 static bool has_include_next(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res);
39 static bool builtin_constant_p(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res);
40 static bool COUNTER__(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res);
41
42 static inline struct psi_plist *builtin_sig(token_t typ, ...)
43 {
44 struct psi_plist *sig;
45 size_t n = 0;
46 va_list args;
47
48 if (typ == (token_t) - 1) {
49 return NULL;
50 }
51
52 sig = psi_plist_init((psi_plist_dtor) psi_token_free);
53 va_start(args, typ);
54 while (typ) {
55 char a = 'a' + n++;
56 struct psi_token *arg;
57
58 arg = psi_token_init(typ, &a, 1, 0, 0, zend_empty_string);
59 sig = psi_plist_add(sig, &arg);
60 typ = va_arg(args, token_t);
61 }
62 va_end(args);
63
64 return sig;
65 }
66
67 static void free_builtin(zval *p)
68 {
69 struct psi_builtin *b = Z_PTR_P(p);
70
71 if (b) {
72 zend_string_release(b->name);
73 psi_cpp_macro_decl_free(&b->decl);
74 pefree(b, 1);
75 }
76 }
77
78 PHP_MINIT_FUNCTION(psi_builtin)
79 {
80 #define PSI_BUILTIN(builtin, ...) do { \
81 struct psi_builtin entry; \
82 struct psi_plist *sig = builtin_sig(__VA_ARGS__, 0); \
83 struct psi_cpp_macro_decl *decl = psi_cpp_macro_decl_init(sig, NULL, NULL); \
84 decl->token = psi_token_init(PSI_T_NAME, "__" #builtin, sizeof("__" #builtin)-1, \
85 0, 0, zend_empty_string); \
86 entry.name = zend_string_copy(decl->token->text); \
87 entry.func = &builtin; \
88 entry.decl = decl; \
89 zend_hash_add_mem(&psi_builtins, entry.name, &entry, sizeof(entry)); \
90 } while(0)
91
92 zend_hash_init(&psi_builtins, 0, NULL, free_builtin, 1);
93 PSI_BUILTIN(has_include, PSI_T_CPP_HEADER);
94 PSI_BUILTIN(has_include_next, PSI_T_CPP_HEADER);
95 PSI_BUILTIN(builtin_constant_p, PSI_T_NAME);
96 PSI_BUILTIN(COUNTER__, -1);
97
98 return SUCCESS;
99 }
100
101 PHP_MSHUTDOWN_FUNCTION(psi_builtin)
102 {
103 zend_hash_destroy(&psi_builtins);
104 return SUCCESS;
105 }
106
107 bool psi_builtin_exists(zend_string *name)
108 {
109 return zend_hash_exists(&psi_builtins, name);
110 }
111
112 struct psi_builtin *psi_builtin_get(zend_string *name)
113 {
114 return zend_hash_find_ptr(&psi_builtins, name);
115 }
116
117 static bool has_include(struct psi_cpp *cpp, struct psi_token *target,
118 struct psi_plist **args, struct psi_plist **res)
119 {
120 struct psi_plist *arg = args[0];
121 struct psi_token *tok;
122
123 switch (psi_plist_count(arg)) {
124 case 1:
125 if (psi_plist_get(arg, 0, &tok)) {
126 const char *cpp_search = cpp->search;
127 bool has = psi_cpp_has_include(cpp, tok, 0, NULL);
128 cpp->search = cpp_search;
129 return has;
130 }
131 /* no break */
132 default:
133 cpp->parser->error(PSI_DATA(cpp->parser), target, PSI_WARNING,
134 "Erroneous usage of builtin __%s", __FUNCTION__);
135 }
136 return false;
137 }
138
139 static bool has_include_next(struct psi_cpp *cpp, struct psi_token *target,
140 struct psi_plist **args, struct psi_plist **res)
141 {
142 struct psi_plist *arg = args[0];
143 struct psi_token *tok;
144
145 switch (psi_plist_count(arg)) {
146 case 1:
147 if (psi_plist_get(arg, 0, &tok)) {
148 const char *cpp_search = cpp->search;
149 bool has = psi_cpp_has_include(cpp, tok, PSI_CPP_INCLUDE_NEXT, NULL);
150 cpp->search = cpp_search;
151 return has;
152 }
153 /* no break */
154 default:
155 cpp->parser->error(PSI_DATA(cpp->parser), target, PSI_WARNING,
156 "Erroneous usage of builtin __%s", __FUNCTION__);
157 }
158 return false;
159 }
160
161 static bool builtin_constant_p(struct psi_cpp *cpp, struct psi_token *target,
162 struct psi_plist **args, struct psi_plist **res_ptr)
163 {
164 /* we want functions, not macros for e.g. htonl() */
165 return false;
166 }
167
168 static bool COUNTER__(struct psi_cpp *cpp, struct psi_token *target,
169 struct psi_plist **args, struct psi_plist **res)
170 {
171 struct psi_token *tok;
172 char buf[0x20];
173 size_t len = sprintf(buf, "%u", cpp->counter++);
174
175 tok = psi_token_init(PSI_T_NUMBER, buf, len, target->col, target->line, target->file);
176 *res = psi_plist_init((psi_plist_dtor) psi_token_free);
177 *res = psi_plist_add(*res, &tok);
178
179 return true;
180 }