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-mapping-common.h"
21 : : #include "rust-system.h"
22 : : #include "rust-tyty.h"
23 : :
24 : : namespace Rust {
25 : : namespace TyTy {
26 : :
27 : 4250770 : TyVar::TyVar (HirId ref) : ref (ref)
28 : : {
29 : : // ensure this reference is defined within the context
30 : 4250770 : auto context = Resolver::TypeCheckContext::get ();
31 : 4250770 : BaseType *lookup = nullptr;
32 : 4250770 : bool ok = context->lookup_type (ref, &lookup);
33 : 4250770 : if (!ok || lookup == nullptr || lookup->get_kind () == TypeKind::ERROR)
34 : 1 : return;
35 : : }
36 : :
37 : : BaseType *
38 : 6213024 : TyVar::get_tyty () const
39 : : {
40 : 6213024 : auto context = Resolver::TypeCheckContext::get ();
41 : 6213024 : BaseType *lookup = nullptr;
42 : 6213024 : bool ok = context->lookup_type (ref, &lookup);
43 : 6213024 : if (!ok || lookup == nullptr)
44 : : return nullptr;
45 : : return lookup;
46 : : }
47 : :
48 : : TyVar
49 : 94549 : TyVar::get_implicit_infer_var (location_t locus)
50 : : {
51 : 94549 : auto &mappings = Analysis::Mappings::get ();
52 : 94549 : auto context = Resolver::TypeCheckContext::get ();
53 : :
54 : 94549 : HirId next = mappings.get_next_hir_id ();
55 : 94549 : auto infer = new InferType (next, InferType::InferTypeKind::GENERAL,
56 : 94549 : InferType::TypeHint::Default (), locus);
57 : :
58 : 94549 : context->insert_implicit_type (infer->get_ref (), infer);
59 : 94549 : mappings.insert_location (infer->get_ref (), locus);
60 : :
61 : 94549 : 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 : 57598 : TyVar::clone () const
100 : : {
101 : 57598 : TyTy::BaseType *base = get_tyty ();
102 : 57598 : if (base == nullptr || base->get_kind () == TypeKind::ERROR)
103 : 0 : return TyVar::get_implicit_infer_var (UNKNOWN_LOCATION);
104 : 57598 : TyTy::BaseType *c = base->clone ();
105 : 57598 : return TyVar (c->get_ref ());
106 : : }
107 : :
108 : : TyVar
109 : 1835 : TyVar::monomorphized_clone () const
110 : : {
111 : 1835 : auto &mappings = Analysis::Mappings::get ();
112 : 1835 : auto context = Resolver::TypeCheckContext::get ();
113 : :
114 : 1835 : TyTy::BaseType *base = get_tyty ();
115 : 1835 : 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 : 1835 : TyTy::BaseType *c = get_tyty ()->monomorphized_clone ();
120 : 1835 : c->set_ref (mappings.get_next_hir_id ());
121 : :
122 : : // insert it
123 : 1835 : context->insert_type (Analysis::NodeMapping (mappings.get_current_crate (),
124 : : UNKNOWN_NODEID, c->get_ref (),
125 : 1835 : UNKNOWN_LOCAL_DEFID),
126 : : c);
127 : :
128 : 1835 : return TyVar (c->get_ref ());
129 : : }
130 : :
131 : 168266 : TyWithLocation::TyWithLocation (BaseType *ty, location_t locus)
132 : 168266 : : ty (ty), locus (locus)
133 : 168266 : {}
134 : :
135 : 4303272 : TyWithLocation::TyWithLocation (BaseType *ty) : ty (ty)
136 : : {
137 : 4303272 : auto &mappings = Analysis::Mappings::get ();
138 : 4303272 : locus = mappings.lookup_location (ty->get_ref ());
139 : 4303272 : }
140 : :
141 : : } // namespace TyTy
142 : : } // namespace Rust
|