aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/parse/rust-parse.h
AgeCommit message (Collapse)AuthorFilesLines
2025-04-24Remove error state for GenericArgPierre-Emmanuel Patry1-1/+1
gcc/rust/ChangeLog: * ast/rust-ast-collector.cc (TokenCollector::visit): Remove error kind and change function call. * ast/rust-ast-visitor.cc (DefaultASTVisitor::visit): Change call name. * ast/rust-path.cc (ConstGenericParam::as_string): Likewise. * ast/rust-path.h: Remove error kind. * hir/rust-ast-lower-type.cc (ASTLowerGenericParam::visit): Change call name. * parse/rust-parse-impl.h (Parser::parse_generic_param): Use optional on parsing failure. (Parser::parse_generic_arg): Likewise. (Parser::parse_path_generic_args): Likewise. * parse/rust-parse.h: Likewise. * resolve/rust-ast-resolve-type.h: Change call name. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2025-04-07Change optional to expected for parse_loop_labelPierre-Emmanuel Patry1-1/+7
gcc/rust/ChangeLog: * parse/rust-parse-impl.h (Parser::parse_loop_label): Change function return type to expected. (Parser::parse_labelled_loop_expr): Adapt call location to new return type. * parse/rust-parse.h (enum class): Update function prototype. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2025-04-07Migrate error state to optionalsPierre-Emmanuel Patry1-8/+19
gcc/rust/ChangeLog: * ast/rust-ast-builder.cc (Builder::self_ref_param): Remove error state and use optional. * ast/rust-ast-visitor.cc (DefaultASTVisitor::visit): Check label before visiting. * ast/rust-ast.cc (ContinueExpr::as_string): Retrieve label value. (Lifetime::as_string): Retrieve lifetime value. (ReferenceType::as_string): Likewise. (SelfParam::as_string): Likewise. * ast/rust-ast.h: Remove lifetime and LifetimeParam error state. * ast/rust-desugar-for-loops.cc (DesugarForLoops::DesugarCtx::make_break_arm): Use optional instead of error state. * ast/rust-expr.h (class ContinueExpr): Make label optional. * ast/rust-item.h (class SelfParam): Make lifetime optional. * ast/rust-type.h (class ReferenceType): Likewise. * backend/rust-compile-base.cc: Use optional for self param instead of error state. * backend/rust-compile-base.h: Update function prototype. * expand/rust-derive-clone.cc (DeriveClone::clone_fn): Use optional. * hir/rust-ast-lower-base.cc (ASTLoweringBase::lower_self): Lower lifetime only if it exists. * hir/rust-ast-lower-block.h: Lower loop label only if it exists. * hir/rust-ast-lower-expr.cc (ASTLoweringExpr::visit): Likewise. * hir/rust-ast-lower-implitem.cc (ASTLowerImplItem::visit): Remove references to error state. (ASTLowerTraitItem::visit): Lower self param only if it exists. * hir/rust-ast-lower-item.cc (ASTLoweringItem::visit): Use nullopt for default value instead of SelfParam error state. * hir/rust-ast-lower.cc (ASTLoweringExprWithBlock::visit): Lower label only if it exists. * hir/rust-hir-dump.cc (Dump::do_traitfunctiondecl): Print self only if it exists. (Dump::visit): Liewise. * hir/tree/rust-hir-bound.h: Remove error state. * hir/tree/rust-hir-expr.cc (ContinueExpr::ContinueExpr): Use optional in constructor for loop label. (BreakExpr::BreakExpr): Likewise. * hir/tree/rust-hir-expr.h (class ContinueExpr): Remove error state implementation. (class BreakExpr): Likewise. * hir/tree/rust-hir-generic-param.h: Likewise. * hir/tree/rust-hir-item.cc (SelfParam::SelfParam): Make lifetime optional. (Function::Function): Make self param optional. * hir/tree/rust-hir-item.h (class Function): Likewise. * hir/tree/rust-hir-type.cc (ReferenceType::ReferenceType): Make lifetime optional. * hir/tree/rust-hir-type.h (class ReferenceType): Likewise. * hir/tree/rust-hir.cc (ContinueExpr::as_string): Use new getter. (BreakExpr::as_string): Likewise. (Lifetime::as_string): Likewise. (ReferenceType::as_string): Likewise. (TraitFunctionDecl::as_string): Likewise. (SelfParam::as_string): Remove error state checking. * parse/rust-parse-impl.h (Parser::parse_generic_param): Adapt to optional. (Parser::parse_lifetime_params): Likewise. (Parser::parse_lifetime_params_objs): Likewise. (Parser::parse_lifetime_param): Likewise. (Parser::parse_lifetime_where_clause_item): Likewise. (Parser::parse_type_param_bound): Likewise. (Parser::parse_lifetime_bounds): Likewise. (Parser::parse_path_generic_args): Likewise. (Parser::parse_self_param): Likewise. (Parser::parse_break_expr): Likewise. (Parser::parse_continue_expr): Likewise. (Parser::parse_reference_type_inner): Likewise. * parse/rust-parse.h (class ParseLifetimeParamError): Add new class for lifetime param parsing errors. (class ParseLifetimeError): Add new class for lifetime parsing errors. (enum ParseSelfError): Add new class for self param parsing errors. * typecheck/rust-hir-type-check-implitem.cc (TypeCheckImplItem::visit): Use unchecked getter in checked context. And make anonymous region. * typecheck/rust-hir-type-check.cc (TraitItemReference::get_type_from_fn): Likewise. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2025-04-07Make loop label truly optionalPierre-Emmanuel Patry1-6/+6
A loop label error state was in use to represent missing loop label but this may be easily forgotten and the optional nature of the label was misrepresented. gcc/rust/ChangeLog: * ast/rust-ast-builder.cc (Builder::block): Call with a nullopt instead of an error loop label. (WhileLetLoopExpr::as_string): Use getter function and adapt to newtype. * ast/rust-ast.cc (WhileLoopExpr::as_string): Likewise. (LoopExpr::as_string): Likewise. (BreakExpr::as_string): Likewise. (ForLoopExpr::as_string): Likewise. * ast/rust-expr.h (class BlockExpr): Make loop label optional. (class BreakExpr): Likewise. * expand/rust-derive-clone.cc (DeriveClone::clone_fn): Use nullopt. * expand/rust-derive-debug.cc (DeriveDebug::stub_debug_fn): Likewise. * expand/rust-derive-default.cc (DeriveDefault::default_fn): Likewise. * expand/rust-derive-eq.cc: Likewise. * parse/rust-parse-impl.h (Parser::parse_block_expr): Use optional for arguments. (Parser::parse_loop_expr): Likewise. (Parser::parse_while_loop_expr): Likewise. (Parser::parse_while_let_loop_expr): Likewise. (Parser::parse_for_loop_expr): Likewise. (Parser::parse_labelled_loop_expr): Likewise. (Parser::parse_loop_label): Return an optional. * parse/rust-parse.h: Update function prototype and use nullopt for default values. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2025-01-13Remove dead code related to external functionsOwen Avery1-4/+0
gcc/rust/ChangeLog: * ast/rust-ast-collector.cc (TokenCollector::visit): Remove visitor for NamedFunctionParam. * ast/rust-ast-collector.h (TokenCollector::visit): Likewise. * ast/rust-ast-full-decls.h (class NamedFunctionParam): Remove forward declaration. * ast/rust-ast-visitor.cc (DefaultASTVisitor::visit): Remove visitor for NamedFunctionParam. * ast/rust-ast-visitor.h (DefaultASTVisitor::visit): Likewise. * ast/rust-ast.cc (NamedFunctionParam::as_string): Remove. * ast/rust-item.h (class NamedFunctionParam): Remove. (class ExternalFunctionItem): Remove. * parse/rust-parse-impl.h (Parser::parse_named_function_param): Remove. (Parser::parse_named_function_params): Remove. * parse/rust-parse.h (Parser::parse_named_function_param): Remove. (Parser::parse_named_function_params): Remove. Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
2024-06-11Parse box expressionsPierre-Emmanuel Patry1-0/+3
Add support for old box expression syntax. gcc/rust/ChangeLog: * ast/rust-ast-collector.cc (TokenCollector::visit): Add visit member function for BoxExpr nodes. * ast/rust-ast-collector.h: Add visit function prototype. * ast/rust-ast-visitor.cc (DefaultASTVisitor::visit): Add visit member function to default ast visitor. * ast/rust-ast-visitor.h: Add visit function's prototype. * ast/rust-ast.cc (BoxExpr::as_string): Add as_string function implementation for BoxExpr. (BoxExpr::accept_vis): Add accept_vis implementation to BoxExpr. * ast/rust-expr.h (class BoxExpr): Add BoxExpr class to represent boxed expressions. * expand/rust-derive.h: Add BoxExpr visit function prototype. * hir/rust-ast-lower-base.cc (ASTLoweringBase::visit): Add BoxExpr visitor implementation. * hir/rust-ast-lower-base.h: Add visit function's prototype. * hir/rust-ast-lower-expr.cc (ASTLoweringExpr::visit): Add BoxExpr visitor implementation. * hir/rust-ast-lower-expr.h: Add visit function's prototype. * parse/rust-parse-impl.h (Parser::parse_box_expr): Add parse_box_expr function's implementation. * parse/rust-parse.h: Add parse_box_expr function's prototype. * resolve/rust-ast-resolve-base.cc (ResolverBase::visit): Add resolver visit implementation. * resolve/rust-ast-resolve-base.h: Add resolver's visit function prototype. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-03-05Remove dead code associated with `AST::ExternalFunctionItem`0xn4utilus1-2/+0
gcc/rust/ChangeLog: * ast/rust-ast-collector.cc (TokenCollector::visit): Remove dead code. * ast/rust-ast-collector.h: Likewise. * ast/rust-ast-full-decls.h (class ExternalFunctionItem): Likewise. * ast/rust-ast-visitor.cc (DefaultASTVisitor::visit): Likewise. * ast/rust-ast-visitor.h: Likewise. * ast/rust-ast.cc (ExternalFunctionItem::as_string): Likewise. (ExternalFunctionItem::accept_vis): Likewise. * checks/errors/rust-ast-validation.cc (ASTValidation::visit): Likewise. * checks/errors/rust-ast-validation.h: Likewise. * checks/errors/rust-feature-gate.h: Likewise. * expand/rust-cfg-strip.cc (CfgStrip::visit): Likewise. * expand/rust-cfg-strip.h: Likewise. * expand/rust-derive.h: Likewise. * expand/rust-expand-visitor.cc (ExpandVisitor::visit): Likewise. * expand/rust-expand-visitor.h: Likewise. * hir/rust-ast-lower-base.cc (ASTLoweringBase::visit): Likewise. * hir/rust-ast-lower-base.h: Likewise. * metadata/rust-export-metadata.cc (ExportContext::emit_function): Likewise. * parse/rust-parse-impl.h: Likewise. * parse/rust-parse.h: Likewise. * resolve/rust-ast-resolve-base.cc (ResolverBase::visit): Likewise. * resolve/rust-ast-resolve-base.h: Likewise. * resolve/rust-default-resolver.cc (DefaultResolver::visit): Likewise. * resolve/rust-default-resolver.h: Likewise. * util/rust-attributes.cc (AttributeChecker::visit): Likewise. * util/rust-attributes.h: Likewise. gcc/testsuite/ChangeLog: * rust/compile/extern_func_with_body.rs: New test. Signed-off-by: 0xn4utilus <gyanendrabanjare8@gmail.com>
2024-03-05Unify ASTValidation::visit for ExternalFunctionItem and Function0xn4utilus1-1/+2
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-03-01parser: Add peek(n) method to parserArthur Cohen1-0/+1
gcc/rust/ChangeLog: * parse/rust-parse.h: New method.
2024-02-05Use AssociatedItem in place of TraitItemKushal Pal1-1/+1
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-01-24Remove TraitImplItemOwen Avery1-2/+2
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-22AST: Fix for lifetime parsingJakub Dupak1-2/+4
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>
2023-12-27ast: Full lifetime elision handlingJakub Dupak1-1/+1
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>
2023-11-30Remove class AST::InherentImplItemOwen Avery1-2/+2
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>
2023-11-21Added support to Parse ASYNC functionM V V S Manoj Kumar1-0/+2
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>
2023-11-21Report self parameter parsing error kindPierre-Emmanuel Patry1-1/+11
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>
2023-11-21Add visibility to trait itemPierre-Emmanuel Patry1-1/+2
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>
2023-11-14Rework function special parametersPierre-Emmanuel Patry1-15/+15
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>
2023-11-09Replace AST::Method with existing AST::FunctionOwen Avery1-1/+1
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>
2023-11-09Fix multiple issues with variadic representationPierre-Emmanuel Patry1-2/+7
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>
2023-10-20ast: Parse labelled blockJakub Dupak1-3/+4
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>
2023-10-17Allow macro named macro_rulesPierre-Emmanuel Patry1-0/+1
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>
2023-07-21Replace Location with location_t in parsing and macro expansionOwen Avery1-16/+16
gcc/rust/ChangeLog: * expand/rust-derive-clone.cc: Replace Location with location_t. * expand/rust-derive-clone.h: Likewise. * expand/rust-derive-copy.cc: Likewise. * expand/rust-derive-copy.h: Likewise. * expand/rust-derive.cc: Likewise. * expand/rust-derive.h: Likewise. * expand/rust-macro-builtins.cc: Likewise. * expand/rust-macro-builtins.h: Likewise. * expand/rust-macro-expand.cc: Likewise. * parse/rust-parse-impl.h: Likewise. * parse/rust-parse.cc: Likewise. * parse/rust-parse.h: Likewise. Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
2023-07-17Replace some usages of Location with location_tOwen Avery1-2/+2
gcc/rust/ChangeLog: * ast/rust-ast-collector.cc: Replace Location with location_t. * ast/rust-ast.cc: Likewise. * ast/rust-ast.h: Likewise. * ast/rust-expr.h: Likewise. * ast/rust-item.h: Likewise. * ast/rust-macro.h: Likewise. * ast/rust-path.cc: Likewise. * ast/rust-path.h: Likewise. * ast/rust-pattern.h: Likewise. * ast/rust-stmt.h: Likewise. * ast/rust-type.h: Likewise. * backend/rust-compile-base.cc: Likewise. * backend/rust-compile-base.h: Likewise. * backend/rust-compile-block.cc: Likewise. * backend/rust-compile-expr.cc: Likewise. * backend/rust-compile-expr.h: Likewise. * backend/rust-compile-fnparam.cc: Likewise. * backend/rust-compile-fnparam.h: Likewise. * backend/rust-compile-intrinsic.cc: Likewise. * backend/rust-compile-pattern.cc: Likewise. * backend/rust-compile-resolve-path.h: Likewise. * backend/rust-compile.cc: Likewise. * checks/errors/rust-const-checker.cc: Likewise. * checks/errors/rust-const-checker.h: Likewise. * checks/errors/rust-unsafe-checker.cc: Likewise. * checks/errors/rust-unsafe-checker.h: Likewise. * expand/rust-macro-builtins.cc: Likewise. * expand/rust-macro-expand.h: Likewise. * hir/rust-ast-lower-base.h: Likewise. * hir/rust-ast-lower-implitem.h: Likewise. * hir/rust-ast-lower-item.cc: Likewise. * hir/tree/rust-hir-expr.h: Likewise. * hir/tree/rust-hir-item.h: Likewise. * hir/tree/rust-hir-path.h: Likewise. * hir/tree/rust-hir-pattern.h: Likewise. * hir/tree/rust-hir-stmt.h: Likewise. * hir/tree/rust-hir-type.h: Likewise. * hir/tree/rust-hir.cc: Likewise. * hir/tree/rust-hir.h: Likewise. * lex/rust-token.h: Likewise. * metadata/rust-extern-crate.cc: Likewise. * metadata/rust-extern-crate.h: Likewise. * parse/rust-parse-impl.h: Likewise. * parse/rust-parse.h: Likewise. * resolve/rust-ast-resolve-expr.cc: Likewise. * resolve/rust-ast-resolve-implitem.h: Likewise. * resolve/rust-ast-resolve-pattern.h: Likewise. * resolve/rust-ast-resolve-stmt.h: Likewise. * resolve/rust-ast-resolve-toplevel.h: Likewise. * resolve/rust-ast-resolve-type.h: Likewise. * resolve/rust-name-resolver.cc: Likewise. * resolve/rust-name-resolver.h: Likewise. * rust-diagnostics.cc: Likewise. * rust-diagnostics.h: Likewise. * rust-gcc.cc: Likewise. * rust-session-manager.cc: Likewise. * rust-session-manager.h: Likewise. * typecheck/rust-casts.cc: Likewise. * typecheck/rust-casts.h: Likewise. * typecheck/rust-coercion.cc: Likewise. * typecheck/rust-coercion.h: Likewise. * typecheck/rust-hir-path-probe.cc: Likewise. * typecheck/rust-hir-path-probe.h: Likewise. * typecheck/rust-hir-trait-reference.cc: Likewise. * typecheck/rust-hir-trait-reference.h: Likewise. * typecheck/rust-hir-trait-resolve.cc: Likewise. * typecheck/rust-hir-type-check-base.cc: Likewise. * typecheck/rust-hir-type-check-base.h: Likewise. * typecheck/rust-hir-type-check-item.cc: Likewise. * typecheck/rust-hir-type-check-item.h: Likewise. * typecheck/rust-hir-type-check-path.cc: Likewise. * typecheck/rust-hir-type-check-pattern.cc: Likewise. * typecheck/rust-hir-type-check-pattern.h: Likewise. * typecheck/rust-hir-type-check-type.cc: Likewise. * typecheck/rust-hir-type-check-type.h: Likewise. * typecheck/rust-hir-type-check.cc: Likewise. * typecheck/rust-hir-type-check.h: Likewise. * typecheck/rust-substitution-mapper.cc: Likewise. * typecheck/rust-substitution-mapper.h: Likewise. * typecheck/rust-type-util.cc: Likewise. * typecheck/rust-typecheck-context.cc: Likewise. * typecheck/rust-tyty-bounds.cc: Likewise. * typecheck/rust-tyty-call.h: Likewise. * typecheck/rust-tyty-subst.cc: Likewise. * typecheck/rust-tyty-subst.h: Likewise. * typecheck/rust-tyty-util.cc: Likewise. * typecheck/rust-tyty-util.h: Likewise. * typecheck/rust-tyty.cc: Likewise. * typecheck/rust-tyty.h: Likewise. * typecheck/rust-unify.cc: Likewise. * typecheck/rust-unify.h: Likewise. * util/rust-hir-map.cc: Likewise. * util/rust-hir-map.h: Likewise. * util/rust-identifier.h: Likewise. Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
2023-07-10Replace Linemap::unknown_location with UNKNOWN_LOCATIONOwen Avery1-20/+15
gcc/rust/ChangeLog: * rust-linemap.h (Linemap::unknown_location): Remove. * ast/rust-ast.cc: Replace Linemap::unknown_location with UNKNOWN_LOCATION. * ast/rust-path.cc: Likewise. * hir/tree/rust-hir.cc: Likewise. * metadata/rust-imports.cc: Likewise. * parse/rust-parse-impl.h: Likewise. * parse/rust-parse.h: Likewise. * rust-backend.h: Likewise. * rust-diagnostics.cc: Likewise. * rust-session-manager.cc: Likewise. * typecheck/rust-tyty.cc: Likewise. Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
2023-06-27Match tokens in macros more closelyOwen Avery1-0/+10
gcc/rust/ChangeLog: * expand/rust-macro-expand.cc (MacroExpander::match_token): Match token instead of token id. * parse/rust-parse-impl.h (Parser::skip_token): Add token-skipping variant. (Parser::expect_token): Likewise. * parse/rust-parse.h (Parser::skip_token): Likewise. (Parser::expect_token): Likewise. gcc/testsuite/ChangeLog: * rust/compile/macro-issue2264.rs: New test. Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
2023-06-20gccrs: Parse statement macros as statements.Matthew Jasper1-5/+14
gcc/rust/ChangeLog: * ast/rust-ast.h (Expr::to_stmt): Remove method. * ast/rust-macro.h (MacroInvocation::to_stmt): Remove override. * ast/rust-stmt.h: Remove use of Expr::to_stmt. * 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 (transcribe_on_delimiter): Remove function. (transcribe_context): Use EXPR/STMT contexts. (MacroExpander::parse_proc_macro_output): Use EXPR/STMT contexts. * parse/rust-parse-impl.h (Parser::parse_stmt): Delegate macro parsing to parse_expr_stmt, check for ! after macro_rules. (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. 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. Signed-off-by: Matthew Jasper <mjjasper1@gmail.com>
2023-06-20gccrs: Parse semicolons in more cases for statement macrosMatthew Jasper1-0/+3
gcc/rust/ChangeLog: * ast/rust-ast.h (MacroInvocation::add_semicolon): New method. * ast/rust-macro.h (MacroInvocation::add_semicolon): Add override. * ast/rust-stmt.h (ExprStmt::add_semicolon): Add override. * expand/rust-macro-expand.cc (parse_many): Pass enum by value. (transcribe_many_stmts): Parse statements with semicolons. * parse/rust-parse-impl.h (Parser::parse_let_stmt): Work around lack of NT tokens. (Parser::parse_expr_stmt): Handle statements at end of macro expansions. * parse/rust-parse.h (struct ParseRestrictions): Additional flag. gcc/testsuite/ChangeLog: * rust/compile/macro53.rs: New test. Signed-off-by: Matthew Jasper <mjjasper1@gmail.com>
2023-06-01Properly match delimitersOwen Avery1-0/+5
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>
2023-05-15gccrs: Fix parsing of block expressions followed by `.`Matthew Jasper1-17/+5
`{ ... }.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>
2023-05-15ast: Remove ExprStmtWithBlock / ExprStmtWithoutBlock distinctionMatthew Jasper1-2/+2
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>
2023-05-12Handle keywords in macro fragmentsOwen Avery1-0/+1
gcc/rust/ChangeLog: * lex/rust-token.cc (token_id_is_keyword): New. (token_id_keyword_string): New. * lex/rust-token.h (token_id_is_keyword): New. (token_id_keyword_string): New. * expand/rust-macro-expand.cc (MacroExpander::match_fragment): Match keywords for ident fragment. * parse/rust-parse-impl.h (Parser::parse_identifier_or_keyword_token): Add. * parse/rust-parse.h (Parser::parse_identifier_or_keyword_token): Add. gcc/testsuite/ChangeLog: * rust/compile/macro-issue2192.rs: New test. Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
2023-04-07ast: Add difference between attributesPierre-Emmanuel Patry1-2/+4
Add a boolean to tell inner and outer attributes ast nodes appart. This meant refactoring a bit their parsing function. gcc/rust/ChangeLog: * ast/rust-ast.h: Add boolean for differenciation. * parse/rust-parse-impl.h (Parser::parse_doc_comment): Change function interface to make code cleaner. It should return a body instead of the whole attribute. (Parser::parse_inner_attribute): Specify the inner status of the node. (Parser::parse_attribute_body): Change function interface to make the code cleaner much like parse_doc_comment. (Parser::parse_outer_attribute): Specify outer status of the node. * parse/rust-parse.h: Update functions prototypes. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2023-02-28parser: Parse external type itemPierre-Emmanuel Patry1-0/+2
Add the code to parse type item declaration within an extern block. gcc/rust/ChangeLog: * parse/rust-parse-impl.h (Parser::parse_external_type_item): Add function to parser an external type item. (Parser::parse_external_item): Add identification and parsing for external type items. * parse/rust-parse.h: Add parser_external_type_item prototype. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2023-02-20Parse AltPatternOwen Avery1-0/+1
Renamed Parser::parse_pattern to Parser::parse_pattern_no_alt and created new method Parser::parse_pattern to handle alternate patterns. gcc/rust/ChangeLog: * parse/rust-parse-impl.h (Parser::parse_pattern): Add. (Parser::parse_pattern_no_alt): Rename. * parse/rust-parse.h: (Parser::parse_pattern): Add. (Parser::parse_pattern_no_alt): Rename. gcc/testsuite/ChangeLog: * rust/compile/pattern-or.rs: New test. Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
2023-02-08parser: Expose parse_macro_invocation as public APIArthur Cohen1-2/+2
gcc/rust/ChangeLog: * parse/rust-parse.h: Move `parse_macro_invocation` to public API.
2023-02-01parser: Allow parsing multiple reference typesArthur Cohen1-0/+2
The parser now recursively tries to parse a reference type after seeing a `&` or `&&` token. gcc/rust/ChangeLog: * parse/rust-parse-impl.h (Parser::parse_type): Handle double ampersan properly (Parser::parse_reference_type): Call into `parse_reference_type_inner` and wrap double reference types in another `AST::ReferenceType` node (Parser::parse_reference_type_inner): Add parsing implementation which does not care about the leading token (& or &&) (Parser::parse_type_no_bounds): Handle double ampersand properly * parse/rust-parse.h: Declare `parse_reference_type_inner` gcc/testsuite/ChangeLog: * rust/compile/multi_reference_type.rs: New test.
2023-01-10Implement declarative macro 2.0 parserRaiki Tamura1-1/+2
Signed-off-by: Raiki Tamura <tamaron1203@gmail.com>
2022-11-19Improve lexer dumpRaiki Tamura1-2/+0
2022-07-18Ensure we parse any inner item attributes when expanding a modulePhilip Herron1-1/+1
When compiling code containing module expansions we must first ensure any possible inner item attributes are parser first and applied to the module, otherwise we end up in a bad state and ignore attributes. Fixes #1089
2022-07-08Refactor mappings class and HIR lowering to be consistentPhilip Herron1-1/+1
In order to support loading extern crates and use statements we needed to clarify the usage of NodeId and HirId within gccrs. Each of these id's were nested behind the CrateNum but the type resolution, linting and code-gen passes do not support that level of nesting. In order to get metadata exports and imports working lets focus on gccrs supporting compilation of a single crate at a time. This means the crate prefix only matters for imports and limits the complexity here. Down the line there might be a way to leverage DefId's for all Path resolution which could solve this problem but significant refactoring would be required here to do this properly and its not nessecary for a basic working rust compiler.
2022-07-07Merge #1363bors[bot]1-2/+2
1363: Refactor Lexer to support an abstract InputSource class r=philberty a=philberty This patch allows us to remove the fmemopen lex_string hack to support parsing buffers. This will allow us to support mutliple sources such as metadata imports etc. The patch here updates the parser to hold onto a reference to the lexer rather than 'owning' the lexer which allows us to decouple the move semantics here. Fixes #1203 Fixes #1000 Co-authored-by: Philip Herron <philip.herron@embecosm.com>
2022-07-07Refactor Lexer to support an abstract InputSource classPhilip Herron1-2/+2
This patch allows us to remove the fmemopen lex_string hack to support parsing buffers. This will allow us to support mutliple sources such as metadata imports etc. The patch here updates the parser to hold onto a reference to the lexer rather than 'owning' the lexer which allows us to decouple the move semantics here. Fixes #1203 #1000
2022-07-07ast: Rename ConstGenericArg -> GenericArgArthur Cohen1-1/+1
This makes the class clearer and exposes the fact that it may contain a type OR a const value clearer. Because we now run in ambiguous cases later in the compiler pipeline, this commit also adds horrible hacks to simply ignore ambiguous generic arguments and treat them as type arguments.
2022-06-17ast: Add const generic argument application typeArthur Cohen1-1/+1
This commit adds a new type to the AST which contains const generic arguments. The `GenericArgs` structure now also keeps a vector of them, and the parser was modified accordingly. We need to handle these extra arguments in the various resolvers and type-checking phase. As pointed out before, const generic arguments are ambiguous at the AST level. Which is why this "sum type" contains a "Clear" variant when there is no ambiguity at all (`<5>`, `<{ 5 }>`, `<{ 5 + 15 }>` or `<{ N }>`) and an "Ambiguous" one if the value in question might be referring to a type argument (`<N>`) instead of a const generic argument. However, when parsing a const generic declaration, we can also have a default value. Thus, we need a mechanism to disambiguate `ConstGenericArg`s early.
2022-06-15parser: Add base for parsing const generic applicationArthur Cohen1-1/+2
1. Refactor const generic declaration The default value for const generics now benefits from using the same function as parsing a regular const generic expression 2. `Parser::parse_type` should not always add errors In the case that we are parsing a const generic and not a type, we should not emit bogus errors from `parse_type` such as "unexpected token in type: LITERAL". Thus, we add a flag to the function to not always add errors to the error table 3. Figure out how to deal with ambiguities In the following cases, parsing is ambiguous: ```rust let a: Foo<N>; ``` What is N? Is it a type to be used as a generic argument? Is it a const value to be used for a const generic argument? We need to keep both possibilities and differentiate later during typechecking. We need to figure out if it would be better to keep the ambiguity in our future `ConstGenericArg` type (something like Kind::ConstVarOrType) or modify our current `AST::Type` to maybe get differentiated later as a const variable, which seems more annoying. Finally, since the const evaluation is not implemented yet, we are getting some bogus errors in the testcase. This commit simply serves as a necessary base: parsing const generics before we can apply them.
2022-06-14parser: Refactor generic parameter parsingArthur Cohen1-1/+3
This allows us to parse lifetimes and types (and later const generics) in any order without necessarily erroring out. It also simplifies the code greatly and makes it easier to modify. Finally, this commit removes an unused duplicate parse_generic_params function
2022-04-12rust: Allow gccrs to build on x86_64-apple-darwin with clang/libc++Simon Cook1-0/+5
This makes changes to the includes such that gccrs can be built with clang/libc++, and for x86_64-apple-darwin. Similarly, a couple of changes have been made to creating MacroInvocations and wrapping into ExprOrStmts for libc++ compatibility.
2022-03-25Merge #1063bors[bot]1-1/+1
1063: Handle :meta fragments properly r=CohenArthur a=CohenArthur This expands :meta fragments properly and allows us to strip assignment expressions Co-authored-by: Arthur Cohen <arthur.cohen@embecosm.com>
2022-03-25macros: Parse :meta properlyArthur Cohen1-1/+1
This allows us to match attribute bodies in macro invocations, which we can use later down the line to perform conditional compilation