gdb smob cleanups
[deliverable/binutils-gdb.git] / gdb / guile / scm-iterator.c
1 /* Simple iterators for GDB/Scheme.
2
3 Copyright (C) 2014 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* See README file in this directory for implementation notes, coding
21 conventions, et.al. */
22
23 /* These are *simple* iterators, used to implement iterating over a collection
24 of objects. They are implemented as a smob containing three objects:
25
26 1) the object being iterated over,
27 2) an object to record the progress of the iteration,
28 3) a procedure of one argument (the iterator object) that returns the next
29 object in the iteration or a pre-determined end marker.
30
31 Simple example:
32
33 (define-public (make-list-iterator l end-marker)
34 "Return a <gdb:iterator> object for a list."
35 (let ((next! (lambda (iter)
36 (let ((l (iterator-progress iter)))
37 (if (eq? l '())
38 end-marker
39 (begin
40 (set-iterator-progress! iter (cdr l))
41 (car l)))))))
42 (make-iterator l l next!)))
43
44 (define l '(1 2))
45 (define i (make-list-iterator l #:eoi))
46 (iterator-next! i) -> 1
47 (iterator-next! i) -> 2
48 (iterator-next! i) -> #:eoi
49
50 There is SRFI 41, Streams. We might support that too eventually (not with
51 this interface of course). */
52
53 #include "defs.h"
54 #include "guile-internal.h"
55
56 /* A smob for iterating over something.
57 Typically this is used when computing a list of everything is
58 too expensive.
59 The typedef for this struct is in guile-internal.h. */
60
61 struct _iterator_smob
62 {
63 /* This always appears first. */
64 gdb_smob base;
65
66 /* The object being iterated over. */
67 SCM object;
68
69 /* An arbitrary object describing the progress of the iteration.
70 This is used by next_x to track progress. */
71 SCM progress;
72
73 /* A procedure of one argument, the iterator.
74 It returns the next object in the iteration.
75 How to signal "end of iteration" is up to next_x. */
76 SCM next_x;
77 };
78
79 static const char iterator_smob_name[] = "gdb:iterator";
80
81 /* The tag Guile knows the iterator smob by. */
82 static scm_t_bits iterator_smob_tag;
83
84 /* A unique-enough marker to denote "end of iteration". */
85 static SCM end_of_iteration;
86
87 const char *
88 itscm_iterator_smob_name (void)
89 {
90 return iterator_smob_name;
91 }
92
93 SCM
94 itscm_iterator_smob_object (iterator_smob *i_smob)
95 {
96 return i_smob->object;
97 }
98
99 SCM
100 itscm_iterator_smob_progress (iterator_smob *i_smob)
101 {
102 return i_smob->progress;
103 }
104
105 void
106 itscm_set_iterator_smob_progress_x (iterator_smob *i_smob, SCM progress)
107 {
108 i_smob->progress = progress;
109 }
110 \f
111 /* Administrivia for iterator smobs. */
112
113 /* The smob "mark" function for <gdb:iterator>. */
114
115 static SCM
116 itscm_mark_iterator_smob (SCM self)
117 {
118 iterator_smob *i_smob = (iterator_smob *) SCM_SMOB_DATA (self);
119
120 scm_gc_mark (i_smob->object);
121 scm_gc_mark (i_smob->progress);
122 return i_smob->next_x;
123 }
124
125 /* The smob "print" function for <gdb:iterator>. */
126
127 static int
128 itscm_print_iterator_smob (SCM self, SCM port, scm_print_state *pstate)
129 {
130 iterator_smob *i_smob = (iterator_smob *) SCM_SMOB_DATA (self);
131
132 gdbscm_printf (port, "#<%s ", iterator_smob_name);
133 scm_write (i_smob->object, port);
134 scm_puts (" ", port);
135 scm_write (i_smob->progress, port);
136 scm_puts (" ", port);
137 scm_write (i_smob->next_x, port);
138 scm_puts (">", port);
139
140 scm_remember_upto_here_1 (self);
141
142 /* Non-zero means success. */
143 return 1;
144 }
145
146 /* Low level routine to make a <gdb:iterator> object.
147 Caller must verify correctness of arguments.
148 No exceptions are thrown. */
149
150 static SCM
151 itscm_make_iterator_smob (SCM object, SCM progress, SCM next)
152 {
153 iterator_smob *i_smob = (iterator_smob *)
154 scm_gc_malloc (sizeof (iterator_smob), iterator_smob_name);
155 SCM i_scm;
156
157 i_smob->object = object;
158 i_smob->progress = progress;
159 i_smob->next_x = next;
160 i_scm = scm_new_smob (iterator_smob_tag, (scm_t_bits) i_smob);
161 gdbscm_init_gsmob (&i_smob->base);
162
163 return i_scm;
164 }
165
166 /* (make-iterator object object procedure) -> <gdb:iterator> */
167
168 SCM
169 gdbscm_make_iterator (SCM object, SCM progress, SCM next)
170 {
171 SCM i_scm;
172
173 SCM_ASSERT_TYPE (gdbscm_is_procedure (next), next, SCM_ARG3, FUNC_NAME,
174 _("procedure"));
175
176 i_scm = itscm_make_iterator_smob (object, progress, next);
177
178 return i_scm;
179 }
180
181 /* Return non-zero if SCM is a <gdb:iterator> object. */
182
183 int
184 itscm_is_iterator (SCM scm)
185 {
186 return SCM_SMOB_PREDICATE (iterator_smob_tag, scm);
187 }
188
189 /* (iterator? object) -> boolean */
190
191 static SCM
192 gdbscm_iterator_p (SCM scm)
193 {
194 return scm_from_bool (itscm_is_iterator (scm));
195 }
196
197 /* (end-of-iteration) -> an "end-of-iteration" marker
198 We rely on this not being used as a data result of an iterator. */
199
200 SCM
201 gdbscm_end_of_iteration (void)
202 {
203 return end_of_iteration;
204 }
205
206 /* Return non-zero if OBJ is the end-of-iteration marker. */
207
208 int
209 itscm_is_end_of_iteration (SCM obj)
210 {
211 return scm_is_eq (obj, end_of_iteration);
212 }
213
214 /* (end-of-iteration? obj) -> boolean */
215
216 static SCM
217 gdbscm_end_of_iteration_p (SCM obj)
218 {
219 return scm_from_bool (itscm_is_end_of_iteration (obj));
220 }
221
222 /* Call the next! method on ITER, which must be a <gdb:iterator> object.
223 Returns a <gdb:exception> object if an exception is thrown.
224 OK_EXCPS is passed to gdbscm_safe_call_1. */
225
226 SCM
227 itscm_safe_call_next_x (SCM iter, excp_matcher_func *ok_excps)
228 {
229 iterator_smob *i_smob;
230
231 gdb_assert (itscm_is_iterator (iter));
232
233 i_smob = (iterator_smob *) SCM_SMOB_DATA (iter);
234 return gdbscm_safe_call_1 (i_smob->next_x, iter, ok_excps);
235 }
236 \f
237 /* Iterator methods. */
238
239 /* Returns the <gdb:iterator> smob in SELF.
240 Throws an exception if SELF is not an iterator smob. */
241
242 SCM
243 itscm_get_iterator_arg_unsafe (SCM self, int arg_pos, const char *func_name)
244 {
245 SCM_ASSERT_TYPE (itscm_is_iterator (self), self, arg_pos, func_name,
246 iterator_smob_name);
247
248 return self;
249 }
250
251 /* (iterator-object <gdb:iterator>) -> object */
252
253 static SCM
254 gdbscm_iterator_object (SCM self)
255 {
256 SCM i_scm;
257 iterator_smob *i_smob;
258
259 i_scm = itscm_get_iterator_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
260 i_smob = (iterator_smob *) SCM_SMOB_DATA (i_scm);
261
262 return i_smob->object;
263 }
264
265 /* (iterator-progress <gdb:iterator>) -> object */
266
267 static SCM
268 gdbscm_iterator_progress (SCM self)
269 {
270 SCM i_scm;
271 iterator_smob *i_smob;
272
273 i_scm = itscm_get_iterator_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
274 i_smob = (iterator_smob *) SCM_SMOB_DATA (i_scm);
275
276 return i_smob->progress;
277 }
278
279 /* (set-iterator-progress! <gdb:iterator> object) -> unspecified */
280
281 static SCM
282 gdbscm_set_iterator_progress_x (SCM self, SCM value)
283 {
284 SCM i_scm;
285 iterator_smob *i_smob;
286
287 i_scm = itscm_get_iterator_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
288 i_smob = (iterator_smob *) SCM_SMOB_DATA (i_scm);
289
290 i_smob->progress = value;
291 return SCM_UNSPECIFIED;
292 }
293
294 /* (iterator-next! <gdb:iterator>) -> object
295 The result is the next value in the iteration or some "end" marker.
296 It is up to each iterator's next! function to specify what its end
297 marker is. */
298
299 static SCM
300 gdbscm_iterator_next_x (SCM self)
301 {
302 SCM i_scm;
303 iterator_smob *i_smob;
304
305 i_scm = itscm_get_iterator_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
306 i_smob = (iterator_smob *) SCM_SMOB_DATA (i_scm);
307 /* We leave type-checking of the procedure to gdbscm_safe_call_1. */
308
309 return gdbscm_safe_call_1 (i_smob->next_x, self, NULL);
310 }
311 \f
312 /* Initialize the Scheme iterator code. */
313
314 static const scheme_function iterator_functions[] =
315 {
316 { "make-iterator", 3, 0, 0, gdbscm_make_iterator,
317 "\
318 Create a <gdb:iterator> object.\n\
319 \n\
320 Arguments: object progress next!\n\
321 object: The object to iterate over.\n\
322 progress: An object to use to track progress of the iteration.\n\
323 next!: A procedure of one argument, the iterator.\n\
324 Returns the next element in the iteration or an implementation-chosen\n\
325 value to signify iteration is complete.\n\
326 By convention end-of-iteration should be marked with (end-of-iteration)\n\
327 from module (gdb iterator)." },
328
329 { "iterator?", 1, 0, 0, gdbscm_iterator_p,
330 "\
331 Return #t if the object is a <gdb:iterator> object." },
332
333 { "iterator-object", 1, 0, 0, gdbscm_iterator_object,
334 "\
335 Return the object being iterated over." },
336
337 { "iterator-progress", 1, 0, 0, gdbscm_iterator_progress,
338 "\
339 Return the progress object of the iterator." },
340
341 { "set-iterator-progress!", 2, 0, 0, gdbscm_set_iterator_progress_x,
342 "\
343 Set the progress object of the iterator." },
344
345 { "iterator-next!", 1, 0, 0, gdbscm_iterator_next_x,
346 "\
347 Invoke the next! procedure of the iterator and return its result." },
348
349 { "end-of-iteration", 0, 0, 0, gdbscm_end_of_iteration,
350 "\
351 Return the end-of-iteration marker." },
352
353 { "end-of-iteration?", 1, 0, 0, gdbscm_end_of_iteration_p,
354 "\
355 Return #t if the object is the end-of-iteration marker." },
356
357 END_FUNCTIONS
358 };
359
360 void
361 gdbscm_initialize_iterators (void)
362 {
363 iterator_smob_tag = gdbscm_make_smob_type (iterator_smob_name,
364 sizeof (iterator_smob));
365 scm_set_smob_mark (iterator_smob_tag, itscm_mark_iterator_smob);
366 scm_set_smob_print (iterator_smob_tag, itscm_print_iterator_smob);
367
368 gdbscm_define_functions (iterator_functions, 1);
369
370 /* We can make this more unique if it's necessary,
371 but this is good enough for now. */
372 end_of_iteration = scm_from_latin1_keyword ("end-of-iteration");
373 }
This page took 0.040895 seconds and 4 git commands to generate.