]> gcc.gnu.org Git - gcc.git/log
gcc.git
7 months agogccrs: Expand expressions/types correctly in more contexts.
Matthew Jasper [Sat, 24 Jun 2023 11:21:44 +0000 (12:21 +0100)]
gccrs: Expand expressions/types correctly in more contexts.

Expressions are expanded in constant/static items, match expressions
and in parentheses now. Types are expanded in enum variants.

gcc/rust/ChangeLog:

* expand/rust-expand-visitor.cc (ExpandVisitor::visit):
Expand expressions in more contexts.
* resolve/rust-early-name-resolver.cc (EarlyNameResolver::visit):
Visit enum variants.

gcc/testsuite/ChangeLog:

* rust/compile/macro54.rs: New test.
* rust/compile/macro55.rs: New test.

Signed-off-by: Matthew Jasper <mjjasper1@gmail.com>
7 months agogccrs: expand: Change names and document behavior
Pierre-Emmanuel Patry [Wed, 21 Jun 2023 13:56:26 +0000 (15:56 +0200)]
gccrs: expand: Change names and document behavior

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.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
7 months agogccrs: expand: Merge arguments into one
Pierre-Emmanuel Patry [Mon, 19 Jun 2023 12:01:13 +0000 (14:01 +0200)]
gccrs: expand: Merge arguments into one

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.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
7 months agogccrs: expand: Convert last statement to tail expr
Pierre-Emmanuel Patry [Mon, 12 Jun 2023 14:36:51 +0000 (16:36 +0200)]
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.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
7 months agogccrs: expand: Add tail expr expansion
Pierre-Emmanuel Patry [Fri, 9 Jun 2023 15:13:41 +0000 (17:13 +0200)]
gccrs: expand: Add tail expr expansion

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.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
7 months agogccrs: expand: Derive statements
Arthur Cohen [Thu, 25 May 2023 13:46:47 +0000 (15:46 +0200)]
gccrs: expand: Derive statements

Expand procedural macros on statements properly.

gcc/rust/ChangeLog:

* expand/rust-expand-visitor.cc : Change type from pointer to
reference and expand statements from statements.
* ast/rust-macro.h: Return macro kind.
* ast/rust-ast.h: Add Statement kind.
* ast/rust-item.h: Change module.
* ast/rust-stmt.h: Return kind.

Co-authored-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
7 months agogccrs: Make MatchedFragmentContainer a tree-like structure
Owen Avery [Thu, 1 Jun 2023 17:37:35 +0000 (13:37 -0400)]
gccrs: Make MatchedFragmentContainer a tree-like structure

gcc/rust/ChangeLog:

* expand/rust-macro-expand.cc
(MacroExpander::expand_decl_macro):
Use pointers/smart pointers to refer to MatchedFragmentContainer instances.
(MacroExpander::transcribe_rule): Likewise.
(MatchedFragmentContainer::get_single_fragment): New implementation.
(MatchedFragmentContainer::get_fragments): Likewise.
(MatchedFragmentContainer::add_fragment): Likewise.
(MatchedFragmentContainer::zero): Likewise.
(MatchedFragmentContainer::metavar): Likewise.
* expand/rust-macro-expand.h
(class MatchedFragmentContainer): Make abstract.
(class MatchedFragmentContainerMetaVar): New.
(class MatchedFragmentContainerRepetition): New.
(class SubstitutionScope):
Use pointers/smart pointers to refer to MatchedFragmentContainer instances.
(MacroExpander::transcribe_rule): Likewise.
* expand/rust-macro-substitute-ctx.cc
(SubstituteCtx::substitute_metavar): Likewise.
(SubstituteCtx::check_repetition_amount): Likewise.
(SubstituteCtx::substitute_repetition): Likewise.
* expand/rust-macro-substitute-ctx.h
(class SubstituteCtx): Likewise.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
7 months agogccrs: ErrorCode[E0433] Use of Undeclared Crate, Module, or Type
Muhammad Mahad [Mon, 19 Jun 2023 19:47:19 +0000 (00:47 +0500)]
gccrs: ErrorCode[E0433] Use of Undeclared Crate, Module, or Type

gcc/rust/ChangeLog:
* resolve/rust-ast-resolve-path.cc (ResolvePath::resolve_path): called rust_error_at
* resolve/rust-ast-resolve-type.cc (ResolveRelativeTypePath::go): called rust_error_at

Signed-off-by: Muhammad Mahad <mahadtxt@gmail.com>
7 months agogccrs: Apply generic arguments to the respective trait bounds
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>
7 months agogccrs: Fix generic argument tracking
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>
7 months agogccrs: add new method to infer impl block type
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>
7 months agogccrs: fix unsafe lookup at zero index
Philip Herron [Tue, 20 Jun 2023 09:39:54 +0000 (10:39 +0100)]
gccrs: fix unsafe lookup at zero index

Using lookup_predciate by DefId is the safe way to get the relevant
predicate.

Addresses #1893

gcc/rust/ChangeLog:

* typecheck/rust-hir-trait-resolve.cc: use lookup_predicate (DefID)
* typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::resolve_segments): fix formatting

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
7 months agogccrs: Parse semicolons in more cases for statement macros
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>
7 months agogccrs: Handle tail expression normalization right before lowering to HIR.
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>
7 months agogccrs: Print macro invocations in AST dump
Matthew Jasper [Sat, 3 Jun 2023 12:37:51 +0000 (13:37 +0100)]
gccrs: Print macro invocations in AST dump

gcc/rust/ChangeLog:

* ast/rust-ast-dump.cc (Dump::visit): Print macro invocations.

Signed-off-by: Matthew Jasper <mjjasper1@gmail.com>
7 months agogccrs: tokenize Unicode identifiers
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

Signed-off-by: Raiki Tamura <tamaron1203@gmail.com>
7 months agogccrs: Prevent invalid iterator dereference
Owen Avery [Sat, 17 Jun 2023 04:32:38 +0000 (00:32 -0400)]
gccrs: Prevent invalid iterator dereference

gcc/rust/ChangeLog:

* lex/rust-lex.cc
(Lexer::classify_keyword): Check if iterator is valid before dereferencing.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
7 months agogccrs: Remove direct error on macro rule count-related match failure
Owen Avery [Mon, 12 Jun 2023 20:17:09 +0000 (16:17 -0400)]
gccrs: Remove direct error on macro rule count-related match failure

gcc/rust/ChangeLog:

* expand/rust-macro-expand.cc
(MacroExpander::match_repetition): Remove redundant rust_error_at call.

gcc/testsuite/ChangeLog:

* rust/compile/macro6.rs: Remove check for direct error.
* rust/compile/macro7.rs: Likewise.
* rust/compile/macro-issue2229.rs: New test.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
7 months agogccrs: we can't check the bounds involving empty placeholder types
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>
7 months agogccrs: fixup: Remove old typecheck dump
tieway59 [Wed, 14 Jun 2023 06:22:34 +0000 (14:22 +0800)]
gccrs: fixup: Remove old typecheck dump

fixes https://github.com/Rust-GCC/gccrs/issues/2020

gcc/rust/ChangeLog:

* typecheck/rust-tycheck-dump.h: Delete this file
* rust-session-manager.cc: Remove `Session::dump_type_resolution`
and `TYPE_RESOLUTION_DUMP`
* rust-session-manager.h: Likewise

Signed-off-by: Taiwei Wu tieway59@foxmail.com
7 months agogccrs: fortify resolve_method_address to match the types
Philip Herron [Tue, 13 Jun 2023 18:14:27 +0000 (19:14 +0100)]
gccrs: fortify resolve_method_address to match the types

