Merge branch 'slimconfigure'
[m6w6/ext-psi] / src / types / cpp_macro_decl.c
1 /*******************************************************************************
2 Copyright (c) 2017, 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 #include "php_psi_stdinc.h"
27
28 #include "cpp.h"
29 #include "data.h"
30
31 struct psi_cpp_macro_decl *psi_cpp_macro_decl_init(struct psi_plist *sig,
32 struct psi_plist *tokens, struct psi_num_exp *exp)
33 {
34 struct psi_cpp_macro_decl *macro = calloc(1, sizeof(*macro));
35 macro->exp = exp;
36 macro->sig = sig;
37 macro->tokens = tokens;
38 return macro;
39 }
40
41 void psi_cpp_macro_decl_free(struct psi_cpp_macro_decl **macro_ptr)
42 {
43 if (*macro_ptr) {
44 struct psi_cpp_macro_decl *macro = *macro_ptr;
45
46 *macro_ptr = NULL;
47 if (macro->token) {
48 free(macro->token);
49 }
50 if (macro->exp) {
51 psi_num_exp_free(&macro->exp);
52 }
53 if (macro->sig) {
54 psi_plist_free(macro->sig);
55 }
56 if (macro->tokens) {
57 psi_plist_free(macro->tokens);
58 }
59 free(macro);
60 }
61 }
62
63 void psi_cpp_macro_decl_dump(int fd, struct psi_cpp_macro_decl *macro)
64 {
65 dprintf(fd, "%s", macro->token->text);
66
67 if (macro->sig) {
68 size_t i = 0;
69 struct psi_token *tok;
70
71 dprintf(fd, "(");
72 while (psi_plist_get(macro->sig, i++, &tok)) {
73 dprintf(fd, "%s%s", i>1?",":"", tok->text);
74 }
75 dprintf(fd, ")");
76 }
77
78 if (macro->exp) {
79 dprintf(fd, " ");
80 psi_num_exp_dump(fd, macro->exp);
81 if (!macro->tokens) abort();
82 } else
83 if (macro->tokens) {
84 size_t i = 0;
85 struct psi_token *tok;
86
87 while (psi_plist_get(macro->tokens, i++, &tok)) {
88 switch (tok->type) {
89 case PSI_T_QUOTED_STRING:
90 dprintf(fd, " \"%s\"", tok->text);
91 break;
92 case PSI_T_QUOTED_CHAR:
93 dprintf(fd, " '%s'", tok->text);
94 break;
95 default:
96 dprintf(fd, " %s", tok->text);
97 }
98 }
99 }
100 }
101
102 static inline bool cmp_token_list(struct psi_plist *l1, struct psi_plist *l2)
103 {
104 size_t c = psi_plist_count(l1), i;
105
106 if (c != psi_plist_count(l2)) {
107 return false;
108 }
109
110 for (i = 0; i < c; ++i) {
111 struct psi_token *t1, *t2;
112
113 psi_plist_get(l1, i, &t1);
114 psi_plist_get(l2, i, &t2);
115
116 if (strcmp(t1->text, t2->text)) {
117 return false;
118 }
119 }
120
121 return true;
122 }
123
124 bool psi_cpp_macro_decl_equal(struct psi_cpp_macro_decl *d1, struct psi_cpp_macro_decl *d2)
125 {
126 if (d1->sig) {
127 if (!d2->sig) {
128 return false;
129 }
130
131 if (!cmp_token_list(d1->sig, d2->sig)) {
132 return false;
133 }
134 } else if (d2->sig) {
135 return false;
136 }
137
138 if (d1->tokens) {
139 if (!d2->tokens) {
140 return false;
141 }
142
143 if (!cmp_token_list(d1->tokens, d2->tokens)) {
144 return false;
145 }
146 } else if (d2->tokens) {
147 return false;
148 }
149
150 /* FIXME compare num_exps */
151
152 return true;
153 }