1 /*******************************************************************************
2 Copyright (c) 2018, Michael Wallner <mike@php.net>.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
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.
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 *******************************************************************************/
26 #include "php_psi_stdinc.h"
35 HashTable psi_builtins
;
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
);
42 static inline struct psi_plist
*builtin_sig(token_t typ
, ...)
44 struct psi_plist
*sig
;
48 if (typ
== (token_t
) - 1) {
52 sig
= psi_plist_init((psi_plist_dtor
) psi_token_free
);
56 struct psi_token
*arg
;
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
);
67 static void free_builtin(zval
*p
)
69 struct psi_builtin
*b
= Z_PTR_P(p
);
72 zend_string_release(b
->name
);
73 psi_cpp_macro_decl_free(&b
->decl
);
78 PHP_MINIT_FUNCTION(psi_builtin
)
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; \
89 zend_hash_add_mem(&psi_builtins, entry.name, &entry, sizeof(entry)); \
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);
101 PHP_MSHUTDOWN_FUNCTION(psi_builtin
)
103 zend_hash_destroy(&psi_builtins
);
107 bool psi_builtin_exists(zend_string
*name
)
109 return zend_hash_exists(&psi_builtins
, name
);
112 struct psi_builtin
*psi_builtin_get(zend_string
*name
)
114 return zend_hash_find_ptr(&psi_builtins
, name
);
117 static bool has_include(struct psi_cpp
*cpp
, struct psi_token
*target
,
118 struct psi_plist
**args
, struct psi_plist
**res
)
120 struct psi_plist
*arg
= args
[0];
121 struct psi_token
*tok
;
123 switch (psi_plist_count(arg
)) {
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
;
133 cpp
->parser
->error(PSI_DATA(cpp
->parser
), target
, PSI_WARNING
,
134 "Erroneous usage of builtin __%s", __FUNCTION__
);
139 static bool has_include_next(struct psi_cpp
*cpp
, struct psi_token
*target
,
140 struct psi_plist
**args
, struct psi_plist
**res
)
142 struct psi_plist
*arg
= args
[0];
143 struct psi_token
*tok
;
145 switch (psi_plist_count(arg
)) {
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
;
155 cpp
->parser
->error(PSI_DATA(cpp
->parser
), target
, PSI_WARNING
,
156 "Erroneous usage of builtin __%s", __FUNCTION__
);
161 static bool builtin_constant_p(struct psi_cpp
*cpp
, struct psi_token
*target
,
162 struct psi_plist
**args
, struct psi_plist
**res_ptr
)
164 /* we want functions, not macros for e.g. htonl() */
168 static bool COUNTER__(struct psi_cpp
*cpp
, struct psi_token
*target
,
169 struct psi_plist
**args
, struct psi_plist
**res
)
171 struct psi_token
*tok
;
173 size_t len
= sprintf(buf
, "%u", cpp
->counter
++);
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
);