Fixes #2019

gcc/rust/ChangeLog:

* backend/rust-compile-base.cc (HIRCompileBase::resolve_method_address):
match the fntype to the candidate

gcc/testsuite/ChangeLog:

* rust/compile/issue-2019-2.rs: New test.
* rust/compile/issue-2019-3.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
7 months agogccrs: derive: Factor common fields inside the base visitor
Arthur Cohen [Fri, 26 May 2023 13:36:23 +0000 (15:36 +0200)]
gccrs: derive: Factor common fields inside the base visitor

gcc/rust/ChangeLog:

* expand/rust-derive.h: Store AstBuilder and location.
* expand/rust-derive.cc (DeriveVisitor::DeriveVisitor): Update constructor.
* expand/rust-derive-clone.h: Remove members now stored in `DeriveVisitor`.
* expand/rust-derive-copy.h: Likewise.
* expand/rust-derive-clone.cc (DeriveClone::DeriveClone): Update constructor.
* expand/rust-derive-copy.cc (DeriveCopy::DeriveCopy): Likewise.

7 months agogccrs: ensure assoicated types are setup for concrete trait impl blocks
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>
7 months agogccrs: Fix ICE for reference patterns in match statements
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.

gcc/testsuite/ChangeLog:

* rust/compile/issue-1813.rs: New test.

Signed-off-by: Dave Evans <dave@dmetwo.org>
7 months agogccrs: Remove obsolete error diagnostic
TieWay59 [Sat, 13 May 2023 07:49:42 +0000 (15:49 +0800)]
gccrs: Remove obsolete error diagnostic

fixes https://github.com/Rust-GCC/gccrs/issues/2064

Remove every `{ dg-error "failed to type resolve expression" }`
annotation that causes a failure when running the testsuite.

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-expr.cc: Remove obsolete error diagnostic

gcc/testsuite/ChangeLog:

* rust/compile/break1.rs: remove dg-error "failed to type resolve expression"
* rust/compile/break2.rs: Likewise
* rust/compile/cfg2.rs: Likewise
* rust/compile/continue1.rs: Likewise
* rust/compile/generics4.rs: Likewise
* rust/compile/generics6.rs: Likewise
* rust/compile/generics7.rs: Likewise
* rust/compile/issue-2029.rs: Likewise
* rust/compile/issue-2139.rs: Likewise
* rust/compile/issue-2190-1.rs: Likewise
* rust/compile/issue-925.rs: Likewise
* rust/compile/method1.rs: Likewise
* rust/compile/shadow1.rs: Likewise
* rust/compile/type-bindings1.rs: Likewise
* rust/compile/unary_negation.rs: Likewise
* rust/compile/unary_not.rs: Likewise
* rust/compile/unconstrained_type_param.rs: Likewise
* rust/compile/usize1.rs: Likewise

Signed-off-by: Taiwei Wu <tieway59@foxmail.com>
7 months agogccrs: introduce new types_compatable
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

gcc/testsuite/ChangeLog:

* rust/compile/traits2.rs: update error message
* rust/compile/traits3.rs: update error message

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
7 months agogccrs: expand: Make to_derive argument const
Pierre-Emmanuel Patry [Wed, 7 Jun 2023 10:43:07 +0000 (12:43 +0200)]
gccrs: expand: Make to_derive argument const

This string will never change and can be constified.

gcc/rust/ChangeLog:

* expand/rust-expand-visitor.cc (derive_item): Add const
attribute to the trait name argument.
* expand/rust-macro-expand.h (struct MacroExpander): Likewise.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
7 months agogccrs: expand: Allow empty derive attribute
Pierre-Emmanuel Patry [Mon, 5 Jun 2023 11:17:22 +0000 (13:17 +0200)]
gccrs: expand: Allow empty derive attribute

Changes derive macro expansion to accept empty derive attributes. This
commit also add a test to prevent future regressions.

gcc/rust/ChangeLog:

* expand/rust-expand-visitor.cc (get_traits_to_derive): Change
assertion to allow empty derive directives.

gcc/testsuite/ChangeLog:

* rust/compile/derive_empty.rs: New test.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
7 months agogccrs: expand: Expand item level attribute proc macros
Pierre-Emmanuel Patry [Mon, 5 Jun 2023 09:24:35 +0000 (11:24 +0200)]
gccrs: expand: Expand item level attribute proc macros

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.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
7 months agogccrs: expand: Add in order trait expansion
Pierre-Emmanuel Patry [Fri, 2 Jun 2023 16:26:58 +0000 (18:26 +0200)]
gccrs: expand: Add in order trait expansion

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.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
7 months agogccrs: expand: Remove proc macro fragment from expander
Pierre-Emmanuel Patry [Mon, 5 Jun 2023 13:21:15 +0000 (15:21 +0200)]
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.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
7 months agogccrs: expand: Move derive system to new one
Pierre-Emmanuel Patry [Fri, 2 Jun 2023 09:41:53 +0000 (11:41 +0200)]
gccrs: expand: Move derive system to new one

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.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
7 months agogccrs: expand: Add derive proc macro draft
Pierre-Emmanuel Patry [Sun, 21 May 2023 16:13:34 +0000 (18:13 +0200)]
gccrs: expand: Add derive proc macro draft

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.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
7 months agogccrs: expand: Add prc macro expander and registration
Pierre-Emmanuel Patry [Thu, 18 May 2023 12:05:25 +0000 (14:05 +0200)]
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.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
7 months agogccrs: ast: Fix attribute collection
Pierre-Emmanuel Patry [Fri, 26 May 2023 11:16:11 +0000 (13:16 +0200)]
gccrs: ast: Fix attribute collection

Items attribute were not correctly collected and thus could not be
expanded from a previous macro invocation.

gcc/rust/ChangeLog:

* ast/rust-ast-collector.cc (TokenCollector::visit): Fix
attribute collection.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
7 months agogccrs: libproc_macro: Fix Tokenstream growth
Pierre-Emmanuel Patry [Tue, 23 May 2023 14:45:08 +0000 (16:45 +0200)]
gccrs: libproc_macro: Fix Tokenstream growth

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.

libgrust/ChangeLog:

* libproc_macro/ffistring.cc (FFIString::make_ffistring):
Add explicit sizeof and utf-8 warning.
(FFIString::clone): Add explicit sizeof and utf-8 warning.
* libproc_macro/ident.cc (Ident::clone): Likewise.
(Ident::make_ident): Likewise.
* libproc_macro/tokenstream.cc (TokenStream::grow):
Fix vector growth.
(TokenStream__clone): Add explicit sizeof.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
7 months agogccrs: libproc_macro: Fix capacity update in tokenstream
Pierre-Emmanuel Patry [Thu, 18 May 2023 14:18:58 +0000 (16:18 +0200)]
gccrs: libproc_macro: Fix capacity update in tokenstream

The capacity was not updated on tokenstream grow. This commit also add a
new mechanism to prevent a tokenstream to grow with a zero delta capacity.

libgrust/ChangeLog:

* libproc_macro/tokenstream.cc (TokenStream::grow): Add
minimum growing capacity.
(TokenStream::push): Change condition.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
7 months agogccrs: collector: Make visitors public
Pierre-Emmanuel Patry [Thu, 18 May 2023 11:58:28 +0000 (13:58 +0200)]
gccrs: collector: Make visitors public

Make all trivial visitor functions public so we could easily call the
collection of an ast's subtree from any node.

gcc/rust/ChangeLog:

