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