Debian Codegraph @

gcc/langhooks.cc

raw
1 /* Default language-specific hooks.
2 Copyright (C) 2001-2025 Free Software Foundation, Inc.
3 Contributed by Alexandre Oliva <[email protected]>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "target.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "timevar.h"
28 #include "stringpool.h"
29 #include "diagnostic.h"
30 #include "intl.h"
31 #include "toplev.h"
32 #include "attribs.h"
33 #include "gimplify.h"
34 #include "langhooks.h"
35 #include "tree-diagnostic.h"
36 #include "output.h"
37 #include "timevar.h"
38 #include "stor-layout.h"
39 #include "cgraph.h"
40 #include "debug.h"
41 #include "diagnostic-format-text.h"
42
43 /* Do nothing; in many cases the default hook. */
44
45 void
46 lhd_do_nothing (void)
47 {
48 }
49
50 /* Do nothing (tree). */
51
52 void
53 lhd_do_nothing_t (tree ARG_UNUSED (t))
54 {
55 }
56
57 /* Pass through (tree). */
58 tree
59 lhd_pass_through_t (tree t)
60 {
61 return t;
62 }
63
64 /* Do nothing (int, int, int). Return NULL_TREE. */
65
66 tree
67 lhd_do_nothing_iii_return_null_tree (int ARG_UNUSED (i),
68 int ARG_UNUSED (j),
69 int ARG_UNUSED (k))
70 {
71 return NULL_TREE;
72 }
73
74 /* Do nothing (function). */
75
76 void
77 lhd_do_nothing_f (struct function * ARG_UNUSED (f))
78 {
79 }
80
81 /* Do nothing (return NULL_TREE). */
82
83 tree
84 lhd_return_null_tree (tree ARG_UNUSED (t))
85 {
86 return NULL_TREE;
87 }
88
89 /* Do nothing (return NULL_TREE). */
90
91 tree
92 lhd_return_null_const_tree (const_tree ARG_UNUSED (t))
93 {
94 return NULL_TREE;
95 }
96
97 /* The default post options hook. */
98
99 bool
100 lhd_post_options (const char ** ARG_UNUSED (pfilename))
101 {
102 /* Excess precision other than "fast" requires front-end
103 support. */
104 flag_excess_precision = EXCESS_PRECISION_FAST;
105 return false;
106 }
107
108 /* Called from by print-tree.cc. */
109
110 void
111 lhd_print_tree_nothing (FILE * ARG_UNUSED (file),
112 tree ARG_UNUSED (node),
113 int ARG_UNUSED (indent))
114 {
115 }
116
117 /* Called from check_global_declaration. */
118
119 bool
120 lhd_warn_unused_global_decl (const_tree decl)
121 {
122 /* This is what used to exist in check_global_declaration. Probably
123 not many of these actually apply to non-C languages. */
124
125 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl))
126 return false;
127 if (VAR_P (decl) && TREE_READONLY (decl))
128 return false;
129 if (DECL_IN_SYSTEM_HEADER (decl))
130 return false;
131
132 return true;
133 }
134
135 /* Set the DECL_ASSEMBLER_NAME for DECL. */
136 void
137 lhd_set_decl_assembler_name (tree decl)
138 {
139 tree id;
140
141 /* set_decl_assembler_name may be called on TYPE_DECL to record ODR
142 name for C++ types. By default types have no ODR names. */
143 if (TREE_CODE (decl) == TYPE_DECL)
144 return;
145
146 /* The language-independent code should never use the
147 DECL_ASSEMBLER_NAME for lots of DECLs. Only FUNCTION_DECLs and
148 VAR_DECLs for variables with static storage duration need a real
149 DECL_ASSEMBLER_NAME. */
150 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
151 || (VAR_P (decl)
152 && (TREE_STATIC (decl)
153 || DECL_EXTERNAL (decl)
154 || TREE_PUBLIC (decl))));
155
156 /* By default, assume the name to use in assembly code is the same
157 as that used in the source language. (That's correct for C, and
158 GCC used to set DECL_ASSEMBLER_NAME to the same value as
159 DECL_NAME in build_decl, so this choice provides backwards
160 compatibility with existing front-ends. This assumption is wrapped
161 in a target hook, to allow for target-specific modification of the
162 identifier.
163
164 Can't use just the variable's own name for a variable whose scope
165 is less than the whole compilation. Concatenate a distinguishing
166 number. */
167
168 if (TREE_PUBLIC (decl) || DECL_FILE_SCOPE_P (decl))
169 id = targetm.mangle_decl_assembler_name (decl, DECL_NAME (decl));
170 else
171 {
172 const char *name = IDENTIFIER_POINTER (DECL_NAME (decl));
173 static unsigned long num;
174 char *label;
175
176 ASM_FORMAT_PRIVATE_NAME (label, name, num++);
177 id = get_identifier (label);
178 }
179
180 SET_DECL_ASSEMBLER_NAME (decl, id);
181 }
182
183 /* Forcibly overwrite the DECL_ASSEMBLER_NAME for DECL to NAME. */
184 void
185 lhd_overwrite_decl_assembler_name (tree decl, tree name)
186 {
187 DECL_ASSEMBLER_NAME_RAW (decl) = name;
188 }
189
190 /* Type promotion for variable arguments. */
191 tree
192 lhd_type_promotes_to (tree ARG_UNUSED (type))
193 {
194 gcc_unreachable ();
195 }
196
197 /* Registration of machine- or os-specific builtin types. */
198 void
199 lhd_register_builtin_type (tree ARG_UNUSED (type),
200 const char * ARG_UNUSED (name))
201 {
202 }
203
204 /* Invalid use of an incomplete type. */
205 void
206 lhd_incomplete_type_error (location_t ARG_UNUSED (loc),
207 const_tree ARG_UNUSED (value), const_tree type)
208 {
209 gcc_assert (TREE_CODE (type) == ERROR_MARK);
210 return;
211 }
212
213 /* Provide a default routine for alias sets that always returns -1. This
214 is used by languages that don't need to do anything special. */
215
216 alias_set_type
217 lhd_get_alias_set (tree ARG_UNUSED (t))
218 {
219 return -1;
220 }
221
222 /* This is the default decl_printable_name function. */
223
224 const char *
225 lhd_decl_printable_name (tree decl, int ARG_UNUSED (verbosity))
226 {
227 gcc_assert (decl && DECL_NAME (decl));
228 return IDENTIFIER_POINTER (DECL_NAME (decl));
229 }
230
231 /* This is the default dwarf_name function. */
232
233 const char *
234 lhd_dwarf_name (tree t, int verbosity)
235 {
236 gcc_assert (DECL_P (t));
237
238 return lang_hooks.decl_printable_name (t, verbosity);
239 }
240
241 /* This compares two types for equivalence ("compatible" in C-based languages).
242 This routine should only return 1 if it is sure. It should not be used
243 in contexts where erroneously returning 0 causes problems. */
244
245 int
246 lhd_types_compatible_p (tree x, tree y)
247 {
248 return TYPE_MAIN_VARIANT (x) == TYPE_MAIN_VARIANT (y);
249 }
250
251 /* lang_hooks.tree_dump.dump_tree: Dump language-specific parts of tree
252 nodes. Returns nonzero if it does not want the usual dumping of the
253 second argument. */
254
255 bool
256 lhd_tree_dump_dump_tree (void *di ATTRIBUTE_UNUSED, tree t ATTRIBUTE_UNUSED)
257 {
258 return false;
259 }
260
261 /* lang_hooks.tree_dump.type_qual: Determine type qualifiers in a
262 language-specific way. */
263
264 int
265 lhd_tree_dump_type_quals (const_tree t)
266 {
267 return TYPE_QUALS (t);
268 }
269
270 /* lang_hooks.gimplify_expr re-writes *EXPR_P into GIMPLE form. */
271
272 int
273 lhd_gimplify_expr (tree *expr_p ATTRIBUTE_UNUSED,
274 gimple_seq *pre_p ATTRIBUTE_UNUSED,
275 gimple_seq *post_p ATTRIBUTE_UNUSED)
276 {
277 return GS_UNHANDLED;
278 }
279
280 /* lang_hooks.tree_size: Determine the size of a tree with code C,
281 which is a language-specific tree code in category tcc_constant,
282 tcc_exceptional or tcc_type. The default expects never to be called. */
283 size_t
284 lhd_tree_size (enum tree_code c ATTRIBUTE_UNUSED)
285 {
286 gcc_unreachable ();
287 }
288
289 /* Return true if decl, which is a function decl, may be called by a
290 sibcall. */
291
292 bool
293 lhd_decl_ok_for_sibcall (const_tree decl ATTRIBUTE_UNUSED)
294 {
295 return true;
296 }
297
298 /* Generic global declaration processing. This is meant to be called
299 by the front-ends at the end of parsing. C/C++ do their own thing,
300 but other front-ends may call this. */
301
302 void
303 global_decl_processing (void)
304 {
305 tree globals, decl, *vec;
306 int len, i;
307
308 timevar_stop (TV_PHASE_PARSING);
309 timevar_start (TV_PHASE_DEFERRED);
310 /* Really define vars that have had only a tentative definition.
311 Really output inline functions that must actually be callable
312 and have not been output so far. */
313
314 globals = lang_hooks.decls.getdecls ();
315 len = list_length (globals);
316 vec = XNEWVEC (tree, len);
317
318 /* Process the decls in reverse order--earliest first.
319 Put them into VEC from back to front, then take out from front. */
320
321 for (i = 0, decl = globals; i < len; i++, decl = DECL_CHAIN (decl))
322 vec[len - i - 1] = decl;
323
324 wrapup_global_declarations (vec, len);
325 timevar_stop (TV_PHASE_DEFERRED);
326
327 timevar_start (TV_PHASE_PARSING);
328 free (vec);
329 }
330
331 /* Called to perform language-specific initialization of CTX. */
332 void
333 lhd_initialize_diagnostics (diagnostic_context *ctx ATTRIBUTE_UNUSED)
334 {
335 }
336
337 /* Called to register dumps. */
338 void
339 lhd_register_dumps (gcc::dump_manager *)
340 {
341 }
342
343 /* Called to perform language-specific options initialization. */
344 void
345 lhd_init_options (unsigned int decoded_options_count ATTRIBUTE_UNUSED,
346 struct cl_decoded_option *decoded_options ATTRIBUTE_UNUSED)
347 {
348 }
349
350 /* By default, always complain about options for the wrong language. */
351 bool
352 lhd_complain_wrong_lang_p (const struct cl_option *option ATTRIBUTE_UNUSED)
353 {
354 return true;
355 }
356
357 /* By default, no language-specific options are valid. */
358 bool
359 lhd_handle_option (size_t code ATTRIBUTE_UNUSED,
360 const char *arg ATTRIBUTE_UNUSED,
361 HOST_WIDE_INT value ATTRIBUTE_UNUSED,
362 int kind ATTRIBUTE_UNUSED,
363 location_t loc ATTRIBUTE_UNUSED,
364 const struct cl_option_handlers *handlers ATTRIBUTE_UNUSED)
365 {
366 return false;
367 }
368
369 /* The default function to print out name of current function that caused
370 an error. */
371 void
372 lhd_print_error_function (diagnostic_text_output_format &text_output,
373 const char *file,
374 const diagnostic_info *diagnostic)
375 {
376 diagnostic_context *const context = &text_output.get_context ();
377 if (diagnostic_last_function_changed (context, diagnostic))
378 {
379 pretty_printer *const pp = text_output.get_printer ();
380 char *old_prefix = pp_take_prefix (pp);
381 tree abstract_origin = diagnostic_abstract_origin (diagnostic);
382 char *new_prefix = (file && abstract_origin == NULL)
383 ? text_output.file_name_as_prefix (file) : NULL;
384
385 pp_set_prefix (pp, new_prefix);
386
387 if (current_function_decl == NULL)
388 pp_printf (pp, _("At top level:"));
389 else
390 {
391 tree fndecl, ao;
392
393 if (abstract_origin)
394 {
395 ao = BLOCK_ABSTRACT_ORIGIN (abstract_origin);
396 gcc_assert (TREE_CODE (ao) == FUNCTION_DECL);
397 fndecl = ao;
398 }
399 else
400 fndecl = current_function_decl;
401
402 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
403 pp_printf
404 (pp, _("In member function %qs"),
405 identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 2)));
406 else
407 pp_printf
408 (pp, _("In function %qs"),
409 identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 2)));
410
411 while (abstract_origin)
412 {
413 location_t *locus;
414 tree block = abstract_origin;
415
416 locus = &BLOCK_SOURCE_LOCATION (block);
417 fndecl = NULL;
418 block = BLOCK_SUPERCONTEXT (block);
419 while (block && TREE_CODE (block) == BLOCK
420 && BLOCK_ABSTRACT_ORIGIN (block))
421 {
422 ao = BLOCK_ABSTRACT_ORIGIN (block);
423 if (TREE_CODE (ao) == FUNCTION_DECL)
424 {
425 fndecl = ao;
426 break;
427 }
428 else if (TREE_CODE (ao) != BLOCK)
429 break;
430
431 block = BLOCK_SUPERCONTEXT (block);
432 }
433 if (fndecl)
434 abstract_origin = block;
435 else
436 {
437 while (block && TREE_CODE (block) == BLOCK)
438 block = BLOCK_SUPERCONTEXT (block);
439
440 if (block && TREE_CODE (block) == FUNCTION_DECL)
441 fndecl = block;
442 abstract_origin = NULL;
443 }
444 if (fndecl)
445 {
446 expanded_location s = expand_location (*locus);
447 pp_comma (pp);
448 pp_newline (pp);
449 if (s.file != NULL)
450 {
451 if (context->m_show_column)
452 pp_printf (pp,
453 _(" inlined from %qs at %r%s:%d:%d%R"),
454 identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 2)),
455 "locus", s.file, s.line, s.column);
456 else
457 pp_printf (pp,
458 _(" inlined from %qs at %r%s:%d%R"),
459 identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 2)),
460 "locus", s.file, s.line);
461
462 }
463 else
464 pp_printf (pp, _(" inlined from %qs"),
465 identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 2)));
466 }
467 }
468 pp_colon (pp);
469 }
470
471 diagnostic_set_last_function (context, diagnostic);
472 pp_newline_and_flush (pp);
473 pp->set_prefix (old_prefix);
474 }
475 }
476
477 tree
478 lhd_make_node (enum tree_code code)
479 {
480 return make_node (code);
481 }
482
483 /* Default implementation of LANG_HOOKS_SIMULATE_ENUM_DECL. Assume a
484 simple int-based enumerator (which is all the hook can be used for
485 at present) and push each decl individually without any decoration.
486
487 This definition is suitable for LTO and is generic enough that it
488 might be reusable elsewhere. */
489 tree
490 lhd_simulate_enum_decl (location_t loc, const char *name,
491 vec<string_int_pair> *values_ptr)
492 {
493 tree enumtype = lang_hooks.types.make_type (ENUMERAL_TYPE);
494 tree enumdecl = build_decl (loc, TYPE_DECL, get_identifier (name), enumtype);
495 TYPE_STUB_DECL (enumtype) = enumdecl;
496
497 tree value_chain = NULL_TREE;
498 string_int_pair *value;
499 vec<string_int_pair> values = *values_ptr;
500 unsigned int i;
501 FOR_EACH_VEC_ELT (values, i, value)
502 {
503 tree value_decl = build_decl (loc, CONST_DECL,
504 get_identifier (value->first), enumtype);
505 DECL_INITIAL (value_decl) = build_int_cst (integer_type_node,
506 value->second);
507 lang_hooks.decls.pushdecl (value_decl);
508 value_chain = tree_cons (value_decl, DECL_INITIAL (value_decl),
509 value_chain);
510 }
511
512 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (integer_type_node);
513 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (integer_type_node);
514 SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (integer_type_node));
515 TYPE_PRECISION (enumtype) = TYPE_PRECISION (integer_type_node);
516 layout_type (enumtype);
517 lang_hooks.decls.pushdecl (enumdecl);
518
519 return enumtype;
520 }
521
522 /* Default implementation of LANG_HOOKS_SIMULATE_RECORD_DECL.
523 Just create a normal RECORD_TYPE and a TYPE_DECL for it. */
524 tree
525 lhd_simulate_record_decl (location_t loc, const char *name,
526 array_slice<const tree> fields)
527 {
528 for (unsigned int i = 1; i < fields.size (); ++i)
529 /* Reversed by finish_builtin_struct. */
530 DECL_CHAIN (fields[i]) = fields[i - 1];
531
532 tree type = lang_hooks.types.make_type (RECORD_TYPE);
533 finish_builtin_struct (type, name, fields.back (), NULL_TREE);
534
535 tree decl = build_decl (loc, TYPE_DECL, get_identifier (name), type);
536 lang_hooks.decls.pushdecl (decl);
537
538 return type;
539 }
540
541 /* Default implementation of LANG_HOOKS_TYPE_FOR_SIZE.
542 Return an integer type with PRECISION bits of precision,
543 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
544
545 tree
546 lhd_type_for_size (unsigned precision, int unsignedp)
547 {
548 int i;
549
550 if (precision == TYPE_PRECISION (integer_type_node))
551 return unsignedp ? unsigned_type_node : integer_type_node;
552
553 if (precision == TYPE_PRECISION (signed_char_type_node))
554 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
555
556 if (precision == TYPE_PRECISION (short_integer_type_node))
557 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
558
559 if (precision == TYPE_PRECISION (long_integer_type_node))
560 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
561
562 if (precision == TYPE_PRECISION (long_long_integer_type_node))
563 return unsignedp
564 ? long_long_unsigned_type_node
565 : long_long_integer_type_node;
566
567 for (i = 0; i < NUM_INT_N_ENTS; i ++)
568 if (int_n_enabled_p[i]
569 && precision == int_n_data[i].bitsize)
570 return (unsignedp ? int_n_trees[i].unsigned_type
571 : int_n_trees[i].signed_type);
572
573 if (precision <= TYPE_PRECISION (intQI_type_node))
574 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
575
576 if (precision <= TYPE_PRECISION (intHI_type_node))
577 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
578
579 if (precision <= TYPE_PRECISION (intSI_type_node))
580 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
581
582 if (precision <= TYPE_PRECISION (intDI_type_node))
583 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
584
585 if (precision <= TYPE_PRECISION (intTI_type_node))
586 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
587
588 return NULL_TREE;
589 }
590
591 HOST_WIDE_INT
592 lhd_to_target_charset (HOST_WIDE_INT c)
593 {
594 return c;
595 }
596
597 tree
598 lhd_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se ATTRIBUTE_UNUSED)
599 {
600 return expr;
601 }
602
603 /* Return sharing kind if OpenMP sharing attribute of DECL is
604 predetermined, OMP_CLAUSE_DEFAULT_UNSPECIFIED otherwise. */
605
606 enum omp_clause_default_kind
607 lhd_omp_predetermined_sharing (tree decl)
608 {
609 if (DECL_ARTIFICIAL (decl))
610 return OMP_CLAUSE_DEFAULT_SHARED;
611 return OMP_CLAUSE_DEFAULT_UNSPECIFIED;
612 }
613
614 /* Return sharing kind if OpenMP mapping attribute of DECL is
615 predetermined, OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED otherwise. */
616
617 enum omp_clause_defaultmap_kind
618 lhd_omp_predetermined_mapping (tree decl)
619 {
620 if (DECL_ARTIFICIAL (decl))
621 return OMP_CLAUSE_DEFAULTMAP_TO;
622 return OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
623 }
624
625 /* Generate code to copy SRC to DST. */
626
627 tree
628 lhd_omp_assignment (tree clause ATTRIBUTE_UNUSED, tree dst, tree src)
629 {
630 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
631 }
632
633 /* Finalize clause C. */
634
635 void
636 lhd_omp_finish_clause (tree, gimple_seq *, bool)
637 {
638 }
639
640 /* Return array size; cf. omp_array_data. */
641
642 tree
643 lhd_omp_array_size (tree, gimple_seq *)
644 {
645 return NULL_TREE;
646 }
647
648 /* Returns true when additional mappings for a decl are needed. */
649
650 bool
651 lhd_omp_deep_mapping_p (const gimple *, tree)
652 {
653 return false;
654 }
655
656 /* Returns number of additional mappings for a decl. */
657
658 tree
659 lhd_omp_deep_mapping_cnt (const gimple *, tree, gimple_seq *)
660 {
661 return NULL_TREE;
662 }
663
664 /* Do the additional mappings. */
665
666 void
667 lhd_omp_deep_mapping (const gimple *, tree, unsigned HOST_WIDE_INT, tree, tree,
668 tree, tree, tree, gimple_seq *)
669 {
670 }
671
672 /* Return true if DECL is a scalar variable (for the purpose of
673 implicit firstprivatization & mapping). Only if alloc_ptr_ok
674 are allocatables and pointers accepted. */
675
676 bool
677 lhd_omp_scalar_p (tree decl, bool ptr_ok)
678 {
679 tree type = TREE_TYPE (decl);
680 if (TREE_CODE (type) == REFERENCE_TYPE)
681 type = TREE_TYPE (type);
682 if (TREE_CODE (type) == COMPLEX_TYPE)
683 type = TREE_TYPE (type);
684 if (INTEGRAL_TYPE_P (type)
685 || SCALAR_FLOAT_TYPE_P (type)
686 || (ptr_ok && TREE_CODE (type) == POINTER_TYPE))
687 return true;
688 return false;
689 }
690
691 /* Return static initializer for DECL. */
692
693 tree *
694 lhd_omp_get_decl_init (tree decl)
695 {
696 return &DECL_INITIAL (decl);
697 }
698
699 /* Free any extra memory used to hold initializer information for
700 variable declarations. */
701
702 void
703 lhd_omp_finish_decl_inits (void)
704 {
705 }
706
707 /* Register language specific type size variables as potentially OpenMP
708 firstprivate variables. */
709
710 void
711 lhd_omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *c ATTRIBUTE_UNUSED,
712 tree t ATTRIBUTE_UNUSED)
713 {
714 }
715
716 /* Common function for add_builtin_function, add_builtin_function_ext_scope
717 and simulate_builtin_function_decl. */
718
719 static tree
720 build_builtin_function (location_t location, const char *name, tree type,
721 int function_code, enum built_in_class cl,
722 const char *library_name, tree attrs)
723 {
724 tree id = get_identifier (name);
725 tree decl = build_decl (location, FUNCTION_DECL, id, type);
726
727 TREE_PUBLIC (decl) = 1;
728 DECL_EXTERNAL (decl) = 1;
729
730 set_decl_built_in_function (decl, cl, function_code);
731
732 if (library_name)
733 {
734 tree libname = get_identifier (library_name);
735
736 libname = targetm.mangle_decl_assembler_name (decl, libname);
737 SET_DECL_ASSEMBLER_NAME (decl, libname);
738 }
739
740 /* Possibly apply some default attributes to this built-in function. */
741 if (attrs)
742 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
743 else
744 decl_attributes (&decl, NULL_TREE, 0);
745
746 return decl;
747 }
748
749 /* Create a builtin function. */
750
751 tree
752 add_builtin_function (const char *name,
753 tree type,
754 int function_code,
755 enum built_in_class cl,
756 const char *library_name,
757 tree attrs)
758 {
759 tree decl = build_builtin_function (BUILTINS_LOCATION, name, type,
760 function_code, cl, library_name, attrs);
761 return lang_hooks.builtin_function (decl);
762 }
763
764 /* Like add_builtin_function, but make sure the scope is the external scope.
765 This is used to delay putting in back end builtin functions until the ISA
766 that defines the builtin is declared via function specific target options,
767 which can save memory for machines like the x86_64 that have multiple ISAs.
768 If this points to the same function as builtin_function, the backend must
769 add all of the builtins at program initialization time. */
770
771 tree
772 add_builtin_function_ext_scope (const char *name,
773 tree type,
774 int function_code,
775 enum built_in_class cl,
776 const char *library_name,
777 tree attrs)
778 {
779 tree decl = build_builtin_function (BUILTINS_LOCATION, name, type,
780 function_code, cl, library_name, attrs);
781 return lang_hooks.builtin_function_ext_scope (decl);
782 }
783
784 /* Simulate a declaration of a target-specific built-in function at
785 location LOCATION, as though it had been declared directly in the
786 source language. NAME is the name of the function, TYPE is its function
787 type, FUNCTION_CODE is the target-specific function code, LIBRARY_NAME
788 is the name of the underlying library function (NULL if none) and
789 ATTRS is a list of function attributes.
790
791 Return the decl of the declared function. */
792
793 tree
794 simulate_builtin_function_decl (location_t location, const char *name,
795 tree type, int function_code,
796 const char *library_name, tree attrs)
797 {
798 tree decl = build_builtin_function (location, name, type,
799 function_code, BUILT_IN_MD,
800 library_name, attrs);
801 tree new_decl = lang_hooks.simulate_builtin_function_decl (decl);
802
803 /* Give the front end a chance to create a new decl if necessary,
804 but if the front end discards the decl in favour of a conflicting
805 (erroneous) previous definition, return the decl that we tried but
806 failed to add. This allows the caller to process the returned decl
807 normally, even though the source code won't be able to use it. */
808 if (TREE_CODE (new_decl) == FUNCTION_DECL
809 && fndecl_built_in_p (new_decl, function_code, BUILT_IN_MD))
810 return new_decl;
811
812 return decl;
813 }
814
815 tree
816 lhd_builtin_function (tree decl)
817 {
818 lang_hooks.decls.pushdecl (decl);
819 return decl;
820 }
821
822 /* Create a builtin type. */
823
824 tree
825 add_builtin_type (const char *name, tree type)
826 {
827 tree id = get_identifier (name);
828 tree decl = build_decl (BUILTINS_LOCATION, TYPE_DECL, id, type);
829 return lang_hooks.decls.pushdecl (decl);
830 }
831
832 /* LTO hooks. */
833
834 /* Used to save and restore any previously active section. */
835 static section *saved_section;
836
837
838 /* Begin a new LTO output section named NAME. This default implementation
839 saves the old section and emits assembly code to switch to the new
840 section. */
841
842 void
843 lhd_begin_section (const char *name)
844 {
845 section *section;
846
847 /* Save the old section so we can restore it in lto_end_asm_section. */
848 gcc_assert (!saved_section);
849 saved_section = in_section;
850 if (!saved_section)
851 saved_section = text_section;
852
853 /* Create a new section and switch to it. */
854 section = get_section (name, SECTION_DEBUG | SECTION_EXCLUDE, NULL, true);
855 switch_to_section (section);
856 }
857
858
859 /* Write DATA of length LEN to the current LTO output section. This default
860 implementation just calls assemble_string. */
861
862 void
863 lhd_append_data (const void *data, size_t len, void *)
864 {
865 if (data)
866 {
867 timevar_push (TV_IPA_LTO_OUTPUT);
868 assemble_string ((const char *)data, len);
869 timevar_pop (TV_IPA_LTO_OUTPUT);
870 }
871 }
872
873
874 /* Finish the current LTO output section. This default implementation emits
875 assembly code to switch to any section previously saved by
876 lhd_begin_section. */
877
878 void
879 lhd_end_section (void)
880 {
881 if (saved_section)
882 {
883 switch_to_section (saved_section);
884 saved_section = NULL;
885 }
886 }
887
888 /* Default implementation of enum_underlying_base_type using type_for_size. */
889
890 tree
891 lhd_enum_underlying_base_type (const_tree enum_type)
892 {
893 return lang_hooks.types.type_for_size (TYPE_PRECISION (enum_type),
894 TYPE_UNSIGNED (enum_type));
895 }
896
897 /* Default implementation of LANG_HOOKS_GET_SUBSTRING_LOCATION. */
898
899 const char *
900 lhd_get_substring_location (const substring_loc &, location_t *)
901 {
902 return "unimplemented";
903 }
904
905 /* Default implementation of LANG_HOOKS_DECL_DWARF_ATTRIBUTE. Don't add
906 any attributes. */
907
908 int
909 lhd_decl_dwarf_attribute (const_tree, int)
910 {
911 return -1;
912 }
913
914 /* Default implementation of LANG_HOOKS_TYPE_DWARF_ATTRIBUTE. Don't add
915 any attributes. */
916
917 int
918 lhd_type_dwarf_attribute (const_tree, int)
919 {
920 return -1;
921 }
922
923 /* Default implementation of LANG_HOOKS_UNIT_SIZE_WITHOUT_REUSABLE_PADDING.
924 Just return TYPE_SIZE_UNIT unadjusted. */
925
926 tree
927 lhd_unit_size_without_reusable_padding (tree t)
928 {
929 return TYPE_SIZE_UNIT (t);
930 }
931
932 /* Default implementation for the finalize_early_debug hook. */
933
934 void
935 lhd_finalize_early_debug (void)
936 {
937 /* Emit early debug for reachable functions, and by consequence,
938 locally scoped symbols. */
939 struct cgraph_node *cnode;
940 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (cnode)
941 (*debug_hooks->early_global_decl) (cnode->decl);
942 }
943
944 /* Default implementation of LANG_HOOKS_GET_SARIF_SOURCE_LANGUAGE. */
945
946 const char *
947 lhd_get_sarif_source_language (const char *)
948 {
949 return NULL;
950 }
951
952 /* Returns true if the current lang_hooks represents the GNU C frontend. */
953
954 bool
955 lang_GNU_C (void)
956 {
957 return (startswith (lang_hooks.name, "GNU C")
958 && (lang_hooks.name[5] == '\0' || ISDIGIT (lang_hooks.name[5])));
959 }
960
961 /* Returns true if the current lang_hooks represents the GNU C++ frontend. */
962
963 bool
964 lang_GNU_CXX (void)
965 {
966 return startswith (lang_hooks.name, "GNU C++");
967 }
968
969 /* Returns true if the current lang_hooks represents the GNU Fortran frontend. */
970
971 bool
972 lang_GNU_Fortran (void)
973 {
974 return startswith (lang_hooks.name, "GNU Fortran");
975 }
976
977 /* Returns true if the current lang_hooks represents the GNU Objective-C
978 frontend. */
979
980 bool
981 lang_GNU_OBJC (void)
982 {
983 return startswith (lang_hooks.name, "GNU Objective-C");
984 }