* ast/rust-ast-collector.h: Make trivial visitors public.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
7 months agogccrs: libproc_macro: Rename Procmacro subtypes
Pierre-Emmanuel Patry [Tue, 16 May 2023 12:54:11 +0000 (14:54 +0200)]
gccrs: libproc_macro: Rename Procmacro subtypes

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.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
7 months agogccrs: expand: Add procmacro loading function
Pierre-Emmanuel Patry [Mon, 15 May 2023 13:23:50 +0000 (15:23 +0200)]
gccrs: expand: Add procmacro loading function

Add a function to load a shared object procmacro and retrieve the list
of available procedural macros.

gcc/rust/ChangeLog:

* Make-lang.in: Add rust-proc-macro object to list.
* expand/rust-proc-macro.cc: New file.
* expand/rust-proc-macro.h: New file.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
7 months agogccrs: libproc_macro: Add procmacro array definition
Pierre-Emmanuel Patry [Mon, 15 May 2023 13:13:38 +0000 (15:13 +0200)]
gccrs: libproc_macro: Add procmacro array definition

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.

libgrust/ChangeLog:

* libproc_macro/proc_macro.h (struct ProcmacroArray):
Add ProcmacroArray definition.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
7 months agogccrs: attributes: Add allow_internal_unstable attribute
Pierre-Emmanuel Patry [Tue, 6 Jun 2023 16:45:36 +0000 (18:45 +0200)]
gccrs: attributes: Add allow_internal_unstable attribute

Add allow_internal_unstable to the attribute table in order for it to be
flagged as builtin.

gcc/rust/ChangeLog:

* util/rust-attributes.cc: Add attribute to the table.

7 months agogccrs: collector: Move implementation to header
Pierre-Emmanuel Patry [Tue, 6 Jun 2023 12:57:14 +0000 (14:57 +0200)]
gccrs: collector: Move implementation to header

Move implementation from cc file to header, in order to allow call from
other headers and prevent linker errors.

gcc/rust/ChangeLog:

* ast/rust-ast-collector.cc (TokenCollector::visit): Move
implementation from here...
* ast/rust-ast-collector.h: ... to here.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
7 months agogccrs: Fix non-mod-rs files' external module loading paths
Owen Avery [Sun, 28 May 2023 13:44:39 +0000 (09:44 -0400)]
gccrs: Fix non-mod-rs files' external module loading paths

gcc/rust/ChangeLog:

* Make-lang.in: Add "rust-dir-owner.o".
* ast/rust-ast.cc: Include "rust-dir-owner.h".
(Module::process_file_path):
Handle non-mod-rs external file modules properly.
* parse/rust-parse-impl.h: Include "rust-dir-owner.h".
(Parser::parse_module):
Handle non-mod-rs external file modules properly.
* util/rust-dir-owner.cc: New file.
* util/rust-dir-owner.h: New file.

gcc/testsuite/ChangeLog:

* rust/compile/test_mod.rs: Moved to...
* rust/compile/issue-1089/test_mod.rs: ...here.
* rust/compile/mod_missing_middle.rs: Fix paths.
* rust/compile/missing_middle/both_path.rs: Moved to...
* rust/compile/mod_missing_middle/missing_middle/both_path.rs: ...here.
* rust/compile/missing_middle/explicit.not.rs: Moved to...
* rust/compile/mod_missing_middle/missing_middle/explicit.not.rs: ...here.
* rust/compile/missing_middle/other.rs: Moved to...
* rust/compile/mod_missing_middle/missing_middle/explicit.not/other.rs: ...here.
* rust/compile/missing_middle/inner_path.rs: Moved to...
* rust/compile/mod_missing_middle/missing_middle/inner_path.rs: ...here.
* rust/compile/missing_middle/outer_path.rs: Moved to...
* rust/compile/mod_missing_middle/missing_middle/outer_path.rs: ...here.
* rust/compile/missing_middle/sub/mod.rs: Moved to...
* rust/compile/mod_missing_middle/missing_middle/sub/mod.rs: ...here.
* rust/compile/torture/modules/mod.rs: Moved to...
* rust/compile/torture/extern_mod1/modules/mod.rs: ...here.
* rust/execute/torture/modules/mod.rs: Moved to...
* rust/execute/torture/extern_mod4/modules/mod.rs: ...here.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
7 months agogccrs: Add error message when derive macro is invoked
Owen Avery [Mon, 5 Jun 2023 14:41:43 +0000 (10:41 -0400)]
gccrs: Add error message when derive macro is invoked

gcc/rust/ChangeLog:

* expand/rust-macro-builtins.cc: Fix comments.
(MacroBuiltin::proc_macro_builtin): Add error message.
* expand/rust-macro-builtins.h: Fix comments.

gcc/testsuite/ChangeLog:

* rust/compile/derive_macro8.rs: New test.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
7 months agogccrs: Respect the concrete type when resolving qualifed path types
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>
7 months agogccrs: Prevent initialization related warnings/errors for Optional
Owen Avery [Mon, 5 Jun 2023 19:54:08 +0000 (15:54 -0400)]
gccrs: Prevent initialization related warnings/errors for Optional

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80635#c53

gcc/rust/ChangeLog:

* util/rust-optional.h
(class Optional): Add volatile field to union.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
7 months agogccrs: Handle crate metavariable
Owen Avery [Sat, 3 Jun 2023 23:57:33 +0000 (19:57 -0400)]
gccrs: Handle crate metavariable

gcc/rust/ChangeLog:

* expand/rust-macro-substitute-ctx.cc
(SubstituteCtx::substitute_metavar): Substitute crate metavar.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
7 months agogccrs: Allow parsing a borrow from struct expression
Owen Avery [Mon, 5 Jun 2023 20:48:55 +0000 (16:48 -0400)]
gccrs: Allow parsing a borrow from struct expression

gcc/rust/ChangeLog:

* parse/rust-parse-impl.h
(Parser::null_denotation): Allow struct expression referencing.

gcc/testsuite/ChangeLog:

* rust/compile/struct-expr-parse.rs: New test.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
7 months agogccrs: ast: dump literals correctly
Charalampos Mitrodimas [Sat, 3 Jun 2023 18:09:26 +0000 (18:09 +0000)]
gccrs: ast: dump literals correctly

This commit fixes printing of literals based on their
type.

Previous implementation printed literals the same, regardless
their type. Now we are printing:
  * int, float, bool don't require special printing
  * char        -> '<char>'
  * string      -> "<string>"
  * byte        -> b'<byte>'
  * byte_string -> b"<byte_string>"

gcc/rust/ChangeLog:

* ast/rust-ast-dump.cc (Dump::visit):
print literals based on their type.

Signed-off-by: Charalampos Mitrodimas <charmitro@gmail.com>
7 months agogccrs: derive: Add dummy builtin transcriber for builtin derive macros.
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.

gcc/testsuite/ChangeLog:

* rust/compile/derive_macro7.rs: New test.

7 months agogccrs: derive: Add proper derive(Clone) for unions
Arthur Cohen [Fri, 12 May 2023 13:14:49 +0000 (15:14 +0200)]
gccrs: derive: Add proper derive(Clone) for unions

gcc/rust/ChangeLog:

* ast/rust-ast-builder.cc (AstBuilder::struct_expr_struct): New function.
(AstBuilder::let): Likewise.
(AstBuilder::struct_expr): Likewise.
(AstBuilder::struct_expr_field): Likewise.
(AstBuilder::field_access): Likewise.
(AstBuilder::wildcard): Likewise.
* ast/rust-ast-builder.h: Likewise.
* expand/rust-derive-clone.cc (DeriveClone::visit_union): Implement
properly.

gcc/testsuite/ChangeLog:

