diff options
Diffstat (limited to 'gcc/rust/hir')
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-base.cc | 8 | ||||
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-block.h | 4 | ||||
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-expr.cc | 21 | ||||
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-implitem.cc | 29 | ||||
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-item.cc | 5 | ||||
-rw-r--r-- | gcc/rust/hir/rust-ast-lower.cc | 11 | ||||
-rw-r--r-- | gcc/rust/hir/rust-hir-dump.cc | 6 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-bound.h | 12 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-expr.cc | 24 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-expr.h | 51 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-generic-param.h | 3 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-item.cc | 28 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-item.h | 61 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-type.cc | 2 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-type.h | 9 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir.cc | 106 |
16 files changed, 204 insertions, 176 deletions
diff --git a/gcc/rust/hir/rust-ast-lower-base.cc b/gcc/rust/hir/rust-ast-lower-base.cc index b0d347e..5039798 100644 --- a/gcc/rust/hir/rust-ast-lower-base.cc +++ b/gcc/rust/hir/rust-ast-lower-base.cc @@ -690,8 +690,12 @@ ASTLoweringBase::lower_self (AST::Param ¶m) self.get_is_mut (), self.get_locus ()); } - AST::Lifetime l = self.get_lifetime (); - return HIR::SelfParam (mapping, lower_lifetime (l), self.get_is_mut (), + tl::optional<HIR::Lifetime> lifetime = tl::nullopt; + + if (self.has_lifetime ()) + lifetime = lower_lifetime (self.get_lifetime ()); + + return HIR::SelfParam (mapping, lifetime, self.get_is_mut (), self.get_locus ()); } diff --git a/gcc/rust/hir/rust-ast-lower-block.h b/gcc/rust/hir/rust-ast-lower-block.h index a39c010..93cd443 100644 --- a/gcc/rust/hir/rust-ast-lower-block.h +++ b/gcc/rust/hir/rust-ast-lower-block.h @@ -195,7 +195,9 @@ public: HIR::BlockExpr *loop_block = ASTLoweringBlock::translate (expr.get_loop_block (), &terminated); - HIR::LoopLabel loop_label = lower_loop_label (expr.get_loop_label ()); + tl::optional<HIR::LoopLabel> loop_label = tl::nullopt; + if (expr.has_loop_label ()) + loop_label = lower_loop_label (expr.get_loop_label ()); auto crate_num = mappings.get_current_crate (); Analysis::NodeMapping mapping (crate_num, expr.get_node_id (), diff --git a/gcc/rust/hir/rust-ast-lower-expr.cc b/gcc/rust/hir/rust-ast-lower-expr.cc index 9f363c0..3784e74 100644 --- a/gcc/rust/hir/rust-ast-lower-expr.cc +++ b/gcc/rust/hir/rust-ast-lower-expr.cc @@ -597,8 +597,10 @@ ASTLoweringExpr::visit (AST::ForLoopExpr &expr) void ASTLoweringExpr::visit (AST::BreakExpr &expr) { - HIR::Lifetime break_label - = lower_lifetime (expr.get_label ().get_lifetime ()); + tl::optional<HIR::Lifetime> break_label = tl::nullopt; + if (expr.has_label ()) + break_label = lower_lifetime (expr.get_label_unchecked ().get_lifetime ()); + HIR::Expr *break_expr = expr.has_break_expr () ? ASTLoweringExpr::translate (expr.get_break_expr ()) @@ -618,7 +620,9 @@ ASTLoweringExpr::visit (AST::BreakExpr &expr) void ASTLoweringExpr::visit (AST::ContinueExpr &expr) { - HIR::Lifetime break_label = lower_lifetime (expr.get_label ()); + tl::optional<HIR::Lifetime> break_label; + if (expr.has_label ()) + break_label = lower_lifetime (expr.get_label_unchecked ()); auto crate_num = mappings.get_current_crate (); Analysis::NodeMapping mapping (crate_num, expr.get_node_id (), @@ -633,9 +637,6 @@ ASTLoweringExpr::visit (AST::ContinueExpr &expr) void ASTLoweringExpr::visit (AST::BorrowExpr &expr) { - if (expr.is_raw_borrow ()) - rust_unreachable (); - HIR::Expr *borrow_lvalue = ASTLoweringExpr::translate (expr.get_borrowed_expr ()); @@ -646,8 +647,8 @@ ASTLoweringExpr::visit (AST::BorrowExpr &expr) auto *borrow_expr = new HIR::BorrowExpr (mapping, std::unique_ptr<HIR::Expr> (borrow_lvalue), - expr.get_mutability (), expr.get_outer_attrs (), - expr.get_locus ()); + expr.get_mutability (), expr.is_raw_borrow (), + expr.get_outer_attrs (), expr.get_locus ()); if (expr.get_is_double_borrow ()) { @@ -659,8 +660,8 @@ ASTLoweringExpr::visit (AST::BorrowExpr &expr) borrow_expr = new HIR::BorrowExpr (mapping, std::unique_ptr<HIR::Expr> (borrow_expr), - expr.get_mutability (), expr.get_outer_attrs (), - expr.get_locus ()); + expr.get_mutability (), expr.is_raw_borrow (), + expr.get_outer_attrs (), expr.get_locus ()); } translated = borrow_expr; diff --git a/gcc/rust/hir/rust-ast-lower-implitem.cc b/gcc/rust/hir/rust-ast-lower-implitem.cc index 5380d25..d815a71 100644 --- a/gcc/rust/hir/rust-ast-lower-implitem.cc +++ b/gcc/rust/hir/rust-ast-lower-implitem.cc @@ -22,6 +22,7 @@ #include "rust-ast-lower-expr.h" #include "rust-ast-lower-pattern.h" #include "rust-ast-lower-block.h" +#include "rust-hir-item.h" #include "rust-item.h" namespace Rust { @@ -131,7 +132,7 @@ ASTLowerImplItem::visit (AST::Function &function) Identifier function_name = function.get_function_name (); location_t locus = function.get_locus (); - HIR::SelfParam self_param = HIR::SelfParam::error (); + tl::optional<HIR::SelfParam> self_param = tl::nullopt; if (function.has_self_param ()) self_param = lower_self (function.get_self_param ()); @@ -140,6 +141,9 @@ ASTLowerImplItem::visit (AST::Function &function) ASTLoweringType::translate (function.get_return_type ())) : nullptr; + Defaultness defaultness + = function.is_default () ? Defaultness::Default : Defaultness::Final; + std::vector<HIR::FunctionParam> function_params; for (auto &p : function.get_function_params ()) { @@ -183,15 +187,15 @@ ASTLowerImplItem::visit (AST::Function &function) std::move (function_params), std::move (return_type), std::move (where_clause), std::move (function_body), std::move (vis), function.get_outer_attrs (), - std::move (self_param), locus); + std::move (self_param), defaultness, locus); - if (!fn->get_self_param ().is_error ()) + if (fn->is_method ()) { // insert mappings for self - mappings.insert_hir_self_param (&fn->get_self_param ()); + mappings.insert_hir_self_param (&fn->get_self_param_unchecked ()); mappings.insert_location ( - fn->get_self_param ().get_mappings ().get_hirid (), - fn->get_self_param ().get_locus ()); + fn->get_self_param_unchecked ().get_mappings ().get_hirid (), + fn->get_self_param_unchecked ().get_locus ()); } // add the mappings for the function params at the end @@ -245,9 +249,9 @@ ASTLowerTraitItem::visit (AST::Function &func) // set self parameter to error if this is a method // else lower to hir - HIR::SelfParam self_param = func.has_self_param () - ? lower_self (func.get_self_param ()) - : HIR::SelfParam::error (); + tl::optional<HIR::SelfParam> self_param = tl::nullopt; + if (func.has_self_param ()) + self_param = lower_self (func.get_self_param ()); std::vector<HIR::FunctionParam> function_params; for (auto &p : func.get_function_params ()) @@ -299,9 +303,10 @@ ASTLowerTraitItem::visit (AST::Function &func) if (func.has_self_param ()) { // insert mappings for self - mappings.insert_hir_self_param (&self_param); - mappings.insert_location (self_param.get_mappings ().get_hirid (), - self_param.get_locus ()); + // TODO: Is this correct ? Looks fishy + mappings.insert_hir_self_param (&*self_param); + mappings.insert_location (self_param->get_mappings ().get_hirid (), + self_param->get_locus ()); } // add the mappings for the function params at the end diff --git a/gcc/rust/hir/rust-ast-lower-item.cc b/gcc/rust/hir/rust-ast-lower-item.cc index 5dbcad5..f4396b5 100644 --- a/gcc/rust/hir/rust-ast-lower-item.cc +++ b/gcc/rust/hir/rust-ast-lower-item.cc @@ -451,13 +451,16 @@ ASTLoweringItem::visit (AST::Function &function) mappings.insert_location (function_body->get_mappings ().get_hirid (), function.get_locus ()); + Defaultness defaultness + = function.is_default () ? Defaultness::Default : Defaultness::Final; + auto fn = new HIR::Function (mapping, std::move (function_name), std::move (qualifiers), std::move (generic_params), std::move (function_params), std::move (return_type), std::move (where_clause), std::move (function_body), std::move (vis), function.get_outer_attrs (), - HIR::SelfParam::error (), locus); + tl::nullopt, defaultness, locus); // add the mappings for the function params at the end for (auto ¶m : fn->get_function_params ()) diff --git a/gcc/rust/hir/rust-ast-lower.cc b/gcc/rust/hir/rust-ast-lower.cc index ebdf981..76bd135 100644 --- a/gcc/rust/hir/rust-ast-lower.cc +++ b/gcc/rust/hir/rust-ast-lower.cc @@ -97,7 +97,11 @@ ASTLowering::go () void ASTLoweringBlock::visit (AST::BlockExpr &expr) { - auto label = lower_loop_label (expr.get_label ()); + tl::optional<HIR::LoopLabel> label; + if (expr.has_label ()) + label = lower_loop_label (expr.get_label ()); + else + label = tl::nullopt; std::vector<std::unique_ptr<HIR::Stmt>> block_stmts; bool block_did_terminate = false; @@ -398,7 +402,10 @@ ASTLoweringExprWithBlock::visit (AST::WhileLoopExpr &expr) HIR::BlockExpr *loop_block = ASTLoweringBlock::translate (expr.get_loop_block (), &terminated); - HIR::LoopLabel loop_label = lower_loop_label (expr.get_loop_label ()); + tl::optional<HIR::LoopLabel> loop_label; + if (expr.has_loop_label ()) + loop_label = lower_loop_label (expr.get_loop_label ()); + HIR::Expr *loop_condition = ASTLoweringExpr::translate (expr.get_predicate_expr (), &terminated); diff --git a/gcc/rust/hir/rust-hir-dump.cc b/gcc/rust/hir/rust-hir-dump.cc index 89fcc3d..dafa823 100644 --- a/gcc/rust/hir/rust-hir-dump.cc +++ b/gcc/rust/hir/rust-hir-dump.cc @@ -546,7 +546,8 @@ Dump::do_traitfunctiondecl (TraitFunctionDecl &e) else put_field ("where_clause", "none"); - put_field ("self", e.get_self ().as_string ()); + if (e.is_method ()) + put_field ("self", e.get_self_unchecked ().as_string ()); end ("TraitFunctionDecl"); } @@ -1693,7 +1694,8 @@ Dump::visit (Function &e) put_field ("where clause", e.get_where_clause ().as_string ()); visit_field ("function_body", e.get_definition ()); - put_field ("self", e.get_self_param ().as_string ()); + if (e.is_method ()) + put_field ("self", e.get_self_param_unchecked ().as_string ()); end ("Function"); } diff --git a/gcc/rust/hir/tree/rust-hir-bound.h b/gcc/rust/hir/tree/rust-hir-bound.h index 78bb133..8fa6a22 100644 --- a/gcc/rust/hir/tree/rust-hir-bound.h +++ b/gcc/rust/hir/tree/rust-hir-bound.h @@ -44,18 +44,6 @@ public: {} // Returns true if the lifetime is in an error state. - bool is_error () const - { - return lifetime_type == AST::Lifetime::LifetimeType::NAMED - && lifetime_name.empty (); - } - - static Lifetime error () - { - return Lifetime (Analysis::NodeMapping::get_error (), - AST::Lifetime::LifetimeType::NAMED, "", UNDEF_LOCATION); - } - std::string as_string () const override; void accept_vis (HIRFullVisitor &vis) override; diff --git a/gcc/rust/hir/tree/rust-hir-expr.cc b/gcc/rust/hir/tree/rust-hir-expr.cc index 2ded789..266c79c 100644 --- a/gcc/rust/hir/tree/rust-hir-expr.cc +++ b/gcc/rust/hir/tree/rust-hir-expr.cc @@ -81,10 +81,10 @@ OperatorExpr::operator= (OperatorExpr const &other) BorrowExpr::BorrowExpr (Analysis::NodeMapping mappings, std::unique_ptr<Expr> borrow_lvalue, Mutability mut, - AST::AttrVec outer_attribs, location_t locus) + bool raw, AST::AttrVec outer_attribs, location_t locus) : OperatorExpr (std::move (mappings), std::move (borrow_lvalue), std::move (outer_attribs), locus), - mut (mut) + mut (mut), raw (raw) {} DereferenceExpr::DereferenceExpr (Analysis::NodeMapping mappings, @@ -749,7 +749,7 @@ BlockExpr::BlockExpr (Analysis::NodeMapping mappings, std::vector<std::unique_ptr<Stmt>> block_statements, std::unique_ptr<Expr> block_expr, bool tail_reachable, AST::AttrVec inner_attribs, AST::AttrVec outer_attribs, - LoopLabel label, location_t start_locus, + tl::optional<LoopLabel> label, location_t start_locus, location_t end_locus) : ExprWithBlock (std::move (mappings), std::move (outer_attribs)), WithInnerAttrs (std::move (inner_attribs)), @@ -791,13 +791,15 @@ BlockExpr::operator= (BlockExpr const &other) } ContinueExpr::ContinueExpr (Analysis::NodeMapping mappings, location_t locus, - Lifetime label, AST::AttrVec outer_attribs) + tl::optional<Lifetime> label, + AST::AttrVec outer_attribs) : ExprWithoutBlock (std::move (mappings), std::move (outer_attribs)), label (std::move (label)), locus (locus) {} BreakExpr::BreakExpr (Analysis::NodeMapping mappings, location_t locus, - Lifetime break_label, std::unique_ptr<Expr> expr_in_break, + tl::optional<Lifetime> break_label, + std::unique_ptr<Expr> expr_in_break, AST::AttrVec outer_attribs) : ExprWithoutBlock (std::move (mappings), std::move (outer_attribs)), label (std::move (break_label)), break_expr (std::move (expr_in_break)), @@ -985,7 +987,8 @@ UnsafeBlockExpr::operator= (UnsafeBlockExpr const &other) BaseLoopExpr::BaseLoopExpr (Analysis::NodeMapping mappings, std::unique_ptr<BlockExpr> loop_block, - location_t locus, LoopLabel loop_label, + location_t locus, + tl::optional<LoopLabel> loop_label, AST::AttrVec outer_attribs) : ExprWithBlock (std::move (mappings), std::move (outer_attribs)), loop_label (std::move (loop_label)), loop_block (std::move (loop_block)), @@ -1011,7 +1014,8 @@ BaseLoopExpr::operator= (BaseLoopExpr const &other) LoopExpr::LoopExpr (Analysis::NodeMapping mappings, std::unique_ptr<BlockExpr> loop_block, location_t locus, - LoopLabel loop_label, AST::AttrVec outer_attribs) + tl::optional<LoopLabel> loop_label, + AST::AttrVec outer_attribs) : BaseLoopExpr (std::move (mappings), std::move (loop_block), locus, std::move (loop_label), std::move (outer_attribs)) {} @@ -1019,7 +1023,8 @@ LoopExpr::LoopExpr (Analysis::NodeMapping mappings, WhileLoopExpr::WhileLoopExpr (Analysis::NodeMapping mappings, std::unique_ptr<Expr> loop_condition, std::unique_ptr<BlockExpr> loop_block, - location_t locus, LoopLabel loop_label, + location_t locus, + tl::optional<LoopLabel> loop_label, AST::AttrVec outer_attribs) : BaseLoopExpr (std::move (mappings), std::move (loop_block), locus, std::move (loop_label), std::move (outer_attribs)), @@ -1046,7 +1051,8 @@ WhileLetLoopExpr::WhileLetLoopExpr ( Analysis::NodeMapping mappings, std::vector<std::unique_ptr<Pattern>> match_arm_patterns, std::unique_ptr<Expr> condition, std::unique_ptr<BlockExpr> loop_block, - location_t locus, LoopLabel loop_label, AST::AttrVec outer_attribs) + location_t locus, tl::optional<LoopLabel> loop_label, + AST::AttrVec outer_attribs) : BaseLoopExpr (std::move (mappings), std::move (loop_block), locus, std::move (loop_label), std::move (outer_attribs)), match_arm_patterns (std::move (match_arm_patterns)), diff --git a/gcc/rust/hir/tree/rust-hir-expr.h b/gcc/rust/hir/tree/rust-hir-expr.h index f8f2128..96f0cf6 100644 --- a/gcc/rust/hir/tree/rust-hir-expr.h +++ b/gcc/rust/hir/tree/rust-hir-expr.h @@ -45,9 +45,6 @@ public: LoopLabel (Analysis::NodeMapping mapping, Lifetime loop_label, location_t locus); - // Returns whether the LoopLabel is in an error state. - bool is_error () const { return label.is_error (); } - location_t get_locus () const { return locus; } Analysis::NodeMapping &get_mappings () { return mappings; } @@ -199,12 +196,13 @@ public: class BorrowExpr : public OperatorExpr { Mutability mut; + bool raw; public: std::string as_string () const override; BorrowExpr (Analysis::NodeMapping mappings, - std::unique_ptr<Expr> borrow_lvalue, Mutability mut, + std::unique_ptr<Expr> borrow_lvalue, Mutability mut, bool raw, AST::AttrVec outer_attribs, location_t locus); void accept_vis (HIRFullVisitor &vis) override; @@ -212,6 +210,7 @@ public: Mutability get_mut () const { return mut; } bool is_mut () const { return mut == Mutability::Mut; } + bool is_raw_borrow () const { return raw; } protected: /* Use covariance to implement clone function as returning this object rather @@ -1715,7 +1714,7 @@ public: std::vector<std::unique_ptr<Stmt>> statements; std::unique_ptr<Expr> expr; bool tail_reachable; - LoopLabel label; + tl::optional<LoopLabel> label; location_t start_locus; location_t end_locus; @@ -1735,7 +1734,8 @@ public: std::vector<std::unique_ptr<Stmt>> block_statements, std::unique_ptr<Expr> block_expr, bool tail_reachable, AST::AttrVec inner_attribs, AST::AttrVec outer_attribs, - LoopLabel label, location_t start_locus, location_t end_locus); + tl::optional<LoopLabel> label, location_t start_locus, + location_t end_locus); // Copy constructor with clone BlockExpr (BlockExpr const &other); @@ -1774,8 +1774,8 @@ public: return ExprType::Block; } - bool has_label () const { return !label.is_error (); } - LoopLabel &get_label () { return label; } + bool has_label () const { return label.has_value (); } + LoopLabel &get_label () { return label.value (); } protected: /* Use covariance to implement clone function as returning this object rather @@ -1803,25 +1803,27 @@ protected: // HIR node representing continue expression within loops class ContinueExpr : public ExprWithoutBlock { - Lifetime label; + tl::optional<Lifetime> label; location_t locus; public: std::string as_string () const override; // Returns true if the continue expr has a label. - bool has_label () const { return !label.is_error (); } + bool has_label () const { return label.has_value (); } // Constructor for a ContinueExpr with a label. ContinueExpr (Analysis::NodeMapping mappings, location_t locus, - Lifetime label, AST::AttrVec outer_attribs = AST::AttrVec ()); + tl::optional<Lifetime> label, + AST::AttrVec outer_attribs = AST::AttrVec ()); location_t get_locus () const override final { return locus; } void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRExpressionVisitor &vis) override; - Lifetime &get_label () { return label; } + Lifetime &get_label () { return label.value (); } + const Lifetime &get_label () const { return label.value (); } ExprType get_expression_type () const final override { @@ -1848,7 +1850,7 @@ protected: class BreakExpr : public ExprWithoutBlock { // bool has_label; - Lifetime label; + tl::optional<Lifetime> label; // bool has_break_expr; std::unique_ptr<Expr> break_expr; @@ -1859,7 +1861,7 @@ public: std::string as_string () const override; // Returns whether the break expression has a label or not. - bool has_label () const { return !label.is_error (); } + bool has_label () const { return label.has_value (); } /* Returns whether the break expression has an expression used in the break or * not. */ @@ -1867,7 +1869,7 @@ public: // Constructor for a break expression BreakExpr (Analysis::NodeMapping mappings, location_t locus, - Lifetime break_label, + tl::optional<Lifetime> break_label, std::unique_ptr<Expr> expr_in_break = nullptr, AST::AttrVec outer_attribs = AST::AttrVec ()); @@ -1886,7 +1888,8 @@ public: void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRExpressionVisitor &vis) override; - Lifetime &get_label () { return label; } + Lifetime &get_label () { return label.value (); } + const Lifetime &get_label () const { return label.value (); } Expr &get_expr () { return *break_expr; } @@ -2293,7 +2296,7 @@ protected: class BaseLoopExpr : public ExprWithBlock { protected: - LoopLabel loop_label; + tl::optional<LoopLabel> loop_label; std::unique_ptr<BlockExpr> loop_block; private: @@ -2303,7 +2306,7 @@ protected: // Constructor for BaseLoopExpr BaseLoopExpr (Analysis::NodeMapping mappings, std::unique_ptr<BlockExpr> loop_block, location_t locus, - LoopLabel loop_label, + tl::optional<LoopLabel> loop_label, AST::AttrVec outer_attribs = AST::AttrVec ()); // Copy constructor for BaseLoopExpr with clone @@ -2322,13 +2325,14 @@ protected: } public: - bool has_loop_label () const { return !loop_label.is_error (); } + bool has_loop_label () const { return loop_label.has_value (); } location_t get_locus () const override final { return locus; } HIR::BlockExpr &get_loop_block () { return *loop_block; }; - LoopLabel &get_loop_label () { return loop_label; } + LoopLabel &get_loop_label () { return loop_label.value (); } + const LoopLabel &get_loop_label () const { return loop_label.value (); } }; // 'Loop' expression (i.e. the infinite loop) HIR node @@ -2340,7 +2344,8 @@ public: // Constructor for LoopExpr LoopExpr (Analysis::NodeMapping mappings, std::unique_ptr<BlockExpr> loop_block, location_t locus, - LoopLabel loop_label, AST::AttrVec outer_attribs = AST::AttrVec ()); + tl::optional<LoopLabel> loop_label, + AST::AttrVec outer_attribs = AST::AttrVec ()); void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRExpressionVisitor &vis) override; @@ -2370,7 +2375,7 @@ public: WhileLoopExpr (Analysis::NodeMapping mappings, std::unique_ptr<Expr> loop_condition, std::unique_ptr<BlockExpr> loop_block, location_t locus, - LoopLabel loop_label, + tl::optional<LoopLabel> loop_label, AST::AttrVec outer_attribs = AST::AttrVec ()); // Copy constructor with clone @@ -2419,7 +2424,7 @@ public: std::vector<std::unique_ptr<Pattern>> match_arm_patterns, std::unique_ptr<Expr> condition, std::unique_ptr<BlockExpr> loop_block, location_t locus, - LoopLabel loop_label, + tl::optional<LoopLabel> loop_label, AST::AttrVec outer_attribs = AST::AttrVec ()); // Copy constructor with clone diff --git a/gcc/rust/hir/tree/rust-hir-generic-param.h b/gcc/rust/hir/tree/rust-hir-generic-param.h index a1c59bf..960de56 100644 --- a/gcc/rust/hir/tree/rust-hir-generic-param.h +++ b/gcc/rust/hir/tree/rust-hir-generic-param.h @@ -97,9 +97,6 @@ public: AST::AttrVec &get_outer_attrs () override { return outer_attrs; } - // Returns whether the lifetime param is in an error state. - bool is_error () const { return lifetime.is_error (); } - // Constructor LifetimeParam (Analysis::NodeMapping mappings, Lifetime lifetime, location_t locus = UNDEF_LOCATION, diff --git a/gcc/rust/hir/tree/rust-hir-item.cc b/gcc/rust/hir/tree/rust-hir-item.cc index cff06d3..160f710 100644 --- a/gcc/rust/hir/tree/rust-hir-item.cc +++ b/gcc/rust/hir/tree/rust-hir-item.cc @@ -123,7 +123,8 @@ TypeBoundWhereClauseItem::get_type_param_bounds () } SelfParam::SelfParam (Analysis::NodeMapping mappings, - ImplicitSelfKind self_kind, Lifetime lifetime, Type *type) + ImplicitSelfKind self_kind, + tl::optional<Lifetime> lifetime, Type *type) : self_kind (self_kind), lifetime (std::move (lifetime)), type (type), mappings (mappings) {} @@ -131,13 +132,13 @@ SelfParam::SelfParam (Analysis::NodeMapping mappings, SelfParam::SelfParam (Analysis::NodeMapping mappings, std::unique_ptr<Type> type, bool is_mut, location_t locus) : self_kind (is_mut ? ImplicitSelfKind::MUT : ImplicitSelfKind::IMM), - lifetime ( - Lifetime (mappings, AST::Lifetime::LifetimeType::NAMED, "", locus)), - type (std::move (type)), locus (locus), mappings (mappings) + lifetime (tl::nullopt), type (std::move (type)), locus (locus), + mappings (mappings) {} -SelfParam::SelfParam (Analysis::NodeMapping mappings, Lifetime lifetime, - bool is_mut, location_t locus) +SelfParam::SelfParam (Analysis::NodeMapping mappings, + tl::optional<Lifetime> lifetime, bool is_mut, + location_t locus) : self_kind (is_mut ? ImplicitSelfKind::MUT_REF : ImplicitSelfKind::IMM_REF), lifetime (std::move (lifetime)), locus (locus), mappings (mappings) {} @@ -263,7 +264,8 @@ Function::Function (Analysis::NodeMapping mappings, Identifier function_name, std::vector<FunctionParam> function_params, std::unique_ptr<Type> return_type, WhereClause where_clause, std::unique_ptr<BlockExpr> function_body, Visibility vis, - AST::AttrVec outer_attrs, SelfParam self, location_t locus) + AST::AttrVec outer_attrs, tl::optional<SelfParam> self, + Defaultness defaultness, location_t locus) : VisItem (std::move (mappings), std::move (vis), std::move (outer_attrs)), qualifiers (std::move (qualifiers)), function_name (std::move (function_name)), @@ -272,7 +274,7 @@ Function::Function (Analysis::NodeMapping mappings, Identifier function_name, return_type (std::move (return_type)), where_clause (std::move (where_clause)), function_body (std::move (function_body)), self (std::move (self)), - locus (locus) + locus (locus), defaultness (defaultness) {} Function::Function (Function const &other) @@ -280,7 +282,7 @@ Function::Function (Function const &other) function_name (other.function_name), function_params (other.function_params), where_clause (other.where_clause), function_body (other.function_body->clone_block_expr ()), self (other.self), - locus (other.locus) + locus (other.locus), defaultness (other.defaultness) { // guard to prevent null dereference (always required) if (other.return_type != nullptr) @@ -312,6 +314,8 @@ Function::operator= (Function const &other) locus = other.locus; self = other.self; + defaultness = other.defaultness; + generic_params.reserve (other.generic_params.size ()); for (const auto &e : other.generic_params) generic_params.push_back (e->clone_generic_param ()); @@ -609,9 +613,9 @@ StaticItem::operator= (StaticItem const &other) TraitFunctionDecl::TraitFunctionDecl ( Identifier function_name, FunctionQualifiers qualifiers, - std::vector<std::unique_ptr<GenericParam>> generic_params, SelfParam self, - std::vector<FunctionParam> function_params, std::unique_ptr<Type> return_type, - WhereClause where_clause) + std::vector<std::unique_ptr<GenericParam>> generic_params, + tl::optional<SelfParam> self, std::vector<FunctionParam> function_params, + std::unique_ptr<Type> return_type, WhereClause where_clause) : qualifiers (std::move (qualifiers)), function_name (std::move (function_name)), generic_params (std::move (generic_params)), diff --git a/gcc/rust/hir/tree/rust-hir-item.h b/gcc/rust/hir/tree/rust-hir-item.h index 4744717..b9b105b 100644 --- a/gcc/rust/hir/tree/rust-hir-item.h +++ b/gcc/rust/hir/tree/rust-hir-item.h @@ -371,13 +371,13 @@ public: private: ImplicitSelfKind self_kind; - Lifetime lifetime; + tl::optional<Lifetime> lifetime; std::unique_ptr<Type> type; location_t locus; Analysis::NodeMapping mappings; SelfParam (Analysis::NodeMapping mappings, ImplicitSelfKind self_kind, - Lifetime lifetime, Type *type); + tl::optional<Lifetime> lifetime, Type *type); public: // Type-based self parameter (not ref, no lifetime) @@ -385,8 +385,8 @@ public: bool is_mut, location_t locus); // Lifetime-based self parameter (is ref, no type) - SelfParam (Analysis::NodeMapping mappings, Lifetime lifetime, bool is_mut, - location_t locus); + SelfParam (Analysis::NodeMapping mappings, tl::optional<Lifetime> lifetime, + bool is_mut, location_t locus); // Copy constructor requires clone SelfParam (SelfParam const &other); @@ -398,22 +398,13 @@ public: SelfParam (SelfParam &&other) = default; SelfParam &operator= (SelfParam &&other) = default; - static SelfParam error () - { - return SelfParam (Analysis::NodeMapping::get_error (), - ImplicitSelfKind::NONE, Lifetime::error (), nullptr); - } - // Returns whether the self-param has a type field. bool has_type () const { return type != nullptr; } // Returns whether the self-param has a valid lifetime. - bool has_lifetime () const { return !lifetime.is_error (); } - - const Lifetime &get_lifetime () const { return lifetime; } + bool has_lifetime () const { return lifetime.has_value (); } - // Returns whether the self-param is in an error state. - bool is_error () const { return self_kind == ImplicitSelfKind::NONE; } + const Lifetime &get_lifetime () const { return lifetime.value (); } std::string as_string () const; @@ -945,6 +936,12 @@ protected: class LetStmt; +enum class Defaultness +{ + Default, + Final, +}; + // Rust function declaration HIR node class Function : public VisItem, public ImplItem { @@ -955,9 +952,14 @@ class Function : public VisItem, public ImplItem std::unique_ptr<Type> return_type; WhereClause where_clause; std::unique_ptr<BlockExpr> function_body; - SelfParam self; + tl::optional<SelfParam> self; location_t locus; + // NOTE: This should be moved to the trait item base class once we start + // implementing specialization for real, instead of just stubbing out the + // feature + Defaultness defaultness; + public: std::string as_string () const override; @@ -973,6 +975,9 @@ public: // Returns whether function has a where clause. bool has_where_clause () const { return !where_clause.is_empty (); } + // Returns whether function has a default qualifier + bool is_default () const { return defaultness == Defaultness::Default; } + ImplItemType get_impl_item_type () const override final { return ImplItem::ImplItemType::FUNCTION; @@ -987,7 +992,8 @@ public: std::vector<FunctionParam> function_params, std::unique_ptr<Type> return_type, WhereClause where_clause, std::unique_ptr<BlockExpr> function_body, Visibility vis, - AST::AttrVec outer_attrs, SelfParam self, location_t locus); + AST::AttrVec outer_attrs, tl::optional<SelfParam> self, + Defaultness defaultness, location_t locus); // Copy constructor with clone Function (Function const &other); @@ -1041,9 +1047,13 @@ public: // TODO: is this better? Or is a "vis_block" better? Type &get_return_type () { return *return_type; } - bool is_method () const { return !self.is_error (); } + bool is_method () const { return self.has_value (); } + + tl::optional<SelfParam> &get_self_param () { return self; } + const tl::optional<SelfParam> &get_self_param () const { return self; } - SelfParam &get_self_param () { return self; } + SelfParam &get_self_param_unchecked () { return self.value (); } + const SelfParam &get_self_param_unchecked () const { return self.value (); } std::string get_impl_item_name () const override final { @@ -1898,13 +1908,14 @@ private: std::vector<FunctionParam> function_params; std::unique_ptr<Type> return_type; WhereClause where_clause; - SelfParam self; + tl::optional<SelfParam> self; public: // Mega-constructor TraitFunctionDecl (Identifier function_name, FunctionQualifiers qualifiers, std::vector<std::unique_ptr<GenericParam>> generic_params, - SelfParam self, std::vector<FunctionParam> function_params, + tl::optional<SelfParam> self, + std::vector<FunctionParam> function_params, std::unique_ptr<Type> return_type, WhereClause where_clause); @@ -1936,9 +1947,13 @@ public: WhereClause &get_where_clause () { return where_clause; } - bool is_method () const { return !self.is_error (); } + bool is_method () const { return self.has_value (); } + + SelfParam &get_self_unchecked () { return self.value (); } + const SelfParam &get_self_unchecked () const { return self.value (); } - SelfParam &get_self () { return self; } + tl::optional<SelfParam> &get_self () { return self; } + const tl::optional<SelfParam> &get_self () const { return self; } Identifier get_function_name () const { return function_name; } diff --git a/gcc/rust/hir/tree/rust-hir-type.cc b/gcc/rust/hir/tree/rust-hir-type.cc index 6a6c319..ec48425 100644 --- a/gcc/rust/hir/tree/rust-hir-type.cc +++ b/gcc/rust/hir/tree/rust-hir-type.cc @@ -162,7 +162,7 @@ RawPointerType::operator= (RawPointerType const &other) ReferenceType::ReferenceType (Analysis::NodeMapping mappings, Mutability mut, std::unique_ptr<Type> type_no_bounds, - location_t locus, Lifetime lifetime) + location_t locus, tl::optional<Lifetime> lifetime) : TypeNoBounds (mappings, locus), lifetime (std::move (lifetime)), mut (mut), type (std::move (type_no_bounds)) {} diff --git a/gcc/rust/hir/tree/rust-hir-type.h b/gcc/rust/hir/tree/rust-hir-type.h index bd0f2b6..cbc20ff 100644 --- a/gcc/rust/hir/tree/rust-hir-type.h +++ b/gcc/rust/hir/tree/rust-hir-type.h @@ -291,7 +291,7 @@ protected: class ReferenceType : public TypeNoBounds { // bool has_lifetime; // TODO: handle in lifetime or something? - Lifetime lifetime; + tl::optional<Lifetime> lifetime; Mutability mut; std::unique_ptr<Type> type; @@ -301,12 +301,12 @@ public: bool is_mut () const { return mut == Mutability::Mut; } // Returns whether the reference has a lifetime. - bool has_lifetime () const { return !lifetime.is_error (); } + bool has_lifetime () const { return lifetime.has_value (); } // Constructor ReferenceType (Analysis::NodeMapping mappings, Mutability mut, std::unique_ptr<Type> type_no_bounds, location_t locus, - Lifetime lifetime); + tl::optional<Lifetime> lifetime); // Copy constructor with custom clone method ReferenceType (ReferenceType const &other); @@ -323,7 +323,8 @@ public: void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRTypeVisitor &vis) override; - Lifetime &get_lifetime () { return lifetime; } + Lifetime &get_lifetime () { return lifetime.value (); } + const Lifetime &get_lifetime () const { return lifetime.value (); } Mutability get_mut () const { return mut; } diff --git a/gcc/rust/hir/tree/rust-hir.cc b/gcc/rust/hir/tree/rust-hir.cc index 822eaff..c8bf9da 100644 --- a/gcc/rust/hir/tree/rust-hir.cc +++ b/gcc/rust/hir/tree/rust-hir.cc @@ -1314,7 +1314,7 @@ ContinueExpr::as_string () const if (has_label ()) { - str += label.as_string (); + str += get_label ().as_string (); } return str; @@ -1704,7 +1704,7 @@ WhileLoopExpr::as_string () const } else { - str += loop_label.as_string (); + str += get_loop_label ().as_string (); } str += "\n Conditional expr: " + condition->as_string (); @@ -1726,7 +1726,7 @@ WhileLetLoopExpr::as_string () const } else { - str += loop_label.as_string (); + str += get_loop_label ().as_string (); } str += "\n Match arm patterns: "; @@ -1761,7 +1761,7 @@ LoopExpr::as_string () const } else { - str += loop_label.as_string (); + str += get_loop_label ().as_string (); } str += "\n Loop block: " + loop_block->as_string (); @@ -1816,7 +1816,7 @@ BreakExpr::as_string () const if (has_label ()) { - str += label.as_string () + " "; + str += get_label ().as_string () + " "; } if (has_break_expr ()) @@ -2101,11 +2101,6 @@ QualifiedPathInType::as_string () const std::string Lifetime::as_string () const { - if (is_error ()) - { - return "error lifetime"; - } - switch (lifetime_type) { case AST::Lifetime::LifetimeType::NAMED: @@ -2760,7 +2755,7 @@ ReferenceType::as_string () const if (has_lifetime ()) { - str += lifetime.as_string () + " "; + str += get_lifetime ().as_string () + " "; } if (is_mut ()) @@ -3411,7 +3406,7 @@ TraitFunctionDecl::as_string () const str += "\n Function params: "; if (is_method ()) { - str += self.as_string () + (has_params () ? ", " : ""); + str += get_self_unchecked ().as_string () + (has_params () ? ", " : ""); } if (has_params ()) @@ -3525,70 +3520,63 @@ TraitItemType::as_string () const std::string SelfParam::as_string () const { - if (is_error ()) - { - return "error"; - } - else + if (has_type ()) { - if (has_type ()) + // type (i.e. not ref, no lifetime) + std::string str; + + if (is_mut ()) { - // type (i.e. not ref, no lifetime) - std::string str; + str += "mut "; + } - if (is_mut ()) - { - str += "mut "; - } + str += "self : "; - str += "self : "; + str += type->as_string (); - str += type->as_string (); + return str; + } + else if (has_lifetime ()) + { + // ref and lifetime + std::string str = "&" + get_lifetime ().as_string () + " "; - return str; - } - else if (has_lifetime ()) + if (is_mut ()) { - // ref and lifetime - std::string str = "&" + lifetime.as_string () + " "; + str += "mut "; + } - if (is_mut ()) - { - str += "mut "; - } + str += "self"; - str += "self"; + return str; + } + else if (is_ref ()) + { + // ref with no lifetime + std::string str = "&"; - return str; - } - else if (is_ref ()) + if (is_mut ()) { - // ref with no lifetime - std::string str = "&"; + str += " mut "; + } - if (is_mut ()) - { - str += " mut "; - } + str += "self"; - str += "self"; + return str; + } + else + { + // no ref, no type + std::string str; - return str; - } - else + if (is_mut ()) { - // no ref, no type - std::string str; - - if (is_mut ()) - { - str += "mut "; - } + str += "mut "; + } - str += "self"; + str += "self"; - return str; - } + return str; } } |