5adf14c5f04157f7b52a326f398bd7cdef4c7b81
[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
33 #define PSI_DEBUG 0x1
34 #define PSI_SILENT 0x2
35
36 #include <stdarg.h>
37
38 #define PSI_DEBUG_PRINT(ctx, msg, ...) do { \
39 if ((ctx) && (PSI_DATA(ctx)->flags & PSI_DEBUG)) { \
40 fprintf(stderr, msg, __VA_ARGS__); \
41 } \
42 } while(0)
43
44
45 #define PSI_DATA(D) ((struct psi_data *) (D))
46
47 #define PSI_DATA_MEMBERS \
48 struct psi_decl_file file; \
49 struct psi_plist *consts; \
50 struct psi_plist *types; \
51 struct psi_plist *structs; \
52 struct psi_plist *unions; \
53 struct psi_plist *enums; \
54 struct psi_plist *decls; \
55 struct psi_plist *vars; \
56 struct psi_plist *impls; \
57 struct psi_plist *libs; \
58 psi_error_cb error; \
59 char last_error[0x1000]; \
60 unsigned errors; \
61 unsigned flags
62
63 struct psi_data {
64 PSI_DATA_MEMBERS;
65 };
66
67 struct psi_data *psi_data_ctor(struct psi_data *data, psi_error_cb error, unsigned flags);
68 struct psi_data *psi_data_ctor_with_dtors(struct psi_data *data, psi_error_cb error, unsigned flags);
69 struct psi_data *psi_data_exchange(struct psi_data *dest, struct psi_data *src);
70 bool psi_data_validate(struct psi_data *dst, struct psi_data *src);
71 void psi_data_dtor(struct psi_data *data);
72 void psi_data_dump(int fd, struct psi_data *data);
73
74 struct psi_validate_stack {
75 HashTable types;
76 HashTable structs;
77 HashTable unions;
78 };
79
80 static inline void psi_validate_stack_ctor(struct psi_validate_stack *stack)
81 {
82 zend_hash_init(&stack->types, 0, NULL, NULL, 0);
83 zend_hash_init(&stack->structs, 0, NULL, NULL, 0);
84 zend_hash_init(&stack->unions, 0, NULL, NULL, 0);
85 }
86
87 static inline void psi_validate_stack_dtor(struct psi_validate_stack *stack)
88 {
89 zend_hash_destroy(&stack->types);
90 zend_hash_destroy(&stack->structs);
91 zend_hash_destroy(&stack->unions);
92 }
93
94 #define psi_validate_stack_has_type(s, t) \
95 ((s) ? zend_hash_str_exists(&(s)->types, (t), strlen(t)) : false)
96 #define psi_validate_stack_has_struct(s, t) \
97 ((s) ? zend_hash_str_exists(&(s)->structs, (t), strlen(t)) : false)
98 #define psi_validate_stack_has_union(s, t) \
99 ((s) ? zend_hash_str_exists(&(s)->unions, (t), strlen(t)) : false)
100
101 #define psi_validate_stack_add_type(s, t, p) \
102 do { if (s) zend_hash_str_add_ptr(&(s)->types, (t), strlen(t), (p)); } while(0)
103 #define psi_validate_stack_add_struct(s, t, p) \
104 do { if (s) zend_hash_str_add_ptr(&(s)->structs, (t), strlen(t), (p)); } while(0)
105 #define psi_validate_stack_add_union(s, t, p) \
106 do { if (s) zend_hash_str_add_ptr(&(s)->unions, (t), strlen(t), (p)); } while(0)
107
108 #define psi_validate_stack_get_type(s, t) \
109 ((s) ? zend_hash_str_find_ptr(&(s)->types, (t), strlen(t)) : NULL)
110 #define psi_validate_stack_get_struct(s, t) \
111 ((s) ? zend_hash_str_find_ptr(&(s)->structs, (t), strlen(t)) : NULL)
112 #define psi_validate_stack_get_union(s, t) \
113 ((s) ? zend_hash_str_find_ptr(&(s)->unions, (t), strlen(t)) : NULL)
114
115 #define psi_validate_stack_del_type(s, t) \
116 do { if (s) zend_hash_str_del(&(s)->types, (t), strlen(t)); } while(0)
117 #define psi_validate_stack_del_struct(s, t) \
118 do { if (s) zend_hash_str_del(&(s)->structs, (t), strlen(t)); } while(0)
119 #define psi_validate_stack_del_union(s, t) \
120 do { if (s) zend_hash_str_del(&(s)->unions, (t), strlen(t)); } while(0)
121
122 #endif