* rust/compile/derive_macro4.rs: New test.
* rust/compile/derive_macro6.rs: New test.

7 months agogccrs: derive: Add #[derive(Copy)] builtin
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.

7 months agogccrs: derive: Add #[derive(Clone)] for regular structs
Arthur Cohen [Wed, 17 May 2023 10:17:58 +0000 (12:17 +0200)]
gccrs: derive: Add #[derive(Clone)] for regular structs

gcc/rust/ChangeLog:

* expand/rust-derive-clone.cc (DeriveClone::visit_struct): Implement proper
cloning for structs with fields.
* ast/rust-ast-builder.cc (AstBuilder::struct_expr): New function.
(AstBuilder::struct_expr_field): Likewise.
(AstBuilder::field_access): Likewise.
(AstBuilder::let): Likewise.
* ast/rust-ast-builder.h: Declare new functions.

gcc/testsuite/ChangeLog:

* rust/execute/torture/derive_macro4.rs: New test.

7 months agogccrs: converter: Refactor locations
Pierre-Emmanuel Patry [Wed, 31 May 2023 11:08:10 +0000 (13:08 +0200)]
gccrs: converter: Refactor locations

Refactor location to make code smaller and cleaner.

gcc/rust/ChangeLog:

* util/rust-token-converter.cc (convert): Use a variable instead
of multiple nested calls.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
7 months agogccrs: converter: Convert back Locations from spans
Pierre-Emmanuel Patry [Wed, 31 May 2023 10:52:16 +0000 (12:52 +0200)]
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.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
7 months agogccrs: converter: Add conversion from locations to spans
Pierre-Emmanuel Patry [Wed, 31 May 2023 09:17:56 +0000 (11:17 +0200)]
gccrs: converter: Add conversion from locations to spans

Add function implementation to convert either a Location or a
RicheLocation to a libproc_macro span.

gcc/rust/ChangeLog:

* util/rust-token-converter.cc (convert): Add implementation.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
7 months agogccrs: libproc_macro: Add cpp Span representation
Pierre-Emmanuel Patry [Tue, 30 May 2023 08:57:01 +0000 (10:57 +0200)]
gccrs: libproc_macro: Add cpp Span representation

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.

libgrust/ChangeLog:

* libproc_macro/Makefile.am: Add span.cc
* libproc_macro/Makefile.in: Regenerate.
* libproc_macro/span.cc: New file.
* libproc_macro/span.h: New file.
* libproc_macro/group.cc (Group::make_group): Add span
argument.
* libproc_macro/group.h (GROUP_H): Add include
directive for spans.
* libproc_macro/ident.cc (Ident__new): Add span
argument.
(Ident__new_raw): Likewise.
(Ident::make_ident): Likewise.
* libproc_macro/ident.h (Ident__new): Likewise.
(Ident__new_raw): Likewise.
* libproc_macro/literal.cc (Literal::clone): Clone the
span.
(Literal::make_literal): Add span argument.
(Literal::make_u8): Likewise.
(Literal::make_u16): Likewise.
(Literal::make_u32): Likewise.
(Literal::make_u64): Likewise.
(Literal::make_i8): Likewise.
(Literal::make_i16): Likewise.
(Literal::make_i32): Likewise.
(Literal::make_i64): Likewise.
(Literal::make_string): Likewise.
(Literal::make_byte_string): Likewise.
(Literal::make_f32): Likewise.
(Literal::make_f64): Likewise.
(Literal::make_char): Likewise.
(Literal::make_usize): Likewise.
(Literal::make_isize): Likewise.
* libproc_macro/literal.h (struct Literal): Add span to
Literal structure.
* libproc_macro/punct.cc (Punct::make_punct): Add span
argument to punct constructor.
* libproc_macro/punct.h (struct Punct): Add span to
Punct structure.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
7 months agogccrs: libproc_macro: Add Span definition
Pierre-Emmanuel Patry [Fri, 26 May 2023 16:03:28 +0000 (18:03 +0200)]
gccrs: libproc_macro: Add Span definition

Add Span type definition in the rust interface.

libgrust/ChangeLog:

* 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.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
7 months agogccrs: Properly match delimiters
Owen Avery [Tue, 30 May 2023 20:24:08 +0000 (16:24 -0400)]
gccrs: Properly match delimiters

gcc/rust/ChangeLog:

* expand/rust-macro-expand.cc
(MacroExpander::try_match_rule): Don't match delimiters for root matcher.
(MacroExpander::match_matcher): Add option to match delimiters.
* expand/rust-macro-expand.h
(MacroExpander::match_matcher): Likewise.
* parse/rust-parse-impl.h
(Parser::skip_token): Add zero argument method.
* parse/rust-parse.h:
(Parser::skip_token): Likewise.

gcc/testsuite/ChangeLog:

* rust/compile/macro-delim.rs: New test.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
7 months agogccrs: get rid of useless helper class and fix header usage
Philip Herron [Thu, 1 Jun 2023 11:51:18 +0000 (12:51 +0100)]
gccrs: get rid of useless helper class and fix header usage

gcc/rust/ChangeLog:

* Make-lang.in: remove files
* backend/rust-compile-block.h: fix headers
* backend/rust-compile-expr.h: likewise
* backend/rust-compile-fnparam.h: likewise
* backend/rust-compile-item.h: likewise
* backend/rust-compile-pattern.h: likewise
* backend/rust-compile-resolve-path.h: likewise
* backend/rust-compile-stmt.h: likewise
* typecheck/rust-autoderef.cc: likewise
* typecheck/rust-hir-path-probe.cc: likewise
* typecheck/rust-hir-path-probe.h: likewise
* typecheck/rust-hir-trait-reference.h: likewise
* typecheck/rust-hir-trait-resolve.cc: use a for loop instead
* typecheck/rust-hir-trait-resolve.h: fix headers
* typecheck/rust-hir-type-check-expr.h: likewise
* typecheck/rust-hir-type-check-implitem.h: likewise
* typecheck/rust-hir-type-check-item.h: likewise
* typecheck/rust-hir-type-check-path.cc: likewise
* typecheck/rust-hir-type-check-pattern.h: likewise
* typecheck/rust-hir-type-check-stmt.h: likewise
* typecheck/rust-hir-type-check-type.h: likewise
* typecheck/rust-hir-type-check-util.cc: Removed.
* typecheck/rust-hir-type-check-util.h: Removed.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
7 months agogccrs: expansion: Add base for deriving builtin macros (Clone, Copy...)
Arthur Cohen [Thu, 25 May 2023 12:18:39 +0000 (14:18 +0200)]
gccrs: expansion: Add base for deriving builtin macros (Clone, Copy...)

gcc/rust/ChangeLog:

* Make-lang.in: Add new object files to Makefile.
* expand/rust-expand-visitor.cc (is_derive): Move function.
(is_builtin): Likewise.
(get_traits_to_derive): New function.
(derive_item): Likewise.
(ExpandVisitor::visit): Visit item/statements containers.
(ExpandVisitor::visit_outer_attrs): Take a reference on the attribute
instead of a copy.
(ExpandVisitor::is_derive): Deleted function.
(ExpandVisitor::is_builtin): Likewise.
* expand/rust-expand-visitor.h (RUST_EXPAND_VISITOR_H): Add missing #ifdef
guards.
(is_derive): Declare function.
(is_builtin): Likewise.
* expand/rust-macro-builtins.cc (builtin_macro_from_string): Use new
MacroBuiltin::builtins map.
(make_macro_path_str): Likewise.
* expand/rust-macro-builtins.h (enum class): Add builtin derive macros.
* expand/rust-derive-clone.cc: New file.
* expand/rust-derive-clone.h: New file.
* expand/rust-derive.cc: New file.
* expand/rust-derive.h: New file.
* util/rust-hir-map.cc (Mappings::insert_macro_def): Fix logic for
setting builtin macro transcribers.

