1
|
1 // Symbol table classes. -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1992, 1993 John W. Eaton |
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef __GNUG__ |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
|
28 // Don't even think about moving the tree.h include to symtab.h... |
|
29 |
|
30 #include "symtab.h" |
|
31 #include "error.h" |
|
32 #include "variables.h" |
|
33 #include "utils.h" |
|
34 #include "tree.h" |
|
35 #include "tree-const.h" |
|
36 |
|
37 /* |
|
38 * Variables and functions. |
|
39 */ |
|
40 symbol_def::symbol_def (void) |
|
41 { |
|
42 help_string = (char *) NULL; |
|
43 type = unknown_type; |
|
44 lifespan = temporary; |
|
45 sym_class = read_write; |
|
46 definition = (tree *) NULL; |
|
47 } |
|
48 |
|
49 symbol_def::symbol_def (tree_constant *t) |
|
50 { |
|
51 help_string = (char *) NULL; |
|
52 type = variable; |
|
53 lifespan = temporary; |
|
54 sym_class = read_write; |
|
55 definition = t; |
|
56 } |
|
57 |
|
58 symbol_def::symbol_def (tree_builtin *t) |
|
59 { |
|
60 help_string = (char *) NULL; |
|
61 type = builtin_function; |
|
62 lifespan = temporary; |
|
63 sym_class = read_write; |
|
64 definition = t; |
|
65 } |
|
66 |
|
67 symbol_def::symbol_def (tree_function *t) |
|
68 { |
|
69 help_string = (char *) NULL; |
|
70 type = user_function; |
|
71 lifespan = temporary; |
|
72 sym_class = read_write; |
|
73 definition = t; |
|
74 } |
|
75 |
|
76 symbol_def::~symbol_def (void) |
|
77 { |
|
78 delete [] help_string; |
|
79 delete definition; |
|
80 } |
|
81 |
|
82 void |
|
83 symbol_def::define (tree_constant *t) |
|
84 { |
|
85 definition = t; |
|
86 type = variable; |
|
87 } |
|
88 |
|
89 void |
|
90 symbol_def::define (tree_builtin *t) |
|
91 { |
|
92 definition = t; |
|
93 type = builtin_function; |
|
94 } |
|
95 |
|
96 void |
|
97 symbol_def::define (tree_function *t) |
|
98 { |
|
99 definition = t; |
|
100 type = user_function; |
|
101 } |
|
102 |
|
103 tree * |
164
|
104 symbol_def::def (void) const |
1
|
105 { |
|
106 return definition; |
|
107 } |
|
108 |
|
109 char * |
164
|
110 symbol_def::help (void) const |
1
|
111 { |
|
112 return help_string; |
|
113 } |
|
114 |
|
115 void |
164
|
116 symbol_def::document (const char *h) |
1
|
117 { |
|
118 delete [] help_string; |
|
119 help_string = strsave (h); |
|
120 } |
|
121 |
|
122 int |
|
123 symbol_def::save (ostream& os, int mark_as_global) |
|
124 { |
|
125 return definition->save (os, mark_as_global); |
|
126 } |
|
127 |
|
128 /* |
|
129 * Individual records in a symbol table. |
|
130 */ |
|
131 symbol_record::symbol_record (void) |
|
132 { |
|
133 nm = (char *) NULL; |
|
134 formal_param = 0; |
122
|
135 forced_global = 0; |
1
|
136 var = (symbol_def *) NULL; |
|
137 fcn = (symbol_def *) NULL; |
|
138 sv_fcn = (sv_Function) NULL; |
|
139 next_elem = (symbol_record *) NULL; |
|
140 } |
|
141 |
164
|
142 symbol_record::symbol_record (const char *n) |
1
|
143 { |
|
144 nm = strsave (n); |
|
145 formal_param = 0; |
122
|
146 forced_global = 0; |
1
|
147 var = (symbol_def *) NULL; |
|
148 fcn = (symbol_def *) NULL; |
|
149 sv_fcn = (sv_Function) NULL; |
|
150 next_elem = (symbol_record *) NULL; |
|
151 } |
|
152 |
164
|
153 symbol_record::symbol_record (const char *n, symbol_record *nxt) |
1
|
154 { |
|
155 nm = strsave (n); |
|
156 formal_param = 0; |
122
|
157 forced_global = 0; |
1
|
158 var = (symbol_def *) NULL; |
|
159 fcn = (symbol_def *) NULL; |
|
160 sv_fcn = (sv_Function) NULL; |
|
161 next_elem = nxt; |
|
162 } |
|
163 |
|
164 symbol_record::~symbol_record (void) |
|
165 { |
|
166 delete [] nm; |
|
167 |
|
168 if (var != (symbol_def *) NULL && --var->count <= 0) |
|
169 delete var; |
|
170 |
|
171 if (fcn != (symbol_def *) NULL && --fcn->count <= 0) |
|
172 delete fcn; |
|
173 } |
|
174 |
|
175 char * |
164
|
176 symbol_record::name (void) const |
1
|
177 { |
|
178 return nm; |
|
179 } |
|
180 |
|
181 char * |
164
|
182 symbol_record::help (void) const |
1
|
183 { |
|
184 if (var != (symbol_def *) NULL) |
|
185 return var->help (); |
|
186 else if (fcn != (symbol_def *) NULL) |
|
187 return fcn->help (); |
|
188 else |
|
189 return (char *) NULL; |
|
190 } |
|
191 |
|
192 tree * |
164
|
193 symbol_record::def (void) const |
1
|
194 { |
|
195 if (var != (symbol_def *) NULL) |
|
196 return var->def (); |
|
197 else if (fcn != (symbol_def *) NULL) |
|
198 return fcn->def (); |
|
199 else |
|
200 return (tree *) NULL; |
|
201 } |
|
202 |
|
203 int |
164
|
204 symbol_record::is_function (void) const |
1
|
205 { |
|
206 return (var == (symbol_def *) NULL && fcn != (symbol_def *) NULL); |
|
207 } |
|
208 |
|
209 int |
164
|
210 symbol_record::is_variable (void) const |
1
|
211 { |
|
212 return (var != (symbol_def *) NULL); |
|
213 } |
|
214 |
|
215 int |
164
|
216 symbol_record::is_defined (void) const |
1
|
217 { |
|
218 return (var != (symbol_def *) NULL || fcn != (symbol_def *) NULL); |
|
219 } |
|
220 |
|
221 void |
|
222 symbol_record::set_sv_function (sv_Function f) |
|
223 { |
|
224 sv_fcn = f; |
|
225 } |
|
226 |
|
227 int |
|
228 symbol_record::var_read_only (void) |
|
229 { |
|
230 if (var != (symbol_def *) NULL |
|
231 && var->sym_class == symbol_def::read_only) |
|
232 { |
|
233 error ("can't assign to read only symbol `%s'", nm); |
|
234 return 1; |
|
235 } |
|
236 else |
|
237 return 0; |
|
238 } |
|
239 |
|
240 int |
|
241 symbol_record::read_only (void) |
|
242 { |
|
243 if ((var != (symbol_def *) NULL |
|
244 && var->sym_class == symbol_def::read_only) |
|
245 || (fcn != (symbol_def *) NULL |
|
246 && fcn->sym_class == symbol_def::read_only)) |
|
247 { |
|
248 error ("can't assign to read only symbol `%s'", nm); |
|
249 return 1; |
|
250 } |
|
251 else |
|
252 return 0; |
|
253 } |
|
254 |
|
255 int |
|
256 symbol_record::define (tree_constant *t) |
|
257 { |
|
258 if (var_read_only ()) |
|
259 return 0; |
|
260 |
|
261 tree_constant *saved_def = NULL_TREE_CONST; |
|
262 |
|
263 if (var != (symbol_def *) NULL) |
|
264 { |
|
265 saved_def = (tree_constant *) var->def (); // XXX FIXME XXX |
|
266 var->define (t); |
|
267 } |
|
268 else |
|
269 { |
|
270 var = new symbol_def (t); |
|
271 var->count = 1; |
|
272 } |
|
273 |
|
274 if (sv_fcn != (sv_Function) NULL && sv_fcn () < 0) |
|
275 { |
|
276 var->define (saved_def); |
|
277 delete t; |
|
278 return 0; |
|
279 } |
|
280 |
|
281 delete saved_def; |
|
282 |
|
283 return 1; |
|
284 } |
|
285 |
|
286 int |
|
287 symbol_record::define (tree_builtin *t) |
|
288 { |
|
289 if (read_only ()) |
|
290 return 0; |
|
291 |
|
292 if (var != (symbol_def *) NULL) |
|
293 { |
|
294 if (--var->count <= 0) |
|
295 delete var; |
|
296 var = (symbol_def *) NULL; |
|
297 } |
|
298 |
|
299 if (fcn != (symbol_def *) NULL) |
|
300 fcn->define (t); |
|
301 else |
|
302 { |
|
303 fcn = new symbol_def (t); |
|
304 fcn->count = 1; |
|
305 } |
|
306 |
|
307 return 1; |
|
308 } |
|
309 |
|
310 int |
|
311 symbol_record::define (tree_function *t) |
|
312 { |
|
313 if (read_only ()) |
|
314 return 0; |
|
315 |
|
316 if (var != (symbol_def *) NULL) |
|
317 { |
|
318 if (--var->count <= 0) |
|
319 delete var; |
|
320 var = (symbol_def *) NULL; |
|
321 } |
|
322 |
|
323 if (fcn != (symbol_def *) NULL) |
|
324 fcn->define (t); |
|
325 else |
|
326 { |
|
327 fcn = new symbol_def (t); |
|
328 fcn->count = 1; |
|
329 } |
|
330 |
|
331 return 1; |
|
332 } |
|
333 |
|
334 int |
|
335 symbol_record::define_as_fcn (tree_constant *t) |
|
336 { |
|
337 if (read_only ()) |
|
338 return 0; |
|
339 |
|
340 if (var != (symbol_def *) NULL) |
|
341 { |
|
342 if (--var->count <= 0) |
|
343 delete var; |
|
344 var = (symbol_def *) NULL; |
|
345 } |
|
346 |
|
347 if (fcn != (symbol_def *) NULL) |
|
348 fcn->define (t); |
|
349 else |
|
350 { |
|
351 fcn = new symbol_def (t); |
|
352 fcn->count = 1; |
|
353 } |
|
354 |
|
355 return 1; |
|
356 } |
|
357 |
|
358 void |
164
|
359 symbol_record::document (const char *h) |
1
|
360 { |
|
361 if (var != (symbol_def *) NULL) |
|
362 var->document (h); |
|
363 else if (fcn != (symbol_def *) NULL) |
|
364 fcn->document (h); |
|
365 else |
|
366 warning ("couldn't document undefined variable `%s'", nm); |
|
367 } |
|
368 |
|
369 void |
|
370 symbol_record::protect (void) |
|
371 { |
|
372 if (var != (symbol_def *) NULL) |
|
373 var->sym_class = symbol_def::read_only; |
|
374 else if (fcn != (symbol_def *) NULL) |
|
375 fcn->sym_class = symbol_def::read_only; |
|
376 else |
|
377 warning ("couldn't protect undefined variable `%s'", nm); |
|
378 } |
|
379 |
|
380 void |
|
381 symbol_record::unprotect (void) |
|
382 { |
|
383 if (var != (symbol_def *) NULL) |
|
384 var->sym_class = symbol_def::read_write; |
|
385 else if (fcn != (symbol_def *) NULL) |
|
386 fcn->sym_class = symbol_def::read_write; |
|
387 } |
|
388 |
|
389 void |
|
390 symbol_record::make_eternal (void) |
|
391 { |
|
392 if (var != (symbol_def *) NULL) |
|
393 var->lifespan = symbol_def::eternal; |
|
394 else if (fcn != (symbol_def *) NULL) |
|
395 fcn->lifespan = symbol_def::eternal; |
|
396 else |
|
397 warning ("couldn't give eternal life to the variable `%s'", nm); |
|
398 } |
|
399 |
|
400 int |
|
401 symbol_record::save (ostream& os, int mark_as_global = 0) |
|
402 { |
|
403 int status = 0; |
|
404 |
|
405 if (var != (symbol_def *) NULL && var->def () != (tree *) NULL) |
|
406 { |
|
407 // For now, eternal implies builtin. |
|
408 if (var->lifespan != symbol_def::eternal) |
|
409 { |
|
410 // Should we also save the help string? Maybe someday. |
|
411 os << "# name: " << nm << "\n"; |
|
412 status = var->save (os, mark_as_global); |
|
413 } |
|
414 } |
|
415 else if (fcn != (symbol_def *) NULL) |
|
416 message ("save", "sorry, can't save functions yet"); |
|
417 else |
111
|
418 { |
|
419 // Kludge! We probably don't want to print warnings for ans, but it |
|
420 // does seem reasonable to print them for other undefined variables. |
|
421 if (strcmp (nm, "ans") != 0) |
|
422 warning ("not saving undefined symbol `%s'", nm); |
|
423 } |
1
|
424 |
|
425 return status; |
|
426 } |
|
427 |
|
428 void |
|
429 symbol_record::clear_visible (void) |
|
430 { |
|
431 if (var != (symbol_def *) NULL && var->lifespan != symbol_def::eternal) |
|
432 { |
|
433 if (--var->count <= 0) |
|
434 delete var; |
|
435 var = (symbol_def *) NULL; |
122
|
436 forced_global = 0; |
1
|
437 } |
|
438 else if (fcn != (symbol_def *) NULL && fcn->lifespan != symbol_def::eternal) |
|
439 { |
|
440 if (--fcn->count <= 0) |
|
441 delete fcn; |
|
442 fcn = (symbol_def *) NULL; |
|
443 } |
|
444 } |
|
445 |
|
446 void |
|
447 symbol_record::clear_all (void) |
|
448 { |
|
449 if (var != (symbol_def *) NULL && var->lifespan != symbol_def::eternal) |
|
450 { |
|
451 if (--var->count <= 0) |
|
452 delete var; |
|
453 var = (symbol_def *) NULL; |
122
|
454 forced_global = 0; |
1
|
455 } |
|
456 |
|
457 if (fcn != (symbol_def *) NULL && fcn->lifespan != symbol_def::eternal) |
|
458 { |
|
459 if (--fcn->count <= 0) |
|
460 delete fcn; |
|
461 fcn = (symbol_def *) NULL; |
|
462 } |
|
463 } |
|
464 |
|
465 void |
|
466 symbol_record::undefine (void) |
|
467 { |
|
468 if (var != (symbol_def *) NULL) |
|
469 { |
|
470 if (--var->count <= 0) |
|
471 delete var; |
|
472 var = (symbol_def *) NULL; |
|
473 } |
|
474 |
|
475 if (fcn != (symbol_def *) NULL) |
|
476 { |
|
477 if (--fcn->count <= 0) |
|
478 delete fcn; |
|
479 fcn = (symbol_def *) NULL; |
|
480 } |
|
481 } |
|
482 |
|
483 void |
|
484 symbol_record::mark_as_formal_parameter (void) |
|
485 { |
|
486 formal_param = 1; |
|
487 } |
|
488 |
|
489 int |
164
|
490 symbol_record::is_formal_parameter (void) const |
1
|
491 { |
|
492 return formal_param; |
|
493 } |
|
494 |
|
495 void |
122
|
496 symbol_record::mark_as_forced_global (void) |
|
497 { |
|
498 forced_global = 1; |
|
499 } |
|
500 |
|
501 int |
164
|
502 symbol_record::is_forced_global (void) const |
122
|
503 { |
|
504 return forced_global; |
|
505 } |
|
506 |
|
507 void |
1
|
508 symbol_record::alias (symbol_record *s, int force = 0) |
|
509 { |
|
510 sv_fcn = s->sv_fcn; // Maybe this should go in the var symbol_def? |
|
511 |
|
512 formal_param = s->formal_param; // Hmm. |
122
|
513 forced_global = s->forced_global; // Hmm. |
1
|
514 |
|
515 if (force && s->var == (symbol_def *) NULL |
|
516 && s->fcn == (symbol_def *) NULL) |
|
517 { |
|
518 s->var = new symbol_def (); |
|
519 var = s->var; |
|
520 var->count = 2; // Yes, this is correct. |
|
521 return; |
|
522 } |
|
523 |
|
524 if (s->var != (symbol_def *) NULL) |
|
525 { |
|
526 var = s->var; |
|
527 var->count++; |
|
528 } |
|
529 else if (s->fcn != (symbol_def *) NULL) |
|
530 { |
|
531 fcn = s->fcn; |
|
532 fcn->count++; |
|
533 } |
|
534 } |
|
535 |
|
536 symbol_record * |
164
|
537 symbol_record::next (void) const |
1
|
538 { |
|
539 return next_elem; |
|
540 } |
|
541 |
|
542 /* |
|
543 * A symbol table. |
|
544 */ |
|
545 |
|
546 symbol_table::symbol_table (void) |
|
547 { |
|
548 } |
|
549 |
|
550 symbol_record * |
164
|
551 symbol_table::lookup (const char *nm, int insert = 0, int warn = 0) |
1
|
552 { |
|
553 int index = hash (nm) & HASH_MASK; |
|
554 |
|
555 symbol_record *ptr = table[index].next (); |
|
556 |
|
557 while (ptr != (symbol_record *) NULL) |
|
558 { |
|
559 if (strcmp (ptr->name (), nm) == 0) |
|
560 return ptr; |
|
561 ptr = ptr->next (); |
|
562 } |
|
563 |
|
564 if (insert) |
|
565 { |
|
566 symbol_record *new_sym; |
|
567 new_sym = new symbol_record (nm, table[index].next ()); |
|
568 table[index].next_elem = new_sym; |
|
569 return new_sym; |
|
570 } |
|
571 else if (warn) |
|
572 message ("lookup", "symbol`%s' not found", nm); |
|
573 |
|
574 return (symbol_record *) NULL; |
|
575 } |
|
576 |
|
577 void |
|
578 symbol_table::clear (void) |
|
579 { |
|
580 for (int i = 0; i < HASH_TABLE_SIZE; i++) |
|
581 { |
|
582 symbol_record *prev = &table[i]; |
|
583 symbol_record *curr = prev->next (); |
|
584 |
|
585 while (curr != (symbol_record *) NULL) |
|
586 { |
|
587 curr->clear_all (); |
|
588 |
|
589 // This record might have been read only. If so, we shouldn't delete |
|
590 // it from the table. |
|
591 if (curr->is_defined ()) |
|
592 { |
|
593 prev = curr; |
|
594 curr = curr->next (); |
|
595 } |
|
596 else |
|
597 { |
|
598 prev->next_elem = curr->next (); |
|
599 symbol_record *tmp = curr; |
|
600 curr = curr->next (); |
|
601 delete tmp; |
|
602 } |
|
603 } |
|
604 } |
|
605 } |
|
606 |
|
607 int |
164
|
608 symbol_table::clear (const char *nm) |
1
|
609 { |
|
610 int index = hash (nm) & HASH_MASK; |
|
611 |
|
612 symbol_record *prev = &table[index]; |
|
613 symbol_record *curr = prev->next (); |
|
614 |
|
615 while (curr != (symbol_record *) NULL) |
|
616 { |
|
617 if (strcmp (curr->name (), nm) == 0) |
|
618 { |
|
619 curr->clear_visible (); |
|
620 |
|
621 if (! curr->is_defined ()) |
|
622 { |
|
623 prev->next_elem = curr->next (); |
|
624 symbol_record *tmp = curr; |
|
625 curr = curr->next (); |
|
626 delete tmp; |
|
627 } |
|
628 |
|
629 return 1; |
|
630 } |
|
631 prev = curr; |
|
632 curr = curr->next (); |
|
633 } |
|
634 |
|
635 return 0; |
|
636 } |
|
637 |
|
638 void |
|
639 symbol_table::undefine (void) |
|
640 { |
|
641 for (int i = 0; i < HASH_TABLE_SIZE; i++) |
|
642 { |
|
643 symbol_record *ptr = table[i].next (); |
|
644 |
|
645 while (ptr != (symbol_record *) NULL) |
|
646 { |
|
647 ptr->undefine (); |
|
648 ptr = ptr->next (); |
|
649 } |
|
650 } |
|
651 } |
|
652 |
|
653 // Ugh. |
|
654 |
|
655 void |
|
656 symbol_table::bind_globals (void) |
|
657 { |
|
658 assert (this != global_sym_tab); |
|
659 |
|
660 for (int i = 0; i < HASH_TABLE_SIZE; i++) |
|
661 { |
|
662 symbol_record *ptr = table[i].next (); |
|
663 |
|
664 while (ptr != (symbol_record *) NULL && ! ptr->formal_param) |
|
665 { |
|
666 char *nm = ptr->name (); |
|
667 symbol_record *sr = global_sym_tab->lookup (nm, 0, 0); |
|
668 if (sr != (symbol_record *) NULL) |
|
669 ptr->alias (sr, 1); |
|
670 ptr = ptr->next (); |
|
671 } |
|
672 } |
|
673 } |
|
674 |
|
675 int |
|
676 symbol_table::save (ostream& os, int mark_as_global = 0) |
|
677 { |
|
678 int status = 0; |
111
|
679 char **names = sorted_var_list (); |
|
680 if (names != (char **) NULL) |
1
|
681 { |
111
|
682 while (*names != (char *) NULL) |
|
683 { |
|
684 if (save (os, *names, mark_as_global)) |
|
685 status++; |
|
686 |
|
687 names++; |
|
688 } |
1
|
689 } |
|
690 return status; |
|
691 } |
|
692 |
|
693 int |
164
|
694 symbol_table::save (ostream& os, const char *name, int mark_as_global = 0) |
1
|
695 { |
|
696 int status = 0; |
|
697 symbol_record *sr = lookup (name, 0, 0); |
|
698 if (sr != (symbol_record *) NULL) |
|
699 status = sr->save (os, mark_as_global); |
|
700 return status; |
|
701 } |
|
702 |
|
703 int |
164
|
704 symbol_table::size (void) const |
1
|
705 { |
|
706 int count = 0; |
|
707 for (int i = 0; i < HASH_TABLE_SIZE; i++) |
|
708 { |
|
709 symbol_record *ptr = table[i].next (); |
|
710 while (ptr != (symbol_record *) NULL) |
|
711 { |
|
712 count++; |
|
713 ptr = ptr->next (); |
|
714 } |
|
715 } |
|
716 return count; |
|
717 } |
|
718 |
|
719 char ** |
164
|
720 symbol_table::list (void) const |
1
|
721 { |
|
722 int count; |
|
723 return list (count); |
|
724 } |
|
725 |
|
726 char ** |
164
|
727 symbol_table::var_list (void) const |
1
|
728 { |
|
729 int count; |
|
730 return var_list (count); |
|
731 } |
|
732 |
|
733 char ** |
164
|
734 symbol_table::fcn_list (void) const |
1
|
735 { |
|
736 int count; |
|
737 return fcn_list (count); |
|
738 } |
|
739 |
|
740 char ** |
164
|
741 symbol_table::list (int& count) const |
1
|
742 { |
115
|
743 count = 0; |
1
|
744 int n = size (); |
|
745 if (n == 0) |
|
746 return (char **) NULL; |
|
747 |
|
748 char **symbols = new char * [n+1]; |
|
749 for (int i = 0; i < HASH_TABLE_SIZE; i++) |
|
750 { |
|
751 symbol_record *ptr = table[i].next (); |
|
752 while (ptr != (symbol_record *) NULL) |
|
753 { |
|
754 assert (count < n); |
|
755 symbols[count++] = strsave (ptr->name ()); |
|
756 ptr = ptr->next (); |
|
757 } |
|
758 } |
|
759 symbols[count] = (char *) NULL; |
|
760 return symbols; |
|
761 } |
|
762 |
|
763 char ** |
164
|
764 symbol_table::var_list (int& count) const |
1
|
765 { |
115
|
766 count = 0; |
1
|
767 int n = size (); |
|
768 if (n == 0) |
|
769 return (char **) NULL; |
|
770 |
|
771 char **symbols = new char * [n+1]; |
|
772 for (int i = 0; i < HASH_TABLE_SIZE; i++) |
|
773 { |
|
774 symbol_record *ptr = table[i].next (); |
|
775 while (ptr != (symbol_record *) NULL) |
|
776 { |
|
777 assert (count < n); |
|
778 if (ptr->is_variable ()) |
|
779 symbols[count++] = strsave (ptr->name ()); |
|
780 ptr = ptr->next (); |
|
781 } |
|
782 } |
|
783 symbols[count] = (char *) NULL; |
|
784 return symbols; |
|
785 } |
|
786 |
|
787 char ** |
164
|
788 symbol_table::fcn_list (int& count) const |
1
|
789 { |
115
|
790 count = 0; |
1
|
791 int n = size (); |
|
792 if (n == 0) |
|
793 return (char **) NULL; |
|
794 |
|
795 char **symbols = new char * [n+1]; |
|
796 for (int i = 0; i < HASH_TABLE_SIZE; i++) |
|
797 { |
|
798 symbol_record *ptr = table[i].next (); |
|
799 while (ptr != (symbol_record *) NULL) |
|
800 { |
|
801 assert (count < n); |
|
802 if (ptr->is_function ()) |
|
803 symbols[count++] = strsave (ptr->name ()); |
|
804 ptr = ptr->next (); |
|
805 } |
|
806 } |
|
807 symbols[count] = (char *) NULL; |
|
808 return symbols; |
|
809 } |
|
810 |
|
811 static inline int |
|
812 pstrcmp (char **a, char **b) |
|
813 { |
|
814 return strcmp (*a, *b); |
|
815 } |
|
816 |
|
817 char ** |
164
|
818 symbol_table::sorted_list (void) const |
1
|
819 { |
|
820 int count = 0; |
|
821 return sorted_list (count); |
|
822 } |
|
823 |
|
824 char ** |
164
|
825 symbol_table::sorted_var_list (void) const |
1
|
826 { |
|
827 int count = 0; |
|
828 return sorted_var_list (count); |
|
829 } |
|
830 |
|
831 char ** |
164
|
832 symbol_table::sorted_fcn_list (void) const |
1
|
833 { |
|
834 int count = 0; |
|
835 return sorted_fcn_list (count); |
|
836 } |
|
837 |
|
838 char ** |
164
|
839 symbol_table::sorted_list (int& count) const |
1
|
840 { |
|
841 char **symbols = list (count); |
|
842 if (symbols != (char **) NULL) |
|
843 qsort ((void **) symbols, count, sizeof (char *), |
|
844 (int (*)(void*, void*)) pstrcmp); |
|
845 return symbols; |
|
846 } |
|
847 |
|
848 char ** |
164
|
849 symbol_table::sorted_var_list (int& count) const |
1
|
850 { |
|
851 char **symbols = var_list (count); |
|
852 if (symbols != (char **) NULL) |
|
853 qsort ((void **) symbols, count, sizeof (char *), |
|
854 (int (*)(void*, void*)) pstrcmp); |
|
855 return symbols; |
|
856 } |
|
857 |
|
858 char ** |
164
|
859 symbol_table::sorted_fcn_list (int& count) const |
1
|
860 { |
|
861 char **symbols = fcn_list (count); |
|
862 if (symbols != (char **) NULL) |
|
863 qsort ((void **) symbols, count, sizeof (char *), |
|
864 (int (*)(void*, void*)) pstrcmp); |
|
865 return symbols; |
|
866 } |
|
867 |
|
868 // Chris Torek's fave hash function. |
|
869 |
|
870 unsigned int |
|
871 symbol_table::hash (const char *str) |
|
872 { |
|
873 unsigned h = 0; |
|
874 while (*str) |
|
875 h = h * 33 + *str++; |
|
876 return h; |
|
877 } |
|
878 |
|
879 /* |
|
880 ;;; Local Variables: *** |
|
881 ;;; mode: C++ *** |
|
882 ;;; page-delimiter: "^/\\*" *** |
|
883 ;;; End: *** |
|
884 */ |