[Ada] Wrong accessibility level under -gnat12
[official-gcc.git] / gcc / go / go-lang.c
blob94f2cb25df3fdd306749add65fa4b3413d6ccb83
1 /* go-lang.c -- Go frontend gcc interface.
2 Copyright (C) 2009-2019 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 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "target.h"
24 #include "tree.h"
25 #include "gimple-expr.h"
26 #include "diagnostic.h"
27 #include "opts.h"
28 #include "fold-const.h"
29 #include "gimplify.h"
30 #include "stor-layout.h"
31 #include "debug.h"
32 #include "convert.h"
33 #include "langhooks.h"
34 #include "langhooks-def.h"
35 #include "common/common-target.h"
37 #include <mpfr.h>
39 #include "go-c.h"
40 #include "go-gcc.h"
42 #ifndef TARGET_AIX
43 #define TARGET_AIX 0
44 #endif
46 /* Language-dependent contents of a type. */
48 struct GTY(()) lang_type
50 char dummy;
53 /* Language-dependent contents of a decl. */
55 struct GTY(()) lang_decl
57 char dummy;
60 /* Language-dependent contents of an identifier. This must include a
61 tree_identifier. */
63 struct GTY(()) lang_identifier
65 struct tree_identifier common;
68 /* The resulting tree type. */
70 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
71 chain_next ("CODE_CONTAINS_STRUCT (TREE_CODE (&%h.generic), TS_COMMON) ? ((union lang_tree_node *) TREE_CHAIN (&%h.generic)) : NULL")))
72 lang_tree_node
74 union tree_node GTY((tag ("0"),
75 desc ("tree_node_structure (&%h)"))) generic;
76 struct lang_identifier GTY((tag ("1"))) identifier;
79 /* We don't use language_function. */
81 struct GTY(()) language_function
83 int dummy;
86 /* Option information we need to pass to go_create_gogo. */
88 static const char *go_pkgpath = NULL;
89 static const char *go_prefix = NULL;
90 static const char *go_relative_import_path = NULL;
91 static const char *go_c_header = NULL;
93 /* Language hooks. */
95 static bool
96 go_langhook_init (void)
98 build_common_tree_nodes (false);
100 /* I don't know why this has to be done explicitly. */
101 void_list_node = build_tree_list (NULL_TREE, void_type_node);
103 /* We must create the gogo IR after calling build_common_tree_nodes
104 (because Gogo::define_builtin_function_trees refers indirectly
105 to, e.g., unsigned_char_type_node) but before calling
106 build_common_builtin_nodes (because it calls, indirectly,
107 go_type_for_size). */
108 struct go_create_gogo_args args;
109 args.int_type_size = INT_TYPE_SIZE;
110 args.pointer_size = POINTER_SIZE;
111 args.pkgpath = go_pkgpath;
112 args.prefix = go_prefix;
113 args.relative_import_path = go_relative_import_path;
114 args.c_header = go_c_header;
115 args.check_divide_by_zero = go_check_divide_zero;
116 args.check_divide_overflow = go_check_divide_overflow;
117 args.compiling_runtime = go_compiling_runtime;
118 args.debug_escape_level = go_debug_escape_level;
119 args.debug_escape_hash = go_debug_escape_hash;
120 args.nil_check_size_threshold = TARGET_AIX ? -1 : 4096;
121 args.debug_optimization = go_debug_optimization;
122 args.linemap = go_get_linemap();
123 args.backend = go_get_backend();
124 go_create_gogo (&args);
126 build_common_builtin_nodes ();
128 /* The default precision for floating point numbers. This is used
129 for floating point constants with abstract type. This may
130 eventually be controllable by a command line option. */
131 mpfr_set_default_prec (256);
133 /* Go uses exceptions. */
134 using_eh_for_cleanups ();
136 return true;
139 /* The option mask. */
141 static unsigned int
142 go_langhook_option_lang_mask (void)
144 return CL_Go;
147 /* Initialize the options structure. */
149 static void
150 go_langhook_init_options_struct (struct gcc_options *opts)
152 /* Go says that signed overflow is precisely defined. */
153 opts->x_flag_wrapv = 1;
155 /* We default to using strict aliasing, since Go pointers are safe.
156 This is turned off for code that imports the "unsafe" package,
157 because using unsafe.pointer violates C style aliasing
158 requirements. */
159 opts->x_flag_strict_aliasing = 1;
161 /* Default to avoiding range issues for complex multiply and
162 divide. */
163 opts->x_flag_complex_method = 2;
165 /* The builtin math functions should not set errno. */
166 opts->x_flag_errno_math = 0;
167 opts->frontend_set_flag_errno_math = true;
169 /* Exceptions are used to handle recovering from panics. */
170 opts->x_flag_exceptions = 1;
171 opts->x_flag_non_call_exceptions = 1;
173 /* We need to keep pointers live for the garbage collector. */
174 opts->x_flag_keep_gc_roots_live = 1;
176 /* Go programs expect runtime.Callers to work, and that uses
177 libbacktrace that uses debug info. Set the debug info level to 1
178 by default. In post_options we will set the debug type if the
179 debug info level was not set back to 0 on the command line. */
180 opts->x_debug_info_level = DINFO_LEVEL_TERSE;
183 /* Infrastructure for a vector of char * pointers. */
185 typedef const char *go_char_p;
187 /* The list of directories to search after all the Go specific
188 directories have been searched. */
190 static vec<go_char_p> go_search_dirs;
192 /* Handle Go specific options. Return 0 if we didn't do anything. */
194 static bool
195 go_langhook_handle_option (
196 size_t scode,
197 const char *arg,
198 HOST_WIDE_INT value,
199 int kind ATTRIBUTE_UNUSED,
200 location_t loc ATTRIBUTE_UNUSED,
201 const struct cl_option_handlers *handlers ATTRIBUTE_UNUSED)
203 enum opt_code code = (enum opt_code) scode;
204 bool ret = true;
206 switch (code)
208 case OPT_I:
209 go_add_search_path (arg);
210 break;
212 case OPT_L:
213 /* A -L option is assumed to come from the compiler driver.
214 This is a system directory. We search the following
215 directories, if they exist, before this one:
216 dir/go/VERSION
217 dir/go/VERSION/MACHINE
218 This is like include/c++. */
220 static const char dir_separator_str[] = { DIR_SEPARATOR, 0 };
221 size_t len;
222 char *p;
223 struct stat st;
225 len = strlen (arg);
226 p = XALLOCAVEC (char,
227 (len + sizeof "go" + sizeof DEFAULT_TARGET_VERSION
228 + sizeof DEFAULT_TARGET_MACHINE + 3));
229 strcpy (p, arg);
230 if (len > 0 && !IS_DIR_SEPARATOR (p[len - 1]))
231 strcat (p, dir_separator_str);
232 strcat (p, "go");
233 strcat (p, dir_separator_str);
234 strcat (p, DEFAULT_TARGET_VERSION);
235 if (stat (p, &st) == 0 && S_ISDIR (st.st_mode))
237 go_add_search_path (p);
238 strcat (p, dir_separator_str);
239 strcat (p, DEFAULT_TARGET_MACHINE);
240 if (stat (p, &st) == 0 && S_ISDIR (st.st_mode))
241 go_add_search_path (p);
244 /* Search ARG too, but only after we've searched to Go
245 specific directories for all -L arguments. */
246 go_search_dirs.safe_push (arg);
248 break;
250 case OPT_fgo_dump_:
251 ret = go_enable_dump (arg) ? true : false;
252 break;
254 case OPT_fgo_optimize_:
255 ret = go_enable_optimize (arg, value) ? true : false;
256 break;
258 case OPT_fgo_pkgpath_:
259 go_pkgpath = arg;
260 break;
262 case OPT_fgo_prefix_:
263 go_prefix = arg;
264 break;
266 case OPT_fgo_relative_import_path_:
267 go_relative_import_path = arg;
268 break;
270 case OPT_fgo_c_header_:
271 go_c_header = arg;
272 break;
274 default:
275 /* Just return 1 to indicate that the option is valid. */
276 break;
279 return ret;
282 /* Run after parsing options. */
284 static bool
285 go_langhook_post_options (const char **pfilename ATTRIBUTE_UNUSED)
287 unsigned int ix;
288 const char *dir;
290 gcc_assert (num_in_fnames > 0);
292 FOR_EACH_VEC_ELT (go_search_dirs, ix, dir)
293 go_add_search_path (dir);
294 go_search_dirs.release ();
296 if (flag_excess_precision_cmdline == EXCESS_PRECISION_DEFAULT)
297 flag_excess_precision_cmdline = EXCESS_PRECISION_STANDARD;
299 /* Tail call optimizations can confuse uses of runtime.Callers. */
300 if (!global_options_set.x_flag_optimize_sibling_calls)
301 global_options.x_flag_optimize_sibling_calls = 0;
303 /* If the debug info level is still 1, as set in init_options, make
304 sure that some debugging type is selected. */
305 if (global_options.x_debug_info_level == DINFO_LEVEL_TERSE
306 && global_options.x_write_symbols == NO_DEBUG)
307 global_options.x_write_symbols = PREFERRED_DEBUGGING_TYPE;
309 /* We turn on stack splitting if we can. */
310 if (!global_options_set.x_flag_split_stack
311 && targetm_common.supports_split_stack (false, &global_options))
312 global_options.x_flag_split_stack = 1;
314 /* If stack splitting is turned on, and the user did not explicitly
315 request function partitioning, turn off partitioning, as it
316 confuses the linker when trying to handle partitioned split-stack
317 code that calls a non-split-stack function. */
318 if (global_options.x_flag_split_stack
319 && global_options.x_flag_reorder_blocks_and_partition
320 && !global_options_set.x_flag_reorder_blocks_and_partition)
321 global_options.x_flag_reorder_blocks_and_partition = 0;
323 /* Returning false means that the backend should be used. */
324 return false;
327 static void
328 go_langhook_parse_file (void)
330 go_parse_input_files (in_fnames, num_in_fnames, flag_syntax_only,
331 go_require_return_statement);
333 /* Final processing of globals and early debug info generation. */
334 go_write_globals ();
337 static tree
338 go_langhook_type_for_size (unsigned int bits, int unsignedp)
340 tree type;
341 if (unsignedp)
343 if (bits == INT_TYPE_SIZE)
344 type = unsigned_type_node;
345 else if (bits == CHAR_TYPE_SIZE)
346 type = unsigned_char_type_node;
347 else if (bits == SHORT_TYPE_SIZE)
348 type = short_unsigned_type_node;
349 else if (bits == LONG_TYPE_SIZE)
350 type = long_unsigned_type_node;
351 else if (bits == LONG_LONG_TYPE_SIZE)
352 type = long_long_unsigned_type_node;
353 else
354 type = make_unsigned_type(bits);
356 else
358 if (bits == INT_TYPE_SIZE)
359 type = integer_type_node;
360 else if (bits == CHAR_TYPE_SIZE)
361 type = signed_char_type_node;
362 else if (bits == SHORT_TYPE_SIZE)
363 type = short_integer_type_node;
364 else if (bits == LONG_TYPE_SIZE)
365 type = long_integer_type_node;
366 else if (bits == LONG_LONG_TYPE_SIZE)
367 type = long_long_integer_type_node;
368 else
369 type = make_signed_type(bits);
371 return type;
374 static tree
375 go_langhook_type_for_mode (machine_mode mode, int unsignedp)
377 tree type;
378 /* Go has no vector types. Build them here. FIXME: It does not
379 make sense for the middle-end to ask the frontend for a type
380 which the frontend does not support. However, at least for now
381 it is required. See PR 46805. */
382 if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL
383 && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
385 unsigned int elem_bits = vector_element_size (GET_MODE_BITSIZE (mode),
386 GET_MODE_NUNITS (mode));
387 tree bool_type = build_nonstandard_boolean_type (elem_bits);
388 return build_vector_type_for_mode (bool_type, mode);
390 else if (VECTOR_MODE_P (mode)
391 && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
393 tree inner;
395 inner = go_langhook_type_for_mode (GET_MODE_INNER (mode), unsignedp);
396 if (inner != NULL_TREE)
397 return build_vector_type_for_mode (inner, mode);
398 return NULL_TREE;
401 scalar_int_mode imode;
402 scalar_float_mode fmode;
403 complex_mode cmode;
404 if (is_int_mode (mode, &imode))
405 return go_langhook_type_for_size (GET_MODE_BITSIZE (imode), unsignedp);
406 else if (is_float_mode (mode, &fmode))
408 switch (GET_MODE_BITSIZE (fmode))
410 case 32:
411 return float_type_node;
412 case 64:
413 return double_type_node;
414 default:
415 // We have to check for long double in order to support
416 // i386 excess precision.
417 if (fmode == TYPE_MODE(long_double_type_node))
418 return long_double_type_node;
421 else if (is_complex_float_mode (mode, &cmode))
423 switch (GET_MODE_BITSIZE (cmode))
425 case 64:
426 return complex_float_type_node;
427 case 128:
428 return complex_double_type_node;
429 default:
430 // We have to check for long double in order to support
431 // i386 excess precision.
432 if (cmode == TYPE_MODE(complex_long_double_type_node))
433 return complex_long_double_type_node;
437 #if HOST_BITS_PER_WIDE_INT >= 64
438 /* The middle-end and some backends rely on TImode being supported
439 for 64-bit HWI. */
440 if (mode == TImode)
442 type = build_nonstandard_integer_type (GET_MODE_BITSIZE (TImode),
443 unsignedp);
444 if (type && TYPE_MODE (type) == TImode)
445 return type;
447 #endif
448 return NULL_TREE;
451 /* Record a builtin function. We just ignore builtin functions. */
453 static tree
454 go_langhook_builtin_function (tree decl)
456 return decl;
459 /* Return true if we are in the global binding level. */
461 static bool
462 go_langhook_global_bindings_p (void)
464 return current_function_decl == NULL_TREE;
467 /* Push a declaration into the current binding level. We can't
468 usefully implement this since we don't want to convert from tree
469 back to one of our internal data structures. I think the only way
470 this is used is to record a decl which is to be returned by
471 getdecls, and we could implement it for that purpose if
472 necessary. */
474 static tree
475 go_langhook_pushdecl (tree decl ATTRIBUTE_UNUSED)
477 gcc_unreachable ();
480 /* This hook is used to get the current list of declarations as trees.
481 We don't support that; instead we use the write_globals hook. This
482 can't simply crash because it is called by -gstabs. */
484 static tree
485 go_langhook_getdecls (void)
487 return NULL;
490 /* Go specific gimplification. We need to gimplify
491 CALL_EXPR_STATIC_CHAIN, because the gimplifier doesn't handle
492 it. */
494 static int
495 go_langhook_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
497 if (TREE_CODE (*expr_p) == CALL_EXPR
498 && CALL_EXPR_STATIC_CHAIN (*expr_p) != NULL_TREE)
499 gimplify_expr (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p, post_p,
500 is_gimple_val, fb_rvalue);
501 return GS_UNHANDLED;
504 /* Return a decl for the exception personality function. The function
505 itself is implemented in libgo/runtime/go-unwind.c. */
507 static tree
508 go_langhook_eh_personality (void)
510 static tree personality_decl;
511 if (personality_decl == NULL_TREE)
513 personality_decl = build_personality_function ("gccgo");
514 go_preserve_from_gc (personality_decl);
516 return personality_decl;
519 /* Functions called directly by the generic backend. */
521 tree
522 convert (tree type, tree expr)
524 if (type == error_mark_node
525 || expr == error_mark_node
526 || TREE_TYPE (expr) == error_mark_node)
527 return error_mark_node;
529 if (type == TREE_TYPE (expr))
530 return expr;
532 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (expr)))
533 return fold_convert (type, expr);
535 switch (TREE_CODE (type))
537 case VOID_TYPE:
538 case BOOLEAN_TYPE:
539 return fold_convert (type, expr);
540 case INTEGER_TYPE:
541 return fold (convert_to_integer (type, expr));
542 case POINTER_TYPE:
543 return fold (convert_to_pointer (type, expr));
544 case REAL_TYPE:
545 return fold (convert_to_real (type, expr));
546 case COMPLEX_TYPE:
547 return fold (convert_to_complex (type, expr));
548 default:
549 break;
552 gcc_unreachable ();
555 /* FIXME: This is a hack to preserve trees that we create from the
556 garbage collector. */
558 static GTY(()) tree go_gc_root;
560 void
561 go_preserve_from_gc (tree t)
563 go_gc_root = tree_cons (NULL_TREE, t, go_gc_root);
566 /* Convert an identifier for use in an error message. */
568 const char *
569 go_localize_identifier (const char *ident)
571 return identifier_to_locale (ident);
574 #undef LANG_HOOKS_NAME
575 #undef LANG_HOOKS_INIT
576 #undef LANG_HOOKS_OPTION_LANG_MASK
577 #undef LANG_HOOKS_INIT_OPTIONS_STRUCT
578 #undef LANG_HOOKS_HANDLE_OPTION
579 #undef LANG_HOOKS_POST_OPTIONS
580 #undef LANG_HOOKS_PARSE_FILE
581 #undef LANG_HOOKS_TYPE_FOR_MODE
582 #undef LANG_HOOKS_TYPE_FOR_SIZE
583 #undef LANG_HOOKS_BUILTIN_FUNCTION
584 #undef LANG_HOOKS_GLOBAL_BINDINGS_P
585 #undef LANG_HOOKS_PUSHDECL
586 #undef LANG_HOOKS_GETDECLS
587 #undef LANG_HOOKS_GIMPLIFY_EXPR
588 #undef LANG_HOOKS_EH_PERSONALITY
590 #define LANG_HOOKS_NAME "GNU Go"
591 #define LANG_HOOKS_INIT go_langhook_init
592 #define LANG_HOOKS_OPTION_LANG_MASK go_langhook_option_lang_mask
593 #define LANG_HOOKS_INIT_OPTIONS_STRUCT go_langhook_init_options_struct
594 #define LANG_HOOKS_HANDLE_OPTION go_langhook_handle_option
595 #define LANG_HOOKS_POST_OPTIONS go_langhook_post_options
596 #define LANG_HOOKS_PARSE_FILE go_langhook_parse_file
597 #define LANG_HOOKS_TYPE_FOR_MODE go_langhook_type_for_mode
598 #define LANG_HOOKS_TYPE_FOR_SIZE go_langhook_type_for_size
599 #define LANG_HOOKS_BUILTIN_FUNCTION go_langhook_builtin_function
600 #define LANG_HOOKS_GLOBAL_BINDINGS_P go_langhook_global_bindings_p
601 #define LANG_HOOKS_PUSHDECL go_langhook_pushdecl
602 #define LANG_HOOKS_GETDECLS go_langhook_getdecls
603 #define LANG_HOOKS_GIMPLIFY_EXPR go_langhook_gimplify_expr
604 #define LANG_HOOKS_EH_PERSONALITY go_langhook_eh_personality
606 struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
608 #include "gt-go-go-lang.h"
609 #include "gtype-go.h"