gcc/testsuite/ChangeLog:

* rust/compile/macro43.rs: Fix test with new derive macros.
* rust/compile/derive_macro1.rs: New test.
* rust/compile/derive_macro3.rs: New test.
* rust/execute/torture/derive_macro1.rs: New test.
* rust/execute/torture/derive_macro3.rs: New test.

7 months agogccrs: ast: Add AstBuilder class.
Arthur Cohen [Thu, 25 May 2023 12:18:47 +0000 (14:18 +0200)]
gccrs: ast: Add AstBuilder class.

gcc/rust/ChangeLog:

* ast/rust-ast-builder.cc: New file.
* ast/rust-ast-builder.h: New file.

7 months agogccrs: expand: Add missing copyright headers
Arthur Cohen [Wed, 31 May 2023 13:12:14 +0000 (15:12 +0200)]
gccrs: expand: Add missing copyright headers

Fixes #2250

gcc/rust/ChangeLog:

* expand/rust-macro-invoc-lexer.cc: Add missing copyright header.
* expand/rust-macro-substitute-ctx.cc: Likewise.

7 months agogccrs: Make key and key/value config options seperate
Owen Avery [Fri, 19 May 2023 04:05:19 +0000 (00:05 -0400)]
gccrs: Make key and key/value config options seperate

gcc/rust/ChangeLog:

* rust-session-manager.h:
Include "rust-optional.h".
(struct TargetOptions):
Store values in config key/value pairs as Optional<std::string> rather than std::string.
* rust-session-manager.cc
(TargetOptions::dump_target_options):
Handle Optional<std::string> values.

gcc/testsuite/ChangeLog:

* rust/compile/cfg6.rs: New test.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
7 months agogccrs: Fix cast site to not miss type-unifications
Philip Herron [Tue, 30 May 2023 16:16:30 +0000 (17:16 +0100)]
gccrs: Fix cast site to not miss type-unifications

When attempting casts we can try a type coercion first, this is a
TryCoercion which will return a result. In the event this is ok we need to
perform a true coercion so that we don't leave missing infeence variable's
ununified.

Fixes #2195

gcc/rust/ChangeLog:

* typecheck/rust-casts.cc (TypeCastRules::check): do coercion
(TypeCastRules::emit_cast_error): clang-format

gcc/testsuite/ChangeLog:

* rust/compile/issue-2195.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
7 months agogccrs: Add missing name resolution to item statements
Philip Herron [Tue, 30 May 2023 12:45:58 +0000 (13:45 +0100)]
gccrs: Add missing name resolution to item statements

This fixes the issue but there are two cleanups to do at some point.

  1. misc namesapce this is a scope AST namespace where we dump resolution
     info when its not defined here. This occurs in the case such as nested
     scopes where the nested scope is popped and we hit an assertion.
     Outside of name resolution this requirement shouldnt really apply
     it should be permissive to allow for this

  2. We reuse our existing name resolution pieces here for Traits and impl
     blocks we should start doing this for the other statements.

Fixes #2238

gcc/rust/ChangeLog:

* resolve/rust-ast-resolve-stmt.cc (ResolveStmt::visit): add name resolution
* resolve/rust-ast-resolve-stmt.h: likewise
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): insert resolved node

gcc/testsuite/ChangeLog:

* rust/compile/issue-2238.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
7 months agogccrs: remove unused header
Philip Herron [Tue, 30 May 2023 12:45:36 +0000 (13:45 +0100)]
gccrs: remove unused header

gcc/rust/ChangeLog:

* resolve/rust-ast-resolve-pattern.cc: remove unused

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
7 months agogccrs: Fix missing pop of name scope at the end of Trait Impl
Philip Herron [Tue, 30 May 2023 12:45:14 +0000 (13:45 +0100)]
gccrs: Fix missing pop of name scope at the end of Trait Impl

This adds the missing label scope and fixes the missing pop of the name
scope at the end of the resolution for a TraitImpl.

gcc/rust/ChangeLog:

* resolve/rust-ast-resolve-item.cc (ResolveItem::visit): fix missing pop

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
7 months agogccrs: remove useless lookup to NodeId
Philip Herron [Tue, 30 May 2023 11:10:40 +0000 (12:10 +0100)]
gccrs: remove useless lookup to NodeId

gcc/rust/ChangeLog:

* backend/rust-compile-expr.cc (CompileExpr::visit): remove unused

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
7 months agogccrs: Add missing HIR lowering to item statements
Philip Herron [Tue, 30 May 2023 08:52:34 +0000 (09:52 +0100)]
gccrs: Add missing HIR lowering to item statements

Addresses #2238

gcc/rust/ChangeLog:

* hir/rust-ast-lower-stmt.cc (ASTLoweringStmt::translate): hir lowering
(ASTLoweringStmt::visit): likewise
* hir/rust-ast-lower-stmt.h: likewise

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
7 months agogccrs: Avoid invalid metavariable substitutions
Owen Avery [Sun, 28 May 2023 17:33:52 +0000 (13:33 -0400)]
gccrs: Avoid invalid metavariable substitutions

gcc/rust/ChangeLog:

* expand/rust-macro-substitute-ctx.cc
(SubstituteCtx::substitute_metavar): Allow substitution failure.
(SubstituteCtx::substitute_token):
Handle substitution failure, include dollar sign in count of tokens to substitute.
(SubstituteCtx::substitute_tokens):
Include dollar sign in count of tokens to substitute.
* expand/rust-macro-substitute-ctx.h
(SubstituteCtx::substitute_metavar):
Adjust signature and document new substitution failure handling.

gcc/testsuite/ChangeLog:

* rust/compile/issue-2225.rs: New test.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
7 months agogccrs: Fix handling of single fragments in repetitions
Owen Avery [Mon, 29 May 2023 03:44:57 +0000 (23:44 -0400)]
gccrs: Fix handling of single fragments in repetitions

gcc/rust/ChangeLog:

* expand/rust-macro-substitute-ctx.cc
(SubstituteCtx::check_repetition_amount):
Ignore single fragments while checking repetition amount.

gcc/testsuite/ChangeLog:

* rust/compile/issue-2207.rs: New test.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
7 months agogccrs: Fix bounds checking to check both sides
Philip Herron [Thu, 25 May 2023 16:43:31 +0000 (17:43 +0100)]
gccrs: Fix bounds checking to check both sides

We were missing bounds checking for both lhs and rhs directions this is
important as we might fail checking for all specified bounds properly.
This is why for #2236 we need the Type parameter T to realise that it
_cannot_ coerce to the i32 max function directly without any adjustments
because T has the specified bound of Deref but i32 does not implement
Deref. This indrectly forces the autoderef cycle to try a deref in order
to get an i32 which _will_ match the i32 max function in the case we
pass an &32 as the type parameter T.

Fixes #2236

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::resolve_segments): stop if error
* typecheck/rust-tyty-bounds.cc (TypeBoundsProbe::assemble_sized_builtin): fix sized options
* typecheck/rust-tyty.cc (BaseType::satisfies_bound): its ok if its an ?T
(BaseType::bounds_compatible): likewise
* typecheck/rust-tyty.h: update prototype
* typecheck/rust-unify.cc (UnifyRules::go): check both sides bounds

gcc/testsuite/ChangeLog:

