cpp
[m6w6/ext-psi] / src / types / cpp_exp.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 <assert.h>
29
30 #include "data.h"
31
32 struct psi_cpp_exp *psi_cpp_exp_init(token_t type, void *data)
33 {
34 struct psi_cpp_exp *exp = calloc(1, sizeof(*exp));
35
36 switch ((exp->type = type)) {
37 case PSI_T_WARNING:
38 case PSI_T_ERROR:
39 case PSI_T_IFDEF:
40 case PSI_T_IFNDEF:
41 exp->data.tok = data;
42 break;
43 case PSI_T_DEFINE:
44 exp->data.decl = data;
45 break;
46 case PSI_T_IF:
47 case PSI_T_ELIF:
48 exp->data.num = data;
49 break;
50 case PSI_T_ENDIF:
51 case PSI_T_ELSE:
52 break;
53 default:
54 assert(0);
55 break;
56 }
57 return exp;
58 }
59
60 void psi_cpp_exp_free(struct psi_cpp_exp **exp_ptr)
61 {
62 if (*exp_ptr) {
63 struct psi_cpp_exp *exp = *exp_ptr;
64
65 *exp_ptr = NULL;
66 switch (exp->type) {
67 case PSI_T_WARNING:
68 case PSI_T_ERROR:
69 case PSI_T_IFDEF:
70 case PSI_T_IFNDEF:
71 free(exp->data.tok);
72 exp->data.tok = NULL;
73 break;
74 case PSI_T_DEFINE:
75 psi_cpp_macro_decl_free(&exp->data.decl);
76 break;
77 case PSI_T_IF:
78 case PSI_T_ELIF:
79 psi_num_exp_free(&exp->data.num);
80 break;
81 case PSI_T_ENDIF:
82 case PSI_T_ELSE:
83 break;
84 default:
85 assert(0);
86 break;
87 }
88 if (exp->token) {
89 free(exp->token);
90 }
91 free(exp);
92 }
93 }
94
95 void psi_cpp_exp_dump(int fd, struct psi_cpp_exp *exp)
96 {
97 dprintf(fd, "#%s ", exp->token->text);
98 switch (exp->type) {
99 case PSI_T_WARNING:
100 case PSI_T_ERROR:
101 case PSI_T_IFDEF:
102 case PSI_T_IFNDEF:
103 dprintf(fd, "%s", exp->data.tok->text);
104 break;
105 case PSI_T_DEFINE:
106 psi_cpp_macro_decl_dump(fd, exp->data.decl);
107 break;
108 case PSI_T_IF:
109 case PSI_T_ELIF:
110 psi_num_exp_dump(fd, exp->data.num);
111 break;
112 case PSI_T_ENDIF:
113 case PSI_T_ELSE:
114 break;
115 default:
116 assert(0);
117 break;
118 }
119 dprintf(fd, "\n");
120 }