]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/go/go-lang.c
compiler, reflect, runtime: Implement method values in reflect.
[thirdparty/gcc.git] / gcc / go / go-lang.c
CommitLineData
7a938933 1/* go-lang.c -- Go frontend gcc interface.
d1e082c2 2 Copyright (C) 2009-2013 Free Software Foundation, Inc.
7a938933
ILT
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
19
20#include "config.h"
21#include "system.h"
22#include "ansidecl.h"
23#include "coretypes.h"
24#include "opts.h"
25#include "tree.h"
2fb9a547
AM
26#include "basic-block.h"
27#include "gimple-expr.h"
45b0be94 28#include "gimplify.h"
d8a2d370 29#include "stor-layout.h"
7a938933
ILT
30#include "toplev.h"
31#include "debug.h"
32#include "options.h"
33#include "flags.h"
34#include "convert.h"
35#include "diagnostic.h"
36#include "langhooks.h"
37#include "langhooks-def.h"
7a938933 38#include "target.h"
677f3fa8 39#include "common/common-target.h"
7a938933
ILT
40
41#include <mpfr.h>
42
43#include "go-c.h"
44
45/* Language-dependent contents of a type. */
46
47struct GTY(()) lang_type
48{
49 char dummy;
50};
51
52/* Language-dependent contents of a decl. */
53
0229aee9 54struct GTY((variable_size)) lang_decl
7a938933
ILT
55{
56 char dummy;
57};
58
59/* Language-dependent contents of an identifier. This must include a
60 tree_identifier. */
61
62struct GTY(()) lang_identifier
63{
64 struct tree_identifier common;
65};
66
67/* The resulting tree type. */
68
69union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
81f653d6 70 chain_next ("CODE_CONTAINS_STRUCT (TREE_CODE (&%h.generic), TS_COMMON) ? ((union lang_tree_node *) TREE_CHAIN (&%h.generic)) : NULL")))
7a938933
ILT
71lang_tree_node
72{
73 union tree_node GTY((tag ("0"),
74 desc ("tree_node_structure (&%h)"))) generic;
75 struct lang_identifier GTY((tag ("1"))) identifier;
76};
77
78/* We don't use language_function. */
79
80struct GTY(()) language_function
81{
82 int dummy;
83};
84
097b12fb
ILT
85/* Option information we need to pass to go_create_gogo. */
86
87static const char *go_pkgpath = NULL;
88static const char *go_prefix = NULL;
4e1866fe 89static const char *go_relative_import_path = NULL;
097b12fb 90
7a938933
ILT
91/* Language hooks. */
92
93static bool
94go_langhook_init (void)
95{
1a072294 96 build_common_tree_nodes (false, false);
7a938933 97
00733a00
ILT
98 /* I don't know why this has to be done explicitly. */
99 void_list_node = build_tree_list (NULL_TREE, void_type_node);
100
7a938933
ILT
101 /* We must create the gogo IR after calling build_common_tree_nodes
102 (because Gogo::define_builtin_function_trees refers indirectly
103 to, e.g., unsigned_char_type_node) but before calling
104 build_common_builtin_nodes (because it calls, indirectly,
105 go_type_for_size). */
4e1866fe
ILT
106 go_create_gogo (INT_TYPE_SIZE, POINTER_SIZE, go_pkgpath, go_prefix,
107 go_relative_import_path);
7a938933
ILT
108
109 build_common_builtin_nodes ();
110
7a938933
ILT
111 /* The default precision for floating point numbers. This is used
112 for floating point constants with abstract type. This may
113 eventually be controllable by a command line option. */
1ec20ea1 114 mpfr_set_default_prec (256);
7a938933
ILT
115
116 /* Go uses exceptions. */
117 using_eh_for_cleanups ();
118
119 return true;
120}
121
122/* The option mask. */
123
124static unsigned int
125go_langhook_option_lang_mask (void)
126{
127 return CL_Go;
128}
129
130/* Initialize the options structure. */
131
132static void
133go_langhook_init_options_struct (struct gcc_options *opts)
134{
135 /* Go says that signed overflow is precisely defined. */
136 opts->x_flag_wrapv = 1;
137
138 /* We default to using strict aliasing, since Go pointers are safe.
139 This is turned off for code that imports the "unsafe" package,
140 because using unsafe.pointer violates C style aliasing
141 requirements. */
142 opts->x_flag_strict_aliasing = 1;
143
144 /* Default to avoiding range issues for complex multiply and
145 divide. */
146 opts->x_flag_complex_method = 2;
147
148 /* The builtin math functions should not set errno. */
149 opts->x_flag_errno_math = 0;
9b548472 150 opts->frontend_set_flag_errno_math = true;
7a938933
ILT
151
152 /* We turn on stack splitting if we can. */
677f3fa8 153 if (targetm_common.supports_split_stack (false, opts))
7a938933
ILT
154 opts->x_flag_split_stack = 1;
155
156 /* Exceptions are used to handle recovering from panics. */
157 opts->x_flag_exceptions = 1;
158 opts->x_flag_non_call_exceptions = 1;
159}
160
9771b263 161/* Infrastructure for a vector of char * pointers. */
ac819ba5
ILT
162
163typedef const char *go_char_p;
ac819ba5
ILT
164
165/* The list of directories to search after all the Go specific
166 directories have been searched. */
167
9771b263 168static vec<go_char_p> go_search_dirs;
ac819ba5 169
7a938933
ILT
170/* Handle Go specific options. Return 0 if we didn't do anything. */
171
172static bool
173go_langhook_handle_option (
174 size_t scode,
175 const char *arg,
176 int value ATTRIBUTE_UNUSED,
177 int kind ATTRIBUTE_UNUSED,
178 location_t loc ATTRIBUTE_UNUSED,
179 const struct cl_option_handlers *handlers ATTRIBUTE_UNUSED)
180{
181 enum opt_code code = (enum opt_code) scode;
182 bool ret = true;
183
184 switch (code)
185 {
186 case OPT_I:
7a938933
ILT
187 go_add_search_path (arg);
188 break;
189
ac819ba5
ILT
190 case OPT_L:
191 /* A -L option is assumed to come from the compiler driver.
192 This is a system directory. We search the following
193 directories, if they exist, before this one:
194 dir/go/VERSION
195 dir/go/VERSION/MACHINE
196 This is like include/c++. */
197 {
198 static const char dir_separator_str[] = { DIR_SEPARATOR, 0 };
199 size_t len;
200 char *p;
201 struct stat st;
202
203 len = strlen (arg);
204 p = XALLOCAVEC (char,
205 (len + sizeof "go" + sizeof DEFAULT_TARGET_VERSION
206 + sizeof DEFAULT_TARGET_MACHINE + 3));
207 strcpy (p, arg);
208 if (len > 0 && !IS_DIR_SEPARATOR (p[len - 1]))
209 strcat (p, dir_separator_str);
210 strcat (p, "go");
211 strcat (p, dir_separator_str);
212 strcat (p, DEFAULT_TARGET_VERSION);
213 if (stat (p, &st) == 0 && S_ISDIR (st.st_mode))
214 {
215 go_add_search_path (p);
216 strcat (p, dir_separator_str);
217 strcat (p, DEFAULT_TARGET_MACHINE);
218 if (stat (p, &st) == 0 && S_ISDIR (st.st_mode))
219 go_add_search_path (p);
220 }
221
222 /* Search ARG too, but only after we've searched to Go
223 specific directories for all -L arguments. */
9771b263 224 go_search_dirs.safe_push (arg);
ac819ba5
ILT
225 }
226 break;
227
7a938933
ILT
228 case OPT_fgo_dump_:
229 ret = go_enable_dump (arg) ? true : false;
230 break;
231
706cd57f
RL
232 case OPT_fgo_optimize_:
233 ret = go_enable_optimize (arg) ? true : false;
234 break;
235
097b12fb
ILT
236 case OPT_fgo_pkgpath_:
237 go_pkgpath = arg;
238 break;
239
7a938933 240 case OPT_fgo_prefix_:
097b12fb 241 go_prefix = arg;
7a938933
ILT
242 break;
243
4e1866fe
ILT
244 case OPT_fgo_relative_import_path_:
245 go_relative_import_path = arg;
246 break;
247
7a938933
ILT
248 default:
249 /* Just return 1 to indicate that the option is valid. */
250 break;
251 }
252
253 return ret;
254}
255
256/* Run after parsing options. */
257
258static bool
259go_langhook_post_options (const char **pfilename ATTRIBUTE_UNUSED)
260{
ac819ba5
ILT
261 unsigned int ix;
262 const char *dir;
263
7a938933
ILT
264 gcc_assert (num_in_fnames > 0);
265
9771b263 266 FOR_EACH_VEC_ELT (go_search_dirs, ix, dir)
ac819ba5 267 go_add_search_path (dir);
9771b263 268 go_search_dirs.release ();
ac819ba5 269
7a938933
ILT
270 if (flag_excess_precision_cmdline == EXCESS_PRECISION_DEFAULT)
271 flag_excess_precision_cmdline = EXCESS_PRECISION_STANDARD;
272
273 /* Returning false means that the backend should be used. */
274 return false;
275}
276
277static void
278go_langhook_parse_file (void)
279{
280 go_parse_input_files (in_fnames, num_in_fnames, flag_syntax_only,
281 go_require_return_statement);
282}
283
284static tree
285go_langhook_type_for_size (unsigned int bits, int unsignedp)
286{
287 return go_type_for_size (bits, unsignedp);
288}
289
290static tree
291go_langhook_type_for_mode (enum machine_mode mode, int unsignedp)
292{
c2b13bb6 293 tree type;
0e29f7e5
ILT
294 /* Go has no vector types. Build them here. FIXME: It does not
295 make sense for the middle-end to ask the frontend for a type
296 which the frontend does not support. However, at least for now
297 it is required. See PR 46805. */
298 if (VECTOR_MODE_P (mode))
299 {
300 tree inner;
301
302 inner = go_langhook_type_for_mode (GET_MODE_INNER (mode), unsignedp);
303 if (inner != NULL_TREE)
304 return build_vector_type_for_mode (inner, mode);
305 return NULL_TREE;
306 }
307
c2b13bb6
JJ
308 type = go_type_for_mode (mode, unsignedp);
309 if (type)
310 return type;
311
312#if HOST_BITS_PER_WIDE_INT >= 64
313 /* The middle-end and some backends rely on TImode being supported
314 for 64-bit HWI. */
315 if (mode == TImode)
316 {
317 type = build_nonstandard_integer_type (GET_MODE_BITSIZE (TImode),
318 unsignedp);
319 if (type && TYPE_MODE (type) == TImode)
320 return type;
321 }
322#endif
323 return NULL_TREE;
7a938933
ILT
324}
325
326/* Record a builtin function. We just ignore builtin functions. */
327
328static tree
329go_langhook_builtin_function (tree decl)
330{
331 return decl;
332}
333
c99c0026
EB
334/* Return true if we are in the global binding level. */
335
336static bool
7a938933
ILT
337go_langhook_global_bindings_p (void)
338{
c99c0026 339 return current_function_decl == NULL_TREE;
7a938933
ILT
340}
341
342/* Push a declaration into the current binding level. We can't
343 usefully implement this since we don't want to convert from tree
344 back to one of our internal data structures. I think the only way
345 this is used is to record a decl which is to be returned by
346 getdecls, and we could implement it for that purpose if
347 necessary. */
348
349static tree
350go_langhook_pushdecl (tree decl ATTRIBUTE_UNUSED)
351{
352 gcc_unreachable ();
353}
354
355/* This hook is used to get the current list of declarations as trees.
356 We don't support that; instead we use the write_globals hook. This
357 can't simply crash because it is called by -gstabs. */
358
359static tree
360go_langhook_getdecls (void)
361{
362 return NULL;
363}
364
365/* Write out globals. */
366
367static void
368go_langhook_write_globals (void)
369{
370 go_write_globals ();
371}
372
373/* Go specific gimplification. We need to gimplify
374 CALL_EXPR_STATIC_CHAIN, because the gimplifier doesn't handle
375 it. */
376
377static int
378go_langhook_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
379{
380 if (TREE_CODE (*expr_p) == CALL_EXPR
381 && CALL_EXPR_STATIC_CHAIN (*expr_p) != NULL_TREE)
382 gimplify_expr (&CALL_EXPR_STATIC_CHAIN (*expr_p), pre_p, post_p,
383 is_gimple_val, fb_rvalue);
384 return GS_UNHANDLED;
385}
386
387/* Return a decl for the exception personality function. The function
388 itself is implemented in libgo/runtime/go-unwind.c. */
389
390static tree
391go_langhook_eh_personality (void)
392{
393 static tree personality_decl;
394 if (personality_decl == NULL_TREE)
395 {
396 personality_decl = build_personality_function ("gccgo");
397 go_preserve_from_gc (personality_decl);
398 }
399 return personality_decl;
400}
401
402/* Functions called directly by the generic backend. */
403
404tree
405convert (tree type, tree expr)
406{
407 if (type == error_mark_node
408 || expr == error_mark_node
409 || TREE_TYPE (expr) == error_mark_node)
410 return error_mark_node;
411
412 if (type == TREE_TYPE (expr))
413 return expr;
414
415 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (expr)))
416 return fold_convert (type, expr);
417
418 switch (TREE_CODE (type))
419 {
420 case VOID_TYPE:
421 case BOOLEAN_TYPE:
422 return fold_convert (type, expr);
423 case INTEGER_TYPE:
424 return fold (convert_to_integer (type, expr));
425 case POINTER_TYPE:
426 return fold (convert_to_pointer (type, expr));
427 case REAL_TYPE:
428 return fold (convert_to_real (type, expr));
429 case COMPLEX_TYPE:
430 return fold (convert_to_complex (type, expr));
431 default:
432 break;
433 }
434
435 gcc_unreachable ();
436}
437
438/* FIXME: This is a hack to preserve trees that we create from the
439 garbage collector. */
440
441static GTY(()) tree go_gc_root;
442
443void
444go_preserve_from_gc (tree t)
445{
446 go_gc_root = tree_cons (NULL_TREE, t, go_gc_root);
447}
448
449/* Convert an identifier for use in an error message. */
450
451const char *
452go_localize_identifier (const char *ident)
453{
454 return identifier_to_locale (ident);
455}
456
457#undef LANG_HOOKS_NAME
458#undef LANG_HOOKS_INIT
459#undef LANG_HOOKS_OPTION_LANG_MASK
460#undef LANG_HOOKS_INIT_OPTIONS_STRUCT
461#undef LANG_HOOKS_HANDLE_OPTION
462#undef LANG_HOOKS_POST_OPTIONS
463#undef LANG_HOOKS_PARSE_FILE
464#undef LANG_HOOKS_TYPE_FOR_MODE
465#undef LANG_HOOKS_TYPE_FOR_SIZE
466#undef LANG_HOOKS_BUILTIN_FUNCTION
467#undef LANG_HOOKS_GLOBAL_BINDINGS_P
468#undef LANG_HOOKS_PUSHDECL
469#undef LANG_HOOKS_GETDECLS
470#undef LANG_HOOKS_WRITE_GLOBALS
471#undef LANG_HOOKS_GIMPLIFY_EXPR
472#undef LANG_HOOKS_EH_PERSONALITY
473
474#define LANG_HOOKS_NAME "GNU Go"
475#define LANG_HOOKS_INIT go_langhook_init
476#define LANG_HOOKS_OPTION_LANG_MASK go_langhook_option_lang_mask
477#define LANG_HOOKS_INIT_OPTIONS_STRUCT go_langhook_init_options_struct
478#define LANG_HOOKS_HANDLE_OPTION go_langhook_handle_option
479#define LANG_HOOKS_POST_OPTIONS go_langhook_post_options
480#define LANG_HOOKS_PARSE_FILE go_langhook_parse_file
481#define LANG_HOOKS_TYPE_FOR_MODE go_langhook_type_for_mode
482#define LANG_HOOKS_TYPE_FOR_SIZE go_langhook_type_for_size
483#define LANG_HOOKS_BUILTIN_FUNCTION go_langhook_builtin_function
484#define LANG_HOOKS_GLOBAL_BINDINGS_P go_langhook_global_bindings_p
485#define LANG_HOOKS_PUSHDECL go_langhook_pushdecl
486#define LANG_HOOKS_GETDECLS go_langhook_getdecls
487#define LANG_HOOKS_WRITE_GLOBALS go_langhook_write_globals
488#define LANG_HOOKS_GIMPLIFY_EXPR go_langhook_gimplify_expr
489#define LANG_HOOKS_EH_PERSONALITY go_langhook_eh_personality
490
491struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
492
493#include "gt-go-go-lang.h"
494#include "gtype-go.h"