Merge branch 'slimconfigure'
[m6w6/ext-psi] / src / types / decl_union.c
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 #include "php_psi_stdinc.h"
27 #include "data.h"
28
29 #include <assert.h>
30
31 struct psi_decl_union* psi_decl_union_init(const char *name,
32 struct psi_plist *args)
33 {
34 struct psi_decl_union *u = calloc(1, sizeof(*u));
35 u->name = strdup(name);
36 u->args = args;
37 return u;
38 }
39
40 void psi_decl_union_free(struct psi_decl_union **u_ptr)
41 {
42 if (*u_ptr) {
43 struct psi_decl_union *u = *u_ptr;
44
45 *u_ptr = NULL;
46 if (u->token) {
47 free(u->token);
48 }
49 if (u->args) {
50 psi_plist_free(u->args);
51 }
52 free(u->name);
53 free(u);
54 }
55 }
56
57 void psi_decl_union_dump(int fd, struct psi_decl_union *unn)
58 {
59 dprintf(fd, "union %s::(%zu, %zu)", unn->name, unn->align, unn->size);
60 if (psi_plist_count(unn->args)) {
61 psi_decl_type_dump_args_with_layout(fd, unn->args, 0);
62 } else {
63 dprintf(fd, ";");
64 }
65 }
66
67 struct psi_decl_arg *psi_decl_union_get_arg(struct psi_decl_union *u,
68 struct psi_decl_var *var)
69 {
70 if (u->args) {
71 return psi_decl_arg_get_by_var(var, u->args, NULL);
72 }
73
74 return NULL;
75 }
76
77 bool psi_decl_union_validate(struct psi_data *data, struct psi_decl_union *u,
78 struct psi_validate_scope *scope)
79 {
80 size_t i, pos, len, size = 0, align;
81 struct psi_decl_arg *darg;
82
83 if (psi_validate_scope_has_union(scope, u->name)) {
84 return true;
85 }
86
87 if (!u->size && !psi_plist_count(u->args)) {
88 data->error(data, u->token, PSI_WARNING,
89 "Cannot compute size of empty union %s", u->name);
90 return false;
91 }
92
93 psi_validate_scope_add_union(scope, u->name, u);
94
95 for (i = 0; psi_plist_get(u->args, i, &darg); ++i) {
96 darg->var->arg = darg;
97
98 if (!psi_decl_arg_validate(data, darg, scope)) {
99 psi_validate_scope_del_union(scope, u->name);
100 return false;
101 }
102
103 if (darg->layout && darg->layout->len) {
104 pos = darg->layout->pos;
105
106 align = psi_decl_arg_align(darg, &pos, &len);
107
108 if (darg->layout->pos != 0) {
109 data->error(data, darg->token, PSI_WARNING,
110 "Offset of %s.%s should be 0", u->name,
111 darg->var->name);
112 darg->layout->pos = 0;
113 }
114 if (darg->layout->len != len) {
115 data->error(data, darg->token, PSI_WARNING,
116 "Computed size %zu of %s.%s does not match"
117 " pre-defined size %zu of type '%s'",
118 len, u->name, darg->var->name, darg->layout->len,
119 darg->type->name);
120 }
121 } else {
122 pos = 0;
123
124 align = psi_decl_arg_align(darg, &pos, &len);
125
126 if (darg->layout) {
127 if (darg->layout->pos != 0) {
128 data->error(data, darg->token, PSI_WARNING,
129 "Offset of %s.%s should be 0", u->name,
130 darg->var->name);
131 darg->layout->pos = 0;
132 }
133 darg->layout->len = len;
134 } else {
135 darg->layout = psi_layout_init(pos, len, NULL);
136 }
137 }
138 if (len > size) {
139 size = len;
140 }
141 if (align > u->align) {
142 u->align = align;
143 }
144 }
145
146 psi_plist_sort(u->args, psi_layout_sort_cmp, NULL);
147
148 if (u->size < size) {
149 u->size = psi_align(size, u->align);
150 }
151
152 assert(u->size);
153
154 return true;
155 }
156
157 size_t psi_decl_union_get_align(struct psi_decl_union *u)
158 {
159 if (!u->align) {
160 u->align = psi_decl_type_get_args_align(u->args);
161 }
162 return u->align;
163 }