* rust/execute/torture/issue-2236.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
7 months agogccrs: support deref cycles to resolve to trait items
Philip Herron [Tue, 23 May 2023 16:55:32 +0000 (17:55 +0100)]
gccrs: support deref cycles to resolve to trait items

Fixes #2190

gcc/rust/ChangeLog:

* backend/rust-compile-expr.cc (HIRCompileBase::resolve_deref_adjustment):
reuse op overload code
* typecheck/rust-autoderef.cc (Adjuster::try_deref_type): update
(Adjuster::try_raw_deref_type): likewise
* typecheck/rust-autoderef.h: likewise
* typecheck/rust-hir-dot-operator.cc (MethodResolver::select):
improve debug loging

gcc/testsuite/ChangeLog:

* rust/compile/issue-2190-1.rs: New test.
* rust/compile/issue-2190-2.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
7 months agogccrs: refactor resolve_method_address to be more generic
Philip Herron [Tue, 23 May 2023 13:35:17 +0000 (14:35 +0100)]
gccrs: refactor resolve_method_address to be more generic

We can reuse the DefId to lookup the mappings to Items or Trait Items
instead of the HirId this is more generic and can then be reused for the
deref operator overload during autoderef cycles.

Addresses #2190

gcc/rust/ChangeLog:

* backend/rust-compile-base.cc (HIRCompileBase::resolve_method_address): refactor
* backend/rust-compile-base.h: likewise
* backend/rust-compile-expr.cc (CompileExpr::visit): likewise
* backend/rust-compile-resolve-path.cc (HIRCompileBase::query_compile): likewise

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
7 months agogccrs: hir: remove stray semicolon
David Malcolm [Wed, 24 May 2023 13:19:43 +0000 (09:19 -0400)]
gccrs: hir: remove stray semicolon

Fix for this warning:

../../src/gcc/rust/typecheck/rust-hir-trait-reference.cc:40:2: warning: extra ‘;’ [-Wpedantic]
   40 | };
      |  ^

gcc/rust/ChangeLog:

* typecheck/rust-hir-trait-reference.cc (TraitItemReference::is_optional): Remove
stray semicolon.

7 months agogccrs: enr: Cleanup #[macro_use] accumulation
Arthur Cohen [Mon, 22 May 2023 09:22:45 +0000 (11:22 +0200)]
gccrs: enr: Cleanup #[macro_use] accumulation

gcc/rust/ChangeLog:

* resolve/rust-early-name-resolver.cc (EarlyNameResolver::go): Cleanup
order of calls when accumulating #[macro_use] macros.
(EarlyNameResolver::visit): Likewise.

7 months agogccrs: Fix macro resolutions in middle of multiple nested macro_use modules
Matthew Jasper [Fri, 19 May 2023 10:21:27 +0000 (11:21 +0100)]
gccrs: Fix macro resolutions in middle of multiple nested macro_use modules

gcc/rust/ChangeLog:

* resolve/rust-early-name-resolver.cc (EarlyNameResolver::accumulate_escaped_macros):
Remove unnecessary visit.

gcc/testsuite/ChangeLog:

* rust/compile/nested_macro_use3.rs: New test.

Signed-off-by: Matthew Jasper <mjjasper1@gmail.com>
7 months agogccrs: Only check first item of cfg_attr attribute as predicate
Matthew Jasper [Sun, 21 May 2023 19:23:27 +0000 (20:23 +0100)]
gccrs: Only check first item of cfg_attr attribute as predicate

In #[cfg_attr(A, B, C(D))], only A is a predicate.

gcc/rust/ChangeLog:

* ast/rust-ast.cc (Attribute::check_cfg_predicate):
Only check first item as a predicate.

gcc/testsuite/ChangeLog:

* rust/compile/cfg-attr.rs: New test.

Signed-off-by: Matthew Jasper <mjjasper1@gmail.com>
7 months agogccrs: Add operator== for Optional.
Owen Avery [Fri, 19 May 2023 17:15:34 +0000 (13:15 -0400)]
gccrs: Add operator== for Optional.

gcc/rust/ChangeLog:

* util/rust-optional.h
(operator==): New for Optional<T> == Optional<U>.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
7 months agogccrs: Parse AttrInputMacro
Owen Avery [Sun, 30 Apr 2023 06:57:15 +0000 (02:57 -0400)]
gccrs: Parse AttrInputMacro

gcc/rust/ChangeLog:

* parse/rust-parse-impl.h
(Parser::parse_attr_input): Parse AttrInputMacro.

gcc/testsuite/ChangeLog:

* rust/compile/doc_macro.rs: New test.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
7 months agogccrs: ast: Propagate type suffix
Pierre-Emmanuel Patry [Thu, 11 May 2023 14:13:41 +0000 (16:13 +0200)]
gccrs: ast: Propagate type suffix

Propagate back type suffix to created tokens.

gcc/rust/ChangeLog:

* ast/rust-ast-collector.cc (TokenCollector::visit): Propagate
coretype hint.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
7 months agogccrs: Improve Optional<T> implementation
Owen Avery [Fri, 12 May 2023 07:13:30 +0000 (03:13 -0400)]
gccrs: Improve Optional<T> implementation

gcc/rust/ChangeLog:

* util/rust-optional.h
(class Optional<T>): Improve inner state.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
7 months agogccrs: Handle keyword metavariables
Owen Avery [Wed, 17 May 2023 16:32:23 +0000 (12:32 -0400)]
gccrs: Handle keyword metavariables

gcc/rust/ChangeLog:

* expand/rust-macro-substitute-ctx.cc
(SubstituteCtx::check_repetition_amount): Handle keywords.
(SubstituteCtx::substitute_token): Likewise.
* lex/rust-token.cc
(Token::get_str): Likewise.
* parse/rust-parse-impl.h
(Parser::parse_macro_match_fragment): Likewise.

gcc/testsuite/ChangeLog:

* rust/compile/macro-issue2194.rs: New test.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
7 months agogccrs: ast: Add `outer_attrs` to all `Item`s
Arthur Cohen [Tue, 25 Apr 2023 12:43:15 +0000 (14:43 +0200)]
gccrs: ast: Add `outer_attrs` to all `Item`s

gcc/rust/ChangeLog:

* ast/rust-ast.h: Add `outer_attrs` to Item.
* ast/rust-expr.h: Make use of new inheritance methods.
* ast/rust-item.h: Likewise.
* ast/rust-macro.h: Likewise.

7 months agogccrs: libproc_macro: Change constructor in ffistring
Pierre-Emmanuel Patry [Tue, 16 May 2023 12:01:36 +0000 (14:01 +0200)]
gccrs: libproc_macro: Change constructor in ffistring

The "new" constructor wasn't fitting it's usage well.

libgrust/ChangeLog:

* libproc_macro/rust/bridge/ffistring.rs: Implement
From trait for FFIString.
* libproc_macro/rust/bridge/literal.rs: Change
constructor call.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
7 months agogccrs: libproc_macro: Copy ffistring data from source
Pierre-Emmanuel Patry [Tue, 16 May 2023 11:25:40 +0000 (13:25 +0200)]
gccrs: libproc_macro: Copy ffistring data from source

Data from source pointer were not copied correctly in the ffistring
constructor.

libgrust/ChangeLog:

* libproc_macro/ffistring.cc (FFIString::make_ffistring):
Add copy from source pointer.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
7 months agogccrs: libproc_macro: Remove has_suffix attribute
Pierre-Emmanuel Patry [Thu, 11 May 2023 10:35:18 +0000 (12:35 +0200)]
gccrs: libproc_macro: Remove has_suffix attribute

