diff options
33 files changed, 762 insertions, 74 deletions
diff --git a/gcc/config.gcc b/gcc/config.gcc index 4cd81c8..7a2daa9 100644 --- a/gcc/config.gcc +++ b/gcc/config.gcc @@ -790,6 +790,8 @@ case ${target} in d_target_objs="${d_target_objs} dragonfly-d.o" tmake_file="${tmake_file} t-dragonfly" target_has_targetdm=yes + rust_target_objs="${rust_target_objs} dragonfly-rust.o" + target_has_targetrustm=yes ;; *-*-freebsd*) # This is the generic ELF configuration of FreeBSD. Later @@ -841,9 +843,14 @@ case ${target} in d_target_objs="${d_target_objs} freebsd-d.o" tmake_file="${tmake_file} t-freebsd" target_has_targetdm=yes + rust_target_objs="${rust_target_objs} freebsd-rust.o" + target_has_targetrustm=yes ;; *-*-fuchsia*) native_system_header_dir=/include + tmake_file="t-fuchsia" + rust_target_objs="${rust_target_objs} fuchsia-rust.o" + target_has_targetrustm=yes ;; *-*-linux* | frv-*-*linux* | *-*-kfreebsd*-gnu | *-*-gnu* | *-*-kopensolaris*-gnu | *-*-uclinuxfdpiceabi) extra_options="$extra_options gnu-user.opt" @@ -912,6 +919,12 @@ case ${target} in gcc_cv_initfini_array=yes ;; esac + case $target in + *linux*) + rust_target_objs="${rust_target_objs} linux-rust.o" + target_has_targetrustm=yes + ;; + esac ;; *-*-netbsd*) tm_p_file="${tm_p_file} netbsd-protos.h" @@ -932,6 +945,8 @@ case ${target} in default_gnu_indirect_function=yes ;; esac + rust_target_objs="${rust_target_objs} netbsd-rust.o" + target_has_targetrustm=yes ;; *-*-openbsd*) tmake_file="t-openbsd" @@ -947,6 +962,8 @@ case ${target} in esac d_target_objs="${d_target_objs} openbsd-d.o" target_has_targetdm=yes + rust_target_objs="${rust_target_objs} openbsd-rust.o" + target_has_targetrustm=yes ;; *-*-phoenix*) gas=yes @@ -1011,6 +1028,8 @@ case ${target} in ;; esac target_has_targetdm=yes + rust_target_objs="${rust_target_objs} sol2-rust.o" + target_has_targetrustm=yes ;; *-*-*vms*) extra_options="${extra_options} vms/vms.opt" @@ -1043,6 +1062,9 @@ case ${target} in extra_headers="${extra_headers} ../vxworks/vxworks-predef.h" target_has_targetcm="yes" + rust_target_objs="${rust_target_objs} vxworks-rust.o" + target_has_targetrustm=yes + # This private header exposes a consistent interface for checks on # the VxWorks version our runtime header files need to perform, based on # what the system headers adverstise: @@ -2100,6 +2122,8 @@ i[34567]86-*-mingw* | x86_64-*-mingw*) d_target_objs="${d_target_objs} winnt-d.o" target_has_targetcm="yes" target_has_targetdm="yes" + rust_target_objs="${rust_target_objs} winnt-rust.o" + target_has_targetrustm="yes" case ${target} in x86_64-*-* | *-w64-*) need_64bit_isa=yes diff --git a/gcc/config/dragonfly-rust.cc b/gcc/config/dragonfly-rust.cc new file mode 100644 index 0000000..ce501d1 --- /dev/null +++ b/gcc/config/dragonfly-rust.cc @@ -0,0 +1,40 @@ +/* DragonFly support needed only by Rust front-end. + Copyright (C) 2022 Free Software Foundation, Inc. + +GCC is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation; either version 3, or (at your option) any later +version. + +GCC is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with GCC; see the file COPYING3. If not see +<http://www.gnu.org/licenses/>. */ + +#include "config.h" +#include "system.h" +#include "coretypes.h" +#include "tm.h" +#include "tm_rust.h" +#include "rust/rust-target.h" +#include "rust/rust-target-def.h" + +/* Implement TARGET_RUST_OS_INFO for DragonFly targets. */ + +static void +dragonfly_rust_target_os_info (void) +{ + rust_add_target_info ("target_family", "unix"); + rust_add_target_info ("target_os", "dragonfly"); + rust_add_target_info ("target_vendor", "unknown"); + rust_add_target_info ("target_env", ""); +} + +#undef TARGET_RUST_OS_INFO +#define TARGET_RUST_OS_INFO dragonfly_rust_target_os_info + +struct gcc_targetrustm targetrustm = TARGETRUSTM_INITIALIZER; diff --git a/gcc/config/freebsd-rust.cc b/gcc/config/freebsd-rust.cc new file mode 100644 index 0000000..1dbf2ed --- /dev/null +++ b/gcc/config/freebsd-rust.cc @@ -0,0 +1,40 @@ +/* FreeBSD support needed only by Rust front-end. + Copyright (C) 2022 Free Software Foundation, Inc. + +GCC is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation; either version 3, or (at your option) any later +version. + +GCC is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with GCC; see the file COPYING3. If not see +<http://www.gnu.org/licenses/>. */ + +#include "config.h" +#include "system.h" +#include "coretypes.h" +#include "tm.h" +#include "tm_rust.h" +#include "rust/rust-target.h" +#include "rust/rust-target-def.h" + +/* Implement TARGET_RUST_OS_INFO for FreeBSD targets. */ + +static void +freebsd_rust_target_os_info (void) +{ + rust_add_target_info ("target_family", "unix"); + rust_add_target_info ("target_os", "freebsd"); + rust_add_target_info ("target_vendor", "unknown"); + rust_add_target_info ("target_env", ""); +} + +#undef TARGET_RUST_OS_INFO +#define TARGET_RUST_OS_INFO freebsd_rust_target_os_info + +struct gcc_targetrustm targetrustm = TARGETRUSTM_INITIALIZER; diff --git a/gcc/config/fuchsia-rust.cc b/gcc/config/fuchsia-rust.cc new file mode 100644 index 0000000..8626250 --- /dev/null +++ b/gcc/config/fuchsia-rust.cc @@ -0,0 +1,40 @@ +/* Fuchsia support needed only by Rust front-end. + Copyright (C) 2022 Free Software Foundation, Inc. + +GCC is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation; either version 3, or (at your option) any later +version. + +GCC is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with GCC; see the file COPYING3. If not see +<http://www.gnu.org/licenses/>. */ + +#include "config.h" +#include "system.h" +#include "coretypes.h" +#include "tm.h" +#include "tm_rust.h" +#include "rust/rust-target.h" +#include "rust/rust-target-def.h" + +/* Implement TARGET_RUST_OS_INFO for Fuchsia targets. */ + +static void +fushsia_rust_target_os_info (void) +{ + rust_add_target_info ("target_family", "unix"); + rust_add_target_info ("target_os", "fushsia"); + rust_add_target_info ("target_vendor", "unknown"); + rust_add_target_info ("target_env", ""); +} + +#undef TARGET_RUST_OS_INFO +#define TARGET_RUST_OS_INFO fushsia_rust_target_os_info + +struct gcc_targetrustm targetrustm = TARGETRUSTM_INITIALIZER; diff --git a/gcc/config/linux-rust.cc b/gcc/config/linux-rust.cc new file mode 100644 index 0000000..3eaa918 --- /dev/null +++ b/gcc/config/linux-rust.cc @@ -0,0 +1,57 @@ +/* Linux support needed only by Rust front-end. + Copyright (C) 2022 Free Software Foundation, Inc. + +GCC is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation; either version 3, or (at your option) any later +version. + +GCC is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with GCC; see the file COPYING3. If not see +<http://www.gnu.org/licenses/>. */ + +#include "config.h" +#include "system.h" +#include "coretypes.h" +#include "tm.h" +#include "tm_rust.h" +#include "rust/rust-target.h" +#include "rust/rust-target-def.h" + +/* `-mandroid' is not available as an command-line option. */ +#ifndef TARGET_ANDROID +#define TARGET_ANDROID 0 +#endif + +/* Implement TARGET_RUST_OS_INFO for Linux targets. */ + +static void +linux_rust_target_os_info (void) +{ + rust_add_target_info ("target_family", "unix"); + rust_add_target_info ("target_vendor", "unknown"); + + if (TARGET_ANDROID) + rust_add_target_info ("target_os", "android"); + else + rust_add_target_info ("target_os", "linux"); + + if (OPTION_GLIBC) + rust_add_target_info ("target_env", "gnu"); + else if (OPTION_MUSL) + rust_add_target_info ("target_env", "musl"); + else if (OPTION_UCLIBC) + rust_add_target_info ("target_env", "uclibc"); + else + rust_add_target_info ("target_env", ""); +} + +#undef TARGET_RUST_OS_INFO +#define TARGET_RUST_OS_INFO linux_rust_target_os_info + +struct gcc_targetrustm targetrustm = TARGETRUSTM_INITIALIZER; diff --git a/gcc/config/netbsd-rust.cc b/gcc/config/netbsd-rust.cc new file mode 100644 index 0000000..9395466 --- /dev/null +++ b/gcc/config/netbsd-rust.cc @@ -0,0 +1,40 @@ +/* NetBSD support needed only by Rust front-end. + Copyright (C) 2022 Free Software Foundation, Inc. + +GCC is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation; either version 3, or (at your option) any later +version. + +GCC is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with GCC; see the file COPYING3. If not see +<http://www.gnu.org/licenses/>. */ + +#include "config.h" +#include "system.h" +#include "coretypes.h" +#include "tm.h" +#include "tm_rust.h" +#include "rust/rust-target.h" +#include "rust/rust-target-def.h" + +/* Implement TARGET_RUST_OS_INFO for NetBSD targets. */ + +static void +netbsd_rust_target_os_info (void) +{ + rust_add_target_info ("target_family", "unix"); + rust_add_target_info ("target_os", "netbsd"); + rust_add_target_info ("target_vendor", "unknown"); + rust_add_target_info ("target_env", ""); +} + +#undef TARGET_RUST_OS_INFO +#define TARGET_RUST_OS_INFO netbsd_rust_target_os_info + +struct gcc_targetrustm targetrustm = TARGETRUSTM_INITIALIZER; diff --git a/gcc/config/openbsd-rust.cc b/gcc/config/openbsd-rust.cc new file mode 100644 index 0000000..c4721ea --- /dev/null +++ b/gcc/config/openbsd-rust.cc @@ -0,0 +1,40 @@ +/* OpenBSD support needed only by Rust front-end. + Copyright (C) 2022 Free Software Foundation, Inc. + +GCC is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation; either version 3, or (at your option) any later +version. + +GCC is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with GCC; see the file COPYING3. If not see +<http://www.gnu.org/licenses/>. */ + +#include "config.h" +#include "system.h" +#include "coretypes.h" +#include "tm.h" +#include "tm_rust.h" +#include "rust/rust-target.h" +#include "rust/rust-target-def.h" + +/* Implement TARGET_RUST_OS_INFO for OpenBSD targets. */ + +static void +openbsd_rust_target_os_info (void) +{ + rust_add_target_info ("target_family", "unix"); + rust_add_target_info ("target_os", "openbsd"); + rust_add_target_info ("target_vendor", "unknown"); + rust_add_target_info ("target_env", ""); +} + +#undef TARGET_RUST_OS_INFO +#define TARGET_RUST_OS_INFO openbsd_rust_target_os_info + +struct gcc_targetrustm targetrustm = TARGETRUSTM_INITIALIZER; diff --git a/gcc/config/sol2-rust.cc b/gcc/config/sol2-rust.cc new file mode 100644 index 0000000..e36bd45 --- /dev/null +++ b/gcc/config/sol2-rust.cc @@ -0,0 +1,40 @@ +/* Solaris support needed only by Rust front-end. + Copyright (C) 2022 Free Software Foundation, Inc. + +GCC is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation; either version 3, or (at your option) any later +version. + +GCC is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with GCC; see the file COPYING3. If not see +<http://www.gnu.org/licenses/>. */ + +#include "config.h" +#include "system.h" +#include "coretypes.h" +#include "tm.h" +#include "tm_rust.h" +#include "rust/rust-target.h" +#include "rust/rust-target-def.h" + +/* Implement TARGET_RUST_OS_INFO for Solaris targets. */ + +static void +solaris_rust_target_os_info (void) +{ + rust_add_target_info ("target_family", "unix"); + rust_add_target_info ("target_os", "solaris"); + rust_add_target_info ("target_vendor", "sun"); + rust_add_target_info ("target_env", ""); +} + +#undef TARGET_RUST_OS_INFO +#define TARGET_RUST_OS_INFO solaris_rust_target_os_info + +struct gcc_targetrustm targetrustm = TARGETRUSTM_INITIALIZER; diff --git a/gcc/config/t-dragonfly b/gcc/config/t-dragonfly index a282579..1eee293 100644 --- a/gcc/config/t-dragonfly +++ b/gcc/config/t-dragonfly @@ -19,3 +19,7 @@ dragonfly-d.o: $(srcdir)/config/dragonfly-d.cc $(COMPILE) $< $(POSTCOMPILE) + +dragonfly-rust.o: $(srcdir)/config/dragonfly-rust.cc + $(COMPILE) $< + $(POSTCOMPILE) diff --git a/gcc/config/t-freebsd b/gcc/config/t-freebsd index 37049f7..dd5dfdd 100644 --- a/gcc/config/t-freebsd +++ b/gcc/config/t-freebsd @@ -19,3 +19,7 @@ freebsd-d.o: $(srcdir)/config/freebsd-d.cc $(COMPILE) $< $(POSTCOMPILE) + +freebsd-rust.o: $(srcdir)/config/freebsd-rust.cc + $(COMPILE) $< + $(POSTCOMPILE) diff --git a/gcc/config/t-fuchsia b/gcc/config/t-fuchsia new file mode 100644 index 0000000..55c884b --- /dev/null +++ b/gcc/config/t-fuchsia @@ -0,0 +1,21 @@ +# Copyright (C) 2022 Free Software Foundation, Inc. +# +# This file is part of GCC. +# +# GCC is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3, or (at your option) +# any later version. +# +# GCC is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with GCC; see the file COPYING3. If not see +# <http://www.gnu.org/licenses/>. + +fuchsia-rust.o: $(srcdir)/config/fuchsia-rust.cc + $(COMPILE) $< + $(POSTCOMPILE) diff --git a/gcc/config/t-linux b/gcc/config/t-linux index d9bc9b8..8404e14 100644 --- a/gcc/config/t-linux +++ b/gcc/config/t-linux @@ -19,3 +19,7 @@ linux.o: $(srcdir)/config/linux.cc $(COMPILE) $< $(POSTCOMPILE) + +linux-rust.o: $(srcdir)/config/linux-rust.cc + $(COMPILE) $< + $(POSTCOMPILE) diff --git a/gcc/config/t-netbsd b/gcc/config/t-netbsd index 77f850f..b9546da 100644 --- a/gcc/config/t-netbsd +++ b/gcc/config/t-netbsd @@ -23,3 +23,7 @@ netbsd.o: $(srcdir)/config/netbsd.cc netbsd-d.o: $(srcdir)/config/netbsd-d.cc $(COMPILE) $< $(POSTCOMPILE) + +netbsd-rust.o: $(srcdir)/config/netbsd-rust.cc + $(COMPILE) $< + $(POSTCOMPILE) diff --git a/gcc/config/t-openbsd b/gcc/config/t-openbsd index 69643f5..3b625d6 100644 --- a/gcc/config/t-openbsd +++ b/gcc/config/t-openbsd @@ -5,3 +5,8 @@ USER_H = $(EXTRA_HEADERS) openbsd-d.o: $(srcdir)/config/openbsd-d.cc $(COMPILE) $< $(POSTCOMPILE) + +# OpenBSD-specific Rust support. +openbsd-rust.o: $(srcdir)/config/openbsd-rust.cc + $(COMPILE) $< + $(POSTCOMPILE) diff --git a/gcc/config/t-sol2 b/gcc/config/t-sol2 index 8732e2e..e207534 100644 --- a/gcc/config/t-sol2 +++ b/gcc/config/t-sol2 @@ -31,6 +31,11 @@ sol2-d.o: $(srcdir)/config/sol2-d.cc $(COMPILE) $< $(POSTCOMPILE) +# Solaris-specific Rust support. +sol2-rust.o: $(srcdir)/config/sol2-rust.cc + $(COMPILE) $< + $(POSTCOMPILE) + # Corresponding stub routines. sol2-stubs.o: $(srcdir)/config/sol2-stubs.cc $(COMPILE) $< diff --git a/gcc/config/t-vxworks b/gcc/config/t-vxworks index 40c6cc4..7fb2adf 100644 --- a/gcc/config/t-vxworks +++ b/gcc/config/t-vxworks @@ -24,6 +24,10 @@ vxworks-c.o: $(srcdir)/config/vxworks-c.cc $(COMPILE) $< $(POSTCOMPILE) +vxworks-rust.o: $(srcdir)/config/vxworks-rust.cc + $(COMPILE) $< + $(POSTCOMPILE) + # We leverage $sysroot to find target system headers only, distributed # in a VxWorks (a)typical fashion with a different set of headers for # rtp vs kernel mode. We setup SYSROOT_HEADERS_SUFFIX_SPEC to handle diff --git a/gcc/config/t-winnt b/gcc/config/t-winnt index c44e9bf..3c7c953 100644 --- a/gcc/config/t-winnt +++ b/gcc/config/t-winnt @@ -20,3 +20,7 @@ winnt-c.o: config/winnt-c.cc $(CONFIG_H) $(SYSTEM_H) coretypes.h \ $(C_TARGET_H) $(C_TARGET_DEF_H) $(COMPILER) -c $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) \ $< $(OUTPUT_OPTION) + +winnt-rust.o: $(srcdir)/config/winnt-rust.cc + $(COMPILE) $< + $(POSTCOMPILE) diff --git a/gcc/config/vxworks-rust.cc b/gcc/config/vxworks-rust.cc new file mode 100644 index 0000000..76d618c --- /dev/null +++ b/gcc/config/vxworks-rust.cc @@ -0,0 +1,40 @@ +/* VxWorks support needed only by Rust front-end. + Copyright (C) 2022 Free Software Foundation, Inc. + +GCC is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation; either version 3, or (at your option) any later +version. + +GCC is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with GCC; see the file COPYING3. If not see +<http://www.gnu.org/licenses/>. */ + +#include "config.h" +#include "system.h" +#include "coretypes.h" +#include "tm.h" +#include "tm_rust.h" +#include "rust/rust-target.h" +#include "rust/rust-target-def.h" + +/* Implement TARGET_RUST_OS_INFO for VxWorks targets. */ + +static void +vxworks_rust_target_os_info (void) +{ + rust_add_target_info ("target_family", "unix"); + rust_add_target_info ("target_os", "vxworks"); + rust_add_target_info ("target_vendor", "wrs"); + rust_add_target_info ("target_env", "gnu"); +} + +#undef TARGET_RUST_OS_INFO +#define TARGET_RUST_OS_INFO vxworks_rust_target_os_info + +struct gcc_targetrustm targetrustm = TARGETRUSTM_INITIALIZER; diff --git a/gcc/config/winnt-rust.cc b/gcc/config/winnt-rust.cc new file mode 100644 index 0000000..190584d --- /dev/null +++ b/gcc/config/winnt-rust.cc @@ -0,0 +1,40 @@ +/* Windows support needed only by Rust front-end. + Copyright (C) 2022 Free Software Foundation, Inc. + +GCC is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation; either version 3, or (at your option) any later +version. + +GCC is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with GCC; see the file COPYING3. If not see +<http://www.gnu.org/licenses/>. */ + +#include "config.h" +#include "system.h" +#include "coretypes.h" +#include "tm.h" +#include "tm_rust.h" +#include "rust/rust-target.h" +#include "rust/rust-target-def.h" + +/* Implement TARGET_RUST_OS_INFO for Windows targets. */ + +static void +winnt_rust_target_os_info (void) +{ + rust_add_target_info ("target_family", "windows"); + rust_add_target_info ("target_os", "windows"); + rust_add_target_info ("target_vendor", "pc"); + rust_add_target_info ("target_env", "gnu"); +} + +#undef TARGET_RUST_OS_INFO +#define TARGET_RUST_OS_INFO winnt_rust_target_os_info + +struct gcc_targetrustm targetrustm = TARGETRUSTM_INITIALIZER; diff --git a/gcc/rust/ast/rust-ast-dump.cc b/gcc/rust/ast/rust-ast-dump.cc index 1e7a235..4817962 100644 --- a/gcc/rust/ast/rust-ast-dump.cc +++ b/gcc/rust/ast/rust-ast-dump.cc @@ -175,11 +175,27 @@ Dump::format_struct_field (StructField &field) void Dump::visit (Token &tok) -{} +{ + stream << tok.as_string (); +} void Dump::visit (DelimTokenTree &delim_tok_tree) -{} +{ + auto tokens = delim_tok_tree.to_token_stream (); + + indentation.increment (); + stream << '\n' << indentation; + + for (const auto &tok : tokens) + { + stream << ' '; + tok->accept_vis (*this); + } + + indentation.decrement (); + stream << '\n' << indentation; +} void Dump::visit (AttrInputMetaItemContainer &input) @@ -810,21 +826,44 @@ Dump::visit (Method &method) void Dump::visit (Module &module) { - emit_visibility (module.get_visibility ()); - stream << "mod " << module.get_name () << " {\n"; + // Syntax: + // mod IDENTIFIER ; + // | mod IDENTIFIER { + // InnerAttribute* + // Item* + // } - indentation.increment (); + emit_visibility (module.get_visibility ()); + stream << "mod " << module.get_name (); - for (auto &item : module.get_items ()) + if (module.get_kind () == Module::UNLOADED) { - stream << indentation; - item->accept_vis (*this); - stream << '\n'; + stream << ";\n"; } + else /* Module::LOADED */ + { + stream << " {\n"; - indentation.decrement (); + indentation.increment (); - stream << indentation << "}\n"; + for (auto &item : module.get_inner_attrs ()) + { + stream << indentation; + emit_attrib (item); + stream << '\n'; + } + + for (auto &item : module.get_items ()) + { + stream << indentation; + item->accept_vis (*this); + stream << '\n'; + } + + indentation.decrement (); + + stream << indentation << "}\n"; + } } void @@ -1285,22 +1324,116 @@ Dump::visit (ExternBlock &block) stream << "\n" << indentation << "}\n"; } -// rust-macro.h +static std::pair<char, char> +get_delimiters (DelimType delim) +{ + auto start_delim = '\0'; + auto end_delim = '\0'; + + switch (delim) + { + case PARENS: + start_delim = '('; + end_delim = ')'; + break; + case SQUARE: + start_delim = '['; + end_delim = ']'; + break; + case CURLY: + start_delim = '{'; + end_delim = '}'; + break; + } + + return {start_delim, end_delim}; +} + void Dump::visit (MacroMatchFragment &match) -{} +{ + stream << '$' << match.get_ident () << ':' + << match.get_frag_spec ().as_string (); +} void -Dump::visit (MacroMatchRepetition &match) -{} +Dump::visit (MacroMatchRepetition &repetition) +{ + stream << "$("; + + for (auto &match : repetition.get_matches ()) + { + match->accept_vis (*this); + stream << ' '; + } + + auto op_char = '\0'; + switch (repetition.get_op ()) + { + case MacroMatchRepetition::ANY: + op_char = '*'; + break; + case MacroMatchRepetition::ONE_OR_MORE: + op_char = '+'; + break; + case MacroMatchRepetition::ZERO_OR_ONE: + op_char = '?'; + break; + case MacroMatchRepetition::NONE: + break; + } + + stream << ')'; + + if (repetition.has_sep ()) + stream << repetition.get_sep ()->as_string (); + + stream << op_char; +} void Dump::visit (MacroMatcher &matcher) -{} +{ + auto delimiters = get_delimiters (matcher.get_delim_type ()); + + stream << delimiters.first; + + for (auto &match : matcher.get_matches ()) + { + match->accept_vis (*this); + stream << ' '; + } + + stream << delimiters.second; +} void Dump::visit (MacroRulesDefinition &rules_def) -{} +{ + for (auto &outer_attr : rules_def.get_outer_attrs ()) + emit_attrib (outer_attr); + + stream << "macro_rules! " << rules_def.get_rule_name () << " {\n"; + + indentation.increment (); + + for (auto &rule : rules_def.get_rules ()) + { + stream << indentation; + + rule.get_matcher ().accept_vis (*this); + + stream << " => "; + + rule.get_transcriber ().get_token_tree ().accept_vis (*this); + + stream << ";\n"; + } + + indentation.decrement (); + + stream << "}\n"; +} void Dump::visit (MacroInvocation ¯o_invoc) diff --git a/gcc/rust/ast/rust-ast-dump.h b/gcc/rust/ast/rust-ast-dump.h index 2da2736..9fe8ee9 100644 --- a/gcc/rust/ast/rust-ast-dump.h +++ b/gcc/rust/ast/rust-ast-dump.h @@ -72,7 +72,9 @@ private: std::ostream &stream; Indent indentation; - // Format together common items of functions: Parameters, return type, block + /** + * Format together common items of functions: Parameters, return type, block + */ void format_function_common (std::unique_ptr<Type> &return_type, std::unique_ptr<BlockExpr> &block); @@ -97,13 +99,19 @@ private: std::ostream &emit_indented_string (const std::string &value, const std::string &comment = ""); - // Emit formatted string for generic parameters. + /** + * Emit formatted string for generic parameters + */ void emit_generic_params (std::vector<std::unique_ptr<GenericParam>> ¶ms); - // Format a single field of a tuple. + /** + * Format a single field of a tuple + */ void format_tuple_field (TupleField &field); - // Format a single field of a struct. + /** + * Format a single field of a struct + */ void format_struct_field (StructField &field); // rust-ast.h diff --git a/gcc/rust/ast/rust-macro.h b/gcc/rust/ast/rust-macro.h index 3afd86c..a02c216 100644 --- a/gcc/rust/ast/rust-macro.h +++ b/gcc/rust/ast/rust-macro.h @@ -408,6 +408,7 @@ public: Location get_locus () const { return locus; } DelimTokenTree &get_token_tree () { return token_tree; } + const DelimTokenTree &get_token_tree () const { return token_tree; } }; // A macro rule? Matcher and transcriber pair? diff --git a/gcc/rust/hir/rust-ast-lower-base.cc b/gcc/rust/hir/rust-ast-lower-base.cc index a674617..d52d108 100644 --- a/gcc/rust/hir/rust-ast-lower-base.cc +++ b/gcc/rust/hir/rust-ast-lower-base.cc @@ -824,7 +824,7 @@ ASTLoweringBase::lower_qualifiers (const AST::FunctionQualifiers &qualifiers) } void -ASTLoweringBase::handle_outer_attributes (const HIR::Item &item) +ASTLoweringBase::handle_outer_attributes (const ItemWrapper &item) { for (const auto &attr : item.get_outer_attrs ()) { @@ -855,7 +855,7 @@ ASTLoweringBase::handle_outer_attributes (const HIR::Item &item) } void -ASTLoweringBase::handle_doc_item_attribute (const HIR::Item &item, +ASTLoweringBase::handle_doc_item_attribute (const ItemWrapper &item, const AST::Attribute &attr) { auto simple_doc_comment = attr.has_attr_input () @@ -878,7 +878,7 @@ ASTLoweringBase::handle_doc_item_attribute (const HIR::Item &item, } void -ASTLoweringBase::handle_lang_item_attribute (const HIR::Item &item, +ASTLoweringBase::handle_lang_item_attribute (const ItemWrapper &item, const AST::Attribute &attr) { auto &literal = static_cast<AST::AttrInputLiteral &> (attr.get_attr_input ()); diff --git a/gcc/rust/hir/rust-ast-lower-base.h b/gcc/rust/hir/rust-ast-lower-base.h index 68c57e0..5479e63 100644 --- a/gcc/rust/hir/rust-ast-lower-base.h +++ b/gcc/rust/hir/rust-ast-lower-base.h @@ -29,6 +29,30 @@ namespace Rust { namespace HIR { +// proxy class so we can do attribute checking on items and trait items +class ItemWrapper +{ +public: + ItemWrapper (const HIR::Item &item) + : mappings (item.get_mappings ()), locus (item.get_locus ()), + outer_attrs (item.get_outer_attrs ()) + {} + + ItemWrapper (const HIR::TraitItem &item) + : mappings (item.get_mappings ()), locus (item.get_trait_locus ()), + outer_attrs (item.get_outer_attrs ()) + {} + + const Analysis::NodeMapping &get_mappings () const { return mappings; } + Location get_locus () const { return locus; } + const AST::AttrVec &get_outer_attrs () const { return outer_attrs; } + +private: + const Analysis::NodeMapping &mappings; + Location locus; + const AST::AttrVec &outer_attrs; +}; + // base class to allow derivatives to overload as needed class ASTLoweringBase : public AST::ASTVisitor { @@ -264,12 +288,12 @@ protected: HIR::FunctionQualifiers lower_qualifiers (const AST::FunctionQualifiers &qualifiers); - void handle_outer_attributes (const HIR::Item &item); + void handle_outer_attributes (const ItemWrapper &item); - void handle_lang_item_attribute (const HIR::Item &item, + void handle_lang_item_attribute (const ItemWrapper &item, const AST::Attribute &attr); - void handle_doc_item_attribute (const HIR::Item &item, + void handle_doc_item_attribute (const ItemWrapper &item, const AST::Attribute &attr); bool is_known_attribute (const std::string &attribute_path) const; diff --git a/gcc/rust/hir/rust-ast-lower-implitem.h b/gcc/rust/hir/rust-ast-lower-implitem.h index d5ca475..4f3e813 100644 --- a/gcc/rust/hir/rust-ast-lower-implitem.h +++ b/gcc/rust/hir/rust-ast-lower-implitem.h @@ -305,16 +305,14 @@ public: if (resolver.translated != nullptr) { - // FIXME + auto id = resolver.translated->get_mappings ().get_hirid (); + auto defid = resolver.translated->get_mappings ().get_defid (); + auto locus = resolver.translated->get_trait_locus (); - // auto id = resolver.translated->get_mappings ().get_hirid (); - // auto defid = resolver.translated->get_mappings ().get_defid (); - // auto locus = resolver.translated->get_locus (); - - // resolver.handle_outer_attributes (*resolver.translated); + resolver.handle_outer_attributes (*resolver.translated); resolver.mappings->insert_hir_trait_item (resolver.translated); - // resolver.mappings->insert_location (id, locus); - // resolver.mappings->insert_defid_mapping (defid, resolver.item_cast); + resolver.mappings->insert_location (id, locus); + resolver.mappings->insert_defid_mapping (defid, resolver.translated); } return resolver.translated; diff --git a/gcc/rust/hir/tree/rust-hir-item.h b/gcc/rust/hir/tree/rust-hir-item.h index 394b04f..d84e41e 100644 --- a/gcc/rust/hir/tree/rust-hir-item.h +++ b/gcc/rust/hir/tree/rust-hir-item.h @@ -2394,6 +2394,8 @@ public: return outer_attrs; } + Location get_trait_locus () const override { return get_locus (); } + protected: // Clone function implementation as (not pure) virtual method TraitItemFunc *clone_trait_item_impl () const override @@ -2480,6 +2482,8 @@ public: return outer_attrs; } + Location get_trait_locus () const override { return get_locus (); } + protected: // Clone function implementation as (not pure) virtual method TraitItemConst *clone_trait_item_impl () const override @@ -2567,6 +2571,8 @@ public: return outer_attrs; } + Location get_trait_locus () const override { return get_locus (); } + protected: // Clone function implementation as (not pure) virtual method TraitItemType *clone_trait_item_impl () const override diff --git a/gcc/rust/hir/tree/rust-hir.h b/gcc/rust/hir/tree/rust-hir.h index 927ac06..26bf302 100644 --- a/gcc/rust/hir/tree/rust-hir.h +++ b/gcc/rust/hir/tree/rust-hir.h @@ -796,7 +796,9 @@ public: virtual const std::string trait_identifier () const = 0; - const Analysis::NodeMapping get_mappings () const { return mappings; } + const Analysis::NodeMapping &get_mappings () const { return mappings; } + + virtual Location get_trait_locus () const = 0; virtual TraitItemKind get_item_kind () const = 0; diff --git a/gcc/rust/typecheck/rust-hir-trait-resolve.cc b/gcc/rust/typecheck/rust-hir-trait-resolve.cc index 146fe26..6bd3cc1 100644 --- a/gcc/rust/typecheck/rust-hir-trait-resolve.cc +++ b/gcc/rust/typecheck/rust-hir-trait-resolve.cc @@ -91,33 +91,42 @@ TraitResolver::Lookup (HIR::TypePath &path) return resolver.lookup_path (path); } -TraitResolver::TraitResolver () - : TypeCheckBase (), resolved_trait_reference (nullptr) -{} +TraitResolver::TraitResolver () : TypeCheckBase () {} -TraitReference * -TraitResolver::resolve_path (HIR::TypePath &path) +bool +TraitResolver::resolve_path_to_trait (const HIR::TypePath &path, + HIR::Trait **resolved) const { NodeId ref; if (!resolver->lookup_resolved_type (path.get_mappings ().get_nodeid (), &ref)) { rust_error_at (path.get_locus (), "Failed to resolve path to node-id"); - return &TraitReference::error_node (); + return false; } HirId hir_node = UNKNOWN_HIRID; if (!mappings->lookup_node_to_hir (ref, &hir_node)) { rust_error_at (path.get_locus (), "Failed to resolve path to hir-id"); - return &TraitReference::error_node (); + return false; } HIR::Item *resolved_item = mappings->lookup_hir_item (hir_node); - rust_assert (resolved_item != nullptr); - resolved_item->accept_vis (*this); - rust_assert (resolved_trait_reference != nullptr); + rust_assert (resolved_item->get_item_kind () == HIR::Item::ItemKind::Trait); + *resolved = static_cast<HIR::Trait *> (resolved_item); + + return true; +} + +TraitReference * +TraitResolver::resolve_path (HIR::TypePath &path) +{ + HIR::Trait *resolved_trait_reference; + bool ok = resolve_path_to_trait (path, &resolved_trait_reference); + if (!ok) + return &TraitReference::error_node (); return resolve_trait (resolved_trait_reference); } @@ -132,6 +141,14 @@ TraitResolver::resolve_trait (HIR::Trait *trait_reference) return tref; } + DefId trait_id = trait_reference->get_mappings ().get_defid (); + if (context->trait_query_in_progress (trait_id)) + { + rust_error_at (trait_reference->get_locus (), "trait cycle detected"); + return &TraitReference::error_node (); + } + + TraitQueryGuard guard (trait_id); TyTy::BaseType *self = nullptr; std::vector<TyTy::SubstitutionParamMapping> substitutions; for (auto &generic_param : trait_reference->get_generic_params ()) @@ -192,8 +209,10 @@ TraitResolver::resolve_trait (HIR::Trait *trait_reference) HIR::TraitBound *b = static_cast<HIR::TraitBound *> (bound.get ()); - // FIXME this might be recursive we need a check for that auto predicate = get_predicate_from_bound (b->get_path ()); + if (predicate.is_error ()) + return &TraitReference::error_node (); + specified_bounds.push_back (predicate); super_traits.push_back (predicate.get ()); } @@ -237,26 +256,10 @@ TraitResolver::resolve_trait (HIR::Trait *trait_reference) TraitReference * TraitResolver::lookup_path (HIR::TypePath &path) { - NodeId ref; - if (!resolver->lookup_resolved_type (path.get_mappings ().get_nodeid (), - &ref)) - { - rust_error_at (path.get_locus (), "Failed to resolve path to node-id"); - return &TraitReference::error_node (); - } - - HirId hir_node = UNKNOWN_HIRID; - if (!mappings->lookup_node_to_hir (ref, &hir_node)) - { - rust_error_at (path.get_locus (), "Failed to resolve path to hir-id"); - return &TraitReference::error_node (); - } - - HIR::Item *resolved_item = mappings->lookup_hir_item (hir_node); - - rust_assert (resolved_item != nullptr); - resolved_item->accept_vis (*this); - rust_assert (resolved_trait_reference != nullptr); + HIR::Trait *resolved_trait_reference; + bool ok = resolve_path_to_trait (path, &resolved_trait_reference); + if (!ok) + return &TraitReference::error_node (); TraitReference *tref = &TraitReference::error_node (); if (context->lookup_trait_reference ( diff --git a/gcc/rust/typecheck/rust-hir-trait-resolve.h b/gcc/rust/typecheck/rust-hir-trait-resolve.h index c4aaf42..468f429 100644 --- a/gcc/rust/typecheck/rust-hir-trait-resolve.h +++ b/gcc/rust/typecheck/rust-hir-trait-resolve.h @@ -55,10 +55,8 @@ private: std::vector<TyTy::SubstitutionParamMapping> substitutions; }; -class TraitResolver : public TypeCheckBase, private HIR::HIRFullVisitorBase +class TraitResolver : public TypeCheckBase { - using HIR::HIRFullVisitorBase::visit; - public: static TraitReference *Resolve (HIR::TypePath &path); @@ -75,10 +73,8 @@ private: TraitReference *lookup_path (HIR::TypePath &path); - HIR::Trait *resolved_trait_reference; - -public: - void visit (HIR::Trait &trait) override { resolved_trait_reference = &trait; } + bool resolve_path_to_trait (const HIR::TypePath &path, + HIR::Trait **resolved) const; }; } // namespace Resolver diff --git a/gcc/rust/typecheck/rust-hir-type-check.h b/gcc/rust/typecheck/rust-hir-type-check.h index f85585b..de3467b 100644 --- a/gcc/rust/typecheck/rust-hir-type-check.h +++ b/gcc/rust/typecheck/rust-hir-type-check.h @@ -381,6 +381,19 @@ public: return querys_in_progress.find (id) != querys_in_progress.end (); } + void insert_trait_query (DefId id) { trait_queries_in_progress.insert (id); } + + void trait_query_completed (DefId id) + { + trait_queries_in_progress.erase (id); + } + + bool trait_query_in_progress (DefId id) const + { + return trait_queries_in_progress.find (id) + != trait_queries_in_progress.end (); + } + private: TypeCheckContext (); @@ -418,6 +431,7 @@ private: // query context lookups std::set<HirId> querys_in_progress; + std::set<DefId> trait_queries_in_progress; }; class TypeResolution @@ -426,6 +440,21 @@ public: static void Resolve (HIR::Crate &crate); }; +class TraitQueryGuard +{ +public: + TraitQueryGuard (DefId id) : id (id), ctx (*TypeCheckContext::get ()) + { + ctx.insert_trait_query (id); + } + + ~TraitQueryGuard () { ctx.trait_query_completed (id); } + +private: + DefId id; + TypeCheckContext &ctx; +}; + } // namespace Resolver } // namespace Rust diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc index 0687b2f..a3073c0 100644 --- a/gcc/rust/util/rust-hir-map.cc +++ b/gcc/rust/util/rust-hir-map.cc @@ -316,6 +316,7 @@ Mappings::insert_defid_mapping (DefId id, HIR::Item *item) rust_assert (lookup_defid (id) == nullptr); rust_assert (lookup_local_defid (crate_num, local_def_id) == nullptr); + rust_assert (lookup_trait_item_defid (id) == nullptr); defIdMappings[id] = item; insert_local_defid_mapping (crate_num, local_def_id, item); @@ -332,6 +333,29 @@ Mappings::lookup_defid (DefId id) } void +Mappings::insert_defid_mapping (DefId id, HIR::TraitItem *item) +{ + CrateNum crate_num = id.crateNum; + LocalDefId local_def_id = id.localDefId; + + rust_assert (lookup_defid (id) == nullptr); + rust_assert (lookup_local_defid (crate_num, local_def_id) == nullptr); + rust_assert (lookup_trait_item_defid (id) == nullptr); + + defIdTraitItemMappings[id] = item; +} + +HIR::TraitItem * +Mappings::lookup_trait_item_defid (DefId id) +{ + auto it = defIdTraitItemMappings.find (id); + if (it == defIdTraitItemMappings.end ()) + return nullptr; + + return it->second; +} + +void Mappings::insert_hir_item (HIR::Item *item) { auto id = item->get_mappings ().get_hirid (); diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h index 5913a02..4c7e562 100644 --- a/gcc/rust/util/rust-hir-map.h +++ b/gcc/rust/util/rust-hir-map.h @@ -104,6 +104,8 @@ public: void insert_defid_mapping (DefId id, HIR::Item *item); HIR::Item *lookup_defid (DefId id); + void insert_defid_mapping (DefId id, HIR::TraitItem *item); + HIR::TraitItem *lookup_trait_item_defid (DefId id); void insert_local_defid_mapping (CrateNum crateNum, LocalDefId id, HIR::Item *item); @@ -307,6 +309,7 @@ private: std::map<CrateNum, AST::Crate *> ast_crate_mappings; std::map<CrateNum, HIR::Crate *> hir_crate_mappings; std::map<DefId, HIR::Item *> defIdMappings; + std::map<DefId, HIR::TraitItem *> defIdTraitItemMappings; std::map<CrateNum, std::map<LocalDefId, HIR::Item *>> localDefIdMappings; std::map<HirId, HIR::Module *> hirModuleMappings; diff --git a/gcc/testsuite/rust/compile/issue-1589.rs b/gcc/testsuite/rust/compile/issue-1589.rs new file mode 100644 index 0000000..79a5866 --- /dev/null +++ b/gcc/testsuite/rust/compile/issue-1589.rs @@ -0,0 +1,5 @@ +pub trait A: B {} +// { dg-error "trait cycle detected" "" { target *-*-* } .-1 } + +pub trait B: A {} +// { dg-error "trait cycle detected" "" { target *-*-* } .-1 } |