/
p6invocation.pmc
287 lines (239 loc) Β· 11.3 KB
/
p6invocation.pmc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
$Id$
Copyright (C) 2009, The Perl Foundation.
=head1 NAME
src/pmc/P6Invocation.pmc - PMC representing a current invocation
=head1 DESCRIPTION
When we invoke a method in Perl 6, we need to keep the list of candidates
available for callwith, callsame, nextwith, nextsame etc. This PMC is what
gets stuck into the lex pad to represent the the candidate list.
=head2 Methods
=cut
*/
#include "parrot/parrot.h"
#include "pmc_perl6multisub.h"
/* Selects whether we're doing a real dispatch or just a test to see if
* we could dispatch. */
#define P6I_MODE_DISPATCH 0
#define P6I_MODE_CHECK 1
/* Flag on the PMC to say whether we're in quiet failure mode (if set)
* or exception mode if we are unable to dispatch. */
#define PObj_P6I_FAILURE_MODE_FLAG PObj_private0_FLAG
/* Declaration; definition of this is in p6opaque.pmc. */
PMC *look_for_method(PARROT_INTERP, PMC *search_list, INTVAL *start_pos, STRING *name);
/* Another declaration; this one is defined in perl6multisub.pmc. */
PMC *get_all_candidates_with_cur_args(PARROT_INTERP, PMC *self);
/* This does the grunt work of working out what the next candidate is. Takes
* account of us maybe needing to look into multi variants and all that lot. */
static PMC *get_next_candidate(PARROT_INTERP, PMC *SELF, int check_only) {
PMC *candidates, *current, *search_list;
STRING *name;
INTVAL position, resume_point;
/* Get candidates and position. */
GETATTR_P6Invocation_candidate_list(interp, SELF, candidates);
GETATTR_P6Invocation_position(interp, SELF, position);
/* Make sure we're not past the end of the candidate list. */
check_and_continue_search:
if (PMC_IS_NULL(candidates) || position >= VTABLE_elements(interp, candidates)) {
/* Past the end; see if we've got a way to resume the search. */
GETATTR_P6Invocation_search_list(interp, SELF, search_list);
GETATTR_P6Invocation_name(interp, SELF, name);
GETATTR_P6Invocation_resume_point(interp, SELF, resume_point);
if (!PMC_IS_NULL(search_list) && name) {
/* We can look for more candidates. Try to do so. */
PMC *found = look_for_method(interp, search_list, &resume_point, name);
SETATTR_P6Invocation_resume_point(interp, SELF, resume_point);
if (PMC_IS_NULL(found))
return PMCNULL;
/* If we get here, we have more. Push this next candidate onto the
* candidate list, creating it in the process if needed. */
if (PMC_IS_NULL(candidates)) {
candidates = pmc_new(interp, enum_class_ResizablePMCArray);
SETATTR_P6Invocation_candidate_list(interp, SELF, candidates);
}
VTABLE_push_pmc(interp, candidates, found);
}
else {
/* No way; give up. */
return PMCNULL;
}
}
/* Grab current candidate. */
current = VTABLE_get_pmc_keyed_int(interp, candidates, position);
if (VTABLE_isa(interp, current, CONST_STRING(interp, "Perl6MultiSub"))) {
/* Multi. Ask the multi-dispatcher for all possible variants that we
* could call with the current argument, unless we have none in
* which we're just gonna have to leave the multi here in the list. */
if (interp->current_args) {
PMC *possibles = get_all_candidates_with_cur_args(interp, current);
if (VTABLE_elements(interp, possibles) == 0) {
/* No candidates here; increment the position, and then jump back
* to check if we need to find another candidate and try again or
* if the list is already containing more things for us to try. */
position++;
SETATTR_P6Invocation_position(interp, SELF, position);
goto check_and_continue_search;
}
/* If we get here, we have some possibles; splice them into the candidate
* list, then continue as normal. This means that deferal will take into
* account the other multi-variants. Then our current becomes the first
* of the multi candidates. */
VTABLE_splice(interp, candidates, possibles, position, 1);
current = VTABLE_get_pmc_keyed_int(interp, candidates, position);
}
}
/* Increment position in candidate list if we're in check mode, and we're done. */
if (!check_only)
position++;
SETATTR_P6Invocation_position(interp, SELF, position);
return current;
}
pmclass P6Invocation need_ext dynpmc group perl6_group {
ATTR PMC *first_candidate;
ATTR PMC *candidate_list;
ATTR INTVAL position;
ATTR STRING *name;
ATTR PMC *search_list;
ATTR INTVAL resume_point;
VTABLE void init() {
PMC_data(SELF) = mem_allocate_zeroed_typed(Parrot_P6Invocation_attributes);
PObj_custom_mark_SET(SELF);
PObj_active_destroy_SET(SELF);
}
VTABLE void init_pmc(PMC *list) {
SELF.init();
SETATTR_P6Invocation_candidate_list(interp, SELF, list);
}
VTABLE void mark() {
if (PMC_data(SELF)) {
PMC *first_candidate, *candidate_list, *search_list;
STRING *name;
GETATTR_P6Invocation_first_candidate(interp, SELF, first_candidate);
GETATTR_P6Invocation_candidate_list(interp, SELF, candidate_list);
GETATTR_P6Invocation_search_list(interp, SELF, search_list);
GETATTR_P6Invocation_name(interp, SELF, name);
if (!PMC_IS_NULL(first_candidate))
Parrot_gc_mark_PObj_alive(interp, (PObj*)first_candidate);
if (!PMC_IS_NULL(candidate_list))
Parrot_gc_mark_PObj_alive(interp, (PObj*)candidate_list);
if (!PMC_IS_NULL(search_list))
Parrot_gc_mark_PObj_alive(interp, (PObj*)search_list);
if (name)
Parrot_gc_mark_PObj_alive(interp, (PObj*)name);
}
}
VTABLE void destroy() {
mem_sys_free(PMC_data(SELF));
PMC_data(SELF) = NULL;
}
VTABLE PMC *clone() {
PMC *first_candidate, *candidate_list, *search_list;
STRING *name;
INTVAL position, resume_point;
PMC *copy = pmc_new(interp, SELF->vtable->base_type);
GETATTR_P6Invocation_first_candidate(interp, SELF, first_candidate);
GETATTR_P6Invocation_candidate_list(interp, SELF, candidate_list);
GETATTR_P6Invocation_position(interp, SELF, position);
GETATTR_P6Invocation_search_list(interp, SELF, search_list);
GETATTR_P6Invocation_name(interp, SELF, name);
GETATTR_P6Invocation_resume_point(interp, SELF, resume_point);
SETATTR_P6Invocation_first_candidate(interp, copy, first_candidate);
SETATTR_P6Invocation_candidate_list(interp, copy, candidate_list);
SETATTR_P6Invocation_position(interp, copy, position);
SETATTR_P6Invocation_search_list(interp, copy, search_list);
SETATTR_P6Invocation_name(interp, copy, name);
SETATTR_P6Invocation_resume_point(interp, copy, resume_point);
if (PObj_flag_TEST(P6I_FAILURE_MODE, SELF))
PObj_flag_SET(P6I_FAILURE_MODE, copy);
return copy;
}
VTABLE INTVAL get_bool() {
PMC *first_candidate, *candidates;
INTVAL position;
/* First, check if we have a first candidate. */
GETATTR_P6Invocation_first_candidate(interp, SELF, first_candidate);
if (!PMC_IS_NULL(first_candidate))
return 1;
/* Get candidates and position, and check if we have more candidates
* already known about. */
GETATTR_P6Invocation_candidate_list(interp, SELF, candidates);
GETATTR_P6Invocation_position(interp, SELF, position);
if (!PMC_IS_NULL(candidates) && position < VTABLE_elements(interp, candidates))
return 1;
/* If not, then we see if the dispatcher can potentially find more. */
return !PMC_IS_NULL(get_next_candidate(interp, SELF, P6I_MODE_CHECK));
}
VTABLE INTVAL get_integer() {
return VTABLE_get_bool(interp, SELF);
}
VTABLE PMC *get_pmc() {
PMC *first_candidate;
PMC *clone = VTABLE_clone(interp, SELF);
GETATTR_P6Invocation_first_candidate(interp, clone, first_candidate);
if (PMC_IS_NULL(first_candidate))
first_candidate = get_next_candidate(interp, clone, P6I_MODE_CHECK);
return first_candidate;
}
VTABLE PMC *get_iter() {
return VTABLE_clone(interp, SELF);
}
VTABLE opcode_t *invoke(void *next) {
STRING *lexname = CONST_STRING(interp, "__CANDIDATE_LIST__");
PMC *lexpad, *first_candidate;
opcode_t *addr;
/* In the straightforward case, we know our first candidate right off the
* bat; if not, use list. We also nullify first candidate so we hit the
* candidate list next time we're used. */
GETATTR_P6Invocation_first_candidate(interp, SELF, first_candidate);
if (PMC_IS_NULL(first_candidate))
first_candidate = get_next_candidate(interp, SELF, P6I_MODE_DISPATCH);
else
SETATTR_P6Invocation_first_candidate(interp, SELF, PMCNULL);
/* Invoke it, then fudge ourself into its lexpad. */
if (PMC_IS_NULL(first_candidate)) {
PMC *ns;
/* Oh noes, no candidate. If we aren't in soft-fail mode, then
* throw an exception. */
if (!PObj_flag_TEST(P6I_FAILURE_MODE, SELF))
Parrot_ex_throw_from_c_args(interp, next, 1, "No candidates found to invoke");
/* Otherwise, we look up something that when invoked will just give a
* dispatch failure. */
ns = Parrot_get_namespace_keyed_str(interp, interp->root_namespace, CONST_STRING(interp, "perl6"));
first_candidate = VTABLE_get_pmc_keyed_str(interp, ns, CONST_STRING(interp, "!deferal_fail"));
}
addr = VTABLE_invoke(interp, first_candidate, next);
lexpad = Parrot_pcc_get_lex_pad(INTERP, CURRENT_CONTEXT(interp));
if (!PMC_IS_NULL(lexpad) && VTABLE_exists_keyed_str(interp, lexpad, lexname))
VTABLE_set_pmc_keyed_str(interp, lexpad, lexname, SELF);
return addr;
}
VTABLE PMC *shift_pmc() {
/* Result is a clone of ourself. We clone ourself rather than handing
* back the method so invocations of it that defer will work out. */
PMC *result = VTABLE_clone(interp, SELF);
/* Now shuffle along one candidate. */
PMC *first_candidate;
GETATTR_P6Invocation_first_candidate(interp, SELF, first_candidate);
if (PMC_IS_NULL(first_candidate))
get_next_candidate(interp, SELF, P6I_MODE_DISPATCH);
else
SETATTR_P6Invocation_first_candidate(interp, SELF, PMCNULL);
return result;
}
METHOD PMC *get() {
PMC *result = VTABLE_shift_pmc(interp, SELF);
RETURN(PMC *result);
}
METHOD void trim_candidate_list() {
/* Just blank out everything we could use to find another
* method. */
SETATTR_P6Invocation_candidate_list(interp, SELF, PMCNULL);
SETATTR_P6Invocation_position(interp, SELF, 0);
SETATTR_P6Invocation_name(interp, SELF, NULL);
SETATTR_P6Invocation_search_list(interp, SELF, PMCNULL);
SETATTR_P6Invocation_resume_point(interp, SELF, 0);
}
METHOD void set_failure_mode() {
PObj_flag_SET(P6I_FAILURE_MODE, SELF);
}
}