This variable was redundant with the empty string representation and
may have lead to desync between both.

libgrust/ChangeLog:

* libproc_macro/literal.h: Remove has_suffix member
attribute from Literal.
* libproc_macro/literal.cc: Update constructors.
* libproc_macro/rust/bridge/literal.rs: Remove
has_suffix attribute from Literal.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
7 months agogccrs: converter: Merge dispatch into one function
Pierre-Emmanuel Patry [Thu, 11 May 2023 10:23:38 +0000 (12:23 +0200)]
gccrs: converter: Merge dispatch into one function

Floating point and integer dispatch now have almost the same behavior,
their code could therefore be merged.

gcc/rust/ChangeLog:

* util/rust-token-converter.cc (dispatch_float_literals): Remove
function.
(handle_suffix): Rename old dispatch and add one LitKind
argument.
(dispatch_integer_literals): Remove function.
(convert): Change call from dispatch to suffix handler.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
7 months agogccrs: libproc_macro: Change cpp literal representation
Pierre-Emmanuel Patry [Wed, 10 May 2023 13:17:46 +0000 (15:17 +0200)]
gccrs: libproc_macro: Change cpp literal representation

Change the literal representation on cpp side to match the new one in
rust. This means FFIString had to be implemented on cpp side. A few
helper functions has also been introduced.

libgrust/ChangeLog:

* libproc_macro/Makefile.am: Add ffistring unit to
compiled objects list.
* libproc_macro/Makefile.in: Regenerate.
* libproc_macro/literal.cc (Literal::drop): Change with
a call to ffistring drop function.
(Literal::make_literal): Add new helper constructor
(Literal__drop): Remove this function.
(Literal__string): Likewise.
(Literal__byte_string): Likewise.
(Literal__from_string): Moved this function.
(Literal::make_unsigned): Changed the constructor to match the
new layout.
(Literal::make_signed): Likewise.
(Literal::clone): Reimplement th eclone function.
(Literal::make_u8): Changed the constructor, make suffixed by
default.
(Literal::make_u16): Likewise.
(Literal::make_u32): Likewise.
(Literal::make_u64): Likewise.
(Literal::make_i8): Likewise.
(Literal::make_i16): Likewise.
(Literal::make_i32): Likewise.
(Literal::make_i64): Likewise.
(Literal::make_string): Likewise.
(Literal::make_byte_string): Likewise.
(Literal::make_f32): Likewise.
(Literal::make_f64): Likewise.
(Literal::make_char): Likewise.
(Literal::make_usize): Likewise.
(Literal::make_isize): Likewise.
(LitKind::make_byte): Add new helper constructor to avoid having
to set the payload value.
(LitKind::make_char): Likewise.
(LitKind::make_integer): Likewise.
(LitKind::make_float): Likewise.
(LitKind::make_str): Likewise.
(LitKind::make_str_raw): Add a new helper constructor which
takes the payload value as an argument.
(LitKind::make_byte_str): Add new helper constructor to avoid
mistakes with payload value.
(LitKind::make_byte_str_raw): Add a new helper constructor which
takes the payload value as an argument.
* libproc_macro/literal.h: Add new functions prototype.
(enum UnsignedTag): Removed because it is now unused.
(struct Payload128): Likewise.
(union UnsignedPayload): Likewise.
(struct Unsigned): Likewise.
(enum SignedTag): Likewise.
(union SignedPayload): Likewise.
(struct Signed): Likewise.
(enum LiteralTag): Likewise.
(enum LitKindTag): Likewise.
(struct StringPayload): Likewise.
(struct ByteStringPayload): Likewise.
(union LitKindPayload): Likewise.
(struct UnsignedSuffixPayload): Likewise.
(struct LitKind): Add new literal kind struct representation to
match the enum on rust side.
(struct SignedSuffixPayload): Removed because now unused.
(struct UsizePayload): Likewise.
(struct IsizePayload): Likewise.
(struct Float32Payload): Likewise.
(struct Float64Payload): Likewise.
(union LiteralPayload): Likewise.
(struct Literal): Changed the internals of the structure.
(Literal__drop): Removed the drop function fom the c interface.
(Literal__string): Removed unused function.
(Literal__byte_string): Removed unused function.
* libproc_macro/ffistring.cc: New file.
* libproc_macro/ffistring.h: New file.

gcc/rust/ChangeLog:

* lex/rust-token.h: Implement hash for token id enumeration.
* util/rust-token-converter.cc (dispatch_float_literals): Update
to new internals.
(dispatch_integer_literals): Likewise.
(convert): Likewise.
(string_literal): Remove function.
(byte_string_literal): Likewise.
(unsigned_literal): Likewise.
(signed_literal): Likewise.
(from_literal): Update with new internals.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
7 months agogccrs: libproc_macro: Change rust literal internals
Pierre-Emmanuel Patry [Wed, 10 May 2023 09:34:45 +0000 (11:34 +0200)]
gccrs: libproc_macro: Change rust literal internals

Rust interface literal internals were taking a type and storing that
type. This lead to multiple problems such as various conversion from
string to int/float/other type as well as dead end on undetermined types
(type checker runs at a later stage).

libgrust/ChangeLog:

* libproc_macro/rust/bridge.rs: Add ffistring module.
* libproc_macro/rust/bridge/literal.rs: Rework type
internals.
* libproc_macro/rust/bridge/ffistring.rs: New file.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
7 months agogccrs: Add AST::AttrInputMacro
Owen Avery [Sun, 30 Apr 2023 06:54:22 +0000 (02:54 -0400)]
gccrs: Add AST::AttrInputMacro

gcc/rust/ChangeLog:

* ast/rust-ast-dump.cc
(Dump::visit): Add AttrInputMacro visitor.
* ast/rust-ast-dump.h:
(Dump::visit): Likewise.
* ast/rust-ast-full-decls.h
(class AttrInputMacro): New.
* ast/rust-ast-collector.cc
(TokenCollector::visit):
Add and call out to AttrInputMacro visitor.
* ast/rust-ast-collector.h:
(TokenCollector::visit): Add AttrInputMacro visitor.
* ast/rust-ast-visitor.h:
(ASTVisitor::visit): Likewise.
* ast/rust-ast.cc
(AttrInputMacro::accept_vis): New.
(AttrInputMacro::as_string): New.
(AttrInputMacro::AttrInputMacro): New.
(AttrInputMacro::operator=): New.
* ast/rust-ast.h:
(AttrInput::AttrInputType::MACRO): New.
* ast/rust-expr.h
(class AttrInputMacro): New.
* checks/errors/rust-feature-gate.h:
(FeatureGate::visit): Add AttrInputMacro visitor.
* expand/rust-cfg-strip.cc
(CfgStrip::visit): Likewise.
* expand/rust-cfg-strip.h:
(CfgStrip::visit): Likewise.
* expand/rust-expand-visitor.cc
(ExpandVisitor::visit): Likewise.
* expand/rust-expand-visitor.h:
(ExpandVisitor::visit): Likewise.
* hir/rust-ast-lower-base.cc
(ASTLoweringBase::visit): Likewise.
* hir/rust-ast-lower-base.h:
(ASTLoweringBase::visit): Likewise.
* resolve/rust-ast-resolve-base.cc
(ResolverBase::visit): Likewise.
* resolve/rust-ast-resolve-base.h:
(ResolverBase::visit): Likewise.
* resolve/rust-early-name-resolver.cc
(EarlyNameResolver::visit): Likewise.
* resolve/rust-early-name-resolver.h:
(EarlyNameResolver::visit): Likewise.
* util/rust-attributes.cc
(AttributeChecker::visit): Likewise.
(check_doc_attribute): Handle AttrInputType::MACRO.
* util/rust-attributes.h:
(AttributeChecker::visit): Add AttrInputMacro visitor.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
7 months agogccrs: Make MacroInvocation cloning public
Owen Avery [Thu, 11 May 2023 04:29:04 +0000 (00:29 -0400)]
gccrs: Make MacroInvocation cloning public

