* pt.c (lookup_template_class_1): Splice out abi_tag attribute if
[official-gcc.git] / gcc / tree-browser.c
blob2a4db1b309579c4737ee97047c76e42041ad9637
1 /* Tree browser.
2 Copyright (C) 2002-2014 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"
27 #include "print-tree.h"
29 #define TB_OUT_FILE stdout
30 #define TB_IN_FILE stdin
31 #define TB_NIY fprintf (TB_OUT_FILE, "Sorry this command is not yet implemented.\n")
32 #define TB_WF fprintf (TB_OUT_FILE, "Warning, this command failed.\n")
34 /* Structures for handling Tree Browser's commands. */
35 #define DEFTBCODE(COMMAND, STRING, HELP) COMMAND,
36 enum TB_Comm_code {
37 #include "tree-browser.def"
38 TB_UNUSED_COMMAND
40 #undef DEFTBCODE
41 typedef enum TB_Comm_code TB_CODE;
43 struct tb_command {
44 const char *help_msg;
45 const char *comm_text;
46 size_t comm_len;
47 TB_CODE comm_code;
50 #define DEFTBCODE(code, str, help) { help, str, sizeof (str) - 1, code },
51 static const struct tb_command tb_commands[] =
53 #include "tree-browser.def"
55 #undef DEFTBCODE
57 #define TB_COMMAND_LEN(N) (tb_commands[N].comm_len)
58 #define TB_COMMAND_TEXT(N) (tb_commands[N].comm_text)
59 #define TB_COMMAND_CODE(N) (tb_commands[N].comm_code)
60 #define TB_COMMAND_HELP(N) (tb_commands[N].help_msg)
63 /* Next structure is for parsing TREE_CODEs. */
64 struct tb_tree_code {
65 enum tree_code code;
66 const char *code_string;
67 size_t code_string_len;
70 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) { SYM, STRING, sizeof (STRING) - 1 },
71 #define END_OF_BASE_TREE_CODES \
72 { LAST_AND_UNUSED_TREE_CODE, "@dummy", sizeof ("@dummy") - 1 },
73 static const struct tb_tree_code tb_tree_codes[] =
75 #include "all-tree.def"
77 #undef DEFTREECODE
78 #undef END_OF_BASE_TREE_CODES
80 #define TB_TREE_CODE(N) (tb_tree_codes[N].code)
81 #define TB_TREE_CODE_TEXT(N) (tb_tree_codes[N].code_string)
82 #define TB_TREE_CODE_LEN(N) (tb_tree_codes[N].code_string_len)
85 /* Function declarations. */
87 static long TB_getline (char **, long *, FILE *);
88 static TB_CODE TB_get_command (char *);
89 static enum tree_code TB_get_tree_code (char *);
90 static tree find_node_with_code (tree *, int *, void *);
91 static tree store_child_info (tree *, int *, void *);
92 static void TB_update_up (tree);
93 static tree TB_current_chain_node (tree);
94 static tree TB_prev_expr (tree);
95 static tree TB_next_expr (tree);
96 static tree TB_up_expr (tree);
97 static tree TB_first_in_bind (tree);
98 static tree TB_last_in_bind (tree);
99 static tree TB_history_prev (void);
101 /* FIXME: To be declared in a .h file. */
102 void browse_tree (tree);
104 /* Hashtable helpers. */
105 struct tree_upper_hasher : pointer_hash<tree_node>
107 static inline bool equal (const value_type &, const compare_type &);
110 inline bool
111 tree_upper_hasher::equal (const value_type &parent, const compare_type &node)
113 if (parent == NULL || node == NULL)
114 return 0;
116 if (EXPR_P (parent))
118 int n = TREE_OPERAND_LENGTH (parent);
119 int i;
120 for (i = 0; i < n; i++)
121 if (node == TREE_OPERAND (parent, i))
122 return true;
124 return false;
127 /* Static variables. */
128 static hash_table<tree_upper_hasher> *TB_up_ht;
129 static vec<tree, va_gc> *TB_history_stack;
130 static int TB_verbose = 1;
133 /* Entry point in the Tree Browser. */
135 void
136 browse_tree (tree begin)
138 tree head;
139 TB_CODE tbc = TB_UNUSED_COMMAND;
140 ssize_t rd;
141 char *input = NULL;
142 long input_size = 0;
144 fprintf (TB_OUT_FILE, "\nTree Browser\n");
146 #define TB_SET_HEAD(N) do { \
147 vec_safe_push (TB_history_stack, N); \
148 head = N; \
149 if (TB_verbose) \
150 if (head) \
152 print_generic_expr (TB_OUT_FILE, head, 0); \
153 fprintf (TB_OUT_FILE, "\n"); \
155 } while (0)
157 TB_SET_HEAD (begin);
159 /* Store in a hashtable information about previous and upper statements. */
161 TB_up_ht = new hash_table<tree_upper_hasher> (1023);
162 TB_update_up (head);
165 while (24)
167 fprintf (TB_OUT_FILE, "TB> ");
168 rd = TB_getline (&input, &input_size, TB_IN_FILE);
170 if (rd == -1)
171 /* EOF. */
172 goto ret;
174 if (rd != 1)
175 /* Get a new command. Otherwise the user just pressed enter, and thus
176 she expects the last command to be reexecuted. */
177 tbc = TB_get_command (input);
179 switch (tbc)
181 case TB_UPDATE_UP:
182 TB_update_up (head);
183 break;
185 case TB_MAX:
186 if (head && (INTEGRAL_TYPE_P (head)
187 || TREE_CODE (head) == REAL_TYPE
188 || TREE_CODE (head) == FIXED_POINT_TYPE))
189 TB_SET_HEAD (TYPE_MAX_VALUE (head));
190 else
191 TB_WF;
192 break;
194 case TB_MIN:
195 if (head && (INTEGRAL_TYPE_P (head)
196 || TREE_CODE (head) == REAL_TYPE
197 || TREE_CODE (head) == FIXED_POINT_TYPE))
198 TB_SET_HEAD (TYPE_MIN_VALUE (head));
199 else
200 TB_WF;
201 break;
203 case TB_ELT:
204 if (head && TREE_CODE (head) == TREE_VEC)
206 /* This command takes another argument: the element number:
207 for example "elt 1". */
208 TB_NIY;
210 else if (head && TREE_CODE (head) == VECTOR_CST)
212 /* This command takes another argument: the element number:
213 for example "elt 1". */
214 TB_NIY;
216 else
217 TB_WF;
218 break;
220 case TB_VALUE:
221 if (head && TREE_CODE (head) == TREE_LIST)
222 TB_SET_HEAD (TREE_VALUE (head));
223 else
224 TB_WF;
225 break;
227 case TB_PURPOSE:
228 if (head && TREE_CODE (head) == TREE_LIST)
229 TB_SET_HEAD (TREE_PURPOSE (head));
230 else
231 TB_WF;
232 break;
234 case TB_IMAG:
235 if (head && TREE_CODE (head) == COMPLEX_CST)
236 TB_SET_HEAD (TREE_IMAGPART (head));
237 else
238 TB_WF;
239 break;
241 case TB_REAL:
242 if (head && TREE_CODE (head) == COMPLEX_CST)
243 TB_SET_HEAD (TREE_REALPART (head));
244 else
245 TB_WF;
246 break;
248 case TB_BLOCK:
249 if (head && TREE_CODE (head) == BIND_EXPR)
250 TB_SET_HEAD (TREE_OPERAND (head, 2));
251 else
252 TB_WF;
253 break;
255 case TB_SUBBLOCKS:
256 if (head && TREE_CODE (head) == BLOCK)
257 TB_SET_HEAD (BLOCK_SUBBLOCKS (head));
258 else
259 TB_WF;
260 break;
262 case TB_SUPERCONTEXT:
263 if (head && TREE_CODE (head) == BLOCK)
264 TB_SET_HEAD (BLOCK_SUPERCONTEXT (head));
265 else
266 TB_WF;
267 break;
269 case TB_VARS:
270 if (head && TREE_CODE (head) == BLOCK)
271 TB_SET_HEAD (BLOCK_VARS (head));
272 else if (head && TREE_CODE (head) == BIND_EXPR)
273 TB_SET_HEAD (TREE_OPERAND (head, 0));
274 else
275 TB_WF;
276 break;
278 case TB_REFERENCE_TO_THIS:
279 if (head && TYPE_P (head))
280 TB_SET_HEAD (TYPE_REFERENCE_TO (head));
281 else
282 TB_WF;
283 break;
285 case TB_POINTER_TO_THIS:
286 if (head && TYPE_P (head))
287 TB_SET_HEAD (TYPE_POINTER_TO (head));
288 else
289 TB_WF;
290 break;
292 case TB_BASETYPE:
293 if (head && TREE_CODE (head) == OFFSET_TYPE)
294 TB_SET_HEAD (TYPE_OFFSET_BASETYPE (head));
295 else
296 TB_WF;
297 break;
299 case TB_ARG_TYPES:
300 if (head && (TREE_CODE (head) == FUNCTION_TYPE
301 || TREE_CODE (head) == METHOD_TYPE))
302 TB_SET_HEAD (TYPE_ARG_TYPES (head));
303 else
304 TB_WF;
305 break;
307 case TB_METHOD_BASE_TYPE:
308 if (head && (TREE_CODE (head) == FUNCTION_TYPE
309 || TREE_CODE (head) == METHOD_TYPE)
310 && TYPE_METHOD_BASETYPE (head))
311 TB_SET_HEAD (TYPE_METHOD_BASETYPE (head));
312 else
313 TB_WF;
314 break;
316 case TB_FIELDS:
317 if (head && (TREE_CODE (head) == RECORD_TYPE
318 || TREE_CODE (head) == UNION_TYPE
319 || TREE_CODE (head) == QUAL_UNION_TYPE))
320 TB_SET_HEAD (TYPE_FIELDS (head));
321 else
322 TB_WF;
323 break;
325 case TB_DOMAIN:
326 if (head && TREE_CODE (head) == ARRAY_TYPE)
327 TB_SET_HEAD (TYPE_DOMAIN (head));
328 else
329 TB_WF;
330 break;
332 case TB_VALUES:
333 if (head && TREE_CODE (head) == ENUMERAL_TYPE)
334 TB_SET_HEAD (TYPE_VALUES (head));
335 else
336 TB_WF;
337 break;
339 case TB_ARG_TYPE:
340 if (head && TREE_CODE (head) == PARM_DECL)
341 TB_SET_HEAD (DECL_ARG_TYPE (head));
342 else
343 TB_WF;
344 break;
346 case TB_INITIAL:
347 if (head && DECL_P (head))
348 TB_SET_HEAD (DECL_INITIAL (head));
349 else
350 TB_WF;
351 break;
353 case TB_RESULT:
354 if (head && DECL_P (head))
355 TB_SET_HEAD (DECL_RESULT_FLD (head));
356 else
357 TB_WF;
358 break;
360 case TB_ARGUMENTS:
361 if (head && DECL_P (head))
362 TB_SET_HEAD (DECL_ARGUMENTS (head));
363 else
364 TB_WF;
365 break;
367 case TB_ABSTRACT_ORIGIN:
368 if (head && DECL_P (head))
369 TB_SET_HEAD (DECL_ABSTRACT_ORIGIN (head));
370 else if (head && TREE_CODE (head) == BLOCK)
371 TB_SET_HEAD (BLOCK_ABSTRACT_ORIGIN (head));
372 else
373 TB_WF;
374 break;
376 case TB_ATTRIBUTES:
377 if (head && DECL_P (head))
378 TB_SET_HEAD (DECL_ATTRIBUTES (head));
379 else if (head && TYPE_P (head))
380 TB_SET_HEAD (TYPE_ATTRIBUTES (head));
381 else
382 TB_WF;
383 break;
385 case TB_CONTEXT:
386 if (head && DECL_P (head))
387 TB_SET_HEAD (DECL_CONTEXT (head));
388 else if (head && TYPE_P (head)
389 && TYPE_CONTEXT (head))
390 TB_SET_HEAD (TYPE_CONTEXT (head));
391 else
392 TB_WF;
393 break;
395 case TB_OFFSET:
396 if (head && TREE_CODE (head) == FIELD_DECL)
397 TB_SET_HEAD (DECL_FIELD_OFFSET (head));
398 else
399 TB_WF;
400 break;
402 case TB_BIT_OFFSET:
403 if (head && TREE_CODE (head) == FIELD_DECL)
404 TB_SET_HEAD (DECL_FIELD_BIT_OFFSET (head));
405 else
406 TB_WF;
407 break;
409 case TB_UNIT_SIZE:
410 if (head && DECL_P (head))
411 TB_SET_HEAD (DECL_SIZE_UNIT (head));
412 else if (head && TYPE_P (head))
413 TB_SET_HEAD (TYPE_SIZE_UNIT (head));
414 else
415 TB_WF;
416 break;
418 case TB_SIZE:
419 if (head && DECL_P (head))
420 TB_SET_HEAD (DECL_SIZE (head));
421 else if (head && TYPE_P (head))
422 TB_SET_HEAD (TYPE_SIZE (head));
423 else
424 TB_WF;
425 break;
427 case TB_TYPE:
428 if (head && TREE_TYPE (head))
429 TB_SET_HEAD (TREE_TYPE (head));
430 else
431 TB_WF;
432 break;
434 case TB_DECL_SAVED_TREE:
435 if (head && TREE_CODE (head) == FUNCTION_DECL
436 && DECL_SAVED_TREE (head))
437 TB_SET_HEAD (DECL_SAVED_TREE (head));
438 else
439 TB_WF;
440 break;
442 case TB_BODY:
443 if (head && TREE_CODE (head) == BIND_EXPR)
444 TB_SET_HEAD (TREE_OPERAND (head, 1));
445 else
446 TB_WF;
447 break;
449 case TB_CHILD_0:
450 if (head && EXPR_P (head) && TREE_OPERAND (head, 0))
451 TB_SET_HEAD (TREE_OPERAND (head, 0));
452 else
453 TB_WF;
454 break;
456 case TB_CHILD_1:
457 if (head && EXPR_P (head) && TREE_OPERAND (head, 1))
458 TB_SET_HEAD (TREE_OPERAND (head, 1));
459 else
460 TB_WF;
461 break;
463 case TB_CHILD_2:
464 if (head && EXPR_P (head) && TREE_OPERAND (head, 2))
465 TB_SET_HEAD (TREE_OPERAND (head, 2));
466 else
467 TB_WF;
468 break;
470 case TB_CHILD_3:
471 if (head && EXPR_P (head) && TREE_OPERAND (head, 3))
472 TB_SET_HEAD (TREE_OPERAND (head, 3));
473 else
474 TB_WF;
475 break;
477 case TB_PRINT:
478 if (head)
479 debug_tree (head);
480 else
481 TB_WF;
482 break;
484 case TB_PRETTY_PRINT:
485 if (head)
487 print_generic_stmt (TB_OUT_FILE, head, 0);
488 fprintf (TB_OUT_FILE, "\n");
490 else
491 TB_WF;
492 break;
494 case TB_SEARCH_NAME:
496 break;
498 case TB_SEARCH_CODE:
500 enum tree_code code;
501 char *arg_text;
503 arg_text = strchr (input, ' ');
504 if (arg_text == NULL)
506 fprintf (TB_OUT_FILE, "First argument is missing. This isn't a valid search command. \n");
507 break;
509 code = TB_get_tree_code (arg_text + 1);
511 /* Search in the subtree a node with the given code. */
513 tree res;
515 res = walk_tree (&head, find_node_with_code, &code, NULL);
516 if (res == NULL_TREE)
518 fprintf (TB_OUT_FILE, "There's no node with this code (reachable via the walk_tree function from this node).\n");
520 else
522 fprintf (TB_OUT_FILE, "Achoo! I got this node in the tree.\n");
523 TB_SET_HEAD (res);
526 break;
529 #define TB_MOVE_HEAD(FCT) do { \
530 if (head) \
532 tree t; \
533 t = FCT (head); \
534 if (t) \
535 TB_SET_HEAD (t); \
536 else \
537 TB_WF; \
539 else \
540 TB_WF; \
541 } while (0)
543 case TB_FIRST:
544 TB_MOVE_HEAD (TB_first_in_bind);
545 break;
547 case TB_LAST:
548 TB_MOVE_HEAD (TB_last_in_bind);
549 break;
551 case TB_UP:
552 TB_MOVE_HEAD (TB_up_expr);
553 break;
555 case TB_PREV:
556 TB_MOVE_HEAD (TB_prev_expr);
557 break;
559 case TB_NEXT:
560 TB_MOVE_HEAD (TB_next_expr);
561 break;
563 case TB_HPREV:
564 /* This command is a little bit special, since it deals with history
565 stack. For this reason it should keep the "head = ..." statement
566 and not use TB_MOVE_HEAD. */
567 if (head)
569 tree t;
570 t = TB_history_prev ();
571 if (t)
573 head = t;
574 if (TB_verbose)
576 print_generic_expr (TB_OUT_FILE, head, 0);
577 fprintf (TB_OUT_FILE, "\n");
580 else
581 TB_WF;
583 else
584 TB_WF;
585 break;
587 case TB_CHAIN:
588 /* Don't go further if it's the last node in this chain. */
589 if (head && TREE_CODE (head) == BLOCK)
590 TB_SET_HEAD (BLOCK_CHAIN (head));
591 else if (head && TREE_CHAIN (head))
592 TB_SET_HEAD (TREE_CHAIN (head));
593 else
594 TB_WF;
595 break;
597 case TB_FUN:
598 /* Go up to the current function declaration. */
599 TB_SET_HEAD (current_function_decl);
600 fprintf (TB_OUT_FILE, "Current function declaration.\n");
601 break;
603 case TB_HELP:
604 /* Display a help message. */
606 int i;
607 fprintf (TB_OUT_FILE, "Possible commands are:\n\n");
608 for (i = 0; i < TB_UNUSED_COMMAND; i++)
610 fprintf (TB_OUT_FILE, "%20s - %s\n", TB_COMMAND_TEXT (i), TB_COMMAND_HELP (i));
613 break;
615 case TB_VERBOSE:
616 if (TB_verbose == 0)
618 TB_verbose = 1;
619 fprintf (TB_OUT_FILE, "Verbose on.\n");
621 else
623 TB_verbose = 0;
624 fprintf (TB_OUT_FILE, "Verbose off.\n");
626 break;
628 case TB_EXIT:
629 case TB_QUIT:
630 /* Just exit from this function. */
631 goto ret;
633 default:
634 TB_NIY;
638 ret:;
639 delete TB_up_ht;
640 TB_up_ht = NULL;
641 return;
645 /* Search the first node in this BIND_EXPR. */
647 static tree
648 TB_first_in_bind (tree node)
650 tree t;
652 if (node == NULL_TREE)
653 return NULL_TREE;
655 while ((t = TB_prev_expr (node)))
656 node = t;
658 return node;
661 /* Search the last node in this BIND_EXPR. */
663 static tree
664 TB_last_in_bind (tree node)
666 tree t;
668 if (node == NULL_TREE)
669 return NULL_TREE;
671 while ((t = TB_next_expr (node)))
672 node = t;
674 return node;
677 /* Search the parent expression for this node. */
679 static tree
680 TB_up_expr (tree node)
682 tree res;
683 if (node == NULL_TREE)
684 return NULL_TREE;
686 res = TB_up_ht->find (node);
687 return res;
690 /* Search the previous expression in this BIND_EXPR. */
692 static tree
693 TB_prev_expr (tree node)
695 node = TB_current_chain_node (node);
697 if (node == NULL_TREE)
698 return NULL_TREE;
700 node = TB_up_expr (node);
701 if (node && TREE_CODE (node) == COMPOUND_EXPR)
702 return node;
703 else
704 return NULL_TREE;
707 /* Search the next expression in this BIND_EXPR. */
709 static tree
710 TB_next_expr (tree node)
712 node = TB_current_chain_node (node);
714 if (node == NULL_TREE)
715 return NULL_TREE;
717 node = TREE_OPERAND (node, 1);
718 return node;
721 static tree
722 TB_current_chain_node (tree node)
724 if (node == NULL_TREE)
725 return NULL_TREE;
727 if (TREE_CODE (node) == COMPOUND_EXPR)
728 return node;
730 node = TB_up_expr (node);
731 if (node)
733 if (TREE_CODE (node) == COMPOUND_EXPR)
734 return node;
736 node = TB_up_expr (node);
737 if (TREE_CODE (node) == COMPOUND_EXPR)
738 return node;
741 return NULL_TREE;
744 /* For each node store in its children nodes that the current node is their
745 parent. This function is used by walk_tree. */
747 static tree
748 store_child_info (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
749 void *data ATTRIBUTE_UNUSED)
751 tree node;
752 tree_node **slot;
754 node = *tp;
756 /* 'node' is the parent of 'TREE_OPERAND (node, *)'. */
757 if (EXPR_P (node))
759 int n = TREE_OPERAND_LENGTH (node);
760 int i;
761 for (i = 0; i < n; i++)
763 tree op = TREE_OPERAND (node, i);
764 slot = TB_up_ht->find_slot (op, INSERT);
765 *slot = node;
769 /* Never stop walk_tree. */
770 return NULL_TREE;
773 /* Update information about upper expressions in the hash table. */
775 static void
776 TB_update_up (tree node)
778 while (node)
780 walk_tree (&node, store_child_info, NULL, NULL);
782 /* Walk function's body. */
783 if (TREE_CODE (node) == FUNCTION_DECL)
784 if (DECL_SAVED_TREE (node))
785 walk_tree (&DECL_SAVED_TREE (node), store_child_info, NULL, NULL);
787 /* Walk rest of the chain. */
788 node = TREE_CHAIN (node);
790 fprintf (TB_OUT_FILE, "Up/prev expressions updated.\n");
793 /* Parse the input string for determining the command the user asked for. */
795 static TB_CODE
796 TB_get_command (char *input)
798 unsigned int mn, size_tok;
799 int comp;
800 char *space;
802 space = strchr (input, ' ');
803 if (space != NULL)
804 size_tok = strlen (input) - strlen (space);
805 else
806 size_tok = strlen (input) - 1;
808 for (mn = 0; mn < TB_UNUSED_COMMAND; mn++)
810 if (size_tok != TB_COMMAND_LEN (mn))
811 continue;
813 comp = memcmp (input, TB_COMMAND_TEXT (mn), TB_COMMAND_LEN (mn));
814 if (comp == 0)
815 /* Here we just determined the command. If this command takes
816 an argument, then the argument is determined later. */
817 return TB_COMMAND_CODE (mn);
820 /* Not a valid command. */
821 return TB_UNUSED_COMMAND;
824 /* Parse the input string for determining the tree code. */
826 static enum tree_code
827 TB_get_tree_code (char *input)
829 unsigned int mn, size_tok;
830 int comp;
831 char *space;
833 space = strchr (input, ' ');
834 if (space != NULL)
835 size_tok = strlen (input) - strlen (space);
836 else
837 size_tok = strlen (input) - 1;
839 for (mn = 0; mn < LAST_AND_UNUSED_TREE_CODE; mn++)
841 if (size_tok != TB_TREE_CODE_LEN (mn))
842 continue;
844 comp = memcmp (input, TB_TREE_CODE_TEXT (mn), TB_TREE_CODE_LEN (mn));
845 if (comp == 0)
847 fprintf (TB_OUT_FILE, "%s\n", TB_TREE_CODE_TEXT (mn));
848 return TB_TREE_CODE (mn);
852 /* This isn't a valid code. */
853 return LAST_AND_UNUSED_TREE_CODE;
856 /* Find a node with a given code. This function is used as an argument to
857 walk_tree. */
859 static tree
860 find_node_with_code (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
861 void *data)
863 enum tree_code *code;
864 code = (enum tree_code *) data;
865 if (*code == TREE_CODE (*tp))
866 return *tp;
868 return NULL_TREE;
871 /* Returns a pointer to the last visited node. */
873 static tree
874 TB_history_prev (void)
876 if (!vec_safe_is_empty (TB_history_stack))
878 tree last = TB_history_stack->last ();
879 TB_history_stack->pop ();
880 return last;
882 return NULL_TREE;
885 /* Read up to (and including) a '\n' from STREAM into *LINEPTR
886 (and null-terminate it). *LINEPTR is a pointer returned from malloc
887 (or NULL), pointing to *N characters of space. It is realloc'd as
888 necessary. Returns the number of characters read (not including the
889 null terminator), or -1 on error or EOF.
890 This function comes from sed (and is supposed to be a portable version
891 of getline). */
893 static long
894 TB_getline (char **lineptr, long *n, FILE *stream)
896 char *line, *p;
897 long size, copy;
899 if (lineptr == NULL || n == NULL)
901 errno = EINVAL;
902 return -1;
905 if (ferror (stream))
906 return -1;
908 /* Make sure we have a line buffer to start with. */
909 if (*lineptr == NULL || *n < 2) /* !seen and no buf yet need 2 chars. */
911 #ifndef MAX_CANON
912 #define MAX_CANON 256
913 #endif
914 line = (char *) xrealloc (*lineptr, MAX_CANON);
915 if (line == NULL)
916 return -1;
917 *lineptr = line;
918 *n = MAX_CANON;
921 line = *lineptr;
922 size = *n;
924 copy = size;
925 p = line;
927 while (1)
929 long len;
931 while (--copy > 0)
933 register int c = getc (stream);
934 if (c == EOF)
935 goto lose;
936 else if ((*p++ = c) == '\n')
937 goto win;
940 /* Need to enlarge the line buffer. */
941 len = p - line;
942 size *= 2;
943 line = (char *) xrealloc (line, size);
944 if (line == NULL)
945 goto lose;
946 *lineptr = line;
947 *n = size;
948 p = line + len;
949 copy = size - len;
952 lose:
953 if (p == *lineptr)
954 return -1;
956 /* Return a partial line since we got an error in the middle. */
957 win:
958 #if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) || defined(MSDOS)
959 if (p - 2 >= *lineptr && p[-2] == '\r')
960 p[-2] = p[-1], --p;
961 #endif
962 *p = '\0';
963 return p - *lineptr;