OSDN Git Service

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