aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/ast
AgeCommit message (Collapse)AuthorFilesLines
2024-02-21Update copyright years.Sahil Yeole9-9/+9
Signed-off-by: Sahil Yeole <sahilyeole93@gmail.com>
2024-02-07gccrs: Remove obsolete classes and functions.Kushal Pal7-706/+0
Trait functions now use AST::Function class, so classes AST::TraitItemFunc, AST::TraitItemMethod, AST::TraitFunctionDecl, AST::TraitMethodDecl and their related functions can be removed. gcc/rust/ChangeLog: * ast/rust-ast-collector.cc (TokenCollector::visit): Remove obsolete classes and functions. * ast/rust-ast-collector.h: Likewise. * ast/rust-ast-full-decls.h (class TraitFunctionDecl): Likewise. (class TraitItemFunc): Likewise. (class TraitMethodDecl): Likewise. (class TraitItemMethod): Likewise. * ast/rust-ast-visitor.cc (DefaultASTVisitor::visit): Likewise. * ast/rust-ast-visitor.h: Likewise. * ast/rust-ast.cc (TraitItemFunc::TraitItemFunc): Likewise. (TraitItemFunc::operator=): Likewise. (TraitItemFunc::as_string): Likewise. (TraitFunctionDecl::as_string): Likewise. (TraitItemMethod::TraitItemMethod): Likewise. (TraitItemMethod::operator=): Likewise. (TraitItemMethod::as_string): Likewise. (TraitMethodDecl::as_string): Likewise. (TraitItemFunc::accept_vis): Likewise. (TraitItemMethod::accept_vis): Likewise. * ast/rust-item.h (class TraitFunctionDecl): Likewise. (class TraitItemFunc): Likewise. (class TraitMethodDecl): Likewise. (class TraitItemMethod): 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::maybe_strip_trait_function_decl): Likewise. (CfgStrip::maybe_strip_trait_method_decl): Likewise. (CfgStrip::visit): Likewise. * expand/rust-cfg-strip.h: Likewise. * expand/rust-derive.h: Likewise. * expand/rust-expand-visitor.cc (ExpandVisitor::expand_trait_function_decl): Likewise. (ExpandVisitor::expand_trait_method_decl): Likewise. (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. * hir/rust-ast-lower-implitem.cc (ASTLowerTraitItem::visit): Likewise. * hir/rust-ast-lower-implitem.h: Likewise. * resolve/rust-ast-resolve-base.cc (ResolverBase::visit): Likewise. * resolve/rust-ast-resolve-base.h: Likewise. * resolve/rust-ast-resolve-implitem.h (visit): Likewise. * resolve/rust-ast-resolve-item.cc (ResolveTraitItems::visit): Likewise. * resolve/rust-ast-resolve-item.h: Likewise. * resolve/rust-default-resolver.cc (DefaultResolver::visit): Likewise. * resolve/rust-default-resolver.h: Likewise. * resolve/rust-toplevel-name-resolver-2.0.cc (TopLevel::visit): Likewise. * resolve/rust-toplevel-name-resolver-2.0.h: Likewise. * util/rust-attributes.cc (AttributeChecker::visit): Likewise. * util/rust-attributes.h: Likewise. Signed-off-by: Kushal Pal <kushalpal109@gmail.com>
2024-02-07gccrs: Use AssociatedItem in place of TraitItemKushal Pal2-11/+11
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-30gccrs: Remove TraitImplItemOwen Avery3-29/+13
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: Unify storage of associated items in SingleASTNodeOwen Avery5-87/+42
gcc/rust/ChangeLog: * ast/rust-ast-fragment.cc (Fragment::assert_single_fragment): Update. * ast/rust-ast.h (class TraitImplItem): Move definition before that of TraitItem. (class TraitItem): Inherit from TraitImplItem instead of AssociatedItem. (class SingleASTNode): Unify handling of associated items. (SingleASTNode::take_assoc_item): Move from... (SingleASTNode::take_impl_item): ...here, but leave stub calling take_assoc_item behind. (SingleASTNode::take_trait_item): Cast associated item to TraitItem. (SingleASTNode::take_trait_impl_item): Cast associated item to TraitImplItem. * ast/rust-ast.cc (SingleASTNode::SingleASTNode): Unify handling of associated items. (SingleASTNode::operator=): Likewise. (SingleASTNode::accept_vis): Likewise. (SingleASTNode::is_error): Likewise. (SingleASTNode::as_string): Likewise. * ast/rust-item.h (class Function): Remove direct inheritence from AssociatedItem. (class ConstantItem): Likewise. * ast/rust-macro.h (class MacroInvocation): Remove direct inheritence from AssociatedItem and TraitImplItem.
2024-01-30gccrs: ast: Unify explicitly and implicitly elided lifettimesJakub Dupak2-1/+3
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: Change cfg stripper to use default visitorPierre-Emmanuel Patry1-10/+20
Cfg visitor used it's own visit patterns, this made the code longer than required. gcc/rust/ChangeLog: * expand/rust-cfg-strip.cc (CfgStrip::visit): Change calls from visitor to default visitor. (CfgStrip::go): Add call to visit crate. * expand/rust-cfg-strip.h (class CfgStrip): Update prototypes and remove empty ones. * ast/rust-ast-visitor.cc: add WhereClause condition check. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-30gccrs: Make function bodies truly optionalPierre-Emmanuel Patry4-25/+22
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: Split async and const function qualifiersPierre-Emmanuel Patry4-38/+23
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 Avery5-31/+16
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-0/+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: Add unsafety member to modulesPierre-Emmanuel Patry1-5/+8
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: ast: Add NodeId to UseTree base classArthur Cohen1-2/+6
gcc/rust/ChangeLog: * ast/rust-item.h (class UseTree): Add `node_id` member.
2024-01-30gccrs: Replace some weak keyword raw value with constexprPierre-Emmanuel Patry1-2/+4
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 Patry2-13/+24
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-15/+15
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: ast: Change *Path nodes APIArthur Cohen3-2/+11
gcc/rust/ChangeLog: * ast/rust-ast.h: Change Path API to be more consistent. * ast/rust-path.h: Likewise. * ast/rust-ast-collector.cc (TokenCollector::visit): Use new API. * resolve/rust-ast-resolve-item.cc (ResolveItem::visit): Likewise. * resolve/rust-ast-resolve-path.cc (ResolvePath::resolve_path): Likewise. * resolve/rust-forever-stack.hxx: Likewise.
2024-01-30gccrs: Add visibility to trait itemPierre-Emmanuel Patry2-122/+131
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: Move default visitor templates to headerPierre-Emmanuel Patry2-16/+5
Move default ast visitor template implementation to headers in order to match the codebase and avoid link errors. gcc/rust/ChangeLog: * ast/rust-ast-visitor.cc (DefaultASTVisitor::visit): Move from here... * ast/rust-ast-visitor.h: ... to here. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-16gccrs: Move templated functions to header filePierre-Emmanuel Patry2-76/+46
Templated functions shall remain in header files to stay in line with the rest of the codebase. gcc/rust/ChangeLog: * ast/rust-ast-collector.cc (TokenCollector::visit): Move to header file. (TokenCollector::visit_items_joined_by_separator): Likewise. (TokenCollector::visit_as_line): Likewise. (TokenCollector::visit_items_as_lines): Likewise. (TokenCollector::visit_items_as_block): Likewise. * ast/rust-ast-collector.h: Add implementation. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-16gccrs: Rework function special parametersPierre-Emmanuel Patry8-381/+546
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: Add trait context to ContextualASTVisitorPierre-Emmanuel Patry2-0/+11
Some construct are forbidden in trait context (eg. pub, async...) and we'll need to reject those. To do so we need to identify a trait context. gcc/rust/ChangeLog: * ast/rust-ast-visitor.cc (ContextualASTVisitor::visit): Push the new trait context when visiting a trait. * ast/rust-ast-visitor.h: Add visit function prototype and TRAIT context. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-16gccrs: Add validation pass for label namePierre-Emmanuel Patry1-1/+3
Prevent from using reserved keyword in label name. gcc/rust/ChangeLog: * ast/rust-ast-visitor.cc (DefaultASTVisitor::visit): Check if there is a label before visit. * checks/errors/rust-ast-validation.cc (ASTValidation::visit): Emit an error when a label has a forbidden name. * checks/errors/rust-ast-validation.h: Add function prototype. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-16gccrs: Use a loop label in break expressionPierre-Emmanuel Patry1-3/+3
Break expression were using a raw lifetime value instead of a loop label this behavior would have lead to some errors in ast validation. gcc/rust/ChangeLog: * ast/rust-expr.h (class BreakExpr): Change Lifetime to LoopLabel. * hir/rust-ast-lower-expr.cc (ASTLoweringExpr::visit): Lower lifetime inside the label instead. * resolve/rust-ast-resolve-expr.cc (ResolveExpr::visit): Resolve the inner lifetime. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-16gccrs: Replace AST::Method with existing AST::FunctionOwen Avery7-334/+28
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: Fix multiple issues with variadic representationPierre-Emmanuel Patry4-53/+58
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: Allow variadic NamedFunctionParamPierre-Emmanuel Patry1-3/+22
This was made to align NamedFunctionParam with FunctionParam. gcc/rust/ChangeLog: * ast/rust-item.h (class NamedFunctionParam): Add variadic boolean and another constructor. * hir/rust-ast-lower-extern.h: Avoid last parameter when variadic. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-16gccrs: Add a function to check if a function is variadicPierre-Emmanuel Patry1-0/+6
This function provides an easy way to check for a function's varidicity. gcc/rust/ChangeLog: * ast/rust-item.h: Add a getter to check if a given function is variadic. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-16gccrs: Change FunctionParam to represent variadic paramsPierre-Emmanuel Patry1-2/+27
Variadic were represented at the function level while retaining most informations of a given parameter. gcc/rust/ChangeLog: * ast/rust-item.h (class FunctionParam): Add some informations to function parameters in order to be able to store variadic argument as a function parameter. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-16gccrs: Add a new visitor that gathers context informationPierre-Emmanuel Patry2-184/+239
This visitor is intended to be used by other visitors that require context at some point for a given item. gcc/rust/ChangeLog: * ast/rust-ast-visitor.cc (ContextualASTVisitor::visit): Add multiple context saving calls. * ast/rust-ast-visitor.h (class DefaultASTVisitor): Make visit functions virtual. (class ContextualASTVisitor): Add a stack like container for the current context chain. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-16gccrs: Add a default AST visitorPierre-Emmanuel Patry3-0/+1661
This will allow us to derive other visitors from it and overload only a few selected visit methods. gcc/rust/ChangeLog: * Make-lang.in: Add the new visitor object file. * ast/rust-ast-visitor.h (class DefaultASTVisitor): Create the default visitor class. * ast/rust-ast.h: Add a new reference getter for visitor pattern. * ast/rust-ast-visitor.cc: New file. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-16gccrs: Add some reference getterPierre-Emmanuel Patry4-3/+31
Visitor pattern requires a getter to children using a mutable reference. gcc/rust/ChangeLog: * ast/rust-ast.h: Add some missing mutable reference getters. * ast/rust-expr.h: Likewise. * ast/rust-item.h: Likewise. * ast/rust-path.h: Likewise. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-16gccrs: Small fix to the ast collector visitorPierre-Emmanuel Patry1-2/+3
The parameter type was used instead of the default value. gcc/rust/ChangeLog: * ast/rust-ast-collector.cc (TokenCollector::visit): Check for presence of a type and use the default value instead of the type. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-16gccrs: Move SingleASTNode implementation out of headerPierre-Emmanuel Patry2-164/+179
Those functions implementation put additional constraints on the headers which makes the codebase harder to work with. gcc/rust/ChangeLog: * ast/rust-ast.h: Move implementation from here... * ast/rust-ast.cc (SingleASTNode::SingleASTNode): ...to here. (SingleASTNode::operator=): ...and here... (SingleASTNode::accept_vis): ...and here... (SingleASTNode::is_error): ...and here... (SingleASTNode::as_string): ...also here. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-16gccrs: Add missing override specifierPierre-Emmanuel Patry2-2/+2
Some function lacked the override specifier, this made the compiler emit several warning. gcc/rust/ChangeLog: * ast/rust-ast.h: Add override specifier. * ast/rust-item.h: Likewise. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-16gccrs: Add const getter for tokentreesPierre-Emmanuel Patry1-0/+5
We often need to retrieve the underlying tokentree without modifying it, this getter will help achieve this. gcc/rust/ChangeLog: * ast/rust-ast.h: Add const getter. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-16gccrs: Fix warning with overridden virtual methodsPierre-Emmanuel Patry1-11/+11
Overridden virtual methods were not marked as such. gcc/rust/ChangeLog: * ast/rust-pattern.h: Add override modifier to overriding methods. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-16gccrs: Parse const with no value expressionPierre-Emmanuel Patry1-0/+7
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 Dupak2-37/+44
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: Create base class AssociatedItemOwen Avery3-72/+40
gcc/rust/ChangeLog: * ast/rust-ast.h (class AssociatedItem): New, based on TraitItem, InherentImplItem, and TraitImplItem classes. (class TraitItem): Inherit from AssociatedItem. (class InherentImplItem): Likewise. (class TraitImplItem): Likewise. * ast/rust-item.h (class Method): Update cloning functions. (class Function): Likewise. (class TypeAlias): Likewise. (class ConstantItem): Likewise. (class TraitItemFunc): Likewise. (class TraitItemMethod): Likewise. (class TraitItemConst): Likewise. (class TraitItemType): Likewise. * ast/rust-macro.h (class MacroInvocation): Likewise. Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
2024-01-16gccrs: Add path to macro fragment follow restrictionsPierre-Emmanuel Patry1-0/+1
The previous follow set rules did not allow PATH fragment, changing this allow gccrs to accept more valid rust macros. gcc/rust/ChangeLog: * ast/rust-macro.h: Add PATH fragment to follow set restrictions. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-16gccrs: Replace get_pattern_node_id with get_node_idOwen Avery4-29/+3
gcc/rust/ChangeLog: * ast/rust-ast.h (Pattern::get_pattern_node_id): Rename to... (Pattern::get_node_id): ...here. * ast/rust-macro.h (MacroInvocation::get_pattern_node_id): Rename to... (MacroInvocation::get_node_id): ...here. * ast/rust-path.h (PathInExpression::get_pattern_node_id): Remove. (QualifiedPathInExpression::get_pattern_node_id): Remove. * ast/rust-pattern.h (LiteralPattern::get_pattern_node_id): Remove. (IdentifierPattern::get_pattern_node_id): Remove. (WildcardPattern::get_pattern_node_id): Remove. (RestPattern::get_pattern_node_id): Rename to... (RestPattern::get_node_id): ...here. (RangePattern::get_pattern_node_id): Remove. (ReferencePattern::get_pattern_node_id): Remove. (StructPattern::get_pattern_node_id): Remove. (TupleStructPattern::get_pattern_node_id): Remove. (TuplePattern::get_pattern_node_id): Remove. (GroupedPattern::get_pattern_node_id): Remove. (SlicePattern::get_pattern_node_id): Remove. (AltPattern::get_pattern_node_id): Remove. * resolve/rust-early-name-resolver.cc (EarlyNameResolver::visit): Use get_node_id instead of get_pattern_node_id. Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
2024-01-16gccrs: Fix spellingOwen Avery1-2/+2
gcc/rust/ChangeLog: * ast/rust-expr.h: Fix spelling of "doesn't". * backend/rust-compile-expr.cc: Fix spelling of "accessors". * backend/rust-compile-implitem.h: Fix spelling of "normal". * backend/rust-constexpr.cc: Fix spelling of "actual". Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
2024-01-16gccrs: Unify raw attribute valuesPierre-Emmanuel Patry1-2/+4
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>
2024-01-16gccrs: Move proc macro definition to mappingsPierre-Emmanuel Patry1-94/+0
This commit moves the procedural macros loaded definition from outside the AST to the mappings. This means most getters/setters around the mappings had to be changed. This commit also introduces the top level visit of those mappings instead of visiting the Crate ast members. gcc/rust/ChangeLog: * ast/rust-ast.h (class BangProcMacro): Move class from here to rust-proc-macro.h. Also remove related functions. (class AttributeProcMacro): Likewise. (class CustomDeriveProcMacro): Likewise. (struct Crate): Remove proc macro vector members. * expand/rust-macro-expand.h (struct MacroExpander): Change the type to the newly created classes. * expand/rust-proc-macro.cc (BangProcMacro::BangProcMacro): Add constructor implementation. (AttributeProcMacro::AttributeProcMacro): Likewise. (CustomDeriveProcMacro::CustomDeriveProcMacro): Likewise. * expand/rust-proc-macro.h (class BangProcMacro): Move class to here. (class AttributeProcMacro): Likewise. (class CustomDeriveProcMacro): Likewise. * resolve/rust-toplevel-name-resolver-2.0.cc (TopLevel::go): Change top level visitor to check mappings instead * rust-session-manager.cc (Session::load_extern_crate): Add back macro collection to mappings. * util/rust-hir-map.cc (Mappings::insert_derive_proc_macros): Update getter signature with new types. (Mappings::insert_bang_proc_macros): Likewise. (Mappings::insert_attribute_proc_macros): Likewise. (Mappings::lookup_derive_proc_macros): Likewise. (Mappings::lookup_bang_proc_macros): Likewise. (Mappings::lookup_attribute_proc_macros): Likewise. (Mappings::insert_derive_proc_macro_def): Likewise. (Mappings::insert_bang_proc_macro_def): Likewise. (Mappings::insert_attribute_proc_macro_def): Likewise. (Mappings::lookup_derive_proc_macro_def): Likewise. (Mappings::lookup_bang_proc_macro_def): Likewise. (Mappings::lookup_attribute_proc_macro_def): Likewise. (Mappings::insert_derive_proc_macro_invocation): Likewise. (Mappings::lookup_derive_proc_macro_invocation): Likewise. (Mappings::insert_bang_proc_macro_invocation): Likewise. (Mappings::lookup_bang_proc_macro_invocation): Likewise. (Mappings::insert_attribute_proc_macro_invocation): Likewise. (Mappings::lookup_attribute_proc_macro_invocation): Likewise. * util/rust-hir-map.h: Update function prototypes as well as map types. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-16gccrs: Add getters for different types of proc macrosPierre-Emmanuel Patry1-0/+15
Add some getters on the ast crate in order to be able to retrieve a reference to a crate's proc macros. gcc/rust/ChangeLog: * ast/rust-ast.h: Add getters. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-16gccrs: Add proc macros abstractions to crate structurePierre-Emmanuel Patry1-0/+14
Add multiple setters for a crate object in order to add macro abstractions previously introduced. gcc/rust/ChangeLog: * ast/rust-ast.h (struct Crate): Add proc macro members. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-16gccrs: Add proc macro ast representationPierre-Emmanuel Patry1-0/+65
When resolving proc macros it is convenient to store every macro directly in the extern crate. These class in the ast module provide a better abstraction over the raw ProcMacro::{CustomDerive, Bang, Attribute} structures provided by the proc_macro library. gcc/rust/ChangeLog: * ast/rust-ast.h (class BangProcMacro): Add new proc macro abstraction. (class AttributeProcMacro): Likewise. (class CustomDeriveProcMacro): Likewise. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-16gccrs: Parse unparsed derive tokenstreamsPierre-Emmanuel Patry1-0/+3
Derive attributes should be parsed before attempting to retrieve any traits. This will convert the tokenstream to a list of path if this hasn't been done previously. gcc/rust/ChangeLog: * ast/rust-ast.cc (Attribute::get_traits_to_derive): Convert tokenstream to path list. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
2024-01-16gccrs: Ease some constraints on derive definitionPierre-Emmanuel Patry1-4/+1
Member function is_derive was overly constrained, the attribute changes when we parse it's meta items and it no longer contains a tokenstream while staying a derive. gcc/rust/ChangeLog: * ast/rust-ast.cc (Attribute::is_derive): Remove tokenstream condition. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>