65d37a2e635d6f8fdf69e7970dcb29450310917a
[m6w6/ext-psi] / src / token.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_TOKEN_H
27 #define PSI_TOKEN_H
28
29 #include <stddef.h>
30 #include <stdarg.h>
31 #include <stdint.h>
32 #include <assert.h>
33 #include <sys/param.h>
34
35 static inline size_t psi_align(size_t s, size_t a) {
36 return ((s - 1) | (a - 1)) + 1;
37 }
38
39 static inline size_t psi_offset_padding(size_t diff, size_t alignment) {
40 if (diff && diff <= psi_align(diff, alignment)) {
41 diff = 0;
42 }
43
44 return diff;
45 }
46
47 #include "parser_proc.h"
48
49 #define PSI_T_POINTER PSI_T_ASTERISK
50 #define PSI_T_LONG_DOUBLE (PSI_T_DOUBLE << 16)
51
52 typedef int token_t;
53
54 static inline int psi_num_exp_op_cmp(token_t op1, token_t op2)
55 {
56 assert(!op1 || op1 == PSI_T_LPAREN || (op1 <= PSI_T_NOT && op1 >= PSI_T_PIPE));
57 assert(!op2 || op2 == PSI_T_LPAREN || (op2 <= PSI_T_NOT && op2 >= PSI_T_PIPE));
58
59 if (PSI_T_LPAREN == op2) {
60 return -1;
61 } else if (PSI_T_LPAREN == op1) {
62 return 1;
63 } else if (op1 == op2) {
64 return 0;
65 } else if (!op1) {
66 return 1;
67 } else if (!op2) {
68 return -1;
69 }
70
71 switch (op1) {
72 case PSI_T_PIPE:
73 return op2 > PSI_T_PIPE ? 1 : (op2 < PSI_T_PIPE ? -1 : 0);
74 case PSI_T_CARET:
75 return op2 > PSI_T_CARET ? 1 : (op2 < PSI_T_CARET ? -1 : 0);
76 case PSI_T_AMPERSAND:
77 return op2 > PSI_T_AMPERSAND ? 1 : (op2 < PSI_T_AMPERSAND ? -1 : 0);
78
79 case PSI_T_LSHIFT:
80 case PSI_T_RSHIFT:
81 return op2 > PSI_T_RSHIFT ? 1 : (op2 < PSI_T_LSHIFT ? -1 : 0);
82
83 case PSI_T_PLUS:
84 case PSI_T_MINUS:
85 return op2 > PSI_T_MINUS ? 1 : (op2 < PSI_T_PLUS ? -1 : 0);
86
87 case PSI_T_ASTERISK:
88 case PSI_T_SLASH:
89 case PSI_T_MODULO:
90 return op2 > PSI_T_MODULO ? 1 : (op2 < PSI_T_ASTERISK ? -1 : 0);
91
92 case PSI_T_NOT:
93 case PSI_T_TILDE:
94 return op2 > PSI_T_TILDE ? 1 : (op2 < PSI_T_NOT ? -1 : 0);
95 }
96
97 return 0;
98 }
99
100 static inline size_t psi_t_alignment(token_t t)
101 {
102 #define PSI_ALIGNOF(T) case PSI_T_## T: return ALIGNOF_## T ##_T;
103 switch (t) {
104 PSI_ALIGNOF(INT8);
105 PSI_ALIGNOF(UINT8);
106 PSI_ALIGNOF(INT16);
107 PSI_ALIGNOF(UINT16);
108 PSI_ALIGNOF(INT32);
109 PSI_ALIGNOF(UINT32);
110 PSI_ALIGNOF(INT64);
111 PSI_ALIGNOF(UINT64);
112 case PSI_T_FLOAT:
113 return ALIGNOF_FLOAT;
114 case PSI_T_DOUBLE:
115 return ALIGNOF_DOUBLE;
116 case PSI_T_POINTER:
117 case PSI_T_FUNCTION:
118 return ALIGNOF_VOID_P;
119 case PSI_T_ENUM:
120 return ALIGNOF_INT;
121 #ifdef HAVE_LONG_DOUBLE
122 case PSI_T_LONG_DOUBLE:
123 return ALIGNOF_LONG_DOUBLE;
124 #endif
125 default:
126 assert(0);
127 }
128 return 0;
129 }
130
131 static inline size_t psi_t_size(token_t t)
132 {
133 #define PSI_SIZEOF(T) case PSI_T_## T : return SIZEOF_## T ##_T;
134 switch (t) {
135 PSI_SIZEOF(INT8);
136 PSI_SIZEOF(UINT8);
137 PSI_SIZEOF(INT16);
138 PSI_SIZEOF(UINT16);
139 PSI_SIZEOF(INT32);
140 PSI_SIZEOF(UINT32);
141 PSI_SIZEOF(INT64);
142 PSI_SIZEOF(UINT64);
143 case PSI_T_FLOAT:
144 return SIZEOF_FLOAT;
145 case PSI_T_DOUBLE:
146 return SIZEOF_DOUBLE;
147 case PSI_T_VOID:
148 case PSI_T_POINTER:
149 case PSI_T_FUNCTION:
150 return SIZEOF_VOID_P;
151 case PSI_T_ENUM:
152 return SIZEOF_INT;
153 #ifdef HAVE_LONG_DOUBLE
154 case PSI_T_LONG_DOUBLE:
155 return SIZEOF_LONG_DOUBLE;
156 #endif
157 default:
158 assert(!t);
159 }
160 return 0;
161 }
162
163 static inline const char *psi_t_indent(unsigned level) {
164 static const char indent[] =
165 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
166 return &indent[32 - MIN(32, level)];
167 }
168
169 static inline const char *psi_t_indirection(unsigned pointer_level) {
170 static const char indir[] = "********************************";
171 return &indir[32 - MIN(32, pointer_level)];
172 }
173
174 struct psi_token {
175 token_t type;
176 unsigned size, line, col;
177 char *text, *file;
178 char buf[1];
179 };
180
181 struct psi_parser;
182
183 struct psi_token *psi_token_alloc(struct psi_parser *P);
184 size_t psi_token_alloc_size(size_t token_len, size_t fname_len);
185 struct psi_token *psi_token_copy(struct psi_token *src);
186 struct psi_token *psi_token_cat(unsigned argc, ...);
187 struct psi_token *psi_token_append(struct psi_token *T, unsigned argc, ...);
188 struct psi_token *psi_token_translit(struct psi_token *T, char *from, char *to);
189 uint64_t psi_token_hash(struct psi_token *t, char *digest_buf);
190
191 #endif