[AArch64] Fix vqtb[lx][234] on big-endian
[official-gcc.git] / gcc / tree.c
blob5b9a7bdc2fe6b0018b1facaa811013a7d3a64237
1 /* Language-independent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This file contains the low level primitives for operating on tree nodes,
21 including allocation, list operations, interning of identifiers,
22 construction of data type nodes and statement nodes,
23 and construction of type conversion nodes. It also contains
24 tables index by tree code that describe how to take apart
25 nodes of that code.
27 It is intended to be language-independent but can occasionally
28 calls language-dependent routines. */
30 #include "config.h"
31 #include "system.h"
32 #include "coretypes.h"
33 #include "backend.h"
34 #include "target.h"
35 #include "tree.h"
36 #include "gimple.h"
37 #include "tree-pass.h"
38 #include "ssa.h"
39 #include "cgraph.h"
40 #include "diagnostic.h"
41 #include "flags.h"
42 #include "alias.h"
43 #include "fold-const.h"
44 #include "stor-layout.h"
45 #include "calls.h"
46 #include "attribs.h"
47 #include "toplev.h" /* get_random_seed */
48 #include "output.h"
49 #include "common/common-target.h"
50 #include "langhooks.h"
51 #include "tree-inline.h"
52 #include "tree-iterator.h"
53 #include "internal-fn.h"
54 #include "gimple-iterator.h"
55 #include "gimplify.h"
56 #include "tree-dfa.h"
57 #include "params.h"
58 #include "langhooks-def.h"
59 #include "tree-diagnostic.h"
60 #include "except.h"
61 #include "builtins.h"
62 #include "print-tree.h"
63 #include "ipa-utils.h"
65 /* Tree code classes. */
67 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
68 #define END_OF_BASE_TREE_CODES tcc_exceptional,
70 const enum tree_code_class tree_code_type[] = {
71 #include "all-tree.def"
74 #undef DEFTREECODE
75 #undef END_OF_BASE_TREE_CODES
77 /* Table indexed by tree code giving number of expression
78 operands beyond the fixed part of the node structure.
79 Not used for types or decls. */
81 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
82 #define END_OF_BASE_TREE_CODES 0,
84 const unsigned char tree_code_length[] = {
85 #include "all-tree.def"
88 #undef DEFTREECODE
89 #undef END_OF_BASE_TREE_CODES
91 /* Names of tree components.
92 Used for printing out the tree and error messages. */
93 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
94 #define END_OF_BASE_TREE_CODES "@dummy",
96 static const char *const tree_code_name[] = {
97 #include "all-tree.def"
100 #undef DEFTREECODE
101 #undef END_OF_BASE_TREE_CODES
103 /* Each tree code class has an associated string representation.
104 These must correspond to the tree_code_class entries. */
106 const char *const tree_code_class_strings[] =
108 "exceptional",
109 "constant",
110 "type",
111 "declaration",
112 "reference",
113 "comparison",
114 "unary",
115 "binary",
116 "statement",
117 "vl_exp",
118 "expression"
121 /* obstack.[ch] explicitly declined to prototype this. */
122 extern int _obstack_allocated_p (struct obstack *h, void *obj);
124 /* Statistics-gathering stuff. */
126 static int tree_code_counts[MAX_TREE_CODES];
127 int tree_node_counts[(int) all_kinds];
128 int tree_node_sizes[(int) all_kinds];
130 /* Keep in sync with tree.h:enum tree_node_kind. */
131 static const char * const tree_node_kind_names[] = {
132 "decls",
133 "types",
134 "blocks",
135 "stmts",
136 "refs",
137 "exprs",
138 "constants",
139 "identifiers",
140 "vecs",
141 "binfos",
142 "ssa names",
143 "constructors",
144 "random kinds",
145 "lang_decl kinds",
146 "lang_type kinds",
147 "omp clauses",
150 /* Unique id for next decl created. */
151 static GTY(()) int next_decl_uid;
152 /* Unique id for next type created. */
153 static GTY(()) int next_type_uid = 1;
154 /* Unique id for next debug decl created. Use negative numbers,
155 to catch erroneous uses. */
156 static GTY(()) int next_debug_decl_uid;
158 /* Since we cannot rehash a type after it is in the table, we have to
159 keep the hash code. */
161 struct GTY((for_user)) type_hash {
162 unsigned long hash;
163 tree type;
166 /* Initial size of the hash table (rounded to next prime). */
167 #define TYPE_HASH_INITIAL_SIZE 1000
169 struct type_cache_hasher : ggc_cache_ptr_hash<type_hash>
171 static hashval_t hash (type_hash *t) { return t->hash; }
172 static bool equal (type_hash *a, type_hash *b);
174 static int
175 keep_cache_entry (type_hash *&t)
177 return ggc_marked_p (t->type);
181 /* Now here is the hash table. When recording a type, it is added to
182 the slot whose index is the hash code. Note that the hash table is
183 used for several kinds of types (function types, array types and
184 array index range types, for now). While all these live in the
185 same table, they are completely independent, and the hash code is
186 computed differently for each of these. */
188 static GTY ((cache)) hash_table<type_cache_hasher> *type_hash_table;
190 /* Hash table and temporary node for larger integer const values. */
191 static GTY (()) tree int_cst_node;
193 struct int_cst_hasher : ggc_cache_ptr_hash<tree_node>
195 static hashval_t hash (tree t);
196 static bool equal (tree x, tree y);
199 static GTY ((cache)) hash_table<int_cst_hasher> *int_cst_hash_table;
201 /* Hash table for optimization flags and target option flags. Use the same
202 hash table for both sets of options. Nodes for building the current
203 optimization and target option nodes. The assumption is most of the time
204 the options created will already be in the hash table, so we avoid
205 allocating and freeing up a node repeatably. */
206 static GTY (()) tree cl_optimization_node;
207 static GTY (()) tree cl_target_option_node;
209 struct cl_option_hasher : ggc_cache_ptr_hash<tree_node>
211 static hashval_t hash (tree t);
212 static bool equal (tree x, tree y);
215 static GTY ((cache)) hash_table<cl_option_hasher> *cl_option_hash_table;
217 /* General tree->tree mapping structure for use in hash tables. */
220 static GTY ((cache))
221 hash_table<tree_decl_map_cache_hasher> *debug_expr_for_decl;
223 static GTY ((cache))
224 hash_table<tree_decl_map_cache_hasher> *value_expr_for_decl;
226 struct tree_vec_map_cache_hasher : ggc_cache_ptr_hash<tree_vec_map>
228 static hashval_t hash (tree_vec_map *m) { return DECL_UID (m->base.from); }
230 static bool
231 equal (tree_vec_map *a, tree_vec_map *b)
233 return a->base.from == b->base.from;
236 static int
237 keep_cache_entry (tree_vec_map *&m)
239 return ggc_marked_p (m->base.from);
243 static GTY ((cache))
244 hash_table<tree_vec_map_cache_hasher> *debug_args_for_decl;
246 static void set_type_quals (tree, int);
247 static void print_type_hash_statistics (void);
248 static void print_debug_expr_statistics (void);
249 static void print_value_expr_statistics (void);
250 static void type_hash_list (const_tree, inchash::hash &);
251 static void attribute_hash_list (const_tree, inchash::hash &);
253 tree global_trees[TI_MAX];
254 tree integer_types[itk_none];
256 bool int_n_enabled_p[NUM_INT_N_ENTS];
257 struct int_n_trees_t int_n_trees [NUM_INT_N_ENTS];
259 unsigned char tree_contains_struct[MAX_TREE_CODES][64];
261 /* Number of operands for each OpenMP clause. */
262 unsigned const char omp_clause_num_ops[] =
264 0, /* OMP_CLAUSE_ERROR */
265 1, /* OMP_CLAUSE_PRIVATE */
266 1, /* OMP_CLAUSE_SHARED */
267 1, /* OMP_CLAUSE_FIRSTPRIVATE */
268 2, /* OMP_CLAUSE_LASTPRIVATE */
269 5, /* OMP_CLAUSE_REDUCTION */
270 1, /* OMP_CLAUSE_COPYIN */
271 1, /* OMP_CLAUSE_COPYPRIVATE */
272 3, /* OMP_CLAUSE_LINEAR */
273 2, /* OMP_CLAUSE_ALIGNED */
274 1, /* OMP_CLAUSE_DEPEND */
275 1, /* OMP_CLAUSE_UNIFORM */
276 1, /* OMP_CLAUSE_TO_DECLARE */
277 1, /* OMP_CLAUSE_LINK */
278 2, /* OMP_CLAUSE_FROM */
279 2, /* OMP_CLAUSE_TO */
280 2, /* OMP_CLAUSE_MAP */
281 1, /* OMP_CLAUSE_USE_DEVICE_PTR */
282 1, /* OMP_CLAUSE_IS_DEVICE_PTR */
283 2, /* OMP_CLAUSE__CACHE_ */
284 1, /* OMP_CLAUSE_DEVICE_RESIDENT */
285 1, /* OMP_CLAUSE_USE_DEVICE */
286 2, /* OMP_CLAUSE_GANG */
287 1, /* OMP_CLAUSE_ASYNC */
288 1, /* OMP_CLAUSE_WAIT */
289 0, /* OMP_CLAUSE_AUTO */
290 0, /* OMP_CLAUSE_SEQ */
291 1, /* OMP_CLAUSE__LOOPTEMP_ */
292 1, /* OMP_CLAUSE_IF */
293 1, /* OMP_CLAUSE_NUM_THREADS */
294 1, /* OMP_CLAUSE_SCHEDULE */
295 0, /* OMP_CLAUSE_NOWAIT */
296 1, /* OMP_CLAUSE_ORDERED */
297 0, /* OMP_CLAUSE_DEFAULT */
298 3, /* OMP_CLAUSE_COLLAPSE */
299 0, /* OMP_CLAUSE_UNTIED */
300 1, /* OMP_CLAUSE_FINAL */
301 0, /* OMP_CLAUSE_MERGEABLE */
302 1, /* OMP_CLAUSE_DEVICE */
303 1, /* OMP_CLAUSE_DIST_SCHEDULE */
304 0, /* OMP_CLAUSE_INBRANCH */
305 0, /* OMP_CLAUSE_NOTINBRANCH */
306 1, /* OMP_CLAUSE_NUM_TEAMS */
307 1, /* OMP_CLAUSE_THREAD_LIMIT */
308 0, /* OMP_CLAUSE_PROC_BIND */
309 1, /* OMP_CLAUSE_SAFELEN */
310 1, /* OMP_CLAUSE_SIMDLEN */
311 0, /* OMP_CLAUSE_FOR */
312 0, /* OMP_CLAUSE_PARALLEL */
313 0, /* OMP_CLAUSE_SECTIONS */
314 0, /* OMP_CLAUSE_TASKGROUP */
315 1, /* OMP_CLAUSE_PRIORITY */
316 1, /* OMP_CLAUSE_GRAINSIZE */
317 1, /* OMP_CLAUSE_NUM_TASKS */
318 0, /* OMP_CLAUSE_NOGROUP */
319 0, /* OMP_CLAUSE_THREADS */
320 0, /* OMP_CLAUSE_SIMD */
321 1, /* OMP_CLAUSE_HINT */
322 0, /* OMP_CLAUSE_DEFALTMAP */
323 1, /* OMP_CLAUSE__SIMDUID_ */
324 1, /* OMP_CLAUSE__CILK_FOR_COUNT_ */
325 0, /* OMP_CLAUSE_INDEPENDENT */
326 1, /* OMP_CLAUSE_WORKER */
327 1, /* OMP_CLAUSE_VECTOR */
328 1, /* OMP_CLAUSE_NUM_GANGS */
329 1, /* OMP_CLAUSE_NUM_WORKERS */
330 1, /* OMP_CLAUSE_VECTOR_LENGTH */
331 1, /* OMP_CLAUSE_TILE */
334 const char * const omp_clause_code_name[] =
336 "error_clause",
337 "private",
338 "shared",
339 "firstprivate",
340 "lastprivate",
341 "reduction",
342 "copyin",
343 "copyprivate",
344 "linear",
345 "aligned",
346 "depend",
347 "uniform",
348 "to",
349 "link",
350 "from",
351 "to",
352 "map",
353 "use_device_ptr",
354 "is_device_ptr",
355 "_cache_",
356 "device_resident",
357 "use_device",
358 "gang",
359 "async",
360 "wait",
361 "auto",
362 "seq",
363 "_looptemp_",
364 "if",
365 "num_threads",
366 "schedule",
367 "nowait",
368 "ordered",
369 "default",
370 "collapse",
371 "untied",
372 "final",
373 "mergeable",
374 "device",
375 "dist_schedule",
376 "inbranch",
377 "notinbranch",
378 "num_teams",
379 "thread_limit",
380 "proc_bind",
381 "safelen",
382 "simdlen",
383 "for",
384 "parallel",
385 "sections",
386 "taskgroup",
387 "priority",
388 "grainsize",
389 "num_tasks",
390 "nogroup",
391 "threads",
392 "simd",
393 "hint",
394 "defaultmap",
395 "_simduid_",
396 "_Cilk_for_count_",
397 "independent",
398 "worker",
399 "vector",
400 "num_gangs",
401 "num_workers",
402 "vector_length",
403 "tile"
407 /* Return the tree node structure used by tree code CODE. */
409 static inline enum tree_node_structure_enum
410 tree_node_structure_for_code (enum tree_code code)
412 switch (TREE_CODE_CLASS (code))
414 case tcc_declaration:
416 switch (code)
418 case FIELD_DECL:
419 return TS_FIELD_DECL;
420 case PARM_DECL:
421 return TS_PARM_DECL;
422 case VAR_DECL:
423 return TS_VAR_DECL;
424 case LABEL_DECL:
425 return TS_LABEL_DECL;
426 case RESULT_DECL:
427 return TS_RESULT_DECL;
428 case DEBUG_EXPR_DECL:
429 return TS_DECL_WRTL;
430 case CONST_DECL:
431 return TS_CONST_DECL;
432 case TYPE_DECL:
433 return TS_TYPE_DECL;
434 case FUNCTION_DECL:
435 return TS_FUNCTION_DECL;
436 case TRANSLATION_UNIT_DECL:
437 return TS_TRANSLATION_UNIT_DECL;
438 default:
439 return TS_DECL_NON_COMMON;
442 case tcc_type:
443 return TS_TYPE_NON_COMMON;
444 case tcc_reference:
445 case tcc_comparison:
446 case tcc_unary:
447 case tcc_binary:
448 case tcc_expression:
449 case tcc_statement:
450 case tcc_vl_exp:
451 return TS_EXP;
452 default: /* tcc_constant and tcc_exceptional */
453 break;
455 switch (code)
457 /* tcc_constant cases. */
458 case VOID_CST: return TS_TYPED;
459 case INTEGER_CST: return TS_INT_CST;
460 case REAL_CST: return TS_REAL_CST;
461 case FIXED_CST: return TS_FIXED_CST;
462 case COMPLEX_CST: return TS_COMPLEX;
463 case VECTOR_CST: return TS_VECTOR;
464 case STRING_CST: return TS_STRING;
465 /* tcc_exceptional cases. */
466 case ERROR_MARK: return TS_COMMON;
467 case IDENTIFIER_NODE: return TS_IDENTIFIER;
468 case TREE_LIST: return TS_LIST;
469 case TREE_VEC: return TS_VEC;
470 case SSA_NAME: return TS_SSA_NAME;
471 case PLACEHOLDER_EXPR: return TS_COMMON;
472 case STATEMENT_LIST: return TS_STATEMENT_LIST;
473 case BLOCK: return TS_BLOCK;
474 case CONSTRUCTOR: return TS_CONSTRUCTOR;
475 case TREE_BINFO: return TS_BINFO;
476 case OMP_CLAUSE: return TS_OMP_CLAUSE;
477 case OPTIMIZATION_NODE: return TS_OPTIMIZATION;
478 case TARGET_OPTION_NODE: return TS_TARGET_OPTION;
480 default:
481 gcc_unreachable ();
486 /* Initialize tree_contains_struct to describe the hierarchy of tree
487 nodes. */
489 static void
490 initialize_tree_contains_struct (void)
492 unsigned i;
494 for (i = ERROR_MARK; i < LAST_AND_UNUSED_TREE_CODE; i++)
496 enum tree_code code;
497 enum tree_node_structure_enum ts_code;
499 code = (enum tree_code) i;
500 ts_code = tree_node_structure_for_code (code);
502 /* Mark the TS structure itself. */
503 tree_contains_struct[code][ts_code] = 1;
505 /* Mark all the structures that TS is derived from. */
506 switch (ts_code)
508 case TS_TYPED:
509 case TS_BLOCK:
510 MARK_TS_BASE (code);
511 break;
513 case TS_COMMON:
514 case TS_INT_CST:
515 case TS_REAL_CST:
516 case TS_FIXED_CST:
517 case TS_VECTOR:
518 case TS_STRING:
519 case TS_COMPLEX:
520 case TS_SSA_NAME:
521 case TS_CONSTRUCTOR:
522 case TS_EXP:
523 case TS_STATEMENT_LIST:
524 MARK_TS_TYPED (code);
525 break;
527 case TS_IDENTIFIER:
528 case TS_DECL_MINIMAL:
529 case TS_TYPE_COMMON:
530 case TS_LIST:
531 case TS_VEC:
532 case TS_BINFO:
533 case TS_OMP_CLAUSE:
534 case TS_OPTIMIZATION:
535 case TS_TARGET_OPTION:
536 MARK_TS_COMMON (code);
537 break;
539 case TS_TYPE_WITH_LANG_SPECIFIC:
540 MARK_TS_TYPE_COMMON (code);
541 break;
543 case TS_TYPE_NON_COMMON:
544 MARK_TS_TYPE_WITH_LANG_SPECIFIC (code);
545 break;
547 case TS_DECL_COMMON:
548 MARK_TS_DECL_MINIMAL (code);
549 break;
551 case TS_DECL_WRTL:
552 case TS_CONST_DECL:
553 MARK_TS_DECL_COMMON (code);
554 break;
556 case TS_DECL_NON_COMMON:
557 MARK_TS_DECL_WITH_VIS (code);
558 break;
560 case TS_DECL_WITH_VIS:
561 case TS_PARM_DECL:
562 case TS_LABEL_DECL:
563 case TS_RESULT_DECL:
564 MARK_TS_DECL_WRTL (code);
565 break;
567 case TS_FIELD_DECL:
568 MARK_TS_DECL_COMMON (code);
569 break;
571 case TS_VAR_DECL:
572 MARK_TS_DECL_WITH_VIS (code);
573 break;
575 case TS_TYPE_DECL:
576 case TS_FUNCTION_DECL:
577 MARK_TS_DECL_NON_COMMON (code);
578 break;
580 case TS_TRANSLATION_UNIT_DECL:
581 MARK_TS_DECL_COMMON (code);
582 break;
584 default:
585 gcc_unreachable ();
589 /* Basic consistency checks for attributes used in fold. */
590 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_NON_COMMON]);
591 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_NON_COMMON]);
592 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_COMMON]);
593 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_COMMON]);
594 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_COMMON]);
595 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_COMMON]);
596 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_COMMON]);
597 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_COMMON]);
598 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_COMMON]);
599 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_COMMON]);
600 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_COMMON]);
601 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WRTL]);
602 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_WRTL]);
603 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_WRTL]);
604 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WRTL]);
605 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_WRTL]);
606 gcc_assert (tree_contains_struct[CONST_DECL][TS_DECL_MINIMAL]);
607 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_MINIMAL]);
608 gcc_assert (tree_contains_struct[PARM_DECL][TS_DECL_MINIMAL]);
609 gcc_assert (tree_contains_struct[RESULT_DECL][TS_DECL_MINIMAL]);
610 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_MINIMAL]);
611 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_MINIMAL]);
612 gcc_assert (tree_contains_struct[TRANSLATION_UNIT_DECL][TS_DECL_MINIMAL]);
613 gcc_assert (tree_contains_struct[LABEL_DECL][TS_DECL_MINIMAL]);
614 gcc_assert (tree_contains_struct[FIELD_DECL][TS_DECL_MINIMAL]);
615 gcc_assert (tree_contains_struct[VAR_DECL][TS_DECL_WITH_VIS]);
616 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_DECL_WITH_VIS]);
617 gcc_assert (tree_contains_struct[TYPE_DECL][TS_DECL_WITH_VIS]);
618 gcc_assert (tree_contains_struct[VAR_DECL][TS_VAR_DECL]);
619 gcc_assert (tree_contains_struct[FIELD_DECL][TS_FIELD_DECL]);
620 gcc_assert (tree_contains_struct[PARM_DECL][TS_PARM_DECL]);
621 gcc_assert (tree_contains_struct[LABEL_DECL][TS_LABEL_DECL]);
622 gcc_assert (tree_contains_struct[RESULT_DECL][TS_RESULT_DECL]);
623 gcc_assert (tree_contains_struct[CONST_DECL][TS_CONST_DECL]);
624 gcc_assert (tree_contains_struct[TYPE_DECL][TS_TYPE_DECL]);
625 gcc_assert (tree_contains_struct[FUNCTION_DECL][TS_FUNCTION_DECL]);
626 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_MINIMAL]);
627 gcc_assert (tree_contains_struct[IMPORTED_DECL][TS_DECL_COMMON]);
628 gcc_assert (tree_contains_struct[NAMELIST_DECL][TS_DECL_MINIMAL]);
629 gcc_assert (tree_contains_struct[NAMELIST_DECL][TS_DECL_COMMON]);
633 /* Init tree.c. */
635 void
636 init_ttree (void)
638 /* Initialize the hash table of types. */
639 type_hash_table
640 = hash_table<type_cache_hasher>::create_ggc (TYPE_HASH_INITIAL_SIZE);
642 debug_expr_for_decl
643 = hash_table<tree_decl_map_cache_hasher>::create_ggc (512);
645 value_expr_for_decl
646 = hash_table<tree_decl_map_cache_hasher>::create_ggc (512);
648 int_cst_hash_table = hash_table<int_cst_hasher>::create_ggc (1024);
650 int_cst_node = make_int_cst (1, 1);
652 cl_option_hash_table = hash_table<cl_option_hasher>::create_ggc (64);
654 cl_optimization_node = make_node (OPTIMIZATION_NODE);
655 cl_target_option_node = make_node (TARGET_OPTION_NODE);
657 /* Initialize the tree_contains_struct array. */
658 initialize_tree_contains_struct ();
659 lang_hooks.init_ts ();
663 /* The name of the object as the assembler will see it (but before any
664 translations made by ASM_OUTPUT_LABELREF). Often this is the same
665 as DECL_NAME. It is an IDENTIFIER_NODE. */
666 tree
667 decl_assembler_name (tree decl)
669 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
670 lang_hooks.set_decl_assembler_name (decl);
671 return DECL_WITH_VIS_CHECK (decl)->decl_with_vis.assembler_name;
674 /* When the target supports COMDAT groups, this indicates which group the
675 DECL is associated with. This can be either an IDENTIFIER_NODE or a
676 decl, in which case its DECL_ASSEMBLER_NAME identifies the group. */
677 tree
678 decl_comdat_group (const_tree node)
680 struct symtab_node *snode = symtab_node::get (node);
681 if (!snode)
682 return NULL;
683 return snode->get_comdat_group ();
686 /* Likewise, but make sure it's been reduced to an IDENTIFIER_NODE. */
687 tree
688 decl_comdat_group_id (const_tree node)
690 struct symtab_node *snode = symtab_node::get (node);
691 if (!snode)
692 return NULL;
693 return snode->get_comdat_group_id ();
696 /* When the target supports named section, return its name as IDENTIFIER_NODE
697 or NULL if it is in no section. */
698 const char *
699 decl_section_name (const_tree node)
701 struct symtab_node *snode = symtab_node::get (node);
702 if (!snode)
703 return NULL;
704 return snode->get_section ();
707 /* Set section name of NODE to VALUE (that is expected to be
708 identifier node) */
709 void
710 set_decl_section_name (tree node, const char *value)
712 struct symtab_node *snode;
714 if (value == NULL)
716 snode = symtab_node::get (node);
717 if (!snode)
718 return;
720 else if (TREE_CODE (node) == VAR_DECL)
721 snode = varpool_node::get_create (node);
722 else
723 snode = cgraph_node::get_create (node);
724 snode->set_section (value);
727 /* Return TLS model of a variable NODE. */
728 enum tls_model
729 decl_tls_model (const_tree node)
731 struct varpool_node *snode = varpool_node::get (node);
732 if (!snode)
733 return TLS_MODEL_NONE;
734 return snode->tls_model;
737 /* Set TLS model of variable NODE to MODEL. */
738 void
739 set_decl_tls_model (tree node, enum tls_model model)
741 struct varpool_node *vnode;
743 if (model == TLS_MODEL_NONE)
745 vnode = varpool_node::get (node);
746 if (!vnode)
747 return;
749 else
750 vnode = varpool_node::get_create (node);
751 vnode->tls_model = model;
754 /* Compute the number of bytes occupied by a tree with code CODE.
755 This function cannot be used for nodes that have variable sizes,
756 including TREE_VEC, INTEGER_CST, STRING_CST, and CALL_EXPR. */
757 size_t
758 tree_code_size (enum tree_code code)
760 switch (TREE_CODE_CLASS (code))
762 case tcc_declaration: /* A decl node */
764 switch (code)
766 case FIELD_DECL:
767 return sizeof (struct tree_field_decl);
768 case PARM_DECL:
769 return sizeof (struct tree_parm_decl);
770 case VAR_DECL:
771 return sizeof (struct tree_var_decl);
772 case LABEL_DECL:
773 return sizeof (struct tree_label_decl);
774 case RESULT_DECL:
775 return sizeof (struct tree_result_decl);
776 case CONST_DECL:
777 return sizeof (struct tree_const_decl);
778 case TYPE_DECL:
779 return sizeof (struct tree_type_decl);
780 case FUNCTION_DECL:
781 return sizeof (struct tree_function_decl);
782 case DEBUG_EXPR_DECL:
783 return sizeof (struct tree_decl_with_rtl);
784 case TRANSLATION_UNIT_DECL:
785 return sizeof (struct tree_translation_unit_decl);
786 case NAMESPACE_DECL:
787 case IMPORTED_DECL:
788 case NAMELIST_DECL:
789 return sizeof (struct tree_decl_non_common);
790 default:
791 return lang_hooks.tree_size (code);
795 case tcc_type: /* a type node */
796 return sizeof (struct tree_type_non_common);
798 case tcc_reference: /* a reference */
799 case tcc_expression: /* an expression */
800 case tcc_statement: /* an expression with side effects */
801 case tcc_comparison: /* a comparison expression */
802 case tcc_unary: /* a unary arithmetic expression */
803 case tcc_binary: /* a binary arithmetic expression */
804 return (sizeof (struct tree_exp)
805 + (TREE_CODE_LENGTH (code) - 1) * sizeof (tree));
807 case tcc_constant: /* a constant */
808 switch (code)
810 case VOID_CST: return sizeof (struct tree_typed);
811 case INTEGER_CST: gcc_unreachable ();
812 case REAL_CST: return sizeof (struct tree_real_cst);
813 case FIXED_CST: return sizeof (struct tree_fixed_cst);
814 case COMPLEX_CST: return sizeof (struct tree_complex);
815 case VECTOR_CST: return sizeof (struct tree_vector);
816 case STRING_CST: gcc_unreachable ();
817 default:
818 return lang_hooks.tree_size (code);
821 case tcc_exceptional: /* something random, like an identifier. */
822 switch (code)
824 case IDENTIFIER_NODE: return lang_hooks.identifier_size;
825 case TREE_LIST: return sizeof (struct tree_list);
827 case ERROR_MARK:
828 case PLACEHOLDER_EXPR: return sizeof (struct tree_common);
830 case TREE_VEC:
831 case OMP_CLAUSE: gcc_unreachable ();
833 case SSA_NAME: return sizeof (struct tree_ssa_name);
835 case STATEMENT_LIST: return sizeof (struct tree_statement_list);
836 case BLOCK: return sizeof (struct tree_block);
837 case CONSTRUCTOR: return sizeof (struct tree_constructor);
838 case OPTIMIZATION_NODE: return sizeof (struct tree_optimization_option);
839 case TARGET_OPTION_NODE: return sizeof (struct tree_target_option);
841 default:
842 return lang_hooks.tree_size (code);
845 default:
846 gcc_unreachable ();
850 /* Compute the number of bytes occupied by NODE. This routine only
851 looks at TREE_CODE, except for those nodes that have variable sizes. */
852 size_t
853 tree_size (const_tree node)
855 const enum tree_code code = TREE_CODE (node);
856 switch (code)
858 case INTEGER_CST:
859 return (sizeof (struct tree_int_cst)
860 + (TREE_INT_CST_EXT_NUNITS (node) - 1) * sizeof (HOST_WIDE_INT));
862 case TREE_BINFO:
863 return (offsetof (struct tree_binfo, base_binfos)
864 + vec<tree, va_gc>
865 ::embedded_size (BINFO_N_BASE_BINFOS (node)));
867 case TREE_VEC:
868 return (sizeof (struct tree_vec)
869 + (TREE_VEC_LENGTH (node) - 1) * sizeof (tree));
871 case VECTOR_CST:
872 return (sizeof (struct tree_vector)
873 + (TYPE_VECTOR_SUBPARTS (TREE_TYPE (node)) - 1) * sizeof (tree));
875 case STRING_CST:
876 return TREE_STRING_LENGTH (node) + offsetof (struct tree_string, str) + 1;
878 case OMP_CLAUSE:
879 return (sizeof (struct tree_omp_clause)
880 + (omp_clause_num_ops[OMP_CLAUSE_CODE (node)] - 1)
881 * sizeof (tree));
883 default:
884 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
885 return (sizeof (struct tree_exp)
886 + (VL_EXP_OPERAND_LENGTH (node) - 1) * sizeof (tree));
887 else
888 return tree_code_size (code);
892 /* Record interesting allocation statistics for a tree node with CODE
893 and LENGTH. */
895 static void
896 record_node_allocation_statistics (enum tree_code code ATTRIBUTE_UNUSED,
897 size_t length ATTRIBUTE_UNUSED)
899 enum tree_code_class type = TREE_CODE_CLASS (code);
900 tree_node_kind kind;
902 if (!GATHER_STATISTICS)
903 return;
905 switch (type)
907 case tcc_declaration: /* A decl node */
908 kind = d_kind;
909 break;
911 case tcc_type: /* a type node */
912 kind = t_kind;
913 break;
915 case tcc_statement: /* an expression with side effects */
916 kind = s_kind;
917 break;
919 case tcc_reference: /* a reference */
920 kind = r_kind;
921 break;
923 case tcc_expression: /* an expression */
924 case tcc_comparison: /* a comparison expression */
925 case tcc_unary: /* a unary arithmetic expression */
926 case tcc_binary: /* a binary arithmetic expression */
927 kind = e_kind;
928 break;
930 case tcc_constant: /* a constant */
931 kind = c_kind;
932 break;
934 case tcc_exceptional: /* something random, like an identifier. */
935 switch (code)
937 case IDENTIFIER_NODE:
938 kind = id_kind;
939 break;
941 case TREE_VEC:
942 kind = vec_kind;
943 break;
945 case TREE_BINFO:
946 kind = binfo_kind;
947 break;
949 case SSA_NAME:
950 kind = ssa_name_kind;
951 break;
953 case BLOCK:
954 kind = b_kind;
955 break;
957 case CONSTRUCTOR:
958 kind = constr_kind;
959 break;
961 case OMP_CLAUSE:
962 kind = omp_clause_kind;
963 break;
965 default:
966 kind = x_kind;
967 break;
969 break;
971 case tcc_vl_exp:
972 kind = e_kind;
973 break;
975 default:
976 gcc_unreachable ();
979 tree_code_counts[(int) code]++;
980 tree_node_counts[(int) kind]++;
981 tree_node_sizes[(int) kind] += length;
984 /* Allocate and return a new UID from the DECL_UID namespace. */
987 allocate_decl_uid (void)
989 return next_decl_uid++;
992 /* Return a newly allocated node of code CODE. For decl and type
993 nodes, some other fields are initialized. The rest of the node is
994 initialized to zero. This function cannot be used for TREE_VEC,
995 INTEGER_CST or OMP_CLAUSE nodes, which is enforced by asserts in
996 tree_code_size.
998 Achoo! I got a code in the node. */
1000 tree
1001 make_node_stat (enum tree_code code MEM_STAT_DECL)
1003 tree t;
1004 enum tree_code_class type = TREE_CODE_CLASS (code);
1005 size_t length = tree_code_size (code);
1007 record_node_allocation_statistics (code, length);
1009 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1010 TREE_SET_CODE (t, code);
1012 switch (type)
1014 case tcc_statement:
1015 TREE_SIDE_EFFECTS (t) = 1;
1016 break;
1018 case tcc_declaration:
1019 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
1021 if (code == FUNCTION_DECL)
1023 DECL_ALIGN (t) = FUNCTION_BOUNDARY;
1024 DECL_MODE (t) = FUNCTION_MODE;
1026 else
1027 DECL_ALIGN (t) = 1;
1029 DECL_SOURCE_LOCATION (t) = input_location;
1030 if (TREE_CODE (t) == DEBUG_EXPR_DECL)
1031 DECL_UID (t) = --next_debug_decl_uid;
1032 else
1034 DECL_UID (t) = allocate_decl_uid ();
1035 SET_DECL_PT_UID (t, -1);
1037 if (TREE_CODE (t) == LABEL_DECL)
1038 LABEL_DECL_UID (t) = -1;
1040 break;
1042 case tcc_type:
1043 TYPE_UID (t) = next_type_uid++;
1044 TYPE_ALIGN (t) = BITS_PER_UNIT;
1045 TYPE_USER_ALIGN (t) = 0;
1046 TYPE_MAIN_VARIANT (t) = t;
1047 TYPE_CANONICAL (t) = t;
1049 /* Default to no attributes for type, but let target change that. */
1050 TYPE_ATTRIBUTES (t) = NULL_TREE;
1051 targetm.set_default_type_attributes (t);
1053 /* We have not yet computed the alias set for this type. */
1054 TYPE_ALIAS_SET (t) = -1;
1055 break;
1057 case tcc_constant:
1058 TREE_CONSTANT (t) = 1;
1059 break;
1061 case tcc_expression:
1062 switch (code)
1064 case INIT_EXPR:
1065 case MODIFY_EXPR:
1066 case VA_ARG_EXPR:
1067 case PREDECREMENT_EXPR:
1068 case PREINCREMENT_EXPR:
1069 case POSTDECREMENT_EXPR:
1070 case POSTINCREMENT_EXPR:
1071 /* All of these have side-effects, no matter what their
1072 operands are. */
1073 TREE_SIDE_EFFECTS (t) = 1;
1074 break;
1076 default:
1077 break;
1079 break;
1081 case tcc_exceptional:
1082 switch (code)
1084 case TARGET_OPTION_NODE:
1085 TREE_TARGET_OPTION(t)
1086 = ggc_cleared_alloc<struct cl_target_option> ();
1087 break;
1089 case OPTIMIZATION_NODE:
1090 TREE_OPTIMIZATION (t)
1091 = ggc_cleared_alloc<struct cl_optimization> ();
1092 break;
1094 default:
1095 break;
1097 break;
1099 default:
1100 /* Other classes need no special treatment. */
1101 break;
1104 return t;
1107 /* Return a new node with the same contents as NODE except that its
1108 TREE_CHAIN, if it has one, is zero and it has a fresh uid. */
1110 tree
1111 copy_node_stat (tree node MEM_STAT_DECL)
1113 tree t;
1114 enum tree_code code = TREE_CODE (node);
1115 size_t length;
1117 gcc_assert (code != STATEMENT_LIST);
1119 length = tree_size (node);
1120 record_node_allocation_statistics (code, length);
1121 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
1122 memcpy (t, node, length);
1124 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
1125 TREE_CHAIN (t) = 0;
1126 TREE_ASM_WRITTEN (t) = 0;
1127 TREE_VISITED (t) = 0;
1129 if (TREE_CODE_CLASS (code) == tcc_declaration)
1131 if (code == DEBUG_EXPR_DECL)
1132 DECL_UID (t) = --next_debug_decl_uid;
1133 else
1135 DECL_UID (t) = allocate_decl_uid ();
1136 if (DECL_PT_UID_SET_P (node))
1137 SET_DECL_PT_UID (t, DECL_PT_UID (node));
1139 if ((TREE_CODE (node) == PARM_DECL || TREE_CODE (node) == VAR_DECL)
1140 && DECL_HAS_VALUE_EXPR_P (node))
1142 SET_DECL_VALUE_EXPR (t, DECL_VALUE_EXPR (node));
1143 DECL_HAS_VALUE_EXPR_P (t) = 1;
1145 /* DECL_DEBUG_EXPR is copied explicitely by callers. */
1146 if (TREE_CODE (node) == VAR_DECL)
1148 DECL_HAS_DEBUG_EXPR_P (t) = 0;
1149 t->decl_with_vis.symtab_node = NULL;
1151 if (TREE_CODE (node) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (node))
1153 SET_DECL_INIT_PRIORITY (t, DECL_INIT_PRIORITY (node));
1154 DECL_HAS_INIT_PRIORITY_P (t) = 1;
1156 if (TREE_CODE (node) == FUNCTION_DECL)
1158 DECL_STRUCT_FUNCTION (t) = NULL;
1159 t->decl_with_vis.symtab_node = NULL;
1162 else if (TREE_CODE_CLASS (code) == tcc_type)
1164 TYPE_UID (t) = next_type_uid++;
1165 /* The following is so that the debug code for
1166 the copy is different from the original type.
1167 The two statements usually duplicate each other
1168 (because they clear fields of the same union),
1169 but the optimizer should catch that. */
1170 TYPE_SYMTAB_POINTER (t) = 0;
1171 TYPE_SYMTAB_ADDRESS (t) = 0;
1173 /* Do not copy the values cache. */
1174 if (TYPE_CACHED_VALUES_P (t))
1176 TYPE_CACHED_VALUES_P (t) = 0;
1177 TYPE_CACHED_VALUES (t) = NULL_TREE;
1180 else if (code == TARGET_OPTION_NODE)
1182 TREE_TARGET_OPTION (t) = ggc_alloc<struct cl_target_option>();
1183 memcpy (TREE_TARGET_OPTION (t), TREE_TARGET_OPTION (node),
1184 sizeof (struct cl_target_option));
1186 else if (code == OPTIMIZATION_NODE)
1188 TREE_OPTIMIZATION (t) = ggc_alloc<struct cl_optimization>();
1189 memcpy (TREE_OPTIMIZATION (t), TREE_OPTIMIZATION (node),
1190 sizeof (struct cl_optimization));
1193 return t;
1196 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
1197 For example, this can copy a list made of TREE_LIST nodes. */
1199 tree
1200 copy_list (tree list)
1202 tree head;
1203 tree prev, next;
1205 if (list == 0)
1206 return 0;
1208 head = prev = copy_node (list);
1209 next = TREE_CHAIN (list);
1210 while (next)
1212 TREE_CHAIN (prev) = copy_node (next);
1213 prev = TREE_CHAIN (prev);
1214 next = TREE_CHAIN (next);
1216 return head;
1220 /* Return the value that TREE_INT_CST_EXT_NUNITS should have for an
1221 INTEGER_CST with value CST and type TYPE. */
1223 static unsigned int
1224 get_int_cst_ext_nunits (tree type, const wide_int &cst)
1226 gcc_checking_assert (cst.get_precision () == TYPE_PRECISION (type));
1227 /* We need an extra zero HWI if CST is an unsigned integer with its
1228 upper bit set, and if CST occupies a whole number of HWIs. */
1229 if (TYPE_UNSIGNED (type)
1230 && wi::neg_p (cst)
1231 && (cst.get_precision () % HOST_BITS_PER_WIDE_INT) == 0)
1232 return cst.get_precision () / HOST_BITS_PER_WIDE_INT + 1;
1233 return cst.get_len ();
1236 /* Return a new INTEGER_CST with value CST and type TYPE. */
1238 static tree
1239 build_new_int_cst (tree type, const wide_int &cst)
1241 unsigned int len = cst.get_len ();
1242 unsigned int ext_len = get_int_cst_ext_nunits (type, cst);
1243 tree nt = make_int_cst (len, ext_len);
1245 if (len < ext_len)
1247 --ext_len;
1248 TREE_INT_CST_ELT (nt, ext_len) = 0;
1249 for (unsigned int i = len; i < ext_len; ++i)
1250 TREE_INT_CST_ELT (nt, i) = -1;
1252 else if (TYPE_UNSIGNED (type)
1253 && cst.get_precision () < len * HOST_BITS_PER_WIDE_INT)
1255 len--;
1256 TREE_INT_CST_ELT (nt, len)
1257 = zext_hwi (cst.elt (len),
1258 cst.get_precision () % HOST_BITS_PER_WIDE_INT);
1261 for (unsigned int i = 0; i < len; i++)
1262 TREE_INT_CST_ELT (nt, i) = cst.elt (i);
1263 TREE_TYPE (nt) = type;
1264 return nt;
1267 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1269 tree
1270 build_int_cst (tree type, HOST_WIDE_INT low)
1272 /* Support legacy code. */
1273 if (!type)
1274 type = integer_type_node;
1276 return wide_int_to_tree (type, wi::shwi (low, TYPE_PRECISION (type)));
1279 tree
1280 build_int_cstu (tree type, unsigned HOST_WIDE_INT cst)
1282 return wide_int_to_tree (type, wi::uhwi (cst, TYPE_PRECISION (type)));
1285 /* Create an INT_CST node with a LOW value sign extended to TYPE. */
1287 tree
1288 build_int_cst_type (tree type, HOST_WIDE_INT low)
1290 gcc_assert (type);
1291 return wide_int_to_tree (type, wi::shwi (low, TYPE_PRECISION (type)));
1294 /* Constructs tree in type TYPE from with value given by CST. Signedness
1295 of CST is assumed to be the same as the signedness of TYPE. */
1297 tree
1298 double_int_to_tree (tree type, double_int cst)
1300 return wide_int_to_tree (type, widest_int::from (cst, TYPE_SIGN (type)));
1303 /* We force the wide_int CST to the range of the type TYPE by sign or
1304 zero extending it. OVERFLOWABLE indicates if we are interested in
1305 overflow of the value, when >0 we are only interested in signed
1306 overflow, for <0 we are interested in any overflow. OVERFLOWED
1307 indicates whether overflow has already occurred. CONST_OVERFLOWED
1308 indicates whether constant overflow has already occurred. We force
1309 T's value to be within range of T's type (by setting to 0 or 1 all
1310 the bits outside the type's range). We set TREE_OVERFLOWED if,
1311 OVERFLOWED is nonzero,
1312 or OVERFLOWABLE is >0 and signed overflow occurs
1313 or OVERFLOWABLE is <0 and any overflow occurs
1314 We return a new tree node for the extended wide_int. The node
1315 is shared if no overflow flags are set. */
1318 tree
1319 force_fit_type (tree type, const wide_int_ref &cst,
1320 int overflowable, bool overflowed)
1322 signop sign = TYPE_SIGN (type);
1324 /* If we need to set overflow flags, return a new unshared node. */
1325 if (overflowed || !wi::fits_to_tree_p (cst, type))
1327 if (overflowed
1328 || overflowable < 0
1329 || (overflowable > 0 && sign == SIGNED))
1331 wide_int tmp = wide_int::from (cst, TYPE_PRECISION (type), sign);
1332 tree t = build_new_int_cst (type, tmp);
1333 TREE_OVERFLOW (t) = 1;
1334 return t;
1338 /* Else build a shared node. */
1339 return wide_int_to_tree (type, cst);
1342 /* These are the hash table functions for the hash table of INTEGER_CST
1343 nodes of a sizetype. */
1345 /* Return the hash code X, an INTEGER_CST. */
1347 hashval_t
1348 int_cst_hasher::hash (tree x)
1350 const_tree const t = x;
1351 hashval_t code = TYPE_UID (TREE_TYPE (t));
1352 int i;
1354 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
1355 code = iterative_hash_host_wide_int (TREE_INT_CST_ELT(t, i), code);
1357 return code;
1360 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
1361 is the same as that given by *Y, which is the same. */
1363 bool
1364 int_cst_hasher::equal (tree x, tree y)
1366 const_tree const xt = x;
1367 const_tree const yt = y;
1369 if (TREE_TYPE (xt) != TREE_TYPE (yt)
1370 || TREE_INT_CST_NUNITS (xt) != TREE_INT_CST_NUNITS (yt)
1371 || TREE_INT_CST_EXT_NUNITS (xt) != TREE_INT_CST_EXT_NUNITS (yt))
1372 return false;
1374 for (int i = 0; i < TREE_INT_CST_NUNITS (xt); i++)
1375 if (TREE_INT_CST_ELT (xt, i) != TREE_INT_CST_ELT (yt, i))
1376 return false;
1378 return true;
1381 /* Create an INT_CST node of TYPE and value CST.
1382 The returned node is always shared. For small integers we use a
1383 per-type vector cache, for larger ones we use a single hash table.
1384 The value is extended from its precision according to the sign of
1385 the type to be a multiple of HOST_BITS_PER_WIDE_INT. This defines
1386 the upper bits and ensures that hashing and value equality based
1387 upon the underlying HOST_WIDE_INTs works without masking. */
1389 tree
1390 wide_int_to_tree (tree type, const wide_int_ref &pcst)
1392 tree t;
1393 int ix = -1;
1394 int limit = 0;
1396 gcc_assert (type);
1397 unsigned int prec = TYPE_PRECISION (type);
1398 signop sgn = TYPE_SIGN (type);
1400 /* Verify that everything is canonical. */
1401 int l = pcst.get_len ();
1402 if (l > 1)
1404 if (pcst.elt (l - 1) == 0)
1405 gcc_checking_assert (pcst.elt (l - 2) < 0);
1406 if (pcst.elt (l - 1) == (HOST_WIDE_INT) -1)
1407 gcc_checking_assert (pcst.elt (l - 2) >= 0);
1410 wide_int cst = wide_int::from (pcst, prec, sgn);
1411 unsigned int ext_len = get_int_cst_ext_nunits (type, cst);
1413 if (ext_len == 1)
1415 /* We just need to store a single HOST_WIDE_INT. */
1416 HOST_WIDE_INT hwi;
1417 if (TYPE_UNSIGNED (type))
1418 hwi = cst.to_uhwi ();
1419 else
1420 hwi = cst.to_shwi ();
1422 switch (TREE_CODE (type))
1424 case NULLPTR_TYPE:
1425 gcc_assert (hwi == 0);
1426 /* Fallthru. */
1428 case POINTER_TYPE:
1429 case REFERENCE_TYPE:
1430 case POINTER_BOUNDS_TYPE:
1431 /* Cache NULL pointer and zero bounds. */
1432 if (hwi == 0)
1434 limit = 1;
1435 ix = 0;
1437 break;
1439 case BOOLEAN_TYPE:
1440 /* Cache false or true. */
1441 limit = 2;
1442 if (hwi < 2)
1443 ix = hwi;
1444 break;
1446 case INTEGER_TYPE:
1447 case OFFSET_TYPE:
1448 if (TYPE_SIGN (type) == UNSIGNED)
1450 /* Cache [0, N). */
1451 limit = INTEGER_SHARE_LIMIT;
1452 if (IN_RANGE (hwi, 0, INTEGER_SHARE_LIMIT - 1))
1453 ix = hwi;
1455 else
1457 /* Cache [-1, N). */
1458 limit = INTEGER_SHARE_LIMIT + 1;
1459 if (IN_RANGE (hwi, -1, INTEGER_SHARE_LIMIT - 1))
1460 ix = hwi + 1;
1462 break;
1464 case ENUMERAL_TYPE:
1465 break;
1467 default:
1468 gcc_unreachable ();
1471 if (ix >= 0)
1473 /* Look for it in the type's vector of small shared ints. */
1474 if (!TYPE_CACHED_VALUES_P (type))
1476 TYPE_CACHED_VALUES_P (type) = 1;
1477 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1480 t = TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix);
1481 if (t)
1482 /* Make sure no one is clobbering the shared constant. */
1483 gcc_checking_assert (TREE_TYPE (t) == type
1484 && TREE_INT_CST_NUNITS (t) == 1
1485 && TREE_INT_CST_OFFSET_NUNITS (t) == 1
1486 && TREE_INT_CST_EXT_NUNITS (t) == 1
1487 && TREE_INT_CST_ELT (t, 0) == hwi);
1488 else
1490 /* Create a new shared int. */
1491 t = build_new_int_cst (type, cst);
1492 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1495 else
1497 /* Use the cache of larger shared ints, using int_cst_node as
1498 a temporary. */
1500 TREE_INT_CST_ELT (int_cst_node, 0) = hwi;
1501 TREE_TYPE (int_cst_node) = type;
1503 tree *slot = int_cst_hash_table->find_slot (int_cst_node, INSERT);
1504 t = *slot;
1505 if (!t)
1507 /* Insert this one into the hash table. */
1508 t = int_cst_node;
1509 *slot = t;
1510 /* Make a new node for next time round. */
1511 int_cst_node = make_int_cst (1, 1);
1515 else
1517 /* The value either hashes properly or we drop it on the floor
1518 for the gc to take care of. There will not be enough of them
1519 to worry about. */
1521 tree nt = build_new_int_cst (type, cst);
1522 tree *slot = int_cst_hash_table->find_slot (nt, INSERT);
1523 t = *slot;
1524 if (!t)
1526 /* Insert this one into the hash table. */
1527 t = nt;
1528 *slot = t;
1532 return t;
1535 void
1536 cache_integer_cst (tree t)
1538 tree type = TREE_TYPE (t);
1539 int ix = -1;
1540 int limit = 0;
1541 int prec = TYPE_PRECISION (type);
1543 gcc_assert (!TREE_OVERFLOW (t));
1545 switch (TREE_CODE (type))
1547 case NULLPTR_TYPE:
1548 gcc_assert (integer_zerop (t));
1549 /* Fallthru. */
1551 case POINTER_TYPE:
1552 case REFERENCE_TYPE:
1553 /* Cache NULL pointer. */
1554 if (integer_zerop (t))
1556 limit = 1;
1557 ix = 0;
1559 break;
1561 case BOOLEAN_TYPE:
1562 /* Cache false or true. */
1563 limit = 2;
1564 if (wi::ltu_p (t, 2))
1565 ix = TREE_INT_CST_ELT (t, 0);
1566 break;
1568 case INTEGER_TYPE:
1569 case OFFSET_TYPE:
1570 if (TYPE_UNSIGNED (type))
1572 /* Cache 0..N */
1573 limit = INTEGER_SHARE_LIMIT;
1575 /* This is a little hokie, but if the prec is smaller than
1576 what is necessary to hold INTEGER_SHARE_LIMIT, then the
1577 obvious test will not get the correct answer. */
1578 if (prec < HOST_BITS_PER_WIDE_INT)
1580 if (tree_to_uhwi (t) < (unsigned HOST_WIDE_INT) INTEGER_SHARE_LIMIT)
1581 ix = tree_to_uhwi (t);
1583 else if (wi::ltu_p (t, INTEGER_SHARE_LIMIT))
1584 ix = tree_to_uhwi (t);
1586 else
1588 /* Cache -1..N */
1589 limit = INTEGER_SHARE_LIMIT + 1;
1591 if (integer_minus_onep (t))
1592 ix = 0;
1593 else if (!wi::neg_p (t))
1595 if (prec < HOST_BITS_PER_WIDE_INT)
1597 if (tree_to_shwi (t) < INTEGER_SHARE_LIMIT)
1598 ix = tree_to_shwi (t) + 1;
1600 else if (wi::ltu_p (t, INTEGER_SHARE_LIMIT))
1601 ix = tree_to_shwi (t) + 1;
1604 break;
1606 case ENUMERAL_TYPE:
1607 break;
1609 default:
1610 gcc_unreachable ();
1613 if (ix >= 0)
1615 /* Look for it in the type's vector of small shared ints. */
1616 if (!TYPE_CACHED_VALUES_P (type))
1618 TYPE_CACHED_VALUES_P (type) = 1;
1619 TYPE_CACHED_VALUES (type) = make_tree_vec (limit);
1622 gcc_assert (TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) == NULL_TREE);
1623 TREE_VEC_ELT (TYPE_CACHED_VALUES (type), ix) = t;
1625 else
1627 /* Use the cache of larger shared ints. */
1628 tree *slot = int_cst_hash_table->find_slot (t, INSERT);
1629 /* If there is already an entry for the number verify it's the
1630 same. */
1631 if (*slot)
1632 gcc_assert (wi::eq_p (tree (*slot), t));
1633 else
1634 /* Otherwise insert this one into the hash table. */
1635 *slot = t;
1640 /* Builds an integer constant in TYPE such that lowest BITS bits are ones
1641 and the rest are zeros. */
1643 tree
1644 build_low_bits_mask (tree type, unsigned bits)
1646 gcc_assert (bits <= TYPE_PRECISION (type));
1648 return wide_int_to_tree (type, wi::mask (bits, false,
1649 TYPE_PRECISION (type)));
1652 /* Checks that X is integer constant that can be expressed in (unsigned)
1653 HOST_WIDE_INT without loss of precision. */
1655 bool
1656 cst_and_fits_in_hwi (const_tree x)
1658 if (TREE_CODE (x) != INTEGER_CST)
1659 return false;
1661 if (TYPE_PRECISION (TREE_TYPE (x)) > HOST_BITS_PER_WIDE_INT)
1662 return false;
1664 return TREE_INT_CST_NUNITS (x) == 1;
1667 /* Build a newly constructed VECTOR_CST node of length LEN. */
1669 tree
1670 make_vector_stat (unsigned len MEM_STAT_DECL)
1672 tree t;
1673 unsigned length = (len - 1) * sizeof (tree) + sizeof (struct tree_vector);
1675 record_node_allocation_statistics (VECTOR_CST, length);
1677 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
1679 TREE_SET_CODE (t, VECTOR_CST);
1680 TREE_CONSTANT (t) = 1;
1682 return t;
1685 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1686 are in a list pointed to by VALS. */
1688 tree
1689 build_vector_stat (tree type, tree *vals MEM_STAT_DECL)
1691 int over = 0;
1692 unsigned cnt = 0;
1693 tree v = make_vector (TYPE_VECTOR_SUBPARTS (type));
1694 TREE_TYPE (v) = type;
1696 /* Iterate through elements and check for overflow. */
1697 for (cnt = 0; cnt < TYPE_VECTOR_SUBPARTS (type); ++cnt)
1699 tree value = vals[cnt];
1701 VECTOR_CST_ELT (v, cnt) = value;
1703 /* Don't crash if we get an address constant. */
1704 if (!CONSTANT_CLASS_P (value))
1705 continue;
1707 over |= TREE_OVERFLOW (value);
1710 TREE_OVERFLOW (v) = over;
1711 return v;
1714 /* Return a new VECTOR_CST node whose type is TYPE and whose values
1715 are extracted from V, a vector of CONSTRUCTOR_ELT. */
1717 tree
1718 build_vector_from_ctor (tree type, vec<constructor_elt, va_gc> *v)
1720 tree *vec = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type));
1721 unsigned HOST_WIDE_INT idx, pos = 0;
1722 tree value;
1724 FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
1726 if (TREE_CODE (value) == VECTOR_CST)
1727 for (unsigned i = 0; i < VECTOR_CST_NELTS (value); ++i)
1728 vec[pos++] = VECTOR_CST_ELT (value, i);
1729 else
1730 vec[pos++] = value;
1732 for (; idx < TYPE_VECTOR_SUBPARTS (type); ++idx)
1733 vec[pos++] = build_zero_cst (TREE_TYPE (type));
1735 return build_vector (type, vec);
1738 /* Build a vector of type VECTYPE where all the elements are SCs. */
1739 tree
1740 build_vector_from_val (tree vectype, tree sc)
1742 int i, nunits = TYPE_VECTOR_SUBPARTS (vectype);
1744 if (sc == error_mark_node)
1745 return sc;
1747 /* Verify that the vector type is suitable for SC. Note that there
1748 is some inconsistency in the type-system with respect to restrict
1749 qualifications of pointers. Vector types always have a main-variant
1750 element type and the qualification is applied to the vector-type.
1751 So TREE_TYPE (vector-type) does not return a properly qualified
1752 vector element-type. */
1753 gcc_checking_assert (types_compatible_p (TYPE_MAIN_VARIANT (TREE_TYPE (sc)),
1754 TREE_TYPE (vectype)));
1756 if (CONSTANT_CLASS_P (sc))
1758 tree *v = XALLOCAVEC (tree, nunits);
1759 for (i = 0; i < nunits; ++i)
1760 v[i] = sc;
1761 return build_vector (vectype, v);
1763 else
1765 vec<constructor_elt, va_gc> *v;
1766 vec_alloc (v, nunits);
1767 for (i = 0; i < nunits; ++i)
1768 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, sc);
1769 return build_constructor (vectype, v);
1773 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1774 are in the vec pointed to by VALS. */
1775 tree
1776 build_constructor (tree type, vec<constructor_elt, va_gc> *vals)
1778 tree c = make_node (CONSTRUCTOR);
1779 unsigned int i;
1780 constructor_elt *elt;
1781 bool constant_p = true;
1782 bool side_effects_p = false;
1784 TREE_TYPE (c) = type;
1785 CONSTRUCTOR_ELTS (c) = vals;
1787 FOR_EACH_VEC_SAFE_ELT (vals, i, elt)
1789 /* Mostly ctors will have elts that don't have side-effects, so
1790 the usual case is to scan all the elements. Hence a single
1791 loop for both const and side effects, rather than one loop
1792 each (with early outs). */
1793 if (!TREE_CONSTANT (elt->value))
1794 constant_p = false;
1795 if (TREE_SIDE_EFFECTS (elt->value))
1796 side_effects_p = true;
1799 TREE_SIDE_EFFECTS (c) = side_effects_p;
1800 TREE_CONSTANT (c) = constant_p;
1802 return c;
1805 /* Build a CONSTRUCTOR node made of a single initializer, with the specified
1806 INDEX and VALUE. */
1807 tree
1808 build_constructor_single (tree type, tree index, tree value)
1810 vec<constructor_elt, va_gc> *v;
1811 constructor_elt elt = {index, value};
1813 vec_alloc (v, 1);
1814 v->quick_push (elt);
1816 return build_constructor (type, v);
1820 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
1821 are in a list pointed to by VALS. */
1822 tree
1823 build_constructor_from_list (tree type, tree vals)
1825 tree t;
1826 vec<constructor_elt, va_gc> *v = NULL;
1828 if (vals)
1830 vec_alloc (v, list_length (vals));
1831 for (t = vals; t; t = TREE_CHAIN (t))
1832 CONSTRUCTOR_APPEND_ELT (v, TREE_PURPOSE (t), TREE_VALUE (t));
1835 return build_constructor (type, v);
1838 /* Return a new CONSTRUCTOR node whose type is TYPE. NELTS is the number
1839 of elements, provided as index/value pairs. */
1841 tree
1842 build_constructor_va (tree type, int nelts, ...)
1844 vec<constructor_elt, va_gc> *v = NULL;
1845 va_list p;
1847 va_start (p, nelts);
1848 vec_alloc (v, nelts);
1849 while (nelts--)
1851 tree index = va_arg (p, tree);
1852 tree value = va_arg (p, tree);
1853 CONSTRUCTOR_APPEND_ELT (v, index, value);
1855 va_end (p);
1856 return build_constructor (type, v);
1859 /* Return a new FIXED_CST node whose type is TYPE and value is F. */
1861 tree
1862 build_fixed (tree type, FIXED_VALUE_TYPE f)
1864 tree v;
1865 FIXED_VALUE_TYPE *fp;
1867 v = make_node (FIXED_CST);
1868 fp = ggc_alloc<fixed_value> ();
1869 memcpy (fp, &f, sizeof (FIXED_VALUE_TYPE));
1871 TREE_TYPE (v) = type;
1872 TREE_FIXED_CST_PTR (v) = fp;
1873 return v;
1876 /* Return a new REAL_CST node whose type is TYPE and value is D. */
1878 tree
1879 build_real (tree type, REAL_VALUE_TYPE d)
1881 tree v;
1882 REAL_VALUE_TYPE *dp;
1883 int overflow = 0;
1885 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
1886 Consider doing it via real_convert now. */
1888 v = make_node (REAL_CST);
1889 dp = ggc_alloc<real_value> ();
1890 memcpy (dp, &d, sizeof (REAL_VALUE_TYPE));
1892 TREE_TYPE (v) = type;
1893 TREE_REAL_CST_PTR (v) = dp;
1894 TREE_OVERFLOW (v) = overflow;
1895 return v;
1898 /* Like build_real, but first truncate D to the type. */
1900 tree
1901 build_real_truncate (tree type, REAL_VALUE_TYPE d)
1903 return build_real (type, real_value_truncate (TYPE_MODE (type), d));
1906 /* Return a new REAL_CST node whose type is TYPE
1907 and whose value is the integer value of the INTEGER_CST node I. */
1909 REAL_VALUE_TYPE
1910 real_value_from_int_cst (const_tree type, const_tree i)
1912 REAL_VALUE_TYPE d;
1914 /* Clear all bits of the real value type so that we can later do
1915 bitwise comparisons to see if two values are the same. */
1916 memset (&d, 0, sizeof d);
1918 real_from_integer (&d, type ? TYPE_MODE (type) : VOIDmode, i,
1919 TYPE_SIGN (TREE_TYPE (i)));
1920 return d;
1923 /* Given a tree representing an integer constant I, return a tree
1924 representing the same value as a floating-point constant of type TYPE. */
1926 tree
1927 build_real_from_int_cst (tree type, const_tree i)
1929 tree v;
1930 int overflow = TREE_OVERFLOW (i);
1932 v = build_real (type, real_value_from_int_cst (type, i));
1934 TREE_OVERFLOW (v) |= overflow;
1935 return v;
1938 /* Return a newly constructed STRING_CST node whose value is
1939 the LEN characters at STR.
1940 Note that for a C string literal, LEN should include the trailing NUL.
1941 The TREE_TYPE is not initialized. */
1943 tree
1944 build_string (int len, const char *str)
1946 tree s;
1947 size_t length;
1949 /* Do not waste bytes provided by padding of struct tree_string. */
1950 length = len + offsetof (struct tree_string, str) + 1;
1952 record_node_allocation_statistics (STRING_CST, length);
1954 s = (tree) ggc_internal_alloc (length);
1956 memset (s, 0, sizeof (struct tree_typed));
1957 TREE_SET_CODE (s, STRING_CST);
1958 TREE_CONSTANT (s) = 1;
1959 TREE_STRING_LENGTH (s) = len;
1960 memcpy (s->string.str, str, len);
1961 s->string.str[len] = '\0';
1963 return s;
1966 /* Return a newly constructed COMPLEX_CST node whose value is
1967 specified by the real and imaginary parts REAL and IMAG.
1968 Both REAL and IMAG should be constant nodes. TYPE, if specified,
1969 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
1971 tree
1972 build_complex (tree type, tree real, tree imag)
1974 tree t = make_node (COMPLEX_CST);
1976 TREE_REALPART (t) = real;
1977 TREE_IMAGPART (t) = imag;
1978 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
1979 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
1980 return t;
1983 /* Build a complex (inf +- 0i), such as for the result of cproj.
1984 TYPE is the complex tree type of the result. If NEG is true, the
1985 imaginary zero is negative. */
1987 tree
1988 build_complex_inf (tree type, bool neg)
1990 REAL_VALUE_TYPE rinf, rzero = dconst0;
1992 real_inf (&rinf);
1993 rzero.sign = neg;
1994 return build_complex (type, build_real (TREE_TYPE (type), rinf),
1995 build_real (TREE_TYPE (type), rzero));
1998 /* Return the constant 1 in type TYPE. If TYPE has several elements, each
1999 element is set to 1. In particular, this is 1 + i for complex types. */
2001 tree
2002 build_each_one_cst (tree type)
2004 if (TREE_CODE (type) == COMPLEX_TYPE)
2006 tree scalar = build_one_cst (TREE_TYPE (type));
2007 return build_complex (type, scalar, scalar);
2009 else
2010 return build_one_cst (type);
2013 /* Return a constant of arithmetic type TYPE which is the
2014 multiplicative identity of the set TYPE. */
2016 tree
2017 build_one_cst (tree type)
2019 switch (TREE_CODE (type))
2021 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2022 case POINTER_TYPE: case REFERENCE_TYPE:
2023 case OFFSET_TYPE:
2024 return build_int_cst (type, 1);
2026 case REAL_TYPE:
2027 return build_real (type, dconst1);
2029 case FIXED_POINT_TYPE:
2030 /* We can only generate 1 for accum types. */
2031 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
2032 return build_fixed (type, FCONST1 (TYPE_MODE (type)));
2034 case VECTOR_TYPE:
2036 tree scalar = build_one_cst (TREE_TYPE (type));
2038 return build_vector_from_val (type, scalar);
2041 case COMPLEX_TYPE:
2042 return build_complex (type,
2043 build_one_cst (TREE_TYPE (type)),
2044 build_zero_cst (TREE_TYPE (type)));
2046 default:
2047 gcc_unreachable ();
2051 /* Return an integer of type TYPE containing all 1's in as much precision as
2052 it contains, or a complex or vector whose subparts are such integers. */
2054 tree
2055 build_all_ones_cst (tree type)
2057 if (TREE_CODE (type) == COMPLEX_TYPE)
2059 tree scalar = build_all_ones_cst (TREE_TYPE (type));
2060 return build_complex (type, scalar, scalar);
2062 else
2063 return build_minus_one_cst (type);
2066 /* Return a constant of arithmetic type TYPE which is the
2067 opposite of the multiplicative identity of the set TYPE. */
2069 tree
2070 build_minus_one_cst (tree type)
2072 switch (TREE_CODE (type))
2074 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2075 case POINTER_TYPE: case REFERENCE_TYPE:
2076 case OFFSET_TYPE:
2077 return build_int_cst (type, -1);
2079 case REAL_TYPE:
2080 return build_real (type, dconstm1);
2082 case FIXED_POINT_TYPE:
2083 /* We can only generate 1 for accum types. */
2084 gcc_assert (ALL_SCALAR_ACCUM_MODE_P (TYPE_MODE (type)));
2085 return build_fixed (type, fixed_from_double_int (double_int_minus_one,
2086 TYPE_MODE (type)));
2088 case VECTOR_TYPE:
2090 tree scalar = build_minus_one_cst (TREE_TYPE (type));
2092 return build_vector_from_val (type, scalar);
2095 case COMPLEX_TYPE:
2096 return build_complex (type,
2097 build_minus_one_cst (TREE_TYPE (type)),
2098 build_zero_cst (TREE_TYPE (type)));
2100 default:
2101 gcc_unreachable ();
2105 /* Build 0 constant of type TYPE. This is used by constructor folding
2106 and thus the constant should be represented in memory by
2107 zero(es). */
2109 tree
2110 build_zero_cst (tree type)
2112 switch (TREE_CODE (type))
2114 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2115 case POINTER_TYPE: case REFERENCE_TYPE:
2116 case OFFSET_TYPE: case NULLPTR_TYPE:
2117 return build_int_cst (type, 0);
2119 case REAL_TYPE:
2120 return build_real (type, dconst0);
2122 case FIXED_POINT_TYPE:
2123 return build_fixed (type, FCONST0 (TYPE_MODE (type)));
2125 case VECTOR_TYPE:
2127 tree scalar = build_zero_cst (TREE_TYPE (type));
2129 return build_vector_from_val (type, scalar);
2132 case COMPLEX_TYPE:
2134 tree zero = build_zero_cst (TREE_TYPE (type));
2136 return build_complex (type, zero, zero);
2139 default:
2140 if (!AGGREGATE_TYPE_P (type))
2141 return fold_convert (type, integer_zero_node);
2142 return build_constructor (type, NULL);
2147 /* Build a BINFO with LEN language slots. */
2149 tree
2150 make_tree_binfo_stat (unsigned base_binfos MEM_STAT_DECL)
2152 tree t;
2153 size_t length = (offsetof (struct tree_binfo, base_binfos)
2154 + vec<tree, va_gc>::embedded_size (base_binfos));
2156 record_node_allocation_statistics (TREE_BINFO, length);
2158 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
2160 memset (t, 0, offsetof (struct tree_binfo, base_binfos));
2162 TREE_SET_CODE (t, TREE_BINFO);
2164 BINFO_BASE_BINFOS (t)->embedded_init (base_binfos);
2166 return t;
2169 /* Create a CASE_LABEL_EXPR tree node and return it. */
2171 tree
2172 build_case_label (tree low_value, tree high_value, tree label_decl)
2174 tree t = make_node (CASE_LABEL_EXPR);
2176 TREE_TYPE (t) = void_type_node;
2177 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (label_decl));
2179 CASE_LOW (t) = low_value;
2180 CASE_HIGH (t) = high_value;
2181 CASE_LABEL (t) = label_decl;
2182 CASE_CHAIN (t) = NULL_TREE;
2184 return t;
2187 /* Build a newly constructed INTEGER_CST node. LEN and EXT_LEN are the
2188 values of TREE_INT_CST_NUNITS and TREE_INT_CST_EXT_NUNITS respectively.
2189 The latter determines the length of the HOST_WIDE_INT vector. */
2191 tree
2192 make_int_cst_stat (int len, int ext_len MEM_STAT_DECL)
2194 tree t;
2195 int length = ((ext_len - 1) * sizeof (HOST_WIDE_INT)
2196 + sizeof (struct tree_int_cst));
2198 gcc_assert (len);
2199 record_node_allocation_statistics (INTEGER_CST, length);
2201 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2203 TREE_SET_CODE (t, INTEGER_CST);
2204 TREE_INT_CST_NUNITS (t) = len;
2205 TREE_INT_CST_EXT_NUNITS (t) = ext_len;
2206 /* to_offset can only be applied to trees that are offset_int-sized
2207 or smaller. EXT_LEN is correct if it fits, otherwise the constant
2208 must be exactly the precision of offset_int and so LEN is correct. */
2209 if (ext_len <= OFFSET_INT_ELTS)
2210 TREE_INT_CST_OFFSET_NUNITS (t) = ext_len;
2211 else
2212 TREE_INT_CST_OFFSET_NUNITS (t) = len;
2214 TREE_CONSTANT (t) = 1;
2216 return t;
2219 /* Build a newly constructed TREE_VEC node of length LEN. */
2221 tree
2222 make_tree_vec_stat (int len MEM_STAT_DECL)
2224 tree t;
2225 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
2227 record_node_allocation_statistics (TREE_VEC, length);
2229 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2231 TREE_SET_CODE (t, TREE_VEC);
2232 TREE_VEC_LENGTH (t) = len;
2234 return t;
2237 /* Grow a TREE_VEC node to new length LEN. */
2239 tree
2240 grow_tree_vec_stat (tree v, int len MEM_STAT_DECL)
2242 gcc_assert (TREE_CODE (v) == TREE_VEC);
2244 int oldlen = TREE_VEC_LENGTH (v);
2245 gcc_assert (len > oldlen);
2247 int oldlength = (oldlen - 1) * sizeof (tree) + sizeof (struct tree_vec);
2248 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
2250 record_node_allocation_statistics (TREE_VEC, length - oldlength);
2252 v = (tree) ggc_realloc (v, length PASS_MEM_STAT);
2254 TREE_VEC_LENGTH (v) = len;
2256 return v;
2259 /* Return 1 if EXPR is the constant zero, whether it is integral, float or
2260 fixed, and scalar, complex or vector. */
2263 zerop (const_tree expr)
2265 return (integer_zerop (expr)
2266 || real_zerop (expr)
2267 || fixed_zerop (expr));
2270 /* Return 1 if EXPR is the integer constant zero or a complex constant
2271 of zero. */
2274 integer_zerop (const_tree expr)
2276 STRIP_NOPS (expr);
2278 switch (TREE_CODE (expr))
2280 case INTEGER_CST:
2281 return wi::eq_p (expr, 0);
2282 case COMPLEX_CST:
2283 return (integer_zerop (TREE_REALPART (expr))
2284 && integer_zerop (TREE_IMAGPART (expr)));
2285 case VECTOR_CST:
2287 unsigned i;
2288 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2289 if (!integer_zerop (VECTOR_CST_ELT (expr, i)))
2290 return false;
2291 return true;
2293 default:
2294 return false;
2298 /* Return 1 if EXPR is the integer constant one or the corresponding
2299 complex constant. */
2302 integer_onep (const_tree expr)
2304 STRIP_NOPS (expr);
2306 switch (TREE_CODE (expr))
2308 case INTEGER_CST:
2309 return wi::eq_p (wi::to_widest (expr), 1);
2310 case COMPLEX_CST:
2311 return (integer_onep (TREE_REALPART (expr))
2312 && integer_zerop (TREE_IMAGPART (expr)));
2313 case VECTOR_CST:
2315 unsigned i;
2316 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2317 if (!integer_onep (VECTOR_CST_ELT (expr, i)))
2318 return false;
2319 return true;
2321 default:
2322 return false;
2326 /* Return 1 if EXPR is the integer constant one. For complex and vector,
2327 return 1 if every piece is the integer constant one. */
2330 integer_each_onep (const_tree expr)
2332 STRIP_NOPS (expr);
2334 if (TREE_CODE (expr) == COMPLEX_CST)
2335 return (integer_onep (TREE_REALPART (expr))
2336 && integer_onep (TREE_IMAGPART (expr)));
2337 else
2338 return integer_onep (expr);
2341 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
2342 it contains, or a complex or vector whose subparts are such integers. */
2345 integer_all_onesp (const_tree expr)
2347 STRIP_NOPS (expr);
2349 if (TREE_CODE (expr) == COMPLEX_CST
2350 && integer_all_onesp (TREE_REALPART (expr))
2351 && integer_all_onesp (TREE_IMAGPART (expr)))
2352 return 1;
2354 else if (TREE_CODE (expr) == VECTOR_CST)
2356 unsigned i;
2357 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2358 if (!integer_all_onesp (VECTOR_CST_ELT (expr, i)))
2359 return 0;
2360 return 1;
2363 else if (TREE_CODE (expr) != INTEGER_CST)
2364 return 0;
2366 return wi::max_value (TYPE_PRECISION (TREE_TYPE (expr)), UNSIGNED) == expr;
2369 /* Return 1 if EXPR is the integer constant minus one. */
2372 integer_minus_onep (const_tree expr)
2374 STRIP_NOPS (expr);
2376 if (TREE_CODE (expr) == COMPLEX_CST)
2377 return (integer_all_onesp (TREE_REALPART (expr))
2378 && integer_zerop (TREE_IMAGPART (expr)));
2379 else
2380 return integer_all_onesp (expr);
2383 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
2384 one bit on). */
2387 integer_pow2p (const_tree expr)
2389 STRIP_NOPS (expr);
2391 if (TREE_CODE (expr) == COMPLEX_CST
2392 && integer_pow2p (TREE_REALPART (expr))
2393 && integer_zerop (TREE_IMAGPART (expr)))
2394 return 1;
2396 if (TREE_CODE (expr) != INTEGER_CST)
2397 return 0;
2399 return wi::popcount (expr) == 1;
2402 /* Return 1 if EXPR is an integer constant other than zero or a
2403 complex constant other than zero. */
2406 integer_nonzerop (const_tree expr)
2408 STRIP_NOPS (expr);
2410 return ((TREE_CODE (expr) == INTEGER_CST
2411 && !wi::eq_p (expr, 0))
2412 || (TREE_CODE (expr) == COMPLEX_CST
2413 && (integer_nonzerop (TREE_REALPART (expr))
2414 || integer_nonzerop (TREE_IMAGPART (expr)))));
2417 /* Return 1 if EXPR is the integer constant one. For vector,
2418 return 1 if every piece is the integer constant minus one
2419 (representing the value TRUE). */
2422 integer_truep (const_tree expr)
2424 STRIP_NOPS (expr);
2426 if (TREE_CODE (expr) == VECTOR_CST)
2427 return integer_all_onesp (expr);
2428 return integer_onep (expr);
2431 /* Return 1 if EXPR is the fixed-point constant zero. */
2434 fixed_zerop (const_tree expr)
2436 return (TREE_CODE (expr) == FIXED_CST
2437 && TREE_FIXED_CST (expr).data.is_zero ());
2440 /* Return the power of two represented by a tree node known to be a
2441 power of two. */
2444 tree_log2 (const_tree expr)
2446 STRIP_NOPS (expr);
2448 if (TREE_CODE (expr) == COMPLEX_CST)
2449 return tree_log2 (TREE_REALPART (expr));
2451 return wi::exact_log2 (expr);
2454 /* Similar, but return the largest integer Y such that 2 ** Y is less
2455 than or equal to EXPR. */
2458 tree_floor_log2 (const_tree expr)
2460 STRIP_NOPS (expr);
2462 if (TREE_CODE (expr) == COMPLEX_CST)
2463 return tree_log2 (TREE_REALPART (expr));
2465 return wi::floor_log2 (expr);
2468 /* Return number of known trailing zero bits in EXPR, or, if the value of
2469 EXPR is known to be zero, the precision of it's type. */
2471 unsigned int
2472 tree_ctz (const_tree expr)
2474 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
2475 && !POINTER_TYPE_P (TREE_TYPE (expr)))
2476 return 0;
2478 unsigned int ret1, ret2, prec = TYPE_PRECISION (TREE_TYPE (expr));
2479 switch (TREE_CODE (expr))
2481 case INTEGER_CST:
2482 ret1 = wi::ctz (expr);
2483 return MIN (ret1, prec);
2484 case SSA_NAME:
2485 ret1 = wi::ctz (get_nonzero_bits (expr));
2486 return MIN (ret1, prec);
2487 case PLUS_EXPR:
2488 case MINUS_EXPR:
2489 case BIT_IOR_EXPR:
2490 case BIT_XOR_EXPR:
2491 case MIN_EXPR:
2492 case MAX_EXPR:
2493 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2494 if (ret1 == 0)
2495 return ret1;
2496 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2497 return MIN (ret1, ret2);
2498 case POINTER_PLUS_EXPR:
2499 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2500 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2501 /* Second operand is sizetype, which could be in theory
2502 wider than pointer's precision. Make sure we never
2503 return more than prec. */
2504 ret2 = MIN (ret2, prec);
2505 return MIN (ret1, ret2);
2506 case BIT_AND_EXPR:
2507 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2508 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2509 return MAX (ret1, ret2);
2510 case MULT_EXPR:
2511 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2512 ret2 = tree_ctz (TREE_OPERAND (expr, 1));
2513 return MIN (ret1 + ret2, prec);
2514 case LSHIFT_EXPR:
2515 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2516 if (tree_fits_uhwi_p (TREE_OPERAND (expr, 1))
2517 && (tree_to_uhwi (TREE_OPERAND (expr, 1)) < prec))
2519 ret2 = tree_to_uhwi (TREE_OPERAND (expr, 1));
2520 return MIN (ret1 + ret2, prec);
2522 return ret1;
2523 case RSHIFT_EXPR:
2524 if (tree_fits_uhwi_p (TREE_OPERAND (expr, 1))
2525 && (tree_to_uhwi (TREE_OPERAND (expr, 1)) < prec))
2527 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2528 ret2 = tree_to_uhwi (TREE_OPERAND (expr, 1));
2529 if (ret1 > ret2)
2530 return ret1 - ret2;
2532 return 0;
2533 case TRUNC_DIV_EXPR:
2534 case CEIL_DIV_EXPR:
2535 case FLOOR_DIV_EXPR:
2536 case ROUND_DIV_EXPR:
2537 case EXACT_DIV_EXPR:
2538 if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
2539 && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) == 1)
2541 int l = tree_log2 (TREE_OPERAND (expr, 1));
2542 if (l >= 0)
2544 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2545 ret2 = l;
2546 if (ret1 > ret2)
2547 return ret1 - ret2;
2550 return 0;
2551 CASE_CONVERT:
2552 ret1 = tree_ctz (TREE_OPERAND (expr, 0));
2553 if (ret1 && ret1 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2554 ret1 = prec;
2555 return MIN (ret1, prec);
2556 case SAVE_EXPR:
2557 return tree_ctz (TREE_OPERAND (expr, 0));
2558 case COND_EXPR:
2559 ret1 = tree_ctz (TREE_OPERAND (expr, 1));
2560 if (ret1 == 0)
2561 return 0;
2562 ret2 = tree_ctz (TREE_OPERAND (expr, 2));
2563 return MIN (ret1, ret2);
2564 case COMPOUND_EXPR:
2565 return tree_ctz (TREE_OPERAND (expr, 1));
2566 case ADDR_EXPR:
2567 ret1 = get_pointer_alignment (CONST_CAST_TREE (expr));
2568 if (ret1 > BITS_PER_UNIT)
2570 ret1 = ctz_hwi (ret1 / BITS_PER_UNIT);
2571 return MIN (ret1, prec);
2573 return 0;
2574 default:
2575 return 0;
2579 /* Return 1 if EXPR is the real constant zero. Trailing zeroes matter for
2580 decimal float constants, so don't return 1 for them. */
2583 real_zerop (const_tree expr)
2585 STRIP_NOPS (expr);
2587 switch (TREE_CODE (expr))
2589 case REAL_CST:
2590 return real_equal (&TREE_REAL_CST (expr), &dconst0)
2591 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2592 case COMPLEX_CST:
2593 return real_zerop (TREE_REALPART (expr))
2594 && real_zerop (TREE_IMAGPART (expr));
2595 case VECTOR_CST:
2597 unsigned i;
2598 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2599 if (!real_zerop (VECTOR_CST_ELT (expr, i)))
2600 return false;
2601 return true;
2603 default:
2604 return false;
2608 /* Return 1 if EXPR is the real constant one in real or complex form.
2609 Trailing zeroes matter for decimal float constants, so don't return
2610 1 for them. */
2613 real_onep (const_tree expr)
2615 STRIP_NOPS (expr);
2617 switch (TREE_CODE (expr))
2619 case REAL_CST:
2620 return real_equal (&TREE_REAL_CST (expr), &dconst1)
2621 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2622 case COMPLEX_CST:
2623 return real_onep (TREE_REALPART (expr))
2624 && real_zerop (TREE_IMAGPART (expr));
2625 case VECTOR_CST:
2627 unsigned i;
2628 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2629 if (!real_onep (VECTOR_CST_ELT (expr, i)))
2630 return false;
2631 return true;
2633 default:
2634 return false;
2638 /* Return 1 if EXPR is the real constant minus one. Trailing zeroes
2639 matter for decimal float constants, so don't return 1 for them. */
2642 real_minus_onep (const_tree expr)
2644 STRIP_NOPS (expr);
2646 switch (TREE_CODE (expr))
2648 case REAL_CST:
2649 return real_equal (&TREE_REAL_CST (expr), &dconstm1)
2650 && !(DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (expr))));
2651 case COMPLEX_CST:
2652 return real_minus_onep (TREE_REALPART (expr))
2653 && real_zerop (TREE_IMAGPART (expr));
2654 case VECTOR_CST:
2656 unsigned i;
2657 for (i = 0; i < VECTOR_CST_NELTS (expr); ++i)
2658 if (!real_minus_onep (VECTOR_CST_ELT (expr, i)))
2659 return false;
2660 return true;
2662 default:
2663 return false;
2667 /* Nonzero if EXP is a constant or a cast of a constant. */
2670 really_constant_p (const_tree exp)
2672 /* This is not quite the same as STRIP_NOPS. It does more. */
2673 while (CONVERT_EXPR_P (exp)
2674 || TREE_CODE (exp) == NON_LVALUE_EXPR)
2675 exp = TREE_OPERAND (exp, 0);
2676 return TREE_CONSTANT (exp);
2679 /* Return first list element whose TREE_VALUE is ELEM.
2680 Return 0 if ELEM is not in LIST. */
2682 tree
2683 value_member (tree elem, tree list)
2685 while (list)
2687 if (elem == TREE_VALUE (list))
2688 return list;
2689 list = TREE_CHAIN (list);
2691 return NULL_TREE;
2694 /* Return first list element whose TREE_PURPOSE is ELEM.
2695 Return 0 if ELEM is not in LIST. */
2697 tree
2698 purpose_member (const_tree elem, tree list)
2700 while (list)
2702 if (elem == TREE_PURPOSE (list))
2703 return list;
2704 list = TREE_CHAIN (list);
2706 return NULL_TREE;
2709 /* Return true if ELEM is in V. */
2711 bool
2712 vec_member (const_tree elem, vec<tree, va_gc> *v)
2714 unsigned ix;
2715 tree t;
2716 FOR_EACH_VEC_SAFE_ELT (v, ix, t)
2717 if (elem == t)
2718 return true;
2719 return false;
2722 /* Returns element number IDX (zero-origin) of chain CHAIN, or
2723 NULL_TREE. */
2725 tree
2726 chain_index (int idx, tree chain)
2728 for (; chain && idx > 0; --idx)
2729 chain = TREE_CHAIN (chain);
2730 return chain;
2733 /* Return nonzero if ELEM is part of the chain CHAIN. */
2736 chain_member (const_tree elem, const_tree chain)
2738 while (chain)
2740 if (elem == chain)
2741 return 1;
2742 chain = DECL_CHAIN (chain);
2745 return 0;
2748 /* Return the length of a chain of nodes chained through TREE_CHAIN.
2749 We expect a null pointer to mark the end of the chain.
2750 This is the Lisp primitive `length'. */
2753 list_length (const_tree t)
2755 const_tree p = t;
2756 #ifdef ENABLE_TREE_CHECKING
2757 const_tree q = t;
2758 #endif
2759 int len = 0;
2761 while (p)
2763 p = TREE_CHAIN (p);
2764 #ifdef ENABLE_TREE_CHECKING
2765 if (len % 2)
2766 q = TREE_CHAIN (q);
2767 gcc_assert (p != q);
2768 #endif
2769 len++;
2772 return len;
2775 /* Returns the first FIELD_DECL in the TYPE_FIELDS of the RECORD_TYPE or
2776 UNION_TYPE TYPE, or NULL_TREE if none. */
2778 tree
2779 first_field (const_tree type)
2781 tree t = TYPE_FIELDS (type);
2782 while (t && TREE_CODE (t) != FIELD_DECL)
2783 t = TREE_CHAIN (t);
2784 return t;
2787 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
2788 by modifying the last node in chain 1 to point to chain 2.
2789 This is the Lisp primitive `nconc'. */
2791 tree
2792 chainon (tree op1, tree op2)
2794 tree t1;
2796 if (!op1)
2797 return op2;
2798 if (!op2)
2799 return op1;
2801 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
2802 continue;
2803 TREE_CHAIN (t1) = op2;
2805 #ifdef ENABLE_TREE_CHECKING
2807 tree t2;
2808 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
2809 gcc_assert (t2 != t1);
2811 #endif
2813 return op1;
2816 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
2818 tree
2819 tree_last (tree chain)
2821 tree next;
2822 if (chain)
2823 while ((next = TREE_CHAIN (chain)))
2824 chain = next;
2825 return chain;
2828 /* Reverse the order of elements in the chain T,
2829 and return the new head of the chain (old last element). */
2831 tree
2832 nreverse (tree t)
2834 tree prev = 0, decl, next;
2835 for (decl = t; decl; decl = next)
2837 /* We shouldn't be using this function to reverse BLOCK chains; we
2838 have blocks_nreverse for that. */
2839 gcc_checking_assert (TREE_CODE (decl) != BLOCK);
2840 next = TREE_CHAIN (decl);
2841 TREE_CHAIN (decl) = prev;
2842 prev = decl;
2844 return prev;
2847 /* Return a newly created TREE_LIST node whose
2848 purpose and value fields are PARM and VALUE. */
2850 tree
2851 build_tree_list_stat (tree parm, tree value MEM_STAT_DECL)
2853 tree t = make_node_stat (TREE_LIST PASS_MEM_STAT);
2854 TREE_PURPOSE (t) = parm;
2855 TREE_VALUE (t) = value;
2856 return t;
2859 /* Build a chain of TREE_LIST nodes from a vector. */
2861 tree
2862 build_tree_list_vec_stat (const vec<tree, va_gc> *vec MEM_STAT_DECL)
2864 tree ret = NULL_TREE;
2865 tree *pp = &ret;
2866 unsigned int i;
2867 tree t;
2868 FOR_EACH_VEC_SAFE_ELT (vec, i, t)
2870 *pp = build_tree_list_stat (NULL, t PASS_MEM_STAT);
2871 pp = &TREE_CHAIN (*pp);
2873 return ret;
2876 /* Return a newly created TREE_LIST node whose
2877 purpose and value fields are PURPOSE and VALUE
2878 and whose TREE_CHAIN is CHAIN. */
2880 tree
2881 tree_cons_stat (tree purpose, tree value, tree chain MEM_STAT_DECL)
2883 tree node;
2885 node = ggc_alloc_tree_node_stat (sizeof (struct tree_list) PASS_MEM_STAT);
2886 memset (node, 0, sizeof (struct tree_common));
2888 record_node_allocation_statistics (TREE_LIST, sizeof (struct tree_list));
2890 TREE_SET_CODE (node, TREE_LIST);
2891 TREE_CHAIN (node) = chain;
2892 TREE_PURPOSE (node) = purpose;
2893 TREE_VALUE (node) = value;
2894 return node;
2897 /* Return the values of the elements of a CONSTRUCTOR as a vector of
2898 trees. */
2900 vec<tree, va_gc> *
2901 ctor_to_vec (tree ctor)
2903 vec<tree, va_gc> *vec;
2904 vec_alloc (vec, CONSTRUCTOR_NELTS (ctor));
2905 unsigned int ix;
2906 tree val;
2908 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), ix, val)
2909 vec->quick_push (val);
2911 return vec;
2914 /* Return the size nominally occupied by an object of type TYPE
2915 when it resides in memory. The value is measured in units of bytes,
2916 and its data type is that normally used for type sizes
2917 (which is the first type created by make_signed_type or
2918 make_unsigned_type). */
2920 tree
2921 size_in_bytes (const_tree type)
2923 tree t;
2925 if (type == error_mark_node)
2926 return integer_zero_node;
2928 type = TYPE_MAIN_VARIANT (type);
2929 t = TYPE_SIZE_UNIT (type);
2931 if (t == 0)
2933 lang_hooks.types.incomplete_type_error (NULL_TREE, type);
2934 return size_zero_node;
2937 return t;
2940 /* Return the size of TYPE (in bytes) as a wide integer
2941 or return -1 if the size can vary or is larger than an integer. */
2943 HOST_WIDE_INT
2944 int_size_in_bytes (const_tree type)
2946 tree t;
2948 if (type == error_mark_node)
2949 return 0;
2951 type = TYPE_MAIN_VARIANT (type);
2952 t = TYPE_SIZE_UNIT (type);
2954 if (t && tree_fits_uhwi_p (t))
2955 return TREE_INT_CST_LOW (t);
2956 else
2957 return -1;
2960 /* Return the maximum size of TYPE (in bytes) as a wide integer
2961 or return -1 if the size can vary or is larger than an integer. */
2963 HOST_WIDE_INT
2964 max_int_size_in_bytes (const_tree type)
2966 HOST_WIDE_INT size = -1;
2967 tree size_tree;
2969 /* If this is an array type, check for a possible MAX_SIZE attached. */
2971 if (TREE_CODE (type) == ARRAY_TYPE)
2973 size_tree = TYPE_ARRAY_MAX_SIZE (type);
2975 if (size_tree && tree_fits_uhwi_p (size_tree))
2976 size = tree_to_uhwi (size_tree);
2979 /* If we still haven't been able to get a size, see if the language
2980 can compute a maximum size. */
2982 if (size == -1)
2984 size_tree = lang_hooks.types.max_size (type);
2986 if (size_tree && tree_fits_uhwi_p (size_tree))
2987 size = tree_to_uhwi (size_tree);
2990 return size;
2993 /* Return the bit position of FIELD, in bits from the start of the record.
2994 This is a tree of type bitsizetype. */
2996 tree
2997 bit_position (const_tree field)
2999 return bit_from_pos (DECL_FIELD_OFFSET (field),
3000 DECL_FIELD_BIT_OFFSET (field));
3003 /* Return the byte position of FIELD, in bytes from the start of the record.
3004 This is a tree of type sizetype. */
3006 tree
3007 byte_position (const_tree field)
3009 return byte_from_pos (DECL_FIELD_OFFSET (field),
3010 DECL_FIELD_BIT_OFFSET (field));
3013 /* Likewise, but return as an integer. It must be representable in
3014 that way (since it could be a signed value, we don't have the
3015 option of returning -1 like int_size_in_byte can. */
3017 HOST_WIDE_INT
3018 int_byte_position (const_tree field)
3020 return tree_to_shwi (byte_position (field));
3023 /* Return the strictest alignment, in bits, that T is known to have. */
3025 unsigned int
3026 expr_align (const_tree t)
3028 unsigned int align0, align1;
3030 switch (TREE_CODE (t))
3032 CASE_CONVERT: case NON_LVALUE_EXPR:
3033 /* If we have conversions, we know that the alignment of the
3034 object must meet each of the alignments of the types. */
3035 align0 = expr_align (TREE_OPERAND (t, 0));
3036 align1 = TYPE_ALIGN (TREE_TYPE (t));
3037 return MAX (align0, align1);
3039 case SAVE_EXPR: case COMPOUND_EXPR: case MODIFY_EXPR:
3040 case INIT_EXPR: case TARGET_EXPR: case WITH_CLEANUP_EXPR:
3041 case CLEANUP_POINT_EXPR:
3042 /* These don't change the alignment of an object. */
3043 return expr_align (TREE_OPERAND (t, 0));
3045 case COND_EXPR:
3046 /* The best we can do is say that the alignment is the least aligned
3047 of the two arms. */
3048 align0 = expr_align (TREE_OPERAND (t, 1));
3049 align1 = expr_align (TREE_OPERAND (t, 2));
3050 return MIN (align0, align1);
3052 /* FIXME: LABEL_DECL and CONST_DECL never have DECL_ALIGN set
3053 meaningfully, it's always 1. */
3054 case LABEL_DECL: case CONST_DECL:
3055 case VAR_DECL: case PARM_DECL: case RESULT_DECL:
3056 case FUNCTION_DECL:
3057 gcc_assert (DECL_ALIGN (t) != 0);
3058 return DECL_ALIGN (t);
3060 default:
3061 break;
3064 /* Otherwise take the alignment from that of the type. */
3065 return TYPE_ALIGN (TREE_TYPE (t));
3068 /* Return, as a tree node, the number of elements for TYPE (which is an
3069 ARRAY_TYPE) minus one. This counts only elements of the top array. */
3071 tree
3072 array_type_nelts (const_tree type)
3074 tree index_type, min, max;
3076 /* If they did it with unspecified bounds, then we should have already
3077 given an error about it before we got here. */
3078 if (! TYPE_DOMAIN (type))
3079 return error_mark_node;
3081 index_type = TYPE_DOMAIN (type);
3082 min = TYPE_MIN_VALUE (index_type);
3083 max = TYPE_MAX_VALUE (index_type);
3085 /* TYPE_MAX_VALUE may not be set if the array has unknown length. */
3086 if (!max)
3087 return error_mark_node;
3089 return (integer_zerop (min)
3090 ? max
3091 : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min));
3094 /* If arg is static -- a reference to an object in static storage -- then
3095 return the object. This is not the same as the C meaning of `static'.
3096 If arg isn't static, return NULL. */
3098 tree
3099 staticp (tree arg)
3101 switch (TREE_CODE (arg))
3103 case FUNCTION_DECL:
3104 /* Nested functions are static, even though taking their address will
3105 involve a trampoline as we unnest the nested function and create
3106 the trampoline on the tree level. */
3107 return arg;
3109 case VAR_DECL:
3110 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
3111 && ! DECL_THREAD_LOCAL_P (arg)
3112 && ! DECL_DLLIMPORT_P (arg)
3113 ? arg : NULL);
3115 case CONST_DECL:
3116 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
3117 ? arg : NULL);
3119 case CONSTRUCTOR:
3120 return TREE_STATIC (arg) ? arg : NULL;
3122 case LABEL_DECL:
3123 case STRING_CST:
3124 return arg;
3126 case COMPONENT_REF:
3127 /* If the thing being referenced is not a field, then it is
3128 something language specific. */
3129 gcc_assert (TREE_CODE (TREE_OPERAND (arg, 1)) == FIELD_DECL);
3131 /* If we are referencing a bitfield, we can't evaluate an
3132 ADDR_EXPR at compile time and so it isn't a constant. */
3133 if (DECL_BIT_FIELD (TREE_OPERAND (arg, 1)))
3134 return NULL;
3136 return staticp (TREE_OPERAND (arg, 0));
3138 case BIT_FIELD_REF:
3139 return NULL;
3141 case INDIRECT_REF:
3142 return TREE_CONSTANT (TREE_OPERAND (arg, 0)) ? arg : NULL;
3144 case ARRAY_REF:
3145 case ARRAY_RANGE_REF:
3146 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
3147 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
3148 return staticp (TREE_OPERAND (arg, 0));
3149 else
3150 return NULL;
3152 case COMPOUND_LITERAL_EXPR:
3153 return TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (arg)) ? arg : NULL;
3155 default:
3156 return NULL;
3163 /* Return whether OP is a DECL whose address is function-invariant. */
3165 bool
3166 decl_address_invariant_p (const_tree op)
3168 /* The conditions below are slightly less strict than the one in
3169 staticp. */
3171 switch (TREE_CODE (op))
3173 case PARM_DECL:
3174 case RESULT_DECL:
3175 case LABEL_DECL:
3176 case FUNCTION_DECL:
3177 return true;
3179 case VAR_DECL:
3180 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
3181 || DECL_THREAD_LOCAL_P (op)
3182 || DECL_CONTEXT (op) == current_function_decl
3183 || decl_function_context (op) == current_function_decl)
3184 return true;
3185 break;
3187 case CONST_DECL:
3188 if ((TREE_STATIC (op) || DECL_EXTERNAL (op))
3189 || decl_function_context (op) == current_function_decl)
3190 return true;
3191 break;
3193 default:
3194 break;
3197 return false;
3200 /* Return whether OP is a DECL whose address is interprocedural-invariant. */
3202 bool
3203 decl_address_ip_invariant_p (const_tree op)
3205 /* The conditions below are slightly less strict than the one in
3206 staticp. */
3208 switch (TREE_CODE (op))
3210 case LABEL_DECL:
3211 case FUNCTION_DECL:
3212 case STRING_CST:
3213 return true;
3215 case VAR_DECL:
3216 if (((TREE_STATIC (op) || DECL_EXTERNAL (op))
3217 && !DECL_DLLIMPORT_P (op))
3218 || DECL_THREAD_LOCAL_P (op))
3219 return true;
3220 break;
3222 case CONST_DECL:
3223 if ((TREE_STATIC (op) || DECL_EXTERNAL (op)))
3224 return true;
3225 break;
3227 default:
3228 break;
3231 return false;
3235 /* Return true if T is function-invariant (internal function, does
3236 not handle arithmetic; that's handled in skip_simple_arithmetic and
3237 tree_invariant_p). */
3239 static bool tree_invariant_p (tree t);
3241 static bool
3242 tree_invariant_p_1 (tree t)
3244 tree op;
3246 if (TREE_CONSTANT (t)
3247 || (TREE_READONLY (t) && !TREE_SIDE_EFFECTS (t)))
3248 return true;
3250 switch (TREE_CODE (t))
3252 case SAVE_EXPR:
3253 return true;
3255 case ADDR_EXPR:
3256 op = TREE_OPERAND (t, 0);
3257 while (handled_component_p (op))
3259 switch (TREE_CODE (op))
3261 case ARRAY_REF:
3262 case ARRAY_RANGE_REF:
3263 if (!tree_invariant_p (TREE_OPERAND (op, 1))
3264 || TREE_OPERAND (op, 2) != NULL_TREE
3265 || TREE_OPERAND (op, 3) != NULL_TREE)
3266 return false;
3267 break;
3269 case COMPONENT_REF:
3270 if (TREE_OPERAND (op, 2) != NULL_TREE)
3271 return false;
3272 break;
3274 default:;
3276 op = TREE_OPERAND (op, 0);
3279 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
3281 default:
3282 break;
3285 return false;
3288 /* Return true if T is function-invariant. */
3290 static bool
3291 tree_invariant_p (tree t)
3293 tree inner = skip_simple_arithmetic (t);
3294 return tree_invariant_p_1 (inner);
3297 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
3298 Do this to any expression which may be used in more than one place,
3299 but must be evaluated only once.
3301 Normally, expand_expr would reevaluate the expression each time.
3302 Calling save_expr produces something that is evaluated and recorded
3303 the first time expand_expr is called on it. Subsequent calls to
3304 expand_expr just reuse the recorded value.
3306 The call to expand_expr that generates code that actually computes
3307 the value is the first call *at compile time*. Subsequent calls
3308 *at compile time* generate code to use the saved value.
3309 This produces correct result provided that *at run time* control
3310 always flows through the insns made by the first expand_expr
3311 before reaching the other places where the save_expr was evaluated.
3312 You, the caller of save_expr, must make sure this is so.
3314 Constants, and certain read-only nodes, are returned with no
3315 SAVE_EXPR because that is safe. Expressions containing placeholders
3316 are not touched; see tree.def for an explanation of what these
3317 are used for. */
3319 tree
3320 save_expr (tree expr)
3322 tree t = fold (expr);
3323 tree inner;
3325 /* If the tree evaluates to a constant, then we don't want to hide that
3326 fact (i.e. this allows further folding, and direct checks for constants).
3327 However, a read-only object that has side effects cannot be bypassed.
3328 Since it is no problem to reevaluate literals, we just return the
3329 literal node. */
3330 inner = skip_simple_arithmetic (t);
3331 if (TREE_CODE (inner) == ERROR_MARK)
3332 return inner;
3334 if (tree_invariant_p_1 (inner))
3335 return t;
3337 /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
3338 it means that the size or offset of some field of an object depends on
3339 the value within another field.
3341 Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
3342 and some variable since it would then need to be both evaluated once and
3343 evaluated more than once. Front-ends must assure this case cannot
3344 happen by surrounding any such subexpressions in their own SAVE_EXPR
3345 and forcing evaluation at the proper time. */
3346 if (contains_placeholder_p (inner))
3347 return t;
3349 t = build1 (SAVE_EXPR, TREE_TYPE (expr), t);
3350 SET_EXPR_LOCATION (t, EXPR_LOCATION (expr));
3352 /* This expression might be placed ahead of a jump to ensure that the
3353 value was computed on both sides of the jump. So make sure it isn't
3354 eliminated as dead. */
3355 TREE_SIDE_EFFECTS (t) = 1;
3356 return t;
3359 /* Look inside EXPR into any simple arithmetic operations. Return the
3360 outermost non-arithmetic or non-invariant node. */
3362 tree
3363 skip_simple_arithmetic (tree expr)
3365 /* We don't care about whether this can be used as an lvalue in this
3366 context. */
3367 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3368 expr = TREE_OPERAND (expr, 0);
3370 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
3371 a constant, it will be more efficient to not make another SAVE_EXPR since
3372 it will allow better simplification and GCSE will be able to merge the
3373 computations if they actually occur. */
3374 while (true)
3376 if (UNARY_CLASS_P (expr))
3377 expr = TREE_OPERAND (expr, 0);
3378 else if (BINARY_CLASS_P (expr))
3380 if (tree_invariant_p (TREE_OPERAND (expr, 1)))
3381 expr = TREE_OPERAND (expr, 0);
3382 else if (tree_invariant_p (TREE_OPERAND (expr, 0)))
3383 expr = TREE_OPERAND (expr, 1);
3384 else
3385 break;
3387 else
3388 break;
3391 return expr;
3394 /* Look inside EXPR into simple arithmetic operations involving constants.
3395 Return the outermost non-arithmetic or non-constant node. */
3397 tree
3398 skip_simple_constant_arithmetic (tree expr)
3400 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
3401 expr = TREE_OPERAND (expr, 0);
3403 while (true)
3405 if (UNARY_CLASS_P (expr))
3406 expr = TREE_OPERAND (expr, 0);
3407 else if (BINARY_CLASS_P (expr))
3409 if (TREE_CONSTANT (TREE_OPERAND (expr, 1)))
3410 expr = TREE_OPERAND (expr, 0);
3411 else if (TREE_CONSTANT (TREE_OPERAND (expr, 0)))
3412 expr = TREE_OPERAND (expr, 1);
3413 else
3414 break;
3416 else
3417 break;
3420 return expr;
3423 /* Return which tree structure is used by T. */
3425 enum tree_node_structure_enum
3426 tree_node_structure (const_tree t)
3428 const enum tree_code code = TREE_CODE (t);
3429 return tree_node_structure_for_code (code);
3432 /* Set various status flags when building a CALL_EXPR object T. */
3434 static void
3435 process_call_operands (tree t)
3437 bool side_effects = TREE_SIDE_EFFECTS (t);
3438 bool read_only = false;
3439 int i = call_expr_flags (t);
3441 /* Calls have side-effects, except those to const or pure functions. */
3442 if ((i & ECF_LOOPING_CONST_OR_PURE) || !(i & (ECF_CONST | ECF_PURE)))
3443 side_effects = true;
3444 /* Propagate TREE_READONLY of arguments for const functions. */
3445 if (i & ECF_CONST)
3446 read_only = true;
3448 if (!side_effects || read_only)
3449 for (i = 1; i < TREE_OPERAND_LENGTH (t); i++)
3451 tree op = TREE_OPERAND (t, i);
3452 if (op && TREE_SIDE_EFFECTS (op))
3453 side_effects = true;
3454 if (op && !TREE_READONLY (op) && !CONSTANT_CLASS_P (op))
3455 read_only = false;
3458 TREE_SIDE_EFFECTS (t) = side_effects;
3459 TREE_READONLY (t) = read_only;
3462 /* Return true if EXP contains a PLACEHOLDER_EXPR, i.e. if it represents a
3463 size or offset that depends on a field within a record. */
3465 bool
3466 contains_placeholder_p (const_tree exp)
3468 enum tree_code code;
3470 if (!exp)
3471 return 0;
3473 code = TREE_CODE (exp);
3474 if (code == PLACEHOLDER_EXPR)
3475 return 1;
3477 switch (TREE_CODE_CLASS (code))
3479 case tcc_reference:
3480 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
3481 position computations since they will be converted into a
3482 WITH_RECORD_EXPR involving the reference, which will assume
3483 here will be valid. */
3484 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
3486 case tcc_exceptional:
3487 if (code == TREE_LIST)
3488 return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
3489 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
3490 break;
3492 case tcc_unary:
3493 case tcc_binary:
3494 case tcc_comparison:
3495 case tcc_expression:
3496 switch (code)
3498 case COMPOUND_EXPR:
3499 /* Ignoring the first operand isn't quite right, but works best. */
3500 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
3502 case COND_EXPR:
3503 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
3504 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
3505 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
3507 case SAVE_EXPR:
3508 /* The save_expr function never wraps anything containing
3509 a PLACEHOLDER_EXPR. */
3510 return 0;
3512 default:
3513 break;
3516 switch (TREE_CODE_LENGTH (code))
3518 case 1:
3519 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
3520 case 2:
3521 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
3522 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
3523 default:
3524 return 0;
3527 case tcc_vl_exp:
3528 switch (code)
3530 case CALL_EXPR:
3532 const_tree arg;
3533 const_call_expr_arg_iterator iter;
3534 FOR_EACH_CONST_CALL_EXPR_ARG (arg, iter, exp)
3535 if (CONTAINS_PLACEHOLDER_P (arg))
3536 return 1;
3537 return 0;
3539 default:
3540 return 0;
3543 default:
3544 return 0;
3546 return 0;
3549 /* Return true if any part of the structure of TYPE involves a PLACEHOLDER_EXPR
3550 directly. This includes size, bounds, qualifiers (for QUAL_UNION_TYPE) and
3551 field positions. */
3553 static bool
3554 type_contains_placeholder_1 (const_tree type)
3556 /* If the size contains a placeholder or the parent type (component type in
3557 the case of arrays) type involves a placeholder, this type does. */
3558 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
3559 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
3560 || (!POINTER_TYPE_P (type)
3561 && TREE_TYPE (type)
3562 && type_contains_placeholder_p (TREE_TYPE (type))))
3563 return true;
3565 /* Now do type-specific checks. Note that the last part of the check above
3566 greatly limits what we have to do below. */
3567 switch (TREE_CODE (type))
3569 case VOID_TYPE:
3570 case POINTER_BOUNDS_TYPE:
3571 case COMPLEX_TYPE:
3572 case ENUMERAL_TYPE:
3573 case BOOLEAN_TYPE:
3574 case POINTER_TYPE:
3575 case OFFSET_TYPE:
3576 case REFERENCE_TYPE:
3577 case METHOD_TYPE:
3578 case FUNCTION_TYPE:
3579 case VECTOR_TYPE:
3580 case NULLPTR_TYPE:
3581 return false;
3583 case INTEGER_TYPE:
3584 case REAL_TYPE:
3585 case FIXED_POINT_TYPE:
3586 /* Here we just check the bounds. */
3587 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
3588 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
3590 case ARRAY_TYPE:
3591 /* We have already checked the component type above, so just check the
3592 domain type. */
3593 return type_contains_placeholder_p (TYPE_DOMAIN (type));
3595 case RECORD_TYPE:
3596 case UNION_TYPE:
3597 case QUAL_UNION_TYPE:
3599 tree field;
3601 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3602 if (TREE_CODE (field) == FIELD_DECL
3603 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
3604 || (TREE_CODE (type) == QUAL_UNION_TYPE
3605 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
3606 || type_contains_placeholder_p (TREE_TYPE (field))))
3607 return true;
3609 return false;
3612 default:
3613 gcc_unreachable ();
3617 /* Wrapper around above function used to cache its result. */
3619 bool
3620 type_contains_placeholder_p (tree type)
3622 bool result;
3624 /* If the contains_placeholder_bits field has been initialized,
3625 then we know the answer. */
3626 if (TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) > 0)
3627 return TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) - 1;
3629 /* Indicate that we've seen this type node, and the answer is false.
3630 This is what we want to return if we run into recursion via fields. */
3631 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = 1;
3633 /* Compute the real value. */
3634 result = type_contains_placeholder_1 (type);
3636 /* Store the real value. */
3637 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type) = result + 1;
3639 return result;
3642 /* Push tree EXP onto vector QUEUE if it is not already present. */
3644 static void
3645 push_without_duplicates (tree exp, vec<tree> *queue)
3647 unsigned int i;
3648 tree iter;
3650 FOR_EACH_VEC_ELT (*queue, i, iter)
3651 if (simple_cst_equal (iter, exp) == 1)
3652 break;
3654 if (!iter)
3655 queue->safe_push (exp);
3658 /* Given a tree EXP, find all occurrences of references to fields
3659 in a PLACEHOLDER_EXPR and place them in vector REFS without
3660 duplicates. Also record VAR_DECLs and CONST_DECLs. Note that
3661 we assume here that EXP contains only arithmetic expressions
3662 or CALL_EXPRs with PLACEHOLDER_EXPRs occurring only in their
3663 argument list. */
3665 void
3666 find_placeholder_in_expr (tree exp, vec<tree> *refs)
3668 enum tree_code code = TREE_CODE (exp);
3669 tree inner;
3670 int i;
3672 /* We handle TREE_LIST and COMPONENT_REF separately. */
3673 if (code == TREE_LIST)
3675 FIND_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), refs);
3676 FIND_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), refs);
3678 else if (code == COMPONENT_REF)
3680 for (inner = TREE_OPERAND (exp, 0);
3681 REFERENCE_CLASS_P (inner);
3682 inner = TREE_OPERAND (inner, 0))
3685 if (TREE_CODE (inner) == PLACEHOLDER_EXPR)
3686 push_without_duplicates (exp, refs);
3687 else
3688 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), refs);
3690 else
3691 switch (TREE_CODE_CLASS (code))
3693 case tcc_constant:
3694 break;
3696 case tcc_declaration:
3697 /* Variables allocated to static storage can stay. */
3698 if (!TREE_STATIC (exp))
3699 push_without_duplicates (exp, refs);
3700 break;
3702 case tcc_expression:
3703 /* This is the pattern built in ada/make_aligning_type. */
3704 if (code == ADDR_EXPR
3705 && TREE_CODE (TREE_OPERAND (exp, 0)) == PLACEHOLDER_EXPR)
3707 push_without_duplicates (exp, refs);
3708 break;
3711 /* Fall through... */
3713 case tcc_exceptional:
3714 case tcc_unary:
3715 case tcc_binary:
3716 case tcc_comparison:
3717 case tcc_reference:
3718 for (i = 0; i < TREE_CODE_LENGTH (code); i++)
3719 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3720 break;
3722 case tcc_vl_exp:
3723 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3724 FIND_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, i), refs);
3725 break;
3727 default:
3728 gcc_unreachable ();
3732 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
3733 return a tree with all occurrences of references to F in a
3734 PLACEHOLDER_EXPR replaced by R. Also handle VAR_DECLs and
3735 CONST_DECLs. Note that we assume here that EXP contains only
3736 arithmetic expressions or CALL_EXPRs with PLACEHOLDER_EXPRs
3737 occurring only in their argument list. */
3739 tree
3740 substitute_in_expr (tree exp, tree f, tree r)
3742 enum tree_code code = TREE_CODE (exp);
3743 tree op0, op1, op2, op3;
3744 tree new_tree;
3746 /* We handle TREE_LIST and COMPONENT_REF separately. */
3747 if (code == TREE_LIST)
3749 op0 = SUBSTITUTE_IN_EXPR (TREE_CHAIN (exp), f, r);
3750 op1 = SUBSTITUTE_IN_EXPR (TREE_VALUE (exp), f, r);
3751 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3752 return exp;
3754 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3756 else if (code == COMPONENT_REF)
3758 tree inner;
3760 /* If this expression is getting a value from a PLACEHOLDER_EXPR
3761 and it is the right field, replace it with R. */
3762 for (inner = TREE_OPERAND (exp, 0);
3763 REFERENCE_CLASS_P (inner);
3764 inner = TREE_OPERAND (inner, 0))
3767 /* The field. */
3768 op1 = TREE_OPERAND (exp, 1);
3770 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && op1 == f)
3771 return r;
3773 /* If this expression hasn't been completed let, leave it alone. */
3774 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && !TREE_TYPE (inner))
3775 return exp;
3777 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3778 if (op0 == TREE_OPERAND (exp, 0))
3779 return exp;
3781 new_tree
3782 = fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, op1, NULL_TREE);
3784 else
3785 switch (TREE_CODE_CLASS (code))
3787 case tcc_constant:
3788 return exp;
3790 case tcc_declaration:
3791 if (exp == f)
3792 return r;
3793 else
3794 return exp;
3796 case tcc_expression:
3797 if (exp == f)
3798 return r;
3800 /* Fall through... */
3802 case tcc_exceptional:
3803 case tcc_unary:
3804 case tcc_binary:
3805 case tcc_comparison:
3806 case tcc_reference:
3807 switch (TREE_CODE_LENGTH (code))
3809 case 0:
3810 return exp;
3812 case 1:
3813 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3814 if (op0 == TREE_OPERAND (exp, 0))
3815 return exp;
3817 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
3818 break;
3820 case 2:
3821 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3822 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3824 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
3825 return exp;
3827 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
3828 break;
3830 case 3:
3831 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3832 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3833 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3835 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3836 && op2 == TREE_OPERAND (exp, 2))
3837 return exp;
3839 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
3840 break;
3842 case 4:
3843 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
3844 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
3845 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
3846 op3 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 3), f, r);
3848 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
3849 && op2 == TREE_OPERAND (exp, 2)
3850 && op3 == TREE_OPERAND (exp, 3))
3851 return exp;
3853 new_tree
3854 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
3855 break;
3857 default:
3858 gcc_unreachable ();
3860 break;
3862 case tcc_vl_exp:
3864 int i;
3866 new_tree = NULL_TREE;
3868 /* If we are trying to replace F with a constant, inline back
3869 functions which do nothing else than computing a value from
3870 the arguments they are passed. This makes it possible to
3871 fold partially or entirely the replacement expression. */
3872 if (CONSTANT_CLASS_P (r) && code == CALL_EXPR)
3874 tree t = maybe_inline_call_in_expr (exp);
3875 if (t)
3876 return SUBSTITUTE_IN_EXPR (t, f, r);
3879 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
3881 tree op = TREE_OPERAND (exp, i);
3882 tree new_op = SUBSTITUTE_IN_EXPR (op, f, r);
3883 if (new_op != op)
3885 if (!new_tree)
3886 new_tree = copy_node (exp);
3887 TREE_OPERAND (new_tree, i) = new_op;
3891 if (new_tree)
3893 new_tree = fold (new_tree);
3894 if (TREE_CODE (new_tree) == CALL_EXPR)
3895 process_call_operands (new_tree);
3897 else
3898 return exp;
3900 break;
3902 default:
3903 gcc_unreachable ();
3906 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
3908 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
3909 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
3911 return new_tree;
3914 /* Similar, but look for a PLACEHOLDER_EXPR in EXP and find a replacement
3915 for it within OBJ, a tree that is an object or a chain of references. */
3917 tree
3918 substitute_placeholder_in_expr (tree exp, tree obj)
3920 enum tree_code code = TREE_CODE (exp);
3921 tree op0, op1, op2, op3;
3922 tree new_tree;
3924 /* If this is a PLACEHOLDER_EXPR, see if we find a corresponding type
3925 in the chain of OBJ. */
3926 if (code == PLACEHOLDER_EXPR)
3928 tree need_type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
3929 tree elt;
3931 for (elt = obj; elt != 0;
3932 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3933 || TREE_CODE (elt) == COND_EXPR)
3934 ? TREE_OPERAND (elt, 1)
3935 : (REFERENCE_CLASS_P (elt)
3936 || UNARY_CLASS_P (elt)
3937 || BINARY_CLASS_P (elt)
3938 || VL_EXP_CLASS_P (elt)
3939 || EXPRESSION_CLASS_P (elt))
3940 ? TREE_OPERAND (elt, 0) : 0))
3941 if (TYPE_MAIN_VARIANT (TREE_TYPE (elt)) == need_type)
3942 return elt;
3944 for (elt = obj; elt != 0;
3945 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
3946 || TREE_CODE (elt) == COND_EXPR)
3947 ? TREE_OPERAND (elt, 1)
3948 : (REFERENCE_CLASS_P (elt)
3949 || UNARY_CLASS_P (elt)
3950 || BINARY_CLASS_P (elt)
3951 || VL_EXP_CLASS_P (elt)
3952 || EXPRESSION_CLASS_P (elt))
3953 ? TREE_OPERAND (elt, 0) : 0))
3954 if (POINTER_TYPE_P (TREE_TYPE (elt))
3955 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (elt)))
3956 == need_type))
3957 return fold_build1 (INDIRECT_REF, need_type, elt);
3959 /* If we didn't find it, return the original PLACEHOLDER_EXPR. If it
3960 survives until RTL generation, there will be an error. */
3961 return exp;
3964 /* TREE_LIST is special because we need to look at TREE_VALUE
3965 and TREE_CHAIN, not TREE_OPERANDS. */
3966 else if (code == TREE_LIST)
3968 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), obj);
3969 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), obj);
3970 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
3971 return exp;
3973 return tree_cons (TREE_PURPOSE (exp), op1, op0);
3975 else
3976 switch (TREE_CODE_CLASS (code))
3978 case tcc_constant:
3979 case tcc_declaration:
3980 return exp;
3982 case tcc_exceptional:
3983 case tcc_unary:
3984 case tcc_binary:
3985 case tcc_comparison:
3986 case tcc_expression:
3987 case tcc_reference:
3988 case tcc_statement:
3989 switch (TREE_CODE_LENGTH (code))
3991 case 0:
3992 return exp;
3994 case 1:
3995 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
3996 if (op0 == TREE_OPERAND (exp, 0))
3997 return exp;
3999 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
4000 break;
4002 case 2:
4003 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
4004 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
4006 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
4007 return exp;
4009 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
4010 break;
4012 case 3:
4013 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
4014 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
4015 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
4017 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
4018 && op2 == TREE_OPERAND (exp, 2))
4019 return exp;
4021 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
4022 break;
4024 case 4:
4025 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
4026 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
4027 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
4028 op3 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 3), obj);
4030 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
4031 && op2 == TREE_OPERAND (exp, 2)
4032 && op3 == TREE_OPERAND (exp, 3))
4033 return exp;
4035 new_tree
4036 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
4037 break;
4039 default:
4040 gcc_unreachable ();
4042 break;
4044 case tcc_vl_exp:
4046 int i;
4048 new_tree = NULL_TREE;
4050 for (i = 1; i < TREE_OPERAND_LENGTH (exp); i++)
4052 tree op = TREE_OPERAND (exp, i);
4053 tree new_op = SUBSTITUTE_PLACEHOLDER_IN_EXPR (op, obj);
4054 if (new_op != op)
4056 if (!new_tree)
4057 new_tree = copy_node (exp);
4058 TREE_OPERAND (new_tree, i) = new_op;
4062 if (new_tree)
4064 new_tree = fold (new_tree);
4065 if (TREE_CODE (new_tree) == CALL_EXPR)
4066 process_call_operands (new_tree);
4068 else
4069 return exp;
4071 break;
4073 default:
4074 gcc_unreachable ();
4077 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
4079 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
4080 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
4082 return new_tree;
4086 /* Subroutine of stabilize_reference; this is called for subtrees of
4087 references. Any expression with side-effects must be put in a SAVE_EXPR
4088 to ensure that it is only evaluated once.
4090 We don't put SAVE_EXPR nodes around everything, because assigning very
4091 simple expressions to temporaries causes us to miss good opportunities
4092 for optimizations. Among other things, the opportunity to fold in the
4093 addition of a constant into an addressing mode often gets lost, e.g.
4094 "y[i+1] += x;". In general, we take the approach that we should not make
4095 an assignment unless we are forced into it - i.e., that any non-side effect
4096 operator should be allowed, and that cse should take care of coalescing
4097 multiple utterances of the same expression should that prove fruitful. */
4099 static tree
4100 stabilize_reference_1 (tree e)
4102 tree result;
4103 enum tree_code code = TREE_CODE (e);
4105 /* We cannot ignore const expressions because it might be a reference
4106 to a const array but whose index contains side-effects. But we can
4107 ignore things that are actual constant or that already have been
4108 handled by this function. */
4110 if (tree_invariant_p (e))
4111 return e;
4113 switch (TREE_CODE_CLASS (code))
4115 case tcc_exceptional:
4116 case tcc_type:
4117 case tcc_declaration:
4118 case tcc_comparison:
4119 case tcc_statement:
4120 case tcc_expression:
4121 case tcc_reference:
4122 case tcc_vl_exp:
4123 /* If the expression has side-effects, then encase it in a SAVE_EXPR
4124 so that it will only be evaluated once. */
4125 /* The reference (r) and comparison (<) classes could be handled as
4126 below, but it is generally faster to only evaluate them once. */
4127 if (TREE_SIDE_EFFECTS (e))
4128 return save_expr (e);
4129 return e;
4131 case tcc_constant:
4132 /* Constants need no processing. In fact, we should never reach
4133 here. */
4134 return e;
4136 case tcc_binary:
4137 /* Division is slow and tends to be compiled with jumps,
4138 especially the division by powers of 2 that is often
4139 found inside of an array reference. So do it just once. */
4140 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
4141 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
4142 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
4143 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
4144 return save_expr (e);
4145 /* Recursively stabilize each operand. */
4146 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
4147 stabilize_reference_1 (TREE_OPERAND (e, 1)));
4148 break;
4150 case tcc_unary:
4151 /* Recursively stabilize each operand. */
4152 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
4153 break;
4155 default:
4156 gcc_unreachable ();
4159 TREE_TYPE (result) = TREE_TYPE (e);
4160 TREE_READONLY (result) = TREE_READONLY (e);
4161 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
4162 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
4164 return result;
4167 /* Stabilize a reference so that we can use it any number of times
4168 without causing its operands to be evaluated more than once.
4169 Returns the stabilized reference. This works by means of save_expr,
4170 so see the caveats in the comments about save_expr.
4172 Also allows conversion expressions whose operands are references.
4173 Any other kind of expression is returned unchanged. */
4175 tree
4176 stabilize_reference (tree ref)
4178 tree result;
4179 enum tree_code code = TREE_CODE (ref);
4181 switch (code)
4183 case VAR_DECL:
4184 case PARM_DECL:
4185 case RESULT_DECL:
4186 /* No action is needed in this case. */
4187 return ref;
4189 CASE_CONVERT:
4190 case FLOAT_EXPR:
4191 case FIX_TRUNC_EXPR:
4192 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
4193 break;
4195 case INDIRECT_REF:
4196 result = build_nt (INDIRECT_REF,
4197 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
4198 break;
4200 case COMPONENT_REF:
4201 result = build_nt (COMPONENT_REF,
4202 stabilize_reference (TREE_OPERAND (ref, 0)),
4203 TREE_OPERAND (ref, 1), NULL_TREE);
4204 break;
4206 case BIT_FIELD_REF:
4207 result = build_nt (BIT_FIELD_REF,
4208 stabilize_reference (TREE_OPERAND (ref, 0)),
4209 TREE_OPERAND (ref, 1), TREE_OPERAND (ref, 2));
4210 break;
4212 case ARRAY_REF:
4213 result = build_nt (ARRAY_REF,
4214 stabilize_reference (TREE_OPERAND (ref, 0)),
4215 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
4216 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
4217 break;
4219 case ARRAY_RANGE_REF:
4220 result = build_nt (ARRAY_RANGE_REF,
4221 stabilize_reference (TREE_OPERAND (ref, 0)),
4222 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
4223 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
4224 break;
4226 case COMPOUND_EXPR:
4227 /* We cannot wrap the first expression in a SAVE_EXPR, as then
4228 it wouldn't be ignored. This matters when dealing with
4229 volatiles. */
4230 return stabilize_reference_1 (ref);
4232 /* If arg isn't a kind of lvalue we recognize, make no change.
4233 Caller should recognize the error for an invalid lvalue. */
4234 default:
4235 return ref;
4237 case ERROR_MARK:
4238 return error_mark_node;
4241 TREE_TYPE (result) = TREE_TYPE (ref);
4242 TREE_READONLY (result) = TREE_READONLY (ref);
4243 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
4244 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
4246 return result;
4249 /* Low-level constructors for expressions. */
4251 /* A helper function for build1 and constant folders. Set TREE_CONSTANT,
4252 and TREE_SIDE_EFFECTS for an ADDR_EXPR. */
4254 void
4255 recompute_tree_invariant_for_addr_expr (tree t)
4257 tree node;
4258 bool tc = true, se = false;
4260 gcc_assert (TREE_CODE (t) == ADDR_EXPR);
4262 /* We started out assuming this address is both invariant and constant, but
4263 does not have side effects. Now go down any handled components and see if
4264 any of them involve offsets that are either non-constant or non-invariant.
4265 Also check for side-effects.
4267 ??? Note that this code makes no attempt to deal with the case where
4268 taking the address of something causes a copy due to misalignment. */
4270 #define UPDATE_FLAGS(NODE) \
4271 do { tree _node = (NODE); \
4272 if (_node && !TREE_CONSTANT (_node)) tc = false; \
4273 if (_node && TREE_SIDE_EFFECTS (_node)) se = true; } while (0)
4275 for (node = TREE_OPERAND (t, 0); handled_component_p (node);
4276 node = TREE_OPERAND (node, 0))
4278 /* If the first operand doesn't have an ARRAY_TYPE, this is a bogus
4279 array reference (probably made temporarily by the G++ front end),
4280 so ignore all the operands. */
4281 if ((TREE_CODE (node) == ARRAY_REF
4282 || TREE_CODE (node) == ARRAY_RANGE_REF)
4283 && TREE_CODE (TREE_TYPE (TREE_OPERAND (node, 0))) == ARRAY_TYPE)
4285 UPDATE_FLAGS (TREE_OPERAND (node, 1));
4286 if (TREE_OPERAND (node, 2))
4287 UPDATE_FLAGS (TREE_OPERAND (node, 2));
4288 if (TREE_OPERAND (node, 3))
4289 UPDATE_FLAGS (TREE_OPERAND (node, 3));
4291 /* Likewise, just because this is a COMPONENT_REF doesn't mean we have a
4292 FIELD_DECL, apparently. The G++ front end can put something else
4293 there, at least temporarily. */
4294 else if (TREE_CODE (node) == COMPONENT_REF
4295 && TREE_CODE (TREE_OPERAND (node, 1)) == FIELD_DECL)
4297 if (TREE_OPERAND (node, 2))
4298 UPDATE_FLAGS (TREE_OPERAND (node, 2));
4302 node = lang_hooks.expr_to_decl (node, &tc, &se);
4304 /* Now see what's inside. If it's an INDIRECT_REF, copy our properties from
4305 the address, since &(*a)->b is a form of addition. If it's a constant, the
4306 address is constant too. If it's a decl, its address is constant if the
4307 decl is static. Everything else is not constant and, furthermore,
4308 taking the address of a volatile variable is not volatile. */
4309 if (TREE_CODE (node) == INDIRECT_REF
4310 || TREE_CODE (node) == MEM_REF)
4311 UPDATE_FLAGS (TREE_OPERAND (node, 0));
4312 else if (CONSTANT_CLASS_P (node))
4314 else if (DECL_P (node))
4315 tc &= (staticp (node) != NULL_TREE);
4316 else
4318 tc = false;
4319 se |= TREE_SIDE_EFFECTS (node);
4323 TREE_CONSTANT (t) = tc;
4324 TREE_SIDE_EFFECTS (t) = se;
4325 #undef UPDATE_FLAGS
4328 /* Build an expression of code CODE, data type TYPE, and operands as
4329 specified. Expressions and reference nodes can be created this way.
4330 Constants, decls, types and misc nodes cannot be.
4332 We define 5 non-variadic functions, from 0 to 4 arguments. This is
4333 enough for all extant tree codes. */
4335 tree
4336 build0_stat (enum tree_code code, tree tt MEM_STAT_DECL)
4338 tree t;
4340 gcc_assert (TREE_CODE_LENGTH (code) == 0);
4342 t = make_node_stat (code PASS_MEM_STAT);
4343 TREE_TYPE (t) = tt;
4345 return t;
4348 tree
4349 build1_stat (enum tree_code code, tree type, tree node MEM_STAT_DECL)
4351 int length = sizeof (struct tree_exp);
4352 tree t;
4354 record_node_allocation_statistics (code, length);
4356 gcc_assert (TREE_CODE_LENGTH (code) == 1);
4358 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
4360 memset (t, 0, sizeof (struct tree_common));
4362 TREE_SET_CODE (t, code);
4364 TREE_TYPE (t) = type;
4365 SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
4366 TREE_OPERAND (t, 0) = node;
4367 if (node && !TYPE_P (node))
4369 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
4370 TREE_READONLY (t) = TREE_READONLY (node);
4373 if (TREE_CODE_CLASS (code) == tcc_statement)
4374 TREE_SIDE_EFFECTS (t) = 1;
4375 else switch (code)
4377 case VA_ARG_EXPR:
4378 /* All of these have side-effects, no matter what their
4379 operands are. */
4380 TREE_SIDE_EFFECTS (t) = 1;
4381 TREE_READONLY (t) = 0;
4382 break;
4384 case INDIRECT_REF:
4385 /* Whether a dereference is readonly has nothing to do with whether
4386 its operand is readonly. */
4387 TREE_READONLY (t) = 0;
4388 break;
4390 case ADDR_EXPR:
4391 if (node)
4392 recompute_tree_invariant_for_addr_expr (t);
4393 break;
4395 default:
4396 if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
4397 && node && !TYPE_P (node)
4398 && TREE_CONSTANT (node))
4399 TREE_CONSTANT (t) = 1;
4400 if (TREE_CODE_CLASS (code) == tcc_reference
4401 && node && TREE_THIS_VOLATILE (node))
4402 TREE_THIS_VOLATILE (t) = 1;
4403 break;
4406 return t;
4409 #define PROCESS_ARG(N) \
4410 do { \
4411 TREE_OPERAND (t, N) = arg##N; \
4412 if (arg##N &&!TYPE_P (arg##N)) \
4414 if (TREE_SIDE_EFFECTS (arg##N)) \
4415 side_effects = 1; \
4416 if (!TREE_READONLY (arg##N) \
4417 && !CONSTANT_CLASS_P (arg##N)) \
4418 (void) (read_only = 0); \
4419 if (!TREE_CONSTANT (arg##N)) \
4420 (void) (constant = 0); \
4422 } while (0)
4424 tree
4425 build2_stat (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
4427 bool constant, read_only, side_effects;
4428 tree t;
4430 gcc_assert (TREE_CODE_LENGTH (code) == 2);
4432 if ((code == MINUS_EXPR || code == PLUS_EXPR || code == MULT_EXPR)
4433 && arg0 && arg1 && tt && POINTER_TYPE_P (tt)
4434 /* When sizetype precision doesn't match that of pointers
4435 we need to be able to build explicit extensions or truncations
4436 of the offset argument. */
4437 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (tt))
4438 gcc_assert (TREE_CODE (arg0) == INTEGER_CST
4439 && TREE_CODE (arg1) == INTEGER_CST);
4441 if (code == POINTER_PLUS_EXPR && arg0 && arg1 && tt)
4442 gcc_assert (POINTER_TYPE_P (tt) && POINTER_TYPE_P (TREE_TYPE (arg0))
4443 && ptrofftype_p (TREE_TYPE (arg1)));
4445 t = make_node_stat (code PASS_MEM_STAT);
4446 TREE_TYPE (t) = tt;
4448 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
4449 result based on those same flags for the arguments. But if the
4450 arguments aren't really even `tree' expressions, we shouldn't be trying
4451 to do this. */
4453 /* Expressions without side effects may be constant if their
4454 arguments are as well. */
4455 constant = (TREE_CODE_CLASS (code) == tcc_comparison
4456 || TREE_CODE_CLASS (code) == tcc_binary);
4457 read_only = 1;
4458 side_effects = TREE_SIDE_EFFECTS (t);
4460 PROCESS_ARG (0);
4461 PROCESS_ARG (1);
4463 TREE_SIDE_EFFECTS (t) = side_effects;
4464 if (code == MEM_REF)
4466 if (arg0 && TREE_CODE (arg0) == ADDR_EXPR)
4468 tree o = TREE_OPERAND (arg0, 0);
4469 TREE_READONLY (t) = TREE_READONLY (o);
4470 TREE_THIS_VOLATILE (t) = TREE_THIS_VOLATILE (o);
4473 else
4475 TREE_READONLY (t) = read_only;
4476 TREE_CONSTANT (t) = constant;
4477 TREE_THIS_VOLATILE (t)
4478 = (TREE_CODE_CLASS (code) == tcc_reference
4479 && arg0 && TREE_THIS_VOLATILE (arg0));
4482 return t;
4486 tree
4487 build3_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4488 tree arg2 MEM_STAT_DECL)
4490 bool constant, read_only, side_effects;
4491 tree t;
4493 gcc_assert (TREE_CODE_LENGTH (code) == 3);
4494 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4496 t = make_node_stat (code PASS_MEM_STAT);
4497 TREE_TYPE (t) = tt;
4499 read_only = 1;
4501 /* As a special exception, if COND_EXPR has NULL branches, we
4502 assume that it is a gimple statement and always consider
4503 it to have side effects. */
4504 if (code == COND_EXPR
4505 && tt == void_type_node
4506 && arg1 == NULL_TREE
4507 && arg2 == NULL_TREE)
4508 side_effects = true;
4509 else
4510 side_effects = TREE_SIDE_EFFECTS (t);
4512 PROCESS_ARG (0);
4513 PROCESS_ARG (1);
4514 PROCESS_ARG (2);
4516 if (code == COND_EXPR)
4517 TREE_READONLY (t) = read_only;
4519 TREE_SIDE_EFFECTS (t) = side_effects;
4520 TREE_THIS_VOLATILE (t)
4521 = (TREE_CODE_CLASS (code) == tcc_reference
4522 && arg0 && TREE_THIS_VOLATILE (arg0));
4524 return t;
4527 tree
4528 build4_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4529 tree arg2, tree arg3 MEM_STAT_DECL)
4531 bool constant, read_only, side_effects;
4532 tree t;
4534 gcc_assert (TREE_CODE_LENGTH (code) == 4);
4536 t = make_node_stat (code PASS_MEM_STAT);
4537 TREE_TYPE (t) = tt;
4539 side_effects = TREE_SIDE_EFFECTS (t);
4541 PROCESS_ARG (0);
4542 PROCESS_ARG (1);
4543 PROCESS_ARG (2);
4544 PROCESS_ARG (3);
4546 TREE_SIDE_EFFECTS (t) = side_effects;
4547 TREE_THIS_VOLATILE (t)
4548 = (TREE_CODE_CLASS (code) == tcc_reference
4549 && arg0 && TREE_THIS_VOLATILE (arg0));
4551 return t;
4554 tree
4555 build5_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4556 tree arg2, tree arg3, tree arg4 MEM_STAT_DECL)
4558 bool constant, read_only, side_effects;
4559 tree t;
4561 gcc_assert (TREE_CODE_LENGTH (code) == 5);
4563 t = make_node_stat (code PASS_MEM_STAT);
4564 TREE_TYPE (t) = tt;
4566 side_effects = TREE_SIDE_EFFECTS (t);
4568 PROCESS_ARG (0);
4569 PROCESS_ARG (1);
4570 PROCESS_ARG (2);
4571 PROCESS_ARG (3);
4572 PROCESS_ARG (4);
4574 TREE_SIDE_EFFECTS (t) = side_effects;
4575 if (code == TARGET_MEM_REF)
4577 if (arg0 && TREE_CODE (arg0) == ADDR_EXPR)
4579 tree o = TREE_OPERAND (arg0, 0);
4580 TREE_READONLY (t) = TREE_READONLY (o);
4581 TREE_THIS_VOLATILE (t) = TREE_THIS_VOLATILE (o);
4584 else
4585 TREE_THIS_VOLATILE (t)
4586 = (TREE_CODE_CLASS (code) == tcc_reference
4587 && arg0 && TREE_THIS_VOLATILE (arg0));
4589 return t;
4592 /* Build a simple MEM_REF tree with the sematics of a plain INDIRECT_REF
4593 on the pointer PTR. */
4595 tree
4596 build_simple_mem_ref_loc (location_t loc, tree ptr)
4598 HOST_WIDE_INT offset = 0;
4599 tree ptype = TREE_TYPE (ptr);
4600 tree tem;
4601 /* For convenience allow addresses that collapse to a simple base
4602 and offset. */
4603 if (TREE_CODE (ptr) == ADDR_EXPR
4604 && (handled_component_p (TREE_OPERAND (ptr, 0))
4605 || TREE_CODE (TREE_OPERAND (ptr, 0)) == MEM_REF))
4607 ptr = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &offset);
4608 gcc_assert (ptr);
4609 ptr = build_fold_addr_expr (ptr);
4610 gcc_assert (is_gimple_reg (ptr) || is_gimple_min_invariant (ptr));
4612 tem = build2 (MEM_REF, TREE_TYPE (ptype),
4613 ptr, build_int_cst (ptype, offset));
4614 SET_EXPR_LOCATION (tem, loc);
4615 return tem;
4618 /* Return the constant offset of a MEM_REF or TARGET_MEM_REF tree T. */
4620 offset_int
4621 mem_ref_offset (const_tree t)
4623 return offset_int::from (TREE_OPERAND (t, 1), SIGNED);
4626 /* Return an invariant ADDR_EXPR of type TYPE taking the address of BASE
4627 offsetted by OFFSET units. */
4629 tree
4630 build_invariant_address (tree type, tree base, HOST_WIDE_INT offset)
4632 tree ref = fold_build2 (MEM_REF, TREE_TYPE (type),
4633 build_fold_addr_expr (base),
4634 build_int_cst (ptr_type_node, offset));
4635 tree addr = build1 (ADDR_EXPR, type, ref);
4636 recompute_tree_invariant_for_addr_expr (addr);
4637 return addr;
4640 /* Similar except don't specify the TREE_TYPE
4641 and leave the TREE_SIDE_EFFECTS as 0.
4642 It is permissible for arguments to be null,
4643 or even garbage if their values do not matter. */
4645 tree
4646 build_nt (enum tree_code code, ...)
4648 tree t;
4649 int length;
4650 int i;
4651 va_list p;
4653 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4655 va_start (p, code);
4657 t = make_node (code);
4658 length = TREE_CODE_LENGTH (code);
4660 for (i = 0; i < length; i++)
4661 TREE_OPERAND (t, i) = va_arg (p, tree);
4663 va_end (p);
4664 return t;
4667 /* Similar to build_nt, but for creating a CALL_EXPR object with a
4668 tree vec. */
4670 tree
4671 build_nt_call_vec (tree fn, vec<tree, va_gc> *args)
4673 tree ret, t;
4674 unsigned int ix;
4676 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
4677 CALL_EXPR_FN (ret) = fn;
4678 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
4679 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
4680 CALL_EXPR_ARG (ret, ix) = t;
4681 return ret;
4684 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
4685 We do NOT enter this node in any sort of symbol table.
4687 LOC is the location of the decl.
4689 layout_decl is used to set up the decl's storage layout.
4690 Other slots are initialized to 0 or null pointers. */
4692 tree
4693 build_decl_stat (location_t loc, enum tree_code code, tree name,
4694 tree type MEM_STAT_DECL)
4696 tree t;
4698 t = make_node_stat (code PASS_MEM_STAT);
4699 DECL_SOURCE_LOCATION (t) = loc;
4701 /* if (type == error_mark_node)
4702 type = integer_type_node; */
4703 /* That is not done, deliberately, so that having error_mark_node
4704 as the type can suppress useless errors in the use of this variable. */
4706 DECL_NAME (t) = name;
4707 TREE_TYPE (t) = type;
4709 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
4710 layout_decl (t, 0);
4712 return t;
4715 /* Builds and returns function declaration with NAME and TYPE. */
4717 tree
4718 build_fn_decl (const char *name, tree type)
4720 tree id = get_identifier (name);
4721 tree decl = build_decl (input_location, FUNCTION_DECL, id, type);
4723 DECL_EXTERNAL (decl) = 1;
4724 TREE_PUBLIC (decl) = 1;
4725 DECL_ARTIFICIAL (decl) = 1;
4726 TREE_NOTHROW (decl) = 1;
4728 return decl;
4731 vec<tree, va_gc> *all_translation_units;
4733 /* Builds a new translation-unit decl with name NAME, queues it in the
4734 global list of translation-unit decls and returns it. */
4736 tree
4737 build_translation_unit_decl (tree name)
4739 tree tu = build_decl (UNKNOWN_LOCATION, TRANSLATION_UNIT_DECL,
4740 name, NULL_TREE);
4741 TRANSLATION_UNIT_LANGUAGE (tu) = lang_hooks.name;
4742 vec_safe_push (all_translation_units, tu);
4743 return tu;
4747 /* BLOCK nodes are used to represent the structure of binding contours
4748 and declarations, once those contours have been exited and their contents
4749 compiled. This information is used for outputting debugging info. */
4751 tree
4752 build_block (tree vars, tree subblocks, tree supercontext, tree chain)
4754 tree block = make_node (BLOCK);
4756 BLOCK_VARS (block) = vars;
4757 BLOCK_SUBBLOCKS (block) = subblocks;
4758 BLOCK_SUPERCONTEXT (block) = supercontext;
4759 BLOCK_CHAIN (block) = chain;
4760 return block;
4764 /* Like SET_EXPR_LOCATION, but make sure the tree can have a location.
4766 LOC is the location to use in tree T. */
4768 void
4769 protected_set_expr_location (tree t, location_t loc)
4771 if (CAN_HAVE_LOCATION_P (t))
4772 SET_EXPR_LOCATION (t, loc);
4775 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
4776 is ATTRIBUTE. */
4778 tree
4779 build_decl_attribute_variant (tree ddecl, tree attribute)
4781 DECL_ATTRIBUTES (ddecl) = attribute;
4782 return ddecl;
4785 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4786 is ATTRIBUTE and its qualifiers are QUALS.
4788 Record such modified types already made so we don't make duplicates. */
4790 tree
4791 build_type_attribute_qual_variant (tree ttype, tree attribute, int quals)
4793 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
4795 inchash::hash hstate;
4796 tree ntype;
4797 int i;
4798 tree t;
4799 enum tree_code code = TREE_CODE (ttype);
4801 /* Building a distinct copy of a tagged type is inappropriate; it
4802 causes breakage in code that expects there to be a one-to-one
4803 relationship between a struct and its fields.
4804 build_duplicate_type is another solution (as used in
4805 handle_transparent_union_attribute), but that doesn't play well
4806 with the stronger C++ type identity model. */
4807 if (TREE_CODE (ttype) == RECORD_TYPE
4808 || TREE_CODE (ttype) == UNION_TYPE
4809 || TREE_CODE (ttype) == QUAL_UNION_TYPE
4810 || TREE_CODE (ttype) == ENUMERAL_TYPE)
4812 warning (OPT_Wattributes,
4813 "ignoring attributes applied to %qT after definition",
4814 TYPE_MAIN_VARIANT (ttype));
4815 return build_qualified_type (ttype, quals);
4818 ttype = build_qualified_type (ttype, TYPE_UNQUALIFIED);
4819 ntype = build_distinct_type_copy (ttype);
4821 TYPE_ATTRIBUTES (ntype) = attribute;
4823 hstate.add_int (code);
4824 if (TREE_TYPE (ntype))
4825 hstate.add_object (TYPE_HASH (TREE_TYPE (ntype)));
4826 attribute_hash_list (attribute, hstate);
4828 switch (TREE_CODE (ntype))
4830 case FUNCTION_TYPE:
4831 type_hash_list (TYPE_ARG_TYPES (ntype), hstate);
4832 break;
4833 case ARRAY_TYPE:
4834 if (TYPE_DOMAIN (ntype))
4835 hstate.add_object (TYPE_HASH (TYPE_DOMAIN (ntype)));
4836 break;
4837 case INTEGER_TYPE:
4838 t = TYPE_MAX_VALUE (ntype);
4839 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
4840 hstate.add_object (TREE_INT_CST_ELT (t, i));
4841 break;
4842 case REAL_TYPE:
4843 case FIXED_POINT_TYPE:
4845 unsigned int precision = TYPE_PRECISION (ntype);
4846 hstate.add_object (precision);
4848 break;
4849 default:
4850 break;
4853 ntype = type_hash_canon (hstate.end(), ntype);
4855 /* If the target-dependent attributes make NTYPE different from
4856 its canonical type, we will need to use structural equality
4857 checks for this type. */
4858 if (TYPE_STRUCTURAL_EQUALITY_P (ttype)
4859 || !comp_type_attributes (ntype, ttype))
4860 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
4861 else if (TYPE_CANONICAL (ntype) == ntype)
4862 TYPE_CANONICAL (ntype) = TYPE_CANONICAL (ttype);
4864 ttype = build_qualified_type (ntype, quals);
4866 else if (TYPE_QUALS (ttype) != quals)
4867 ttype = build_qualified_type (ttype, quals);
4869 return ttype;
4872 /* Check if "omp declare simd" attribute arguments, CLAUSES1 and CLAUSES2, are
4873 the same. */
4875 static bool
4876 omp_declare_simd_clauses_equal (tree clauses1, tree clauses2)
4878 tree cl1, cl2;
4879 for (cl1 = clauses1, cl2 = clauses2;
4880 cl1 && cl2;
4881 cl1 = OMP_CLAUSE_CHAIN (cl1), cl2 = OMP_CLAUSE_CHAIN (cl2))
4883 if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_CODE (cl2))
4884 return false;
4885 if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_SIMDLEN)
4887 if (simple_cst_equal (OMP_CLAUSE_DECL (cl1),
4888 OMP_CLAUSE_DECL (cl2)) != 1)
4889 return false;
4891 switch (OMP_CLAUSE_CODE (cl1))
4893 case OMP_CLAUSE_ALIGNED:
4894 if (simple_cst_equal (OMP_CLAUSE_ALIGNED_ALIGNMENT (cl1),
4895 OMP_CLAUSE_ALIGNED_ALIGNMENT (cl2)) != 1)
4896 return false;
4897 break;
4898 case OMP_CLAUSE_LINEAR:
4899 if (simple_cst_equal (OMP_CLAUSE_LINEAR_STEP (cl1),
4900 OMP_CLAUSE_LINEAR_STEP (cl2)) != 1)
4901 return false;
4902 break;
4903 case OMP_CLAUSE_SIMDLEN:
4904 if (simple_cst_equal (OMP_CLAUSE_SIMDLEN_EXPR (cl1),
4905 OMP_CLAUSE_SIMDLEN_EXPR (cl2)) != 1)
4906 return false;
4907 default:
4908 break;
4911 return true;
4914 /* Compare two constructor-element-type constants. Return 1 if the lists
4915 are known to be equal; otherwise return 0. */
4917 static bool
4918 simple_cst_list_equal (const_tree l1, const_tree l2)
4920 while (l1 != NULL_TREE && l2 != NULL_TREE)
4922 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
4923 return false;
4925 l1 = TREE_CHAIN (l1);
4926 l2 = TREE_CHAIN (l2);
4929 return l1 == l2;
4932 /* Compare two identifier nodes representing attributes. Either one may
4933 be in wrapped __ATTR__ form. Return true if they are the same, false
4934 otherwise. */
4936 static bool
4937 cmp_attrib_identifiers (const_tree attr1, const_tree attr2)
4939 /* Make sure we're dealing with IDENTIFIER_NODEs. */
4940 gcc_checking_assert (TREE_CODE (attr1) == IDENTIFIER_NODE
4941 && TREE_CODE (attr2) == IDENTIFIER_NODE);
4943 /* Identifiers can be compared directly for equality. */
4944 if (attr1 == attr2)
4945 return true;
4947 /* If they are not equal, they may still be one in the form
4948 'text' while the other one is in the form '__text__'. TODO:
4949 If we were storing attributes in normalized 'text' form, then
4950 this could all go away and we could take full advantage of
4951 the fact that we're comparing identifiers. :-) */
4952 const size_t attr1_len = IDENTIFIER_LENGTH (attr1);
4953 const size_t attr2_len = IDENTIFIER_LENGTH (attr2);
4955 if (attr2_len == attr1_len + 4)
4957 const char *p = IDENTIFIER_POINTER (attr2);
4958 const char *q = IDENTIFIER_POINTER (attr1);
4959 if (p[0] == '_' && p[1] == '_'
4960 && p[attr2_len - 2] == '_' && p[attr2_len - 1] == '_'
4961 && strncmp (q, p + 2, attr1_len) == 0)
4962 return true;;
4964 else if (attr2_len + 4 == attr1_len)
4966 const char *p = IDENTIFIER_POINTER (attr2);
4967 const char *q = IDENTIFIER_POINTER (attr1);
4968 if (q[0] == '_' && q[1] == '_'
4969 && q[attr1_len - 2] == '_' && q[attr1_len - 1] == '_'
4970 && strncmp (q + 2, p, attr2_len) == 0)
4971 return true;
4974 return false;
4977 /* Compare two attributes for their value identity. Return true if the
4978 attribute values are known to be equal; otherwise return false. */
4980 bool
4981 attribute_value_equal (const_tree attr1, const_tree attr2)
4983 if (TREE_VALUE (attr1) == TREE_VALUE (attr2))
4984 return true;
4986 if (TREE_VALUE (attr1) != NULL_TREE
4987 && TREE_CODE (TREE_VALUE (attr1)) == TREE_LIST
4988 && TREE_VALUE (attr2) != NULL_TREE
4989 && TREE_CODE (TREE_VALUE (attr2)) == TREE_LIST)
4991 /* Handle attribute format. */
4992 if (is_attribute_p ("format", TREE_PURPOSE (attr1)))
4994 attr1 = TREE_VALUE (attr1);
4995 attr2 = TREE_VALUE (attr2);
4996 /* Compare the archetypes (printf/scanf/strftime/...). */
4997 if (!cmp_attrib_identifiers (TREE_VALUE (attr1),
4998 TREE_VALUE (attr2)))
4999 return false;
5000 /* Archetypes are the same. Compare the rest. */
5001 return (simple_cst_list_equal (TREE_CHAIN (attr1),
5002 TREE_CHAIN (attr2)) == 1);
5004 return (simple_cst_list_equal (TREE_VALUE (attr1),
5005 TREE_VALUE (attr2)) == 1);
5008 if ((flag_openmp || flag_openmp_simd)
5009 && TREE_VALUE (attr1) && TREE_VALUE (attr2)
5010 && TREE_CODE (TREE_VALUE (attr1)) == OMP_CLAUSE
5011 && TREE_CODE (TREE_VALUE (attr2)) == OMP_CLAUSE)
5012 return omp_declare_simd_clauses_equal (TREE_VALUE (attr1),
5013 TREE_VALUE (attr2));
5015 return (simple_cst_equal (TREE_VALUE (attr1), TREE_VALUE (attr2)) == 1);
5018 /* Return 0 if the attributes for two types are incompatible, 1 if they
5019 are compatible, and 2 if they are nearly compatible (which causes a
5020 warning to be generated). */
5022 comp_type_attributes (const_tree type1, const_tree type2)
5024 const_tree a1 = TYPE_ATTRIBUTES (type1);
5025 const_tree a2 = TYPE_ATTRIBUTES (type2);
5026 const_tree a;
5028 if (a1 == a2)
5029 return 1;
5030 for (a = a1; a != NULL_TREE; a = TREE_CHAIN (a))
5032 const struct attribute_spec *as;
5033 const_tree attr;
5035 as = lookup_attribute_spec (get_attribute_name (a));
5036 if (!as || as->affects_type_identity == false)
5037 continue;
5039 attr = lookup_attribute (as->name, CONST_CAST_TREE (a2));
5040 if (!attr || !attribute_value_equal (a, attr))
5041 break;
5043 if (!a)
5045 for (a = a2; a != NULL_TREE; a = TREE_CHAIN (a))
5047 const struct attribute_spec *as;
5049 as = lookup_attribute_spec (get_attribute_name (a));
5050 if (!as || as->affects_type_identity == false)
5051 continue;
5053 if (!lookup_attribute (as->name, CONST_CAST_TREE (a1)))
5054 break;
5055 /* We don't need to compare trees again, as we did this
5056 already in first loop. */
5058 /* All types - affecting identity - are equal, so
5059 there is no need to call target hook for comparison. */
5060 if (!a)
5061 return 1;
5063 if (lookup_attribute ("transaction_safe", CONST_CAST_TREE (a)))
5064 return 0;
5065 /* As some type combinations - like default calling-convention - might
5066 be compatible, we have to call the target hook to get the final result. */
5067 return targetm.comp_type_attributes (type1, type2);
5070 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
5071 is ATTRIBUTE.
5073 Record such modified types already made so we don't make duplicates. */
5075 tree
5076 build_type_attribute_variant (tree ttype, tree attribute)
5078 return build_type_attribute_qual_variant (ttype, attribute,
5079 TYPE_QUALS (ttype));
5083 /* Reset the expression *EXPR_P, a size or position.
5085 ??? We could reset all non-constant sizes or positions. But it's cheap
5086 enough to not do so and refrain from adding workarounds to dwarf2out.c.
5088 We need to reset self-referential sizes or positions because they cannot
5089 be gimplified and thus can contain a CALL_EXPR after the gimplification
5090 is finished, which will run afoul of LTO streaming. And they need to be
5091 reset to something essentially dummy but not constant, so as to preserve
5092 the properties of the object they are attached to. */
5094 static inline void
5095 free_lang_data_in_one_sizepos (tree *expr_p)
5097 tree expr = *expr_p;
5098 if (CONTAINS_PLACEHOLDER_P (expr))
5099 *expr_p = build0 (PLACEHOLDER_EXPR, TREE_TYPE (expr));
5103 /* Reset all the fields in a binfo node BINFO. We only keep
5104 BINFO_VTABLE, which is used by gimple_fold_obj_type_ref. */
5106 static void
5107 free_lang_data_in_binfo (tree binfo)
5109 unsigned i;
5110 tree t;
5112 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
5114 BINFO_VIRTUALS (binfo) = NULL_TREE;
5115 BINFO_BASE_ACCESSES (binfo) = NULL;
5116 BINFO_INHERITANCE_CHAIN (binfo) = NULL_TREE;
5117 BINFO_SUBVTT_INDEX (binfo) = NULL_TREE;
5119 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (binfo), i, t)
5120 free_lang_data_in_binfo (t);
5124 /* Reset all language specific information still present in TYPE. */
5126 static void
5127 free_lang_data_in_type (tree type)
5129 gcc_assert (TYPE_P (type));
5131 /* Give the FE a chance to remove its own data first. */
5132 lang_hooks.free_lang_data (type);
5134 TREE_LANG_FLAG_0 (type) = 0;
5135 TREE_LANG_FLAG_1 (type) = 0;
5136 TREE_LANG_FLAG_2 (type) = 0;
5137 TREE_LANG_FLAG_3 (type) = 0;
5138 TREE_LANG_FLAG_4 (type) = 0;
5139 TREE_LANG_FLAG_5 (type) = 0;
5140 TREE_LANG_FLAG_6 (type) = 0;
5142 if (TREE_CODE (type) == FUNCTION_TYPE)
5144 /* Remove the const and volatile qualifiers from arguments. The
5145 C++ front end removes them, but the C front end does not,
5146 leading to false ODR violation errors when merging two
5147 instances of the same function signature compiled by
5148 different front ends. */
5149 tree p;
5151 for (p = TYPE_ARG_TYPES (type); p; p = TREE_CHAIN (p))
5153 tree arg_type = TREE_VALUE (p);
5155 if (TYPE_READONLY (arg_type) || TYPE_VOLATILE (arg_type))
5157 int quals = TYPE_QUALS (arg_type)
5158 & ~TYPE_QUAL_CONST
5159 & ~TYPE_QUAL_VOLATILE;
5160 TREE_VALUE (p) = build_qualified_type (arg_type, quals);
5161 free_lang_data_in_type (TREE_VALUE (p));
5163 /* C++ FE uses TREE_PURPOSE to store initial values. */
5164 TREE_PURPOSE (p) = NULL;
5166 /* Java uses TYPE_MINVAL for TYPE_ARGUMENT_SIGNATURE. */
5167 TYPE_MINVAL (type) = NULL;
5169 if (TREE_CODE (type) == METHOD_TYPE)
5171 tree p;
5173 for (p = TYPE_ARG_TYPES (type); p; p = TREE_CHAIN (p))
5175 /* C++ FE uses TREE_PURPOSE to store initial values. */
5176 TREE_PURPOSE (p) = NULL;
5178 /* Java uses TYPE_MINVAL for TYPE_ARGUMENT_SIGNATURE. */
5179 TYPE_MINVAL (type) = NULL;
5182 /* Remove members that are not actually FIELD_DECLs from the field
5183 list of an aggregate. These occur in C++. */
5184 if (RECORD_OR_UNION_TYPE_P (type))
5186 tree prev, member;
5188 /* Note that TYPE_FIELDS can be shared across distinct
5189 TREE_TYPEs. Therefore, if the first field of TYPE_FIELDS is
5190 to be removed, we cannot set its TREE_CHAIN to NULL.
5191 Otherwise, we would not be able to find all the other fields
5192 in the other instances of this TREE_TYPE.
5194 This was causing an ICE in testsuite/g++.dg/lto/20080915.C. */
5195 prev = NULL_TREE;
5196 member = TYPE_FIELDS (type);
5197 while (member)
5199 if (TREE_CODE (member) == FIELD_DECL
5200 || TREE_CODE (member) == TYPE_DECL)
5202 if (prev)
5203 TREE_CHAIN (prev) = member;
5204 else
5205 TYPE_FIELDS (type) = member;
5206 prev = member;
5209 member = TREE_CHAIN (member);
5212 if (prev)
5213 TREE_CHAIN (prev) = NULL_TREE;
5214 else
5215 TYPE_FIELDS (type) = NULL_TREE;
5217 /* FIXME: C FE uses TYPE_VFIELD to record C_TYPE_INCOMPLETE_VARS
5218 and danagle the pointer from time to time. */
5219 if (TYPE_VFIELD (type) && TREE_CODE (TYPE_VFIELD (type)) != FIELD_DECL)
5220 TYPE_VFIELD (type) = NULL_TREE;
5222 /* Remove TYPE_METHODS list. While it would be nice to keep it
5223 to enable ODR warnings about different method lists, doing so
5224 seems to impractically increase size of LTO data streamed.
5225 Keep the infrmation if TYPE_METHODS was non-NULL. This is used
5226 by function.c and pretty printers. */
5227 if (TYPE_METHODS (type))
5228 TYPE_METHODS (type) = error_mark_node;
5229 if (TYPE_BINFO (type))
5231 free_lang_data_in_binfo (TYPE_BINFO (type));
5232 /* We need to preserve link to bases and virtual table for all
5233 polymorphic types to make devirtualization machinery working.
5234 Debug output cares only about bases, but output also
5235 virtual table pointers so merging of -fdevirtualize and
5236 -fno-devirtualize units is easier. */
5237 if ((!BINFO_VTABLE (TYPE_BINFO (type))
5238 || !flag_devirtualize)
5239 && ((!BINFO_N_BASE_BINFOS (TYPE_BINFO (type))
5240 && !BINFO_VTABLE (TYPE_BINFO (type)))
5241 || debug_info_level != DINFO_LEVEL_NONE))
5242 TYPE_BINFO (type) = NULL;
5245 else
5247 /* For non-aggregate types, clear out the language slot (which
5248 overloads TYPE_BINFO). */
5249 TYPE_LANG_SLOT_1 (type) = NULL_TREE;
5251 if (INTEGRAL_TYPE_P (type)
5252 || SCALAR_FLOAT_TYPE_P (type)
5253 || FIXED_POINT_TYPE_P (type))
5255 free_lang_data_in_one_sizepos (&TYPE_MIN_VALUE (type));
5256 free_lang_data_in_one_sizepos (&TYPE_MAX_VALUE (type));
5260 free_lang_data_in_one_sizepos (&TYPE_SIZE (type));
5261 free_lang_data_in_one_sizepos (&TYPE_SIZE_UNIT (type));
5263 if (TYPE_CONTEXT (type)
5264 && TREE_CODE (TYPE_CONTEXT (type)) == BLOCK)
5266 tree ctx = TYPE_CONTEXT (type);
5269 ctx = BLOCK_SUPERCONTEXT (ctx);
5271 while (ctx && TREE_CODE (ctx) == BLOCK);
5272 TYPE_CONTEXT (type) = ctx;
5277 /* Return true if DECL may need an assembler name to be set. */
5279 static inline bool
5280 need_assembler_name_p (tree decl)
5282 /* We use DECL_ASSEMBLER_NAME to hold mangled type names for One Definition
5283 Rule merging. This makes type_odr_p to return true on those types during
5284 LTO and by comparing the mangled name, we can say what types are intended
5285 to be equivalent across compilation unit.
5287 We do not store names of type_in_anonymous_namespace_p.
5289 Record, union and enumeration type have linkage that allows use
5290 to check type_in_anonymous_namespace_p. We do not mangle compound types
5291 that always can be compared structurally.
5293 Similarly for builtin types, we compare properties of their main variant.
5294 A special case are integer types where mangling do make differences
5295 between char/signed char/unsigned char etc. Storing name for these makes
5296 e.g. -fno-signed-char/-fsigned-char mismatches to be handled well.
5297 See cp/mangle.c:write_builtin_type for details. */
5299 if (flag_lto_odr_type_mering
5300 && TREE_CODE (decl) == TYPE_DECL
5301 && DECL_NAME (decl)
5302 && decl == TYPE_NAME (TREE_TYPE (decl))
5303 && !TYPE_ARTIFICIAL (TREE_TYPE (decl))
5304 && (type_with_linkage_p (TREE_TYPE (decl))
5305 || TREE_CODE (TREE_TYPE (decl)) == INTEGER_TYPE)
5306 && !variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
5307 return !DECL_ASSEMBLER_NAME_SET_P (decl);
5308 /* Only FUNCTION_DECLs and VAR_DECLs are considered. */
5309 if (TREE_CODE (decl) != FUNCTION_DECL
5310 && TREE_CODE (decl) != VAR_DECL)
5311 return false;
5313 /* If DECL already has its assembler name set, it does not need a
5314 new one. */
5315 if (!HAS_DECL_ASSEMBLER_NAME_P (decl)
5316 || DECL_ASSEMBLER_NAME_SET_P (decl))
5317 return false;
5319 /* Abstract decls do not need an assembler name. */
5320 if (DECL_ABSTRACT_P (decl))
5321 return false;
5323 /* For VAR_DECLs, only static, public and external symbols need an
5324 assembler name. */
5325 if (TREE_CODE (decl) == VAR_DECL
5326 && !TREE_STATIC (decl)
5327 && !TREE_PUBLIC (decl)
5328 && !DECL_EXTERNAL (decl))
5329 return false;
5331 if (TREE_CODE (decl) == FUNCTION_DECL)
5333 /* Do not set assembler name on builtins. Allow RTL expansion to
5334 decide whether to expand inline or via a regular call. */
5335 if (DECL_BUILT_IN (decl)
5336 && DECL_BUILT_IN_CLASS (decl) != BUILT_IN_FRONTEND)
5337 return false;
5339 /* Functions represented in the callgraph need an assembler name. */
5340 if (cgraph_node::get (decl) != NULL)
5341 return true;
5343 /* Unused and not public functions don't need an assembler name. */
5344 if (!TREE_USED (decl) && !TREE_PUBLIC (decl))
5345 return false;
5348 return true;
5352 /* Reset all language specific information still present in symbol
5353 DECL. */
5355 static void
5356 free_lang_data_in_decl (tree decl)
5358 gcc_assert (DECL_P (decl));
5360 /* Give the FE a chance to remove its own data first. */
5361 lang_hooks.free_lang_data (decl);
5363 TREE_LANG_FLAG_0 (decl) = 0;
5364 TREE_LANG_FLAG_1 (decl) = 0;
5365 TREE_LANG_FLAG_2 (decl) = 0;
5366 TREE_LANG_FLAG_3 (decl) = 0;
5367 TREE_LANG_FLAG_4 (decl) = 0;
5368 TREE_LANG_FLAG_5 (decl) = 0;
5369 TREE_LANG_FLAG_6 (decl) = 0;
5371 free_lang_data_in_one_sizepos (&DECL_SIZE (decl));
5372 free_lang_data_in_one_sizepos (&DECL_SIZE_UNIT (decl));
5373 if (TREE_CODE (decl) == FIELD_DECL)
5375 free_lang_data_in_one_sizepos (&DECL_FIELD_OFFSET (decl));
5376 if (TREE_CODE (DECL_CONTEXT (decl)) == QUAL_UNION_TYPE)
5377 DECL_QUALIFIER (decl) = NULL_TREE;
5380 if (TREE_CODE (decl) == FUNCTION_DECL)
5382 struct cgraph_node *node;
5383 if (!(node = cgraph_node::get (decl))
5384 || (!node->definition && !node->clones))
5386 if (node)
5387 node->release_body ();
5388 else
5390 release_function_body (decl);
5391 DECL_ARGUMENTS (decl) = NULL;
5392 DECL_RESULT (decl) = NULL;
5393 DECL_INITIAL (decl) = error_mark_node;
5396 if (gimple_has_body_p (decl))
5398 tree t;
5400 /* If DECL has a gimple body, then the context for its
5401 arguments must be DECL. Otherwise, it doesn't really
5402 matter, as we will not be emitting any code for DECL. In
5403 general, there may be other instances of DECL created by
5404 the front end and since PARM_DECLs are generally shared,
5405 their DECL_CONTEXT changes as the replicas of DECL are
5406 created. The only time where DECL_CONTEXT is important
5407 is for the FUNCTION_DECLs that have a gimple body (since
5408 the PARM_DECL will be used in the function's body). */
5409 for (t = DECL_ARGUMENTS (decl); t; t = TREE_CHAIN (t))
5410 DECL_CONTEXT (t) = decl;
5411 if (!DECL_FUNCTION_SPECIFIC_TARGET (decl))
5412 DECL_FUNCTION_SPECIFIC_TARGET (decl)
5413 = target_option_default_node;
5414 if (!DECL_FUNCTION_SPECIFIC_OPTIMIZATION (decl))
5415 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (decl)
5416 = optimization_default_node;
5419 /* DECL_SAVED_TREE holds the GENERIC representation for DECL.
5420 At this point, it is not needed anymore. */
5421 DECL_SAVED_TREE (decl) = NULL_TREE;
5423 /* Clear the abstract origin if it refers to a method. Otherwise
5424 dwarf2out.c will ICE as we clear TYPE_METHODS and thus the
5425 origin will not be output correctly. */
5426 if (DECL_ABSTRACT_ORIGIN (decl)
5427 && DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))
5428 && RECORD_OR_UNION_TYPE_P
5429 (DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))))
5430 DECL_ABSTRACT_ORIGIN (decl) = NULL_TREE;
5432 /* Sometimes the C++ frontend doesn't manage to transform a temporary
5433 DECL_VINDEX referring to itself into a vtable slot number as it
5434 should. Happens with functions that are copied and then forgotten
5435 about. Just clear it, it won't matter anymore. */
5436 if (DECL_VINDEX (decl) && !tree_fits_shwi_p (DECL_VINDEX (decl)))
5437 DECL_VINDEX (decl) = NULL_TREE;
5439 else if (TREE_CODE (decl) == VAR_DECL)
5441 if ((DECL_EXTERNAL (decl)
5442 && (!TREE_STATIC (decl) || !TREE_READONLY (decl)))
5443 || (decl_function_context (decl) && !TREE_STATIC (decl)))
5444 DECL_INITIAL (decl) = NULL_TREE;
5446 else if (TREE_CODE (decl) == TYPE_DECL
5447 || TREE_CODE (decl) == FIELD_DECL)
5448 DECL_INITIAL (decl) = NULL_TREE;
5449 else if (TREE_CODE (decl) == TRANSLATION_UNIT_DECL
5450 && DECL_INITIAL (decl)
5451 && TREE_CODE (DECL_INITIAL (decl)) == BLOCK)
5453 /* Strip builtins from the translation-unit BLOCK. We still have targets
5454 without builtin_decl_explicit support and also builtins are shared
5455 nodes and thus we can't use TREE_CHAIN in multiple lists. */
5456 tree *nextp = &BLOCK_VARS (DECL_INITIAL (decl));
5457 while (*nextp)
5459 tree var = *nextp;
5460 if (TREE_CODE (var) == FUNCTION_DECL
5461 && DECL_BUILT_IN (var))
5462 *nextp = TREE_CHAIN (var);
5463 else
5464 nextp = &TREE_CHAIN (var);
5470 /* Data used when collecting DECLs and TYPEs for language data removal. */
5472 struct free_lang_data_d
5474 /* Worklist to avoid excessive recursion. */
5475 vec<tree> worklist;
5477 /* Set of traversed objects. Used to avoid duplicate visits. */
5478 hash_set<tree> *pset;
5480 /* Array of symbols to process with free_lang_data_in_decl. */
5481 vec<tree> decls;
5483 /* Array of types to process with free_lang_data_in_type. */
5484 vec<tree> types;
5488 /* Save all language fields needed to generate proper debug information
5489 for DECL. This saves most fields cleared out by free_lang_data_in_decl. */
5491 static void
5492 save_debug_info_for_decl (tree t)
5494 /*struct saved_debug_info_d *sdi;*/
5496 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && DECL_P (t));
5498 /* FIXME. Partial implementation for saving debug info removed. */
5502 /* Save all language fields needed to generate proper debug information
5503 for TYPE. This saves most fields cleared out by free_lang_data_in_type. */
5505 static void
5506 save_debug_info_for_type (tree t)
5508 /*struct saved_debug_info_d *sdi;*/
5510 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && TYPE_P (t));
5512 /* FIXME. Partial implementation for saving debug info removed. */
5516 /* Add type or decl T to one of the list of tree nodes that need their
5517 language data removed. The lists are held inside FLD. */
5519 static void
5520 add_tree_to_fld_list (tree t, struct free_lang_data_d *fld)
5522 if (DECL_P (t))
5524 fld->decls.safe_push (t);
5525 if (debug_info_level > DINFO_LEVEL_TERSE)
5526 save_debug_info_for_decl (t);
5528 else if (TYPE_P (t))
5530 fld->types.safe_push (t);
5531 if (debug_info_level > DINFO_LEVEL_TERSE)
5532 save_debug_info_for_type (t);
5534 else
5535 gcc_unreachable ();
5538 /* Push tree node T into FLD->WORKLIST. */
5540 static inline void
5541 fld_worklist_push (tree t, struct free_lang_data_d *fld)
5543 if (t && !is_lang_specific (t) && !fld->pset->contains (t))
5544 fld->worklist.safe_push ((t));
5548 /* Operand callback helper for free_lang_data_in_node. *TP is the
5549 subtree operand being considered. */
5551 static tree
5552 find_decls_types_r (tree *tp, int *ws, void *data)
5554 tree t = *tp;
5555 struct free_lang_data_d *fld = (struct free_lang_data_d *) data;
5557 if (TREE_CODE (t) == TREE_LIST)
5558 return NULL_TREE;
5560 /* Language specific nodes will be removed, so there is no need
5561 to gather anything under them. */
5562 if (is_lang_specific (t))
5564 *ws = 0;
5565 return NULL_TREE;
5568 if (DECL_P (t))
5570 /* Note that walk_tree does not traverse every possible field in
5571 decls, so we have to do our own traversals here. */
5572 add_tree_to_fld_list (t, fld);
5574 fld_worklist_push (DECL_NAME (t), fld);
5575 fld_worklist_push (DECL_CONTEXT (t), fld);
5576 fld_worklist_push (DECL_SIZE (t), fld);
5577 fld_worklist_push (DECL_SIZE_UNIT (t), fld);
5579 /* We are going to remove everything under DECL_INITIAL for
5580 TYPE_DECLs. No point walking them. */
5581 if (TREE_CODE (t) != TYPE_DECL)
5582 fld_worklist_push (DECL_INITIAL (t), fld);
5584 fld_worklist_push (DECL_ATTRIBUTES (t), fld);
5585 fld_worklist_push (DECL_ABSTRACT_ORIGIN (t), fld);
5587 if (TREE_CODE (t) == FUNCTION_DECL)
5589 fld_worklist_push (DECL_ARGUMENTS (t), fld);
5590 fld_worklist_push (DECL_RESULT (t), fld);
5592 else if (TREE_CODE (t) == TYPE_DECL)
5594 fld_worklist_push (DECL_ORIGINAL_TYPE (t), fld);
5596 else if (TREE_CODE (t) == FIELD_DECL)
5598 fld_worklist_push (DECL_FIELD_OFFSET (t), fld);
5599 fld_worklist_push (DECL_BIT_FIELD_TYPE (t), fld);
5600 fld_worklist_push (DECL_FIELD_BIT_OFFSET (t), fld);
5601 fld_worklist_push (DECL_FCONTEXT (t), fld);
5604 if ((TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
5605 && DECL_HAS_VALUE_EXPR_P (t))
5606 fld_worklist_push (DECL_VALUE_EXPR (t), fld);
5608 if (TREE_CODE (t) != FIELD_DECL
5609 && TREE_CODE (t) != TYPE_DECL)
5610 fld_worklist_push (TREE_CHAIN (t), fld);
5611 *ws = 0;
5613 else if (TYPE_P (t))
5615 /* Note that walk_tree does not traverse every possible field in
5616 types, so we have to do our own traversals here. */
5617 add_tree_to_fld_list (t, fld);
5619 if (!RECORD_OR_UNION_TYPE_P (t))
5620 fld_worklist_push (TYPE_CACHED_VALUES (t), fld);
5621 fld_worklist_push (TYPE_SIZE (t), fld);
5622 fld_worklist_push (TYPE_SIZE_UNIT (t), fld);
5623 fld_worklist_push (TYPE_ATTRIBUTES (t), fld);
5624 fld_worklist_push (TYPE_POINTER_TO (t), fld);
5625 fld_worklist_push (TYPE_REFERENCE_TO (t), fld);
5626 fld_worklist_push (TYPE_NAME (t), fld);
5627 /* Do not walk TYPE_NEXT_PTR_TO or TYPE_NEXT_REF_TO. We do not stream
5628 them and thus do not and want not to reach unused pointer types
5629 this way. */
5630 if (!POINTER_TYPE_P (t))
5631 fld_worklist_push (TYPE_MINVAL (t), fld);
5632 if (!RECORD_OR_UNION_TYPE_P (t))
5633 fld_worklist_push (TYPE_MAXVAL (t), fld);
5634 fld_worklist_push (TYPE_MAIN_VARIANT (t), fld);
5635 /* Do not walk TYPE_NEXT_VARIANT. We do not stream it and thus
5636 do not and want not to reach unused variants this way. */
5637 if (TYPE_CONTEXT (t))
5639 tree ctx = TYPE_CONTEXT (t);
5640 /* We adjust BLOCK TYPE_CONTEXTs to the innermost non-BLOCK one.
5641 So push that instead. */
5642 while (ctx && TREE_CODE (ctx) == BLOCK)
5643 ctx = BLOCK_SUPERCONTEXT (ctx);
5644 fld_worklist_push (ctx, fld);
5646 /* Do not walk TYPE_CANONICAL. We do not stream it and thus do not
5647 and want not to reach unused types this way. */
5649 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t))
5651 unsigned i;
5652 tree tem;
5653 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (TYPE_BINFO (t)), i, tem)
5654 fld_worklist_push (TREE_TYPE (tem), fld);
5655 tem = BINFO_VIRTUALS (TYPE_BINFO (t));
5656 if (tem
5657 /* The Java FE overloads BINFO_VIRTUALS for its own purpose. */
5658 && TREE_CODE (tem) == TREE_LIST)
5661 fld_worklist_push (TREE_VALUE (tem), fld);
5662 tem = TREE_CHAIN (tem);
5664 while (tem);
5666 if (RECORD_OR_UNION_TYPE_P (t))
5668 tree tem;
5669 /* Push all TYPE_FIELDS - there can be interleaving interesting
5670 and non-interesting things. */
5671 tem = TYPE_FIELDS (t);
5672 while (tem)
5674 if (TREE_CODE (tem) == FIELD_DECL
5675 || TREE_CODE (tem) == TYPE_DECL)
5676 fld_worklist_push (tem, fld);
5677 tem = TREE_CHAIN (tem);
5681 fld_worklist_push (TYPE_STUB_DECL (t), fld);
5682 *ws = 0;
5684 else if (TREE_CODE (t) == BLOCK)
5686 tree tem;
5687 for (tem = BLOCK_VARS (t); tem; tem = TREE_CHAIN (tem))
5688 fld_worklist_push (tem, fld);
5689 for (tem = BLOCK_SUBBLOCKS (t); tem; tem = BLOCK_CHAIN (tem))
5690 fld_worklist_push (tem, fld);
5691 fld_worklist_push (BLOCK_ABSTRACT_ORIGIN (t), fld);
5694 if (TREE_CODE (t) != IDENTIFIER_NODE
5695 && CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED))
5696 fld_worklist_push (TREE_TYPE (t), fld);
5698 return NULL_TREE;
5702 /* Find decls and types in T. */
5704 static void
5705 find_decls_types (tree t, struct free_lang_data_d *fld)
5707 while (1)
5709 if (!fld->pset->contains (t))
5710 walk_tree (&t, find_decls_types_r, fld, fld->pset);
5711 if (fld->worklist.is_empty ())
5712 break;
5713 t = fld->worklist.pop ();
5717 /* Translate all the types in LIST with the corresponding runtime
5718 types. */
5720 static tree
5721 get_eh_types_for_runtime (tree list)
5723 tree head, prev;
5725 if (list == NULL_TREE)
5726 return NULL_TREE;
5728 head = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5729 prev = head;
5730 list = TREE_CHAIN (list);
5731 while (list)
5733 tree n = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5734 TREE_CHAIN (prev) = n;
5735 prev = TREE_CHAIN (prev);
5736 list = TREE_CHAIN (list);
5739 return head;
5743 /* Find decls and types referenced in EH region R and store them in
5744 FLD->DECLS and FLD->TYPES. */
5746 static void
5747 find_decls_types_in_eh_region (eh_region r, struct free_lang_data_d *fld)
5749 switch (r->type)
5751 case ERT_CLEANUP:
5752 break;
5754 case ERT_TRY:
5756 eh_catch c;
5758 /* The types referenced in each catch must first be changed to the
5759 EH types used at runtime. This removes references to FE types
5760 in the region. */
5761 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
5763 c->type_list = get_eh_types_for_runtime (c->type_list);
5764 walk_tree (&c->type_list, find_decls_types_r, fld, fld->pset);
5767 break;
5769 case ERT_ALLOWED_EXCEPTIONS:
5770 r->u.allowed.type_list
5771 = get_eh_types_for_runtime (r->u.allowed.type_list);
5772 walk_tree (&r->u.allowed.type_list, find_decls_types_r, fld, fld->pset);
5773 break;
5775 case ERT_MUST_NOT_THROW:
5776 walk_tree (&r->u.must_not_throw.failure_decl,
5777 find_decls_types_r, fld, fld->pset);
5778 break;
5783 /* Find decls and types referenced in cgraph node N and store them in
5784 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5785 look for *every* kind of DECL and TYPE node reachable from N,
5786 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5787 NAMESPACE_DECLs, etc). */
5789 static void
5790 find_decls_types_in_node (struct cgraph_node *n, struct free_lang_data_d *fld)
5792 basic_block bb;
5793 struct function *fn;
5794 unsigned ix;
5795 tree t;
5797 find_decls_types (n->decl, fld);
5799 if (!gimple_has_body_p (n->decl))
5800 return;
5802 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
5804 fn = DECL_STRUCT_FUNCTION (n->decl);
5806 /* Traverse locals. */
5807 FOR_EACH_LOCAL_DECL (fn, ix, t)
5808 find_decls_types (t, fld);
5810 /* Traverse EH regions in FN. */
5812 eh_region r;
5813 FOR_ALL_EH_REGION_FN (r, fn)
5814 find_decls_types_in_eh_region (r, fld);
5817 /* Traverse every statement in FN. */
5818 FOR_EACH_BB_FN (bb, fn)
5820 gphi_iterator psi;
5821 gimple_stmt_iterator si;
5822 unsigned i;
5824 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
5826 gphi *phi = psi.phi ();
5828 for (i = 0; i < gimple_phi_num_args (phi); i++)
5830 tree *arg_p = gimple_phi_arg_def_ptr (phi, i);
5831 find_decls_types (*arg_p, fld);
5835 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
5837 gimple *stmt = gsi_stmt (si);
5839 if (is_gimple_call (stmt))
5840 find_decls_types (gimple_call_fntype (stmt), fld);
5842 for (i = 0; i < gimple_num_ops (stmt); i++)
5844 tree arg = gimple_op (stmt, i);
5845 find_decls_types (arg, fld);
5852 /* Find decls and types referenced in varpool node N and store them in
5853 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5854 look for *every* kind of DECL and TYPE node reachable from N,
5855 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5856 NAMESPACE_DECLs, etc). */
5858 static void
5859 find_decls_types_in_var (varpool_node *v, struct free_lang_data_d *fld)
5861 find_decls_types (v->decl, fld);
5864 /* If T needs an assembler name, have one created for it. */
5866 void
5867 assign_assembler_name_if_neeeded (tree t)
5869 if (need_assembler_name_p (t))
5871 /* When setting DECL_ASSEMBLER_NAME, the C++ mangler may emit
5872 diagnostics that use input_location to show locus
5873 information. The problem here is that, at this point,
5874 input_location is generally anchored to the end of the file
5875 (since the parser is long gone), so we don't have a good
5876 position to pin it to.
5878 To alleviate this problem, this uses the location of T's
5879 declaration. Examples of this are
5880 testsuite/g++.dg/template/cond2.C and
5881 testsuite/g++.dg/template/pr35240.C. */
5882 location_t saved_location = input_location;
5883 input_location = DECL_SOURCE_LOCATION (t);
5885 decl_assembler_name (t);
5887 input_location = saved_location;
5892 /* Free language specific information for every operand and expression
5893 in every node of the call graph. This process operates in three stages:
5895 1- Every callgraph node and varpool node is traversed looking for
5896 decls and types embedded in them. This is a more exhaustive
5897 search than that done by find_referenced_vars, because it will
5898 also collect individual fields, decls embedded in types, etc.
5900 2- All the decls found are sent to free_lang_data_in_decl.
5902 3- All the types found are sent to free_lang_data_in_type.
5904 The ordering between decls and types is important because
5905 free_lang_data_in_decl sets assembler names, which includes
5906 mangling. So types cannot be freed up until assembler names have
5907 been set up. */
5909 static void
5910 free_lang_data_in_cgraph (void)
5912 struct cgraph_node *n;
5913 varpool_node *v;
5914 struct free_lang_data_d fld;
5915 tree t;
5916 unsigned i;
5917 alias_pair *p;
5919 /* Initialize sets and arrays to store referenced decls and types. */
5920 fld.pset = new hash_set<tree>;
5921 fld.worklist.create (0);
5922 fld.decls.create (100);
5923 fld.types.create (100);
5925 /* Find decls and types in the body of every function in the callgraph. */
5926 FOR_EACH_FUNCTION (n)
5927 find_decls_types_in_node (n, &fld);
5929 FOR_EACH_VEC_SAFE_ELT (alias_pairs, i, p)
5930 find_decls_types (p->decl, &fld);
5932 /* Find decls and types in every varpool symbol. */
5933 FOR_EACH_VARIABLE (v)
5934 find_decls_types_in_var (v, &fld);
5936 /* Set the assembler name on every decl found. We need to do this
5937 now because free_lang_data_in_decl will invalidate data needed
5938 for mangling. This breaks mangling on interdependent decls. */
5939 FOR_EACH_VEC_ELT (fld.decls, i, t)
5940 assign_assembler_name_if_neeeded (t);
5942 /* Traverse every decl found freeing its language data. */
5943 FOR_EACH_VEC_ELT (fld.decls, i, t)
5944 free_lang_data_in_decl (t);
5946 /* Traverse every type found freeing its language data. */
5947 FOR_EACH_VEC_ELT (fld.types, i, t)
5948 free_lang_data_in_type (t);
5949 if (flag_checking)
5951 FOR_EACH_VEC_ELT (fld.types, i, t)
5952 verify_type (t);
5955 delete fld.pset;
5956 fld.worklist.release ();
5957 fld.decls.release ();
5958 fld.types.release ();
5962 /* Free resources that are used by FE but are not needed once they are done. */
5964 static unsigned
5965 free_lang_data (void)
5967 unsigned i;
5969 /* If we are the LTO frontend we have freed lang-specific data already. */
5970 if (in_lto_p
5971 || (!flag_generate_lto && !flag_generate_offload))
5972 return 0;
5974 /* Allocate and assign alias sets to the standard integer types
5975 while the slots are still in the way the frontends generated them. */
5976 for (i = 0; i < itk_none; ++i)
5977 if (integer_types[i])
5978 TYPE_ALIAS_SET (integer_types[i]) = get_alias_set (integer_types[i]);
5980 /* Traverse the IL resetting language specific information for
5981 operands, expressions, etc. */
5982 free_lang_data_in_cgraph ();
5984 /* Create gimple variants for common types. */
5985 ptrdiff_type_node = integer_type_node;
5986 fileptr_type_node = ptr_type_node;
5988 /* Reset some langhooks. Do not reset types_compatible_p, it may
5989 still be used indirectly via the get_alias_set langhook. */
5990 lang_hooks.dwarf_name = lhd_dwarf_name;
5991 lang_hooks.decl_printable_name = gimple_decl_printable_name;
5992 lang_hooks.gimplify_expr = lhd_gimplify_expr;
5994 /* We do not want the default decl_assembler_name implementation,
5995 rather if we have fixed everything we want a wrapper around it
5996 asserting that all non-local symbols already got their assembler
5997 name and only produce assembler names for local symbols. Or rather
5998 make sure we never call decl_assembler_name on local symbols and
5999 devise a separate, middle-end private scheme for it. */
6001 /* Reset diagnostic machinery. */
6002 tree_diagnostics_defaults (global_dc);
6004 return 0;
6008 namespace {
6010 const pass_data pass_data_ipa_free_lang_data =
6012 SIMPLE_IPA_PASS, /* type */
6013 "*free_lang_data", /* name */
6014 OPTGROUP_NONE, /* optinfo_flags */
6015 TV_IPA_FREE_LANG_DATA, /* tv_id */
6016 0, /* properties_required */
6017 0, /* properties_provided */
6018 0, /* properties_destroyed */
6019 0, /* todo_flags_start */
6020 0, /* todo_flags_finish */
6023 class pass_ipa_free_lang_data : public simple_ipa_opt_pass
6025 public:
6026 pass_ipa_free_lang_data (gcc::context *ctxt)
6027 : simple_ipa_opt_pass (pass_data_ipa_free_lang_data, ctxt)
6030 /* opt_pass methods: */
6031 virtual unsigned int execute (function *) { return free_lang_data (); }
6033 }; // class pass_ipa_free_lang_data
6035 } // anon namespace
6037 simple_ipa_opt_pass *
6038 make_pass_ipa_free_lang_data (gcc::context *ctxt)
6040 return new pass_ipa_free_lang_data (ctxt);
6043 /* The backbone of is_attribute_p(). ATTR_LEN is the string length of
6044 ATTR_NAME. Also used internally by remove_attribute(). */
6045 bool
6046 private_is_attribute_p (const char *attr_name, size_t attr_len, const_tree ident)
6048 size_t ident_len = IDENTIFIER_LENGTH (ident);
6050 if (ident_len == attr_len)
6052 if (strcmp (attr_name, IDENTIFIER_POINTER (ident)) == 0)
6053 return true;
6055 else if (ident_len == attr_len + 4)
6057 /* There is the possibility that ATTR is 'text' and IDENT is
6058 '__text__'. */
6059 const char *p = IDENTIFIER_POINTER (ident);
6060 if (p[0] == '_' && p[1] == '_'
6061 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
6062 && strncmp (attr_name, p + 2, attr_len) == 0)
6063 return true;
6066 return false;
6069 /* The backbone of lookup_attribute(). ATTR_LEN is the string length
6070 of ATTR_NAME, and LIST is not NULL_TREE. */
6071 tree
6072 private_lookup_attribute (const char *attr_name, size_t attr_len, tree list)
6074 while (list)
6076 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
6078 if (ident_len == attr_len)
6080 if (!strcmp (attr_name,
6081 IDENTIFIER_POINTER (get_attribute_name (list))))
6082 break;
6084 /* TODO: If we made sure that attributes were stored in the
6085 canonical form without '__...__' (ie, as in 'text' as opposed
6086 to '__text__') then we could avoid the following case. */
6087 else if (ident_len == attr_len + 4)
6089 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
6090 if (p[0] == '_' && p[1] == '_'
6091 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
6092 && strncmp (attr_name, p + 2, attr_len) == 0)
6093 break;
6095 list = TREE_CHAIN (list);
6098 return list;
6101 /* Given an attribute name ATTR_NAME and a list of attributes LIST,
6102 return a pointer to the attribute's list first element if the attribute
6103 starts with ATTR_NAME. ATTR_NAME must be in the form 'text' (not
6104 '__text__'). */
6106 tree
6107 private_lookup_attribute_by_prefix (const char *attr_name, size_t attr_len,
6108 tree list)
6110 while (list)
6112 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
6114 if (attr_len > ident_len)
6116 list = TREE_CHAIN (list);
6117 continue;
6120 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
6122 if (strncmp (attr_name, p, attr_len) == 0)
6123 break;
6125 /* TODO: If we made sure that attributes were stored in the
6126 canonical form without '__...__' (ie, as in 'text' as opposed
6127 to '__text__') then we could avoid the following case. */
6128 if (p[0] == '_' && p[1] == '_' &&
6129 strncmp (attr_name, p + 2, attr_len) == 0)
6130 break;
6132 list = TREE_CHAIN (list);
6135 return list;
6139 /* A variant of lookup_attribute() that can be used with an identifier
6140 as the first argument, and where the identifier can be either
6141 'text' or '__text__'.
6143 Given an attribute ATTR_IDENTIFIER, and a list of attributes LIST,
6144 return a pointer to the attribute's list element if the attribute
6145 is part of the list, or NULL_TREE if not found. If the attribute
6146 appears more than once, this only returns the first occurrence; the
6147 TREE_CHAIN of the return value should be passed back in if further
6148 occurrences are wanted. ATTR_IDENTIFIER must be an identifier but
6149 can be in the form 'text' or '__text__'. */
6150 static tree
6151 lookup_ident_attribute (tree attr_identifier, tree list)
6153 gcc_checking_assert (TREE_CODE (attr_identifier) == IDENTIFIER_NODE);
6155 while (list)
6157 gcc_checking_assert (TREE_CODE (get_attribute_name (list))
6158 == IDENTIFIER_NODE);
6160 if (cmp_attrib_identifiers (attr_identifier,
6161 get_attribute_name (list)))
6162 /* Found it. */
6163 break;
6164 list = TREE_CHAIN (list);
6167 return list;
6170 /* Remove any instances of attribute ATTR_NAME in LIST and return the
6171 modified list. */
6173 tree
6174 remove_attribute (const char *attr_name, tree list)
6176 tree *p;
6177 size_t attr_len = strlen (attr_name);
6179 gcc_checking_assert (attr_name[0] != '_');
6181 for (p = &list; *p; )
6183 tree l = *p;
6184 /* TODO: If we were storing attributes in normalized form, here
6185 we could use a simple strcmp(). */
6186 if (private_is_attribute_p (attr_name, attr_len, get_attribute_name (l)))
6187 *p = TREE_CHAIN (l);
6188 else
6189 p = &TREE_CHAIN (l);
6192 return list;
6195 /* Return an attribute list that is the union of a1 and a2. */
6197 tree
6198 merge_attributes (tree a1, tree a2)
6200 tree attributes;
6202 /* Either one unset? Take the set one. */
6204 if ((attributes = a1) == 0)
6205 attributes = a2;
6207 /* One that completely contains the other? Take it. */
6209 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
6211 if (attribute_list_contained (a2, a1))
6212 attributes = a2;
6213 else
6215 /* Pick the longest list, and hang on the other list. */
6217 if (list_length (a1) < list_length (a2))
6218 attributes = a2, a2 = a1;
6220 for (; a2 != 0; a2 = TREE_CHAIN (a2))
6222 tree a;
6223 for (a = lookup_ident_attribute (get_attribute_name (a2),
6224 attributes);
6225 a != NULL_TREE && !attribute_value_equal (a, a2);
6226 a = lookup_ident_attribute (get_attribute_name (a2),
6227 TREE_CHAIN (a)))
6229 if (a == NULL_TREE)
6231 a1 = copy_node (a2);
6232 TREE_CHAIN (a1) = attributes;
6233 attributes = a1;
6238 return attributes;
6241 /* Given types T1 and T2, merge their attributes and return
6242 the result. */
6244 tree
6245 merge_type_attributes (tree t1, tree t2)
6247 return merge_attributes (TYPE_ATTRIBUTES (t1),
6248 TYPE_ATTRIBUTES (t2));
6251 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
6252 the result. */
6254 tree
6255 merge_decl_attributes (tree olddecl, tree newdecl)
6257 return merge_attributes (DECL_ATTRIBUTES (olddecl),
6258 DECL_ATTRIBUTES (newdecl));
6261 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
6263 /* Specialization of merge_decl_attributes for various Windows targets.
6265 This handles the following situation:
6267 __declspec (dllimport) int foo;
6268 int foo;
6270 The second instance of `foo' nullifies the dllimport. */
6272 tree
6273 merge_dllimport_decl_attributes (tree old, tree new_tree)
6275 tree a;
6276 int delete_dllimport_p = 1;
6278 /* What we need to do here is remove from `old' dllimport if it doesn't
6279 appear in `new'. dllimport behaves like extern: if a declaration is
6280 marked dllimport and a definition appears later, then the object
6281 is not dllimport'd. We also remove a `new' dllimport if the old list
6282 contains dllexport: dllexport always overrides dllimport, regardless
6283 of the order of declaration. */
6284 if (!VAR_OR_FUNCTION_DECL_P (new_tree))
6285 delete_dllimport_p = 0;
6286 else if (DECL_DLLIMPORT_P (new_tree)
6287 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old)))
6289 DECL_DLLIMPORT_P (new_tree) = 0;
6290 warning (OPT_Wattributes, "%q+D already declared with dllexport attribute: "
6291 "dllimport ignored", new_tree);
6293 else if (DECL_DLLIMPORT_P (old) && !DECL_DLLIMPORT_P (new_tree))
6295 /* Warn about overriding a symbol that has already been used, e.g.:
6296 extern int __attribute__ ((dllimport)) foo;
6297 int* bar () {return &foo;}
6298 int foo;
6300 if (TREE_USED (old))
6302 warning (0, "%q+D redeclared without dllimport attribute "
6303 "after being referenced with dll linkage", new_tree);
6304 /* If we have used a variable's address with dllimport linkage,
6305 keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
6306 decl may already have had TREE_CONSTANT computed.
6307 We still remove the attribute so that assembler code refers
6308 to '&foo rather than '_imp__foo'. */
6309 if (TREE_CODE (old) == VAR_DECL && TREE_ADDRESSABLE (old))
6310 DECL_DLLIMPORT_P (new_tree) = 1;
6313 /* Let an inline definition silently override the external reference,
6314 but otherwise warn about attribute inconsistency. */
6315 else if (TREE_CODE (new_tree) == VAR_DECL
6316 || !DECL_DECLARED_INLINE_P (new_tree))
6317 warning (OPT_Wattributes, "%q+D redeclared without dllimport attribute: "
6318 "previous dllimport ignored", new_tree);
6320 else
6321 delete_dllimport_p = 0;
6323 a = merge_attributes (DECL_ATTRIBUTES (old), DECL_ATTRIBUTES (new_tree));
6325 if (delete_dllimport_p)
6326 a = remove_attribute ("dllimport", a);
6328 return a;
6331 /* Handle a "dllimport" or "dllexport" attribute; arguments as in
6332 struct attribute_spec.handler. */
6334 tree
6335 handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
6336 bool *no_add_attrs)
6338 tree node = *pnode;
6339 bool is_dllimport;
6341 /* These attributes may apply to structure and union types being created,
6342 but otherwise should pass to the declaration involved. */
6343 if (!DECL_P (node))
6345 if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
6346 | (int) ATTR_FLAG_ARRAY_NEXT))
6348 *no_add_attrs = true;
6349 return tree_cons (name, args, NULL_TREE);
6351 if (TREE_CODE (node) == RECORD_TYPE
6352 || TREE_CODE (node) == UNION_TYPE)
6354 node = TYPE_NAME (node);
6355 if (!node)
6356 return NULL_TREE;
6358 else
6360 warning (OPT_Wattributes, "%qE attribute ignored",
6361 name);
6362 *no_add_attrs = true;
6363 return NULL_TREE;
6367 if (TREE_CODE (node) != FUNCTION_DECL
6368 && TREE_CODE (node) != VAR_DECL
6369 && TREE_CODE (node) != TYPE_DECL)
6371 *no_add_attrs = true;
6372 warning (OPT_Wattributes, "%qE attribute ignored",
6373 name);
6374 return NULL_TREE;
6377 if (TREE_CODE (node) == TYPE_DECL
6378 && TREE_CODE (TREE_TYPE (node)) != RECORD_TYPE
6379 && TREE_CODE (TREE_TYPE (node)) != UNION_TYPE)
6381 *no_add_attrs = true;
6382 warning (OPT_Wattributes, "%qE attribute ignored",
6383 name);
6384 return NULL_TREE;
6387 is_dllimport = is_attribute_p ("dllimport", name);
6389 /* Report error on dllimport ambiguities seen now before they cause
6390 any damage. */
6391 if (is_dllimport)
6393 /* Honor any target-specific overrides. */
6394 if (!targetm.valid_dllimport_attribute_p (node))
6395 *no_add_attrs = true;
6397 else if (TREE_CODE (node) == FUNCTION_DECL
6398 && DECL_DECLARED_INLINE_P (node))
6400 warning (OPT_Wattributes, "inline function %q+D declared as "
6401 " dllimport: attribute ignored", node);
6402 *no_add_attrs = true;
6404 /* Like MS, treat definition of dllimported variables and
6405 non-inlined functions on declaration as syntax errors. */
6406 else if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node))
6408 error ("function %q+D definition is marked dllimport", node);
6409 *no_add_attrs = true;
6412 else if (TREE_CODE (node) == VAR_DECL)
6414 if (DECL_INITIAL (node))
6416 error ("variable %q+D definition is marked dllimport",
6417 node);
6418 *no_add_attrs = true;
6421 /* `extern' needn't be specified with dllimport.
6422 Specify `extern' now and hope for the best. Sigh. */
6423 DECL_EXTERNAL (node) = 1;
6424 /* Also, implicitly give dllimport'd variables declared within
6425 a function global scope, unless declared static. */
6426 if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
6427 TREE_PUBLIC (node) = 1;
6430 if (*no_add_attrs == false)
6431 DECL_DLLIMPORT_P (node) = 1;
6433 else if (TREE_CODE (node) == FUNCTION_DECL
6434 && DECL_DECLARED_INLINE_P (node)
6435 && flag_keep_inline_dllexport)
6436 /* An exported function, even if inline, must be emitted. */
6437 DECL_EXTERNAL (node) = 0;
6439 /* Report error if symbol is not accessible at global scope. */
6440 if (!TREE_PUBLIC (node)
6441 && (TREE_CODE (node) == VAR_DECL
6442 || TREE_CODE (node) == FUNCTION_DECL))
6444 error ("external linkage required for symbol %q+D because of "
6445 "%qE attribute", node, name);
6446 *no_add_attrs = true;
6449 /* A dllexport'd entity must have default visibility so that other
6450 program units (shared libraries or the main executable) can see
6451 it. A dllimport'd entity must have default visibility so that
6452 the linker knows that undefined references within this program
6453 unit can be resolved by the dynamic linker. */
6454 if (!*no_add_attrs)
6456 if (DECL_VISIBILITY_SPECIFIED (node)
6457 && DECL_VISIBILITY (node) != VISIBILITY_DEFAULT)
6458 error ("%qE implies default visibility, but %qD has already "
6459 "been declared with a different visibility",
6460 name, node);
6461 DECL_VISIBILITY (node) = VISIBILITY_DEFAULT;
6462 DECL_VISIBILITY_SPECIFIED (node) = 1;
6465 return NULL_TREE;
6468 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
6470 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
6471 of the various TYPE_QUAL values. */
6473 static void
6474 set_type_quals (tree type, int type_quals)
6476 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
6477 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
6478 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
6479 TYPE_ATOMIC (type) = (type_quals & TYPE_QUAL_ATOMIC) != 0;
6480 TYPE_ADDR_SPACE (type) = DECODE_QUAL_ADDR_SPACE (type_quals);
6483 /* Returns true iff unqualified CAND and BASE are equivalent. */
6485 bool
6486 check_base_type (const_tree cand, const_tree base)
6488 return (TYPE_NAME (cand) == TYPE_NAME (base)
6489 /* Apparently this is needed for Objective-C. */
6490 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
6491 /* Check alignment. */
6492 && TYPE_ALIGN (cand) == TYPE_ALIGN (base)
6493 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
6494 TYPE_ATTRIBUTES (base)));
6497 /* Returns true iff CAND is equivalent to BASE with TYPE_QUALS. */
6499 bool
6500 check_qualified_type (const_tree cand, const_tree base, int type_quals)
6502 return (TYPE_QUALS (cand) == type_quals
6503 && check_base_type (cand, base));
6506 /* Returns true iff CAND is equivalent to BASE with ALIGN. */
6508 static bool
6509 check_aligned_type (const_tree cand, const_tree base, unsigned int align)
6511 return (TYPE_QUALS (cand) == TYPE_QUALS (base)
6512 && TYPE_NAME (cand) == TYPE_NAME (base)
6513 /* Apparently this is needed for Objective-C. */
6514 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
6515 /* Check alignment. */
6516 && TYPE_ALIGN (cand) == align
6517 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
6518 TYPE_ATTRIBUTES (base)));
6521 /* This function checks to see if TYPE matches the size one of the built-in
6522 atomic types, and returns that core atomic type. */
6524 static tree
6525 find_atomic_core_type (tree type)
6527 tree base_atomic_type;
6529 /* Only handle complete types. */
6530 if (TYPE_SIZE (type) == NULL_TREE)
6531 return NULL_TREE;
6533 HOST_WIDE_INT type_size = tree_to_uhwi (TYPE_SIZE (type));
6534 switch (type_size)
6536 case 8:
6537 base_atomic_type = atomicQI_type_node;
6538 break;
6540 case 16:
6541 base_atomic_type = atomicHI_type_node;
6542 break;
6544 case 32:
6545 base_atomic_type = atomicSI_type_node;
6546 break;
6548 case 64:
6549 base_atomic_type = atomicDI_type_node;
6550 break;
6552 case 128:
6553 base_atomic_type = atomicTI_type_node;
6554 break;
6556 default:
6557 base_atomic_type = NULL_TREE;
6560 return base_atomic_type;
6563 /* Return a version of the TYPE, qualified as indicated by the
6564 TYPE_QUALS, if one exists. If no qualified version exists yet,
6565 return NULL_TREE. */
6567 tree
6568 get_qualified_type (tree type, int type_quals)
6570 tree t;
6572 if (TYPE_QUALS (type) == type_quals)
6573 return type;
6575 /* Search the chain of variants to see if there is already one there just
6576 like the one we need to have. If so, use that existing one. We must
6577 preserve the TYPE_NAME, since there is code that depends on this. */
6578 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6579 if (check_qualified_type (t, type, type_quals))
6580 return t;
6582 return NULL_TREE;
6585 /* Like get_qualified_type, but creates the type if it does not
6586 exist. This function never returns NULL_TREE. */
6588 tree
6589 build_qualified_type (tree type, int type_quals)
6591 tree t;
6593 /* See if we already have the appropriate qualified variant. */
6594 t = get_qualified_type (type, type_quals);
6596 /* If not, build it. */
6597 if (!t)
6599 t = build_variant_type_copy (type);
6600 set_type_quals (t, type_quals);
6602 if (((type_quals & TYPE_QUAL_ATOMIC) == TYPE_QUAL_ATOMIC))
6604 /* See if this object can map to a basic atomic type. */
6605 tree atomic_type = find_atomic_core_type (type);
6606 if (atomic_type)
6608 /* Ensure the alignment of this type is compatible with
6609 the required alignment of the atomic type. */
6610 if (TYPE_ALIGN (atomic_type) > TYPE_ALIGN (t))
6611 TYPE_ALIGN (t) = TYPE_ALIGN (atomic_type);
6615 if (TYPE_STRUCTURAL_EQUALITY_P (type))
6616 /* Propagate structural equality. */
6617 SET_TYPE_STRUCTURAL_EQUALITY (t);
6618 else if (TYPE_CANONICAL (type) != type)
6619 /* Build the underlying canonical type, since it is different
6620 from TYPE. */
6622 tree c = build_qualified_type (TYPE_CANONICAL (type), type_quals);
6623 TYPE_CANONICAL (t) = TYPE_CANONICAL (c);
6625 else
6626 /* T is its own canonical type. */
6627 TYPE_CANONICAL (t) = t;
6631 return t;
6634 /* Create a variant of type T with alignment ALIGN. */
6636 tree
6637 build_aligned_type (tree type, unsigned int align)
6639 tree t;
6641 if (TYPE_PACKED (type)
6642 || TYPE_ALIGN (type) == align)
6643 return type;
6645 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6646 if (check_aligned_type (t, type, align))
6647 return t;
6649 t = build_variant_type_copy (type);
6650 TYPE_ALIGN (t) = align;
6652 return t;
6655 /* Create a new distinct copy of TYPE. The new type is made its own
6656 MAIN_VARIANT. If TYPE requires structural equality checks, the
6657 resulting type requires structural equality checks; otherwise, its
6658 TYPE_CANONICAL points to itself. */
6660 tree
6661 build_distinct_type_copy (tree type)
6663 tree t = copy_node (type);
6665 TYPE_POINTER_TO (t) = 0;
6666 TYPE_REFERENCE_TO (t) = 0;
6668 /* Set the canonical type either to a new equivalence class, or
6669 propagate the need for structural equality checks. */
6670 if (TYPE_STRUCTURAL_EQUALITY_P (type))
6671 SET_TYPE_STRUCTURAL_EQUALITY (t);
6672 else
6673 TYPE_CANONICAL (t) = t;
6675 /* Make it its own variant. */
6676 TYPE_MAIN_VARIANT (t) = t;
6677 TYPE_NEXT_VARIANT (t) = 0;
6679 /* We do not record methods in type copies nor variants
6680 so we do not need to keep them up to date when new method
6681 is inserted. */
6682 if (RECORD_OR_UNION_TYPE_P (t))
6683 TYPE_METHODS (t) = NULL_TREE;
6685 /* Note that it is now possible for TYPE_MIN_VALUE to be a value
6686 whose TREE_TYPE is not t. This can also happen in the Ada
6687 frontend when using subtypes. */
6689 return t;
6692 /* Create a new variant of TYPE, equivalent but distinct. This is so
6693 the caller can modify it. TYPE_CANONICAL for the return type will
6694 be equivalent to TYPE_CANONICAL of TYPE, indicating that the types
6695 are considered equal by the language itself (or that both types
6696 require structural equality checks). */
6698 tree
6699 build_variant_type_copy (tree type)
6701 tree t, m = TYPE_MAIN_VARIANT (type);
6703 t = build_distinct_type_copy (type);
6705 /* Since we're building a variant, assume that it is a non-semantic
6706 variant. This also propagates TYPE_STRUCTURAL_EQUALITY_P. */
6707 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
6709 /* Add the new type to the chain of variants of TYPE. */
6710 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
6711 TYPE_NEXT_VARIANT (m) = t;
6712 TYPE_MAIN_VARIANT (t) = m;
6714 return t;
6717 /* Return true if the from tree in both tree maps are equal. */
6720 tree_map_base_eq (const void *va, const void *vb)
6722 const struct tree_map_base *const a = (const struct tree_map_base *) va,
6723 *const b = (const struct tree_map_base *) vb;
6724 return (a->from == b->from);
6727 /* Hash a from tree in a tree_base_map. */
6729 unsigned int
6730 tree_map_base_hash (const void *item)
6732 return htab_hash_pointer (((const struct tree_map_base *)item)->from);
6735 /* Return true if this tree map structure is marked for garbage collection
6736 purposes. We simply return true if the from tree is marked, so that this
6737 structure goes away when the from tree goes away. */
6740 tree_map_base_marked_p (const void *p)
6742 return ggc_marked_p (((const struct tree_map_base *) p)->from);
6745 /* Hash a from tree in a tree_map. */
6747 unsigned int
6748 tree_map_hash (const void *item)
6750 return (((const struct tree_map *) item)->hash);
6753 /* Hash a from tree in a tree_decl_map. */
6755 unsigned int
6756 tree_decl_map_hash (const void *item)
6758 return DECL_UID (((const struct tree_decl_map *) item)->base.from);
6761 /* Return the initialization priority for DECL. */
6763 priority_type
6764 decl_init_priority_lookup (tree decl)
6766 symtab_node *snode = symtab_node::get (decl);
6768 if (!snode)
6769 return DEFAULT_INIT_PRIORITY;
6770 return
6771 snode->get_init_priority ();
6774 /* Return the finalization priority for DECL. */
6776 priority_type
6777 decl_fini_priority_lookup (tree decl)
6779 cgraph_node *node = cgraph_node::get (decl);
6781 if (!node)
6782 return DEFAULT_INIT_PRIORITY;
6783 return
6784 node->get_fini_priority ();
6787 /* Set the initialization priority for DECL to PRIORITY. */
6789 void
6790 decl_init_priority_insert (tree decl, priority_type priority)
6792 struct symtab_node *snode;
6794 if (priority == DEFAULT_INIT_PRIORITY)
6796 snode = symtab_node::get (decl);
6797 if (!snode)
6798 return;
6800 else if (TREE_CODE (decl) == VAR_DECL)
6801 snode = varpool_node::get_create (decl);
6802 else
6803 snode = cgraph_node::get_create (decl);
6804 snode->set_init_priority (priority);
6807 /* Set the finalization priority for DECL to PRIORITY. */
6809 void
6810 decl_fini_priority_insert (tree decl, priority_type priority)
6812 struct cgraph_node *node;
6814 if (priority == DEFAULT_INIT_PRIORITY)
6816 node = cgraph_node::get (decl);
6817 if (!node)
6818 return;
6820 else
6821 node = cgraph_node::get_create (decl);
6822 node->set_fini_priority (priority);
6825 /* Print out the statistics for the DECL_DEBUG_EXPR hash table. */
6827 static void
6828 print_debug_expr_statistics (void)
6830 fprintf (stderr, "DECL_DEBUG_EXPR hash: size %ld, %ld elements, %f collisions\n",
6831 (long) debug_expr_for_decl->size (),
6832 (long) debug_expr_for_decl->elements (),
6833 debug_expr_for_decl->collisions ());
6836 /* Print out the statistics for the DECL_VALUE_EXPR hash table. */
6838 static void
6839 print_value_expr_statistics (void)
6841 fprintf (stderr, "DECL_VALUE_EXPR hash: size %ld, %ld elements, %f collisions\n",
6842 (long) value_expr_for_decl->size (),
6843 (long) value_expr_for_decl->elements (),
6844 value_expr_for_decl->collisions ());
6847 /* Lookup a debug expression for FROM, and return it if we find one. */
6849 tree
6850 decl_debug_expr_lookup (tree from)
6852 struct tree_decl_map *h, in;
6853 in.base.from = from;
6855 h = debug_expr_for_decl->find_with_hash (&in, DECL_UID (from));
6856 if (h)
6857 return h->to;
6858 return NULL_TREE;
6861 /* Insert a mapping FROM->TO in the debug expression hashtable. */
6863 void
6864 decl_debug_expr_insert (tree from, tree to)
6866 struct tree_decl_map *h;
6868 h = ggc_alloc<tree_decl_map> ();
6869 h->base.from = from;
6870 h->to = to;
6871 *debug_expr_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT) = h;
6874 /* Lookup a value expression for FROM, and return it if we find one. */
6876 tree
6877 decl_value_expr_lookup (tree from)
6879 struct tree_decl_map *h, in;
6880 in.base.from = from;
6882 h = value_expr_for_decl->find_with_hash (&in, DECL_UID (from));
6883 if (h)
6884 return h->to;
6885 return NULL_TREE;
6888 /* Insert a mapping FROM->TO in the value expression hashtable. */
6890 void
6891 decl_value_expr_insert (tree from, tree to)
6893 struct tree_decl_map *h;
6895 h = ggc_alloc<tree_decl_map> ();
6896 h->base.from = from;
6897 h->to = to;
6898 *value_expr_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT) = h;
6901 /* Lookup a vector of debug arguments for FROM, and return it if we
6902 find one. */
6904 vec<tree, va_gc> **
6905 decl_debug_args_lookup (tree from)
6907 struct tree_vec_map *h, in;
6909 if (!DECL_HAS_DEBUG_ARGS_P (from))
6910 return NULL;
6911 gcc_checking_assert (debug_args_for_decl != NULL);
6912 in.base.from = from;
6913 h = debug_args_for_decl->find_with_hash (&in, DECL_UID (from));
6914 if (h)
6915 return &h->to;
6916 return NULL;
6919 /* Insert a mapping FROM->empty vector of debug arguments in the value
6920 expression hashtable. */
6922 vec<tree, va_gc> **
6923 decl_debug_args_insert (tree from)
6925 struct tree_vec_map *h;
6926 tree_vec_map **loc;
6928 if (DECL_HAS_DEBUG_ARGS_P (from))
6929 return decl_debug_args_lookup (from);
6930 if (debug_args_for_decl == NULL)
6931 debug_args_for_decl = hash_table<tree_vec_map_cache_hasher>::create_ggc (64);
6932 h = ggc_alloc<tree_vec_map> ();
6933 h->base.from = from;
6934 h->to = NULL;
6935 loc = debug_args_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT);
6936 *loc = h;
6937 DECL_HAS_DEBUG_ARGS_P (from) = 1;
6938 return &h->to;
6941 /* Hashing of types so that we don't make duplicates.
6942 The entry point is `type_hash_canon'. */
6944 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
6945 with types in the TREE_VALUE slots), by adding the hash codes
6946 of the individual types. */
6948 static void
6949 type_hash_list (const_tree list, inchash::hash &hstate)
6951 const_tree tail;
6953 for (tail = list; tail; tail = TREE_CHAIN (tail))
6954 if (TREE_VALUE (tail) != error_mark_node)
6955 hstate.add_object (TYPE_HASH (TREE_VALUE (tail)));
6958 /* These are the Hashtable callback functions. */
6960 /* Returns true iff the types are equivalent. */
6962 bool
6963 type_cache_hasher::equal (type_hash *a, type_hash *b)
6965 /* First test the things that are the same for all types. */
6966 if (a->hash != b->hash
6967 || TREE_CODE (a->type) != TREE_CODE (b->type)
6968 || TREE_TYPE (a->type) != TREE_TYPE (b->type)
6969 || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
6970 TYPE_ATTRIBUTES (b->type))
6971 || (TREE_CODE (a->type) != COMPLEX_TYPE
6972 && TYPE_NAME (a->type) != TYPE_NAME (b->type)))
6973 return 0;
6975 /* Be careful about comparing arrays before and after the element type
6976 has been completed; don't compare TYPE_ALIGN unless both types are
6977 complete. */
6978 if (COMPLETE_TYPE_P (a->type) && COMPLETE_TYPE_P (b->type)
6979 && (TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
6980 || TYPE_MODE (a->type) != TYPE_MODE (b->type)))
6981 return 0;
6983 switch (TREE_CODE (a->type))
6985 case VOID_TYPE:
6986 case COMPLEX_TYPE:
6987 case POINTER_TYPE:
6988 case REFERENCE_TYPE:
6989 case NULLPTR_TYPE:
6990 return 1;
6992 case VECTOR_TYPE:
6993 return TYPE_VECTOR_SUBPARTS (a->type) == TYPE_VECTOR_SUBPARTS (b->type);
6995 case ENUMERAL_TYPE:
6996 if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
6997 && !(TYPE_VALUES (a->type)
6998 && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
6999 && TYPE_VALUES (b->type)
7000 && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
7001 && type_list_equal (TYPE_VALUES (a->type),
7002 TYPE_VALUES (b->type))))
7003 return 0;
7005 /* ... fall through ... */
7007 case INTEGER_TYPE:
7008 case REAL_TYPE:
7009 case BOOLEAN_TYPE:
7010 if (TYPE_PRECISION (a->type) != TYPE_PRECISION (b->type))
7011 return false;
7012 return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
7013 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
7014 TYPE_MAX_VALUE (b->type)))
7015 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
7016 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
7017 TYPE_MIN_VALUE (b->type))));
7019 case FIXED_POINT_TYPE:
7020 return TYPE_SATURATING (a->type) == TYPE_SATURATING (b->type);
7022 case OFFSET_TYPE:
7023 return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
7025 case METHOD_TYPE:
7026 if (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
7027 && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
7028 || (TYPE_ARG_TYPES (a->type)
7029 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
7030 && TYPE_ARG_TYPES (b->type)
7031 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
7032 && type_list_equal (TYPE_ARG_TYPES (a->type),
7033 TYPE_ARG_TYPES (b->type)))))
7034 break;
7035 return 0;
7036 case ARRAY_TYPE:
7037 return TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type);
7039 case RECORD_TYPE:
7040 case UNION_TYPE:
7041 case QUAL_UNION_TYPE:
7042 return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
7043 || (TYPE_FIELDS (a->type)
7044 && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
7045 && TYPE_FIELDS (b->type)
7046 && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
7047 && type_list_equal (TYPE_FIELDS (a->type),
7048 TYPE_FIELDS (b->type))));
7050 case FUNCTION_TYPE:
7051 if (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
7052 || (TYPE_ARG_TYPES (a->type)
7053 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
7054 && TYPE_ARG_TYPES (b->type)
7055 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
7056 && type_list_equal (TYPE_ARG_TYPES (a->type),
7057 TYPE_ARG_TYPES (b->type))))
7058 break;
7059 return 0;
7061 default:
7062 return 0;
7065 if (lang_hooks.types.type_hash_eq != NULL)
7066 return lang_hooks.types.type_hash_eq (a->type, b->type);
7068 return 1;
7071 /* Given TYPE, and HASHCODE its hash code, return the canonical
7072 object for an identical type if one already exists.
7073 Otherwise, return TYPE, and record it as the canonical object.
7075 To use this function, first create a type of the sort you want.
7076 Then compute its hash code from the fields of the type that
7077 make it different from other similar types.
7078 Then call this function and use the value. */
7080 tree
7081 type_hash_canon (unsigned int hashcode, tree type)
7083 type_hash in;
7084 type_hash **loc;
7086 /* The hash table only contains main variants, so ensure that's what we're
7087 being passed. */
7088 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
7090 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
7091 must call that routine before comparing TYPE_ALIGNs. */
7092 layout_type (type);
7094 in.hash = hashcode;
7095 in.type = type;
7097 loc = type_hash_table->find_slot_with_hash (&in, hashcode, INSERT);
7098 if (*loc)
7100 tree t1 = ((type_hash *) *loc)->type;
7101 gcc_assert (TYPE_MAIN_VARIANT (t1) == t1);
7102 if (GATHER_STATISTICS)
7104 tree_code_counts[(int) TREE_CODE (type)]--;
7105 tree_node_counts[(int) t_kind]--;
7106 tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type_non_common);
7108 return t1;
7110 else
7112 struct type_hash *h;
7114 h = ggc_alloc<type_hash> ();
7115 h->hash = hashcode;
7116 h->type = type;
7117 *loc = h;
7119 return type;
7123 static void
7124 print_type_hash_statistics (void)
7126 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
7127 (long) type_hash_table->size (),
7128 (long) type_hash_table->elements (),
7129 type_hash_table->collisions ());
7132 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
7133 with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
7134 by adding the hash codes of the individual attributes. */
7136 static void
7137 attribute_hash_list (const_tree list, inchash::hash &hstate)
7139 const_tree tail;
7141 for (tail = list; tail; tail = TREE_CHAIN (tail))
7142 /* ??? Do we want to add in TREE_VALUE too? */
7143 hstate.add_object (IDENTIFIER_HASH_VALUE (get_attribute_name (tail)));
7146 /* Given two lists of attributes, return true if list l2 is
7147 equivalent to l1. */
7150 attribute_list_equal (const_tree l1, const_tree l2)
7152 if (l1 == l2)
7153 return 1;
7155 return attribute_list_contained (l1, l2)
7156 && attribute_list_contained (l2, l1);
7159 /* Given two lists of attributes, return true if list L2 is
7160 completely contained within L1. */
7161 /* ??? This would be faster if attribute names were stored in a canonicalized
7162 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
7163 must be used to show these elements are equivalent (which they are). */
7164 /* ??? It's not clear that attributes with arguments will always be handled
7165 correctly. */
7168 attribute_list_contained (const_tree l1, const_tree l2)
7170 const_tree t1, t2;
7172 /* First check the obvious, maybe the lists are identical. */
7173 if (l1 == l2)
7174 return 1;
7176 /* Maybe the lists are similar. */
7177 for (t1 = l1, t2 = l2;
7178 t1 != 0 && t2 != 0
7179 && get_attribute_name (t1) == get_attribute_name (t2)
7180 && TREE_VALUE (t1) == TREE_VALUE (t2);
7181 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
7184 /* Maybe the lists are equal. */
7185 if (t1 == 0 && t2 == 0)
7186 return 1;
7188 for (; t2 != 0; t2 = TREE_CHAIN (t2))
7190 const_tree attr;
7191 /* This CONST_CAST is okay because lookup_attribute does not
7192 modify its argument and the return value is assigned to a
7193 const_tree. */
7194 for (attr = lookup_ident_attribute (get_attribute_name (t2),
7195 CONST_CAST_TREE (l1));
7196 attr != NULL_TREE && !attribute_value_equal (t2, attr);
7197 attr = lookup_ident_attribute (get_attribute_name (t2),
7198 TREE_CHAIN (attr)))
7201 if (attr == NULL_TREE)
7202 return 0;
7205 return 1;
7208 /* Given two lists of types
7209 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
7210 return 1 if the lists contain the same types in the same order.
7211 Also, the TREE_PURPOSEs must match. */
7214 type_list_equal (const_tree l1, const_tree l2)
7216 const_tree t1, t2;
7218 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
7219 if (TREE_VALUE (t1) != TREE_VALUE (t2)
7220 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
7221 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
7222 && (TREE_TYPE (TREE_PURPOSE (t1))
7223 == TREE_TYPE (TREE_PURPOSE (t2))))))
7224 return 0;
7226 return t1 == t2;
7229 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
7230 given by TYPE. If the argument list accepts variable arguments,
7231 then this function counts only the ordinary arguments. */
7234 type_num_arguments (const_tree type)
7236 int i = 0;
7237 tree t;
7239 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
7240 /* If the function does not take a variable number of arguments,
7241 the last element in the list will have type `void'. */
7242 if (VOID_TYPE_P (TREE_VALUE (t)))
7243 break;
7244 else
7245 ++i;
7247 return i;
7250 /* Nonzero if integer constants T1 and T2
7251 represent the same constant value. */
7254 tree_int_cst_equal (const_tree t1, const_tree t2)
7256 if (t1 == t2)
7257 return 1;
7259 if (t1 == 0 || t2 == 0)
7260 return 0;
7262 if (TREE_CODE (t1) == INTEGER_CST
7263 && TREE_CODE (t2) == INTEGER_CST
7264 && wi::to_widest (t1) == wi::to_widest (t2))
7265 return 1;
7267 return 0;
7270 /* Return true if T is an INTEGER_CST whose numerical value (extended
7271 according to TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. */
7273 bool
7274 tree_fits_shwi_p (const_tree t)
7276 return (t != NULL_TREE
7277 && TREE_CODE (t) == INTEGER_CST
7278 && wi::fits_shwi_p (wi::to_widest (t)));
7281 /* Return true if T is an INTEGER_CST whose numerical value (extended
7282 according to TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. */
7284 bool
7285 tree_fits_uhwi_p (const_tree t)
7287 return (t != NULL_TREE
7288 && TREE_CODE (t) == INTEGER_CST
7289 && wi::fits_uhwi_p (wi::to_widest (t)));
7292 /* T is an INTEGER_CST whose numerical value (extended according to
7293 TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. Return that
7294 HOST_WIDE_INT. */
7296 HOST_WIDE_INT
7297 tree_to_shwi (const_tree t)
7299 gcc_assert (tree_fits_shwi_p (t));
7300 return TREE_INT_CST_LOW (t);
7303 /* T is an INTEGER_CST whose numerical value (extended according to
7304 TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. Return that
7305 HOST_WIDE_INT. */
7307 unsigned HOST_WIDE_INT
7308 tree_to_uhwi (const_tree t)
7310 gcc_assert (tree_fits_uhwi_p (t));
7311 return TREE_INT_CST_LOW (t);
7314 /* Return the most significant (sign) bit of T. */
7317 tree_int_cst_sign_bit (const_tree t)
7319 unsigned bitno = TYPE_PRECISION (TREE_TYPE (t)) - 1;
7321 return wi::extract_uhwi (t, bitno, 1);
7324 /* Return an indication of the sign of the integer constant T.
7325 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
7326 Note that -1 will never be returned if T's type is unsigned. */
7329 tree_int_cst_sgn (const_tree t)
7331 if (wi::eq_p (t, 0))
7332 return 0;
7333 else if (TYPE_UNSIGNED (TREE_TYPE (t)))
7334 return 1;
7335 else if (wi::neg_p (t))
7336 return -1;
7337 else
7338 return 1;
7341 /* Return the minimum number of bits needed to represent VALUE in a
7342 signed or unsigned type, UNSIGNEDP says which. */
7344 unsigned int
7345 tree_int_cst_min_precision (tree value, signop sgn)
7347 /* If the value is negative, compute its negative minus 1. The latter
7348 adjustment is because the absolute value of the largest negative value
7349 is one larger than the largest positive value. This is equivalent to
7350 a bit-wise negation, so use that operation instead. */
7352 if (tree_int_cst_sgn (value) < 0)
7353 value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value);
7355 /* Return the number of bits needed, taking into account the fact
7356 that we need one more bit for a signed than unsigned type.
7357 If value is 0 or -1, the minimum precision is 1 no matter
7358 whether unsignedp is true or false. */
7360 if (integer_zerop (value))
7361 return 1;
7362 else
7363 return tree_floor_log2 (value) + 1 + (sgn == SIGNED ? 1 : 0) ;
7366 /* Return truthvalue of whether T1 is the same tree structure as T2.
7367 Return 1 if they are the same.
7368 Return 0 if they are understandably different.
7369 Return -1 if either contains tree structure not understood by
7370 this function. */
7373 simple_cst_equal (const_tree t1, const_tree t2)
7375 enum tree_code code1, code2;
7376 int cmp;
7377 int i;
7379 if (t1 == t2)
7380 return 1;
7381 if (t1 == 0 || t2 == 0)
7382 return 0;
7384 code1 = TREE_CODE (t1);
7385 code2 = TREE_CODE (t2);
7387 if (CONVERT_EXPR_CODE_P (code1) || code1 == NON_LVALUE_EXPR)
7389 if (CONVERT_EXPR_CODE_P (code2)
7390 || code2 == NON_LVALUE_EXPR)
7391 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7392 else
7393 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
7396 else if (CONVERT_EXPR_CODE_P (code2)
7397 || code2 == NON_LVALUE_EXPR)
7398 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
7400 if (code1 != code2)
7401 return 0;
7403 switch (code1)
7405 case INTEGER_CST:
7406 return wi::to_widest (t1) == wi::to_widest (t2);
7408 case REAL_CST:
7409 return real_identical (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
7411 case FIXED_CST:
7412 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
7414 case STRING_CST:
7415 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
7416 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
7417 TREE_STRING_LENGTH (t1)));
7419 case CONSTRUCTOR:
7421 unsigned HOST_WIDE_INT idx;
7422 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (t1);
7423 vec<constructor_elt, va_gc> *v2 = CONSTRUCTOR_ELTS (t2);
7425 if (vec_safe_length (v1) != vec_safe_length (v2))
7426 return false;
7428 for (idx = 0; idx < vec_safe_length (v1); ++idx)
7429 /* ??? Should we handle also fields here? */
7430 if (!simple_cst_equal ((*v1)[idx].value, (*v2)[idx].value))
7431 return false;
7432 return true;
7435 case SAVE_EXPR:
7436 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7438 case CALL_EXPR:
7439 cmp = simple_cst_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2));
7440 if (cmp <= 0)
7441 return cmp;
7442 if (call_expr_nargs (t1) != call_expr_nargs (t2))
7443 return 0;
7445 const_tree arg1, arg2;
7446 const_call_expr_arg_iterator iter1, iter2;
7447 for (arg1 = first_const_call_expr_arg (t1, &iter1),
7448 arg2 = first_const_call_expr_arg (t2, &iter2);
7449 arg1 && arg2;
7450 arg1 = next_const_call_expr_arg (&iter1),
7451 arg2 = next_const_call_expr_arg (&iter2))
7453 cmp = simple_cst_equal (arg1, arg2);
7454 if (cmp <= 0)
7455 return cmp;
7457 return arg1 == arg2;
7460 case TARGET_EXPR:
7461 /* Special case: if either target is an unallocated VAR_DECL,
7462 it means that it's going to be unified with whatever the
7463 TARGET_EXPR is really supposed to initialize, so treat it
7464 as being equivalent to anything. */
7465 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
7466 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
7467 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
7468 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
7469 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
7470 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
7471 cmp = 1;
7472 else
7473 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7475 if (cmp <= 0)
7476 return cmp;
7478 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
7480 case WITH_CLEANUP_EXPR:
7481 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7482 if (cmp <= 0)
7483 return cmp;
7485 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
7487 case COMPONENT_REF:
7488 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
7489 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7491 return 0;
7493 case VAR_DECL:
7494 case PARM_DECL:
7495 case CONST_DECL:
7496 case FUNCTION_DECL:
7497 return 0;
7499 default:
7500 break;
7503 /* This general rule works for most tree codes. All exceptions should be
7504 handled above. If this is a language-specific tree code, we can't
7505 trust what might be in the operand, so say we don't know
7506 the situation. */
7507 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
7508 return -1;
7510 switch (TREE_CODE_CLASS (code1))
7512 case tcc_unary:
7513 case tcc_binary:
7514 case tcc_comparison:
7515 case tcc_expression:
7516 case tcc_reference:
7517 case tcc_statement:
7518 cmp = 1;
7519 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
7521 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
7522 if (cmp <= 0)
7523 return cmp;
7526 return cmp;
7528 default:
7529 return -1;
7533 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
7534 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
7535 than U, respectively. */
7538 compare_tree_int (const_tree t, unsigned HOST_WIDE_INT u)
7540 if (tree_int_cst_sgn (t) < 0)
7541 return -1;
7542 else if (!tree_fits_uhwi_p (t))
7543 return 1;
7544 else if (TREE_INT_CST_LOW (t) == u)
7545 return 0;
7546 else if (TREE_INT_CST_LOW (t) < u)
7547 return -1;
7548 else
7549 return 1;
7552 /* Return true if SIZE represents a constant size that is in bounds of
7553 what the middle-end and the backend accepts (covering not more than
7554 half of the address-space). */
7556 bool
7557 valid_constant_size_p (const_tree size)
7559 if (! tree_fits_uhwi_p (size)
7560 || TREE_OVERFLOW (size)
7561 || tree_int_cst_sign_bit (size) != 0)
7562 return false;
7563 return true;
7566 /* Return the precision of the type, or for a complex or vector type the
7567 precision of the type of its elements. */
7569 unsigned int
7570 element_precision (const_tree type)
7572 if (!TYPE_P (type))
7573 type = TREE_TYPE (type);
7574 enum tree_code code = TREE_CODE (type);
7575 if (code == COMPLEX_TYPE || code == VECTOR_TYPE)
7576 type = TREE_TYPE (type);
7578 return TYPE_PRECISION (type);
7581 /* Return true if CODE represents an associative tree code. Otherwise
7582 return false. */
7583 bool
7584 associative_tree_code (enum tree_code code)
7586 switch (code)
7588 case BIT_IOR_EXPR:
7589 case BIT_AND_EXPR:
7590 case BIT_XOR_EXPR:
7591 case PLUS_EXPR:
7592 case MULT_EXPR:
7593 case MIN_EXPR:
7594 case MAX_EXPR:
7595 return true;
7597 default:
7598 break;
7600 return false;
7603 /* Return true if CODE represents a commutative tree code. Otherwise
7604 return false. */
7605 bool
7606 commutative_tree_code (enum tree_code code)
7608 switch (code)
7610 case PLUS_EXPR:
7611 case MULT_EXPR:
7612 case MULT_HIGHPART_EXPR:
7613 case MIN_EXPR:
7614 case MAX_EXPR:
7615 case BIT_IOR_EXPR:
7616 case BIT_XOR_EXPR:
7617 case BIT_AND_EXPR:
7618 case NE_EXPR:
7619 case EQ_EXPR:
7620 case UNORDERED_EXPR:
7621 case ORDERED_EXPR:
7622 case UNEQ_EXPR:
7623 case LTGT_EXPR:
7624 case TRUTH_AND_EXPR:
7625 case TRUTH_XOR_EXPR:
7626 case TRUTH_OR_EXPR:
7627 case WIDEN_MULT_EXPR:
7628 case VEC_WIDEN_MULT_HI_EXPR:
7629 case VEC_WIDEN_MULT_LO_EXPR:
7630 case VEC_WIDEN_MULT_EVEN_EXPR:
7631 case VEC_WIDEN_MULT_ODD_EXPR:
7632 return true;
7634 default:
7635 break;
7637 return false;
7640 /* Return true if CODE represents a ternary tree code for which the
7641 first two operands are commutative. Otherwise return false. */
7642 bool
7643 commutative_ternary_tree_code (enum tree_code code)
7645 switch (code)
7647 case WIDEN_MULT_PLUS_EXPR:
7648 case WIDEN_MULT_MINUS_EXPR:
7649 case DOT_PROD_EXPR:
7650 case FMA_EXPR:
7651 return true;
7653 default:
7654 break;
7656 return false;
7659 /* Returns true if CODE can overflow. */
7661 bool
7662 operation_can_overflow (enum tree_code code)
7664 switch (code)
7666 case PLUS_EXPR:
7667 case MINUS_EXPR:
7668 case MULT_EXPR:
7669 case LSHIFT_EXPR:
7670 /* Can overflow in various ways. */
7671 return true;
7672 case TRUNC_DIV_EXPR:
7673 case EXACT_DIV_EXPR:
7674 case FLOOR_DIV_EXPR:
7675 case CEIL_DIV_EXPR:
7676 /* For INT_MIN / -1. */
7677 return true;
7678 case NEGATE_EXPR:
7679 case ABS_EXPR:
7680 /* For -INT_MIN. */
7681 return true;
7682 default:
7683 /* These operators cannot overflow. */
7684 return false;
7688 /* Returns true if CODE operating on operands of type TYPE doesn't overflow, or
7689 ftrapv doesn't generate trapping insns for CODE. */
7691 bool
7692 operation_no_trapping_overflow (tree type, enum tree_code code)
7694 gcc_checking_assert (ANY_INTEGRAL_TYPE_P (type));
7696 /* We don't generate instructions that trap on overflow for complex or vector
7697 types. */
7698 if (!INTEGRAL_TYPE_P (type))
7699 return true;
7701 if (!TYPE_OVERFLOW_TRAPS (type))
7702 return true;
7704 switch (code)
7706 case PLUS_EXPR:
7707 case MINUS_EXPR:
7708 case MULT_EXPR:
7709 case NEGATE_EXPR:
7710 case ABS_EXPR:
7711 /* These operators can overflow, and -ftrapv generates trapping code for
7712 these. */
7713 return false;
7714 case TRUNC_DIV_EXPR:
7715 case EXACT_DIV_EXPR:
7716 case FLOOR_DIV_EXPR:
7717 case CEIL_DIV_EXPR:
7718 case LSHIFT_EXPR:
7719 /* These operators can overflow, but -ftrapv does not generate trapping
7720 code for these. */
7721 return true;
7722 default:
7723 /* These operators cannot overflow. */
7724 return true;
7728 namespace inchash
7731 /* Generate a hash value for an expression. This can be used iteratively
7732 by passing a previous result as the HSTATE argument.
7734 This function is intended to produce the same hash for expressions which
7735 would compare equal using operand_equal_p. */
7736 void
7737 add_expr (const_tree t, inchash::hash &hstate)
7739 int i;
7740 enum tree_code code;
7741 enum tree_code_class tclass;
7743 if (t == NULL_TREE)
7745 hstate.merge_hash (0);
7746 return;
7749 code = TREE_CODE (t);
7751 switch (code)
7753 /* Alas, constants aren't shared, so we can't rely on pointer
7754 identity. */
7755 case VOID_CST:
7756 hstate.merge_hash (0);
7757 return;
7758 case INTEGER_CST:
7759 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
7760 hstate.add_wide_int (TREE_INT_CST_ELT (t, i));
7761 return;
7762 case REAL_CST:
7764 unsigned int val2 = real_hash (TREE_REAL_CST_PTR (t));
7765 hstate.merge_hash (val2);
7766 return;
7768 case FIXED_CST:
7770 unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
7771 hstate.merge_hash (val2);
7772 return;
7774 case STRING_CST:
7775 hstate.add ((const void *) TREE_STRING_POINTER (t), TREE_STRING_LENGTH (t));
7776 return;
7777 case COMPLEX_CST:
7778 inchash::add_expr (TREE_REALPART (t), hstate);
7779 inchash::add_expr (TREE_IMAGPART (t), hstate);
7780 return;
7781 case VECTOR_CST:
7783 unsigned i;
7784 for (i = 0; i < VECTOR_CST_NELTS (t); ++i)
7785 inchash::add_expr (VECTOR_CST_ELT (t, i), hstate);
7786 return;
7788 case SSA_NAME:
7789 /* We can just compare by pointer. */
7790 hstate.add_wide_int (SSA_NAME_VERSION (t));
7791 return;
7792 case PLACEHOLDER_EXPR:
7793 /* The node itself doesn't matter. */
7794 return;
7795 case TREE_LIST:
7796 /* A list of expressions, for a CALL_EXPR or as the elements of a
7797 VECTOR_CST. */
7798 for (; t; t = TREE_CHAIN (t))
7799 inchash::add_expr (TREE_VALUE (t), hstate);
7800 return;
7801 case CONSTRUCTOR:
7803 unsigned HOST_WIDE_INT idx;
7804 tree field, value;
7805 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
7807 inchash::add_expr (field, hstate);
7808 inchash::add_expr (value, hstate);
7810 return;
7812 case FUNCTION_DECL:
7813 /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
7814 Otherwise nodes that compare equal according to operand_equal_p might
7815 get different hash codes. However, don't do this for machine specific
7816 or front end builtins, since the function code is overloaded in those
7817 cases. */
7818 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
7819 && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
7821 t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
7822 code = TREE_CODE (t);
7824 /* FALL THROUGH */
7825 default:
7826 tclass = TREE_CODE_CLASS (code);
7828 if (tclass == tcc_declaration)
7830 /* DECL's have a unique ID */
7831 hstate.add_wide_int (DECL_UID (t));
7833 else
7835 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
7837 hstate.add_object (code);
7839 /* Don't hash the type, that can lead to having nodes which
7840 compare equal according to operand_equal_p, but which
7841 have different hash codes. */
7842 if (CONVERT_EXPR_CODE_P (code)
7843 || code == NON_LVALUE_EXPR)
7845 /* Make sure to include signness in the hash computation. */
7846 hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
7847 inchash::add_expr (TREE_OPERAND (t, 0), hstate);
7850 else if (commutative_tree_code (code))
7852 /* It's a commutative expression. We want to hash it the same
7853 however it appears. We do this by first hashing both operands
7854 and then rehashing based on the order of their independent
7855 hashes. */
7856 inchash::hash one, two;
7857 inchash::add_expr (TREE_OPERAND (t, 0), one);
7858 inchash::add_expr (TREE_OPERAND (t, 1), two);
7859 hstate.add_commutative (one, two);
7861 else
7862 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
7863 inchash::add_expr (TREE_OPERAND (t, i), hstate);
7865 return;
7871 /* Constructors for pointer, array and function types.
7872 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
7873 constructed by language-dependent code, not here.) */
7875 /* Construct, lay out and return the type of pointers to TO_TYPE with
7876 mode MODE. If CAN_ALIAS_ALL is TRUE, indicate this type can
7877 reference all of memory. If such a type has already been
7878 constructed, reuse it. */
7880 tree
7881 build_pointer_type_for_mode (tree to_type, machine_mode mode,
7882 bool can_alias_all)
7884 tree t;
7885 bool could_alias = can_alias_all;
7887 if (to_type == error_mark_node)
7888 return error_mark_node;
7890 /* If the pointed-to type has the may_alias attribute set, force
7891 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7892 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7893 can_alias_all = true;
7895 /* In some cases, languages will have things that aren't a POINTER_TYPE
7896 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
7897 In that case, return that type without regard to the rest of our
7898 operands.
7900 ??? This is a kludge, but consistent with the way this function has
7901 always operated and there doesn't seem to be a good way to avoid this
7902 at the moment. */
7903 if (TYPE_POINTER_TO (to_type) != 0
7904 && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
7905 return TYPE_POINTER_TO (to_type);
7907 /* First, if we already have a type for pointers to TO_TYPE and it's
7908 the proper mode, use it. */
7909 for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
7910 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7911 return t;
7913 t = make_node (POINTER_TYPE);
7915 TREE_TYPE (t) = to_type;
7916 SET_TYPE_MODE (t, mode);
7917 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7918 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
7919 TYPE_POINTER_TO (to_type) = t;
7921 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7922 SET_TYPE_STRUCTURAL_EQUALITY (t);
7923 else if (TYPE_CANONICAL (to_type) != to_type || could_alias)
7924 TYPE_CANONICAL (t)
7925 = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
7926 mode, false);
7928 /* Lay out the type. This function has many callers that are concerned
7929 with expression-construction, and this simplifies them all. */
7930 layout_type (t);
7932 return t;
7935 /* By default build pointers in ptr_mode. */
7937 tree
7938 build_pointer_type (tree to_type)
7940 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7941 : TYPE_ADDR_SPACE (to_type);
7942 machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7943 return build_pointer_type_for_mode (to_type, pointer_mode, false);
7946 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE. */
7948 tree
7949 build_reference_type_for_mode (tree to_type, machine_mode mode,
7950 bool can_alias_all)
7952 tree t;
7953 bool could_alias = can_alias_all;
7955 if (to_type == error_mark_node)
7956 return error_mark_node;
7958 /* If the pointed-to type has the may_alias attribute set, force
7959 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7960 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7961 can_alias_all = true;
7963 /* In some cases, languages will have things that aren't a REFERENCE_TYPE
7964 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
7965 In that case, return that type without regard to the rest of our
7966 operands.
7968 ??? This is a kludge, but consistent with the way this function has
7969 always operated and there doesn't seem to be a good way to avoid this
7970 at the moment. */
7971 if (TYPE_REFERENCE_TO (to_type) != 0
7972 && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
7973 return TYPE_REFERENCE_TO (to_type);
7975 /* First, if we already have a type for pointers to TO_TYPE and it's
7976 the proper mode, use it. */
7977 for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
7978 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7979 return t;
7981 t = make_node (REFERENCE_TYPE);
7983 TREE_TYPE (t) = to_type;
7984 SET_TYPE_MODE (t, mode);
7985 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7986 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
7987 TYPE_REFERENCE_TO (to_type) = t;
7989 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
7990 SET_TYPE_STRUCTURAL_EQUALITY (t);
7991 else if (TYPE_CANONICAL (to_type) != to_type || could_alias)
7992 TYPE_CANONICAL (t)
7993 = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
7994 mode, false);
7996 layout_type (t);
7998 return t;
8002 /* Build the node for the type of references-to-TO_TYPE by default
8003 in ptr_mode. */
8005 tree
8006 build_reference_type (tree to_type)
8008 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
8009 : TYPE_ADDR_SPACE (to_type);
8010 machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
8011 return build_reference_type_for_mode (to_type, pointer_mode, false);
8014 #define MAX_INT_CACHED_PREC \
8015 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
8016 static GTY(()) tree nonstandard_integer_type_cache[2 * MAX_INT_CACHED_PREC + 2];
8018 /* Builds a signed or unsigned integer type of precision PRECISION.
8019 Used for C bitfields whose precision does not match that of
8020 built-in target types. */
8021 tree
8022 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
8023 int unsignedp)
8025 tree itype, ret;
8027 if (unsignedp)
8028 unsignedp = MAX_INT_CACHED_PREC + 1;
8030 if (precision <= MAX_INT_CACHED_PREC)
8032 itype = nonstandard_integer_type_cache[precision + unsignedp];
8033 if (itype)
8034 return itype;
8037 itype = make_node (INTEGER_TYPE);
8038 TYPE_PRECISION (itype) = precision;
8040 if (unsignedp)
8041 fixup_unsigned_type (itype);
8042 else
8043 fixup_signed_type (itype);
8045 ret = itype;
8046 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (itype)))
8047 ret = type_hash_canon (tree_to_uhwi (TYPE_MAX_VALUE (itype)), itype);
8048 if (precision <= MAX_INT_CACHED_PREC)
8049 nonstandard_integer_type_cache[precision + unsignedp] = ret;
8051 return ret;
8054 #define MAX_BOOL_CACHED_PREC \
8055 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
8056 static GTY(()) tree nonstandard_boolean_type_cache[MAX_BOOL_CACHED_PREC + 1];
8058 /* Builds a boolean type of precision PRECISION.
8059 Used for boolean vectors to choose proper vector element size. */
8060 tree
8061 build_nonstandard_boolean_type (unsigned HOST_WIDE_INT precision)
8063 tree type;
8065 if (precision <= MAX_BOOL_CACHED_PREC)
8067 type = nonstandard_boolean_type_cache[precision];
8068 if (type)
8069 return type;
8072 type = make_node (BOOLEAN_TYPE);
8073 TYPE_PRECISION (type) = precision;
8074 fixup_unsigned_type (type);
8076 if (precision <= MAX_INT_CACHED_PREC)
8077 nonstandard_boolean_type_cache[precision] = type;
8079 return type;
8082 /* Create a range of some discrete type TYPE (an INTEGER_TYPE, ENUMERAL_TYPE
8083 or BOOLEAN_TYPE) with low bound LOWVAL and high bound HIGHVAL. If SHARED
8084 is true, reuse such a type that has already been constructed. */
8086 static tree
8087 build_range_type_1 (tree type, tree lowval, tree highval, bool shared)
8089 tree itype = make_node (INTEGER_TYPE);
8090 inchash::hash hstate;
8092 TREE_TYPE (itype) = type;
8094 TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
8095 TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
8097 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
8098 SET_TYPE_MODE (itype, TYPE_MODE (type));
8099 TYPE_SIZE (itype) = TYPE_SIZE (type);
8100 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
8101 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
8102 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
8104 if (!shared)
8105 return itype;
8107 if ((TYPE_MIN_VALUE (itype)
8108 && TREE_CODE (TYPE_MIN_VALUE (itype)) != INTEGER_CST)
8109 || (TYPE_MAX_VALUE (itype)
8110 && TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST))
8112 /* Since we cannot reliably merge this type, we need to compare it using
8113 structural equality checks. */
8114 SET_TYPE_STRUCTURAL_EQUALITY (itype);
8115 return itype;
8118 inchash::add_expr (TYPE_MIN_VALUE (itype), hstate);
8119 inchash::add_expr (TYPE_MAX_VALUE (itype), hstate);
8120 hstate.merge_hash (TYPE_HASH (type));
8121 itype = type_hash_canon (hstate.end (), itype);
8123 return itype;
8126 /* Wrapper around build_range_type_1 with SHARED set to true. */
8128 tree
8129 build_range_type (tree type, tree lowval, tree highval)
8131 return build_range_type_1 (type, lowval, highval, true);
8134 /* Wrapper around build_range_type_1 with SHARED set to false. */
8136 tree
8137 build_nonshared_range_type (tree type, tree lowval, tree highval)
8139 return build_range_type_1 (type, lowval, highval, false);
8142 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
8143 MAXVAL should be the maximum value in the domain
8144 (one less than the length of the array).
8146 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
8147 We don't enforce this limit, that is up to caller (e.g. language front end).
8148 The limit exists because the result is a signed type and we don't handle
8149 sizes that use more than one HOST_WIDE_INT. */
8151 tree
8152 build_index_type (tree maxval)
8154 return build_range_type (sizetype, size_zero_node, maxval);
8157 /* Return true if the debug information for TYPE, a subtype, should be emitted
8158 as a subrange type. If so, set LOWVAL to the low bound and HIGHVAL to the
8159 high bound, respectively. Sometimes doing so unnecessarily obfuscates the
8160 debug info and doesn't reflect the source code. */
8162 bool
8163 subrange_type_for_debug_p (const_tree type, tree *lowval, tree *highval)
8165 tree base_type = TREE_TYPE (type), low, high;
8167 /* Subrange types have a base type which is an integral type. */
8168 if (!INTEGRAL_TYPE_P (base_type))
8169 return false;
8171 /* Get the real bounds of the subtype. */
8172 if (lang_hooks.types.get_subrange_bounds)
8173 lang_hooks.types.get_subrange_bounds (type, &low, &high);
8174 else
8176 low = TYPE_MIN_VALUE (type);
8177 high = TYPE_MAX_VALUE (type);
8180 /* If the type and its base type have the same representation and the same
8181 name, then the type is not a subrange but a copy of the base type. */
8182 if ((TREE_CODE (base_type) == INTEGER_TYPE
8183 || TREE_CODE (base_type) == BOOLEAN_TYPE)
8184 && int_size_in_bytes (type) == int_size_in_bytes (base_type)
8185 && tree_int_cst_equal (low, TYPE_MIN_VALUE (base_type))
8186 && tree_int_cst_equal (high, TYPE_MAX_VALUE (base_type))
8187 && TYPE_IDENTIFIER (type) == TYPE_IDENTIFIER (base_type))
8188 return false;
8190 if (lowval)
8191 *lowval = low;
8192 if (highval)
8193 *highval = high;
8194 return true;
8197 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
8198 and number of elements specified by the range of values of INDEX_TYPE.
8199 If SHARED is true, reuse such a type that has already been constructed. */
8201 static tree
8202 build_array_type_1 (tree elt_type, tree index_type, bool shared)
8204 tree t;
8206 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
8208 error ("arrays of functions are not meaningful");
8209 elt_type = integer_type_node;
8212 t = make_node (ARRAY_TYPE);
8213 TREE_TYPE (t) = elt_type;
8214 TYPE_DOMAIN (t) = index_type;
8215 TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (elt_type);
8216 layout_type (t);
8218 /* If the element type is incomplete at this point we get marked for
8219 structural equality. Do not record these types in the canonical
8220 type hashtable. */
8221 if (TYPE_STRUCTURAL_EQUALITY_P (t))
8222 return t;
8224 if (shared)
8226 inchash::hash hstate;
8227 hstate.add_object (TYPE_HASH (elt_type));
8228 if (index_type)
8229 hstate.add_object (TYPE_HASH (index_type));
8230 t = type_hash_canon (hstate.end (), t);
8233 if (TYPE_CANONICAL (t) == t)
8235 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
8236 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
8237 SET_TYPE_STRUCTURAL_EQUALITY (t);
8238 else if (TYPE_CANONICAL (elt_type) != elt_type
8239 || (index_type && TYPE_CANONICAL (index_type) != index_type))
8240 TYPE_CANONICAL (t)
8241 = build_array_type_1 (TYPE_CANONICAL (elt_type),
8242 index_type
8243 ? TYPE_CANONICAL (index_type) : NULL_TREE,
8244 shared);
8247 return t;
8250 /* Wrapper around build_array_type_1 with SHARED set to true. */
8252 tree
8253 build_array_type (tree elt_type, tree index_type)
8255 return build_array_type_1 (elt_type, index_type, true);
8258 /* Wrapper around build_array_type_1 with SHARED set to false. */
8260 tree
8261 build_nonshared_array_type (tree elt_type, tree index_type)
8263 return build_array_type_1 (elt_type, index_type, false);
8266 /* Return a representation of ELT_TYPE[NELTS], using indices of type
8267 sizetype. */
8269 tree
8270 build_array_type_nelts (tree elt_type, unsigned HOST_WIDE_INT nelts)
8272 return build_array_type (elt_type, build_index_type (size_int (nelts - 1)));
8275 /* Recursively examines the array elements of TYPE, until a non-array
8276 element type is found. */
8278 tree
8279 strip_array_types (tree type)
8281 while (TREE_CODE (type) == ARRAY_TYPE)
8282 type = TREE_TYPE (type);
8284 return type;
8287 /* Computes the canonical argument types from the argument type list
8288 ARGTYPES.
8290 Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
8291 on entry to this function, or if any of the ARGTYPES are
8292 structural.
8294 Upon return, *ANY_NONCANONICAL_P will be true iff either it was
8295 true on entry to this function, or if any of the ARGTYPES are
8296 non-canonical.
8298 Returns a canonical argument list, which may be ARGTYPES when the
8299 canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
8300 true) or would not differ from ARGTYPES. */
8302 static tree
8303 maybe_canonicalize_argtypes (tree argtypes,
8304 bool *any_structural_p,
8305 bool *any_noncanonical_p)
8307 tree arg;
8308 bool any_noncanonical_argtypes_p = false;
8310 for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
8312 if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
8313 /* Fail gracefully by stating that the type is structural. */
8314 *any_structural_p = true;
8315 else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
8316 *any_structural_p = true;
8317 else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
8318 || TREE_PURPOSE (arg))
8319 /* If the argument has a default argument, we consider it
8320 non-canonical even though the type itself is canonical.
8321 That way, different variants of function and method types
8322 with default arguments will all point to the variant with
8323 no defaults as their canonical type. */
8324 any_noncanonical_argtypes_p = true;
8327 if (*any_structural_p)
8328 return argtypes;
8330 if (any_noncanonical_argtypes_p)
8332 /* Build the canonical list of argument types. */
8333 tree canon_argtypes = NULL_TREE;
8334 bool is_void = false;
8336 for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
8338 if (arg == void_list_node)
8339 is_void = true;
8340 else
8341 canon_argtypes = tree_cons (NULL_TREE,
8342 TYPE_CANONICAL (TREE_VALUE (arg)),
8343 canon_argtypes);
8346 canon_argtypes = nreverse (canon_argtypes);
8347 if (is_void)
8348 canon_argtypes = chainon (canon_argtypes, void_list_node);
8350 /* There is a non-canonical type. */
8351 *any_noncanonical_p = true;
8352 return canon_argtypes;
8355 /* The canonical argument types are the same as ARGTYPES. */
8356 return argtypes;
8359 /* Construct, lay out and return
8360 the type of functions returning type VALUE_TYPE
8361 given arguments of types ARG_TYPES.
8362 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
8363 are data type nodes for the arguments of the function.
8364 If such a type has already been constructed, reuse it. */
8366 tree
8367 build_function_type (tree value_type, tree arg_types)
8369 tree t;
8370 inchash::hash hstate;
8371 bool any_structural_p, any_noncanonical_p;
8372 tree canon_argtypes;
8374 if (TREE_CODE (value_type) == FUNCTION_TYPE)
8376 error ("function return type cannot be function");
8377 value_type = integer_type_node;
8380 /* Make a node of the sort we want. */
8381 t = make_node (FUNCTION_TYPE);
8382 TREE_TYPE (t) = value_type;
8383 TYPE_ARG_TYPES (t) = arg_types;
8385 /* If we already have such a type, use the old one. */
8386 hstate.add_object (TYPE_HASH (value_type));
8387 type_hash_list (arg_types, hstate);
8388 t = type_hash_canon (hstate.end (), t);
8390 /* Set up the canonical type. */
8391 any_structural_p = TYPE_STRUCTURAL_EQUALITY_P (value_type);
8392 any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
8393 canon_argtypes = maybe_canonicalize_argtypes (arg_types,
8394 &any_structural_p,
8395 &any_noncanonical_p);
8396 if (any_structural_p)
8397 SET_TYPE_STRUCTURAL_EQUALITY (t);
8398 else if (any_noncanonical_p)
8399 TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
8400 canon_argtypes);
8402 if (!COMPLETE_TYPE_P (t))
8403 layout_type (t);
8404 return t;
8407 /* Build a function type. The RETURN_TYPE is the type returned by the
8408 function. If VAARGS is set, no void_type_node is appended to the
8409 the list. ARGP must be always be terminated be a NULL_TREE. */
8411 static tree
8412 build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
8414 tree t, args, last;
8416 t = va_arg (argp, tree);
8417 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
8418 args = tree_cons (NULL_TREE, t, args);
8420 if (vaargs)
8422 last = args;
8423 if (args != NULL_TREE)
8424 args = nreverse (args);
8425 gcc_assert (last != void_list_node);
8427 else if (args == NULL_TREE)
8428 args = void_list_node;
8429 else
8431 last = args;
8432 args = nreverse (args);
8433 TREE_CHAIN (last) = void_list_node;
8435 args = build_function_type (return_type, args);
8437 return args;
8440 /* Build a function type. The RETURN_TYPE is the type returned by the
8441 function. If additional arguments are provided, they are
8442 additional argument types. The list of argument types must always
8443 be terminated by NULL_TREE. */
8445 tree
8446 build_function_type_list (tree return_type, ...)
8448 tree args;
8449 va_list p;
8451 va_start (p, return_type);
8452 args = build_function_type_list_1 (false, return_type, p);
8453 va_end (p);
8454 return args;
8457 /* Build a variable argument function type. The RETURN_TYPE is the
8458 type returned by the function. If additional arguments are provided,
8459 they are additional argument types. The list of argument types must
8460 always be terminated by NULL_TREE. */
8462 tree
8463 build_varargs_function_type_list (tree return_type, ...)
8465 tree args;
8466 va_list p;
8468 va_start (p, return_type);
8469 args = build_function_type_list_1 (true, return_type, p);
8470 va_end (p);
8472 return args;
8475 /* Build a function type. RETURN_TYPE is the type returned by the
8476 function; VAARGS indicates whether the function takes varargs. The
8477 function takes N named arguments, the types of which are provided in
8478 ARG_TYPES. */
8480 static tree
8481 build_function_type_array_1 (bool vaargs, tree return_type, int n,
8482 tree *arg_types)
8484 int i;
8485 tree t = vaargs ? NULL_TREE : void_list_node;
8487 for (i = n - 1; i >= 0; i--)
8488 t = tree_cons (NULL_TREE, arg_types[i], t);
8490 return build_function_type (return_type, t);
8493 /* Build a function type. RETURN_TYPE is the type returned by the
8494 function. The function takes N named arguments, the types of which
8495 are provided in ARG_TYPES. */
8497 tree
8498 build_function_type_array (tree return_type, int n, tree *arg_types)
8500 return build_function_type_array_1 (false, return_type, n, arg_types);
8503 /* Build a variable argument function type. RETURN_TYPE is the type
8504 returned by the function. The function takes N named arguments, the
8505 types of which are provided in ARG_TYPES. */
8507 tree
8508 build_varargs_function_type_array (tree return_type, int n, tree *arg_types)
8510 return build_function_type_array_1 (true, return_type, n, arg_types);
8513 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
8514 and ARGTYPES (a TREE_LIST) are the return type and arguments types
8515 for the method. An implicit additional parameter (of type
8516 pointer-to-BASETYPE) is added to the ARGTYPES. */
8518 tree
8519 build_method_type_directly (tree basetype,
8520 tree rettype,
8521 tree argtypes)
8523 tree t;
8524 tree ptype;
8525 inchash::hash hstate;
8526 bool any_structural_p, any_noncanonical_p;
8527 tree canon_argtypes;
8529 /* Make a node of the sort we want. */
8530 t = make_node (METHOD_TYPE);
8532 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8533 TREE_TYPE (t) = rettype;
8534 ptype = build_pointer_type (basetype);
8536 /* The actual arglist for this function includes a "hidden" argument
8537 which is "this". Put it into the list of argument types. */
8538 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
8539 TYPE_ARG_TYPES (t) = argtypes;
8541 /* If we already have such a type, use the old one. */
8542 hstate.add_object (TYPE_HASH (basetype));
8543 hstate.add_object (TYPE_HASH (rettype));
8544 type_hash_list (argtypes, hstate);
8545 t = type_hash_canon (hstate.end (), t);
8547 /* Set up the canonical type. */
8548 any_structural_p
8549 = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8550 || TYPE_STRUCTURAL_EQUALITY_P (rettype));
8551 any_noncanonical_p
8552 = (TYPE_CANONICAL (basetype) != basetype
8553 || TYPE_CANONICAL (rettype) != rettype);
8554 canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
8555 &any_structural_p,
8556 &any_noncanonical_p);
8557 if (any_structural_p)
8558 SET_TYPE_STRUCTURAL_EQUALITY (t);
8559 else if (any_noncanonical_p)
8560 TYPE_CANONICAL (t)
8561 = build_method_type_directly (TYPE_CANONICAL (basetype),
8562 TYPE_CANONICAL (rettype),
8563 canon_argtypes);
8564 if (!COMPLETE_TYPE_P (t))
8565 layout_type (t);
8567 return t;
8570 /* Construct, lay out and return the type of methods belonging to class
8571 BASETYPE and whose arguments and values are described by TYPE.
8572 If that type exists already, reuse it.
8573 TYPE must be a FUNCTION_TYPE node. */
8575 tree
8576 build_method_type (tree basetype, tree type)
8578 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8580 return build_method_type_directly (basetype,
8581 TREE_TYPE (type),
8582 TYPE_ARG_TYPES (type));
8585 /* Construct, lay out and return the type of offsets to a value
8586 of type TYPE, within an object of type BASETYPE.
8587 If a suitable offset type exists already, reuse it. */
8589 tree
8590 build_offset_type (tree basetype, tree type)
8592 tree t;
8593 inchash::hash hstate;
8595 /* Make a node of the sort we want. */
8596 t = make_node (OFFSET_TYPE);
8598 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8599 TREE_TYPE (t) = type;
8601 /* If we already have such a type, use the old one. */
8602 hstate.add_object (TYPE_HASH (basetype));
8603 hstate.add_object (TYPE_HASH (type));
8604 t = type_hash_canon (hstate.end (), t);
8606 if (!COMPLETE_TYPE_P (t))
8607 layout_type (t);
8609 if (TYPE_CANONICAL (t) == t)
8611 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8612 || TYPE_STRUCTURAL_EQUALITY_P (type))
8613 SET_TYPE_STRUCTURAL_EQUALITY (t);
8614 else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
8615 || TYPE_CANONICAL (type) != type)
8616 TYPE_CANONICAL (t)
8617 = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
8618 TYPE_CANONICAL (type));
8621 return t;
8624 /* Create a complex type whose components are COMPONENT_TYPE. */
8626 tree
8627 build_complex_type (tree component_type)
8629 tree t;
8630 inchash::hash hstate;
8632 gcc_assert (INTEGRAL_TYPE_P (component_type)
8633 || SCALAR_FLOAT_TYPE_P (component_type)
8634 || FIXED_POINT_TYPE_P (component_type));
8636 /* Make a node of the sort we want. */
8637 t = make_node (COMPLEX_TYPE);
8639 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
8641 /* If we already have such a type, use the old one. */
8642 hstate.add_object (TYPE_HASH (component_type));
8643 t = type_hash_canon (hstate.end (), t);
8645 if (!COMPLETE_TYPE_P (t))
8646 layout_type (t);
8648 if (TYPE_CANONICAL (t) == t)
8650 if (TYPE_STRUCTURAL_EQUALITY_P (component_type))
8651 SET_TYPE_STRUCTURAL_EQUALITY (t);
8652 else if (TYPE_CANONICAL (component_type) != component_type)
8653 TYPE_CANONICAL (t)
8654 = build_complex_type (TYPE_CANONICAL (component_type));
8657 /* We need to create a name, since complex is a fundamental type. */
8658 if (! TYPE_NAME (t))
8660 const char *name;
8661 if (component_type == char_type_node)
8662 name = "complex char";
8663 else if (component_type == signed_char_type_node)
8664 name = "complex signed char";
8665 else if (component_type == unsigned_char_type_node)
8666 name = "complex unsigned char";
8667 else if (component_type == short_integer_type_node)
8668 name = "complex short int";
8669 else if (component_type == short_unsigned_type_node)
8670 name = "complex short unsigned int";
8671 else if (component_type == integer_type_node)
8672 name = "complex int";
8673 else if (component_type == unsigned_type_node)
8674 name = "complex unsigned int";
8675 else if (component_type == long_integer_type_node)
8676 name = "complex long int";
8677 else if (component_type == long_unsigned_type_node)
8678 name = "complex long unsigned int";
8679 else if (component_type == long_long_integer_type_node)
8680 name = "complex long long int";
8681 else if (component_type == long_long_unsigned_type_node)
8682 name = "complex long long unsigned int";
8683 else
8684 name = 0;
8686 if (name != 0)
8687 TYPE_NAME (t) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
8688 get_identifier (name), t);
8691 return build_qualified_type (t, TYPE_QUALS (component_type));
8694 /* If TYPE is a real or complex floating-point type and the target
8695 does not directly support arithmetic on TYPE then return the wider
8696 type to be used for arithmetic on TYPE. Otherwise, return
8697 NULL_TREE. */
8699 tree
8700 excess_precision_type (tree type)
8702 if (flag_excess_precision != EXCESS_PRECISION_FAST)
8704 int flt_eval_method = TARGET_FLT_EVAL_METHOD;
8705 switch (TREE_CODE (type))
8707 case REAL_TYPE:
8708 switch (flt_eval_method)
8710 case 1:
8711 if (TYPE_MODE (type) == TYPE_MODE (float_type_node))
8712 return double_type_node;
8713 break;
8714 case 2:
8715 if (TYPE_MODE (type) == TYPE_MODE (float_type_node)
8716 || TYPE_MODE (type) == TYPE_MODE (double_type_node))
8717 return long_double_type_node;
8718 break;
8719 default:
8720 gcc_unreachable ();
8722 break;
8723 case COMPLEX_TYPE:
8724 if (TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
8725 return NULL_TREE;
8726 switch (flt_eval_method)
8728 case 1:
8729 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node))
8730 return complex_double_type_node;
8731 break;
8732 case 2:
8733 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node)
8734 || (TYPE_MODE (TREE_TYPE (type))
8735 == TYPE_MODE (double_type_node)))
8736 return complex_long_double_type_node;
8737 break;
8738 default:
8739 gcc_unreachable ();
8741 break;
8742 default:
8743 break;
8746 return NULL_TREE;
8749 /* Return OP, stripped of any conversions to wider types as much as is safe.
8750 Converting the value back to OP's type makes a value equivalent to OP.
8752 If FOR_TYPE is nonzero, we return a value which, if converted to
8753 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
8755 OP must have integer, real or enumeral type. Pointers are not allowed!
8757 There are some cases where the obvious value we could return
8758 would regenerate to OP if converted to OP's type,
8759 but would not extend like OP to wider types.
8760 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
8761 For example, if OP is (unsigned short)(signed char)-1,
8762 we avoid returning (signed char)-1 if FOR_TYPE is int,
8763 even though extending that to an unsigned short would regenerate OP,
8764 since the result of extending (signed char)-1 to (int)
8765 is different from (int) OP. */
8767 tree
8768 get_unwidened (tree op, tree for_type)
8770 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
8771 tree type = TREE_TYPE (op);
8772 unsigned final_prec
8773 = TYPE_PRECISION (for_type != 0 ? for_type : type);
8774 int uns
8775 = (for_type != 0 && for_type != type
8776 && final_prec > TYPE_PRECISION (type)
8777 && TYPE_UNSIGNED (type));
8778 tree win = op;
8780 while (CONVERT_EXPR_P (op))
8782 int bitschange;
8784 /* TYPE_PRECISION on vector types has different meaning
8785 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
8786 so avoid them here. */
8787 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
8788 break;
8790 bitschange = TYPE_PRECISION (TREE_TYPE (op))
8791 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
8793 /* Truncations are many-one so cannot be removed.
8794 Unless we are later going to truncate down even farther. */
8795 if (bitschange < 0
8796 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
8797 break;
8799 /* See what's inside this conversion. If we decide to strip it,
8800 we will set WIN. */
8801 op = TREE_OPERAND (op, 0);
8803 /* If we have not stripped any zero-extensions (uns is 0),
8804 we can strip any kind of extension.
8805 If we have previously stripped a zero-extension,
8806 only zero-extensions can safely be stripped.
8807 Any extension can be stripped if the bits it would produce
8808 are all going to be discarded later by truncating to FOR_TYPE. */
8810 if (bitschange > 0)
8812 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
8813 win = op;
8814 /* TYPE_UNSIGNED says whether this is a zero-extension.
8815 Let's avoid computing it if it does not affect WIN
8816 and if UNS will not be needed again. */
8817 if ((uns
8818 || CONVERT_EXPR_P (op))
8819 && TYPE_UNSIGNED (TREE_TYPE (op)))
8821 uns = 1;
8822 win = op;
8827 /* If we finally reach a constant see if it fits in for_type and
8828 in that case convert it. */
8829 if (for_type
8830 && TREE_CODE (win) == INTEGER_CST
8831 && TREE_TYPE (win) != for_type
8832 && int_fits_type_p (win, for_type))
8833 win = fold_convert (for_type, win);
8835 return win;
8838 /* Return OP or a simpler expression for a narrower value
8839 which can be sign-extended or zero-extended to give back OP.
8840 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
8841 or 0 if the value should be sign-extended. */
8843 tree
8844 get_narrower (tree op, int *unsignedp_ptr)
8846 int uns = 0;
8847 int first = 1;
8848 tree win = op;
8849 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
8851 while (TREE_CODE (op) == NOP_EXPR)
8853 int bitschange
8854 = (TYPE_PRECISION (TREE_TYPE (op))
8855 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
8857 /* Truncations are many-one so cannot be removed. */
8858 if (bitschange < 0)
8859 break;
8861 /* See what's inside this conversion. If we decide to strip it,
8862 we will set WIN. */
8864 if (bitschange > 0)
8866 op = TREE_OPERAND (op, 0);
8867 /* An extension: the outermost one can be stripped,
8868 but remember whether it is zero or sign extension. */
8869 if (first)
8870 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8871 /* Otherwise, if a sign extension has been stripped,
8872 only sign extensions can now be stripped;
8873 if a zero extension has been stripped, only zero-extensions. */
8874 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
8875 break;
8876 first = 0;
8878 else /* bitschange == 0 */
8880 /* A change in nominal type can always be stripped, but we must
8881 preserve the unsignedness. */
8882 if (first)
8883 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8884 first = 0;
8885 op = TREE_OPERAND (op, 0);
8886 /* Keep trying to narrow, but don't assign op to win if it
8887 would turn an integral type into something else. */
8888 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
8889 continue;
8892 win = op;
8895 if (TREE_CODE (op) == COMPONENT_REF
8896 /* Since type_for_size always gives an integer type. */
8897 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
8898 && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
8899 /* Ensure field is laid out already. */
8900 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
8901 && tree_fits_uhwi_p (DECL_SIZE (TREE_OPERAND (op, 1))))
8903 unsigned HOST_WIDE_INT innerprec
8904 = tree_to_uhwi (DECL_SIZE (TREE_OPERAND (op, 1)));
8905 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
8906 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
8907 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
8909 /* We can get this structure field in a narrower type that fits it,
8910 but the resulting extension to its nominal type (a fullword type)
8911 must satisfy the same conditions as for other extensions.
8913 Do this only for fields that are aligned (not bit-fields),
8914 because when bit-field insns will be used there is no
8915 advantage in doing this. */
8917 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
8918 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
8919 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
8920 && type != 0)
8922 if (first)
8923 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
8924 win = fold_convert (type, op);
8928 *unsignedp_ptr = uns;
8929 return win;
8932 /* Returns true if integer constant C has a value that is permissible
8933 for type TYPE (an INTEGER_TYPE). */
8935 bool
8936 int_fits_type_p (const_tree c, const_tree type)
8938 tree type_low_bound, type_high_bound;
8939 bool ok_for_low_bound, ok_for_high_bound;
8940 signop sgn_c = TYPE_SIGN (TREE_TYPE (c));
8942 retry:
8943 type_low_bound = TYPE_MIN_VALUE (type);
8944 type_high_bound = TYPE_MAX_VALUE (type);
8946 /* If at least one bound of the type is a constant integer, we can check
8947 ourselves and maybe make a decision. If no such decision is possible, but
8948 this type is a subtype, try checking against that. Otherwise, use
8949 fits_to_tree_p, which checks against the precision.
8951 Compute the status for each possibly constant bound, and return if we see
8952 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
8953 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
8954 for "constant known to fit". */
8956 /* Check if c >= type_low_bound. */
8957 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
8959 if (tree_int_cst_lt (c, type_low_bound))
8960 return false;
8961 ok_for_low_bound = true;
8963 else
8964 ok_for_low_bound = false;
8966 /* Check if c <= type_high_bound. */
8967 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
8969 if (tree_int_cst_lt (type_high_bound, c))
8970 return false;
8971 ok_for_high_bound = true;
8973 else
8974 ok_for_high_bound = false;
8976 /* If the constant fits both bounds, the result is known. */
8977 if (ok_for_low_bound && ok_for_high_bound)
8978 return true;
8980 /* Perform some generic filtering which may allow making a decision
8981 even if the bounds are not constant. First, negative integers
8982 never fit in unsigned types, */
8983 if (TYPE_UNSIGNED (type) && sgn_c == SIGNED && wi::neg_p (c))
8984 return false;
8986 /* Second, narrower types always fit in wider ones. */
8987 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
8988 return true;
8990 /* Third, unsigned integers with top bit set never fit signed types. */
8991 if (!TYPE_UNSIGNED (type) && sgn_c == UNSIGNED)
8993 int prec = GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (c))) - 1;
8994 if (prec < TYPE_PRECISION (TREE_TYPE (c)))
8996 /* When a tree_cst is converted to a wide-int, the precision
8997 is taken from the type. However, if the precision of the
8998 mode underneath the type is smaller than that, it is
8999 possible that the value will not fit. The test below
9000 fails if any bit is set between the sign bit of the
9001 underlying mode and the top bit of the type. */
9002 if (wi::ne_p (wi::zext (c, prec - 1), c))
9003 return false;
9005 else if (wi::neg_p (c))
9006 return false;
9009 /* If we haven't been able to decide at this point, there nothing more we
9010 can check ourselves here. Look at the base type if we have one and it
9011 has the same precision. */
9012 if (TREE_CODE (type) == INTEGER_TYPE
9013 && TREE_TYPE (type) != 0
9014 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
9016 type = TREE_TYPE (type);
9017 goto retry;
9020 /* Or to fits_to_tree_p, if nothing else. */
9021 return wi::fits_to_tree_p (c, type);
9024 /* Stores bounds of an integer TYPE in MIN and MAX. If TYPE has non-constant
9025 bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
9026 represented (assuming two's-complement arithmetic) within the bit
9027 precision of the type are returned instead. */
9029 void
9030 get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
9032 if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
9033 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
9034 wi::to_mpz (TYPE_MIN_VALUE (type), min, TYPE_SIGN (type));
9035 else
9037 if (TYPE_UNSIGNED (type))
9038 mpz_set_ui (min, 0);
9039 else
9041 wide_int mn = wi::min_value (TYPE_PRECISION (type), SIGNED);
9042 wi::to_mpz (mn, min, SIGNED);
9046 if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type)
9047 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
9048 wi::to_mpz (TYPE_MAX_VALUE (type), max, TYPE_SIGN (type));
9049 else
9051 wide_int mn = wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type));
9052 wi::to_mpz (mn, max, TYPE_SIGN (type));
9056 /* Return true if VAR is an automatic variable defined in function FN. */
9058 bool
9059 auto_var_in_fn_p (const_tree var, const_tree fn)
9061 return (DECL_P (var) && DECL_CONTEXT (var) == fn
9062 && ((((TREE_CODE (var) == VAR_DECL && ! DECL_EXTERNAL (var))
9063 || TREE_CODE (var) == PARM_DECL)
9064 && ! TREE_STATIC (var))
9065 || TREE_CODE (var) == LABEL_DECL
9066 || TREE_CODE (var) == RESULT_DECL));
9069 /* Subprogram of following function. Called by walk_tree.
9071 Return *TP if it is an automatic variable or parameter of the
9072 function passed in as DATA. */
9074 static tree
9075 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
9077 tree fn = (tree) data;
9079 if (TYPE_P (*tp))
9080 *walk_subtrees = 0;
9082 else if (DECL_P (*tp)
9083 && auto_var_in_fn_p (*tp, fn))
9084 return *tp;
9086 return NULL_TREE;
9089 /* Returns true if T is, contains, or refers to a type with variable
9090 size. For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
9091 arguments, but not the return type. If FN is nonzero, only return
9092 true if a modifier of the type or position of FN is a variable or
9093 parameter inside FN.
9095 This concept is more general than that of C99 'variably modified types':
9096 in C99, a struct type is never variably modified because a VLA may not
9097 appear as a structure member. However, in GNU C code like:
9099 struct S { int i[f()]; };
9101 is valid, and other languages may define similar constructs. */
9103 bool
9104 variably_modified_type_p (tree type, tree fn)
9106 tree t;
9108 /* Test if T is either variable (if FN is zero) or an expression containing
9109 a variable in FN. If TYPE isn't gimplified, return true also if
9110 gimplify_one_sizepos would gimplify the expression into a local
9111 variable. */
9112 #define RETURN_TRUE_IF_VAR(T) \
9113 do { tree _t = (T); \
9114 if (_t != NULL_TREE \
9115 && _t != error_mark_node \
9116 && TREE_CODE (_t) != INTEGER_CST \
9117 && TREE_CODE (_t) != PLACEHOLDER_EXPR \
9118 && (!fn \
9119 || (!TYPE_SIZES_GIMPLIFIED (type) \
9120 && !is_gimple_sizepos (_t)) \
9121 || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
9122 return true; } while (0)
9124 if (type == error_mark_node)
9125 return false;
9127 /* If TYPE itself has variable size, it is variably modified. */
9128 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
9129 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
9131 switch (TREE_CODE (type))
9133 case POINTER_TYPE:
9134 case REFERENCE_TYPE:
9135 case VECTOR_TYPE:
9136 if (variably_modified_type_p (TREE_TYPE (type), fn))
9137 return true;
9138 break;
9140 case FUNCTION_TYPE:
9141 case METHOD_TYPE:
9142 /* If TYPE is a function type, it is variably modified if the
9143 return type is variably modified. */
9144 if (variably_modified_type_p (TREE_TYPE (type), fn))
9145 return true;
9146 break;
9148 case INTEGER_TYPE:
9149 case REAL_TYPE:
9150 case FIXED_POINT_TYPE:
9151 case ENUMERAL_TYPE:
9152 case BOOLEAN_TYPE:
9153 /* Scalar types are variably modified if their end points
9154 aren't constant. */
9155 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
9156 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
9157 break;
9159 case RECORD_TYPE:
9160 case UNION_TYPE:
9161 case QUAL_UNION_TYPE:
9162 /* We can't see if any of the fields are variably-modified by the
9163 definition we normally use, since that would produce infinite
9164 recursion via pointers. */
9165 /* This is variably modified if some field's type is. */
9166 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
9167 if (TREE_CODE (t) == FIELD_DECL)
9169 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
9170 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
9171 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
9173 if (TREE_CODE (type) == QUAL_UNION_TYPE)
9174 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
9176 break;
9178 case ARRAY_TYPE:
9179 /* Do not call ourselves to avoid infinite recursion. This is
9180 variably modified if the element type is. */
9181 RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
9182 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
9183 break;
9185 default:
9186 break;
9189 /* The current language may have other cases to check, but in general,
9190 all other types are not variably modified. */
9191 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
9193 #undef RETURN_TRUE_IF_VAR
9196 /* Given a DECL or TYPE, return the scope in which it was declared, or
9197 NULL_TREE if there is no containing scope. */
9199 tree
9200 get_containing_scope (const_tree t)
9202 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
9205 /* Return the innermost context enclosing DECL that is
9206 a FUNCTION_DECL, or zero if none. */
9208 tree
9209 decl_function_context (const_tree decl)
9211 tree context;
9213 if (TREE_CODE (decl) == ERROR_MARK)
9214 return 0;
9216 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
9217 where we look up the function at runtime. Such functions always take
9218 a first argument of type 'pointer to real context'.
9220 C++ should really be fixed to use DECL_CONTEXT for the real context,
9221 and use something else for the "virtual context". */
9222 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
9223 context
9224 = TYPE_MAIN_VARIANT
9225 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
9226 else
9227 context = DECL_CONTEXT (decl);
9229 while (context && TREE_CODE (context) != FUNCTION_DECL)
9231 if (TREE_CODE (context) == BLOCK)
9232 context = BLOCK_SUPERCONTEXT (context);
9233 else
9234 context = get_containing_scope (context);
9237 return context;
9240 /* Return the innermost context enclosing DECL that is
9241 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
9242 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
9244 tree
9245 decl_type_context (const_tree decl)
9247 tree context = DECL_CONTEXT (decl);
9249 while (context)
9250 switch (TREE_CODE (context))
9252 case NAMESPACE_DECL:
9253 case TRANSLATION_UNIT_DECL:
9254 return NULL_TREE;
9256 case RECORD_TYPE:
9257 case UNION_TYPE:
9258 case QUAL_UNION_TYPE:
9259 return context;
9261 case TYPE_DECL:
9262 case FUNCTION_DECL:
9263 context = DECL_CONTEXT (context);
9264 break;
9266 case BLOCK:
9267 context = BLOCK_SUPERCONTEXT (context);
9268 break;
9270 default:
9271 gcc_unreachable ();
9274 return NULL_TREE;
9277 /* CALL is a CALL_EXPR. Return the declaration for the function
9278 called, or NULL_TREE if the called function cannot be
9279 determined. */
9281 tree
9282 get_callee_fndecl (const_tree call)
9284 tree addr;
9286 if (call == error_mark_node)
9287 return error_mark_node;
9289 /* It's invalid to call this function with anything but a
9290 CALL_EXPR. */
9291 gcc_assert (TREE_CODE (call) == CALL_EXPR);
9293 /* The first operand to the CALL is the address of the function
9294 called. */
9295 addr = CALL_EXPR_FN (call);
9297 /* If there is no function, return early. */
9298 if (addr == NULL_TREE)
9299 return NULL_TREE;
9301 STRIP_NOPS (addr);
9303 /* If this is a readonly function pointer, extract its initial value. */
9304 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
9305 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
9306 && DECL_INITIAL (addr))
9307 addr = DECL_INITIAL (addr);
9309 /* If the address is just `&f' for some function `f', then we know
9310 that `f' is being called. */
9311 if (TREE_CODE (addr) == ADDR_EXPR
9312 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
9313 return TREE_OPERAND (addr, 0);
9315 /* We couldn't figure out what was being called. */
9316 return NULL_TREE;
9319 #define TREE_MEM_USAGE_SPACES 40
9321 /* Print debugging information about tree nodes generated during the compile,
9322 and any language-specific information. */
9324 void
9325 dump_tree_statistics (void)
9327 if (GATHER_STATISTICS)
9329 int i;
9330 int total_nodes, total_bytes;
9331 fprintf (stderr, "\nKind Nodes Bytes\n");
9332 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
9333 total_nodes = total_bytes = 0;
9334 for (i = 0; i < (int) all_kinds; i++)
9336 fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
9337 tree_node_counts[i], tree_node_sizes[i]);
9338 total_nodes += tree_node_counts[i];
9339 total_bytes += tree_node_sizes[i];
9341 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
9342 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
9343 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
9344 fprintf (stderr, "Code Nodes\n");
9345 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
9346 for (i = 0; i < (int) MAX_TREE_CODES; i++)
9347 fprintf (stderr, "%-32s %7d\n", get_tree_code_name ((enum tree_code) i),
9348 tree_code_counts[i]);
9349 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
9350 fprintf (stderr, "\n");
9351 ssanames_print_statistics ();
9352 fprintf (stderr, "\n");
9353 phinodes_print_statistics ();
9354 fprintf (stderr, "\n");
9356 else
9357 fprintf (stderr, "(No per-node statistics)\n");
9359 print_type_hash_statistics ();
9360 print_debug_expr_statistics ();
9361 print_value_expr_statistics ();
9362 lang_hooks.print_statistics ();
9365 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
9367 /* Generate a crc32 of a byte. */
9369 static unsigned
9370 crc32_unsigned_bits (unsigned chksum, unsigned value, unsigned bits)
9372 unsigned ix;
9374 for (ix = bits; ix--; value <<= 1)
9376 unsigned feedback;
9378 feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
9379 chksum <<= 1;
9380 chksum ^= feedback;
9382 return chksum;
9385 /* Generate a crc32 of a 32-bit unsigned. */
9387 unsigned
9388 crc32_unsigned (unsigned chksum, unsigned value)
9390 return crc32_unsigned_bits (chksum, value, 32);
9393 /* Generate a crc32 of a byte. */
9395 unsigned
9396 crc32_byte (unsigned chksum, char byte)
9398 return crc32_unsigned_bits (chksum, (unsigned) byte << 24, 8);
9401 /* Generate a crc32 of a string. */
9403 unsigned
9404 crc32_string (unsigned chksum, const char *string)
9408 chksum = crc32_byte (chksum, *string);
9410 while (*string++);
9411 return chksum;
9414 /* P is a string that will be used in a symbol. Mask out any characters
9415 that are not valid in that context. */
9417 void
9418 clean_symbol_name (char *p)
9420 for (; *p; p++)
9421 if (! (ISALNUM (*p)
9422 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
9423 || *p == '$'
9424 #endif
9425 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
9426 || *p == '.'
9427 #endif
9429 *p = '_';
9432 /* For anonymous aggregate types, we need some sort of name to
9433 hold on to. In practice, this should not appear, but it should
9434 not be harmful if it does. */
9435 bool
9436 anon_aggrname_p(const_tree id_node)
9438 #ifndef NO_DOT_IN_LABEL
9439 return (IDENTIFIER_POINTER (id_node)[0] == '.'
9440 && IDENTIFIER_POINTER (id_node)[1] == '_');
9441 #else /* NO_DOT_IN_LABEL */
9442 #ifndef NO_DOLLAR_IN_LABEL
9443 return (IDENTIFIER_POINTER (id_node)[0] == '$' \
9444 && IDENTIFIER_POINTER (id_node)[1] == '_');
9445 #else /* NO_DOLLAR_IN_LABEL */
9446 #define ANON_AGGRNAME_PREFIX "__anon_"
9447 return (!strncmp (IDENTIFIER_POINTER (id_node), ANON_AGGRNAME_PREFIX,
9448 sizeof (ANON_AGGRNAME_PREFIX) - 1));
9449 #endif /* NO_DOLLAR_IN_LABEL */
9450 #endif /* NO_DOT_IN_LABEL */
9453 /* Return a format for an anonymous aggregate name. */
9454 const char *
9455 anon_aggrname_format()
9457 #ifndef NO_DOT_IN_LABEL
9458 return "._%d";
9459 #else /* NO_DOT_IN_LABEL */
9460 #ifndef NO_DOLLAR_IN_LABEL
9461 return "$_%d";
9462 #else /* NO_DOLLAR_IN_LABEL */
9463 return "__anon_%d";
9464 #endif /* NO_DOLLAR_IN_LABEL */
9465 #endif /* NO_DOT_IN_LABEL */
9468 /* Generate a name for a special-purpose function.
9469 The generated name may need to be unique across the whole link.
9470 Changes to this function may also require corresponding changes to
9471 xstrdup_mask_random.
9472 TYPE is some string to identify the purpose of this function to the
9473 linker or collect2; it must start with an uppercase letter,
9474 one of:
9475 I - for constructors
9476 D - for destructors
9477 N - for C++ anonymous namespaces
9478 F - for DWARF unwind frame information. */
9480 tree
9481 get_file_function_name (const char *type)
9483 char *buf;
9484 const char *p;
9485 char *q;
9487 /* If we already have a name we know to be unique, just use that. */
9488 if (first_global_object_name)
9489 p = q = ASTRDUP (first_global_object_name);
9490 /* If the target is handling the constructors/destructors, they
9491 will be local to this file and the name is only necessary for
9492 debugging purposes.
9493 We also assign sub_I and sub_D sufixes to constructors called from
9494 the global static constructors. These are always local. */
9495 else if (((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
9496 || (strncmp (type, "sub_", 4) == 0
9497 && (type[4] == 'I' || type[4] == 'D')))
9499 const char *file = main_input_filename;
9500 if (! file)
9501 file = LOCATION_FILE (input_location);
9502 /* Just use the file's basename, because the full pathname
9503 might be quite long. */
9504 p = q = ASTRDUP (lbasename (file));
9506 else
9508 /* Otherwise, the name must be unique across the entire link.
9509 We don't have anything that we know to be unique to this translation
9510 unit, so use what we do have and throw in some randomness. */
9511 unsigned len;
9512 const char *name = weak_global_object_name;
9513 const char *file = main_input_filename;
9515 if (! name)
9516 name = "";
9517 if (! file)
9518 file = LOCATION_FILE (input_location);
9520 len = strlen (file);
9521 q = (char *) alloca (9 + 17 + len + 1);
9522 memcpy (q, file, len + 1);
9524 snprintf (q + len, 9 + 17 + 1, "_%08X_" HOST_WIDE_INT_PRINT_HEX,
9525 crc32_string (0, name), get_random_seed (false));
9527 p = q;
9530 clean_symbol_name (q);
9531 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
9532 + strlen (type));
9534 /* Set up the name of the file-level functions we may need.
9535 Use a global object (which is already required to be unique over
9536 the program) rather than the file name (which imposes extra
9537 constraints). */
9538 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
9540 return get_identifier (buf);
9543 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
9545 /* Complain that the tree code of NODE does not match the expected 0
9546 terminated list of trailing codes. The trailing code list can be
9547 empty, for a more vague error message. FILE, LINE, and FUNCTION
9548 are of the caller. */
9550 void
9551 tree_check_failed (const_tree node, const char *file,
9552 int line, const char *function, ...)
9554 va_list args;
9555 const char *buffer;
9556 unsigned length = 0;
9557 enum tree_code code;
9559 va_start (args, function);
9560 while ((code = (enum tree_code) va_arg (args, int)))
9561 length += 4 + strlen (get_tree_code_name (code));
9562 va_end (args);
9563 if (length)
9565 char *tmp;
9566 va_start (args, function);
9567 length += strlen ("expected ");
9568 buffer = tmp = (char *) alloca (length);
9569 length = 0;
9570 while ((code = (enum tree_code) va_arg (args, int)))
9572 const char *prefix = length ? " or " : "expected ";
9574 strcpy (tmp + length, prefix);
9575 length += strlen (prefix);
9576 strcpy (tmp + length, get_tree_code_name (code));
9577 length += strlen (get_tree_code_name (code));
9579 va_end (args);
9581 else
9582 buffer = "unexpected node";
9584 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9585 buffer, get_tree_code_name (TREE_CODE (node)),
9586 function, trim_filename (file), line);
9589 /* Complain that the tree code of NODE does match the expected 0
9590 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
9591 the caller. */
9593 void
9594 tree_not_check_failed (const_tree node, const char *file,
9595 int line, const char *function, ...)
9597 va_list args;
9598 char *buffer;
9599 unsigned length = 0;
9600 enum tree_code code;
9602 va_start (args, function);
9603 while ((code = (enum tree_code) va_arg (args, int)))
9604 length += 4 + strlen (get_tree_code_name (code));
9605 va_end (args);
9606 va_start (args, function);
9607 buffer = (char *) alloca (length);
9608 length = 0;
9609 while ((code = (enum tree_code) va_arg (args, int)))
9611 if (length)
9613 strcpy (buffer + length, " or ");
9614 length += 4;
9616 strcpy (buffer + length, get_tree_code_name (code));
9617 length += strlen (get_tree_code_name (code));
9619 va_end (args);
9621 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
9622 buffer, get_tree_code_name (TREE_CODE (node)),
9623 function, trim_filename (file), line);
9626 /* Similar to tree_check_failed, except that we check for a class of tree
9627 code, given in CL. */
9629 void
9630 tree_class_check_failed (const_tree node, const enum tree_code_class cl,
9631 const char *file, int line, const char *function)
9633 internal_error
9634 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
9635 TREE_CODE_CLASS_STRING (cl),
9636 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9637 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9640 /* Similar to tree_check_failed, except that instead of specifying a
9641 dozen codes, use the knowledge that they're all sequential. */
9643 void
9644 tree_range_check_failed (const_tree node, const char *file, int line,
9645 const char *function, enum tree_code c1,
9646 enum tree_code c2)
9648 char *buffer;
9649 unsigned length = 0;
9650 unsigned int c;
9652 for (c = c1; c <= c2; ++c)
9653 length += 4 + strlen (get_tree_code_name ((enum tree_code) c));
9655 length += strlen ("expected ");
9656 buffer = (char *) alloca (length);
9657 length = 0;
9659 for (c = c1; c <= c2; ++c)
9661 const char *prefix = length ? " or " : "expected ";
9663 strcpy (buffer + length, prefix);
9664 length += strlen (prefix);
9665 strcpy (buffer + length, get_tree_code_name ((enum tree_code) c));
9666 length += strlen (get_tree_code_name ((enum tree_code) c));
9669 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9670 buffer, get_tree_code_name (TREE_CODE (node)),
9671 function, trim_filename (file), line);
9675 /* Similar to tree_check_failed, except that we check that a tree does
9676 not have the specified code, given in CL. */
9678 void
9679 tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
9680 const char *file, int line, const char *function)
9682 internal_error
9683 ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
9684 TREE_CODE_CLASS_STRING (cl),
9685 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9686 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9690 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes. */
9692 void
9693 omp_clause_check_failed (const_tree node, const char *file, int line,
9694 const char *function, enum omp_clause_code code)
9696 internal_error ("tree check: expected omp_clause %s, have %s in %s, at %s:%d",
9697 omp_clause_code_name[code], get_tree_code_name (TREE_CODE (node)),
9698 function, trim_filename (file), line);
9702 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes. */
9704 void
9705 omp_clause_range_check_failed (const_tree node, const char *file, int line,
9706 const char *function, enum omp_clause_code c1,
9707 enum omp_clause_code c2)
9709 char *buffer;
9710 unsigned length = 0;
9711 unsigned int c;
9713 for (c = c1; c <= c2; ++c)
9714 length += 4 + strlen (omp_clause_code_name[c]);
9716 length += strlen ("expected ");
9717 buffer = (char *) alloca (length);
9718 length = 0;
9720 for (c = c1; c <= c2; ++c)
9722 const char *prefix = length ? " or " : "expected ";
9724 strcpy (buffer + length, prefix);
9725 length += strlen (prefix);
9726 strcpy (buffer + length, omp_clause_code_name[c]);
9727 length += strlen (omp_clause_code_name[c]);
9730 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9731 buffer, omp_clause_code_name[TREE_CODE (node)],
9732 function, trim_filename (file), line);
9736 #undef DEFTREESTRUCT
9737 #define DEFTREESTRUCT(VAL, NAME) NAME,
9739 static const char *ts_enum_names[] = {
9740 #include "treestruct.def"
9742 #undef DEFTREESTRUCT
9744 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
9746 /* Similar to tree_class_check_failed, except that we check for
9747 whether CODE contains the tree structure identified by EN. */
9749 void
9750 tree_contains_struct_check_failed (const_tree node,
9751 const enum tree_node_structure_enum en,
9752 const char *file, int line,
9753 const char *function)
9755 internal_error
9756 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
9757 TS_ENUM_NAME (en),
9758 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9762 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9763 (dynamically sized) vector. */
9765 void
9766 tree_int_cst_elt_check_failed (int idx, int len, const char *file, int line,
9767 const char *function)
9769 internal_error
9770 ("tree check: accessed elt %d of tree_int_cst with %d elts in %s, at %s:%d",
9771 idx + 1, len, function, trim_filename (file), line);
9774 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9775 (dynamically sized) vector. */
9777 void
9778 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
9779 const char *function)
9781 internal_error
9782 ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
9783 idx + 1, len, function, trim_filename (file), line);
9786 /* Similar to above, except that the check is for the bounds of the operand
9787 vector of an expression node EXP. */
9789 void
9790 tree_operand_check_failed (int idx, const_tree exp, const char *file,
9791 int line, const char *function)
9793 enum tree_code code = TREE_CODE (exp);
9794 internal_error
9795 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
9796 idx + 1, get_tree_code_name (code), TREE_OPERAND_LENGTH (exp),
9797 function, trim_filename (file), line);
9800 /* Similar to above, except that the check is for the number of
9801 operands of an OMP_CLAUSE node. */
9803 void
9804 omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
9805 int line, const char *function)
9807 internal_error
9808 ("tree check: accessed operand %d of omp_clause %s with %d operands "
9809 "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
9810 omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
9811 trim_filename (file), line);
9813 #endif /* ENABLE_TREE_CHECKING */
9815 /* Create a new vector type node holding SUBPARTS units of type INNERTYPE,
9816 and mapped to the machine mode MODE. Initialize its fields and build
9817 the information necessary for debugging output. */
9819 static tree
9820 make_vector_type (tree innertype, int nunits, machine_mode mode)
9822 tree t;
9823 inchash::hash hstate;
9825 t = make_node (VECTOR_TYPE);
9826 TREE_TYPE (t) = TYPE_MAIN_VARIANT (innertype);
9827 SET_TYPE_VECTOR_SUBPARTS (t, nunits);
9828 SET_TYPE_MODE (t, mode);
9830 if (TYPE_STRUCTURAL_EQUALITY_P (innertype))
9831 SET_TYPE_STRUCTURAL_EQUALITY (t);
9832 else if ((TYPE_CANONICAL (innertype) != innertype
9833 || mode != VOIDmode)
9834 && !VECTOR_BOOLEAN_TYPE_P (t))
9835 TYPE_CANONICAL (t)
9836 = make_vector_type (TYPE_CANONICAL (innertype), nunits, VOIDmode);
9838 layout_type (t);
9840 hstate.add_wide_int (VECTOR_TYPE);
9841 hstate.add_wide_int (nunits);
9842 hstate.add_wide_int (mode);
9843 hstate.add_object (TYPE_HASH (TREE_TYPE (t)));
9844 t = type_hash_canon (hstate.end (), t);
9846 /* We have built a main variant, based on the main variant of the
9847 inner type. Use it to build the variant we return. */
9848 if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
9849 && TREE_TYPE (t) != innertype)
9850 return build_type_attribute_qual_variant (t,
9851 TYPE_ATTRIBUTES (innertype),
9852 TYPE_QUALS (innertype));
9854 return t;
9857 static tree
9858 make_or_reuse_type (unsigned size, int unsignedp)
9860 int i;
9862 if (size == INT_TYPE_SIZE)
9863 return unsignedp ? unsigned_type_node : integer_type_node;
9864 if (size == CHAR_TYPE_SIZE)
9865 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
9866 if (size == SHORT_TYPE_SIZE)
9867 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
9868 if (size == LONG_TYPE_SIZE)
9869 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
9870 if (size == LONG_LONG_TYPE_SIZE)
9871 return (unsignedp ? long_long_unsigned_type_node
9872 : long_long_integer_type_node);
9874 for (i = 0; i < NUM_INT_N_ENTS; i ++)
9875 if (size == int_n_data[i].bitsize
9876 && int_n_enabled_p[i])
9877 return (unsignedp ? int_n_trees[i].unsigned_type
9878 : int_n_trees[i].signed_type);
9880 if (unsignedp)
9881 return make_unsigned_type (size);
9882 else
9883 return make_signed_type (size);
9886 /* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP. */
9888 static tree
9889 make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
9891 if (satp)
9893 if (size == SHORT_FRACT_TYPE_SIZE)
9894 return unsignedp ? sat_unsigned_short_fract_type_node
9895 : sat_short_fract_type_node;
9896 if (size == FRACT_TYPE_SIZE)
9897 return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
9898 if (size == LONG_FRACT_TYPE_SIZE)
9899 return unsignedp ? sat_unsigned_long_fract_type_node
9900 : sat_long_fract_type_node;
9901 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9902 return unsignedp ? sat_unsigned_long_long_fract_type_node
9903 : sat_long_long_fract_type_node;
9905 else
9907 if (size == SHORT_FRACT_TYPE_SIZE)
9908 return unsignedp ? unsigned_short_fract_type_node
9909 : short_fract_type_node;
9910 if (size == FRACT_TYPE_SIZE)
9911 return unsignedp ? unsigned_fract_type_node : fract_type_node;
9912 if (size == LONG_FRACT_TYPE_SIZE)
9913 return unsignedp ? unsigned_long_fract_type_node
9914 : long_fract_type_node;
9915 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9916 return unsignedp ? unsigned_long_long_fract_type_node
9917 : long_long_fract_type_node;
9920 return make_fract_type (size, unsignedp, satp);
9923 /* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP. */
9925 static tree
9926 make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
9928 if (satp)
9930 if (size == SHORT_ACCUM_TYPE_SIZE)
9931 return unsignedp ? sat_unsigned_short_accum_type_node
9932 : sat_short_accum_type_node;
9933 if (size == ACCUM_TYPE_SIZE)
9934 return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
9935 if (size == LONG_ACCUM_TYPE_SIZE)
9936 return unsignedp ? sat_unsigned_long_accum_type_node
9937 : sat_long_accum_type_node;
9938 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9939 return unsignedp ? sat_unsigned_long_long_accum_type_node
9940 : sat_long_long_accum_type_node;
9942 else
9944 if (size == SHORT_ACCUM_TYPE_SIZE)
9945 return unsignedp ? unsigned_short_accum_type_node
9946 : short_accum_type_node;
9947 if (size == ACCUM_TYPE_SIZE)
9948 return unsignedp ? unsigned_accum_type_node : accum_type_node;
9949 if (size == LONG_ACCUM_TYPE_SIZE)
9950 return unsignedp ? unsigned_long_accum_type_node
9951 : long_accum_type_node;
9952 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9953 return unsignedp ? unsigned_long_long_accum_type_node
9954 : long_long_accum_type_node;
9957 return make_accum_type (size, unsignedp, satp);
9961 /* Create an atomic variant node for TYPE. This routine is called
9962 during initialization of data types to create the 5 basic atomic
9963 types. The generic build_variant_type function requires these to
9964 already be set up in order to function properly, so cannot be
9965 called from there. If ALIGN is non-zero, then ensure alignment is
9966 overridden to this value. */
9968 static tree
9969 build_atomic_base (tree type, unsigned int align)
9971 tree t;
9973 /* Make sure its not already registered. */
9974 if ((t = get_qualified_type (type, TYPE_QUAL_ATOMIC)))
9975 return t;
9977 t = build_variant_type_copy (type);
9978 set_type_quals (t, TYPE_QUAL_ATOMIC);
9980 if (align)
9981 TYPE_ALIGN (t) = align;
9983 return t;
9986 /* Create nodes for all integer types (and error_mark_node) using the sizes
9987 of C datatypes. SIGNED_CHAR specifies whether char is signed,
9988 SHORT_DOUBLE specifies whether double should be of the same precision
9989 as float. */
9991 void
9992 build_common_tree_nodes (bool signed_char, bool short_double)
9994 int i;
9996 error_mark_node = make_node (ERROR_MARK);
9997 TREE_TYPE (error_mark_node) = error_mark_node;
9999 initialize_sizetypes ();
10001 /* Define both `signed char' and `unsigned char'. */
10002 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
10003 TYPE_STRING_FLAG (signed_char_type_node) = 1;
10004 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
10005 TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
10007 /* Define `char', which is like either `signed char' or `unsigned char'
10008 but not the same as either. */
10009 char_type_node
10010 = (signed_char
10011 ? make_signed_type (CHAR_TYPE_SIZE)
10012 : make_unsigned_type (CHAR_TYPE_SIZE));
10013 TYPE_STRING_FLAG (char_type_node) = 1;
10015 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
10016 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
10017 integer_type_node = make_signed_type (INT_TYPE_SIZE);
10018 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
10019 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
10020 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
10021 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
10022 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
10024 for (i = 0; i < NUM_INT_N_ENTS; i ++)
10026 int_n_trees[i].signed_type = make_signed_type (int_n_data[i].bitsize);
10027 int_n_trees[i].unsigned_type = make_unsigned_type (int_n_data[i].bitsize);
10028 TYPE_SIZE (int_n_trees[i].signed_type) = bitsize_int (int_n_data[i].bitsize);
10029 TYPE_SIZE (int_n_trees[i].unsigned_type) = bitsize_int (int_n_data[i].bitsize);
10031 if (int_n_data[i].bitsize > LONG_LONG_TYPE_SIZE
10032 && int_n_enabled_p[i])
10034 integer_types[itk_intN_0 + i * 2] = int_n_trees[i].signed_type;
10035 integer_types[itk_unsigned_intN_0 + i * 2] = int_n_trees[i].unsigned_type;
10039 /* Define a boolean type. This type only represents boolean values but
10040 may be larger than char depending on the value of BOOL_TYPE_SIZE. */
10041 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
10042 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
10043 TYPE_PRECISION (boolean_type_node) = 1;
10044 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
10046 /* Define what type to use for size_t. */
10047 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
10048 size_type_node = unsigned_type_node;
10049 else if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
10050 size_type_node = long_unsigned_type_node;
10051 else if (strcmp (SIZE_TYPE, "long long unsigned int") == 0)
10052 size_type_node = long_long_unsigned_type_node;
10053 else if (strcmp (SIZE_TYPE, "short unsigned int") == 0)
10054 size_type_node = short_unsigned_type_node;
10055 else
10057 int i;
10059 size_type_node = NULL_TREE;
10060 for (i = 0; i < NUM_INT_N_ENTS; i++)
10061 if (int_n_enabled_p[i])
10063 char name[50];
10064 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
10066 if (strcmp (name, SIZE_TYPE) == 0)
10068 size_type_node = int_n_trees[i].unsigned_type;
10071 if (size_type_node == NULL_TREE)
10072 gcc_unreachable ();
10075 /* Fill in the rest of the sized types. Reuse existing type nodes
10076 when possible. */
10077 intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
10078 intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
10079 intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
10080 intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
10081 intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
10083 unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
10084 unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
10085 unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
10086 unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
10087 unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
10089 /* Don't call build_qualified type for atomics. That routine does
10090 special processing for atomics, and until they are initialized
10091 it's better not to make that call.
10093 Check to see if there is a target override for atomic types. */
10095 atomicQI_type_node = build_atomic_base (unsigned_intQI_type_node,
10096 targetm.atomic_align_for_mode (QImode));
10097 atomicHI_type_node = build_atomic_base (unsigned_intHI_type_node,
10098 targetm.atomic_align_for_mode (HImode));
10099 atomicSI_type_node = build_atomic_base (unsigned_intSI_type_node,
10100 targetm.atomic_align_for_mode (SImode));
10101 atomicDI_type_node = build_atomic_base (unsigned_intDI_type_node,
10102 targetm.atomic_align_for_mode (DImode));
10103 atomicTI_type_node = build_atomic_base (unsigned_intTI_type_node,
10104 targetm.atomic_align_for_mode (TImode));
10106 access_public_node = get_identifier ("public");
10107 access_protected_node = get_identifier ("protected");
10108 access_private_node = get_identifier ("private");
10110 /* Define these next since types below may used them. */
10111 integer_zero_node = build_int_cst (integer_type_node, 0);
10112 integer_one_node = build_int_cst (integer_type_node, 1);
10113 integer_three_node = build_int_cst (integer_type_node, 3);
10114 integer_minus_one_node = build_int_cst (integer_type_node, -1);
10116 size_zero_node = size_int (0);
10117 size_one_node = size_int (1);
10118 bitsize_zero_node = bitsize_int (0);
10119 bitsize_one_node = bitsize_int (1);
10120 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
10122 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
10123 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
10125 void_type_node = make_node (VOID_TYPE);
10126 layout_type (void_type_node);
10128 pointer_bounds_type_node = targetm.chkp_bound_type ();
10130 /* We are not going to have real types in C with less than byte alignment,
10131 so we might as well not have any types that claim to have it. */
10132 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
10133 TYPE_USER_ALIGN (void_type_node) = 0;
10135 void_node = make_node (VOID_CST);
10136 TREE_TYPE (void_node) = void_type_node;
10138 null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
10139 layout_type (TREE_TYPE (null_pointer_node));
10141 ptr_type_node = build_pointer_type (void_type_node);
10142 const_ptr_type_node
10143 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
10144 fileptr_type_node = ptr_type_node;
10146 pointer_sized_int_node = build_nonstandard_integer_type (POINTER_SIZE, 1);
10148 float_type_node = make_node (REAL_TYPE);
10149 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
10150 layout_type (float_type_node);
10152 double_type_node = make_node (REAL_TYPE);
10153 if (short_double)
10154 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
10155 else
10156 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
10157 layout_type (double_type_node);
10159 long_double_type_node = make_node (REAL_TYPE);
10160 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
10161 layout_type (long_double_type_node);
10163 float_ptr_type_node = build_pointer_type (float_type_node);
10164 double_ptr_type_node = build_pointer_type (double_type_node);
10165 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
10166 integer_ptr_type_node = build_pointer_type (integer_type_node);
10168 /* Fixed size integer types. */
10169 uint16_type_node = make_or_reuse_type (16, 1);
10170 uint32_type_node = make_or_reuse_type (32, 1);
10171 uint64_type_node = make_or_reuse_type (64, 1);
10173 /* Decimal float types. */
10174 dfloat32_type_node = make_node (REAL_TYPE);
10175 TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
10176 layout_type (dfloat32_type_node);
10177 SET_TYPE_MODE (dfloat32_type_node, SDmode);
10178 dfloat32_ptr_type_node = build_pointer_type (dfloat32_type_node);
10180 dfloat64_type_node = make_node (REAL_TYPE);
10181 TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
10182 layout_type (dfloat64_type_node);
10183 SET_TYPE_MODE (dfloat64_type_node, DDmode);
10184 dfloat64_ptr_type_node = build_pointer_type (dfloat64_type_node);
10186 dfloat128_type_node = make_node (REAL_TYPE);
10187 TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
10188 layout_type (dfloat128_type_node);
10189 SET_TYPE_MODE (dfloat128_type_node, TDmode);
10190 dfloat128_ptr_type_node = build_pointer_type (dfloat128_type_node);
10192 complex_integer_type_node = build_complex_type (integer_type_node);
10193 complex_float_type_node = build_complex_type (float_type_node);
10194 complex_double_type_node = build_complex_type (double_type_node);
10195 complex_long_double_type_node = build_complex_type (long_double_type_node);
10197 /* Make fixed-point nodes based on sat/non-sat and signed/unsigned. */
10198 #define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
10199 sat_ ## KIND ## _type_node = \
10200 make_sat_signed_ ## KIND ## _type (SIZE); \
10201 sat_unsigned_ ## KIND ## _type_node = \
10202 make_sat_unsigned_ ## KIND ## _type (SIZE); \
10203 KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
10204 unsigned_ ## KIND ## _type_node = \
10205 make_unsigned_ ## KIND ## _type (SIZE);
10207 #define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
10208 sat_ ## WIDTH ## KIND ## _type_node = \
10209 make_sat_signed_ ## KIND ## _type (SIZE); \
10210 sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
10211 make_sat_unsigned_ ## KIND ## _type (SIZE); \
10212 WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
10213 unsigned_ ## WIDTH ## KIND ## _type_node = \
10214 make_unsigned_ ## KIND ## _type (SIZE);
10216 /* Make fixed-point type nodes based on four different widths. */
10217 #define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
10218 MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
10219 MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
10220 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
10221 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
10223 /* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned. */
10224 #define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
10225 NAME ## _type_node = \
10226 make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
10227 u ## NAME ## _type_node = \
10228 make_or_reuse_unsigned_ ## KIND ## _type \
10229 (GET_MODE_BITSIZE (U ## MODE ## mode)); \
10230 sat_ ## NAME ## _type_node = \
10231 make_or_reuse_sat_signed_ ## KIND ## _type \
10232 (GET_MODE_BITSIZE (MODE ## mode)); \
10233 sat_u ## NAME ## _type_node = \
10234 make_or_reuse_sat_unsigned_ ## KIND ## _type \
10235 (GET_MODE_BITSIZE (U ## MODE ## mode));
10237 /* Fixed-point type and mode nodes. */
10238 MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
10239 MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
10240 MAKE_FIXED_MODE_NODE (fract, qq, QQ)
10241 MAKE_FIXED_MODE_NODE (fract, hq, HQ)
10242 MAKE_FIXED_MODE_NODE (fract, sq, SQ)
10243 MAKE_FIXED_MODE_NODE (fract, dq, DQ)
10244 MAKE_FIXED_MODE_NODE (fract, tq, TQ)
10245 MAKE_FIXED_MODE_NODE (accum, ha, HA)
10246 MAKE_FIXED_MODE_NODE (accum, sa, SA)
10247 MAKE_FIXED_MODE_NODE (accum, da, DA)
10248 MAKE_FIXED_MODE_NODE (accum, ta, TA)
10251 tree t = targetm.build_builtin_va_list ();
10253 /* Many back-ends define record types without setting TYPE_NAME.
10254 If we copied the record type here, we'd keep the original
10255 record type without a name. This breaks name mangling. So,
10256 don't copy record types and let c_common_nodes_and_builtins()
10257 declare the type to be __builtin_va_list. */
10258 if (TREE_CODE (t) != RECORD_TYPE)
10259 t = build_variant_type_copy (t);
10261 va_list_type_node = t;
10265 /* Modify DECL for given flags.
10266 TM_PURE attribute is set only on types, so the function will modify
10267 DECL's type when ECF_TM_PURE is used. */
10269 void
10270 set_call_expr_flags (tree decl, int flags)
10272 if (flags & ECF_NOTHROW)
10273 TREE_NOTHROW (decl) = 1;
10274 if (flags & ECF_CONST)
10275 TREE_READONLY (decl) = 1;
10276 if (flags & ECF_PURE)
10277 DECL_PURE_P (decl) = 1;
10278 if (flags & ECF_LOOPING_CONST_OR_PURE)
10279 DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
10280 if (flags & ECF_NOVOPS)
10281 DECL_IS_NOVOPS (decl) = 1;
10282 if (flags & ECF_NORETURN)
10283 TREE_THIS_VOLATILE (decl) = 1;
10284 if (flags & ECF_MALLOC)
10285 DECL_IS_MALLOC (decl) = 1;
10286 if (flags & ECF_RETURNS_TWICE)
10287 DECL_IS_RETURNS_TWICE (decl) = 1;
10288 if (flags & ECF_LEAF)
10289 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("leaf"),
10290 NULL, DECL_ATTRIBUTES (decl));
10291 if ((flags & ECF_TM_PURE) && flag_tm)
10292 apply_tm_attr (decl, get_identifier ("transaction_pure"));
10293 /* Looping const or pure is implied by noreturn.
10294 There is currently no way to declare looping const or looping pure alone. */
10295 gcc_assert (!(flags & ECF_LOOPING_CONST_OR_PURE)
10296 || ((flags & ECF_NORETURN) && (flags & (ECF_CONST | ECF_PURE))));
10300 /* A subroutine of build_common_builtin_nodes. Define a builtin function. */
10302 static void
10303 local_define_builtin (const char *name, tree type, enum built_in_function code,
10304 const char *library_name, int ecf_flags)
10306 tree decl;
10308 decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
10309 library_name, NULL_TREE);
10310 set_call_expr_flags (decl, ecf_flags);
10312 set_builtin_decl (code, decl, true);
10315 /* Call this function after instantiating all builtins that the language
10316 front end cares about. This will build the rest of the builtins
10317 and internal functions that are relied upon by the tree optimizers and
10318 the middle-end. */
10320 void
10321 build_common_builtin_nodes (void)
10323 tree tmp, ftype;
10324 int ecf_flags;
10326 if (!builtin_decl_explicit_p (BUILT_IN_UNREACHABLE))
10328 ftype = build_function_type (void_type_node, void_list_node);
10329 local_define_builtin ("__builtin_unreachable", ftype, BUILT_IN_UNREACHABLE,
10330 "__builtin_unreachable",
10331 ECF_NOTHROW | ECF_LEAF | ECF_NORETURN
10332 | ECF_CONST);
10335 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY)
10336 || !builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
10338 ftype = build_function_type_list (ptr_type_node,
10339 ptr_type_node, const_ptr_type_node,
10340 size_type_node, NULL_TREE);
10342 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY))
10343 local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
10344 "memcpy", ECF_NOTHROW | ECF_LEAF);
10345 if (!builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
10346 local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
10347 "memmove", ECF_NOTHROW | ECF_LEAF);
10350 if (!builtin_decl_explicit_p (BUILT_IN_MEMCMP))
10352 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
10353 const_ptr_type_node, size_type_node,
10354 NULL_TREE);
10355 local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
10356 "memcmp", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10359 if (!builtin_decl_explicit_p (BUILT_IN_MEMSET))
10361 ftype = build_function_type_list (ptr_type_node,
10362 ptr_type_node, integer_type_node,
10363 size_type_node, NULL_TREE);
10364 local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
10365 "memset", ECF_NOTHROW | ECF_LEAF);
10368 if (!builtin_decl_explicit_p (BUILT_IN_ALLOCA))
10370 ftype = build_function_type_list (ptr_type_node,
10371 size_type_node, NULL_TREE);
10372 local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
10373 "alloca", ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
10376 ftype = build_function_type_list (ptr_type_node, size_type_node,
10377 size_type_node, NULL_TREE);
10378 local_define_builtin ("__builtin_alloca_with_align", ftype,
10379 BUILT_IN_ALLOCA_WITH_ALIGN,
10380 "__builtin_alloca_with_align",
10381 ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
10383 /* If we're checking the stack, `alloca' can throw. */
10384 if (flag_stack_check)
10386 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA)) = 0;
10387 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN)) = 0;
10390 ftype = build_function_type_list (void_type_node,
10391 ptr_type_node, ptr_type_node,
10392 ptr_type_node, NULL_TREE);
10393 local_define_builtin ("__builtin_init_trampoline", ftype,
10394 BUILT_IN_INIT_TRAMPOLINE,
10395 "__builtin_init_trampoline", ECF_NOTHROW | ECF_LEAF);
10396 local_define_builtin ("__builtin_init_heap_trampoline", ftype,
10397 BUILT_IN_INIT_HEAP_TRAMPOLINE,
10398 "__builtin_init_heap_trampoline",
10399 ECF_NOTHROW | ECF_LEAF);
10401 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
10402 local_define_builtin ("__builtin_adjust_trampoline", ftype,
10403 BUILT_IN_ADJUST_TRAMPOLINE,
10404 "__builtin_adjust_trampoline",
10405 ECF_CONST | ECF_NOTHROW);
10407 ftype = build_function_type_list (void_type_node,
10408 ptr_type_node, ptr_type_node, NULL_TREE);
10409 local_define_builtin ("__builtin_nonlocal_goto", ftype,
10410 BUILT_IN_NONLOCAL_GOTO,
10411 "__builtin_nonlocal_goto",
10412 ECF_NORETURN | ECF_NOTHROW);
10414 ftype = build_function_type_list (void_type_node,
10415 ptr_type_node, ptr_type_node, NULL_TREE);
10416 local_define_builtin ("__builtin_setjmp_setup", ftype,
10417 BUILT_IN_SETJMP_SETUP,
10418 "__builtin_setjmp_setup", ECF_NOTHROW);
10420 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10421 local_define_builtin ("__builtin_setjmp_receiver", ftype,
10422 BUILT_IN_SETJMP_RECEIVER,
10423 "__builtin_setjmp_receiver", ECF_NOTHROW | ECF_LEAF);
10425 ftype = build_function_type_list (ptr_type_node, NULL_TREE);
10426 local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
10427 "__builtin_stack_save", ECF_NOTHROW | ECF_LEAF);
10429 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10430 local_define_builtin ("__builtin_stack_restore", ftype,
10431 BUILT_IN_STACK_RESTORE,
10432 "__builtin_stack_restore", ECF_NOTHROW | ECF_LEAF);
10434 /* If there's a possibility that we might use the ARM EABI, build the
10435 alternate __cxa_end_cleanup node used to resume from C++ and Java. */
10436 if (targetm.arm_eabi_unwinder)
10438 ftype = build_function_type_list (void_type_node, NULL_TREE);
10439 local_define_builtin ("__builtin_cxa_end_cleanup", ftype,
10440 BUILT_IN_CXA_END_CLEANUP,
10441 "__cxa_end_cleanup", ECF_NORETURN | ECF_LEAF);
10444 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10445 local_define_builtin ("__builtin_unwind_resume", ftype,
10446 BUILT_IN_UNWIND_RESUME,
10447 ((targetm_common.except_unwind_info (&global_options)
10448 == UI_SJLJ)
10449 ? "_Unwind_SjLj_Resume" : "_Unwind_Resume"),
10450 ECF_NORETURN);
10452 if (builtin_decl_explicit (BUILT_IN_RETURN_ADDRESS) == NULL_TREE)
10454 ftype = build_function_type_list (ptr_type_node, integer_type_node,
10455 NULL_TREE);
10456 local_define_builtin ("__builtin_return_address", ftype,
10457 BUILT_IN_RETURN_ADDRESS,
10458 "__builtin_return_address",
10459 ECF_NOTHROW);
10462 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER)
10463 || !builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
10465 ftype = build_function_type_list (void_type_node, ptr_type_node,
10466 ptr_type_node, NULL_TREE);
10467 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER))
10468 local_define_builtin ("__cyg_profile_func_enter", ftype,
10469 BUILT_IN_PROFILE_FUNC_ENTER,
10470 "__cyg_profile_func_enter", 0);
10471 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
10472 local_define_builtin ("__cyg_profile_func_exit", ftype,
10473 BUILT_IN_PROFILE_FUNC_EXIT,
10474 "__cyg_profile_func_exit", 0);
10477 /* The exception object and filter values from the runtime. The argument
10478 must be zero before exception lowering, i.e. from the front end. After
10479 exception lowering, it will be the region number for the exception
10480 landing pad. These functions are PURE instead of CONST to prevent
10481 them from being hoisted past the exception edge that will initialize
10482 its value in the landing pad. */
10483 ftype = build_function_type_list (ptr_type_node,
10484 integer_type_node, NULL_TREE);
10485 ecf_flags = ECF_PURE | ECF_NOTHROW | ECF_LEAF;
10486 /* Only use TM_PURE if we have TM language support. */
10487 if (builtin_decl_explicit_p (BUILT_IN_TM_LOAD_1))
10488 ecf_flags |= ECF_TM_PURE;
10489 local_define_builtin ("__builtin_eh_pointer", ftype, BUILT_IN_EH_POINTER,
10490 "__builtin_eh_pointer", ecf_flags);
10492 tmp = lang_hooks.types.type_for_mode (targetm.eh_return_filter_mode (), 0);
10493 ftype = build_function_type_list (tmp, integer_type_node, NULL_TREE);
10494 local_define_builtin ("__builtin_eh_filter", ftype, BUILT_IN_EH_FILTER,
10495 "__builtin_eh_filter", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10497 ftype = build_function_type_list (void_type_node,
10498 integer_type_node, integer_type_node,
10499 NULL_TREE);
10500 local_define_builtin ("__builtin_eh_copy_values", ftype,
10501 BUILT_IN_EH_COPY_VALUES,
10502 "__builtin_eh_copy_values", ECF_NOTHROW);
10504 /* Complex multiplication and division. These are handled as builtins
10505 rather than optabs because emit_library_call_value doesn't support
10506 complex. Further, we can do slightly better with folding these
10507 beasties if the real and complex parts of the arguments are separate. */
10509 int mode;
10511 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
10513 char mode_name_buf[4], *q;
10514 const char *p;
10515 enum built_in_function mcode, dcode;
10516 tree type, inner_type;
10517 const char *prefix = "__";
10519 if (targetm.libfunc_gnu_prefix)
10520 prefix = "__gnu_";
10522 type = lang_hooks.types.type_for_mode ((machine_mode) mode, 0);
10523 if (type == NULL)
10524 continue;
10525 inner_type = TREE_TYPE (type);
10527 ftype = build_function_type_list (type, inner_type, inner_type,
10528 inner_type, inner_type, NULL_TREE);
10530 mcode = ((enum built_in_function)
10531 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10532 dcode = ((enum built_in_function)
10533 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10535 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
10536 *q = TOLOWER (*p);
10537 *q = '\0';
10539 built_in_names[mcode] = concat (prefix, "mul", mode_name_buf, "3",
10540 NULL);
10541 local_define_builtin (built_in_names[mcode], ftype, mcode,
10542 built_in_names[mcode],
10543 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
10545 built_in_names[dcode] = concat (prefix, "div", mode_name_buf, "3",
10546 NULL);
10547 local_define_builtin (built_in_names[dcode], ftype, dcode,
10548 built_in_names[dcode],
10549 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
10553 init_internal_fns ();
10556 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
10557 better way.
10559 If we requested a pointer to a vector, build up the pointers that
10560 we stripped off while looking for the inner type. Similarly for
10561 return values from functions.
10563 The argument TYPE is the top of the chain, and BOTTOM is the
10564 new type which we will point to. */
10566 tree
10567 reconstruct_complex_type (tree type, tree bottom)
10569 tree inner, outer;
10571 if (TREE_CODE (type) == POINTER_TYPE)
10573 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10574 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
10575 TYPE_REF_CAN_ALIAS_ALL (type));
10577 else if (TREE_CODE (type) == REFERENCE_TYPE)
10579 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10580 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
10581 TYPE_REF_CAN_ALIAS_ALL (type));
10583 else if (TREE_CODE (type) == ARRAY_TYPE)
10585 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10586 outer = build_array_type (inner, TYPE_DOMAIN (type));
10588 else if (TREE_CODE (type) == FUNCTION_TYPE)
10590 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10591 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
10593 else if (TREE_CODE (type) == METHOD_TYPE)
10595 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10596 /* The build_method_type_directly() routine prepends 'this' to argument list,
10597 so we must compensate by getting rid of it. */
10598 outer
10599 = build_method_type_directly
10600 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
10601 inner,
10602 TREE_CHAIN (TYPE_ARG_TYPES (type)));
10604 else if (TREE_CODE (type) == OFFSET_TYPE)
10606 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10607 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
10609 else
10610 return bottom;
10612 return build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
10613 TYPE_QUALS (type));
10616 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
10617 the inner type. */
10618 tree
10619 build_vector_type_for_mode (tree innertype, machine_mode mode)
10621 int nunits;
10623 switch (GET_MODE_CLASS (mode))
10625 case MODE_VECTOR_INT:
10626 case MODE_VECTOR_FLOAT:
10627 case MODE_VECTOR_FRACT:
10628 case MODE_VECTOR_UFRACT:
10629 case MODE_VECTOR_ACCUM:
10630 case MODE_VECTOR_UACCUM:
10631 nunits = GET_MODE_NUNITS (mode);
10632 break;
10634 case MODE_INT:
10635 /* Check that there are no leftover bits. */
10636 gcc_assert (GET_MODE_BITSIZE (mode)
10637 % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
10639 nunits = GET_MODE_BITSIZE (mode)
10640 / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
10641 break;
10643 default:
10644 gcc_unreachable ();
10647 return make_vector_type (innertype, nunits, mode);
10650 /* Similarly, but takes the inner type and number of units, which must be
10651 a power of two. */
10653 tree
10654 build_vector_type (tree innertype, int nunits)
10656 return make_vector_type (innertype, nunits, VOIDmode);
10659 /* Build truth vector with specified length and number of units. */
10661 tree
10662 build_truth_vector_type (unsigned nunits, unsigned vector_size)
10664 machine_mode mask_mode = targetm.vectorize.get_mask_mode (nunits,
10665 vector_size);
10667 gcc_assert (mask_mode != VOIDmode);
10669 unsigned HOST_WIDE_INT vsize;
10670 if (mask_mode == BLKmode)
10671 vsize = vector_size * BITS_PER_UNIT;
10672 else
10673 vsize = GET_MODE_BITSIZE (mask_mode);
10675 unsigned HOST_WIDE_INT esize = vsize / nunits;
10676 gcc_assert (esize * nunits == vsize);
10678 tree bool_type = build_nonstandard_boolean_type (esize);
10680 return make_vector_type (bool_type, nunits, mask_mode);
10683 /* Returns a vector type corresponding to a comparison of VECTYPE. */
10685 tree
10686 build_same_sized_truth_vector_type (tree vectype)
10688 if (VECTOR_BOOLEAN_TYPE_P (vectype))
10689 return vectype;
10691 unsigned HOST_WIDE_INT size = GET_MODE_SIZE (TYPE_MODE (vectype));
10693 if (!size)
10694 size = tree_to_uhwi (TYPE_SIZE_UNIT (vectype));
10696 return build_truth_vector_type (TYPE_VECTOR_SUBPARTS (vectype), size);
10699 /* Similarly, but builds a variant type with TYPE_VECTOR_OPAQUE set. */
10701 tree
10702 build_opaque_vector_type (tree innertype, int nunits)
10704 tree t = make_vector_type (innertype, nunits, VOIDmode);
10705 tree cand;
10706 /* We always build the non-opaque variant before the opaque one,
10707 so if it already exists, it is TYPE_NEXT_VARIANT of this one. */
10708 cand = TYPE_NEXT_VARIANT (t);
10709 if (cand
10710 && TYPE_VECTOR_OPAQUE (cand)
10711 && check_qualified_type (cand, t, TYPE_QUALS (t)))
10712 return cand;
10713 /* Othewise build a variant type and make sure to queue it after
10714 the non-opaque type. */
10715 cand = build_distinct_type_copy (t);
10716 TYPE_VECTOR_OPAQUE (cand) = true;
10717 TYPE_CANONICAL (cand) = TYPE_CANONICAL (t);
10718 TYPE_NEXT_VARIANT (cand) = TYPE_NEXT_VARIANT (t);
10719 TYPE_NEXT_VARIANT (t) = cand;
10720 TYPE_MAIN_VARIANT (cand) = TYPE_MAIN_VARIANT (t);
10721 return cand;
10725 /* Given an initializer INIT, return TRUE if INIT is zero or some
10726 aggregate of zeros. Otherwise return FALSE. */
10727 bool
10728 initializer_zerop (const_tree init)
10730 tree elt;
10732 STRIP_NOPS (init);
10734 switch (TREE_CODE (init))
10736 case INTEGER_CST:
10737 return integer_zerop (init);
10739 case REAL_CST:
10740 /* ??? Note that this is not correct for C4X float formats. There,
10741 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
10742 negative exponent. */
10743 return real_zerop (init)
10744 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
10746 case FIXED_CST:
10747 return fixed_zerop (init);
10749 case COMPLEX_CST:
10750 return integer_zerop (init)
10751 || (real_zerop (init)
10752 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
10753 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
10755 case VECTOR_CST:
10757 unsigned i;
10758 for (i = 0; i < VECTOR_CST_NELTS (init); ++i)
10759 if (!initializer_zerop (VECTOR_CST_ELT (init, i)))
10760 return false;
10761 return true;
10764 case CONSTRUCTOR:
10766 unsigned HOST_WIDE_INT idx;
10768 if (TREE_CLOBBER_P (init))
10769 return false;
10770 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
10771 if (!initializer_zerop (elt))
10772 return false;
10773 return true;
10776 case STRING_CST:
10778 int i;
10780 /* We need to loop through all elements to handle cases like
10781 "\0" and "\0foobar". */
10782 for (i = 0; i < TREE_STRING_LENGTH (init); ++i)
10783 if (TREE_STRING_POINTER (init)[i] != '\0')
10784 return false;
10786 return true;
10789 default:
10790 return false;
10794 /* Check if vector VEC consists of all the equal elements and
10795 that the number of elements corresponds to the type of VEC.
10796 The function returns first element of the vector
10797 or NULL_TREE if the vector is not uniform. */
10798 tree
10799 uniform_vector_p (const_tree vec)
10801 tree first, t;
10802 unsigned i;
10804 if (vec == NULL_TREE)
10805 return NULL_TREE;
10807 gcc_assert (VECTOR_TYPE_P (TREE_TYPE (vec)));
10809 if (TREE_CODE (vec) == VECTOR_CST)
10811 first = VECTOR_CST_ELT (vec, 0);
10812 for (i = 1; i < VECTOR_CST_NELTS (vec); ++i)
10813 if (!operand_equal_p (first, VECTOR_CST_ELT (vec, i), 0))
10814 return NULL_TREE;
10816 return first;
10819 else if (TREE_CODE (vec) == CONSTRUCTOR)
10821 first = error_mark_node;
10823 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (vec), i, t)
10825 if (i == 0)
10827 first = t;
10828 continue;
10830 if (!operand_equal_p (first, t, 0))
10831 return NULL_TREE;
10833 if (i != TYPE_VECTOR_SUBPARTS (TREE_TYPE (vec)))
10834 return NULL_TREE;
10836 return first;
10839 return NULL_TREE;
10842 /* Build an empty statement at location LOC. */
10844 tree
10845 build_empty_stmt (location_t loc)
10847 tree t = build1 (NOP_EXPR, void_type_node, size_zero_node);
10848 SET_EXPR_LOCATION (t, loc);
10849 return t;
10853 /* Build an OpenMP clause with code CODE. LOC is the location of the
10854 clause. */
10856 tree
10857 build_omp_clause (location_t loc, enum omp_clause_code code)
10859 tree t;
10860 int size, length;
10862 length = omp_clause_num_ops[code];
10863 size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
10865 record_node_allocation_statistics (OMP_CLAUSE, size);
10867 t = (tree) ggc_internal_alloc (size);
10868 memset (t, 0, size);
10869 TREE_SET_CODE (t, OMP_CLAUSE);
10870 OMP_CLAUSE_SET_CODE (t, code);
10871 OMP_CLAUSE_LOCATION (t) = loc;
10873 return t;
10876 /* Build a tcc_vl_exp object with code CODE and room for LEN operands. LEN
10877 includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
10878 Except for the CODE and operand count field, other storage for the
10879 object is initialized to zeros. */
10881 tree
10882 build_vl_exp_stat (enum tree_code code, int len MEM_STAT_DECL)
10884 tree t;
10885 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
10887 gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
10888 gcc_assert (len >= 1);
10890 record_node_allocation_statistics (code, length);
10892 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
10894 TREE_SET_CODE (t, code);
10896 /* Can't use TREE_OPERAND to store the length because if checking is
10897 enabled, it will try to check the length before we store it. :-P */
10898 t->exp.operands[0] = build_int_cst (sizetype, len);
10900 return t;
10903 /* Helper function for build_call_* functions; build a CALL_EXPR with
10904 indicated RETURN_TYPE, FN, and NARGS, but do not initialize any of
10905 the argument slots. */
10907 static tree
10908 build_call_1 (tree return_type, tree fn, int nargs)
10910 tree t;
10912 t = build_vl_exp (CALL_EXPR, nargs + 3);
10913 TREE_TYPE (t) = return_type;
10914 CALL_EXPR_FN (t) = fn;
10915 CALL_EXPR_STATIC_CHAIN (t) = NULL;
10917 return t;
10920 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10921 FN and a null static chain slot. NARGS is the number of call arguments
10922 which are specified as "..." arguments. */
10924 tree
10925 build_call_nary (tree return_type, tree fn, int nargs, ...)
10927 tree ret;
10928 va_list args;
10929 va_start (args, nargs);
10930 ret = build_call_valist (return_type, fn, nargs, args);
10931 va_end (args);
10932 return ret;
10935 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10936 FN and a null static chain slot. NARGS is the number of call arguments
10937 which are specified as a va_list ARGS. */
10939 tree
10940 build_call_valist (tree return_type, tree fn, int nargs, va_list args)
10942 tree t;
10943 int i;
10945 t = build_call_1 (return_type, fn, nargs);
10946 for (i = 0; i < nargs; i++)
10947 CALL_EXPR_ARG (t, i) = va_arg (args, tree);
10948 process_call_operands (t);
10949 return t;
10952 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10953 FN and a null static chain slot. NARGS is the number of call arguments
10954 which are specified as a tree array ARGS. */
10956 tree
10957 build_call_array_loc (location_t loc, tree return_type, tree fn,
10958 int nargs, const tree *args)
10960 tree t;
10961 int i;
10963 t = build_call_1 (return_type, fn, nargs);
10964 for (i = 0; i < nargs; i++)
10965 CALL_EXPR_ARG (t, i) = args[i];
10966 process_call_operands (t);
10967 SET_EXPR_LOCATION (t, loc);
10968 return t;
10971 /* Like build_call_array, but takes a vec. */
10973 tree
10974 build_call_vec (tree return_type, tree fn, vec<tree, va_gc> *args)
10976 tree ret, t;
10977 unsigned int ix;
10979 ret = build_call_1 (return_type, fn, vec_safe_length (args));
10980 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
10981 CALL_EXPR_ARG (ret, ix) = t;
10982 process_call_operands (ret);
10983 return ret;
10986 /* Conveniently construct a function call expression. FNDECL names the
10987 function to be called and N arguments are passed in the array
10988 ARGARRAY. */
10990 tree
10991 build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
10993 tree fntype = TREE_TYPE (fndecl);
10994 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
10996 return fold_build_call_array_loc (loc, TREE_TYPE (fntype), fn, n, argarray);
10999 /* Conveniently construct a function call expression. FNDECL names the
11000 function to be called and the arguments are passed in the vector
11001 VEC. */
11003 tree
11004 build_call_expr_loc_vec (location_t loc, tree fndecl, vec<tree, va_gc> *vec)
11006 return build_call_expr_loc_array (loc, fndecl, vec_safe_length (vec),
11007 vec_safe_address (vec));
11011 /* Conveniently construct a function call expression. FNDECL names the
11012 function to be called, N is the number of arguments, and the "..."
11013 parameters are the argument expressions. */
11015 tree
11016 build_call_expr_loc (location_t loc, tree fndecl, int n, ...)
11018 va_list ap;
11019 tree *argarray = XALLOCAVEC (tree, n);
11020 int i;
11022 va_start (ap, n);
11023 for (i = 0; i < n; i++)
11024 argarray[i] = va_arg (ap, tree);
11025 va_end (ap);
11026 return build_call_expr_loc_array (loc, fndecl, n, argarray);
11029 /* Like build_call_expr_loc (UNKNOWN_LOCATION, ...). Duplicated because
11030 varargs macros aren't supported by all bootstrap compilers. */
11032 tree
11033 build_call_expr (tree fndecl, int n, ...)
11035 va_list ap;
11036 tree *argarray = XALLOCAVEC (tree, n);
11037 int i;
11039 va_start (ap, n);
11040 for (i = 0; i < n; i++)
11041 argarray[i] = va_arg (ap, tree);
11042 va_end (ap);
11043 return build_call_expr_loc_array (UNKNOWN_LOCATION, fndecl, n, argarray);
11046 /* Build internal call expression. This is just like CALL_EXPR, except
11047 its CALL_EXPR_FN is NULL. It will get gimplified later into ordinary
11048 internal function. */
11050 tree
11051 build_call_expr_internal_loc (location_t loc, enum internal_fn ifn,
11052 tree type, int n, ...)
11054 va_list ap;
11055 int i;
11057 tree fn = build_call_1 (type, NULL_TREE, n);
11058 va_start (ap, n);
11059 for (i = 0; i < n; i++)
11060 CALL_EXPR_ARG (fn, i) = va_arg (ap, tree);
11061 va_end (ap);
11062 SET_EXPR_LOCATION (fn, loc);
11063 CALL_EXPR_IFN (fn) = ifn;
11064 return fn;
11067 /* Create a new constant string literal and return a char* pointer to it.
11068 The STRING_CST value is the LEN characters at STR. */
11069 tree
11070 build_string_literal (int len, const char *str)
11072 tree t, elem, index, type;
11074 t = build_string (len, str);
11075 elem = build_type_variant (char_type_node, 1, 0);
11076 index = build_index_type (size_int (len - 1));
11077 type = build_array_type (elem, index);
11078 TREE_TYPE (t) = type;
11079 TREE_CONSTANT (t) = 1;
11080 TREE_READONLY (t) = 1;
11081 TREE_STATIC (t) = 1;
11083 type = build_pointer_type (elem);
11084 t = build1 (ADDR_EXPR, type,
11085 build4 (ARRAY_REF, elem,
11086 t, integer_zero_node, NULL_TREE, NULL_TREE));
11087 return t;
11092 /* Return true if T (assumed to be a DECL) must be assigned a memory
11093 location. */
11095 bool
11096 needs_to_live_in_memory (const_tree t)
11098 return (TREE_ADDRESSABLE (t)
11099 || is_global_var (t)
11100 || (TREE_CODE (t) == RESULT_DECL
11101 && !DECL_BY_REFERENCE (t)
11102 && aggregate_value_p (t, current_function_decl)));
11105 /* Return value of a constant X and sign-extend it. */
11107 HOST_WIDE_INT
11108 int_cst_value (const_tree x)
11110 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
11111 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
11113 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
11114 gcc_assert (cst_and_fits_in_hwi (x));
11116 if (bits < HOST_BITS_PER_WIDE_INT)
11118 bool negative = ((val >> (bits - 1)) & 1) != 0;
11119 if (negative)
11120 val |= (~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1;
11121 else
11122 val &= ~((~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1);
11125 return val;
11128 /* If TYPE is an integral or pointer type, return an integer type with
11129 the same precision which is unsigned iff UNSIGNEDP is true, or itself
11130 if TYPE is already an integer type of signedness UNSIGNEDP. */
11132 tree
11133 signed_or_unsigned_type_for (int unsignedp, tree type)
11135 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type) == unsignedp)
11136 return type;
11138 if (TREE_CODE (type) == VECTOR_TYPE)
11140 tree inner = TREE_TYPE (type);
11141 tree inner2 = signed_or_unsigned_type_for (unsignedp, inner);
11142 if (!inner2)
11143 return NULL_TREE;
11144 if (inner == inner2)
11145 return type;
11146 return build_vector_type (inner2, TYPE_VECTOR_SUBPARTS (type));
11149 if (!INTEGRAL_TYPE_P (type)
11150 && !POINTER_TYPE_P (type)
11151 && TREE_CODE (type) != OFFSET_TYPE)
11152 return NULL_TREE;
11154 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
11157 /* If TYPE is an integral or pointer type, return an integer type with
11158 the same precision which is unsigned, or itself if TYPE is already an
11159 unsigned integer type. */
11161 tree
11162 unsigned_type_for (tree type)
11164 return signed_or_unsigned_type_for (1, type);
11167 /* If TYPE is an integral or pointer type, return an integer type with
11168 the same precision which is signed, or itself if TYPE is already a
11169 signed integer type. */
11171 tree
11172 signed_type_for (tree type)
11174 return signed_or_unsigned_type_for (0, type);
11177 /* If TYPE is a vector type, return a signed integer vector type with the
11178 same width and number of subparts. Otherwise return boolean_type_node. */
11180 tree
11181 truth_type_for (tree type)
11183 if (TREE_CODE (type) == VECTOR_TYPE)
11185 if (VECTOR_BOOLEAN_TYPE_P (type))
11186 return type;
11187 return build_truth_vector_type (TYPE_VECTOR_SUBPARTS (type),
11188 GET_MODE_SIZE (TYPE_MODE (type)));
11190 else
11191 return boolean_type_node;
11194 /* Returns the largest value obtainable by casting something in INNER type to
11195 OUTER type. */
11197 tree
11198 upper_bound_in_type (tree outer, tree inner)
11200 unsigned int det = 0;
11201 unsigned oprec = TYPE_PRECISION (outer);
11202 unsigned iprec = TYPE_PRECISION (inner);
11203 unsigned prec;
11205 /* Compute a unique number for every combination. */
11206 det |= (oprec > iprec) ? 4 : 0;
11207 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
11208 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
11210 /* Determine the exponent to use. */
11211 switch (det)
11213 case 0:
11214 case 1:
11215 /* oprec <= iprec, outer: signed, inner: don't care. */
11216 prec = oprec - 1;
11217 break;
11218 case 2:
11219 case 3:
11220 /* oprec <= iprec, outer: unsigned, inner: don't care. */
11221 prec = oprec;
11222 break;
11223 case 4:
11224 /* oprec > iprec, outer: signed, inner: signed. */
11225 prec = iprec - 1;
11226 break;
11227 case 5:
11228 /* oprec > iprec, outer: signed, inner: unsigned. */
11229 prec = iprec;
11230 break;
11231 case 6:
11232 /* oprec > iprec, outer: unsigned, inner: signed. */
11233 prec = oprec;
11234 break;
11235 case 7:
11236 /* oprec > iprec, outer: unsigned, inner: unsigned. */
11237 prec = iprec;
11238 break;
11239 default:
11240 gcc_unreachable ();
11243 return wide_int_to_tree (outer,
11244 wi::mask (prec, false, TYPE_PRECISION (outer)));
11247 /* Returns the smallest value obtainable by casting something in INNER type to
11248 OUTER type. */
11250 tree
11251 lower_bound_in_type (tree outer, tree inner)
11253 unsigned oprec = TYPE_PRECISION (outer);
11254 unsigned iprec = TYPE_PRECISION (inner);
11256 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
11257 and obtain 0. */
11258 if (TYPE_UNSIGNED (outer)
11259 /* If we are widening something of an unsigned type, OUTER type
11260 contains all values of INNER type. In particular, both INNER
11261 and OUTER types have zero in common. */
11262 || (oprec > iprec && TYPE_UNSIGNED (inner)))
11263 return build_int_cst (outer, 0);
11264 else
11266 /* If we are widening a signed type to another signed type, we
11267 want to obtain -2^^(iprec-1). If we are keeping the
11268 precision or narrowing to a signed type, we want to obtain
11269 -2^(oprec-1). */
11270 unsigned prec = oprec > iprec ? iprec : oprec;
11271 return wide_int_to_tree (outer,
11272 wi::mask (prec - 1, true,
11273 TYPE_PRECISION (outer)));
11277 /* Return nonzero if two operands that are suitable for PHI nodes are
11278 necessarily equal. Specifically, both ARG0 and ARG1 must be either
11279 SSA_NAME or invariant. Note that this is strictly an optimization.
11280 That is, callers of this function can directly call operand_equal_p
11281 and get the same result, only slower. */
11284 operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
11286 if (arg0 == arg1)
11287 return 1;
11288 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
11289 return 0;
11290 return operand_equal_p (arg0, arg1, 0);
11293 /* Returns number of zeros at the end of binary representation of X. */
11295 tree
11296 num_ending_zeros (const_tree x)
11298 return build_int_cst (TREE_TYPE (x), wi::ctz (x));
11302 #define WALK_SUBTREE(NODE) \
11303 do \
11305 result = walk_tree_1 (&(NODE), func, data, pset, lh); \
11306 if (result) \
11307 return result; \
11309 while (0)
11311 /* This is a subroutine of walk_tree that walks field of TYPE that are to
11312 be walked whenever a type is seen in the tree. Rest of operands and return
11313 value are as for walk_tree. */
11315 static tree
11316 walk_type_fields (tree type, walk_tree_fn func, void *data,
11317 hash_set<tree> *pset, walk_tree_lh lh)
11319 tree result = NULL_TREE;
11321 switch (TREE_CODE (type))
11323 case POINTER_TYPE:
11324 case REFERENCE_TYPE:
11325 case VECTOR_TYPE:
11326 /* We have to worry about mutually recursive pointers. These can't
11327 be written in C. They can in Ada. It's pathological, but
11328 there's an ACATS test (c38102a) that checks it. Deal with this
11329 by checking if we're pointing to another pointer, that one
11330 points to another pointer, that one does too, and we have no htab.
11331 If so, get a hash table. We check three levels deep to avoid
11332 the cost of the hash table if we don't need one. */
11333 if (POINTER_TYPE_P (TREE_TYPE (type))
11334 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
11335 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
11336 && !pset)
11338 result = walk_tree_without_duplicates (&TREE_TYPE (type),
11339 func, data);
11340 if (result)
11341 return result;
11343 break;
11346 /* ... fall through ... */
11348 case COMPLEX_TYPE:
11349 WALK_SUBTREE (TREE_TYPE (type));
11350 break;
11352 case METHOD_TYPE:
11353 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
11355 /* Fall through. */
11357 case FUNCTION_TYPE:
11358 WALK_SUBTREE (TREE_TYPE (type));
11360 tree arg;
11362 /* We never want to walk into default arguments. */
11363 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
11364 WALK_SUBTREE (TREE_VALUE (arg));
11366 break;
11368 case ARRAY_TYPE:
11369 /* Don't follow this nodes's type if a pointer for fear that
11370 we'll have infinite recursion. If we have a PSET, then we
11371 need not fear. */
11372 if (pset
11373 || (!POINTER_TYPE_P (TREE_TYPE (type))
11374 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
11375 WALK_SUBTREE (TREE_TYPE (type));
11376 WALK_SUBTREE (TYPE_DOMAIN (type));
11377 break;
11379 case OFFSET_TYPE:
11380 WALK_SUBTREE (TREE_TYPE (type));
11381 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
11382 break;
11384 default:
11385 break;
11388 return NULL_TREE;
11391 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
11392 called with the DATA and the address of each sub-tree. If FUNC returns a
11393 non-NULL value, the traversal is stopped, and the value returned by FUNC
11394 is returned. If PSET is non-NULL it is used to record the nodes visited,
11395 and to avoid visiting a node more than once. */
11397 tree
11398 walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
11399 hash_set<tree> *pset, walk_tree_lh lh)
11401 enum tree_code code;
11402 int walk_subtrees;
11403 tree result;
11405 #define WALK_SUBTREE_TAIL(NODE) \
11406 do \
11408 tp = & (NODE); \
11409 goto tail_recurse; \
11411 while (0)
11413 tail_recurse:
11414 /* Skip empty subtrees. */
11415 if (!*tp)
11416 return NULL_TREE;
11418 /* Don't walk the same tree twice, if the user has requested
11419 that we avoid doing so. */
11420 if (pset && pset->add (*tp))
11421 return NULL_TREE;
11423 /* Call the function. */
11424 walk_subtrees = 1;
11425 result = (*func) (tp, &walk_subtrees, data);
11427 /* If we found something, return it. */
11428 if (result)
11429 return result;
11431 code = TREE_CODE (*tp);
11433 /* Even if we didn't, FUNC may have decided that there was nothing
11434 interesting below this point in the tree. */
11435 if (!walk_subtrees)
11437 /* But we still need to check our siblings. */
11438 if (code == TREE_LIST)
11439 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
11440 else if (code == OMP_CLAUSE)
11441 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11442 else
11443 return NULL_TREE;
11446 if (lh)
11448 result = (*lh) (tp, &walk_subtrees, func, data, pset);
11449 if (result || !walk_subtrees)
11450 return result;
11453 switch (code)
11455 case ERROR_MARK:
11456 case IDENTIFIER_NODE:
11457 case INTEGER_CST:
11458 case REAL_CST:
11459 case FIXED_CST:
11460 case VECTOR_CST:
11461 case STRING_CST:
11462 case BLOCK:
11463 case PLACEHOLDER_EXPR:
11464 case SSA_NAME:
11465 case FIELD_DECL:
11466 case RESULT_DECL:
11467 /* None of these have subtrees other than those already walked
11468 above. */
11469 break;
11471 case TREE_LIST:
11472 WALK_SUBTREE (TREE_VALUE (*tp));
11473 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
11474 break;
11476 case TREE_VEC:
11478 int len = TREE_VEC_LENGTH (*tp);
11480 if (len == 0)
11481 break;
11483 /* Walk all elements but the first. */
11484 while (--len)
11485 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
11487 /* Now walk the first one as a tail call. */
11488 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
11491 case COMPLEX_CST:
11492 WALK_SUBTREE (TREE_REALPART (*tp));
11493 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
11495 case CONSTRUCTOR:
11497 unsigned HOST_WIDE_INT idx;
11498 constructor_elt *ce;
11500 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (*tp), idx, &ce);
11501 idx++)
11502 WALK_SUBTREE (ce->value);
11504 break;
11506 case SAVE_EXPR:
11507 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
11509 case BIND_EXPR:
11511 tree decl;
11512 for (decl = BIND_EXPR_VARS (*tp); decl; decl = DECL_CHAIN (decl))
11514 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
11515 into declarations that are just mentioned, rather than
11516 declared; they don't really belong to this part of the tree.
11517 And, we can see cycles: the initializer for a declaration
11518 can refer to the declaration itself. */
11519 WALK_SUBTREE (DECL_INITIAL (decl));
11520 WALK_SUBTREE (DECL_SIZE (decl));
11521 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
11523 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
11526 case STATEMENT_LIST:
11528 tree_stmt_iterator i;
11529 for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
11530 WALK_SUBTREE (*tsi_stmt_ptr (i));
11532 break;
11534 case OMP_CLAUSE:
11535 switch (OMP_CLAUSE_CODE (*tp))
11537 case OMP_CLAUSE_GANG:
11538 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 1));
11539 /* FALLTHRU */
11541 case OMP_CLAUSE_DEVICE_RESIDENT:
11542 case OMP_CLAUSE_USE_DEVICE:
11543 case OMP_CLAUSE_ASYNC:
11544 case OMP_CLAUSE_WAIT:
11545 case OMP_CLAUSE_WORKER:
11546 case OMP_CLAUSE_VECTOR:
11547 case OMP_CLAUSE_NUM_GANGS:
11548 case OMP_CLAUSE_NUM_WORKERS:
11549 case OMP_CLAUSE_VECTOR_LENGTH:
11550 case OMP_CLAUSE_PRIVATE:
11551 case OMP_CLAUSE_SHARED:
11552 case OMP_CLAUSE_FIRSTPRIVATE:
11553 case OMP_CLAUSE_COPYIN:
11554 case OMP_CLAUSE_COPYPRIVATE:
11555 case OMP_CLAUSE_FINAL:
11556 case OMP_CLAUSE_IF:
11557 case OMP_CLAUSE_NUM_THREADS:
11558 case OMP_CLAUSE_SCHEDULE:
11559 case OMP_CLAUSE_UNIFORM:
11560 case OMP_CLAUSE_DEPEND:
11561 case OMP_CLAUSE_NUM_TEAMS:
11562 case OMP_CLAUSE_THREAD_LIMIT:
11563 case OMP_CLAUSE_DEVICE:
11564 case OMP_CLAUSE_DIST_SCHEDULE:
11565 case OMP_CLAUSE_SAFELEN:
11566 case OMP_CLAUSE_SIMDLEN:
11567 case OMP_CLAUSE_ORDERED:
11568 case OMP_CLAUSE_PRIORITY:
11569 case OMP_CLAUSE_GRAINSIZE:
11570 case OMP_CLAUSE_NUM_TASKS:
11571 case OMP_CLAUSE_HINT:
11572 case OMP_CLAUSE_TO_DECLARE:
11573 case OMP_CLAUSE_LINK:
11574 case OMP_CLAUSE_USE_DEVICE_PTR:
11575 case OMP_CLAUSE_IS_DEVICE_PTR:
11576 case OMP_CLAUSE__LOOPTEMP_:
11577 case OMP_CLAUSE__SIMDUID_:
11578 case OMP_CLAUSE__CILK_FOR_COUNT_:
11579 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 0));
11580 /* FALLTHRU */
11582 case OMP_CLAUSE_INDEPENDENT:
11583 case OMP_CLAUSE_NOWAIT:
11584 case OMP_CLAUSE_DEFAULT:
11585 case OMP_CLAUSE_UNTIED:
11586 case OMP_CLAUSE_MERGEABLE:
11587 case OMP_CLAUSE_PROC_BIND:
11588 case OMP_CLAUSE_INBRANCH:
11589 case OMP_CLAUSE_NOTINBRANCH:
11590 case OMP_CLAUSE_FOR:
11591 case OMP_CLAUSE_PARALLEL:
11592 case OMP_CLAUSE_SECTIONS:
11593 case OMP_CLAUSE_TASKGROUP:
11594 case OMP_CLAUSE_NOGROUP:
11595 case OMP_CLAUSE_THREADS:
11596 case OMP_CLAUSE_SIMD:
11597 case OMP_CLAUSE_DEFAULTMAP:
11598 case OMP_CLAUSE_AUTO:
11599 case OMP_CLAUSE_SEQ:
11600 case OMP_CLAUSE_TILE:
11601 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11603 case OMP_CLAUSE_LASTPRIVATE:
11604 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11605 WALK_SUBTREE (OMP_CLAUSE_LASTPRIVATE_STMT (*tp));
11606 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11608 case OMP_CLAUSE_COLLAPSE:
11610 int i;
11611 for (i = 0; i < 3; i++)
11612 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
11613 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11616 case OMP_CLAUSE_LINEAR:
11617 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11618 WALK_SUBTREE (OMP_CLAUSE_LINEAR_STEP (*tp));
11619 WALK_SUBTREE (OMP_CLAUSE_LINEAR_STMT (*tp));
11620 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11622 case OMP_CLAUSE_ALIGNED:
11623 case OMP_CLAUSE_FROM:
11624 case OMP_CLAUSE_TO:
11625 case OMP_CLAUSE_MAP:
11626 case OMP_CLAUSE__CACHE_:
11627 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11628 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 1));
11629 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11631 case OMP_CLAUSE_REDUCTION:
11633 int i;
11634 for (i = 0; i < 5; i++)
11635 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
11636 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11639 default:
11640 gcc_unreachable ();
11642 break;
11644 case TARGET_EXPR:
11646 int i, len;
11648 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
11649 But, we only want to walk once. */
11650 len = (TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) ? 2 : 3;
11651 for (i = 0; i < len; ++i)
11652 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11653 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len));
11656 case DECL_EXPR:
11657 /* If this is a TYPE_DECL, walk into the fields of the type that it's
11658 defining. We only want to walk into these fields of a type in this
11659 case and not in the general case of a mere reference to the type.
11661 The criterion is as follows: if the field can be an expression, it
11662 must be walked only here. This should be in keeping with the fields
11663 that are directly gimplified in gimplify_type_sizes in order for the
11664 mark/copy-if-shared/unmark machinery of the gimplifier to work with
11665 variable-sized types.
11667 Note that DECLs get walked as part of processing the BIND_EXPR. */
11668 if (TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL)
11670 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
11671 if (TREE_CODE (*type_p) == ERROR_MARK)
11672 return NULL_TREE;
11674 /* Call the function for the type. See if it returns anything or
11675 doesn't want us to continue. If we are to continue, walk both
11676 the normal fields and those for the declaration case. */
11677 result = (*func) (type_p, &walk_subtrees, data);
11678 if (result || !walk_subtrees)
11679 return result;
11681 /* But do not walk a pointed-to type since it may itself need to
11682 be walked in the declaration case if it isn't anonymous. */
11683 if (!POINTER_TYPE_P (*type_p))
11685 result = walk_type_fields (*type_p, func, data, pset, lh);
11686 if (result)
11687 return result;
11690 /* If this is a record type, also walk the fields. */
11691 if (RECORD_OR_UNION_TYPE_P (*type_p))
11693 tree field;
11695 for (field = TYPE_FIELDS (*type_p); field;
11696 field = DECL_CHAIN (field))
11698 /* We'd like to look at the type of the field, but we can
11699 easily get infinite recursion. So assume it's pointed
11700 to elsewhere in the tree. Also, ignore things that
11701 aren't fields. */
11702 if (TREE_CODE (field) != FIELD_DECL)
11703 continue;
11705 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
11706 WALK_SUBTREE (DECL_SIZE (field));
11707 WALK_SUBTREE (DECL_SIZE_UNIT (field));
11708 if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
11709 WALK_SUBTREE (DECL_QUALIFIER (field));
11713 /* Same for scalar types. */
11714 else if (TREE_CODE (*type_p) == BOOLEAN_TYPE
11715 || TREE_CODE (*type_p) == ENUMERAL_TYPE
11716 || TREE_CODE (*type_p) == INTEGER_TYPE
11717 || TREE_CODE (*type_p) == FIXED_POINT_TYPE
11718 || TREE_CODE (*type_p) == REAL_TYPE)
11720 WALK_SUBTREE (TYPE_MIN_VALUE (*type_p));
11721 WALK_SUBTREE (TYPE_MAX_VALUE (*type_p));
11724 WALK_SUBTREE (TYPE_SIZE (*type_p));
11725 WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (*type_p));
11727 /* FALLTHRU */
11729 default:
11730 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
11732 int i, len;
11734 /* Walk over all the sub-trees of this operand. */
11735 len = TREE_OPERAND_LENGTH (*tp);
11737 /* Go through the subtrees. We need to do this in forward order so
11738 that the scope of a FOR_EXPR is handled properly. */
11739 if (len)
11741 for (i = 0; i < len - 1; ++i)
11742 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11743 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len - 1));
11746 /* If this is a type, walk the needed fields in the type. */
11747 else if (TYPE_P (*tp))
11748 return walk_type_fields (*tp, func, data, pset, lh);
11749 break;
11752 /* We didn't find what we were looking for. */
11753 return NULL_TREE;
11755 #undef WALK_SUBTREE_TAIL
11757 #undef WALK_SUBTREE
11759 /* Like walk_tree, but does not walk duplicate nodes more than once. */
11761 tree
11762 walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
11763 walk_tree_lh lh)
11765 tree result;
11767 hash_set<tree> pset;
11768 result = walk_tree_1 (tp, func, data, &pset, lh);
11769 return result;
11773 tree
11774 tree_block (tree t)
11776 const enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
11778 if (IS_EXPR_CODE_CLASS (c))
11779 return LOCATION_BLOCK (t->exp.locus);
11780 gcc_unreachable ();
11781 return NULL;
11784 void
11785 tree_set_block (tree t, tree b)
11787 const enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
11789 if (IS_EXPR_CODE_CLASS (c))
11791 if (b)
11792 t->exp.locus = COMBINE_LOCATION_DATA (line_table, t->exp.locus, b);
11793 else
11794 t->exp.locus = LOCATION_LOCUS (t->exp.locus);
11796 else
11797 gcc_unreachable ();
11800 /* Create a nameless artificial label and put it in the current
11801 function context. The label has a location of LOC. Returns the
11802 newly created label. */
11804 tree
11805 create_artificial_label (location_t loc)
11807 tree lab = build_decl (loc,
11808 LABEL_DECL, NULL_TREE, void_type_node);
11810 DECL_ARTIFICIAL (lab) = 1;
11811 DECL_IGNORED_P (lab) = 1;
11812 DECL_CONTEXT (lab) = current_function_decl;
11813 return lab;
11816 /* Given a tree, try to return a useful variable name that we can use
11817 to prefix a temporary that is being assigned the value of the tree.
11818 I.E. given <temp> = &A, return A. */
11820 const char *
11821 get_name (tree t)
11823 tree stripped_decl;
11825 stripped_decl = t;
11826 STRIP_NOPS (stripped_decl);
11827 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
11828 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
11829 else if (TREE_CODE (stripped_decl) == SSA_NAME)
11831 tree name = SSA_NAME_IDENTIFIER (stripped_decl);
11832 if (!name)
11833 return NULL;
11834 return IDENTIFIER_POINTER (name);
11836 else
11838 switch (TREE_CODE (stripped_decl))
11840 case ADDR_EXPR:
11841 return get_name (TREE_OPERAND (stripped_decl, 0));
11842 default:
11843 return NULL;
11848 /* Return true if TYPE has a variable argument list. */
11850 bool
11851 stdarg_p (const_tree fntype)
11853 function_args_iterator args_iter;
11854 tree n = NULL_TREE, t;
11856 if (!fntype)
11857 return false;
11859 FOREACH_FUNCTION_ARGS (fntype, t, args_iter)
11861 n = t;
11864 return n != NULL_TREE && n != void_type_node;
11867 /* Return true if TYPE has a prototype. */
11869 bool
11870 prototype_p (const_tree fntype)
11872 tree t;
11874 gcc_assert (fntype != NULL_TREE);
11876 t = TYPE_ARG_TYPES (fntype);
11877 return (t != NULL_TREE);
11880 /* If BLOCK is inlined from an __attribute__((__artificial__))
11881 routine, return pointer to location from where it has been
11882 called. */
11883 location_t *
11884 block_nonartificial_location (tree block)
11886 location_t *ret = NULL;
11888 while (block && TREE_CODE (block) == BLOCK
11889 && BLOCK_ABSTRACT_ORIGIN (block))
11891 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
11893 while (TREE_CODE (ao) == BLOCK
11894 && BLOCK_ABSTRACT_ORIGIN (ao)
11895 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
11896 ao = BLOCK_ABSTRACT_ORIGIN (ao);
11898 if (TREE_CODE (ao) == FUNCTION_DECL)
11900 /* If AO is an artificial inline, point RET to the
11901 call site locus at which it has been inlined and continue
11902 the loop, in case AO's caller is also an artificial
11903 inline. */
11904 if (DECL_DECLARED_INLINE_P (ao)
11905 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
11906 ret = &BLOCK_SOURCE_LOCATION (block);
11907 else
11908 break;
11910 else if (TREE_CODE (ao) != BLOCK)
11911 break;
11913 block = BLOCK_SUPERCONTEXT (block);
11915 return ret;
11919 /* If EXP is inlined from an __attribute__((__artificial__))
11920 function, return the location of the original call expression. */
11922 location_t
11923 tree_nonartificial_location (tree exp)
11925 location_t *loc = block_nonartificial_location (TREE_BLOCK (exp));
11927 if (loc)
11928 return *loc;
11929 else
11930 return EXPR_LOCATION (exp);
11934 /* These are the hash table functions for the hash table of OPTIMIZATION_NODEq
11935 nodes. */
11937 /* Return the hash code X, an OPTIMIZATION_NODE or TARGET_OPTION code. */
11939 hashval_t
11940 cl_option_hasher::hash (tree x)
11942 const_tree const t = x;
11943 const char *p;
11944 size_t i;
11945 size_t len = 0;
11946 hashval_t hash = 0;
11948 if (TREE_CODE (t) == OPTIMIZATION_NODE)
11950 p = (const char *)TREE_OPTIMIZATION (t);
11951 len = sizeof (struct cl_optimization);
11954 else if (TREE_CODE (t) == TARGET_OPTION_NODE)
11955 return cl_target_option_hash (TREE_TARGET_OPTION (t));
11957 else
11958 gcc_unreachable ();
11960 /* assume most opt flags are just 0/1, some are 2-3, and a few might be
11961 something else. */
11962 for (i = 0; i < len; i++)
11963 if (p[i])
11964 hash = (hash << 4) ^ ((i << 2) | p[i]);
11966 return hash;
11969 /* Return nonzero if the value represented by *X (an OPTIMIZATION or
11970 TARGET_OPTION tree node) is the same as that given by *Y, which is the
11971 same. */
11973 bool
11974 cl_option_hasher::equal (tree x, tree y)
11976 const_tree const xt = x;
11977 const_tree const yt = y;
11978 const char *xp;
11979 const char *yp;
11980 size_t len;
11982 if (TREE_CODE (xt) != TREE_CODE (yt))
11983 return 0;
11985 if (TREE_CODE (xt) == OPTIMIZATION_NODE)
11987 xp = (const char *)TREE_OPTIMIZATION (xt);
11988 yp = (const char *)TREE_OPTIMIZATION (yt);
11989 len = sizeof (struct cl_optimization);
11992 else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
11994 return cl_target_option_eq (TREE_TARGET_OPTION (xt),
11995 TREE_TARGET_OPTION (yt));
11998 else
11999 gcc_unreachable ();
12001 return (memcmp (xp, yp, len) == 0);
12004 /* Build an OPTIMIZATION_NODE based on the options in OPTS. */
12006 tree
12007 build_optimization_node (struct gcc_options *opts)
12009 tree t;
12011 /* Use the cache of optimization nodes. */
12013 cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node),
12014 opts);
12016 tree *slot = cl_option_hash_table->find_slot (cl_optimization_node, INSERT);
12017 t = *slot;
12018 if (!t)
12020 /* Insert this one into the hash table. */
12021 t = cl_optimization_node;
12022 *slot = t;
12024 /* Make a new node for next time round. */
12025 cl_optimization_node = make_node (OPTIMIZATION_NODE);
12028 return t;
12031 /* Build a TARGET_OPTION_NODE based on the options in OPTS. */
12033 tree
12034 build_target_option_node (struct gcc_options *opts)
12036 tree t;
12038 /* Use the cache of optimization nodes. */
12040 cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node),
12041 opts);
12043 tree *slot = cl_option_hash_table->find_slot (cl_target_option_node, INSERT);
12044 t = *slot;
12045 if (!t)
12047 /* Insert this one into the hash table. */
12048 t = cl_target_option_node;
12049 *slot = t;
12051 /* Make a new node for next time round. */
12052 cl_target_option_node = make_node (TARGET_OPTION_NODE);
12055 return t;
12058 /* Clear TREE_TARGET_GLOBALS of all TARGET_OPTION_NODE trees,
12059 so that they aren't saved during PCH writing. */
12061 void
12062 prepare_target_option_nodes_for_pch (void)
12064 hash_table<cl_option_hasher>::iterator iter = cl_option_hash_table->begin ();
12065 for (; iter != cl_option_hash_table->end (); ++iter)
12066 if (TREE_CODE (*iter) == TARGET_OPTION_NODE)
12067 TREE_TARGET_GLOBALS (*iter) = NULL;
12070 /* Determine the "ultimate origin" of a block. The block may be an inlined
12071 instance of an inlined instance of a block which is local to an inline
12072 function, so we have to trace all of the way back through the origin chain
12073 to find out what sort of node actually served as the original seed for the
12074 given block. */
12076 tree
12077 block_ultimate_origin (const_tree block)
12079 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
12081 /* BLOCK_ABSTRACT_ORIGIN can point to itself; ignore that if
12082 we're trying to output the abstract instance of this function. */
12083 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
12084 return NULL_TREE;
12086 if (immediate_origin == NULL_TREE)
12087 return NULL_TREE;
12088 else
12090 tree ret_val;
12091 tree lookahead = immediate_origin;
12095 ret_val = lookahead;
12096 lookahead = (TREE_CODE (ret_val) == BLOCK
12097 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
12099 while (lookahead != NULL && lookahead != ret_val);
12101 /* The block's abstract origin chain may not be the *ultimate* origin of
12102 the block. It could lead to a DECL that has an abstract origin set.
12103 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
12104 will give us if it has one). Note that DECL's abstract origins are
12105 supposed to be the most distant ancestor (or so decl_ultimate_origin
12106 claims), so we don't need to loop following the DECL origins. */
12107 if (DECL_P (ret_val))
12108 return DECL_ORIGIN (ret_val);
12110 return ret_val;
12114 /* Return true iff conversion from INNER_TYPE to OUTER_TYPE generates
12115 no instruction. */
12117 bool
12118 tree_nop_conversion_p (const_tree outer_type, const_tree inner_type)
12120 /* Use precision rather then machine mode when we can, which gives
12121 the correct answer even for submode (bit-field) types. */
12122 if ((INTEGRAL_TYPE_P (outer_type)
12123 || POINTER_TYPE_P (outer_type)
12124 || TREE_CODE (outer_type) == OFFSET_TYPE)
12125 && (INTEGRAL_TYPE_P (inner_type)
12126 || POINTER_TYPE_P (inner_type)
12127 || TREE_CODE (inner_type) == OFFSET_TYPE))
12128 return TYPE_PRECISION (outer_type) == TYPE_PRECISION (inner_type);
12130 /* Otherwise fall back on comparing machine modes (e.g. for
12131 aggregate types, floats). */
12132 return TYPE_MODE (outer_type) == TYPE_MODE (inner_type);
12135 /* Return true iff conversion in EXP generates no instruction. Mark
12136 it inline so that we fully inline into the stripping functions even
12137 though we have two uses of this function. */
12139 static inline bool
12140 tree_nop_conversion (const_tree exp)
12142 tree outer_type, inner_type;
12144 if (!CONVERT_EXPR_P (exp)
12145 && TREE_CODE (exp) != NON_LVALUE_EXPR)
12146 return false;
12147 if (TREE_OPERAND (exp, 0) == error_mark_node)
12148 return false;
12150 outer_type = TREE_TYPE (exp);
12151 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
12153 if (!inner_type)
12154 return false;
12156 return tree_nop_conversion_p (outer_type, inner_type);
12159 /* Return true iff conversion in EXP generates no instruction. Don't
12160 consider conversions changing the signedness. */
12162 static bool
12163 tree_sign_nop_conversion (const_tree exp)
12165 tree outer_type, inner_type;
12167 if (!tree_nop_conversion (exp))
12168 return false;
12170 outer_type = TREE_TYPE (exp);
12171 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
12173 return (TYPE_UNSIGNED (outer_type) == TYPE_UNSIGNED (inner_type)
12174 && POINTER_TYPE_P (outer_type) == POINTER_TYPE_P (inner_type));
12177 /* Strip conversions from EXP according to tree_nop_conversion and
12178 return the resulting expression. */
12180 tree
12181 tree_strip_nop_conversions (tree exp)
12183 while (tree_nop_conversion (exp))
12184 exp = TREE_OPERAND (exp, 0);
12185 return exp;
12188 /* Strip conversions from EXP according to tree_sign_nop_conversion
12189 and return the resulting expression. */
12191 tree
12192 tree_strip_sign_nop_conversions (tree exp)
12194 while (tree_sign_nop_conversion (exp))
12195 exp = TREE_OPERAND (exp, 0);
12196 return exp;
12199 /* Avoid any floating point extensions from EXP. */
12200 tree
12201 strip_float_extensions (tree exp)
12203 tree sub, expt, subt;
12205 /* For floating point constant look up the narrowest type that can hold
12206 it properly and handle it like (type)(narrowest_type)constant.
12207 This way we can optimize for instance a=a*2.0 where "a" is float
12208 but 2.0 is double constant. */
12209 if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
12211 REAL_VALUE_TYPE orig;
12212 tree type = NULL;
12214 orig = TREE_REAL_CST (exp);
12215 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
12216 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
12217 type = float_type_node;
12218 else if (TYPE_PRECISION (TREE_TYPE (exp))
12219 > TYPE_PRECISION (double_type_node)
12220 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
12221 type = double_type_node;
12222 if (type)
12223 return build_real_truncate (type, orig);
12226 if (!CONVERT_EXPR_P (exp))
12227 return exp;
12229 sub = TREE_OPERAND (exp, 0);
12230 subt = TREE_TYPE (sub);
12231 expt = TREE_TYPE (exp);
12233 if (!FLOAT_TYPE_P (subt))
12234 return exp;
12236 if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
12237 return exp;
12239 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
12240 return exp;
12242 return strip_float_extensions (sub);
12245 /* Strip out all handled components that produce invariant
12246 offsets. */
12248 const_tree
12249 strip_invariant_refs (const_tree op)
12251 while (handled_component_p (op))
12253 switch (TREE_CODE (op))
12255 case ARRAY_REF:
12256 case ARRAY_RANGE_REF:
12257 if (!is_gimple_constant (TREE_OPERAND (op, 1))
12258 || TREE_OPERAND (op, 2) != NULL_TREE
12259 || TREE_OPERAND (op, 3) != NULL_TREE)
12260 return NULL;
12261 break;
12263 case COMPONENT_REF:
12264 if (TREE_OPERAND (op, 2) != NULL_TREE)
12265 return NULL;
12266 break;
12268 default:;
12270 op = TREE_OPERAND (op, 0);
12273 return op;
12276 static GTY(()) tree gcc_eh_personality_decl;
12278 /* Return the GCC personality function decl. */
12280 tree
12281 lhd_gcc_personality (void)
12283 if (!gcc_eh_personality_decl)
12284 gcc_eh_personality_decl = build_personality_function ("gcc");
12285 return gcc_eh_personality_decl;
12288 /* TARGET is a call target of GIMPLE call statement
12289 (obtained by gimple_call_fn). Return true if it is
12290 OBJ_TYPE_REF representing an virtual call of C++ method.
12291 (As opposed to OBJ_TYPE_REF representing objc calls
12292 through a cast where middle-end devirtualization machinery
12293 can't apply.) */
12295 bool
12296 virtual_method_call_p (const_tree target)
12298 if (TREE_CODE (target) != OBJ_TYPE_REF)
12299 return false;
12300 tree t = TREE_TYPE (target);
12301 gcc_checking_assert (TREE_CODE (t) == POINTER_TYPE);
12302 t = TREE_TYPE (t);
12303 if (TREE_CODE (t) == FUNCTION_TYPE)
12304 return false;
12305 gcc_checking_assert (TREE_CODE (t) == METHOD_TYPE);
12306 /* If we do not have BINFO associated, it means that type was built
12307 without devirtualization enabled. Do not consider this a virtual
12308 call. */
12309 if (!TYPE_BINFO (obj_type_ref_class (target)))
12310 return false;
12311 return true;
12314 /* REF is OBJ_TYPE_REF, return the class the ref corresponds to. */
12316 tree
12317 obj_type_ref_class (const_tree ref)
12319 gcc_checking_assert (TREE_CODE (ref) == OBJ_TYPE_REF);
12320 ref = TREE_TYPE (ref);
12321 gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE);
12322 ref = TREE_TYPE (ref);
12323 /* We look for type THIS points to. ObjC also builds
12324 OBJ_TYPE_REF with non-method calls, Their first parameter
12325 ID however also corresponds to class type. */
12326 gcc_checking_assert (TREE_CODE (ref) == METHOD_TYPE
12327 || TREE_CODE (ref) == FUNCTION_TYPE);
12328 ref = TREE_VALUE (TYPE_ARG_TYPES (ref));
12329 gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE);
12330 return TREE_TYPE (ref);
12333 /* Lookup sub-BINFO of BINFO of TYPE at offset POS. */
12335 static tree
12336 lookup_binfo_at_offset (tree binfo, tree type, HOST_WIDE_INT pos)
12338 unsigned int i;
12339 tree base_binfo, b;
12341 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
12342 if (pos == tree_to_shwi (BINFO_OFFSET (base_binfo))
12343 && types_same_for_odr (TREE_TYPE (base_binfo), type))
12344 return base_binfo;
12345 else if ((b = lookup_binfo_at_offset (base_binfo, type, pos)) != NULL)
12346 return b;
12347 return NULL;
12350 /* Try to find a base info of BINFO that would have its field decl at offset
12351 OFFSET within the BINFO type and which is of EXPECTED_TYPE. If it can be
12352 found, return, otherwise return NULL_TREE. */
12354 tree
12355 get_binfo_at_offset (tree binfo, HOST_WIDE_INT offset, tree expected_type)
12357 tree type = BINFO_TYPE (binfo);
12359 while (true)
12361 HOST_WIDE_INT pos, size;
12362 tree fld;
12363 int i;
12365 if (types_same_for_odr (type, expected_type))
12366 return binfo;
12367 if (offset < 0)
12368 return NULL_TREE;
12370 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
12372 if (TREE_CODE (fld) != FIELD_DECL || !DECL_ARTIFICIAL (fld))
12373 continue;
12375 pos = int_bit_position (fld);
12376 size = tree_to_uhwi (DECL_SIZE (fld));
12377 if (pos <= offset && (pos + size) > offset)
12378 break;
12380 if (!fld || TREE_CODE (TREE_TYPE (fld)) != RECORD_TYPE)
12381 return NULL_TREE;
12383 /* Offset 0 indicates the primary base, whose vtable contents are
12384 represented in the binfo for the derived class. */
12385 else if (offset != 0)
12387 tree found_binfo = NULL, base_binfo;
12388 /* Offsets in BINFO are in bytes relative to the whole structure
12389 while POS is in bits relative to the containing field. */
12390 int binfo_offset = (tree_to_shwi (BINFO_OFFSET (binfo)) + pos
12391 / BITS_PER_UNIT);
12393 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
12394 if (tree_to_shwi (BINFO_OFFSET (base_binfo)) == binfo_offset
12395 && types_same_for_odr (TREE_TYPE (base_binfo), TREE_TYPE (fld)))
12397 found_binfo = base_binfo;
12398 break;
12400 if (found_binfo)
12401 binfo = found_binfo;
12402 else
12403 binfo = lookup_binfo_at_offset (binfo, TREE_TYPE (fld),
12404 binfo_offset);
12407 type = TREE_TYPE (fld);
12408 offset -= pos;
12412 /* Returns true if X is a typedef decl. */
12414 bool
12415 is_typedef_decl (const_tree x)
12417 return (x && TREE_CODE (x) == TYPE_DECL
12418 && DECL_ORIGINAL_TYPE (x) != NULL_TREE);
12421 /* Returns true iff TYPE is a type variant created for a typedef. */
12423 bool
12424 typedef_variant_p (const_tree type)
12426 return is_typedef_decl (TYPE_NAME (type));
12429 /* Warn about a use of an identifier which was marked deprecated. */
12430 void
12431 warn_deprecated_use (tree node, tree attr)
12433 const char *msg;
12435 if (node == 0 || !warn_deprecated_decl)
12436 return;
12438 if (!attr)
12440 if (DECL_P (node))
12441 attr = DECL_ATTRIBUTES (node);
12442 else if (TYPE_P (node))
12444 tree decl = TYPE_STUB_DECL (node);
12445 if (decl)
12446 attr = lookup_attribute ("deprecated",
12447 TYPE_ATTRIBUTES (TREE_TYPE (decl)));
12451 if (attr)
12452 attr = lookup_attribute ("deprecated", attr);
12454 if (attr)
12455 msg = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr)));
12456 else
12457 msg = NULL;
12459 bool w;
12460 if (DECL_P (node))
12462 if (msg)
12463 w = warning (OPT_Wdeprecated_declarations,
12464 "%qD is deprecated: %s", node, msg);
12465 else
12466 w = warning (OPT_Wdeprecated_declarations,
12467 "%qD is deprecated", node);
12468 if (w)
12469 inform (DECL_SOURCE_LOCATION (node), "declared here");
12471 else if (TYPE_P (node))
12473 tree what = NULL_TREE;
12474 tree decl = TYPE_STUB_DECL (node);
12476 if (TYPE_NAME (node))
12478 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
12479 what = TYPE_NAME (node);
12480 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
12481 && DECL_NAME (TYPE_NAME (node)))
12482 what = DECL_NAME (TYPE_NAME (node));
12485 if (decl)
12487 if (what)
12489 if (msg)
12490 w = warning (OPT_Wdeprecated_declarations,
12491 "%qE is deprecated: %s", what, msg);
12492 else
12493 w = warning (OPT_Wdeprecated_declarations,
12494 "%qE is deprecated", what);
12496 else
12498 if (msg)
12499 w = warning (OPT_Wdeprecated_declarations,
12500 "type is deprecated: %s", msg);
12501 else
12502 w = warning (OPT_Wdeprecated_declarations,
12503 "type is deprecated");
12505 if (w)
12506 inform (DECL_SOURCE_LOCATION (decl), "declared here");
12508 else
12510 if (what)
12512 if (msg)
12513 warning (OPT_Wdeprecated_declarations, "%qE is deprecated: %s",
12514 what, msg);
12515 else
12516 warning (OPT_Wdeprecated_declarations, "%qE is deprecated", what);
12518 else
12520 if (msg)
12521 warning (OPT_Wdeprecated_declarations, "type is deprecated: %s",
12522 msg);
12523 else
12524 warning (OPT_Wdeprecated_declarations, "type is deprecated");
12530 /* Return true if REF has a COMPONENT_REF with a bit-field field declaration
12531 somewhere in it. */
12533 bool
12534 contains_bitfld_component_ref_p (const_tree ref)
12536 while (handled_component_p (ref))
12538 if (TREE_CODE (ref) == COMPONENT_REF
12539 && DECL_BIT_FIELD (TREE_OPERAND (ref, 1)))
12540 return true;
12541 ref = TREE_OPERAND (ref, 0);
12544 return false;
12547 /* Try to determine whether a TRY_CATCH expression can fall through.
12548 This is a subroutine of block_may_fallthru. */
12550 static bool
12551 try_catch_may_fallthru (const_tree stmt)
12553 tree_stmt_iterator i;
12555 /* If the TRY block can fall through, the whole TRY_CATCH can
12556 fall through. */
12557 if (block_may_fallthru (TREE_OPERAND (stmt, 0)))
12558 return true;
12560 i = tsi_start (TREE_OPERAND (stmt, 1));
12561 switch (TREE_CODE (tsi_stmt (i)))
12563 case CATCH_EXPR:
12564 /* We expect to see a sequence of CATCH_EXPR trees, each with a
12565 catch expression and a body. The whole TRY_CATCH may fall
12566 through iff any of the catch bodies falls through. */
12567 for (; !tsi_end_p (i); tsi_next (&i))
12569 if (block_may_fallthru (CATCH_BODY (tsi_stmt (i))))
12570 return true;
12572 return false;
12574 case EH_FILTER_EXPR:
12575 /* The exception filter expression only matters if there is an
12576 exception. If the exception does not match EH_FILTER_TYPES,
12577 we will execute EH_FILTER_FAILURE, and we will fall through
12578 if that falls through. If the exception does match
12579 EH_FILTER_TYPES, the stack unwinder will continue up the
12580 stack, so we will not fall through. We don't know whether we
12581 will throw an exception which matches EH_FILTER_TYPES or not,
12582 so we just ignore EH_FILTER_TYPES and assume that we might
12583 throw an exception which doesn't match. */
12584 return block_may_fallthru (EH_FILTER_FAILURE (tsi_stmt (i)));
12586 default:
12587 /* This case represents statements to be executed when an
12588 exception occurs. Those statements are implicitly followed
12589 by a RESX statement to resume execution after the exception.
12590 So in this case the TRY_CATCH never falls through. */
12591 return false;
12595 /* Try to determine if we can fall out of the bottom of BLOCK. This guess
12596 need not be 100% accurate; simply be conservative and return true if we
12597 don't know. This is used only to avoid stupidly generating extra code.
12598 If we're wrong, we'll just delete the extra code later. */
12600 bool
12601 block_may_fallthru (const_tree block)
12603 /* This CONST_CAST is okay because expr_last returns its argument
12604 unmodified and we assign it to a const_tree. */
12605 const_tree stmt = expr_last (CONST_CAST_TREE (block));
12607 switch (stmt ? TREE_CODE (stmt) : ERROR_MARK)
12609 case GOTO_EXPR:
12610 case RETURN_EXPR:
12611 /* Easy cases. If the last statement of the block implies
12612 control transfer, then we can't fall through. */
12613 return false;
12615 case SWITCH_EXPR:
12616 /* If SWITCH_LABELS is set, this is lowered, and represents a
12617 branch to a selected label and hence can not fall through.
12618 Otherwise SWITCH_BODY is set, and the switch can fall
12619 through. */
12620 return SWITCH_LABELS (stmt) == NULL_TREE;
12622 case COND_EXPR:
12623 if (block_may_fallthru (COND_EXPR_THEN (stmt)))
12624 return true;
12625 return block_may_fallthru (COND_EXPR_ELSE (stmt));
12627 case BIND_EXPR:
12628 return block_may_fallthru (BIND_EXPR_BODY (stmt));
12630 case TRY_CATCH_EXPR:
12631 return try_catch_may_fallthru (stmt);
12633 case TRY_FINALLY_EXPR:
12634 /* The finally clause is always executed after the try clause,
12635 so if it does not fall through, then the try-finally will not
12636 fall through. Otherwise, if the try clause does not fall
12637 through, then when the finally clause falls through it will
12638 resume execution wherever the try clause was going. So the
12639 whole try-finally will only fall through if both the try
12640 clause and the finally clause fall through. */
12641 return (block_may_fallthru (TREE_OPERAND (stmt, 0))
12642 && block_may_fallthru (TREE_OPERAND (stmt, 1)));
12644 case MODIFY_EXPR:
12645 if (TREE_CODE (TREE_OPERAND (stmt, 1)) == CALL_EXPR)
12646 stmt = TREE_OPERAND (stmt, 1);
12647 else
12648 return true;
12649 /* FALLTHRU */
12651 case CALL_EXPR:
12652 /* Functions that do not return do not fall through. */
12653 return (call_expr_flags (stmt) & ECF_NORETURN) == 0;
12655 case CLEANUP_POINT_EXPR:
12656 return block_may_fallthru (TREE_OPERAND (stmt, 0));
12658 case TARGET_EXPR:
12659 return block_may_fallthru (TREE_OPERAND (stmt, 1));
12661 case ERROR_MARK:
12662 return true;
12664 default:
12665 return lang_hooks.block_may_fallthru (stmt);
12669 /* True if we are using EH to handle cleanups. */
12670 static bool using_eh_for_cleanups_flag = false;
12672 /* This routine is called from front ends to indicate eh should be used for
12673 cleanups. */
12674 void
12675 using_eh_for_cleanups (void)
12677 using_eh_for_cleanups_flag = true;
12680 /* Query whether EH is used for cleanups. */
12681 bool
12682 using_eh_for_cleanups_p (void)
12684 return using_eh_for_cleanups_flag;
12687 /* Wrapper for tree_code_name to ensure that tree code is valid */
12688 const char *
12689 get_tree_code_name (enum tree_code code)
12691 const char *invalid = "<invalid tree code>";
12693 if (code >= MAX_TREE_CODES)
12694 return invalid;
12696 return tree_code_name[code];
12699 /* Drops the TREE_OVERFLOW flag from T. */
12701 tree
12702 drop_tree_overflow (tree t)
12704 gcc_checking_assert (TREE_OVERFLOW (t));
12706 /* For tree codes with a sharing machinery re-build the result. */
12707 if (TREE_CODE (t) == INTEGER_CST)
12708 return wide_int_to_tree (TREE_TYPE (t), t);
12710 /* Otherwise, as all tcc_constants are possibly shared, copy the node
12711 and drop the flag. */
12712 t = copy_node (t);
12713 TREE_OVERFLOW (t) = 0;
12714 return t;
12717 /* Given a memory reference expression T, return its base address.
12718 The base address of a memory reference expression is the main
12719 object being referenced. For instance, the base address for
12720 'array[i].fld[j]' is 'array'. You can think of this as stripping
12721 away the offset part from a memory address.
12723 This function calls handled_component_p to strip away all the inner
12724 parts of the memory reference until it reaches the base object. */
12726 tree
12727 get_base_address (tree t)
12729 while (handled_component_p (t))
12730 t = TREE_OPERAND (t, 0);
12732 if ((TREE_CODE (t) == MEM_REF
12733 || TREE_CODE (t) == TARGET_MEM_REF)
12734 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR)
12735 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
12737 /* ??? Either the alias oracle or all callers need to properly deal
12738 with WITH_SIZE_EXPRs before we can look through those. */
12739 if (TREE_CODE (t) == WITH_SIZE_EXPR)
12740 return NULL_TREE;
12742 return t;
12745 /* Return a tree of sizetype representing the size, in bytes, of the element
12746 of EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
12748 tree
12749 array_ref_element_size (tree exp)
12751 tree aligned_size = TREE_OPERAND (exp, 3);
12752 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)));
12753 location_t loc = EXPR_LOCATION (exp);
12755 /* If a size was specified in the ARRAY_REF, it's the size measured
12756 in alignment units of the element type. So multiply by that value. */
12757 if (aligned_size)
12759 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
12760 sizetype from another type of the same width and signedness. */
12761 if (TREE_TYPE (aligned_size) != sizetype)
12762 aligned_size = fold_convert_loc (loc, sizetype, aligned_size);
12763 return size_binop_loc (loc, MULT_EXPR, aligned_size,
12764 size_int (TYPE_ALIGN_UNIT (elmt_type)));
12767 /* Otherwise, take the size from that of the element type. Substitute
12768 any PLACEHOLDER_EXPR that we have. */
12769 else
12770 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (elmt_type), exp);
12773 /* Return a tree representing the lower bound of the array mentioned in
12774 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
12776 tree
12777 array_ref_low_bound (tree exp)
12779 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
12781 /* If a lower bound is specified in EXP, use it. */
12782 if (TREE_OPERAND (exp, 2))
12783 return TREE_OPERAND (exp, 2);
12785 /* Otherwise, if there is a domain type and it has a lower bound, use it,
12786 substituting for a PLACEHOLDER_EXPR as needed. */
12787 if (domain_type && TYPE_MIN_VALUE (domain_type))
12788 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MIN_VALUE (domain_type), exp);
12790 /* Otherwise, return a zero of the appropriate type. */
12791 return build_int_cst (TREE_TYPE (TREE_OPERAND (exp, 1)), 0);
12794 /* Return a tree representing the upper bound of the array mentioned in
12795 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
12797 tree
12798 array_ref_up_bound (tree exp)
12800 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
12802 /* If there is a domain type and it has an upper bound, use it, substituting
12803 for a PLACEHOLDER_EXPR as needed. */
12804 if (domain_type && TYPE_MAX_VALUE (domain_type))
12805 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MAX_VALUE (domain_type), exp);
12807 /* Otherwise fail. */
12808 return NULL_TREE;
12811 /* Returns true if REF is an array reference to an array at the end of
12812 a structure. If this is the case, the array may be allocated larger
12813 than its upper bound implies. */
12815 bool
12816 array_at_struct_end_p (tree ref)
12818 if (TREE_CODE (ref) != ARRAY_REF
12819 && TREE_CODE (ref) != ARRAY_RANGE_REF)
12820 return false;
12822 while (handled_component_p (ref))
12824 /* If the reference chain contains a component reference to a
12825 non-union type and there follows another field the reference
12826 is not at the end of a structure. */
12827 if (TREE_CODE (ref) == COMPONENT_REF
12828 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
12830 tree nextf = DECL_CHAIN (TREE_OPERAND (ref, 1));
12831 while (nextf && TREE_CODE (nextf) != FIELD_DECL)
12832 nextf = DECL_CHAIN (nextf);
12833 if (nextf)
12834 return false;
12837 ref = TREE_OPERAND (ref, 0);
12840 /* If the reference is based on a declared entity, the size of the array
12841 is constrained by its given domain. */
12842 if (DECL_P (ref))
12843 return false;
12845 return true;
12848 /* Return a tree representing the offset, in bytes, of the field referenced
12849 by EXP. This does not include any offset in DECL_FIELD_BIT_OFFSET. */
12851 tree
12852 component_ref_field_offset (tree exp)
12854 tree aligned_offset = TREE_OPERAND (exp, 2);
12855 tree field = TREE_OPERAND (exp, 1);
12856 location_t loc = EXPR_LOCATION (exp);
12858 /* If an offset was specified in the COMPONENT_REF, it's the offset measured
12859 in units of DECL_OFFSET_ALIGN / BITS_PER_UNIT. So multiply by that
12860 value. */
12861 if (aligned_offset)
12863 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
12864 sizetype from another type of the same width and signedness. */
12865 if (TREE_TYPE (aligned_offset) != sizetype)
12866 aligned_offset = fold_convert_loc (loc, sizetype, aligned_offset);
12867 return size_binop_loc (loc, MULT_EXPR, aligned_offset,
12868 size_int (DECL_OFFSET_ALIGN (field)
12869 / BITS_PER_UNIT));
12872 /* Otherwise, take the offset from that of the field. Substitute
12873 any PLACEHOLDER_EXPR that we have. */
12874 else
12875 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (DECL_FIELD_OFFSET (field), exp);
12878 /* Return the machine mode of T. For vectors, returns the mode of the
12879 inner type. The main use case is to feed the result to HONOR_NANS,
12880 avoiding the BLKmode that a direct TYPE_MODE (T) might return. */
12882 machine_mode
12883 element_mode (const_tree t)
12885 if (!TYPE_P (t))
12886 t = TREE_TYPE (t);
12887 if (VECTOR_TYPE_P (t) || TREE_CODE (t) == COMPLEX_TYPE)
12888 t = TREE_TYPE (t);
12889 return TYPE_MODE (t);
12893 /* Veirfy that basic properties of T match TV and thus T can be a variant of
12894 TV. TV should be the more specified variant (i.e. the main variant). */
12896 static bool
12897 verify_type_variant (const_tree t, tree tv)
12899 /* Type variant can differ by:
12901 - TYPE_QUALS: TYPE_READONLY, TYPE_VOLATILE, TYPE_ATOMIC, TYPE_RESTRICT,
12902 ENCODE_QUAL_ADDR_SPACE.
12903 - main variant may be TYPE_COMPLETE_P and variant types !TYPE_COMPLETE_P
12904 in this case some values may not be set in the variant types
12905 (see TYPE_COMPLETE_P checks).
12906 - it is possible to have TYPE_ARTIFICIAL variant of non-artifical type
12907 - by TYPE_NAME and attributes (i.e. when variant originate by typedef)
12908 - TYPE_CANONICAL (TYPE_ALIAS_SET is the same among variants)
12909 - by the alignment: TYPE_ALIGN and TYPE_USER_ALIGN
12910 - during LTO by TYPE_CONTEXT if type is TYPE_FILE_SCOPE_P
12911 this is necessary to make it possible to merge types form different TUs
12912 - arrays, pointers and references may have TREE_TYPE that is a variant
12913 of TREE_TYPE of their main variants.
12914 - aggregates may have new TYPE_FIELDS list that list variants of
12915 the main variant TYPE_FIELDS.
12916 - vector types may differ by TYPE_VECTOR_OPAQUE
12917 - TYPE_METHODS is always NULL for vairant types and maintained for
12918 main variant only.
12921 /* Convenience macro for matching individual fields. */
12922 #define verify_variant_match(flag) \
12923 do { \
12924 if (flag (tv) != flag (t)) \
12926 error ("type variant differs by " #flag "."); \
12927 debug_tree (tv); \
12928 return false; \
12930 } while (false)
12932 /* tree_base checks. */
12934 verify_variant_match (TREE_CODE);
12935 /* FIXME: Ada builds non-artificial variants of artificial types. */
12936 if (TYPE_ARTIFICIAL (tv) && 0)
12937 verify_variant_match (TYPE_ARTIFICIAL);
12938 if (POINTER_TYPE_P (tv))
12939 verify_variant_match (TYPE_REF_CAN_ALIAS_ALL);
12940 /* FIXME: TYPE_SIZES_GIMPLIFIED may differs for Ada build. */
12941 verify_variant_match (TYPE_UNSIGNED);
12942 verify_variant_match (TYPE_ALIGN_OK);
12943 verify_variant_match (TYPE_PACKED);
12944 if (TREE_CODE (t) == REFERENCE_TYPE)
12945 verify_variant_match (TYPE_REF_IS_RVALUE);
12946 verify_variant_match (TYPE_SATURATING);
12947 /* FIXME: This check trigger during libstdc++ build. */
12948 if (RECORD_OR_UNION_TYPE_P (t) && COMPLETE_TYPE_P (t) && 0)
12949 verify_variant_match (TYPE_FINAL_P);
12951 /* tree_type_common checks. */
12953 if (COMPLETE_TYPE_P (t))
12955 verify_variant_match (TYPE_SIZE);
12956 verify_variant_match (TYPE_MODE);
12957 if (TYPE_SIZE_UNIT (t) != TYPE_SIZE_UNIT (tv)
12958 /* FIXME: ideally we should compare pointer equality, but java FE
12959 produce variants where size is INTEGER_CST of different type (int
12960 wrt size_type) during libjava biuld. */
12961 && !operand_equal_p (TYPE_SIZE_UNIT (t), TYPE_SIZE_UNIT (tv), 0))
12963 error ("type variant has different TYPE_SIZE_UNIT");
12964 debug_tree (tv);
12965 error ("type variant's TYPE_SIZE_UNIT");
12966 debug_tree (TYPE_SIZE_UNIT (tv));
12967 error ("type's TYPE_SIZE_UNIT");
12968 debug_tree (TYPE_SIZE_UNIT (t));
12969 return false;
12972 verify_variant_match (TYPE_PRECISION);
12973 verify_variant_match (TYPE_NEEDS_CONSTRUCTING);
12974 if (RECORD_OR_UNION_TYPE_P (t))
12975 verify_variant_match (TYPE_TRANSPARENT_AGGR);
12976 else if (TREE_CODE (t) == ARRAY_TYPE)
12977 verify_variant_match (TYPE_NONALIASED_COMPONENT);
12978 /* During LTO we merge variant lists from diferent translation units
12979 that may differ BY TYPE_CONTEXT that in turn may point
12980 to TRANSLATION_UNIT_DECL.
12981 Ada also builds variants of types with different TYPE_CONTEXT. */
12982 if ((!in_lto_p || !TYPE_FILE_SCOPE_P (t)) && 0)
12983 verify_variant_match (TYPE_CONTEXT);
12984 verify_variant_match (TYPE_STRING_FLAG);
12985 if (TYPE_ALIAS_SET_KNOWN_P (t) && TYPE_ALIAS_SET_KNOWN_P (tv))
12986 verify_variant_match (TYPE_ALIAS_SET);
12988 /* tree_type_non_common checks. */
12990 /* FIXME: C FE uses TYPE_VFIELD to record C_TYPE_INCOMPLETE_VARS
12991 and dangle the pointer from time to time. */
12992 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_VFIELD (t) != TYPE_VFIELD (tv)
12993 && (in_lto_p || !TYPE_VFIELD (tv)
12994 || TREE_CODE (TYPE_VFIELD (tv)) != TREE_LIST))
12996 error ("type variant has different TYPE_VFIELD");
12997 debug_tree (tv);
12998 return false;
13000 if ((TREE_CODE (t) == ENUMERAL_TYPE && COMPLETE_TYPE_P (t))
13001 || TREE_CODE (t) == INTEGER_TYPE
13002 || TREE_CODE (t) == BOOLEAN_TYPE
13003 || TREE_CODE (t) == REAL_TYPE
13004 || TREE_CODE (t) == FIXED_POINT_TYPE)
13006 verify_variant_match (TYPE_MAX_VALUE);
13007 verify_variant_match (TYPE_MIN_VALUE);
13009 if (TREE_CODE (t) == METHOD_TYPE)
13010 verify_variant_match (TYPE_METHOD_BASETYPE);
13011 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_METHODS (t))
13013 error ("type variant has TYPE_METHODS");
13014 debug_tree (tv);
13015 return false;
13017 if (TREE_CODE (t) == OFFSET_TYPE)
13018 verify_variant_match (TYPE_OFFSET_BASETYPE);
13019 if (TREE_CODE (t) == ARRAY_TYPE)
13020 verify_variant_match (TYPE_ARRAY_MAX_SIZE);
13021 /* FIXME: Be lax and allow TYPE_BINFO to be missing in variant types
13022 or even type's main variant. This is needed to make bootstrap pass
13023 and the bug seems new in GCC 5.
13024 C++ FE should be updated to make this consistent and we should check
13025 that TYPE_BINFO is always NULL for !COMPLETE_TYPE_P and otherwise there
13026 is a match with main variant.
13028 Also disable the check for Java for now because of parser hack that builds
13029 first an dummy BINFO and then sometimes replace it by real BINFO in some
13030 of the copies. */
13031 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t) && TYPE_BINFO (tv)
13032 && TYPE_BINFO (t) != TYPE_BINFO (tv)
13033 /* FIXME: Java sometimes keep dump TYPE_BINFOs on variant types.
13034 Since there is no cheap way to tell C++/Java type w/o LTO, do checking
13035 at LTO time only. */
13036 && (in_lto_p && odr_type_p (t)))
13038 error ("type variant has different TYPE_BINFO");
13039 debug_tree (tv);
13040 error ("type variant's TYPE_BINFO");
13041 debug_tree (TYPE_BINFO (tv));
13042 error ("type's TYPE_BINFO");
13043 debug_tree (TYPE_BINFO (t));
13044 return false;
13047 /* Check various uses of TYPE_VALUES_RAW. */
13048 if (TREE_CODE (t) == ENUMERAL_TYPE)
13049 verify_variant_match (TYPE_VALUES);
13050 else if (TREE_CODE (t) == ARRAY_TYPE)
13051 verify_variant_match (TYPE_DOMAIN);
13052 /* Permit incomplete variants of complete type. While FEs may complete
13053 all variants, this does not happen for C++ templates in all cases. */
13054 else if (RECORD_OR_UNION_TYPE_P (t)
13055 && COMPLETE_TYPE_P (t)
13056 && TYPE_FIELDS (t) != TYPE_FIELDS (tv))
13058 tree f1, f2;
13060 /* Fortran builds qualified variants as new records with items of
13061 qualified type. Verify that they looks same. */
13062 for (f1 = TYPE_FIELDS (t), f2 = TYPE_FIELDS (tv);
13063 f1 && f2;
13064 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
13065 if (TREE_CODE (f1) != FIELD_DECL || TREE_CODE (f2) != FIELD_DECL
13066 || (TYPE_MAIN_VARIANT (TREE_TYPE (f1))
13067 != TYPE_MAIN_VARIANT (TREE_TYPE (f2))
13068 /* FIXME: gfc_nonrestricted_type builds all types as variants
13069 with exception of pointer types. It deeply copies the type
13070 which means that we may end up with a variant type
13071 referring non-variant pointer. We may change it to
13072 produce types as variants, too, like
13073 objc_get_protocol_qualified_type does. */
13074 && !POINTER_TYPE_P (TREE_TYPE (f1)))
13075 || DECL_FIELD_OFFSET (f1) != DECL_FIELD_OFFSET (f2)
13076 || DECL_FIELD_BIT_OFFSET (f1) != DECL_FIELD_BIT_OFFSET (f2))
13077 break;
13078 if (f1 || f2)
13080 error ("type variant has different TYPE_FIELDS");
13081 debug_tree (tv);
13082 error ("first mismatch is field");
13083 debug_tree (f1);
13084 error ("and field");
13085 debug_tree (f2);
13086 return false;
13089 else if ((TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE))
13090 verify_variant_match (TYPE_ARG_TYPES);
13091 /* For C++ the qualified variant of array type is really an array type
13092 of qualified TREE_TYPE.
13093 objc builds variants of pointer where pointer to type is a variant, too
13094 in objc_get_protocol_qualified_type. */
13095 if (TREE_TYPE (t) != TREE_TYPE (tv)
13096 && ((TREE_CODE (t) != ARRAY_TYPE
13097 && !POINTER_TYPE_P (t))
13098 || TYPE_MAIN_VARIANT (TREE_TYPE (t))
13099 != TYPE_MAIN_VARIANT (TREE_TYPE (tv))))
13101 error ("type variant has different TREE_TYPE");
13102 debug_tree (tv);
13103 error ("type variant's TREE_TYPE");
13104 debug_tree (TREE_TYPE (tv));
13105 error ("type's TREE_TYPE");
13106 debug_tree (TREE_TYPE (t));
13107 return false;
13109 if (type_with_alias_set_p (t)
13110 && !gimple_canonical_types_compatible_p (t, tv, false))
13112 error ("type is not compatible with its vairant");
13113 debug_tree (tv);
13114 error ("type variant's TREE_TYPE");
13115 debug_tree (TREE_TYPE (tv));
13116 error ("type's TREE_TYPE");
13117 debug_tree (TREE_TYPE (t));
13118 return false;
13120 return true;
13121 #undef verify_variant_match
13125 /* The TYPE_CANONICAL merging machinery. It should closely resemble
13126 the middle-end types_compatible_p function. It needs to avoid
13127 claiming types are different for types that should be treated
13128 the same with respect to TBAA. Canonical types are also used
13129 for IL consistency checks via the useless_type_conversion_p
13130 predicate which does not handle all type kinds itself but falls
13131 back to pointer-comparison of TYPE_CANONICAL for aggregates
13132 for example. */
13134 /* Return true if TYPE_UNSIGNED of TYPE should be ignored for canonical
13135 type calculation because we need to allow inter-operability between signed
13136 and unsigned variants. */
13138 bool
13139 type_with_interoperable_signedness (const_tree type)
13141 /* Fortran standard require C_SIGNED_CHAR to be interoperable with both
13142 signed char and unsigned char. Similarly fortran FE builds
13143 C_SIZE_T as signed type, while C defines it unsigned. */
13145 return tree_code_for_canonical_type_merging (TREE_CODE (type))
13146 == INTEGER_TYPE
13147 && (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node)
13148 || TYPE_PRECISION (type) == TYPE_PRECISION (size_type_node));
13151 /* Return true iff T1 and T2 are structurally identical for what
13152 TBAA is concerned.
13153 This function is used both by lto.c canonical type merging and by the
13154 verifier. If TRUST_TYPE_CANONICAL we do not look into structure of types
13155 that have TYPE_CANONICAL defined and assume them equivalent. */
13157 bool
13158 gimple_canonical_types_compatible_p (const_tree t1, const_tree t2,
13159 bool trust_type_canonical)
13161 /* Type variants should be same as the main variant. When not doing sanity
13162 checking to verify this fact, go to main variants and save some work. */
13163 if (trust_type_canonical)
13165 t1 = TYPE_MAIN_VARIANT (t1);
13166 t2 = TYPE_MAIN_VARIANT (t2);
13169 /* Check first for the obvious case of pointer identity. */
13170 if (t1 == t2)
13171 return true;
13173 /* Check that we have two types to compare. */
13174 if (t1 == NULL_TREE || t2 == NULL_TREE)
13175 return false;
13177 /* We consider complete types always compatible with incomplete type.
13178 This does not make sense for canonical type calculation and thus we
13179 need to ensure that we are never called on it.
13181 FIXME: For more correctness the function probably should have three modes
13182 1) mode assuming that types are complete mathcing their structure
13183 2) mode allowing incomplete types but producing equivalence classes
13184 and thus ignoring all info from complete types
13185 3) mode allowing incomplete types to match complete but checking
13186 compatibility between complete types.
13188 1 and 2 can be used for canonical type calculation. 3 is the real
13189 definition of type compatibility that can be used i.e. for warnings during
13190 declaration merging. */
13192 gcc_assert (!trust_type_canonical
13193 || (type_with_alias_set_p (t1) && type_with_alias_set_p (t2)));
13194 /* If the types have been previously registered and found equal
13195 they still are. */
13196 if (TYPE_CANONICAL (t1) && TYPE_CANONICAL (t2)
13197 && trust_type_canonical)
13198 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
13200 /* Can't be the same type if the types don't have the same code. */
13201 enum tree_code code = tree_code_for_canonical_type_merging (TREE_CODE (t1));
13202 if (code != tree_code_for_canonical_type_merging (TREE_CODE (t2)))
13203 return false;
13205 /* Qualifiers do not matter for canonical type comparison purposes. */
13207 /* Void types and nullptr types are always the same. */
13208 if (TREE_CODE (t1) == VOID_TYPE
13209 || TREE_CODE (t1) == NULLPTR_TYPE)
13210 return true;
13212 /* Can't be the same type if they have different mode. */
13213 if (TYPE_MODE (t1) != TYPE_MODE (t2))
13214 return false;
13216 /* Non-aggregate types can be handled cheaply. */
13217 if (INTEGRAL_TYPE_P (t1)
13218 || SCALAR_FLOAT_TYPE_P (t1)
13219 || FIXED_POINT_TYPE_P (t1)
13220 || TREE_CODE (t1) == VECTOR_TYPE
13221 || TREE_CODE (t1) == COMPLEX_TYPE
13222 || TREE_CODE (t1) == OFFSET_TYPE
13223 || POINTER_TYPE_P (t1))
13225 /* Can't be the same type if they have different recision. */
13226 if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2))
13227 return false;
13229 /* In some cases the signed and unsigned types are required to be
13230 inter-operable. */
13231 if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2)
13232 && !type_with_interoperable_signedness (t1))
13233 return false;
13235 /* Fortran's C_SIGNED_CHAR is !TYPE_STRING_FLAG but needs to be
13236 interoperable with "signed char". Unless all frontends are revisited
13237 to agree on these types, we must ignore the flag completely. */
13239 /* Fortran standard define C_PTR type that is compatible with every
13240 C pointer. For this reason we need to glob all pointers into one.
13241 Still pointers in different address spaces are not compatible. */
13242 if (POINTER_TYPE_P (t1))
13244 if (TYPE_ADDR_SPACE (TREE_TYPE (t1))
13245 != TYPE_ADDR_SPACE (TREE_TYPE (t2)))
13246 return false;
13249 /* Tail-recurse to components. */
13250 if (TREE_CODE (t1) == VECTOR_TYPE
13251 || TREE_CODE (t1) == COMPLEX_TYPE)
13252 return gimple_canonical_types_compatible_p (TREE_TYPE (t1),
13253 TREE_TYPE (t2),
13254 trust_type_canonical);
13256 return true;
13259 /* Do type-specific comparisons. */
13260 switch (TREE_CODE (t1))
13262 case ARRAY_TYPE:
13263 /* Array types are the same if the element types are the same and
13264 the number of elements are the same. */
13265 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2),
13266 trust_type_canonical)
13267 || TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2)
13268 || TYPE_NONALIASED_COMPONENT (t1) != TYPE_NONALIASED_COMPONENT (t2))
13269 return false;
13270 else
13272 tree i1 = TYPE_DOMAIN (t1);
13273 tree i2 = TYPE_DOMAIN (t2);
13275 /* For an incomplete external array, the type domain can be
13276 NULL_TREE. Check this condition also. */
13277 if (i1 == NULL_TREE && i2 == NULL_TREE)
13278 return true;
13279 else if (i1 == NULL_TREE || i2 == NULL_TREE)
13280 return false;
13281 else
13283 tree min1 = TYPE_MIN_VALUE (i1);
13284 tree min2 = TYPE_MIN_VALUE (i2);
13285 tree max1 = TYPE_MAX_VALUE (i1);
13286 tree max2 = TYPE_MAX_VALUE (i2);
13288 /* The minimum/maximum values have to be the same. */
13289 if ((min1 == min2
13290 || (min1 && min2
13291 && ((TREE_CODE (min1) == PLACEHOLDER_EXPR
13292 && TREE_CODE (min2) == PLACEHOLDER_EXPR)
13293 || operand_equal_p (min1, min2, 0))))
13294 && (max1 == max2
13295 || (max1 && max2
13296 && ((TREE_CODE (max1) == PLACEHOLDER_EXPR
13297 && TREE_CODE (max2) == PLACEHOLDER_EXPR)
13298 || operand_equal_p (max1, max2, 0)))))
13299 return true;
13300 else
13301 return false;
13305 case METHOD_TYPE:
13306 case FUNCTION_TYPE:
13307 /* Function types are the same if the return type and arguments types
13308 are the same. */
13309 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2),
13310 trust_type_canonical))
13311 return false;
13313 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2))
13314 return true;
13315 else
13317 tree parms1, parms2;
13319 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
13320 parms1 && parms2;
13321 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
13323 if (!gimple_canonical_types_compatible_p
13324 (TREE_VALUE (parms1), TREE_VALUE (parms2),
13325 trust_type_canonical))
13326 return false;
13329 if (parms1 || parms2)
13330 return false;
13332 return true;
13335 case RECORD_TYPE:
13336 case UNION_TYPE:
13337 case QUAL_UNION_TYPE:
13339 tree f1, f2;
13341 /* For aggregate types, all the fields must be the same. */
13342 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
13343 f1 || f2;
13344 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
13346 /* Skip non-fields. */
13347 while (f1 && TREE_CODE (f1) != FIELD_DECL)
13348 f1 = TREE_CHAIN (f1);
13349 while (f2 && TREE_CODE (f2) != FIELD_DECL)
13350 f2 = TREE_CHAIN (f2);
13351 if (!f1 || !f2)
13352 break;
13353 /* The fields must have the same name, offset and type. */
13354 if (DECL_NONADDRESSABLE_P (f1) != DECL_NONADDRESSABLE_P (f2)
13355 || !gimple_compare_field_offset (f1, f2)
13356 || !gimple_canonical_types_compatible_p
13357 (TREE_TYPE (f1), TREE_TYPE (f2),
13358 trust_type_canonical))
13359 return false;
13362 /* If one aggregate has more fields than the other, they
13363 are not the same. */
13364 if (f1 || f2)
13365 return false;
13367 return true;
13370 default:
13371 /* Consider all types with language specific trees in them mutually
13372 compatible. This is executed only from verify_type and false
13373 positives can be tolerated. */
13374 gcc_assert (!in_lto_p);
13375 return true;
13379 /* Verify type T. */
13381 void
13382 verify_type (const_tree t)
13384 bool error_found = false;
13385 tree mv = TYPE_MAIN_VARIANT (t);
13386 if (!mv)
13388 error ("Main variant is not defined");
13389 error_found = true;
13391 else if (mv != TYPE_MAIN_VARIANT (mv))
13393 error ("TYPE_MAIN_VARIANT has different TYPE_MAIN_VARIANT");
13394 debug_tree (mv);
13395 error_found = true;
13397 else if (t != mv && !verify_type_variant (t, mv))
13398 error_found = true;
13400 tree ct = TYPE_CANONICAL (t);
13401 if (!ct)
13403 else if (TYPE_CANONICAL (t) != ct)
13405 error ("TYPE_CANONICAL has different TYPE_CANONICAL");
13406 debug_tree (ct);
13407 error_found = true;
13409 /* Method and function types can not be used to address memory and thus
13410 TYPE_CANONICAL really matters only for determining useless conversions.
13412 FIXME: C++ FE produce declarations of builtin functions that are not
13413 compatible with main variants. */
13414 else if (TREE_CODE (t) == FUNCTION_TYPE)
13416 else if (t != ct
13417 /* FIXME: gimple_canonical_types_compatible_p can not compare types
13418 with variably sized arrays because their sizes possibly
13419 gimplified to different variables. */
13420 && !variably_modified_type_p (ct, NULL)
13421 && !gimple_canonical_types_compatible_p (t, ct, false))
13423 error ("TYPE_CANONICAL is not compatible");
13424 debug_tree (ct);
13425 error_found = true;
13428 if (COMPLETE_TYPE_P (t) && TYPE_CANONICAL (t)
13429 && TYPE_MODE (t) != TYPE_MODE (TYPE_CANONICAL (t)))
13431 error ("TYPE_MODE of TYPE_CANONICAL is not compatible");
13432 debug_tree (ct);
13433 error_found = true;
13437 /* Check various uses of TYPE_MINVAL. */
13438 if (RECORD_OR_UNION_TYPE_P (t))
13440 /* FIXME: C FE uses TYPE_VFIELD to record C_TYPE_INCOMPLETE_VARS
13441 and danagle the pointer from time to time. */
13442 if (TYPE_VFIELD (t)
13443 && TREE_CODE (TYPE_VFIELD (t)) != FIELD_DECL
13444 && TREE_CODE (TYPE_VFIELD (t)) != TREE_LIST)
13446 error ("TYPE_VFIELD is not FIELD_DECL nor TREE_LIST");
13447 debug_tree (TYPE_VFIELD (t));
13448 error_found = true;
13451 else if (TREE_CODE (t) == POINTER_TYPE)
13453 if (TYPE_NEXT_PTR_TO (t)
13454 && TREE_CODE (TYPE_NEXT_PTR_TO (t)) != POINTER_TYPE)
13456 error ("TYPE_NEXT_PTR_TO is not POINTER_TYPE");
13457 debug_tree (TYPE_NEXT_PTR_TO (t));
13458 error_found = true;
13461 else if (TREE_CODE (t) == REFERENCE_TYPE)
13463 if (TYPE_NEXT_REF_TO (t)
13464 && TREE_CODE (TYPE_NEXT_REF_TO (t)) != REFERENCE_TYPE)
13466 error ("TYPE_NEXT_REF_TO is not REFERENCE_TYPE");
13467 debug_tree (TYPE_NEXT_REF_TO (t));
13468 error_found = true;
13471 else if (INTEGRAL_TYPE_P (t) || TREE_CODE (t) == REAL_TYPE
13472 || TREE_CODE (t) == FIXED_POINT_TYPE)
13474 /* FIXME: The following check should pass:
13475 useless_type_conversion_p (const_cast <tree> (t),
13476 TREE_TYPE (TYPE_MIN_VALUE (t))
13477 but does not for C sizetypes in LTO. */
13479 /* Java uses TYPE_MINVAL for TYPE_ARGUMENT_SIGNATURE. */
13480 else if (TYPE_MINVAL (t)
13481 && ((TREE_CODE (t) != METHOD_TYPE && TREE_CODE (t) != FUNCTION_TYPE)
13482 || in_lto_p))
13484 error ("TYPE_MINVAL non-NULL");
13485 debug_tree (TYPE_MINVAL (t));
13486 error_found = true;
13489 /* Check various uses of TYPE_MAXVAL. */
13490 if (RECORD_OR_UNION_TYPE_P (t))
13492 if (TYPE_METHODS (t) && TREE_CODE (TYPE_METHODS (t)) != FUNCTION_DECL
13493 && TREE_CODE (TYPE_METHODS (t)) != TEMPLATE_DECL
13494 && TYPE_METHODS (t) != error_mark_node)
13496 error ("TYPE_METHODS is not FUNCTION_DECL, TEMPLATE_DECL nor error_mark_node");
13497 debug_tree (TYPE_METHODS (t));
13498 error_found = true;
13501 else if (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE)
13503 if (TYPE_METHOD_BASETYPE (t)
13504 && TREE_CODE (TYPE_METHOD_BASETYPE (t)) != RECORD_TYPE
13505 && TREE_CODE (TYPE_METHOD_BASETYPE (t)) != UNION_TYPE)
13507 error ("TYPE_METHOD_BASETYPE is not record nor union");
13508 debug_tree (TYPE_METHOD_BASETYPE (t));
13509 error_found = true;
13512 else if (TREE_CODE (t) == OFFSET_TYPE)
13514 if (TYPE_OFFSET_BASETYPE (t)
13515 && TREE_CODE (TYPE_OFFSET_BASETYPE (t)) != RECORD_TYPE
13516 && TREE_CODE (TYPE_OFFSET_BASETYPE (t)) != UNION_TYPE)
13518 error ("TYPE_OFFSET_BASETYPE is not record nor union");
13519 debug_tree (TYPE_OFFSET_BASETYPE (t));
13520 error_found = true;
13523 else if (INTEGRAL_TYPE_P (t) || TREE_CODE (t) == REAL_TYPE
13524 || TREE_CODE (t) == FIXED_POINT_TYPE)
13526 /* FIXME: The following check should pass:
13527 useless_type_conversion_p (const_cast <tree> (t),
13528 TREE_TYPE (TYPE_MAX_VALUE (t))
13529 but does not for C sizetypes in LTO. */
13531 else if (TREE_CODE (t) == ARRAY_TYPE)
13533 if (TYPE_ARRAY_MAX_SIZE (t)
13534 && TREE_CODE (TYPE_ARRAY_MAX_SIZE (t)) != INTEGER_CST)
13536 error ("TYPE_ARRAY_MAX_SIZE not INTEGER_CST");
13537 debug_tree (TYPE_ARRAY_MAX_SIZE (t));
13538 error_found = true;
13541 else if (TYPE_MAXVAL (t))
13543 error ("TYPE_MAXVAL non-NULL");
13544 debug_tree (TYPE_MAXVAL (t));
13545 error_found = true;
13548 /* Check various uses of TYPE_BINFO. */
13549 if (RECORD_OR_UNION_TYPE_P (t))
13551 if (!TYPE_BINFO (t))
13553 else if (TREE_CODE (TYPE_BINFO (t)) != TREE_BINFO)
13555 error ("TYPE_BINFO is not TREE_BINFO");
13556 debug_tree (TYPE_BINFO (t));
13557 error_found = true;
13559 /* FIXME: Java builds invalid empty binfos that do not have
13560 TREE_TYPE set. */
13561 else if (TREE_TYPE (TYPE_BINFO (t)) != TYPE_MAIN_VARIANT (t) && 0)
13563 error ("TYPE_BINFO type is not TYPE_MAIN_VARIANT");
13564 debug_tree (TREE_TYPE (TYPE_BINFO (t)));
13565 error_found = true;
13568 else if (TYPE_LANG_SLOT_1 (t) && in_lto_p)
13570 error ("TYPE_LANG_SLOT_1 (binfo) field is non-NULL");
13571 debug_tree (TYPE_LANG_SLOT_1 (t));
13572 error_found = true;
13575 /* Check various uses of TYPE_VALUES_RAW. */
13576 if (TREE_CODE (t) == ENUMERAL_TYPE)
13577 for (tree l = TYPE_VALUES (t); l; l = TREE_CHAIN (l))
13579 tree value = TREE_VALUE (l);
13580 tree name = TREE_PURPOSE (l);
13582 /* C FE porduce INTEGER_CST of INTEGER_TYPE, while C++ FE uses
13583 CONST_DECL of ENUMERAL TYPE. */
13584 if (TREE_CODE (value) != INTEGER_CST && TREE_CODE (value) != CONST_DECL)
13586 error ("Enum value is not CONST_DECL or INTEGER_CST");
13587 debug_tree (value);
13588 debug_tree (name);
13589 error_found = true;
13591 if (TREE_CODE (TREE_TYPE (value)) != INTEGER_TYPE
13592 && !useless_type_conversion_p (const_cast <tree> (t), TREE_TYPE (value)))
13594 error ("Enum value type is not INTEGER_TYPE nor convertible to the enum");
13595 debug_tree (value);
13596 debug_tree (name);
13597 error_found = true;
13599 if (TREE_CODE (name) != IDENTIFIER_NODE)
13601 error ("Enum value name is not IDENTIFIER_NODE");
13602 debug_tree (value);
13603 debug_tree (name);
13604 error_found = true;
13607 else if (TREE_CODE (t) == ARRAY_TYPE)
13609 if (TYPE_DOMAIN (t) && TREE_CODE (TYPE_DOMAIN (t)) != INTEGER_TYPE)
13611 error ("Array TYPE_DOMAIN is not integer type");
13612 debug_tree (TYPE_DOMAIN (t));
13613 error_found = true;
13616 else if (RECORD_OR_UNION_TYPE_P (t))
13617 for (tree fld = TYPE_FIELDS (t); fld; fld = TREE_CHAIN (fld))
13619 /* TODO: verify properties of decls. */
13620 if (TREE_CODE (fld) == FIELD_DECL)
13622 else if (TREE_CODE (fld) == TYPE_DECL)
13624 else if (TREE_CODE (fld) == CONST_DECL)
13626 else if (TREE_CODE (fld) == VAR_DECL)
13628 else if (TREE_CODE (fld) == TEMPLATE_DECL)
13630 else if (TREE_CODE (fld) == USING_DECL)
13632 else
13634 error ("Wrong tree in TYPE_FIELDS list");
13635 debug_tree (fld);
13636 error_found = true;
13639 else if (TREE_CODE (t) == INTEGER_TYPE
13640 || TREE_CODE (t) == BOOLEAN_TYPE
13641 || TREE_CODE (t) == OFFSET_TYPE
13642 || TREE_CODE (t) == REFERENCE_TYPE
13643 || TREE_CODE (t) == NULLPTR_TYPE
13644 || TREE_CODE (t) == POINTER_TYPE)
13646 if (TYPE_CACHED_VALUES_P (t) != (TYPE_CACHED_VALUES (t) != NULL))
13648 error ("TYPE_CACHED_VALUES_P is %i while TYPE_CACHED_VALUES is %p",
13649 TYPE_CACHED_VALUES_P (t), (void *)TYPE_CACHED_VALUES (t));
13650 error_found = true;
13652 else if (TYPE_CACHED_VALUES_P (t) && TREE_CODE (TYPE_CACHED_VALUES (t)) != TREE_VEC)
13654 error ("TYPE_CACHED_VALUES is not TREE_VEC");
13655 debug_tree (TYPE_CACHED_VALUES (t));
13656 error_found = true;
13658 /* Verify just enough of cache to ensure that no one copied it to new type.
13659 All copying should go by copy_node that should clear it. */
13660 else if (TYPE_CACHED_VALUES_P (t))
13662 int i;
13663 for (i = 0; i < TREE_VEC_LENGTH (TYPE_CACHED_VALUES (t)); i++)
13664 if (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i)
13665 && TREE_TYPE (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i)) != t)
13667 error ("wrong TYPE_CACHED_VALUES entry");
13668 debug_tree (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i));
13669 error_found = true;
13670 break;
13674 else if (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE)
13675 for (tree l = TYPE_ARG_TYPES (t); l; l = TREE_CHAIN (l))
13677 /* C++ FE uses TREE_PURPOSE to store initial values. */
13678 if (TREE_PURPOSE (l) && in_lto_p)
13680 error ("TREE_PURPOSE is non-NULL in TYPE_ARG_TYPES list");
13681 debug_tree (l);
13682 error_found = true;
13684 if (!TYPE_P (TREE_VALUE (l)))
13686 error ("Wrong entry in TYPE_ARG_TYPES list");
13687 debug_tree (l);
13688 error_found = true;
13691 else if (!is_lang_specific (t) && TYPE_VALUES_RAW (t))
13693 error ("TYPE_VALUES_RAW field is non-NULL");
13694 debug_tree (TYPE_VALUES_RAW (t));
13695 error_found = true;
13697 if (TREE_CODE (t) != INTEGER_TYPE
13698 && TREE_CODE (t) != BOOLEAN_TYPE
13699 && TREE_CODE (t) != OFFSET_TYPE
13700 && TREE_CODE (t) != REFERENCE_TYPE
13701 && TREE_CODE (t) != NULLPTR_TYPE
13702 && TREE_CODE (t) != POINTER_TYPE
13703 && TYPE_CACHED_VALUES_P (t))
13705 error ("TYPE_CACHED_VALUES_P is set while it should not");
13706 error_found = true;
13708 if (TYPE_STRING_FLAG (t)
13709 && TREE_CODE (t) != ARRAY_TYPE && TREE_CODE (t) != INTEGER_TYPE)
13711 error ("TYPE_STRING_FLAG is set on wrong type code");
13712 error_found = true;
13714 else if (TYPE_STRING_FLAG (t))
13716 const_tree b = t;
13717 if (TREE_CODE (b) == ARRAY_TYPE)
13718 b = TREE_TYPE (t);
13719 /* Java builds arrays with TYPE_STRING_FLAG of promoted_char_type
13720 that is 32bits. */
13721 if (TREE_CODE (b) != INTEGER_TYPE)
13723 error ("TYPE_STRING_FLAG is set on type that does not look like "
13724 "char nor array of chars");
13725 error_found = true;
13729 /* ipa-devirt makes an assumption that TYPE_METHOD_BASETYPE is always
13730 TYPE_MAIN_VARIANT and it would be odd to add methods only to variatns
13731 of a type. */
13732 if (TREE_CODE (t) == METHOD_TYPE
13733 && TYPE_MAIN_VARIANT (TYPE_METHOD_BASETYPE (t)) != TYPE_METHOD_BASETYPE (t))
13735 error ("TYPE_METHOD_BASETYPE is not main variant");
13736 error_found = true;
13739 if (error_found)
13741 debug_tree (const_cast <tree> (t));
13742 internal_error ("verify_type failed");
13747 /* Return true if ARG is marked with the nonnull attribute in the
13748 current function signature. */
13750 bool
13751 nonnull_arg_p (const_tree arg)
13753 tree t, attrs, fntype;
13754 unsigned HOST_WIDE_INT arg_num;
13756 gcc_assert (TREE_CODE (arg) == PARM_DECL && POINTER_TYPE_P (TREE_TYPE (arg)));
13758 /* The static chain decl is always non null. */
13759 if (arg == cfun->static_chain_decl)
13760 return true;
13762 /* THIS argument of method is always non-NULL. */
13763 if (TREE_CODE (TREE_TYPE (cfun->decl)) == METHOD_TYPE
13764 && arg == DECL_ARGUMENTS (cfun->decl)
13765 && flag_delete_null_pointer_checks)
13766 return true;
13768 /* Values passed by reference are always non-NULL. */
13769 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE
13770 && flag_delete_null_pointer_checks)
13771 return true;
13773 fntype = TREE_TYPE (cfun->decl);
13774 for (attrs = TYPE_ATTRIBUTES (fntype); attrs; attrs = TREE_CHAIN (attrs))
13776 attrs = lookup_attribute ("nonnull", attrs);
13778 /* If "nonnull" wasn't specified, we know nothing about the argument. */
13779 if (attrs == NULL_TREE)
13780 return false;
13782 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
13783 if (TREE_VALUE (attrs) == NULL_TREE)
13784 return true;
13786 /* Get the position number for ARG in the function signature. */
13787 for (arg_num = 1, t = DECL_ARGUMENTS (cfun->decl);
13789 t = DECL_CHAIN (t), arg_num++)
13791 if (t == arg)
13792 break;
13795 gcc_assert (t == arg);
13797 /* Now see if ARG_NUM is mentioned in the nonnull list. */
13798 for (t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
13800 if (compare_tree_int (TREE_VALUE (t), arg_num) == 0)
13801 return true;
13805 return false;
13809 #include "gt-tree.h"