The function to get a literal from a string missed an implementation.
It did require a conversion function to achieve it, now that callback
system has been merged this function can be easily implemented.
* libproc_macro/literal.cc (Literal__from_string): Add
implementation with call to constructor.
(Literal::make_literal): Add new constructor which calls the
callback.
* libproc_macro/literal.h: Add new constructor's
prototype.
* libproc_macro/proc_macro.cc (bridge_is_available):
Change symbol name to match convention.
* libproc_macro/registration.h: Add lit_from_str
symbol.
* libproc_macro/tokenstream.cc (TokenStream::make_tokenstream):
Change symbol name to disambiguate with literal from string.
The literal conversion code could be used for the literal_from_string
callback, this means we should move it out of the function in it's own
function. This involves a new switch, which is quite sad but we're not
yet at a performance profiling phase, there may be lower hanging fruits.
gcc/rust/ChangeLog:
* util/rust-token-converter.cc (handle_suffix): Rework function
to make it work with the new literal conversion function.
(convert_literal): Add a new function to convert to a proc macro
literal from a literal tokenptr. The function will abort if the
pointer does not point to a literal.
(convert): Add call to convert literal for every literal case.
* util/rust-token-converter.h (convert_literal): Add public
prototype.
Muhammad Mahad [Thu, 27 Jul 2023 16:36:55 +0000 (21:36 +0500)]
gccrs: New Error Code Framework
Updated ErrorCode struct to enum class to enforce proper
error codes, similiar to rustc. For converting the enum
to the respective error code, I used a map and updated
make_description & make_url function accordingly and
also removes the memory leak from the previous frame-
work. Also, added macro to safely convert the enum
number to string.
gcc/rust/ChangeLog:
* backend/rust-compile-intrinsic.cc (Intrinsics::compile):
Formatted according to enum class.
* checks/errors/rust-feature-gate.cc (FeatureGate::gate): likewise.
* checks/errors/rust-unsafe-checker.cc (check_unsafe_call): likewise.
* hir/rust-ast-lower-base.cc (struct_field_name_exists): likewise.
* resolve/rust-ast-resolve-expr.cc (ResolveExpr::visit): likewise.
* resolve/rust-ast-resolve-path.cc (ResolvePath::resolve_path):
likewise.
* resolve/rust-ast-resolve-pattern.cc (PatternDeclaration::go): likewise.
(PatternDeclaration::add_new_binding): likewise.
* resolve/rust-ast-resolve-type.cc (ResolveRelativeTypePath::go): likewise.
* resolve/rust-ast-verify-assignee.h: likewise.
* rust-diagnostics.cc: updated make_desc & url function for enum class.
* rust-diagnostics.h (struct ErrorCode): removed struct to switch to enum.
(enum class): Switched from errorcode struct to enum class.
(XSTR): Macro for converting enum to string.
(STR): macro Used by XSTR for converting to string.
(ERROR_CODE): macro used by map for check.
(TABLE_TO_MAP): macro used by map for check
* typecheck/rust-casts.cc (TypeCastRules::emit_cast_error):
Formatted according to enum class.
* typecheck/rust-hir-path-probe.h: likewise.
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): likewise.
* typecheck/rust-hir-type-check-implitem.cc (TypeCheckTopLevelExternItem::visit):
likewise.
(TypeCheckImplItemWithTrait::visit): likewise.
* typecheck/rust-hir-type-check-item.cc: likewise.
* typecheck/rust-hir-type-check-pattern.cc (TypeCheckPattern::visit): likewise.
(emit_invalid_field_error): likewise.
* typecheck/rust-hir-type-check-struct.cc (TypeCheckStructExpr::resolve): likewise.
* typecheck/rust-tyty-call.cc (emit_unexpected_argument_error): likewise.
(TypeCheckCallExpr::visit): likewise.
* typecheck/rust-tyty-subst.cc (SubstitutionRef::get_mappings_from_generic_args):
likewise.
* typecheck/rust-tyty.cc (BaseType::bounds_compatible): likewise.
Signed-off-by: Muhammad Mahad <mahadtxt@gmail.com>
Add multiple tests to prevent regressions on procedural macros errors
when one is declared outside of the crate's top level.
gcc/testsuite/ChangeLog:
* rust/compile/proc_macro_attribute_non_root_function.rs: New test.
* rust/compile/proc_macro_attribute_non_root_method.rs: New test.
* rust/compile/proc_macro_attribute_non_root_module.rs: New test.
* rust/compile/proc_macro_derive_non_root_function.rs: New test.
* rust/compile/proc_macro_derive_non_root_method.rs: New test.
* rust/compile/proc_macro_derive_non_root_module.rs: New test.
* rust/compile/proc_macro_non_root_function.rs: New test.
* rust/compile/proc_macro_non_root_method.rs: New test.
* rust/compile/proc_macro_non_root_module.rs: New test.
gccrs: Check proc_macro attributes on non root functions
Check proc_macro, proc_macro_attribute and proc_macro_derive attributes
on non root functions, emit an error when one is found.
gcc/rust/ChangeLog:
* util/rust-attributes.cc (check_proc_macro_non_root): Add
function to emit this specific error.
(AttributeChecker::visit): Modify visitor to propagate to some
containers that were not handled correctly.
Initially a typedef was used to easily get the callback function pointer
type. Since this type has changed to a simpler well defined type, this
using declaration has no more purpose.
libgrust/ChangeLog:
* libproc_macro/proc_macro.cc: Remove type alias.
* libproc_macro/registration.h: Likewise.
Add a callback from gcc to determine wether the bridge is available or
not.
gcc/rust/ChangeLog:
* expand/rust-proc-macro.cc (available): Add symbol
registration.
(load_macros_array): Likewise.
libgrust/ChangeLog:
* libproc_macro/proc_macro.cc (not_available): Add a
function to express bridge unavailability.
* libproc_macro/proc_macro.h (not_available): Likewise.
* libproc_macro/registration.h: Add symbol type.
Refactor attribute search with early return. Also fix the optional
building an object with it's default empty constructor by explicitely
using tl::null_opt.
gcc/rust/ChangeLog:
* checks/errors/privacy/rust-privacy-reporter.cc (find_proc_macro_attribute):
Refactor the function to be safer and more efficient.
Refactor proc macro specific privacy check in multiple shorter
functions.
gcc/rust/ChangeLog:
* checks/errors/privacy/rust-privacy-reporter.cc (find_proc_macro_attribute):
Add a new function to find a potential proc macro type
attribute on a given item.
(proc_macro_privacy_check): Move all proc macro privacy check in
their own function to avoid cluttering the usual privacy check.
(PrivacyReporter::go): Add call to newly created proc macro
privacy check function.
gccrs: testsuite: Add tests for pub items in proc_macros
Crates of type 'proc-macro' should not have any other pub member than
procedural macros. These new test will avoid regression on error
messages in such sitation.
gcc/testsuite/ChangeLog:
* rust/compile/proc_macro_pub_function.rs: New test.
* rust/compile/proc_macro_pub_module.rs: New test.
gccrs: testsuite: Replace dg-excess-errors with dg-error
Some test were using dg-excess-errors instead of dg-error, this is now
fixed.
gcc/testsuite/ChangeLog:
* rust/compile/proc_macro_attribute_crate_type.rs: Replace
dg-excess-errors with dg-error and fix the line so the test
can pass.
* rust/compile/proc_macro_crate_type.rs: Likewise.
* rust/compile/proc_macro_derive_crate_type.rs: Likewise.
gccrs: privacy: Add tests for private proc macro error
Add some tests to prevent regression on private procedural macros error
messages.
gcc/testsuite/ChangeLog:
* rust/compile/proc_macro_attribute_private.rs: New test.
* rust/compile/proc_macro_derive_private.rs: New test.
* rust/compile/proc_macro_private.rs: New test.
gccrs: Emit error with proc macro on non functions
An error should be emitted when a proc_macro, proc_macro_attribute or
proc_macro_derive attribute is met on any non function item. Those
attribute shall be placed only on bare functions.
gcc/rust/ChangeLog:
* util/rust-attributes.cc (is_proc_macro_type): Add function to
identify matching proc macro attribute type.
(check_proc_macro_non_function): Add function to check and emit
error.
(AttributeChecker::visit): Add cal to new check function.
Two remaining structures in the rust interface were still using raw
string pointer and length couples to communicate with the C++ library
throught extern C functions. Using FFIString instead allow us to reduce
the scope of potential errors using those raw pointers. As FFIString
encapsulate raw pointer operations there will be only one locaiton to
look after.
libgrust/ChangeLog:
* libproc_macro/rust/bridge/literal.rs: Change extern C
function argument from raw string pointer and length to
FFIString.
* libproc_macro/rust/bridge/token_stream.rs: Likewise.
gccrs: resolve: Convert identifier to a SimplePath
We wish to remove node ids from identifiers, because they do not make
that much sense and are only used for procedural macros anyway. This
means we either have to wrap those into a structure or converting them
to an existing structure that already have a node id. This commit
convert those meta word identifiers to a meta path SimplePath.
gcc/rust/ChangeLog:
* ast/rust-ast.h: Add new constructor for SimplePath from an
identifier.
* expand/rust-expand-visitor.cc (get_traits_to_derive): Add
conversion.
gccrs: resolve: Add mappings for proc macros and resolving
Add multiple mappings for procedural macro name resolution.
Procedural macros were not resolved using name resolution and mapping
but rather with some hacky path comparison.
gcc/rust/ChangeLog:
* expand/rust-macro-expand.cc (MacroExpander::import_proc_macros):
Remove function.
* expand/rust-macro-expand.h (struct MacroExpander): Remove
import_proc_macro function.
* util/rust-hir-map.cc (Mappings::insert_derive_proc_macro_def):
Add a function to insert a derive proc macro definition.
(Mappings::insert_bang_proc_macro): Remove function.
(Mappings::insert_bang_proc_macro_def): Add function to insert a
bang proc macro definition.
(Mappings::insert_attribute_proc_macro_def): Likewise with
attribute proc macros.
(Mappings::lookup_derive_proc_macro_def): Add a function to
lookup a defined derive proc macro definition.
(Mappings::lookup_bang_proc_macro): Remove function.
(Mappings::lookup_bang_proc_macro_def): Add a function to lookup
a bang proc macro definition.
(Mappings::lookup_attribute_proc_macro_def): Add a function to
lookup an attribute prod macro definition.
(Mappings::insert_derive_proc_macro_invocation): Add a function
to insert a derive proc macro invocation.
(Mappings::lookup_derive_proc_macro_invocation): Add a function
to lookup a derive proc macro invocation.
(Mappings::insert_bang_proc_macro_invocation): Add a function to
insert a bang proc macro invocation.
(Mappings::lookup_bang_proc_macro_invocation): Add a function to
lookup a bang proc macro invocation.
(Mappings::insert_attribute_proc_macro_invocation): Add a
function to insert an attribute proc macro invocation.
(Mappings::lookup_attribute_proc_macro_invocation): Add a
function to lookup an attribute proc macro invocation.
* util/rust-hir-map.h: Add different proc macro mappings
and change function prototypes.
* expand/rust-expand-visitor.cc (get_traits_to_derive): Return a
vector of SimplePath instead.
(derive_item): Accept SimplePath instead of a string.
* ast/rust-ast.h: Add common ProcMacroInvocable interface to
Identifiers and SimplePath nodes.
* ast/rust-ast.cc: Add const modifier.
* ast/rust-macro.h: Change path and identifier getters.
* ast/rust-path.h: Change return type to reference.
This commit add the ability to specify the path to an extern crate
through the -frust-extern cli option. Path given as cli argument
shall resolve to the exact extern crate location.
gcc/rust/ChangeLog:
* metadata/rust-imports.h: Make the function to load a given
file public.
* rust-session-manager.cc (Session::load_extern_crate): Change
path resolution depending on extern crate declaration in cli
arguments.
gccrs: proc macro: Add crate type regression tests
Add several tests to prevent regression with proc_macro,
proc_macro_derive and proc_macro_attribute attributes error messages.
gcc/testsuite/ChangeLog:
* rust/compile/proc_macro_attribute_crate_type.rs: New test.
* rust/compile/proc_macro_crate_type.rs: New test.
* rust/compile/proc_macro_derive_crate_type.rs: New test.
Use FFIString in Ident structure rather that a raw pointer and a
length, this will reduce the size of the code dealing with raw
pointers. Which should prevent some error.
gcc/rust/ChangeLog:
* util/rust-token-converter.cc (from_ident): Adapt code to new
constructor.
libgrust/ChangeLog:
* libproc_macro/ident.cc (Ident__new): Constructor
accepts an FFIString now.
(Ident__new_raw): Likewise.
(Ident::clone): Internal members change means clone also change.
(Ident::make_ident): Change constructor call.
(Ident::drop): Add call to FFIString::clone.
* libproc_macro/ident.h (struct Ident): Remove raw
pointer and length, add an FFIString inside instead.
(Ident__new): Change constructor.
(Ident__new_raw): Change constructor.
Add a callback registration function into the proc macro library so the
compiler can register it's own lexing/parsing functions on load.
gcc/rust/ChangeLog:
* expand/rust-proc-macro.cc (tokenstream_from_string): Add a
function that creates a tokenstream from a given string.
(load_macros_array): Add call to registration function.
libgrust/ChangeLog:
* libproc_macro/proc_macro.cc (proc_macro_register_from_str):
Add registration function.
* libproc_macro/proc_macro.h (proc_macro_register_from_str):
Add registration function prototype.
* libproc_macro/tokenstream.cc (TokenStream::make_tokenstream):
Add a new constructor from a string that uses the registered
callback.
(TokenStream__from_string): Add call to new constructor.
* libproc_macro/tokenstream.h: Add registration
declaration.
* libproc_macro/registration.h: New file.
Add a new option to crab1 cli to accept crate type. This version of the
argument only accept a single crate type. Rustc accepts a comma
separated list of crate types but this might require a litle more work
for gcc.
gcc/rust/ChangeLog:
* lang.opt: Add option
* rust-session-manager.cc: Add option registration in session
target options.
* rust-session-manager.h (struct CompileOptions): Add new getter
for proc macros instead of a boolean.
Arthur Cohen [Fri, 23 Jun 2023 14:16:34 +0000 (16:16 +0200)]
gccrs: nr2.0: Add `ForeverStack` data structure.
This data structure replaces our existing scopes and allows for multiple
name resolution passes to insert definitions or look up names at different
stages of the pipeline. The documentation goes into detail about how the
data structure works, and how the source's hierarchy is preserved
throughout the pipeline.
The class is templated to enforce specific behavior based on the namespace
the scope map targets.
gcc/rust/ChangeLog:
* resolve/rust-forever-stack.h: New file.
* resolve/rust-forever-stack.hxx: New file.
Arthur Cohen [Fri, 23 Jun 2023 14:15:29 +0000 (16:15 +0200)]
gccrs: nr2.0: Add basic Rib class
This class adds a basic Rib class for the new name resolution algorithm.
It uses `optional` and `expected` return types in order to try and
improve error handling in these new passes.
gcc/rust/ChangeLog:
* Make-lang.in: Add `rust-rib.cc` object.
* resolve/rust-rib.cc: New file.
* resolve/rust-rib.h: New file.
Co-authored-by: Matthew Jasper <mjjasper1@gmail.com>
Arthur Cohen [Sun, 9 Jul 2023 11:44:52 +0000 (13:44 +0200)]
gccrs: rust-unreachable: Add specific behavior for rust_unreachable
As discussed during the community call, rust_unreachable() should
always trigger an internal compiler error and should not offer
the behavior of __builtin_unreachable when asserts are disabled, unlike
gcc_unreachable().
Output escaped character values instead of their raw values in string.
gcc/rust/ChangeLog:
* lex/rust-token.cc (escape_special_chars): Add a function that
escape a given string.
(Token::as_string): Add call to escape function for string/char
representations.
Muhammad Mahad [Mon, 10 Jul 2023 12:26:45 +0000 (17:26 +0500)]
gccrs: [E0124] field x is already declared in struct
Refactored error message for more
than one duplicate fields.
gcc/rust/ChangeLog:
* hir/rust-ast-lower-base.cc (struct_field_name_exists):
called error function.
gcc/testsuite/ChangeLog:
* rust/compile/bad_pub_enumitems.rs: changed comment to pass test cases.
* rust/compile/dup_fields.rs: likewise.
* rust/execute/same_field_name.rs: New test.
Signed-off-by: Muhammad Mahad <mahadtxt@gmail.com>
Muhammad Mahad [Thu, 6 Jul 2023 12:08:56 +0000 (17:08 +0500)]
gccrs: [E0061] Refactored argument mismatch error function
Added Invalid number of arguments (argument mismatch)
was passed when calling a function - unexpected
number of arguments `x` expected `y` And Refactored
error into one function.
gcc/rust/ChangeLog:
* typecheck/rust-tyty-call.cc (emit_unexpected_argument_error):
Refactored invalid number of argument into one function.
(TypeCheckCallExpr::visit): called refactored function.
(TypeCheckMethodCallExpr::check): likewise.
gcc/testsuite/ChangeLog:
* rust/compile/func2.rs: updated comment to pass new test cases.
* rust/compile/tuple_struct2.rs: likewise.
* rust/compile/wrong_no_of_parameters.rs: New test.
Signed-off-by: Muhammad Mahad <mahadtxt@gmail.com>
Arthur Cohen [Tue, 27 Jun 2023 13:56:04 +0000 (15:56 +0200)]
gccrs: nr2.0: Add complex testcases
gcc/testsuite/ChangeLog:
* rust/compile/name_resolution1.rs: New test.
* rust/compile/name_resolution2.rs: New test.
* rust/compile/name_resolution3.rs: New test.
* rust/compile/name_resolution4.rs: New test.
* rust/compile/name_resolution5.rs: New test.
Arthur Cohen [Fri, 23 Jun 2023 14:37:16 +0000 (16:37 +0200)]
gccrs: optional: Import tl's optional type
This is directly adapted from https://github.com/TartanLlama/optional.
This class is tested for C++11 and on GCC 4.8, which is our target mimimum
GCC version.
This optional class provides multiple advantages over our existing
implementation of Optional<T>:
- Easier API (no static constructors)
- Better method chaining (and_then, map...)
- Harsher semantics (no assign-through on Optional<T&>)
Over the std::optional type present in C++'s standard library, this type offers:
- Optional references without going through std::reference_wrapper
- Better method chaining
For these reasons, I am in favor of importing it directly rather than
adapt C++14's std::optional type to work with C++11 and GCC 4.8.