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 : 1935044 : TyVar::TyVar (HirId ref) : ref (ref)
28 : : {
29 : : // ensure this reference is defined within the context
30 : 1935044 : auto context = Resolver::TypeCheckContext::get ();
31 : 1935044 : BaseType *lookup = nullptr;
32 : 1935044 : bool ok = context->lookup_type (ref, &lookup);
33 : 1935044 : rust_assert (ok);
34 : 1935044 : }
35 : :
36 : : BaseType *
37 : 3065965 : TyVar::get_tyty () const
38 : : {
39 : 3065965 : auto context = Resolver::TypeCheckContext::get ();
40 : 3065965 : BaseType *lookup = nullptr;
41 : 3065965 : bool ok = context->lookup_type (ref, &lookup);
42 : 3065965 : rust_assert (ok);
43 : 3065965 : return lookup;
44 : : }
45 : :
46 : : TyVar
47 : 11589 : TyVar::get_implicit_infer_var (location_t locus)
48 : : {
49 : 11589 : auto &mappings = Analysis::Mappings::get ();
50 : 11589 : auto context = Resolver::TypeCheckContext::get ();
51 : :
52 : 11589 : HirId next = mappings.get_next_hir_id ();
53 : 11589 : auto infer = new InferType (next, InferType::InferTypeKind::GENERAL,
54 : 11589 : InferType::TypeHint::Default (), locus);
55 : :
56 : 11589 : context->insert_implicit_type (infer->get_ref (), infer);
57 : 11589 : mappings.insert_location (infer->get_ref (), locus);
58 : :
59 : 11589 : return TyVar (infer->get_ref ());
60 : : }
61 : :
62 : : TyVar
63 : 12 : TyVar::get_implicit_const_infer_var (const ConstType &const_type,
64 : : location_t locus)
65 : : {
66 : 12 : auto &mappings = Analysis::Mappings::get ();
67 : 12 : auto context = Resolver::TypeCheckContext::get ();
68 : :
69 : 12 : HirId next = mappings.get_next_hir_id ();
70 : 12 : auto infer
71 : 12 : = new ConstType (ConstType::ConstKind::Infer, const_type.get_symbol (),
72 : : const_type.get_ty (), error_mark_node,
73 : 12 : const_type.get_specified_bounds (), locus, next, next, {});
74 : :
75 : 12 : context->insert_implicit_type (infer->get_ref (), infer);
76 : 12 : mappings.insert_location (infer->get_ref (), locus);
77 : :
78 : 12 : return TyVar (infer->get_ref ());
79 : : }
80 : :
81 : : TyVar
82 : 14398 : TyVar::subst_covariant_var (TyTy::BaseType *orig, TyTy::BaseType *subst)
83 : : {
84 : 14398 : if (orig->get_kind () != TyTy::TypeKind::PARAM)
85 : 1508 : return TyVar (subst->get_ty_ref ());
86 : 12890 : else if (subst->get_kind () == TyTy::TypeKind::PARAM)
87 : : {
88 : 12890 : TyTy::ParamType *p = static_cast<TyTy::ParamType *> (subst);
89 : 12890 : if (p->resolve ()->get_kind () == TyTy::TypeKind::PARAM)
90 : : {
91 : 1453 : return TyVar (subst->get_ty_ref ());
92 : : }
93 : : }
94 : :
95 : 11437 : return TyVar (subst->get_ref ());
96 : : }
97 : :
98 : : TyVar
99 : 57928 : TyVar::clone () const
100 : : {
101 : 57928 : TyTy::BaseType *c = get_tyty ()->clone ();
102 : 57928 : return TyVar (c->get_ref ());
103 : : }
104 : :
105 : : TyVar
106 : 1821 : TyVar::monomorphized_clone () const
107 : : {
108 : 1821 : auto &mappings = Analysis::Mappings::get ();
109 : 1821 : auto context = Resolver::TypeCheckContext::get ();
110 : :
111 : : // this needs a new hirid
112 : 1821 : TyTy::BaseType *c = get_tyty ()->monomorphized_clone ();
113 : 1821 : c->set_ref (mappings.get_next_hir_id ());
114 : :
115 : : // insert it
116 : 1821 : context->insert_type (Analysis::NodeMapping (mappings.get_current_crate (),
117 : : UNKNOWN_NODEID, c->get_ref (),
118 : 1821 : UNKNOWN_LOCAL_DEFID),
119 : : c);
120 : :
121 : 1821 : return TyVar (c->get_ref ());
122 : : }
123 : :
124 : 134641 : TyWithLocation::TyWithLocation (BaseType *ty, location_t locus)
125 : 134641 : : ty (ty), locus (locus)
126 : 134641 : {}
127 : :
128 : 410728 : TyWithLocation::TyWithLocation (BaseType *ty) : ty (ty)
129 : : {
130 : 410728 : auto &mappings = Analysis::Mappings::get ();
131 : 410728 : locus = mappings.lookup_location (ty->get_ref ());
132 : 410728 : }
133 : :
134 : : } // namespace TyTy
135 : : } // namespace Rust
|