fix gdbinit; postprocessing macros
[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 = pecalloc(1, sizeof(*macro), 1);
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 psi_token_free(&macro->token);
48 if (macro->exp) {
49 psi_num_exp_free(&macro->exp);
50 }
51 if (macro->sig) {
52 psi_plist_free(macro->sig);
53 }
54 if (macro->tokens) {
55 psi_plist_free(macro->tokens);
56 }
57 free(macro);
58 }
59 }
60
61 void psi_cpp_macro_decl_dump(struct psi_dump *dump, struct psi_cpp_macro_decl *macro)
62 {
63 PSI_DUMP(dump, "%s", macro->token->text->val);
64
65 if (macro->sig) {
66 size_t i = 0;
67 struct psi_token *tok;
68
69 PSI_DUMP(dump, "(");
70 while (psi_plist_get(macro->sig, i++, &tok)) {
71 PSI_DUMP(dump, "%s%s", i>1?",":"", tok->text->val);
72 }
73 PSI_DUMP(dump, ")");
74 }
75
76 if (macro->exp) {
77 PSI_DUMP(dump, " ");
78 psi_num_exp_dump(dump, macro->exp);
79
80 assert(macro->tokens);
81
82 } else if (macro->tokens) {
83 size_t i = 0;
84 struct psi_token *tok;
85
86 while (psi_plist_get(macro->tokens, i++, &tok)) {
87 switch (tok->type) {
88 case PSI_T_QUOTED_STRING:
89 PSI_DUMP(dump, " \"%s\"", tok->text->val);
90 break;
91 case PSI_T_QUOTED_CHAR:
92 PSI_DUMP(dump, " '%s'", tok->text->val);
93 break;
94 default:
95 PSI_DUMP(dump, " %s", tok->text->val);
96 }
97 }
98 }
99 }
100
101 static inline bool cmp_token_list(struct psi_plist *l1, struct psi_plist *l2)
102 {
103 size_t c = psi_plist_count(l1), i;
104
105 if (c != psi_plist_count(l2)) {
106 return false;
107 }
108
109 for (i = 0; i < c; ++i) {
110 struct psi_token *t1, *t2;
111
112 psi_plist_get(l1, i, &t1);
113 psi_plist_get(l2, i, &t2);
114
115 if (!zend_string_equals(t1->text, t2->text)) {
116 return false;
117 }
118 }
119
120 return true;
121 }
122
123 bool psi_cpp_macro_decl_equal(struct psi_cpp_macro_decl *d1, struct psi_cpp_macro_decl *d2)
124 {
125 if (d1->sig) {
126 if (!d2->sig) {
127 return false;
128 }
129
130 if (!cmp_token_list(d1->sig, d2->sig)) {
131 return false;
132 }
133 } else if (d2->sig) {
134 return false;
135 }
136
137 if (d1->tokens) {
138 if (!d2->tokens) {
139 return false;
140 }
141
142 if (!cmp_token_list(d1->tokens, d2->tokens)) {
143 return false;
144 }
145 } else if (d2->tokens) {
146 return false;
147 }
148
149 /* FIXME compare num_exps */
150
151 return true;
152 }