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