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