types refactoring
[m6w6/ext-psi] / src / types / let_func.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 #include "marshal.h"
39
40 let_func *init_let_func(token_t type, const char *name, impl_var *var) {
41 let_func *func = calloc(1, sizeof(*func));
42 func->type = type;
43 func->name = strdup(name);
44 func->var = var;
45 return func;
46 }
47
48 void free_let_func(let_func *func) {
49 free_impl_var(func->var);
50 free(func->name);
51 if (func->inner) {
52 free_let_vals(func->inner);
53 }
54 free(func);
55 }
56
57 void dump_let_func(int fd, let_func *func, unsigned level) {
58 dprintf(fd, "%s(%s", func->name, func->var->name);
59
60 if (func->inner) {
61 size_t i;
62
63 dprintf(fd, ",");
64 for (i = 0; i < func->inner->count; ++i) {
65 dprintf(fd, "\n");
66 dump_let_val(fd, func->inner->vals[i], level+1, i+1 == func->inner->count);
67 }
68 dprintf(fd, "\n");
69 dprintf(fd, "%s", psi_t_indent(level));
70 }
71 dprintf(fd, ")");
72 }
73
74 static inline int validate_let_func_type(struct psi_data *data, let_func *func, impl *impl) {
75 switch (func->type) {
76 case PSI_T_BOOLVAL:
77 case PSI_T_INTVAL:
78 case PSI_T_FLOATVAL:
79 case PSI_T_STRVAL:
80 case PSI_T_STRLEN:
81 case PSI_T_PATHVAL:
82 case PSI_T_ARRVAL:
83 case PSI_T_OBJVAL:
84 case PSI_T_ZVAL:
85 case PSI_T_VOID:
86 return 1;
87 default:
88 data->error(data, func->var->token, PSI_WARNING,
89 "Unknown `let` cast function '%s' of implementation '%s'",
90 func->name, impl->func->name);
91 return 0;
92 }
93 }
94
95 static inline int validate_let_func_inner(struct psi_data *data, let_func *func, decl_var *let_var, impl *impl) {
96 if (func->inner) {
97 size_t i;
98 decl_type *var_typ;
99 decl_args *sub_args = extract_decl_type_args(let_var->arg->type, &var_typ);
100
101 if (!sub_args) {
102 data->error(data, let_var->token, PSI_WARNING,
103 "Inner let statement's values must refer to a structure type,"
104 " got '%s' for '%s'",
105 var_typ->name, let_var->name);
106 return 0;
107 }
108
109 for (i = 0; i < func->inner->count; ++i) {
110 let_val *inner = func->inner->vals[i];
111 let_val *outer = func->outer;
112 const char *name = locate_let_val_varname(inner);
113 decl_arg *sub_arg;
114
115 if (name) {
116 sub_arg = locate_decl_arg(sub_args, name);
117 }
118 if (!name || !sub_arg) {
119 data->error(data, let_var->token, PSI_WARNING,
120 "Unknown variable '%s' of '%s'",
121 name, var_typ->real.strct->name);
122 return 0;
123 }
124 if (!validate_let_val(data, inner, sub_arg->var, impl)) {
125 return 0;
126 }
127 }
128 }
129 return 1;
130 }
131
132 int validate_let_func(struct psi_data *data, let_func *func, decl_var *let_var, impl *impl) {
133 if (func->outer) {
134
135 }
136 if (impl->func->args) {
137 locate_impl_var_arg(func->var, impl->func->args);
138 }
139 if (!func->var->arg && !func->ref) {
140 data->error(data, func->var->token, PSI_WARNING,
141 "Unknown variable '%s%s' of `let` statement"
142 " for cast '%s' of implementation '%s'",
143 *func->var->name == '$' ? "" : "$",
144 func->var->name, func->name, impl->func->name);
145 return 0;
146 }
147 if (!validate_let_func_type(data, func, impl)) {
148 return 0;
149 }
150 if (!validate_let_func_inner(data, func, let_var, impl)) {
151 return 0;
152 }
153 return 1;
154 }