aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/parse/rust-parse-impl.h
AgeCommit message (Collapse)AuthorFilesLines
2024-08-01gccrs: Unify ASTValidation::visit for ExternalFunctionItem and Function0xn4utilus1-4/+5
gcc/rust/ChangeLog: * checks/errors/rust-ast-validation.cc (ASTValidation::visit): Add external function validation support. Add ErrorCode::E0130. * parse/rust-parse-impl.h (Parser::parse_function): Parse external functions from `parse_function`. (Parser::parse_external_item): Clang format. (Parser::parse_pattern): Clang format. * parse/rust-parse.h: Add default parameter `is_external` in `parse_function`. Signed-off-by: 0xn4utilus <gyanendrabanjare8@gmail.com>
2024-08-01gccrs: Ensure TupleStructPattern and TuplePattern have itemsOwen Avery1-16/+0
Note that instances of both classes which have been moved from will have (items == nullptr). gcc/rust/ChangeLog: * ast/rust-pattern.h (class TupleStructPattern): Assert that items != nullptr. (class TuplePattern): Likewise. (TupleStructPattern::has_items): Remove. (TuplePattern::has_tuple_pattern_items): Likewise. * parse/rust-parse-impl.h (Parser::parse_ident_leading_pattern): Prevent construction of TupleStructPattern with (items == nullptr). (Parser::parse_pattern_no_alt): Likewise. * ast/rust-ast-collector.cc (TokenCollector::visit): Remove usage of TupleStructPattern::has_items. * ast/rust-ast-visitor.cc (DefaultASTVisitor::visit): Likewise. * resolve/rust-early-name-resolver.cc (EarlyNameResolver::visit): Likewise. gcc/testsuite/ChangeLog: * rust/compile/pattern-struct.rs: Fix test. Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
2024-02-07gccrs: Fix macro parsing for trait items.Kushal Pal1-18/+19
gcc/rust/ChangeLog: * parse/rust-parse-impl.h (Parser::parse_trait_item): Handle macros in trait items similar to how its handled for trait implementation items. Signed-off-by: Kushal Pal <kushalpal109@gmail.com>
2024-02-07gccrs: Parse trait functions as `AST::Function`.Kushal Pal1-125/+2
To use AST::Function for trait functions, we can parse trait functions using available parse_function(). gcc/rust/ChangeLog: * parse/rust-parse-impl.h (Parser::parse_trait_item): Use parse_function() to parse trait functions. Signed-off-by: Kushal Pal <kushalpal109@gmail.com>
2024-02-07gccrs: Use AssociatedItem in place of TraitItemKushal Pal1-3/+3
gcc/rust/ChangeLog: * ast/rust-ast.h: Replace TraitItem with AssociatedItem. * ast/rust-item.h (class Trait): Likewise. * expand/rust-expand-visitor.cc (ExpandVisitor::visit): Likewise. * parse/rust-parse-impl.h (Parser::parse_trait): Likewise. * parse/rust-parse.h: Likewise. Signed-off-by: Kushal Pal <kushalpal109@gmail.com>
2024-02-07gccrs: Parse normal functions with `self` parameter correctlyKushal Pal1-2/+2
Fixes #2812 gcc/rust/ChangeLog: * parse/rust-parse-impl.h (Parser::parse_function): Skip token if its a COMMA. gcc/testsuite/ChangeLog: * rust/compile/issue-2812.rs: New test. Signed-off-by: Kushal Pal <kushalpal109@gmail.com>
2024-01-30gccrs: Remove TraitImplItemOwen Avery1-4/+4
gcc/rust/ChangeLog: * ast/rust-ast-full-decls.h (class TraitImplItem): Remove forward declaration. (class AssociatedItem): Add forward declaration. * ast/rust-ast.h (class TraitImplItem): Remove. (class TraitItem): Inherit from AssociatedItem. (SingleASTNode::take_trait_impl_item): Return std::unique_ptr<AssociatedItem> instead of std::unique_ptr<TraitImplItem>. * ast/rust-item.h (class Function): Inherit from AssociatedItem instead of TraitImplItem. (class TypeAlias): Likewise. (class ConstantItem): Likewise. (class TraitImpl): Store items as AssociatedItem. * expand/rust-derive-clone.cc (DeriveClone::clone_fn): Return std::unique_ptr<AssociatedItem>. (DeriveClone::clone_impl): Take std::unique_ptr<AssociatedItem>. * expand/rust-derive-clone.h (DeriveClone::clone_fn): Return std::unique_ptr<AssociatedItem>. (DeriveClone::clone_impl): Take std::unique_ptr<AssociatedItem>. * expand/rust-expand-visitor.cc (ExpandVisitor::visit): Handle changes to SingleASTNode::take_trait_impl_item. * parse/rust-parse-impl.h (Parser::parse_impl): Parse TraitImpl as containing AssociatedItem. (Parser::parse_trait_impl_item): Return std::unique_ptr<AssociatedItem>. (Parser::parse_trait_impl_function_or_method): Likewise. * parse/rust-parse.h (Parser::parse_trait_impl_item): Return std::unique_ptr<AssociatedItem>. (Parser::parse_trait_impl_function_or_method): Likewise. Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
2024-01-30gccrs: AST: Fix for lifetime parsingJakub Dupak1-14/+23
gcc/rust/ChangeLog: * parse/rust-parse-impl.h (Parser::parse_where_clause): fix parsing (Parser::parse_where_clause_item): fix parsing (Parser::parse_type_bound_where_clause_item): fix parsing (Parser::parse_trait_bound): fix parsing * parse/rust-parse.h: fix parsing Signed-off-by: Jakub Dupak <dev@jakubdupak.com>
2024-01-30gccrs: Handle `async` keyword for regular implementationsKushal Pal1-0/+1
Fixes #2788 gcc/rust/ChangeLog: * parse/rust-parse-impl.h (Parser::parse_inherent_impl_item): Added switch-case for ASYNC token. gcc/testsuite/ChangeLog: * rust/compile/issue-2788.rs: New test. Signed-off-by: Kushal Pal <kushalpal109@gmail.com>
2024-01-30gccrs: Handle `async` functions in traitsKushal Pal1-0/+1
Fixes #2785 gcc/rust/ChangeLog: * checks/errors/rust-ast-validation.cc (ASTValidation::visit): Added check for `async` functions inside trait. * parse/rust-parse-impl.h (Parser::parse_trait_item): Added switch-case for ASYNC token. gcc/testsuite/ChangeLog: * rust/compile/issue-2785.rs: New test. Signed-off-by: Kushal Pal <kushalpal109@gmail.com>
2024-01-30gccrs: ast: Full lifetime elision handlingJakub Dupak1-12/+13
gcc/rust/ChangeLog: * parse/rust-parse-impl.h (Parser::parse_generic_param): Lifetime elision control. (Parser::parse_lifetime_where_clause_item): Lifetime elision control. (Parser::parse_type_param_bound): Lifetime elision control. (Parser::parse_lifetime_bounds): Lifetime elision control. (Parser::parse_lifetime): Lifetime elision control. (Parser::parse_path_generic_args): Lifetime elision control. (Parser::parse_self_param): Lifetime elision control. (Parser::parse_break_expr): Lifetime elision control. (Parser::parse_continue_expr): Lifetime elision control. (Parser::parse_reference_type_inner): Lifetime elision control. * parse/rust-parse.h: Lifetime elision control. Signed-off-by: Jakub Dupak <dev@jakubdupak.com>
2024-01-30gccrs: ast: Unify explicitly and implicitly elided lifettimesJakub Dupak1-6/+4
gcc/rust/ChangeLog: * ast/rust-ast.h: Elided lifetime static constructor * ast/rust-type.h: Default lifetime to elided. * parse/rust-parse-impl.h (Parser::parse_lifetime_param): Use elided lifetime. (Parser::parse_lifetime): Use elided lifetime/ (Parser::lifetime_from_token): Use elided lifetime. (Parser::parse_self_param): Use elided lifetime. (Parser::parse_reference_type_inner): Use elided lifetime. Signed-off-by: Jakub Dupak <dev@jakubdupak.com>
2024-01-30gccrs: ast: Fix lifetime type parsingJakub Dupak1-2/+2
There was a mismatch whether lifetime 'static is parsed as "static" or "'static". gcc/rust/ChangeLog: * parse/rust-parse-impl.h (Parser::lifetime_from_token): Fix matched pattern. Signed-off-by: Jakub Dupak <dev@jakubdupak.com>
2024-01-30gccrs: Handle `async` qualifier inside traitKushal Pal1-0/+2
Fixes #2778 gcc/rust/ChangeLog: * parse/rust-parse-impl.h (Parser::parse_trait_impl_item): Handled `async` items Signed-off-by: Kushal Pal <kushalpal109@gmail.com>
2024-01-30gccrs: Make function bodies truly optionalPierre-Emmanuel Patry1-58/+47
Missing body on a function should be rejected at a later stage in the compiler, not during parsing. gcc/rust/ChangeLog: * ast/rust-ast-collector.cc (TokenCollector::visit): Adapt defintion getter. * ast/rust-ast-visitor.cc (DefaultASTVisitor::visit): Likewise. * expand/rust-cfg-strip.cc (CfgStrip::visit): Likewise. * expand/rust-expand-visitor.cc (ExpandVisitor::visit): Likewise. * hir/rust-ast-lower-implitem.h: Likewise. * hir/rust-ast-lower-item.cc (ASTLoweringItem::visit): Likewise. * resolve/rust-ast-resolve-item.cc (ResolveItem::visit): Likewise. * resolve/rust-ast-resolve-stmt.h: Likewise. * resolve/rust-default-resolver.cc (DefaultResolver::visit): Likewise. * util/rust-attributes.cc (AttributeChecker::visit): Likewise. * parse/rust-parse-impl.h: Allow empty function body during parsing. * ast/rust-ast.cc (Function::Function): Constructor now take an optional for the body. (Function::operator=): Adapt to new optional member. (Function::as_string): Likewise. * ast/rust-item.h (class Function): Make body optional and do not rely on nullptr anymore. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-30gccrs: Allow const and async specifiers in functionsPierre-Emmanuel Patry1-2/+6
We need to account for const specifiers in async parsing as const can be used in the syntax before the async keyword. gcc/rust/ChangeLog: * parse/rust-parse-impl.h (Parser::parse_vis_item): Allow parsing async items in const. (Parser::parse_async_item): Account for const offset during async lookahead. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-30gccrs: Split async and const function qualifiersPierre-Emmanuel Patry1-19/+27
A function cannot be both async and const, however this should not be handled in the parser but rather at a later stage in the compiler. This commit change the AsyncConstStatus in the AST and the HIR to allows a function to be both async and const. gcc/rust/ChangeLog: * ast/rust-ast-builder.cc (AstBuilder::fn_qualifiers): Change constructor to match the new arguments. * ast/rust-ast-collector.cc (TokenCollector::visit): Change behavior to handle both const and async specifiers at the same time. * ast/rust-ast.cc (FunctionQualifiers::as_string): Likewise. * ast/rust-item.h (class FunctionQualifiers): Remove AsyncConstStatus and replace it with both Async and Const status. Also change the safety arguments to use an enum instead of a boolean. * hir/rust-ast-lower-base.cc (ASTLoweringBase::lower_qualifiers): Update constructor call. * hir/tree/rust-hir-item.h: Add Const and Async status, remove AsyncConstStatus, update the constructor. * hir/tree/rust-hir.cc (FunctionQualifiers::as_string): Update with the new status. * parse/rust-parse-impl.h (Parser::parse_function_qualifiers): Update constructor call. * util/rust-common.h (enum Mutability): Make an enum class. (enum class): Add Async and Const enum class to avoid booleans. (enum Unsafety): Change to an enum class. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-30gccrs: Remove class AST::InherentImplItemOwen Avery1-4/+4
gcc/rust/ChangeLog: * ast/rust-ast-full-decls.h (class InherentImplItem): Remove. * ast/rust-ast.h (class InherentImplItem): Remove. (class SingleASTNode): Store pointer to AssociatedItem instead of InherentImplItem. * ast/rust-ast.cc (SingleASTNode::SingleASTNode): Use clone_associated_item instead of clone_inherent_impl_item. (SingleASTNode::operator=): Likewise. * ast/rust-item.h (class InherentImpl): Use AssociatedItem rather than InherentImplItem. (class Function): Likewise. (class ConstantItem): Likewise. * ast/rust-macro.h (class MacroInvocation): Likewise. * expand/rust-expand-visitor.cc (ExpandVisitor::visit): Likewise. * parse/rust-parse-impl.h (Parser::parse_impl): Likewise. (Parser::parse_inherent_impl_item): Likewise. (Parser::parse_inherent_impl_function_or_method): Likewise. * parse/rust-parse.h (Parser::parse_inherent_impl_item): Likewise. (Parser::parse_inherent_impl_function_or_method): Likewise. Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
2024-01-30gccrs: Emit an error on unsafe modulesPierre-Emmanuel Patry1-1/+2
An error should be emitted on unsafe modules during the AST validation pass as the syntax allows those even though they're not alowed later down the line. gcc/rust/ChangeLog: * ast/rust-item.h: Add safety getter to modules. * checks/errors/rust-ast-validation.cc (ASTValidation::visit): Check a module's safety and emit an error when meeting an unsafe module. * checks/errors/rust-ast-validation.h: Add function prototype. * parse/rust-parse-impl.h (Parser::parse_module): Move the module locus to the first token instead of the mod keyword. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-30gccrs: Parse module safetyPierre-Emmanuel Patry1-3/+11
Unsafe module are rejected at a later stage but could not be parsed properly. This commit changes the parser in order to allow unsafe module in the AST. gcc/rust/ChangeLog: * parse/rust-parse-impl.h (Parser::parse_vis_item): Dispatch to parse module when meeting an unsafe module. (Parser::parse_module): Set unsafe status when the parser encounter an unsafe keyword. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-30gccrs: Add unsafety member to modulesPierre-Emmanuel Patry1-3/+5
The rust syntax allows unsafe module even if those are rejected at a later stage. gcc/rust/ChangeLog: * ast/rust-item.h: Add safety status to Modules in the AST. * parse/rust-parse-impl.h (Parser::parse_module): Adapt constructors. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-30gccrs: Added support to Parse ASYNC functionM V V S Manoj Kumar1-0/+40
Fixes issue #2650 The parser now parses ASYNC functions. Added ASYNC case to parse_item Added a new function parse_async_item which is called in parse_vis_item to handle the ASYNC case. Parse_async_item also checks the current Rust edition and generates an error if the edition is 2015 gcc/rust/ChangeLog: * parse/rust-parse-impl.h (Parser::parse_item): Likewise. (Parser::parse_vis_item): Likewise. (Parser::parse_async_item): Likewise. * parse/rust-parse.h: Made declaration for parse_async_item. gcc/testsuite/ChangeLog: * rust/compile/issue-2650-1.rs: New test.(edition=2018) * rust/compile/issue-2650-2.rs: New test.(edition=2015) Signed-off-by: M V V S Manoj Kumar <mvvsmanojkumar@gmail.com>
2024-01-30gccrs: Replace some weak keyword raw value with constexprPierre-Emmanuel Patry1-11/+13
Raw values may have typos or contain error, replacing those will improve the codebase. gcc/rust/ChangeLog: * ast/rust-ast-collector.cc (TokenCollector::visit): Replace raw value. * parse/rust-parse-impl.h (Parser::is_macro_rules_def): Likewise. (Parser::parse_item): Likewise. (Parser::parse_vis_item): Likewise. (Parser::parse_macro_rules_def): Likewise. (Parser::parse_union): Likewise. (Parser::parse_trait_impl_item): Likewise. (Parser::parse_stmt): Likewise. (Parser::parse_stmt_or_expr): Likewise. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-30gccrs: Replace some keyword raw valuesPierre-Emmanuel Patry1-23/+29
Raw values cannot be understood easily by most tools. This commit replace some raw values with their variable counterpart. gcc/rust/ChangeLog: * ast/rust-ast-collector.cc (TokenCollector::visit): Replace raw value with keyword call. * ast/rust-ast.h: Likewise. * parse/rust-parse-impl.h (Parser::parse_path_ident_segment): Likewise. (Parser::parse_macro_match_fragment): Likewise. (Parser::parse_extern_crate): Likewise. (Parser::parse_use_tree): Likewise. (Parser::parse_const_item): Likewise. (Parser::parse_literal_expr): Likewise. (Parser::parse_maybe_named_param): Likewise. (Parser::parse_pattern_no_alt): Likewise. (Parser::left_denotation): Likewise. (Parser::parse_path_in_expression_pratt): Likewise. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-30gccrs: Replace TOK suffix with KWPierre-Emmanuel Patry1-67/+67
TOK suffix was chosen to disambiguate some identifiers with c++ reserved keyword. Even though this list lies within the rust-token header, this macro is used in many context sometimes unrelated with the lexer and tokens. This TOK suffix may appear surprising in such context. gcc/rust/ChangeLog: * lex/rust-token.h (enum PrimitiveCoreType): Change keyword suffix from tok to kw. * ast/rust-ast-collector.cc (TokenCollector::visit): Update suffix to match the new declaration. * lex/rust-lex.cc (Lexer::parse_raw_identifier): Likewise. * parse/rust-parse-impl.h (can_tok_start_type): Likewise. (Parser::parse_item): Likewise. (Parser::parse_vis_item): Likewise. (Parser::parse_extern_crate): Likewise. (Parser::parse_function): Likewise. (Parser::parse_function_qualifiers): Likewise. (Parser::parse_struct): Likewise. (Parser::parse_enum): Likewise. (Parser::parse_static_item): Likewise. (Parser::parse_trait_item): Likewise. (Parser::parse_inherent_impl_item): Likewise. (Parser::parse_trait_impl_item): Likewise. (Parser::parse_extern_block): Likewise. (Parser::parse_external_item): Likewise. (Parser::parse_stmt): Likewise. (Parser::parse_return_expr): Likewise. (Parser::parse_match_expr): Likewise. (Parser::parse_type): Likewise. (Parser::parse_for_prefixed_type): Likewise. (Parser::parse_type_no_bounds): Likewise. (Parser::parse_stmt_or_expr): Likewise. * parse/rust-parse.cc (peculiar_fragment_match_compatible): Likewie. * util/rust-token-converter.cc (convert): Likewise. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-30gccrs: Add check for associated items on auto traitsPierre-Emmanuel Patry1-12/+0
Reject rust code with associated items on auto traits. gcc/rust/ChangeLog: * checks/errors/rust-ast-validation.cc (ASTValidation::visit): Add auto trait associated item check in AST validation pass. * parse/rust-parse-impl.h: Remove old error emission done during parsing pass. gcc/testsuite/ChangeLog: * rust/compile/auto_trait_invalid.rs: Update old test with updated error message. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-30gccrs: Report self parameter parsing error kindPierre-Emmanuel Patry1-27/+45
Self parameter parsing errors may come from different situations, which should not be handled in the same way. It is now possible to differentiate a missing self parameter from a self pointer or a parsing error. gcc/rust/ChangeLog: * parse/rust-parse-impl.h (Parser::parse_function): Early return on unrecoverable errors. (Parser::parse_trait_item): Likewise. (Parser::parse_self_param): Update return type. * parse/rust-parse.h (enum ParseSelfError): Add enumeration to describe different self parameter parsing errors. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-30gccrs: Fix error emission for self pointersPierre-Emmanuel Patry1-1/+1
Self pointer checking loop condition was inverted, the latter was therefore never executed. gcc/rust/ChangeLog: * parse/rust-parse-impl.h (Parser::parse_self_param): Fix the loop exit condition. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-30gccrs: Add visibility to trait itemPierre-Emmanuel Patry1-3/+7
The compiler shall parse visibility modifiers on trait items and reject those at a later stage (ast validation). gcc/rust/ChangeLog: * ast/rust-item.h (struct Visibility): Move Visibility from here... * ast/rust-ast.h (struct Visibility): ...to here. * parse/rust-parse-impl.h (Parser::parse_trait_item): Parse visibility before giving it back to the item parsing function. (Parser::parse_trait_type): Add visibility modifier. * parse/rust-parse.h (RUST_PARSE_H): Change function prototype. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-16gccrs: Fix float lexing and tuple index disambiguationPierre-Emmanuel Patry1-6/+12
When a float has a floating point but no value after it, a zero was added this lead to errors when trying to disambiguate a float into a tuple index. gcc/rust/ChangeLog: * lex/rust-lex.cc (Lexer::parse_decimal_int_or_float): Remove additional zero after empty floating point. * parse/rust-parse-impl.h (Parser::left_denotation): Handle float with empty floating point. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-16gccrs: Rework function special parametersPierre-Emmanuel Patry1-165/+118
Make self param and variadic param Param, introduce Param class and make function parameters param too. Self can now be represented as a standard parameter and is thus no longer required as a separate function attribute. Prevent self pointers and allow self in standard functions during parsing so they could be rejected at a later stage. gcc/rust/ChangeLog: * ast/rust-ast-collector.cc (TokenCollector::visit): Add visitor for VariadicParam and remove Self parameter visitor from Function visit. * expand/rust-cfg-strip.cc (CfgStrip::maybe_strip_self_param): Remove function. (CfgStrip::maybe_strip_trait_method_decl): Remove self parameter visit. (CfgStrip::maybe_strip_function_params): Handle new function parameters. (CfgStrip::visit): Handle VariadicParam, SelfParam and FunctionParam. * expand/rust-expand-visitor.cc (ExpandVisitor::expand_self_param): Remove function. (ExpandVisitor::expand_trait_method_decl): Do not visit self parameter. (ExpandVisitor::visit): Add visit for VariadicParam, FunctionParam and SelfParam. (ExpandVisitor::expand_function_params): Visit parameters instead. * expand/rust-expand-visitor.h: Update function prototypes. * resolve/rust-ast-resolve-item.cc (ResolveItem::visit): Update visit with new parameters. (ResolveTraitItems::visit): Likewise. * resolve/rust-early-name-resolver.cc (EarlyNameResolver::visit): Update visit functions with the new visitor functions for VariadicParam SelfParam and FunctionParam. * resolve/rust-early-name-resolver.h: Update function prototypes. * ast/rust-ast-visitor.cc (DefaultASTVisitor::visit): Update visitor according to the new function parameter structures. * ast/rust-ast-visitor.h: Update prototypes and add visitor virtual functions for SelfParam, FunctionParam and VariadicParam. * ast/rust-ast.cc (Function::Function): Move constructor in implementation instead of header. (Function::operator=): Likewise. (Function::as_string): Update function with pointer dereference. (VariadicParam::as_string): Likewise. (TraitFunctionDecl::as_string): Likewise. (TraitMethodDecl::as_string): Likewise. (FunctionParam::accept_vis): Add function for visitor. (SelfParam::accept_vis): Likewise. (VariadicParam::accept_vis): Likewise. (TraitItemFunc::TraitItemFunc): Move constructor to implementation file. (TraitItemFunc::operator=): Likewise. (TraitItemMethod::TraitItemMethod): Likewise. (TraitItemMethod::operator=): Likewise. * ast/rust-item.h (class Function): Remove self optional member. (class TraitMethodDecl): Likewise. (class TraitFunctionDecl): Likewise. (class Param): Add abstract parameter class. (class SelfParam): Inherit from Param and remove parameter common members. (class FunctionParam): Likewise. (class VariadicParam): Likewise. (struct Visibility): Move structure declaration. (class VisItem): Likewise. * checks/errors/rust-ast-validation.cc (ASTValidation::visit): Add a self parameter check during AST validation. * checks/errors/rust-ast-validation.h: Add function prototype. * expand/rust-derive-clone.cc (DeriveClone::clone_fn): Update function constructor. * hir/rust-ast-lower-base.cc (ASTLoweringBase::lower_self): Rework function for the new parameters. (ASTLoweringBase::visit): Add visit functions for VariadicParam, FunctionParam and SelfParam. * hir/rust-ast-lower-base.h: Update function prototypes. * parse/rust-parse-impl.h (Parser::parse_function): Update function according to new function representation. (Parser::parse_function_param): Return vector of abstract param instead of FunctionParam. (Parser::parse_method): Update according to new representation. (Parser::parse_trait_item): Likewise. (Parser::parse_self_param): Error out with self pointers and prevent the lexer from eating regular function parameters. Update return type. * parse/rust-parse.h: Update function return types. * ast/rust-ast-collector.h: Add VariadicParam visit prototype. * ast/rust-ast.h (struct Visibility): Move struct declaration. (class VisItem): Likewise. * ast/rust-expr.h: Update included files. * checks/errors/rust-feature-gate.h: Add visitor functions for SelfParam, FunctionParam and VariadicParam. * expand/rust-cfg-strip.h: Update function prototypes. * expand/rust-derive.h: Likewise. * hir/rust-ast-lower-implitem.h: Handle special arguments. * hir/rust-ast-lower-item.cc (ASTLoweringItem::visit): Likewise. * metadata/rust-export-metadata.cc (ExportContext::emit_function): Likewise. * resolve/rust-ast-resolve-base.cc (ResolverBase::visit): Add visitor functions. * resolve/rust-ast-resolve-base.h: Update prototypes. * resolve/rust-ast-resolve-stmt.h: Handle new parameter kind. * resolve/rust-default-resolver.cc (DefaultResolver::visit): Likewise. * resolve/rust-default-resolver.h: Update prototype. * util/rust-attributes.cc (AttributeChecker::visit): Add visitor functions for SelfParam and VariadicParam. * util/rust-attributes.h: Add visit prototypes. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-16gccrs: Use keyword const values instead of raw valuesPierre-Emmanuel Patry1-3/+5
Change the keyword values from a raw string value to their matching const value in utils. gcc/rust/ChangeLog: * lex/rust-lex.cc (Lexer::parse_raw_identifier): Use const value. * parse/rust-parse-impl.h (Parser::parse_simple_path_segment): Likewise. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-16gccrs: Replace AST::Method with existing AST::FunctionOwen Avery1-27/+32
gcc/rust/ChangeLog: * ast/rust-item.h (class Method): Remove. (Function::self_param): New. (Function::has_self_param): New. (Function::Function): Initialize self_param. (Function::operator=): Likewise. (Function::get_self_param): New. * ast/rust-ast.cc (Method::as_string): Remove. (Method::accept_vis): Remove. * ast/rust-ast-collector.cc (TokenCollector::visit): Remove AST::Method visitor, handle self_param in AST::Function visitor. * ast/rust-ast-collector.h (TokenCollector::visit): Remove AST::Method visitor. * ast/rust-ast-full-decls.h (class Method): Remove. * ast/rust-ast-visitor.h (ASTVisitor::visit): Remove AST::Method visitor. (DefaultASTVisitor::visit): Likewise. * ast/rust-ast-visitor.cc (DefaultASTVisitor::visit): Remove AST::Method visitor, handle self_param in AST::Function visitor. * checks/errors/rust-feature-gate.cc (FeatureGate::visit): Remove AST::Method visitor. * checks/errors/rust-feature-gate.h (FeatureGate::visit): Likewise.. * expand/rust-cfg-strip.cc (CfgStrip::visit): Remove AST::Method visitor, handle self_param in AST::Function visitor. * expand/rust-cfg-strip.h (CfgStrip::visit): Remove AST::Method visitor. * expand/rust-derive-clone.cc (DeriveClone::clone_fn): Return AST::Function instead of AST::Method. * expand/rust-derive.h (DeriveVisitor::visit): Remove AST::Method visitor. * expand/rust-expand-visitor.cc (ExpandVisitor::visit): Remove AST::Method visitor, handle self_param in AST::Function visitor. * expand/rust-expand-visitor.h: (ExpandVisitor::visit): Remove AST::Method visitor. * hir/rust-ast-lower-base.cc (ASTLoweringBase::visit): Likewise. * hir/rust-ast-lower-base.h (ASTLoweringBase::visit): Likewise. * hir/rust-ast-lower-implitem.h (ASTLowerImplItem::visit): Remove AST::Method visitor, handle self_param in AST::Function visitor. * parse/rust-parse-impl.h: Include optional.h. (Parser::parse_function): Adjust AST::Function construction. (Parser::parse_inherent_impl_function_or_method): Construct AST::Function instead of AST::Method, adjust AST::Function construction. (Parser::parse_trait_impl_function_or_method): Likewise. (Parser::parse_method): Return std::unique_ptr<AST::Function> instead of AST::Method. * parse/rust-parse.h (Parser::parse_method): Likewise. * resolve/rust-ast-resolve-base.cc (ResolverBase::visit): Remove AST::Method visitor. * resolve/rust-ast-resolve-base.h (ResolverBase::visit): Likewise. * resolve/rust-ast-resolve-implitem.h (ResolveToplevelImplItem::visit): Likewise. * resolve/rust-ast-resolve-item.cc (ResolveItem::visit): Remove AST::Method visitor, handle self_param in AST::Function visitor. * resolve/rust-ast-resolve-item.h (ResolveItem::visit): Remove AST::Method visitor. * resolve/rust-default-resolver.cc (DefaultResolver::visit): Remove AST::Method visitor. * resolve/rust-default-resolver.h (DefaultResolver::visit): Likewise. * resolve/rust-early-name-resolver.cc (EarlyNameResolver::visit): Remove AST::Method visitor, handle self_param in AST::Function visitor. * resolve/rust-early-name-resolver.h (EarlyNameResolver::visit): Remove AST::Method visitor. * resolve/rust-toplevel-name-resolver-2.0.cc (TopLevel::visit): Likewise. * resolve/rust-toplevel-name-resolver-2.0.h (TopLevel::visit): Likewise. * util/rust-attributes.cc (AttributeChecker::visit): Likewise. * util/rust-attributes.h (AttributeChecker::visit): Likewise. Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
2024-01-16gccrs: Parse named variadic parametersPierre-Emmanuel Patry1-1/+8
Add ability to parse named variadic parameters in extern c functions. gcc/rust/ChangeLog: * parse/rust-parse-impl.h (Parser::parse_named_function_param): Add new parsing ability. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-16gccrs: Fix multiple issues with variadic representationPierre-Emmanuel Patry1-156/+158
The new variadic representation has introduced multiple issues and ICE into the codebase. Some early passes in the compiler depend on the parameters all having a type and being an actual parameter. gcc/rust/ChangeLog: * ast/rust-ast.cc (ExternalFunctionItem::as_string): Adapt as_string function to the new ast representation. (NamedFunctionParam::as_string): Likewise. * ast/rust-item.h: Add a function to test whether a FunctionParam has a name pattern. * expand/rust-cfg-strip.cc (CfgStrip::visit): Adapt cfg strip visitor for the new variadic arguments. * hir/rust-ast-lower-extern.h: Adapt lowering to the new variadic function representation. * metadata/rust-export-metadata.cc (ExportContext::emit_function): Change call to constructor. * parse/rust-parse-impl.h (Parser::parse_named_function_param): Change NamedFunctionParam parsing to accomodate new variadic representation. (Parser::parse_external_item): Change external item parsing to use the new NamedFunctionParam variadics. * parse/rust-parse.h: Add new parsing function prototypes. * ast/rust-ast-collector.cc (TokenCollector::visit): Rework token collection to take into account variadic parameters. * ast/rust-ast-visitor.cc: Likewise. * expand/rust-expand-visitor.cc (ExpandVisitor::visit): Change function bound to avoid getting the type of a variadic parameter. * resolve/rust-ast-resolve-item.cc (ResolveExternItem::visit): Likewise. * resolve/rust-early-name-resolver.cc (EarlyNameResolver::visit): Likewise. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-16gccrs: Parse variadic functionsPierre-Emmanuel Patry1-7/+23
Variadic functions were not parsed because it is an unstable feature. While it is still unstable, it is required in order to parse libcore. gcc/rust/ChangeLog: * parse/rust-parse-impl.h (Parser::parse_function_param): Parse variadic functions. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-16gccrs: Fix token lexed as a float literalPierre-Emmanuel Patry1-0/+22
The lexer cannot distinguish the difference between a float literal and a tuple index in some cases. This means we should fix this while parsing depending on the context. gcc/rust/ChangeLog: * expand/rust-macro-invoc-lexer.cc (MacroInvocLexer::split_current_token): Add implementation for multiple token split. * expand/rust-macro-invoc-lexer.h: Add function prototype. * expand/rust-proc-macro-invoc-lexer.cc (ProcMacroInvocLexer::split_current_token): Add implementation for 2+ token split for procedural macros. * expand/rust-proc-macro-invoc-lexer.h: Add function prototype. * lex/rust-lex.cc (Lexer::split_current_token): Add function to split a token in multiple other tokens. * lex/rust-lex.h: Add function prototype for split_current_token. * parse/rust-parse-impl.h (Parser::left_denotation): Handle float tuple index identified as a float literal. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-16gccrs: Parse const with no value expressionPierre-Emmanuel Patry1-0/+10
Const with no value expression may exist either in trait or in disabled blocks. This means we should be able to parse those correctly. gcc/rust/ChangeLog: * ast/rust-item.h: Add a new constructor for const with no value expression. * parse/rust-parse-impl.h (Parser::parse_const_item): Allow const with no expression value. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-16gccrs: ast: Parse labelled blockJakub Dupak1-4/+8
gcc/rust/ChangeLog: * ast/rust-ast-builder.cc (AstBuilder::block): Add label arg to constructor call. * ast/rust-expr.h (class LoopLabel): Move before BlockExpr. (class BlockExpr): Add LoopLabel member. * expand/rust-derive-clone.cc (DeriveClone::clone_fn): Add label arg to constructor call. * parse/rust-parse-impl.h (Parser::parse_block_expr): Add label parameter. (Parser::parse_labelled_loop_expr): Add label arg to constructor call. * parse/rust-parse.h: Add label arg to constructor call. Signed-off-by: Jakub Dupak <dev@jakubdupak.com>
2024-01-16gccrs: Break OR tokens in closure parameter list contextPierre-Emmanuel Patry1-0/+5
The parser was unable to process as closure inside a closure because the lexer could not differentiate an OR from two PIPE tokens. gcc/rust/ChangeLog: * parse/rust-parse-impl.h (Parser::parse_closure_expr_pratt): Fix closure parsing function to handle consecutive parameter lists. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-16gccrs: Allow macro named macro_rulesPierre-Emmanuel Patry1-3/+14
Change the constraints around macro rules declaration in order to allow macro_rules named macro as well as tighter constraint around macro rules definitions. gcc/rust/ChangeLog: * parse/rust-parse-impl.h (Parser::is_macro_rules_def): Add a function that checks tokens given by the lexer represents an accurate macro definition. This will reduce code duplication. (Parser::parse_item): Replace condition with call to new checking function. (Parser::parse_stmt): Likewise. * parse/rust-parse.h: Add function prototype for is_macro_rules_def. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-16gccrs: Fix RangeFromExpr parsing in for loopsPierre-Emmanuel Patry1-1/+2
Those ranges were looking for a curly brace after the brace, leading to an error when using range from expr in for loops. gcc/rust/ChangeLog: * parse/rust-parse-impl.h (Parser::parse_expr): Fix range from expr. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-16gccrs: Allow call to macro named "default" at item levelPierre-Emmanuel Patry1-1/+2
The parser was too agressive and did reject any line beginning with default even if this was a macro call. gcc/rust/ChangeLog: * parse/rust-parse-impl.h (Parser::parse_item): Relax constraints around default identifier at item scope to accept "default" macros. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-16gccrs: Fix path expr segment parsing with generic pathPierre-Emmanuel Patry1-1/+3
When a token was identified as bit left shift it slipped through the parser and resulted in an error. gcc/rust/ChangeLog: * parse/rust-parse-impl.h (Parser::parse_path_expr_segment): Accept left shift tokens in order to let generic parsing function split the token. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-16gccrs: Add comma for expr delimiter to fix match armsPierre-Emmanuel Patry1-1/+1
Add a comma as an expr delimiter, this will allow correct parsing of match arm expressions. gcc/rust/ChangeLog: * parse/rust-parse-impl.h (Parser::parse_expr): Add comma delimiter. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-16gccrs: Fix pub unit type parsingPierre-Emmanuel Patry1-2/+5
Public unit types where not parsed correctly due to visibility specifiers within parenthesis. Fixes #2648. gcc/rust/ChangeLog: * parse/rust-parse-impl.h (Parser::parse_visibility): Relax constraints over public visibility return condition in order to accept pub unit types. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-16gccrs: Fix type param bound parsing with opening scopePierre-Emmanuel Patry1-0/+1
The function parsing type param bounds had a problem with scope resolution opening token. gcc/rust/ChangeLog: * parse/rust-parse-impl.h (Parser::parse_type_param_bound): Add missing case for lifetime switch. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-16gccrs: Change debug log call to as_string functionPierre-Emmanuel Patry1-1/+1
This will ensure an accurate representation of the token. Also update the as_string function to represent accurately scope resolution tokens. gcc/rust/ChangeLog: * lex/rust-token.cc (Token::as_string): Update function to output scope resolution tokens correctly. * parse/rust-parse-impl.h (Parser::parse_generic_param): Change call to as_string. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-16gccrs: Fix parser bug on tupplestruct patternJakub Dupak1-0/+2
gcc/rust/ChangeLog: * parse/rust-parse-impl.h: Add missing token consumption Signed-off-by: Jakub Dupak <dev@jakubdupak.com>
2024-01-16gccrs: Unify raw attribute valuesPierre-Emmanuel Patry1-1/+2
Attribute values were used as raw string, this is error prone and makes renaming harder. Using a constexpr instead will leverage the power of the compiler and emit an error when an incorrect builtin attribute value is used. gcc/rust/ChangeLog: * ast/rust-ast.cc (Attribute::check_cfg_predicate): Change raw string to constexpr call. (Attribute::separate_cfg_attrs): Likewise. * backend/rust-compile-base.cc (should_mangle_item): Likewise. (HIRCompileBase::setup_fndecl): Likewise. (HIRCompileBase::handle_cold_attribute_on_fndecl): Likewise. * checks/errors/privacy/rust-privacy-reporter.cc (find_proc_macro_attribute): Likewise. * checks/errors/rust-unsafe-checker.cc (check_target_attr): Likewise. * expand/rust-cfg-strip.cc (fails_cfg): Likewise. (fails_cfg_with_expand): Likewise. (expand_cfg_attrs): Likewise. * expand/rust-macro-builtins.cc: Likewise. * hir/rust-ast-lower-base.cc (ASTLoweringBase::handle_outer_attributes): Likewise. (ASTLoweringBase::lower_macro_definition): Likewise. * hir/rust-hir-dump.cc (Dump::visit): Likewise. * parse/rust-parse-impl.h (Parser::parse_doc_comment): Likewise. * parse/rust-parse.cc (extract_module_path): Likewise. * resolve/rust-early-name-resolver.cc (is_macro_use_module): Likewise. (EarlyNameResolver::visit): Likewise. * resolve/rust-toplevel-name-resolver-2.0.cc (is_macro_export): Likwise. * rust-session-manager.cc (Session::injection): Likewise. * typecheck/rust-hir-type-check-base.cc (TypeCheckBase::parse_repr_options): Likewise. * util/rust-attributes.cc (is_proc_macro_type): Likewise. (AttributeChecker::check_attribute): Likewise. (AttributeChecker::visit): Likewise. * util/rust-hir-map.cc (Mappings::insert_macro_def): Likewise. * util/rust-attribute-values.h: New file. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>