(root)/
gcc-13.2.0/
gcc/
rust/
typecheck/
rust-coercion.h
       1  // Copyright (C) 2020-2023 Free Software Foundation, Inc.
       2  
       3  // This file is part of GCC.
       4  
       5  // GCC is free software; you can redistribute it and/or modify it under
       6  // the terms of the GNU General Public License as published by the Free
       7  // Software Foundation; either version 3, or (at your option) any later
       8  // version.
       9  
      10  // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      11  // WARRANTY; without even the implied warranty of MERCHANTABILITY or
      12  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      13  // for more details.
      14  
      15  // You should have received a copy of the GNU General Public License
      16  // along with GCC; see the file COPYING3.  If not see
      17  // <http://www.gnu.org/licenses/>.
      18  
      19  #ifndef RUST_COERCION
      20  #define RUST_COERCION
      21  
      22  #include "rust-autoderef.h"
      23  #include "rust-hir-type-check.h"
      24  
      25  namespace Rust {
      26  namespace Resolver {
      27  
      28  class TypeCoercionRules : protected AutoderefCycle
      29  {
      30  public:
      31    struct CoercionResult
      32    {
      33      std::vector<Adjustment> adjustments;
      34      TyTy::BaseType *tyty;
      35  
      36      bool is_error ()
      37      {
      38        return tyty == nullptr || tyty->get_kind () == TyTy::TypeKind::ERROR;
      39      }
      40  
      41      static CoercionResult get_error () { return CoercionResult{{}, nullptr}; }
      42    };
      43  
      44    static CoercionResult Coerce (TyTy::BaseType *receiver,
      45  				TyTy::BaseType *expected, Location locus);
      46  
      47    static CoercionResult TryCoerce (TyTy::BaseType *receiver,
      48  				   TyTy::BaseType *expected, Location locus);
      49  
      50    CoercionResult coerce_unsafe_ptr (TyTy::BaseType *receiver,
      51  				    TyTy::PointerType *expected,
      52  				    Mutability mutability);
      53  
      54    CoercionResult coerce_borrowed_pointer (TyTy::BaseType *receiver,
      55  					  TyTy::ReferenceType *expected,
      56  					  Mutability mutability);
      57  
      58    CoercionResult coerce_unsized (TyTy::BaseType *receiver,
      59  				 TyTy::BaseType *expected, bool &unsafe_error);
      60  
      61    static bool coerceable_mutability (Mutability from_mutbl,
      62  				     Mutability to_mutbl);
      63  
      64    void mismatched_mutability_error (Location expr_locus, Location lhs,
      65  				    Location rhs);
      66    void object_unsafe_error (Location expr_locus, Location lhs, Location rhs);
      67  
      68  protected:
      69    TypeCoercionRules (TyTy::BaseType *expected, Location locus,
      70  		     bool emit_errors);
      71  
      72    bool select (const TyTy::BaseType &autoderefed) override;
      73  
      74    bool do_coercion (TyTy::BaseType *receiver);
      75  
      76  private:
      77    // context info
      78    Analysis::Mappings *mappings;
      79    TypeCheckContext *context;
      80  
      81    // search
      82    TyTy::BaseType *expected;
      83    Location locus;
      84  
      85    // mutable fields
      86    CoercionResult try_result;
      87    bool emit_errors;
      88  };
      89  
      90  } // namespace Resolver
      91  } // namespace Rust
      92  
      93  #endif // RUST_COERCION