parser: accept trailing comma in enums; __restrict for arrays
[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 *impls; \
56 struct psi_plist *libs; \
57 psi_error_cb error; \
58 char last_error[0x1000]; \
59 unsigned errors; \
60 unsigned flags
61
62 struct psi_data {
63 PSI_DATA_MEMBERS;
64 };
65
66 struct psi_data *psi_data_ctor(struct psi_data *data, psi_error_cb error, unsigned flags);
67 struct psi_data *psi_data_ctor_with_dtors(struct psi_data *data, psi_error_cb error, unsigned flags);
68 struct psi_data *psi_data_exchange(struct psi_data *dest, struct psi_data *src);
69 bool psi_data_validate(struct psi_data *dst, struct psi_data *src);
70 void psi_data_dtor(struct psi_data *data);
71 void psi_data_dump(int fd, struct psi_data *data);
72
73 struct psi_validate_stack {
74 HashTable types;
75 HashTable structs;
76 HashTable unions;
77 };
78
79 static inline void psi_validate_stack_ctor(struct psi_validate_stack *stack)
80 {
81 zend_hash_init(&stack->types, 0, NULL, NULL, 0);
82 zend_hash_init(&stack->structs, 0, NULL, NULL, 0);
83 zend_hash_init(&stack->unions, 0, NULL, NULL, 0);
84 }
85
86 static inline void psi_validate_stack_dtor(struct psi_validate_stack *stack)
87 {
88 zend_hash_destroy(&stack->types);
89 zend_hash_destroy(&stack->structs);
90 zend_hash_destroy(&stack->unions);
91 }
92
93 #define psi_validate_stack_has_type(s, t) \
94 ((s) ? zend_hash_str_exists(&(s)->types, (t), strlen(t)) : false)
95 #define psi_validate_stack_has_struct(s, t) \
96 ((s) ? zend_hash_str_exists(&(s)->structs, (t), strlen(t)) : false)
97 #define psi_validate_stack_has_union(s, t) \
98 ((s) ? zend_hash_str_exists(&(s)->unions, (t), strlen(t)) : false)
99
100 #define psi_validate_stack_add_type(s, t, p) \
101 do { if (s) zend_hash_str_add_ptr(&(s)->types, (t), strlen(t), (p)); } while(0)
102 #define psi_validate_stack_add_struct(s, t, p) \
103 do { if (s) zend_hash_str_add_ptr(&(s)->structs, (t), strlen(t), (p)); } while(0)
104 #define psi_validate_stack_add_union(s, t, p) \
105 do { if (s) zend_hash_str_add_ptr(&(s)->unions, (t), strlen(t), (p)); } while(0)
106
107 #define psi_validate_stack_get_type(s, t) \
108 ((s) ? zend_hash_str_find_ptr(&(s)->types, (t), strlen(t)) : NULL)
109 #define psi_validate_stack_get_struct(s, t) \
110 ((s) ? zend_hash_str_find_ptr(&(s)->structs, (t), strlen(t)) : NULL)
111 #define psi_validate_stack_get_union(s, t) \
112 ((s) ? zend_hash_str_find_ptr(&(s)->unions, (t), strlen(t)) : NULL)
113
114 #define psi_validate_stack_del_type(s, t) \
115 do { if (s) zend_hash_str_del(&(s)->types, (t), strlen(t)); } while(0)
116 #define psi_validate_stack_del_struct(s, t) \
117 do { if (s) zend_hash_str_del(&(s)->structs, (t), strlen(t)); } while(0)
118 #define psi_validate_stack_del_union(s, t) \
119 do { if (s) zend_hash_str_del(&(s)->unions, (t), strlen(t)); } while(0)
120
121 #endif