build: PHP<7.2 compatibility
[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 size_t psi_t_alignment(token_t t)
55 {
56 #define PSI_ALIGNOF(T) case PSI_T_## T: return ALIGNOF_## T ##_T;
57 switch (t) {
58 PSI_ALIGNOF(INT8);
59 PSI_ALIGNOF(UINT8);
60 PSI_ALIGNOF(INT16);
61 PSI_ALIGNOF(UINT16);
62 PSI_ALIGNOF(INT32);
63 PSI_ALIGNOF(UINT32);
64 PSI_ALIGNOF(INT64);
65 PSI_ALIGNOF(UINT64);
66 case PSI_T_FLOAT:
67 return ALIGNOF_FLOAT;
68 case PSI_T_DOUBLE:
69 return ALIGNOF_DOUBLE;
70 case PSI_T_POINTER:
71 case PSI_T_FUNCTION:
72 return ALIGNOF_VOID_P;
73 case PSI_T_ENUM:
74 return ALIGNOF_INT;
75 #ifdef HAVE_LONG_DOUBLE
76 case PSI_T_LONG_DOUBLE:
77 return ALIGNOF_LONG_DOUBLE;
78 #endif
79 default:
80 assert(0);
81 }
82 return 0;
83 }
84
85 static inline size_t psi_t_size(token_t t)
86 {
87 #define PSI_SIZEOF(T) case PSI_T_## T : return SIZEOF_## T ##_T;
88 switch (t) {
89 PSI_SIZEOF(INT8);
90 PSI_SIZEOF(UINT8);
91 PSI_SIZEOF(INT16);
92 PSI_SIZEOF(UINT16);
93 PSI_SIZEOF(INT32);
94 PSI_SIZEOF(UINT32);
95 PSI_SIZEOF(INT64);
96 PSI_SIZEOF(UINT64);
97 case PSI_T_FLOAT:
98 return SIZEOF_FLOAT;
99 case PSI_T_DOUBLE:
100 return SIZEOF_DOUBLE;
101 case PSI_T_VOID:
102 case PSI_T_POINTER:
103 case PSI_T_FUNCTION:
104 return SIZEOF_VOID_P;
105 case PSI_T_ENUM:
106 return SIZEOF_INT;
107 #ifdef HAVE_LONG_DOUBLE
108 case PSI_T_LONG_DOUBLE:
109 return SIZEOF_LONG_DOUBLE;
110 #endif
111 default:
112 assert(!t);
113 }
114 return 0;
115 }
116
117 static inline const char *psi_t_indent(unsigned level) {
118 static const char indent[] =
119 "\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";
120 return &indent[32 - MIN(32, level)];
121 }
122
123 static inline const char *psi_t_indirection(unsigned pointer_level) {
124 static const char indir[] = "********************************";
125 return &indir[32 - MIN(32, pointer_level)];
126 }
127
128 struct psi_token {
129 token_t type;
130 unsigned size, line, col;
131 char *text, *file;
132 char buf[1];
133 };
134
135 struct psi_parser;
136
137 struct psi_token *psi_token_alloc(struct psi_parser *P);
138 size_t psi_token_alloc_size(size_t token_len, size_t fname_len);
139 struct psi_token *psi_token_copy(struct psi_token *src);
140 struct psi_token *psi_token_cat(unsigned argc, ...);
141 struct psi_token *psi_token_append(struct psi_token *T, unsigned argc, ...);
142 struct psi_token *psi_token_translit(struct psi_token *T, char *from, char *to);
143 uint64_t psi_token_hash(struct psi_token *t, char *digest_buf);
144
145 #endif