Philip Herron [Fri, 30 Jun 2023 21:08:10 +0000 (22:08 +0100)]
gccrs: fix ICE with inserting autoderef mappings
We were using the call-expr id for the autoderef mappings but this doesn't
work when the call expression itself is part of a coercion-site so this
changes the code to use the call-expression function path expression for
the id instead which will not be duplicated again.
Addresses #2105
gcc/rust/ChangeLog:
* backend/rust-compile-expr.cc (CompileExpr::generate_possible_fn_trait_call): use fnexpr
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::resolve_fn_trait_call): likewise
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Type hint value was not propagated correctly to LiteralPattern in ast.
This was defaulted to CORETYPE_STR instead, which introduced several
bugs with systems that did require any ast collection.
gcc/rust/ChangeLog:
* ast/rust-pattern.h: Change constructor to accept new parameter
instead of defaulting on string type.
* parse/rust-parse-impl.h (Parser::parse_literal_or_range_pattern):
Propagate type hint.
(Parser::parse_pattern_no_alt): Likewise.
Muhammad Mahad [Thu, 29 Jun 2023 14:24:00 +0000 (19:24 +0500)]
gccrs: refactored field error function
Refactored field error function into one funciton
Fixes #2336
gcc/rust/ChangeLog:
* typecheck/rust-hir-type-check-pattern.cc (emit_invalid_field_error): Refactored Field
Error Function
(TypeCheckPattern::visit): called emit_invalid_field_error function
Signed-off-by: Muhammad Mahad <mahadtxt@gmail.com>
Philip Herron [Thu, 29 Jun 2023 09:13:01 +0000 (10:13 +0100)]
gccrs: fix how we handle coercions and casts of unsafe pointers
I copied swap_nonoverlapping badly from libcore which uses an unsafe
pointer case of x as *mut u8, I thought this was x as *mut T when i was
copying code form libcore this caused new infererence variables to be
introduced which actually automatically turned this swap_nonoverlapping
function to turn into a swap_nonoverlapping<u8> and lost the general
generic one in the type system context.
Rust allows for you to cast any pointer to another pointer of different
base type. The rules here are really subtle though because we need to
handle the case where we do really need to unify the types here there
are a few cases to consider but the main three are:
*u32 vs *u32 -> valid pointers match = simple coercion
*<?> vs *u8 -> inference variable unified with u8 which is valid
*T vs *u8 -> invalid coercion as the element types don't match
-> But this is a valid cast site
The code for casts try a coercion first then if that was sucsessful goes
on to perform a real coercion. Otherwise it follows the cast rules. The
bug here was that we saw the case of *T vs *u8 the try coercion used
inference variables to match T vs u8 but this will cause a cascase of
bad inference variables which we don't want when we perform the simple
coercion.
Fixes #2330
gcc/rust/ChangeLog:
* typecheck/rust-casts.cc (TypeCastRules::check): apply new argument
* typecheck/rust-coercion.cc (TypeCoercionRules::Coerce): track if this is a cast site
(TypeCoercionRules::TryCoerce): update ctor
(TypeCoercionRules::TypeCoercionRules): cleanup error handling
(TypeCoercionRules::do_coercion): likewise
(TypeCoercionRules::coerce_unsafe_ptr): only infer in try mode
(TypeCoercionRules::coerce_borrowed_pointer): cleanup
* typecheck/rust-coercion.h: update header
gcc/testsuite/ChangeLog:
* rust/compile/issue-1981.rs: Fix badly copied libcore code test
* rust/compile/issue-2330.rs: New test.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Philip Herron [Thu, 29 Jun 2023 09:13:29 +0000 (10:13 +0100)]
gccrs: Track associated type generics
This fixes an issue with our qualified type-paths if we point to a generic
associated type this needs to either use the placeholder type on the trait
reference or use the impl-type but apply the generics.
gcc/rust/ChangeLog:
* typecheck/rust-hir-trait-reference.h: update prototype
* typecheck/rust-hir-trait-resolve.cc: add generic args as an out param
* typecheck/rust-hir-type-check-type.cc (TypeCheckType::visit): use the generics
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gccrs: collector: Store CollectItem instead of TokenPtr
Store a custom type to differentiate formatting and metadata from data.
gcc/rust/ChangeLog:
* ast/rust-ast-collector.cc (TokenCollector::collect_tokens):
Filter tokens.
(TokenCollector::visit_items_joined_by_separator): Change store
call.
(TokenCollector::visit_as_line): Likewise.
(TokenCollector::visit_items_as_block): Likewise.
(TokenCollector::trailing_comma): Likewise.
(TokenCollector::visit): Likewise.
(TokenCollector::visit_closure_common): Likewise.
(TokenCollector::visit_function_common): Likewise.
* ast/rust-ast-collector.h (class CollectItem): Add class to
differentiate between formatting and raw data (tokens).
The token collector was using an external container but it was later
revealed that this whole reuse thing was not necessary.
gcc/rust/ChangeLog:
* ast/rust-ast-collector.cc (TokenCollector::TokenCollector):
Remove constructor, changing it to an implicit default
constructor.
* ast/rust-ast-collector.h: Change from container reference to
direct container.
* ast/rust-ast-dump.h: Change call to constructor.
* expand/rust-macro-expand.h (struct MacroExpander): Likewise.
gccrs: dump: Output separating space under condition
Separating space shall not be output between every tokens to make the
dump clear and easy to read.
gcc/rust/ChangeLog:
* ast/rust-ast-dump.cc (Dump::require_spacing): Add a function
to determine wether a space shall output.
* ast/rust-ast-dump.h: Add function prototype as well as
condition.
Replace ast dump visitor with call to token collector.
gcc/rust/ChangeLog:
* ast/rust-ast-collector.cc (TokenCollector::visit): Add a visit
function for visitable abstract class so we can call a dump on
any visitable item.
* ast/rust-ast-collector.h: Add new visit function prototype.
* ast/rust-ast-dump.cc (Dump::go): Add call to process function.
(Dump::visit): Remove functions.
(Dump::visit_items_joined_by_separator): Likewise.
(Dump::visit_as_line): Likewise.
(Dump::visit_items_as_lines): Likewise.
(Dump::visit_items_as_block): Likewise.
(Dump::visit_function_common): Likewise.
(get_delimiters): Likewise.
(Dump::debug): Update with call to tokencollector.
* ast/rust-ast-dump.h (RUST_AST_DUMP_H): Move include
directives.
(class Dump): Remove multiple prototypes and add generic process
function.
Raiki Tamura [Sun, 18 Jun 2023 14:25:31 +0000 (23:25 +0900)]
gccrs: Refactor lexer to handle UTF-8
gcc/rust/ChangeLog:
* lex/rust-lex.cc (is_float_digit):Change types of param to `uint32_t`
(is_x_digit):Likewise
(is_octal_digit):Likewise
(is_bin_digit):Likewise
(check_valid_float_dot_end):Likewise
(is_whitespace):Likewise
(is_non_decimal_int_literal_separator):Likewise
(is_identifier_start):Likewise
(is_identifier_continue):Likewise
(Lexer::skip_broken_string_input):Likewise
(Lexer::build_token):Remove handling BOM
(Lexer::parse_in_type_suffix):Modify use of `current_char`
(Lexer::parse_in_decimal):Likewise
(Lexer::parse_escape):Likewise
(Lexer::parse_utf8_escape):Likewise
(Lexer::parse_partial_string_continue):Likewise
(Lexer::parse_partial_hex_escape):Likewise
(Lexer::parse_partial_unicode_escape):Likewise
(Lexer::parse_byte_char):Likewise
(Lexer::parse_byte_string):Likewise
(Lexer::parse_raw_byte_string):Likewise
(Lexer::parse_raw_identifier):Likewise
(Lexer::parse_non_decimal_int_literal):Likewise
(Lexer::parse_decimal_int_or_float):Likewise
(Lexer::peek_input):Change return type to `Codepoint`
(Lexer::get_input_codepoint_length):Change to return 1
(Lexer::peek_codepoint_input):Change to be wrapper of `peek_input`
(Lexer::skip_codepoint_input):Change to be wrapper of `skip_input`
(Lexer::test_get_input_codepoint_n_length):Deleted
(Lexer::split_current_token):Deleted
(Lexer::test_peek_codepoint_input):Deleted
(Lexer::start_line):Move backwards
(assert_source_content):New helper function for selftest
(test_buffer_input_source):New helper function for selftest
(test_file_input_source):Likewise
(rust_input_source_test):New test
* lex/rust-lex.h (rust_input_source_test):New test
* rust-lang.cc (run_rust_tests):Add selftest
Philip Herron [Sun, 25 Jun 2023 20:21:36 +0000 (21:21 +0100)]
gccrs: Add method selection to operator overloading
When we do operator overloading we can get multiple candidates and we need
to use the optional arguments to filter the candidates event further. In
the bug we had:
<integer> + <usize>
Without the Add impl blocks for the primitive interger types we unify
against the rhs to figure out that the lhs should be a usize but when we
are using the impl blocks we need to use the rhs to ensure that its
possible to coerce the rhs to the expected fntype parameter to filter the
candidates.
Fixes #2304
gcc/rust/ChangeLog:
* typecheck/rust-autoderef.cc: use new selection filter
* typecheck/rust-hir-dot-operator.cc (MethodResolver::Select): new slection Filter
* typecheck/rust-hir-dot-operator.h: New select prototype
* typecheck/rust-hir-type-check-expr.cc: call select
* typecheck/rust-type-util.cc (try_coercion): new helper
* typecheck/rust-type-util.h (try_coercion): helper prototype
gcc/testsuite/ChangeLog:
* rust/compile/issue-2304.rs: New test.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Change some argument name, as well as some documentation.
gcc/rust/ChangeLog:
* ast/rust-ast.cc (BlockExpr::normalize_tail_expr): Refactor
code and warn about dangling reference.
* expand/rust-expand-visitor.cc (expand_stmt_attribute):
Document function and change argument names to make those
clearer.
The second argument could be derived from the first one, the second
argument is hence useless as the first argument alone could be kept to
access it's data.
gcc/rust/ChangeLog:
* expand/rust-expand-visitor.cc (ExpandVisitor::expand_inner_stmts):
Remove second argument.
(ExpandVisitor::visit): Remove argument from function call.
* expand/rust-expand-visitor.h: Update function prototype.
gccrs: expand: Convert last statement to tail expr
Some inner statements may be expanded to statements as well as a tail
expression, which should then be propagated to the parent tail
expression field.
gcc/rust/ChangeLog:
* expand/rust-expand-visitor.cc (ExpandVisitor::visit): Change
call to expand_inner_stmts.
(expand_tail_expr): Change argument name and try to expand the
last statement as a tail expression once all statements have
been expanded.
* expand/rust-expand-visitor.h: Change prototype to accept
parent class.
* ast/rust-expr.h: Add try_convert_last_stmt function prototype.
Tail expression may contain attribute and thus should be expanded.
gcc/rust/ChangeLog:
* ast/rust-expr.h: Add a function to take tail expr as well
as a function to set a tail expression.
* expand/rust-expand-visitor.cc (expand_tail_expr): Add tail
expression expansion function.
(ExpandVisitor::visit): Add call to tail expr expansion in for
BlockExpr.
Philip Herron [Tue, 20 Jun 2023 18:41:42 +0000 (19:41 +0100)]
gccrs: Apply generic arguments to the respective trait bounds
When we have an impl block for a generic type such as T which is a generic
type which does not 'bind' generic arguments, which means its not a type
such as an ADT or Fn which holds generic parameter mappings we need to
ensure inference variables are applied to the segment type apropriately so
that inference variables unified correctly and the higher ranked trait
bounds are as well.
Fixes: #1893
gcc/rust/ChangeLog:
* typecheck/rust-hir-type-check-item.cc (TypeCheckItem::ResolveImplBlockSelfWithInference):
arguments mappings as an out parameter and apply them to the bounds
* typecheck/rust-hir-type-check-item.h: update the prototype
* typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::resolve_segments):
apply the arguments to the segment type
gcc/testsuite/ChangeLog:
* rust/compile/issue-1893.rs: fully compile the test case
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Philip Herron [Tue, 20 Jun 2023 15:39:25 +0000 (16:39 +0100)]
gccrs: Fix generic argument tracking
When we do generic argument substitution we creating mappings of the
HIR::GenericArgs argument to the TyTy::SubstitutionParam as a pointer. So
for example when we have Generic Parameters <Self, T> and arguments <T, U>
T0: Arguments: <Self=T, T=U>
T1: Self -> replaced-with T
T2: Arguments: <T=T, T=U>
T3: T maps back to the replace Self->T
T4: Arguments <T=T, T=T>
Which is wrong but because we do a string comparison to find the argument
mapping we cant reply on the pointer to the origin parameter mapping as
the parameter will be updated resulting in bad mappings.
This patch changes the Argument mappings to track the _original_ parameter
type so that lookup's for the mappings use this symbol instead not the
updated ones during substitution.
Addresses #1893
gcc/rust/ChangeLog:
* typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::resolve_segments):
simplify lookup of the respective predicate
* typecheck/rust-tyty-subst.cc (SubstitutionArg::SubstitutionArg): track original parameter
(SubstitutionArg::operator=): update copy ctor
(SubstitutionArg::get_param_ty): use original param
(SubstitutionArg::as_string): update as_string
* typecheck/rust-tyty-subst.h: add new private field
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Philip Herron [Tue, 20 Jun 2023 11:21:02 +0000 (12:21 +0100)]
gccrs: add new method to infer impl block type
When we resolve a path segment the associated impl Self may not be a type
which 'binds' generic types. Which would be any type which does not inherit
SubstitionRef base class. There is a case where an impl block for generic
type T has bound generics on the trait bound of the trait impl block this
allows us to infer the T for this case.
Addresses #1893
gcc/rust/ChangeLog:
* typecheck/rust-hir-type-check-item.cc (TypeCheckItem::ResolveImplBlockSelfWithInference):
New helper utility to infer non concrete types and handle the case where its not a
binding generic type (which inherits SubstitutionRef)
* typecheck/rust-hir-type-check-item.h: new helper prototype
* typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::resolve_segments): use this helper
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Matthew Jasper [Thu, 8 Jun 2023 19:14:47 +0000 (20:14 +0100)]
gccrs: Parse semicolons in more cases for statement macros
gccrs: Parse statement macros as statements.
gcc/rust/ChangeLog:
* ast/rust-ast.h (MacroInvocation::add_semicolon): New method.
(Expr::to_stmt): Remove method.
* ast/rust-macro.h (MacroInvocation::add_semicolon): Add override.
(MacroInvocation::to_stmt): Remove override.
* ast/rust-stmt.h: Remove use of Expr::to_stmt.
(ExprStmt::add_semicolon): Add override.
* expand/rust-macro-expand.h (struct MacroExpander):
Add EXPR/STMT and remove BLOCK from ContextType.
* expand/rust-expand-visitor.cc (ExpandVisitor::maybe_expand_expr): Use EXPR context.
(ExpandVisitor::expand_inner_stmts): Use STMT context.
(ExpandVisitor::visitor): Remove use of BLOCK context.
* expand/rust-macro-expand.cc (parse_many): Pass enum by value.
(transcribe_on_delimiter): Remove function.
(transcribe_context): Use EXPR/STMT contexts.
(MacroExpander::parse_proc_macro_output): Use EXPR/STMT contexts.
(transcribe_many_stmts): Parse statements with semicolons.
* parse/rust-parse-impl.h (Parser::parse_stmt):
Delegate macro parsing to parse_expr_stmt, check for ! after macro_rules.
(Parser::parse_let_stmt): Work around lack of NT tokens.
(Parser::parse_expr_stmt): Handle statements at end of macro expansions.
(Parser::parse_expr_stmt): Parse macro statements/expression statements
starting with a macro.
(Parser::parse_match_expr): Don't modify flag unnecessarily.
(Parser::parse_stmt_or_expr):
Parse macro statements/expression statements starting with a macro.
(Parser::parse_path_based_stmt_or_expr): Remove method.
(Parser::parse_macro_invocation_maybe_semi): Remove method.
(Parser::parse_expr): Move code into left_denotations.
(Parser::left_denotations): New method.
(Parser::null_denotation): Split out methods for cases with and without paths.
(Parser::null_denotation_path): New method.
(Parser::null_denotation_not_path): New method.
(Parser::parse_macro_invocation_partial): Don't check for semicolon here.
* parse/rust-parse.h: Update declarations.
(struct ParseRestrictions): Additional flag.
gcc/testsuite/ChangeLog:
* rust/compile/braced_macro_arm.rs: New test.
* rust/compile/braced_macro_statements1.rs: New test.
* rust/compile/braced_macro_statements2.rs: New test.
* rust/compile/braced_macro_statements3.rs: New test.
* rust/compile/issue-2225.rs: Update test.
* rust/compile/macro53.rs: New test.
Signed-off-by: Matthew Jasper <mjjasper1@gmail.com>
Matthew Jasper [Thu, 8 Jun 2023 18:29:44 +0000 (19:29 +0100)]
gccrs: Handle tail expression normalization right before lowering to HIR.
This allows braced macros at the end of blocks to correctly expand to
zero or more statements followed by a tail expression. Parsing still
creates a tail expression for now.
gcc/rust/ChangeLog:
* ast/rust-ast.cc (BlockExpr::strip_tail_expr):
Don't normalize tail expression in this method.
(BlockExpr::normalize_tail_expr): New method that only does the normalization.
* ast/rust-expr.h: Declare new method.
* hir/rust-ast-lower-block.h: Normalize tail expressions on blocks before lowering.
Signed-off-by: Matthew Jasper <mjjasper1@gmail.com>
Raiki Tamura [Mon, 19 Jun 2023 09:06:11 +0000 (18:06 +0900)]
gccrs: tokenize Unicode identifiers
gcc/rust/ChangeLog:
* lex/rust-lex.cc (is_whitespace):add all lacked codepoints valid as whitespaces
(is_identifier_start):new function to check XID_Start and underscore
(is_identifier_continue):new function to check XID_Continue
(Lexer::build_token):tokenize Unicode identifiers
(Lexer::parse_partial_string_continue):add comments
(Lexer::parse_partial_unicode_escape):add comments
(Lexer::parse_raw_identifier):change to use `is_identifier_scontinue`
(Lexer::parse_identifier_or_keyword):change to use `is_identifier_continue`
(Lexer::parse_char_or_lifetime):change to use `is_identifier_start/continue`
(Lexer::skip_codepoint_input):do not attempt to skip input when bumping EOF
* lex/rust-lex.h:add `current_char32` field
Philip Herron [Wed, 14 Jun 2023 11:11:38 +0000 (12:11 +0100)]
gccrs: we can't check the bounds involving empty placeholder types
We use placeholders for assoicated types on traits but if we are unifying
types against a placeholder its not possible to check the bounds as the
placeholder does not have enough information yet at this point to determine
if bounds will or won't be satisfied. That check will occur when associated
types and generics are setup.
Fixes #2036
gcc/rust/ChangeLog:
* typecheck/rust-unify.cc (UnifyRules::go): dont check bounds on placeholders
gcc/testsuite/ChangeLog:
* rust/compile/issue-2036.rs: New test.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Philip Herron [Mon, 12 Jun 2023 15:07:13 +0000 (16:07 +0100)]
gccrs: ensure assoicated types are setup for concrete trait impl blocks
We have been concentrating on generic impl blocks for so long that we have
not handled the simple concrete impl block case where associated types need
to be setup when compling a function/method on a trait impl block which
uses associated types.
Addresses #2019
gcc/rust/ChangeLog:
* backend/rust-compile-item.cc (CompileItem::visit): ensure assoicated types are setup
* typecheck/rust-hir-trait-reference.h: new interface for concrete impl block
* typecheck/rust-hir-trait-resolve.cc (AssociatedImplTrait::setup_raw_associated_types):
ensure we setup assoicated types for this impl block
gcc/testsuite/ChangeLog:
* rust/compile/issue-2019-1.rs: New test.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Dave Evans [Fri, 9 Jun 2023 00:18:42 +0000 (19:18 -0500)]
gccrs: Fix ICE for reference patterns in match statements
gcc/rust/ChangeLog:
* backend/rust-compile-expr.cc (check_match_scrutinee): Add REF type to rust_assert.
(CompileExpr::visit): Add REF type for scrutinee_kind in MatchExpr visitor.
Philip Herron [Sat, 10 Jun 2023 20:07:27 +0000 (21:07 +0100)]
gccrs: introduce new types_compatable
This is an initiative to begin getting rid of the can_eq interface.
Addresses #2019
gcc/rust/ChangeLog:
* typecheck/rust-hir-type-check-implitem.cc (TypeCheckImplItemWithTrait::visit):
use new interface
* typecheck/rust-type-util.cc (types_compatable): implementation of new interface
* typecheck/rust-type-util.h (types_compatable): prototype
* typecheck/rust-unify.cc (UnifyRules::expect_placeholder):
It is allow for unification against placeholders
Expand custom attributes from procedural macros at item level.
gcc/rust/ChangeLog:
* expand/rust-expand-visitor.cc (expand_attribute): Add function
to expand a given attribute on a given item.
* expand/rust-macro-expand.h (struct MacroExpander): Change
return type to ast fragment.
Add in order trait expansion using derive macros. Remove expansion of
outer attributes.
gcc/rust/ChangeLog:
* expand/rust-expand-visitor.cc (get_traits_to_derive): Change
the function to get traits in a given attribute instead of an
attribute vector.
(ExpandVisitor::expand_struct_fields): Remove call in visitor.
(ExpandVisitor::expand_function_params): Likewise.
(ExpandVisitor::expand_closure_params): Likewise.
(ExpandVisitor::visit): Likewise.
(ExpandVisitor::expand_outer_attribute): Remove function.
(ExpandVisitor::visit_outer_attrs): Remove function.
* expand/rust-expand-visitor.h: Remove function prototypes.
(ExpandVisitor::visit_outer_attrs): Remove function.
gccrs: expand: Remove proc macro fragment from expander
Procedural macros are no longer expanded on post order but rather
expanded from their parent. Hence they are no longer stored in the
expander which means this attribute can be removed. This commit also
remove the old expansion mechanism.
gcc/rust/ChangeLog:
* expand/rust-expand-visitor.h: Remove fragment collection fro
procedural macros.
* expand/rust-macro-expand.h (struct MacroExpander): Remove
procedural macro fragment from expander.
Builtin derive already had their own code scheme, incompatible with the
proc macro pattern. This commit unifies derive macros with it.
gcc/rust/ChangeLog:
* ast/rust-ast-fragment.cc (Fragment::Fragment): Remove
overwrite member in constructor.
(Fragment::operator=): Removal of overwrite member in copy.
(Fragment::should_overwrite): Remove overwrite getter.
* ast/rust-ast-fragment.h: Remove overwrite boolean member.
* expand/rust-expand-visitor.cc (derive_item): Add a function to
derive items using the expander for now.
(builtin_derive_item): Rename from derive_item to
builtin_derive_item.
(ExpandVisitor::visit): Remove visit to derive attributes.
(ExpandVisitor::expand_derive): Remove derive attribute visitor.
(ExpandVisitor::visit_attrs_with_derive): Likewise.
* expand/rust-expand-visitor.h: Update insertion of other kind
of proc macros. We no longer have an overwrite attribute in the
fragment.
* expand/rust-macro-expand.cc (MacroExpander::parse_procmacro_output):
Return the fragment instead of inserting it.
* expand/rust-macro-expand.h (struct MacroExpander): Return
fragment.
Add a first draft for derive proc macros based on the attribute expander
code. Convert the result back to a parsable entity and parse it.
The procedural macro expander was beginning to require almost all
functionalities already provided by the macro expander, hence the merge.
gcc/rust/ChangeLog:
* ast/rust-ast-collector.h: Update enum name to match
definition.
* expand/rust-expand-visitor.cc (ExpandVisitor::expand_derive):
Add call to expander.
(ExpandVisitor::expand_outer_attribute):
Change call to macro expander.
(ExpandVisitor::expand_inner_attribute): Likewise.
* expand/rust-expand-visitor.h: Remove const attribute to match
visitor declaration. Attach result to the AST. Add condition for
item erasure.
* expand/rust-proc-macro.h: Add token collector and expansion
call. Add lexers and parsers for each proc macro type and uses
them to parse macro output.
* expand/rust-macro-expand.h (struct MacroExpander): Add
functions.
* expand/rust-proc-macro.cc (ProcMacroExpander::import_proc_macros):
Moved from here...
* expand/rust-macro-expand.cc (MacroExpander::import_proc_macros):
... to here. Unify procedural macro parsing under one function.
Add a flag to determine whether it originate from a derive
macro.
(MacroExpander::parse_procmacro_output):
Parse macro output to statements. Store an error on parsing
failure.
* Make-lang.in: Add const_TokenPtr specific lexer.
* expand/rust-proc-macro-invoc-lexer.cc: New file.
* expand/rust-proc-macro-invoc-lexer.h: New file.
* rust-session-manager.cc (Session::expansion): Remove
ProcMacroExpander declaration.
* ast/rust-ast-fragment.cc (Fragment::Fragment): Add overwrite
flag.
(Fragment::should_overwrite): Add a getter to determine whether
the fragment shall overwrite it's parent or be appended after.
* ast/rust-ast-fragment.h: Add flag to declaration.
gccrs: expand: Add prc macro expander and registration
Add containers and functions to the hir-map in order to register and
lookup all three kind of procedural macros.
Add a first draft for attribute procedural macro expansion. This
expander still lack proper error handling as well as polishing.
gcc/rust/ChangeLog:
* util/rust-hir-map.cc (Mappings::insert_bang_proc_macro):
Add a function to insert a new bang proc macro.
(Mappings::lookup_bang_proc_macro): Add a function to lookup a
bang procedural macro.
(Mappings::insert_derive_proc_macro): Add a function to insert a
derive procedural macro.
(Mappings::lookup_derive_proc_macro): Add a function to lookup a
derive procedural macro.
(Mappings::insert_attribute_proc_macro): Add a function to
insert an attribute procedural macro.
(Mappings::lookup_attribute_proc_macro): Add a function to
lookup an attribute procedural macro.
* util/rust-hir-map.h: Add function prototypes.
* expand/rust-expand-visitor.cc (ExpandVisitor::expand_outer_attribute):
Implement expansion of outer attributes.
(ExpandVisitor::expand_inner_attribute):
Add call for inner attribute expansion.
* expand/rust-expand-visitor.h:
Add new procedural macro expander attribute.
* expand/rust-proc-macro.cc (load_macros_array): Add a function
to load the proc macro array from a given shared object.
(load_macros): Add a function to retrieve procedural macro
vector from a given shared object.
(ProcMacroExpander::import_proc_macros): Add a function to load
procedural macros from a given extern crate name.
* expand/rust-proc-macro.h (RUST_PROC_MACRO_H): Add new
proc-macro file.
(class ProcMacroExpander): Add new ProcMacroExpander class.
* rust-session-manager.cc (Session::expansion): Create new macro
expander and feed it to the expand visitor.
* util/rust-attributes.cc: Add macro_export builtin attribute.
TokenStream did not copy back enough old data to the new location. This
commit also add more explicit memcpy usages in order to facilitate
change to utf-8 later.
Rename procedural macro specializations without the "payload" suffix.
Those types will be reused in several places where the old suffix might
be counterintuitive.
libgrust/ChangeLog:
* libproc_macro/proc_macro.h (struct CustomDerivePayload):
Rename from CustomDerivePayload to...
(struct CustomDerive): ... CustomDerive
(struct AttrPayload): Rename from AttrPayload to ...
(struct Attribute): ... Attribute
(struct BangPayload): Rename from BangPayload to ...
(struct Bang): ... Bang
(union ProcmacroPayload): Update union with new names.
Standard types do not guarantee anything about their ABI and are
therefore unconvenient to use as a primarily exposed container.
With such asumption this commit introduces a container.
Philip Herron [Wed, 31 May 2023 15:06:58 +0000 (16:06 +0100)]
gccrs: Respect the concrete type when resolving qualifed path types
Concrete types can resolve to assoicated impl blocks which will allow us to
resolve the path to the projection type instead of the placeholder trait
associated type which can change. The projection will be fixed and is
safer to use.
Fixes #2165 #2166
gcc/rust/ChangeLog:
* typecheck/rust-hir-trait-resolve.cc: when the bound is concrete keep the mapping
* typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::visit): add missing call
* typecheck/rust-hir-type-check-type.cc (TypeCheckType::visit): make this the same as paths
gcc/testsuite/ChangeLog:
* rust/compile/issue-2165.rs: New test.
* rust/compile/issue-2166.rs: New test.
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Arthur Cohen [Thu, 1 Jun 2023 14:23:27 +0000 (16:23 +0200)]
gccrs: derive: Add dummy builtin transcriber for builtin derive macros.
Fixes #2257.
Derive builtins do not need a real transcriber, but still need one. It
will however never be called since builtin derive macros get expanded
differently, and benefit from knowing on what kind of items they are
applied (struct, enums, unions) rather than receiving a list of tokens
like regular builtin macros.
gcc/rust/ChangeLog:
* expand/rust-macro-builtins.cc (MacroBuiltin::dummy): New function.
* expand/rust-macro-builtins.h: Declare it.
Arthur Cohen [Fri, 26 May 2023 11:37:13 +0000 (13:37 +0200)]
gccrs: derive: Add #[derive(Copy)] builtin
gcc/rust/ChangeLog:
* Make-lang.in: Add new files.
* expand/rust-derive.cc (DeriveVisitor::derive): Call into DeriveCopy.
* expand/rust-derive-copy.cc: New file.
* expand/rust-derive-copy.h: New file.
gccrs: converter: Convert back Locations from spans
Use spans to get locations of expanded tokens instead of using unknown
locations.
gcc/rust/ChangeLog:
* lex/rust-lex.h: Make build_token method public.
* lex/rust-token.h: Add a setter for a token location.
* util/rust-token-converter.cc (convert): Add the function to
convert from a Span to a Location.
(from_ident): Convert Ident span to a Location.
(from_literal): Convert Literal span to a Location.
(from_punct): Convert Punct span to a Location.
(from_group): Convert Group span to a Location.
Add Span representation in libproc_macro cpp part of the library.
Integrate spans to existing types.
gcc/rust/ChangeLog:
* util/rust-token-converter.cc (convert): Update call to
constructors with location information.
(handle_suffix): Convert token locus to a Span and use it in the
literal.
* libproc_macro/rust/bridge/group.rs: Add span member
to the Group structure.
* libproc_macro/rust/bridge/ident.rs: Likewise with the
Ident structure.
* libproc_macro/rust/bridge/literal.rs: Likewise with
the Literal structure.
* libproc_macro/rust/bridge/punct.rs: Likewise with the
Punct structure.
* libproc_macro/rust/bridge/span.rs: Add span
internals.