]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/rust/rust-lang.cc
Update copyright years.
[thirdparty/gcc.git] / gcc / rust / rust-lang.cc
1 // Copyright (C) 2020-2023 Free Software Foundation, Inc.
2
3 // This file is part of GCC.
4
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
8 // version.
9
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
13 // for more details.
14
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/>.
18
19 #include "rust-system.h"
20 #include "rust-diagnostics.h"
21 #include "coretypes.h"
22 #include "target.h"
23 #include "tree.h"
24 #include "gimple-expr.h"
25 #include "diagnostic.h"
26 #include "opts.h"
27 #include "fold-const.h"
28 #include "gimplify.h"
29 #include "stor-layout.h"
30 #include "debug.h"
31 #include "convert.h"
32 #include "langhooks.h"
33 #include "langhooks-def.h"
34 #include "selftest.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"
39
40 #include <mpfr.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,
46 * langhooks-def */
47
48 // FIXME: test saving intellisense
49 #include "options.h"
50
51 // version check to stop compiling if c++ isn't c++11 or higher
52 #if __cplusplus < 201103
53 #error \
54 "GCC Rust frontend requires C++11 or higher. You can compile the g++ frontend first and then compile the Rust frontend using that."
55 #endif
56 // TODO: is this best way to do it? Is it allowed? (should be)
57
58 /* General TODOs:
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.
62 */
63
64 #include "rust-session-manager.h"
65
66 // Language-dependent contents of a type. GTY() mark used for garbage collector.
67 struct GTY (()) lang_type
68 {
69 };
70
71 // Language-dependent contents of a decl.
72 struct GTY (()) lang_decl
73 {
74 };
75
76 // Language-dependent contents of an identifier. This must include a
77 // tree_identifier.
78 struct GTY (()) lang_identifier
79 {
80 struct tree_identifier common;
81 };
82
83 // The resulting tree type.
84 union GTY ((
85 desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
86 chain_next (
87 "CODE_CONTAINS_STRUCT (TREE_CODE (&%h.generic), "
88 "TS_COMMON) ? ((union lang_tree_node *) TREE_CHAIN (&%h.generic)) : NULL")))
89 lang_tree_node
90 {
91 union tree_node GTY ((tag ("0"), desc ("tree_node_structure (&%h)"))) generic;
92 struct lang_identifier GTY ((tag ("1"))) identifier;
93 };
94
95 // We don't use language_function.
96 struct GTY (()) language_function
97 {
98 };
99
100 // has to be in same compilation unit as session, so here for now
101 void
102 rust_add_target_info (const char *key, const char *value)
103 {
104 sorry ("TODO");
105
106 Rust::Session::get_instance ().options.target_data.insert_key_value_pair (
107 key, value);
108 }
109
110 /* Language hooks. */
111
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? */
116 static bool
117 grs_langhook_init (void)
118 {
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);
125
126 // Creates a new TREE_LIST node with purpose NULL_TREE and value
127 // void_type_node
128 void_list_node = build_tree_list (NULL_TREE, void_type_node);
129
130 // Builds built-ins for middle-end after all front-end built-ins are already
131 // instantiated
132 build_common_builtin_nodes ();
133
134 mpfr_set_default_prec (128);
135
136 using_eh_for_cleanups ();
137
138 // initialise compiler session
139 Rust::Session::get_instance ().init ();
140
141 return true;
142 }
143
144 /* The option mask (something to do with options for specific frontends or
145 * something). */
146 static unsigned int
147 grs_langhook_option_lang_mask (void)
148 {
149 return CL_Rust;
150 }
151
152 /* Initialize the options structure. */
153 static void
154 grs_langhook_init_options_struct (struct gcc_options *opts)
155 {
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
159 * builtins */
160 opts->x_flag_wrapv = 1;
161
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;
168
169 // nothing yet - used by frontends to change specific options for the language
170 Rust::Session::get_instance ().init_options ();
171 }
172
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.
177 *
178 * Some consider this the "start of compilation". */
179 static void
180 grs_langhook_parse_file (void)
181 {
182 rust_debug ("Preparing to parse files. ");
183
184 Rust::Session::get_instance ().handle_input_files (num_in_fnames, in_fnames);
185 }
186
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. */
190 static tree
191 grs_langhook_type_for_mode (machine_mode mode, int unsignedp)
192 {
193 // TODO: change all this later to match rustc types
194 if (mode == TYPE_MODE (float_type_node))
195 return float_type_node;
196
197 if (mode == TYPE_MODE (double_type_node))
198 return double_type_node;
199
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;
203 if (mode
204 == TYPE_MODE (intHI_type_node)) // half integer mode - two-byte integer
205 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
206 if (mode
207 == TYPE_MODE (intSI_type_node)) // single integer mode - four-byte integer
208 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
209 if (mode
210 == TYPE_MODE (
211 intDI_type_node)) // double integer mode - eight-byte integer
212 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
213 if (mode
214 == TYPE_MODE (intTI_type_node)) // tetra integer mode - 16-byte integer
215 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
216
217 if (mode == TYPE_MODE (integer_type_node))
218 return unsignedp ? unsigned_type_node : integer_type_node;
219
220 if (mode == TYPE_MODE (long_integer_type_node))
221 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
222
223 if (mode == TYPE_MODE (long_long_integer_type_node))
224 return unsignedp ? long_long_unsigned_type_node
225 : long_long_integer_type_node;
226
227 if (COMPLEX_MODE_P (mode))
228 {
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;
237 }
238 /* gcc_unreachable */
239 return NULL;
240 }
241
242 // Record a builtin function. We just ignore builtin functions.
243 static tree
244 grs_langhook_builtin_function (tree decl ATTRIBUTE_UNUSED)
245 {
246 return decl;
247 }
248
249 /* Return true if we are in the global binding level (which is never,
250 * apparently). */
251 static bool
252 grs_langhook_global_bindings_p (void)
253 {
254 // return current_function_decl == NULL_TREE;
255 // gcc_unreachable();
256 // return true;
257 return false;
258 }
259
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
265 necessary. */
266 static tree
267 grs_langhook_pushdecl (tree decl ATTRIBUTE_UNUSED)
268 {
269 gcc_unreachable ();
270 return NULL;
271 }
272
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. */
276 static tree
277 grs_langhook_getdecls (void)
278 {
279 // gcc_unreachable();
280 return NULL;
281 }
282
283 // Handle Rust-specific options. Return false if nothing happened.
284 static bool
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)
289 {
290 // Convert integer code to lang.opt enum codes with names.
291 enum opt_code code = (enum opt_code) scode;
292
293 // Delegate to session manager
294 return Rust::Session::get_instance ().handle_option (code, arg, value, kind,
295 loc, handlers);
296 }
297
298 /* Run after parsing options. */
299 static bool
300 grs_langhook_post_options (const char **pfilename ATTRIBUTE_UNUSED)
301 {
302 // can be used to override other options if required
303
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;
307
308 /* Returning false means that the backend should be used. */
309 return false;
310 }
311
312 /* Rust-specific gimplification. May need to gimplify e.g.
313 * CALL_EXPR_STATIC_CHAIN */
314 static int
315 grs_langhook_gimplify_expr (tree *expr_p ATTRIBUTE_UNUSED,
316 gimple_seq *pre_p ATTRIBUTE_UNUSED,
317 gimple_seq *post_p ATTRIBUTE_UNUSED)
318 {
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);
323 return GS_UNHANDLED;
324 }
325
326 static tree
327 grs_langhook_eh_personality (void)
328 {
329 static tree personality_decl;
330 if (personality_decl == NULL_TREE)
331 {
332 personality_decl = build_personality_function ("gccrs");
333 rust_preserve_from_gc (personality_decl);
334 }
335 return personality_decl;
336 }
337
338 tree
339 convert (tree type, tree expr)
340 {
341 if (type == error_mark_node || expr == error_mark_node
342 || TREE_TYPE (expr) == error_mark_node)
343 return error_mark_node;
344
345 if (type == TREE_TYPE (expr))
346 return expr;
347
348 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (expr)))
349 return fold_convert (type, expr);
350
351 switch (TREE_CODE (type))
352 {
353 case VOID_TYPE:
354 case BOOLEAN_TYPE:
355 return fold_convert (type, expr);
356 case INTEGER_TYPE:
357 return convert_to_integer (type, expr);
358 case POINTER_TYPE:
359 return convert_to_pointer (type, expr);
360 case REAL_TYPE:
361 return convert_to_real (type, expr);
362 case COMPLEX_TYPE:
363 return convert_to_complex (type, expr);
364 default:
365 break;
366 }
367
368 gcc_unreachable ();
369 }
370
371 /* FIXME: This is a hack to preserve trees that we create from the
372 garbage collector. */
373
374 static GTY (()) tree rust_gc_root;
375
376 void
377 rust_preserve_from_gc (tree t)
378 {
379 rust_gc_root = tree_cons (NULL_TREE, t, rust_gc_root);
380 }
381
382 /* Convert an identifier for use in an error message. */
383
384 const char *
385 rust_localize_identifier (const char *ident)
386 {
387 return identifier_to_locale (ident);
388 }
389
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
393 */
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
409
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.
420 */
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
429
430 #if CHECKING_P
431
432 #undef LANG_HOOKS_RUN_LANG_SELFTESTS
433 #define LANG_HOOKS_RUN_LANG_SELFTESTS selftest::run_rust_tests
434
435 namespace selftest {
436
437 void
438 run_rust_tests ()
439 {
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 ();
446 }
447 } // namespace selftest
448
449 #endif /* !CHECKING_P */
450
451 // Expands all LANG_HOOKS_x of GCC
452 struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
453
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"