types 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 #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_struct* init_decl_struct(const char* name, decl_args* args) {
40 decl_struct* s = calloc(1, sizeof(*s));
41 s->name = strdup(name);
42 s->args = args;
43 return s;
44 }
45
46 void free_decl_struct(decl_struct* s) {
47 if (s->token) {
48 free(s->token);
49 }
50 if (s->args) {
51 free_decl_args(s->args);
52 }
53 if (s->engine.type && s->engine.dtor) {
54 s->engine.dtor(s->engine.type);
55 }
56 free(s->name);
57 free(s);
58 }
59
60 void dump_decl_struct(int fd, decl_struct *strct) {
61 dprintf(fd, "struct %s::(%zu, %zu)", strct->name, strct->align, strct->size);
62 if (strct->args && strct->args->count) {
63 dump_decl_args_with_layout(fd, strct->args, 0);
64 } else {
65 dprintf(fd, ";");
66 }
67 }
68
69 decl_arg *locate_decl_struct_member(decl_struct *s, decl_var *var) {
70 if (s->args) {
71 return locate_decl_var_arg(var, s->args, NULL);
72 }
73
74 return NULL;
75 }
76
77 int validate_decl_struct(struct psi_data *data, decl_struct *s) {
78 size_t i, pos, len, size, align;
79
80 if (!s->size && !s->args->count) {
81 data->error(data, s->token, PSI_WARNING,
82 "Cannot compute size of empty struct '%s'",
83 s->name);
84 return 0;
85 }
86
87 for (i = 0; i < s->args->count; ++i) {
88 decl_arg *darg = s->args->args[i];
89
90 if (!validate_decl_arg(data, darg)) {
91 return 0;
92 }
93
94 assert(!darg->var->arg || darg->var->arg == darg);
95
96 darg->var->arg = darg;
97
98 if (!validate_decl_arg_args(data, darg, s)) {
99 return 0;
100 } else if (darg->layout) {
101 pos = darg->layout->pos;
102
103 align = align_decl_arg(darg, &pos, &len);
104
105 if (darg->layout->len != len) {
106 data->error(data, darg->token, PSI_WARNING,
107 "Computed size %zu of %s.%s does not match"
108 " pre-defined size %zu of type '%s'",
109 len, s->name, darg->var->name, darg->layout->len,
110 darg->type->name);
111 }
112 if (darg->layout->pos != pos) {
113 data->error(data, darg->token, PSI_WARNING,
114 "Computed offset %zu of %s.%s does not match"
115 " pre-defined offset %zu",
116 pos, s->name, darg->var->name, darg->layout->pos);
117 }
118 } else {
119 if (i) {
120 pos = s->args->args[i-1]->layout->pos +
121 s->args->args[i-1]->layout->len;
122 } else {
123 pos = 0;
124 }
125
126 align = align_decl_arg(darg, &pos, &len);
127 darg->layout = init_decl_struct_layout(pos, len);
128 }
129
130 if (align > s->align) {
131 s->align = align;
132 }
133 }
134
135 sort_decl_args(s->args);
136
137 if (s->args->count) {
138 decl_arg *darg = s->args->args[s->args->count-1];
139
140 size = darg->layout->pos + darg->layout->len;
141 if (s->size < size) {
142 s->size = psi_align(size, s->align);
143 }
144 }
145
146 return 1;
147 }
148
149 size_t alignof_decl_struct(decl_struct *s) {
150 if (!s->align) {
151 s->align = alignof_decl_args(s->args);
152 }
153 return s->align;
154 }