1
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
4192
|
23 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
1297
|
24 #pragma implementation |
|
25 #endif |
|
26 |
240
|
27 #ifdef HAVE_CONFIG_H |
1192
|
28 #include <config.h> |
1
|
29 #endif |
|
30 |
3013
|
31 #include <cassert> |
1962
|
32 #include <cctype> |
3013
|
33 #include <climits> |
|
34 |
3503
|
35 #include <iomanip> |
|
36 #include <fstream> |
1962
|
37 |
2926
|
38 #include "glob-match.h" |
1755
|
39 #include "str-vec.h" |
|
40 |
3308
|
41 #include "defun.h" |
1352
|
42 #include "error.h" |
2979
|
43 #include "oct-lvalue.h" |
2975
|
44 #include "ov.h" |
3356
|
45 #include "pt-pr-code.h" |
1755
|
46 #include "symtab.h" |
1
|
47 #include "utils.h" |
1352
|
48 #include "variables.h" |
1
|
49 |
4238
|
50 unsigned long int symbol_table::symtab_count = 0; |
|
51 |
3308
|
52 // Should variables be allowed to hide functions of the same name? A |
|
53 // positive value means yes. A negative value means yes, but print a |
|
54 // warning message. Zero means it should be considered an error. |
|
55 static int Vvariables_can_hide_functions; |
|
56 |
4238
|
57 // Nonzero means we print debugging info about symbol table lookups. |
|
58 static int Vdebug_symtab_lookups; |
|
59 |
3013
|
60 octave_allocator |
|
61 symbol_record::symbol_def::allocator (sizeof (symbol_record::symbol_def)); |
195
|
62 |
3355
|
63 #define SYMBOL_DEF symbol_record::symbol_def |
|
64 |
3536
|
65 std::string |
3355
|
66 SYMBOL_DEF::type_as_string (void) const |
|
67 { |
3523
|
68 std::string retval = "<unknown type>"; |
3355
|
69 |
|
70 if (is_user_variable ()) |
|
71 retval = "user-defined variable"; |
4208
|
72 else if (is_command ()) |
|
73 retval = "built-in command"; |
3355
|
74 else if (is_mapper_function ()) |
|
75 retval = "built-in mapper function"; |
|
76 else if (is_user_function ()) |
|
77 retval = "user-defined function"; |
|
78 else if (is_builtin_constant ()) |
|
79 retval = "built-in constant"; |
|
80 else if (is_builtin_variable ()) |
|
81 retval = "built-in variable"; |
|
82 else if (is_builtin_function ()) |
|
83 retval = "built-in function"; |
|
84 else if (is_dld_function ()) |
|
85 retval = "dynamically-linked function"; |
|
86 |
|
87 return retval; |
|
88 } |
|
89 |
3356
|
90 void |
3523
|
91 SYMBOL_DEF::type (std::ostream& os, const std::string& name, bool pr_type_info, |
3356
|
92 bool quiet, bool pr_orig_txt) |
|
93 { |
|
94 if (is_user_function ()) |
|
95 { |
|
96 octave_function *defn = definition.function_value (); |
|
97 |
3523
|
98 std::string fn = defn ? defn->fcn_file_name () : std::string (); |
3356
|
99 |
|
100 if (pr_orig_txt && ! fn.empty ()) |
|
101 { |
3544
|
102 std::ifstream fs (fn.c_str (), std::ios::in); |
3356
|
103 |
|
104 if (fs) |
|
105 { |
|
106 if (pr_type_info && ! quiet) |
|
107 os << name << " is the " << type_as_string () |
|
108 << " defined from: " << fn << "\n\n"; |
|
109 |
|
110 char ch; |
|
111 |
|
112 while (fs.get (ch)) |
|
113 os << ch; |
|
114 } |
|
115 else |
|
116 os << "unable to open `" << fn << "' for reading!\n"; |
|
117 } |
|
118 else |
|
119 { |
|
120 if (pr_type_info && ! quiet) |
|
121 os << name << " is a " << type_as_string () << ":\n\n"; |
|
122 |
|
123 tree_print_code tpc (os, "", pr_orig_txt); |
|
124 |
|
125 defn->accept (tpc); |
|
126 } |
|
127 } |
|
128 else if (is_user_variable () |
|
129 || is_builtin_variable () |
|
130 || is_builtin_constant ()) |
|
131 { |
|
132 if (pr_type_info && ! quiet) |
|
133 os << name << " is a " << type_as_string () << "\n"; |
|
134 |
|
135 definition.print_raw (os, true); |
|
136 |
|
137 if (pr_type_info) |
|
138 os << "\n"; |
|
139 } |
|
140 else |
|
141 os << name << " is a " << type_as_string () << "\n"; |
|
142 } |
|
143 |
3536
|
144 std::string |
3523
|
145 SYMBOL_DEF::which (const std::string& name) |
3355
|
146 { |
3523
|
147 std::string retval; |
3355
|
148 |
|
149 if (is_user_function () || is_dld_function ()) |
|
150 { |
|
151 octave_function *defn = definition.function_value (); |
|
152 |
|
153 if (defn) |
|
154 retval = defn->fcn_file_name (); |
|
155 } |
|
156 else |
|
157 retval = name + " is a " + type_as_string (); |
|
158 |
|
159 return retval; |
|
160 } |
|
161 |
3239
|
162 void |
3523
|
163 SYMBOL_DEF::which (std::ostream& os, const std::string& name) |
3355
|
164 { |
|
165 os << name; |
|
166 |
|
167 if (is_user_function () || is_dld_function ()) |
|
168 { |
|
169 octave_function *defn = definition.function_value (); |
|
170 |
3523
|
171 std::string fn = defn ? defn->fcn_file_name () : std::string (); |
3355
|
172 |
|
173 if (! fn.empty ()) |
|
174 { |
|
175 os << " is the " << type_as_string () << " from the file\n" |
|
176 << fn << "\n"; |
|
177 |
|
178 return; |
|
179 } |
|
180 } |
|
181 |
|
182 os << " is a " << type_as_string () << "\n"; |
|
183 } |
|
184 |
|
185 void |
3944
|
186 SYMBOL_DEF::print_info (std::ostream& os, const std::string& prefix) const |
3239
|
187 { |
3933
|
188 os << prefix << "symbol_def::count: " << count << "\n"; |
|
189 |
|
190 definition.print_info (os, prefix + " "); |
3239
|
191 } |
|
192 |
767
|
193 // Individual records in a symbol table. |
|
194 |
3013
|
195 // XXX FIXME XXX -- there are lots of places below where we should |
|
196 // probably be temporarily ignoring interrupts. |
1
|
197 |
572
|
198 void |
3523
|
199 symbol_record::rename (const std::string& new_name) |
572
|
200 { |
2791
|
201 if (! read_only_error ("rename")) |
|
202 nm = new_name; |
572
|
203 } |
|
204 |
3013
|
205 void |
|
206 symbol_record::define (const octave_value& v, unsigned int sym_type) |
195
|
207 { |
3013
|
208 if (! (is_variable () && read_only_error ("redefine"))) |
|
209 { |
|
210 if (definition->type () == symbol_record::BUILTIN_VARIABLE) |
|
211 sym_type = symbol_record::BUILTIN_VARIABLE; |
1
|
212 |
3013
|
213 definition->define (v, sym_type); |
195
|
214 } |
|
215 } |
|
216 |
|
217 void |
3013
|
218 symbol_record::define_builtin_var (const octave_value& v) |
195
|
219 { |
3013
|
220 define (v, symbol_record::BUILTIN_VARIABLE); |
195
|
221 |
3013
|
222 if (chg_fcn) |
|
223 chg_fcn (); |
1
|
224 } |
|
225 |
3013
|
226 bool |
3258
|
227 symbol_record::define_builtin_const (const octave_value& v) |
1
|
228 { |
3013
|
229 bool retval = false; |
1
|
230 |
3258
|
231 if (! read_only_error ("redefine")) |
195
|
232 { |
3811
|
233 definition->define (v, symbol_record::BUILTIN_CONSTANT); |
2893
|
234 |
3013
|
235 retval = true; |
1
|
236 } |
|
237 |
2893
|
238 return retval; |
2390
|
239 } |
|
240 |
3013
|
241 bool |
|
242 symbol_record::define (octave_function *f, unsigned int sym_type) |
1
|
243 { |
3013
|
244 bool retval = false; |
2949
|
245 |
3013
|
246 if (! read_only_error ("redefine")) |
|
247 { |
|
248 octave_value tmp (f); |
|
249 |
4261
|
250 if (! definition) |
|
251 definition = new symbol_def (tmp, sym_type); |
|
252 else |
|
253 definition->define (tmp, sym_type); |
3013
|
254 |
|
255 retval = true; |
|
256 } |
2949
|
257 |
|
258 return retval; |
1
|
259 } |
|
260 |
|
261 void |
195
|
262 symbol_record::clear (void) |
1
|
263 { |
4261
|
264 if (is_defined ()) |
1
|
265 { |
4261
|
266 if (! tagged_static) |
|
267 { |
|
268 if (--definition->count <= 0) |
|
269 delete definition; |
3013
|
270 |
4261
|
271 definition = new symbol_def (); |
|
272 } |
3013
|
273 |
4261
|
274 if (linked_to_global) |
|
275 linked_to_global = 0; |
|
276 } |
1
|
277 } |
|
278 |
|
279 void |
4009
|
280 symbol_record::alias (symbol_record *s) |
1
|
281 { |
3013
|
282 chg_fcn = s->chg_fcn; |
195
|
283 |
4009
|
284 if (--definition->count <= 0) |
|
285 delete definition; |
|
286 |
|
287 definition = (s->definition); |
3013
|
288 |
|
289 definition->count++; |
1
|
290 } |
|
291 |
|
292 void |
|
293 symbol_record::mark_as_formal_parameter (void) |
|
294 { |
3013
|
295 if (is_linked_to_global ()) |
|
296 error ("can't mark global variable `%s' as function parameter", |
|
297 nm.c_str ()); |
|
298 else if (is_static ()) |
|
299 error ("can't mark static variable `%s' as function paraemter", |
|
300 nm.c_str ()); |
|
301 else |
|
302 formal_param = 1; |
1
|
303 } |
|
304 |
|
305 void |
195
|
306 symbol_record::mark_as_linked_to_global (void) |
122
|
307 { |
2975
|
308 if (is_formal_parameter ()) |
|
309 error ("can't make function parameter `%s' global", nm.c_str ()); |
|
310 else if (is_static ()) |
|
311 error ("can't make static variable `%s' global", nm.c_str ()); |
|
312 else |
|
313 linked_to_global = 1; |
122
|
314 } |
|
315 |
2846
|
316 void |
|
317 symbol_record::mark_as_static (void) |
|
318 { |
|
319 if (is_linked_to_global ()) |
2975
|
320 error ("can't make global variable `%s' static", nm.c_str ()); |
2846
|
321 else if (is_formal_parameter ()) |
2975
|
322 error ("can't make formal parameter `%s' static", nm.c_str ()); |
2846
|
323 else |
|
324 tagged_static = 1; |
|
325 } |
|
326 |
2975
|
327 octave_value& |
|
328 symbol_record::variable_value (void) |
2390
|
329 { |
2975
|
330 static octave_value foo; |
2390
|
331 |
2975
|
332 return is_variable () ? def () : foo; |
2390
|
333 } |
|
334 |
3258
|
335 inline void |
|
336 symbol_record::link_to_builtin_variable (void) |
|
337 { |
4009
|
338 symbol_record *tmp_sym = fbi_sym_tab->lookup (name ()); |
3258
|
339 |
|
340 if (tmp_sym && tmp_sym->is_builtin_variable ()) |
|
341 alias (tmp_sym); |
|
342 } |
|
343 |
2979
|
344 octave_lvalue |
2390
|
345 symbol_record::variable_reference (void) |
|
346 { |
4234
|
347 if ((Vvariables_can_hide_functions <= 0 || ! can_hide_function) |
3308
|
348 && (is_function () |
|
349 || (! is_defined () && is_valid_function (nm)))) |
|
350 { |
4234
|
351 if (Vvariables_can_hide_functions < 0 && can_hide_function) |
3308
|
352 warning ("variable `%s' hides function", nm.c_str ()); |
|
353 else |
|
354 { |
|
355 error ("variable `%s' hides function", nm.c_str ()); |
|
356 return octave_lvalue (); |
|
357 } |
|
358 } |
|
359 |
3258
|
360 if (is_function () || is_constant ()) |
2390
|
361 clear (); |
|
362 |
|
363 if (! is_defined ()) |
|
364 { |
|
365 if (! (is_formal_parameter () || is_linked_to_global ())) |
3258
|
366 link_to_builtin_variable (); |
2390
|
367 |
|
368 if (! is_defined ()) |
2975
|
369 { |
|
370 octave_value tmp; |
|
371 define (tmp); |
|
372 } |
2390
|
373 } |
|
374 |
3013
|
375 return octave_lvalue (&(def ()), chg_fcn); |
195
|
376 } |
|
377 |
220
|
378 void |
|
379 symbol_record::push_context (void) |
|
380 { |
2846
|
381 if (! is_static ()) |
|
382 { |
|
383 context.push (definition); |
3013
|
384 |
|
385 definition = new symbol_def (); |
395
|
386 |
2893
|
387 global_link_context.push (static_cast<unsigned int> (linked_to_global)); |
3013
|
388 |
2846
|
389 linked_to_global = 0; |
|
390 } |
220
|
391 } |
|
392 |
|
393 void |
|
394 symbol_record::pop_context (void) |
|
395 { |
1653
|
396 // It is possible for context to be empty if new symbols have been |
|
397 // inserted in the symbol table during recursive calls. This can |
|
398 // happen as a result of calls to eval() and feval(). |
220
|
399 |
1653
|
400 if (! context.empty ()) |
220
|
401 { |
4009
|
402 if (--definition->count <= 0) |
|
403 delete definition; |
|
404 |
4214
|
405 definition = context.top (); |
|
406 context.pop (); |
1653
|
407 |
4214
|
408 linked_to_global = global_link_context.top (); |
|
409 global_link_context.pop (); |
220
|
410 } |
|
411 } |
|
412 |
3013
|
413 void |
3933
|
414 symbol_record::print_symbol_info_line (std::ostream& os) const |
3013
|
415 { |
|
416 os << (is_read_only () ? " r-" : " rw") |
4681
|
417 << (is_static () || is_eternal () ? "-" : "d") |
3013
|
418 << " " |
3548
|
419 << std::setiosflags (std::ios::left) << std::setw (24) |
|
420 << type_name () . c_str (); |
3013
|
421 |
3548
|
422 os << std::resetiosflags (std::ios::left); |
3013
|
423 |
|
424 int nr = rows (); |
|
425 int nc = columns (); |
|
426 |
|
427 if (nr < 0) |
|
428 os << " -"; |
|
429 else |
3548
|
430 os << std::setiosflags (std::ios::right) << std::setw (7) << nr; |
3013
|
431 |
|
432 if (nc < 0) |
|
433 os << " -"; |
|
434 else |
3548
|
435 os << std::setiosflags (std::ios::right) << std::setw (7) << nc; |
3013
|
436 |
3548
|
437 os << std::resetiosflags (std::ios::right); |
3013
|
438 |
|
439 os << " " << name () << "\n"; |
|
440 } |
|
441 |
3239
|
442 void |
3944
|
443 symbol_record::print_info (std::ostream& os, const std::string& prefix) const |
3239
|
444 { |
|
445 if (definition) |
3933
|
446 definition->print_info (os, prefix); |
3239
|
447 else |
3933
|
448 os << prefix << "symbol " << name () << " is undefined\n"; |
3239
|
449 } |
|
450 |
3013
|
451 bool |
2791
|
452 symbol_record::read_only_error (const char *action) |
195
|
453 { |
|
454 if (is_read_only ()) |
|
455 { |
3258
|
456 if (is_variable () || is_constant ()) |
3013
|
457 ::error ("can't %s read-only constant `%s'", action, nm.c_str ()); |
195
|
458 else if (is_function ()) |
3013
|
459 ::error ("can't %s read-only function `%s'", action, nm.c_str ()); |
1045
|
460 else |
3013
|
461 ::error ("can't %s read-only symbol `%s'", action, nm.c_str ()); |
195
|
462 |
3013
|
463 return true; |
195
|
464 } |
|
465 else |
3013
|
466 return false; |
195
|
467 } |
|
468 |
767
|
469 // A symbol table. |
1
|
470 |
|
471 symbol_record * |
3523
|
472 symbol_table::lookup (const std::string& nm, bool insert, bool warn) |
1
|
473 { |
4238
|
474 if (Vdebug_symtab_lookups) |
|
475 { |
4269
|
476 std::cerr << (table_name.empty () ? std::string ("???") : table_name) |
4238
|
477 << " symtab::lookup [" |
|
478 << (insert ? "I" : "-") |
|
479 << (warn ? "W" : "-") |
|
480 << "] \"" << nm << "\"\n"; |
|
481 } |
|
482 |
3013
|
483 unsigned int index = hash (nm); |
1
|
484 |
|
485 symbol_record *ptr = table[index].next (); |
|
486 |
530
|
487 while (ptr) |
1
|
488 { |
1755
|
489 if (ptr->name () == nm) |
1
|
490 return ptr; |
3013
|
491 |
1
|
492 ptr = ptr->next (); |
|
493 } |
|
494 |
|
495 if (insert) |
|
496 { |
3013
|
497 symbol_record *sr = new symbol_record (nm, table[index].next ()); |
|
498 |
|
499 table[index].chain (sr); |
|
500 |
|
501 return sr; |
1
|
502 } |
|
503 else if (warn) |
3356
|
504 warning ("lookup: symbol `%s' not found", nm.c_str ()); |
1
|
505 |
530
|
506 return 0; |
1
|
507 } |
|
508 |
|
509 void |
3523
|
510 symbol_table::rename (const std::string& old_name, const std::string& new_name) |
572
|
511 { |
4238
|
512 if (Vdebug_symtab_lookups) |
|
513 { |
4269
|
514 std::cerr << (table_name.empty () ? std::string ("???") : table_name) |
4238
|
515 << " symtab::rename " |
|
516 << "\"" << old_name << "\"" |
|
517 << " to " |
|
518 << "\"" << new_name << "\"\n"; |
|
519 } |
|
520 |
3013
|
521 unsigned int index = hash (old_name); |
572
|
522 |
|
523 symbol_record *prev = &table[index]; |
|
524 symbol_record *ptr = prev->next (); |
|
525 |
|
526 while (ptr) |
|
527 { |
1755
|
528 if (ptr->name () == old_name) |
572
|
529 { |
|
530 ptr->rename (new_name); |
|
531 |
2791
|
532 if (! error_state) |
|
533 { |
|
534 prev->chain (ptr->next ()); |
|
535 |
3013
|
536 index = hash (new_name); |
3125
|
537 ptr->chain (table[index].next ()); |
2791
|
538 table[index].chain (ptr); |
|
539 |
|
540 return; |
|
541 } |
|
542 |
|
543 break; |
572
|
544 } |
|
545 |
|
546 prev = ptr; |
|
547 ptr = ptr->next (); |
|
548 } |
|
549 |
1755
|
550 error ("unable to rename `%s' to `%s'", old_name.c_str (), |
|
551 new_name.c_str ()); |
572
|
552 } |
|
553 |
4009
|
554 // XXX FIXME XXX -- it would be nice to eliminate a lot of the |
|
555 // following duplicate code. |
|
556 |
572
|
557 void |
4009
|
558 symbol_table::clear (void) |
|
559 { |
|
560 for (unsigned int i = 0; i < table_size; i++) |
|
561 { |
|
562 symbol_record *ptr = table[i].next (); |
|
563 |
|
564 while (ptr) |
|
565 { |
|
566 ptr->clear (); |
|
567 |
|
568 ptr = ptr->next (); |
|
569 } |
|
570 } |
|
571 } |
|
572 |
|
573 void |
|
574 symbol_table::clear_variables (void) |
|
575 { |
|
576 for (unsigned int i = 0; i < table_size; i++) |
|
577 { |
|
578 symbol_record *ptr = table[i].next (); |
|
579 |
|
580 while (ptr) |
|
581 { |
|
582 if (ptr->is_user_variable ()) |
|
583 ptr->clear (); |
|
584 |
|
585 ptr = ptr->next (); |
|
586 } |
|
587 } |
|
588 } |
|
589 |
|
590 // Really only clear functions that can be reloaded. |
|
591 |
|
592 void |
|
593 symbol_table::clear_functions (void) |
1
|
594 { |
3013
|
595 for (unsigned int i = 0; i < table_size; i++) |
1
|
596 { |
169
|
597 symbol_record *ptr = table[i].next (); |
1
|
598 |
530
|
599 while (ptr) |
169
|
600 { |
4009
|
601 if (ptr->is_user_function () || ptr->is_dld_function ()) |
|
602 ptr->clear (); |
|
603 |
|
604 ptr = ptr->next (); |
|
605 } |
|
606 } |
|
607 } |
|
608 |
|
609 void |
|
610 symbol_table::clear_globals (void) |
|
611 { |
|
612 for (unsigned int i = 0; i < table_size; i++) |
|
613 { |
|
614 symbol_record *ptr = table[i].next (); |
|
615 |
|
616 while (ptr) |
|
617 { |
|
618 if (ptr->is_user_variable () && ptr->is_linked_to_global ()) |
|
619 ptr->clear (); |
195
|
620 |
169
|
621 ptr = ptr->next (); |
1
|
622 } |
|
623 } |
|
624 } |
|
625 |
3013
|
626 bool |
4009
|
627 symbol_table::clear (const std::string& nm) |
|
628 { |
|
629 unsigned int index = hash (nm); |
|
630 |
|
631 symbol_record *ptr = table[index].next (); |
|
632 |
|
633 while (ptr) |
|
634 { |
|
635 if (ptr->name () == nm) |
|
636 { |
|
637 ptr->clear (); |
|
638 return true; |
|
639 } |
|
640 ptr = ptr->next (); |
|
641 } |
|
642 |
|
643 return false; |
|
644 } |
|
645 |
|
646 bool |
|
647 symbol_table::clear_variable (const std::string& nm) |
|
648 { |
|
649 unsigned int index = hash (nm); |
|
650 |
|
651 symbol_record *ptr = table[index].next (); |
|
652 |
|
653 while (ptr) |
|
654 { |
|
655 if (ptr->name () == nm && ptr->is_user_variable ()) |
|
656 { |
|
657 ptr->clear (); |
|
658 return true; |
|
659 } |
|
660 ptr = ptr->next (); |
|
661 } |
|
662 |
|
663 return false; |
|
664 } |
|
665 |
|
666 bool |
|
667 symbol_table::clear_global (const std::string& nm) |
1
|
668 { |
3013
|
669 unsigned int index = hash (nm); |
1
|
670 |
195
|
671 symbol_record *ptr = table[index].next (); |
1
|
672 |
530
|
673 while (ptr) |
1
|
674 { |
1755
|
675 if (ptr->name () == nm |
4009
|
676 && ptr->is_user_variable () |
|
677 && ptr->is_linked_to_global ()) |
|
678 { |
|
679 ptr->clear (); |
|
680 return true; |
|
681 } |
|
682 ptr = ptr->next (); |
|
683 } |
|
684 |
|
685 return false; |
|
686 } |
|
687 |
|
688 // Really only clear functions that can be reloaded. |
|
689 |
|
690 bool |
|
691 symbol_table::clear_function (const std::string& nm) |
|
692 { |
|
693 unsigned int index = hash (nm); |
|
694 |
|
695 symbol_record *ptr = table[index].next (); |
|
696 |
|
697 while (ptr) |
|
698 { |
|
699 if (ptr->name () == nm |
|
700 && (ptr->is_user_function () || ptr->is_dld_function ())) |
1
|
701 { |
195
|
702 ptr->clear (); |
3013
|
703 return true; |
1
|
704 } |
195
|
705 ptr = ptr->next (); |
1
|
706 } |
|
707 |
3013
|
708 return false; |
1
|
709 } |
|
710 |
4009
|
711 bool |
|
712 symbol_table::clear_variable_pattern (const std::string& pat) |
|
713 { |
|
714 bool retval = false; |
|
715 |
|
716 for (unsigned int i = 0; i < table_size; i++) |
|
717 { |
|
718 symbol_record *ptr = table[i].next (); |
|
719 |
|
720 while (ptr) |
|
721 { |
|
722 if (ptr->is_user_variable ()) |
|
723 { |
|
724 glob_match pattern (pat); |
|
725 |
|
726 if (pattern.match (ptr->name ())) |
|
727 { |
|
728 ptr->clear (); |
|
729 |
|
730 retval = true; |
|
731 } |
|
732 } |
|
733 |
|
734 ptr = ptr->next (); |
|
735 } |
|
736 } |
|
737 |
|
738 return retval; |
|
739 } |
|
740 |
|
741 bool |
|
742 symbol_table::clear_global_pattern (const std::string& pat) |
|
743 { |
|
744 bool retval = false; |
|
745 |
|
746 for (unsigned int i = 0; i < table_size; i++) |
|
747 { |
|
748 symbol_record *ptr = table[i].next (); |
|
749 |
|
750 while (ptr) |
|
751 { |
|
752 if (ptr->is_user_variable () && ptr->is_linked_to_global ()) |
|
753 { |
|
754 glob_match pattern (pat); |
|
755 |
|
756 if (pattern.match (ptr->name ())) |
|
757 { |
|
758 ptr->clear (); |
|
759 |
|
760 retval = true; |
|
761 } |
|
762 } |
|
763 |
|
764 ptr = ptr->next (); |
|
765 } |
|
766 } |
|
767 |
|
768 return retval; |
|
769 } |
|
770 |
|
771 // Really only clear functions that can be reloaded. |
|
772 |
|
773 bool |
|
774 symbol_table::clear_function_pattern (const std::string& pat) |
|
775 { |
|
776 bool retval = false; |
|
777 |
|
778 for (unsigned int i = 0; i < table_size; i++) |
|
779 { |
|
780 symbol_record *ptr = table[i].next (); |
|
781 |
|
782 while (ptr) |
|
783 { |
|
784 if (ptr->is_user_function () || ptr->is_dld_function ()) |
|
785 { |
|
786 glob_match pattern (pat); |
|
787 |
|
788 if (pattern.match (ptr->name ())) |
|
789 { |
|
790 ptr->clear (); |
|
791 |
|
792 retval = true; |
|
793 } |
|
794 } |
|
795 |
|
796 ptr = ptr->next (); |
|
797 } |
|
798 } |
|
799 |
|
800 return retval; |
|
801 } |
|
802 |
1
|
803 int |
164
|
804 symbol_table::size (void) const |
1
|
805 { |
|
806 int count = 0; |
3013
|
807 |
|
808 for (unsigned int i = 0; i < table_size; i++) |
1
|
809 { |
|
810 symbol_record *ptr = table[i].next (); |
3013
|
811 |
530
|
812 while (ptr) |
1
|
813 { |
|
814 count++; |
|
815 ptr = ptr->next (); |
|
816 } |
|
817 } |
3013
|
818 |
1
|
819 return count; |
|
820 } |
|
821 |
3013
|
822 static bool |
3523
|
823 matches_patterns (const std::string& name, const string_vector& pats) |
1
|
824 { |
3013
|
825 int npats = pats.length (); |
1
|
826 |
3013
|
827 if (npats == 0) |
|
828 return true; |
|
829 |
|
830 glob_match pattern (pats); |
|
831 |
|
832 return pattern.match (name); |
1
|
833 } |
|
834 |
3013
|
835 Array<symbol_record *> |
3355
|
836 symbol_table::symbol_list (const string_vector& pats, |
3013
|
837 unsigned int type, unsigned int scope) const |
1
|
838 { |
3355
|
839 int count = 0; |
3013
|
840 |
1
|
841 int n = size (); |
3013
|
842 |
3355
|
843 Array<symbol_record *> symbols (n); |
|
844 |
1
|
845 if (n == 0) |
3355
|
846 return symbols; |
3013
|
847 |
|
848 for (unsigned int i = 0; i < table_size; i++) |
1
|
849 { |
|
850 symbol_record *ptr = table[i].next (); |
3013
|
851 |
530
|
852 while (ptr) |
1
|
853 { |
|
854 assert (count < n); |
867
|
855 |
2893
|
856 unsigned int my_scope = ptr->is_linked_to_global () + 1; // Tricky... |
867
|
857 |
2893
|
858 unsigned int my_type = ptr->type (); |
867
|
859 |
3523
|
860 std::string my_name = ptr->name (); |
867
|
861 |
|
862 if ((type & my_type) && (scope & my_scope) |
3013
|
863 && matches_patterns (my_name, pats)) |
|
864 symbols(count++) = ptr; |
605
|
865 |
1
|
866 ptr = ptr->next (); |
|
867 } |
|
868 } |
1755
|
869 |
|
870 symbols.resize (count); |
1
|
871 |
3013
|
872 return symbols; |
|
873 } |
|
874 |
|
875 string_vector |
3355
|
876 symbol_table::name_list (const string_vector& pats, bool sort, |
3013
|
877 unsigned int type, unsigned int scope) const |
|
878 { |
3355
|
879 Array<symbol_record *> symbols = symbol_list (pats, type, scope); |
3013
|
880 |
|
881 string_vector names; |
|
882 |
|
883 int n = symbols.length (); |
|
884 |
|
885 if (n > 0) |
|
886 { |
|
887 names.resize (n); |
|
888 |
|
889 for (int i = 0; i < n; i++) |
|
890 names[i] = symbols(i)->name (); |
|
891 } |
|
892 |
|
893 if (sort) |
|
894 names.qsort (); |
|
895 |
|
896 return names; |
|
897 } |
|
898 |
|
899 static int |
3145
|
900 maybe_list_cmp_fcn (const void *a_arg, const void *b_arg) |
3013
|
901 { |
3145
|
902 const symbol_record *a = *(X_CAST (const symbol_record **, a_arg)); |
|
903 const symbol_record *b = *(X_CAST (const symbol_record **, b_arg)); |
3013
|
904 |
3523
|
905 std::string a_nm = a->name (); |
|
906 std::string b_nm = b->name (); |
3145
|
907 |
|
908 return a_nm.compare (b_nm); |
3013
|
909 } |
1
|
910 |
3013
|
911 int |
|
912 symbol_table::maybe_list (const char *header, const string_vector& argv, |
3523
|
913 std::ostream& os, bool show_verbose, |
3013
|
914 unsigned type, unsigned scope) |
|
915 { |
|
916 int status = 0; |
|
917 |
|
918 if (show_verbose) |
|
919 { |
3355
|
920 Array<symbol_record *> symbols = symbol_list (argv, type, scope); |
3013
|
921 |
|
922 int len = symbols.length (); |
|
923 |
3355
|
924 if (len > 0) |
3013
|
925 { |
|
926 os << "\n" << header << "\n\n" |
|
927 << "prot type rows cols name\n" |
|
928 << "==== ==== ==== ==== ====\n"; |
|
929 |
|
930 symbols.qsort (maybe_list_cmp_fcn); |
|
931 |
|
932 for (int i = 0; i < len; i++) |
|
933 symbols(i)->print_symbol_info_line (os); |
|
934 |
|
935 status = 1; |
|
936 } |
|
937 } |
|
938 else |
|
939 { |
3355
|
940 string_vector symbols = name_list (argv, 1, type, scope); |
3013
|
941 |
3355
|
942 if (! symbols.empty ()) |
3013
|
943 { |
|
944 os << "\n" << header << "\n\n"; |
|
945 |
|
946 symbols.list_in_columns (os); |
|
947 |
|
948 status = 1; |
|
949 } |
|
950 } |
|
951 |
|
952 return status; |
1
|
953 } |
|
954 |
3355
|
955 Array<symbol_record *> |
3523
|
956 symbol_table::glob (const std::string& pat, unsigned int type, |
2893
|
957 unsigned int scope) const |
605
|
958 { |
3355
|
959 int count = 0; |
|
960 |
605
|
961 int n = size (); |
3355
|
962 |
|
963 Array<symbol_record *> symbols (n); |
|
964 |
605
|
965 if (n == 0) |
3355
|
966 return symbols; |
3013
|
967 |
|
968 for (unsigned int i = 0; i < table_size; i++) |
605
|
969 { |
|
970 symbol_record *ptr = table[i].next (); |
3013
|
971 |
605
|
972 while (ptr) |
|
973 { |
|
974 assert (count < n); |
|
975 |
2893
|
976 unsigned int my_scope = ptr->is_linked_to_global () + 1; // Tricky... |
605
|
977 |
2893
|
978 unsigned int my_type = ptr->type (); |
605
|
979 |
1792
|
980 glob_match pattern (pat); |
1755
|
981 |
605
|
982 if ((type & my_type) && (scope & my_scope) |
1792
|
983 && pattern.match (ptr->name ())) |
605
|
984 { |
3355
|
985 symbols(count++) = ptr; |
605
|
986 } |
|
987 |
|
988 ptr = ptr->next (); |
|
989 } |
|
990 } |
3355
|
991 |
|
992 symbols.resize (count); |
605
|
993 |
|
994 return symbols; |
|
995 } |
|
996 |
220
|
997 void |
|
998 symbol_table::push_context (void) |
|
999 { |
3013
|
1000 for (unsigned int i = 0; i < table_size; i++) |
220
|
1001 { |
|
1002 symbol_record *ptr = table[i].next (); |
|
1003 |
530
|
1004 while (ptr) |
220
|
1005 { |
|
1006 ptr->push_context (); |
|
1007 ptr = ptr->next (); |
|
1008 } |
|
1009 } |
|
1010 } |
|
1011 |
|
1012 void |
|
1013 symbol_table::pop_context (void) |
|
1014 { |
3013
|
1015 for (unsigned int i = 0; i < table_size; i++) |
220
|
1016 { |
|
1017 symbol_record *ptr = table[i].next (); |
|
1018 |
530
|
1019 while (ptr) |
220
|
1020 { |
|
1021 ptr->pop_context (); |
|
1022 ptr = ptr->next (); |
|
1023 } |
|
1024 } |
|
1025 } |
|
1026 |
3013
|
1027 void |
3944
|
1028 symbol_table::print_info (std::ostream& os) const |
3013
|
1029 { |
|
1030 int count = 0; |
|
1031 int empty_chains = 0; |
|
1032 int max_chain_length = 0; |
|
1033 int min_chain_length = INT_MAX; |
|
1034 |
|
1035 for (unsigned int i = 0; i < table_size; i++) |
|
1036 { |
|
1037 int num_this_chain = 0; |
|
1038 |
|
1039 symbol_record *ptr = table[i].next (); |
|
1040 |
|
1041 if (ptr) |
3933
|
1042 os << "chain number " << i << ":\n"; |
3013
|
1043 else |
|
1044 { |
|
1045 empty_chains++; |
|
1046 min_chain_length = 0; |
|
1047 } |
|
1048 |
|
1049 while (ptr) |
|
1050 { |
|
1051 num_this_chain++; |
|
1052 |
3933
|
1053 os << " " << ptr->name () << "\n"; |
|
1054 |
|
1055 ptr->print_info (os, " "); |
3013
|
1056 |
|
1057 ptr = ptr->next (); |
|
1058 } |
|
1059 |
|
1060 count += num_this_chain; |
|
1061 |
|
1062 if (num_this_chain > max_chain_length) |
|
1063 max_chain_length = num_this_chain; |
|
1064 |
|
1065 if (num_this_chain < min_chain_length) |
|
1066 min_chain_length = num_this_chain; |
|
1067 |
|
1068 if (num_this_chain > 0) |
3933
|
1069 os << "\n"; |
3013
|
1070 } |
|
1071 |
3933
|
1072 os << "max chain length: " << max_chain_length << "\n"; |
|
1073 os << "min chain length: " << min_chain_length << "\n"; |
|
1074 os << "empty chains: " << empty_chains << "\n"; |
|
1075 os << "total chains: " << table_size << "\n"; |
|
1076 os << "total symbols: " << count << "\n"; |
3013
|
1077 } |
|
1078 |
1
|
1079 // Chris Torek's fave hash function. |
|
1080 |
|
1081 unsigned int |
3523
|
1082 symbol_table::hash (const std::string& str) |
1
|
1083 { |
2893
|
1084 unsigned int h = 0; |
3013
|
1085 |
2893
|
1086 for (unsigned int i = 0; i < str.length (); i++) |
1755
|
1087 h = h * 33 + str[i]; |
1
|
1088 |
3013
|
1089 return h & (table_size - 1); |
2790
|
1090 } |
|
1091 |
3308
|
1092 |
|
1093 static int |
|
1094 variables_can_hide_functions (void) |
|
1095 { |
|
1096 Vvariables_can_hide_functions |
|
1097 = check_preference ("variables_can_hide_functions"); |
|
1098 |
|
1099 return 0; |
|
1100 } |
|
1101 |
4238
|
1102 static int |
|
1103 debug_symtab_lookups (void) |
|
1104 { |
|
1105 Vdebug_symtab_lookups = check_preference ("debug_symtab_lookups"); |
|
1106 |
|
1107 return 0; |
|
1108 } |
|
1109 |
3308
|
1110 void |
|
1111 symbols_of_symtab (void) |
|
1112 { |
4233
|
1113 DEFVAR (variables_can_hide_functions, true, variables_can_hide_functions, |
3448
|
1114 "-*- texinfo -*-\n\ |
|
1115 @defvr variables_can_hide_functions\n\ |
|
1116 If the value of this variable is nonzero, assignments to variables may\n\ |
|
1117 hide previously defined functions of the same name. A negative value\n\ |
|
1118 will cause Octave to print a warning, but allow the operation.\n\ |
|
1119 @end defvr"); |
|
1120 |
4238
|
1121 DEFVAR (debug_symtab_lookups, false, debug_symtab_lookups, |
|
1122 "-*- texinfo -*-\n\ |
|
1123 @defvr debug_symtab_lookups\n\ |
|
1124 If the value of htis variable is nonzero, print debugging info when\n\ |
4911
|
1125 searching for symbols in the symbol tables.\n\ |
|
1126 @end defvr"); |
3308
|
1127 } |
|
1128 |
|
1129 |
1
|
1130 /* |
|
1131 ;;; Local Variables: *** |
|
1132 ;;; mode: C++ *** |
|
1133 ;;; End: *** |
|
1134 */ |