build administrativa
[m6w6/ext-psi] / src / types / let_stmt.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 #include "data.h"
32
33 struct psi_let_stmt *psi_let_stmt_init(struct psi_let_exp *exp)
34 {
35 struct psi_let_stmt *let = pecalloc(1, sizeof(*let), 1);
36 let->exp = exp;
37
38 return let;
39 }
40
41 void psi_let_stmt_free(struct psi_let_stmt **stmt_ptr)
42 {
43 if (*stmt_ptr) {
44 struct psi_let_stmt *stmt = *stmt_ptr;
45
46 *stmt_ptr = NULL;
47 if (stmt->exp) {
48 psi_let_exp_free(&stmt->exp);
49 }
50 psi_token_free(&stmt->token);
51 free(stmt);
52 }
53 }
54
55 void psi_let_stmt_dump(struct psi_dump *dump, struct psi_let_stmt *let)
56 {
57 PSI_DUMP(dump, "\t%s ", let->exp->kind == PSI_LET_TMP ? "temp" : "let");
58 psi_let_exp_dump(dump, let->exp, 1, 1);
59 PSI_DUMP(dump, "\n");
60 }
61
62 bool psi_let_stmts_validate(struct psi_data *data, struct psi_validate_scope *scope)
63 {
64 size_t i = 0;
65 struct psi_let_stmt *let;
66
67 /* we can have multiple let stmts */
68 /* check that we have a decl arg and impl arg for every let stmt */
69 while (psi_plist_get(scope->impl->stmts.let, i++, &let)) {
70 struct psi_decl_var *let_var;
71 struct psi_impl_var *let_ivar = NULL;
72
73 if (let->exp->kind == PSI_LET_TMP) {
74 let_var = let->exp->data.var;
75 } else {
76 let_var = let->exp->var;
77 }
78
79 if (!let->exp->var) {
80 data->error(data, let->token, PSI_WARNING,
81 "Missing variable in `let` statement for implementation %s",
82 scope->impl->func->name->val);
83 return false;
84 }
85
86 if (!psi_impl_get_decl_arg(scope->impl, let_var)) {
87 data->error(data, let_var->token, PSI_WARNING,
88 "Unknown variable '%s' in `let` statement of implementation '%s'",
89 let_var->name->val, scope->impl->func->name->val);
90 return false;
91 }
92 switch (let->exp->kind) {
93 case PSI_LET_CALLBACK:
94 let_ivar = let->exp->data.callback->func->var;
95 break;
96 case PSI_LET_FUNC:
97 let_ivar = let->exp->data.func->var;
98 break;
99 default:
100 break;
101 }
102 if (let_ivar && !psi_impl_get_arg(scope->impl, let_ivar)) {
103 data->error(data, let_var->token, PSI_WARNING,
104 "Unknown variable '%s' in `let` statement of implementation '%s'",
105 let_ivar->name->val, scope->impl->func->name->val);
106 return false;
107 }
108
109 scope->current_let = let->exp;
110 if (!psi_let_exp_validate(data, let->exp, scope)) {
111 scope->current_let = NULL;
112 return false;
113 }
114 scope->current_let = NULL;
115 }
116 /* check that we have a let stmt for every decl arg */
117 if (scope->impl->decl->args) {
118 struct psi_decl_arg *darg;
119
120 for (i = 0; psi_plist_get(scope->impl->decl->args, i, &darg); ++i) {
121 if (!psi_impl_get_let(scope->impl, darg->var)) {
122 data->error(data, scope->impl->func->token, PSI_WARNING,
123 "Missing `let` statement for arg '%s %s%s'"
124 " of declaration '%s' for implementation '%s'",
125 darg->type->name->val,
126 psi_t_indirection(darg->var->pointer_level),
127 darg->var->name->val,
128 scope->impl->decl->func->var->name->val,
129 scope->impl->func->name->val);
130 return false;
131 }
132 }
133 }
134
135 return true;
136 }
137
138 void *psi_let_stmt_exec(struct psi_let_stmt *let, struct psi_call_frame *frame)
139 {
140 return psi_let_exp_exec(let->exp, let->exp->var->arg, NULL, 0, frame);
141 }