Daily bump.
[official-gcc.git] / gcc / tree-browser.c
blobdf1c7e9cc2ddf380efa594aa016d834ec7fbb87a
1 /* Tree browser.
2 Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
3 Contributed by Sebastian Pop <s.pop@laposte.net>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 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.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "errors.h"
27 #include "tree.h"
28 #include "tree-inline.h"
29 #include "diagnostic.h"
30 #include "hashtab.h"
33 #define TB_OUT_FILE stdout
34 #define TB_IN_FILE stdin
35 #define TB_NIY fprintf (TB_OUT_FILE, "Sorry this command is not yet implemented.\n")
36 #define TB_WF fprintf (TB_OUT_FILE, "Warning, this command failed.\n")
39 /* Structures for handling Tree Browser's commands. */
40 #define DEFTBCODE(COMMAND, STRING, HELP) COMMAND,
41 enum TB_Comm_code {
42 #include "tree-browser.def"
43 TB_UNUSED_COMMAND
45 #undef DEFTBCODE
46 typedef enum TB_Comm_code TB_CODE;
48 struct tb_command {
49 const char *help_msg;
50 const char *comm_text;
51 size_t comm_len;
52 TB_CODE comm_code;
55 #define DEFTBCODE(code, str, help) { help, str, sizeof(str) - 1, code },
56 static const struct tb_command tb_commands[] =
58 #include "tree-browser.def"
60 #undef DEFTBCODE
62 #define TB_COMMAND_LEN(N) (tb_commands[N].comm_len)
63 #define TB_COMMAND_TEXT(N) (tb_commands[N].comm_text)
64 #define TB_COMMAND_CODE(N) (tb_commands[N].comm_code)
65 #define TB_COMMAND_HELP(N) (tb_commands[N].help_msg)
68 /* Next structure is for parsing TREE_CODEs. */
69 struct tb_tree_code {
70 enum tree_code code;
71 const char *code_string;
72 size_t code_string_len;
75 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) { SYM, STRING, sizeof (STRING) - 1 },
76 static const struct tb_tree_code tb_tree_codes[] =
78 #include "tree.def"
80 #undef DEFTREECODE
82 #define TB_TREE_CODE(N) (tb_tree_codes[N].code)
83 #define TB_TREE_CODE_TEXT(N) (tb_tree_codes[N].code_string)
84 #define TB_TREE_CODE_LEN(N) (tb_tree_codes[N].code_string_len)
87 /* Function declarations. */
89 static long TB_getline (char **, long *, FILE *);
90 static TB_CODE TB_get_command (char *);
91 static enum tree_code TB_get_tree_code (char *);
92 static tree find_node_with_code (tree *, int *, void *);
93 static tree store_child_info (tree *, int *, void *);
94 static void TB_update_up (tree);
95 static tree TB_current_chain_node (tree);
96 static tree TB_prev_expr (tree);
97 static tree TB_next_expr (tree);
98 static tree TB_up_expr (tree);
99 static tree TB_first_in_bind (tree);
100 static tree TB_last_in_bind (tree);
101 static int TB_parent_eq (const void *, const void *);
102 static tree TB_history_prev (void);
104 /* FIXME: To be declared in a .h file. */
105 void browse_tree (tree);
107 /* Static variables. */
108 static htab_t TB_up_ht;
109 static tree TB_history_stack = NULL_TREE;
110 static int TB_verbose = 1;
113 /* Entry point in the Tree Browser. */
115 void
116 browse_tree (tree begin)
118 tree head;
119 TB_CODE tbc = TB_UNUSED_COMMAND;
120 ssize_t rd;
121 char *input = NULL;
122 long input_size = 0;
124 fprintf (TB_OUT_FILE, "\nTree Browser\n");
126 #define TB_SET_HEAD(N) do { \
127 TB_history_stack = tree_cons (NULL_TREE, (N), TB_history_stack); \
128 head = N; \
129 if (TB_verbose) \
130 if (head) \
132 print_generic_expr (TB_OUT_FILE, head, 0); \
133 fprintf (TB_OUT_FILE, "\n"); \
135 } while (0)
137 TB_SET_HEAD (begin);
139 /* Store in a hashtable information about previous and upper statements. */
141 TB_up_ht = htab_create (1023, htab_hash_pointer, &TB_parent_eq, NULL);
142 TB_update_up (head);
145 while (24)
147 fprintf (TB_OUT_FILE, "TB> ");
148 rd = TB_getline (&input, &input_size, TB_IN_FILE);
150 if (rd == -1)
151 /* EOF. */
152 goto ret;
154 if (rd != 1)
155 /* Get a new command. Otherwise the user just pressed enter, and thus
156 she expects the last command to be reexecuted. */
157 tbc = TB_get_command (input);
159 switch (tbc)
161 case TB_UPDATE_UP:
162 TB_update_up (head);
163 break;
165 case TB_MAX:
166 if (head && (INTEGRAL_TYPE_P (head)
167 || TREE_CODE (head) == REAL_TYPE))
168 TB_SET_HEAD (TYPE_MAX_VALUE (head));
169 else
170 TB_WF;
171 break;
173 case TB_MIN:
174 if (head && (INTEGRAL_TYPE_P (head)
175 || TREE_CODE (head) == REAL_TYPE))
176 TB_SET_HEAD (TYPE_MIN_VALUE (head));
177 else
178 TB_WF;
179 break;
181 case TB_ELT:
182 if (head && TREE_CODE (head) == TREE_VEC)
184 /* This command takes another argument: the element number:
185 for example "elt 1". */
186 TB_NIY;
188 else if (head && TREE_CODE (head) == VECTOR_CST)
190 /* This command takes another argument: the element number:
191 for example "elt 1". */
192 TB_NIY;
194 else
195 TB_WF;
196 break;
198 case TB_VALUE:
199 if (head && TREE_CODE (head) == TREE_LIST)
200 TB_SET_HEAD (TREE_VALUE (head));
201 else
202 TB_WF;
203 break;
205 case TB_PURPOSE:
206 if (head && TREE_CODE (head) == TREE_LIST)
207 TB_SET_HEAD (TREE_PURPOSE (head));
208 else
209 TB_WF;
210 break;
212 case TB_IMAG:
213 if (head && TREE_CODE (head) == COMPLEX_CST)
214 TB_SET_HEAD (TREE_IMAGPART (head));
215 else
216 TB_WF;
217 break;
219 case TB_REAL:
220 if (head && TREE_CODE (head) == COMPLEX_CST)
221 TB_SET_HEAD (TREE_REALPART (head));
222 else
223 TB_WF;
224 break;
226 case TB_BLOCK:
227 if (head && TREE_CODE (head) == BIND_EXPR)
228 TB_SET_HEAD (TREE_OPERAND (head, 2));
229 else
230 TB_WF;
231 break;
233 case TB_SUBBLOCKS:
234 if (head && TREE_CODE (head) == BLOCK)
235 TB_SET_HEAD (BLOCK_SUBBLOCKS (head));
236 else
237 TB_WF;
238 break;
240 case TB_SUPERCONTEXT:
241 if (head && TREE_CODE (head) == BLOCK)
242 TB_SET_HEAD (BLOCK_SUPERCONTEXT (head));
243 else
244 TB_WF;
245 break;
247 case TB_VARS:
248 if (head && TREE_CODE (head) == BLOCK)
249 TB_SET_HEAD (BLOCK_VARS (head));
250 else if (head && TREE_CODE (head) == BIND_EXPR)
251 TB_SET_HEAD (TREE_OPERAND (head, 0));
252 else
253 TB_WF;
254 break;
256 case TB_REFERENCE_TO_THIS:
257 if (head && TYPE_P (head))
258 TB_SET_HEAD (TYPE_REFERENCE_TO (head));
259 else
260 TB_WF;
261 break;
263 case TB_POINTER_TO_THIS:
264 if (head && TYPE_P (head))
265 TB_SET_HEAD (TYPE_POINTER_TO (head));
266 else
267 TB_WF;
268 break;
270 case TB_BASETYPE:
271 if (head && TREE_CODE (head) == OFFSET_TYPE)
272 TB_SET_HEAD (TYPE_OFFSET_BASETYPE (head));
273 else
274 TB_WF;
275 break;
277 case TB_ARG_TYPES:
278 if (head && (TREE_CODE (head) == FUNCTION_TYPE
279 || TREE_CODE (head) == METHOD_TYPE))
280 TB_SET_HEAD (TYPE_ARG_TYPES (head));
281 else
282 TB_WF;
283 break;
285 case TB_METHOD_BASE_TYPE:
286 if (head && (TREE_CODE (head) == FUNCTION_TYPE
287 || TREE_CODE (head) == METHOD_TYPE)
288 && TYPE_METHOD_BASETYPE (head))
289 TB_SET_HEAD (TYPE_METHOD_BASETYPE (head));
290 else
291 TB_WF;
292 break;
294 case TB_FIELDS:
295 if (head && (TREE_CODE (head) == RECORD_TYPE
296 || TREE_CODE (head) == UNION_TYPE
297 || TREE_CODE (head) == QUAL_UNION_TYPE))
298 TB_SET_HEAD (TYPE_FIELDS (head));
299 else
300 TB_WF;
301 break;
303 case TB_DOMAIN:
304 if (head && TREE_CODE (head) == ARRAY_TYPE)
305 TB_SET_HEAD (TYPE_DOMAIN (head));
306 else
307 TB_WF;
308 break;
310 case TB_VALUES:
311 if (head && TREE_CODE (head) == ENUMERAL_TYPE)
312 TB_SET_HEAD (TYPE_VALUES (head));
313 else
314 TB_WF;
315 break;
317 case TB_ARG_TYPE_AS_WRITTEN:
318 if (head && TREE_CODE (head) == PARM_DECL)
319 TB_SET_HEAD (DECL_ARG_TYPE_AS_WRITTEN (head));
320 else
321 TB_WF;
322 break;
324 case TB_ARG_TYPE:
325 if (head && TREE_CODE (head) == PARM_DECL)
326 TB_SET_HEAD (DECL_ARG_TYPE (head));
327 else
328 TB_WF;
329 break;
331 case TB_INITIAL:
332 if (head && DECL_P (head))
333 TB_SET_HEAD (DECL_INITIAL (head));
334 else
335 TB_WF;
336 break;
338 case TB_RESULT:
339 if (head && DECL_P (head))
340 TB_SET_HEAD (DECL_RESULT_FLD (head));
341 else
342 TB_WF;
343 break;
345 case TB_ARGUMENTS:
346 if (head && DECL_P (head))
347 TB_SET_HEAD (DECL_ARGUMENTS (head));
348 else
349 TB_WF;
350 break;
352 case TB_ABSTRACT_ORIGIN:
353 if (head && DECL_P (head))
354 TB_SET_HEAD (DECL_ABSTRACT_ORIGIN (head));
355 else if (head && TREE_CODE (head) == BLOCK)
356 TB_SET_HEAD (BLOCK_ABSTRACT_ORIGIN (head));
357 else
358 TB_WF;
359 break;
361 case TB_ATTRIBUTES:
362 if (head && DECL_P (head))
363 TB_SET_HEAD (DECL_ATTRIBUTES (head));
364 else if (head && TYPE_P (head))
365 TB_SET_HEAD (TYPE_ATTRIBUTES (head));
366 else
367 TB_WF;
368 break;
370 case TB_CONTEXT:
371 if (head && DECL_P (head))
372 TB_SET_HEAD (DECL_CONTEXT (head));
373 else if (head && TYPE_P (head)
374 && TYPE_CONTEXT (head))
375 TB_SET_HEAD (TYPE_CONTEXT (head));
376 else
377 TB_WF;
378 break;
380 case TB_OFFSET:
381 if (head && TREE_CODE (head) == FIELD_DECL)
382 TB_SET_HEAD (DECL_FIELD_OFFSET (head));
383 else
384 TB_WF;
385 break;
387 case TB_BIT_OFFSET:
388 if (head && TREE_CODE (head) == FIELD_DECL)
389 TB_SET_HEAD (DECL_FIELD_BIT_OFFSET (head));
390 else
391 TB_WF;
392 break;
394 case TB_UNIT_SIZE:
395 if (head && DECL_P (head))
396 TB_SET_HEAD (DECL_SIZE_UNIT (head));
397 else if (head && TYPE_P (head))
398 TB_SET_HEAD (TYPE_SIZE_UNIT (head));
399 else
400 TB_WF;
401 break;
403 case TB_SIZE:
404 if (head && DECL_P (head))
405 TB_SET_HEAD (DECL_SIZE (head));
406 else if (head && TYPE_P (head))
407 TB_SET_HEAD (TYPE_SIZE (head));
408 else
409 TB_WF;
410 break;
412 case TB_TYPE:
413 if (head && TREE_TYPE (head))
414 TB_SET_HEAD (TREE_TYPE (head));
415 else
416 TB_WF;
417 break;
419 case TB_DECL_SAVED_TREE:
420 if (head && TREE_CODE (head) == FUNCTION_DECL
421 && DECL_SAVED_TREE (head))
422 TB_SET_HEAD (DECL_SAVED_TREE (head));
423 else
424 TB_WF;
425 break;
427 case TB_BODY:
428 if (head && TREE_CODE (head) == BIND_EXPR)
429 TB_SET_HEAD (TREE_OPERAND (head, 1));
430 else
431 TB_WF;
432 break;
434 case TB_CHILD_0:
435 if (head && EXPR_P (head) && TREE_OPERAND (head, 0))
436 TB_SET_HEAD (TREE_OPERAND (head, 0));
437 else
438 TB_WF;
439 break;
441 case TB_CHILD_1:
442 if (head && EXPR_P (head) && TREE_OPERAND (head, 1))
443 TB_SET_HEAD (TREE_OPERAND (head, 1));
444 else
445 TB_WF;
446 break;
448 case TB_CHILD_2:
449 if (head && EXPR_P (head) && TREE_OPERAND (head, 2))
450 TB_SET_HEAD (TREE_OPERAND (head, 2));
451 else
452 TB_WF;
453 break;
455 case TB_CHILD_3:
456 if (head && EXPR_P (head) && TREE_OPERAND (head, 3))
457 TB_SET_HEAD (TREE_OPERAND (head, 3));
458 else
459 TB_WF;
460 break;
462 case TB_PRINT:
463 if (head)
464 debug_tree (head);
465 else
466 TB_WF;
467 break;
469 case TB_PRETTY_PRINT:
470 if (head)
472 print_generic_stmt (TB_OUT_FILE, head, 0);
473 fprintf (TB_OUT_FILE, "\n");
475 else
476 TB_WF;
477 break;
479 case TB_SEARCH_NAME:
481 break;
483 case TB_SEARCH_CODE:
485 enum tree_code code;
486 char *arg_text;
488 arg_text = strchr (input, ' ');
489 if (arg_text == NULL)
491 fprintf (TB_OUT_FILE, "First argument is missing. This isn't a valid search command. \n");
492 break;
494 code = TB_get_tree_code (arg_text + 1);
496 /* Search in the subtree a node with the given code. */
498 tree res;
500 res = walk_tree (&head, find_node_with_code, &code, NULL);
501 if (res == NULL_TREE)
503 fprintf (TB_OUT_FILE, "There's no node with this code (reachable via the walk_tree function from this node).\n");
505 else
507 fprintf (TB_OUT_FILE, "Achoo! I got this node in the tree.\n");
508 TB_SET_HEAD (res);
511 break;
514 #define TB_MOVE_HEAD(FCT) do { \
515 if (head) \
517 tree t; \
518 t = FCT (head); \
519 if (t) \
520 TB_SET_HEAD (t); \
521 else \
522 TB_WF; \
524 else \
525 TB_WF; \
526 } while (0)
528 case TB_FIRST:
529 TB_MOVE_HEAD (TB_first_in_bind);
530 break;
532 case TB_LAST:
533 TB_MOVE_HEAD (TB_last_in_bind);
534 break;
536 case TB_UP:
537 TB_MOVE_HEAD (TB_up_expr);
538 break;
540 case TB_PREV:
541 TB_MOVE_HEAD (TB_prev_expr);
542 break;
544 case TB_NEXT:
545 TB_MOVE_HEAD (TB_next_expr);
546 break;
548 case TB_HPREV:
549 /* This command is a little bit special, since it deals with history
550 stack. For this reason it should keep the "head = ..." statement
551 and not use TB_MOVE_HEAD. */
552 if (head)
554 tree t;
555 t = TB_history_prev ();
556 if (t)
558 head = t;
559 if (TB_verbose)
561 print_generic_expr (TB_OUT_FILE, head, 0);
562 fprintf (TB_OUT_FILE, "\n");
565 else
566 TB_WF;
568 else
569 TB_WF;
570 break;
572 case TB_CHAIN:
573 /* Don't go further if it's the last node in this chain. */
574 if (head && TREE_CODE (head) == BLOCK)
575 TB_SET_HEAD (BLOCK_CHAIN (head));
576 else if (head && TREE_CHAIN (head))
577 TB_SET_HEAD (TREE_CHAIN (head));
578 else
579 TB_WF;
580 break;
582 case TB_FUN:
583 /* Go up to the current function declaration. */
584 TB_SET_HEAD (current_function_decl);
585 fprintf (TB_OUT_FILE, "Current function declaration.\n");
586 break;
588 case TB_HELP:
589 /* Display a help message. */
591 int i;
592 fprintf (TB_OUT_FILE, "Possible commands are:\n\n");
593 for (i = 0; i < TB_UNUSED_COMMAND; i++)
595 fprintf (TB_OUT_FILE, "%20s - %s\n", TB_COMMAND_TEXT (i), TB_COMMAND_HELP (i));
598 break;
600 case TB_VERBOSE:
601 if (TB_verbose == 0)
603 TB_verbose = 1;
604 fprintf (TB_OUT_FILE, "Verbose on.\n");
606 else
608 TB_verbose = 0;
609 fprintf (TB_OUT_FILE, "Verbose off.\n");
611 break;
613 case TB_EXIT:
614 case TB_QUIT:
615 /* Just exit from this function. */
616 goto ret;
618 default:
619 TB_NIY;
623 ret:;
624 htab_delete (TB_up_ht);
625 return;
629 /* Search the first node in this BIND_EXPR. */
631 static tree
632 TB_first_in_bind (tree node)
634 tree t;
636 if (node == NULL_TREE)
637 return NULL_TREE;
639 while ((t = TB_prev_expr (node)))
640 node = t;
642 return node;
645 /* Search the last node in this BIND_EXPR. */
647 static tree
648 TB_last_in_bind (tree node)
650 tree t;
652 if (node == NULL_TREE)
653 return NULL_TREE;
655 while ((t = TB_next_expr (node)))
656 node = t;
658 return node;
661 /* Search the parent expression for this node. */
663 static tree
664 TB_up_expr (tree node)
666 tree res;
667 if (node == NULL_TREE)
668 return NULL_TREE;
670 res = (tree) htab_find (TB_up_ht, node);
671 return res;
674 /* Search the previous expression in this BIND_EXPR. */
676 static tree
677 TB_prev_expr (tree node)
679 node = TB_current_chain_node (node);
681 if (node == NULL_TREE)
682 return NULL_TREE;
684 node = TB_up_expr (node);
685 if (node && TREE_CODE (node) == COMPOUND_EXPR)
686 return node;
687 else
688 return NULL_TREE;
691 /* Search the next expression in this BIND_EXPR. */
693 static tree
694 TB_next_expr (tree node)
696 node = TB_current_chain_node (node);
698 if (node == NULL_TREE)
699 return NULL_TREE;
701 node = TREE_OPERAND (node, 1);
702 return node;
705 static tree
706 TB_current_chain_node (tree node)
708 if (node == NULL_TREE)
709 return NULL_TREE;
711 if (TREE_CODE (node) == COMPOUND_EXPR)
712 return node;
714 node = TB_up_expr (node);
715 if (node)
717 if (TREE_CODE (node) == COMPOUND_EXPR)
718 return node;
720 node = TB_up_expr (node);
721 if (TREE_CODE (node) == COMPOUND_EXPR)
722 return node;
725 return NULL_TREE;
728 /* For each node store in its children nodes that the current node is their
729 parent. This function is used by walk_tree. */
731 static tree
732 store_child_info (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
733 void *data ATTRIBUTE_UNUSED)
735 tree node;
736 void **slot;
738 node = *tp;
740 /* 'node' is the parent of 'TREE_OPERAND (node, *)'. */
741 if (EXPRESSION_CLASS_P (node))
744 #define STORE_CHILD(N) do { \
745 tree op = TREE_OPERAND (node, N); \
746 slot = htab_find_slot (TB_up_ht, op, INSERT); \
747 *slot = (void *) node; \
748 } while (0)
750 switch (TREE_CODE_LENGTH (TREE_CODE (node)))
752 case 4:
753 STORE_CHILD (0);
754 STORE_CHILD (1);
755 STORE_CHILD (2);
756 STORE_CHILD (3);
757 break;
759 case 3:
760 STORE_CHILD (0);
761 STORE_CHILD (1);
762 STORE_CHILD (2);
763 break;
765 case 2:
766 STORE_CHILD (0);
767 STORE_CHILD (1);
768 break;
770 case 1:
771 STORE_CHILD (0);
772 break;
774 case 0:
775 default:
776 /* No children: nothing to do. */
777 break;
779 #undef STORE_CHILD
782 /* Never stop walk_tree. */
783 return NULL_TREE;
786 /* Function used in TB_up_ht. */
788 static int
789 TB_parent_eq (const void *p1, const void *p2)
791 tree node, parent;
792 node = (tree) p2;
793 parent = (tree) p1;
795 if (p1 == NULL || p2 == NULL)
796 return 0;
798 if (EXPRESSION_CLASS_P (parent))
801 #define TEST_CHILD(N) do { \
802 if (node == TREE_OPERAND (parent, N)) \
803 return 1; \
804 } while (0)
806 switch (TREE_CODE_LENGTH (TREE_CODE (parent)))
808 case 4:
809 TEST_CHILD (0);
810 TEST_CHILD (1);
811 TEST_CHILD (2);
812 TEST_CHILD (3);
813 break;
815 case 3:
816 TEST_CHILD (0);
817 TEST_CHILD (1);
818 TEST_CHILD (2);
819 break;
821 case 2:
822 TEST_CHILD (0);
823 TEST_CHILD (1);
824 break;
826 case 1:
827 TEST_CHILD (0);
828 break;
830 case 0:
831 default:
832 /* No children: nothing to do. */
833 break;
835 #undef TEST_CHILD
838 return 0;
841 /* Update information about upper expressions in the hash table. */
843 static void
844 TB_update_up (tree node)
846 while (node)
848 walk_tree (&node, store_child_info, NULL, NULL);
850 /* Walk function's body. */
851 if (TREE_CODE (node) == FUNCTION_DECL)
852 if (DECL_SAVED_TREE (node))
853 walk_tree (&DECL_SAVED_TREE (node), store_child_info, NULL, NULL);
855 /* Walk rest of the chain. */
856 node = TREE_CHAIN (node);
858 fprintf (TB_OUT_FILE, "Up/prev expressions updated.\n");
861 /* Parse the input string for determining the command the user asked for. */
863 static TB_CODE
864 TB_get_command (char *input)
866 unsigned int mn, size_tok;
867 int comp;
868 char *space;
870 space = strchr (input, ' ');
871 if (space != NULL)
872 size_tok = strlen (input) - strlen (space);
873 else
874 size_tok = strlen (input) - 1;
876 for (mn = 0; mn < TB_UNUSED_COMMAND; mn++)
878 if (size_tok != TB_COMMAND_LEN (mn))
879 continue;
881 comp = memcmp (input, TB_COMMAND_TEXT (mn), TB_COMMAND_LEN (mn));
882 if (comp == 0)
883 /* Here we just determined the command. If this command takes
884 an argument, then the argument is determined later. */
885 return TB_COMMAND_CODE (mn);
888 /* Not a valid command. */
889 return TB_UNUSED_COMMAND;
892 /* Parse the input string for determining the tree code. */
894 static enum tree_code
895 TB_get_tree_code (char *input)
897 unsigned int mn, size_tok;
898 int comp;
899 char *space;
901 space = strchr (input, ' ');
902 if (space != NULL)
903 size_tok = strlen (input) - strlen (space);
904 else
905 size_tok = strlen (input) - 1;
907 for (mn = 0; mn < LAST_AND_UNUSED_TREE_CODE; mn++)
909 if (size_tok != TB_TREE_CODE_LEN (mn))
910 continue;
912 comp = memcmp (input, TB_TREE_CODE_TEXT (mn), TB_TREE_CODE_LEN (mn));
913 if (comp == 0)
915 fprintf (TB_OUT_FILE, "%s\n", TB_TREE_CODE_TEXT (mn));
916 return TB_TREE_CODE (mn);
920 /* This isn't a valid code. */
921 return LAST_AND_UNUSED_TREE_CODE;
924 /* Find a node with a given code. This function is used as an argument to
925 walk_tree. */
927 static tree
928 find_node_with_code (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
929 void *data)
931 enum tree_code *code;
932 code = (enum tree_code *) data;
933 if (*code == TREE_CODE (*tp))
934 return *tp;
936 return NULL_TREE;
939 /* Returns a pointer to the last visited node. */
941 static tree
942 TB_history_prev (void)
944 if (TB_history_stack)
946 TB_history_stack = TREE_CHAIN (TB_history_stack);
947 if (TB_history_stack)
948 return TREE_VALUE (TB_history_stack);
950 return NULL_TREE;
953 /* Read up to (and including) a '\n' from STREAM into *LINEPTR
954 (and null-terminate it). *LINEPTR is a pointer returned from malloc
955 (or NULL), pointing to *N characters of space. It is realloc'd as
956 necessary. Returns the number of characters read (not including the
957 null terminator), or -1 on error or EOF.
958 This function comes from sed (and is supposed to be a portable version
959 of getline). */
961 static long
962 TB_getline (char **lineptr, long *n, FILE *stream)
964 char *line, *p;
965 long size, copy;
967 if (lineptr == NULL || n == NULL)
969 errno = EINVAL;
970 return -1;
973 if (ferror (stream))
974 return -1;
976 /* Make sure we have a line buffer to start with. */
977 if (*lineptr == NULL || *n < 2) /* !seen and no buf yet need 2 chars. */
979 #ifndef MAX_CANON
980 #define MAX_CANON 256
981 #endif
982 line = (char *) xrealloc (*lineptr, MAX_CANON);
983 if (line == NULL)
984 return -1;
985 *lineptr = line;
986 *n = MAX_CANON;
989 line = *lineptr;
990 size = *n;
992 copy = size;
993 p = line;
995 while (1)
997 long len;
999 while (--copy > 0)
1001 register int c = getc (stream);
1002 if (c == EOF)
1003 goto lose;
1004 else if ((*p++ = c) == '\n')
1005 goto win;
1008 /* Need to enlarge the line buffer. */
1009 len = p - line;
1010 size *= 2;
1011 line = (char *) xrealloc (line, size);
1012 if (line == NULL)
1013 goto lose;
1014 *lineptr = line;
1015 *n = size;
1016 p = line + len;
1017 copy = size - len;
1020 lose:
1021 if (p == *lineptr)
1022 return -1;
1024 /* Return a partial line since we got an error in the middle. */
1025 win:
1026 #if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) || defined(MSDOS)
1027 if (p - 2 >= *lineptr && p[-2] == '\r')
1028 p[-2] = p[-1], --p;
1029 #endif
1030 *p = '\0';
1031 return p - *lineptr;