42ed0f4208bbad931b8879dc6b575e370cc048e4
[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 } else if (macro->tokens) {
82 size_t i = 0;
83 struct psi_token *tok;
84
85 while (psi_plist_get(macro->tokens, i++, &tok)) {
86 dprintf(fd, " %s", tok->text);
87 }
88 }
89 }
90
91 static inline bool cmp_token_list(struct psi_plist *l1, struct psi_plist *l2)
92 {
93 size_t c = psi_plist_count(l1), i;
94
95 if (c != psi_plist_count(l2)) {
96 return false;
97 }
98
99 for (i = 0; i < c; ++i) {
100 struct psi_token *t1, *t2;
101
102 psi_plist_get(l1, i, &t1);
103 psi_plist_get(l2, i, &t2);
104
105 if (strcmp(t1->text, t2->text)) {
106 return false;
107 }
108 }
109
110 return true;
111 }
112
113 bool psi_cpp_macro_decl_equal(struct psi_cpp_macro_decl *d1, struct psi_cpp_macro_decl *d2)
114 {
115 if (d1->sig) {
116 if (!d2->sig) {
117 return false;
118 }
119
120 if (!cmp_token_list(d1->sig, d2->sig)) {
121 return false;
122 }
123 }
124
125 if (d1->tokens) {
126 if (!d2->tokens) {
127 return false;
128 }
129
130 if (!cmp_token_list(d1->tokens, d2->tokens)) {
131 return false;
132 }
133 }
134
135 /* FIXME compare num_exps */
136
137 return true;
138 }