raising the head after a three-weeks refactoring
[m6w6/ext-psi] / src / types / decl_struct.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_struct* psi_decl_struct_init(const char *name,
32 struct psi_plist *args)
33 {
34 struct psi_decl_struct *s = calloc(1, sizeof(*s));
35 s->name = strdup(name);
36 s->args = args;
37 return s;
38 }
39
40 void psi_decl_struct_free(struct psi_decl_struct **s_ptr)
41 {
42 if (*s_ptr) {
43 struct psi_decl_struct *s = *s_ptr;
44
45 *s_ptr = NULL;
46 if (s->token) {
47 free(s->token);
48 }
49 if (s->args) {
50 psi_plist_free(s->args);
51 }
52 if (s->engine.type && s->engine.dtor) {
53 s->engine.dtor(s->engine.type);
54 }
55 free(s->name);
56 free(s);
57 }
58 }
59
60 void psi_decl_struct_dump(int fd, struct psi_decl_struct *strct)
61 {
62 dprintf(fd, "struct %s::(%zu, %zu)", strct->name, strct->align,
63 strct->size);
64 if (psi_plist_count(strct->args)) {
65 psi_decl_type_dump_args_with_layout(fd, strct->args, 0);
66 } else {
67 dprintf(fd, ";");
68 }
69 }
70
71 struct psi_decl_arg *psi_decl_struct_get_arg(struct psi_decl_struct *s,
72 struct psi_decl_var *var)
73 {
74 if (s->args) {
75 return psi_decl_arg_get_by_var(var, s->args, NULL);
76 }
77
78 return NULL;
79 }
80
81 bool psi_decl_struct_validate(struct psi_data *data, struct psi_decl_struct *s)
82 {
83 size_t i, pos, len, size, align;
84 struct psi_decl_arg *darg, *prev_arg;
85
86 if (!s->size && !psi_plist_count(s->args)) {
87 data->error(data, s->token, PSI_WARNING,
88 "Cannot compute size of empty struct '%s'", s->name);
89 return false;
90 }
91
92 for (i = 0; psi_plist_get(s->args, i, &darg); ++i) {
93 darg->var->arg = darg;
94
95 if (!psi_decl_arg_validate(data, darg)) {
96 return false;
97 }
98
99 if (darg->layout) {
100 pos = darg->layout->pos;
101 align = psi_decl_arg_align(darg, &pos, &len);
102
103 if (darg->layout->len != len) {
104 data->error(data, darg->token, PSI_WARNING,
105 "Computed size %zu of %s.%s does not match"
106 " pre-defined size %zu of type '%s'",
107 len, s->name, darg->var->name, darg->layout->len,
108 darg->type->name);
109 }
110 if (darg->layout->pos != pos) {
111 data->error(data, darg->token, PSI_WARNING,
112 "Computed offset %zu of %s.%s does not match"
113 " pre-defined offset %zu",
114 pos, s->name, darg->var->name, darg->layout->pos);
115 }
116 } else {
117 if (i) {
118 pos = prev_arg->layout->pos + prev_arg->layout->len;
119 } else {
120 pos = 0;
121 }
122
123 align = psi_decl_arg_align(darg, &pos, &len);
124 darg->layout = psi_layout_init(pos, len);
125 }
126
127 if (align > s->align) {
128 s->align = align;
129 }
130 prev_arg = darg;
131 }
132
133 if (psi_plist_count(s->args)) {
134 psi_plist_sort(s->args, psi_layout_sort_cmp, NULL);
135 psi_plist_get(s->args, psi_plist_count(s->args) - 1, &darg);
136
137 size = darg->layout->pos + darg->layout->len;
138 if (s->size < size) {
139 s->size = psi_align(size, s->align);
140 }
141 }
142
143 return true;
144 }
145
146 size_t psi_decl_struct_get_align(struct psi_decl_struct *s)
147 {
148 if (!s->align) {
149 s->align = psi_decl_type_get_args_align(s->args);
150 }
151 return s->align;
152 }