7e58cbfb7821320ea941138f8f31479af08cdef8
[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
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <assert.h>
36
37 #include "data.h"
38
39 decl_union* init_decl_union(const char* name, decl_args* args) {
40 decl_union* u = calloc(1, sizeof(*u));
41 u->name = strdup(name);
42 u->args = args;
43 return u;
44 }
45
46 void free_decl_union(decl_union* u) {
47 if (u->token) {
48 free(u->token);
49 }
50 if (u->args) {
51 free_decl_args(u->args);
52 }
53 free(u->name);
54 free(u);
55 }
56
57 void dump_decl_union(int fd, decl_union *unn) {
58 dprintf(fd, "union %s::(%zu, %zu)", unn->name, unn->align, unn->size);
59 if (unn->args && unn->args->count) {
60 dump_decl_args_with_layout(fd, unn->args, 0);
61 } else {
62 dprintf(fd, ";");
63 }
64 }
65
66 decl_arg *locate_decl_union_member(decl_union *u, decl_var *var) {
67 if (u->args) {
68 return locate_decl_var_arg(var, u->args, NULL);
69 }
70
71 return NULL;
72 }
73
74 int validate_decl_union(struct psi_data *data, decl_union *u) {
75 size_t i, pos, len, size = 0, align;
76
77 if (!u->size && !u->args->count) {
78 data->error(data, u->token, PSI_WARNING,
79 "Cannot compute size of empty union %s",
80 u->name);
81 return 0;
82 }
83
84 for (i = 0; i < u->args->count; ++i) {
85 decl_arg *darg = u->args->args[i];
86
87 if (!validate_decl_arg(data, darg)) {
88 return 0;
89 }
90
91 assert(!darg->var->arg || darg->var->arg == darg);
92
93 darg->var->arg = darg;
94
95 if (!validate_decl_arg_args(data, darg, u)) {
96 return 0;
97 } else if (darg->layout) {
98 pos = darg->layout->pos;
99
100 align = align_decl_arg(darg, &pos, &len);
101
102 if (darg->layout->pos != 0) {
103 data->error(data, darg->token, PSI_WARNING,
104 "Offset of %s.%s should be 0",
105 u->name, darg->var->name);
106 darg->layout->pos = 0;
107 }
108 if (darg->layout->len != len) {
109 data->error(data, darg->token, PSI_WARNING,
110 "Computed size %zu of %s.%s does not match"
111 " pre-defined size %zu of type '%s'",
112 len, u->name, darg->var->name, darg->layout->len,
113 darg->type->name);
114 }
115 } else {
116 pos = 0;
117
118 align = align_decl_arg(darg, &pos, &len);
119 darg->layout = init_decl_struct_layout(pos, len);
120
121 }
122 if (len > size) {
123 size = len;
124 }
125 if (align > u->align) {
126 u->align = align;
127 }
128 }
129
130 sort_decl_args(u->args);
131
132 if (u->size < size) {
133 u->size = psi_align(size, u->align);
134 }
135
136 return 1;
137 }
138
139 size_t alignof_decl_union(decl_union *u) {
140 if (!u->align) {
141 u->align = alignof_decl_args(u->args);
142 }
143 return u->align;
144 }