build administrativa
[m6w6/ext-psi] / src / data.h
1 /*******************************************************************************
2 Copyright (c) 2016, 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 #ifndef PSI_DATA_H
27 #define PSI_DATA_H
28
29 #include "types.h"
30 #include "error.h"
31 #include "plist.h"
32 #include "validate.h"
33
34 #include <stdarg.h>
35 #include <dlfcn.h>
36 #include <unistd.h>
37
38 #define PSI_DEBUG 0x1
39 #define PSI_SILENT 0x2
40
41 #ifndef RTLD_NEXT
42 # define RTLD_NEXT ((void *) -1l)
43 #endif
44 #ifndef RTLD_DEFAULT
45 # define RTLD_DEFAULT ((void *) 0)
46 #endif
47
48 #if PSI_THREADED_PARSER
49 zend_string *psi_string_init_interned(const char *buf, size_t len, int p);
50 zend_string *psi_new_interned_string(zend_string *str);
51 #else
52 # define psi_string_init_interned zend_string_init_interned
53 # define psi_new_interned_string zend_new_interned_string
54 #endif
55
56 static inline void *psi_dlsym(struct psi_plist *dllist, const char *name, const char *redir)
57 {
58 void *dl, *sym = NULL;
59 const char *test = redir ?: name;
60
61 again:
62 if (dllist) {
63 size_t i = 0;
64
65 while (!sym && psi_plist_get(dllist, i++, &dl)) {
66 sym = dlsym(dl, test);
67 }
68 }
69 if (!sym) {
70 sym = dlsym(RTLD_DEFAULT, test);
71 }
72 if (!sym && test == redir) {
73 test = name;
74 goto again;
75 }
76
77 return sym;
78 }
79
80 #define PSI_DEBUG_PRINT(ctx, ...) do { \
81 if ((ctx) && (PSI_DATA(ctx)->flags & PSI_DEBUG)) { \
82 dprintf(PSI_DATA(ctx)->debug_fd, __VA_ARGS__); \
83 } \
84 } while(0)
85 #define PSI_DEBUG_PRINTV(ctx, msg, argv) do { \
86 if ((ctx) && (PSI_DATA(ctx)->flags & PSI_DEBUG)) { \
87 vdprintf(PSI_DATA(ctx)->debug_fd, msg, argv); \
88 } \
89 } while(0)
90 #define PSI_DEBUG_DUMP(ctx, dump_func, ...) do { \
91 if ((ctx) && (PSI_DATA(ctx)->flags & PSI_DEBUG)) { \
92 struct psi_dump dump_ = {{ .fd = PSI_DATA(ctx)->debug_fd}, \
93 .fun = (psi_dump_cb) dprintf}; \
94 dump_func(&dump_, __VA_ARGS__); \
95 } \
96 } while (0)
97
98 union psi_dump_arg {
99 void *hn;
100 int fd;
101 };
102 typedef int (*psi_dump_cb)(union psi_dump_arg, const char *msg, ...);
103 struct psi_dump {
104 union psi_dump_arg ctx;
105 psi_dump_cb fun;
106 };
107 #define PSI_DUMP(dump, ...) do { \
108 struct psi_dump _dump_tmp, *_dump_ptr = dump; \
109 if (!_dump_ptr) { \
110 _dump_ptr = &_dump_tmp; \
111 _dump_tmp.ctx.fd = STDOUT_FILENO; \
112 _dump_tmp.fun = (psi_dump_cb) dprintf; \
113 } \
114 _dump_ptr->fun(_dump_ptr->ctx, __VA_ARGS__); \
115 } while(0)
116
117 #define PSI_DATA(D) ((struct psi_data *) (D))
118
119 #define PSI_DATA_MEMBERS \
120 struct psi_decl_file file; \
121 struct psi_plist *consts; \
122 struct psi_plist *types; \
123 struct psi_plist *structs; \
124 struct psi_plist *unions; \
125 struct psi_plist *enums; \
126 struct psi_plist *decls; \
127 struct psi_plist *vars; \
128 struct psi_plist *impls; \
129 psi_error_cb error; \
130 char last_error[0x1000]; \
131 unsigned errors; \
132 unsigned flags; \
133 int debug_fd
134
135 struct psi_data {
136 PSI_DATA_MEMBERS;
137 };
138
139 struct psi_data *psi_data_ctor(struct psi_data *data, psi_error_cb error, unsigned flags);
140 struct psi_data *psi_data_ctor_with_dtors(struct psi_data *data, psi_error_cb error, unsigned flags);
141 struct psi_data *psi_data_exchange(struct psi_data *dest, struct psi_data *src);
142 void psi_data_dtor(struct psi_data *data);
143 void psi_data_dump(struct psi_dump *dump, struct psi_data *data);
144
145 #endif