2007-01-03 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / gcc / fortran / f95-lang.c
blob4caaa1a63c315e724b3f77c8b216df8abb6481d8
1 /* gfortran backend interface
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006
3 Free Software Foundation, Inc.
4 Contributed by Paul Brook.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 /* f95-lang.c-- GCC backend interface stuff */
25 /* declare required prototypes: */
27 #include "config.h"
28 #include "system.h"
29 #include "ansidecl.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tree.h"
33 #include "tree-gimple.h"
34 #include "flags.h"
35 #include "langhooks.h"
36 #include "langhooks-def.h"
37 #include "timevar.h"
38 #include "tm.h"
39 #include "function.h"
40 #include "ggc.h"
41 #include "toplev.h"
42 #include "target.h"
43 #include "debug.h"
44 #include "diagnostic.h"
45 #include "tree-dump.h"
46 #include "cgraph.h"
48 #include "gfortran.h"
49 #include "trans.h"
50 #include "trans-types.h"
51 #include "trans-const.h"
53 /* Language-dependent contents of an identifier. */
55 struct lang_identifier
56 GTY(())
58 struct tree_identifier common;
61 /* The resulting tree type. */
63 union lang_tree_node
64 GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
65 chain_next ("(GIMPLE_STMT_P (&%h.generic) ? (union lang_tree_node *) 0 : (union lang_tree_node *)TREE_CHAIN (&%h.generic))")))
68 union tree_node GTY((tag ("0"),
69 desc ("tree_node_structure (&%h)"))) generic;
70 struct lang_identifier GTY((tag ("1"))) identifier;
73 /* Save and restore the variables in this file and elsewhere
74 that keep track of the progress of compilation of the current function.
75 Used for nested functions. */
77 struct language_function
78 GTY(())
80 /* struct gfc_language_function base; */
81 struct binding_level *binding_level;
84 /* We don't have a lex/yacc lexer/parser, but toplev expects these to
85 exist anyway. */
86 void yyerror (const char *str);
87 int yylex (void);
89 static void gfc_init_decl_processing (void);
90 static void gfc_init_builtin_functions (void);
92 /* Each front end provides its own. */
93 static bool gfc_init (void);
94 static void gfc_finish (void);
95 static void gfc_print_identifier (FILE *, tree, int);
96 static bool gfc_mark_addressable (tree);
97 void do_function_end (void);
98 int global_bindings_p (void);
99 void insert_block (tree);
100 static void gfc_clear_binding_stack (void);
101 static void gfc_be_parse_file (int);
102 static void gfc_expand_function (tree);
103 static HOST_WIDE_INT gfc_get_alias_set (tree);
105 #undef LANG_HOOKS_NAME
106 #undef LANG_HOOKS_INIT
107 #undef LANG_HOOKS_FINISH
108 #undef LANG_HOOKS_INIT_OPTIONS
109 #undef LANG_HOOKS_HANDLE_OPTION
110 #undef LANG_HOOKS_POST_OPTIONS
111 #undef LANG_HOOKS_PRINT_IDENTIFIER
112 #undef LANG_HOOKS_PARSE_FILE
113 #undef LANG_HOOKS_MARK_ADDRESSABLE
114 #undef LANG_HOOKS_TYPE_FOR_MODE
115 #undef LANG_HOOKS_TYPE_FOR_SIZE
116 #undef LANG_HOOKS_UNSIGNED_TYPE
117 #undef LANG_HOOKS_SIGNED_TYPE
118 #undef LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE
119 #undef LANG_HOOKS_CALLGRAPH_EXPAND_FUNCTION
120 #undef LANG_HOOKS_CLEAR_BINDING_STACK
121 #undef LANG_HOOKS_GET_ALIAS_SET
122 #undef LANG_HOOKS_OMP_PRIVATIZE_BY_REFERENCE
123 #undef LANG_HOOKS_OMP_PREDETERMINED_SHARING
124 #undef LANG_HOOKS_OMP_CLAUSE_DEFAULT_CTOR
125 #undef LANG_HOOKS_OMP_DISREGARD_VALUE_EXPR
126 #undef LANG_HOOKS_OMP_PRIVATE_DEBUG_CLAUSE
127 #undef LANG_HOOKS_OMP_FIRSTPRIVATIZE_TYPE_SIZES
128 #undef LANG_HOOKS_BUILTIN_FUNCTION
130 /* Define lang hooks. */
131 #define LANG_HOOKS_NAME "GNU F95"
132 #define LANG_HOOKS_INIT gfc_init
133 #define LANG_HOOKS_FINISH gfc_finish
134 #define LANG_HOOKS_INIT_OPTIONS gfc_init_options
135 #define LANG_HOOKS_HANDLE_OPTION gfc_handle_option
136 #define LANG_HOOKS_POST_OPTIONS gfc_post_options
137 #define LANG_HOOKS_PRINT_IDENTIFIER gfc_print_identifier
138 #define LANG_HOOKS_PARSE_FILE gfc_be_parse_file
139 #define LANG_HOOKS_MARK_ADDRESSABLE gfc_mark_addressable
140 #define LANG_HOOKS_TYPE_FOR_MODE gfc_type_for_mode
141 #define LANG_HOOKS_TYPE_FOR_SIZE gfc_type_for_size
142 #define LANG_HOOKS_UNSIGNED_TYPE gfc_unsigned_type
143 #define LANG_HOOKS_SIGNED_TYPE gfc_signed_type
144 #define LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE gfc_signed_or_unsigned_type
145 #define LANG_HOOKS_CALLGRAPH_EXPAND_FUNCTION gfc_expand_function
146 #define LANG_HOOKS_CLEAR_BINDING_STACK gfc_clear_binding_stack
147 #define LANG_HOOKS_GET_ALIAS_SET gfc_get_alias_set
148 #define LANG_HOOKS_OMP_PRIVATIZE_BY_REFERENCE gfc_omp_privatize_by_reference
149 #define LANG_HOOKS_OMP_PREDETERMINED_SHARING gfc_omp_predetermined_sharing
150 #define LANG_HOOKS_OMP_CLAUSE_DEFAULT_CTOR gfc_omp_clause_default_ctor
151 #define LANG_HOOKS_OMP_DISREGARD_VALUE_EXPR gfc_omp_disregard_value_expr
152 #define LANG_HOOKS_OMP_PRIVATE_DEBUG_CLAUSE gfc_omp_private_debug_clause
153 #define LANG_HOOKS_OMP_FIRSTPRIVATIZE_TYPE_SIZES \
154 gfc_omp_firstprivatize_type_sizes
155 #define LANG_HOOKS_BUILTIN_FUNCTION gfc_builtin_function
157 const struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
159 /* A list (chain of TREE_LIST nodes) of all LABEL_DECLs in the function
160 that have names. Here so we can clear out their names' definitions
161 at the end of the function. */
163 /* Tree code classes. */
165 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
167 const enum tree_code_class tree_code_type[] = {
168 #include "tree.def"
170 #undef DEFTREECODE
172 /* Table indexed by tree code giving number of expression
173 operands beyond the fixed part of the node structure.
174 Not used for types or decls. */
176 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
178 const unsigned char tree_code_length[] = {
179 #include "tree.def"
181 #undef DEFTREECODE
183 /* Names of tree components.
184 Used for printing out the tree and error messages. */
185 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
187 const char *const tree_code_name[] = {
188 #include "tree.def"
190 #undef DEFTREECODE
193 #define NULL_BINDING_LEVEL (struct binding_level *) NULL
195 /* A chain of binding_level structures awaiting reuse. */
197 static GTY(()) struct binding_level *free_binding_level;
199 /* The elements of `ridpointers' are identifier nodes
200 for the reserved type names and storage classes.
201 It is indexed by a RID_... value. */
202 tree *ridpointers = NULL;
204 /* language-specific flags. */
206 static void
207 gfc_expand_function (tree fndecl)
209 tree t;
211 if (DECL_INITIAL (fndecl)
212 && BLOCK_SUBBLOCKS (DECL_INITIAL (fndecl)))
214 /* Local static equivalenced variables are never seen by
215 check_global_declarations, so we need to output debug
216 info by hand. */
218 t = BLOCK_SUBBLOCKS (DECL_INITIAL (fndecl));
219 for (t = BLOCK_VARS (t); t; t = TREE_CHAIN (t))
220 if (TREE_CODE (t) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (t)
221 && TREE_STATIC (t))
223 tree expr = DECL_VALUE_EXPR (t);
225 if (TREE_CODE (expr) == COMPONENT_REF
226 && TREE_CODE (TREE_OPERAND (expr, 0)) == VAR_DECL
227 && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0)))
228 == UNION_TYPE
229 && varpool_node (TREE_OPERAND (expr, 0))->needed
230 && errorcount == 0 && sorrycount == 0)
232 timevar_push (TV_SYMOUT);
233 (*debug_hooks->global_decl) (t);
234 timevar_pop (TV_SYMOUT);
239 tree_rest_of_compilation (fndecl);
243 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
244 or validate its data type for an `if' or `while' statement or ?..: exp.
246 This preparation consists of taking the ordinary
247 representation of an expression expr and producing a valid tree
248 boolean expression describing whether expr is nonzero. We could
249 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
250 but we optimize comparisons, &&, ||, and !.
252 The resulting type should always be `boolean_type_node'.
253 This is much simpler than the corresponding C version because we have a
254 distinct boolean type. */
256 tree
257 gfc_truthvalue_conversion (tree expr)
259 switch (TREE_CODE (TREE_TYPE (expr)))
261 case BOOLEAN_TYPE:
262 if (TREE_TYPE (expr) == boolean_type_node)
263 return expr;
264 else if (COMPARISON_CLASS_P (expr))
266 TREE_TYPE (expr) = boolean_type_node;
267 return expr;
269 else if (TREE_CODE (expr) == NOP_EXPR)
270 return build1 (NOP_EXPR, boolean_type_node,
271 TREE_OPERAND (expr, 0));
272 else
273 return build1 (NOP_EXPR, boolean_type_node, expr);
275 case INTEGER_TYPE:
276 if (TREE_CODE (expr) == INTEGER_CST)
277 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
278 else
279 return build2 (NE_EXPR, boolean_type_node, expr,
280 build_int_cst (TREE_TYPE (expr), 0));
282 default:
283 internal_error ("Unexpected type in truthvalue_conversion");
287 static void
288 gfc_create_decls (void)
290 /* GCC builtins. */
291 gfc_init_builtin_functions ();
293 /* Runtime/IO library functions. */
294 gfc_build_builtin_function_decls ();
296 gfc_init_constants ();
299 static void
300 gfc_be_parse_file (int set_yydebug ATTRIBUTE_UNUSED)
302 int errors;
303 int warnings;
305 gfc_create_decls ();
306 gfc_parse_file ();
307 gfc_generate_constructors ();
309 cgraph_finalize_compilation_unit ();
310 cgraph_optimize ();
312 /* Tell the frontent about any errors. */
313 gfc_get_errors (&warnings, &errors);
314 errorcount += errors;
315 warningcount += warnings;
318 /* Initialize everything. */
320 static bool
321 gfc_init (void)
323 #ifdef USE_MAPPED_LOCATION
324 linemap_add (&line_table, LC_ENTER, false, gfc_source_file, 1);
325 linemap_add (&line_table, LC_RENAME, false, "<built-in>", 0);
326 #endif
328 /* First initialize the backend. */
329 gfc_init_decl_processing ();
330 gfc_static_ctors = NULL_TREE;
332 /* Then the frontend. */
333 gfc_init_1 ();
335 if (gfc_new_file () != SUCCESS)
336 fatal_error ("can't open input file: %s", gfc_source_file);
337 return true;
341 static void
342 gfc_finish (void)
344 gfc_done_1 ();
345 gfc_release_include_path ();
346 return;
349 static void
350 gfc_print_identifier (FILE * file ATTRIBUTE_UNUSED,
351 tree node ATTRIBUTE_UNUSED,
352 int indent ATTRIBUTE_UNUSED)
354 return;
358 /* These functions and variables deal with binding contours. We only
359 need these functions for the list of PARM_DECLs, but we leave the
360 functions more general; these are a simplified version of the
361 functions from GNAT. */
363 /* For each binding contour we allocate a binding_level structure which records
364 the entities defined or declared in that contour. Contours include:
366 the global one
367 one for each subprogram definition
368 one for each compound statement (declare block)
370 Binding contours are used to create GCC tree BLOCK nodes. */
372 struct binding_level
373 GTY(())
375 /* A chain of ..._DECL nodes for all variables, constants, functions,
376 parameters and type declarations. These ..._DECL nodes are chained
377 through the TREE_CHAIN field. Note that these ..._DECL nodes are stored
378 in the reverse of the order supplied to be compatible with the
379 back-end. */
380 tree names;
381 /* For each level (except the global one), a chain of BLOCK nodes for all
382 the levels that were entered and exited one level down from this one. */
383 tree blocks;
384 /* The binding level containing this one (the enclosing binding level). */
385 struct binding_level *level_chain;
388 /* The binding level currently in effect. */
389 static GTY(()) struct binding_level *current_binding_level = NULL;
391 /* The outermost binding level. This binding level is created when the
392 compiler is started and it will exist through the entire compilation. */
393 static GTY(()) struct binding_level *global_binding_level;
395 /* Binding level structures are initialized by copying this one. */
396 static struct binding_level clear_binding_level = { NULL, NULL, NULL };
398 /* Return nonzero if we are currently in the global binding level. */
401 global_bindings_p (void)
403 return current_binding_level == global_binding_level ? -1 : 0;
406 tree
407 getdecls (void)
409 return current_binding_level->names;
412 /* Enter a new binding level. The input parameter is ignored, but has to be
413 specified for back-end compatibility. */
415 void
416 pushlevel (int ignore ATTRIBUTE_UNUSED)
418 struct binding_level *newlevel
419 = (struct binding_level *) ggc_alloc (sizeof (struct binding_level));
421 *newlevel = clear_binding_level;
423 /* Add this level to the front of the chain (stack) of levels that are
424 active. */
425 newlevel->level_chain = current_binding_level;
426 current_binding_level = newlevel;
429 /* Exit a binding level.
430 Pop the level off, and restore the state of the identifier-decl mappings
431 that were in effect when this level was entered.
433 If KEEP is nonzero, this level had explicit declarations, so
434 and create a "block" (a BLOCK node) for the level
435 to record its declarations and subblocks for symbol table output.
437 If FUNCTIONBODY is nonzero, this level is the body of a function,
438 so create a block as if KEEP were set and also clear out all
439 label names.
441 If REVERSE is nonzero, reverse the order of decls before putting
442 them into the BLOCK. */
444 tree
445 poplevel (int keep, int reverse, int functionbody)
447 /* Points to a BLOCK tree node. This is the BLOCK node constructed for the
448 binding level that we are about to exit and which is returned by this
449 routine. */
450 tree block_node = NULL_TREE;
451 tree decl_chain;
452 tree subblock_chain = current_binding_level->blocks;
453 tree subblock_node;
455 /* Reverse the list of XXXX_DECL nodes if desired. Note that the ..._DECL
456 nodes chained through the `names' field of current_binding_level are in
457 reverse order except for PARM_DECL node, which are explicitly stored in
458 the right order. */
459 decl_chain = (reverse) ? nreverse (current_binding_level->names)
460 : current_binding_level->names;
462 /* If there were any declarations in the current binding level, or if this
463 binding level is a function body, or if there are any nested blocks then
464 create a BLOCK node to record them for the life of this function. */
465 if (keep || functionbody)
466 block_node = build_block (keep ? decl_chain : 0, subblock_chain, 0, 0);
468 /* Record the BLOCK node just built as the subblock its enclosing scope. */
469 for (subblock_node = subblock_chain; subblock_node;
470 subblock_node = TREE_CHAIN (subblock_node))
471 BLOCK_SUPERCONTEXT (subblock_node) = block_node;
473 /* Clear out the meanings of the local variables of this level. */
475 for (subblock_node = decl_chain; subblock_node;
476 subblock_node = TREE_CHAIN (subblock_node))
477 if (DECL_NAME (subblock_node) != 0)
478 /* If the identifier was used or addressed via a local extern decl,
479 don't forget that fact. */
480 if (DECL_EXTERNAL (subblock_node))
482 if (TREE_USED (subblock_node))
483 TREE_USED (DECL_NAME (subblock_node)) = 1;
484 if (TREE_ADDRESSABLE (subblock_node))
485 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (subblock_node)) = 1;
488 /* Pop the current level. */
489 current_binding_level = current_binding_level->level_chain;
491 if (functionbody)
493 /* This is the top level block of a function. The ..._DECL chain stored
494 in BLOCK_VARS are the function's parameters (PARM_DECL nodes). Don't
495 leave them in the BLOCK because they are found in the FUNCTION_DECL
496 instead. */
497 DECL_INITIAL (current_function_decl) = block_node;
498 BLOCK_VARS (block_node) = 0;
500 else if (block_node)
502 current_binding_level->blocks
503 = chainon (current_binding_level->blocks, block_node);
506 /* If we did not make a block for the level just exited, any blocks made for
507 inner levels (since they cannot be recorded as subblocks in that level)
508 must be carried forward so they will later become subblocks of something
509 else. */
510 else if (subblock_chain)
511 current_binding_level->blocks
512 = chainon (current_binding_level->blocks, subblock_chain);
513 if (block_node)
514 TREE_USED (block_node) = 1;
516 return block_node;
519 /* Insert BLOCK at the end of the list of subblocks of the
520 current binding level. This is used when a BIND_EXPR is expanded,
521 to handle the BLOCK node inside the BIND_EXPR. */
523 void
524 insert_block (tree block)
526 TREE_USED (block) = 1;
527 current_binding_level->blocks
528 = chainon (current_binding_level->blocks, block);
531 /* Records a ..._DECL node DECL as belonging to the current lexical scope.
532 Returns the ..._DECL node. */
534 tree
535 pushdecl (tree decl)
537 /* External objects aren't nested, other objects may be. */
538 if ((DECL_EXTERNAL (decl)) || (decl == current_function_decl))
539 DECL_CONTEXT (decl) = 0;
540 else
541 DECL_CONTEXT (decl) = current_function_decl;
543 /* Put the declaration on the list. The list of declarations is in reverse
544 order. The list will be reversed later if necessary. This needs to be
545 this way for compatibility with the back-end. */
547 TREE_CHAIN (decl) = current_binding_level->names;
548 current_binding_level->names = decl;
550 /* For the declaration of a type, set its name if it is not already set. */
552 if (TREE_CODE (decl) == TYPE_DECL && TYPE_NAME (TREE_TYPE (decl)) == 0)
554 if (DECL_SOURCE_LINE (decl) == 0)
555 TYPE_NAME (TREE_TYPE (decl)) = decl;
556 else
557 TYPE_NAME (TREE_TYPE (decl)) = DECL_NAME (decl);
560 return decl;
564 /* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL. */
566 tree
567 pushdecl_top_level (tree x)
569 tree t;
570 struct binding_level *b = current_binding_level;
572 current_binding_level = global_binding_level;
573 t = pushdecl (x);
574 current_binding_level = b;
575 return t;
579 /* Clear the binding stack. */
580 static void
581 gfc_clear_binding_stack (void)
583 while (!global_bindings_p ())
584 poplevel (0, 0, 0);
588 #ifndef CHAR_TYPE_SIZE
589 #define CHAR_TYPE_SIZE BITS_PER_UNIT
590 #endif
592 #ifndef INT_TYPE_SIZE
593 #define INT_TYPE_SIZE BITS_PER_WORD
594 #endif
596 #undef SIZE_TYPE
597 #define SIZE_TYPE "long unsigned int"
599 /* Create tree nodes for the basic scalar types of Fortran 95,
600 and some nodes representing standard constants (0, 1, (void *) 0).
601 Initialize the global binding level.
602 Make definitions for built-in primitive functions. */
603 static void
604 gfc_init_decl_processing (void)
606 current_function_decl = NULL;
607 current_binding_level = NULL_BINDING_LEVEL;
608 free_binding_level = NULL_BINDING_LEVEL;
610 /* Make the binding_level structure for global names. We move all
611 variables that are in a COMMON block to this binding level. */
612 pushlevel (0);
613 global_binding_level = current_binding_level;
615 /* Build common tree nodes. char_type_node is unsigned because we
616 only use it for actual characters, not for INTEGER(1). Also, we
617 want double_type_node to actually have double precision. */
618 build_common_tree_nodes (false, false);
619 set_sizetype (long_unsigned_type_node);
620 build_common_tree_nodes_2 (0);
621 void_list_node = build_tree_list (NULL_TREE, void_type_node);
623 /* Set up F95 type nodes. */
624 gfc_init_kinds ();
625 gfc_init_types ();
628 /* Mark EXP saying that we need to be able to take the
629 address of it; it should not be allocated in a register.
630 In Fortran 95 this is only the case for variables with
631 the TARGET attribute, but we implement it here for a
632 likely future Cray pointer extension.
633 Value is 1 if successful. */
634 /* TODO: Check/fix mark_addressable. */
635 bool
636 gfc_mark_addressable (tree exp)
638 register tree x = exp;
639 while (1)
640 switch (TREE_CODE (x))
642 case COMPONENT_REF:
643 case ADDR_EXPR:
644 case ARRAY_REF:
645 case REALPART_EXPR:
646 case IMAGPART_EXPR:
647 x = TREE_OPERAND (x, 0);
648 break;
650 case CONSTRUCTOR:
651 TREE_ADDRESSABLE (x) = 1;
652 return true;
654 case VAR_DECL:
655 case CONST_DECL:
656 case PARM_DECL:
657 case RESULT_DECL:
658 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x) && DECL_NONLOCAL (x))
660 if (TREE_PUBLIC (x))
662 error
663 ("global register variable %qs used in nested function",
664 IDENTIFIER_POINTER (DECL_NAME (x)));
665 return false;
667 pedwarn ("register variable %qs used in nested function",
668 IDENTIFIER_POINTER (DECL_NAME (x)));
670 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
672 if (TREE_PUBLIC (x))
674 error ("address of global register variable %qs requested",
675 IDENTIFIER_POINTER (DECL_NAME (x)));
676 return true;
679 #if 0
680 /* If we are making this addressable due to its having
681 volatile components, give a different error message. Also
682 handle the case of an unnamed parameter by not trying
683 to give the name. */
685 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
687 error ("cannot put object with volatile field into register");
688 return false;
690 #endif
692 pedwarn ("address of register variable %qs requested",
693 IDENTIFIER_POINTER (DECL_NAME (x)));
696 /* drops in */
697 case FUNCTION_DECL:
698 TREE_ADDRESSABLE (x) = 1;
700 default:
701 return true;
705 /* Return the typed-based alias set for T, which may be an expression
706 or a type. Return -1 if we don't do anything special. */
708 static HOST_WIDE_INT
709 gfc_get_alias_set (tree t)
711 tree u;
713 /* Permit type-punning when accessing an EQUIVALENCEd variable or
714 mixed type entry master's return value. */
715 for (u = t; handled_component_p (u); u = TREE_OPERAND (u, 0))
716 if (TREE_CODE (u) == COMPONENT_REF
717 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
718 return 0;
720 return -1;
723 /* press the big red button - garbage (ggc) collection is on */
725 int ggc_p = 1;
727 /* Builtin function initialization. */
729 tree
730 gfc_builtin_function (tree decl)
732 make_decl_rtl (decl);
733 pushdecl (decl);
734 return decl;
738 static void
739 gfc_define_builtin (const char * name,
740 tree type,
741 int code,
742 const char * library_name,
743 bool const_p)
745 tree decl;
747 decl = add_builtin_function (name, type, code, BUILT_IN_NORMAL,
748 library_name, NULL_TREE);
749 if (const_p)
750 TREE_READONLY (decl) = 1;
752 built_in_decls[code] = decl;
753 implicit_built_in_decls[code] = decl;
757 #define DO_DEFINE_MATH_BUILTIN(code, name, argtype, tbase) \
758 gfc_define_builtin ("__builtin_" name "l", tbase##longdouble[argtype], \
759 BUILT_IN_ ## code ## L, name "l", true); \
760 gfc_define_builtin ("__builtin_" name, tbase##double[argtype], \
761 BUILT_IN_ ## code, name, true); \
762 gfc_define_builtin ("__builtin_" name "f", tbase##float[argtype], \
763 BUILT_IN_ ## code ## F, name "f", true);
765 #define DEFINE_MATH_BUILTIN(code, name, argtype) \
766 DO_DEFINE_MATH_BUILTIN (code, name, argtype, mfunc_)
768 #define DEFINE_MATH_BUILTIN_C(code, name, argtype) \
769 DO_DEFINE_MATH_BUILTIN (code, name, argtype, mfunc_) \
770 DO_DEFINE_MATH_BUILTIN (C##code, "c" name, argtype, mfunc_c)
773 /* Create function types for builtin functions. */
775 static void
776 build_builtin_fntypes (tree * fntype, tree type)
778 tree tmp;
780 /* type (*) (type) */
781 tmp = tree_cons (NULL_TREE, type, void_list_node);
782 fntype[0] = build_function_type (type, tmp);
783 /* type (*) (type, type) */
784 tmp = tree_cons (NULL_TREE, type, tmp);
785 fntype[1] = build_function_type (type, tmp);
786 /* type (*) (int, type) */
787 tmp = tree_cons (NULL_TREE, integer_type_node, void_list_node);
788 tmp = tree_cons (NULL_TREE, type, tmp);
789 fntype[2] = build_function_type (type, tmp);
792 static tree
793 builtin_type_for_size (int size, bool unsignedp)
795 tree type = lang_hooks.types.type_for_size (size, unsignedp);
796 return type ? type : error_mark_node;
799 /* Initialization of builtin function nodes. */
801 static void
802 gfc_init_builtin_functions (void)
804 enum builtin_type
806 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
807 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
808 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
809 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
810 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
811 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
812 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
813 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) NAME,
814 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) NAME,
815 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
816 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
817 #include "types.def"
818 #undef DEF_PRIMITIVE_TYPE
819 #undef DEF_FUNCTION_TYPE_0
820 #undef DEF_FUNCTION_TYPE_1
821 #undef DEF_FUNCTION_TYPE_2
822 #undef DEF_FUNCTION_TYPE_3
823 #undef DEF_FUNCTION_TYPE_4
824 #undef DEF_FUNCTION_TYPE_5
825 #undef DEF_FUNCTION_TYPE_6
826 #undef DEF_FUNCTION_TYPE_7
827 #undef DEF_FUNCTION_TYPE_VAR_0
828 #undef DEF_POINTER_TYPE
829 BT_LAST
831 typedef enum builtin_type builtin_type;
832 enum
834 /* So far we need just these 2 attribute types. */
835 ATTR_NOTHROW_LIST,
836 ATTR_CONST_NOTHROW_LIST
839 tree mfunc_float[3];
840 tree mfunc_double[3];
841 tree mfunc_longdouble[3];
842 tree mfunc_cfloat[3];
843 tree mfunc_cdouble[3];
844 tree mfunc_clongdouble[3];
845 tree func_cfloat_float;
846 tree func_cdouble_double;
847 tree func_clongdouble_longdouble;
848 tree ftype;
849 tree tmp;
850 tree builtin_types[(int) BT_LAST + 1];
852 build_builtin_fntypes (mfunc_float, float_type_node);
853 build_builtin_fntypes (mfunc_double, double_type_node);
854 build_builtin_fntypes (mfunc_longdouble, long_double_type_node);
855 build_builtin_fntypes (mfunc_cfloat, complex_float_type_node);
856 build_builtin_fntypes (mfunc_cdouble, complex_double_type_node);
857 build_builtin_fntypes (mfunc_clongdouble, complex_long_double_type_node);
859 tmp = tree_cons (NULL_TREE, complex_float_type_node, void_list_node);
860 func_cfloat_float = build_function_type (float_type_node, tmp);
862 tmp = tree_cons (NULL_TREE, complex_double_type_node, void_list_node);
863 func_cdouble_double = build_function_type (double_type_node, tmp);
865 tmp = tree_cons (NULL_TREE, complex_long_double_type_node, void_list_node);
866 func_clongdouble_longdouble =
867 build_function_type (long_double_type_node, tmp);
869 #include "mathbuiltins.def"
871 /* We define these separately as the fortran versions have different
872 semantics (they return an integer type) */
873 gfc_define_builtin ("__builtin_roundl", mfunc_longdouble[0],
874 BUILT_IN_ROUNDL, "roundl", true);
875 gfc_define_builtin ("__builtin_round", mfunc_double[0],
876 BUILT_IN_ROUND, "round", true);
877 gfc_define_builtin ("__builtin_roundf", mfunc_float[0],
878 BUILT_IN_ROUNDF, "roundf", true);
880 gfc_define_builtin ("__builtin_truncl", mfunc_longdouble[0],
881 BUILT_IN_TRUNCL, "truncl", true);
882 gfc_define_builtin ("__builtin_trunc", mfunc_double[0],
883 BUILT_IN_TRUNC, "trunc", true);
884 gfc_define_builtin ("__builtin_truncf", mfunc_float[0],
885 BUILT_IN_TRUNCF, "truncf", true);
887 gfc_define_builtin ("__builtin_cabsl", func_clongdouble_longdouble,
888 BUILT_IN_CABSL, "cabsl", true);
889 gfc_define_builtin ("__builtin_cabs", func_cdouble_double,
890 BUILT_IN_CABS, "cabs", true);
891 gfc_define_builtin ("__builtin_cabsf", func_cfloat_float,
892 BUILT_IN_CABSF, "cabsf", true);
894 gfc_define_builtin ("__builtin_copysignl", mfunc_longdouble[1],
895 BUILT_IN_COPYSIGNL, "copysignl", true);
896 gfc_define_builtin ("__builtin_copysign", mfunc_double[1],
897 BUILT_IN_COPYSIGN, "copysign", true);
898 gfc_define_builtin ("__builtin_copysignf", mfunc_float[1],
899 BUILT_IN_COPYSIGNF, "copysignf", true);
901 gfc_define_builtin ("__builtin_fmodl", mfunc_longdouble[1],
902 BUILT_IN_FMODL, "fmodl", true);
903 gfc_define_builtin ("__builtin_fmod", mfunc_double[1],
904 BUILT_IN_FMOD, "fmod", true);
905 gfc_define_builtin ("__builtin_fmodf", mfunc_float[1],
906 BUILT_IN_FMODF, "fmodf", true);
908 /* These are used to implement the ** operator. */
909 gfc_define_builtin ("__builtin_powl", mfunc_longdouble[1],
910 BUILT_IN_POWL, "powl", true);
911 gfc_define_builtin ("__builtin_pow", mfunc_double[1],
912 BUILT_IN_POW, "pow", true);
913 gfc_define_builtin ("__builtin_powf", mfunc_float[1],
914 BUILT_IN_POWF, "powf", true);
916 /* Other builtin functions we use. */
918 tmp = tree_cons (NULL_TREE, long_integer_type_node, void_list_node);
919 tmp = tree_cons (NULL_TREE, long_integer_type_node, tmp);
920 ftype = build_function_type (long_integer_type_node, tmp);
921 gfc_define_builtin ("__builtin_expect", ftype, BUILT_IN_EXPECT,
922 "__builtin_expect", true);
924 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
925 builtin_types[(int) ENUM] = VALUE;
926 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
927 builtin_types[(int) ENUM] \
928 = build_function_type (builtin_types[(int) RETURN], \
929 void_list_node);
930 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
931 builtin_types[(int) ENUM] \
932 = build_function_type (builtin_types[(int) RETURN], \
933 tree_cons (NULL_TREE, \
934 builtin_types[(int) ARG1], \
935 void_list_node));
936 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
937 builtin_types[(int) ENUM] \
938 = build_function_type \
939 (builtin_types[(int) RETURN], \
940 tree_cons (NULL_TREE, \
941 builtin_types[(int) ARG1], \
942 tree_cons (NULL_TREE, \
943 builtin_types[(int) ARG2], \
944 void_list_node)));
945 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
946 builtin_types[(int) ENUM] \
947 = build_function_type \
948 (builtin_types[(int) RETURN], \
949 tree_cons (NULL_TREE, \
950 builtin_types[(int) ARG1], \
951 tree_cons (NULL_TREE, \
952 builtin_types[(int) ARG2], \
953 tree_cons (NULL_TREE, \
954 builtin_types[(int) ARG3], \
955 void_list_node))));
956 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
957 builtin_types[(int) ENUM] \
958 = build_function_type \
959 (builtin_types[(int) RETURN], \
960 tree_cons (NULL_TREE, \
961 builtin_types[(int) ARG1], \
962 tree_cons (NULL_TREE, \
963 builtin_types[(int) ARG2], \
964 tree_cons \
965 (NULL_TREE, \
966 builtin_types[(int) ARG3], \
967 tree_cons (NULL_TREE, \
968 builtin_types[(int) ARG4], \
969 void_list_node)))));
970 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
971 builtin_types[(int) ENUM] \
972 = build_function_type \
973 (builtin_types[(int) RETURN], \
974 tree_cons (NULL_TREE, \
975 builtin_types[(int) ARG1], \
976 tree_cons (NULL_TREE, \
977 builtin_types[(int) ARG2], \
978 tree_cons \
979 (NULL_TREE, \
980 builtin_types[(int) ARG3], \
981 tree_cons (NULL_TREE, \
982 builtin_types[(int) ARG4], \
983 tree_cons (NULL_TREE, \
984 builtin_types[(int) ARG5],\
985 void_list_node))))));
986 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
987 ARG6) \
988 builtin_types[(int) ENUM] \
989 = build_function_type \
990 (builtin_types[(int) RETURN], \
991 tree_cons (NULL_TREE, \
992 builtin_types[(int) ARG1], \
993 tree_cons (NULL_TREE, \
994 builtin_types[(int) ARG2], \
995 tree_cons \
996 (NULL_TREE, \
997 builtin_types[(int) ARG3], \
998 tree_cons \
999 (NULL_TREE, \
1000 builtin_types[(int) ARG4], \
1001 tree_cons (NULL_TREE, \
1002 builtin_types[(int) ARG5], \
1003 tree_cons (NULL_TREE, \
1004 builtin_types[(int) ARG6],\
1005 void_list_node)))))));
1006 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
1007 ARG6, ARG7) \
1008 builtin_types[(int) ENUM] \
1009 = build_function_type \
1010 (builtin_types[(int) RETURN], \
1011 tree_cons (NULL_TREE, \
1012 builtin_types[(int) ARG1], \
1013 tree_cons (NULL_TREE, \
1014 builtin_types[(int) ARG2], \
1015 tree_cons \
1016 (NULL_TREE, \
1017 builtin_types[(int) ARG3], \
1018 tree_cons \
1019 (NULL_TREE, \
1020 builtin_types[(int) ARG4], \
1021 tree_cons (NULL_TREE, \
1022 builtin_types[(int) ARG5], \
1023 tree_cons (NULL_TREE, \
1024 builtin_types[(int) ARG6],\
1025 tree_cons (NULL_TREE, \
1026 builtin_types[(int) ARG6], \
1027 void_list_node))))))));
1028 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
1029 builtin_types[(int) ENUM] \
1030 = build_function_type (builtin_types[(int) RETURN], NULL_TREE);
1031 #define DEF_POINTER_TYPE(ENUM, TYPE) \
1032 builtin_types[(int) ENUM] \
1033 = build_pointer_type (builtin_types[(int) TYPE]);
1034 #include "types.def"
1035 #undef DEF_PRIMITIVE_TYPE
1036 #undef DEF_FUNCTION_TYPE_1
1037 #undef DEF_FUNCTION_TYPE_2
1038 #undef DEF_FUNCTION_TYPE_3
1039 #undef DEF_FUNCTION_TYPE_4
1040 #undef DEF_FUNCTION_TYPE_5
1041 #undef DEF_FUNCTION_TYPE_6
1042 #undef DEF_FUNCTION_TYPE_VAR_0
1043 #undef DEF_POINTER_TYPE
1044 builtin_types[(int) BT_LAST] = NULL_TREE;
1046 /* Initialize synchronization builtins. */
1047 #undef DEF_SYNC_BUILTIN
1048 #define DEF_SYNC_BUILTIN(code, name, type, attr) \
1049 gfc_define_builtin (name, builtin_types[type], code, name, \
1050 attr == ATTR_CONST_NOTHROW_LIST);
1051 #include "../sync-builtins.def"
1052 #undef DEF_SYNC_BUILTIN
1054 if (gfc_option.flag_openmp)
1056 #undef DEF_GOMP_BUILTIN
1057 #define DEF_GOMP_BUILTIN(code, name, type, attr) \
1058 gfc_define_builtin ("__builtin_" name, builtin_types[type], \
1059 code, name, attr == ATTR_CONST_NOTHROW_LIST);
1060 #include "../omp-builtins.def"
1061 #undef DEF_GOMP_BUILTIN
1064 gfc_define_builtin ("__builtin_trap", builtin_types[BT_FN_VOID],
1065 BUILT_IN_TRAP, NULL, false);
1066 TREE_THIS_VOLATILE (built_in_decls[BUILT_IN_TRAP]) = 1;
1068 build_common_builtin_nodes ();
1069 targetm.init_builtins ();
1072 #undef DEFINE_MATH_BUILTIN_C
1073 #undef DEFINE_MATH_BUILTIN
1075 #include "gt-fortran-f95-lang.h"
1076 #include "gtype-fortran.h"