2015-06-25 Zhouyi Zhou <yizhouzhou@ict.ac.cn>
[official-gcc.git] / gcc / tree-browser.c
blobbd2e0fa24b8a4203baed9a41e4cf738a752f7bd4
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 "alias.h"
25 #include "symtab.h"
26 #include "options.h"
27 #include "tree.h"
28 #include "tree-pretty-print.h"
29 #include "print-tree.h"
31 #define TB_OUT_FILE stdout
32 #define TB_IN_FILE stdin
33 #define TB_NIY fprintf (TB_OUT_FILE, "Sorry this command is not yet implemented.\n")
34 #define TB_WF fprintf (TB_OUT_FILE, "Warning, this command failed.\n")
36 /* Structures for handling Tree Browser's commands. */
37 #define DEFTBCODE(COMMAND, STRING, HELP) COMMAND,
38 enum TB_Comm_code {
39 #include "tree-browser.def"
40 TB_UNUSED_COMMAND
42 #undef DEFTBCODE
43 typedef enum TB_Comm_code TB_CODE;
45 struct tb_command {
46 const char *help_msg;
47 const char *comm_text;
48 size_t comm_len;
49 TB_CODE comm_code;
52 #define DEFTBCODE(code, str, help) { help, str, sizeof (str) - 1, code },
53 static const struct tb_command tb_commands[] =
55 #include "tree-browser.def"
57 #undef DEFTBCODE
59 #define TB_COMMAND_LEN(N) (tb_commands[N].comm_len)
60 #define TB_COMMAND_TEXT(N) (tb_commands[N].comm_text)
61 #define TB_COMMAND_CODE(N) (tb_commands[N].comm_code)
62 #define TB_COMMAND_HELP(N) (tb_commands[N].help_msg)
65 /* Next structure is for parsing TREE_CODEs. */
66 struct tb_tree_code {
67 enum tree_code code;
68 const char *code_string;
69 size_t code_string_len;
72 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) { SYM, STRING, sizeof (STRING) - 1 },
73 #define END_OF_BASE_TREE_CODES \
74 { LAST_AND_UNUSED_TREE_CODE, "@dummy", sizeof ("@dummy") - 1 },
75 static const struct tb_tree_code tb_tree_codes[] =
77 #include "all-tree.def"
79 #undef DEFTREECODE
80 #undef END_OF_BASE_TREE_CODES
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 tree TB_history_prev (void);
103 /* FIXME: To be declared in a .h file. */
104 void browse_tree (tree);
106 /* Hashtable helpers. */
107 struct tree_upper_hasher : pointer_hash<tree_node>
109 static inline bool equal (const value_type &, const compare_type &);
112 inline bool
113 tree_upper_hasher::equal (const value_type &parent, const compare_type &node)
115 if (parent == NULL || node == NULL)
116 return 0;
118 if (EXPR_P (parent))
120 int n = TREE_OPERAND_LENGTH (parent);
121 int i;
122 for (i = 0; i < n; i++)
123 if (node == TREE_OPERAND (parent, i))
124 return true;
126 return false;
129 /* Static variables. */
130 static hash_table<tree_upper_hasher> *TB_up_ht;
131 static vec<tree, va_gc> *TB_history_stack;
132 static int TB_verbose = 1;
135 /* Entry point in the Tree Browser. */
137 void
138 browse_tree (tree begin)
140 tree head;
141 TB_CODE tbc = TB_UNUSED_COMMAND;
142 ssize_t rd;
143 char *input = NULL;
144 long input_size = 0;
146 fprintf (TB_OUT_FILE, "\nTree Browser\n");
148 #define TB_SET_HEAD(N) do { \
149 vec_safe_push (TB_history_stack, N); \
150 head = N; \
151 if (TB_verbose) \
152 if (head) \
154 print_generic_expr (TB_OUT_FILE, head, 0); \
155 fprintf (TB_OUT_FILE, "\n"); \
157 } while (0)
159 TB_SET_HEAD (begin);
161 /* Store in a hashtable information about previous and upper statements. */
163 TB_up_ht = new hash_table<tree_upper_hasher> (1023);
164 TB_update_up (head);
167 while (24)
169 fprintf (TB_OUT_FILE, "TB> ");
170 rd = TB_getline (&input, &input_size, TB_IN_FILE);
172 if (rd == -1)
173 /* EOF. */
174 goto ret;
176 if (rd != 1)
177 /* Get a new command. Otherwise the user just pressed enter, and thus
178 she expects the last command to be reexecuted. */
179 tbc = TB_get_command (input);
181 switch (tbc)
183 case TB_UPDATE_UP:
184 TB_update_up (head);
185 break;
187 case TB_MAX:
188 if (head && (INTEGRAL_TYPE_P (head)
189 || TREE_CODE (head) == REAL_TYPE
190 || TREE_CODE (head) == FIXED_POINT_TYPE))
191 TB_SET_HEAD (TYPE_MAX_VALUE (head));
192 else
193 TB_WF;
194 break;
196 case TB_MIN:
197 if (head && (INTEGRAL_TYPE_P (head)
198 || TREE_CODE (head) == REAL_TYPE
199 || TREE_CODE (head) == FIXED_POINT_TYPE))
200 TB_SET_HEAD (TYPE_MIN_VALUE (head));
201 else
202 TB_WF;
203 break;
205 case TB_ELT:
206 if (head && TREE_CODE (head) == TREE_VEC)
208 /* This command takes another argument: the element number:
209 for example "elt 1". */
210 TB_NIY;
212 else if (head && TREE_CODE (head) == VECTOR_CST)
214 /* This command takes another argument: the element number:
215 for example "elt 1". */
216 TB_NIY;
218 else
219 TB_WF;
220 break;
222 case TB_VALUE:
223 if (head && TREE_CODE (head) == TREE_LIST)
224 TB_SET_HEAD (TREE_VALUE (head));
225 else
226 TB_WF;
227 break;
229 case TB_PURPOSE:
230 if (head && TREE_CODE (head) == TREE_LIST)
231 TB_SET_HEAD (TREE_PURPOSE (head));
232 else
233 TB_WF;
234 break;
236 case TB_IMAG:
237 if (head && TREE_CODE (head) == COMPLEX_CST)
238 TB_SET_HEAD (TREE_IMAGPART (head));
239 else
240 TB_WF;
241 break;
243 case TB_REAL:
244 if (head && TREE_CODE (head) == COMPLEX_CST)
245 TB_SET_HEAD (TREE_REALPART (head));
246 else
247 TB_WF;
248 break;
250 case TB_BLOCK:
251 if (head && TREE_CODE (head) == BIND_EXPR)
252 TB_SET_HEAD (TREE_OPERAND (head, 2));
253 else
254 TB_WF;
255 break;
257 case TB_SUBBLOCKS:
258 if (head && TREE_CODE (head) == BLOCK)
259 TB_SET_HEAD (BLOCK_SUBBLOCKS (head));
260 else
261 TB_WF;
262 break;
264 case TB_SUPERCONTEXT:
265 if (head && TREE_CODE (head) == BLOCK)
266 TB_SET_HEAD (BLOCK_SUPERCONTEXT (head));
267 else
268 TB_WF;
269 break;
271 case TB_VARS:
272 if (head && TREE_CODE (head) == BLOCK)
273 TB_SET_HEAD (BLOCK_VARS (head));
274 else if (head && TREE_CODE (head) == BIND_EXPR)
275 TB_SET_HEAD (TREE_OPERAND (head, 0));
276 else
277 TB_WF;
278 break;
280 case TB_REFERENCE_TO_THIS:
281 if (head && TYPE_P (head))
282 TB_SET_HEAD (TYPE_REFERENCE_TO (head));
283 else
284 TB_WF;
285 break;
287 case TB_POINTER_TO_THIS:
288 if (head && TYPE_P (head))
289 TB_SET_HEAD (TYPE_POINTER_TO (head));
290 else
291 TB_WF;
292 break;
294 case TB_BASETYPE:
295 if (head && TREE_CODE (head) == OFFSET_TYPE)
296 TB_SET_HEAD (TYPE_OFFSET_BASETYPE (head));
297 else
298 TB_WF;
299 break;
301 case TB_ARG_TYPES:
302 if (head && (TREE_CODE (head) == FUNCTION_TYPE
303 || TREE_CODE (head) == METHOD_TYPE))
304 TB_SET_HEAD (TYPE_ARG_TYPES (head));
305 else
306 TB_WF;
307 break;
309 case TB_METHOD_BASE_TYPE:
310 if (head && (TREE_CODE (head) == FUNCTION_TYPE
311 || TREE_CODE (head) == METHOD_TYPE)
312 && TYPE_METHOD_BASETYPE (head))
313 TB_SET_HEAD (TYPE_METHOD_BASETYPE (head));
314 else
315 TB_WF;
316 break;
318 case TB_FIELDS:
319 if (head && (TREE_CODE (head) == RECORD_TYPE
320 || TREE_CODE (head) == UNION_TYPE
321 || TREE_CODE (head) == QUAL_UNION_TYPE))
322 TB_SET_HEAD (TYPE_FIELDS (head));
323 else
324 TB_WF;
325 break;
327 case TB_DOMAIN:
328 if (head && TREE_CODE (head) == ARRAY_TYPE)
329 TB_SET_HEAD (TYPE_DOMAIN (head));
330 else
331 TB_WF;
332 break;
334 case TB_VALUES:
335 if (head && TREE_CODE (head) == ENUMERAL_TYPE)
336 TB_SET_HEAD (TYPE_VALUES (head));
337 else
338 TB_WF;
339 break;
341 case TB_ARG_TYPE:
342 if (head && TREE_CODE (head) == PARM_DECL)
343 TB_SET_HEAD (DECL_ARG_TYPE (head));
344 else
345 TB_WF;
346 break;
348 case TB_INITIAL:
349 if (head && DECL_P (head))
350 TB_SET_HEAD (DECL_INITIAL (head));
351 else
352 TB_WF;
353 break;
355 case TB_RESULT:
356 if (head && DECL_P (head))
357 TB_SET_HEAD (DECL_RESULT_FLD (head));
358 else
359 TB_WF;
360 break;
362 case TB_ARGUMENTS:
363 if (head && DECL_P (head))
364 TB_SET_HEAD (DECL_ARGUMENTS (head));
365 else
366 TB_WF;
367 break;
369 case TB_ABSTRACT_ORIGIN:
370 if (head && DECL_P (head))
371 TB_SET_HEAD (DECL_ABSTRACT_ORIGIN (head));
372 else if (head && TREE_CODE (head) == BLOCK)
373 TB_SET_HEAD (BLOCK_ABSTRACT_ORIGIN (head));
374 else
375 TB_WF;
376 break;
378 case TB_ATTRIBUTES:
379 if (head && DECL_P (head))
380 TB_SET_HEAD (DECL_ATTRIBUTES (head));
381 else if (head && TYPE_P (head))
382 TB_SET_HEAD (TYPE_ATTRIBUTES (head));
383 else
384 TB_WF;
385 break;
387 case TB_CONTEXT:
388 if (head && DECL_P (head))
389 TB_SET_HEAD (DECL_CONTEXT (head));
390 else if (head && TYPE_P (head)
391 && TYPE_CONTEXT (head))
392 TB_SET_HEAD (TYPE_CONTEXT (head));
393 else
394 TB_WF;
395 break;
397 case TB_OFFSET:
398 if (head && TREE_CODE (head) == FIELD_DECL)
399 TB_SET_HEAD (DECL_FIELD_OFFSET (head));
400 else
401 TB_WF;
402 break;
404 case TB_BIT_OFFSET:
405 if (head && TREE_CODE (head) == FIELD_DECL)
406 TB_SET_HEAD (DECL_FIELD_BIT_OFFSET (head));
407 else
408 TB_WF;
409 break;
411 case TB_UNIT_SIZE:
412 if (head && DECL_P (head))
413 TB_SET_HEAD (DECL_SIZE_UNIT (head));
414 else if (head && TYPE_P (head))
415 TB_SET_HEAD (TYPE_SIZE_UNIT (head));
416 else
417 TB_WF;
418 break;
420 case TB_SIZE:
421 if (head && DECL_P (head))
422 TB_SET_HEAD (DECL_SIZE (head));
423 else if (head && TYPE_P (head))
424 TB_SET_HEAD (TYPE_SIZE (head));
425 else
426 TB_WF;
427 break;
429 case TB_TYPE:
430 if (head && TREE_TYPE (head))
431 TB_SET_HEAD (TREE_TYPE (head));
432 else
433 TB_WF;
434 break;
436 case TB_DECL_SAVED_TREE:
437 if (head && TREE_CODE (head) == FUNCTION_DECL
438 && DECL_SAVED_TREE (head))
439 TB_SET_HEAD (DECL_SAVED_TREE (head));
440 else
441 TB_WF;
442 break;
444 case TB_BODY:
445 if (head && TREE_CODE (head) == BIND_EXPR)
446 TB_SET_HEAD (TREE_OPERAND (head, 1));
447 else
448 TB_WF;
449 break;
451 case TB_CHILD_0:
452 if (head && EXPR_P (head) && TREE_OPERAND (head, 0))
453 TB_SET_HEAD (TREE_OPERAND (head, 0));
454 else
455 TB_WF;
456 break;
458 case TB_CHILD_1:
459 if (head && EXPR_P (head) && TREE_OPERAND (head, 1))
460 TB_SET_HEAD (TREE_OPERAND (head, 1));
461 else
462 TB_WF;
463 break;
465 case TB_CHILD_2:
466 if (head && EXPR_P (head) && TREE_OPERAND (head, 2))
467 TB_SET_HEAD (TREE_OPERAND (head, 2));
468 else
469 TB_WF;
470 break;
472 case TB_CHILD_3:
473 if (head && EXPR_P (head) && TREE_OPERAND (head, 3))
474 TB_SET_HEAD (TREE_OPERAND (head, 3));
475 else
476 TB_WF;
477 break;
479 case TB_PRINT:
480 if (head)
481 debug_tree (head);
482 else
483 TB_WF;
484 break;
486 case TB_PRETTY_PRINT:
487 if (head)
489 print_generic_stmt (TB_OUT_FILE, head, 0);
490 fprintf (TB_OUT_FILE, "\n");
492 else
493 TB_WF;
494 break;
496 case TB_SEARCH_NAME:
498 break;
500 case TB_SEARCH_CODE:
502 enum tree_code code;
503 char *arg_text;
505 arg_text = strchr (input, ' ');
506 if (arg_text == NULL)
508 fprintf (TB_OUT_FILE, "First argument is missing. This isn't a valid search command. \n");
509 break;
511 code = TB_get_tree_code (arg_text + 1);
513 /* Search in the subtree a node with the given code. */
515 tree res;
517 res = walk_tree (&head, find_node_with_code, &code, NULL);
518 if (res == NULL_TREE)
520 fprintf (TB_OUT_FILE, "There's no node with this code (reachable via the walk_tree function from this node).\n");
522 else
524 fprintf (TB_OUT_FILE, "Achoo! I got this node in the tree.\n");
525 TB_SET_HEAD (res);
528 break;
531 #define TB_MOVE_HEAD(FCT) do { \
532 if (head) \
534 tree t; \
535 t = FCT (head); \
536 if (t) \
537 TB_SET_HEAD (t); \
538 else \
539 TB_WF; \
541 else \
542 TB_WF; \
543 } while (0)
545 case TB_FIRST:
546 TB_MOVE_HEAD (TB_first_in_bind);
547 break;
549 case TB_LAST:
550 TB_MOVE_HEAD (TB_last_in_bind);
551 break;
553 case TB_UP:
554 TB_MOVE_HEAD (TB_up_expr);
555 break;
557 case TB_PREV:
558 TB_MOVE_HEAD (TB_prev_expr);
559 break;
561 case TB_NEXT:
562 TB_MOVE_HEAD (TB_next_expr);
563 break;
565 case TB_HPREV:
566 /* This command is a little bit special, since it deals with history
567 stack. For this reason it should keep the "head = ..." statement
568 and not use TB_MOVE_HEAD. */
569 if (head)
571 tree t;
572 t = TB_history_prev ();
573 if (t)
575 head = t;
576 if (TB_verbose)
578 print_generic_expr (TB_OUT_FILE, head, 0);
579 fprintf (TB_OUT_FILE, "\n");
582 else
583 TB_WF;
585 else
586 TB_WF;
587 break;
589 case TB_CHAIN:
590 /* Don't go further if it's the last node in this chain. */
591 if (head && TREE_CODE (head) == BLOCK)
592 TB_SET_HEAD (BLOCK_CHAIN (head));
593 else if (head && TREE_CHAIN (head))
594 TB_SET_HEAD (TREE_CHAIN (head));
595 else
596 TB_WF;
597 break;
599 case TB_FUN:
600 /* Go up to the current function declaration. */
601 TB_SET_HEAD (current_function_decl);
602 fprintf (TB_OUT_FILE, "Current function declaration.\n");
603 break;
605 case TB_HELP:
606 /* Display a help message. */
608 int i;
609 fprintf (TB_OUT_FILE, "Possible commands are:\n\n");
610 for (i = 0; i < TB_UNUSED_COMMAND; i++)
612 fprintf (TB_OUT_FILE, "%20s - %s\n", TB_COMMAND_TEXT (i), TB_COMMAND_HELP (i));
615 break;
617 case TB_VERBOSE:
618 if (TB_verbose == 0)
620 TB_verbose = 1;
621 fprintf (TB_OUT_FILE, "Verbose on.\n");
623 else
625 TB_verbose = 0;
626 fprintf (TB_OUT_FILE, "Verbose off.\n");
628 break;
630 case TB_EXIT:
631 case TB_QUIT:
632 /* Just exit from this function. */
633 goto ret;
635 default:
636 TB_NIY;
640 ret:;
641 delete TB_up_ht;
642 TB_up_ht = NULL;
643 return;
647 /* Search the first node in this BIND_EXPR. */
649 static tree
650 TB_first_in_bind (tree node)
652 tree t;
654 if (node == NULL_TREE)
655 return NULL_TREE;
657 while ((t = TB_prev_expr (node)))
658 node = t;
660 return node;
663 /* Search the last node in this BIND_EXPR. */
665 static tree
666 TB_last_in_bind (tree node)
668 tree t;
670 if (node == NULL_TREE)
671 return NULL_TREE;
673 while ((t = TB_next_expr (node)))
674 node = t;
676 return node;
679 /* Search the parent expression for this node. */
681 static tree
682 TB_up_expr (tree node)
684 tree res;
685 if (node == NULL_TREE)
686 return NULL_TREE;
688 res = TB_up_ht->find (node);
689 return res;
692 /* Search the previous expression in this BIND_EXPR. */
694 static tree
695 TB_prev_expr (tree node)
697 node = TB_current_chain_node (node);
699 if (node == NULL_TREE)
700 return NULL_TREE;
702 node = TB_up_expr (node);
703 if (node && TREE_CODE (node) == COMPOUND_EXPR)
704 return node;
705 else
706 return NULL_TREE;
709 /* Search the next expression in this BIND_EXPR. */
711 static tree
712 TB_next_expr (tree node)
714 node = TB_current_chain_node (node);
716 if (node == NULL_TREE)
717 return NULL_TREE;
719 node = TREE_OPERAND (node, 1);
720 return node;
723 static tree
724 TB_current_chain_node (tree node)
726 if (node == NULL_TREE)
727 return NULL_TREE;
729 if (TREE_CODE (node) == COMPOUND_EXPR)
730 return node;
732 node = TB_up_expr (node);
733 if (node)
735 if (TREE_CODE (node) == COMPOUND_EXPR)
736 return node;
738 node = TB_up_expr (node);
739 if (TREE_CODE (node) == COMPOUND_EXPR)
740 return node;
743 return NULL_TREE;
746 /* For each node store in its children nodes that the current node is their
747 parent. This function is used by walk_tree. */
749 static tree
750 store_child_info (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
751 void *data ATTRIBUTE_UNUSED)
753 tree node;
754 tree_node **slot;
756 node = *tp;
758 /* 'node' is the parent of 'TREE_OPERAND (node, *)'. */
759 if (EXPR_P (node))
761 int n = TREE_OPERAND_LENGTH (node);
762 int i;
763 for (i = 0; i < n; i++)
765 tree op = TREE_OPERAND (node, i);
766 slot = TB_up_ht->find_slot (op, INSERT);
767 *slot = node;
771 /* Never stop walk_tree. */
772 return NULL_TREE;
775 /* Update information about upper expressions in the hash table. */
777 static void
778 TB_update_up (tree node)
780 while (node)
782 walk_tree (&node, store_child_info, NULL, NULL);
784 /* Walk function's body. */
785 if (TREE_CODE (node) == FUNCTION_DECL)
786 if (DECL_SAVED_TREE (node))
787 walk_tree (&DECL_SAVED_TREE (node), store_child_info, NULL, NULL);
789 /* Walk rest of the chain. */
790 node = TREE_CHAIN (node);
792 fprintf (TB_OUT_FILE, "Up/prev expressions updated.\n");
795 /* Parse the input string for determining the command the user asked for. */
797 static TB_CODE
798 TB_get_command (char *input)
800 unsigned int mn, size_tok;
801 int comp;
802 char *space;
804 space = strchr (input, ' ');
805 if (space != NULL)
806 size_tok = strlen (input) - strlen (space);
807 else
808 size_tok = strlen (input) - 1;
810 for (mn = 0; mn < TB_UNUSED_COMMAND; mn++)
812 if (size_tok != TB_COMMAND_LEN (mn))
813 continue;
815 comp = memcmp (input, TB_COMMAND_TEXT (mn), TB_COMMAND_LEN (mn));
816 if (comp == 0)
817 /* Here we just determined the command. If this command takes
818 an argument, then the argument is determined later. */
819 return TB_COMMAND_CODE (mn);
822 /* Not a valid command. */
823 return TB_UNUSED_COMMAND;
826 /* Parse the input string for determining the tree code. */
828 static enum tree_code
829 TB_get_tree_code (char *input)
831 unsigned int mn, size_tok;
832 int comp;
833 char *space;
835 space = strchr (input, ' ');
836 if (space != NULL)
837 size_tok = strlen (input) - strlen (space);
838 else
839 size_tok = strlen (input) - 1;
841 for (mn = 0; mn < LAST_AND_UNUSED_TREE_CODE; mn++)
843 if (size_tok != TB_TREE_CODE_LEN (mn))
844 continue;
846 comp = memcmp (input, TB_TREE_CODE_TEXT (mn), TB_TREE_CODE_LEN (mn));
847 if (comp == 0)
849 fprintf (TB_OUT_FILE, "%s\n", TB_TREE_CODE_TEXT (mn));
850 return TB_TREE_CODE (mn);
854 /* This isn't a valid code. */
855 return LAST_AND_UNUSED_TREE_CODE;
858 /* Find a node with a given code. This function is used as an argument to
859 walk_tree. */
861 static tree
862 find_node_with_code (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
863 void *data)
865 enum tree_code *code;
866 code = (enum tree_code *) data;
867 if (*code == TREE_CODE (*tp))
868 return *tp;
870 return NULL_TREE;
873 /* Returns a pointer to the last visited node. */
875 static tree
876 TB_history_prev (void)
878 if (!vec_safe_is_empty (TB_history_stack))
880 tree last = TB_history_stack->last ();
881 TB_history_stack->pop ();
882 return last;
884 return NULL_TREE;
887 /* Read up to (and including) a '\n' from STREAM into *LINEPTR
888 (and null-terminate it). *LINEPTR is a pointer returned from malloc
889 (or NULL), pointing to *N characters of space. It is realloc'd as
890 necessary. Returns the number of characters read (not including the
891 null terminator), or -1 on error or EOF.
892 This function comes from sed (and is supposed to be a portable version
893 of getline). */
895 static long
896 TB_getline (char **lineptr, long *n, FILE *stream)
898 char *line, *p;
899 long size, copy;
901 if (lineptr == NULL || n == NULL)
903 errno = EINVAL;
904 return -1;
907 if (ferror (stream))
908 return -1;
910 /* Make sure we have a line buffer to start with. */
911 if (*lineptr == NULL || *n < 2) /* !seen and no buf yet need 2 chars. */
913 #ifndef MAX_CANON
914 #define MAX_CANON 256
915 #endif
916 line = (char *) xrealloc (*lineptr, MAX_CANON);
917 if (line == NULL)
918 return -1;
919 *lineptr = line;
920 *n = MAX_CANON;
923 line = *lineptr;
924 size = *n;
926 copy = size;
927 p = line;
929 while (1)
931 long len;
933 while (--copy > 0)
935 register int c = getc (stream);
936 if (c == EOF)
937 goto lose;
938 else if ((*p++ = c) == '\n')
939 goto win;
942 /* Need to enlarge the line buffer. */
943 len = p - line;
944 size *= 2;
945 line = (char *) xrealloc (line, size);
946 if (line == NULL)
947 goto lose;
948 *lineptr = line;
949 *n = size;
950 p = line + len;
951 copy = size - len;
954 lose:
955 if (p == *lineptr)
956 return -1;
958 /* Return a partial line since we got an error in the middle. */
959 win:
960 #if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) || defined(MSDOS)
961 if (p - 2 >= *lineptr && p[-2] == '\r')
962 p[-2] = p[-1], --p;
963 #endif
964 *p = '\0';
965 return p - *lineptr;