gcc/rust/ChangeLog:

* ast/rust-macro.h
(MacroInvocation::clone_macro_invocation_impl): Make public.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
7 months agogccrs: Allow hashing Optional<T>
Owen Avery [Fri, 12 May 2023 07:14:29 +0000 (03:14 -0400)]
gccrs: Allow hashing Optional<T>

gcc/rust/ChangeLog:

* util/rust-optional.h
(struct std::hash<Optional<T>>): New.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
7 months agogccrs: Improve Optional<T&> implementation
Owen Avery [Fri, 12 May 2023 07:05:03 +0000 (03:05 -0400)]
gccrs: Improve Optional<T&> implementation

gcc/rust/ChangeLog:

* util/rust-optional.h
(class Optional<T&>): Use pointers internally.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
7 months agogccrs: dump: Dump `TupleIndexExpr`s
Arthur Cohen [Fri, 12 May 2023 12:44:54 +0000 (14:44 +0200)]
gccrs: dump: Dump `TupleIndexExpr`s

gcc/rust/ChangeLog:

* ast/rust-ast-dump.cc (Dump::visit): Implement dump for
`TupleIndexExpr`.

7 months agogccrs: Change return type of token_id_keyword_string
Owen Avery [Tue, 16 May 2023 04:31:38 +0000 (00:31 -0400)]
gccrs: Change return type of token_id_keyword_string

gcc/rust/ChangeLog:

* lex/rust-token.cc
(token_id_keyword_string): Return std::string reference.
* lex/rust-token.h
(token_id_keyword_string): Return std::string reference.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
7 months agogccrs: Fix parsing of block expressions followed by `.`
Matthew Jasper [Wed, 10 May 2023 01:08:37 +0000 (02:08 +0100)]
gccrs: Fix parsing of block expressions followed by `.`

`{ ... }.f;` is parsed as a single statement in rust. This means that we can't
determine whether an expression statement will need a semicolon terminator
until we finish parsing it. To handle this we change expression parsing to
check for this case by inspecting the expression returned from null_denotation
and looking ahead for a `.` or `?` token.

gcc/rust/ChangeLog:

* ast/rust-ast.h (Expr::as_expr_without_block): Remove.
(Expr::set_outer_attrs): Make public in base class.
* expand/rust-macro-expand.cc:
Add fixme comment for pre-existing bug.
* hir/tree/rust-hir.h: Remove Expr::as_expr_without_block.
* parse/rust-parse-impl.h (Parser::parse_lifetime): Use lifetime_from_token.
(Parser::lifetime_from_token): New method.
(Parser::null_denotation): Handle labelled loop expressions and for loop expressions.
(Parser::parse_loop_label): Make initial token a parameter.
(Parser::parse_labelled_loop_expr): Likewise.
(Parser::parse_for_loop_expr): Allow FOR token to already be skipped.
(Parser::parse_expr): Handle expr_can_be_stmt.
(Parser::parse_expr_with_block): Remove.
(Parser::parse_expr_stmt_with_block): Remove.
(Parser::parse_expr_stmt_without_block): Remove.
(Parser::parse_expr_without_block): Remove.
(Parser::parse_stmt_or_expr_with_block): Remove.
(Parser::parse_expr_stmt): Use parse_expr directly.
(Parser::parse_match_expr): Likewise.
(Parser::parse_stmt): Use parse_expr_stmt in more cases.
(Parser::parse_stmt_or_expr):
Rename from parse_stmt_or_expr_without_block, use parse_expr directly.
(Parser::parse_block_expr): Update error message.
* parse/rust-parse.h: Update declarations.

gcc/testsuite/ChangeLog:

* rust/compile/for_expr.rs: New test.
* rust/compile/issue-407-2.rs: Update compiler output.
* rust/compile/issue-407.rs: Update compiler output.
* rust/compile/issue-867.rs: Update compiler output.
* rust/compile/issue-2189.rs: New test.
* rust/compile/macro_call_statement.rs: New test.
* rust/compile/stmt_with_block_dot.rs: New test.
* rust/compile/torture/loop8.rs: New test.

Signed-off-by: Matthew Jasper <mjjasper1@gmail.com>
7 months agogccrs: ast: Remove ExprStmtWithBlock / ExprStmtWithoutBlock distinction
Matthew Jasper [Tue, 9 May 2023 11:53:33 +0000 (12:53 +0100)]
gccrs: ast: Remove ExprStmtWithBlock / ExprStmtWithoutBlock distinction

This distinction isn't very helpful and makes correct parsing harder.

gcc/rust/ChangeLog:

* ast/rust-ast-full-decls.h (class ExprStmtWithoutBlock): Remove.
(class ExprStmtWithBlock): Remove.
* ast/rust-stmt.h (class ExprStmtWithoutBlock): Remove.
(class ExprStmtWithBlock): Remove.
(class ExprStmt):
Make non-abstract, add common functionality from removed base classes.
* ast/rust-ast.h: Move to_stmt to base class.
* ast/rust-ast.cc (ExprStmtWithBlock::as_string): Remove.
* ast/rust-macro.h: Use new signature for to_stmt.
(ExprStmt::as_string): New method.
(ExprStmtWithoutBlock::as_string): Remove.
(BlockExpr::strip_tail_expr): Update for removed classes.
(ExprStmtWithoutBlock::accept_vis): Remove.
(ExprStmtWithBlock::accept_vis): Remove.
(ExprStmt::accept_vis): New method.
* ast/rust-ast-dump.cc (Dump::visit): Update for removed classes.
* ast/rust-ast-dump.h: Likewise.
* ast/rust-ast-collector.h: Likewise.
* ast/rust-ast-collector.cc (TokenStream::visit): Likewise.
* ast/rust-ast-visitor.h: Likewise.
* checks/errors/rust-feature-gate.h: Likewise.
* expand/rust-expand-visitor.cc (ExpandVisitor::visit): Likewise.
* expand/rust-expand-visitor.h: Likewise.
* expand/rust-cfg-strip.cc (CfgStrip::visit): Likewise.
* expand/rust-cfg-strip.h: Likewise.
* hir/rust-ast-lower-base.cc (ASTLoweringBase::visit): Likewise.
* hir/rust-ast-lower-base.h: Likewise.
* hir/rust-ast-lower-stmt.cc (ASTLoweringStmt::visit): Likewise.
* hir/rust-ast-lower-stmt.h: Likewise.
* util/rust-attributes.cc (AttributeChecker::visit): Likewise.
* util/rust-attributes.h: Likewise.
* resolve/rust-ast-resolve-base.cc (ResolverBase::visit): Likewise.
* resolve/rust-ast-resolve-base.h: Likewise.
* resolve/rust-ast-resolve-stmt.h: Likewise.
* resolve/rust-early-name-resolver.cc (EarlyNameResolver::visit):
Likewise.
* resolve/rust-early-name-resolver.h: Likewise.
* parse/rust-parse-impl.h (Parser::parse_match_expr): Likewise.
(Parser::parse_stmt_or_expr_without_block): Likewise.
* parse/rust-parse.h: Likewise.

Signed-off-by: Matthew Jasper <mjjasper1@gmail.com>
This page took 0.119213 seconds and 5 git commands to generate.