OSDN Git Service

(TYPE_HASH): Use HOST_WIDE_INT instead of int when casting pointers to
[pf3gnuchains/gcc-fork.git] / gcc / tree.c
1 /* Language-independent node constructors for parse phase of GNU compiler.
2    Copyright (C) 1987, 1988, 1992 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20
21 /* This file contains the low level primitives for operating on tree nodes,
22    including allocation, list operations, interning of identifiers,
23    construction of data type nodes and statement nodes,
24    and construction of type conversion nodes.  It also contains
25    tables index by tree code that describe how to take apart
26    nodes of that code.
27
28    It is intended to be language-independent, but occasionally
29    calls language-dependent routines defined (for C) in typecheck.c.
30
31    The low-level allocation routines oballoc and permalloc
32    are used also for allocating many other kinds of objects
33    by all passes of the compiler.  */
34
35 #include "config.h"
36 #include <stdio.h>
37 #include "flags.h"
38 #include "tree.h"
39 #include "function.h"
40 #include "obstack.h"
41 #include "gvarargs.h"
42
43 #define obstack_chunk_alloc xmalloc
44 #define obstack_chunk_free free
45
46 /* Tree nodes of permanent duration are allocated in this obstack.
47    They are the identifier nodes, and everything outside of
48    the bodies and parameters of function definitions.  */
49
50 struct obstack permanent_obstack;
51
52 /* The initial RTL, and all ..._TYPE nodes, in a function
53    are allocated in this obstack.  Usually they are freed at the
54    end of the function, but if the function is inline they are saved.
55    For top-level functions, this is maybepermanent_obstack.
56    Separate obstacks are made for nested functions.  */
57
58 struct obstack *function_maybepermanent_obstack;
59
60 /* This is the function_maybepermanent_obstack for top-level functions.  */
61
62 struct obstack maybepermanent_obstack;
63
64 /* The contents of the current function definition are allocated
65    in this obstack, and all are freed at the end of the function.
66    For top-level functions, this is temporary_obstack.
67    Separate obstacks are made for nested functions.  */
68
69 struct obstack *function_obstack;
70
71 /* This is used for reading initializers of global variables.  */
72
73 struct obstack temporary_obstack;
74
75 /* The tree nodes of an expression are allocated
76    in this obstack, and all are freed at the end of the expression.  */
77
78 struct obstack momentary_obstack;
79
80 /* The tree nodes of a declarator are allocated
81    in this obstack, and all are freed when the declarator
82    has been parsed.  */
83
84 static struct obstack temp_decl_obstack;
85
86 /* This points at either permanent_obstack
87    or the current function_maybepermanent_obstack.  */
88
89 struct obstack *saveable_obstack;
90
91 /* This is same as saveable_obstack during parse and expansion phase;
92    it points to the current function's obstack during optimization.
93    This is the obstack to be used for creating rtl objects.  */
94
95 struct obstack *rtl_obstack;
96
97 /* This points at either permanent_obstack or the current function_obstack.  */
98
99 struct obstack *current_obstack;
100
101 /* This points at either permanent_obstack or the current function_obstack
102    or momentary_obstack.  */
103
104 struct obstack *expression_obstack;
105
106 /* Stack of obstack selections for push_obstacks and pop_obstacks.  */
107
108 struct obstack_stack
109 {
110   struct obstack_stack *next;
111   struct obstack *current;
112   struct obstack *saveable;
113   struct obstack *expression;
114   struct obstack *rtl;
115 };
116
117 struct obstack_stack *obstack_stack;
118
119 /* Obstack for allocating struct obstack_stack entries.  */
120
121 static struct obstack obstack_stack_obstack;
122
123 /* Addresses of first objects in some obstacks.
124    This is for freeing their entire contents.  */
125 char *maybepermanent_firstobj;
126 char *temporary_firstobj;
127 char *momentary_firstobj;
128 char *temp_decl_firstobj;
129
130 /* Nonzero means all ..._TYPE nodes should be allocated permanently.  */
131
132 int all_types_permanent;
133
134 /* Stack of places to restore the momentary obstack back to.  */
135    
136 struct momentary_level
137 {
138   /* Pointer back to previous such level.  */
139   struct momentary_level *prev;
140   /* First object allocated within this level.  */
141   char *base;
142   /* Value of expression_obstack saved at entry to this level.  */
143   struct obstack *obstack;
144 };
145
146 struct momentary_level *momentary_stack;
147
148 /* Table indexed by tree code giving a string containing a character
149    classifying the tree code.  Possibilities are
150    t, d, s, c, r, <, 1, 2 and e.  See tree.def for details.  */
151
152 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
153
154 char *standard_tree_code_type[] = {
155 #include "tree.def"
156 };
157 #undef DEFTREECODE
158
159 /* Table indexed by tree code giving number of expression
160    operands beyond the fixed part of the node structure.
161    Not used for types or decls.  */
162
163 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
164
165 int standard_tree_code_length[] = {
166 #include "tree.def"
167 };
168 #undef DEFTREECODE
169
170 /* Names of tree components.
171    Used for printing out the tree and error messages.  */
172 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
173
174 char *standard_tree_code_name[] = {
175 #include "tree.def"
176 };
177 #undef DEFTREECODE
178
179 /* Table indexed by tree code giving a string containing a character
180    classifying the tree code.  Possibilities are
181    t, d, s, c, r, e, <, 1 and 2.  See tree.def for details.  */
182
183 char **tree_code_type;
184
185 /* Table indexed by tree code giving number of expression
186    operands beyond the fixed part of the node structure.
187    Not used for types or decls.  */
188
189 int *tree_code_length;
190
191 /* Table indexed by tree code giving name of tree code, as a string.  */
192
193 char **tree_code_name;
194
195 /* Statistics-gathering stuff.  */
196 typedef enum
197 {
198   d_kind,
199   t_kind,
200   b_kind,
201   s_kind,
202   r_kind,
203   e_kind,
204   c_kind,
205   id_kind,
206   op_id_kind,
207   perm_list_kind,
208   temp_list_kind,
209   vec_kind,
210   x_kind,
211   lang_decl,
212   lang_type,
213   all_kinds
214 } tree_node_kind;
215
216 int tree_node_counts[(int)all_kinds];
217 int tree_node_sizes[(int)all_kinds];
218 int id_string_size = 0;
219
220 char *tree_node_kind_names[] = {
221   "decls",
222   "types",
223   "blocks",
224   "stmts",
225   "refs",
226   "exprs",
227   "constants",
228   "identifiers",
229   "op_identifiers",
230   "perm_tree_lists",
231   "temp_tree_lists",
232   "vecs",
233   "random kinds",
234   "lang_decl kinds",
235   "lang_type kinds"
236 };
237
238 /* Hash table for uniquizing IDENTIFIER_NODEs by name.  */
239
240 #define MAX_HASH_TABLE 1009
241 static tree hash_table[MAX_HASH_TABLE]; /* id hash buckets */
242
243 /* 0 while creating built-in identifiers.  */
244 static int do_identifier_warnings;
245
246 /* Unique id for next decl created.  */
247 static int next_decl_uid;
248
249 extern char *mode_name[];
250
251 void gcc_obstack_init ();
252 static tree stabilize_reference_1 ();
253 \f
254 /* Init the principal obstacks.  */
255
256 void
257 init_obstacks ()
258 {
259   gcc_obstack_init (&obstack_stack_obstack);
260   gcc_obstack_init (&permanent_obstack);
261
262   gcc_obstack_init (&temporary_obstack);
263   temporary_firstobj = (char *) obstack_alloc (&temporary_obstack, 0);
264   gcc_obstack_init (&momentary_obstack);
265   momentary_firstobj = (char *) obstack_alloc (&momentary_obstack, 0);
266   gcc_obstack_init (&maybepermanent_obstack);
267   maybepermanent_firstobj
268     = (char *) obstack_alloc (&maybepermanent_obstack, 0);
269   gcc_obstack_init (&temp_decl_obstack);
270   temp_decl_firstobj = (char *) obstack_alloc (&temp_decl_obstack, 0);
271
272   function_obstack = &temporary_obstack;
273   function_maybepermanent_obstack = &maybepermanent_obstack;
274   current_obstack = &permanent_obstack;
275   expression_obstack = &permanent_obstack;
276   rtl_obstack = saveable_obstack = &permanent_obstack;
277
278   /* Init the hash table of identifiers.  */
279   bzero (hash_table, sizeof hash_table);
280 }
281
282 void
283 gcc_obstack_init (obstack)
284      struct obstack *obstack;
285 {
286   /* Let particular systems override the size of a chunk.  */
287 #ifndef OBSTACK_CHUNK_SIZE
288 #define OBSTACK_CHUNK_SIZE 0
289 #endif
290   /* Let them override the alloc and free routines too.  */
291 #ifndef OBSTACK_CHUNK_ALLOC
292 #define OBSTACK_CHUNK_ALLOC xmalloc
293 #endif
294 #ifndef OBSTACK_CHUNK_FREE
295 #define OBSTACK_CHUNK_FREE free
296 #endif
297   _obstack_begin (obstack, OBSTACK_CHUNK_SIZE, 0,
298                   (void *(*) ()) OBSTACK_CHUNK_ALLOC,
299                   (void (*) ()) OBSTACK_CHUNK_FREE);
300 }
301
302 /* Save all variables describing the current status into the structure *P.
303    This is used before starting a nested function.  */
304
305 void
306 save_tree_status (p)
307      struct function *p;
308 {
309   p->all_types_permanent = all_types_permanent;
310   p->momentary_stack = momentary_stack;
311   p->maybepermanent_firstobj = maybepermanent_firstobj;
312   p->momentary_firstobj = momentary_firstobj;
313   p->function_obstack = function_obstack;
314   p->function_maybepermanent_obstack = function_maybepermanent_obstack;
315   p->current_obstack = current_obstack;
316   p->expression_obstack = expression_obstack;
317   p->saveable_obstack = saveable_obstack;
318   p->rtl_obstack = rtl_obstack;
319
320   function_obstack = (struct obstack *) xmalloc (sizeof (struct obstack));
321   gcc_obstack_init (function_obstack);
322
323   function_maybepermanent_obstack
324     = (struct obstack *) xmalloc (sizeof (struct obstack));
325   gcc_obstack_init (function_maybepermanent_obstack);
326
327   current_obstack = &permanent_obstack;
328   expression_obstack = &permanent_obstack;
329   rtl_obstack = saveable_obstack = &permanent_obstack;
330
331   momentary_firstobj = (char *) obstack_finish (&momentary_obstack);
332   maybepermanent_firstobj
333     = (char *) obstack_finish (function_maybepermanent_obstack);
334 }
335
336 /* Restore all variables describing the current status from the structure *P.
337    This is used after a nested function.  */
338
339 void
340 restore_tree_status (p)
341      struct function *p;
342 {
343   all_types_permanent = p->all_types_permanent;
344   momentary_stack = p->momentary_stack;
345
346   obstack_free (&momentary_obstack, momentary_firstobj);
347   obstack_free (function_obstack, 0);
348   obstack_free (function_maybepermanent_obstack, 0);
349   free (function_obstack);
350
351   momentary_firstobj = p->momentary_firstobj;
352   maybepermanent_firstobj = p->maybepermanent_firstobj;
353   function_obstack = p->function_obstack;
354   function_maybepermanent_obstack = p->function_maybepermanent_obstack;
355   current_obstack = p->current_obstack;
356   expression_obstack = p->expression_obstack;
357   saveable_obstack = p->saveable_obstack;
358   rtl_obstack = p->rtl_obstack;
359 }
360 \f
361 /* Start allocating on the temporary (per function) obstack.
362    This is done in start_function before parsing the function body,
363    and before each initialization at top level, and to go back
364    to temporary allocation after doing end_temporary_allocation.  */
365
366 void
367 temporary_allocation ()
368 {
369   /* Note that function_obstack at top level points to temporary_obstack.
370      But within a nested function context, it is a separate obstack.  */
371   current_obstack = function_obstack;
372   expression_obstack = function_obstack;
373   rtl_obstack = saveable_obstack = function_maybepermanent_obstack;
374   momentary_stack = 0;
375 }
376
377 /* Start allocating on the permanent obstack but don't
378    free the temporary data.  After calling this, call
379    `permanent_allocation' to fully resume permanent allocation status.  */
380
381 void
382 end_temporary_allocation ()
383 {
384   current_obstack = &permanent_obstack;
385   expression_obstack = &permanent_obstack;
386   rtl_obstack = saveable_obstack = &permanent_obstack;
387 }
388
389 /* Resume allocating on the temporary obstack, undoing
390    effects of `end_temporary_allocation'.  */
391
392 void
393 resume_temporary_allocation ()
394 {
395   current_obstack = function_obstack;
396   expression_obstack = function_obstack;
397   rtl_obstack = saveable_obstack = function_maybepermanent_obstack;
398 }
399
400 /* While doing temporary allocation, switch to allocating in such a
401    way as to save all nodes if the function is inlined.  Call
402    resume_temporary_allocation to go back to ordinary temporary
403    allocation.  */
404
405 void
406 saveable_allocation ()
407 {
408   /* Note that function_obstack at top level points to temporary_obstack.
409      But within a nested function context, it is a separate obstack.  */
410   expression_obstack = current_obstack = saveable_obstack;
411 }
412
413 /* Switch to current obstack CURRENT and maybepermanent obstack SAVEABLE,
414    recording the previously current obstacks on a stack.
415    This does not free any storage in any obstack.  */
416
417 void
418 push_obstacks (current, saveable)
419      struct obstack *current, *saveable;
420 {
421   struct obstack_stack *p
422     = (struct obstack_stack *) obstack_alloc (&obstack_stack_obstack,
423                                               (sizeof (struct obstack_stack)));
424
425   p->current = current_obstack;
426   p->saveable = saveable_obstack;
427   p->expression = expression_obstack;
428   p->rtl = rtl_obstack;
429   p->next = obstack_stack;
430   obstack_stack = p;
431
432   current_obstack = current;
433   expression_obstack = current;
434   rtl_obstack = saveable_obstack = saveable;
435 }
436
437 /* Save the current set of obstacks, but don't change them.  */
438
439 void
440 push_obstacks_nochange ()
441 {
442   struct obstack_stack *p
443     = (struct obstack_stack *) obstack_alloc (&obstack_stack_obstack,
444                                               (sizeof (struct obstack_stack)));
445
446   p->current = current_obstack;
447   p->saveable = saveable_obstack;
448   p->expression = expression_obstack;
449   p->rtl = rtl_obstack;
450   p->next = obstack_stack;
451   obstack_stack = p;
452 }
453
454 /* Pop the obstack selection stack.  */
455
456 void
457 pop_obstacks ()
458 {
459   struct obstack_stack *p = obstack_stack;
460   obstack_stack = p->next;
461
462   current_obstack = p->current;
463   saveable_obstack = p->saveable;
464   expression_obstack = p->expression;
465   rtl_obstack = p->rtl;
466
467   obstack_free (&obstack_stack_obstack, p);
468 }
469
470 /* Nonzero if temporary allocation is currently in effect.
471    Zero if currently doing permanent allocation.  */
472
473 int
474 allocation_temporary_p ()
475 {
476   return current_obstack != &permanent_obstack;
477 }
478
479 /* Go back to allocating on the permanent obstack
480    and free everything in the temporary obstack.
481    This is done in finish_function after fully compiling a function.  */
482
483 void
484 permanent_allocation ()
485 {
486   /* Free up previous temporary obstack data */
487   obstack_free (&temporary_obstack, temporary_firstobj);
488   obstack_free (&momentary_obstack, momentary_firstobj);
489   obstack_free (&maybepermanent_obstack, maybepermanent_firstobj);
490   obstack_free (&temp_decl_obstack, temp_decl_firstobj);
491
492   current_obstack = &permanent_obstack;
493   expression_obstack = &permanent_obstack;
494   rtl_obstack = saveable_obstack = &permanent_obstack;
495 }
496
497 /* Save permanently everything on the maybepermanent_obstack.  */
498
499 void
500 preserve_data ()
501 {
502   maybepermanent_firstobj
503     = (char *) obstack_alloc (function_maybepermanent_obstack, 0);
504 }
505
506 void
507 preserve_initializer ()
508 {
509   temporary_firstobj
510     = (char *) obstack_alloc (&temporary_obstack, 0);
511   momentary_firstobj
512     = (char *) obstack_alloc (&momentary_obstack, 0);
513   maybepermanent_firstobj
514     = (char *) obstack_alloc (function_maybepermanent_obstack, 0);
515 }
516
517 /* Start allocating new rtl in current_obstack.
518    Use resume_temporary_allocation
519    to go back to allocating rtl in saveable_obstack.  */
520
521 void
522 rtl_in_current_obstack ()
523 {
524   rtl_obstack = current_obstack;
525 }
526
527 /* Temporarily allocate rtl from saveable_obstack.  Return 1 if we were
528    previously allocating it from current_obstack.  */
529
530 int
531 rtl_in_saveable_obstack ()
532 {
533   if (rtl_obstack == current_obstack)
534     {
535       rtl_obstack = saveable_obstack;
536       return 1;
537     }
538   else
539     return 0;
540 }
541 \f
542 /* Allocate SIZE bytes in the current obstack
543    and return a pointer to them.
544    In practice the current obstack is always the temporary one.  */
545
546 char *
547 oballoc (size)
548      int size;
549 {
550   return (char *) obstack_alloc (current_obstack, size);
551 }
552
553 /* Free the object PTR in the current obstack
554    as well as everything allocated since PTR.
555    In practice the current obstack is always the temporary one.  */
556
557 void
558 obfree (ptr)
559      char *ptr;
560 {
561   obstack_free (current_obstack, ptr);
562 }
563
564 /* Allocate SIZE bytes in the permanent obstack
565    and return a pointer to them.  */
566
567 char *
568 permalloc (size)
569      int size;
570 {
571   return (char *) obstack_alloc (&permanent_obstack, size);
572 }
573
574 /* Allocate NELEM items of SIZE bytes in the permanent obstack
575    and return a pointer to them.  The storage is cleared before
576    returning the value.  */
577
578 char *
579 perm_calloc (nelem, size)
580      int nelem;
581      long size;
582 {
583   char *rval = (char *) obstack_alloc (&permanent_obstack, nelem * size);
584   bzero (rval, nelem * size);
585   return rval;
586 }
587
588 /* Allocate SIZE bytes in the saveable obstack
589    and return a pointer to them.  */
590
591 char *
592 savealloc (size)
593      int size;
594 {
595   return (char *) obstack_alloc (saveable_obstack, size);
596 }
597 \f
598 /* Print out which obstack an object is in.  */
599
600 void
601 debug_obstack (object)
602      char *object;
603 {
604   struct obstack *obstack = NULL;
605   char *obstack_name = NULL;
606   struct function *p;
607
608   for (p = outer_function_chain; p; p = p->next)
609     {
610       if (_obstack_allocated_p (p->function_obstack, object))
611         {
612           obstack = p->function_obstack;
613           obstack_name = "containing function obstack";
614         }
615       if (_obstack_allocated_p (p->function_maybepermanent_obstack, object))
616         {
617           obstack = p->function_maybepermanent_obstack;
618           obstack_name = "containing function maybepermanent obstack";
619         }
620     }
621
622   if (_obstack_allocated_p (&obstack_stack_obstack, object))
623     {
624       obstack = &obstack_stack_obstack;
625       obstack_name = "obstack_stack_obstack";
626     }
627   else if (_obstack_allocated_p (function_obstack, object))
628     {
629       obstack = function_obstack;
630       obstack_name = "function obstack";
631     }
632   else if (_obstack_allocated_p (&permanent_obstack, object))
633     {
634       obstack = &permanent_obstack;
635       obstack_name = "permanent_obstack";
636     }
637   else if (_obstack_allocated_p (&momentary_obstack, object))
638     {
639       obstack = &momentary_obstack;
640       obstack_name = "momentary_obstack";
641     }
642   else if (_obstack_allocated_p (function_maybepermanent_obstack, object))
643     {
644       obstack = function_maybepermanent_obstack;
645       obstack_name = "function maybepermanent obstack";
646     }
647   else if (_obstack_allocated_p (&temp_decl_obstack, object))
648     {
649       obstack = &temp_decl_obstack;
650       obstack_name = "temp_decl_obstack";
651     }
652
653   /* Check to see if the object is in the free area of the obstack. */
654   if (obstack != NULL)
655     {
656       if (object >= obstack->next_free
657           && object < obstack->chunk_limit)
658         fprintf (stderr, "object in free portion of obstack %s.\n",
659                  obstack_name);
660       else
661         fprintf (stderr, "object allocated from %s.\n", obstack_name);
662     }
663   else
664     fprintf (stderr, "object not allocated from any obstack.\n");
665 }
666
667 /* Return 1 if OBJ is in the permanent obstack.
668    This is slow, and should be used only for debugging.
669    Use TREE_PERMANENT for other purposes.  */
670
671 int
672 object_permanent_p (obj)
673      tree obj;
674 {
675   return _obstack_allocated_p (&permanent_obstack, obj);
676 }
677 \f
678 /* Start a level of momentary allocation.
679    In C, each compound statement has its own level
680    and that level is freed at the end of each statement.
681    All expression nodes are allocated in the momentary allocation level.  */
682
683 void
684 push_momentary ()
685 {
686   struct momentary_level *tem
687     = (struct momentary_level *) obstack_alloc (&momentary_obstack,
688                                                 sizeof (struct momentary_level));
689   tem->prev = momentary_stack;
690   tem->base = (char *) obstack_base (&momentary_obstack);
691   tem->obstack = expression_obstack;
692   momentary_stack = tem;
693   expression_obstack = &momentary_obstack;
694 }
695
696 /* Free all the storage in the current momentary-allocation level.
697    In C, this happens at the end of each statement.  */
698
699 void
700 clear_momentary ()
701 {
702   obstack_free (&momentary_obstack, momentary_stack->base);
703 }
704
705 /* Discard a level of momentary allocation.
706    In C, this happens at the end of each compound statement.
707    Restore the status of expression node allocation
708    that was in effect before this level was created.  */
709
710 void
711 pop_momentary ()
712 {
713   struct momentary_level *tem = momentary_stack;
714   momentary_stack = tem->prev;
715   expression_obstack = tem->obstack;
716   obstack_free (&momentary_obstack, tem);
717 }
718
719 /* Call when starting to parse a declaration:
720    make expressions in the declaration last the length of the function.
721    Returns an argument that should be passed to resume_momentary later.  */
722
723 int
724 suspend_momentary ()
725 {
726   register int tem = expression_obstack == &momentary_obstack;
727   expression_obstack = saveable_obstack;
728   return tem;
729 }
730
731 /* Call when finished parsing a declaration:
732    restore the treatment of node-allocation that was
733    in effect before the suspension.
734    YES should be the value previously returned by suspend_momentary.  */
735
736 void
737 resume_momentary (yes)
738      int yes;
739 {
740   if (yes)
741     expression_obstack = &momentary_obstack;
742 }
743 \f
744 /* Init the tables indexed by tree code.
745    Note that languages can add to these tables to define their own codes.  */
746
747 void
748 init_tree_codes ()
749 {
750   tree_code_type = (char **) xmalloc (sizeof (standard_tree_code_type));
751   tree_code_length = (int *) xmalloc (sizeof (standard_tree_code_length));
752   tree_code_name = (char **) xmalloc (sizeof (standard_tree_code_name));
753   bcopy (standard_tree_code_type, tree_code_type,
754          sizeof (standard_tree_code_type));
755   bcopy (standard_tree_code_length, tree_code_length,
756          sizeof (standard_tree_code_length));
757   bcopy (standard_tree_code_name, tree_code_name,
758          sizeof (standard_tree_code_name));
759 }
760
761 /* Return a newly allocated node of code CODE.
762    Initialize the node's unique id and its TREE_PERMANENT flag.
763    For decl and type nodes, some other fields are initialized.
764    The rest of the node is initialized to zero.
765
766    Achoo!  I got a code in the node.  */
767
768 tree
769 make_node (code)
770      enum tree_code code;
771 {
772   register tree t;
773   register int type = TREE_CODE_CLASS (code);
774   register int length;
775   register struct obstack *obstack = current_obstack;
776   register int i;
777   register tree_node_kind kind;
778
779   switch (type)
780     {
781     case 'd':  /* A decl node */
782 #ifdef GATHER_STATISTICS
783       kind = d_kind;
784 #endif
785       length = sizeof (struct tree_decl);
786       /* All decls in an inline function need to be saved.  */
787       if (obstack != &permanent_obstack)
788         obstack = saveable_obstack;
789       /* PARM_DECLs always go on saveable_obstack, not permanent,
790          even though we may make them before the function turns
791          on temporary allocation.  */
792       else if (code == PARM_DECL)
793         obstack = function_maybepermanent_obstack;
794       break;
795
796     case 't':  /* a type node */
797 #ifdef GATHER_STATISTICS
798       kind = t_kind;
799 #endif
800       length = sizeof (struct tree_type);
801       /* All data types are put where we can preserve them if nec.  */
802       if (obstack != &permanent_obstack)
803         obstack = all_types_permanent ? &permanent_obstack : saveable_obstack;
804       break;
805
806     case 'b':  /* a lexical block */
807 #ifdef GATHER_STATISTICS
808       kind = b_kind;
809 #endif
810       length = sizeof (struct tree_block);
811       /* All BLOCK nodes are put where we can preserve them if nec.  */
812       if (obstack != &permanent_obstack)
813         obstack = saveable_obstack;
814       break;
815
816     case 's':  /* an expression with side effects */
817 #ifdef GATHER_STATISTICS
818       kind = s_kind;
819       goto usual_kind;
820 #endif
821     case 'r':  /* a reference */
822 #ifdef GATHER_STATISTICS
823       kind = r_kind;
824       goto usual_kind;
825 #endif
826     case 'e':  /* an expression */
827     case '<':  /* a comparison expression */
828     case '1':  /* a unary arithmetic expression */
829     case '2':  /* a binary arithmetic expression */
830 #ifdef GATHER_STATISTICS
831       kind = e_kind;
832     usual_kind:
833 #endif
834       obstack = expression_obstack;
835       /* All BIND_EXPR nodes are put where we can preserve them if nec.  */
836       if (code == BIND_EXPR && obstack != &permanent_obstack)
837         obstack = saveable_obstack;
838       length = sizeof (struct tree_exp)
839         + (tree_code_length[(int) code] - 1) * sizeof (char *);
840       break;
841
842     case 'c':  /* a constant */
843 #ifdef GATHER_STATISTICS
844       kind = c_kind;
845 #endif
846       obstack = expression_obstack;
847       /* We can't use tree_code_length for this, since the number of words
848          is machine-dependent due to varying alignment of `double'.  */
849       if (code == REAL_CST)
850         {
851           length = sizeof (struct tree_real_cst);
852           break;
853         }
854
855     case 'x':  /* something random, like an identifier.  */
856 #ifdef GATHER_STATISTICS
857       if (code == IDENTIFIER_NODE)
858         kind = id_kind;
859       else if (code == OP_IDENTIFIER)
860         kind = op_id_kind;
861       else if (code == TREE_VEC)
862         kind = vec_kind;
863       else
864         kind = x_kind;
865 #endif
866       length = sizeof (struct tree_common)
867         + tree_code_length[(int) code] * sizeof (char *);
868       /* Identifier nodes are always permanent since they are
869          unique in a compiler run.  */
870       if (code == IDENTIFIER_NODE) obstack = &permanent_obstack;
871     }
872
873   t = (tree) obstack_alloc (obstack, length);
874
875 #ifdef GATHER_STATISTICS
876   tree_node_counts[(int)kind]++;
877   tree_node_sizes[(int)kind] += length;
878 #endif
879
880   /* We assume here that the length of a tree node is a multiple of the
881      size of an int.  Rounding up won't work because it would clobber
882      the next object.  */
883   for (i = (length / sizeof (int)) - 1; i >= 0; i--)
884     ((int *) t)[i] = 0;
885
886   TREE_SET_CODE (t, code);
887   if (obstack == &permanent_obstack)
888     TREE_PERMANENT (t) = 1;
889
890   switch (type)
891     {
892     case 's':
893       TREE_SIDE_EFFECTS (t) = 1;
894       TREE_TYPE (t) = void_type_node;
895       break;
896
897     case 'd':
898       if (code != FUNCTION_DECL)
899       DECL_IN_SYSTEM_HEADER (t)
900         = in_system_header && (obstack == &permanent_obstack);
901       DECL_ALIGN (t) = 1;
902       DECL_SOURCE_LINE (t) = lineno;
903       DECL_SOURCE_FILE (t) = (input_filename) ? input_filename : "<built-in>";
904       DECL_UID (t) = next_decl_uid++;
905       break;
906
907     case 't':
908       {
909         static unsigned next_type_uid = 1;
910
911         TYPE_UID (t) = next_type_uid++;
912       }
913       TYPE_ALIGN (t) = 1;
914       TYPE_MAIN_VARIANT (t) = t;
915       break;
916
917     case 'c':
918       TREE_CONSTANT (t) = 1;
919       break;
920     }
921
922   return t;
923 }
924 \f
925 /* Return a new node with the same contents as NODE
926    except that its TREE_CHAIN is zero and it has a fresh uid.  */
927
928 tree
929 copy_node (node)
930      tree node;
931 {
932   register tree t;
933   register enum tree_code code = TREE_CODE (node);
934   register int length;
935   register int i;
936
937   switch (TREE_CODE_CLASS (code))
938     {
939     case 'd':  /* A decl node */
940       length = sizeof (struct tree_decl);
941       break;
942
943     case 't':  /* a type node */
944       length = sizeof (struct tree_type);
945       break;
946
947     case 'b':  /* a lexical block node */
948       length = sizeof (struct tree_block);
949       break;
950
951     case 'r':  /* a reference */
952     case 'e':  /* a expression */
953     case 's':  /* an expression with side effects */
954     case '<':  /* a comparison expression */
955     case '1':  /* a unary arithmetic expression */
956     case '2':  /* a binary arithmetic expression */
957       length = sizeof (struct tree_exp)
958         + (tree_code_length[(int) code] - 1) * sizeof (char *);
959       break;
960
961     case 'c':  /* a constant */
962       /* We can't use tree_code_length for this, since the number of words
963          is machine-dependent due to varying alignment of `double'.  */
964       if (code == REAL_CST)
965         {
966           length = sizeof (struct tree_real_cst);
967           break;
968         }
969
970     case 'x':  /* something random, like an identifier.  */
971       length = sizeof (struct tree_common)
972         + tree_code_length[(int) code] * sizeof (char *);
973       if (code == TREE_VEC)
974         length += (TREE_VEC_LENGTH (node) - 1) * sizeof (char *);
975     }
976
977   t = (tree) obstack_alloc (current_obstack, length);
978
979   for (i = (length / sizeof (int)) - 1; i >= 0; i--)
980     ((int *) t)[i] = ((int *) node)[i];
981
982   TREE_CHAIN (t) = 0;
983
984   TREE_PERMANENT (t) = (current_obstack == &permanent_obstack);
985
986   return t;
987 }
988
989 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
990    For example, this can copy a list made of TREE_LIST nodes.  */
991
992 tree
993 copy_list (list)
994      tree list;
995 {
996   tree head;
997   register tree prev, next;
998
999   if (list == 0)
1000     return 0;
1001
1002   head = prev = copy_node (list);
1003   next = TREE_CHAIN (list);
1004   while (next)
1005     {
1006       TREE_CHAIN (prev) = copy_node (next);
1007       prev = TREE_CHAIN (prev);
1008       next = TREE_CHAIN (next);
1009     }
1010   return head;
1011 }
1012 \f
1013 #define HASHBITS 30
1014
1015 /* Return an IDENTIFIER_NODE whose name is TEXT (a null-terminated string).
1016    If an identifier with that name has previously been referred to,
1017    the same node is returned this time.  */
1018
1019 tree
1020 get_identifier (text)
1021      register char *text;
1022 {
1023   register int hi;
1024   register int i;
1025   register tree idp;
1026   register int len, hash_len;
1027
1028   /* Compute length of text in len.  */
1029   for (len = 0; text[len]; len++);
1030
1031   /* Decide how much of that length to hash on */
1032   hash_len = len;
1033   if (warn_id_clash && len > id_clash_len)
1034     hash_len = id_clash_len;
1035
1036   /* Compute hash code */
1037   hi = hash_len * 613 + (unsigned)text[0];
1038   for (i = 1; i < hash_len; i += 2)
1039     hi = ((hi * 613) + (unsigned)(text[i]));
1040
1041   hi &= (1 << HASHBITS) - 1;
1042   hi %= MAX_HASH_TABLE;
1043   
1044   /* Search table for identifier */
1045   for (idp = hash_table[hi]; idp; idp = TREE_CHAIN (idp))
1046     if (IDENTIFIER_LENGTH (idp) == len
1047         && IDENTIFIER_POINTER (idp)[0] == text[0]
1048         && !bcmp (IDENTIFIER_POINTER (idp), text, len))
1049       return idp;               /* <-- return if found */
1050
1051   /* Not found; optionally warn about a similar identifier */
1052   if (warn_id_clash && do_identifier_warnings && len >= id_clash_len)
1053     for (idp = hash_table[hi]; idp; idp = TREE_CHAIN (idp))
1054       if (!strncmp (IDENTIFIER_POINTER (idp), text, id_clash_len))
1055         {
1056           warning ("`%s' and `%s' identical in first %d characters",
1057                    IDENTIFIER_POINTER (idp), text, id_clash_len);
1058           break;
1059         }
1060
1061   if (tree_code_length[(int) IDENTIFIER_NODE] < 0)
1062     abort ();                   /* set_identifier_size hasn't been called.  */
1063
1064   /* Not found, create one, add to chain */
1065   idp = make_node (IDENTIFIER_NODE);
1066   IDENTIFIER_LENGTH (idp) = len;
1067 #ifdef GATHER_STATISTICS
1068   id_string_size += len;
1069 #endif
1070
1071   IDENTIFIER_POINTER (idp) = obstack_copy0 (&permanent_obstack, text, len);
1072
1073   TREE_CHAIN (idp) = hash_table[hi];
1074   hash_table[hi] = idp;
1075   return idp;                   /* <-- return if created */
1076 }
1077
1078 /* Enable warnings on similar identifiers (if requested).
1079    Done after the built-in identifiers are created.  */
1080
1081 void
1082 start_identifier_warnings ()
1083 {
1084   do_identifier_warnings = 1;
1085 }
1086
1087 /* Record the size of an identifier node for the language in use.
1088    SIZE is the total size in bytes.
1089    This is called by the language-specific files.  This must be
1090    called before allocating any identifiers.  */
1091
1092 void
1093 set_identifier_size (size)
1094      int size;
1095 {
1096   tree_code_length[(int) IDENTIFIER_NODE]
1097     = (size - sizeof (struct tree_common)) / sizeof (tree);
1098 }
1099 \f
1100 /* Return a newly constructed INTEGER_CST node whose constant value
1101    is specified by the two ints LOW and HI.
1102    The TREE_TYPE is set to `int'. 
1103
1104    This function should be used via the `build_int_2' macro.  */
1105
1106 tree
1107 build_int_2_wide (low, hi)
1108      HOST_WIDE_INT low, hi;
1109 {
1110   register tree t = make_node (INTEGER_CST);
1111   TREE_INT_CST_LOW (t) = low;
1112   TREE_INT_CST_HIGH (t) = hi;
1113   TREE_TYPE (t) = integer_type_node;
1114   return t;
1115 }
1116
1117 /* Return a new REAL_CST node whose type is TYPE and value is D.  */
1118
1119 tree
1120 build_real (type, d)
1121      tree type;
1122      REAL_VALUE_TYPE d;
1123 {
1124   tree v;
1125
1126   /* Check for valid float value for this type on this target machine;
1127      if not, can print error message and store a valid value in D.  */
1128 #ifdef CHECK_FLOAT_VALUE
1129   CHECK_FLOAT_VALUE (TYPE_MODE (type), d);
1130 #endif
1131
1132   v = make_node (REAL_CST);
1133   TREE_TYPE (v) = type;
1134   TREE_REAL_CST (v) = d;
1135   return v;
1136 }
1137
1138 /* Return a new REAL_CST node whose type is TYPE
1139    and whose value is the integer value of the INTEGER_CST node I.  */
1140
1141 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1142
1143 REAL_VALUE_TYPE
1144 real_value_from_int_cst (i)
1145      tree i;
1146 {
1147   REAL_VALUE_TYPE d;
1148 #ifdef REAL_ARITHMETIC
1149   REAL_VALUE_FROM_INT (d, TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i));
1150 #else /* not REAL_ARITHMETIC */
1151   if (TREE_INT_CST_HIGH (i) < 0 && ! TREE_UNSIGNED (TREE_TYPE (i)))
1152     {
1153       d = (double) (~ TREE_INT_CST_HIGH (i));
1154       d *= ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))
1155             * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)));
1156       d += (double) (unsigned HOST_WIDE_INT) (~ TREE_INT_CST_LOW (i));
1157       d = (- d - 1.0);
1158     }
1159   else
1160     {
1161       d = (double) (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (i);
1162       d *= ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))
1163             * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)));
1164       d += (double) (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (i);
1165     }
1166 #endif /* not REAL_ARITHMETIC */
1167   return d;
1168 }
1169
1170 /* This function can't be implemented if we can't do arithmetic
1171    on the float representation.  */
1172
1173 tree
1174 build_real_from_int_cst (type, i)
1175      tree type;
1176      tree i;
1177 {
1178   tree v;
1179   REAL_VALUE_TYPE d;
1180
1181   v = make_node (REAL_CST);
1182   TREE_TYPE (v) = type;
1183
1184   d = REAL_VALUE_TRUNCATE (TYPE_MODE (type), real_value_from_int_cst (i));
1185   /* Check for valid float value for this type on this target machine;
1186      if not, can print error message and store a valid value in D.  */
1187 #ifdef CHECK_FLOAT_VALUE
1188   CHECK_FLOAT_VALUE (TYPE_MODE (type), d);
1189 #endif
1190
1191   TREE_REAL_CST (v) = d;
1192   return v;
1193 }
1194
1195 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1196
1197 /* Return a newly constructed STRING_CST node whose value is
1198    the LEN characters at STR.
1199    The TREE_TYPE is not initialized.  */
1200
1201 tree
1202 build_string (len, str)
1203      int len;
1204      char *str;
1205 {
1206   register tree s = make_node (STRING_CST);
1207   TREE_STRING_LENGTH (s) = len;
1208   TREE_STRING_POINTER (s) = obstack_copy0 (saveable_obstack, str, len);
1209   return s;
1210 }
1211
1212 /* Return a newly constructed COMPLEX_CST node whose value is
1213    specified by the real and imaginary parts REAL and IMAG.
1214    Both REAL and IMAG should be constant nodes.
1215    The TREE_TYPE is not initialized.  */
1216
1217 tree
1218 build_complex (real, imag)
1219      tree real, imag;
1220 {
1221   register tree t = make_node (COMPLEX_CST);
1222   TREE_REALPART (t) = real;
1223   TREE_IMAGPART (t) = imag;
1224   return t;
1225 }
1226
1227 /* Build a newly constructed TREE_VEC node of length LEN.  */
1228 tree
1229 make_tree_vec (len)
1230      int len;
1231 {
1232   register tree t;
1233   register int length = (len-1) * sizeof (tree) + sizeof (struct tree_vec);
1234   register struct obstack *obstack = current_obstack;
1235   register int i;
1236
1237 #ifdef GATHER_STATISTICS
1238   tree_node_counts[(int)vec_kind]++;
1239   tree_node_sizes[(int)vec_kind] += length;
1240 #endif
1241
1242   t = (tree) obstack_alloc (obstack, length);
1243
1244   for (i = (length / sizeof (int)) - 1; i >= 0; i--)
1245     ((int *) t)[i] = 0;
1246
1247   TREE_SET_CODE (t, TREE_VEC);
1248   TREE_VEC_LENGTH (t) = len;
1249   if (obstack == &permanent_obstack)
1250     TREE_PERMANENT (t) = 1;
1251
1252   return t;
1253 }
1254 \f
1255 /* Return 1 if EXPR is the integer constant zero.  */
1256
1257 int
1258 integer_zerop (expr)
1259      tree expr;
1260 {
1261   STRIP_NOPS (expr);
1262
1263   return (TREE_CODE (expr) == INTEGER_CST
1264           && TREE_INT_CST_LOW (expr) == 0
1265           && TREE_INT_CST_HIGH (expr) == 0);
1266 }
1267
1268 /* Return 1 if EXPR is the integer constant one.  */
1269
1270 int
1271 integer_onep (expr)
1272      tree expr;
1273 {
1274   STRIP_NOPS (expr);
1275
1276   return (TREE_CODE (expr) == INTEGER_CST
1277           && TREE_INT_CST_LOW (expr) == 1
1278           && TREE_INT_CST_HIGH (expr) == 0);
1279 }
1280
1281 /* Return 1 if EXPR is an integer containing all 1's
1282    in as much precision as it contains.  */
1283
1284 int
1285 integer_all_onesp (expr)
1286      tree expr;
1287 {
1288   register int prec;
1289   register int uns;
1290
1291   STRIP_NOPS (expr);
1292
1293   if (TREE_CODE (expr) != INTEGER_CST)
1294     return 0;
1295
1296   uns = TREE_UNSIGNED (TREE_TYPE (expr));
1297   if (!uns)
1298     return TREE_INT_CST_LOW (expr) == -1 && TREE_INT_CST_HIGH (expr) == -1;
1299
1300   prec = TYPE_PRECISION (TREE_TYPE (expr));
1301   if (prec >= HOST_BITS_PER_WIDE_INT)
1302     {
1303       int high_value, shift_amount;
1304
1305       shift_amount = prec - HOST_BITS_PER_WIDE_INT;
1306
1307       if (shift_amount > HOST_BITS_PER_WIDE_INT)
1308         /* Can not handle precisions greater than twice the host int size.  */
1309         abort ();
1310       else if (shift_amount == HOST_BITS_PER_WIDE_INT)
1311         /* Shifting by the host word size is undefined according to the ANSI
1312            standard, so we must handle this as a special case.  */
1313         high_value = -1;
1314       else
1315         high_value = ((HOST_WIDE_INT) 1 << shift_amount) - 1;
1316
1317       return TREE_INT_CST_LOW (expr) == -1
1318         && TREE_INT_CST_HIGH (expr) == high_value;
1319     }
1320   else
1321     return TREE_INT_CST_LOW (expr) == ((HOST_WIDE_INT) 1 << prec) - 1;
1322 }
1323
1324 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
1325    one bit on).  */
1326
1327 int
1328 integer_pow2p (expr)
1329      tree expr;
1330 {
1331   HOST_WIDE_INT high, low;
1332
1333   STRIP_NOPS (expr);
1334
1335   if (TREE_CODE (expr) != INTEGER_CST)
1336     return 0;
1337
1338   high = TREE_INT_CST_HIGH (expr);
1339   low = TREE_INT_CST_LOW (expr);
1340
1341   if (high == 0 && low == 0)
1342     return 0;
1343
1344   return ((high == 0 && (low & (low - 1)) == 0)
1345           || (low == 0 && (high & (high - 1)) == 0));
1346 }
1347
1348 /* Return 1 if EXPR is the real constant zero.  */
1349
1350 int
1351 real_zerop (expr)
1352      tree expr;
1353 {
1354   STRIP_NOPS (expr);
1355
1356   return (TREE_CODE (expr) == REAL_CST
1357           && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0));
1358 }
1359
1360 /* Return 1 if EXPR is the real constant one.  */
1361
1362 int
1363 real_onep (expr)
1364      tree expr;
1365 {
1366   STRIP_NOPS (expr);
1367
1368   return (TREE_CODE (expr) == REAL_CST
1369           && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1));
1370 }
1371
1372 /* Return 1 if EXPR is the real constant two.  */
1373
1374 int
1375 real_twop (expr)
1376      tree expr;
1377 {
1378   STRIP_NOPS (expr);
1379
1380   return (TREE_CODE (expr) == REAL_CST
1381           && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst2));
1382 }
1383
1384 /* Nonzero if EXP is a constant or a cast of a constant.  */
1385  
1386 int
1387 really_constant_p (exp)
1388      tree exp;
1389 {
1390   /* This is not quite the same as STRIP_NOPS.  It does more.  */
1391   while (TREE_CODE (exp) == NOP_EXPR
1392          || TREE_CODE (exp) == CONVERT_EXPR
1393          || TREE_CODE (exp) == NON_LVALUE_EXPR)
1394     exp = TREE_OPERAND (exp, 0);
1395   return TREE_CONSTANT (exp);
1396 }
1397 \f
1398 /* Return first list element whose TREE_VALUE is ELEM.
1399    Return 0 if ELEM is not it LIST.  */
1400
1401 tree
1402 value_member (elem, list)
1403      tree elem, list;
1404 {
1405   while (list)
1406     {
1407       if (elem == TREE_VALUE (list))
1408         return list;
1409       list = TREE_CHAIN (list);
1410     }
1411   return NULL_TREE;
1412 }
1413
1414 /* Return first list element whose TREE_PURPOSE is ELEM.
1415    Return 0 if ELEM is not it LIST.  */
1416
1417 tree
1418 purpose_member (elem, list)
1419      tree elem, list;
1420 {
1421   while (list)
1422     {
1423       if (elem == TREE_PURPOSE (list))
1424         return list;
1425       list = TREE_CHAIN (list);
1426     }
1427   return NULL_TREE;
1428 }
1429
1430 /* Return first list element whose BINFO_TYPE is ELEM.
1431    Return 0 if ELEM is not it LIST.  */
1432
1433 tree
1434 binfo_member (elem, list)
1435      tree elem, list;
1436 {
1437   while (list)
1438     {
1439       if (elem == BINFO_TYPE (list))
1440         return list;
1441       list = TREE_CHAIN (list);
1442     }
1443   return NULL_TREE;
1444 }
1445
1446 /* Return nonzero if ELEM is part of the chain CHAIN.  */
1447
1448 int
1449 chain_member (elem, chain)
1450      tree elem, chain;
1451 {
1452   while (chain)
1453     {
1454       if (elem == chain)
1455         return 1;
1456       chain = TREE_CHAIN (chain);
1457     }
1458
1459   return 0;
1460 }
1461
1462 /* Return the length of a chain of nodes chained through TREE_CHAIN.
1463    We expect a null pointer to mark the end of the chain.
1464    This is the Lisp primitive `length'.  */
1465
1466 int
1467 list_length (t)
1468      tree t;
1469 {
1470   register tree tail;
1471   register int len = 0;
1472
1473   for (tail = t; tail; tail = TREE_CHAIN (tail))
1474     len++;
1475
1476   return len;
1477 }
1478
1479 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
1480    by modifying the last node in chain 1 to point to chain 2.
1481    This is the Lisp primitive `nconc'.  */
1482
1483 tree
1484 chainon (op1, op2)
1485      tree op1, op2;
1486 {
1487   tree t;
1488
1489   if (op1)
1490     {
1491       for (t = op1; TREE_CHAIN (t); t = TREE_CHAIN (t))
1492         if (t == op2) abort (); /* Circularity being created */
1493       TREE_CHAIN (t) = op2;
1494       return op1;
1495     }
1496   else return op2;
1497 }
1498
1499 /* Return the last node in a chain of nodes (chained through TREE_CHAIN).  */
1500
1501 tree
1502 tree_last (chain)
1503      register tree chain;
1504 {
1505   register tree next;
1506   if (chain)
1507     while (next = TREE_CHAIN (chain))
1508       chain = next;
1509   return chain;
1510 }
1511
1512 /* Reverse the order of elements in the chain T,
1513    and return the new head of the chain (old last element).  */
1514
1515 tree
1516 nreverse (t)
1517      tree t;
1518 {
1519   register tree prev = 0, decl, next;
1520   for (decl = t; decl; decl = next)
1521     {
1522       next = TREE_CHAIN (decl);
1523       TREE_CHAIN (decl) = prev;
1524       prev = decl;
1525     }
1526   return prev;
1527 }
1528
1529 /* Given a chain CHAIN of tree nodes,
1530    construct and return a list of those nodes.  */
1531
1532 tree
1533 listify (chain)
1534      tree chain;
1535 {
1536   tree result = NULL_TREE;
1537   tree in_tail = chain;
1538   tree out_tail = NULL_TREE;
1539
1540   while (in_tail)
1541     {
1542       tree next = tree_cons (NULL_TREE, in_tail, NULL_TREE);
1543       if (out_tail)
1544         TREE_CHAIN (out_tail) = next;
1545       else
1546         result = next;
1547       out_tail = next;
1548       in_tail = TREE_CHAIN (in_tail);
1549     }
1550
1551   return result;
1552 }
1553 \f
1554 /* Return a newly created TREE_LIST node whose
1555    purpose and value fields are PARM and VALUE.  */
1556
1557 tree
1558 build_tree_list (parm, value)
1559      tree parm, value;
1560 {
1561   register tree t = make_node (TREE_LIST);
1562   TREE_PURPOSE (t) = parm;
1563   TREE_VALUE (t) = value;
1564   return t;
1565 }
1566
1567 /* Similar, but build on the temp_decl_obstack.  */
1568
1569 tree
1570 build_decl_list (parm, value)
1571      tree parm, value;
1572 {
1573   register tree node;
1574   register struct obstack *ambient_obstack = current_obstack;
1575   current_obstack = &temp_decl_obstack;
1576   node = build_tree_list (parm, value);
1577   current_obstack = ambient_obstack;
1578   return node;
1579 }
1580
1581 /* Return a newly created TREE_LIST node whose
1582    purpose and value fields are PARM and VALUE
1583    and whose TREE_CHAIN is CHAIN.  */
1584
1585 tree
1586 tree_cons (purpose, value, chain)
1587      tree purpose, value, chain;
1588 {
1589 #if 0
1590   register tree node = make_node (TREE_LIST);
1591 #else
1592   register int i;
1593   register tree node = (tree) obstack_alloc (current_obstack, sizeof (struct tree_list));
1594 #ifdef GATHER_STATISTICS
1595   tree_node_counts[(int)x_kind]++;
1596   tree_node_sizes[(int)x_kind] += sizeof (struct tree_list);
1597 #endif
1598
1599   for (i = (sizeof (struct tree_common) / sizeof (int)) - 1; i >= 0; i--)
1600     ((int *) node)[i] = 0;
1601
1602   TREE_SET_CODE (node, TREE_LIST);
1603   if (current_obstack == &permanent_obstack)
1604     TREE_PERMANENT (node) = 1;
1605 #endif
1606
1607   TREE_CHAIN (node) = chain;
1608   TREE_PURPOSE (node) = purpose;
1609   TREE_VALUE (node) = value;
1610   return node;
1611 }
1612
1613 /* Similar, but build on the temp_decl_obstack.  */
1614
1615 tree
1616 decl_tree_cons (purpose, value, chain)
1617      tree purpose, value, chain;
1618 {
1619   register tree node;
1620   register struct obstack *ambient_obstack = current_obstack;
1621   current_obstack = &temp_decl_obstack;
1622   node = tree_cons (purpose, value, chain);
1623   current_obstack = ambient_obstack;
1624   return node;
1625 }
1626
1627 /* Same as `tree_cons' but make a permanent object.  */
1628
1629 tree
1630 perm_tree_cons (purpose, value, chain)
1631      tree purpose, value, chain;
1632 {
1633   register tree node;
1634   register struct obstack *ambient_obstack = current_obstack;
1635   current_obstack = &permanent_obstack;
1636
1637   node = tree_cons (purpose, value, chain);
1638   current_obstack = ambient_obstack;
1639   return node;
1640 }
1641
1642 /* Same as `tree_cons', but make this node temporary, regardless.  */
1643
1644 tree
1645 temp_tree_cons (purpose, value, chain)
1646      tree purpose, value, chain;
1647 {
1648   register tree node;
1649   register struct obstack *ambient_obstack = current_obstack;
1650   current_obstack = &temporary_obstack;
1651
1652   node = tree_cons (purpose, value, chain);
1653   current_obstack = ambient_obstack;
1654   return node;
1655 }
1656
1657 /* Same as `tree_cons', but save this node if the function's RTL is saved.  */
1658
1659 tree
1660 saveable_tree_cons (purpose, value, chain)
1661      tree purpose, value, chain;
1662 {
1663   register tree node;
1664   register struct obstack *ambient_obstack = current_obstack;
1665   current_obstack = saveable_obstack;
1666
1667   node = tree_cons (purpose, value, chain);
1668   current_obstack = ambient_obstack;
1669   return node;
1670 }
1671 \f
1672 /* Return the size nominally occupied by an object of type TYPE
1673    when it resides in memory.  The value is measured in units of bytes,
1674    and its data type is that normally used for type sizes
1675    (which is the first type created by make_signed_type or
1676    make_unsigned_type).  */
1677
1678 tree
1679 size_in_bytes (type)
1680      tree type;
1681 {
1682   if (type == error_mark_node)
1683     return integer_zero_node;
1684   type = TYPE_MAIN_VARIANT (type);
1685   if (TYPE_SIZE (type) == 0)
1686     {
1687       incomplete_type_error (NULL_TREE, type);
1688       return integer_zero_node;
1689     }
1690   return size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1691                      size_int (BITS_PER_UNIT));
1692 }
1693
1694 /* Return the size of TYPE (in bytes) as an integer,
1695    or return -1 if the size can vary.  */
1696
1697 int
1698 int_size_in_bytes (type)
1699      tree type;
1700 {
1701   int size;
1702   if (type == error_mark_node)
1703     return 0;
1704   type = TYPE_MAIN_VARIANT (type);
1705   if (TYPE_SIZE (type) == 0)
1706     return -1;
1707   if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
1708     return -1;
1709   size = TREE_INT_CST_LOW (TYPE_SIZE (type));
1710   return (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
1711 }
1712
1713 /* Return, as an INTEGER_CST node, the number of elements for
1714    TYPE (which is an ARRAY_TYPE) minus one. 
1715    This counts only elements of the top array.  */
1716
1717 tree
1718 array_type_nelts (type)
1719      tree type;
1720 {
1721   tree index_type = TYPE_DOMAIN (type);
1722   return (tree_int_cst_equal (TYPE_MIN_VALUE (index_type), integer_zero_node)
1723           ? TYPE_MAX_VALUE (index_type)
1724           : fold (build (MINUS_EXPR, integer_type_node,
1725                          TYPE_MAX_VALUE (index_type),
1726                          TYPE_MIN_VALUE (index_type))));
1727 }
1728 \f
1729 /* Return nonzero if arg is static -- a reference to an object in
1730    static storage.  This is not the same as the C meaning of `static'.  */
1731
1732 int
1733 staticp (arg)
1734      tree arg;
1735 {
1736   switch (TREE_CODE (arg))
1737     {
1738     case VAR_DECL:
1739     case FUNCTION_DECL:
1740     case CONSTRUCTOR:
1741       return TREE_STATIC (arg) || DECL_EXTERNAL (arg);
1742
1743     case STRING_CST:
1744       return 1;
1745
1746     case COMPONENT_REF:
1747     case BIT_FIELD_REF:
1748       return staticp (TREE_OPERAND (arg, 0));
1749
1750     case INDIRECT_REF:
1751       return TREE_CONSTANT (TREE_OPERAND (arg, 0));
1752
1753     case ARRAY_REF:
1754       if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
1755           && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
1756         return staticp (TREE_OPERAND (arg, 0));
1757     }
1758
1759   return 0;
1760 }
1761 \f
1762 /* This should be applied to any node which may be used in more than one place,
1763    but must be evaluated only once.  Normally, the code generator would
1764    reevaluate the node each time; this forces it to compute it once and save
1765    the result.  This is done by encapsulating the node in a SAVE_EXPR.  */
1766
1767 tree
1768 save_expr (expr)
1769      tree expr;
1770 {
1771   register tree t = fold (expr);
1772
1773   /* We don't care about whether this can be used as an lvalue in this
1774      context.  */
1775   while (TREE_CODE (t) == NON_LVALUE_EXPR)
1776     t = TREE_OPERAND (t, 0);
1777
1778   /* If the tree evaluates to a constant, then we don't want to hide that
1779      fact (i.e. this allows further folding, and direct checks for constants).
1780      However, a read-only object that has side effects cannot be bypassed.
1781      Since it is no problem to reevaluate literals, we just return the 
1782      literal node. */
1783
1784   if (TREE_CONSTANT (t) || (TREE_READONLY (t) && ! TREE_SIDE_EFFECTS (t))
1785       || TREE_CODE (t) == SAVE_EXPR)
1786     return t;
1787
1788   t = build (SAVE_EXPR, TREE_TYPE (expr), t, current_function_decl, NULL_TREE);
1789
1790   /* This expression might be placed ahead of a jump to ensure that the
1791      value was computed on both sides of the jump.  So make sure it isn't
1792      eliminated as dead.  */
1793   TREE_SIDE_EFFECTS (t) = 1;
1794   return t;
1795 }
1796
1797 /* Stabilize a reference so that we can use it any number of times
1798    without causing its operands to be evaluated more than once.
1799    Returns the stabilized reference.
1800
1801    Also allows conversion expressions whose operands are references.
1802    Any other kind of expression is returned unchanged.  */
1803
1804 tree
1805 stabilize_reference (ref)
1806      tree ref;
1807 {
1808   register tree result;
1809   register enum tree_code code = TREE_CODE (ref);
1810
1811   switch (code)
1812     {
1813     case VAR_DECL:
1814     case PARM_DECL:
1815     case RESULT_DECL:
1816       /* No action is needed in this case.  */
1817       return ref;
1818
1819     case NOP_EXPR:
1820     case CONVERT_EXPR:
1821     case FLOAT_EXPR:
1822     case FIX_TRUNC_EXPR:
1823     case FIX_FLOOR_EXPR:
1824     case FIX_ROUND_EXPR:
1825     case FIX_CEIL_EXPR:
1826       result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
1827       break;
1828
1829     case INDIRECT_REF:
1830       result = build_nt (INDIRECT_REF,
1831                          stabilize_reference_1 (TREE_OPERAND (ref, 0)));
1832       break;
1833
1834     case COMPONENT_REF:
1835       result = build_nt (COMPONENT_REF,
1836                          stabilize_reference (TREE_OPERAND (ref, 0)),
1837                          TREE_OPERAND (ref, 1));
1838       break;
1839
1840     case BIT_FIELD_REF:
1841       result = build_nt (BIT_FIELD_REF,
1842                          stabilize_reference (TREE_OPERAND (ref, 0)),
1843                          stabilize_reference_1 (TREE_OPERAND (ref, 1)),
1844                          stabilize_reference_1 (TREE_OPERAND (ref, 2)));
1845       break;
1846
1847     case ARRAY_REF:
1848       result = build_nt (ARRAY_REF,
1849                          stabilize_reference (TREE_OPERAND (ref, 0)),
1850                          stabilize_reference_1 (TREE_OPERAND (ref, 1)));
1851       break;
1852
1853       /* If arg isn't a kind of lvalue we recognize, make no change.
1854          Caller should recognize the error for an invalid lvalue.  */
1855     default:
1856       return ref;
1857
1858     case ERROR_MARK:
1859       return error_mark_node;
1860     }
1861
1862   TREE_TYPE (result) = TREE_TYPE (ref);
1863   TREE_READONLY (result) = TREE_READONLY (ref);
1864   TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
1865   TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
1866   TREE_RAISES (result) = TREE_RAISES (ref);
1867
1868   return result;
1869 }
1870
1871 /* Subroutine of stabilize_reference; this is called for subtrees of
1872    references.  Any expression with side-effects must be put in a SAVE_EXPR
1873    to ensure that it is only evaluated once.
1874
1875    We don't put SAVE_EXPR nodes around everything, because assigning very
1876    simple expressions to temporaries causes us to miss good opportunities
1877    for optimizations.  Among other things, the opportunity to fold in the
1878    addition of a constant into an addressing mode often gets lost, e.g.
1879    "y[i+1] += x;".  In general, we take the approach that we should not make
1880    an assignment unless we are forced into it - i.e., that any non-side effect
1881    operator should be allowed, and that cse should take care of coalescing
1882    multiple utterances of the same expression should that prove fruitful.  */
1883
1884 static tree
1885 stabilize_reference_1 (e)
1886      tree e;
1887 {
1888   register tree result;
1889   register int length;
1890   register enum tree_code code = TREE_CODE (e);
1891
1892   /* We cannot ignore const expressions because it might be a reference
1893      to a const array but whose index contains side-effects.  But we can
1894      ignore things that are actual constant or that already have been
1895      handled by this function.  */
1896
1897   if (TREE_CONSTANT (e) || code == SAVE_EXPR)
1898     return e;
1899
1900   switch (TREE_CODE_CLASS (code))
1901     {
1902     case 'x':
1903     case 't':
1904     case 'd':
1905     case 'b':
1906     case '<':
1907     case 's':
1908     case 'e':
1909     case 'r':
1910       /* If the expression has side-effects, then encase it in a SAVE_EXPR
1911          so that it will only be evaluated once.  */
1912       /* The reference (r) and comparison (<) classes could be handled as
1913          below, but it is generally faster to only evaluate them once.  */
1914       if (TREE_SIDE_EFFECTS (e))
1915         return save_expr (e);
1916       return e;
1917
1918     case 'c':
1919       /* Constants need no processing.  In fact, we should never reach
1920          here.  */
1921       return e;
1922       
1923     case '2':
1924       /* Recursively stabilize each operand.  */
1925       result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
1926                          stabilize_reference_1 (TREE_OPERAND (e, 1)));
1927       break;
1928
1929     case '1':
1930       /* Recursively stabilize each operand.  */
1931       result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
1932       break;
1933     }
1934   
1935   TREE_TYPE (result) = TREE_TYPE (e);
1936   TREE_READONLY (result) = TREE_READONLY (e);
1937   TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
1938   TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
1939   TREE_RAISES (result) = TREE_RAISES (e);
1940
1941   return result;
1942 }
1943 \f
1944 /* Low-level constructors for expressions.  */
1945
1946 /* Build an expression of code CODE, data type TYPE,
1947    and operands as specified by the arguments ARG1 and following arguments.
1948    Expressions and reference nodes can be created this way.
1949    Constants, decls, types and misc nodes cannot be.  */
1950
1951 tree
1952 build (va_alist)
1953      va_dcl
1954 {
1955   va_list p;
1956   enum tree_code code;
1957   register tree t;
1958   register int length;
1959   register int i;
1960
1961   va_start (p);
1962
1963   code = va_arg (p, enum tree_code);
1964   t = make_node (code);
1965   length = tree_code_length[(int) code];
1966   TREE_TYPE (t) = va_arg (p, tree);
1967
1968   if (length == 2)
1969     {
1970       /* This is equivalent to the loop below, but faster.  */
1971       register tree arg0 = va_arg (p, tree);
1972       register tree arg1 = va_arg (p, tree);
1973       TREE_OPERAND (t, 0) = arg0;
1974       TREE_OPERAND (t, 1) = arg1;
1975       if ((arg0 && TREE_SIDE_EFFECTS (arg0))
1976           || (arg1 && TREE_SIDE_EFFECTS (arg1)))
1977         TREE_SIDE_EFFECTS (t) = 1;
1978       TREE_RAISES (t)
1979         = (arg0 && TREE_RAISES (arg0)) || (arg1 && TREE_RAISES (arg1));
1980     }
1981   else if (length == 1)
1982     {
1983       register tree arg0 = va_arg (p, tree);
1984
1985       /* Call build1 for this!  */
1986       if (TREE_CODE_CLASS (code) != 's')
1987         abort ();
1988       TREE_OPERAND (t, 0) = arg0;
1989       if (arg0 && TREE_SIDE_EFFECTS (arg0))
1990         TREE_SIDE_EFFECTS (t) = 1;
1991       TREE_RAISES (t) = (arg0 && TREE_RAISES (arg0));
1992     }
1993   else
1994     {
1995       for (i = 0; i < length; i++)
1996         {
1997           register tree operand = va_arg (p, tree);
1998           TREE_OPERAND (t, i) = operand;
1999           if (operand)
2000             {
2001               if (TREE_SIDE_EFFECTS (operand))
2002                 TREE_SIDE_EFFECTS (t) = 1;
2003               if (TREE_RAISES (operand))
2004                 TREE_RAISES (t) = 1;
2005             }
2006         }
2007     }
2008   va_end (p);
2009   return t;
2010 }
2011
2012 /* Same as above, but only builds for unary operators.
2013    Saves lions share of calls to `build'; cuts down use
2014    of varargs, which is expensive for RISC machines.  */
2015 tree
2016 build1 (code, type, node)
2017      enum tree_code code;
2018      tree type;
2019      tree node;
2020 {
2021   register struct obstack *obstack = current_obstack;
2022   register int i, length;
2023   register tree_node_kind kind;
2024   register tree t;
2025
2026 #ifdef GATHER_STATISTICS
2027   if (TREE_CODE_CLASS (code) == 'r')
2028     kind = r_kind;
2029   else
2030     kind = e_kind;
2031 #endif
2032
2033   obstack = expression_obstack;
2034   length = sizeof (struct tree_exp);
2035
2036   t = (tree) obstack_alloc (obstack, length);
2037
2038 #ifdef GATHER_STATISTICS
2039   tree_node_counts[(int)kind]++;
2040   tree_node_sizes[(int)kind] += length;
2041 #endif
2042
2043   for (i = (length / sizeof (int)) - 1; i >= 0; i--)
2044     ((int *) t)[i] = 0;
2045
2046   TREE_TYPE (t) = type;
2047   TREE_SET_CODE (t, code);
2048
2049   if (obstack == &permanent_obstack)
2050     TREE_PERMANENT (t) = 1;
2051
2052   TREE_OPERAND (t, 0) = node;
2053   if (node)
2054     {
2055       if (TREE_SIDE_EFFECTS (node))
2056         TREE_SIDE_EFFECTS (t) = 1;
2057       if (TREE_RAISES (node))
2058         TREE_RAISES (t) = 1;
2059     }
2060
2061   return t;
2062 }
2063
2064 /* Similar except don't specify the TREE_TYPE
2065    and leave the TREE_SIDE_EFFECTS as 0.
2066    It is permissible for arguments to be null,
2067    or even garbage if their values do not matter.  */
2068
2069 tree
2070 build_nt (va_alist)
2071      va_dcl
2072 {
2073   va_list p;
2074   register enum tree_code code;
2075   register tree t;
2076   register int length;
2077   register int i;
2078
2079   va_start (p);
2080
2081   code = va_arg (p, enum tree_code);
2082   t = make_node (code);
2083   length = tree_code_length[(int) code];
2084
2085   for (i = 0; i < length; i++)
2086     TREE_OPERAND (t, i) = va_arg (p, tree);
2087
2088   va_end (p);
2089   return t;
2090 }
2091
2092 /* Similar to `build_nt', except we build
2093    on the temp_decl_obstack, regardless.  */
2094
2095 tree
2096 build_parse_node (va_alist)
2097      va_dcl
2098 {
2099   register struct obstack *ambient_obstack = expression_obstack;
2100   va_list p;
2101   register enum tree_code code;
2102   register tree t;
2103   register int length;
2104   register int i;
2105
2106   expression_obstack = &temp_decl_obstack;
2107
2108   va_start (p);
2109
2110   code = va_arg (p, enum tree_code);
2111   t = make_node (code);
2112   length = tree_code_length[(int) code];
2113
2114   for (i = 0; i < length; i++)
2115     TREE_OPERAND (t, i) = va_arg (p, tree);
2116
2117   va_end (p);
2118   expression_obstack = ambient_obstack;
2119   return t;
2120 }
2121
2122 #if 0
2123 /* Commented out because this wants to be done very
2124    differently.  See cp-lex.c.  */
2125 tree
2126 build_op_identifier (op1, op2)
2127      tree op1, op2;
2128 {
2129   register tree t = make_node (OP_IDENTIFIER);
2130   TREE_PURPOSE (t) = op1;
2131   TREE_VALUE (t) = op2;
2132   return t;
2133 }
2134 #endif
2135 \f
2136 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
2137    We do NOT enter this node in any sort of symbol table.
2138
2139    layout_decl is used to set up the decl's storage layout.
2140    Other slots are initialized to 0 or null pointers.  */
2141
2142 tree
2143 build_decl (code, name, type)
2144      enum tree_code code;
2145      tree name, type;
2146 {
2147   register tree t;
2148
2149   t = make_node (code);
2150
2151 /*  if (type == error_mark_node)
2152     type = integer_type_node; */
2153 /* That is not done, deliberately, so that having error_mark_node
2154    as the type can suppress useless errors in the use of this variable.  */
2155
2156   DECL_NAME (t) = name;
2157   DECL_ASSEMBLER_NAME (t) = name;
2158   TREE_TYPE (t) = type;
2159
2160   if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
2161     layout_decl (t, 0);
2162   else if (code == FUNCTION_DECL)
2163     DECL_MODE (t) = FUNCTION_MODE;
2164
2165   return t;
2166 }
2167 \f
2168 /* BLOCK nodes are used to represent the structure of binding contours
2169    and declarations, once those contours have been exited and their contents
2170    compiled.  This information is used for outputting debugging info.
2171    A BLOCK may have a "controller" which is a BIND_EXPR node.
2172    Then the BLOCK is ignored unless the controller has the TREE_USED flag.  */
2173
2174 tree
2175 build_block (vars, tags, subblocks, supercontext, chain)
2176      tree vars, tags, subblocks, supercontext, chain;
2177 {
2178   register tree block = make_node (BLOCK);
2179   BLOCK_VARS (block) = vars;
2180   BLOCK_TYPE_TAGS (block) = tags;
2181   BLOCK_SUBBLOCKS (block) = subblocks;
2182   BLOCK_SUPERCONTEXT (block) = supercontext;
2183   BLOCK_CHAIN (block) = chain;
2184   return block;
2185 }
2186 \f
2187 /* Return a type like TYPE except that its TYPE_READONLY is CONSTP
2188    and its TYPE_VOLATILE is VOLATILEP.
2189
2190    Such variant types already made are recorded so that duplicates
2191    are not made.
2192
2193    A variant types should never be used as the type of an expression.
2194    Always copy the variant information into the TREE_READONLY
2195    and TREE_THIS_VOLATILE of the expression, and then give the expression
2196    as its type the "main variant", the variant whose TYPE_READONLY
2197    and TYPE_VOLATILE are zero.  Use TYPE_MAIN_VARIANT to find the
2198    main variant.  */
2199
2200 tree
2201 build_type_variant (type, constp, volatilep)
2202      tree type;
2203      int constp, volatilep;
2204 {
2205   register tree t, m = TYPE_MAIN_VARIANT (type);
2206   register struct obstack *ambient_obstack = current_obstack;
2207
2208   /* Treat any nonzero argument as 1.  */
2209   constp = !!constp;
2210   volatilep = !!volatilep;
2211
2212   /* If not generating auxiliary info, search the chain of variants to see
2213      if there is already one there just like the one we need to have.  If so,
2214      use that existing one.
2215
2216      We don't do this in the case where we are generating aux info because
2217      in that case we want each typedef names to get it's own distinct type
2218      node, even if the type of this new typedef is the same as some other
2219      (existing) type.  */
2220
2221   if (!flag_gen_aux_info)
2222     for (t = m; t; t = TYPE_NEXT_VARIANT (t))
2223       if (constp == TYPE_READONLY (t) && volatilep == TYPE_VOLATILE (t))
2224         return t;
2225
2226   /* We need a new one.  */
2227   current_obstack
2228     = TREE_PERMANENT (type) ? &permanent_obstack : saveable_obstack;
2229
2230   t = copy_node (type);
2231   TYPE_READONLY (t) = constp;
2232   TYPE_VOLATILE (t) = volatilep;
2233   TYPE_POINTER_TO (t) = 0;
2234   TYPE_REFERENCE_TO (t) = 0;
2235
2236   /* Add this type to the chain of variants of TYPE.  */
2237   TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
2238   TYPE_NEXT_VARIANT (m) = t;
2239
2240   current_obstack = ambient_obstack;
2241   return t;
2242 }
2243
2244 /* Create a new variant of TYPE, equivalent but distinct.
2245    This is so the caller can modify it.  */
2246
2247 tree
2248 build_type_copy (type)
2249      tree type;
2250 {
2251   register tree t, m = TYPE_MAIN_VARIANT (type);
2252   register struct obstack *ambient_obstack = current_obstack;
2253
2254   current_obstack
2255     = TREE_PERMANENT (type) ? &permanent_obstack : saveable_obstack;
2256
2257   t = copy_node (type);
2258   TYPE_POINTER_TO (t) = 0;
2259   TYPE_REFERENCE_TO (t) = 0;
2260
2261   /* Add this type to the chain of variants of TYPE.  */
2262   TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
2263   TYPE_NEXT_VARIANT (m) = t;
2264
2265   current_obstack = ambient_obstack;
2266   return t;
2267 }
2268 \f
2269 /* Hashing of types so that we don't make duplicates.
2270    The entry point is `type_hash_canon'.  */
2271
2272 /* Each hash table slot is a bucket containing a chain
2273    of these structures.  */
2274
2275 struct type_hash
2276 {
2277   struct type_hash *next;       /* Next structure in the bucket.  */
2278   int hashcode;                 /* Hash code of this type.  */
2279   tree type;                    /* The type recorded here.  */
2280 };
2281
2282 /* Now here is the hash table.  When recording a type, it is added
2283    to the slot whose index is the hash code mod the table size.
2284    Note that the hash table is used for several kinds of types
2285    (function types, array types and array index range types, for now).
2286    While all these live in the same table, they are completely independent,
2287    and the hash code is computed differently for each of these.  */
2288
2289 #define TYPE_HASH_SIZE 59
2290 struct type_hash *type_hash_table[TYPE_HASH_SIZE];
2291
2292 /* Here is how primitive or already-canonicalized types' hash
2293    codes are made.  */
2294 #define TYPE_HASH(TYPE) ((HOST_WIDE_INT) (TYPE) & 0777777)
2295
2296 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
2297    with types in the TREE_VALUE slots), by adding the hash codes
2298    of the individual types.  */
2299
2300 int
2301 type_hash_list (list)
2302      tree list;
2303 {
2304   register int hashcode;
2305   register tree tail;
2306   for (hashcode = 0, tail = list; tail; tail = TREE_CHAIN (tail))
2307     hashcode += TYPE_HASH (TREE_VALUE (tail));
2308   return hashcode;
2309 }
2310
2311 /* Look in the type hash table for a type isomorphic to TYPE.
2312    If one is found, return it.  Otherwise return 0.  */
2313
2314 tree
2315 type_hash_lookup (hashcode, type)
2316      int hashcode;
2317      tree type;
2318 {
2319   register struct type_hash *h;
2320   for (h = type_hash_table[hashcode % TYPE_HASH_SIZE]; h; h = h->next)
2321     if (h->hashcode == hashcode
2322         && TREE_CODE (h->type) == TREE_CODE (type)
2323         && TREE_TYPE (h->type) == TREE_TYPE (type)
2324         && (TYPE_MAX_VALUE (h->type) == TYPE_MAX_VALUE (type)
2325             || tree_int_cst_equal (TYPE_MAX_VALUE (h->type),
2326                                    TYPE_MAX_VALUE (type)))
2327         && (TYPE_MIN_VALUE (h->type) == TYPE_MIN_VALUE (type)
2328             || tree_int_cst_equal (TYPE_MIN_VALUE (h->type),
2329                                    TYPE_MIN_VALUE (type)))
2330         && (TYPE_DOMAIN (h->type) == TYPE_DOMAIN (type)
2331             || (TYPE_DOMAIN (h->type)
2332                 && TREE_CODE (TYPE_DOMAIN (h->type)) == TREE_LIST
2333                 && TYPE_DOMAIN (type)
2334                 && TREE_CODE (TYPE_DOMAIN (type)) == TREE_LIST
2335                 && type_list_equal (TYPE_DOMAIN (h->type), TYPE_DOMAIN (type)))))
2336       return h->type;
2337   return 0;
2338 }
2339
2340 /* Add an entry to the type-hash-table
2341    for a type TYPE whose hash code is HASHCODE.  */
2342
2343 void
2344 type_hash_add (hashcode, type)
2345      int hashcode;
2346      tree type;
2347 {
2348   register struct type_hash *h;
2349
2350   h = (struct type_hash *) oballoc (sizeof (struct type_hash));
2351   h->hashcode = hashcode;
2352   h->type = type;
2353   h->next = type_hash_table[hashcode % TYPE_HASH_SIZE];
2354   type_hash_table[hashcode % TYPE_HASH_SIZE] = h;
2355 }
2356
2357 /* Given TYPE, and HASHCODE its hash code, return the canonical
2358    object for an identical type if one already exists.
2359    Otherwise, return TYPE, and record it as the canonical object
2360    if it is a permanent object.
2361
2362    To use this function, first create a type of the sort you want.
2363    Then compute its hash code from the fields of the type that
2364    make it different from other similar types.
2365    Then call this function and use the value.
2366    This function frees the type you pass in if it is a duplicate.  */
2367
2368 /* Set to 1 to debug without canonicalization.  Never set by program.  */
2369 int debug_no_type_hash = 0;
2370
2371 tree
2372 type_hash_canon (hashcode, type)
2373      int hashcode;
2374      tree type;
2375 {
2376   tree t1;
2377
2378   if (debug_no_type_hash)
2379     return type;
2380
2381   t1 = type_hash_lookup (hashcode, type);
2382   if (t1 != 0)
2383     {
2384       struct obstack *o
2385         = TREE_PERMANENT (type) ? &permanent_obstack : saveable_obstack;
2386       obstack_free (o, type);
2387 #ifdef GATHER_STATISTICS
2388       tree_node_counts[(int)t_kind]--;
2389       tree_node_sizes[(int)t_kind] -= sizeof (struct tree_type);
2390 #endif
2391       return t1;
2392     }
2393
2394   /* If this is a new type, record it for later reuse.  */
2395   if (current_obstack == &permanent_obstack)
2396     type_hash_add (hashcode, type);
2397
2398   return type;
2399 }
2400
2401 /* Given two lists of types
2402    (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
2403    return 1 if the lists contain the same types in the same order.
2404    Also, the TREE_PURPOSEs must match.  */
2405
2406 int
2407 type_list_equal (l1, l2)
2408      tree l1, l2;
2409 {
2410   register tree t1, t2;
2411   for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
2412     {
2413       if (TREE_VALUE (t1) != TREE_VALUE (t2))
2414         return 0;
2415       if (TREE_PURPOSE (t1) != TREE_PURPOSE (t2))
2416         {
2417           int cmp = simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2));
2418           if (cmp < 0)
2419             abort ();
2420           if (cmp == 0)
2421             return 0;
2422         }
2423     }
2424
2425   return t1 == t2;
2426 }
2427
2428 /* Nonzero if integer constants T1 and T2
2429    represent the same constant value.  */
2430
2431 int
2432 tree_int_cst_equal (t1, t2)
2433      tree t1, t2;
2434 {
2435   if (t1 == t2)
2436     return 1;
2437   if (t1 == 0 || t2 == 0)
2438     return 0;
2439   if (TREE_CODE (t1) == INTEGER_CST
2440       && TREE_CODE (t2) == INTEGER_CST
2441       && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
2442       && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
2443     return 1;
2444   return 0;
2445 }
2446
2447 /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
2448    The precise way of comparison depends on their data type.  */
2449
2450 int
2451 tree_int_cst_lt (t1, t2)
2452      tree t1, t2;
2453 {
2454   if (t1 == t2)
2455     return 0;
2456
2457   if (!TREE_UNSIGNED (TREE_TYPE (t1)))
2458     return INT_CST_LT (t1, t2);
2459   return INT_CST_LT_UNSIGNED (t1, t2);
2460 }
2461
2462 /* Compare two constructor-element-type constants.  */
2463 int
2464 simple_cst_list_equal (l1, l2)
2465      tree l1, l2;
2466 {
2467   while (l1 != NULL_TREE && l2 != NULL_TREE)
2468     {
2469       int cmp = simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2));
2470       if (cmp < 0)
2471         abort ();
2472       if (cmp == 0)
2473         return 0;
2474       l1 = TREE_CHAIN (l1);
2475       l2 = TREE_CHAIN (l2);
2476     }
2477   return (l1 == l2);
2478 }
2479
2480 /* Return truthvalue of whether T1 is the same tree structure as T2.
2481    Return 1 if they are the same.
2482    Return 0 if they are understandably different.
2483    Return -1 if either contains tree structure not understood by
2484    this function.  */
2485
2486 int
2487 simple_cst_equal (t1, t2)
2488      tree t1, t2;
2489 {
2490   register enum tree_code code1, code2;
2491   int cmp;
2492
2493   if (t1 == t2)
2494     return 1;
2495   if (t1 == 0 || t2 == 0)
2496     return 0;
2497
2498   code1 = TREE_CODE (t1);
2499   code2 = TREE_CODE (t2);
2500
2501   if (code1 == NOP_EXPR || code1 == CONVERT_EXPR || code1 == NON_LVALUE_EXPR)
2502     if (code2 == NOP_EXPR || code2 == CONVERT_EXPR || code2 == NON_LVALUE_EXPR)
2503       return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2504     else
2505       return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
2506   else if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
2507            || code2 == NON_LVALUE_EXPR)
2508     return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
2509
2510   if (code1 != code2)
2511     return 0;
2512
2513   switch (code1)
2514     {
2515     case INTEGER_CST:
2516       return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
2517         && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
2518
2519     case REAL_CST:
2520       return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
2521
2522     case STRING_CST:
2523       return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
2524         && !bcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
2525                   TREE_STRING_LENGTH (t1));
2526
2527     case CONSTRUCTOR:
2528       abort ();
2529
2530     case SAVE_EXPR:
2531       return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2532
2533     case CALL_EXPR:
2534       cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2535       if (cmp <= 0)
2536         return cmp;
2537       return simple_cst_list_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
2538
2539     case TARGET_EXPR:
2540       /* Special case: if either target is an unallocated VAR_DECL,
2541          it means that it's going to be unified with whatever the
2542          TARGET_EXPR is really supposed to initialize, so treat it
2543          as being equivalent to anything.  */
2544       if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
2545            && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
2546            && DECL_RTL (TREE_OPERAND (t1, 0)) == 0)
2547           || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
2548               && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
2549               && DECL_RTL (TREE_OPERAND (t2, 0)) == 0))
2550         cmp = 1;
2551       else
2552         cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2553       if (cmp <= 0)
2554         return cmp;
2555       return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
2556
2557     case WITH_CLEANUP_EXPR:
2558       cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2559       if (cmp <= 0)
2560         return cmp;
2561       return simple_cst_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t1, 2));
2562
2563     case COMPONENT_REF:
2564       if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
2565         return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2566       return 0;
2567
2568     case BIT_FIELD_REF:
2569       return (simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
2570               && simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1))
2571               && simple_cst_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t2, 2)));
2572
2573     case VAR_DECL:
2574     case PARM_DECL:
2575     case CONST_DECL:
2576     case FUNCTION_DECL:
2577       return 0;
2578
2579     case PLUS_EXPR:
2580     case MINUS_EXPR:
2581     case MULT_EXPR:
2582     case TRUNC_DIV_EXPR:
2583     case TRUNC_MOD_EXPR:
2584     case LSHIFT_EXPR:
2585     case RSHIFT_EXPR:
2586       cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2587       if (cmp <= 0)
2588         return cmp;
2589       return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
2590
2591     case NEGATE_EXPR:
2592     case ADDR_EXPR:
2593     case REFERENCE_EXPR:
2594     case INDIRECT_REF:
2595       return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2596
2597     default:
2598 #if 0
2599       return lang_simple_cst_equal (t1, t2);
2600 #else
2601       return -1;
2602 #endif
2603     }
2604 }
2605 \f
2606 /* Constructors for pointer, array and function types.
2607    (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
2608    constructed by language-dependent code, not here.)  */
2609
2610 /* Construct, lay out and return the type of pointers to TO_TYPE.
2611    If such a type has already been constructed, reuse it.  */
2612
2613 tree
2614 build_pointer_type (to_type)
2615      tree to_type;
2616 {
2617   register tree t = TYPE_POINTER_TO (to_type);
2618   register struct obstack *ambient_obstack = current_obstack;
2619   register struct obstack *ambient_saveable_obstack = saveable_obstack;
2620
2621   /* First, if we already have a type for pointers to TO_TYPE, use it.  */
2622
2623   if (t)
2624     return t;
2625
2626   /* We need a new one.  If TO_TYPE is permanent, make this permanent too.  */
2627   if (TREE_PERMANENT (to_type))
2628     {
2629       current_obstack = &permanent_obstack;
2630       saveable_obstack = &permanent_obstack;
2631     }
2632
2633   t = make_node (POINTER_TYPE);
2634   TREE_TYPE (t) = to_type;
2635
2636   /* Record this type as the pointer to TO_TYPE.  */
2637   TYPE_POINTER_TO (to_type) = t;
2638
2639   /* Lay out the type.  This function has many callers that are concerned
2640      with expression-construction, and this simplifies them all.
2641      Also, it guarantees the TYPE_SIZE is permanent if the type is.  */
2642   layout_type (t);
2643
2644   current_obstack = ambient_obstack;
2645   saveable_obstack = ambient_saveable_obstack;
2646   return t;
2647 }
2648
2649 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
2650    MAXVAL should be the maximum value in the domain
2651    (one less than the length of the array).  */
2652
2653 tree
2654 build_index_type (maxval)
2655      tree maxval;
2656 {
2657   register tree itype = make_node (INTEGER_TYPE);
2658   TYPE_PRECISION (itype) = TYPE_PRECISION (sizetype);
2659   TYPE_MIN_VALUE (itype) = build_int_2 (0, 0);
2660   TREE_TYPE (TYPE_MIN_VALUE (itype)) = sizetype;
2661   TYPE_MAX_VALUE (itype) = convert (sizetype, maxval);
2662   TYPE_MODE (itype) = TYPE_MODE (sizetype);
2663   TYPE_SIZE (itype) = TYPE_SIZE (sizetype);
2664   TYPE_ALIGN (itype) = TYPE_ALIGN (sizetype);
2665   if (TREE_CODE (maxval) == INTEGER_CST)
2666     {
2667       HOST_WIDE_INT maxint = TREE_INT_CST_LOW (maxval);
2668       return type_hash_canon (maxint > 0 ? maxint : - maxint, itype);
2669     }
2670   else
2671     return itype;
2672 }
2673
2674 /* Just like build_index_type, but takes lowval and highval instead
2675    of just highval (maxval). */
2676
2677 tree
2678 build_index_2_type (lowval,highval)
2679      tree lowval, highval;
2680 {
2681   register tree itype = make_node (INTEGER_TYPE);
2682   TYPE_PRECISION (itype) = TYPE_PRECISION (sizetype);
2683   TYPE_MIN_VALUE (itype) = convert (sizetype, lowval);
2684   TYPE_MAX_VALUE (itype) = convert (sizetype, highval);
2685   TYPE_MODE (itype) = TYPE_MODE (sizetype);
2686   TYPE_SIZE (itype) = TYPE_SIZE (sizetype);
2687   TYPE_ALIGN (itype) = TYPE_ALIGN (sizetype);
2688   if ((TREE_CODE (lowval) == INTEGER_CST)
2689       && (TREE_CODE (highval) == INTEGER_CST))
2690     {
2691       HOST_WIDE_INT highint = TREE_INT_CST_LOW (highval);
2692       HOST_WIDE_INT lowint = TREE_INT_CST_LOW (lowval);
2693       HOST_WIDE_INT maxint = highint - lowint;
2694       return type_hash_canon (maxint > 0 ? maxint : - maxint, itype);
2695     }
2696   else
2697     return itype;
2698 }
2699
2700 /* Return nonzero iff ITYPE1 and ITYPE2 are equal (in the LISP sense).
2701    Needed because when index types are not hashed, equal index types
2702    built at different times appear distinct, even though structurally,
2703    they are not.  */
2704
2705 int
2706 index_type_equal (itype1, itype2)
2707      tree itype1, itype2;
2708 {
2709   if (TREE_CODE (itype1) != TREE_CODE (itype2))
2710     return 0;
2711   if (TREE_CODE (itype1) == INTEGER_TYPE)
2712     {
2713       if (TYPE_PRECISION (itype1) != TYPE_PRECISION (itype2)
2714           || TYPE_MODE (itype1) != TYPE_MODE (itype2)
2715           || ! simple_cst_equal (TYPE_SIZE (itype1), TYPE_SIZE (itype2))
2716           || TYPE_ALIGN (itype1) != TYPE_ALIGN (itype2))
2717         return 0;
2718       if (simple_cst_equal (TYPE_MIN_VALUE (itype1), TYPE_MIN_VALUE (itype2))
2719           && simple_cst_equal (TYPE_MAX_VALUE (itype1), TYPE_MAX_VALUE (itype2)))
2720         return 1;
2721     }
2722   return 0;
2723 }
2724
2725 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
2726    and number of elements specified by the range of values of INDEX_TYPE.
2727    If such a type has already been constructed, reuse it.  */
2728
2729 tree
2730 build_array_type (elt_type, index_type)
2731      tree elt_type, index_type;
2732 {
2733   register tree t;
2734   int hashcode;
2735
2736   if (TREE_CODE (elt_type) == FUNCTION_TYPE)
2737     {
2738       error ("arrays of functions are not meaningful");
2739       elt_type = integer_type_node;
2740     }
2741
2742   /* Make sure TYPE_POINTER_TO (elt_type) is filled in.  */
2743   build_pointer_type (elt_type);
2744
2745   /* Allocate the array after the pointer type,
2746      in case we free it in type_hash_canon.  */
2747   t = make_node (ARRAY_TYPE);
2748   TREE_TYPE (t) = elt_type;
2749   TYPE_DOMAIN (t) = index_type;
2750
2751   if (index_type == 0)
2752     return t;
2753
2754   hashcode = TYPE_HASH (elt_type) + TYPE_HASH (index_type);
2755   t = type_hash_canon (hashcode, t);
2756
2757   if (TYPE_SIZE (t) == 0)
2758     layout_type (t);
2759   return t;
2760 }
2761
2762 /* Construct, lay out and return
2763    the type of functions returning type VALUE_TYPE
2764    given arguments of types ARG_TYPES.
2765    ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
2766    are data type nodes for the arguments of the function.
2767    If such a type has already been constructed, reuse it.  */
2768
2769 tree
2770 build_function_type (value_type, arg_types)
2771      tree value_type, arg_types;
2772 {
2773   register tree t;
2774   int hashcode;
2775
2776   if (TREE_CODE (value_type) == FUNCTION_TYPE
2777       || TREE_CODE (value_type) == ARRAY_TYPE)
2778     {
2779       error ("function return type cannot be function or array");
2780       value_type = integer_type_node;
2781     }
2782
2783   /* Make a node of the sort we want.  */
2784   t = make_node (FUNCTION_TYPE);
2785   TREE_TYPE (t) = value_type;
2786   TYPE_ARG_TYPES (t) = arg_types;
2787
2788   /* If we already have such a type, use the old one and free this one.  */
2789   hashcode = TYPE_HASH (value_type) + type_hash_list (arg_types);
2790   t = type_hash_canon (hashcode, t);
2791
2792   if (TYPE_SIZE (t) == 0)
2793     layout_type (t);
2794   return t;
2795 }
2796
2797 /* Build the node for the type of references-to-TO_TYPE.  */
2798
2799 tree
2800 build_reference_type (to_type)
2801      tree to_type;
2802 {
2803   register tree t = TYPE_REFERENCE_TO (to_type);
2804   register struct obstack *ambient_obstack = current_obstack;
2805   register struct obstack *ambient_saveable_obstack = saveable_obstack;
2806
2807   /* First, if we already have a type for pointers to TO_TYPE, use it.  */
2808
2809   if (t)
2810     return t;
2811
2812   /* We need a new one.  If TO_TYPE is permanent, make this permanent too.  */
2813   if (TREE_PERMANENT (to_type))
2814     {
2815       current_obstack = &permanent_obstack;
2816       saveable_obstack = &permanent_obstack;
2817     }
2818
2819   t = make_node (REFERENCE_TYPE);
2820   TREE_TYPE (t) = to_type;
2821
2822   /* Record this type as the pointer to TO_TYPE.  */
2823   TYPE_REFERENCE_TO (to_type) = t;
2824
2825   layout_type (t);
2826
2827   current_obstack = ambient_obstack;
2828   saveable_obstack = ambient_saveable_obstack;
2829   return t;
2830 }
2831
2832 /* Construct, lay out and return the type of methods belonging to class
2833    BASETYPE and whose arguments and values are described by TYPE.
2834    If that type exists already, reuse it.
2835    TYPE must be a FUNCTION_TYPE node.  */
2836
2837 tree
2838 build_method_type (basetype, type)
2839      tree basetype, type;
2840 {
2841   register tree t;
2842   int hashcode;
2843
2844   /* Make a node of the sort we want.  */
2845   t = make_node (METHOD_TYPE);
2846
2847   if (TREE_CODE (type) != FUNCTION_TYPE)
2848     abort ();
2849
2850   TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
2851   TREE_TYPE (t) = TREE_TYPE (type);
2852
2853   /* The actual arglist for this function includes a "hidden" argument
2854      which is "this".  Put it into the list of argument types.  */
2855
2856   TYPE_ARG_TYPES (t)
2857     = tree_cons (NULL_TREE,
2858                  build_pointer_type (basetype), TYPE_ARG_TYPES (type));
2859
2860   /* If we already have such a type, use the old one and free this one.  */
2861   hashcode = TYPE_HASH (basetype) + TYPE_HASH (type);
2862   t = type_hash_canon (hashcode, t);
2863
2864   if (TYPE_SIZE (t) == 0)
2865     layout_type (t);
2866
2867   return t;
2868 }
2869
2870 /* Construct, lay out and return the type of methods belonging to class
2871    BASETYPE and whose arguments and values are described by TYPE.
2872    If that type exists already, reuse it.
2873    TYPE must be a FUNCTION_TYPE node.  */
2874
2875 tree
2876 build_offset_type (basetype, type)
2877      tree basetype, type;
2878 {
2879   register tree t;
2880   int hashcode;
2881
2882   /* Make a node of the sort we want.  */
2883   t = make_node (OFFSET_TYPE);
2884
2885   TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
2886   TREE_TYPE (t) = type;
2887
2888   /* If we already have such a type, use the old one and free this one.  */
2889   hashcode = TYPE_HASH (basetype) + TYPE_HASH (type);
2890   t = type_hash_canon (hashcode, t);
2891
2892   if (TYPE_SIZE (t) == 0)
2893     layout_type (t);
2894
2895   return t;
2896 }
2897
2898 /* Create a complex type whose components are COMPONENT_TYPE.  */
2899
2900 tree
2901 build_complex_type (component_type)
2902      tree component_type;
2903 {
2904   register tree t;
2905   int hashcode;
2906
2907   /* Make a node of the sort we want.  */
2908   t = make_node (COMPLEX_TYPE);
2909
2910   TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
2911   TYPE_VOLATILE (t) = TYPE_VOLATILE (component_type);
2912   TYPE_READONLY (t) = TYPE_READONLY (component_type);
2913
2914   /* If we already have such a type, use the old one and free this one.  */
2915   hashcode = TYPE_HASH (component_type);
2916   t = type_hash_canon (hashcode, t);
2917
2918   if (TYPE_SIZE (t) == 0)
2919     layout_type (t);
2920
2921   return t;
2922 }
2923 \f
2924 /* Return OP, stripped of any conversions to wider types as much as is safe.
2925    Converting the value back to OP's type makes a value equivalent to OP.
2926
2927    If FOR_TYPE is nonzero, we return a value which, if converted to
2928    type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
2929
2930    If FOR_TYPE is nonzero, unaligned bit-field references may be changed to the
2931    narrowest type that can hold the value, even if they don't exactly fit.
2932    Otherwise, bit-field references are changed to a narrower type
2933    only if they can be fetched directly from memory in that type.
2934
2935    OP must have integer, real or enumeral type.  Pointers are not allowed!
2936
2937    There are some cases where the obvious value we could return
2938    would regenerate to OP if converted to OP's type, 
2939    but would not extend like OP to wider types.
2940    If FOR_TYPE indicates such extension is contemplated, we eschew such values.
2941    For example, if OP is (unsigned short)(signed char)-1,
2942    we avoid returning (signed char)-1 if FOR_TYPE is int,
2943    even though extending that to an unsigned short would regenerate OP,
2944    since the result of extending (signed char)-1 to (int)
2945    is different from (int) OP.  */
2946
2947 tree
2948 get_unwidened (op, for_type)
2949      register tree op;
2950      tree for_type;
2951 {
2952   /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension.  */
2953   /* TYPE_PRECISION is safe in place of type_precision since
2954      pointer types are not allowed.  */
2955   register tree type = TREE_TYPE (op);
2956   register unsigned final_prec
2957     = TYPE_PRECISION (for_type != 0 ? for_type : type);
2958   register int uns
2959     = (for_type != 0 && for_type != type
2960        && final_prec > TYPE_PRECISION (type)
2961        && TREE_UNSIGNED (type));
2962   register tree win = op;
2963
2964   while (TREE_CODE (op) == NOP_EXPR)
2965     {
2966       register int bitschange
2967         = TYPE_PRECISION (TREE_TYPE (op))
2968           - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
2969
2970       /* Truncations are many-one so cannot be removed.
2971          Unless we are later going to truncate down even farther.  */
2972       if (bitschange < 0
2973           && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
2974         break;
2975
2976       /* See what's inside this conversion.  If we decide to strip it,
2977          we will set WIN.  */
2978       op = TREE_OPERAND (op, 0);
2979
2980       /* If we have not stripped any zero-extensions (uns is 0),
2981          we can strip any kind of extension.
2982          If we have previously stripped a zero-extension,
2983          only zero-extensions can safely be stripped.
2984          Any extension can be stripped if the bits it would produce
2985          are all going to be discarded later by truncating to FOR_TYPE.  */
2986
2987       if (bitschange > 0)
2988         {
2989           if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
2990             win = op;
2991           /* TREE_UNSIGNED says whether this is a zero-extension.
2992              Let's avoid computing it if it does not affect WIN
2993              and if UNS will not be needed again.  */
2994           if ((uns || TREE_CODE (op) == NOP_EXPR)
2995               && TREE_UNSIGNED (TREE_TYPE (op)))
2996             {
2997               uns = 1;
2998               win = op;
2999             }
3000         }
3001     }
3002
3003   if (TREE_CODE (op) == COMPONENT_REF
3004       /* Since type_for_size always gives an integer type.  */
3005       && TREE_CODE (type) != REAL_TYPE)
3006     {
3007       unsigned innerprec = TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (op, 1)));
3008       type = type_for_size (innerprec, TREE_UNSIGNED (TREE_OPERAND (op, 1)));
3009
3010       /* We can get this structure field in the narrowest type it fits in.
3011          If FOR_TYPE is 0, do this only for a field that matches the
3012          narrower type exactly and is aligned for it
3013          The resulting extension to its nominal type (a fullword type)
3014          must fit the same conditions as for other extensions.  */
3015
3016       if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
3017           && (for_type || ! DECL_BIT_FIELD (TREE_OPERAND (op, 1)))
3018           && (! uns || final_prec <= innerprec
3019               || TREE_UNSIGNED (TREE_OPERAND (op, 1)))
3020           && type != 0)
3021         {
3022           win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
3023                        TREE_OPERAND (op, 1));
3024           TREE_SIDE_EFFECTS (win) = TREE_SIDE_EFFECTS (op);
3025           TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
3026           TREE_RAISES (win) = TREE_RAISES (op);
3027         }
3028     }
3029   return win;
3030 }
3031 \f
3032 /* Return OP or a simpler expression for a narrower value
3033    which can be sign-extended or zero-extended to give back OP.
3034    Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
3035    or 0 if the value should be sign-extended.  */
3036
3037 tree
3038 get_narrower (op, unsignedp_ptr)
3039      register tree op;
3040      int *unsignedp_ptr;
3041 {
3042   register int uns = 0;
3043   int first = 1;
3044   register tree win = op;
3045
3046   while (TREE_CODE (op) == NOP_EXPR)
3047     {
3048       register int bitschange
3049         = TYPE_PRECISION (TREE_TYPE (op))
3050           - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
3051
3052       /* Truncations are many-one so cannot be removed.  */
3053       if (bitschange < 0)
3054         break;
3055
3056       /* See what's inside this conversion.  If we decide to strip it,
3057          we will set WIN.  */
3058       op = TREE_OPERAND (op, 0);
3059
3060       if (bitschange > 0)
3061         {
3062           /* An extension: the outermost one can be stripped,
3063              but remember whether it is zero or sign extension.  */
3064           if (first)
3065             uns = TREE_UNSIGNED (TREE_TYPE (op));
3066           /* Otherwise, if a sign extension has been stripped,
3067              only sign extensions can now be stripped;
3068              if a zero extension has been stripped, only zero-extensions.  */
3069           else if (uns != TREE_UNSIGNED (TREE_TYPE (op)))
3070             break;
3071           first = 0;
3072         }
3073       /* A change in nominal type can always be stripped.  */
3074
3075       win = op;
3076     }
3077
3078   if (TREE_CODE (op) == COMPONENT_REF
3079       /* Since type_for_size always gives an integer type.  */
3080       && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE)
3081     {
3082       unsigned innerprec = TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (op, 1)));
3083       tree type = type_for_size (innerprec, TREE_UNSIGNED (op));
3084
3085       /* We can get this structure field in a narrower type that fits it,
3086          but the resulting extension to its nominal type (a fullword type)
3087          must satisfy the same conditions as for other extensions.
3088
3089          Do this only for fields that are aligned (not bit-fields),
3090          because when bit-field insns will be used there is no
3091          advantage in doing this.  */
3092
3093       if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
3094           && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
3095           && (first || uns == TREE_UNSIGNED (TREE_OPERAND (op, 1)))
3096           && type != 0)
3097         {
3098           if (first)
3099             uns = TREE_UNSIGNED (TREE_OPERAND (op, 1));
3100           win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
3101                        TREE_OPERAND (op, 1));
3102           TREE_SIDE_EFFECTS (win) = TREE_SIDE_EFFECTS (op);
3103           TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
3104           TREE_RAISES (win) = TREE_RAISES (op);
3105         }
3106     }
3107   *unsignedp_ptr = uns;
3108   return win;
3109 }
3110 \f
3111 /* Return the precision of a type, for arithmetic purposes.
3112    Supports all types on which arithmetic is possible
3113    (including pointer types).
3114    It's not clear yet what will be right for complex types.  */
3115
3116 int
3117 type_precision (type)
3118      register tree type;
3119 {
3120   return ((TREE_CODE (type) == INTEGER_TYPE
3121            || TREE_CODE (type) == ENUMERAL_TYPE
3122            || TREE_CODE (type) == REAL_TYPE)
3123           ? TYPE_PRECISION (type) : POINTER_SIZE);
3124 }
3125
3126 /* Nonzero if integer constant C has a value that is permissible
3127    for type TYPE (an INTEGER_TYPE).  */
3128
3129 int
3130 int_fits_type_p (c, type)
3131      tree c, type;
3132 {
3133   if (TREE_UNSIGNED (type))
3134     return (!INT_CST_LT_UNSIGNED (TYPE_MAX_VALUE (type), c)
3135             && !INT_CST_LT_UNSIGNED (c, TYPE_MIN_VALUE (type)));
3136   else
3137     return (!INT_CST_LT (TYPE_MAX_VALUE (type), c)
3138             && !INT_CST_LT (c, TYPE_MIN_VALUE (type)));
3139 }
3140
3141 /* Return the innermost context enclosing DECL that is
3142    a FUNCTION_DECL, or zero if none.  */
3143
3144 tree
3145 decl_function_context (decl)
3146      tree decl;
3147 {
3148   tree context;
3149
3150   if (TREE_CODE (decl) == ERROR_MARK)
3151     return 0;
3152
3153   if (TREE_CODE (decl) == SAVE_EXPR)
3154     context = SAVE_EXPR_CONTEXT (decl);
3155   else
3156     context = DECL_CONTEXT (decl);
3157
3158   while (context && TREE_CODE (context) != FUNCTION_DECL)
3159     {
3160       if (TREE_CODE (context) == RECORD_TYPE
3161           || TREE_CODE (context) == UNION_TYPE)
3162         context = TYPE_CONTEXT (context);
3163       else if (TREE_CODE (context) == TYPE_DECL)
3164         context = DECL_CONTEXT (context);
3165       else if (TREE_CODE (context) == BLOCK)
3166         context = BLOCK_SUPERCONTEXT (context);
3167       else
3168         /* Unhandled CONTEXT !?  */
3169         abort ();
3170     }
3171
3172   return context;
3173 }
3174
3175 /* Return the innermost context enclosing DECL that is
3176    a RECORD_TYPE or UNION_TYPE, or zero if none.
3177    TYPE_DECLs and FUNCTION_DECLs are transparent to this function.  */
3178
3179 tree
3180 decl_type_context (decl)
3181      tree decl;
3182 {
3183   tree context = DECL_CONTEXT (decl);
3184
3185   while (context)
3186     {
3187       if (TREE_CODE (context) == RECORD_TYPE
3188           || TREE_CODE (context) == UNION_TYPE)
3189         return context;
3190       if (TREE_CODE (context) == TYPE_DECL
3191           || TREE_CODE (context) == FUNCTION_DECL)
3192         context = DECL_CONTEXT (context);
3193       else if (TREE_CODE (context) == BLOCK)
3194         context = BLOCK_SUPERCONTEXT (context);
3195       else
3196         /* Unhandled CONTEXT!?  */
3197         abort ();
3198     }
3199   return NULL_TREE;
3200 }
3201
3202 void
3203 print_obstack_statistics (str, o)
3204      char *str;
3205      struct obstack *o;
3206 {
3207   struct _obstack_chunk *chunk = o->chunk;
3208   int n_chunks = 0;
3209   int n_alloc = 0;
3210
3211   while (chunk)
3212     {
3213       n_chunks += 1;
3214       n_alloc += chunk->limit - &chunk->contents[0];
3215       chunk = chunk->prev;
3216     }
3217   fprintf (stderr, "obstack %s: %d bytes, %d chunks\n",
3218            str, n_alloc, n_chunks);
3219 }
3220 void
3221 dump_tree_statistics ()
3222 {
3223   int i;
3224   int total_nodes, total_bytes;
3225
3226   fprintf (stderr, "\n??? tree nodes created\n\n");
3227 #ifdef GATHER_STATISTICS
3228   fprintf (stderr, "Kind                  Nodes     Bytes\n");
3229   fprintf (stderr, "-------------------------------------\n");
3230   total_nodes = total_bytes = 0;
3231   for (i = 0; i < (int) all_kinds; i++)
3232     {
3233       fprintf (stderr, "%-20s %6d %9d\n", tree_node_kind_names[i],
3234                tree_node_counts[i], tree_node_sizes[i]);
3235       total_nodes += tree_node_counts[i];
3236       total_bytes += tree_node_sizes[i];
3237     }
3238   fprintf (stderr, "%-20s        %9d\n", "identifier names", id_string_size);
3239   fprintf (stderr, "-------------------------------------\n");
3240   fprintf (stderr, "%-20s %6d %9d\n", "Total", total_nodes, total_bytes);
3241   fprintf (stderr, "-------------------------------------\n");
3242 #else
3243   fprintf (stderr, "(No per-node statistics)\n");
3244 #endif
3245   print_lang_statistics ();
3246 }