2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / tree-browser.c
blob76526d6ac4784499966ffa0d4bcaf7b978fd07ea
1 /* Tree browser.
2 Copyright (C) 2002-2015 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 "input.h"
25 #include "alias.h"
26 #include "symtab.h"
27 #include "options.h"
28 #include "tree.h"
29 #include "tree-pretty-print.h"
30 #include "print-tree.h"
32 #define TB_OUT_FILE stdout
33 #define TB_IN_FILE stdin
34 #define TB_NIY fprintf (TB_OUT_FILE, "Sorry this command is not yet implemented.\n")
35 #define TB_WF fprintf (TB_OUT_FILE, "Warning, this command failed.\n")
37 /* Structures for handling Tree Browser's commands. */
38 #define DEFTBCODE(COMMAND, STRING, HELP) COMMAND,
39 enum TB_Comm_code {
40 #include "tree-browser.def"
41 TB_UNUSED_COMMAND
43 #undef DEFTBCODE
44 typedef enum TB_Comm_code TB_CODE;
46 struct tb_command {
47 const char *help_msg;
48 const char *comm_text;
49 size_t comm_len;
50 TB_CODE comm_code;
53 #define DEFTBCODE(code, str, help) { help, str, sizeof (str) - 1, code },
54 static const struct tb_command tb_commands[] =
56 #include "tree-browser.def"
58 #undef DEFTBCODE
60 #define TB_COMMAND_LEN(N) (tb_commands[N].comm_len)
61 #define TB_COMMAND_TEXT(N) (tb_commands[N].comm_text)
62 #define TB_COMMAND_CODE(N) (tb_commands[N].comm_code)
63 #define TB_COMMAND_HELP(N) (tb_commands[N].help_msg)
66 /* Next structure is for parsing TREE_CODEs. */
67 struct tb_tree_code {
68 enum tree_code code;
69 const char *code_string;
70 size_t code_string_len;
73 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) { SYM, STRING, sizeof (STRING) - 1 },
74 #define END_OF_BASE_TREE_CODES \
75 { LAST_AND_UNUSED_TREE_CODE, "@dummy", sizeof ("@dummy") - 1 },
76 static const struct tb_tree_code tb_tree_codes[] =
78 #include "all-tree.def"
80 #undef DEFTREECODE
81 #undef END_OF_BASE_TREE_CODES
83 #define TB_TREE_CODE(N) (tb_tree_codes[N].code)
84 #define TB_TREE_CODE_TEXT(N) (tb_tree_codes[N].code_string)
85 #define TB_TREE_CODE_LEN(N) (tb_tree_codes[N].code_string_len)
88 /* Function declarations. */
90 static long TB_getline (char **, long *, FILE *);
91 static TB_CODE TB_get_command (char *);
92 static enum tree_code TB_get_tree_code (char *);
93 static tree find_node_with_code (tree *, int *, void *);
94 static tree store_child_info (tree *, int *, void *);
95 static void TB_update_up (tree);
96 static tree TB_current_chain_node (tree);
97 static tree TB_prev_expr (tree);
98 static tree TB_next_expr (tree);
99 static tree TB_up_expr (tree);
100 static tree TB_first_in_bind (tree);
101 static tree TB_last_in_bind (tree);
102 static tree TB_history_prev (void);
104 /* FIXME: To be declared in a .h file. */
105 void browse_tree (tree);
107 /* Hashtable helpers. */
108 struct tree_upper_hasher : pointer_hash<tree_node>
110 static inline bool equal (const value_type &, const compare_type &);
113 inline bool
114 tree_upper_hasher::equal (const value_type &parent, const compare_type &node)
116 if (parent == NULL || node == NULL)
117 return 0;
119 if (EXPR_P (parent))
121 int n = TREE_OPERAND_LENGTH (parent);
122 int i;
123 for (i = 0; i < n; i++)
124 if (node == TREE_OPERAND (parent, i))
125 return true;
127 return false;
130 /* Static variables. */
131 static hash_table<tree_upper_hasher> *TB_up_ht;
132 static vec<tree, va_gc> *TB_history_stack;
133 static int TB_verbose = 1;
136 /* Entry point in the Tree Browser. */
138 void
139 browse_tree (tree begin)
141 tree head;
142 TB_CODE tbc = TB_UNUSED_COMMAND;
143 ssize_t rd;
144 char *input = NULL;
145 long input_size = 0;
147 fprintf (TB_OUT_FILE, "\nTree Browser\n");
149 #define TB_SET_HEAD(N) do { \
150 vec_safe_push (TB_history_stack, N); \
151 head = N; \
152 if (TB_verbose) \
153 if (head) \
155 print_generic_expr (TB_OUT_FILE, head, 0); \
156 fprintf (TB_OUT_FILE, "\n"); \
158 } while (0)
160 TB_SET_HEAD (begin);
162 /* Store in a hashtable information about previous and upper statements. */
164 TB_up_ht = new hash_table<tree_upper_hasher> (1023);
165 TB_update_up (head);
168 while (24)
170 fprintf (TB_OUT_FILE, "TB> ");
171 rd = TB_getline (&input, &input_size, TB_IN_FILE);
173 if (rd == -1)
174 /* EOF. */
175 goto ret;
177 if (rd != 1)
178 /* Get a new command. Otherwise the user just pressed enter, and thus
179 she expects the last command to be reexecuted. */
180 tbc = TB_get_command (input);
182 switch (tbc)
184 case TB_UPDATE_UP:
185 TB_update_up (head);
186 break;
188 case TB_MAX:
189 if (head && (INTEGRAL_TYPE_P (head)
190 || TREE_CODE (head) == REAL_TYPE
191 || TREE_CODE (head) == FIXED_POINT_TYPE))
192 TB_SET_HEAD (TYPE_MAX_VALUE (head));
193 else
194 TB_WF;
195 break;
197 case TB_MIN:
198 if (head && (INTEGRAL_TYPE_P (head)
199 || TREE_CODE (head) == REAL_TYPE
200 || TREE_CODE (head) == FIXED_POINT_TYPE))
201 TB_SET_HEAD (TYPE_MIN_VALUE (head));
202 else
203 TB_WF;
204 break;
206 case TB_ELT:
207 if (head && TREE_CODE (head) == TREE_VEC)
209 /* This command takes another argument: the element number:
210 for example "elt 1". */
211 TB_NIY;
213 else if (head && TREE_CODE (head) == VECTOR_CST)
215 /* This command takes another argument: the element number:
216 for example "elt 1". */
217 TB_NIY;
219 else
220 TB_WF;
221 break;
223 case TB_VALUE:
224 if (head && TREE_CODE (head) == TREE_LIST)
225 TB_SET_HEAD (TREE_VALUE (head));
226 else
227 TB_WF;
228 break;
230 case TB_PURPOSE:
231 if (head && TREE_CODE (head) == TREE_LIST)
232 TB_SET_HEAD (TREE_PURPOSE (head));
233 else
234 TB_WF;
235 break;
237 case TB_IMAG:
238 if (head && TREE_CODE (head) == COMPLEX_CST)
239 TB_SET_HEAD (TREE_IMAGPART (head));
240 else
241 TB_WF;
242 break;
244 case TB_REAL:
245 if (head && TREE_CODE (head) == COMPLEX_CST)
246 TB_SET_HEAD (TREE_REALPART (head));
247 else
248 TB_WF;
249 break;
251 case TB_BLOCK:
252 if (head && TREE_CODE (head) == BIND_EXPR)
253 TB_SET_HEAD (TREE_OPERAND (head, 2));
254 else
255 TB_WF;
256 break;
258 case TB_SUBBLOCKS:
259 if (head && TREE_CODE (head) == BLOCK)
260 TB_SET_HEAD (BLOCK_SUBBLOCKS (head));
261 else
262 TB_WF;
263 break;
265 case TB_SUPERCONTEXT:
266 if (head && TREE_CODE (head) == BLOCK)
267 TB_SET_HEAD (BLOCK_SUPERCONTEXT (head));
268 else
269 TB_WF;
270 break;
272 case TB_VARS:
273 if (head && TREE_CODE (head) == BLOCK)
274 TB_SET_HEAD (BLOCK_VARS (head));
275 else if (head && TREE_CODE (head) == BIND_EXPR)
276 TB_SET_HEAD (TREE_OPERAND (head, 0));
277 else
278 TB_WF;
279 break;
281 case TB_REFERENCE_TO_THIS:
282 if (head && TYPE_P (head))
283 TB_SET_HEAD (TYPE_REFERENCE_TO (head));
284 else
285 TB_WF;
286 break;
288 case TB_POINTER_TO_THIS:
289 if (head && TYPE_P (head))
290 TB_SET_HEAD (TYPE_POINTER_TO (head));
291 else
292 TB_WF;
293 break;
295 case TB_BASETYPE:
296 if (head && TREE_CODE (head) == OFFSET_TYPE)
297 TB_SET_HEAD (TYPE_OFFSET_BASETYPE (head));
298 else
299 TB_WF;
300 break;
302 case TB_ARG_TYPES:
303 if (head && (TREE_CODE (head) == FUNCTION_TYPE
304 || TREE_CODE (head) == METHOD_TYPE))
305 TB_SET_HEAD (TYPE_ARG_TYPES (head));
306 else
307 TB_WF;
308 break;
310 case TB_METHOD_BASE_TYPE:
311 if (head && (TREE_CODE (head) == FUNCTION_TYPE
312 || TREE_CODE (head) == METHOD_TYPE)
313 && TYPE_METHOD_BASETYPE (head))
314 TB_SET_HEAD (TYPE_METHOD_BASETYPE (head));
315 else
316 TB_WF;
317 break;
319 case TB_FIELDS:
320 if (head && (TREE_CODE (head) == RECORD_TYPE
321 || TREE_CODE (head) == UNION_TYPE
322 || TREE_CODE (head) == QUAL_UNION_TYPE))
323 TB_SET_HEAD (TYPE_FIELDS (head));
324 else
325 TB_WF;
326 break;
328 case TB_DOMAIN:
329 if (head && TREE_CODE (head) == ARRAY_TYPE)
330 TB_SET_HEAD (TYPE_DOMAIN (head));
331 else
332 TB_WF;
333 break;
335 case TB_VALUES:
336 if (head && TREE_CODE (head) == ENUMERAL_TYPE)
337 TB_SET_HEAD (TYPE_VALUES (head));
338 else
339 TB_WF;
340 break;
342 case TB_ARG_TYPE:
343 if (head && TREE_CODE (head) == PARM_DECL)
344 TB_SET_HEAD (DECL_ARG_TYPE (head));
345 else
346 TB_WF;
347 break;
349 case TB_INITIAL:
350 if (head && DECL_P (head))
351 TB_SET_HEAD (DECL_INITIAL (head));
352 else
353 TB_WF;
354 break;
356 case TB_RESULT:
357 if (head && DECL_P (head))
358 TB_SET_HEAD (DECL_RESULT_FLD (head));
359 else
360 TB_WF;
361 break;
363 case TB_ARGUMENTS:
364 if (head && DECL_P (head))
365 TB_SET_HEAD (DECL_ARGUMENTS (head));
366 else
367 TB_WF;
368 break;
370 case TB_ABSTRACT_ORIGIN:
371 if (head && DECL_P (head))
372 TB_SET_HEAD (DECL_ABSTRACT_ORIGIN (head));
373 else if (head && TREE_CODE (head) == BLOCK)
374 TB_SET_HEAD (BLOCK_ABSTRACT_ORIGIN (head));
375 else
376 TB_WF;
377 break;
379 case TB_ATTRIBUTES:
380 if (head && DECL_P (head))
381 TB_SET_HEAD (DECL_ATTRIBUTES (head));
382 else if (head && TYPE_P (head))
383 TB_SET_HEAD (TYPE_ATTRIBUTES (head));
384 else
385 TB_WF;
386 break;
388 case TB_CONTEXT:
389 if (head && DECL_P (head))
390 TB_SET_HEAD (DECL_CONTEXT (head));
391 else if (head && TYPE_P (head)
392 && TYPE_CONTEXT (head))
393 TB_SET_HEAD (TYPE_CONTEXT (head));
394 else
395 TB_WF;
396 break;
398 case TB_OFFSET:
399 if (head && TREE_CODE (head) == FIELD_DECL)
400 TB_SET_HEAD (DECL_FIELD_OFFSET (head));
401 else
402 TB_WF;
403 break;
405 case TB_BIT_OFFSET:
406 if (head && TREE_CODE (head) == FIELD_DECL)
407 TB_SET_HEAD (DECL_FIELD_BIT_OFFSET (head));
408 else
409 TB_WF;
410 break;
412 case TB_UNIT_SIZE:
413 if (head && DECL_P (head))
414 TB_SET_HEAD (DECL_SIZE_UNIT (head));
415 else if (head && TYPE_P (head))
416 TB_SET_HEAD (TYPE_SIZE_UNIT (head));
417 else
418 TB_WF;
419 break;
421 case TB_SIZE:
422 if (head && DECL_P (head))
423 TB_SET_HEAD (DECL_SIZE (head));
424 else if (head && TYPE_P (head))
425 TB_SET_HEAD (TYPE_SIZE (head));
426 else
427 TB_WF;
428 break;
430 case TB_TYPE:
431 if (head && TREE_TYPE (head))
432 TB_SET_HEAD (TREE_TYPE (head));
433 else
434 TB_WF;
435 break;
437 case TB_DECL_SAVED_TREE:
438 if (head && TREE_CODE (head) == FUNCTION_DECL
439 && DECL_SAVED_TREE (head))
440 TB_SET_HEAD (DECL_SAVED_TREE (head));
441 else
442 TB_WF;
443 break;
445 case TB_BODY:
446 if (head && TREE_CODE (head) == BIND_EXPR)
447 TB_SET_HEAD (TREE_OPERAND (head, 1));
448 else
449 TB_WF;
450 break;
452 case TB_CHILD_0:
453 if (head && EXPR_P (head) && TREE_OPERAND (head, 0))
454 TB_SET_HEAD (TREE_OPERAND (head, 0));
455 else
456 TB_WF;
457 break;
459 case TB_CHILD_1:
460 if (head && EXPR_P (head) && TREE_OPERAND (head, 1))
461 TB_SET_HEAD (TREE_OPERAND (head, 1));
462 else
463 TB_WF;
464 break;
466 case TB_CHILD_2:
467 if (head && EXPR_P (head) && TREE_OPERAND (head, 2))
468 TB_SET_HEAD (TREE_OPERAND (head, 2));
469 else
470 TB_WF;
471 break;
473 case TB_CHILD_3:
474 if (head && EXPR_P (head) && TREE_OPERAND (head, 3))
475 TB_SET_HEAD (TREE_OPERAND (head, 3));
476 else
477 TB_WF;
478 break;
480 case TB_PRINT:
481 if (head)
482 debug_tree (head);
483 else
484 TB_WF;
485 break;
487 case TB_PRETTY_PRINT:
488 if (head)
490 print_generic_stmt (TB_OUT_FILE, head, 0);
491 fprintf (TB_OUT_FILE, "\n");
493 else
494 TB_WF;
495 break;
497 case TB_SEARCH_NAME:
499 break;
501 case TB_SEARCH_CODE:
503 enum tree_code code;
504 char *arg_text;
506 arg_text = strchr (input, ' ');
507 if (arg_text == NULL)
509 fprintf (TB_OUT_FILE, "First argument is missing. This isn't a valid search command. \n");
510 break;
512 code = TB_get_tree_code (arg_text + 1);
514 /* Search in the subtree a node with the given code. */
516 tree res;
518 res = walk_tree (&head, find_node_with_code, &code, NULL);
519 if (res == NULL_TREE)
521 fprintf (TB_OUT_FILE, "There's no node with this code (reachable via the walk_tree function from this node).\n");
523 else
525 fprintf (TB_OUT_FILE, "Achoo! I got this node in the tree.\n");
526 TB_SET_HEAD (res);
529 break;
532 #define TB_MOVE_HEAD(FCT) do { \
533 if (head) \
535 tree t; \
536 t = FCT (head); \
537 if (t) \
538 TB_SET_HEAD (t); \
539 else \
540 TB_WF; \
542 else \
543 TB_WF; \
544 } while (0)
546 case TB_FIRST:
547 TB_MOVE_HEAD (TB_first_in_bind);
548 break;
550 case TB_LAST:
551 TB_MOVE_HEAD (TB_last_in_bind);
552 break;
554 case TB_UP:
555 TB_MOVE_HEAD (TB_up_expr);
556 break;
558 case TB_PREV:
559 TB_MOVE_HEAD (TB_prev_expr);
560 break;
562 case TB_NEXT:
563 TB_MOVE_HEAD (TB_next_expr);
564 break;
566 case TB_HPREV:
567 /* This command is a little bit special, since it deals with history
568 stack. For this reason it should keep the "head = ..." statement
569 and not use TB_MOVE_HEAD. */
570 if (head)
572 tree t;
573 t = TB_history_prev ();
574 if (t)
576 head = t;
577 if (TB_verbose)
579 print_generic_expr (TB_OUT_FILE, head, 0);
580 fprintf (TB_OUT_FILE, "\n");
583 else
584 TB_WF;
586 else
587 TB_WF;
588 break;
590 case TB_CHAIN:
591 /* Don't go further if it's the last node in this chain. */
592 if (head && TREE_CODE (head) == BLOCK)
593 TB_SET_HEAD (BLOCK_CHAIN (head));
594 else if (head && TREE_CHAIN (head))
595 TB_SET_HEAD (TREE_CHAIN (head));
596 else
597 TB_WF;
598 break;
600 case TB_FUN:
601 /* Go up to the current function declaration. */
602 TB_SET_HEAD (current_function_decl);
603 fprintf (TB_OUT_FILE, "Current function declaration.\n");
604 break;
606 case TB_HELP:
607 /* Display a help message. */
609 int i;
610 fprintf (TB_OUT_FILE, "Possible commands are:\n\n");
611 for (i = 0; i < TB_UNUSED_COMMAND; i++)
613 fprintf (TB_OUT_FILE, "%20s - %s\n", TB_COMMAND_TEXT (i), TB_COMMAND_HELP (i));
616 break;
618 case TB_VERBOSE:
619 if (TB_verbose == 0)
621 TB_verbose = 1;
622 fprintf (TB_OUT_FILE, "Verbose on.\n");
624 else
626 TB_verbose = 0;
627 fprintf (TB_OUT_FILE, "Verbose off.\n");
629 break;
631 case TB_EXIT:
632 case TB_QUIT:
633 /* Just exit from this function. */
634 goto ret;
636 default:
637 TB_NIY;
641 ret:;
642 delete TB_up_ht;
643 TB_up_ht = NULL;
644 return;
648 /* Search the first node in this BIND_EXPR. */
650 static tree
651 TB_first_in_bind (tree node)
653 tree t;
655 if (node == NULL_TREE)
656 return NULL_TREE;
658 while ((t = TB_prev_expr (node)))
659 node = t;
661 return node;
664 /* Search the last node in this BIND_EXPR. */
666 static tree
667 TB_last_in_bind (tree node)
669 tree t;
671 if (node == NULL_TREE)
672 return NULL_TREE;
674 while ((t = TB_next_expr (node)))
675 node = t;
677 return node;
680 /* Search the parent expression for this node. */
682 static tree
683 TB_up_expr (tree node)
685 tree res;
686 if (node == NULL_TREE)
687 return NULL_TREE;
689 res = TB_up_ht->find (node);
690 return res;
693 /* Search the previous expression in this BIND_EXPR. */
695 static tree
696 TB_prev_expr (tree node)
698 node = TB_current_chain_node (node);
700 if (node == NULL_TREE)
701 return NULL_TREE;
703 node = TB_up_expr (node);
704 if (node && TREE_CODE (node) == COMPOUND_EXPR)
705 return node;
706 else
707 return NULL_TREE;
710 /* Search the next expression in this BIND_EXPR. */
712 static tree
713 TB_next_expr (tree node)
715 node = TB_current_chain_node (node);
717 if (node == NULL_TREE)
718 return NULL_TREE;
720 node = TREE_OPERAND (node, 1);
721 return node;
724 static tree
725 TB_current_chain_node (tree node)
727 if (node == NULL_TREE)
728 return NULL_TREE;
730 if (TREE_CODE (node) == COMPOUND_EXPR)
731 return node;
733 node = TB_up_expr (node);
734 if (node)
736 if (TREE_CODE (node) == COMPOUND_EXPR)
737 return node;
739 node = TB_up_expr (node);
740 if (TREE_CODE (node) == COMPOUND_EXPR)
741 return node;
744 return NULL_TREE;
747 /* For each node store in its children nodes that the current node is their
748 parent. This function is used by walk_tree. */
750 static tree
751 store_child_info (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
752 void *data ATTRIBUTE_UNUSED)
754 tree node;
755 tree_node **slot;
757 node = *tp;
759 /* 'node' is the parent of 'TREE_OPERAND (node, *)'. */
760 if (EXPR_P (node))
762 int n = TREE_OPERAND_LENGTH (node);
763 int i;
764 for (i = 0; i < n; i++)
766 tree op = TREE_OPERAND (node, i);
767 slot = TB_up_ht->find_slot (op, INSERT);
768 *slot = node;
772 /* Never stop walk_tree. */
773 return NULL_TREE;
776 /* Update information about upper expressions in the hash table. */
778 static void
779 TB_update_up (tree node)
781 while (node)
783 walk_tree (&node, store_child_info, NULL, NULL);
785 /* Walk function's body. */
786 if (TREE_CODE (node) == FUNCTION_DECL)
787 if (DECL_SAVED_TREE (node))
788 walk_tree (&DECL_SAVED_TREE (node), store_child_info, NULL, NULL);
790 /* Walk rest of the chain. */
791 node = TREE_CHAIN (node);
793 fprintf (TB_OUT_FILE, "Up/prev expressions updated.\n");
796 /* Parse the input string for determining the command the user asked for. */
798 static TB_CODE
799 TB_get_command (char *input)
801 unsigned int mn, size_tok;
802 int comp;
803 char *space;
805 space = strchr (input, ' ');
806 if (space != NULL)
807 size_tok = strlen (input) - strlen (space);
808 else
809 size_tok = strlen (input) - 1;
811 for (mn = 0; mn < TB_UNUSED_COMMAND; mn++)
813 if (size_tok != TB_COMMAND_LEN (mn))
814 continue;
816 comp = memcmp (input, TB_COMMAND_TEXT (mn), TB_COMMAND_LEN (mn));
817 if (comp == 0)
818 /* Here we just determined the command. If this command takes
819 an argument, then the argument is determined later. */
820 return TB_COMMAND_CODE (mn);
823 /* Not a valid command. */
824 return TB_UNUSED_COMMAND;
827 /* Parse the input string for determining the tree code. */
829 static enum tree_code
830 TB_get_tree_code (char *input)
832 unsigned int mn, size_tok;
833 int comp;
834 char *space;
836 space = strchr (input, ' ');
837 if (space != NULL)
838 size_tok = strlen (input) - strlen (space);
839 else
840 size_tok = strlen (input) - 1;
842 for (mn = 0; mn < LAST_AND_UNUSED_TREE_CODE; mn++)
844 if (size_tok != TB_TREE_CODE_LEN (mn))
845 continue;
847 comp = memcmp (input, TB_TREE_CODE_TEXT (mn), TB_TREE_CODE_LEN (mn));
848 if (comp == 0)
850 fprintf (TB_OUT_FILE, "%s\n", TB_TREE_CODE_TEXT (mn));
851 return TB_TREE_CODE (mn);
855 /* This isn't a valid code. */
856 return LAST_AND_UNUSED_TREE_CODE;
859 /* Find a node with a given code. This function is used as an argument to
860 walk_tree. */
862 static tree
863 find_node_with_code (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
864 void *data)
866 enum tree_code *code;
867 code = (enum tree_code *) data;
868 if (*code == TREE_CODE (*tp))
869 return *tp;
871 return NULL_TREE;
874 /* Returns a pointer to the last visited node. */
876 static tree
877 TB_history_prev (void)
879 if (!vec_safe_is_empty (TB_history_stack))
881 tree last = TB_history_stack->last ();
882 TB_history_stack->pop ();
883 return last;
885 return NULL_TREE;
888 /* Read up to (and including) a '\n' from STREAM into *LINEPTR
889 (and null-terminate it). *LINEPTR is a pointer returned from malloc
890 (or NULL), pointing to *N characters of space. It is realloc'd as
891 necessary. Returns the number of characters read (not including the
892 null terminator), or -1 on error or EOF.
893 This function comes from sed (and is supposed to be a portable version
894 of getline). */
896 static long
897 TB_getline (char **lineptr, long *n, FILE *stream)
899 char *line, *p;
900 long size, copy;
902 if (lineptr == NULL || n == NULL)
904 errno = EINVAL;
905 return -1;
908 if (ferror (stream))
909 return -1;
911 /* Make sure we have a line buffer to start with. */
912 if (*lineptr == NULL || *n < 2) /* !seen and no buf yet need 2 chars. */
914 #ifndef MAX_CANON
915 #define MAX_CANON 256
916 #endif
917 line = (char *) xrealloc (*lineptr, MAX_CANON);
918 if (line == NULL)
919 return -1;
920 *lineptr = line;
921 *n = MAX_CANON;
924 line = *lineptr;
925 size = *n;
927 copy = size;
928 p = line;
930 while (1)
932 long len;
934 while (--copy > 0)
936 register int c = getc (stream);
937 if (c == EOF)
938 goto lose;
939 else if ((*p++ = c) == '\n')
940 goto win;
943 /* Need to enlarge the line buffer. */
944 len = p - line;
945 size *= 2;
946 line = (char *) xrealloc (line, size);
947 if (line == NULL)
948 goto lose;
949 *lineptr = line;
950 *n = size;
951 p = line + len;
952 copy = size - len;
955 lose:
956 if (p == *lineptr)
957 return -1;
959 /* Return a partial line since we got an error in the middle. */
960 win:
961 #if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) || defined(MSDOS)
962 if (p - 2 >= *lineptr && p[-2] == '\r')
963 p[-2] = p[-1], --p;
964 #endif
965 *p = '\0';
966 return p - *lineptr;