Change the constraints around macro rules declaration in order to allow
macro_rules named macro as well as tighter constraint around macro rules
definitions.
gcc/rust/ChangeLog:
* parse/rust-parse-impl.h (Parser::is_macro_rules_def): Add a function
that checks tokens given by the lexer represents an accurate macro
definition. This will reduce code duplication.
(Parser::parse_item): Replace condition with call to new checking
function.
(Parser::parse_stmt): Likewise.
* parse/rust-parse.h: Add function prototype for is_macro_rules_def.
Jakub Dupak [Tue, 3 Oct 2023 14:14:39 +0000 (16:14 +0200)]
gccrs: TyTy: TyTy improved subclass casting and checking
Associate each subclass with its kind and create cast/match+cast
methods.
gcc/rust/ChangeLog:
* typecheck/rust-tyty.cc (InferType::InferType): Use static constant for kind information.
(ErrorType::ErrorType): Use static constant for kind information.
(TupleType::TupleType): Use static constant for kind information.
(BoolType::BoolType): Use static constant for kind information.
(IntType::IntType): Use static constant for kind information.
(UintType::UintType): Use static constant for kind information.
(FloatType::FloatType): Use static constant for kind information.
(USizeType::USizeType): Use static constant for kind information.
(ISizeType::ISizeType): Use static constant for kind information.
(CharType::CharType): Use static constant for kind information.
(ReferenceType::ReferenceType): Use static constant for kind information.
(PointerType::PointerType): Use static constant for kind information.
(ParamType::ParamType): Use static constant for kind information.
(StrType::StrType): Use static constant for kind information.
(NeverType::NeverType): Use static constant for kind information.
(PlaceholderType::PlaceholderType): Use static constant for kind information.
* typecheck/rust-tyty.h: Add static kind information to all TyTy classes.
Create safe cast and check methods.
Raiki Tamura [Tue, 8 Aug 2023 08:13:29 +0000 (17:13 +0900)]
gccrs: Initial implementation of v0 mangling
gcc/rust/ChangeLog:
* backend/rust-compile-context.h: Modify declaration.
* backend/rust-mangle.cc (struct V0Path): New struct.
(v0_path): New function.
(legacy_mangle_name): Take Context as argument.
(v0_numeric_prefix): Fix type strings.
(v0_complex_type_prefix): New function.
(v0_add_integer_62): Deleted
(v0_integer_62): New function.
(v0_add_opt_integer_62): Deleted.
(v0_opt_integer_62): New function.
(v0_add_disambiguator): Deleted.
(v0_disambiguator): New function.
(v0_type_prefix): Support more types.
(v0_generic_args): New function.
(v0_add_identifier): Deleted.
(v0_identifier): New function.
(v0_type_path): New function.
(v0_function_path): New function.
(v0_scope_path): New function.
(v0_crate_path): New function.
(v0_inherent_or_trait_impl_path): New function.
(v0_mangle_item): Use v0_path.
(Mangler::mangle_item): Take Context as argument.
* backend/rust-mangle.h (class Context): Add forward declaration.
* hir/tree/rust-hir-item.h: Fix include.
gccrs: Fix ICE when emitting an error during cfg strip
When an error was emitted during the cfg strip pass by the crate loader,
it was ignored and the error state propagated until another pass
(name resolver).
gcc/rust/ChangeLog:
* rust-session-manager.cc (Session::expansion): Add early break on
error.
* util/rust-canonical-path.h: Add new segment kind for inherent impl.
* resolve/rust-ast-resolve-item.cc (ResolveItem::visit): Use it.
* resolve/rust-ast-resolve-toplevel.h: Use it.
Muhammad Mahad [Sun, 17 Sep 2023 11:58:51 +0000 (16:58 +0500)]
gccrs: [E0617] attempt for invalid type variable in variadic function
There are some certain rust types must be cast before
passing them to a variadic function, because of arcane
ABI rules dictated by the C standard. To fix the error,
cast the value to the type specified by the error message.
gcc/rust/ChangeLog:
* typecheck/rust-tyty-call.cc (TypeCheckCallExpr::visit):
Added ErrorCode & more fixit hints.
gcc/testsuite/ChangeLog:
* rust/compile/variadic.rs: Added new checks.
Signed-off-by: Muhammad Mahad <mahadtxt@gmail.com>
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>