1 // Copyright (C) 2020-2022 Free Software Foundation, Inc.
3 // This file is part of GCC.
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
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
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/>.
19 #ifndef RUST_AST_RESOLVE_TYPE_H
20 #define RUST_AST_RESOLVE_TYPE_H
22 #include "rust-ast-resolve-base.h"
23 #include "rust-ast-resolve-expr.h"
24 #include "rust-ast-full.h"
29 class ResolveRelativeTypePath
32 static bool go (AST::TypePath
&path
, NodeId
&resolved_node_id
);
35 class ResolveRelativeQualTypePath
: public ResolverBase
37 using ResolverBase::visit
;
40 static bool go (AST::QualifiedPathInType
&path
);
42 void visit (AST::TypePathSegmentGeneric
&seg
) override
;
44 void visit (AST::TypePathSegment
&seg
) override
;
47 bool resolve_qual_seg (AST::QualifiedPathType
&seg
);
50 ResolveRelativeQualTypePath ();
55 class ResolveType
: public ResolverBase
57 using Rust::Resolver::ResolverBase::visit
;
60 static NodeId
go (AST::Type
*type
)
63 type
->accept_vis (resolver
);
64 return resolver
.resolved_node
;
67 void visit (AST::BareFunctionType
&fntype
) override
69 for (auto ¶m
: fntype
.get_function_params ())
70 ResolveType::go (param
.get_type ().get ());
72 if (fntype
.has_return_type ())
73 ResolveType::go (fntype
.get_return_type ().get ());
76 void visit (AST::TupleType
&tuple
) override
78 if (tuple
.is_unit_type ())
80 resolved_node
= resolver
->get_unit_type_node_id ();
84 for (auto &elem
: tuple
.get_elems ())
85 ResolveType::go (elem
.get ());
88 void visit (AST::TypePath
&path
) override
90 ResolveRelativeTypePath::go (path
, resolved_node
);
93 void visit (AST::QualifiedPathInType
&path
) override
95 ResolveRelativeQualTypePath::go (path
);
98 void visit (AST::ArrayType
&type
) override
;
100 void visit (AST::ReferenceType
&type
) override
;
102 void visit (AST::InferredType
&type
) override
;
104 void visit (AST::NeverType
&type
) override
;
106 void visit (AST::RawPointerType
&type
) override
;
108 void visit (AST::TraitObjectTypeOneBound
&type
) override
;
110 void visit (AST::TraitObjectType
&type
) override
;
112 void visit (AST::SliceType
&type
) override
;
115 ResolveType () : ResolverBase () {}
118 class ResolveTypeBound
: public ResolverBase
120 using Rust::Resolver::ResolverBase::visit
;
123 static NodeId
go (AST::TypeParamBound
*type
)
125 ResolveTypeBound resolver
;
126 type
->accept_vis (resolver
);
127 return resolver
.resolved_node
;
130 void visit (AST::TraitBound
&bound
) override
132 resolved_node
= ResolveType::go (&bound
.get_type_path ());
136 ResolveTypeBound () : ResolverBase () {}
139 class ResolveGenericParam
: public ResolverBase
141 using Rust::Resolver::ResolverBase::visit
;
144 static NodeId
go (AST::GenericParam
*param
, const CanonicalPath
&prefix
,
145 const CanonicalPath
&canonical_prefix
)
147 ResolveGenericParam
resolver (prefix
, canonical_prefix
);
148 param
->accept_vis (resolver
);
149 return resolver
.resolved_node
;
152 void visit (AST::ConstGenericParam
¶m
) override
154 ResolveType::go (param
.get_type ().get ());
156 if (param
.has_default_value ())
157 ResolveExpr::go (param
.get_default_value ().get_expression ().get (),
158 prefix
, canonical_prefix
);
163 void visit (AST::TypeParam
¶m
) override
165 // if it has a type lets resolve it
166 if (param
.has_type ())
167 ResolveType::go (param
.get_type ().get ());
169 if (param
.has_type_param_bounds ())
171 for (auto &bound
: param
.get_type_param_bounds ())
173 ResolveTypeBound::go (bound
.get ());
177 auto seg
= CanonicalPath::new_seg (param
.get_node_id (),
178 param
.get_type_representation ());
179 resolver
->get_type_scope ().insert (
180 seg
, param
.get_node_id (), param
.get_locus (), false,
181 [&] (const CanonicalPath
&, NodeId
, Location locus
) -> void {
182 rust_error_at (param
.get_locus (),
183 "generic param redefined multiple times");
184 rust_error_at (locus
, "was defined here");
187 mappings
->insert_canonical_path (param
.get_node_id (), seg
);
191 ResolveGenericParam (const CanonicalPath
&prefix
,
192 const CanonicalPath
&canonical_prefix
)
193 : ResolverBase (), ok (false), prefix (prefix
),
194 canonical_prefix (canonical_prefix
)
198 const CanonicalPath
&prefix
;
199 const CanonicalPath
&canonical_prefix
;
202 class ResolveWhereClause
: public ResolverBase
204 using Rust::Resolver::ResolverBase::visit
;
207 static void Resolve (AST::WhereClause
&where_clause
)
209 ResolveWhereClause r
;
210 for (auto &clause
: where_clause
.get_items ())
211 clause
->accept_vis (r
);
214 void visit (AST::TypeBoundWhereClauseItem
&item
) override
216 ResolveType::go (item
.get_type ().get ());
217 if (item
.has_type_param_bounds ())
219 for (auto &bound
: item
.get_type_param_bounds ())
221 ResolveTypeBound::go (bound
.get ());
227 ResolveWhereClause () : ResolverBase () {}
230 class ResolveTypeToCanonicalPath
: public ResolverBase
232 using Rust::Resolver::ResolverBase::visit
;
235 static bool go (AST::Type
*type
, CanonicalPath
&result
);
237 void visit (AST::TypePath
&path
) override
;
239 void visit (AST::ReferenceType
&type
) override
;
241 void visit (AST::RawPointerType
&type
) override
;
243 void visit (AST::SliceType
&type
) override
;
246 ResolveTypeToCanonicalPath ();
248 CanonicalPath result
;
251 class ResolveGenericArgs
: public ResolverBase
253 using Rust::Resolver::ResolverBase::visit
;
256 static void go (AST::GenericArgs
&generic_args
);
257 static void go (AST::GenericArgs
&generic_args
, const CanonicalPath
&prefix
,
258 const CanonicalPath
&canonical_prefix
);
261 ResolveGenericArgs (const CanonicalPath
&prefix
,
262 const CanonicalPath
&canonical_prefix
)
263 : ResolverBase (), prefix (prefix
), canonical_prefix (canonical_prefix
)
266 bool is_type_name (const CanonicalPath
&path
);
267 bool is_const_value_name (const CanonicalPath
&path
);
270 * Resolve a disambiguated generic arg
272 void disambiguate (AST::GenericArg
&arg
);
275 * Resolve a disambiguated generic arg
277 void resolve_disambiguated_generic (AST::GenericArg
&arg
);
279 const CanonicalPath
&prefix
;
280 const CanonicalPath
&canonical_prefix
;
283 } // namespace Resolver
286 #endif // RUST_AST_RESOLVE_TYPE_H