parser: fix parsing decls with anon params
[m6w6/ext-psi] / src / types / decl.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
28 #include <dlfcn.h>
29 #include "data.h"
30
31 #define PSI_FUNC_REDIRS
32 #include "php_psi_posix.h"
33
34 struct psi_decl *psi_decl_init(struct psi_decl_arg *func, struct psi_plist *args)
35 {
36 struct psi_decl *d = calloc(1, sizeof(*d));
37
38 d->func = func;
39 d->args = args;
40
41 return d;
42 }
43
44 void psi_decl_free(struct psi_decl **d_ptr)
45 {
46 if (*d_ptr) {
47 struct psi_decl *d = *d_ptr;
48
49 *d_ptr = NULL;
50
51 psi_decl_abi_free(&d->abi);
52 psi_decl_arg_free(&d->func);
53 if (d->args) {
54 psi_plist_free(d->args);
55 }
56 free(d);
57 }
58 }
59
60 void psi_decl_dump(int fd, struct psi_decl *decl)
61 {
62 psi_decl_abi_dump(fd, decl->abi);
63 dprintf(fd, " ");
64 psi_decl_arg_dump(fd, decl->func, 0);
65 dprintf(fd, "(");
66 if (decl->args) {
67 size_t i;
68 struct psi_decl_arg *arg;
69
70 for (i = 0; psi_plist_get(decl->args, i, &arg); ++i) {
71 if (i) {
72 dprintf(fd, ", ");
73 }
74 psi_decl_arg_dump(fd, arg, 0);
75 }
76 if (decl->varargs) {
77 dprintf(fd, ", ...");
78 }
79 }
80 dprintf(fd, ");");
81 }
82
83 static inline bool psi_decl_validate_func(struct psi_data *data,
84 struct psi_decl *decl, struct psi_decl_arg *func, void *dl)
85 {
86 struct psi_func_redir *redir;
87
88 if (!strcmp(func->var->name, "dlsym")) {
89 data->error(data, func->token, PSI_WARNING,
90 "Cannot dlsym dlsym (sic!)");
91 return false;
92 }
93
94 for (redir = &psi_func_redirs[0]; redir->name; ++redir) {
95 if (!strcmp(func->var->name, redir->name)) {
96 decl->sym = redir->func;
97 }
98 }
99 if (!decl->sym) {
100 #ifndef RTLD_NEXT
101 # define RTLD_NEXT ((void *) -1l)
102 #endif
103 #ifndef RTLD_DEFAULT
104 # define RTLD_DEFAULT ((void *) 0)
105 #endif
106 decl->sym = dlsym(dl ?: RTLD_DEFAULT, func->var->name);
107 if (!decl->sym) {
108 data->error(data, func->token, PSI_WARNING,
109 "Failed to locate symbol '%s': %s", func->var->name,
110 dlerror() ?: "not found");
111 return false;
112 }
113 }
114 return true;
115 }
116
117 bool psi_decl_validate(struct psi_data *data, struct psi_decl *decl, void *dl,
118 struct psi_validate_stack *type_stack)
119 {
120 if (!psi_decl_validate_nodl(data, decl, type_stack)) {
121 return false;
122 }
123 if (!psi_decl_validate_func(data, decl, decl->func, dl)) {
124 return false;
125 }
126
127 return true;
128 }
129
130 bool psi_decl_validate_nodl(struct psi_data *data, struct psi_decl *decl,
131 struct psi_validate_stack *type_stack)
132 {
133 if (!decl->abi) {
134 decl->abi = psi_decl_abi_init("default");
135 } else if (!psi_decl_abi_validate(data, decl->abi)) {
136 data->error(data, decl->abi->token, PSI_WARNING,
137 "Invalid calling convention: '%s'", decl->abi->token->text);
138 return false;
139 }
140 if (!psi_decl_arg_validate(data, decl->func, type_stack)) {
141 return false;
142 }
143 if (decl->args) {
144 size_t i = 0;
145 struct psi_decl_arg *arg;
146
147 while (psi_plist_get(decl->args, i++, &arg)) {
148 if (!arg->var->name) {
149 arg->var->name = malloc(7);
150 snprintf(arg->var->name, 6, "arg%zu", i);
151 arg->var->fqn = strdup(arg->var->name);
152 }
153 if (!psi_decl_arg_validate(data, arg, type_stack)) {
154 return false;
155 }
156 }
157 }
158
159 return true;
160 }