Fix missing ChangeLog entry for Graphite head files fix.
[official-gcc.git] / gcc / tree.c
blob779fe9386a27b2b09b66c06e52eddc30b072b8a8
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 (IN_RANGE (hwi, 0, 1))
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 REF_REVERSE_STORAGE_ORDER (result) = REF_REVERSE_STORAGE_ORDER (ref);
4211 break;
4213 case ARRAY_REF:
4214 result = build_nt (ARRAY_REF,
4215 stabilize_reference (TREE_OPERAND (ref, 0)),
4216 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
4217 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
4218 break;
4220 case ARRAY_RANGE_REF:
4221 result = build_nt (ARRAY_RANGE_REF,
4222 stabilize_reference (TREE_OPERAND (ref, 0)),
4223 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
4224 TREE_OPERAND (ref, 2), TREE_OPERAND (ref, 3));
4225 break;
4227 case COMPOUND_EXPR:
4228 /* We cannot wrap the first expression in a SAVE_EXPR, as then
4229 it wouldn't be ignored. This matters when dealing with
4230 volatiles. */
4231 return stabilize_reference_1 (ref);
4233 /* If arg isn't a kind of lvalue we recognize, make no change.
4234 Caller should recognize the error for an invalid lvalue. */
4235 default:
4236 return ref;
4238 case ERROR_MARK:
4239 return error_mark_node;
4242 TREE_TYPE (result) = TREE_TYPE (ref);
4243 TREE_READONLY (result) = TREE_READONLY (ref);
4244 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
4245 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
4247 return result;
4250 /* Low-level constructors for expressions. */
4252 /* A helper function for build1 and constant folders. Set TREE_CONSTANT,
4253 and TREE_SIDE_EFFECTS for an ADDR_EXPR. */
4255 void
4256 recompute_tree_invariant_for_addr_expr (tree t)
4258 tree node;
4259 bool tc = true, se = false;
4261 gcc_assert (TREE_CODE (t) == ADDR_EXPR);
4263 /* We started out assuming this address is both invariant and constant, but
4264 does not have side effects. Now go down any handled components and see if
4265 any of them involve offsets that are either non-constant or non-invariant.
4266 Also check for side-effects.
4268 ??? Note that this code makes no attempt to deal with the case where
4269 taking the address of something causes a copy due to misalignment. */
4271 #define UPDATE_FLAGS(NODE) \
4272 do { tree _node = (NODE); \
4273 if (_node && !TREE_CONSTANT (_node)) tc = false; \
4274 if (_node && TREE_SIDE_EFFECTS (_node)) se = true; } while (0)
4276 for (node = TREE_OPERAND (t, 0); handled_component_p (node);
4277 node = TREE_OPERAND (node, 0))
4279 /* If the first operand doesn't have an ARRAY_TYPE, this is a bogus
4280 array reference (probably made temporarily by the G++ front end),
4281 so ignore all the operands. */
4282 if ((TREE_CODE (node) == ARRAY_REF
4283 || TREE_CODE (node) == ARRAY_RANGE_REF)
4284 && TREE_CODE (TREE_TYPE (TREE_OPERAND (node, 0))) == ARRAY_TYPE)
4286 UPDATE_FLAGS (TREE_OPERAND (node, 1));
4287 if (TREE_OPERAND (node, 2))
4288 UPDATE_FLAGS (TREE_OPERAND (node, 2));
4289 if (TREE_OPERAND (node, 3))
4290 UPDATE_FLAGS (TREE_OPERAND (node, 3));
4292 /* Likewise, just because this is a COMPONENT_REF doesn't mean we have a
4293 FIELD_DECL, apparently. The G++ front end can put something else
4294 there, at least temporarily. */
4295 else if (TREE_CODE (node) == COMPONENT_REF
4296 && TREE_CODE (TREE_OPERAND (node, 1)) == FIELD_DECL)
4298 if (TREE_OPERAND (node, 2))
4299 UPDATE_FLAGS (TREE_OPERAND (node, 2));
4303 node = lang_hooks.expr_to_decl (node, &tc, &se);
4305 /* Now see what's inside. If it's an INDIRECT_REF, copy our properties from
4306 the address, since &(*a)->b is a form of addition. If it's a constant, the
4307 address is constant too. If it's a decl, its address is constant if the
4308 decl is static. Everything else is not constant and, furthermore,
4309 taking the address of a volatile variable is not volatile. */
4310 if (TREE_CODE (node) == INDIRECT_REF
4311 || TREE_CODE (node) == MEM_REF)
4312 UPDATE_FLAGS (TREE_OPERAND (node, 0));
4313 else if (CONSTANT_CLASS_P (node))
4315 else if (DECL_P (node))
4316 tc &= (staticp (node) != NULL_TREE);
4317 else
4319 tc = false;
4320 se |= TREE_SIDE_EFFECTS (node);
4324 TREE_CONSTANT (t) = tc;
4325 TREE_SIDE_EFFECTS (t) = se;
4326 #undef UPDATE_FLAGS
4329 /* Build an expression of code CODE, data type TYPE, and operands as
4330 specified. Expressions and reference nodes can be created this way.
4331 Constants, decls, types and misc nodes cannot be.
4333 We define 5 non-variadic functions, from 0 to 4 arguments. This is
4334 enough for all extant tree codes. */
4336 tree
4337 build0_stat (enum tree_code code, tree tt MEM_STAT_DECL)
4339 tree t;
4341 gcc_assert (TREE_CODE_LENGTH (code) == 0);
4343 t = make_node_stat (code PASS_MEM_STAT);
4344 TREE_TYPE (t) = tt;
4346 return t;
4349 tree
4350 build1_stat (enum tree_code code, tree type, tree node MEM_STAT_DECL)
4352 int length = sizeof (struct tree_exp);
4353 tree t;
4355 record_node_allocation_statistics (code, length);
4357 gcc_assert (TREE_CODE_LENGTH (code) == 1);
4359 t = ggc_alloc_tree_node_stat (length PASS_MEM_STAT);
4361 memset (t, 0, sizeof (struct tree_common));
4363 TREE_SET_CODE (t, code);
4365 TREE_TYPE (t) = type;
4366 SET_EXPR_LOCATION (t, UNKNOWN_LOCATION);
4367 TREE_OPERAND (t, 0) = node;
4368 if (node && !TYPE_P (node))
4370 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
4371 TREE_READONLY (t) = TREE_READONLY (node);
4374 if (TREE_CODE_CLASS (code) == tcc_statement)
4375 TREE_SIDE_EFFECTS (t) = 1;
4376 else switch (code)
4378 case VA_ARG_EXPR:
4379 /* All of these have side-effects, no matter what their
4380 operands are. */
4381 TREE_SIDE_EFFECTS (t) = 1;
4382 TREE_READONLY (t) = 0;
4383 break;
4385 case INDIRECT_REF:
4386 /* Whether a dereference is readonly has nothing to do with whether
4387 its operand is readonly. */
4388 TREE_READONLY (t) = 0;
4389 break;
4391 case ADDR_EXPR:
4392 if (node)
4393 recompute_tree_invariant_for_addr_expr (t);
4394 break;
4396 default:
4397 if ((TREE_CODE_CLASS (code) == tcc_unary || code == VIEW_CONVERT_EXPR)
4398 && node && !TYPE_P (node)
4399 && TREE_CONSTANT (node))
4400 TREE_CONSTANT (t) = 1;
4401 if (TREE_CODE_CLASS (code) == tcc_reference
4402 && node && TREE_THIS_VOLATILE (node))
4403 TREE_THIS_VOLATILE (t) = 1;
4404 break;
4407 return t;
4410 #define PROCESS_ARG(N) \
4411 do { \
4412 TREE_OPERAND (t, N) = arg##N; \
4413 if (arg##N &&!TYPE_P (arg##N)) \
4415 if (TREE_SIDE_EFFECTS (arg##N)) \
4416 side_effects = 1; \
4417 if (!TREE_READONLY (arg##N) \
4418 && !CONSTANT_CLASS_P (arg##N)) \
4419 (void) (read_only = 0); \
4420 if (!TREE_CONSTANT (arg##N)) \
4421 (void) (constant = 0); \
4423 } while (0)
4425 tree
4426 build2_stat (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
4428 bool constant, read_only, side_effects;
4429 tree t;
4431 gcc_assert (TREE_CODE_LENGTH (code) == 2);
4433 if ((code == MINUS_EXPR || code == PLUS_EXPR || code == MULT_EXPR)
4434 && arg0 && arg1 && tt && POINTER_TYPE_P (tt)
4435 /* When sizetype precision doesn't match that of pointers
4436 we need to be able to build explicit extensions or truncations
4437 of the offset argument. */
4438 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (tt))
4439 gcc_assert (TREE_CODE (arg0) == INTEGER_CST
4440 && TREE_CODE (arg1) == INTEGER_CST);
4442 if (code == POINTER_PLUS_EXPR && arg0 && arg1 && tt)
4443 gcc_assert (POINTER_TYPE_P (tt) && POINTER_TYPE_P (TREE_TYPE (arg0))
4444 && ptrofftype_p (TREE_TYPE (arg1)));
4446 t = make_node_stat (code PASS_MEM_STAT);
4447 TREE_TYPE (t) = tt;
4449 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
4450 result based on those same flags for the arguments. But if the
4451 arguments aren't really even `tree' expressions, we shouldn't be trying
4452 to do this. */
4454 /* Expressions without side effects may be constant if their
4455 arguments are as well. */
4456 constant = (TREE_CODE_CLASS (code) == tcc_comparison
4457 || TREE_CODE_CLASS (code) == tcc_binary);
4458 read_only = 1;
4459 side_effects = TREE_SIDE_EFFECTS (t);
4461 PROCESS_ARG (0);
4462 PROCESS_ARG (1);
4464 TREE_SIDE_EFFECTS (t) = side_effects;
4465 if (code == MEM_REF)
4467 if (arg0 && TREE_CODE (arg0) == ADDR_EXPR)
4469 tree o = TREE_OPERAND (arg0, 0);
4470 TREE_READONLY (t) = TREE_READONLY (o);
4471 TREE_THIS_VOLATILE (t) = TREE_THIS_VOLATILE (o);
4474 else
4476 TREE_READONLY (t) = read_only;
4477 TREE_CONSTANT (t) = constant;
4478 TREE_THIS_VOLATILE (t)
4479 = (TREE_CODE_CLASS (code) == tcc_reference
4480 && arg0 && TREE_THIS_VOLATILE (arg0));
4483 return t;
4487 tree
4488 build3_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4489 tree arg2 MEM_STAT_DECL)
4491 bool constant, read_only, side_effects;
4492 tree t;
4494 gcc_assert (TREE_CODE_LENGTH (code) == 3);
4495 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4497 t = make_node_stat (code PASS_MEM_STAT);
4498 TREE_TYPE (t) = tt;
4500 read_only = 1;
4502 /* As a special exception, if COND_EXPR has NULL branches, we
4503 assume that it is a gimple statement and always consider
4504 it to have side effects. */
4505 if (code == COND_EXPR
4506 && tt == void_type_node
4507 && arg1 == NULL_TREE
4508 && arg2 == NULL_TREE)
4509 side_effects = true;
4510 else
4511 side_effects = TREE_SIDE_EFFECTS (t);
4513 PROCESS_ARG (0);
4514 PROCESS_ARG (1);
4515 PROCESS_ARG (2);
4517 if (code == COND_EXPR)
4518 TREE_READONLY (t) = read_only;
4520 TREE_SIDE_EFFECTS (t) = side_effects;
4521 TREE_THIS_VOLATILE (t)
4522 = (TREE_CODE_CLASS (code) == tcc_reference
4523 && arg0 && TREE_THIS_VOLATILE (arg0));
4525 return t;
4528 tree
4529 build4_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4530 tree arg2, tree arg3 MEM_STAT_DECL)
4532 bool constant, read_only, side_effects;
4533 tree t;
4535 gcc_assert (TREE_CODE_LENGTH (code) == 4);
4537 t = make_node_stat (code PASS_MEM_STAT);
4538 TREE_TYPE (t) = tt;
4540 side_effects = TREE_SIDE_EFFECTS (t);
4542 PROCESS_ARG (0);
4543 PROCESS_ARG (1);
4544 PROCESS_ARG (2);
4545 PROCESS_ARG (3);
4547 TREE_SIDE_EFFECTS (t) = side_effects;
4548 TREE_THIS_VOLATILE (t)
4549 = (TREE_CODE_CLASS (code) == tcc_reference
4550 && arg0 && TREE_THIS_VOLATILE (arg0));
4552 return t;
4555 tree
4556 build5_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
4557 tree arg2, tree arg3, tree arg4 MEM_STAT_DECL)
4559 bool constant, read_only, side_effects;
4560 tree t;
4562 gcc_assert (TREE_CODE_LENGTH (code) == 5);
4564 t = make_node_stat (code PASS_MEM_STAT);
4565 TREE_TYPE (t) = tt;
4567 side_effects = TREE_SIDE_EFFECTS (t);
4569 PROCESS_ARG (0);
4570 PROCESS_ARG (1);
4571 PROCESS_ARG (2);
4572 PROCESS_ARG (3);
4573 PROCESS_ARG (4);
4575 TREE_SIDE_EFFECTS (t) = side_effects;
4576 if (code == TARGET_MEM_REF)
4578 if (arg0 && TREE_CODE (arg0) == ADDR_EXPR)
4580 tree o = TREE_OPERAND (arg0, 0);
4581 TREE_READONLY (t) = TREE_READONLY (o);
4582 TREE_THIS_VOLATILE (t) = TREE_THIS_VOLATILE (o);
4585 else
4586 TREE_THIS_VOLATILE (t)
4587 = (TREE_CODE_CLASS (code) == tcc_reference
4588 && arg0 && TREE_THIS_VOLATILE (arg0));
4590 return t;
4593 /* Build a simple MEM_REF tree with the sematics of a plain INDIRECT_REF
4594 on the pointer PTR. */
4596 tree
4597 build_simple_mem_ref_loc (location_t loc, tree ptr)
4599 HOST_WIDE_INT offset = 0;
4600 tree ptype = TREE_TYPE (ptr);
4601 tree tem;
4602 /* For convenience allow addresses that collapse to a simple base
4603 and offset. */
4604 if (TREE_CODE (ptr) == ADDR_EXPR
4605 && (handled_component_p (TREE_OPERAND (ptr, 0))
4606 || TREE_CODE (TREE_OPERAND (ptr, 0)) == MEM_REF))
4608 ptr = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &offset);
4609 gcc_assert (ptr);
4610 ptr = build_fold_addr_expr (ptr);
4611 gcc_assert (is_gimple_reg (ptr) || is_gimple_min_invariant (ptr));
4613 tem = build2 (MEM_REF, TREE_TYPE (ptype),
4614 ptr, build_int_cst (ptype, offset));
4615 SET_EXPR_LOCATION (tem, loc);
4616 return tem;
4619 /* Return the constant offset of a MEM_REF or TARGET_MEM_REF tree T. */
4621 offset_int
4622 mem_ref_offset (const_tree t)
4624 return offset_int::from (TREE_OPERAND (t, 1), SIGNED);
4627 /* Return an invariant ADDR_EXPR of type TYPE taking the address of BASE
4628 offsetted by OFFSET units. */
4630 tree
4631 build_invariant_address (tree type, tree base, HOST_WIDE_INT offset)
4633 tree ref = fold_build2 (MEM_REF, TREE_TYPE (type),
4634 build_fold_addr_expr (base),
4635 build_int_cst (ptr_type_node, offset));
4636 tree addr = build1 (ADDR_EXPR, type, ref);
4637 recompute_tree_invariant_for_addr_expr (addr);
4638 return addr;
4641 /* Similar except don't specify the TREE_TYPE
4642 and leave the TREE_SIDE_EFFECTS as 0.
4643 It is permissible for arguments to be null,
4644 or even garbage if their values do not matter. */
4646 tree
4647 build_nt (enum tree_code code, ...)
4649 tree t;
4650 int length;
4651 int i;
4652 va_list p;
4654 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4656 va_start (p, code);
4658 t = make_node (code);
4659 length = TREE_CODE_LENGTH (code);
4661 for (i = 0; i < length; i++)
4662 TREE_OPERAND (t, i) = va_arg (p, tree);
4664 va_end (p);
4665 return t;
4668 /* Similar to build_nt, but for creating a CALL_EXPR object with a
4669 tree vec. */
4671 tree
4672 build_nt_call_vec (tree fn, vec<tree, va_gc> *args)
4674 tree ret, t;
4675 unsigned int ix;
4677 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
4678 CALL_EXPR_FN (ret) = fn;
4679 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
4680 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
4681 CALL_EXPR_ARG (ret, ix) = t;
4682 return ret;
4685 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
4686 We do NOT enter this node in any sort of symbol table.
4688 LOC is the location of the decl.
4690 layout_decl is used to set up the decl's storage layout.
4691 Other slots are initialized to 0 or null pointers. */
4693 tree
4694 build_decl_stat (location_t loc, enum tree_code code, tree name,
4695 tree type MEM_STAT_DECL)
4697 tree t;
4699 t = make_node_stat (code PASS_MEM_STAT);
4700 DECL_SOURCE_LOCATION (t) = loc;
4702 /* if (type == error_mark_node)
4703 type = integer_type_node; */
4704 /* That is not done, deliberately, so that having error_mark_node
4705 as the type can suppress useless errors in the use of this variable. */
4707 DECL_NAME (t) = name;
4708 TREE_TYPE (t) = type;
4710 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
4711 layout_decl (t, 0);
4713 return t;
4716 /* Builds and returns function declaration with NAME and TYPE. */
4718 tree
4719 build_fn_decl (const char *name, tree type)
4721 tree id = get_identifier (name);
4722 tree decl = build_decl (input_location, FUNCTION_DECL, id, type);
4724 DECL_EXTERNAL (decl) = 1;
4725 TREE_PUBLIC (decl) = 1;
4726 DECL_ARTIFICIAL (decl) = 1;
4727 TREE_NOTHROW (decl) = 1;
4729 return decl;
4732 vec<tree, va_gc> *all_translation_units;
4734 /* Builds a new translation-unit decl with name NAME, queues it in the
4735 global list of translation-unit decls and returns it. */
4737 tree
4738 build_translation_unit_decl (tree name)
4740 tree tu = build_decl (UNKNOWN_LOCATION, TRANSLATION_UNIT_DECL,
4741 name, NULL_TREE);
4742 TRANSLATION_UNIT_LANGUAGE (tu) = lang_hooks.name;
4743 vec_safe_push (all_translation_units, tu);
4744 return tu;
4748 /* BLOCK nodes are used to represent the structure of binding contours
4749 and declarations, once those contours have been exited and their contents
4750 compiled. This information is used for outputting debugging info. */
4752 tree
4753 build_block (tree vars, tree subblocks, tree supercontext, tree chain)
4755 tree block = make_node (BLOCK);
4757 BLOCK_VARS (block) = vars;
4758 BLOCK_SUBBLOCKS (block) = subblocks;
4759 BLOCK_SUPERCONTEXT (block) = supercontext;
4760 BLOCK_CHAIN (block) = chain;
4761 return block;
4765 /* Like SET_EXPR_LOCATION, but make sure the tree can have a location.
4767 LOC is the location to use in tree T. */
4769 void
4770 protected_set_expr_location (tree t, location_t loc)
4772 if (CAN_HAVE_LOCATION_P (t))
4773 SET_EXPR_LOCATION (t, loc);
4776 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
4777 is ATTRIBUTE. */
4779 tree
4780 build_decl_attribute_variant (tree ddecl, tree attribute)
4782 DECL_ATTRIBUTES (ddecl) = attribute;
4783 return ddecl;
4786 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
4787 is ATTRIBUTE and its qualifiers are QUALS.
4789 Record such modified types already made so we don't make duplicates. */
4791 tree
4792 build_type_attribute_qual_variant (tree ttype, tree attribute, int quals)
4794 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
4796 inchash::hash hstate;
4797 tree ntype;
4798 int i;
4799 tree t;
4800 enum tree_code code = TREE_CODE (ttype);
4802 /* Building a distinct copy of a tagged type is inappropriate; it
4803 causes breakage in code that expects there to be a one-to-one
4804 relationship between a struct and its fields.
4805 build_duplicate_type is another solution (as used in
4806 handle_transparent_union_attribute), but that doesn't play well
4807 with the stronger C++ type identity model. */
4808 if (TREE_CODE (ttype) == RECORD_TYPE
4809 || TREE_CODE (ttype) == UNION_TYPE
4810 || TREE_CODE (ttype) == QUAL_UNION_TYPE
4811 || TREE_CODE (ttype) == ENUMERAL_TYPE)
4813 warning (OPT_Wattributes,
4814 "ignoring attributes applied to %qT after definition",
4815 TYPE_MAIN_VARIANT (ttype));
4816 return build_qualified_type (ttype, quals);
4819 ttype = build_qualified_type (ttype, TYPE_UNQUALIFIED);
4820 ntype = build_distinct_type_copy (ttype);
4822 TYPE_ATTRIBUTES (ntype) = attribute;
4824 hstate.add_int (code);
4825 if (TREE_TYPE (ntype))
4826 hstate.add_object (TYPE_HASH (TREE_TYPE (ntype)));
4827 attribute_hash_list (attribute, hstate);
4829 switch (TREE_CODE (ntype))
4831 case FUNCTION_TYPE:
4832 type_hash_list (TYPE_ARG_TYPES (ntype), hstate);
4833 break;
4834 case ARRAY_TYPE:
4835 if (TYPE_DOMAIN (ntype))
4836 hstate.add_object (TYPE_HASH (TYPE_DOMAIN (ntype)));
4837 break;
4838 case INTEGER_TYPE:
4839 t = TYPE_MAX_VALUE (ntype);
4840 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
4841 hstate.add_object (TREE_INT_CST_ELT (t, i));
4842 break;
4843 case REAL_TYPE:
4844 case FIXED_POINT_TYPE:
4846 unsigned int precision = TYPE_PRECISION (ntype);
4847 hstate.add_object (precision);
4849 break;
4850 default:
4851 break;
4854 ntype = type_hash_canon (hstate.end(), ntype);
4856 /* If the target-dependent attributes make NTYPE different from
4857 its canonical type, we will need to use structural equality
4858 checks for this type. */
4859 if (TYPE_STRUCTURAL_EQUALITY_P (ttype)
4860 || !comp_type_attributes (ntype, ttype))
4861 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
4862 else if (TYPE_CANONICAL (ntype) == ntype)
4863 TYPE_CANONICAL (ntype) = TYPE_CANONICAL (ttype);
4865 ttype = build_qualified_type (ntype, quals);
4867 else if (TYPE_QUALS (ttype) != quals)
4868 ttype = build_qualified_type (ttype, quals);
4870 return ttype;
4873 /* Check if "omp declare simd" attribute arguments, CLAUSES1 and CLAUSES2, are
4874 the same. */
4876 static bool
4877 omp_declare_simd_clauses_equal (tree clauses1, tree clauses2)
4879 tree cl1, cl2;
4880 for (cl1 = clauses1, cl2 = clauses2;
4881 cl1 && cl2;
4882 cl1 = OMP_CLAUSE_CHAIN (cl1), cl2 = OMP_CLAUSE_CHAIN (cl2))
4884 if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_CODE (cl2))
4885 return false;
4886 if (OMP_CLAUSE_CODE (cl1) != OMP_CLAUSE_SIMDLEN)
4888 if (simple_cst_equal (OMP_CLAUSE_DECL (cl1),
4889 OMP_CLAUSE_DECL (cl2)) != 1)
4890 return false;
4892 switch (OMP_CLAUSE_CODE (cl1))
4894 case OMP_CLAUSE_ALIGNED:
4895 if (simple_cst_equal (OMP_CLAUSE_ALIGNED_ALIGNMENT (cl1),
4896 OMP_CLAUSE_ALIGNED_ALIGNMENT (cl2)) != 1)
4897 return false;
4898 break;
4899 case OMP_CLAUSE_LINEAR:
4900 if (simple_cst_equal (OMP_CLAUSE_LINEAR_STEP (cl1),
4901 OMP_CLAUSE_LINEAR_STEP (cl2)) != 1)
4902 return false;
4903 break;
4904 case OMP_CLAUSE_SIMDLEN:
4905 if (simple_cst_equal (OMP_CLAUSE_SIMDLEN_EXPR (cl1),
4906 OMP_CLAUSE_SIMDLEN_EXPR (cl2)) != 1)
4907 return false;
4908 default:
4909 break;
4912 return true;
4915 /* Compare two constructor-element-type constants. Return 1 if the lists
4916 are known to be equal; otherwise return 0. */
4918 static bool
4919 simple_cst_list_equal (const_tree l1, const_tree l2)
4921 while (l1 != NULL_TREE && l2 != NULL_TREE)
4923 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
4924 return false;
4926 l1 = TREE_CHAIN (l1);
4927 l2 = TREE_CHAIN (l2);
4930 return l1 == l2;
4933 /* Compare two identifier nodes representing attributes. Either one may
4934 be in wrapped __ATTR__ form. Return true if they are the same, false
4935 otherwise. */
4937 static bool
4938 cmp_attrib_identifiers (const_tree attr1, const_tree attr2)
4940 /* Make sure we're dealing with IDENTIFIER_NODEs. */
4941 gcc_checking_assert (TREE_CODE (attr1) == IDENTIFIER_NODE
4942 && TREE_CODE (attr2) == IDENTIFIER_NODE);
4944 /* Identifiers can be compared directly for equality. */
4945 if (attr1 == attr2)
4946 return true;
4948 /* If they are not equal, they may still be one in the form
4949 'text' while the other one is in the form '__text__'. TODO:
4950 If we were storing attributes in normalized 'text' form, then
4951 this could all go away and we could take full advantage of
4952 the fact that we're comparing identifiers. :-) */
4953 const size_t attr1_len = IDENTIFIER_LENGTH (attr1);
4954 const size_t attr2_len = IDENTIFIER_LENGTH (attr2);
4956 if (attr2_len == attr1_len + 4)
4958 const char *p = IDENTIFIER_POINTER (attr2);
4959 const char *q = IDENTIFIER_POINTER (attr1);
4960 if (p[0] == '_' && p[1] == '_'
4961 && p[attr2_len - 2] == '_' && p[attr2_len - 1] == '_'
4962 && strncmp (q, p + 2, attr1_len) == 0)
4963 return true;;
4965 else if (attr2_len + 4 == attr1_len)
4967 const char *p = IDENTIFIER_POINTER (attr2);
4968 const char *q = IDENTIFIER_POINTER (attr1);
4969 if (q[0] == '_' && q[1] == '_'
4970 && q[attr1_len - 2] == '_' && q[attr1_len - 1] == '_'
4971 && strncmp (q + 2, p, attr2_len) == 0)
4972 return true;
4975 return false;
4978 /* Compare two attributes for their value identity. Return true if the
4979 attribute values are known to be equal; otherwise return false. */
4981 bool
4982 attribute_value_equal (const_tree attr1, const_tree attr2)
4984 if (TREE_VALUE (attr1) == TREE_VALUE (attr2))
4985 return true;
4987 if (TREE_VALUE (attr1) != NULL_TREE
4988 && TREE_CODE (TREE_VALUE (attr1)) == TREE_LIST
4989 && TREE_VALUE (attr2) != NULL_TREE
4990 && TREE_CODE (TREE_VALUE (attr2)) == TREE_LIST)
4992 /* Handle attribute format. */
4993 if (is_attribute_p ("format", TREE_PURPOSE (attr1)))
4995 attr1 = TREE_VALUE (attr1);
4996 attr2 = TREE_VALUE (attr2);
4997 /* Compare the archetypes (printf/scanf/strftime/...). */
4998 if (!cmp_attrib_identifiers (TREE_VALUE (attr1),
4999 TREE_VALUE (attr2)))
5000 return false;
5001 /* Archetypes are the same. Compare the rest. */
5002 return (simple_cst_list_equal (TREE_CHAIN (attr1),
5003 TREE_CHAIN (attr2)) == 1);
5005 return (simple_cst_list_equal (TREE_VALUE (attr1),
5006 TREE_VALUE (attr2)) == 1);
5009 if ((flag_openmp || flag_openmp_simd)
5010 && TREE_VALUE (attr1) && TREE_VALUE (attr2)
5011 && TREE_CODE (TREE_VALUE (attr1)) == OMP_CLAUSE
5012 && TREE_CODE (TREE_VALUE (attr2)) == OMP_CLAUSE)
5013 return omp_declare_simd_clauses_equal (TREE_VALUE (attr1),
5014 TREE_VALUE (attr2));
5016 return (simple_cst_equal (TREE_VALUE (attr1), TREE_VALUE (attr2)) == 1);
5019 /* Return 0 if the attributes for two types are incompatible, 1 if they
5020 are compatible, and 2 if they are nearly compatible (which causes a
5021 warning to be generated). */
5023 comp_type_attributes (const_tree type1, const_tree type2)
5025 const_tree a1 = TYPE_ATTRIBUTES (type1);
5026 const_tree a2 = TYPE_ATTRIBUTES (type2);
5027 const_tree a;
5029 if (a1 == a2)
5030 return 1;
5031 for (a = a1; a != NULL_TREE; a = TREE_CHAIN (a))
5033 const struct attribute_spec *as;
5034 const_tree attr;
5036 as = lookup_attribute_spec (get_attribute_name (a));
5037 if (!as || as->affects_type_identity == false)
5038 continue;
5040 attr = lookup_attribute (as->name, CONST_CAST_TREE (a2));
5041 if (!attr || !attribute_value_equal (a, attr))
5042 break;
5044 if (!a)
5046 for (a = a2; a != NULL_TREE; a = TREE_CHAIN (a))
5048 const struct attribute_spec *as;
5050 as = lookup_attribute_spec (get_attribute_name (a));
5051 if (!as || as->affects_type_identity == false)
5052 continue;
5054 if (!lookup_attribute (as->name, CONST_CAST_TREE (a1)))
5055 break;
5056 /* We don't need to compare trees again, as we did this
5057 already in first loop. */
5059 /* All types - affecting identity - are equal, so
5060 there is no need to call target hook for comparison. */
5061 if (!a)
5062 return 1;
5064 if (lookup_attribute ("transaction_safe", CONST_CAST_TREE (a)))
5065 return 0;
5066 /* As some type combinations - like default calling-convention - might
5067 be compatible, we have to call the target hook to get the final result. */
5068 return targetm.comp_type_attributes (type1, type2);
5071 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
5072 is ATTRIBUTE.
5074 Record such modified types already made so we don't make duplicates. */
5076 tree
5077 build_type_attribute_variant (tree ttype, tree attribute)
5079 return build_type_attribute_qual_variant (ttype, attribute,
5080 TYPE_QUALS (ttype));
5084 /* Reset the expression *EXPR_P, a size or position.
5086 ??? We could reset all non-constant sizes or positions. But it's cheap
5087 enough to not do so and refrain from adding workarounds to dwarf2out.c.
5089 We need to reset self-referential sizes or positions because they cannot
5090 be gimplified and thus can contain a CALL_EXPR after the gimplification
5091 is finished, which will run afoul of LTO streaming. And they need to be
5092 reset to something essentially dummy but not constant, so as to preserve
5093 the properties of the object they are attached to. */
5095 static inline void
5096 free_lang_data_in_one_sizepos (tree *expr_p)
5098 tree expr = *expr_p;
5099 if (CONTAINS_PLACEHOLDER_P (expr))
5100 *expr_p = build0 (PLACEHOLDER_EXPR, TREE_TYPE (expr));
5104 /* Reset all the fields in a binfo node BINFO. We only keep
5105 BINFO_VTABLE, which is used by gimple_fold_obj_type_ref. */
5107 static void
5108 free_lang_data_in_binfo (tree binfo)
5110 unsigned i;
5111 tree t;
5113 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
5115 BINFO_VIRTUALS (binfo) = NULL_TREE;
5116 BINFO_BASE_ACCESSES (binfo) = NULL;
5117 BINFO_INHERITANCE_CHAIN (binfo) = NULL_TREE;
5118 BINFO_SUBVTT_INDEX (binfo) = NULL_TREE;
5120 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (binfo), i, t)
5121 free_lang_data_in_binfo (t);
5125 /* Reset all language specific information still present in TYPE. */
5127 static void
5128 free_lang_data_in_type (tree type)
5130 gcc_assert (TYPE_P (type));
5132 /* Give the FE a chance to remove its own data first. */
5133 lang_hooks.free_lang_data (type);
5135 TREE_LANG_FLAG_0 (type) = 0;
5136 TREE_LANG_FLAG_1 (type) = 0;
5137 TREE_LANG_FLAG_2 (type) = 0;
5138 TREE_LANG_FLAG_3 (type) = 0;
5139 TREE_LANG_FLAG_4 (type) = 0;
5140 TREE_LANG_FLAG_5 (type) = 0;
5141 TREE_LANG_FLAG_6 (type) = 0;
5143 if (TREE_CODE (type) == FUNCTION_TYPE)
5145 /* Remove the const and volatile qualifiers from arguments. The
5146 C++ front end removes them, but the C front end does not,
5147 leading to false ODR violation errors when merging two
5148 instances of the same function signature compiled by
5149 different front ends. */
5150 tree p;
5152 for (p = TYPE_ARG_TYPES (type); p; p = TREE_CHAIN (p))
5154 tree arg_type = TREE_VALUE (p);
5156 if (TYPE_READONLY (arg_type) || TYPE_VOLATILE (arg_type))
5158 int quals = TYPE_QUALS (arg_type)
5159 & ~TYPE_QUAL_CONST
5160 & ~TYPE_QUAL_VOLATILE;
5161 TREE_VALUE (p) = build_qualified_type (arg_type, quals);
5162 free_lang_data_in_type (TREE_VALUE (p));
5164 /* C++ FE uses TREE_PURPOSE to store initial values. */
5165 TREE_PURPOSE (p) = NULL;
5167 /* Java uses TYPE_MINVAL for TYPE_ARGUMENT_SIGNATURE. */
5168 TYPE_MINVAL (type) = NULL;
5170 if (TREE_CODE (type) == METHOD_TYPE)
5172 tree p;
5174 for (p = TYPE_ARG_TYPES (type); p; p = TREE_CHAIN (p))
5176 /* C++ FE uses TREE_PURPOSE to store initial values. */
5177 TREE_PURPOSE (p) = NULL;
5179 /* Java uses TYPE_MINVAL for TYPE_ARGUMENT_SIGNATURE. */
5180 TYPE_MINVAL (type) = NULL;
5183 /* Remove members that are not actually FIELD_DECLs from the field
5184 list of an aggregate. These occur in C++. */
5185 if (RECORD_OR_UNION_TYPE_P (type))
5187 tree prev, member;
5189 /* Note that TYPE_FIELDS can be shared across distinct
5190 TREE_TYPEs. Therefore, if the first field of TYPE_FIELDS is
5191 to be removed, we cannot set its TREE_CHAIN to NULL.
5192 Otherwise, we would not be able to find all the other fields
5193 in the other instances of this TREE_TYPE.
5195 This was causing an ICE in testsuite/g++.dg/lto/20080915.C. */
5196 prev = NULL_TREE;
5197 member = TYPE_FIELDS (type);
5198 while (member)
5200 if (TREE_CODE (member) == FIELD_DECL
5201 || TREE_CODE (member) == TYPE_DECL)
5203 if (prev)
5204 TREE_CHAIN (prev) = member;
5205 else
5206 TYPE_FIELDS (type) = member;
5207 prev = member;
5210 member = TREE_CHAIN (member);
5213 if (prev)
5214 TREE_CHAIN (prev) = NULL_TREE;
5215 else
5216 TYPE_FIELDS (type) = NULL_TREE;
5218 /* FIXME: C FE uses TYPE_VFIELD to record C_TYPE_INCOMPLETE_VARS
5219 and danagle the pointer from time to time. */
5220 if (TYPE_VFIELD (type) && TREE_CODE (TYPE_VFIELD (type)) != FIELD_DECL)
5221 TYPE_VFIELD (type) = NULL_TREE;
5223 /* Remove TYPE_METHODS list. While it would be nice to keep it
5224 to enable ODR warnings about different method lists, doing so
5225 seems to impractically increase size of LTO data streamed.
5226 Keep the infrmation if TYPE_METHODS was non-NULL. This is used
5227 by function.c and pretty printers. */
5228 if (TYPE_METHODS (type))
5229 TYPE_METHODS (type) = error_mark_node;
5230 if (TYPE_BINFO (type))
5232 free_lang_data_in_binfo (TYPE_BINFO (type));
5233 /* We need to preserve link to bases and virtual table for all
5234 polymorphic types to make devirtualization machinery working.
5235 Debug output cares only about bases, but output also
5236 virtual table pointers so merging of -fdevirtualize and
5237 -fno-devirtualize units is easier. */
5238 if ((!BINFO_VTABLE (TYPE_BINFO (type))
5239 || !flag_devirtualize)
5240 && ((!BINFO_N_BASE_BINFOS (TYPE_BINFO (type))
5241 && !BINFO_VTABLE (TYPE_BINFO (type)))
5242 || debug_info_level != DINFO_LEVEL_NONE))
5243 TYPE_BINFO (type) = NULL;
5246 else
5248 /* For non-aggregate types, clear out the language slot (which
5249 overloads TYPE_BINFO). */
5250 TYPE_LANG_SLOT_1 (type) = NULL_TREE;
5252 if (INTEGRAL_TYPE_P (type)
5253 || SCALAR_FLOAT_TYPE_P (type)
5254 || FIXED_POINT_TYPE_P (type))
5256 free_lang_data_in_one_sizepos (&TYPE_MIN_VALUE (type));
5257 free_lang_data_in_one_sizepos (&TYPE_MAX_VALUE (type));
5261 free_lang_data_in_one_sizepos (&TYPE_SIZE (type));
5262 free_lang_data_in_one_sizepos (&TYPE_SIZE_UNIT (type));
5264 if (TYPE_CONTEXT (type)
5265 && TREE_CODE (TYPE_CONTEXT (type)) == BLOCK)
5267 tree ctx = TYPE_CONTEXT (type);
5270 ctx = BLOCK_SUPERCONTEXT (ctx);
5272 while (ctx && TREE_CODE (ctx) == BLOCK);
5273 TYPE_CONTEXT (type) = ctx;
5278 /* Return true if DECL may need an assembler name to be set. */
5280 static inline bool
5281 need_assembler_name_p (tree decl)
5283 /* We use DECL_ASSEMBLER_NAME to hold mangled type names for One Definition
5284 Rule merging. This makes type_odr_p to return true on those types during
5285 LTO and by comparing the mangled name, we can say what types are intended
5286 to be equivalent across compilation unit.
5288 We do not store names of type_in_anonymous_namespace_p.
5290 Record, union and enumeration type have linkage that allows use
5291 to check type_in_anonymous_namespace_p. We do not mangle compound types
5292 that always can be compared structurally.
5294 Similarly for builtin types, we compare properties of their main variant.
5295 A special case are integer types where mangling do make differences
5296 between char/signed char/unsigned char etc. Storing name for these makes
5297 e.g. -fno-signed-char/-fsigned-char mismatches to be handled well.
5298 See cp/mangle.c:write_builtin_type for details. */
5300 if (flag_lto_odr_type_mering
5301 && TREE_CODE (decl) == TYPE_DECL
5302 && DECL_NAME (decl)
5303 && decl == TYPE_NAME (TREE_TYPE (decl))
5304 && !TYPE_ARTIFICIAL (TREE_TYPE (decl))
5305 && (type_with_linkage_p (TREE_TYPE (decl))
5306 || TREE_CODE (TREE_TYPE (decl)) == INTEGER_TYPE)
5307 && !variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
5308 return !DECL_ASSEMBLER_NAME_SET_P (decl);
5309 /* Only FUNCTION_DECLs and VAR_DECLs are considered. */
5310 if (TREE_CODE (decl) != FUNCTION_DECL
5311 && TREE_CODE (decl) != VAR_DECL)
5312 return false;
5314 /* If DECL already has its assembler name set, it does not need a
5315 new one. */
5316 if (!HAS_DECL_ASSEMBLER_NAME_P (decl)
5317 || DECL_ASSEMBLER_NAME_SET_P (decl))
5318 return false;
5320 /* Abstract decls do not need an assembler name. */
5321 if (DECL_ABSTRACT_P (decl))
5322 return false;
5324 /* For VAR_DECLs, only static, public and external symbols need an
5325 assembler name. */
5326 if (TREE_CODE (decl) == VAR_DECL
5327 && !TREE_STATIC (decl)
5328 && !TREE_PUBLIC (decl)
5329 && !DECL_EXTERNAL (decl))
5330 return false;
5332 if (TREE_CODE (decl) == FUNCTION_DECL)
5334 /* Do not set assembler name on builtins. Allow RTL expansion to
5335 decide whether to expand inline or via a regular call. */
5336 if (DECL_BUILT_IN (decl)
5337 && DECL_BUILT_IN_CLASS (decl) != BUILT_IN_FRONTEND)
5338 return false;
5340 /* Functions represented in the callgraph need an assembler name. */
5341 if (cgraph_node::get (decl) != NULL)
5342 return true;
5344 /* Unused and not public functions don't need an assembler name. */
5345 if (!TREE_USED (decl) && !TREE_PUBLIC (decl))
5346 return false;
5349 return true;
5353 /* Reset all language specific information still present in symbol
5354 DECL. */
5356 static void
5357 free_lang_data_in_decl (tree decl)
5359 gcc_assert (DECL_P (decl));
5361 /* Give the FE a chance to remove its own data first. */
5362 lang_hooks.free_lang_data (decl);
5364 TREE_LANG_FLAG_0 (decl) = 0;
5365 TREE_LANG_FLAG_1 (decl) = 0;
5366 TREE_LANG_FLAG_2 (decl) = 0;
5367 TREE_LANG_FLAG_3 (decl) = 0;
5368 TREE_LANG_FLAG_4 (decl) = 0;
5369 TREE_LANG_FLAG_5 (decl) = 0;
5370 TREE_LANG_FLAG_6 (decl) = 0;
5372 free_lang_data_in_one_sizepos (&DECL_SIZE (decl));
5373 free_lang_data_in_one_sizepos (&DECL_SIZE_UNIT (decl));
5374 if (TREE_CODE (decl) == FIELD_DECL)
5376 free_lang_data_in_one_sizepos (&DECL_FIELD_OFFSET (decl));
5377 if (TREE_CODE (DECL_CONTEXT (decl)) == QUAL_UNION_TYPE)
5378 DECL_QUALIFIER (decl) = NULL_TREE;
5381 if (TREE_CODE (decl) == FUNCTION_DECL)
5383 struct cgraph_node *node;
5384 if (!(node = cgraph_node::get (decl))
5385 || (!node->definition && !node->clones))
5387 if (node)
5388 node->release_body ();
5389 else
5391 release_function_body (decl);
5392 DECL_ARGUMENTS (decl) = NULL;
5393 DECL_RESULT (decl) = NULL;
5394 DECL_INITIAL (decl) = error_mark_node;
5397 if (gimple_has_body_p (decl))
5399 tree t;
5401 /* If DECL has a gimple body, then the context for its
5402 arguments must be DECL. Otherwise, it doesn't really
5403 matter, as we will not be emitting any code for DECL. In
5404 general, there may be other instances of DECL created by
5405 the front end and since PARM_DECLs are generally shared,
5406 their DECL_CONTEXT changes as the replicas of DECL are
5407 created. The only time where DECL_CONTEXT is important
5408 is for the FUNCTION_DECLs that have a gimple body (since
5409 the PARM_DECL will be used in the function's body). */
5410 for (t = DECL_ARGUMENTS (decl); t; t = TREE_CHAIN (t))
5411 DECL_CONTEXT (t) = decl;
5412 if (!DECL_FUNCTION_SPECIFIC_TARGET (decl))
5413 DECL_FUNCTION_SPECIFIC_TARGET (decl)
5414 = target_option_default_node;
5415 if (!DECL_FUNCTION_SPECIFIC_OPTIMIZATION (decl))
5416 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (decl)
5417 = optimization_default_node;
5420 /* DECL_SAVED_TREE holds the GENERIC representation for DECL.
5421 At this point, it is not needed anymore. */
5422 DECL_SAVED_TREE (decl) = NULL_TREE;
5424 /* Clear the abstract origin if it refers to a method. Otherwise
5425 dwarf2out.c will ICE as we clear TYPE_METHODS and thus the
5426 origin will not be output correctly. */
5427 if (DECL_ABSTRACT_ORIGIN (decl)
5428 && DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))
5429 && RECORD_OR_UNION_TYPE_P
5430 (DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (decl))))
5431 DECL_ABSTRACT_ORIGIN (decl) = NULL_TREE;
5433 /* Sometimes the C++ frontend doesn't manage to transform a temporary
5434 DECL_VINDEX referring to itself into a vtable slot number as it
5435 should. Happens with functions that are copied and then forgotten
5436 about. Just clear it, it won't matter anymore. */
5437 if (DECL_VINDEX (decl) && !tree_fits_shwi_p (DECL_VINDEX (decl)))
5438 DECL_VINDEX (decl) = NULL_TREE;
5440 else if (TREE_CODE (decl) == VAR_DECL)
5442 if ((DECL_EXTERNAL (decl)
5443 && (!TREE_STATIC (decl) || !TREE_READONLY (decl)))
5444 || (decl_function_context (decl) && !TREE_STATIC (decl)))
5445 DECL_INITIAL (decl) = NULL_TREE;
5447 else if (TREE_CODE (decl) == TYPE_DECL
5448 || TREE_CODE (decl) == FIELD_DECL)
5449 DECL_INITIAL (decl) = NULL_TREE;
5450 else if (TREE_CODE (decl) == TRANSLATION_UNIT_DECL
5451 && DECL_INITIAL (decl)
5452 && TREE_CODE (DECL_INITIAL (decl)) == BLOCK)
5454 /* Strip builtins from the translation-unit BLOCK. We still have targets
5455 without builtin_decl_explicit support and also builtins are shared
5456 nodes and thus we can't use TREE_CHAIN in multiple lists. */
5457 tree *nextp = &BLOCK_VARS (DECL_INITIAL (decl));
5458 while (*nextp)
5460 tree var = *nextp;
5461 if (TREE_CODE (var) == FUNCTION_DECL
5462 && DECL_BUILT_IN (var))
5463 *nextp = TREE_CHAIN (var);
5464 else
5465 nextp = &TREE_CHAIN (var);
5471 /* Data used when collecting DECLs and TYPEs for language data removal. */
5473 struct free_lang_data_d
5475 /* Worklist to avoid excessive recursion. */
5476 vec<tree> worklist;
5478 /* Set of traversed objects. Used to avoid duplicate visits. */
5479 hash_set<tree> *pset;
5481 /* Array of symbols to process with free_lang_data_in_decl. */
5482 vec<tree> decls;
5484 /* Array of types to process with free_lang_data_in_type. */
5485 vec<tree> types;
5489 /* Save all language fields needed to generate proper debug information
5490 for DECL. This saves most fields cleared out by free_lang_data_in_decl. */
5492 static void
5493 save_debug_info_for_decl (tree t)
5495 /*struct saved_debug_info_d *sdi;*/
5497 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && DECL_P (t));
5499 /* FIXME. Partial implementation for saving debug info removed. */
5503 /* Save all language fields needed to generate proper debug information
5504 for TYPE. This saves most fields cleared out by free_lang_data_in_type. */
5506 static void
5507 save_debug_info_for_type (tree t)
5509 /*struct saved_debug_info_d *sdi;*/
5511 gcc_assert (debug_info_level > DINFO_LEVEL_TERSE && t && TYPE_P (t));
5513 /* FIXME. Partial implementation for saving debug info removed. */
5517 /* Add type or decl T to one of the list of tree nodes that need their
5518 language data removed. The lists are held inside FLD. */
5520 static void
5521 add_tree_to_fld_list (tree t, struct free_lang_data_d *fld)
5523 if (DECL_P (t))
5525 fld->decls.safe_push (t);
5526 if (debug_info_level > DINFO_LEVEL_TERSE)
5527 save_debug_info_for_decl (t);
5529 else if (TYPE_P (t))
5531 fld->types.safe_push (t);
5532 if (debug_info_level > DINFO_LEVEL_TERSE)
5533 save_debug_info_for_type (t);
5535 else
5536 gcc_unreachable ();
5539 /* Push tree node T into FLD->WORKLIST. */
5541 static inline void
5542 fld_worklist_push (tree t, struct free_lang_data_d *fld)
5544 if (t && !is_lang_specific (t) && !fld->pset->contains (t))
5545 fld->worklist.safe_push ((t));
5549 /* Operand callback helper for free_lang_data_in_node. *TP is the
5550 subtree operand being considered. */
5552 static tree
5553 find_decls_types_r (tree *tp, int *ws, void *data)
5555 tree t = *tp;
5556 struct free_lang_data_d *fld = (struct free_lang_data_d *) data;
5558 if (TREE_CODE (t) == TREE_LIST)
5559 return NULL_TREE;
5561 /* Language specific nodes will be removed, so there is no need
5562 to gather anything under them. */
5563 if (is_lang_specific (t))
5565 *ws = 0;
5566 return NULL_TREE;
5569 if (DECL_P (t))
5571 /* Note that walk_tree does not traverse every possible field in
5572 decls, so we have to do our own traversals here. */
5573 add_tree_to_fld_list (t, fld);
5575 fld_worklist_push (DECL_NAME (t), fld);
5576 fld_worklist_push (DECL_CONTEXT (t), fld);
5577 fld_worklist_push (DECL_SIZE (t), fld);
5578 fld_worklist_push (DECL_SIZE_UNIT (t), fld);
5580 /* We are going to remove everything under DECL_INITIAL for
5581 TYPE_DECLs. No point walking them. */
5582 if (TREE_CODE (t) != TYPE_DECL)
5583 fld_worklist_push (DECL_INITIAL (t), fld);
5585 fld_worklist_push (DECL_ATTRIBUTES (t), fld);
5586 fld_worklist_push (DECL_ABSTRACT_ORIGIN (t), fld);
5588 if (TREE_CODE (t) == FUNCTION_DECL)
5590 fld_worklist_push (DECL_ARGUMENTS (t), fld);
5591 fld_worklist_push (DECL_RESULT (t), fld);
5593 else if (TREE_CODE (t) == TYPE_DECL)
5595 fld_worklist_push (DECL_ORIGINAL_TYPE (t), fld);
5597 else if (TREE_CODE (t) == FIELD_DECL)
5599 fld_worklist_push (DECL_FIELD_OFFSET (t), fld);
5600 fld_worklist_push (DECL_BIT_FIELD_TYPE (t), fld);
5601 fld_worklist_push (DECL_FIELD_BIT_OFFSET (t), fld);
5602 fld_worklist_push (DECL_FCONTEXT (t), fld);
5605 if ((TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == PARM_DECL)
5606 && DECL_HAS_VALUE_EXPR_P (t))
5607 fld_worklist_push (DECL_VALUE_EXPR (t), fld);
5609 if (TREE_CODE (t) != FIELD_DECL
5610 && TREE_CODE (t) != TYPE_DECL)
5611 fld_worklist_push (TREE_CHAIN (t), fld);
5612 *ws = 0;
5614 else if (TYPE_P (t))
5616 /* Note that walk_tree does not traverse every possible field in
5617 types, so we have to do our own traversals here. */
5618 add_tree_to_fld_list (t, fld);
5620 if (!RECORD_OR_UNION_TYPE_P (t))
5621 fld_worklist_push (TYPE_CACHED_VALUES (t), fld);
5622 fld_worklist_push (TYPE_SIZE (t), fld);
5623 fld_worklist_push (TYPE_SIZE_UNIT (t), fld);
5624 fld_worklist_push (TYPE_ATTRIBUTES (t), fld);
5625 fld_worklist_push (TYPE_POINTER_TO (t), fld);
5626 fld_worklist_push (TYPE_REFERENCE_TO (t), fld);
5627 fld_worklist_push (TYPE_NAME (t), fld);
5628 /* Do not walk TYPE_NEXT_PTR_TO or TYPE_NEXT_REF_TO. We do not stream
5629 them and thus do not and want not to reach unused pointer types
5630 this way. */
5631 if (!POINTER_TYPE_P (t))
5632 fld_worklist_push (TYPE_MINVAL (t), fld);
5633 if (!RECORD_OR_UNION_TYPE_P (t))
5634 fld_worklist_push (TYPE_MAXVAL (t), fld);
5635 fld_worklist_push (TYPE_MAIN_VARIANT (t), fld);
5636 /* Do not walk TYPE_NEXT_VARIANT. We do not stream it and thus
5637 do not and want not to reach unused variants this way. */
5638 if (TYPE_CONTEXT (t))
5640 tree ctx = TYPE_CONTEXT (t);
5641 /* We adjust BLOCK TYPE_CONTEXTs to the innermost non-BLOCK one.
5642 So push that instead. */
5643 while (ctx && TREE_CODE (ctx) == BLOCK)
5644 ctx = BLOCK_SUPERCONTEXT (ctx);
5645 fld_worklist_push (ctx, fld);
5647 /* Do not walk TYPE_CANONICAL. We do not stream it and thus do not
5648 and want not to reach unused types this way. */
5650 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t))
5652 unsigned i;
5653 tree tem;
5654 FOR_EACH_VEC_ELT (*BINFO_BASE_BINFOS (TYPE_BINFO (t)), i, tem)
5655 fld_worklist_push (TREE_TYPE (tem), fld);
5656 tem = BINFO_VIRTUALS (TYPE_BINFO (t));
5657 if (tem
5658 /* The Java FE overloads BINFO_VIRTUALS for its own purpose. */
5659 && TREE_CODE (tem) == TREE_LIST)
5662 fld_worklist_push (TREE_VALUE (tem), fld);
5663 tem = TREE_CHAIN (tem);
5665 while (tem);
5667 if (RECORD_OR_UNION_TYPE_P (t))
5669 tree tem;
5670 /* Push all TYPE_FIELDS - there can be interleaving interesting
5671 and non-interesting things. */
5672 tem = TYPE_FIELDS (t);
5673 while (tem)
5675 if (TREE_CODE (tem) == FIELD_DECL
5676 || TREE_CODE (tem) == TYPE_DECL)
5677 fld_worklist_push (tem, fld);
5678 tem = TREE_CHAIN (tem);
5682 fld_worklist_push (TYPE_STUB_DECL (t), fld);
5683 *ws = 0;
5685 else if (TREE_CODE (t) == BLOCK)
5687 tree tem;
5688 for (tem = BLOCK_VARS (t); tem; tem = TREE_CHAIN (tem))
5689 fld_worklist_push (tem, fld);
5690 for (tem = BLOCK_SUBBLOCKS (t); tem; tem = BLOCK_CHAIN (tem))
5691 fld_worklist_push (tem, fld);
5692 fld_worklist_push (BLOCK_ABSTRACT_ORIGIN (t), fld);
5695 if (TREE_CODE (t) != IDENTIFIER_NODE
5696 && CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED))
5697 fld_worklist_push (TREE_TYPE (t), fld);
5699 return NULL_TREE;
5703 /* Find decls and types in T. */
5705 static void
5706 find_decls_types (tree t, struct free_lang_data_d *fld)
5708 while (1)
5710 if (!fld->pset->contains (t))
5711 walk_tree (&t, find_decls_types_r, fld, fld->pset);
5712 if (fld->worklist.is_empty ())
5713 break;
5714 t = fld->worklist.pop ();
5718 /* Translate all the types in LIST with the corresponding runtime
5719 types. */
5721 static tree
5722 get_eh_types_for_runtime (tree list)
5724 tree head, prev;
5726 if (list == NULL_TREE)
5727 return NULL_TREE;
5729 head = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5730 prev = head;
5731 list = TREE_CHAIN (list);
5732 while (list)
5734 tree n = build_tree_list (0, lookup_type_for_runtime (TREE_VALUE (list)));
5735 TREE_CHAIN (prev) = n;
5736 prev = TREE_CHAIN (prev);
5737 list = TREE_CHAIN (list);
5740 return head;
5744 /* Find decls and types referenced in EH region R and store them in
5745 FLD->DECLS and FLD->TYPES. */
5747 static void
5748 find_decls_types_in_eh_region (eh_region r, struct free_lang_data_d *fld)
5750 switch (r->type)
5752 case ERT_CLEANUP:
5753 break;
5755 case ERT_TRY:
5757 eh_catch c;
5759 /* The types referenced in each catch must first be changed to the
5760 EH types used at runtime. This removes references to FE types
5761 in the region. */
5762 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
5764 c->type_list = get_eh_types_for_runtime (c->type_list);
5765 walk_tree (&c->type_list, find_decls_types_r, fld, fld->pset);
5768 break;
5770 case ERT_ALLOWED_EXCEPTIONS:
5771 r->u.allowed.type_list
5772 = get_eh_types_for_runtime (r->u.allowed.type_list);
5773 walk_tree (&r->u.allowed.type_list, find_decls_types_r, fld, fld->pset);
5774 break;
5776 case ERT_MUST_NOT_THROW:
5777 walk_tree (&r->u.must_not_throw.failure_decl,
5778 find_decls_types_r, fld, fld->pset);
5779 break;
5784 /* Find decls and types referenced in cgraph node N and store them in
5785 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5786 look for *every* kind of DECL and TYPE node reachable from N,
5787 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5788 NAMESPACE_DECLs, etc). */
5790 static void
5791 find_decls_types_in_node (struct cgraph_node *n, struct free_lang_data_d *fld)
5793 basic_block bb;
5794 struct function *fn;
5795 unsigned ix;
5796 tree t;
5798 find_decls_types (n->decl, fld);
5800 if (!gimple_has_body_p (n->decl))
5801 return;
5803 gcc_assert (current_function_decl == NULL_TREE && cfun == NULL);
5805 fn = DECL_STRUCT_FUNCTION (n->decl);
5807 /* Traverse locals. */
5808 FOR_EACH_LOCAL_DECL (fn, ix, t)
5809 find_decls_types (t, fld);
5811 /* Traverse EH regions in FN. */
5813 eh_region r;
5814 FOR_ALL_EH_REGION_FN (r, fn)
5815 find_decls_types_in_eh_region (r, fld);
5818 /* Traverse every statement in FN. */
5819 FOR_EACH_BB_FN (bb, fn)
5821 gphi_iterator psi;
5822 gimple_stmt_iterator si;
5823 unsigned i;
5825 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
5827 gphi *phi = psi.phi ();
5829 for (i = 0; i < gimple_phi_num_args (phi); i++)
5831 tree *arg_p = gimple_phi_arg_def_ptr (phi, i);
5832 find_decls_types (*arg_p, fld);
5836 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
5838 gimple *stmt = gsi_stmt (si);
5840 if (is_gimple_call (stmt))
5841 find_decls_types (gimple_call_fntype (stmt), fld);
5843 for (i = 0; i < gimple_num_ops (stmt); i++)
5845 tree arg = gimple_op (stmt, i);
5846 find_decls_types (arg, fld);
5853 /* Find decls and types referenced in varpool node N and store them in
5854 FLD->DECLS and FLD->TYPES. Unlike pass_referenced_vars, this will
5855 look for *every* kind of DECL and TYPE node reachable from N,
5856 including those embedded inside types and decls (i.e,, TYPE_DECLs,
5857 NAMESPACE_DECLs, etc). */
5859 static void
5860 find_decls_types_in_var (varpool_node *v, struct free_lang_data_d *fld)
5862 find_decls_types (v->decl, fld);
5865 /* If T needs an assembler name, have one created for it. */
5867 void
5868 assign_assembler_name_if_neeeded (tree t)
5870 if (need_assembler_name_p (t))
5872 /* When setting DECL_ASSEMBLER_NAME, the C++ mangler may emit
5873 diagnostics that use input_location to show locus
5874 information. The problem here is that, at this point,
5875 input_location is generally anchored to the end of the file
5876 (since the parser is long gone), so we don't have a good
5877 position to pin it to.
5879 To alleviate this problem, this uses the location of T's
5880 declaration. Examples of this are
5881 testsuite/g++.dg/template/cond2.C and
5882 testsuite/g++.dg/template/pr35240.C. */
5883 location_t saved_location = input_location;
5884 input_location = DECL_SOURCE_LOCATION (t);
5886 decl_assembler_name (t);
5888 input_location = saved_location;
5893 /* Free language specific information for every operand and expression
5894 in every node of the call graph. This process operates in three stages:
5896 1- Every callgraph node and varpool node is traversed looking for
5897 decls and types embedded in them. This is a more exhaustive
5898 search than that done by find_referenced_vars, because it will
5899 also collect individual fields, decls embedded in types, etc.
5901 2- All the decls found are sent to free_lang_data_in_decl.
5903 3- All the types found are sent to free_lang_data_in_type.
5905 The ordering between decls and types is important because
5906 free_lang_data_in_decl sets assembler names, which includes
5907 mangling. So types cannot be freed up until assembler names have
5908 been set up. */
5910 static void
5911 free_lang_data_in_cgraph (void)
5913 struct cgraph_node *n;
5914 varpool_node *v;
5915 struct free_lang_data_d fld;
5916 tree t;
5917 unsigned i;
5918 alias_pair *p;
5920 /* Initialize sets and arrays to store referenced decls and types. */
5921 fld.pset = new hash_set<tree>;
5922 fld.worklist.create (0);
5923 fld.decls.create (100);
5924 fld.types.create (100);
5926 /* Find decls and types in the body of every function in the callgraph. */
5927 FOR_EACH_FUNCTION (n)
5928 find_decls_types_in_node (n, &fld);
5930 FOR_EACH_VEC_SAFE_ELT (alias_pairs, i, p)
5931 find_decls_types (p->decl, &fld);
5933 /* Find decls and types in every varpool symbol. */
5934 FOR_EACH_VARIABLE (v)
5935 find_decls_types_in_var (v, &fld);
5937 /* Set the assembler name on every decl found. We need to do this
5938 now because free_lang_data_in_decl will invalidate data needed
5939 for mangling. This breaks mangling on interdependent decls. */
5940 FOR_EACH_VEC_ELT (fld.decls, i, t)
5941 assign_assembler_name_if_neeeded (t);
5943 /* Traverse every decl found freeing its language data. */
5944 FOR_EACH_VEC_ELT (fld.decls, i, t)
5945 free_lang_data_in_decl (t);
5947 /* Traverse every type found freeing its language data. */
5948 FOR_EACH_VEC_ELT (fld.types, i, t)
5949 free_lang_data_in_type (t);
5950 if (flag_checking)
5952 FOR_EACH_VEC_ELT (fld.types, i, t)
5953 verify_type (t);
5956 delete fld.pset;
5957 fld.worklist.release ();
5958 fld.decls.release ();
5959 fld.types.release ();
5963 /* Free resources that are used by FE but are not needed once they are done. */
5965 static unsigned
5966 free_lang_data (void)
5968 unsigned i;
5970 /* If we are the LTO frontend we have freed lang-specific data already. */
5971 if (in_lto_p
5972 || (!flag_generate_lto && !flag_generate_offload))
5973 return 0;
5975 /* Allocate and assign alias sets to the standard integer types
5976 while the slots are still in the way the frontends generated them. */
5977 for (i = 0; i < itk_none; ++i)
5978 if (integer_types[i])
5979 TYPE_ALIAS_SET (integer_types[i]) = get_alias_set (integer_types[i]);
5981 /* Traverse the IL resetting language specific information for
5982 operands, expressions, etc. */
5983 free_lang_data_in_cgraph ();
5985 /* Create gimple variants for common types. */
5986 ptrdiff_type_node = integer_type_node;
5987 fileptr_type_node = ptr_type_node;
5989 /* Reset some langhooks. Do not reset types_compatible_p, it may
5990 still be used indirectly via the get_alias_set langhook. */
5991 lang_hooks.dwarf_name = lhd_dwarf_name;
5992 lang_hooks.decl_printable_name = gimple_decl_printable_name;
5993 lang_hooks.gimplify_expr = lhd_gimplify_expr;
5995 /* We do not want the default decl_assembler_name implementation,
5996 rather if we have fixed everything we want a wrapper around it
5997 asserting that all non-local symbols already got their assembler
5998 name and only produce assembler names for local symbols. Or rather
5999 make sure we never call decl_assembler_name on local symbols and
6000 devise a separate, middle-end private scheme for it. */
6002 /* Reset diagnostic machinery. */
6003 tree_diagnostics_defaults (global_dc);
6005 return 0;
6009 namespace {
6011 const pass_data pass_data_ipa_free_lang_data =
6013 SIMPLE_IPA_PASS, /* type */
6014 "*free_lang_data", /* name */
6015 OPTGROUP_NONE, /* optinfo_flags */
6016 TV_IPA_FREE_LANG_DATA, /* tv_id */
6017 0, /* properties_required */
6018 0, /* properties_provided */
6019 0, /* properties_destroyed */
6020 0, /* todo_flags_start */
6021 0, /* todo_flags_finish */
6024 class pass_ipa_free_lang_data : public simple_ipa_opt_pass
6026 public:
6027 pass_ipa_free_lang_data (gcc::context *ctxt)
6028 : simple_ipa_opt_pass (pass_data_ipa_free_lang_data, ctxt)
6031 /* opt_pass methods: */
6032 virtual unsigned int execute (function *) { return free_lang_data (); }
6034 }; // class pass_ipa_free_lang_data
6036 } // anon namespace
6038 simple_ipa_opt_pass *
6039 make_pass_ipa_free_lang_data (gcc::context *ctxt)
6041 return new pass_ipa_free_lang_data (ctxt);
6044 /* The backbone of is_attribute_p(). ATTR_LEN is the string length of
6045 ATTR_NAME. Also used internally by remove_attribute(). */
6046 bool
6047 private_is_attribute_p (const char *attr_name, size_t attr_len, const_tree ident)
6049 size_t ident_len = IDENTIFIER_LENGTH (ident);
6051 if (ident_len == attr_len)
6053 if (strcmp (attr_name, IDENTIFIER_POINTER (ident)) == 0)
6054 return true;
6056 else if (ident_len == attr_len + 4)
6058 /* There is the possibility that ATTR is 'text' and IDENT is
6059 '__text__'. */
6060 const char *p = IDENTIFIER_POINTER (ident);
6061 if (p[0] == '_' && p[1] == '_'
6062 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
6063 && strncmp (attr_name, p + 2, attr_len) == 0)
6064 return true;
6067 return false;
6070 /* The backbone of lookup_attribute(). ATTR_LEN is the string length
6071 of ATTR_NAME, and LIST is not NULL_TREE. */
6072 tree
6073 private_lookup_attribute (const char *attr_name, size_t attr_len, tree list)
6075 while (list)
6077 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
6079 if (ident_len == attr_len)
6081 if (!strcmp (attr_name,
6082 IDENTIFIER_POINTER (get_attribute_name (list))))
6083 break;
6085 /* TODO: If we made sure that attributes were stored in the
6086 canonical form without '__...__' (ie, as in 'text' as opposed
6087 to '__text__') then we could avoid the following case. */
6088 else if (ident_len == attr_len + 4)
6090 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
6091 if (p[0] == '_' && p[1] == '_'
6092 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
6093 && strncmp (attr_name, p + 2, attr_len) == 0)
6094 break;
6096 list = TREE_CHAIN (list);
6099 return list;
6102 /* Given an attribute name ATTR_NAME and a list of attributes LIST,
6103 return a pointer to the attribute's list first element if the attribute
6104 starts with ATTR_NAME. ATTR_NAME must be in the form 'text' (not
6105 '__text__'). */
6107 tree
6108 private_lookup_attribute_by_prefix (const char *attr_name, size_t attr_len,
6109 tree list)
6111 while (list)
6113 size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
6115 if (attr_len > ident_len)
6117 list = TREE_CHAIN (list);
6118 continue;
6121 const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
6123 if (strncmp (attr_name, p, attr_len) == 0)
6124 break;
6126 /* TODO: If we made sure that attributes were stored in the
6127 canonical form without '__...__' (ie, as in 'text' as opposed
6128 to '__text__') then we could avoid the following case. */
6129 if (p[0] == '_' && p[1] == '_' &&
6130 strncmp (attr_name, p + 2, attr_len) == 0)
6131 break;
6133 list = TREE_CHAIN (list);
6136 return list;
6140 /* A variant of lookup_attribute() that can be used with an identifier
6141 as the first argument, and where the identifier can be either
6142 'text' or '__text__'.
6144 Given an attribute ATTR_IDENTIFIER, and a list of attributes LIST,
6145 return a pointer to the attribute's list element if the attribute
6146 is part of the list, or NULL_TREE if not found. If the attribute
6147 appears more than once, this only returns the first occurrence; the
6148 TREE_CHAIN of the return value should be passed back in if further
6149 occurrences are wanted. ATTR_IDENTIFIER must be an identifier but
6150 can be in the form 'text' or '__text__'. */
6151 static tree
6152 lookup_ident_attribute (tree attr_identifier, tree list)
6154 gcc_checking_assert (TREE_CODE (attr_identifier) == IDENTIFIER_NODE);
6156 while (list)
6158 gcc_checking_assert (TREE_CODE (get_attribute_name (list))
6159 == IDENTIFIER_NODE);
6161 if (cmp_attrib_identifiers (attr_identifier,
6162 get_attribute_name (list)))
6163 /* Found it. */
6164 break;
6165 list = TREE_CHAIN (list);
6168 return list;
6171 /* Remove any instances of attribute ATTR_NAME in LIST and return the
6172 modified list. */
6174 tree
6175 remove_attribute (const char *attr_name, tree list)
6177 tree *p;
6178 size_t attr_len = strlen (attr_name);
6180 gcc_checking_assert (attr_name[0] != '_');
6182 for (p = &list; *p; )
6184 tree l = *p;
6185 /* TODO: If we were storing attributes in normalized form, here
6186 we could use a simple strcmp(). */
6187 if (private_is_attribute_p (attr_name, attr_len, get_attribute_name (l)))
6188 *p = TREE_CHAIN (l);
6189 else
6190 p = &TREE_CHAIN (l);
6193 return list;
6196 /* Return an attribute list that is the union of a1 and a2. */
6198 tree
6199 merge_attributes (tree a1, tree a2)
6201 tree attributes;
6203 /* Either one unset? Take the set one. */
6205 if ((attributes = a1) == 0)
6206 attributes = a2;
6208 /* One that completely contains the other? Take it. */
6210 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
6212 if (attribute_list_contained (a2, a1))
6213 attributes = a2;
6214 else
6216 /* Pick the longest list, and hang on the other list. */
6218 if (list_length (a1) < list_length (a2))
6219 attributes = a2, a2 = a1;
6221 for (; a2 != 0; a2 = TREE_CHAIN (a2))
6223 tree a;
6224 for (a = lookup_ident_attribute (get_attribute_name (a2),
6225 attributes);
6226 a != NULL_TREE && !attribute_value_equal (a, a2);
6227 a = lookup_ident_attribute (get_attribute_name (a2),
6228 TREE_CHAIN (a)))
6230 if (a == NULL_TREE)
6232 a1 = copy_node (a2);
6233 TREE_CHAIN (a1) = attributes;
6234 attributes = a1;
6239 return attributes;
6242 /* Given types T1 and T2, merge their attributes and return
6243 the result. */
6245 tree
6246 merge_type_attributes (tree t1, tree t2)
6248 return merge_attributes (TYPE_ATTRIBUTES (t1),
6249 TYPE_ATTRIBUTES (t2));
6252 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
6253 the result. */
6255 tree
6256 merge_decl_attributes (tree olddecl, tree newdecl)
6258 return merge_attributes (DECL_ATTRIBUTES (olddecl),
6259 DECL_ATTRIBUTES (newdecl));
6262 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
6264 /* Specialization of merge_decl_attributes for various Windows targets.
6266 This handles the following situation:
6268 __declspec (dllimport) int foo;
6269 int foo;
6271 The second instance of `foo' nullifies the dllimport. */
6273 tree
6274 merge_dllimport_decl_attributes (tree old, tree new_tree)
6276 tree a;
6277 int delete_dllimport_p = 1;
6279 /* What we need to do here is remove from `old' dllimport if it doesn't
6280 appear in `new'. dllimport behaves like extern: if a declaration is
6281 marked dllimport and a definition appears later, then the object
6282 is not dllimport'd. We also remove a `new' dllimport if the old list
6283 contains dllexport: dllexport always overrides dllimport, regardless
6284 of the order of declaration. */
6285 if (!VAR_OR_FUNCTION_DECL_P (new_tree))
6286 delete_dllimport_p = 0;
6287 else if (DECL_DLLIMPORT_P (new_tree)
6288 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (old)))
6290 DECL_DLLIMPORT_P (new_tree) = 0;
6291 warning (OPT_Wattributes, "%q+D already declared with dllexport attribute: "
6292 "dllimport ignored", new_tree);
6294 else if (DECL_DLLIMPORT_P (old) && !DECL_DLLIMPORT_P (new_tree))
6296 /* Warn about overriding a symbol that has already been used, e.g.:
6297 extern int __attribute__ ((dllimport)) foo;
6298 int* bar () {return &foo;}
6299 int foo;
6301 if (TREE_USED (old))
6303 warning (0, "%q+D redeclared without dllimport attribute "
6304 "after being referenced with dll linkage", new_tree);
6305 /* If we have used a variable's address with dllimport linkage,
6306 keep the old DECL_DLLIMPORT_P flag: the ADDR_EXPR using the
6307 decl may already have had TREE_CONSTANT computed.
6308 We still remove the attribute so that assembler code refers
6309 to '&foo rather than '_imp__foo'. */
6310 if (TREE_CODE (old) == VAR_DECL && TREE_ADDRESSABLE (old))
6311 DECL_DLLIMPORT_P (new_tree) = 1;
6314 /* Let an inline definition silently override the external reference,
6315 but otherwise warn about attribute inconsistency. */
6316 else if (TREE_CODE (new_tree) == VAR_DECL
6317 || !DECL_DECLARED_INLINE_P (new_tree))
6318 warning (OPT_Wattributes, "%q+D redeclared without dllimport attribute: "
6319 "previous dllimport ignored", new_tree);
6321 else
6322 delete_dllimport_p = 0;
6324 a = merge_attributes (DECL_ATTRIBUTES (old), DECL_ATTRIBUTES (new_tree));
6326 if (delete_dllimport_p)
6327 a = remove_attribute ("dllimport", a);
6329 return a;
6332 /* Handle a "dllimport" or "dllexport" attribute; arguments as in
6333 struct attribute_spec.handler. */
6335 tree
6336 handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
6337 bool *no_add_attrs)
6339 tree node = *pnode;
6340 bool is_dllimport;
6342 /* These attributes may apply to structure and union types being created,
6343 but otherwise should pass to the declaration involved. */
6344 if (!DECL_P (node))
6346 if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
6347 | (int) ATTR_FLAG_ARRAY_NEXT))
6349 *no_add_attrs = true;
6350 return tree_cons (name, args, NULL_TREE);
6352 if (TREE_CODE (node) == RECORD_TYPE
6353 || TREE_CODE (node) == UNION_TYPE)
6355 node = TYPE_NAME (node);
6356 if (!node)
6357 return NULL_TREE;
6359 else
6361 warning (OPT_Wattributes, "%qE attribute ignored",
6362 name);
6363 *no_add_attrs = true;
6364 return NULL_TREE;
6368 if (TREE_CODE (node) != FUNCTION_DECL
6369 && TREE_CODE (node) != VAR_DECL
6370 && TREE_CODE (node) != TYPE_DECL)
6372 *no_add_attrs = true;
6373 warning (OPT_Wattributes, "%qE attribute ignored",
6374 name);
6375 return NULL_TREE;
6378 if (TREE_CODE (node) == TYPE_DECL
6379 && TREE_CODE (TREE_TYPE (node)) != RECORD_TYPE
6380 && TREE_CODE (TREE_TYPE (node)) != UNION_TYPE)
6382 *no_add_attrs = true;
6383 warning (OPT_Wattributes, "%qE attribute ignored",
6384 name);
6385 return NULL_TREE;
6388 is_dllimport = is_attribute_p ("dllimport", name);
6390 /* Report error on dllimport ambiguities seen now before they cause
6391 any damage. */
6392 if (is_dllimport)
6394 /* Honor any target-specific overrides. */
6395 if (!targetm.valid_dllimport_attribute_p (node))
6396 *no_add_attrs = true;
6398 else if (TREE_CODE (node) == FUNCTION_DECL
6399 && DECL_DECLARED_INLINE_P (node))
6401 warning (OPT_Wattributes, "inline function %q+D declared as "
6402 " dllimport: attribute ignored", node);
6403 *no_add_attrs = true;
6405 /* Like MS, treat definition of dllimported variables and
6406 non-inlined functions on declaration as syntax errors. */
6407 else if (TREE_CODE (node) == FUNCTION_DECL && DECL_INITIAL (node))
6409 error ("function %q+D definition is marked dllimport", node);
6410 *no_add_attrs = true;
6413 else if (TREE_CODE (node) == VAR_DECL)
6415 if (DECL_INITIAL (node))
6417 error ("variable %q+D definition is marked dllimport",
6418 node);
6419 *no_add_attrs = true;
6422 /* `extern' needn't be specified with dllimport.
6423 Specify `extern' now and hope for the best. Sigh. */
6424 DECL_EXTERNAL (node) = 1;
6425 /* Also, implicitly give dllimport'd variables declared within
6426 a function global scope, unless declared static. */
6427 if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
6428 TREE_PUBLIC (node) = 1;
6431 if (*no_add_attrs == false)
6432 DECL_DLLIMPORT_P (node) = 1;
6434 else if (TREE_CODE (node) == FUNCTION_DECL
6435 && DECL_DECLARED_INLINE_P (node)
6436 && flag_keep_inline_dllexport)
6437 /* An exported function, even if inline, must be emitted. */
6438 DECL_EXTERNAL (node) = 0;
6440 /* Report error if symbol is not accessible at global scope. */
6441 if (!TREE_PUBLIC (node)
6442 && (TREE_CODE (node) == VAR_DECL
6443 || TREE_CODE (node) == FUNCTION_DECL))
6445 error ("external linkage required for symbol %q+D because of "
6446 "%qE attribute", node, name);
6447 *no_add_attrs = true;
6450 /* A dllexport'd entity must have default visibility so that other
6451 program units (shared libraries or the main executable) can see
6452 it. A dllimport'd entity must have default visibility so that
6453 the linker knows that undefined references within this program
6454 unit can be resolved by the dynamic linker. */
6455 if (!*no_add_attrs)
6457 if (DECL_VISIBILITY_SPECIFIED (node)
6458 && DECL_VISIBILITY (node) != VISIBILITY_DEFAULT)
6459 error ("%qE implies default visibility, but %qD has already "
6460 "been declared with a different visibility",
6461 name, node);
6462 DECL_VISIBILITY (node) = VISIBILITY_DEFAULT;
6463 DECL_VISIBILITY_SPECIFIED (node) = 1;
6466 return NULL_TREE;
6469 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
6471 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
6472 of the various TYPE_QUAL values. */
6474 static void
6475 set_type_quals (tree type, int type_quals)
6477 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
6478 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
6479 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
6480 TYPE_ATOMIC (type) = (type_quals & TYPE_QUAL_ATOMIC) != 0;
6481 TYPE_ADDR_SPACE (type) = DECODE_QUAL_ADDR_SPACE (type_quals);
6484 /* Returns true iff unqualified CAND and BASE are equivalent. */
6486 bool
6487 check_base_type (const_tree cand, const_tree base)
6489 return (TYPE_NAME (cand) == TYPE_NAME (base)
6490 /* Apparently this is needed for Objective-C. */
6491 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
6492 /* Check alignment. */
6493 && TYPE_ALIGN (cand) == TYPE_ALIGN (base)
6494 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
6495 TYPE_ATTRIBUTES (base)));
6498 /* Returns true iff CAND is equivalent to BASE with TYPE_QUALS. */
6500 bool
6501 check_qualified_type (const_tree cand, const_tree base, int type_quals)
6503 return (TYPE_QUALS (cand) == type_quals
6504 && check_base_type (cand, base));
6507 /* Returns true iff CAND is equivalent to BASE with ALIGN. */
6509 static bool
6510 check_aligned_type (const_tree cand, const_tree base, unsigned int align)
6512 return (TYPE_QUALS (cand) == TYPE_QUALS (base)
6513 && TYPE_NAME (cand) == TYPE_NAME (base)
6514 /* Apparently this is needed for Objective-C. */
6515 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
6516 /* Check alignment. */
6517 && TYPE_ALIGN (cand) == align
6518 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
6519 TYPE_ATTRIBUTES (base)));
6522 /* This function checks to see if TYPE matches the size one of the built-in
6523 atomic types, and returns that core atomic type. */
6525 static tree
6526 find_atomic_core_type (tree type)
6528 tree base_atomic_type;
6530 /* Only handle complete types. */
6531 if (TYPE_SIZE (type) == NULL_TREE)
6532 return NULL_TREE;
6534 HOST_WIDE_INT type_size = tree_to_uhwi (TYPE_SIZE (type));
6535 switch (type_size)
6537 case 8:
6538 base_atomic_type = atomicQI_type_node;
6539 break;
6541 case 16:
6542 base_atomic_type = atomicHI_type_node;
6543 break;
6545 case 32:
6546 base_atomic_type = atomicSI_type_node;
6547 break;
6549 case 64:
6550 base_atomic_type = atomicDI_type_node;
6551 break;
6553 case 128:
6554 base_atomic_type = atomicTI_type_node;
6555 break;
6557 default:
6558 base_atomic_type = NULL_TREE;
6561 return base_atomic_type;
6564 /* Return a version of the TYPE, qualified as indicated by the
6565 TYPE_QUALS, if one exists. If no qualified version exists yet,
6566 return NULL_TREE. */
6568 tree
6569 get_qualified_type (tree type, int type_quals)
6571 tree t;
6573 if (TYPE_QUALS (type) == type_quals)
6574 return type;
6576 /* Search the chain of variants to see if there is already one there just
6577 like the one we need to have. If so, use that existing one. We must
6578 preserve the TYPE_NAME, since there is code that depends on this. */
6579 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6580 if (check_qualified_type (t, type, type_quals))
6581 return t;
6583 return NULL_TREE;
6586 /* Like get_qualified_type, but creates the type if it does not
6587 exist. This function never returns NULL_TREE. */
6589 tree
6590 build_qualified_type (tree type, int type_quals)
6592 tree t;
6594 /* See if we already have the appropriate qualified variant. */
6595 t = get_qualified_type (type, type_quals);
6597 /* If not, build it. */
6598 if (!t)
6600 t = build_variant_type_copy (type);
6601 set_type_quals (t, type_quals);
6603 if (((type_quals & TYPE_QUAL_ATOMIC) == TYPE_QUAL_ATOMIC))
6605 /* See if this object can map to a basic atomic type. */
6606 tree atomic_type = find_atomic_core_type (type);
6607 if (atomic_type)
6609 /* Ensure the alignment of this type is compatible with
6610 the required alignment of the atomic type. */
6611 if (TYPE_ALIGN (atomic_type) > TYPE_ALIGN (t))
6612 TYPE_ALIGN (t) = TYPE_ALIGN (atomic_type);
6616 if (TYPE_STRUCTURAL_EQUALITY_P (type))
6617 /* Propagate structural equality. */
6618 SET_TYPE_STRUCTURAL_EQUALITY (t);
6619 else if (TYPE_CANONICAL (type) != type)
6620 /* Build the underlying canonical type, since it is different
6621 from TYPE. */
6623 tree c = build_qualified_type (TYPE_CANONICAL (type), type_quals);
6624 TYPE_CANONICAL (t) = TYPE_CANONICAL (c);
6626 else
6627 /* T is its own canonical type. */
6628 TYPE_CANONICAL (t) = t;
6632 return t;
6635 /* Create a variant of type T with alignment ALIGN. */
6637 tree
6638 build_aligned_type (tree type, unsigned int align)
6640 tree t;
6642 if (TYPE_PACKED (type)
6643 || TYPE_ALIGN (type) == align)
6644 return type;
6646 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
6647 if (check_aligned_type (t, type, align))
6648 return t;
6650 t = build_variant_type_copy (type);
6651 TYPE_ALIGN (t) = align;
6653 return t;
6656 /* Create a new distinct copy of TYPE. The new type is made its own
6657 MAIN_VARIANT. If TYPE requires structural equality checks, the
6658 resulting type requires structural equality checks; otherwise, its
6659 TYPE_CANONICAL points to itself. */
6661 tree
6662 build_distinct_type_copy (tree type)
6664 tree t = copy_node (type);
6666 TYPE_POINTER_TO (t) = 0;
6667 TYPE_REFERENCE_TO (t) = 0;
6669 /* Set the canonical type either to a new equivalence class, or
6670 propagate the need for structural equality checks. */
6671 if (TYPE_STRUCTURAL_EQUALITY_P (type))
6672 SET_TYPE_STRUCTURAL_EQUALITY (t);
6673 else
6674 TYPE_CANONICAL (t) = t;
6676 /* Make it its own variant. */
6677 TYPE_MAIN_VARIANT (t) = t;
6678 TYPE_NEXT_VARIANT (t) = 0;
6680 /* We do not record methods in type copies nor variants
6681 so we do not need to keep them up to date when new method
6682 is inserted. */
6683 if (RECORD_OR_UNION_TYPE_P (t))
6684 TYPE_METHODS (t) = NULL_TREE;
6686 /* Note that it is now possible for TYPE_MIN_VALUE to be a value
6687 whose TREE_TYPE is not t. This can also happen in the Ada
6688 frontend when using subtypes. */
6690 return t;
6693 /* Create a new variant of TYPE, equivalent but distinct. This is so
6694 the caller can modify it. TYPE_CANONICAL for the return type will
6695 be equivalent to TYPE_CANONICAL of TYPE, indicating that the types
6696 are considered equal by the language itself (or that both types
6697 require structural equality checks). */
6699 tree
6700 build_variant_type_copy (tree type)
6702 tree t, m = TYPE_MAIN_VARIANT (type);
6704 t = build_distinct_type_copy (type);
6706 /* Since we're building a variant, assume that it is a non-semantic
6707 variant. This also propagates TYPE_STRUCTURAL_EQUALITY_P. */
6708 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
6710 /* Add the new type to the chain of variants of TYPE. */
6711 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
6712 TYPE_NEXT_VARIANT (m) = t;
6713 TYPE_MAIN_VARIANT (t) = m;
6715 return t;
6718 /* Return true if the from tree in both tree maps are equal. */
6721 tree_map_base_eq (const void *va, const void *vb)
6723 const struct tree_map_base *const a = (const struct tree_map_base *) va,
6724 *const b = (const struct tree_map_base *) vb;
6725 return (a->from == b->from);
6728 /* Hash a from tree in a tree_base_map. */
6730 unsigned int
6731 tree_map_base_hash (const void *item)
6733 return htab_hash_pointer (((const struct tree_map_base *)item)->from);
6736 /* Return true if this tree map structure is marked for garbage collection
6737 purposes. We simply return true if the from tree is marked, so that this
6738 structure goes away when the from tree goes away. */
6741 tree_map_base_marked_p (const void *p)
6743 return ggc_marked_p (((const struct tree_map_base *) p)->from);
6746 /* Hash a from tree in a tree_map. */
6748 unsigned int
6749 tree_map_hash (const void *item)
6751 return (((const struct tree_map *) item)->hash);
6754 /* Hash a from tree in a tree_decl_map. */
6756 unsigned int
6757 tree_decl_map_hash (const void *item)
6759 return DECL_UID (((const struct tree_decl_map *) item)->base.from);
6762 /* Return the initialization priority for DECL. */
6764 priority_type
6765 decl_init_priority_lookup (tree decl)
6767 symtab_node *snode = symtab_node::get (decl);
6769 if (!snode)
6770 return DEFAULT_INIT_PRIORITY;
6771 return
6772 snode->get_init_priority ();
6775 /* Return the finalization priority for DECL. */
6777 priority_type
6778 decl_fini_priority_lookup (tree decl)
6780 cgraph_node *node = cgraph_node::get (decl);
6782 if (!node)
6783 return DEFAULT_INIT_PRIORITY;
6784 return
6785 node->get_fini_priority ();
6788 /* Set the initialization priority for DECL to PRIORITY. */
6790 void
6791 decl_init_priority_insert (tree decl, priority_type priority)
6793 struct symtab_node *snode;
6795 if (priority == DEFAULT_INIT_PRIORITY)
6797 snode = symtab_node::get (decl);
6798 if (!snode)
6799 return;
6801 else if (TREE_CODE (decl) == VAR_DECL)
6802 snode = varpool_node::get_create (decl);
6803 else
6804 snode = cgraph_node::get_create (decl);
6805 snode->set_init_priority (priority);
6808 /* Set the finalization priority for DECL to PRIORITY. */
6810 void
6811 decl_fini_priority_insert (tree decl, priority_type priority)
6813 struct cgraph_node *node;
6815 if (priority == DEFAULT_INIT_PRIORITY)
6817 node = cgraph_node::get (decl);
6818 if (!node)
6819 return;
6821 else
6822 node = cgraph_node::get_create (decl);
6823 node->set_fini_priority (priority);
6826 /* Print out the statistics for the DECL_DEBUG_EXPR hash table. */
6828 static void
6829 print_debug_expr_statistics (void)
6831 fprintf (stderr, "DECL_DEBUG_EXPR hash: size %ld, %ld elements, %f collisions\n",
6832 (long) debug_expr_for_decl->size (),
6833 (long) debug_expr_for_decl->elements (),
6834 debug_expr_for_decl->collisions ());
6837 /* Print out the statistics for the DECL_VALUE_EXPR hash table. */
6839 static void
6840 print_value_expr_statistics (void)
6842 fprintf (stderr, "DECL_VALUE_EXPR hash: size %ld, %ld elements, %f collisions\n",
6843 (long) value_expr_for_decl->size (),
6844 (long) value_expr_for_decl->elements (),
6845 value_expr_for_decl->collisions ());
6848 /* Lookup a debug expression for FROM, and return it if we find one. */
6850 tree
6851 decl_debug_expr_lookup (tree from)
6853 struct tree_decl_map *h, in;
6854 in.base.from = from;
6856 h = debug_expr_for_decl->find_with_hash (&in, DECL_UID (from));
6857 if (h)
6858 return h->to;
6859 return NULL_TREE;
6862 /* Insert a mapping FROM->TO in the debug expression hashtable. */
6864 void
6865 decl_debug_expr_insert (tree from, tree to)
6867 struct tree_decl_map *h;
6869 h = ggc_alloc<tree_decl_map> ();
6870 h->base.from = from;
6871 h->to = to;
6872 *debug_expr_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT) = h;
6875 /* Lookup a value expression for FROM, and return it if we find one. */
6877 tree
6878 decl_value_expr_lookup (tree from)
6880 struct tree_decl_map *h, in;
6881 in.base.from = from;
6883 h = value_expr_for_decl->find_with_hash (&in, DECL_UID (from));
6884 if (h)
6885 return h->to;
6886 return NULL_TREE;
6889 /* Insert a mapping FROM->TO in the value expression hashtable. */
6891 void
6892 decl_value_expr_insert (tree from, tree to)
6894 struct tree_decl_map *h;
6896 h = ggc_alloc<tree_decl_map> ();
6897 h->base.from = from;
6898 h->to = to;
6899 *value_expr_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT) = h;
6902 /* Lookup a vector of debug arguments for FROM, and return it if we
6903 find one. */
6905 vec<tree, va_gc> **
6906 decl_debug_args_lookup (tree from)
6908 struct tree_vec_map *h, in;
6910 if (!DECL_HAS_DEBUG_ARGS_P (from))
6911 return NULL;
6912 gcc_checking_assert (debug_args_for_decl != NULL);
6913 in.base.from = from;
6914 h = debug_args_for_decl->find_with_hash (&in, DECL_UID (from));
6915 if (h)
6916 return &h->to;
6917 return NULL;
6920 /* Insert a mapping FROM->empty vector of debug arguments in the value
6921 expression hashtable. */
6923 vec<tree, va_gc> **
6924 decl_debug_args_insert (tree from)
6926 struct tree_vec_map *h;
6927 tree_vec_map **loc;
6929 if (DECL_HAS_DEBUG_ARGS_P (from))
6930 return decl_debug_args_lookup (from);
6931 if (debug_args_for_decl == NULL)
6932 debug_args_for_decl = hash_table<tree_vec_map_cache_hasher>::create_ggc (64);
6933 h = ggc_alloc<tree_vec_map> ();
6934 h->base.from = from;
6935 h->to = NULL;
6936 loc = debug_args_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT);
6937 *loc = h;
6938 DECL_HAS_DEBUG_ARGS_P (from) = 1;
6939 return &h->to;
6942 /* Hashing of types so that we don't make duplicates.
6943 The entry point is `type_hash_canon'. */
6945 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
6946 with types in the TREE_VALUE slots), by adding the hash codes
6947 of the individual types. */
6949 static void
6950 type_hash_list (const_tree list, inchash::hash &hstate)
6952 const_tree tail;
6954 for (tail = list; tail; tail = TREE_CHAIN (tail))
6955 if (TREE_VALUE (tail) != error_mark_node)
6956 hstate.add_object (TYPE_HASH (TREE_VALUE (tail)));
6959 /* These are the Hashtable callback functions. */
6961 /* Returns true iff the types are equivalent. */
6963 bool
6964 type_cache_hasher::equal (type_hash *a, type_hash *b)
6966 /* First test the things that are the same for all types. */
6967 if (a->hash != b->hash
6968 || TREE_CODE (a->type) != TREE_CODE (b->type)
6969 || TREE_TYPE (a->type) != TREE_TYPE (b->type)
6970 || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
6971 TYPE_ATTRIBUTES (b->type))
6972 || (TREE_CODE (a->type) != COMPLEX_TYPE
6973 && TYPE_NAME (a->type) != TYPE_NAME (b->type)))
6974 return 0;
6976 /* Be careful about comparing arrays before and after the element type
6977 has been completed; don't compare TYPE_ALIGN unless both types are
6978 complete. */
6979 if (COMPLETE_TYPE_P (a->type) && COMPLETE_TYPE_P (b->type)
6980 && (TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
6981 || TYPE_MODE (a->type) != TYPE_MODE (b->type)))
6982 return 0;
6984 switch (TREE_CODE (a->type))
6986 case VOID_TYPE:
6987 case COMPLEX_TYPE:
6988 case POINTER_TYPE:
6989 case REFERENCE_TYPE:
6990 case NULLPTR_TYPE:
6991 return 1;
6993 case VECTOR_TYPE:
6994 return TYPE_VECTOR_SUBPARTS (a->type) == TYPE_VECTOR_SUBPARTS (b->type);
6996 case ENUMERAL_TYPE:
6997 if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
6998 && !(TYPE_VALUES (a->type)
6999 && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
7000 && TYPE_VALUES (b->type)
7001 && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
7002 && type_list_equal (TYPE_VALUES (a->type),
7003 TYPE_VALUES (b->type))))
7004 return 0;
7006 /* ... fall through ... */
7008 case INTEGER_TYPE:
7009 case REAL_TYPE:
7010 case BOOLEAN_TYPE:
7011 if (TYPE_PRECISION (a->type) != TYPE_PRECISION (b->type))
7012 return false;
7013 return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
7014 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
7015 TYPE_MAX_VALUE (b->type)))
7016 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
7017 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
7018 TYPE_MIN_VALUE (b->type))));
7020 case FIXED_POINT_TYPE:
7021 return TYPE_SATURATING (a->type) == TYPE_SATURATING (b->type);
7023 case OFFSET_TYPE:
7024 return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
7026 case METHOD_TYPE:
7027 if (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
7028 && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
7029 || (TYPE_ARG_TYPES (a->type)
7030 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
7031 && TYPE_ARG_TYPES (b->type)
7032 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
7033 && type_list_equal (TYPE_ARG_TYPES (a->type),
7034 TYPE_ARG_TYPES (b->type)))))
7035 break;
7036 return 0;
7037 case ARRAY_TYPE:
7038 return TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type);
7040 case RECORD_TYPE:
7041 case UNION_TYPE:
7042 case QUAL_UNION_TYPE:
7043 return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
7044 || (TYPE_FIELDS (a->type)
7045 && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
7046 && TYPE_FIELDS (b->type)
7047 && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
7048 && type_list_equal (TYPE_FIELDS (a->type),
7049 TYPE_FIELDS (b->type))));
7051 case FUNCTION_TYPE:
7052 if (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
7053 || (TYPE_ARG_TYPES (a->type)
7054 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
7055 && TYPE_ARG_TYPES (b->type)
7056 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
7057 && type_list_equal (TYPE_ARG_TYPES (a->type),
7058 TYPE_ARG_TYPES (b->type))))
7059 break;
7060 return 0;
7062 default:
7063 return 0;
7066 if (lang_hooks.types.type_hash_eq != NULL)
7067 return lang_hooks.types.type_hash_eq (a->type, b->type);
7069 return 1;
7072 /* Given TYPE, and HASHCODE its hash code, return the canonical
7073 object for an identical type if one already exists.
7074 Otherwise, return TYPE, and record it as the canonical object.
7076 To use this function, first create a type of the sort you want.
7077 Then compute its hash code from the fields of the type that
7078 make it different from other similar types.
7079 Then call this function and use the value. */
7081 tree
7082 type_hash_canon (unsigned int hashcode, tree type)
7084 type_hash in;
7085 type_hash **loc;
7087 /* The hash table only contains main variants, so ensure that's what we're
7088 being passed. */
7089 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
7091 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
7092 must call that routine before comparing TYPE_ALIGNs. */
7093 layout_type (type);
7095 in.hash = hashcode;
7096 in.type = type;
7098 loc = type_hash_table->find_slot_with_hash (&in, hashcode, INSERT);
7099 if (*loc)
7101 tree t1 = ((type_hash *) *loc)->type;
7102 gcc_assert (TYPE_MAIN_VARIANT (t1) == t1);
7103 if (GATHER_STATISTICS)
7105 tree_code_counts[(int) TREE_CODE (type)]--;
7106 tree_node_counts[(int) t_kind]--;
7107 tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type_non_common);
7109 return t1;
7111 else
7113 struct type_hash *h;
7115 h = ggc_alloc<type_hash> ();
7116 h->hash = hashcode;
7117 h->type = type;
7118 *loc = h;
7120 return type;
7124 static void
7125 print_type_hash_statistics (void)
7127 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
7128 (long) type_hash_table->size (),
7129 (long) type_hash_table->elements (),
7130 type_hash_table->collisions ());
7133 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
7134 with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
7135 by adding the hash codes of the individual attributes. */
7137 static void
7138 attribute_hash_list (const_tree list, inchash::hash &hstate)
7140 const_tree tail;
7142 for (tail = list; tail; tail = TREE_CHAIN (tail))
7143 /* ??? Do we want to add in TREE_VALUE too? */
7144 hstate.add_object (IDENTIFIER_HASH_VALUE (get_attribute_name (tail)));
7147 /* Given two lists of attributes, return true if list l2 is
7148 equivalent to l1. */
7151 attribute_list_equal (const_tree l1, const_tree l2)
7153 if (l1 == l2)
7154 return 1;
7156 return attribute_list_contained (l1, l2)
7157 && attribute_list_contained (l2, l1);
7160 /* Given two lists of attributes, return true if list L2 is
7161 completely contained within L1. */
7162 /* ??? This would be faster if attribute names were stored in a canonicalized
7163 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
7164 must be used to show these elements are equivalent (which they are). */
7165 /* ??? It's not clear that attributes with arguments will always be handled
7166 correctly. */
7169 attribute_list_contained (const_tree l1, const_tree l2)
7171 const_tree t1, t2;
7173 /* First check the obvious, maybe the lists are identical. */
7174 if (l1 == l2)
7175 return 1;
7177 /* Maybe the lists are similar. */
7178 for (t1 = l1, t2 = l2;
7179 t1 != 0 && t2 != 0
7180 && get_attribute_name (t1) == get_attribute_name (t2)
7181 && TREE_VALUE (t1) == TREE_VALUE (t2);
7182 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
7185 /* Maybe the lists are equal. */
7186 if (t1 == 0 && t2 == 0)
7187 return 1;
7189 for (; t2 != 0; t2 = TREE_CHAIN (t2))
7191 const_tree attr;
7192 /* This CONST_CAST is okay because lookup_attribute does not
7193 modify its argument and the return value is assigned to a
7194 const_tree. */
7195 for (attr = lookup_ident_attribute (get_attribute_name (t2),
7196 CONST_CAST_TREE (l1));
7197 attr != NULL_TREE && !attribute_value_equal (t2, attr);
7198 attr = lookup_ident_attribute (get_attribute_name (t2),
7199 TREE_CHAIN (attr)))
7202 if (attr == NULL_TREE)
7203 return 0;
7206 return 1;
7209 /* Given two lists of types
7210 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
7211 return 1 if the lists contain the same types in the same order.
7212 Also, the TREE_PURPOSEs must match. */
7215 type_list_equal (const_tree l1, const_tree l2)
7217 const_tree t1, t2;
7219 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
7220 if (TREE_VALUE (t1) != TREE_VALUE (t2)
7221 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
7222 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
7223 && (TREE_TYPE (TREE_PURPOSE (t1))
7224 == TREE_TYPE (TREE_PURPOSE (t2))))))
7225 return 0;
7227 return t1 == t2;
7230 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
7231 given by TYPE. If the argument list accepts variable arguments,
7232 then this function counts only the ordinary arguments. */
7235 type_num_arguments (const_tree type)
7237 int i = 0;
7238 tree t;
7240 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
7241 /* If the function does not take a variable number of arguments,
7242 the last element in the list will have type `void'. */
7243 if (VOID_TYPE_P (TREE_VALUE (t)))
7244 break;
7245 else
7246 ++i;
7248 return i;
7251 /* Nonzero if integer constants T1 and T2
7252 represent the same constant value. */
7255 tree_int_cst_equal (const_tree t1, const_tree t2)
7257 if (t1 == t2)
7258 return 1;
7260 if (t1 == 0 || t2 == 0)
7261 return 0;
7263 if (TREE_CODE (t1) == INTEGER_CST
7264 && TREE_CODE (t2) == INTEGER_CST
7265 && wi::to_widest (t1) == wi::to_widest (t2))
7266 return 1;
7268 return 0;
7271 /* Return true if T is an INTEGER_CST whose numerical value (extended
7272 according to TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. */
7274 bool
7275 tree_fits_shwi_p (const_tree t)
7277 return (t != NULL_TREE
7278 && TREE_CODE (t) == INTEGER_CST
7279 && wi::fits_shwi_p (wi::to_widest (t)));
7282 /* Return true if T is an INTEGER_CST whose numerical value (extended
7283 according to TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. */
7285 bool
7286 tree_fits_uhwi_p (const_tree t)
7288 return (t != NULL_TREE
7289 && TREE_CODE (t) == INTEGER_CST
7290 && wi::fits_uhwi_p (wi::to_widest (t)));
7293 /* T is an INTEGER_CST whose numerical value (extended according to
7294 TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. Return that
7295 HOST_WIDE_INT. */
7297 HOST_WIDE_INT
7298 tree_to_shwi (const_tree t)
7300 gcc_assert (tree_fits_shwi_p (t));
7301 return TREE_INT_CST_LOW (t);
7304 /* T is an INTEGER_CST whose numerical value (extended according to
7305 TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. Return that
7306 HOST_WIDE_INT. */
7308 unsigned HOST_WIDE_INT
7309 tree_to_uhwi (const_tree t)
7311 gcc_assert (tree_fits_uhwi_p (t));
7312 return TREE_INT_CST_LOW (t);
7315 /* Return the most significant (sign) bit of T. */
7318 tree_int_cst_sign_bit (const_tree t)
7320 unsigned bitno = TYPE_PRECISION (TREE_TYPE (t)) - 1;
7322 return wi::extract_uhwi (t, bitno, 1);
7325 /* Return an indication of the sign of the integer constant T.
7326 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
7327 Note that -1 will never be returned if T's type is unsigned. */
7330 tree_int_cst_sgn (const_tree t)
7332 if (wi::eq_p (t, 0))
7333 return 0;
7334 else if (TYPE_UNSIGNED (TREE_TYPE (t)))
7335 return 1;
7336 else if (wi::neg_p (t))
7337 return -1;
7338 else
7339 return 1;
7342 /* Return the minimum number of bits needed to represent VALUE in a
7343 signed or unsigned type, UNSIGNEDP says which. */
7345 unsigned int
7346 tree_int_cst_min_precision (tree value, signop sgn)
7348 /* If the value is negative, compute its negative minus 1. The latter
7349 adjustment is because the absolute value of the largest negative value
7350 is one larger than the largest positive value. This is equivalent to
7351 a bit-wise negation, so use that operation instead. */
7353 if (tree_int_cst_sgn (value) < 0)
7354 value = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (value), value);
7356 /* Return the number of bits needed, taking into account the fact
7357 that we need one more bit for a signed than unsigned type.
7358 If value is 0 or -1, the minimum precision is 1 no matter
7359 whether unsignedp is true or false. */
7361 if (integer_zerop (value))
7362 return 1;
7363 else
7364 return tree_floor_log2 (value) + 1 + (sgn == SIGNED ? 1 : 0) ;
7367 /* Return truthvalue of whether T1 is the same tree structure as T2.
7368 Return 1 if they are the same.
7369 Return 0 if they are understandably different.
7370 Return -1 if either contains tree structure not understood by
7371 this function. */
7374 simple_cst_equal (const_tree t1, const_tree t2)
7376 enum tree_code code1, code2;
7377 int cmp;
7378 int i;
7380 if (t1 == t2)
7381 return 1;
7382 if (t1 == 0 || t2 == 0)
7383 return 0;
7385 code1 = TREE_CODE (t1);
7386 code2 = TREE_CODE (t2);
7388 if (CONVERT_EXPR_CODE_P (code1) || code1 == NON_LVALUE_EXPR)
7390 if (CONVERT_EXPR_CODE_P (code2)
7391 || code2 == NON_LVALUE_EXPR)
7392 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7393 else
7394 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
7397 else if (CONVERT_EXPR_CODE_P (code2)
7398 || code2 == NON_LVALUE_EXPR)
7399 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
7401 if (code1 != code2)
7402 return 0;
7404 switch (code1)
7406 case INTEGER_CST:
7407 return wi::to_widest (t1) == wi::to_widest (t2);
7409 case REAL_CST:
7410 return real_identical (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
7412 case FIXED_CST:
7413 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
7415 case STRING_CST:
7416 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
7417 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
7418 TREE_STRING_LENGTH (t1)));
7420 case CONSTRUCTOR:
7422 unsigned HOST_WIDE_INT idx;
7423 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (t1);
7424 vec<constructor_elt, va_gc> *v2 = CONSTRUCTOR_ELTS (t2);
7426 if (vec_safe_length (v1) != vec_safe_length (v2))
7427 return false;
7429 for (idx = 0; idx < vec_safe_length (v1); ++idx)
7430 /* ??? Should we handle also fields here? */
7431 if (!simple_cst_equal ((*v1)[idx].value, (*v2)[idx].value))
7432 return false;
7433 return true;
7436 case SAVE_EXPR:
7437 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7439 case CALL_EXPR:
7440 cmp = simple_cst_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2));
7441 if (cmp <= 0)
7442 return cmp;
7443 if (call_expr_nargs (t1) != call_expr_nargs (t2))
7444 return 0;
7446 const_tree arg1, arg2;
7447 const_call_expr_arg_iterator iter1, iter2;
7448 for (arg1 = first_const_call_expr_arg (t1, &iter1),
7449 arg2 = first_const_call_expr_arg (t2, &iter2);
7450 arg1 && arg2;
7451 arg1 = next_const_call_expr_arg (&iter1),
7452 arg2 = next_const_call_expr_arg (&iter2))
7454 cmp = simple_cst_equal (arg1, arg2);
7455 if (cmp <= 0)
7456 return cmp;
7458 return arg1 == arg2;
7461 case TARGET_EXPR:
7462 /* Special case: if either target is an unallocated VAR_DECL,
7463 it means that it's going to be unified with whatever the
7464 TARGET_EXPR is really supposed to initialize, so treat it
7465 as being equivalent to anything. */
7466 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
7467 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
7468 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
7469 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
7470 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
7471 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
7472 cmp = 1;
7473 else
7474 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7476 if (cmp <= 0)
7477 return cmp;
7479 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
7481 case WITH_CLEANUP_EXPR:
7482 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7483 if (cmp <= 0)
7484 return cmp;
7486 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
7488 case COMPONENT_REF:
7489 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
7490 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
7492 return 0;
7494 case VAR_DECL:
7495 case PARM_DECL:
7496 case CONST_DECL:
7497 case FUNCTION_DECL:
7498 return 0;
7500 default:
7501 break;
7504 /* This general rule works for most tree codes. All exceptions should be
7505 handled above. If this is a language-specific tree code, we can't
7506 trust what might be in the operand, so say we don't know
7507 the situation. */
7508 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
7509 return -1;
7511 switch (TREE_CODE_CLASS (code1))
7513 case tcc_unary:
7514 case tcc_binary:
7515 case tcc_comparison:
7516 case tcc_expression:
7517 case tcc_reference:
7518 case tcc_statement:
7519 cmp = 1;
7520 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
7522 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
7523 if (cmp <= 0)
7524 return cmp;
7527 return cmp;
7529 default:
7530 return -1;
7534 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
7535 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
7536 than U, respectively. */
7539 compare_tree_int (const_tree t, unsigned HOST_WIDE_INT u)
7541 if (tree_int_cst_sgn (t) < 0)
7542 return -1;
7543 else if (!tree_fits_uhwi_p (t))
7544 return 1;
7545 else if (TREE_INT_CST_LOW (t) == u)
7546 return 0;
7547 else if (TREE_INT_CST_LOW (t) < u)
7548 return -1;
7549 else
7550 return 1;
7553 /* Return true if SIZE represents a constant size that is in bounds of
7554 what the middle-end and the backend accepts (covering not more than
7555 half of the address-space). */
7557 bool
7558 valid_constant_size_p (const_tree size)
7560 if (! tree_fits_uhwi_p (size)
7561 || TREE_OVERFLOW (size)
7562 || tree_int_cst_sign_bit (size) != 0)
7563 return false;
7564 return true;
7567 /* Return the precision of the type, or for a complex or vector type the
7568 precision of the type of its elements. */
7570 unsigned int
7571 element_precision (const_tree type)
7573 if (!TYPE_P (type))
7574 type = TREE_TYPE (type);
7575 enum tree_code code = TREE_CODE (type);
7576 if (code == COMPLEX_TYPE || code == VECTOR_TYPE)
7577 type = TREE_TYPE (type);
7579 return TYPE_PRECISION (type);
7582 /* Return true if CODE represents an associative tree code. Otherwise
7583 return false. */
7584 bool
7585 associative_tree_code (enum tree_code code)
7587 switch (code)
7589 case BIT_IOR_EXPR:
7590 case BIT_AND_EXPR:
7591 case BIT_XOR_EXPR:
7592 case PLUS_EXPR:
7593 case MULT_EXPR:
7594 case MIN_EXPR:
7595 case MAX_EXPR:
7596 return true;
7598 default:
7599 break;
7601 return false;
7604 /* Return true if CODE represents a commutative tree code. Otherwise
7605 return false. */
7606 bool
7607 commutative_tree_code (enum tree_code code)
7609 switch (code)
7611 case PLUS_EXPR:
7612 case MULT_EXPR:
7613 case MULT_HIGHPART_EXPR:
7614 case MIN_EXPR:
7615 case MAX_EXPR:
7616 case BIT_IOR_EXPR:
7617 case BIT_XOR_EXPR:
7618 case BIT_AND_EXPR:
7619 case NE_EXPR:
7620 case EQ_EXPR:
7621 case UNORDERED_EXPR:
7622 case ORDERED_EXPR:
7623 case UNEQ_EXPR:
7624 case LTGT_EXPR:
7625 case TRUTH_AND_EXPR:
7626 case TRUTH_XOR_EXPR:
7627 case TRUTH_OR_EXPR:
7628 case WIDEN_MULT_EXPR:
7629 case VEC_WIDEN_MULT_HI_EXPR:
7630 case VEC_WIDEN_MULT_LO_EXPR:
7631 case VEC_WIDEN_MULT_EVEN_EXPR:
7632 case VEC_WIDEN_MULT_ODD_EXPR:
7633 return true;
7635 default:
7636 break;
7638 return false;
7641 /* Return true if CODE represents a ternary tree code for which the
7642 first two operands are commutative. Otherwise return false. */
7643 bool
7644 commutative_ternary_tree_code (enum tree_code code)
7646 switch (code)
7648 case WIDEN_MULT_PLUS_EXPR:
7649 case WIDEN_MULT_MINUS_EXPR:
7650 case DOT_PROD_EXPR:
7651 case FMA_EXPR:
7652 return true;
7654 default:
7655 break;
7657 return false;
7660 /* Returns true if CODE can overflow. */
7662 bool
7663 operation_can_overflow (enum tree_code code)
7665 switch (code)
7667 case PLUS_EXPR:
7668 case MINUS_EXPR:
7669 case MULT_EXPR:
7670 case LSHIFT_EXPR:
7671 /* Can overflow in various ways. */
7672 return true;
7673 case TRUNC_DIV_EXPR:
7674 case EXACT_DIV_EXPR:
7675 case FLOOR_DIV_EXPR:
7676 case CEIL_DIV_EXPR:
7677 /* For INT_MIN / -1. */
7678 return true;
7679 case NEGATE_EXPR:
7680 case ABS_EXPR:
7681 /* For -INT_MIN. */
7682 return true;
7683 default:
7684 /* These operators cannot overflow. */
7685 return false;
7689 /* Returns true if CODE operating on operands of type TYPE doesn't overflow, or
7690 ftrapv doesn't generate trapping insns for CODE. */
7692 bool
7693 operation_no_trapping_overflow (tree type, enum tree_code code)
7695 gcc_checking_assert (ANY_INTEGRAL_TYPE_P (type));
7697 /* We don't generate instructions that trap on overflow for complex or vector
7698 types. */
7699 if (!INTEGRAL_TYPE_P (type))
7700 return true;
7702 if (!TYPE_OVERFLOW_TRAPS (type))
7703 return true;
7705 switch (code)
7707 case PLUS_EXPR:
7708 case MINUS_EXPR:
7709 case MULT_EXPR:
7710 case NEGATE_EXPR:
7711 case ABS_EXPR:
7712 /* These operators can overflow, and -ftrapv generates trapping code for
7713 these. */
7714 return false;
7715 case TRUNC_DIV_EXPR:
7716 case EXACT_DIV_EXPR:
7717 case FLOOR_DIV_EXPR:
7718 case CEIL_DIV_EXPR:
7719 case LSHIFT_EXPR:
7720 /* These operators can overflow, but -ftrapv does not generate trapping
7721 code for these. */
7722 return true;
7723 default:
7724 /* These operators cannot overflow. */
7725 return true;
7729 namespace inchash
7732 /* Generate a hash value for an expression. This can be used iteratively
7733 by passing a previous result as the HSTATE argument.
7735 This function is intended to produce the same hash for expressions which
7736 would compare equal using operand_equal_p. */
7737 void
7738 add_expr (const_tree t, inchash::hash &hstate)
7740 int i;
7741 enum tree_code code;
7742 enum tree_code_class tclass;
7744 if (t == NULL_TREE)
7746 hstate.merge_hash (0);
7747 return;
7750 code = TREE_CODE (t);
7752 switch (code)
7754 /* Alas, constants aren't shared, so we can't rely on pointer
7755 identity. */
7756 case VOID_CST:
7757 hstate.merge_hash (0);
7758 return;
7759 case INTEGER_CST:
7760 for (i = 0; i < TREE_INT_CST_NUNITS (t); i++)
7761 hstate.add_wide_int (TREE_INT_CST_ELT (t, i));
7762 return;
7763 case REAL_CST:
7765 unsigned int val2 = real_hash (TREE_REAL_CST_PTR (t));
7766 hstate.merge_hash (val2);
7767 return;
7769 case FIXED_CST:
7771 unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
7772 hstate.merge_hash (val2);
7773 return;
7775 case STRING_CST:
7776 hstate.add ((const void *) TREE_STRING_POINTER (t), TREE_STRING_LENGTH (t));
7777 return;
7778 case COMPLEX_CST:
7779 inchash::add_expr (TREE_REALPART (t), hstate);
7780 inchash::add_expr (TREE_IMAGPART (t), hstate);
7781 return;
7782 case VECTOR_CST:
7784 unsigned i;
7785 for (i = 0; i < VECTOR_CST_NELTS (t); ++i)
7786 inchash::add_expr (VECTOR_CST_ELT (t, i), hstate);
7787 return;
7789 case SSA_NAME:
7790 /* We can just compare by pointer. */
7791 hstate.add_wide_int (SSA_NAME_VERSION (t));
7792 return;
7793 case PLACEHOLDER_EXPR:
7794 /* The node itself doesn't matter. */
7795 return;
7796 case TREE_LIST:
7797 /* A list of expressions, for a CALL_EXPR or as the elements of a
7798 VECTOR_CST. */
7799 for (; t; t = TREE_CHAIN (t))
7800 inchash::add_expr (TREE_VALUE (t), hstate);
7801 return;
7802 case CONSTRUCTOR:
7804 unsigned HOST_WIDE_INT idx;
7805 tree field, value;
7806 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
7808 inchash::add_expr (field, hstate);
7809 inchash::add_expr (value, hstate);
7811 return;
7813 case FUNCTION_DECL:
7814 /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
7815 Otherwise nodes that compare equal according to operand_equal_p might
7816 get different hash codes. However, don't do this for machine specific
7817 or front end builtins, since the function code is overloaded in those
7818 cases. */
7819 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
7820 && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
7822 t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
7823 code = TREE_CODE (t);
7825 /* FALL THROUGH */
7826 default:
7827 tclass = TREE_CODE_CLASS (code);
7829 if (tclass == tcc_declaration)
7831 /* DECL's have a unique ID */
7832 hstate.add_wide_int (DECL_UID (t));
7834 else
7836 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
7838 hstate.add_object (code);
7840 /* Don't hash the type, that can lead to having nodes which
7841 compare equal according to operand_equal_p, but which
7842 have different hash codes. */
7843 if (CONVERT_EXPR_CODE_P (code)
7844 || code == NON_LVALUE_EXPR)
7846 /* Make sure to include signness in the hash computation. */
7847 hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
7848 inchash::add_expr (TREE_OPERAND (t, 0), hstate);
7851 else if (commutative_tree_code (code))
7853 /* It's a commutative expression. We want to hash it the same
7854 however it appears. We do this by first hashing both operands
7855 and then rehashing based on the order of their independent
7856 hashes. */
7857 inchash::hash one, two;
7858 inchash::add_expr (TREE_OPERAND (t, 0), one);
7859 inchash::add_expr (TREE_OPERAND (t, 1), two);
7860 hstate.add_commutative (one, two);
7862 else
7863 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
7864 inchash::add_expr (TREE_OPERAND (t, i), hstate);
7866 return;
7872 /* Constructors for pointer, array and function types.
7873 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
7874 constructed by language-dependent code, not here.) */
7876 /* Construct, lay out and return the type of pointers to TO_TYPE with
7877 mode MODE. If CAN_ALIAS_ALL is TRUE, indicate this type can
7878 reference all of memory. If such a type has already been
7879 constructed, reuse it. */
7881 tree
7882 build_pointer_type_for_mode (tree to_type, machine_mode mode,
7883 bool can_alias_all)
7885 tree t;
7886 bool could_alias = can_alias_all;
7888 if (to_type == error_mark_node)
7889 return error_mark_node;
7891 /* If the pointed-to type has the may_alias attribute set, force
7892 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7893 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7894 can_alias_all = true;
7896 /* In some cases, languages will have things that aren't a POINTER_TYPE
7897 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_POINTER_TO.
7898 In that case, return that type without regard to the rest of our
7899 operands.
7901 ??? This is a kludge, but consistent with the way this function has
7902 always operated and there doesn't seem to be a good way to avoid this
7903 at the moment. */
7904 if (TYPE_POINTER_TO (to_type) != 0
7905 && TREE_CODE (TYPE_POINTER_TO (to_type)) != POINTER_TYPE)
7906 return TYPE_POINTER_TO (to_type);
7908 /* First, if we already have a type for pointers to TO_TYPE and it's
7909 the proper mode, use it. */
7910 for (t = TYPE_POINTER_TO (to_type); t; t = TYPE_NEXT_PTR_TO (t))
7911 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7912 return t;
7914 t = make_node (POINTER_TYPE);
7916 TREE_TYPE (t) = to_type;
7917 SET_TYPE_MODE (t, mode);
7918 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7919 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (to_type);
7920 TYPE_POINTER_TO (to_type) = t;
7922 /* During LTO we do not set TYPE_CANONICAL of pointers and references. */
7923 if (TYPE_STRUCTURAL_EQUALITY_P (to_type) || in_lto_p)
7924 SET_TYPE_STRUCTURAL_EQUALITY (t);
7925 else if (TYPE_CANONICAL (to_type) != to_type || could_alias)
7926 TYPE_CANONICAL (t)
7927 = build_pointer_type_for_mode (TYPE_CANONICAL (to_type),
7928 mode, false);
7930 /* Lay out the type. This function has many callers that are concerned
7931 with expression-construction, and this simplifies them all. */
7932 layout_type (t);
7934 return t;
7937 /* By default build pointers in ptr_mode. */
7939 tree
7940 build_pointer_type (tree to_type)
7942 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
7943 : TYPE_ADDR_SPACE (to_type);
7944 machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
7945 return build_pointer_type_for_mode (to_type, pointer_mode, false);
7948 /* Same as build_pointer_type_for_mode, but for REFERENCE_TYPE. */
7950 tree
7951 build_reference_type_for_mode (tree to_type, machine_mode mode,
7952 bool can_alias_all)
7954 tree t;
7955 bool could_alias = can_alias_all;
7957 if (to_type == error_mark_node)
7958 return error_mark_node;
7960 /* If the pointed-to type has the may_alias attribute set, force
7961 a TYPE_REF_CAN_ALIAS_ALL pointer to be generated. */
7962 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (to_type)))
7963 can_alias_all = true;
7965 /* In some cases, languages will have things that aren't a REFERENCE_TYPE
7966 (such as a RECORD_TYPE for fat pointers in Ada) as TYPE_REFERENCE_TO.
7967 In that case, return that type without regard to the rest of our
7968 operands.
7970 ??? This is a kludge, but consistent with the way this function has
7971 always operated and there doesn't seem to be a good way to avoid this
7972 at the moment. */
7973 if (TYPE_REFERENCE_TO (to_type) != 0
7974 && TREE_CODE (TYPE_REFERENCE_TO (to_type)) != REFERENCE_TYPE)
7975 return TYPE_REFERENCE_TO (to_type);
7977 /* First, if we already have a type for pointers to TO_TYPE and it's
7978 the proper mode, use it. */
7979 for (t = TYPE_REFERENCE_TO (to_type); t; t = TYPE_NEXT_REF_TO (t))
7980 if (TYPE_MODE (t) == mode && TYPE_REF_CAN_ALIAS_ALL (t) == can_alias_all)
7981 return t;
7983 t = make_node (REFERENCE_TYPE);
7985 TREE_TYPE (t) = to_type;
7986 SET_TYPE_MODE (t, mode);
7987 TYPE_REF_CAN_ALIAS_ALL (t) = can_alias_all;
7988 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (to_type);
7989 TYPE_REFERENCE_TO (to_type) = t;
7991 /* During LTO we do not set TYPE_CANONICAL of pointers and references. */
7992 if (TYPE_STRUCTURAL_EQUALITY_P (to_type) || in_lto_p)
7993 SET_TYPE_STRUCTURAL_EQUALITY (t);
7994 else if (TYPE_CANONICAL (to_type) != to_type || could_alias)
7995 TYPE_CANONICAL (t)
7996 = build_reference_type_for_mode (TYPE_CANONICAL (to_type),
7997 mode, false);
7999 layout_type (t);
8001 return t;
8005 /* Build the node for the type of references-to-TO_TYPE by default
8006 in ptr_mode. */
8008 tree
8009 build_reference_type (tree to_type)
8011 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
8012 : TYPE_ADDR_SPACE (to_type);
8013 machine_mode pointer_mode = targetm.addr_space.pointer_mode (as);
8014 return build_reference_type_for_mode (to_type, pointer_mode, false);
8017 #define MAX_INT_CACHED_PREC \
8018 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
8019 static GTY(()) tree nonstandard_integer_type_cache[2 * MAX_INT_CACHED_PREC + 2];
8021 /* Builds a signed or unsigned integer type of precision PRECISION.
8022 Used for C bitfields whose precision does not match that of
8023 built-in target types. */
8024 tree
8025 build_nonstandard_integer_type (unsigned HOST_WIDE_INT precision,
8026 int unsignedp)
8028 tree itype, ret;
8030 if (unsignedp)
8031 unsignedp = MAX_INT_CACHED_PREC + 1;
8033 if (precision <= MAX_INT_CACHED_PREC)
8035 itype = nonstandard_integer_type_cache[precision + unsignedp];
8036 if (itype)
8037 return itype;
8040 itype = make_node (INTEGER_TYPE);
8041 TYPE_PRECISION (itype) = precision;
8043 if (unsignedp)
8044 fixup_unsigned_type (itype);
8045 else
8046 fixup_signed_type (itype);
8048 ret = itype;
8049 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (itype)))
8050 ret = type_hash_canon (tree_to_uhwi (TYPE_MAX_VALUE (itype)), itype);
8051 if (precision <= MAX_INT_CACHED_PREC)
8052 nonstandard_integer_type_cache[precision + unsignedp] = ret;
8054 return ret;
8057 #define MAX_BOOL_CACHED_PREC \
8058 (HOST_BITS_PER_WIDE_INT > 64 ? HOST_BITS_PER_WIDE_INT : 64)
8059 static GTY(()) tree nonstandard_boolean_type_cache[MAX_BOOL_CACHED_PREC + 1];
8061 /* Builds a boolean type of precision PRECISION.
8062 Used for boolean vectors to choose proper vector element size. */
8063 tree
8064 build_nonstandard_boolean_type (unsigned HOST_WIDE_INT precision)
8066 tree type;
8068 if (precision <= MAX_BOOL_CACHED_PREC)
8070 type = nonstandard_boolean_type_cache[precision];
8071 if (type)
8072 return type;
8075 type = make_node (BOOLEAN_TYPE);
8076 TYPE_PRECISION (type) = precision;
8077 fixup_signed_type (type);
8079 if (precision <= MAX_INT_CACHED_PREC)
8080 nonstandard_boolean_type_cache[precision] = type;
8082 return type;
8085 /* Create a range of some discrete type TYPE (an INTEGER_TYPE, ENUMERAL_TYPE
8086 or BOOLEAN_TYPE) with low bound LOWVAL and high bound HIGHVAL. If SHARED
8087 is true, reuse such a type that has already been constructed. */
8089 static tree
8090 build_range_type_1 (tree type, tree lowval, tree highval, bool shared)
8092 tree itype = make_node (INTEGER_TYPE);
8093 inchash::hash hstate;
8095 TREE_TYPE (itype) = type;
8097 TYPE_MIN_VALUE (itype) = fold_convert (type, lowval);
8098 TYPE_MAX_VALUE (itype) = highval ? fold_convert (type, highval) : NULL;
8100 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
8101 SET_TYPE_MODE (itype, TYPE_MODE (type));
8102 TYPE_SIZE (itype) = TYPE_SIZE (type);
8103 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
8104 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
8105 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
8107 if (!shared)
8108 return itype;
8110 if ((TYPE_MIN_VALUE (itype)
8111 && TREE_CODE (TYPE_MIN_VALUE (itype)) != INTEGER_CST)
8112 || (TYPE_MAX_VALUE (itype)
8113 && TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST))
8115 /* Since we cannot reliably merge this type, we need to compare it using
8116 structural equality checks. */
8117 SET_TYPE_STRUCTURAL_EQUALITY (itype);
8118 return itype;
8121 inchash::add_expr (TYPE_MIN_VALUE (itype), hstate);
8122 inchash::add_expr (TYPE_MAX_VALUE (itype), hstate);
8123 hstate.merge_hash (TYPE_HASH (type));
8124 itype = type_hash_canon (hstate.end (), itype);
8126 return itype;
8129 /* Wrapper around build_range_type_1 with SHARED set to true. */
8131 tree
8132 build_range_type (tree type, tree lowval, tree highval)
8134 return build_range_type_1 (type, lowval, highval, true);
8137 /* Wrapper around build_range_type_1 with SHARED set to false. */
8139 tree
8140 build_nonshared_range_type (tree type, tree lowval, tree highval)
8142 return build_range_type_1 (type, lowval, highval, false);
8145 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
8146 MAXVAL should be the maximum value in the domain
8147 (one less than the length of the array).
8149 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
8150 We don't enforce this limit, that is up to caller (e.g. language front end).
8151 The limit exists because the result is a signed type and we don't handle
8152 sizes that use more than one HOST_WIDE_INT. */
8154 tree
8155 build_index_type (tree maxval)
8157 return build_range_type (sizetype, size_zero_node, maxval);
8160 /* Return true if the debug information for TYPE, a subtype, should be emitted
8161 as a subrange type. If so, set LOWVAL to the low bound and HIGHVAL to the
8162 high bound, respectively. Sometimes doing so unnecessarily obfuscates the
8163 debug info and doesn't reflect the source code. */
8165 bool
8166 subrange_type_for_debug_p (const_tree type, tree *lowval, tree *highval)
8168 tree base_type = TREE_TYPE (type), low, high;
8170 /* Subrange types have a base type which is an integral type. */
8171 if (!INTEGRAL_TYPE_P (base_type))
8172 return false;
8174 /* Get the real bounds of the subtype. */
8175 if (lang_hooks.types.get_subrange_bounds)
8176 lang_hooks.types.get_subrange_bounds (type, &low, &high);
8177 else
8179 low = TYPE_MIN_VALUE (type);
8180 high = TYPE_MAX_VALUE (type);
8183 /* If the type and its base type have the same representation and the same
8184 name, then the type is not a subrange but a copy of the base type. */
8185 if ((TREE_CODE (base_type) == INTEGER_TYPE
8186 || TREE_CODE (base_type) == BOOLEAN_TYPE)
8187 && int_size_in_bytes (type) == int_size_in_bytes (base_type)
8188 && tree_int_cst_equal (low, TYPE_MIN_VALUE (base_type))
8189 && tree_int_cst_equal (high, TYPE_MAX_VALUE (base_type))
8190 && TYPE_IDENTIFIER (type) == TYPE_IDENTIFIER (base_type))
8191 return false;
8193 if (lowval)
8194 *lowval = low;
8195 if (highval)
8196 *highval = high;
8197 return true;
8200 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
8201 and number of elements specified by the range of values of INDEX_TYPE.
8202 If SHARED is true, reuse such a type that has already been constructed. */
8204 static tree
8205 build_array_type_1 (tree elt_type, tree index_type, bool shared)
8207 tree t;
8209 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
8211 error ("arrays of functions are not meaningful");
8212 elt_type = integer_type_node;
8215 t = make_node (ARRAY_TYPE);
8216 TREE_TYPE (t) = elt_type;
8217 TYPE_DOMAIN (t) = index_type;
8218 TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (elt_type);
8219 layout_type (t);
8221 /* If the element type is incomplete at this point we get marked for
8222 structural equality. Do not record these types in the canonical
8223 type hashtable. */
8224 if (TYPE_STRUCTURAL_EQUALITY_P (t))
8225 return t;
8227 if (shared)
8229 inchash::hash hstate;
8230 hstate.add_object (TYPE_HASH (elt_type));
8231 if (index_type)
8232 hstate.add_object (TYPE_HASH (index_type));
8233 t = type_hash_canon (hstate.end (), t);
8236 if (TYPE_CANONICAL (t) == t)
8238 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
8239 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
8240 SET_TYPE_STRUCTURAL_EQUALITY (t);
8241 else if (TYPE_CANONICAL (elt_type) != elt_type
8242 || (index_type && TYPE_CANONICAL (index_type) != index_type))
8243 TYPE_CANONICAL (t)
8244 = build_array_type_1 (TYPE_CANONICAL (elt_type),
8245 index_type
8246 ? TYPE_CANONICAL (index_type) : NULL_TREE,
8247 shared);
8250 return t;
8253 /* Wrapper around build_array_type_1 with SHARED set to true. */
8255 tree
8256 build_array_type (tree elt_type, tree index_type)
8258 return build_array_type_1 (elt_type, index_type, true);
8261 /* Wrapper around build_array_type_1 with SHARED set to false. */
8263 tree
8264 build_nonshared_array_type (tree elt_type, tree index_type)
8266 return build_array_type_1 (elt_type, index_type, false);
8269 /* Return a representation of ELT_TYPE[NELTS], using indices of type
8270 sizetype. */
8272 tree
8273 build_array_type_nelts (tree elt_type, unsigned HOST_WIDE_INT nelts)
8275 return build_array_type (elt_type, build_index_type (size_int (nelts - 1)));
8278 /* Recursively examines the array elements of TYPE, until a non-array
8279 element type is found. */
8281 tree
8282 strip_array_types (tree type)
8284 while (TREE_CODE (type) == ARRAY_TYPE)
8285 type = TREE_TYPE (type);
8287 return type;
8290 /* Computes the canonical argument types from the argument type list
8291 ARGTYPES.
8293 Upon return, *ANY_STRUCTURAL_P will be true iff either it was true
8294 on entry to this function, or if any of the ARGTYPES are
8295 structural.
8297 Upon return, *ANY_NONCANONICAL_P will be true iff either it was
8298 true on entry to this function, or if any of the ARGTYPES are
8299 non-canonical.
8301 Returns a canonical argument list, which may be ARGTYPES when the
8302 canonical argument list is unneeded (i.e., *ANY_STRUCTURAL_P is
8303 true) or would not differ from ARGTYPES. */
8305 static tree
8306 maybe_canonicalize_argtypes (tree argtypes,
8307 bool *any_structural_p,
8308 bool *any_noncanonical_p)
8310 tree arg;
8311 bool any_noncanonical_argtypes_p = false;
8313 for (arg = argtypes; arg && !(*any_structural_p); arg = TREE_CHAIN (arg))
8315 if (!TREE_VALUE (arg) || TREE_VALUE (arg) == error_mark_node)
8316 /* Fail gracefully by stating that the type is structural. */
8317 *any_structural_p = true;
8318 else if (TYPE_STRUCTURAL_EQUALITY_P (TREE_VALUE (arg)))
8319 *any_structural_p = true;
8320 else if (TYPE_CANONICAL (TREE_VALUE (arg)) != TREE_VALUE (arg)
8321 || TREE_PURPOSE (arg))
8322 /* If the argument has a default argument, we consider it
8323 non-canonical even though the type itself is canonical.
8324 That way, different variants of function and method types
8325 with default arguments will all point to the variant with
8326 no defaults as their canonical type. */
8327 any_noncanonical_argtypes_p = true;
8330 if (*any_structural_p)
8331 return argtypes;
8333 if (any_noncanonical_argtypes_p)
8335 /* Build the canonical list of argument types. */
8336 tree canon_argtypes = NULL_TREE;
8337 bool is_void = false;
8339 for (arg = argtypes; arg; arg = TREE_CHAIN (arg))
8341 if (arg == void_list_node)
8342 is_void = true;
8343 else
8344 canon_argtypes = tree_cons (NULL_TREE,
8345 TYPE_CANONICAL (TREE_VALUE (arg)),
8346 canon_argtypes);
8349 canon_argtypes = nreverse (canon_argtypes);
8350 if (is_void)
8351 canon_argtypes = chainon (canon_argtypes, void_list_node);
8353 /* There is a non-canonical type. */
8354 *any_noncanonical_p = true;
8355 return canon_argtypes;
8358 /* The canonical argument types are the same as ARGTYPES. */
8359 return argtypes;
8362 /* Construct, lay out and return
8363 the type of functions returning type VALUE_TYPE
8364 given arguments of types ARG_TYPES.
8365 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
8366 are data type nodes for the arguments of the function.
8367 If such a type has already been constructed, reuse it. */
8369 tree
8370 build_function_type (tree value_type, tree arg_types)
8372 tree t;
8373 inchash::hash hstate;
8374 bool any_structural_p, any_noncanonical_p;
8375 tree canon_argtypes;
8377 if (TREE_CODE (value_type) == FUNCTION_TYPE)
8379 error ("function return type cannot be function");
8380 value_type = integer_type_node;
8383 /* Make a node of the sort we want. */
8384 t = make_node (FUNCTION_TYPE);
8385 TREE_TYPE (t) = value_type;
8386 TYPE_ARG_TYPES (t) = arg_types;
8388 /* If we already have such a type, use the old one. */
8389 hstate.add_object (TYPE_HASH (value_type));
8390 type_hash_list (arg_types, hstate);
8391 t = type_hash_canon (hstate.end (), t);
8393 /* Set up the canonical type. */
8394 any_structural_p = TYPE_STRUCTURAL_EQUALITY_P (value_type);
8395 any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type;
8396 canon_argtypes = maybe_canonicalize_argtypes (arg_types,
8397 &any_structural_p,
8398 &any_noncanonical_p);
8399 if (any_structural_p)
8400 SET_TYPE_STRUCTURAL_EQUALITY (t);
8401 else if (any_noncanonical_p)
8402 TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type),
8403 canon_argtypes);
8405 if (!COMPLETE_TYPE_P (t))
8406 layout_type (t);
8407 return t;
8410 /* Build a function type. The RETURN_TYPE is the type returned by the
8411 function. If VAARGS is set, no void_type_node is appended to the
8412 the list. ARGP must be always be terminated be a NULL_TREE. */
8414 static tree
8415 build_function_type_list_1 (bool vaargs, tree return_type, va_list argp)
8417 tree t, args, last;
8419 t = va_arg (argp, tree);
8420 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (argp, tree))
8421 args = tree_cons (NULL_TREE, t, args);
8423 if (vaargs)
8425 last = args;
8426 if (args != NULL_TREE)
8427 args = nreverse (args);
8428 gcc_assert (last != void_list_node);
8430 else if (args == NULL_TREE)
8431 args = void_list_node;
8432 else
8434 last = args;
8435 args = nreverse (args);
8436 TREE_CHAIN (last) = void_list_node;
8438 args = build_function_type (return_type, args);
8440 return args;
8443 /* Build a function type. The RETURN_TYPE is the type returned by the
8444 function. If additional arguments are provided, they are
8445 additional argument types. The list of argument types must always
8446 be terminated by NULL_TREE. */
8448 tree
8449 build_function_type_list (tree return_type, ...)
8451 tree args;
8452 va_list p;
8454 va_start (p, return_type);
8455 args = build_function_type_list_1 (false, return_type, p);
8456 va_end (p);
8457 return args;
8460 /* Build a variable argument function type. The RETURN_TYPE is the
8461 type returned by the function. If additional arguments are provided,
8462 they are additional argument types. The list of argument types must
8463 always be terminated by NULL_TREE. */
8465 tree
8466 build_varargs_function_type_list (tree return_type, ...)
8468 tree args;
8469 va_list p;
8471 va_start (p, return_type);
8472 args = build_function_type_list_1 (true, return_type, p);
8473 va_end (p);
8475 return args;
8478 /* Build a function type. RETURN_TYPE is the type returned by the
8479 function; VAARGS indicates whether the function takes varargs. The
8480 function takes N named arguments, the types of which are provided in
8481 ARG_TYPES. */
8483 static tree
8484 build_function_type_array_1 (bool vaargs, tree return_type, int n,
8485 tree *arg_types)
8487 int i;
8488 tree t = vaargs ? NULL_TREE : void_list_node;
8490 for (i = n - 1; i >= 0; i--)
8491 t = tree_cons (NULL_TREE, arg_types[i], t);
8493 return build_function_type (return_type, t);
8496 /* Build a function type. RETURN_TYPE is the type returned by the
8497 function. The function takes N named arguments, the types of which
8498 are provided in ARG_TYPES. */
8500 tree
8501 build_function_type_array (tree return_type, int n, tree *arg_types)
8503 return build_function_type_array_1 (false, return_type, n, arg_types);
8506 /* Build a variable argument function type. RETURN_TYPE is the type
8507 returned by the function. The function takes N named arguments, the
8508 types of which are provided in ARG_TYPES. */
8510 tree
8511 build_varargs_function_type_array (tree return_type, int n, tree *arg_types)
8513 return build_function_type_array_1 (true, return_type, n, arg_types);
8516 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
8517 and ARGTYPES (a TREE_LIST) are the return type and arguments types
8518 for the method. An implicit additional parameter (of type
8519 pointer-to-BASETYPE) is added to the ARGTYPES. */
8521 tree
8522 build_method_type_directly (tree basetype,
8523 tree rettype,
8524 tree argtypes)
8526 tree t;
8527 tree ptype;
8528 inchash::hash hstate;
8529 bool any_structural_p, any_noncanonical_p;
8530 tree canon_argtypes;
8532 /* Make a node of the sort we want. */
8533 t = make_node (METHOD_TYPE);
8535 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8536 TREE_TYPE (t) = rettype;
8537 ptype = build_pointer_type (basetype);
8539 /* The actual arglist for this function includes a "hidden" argument
8540 which is "this". Put it into the list of argument types. */
8541 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
8542 TYPE_ARG_TYPES (t) = argtypes;
8544 /* If we already have such a type, use the old one. */
8545 hstate.add_object (TYPE_HASH (basetype));
8546 hstate.add_object (TYPE_HASH (rettype));
8547 type_hash_list (argtypes, hstate);
8548 t = type_hash_canon (hstate.end (), t);
8550 /* Set up the canonical type. */
8551 any_structural_p
8552 = (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8553 || TYPE_STRUCTURAL_EQUALITY_P (rettype));
8554 any_noncanonical_p
8555 = (TYPE_CANONICAL (basetype) != basetype
8556 || TYPE_CANONICAL (rettype) != rettype);
8557 canon_argtypes = maybe_canonicalize_argtypes (TREE_CHAIN (argtypes),
8558 &any_structural_p,
8559 &any_noncanonical_p);
8560 if (any_structural_p)
8561 SET_TYPE_STRUCTURAL_EQUALITY (t);
8562 else if (any_noncanonical_p)
8563 TYPE_CANONICAL (t)
8564 = build_method_type_directly (TYPE_CANONICAL (basetype),
8565 TYPE_CANONICAL (rettype),
8566 canon_argtypes);
8567 if (!COMPLETE_TYPE_P (t))
8568 layout_type (t);
8570 return t;
8573 /* Construct, lay out and return the type of methods belonging to class
8574 BASETYPE and whose arguments and values are described by TYPE.
8575 If that type exists already, reuse it.
8576 TYPE must be a FUNCTION_TYPE node. */
8578 tree
8579 build_method_type (tree basetype, tree type)
8581 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8583 return build_method_type_directly (basetype,
8584 TREE_TYPE (type),
8585 TYPE_ARG_TYPES (type));
8588 /* Construct, lay out and return the type of offsets to a value
8589 of type TYPE, within an object of type BASETYPE.
8590 If a suitable offset type exists already, reuse it. */
8592 tree
8593 build_offset_type (tree basetype, tree type)
8595 tree t;
8596 inchash::hash hstate;
8598 /* Make a node of the sort we want. */
8599 t = make_node (OFFSET_TYPE);
8601 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
8602 TREE_TYPE (t) = type;
8604 /* If we already have such a type, use the old one. */
8605 hstate.add_object (TYPE_HASH (basetype));
8606 hstate.add_object (TYPE_HASH (type));
8607 t = type_hash_canon (hstate.end (), t);
8609 if (!COMPLETE_TYPE_P (t))
8610 layout_type (t);
8612 if (TYPE_CANONICAL (t) == t)
8614 if (TYPE_STRUCTURAL_EQUALITY_P (basetype)
8615 || TYPE_STRUCTURAL_EQUALITY_P (type))
8616 SET_TYPE_STRUCTURAL_EQUALITY (t);
8617 else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) != basetype
8618 || TYPE_CANONICAL (type) != type)
8619 TYPE_CANONICAL (t)
8620 = build_offset_type (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)),
8621 TYPE_CANONICAL (type));
8624 return t;
8627 /* Create a complex type whose components are COMPONENT_TYPE. */
8629 tree
8630 build_complex_type (tree component_type)
8632 tree t;
8633 inchash::hash hstate;
8635 gcc_assert (INTEGRAL_TYPE_P (component_type)
8636 || SCALAR_FLOAT_TYPE_P (component_type)
8637 || FIXED_POINT_TYPE_P (component_type));
8639 /* Make a node of the sort we want. */
8640 t = make_node (COMPLEX_TYPE);
8642 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
8644 /* If we already have such a type, use the old one. */
8645 hstate.add_object (TYPE_HASH (component_type));
8646 t = type_hash_canon (hstate.end (), t);
8648 if (!COMPLETE_TYPE_P (t))
8649 layout_type (t);
8651 if (TYPE_CANONICAL (t) == t)
8653 if (TYPE_STRUCTURAL_EQUALITY_P (component_type))
8654 SET_TYPE_STRUCTURAL_EQUALITY (t);
8655 else if (TYPE_CANONICAL (component_type) != component_type)
8656 TYPE_CANONICAL (t)
8657 = build_complex_type (TYPE_CANONICAL (component_type));
8660 /* We need to create a name, since complex is a fundamental type. */
8661 if (! TYPE_NAME (t))
8663 const char *name;
8664 if (component_type == char_type_node)
8665 name = "complex char";
8666 else if (component_type == signed_char_type_node)
8667 name = "complex signed char";
8668 else if (component_type == unsigned_char_type_node)
8669 name = "complex unsigned char";
8670 else if (component_type == short_integer_type_node)
8671 name = "complex short int";
8672 else if (component_type == short_unsigned_type_node)
8673 name = "complex short unsigned int";
8674 else if (component_type == integer_type_node)
8675 name = "complex int";
8676 else if (component_type == unsigned_type_node)
8677 name = "complex unsigned int";
8678 else if (component_type == long_integer_type_node)
8679 name = "complex long int";
8680 else if (component_type == long_unsigned_type_node)
8681 name = "complex long unsigned int";
8682 else if (component_type == long_long_integer_type_node)
8683 name = "complex long long int";
8684 else if (component_type == long_long_unsigned_type_node)
8685 name = "complex long long unsigned int";
8686 else
8687 name = 0;
8689 if (name != 0)
8690 TYPE_NAME (t) = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
8691 get_identifier (name), t);
8694 return build_qualified_type (t, TYPE_QUALS (component_type));
8697 /* If TYPE is a real or complex floating-point type and the target
8698 does not directly support arithmetic on TYPE then return the wider
8699 type to be used for arithmetic on TYPE. Otherwise, return
8700 NULL_TREE. */
8702 tree
8703 excess_precision_type (tree type)
8705 if (flag_excess_precision != EXCESS_PRECISION_FAST)
8707 int flt_eval_method = TARGET_FLT_EVAL_METHOD;
8708 switch (TREE_CODE (type))
8710 case REAL_TYPE:
8711 switch (flt_eval_method)
8713 case 1:
8714 if (TYPE_MODE (type) == TYPE_MODE (float_type_node))
8715 return double_type_node;
8716 break;
8717 case 2:
8718 if (TYPE_MODE (type) == TYPE_MODE (float_type_node)
8719 || TYPE_MODE (type) == TYPE_MODE (double_type_node))
8720 return long_double_type_node;
8721 break;
8722 default:
8723 gcc_unreachable ();
8725 break;
8726 case COMPLEX_TYPE:
8727 if (TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
8728 return NULL_TREE;
8729 switch (flt_eval_method)
8731 case 1:
8732 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node))
8733 return complex_double_type_node;
8734 break;
8735 case 2:
8736 if (TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (float_type_node)
8737 || (TYPE_MODE (TREE_TYPE (type))
8738 == TYPE_MODE (double_type_node)))
8739 return complex_long_double_type_node;
8740 break;
8741 default:
8742 gcc_unreachable ();
8744 break;
8745 default:
8746 break;
8749 return NULL_TREE;
8752 /* Return OP, stripped of any conversions to wider types as much as is safe.
8753 Converting the value back to OP's type makes a value equivalent to OP.
8755 If FOR_TYPE is nonzero, we return a value which, if converted to
8756 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
8758 OP must have integer, real or enumeral type. Pointers are not allowed!
8760 There are some cases where the obvious value we could return
8761 would regenerate to OP if converted to OP's type,
8762 but would not extend like OP to wider types.
8763 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
8764 For example, if OP is (unsigned short)(signed char)-1,
8765 we avoid returning (signed char)-1 if FOR_TYPE is int,
8766 even though extending that to an unsigned short would regenerate OP,
8767 since the result of extending (signed char)-1 to (int)
8768 is different from (int) OP. */
8770 tree
8771 get_unwidened (tree op, tree for_type)
8773 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
8774 tree type = TREE_TYPE (op);
8775 unsigned final_prec
8776 = TYPE_PRECISION (for_type != 0 ? for_type : type);
8777 int uns
8778 = (for_type != 0 && for_type != type
8779 && final_prec > TYPE_PRECISION (type)
8780 && TYPE_UNSIGNED (type));
8781 tree win = op;
8783 while (CONVERT_EXPR_P (op))
8785 int bitschange;
8787 /* TYPE_PRECISION on vector types has different meaning
8788 (TYPE_VECTOR_SUBPARTS) and casts from vectors are view conversions,
8789 so avoid them here. */
8790 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == VECTOR_TYPE)
8791 break;
8793 bitschange = TYPE_PRECISION (TREE_TYPE (op))
8794 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
8796 /* Truncations are many-one so cannot be removed.
8797 Unless we are later going to truncate down even farther. */
8798 if (bitschange < 0
8799 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
8800 break;
8802 /* See what's inside this conversion. If we decide to strip it,
8803 we will set WIN. */
8804 op = TREE_OPERAND (op, 0);
8806 /* If we have not stripped any zero-extensions (uns is 0),
8807 we can strip any kind of extension.
8808 If we have previously stripped a zero-extension,
8809 only zero-extensions can safely be stripped.
8810 Any extension can be stripped if the bits it would produce
8811 are all going to be discarded later by truncating to FOR_TYPE. */
8813 if (bitschange > 0)
8815 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
8816 win = op;
8817 /* TYPE_UNSIGNED says whether this is a zero-extension.
8818 Let's avoid computing it if it does not affect WIN
8819 and if UNS will not be needed again. */
8820 if ((uns
8821 || CONVERT_EXPR_P (op))
8822 && TYPE_UNSIGNED (TREE_TYPE (op)))
8824 uns = 1;
8825 win = op;
8830 /* If we finally reach a constant see if it fits in for_type and
8831 in that case convert it. */
8832 if (for_type
8833 && TREE_CODE (win) == INTEGER_CST
8834 && TREE_TYPE (win) != for_type
8835 && int_fits_type_p (win, for_type))
8836 win = fold_convert (for_type, win);
8838 return win;
8841 /* Return OP or a simpler expression for a narrower value
8842 which can be sign-extended or zero-extended to give back OP.
8843 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
8844 or 0 if the value should be sign-extended. */
8846 tree
8847 get_narrower (tree op, int *unsignedp_ptr)
8849 int uns = 0;
8850 int first = 1;
8851 tree win = op;
8852 bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
8854 while (TREE_CODE (op) == NOP_EXPR)
8856 int bitschange
8857 = (TYPE_PRECISION (TREE_TYPE (op))
8858 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
8860 /* Truncations are many-one so cannot be removed. */
8861 if (bitschange < 0)
8862 break;
8864 /* See what's inside this conversion. If we decide to strip it,
8865 we will set WIN. */
8867 if (bitschange > 0)
8869 op = TREE_OPERAND (op, 0);
8870 /* An extension: the outermost one can be stripped,
8871 but remember whether it is zero or sign extension. */
8872 if (first)
8873 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8874 /* Otherwise, if a sign extension has been stripped,
8875 only sign extensions can now be stripped;
8876 if a zero extension has been stripped, only zero-extensions. */
8877 else if (uns != TYPE_UNSIGNED (TREE_TYPE (op)))
8878 break;
8879 first = 0;
8881 else /* bitschange == 0 */
8883 /* A change in nominal type can always be stripped, but we must
8884 preserve the unsignedness. */
8885 if (first)
8886 uns = TYPE_UNSIGNED (TREE_TYPE (op));
8887 first = 0;
8888 op = TREE_OPERAND (op, 0);
8889 /* Keep trying to narrow, but don't assign op to win if it
8890 would turn an integral type into something else. */
8891 if (INTEGRAL_TYPE_P (TREE_TYPE (op)) != integral_p)
8892 continue;
8895 win = op;
8898 if (TREE_CODE (op) == COMPONENT_REF
8899 /* Since type_for_size always gives an integer type. */
8900 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
8901 && TREE_CODE (TREE_TYPE (op)) != FIXED_POINT_TYPE
8902 /* Ensure field is laid out already. */
8903 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
8904 && tree_fits_uhwi_p (DECL_SIZE (TREE_OPERAND (op, 1))))
8906 unsigned HOST_WIDE_INT innerprec
8907 = tree_to_uhwi (DECL_SIZE (TREE_OPERAND (op, 1)));
8908 int unsignedp = (DECL_UNSIGNED (TREE_OPERAND (op, 1))
8909 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
8910 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
8912 /* We can get this structure field in a narrower type that fits it,
8913 but the resulting extension to its nominal type (a fullword type)
8914 must satisfy the same conditions as for other extensions.
8916 Do this only for fields that are aligned (not bit-fields),
8917 because when bit-field insns will be used there is no
8918 advantage in doing this. */
8920 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
8921 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
8922 && (first || uns == DECL_UNSIGNED (TREE_OPERAND (op, 1)))
8923 && type != 0)
8925 if (first)
8926 uns = DECL_UNSIGNED (TREE_OPERAND (op, 1));
8927 win = fold_convert (type, op);
8931 *unsignedp_ptr = uns;
8932 return win;
8935 /* Returns true if integer constant C has a value that is permissible
8936 for type TYPE (an INTEGER_TYPE). */
8938 bool
8939 int_fits_type_p (const_tree c, const_tree type)
8941 tree type_low_bound, type_high_bound;
8942 bool ok_for_low_bound, ok_for_high_bound;
8943 signop sgn_c = TYPE_SIGN (TREE_TYPE (c));
8945 retry:
8946 type_low_bound = TYPE_MIN_VALUE (type);
8947 type_high_bound = TYPE_MAX_VALUE (type);
8949 /* If at least one bound of the type is a constant integer, we can check
8950 ourselves and maybe make a decision. If no such decision is possible, but
8951 this type is a subtype, try checking against that. Otherwise, use
8952 fits_to_tree_p, which checks against the precision.
8954 Compute the status for each possibly constant bound, and return if we see
8955 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
8956 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
8957 for "constant known to fit". */
8959 /* Check if c >= type_low_bound. */
8960 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
8962 if (tree_int_cst_lt (c, type_low_bound))
8963 return false;
8964 ok_for_low_bound = true;
8966 else
8967 ok_for_low_bound = false;
8969 /* Check if c <= type_high_bound. */
8970 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
8972 if (tree_int_cst_lt (type_high_bound, c))
8973 return false;
8974 ok_for_high_bound = true;
8976 else
8977 ok_for_high_bound = false;
8979 /* If the constant fits both bounds, the result is known. */
8980 if (ok_for_low_bound && ok_for_high_bound)
8981 return true;
8983 /* Perform some generic filtering which may allow making a decision
8984 even if the bounds are not constant. First, negative integers
8985 never fit in unsigned types, */
8986 if (TYPE_UNSIGNED (type) && sgn_c == SIGNED && wi::neg_p (c))
8987 return false;
8989 /* Second, narrower types always fit in wider ones. */
8990 if (TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (c)))
8991 return true;
8993 /* Third, unsigned integers with top bit set never fit signed types. */
8994 if (!TYPE_UNSIGNED (type) && sgn_c == UNSIGNED)
8996 int prec = GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (c))) - 1;
8997 if (prec < TYPE_PRECISION (TREE_TYPE (c)))
8999 /* When a tree_cst is converted to a wide-int, the precision
9000 is taken from the type. However, if the precision of the
9001 mode underneath the type is smaller than that, it is
9002 possible that the value will not fit. The test below
9003 fails if any bit is set between the sign bit of the
9004 underlying mode and the top bit of the type. */
9005 if (wi::ne_p (wi::zext (c, prec - 1), c))
9006 return false;
9008 else if (wi::neg_p (c))
9009 return false;
9012 /* If we haven't been able to decide at this point, there nothing more we
9013 can check ourselves here. Look at the base type if we have one and it
9014 has the same precision. */
9015 if (TREE_CODE (type) == INTEGER_TYPE
9016 && TREE_TYPE (type) != 0
9017 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (type)))
9019 type = TREE_TYPE (type);
9020 goto retry;
9023 /* Or to fits_to_tree_p, if nothing else. */
9024 return wi::fits_to_tree_p (c, type);
9027 /* Stores bounds of an integer TYPE in MIN and MAX. If TYPE has non-constant
9028 bounds or is a POINTER_TYPE, the maximum and/or minimum values that can be
9029 represented (assuming two's-complement arithmetic) within the bit
9030 precision of the type are returned instead. */
9032 void
9033 get_type_static_bounds (const_tree type, mpz_t min, mpz_t max)
9035 if (!POINTER_TYPE_P (type) && TYPE_MIN_VALUE (type)
9036 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
9037 wi::to_mpz (TYPE_MIN_VALUE (type), min, TYPE_SIGN (type));
9038 else
9040 if (TYPE_UNSIGNED (type))
9041 mpz_set_ui (min, 0);
9042 else
9044 wide_int mn = wi::min_value (TYPE_PRECISION (type), SIGNED);
9045 wi::to_mpz (mn, min, SIGNED);
9049 if (!POINTER_TYPE_P (type) && TYPE_MAX_VALUE (type)
9050 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST)
9051 wi::to_mpz (TYPE_MAX_VALUE (type), max, TYPE_SIGN (type));
9052 else
9054 wide_int mn = wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type));
9055 wi::to_mpz (mn, max, TYPE_SIGN (type));
9059 /* Return true if VAR is an automatic variable defined in function FN. */
9061 bool
9062 auto_var_in_fn_p (const_tree var, const_tree fn)
9064 return (DECL_P (var) && DECL_CONTEXT (var) == fn
9065 && ((((TREE_CODE (var) == VAR_DECL && ! DECL_EXTERNAL (var))
9066 || TREE_CODE (var) == PARM_DECL)
9067 && ! TREE_STATIC (var))
9068 || TREE_CODE (var) == LABEL_DECL
9069 || TREE_CODE (var) == RESULT_DECL));
9072 /* Subprogram of following function. Called by walk_tree.
9074 Return *TP if it is an automatic variable or parameter of the
9075 function passed in as DATA. */
9077 static tree
9078 find_var_from_fn (tree *tp, int *walk_subtrees, void *data)
9080 tree fn = (tree) data;
9082 if (TYPE_P (*tp))
9083 *walk_subtrees = 0;
9085 else if (DECL_P (*tp)
9086 && auto_var_in_fn_p (*tp, fn))
9087 return *tp;
9089 return NULL_TREE;
9092 /* Returns true if T is, contains, or refers to a type with variable
9093 size. For METHOD_TYPEs and FUNCTION_TYPEs we exclude the
9094 arguments, but not the return type. If FN is nonzero, only return
9095 true if a modifier of the type or position of FN is a variable or
9096 parameter inside FN.
9098 This concept is more general than that of C99 'variably modified types':
9099 in C99, a struct type is never variably modified because a VLA may not
9100 appear as a structure member. However, in GNU C code like:
9102 struct S { int i[f()]; };
9104 is valid, and other languages may define similar constructs. */
9106 bool
9107 variably_modified_type_p (tree type, tree fn)
9109 tree t;
9111 /* Test if T is either variable (if FN is zero) or an expression containing
9112 a variable in FN. If TYPE isn't gimplified, return true also if
9113 gimplify_one_sizepos would gimplify the expression into a local
9114 variable. */
9115 #define RETURN_TRUE_IF_VAR(T) \
9116 do { tree _t = (T); \
9117 if (_t != NULL_TREE \
9118 && _t != error_mark_node \
9119 && TREE_CODE (_t) != INTEGER_CST \
9120 && TREE_CODE (_t) != PLACEHOLDER_EXPR \
9121 && (!fn \
9122 || (!TYPE_SIZES_GIMPLIFIED (type) \
9123 && !is_gimple_sizepos (_t)) \
9124 || walk_tree (&_t, find_var_from_fn, fn, NULL))) \
9125 return true; } while (0)
9127 if (type == error_mark_node)
9128 return false;
9130 /* If TYPE itself has variable size, it is variably modified. */
9131 RETURN_TRUE_IF_VAR (TYPE_SIZE (type));
9132 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (type));
9134 switch (TREE_CODE (type))
9136 case POINTER_TYPE:
9137 case REFERENCE_TYPE:
9138 case VECTOR_TYPE:
9139 if (variably_modified_type_p (TREE_TYPE (type), fn))
9140 return true;
9141 break;
9143 case FUNCTION_TYPE:
9144 case METHOD_TYPE:
9145 /* If TYPE is a function type, it is variably modified if the
9146 return type is variably modified. */
9147 if (variably_modified_type_p (TREE_TYPE (type), fn))
9148 return true;
9149 break;
9151 case INTEGER_TYPE:
9152 case REAL_TYPE:
9153 case FIXED_POINT_TYPE:
9154 case ENUMERAL_TYPE:
9155 case BOOLEAN_TYPE:
9156 /* Scalar types are variably modified if their end points
9157 aren't constant. */
9158 RETURN_TRUE_IF_VAR (TYPE_MIN_VALUE (type));
9159 RETURN_TRUE_IF_VAR (TYPE_MAX_VALUE (type));
9160 break;
9162 case RECORD_TYPE:
9163 case UNION_TYPE:
9164 case QUAL_UNION_TYPE:
9165 /* We can't see if any of the fields are variably-modified by the
9166 definition we normally use, since that would produce infinite
9167 recursion via pointers. */
9168 /* This is variably modified if some field's type is. */
9169 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
9170 if (TREE_CODE (t) == FIELD_DECL)
9172 RETURN_TRUE_IF_VAR (DECL_FIELD_OFFSET (t));
9173 RETURN_TRUE_IF_VAR (DECL_SIZE (t));
9174 RETURN_TRUE_IF_VAR (DECL_SIZE_UNIT (t));
9176 if (TREE_CODE (type) == QUAL_UNION_TYPE)
9177 RETURN_TRUE_IF_VAR (DECL_QUALIFIER (t));
9179 break;
9181 case ARRAY_TYPE:
9182 /* Do not call ourselves to avoid infinite recursion. This is
9183 variably modified if the element type is. */
9184 RETURN_TRUE_IF_VAR (TYPE_SIZE (TREE_TYPE (type)));
9185 RETURN_TRUE_IF_VAR (TYPE_SIZE_UNIT (TREE_TYPE (type)));
9186 break;
9188 default:
9189 break;
9192 /* The current language may have other cases to check, but in general,
9193 all other types are not variably modified. */
9194 return lang_hooks.tree_inlining.var_mod_type_p (type, fn);
9196 #undef RETURN_TRUE_IF_VAR
9199 /* Given a DECL or TYPE, return the scope in which it was declared, or
9200 NULL_TREE if there is no containing scope. */
9202 tree
9203 get_containing_scope (const_tree t)
9205 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
9208 /* Return the innermost context enclosing DECL that is
9209 a FUNCTION_DECL, or zero if none. */
9211 tree
9212 decl_function_context (const_tree decl)
9214 tree context;
9216 if (TREE_CODE (decl) == ERROR_MARK)
9217 return 0;
9219 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
9220 where we look up the function at runtime. Such functions always take
9221 a first argument of type 'pointer to real context'.
9223 C++ should really be fixed to use DECL_CONTEXT for the real context,
9224 and use something else for the "virtual context". */
9225 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
9226 context
9227 = TYPE_MAIN_VARIANT
9228 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
9229 else
9230 context = DECL_CONTEXT (decl);
9232 while (context && TREE_CODE (context) != FUNCTION_DECL)
9234 if (TREE_CODE (context) == BLOCK)
9235 context = BLOCK_SUPERCONTEXT (context);
9236 else
9237 context = get_containing_scope (context);
9240 return context;
9243 /* Return the innermost context enclosing DECL that is
9244 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
9245 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
9247 tree
9248 decl_type_context (const_tree decl)
9250 tree context = DECL_CONTEXT (decl);
9252 while (context)
9253 switch (TREE_CODE (context))
9255 case NAMESPACE_DECL:
9256 case TRANSLATION_UNIT_DECL:
9257 return NULL_TREE;
9259 case RECORD_TYPE:
9260 case UNION_TYPE:
9261 case QUAL_UNION_TYPE:
9262 return context;
9264 case TYPE_DECL:
9265 case FUNCTION_DECL:
9266 context = DECL_CONTEXT (context);
9267 break;
9269 case BLOCK:
9270 context = BLOCK_SUPERCONTEXT (context);
9271 break;
9273 default:
9274 gcc_unreachable ();
9277 return NULL_TREE;
9280 /* CALL is a CALL_EXPR. Return the declaration for the function
9281 called, or NULL_TREE if the called function cannot be
9282 determined. */
9284 tree
9285 get_callee_fndecl (const_tree call)
9287 tree addr;
9289 if (call == error_mark_node)
9290 return error_mark_node;
9292 /* It's invalid to call this function with anything but a
9293 CALL_EXPR. */
9294 gcc_assert (TREE_CODE (call) == CALL_EXPR);
9296 /* The first operand to the CALL is the address of the function
9297 called. */
9298 addr = CALL_EXPR_FN (call);
9300 /* If there is no function, return early. */
9301 if (addr == NULL_TREE)
9302 return NULL_TREE;
9304 STRIP_NOPS (addr);
9306 /* If this is a readonly function pointer, extract its initial value. */
9307 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
9308 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
9309 && DECL_INITIAL (addr))
9310 addr = DECL_INITIAL (addr);
9312 /* If the address is just `&f' for some function `f', then we know
9313 that `f' is being called. */
9314 if (TREE_CODE (addr) == ADDR_EXPR
9315 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
9316 return TREE_OPERAND (addr, 0);
9318 /* We couldn't figure out what was being called. */
9319 return NULL_TREE;
9322 /* If CALL_EXPR CALL calls a normal built-in function or an internal function,
9323 return the associated function code, otherwise return CFN_LAST. */
9325 combined_fn
9326 get_call_combined_fn (const_tree call)
9328 /* It's invalid to call this function with anything but a CALL_EXPR. */
9329 gcc_assert (TREE_CODE (call) == CALL_EXPR);
9331 if (!CALL_EXPR_FN (call))
9332 return as_combined_fn (CALL_EXPR_IFN (call));
9334 tree fndecl = get_callee_fndecl (call);
9335 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
9336 return as_combined_fn (DECL_FUNCTION_CODE (fndecl));
9338 return CFN_LAST;
9341 #define TREE_MEM_USAGE_SPACES 40
9343 /* Print debugging information about tree nodes generated during the compile,
9344 and any language-specific information. */
9346 void
9347 dump_tree_statistics (void)
9349 if (GATHER_STATISTICS)
9351 int i;
9352 int total_nodes, total_bytes;
9353 fprintf (stderr, "\nKind Nodes Bytes\n");
9354 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
9355 total_nodes = total_bytes = 0;
9356 for (i = 0; i < (int) all_kinds; i++)
9358 fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
9359 tree_node_counts[i], tree_node_sizes[i]);
9360 total_nodes += tree_node_counts[i];
9361 total_bytes += tree_node_sizes[i];
9363 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
9364 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
9365 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
9366 fprintf (stderr, "Code Nodes\n");
9367 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
9368 for (i = 0; i < (int) MAX_TREE_CODES; i++)
9369 fprintf (stderr, "%-32s %7d\n", get_tree_code_name ((enum tree_code) i),
9370 tree_code_counts[i]);
9371 mem_usage::print_dash_line (TREE_MEM_USAGE_SPACES);
9372 fprintf (stderr, "\n");
9373 ssanames_print_statistics ();
9374 fprintf (stderr, "\n");
9375 phinodes_print_statistics ();
9376 fprintf (stderr, "\n");
9378 else
9379 fprintf (stderr, "(No per-node statistics)\n");
9381 print_type_hash_statistics ();
9382 print_debug_expr_statistics ();
9383 print_value_expr_statistics ();
9384 lang_hooks.print_statistics ();
9387 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
9389 /* Generate a crc32 of a byte. */
9391 static unsigned
9392 crc32_unsigned_bits (unsigned chksum, unsigned value, unsigned bits)
9394 unsigned ix;
9396 for (ix = bits; ix--; value <<= 1)
9398 unsigned feedback;
9400 feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
9401 chksum <<= 1;
9402 chksum ^= feedback;
9404 return chksum;
9407 /* Generate a crc32 of a 32-bit unsigned. */
9409 unsigned
9410 crc32_unsigned (unsigned chksum, unsigned value)
9412 return crc32_unsigned_bits (chksum, value, 32);
9415 /* Generate a crc32 of a byte. */
9417 unsigned
9418 crc32_byte (unsigned chksum, char byte)
9420 return crc32_unsigned_bits (chksum, (unsigned) byte << 24, 8);
9423 /* Generate a crc32 of a string. */
9425 unsigned
9426 crc32_string (unsigned chksum, const char *string)
9430 chksum = crc32_byte (chksum, *string);
9432 while (*string++);
9433 return chksum;
9436 /* P is a string that will be used in a symbol. Mask out any characters
9437 that are not valid in that context. */
9439 void
9440 clean_symbol_name (char *p)
9442 for (; *p; p++)
9443 if (! (ISALNUM (*p)
9444 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
9445 || *p == '$'
9446 #endif
9447 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
9448 || *p == '.'
9449 #endif
9451 *p = '_';
9454 /* For anonymous aggregate types, we need some sort of name to
9455 hold on to. In practice, this should not appear, but it should
9456 not be harmful if it does. */
9457 bool
9458 anon_aggrname_p(const_tree id_node)
9460 #ifndef NO_DOT_IN_LABEL
9461 return (IDENTIFIER_POINTER (id_node)[0] == '.'
9462 && IDENTIFIER_POINTER (id_node)[1] == '_');
9463 #else /* NO_DOT_IN_LABEL */
9464 #ifndef NO_DOLLAR_IN_LABEL
9465 return (IDENTIFIER_POINTER (id_node)[0] == '$' \
9466 && IDENTIFIER_POINTER (id_node)[1] == '_');
9467 #else /* NO_DOLLAR_IN_LABEL */
9468 #define ANON_AGGRNAME_PREFIX "__anon_"
9469 return (!strncmp (IDENTIFIER_POINTER (id_node), ANON_AGGRNAME_PREFIX,
9470 sizeof (ANON_AGGRNAME_PREFIX) - 1));
9471 #endif /* NO_DOLLAR_IN_LABEL */
9472 #endif /* NO_DOT_IN_LABEL */
9475 /* Return a format for an anonymous aggregate name. */
9476 const char *
9477 anon_aggrname_format()
9479 #ifndef NO_DOT_IN_LABEL
9480 return "._%d";
9481 #else /* NO_DOT_IN_LABEL */
9482 #ifndef NO_DOLLAR_IN_LABEL
9483 return "$_%d";
9484 #else /* NO_DOLLAR_IN_LABEL */
9485 return "__anon_%d";
9486 #endif /* NO_DOLLAR_IN_LABEL */
9487 #endif /* NO_DOT_IN_LABEL */
9490 /* Generate a name for a special-purpose function.
9491 The generated name may need to be unique across the whole link.
9492 Changes to this function may also require corresponding changes to
9493 xstrdup_mask_random.
9494 TYPE is some string to identify the purpose of this function to the
9495 linker or collect2; it must start with an uppercase letter,
9496 one of:
9497 I - for constructors
9498 D - for destructors
9499 N - for C++ anonymous namespaces
9500 F - for DWARF unwind frame information. */
9502 tree
9503 get_file_function_name (const char *type)
9505 char *buf;
9506 const char *p;
9507 char *q;
9509 /* If we already have a name we know to be unique, just use that. */
9510 if (first_global_object_name)
9511 p = q = ASTRDUP (first_global_object_name);
9512 /* If the target is handling the constructors/destructors, they
9513 will be local to this file and the name is only necessary for
9514 debugging purposes.
9515 We also assign sub_I and sub_D sufixes to constructors called from
9516 the global static constructors. These are always local. */
9517 else if (((type[0] == 'I' || type[0] == 'D') && targetm.have_ctors_dtors)
9518 || (strncmp (type, "sub_", 4) == 0
9519 && (type[4] == 'I' || type[4] == 'D')))
9521 const char *file = main_input_filename;
9522 if (! file)
9523 file = LOCATION_FILE (input_location);
9524 /* Just use the file's basename, because the full pathname
9525 might be quite long. */
9526 p = q = ASTRDUP (lbasename (file));
9528 else
9530 /* Otherwise, the name must be unique across the entire link.
9531 We don't have anything that we know to be unique to this translation
9532 unit, so use what we do have and throw in some randomness. */
9533 unsigned len;
9534 const char *name = weak_global_object_name;
9535 const char *file = main_input_filename;
9537 if (! name)
9538 name = "";
9539 if (! file)
9540 file = LOCATION_FILE (input_location);
9542 len = strlen (file);
9543 q = (char *) alloca (9 + 17 + len + 1);
9544 memcpy (q, file, len + 1);
9546 snprintf (q + len, 9 + 17 + 1, "_%08X_" HOST_WIDE_INT_PRINT_HEX,
9547 crc32_string (0, name), get_random_seed (false));
9549 p = q;
9552 clean_symbol_name (q);
9553 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
9554 + strlen (type));
9556 /* Set up the name of the file-level functions we may need.
9557 Use a global object (which is already required to be unique over
9558 the program) rather than the file name (which imposes extra
9559 constraints). */
9560 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
9562 return get_identifier (buf);
9565 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
9567 /* Complain that the tree code of NODE does not match the expected 0
9568 terminated list of trailing codes. The trailing code list can be
9569 empty, for a more vague error message. FILE, LINE, and FUNCTION
9570 are of the caller. */
9572 void
9573 tree_check_failed (const_tree node, const char *file,
9574 int line, const char *function, ...)
9576 va_list args;
9577 const char *buffer;
9578 unsigned length = 0;
9579 enum tree_code code;
9581 va_start (args, function);
9582 while ((code = (enum tree_code) va_arg (args, int)))
9583 length += 4 + strlen (get_tree_code_name (code));
9584 va_end (args);
9585 if (length)
9587 char *tmp;
9588 va_start (args, function);
9589 length += strlen ("expected ");
9590 buffer = tmp = (char *) alloca (length);
9591 length = 0;
9592 while ((code = (enum tree_code) va_arg (args, int)))
9594 const char *prefix = length ? " or " : "expected ";
9596 strcpy (tmp + length, prefix);
9597 length += strlen (prefix);
9598 strcpy (tmp + length, get_tree_code_name (code));
9599 length += strlen (get_tree_code_name (code));
9601 va_end (args);
9603 else
9604 buffer = "unexpected node";
9606 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9607 buffer, get_tree_code_name (TREE_CODE (node)),
9608 function, trim_filename (file), line);
9611 /* Complain that the tree code of NODE does match the expected 0
9612 terminated list of trailing codes. FILE, LINE, and FUNCTION are of
9613 the caller. */
9615 void
9616 tree_not_check_failed (const_tree node, const char *file,
9617 int line, const char *function, ...)
9619 va_list args;
9620 char *buffer;
9621 unsigned length = 0;
9622 enum tree_code code;
9624 va_start (args, function);
9625 while ((code = (enum tree_code) va_arg (args, int)))
9626 length += 4 + strlen (get_tree_code_name (code));
9627 va_end (args);
9628 va_start (args, function);
9629 buffer = (char *) alloca (length);
9630 length = 0;
9631 while ((code = (enum tree_code) va_arg (args, int)))
9633 if (length)
9635 strcpy (buffer + length, " or ");
9636 length += 4;
9638 strcpy (buffer + length, get_tree_code_name (code));
9639 length += strlen (get_tree_code_name (code));
9641 va_end (args);
9643 internal_error ("tree check: expected none of %s, have %s in %s, at %s:%d",
9644 buffer, get_tree_code_name (TREE_CODE (node)),
9645 function, trim_filename (file), line);
9648 /* Similar to tree_check_failed, except that we check for a class of tree
9649 code, given in CL. */
9651 void
9652 tree_class_check_failed (const_tree node, const enum tree_code_class cl,
9653 const char *file, int line, const char *function)
9655 internal_error
9656 ("tree check: expected class %qs, have %qs (%s) in %s, at %s:%d",
9657 TREE_CODE_CLASS_STRING (cl),
9658 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9659 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9662 /* Similar to tree_check_failed, except that instead of specifying a
9663 dozen codes, use the knowledge that they're all sequential. */
9665 void
9666 tree_range_check_failed (const_tree node, const char *file, int line,
9667 const char *function, enum tree_code c1,
9668 enum tree_code c2)
9670 char *buffer;
9671 unsigned length = 0;
9672 unsigned int c;
9674 for (c = c1; c <= c2; ++c)
9675 length += 4 + strlen (get_tree_code_name ((enum tree_code) c));
9677 length += strlen ("expected ");
9678 buffer = (char *) alloca (length);
9679 length = 0;
9681 for (c = c1; c <= c2; ++c)
9683 const char *prefix = length ? " or " : "expected ";
9685 strcpy (buffer + length, prefix);
9686 length += strlen (prefix);
9687 strcpy (buffer + length, get_tree_code_name ((enum tree_code) c));
9688 length += strlen (get_tree_code_name ((enum tree_code) c));
9691 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9692 buffer, get_tree_code_name (TREE_CODE (node)),
9693 function, trim_filename (file), line);
9697 /* Similar to tree_check_failed, except that we check that a tree does
9698 not have the specified code, given in CL. */
9700 void
9701 tree_not_class_check_failed (const_tree node, const enum tree_code_class cl,
9702 const char *file, int line, const char *function)
9704 internal_error
9705 ("tree check: did not expect class %qs, have %qs (%s) in %s, at %s:%d",
9706 TREE_CODE_CLASS_STRING (cl),
9707 TREE_CODE_CLASS_STRING (TREE_CODE_CLASS (TREE_CODE (node))),
9708 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9712 /* Similar to tree_check_failed but applied to OMP_CLAUSE codes. */
9714 void
9715 omp_clause_check_failed (const_tree node, const char *file, int line,
9716 const char *function, enum omp_clause_code code)
9718 internal_error ("tree check: expected omp_clause %s, have %s in %s, at %s:%d",
9719 omp_clause_code_name[code], get_tree_code_name (TREE_CODE (node)),
9720 function, trim_filename (file), line);
9724 /* Similar to tree_range_check_failed but applied to OMP_CLAUSE codes. */
9726 void
9727 omp_clause_range_check_failed (const_tree node, const char *file, int line,
9728 const char *function, enum omp_clause_code c1,
9729 enum omp_clause_code c2)
9731 char *buffer;
9732 unsigned length = 0;
9733 unsigned int c;
9735 for (c = c1; c <= c2; ++c)
9736 length += 4 + strlen (omp_clause_code_name[c]);
9738 length += strlen ("expected ");
9739 buffer = (char *) alloca (length);
9740 length = 0;
9742 for (c = c1; c <= c2; ++c)
9744 const char *prefix = length ? " or " : "expected ";
9746 strcpy (buffer + length, prefix);
9747 length += strlen (prefix);
9748 strcpy (buffer + length, omp_clause_code_name[c]);
9749 length += strlen (omp_clause_code_name[c]);
9752 internal_error ("tree check: %s, have %s in %s, at %s:%d",
9753 buffer, omp_clause_code_name[TREE_CODE (node)],
9754 function, trim_filename (file), line);
9758 #undef DEFTREESTRUCT
9759 #define DEFTREESTRUCT(VAL, NAME) NAME,
9761 static const char *ts_enum_names[] = {
9762 #include "treestruct.def"
9764 #undef DEFTREESTRUCT
9766 #define TS_ENUM_NAME(EN) (ts_enum_names[(EN)])
9768 /* Similar to tree_class_check_failed, except that we check for
9769 whether CODE contains the tree structure identified by EN. */
9771 void
9772 tree_contains_struct_check_failed (const_tree node,
9773 const enum tree_node_structure_enum en,
9774 const char *file, int line,
9775 const char *function)
9777 internal_error
9778 ("tree check: expected tree that contains %qs structure, have %qs in %s, at %s:%d",
9779 TS_ENUM_NAME (en),
9780 get_tree_code_name (TREE_CODE (node)), function, trim_filename (file), line);
9784 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9785 (dynamically sized) vector. */
9787 void
9788 tree_int_cst_elt_check_failed (int idx, int len, const char *file, int line,
9789 const char *function)
9791 internal_error
9792 ("tree check: accessed elt %d of tree_int_cst with %d elts in %s, at %s:%d",
9793 idx + 1, len, function, trim_filename (file), line);
9796 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
9797 (dynamically sized) vector. */
9799 void
9800 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
9801 const char *function)
9803 internal_error
9804 ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
9805 idx + 1, len, function, trim_filename (file), line);
9808 /* Similar to above, except that the check is for the bounds of the operand
9809 vector of an expression node EXP. */
9811 void
9812 tree_operand_check_failed (int idx, const_tree exp, const char *file,
9813 int line, const char *function)
9815 enum tree_code code = TREE_CODE (exp);
9816 internal_error
9817 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
9818 idx + 1, get_tree_code_name (code), TREE_OPERAND_LENGTH (exp),
9819 function, trim_filename (file), line);
9822 /* Similar to above, except that the check is for the number of
9823 operands of an OMP_CLAUSE node. */
9825 void
9826 omp_clause_operand_check_failed (int idx, const_tree t, const char *file,
9827 int line, const char *function)
9829 internal_error
9830 ("tree check: accessed operand %d of omp_clause %s with %d operands "
9831 "in %s, at %s:%d", idx + 1, omp_clause_code_name[OMP_CLAUSE_CODE (t)],
9832 omp_clause_num_ops [OMP_CLAUSE_CODE (t)], function,
9833 trim_filename (file), line);
9835 #endif /* ENABLE_TREE_CHECKING */
9837 /* Create a new vector type node holding SUBPARTS units of type INNERTYPE,
9838 and mapped to the machine mode MODE. Initialize its fields and build
9839 the information necessary for debugging output. */
9841 static tree
9842 make_vector_type (tree innertype, int nunits, machine_mode mode)
9844 tree t;
9845 inchash::hash hstate;
9847 t = make_node (VECTOR_TYPE);
9848 TREE_TYPE (t) = TYPE_MAIN_VARIANT (innertype);
9849 SET_TYPE_VECTOR_SUBPARTS (t, nunits);
9850 SET_TYPE_MODE (t, mode);
9852 if (TYPE_STRUCTURAL_EQUALITY_P (innertype))
9853 SET_TYPE_STRUCTURAL_EQUALITY (t);
9854 else if ((TYPE_CANONICAL (innertype) != innertype
9855 || mode != VOIDmode)
9856 && !VECTOR_BOOLEAN_TYPE_P (t))
9857 TYPE_CANONICAL (t)
9858 = make_vector_type (TYPE_CANONICAL (innertype), nunits, VOIDmode);
9860 layout_type (t);
9862 hstate.add_wide_int (VECTOR_TYPE);
9863 hstate.add_wide_int (nunits);
9864 hstate.add_wide_int (mode);
9865 hstate.add_object (TYPE_HASH (TREE_TYPE (t)));
9866 t = type_hash_canon (hstate.end (), t);
9868 /* We have built a main variant, based on the main variant of the
9869 inner type. Use it to build the variant we return. */
9870 if ((TYPE_ATTRIBUTES (innertype) || TYPE_QUALS (innertype))
9871 && TREE_TYPE (t) != innertype)
9872 return build_type_attribute_qual_variant (t,
9873 TYPE_ATTRIBUTES (innertype),
9874 TYPE_QUALS (innertype));
9876 return t;
9879 static tree
9880 make_or_reuse_type (unsigned size, int unsignedp)
9882 int i;
9884 if (size == INT_TYPE_SIZE)
9885 return unsignedp ? unsigned_type_node : integer_type_node;
9886 if (size == CHAR_TYPE_SIZE)
9887 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
9888 if (size == SHORT_TYPE_SIZE)
9889 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
9890 if (size == LONG_TYPE_SIZE)
9891 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
9892 if (size == LONG_LONG_TYPE_SIZE)
9893 return (unsignedp ? long_long_unsigned_type_node
9894 : long_long_integer_type_node);
9896 for (i = 0; i < NUM_INT_N_ENTS; i ++)
9897 if (size == int_n_data[i].bitsize
9898 && int_n_enabled_p[i])
9899 return (unsignedp ? int_n_trees[i].unsigned_type
9900 : int_n_trees[i].signed_type);
9902 if (unsignedp)
9903 return make_unsigned_type (size);
9904 else
9905 return make_signed_type (size);
9908 /* Create or reuse a fract type by SIZE, UNSIGNEDP, and SATP. */
9910 static tree
9911 make_or_reuse_fract_type (unsigned size, int unsignedp, int satp)
9913 if (satp)
9915 if (size == SHORT_FRACT_TYPE_SIZE)
9916 return unsignedp ? sat_unsigned_short_fract_type_node
9917 : sat_short_fract_type_node;
9918 if (size == FRACT_TYPE_SIZE)
9919 return unsignedp ? sat_unsigned_fract_type_node : sat_fract_type_node;
9920 if (size == LONG_FRACT_TYPE_SIZE)
9921 return unsignedp ? sat_unsigned_long_fract_type_node
9922 : sat_long_fract_type_node;
9923 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9924 return unsignedp ? sat_unsigned_long_long_fract_type_node
9925 : sat_long_long_fract_type_node;
9927 else
9929 if (size == SHORT_FRACT_TYPE_SIZE)
9930 return unsignedp ? unsigned_short_fract_type_node
9931 : short_fract_type_node;
9932 if (size == FRACT_TYPE_SIZE)
9933 return unsignedp ? unsigned_fract_type_node : fract_type_node;
9934 if (size == LONG_FRACT_TYPE_SIZE)
9935 return unsignedp ? unsigned_long_fract_type_node
9936 : long_fract_type_node;
9937 if (size == LONG_LONG_FRACT_TYPE_SIZE)
9938 return unsignedp ? unsigned_long_long_fract_type_node
9939 : long_long_fract_type_node;
9942 return make_fract_type (size, unsignedp, satp);
9945 /* Create or reuse an accum type by SIZE, UNSIGNEDP, and SATP. */
9947 static tree
9948 make_or_reuse_accum_type (unsigned size, int unsignedp, int satp)
9950 if (satp)
9952 if (size == SHORT_ACCUM_TYPE_SIZE)
9953 return unsignedp ? sat_unsigned_short_accum_type_node
9954 : sat_short_accum_type_node;
9955 if (size == ACCUM_TYPE_SIZE)
9956 return unsignedp ? sat_unsigned_accum_type_node : sat_accum_type_node;
9957 if (size == LONG_ACCUM_TYPE_SIZE)
9958 return unsignedp ? sat_unsigned_long_accum_type_node
9959 : sat_long_accum_type_node;
9960 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9961 return unsignedp ? sat_unsigned_long_long_accum_type_node
9962 : sat_long_long_accum_type_node;
9964 else
9966 if (size == SHORT_ACCUM_TYPE_SIZE)
9967 return unsignedp ? unsigned_short_accum_type_node
9968 : short_accum_type_node;
9969 if (size == ACCUM_TYPE_SIZE)
9970 return unsignedp ? unsigned_accum_type_node : accum_type_node;
9971 if (size == LONG_ACCUM_TYPE_SIZE)
9972 return unsignedp ? unsigned_long_accum_type_node
9973 : long_accum_type_node;
9974 if (size == LONG_LONG_ACCUM_TYPE_SIZE)
9975 return unsignedp ? unsigned_long_long_accum_type_node
9976 : long_long_accum_type_node;
9979 return make_accum_type (size, unsignedp, satp);
9983 /* Create an atomic variant node for TYPE. This routine is called
9984 during initialization of data types to create the 5 basic atomic
9985 types. The generic build_variant_type function requires these to
9986 already be set up in order to function properly, so cannot be
9987 called from there. If ALIGN is non-zero, then ensure alignment is
9988 overridden to this value. */
9990 static tree
9991 build_atomic_base (tree type, unsigned int align)
9993 tree t;
9995 /* Make sure its not already registered. */
9996 if ((t = get_qualified_type (type, TYPE_QUAL_ATOMIC)))
9997 return t;
9999 t = build_variant_type_copy (type);
10000 set_type_quals (t, TYPE_QUAL_ATOMIC);
10002 if (align)
10003 TYPE_ALIGN (t) = align;
10005 return t;
10008 /* Create nodes for all integer types (and error_mark_node) using the sizes
10009 of C datatypes. SIGNED_CHAR specifies whether char is signed,
10010 SHORT_DOUBLE specifies whether double should be of the same precision
10011 as float. */
10013 void
10014 build_common_tree_nodes (bool signed_char, bool short_double)
10016 int i;
10018 error_mark_node = make_node (ERROR_MARK);
10019 TREE_TYPE (error_mark_node) = error_mark_node;
10021 initialize_sizetypes ();
10023 /* Define both `signed char' and `unsigned char'. */
10024 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
10025 TYPE_STRING_FLAG (signed_char_type_node) = 1;
10026 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
10027 TYPE_STRING_FLAG (unsigned_char_type_node) = 1;
10029 /* Define `char', which is like either `signed char' or `unsigned char'
10030 but not the same as either. */
10031 char_type_node
10032 = (signed_char
10033 ? make_signed_type (CHAR_TYPE_SIZE)
10034 : make_unsigned_type (CHAR_TYPE_SIZE));
10035 TYPE_STRING_FLAG (char_type_node) = 1;
10037 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
10038 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
10039 integer_type_node = make_signed_type (INT_TYPE_SIZE);
10040 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
10041 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
10042 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
10043 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
10044 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
10046 for (i = 0; i < NUM_INT_N_ENTS; i ++)
10048 int_n_trees[i].signed_type = make_signed_type (int_n_data[i].bitsize);
10049 int_n_trees[i].unsigned_type = make_unsigned_type (int_n_data[i].bitsize);
10050 TYPE_SIZE (int_n_trees[i].signed_type) = bitsize_int (int_n_data[i].bitsize);
10051 TYPE_SIZE (int_n_trees[i].unsigned_type) = bitsize_int (int_n_data[i].bitsize);
10053 if (int_n_data[i].bitsize > LONG_LONG_TYPE_SIZE
10054 && int_n_enabled_p[i])
10056 integer_types[itk_intN_0 + i * 2] = int_n_trees[i].signed_type;
10057 integer_types[itk_unsigned_intN_0 + i * 2] = int_n_trees[i].unsigned_type;
10061 /* Define a boolean type. This type only represents boolean values but
10062 may be larger than char depending on the value of BOOL_TYPE_SIZE. */
10063 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
10064 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
10065 TYPE_PRECISION (boolean_type_node) = 1;
10066 TYPE_MAX_VALUE (boolean_type_node) = build_int_cst (boolean_type_node, 1);
10068 /* Define what type to use for size_t. */
10069 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
10070 size_type_node = unsigned_type_node;
10071 else if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
10072 size_type_node = long_unsigned_type_node;
10073 else if (strcmp (SIZE_TYPE, "long long unsigned int") == 0)
10074 size_type_node = long_long_unsigned_type_node;
10075 else if (strcmp (SIZE_TYPE, "short unsigned int") == 0)
10076 size_type_node = short_unsigned_type_node;
10077 else
10079 int i;
10081 size_type_node = NULL_TREE;
10082 for (i = 0; i < NUM_INT_N_ENTS; i++)
10083 if (int_n_enabled_p[i])
10085 char name[50];
10086 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
10088 if (strcmp (name, SIZE_TYPE) == 0)
10090 size_type_node = int_n_trees[i].unsigned_type;
10093 if (size_type_node == NULL_TREE)
10094 gcc_unreachable ();
10097 /* Fill in the rest of the sized types. Reuse existing type nodes
10098 when possible. */
10099 intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);
10100 intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 0);
10101 intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 0);
10102 intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 0);
10103 intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 0);
10105 unsigned_intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 1);
10106 unsigned_intHI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (HImode), 1);
10107 unsigned_intSI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (SImode), 1);
10108 unsigned_intDI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (DImode), 1);
10109 unsigned_intTI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (TImode), 1);
10111 /* Don't call build_qualified type for atomics. That routine does
10112 special processing for atomics, and until they are initialized
10113 it's better not to make that call.
10115 Check to see if there is a target override for atomic types. */
10117 atomicQI_type_node = build_atomic_base (unsigned_intQI_type_node,
10118 targetm.atomic_align_for_mode (QImode));
10119 atomicHI_type_node = build_atomic_base (unsigned_intHI_type_node,
10120 targetm.atomic_align_for_mode (HImode));
10121 atomicSI_type_node = build_atomic_base (unsigned_intSI_type_node,
10122 targetm.atomic_align_for_mode (SImode));
10123 atomicDI_type_node = build_atomic_base (unsigned_intDI_type_node,
10124 targetm.atomic_align_for_mode (DImode));
10125 atomicTI_type_node = build_atomic_base (unsigned_intTI_type_node,
10126 targetm.atomic_align_for_mode (TImode));
10128 access_public_node = get_identifier ("public");
10129 access_protected_node = get_identifier ("protected");
10130 access_private_node = get_identifier ("private");
10132 /* Define these next since types below may used them. */
10133 integer_zero_node = build_int_cst (integer_type_node, 0);
10134 integer_one_node = build_int_cst (integer_type_node, 1);
10135 integer_three_node = build_int_cst (integer_type_node, 3);
10136 integer_minus_one_node = build_int_cst (integer_type_node, -1);
10138 size_zero_node = size_int (0);
10139 size_one_node = size_int (1);
10140 bitsize_zero_node = bitsize_int (0);
10141 bitsize_one_node = bitsize_int (1);
10142 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
10144 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
10145 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
10147 void_type_node = make_node (VOID_TYPE);
10148 layout_type (void_type_node);
10150 pointer_bounds_type_node = targetm.chkp_bound_type ();
10152 /* We are not going to have real types in C with less than byte alignment,
10153 so we might as well not have any types that claim to have it. */
10154 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
10155 TYPE_USER_ALIGN (void_type_node) = 0;
10157 void_node = make_node (VOID_CST);
10158 TREE_TYPE (void_node) = void_type_node;
10160 null_pointer_node = build_int_cst (build_pointer_type (void_type_node), 0);
10161 layout_type (TREE_TYPE (null_pointer_node));
10163 ptr_type_node = build_pointer_type (void_type_node);
10164 const_ptr_type_node
10165 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
10166 fileptr_type_node = ptr_type_node;
10168 pointer_sized_int_node = build_nonstandard_integer_type (POINTER_SIZE, 1);
10170 float_type_node = make_node (REAL_TYPE);
10171 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
10172 layout_type (float_type_node);
10174 double_type_node = make_node (REAL_TYPE);
10175 if (short_double)
10176 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
10177 else
10178 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
10179 layout_type (double_type_node);
10181 long_double_type_node = make_node (REAL_TYPE);
10182 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
10183 layout_type (long_double_type_node);
10185 float_ptr_type_node = build_pointer_type (float_type_node);
10186 double_ptr_type_node = build_pointer_type (double_type_node);
10187 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
10188 integer_ptr_type_node = build_pointer_type (integer_type_node);
10190 /* Fixed size integer types. */
10191 uint16_type_node = make_or_reuse_type (16, 1);
10192 uint32_type_node = make_or_reuse_type (32, 1);
10193 uint64_type_node = make_or_reuse_type (64, 1);
10195 /* Decimal float types. */
10196 dfloat32_type_node = make_node (REAL_TYPE);
10197 TYPE_PRECISION (dfloat32_type_node) = DECIMAL32_TYPE_SIZE;
10198 layout_type (dfloat32_type_node);
10199 SET_TYPE_MODE (dfloat32_type_node, SDmode);
10200 dfloat32_ptr_type_node = build_pointer_type (dfloat32_type_node);
10202 dfloat64_type_node = make_node (REAL_TYPE);
10203 TYPE_PRECISION (dfloat64_type_node) = DECIMAL64_TYPE_SIZE;
10204 layout_type (dfloat64_type_node);
10205 SET_TYPE_MODE (dfloat64_type_node, DDmode);
10206 dfloat64_ptr_type_node = build_pointer_type (dfloat64_type_node);
10208 dfloat128_type_node = make_node (REAL_TYPE);
10209 TYPE_PRECISION (dfloat128_type_node) = DECIMAL128_TYPE_SIZE;
10210 layout_type (dfloat128_type_node);
10211 SET_TYPE_MODE (dfloat128_type_node, TDmode);
10212 dfloat128_ptr_type_node = build_pointer_type (dfloat128_type_node);
10214 complex_integer_type_node = build_complex_type (integer_type_node);
10215 complex_float_type_node = build_complex_type (float_type_node);
10216 complex_double_type_node = build_complex_type (double_type_node);
10217 complex_long_double_type_node = build_complex_type (long_double_type_node);
10219 /* Make fixed-point nodes based on sat/non-sat and signed/unsigned. */
10220 #define MAKE_FIXED_TYPE_NODE(KIND,SIZE) \
10221 sat_ ## KIND ## _type_node = \
10222 make_sat_signed_ ## KIND ## _type (SIZE); \
10223 sat_unsigned_ ## KIND ## _type_node = \
10224 make_sat_unsigned_ ## KIND ## _type (SIZE); \
10225 KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
10226 unsigned_ ## KIND ## _type_node = \
10227 make_unsigned_ ## KIND ## _type (SIZE);
10229 #define MAKE_FIXED_TYPE_NODE_WIDTH(KIND,WIDTH,SIZE) \
10230 sat_ ## WIDTH ## KIND ## _type_node = \
10231 make_sat_signed_ ## KIND ## _type (SIZE); \
10232 sat_unsigned_ ## WIDTH ## KIND ## _type_node = \
10233 make_sat_unsigned_ ## KIND ## _type (SIZE); \
10234 WIDTH ## KIND ## _type_node = make_signed_ ## KIND ## _type (SIZE); \
10235 unsigned_ ## WIDTH ## KIND ## _type_node = \
10236 make_unsigned_ ## KIND ## _type (SIZE);
10238 /* Make fixed-point type nodes based on four different widths. */
10239 #define MAKE_FIXED_TYPE_NODE_FAMILY(N1,N2) \
10240 MAKE_FIXED_TYPE_NODE_WIDTH (N1, short_, SHORT_ ## N2 ## _TYPE_SIZE) \
10241 MAKE_FIXED_TYPE_NODE (N1, N2 ## _TYPE_SIZE) \
10242 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_, LONG_ ## N2 ## _TYPE_SIZE) \
10243 MAKE_FIXED_TYPE_NODE_WIDTH (N1, long_long_, LONG_LONG_ ## N2 ## _TYPE_SIZE)
10245 /* Make fixed-point mode nodes based on sat/non-sat and signed/unsigned. */
10246 #define MAKE_FIXED_MODE_NODE(KIND,NAME,MODE) \
10247 NAME ## _type_node = \
10248 make_or_reuse_signed_ ## KIND ## _type (GET_MODE_BITSIZE (MODE ## mode)); \
10249 u ## NAME ## _type_node = \
10250 make_or_reuse_unsigned_ ## KIND ## _type \
10251 (GET_MODE_BITSIZE (U ## MODE ## mode)); \
10252 sat_ ## NAME ## _type_node = \
10253 make_or_reuse_sat_signed_ ## KIND ## _type \
10254 (GET_MODE_BITSIZE (MODE ## mode)); \
10255 sat_u ## NAME ## _type_node = \
10256 make_or_reuse_sat_unsigned_ ## KIND ## _type \
10257 (GET_MODE_BITSIZE (U ## MODE ## mode));
10259 /* Fixed-point type and mode nodes. */
10260 MAKE_FIXED_TYPE_NODE_FAMILY (fract, FRACT)
10261 MAKE_FIXED_TYPE_NODE_FAMILY (accum, ACCUM)
10262 MAKE_FIXED_MODE_NODE (fract, qq, QQ)
10263 MAKE_FIXED_MODE_NODE (fract, hq, HQ)
10264 MAKE_FIXED_MODE_NODE (fract, sq, SQ)
10265 MAKE_FIXED_MODE_NODE (fract, dq, DQ)
10266 MAKE_FIXED_MODE_NODE (fract, tq, TQ)
10267 MAKE_FIXED_MODE_NODE (accum, ha, HA)
10268 MAKE_FIXED_MODE_NODE (accum, sa, SA)
10269 MAKE_FIXED_MODE_NODE (accum, da, DA)
10270 MAKE_FIXED_MODE_NODE (accum, ta, TA)
10273 tree t = targetm.build_builtin_va_list ();
10275 /* Many back-ends define record types without setting TYPE_NAME.
10276 If we copied the record type here, we'd keep the original
10277 record type without a name. This breaks name mangling. So,
10278 don't copy record types and let c_common_nodes_and_builtins()
10279 declare the type to be __builtin_va_list. */
10280 if (TREE_CODE (t) != RECORD_TYPE)
10281 t = build_variant_type_copy (t);
10283 va_list_type_node = t;
10287 /* Modify DECL for given flags.
10288 TM_PURE attribute is set only on types, so the function will modify
10289 DECL's type when ECF_TM_PURE is used. */
10291 void
10292 set_call_expr_flags (tree decl, int flags)
10294 if (flags & ECF_NOTHROW)
10295 TREE_NOTHROW (decl) = 1;
10296 if (flags & ECF_CONST)
10297 TREE_READONLY (decl) = 1;
10298 if (flags & ECF_PURE)
10299 DECL_PURE_P (decl) = 1;
10300 if (flags & ECF_LOOPING_CONST_OR_PURE)
10301 DECL_LOOPING_CONST_OR_PURE_P (decl) = 1;
10302 if (flags & ECF_NOVOPS)
10303 DECL_IS_NOVOPS (decl) = 1;
10304 if (flags & ECF_NORETURN)
10305 TREE_THIS_VOLATILE (decl) = 1;
10306 if (flags & ECF_MALLOC)
10307 DECL_IS_MALLOC (decl) = 1;
10308 if (flags & ECF_RETURNS_TWICE)
10309 DECL_IS_RETURNS_TWICE (decl) = 1;
10310 if (flags & ECF_LEAF)
10311 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("leaf"),
10312 NULL, DECL_ATTRIBUTES (decl));
10313 if ((flags & ECF_TM_PURE) && flag_tm)
10314 apply_tm_attr (decl, get_identifier ("transaction_pure"));
10315 /* Looping const or pure is implied by noreturn.
10316 There is currently no way to declare looping const or looping pure alone. */
10317 gcc_assert (!(flags & ECF_LOOPING_CONST_OR_PURE)
10318 || ((flags & ECF_NORETURN) && (flags & (ECF_CONST | ECF_PURE))));
10322 /* A subroutine of build_common_builtin_nodes. Define a builtin function. */
10324 static void
10325 local_define_builtin (const char *name, tree type, enum built_in_function code,
10326 const char *library_name, int ecf_flags)
10328 tree decl;
10330 decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
10331 library_name, NULL_TREE);
10332 set_call_expr_flags (decl, ecf_flags);
10334 set_builtin_decl (code, decl, true);
10337 /* Call this function after instantiating all builtins that the language
10338 front end cares about. This will build the rest of the builtins
10339 and internal functions that are relied upon by the tree optimizers and
10340 the middle-end. */
10342 void
10343 build_common_builtin_nodes (void)
10345 tree tmp, ftype;
10346 int ecf_flags;
10348 if (!builtin_decl_explicit_p (BUILT_IN_UNREACHABLE))
10350 ftype = build_function_type (void_type_node, void_list_node);
10351 local_define_builtin ("__builtin_unreachable", ftype, BUILT_IN_UNREACHABLE,
10352 "__builtin_unreachable",
10353 ECF_NOTHROW | ECF_LEAF | ECF_NORETURN
10354 | ECF_CONST);
10357 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY)
10358 || !builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
10360 ftype = build_function_type_list (ptr_type_node,
10361 ptr_type_node, const_ptr_type_node,
10362 size_type_node, NULL_TREE);
10364 if (!builtin_decl_explicit_p (BUILT_IN_MEMCPY))
10365 local_define_builtin ("__builtin_memcpy", ftype, BUILT_IN_MEMCPY,
10366 "memcpy", ECF_NOTHROW | ECF_LEAF);
10367 if (!builtin_decl_explicit_p (BUILT_IN_MEMMOVE))
10368 local_define_builtin ("__builtin_memmove", ftype, BUILT_IN_MEMMOVE,
10369 "memmove", ECF_NOTHROW | ECF_LEAF);
10372 if (!builtin_decl_explicit_p (BUILT_IN_MEMCMP))
10374 ftype = build_function_type_list (integer_type_node, const_ptr_type_node,
10375 const_ptr_type_node, size_type_node,
10376 NULL_TREE);
10377 local_define_builtin ("__builtin_memcmp", ftype, BUILT_IN_MEMCMP,
10378 "memcmp", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10381 if (!builtin_decl_explicit_p (BUILT_IN_MEMSET))
10383 ftype = build_function_type_list (ptr_type_node,
10384 ptr_type_node, integer_type_node,
10385 size_type_node, NULL_TREE);
10386 local_define_builtin ("__builtin_memset", ftype, BUILT_IN_MEMSET,
10387 "memset", ECF_NOTHROW | ECF_LEAF);
10390 if (!builtin_decl_explicit_p (BUILT_IN_ALLOCA))
10392 ftype = build_function_type_list (ptr_type_node,
10393 size_type_node, NULL_TREE);
10394 local_define_builtin ("__builtin_alloca", ftype, BUILT_IN_ALLOCA,
10395 "alloca", ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
10398 ftype = build_function_type_list (ptr_type_node, size_type_node,
10399 size_type_node, NULL_TREE);
10400 local_define_builtin ("__builtin_alloca_with_align", ftype,
10401 BUILT_IN_ALLOCA_WITH_ALIGN,
10402 "__builtin_alloca_with_align",
10403 ECF_MALLOC | ECF_NOTHROW | ECF_LEAF);
10405 /* If we're checking the stack, `alloca' can throw. */
10406 if (flag_stack_check)
10408 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA)) = 0;
10409 TREE_NOTHROW (builtin_decl_explicit (BUILT_IN_ALLOCA_WITH_ALIGN)) = 0;
10412 ftype = build_function_type_list (void_type_node,
10413 ptr_type_node, ptr_type_node,
10414 ptr_type_node, NULL_TREE);
10415 local_define_builtin ("__builtin_init_trampoline", ftype,
10416 BUILT_IN_INIT_TRAMPOLINE,
10417 "__builtin_init_trampoline", ECF_NOTHROW | ECF_LEAF);
10418 local_define_builtin ("__builtin_init_heap_trampoline", ftype,
10419 BUILT_IN_INIT_HEAP_TRAMPOLINE,
10420 "__builtin_init_heap_trampoline",
10421 ECF_NOTHROW | ECF_LEAF);
10423 ftype = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
10424 local_define_builtin ("__builtin_adjust_trampoline", ftype,
10425 BUILT_IN_ADJUST_TRAMPOLINE,
10426 "__builtin_adjust_trampoline",
10427 ECF_CONST | ECF_NOTHROW);
10429 ftype = build_function_type_list (void_type_node,
10430 ptr_type_node, ptr_type_node, NULL_TREE);
10431 local_define_builtin ("__builtin_nonlocal_goto", ftype,
10432 BUILT_IN_NONLOCAL_GOTO,
10433 "__builtin_nonlocal_goto",
10434 ECF_NORETURN | ECF_NOTHROW);
10436 ftype = build_function_type_list (void_type_node,
10437 ptr_type_node, ptr_type_node, NULL_TREE);
10438 local_define_builtin ("__builtin_setjmp_setup", ftype,
10439 BUILT_IN_SETJMP_SETUP,
10440 "__builtin_setjmp_setup", ECF_NOTHROW);
10442 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10443 local_define_builtin ("__builtin_setjmp_receiver", ftype,
10444 BUILT_IN_SETJMP_RECEIVER,
10445 "__builtin_setjmp_receiver", ECF_NOTHROW | ECF_LEAF);
10447 ftype = build_function_type_list (ptr_type_node, NULL_TREE);
10448 local_define_builtin ("__builtin_stack_save", ftype, BUILT_IN_STACK_SAVE,
10449 "__builtin_stack_save", ECF_NOTHROW | ECF_LEAF);
10451 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10452 local_define_builtin ("__builtin_stack_restore", ftype,
10453 BUILT_IN_STACK_RESTORE,
10454 "__builtin_stack_restore", ECF_NOTHROW | ECF_LEAF);
10456 /* If there's a possibility that we might use the ARM EABI, build the
10457 alternate __cxa_end_cleanup node used to resume from C++ and Java. */
10458 if (targetm.arm_eabi_unwinder)
10460 ftype = build_function_type_list (void_type_node, NULL_TREE);
10461 local_define_builtin ("__builtin_cxa_end_cleanup", ftype,
10462 BUILT_IN_CXA_END_CLEANUP,
10463 "__cxa_end_cleanup", ECF_NORETURN | ECF_LEAF);
10466 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
10467 local_define_builtin ("__builtin_unwind_resume", ftype,
10468 BUILT_IN_UNWIND_RESUME,
10469 ((targetm_common.except_unwind_info (&global_options)
10470 == UI_SJLJ)
10471 ? "_Unwind_SjLj_Resume" : "_Unwind_Resume"),
10472 ECF_NORETURN);
10474 if (builtin_decl_explicit (BUILT_IN_RETURN_ADDRESS) == NULL_TREE)
10476 ftype = build_function_type_list (ptr_type_node, integer_type_node,
10477 NULL_TREE);
10478 local_define_builtin ("__builtin_return_address", ftype,
10479 BUILT_IN_RETURN_ADDRESS,
10480 "__builtin_return_address",
10481 ECF_NOTHROW);
10484 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER)
10485 || !builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
10487 ftype = build_function_type_list (void_type_node, ptr_type_node,
10488 ptr_type_node, NULL_TREE);
10489 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_ENTER))
10490 local_define_builtin ("__cyg_profile_func_enter", ftype,
10491 BUILT_IN_PROFILE_FUNC_ENTER,
10492 "__cyg_profile_func_enter", 0);
10493 if (!builtin_decl_explicit_p (BUILT_IN_PROFILE_FUNC_EXIT))
10494 local_define_builtin ("__cyg_profile_func_exit", ftype,
10495 BUILT_IN_PROFILE_FUNC_EXIT,
10496 "__cyg_profile_func_exit", 0);
10499 /* The exception object and filter values from the runtime. The argument
10500 must be zero before exception lowering, i.e. from the front end. After
10501 exception lowering, it will be the region number for the exception
10502 landing pad. These functions are PURE instead of CONST to prevent
10503 them from being hoisted past the exception edge that will initialize
10504 its value in the landing pad. */
10505 ftype = build_function_type_list (ptr_type_node,
10506 integer_type_node, NULL_TREE);
10507 ecf_flags = ECF_PURE | ECF_NOTHROW | ECF_LEAF;
10508 /* Only use TM_PURE if we have TM language support. */
10509 if (builtin_decl_explicit_p (BUILT_IN_TM_LOAD_1))
10510 ecf_flags |= ECF_TM_PURE;
10511 local_define_builtin ("__builtin_eh_pointer", ftype, BUILT_IN_EH_POINTER,
10512 "__builtin_eh_pointer", ecf_flags);
10514 tmp = lang_hooks.types.type_for_mode (targetm.eh_return_filter_mode (), 0);
10515 ftype = build_function_type_list (tmp, integer_type_node, NULL_TREE);
10516 local_define_builtin ("__builtin_eh_filter", ftype, BUILT_IN_EH_FILTER,
10517 "__builtin_eh_filter", ECF_PURE | ECF_NOTHROW | ECF_LEAF);
10519 ftype = build_function_type_list (void_type_node,
10520 integer_type_node, integer_type_node,
10521 NULL_TREE);
10522 local_define_builtin ("__builtin_eh_copy_values", ftype,
10523 BUILT_IN_EH_COPY_VALUES,
10524 "__builtin_eh_copy_values", ECF_NOTHROW);
10526 /* Complex multiplication and division. These are handled as builtins
10527 rather than optabs because emit_library_call_value doesn't support
10528 complex. Further, we can do slightly better with folding these
10529 beasties if the real and complex parts of the arguments are separate. */
10531 int mode;
10533 for (mode = MIN_MODE_COMPLEX_FLOAT; mode <= MAX_MODE_COMPLEX_FLOAT; ++mode)
10535 char mode_name_buf[4], *q;
10536 const char *p;
10537 enum built_in_function mcode, dcode;
10538 tree type, inner_type;
10539 const char *prefix = "__";
10541 if (targetm.libfunc_gnu_prefix)
10542 prefix = "__gnu_";
10544 type = lang_hooks.types.type_for_mode ((machine_mode) mode, 0);
10545 if (type == NULL)
10546 continue;
10547 inner_type = TREE_TYPE (type);
10549 ftype = build_function_type_list (type, inner_type, inner_type,
10550 inner_type, inner_type, NULL_TREE);
10552 mcode = ((enum built_in_function)
10553 (BUILT_IN_COMPLEX_MUL_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10554 dcode = ((enum built_in_function)
10555 (BUILT_IN_COMPLEX_DIV_MIN + mode - MIN_MODE_COMPLEX_FLOAT));
10557 for (p = GET_MODE_NAME (mode), q = mode_name_buf; *p; p++, q++)
10558 *q = TOLOWER (*p);
10559 *q = '\0';
10561 built_in_names[mcode] = concat (prefix, "mul", mode_name_buf, "3",
10562 NULL);
10563 local_define_builtin (built_in_names[mcode], ftype, mcode,
10564 built_in_names[mcode],
10565 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
10567 built_in_names[dcode] = concat (prefix, "div", mode_name_buf, "3",
10568 NULL);
10569 local_define_builtin (built_in_names[dcode], ftype, dcode,
10570 built_in_names[dcode],
10571 ECF_CONST | ECF_NOTHROW | ECF_LEAF);
10575 init_internal_fns ();
10578 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
10579 better way.
10581 If we requested a pointer to a vector, build up the pointers that
10582 we stripped off while looking for the inner type. Similarly for
10583 return values from functions.
10585 The argument TYPE is the top of the chain, and BOTTOM is the
10586 new type which we will point to. */
10588 tree
10589 reconstruct_complex_type (tree type, tree bottom)
10591 tree inner, outer;
10593 if (TREE_CODE (type) == POINTER_TYPE)
10595 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10596 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type),
10597 TYPE_REF_CAN_ALIAS_ALL (type));
10599 else if (TREE_CODE (type) == REFERENCE_TYPE)
10601 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10602 outer = build_reference_type_for_mode (inner, TYPE_MODE (type),
10603 TYPE_REF_CAN_ALIAS_ALL (type));
10605 else if (TREE_CODE (type) == ARRAY_TYPE)
10607 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10608 outer = build_array_type (inner, TYPE_DOMAIN (type));
10610 else if (TREE_CODE (type) == FUNCTION_TYPE)
10612 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10613 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
10615 else if (TREE_CODE (type) == METHOD_TYPE)
10617 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10618 /* The build_method_type_directly() routine prepends 'this' to argument list,
10619 so we must compensate by getting rid of it. */
10620 outer
10621 = build_method_type_directly
10622 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))),
10623 inner,
10624 TREE_CHAIN (TYPE_ARG_TYPES (type)));
10626 else if (TREE_CODE (type) == OFFSET_TYPE)
10628 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
10629 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner);
10631 else
10632 return bottom;
10634 return build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
10635 TYPE_QUALS (type));
10638 /* Returns a vector tree node given a mode (integer, vector, or BLKmode) and
10639 the inner type. */
10640 tree
10641 build_vector_type_for_mode (tree innertype, machine_mode mode)
10643 int nunits;
10645 switch (GET_MODE_CLASS (mode))
10647 case MODE_VECTOR_INT:
10648 case MODE_VECTOR_FLOAT:
10649 case MODE_VECTOR_FRACT:
10650 case MODE_VECTOR_UFRACT:
10651 case MODE_VECTOR_ACCUM:
10652 case MODE_VECTOR_UACCUM:
10653 nunits = GET_MODE_NUNITS (mode);
10654 break;
10656 case MODE_INT:
10657 /* Check that there are no leftover bits. */
10658 gcc_assert (GET_MODE_BITSIZE (mode)
10659 % TREE_INT_CST_LOW (TYPE_SIZE (innertype)) == 0);
10661 nunits = GET_MODE_BITSIZE (mode)
10662 / TREE_INT_CST_LOW (TYPE_SIZE (innertype));
10663 break;
10665 default:
10666 gcc_unreachable ();
10669 return make_vector_type (innertype, nunits, mode);
10672 /* Similarly, but takes the inner type and number of units, which must be
10673 a power of two. */
10675 tree
10676 build_vector_type (tree innertype, int nunits)
10678 return make_vector_type (innertype, nunits, VOIDmode);
10681 /* Build truth vector with specified length and number of units. */
10683 tree
10684 build_truth_vector_type (unsigned nunits, unsigned vector_size)
10686 machine_mode mask_mode = targetm.vectorize.get_mask_mode (nunits,
10687 vector_size);
10689 gcc_assert (mask_mode != VOIDmode);
10691 unsigned HOST_WIDE_INT vsize;
10692 if (mask_mode == BLKmode)
10693 vsize = vector_size * BITS_PER_UNIT;
10694 else
10695 vsize = GET_MODE_BITSIZE (mask_mode);
10697 unsigned HOST_WIDE_INT esize = vsize / nunits;
10698 gcc_assert (esize * nunits == vsize);
10700 tree bool_type = build_nonstandard_boolean_type (esize);
10702 return make_vector_type (bool_type, nunits, mask_mode);
10705 /* Returns a vector type corresponding to a comparison of VECTYPE. */
10707 tree
10708 build_same_sized_truth_vector_type (tree vectype)
10710 if (VECTOR_BOOLEAN_TYPE_P (vectype))
10711 return vectype;
10713 unsigned HOST_WIDE_INT size = GET_MODE_SIZE (TYPE_MODE (vectype));
10715 if (!size)
10716 size = tree_to_uhwi (TYPE_SIZE_UNIT (vectype));
10718 return build_truth_vector_type (TYPE_VECTOR_SUBPARTS (vectype), size);
10721 /* Similarly, but builds a variant type with TYPE_VECTOR_OPAQUE set. */
10723 tree
10724 build_opaque_vector_type (tree innertype, int nunits)
10726 tree t = make_vector_type (innertype, nunits, VOIDmode);
10727 tree cand;
10728 /* We always build the non-opaque variant before the opaque one,
10729 so if it already exists, it is TYPE_NEXT_VARIANT of this one. */
10730 cand = TYPE_NEXT_VARIANT (t);
10731 if (cand
10732 && TYPE_VECTOR_OPAQUE (cand)
10733 && check_qualified_type (cand, t, TYPE_QUALS (t)))
10734 return cand;
10735 /* Othewise build a variant type and make sure to queue it after
10736 the non-opaque type. */
10737 cand = build_distinct_type_copy (t);
10738 TYPE_VECTOR_OPAQUE (cand) = true;
10739 TYPE_CANONICAL (cand) = TYPE_CANONICAL (t);
10740 TYPE_NEXT_VARIANT (cand) = TYPE_NEXT_VARIANT (t);
10741 TYPE_NEXT_VARIANT (t) = cand;
10742 TYPE_MAIN_VARIANT (cand) = TYPE_MAIN_VARIANT (t);
10743 return cand;
10747 /* Given an initializer INIT, return TRUE if INIT is zero or some
10748 aggregate of zeros. Otherwise return FALSE. */
10749 bool
10750 initializer_zerop (const_tree init)
10752 tree elt;
10754 STRIP_NOPS (init);
10756 switch (TREE_CODE (init))
10758 case INTEGER_CST:
10759 return integer_zerop (init);
10761 case REAL_CST:
10762 /* ??? Note that this is not correct for C4X float formats. There,
10763 a bit pattern of all zeros is 1.0; 0.0 is encoded with the most
10764 negative exponent. */
10765 return real_zerop (init)
10766 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
10768 case FIXED_CST:
10769 return fixed_zerop (init);
10771 case COMPLEX_CST:
10772 return integer_zerop (init)
10773 || (real_zerop (init)
10774 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
10775 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
10777 case VECTOR_CST:
10779 unsigned i;
10780 for (i = 0; i < VECTOR_CST_NELTS (init); ++i)
10781 if (!initializer_zerop (VECTOR_CST_ELT (init, i)))
10782 return false;
10783 return true;
10786 case CONSTRUCTOR:
10788 unsigned HOST_WIDE_INT idx;
10790 if (TREE_CLOBBER_P (init))
10791 return false;
10792 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
10793 if (!initializer_zerop (elt))
10794 return false;
10795 return true;
10798 case STRING_CST:
10800 int i;
10802 /* We need to loop through all elements to handle cases like
10803 "\0" and "\0foobar". */
10804 for (i = 0; i < TREE_STRING_LENGTH (init); ++i)
10805 if (TREE_STRING_POINTER (init)[i] != '\0')
10806 return false;
10808 return true;
10811 default:
10812 return false;
10816 /* Check if vector VEC consists of all the equal elements and
10817 that the number of elements corresponds to the type of VEC.
10818 The function returns first element of the vector
10819 or NULL_TREE if the vector is not uniform. */
10820 tree
10821 uniform_vector_p (const_tree vec)
10823 tree first, t;
10824 unsigned i;
10826 if (vec == NULL_TREE)
10827 return NULL_TREE;
10829 gcc_assert (VECTOR_TYPE_P (TREE_TYPE (vec)));
10831 if (TREE_CODE (vec) == VECTOR_CST)
10833 first = VECTOR_CST_ELT (vec, 0);
10834 for (i = 1; i < VECTOR_CST_NELTS (vec); ++i)
10835 if (!operand_equal_p (first, VECTOR_CST_ELT (vec, i), 0))
10836 return NULL_TREE;
10838 return first;
10841 else if (TREE_CODE (vec) == CONSTRUCTOR)
10843 first = error_mark_node;
10845 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (vec), i, t)
10847 if (i == 0)
10849 first = t;
10850 continue;
10852 if (!operand_equal_p (first, t, 0))
10853 return NULL_TREE;
10855 if (i != TYPE_VECTOR_SUBPARTS (TREE_TYPE (vec)))
10856 return NULL_TREE;
10858 return first;
10861 return NULL_TREE;
10864 /* Build an empty statement at location LOC. */
10866 tree
10867 build_empty_stmt (location_t loc)
10869 tree t = build1 (NOP_EXPR, void_type_node, size_zero_node);
10870 SET_EXPR_LOCATION (t, loc);
10871 return t;
10875 /* Build an OpenMP clause with code CODE. LOC is the location of the
10876 clause. */
10878 tree
10879 build_omp_clause (location_t loc, enum omp_clause_code code)
10881 tree t;
10882 int size, length;
10884 length = omp_clause_num_ops[code];
10885 size = (sizeof (struct tree_omp_clause) + (length - 1) * sizeof (tree));
10887 record_node_allocation_statistics (OMP_CLAUSE, size);
10889 t = (tree) ggc_internal_alloc (size);
10890 memset (t, 0, size);
10891 TREE_SET_CODE (t, OMP_CLAUSE);
10892 OMP_CLAUSE_SET_CODE (t, code);
10893 OMP_CLAUSE_LOCATION (t) = loc;
10895 return t;
10898 /* Build a tcc_vl_exp object with code CODE and room for LEN operands. LEN
10899 includes the implicit operand count in TREE_OPERAND 0, and so must be >= 1.
10900 Except for the CODE and operand count field, other storage for the
10901 object is initialized to zeros. */
10903 tree
10904 build_vl_exp_stat (enum tree_code code, int len MEM_STAT_DECL)
10906 tree t;
10907 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_exp);
10909 gcc_assert (TREE_CODE_CLASS (code) == tcc_vl_exp);
10910 gcc_assert (len >= 1);
10912 record_node_allocation_statistics (code, length);
10914 t = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
10916 TREE_SET_CODE (t, code);
10918 /* Can't use TREE_OPERAND to store the length because if checking is
10919 enabled, it will try to check the length before we store it. :-P */
10920 t->exp.operands[0] = build_int_cst (sizetype, len);
10922 return t;
10925 /* Helper function for build_call_* functions; build a CALL_EXPR with
10926 indicated RETURN_TYPE, FN, and NARGS, but do not initialize any of
10927 the argument slots. */
10929 static tree
10930 build_call_1 (tree return_type, tree fn, int nargs)
10932 tree t;
10934 t = build_vl_exp (CALL_EXPR, nargs + 3);
10935 TREE_TYPE (t) = return_type;
10936 CALL_EXPR_FN (t) = fn;
10937 CALL_EXPR_STATIC_CHAIN (t) = NULL;
10939 return t;
10942 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10943 FN and a null static chain slot. NARGS is the number of call arguments
10944 which are specified as "..." arguments. */
10946 tree
10947 build_call_nary (tree return_type, tree fn, int nargs, ...)
10949 tree ret;
10950 va_list args;
10951 va_start (args, nargs);
10952 ret = build_call_valist (return_type, fn, nargs, args);
10953 va_end (args);
10954 return ret;
10957 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10958 FN and a null static chain slot. NARGS is the number of call arguments
10959 which are specified as a va_list ARGS. */
10961 tree
10962 build_call_valist (tree return_type, tree fn, int nargs, va_list args)
10964 tree t;
10965 int i;
10967 t = build_call_1 (return_type, fn, nargs);
10968 for (i = 0; i < nargs; i++)
10969 CALL_EXPR_ARG (t, i) = va_arg (args, tree);
10970 process_call_operands (t);
10971 return t;
10974 /* Build a CALL_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE and
10975 FN and a null static chain slot. NARGS is the number of call arguments
10976 which are specified as a tree array ARGS. */
10978 tree
10979 build_call_array_loc (location_t loc, tree return_type, tree fn,
10980 int nargs, const tree *args)
10982 tree t;
10983 int i;
10985 t = build_call_1 (return_type, fn, nargs);
10986 for (i = 0; i < nargs; i++)
10987 CALL_EXPR_ARG (t, i) = args[i];
10988 process_call_operands (t);
10989 SET_EXPR_LOCATION (t, loc);
10990 return t;
10993 /* Like build_call_array, but takes a vec. */
10995 tree
10996 build_call_vec (tree return_type, tree fn, vec<tree, va_gc> *args)
10998 tree ret, t;
10999 unsigned int ix;
11001 ret = build_call_1 (return_type, fn, vec_safe_length (args));
11002 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
11003 CALL_EXPR_ARG (ret, ix) = t;
11004 process_call_operands (ret);
11005 return ret;
11008 /* Conveniently construct a function call expression. FNDECL names the
11009 function to be called and N arguments are passed in the array
11010 ARGARRAY. */
11012 tree
11013 build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
11015 tree fntype = TREE_TYPE (fndecl);
11016 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
11018 return fold_build_call_array_loc (loc, TREE_TYPE (fntype), fn, n, argarray);
11021 /* Conveniently construct a function call expression. FNDECL names the
11022 function to be called and the arguments are passed in the vector
11023 VEC. */
11025 tree
11026 build_call_expr_loc_vec (location_t loc, tree fndecl, vec<tree, va_gc> *vec)
11028 return build_call_expr_loc_array (loc, fndecl, vec_safe_length (vec),
11029 vec_safe_address (vec));
11033 /* Conveniently construct a function call expression. FNDECL names the
11034 function to be called, N is the number of arguments, and the "..."
11035 parameters are the argument expressions. */
11037 tree
11038 build_call_expr_loc (location_t loc, tree fndecl, int n, ...)
11040 va_list ap;
11041 tree *argarray = XALLOCAVEC (tree, n);
11042 int i;
11044 va_start (ap, n);
11045 for (i = 0; i < n; i++)
11046 argarray[i] = va_arg (ap, tree);
11047 va_end (ap);
11048 return build_call_expr_loc_array (loc, fndecl, n, argarray);
11051 /* Like build_call_expr_loc (UNKNOWN_LOCATION, ...). Duplicated because
11052 varargs macros aren't supported by all bootstrap compilers. */
11054 tree
11055 build_call_expr (tree fndecl, int n, ...)
11057 va_list ap;
11058 tree *argarray = XALLOCAVEC (tree, n);
11059 int i;
11061 va_start (ap, n);
11062 for (i = 0; i < n; i++)
11063 argarray[i] = va_arg (ap, tree);
11064 va_end (ap);
11065 return build_call_expr_loc_array (UNKNOWN_LOCATION, fndecl, n, argarray);
11068 /* Build an internal call to IFN, with arguments ARGS[0:N-1] and with return
11069 type TYPE. This is just like CALL_EXPR, except its CALL_EXPR_FN is NULL.
11070 It will get gimplified later into an ordinary internal function. */
11072 tree
11073 build_call_expr_internal_loc_array (location_t loc, internal_fn ifn,
11074 tree type, int n, const tree *args)
11076 tree t = build_call_1 (type, NULL_TREE, n);
11077 for (int i = 0; i < n; ++i)
11078 CALL_EXPR_ARG (t, i) = args[i];
11079 SET_EXPR_LOCATION (t, loc);
11080 CALL_EXPR_IFN (t) = ifn;
11081 return t;
11084 /* Build internal call expression. This is just like CALL_EXPR, except
11085 its CALL_EXPR_FN is NULL. It will get gimplified later into ordinary
11086 internal function. */
11088 tree
11089 build_call_expr_internal_loc (location_t loc, enum internal_fn ifn,
11090 tree type, int n, ...)
11092 va_list ap;
11093 tree *argarray = XALLOCAVEC (tree, n);
11094 int i;
11096 va_start (ap, n);
11097 for (i = 0; i < n; i++)
11098 argarray[i] = va_arg (ap, tree);
11099 va_end (ap);
11100 return build_call_expr_internal_loc_array (loc, ifn, type, n, argarray);
11103 /* Return a function call to FN, if the target is guaranteed to support it,
11104 or null otherwise.
11106 N is the number of arguments, passed in the "...", and TYPE is the
11107 type of the return value. */
11109 tree
11110 maybe_build_call_expr_loc (location_t loc, combined_fn fn, tree type,
11111 int n, ...)
11113 va_list ap;
11114 tree *argarray = XALLOCAVEC (tree, n);
11115 int i;
11117 va_start (ap, n);
11118 for (i = 0; i < n; i++)
11119 argarray[i] = va_arg (ap, tree);
11120 va_end (ap);
11121 if (internal_fn_p (fn))
11123 internal_fn ifn = as_internal_fn (fn);
11124 if (direct_internal_fn_p (ifn))
11126 tree_pair types = direct_internal_fn_types (ifn, type, argarray);
11127 if (!direct_internal_fn_supported_p (ifn, types))
11128 return NULL_TREE;
11130 return build_call_expr_internal_loc_array (loc, ifn, type, n, argarray);
11132 else
11134 tree fndecl = builtin_decl_implicit (as_builtin_fn (fn));
11135 if (!fndecl)
11136 return NULL_TREE;
11137 return build_call_expr_loc_array (loc, fndecl, n, argarray);
11141 /* Create a new constant string literal and return a char* pointer to it.
11142 The STRING_CST value is the LEN characters at STR. */
11143 tree
11144 build_string_literal (int len, const char *str)
11146 tree t, elem, index, type;
11148 t = build_string (len, str);
11149 elem = build_type_variant (char_type_node, 1, 0);
11150 index = build_index_type (size_int (len - 1));
11151 type = build_array_type (elem, index);
11152 TREE_TYPE (t) = type;
11153 TREE_CONSTANT (t) = 1;
11154 TREE_READONLY (t) = 1;
11155 TREE_STATIC (t) = 1;
11157 type = build_pointer_type (elem);
11158 t = build1 (ADDR_EXPR, type,
11159 build4 (ARRAY_REF, elem,
11160 t, integer_zero_node, NULL_TREE, NULL_TREE));
11161 return t;
11166 /* Return true if T (assumed to be a DECL) must be assigned a memory
11167 location. */
11169 bool
11170 needs_to_live_in_memory (const_tree t)
11172 return (TREE_ADDRESSABLE (t)
11173 || is_global_var (t)
11174 || (TREE_CODE (t) == RESULT_DECL
11175 && !DECL_BY_REFERENCE (t)
11176 && aggregate_value_p (t, current_function_decl)));
11179 /* Return value of a constant X and sign-extend it. */
11181 HOST_WIDE_INT
11182 int_cst_value (const_tree x)
11184 unsigned bits = TYPE_PRECISION (TREE_TYPE (x));
11185 unsigned HOST_WIDE_INT val = TREE_INT_CST_LOW (x);
11187 /* Make sure the sign-extended value will fit in a HOST_WIDE_INT. */
11188 gcc_assert (cst_and_fits_in_hwi (x));
11190 if (bits < HOST_BITS_PER_WIDE_INT)
11192 bool negative = ((val >> (bits - 1)) & 1) != 0;
11193 if (negative)
11194 val |= (~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1;
11195 else
11196 val &= ~((~(unsigned HOST_WIDE_INT) 0) << (bits - 1) << 1);
11199 return val;
11202 /* If TYPE is an integral or pointer type, return an integer type with
11203 the same precision which is unsigned iff UNSIGNEDP is true, or itself
11204 if TYPE is already an integer type of signedness UNSIGNEDP. */
11206 tree
11207 signed_or_unsigned_type_for (int unsignedp, tree type)
11209 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type) == unsignedp)
11210 return type;
11212 if (TREE_CODE (type) == VECTOR_TYPE)
11214 tree inner = TREE_TYPE (type);
11215 tree inner2 = signed_or_unsigned_type_for (unsignedp, inner);
11216 if (!inner2)
11217 return NULL_TREE;
11218 if (inner == inner2)
11219 return type;
11220 return build_vector_type (inner2, TYPE_VECTOR_SUBPARTS (type));
11223 if (!INTEGRAL_TYPE_P (type)
11224 && !POINTER_TYPE_P (type)
11225 && TREE_CODE (type) != OFFSET_TYPE)
11226 return NULL_TREE;
11228 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
11231 /* If TYPE is an integral or pointer type, return an integer type with
11232 the same precision which is unsigned, or itself if TYPE is already an
11233 unsigned integer type. */
11235 tree
11236 unsigned_type_for (tree type)
11238 return signed_or_unsigned_type_for (1, type);
11241 /* If TYPE is an integral or pointer type, return an integer type with
11242 the same precision which is signed, or itself if TYPE is already a
11243 signed integer type. */
11245 tree
11246 signed_type_for (tree type)
11248 return signed_or_unsigned_type_for (0, type);
11251 /* If TYPE is a vector type, return a signed integer vector type with the
11252 same width and number of subparts. Otherwise return boolean_type_node. */
11254 tree
11255 truth_type_for (tree type)
11257 if (TREE_CODE (type) == VECTOR_TYPE)
11259 if (VECTOR_BOOLEAN_TYPE_P (type))
11260 return type;
11261 return build_truth_vector_type (TYPE_VECTOR_SUBPARTS (type),
11262 GET_MODE_SIZE (TYPE_MODE (type)));
11264 else
11265 return boolean_type_node;
11268 /* Returns the largest value obtainable by casting something in INNER type to
11269 OUTER type. */
11271 tree
11272 upper_bound_in_type (tree outer, tree inner)
11274 unsigned int det = 0;
11275 unsigned oprec = TYPE_PRECISION (outer);
11276 unsigned iprec = TYPE_PRECISION (inner);
11277 unsigned prec;
11279 /* Compute a unique number for every combination. */
11280 det |= (oprec > iprec) ? 4 : 0;
11281 det |= TYPE_UNSIGNED (outer) ? 2 : 0;
11282 det |= TYPE_UNSIGNED (inner) ? 1 : 0;
11284 /* Determine the exponent to use. */
11285 switch (det)
11287 case 0:
11288 case 1:
11289 /* oprec <= iprec, outer: signed, inner: don't care. */
11290 prec = oprec - 1;
11291 break;
11292 case 2:
11293 case 3:
11294 /* oprec <= iprec, outer: unsigned, inner: don't care. */
11295 prec = oprec;
11296 break;
11297 case 4:
11298 /* oprec > iprec, outer: signed, inner: signed. */
11299 prec = iprec - 1;
11300 break;
11301 case 5:
11302 /* oprec > iprec, outer: signed, inner: unsigned. */
11303 prec = iprec;
11304 break;
11305 case 6:
11306 /* oprec > iprec, outer: unsigned, inner: signed. */
11307 prec = oprec;
11308 break;
11309 case 7:
11310 /* oprec > iprec, outer: unsigned, inner: unsigned. */
11311 prec = iprec;
11312 break;
11313 default:
11314 gcc_unreachable ();
11317 return wide_int_to_tree (outer,
11318 wi::mask (prec, false, TYPE_PRECISION (outer)));
11321 /* Returns the smallest value obtainable by casting something in INNER type to
11322 OUTER type. */
11324 tree
11325 lower_bound_in_type (tree outer, tree inner)
11327 unsigned oprec = TYPE_PRECISION (outer);
11328 unsigned iprec = TYPE_PRECISION (inner);
11330 /* If OUTER type is unsigned, we can definitely cast 0 to OUTER type
11331 and obtain 0. */
11332 if (TYPE_UNSIGNED (outer)
11333 /* If we are widening something of an unsigned type, OUTER type
11334 contains all values of INNER type. In particular, both INNER
11335 and OUTER types have zero in common. */
11336 || (oprec > iprec && TYPE_UNSIGNED (inner)))
11337 return build_int_cst (outer, 0);
11338 else
11340 /* If we are widening a signed type to another signed type, we
11341 want to obtain -2^^(iprec-1). If we are keeping the
11342 precision or narrowing to a signed type, we want to obtain
11343 -2^(oprec-1). */
11344 unsigned prec = oprec > iprec ? iprec : oprec;
11345 return wide_int_to_tree (outer,
11346 wi::mask (prec - 1, true,
11347 TYPE_PRECISION (outer)));
11351 /* Return nonzero if two operands that are suitable for PHI nodes are
11352 necessarily equal. Specifically, both ARG0 and ARG1 must be either
11353 SSA_NAME or invariant. Note that this is strictly an optimization.
11354 That is, callers of this function can directly call operand_equal_p
11355 and get the same result, only slower. */
11358 operand_equal_for_phi_arg_p (const_tree arg0, const_tree arg1)
11360 if (arg0 == arg1)
11361 return 1;
11362 if (TREE_CODE (arg0) == SSA_NAME || TREE_CODE (arg1) == SSA_NAME)
11363 return 0;
11364 return operand_equal_p (arg0, arg1, 0);
11367 /* Returns number of zeros at the end of binary representation of X. */
11369 tree
11370 num_ending_zeros (const_tree x)
11372 return build_int_cst (TREE_TYPE (x), wi::ctz (x));
11376 #define WALK_SUBTREE(NODE) \
11377 do \
11379 result = walk_tree_1 (&(NODE), func, data, pset, lh); \
11380 if (result) \
11381 return result; \
11383 while (0)
11385 /* This is a subroutine of walk_tree that walks field of TYPE that are to
11386 be walked whenever a type is seen in the tree. Rest of operands and return
11387 value are as for walk_tree. */
11389 static tree
11390 walk_type_fields (tree type, walk_tree_fn func, void *data,
11391 hash_set<tree> *pset, walk_tree_lh lh)
11393 tree result = NULL_TREE;
11395 switch (TREE_CODE (type))
11397 case POINTER_TYPE:
11398 case REFERENCE_TYPE:
11399 case VECTOR_TYPE:
11400 /* We have to worry about mutually recursive pointers. These can't
11401 be written in C. They can in Ada. It's pathological, but
11402 there's an ACATS test (c38102a) that checks it. Deal with this
11403 by checking if we're pointing to another pointer, that one
11404 points to another pointer, that one does too, and we have no htab.
11405 If so, get a hash table. We check three levels deep to avoid
11406 the cost of the hash table if we don't need one. */
11407 if (POINTER_TYPE_P (TREE_TYPE (type))
11408 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (type)))
11409 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (TREE_TYPE (type))))
11410 && !pset)
11412 result = walk_tree_without_duplicates (&TREE_TYPE (type),
11413 func, data);
11414 if (result)
11415 return result;
11417 break;
11420 /* ... fall through ... */
11422 case COMPLEX_TYPE:
11423 WALK_SUBTREE (TREE_TYPE (type));
11424 break;
11426 case METHOD_TYPE:
11427 WALK_SUBTREE (TYPE_METHOD_BASETYPE (type));
11429 /* Fall through. */
11431 case FUNCTION_TYPE:
11432 WALK_SUBTREE (TREE_TYPE (type));
11434 tree arg;
11436 /* We never want to walk into default arguments. */
11437 for (arg = TYPE_ARG_TYPES (type); arg; arg = TREE_CHAIN (arg))
11438 WALK_SUBTREE (TREE_VALUE (arg));
11440 break;
11442 case ARRAY_TYPE:
11443 /* Don't follow this nodes's type if a pointer for fear that
11444 we'll have infinite recursion. If we have a PSET, then we
11445 need not fear. */
11446 if (pset
11447 || (!POINTER_TYPE_P (TREE_TYPE (type))
11448 && TREE_CODE (TREE_TYPE (type)) != OFFSET_TYPE))
11449 WALK_SUBTREE (TREE_TYPE (type));
11450 WALK_SUBTREE (TYPE_DOMAIN (type));
11451 break;
11453 case OFFSET_TYPE:
11454 WALK_SUBTREE (TREE_TYPE (type));
11455 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (type));
11456 break;
11458 default:
11459 break;
11462 return NULL_TREE;
11465 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal. FUNC is
11466 called with the DATA and the address of each sub-tree. If FUNC returns a
11467 non-NULL value, the traversal is stopped, and the value returned by FUNC
11468 is returned. If PSET is non-NULL it is used to record the nodes visited,
11469 and to avoid visiting a node more than once. */
11471 tree
11472 walk_tree_1 (tree *tp, walk_tree_fn func, void *data,
11473 hash_set<tree> *pset, walk_tree_lh lh)
11475 enum tree_code code;
11476 int walk_subtrees;
11477 tree result;
11479 #define WALK_SUBTREE_TAIL(NODE) \
11480 do \
11482 tp = & (NODE); \
11483 goto tail_recurse; \
11485 while (0)
11487 tail_recurse:
11488 /* Skip empty subtrees. */
11489 if (!*tp)
11490 return NULL_TREE;
11492 /* Don't walk the same tree twice, if the user has requested
11493 that we avoid doing so. */
11494 if (pset && pset->add (*tp))
11495 return NULL_TREE;
11497 /* Call the function. */
11498 walk_subtrees = 1;
11499 result = (*func) (tp, &walk_subtrees, data);
11501 /* If we found something, return it. */
11502 if (result)
11503 return result;
11505 code = TREE_CODE (*tp);
11507 /* Even if we didn't, FUNC may have decided that there was nothing
11508 interesting below this point in the tree. */
11509 if (!walk_subtrees)
11511 /* But we still need to check our siblings. */
11512 if (code == TREE_LIST)
11513 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
11514 else if (code == OMP_CLAUSE)
11515 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11516 else
11517 return NULL_TREE;
11520 if (lh)
11522 result = (*lh) (tp, &walk_subtrees, func, data, pset);
11523 if (result || !walk_subtrees)
11524 return result;
11527 switch (code)
11529 case ERROR_MARK:
11530 case IDENTIFIER_NODE:
11531 case INTEGER_CST:
11532 case REAL_CST:
11533 case FIXED_CST:
11534 case VECTOR_CST:
11535 case STRING_CST:
11536 case BLOCK:
11537 case PLACEHOLDER_EXPR:
11538 case SSA_NAME:
11539 case FIELD_DECL:
11540 case RESULT_DECL:
11541 /* None of these have subtrees other than those already walked
11542 above. */
11543 break;
11545 case TREE_LIST:
11546 WALK_SUBTREE (TREE_VALUE (*tp));
11547 WALK_SUBTREE_TAIL (TREE_CHAIN (*tp));
11548 break;
11550 case TREE_VEC:
11552 int len = TREE_VEC_LENGTH (*tp);
11554 if (len == 0)
11555 break;
11557 /* Walk all elements but the first. */
11558 while (--len)
11559 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
11561 /* Now walk the first one as a tail call. */
11562 WALK_SUBTREE_TAIL (TREE_VEC_ELT (*tp, 0));
11565 case COMPLEX_CST:
11566 WALK_SUBTREE (TREE_REALPART (*tp));
11567 WALK_SUBTREE_TAIL (TREE_IMAGPART (*tp));
11569 case CONSTRUCTOR:
11571 unsigned HOST_WIDE_INT idx;
11572 constructor_elt *ce;
11574 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (*tp), idx, &ce);
11575 idx++)
11576 WALK_SUBTREE (ce->value);
11578 break;
11580 case SAVE_EXPR:
11581 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, 0));
11583 case BIND_EXPR:
11585 tree decl;
11586 for (decl = BIND_EXPR_VARS (*tp); decl; decl = DECL_CHAIN (decl))
11588 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
11589 into declarations that are just mentioned, rather than
11590 declared; they don't really belong to this part of the tree.
11591 And, we can see cycles: the initializer for a declaration
11592 can refer to the declaration itself. */
11593 WALK_SUBTREE (DECL_INITIAL (decl));
11594 WALK_SUBTREE (DECL_SIZE (decl));
11595 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
11597 WALK_SUBTREE_TAIL (BIND_EXPR_BODY (*tp));
11600 case STATEMENT_LIST:
11602 tree_stmt_iterator i;
11603 for (i = tsi_start (*tp); !tsi_end_p (i); tsi_next (&i))
11604 WALK_SUBTREE (*tsi_stmt_ptr (i));
11606 break;
11608 case OMP_CLAUSE:
11609 switch (OMP_CLAUSE_CODE (*tp))
11611 case OMP_CLAUSE_GANG:
11612 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 1));
11613 /* FALLTHRU */
11615 case OMP_CLAUSE_DEVICE_RESIDENT:
11616 case OMP_CLAUSE_USE_DEVICE:
11617 case OMP_CLAUSE_ASYNC:
11618 case OMP_CLAUSE_WAIT:
11619 case OMP_CLAUSE_WORKER:
11620 case OMP_CLAUSE_VECTOR:
11621 case OMP_CLAUSE_NUM_GANGS:
11622 case OMP_CLAUSE_NUM_WORKERS:
11623 case OMP_CLAUSE_VECTOR_LENGTH:
11624 case OMP_CLAUSE_PRIVATE:
11625 case OMP_CLAUSE_SHARED:
11626 case OMP_CLAUSE_FIRSTPRIVATE:
11627 case OMP_CLAUSE_COPYIN:
11628 case OMP_CLAUSE_COPYPRIVATE:
11629 case OMP_CLAUSE_FINAL:
11630 case OMP_CLAUSE_IF:
11631 case OMP_CLAUSE_NUM_THREADS:
11632 case OMP_CLAUSE_SCHEDULE:
11633 case OMP_CLAUSE_UNIFORM:
11634 case OMP_CLAUSE_DEPEND:
11635 case OMP_CLAUSE_NUM_TEAMS:
11636 case OMP_CLAUSE_THREAD_LIMIT:
11637 case OMP_CLAUSE_DEVICE:
11638 case OMP_CLAUSE_DIST_SCHEDULE:
11639 case OMP_CLAUSE_SAFELEN:
11640 case OMP_CLAUSE_SIMDLEN:
11641 case OMP_CLAUSE_ORDERED:
11642 case OMP_CLAUSE_PRIORITY:
11643 case OMP_CLAUSE_GRAINSIZE:
11644 case OMP_CLAUSE_NUM_TASKS:
11645 case OMP_CLAUSE_HINT:
11646 case OMP_CLAUSE_TO_DECLARE:
11647 case OMP_CLAUSE_LINK:
11648 case OMP_CLAUSE_USE_DEVICE_PTR:
11649 case OMP_CLAUSE_IS_DEVICE_PTR:
11650 case OMP_CLAUSE__LOOPTEMP_:
11651 case OMP_CLAUSE__SIMDUID_:
11652 case OMP_CLAUSE__CILK_FOR_COUNT_:
11653 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 0));
11654 /* FALLTHRU */
11656 case OMP_CLAUSE_INDEPENDENT:
11657 case OMP_CLAUSE_NOWAIT:
11658 case OMP_CLAUSE_DEFAULT:
11659 case OMP_CLAUSE_UNTIED:
11660 case OMP_CLAUSE_MERGEABLE:
11661 case OMP_CLAUSE_PROC_BIND:
11662 case OMP_CLAUSE_INBRANCH:
11663 case OMP_CLAUSE_NOTINBRANCH:
11664 case OMP_CLAUSE_FOR:
11665 case OMP_CLAUSE_PARALLEL:
11666 case OMP_CLAUSE_SECTIONS:
11667 case OMP_CLAUSE_TASKGROUP:
11668 case OMP_CLAUSE_NOGROUP:
11669 case OMP_CLAUSE_THREADS:
11670 case OMP_CLAUSE_SIMD:
11671 case OMP_CLAUSE_DEFAULTMAP:
11672 case OMP_CLAUSE_AUTO:
11673 case OMP_CLAUSE_SEQ:
11674 case OMP_CLAUSE_TILE:
11675 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11677 case OMP_CLAUSE_LASTPRIVATE:
11678 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11679 WALK_SUBTREE (OMP_CLAUSE_LASTPRIVATE_STMT (*tp));
11680 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11682 case OMP_CLAUSE_COLLAPSE:
11684 int i;
11685 for (i = 0; i < 3; i++)
11686 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
11687 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11690 case OMP_CLAUSE_LINEAR:
11691 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11692 WALK_SUBTREE (OMP_CLAUSE_LINEAR_STEP (*tp));
11693 WALK_SUBTREE (OMP_CLAUSE_LINEAR_STMT (*tp));
11694 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11696 case OMP_CLAUSE_ALIGNED:
11697 case OMP_CLAUSE_FROM:
11698 case OMP_CLAUSE_TO:
11699 case OMP_CLAUSE_MAP:
11700 case OMP_CLAUSE__CACHE_:
11701 WALK_SUBTREE (OMP_CLAUSE_DECL (*tp));
11702 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, 1));
11703 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11705 case OMP_CLAUSE_REDUCTION:
11707 int i;
11708 for (i = 0; i < 5; i++)
11709 WALK_SUBTREE (OMP_CLAUSE_OPERAND (*tp, i));
11710 WALK_SUBTREE_TAIL (OMP_CLAUSE_CHAIN (*tp));
11713 default:
11714 gcc_unreachable ();
11716 break;
11718 case TARGET_EXPR:
11720 int i, len;
11722 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
11723 But, we only want to walk once. */
11724 len = (TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1)) ? 2 : 3;
11725 for (i = 0; i < len; ++i)
11726 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11727 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len));
11730 case DECL_EXPR:
11731 /* If this is a TYPE_DECL, walk into the fields of the type that it's
11732 defining. We only want to walk into these fields of a type in this
11733 case and not in the general case of a mere reference to the type.
11735 The criterion is as follows: if the field can be an expression, it
11736 must be walked only here. This should be in keeping with the fields
11737 that are directly gimplified in gimplify_type_sizes in order for the
11738 mark/copy-if-shared/unmark machinery of the gimplifier to work with
11739 variable-sized types.
11741 Note that DECLs get walked as part of processing the BIND_EXPR. */
11742 if (TREE_CODE (DECL_EXPR_DECL (*tp)) == TYPE_DECL)
11744 tree *type_p = &TREE_TYPE (DECL_EXPR_DECL (*tp));
11745 if (TREE_CODE (*type_p) == ERROR_MARK)
11746 return NULL_TREE;
11748 /* Call the function for the type. See if it returns anything or
11749 doesn't want us to continue. If we are to continue, walk both
11750 the normal fields and those for the declaration case. */
11751 result = (*func) (type_p, &walk_subtrees, data);
11752 if (result || !walk_subtrees)
11753 return result;
11755 /* But do not walk a pointed-to type since it may itself need to
11756 be walked in the declaration case if it isn't anonymous. */
11757 if (!POINTER_TYPE_P (*type_p))
11759 result = walk_type_fields (*type_p, func, data, pset, lh);
11760 if (result)
11761 return result;
11764 /* If this is a record type, also walk the fields. */
11765 if (RECORD_OR_UNION_TYPE_P (*type_p))
11767 tree field;
11769 for (field = TYPE_FIELDS (*type_p); field;
11770 field = DECL_CHAIN (field))
11772 /* We'd like to look at the type of the field, but we can
11773 easily get infinite recursion. So assume it's pointed
11774 to elsewhere in the tree. Also, ignore things that
11775 aren't fields. */
11776 if (TREE_CODE (field) != FIELD_DECL)
11777 continue;
11779 WALK_SUBTREE (DECL_FIELD_OFFSET (field));
11780 WALK_SUBTREE (DECL_SIZE (field));
11781 WALK_SUBTREE (DECL_SIZE_UNIT (field));
11782 if (TREE_CODE (*type_p) == QUAL_UNION_TYPE)
11783 WALK_SUBTREE (DECL_QUALIFIER (field));
11787 /* Same for scalar types. */
11788 else if (TREE_CODE (*type_p) == BOOLEAN_TYPE
11789 || TREE_CODE (*type_p) == ENUMERAL_TYPE
11790 || TREE_CODE (*type_p) == INTEGER_TYPE
11791 || TREE_CODE (*type_p) == FIXED_POINT_TYPE
11792 || TREE_CODE (*type_p) == REAL_TYPE)
11794 WALK_SUBTREE (TYPE_MIN_VALUE (*type_p));
11795 WALK_SUBTREE (TYPE_MAX_VALUE (*type_p));
11798 WALK_SUBTREE (TYPE_SIZE (*type_p));
11799 WALK_SUBTREE_TAIL (TYPE_SIZE_UNIT (*type_p));
11801 /* FALLTHRU */
11803 default:
11804 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
11806 int i, len;
11808 /* Walk over all the sub-trees of this operand. */
11809 len = TREE_OPERAND_LENGTH (*tp);
11811 /* Go through the subtrees. We need to do this in forward order so
11812 that the scope of a FOR_EXPR is handled properly. */
11813 if (len)
11815 for (i = 0; i < len - 1; ++i)
11816 WALK_SUBTREE (TREE_OPERAND (*tp, i));
11817 WALK_SUBTREE_TAIL (TREE_OPERAND (*tp, len - 1));
11820 /* If this is a type, walk the needed fields in the type. */
11821 else if (TYPE_P (*tp))
11822 return walk_type_fields (*tp, func, data, pset, lh);
11823 break;
11826 /* We didn't find what we were looking for. */
11827 return NULL_TREE;
11829 #undef WALK_SUBTREE_TAIL
11831 #undef WALK_SUBTREE
11833 /* Like walk_tree, but does not walk duplicate nodes more than once. */
11835 tree
11836 walk_tree_without_duplicates_1 (tree *tp, walk_tree_fn func, void *data,
11837 walk_tree_lh lh)
11839 tree result;
11841 hash_set<tree> pset;
11842 result = walk_tree_1 (tp, func, data, &pset, lh);
11843 return result;
11847 tree
11848 tree_block (tree t)
11850 const enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
11852 if (IS_EXPR_CODE_CLASS (c))
11853 return LOCATION_BLOCK (t->exp.locus);
11854 gcc_unreachable ();
11855 return NULL;
11858 void
11859 tree_set_block (tree t, tree b)
11861 const enum tree_code_class c = TREE_CODE_CLASS (TREE_CODE (t));
11863 if (IS_EXPR_CODE_CLASS (c))
11865 t->exp.locus = set_block (t->exp.locus, b);
11867 else
11868 gcc_unreachable ();
11871 /* Create a nameless artificial label and put it in the current
11872 function context. The label has a location of LOC. Returns the
11873 newly created label. */
11875 tree
11876 create_artificial_label (location_t loc)
11878 tree lab = build_decl (loc,
11879 LABEL_DECL, NULL_TREE, void_type_node);
11881 DECL_ARTIFICIAL (lab) = 1;
11882 DECL_IGNORED_P (lab) = 1;
11883 DECL_CONTEXT (lab) = current_function_decl;
11884 return lab;
11887 /* Given a tree, try to return a useful variable name that we can use
11888 to prefix a temporary that is being assigned the value of the tree.
11889 I.E. given <temp> = &A, return A. */
11891 const char *
11892 get_name (tree t)
11894 tree stripped_decl;
11896 stripped_decl = t;
11897 STRIP_NOPS (stripped_decl);
11898 if (DECL_P (stripped_decl) && DECL_NAME (stripped_decl))
11899 return IDENTIFIER_POINTER (DECL_NAME (stripped_decl));
11900 else if (TREE_CODE (stripped_decl) == SSA_NAME)
11902 tree name = SSA_NAME_IDENTIFIER (stripped_decl);
11903 if (!name)
11904 return NULL;
11905 return IDENTIFIER_POINTER (name);
11907 else
11909 switch (TREE_CODE (stripped_decl))
11911 case ADDR_EXPR:
11912 return get_name (TREE_OPERAND (stripped_decl, 0));
11913 default:
11914 return NULL;
11919 /* Return true if TYPE has a variable argument list. */
11921 bool
11922 stdarg_p (const_tree fntype)
11924 function_args_iterator args_iter;
11925 tree n = NULL_TREE, t;
11927 if (!fntype)
11928 return false;
11930 FOREACH_FUNCTION_ARGS (fntype, t, args_iter)
11932 n = t;
11935 return n != NULL_TREE && n != void_type_node;
11938 /* Return true if TYPE has a prototype. */
11940 bool
11941 prototype_p (const_tree fntype)
11943 tree t;
11945 gcc_assert (fntype != NULL_TREE);
11947 t = TYPE_ARG_TYPES (fntype);
11948 return (t != NULL_TREE);
11951 /* If BLOCK is inlined from an __attribute__((__artificial__))
11952 routine, return pointer to location from where it has been
11953 called. */
11954 location_t *
11955 block_nonartificial_location (tree block)
11957 location_t *ret = NULL;
11959 while (block && TREE_CODE (block) == BLOCK
11960 && BLOCK_ABSTRACT_ORIGIN (block))
11962 tree ao = BLOCK_ABSTRACT_ORIGIN (block);
11964 while (TREE_CODE (ao) == BLOCK
11965 && BLOCK_ABSTRACT_ORIGIN (ao)
11966 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
11967 ao = BLOCK_ABSTRACT_ORIGIN (ao);
11969 if (TREE_CODE (ao) == FUNCTION_DECL)
11971 /* If AO is an artificial inline, point RET to the
11972 call site locus at which it has been inlined and continue
11973 the loop, in case AO's caller is also an artificial
11974 inline. */
11975 if (DECL_DECLARED_INLINE_P (ao)
11976 && lookup_attribute ("artificial", DECL_ATTRIBUTES (ao)))
11977 ret = &BLOCK_SOURCE_LOCATION (block);
11978 else
11979 break;
11981 else if (TREE_CODE (ao) != BLOCK)
11982 break;
11984 block = BLOCK_SUPERCONTEXT (block);
11986 return ret;
11990 /* If EXP is inlined from an __attribute__((__artificial__))
11991 function, return the location of the original call expression. */
11993 location_t
11994 tree_nonartificial_location (tree exp)
11996 location_t *loc = block_nonartificial_location (TREE_BLOCK (exp));
11998 if (loc)
11999 return *loc;
12000 else
12001 return EXPR_LOCATION (exp);
12005 /* These are the hash table functions for the hash table of OPTIMIZATION_NODEq
12006 nodes. */
12008 /* Return the hash code X, an OPTIMIZATION_NODE or TARGET_OPTION code. */
12010 hashval_t
12011 cl_option_hasher::hash (tree x)
12013 const_tree const t = x;
12014 const char *p;
12015 size_t i;
12016 size_t len = 0;
12017 hashval_t hash = 0;
12019 if (TREE_CODE (t) == OPTIMIZATION_NODE)
12021 p = (const char *)TREE_OPTIMIZATION (t);
12022 len = sizeof (struct cl_optimization);
12025 else if (TREE_CODE (t) == TARGET_OPTION_NODE)
12026 return cl_target_option_hash (TREE_TARGET_OPTION (t));
12028 else
12029 gcc_unreachable ();
12031 /* assume most opt flags are just 0/1, some are 2-3, and a few might be
12032 something else. */
12033 for (i = 0; i < len; i++)
12034 if (p[i])
12035 hash = (hash << 4) ^ ((i << 2) | p[i]);
12037 return hash;
12040 /* Return nonzero if the value represented by *X (an OPTIMIZATION or
12041 TARGET_OPTION tree node) is the same as that given by *Y, which is the
12042 same. */
12044 bool
12045 cl_option_hasher::equal (tree x, tree y)
12047 const_tree const xt = x;
12048 const_tree const yt = y;
12049 const char *xp;
12050 const char *yp;
12051 size_t len;
12053 if (TREE_CODE (xt) != TREE_CODE (yt))
12054 return 0;
12056 if (TREE_CODE (xt) == OPTIMIZATION_NODE)
12058 xp = (const char *)TREE_OPTIMIZATION (xt);
12059 yp = (const char *)TREE_OPTIMIZATION (yt);
12060 len = sizeof (struct cl_optimization);
12063 else if (TREE_CODE (xt) == TARGET_OPTION_NODE)
12065 return cl_target_option_eq (TREE_TARGET_OPTION (xt),
12066 TREE_TARGET_OPTION (yt));
12069 else
12070 gcc_unreachable ();
12072 return (memcmp (xp, yp, len) == 0);
12075 /* Build an OPTIMIZATION_NODE based on the options in OPTS. */
12077 tree
12078 build_optimization_node (struct gcc_options *opts)
12080 tree t;
12082 /* Use the cache of optimization nodes. */
12084 cl_optimization_save (TREE_OPTIMIZATION (cl_optimization_node),
12085 opts);
12087 tree *slot = cl_option_hash_table->find_slot (cl_optimization_node, INSERT);
12088 t = *slot;
12089 if (!t)
12091 /* Insert this one into the hash table. */
12092 t = cl_optimization_node;
12093 *slot = t;
12095 /* Make a new node for next time round. */
12096 cl_optimization_node = make_node (OPTIMIZATION_NODE);
12099 return t;
12102 /* Build a TARGET_OPTION_NODE based on the options in OPTS. */
12104 tree
12105 build_target_option_node (struct gcc_options *opts)
12107 tree t;
12109 /* Use the cache of optimization nodes. */
12111 cl_target_option_save (TREE_TARGET_OPTION (cl_target_option_node),
12112 opts);
12114 tree *slot = cl_option_hash_table->find_slot (cl_target_option_node, INSERT);
12115 t = *slot;
12116 if (!t)
12118 /* Insert this one into the hash table. */
12119 t = cl_target_option_node;
12120 *slot = t;
12122 /* Make a new node for next time round. */
12123 cl_target_option_node = make_node (TARGET_OPTION_NODE);
12126 return t;
12129 /* Clear TREE_TARGET_GLOBALS of all TARGET_OPTION_NODE trees,
12130 so that they aren't saved during PCH writing. */
12132 void
12133 prepare_target_option_nodes_for_pch (void)
12135 hash_table<cl_option_hasher>::iterator iter = cl_option_hash_table->begin ();
12136 for (; iter != cl_option_hash_table->end (); ++iter)
12137 if (TREE_CODE (*iter) == TARGET_OPTION_NODE)
12138 TREE_TARGET_GLOBALS (*iter) = NULL;
12141 /* Determine the "ultimate origin" of a block. The block may be an inlined
12142 instance of an inlined instance of a block which is local to an inline
12143 function, so we have to trace all of the way back through the origin chain
12144 to find out what sort of node actually served as the original seed for the
12145 given block. */
12147 tree
12148 block_ultimate_origin (const_tree block)
12150 tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
12152 /* BLOCK_ABSTRACT_ORIGIN can point to itself; ignore that if
12153 we're trying to output the abstract instance of this function. */
12154 if (BLOCK_ABSTRACT (block) && immediate_origin == block)
12155 return NULL_TREE;
12157 if (immediate_origin == NULL_TREE)
12158 return NULL_TREE;
12159 else
12161 tree ret_val;
12162 tree lookahead = immediate_origin;
12166 ret_val = lookahead;
12167 lookahead = (TREE_CODE (ret_val) == BLOCK
12168 ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
12170 while (lookahead != NULL && lookahead != ret_val);
12172 /* The block's abstract origin chain may not be the *ultimate* origin of
12173 the block. It could lead to a DECL that has an abstract origin set.
12174 If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
12175 will give us if it has one). Note that DECL's abstract origins are
12176 supposed to be the most distant ancestor (or so decl_ultimate_origin
12177 claims), so we don't need to loop following the DECL origins. */
12178 if (DECL_P (ret_val))
12179 return DECL_ORIGIN (ret_val);
12181 return ret_val;
12185 /* Return true iff conversion from INNER_TYPE to OUTER_TYPE generates
12186 no instruction. */
12188 bool
12189 tree_nop_conversion_p (const_tree outer_type, const_tree inner_type)
12191 /* Use precision rather then machine mode when we can, which gives
12192 the correct answer even for submode (bit-field) types. */
12193 if ((INTEGRAL_TYPE_P (outer_type)
12194 || POINTER_TYPE_P (outer_type)
12195 || TREE_CODE (outer_type) == OFFSET_TYPE)
12196 && (INTEGRAL_TYPE_P (inner_type)
12197 || POINTER_TYPE_P (inner_type)
12198 || TREE_CODE (inner_type) == OFFSET_TYPE))
12199 return TYPE_PRECISION (outer_type) == TYPE_PRECISION (inner_type);
12201 /* Otherwise fall back on comparing machine modes (e.g. for
12202 aggregate types, floats). */
12203 return TYPE_MODE (outer_type) == TYPE_MODE (inner_type);
12206 /* Return true iff conversion in EXP generates no instruction. Mark
12207 it inline so that we fully inline into the stripping functions even
12208 though we have two uses of this function. */
12210 static inline bool
12211 tree_nop_conversion (const_tree exp)
12213 tree outer_type, inner_type;
12215 if (!CONVERT_EXPR_P (exp)
12216 && TREE_CODE (exp) != NON_LVALUE_EXPR)
12217 return false;
12218 if (TREE_OPERAND (exp, 0) == error_mark_node)
12219 return false;
12221 outer_type = TREE_TYPE (exp);
12222 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
12224 if (!inner_type)
12225 return false;
12227 return tree_nop_conversion_p (outer_type, inner_type);
12230 /* Return true iff conversion in EXP generates no instruction. Don't
12231 consider conversions changing the signedness. */
12233 static bool
12234 tree_sign_nop_conversion (const_tree exp)
12236 tree outer_type, inner_type;
12238 if (!tree_nop_conversion (exp))
12239 return false;
12241 outer_type = TREE_TYPE (exp);
12242 inner_type = TREE_TYPE (TREE_OPERAND (exp, 0));
12244 return (TYPE_UNSIGNED (outer_type) == TYPE_UNSIGNED (inner_type)
12245 && POINTER_TYPE_P (outer_type) == POINTER_TYPE_P (inner_type));
12248 /* Strip conversions from EXP according to tree_nop_conversion and
12249 return the resulting expression. */
12251 tree
12252 tree_strip_nop_conversions (tree exp)
12254 while (tree_nop_conversion (exp))
12255 exp = TREE_OPERAND (exp, 0);
12256 return exp;
12259 /* Strip conversions from EXP according to tree_sign_nop_conversion
12260 and return the resulting expression. */
12262 tree
12263 tree_strip_sign_nop_conversions (tree exp)
12265 while (tree_sign_nop_conversion (exp))
12266 exp = TREE_OPERAND (exp, 0);
12267 return exp;
12270 /* Avoid any floating point extensions from EXP. */
12271 tree
12272 strip_float_extensions (tree exp)
12274 tree sub, expt, subt;
12276 /* For floating point constant look up the narrowest type that can hold
12277 it properly and handle it like (type)(narrowest_type)constant.
12278 This way we can optimize for instance a=a*2.0 where "a" is float
12279 but 2.0 is double constant. */
12280 if (TREE_CODE (exp) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (exp)))
12282 REAL_VALUE_TYPE orig;
12283 tree type = NULL;
12285 orig = TREE_REAL_CST (exp);
12286 if (TYPE_PRECISION (TREE_TYPE (exp)) > TYPE_PRECISION (float_type_node)
12287 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
12288 type = float_type_node;
12289 else if (TYPE_PRECISION (TREE_TYPE (exp))
12290 > TYPE_PRECISION (double_type_node)
12291 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
12292 type = double_type_node;
12293 if (type)
12294 return build_real_truncate (type, orig);
12297 if (!CONVERT_EXPR_P (exp))
12298 return exp;
12300 sub = TREE_OPERAND (exp, 0);
12301 subt = TREE_TYPE (sub);
12302 expt = TREE_TYPE (exp);
12304 if (!FLOAT_TYPE_P (subt))
12305 return exp;
12307 if (DECIMAL_FLOAT_TYPE_P (expt) != DECIMAL_FLOAT_TYPE_P (subt))
12308 return exp;
12310 if (TYPE_PRECISION (subt) > TYPE_PRECISION (expt))
12311 return exp;
12313 return strip_float_extensions (sub);
12316 /* Strip out all handled components that produce invariant
12317 offsets. */
12319 const_tree
12320 strip_invariant_refs (const_tree op)
12322 while (handled_component_p (op))
12324 switch (TREE_CODE (op))
12326 case ARRAY_REF:
12327 case ARRAY_RANGE_REF:
12328 if (!is_gimple_constant (TREE_OPERAND (op, 1))
12329 || TREE_OPERAND (op, 2) != NULL_TREE
12330 || TREE_OPERAND (op, 3) != NULL_TREE)
12331 return NULL;
12332 break;
12334 case COMPONENT_REF:
12335 if (TREE_OPERAND (op, 2) != NULL_TREE)
12336 return NULL;
12337 break;
12339 default:;
12341 op = TREE_OPERAND (op, 0);
12344 return op;
12347 static GTY(()) tree gcc_eh_personality_decl;
12349 /* Return the GCC personality function decl. */
12351 tree
12352 lhd_gcc_personality (void)
12354 if (!gcc_eh_personality_decl)
12355 gcc_eh_personality_decl = build_personality_function ("gcc");
12356 return gcc_eh_personality_decl;
12359 /* TARGET is a call target of GIMPLE call statement
12360 (obtained by gimple_call_fn). Return true if it is
12361 OBJ_TYPE_REF representing an virtual call of C++ method.
12362 (As opposed to OBJ_TYPE_REF representing objc calls
12363 through a cast where middle-end devirtualization machinery
12364 can't apply.) */
12366 bool
12367 virtual_method_call_p (const_tree target)
12369 if (TREE_CODE (target) != OBJ_TYPE_REF)
12370 return false;
12371 tree t = TREE_TYPE (target);
12372 gcc_checking_assert (TREE_CODE (t) == POINTER_TYPE);
12373 t = TREE_TYPE (t);
12374 if (TREE_CODE (t) == FUNCTION_TYPE)
12375 return false;
12376 gcc_checking_assert (TREE_CODE (t) == METHOD_TYPE);
12377 /* If we do not have BINFO associated, it means that type was built
12378 without devirtualization enabled. Do not consider this a virtual
12379 call. */
12380 if (!TYPE_BINFO (obj_type_ref_class (target)))
12381 return false;
12382 return true;
12385 /* REF is OBJ_TYPE_REF, return the class the ref corresponds to. */
12387 tree
12388 obj_type_ref_class (const_tree ref)
12390 gcc_checking_assert (TREE_CODE (ref) == OBJ_TYPE_REF);
12391 ref = TREE_TYPE (ref);
12392 gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE);
12393 ref = TREE_TYPE (ref);
12394 /* We look for type THIS points to. ObjC also builds
12395 OBJ_TYPE_REF with non-method calls, Their first parameter
12396 ID however also corresponds to class type. */
12397 gcc_checking_assert (TREE_CODE (ref) == METHOD_TYPE
12398 || TREE_CODE (ref) == FUNCTION_TYPE);
12399 ref = TREE_VALUE (TYPE_ARG_TYPES (ref));
12400 gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE);
12401 return TREE_TYPE (ref);
12404 /* Lookup sub-BINFO of BINFO of TYPE at offset POS. */
12406 static tree
12407 lookup_binfo_at_offset (tree binfo, tree type, HOST_WIDE_INT pos)
12409 unsigned int i;
12410 tree base_binfo, b;
12412 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
12413 if (pos == tree_to_shwi (BINFO_OFFSET (base_binfo))
12414 && types_same_for_odr (TREE_TYPE (base_binfo), type))
12415 return base_binfo;
12416 else if ((b = lookup_binfo_at_offset (base_binfo, type, pos)) != NULL)
12417 return b;
12418 return NULL;
12421 /* Try to find a base info of BINFO that would have its field decl at offset
12422 OFFSET within the BINFO type and which is of EXPECTED_TYPE. If it can be
12423 found, return, otherwise return NULL_TREE. */
12425 tree
12426 get_binfo_at_offset (tree binfo, HOST_WIDE_INT offset, tree expected_type)
12428 tree type = BINFO_TYPE (binfo);
12430 while (true)
12432 HOST_WIDE_INT pos, size;
12433 tree fld;
12434 int i;
12436 if (types_same_for_odr (type, expected_type))
12437 return binfo;
12438 if (offset < 0)
12439 return NULL_TREE;
12441 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
12443 if (TREE_CODE (fld) != FIELD_DECL || !DECL_ARTIFICIAL (fld))
12444 continue;
12446 pos = int_bit_position (fld);
12447 size = tree_to_uhwi (DECL_SIZE (fld));
12448 if (pos <= offset && (pos + size) > offset)
12449 break;
12451 if (!fld || TREE_CODE (TREE_TYPE (fld)) != RECORD_TYPE)
12452 return NULL_TREE;
12454 /* Offset 0 indicates the primary base, whose vtable contents are
12455 represented in the binfo for the derived class. */
12456 else if (offset != 0)
12458 tree found_binfo = NULL, base_binfo;
12459 /* Offsets in BINFO are in bytes relative to the whole structure
12460 while POS is in bits relative to the containing field. */
12461 int binfo_offset = (tree_to_shwi (BINFO_OFFSET (binfo)) + pos
12462 / BITS_PER_UNIT);
12464 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
12465 if (tree_to_shwi (BINFO_OFFSET (base_binfo)) == binfo_offset
12466 && types_same_for_odr (TREE_TYPE (base_binfo), TREE_TYPE (fld)))
12468 found_binfo = base_binfo;
12469 break;
12471 if (found_binfo)
12472 binfo = found_binfo;
12473 else
12474 binfo = lookup_binfo_at_offset (binfo, TREE_TYPE (fld),
12475 binfo_offset);
12478 type = TREE_TYPE (fld);
12479 offset -= pos;
12483 /* Returns true if X is a typedef decl. */
12485 bool
12486 is_typedef_decl (const_tree x)
12488 return (x && TREE_CODE (x) == TYPE_DECL
12489 && DECL_ORIGINAL_TYPE (x) != NULL_TREE);
12492 /* Returns true iff TYPE is a type variant created for a typedef. */
12494 bool
12495 typedef_variant_p (const_tree type)
12497 return is_typedef_decl (TYPE_NAME (type));
12500 /* Warn about a use of an identifier which was marked deprecated. */
12501 void
12502 warn_deprecated_use (tree node, tree attr)
12504 const char *msg;
12506 if (node == 0 || !warn_deprecated_decl)
12507 return;
12509 if (!attr)
12511 if (DECL_P (node))
12512 attr = DECL_ATTRIBUTES (node);
12513 else if (TYPE_P (node))
12515 tree decl = TYPE_STUB_DECL (node);
12516 if (decl)
12517 attr = lookup_attribute ("deprecated",
12518 TYPE_ATTRIBUTES (TREE_TYPE (decl)));
12522 if (attr)
12523 attr = lookup_attribute ("deprecated", attr);
12525 if (attr)
12526 msg = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr)));
12527 else
12528 msg = NULL;
12530 bool w;
12531 if (DECL_P (node))
12533 if (msg)
12534 w = warning (OPT_Wdeprecated_declarations,
12535 "%qD is deprecated: %s", node, msg);
12536 else
12537 w = warning (OPT_Wdeprecated_declarations,
12538 "%qD is deprecated", node);
12539 if (w)
12540 inform (DECL_SOURCE_LOCATION (node), "declared here");
12542 else if (TYPE_P (node))
12544 tree what = NULL_TREE;
12545 tree decl = TYPE_STUB_DECL (node);
12547 if (TYPE_NAME (node))
12549 if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
12550 what = TYPE_NAME (node);
12551 else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
12552 && DECL_NAME (TYPE_NAME (node)))
12553 what = DECL_NAME (TYPE_NAME (node));
12556 if (decl)
12558 if (what)
12560 if (msg)
12561 w = warning (OPT_Wdeprecated_declarations,
12562 "%qE is deprecated: %s", what, msg);
12563 else
12564 w = warning (OPT_Wdeprecated_declarations,
12565 "%qE is deprecated", what);
12567 else
12569 if (msg)
12570 w = warning (OPT_Wdeprecated_declarations,
12571 "type is deprecated: %s", msg);
12572 else
12573 w = warning (OPT_Wdeprecated_declarations,
12574 "type is deprecated");
12576 if (w)
12577 inform (DECL_SOURCE_LOCATION (decl), "declared here");
12579 else
12581 if (what)
12583 if (msg)
12584 warning (OPT_Wdeprecated_declarations, "%qE is deprecated: %s",
12585 what, msg);
12586 else
12587 warning (OPT_Wdeprecated_declarations, "%qE is deprecated", what);
12589 else
12591 if (msg)
12592 warning (OPT_Wdeprecated_declarations, "type is deprecated: %s",
12593 msg);
12594 else
12595 warning (OPT_Wdeprecated_declarations, "type is deprecated");
12601 /* Return true if REF has a COMPONENT_REF with a bit-field field declaration
12602 somewhere in it. */
12604 bool
12605 contains_bitfld_component_ref_p (const_tree ref)
12607 while (handled_component_p (ref))
12609 if (TREE_CODE (ref) == COMPONENT_REF
12610 && DECL_BIT_FIELD (TREE_OPERAND (ref, 1)))
12611 return true;
12612 ref = TREE_OPERAND (ref, 0);
12615 return false;
12618 /* Try to determine whether a TRY_CATCH expression can fall through.
12619 This is a subroutine of block_may_fallthru. */
12621 static bool
12622 try_catch_may_fallthru (const_tree stmt)
12624 tree_stmt_iterator i;
12626 /* If the TRY block can fall through, the whole TRY_CATCH can
12627 fall through. */
12628 if (block_may_fallthru (TREE_OPERAND (stmt, 0)))
12629 return true;
12631 i = tsi_start (TREE_OPERAND (stmt, 1));
12632 switch (TREE_CODE (tsi_stmt (i)))
12634 case CATCH_EXPR:
12635 /* We expect to see a sequence of CATCH_EXPR trees, each with a
12636 catch expression and a body. The whole TRY_CATCH may fall
12637 through iff any of the catch bodies falls through. */
12638 for (; !tsi_end_p (i); tsi_next (&i))
12640 if (block_may_fallthru (CATCH_BODY (tsi_stmt (i))))
12641 return true;
12643 return false;
12645 case EH_FILTER_EXPR:
12646 /* The exception filter expression only matters if there is an
12647 exception. If the exception does not match EH_FILTER_TYPES,
12648 we will execute EH_FILTER_FAILURE, and we will fall through
12649 if that falls through. If the exception does match
12650 EH_FILTER_TYPES, the stack unwinder will continue up the
12651 stack, so we will not fall through. We don't know whether we
12652 will throw an exception which matches EH_FILTER_TYPES or not,
12653 so we just ignore EH_FILTER_TYPES and assume that we might
12654 throw an exception which doesn't match. */
12655 return block_may_fallthru (EH_FILTER_FAILURE (tsi_stmt (i)));
12657 default:
12658 /* This case represents statements to be executed when an
12659 exception occurs. Those statements are implicitly followed
12660 by a RESX statement to resume execution after the exception.
12661 So in this case the TRY_CATCH never falls through. */
12662 return false;
12666 /* Try to determine if we can fall out of the bottom of BLOCK. This guess
12667 need not be 100% accurate; simply be conservative and return true if we
12668 don't know. This is used only to avoid stupidly generating extra code.
12669 If we're wrong, we'll just delete the extra code later. */
12671 bool
12672 block_may_fallthru (const_tree block)
12674 /* This CONST_CAST is okay because expr_last returns its argument
12675 unmodified and we assign it to a const_tree. */
12676 const_tree stmt = expr_last (CONST_CAST_TREE (block));
12678 switch (stmt ? TREE_CODE (stmt) : ERROR_MARK)
12680 case GOTO_EXPR:
12681 case RETURN_EXPR:
12682 /* Easy cases. If the last statement of the block implies
12683 control transfer, then we can't fall through. */
12684 return false;
12686 case SWITCH_EXPR:
12687 /* If SWITCH_LABELS is set, this is lowered, and represents a
12688 branch to a selected label and hence can not fall through.
12689 Otherwise SWITCH_BODY is set, and the switch can fall
12690 through. */
12691 return SWITCH_LABELS (stmt) == NULL_TREE;
12693 case COND_EXPR:
12694 if (block_may_fallthru (COND_EXPR_THEN (stmt)))
12695 return true;
12696 return block_may_fallthru (COND_EXPR_ELSE (stmt));
12698 case BIND_EXPR:
12699 return block_may_fallthru (BIND_EXPR_BODY (stmt));
12701 case TRY_CATCH_EXPR:
12702 return try_catch_may_fallthru (stmt);
12704 case TRY_FINALLY_EXPR:
12705 /* The finally clause is always executed after the try clause,
12706 so if it does not fall through, then the try-finally will not
12707 fall through. Otherwise, if the try clause does not fall
12708 through, then when the finally clause falls through it will
12709 resume execution wherever the try clause was going. So the
12710 whole try-finally will only fall through if both the try
12711 clause and the finally clause fall through. */
12712 return (block_may_fallthru (TREE_OPERAND (stmt, 0))
12713 && block_may_fallthru (TREE_OPERAND (stmt, 1)));
12715 case MODIFY_EXPR:
12716 if (TREE_CODE (TREE_OPERAND (stmt, 1)) == CALL_EXPR)
12717 stmt = TREE_OPERAND (stmt, 1);
12718 else
12719 return true;
12720 /* FALLTHRU */
12722 case CALL_EXPR:
12723 /* Functions that do not return do not fall through. */
12724 return (call_expr_flags (stmt) & ECF_NORETURN) == 0;
12726 case CLEANUP_POINT_EXPR:
12727 return block_may_fallthru (TREE_OPERAND (stmt, 0));
12729 case TARGET_EXPR:
12730 return block_may_fallthru (TREE_OPERAND (stmt, 1));
12732 case ERROR_MARK:
12733 return true;
12735 default:
12736 return lang_hooks.block_may_fallthru (stmt);
12740 /* True if we are using EH to handle cleanups. */
12741 static bool using_eh_for_cleanups_flag = false;
12743 /* This routine is called from front ends to indicate eh should be used for
12744 cleanups. */
12745 void
12746 using_eh_for_cleanups (void)
12748 using_eh_for_cleanups_flag = true;
12751 /* Query whether EH is used for cleanups. */
12752 bool
12753 using_eh_for_cleanups_p (void)
12755 return using_eh_for_cleanups_flag;
12758 /* Wrapper for tree_code_name to ensure that tree code is valid */
12759 const char *
12760 get_tree_code_name (enum tree_code code)
12762 const char *invalid = "<invalid tree code>";
12764 if (code >= MAX_TREE_CODES)
12765 return invalid;
12767 return tree_code_name[code];
12770 /* Drops the TREE_OVERFLOW flag from T. */
12772 tree
12773 drop_tree_overflow (tree t)
12775 gcc_checking_assert (TREE_OVERFLOW (t));
12777 /* For tree codes with a sharing machinery re-build the result. */
12778 if (TREE_CODE (t) == INTEGER_CST)
12779 return wide_int_to_tree (TREE_TYPE (t), t);
12781 /* Otherwise, as all tcc_constants are possibly shared, copy the node
12782 and drop the flag. */
12783 t = copy_node (t);
12784 TREE_OVERFLOW (t) = 0;
12785 return t;
12788 /* Given a memory reference expression T, return its base address.
12789 The base address of a memory reference expression is the main
12790 object being referenced. For instance, the base address for
12791 'array[i].fld[j]' is 'array'. You can think of this as stripping
12792 away the offset part from a memory address.
12794 This function calls handled_component_p to strip away all the inner
12795 parts of the memory reference until it reaches the base object. */
12797 tree
12798 get_base_address (tree t)
12800 while (handled_component_p (t))
12801 t = TREE_OPERAND (t, 0);
12803 if ((TREE_CODE (t) == MEM_REF
12804 || TREE_CODE (t) == TARGET_MEM_REF)
12805 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR)
12806 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
12808 /* ??? Either the alias oracle or all callers need to properly deal
12809 with WITH_SIZE_EXPRs before we can look through those. */
12810 if (TREE_CODE (t) == WITH_SIZE_EXPR)
12811 return NULL_TREE;
12813 return t;
12816 /* Return a tree of sizetype representing the size, in bytes, of the element
12817 of EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
12819 tree
12820 array_ref_element_size (tree exp)
12822 tree aligned_size = TREE_OPERAND (exp, 3);
12823 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)));
12824 location_t loc = EXPR_LOCATION (exp);
12826 /* If a size was specified in the ARRAY_REF, it's the size measured
12827 in alignment units of the element type. So multiply by that value. */
12828 if (aligned_size)
12830 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
12831 sizetype from another type of the same width and signedness. */
12832 if (TREE_TYPE (aligned_size) != sizetype)
12833 aligned_size = fold_convert_loc (loc, sizetype, aligned_size);
12834 return size_binop_loc (loc, MULT_EXPR, aligned_size,
12835 size_int (TYPE_ALIGN_UNIT (elmt_type)));
12838 /* Otherwise, take the size from that of the element type. Substitute
12839 any PLACEHOLDER_EXPR that we have. */
12840 else
12841 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (elmt_type), exp);
12844 /* Return a tree representing the lower bound of the array mentioned in
12845 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
12847 tree
12848 array_ref_low_bound (tree exp)
12850 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
12852 /* If a lower bound is specified in EXP, use it. */
12853 if (TREE_OPERAND (exp, 2))
12854 return TREE_OPERAND (exp, 2);
12856 /* Otherwise, if there is a domain type and it has a lower bound, use it,
12857 substituting for a PLACEHOLDER_EXPR as needed. */
12858 if (domain_type && TYPE_MIN_VALUE (domain_type))
12859 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MIN_VALUE (domain_type), exp);
12861 /* Otherwise, return a zero of the appropriate type. */
12862 return build_int_cst (TREE_TYPE (TREE_OPERAND (exp, 1)), 0);
12865 /* Return a tree representing the upper bound of the array mentioned in
12866 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
12868 tree
12869 array_ref_up_bound (tree exp)
12871 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
12873 /* If there is a domain type and it has an upper bound, use it, substituting
12874 for a PLACEHOLDER_EXPR as needed. */
12875 if (domain_type && TYPE_MAX_VALUE (domain_type))
12876 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MAX_VALUE (domain_type), exp);
12878 /* Otherwise fail. */
12879 return NULL_TREE;
12882 /* Returns true if REF is an array reference to an array at the end of
12883 a structure. If this is the case, the array may be allocated larger
12884 than its upper bound implies. */
12886 bool
12887 array_at_struct_end_p (tree ref)
12889 if (TREE_CODE (ref) != ARRAY_REF
12890 && TREE_CODE (ref) != ARRAY_RANGE_REF)
12891 return false;
12893 while (handled_component_p (ref))
12895 /* If the reference chain contains a component reference to a
12896 non-union type and there follows another field the reference
12897 is not at the end of a structure. */
12898 if (TREE_CODE (ref) == COMPONENT_REF
12899 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
12901 tree nextf = DECL_CHAIN (TREE_OPERAND (ref, 1));
12902 while (nextf && TREE_CODE (nextf) != FIELD_DECL)
12903 nextf = DECL_CHAIN (nextf);
12904 if (nextf)
12905 return false;
12908 ref = TREE_OPERAND (ref, 0);
12911 /* If the reference is based on a declared entity, the size of the array
12912 is constrained by its given domain. */
12913 if (DECL_P (ref))
12914 return false;
12916 return true;
12919 /* Return a tree representing the offset, in bytes, of the field referenced
12920 by EXP. This does not include any offset in DECL_FIELD_BIT_OFFSET. */
12922 tree
12923 component_ref_field_offset (tree exp)
12925 tree aligned_offset = TREE_OPERAND (exp, 2);
12926 tree field = TREE_OPERAND (exp, 1);
12927 location_t loc = EXPR_LOCATION (exp);
12929 /* If an offset was specified in the COMPONENT_REF, it's the offset measured
12930 in units of DECL_OFFSET_ALIGN / BITS_PER_UNIT. So multiply by that
12931 value. */
12932 if (aligned_offset)
12934 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
12935 sizetype from another type of the same width and signedness. */
12936 if (TREE_TYPE (aligned_offset) != sizetype)
12937 aligned_offset = fold_convert_loc (loc, sizetype, aligned_offset);
12938 return size_binop_loc (loc, MULT_EXPR, aligned_offset,
12939 size_int (DECL_OFFSET_ALIGN (field)
12940 / BITS_PER_UNIT));
12943 /* Otherwise, take the offset from that of the field. Substitute
12944 any PLACEHOLDER_EXPR that we have. */
12945 else
12946 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (DECL_FIELD_OFFSET (field), exp);
12949 /* Return the machine mode of T. For vectors, returns the mode of the
12950 inner type. The main use case is to feed the result to HONOR_NANS,
12951 avoiding the BLKmode that a direct TYPE_MODE (T) might return. */
12953 machine_mode
12954 element_mode (const_tree t)
12956 if (!TYPE_P (t))
12957 t = TREE_TYPE (t);
12958 if (VECTOR_TYPE_P (t) || TREE_CODE (t) == COMPLEX_TYPE)
12959 t = TREE_TYPE (t);
12960 return TYPE_MODE (t);
12964 /* Veirfy that basic properties of T match TV and thus T can be a variant of
12965 TV. TV should be the more specified variant (i.e. the main variant). */
12967 static bool
12968 verify_type_variant (const_tree t, tree tv)
12970 /* Type variant can differ by:
12972 - TYPE_QUALS: TYPE_READONLY, TYPE_VOLATILE, TYPE_ATOMIC, TYPE_RESTRICT,
12973 ENCODE_QUAL_ADDR_SPACE.
12974 - main variant may be TYPE_COMPLETE_P and variant types !TYPE_COMPLETE_P
12975 in this case some values may not be set in the variant types
12976 (see TYPE_COMPLETE_P checks).
12977 - it is possible to have TYPE_ARTIFICIAL variant of non-artifical type
12978 - by TYPE_NAME and attributes (i.e. when variant originate by typedef)
12979 - TYPE_CANONICAL (TYPE_ALIAS_SET is the same among variants)
12980 - by the alignment: TYPE_ALIGN and TYPE_USER_ALIGN
12981 - during LTO by TYPE_CONTEXT if type is TYPE_FILE_SCOPE_P
12982 this is necessary to make it possible to merge types form different TUs
12983 - arrays, pointers and references may have TREE_TYPE that is a variant
12984 of TREE_TYPE of their main variants.
12985 - aggregates may have new TYPE_FIELDS list that list variants of
12986 the main variant TYPE_FIELDS.
12987 - vector types may differ by TYPE_VECTOR_OPAQUE
12988 - TYPE_METHODS is always NULL for vairant types and maintained for
12989 main variant only.
12992 /* Convenience macro for matching individual fields. */
12993 #define verify_variant_match(flag) \
12994 do { \
12995 if (flag (tv) != flag (t)) \
12997 error ("type variant differs by " #flag "."); \
12998 debug_tree (tv); \
12999 return false; \
13001 } while (false)
13003 /* tree_base checks. */
13005 verify_variant_match (TREE_CODE);
13006 /* FIXME: Ada builds non-artificial variants of artificial types. */
13007 if (TYPE_ARTIFICIAL (tv) && 0)
13008 verify_variant_match (TYPE_ARTIFICIAL);
13009 if (POINTER_TYPE_P (tv))
13010 verify_variant_match (TYPE_REF_CAN_ALIAS_ALL);
13011 /* FIXME: TYPE_SIZES_GIMPLIFIED may differs for Ada build. */
13012 verify_variant_match (TYPE_UNSIGNED);
13013 verify_variant_match (TYPE_ALIGN_OK);
13014 verify_variant_match (TYPE_PACKED);
13015 if (TREE_CODE (t) == REFERENCE_TYPE)
13016 verify_variant_match (TYPE_REF_IS_RVALUE);
13017 if (AGGREGATE_TYPE_P (t))
13018 verify_variant_match (TYPE_REVERSE_STORAGE_ORDER);
13019 else
13020 verify_variant_match (TYPE_SATURATING);
13021 /* FIXME: This check trigger during libstdc++ build. */
13022 if (RECORD_OR_UNION_TYPE_P (t) && COMPLETE_TYPE_P (t) && 0)
13023 verify_variant_match (TYPE_FINAL_P);
13025 /* tree_type_common checks. */
13027 if (COMPLETE_TYPE_P (t))
13029 verify_variant_match (TYPE_SIZE);
13030 verify_variant_match (TYPE_MODE);
13031 if (TYPE_SIZE_UNIT (t) != TYPE_SIZE_UNIT (tv)
13032 /* FIXME: ideally we should compare pointer equality, but java FE
13033 produce variants where size is INTEGER_CST of different type (int
13034 wrt size_type) during libjava biuld. */
13035 && !operand_equal_p (TYPE_SIZE_UNIT (t), TYPE_SIZE_UNIT (tv), 0))
13037 error ("type variant has different TYPE_SIZE_UNIT");
13038 debug_tree (tv);
13039 error ("type variant's TYPE_SIZE_UNIT");
13040 debug_tree (TYPE_SIZE_UNIT (tv));
13041 error ("type's TYPE_SIZE_UNIT");
13042 debug_tree (TYPE_SIZE_UNIT (t));
13043 return false;
13046 verify_variant_match (TYPE_PRECISION);
13047 verify_variant_match (TYPE_NEEDS_CONSTRUCTING);
13048 if (RECORD_OR_UNION_TYPE_P (t))
13049 verify_variant_match (TYPE_TRANSPARENT_AGGR);
13050 else if (TREE_CODE (t) == ARRAY_TYPE)
13051 verify_variant_match (TYPE_NONALIASED_COMPONENT);
13052 /* During LTO we merge variant lists from diferent translation units
13053 that may differ BY TYPE_CONTEXT that in turn may point
13054 to TRANSLATION_UNIT_DECL.
13055 Ada also builds variants of types with different TYPE_CONTEXT. */
13056 if ((!in_lto_p || !TYPE_FILE_SCOPE_P (t)) && 0)
13057 verify_variant_match (TYPE_CONTEXT);
13058 verify_variant_match (TYPE_STRING_FLAG);
13059 if (TYPE_ALIAS_SET_KNOWN_P (t) && TYPE_ALIAS_SET_KNOWN_P (tv))
13060 verify_variant_match (TYPE_ALIAS_SET);
13062 /* tree_type_non_common checks. */
13064 /* FIXME: C FE uses TYPE_VFIELD to record C_TYPE_INCOMPLETE_VARS
13065 and dangle the pointer from time to time. */
13066 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_VFIELD (t) != TYPE_VFIELD (tv)
13067 && (in_lto_p || !TYPE_VFIELD (tv)
13068 || TREE_CODE (TYPE_VFIELD (tv)) != TREE_LIST))
13070 error ("type variant has different TYPE_VFIELD");
13071 debug_tree (tv);
13072 return false;
13074 if ((TREE_CODE (t) == ENUMERAL_TYPE && COMPLETE_TYPE_P (t))
13075 || TREE_CODE (t) == INTEGER_TYPE
13076 || TREE_CODE (t) == BOOLEAN_TYPE
13077 || TREE_CODE (t) == REAL_TYPE
13078 || TREE_CODE (t) == FIXED_POINT_TYPE)
13080 verify_variant_match (TYPE_MAX_VALUE);
13081 verify_variant_match (TYPE_MIN_VALUE);
13083 if (TREE_CODE (t) == METHOD_TYPE)
13084 verify_variant_match (TYPE_METHOD_BASETYPE);
13085 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_METHODS (t))
13087 error ("type variant has TYPE_METHODS");
13088 debug_tree (tv);
13089 return false;
13091 if (TREE_CODE (t) == OFFSET_TYPE)
13092 verify_variant_match (TYPE_OFFSET_BASETYPE);
13093 if (TREE_CODE (t) == ARRAY_TYPE)
13094 verify_variant_match (TYPE_ARRAY_MAX_SIZE);
13095 /* FIXME: Be lax and allow TYPE_BINFO to be missing in variant types
13096 or even type's main variant. This is needed to make bootstrap pass
13097 and the bug seems new in GCC 5.
13098 C++ FE should be updated to make this consistent and we should check
13099 that TYPE_BINFO is always NULL for !COMPLETE_TYPE_P and otherwise there
13100 is a match with main variant.
13102 Also disable the check for Java for now because of parser hack that builds
13103 first an dummy BINFO and then sometimes replace it by real BINFO in some
13104 of the copies. */
13105 if (RECORD_OR_UNION_TYPE_P (t) && TYPE_BINFO (t) && TYPE_BINFO (tv)
13106 && TYPE_BINFO (t) != TYPE_BINFO (tv)
13107 /* FIXME: Java sometimes keep dump TYPE_BINFOs on variant types.
13108 Since there is no cheap way to tell C++/Java type w/o LTO, do checking
13109 at LTO time only. */
13110 && (in_lto_p && odr_type_p (t)))
13112 error ("type variant has different TYPE_BINFO");
13113 debug_tree (tv);
13114 error ("type variant's TYPE_BINFO");
13115 debug_tree (TYPE_BINFO (tv));
13116 error ("type's TYPE_BINFO");
13117 debug_tree (TYPE_BINFO (t));
13118 return false;
13121 /* Check various uses of TYPE_VALUES_RAW. */
13122 if (TREE_CODE (t) == ENUMERAL_TYPE)
13123 verify_variant_match (TYPE_VALUES);
13124 else if (TREE_CODE (t) == ARRAY_TYPE)
13125 verify_variant_match (TYPE_DOMAIN);
13126 /* Permit incomplete variants of complete type. While FEs may complete
13127 all variants, this does not happen for C++ templates in all cases. */
13128 else if (RECORD_OR_UNION_TYPE_P (t)
13129 && COMPLETE_TYPE_P (t)
13130 && TYPE_FIELDS (t) != TYPE_FIELDS (tv))
13132 tree f1, f2;
13134 /* Fortran builds qualified variants as new records with items of
13135 qualified type. Verify that they looks same. */
13136 for (f1 = TYPE_FIELDS (t), f2 = TYPE_FIELDS (tv);
13137 f1 && f2;
13138 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
13139 if (TREE_CODE (f1) != FIELD_DECL || TREE_CODE (f2) != FIELD_DECL
13140 || (TYPE_MAIN_VARIANT (TREE_TYPE (f1))
13141 != TYPE_MAIN_VARIANT (TREE_TYPE (f2))
13142 /* FIXME: gfc_nonrestricted_type builds all types as variants
13143 with exception of pointer types. It deeply copies the type
13144 which means that we may end up with a variant type
13145 referring non-variant pointer. We may change it to
13146 produce types as variants, too, like
13147 objc_get_protocol_qualified_type does. */
13148 && !POINTER_TYPE_P (TREE_TYPE (f1)))
13149 || DECL_FIELD_OFFSET (f1) != DECL_FIELD_OFFSET (f2)
13150 || DECL_FIELD_BIT_OFFSET (f1) != DECL_FIELD_BIT_OFFSET (f2))
13151 break;
13152 if (f1 || f2)
13154 error ("type variant has different TYPE_FIELDS");
13155 debug_tree (tv);
13156 error ("first mismatch is field");
13157 debug_tree (f1);
13158 error ("and field");
13159 debug_tree (f2);
13160 return false;
13163 else if ((TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE))
13164 verify_variant_match (TYPE_ARG_TYPES);
13165 /* For C++ the qualified variant of array type is really an array type
13166 of qualified TREE_TYPE.
13167 objc builds variants of pointer where pointer to type is a variant, too
13168 in objc_get_protocol_qualified_type. */
13169 if (TREE_TYPE (t) != TREE_TYPE (tv)
13170 && ((TREE_CODE (t) != ARRAY_TYPE
13171 && !POINTER_TYPE_P (t))
13172 || TYPE_MAIN_VARIANT (TREE_TYPE (t))
13173 != TYPE_MAIN_VARIANT (TREE_TYPE (tv))))
13175 error ("type variant has different TREE_TYPE");
13176 debug_tree (tv);
13177 error ("type variant's TREE_TYPE");
13178 debug_tree (TREE_TYPE (tv));
13179 error ("type's TREE_TYPE");
13180 debug_tree (TREE_TYPE (t));
13181 return false;
13183 if (type_with_alias_set_p (t)
13184 && !gimple_canonical_types_compatible_p (t, tv, false))
13186 error ("type is not compatible with its vairant");
13187 debug_tree (tv);
13188 error ("type variant's TREE_TYPE");
13189 debug_tree (TREE_TYPE (tv));
13190 error ("type's TREE_TYPE");
13191 debug_tree (TREE_TYPE (t));
13192 return false;
13194 return true;
13195 #undef verify_variant_match
13199 /* The TYPE_CANONICAL merging machinery. It should closely resemble
13200 the middle-end types_compatible_p function. It needs to avoid
13201 claiming types are different for types that should be treated
13202 the same with respect to TBAA. Canonical types are also used
13203 for IL consistency checks via the useless_type_conversion_p
13204 predicate which does not handle all type kinds itself but falls
13205 back to pointer-comparison of TYPE_CANONICAL for aggregates
13206 for example. */
13208 /* Return true if TYPE_UNSIGNED of TYPE should be ignored for canonical
13209 type calculation because we need to allow inter-operability between signed
13210 and unsigned variants. */
13212 bool
13213 type_with_interoperable_signedness (const_tree type)
13215 /* Fortran standard require C_SIGNED_CHAR to be interoperable with both
13216 signed char and unsigned char. Similarly fortran FE builds
13217 C_SIZE_T as signed type, while C defines it unsigned. */
13219 return tree_code_for_canonical_type_merging (TREE_CODE (type))
13220 == INTEGER_TYPE
13221 && (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node)
13222 || TYPE_PRECISION (type) == TYPE_PRECISION (size_type_node));
13225 /* Return true iff T1 and T2 are structurally identical for what
13226 TBAA is concerned.
13227 This function is used both by lto.c canonical type merging and by the
13228 verifier. If TRUST_TYPE_CANONICAL we do not look into structure of types
13229 that have TYPE_CANONICAL defined and assume them equivalent. This is useful
13230 only for LTO because only in these cases TYPE_CANONICAL equivalence
13231 correspond to one defined by gimple_canonical_types_compatible_p. */
13233 bool
13234 gimple_canonical_types_compatible_p (const_tree t1, const_tree t2,
13235 bool trust_type_canonical)
13237 /* Type variants should be same as the main variant. When not doing sanity
13238 checking to verify this fact, go to main variants and save some work. */
13239 if (trust_type_canonical)
13241 t1 = TYPE_MAIN_VARIANT (t1);
13242 t2 = TYPE_MAIN_VARIANT (t2);
13245 /* Check first for the obvious case of pointer identity. */
13246 if (t1 == t2)
13247 return true;
13249 /* Check that we have two types to compare. */
13250 if (t1 == NULL_TREE || t2 == NULL_TREE)
13251 return false;
13253 /* We consider complete types always compatible with incomplete type.
13254 This does not make sense for canonical type calculation and thus we
13255 need to ensure that we are never called on it.
13257 FIXME: For more correctness the function probably should have three modes
13258 1) mode assuming that types are complete mathcing their structure
13259 2) mode allowing incomplete types but producing equivalence classes
13260 and thus ignoring all info from complete types
13261 3) mode allowing incomplete types to match complete but checking
13262 compatibility between complete types.
13264 1 and 2 can be used for canonical type calculation. 3 is the real
13265 definition of type compatibility that can be used i.e. for warnings during
13266 declaration merging. */
13268 gcc_assert (!trust_type_canonical
13269 || (type_with_alias_set_p (t1) && type_with_alias_set_p (t2)));
13270 /* If the types have been previously registered and found equal
13271 they still are. */
13273 if (TYPE_CANONICAL (t1) && TYPE_CANONICAL (t2)
13274 && trust_type_canonical)
13276 /* Do not use TYPE_CANONICAL of pointer types. For LTO streamed types
13277 they are always NULL, but they are set to non-NULL for types
13278 constructed by build_pointer_type and variants. In this case the
13279 TYPE_CANONICAL is more fine grained than the equivalnce we test (where
13280 all pointers are considered equal. Be sure to not return false
13281 negatives. */
13282 gcc_checking_assert (!POINTER_TYPE_P (t1) && !POINTER_TYPE_P (t2));
13283 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
13286 /* Can't be the same type if the types don't have the same code. */
13287 enum tree_code code = tree_code_for_canonical_type_merging (TREE_CODE (t1));
13288 if (code != tree_code_for_canonical_type_merging (TREE_CODE (t2)))
13289 return false;
13291 /* Qualifiers do not matter for canonical type comparison purposes. */
13293 /* Void types and nullptr types are always the same. */
13294 if (TREE_CODE (t1) == VOID_TYPE
13295 || TREE_CODE (t1) == NULLPTR_TYPE)
13296 return true;
13298 /* Can't be the same type if they have different mode. */
13299 if (TYPE_MODE (t1) != TYPE_MODE (t2))
13300 return false;
13302 /* Non-aggregate types can be handled cheaply. */
13303 if (INTEGRAL_TYPE_P (t1)
13304 || SCALAR_FLOAT_TYPE_P (t1)
13305 || FIXED_POINT_TYPE_P (t1)
13306 || TREE_CODE (t1) == VECTOR_TYPE
13307 || TREE_CODE (t1) == COMPLEX_TYPE
13308 || TREE_CODE (t1) == OFFSET_TYPE
13309 || POINTER_TYPE_P (t1))
13311 /* Can't be the same type if they have different recision. */
13312 if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2))
13313 return false;
13315 /* In some cases the signed and unsigned types are required to be
13316 inter-operable. */
13317 if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2)
13318 && !type_with_interoperable_signedness (t1))
13319 return false;
13321 /* Fortran's C_SIGNED_CHAR is !TYPE_STRING_FLAG but needs to be
13322 interoperable with "signed char". Unless all frontends are revisited
13323 to agree on these types, we must ignore the flag completely. */
13325 /* Fortran standard define C_PTR type that is compatible with every
13326 C pointer. For this reason we need to glob all pointers into one.
13327 Still pointers in different address spaces are not compatible. */
13328 if (POINTER_TYPE_P (t1))
13330 if (TYPE_ADDR_SPACE (TREE_TYPE (t1))
13331 != TYPE_ADDR_SPACE (TREE_TYPE (t2)))
13332 return false;
13335 /* Tail-recurse to components. */
13336 if (TREE_CODE (t1) == VECTOR_TYPE
13337 || TREE_CODE (t1) == COMPLEX_TYPE)
13338 return gimple_canonical_types_compatible_p (TREE_TYPE (t1),
13339 TREE_TYPE (t2),
13340 trust_type_canonical);
13342 return true;
13345 /* Do type-specific comparisons. */
13346 switch (TREE_CODE (t1))
13348 case ARRAY_TYPE:
13349 /* Array types are the same if the element types are the same and
13350 the number of elements are the same. */
13351 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2),
13352 trust_type_canonical)
13353 || TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2)
13354 || TYPE_REVERSE_STORAGE_ORDER (t1) != TYPE_REVERSE_STORAGE_ORDER (t2)
13355 || TYPE_NONALIASED_COMPONENT (t1) != TYPE_NONALIASED_COMPONENT (t2))
13356 return false;
13357 else
13359 tree i1 = TYPE_DOMAIN (t1);
13360 tree i2 = TYPE_DOMAIN (t2);
13362 /* For an incomplete external array, the type domain can be
13363 NULL_TREE. Check this condition also. */
13364 if (i1 == NULL_TREE && i2 == NULL_TREE)
13365 return true;
13366 else if (i1 == NULL_TREE || i2 == NULL_TREE)
13367 return false;
13368 else
13370 tree min1 = TYPE_MIN_VALUE (i1);
13371 tree min2 = TYPE_MIN_VALUE (i2);
13372 tree max1 = TYPE_MAX_VALUE (i1);
13373 tree max2 = TYPE_MAX_VALUE (i2);
13375 /* The minimum/maximum values have to be the same. */
13376 if ((min1 == min2
13377 || (min1 && min2
13378 && ((TREE_CODE (min1) == PLACEHOLDER_EXPR
13379 && TREE_CODE (min2) == PLACEHOLDER_EXPR)
13380 || operand_equal_p (min1, min2, 0))))
13381 && (max1 == max2
13382 || (max1 && max2
13383 && ((TREE_CODE (max1) == PLACEHOLDER_EXPR
13384 && TREE_CODE (max2) == PLACEHOLDER_EXPR)
13385 || operand_equal_p (max1, max2, 0)))))
13386 return true;
13387 else
13388 return false;
13392 case METHOD_TYPE:
13393 case FUNCTION_TYPE:
13394 /* Function types are the same if the return type and arguments types
13395 are the same. */
13396 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2),
13397 trust_type_canonical))
13398 return false;
13400 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2))
13401 return true;
13402 else
13404 tree parms1, parms2;
13406 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
13407 parms1 && parms2;
13408 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
13410 if (!gimple_canonical_types_compatible_p
13411 (TREE_VALUE (parms1), TREE_VALUE (parms2),
13412 trust_type_canonical))
13413 return false;
13416 if (parms1 || parms2)
13417 return false;
13419 return true;
13422 case RECORD_TYPE:
13423 case UNION_TYPE:
13424 case QUAL_UNION_TYPE:
13426 tree f1, f2;
13428 if (TYPE_REVERSE_STORAGE_ORDER (t1) != TYPE_REVERSE_STORAGE_ORDER (t2))
13429 return false;
13431 /* For aggregate types, all the fields must be the same. */
13432 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
13433 f1 || f2;
13434 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
13436 /* Skip non-fields. */
13437 while (f1 && TREE_CODE (f1) != FIELD_DECL)
13438 f1 = TREE_CHAIN (f1);
13439 while (f2 && TREE_CODE (f2) != FIELD_DECL)
13440 f2 = TREE_CHAIN (f2);
13441 if (!f1 || !f2)
13442 break;
13443 /* The fields must have the same name, offset and type. */
13444 if (DECL_NONADDRESSABLE_P (f1) != DECL_NONADDRESSABLE_P (f2)
13445 || !gimple_compare_field_offset (f1, f2)
13446 || !gimple_canonical_types_compatible_p
13447 (TREE_TYPE (f1), TREE_TYPE (f2),
13448 trust_type_canonical))
13449 return false;
13452 /* If one aggregate has more fields than the other, they
13453 are not the same. */
13454 if (f1 || f2)
13455 return false;
13457 return true;
13460 default:
13461 /* Consider all types with language specific trees in them mutually
13462 compatible. This is executed only from verify_type and false
13463 positives can be tolerated. */
13464 gcc_assert (!in_lto_p);
13465 return true;
13469 /* Verify type T. */
13471 void
13472 verify_type (const_tree t)
13474 bool error_found = false;
13475 tree mv = TYPE_MAIN_VARIANT (t);
13476 if (!mv)
13478 error ("Main variant is not defined");
13479 error_found = true;
13481 else if (mv != TYPE_MAIN_VARIANT (mv))
13483 error ("TYPE_MAIN_VARIANT has different TYPE_MAIN_VARIANT");
13484 debug_tree (mv);
13485 error_found = true;
13487 else if (t != mv && !verify_type_variant (t, mv))
13488 error_found = true;
13490 tree ct = TYPE_CANONICAL (t);
13491 if (!ct)
13493 else if (TYPE_CANONICAL (t) != ct)
13495 error ("TYPE_CANONICAL has different TYPE_CANONICAL");
13496 debug_tree (ct);
13497 error_found = true;
13499 /* Method and function types can not be used to address memory and thus
13500 TYPE_CANONICAL really matters only for determining useless conversions.
13502 FIXME: C++ FE produce declarations of builtin functions that are not
13503 compatible with main variants. */
13504 else if (TREE_CODE (t) == FUNCTION_TYPE)
13506 else if (t != ct
13507 /* FIXME: gimple_canonical_types_compatible_p can not compare types
13508 with variably sized arrays because their sizes possibly
13509 gimplified to different variables. */
13510 && !variably_modified_type_p (ct, NULL)
13511 && !gimple_canonical_types_compatible_p (t, ct, false))
13513 error ("TYPE_CANONICAL is not compatible");
13514 debug_tree (ct);
13515 error_found = true;
13518 if (COMPLETE_TYPE_P (t) && TYPE_CANONICAL (t)
13519 && TYPE_MODE (t) != TYPE_MODE (TYPE_CANONICAL (t)))
13521 error ("TYPE_MODE of TYPE_CANONICAL is not compatible");
13522 debug_tree (ct);
13523 error_found = true;
13527 /* Check various uses of TYPE_MINVAL. */
13528 if (RECORD_OR_UNION_TYPE_P (t))
13530 /* FIXME: C FE uses TYPE_VFIELD to record C_TYPE_INCOMPLETE_VARS
13531 and danagle the pointer from time to time. */
13532 if (TYPE_VFIELD (t)
13533 && TREE_CODE (TYPE_VFIELD (t)) != FIELD_DECL
13534 && TREE_CODE (TYPE_VFIELD (t)) != TREE_LIST)
13536 error ("TYPE_VFIELD is not FIELD_DECL nor TREE_LIST");
13537 debug_tree (TYPE_VFIELD (t));
13538 error_found = true;
13541 else if (TREE_CODE (t) == POINTER_TYPE)
13543 if (TYPE_NEXT_PTR_TO (t)
13544 && TREE_CODE (TYPE_NEXT_PTR_TO (t)) != POINTER_TYPE)
13546 error ("TYPE_NEXT_PTR_TO is not POINTER_TYPE");
13547 debug_tree (TYPE_NEXT_PTR_TO (t));
13548 error_found = true;
13551 else if (TREE_CODE (t) == REFERENCE_TYPE)
13553 if (TYPE_NEXT_REF_TO (t)
13554 && TREE_CODE (TYPE_NEXT_REF_TO (t)) != REFERENCE_TYPE)
13556 error ("TYPE_NEXT_REF_TO is not REFERENCE_TYPE");
13557 debug_tree (TYPE_NEXT_REF_TO (t));
13558 error_found = true;
13561 else if (INTEGRAL_TYPE_P (t) || TREE_CODE (t) == REAL_TYPE
13562 || TREE_CODE (t) == FIXED_POINT_TYPE)
13564 /* FIXME: The following check should pass:
13565 useless_type_conversion_p (const_cast <tree> (t),
13566 TREE_TYPE (TYPE_MIN_VALUE (t))
13567 but does not for C sizetypes in LTO. */
13569 /* Java uses TYPE_MINVAL for TYPE_ARGUMENT_SIGNATURE. */
13570 else if (TYPE_MINVAL (t)
13571 && ((TREE_CODE (t) != METHOD_TYPE && TREE_CODE (t) != FUNCTION_TYPE)
13572 || in_lto_p))
13574 error ("TYPE_MINVAL non-NULL");
13575 debug_tree (TYPE_MINVAL (t));
13576 error_found = true;
13579 /* Check various uses of TYPE_MAXVAL. */
13580 if (RECORD_OR_UNION_TYPE_P (t))
13582 if (TYPE_METHODS (t) && TREE_CODE (TYPE_METHODS (t)) != FUNCTION_DECL
13583 && TREE_CODE (TYPE_METHODS (t)) != TEMPLATE_DECL
13584 && TYPE_METHODS (t) != error_mark_node)
13586 error ("TYPE_METHODS is not FUNCTION_DECL, TEMPLATE_DECL nor error_mark_node");
13587 debug_tree (TYPE_METHODS (t));
13588 error_found = true;
13591 else if (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE)
13593 if (TYPE_METHOD_BASETYPE (t)
13594 && TREE_CODE (TYPE_METHOD_BASETYPE (t)) != RECORD_TYPE
13595 && TREE_CODE (TYPE_METHOD_BASETYPE (t)) != UNION_TYPE)
13597 error ("TYPE_METHOD_BASETYPE is not record nor union");
13598 debug_tree (TYPE_METHOD_BASETYPE (t));
13599 error_found = true;
13602 else if (TREE_CODE (t) == OFFSET_TYPE)
13604 if (TYPE_OFFSET_BASETYPE (t)
13605 && TREE_CODE (TYPE_OFFSET_BASETYPE (t)) != RECORD_TYPE
13606 && TREE_CODE (TYPE_OFFSET_BASETYPE (t)) != UNION_TYPE)
13608 error ("TYPE_OFFSET_BASETYPE is not record nor union");
13609 debug_tree (TYPE_OFFSET_BASETYPE (t));
13610 error_found = true;
13613 else if (INTEGRAL_TYPE_P (t) || TREE_CODE (t) == REAL_TYPE
13614 || TREE_CODE (t) == FIXED_POINT_TYPE)
13616 /* FIXME: The following check should pass:
13617 useless_type_conversion_p (const_cast <tree> (t),
13618 TREE_TYPE (TYPE_MAX_VALUE (t))
13619 but does not for C sizetypes in LTO. */
13621 else if (TREE_CODE (t) == ARRAY_TYPE)
13623 if (TYPE_ARRAY_MAX_SIZE (t)
13624 && TREE_CODE (TYPE_ARRAY_MAX_SIZE (t)) != INTEGER_CST)
13626 error ("TYPE_ARRAY_MAX_SIZE not INTEGER_CST");
13627 debug_tree (TYPE_ARRAY_MAX_SIZE (t));
13628 error_found = true;
13631 else if (TYPE_MAXVAL (t))
13633 error ("TYPE_MAXVAL non-NULL");
13634 debug_tree (TYPE_MAXVAL (t));
13635 error_found = true;
13638 /* Check various uses of TYPE_BINFO. */
13639 if (RECORD_OR_UNION_TYPE_P (t))
13641 if (!TYPE_BINFO (t))
13643 else if (TREE_CODE (TYPE_BINFO (t)) != TREE_BINFO)
13645 error ("TYPE_BINFO is not TREE_BINFO");
13646 debug_tree (TYPE_BINFO (t));
13647 error_found = true;
13649 /* FIXME: Java builds invalid empty binfos that do not have
13650 TREE_TYPE set. */
13651 else if (TREE_TYPE (TYPE_BINFO (t)) != TYPE_MAIN_VARIANT (t) && 0)
13653 error ("TYPE_BINFO type is not TYPE_MAIN_VARIANT");
13654 debug_tree (TREE_TYPE (TYPE_BINFO (t)));
13655 error_found = true;
13658 else if (TYPE_LANG_SLOT_1 (t) && in_lto_p)
13660 error ("TYPE_LANG_SLOT_1 (binfo) field is non-NULL");
13661 debug_tree (TYPE_LANG_SLOT_1 (t));
13662 error_found = true;
13665 /* Check various uses of TYPE_VALUES_RAW. */
13666 if (TREE_CODE (t) == ENUMERAL_TYPE)
13667 for (tree l = TYPE_VALUES (t); l; l = TREE_CHAIN (l))
13669 tree value = TREE_VALUE (l);
13670 tree name = TREE_PURPOSE (l);
13672 /* C FE porduce INTEGER_CST of INTEGER_TYPE, while C++ FE uses
13673 CONST_DECL of ENUMERAL TYPE. */
13674 if (TREE_CODE (value) != INTEGER_CST && TREE_CODE (value) != CONST_DECL)
13676 error ("Enum value is not CONST_DECL or INTEGER_CST");
13677 debug_tree (value);
13678 debug_tree (name);
13679 error_found = true;
13681 if (TREE_CODE (TREE_TYPE (value)) != INTEGER_TYPE
13682 && !useless_type_conversion_p (const_cast <tree> (t), TREE_TYPE (value)))
13684 error ("Enum value type is not INTEGER_TYPE nor convertible to the enum");
13685 debug_tree (value);
13686 debug_tree (name);
13687 error_found = true;
13689 if (TREE_CODE (name) != IDENTIFIER_NODE)
13691 error ("Enum value name is not IDENTIFIER_NODE");
13692 debug_tree (value);
13693 debug_tree (name);
13694 error_found = true;
13697 else if (TREE_CODE (t) == ARRAY_TYPE)
13699 if (TYPE_DOMAIN (t) && TREE_CODE (TYPE_DOMAIN (t)) != INTEGER_TYPE)
13701 error ("Array TYPE_DOMAIN is not integer type");
13702 debug_tree (TYPE_DOMAIN (t));
13703 error_found = true;
13706 else if (RECORD_OR_UNION_TYPE_P (t))
13707 for (tree fld = TYPE_FIELDS (t); fld; fld = TREE_CHAIN (fld))
13709 /* TODO: verify properties of decls. */
13710 if (TREE_CODE (fld) == FIELD_DECL)
13712 else if (TREE_CODE (fld) == TYPE_DECL)
13714 else if (TREE_CODE (fld) == CONST_DECL)
13716 else if (TREE_CODE (fld) == VAR_DECL)
13718 else if (TREE_CODE (fld) == TEMPLATE_DECL)
13720 else if (TREE_CODE (fld) == USING_DECL)
13722 else
13724 error ("Wrong tree in TYPE_FIELDS list");
13725 debug_tree (fld);
13726 error_found = true;
13729 else if (TREE_CODE (t) == INTEGER_TYPE
13730 || TREE_CODE (t) == BOOLEAN_TYPE
13731 || TREE_CODE (t) == OFFSET_TYPE
13732 || TREE_CODE (t) == REFERENCE_TYPE
13733 || TREE_CODE (t) == NULLPTR_TYPE
13734 || TREE_CODE (t) == POINTER_TYPE)
13736 if (TYPE_CACHED_VALUES_P (t) != (TYPE_CACHED_VALUES (t) != NULL))
13738 error ("TYPE_CACHED_VALUES_P is %i while TYPE_CACHED_VALUES is %p",
13739 TYPE_CACHED_VALUES_P (t), (void *)TYPE_CACHED_VALUES (t));
13740 error_found = true;
13742 else if (TYPE_CACHED_VALUES_P (t) && TREE_CODE (TYPE_CACHED_VALUES (t)) != TREE_VEC)
13744 error ("TYPE_CACHED_VALUES is not TREE_VEC");
13745 debug_tree (TYPE_CACHED_VALUES (t));
13746 error_found = true;
13748 /* Verify just enough of cache to ensure that no one copied it to new type.
13749 All copying should go by copy_node that should clear it. */
13750 else if (TYPE_CACHED_VALUES_P (t))
13752 int i;
13753 for (i = 0; i < TREE_VEC_LENGTH (TYPE_CACHED_VALUES (t)); i++)
13754 if (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i)
13755 && TREE_TYPE (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i)) != t)
13757 error ("wrong TYPE_CACHED_VALUES entry");
13758 debug_tree (TREE_VEC_ELT (TYPE_CACHED_VALUES (t), i));
13759 error_found = true;
13760 break;
13764 else if (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE)
13765 for (tree l = TYPE_ARG_TYPES (t); l; l = TREE_CHAIN (l))
13767 /* C++ FE uses TREE_PURPOSE to store initial values. */
13768 if (TREE_PURPOSE (l) && in_lto_p)
13770 error ("TREE_PURPOSE is non-NULL in TYPE_ARG_TYPES list");
13771 debug_tree (l);
13772 error_found = true;
13774 if (!TYPE_P (TREE_VALUE (l)))
13776 error ("Wrong entry in TYPE_ARG_TYPES list");
13777 debug_tree (l);
13778 error_found = true;
13781 else if (!is_lang_specific (t) && TYPE_VALUES_RAW (t))
13783 error ("TYPE_VALUES_RAW field is non-NULL");
13784 debug_tree (TYPE_VALUES_RAW (t));
13785 error_found = true;
13787 if (TREE_CODE (t) != INTEGER_TYPE
13788 && TREE_CODE (t) != BOOLEAN_TYPE
13789 && TREE_CODE (t) != OFFSET_TYPE
13790 && TREE_CODE (t) != REFERENCE_TYPE
13791 && TREE_CODE (t) != NULLPTR_TYPE
13792 && TREE_CODE (t) != POINTER_TYPE
13793 && TYPE_CACHED_VALUES_P (t))
13795 error ("TYPE_CACHED_VALUES_P is set while it should not");
13796 error_found = true;
13798 if (TYPE_STRING_FLAG (t)
13799 && TREE_CODE (t) != ARRAY_TYPE && TREE_CODE (t) != INTEGER_TYPE)
13801 error ("TYPE_STRING_FLAG is set on wrong type code");
13802 error_found = true;
13804 else if (TYPE_STRING_FLAG (t))
13806 const_tree b = t;
13807 if (TREE_CODE (b) == ARRAY_TYPE)
13808 b = TREE_TYPE (t);
13809 /* Java builds arrays with TYPE_STRING_FLAG of promoted_char_type
13810 that is 32bits. */
13811 if (TREE_CODE (b) != INTEGER_TYPE)
13813 error ("TYPE_STRING_FLAG is set on type that does not look like "
13814 "char nor array of chars");
13815 error_found = true;
13819 /* ipa-devirt makes an assumption that TYPE_METHOD_BASETYPE is always
13820 TYPE_MAIN_VARIANT and it would be odd to add methods only to variatns
13821 of a type. */
13822 if (TREE_CODE (t) == METHOD_TYPE
13823 && TYPE_MAIN_VARIANT (TYPE_METHOD_BASETYPE (t)) != TYPE_METHOD_BASETYPE (t))
13825 error ("TYPE_METHOD_BASETYPE is not main variant");
13826 error_found = true;
13829 if (error_found)
13831 debug_tree (const_cast <tree> (t));
13832 internal_error ("verify_type failed");
13837 /* Return true if ARG is marked with the nonnull attribute in the
13838 current function signature. */
13840 bool
13841 nonnull_arg_p (const_tree arg)
13843 tree t, attrs, fntype;
13844 unsigned HOST_WIDE_INT arg_num;
13846 gcc_assert (TREE_CODE (arg) == PARM_DECL && POINTER_TYPE_P (TREE_TYPE (arg)));
13848 /* The static chain decl is always non null. */
13849 if (arg == cfun->static_chain_decl)
13850 return true;
13852 /* THIS argument of method is always non-NULL. */
13853 if (TREE_CODE (TREE_TYPE (cfun->decl)) == METHOD_TYPE
13854 && arg == DECL_ARGUMENTS (cfun->decl)
13855 && flag_delete_null_pointer_checks)
13856 return true;
13858 /* Values passed by reference are always non-NULL. */
13859 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE
13860 && flag_delete_null_pointer_checks)
13861 return true;
13863 fntype = TREE_TYPE (cfun->decl);
13864 for (attrs = TYPE_ATTRIBUTES (fntype); attrs; attrs = TREE_CHAIN (attrs))
13866 attrs = lookup_attribute ("nonnull", attrs);
13868 /* If "nonnull" wasn't specified, we know nothing about the argument. */
13869 if (attrs == NULL_TREE)
13870 return false;
13872 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
13873 if (TREE_VALUE (attrs) == NULL_TREE)
13874 return true;
13876 /* Get the position number for ARG in the function signature. */
13877 for (arg_num = 1, t = DECL_ARGUMENTS (cfun->decl);
13879 t = DECL_CHAIN (t), arg_num++)
13881 if (t == arg)
13882 break;
13885 gcc_assert (t == arg);
13887 /* Now see if ARG_NUM is mentioned in the nonnull list. */
13888 for (t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
13890 if (compare_tree_int (TREE_VALUE (t), arg_num) == 0)
13891 return true;
13895 return false;
13898 /* Given location LOC, strip away any packed range information
13899 or ad-hoc information. */
13901 static location_t
13902 get_pure_location (location_t loc)
13904 if (IS_ADHOC_LOC (loc))
13906 = line_table->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
13908 if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (line_table))
13909 return loc;
13911 if (loc < RESERVED_LOCATION_COUNT)
13912 return loc;
13914 const line_map *map = linemap_lookup (line_table, loc);
13915 const line_map_ordinary *ordmap = linemap_check_ordinary (map);
13917 return loc & ~((1 << ordmap->m_range_bits) - 1);
13920 /* Combine LOC and BLOCK to a combined adhoc loc, retaining any range
13921 information. */
13923 location_t
13924 set_block (location_t loc, tree block)
13926 location_t pure_loc = get_pure_location (loc);
13927 source_range src_range = get_range_from_loc (line_table, loc);
13928 return COMBINE_LOCATION_DATA (line_table, pure_loc, src_range, block);
13931 void
13932 set_source_range (tree expr, location_t start, location_t finish)
13934 source_range src_range;
13935 src_range.m_start = start;
13936 src_range.m_finish = finish;
13937 set_source_range (expr, src_range);
13940 void
13941 set_source_range (tree expr, source_range src_range)
13943 if (!EXPR_P (expr))
13944 return;
13946 location_t pure_loc = get_pure_location (EXPR_LOCATION (expr));
13947 location_t adhoc = COMBINE_LOCATION_DATA (line_table,
13948 pure_loc,
13949 src_range,
13950 NULL);
13951 SET_EXPR_LOCATION (expr, adhoc);
13954 /* Return the name of combined function FN, for debugging purposes. */
13956 const char *
13957 combined_fn_name (combined_fn fn)
13959 if (builtin_fn_p (fn))
13961 tree fndecl = builtin_decl_explicit (as_builtin_fn (fn));
13962 return IDENTIFIER_POINTER (DECL_NAME (fndecl));
13964 else
13965 return internal_fn_name (as_internal_fn (fn));
13968 #include "gt-tree.h"