]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/langhooks.c
c-decl.c (pop_scope): Do not set DECL_CONTEXT on file-scope decls when...
[thirdparty/gcc.git] / gcc / langhooks.c
1 /* Default language-specific hooks.
2 Copyright 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3 Contributed by Alexandre Oliva <aoliva@redhat.com>
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 2, 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 COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "toplev.h"
27 #include "tree.h"
28 #include "tree-inline.h"
29 #include "tree-gimple.h"
30 #include "rtl.h"
31 #include "insn-config.h"
32 #include "integrate.h"
33 #include "flags.h"
34 #include "langhooks.h"
35 #include "langhooks-def.h"
36 #include "ggc.h"
37 #include "diagnostic.h"
38
39 /* Do nothing; in many cases the default hook. */
40
41 void
42 lhd_do_nothing (void)
43 {
44 }
45
46 /* Do nothing (tree). */
47
48 void
49 lhd_do_nothing_t (tree t ATTRIBUTE_UNUSED)
50 {
51 }
52
53 /* Do nothing (int). */
54
55 void
56 lhd_do_nothing_i (int i ATTRIBUTE_UNUSED)
57 {
58 }
59
60 /* Do nothing (int, int, int). Return NULL_TREE. */
61
62 tree
63 lhd_do_nothing_iii_return_null_tree (int i ATTRIBUTE_UNUSED,
64 int j ATTRIBUTE_UNUSED,
65 int k ATTRIBUTE_UNUSED)
66 {
67 return NULL_TREE;
68 }
69
70 /* Do nothing (function). */
71
72 void
73 lhd_do_nothing_f (struct function *f ATTRIBUTE_UNUSED)
74 {
75 }
76
77 /* Do nothing (return the tree node passed). */
78
79 tree
80 lhd_return_tree (tree t)
81 {
82 return t;
83 }
84
85 /* Do nothing (return NULL_TREE). */
86
87 tree
88 lhd_return_null_tree_v (void)
89 {
90 return NULL_TREE;
91 }
92
93 /* Do nothing (return NULL_TREE). */
94
95 tree
96 lhd_return_null_tree (tree t ATTRIBUTE_UNUSED)
97 {
98 return NULL_TREE;
99 }
100
101 /* The default post options hook. */
102
103 bool
104 lhd_post_options (const char **pfilename ATTRIBUTE_UNUSED)
105 {
106 return false;
107 }
108
109 /* Called from by print-tree.c. */
110
111 void
112 lhd_print_tree_nothing (FILE *file ATTRIBUTE_UNUSED,
113 tree node ATTRIBUTE_UNUSED,
114 int indent ATTRIBUTE_UNUSED)
115 {
116 }
117
118 /* Called from safe_from_p. */
119
120 int
121 lhd_safe_from_p (rtx x ATTRIBUTE_UNUSED, tree exp ATTRIBUTE_UNUSED)
122 {
123 return 1;
124 }
125
126 /* Called from unsafe_for_reeval. */
127
128 int
129 lhd_unsafe_for_reeval (tree t ATTRIBUTE_UNUSED)
130 {
131 return -1;
132 }
133
134 /* Called from staticp. */
135
136 int
137 lhd_staticp (tree exp ATTRIBUTE_UNUSED)
138 {
139 return 0;
140 }
141
142 /* Called from check_global_declarations. */
143
144 bool
145 lhd_warn_unused_global_decl (tree decl)
146 {
147 /* This is what used to exist in check_global_declarations. Probably
148 not many of these actually apply to non-C languages. */
149
150 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_INLINE (decl))
151 return false;
152 if (TREE_CODE (decl) == VAR_DECL && TREE_READONLY (decl))
153 return false;
154 if (DECL_IN_SYSTEM_HEADER (decl))
155 return false;
156
157 return true;
158 }
159
160 /* Set the DECL_ASSEMBLER_NAME for DECL. */
161 void
162 lhd_set_decl_assembler_name (tree decl)
163 {
164 /* The language-independent code should never use the
165 DECL_ASSEMBLER_NAME for lots of DECLs. Only FUNCTION_DECLs and
166 VAR_DECLs for variables with static storage duration need a real
167 DECL_ASSEMBLER_NAME. */
168 if (TREE_CODE (decl) == FUNCTION_DECL
169 || (TREE_CODE (decl) == VAR_DECL
170 && (TREE_STATIC (decl)
171 || DECL_EXTERNAL (decl)
172 || TREE_PUBLIC (decl))))
173 {
174 /* By default, assume the name to use in assembly code is the
175 same as that used in the source language. (That's correct
176 for C, and GCC used to set DECL_ASSEMBLER_NAME to the same
177 value as DECL_NAME in build_decl, so this choice provides
178 backwards compatibility with existing front-ends.
179
180 Can't use just the variable's own name for a variable whose
181 scope is less than the whole compilation. Concatenate a
182 distinguishing number - we use the DECL_UID. */
183 if (TREE_PUBLIC (decl) || DECL_CONTEXT (decl) == NULL_TREE)
184 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
185 else
186 {
187 const char *name = IDENTIFIER_POINTER (DECL_NAME (decl));
188 char *label;
189
190 ASM_FORMAT_PRIVATE_NAME (label, name, DECL_UID (decl));
191 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (label));
192 }
193 }
194 else
195 /* Nobody should ever be asking for the DECL_ASSEMBLER_NAME of
196 these DECLs -- unless they're in language-dependent code, in
197 which case set_decl_assembler_name hook should handle things. */
198 abort ();
199 }
200
201 /* By default we always allow bit-field based optimizations. */
202 bool
203 lhd_can_use_bit_fields_p (void)
204 {
205 return true;
206 }
207
208 /* Provide a default routine to clear the binding stack. This is used
209 by languages that don't need to do anything special. */
210 void
211 lhd_clear_binding_stack (void)
212 {
213 while (! lang_hooks.decls.global_bindings_p ())
214 lang_hooks.decls.poplevel (0, 0, 0);
215 }
216
217 /* Type promotion for variable arguments. */
218 tree
219 lhd_type_promotes_to (tree type ATTRIBUTE_UNUSED)
220 {
221 abort ();
222 }
223
224 /* Registration of machine- or os-specific builtin types. */
225 void
226 lhd_register_builtin_type (tree type ATTRIBUTE_UNUSED,
227 const char* name ATTRIBUTE_UNUSED)
228 {
229 }
230
231 /* Invalid use of an incomplete type. */
232 void
233 lhd_incomplete_type_error (tree value ATTRIBUTE_UNUSED, tree type)
234 {
235 if (TREE_CODE (type) == ERROR_MARK)
236 return;
237
238 abort ();
239 }
240
241 /* Provide a default routine for alias sets that always returns -1. This
242 is used by languages that don't need to do anything special. */
243
244 HOST_WIDE_INT
245 lhd_get_alias_set (tree t ATTRIBUTE_UNUSED)
246 {
247 return -1;
248 }
249
250 /* Provide a hook routine for alias sets that always returns 0. This is
251 used by languages that haven't deal with alias sets yet. */
252
253 HOST_WIDE_INT
254 hook_get_alias_set_0 (tree t ATTRIBUTE_UNUSED)
255 {
256 return 0;
257 }
258
259 /* This is the default expand_expr function. */
260
261 rtx
262 lhd_expand_expr (tree t ATTRIBUTE_UNUSED, rtx r ATTRIBUTE_UNUSED,
263 enum machine_mode mm ATTRIBUTE_UNUSED,
264 int em ATTRIBUTE_UNUSED,
265 rtx *a ATTRIBUTE_UNUSED)
266 {
267 abort ();
268 }
269
270 /* The default language-specific function for expanding a decl. After
271 the language-independent cases are handled, this function will be
272 called. If this function is not defined, it is assumed that
273 declarations other than those for variables and labels do not require
274 any RTL generation. */
275
276 int
277 lhd_expand_decl (tree t ATTRIBUTE_UNUSED)
278 {
279 return 0;
280 }
281
282 /* This is the default decl_printable_name function. */
283
284 const char *
285 lhd_decl_printable_name (tree decl, int verbosity ATTRIBUTE_UNUSED)
286 {
287 return IDENTIFIER_POINTER (DECL_NAME (decl));
288 }
289
290 /* This compares two types for equivalence ("compatible" in C-based languages).
291 This routine should only return 1 if it is sure. It should not be used
292 in contexts where erroneously returning 0 causes problems. */
293
294 int
295 lhd_types_compatible_p (tree x, tree y)
296 {
297 return TYPE_MAIN_VARIANT (x) == TYPE_MAIN_VARIANT (y);
298 }
299
300 /* lang_hooks.tree_inlining.walk_subtrees is called by walk_tree()
301 after handling common cases, but before walking code-specific
302 sub-trees. If this hook is overridden for a language, it should
303 handle language-specific tree codes, as well as language-specific
304 information associated to common tree codes. If a tree node is
305 completely handled within this function, it should set *SUBTREES to
306 0, so that generic handling isn't attempted. For language-specific
307 tree codes, generic handling would abort(), so make sure it is set
308 properly. Both SUBTREES and *SUBTREES is guaranteed to be nonzero
309 when the function is called. */
310
311 tree
312 lhd_tree_inlining_walk_subtrees (tree *tp ATTRIBUTE_UNUSED,
313 int *subtrees ATTRIBUTE_UNUSED,
314 walk_tree_fn func ATTRIBUTE_UNUSED,
315 void *data ATTRIBUTE_UNUSED,
316 void *htab ATTRIBUTE_UNUSED)
317 {
318 return NULL_TREE;
319 }
320
321 /* lang_hooks.tree_inlining.cannot_inline_tree_fn is called to
322 determine whether there are language-specific reasons for not
323 inlining a given function. */
324
325 int
326 lhd_tree_inlining_cannot_inline_tree_fn (tree *fnp)
327 {
328 if (flag_really_no_inline
329 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (*fnp)) == NULL)
330 return 1;
331
332 return 0;
333 }
334
335 /* lang_hooks.tree_inlining.disregard_inline_limits is called to
336 determine whether a function should be considered for inlining even
337 if it would exceed inlining limits. */
338
339 int
340 lhd_tree_inlining_disregard_inline_limits (tree fn)
341 {
342 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)) != NULL)
343 return 1;
344
345 return 0;
346 }
347
348 /* lang_hooks.tree_inlining.add_pending_fn_decls is called before
349 starting to inline a function, to push any language-specific
350 functions that should not be inlined into the current function,
351 into VAFNP. PFN is the top of varray, and should be returned if no
352 functions are pushed into VAFNP. The top of the varray should be
353 returned. */
354
355 tree
356 lhd_tree_inlining_add_pending_fn_decls (void *vafnp ATTRIBUTE_UNUSED, tree pfn)
357 {
358 return pfn;
359 }
360
361 /* lang_hooks.tree_inlining.auto_var_in_fn_p is called to determine
362 whether VT is an automatic variable defined in function FT. */
363
364 int
365 lhd_tree_inlining_auto_var_in_fn_p (tree var, tree fn)
366 {
367 return (DECL_P (var) && DECL_CONTEXT (var) == fn
368 && (((TREE_CODE (var) == VAR_DECL || TREE_CODE (var) == PARM_DECL)
369 && ! TREE_STATIC (var))
370 || TREE_CODE (var) == LABEL_DECL
371 || TREE_CODE (var) == RESULT_DECL));
372 }
373
374 /* lang_hooks.tree_inlining.copy_res_decl_for_inlining should return a
375 declaration for the result RES of function FN to be inlined into
376 CALLER. NDP points to an integer that should be set in case a new
377 declaration wasn't created (presumably because RES was of aggregate
378 type, such that a TARGET_EXPR is used for the result). TEXPS is a
379 pointer to a varray with the stack of TARGET_EXPRs seen while
380 inlining functions into caller; the top of TEXPS is supposed to
381 match RES. */
382
383 tree
384 lhd_tree_inlining_copy_res_decl_for_inlining (tree res, tree fn, tree caller,
385 void *dm ATTRIBUTE_UNUSED,
386 int *ndp ATTRIBUTE_UNUSED,
387 tree return_slot_addr ATTRIBUTE_UNUSED)
388 {
389 if (return_slot_addr)
390 return build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (return_slot_addr)),
391 return_slot_addr);
392 else
393 return copy_decl_for_inlining (res, fn, caller);
394 }
395
396 /* lang_hooks.tree_inlining.anon_aggr_type_p determines whether T is a
397 type node representing an anonymous aggregate (union, struct, etc),
398 i.e., one whose members are in the same scope as the union itself. */
399
400 int
401 lhd_tree_inlining_anon_aggr_type_p (tree t ATTRIBUTE_UNUSED)
402 {
403 return 0;
404 }
405
406 /* lang_hooks.tree_inlining.start_inlining and end_inlining perform any
407 language-specific bookkeeping necessary for processing
408 FN. start_inlining returns nonzero if inlining should proceed, zero if
409 not.
410
411 For instance, the C++ version keeps track of template instantiations to
412 avoid infinite recursion. */
413
414 int
415 lhd_tree_inlining_start_inlining (tree fn ATTRIBUTE_UNUSED)
416 {
417 return 1;
418 }
419
420 void
421 lhd_tree_inlining_end_inlining (tree fn ATTRIBUTE_UNUSED)
422 {
423 }
424
425 /* lang_hooks.tree_inlining.convert_parm_for_inlining performs any
426 language-specific conversion before assigning VALUE to PARM. */
427
428 tree
429 lhd_tree_inlining_convert_parm_for_inlining (tree parm ATTRIBUTE_UNUSED,
430 tree value,
431 tree fndecl ATTRIBUTE_UNUSED,
432 int argnum ATTRIBUTE_UNUSED)
433 {
434 return value;
435 }
436
437 /* lang_hooks.tree_dump.dump_tree: Dump language-specific parts of tree
438 nodes. Returns nonzero if it does not want the usual dumping of the
439 second argument. */
440
441 bool
442 lhd_tree_dump_dump_tree (void *di ATTRIBUTE_UNUSED, tree t ATTRIBUTE_UNUSED)
443 {
444 return false;
445 }
446
447 /* lang_hooks.tree_dump.type_qual: Determine type qualifiers in a
448 language-specific way. */
449
450 int
451 lhd_tree_dump_type_quals (tree t)
452 {
453 return TYPE_QUALS (t);
454 }
455
456 /* lang_hooks.expr_size: Determine the size of the value of an expression T
457 in a language-specific way. Returns a tree for the size in bytes. */
458
459 tree
460 lhd_expr_size (tree exp)
461 {
462 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'd'
463 && DECL_SIZE_UNIT (exp) != 0)
464 return DECL_SIZE_UNIT (exp);
465 else
466 return size_in_bytes (TREE_TYPE (exp));
467 }
468
469 /* lang_hooks.gimplify_expr re-writes *EXPR_P into GIMPLE form. */
470
471 int
472 lhd_gimplify_expr (tree *expr_p ATTRIBUTE_UNUSED, tree *pre_p ATTRIBUTE_UNUSED,
473 tree *post_p ATTRIBUTE_UNUSED)
474 {
475 return GS_UNHANDLED;
476 }
477
478 /* lang_hooks.tree_size: Determine the size of a tree with code C,
479 which is a language-specific tree code in category 'x'. The
480 default expects never to be called. */
481 size_t
482 lhd_tree_size (enum tree_code c ATTRIBUTE_UNUSED)
483 {
484 abort ();
485 return 0;
486 }
487
488 /* Return true if decl, which is a function decl, may be called by a
489 sibcall. */
490
491 bool
492 lhd_decl_ok_for_sibcall (tree decl ATTRIBUTE_UNUSED)
493 {
494 return true;
495 }
496
497 /* lang_hooks.decls.final_write_globals: perform final processing on
498 global variables. */
499 void
500 write_global_declarations (void)
501 {
502 /* Really define vars that have had only a tentative definition.
503 Really output inline functions that must actually be callable
504 and have not been output so far. */
505
506 tree globals = lang_hooks.decls.getdecls ();
507 int len = list_length (globals);
508 tree *vec = xmalloc (sizeof (tree) * len);
509 int i;
510 tree decl;
511
512 /* Process the decls in reverse order--earliest first.
513 Put them into VEC from back to front, then take out from front. */
514
515 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
516 vec[len - i - 1] = decl;
517
518 wrapup_global_declarations (vec, len);
519
520 check_global_declarations (vec, len);
521
522 /* Clean up. */
523 free (vec);
524 }
525
526 /* Called to perform language-specific initialization of CTX. */
527 void
528 lhd_initialize_diagnostics (struct diagnostic_context *ctx ATTRIBUTE_UNUSED)
529 {
530 }
531
532 /* The default function to print out name of current function that caused
533 an error. */
534 void
535 lhd_print_error_function (diagnostic_context *context, const char *file)
536 {
537 if (diagnostic_last_function_changed (context))
538 {
539 const char *old_prefix = context->printer->prefix;
540 char *new_prefix = file ? file_name_as_prefix (file) : NULL;
541
542 pp_set_prefix (context->printer, new_prefix);
543
544 if (current_function_decl == NULL)
545 pp_printf (context->printer, "At top level:");
546 else
547 {
548 if (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE)
549 pp_printf
550 (context->printer, "In member function `%s':",
551 lang_hooks.decl_printable_name (current_function_decl, 2));
552 else
553 pp_printf
554 (context->printer, "In function `%s':",
555 lang_hooks.decl_printable_name (current_function_decl, 2));
556 }
557
558 diagnostic_set_last_function (context);
559 pp_flush (context->printer);
560 context->printer->prefix = old_prefix;
561 free ((char*) new_prefix);
562 }
563 }
564
565 tree
566 lhd_callgraph_analyze_expr (tree *tp ATTRIBUTE_UNUSED,
567 int *walk_subtrees ATTRIBUTE_UNUSED,
568 tree decl ATTRIBUTE_UNUSED)
569 {
570 return NULL;
571 }
572
573 tree
574 lhd_make_node (enum tree_code code)
575 {
576 return make_node (code);
577 }