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