gcc/ChangeLog
[official-gcc.git] / gcc / tree-browser.c
blobb236cabd8dff6b5389d267c13c4a4f91c6f097ba
1 /* Tree browser.
2 Copyright (C) 2002-2013 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 3, 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 COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "hash-table.h"
25 #include "tree.h"
26 #include "tree-pretty-print.h"
28 #define TB_OUT_FILE stdout
29 #define TB_IN_FILE stdin
30 #define TB_NIY fprintf (TB_OUT_FILE, "Sorry this command is not yet implemented.\n")
31 #define TB_WF fprintf (TB_OUT_FILE, "Warning, this command failed.\n")
33 /* Structures for handling Tree Browser's commands. */
34 #define DEFTBCODE(COMMAND, STRING, HELP) COMMAND,
35 enum TB_Comm_code {
36 #include "tree-browser.def"
37 TB_UNUSED_COMMAND
39 #undef DEFTBCODE
40 typedef enum TB_Comm_code TB_CODE;
42 struct tb_command {
43 const char *help_msg;
44 const char *comm_text;
45 size_t comm_len;
46 TB_CODE comm_code;
49 #define DEFTBCODE(code, str, help) { help, str, sizeof(str) - 1, code },
50 static const struct tb_command tb_commands[] =
52 #include "tree-browser.def"
54 #undef DEFTBCODE
56 #define TB_COMMAND_LEN(N) (tb_commands[N].comm_len)
57 #define TB_COMMAND_TEXT(N) (tb_commands[N].comm_text)
58 #define TB_COMMAND_CODE(N) (tb_commands[N].comm_code)
59 #define TB_COMMAND_HELP(N) (tb_commands[N].help_msg)
62 /* Next structure is for parsing TREE_CODEs. */
63 struct tb_tree_code {
64 enum tree_code code;
65 const char *code_string;
66 size_t code_string_len;
69 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) { SYM, STRING, sizeof (STRING) - 1 },
70 #define END_OF_BASE_TREE_CODES \
71 { LAST_AND_UNUSED_TREE_CODE, "@dummy", sizeof ("@dummy") - 1 },
72 static const struct tb_tree_code tb_tree_codes[] =
74 #include "all-tree.def"
76 #undef DEFTREECODE
77 #undef END_OF_BASE_TREE_CODES
79 #define TB_TREE_CODE(N) (tb_tree_codes[N].code)
80 #define TB_TREE_CODE_TEXT(N) (tb_tree_codes[N].code_string)
81 #define TB_TREE_CODE_LEN(N) (tb_tree_codes[N].code_string_len)
84 /* Function declarations. */
86 static long TB_getline (char **, long *, FILE *);
87 static TB_CODE TB_get_command (char *);
88 static enum tree_code TB_get_tree_code (char *);
89 static tree find_node_with_code (tree *, int *, void *);
90 static tree store_child_info (tree *, int *, void *);
91 static void TB_update_up (tree);
92 static tree TB_current_chain_node (tree);
93 static tree TB_prev_expr (tree);
94 static tree TB_next_expr (tree);
95 static tree TB_up_expr (tree);
96 static tree TB_first_in_bind (tree);
97 static tree TB_last_in_bind (tree);
98 static tree TB_history_prev (void);
100 /* FIXME: To be declared in a .h file. */
101 void browse_tree (tree);
103 /* Hashtable helpers. */
104 struct tree_upper_hasher : typed_noop_remove <tree_node>
106 typedef tree_node value_type;
107 typedef tree_node compare_type;
108 static inline hashval_t hash (const value_type *);
109 static inline bool equal (const value_type *, const compare_type *);
112 inline hashval_t
113 tree_upper_hasher::hash (const value_type *v)
115 return pointer_hash <value_type>::hash (v);
118 inline bool
119 tree_upper_hasher::equal (const value_type *parent, const compare_type *node)
121 if (parent == NULL || node == NULL)
122 return 0;
124 if (EXPR_P (parent))
126 int n = TREE_OPERAND_LENGTH (parent);
127 int i;
128 for (i = 0; i < n; i++)
129 if (node == TREE_OPERAND (parent, i))
130 return true;
132 return false;
135 /* Static variables. */
136 static hash_table <tree_upper_hasher> TB_up_ht;
137 static vec<tree, va_gc> *TB_history_stack;
138 static int TB_verbose = 1;
141 /* Entry point in the Tree Browser. */
143 void
144 browse_tree (tree begin)
146 tree head;
147 TB_CODE tbc = TB_UNUSED_COMMAND;
148 ssize_t rd;
149 char *input = NULL;
150 long input_size = 0;
152 fprintf (TB_OUT_FILE, "\nTree Browser\n");
154 #define TB_SET_HEAD(N) do { \
155 vec_safe_push (TB_history_stack, N); \
156 head = N; \
157 if (TB_verbose) \
158 if (head) \
160 print_generic_expr (TB_OUT_FILE, head, 0); \
161 fprintf (TB_OUT_FILE, "\n"); \
163 } while (0)
165 TB_SET_HEAD (begin);
167 /* Store in a hashtable information about previous and upper statements. */
169 TB_up_ht.create (1023);
170 TB_update_up (head);
173 while (24)
175 fprintf (TB_OUT_FILE, "TB> ");
176 rd = TB_getline (&input, &input_size, TB_IN_FILE);
178 if (rd == -1)
179 /* EOF. */
180 goto ret;
182 if (rd != 1)
183 /* Get a new command. Otherwise the user just pressed enter, and thus
184 she expects the last command to be reexecuted. */
185 tbc = TB_get_command (input);
187 switch (tbc)
189 case TB_UPDATE_UP:
190 TB_update_up (head);
191 break;
193 case TB_MAX:
194 if (head && (INTEGRAL_TYPE_P (head)
195 || TREE_CODE (head) == REAL_TYPE
196 || TREE_CODE (head) == FIXED_POINT_TYPE))
197 TB_SET_HEAD (TYPE_MAX_VALUE (head));
198 else
199 TB_WF;
200 break;
202 case TB_MIN:
203 if (head && (INTEGRAL_TYPE_P (head)
204 || TREE_CODE (head) == REAL_TYPE
205 || TREE_CODE (head) == FIXED_POINT_TYPE))
206 TB_SET_HEAD (TYPE_MIN_VALUE (head));
207 else
208 TB_WF;
209 break;
211 case TB_ELT:
212 if (head && TREE_CODE (head) == TREE_VEC)
214 /* This command takes another argument: the element number:
215 for example "elt 1". */
216 TB_NIY;
218 else if (head && TREE_CODE (head) == VECTOR_CST)
220 /* This command takes another argument: the element number:
221 for example "elt 1". */
222 TB_NIY;
224 else
225 TB_WF;
226 break;
228 case TB_VALUE:
229 if (head && TREE_CODE (head) == TREE_LIST)
230 TB_SET_HEAD (TREE_VALUE (head));
231 else
232 TB_WF;
233 break;
235 case TB_PURPOSE:
236 if (head && TREE_CODE (head) == TREE_LIST)
237 TB_SET_HEAD (TREE_PURPOSE (head));
238 else
239 TB_WF;
240 break;
242 case TB_IMAG:
243 if (head && TREE_CODE (head) == COMPLEX_CST)
244 TB_SET_HEAD (TREE_IMAGPART (head));
245 else
246 TB_WF;
247 break;
249 case TB_REAL:
250 if (head && TREE_CODE (head) == COMPLEX_CST)
251 TB_SET_HEAD (TREE_REALPART (head));
252 else
253 TB_WF;
254 break;
256 case TB_BLOCK:
257 if (head && TREE_CODE (head) == BIND_EXPR)
258 TB_SET_HEAD (TREE_OPERAND (head, 2));
259 else
260 TB_WF;
261 break;
263 case TB_SUBBLOCKS:
264 if (head && TREE_CODE (head) == BLOCK)
265 TB_SET_HEAD (BLOCK_SUBBLOCKS (head));
266 else
267 TB_WF;
268 break;
270 case TB_SUPERCONTEXT:
271 if (head && TREE_CODE (head) == BLOCK)
272 TB_SET_HEAD (BLOCK_SUPERCONTEXT (head));
273 else
274 TB_WF;
275 break;
277 case TB_VARS:
278 if (head && TREE_CODE (head) == BLOCK)
279 TB_SET_HEAD (BLOCK_VARS (head));
280 else if (head && TREE_CODE (head) == BIND_EXPR)
281 TB_SET_HEAD (TREE_OPERAND (head, 0));
282 else
283 TB_WF;
284 break;
286 case TB_REFERENCE_TO_THIS:
287 if (head && TYPE_P (head))
288 TB_SET_HEAD (TYPE_REFERENCE_TO (head));
289 else
290 TB_WF;
291 break;
293 case TB_POINTER_TO_THIS:
294 if (head && TYPE_P (head))
295 TB_SET_HEAD (TYPE_POINTER_TO (head));
296 else
297 TB_WF;
298 break;
300 case TB_BASETYPE:
301 if (head && TREE_CODE (head) == OFFSET_TYPE)
302 TB_SET_HEAD (TYPE_OFFSET_BASETYPE (head));
303 else
304 TB_WF;
305 break;
307 case TB_ARG_TYPES:
308 if (head && (TREE_CODE (head) == FUNCTION_TYPE
309 || TREE_CODE (head) == METHOD_TYPE))
310 TB_SET_HEAD (TYPE_ARG_TYPES (head));
311 else
312 TB_WF;
313 break;
315 case TB_METHOD_BASE_TYPE:
316 if (head && (TREE_CODE (head) == FUNCTION_TYPE
317 || TREE_CODE (head) == METHOD_TYPE)
318 && TYPE_METHOD_BASETYPE (head))
319 TB_SET_HEAD (TYPE_METHOD_BASETYPE (head));
320 else
321 TB_WF;
322 break;
324 case TB_FIELDS:
325 if (head && (TREE_CODE (head) == RECORD_TYPE
326 || TREE_CODE (head) == UNION_TYPE
327 || TREE_CODE (head) == QUAL_UNION_TYPE))
328 TB_SET_HEAD (TYPE_FIELDS (head));
329 else
330 TB_WF;
331 break;
333 case TB_DOMAIN:
334 if (head && TREE_CODE (head) == ARRAY_TYPE)
335 TB_SET_HEAD (TYPE_DOMAIN (head));
336 else
337 TB_WF;
338 break;
340 case TB_VALUES:
341 if (head && TREE_CODE (head) == ENUMERAL_TYPE)
342 TB_SET_HEAD (TYPE_VALUES (head));
343 else
344 TB_WF;
345 break;
347 case TB_ARG_TYPE:
348 if (head && TREE_CODE (head) == PARM_DECL)
349 TB_SET_HEAD (DECL_ARG_TYPE (head));
350 else
351 TB_WF;
352 break;
354 case TB_INITIAL:
355 if (head && DECL_P (head))
356 TB_SET_HEAD (DECL_INITIAL (head));
357 else
358 TB_WF;
359 break;
361 case TB_RESULT:
362 if (head && DECL_P (head))
363 TB_SET_HEAD (DECL_RESULT_FLD (head));
364 else
365 TB_WF;
366 break;
368 case TB_ARGUMENTS:
369 if (head && DECL_P (head))
370 TB_SET_HEAD (DECL_ARGUMENTS (head));
371 else
372 TB_WF;
373 break;
375 case TB_ABSTRACT_ORIGIN:
376 if (head && DECL_P (head))
377 TB_SET_HEAD (DECL_ABSTRACT_ORIGIN (head));
378 else if (head && TREE_CODE (head) == BLOCK)
379 TB_SET_HEAD (BLOCK_ABSTRACT_ORIGIN (head));
380 else
381 TB_WF;
382 break;
384 case TB_ATTRIBUTES:
385 if (head && DECL_P (head))
386 TB_SET_HEAD (DECL_ATTRIBUTES (head));
387 else if (head && TYPE_P (head))
388 TB_SET_HEAD (TYPE_ATTRIBUTES (head));
389 else
390 TB_WF;
391 break;
393 case TB_CONTEXT:
394 if (head && DECL_P (head))
395 TB_SET_HEAD (DECL_CONTEXT (head));
396 else if (head && TYPE_P (head)
397 && TYPE_CONTEXT (head))
398 TB_SET_HEAD (TYPE_CONTEXT (head));
399 else
400 TB_WF;
401 break;
403 case TB_OFFSET:
404 if (head && TREE_CODE (head) == FIELD_DECL)
405 TB_SET_HEAD (DECL_FIELD_OFFSET (head));
406 else
407 TB_WF;
408 break;
410 case TB_BIT_OFFSET:
411 if (head && TREE_CODE (head) == FIELD_DECL)
412 TB_SET_HEAD (DECL_FIELD_BIT_OFFSET (head));
413 else
414 TB_WF;
415 break;
417 case TB_UNIT_SIZE:
418 if (head && DECL_P (head))
419 TB_SET_HEAD (DECL_SIZE_UNIT (head));
420 else if (head && TYPE_P (head))
421 TB_SET_HEAD (TYPE_SIZE_UNIT (head));
422 else
423 TB_WF;
424 break;
426 case TB_SIZE:
427 if (head && DECL_P (head))
428 TB_SET_HEAD (DECL_SIZE (head));
429 else if (head && TYPE_P (head))
430 TB_SET_HEAD (TYPE_SIZE (head));
431 else
432 TB_WF;
433 break;
435 case TB_TYPE:
436 if (head && TREE_TYPE (head))
437 TB_SET_HEAD (TREE_TYPE (head));
438 else
439 TB_WF;
440 break;
442 case TB_DECL_SAVED_TREE:
443 if (head && TREE_CODE (head) == FUNCTION_DECL
444 && DECL_SAVED_TREE (head))
445 TB_SET_HEAD (DECL_SAVED_TREE (head));
446 else
447 TB_WF;
448 break;
450 case TB_BODY:
451 if (head && TREE_CODE (head) == BIND_EXPR)
452 TB_SET_HEAD (TREE_OPERAND (head, 1));
453 else
454 TB_WF;
455 break;
457 case TB_CHILD_0:
458 if (head && EXPR_P (head) && TREE_OPERAND (head, 0))
459 TB_SET_HEAD (TREE_OPERAND (head, 0));
460 else
461 TB_WF;
462 break;
464 case TB_CHILD_1:
465 if (head && EXPR_P (head) && TREE_OPERAND (head, 1))
466 TB_SET_HEAD (TREE_OPERAND (head, 1));
467 else
468 TB_WF;
469 break;
471 case TB_CHILD_2:
472 if (head && EXPR_P (head) && TREE_OPERAND (head, 2))
473 TB_SET_HEAD (TREE_OPERAND (head, 2));
474 else
475 TB_WF;
476 break;
478 case TB_CHILD_3:
479 if (head && EXPR_P (head) && TREE_OPERAND (head, 3))
480 TB_SET_HEAD (TREE_OPERAND (head, 3));
481 else
482 TB_WF;
483 break;
485 case TB_PRINT:
486 if (head)
487 debug_tree (head);
488 else
489 TB_WF;
490 break;
492 case TB_PRETTY_PRINT:
493 if (head)
495 print_generic_stmt (TB_OUT_FILE, head, 0);
496 fprintf (TB_OUT_FILE, "\n");
498 else
499 TB_WF;
500 break;
502 case TB_SEARCH_NAME:
504 break;
506 case TB_SEARCH_CODE:
508 enum tree_code code;
509 char *arg_text;
511 arg_text = strchr (input, ' ');
512 if (arg_text == NULL)
514 fprintf (TB_OUT_FILE, "First argument is missing. This isn't a valid search command. \n");
515 break;
517 code = TB_get_tree_code (arg_text + 1);
519 /* Search in the subtree a node with the given code. */
521 tree res;
523 res = walk_tree (&head, find_node_with_code, &code, NULL);
524 if (res == NULL_TREE)
526 fprintf (TB_OUT_FILE, "There's no node with this code (reachable via the walk_tree function from this node).\n");
528 else
530 fprintf (TB_OUT_FILE, "Achoo! I got this node in the tree.\n");
531 TB_SET_HEAD (res);
534 break;
537 #define TB_MOVE_HEAD(FCT) do { \
538 if (head) \
540 tree t; \
541 t = FCT (head); \
542 if (t) \
543 TB_SET_HEAD (t); \
544 else \
545 TB_WF; \
547 else \
548 TB_WF; \
549 } while (0)
551 case TB_FIRST:
552 TB_MOVE_HEAD (TB_first_in_bind);
553 break;
555 case TB_LAST:
556 TB_MOVE_HEAD (TB_last_in_bind);
557 break;
559 case TB_UP:
560 TB_MOVE_HEAD (TB_up_expr);
561 break;
563 case TB_PREV:
564 TB_MOVE_HEAD (TB_prev_expr);
565 break;
567 case TB_NEXT:
568 TB_MOVE_HEAD (TB_next_expr);
569 break;
571 case TB_HPREV:
572 /* This command is a little bit special, since it deals with history
573 stack. For this reason it should keep the "head = ..." statement
574 and not use TB_MOVE_HEAD. */
575 if (head)
577 tree t;
578 t = TB_history_prev ();
579 if (t)
581 head = t;
582 if (TB_verbose)
584 print_generic_expr (TB_OUT_FILE, head, 0);
585 fprintf (TB_OUT_FILE, "\n");
588 else
589 TB_WF;
591 else
592 TB_WF;
593 break;
595 case TB_CHAIN:
596 /* Don't go further if it's the last node in this chain. */
597 if (head && TREE_CODE (head) == BLOCK)
598 TB_SET_HEAD (BLOCK_CHAIN (head));
599 else if (head && TREE_CHAIN (head))
600 TB_SET_HEAD (TREE_CHAIN (head));
601 else
602 TB_WF;
603 break;
605 case TB_FUN:
606 /* Go up to the current function declaration. */
607 TB_SET_HEAD (current_function_decl);
608 fprintf (TB_OUT_FILE, "Current function declaration.\n");
609 break;
611 case TB_HELP:
612 /* Display a help message. */
614 int i;
615 fprintf (TB_OUT_FILE, "Possible commands are:\n\n");
616 for (i = 0; i < TB_UNUSED_COMMAND; i++)
618 fprintf (TB_OUT_FILE, "%20s - %s\n", TB_COMMAND_TEXT (i), TB_COMMAND_HELP (i));
621 break;
623 case TB_VERBOSE:
624 if (TB_verbose == 0)
626 TB_verbose = 1;
627 fprintf (TB_OUT_FILE, "Verbose on.\n");
629 else
631 TB_verbose = 0;
632 fprintf (TB_OUT_FILE, "Verbose off.\n");
634 break;
636 case TB_EXIT:
637 case TB_QUIT:
638 /* Just exit from this function. */
639 goto ret;
641 default:
642 TB_NIY;
646 ret:;
647 TB_up_ht.dispose ();
648 return;
652 /* Search the first node in this BIND_EXPR. */
654 static tree
655 TB_first_in_bind (tree node)
657 tree t;
659 if (node == NULL_TREE)
660 return NULL_TREE;
662 while ((t = TB_prev_expr (node)))
663 node = t;
665 return node;
668 /* Search the last node in this BIND_EXPR. */
670 static tree
671 TB_last_in_bind (tree node)
673 tree t;
675 if (node == NULL_TREE)
676 return NULL_TREE;
678 while ((t = TB_next_expr (node)))
679 node = t;
681 return node;
684 /* Search the parent expression for this node. */
686 static tree
687 TB_up_expr (tree node)
689 tree res;
690 if (node == NULL_TREE)
691 return NULL_TREE;
693 res = TB_up_ht.find (node);
694 return res;
697 /* Search the previous expression in this BIND_EXPR. */
699 static tree
700 TB_prev_expr (tree node)
702 node = TB_current_chain_node (node);
704 if (node == NULL_TREE)
705 return NULL_TREE;
707 node = TB_up_expr (node);
708 if (node && TREE_CODE (node) == COMPOUND_EXPR)
709 return node;
710 else
711 return NULL_TREE;
714 /* Search the next expression in this BIND_EXPR. */
716 static tree
717 TB_next_expr (tree node)
719 node = TB_current_chain_node (node);
721 if (node == NULL_TREE)
722 return NULL_TREE;
724 node = TREE_OPERAND (node, 1);
725 return node;
728 static tree
729 TB_current_chain_node (tree node)
731 if (node == NULL_TREE)
732 return NULL_TREE;
734 if (TREE_CODE (node) == COMPOUND_EXPR)
735 return node;
737 node = TB_up_expr (node);
738 if (node)
740 if (TREE_CODE (node) == COMPOUND_EXPR)
741 return node;
743 node = TB_up_expr (node);
744 if (TREE_CODE (node) == COMPOUND_EXPR)
745 return node;
748 return NULL_TREE;
751 /* For each node store in its children nodes that the current node is their
752 parent. This function is used by walk_tree. */
754 static tree
755 store_child_info (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
756 void *data ATTRIBUTE_UNUSED)
758 tree node;
759 tree_node **slot;
761 node = *tp;
763 /* 'node' is the parent of 'TREE_OPERAND (node, *)'. */
764 if (EXPR_P (node))
766 int n = TREE_OPERAND_LENGTH (node);
767 int i;
768 for (i = 0; i < n; i++)
770 tree op = TREE_OPERAND (node, i);
771 slot = TB_up_ht.find_slot (op, INSERT);
772 *slot = node;
776 /* Never stop walk_tree. */
777 return NULL_TREE;
780 /* Update information about upper expressions in the hash table. */
782 static void
783 TB_update_up (tree node)
785 while (node)
787 walk_tree (&node, store_child_info, NULL, NULL);
789 /* Walk function's body. */
790 if (TREE_CODE (node) == FUNCTION_DECL)
791 if (DECL_SAVED_TREE (node))
792 walk_tree (&DECL_SAVED_TREE (node), store_child_info, NULL, NULL);
794 /* Walk rest of the chain. */
795 node = TREE_CHAIN (node);
797 fprintf (TB_OUT_FILE, "Up/prev expressions updated.\n");
800 /* Parse the input string for determining the command the user asked for. */
802 static TB_CODE
803 TB_get_command (char *input)
805 unsigned int mn, size_tok;
806 int comp;
807 char *space;
809 space = strchr (input, ' ');
810 if (space != NULL)
811 size_tok = strlen (input) - strlen (space);
812 else
813 size_tok = strlen (input) - 1;
815 for (mn = 0; mn < TB_UNUSED_COMMAND; mn++)
817 if (size_tok != TB_COMMAND_LEN (mn))
818 continue;
820 comp = memcmp (input, TB_COMMAND_TEXT (mn), TB_COMMAND_LEN (mn));
821 if (comp == 0)
822 /* Here we just determined the command. If this command takes
823 an argument, then the argument is determined later. */
824 return TB_COMMAND_CODE (mn);
827 /* Not a valid command. */
828 return TB_UNUSED_COMMAND;
831 /* Parse the input string for determining the tree code. */
833 static enum tree_code
834 TB_get_tree_code (char *input)
836 unsigned int mn, size_tok;
837 int comp;
838 char *space;
840 space = strchr (input, ' ');
841 if (space != NULL)
842 size_tok = strlen (input) - strlen (space);
843 else
844 size_tok = strlen (input) - 1;
846 for (mn = 0; mn < LAST_AND_UNUSED_TREE_CODE; mn++)
848 if (size_tok != TB_TREE_CODE_LEN (mn))
849 continue;
851 comp = memcmp (input, TB_TREE_CODE_TEXT (mn), TB_TREE_CODE_LEN (mn));
852 if (comp == 0)
854 fprintf (TB_OUT_FILE, "%s\n", TB_TREE_CODE_TEXT (mn));
855 return TB_TREE_CODE (mn);
859 /* This isn't a valid code. */
860 return LAST_AND_UNUSED_TREE_CODE;
863 /* Find a node with a given code. This function is used as an argument to
864 walk_tree. */
866 static tree
867 find_node_with_code (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
868 void *data)
870 enum tree_code *code;
871 code = (enum tree_code *) data;
872 if (*code == TREE_CODE (*tp))
873 return *tp;
875 return NULL_TREE;
878 /* Returns a pointer to the last visited node. */
880 static tree
881 TB_history_prev (void)
883 if (!vec_safe_is_empty (TB_history_stack))
885 tree last = TB_history_stack->last ();
886 TB_history_stack->pop ();
887 return last;
889 return NULL_TREE;
892 /* Read up to (and including) a '\n' from STREAM into *LINEPTR
893 (and null-terminate it). *LINEPTR is a pointer returned from malloc
894 (or NULL), pointing to *N characters of space. It is realloc'd as
895 necessary. Returns the number of characters read (not including the
896 null terminator), or -1 on error or EOF.
897 This function comes from sed (and is supposed to be a portable version
898 of getline). */
900 static long
901 TB_getline (char **lineptr, long *n, FILE *stream)
903 char *line, *p;
904 long size, copy;
906 if (lineptr == NULL || n == NULL)
908 errno = EINVAL;
909 return -1;
912 if (ferror (stream))
913 return -1;
915 /* Make sure we have a line buffer to start with. */
916 if (*lineptr == NULL || *n < 2) /* !seen and no buf yet need 2 chars. */
918 #ifndef MAX_CANON
919 #define MAX_CANON 256
920 #endif
921 line = (char *) xrealloc (*lineptr, MAX_CANON);
922 if (line == NULL)
923 return -1;
924 *lineptr = line;
925 *n = MAX_CANON;
928 line = *lineptr;
929 size = *n;
931 copy = size;
932 p = line;
934 while (1)
936 long len;
938 while (--copy > 0)
940 register int c = getc (stream);
941 if (c == EOF)
942 goto lose;
943 else if ((*p++ = c) == '\n')
944 goto win;
947 /* Need to enlarge the line buffer. */
948 len = p - line;
949 size *= 2;
950 line = (char *) xrealloc (line, size);
951 if (line == NULL)
952 goto lose;
953 *lineptr = line;
954 *n = size;
955 p = line + len;
956 copy = size - len;
959 lose:
960 if (p == *lineptr)
961 return -1;
963 /* Return a partial line since we got an error in the middle. */
964 win:
965 #if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) || defined(MSDOS)
966 if (p - 2 >= *lineptr && p[-2] == '\r')
967 p[-2] = p[-1], --p;
968 #endif
969 *p = '\0';
970 return p - *lineptr;