Attribute values were used as raw string, this is error prone and
makes renaming harder. Using a constexpr instead will leverage the power
of the compiler and emit an error when an incorrect builtin attribute
value is used.
When some proc macro create tokens and later have some code referring to
those created tokens the code was missing a linemap since the pointer
was null, throwing an ICE.
gcc/rust/ChangeLog:
* expand/rust-proc-macro.cc (tokenstream_from_string): Change
linemap null pointer to the current linemap.
Since the node id already is contained into the proc macro we may omit
it from the function arguments.
gcc/rust/ChangeLog:
* util/rust-hir-map.cc (Mappings::insert_derive_proc_macro_def):
Change the function body to fetch the node id from the macro
parameter.
(Mappings::insert_bang_proc_macro_def): Likewise.
(Mappings::insert_attribute_proc_macro_def): Likewise.
* util/rust-hir-map.h: Update the function's prototype by
removing the node id from the list of arguments.
gccrs: Change proc macro definition lookup to optionals
Continue changing the api to use optionals instead of bools and double
pointers.
gcc/rust/ChangeLog:
* util/rust-hir-map.cc (Mappings::lookup_derive_proc_macro_def):
Update function to accomodate new signature.
(Mappings::lookup_bang_proc_macro_def): Likewise.
(Mappings::lookup_attribute_proc_macro_def): Likewise.
* util/rust-hir-map.h: Update function signatures.
gccrs: Use optional for proc macro invocation lookup
The old interface for those mappings was clunky at best. Now we can use
the optional structure to return a reference to the instance.
gcc/rust/ChangeLog:
* expand/rust-macro-expand.h (struct MacroExpander): Update
lookup function prototypes.
* util/rust-hir-map.cc (Mappings::lookup_derive_proc_macro_invocation):
Update lookup function according to new signature.
(Mappings::lookup_bang_proc_macro_invocation): Likewise.
(Mappings::lookup_attribute_proc_macro_invocation): Likewise.
* util/rust-hir-map.h: Update function prototypes.
This part of the code is a bit tricky as it calls multiple functions
with almost the same name and slightly different behaviors. It was even
more with a meaningless variable name.
gcc/rust/ChangeLog:
* rust-session-manager.cc (Session::load_extern_crate): Change
variable name, add temporaries and comments.
Add a templated function to insert any of the three kind of proc macro
into the resolver context.
gcc/rust/ChangeLog:
* expand/rust-proc-macro.h: Change get_trait_name to get_name in
order to be coherent with the others proc macro type name
convention.
* resolve/rust-toplevel-name-resolver-2.0.cc (insert_macros):
Add a templated funtion that inserts a proc macro into the
context and emit an error on failure.
(TopLevel::visit): Change from manual insertion to a function
call.
Change the condition with rust_unreachable to an assertion. This will
keep the code clean and concise.
Some styling issues appeared during review, this commit make the code
more readable.
Move extern crate resolving under the extern crate declaration instead
of doing it under the crate's root as extern crates are not resolved by
the top level resolver.
gcc/rust/ChangeLog:
* metadata/rust-extern-crate.cc (ExternCrate::ExternCrate):
Update definition to allow Extern crate with no content (pure
proc macros).
(ExternCrate::ok): Panic on no content.
(ExternCrate::load): Likewise.
* metadata/rust-extern-crate.h: Update prototypes.
* resolve/rust-toplevel-name-resolver-2.0.cc (TopLevel::go):
Remove macro resolution.
(TopLevel::visit): Likewise.
* resolve/rust-toplevel-name-resolver-2.0.h: Add visit prototype
for extern crate.
* rust-session-manager.cc (Session::load_extern_crate): Adapt
content depending on the loaded crate's content.
* util/rust-hir-map.cc (Mappings::lookup_derive_proc_macros):
Change return type to optional because it is way more
convenient.
(Mappings::lookup_bang_proc_macros): Likewise.
(Mappings::lookup_attribute_proc_macros): Likewise.
* util/rust-hir-map.h: Update function prototypes.
This commit moves the procedural macros loaded definition from outside
the AST to the mappings. This means most getters/setters around the
mappings had to be changed. This commit also introduces the top level
visit of those mappings instead of visiting the Crate ast members.
gcc/rust/ChangeLog:
* ast/rust-ast.h (class BangProcMacro): Move class from here to
rust-proc-macro.h. Also remove related functions.
(class AttributeProcMacro): Likewise.
(class CustomDeriveProcMacro): Likewise.
(struct Crate): Remove proc macro vector members.
* expand/rust-macro-expand.h (struct MacroExpander): Change the
type to the newly created classes.
* expand/rust-proc-macro.cc (BangProcMacro::BangProcMacro): Add
constructor implementation.
(AttributeProcMacro::AttributeProcMacro): Likewise.
(CustomDeriveProcMacro::CustomDeriveProcMacro): Likewise.
* expand/rust-proc-macro.h (class BangProcMacro): Move class to
here.
(class AttributeProcMacro): Likewise.
(class CustomDeriveProcMacro): Likewise.
* resolve/rust-toplevel-name-resolver-2.0.cc (TopLevel::go):
Change top level visitor to check mappings instead
* rust-session-manager.cc (Session::load_extern_crate):
Add back macro collection to mappings.
* util/rust-hir-map.cc (Mappings::insert_derive_proc_macros):
Update getter signature with new types.
(Mappings::insert_bang_proc_macros): Likewise.
(Mappings::insert_attribute_proc_macros): Likewise.
(Mappings::lookup_derive_proc_macros): Likewise.
(Mappings::lookup_bang_proc_macros): Likewise.
(Mappings::lookup_attribute_proc_macros): Likewise.
(Mappings::insert_derive_proc_macro_def): Likewise.
(Mappings::insert_bang_proc_macro_def): Likewise.
(Mappings::insert_attribute_proc_macro_def): Likewise.
(Mappings::lookup_derive_proc_macro_def): Likewise.
(Mappings::lookup_bang_proc_macro_def): Likewise.
(Mappings::lookup_attribute_proc_macro_def): Likewise.
(Mappings::insert_derive_proc_macro_invocation): Likewise.
(Mappings::lookup_derive_proc_macro_invocation): Likewise.
(Mappings::insert_bang_proc_macro_invocation): Likewise.
(Mappings::lookup_bang_proc_macro_invocation): Likewise.
(Mappings::insert_attribute_proc_macro_invocation): Likewise.
(Mappings::lookup_attribute_proc_macro_invocation): Likewise.
* util/rust-hir-map.h: Update function prototypes as well as map
types.
Add mechanism to discover proc macros in loaded extern crates. In the
top level resolver.
gcc/rust/ChangeLog:
* resolve/rust-toplevel-name-resolver-2.0.cc (TopLevel::go):
Visit crate's newly stored proc macros.
* rust-session-manager.cc (Session::load_extern_crate):
Store proc macros in the parsed crate instead of a local
variable to achieve mappings.
gccrs: Change early resolver visit_attributes arguments
We do not need to copy the whole vector we can simply take a reference
instead.
gcc/rust/ChangeLog:
* resolve/rust-early-name-resolver-2.0.cc (Early::visit_attributes):
Change argument to reference.
* resolve/rust-early-name-resolver-2.0.h: Update function
prototype.
When resolving proc macros it is convenient to store every macro
directly in the extern crate. These class in the ast module provide a
better abstraction over the raw ProcMacro::{CustomDerive, Bang,
Attribute} structures provided by the proc_macro library.
Derive attributes should be parsed before attempting to retrieve any
traits. This will convert the tokenstream to a list of path if this
hasn't been done previously.
gcc/rust/ChangeLog:
* ast/rust-ast.cc (Attribute::get_traits_to_derive): Convert
tokenstream to path list.
Member function is_derive was overly constrained, the attribute changes
when we parse it's meta items and it no longer contains a tokenstream
while staying a derive.
Having copy and any other constructor stuff might lead to a breakage in
the future where the node id differs due to a newly constructed
SimplePath node. This change will allow us to assert the NodeId is from
the ast and not any copy made in between.
gcc/rust/ChangeLog:
* ast/rust-ast.cc (Attribute::get_traits_to_derive): Change
return type to a vector of references.
* ast/rust-ast.h: Update constructor.
* expand/rust-expand-visitor.cc (ExpandVisitor::expand_inner_stmts):
Update function call.
Philip Herron [Mon, 4 Sep 2023 14:28:46 +0000 (15:28 +0100)]
gccrs: Fix match-expression code-gen
We were massing the match scruitinee expression as a way to access the
result of the expression. This is wrong and needs to be stored in a
temporary otherwise it will cause the code to be regnerated for each time
it is used. This is not an issue in the case where the expression is only
used once.
Fixes #1895
gcc/rust/ChangeLog:
* backend/rust-compile-expr.cc (CompileExpr::visit): use a temp for the value
gcc/testsuite/ChangeLog:
* rust/execute/torture/iter1.rs: New test.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gccrs: Rename libproc_macro to libproc_macro_internal
The name was a bit misleading since libproc_macro is a rust library that
should we should be able to link against some rust code. This cpp
library was the layer below the libproc_macro library, the "real" gcc
libproc_macro act as a translation layer for the internal library.
gcc/rust/ChangeLog:
* expand/rust-macro-expand.h: Change include directive with the
new name.
* expand/rust-proc-macro.h (RUST_PROC_MACRO_H): Likewise.
* util/rust-hir-map.h: Likewise.
* util/rust-token-converter.h: Likewise.
* util/rust-token-converter.cc: Remove useless include
directive.
* Make-lang.in: Rename library.
libgrust/ChangeLog:
* Makefile.am: Change library name.
* Makefile.in: Likewise.
* configure.ac: Likewise.
* configure: Regenerate.
* libproc_macro/Makefile.am: Moved to...
* libproc_macro_internal/Makefile.am: ...here.
* libproc_macro/Makefile.in: Moved to...
* libproc_macro_internal/Makefile.in: ...here.
* libproc_macro/bridge.h: Moved to...
* libproc_macro_internal/bridge.h: ...here.
* libproc_macro/ffistring.cc: Moved to...
* libproc_macro_internal/ffistring.cc: ...here.
* libproc_macro/ffistring.h: Moved to...
* libproc_macro_internal/ffistring.h: ...here.
* libproc_macro/group.cc: Moved to...
* libproc_macro_internal/group.cc: ...here.
* libproc_macro/group.h: Moved to...
* libproc_macro_internal/group.h: ...here.
* libproc_macro/ident.cc: Moved to...
* libproc_macro_internal/ident.cc: ...here.
* libproc_macro/ident.h: Moved to...
* libproc_macro_internal/ident.h: ...here.
* libproc_macro/literal.cc: Moved to...
* libproc_macro_internal/literal.cc: ...here.
* libproc_macro/literal.h: Moved to...
* libproc_macro_internal/literal.h: ...here.
* libproc_macro/proc_macro.cc: Moved to...
* libproc_macro_internal/proc_macro.cc: ...here.
* libproc_macro/proc_macro.h: Moved to...
* libproc_macro_internal/proc_macro.h: ...here.
* libproc_macro/punct.cc: Moved to...
* libproc_macro_internal/punct.cc: ...here.
* libproc_macro/punct.h: Moved to...
* libproc_macro_internal/punct.h: ...here.
* libproc_macro/registration.h: Moved to...
* libproc_macro_internal/registration.h: ...here.
* libproc_macro/rust/bridge.rs: Moved to...
* libproc_macro_internal/rust/bridge.rs: ...here.
* libproc_macro/rust/bridge/ffistring.rs: Moved to...
* libproc_macro_internal/rust/bridge/ffistring.rs: ...here.
* libproc_macro/rust/bridge/group.rs: Moved to...
* libproc_macro_internal/rust/bridge/group.rs: ...here.
* libproc_macro/rust/bridge/ident.rs: Moved to...
* libproc_macro_internal/rust/bridge/ident.rs: ...here.
* libproc_macro/rust/bridge/literal.rs: Moved to...
* libproc_macro_internal/rust/bridge/literal.rs: ...here.
* libproc_macro/rust/bridge/punct.rs: Moved to...
* libproc_macro_internal/rust/bridge/punct.rs: ...here.
* libproc_macro/rust/bridge/span.rs: Moved to...
* libproc_macro_internal/rust/bridge/span.rs: ...here.
* libproc_macro/rust/bridge/token_stream.rs: Moved to...
* libproc_macro_internal/rust/bridge/token_stream.rs: ...here.
* libproc_macro/rust/group.rs: Moved to...
* libproc_macro_internal/rust/group.rs: ...here.
* libproc_macro/rust/ident.rs: Moved to...
* libproc_macro_internal/rust/ident.rs: ...here.
* libproc_macro/rust/lib.rs: Moved to...
* libproc_macro_internal/rust/lib.rs: ...here.
* libproc_macro/rust/literal.rs: Moved to...
* libproc_macro_internal/rust/literal.rs: ...here.
* libproc_macro/rust/punct.rs: Moved to...
* libproc_macro_internal/rust/punct.rs: ...here.
* libproc_macro/rust/span.rs: Moved to...
* libproc_macro_internal/rust/span.rs: ...here.
* libproc_macro/rust/token_stream.rs: Moved to...
* libproc_macro_internal/rust/token_stream.rs: ...here.
* libproc_macro/span.cc: Moved to...
* libproc_macro_internal/span.cc: ...here.
* libproc_macro/span.h: Moved to...
* libproc_macro_internal/span.h: ...here.
* libproc_macro/tokenstream.cc: Moved to...
* libproc_macro_internal/tokenstream.cc: ...here.
* libproc_macro/tokenstream.h: Moved to...
* libproc_macro_internal/tokenstream.h: ...here.
* libproc_macro/tokentree.cc: Moved to...
* libproc_macro_internal/tokentree.cc: ...here.
* libproc_macro/tokentree.h: Moved to...
* libproc_macro_internal/tokentree.h: ...here.
Raiki Tamura [Fri, 18 Aug 2023 04:00:47 +0000 (13:00 +0900)]
gccrs: Add check for no_mangle attribute
gcc/rust/ChangeLog:
* lex/rust-input-source.h: Move constants from here...
* util/rust-codepoint.h (struct Codepoint): ... to here
* util/rust-attributes.cc (check_no_mangle_function): New function.
(AttributeChecker::visit): Use it.
* util/rust-unicode.cc (is_ascii_only): New function.
* util/rust-unicode.h (is_ascii_only): Likewise.
* backend/rust-mangle.cc (legacy_mangle_name): Use it.
* util/rust-punycode.cc (extract_basic_string): Likewise.
* lex/rust-lex.cc (Lexer::parse_byte_char): Likewise.
Philip Herron [Thu, 31 Aug 2023 10:33:27 +0000 (11:33 +0100)]
gccrs: Fix move_val_init
The intrinsic move_val_init was being optimized away even at -O0 because
the function looked "pure" but this adds in the attributes to enforce that
this function has side-effects to override that bad assumption by the
middle-end.
Addresses #1895
gcc/rust/ChangeLog:
* backend/rust-compile-intrinsic.cc (move_val_init_handler): mark as side-effects
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Philip Herron [Thu, 31 Aug 2023 10:32:18 +0000 (11:32 +0100)]
gccrs: Fix overflow intrinsic use before init
The overflow intrinsic returns a tuple of (value, boolean) where it value
is the operator result and boolean if it overflowed or not. The intrinsic
here did not initilize the resulting tuple and therefore was creating a use
before init error resulting in garbage results
Addresses #1895
gcc/rust/ChangeLog:
* backend/rust-compile-intrinsic.cc (op_with_overflow_inner): fix use before init
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Philip Herron [Mon, 28 Aug 2023 13:20:04 +0000 (14:20 +0100)]
gccrs: Fix bad uninit intrinsic
We were using the DECL_RESULT but this just contains the TREE_TYPE of the
retval. It was also missing taking the address of the destination for the
memset call. This changes the code to create a temp variable for the return
value and asserts the destination size is the same as the size of the
template param.
Fixes #2583
gcc/rust/ChangeLog:
* backend/rust-compile-intrinsic.cc (enter_intrinsic_block): take the locals vector
(uninit_handler): make a temp variable and use the address of it
gcc/testsuite/ChangeLog:
* rust/execute/torture/issue-2583.rs: New test.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
We need to collect the early resolver's macros error to emit them at a
later stage after further expansion in order to retrieve macros defined
by other macro invocations.
Register some mappings for macro invocations and macro definitions.
gcc/rust/ChangeLog:
* resolve/rust-early-name-resolver-2.0.cc (Early::visit):
Collect error instead of emitting it. Also add invocation
registration.
* resolve/rust-early-name-resolver-2.0.h (std::function<void):
Add type definition for collection.
* resolve/rust-toplevel-name-resolver-2.0.cc (TopLevel::visit):
Register macro rule definition in mappings.
* rust-session-manager.cc (Session::expansion): Add macro
resolve error collection.
Muhammad Mahad [Sun, 20 Aug 2023 12:32:29 +0000 (17:32 +0500)]
gccrs: diagnostics: Added non-const `rich_location *` function
Currently, gccrs using the const `rich_location &`, and
then using the cast to convert it to the functions which
was used to emit the errors, having the overloaded is
good, similiar to other frontends.
Owen Avery [Sat, 19 Aug 2023 17:13:21 +0000 (13:13 -0400)]
gccrs: Namespace related tweaks
gcc/rust/ChangeLog:
* backend/rust-tree.cc
(Rust::gt_pch_nx): Move external function declaration ...
(gt_pch_nx): ... out of Rust namespace.
* backend/rust-tree.h
(OVL_FIRST): Qualify function name.
Philip Herron [Mon, 21 Aug 2023 11:35:26 +0000 (12:35 +0100)]
gccrs: Fix compilation of types which hold onto dangling infer vars
There is a case where some generic types are holding onto inference
variable pointers directly. So this gives the backend a chance to do one
final lookup to resolve the type.
This now allows us to compile a full test case for iterators but there is
still one miscompilation in here which results in a segv on O2 and bad
result on -O0.
Addresses #1895
gcc/rust/ChangeLog:
* backend/rust-compile-type.cc (TyTyResolveCompile::visit): do a final lookup
gcc/testsuite/ChangeLog:
* rust/compile/iterators1.rs: New test.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Muhammad Mahad [Tue, 15 Aug 2023 16:29:03 +0000 (21:29 +0500)]
gccrs: [E0391] Detected type dependency cycle
This errorcode & message emits when cycle
detected when computing the super predicates
of `x` which requires computing the super
predicates of `y`, which again requires
computing the super predicates of `x`,
completing the cycle.
gcc/rust/ChangeLog:
* typecheck/rust-hir-trait-resolve.cc (TraitResolver::resolve_trait):
Updated errorcode & more userfriendly message.
gcc/testsuite/ChangeLog:
* rust/compile/issue-1589.rs: Updated comment for dejagnu.
* rust/compile/trait-cycle.rs: New relevant test.
Signed-off-by: Muhammad Mahad <mahadtxt@gmail.com>
Muhammad Mahad [Wed, 16 Aug 2023 12:40:09 +0000 (17:40 +0500)]
gccrs: [E0164] Neither tuple struct nor tuple variant used as a pattern
Checking if pattern has items, before returing solves ICE.
Added error code and rich location.
Also, fixes https://github.com/Rust-GCC/gccrs/issues/2430
gcc/rust/ChangeLog:
* ast/rust-pattern.h: No need of assertion, we are handling it.
* resolve/rust-early-name-resolver.cc (EarlyNameResolver::visit):
Added check which emits error instead of using assertion.
* typecheck/rust-hir-type-check-pattern.cc (TypeCheckPattern::visit):
Added rich location and error code.
gcc/testsuite/ChangeLog:
* rust/compile/match5.rs:
Updated comment for dejagnu.
* rust/compile/pattern-struct.rs: New test for ICE.
Signed-off-by: Muhammad Mahad <mahadtxt@gmail.com>
Muhammad Mahad [Thu, 10 Aug 2023 17:10:09 +0000 (22:10 +0500)]
gccrs: Support for rich-loc & errorcode in parser error
Added method of binding ErrorCode & rich location to
parser and expansion errors.
Fixes https://github.com/Rust-GCC/gccrs/issues/2385
gcc/rust/ChangeLog:
* rust-diagnostics.cc (va_constructor):
Added constructor for all possible cases.
(Error::Error): Updated error struct
for all possible cases.
* rust-diagnostics.h (struct Error):
Updated error struct to support error
code & rich location support.
Signed-off-by: Muhammad Mahad <mahadtxt@gmail.com>
Arthur Cohen [Thu, 3 Aug 2023 14:40:07 +0000 (16:40 +0200)]
gccrs: diagnostics: Fix mismatch between new[] and free
We cannot use `free` on a pointer allocated through `new[]`, and this
causes an ASAN failure. This fixes it by using `xcalloc` instead of
`new[]` when creating description buffers for our error codes.
gcc/rust/ChangeLog:
* rust-diagnostics.cc: Switch from new[] to xcalloc
Philip Herron [Sat, 12 Aug 2023 17:18:51 +0000 (18:18 +0100)]
gccrs: improve name mangling hash
We can endup with duplicate symbol names for different intrinsics with our
current hash setup. This adds in the mappings and extra info to improve
hash uniqueness.
Addresses #1895
gcc/rust/ChangeLog:
* backend/rust-compile-intrinsic.cc (check_for_cached_intrinsic):
simplify this cached intrinsic check
* backend/rust-mangle.cc (legacy_mangle_item): use new interface
* typecheck/rust-tyty.h: new managle helper
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
We need to solve the generic argument T from the impl block by infering the
arguments there and applying them so that when we apply the generic
argument bool we dont end up in the case of missing number of generics.
Arthur Cohen [Thu, 3 Aug 2023 15:24:14 +0000 (17:24 +0200)]
gccrs: macro-expand: Keep optional references to last_{def, invoc}
This avoids a use-after-free when reaching the recursion limit.
gcc/rust/ChangeLog:
* expand/rust-macro-expand.h: Keep optional references for last_def
and last_invoc.
* expand/rust-macro-expand.cc (MacroExpander::expand_invoc): Store
optional references.
* rust-session-manager.cc (Session::expansion): Use tl::optional's APIs
for better checking.
* rust/compile/bad_as_bool_char.rs:
Updated comment to pass test case.
* rust/compile/cast1.rs: likewise.
* rust/compile/cast4.rs: likewise.
* rust/compile/cast5.rs: likewise.
* rust/compile/all-cast.rs: New test for all error codes.
Signed-off-by: Muhammad Mahad <mahadtxt@gmail.com>