types refactoring
[m6w6/ext-psi] / src / types / return_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
32 #include <stdlib.h>
33 #include <stdio.h>
34
35 #include "data.h"
36
37 return_stmt *init_return_stmt(set_value *val) {
38 return_stmt *ret = calloc(1, sizeof(*ret));
39 ret->set = val;
40 return ret;
41 }
42
43 void free_return_stmt(return_stmt *ret) {
44 if (ret->token) {
45 free(ret->token);
46 }
47 free_set_value(ret->set);
48 free(ret);
49 }
50
51 void dump_return_stmt(int fd, return_stmt *ret) {
52 dprintf(fd, "\treturn ");
53 dump_set_value(fd, ret->set, 1, 0);
54 }
55
56 static inline decl *locate_impl_decl(decls *decls, return_stmt *ret) {
57 if (decls) {
58 size_t i;
59
60 for (i = 0; i < decls->count; ++i) {
61 if (!strcmp(decls->list[i]->func->var->name, ret->set->vars->vars[0]->name)) {
62 ret->decl = decls->list[i]->func;
63 return decls->list[i];
64 }
65 }
66 }
67
68 return NULL;
69 }
70
71 int validate_return_stmt(struct psi_data *data, impl *impl) {
72 return_stmt *ret;
73
74 /* we must have exactly one ret stmt delcaring the native func to call */
75 /* and which type cast to apply */
76 if (impl->stmts->ret.count != 1) {
77 if (impl->stmts->ret.count > 1) {
78 data->error(data, impl->stmts->ret.list[1]->token, PSI_WARNING,
79 "Too many `return` statements for implmentation %s;"
80 " found %zu, exactly one is needed",
81 impl->func->name, impl->stmts->ret.count);
82 } else {
83 data->error(data, impl->func->token, PSI_WARNING,
84 "Missing `return` statement for implementation %s",
85 impl->func->name);
86 }
87 return 0;
88 }
89
90 ret = impl->stmts->ret.list[0];
91
92 if (!(impl->decl = locate_impl_decl(data->decls, ret))) {
93 data->error(data, ret->token, PSI_WARNING,
94 "Missing declaration '%s' for `return` statment for implementation %s",
95 ret->set->vars->vars[0]->name, impl->func->name);
96 return 0;
97 }
98
99 if (!validate_set_value(data, ret->set, 1, &ret->decl, impl->decl->args ? (int) impl->decl->args->count : 0, impl->decl->args ? impl->decl->args->args : NULL, 0)) {
100 return 0;
101 }
102
103 //impl->decl->impl = impl;
104
105 return 1;
106 }