* inclhack.def (AAB_aix_fcntl): New fix.
[official-gcc.git] / gcc / go / go-lang.c
blob8aa056fc32f4daa8762ddd52ba59204735402bfc
1 /* go-lang.c -- Go frontend gcc interface.
2 Copyright (C) 2009, 2010, 2011, 2012 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 "ansidecl.h"
23 #include "coretypes.h"
24 #include "opts.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "ggc.h"
28 #include "toplev.h"
29 #include "debug.h"
30 #include "options.h"
31 #include "flags.h"
32 #include "convert.h"
33 #include "diagnostic.h"
34 #include "langhooks.h"
35 #include "langhooks-def.h"
36 #include "except.h"
37 #include "target.h"
38 #include "common/common-target.h"
40 #include <mpfr.h>
42 #include "go-c.h"
44 /* Language-dependent contents of a type. */
46 struct GTY(()) lang_type
48 char dummy;
51 /* Language-dependent contents of a decl. */
53 struct GTY((variable_size)) lang_decl
55 char dummy;
58 /* Language-dependent contents of an identifier. This must include a
59 tree_identifier. */
61 struct GTY(()) lang_identifier
63 struct tree_identifier common;
66 /* The resulting tree type. */
68 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
69 chain_next ("CODE_CONTAINS_STRUCT (TREE_CODE (&%h.generic), TS_COMMON) ? ((union lang_tree_node *) TREE_CHAIN (&%h.generic)) : NULL")))
70 lang_tree_node
72 union tree_node GTY((tag ("0"),
73 desc ("tree_node_structure (&%h)"))) generic;
74 struct lang_identifier GTY((tag ("1"))) identifier;
77 /* We don't use language_function. */
79 struct GTY(()) language_function
81 int dummy;
84 /* Option information we need to pass to go_create_gogo. */
86 static const char *go_pkgpath = NULL;
87 static const char *go_prefix = NULL;
89 /* Language hooks. */
91 static bool
92 go_langhook_init (void)
94 build_common_tree_nodes (false, false);
96 /* I don't know why this has to be done explicitly. */
97 void_list_node = build_tree_list (NULL_TREE, void_type_node);
99 /* We must create the gogo IR after calling build_common_tree_nodes
100 (because Gogo::define_builtin_function_trees refers indirectly
101 to, e.g., unsigned_char_type_node) but before calling
102 build_common_builtin_nodes (because it calls, indirectly,
103 go_type_for_size). */
104 go_create_gogo (INT_TYPE_SIZE, POINTER_SIZE, go_pkgpath, go_prefix);
106 build_common_builtin_nodes ();
108 /* The default precision for floating point numbers. This is used
109 for floating point constants with abstract type. This may
110 eventually be controllable by a command line option. */
111 mpfr_set_default_prec (256);
113 /* Go uses exceptions. */
114 using_eh_for_cleanups ();
116 return true;
119 /* The option mask. */
121 static unsigned int
122 go_langhook_option_lang_mask (void)
124 return CL_Go;
127 /* Initialize the options structure. */
129 static void
130 go_langhook_init_options_struct (struct gcc_options *opts)
132 /* Go says that signed overflow is precisely defined. */
133 opts->x_flag_wrapv = 1;
135 /* We default to using strict aliasing, since Go pointers are safe.
136 This is turned off for code that imports the "unsafe" package,
137 because using unsafe.pointer violates C style aliasing
138 requirements. */
139 opts->x_flag_strict_aliasing = 1;
141 /* Default to avoiding range issues for complex multiply and
142 divide. */
143 opts->x_flag_complex_method = 2;
145 /* The builtin math functions should not set errno. */
146 opts->x_flag_errno_math = 0;
147 opts->frontend_set_flag_errno_math = true;
149 /* We turn on stack splitting if we can. */
150 if (targetm_common.supports_split_stack (false, opts))
151 opts->x_flag_split_stack = 1;
153 /* Exceptions are used to handle recovering from panics. */
154 opts->x_flag_exceptions = 1;
155 opts->x_flag_non_call_exceptions = 1;
158 /* Infrastructure for a VEC of char * pointers. */
160 typedef const char *go_char_p;
161 DEF_VEC_P(go_char_p);
162 DEF_VEC_ALLOC_P(go_char_p, heap);
164 /* The list of directories to search after all the Go specific
165 directories have been searched. */
167 static VEC(go_char_p, heap) *go_search_dirs;
169 /* Handle Go specific options. Return 0 if we didn't do anything. */
171 static bool
172 go_langhook_handle_option (
173 size_t scode,
174 const char *arg,
175 int value ATTRIBUTE_UNUSED,
176 int kind ATTRIBUTE_UNUSED,
177 location_t loc ATTRIBUTE_UNUSED,
178 const struct cl_option_handlers *handlers ATTRIBUTE_UNUSED)
180 enum opt_code code = (enum opt_code) scode;
181 bool ret = true;
183 switch (code)
185 case OPT_I:
186 go_add_search_path (arg);
187 break;
189 case OPT_L:
190 /* A -L option is assumed to come from the compiler driver.
191 This is a system directory. We search the following
192 directories, if they exist, before this one:
193 dir/go/VERSION
194 dir/go/VERSION/MACHINE
195 This is like include/c++. */
197 static const char dir_separator_str[] = { DIR_SEPARATOR, 0 };
198 size_t len;
199 char *p;
200 struct stat st;
202 len = strlen (arg);
203 p = XALLOCAVEC (char,
204 (len + sizeof "go" + sizeof DEFAULT_TARGET_VERSION
205 + sizeof DEFAULT_TARGET_MACHINE + 3));
206 strcpy (p, arg);
207 if (len > 0 && !IS_DIR_SEPARATOR (p[len - 1]))
208 strcat (p, dir_separator_str);
209 strcat (p, "go");
210 strcat (p, dir_separator_str);
211 strcat (p, DEFAULT_TARGET_VERSION);
212 if (stat (p, &st) == 0 && S_ISDIR (st.st_mode))
214 go_add_search_path (p);
215 strcat (p, dir_separator_str);
216 strcat (p, DEFAULT_TARGET_MACHINE);
217 if (stat (p, &st) == 0 && S_ISDIR (st.st_mode))
218 go_add_search_path (p);
221 /* Search ARG too, but only after we've searched to Go
222 specific directories for all -L arguments. */
223 VEC_safe_push (go_char_p, heap, go_search_dirs, arg);
225 break;
227 case OPT_fgo_dump_:
228 ret = go_enable_dump (arg) ? true : false;
229 break;
231 case OPT_fgo_optimize_:
232 ret = go_enable_optimize (arg) ? true : false;
233 break;
235 case OPT_fgo_pkgpath_:
236 go_pkgpath = arg;
237 break;
239 case OPT_fgo_prefix_:
240 go_prefix = arg;
241 break;
243 default:
244 /* Just return 1 to indicate that the option is valid. */
245 break;
248 return ret;
251 /* Run after parsing options. */
253 static bool
254 go_langhook_post_options (const char **pfilename ATTRIBUTE_UNUSED)
256 unsigned int ix;
257 const char *dir;
259 gcc_assert (num_in_fnames > 0);
261 FOR_EACH_VEC_ELT (go_char_p, go_search_dirs, ix, dir)
262 go_add_search_path (dir);
263 VEC_free (go_char_p, heap, go_search_dirs);
264 go_search_dirs = NULL;
266 if (flag_excess_precision_cmdline == EXCESS_PRECISION_DEFAULT)
267 flag_excess_precision_cmdline = EXCESS_PRECISION_STANDARD;
269 /* Returning false means that the backend should be used. */
270 return false;
273 static void
274 go_langhook_parse_file (void)
276 go_parse_input_files (in_fnames, num_in_fnames, flag_syntax_only,
277 go_require_return_statement);
280 static tree
281 go_langhook_type_for_size (unsigned int bits, int unsignedp)
283 return go_type_for_size (bits, unsignedp);
286 static tree
287 go_langhook_type_for_mode (enum machine_mode mode, int unsignedp)
289 tree type;
290 /* Go has no vector types. Build them here. FIXME: It does not
291 make sense for the middle-end to ask the frontend for a type
292 which the frontend does not support. However, at least for now
293 it is required. See PR 46805. */
294 if (VECTOR_MODE_P (mode))
296 tree inner;
298 inner = go_langhook_type_for_mode (GET_MODE_INNER (mode), unsignedp);
299 if (inner != NULL_TREE)
300 return build_vector_type_for_mode (inner, mode);
301 return NULL_TREE;
304 type = go_type_for_mode (mode, unsignedp);
305 if (type)
306 return type;
308 #if HOST_BITS_PER_WIDE_INT >= 64
309 /* The middle-end and some backends rely on TImode being supported
310 for 64-bit HWI. */
311 if (mode == TImode)
313 type = build_nonstandard_integer_type (GET_MODE_BITSIZE (TImode),
314 unsignedp);
315 if (type && TYPE_MODE (type) == TImode)
316 return type;
318 #endif
319 return NULL_TREE;
322 /* Record a builtin function. We just ignore builtin functions. */
324 static tree
325 go_langhook_builtin_function (tree decl)
327 return decl;
330 /* Return true if we are in the global binding level. */
332 static bool
333 go_langhook_global_bindings_p (void)
335 return current_function_decl == NULL_TREE;
338 /* Push a declaration into the current binding level. We can't
339 usefully implement this since we don't want to convert from tree
340 back to one of our internal data structures. I think the only way
341 this is used is to record a decl which is to be returned by
342 getdecls, and we could implement it for that purpose if
343 necessary. */
345 static tree
346 go_langhook_pushdecl (tree decl ATTRIBUTE_UNUSED)
348 gcc_unreachable ();
351 /* This hook is used to get the current list of declarations as trees.
352 We don't support that; instead we use the write_globals hook. This
353 can't simply crash because it is called by -gstabs. */
355 static tree
356 go_langhook_getdecls (void)
358 return NULL;
361 /* Write out globals. */
363 static void
364 go_langhook_write_globals (void)
366 go_write_globals ();
369 /* Go specific gimplification. We need to gimplify
370 CALL_EXPR_STATIC_CHAIN, because the gimplifier doesn't handle
371 it. */
373 static int
374 go_langhook_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
376 if (TREE_CODE (*expr_p) == CALL_EXPR
377 && CALL_EXPR_STATIC_CHAIN (*expr_p) != NULL_TREE)
378 gimplify_expr (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p, post_p,
379 is_gimple_val, fb_rvalue);
380 return GS_UNHANDLED;
383 /* Return a decl for the exception personality function. The function
384 itself is implemented in libgo/runtime/go-unwind.c. */
386 static tree
387 go_langhook_eh_personality (void)
389 static tree personality_decl;
390 if (personality_decl == NULL_TREE)
392 personality_decl = build_personality_function ("gccgo");
393 go_preserve_from_gc (personality_decl);
395 return personality_decl;
398 /* Functions called directly by the generic backend. */
400 tree
401 convert (tree type, tree expr)
403 if (type == error_mark_node
404 || expr == error_mark_node
405 || TREE_TYPE (expr) == error_mark_node)
406 return error_mark_node;
408 if (type == TREE_TYPE (expr))
409 return expr;
411 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (expr)))
412 return fold_convert (type, expr);
414 switch (TREE_CODE (type))
416 case VOID_TYPE:
417 case BOOLEAN_TYPE:
418 return fold_convert (type, expr);
419 case INTEGER_TYPE:
420 return fold (convert_to_integer (type, expr));
421 case POINTER_TYPE:
422 return fold (convert_to_pointer (type, expr));
423 case REAL_TYPE:
424 return fold (convert_to_real (type, expr));
425 case COMPLEX_TYPE:
426 return fold (convert_to_complex (type, expr));
427 default:
428 break;
431 gcc_unreachable ();
434 /* FIXME: This is a hack to preserve trees that we create from the
435 garbage collector. */
437 static GTY(()) tree go_gc_root;
439 void
440 go_preserve_from_gc (tree t)
442 go_gc_root = tree_cons (NULL_TREE, t, go_gc_root);
445 /* Convert an identifier for use in an error message. */
447 const char *
448 go_localize_identifier (const char *ident)
450 return identifier_to_locale (ident);
453 #undef LANG_HOOKS_NAME
454 #undef LANG_HOOKS_INIT
455 #undef LANG_HOOKS_OPTION_LANG_MASK
456 #undef LANG_HOOKS_INIT_OPTIONS_STRUCT
457 #undef LANG_HOOKS_HANDLE_OPTION
458 #undef LANG_HOOKS_POST_OPTIONS
459 #undef LANG_HOOKS_PARSE_FILE
460 #undef LANG_HOOKS_TYPE_FOR_MODE
461 #undef LANG_HOOKS_TYPE_FOR_SIZE
462 #undef LANG_HOOKS_BUILTIN_FUNCTION
463 #undef LANG_HOOKS_GLOBAL_BINDINGS_P
464 #undef LANG_HOOKS_PUSHDECL
465 #undef LANG_HOOKS_GETDECLS
466 #undef LANG_HOOKS_WRITE_GLOBALS
467 #undef LANG_HOOKS_GIMPLIFY_EXPR
468 #undef LANG_HOOKS_EH_PERSONALITY
470 #define LANG_HOOKS_NAME "GNU Go"
471 #define LANG_HOOKS_INIT go_langhook_init
472 #define LANG_HOOKS_OPTION_LANG_MASK go_langhook_option_lang_mask
473 #define LANG_HOOKS_INIT_OPTIONS_STRUCT go_langhook_init_options_struct
474 #define LANG_HOOKS_HANDLE_OPTION go_langhook_handle_option
475 #define LANG_HOOKS_POST_OPTIONS go_langhook_post_options
476 #define LANG_HOOKS_PARSE_FILE go_langhook_parse_file
477 #define LANG_HOOKS_TYPE_FOR_MODE go_langhook_type_for_mode
478 #define LANG_HOOKS_TYPE_FOR_SIZE go_langhook_type_for_size
479 #define LANG_HOOKS_BUILTIN_FUNCTION go_langhook_builtin_function
480 #define LANG_HOOKS_GLOBAL_BINDINGS_P go_langhook_global_bindings_p
481 #define LANG_HOOKS_PUSHDECL go_langhook_pushdecl
482 #define LANG_HOOKS_GETDECLS go_langhook_getdecls
483 #define LANG_HOOKS_WRITE_GLOBALS go_langhook_write_globals
484 #define LANG_HOOKS_GIMPLIFY_EXPR go_langhook_gimplify_expr
485 #define LANG_HOOKS_EH_PERSONALITY go_langhook_eh_personality
487 struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
489 #include "gt-go-go-lang.h"
490 #include "gtype-go.h"