1 // Copyright (C) 2020-2023 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
)
106 Rust::Session::get_instance ().options
.target_data
.insert_key_value_pair (
110 /* Language hooks. */
112 /* Initial lang hook called (possibly), used for initialisation.
113 * Must call build_common_tree_nodes, set_sizetype, build_common_tree_nodes_2,
114 * and build_common_builtin_nodes, as well as set global variable
115 * void_list_node. Apparently called after option handling? */
117 grs_langhook_init (void)
119 /* Something to do with this:
120 This allows the code in d-builtins.cc to not have to worry about
121 converting (C signed char *) to (D char *) for string arguments of
122 built-in functions. The parameter (signed_char = false) specifies
123 whether char is signed. */
124 build_common_tree_nodes (false);
126 // Creates a new TREE_LIST node with purpose NULL_TREE and value
128 void_list_node
= build_tree_list (NULL_TREE
, void_type_node
);
130 // Builds built-ins for middle-end after all front-end built-ins are already
132 build_common_builtin_nodes ();
134 mpfr_set_default_prec (128);
136 using_eh_for_cleanups ();
138 // initialise compiler session
139 Rust::Session::get_instance ().init ();
144 /* The option mask (something to do with options for specific frontends or
147 grs_langhook_option_lang_mask (void)
152 /* Initialize the options structure. */
154 grs_langhook_init_options_struct (struct gcc_options
*opts
)
156 /* Operations are always wrapping in Rust, even on signed integer. This is
157 * useful for the low level wrapping_{add, sub, mul} intrinsics, not for
158 * regular arithmetic operations which are checked for overflow anyway using
160 opts
->x_flag_wrapv
= 1;
162 /* We need to warn on unused variables by default */
163 opts
->x_warn_unused_variable
= 1;
164 /* For const variables too */
165 opts
->x_warn_unused_const_variable
= 1;
166 /* And finally unused result for #[must_use] */
167 opts
->x_warn_unused_result
= 1;
169 // nothing yet - used by frontends to change specific options for the language
170 Rust::Session::get_instance ().init_options ();
173 /* Main entry point for front-end, apparently. Finds input file names in global
174 * vars in_fnames and num_in_fnames. From this, frontend can take over and do
175 * actual parsing and initial compilation. This function must create a complete
176 * parse tree in a global var, and then return.
178 * Some consider this the "start of compilation". */
180 grs_langhook_parse_file (void)
182 rust_debug ("Preparing to parse files. ");
184 Rust::Session::get_instance ().handle_input_files (num_in_fnames
, in_fnames
);
187 /* Seems to get the exact type for a specific type - e.g. for scalar float with
188 * 32-bit bitsize, it returns float, and for 64-bit bitsize, it returns double.
189 * Used to map RTL nodes to machine modes or something like that. */
191 grs_langhook_type_for_mode (machine_mode mode
, int unsignedp
)
193 // TODO: change all this later to match rustc types
194 if (mode
== TYPE_MODE (float_type_node
))
195 return float_type_node
;
197 if (mode
== TYPE_MODE (double_type_node
))
198 return double_type_node
;
200 if (mode
== TYPE_MODE (intQI_type_node
)) // quarter integer mode - single byte
201 // treated as integer
202 return unsignedp
? unsigned_intQI_type_node
: intQI_type_node
;
204 == TYPE_MODE (intHI_type_node
)) // half integer mode - two-byte integer
205 return unsignedp
? unsigned_intHI_type_node
: intHI_type_node
;
207 == TYPE_MODE (intSI_type_node
)) // single integer mode - four-byte integer
208 return unsignedp
? unsigned_intSI_type_node
: intSI_type_node
;
211 intDI_type_node
)) // double integer mode - eight-byte integer
212 return unsignedp
? unsigned_intDI_type_node
: intDI_type_node
;
214 == TYPE_MODE (intTI_type_node
)) // tetra integer mode - 16-byte integer
215 return unsignedp
? unsigned_intTI_type_node
: intTI_type_node
;
217 if (mode
== TYPE_MODE (integer_type_node
))
218 return unsignedp
? unsigned_type_node
: integer_type_node
;
220 if (mode
== TYPE_MODE (long_integer_type_node
))
221 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
223 if (mode
== TYPE_MODE (long_long_integer_type_node
))
224 return unsignedp
? long_long_unsigned_type_node
225 : long_long_integer_type_node
;
227 if (COMPLEX_MODE_P (mode
))
229 if (mode
== TYPE_MODE (complex_float_type_node
))
230 return complex_float_type_node
;
231 if (mode
== TYPE_MODE (complex_double_type_node
))
232 return complex_double_type_node
;
233 if (mode
== TYPE_MODE (complex_long_double_type_node
))
234 return complex_long_double_type_node
;
235 if (mode
== TYPE_MODE (complex_integer_type_node
) && !unsignedp
)
236 return complex_integer_type_node
;
238 /* gcc_unreachable */
242 // Record a builtin function. We just ignore builtin functions.
244 grs_langhook_builtin_function (tree decl ATTRIBUTE_UNUSED
)
249 /* Return true if we are in the global binding level (which is never,
252 grs_langhook_global_bindings_p (void)
254 // return current_function_decl == NULL_TREE;
255 // gcc_unreachable();
260 /* Push a declaration into the current binding level. We can't
261 usefully implement this since we don't want to convert from tree
262 back to one of our internal data structures. I think the only way
263 this is used is to record a decl which is to be returned by
264 getdecls, and we could implement it for that purpose if
267 grs_langhook_pushdecl (tree decl ATTRIBUTE_UNUSED
)
273 /* This hook is used to get the current list of declarations as trees.
274 We don't support that; instead we use the write_globals hook. This
275 can't simply crash because it is called by -gstabs. */
277 grs_langhook_getdecls (void)
279 // gcc_unreachable();
283 // Handle Rust-specific options. Return false if nothing happened.
285 grs_langhook_handle_option (
286 size_t scode
, const char *arg
, HOST_WIDE_INT value
, int kind ATTRIBUTE_UNUSED
,
287 location_t loc ATTRIBUTE_UNUSED
,
288 const struct cl_option_handlers
*handlers ATTRIBUTE_UNUSED
)
290 // Convert integer code to lang.opt enum codes with names.
291 enum opt_code code
= (enum opt_code
) scode
;
293 // Delegate to session manager
294 return Rust::Session::get_instance ().handle_option (code
, arg
, value
, kind
,
298 /* Run after parsing options. */
300 grs_langhook_post_options (const char **pfilename ATTRIBUTE_UNUSED
)
302 // can be used to override other options if required
304 // satisfies an assert in init_excess_precision in toplev.cc
305 if (flag_excess_precision
/*_cmdline*/ == EXCESS_PRECISION_DEFAULT
)
306 flag_excess_precision
/*_cmdline*/ = EXCESS_PRECISION_STANDARD
;
308 /* Returning false means that the backend should be used. */
312 /* Rust-specific gimplification. May need to gimplify e.g.
313 * CALL_EXPR_STATIC_CHAIN */
315 grs_langhook_gimplify_expr (tree
*expr_p ATTRIBUTE_UNUSED
,
316 gimple_seq
*pre_p ATTRIBUTE_UNUSED
,
317 gimple_seq
*post_p ATTRIBUTE_UNUSED
)
319 if (TREE_CODE (*expr_p
) == CALL_EXPR
320 && CALL_EXPR_STATIC_CHAIN (*expr_p
) != NULL_TREE
)
321 gimplify_expr (&CALL_EXPR_STATIC_CHAIN (*expr_p
), pre_p
, post_p
,
322 is_gimple_val
, fb_rvalue
);
327 grs_langhook_eh_personality (void)
329 static tree personality_decl
;
330 if (personality_decl
== NULL_TREE
)
332 personality_decl
= build_personality_function ("gccrs");
333 rust_preserve_from_gc (personality_decl
);
335 return personality_decl
;
339 convert (tree type
, tree expr
)
341 if (type
== error_mark_node
|| expr
== error_mark_node
342 || TREE_TYPE (expr
) == error_mark_node
)
343 return error_mark_node
;
345 if (type
== TREE_TYPE (expr
))
348 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (TREE_TYPE (expr
)))
349 return fold_convert (type
, expr
);
351 switch (TREE_CODE (type
))
355 return fold_convert (type
, expr
);
357 return convert_to_integer (type
, expr
);
359 return convert_to_pointer (type
, expr
);
361 return convert_to_real (type
, expr
);
363 return convert_to_complex (type
, expr
);
371 /* FIXME: This is a hack to preserve trees that we create from the
372 garbage collector. */
374 static GTY (()) tree rust_gc_root
;
377 rust_preserve_from_gc (tree t
)
379 rust_gc_root
= tree_cons (NULL_TREE
, t
, rust_gc_root
);
382 /* Convert an identifier for use in an error message. */
385 rust_localize_identifier (const char *ident
)
387 return identifier_to_locale (ident
);
390 /* The language hooks data structure. This is the main interface between the GCC
391 * front-end and the GCC middle-end/back-end. A list of language hooks could be
392 * found in <gcc>/langhooks.h
394 #undef LANG_HOOKS_NAME
395 #undef LANG_HOOKS_INIT
396 #undef LANG_HOOKS_OPTION_LANG_MASK
397 #undef LANG_HOOKS_INIT_OPTIONS_STRUCT
398 #undef LANG_HOOKS_HANDLE_OPTION
399 #undef LANG_HOOKS_POST_OPTIONS
400 #undef LANG_HOOKS_PARSE_FILE
401 #undef LANG_HOOKS_TYPE_FOR_MODE
402 #undef LANG_HOOKS_BUILTIN_FUNCTION
403 #undef LANG_HOOKS_GLOBAL_BINDINGS_P
404 #undef LANG_HOOKS_PUSHDECL
405 #undef LANG_HOOKS_GETDECLS
406 #undef LANG_HOOKS_WRITE_GLOBALS
407 #undef LANG_HOOKS_GIMPLIFY_EXPR
408 #undef LANG_HOOKS_EH_PERSONALITY
410 #define LANG_HOOKS_NAME "GNU Rust"
411 #define LANG_HOOKS_INIT grs_langhook_init
412 #define LANG_HOOKS_OPTION_LANG_MASK grs_langhook_option_lang_mask
413 #define LANG_HOOKS_INIT_OPTIONS_STRUCT grs_langhook_init_options_struct
414 #define LANG_HOOKS_HANDLE_OPTION grs_langhook_handle_option
415 #define LANG_HOOKS_POST_OPTIONS grs_langhook_post_options
416 /* Main lang-hook, apparently. Finds input file names in global vars in_fnames
417 * and num_in_fnames From this, frontend can take over and do actual parsing and
418 * initial compilation.
419 * This hook must create a complete parse tree in a global var, and then return.
421 #define LANG_HOOKS_PARSE_FILE grs_langhook_parse_file
422 #define LANG_HOOKS_TYPE_FOR_MODE grs_langhook_type_for_mode
423 #define LANG_HOOKS_BUILTIN_FUNCTION grs_langhook_builtin_function
424 #define LANG_HOOKS_GLOBAL_BINDINGS_P grs_langhook_global_bindings_p
425 #define LANG_HOOKS_PUSHDECL grs_langhook_pushdecl
426 #define LANG_HOOKS_GETDECLS grs_langhook_getdecls
427 #define LANG_HOOKS_GIMPLIFY_EXPR grs_langhook_gimplify_expr
428 #define LANG_HOOKS_EH_PERSONALITY grs_langhook_eh_personality
432 #undef LANG_HOOKS_RUN_LANG_SELFTESTS
433 #define LANG_HOOKS_RUN_LANG_SELFTESTS selftest::run_rust_tests
440 // Call tests for the rust frontend here
441 rust_cfg_parser_test ();
442 rust_privacy_ctx_test ();
443 rust_crate_name_validation_test ();
444 rust_simple_path_resolve_test ();
445 rust_optional_test ();
447 } // namespace selftest
449 #endif /* !CHECKING_P */
451 // Expands all LANG_HOOKS_x of GCC
452 struct lang_hooks lang_hooks
= LANG_HOOKS_INITIALIZER
;
454 // These are for GCC's garbage collector to work properly or something
455 #include "gt-rust-rust-lang.h"
456 #include "gtype-rust.h"