1 // Copyright (C) 2020-2025 Free Software Foundation, Inc.
3 // This file is part of GCC.
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
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
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/>.
19 #include "rust-hir-type-check.h"
20 #include "rust-tyty.h"
25 TyVar::TyVar (HirId ref
) : ref (ref
)
27 // ensure this reference is defined within the context
28 auto context
= Resolver::TypeCheckContext::get ();
29 BaseType
*lookup
= nullptr;
30 bool ok
= context
->lookup_type (ref
, &lookup
);
35 TyVar::get_tyty () const
37 auto context
= Resolver::TypeCheckContext::get ();
38 BaseType
*lookup
= nullptr;
39 bool ok
= context
->lookup_type (ref
, &lookup
);
45 TyVar::get_implicit_infer_var (location_t locus
)
47 auto mappings
= Analysis::Mappings::get ();
48 auto context
= Resolver::TypeCheckContext::get ();
50 InferType
*infer
= new InferType (mappings
->get_next_hir_id (),
51 InferType::InferTypeKind::GENERAL
,
52 InferType::TypeHint::Default (), locus
);
53 context
->insert_type (Analysis::NodeMapping (mappings
->get_current_crate (),
58 mappings
->insert_location (infer
->get_ref (), locus
);
60 return TyVar (infer
->get_ref ());
64 TyVar::subst_covariant_var (TyTy::BaseType
*orig
, TyTy::BaseType
*subst
)
66 if (orig
->get_kind () != TyTy::TypeKind::PARAM
)
67 return TyVar (subst
->get_ty_ref ());
68 else if (subst
->get_kind () == TyTy::TypeKind::PARAM
)
70 TyTy::ParamType
*p
= static_cast<TyTy::ParamType
*> (subst
);
71 if (p
->resolve ()->get_kind () == TyTy::TypeKind::PARAM
)
73 return TyVar (subst
->get_ty_ref ());
77 return TyVar (subst
->get_ref ());
83 TyTy::BaseType
*c
= get_tyty ()->clone ();
84 return TyVar (c
->get_ref ());
88 TyVar::monomorphized_clone () const
90 auto mappings
= Analysis::Mappings::get ();
91 auto context
= Resolver::TypeCheckContext::get ();
93 // this needs a new hirid
94 TyTy::BaseType
*c
= get_tyty ()->monomorphized_clone ();
95 c
->set_ref (mappings
->get_next_hir_id ());
98 context
->insert_type (Analysis::NodeMapping (mappings
->get_current_crate (),
99 UNKNOWN_NODEID
, c
->get_ref (),
100 UNKNOWN_LOCAL_DEFID
),
103 return TyVar (c
->get_ref ());
106 TyWithLocation::TyWithLocation (BaseType
*ty
, location_t locus
)
107 : ty (ty
), locus (locus
)
110 TyWithLocation::TyWithLocation (BaseType
*ty
) : ty (ty
)
112 auto mappings
= Analysis::Mappings::get ();
113 locus
= mappings
->lookup_location (ty
->get_ref ());