Branch data Line data Source code
1 : : // Copyright (C) 2020-2025 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 : : #include "rust-hir-type-check.h"
20 : : #include "rust-tyty.h"
21 : :
22 : : namespace Rust {
23 : : namespace TyTy {
24 : :
25 : 870951 : TyVar::TyVar (HirId ref) : ref (ref)
26 : : {
27 : : // ensure this reference is defined within the context
28 : 870951 : auto context = Resolver::TypeCheckContext::get ();
29 : 870951 : BaseType *lookup = nullptr;
30 : 870951 : bool ok = context->lookup_type (ref, &lookup);
31 : 870951 : rust_assert (ok);
32 : 870951 : }
33 : :
34 : : BaseType *
35 : 1310158 : TyVar::get_tyty () const
36 : : {
37 : 1310158 : auto context = Resolver::TypeCheckContext::get ();
38 : 1310158 : BaseType *lookup = nullptr;
39 : 1310158 : bool ok = context->lookup_type (ref, &lookup);
40 : 1310158 : rust_assert (ok);
41 : 1310158 : return lookup;
42 : : }
43 : :
44 : : TyVar
45 : 5364 : TyVar::get_implicit_infer_var (location_t locus)
46 : : {
47 : 5364 : auto mappings = Analysis::Mappings::get ();
48 : 5364 : auto context = Resolver::TypeCheckContext::get ();
49 : :
50 : 5364 : InferType *infer = new InferType (mappings->get_next_hir_id (),
51 : : InferType::InferTypeKind::GENERAL,
52 : 5364 : InferType::TypeHint::Default (), locus);
53 : 5364 : context->insert_type (Analysis::NodeMapping (mappings->get_current_crate (),
54 : : UNKNOWN_NODEID,
55 : : infer->get_ref (),
56 : 5364 : UNKNOWN_LOCAL_DEFID),
57 : : infer);
58 : 5364 : mappings->insert_location (infer->get_ref (), locus);
59 : :
60 : 5364 : return TyVar (infer->get_ref ());
61 : : }
62 : :
63 : : TyVar
64 : 6532 : TyVar::subst_covariant_var (TyTy::BaseType *orig, TyTy::BaseType *subst)
65 : : {
66 : 6532 : if (orig->get_kind () != TyTy::TypeKind::PARAM)
67 : 1323 : return TyVar (subst->get_ty_ref ());
68 : 5209 : else if (subst->get_kind () == TyTy::TypeKind::PARAM)
69 : : {
70 : 5209 : TyTy::ParamType *p = static_cast<TyTy::ParamType *> (subst);
71 : 5209 : if (p->resolve ()->get_kind () == TyTy::TypeKind::PARAM)
72 : : {
73 : 257 : return TyVar (subst->get_ty_ref ());
74 : : }
75 : : }
76 : :
77 : 4952 : return TyVar (subst->get_ref ());
78 : : }
79 : :
80 : : TyVar
81 : 42895 : TyVar::clone () const
82 : : {
83 : 42895 : TyTy::BaseType *c = get_tyty ()->clone ();
84 : 42895 : return TyVar (c->get_ref ());
85 : : }
86 : :
87 : : TyVar
88 : 773 : TyVar::monomorphized_clone () const
89 : : {
90 : 773 : auto mappings = Analysis::Mappings::get ();
91 : 773 : auto context = Resolver::TypeCheckContext::get ();
92 : :
93 : : // this needs a new hirid
94 : 773 : TyTy::BaseType *c = get_tyty ()->monomorphized_clone ();
95 : 773 : c->set_ref (mappings->get_next_hir_id ());
96 : :
97 : : // insert it
98 : 773 : context->insert_type (Analysis::NodeMapping (mappings->get_current_crate (),
99 : : UNKNOWN_NODEID, c->get_ref (),
100 : 773 : UNKNOWN_LOCAL_DEFID),
101 : : c);
102 : :
103 : 773 : return TyVar (c->get_ref ());
104 : : }
105 : :
106 : 78241 : TyWithLocation::TyWithLocation (BaseType *ty, location_t locus)
107 : 78241 : : ty (ty), locus (locus)
108 : 78241 : {}
109 : :
110 : 231529 : TyWithLocation::TyWithLocation (BaseType *ty) : ty (ty)
111 : : {
112 : 231529 : auto mappings = Analysis::Mappings::get ();
113 : 231529 : locus = mappings->lookup_location (ty->get_ref ());
114 : 231529 : }
115 : :
116 : : } // namespace TyTy
117 : : } // namespace Rust
|