Line data Source code
1 : // Copyright (C) 2020-2026 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-mapping-common.h"
21 : #include "rust-system.h"
22 : #include "rust-tyty.h"
23 :
24 : namespace Rust {
25 : namespace TyTy {
26 :
27 1439182 : TyVar::TyVar (HirId ref) : ref (ref)
28 : {
29 : // ensure this reference is defined within the context
30 1439182 : auto context = Resolver::TypeCheckContext::get ();
31 1439182 : BaseType *lookup = nullptr;
32 1439182 : bool ok = context->lookup_type (ref, &lookup);
33 1439182 : if (!ok || lookup == nullptr || lookup->get_kind () == TypeKind::ERROR)
34 3 : return;
35 : }
36 :
37 : BaseType *
38 2236647 : TyVar::get_tyty () const
39 : {
40 2236647 : auto context = Resolver::TypeCheckContext::get ();
41 2236647 : BaseType *lookup = nullptr;
42 2236647 : bool ok = context->lookup_type (ref, &lookup);
43 2236647 : if (!ok || lookup == nullptr)
44 : return nullptr;
45 : return lookup;
46 : }
47 :
48 : TyVar
49 16588 : TyVar::get_implicit_infer_var (location_t locus)
50 : {
51 16588 : auto &mappings = Analysis::Mappings::get ();
52 16588 : auto context = Resolver::TypeCheckContext::get ();
53 :
54 16588 : HirId next = mappings.get_next_hir_id ();
55 16588 : auto infer = new InferType (next, InferType::InferTypeKind::GENERAL,
56 16588 : InferType::TypeHint::Default (), locus);
57 :
58 16588 : context->insert_implicit_type (infer->get_ref (), infer);
59 16588 : mappings.insert_location (infer->get_ref (), locus);
60 :
61 16588 : return TyVar (infer->get_ref ());
62 : }
63 :
64 : TyVar
65 109 : TyVar::get_implicit_const_infer_var (location_t locus, TyVar *implicit_type)
66 : {
67 109 : auto &mappings = Analysis::Mappings::get ();
68 109 : auto context = Resolver::TypeCheckContext::get ();
69 :
70 109 : TyVar it = (implicit_type != nullptr) ? *implicit_type
71 109 : : get_implicit_infer_var (locus);
72 109 : HirId next = mappings.get_next_hir_id ();
73 109 : auto infer = new ConstInferType (it.get_tyty (), next, next, {});
74 :
75 109 : context->insert_implicit_type (infer->get_ref (), infer);
76 109 : mappings.insert_location (infer->get_ref (), locus);
77 :
78 109 : return TyVar (infer->get_ref ());
79 : }
80 :
81 : TyVar
82 16694 : TyVar::subst_covariant_var (TyTy::BaseType *orig, TyTy::BaseType *subst)
83 : {
84 16694 : if (orig->get_kind () != TyTy::TypeKind::PARAM)
85 2054 : return TyVar (subst->get_ty_ref ());
86 14640 : else if (subst->get_kind () == TyTy::TypeKind::PARAM)
87 : {
88 14640 : TyTy::ParamType *p = static_cast<TyTy::ParamType *> (subst);
89 14640 : if (p->resolve ()->get_kind () == TyTy::TypeKind::PARAM)
90 : {
91 1514 : return TyVar (subst->get_ty_ref ());
92 : }
93 : }
94 :
95 13126 : return TyVar (subst->get_ref ());
96 : }
97 :
98 : TyVar
99 39940 : TyVar::clone () const
100 : {
101 39940 : TyTy::BaseType *base = get_tyty ();
102 39940 : if (base == nullptr || base->get_kind () == TypeKind::ERROR)
103 0 : return TyVar::get_implicit_infer_var (UNKNOWN_LOCATION);
104 39940 : TyTy::BaseType *c = base->clone ();
105 39940 : return TyVar (c->get_ref ());
106 : }
107 :
108 : TyVar
109 1773 : TyVar::monomorphized_clone () const
110 : {
111 1773 : auto &mappings = Analysis::Mappings::get ();
112 1773 : auto context = Resolver::TypeCheckContext::get ();
113 :
114 1773 : TyTy::BaseType *base = get_tyty ();
115 1773 : if (base == nullptr || base->get_kind () == TypeKind::ERROR)
116 0 : return TyVar::get_implicit_infer_var (UNKNOWN_LOCATION);
117 :
118 : // this needs a new hirid
119 1773 : TyTy::BaseType *c = get_tyty ()->monomorphized_clone ();
120 1773 : c->set_ref (mappings.get_next_hir_id ());
121 :
122 : // insert it
123 1773 : context->insert_type (Analysis::NodeMapping (mappings.get_current_crate (),
124 : UNKNOWN_NODEID, c->get_ref (),
125 1773 : UNKNOWN_LOCAL_DEFID),
126 : c);
127 :
128 1773 : return TyVar (c->get_ref ());
129 : }
130 :
131 158058 : TyWithLocation::TyWithLocation (BaseType *ty, location_t locus)
132 158058 : : ty (ty), locus (locus)
133 158058 : {}
134 :
135 1148507 : TyWithLocation::TyWithLocation (BaseType *ty) : ty (ty)
136 : {
137 1148507 : auto &mappings = Analysis::Mappings::get ();
138 1148507 : locus = mappings.lookup_location (ty->get_ref ());
139 1148507 : }
140 :
141 : } // namespace TyTy
142 : } // namespace Rust
|