OSDN Git Service

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