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 4245634 : TyVar::TyVar (HirId ref) : ref (ref)
28 : {
29 : // ensure this reference is defined within the context
30 4245634 : auto context = Resolver::TypeCheckContext::get ();
31 4245634 : BaseType *lookup = nullptr;
32 4245634 : bool ok = context->lookup_type (ref, &lookup);
33 4245634 : if (!ok || lookup == nullptr || lookup->get_kind () == TypeKind::ERROR)
34 1 : return;
35 : }
36 :
37 : BaseType *
38 6208424 : TyVar::get_tyty () const
39 : {
40 6208424 : auto context = Resolver::TypeCheckContext::get ();
41 6208424 : BaseType *lookup = nullptr;
42 6208424 : bool ok = context->lookup_type (ref, &lookup);
43 6208424 : if (!ok || lookup == nullptr)
44 : return nullptr;
45 : return lookup;
46 : }
47 :
48 : TyVar
49 94556 : TyVar::get_implicit_infer_var (location_t locus)
50 : {
51 94556 : auto &mappings = Analysis::Mappings::get ();
52 94556 : auto context = Resolver::TypeCheckContext::get ();
53 :
54 94556 : HirId next = mappings.get_next_hir_id ();
55 94556 : auto infer = new InferType (next, InferType::InferTypeKind::GENERAL,
56 94556 : InferType::TypeHint::Default (), locus);
57 :
58 94556 : context->insert_implicit_type (infer->get_ref (), infer);
59 94556 : mappings.insert_location (infer->get_ref (), locus);
60 :
61 94556 : return TyVar (infer->get_ref ());
62 : }
63 :
64 : TyVar
65 106 : TyVar::get_implicit_const_infer_var (location_t locus, TyVar *implicit_type)
66 : {
67 106 : auto &mappings = Analysis::Mappings::get ();
68 106 : auto context = Resolver::TypeCheckContext::get ();
69 :
70 106 : TyVar it = (implicit_type != nullptr) ? *implicit_type
71 106 : : get_implicit_infer_var (locus);
72 106 : HirId next = mappings.get_next_hir_id ();
73 106 : auto infer = new ConstInferType (it.get_tyty (), next, next, {});
74 :
75 106 : context->insert_implicit_type (infer->get_ref (), infer);
76 106 : mappings.insert_location (infer->get_ref (), locus);
77 :
78 106 : return TyVar (infer->get_ref ());
79 : }
80 :
81 : TyVar
82 14448 : TyVar::subst_covariant_var (TyTy::BaseType *orig, TyTy::BaseType *subst)
83 : {
84 14448 : if (orig->get_kind () != TyTy::TypeKind::PARAM)
85 1594 : return TyVar (subst->get_ty_ref ());
86 12854 : else if (subst->get_kind () == TyTy::TypeKind::PARAM)
87 : {
88 12854 : TyTy::ParamType *p = static_cast<TyTy::ParamType *> (subst);
89 12854 : if (p->resolve ()->get_kind () == TyTy::TypeKind::PARAM)
90 : {
91 1459 : return TyVar (subst->get_ty_ref ());
92 : }
93 : }
94 :
95 11395 : return TyVar (subst->get_ref ());
96 : }
97 :
98 : TyVar
99 57614 : TyVar::clone () const
100 : {
101 57614 : TyTy::BaseType *base = get_tyty ();
102 57614 : if (base == nullptr || base->get_kind () == TypeKind::ERROR)
103 0 : return TyVar::get_implicit_infer_var (UNKNOWN_LOCATION);
104 57614 : TyTy::BaseType *c = base->clone ();
105 57614 : return TyVar (c->get_ref ());
106 : }
107 :
108 : TyVar
109 1837 : TyVar::monomorphized_clone () const
110 : {
111 1837 : auto &mappings = Analysis::Mappings::get ();
112 1837 : auto context = Resolver::TypeCheckContext::get ();
113 :
114 1837 : TyTy::BaseType *base = get_tyty ();
115 1837 : 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 1837 : TyTy::BaseType *c = get_tyty ()->monomorphized_clone ();
120 1837 : c->set_ref (mappings.get_next_hir_id ());
121 :
122 : // insert it
123 1837 : context->insert_type (Analysis::NodeMapping (mappings.get_current_crate (),
124 : UNKNOWN_NODEID, c->get_ref (),
125 1837 : UNKNOWN_LOCAL_DEFID),
126 : c);
127 :
128 1837 : return TyVar (c->get_ref ());
129 : }
130 :
131 168658 : TyWithLocation::TyWithLocation (BaseType *ty, location_t locus)
132 168658 : : ty (ty), locus (locus)
133 168658 : {}
134 :
135 4303934 : TyWithLocation::TyWithLocation (BaseType *ty) : ty (ty)
136 : {
137 4303934 : auto &mappings = Analysis::Mappings::get ();
138 4303934 : locus = mappings.lookup_location (ty->get_ref ());
139 4303934 : }
140 :
141 : } // namespace TyTy
142 : } // namespace Rust
|