1 // Copyright (C) 2020-2024 Free Software Foundation, Inc.
3 // This file is part of GCC.
5 // GCC is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation; either version 3, or (at your option) any later
10 // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 // You should have received a copy of the GNU General Public License
16 // along with GCC; see the file COPYING3. If not see
17 // <http://www.gnu.org/licenses/>.
19 #include "rust-system.h"
20 #include "rust-diagnostics.h"
21 #include "coretypes.h"
24 #include "gimple-expr.h"
25 #include "diagnostic.h"
27 #include "fold-const.h"
29 #include "stor-layout.h"
32 #include "langhooks.h"
33 #include "langhooks-def.h"
35 #include "rust-cfg-parser.h"
36 #include "rust-privacy-ctx.h"
37 #include "rust-ast-resolve-item.h"
38 #include "rust-optional.h"
41 // note: header files must be in this order or else forward declarations don't
42 // work properly. Kinda dumb system, but have to live with it. clang-format
43 // seems to mess it up
44 /* Order: config, system, coretypes, target, tree, gimple-expr, diagnostic,
45 * opts, fold-const, gimplify, stor-layout, debug, convert, langhooks,
48 // FIXME: test saving intellisense
51 // version check to stop compiling if c++ isn't c++11 or higher
52 #if __cplusplus < 201103
54 "GCC Rust frontend requires C++11 or higher. You can compile the g++ frontend first and then compile the Rust frontend using that."
56 // TODO: is this best way to do it? Is it allowed? (should be)
59 * - convert all copies of expensive-to-copy (deep copy) AST objects into
60 * moves, if possible. Don't remove clone functionality - it may be required for
61 * e.g. HIR conversion.
64 #include "rust-session-manager.h"
66 // Language-dependent contents of a type. GTY() mark used for garbage collector.
67 struct GTY (()) lang_type
71 // Language-dependent contents of a decl.
72 struct GTY (()) lang_decl
76 // Language-dependent contents of an identifier. This must include a
78 struct GTY (()) lang_identifier
80 struct tree_identifier common
;
83 // The resulting tree type.
85 desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
87 "CODE_CONTAINS_STRUCT (TREE_CODE (&%h.generic), "
88 "TS_COMMON) ? ((union lang_tree_node *) TREE_CHAIN (&%h.generic)) : NULL")))
91 union tree_node
GTY ((tag ("0"), desc ("tree_node_structure (&%h)"))) generic
;
92 struct lang_identifier
GTY ((tag ("1"))) identifier
;
95 // We don't use language_function.
96 struct GTY (()) language_function
100 // has to be in same compilation unit as session, so here for now
102 rust_add_target_info (const char *key
, const char *value
)
104 Rust::Session::get_instance ().options
.target_data
.insert_key_value_pair (
108 /* Language hooks. */
110 /* Initial lang hook called (possibly), used for initialisation.
111 * Must call build_common_tree_nodes, set_sizetype, build_common_tree_nodes_2,
112 * and build_common_builtin_nodes, as well as set global variable
113 * void_list_node. Apparently called after option handling? */
115 grs_langhook_init (void)
117 /* Something to do with this:
118 This allows the code in d-builtins.cc to not have to worry about
119 converting (C signed char *) to (D char *) for string arguments of
120 built-in functions. The parameter (signed_char = false) specifies
121 whether char is signed. */
122 build_common_tree_nodes (false);
124 // Builds built-ins for middle-end after all front-end built-ins are already
126 build_common_builtin_nodes ();
128 mpfr_set_default_prec (128);
130 using_eh_for_cleanups ();
132 // initialise compiler session
133 Rust::Session::get_instance ().init ();
138 /* The option mask (something to do with options for specific frontends or
141 grs_langhook_option_lang_mask (void)
146 /* Initialize the options structure. */
148 grs_langhook_init_options_struct (struct gcc_options
*opts
)
150 /* Operations are always wrapping in Rust, even on signed integer. This is
151 * useful for the low level wrapping_{add, sub, mul} intrinsics, not for
152 * regular arithmetic operations which are checked for overflow anyway using
154 opts
->x_flag_wrapv
= 1;
156 /* We need to warn on unused variables by default */
157 opts
->x_warn_unused_variable
= 1;
158 /* For const variables too */
159 opts
->x_warn_unused_const_variable
= 1;
160 /* And finally unused result for #[must_use] */
161 opts
->x_warn_unused_result
= 1;
163 // nothing yet - used by frontends to change specific options for the language
164 Rust::Session::get_instance ().init_options ();
167 /* Main entry point for front-end, apparently. Finds input file names in global
168 * vars in_fnames and num_in_fnames. From this, frontend can take over and do
169 * actual parsing and initial compilation. This function must create a complete
170 * parse tree in a global var, and then return.
172 * Some consider this the "start of compilation". */
174 grs_langhook_parse_file (void)
176 rust_debug ("Preparing to parse files. ");
178 Rust::Session::get_instance ().handle_input_files (num_in_fnames
, in_fnames
);
181 /* Seems to get the exact type for a specific type - e.g. for scalar float with
182 * 32-bit bitsize, it returns float, and for 64-bit bitsize, it returns double.
183 * Used to map RTL nodes to machine modes or something like that. */
185 grs_langhook_type_for_mode (machine_mode mode
, int unsignedp
)
187 // TODO: change all this later to match rustc types
188 if (mode
== TYPE_MODE (float_type_node
))
189 return float_type_node
;
191 if (mode
== TYPE_MODE (double_type_node
))
192 return double_type_node
;
194 if (mode
== TYPE_MODE (intQI_type_node
)) // quarter integer mode - single byte
195 // treated as integer
196 return unsignedp
? unsigned_intQI_type_node
: intQI_type_node
;
198 == TYPE_MODE (intHI_type_node
)) // half integer mode - two-byte integer
199 return unsignedp
? unsigned_intHI_type_node
: intHI_type_node
;
201 == TYPE_MODE (intSI_type_node
)) // single integer mode - four-byte integer
202 return unsignedp
? unsigned_intSI_type_node
: intSI_type_node
;
205 intDI_type_node
)) // double integer mode - eight-byte integer
206 return unsignedp
? unsigned_intDI_type_node
: intDI_type_node
;
208 == TYPE_MODE (intTI_type_node
)) // tetra integer mode - 16-byte integer
209 return unsignedp
? unsigned_intTI_type_node
: intTI_type_node
;
211 if (mode
== TYPE_MODE (integer_type_node
))
212 return unsignedp
? unsigned_type_node
: integer_type_node
;
214 if (mode
== TYPE_MODE (long_integer_type_node
))
215 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
217 if (mode
== TYPE_MODE (long_long_integer_type_node
))
218 return unsignedp
? long_long_unsigned_type_node
219 : long_long_integer_type_node
;
221 if (COMPLEX_MODE_P (mode
))
223 if (mode
== TYPE_MODE (complex_float_type_node
))
224 return complex_float_type_node
;
225 if (mode
== TYPE_MODE (complex_double_type_node
))
226 return complex_double_type_node
;
227 if (mode
== TYPE_MODE (complex_long_double_type_node
))
228 return complex_long_double_type_node
;
229 if (mode
== TYPE_MODE (complex_integer_type_node
) && !unsignedp
)
230 return complex_integer_type_node
;
233 /* See (a) <https://github.com/Rust-GCC/gccrs/issues/1713>
234 "Test failure on msp430-elfbare target", and
235 (b) <https://gcc.gnu.org/PR46805>
236 "ICE: SIGSEGV in optab_for_tree_code (optabs.c:407) with -O -fno-tree-scev-cprop -ftree-vectorize"
237 -- we have to support "random" modes/types here.
238 TODO Clean all this up (either locally, or preferably per PR46805:
239 "Ideally we'd never use lang_hooks.types.type_for_mode (or _for_size) in the
240 middle-end but had a pure middle-end based implementation". */
241 for (size_t i
= 0; i
< NUM_INT_N_ENTS
; i
++)
242 if (int_n_enabled_p
[i
]
243 && mode
== int_n_data
[i
].m
)
244 return (unsignedp
? int_n_trees
[i
].unsigned_type
245 : int_n_trees
[i
].signed_type
);
247 /* gcc_unreachable */
251 // Record a builtin function. We just ignore builtin functions.
253 grs_langhook_builtin_function (tree decl ATTRIBUTE_UNUSED
)
258 /* Return true if we are in the global binding level (which is never,
261 grs_langhook_global_bindings_p (void)
263 // return current_function_decl == NULL_TREE;
264 // gcc_unreachable();
269 /* Push a declaration into the current binding level. We can't
270 usefully implement this since we don't want to convert from tree
271 back to one of our internal data structures. I think the only way
272 this is used is to record a decl which is to be returned by
273 getdecls, and we could implement it for that purpose if
276 grs_langhook_pushdecl (tree decl ATTRIBUTE_UNUSED
)
282 /* This hook is used to get the current list of declarations as trees.
283 We don't support that; instead we use the write_globals hook. This
284 can't simply crash because it is called by -gstabs. */
286 grs_langhook_getdecls (void)
288 // gcc_unreachable();
292 // Handle Rust-specific options. Return false if nothing happened.
294 grs_langhook_handle_option (
295 size_t scode
, const char *arg
, HOST_WIDE_INT value
, int kind ATTRIBUTE_UNUSED
,
296 location_t loc ATTRIBUTE_UNUSED
,
297 const struct cl_option_handlers
*handlers ATTRIBUTE_UNUSED
)
299 // Convert integer code to lang.opt enum codes with names.
300 enum opt_code code
= (enum opt_code
) scode
;
302 // Delegate to session manager
303 return Rust::Session::get_instance ().handle_option (code
, arg
, value
, kind
,
307 /* Run after parsing options. */
309 grs_langhook_post_options (const char **pfilename ATTRIBUTE_UNUSED
)
311 // can be used to override other options if required
313 // satisfies an assert in init_excess_precision in toplev.cc
314 if (flag_excess_precision
/*_cmdline*/ == EXCESS_PRECISION_DEFAULT
)
315 flag_excess_precision
/*_cmdline*/ = EXCESS_PRECISION_STANDARD
;
317 /* Returning false means that the backend should be used. */
321 /* Rust-specific gimplification. May need to gimplify e.g.
322 * CALL_EXPR_STATIC_CHAIN */
324 grs_langhook_gimplify_expr (tree
*expr_p ATTRIBUTE_UNUSED
,
325 gimple_seq
*pre_p ATTRIBUTE_UNUSED
,
326 gimple_seq
*post_p ATTRIBUTE_UNUSED
)
328 if (TREE_CODE (*expr_p
) == CALL_EXPR
329 && CALL_EXPR_STATIC_CHAIN (*expr_p
) != NULL_TREE
)
330 gimplify_expr (&CALL_EXPR_STATIC_CHAIN (*expr_p
), pre_p
, post_p
,
331 is_gimple_val
, fb_rvalue
);
336 grs_langhook_eh_personality (void)
338 static tree personality_decl
;
339 if (personality_decl
== NULL_TREE
)
341 personality_decl
= build_personality_function ("gccrs");
342 rust_preserve_from_gc (personality_decl
);
344 return personality_decl
;
348 convert (tree type
, tree expr
)
350 if (type
== error_mark_node
|| expr
== error_mark_node
351 || TREE_TYPE (expr
) == error_mark_node
)
352 return error_mark_node
;
354 if (type
== TREE_TYPE (expr
))
357 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (TREE_TYPE (expr
)))
358 return fold_convert (type
, expr
);
360 switch (TREE_CODE (type
))
364 return fold_convert (type
, expr
);
366 return convert_to_integer (type
, expr
);
368 return convert_to_pointer (type
, expr
);
370 return convert_to_real (type
, expr
);
372 return convert_to_complex (type
, expr
);
380 /* FIXME: This is a hack to preserve trees that we create from the
381 garbage collector. */
383 static GTY (()) tree rust_gc_root
;
386 rust_preserve_from_gc (tree t
)
388 rust_gc_root
= tree_cons (NULL_TREE
, t
, rust_gc_root
);
391 /* Convert an identifier for use in an error message. */
394 rust_localize_identifier (const char *ident
)
396 return identifier_to_locale (ident
);
399 /* The language hooks data structure. This is the main interface between the GCC
400 * front-end and the GCC middle-end/back-end. A list of language hooks could be
401 * found in <gcc>/langhooks.h
403 #undef LANG_HOOKS_NAME
404 #undef LANG_HOOKS_INIT
405 #undef LANG_HOOKS_OPTION_LANG_MASK
406 #undef LANG_HOOKS_INIT_OPTIONS_STRUCT
407 #undef LANG_HOOKS_HANDLE_OPTION
408 #undef LANG_HOOKS_POST_OPTIONS
409 #undef LANG_HOOKS_PARSE_FILE
410 #undef LANG_HOOKS_TYPE_FOR_MODE
411 #undef LANG_HOOKS_BUILTIN_FUNCTION
412 #undef LANG_HOOKS_GLOBAL_BINDINGS_P
413 #undef LANG_HOOKS_PUSHDECL
414 #undef LANG_HOOKS_GETDECLS
415 #undef LANG_HOOKS_WRITE_GLOBALS
416 #undef LANG_HOOKS_GIMPLIFY_EXPR
417 #undef LANG_HOOKS_EH_PERSONALITY
419 #define LANG_HOOKS_NAME "GNU Rust"
420 #define LANG_HOOKS_INIT grs_langhook_init
421 #define LANG_HOOKS_OPTION_LANG_MASK grs_langhook_option_lang_mask
422 #define LANG_HOOKS_INIT_OPTIONS_STRUCT grs_langhook_init_options_struct
423 #define LANG_HOOKS_HANDLE_OPTION grs_langhook_handle_option
424 #define LANG_HOOKS_POST_OPTIONS grs_langhook_post_options
425 /* Main lang-hook, apparently. Finds input file names in global vars in_fnames
426 * and num_in_fnames From this, frontend can take over and do actual parsing and
427 * initial compilation.
428 * This hook must create a complete parse tree in a global var, and then return.
430 #define LANG_HOOKS_PARSE_FILE grs_langhook_parse_file
431 #define LANG_HOOKS_TYPE_FOR_MODE grs_langhook_type_for_mode
432 #define LANG_HOOKS_BUILTIN_FUNCTION grs_langhook_builtin_function
433 #define LANG_HOOKS_GLOBAL_BINDINGS_P grs_langhook_global_bindings_p
434 #define LANG_HOOKS_PUSHDECL grs_langhook_pushdecl
435 #define LANG_HOOKS_GETDECLS grs_langhook_getdecls
436 #define LANG_HOOKS_GIMPLIFY_EXPR grs_langhook_gimplify_expr
437 #define LANG_HOOKS_EH_PERSONALITY grs_langhook_eh_personality
441 #undef LANG_HOOKS_RUN_LANG_SELFTESTS
442 #define LANG_HOOKS_RUN_LANG_SELFTESTS selftest::run_rust_tests
449 // Call tests for the rust frontend here
450 rust_cfg_parser_test ();
451 rust_privacy_ctx_test ();
452 rust_crate_name_validation_test ();
453 rust_simple_path_resolve_test ();
454 rust_optional_test ();
456 } // namespace selftest
458 #endif /* !CHECKING_P */
460 // Expands all LANG_HOOKS_x of GCC
461 struct lang_hooks lang_hooks
= LANG_HOOKS_INITIALIZER
;
463 // These are for GCC's garbage collector to work properly or something
464 #include "gt-rust-rust-lang.h"
465 #include "gtype-rust.h"