Skip to content

Commit d54ca71

Browse files
Merge #1160
1160: Refactor name resolver r=CohenArthur a=CohenArthur This PR splits up the `rust-name-resolver.h` file into source and header. It also removes a bunch of `iterate_*` functions: Some were not used anymore, and some were refactored. Co-authored-by: Arthur Cohen <[email protected]>
2 parents bbbd15a + 23db789 commit d54ca71

File tree

5 files changed

+563
-541
lines changed

5 files changed

+563
-541
lines changed

gcc/rust/Make-lang.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ GRS_OBJS = \
8585
rust/rust-ast-lower.o \
8686
rust/rust-ast-lower-base.o \
8787
rust/rust-ast-lower-pattern.o \
88+
rust/rust-name-resolver.o \
8889
rust/rust-ast-resolve.o \
8990
rust/rust-ast-resolve-pattern.o \
9091
rust/rust-ast-resolve-expr.o \

gcc/rust/backend/rust-compile-base.cc

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -333,33 +333,33 @@ HIRCompileBase::compile_locals_for_block (Context *ctx, Resolver::Rib &rib,
333333
tree fndecl)
334334
{
335335
std::vector<Bvariable *> locals;
336-
rib.iterate_decls ([&] (NodeId n, Location) mutable -> bool {
337-
Resolver::Definition d;
338-
bool ok = ctx->get_resolver ()->lookup_definition (n, &d);
339-
rust_assert (ok);
340-
341-
HIR::Stmt *decl = nullptr;
342-
ok = ctx->get_mappings ()->resolve_nodeid_to_stmt (d.parent, &decl);
343-
rust_assert (ok);
344-
345-
// if its a function we extract this out side of this fn context
346-
// and it is not a local to this function
347-
bool is_item = ctx->get_mappings ()->lookup_hir_item (
348-
decl->get_mappings ().get_crate_num (),
349-
decl->get_mappings ().get_hirid ())
350-
!= nullptr;
351-
if (is_item)
352-
{
353-
HIR::Item *item = static_cast<HIR::Item *> (decl);
354-
CompileItem::compile (item, ctx);
355-
return true;
356-
}
357-
358-
Bvariable *compiled = CompileVarDecl::compile (fndecl, decl, ctx);
359-
locals.push_back (compiled);
336+
for (auto it : rib.get_declarations ())
337+
{
338+
auto node_id = it.first;
339+
340+
Resolver::Definition d;
341+
bool ok = ctx->get_resolver ()->lookup_definition (node_id, &d);
342+
rust_assert (ok);
343+
344+
HIR::Stmt *decl = nullptr;
345+
ok = ctx->get_mappings ()->resolve_nodeid_to_stmt (d.parent, &decl);
346+
rust_assert (ok);
347+
348+
// if its a function we extract this out side of this fn context
349+
// and it is not a local to this function
350+
bool is_item = ctx->get_mappings ()->lookup_hir_item (
351+
decl->get_mappings ().get_crate_num (),
352+
decl->get_mappings ().get_hirid ())
353+
!= nullptr;
354+
if (is_item)
355+
{
356+
HIR::Item *item = static_cast<HIR::Item *> (decl);
357+
CompileItem::compile (item, ctx);
358+
}
360359

361-
return true;
362-
});
360+
Bvariable *compiled = CompileVarDecl::compile (fndecl, decl, ctx);
361+
locals.push_back (compiled);
362+
};
363363

364364
return locals;
365365
}

gcc/rust/resolve/rust-ast-resolve.cc

Lines changed: 0 additions & 294 deletions
Original file line numberDiff line numberDiff line change
@@ -24,306 +24,12 @@
2424
#include "rust-ast-resolve-expr.h"
2525
#include "rust-ast-resolve-struct-expr-field.h"
2626

27-
#define MKBUILTIN_TYPE(_X, _R, _TY) \
28-
do \
29-
{ \
30-
AST::PathIdentSegment seg (_X, Linemap::predeclared_location ()); \
31-
auto typePath = ::std::unique_ptr<AST::TypePathSegment> ( \
32-
new AST::TypePathSegment (::std::move (seg), false, \
33-
Linemap::predeclared_location ())); \
34-
::std::vector< ::std::unique_ptr<AST::TypePathSegment> > segs; \
35-
segs.push_back (::std::move (typePath)); \
36-
auto builtin_type \
37-
= new AST::TypePath (::std::move (segs), \
38-
Linemap::predeclared_location (), false); \
39-
_R.push_back (builtin_type); \
40-
tyctx->insert_builtin (_TY->get_ref (), builtin_type->get_node_id (), \
41-
_TY); \
42-
} \
43-
while (0)
44-
4527
extern bool
4628
saw_errors (void);
4729

4830
namespace Rust {
4931
namespace Resolver {
5032

51-
// Resolver
52-
53-
Resolver::Resolver ()
54-
: mappings (Analysis::Mappings::get ()), tyctx (TypeCheckContext::get ()),
55-
name_scope (Scope (mappings->get_current_crate ())),
56-
type_scope (Scope (mappings->get_current_crate ())),
57-
label_scope (Scope (mappings->get_current_crate ())),
58-
macro_scope (Scope (mappings->get_current_crate ())),
59-
global_type_node_id (UNKNOWN_NODEID), unit_ty_node_id (UNKNOWN_NODEID)
60-
{
61-
generate_builtins ();
62-
}
63-
64-
Resolver *
65-
Resolver::get ()
66-
{
67-
static Resolver *instance;
68-
if (instance == nullptr)
69-
instance = new Resolver ();
70-
71-
return instance;
72-
}
73-
74-
void
75-
Resolver::push_new_name_rib (Rib *r)
76-
{
77-
rust_assert (name_ribs.find (r->get_node_id ()) == name_ribs.end ());
78-
name_ribs[r->get_node_id ()] = r;
79-
}
80-
81-
void
82-
Resolver::push_new_type_rib (Rib *r)
83-
{
84-
if (type_ribs.size () == 0)
85-
global_type_node_id = r->get_node_id ();
86-
87-
rust_assert (type_ribs.find (r->get_node_id ()) == type_ribs.end ());
88-
type_ribs[r->get_node_id ()] = r;
89-
}
90-
91-
void
92-
Resolver::push_new_label_rib (Rib *r)
93-
{
94-
rust_assert (label_ribs.find (r->get_node_id ()) == label_ribs.end ());
95-
label_ribs[r->get_node_id ()] = r;
96-
}
97-
98-
void
99-
Resolver::push_new_macro_rib (Rib *r)
100-
{
101-
rust_assert (label_ribs.find (r->get_node_id ()) == label_ribs.end ());
102-
macro_ribs[r->get_node_id ()] = r;
103-
}
104-
105-
bool
106-
Resolver::find_name_rib (NodeId id, Rib **rib)
107-
{
108-
auto it = name_ribs.find (id);
109-
if (it == name_ribs.end ())
110-
return false;
111-
112-
*rib = it->second;
113-
return true;
114-
}
115-
116-
bool
117-
Resolver::find_type_rib (NodeId id, Rib **rib)
118-
{
119-
auto it = type_ribs.find (id);
120-
if (it == type_ribs.end ())
121-
return false;
122-
123-
*rib = it->second;
124-
return true;
125-
}
126-
127-
bool
128-
Resolver::find_macro_rib (NodeId id, Rib **rib)
129-
{
130-
auto it = macro_ribs.find (id);
131-
if (it == macro_ribs.end ())
132-
return false;
133-
134-
*rib = it->second;
135-
return true;
136-
}
137-
138-
void
139-
Resolver::insert_builtin_types (Rib *r)
140-
{
141-
auto builtins = get_builtin_types ();
142-
for (auto &builtin : builtins)
143-
{
144-
CanonicalPath builtin_path
145-
= CanonicalPath::new_seg (builtin->get_node_id (),
146-
builtin->as_string ());
147-
r->insert_name (builtin_path, builtin->get_node_id (),
148-
Linemap::predeclared_location (), false,
149-
[] (const CanonicalPath &, NodeId, Location) -> void {});
150-
}
151-
}
152-
153-
std::vector<AST::Type *> &
154-
Resolver::get_builtin_types ()
155-
{
156-
return builtins;
157-
}
158-
159-
void
160-
Resolver::generate_builtins ()
161-
{
162-
auto u8
163-
= new TyTy::UintType (mappings->get_next_hir_id (), TyTy::UintType::U8);
164-
auto u16
165-
= new TyTy::UintType (mappings->get_next_hir_id (), TyTy::UintType::U16);
166-
auto u32
167-
= new TyTy::UintType (mappings->get_next_hir_id (), TyTy::UintType::U32);
168-
auto u64
169-
= new TyTy::UintType (mappings->get_next_hir_id (), TyTy::UintType::U64);
170-
auto u128
171-
= new TyTy::UintType (mappings->get_next_hir_id (), TyTy::UintType::U128);
172-
auto i8 = new TyTy::IntType (mappings->get_next_hir_id (), TyTy::IntType::I8);
173-
auto i16
174-
= new TyTy::IntType (mappings->get_next_hir_id (), TyTy::IntType::I16);
175-
auto i32
176-
= new TyTy::IntType (mappings->get_next_hir_id (), TyTy::IntType::I32);
177-
auto i64
178-
= new TyTy::IntType (mappings->get_next_hir_id (), TyTy::IntType::I64);
179-
auto i128
180-
= new TyTy::IntType (mappings->get_next_hir_id (), TyTy::IntType::I128);
181-
auto rbool = new TyTy::BoolType (mappings->get_next_hir_id ());
182-
auto f32
183-
= new TyTy::FloatType (mappings->get_next_hir_id (), TyTy::FloatType::F32);
184-
auto f64
185-
= new TyTy::FloatType (mappings->get_next_hir_id (), TyTy::FloatType::F64);
186-
auto usize = new TyTy::USizeType (mappings->get_next_hir_id ());
187-
auto isize = new TyTy::ISizeType (mappings->get_next_hir_id ());
188-
auto char_tyty = new TyTy::CharType (mappings->get_next_hir_id ());
189-
auto str = new TyTy::StrType (mappings->get_next_hir_id ());
190-
auto never = new TyTy::NeverType (mappings->get_next_hir_id ());
191-
192-
MKBUILTIN_TYPE ("u8", builtins, u8);
193-
MKBUILTIN_TYPE ("u16", builtins, u16);
194-
MKBUILTIN_TYPE ("u32", builtins, u32);
195-
MKBUILTIN_TYPE ("u64", builtins, u64);
196-
MKBUILTIN_TYPE ("u128", builtins, u128);
197-
MKBUILTIN_TYPE ("i8", builtins, i8);
198-
MKBUILTIN_TYPE ("i16", builtins, i16);
199-
MKBUILTIN_TYPE ("i32", builtins, i32);
200-
MKBUILTIN_TYPE ("i64", builtins, i64);
201-
MKBUILTIN_TYPE ("i128", builtins, i128);
202-
MKBUILTIN_TYPE ("bool", builtins, rbool);
203-
MKBUILTIN_TYPE ("f32", builtins, f32);
204-
MKBUILTIN_TYPE ("f64", builtins, f64);
205-
MKBUILTIN_TYPE ("usize", builtins, usize);
206-
MKBUILTIN_TYPE ("isize", builtins, isize);
207-
MKBUILTIN_TYPE ("char", builtins, char_tyty);
208-
MKBUILTIN_TYPE ("str", builtins, str);
209-
MKBUILTIN_TYPE ("!", builtins, never);
210-
211-
// unit type ()
212-
TyTy::TupleType *unit_tyty
213-
= TyTy::TupleType::get_unit_type (mappings->get_next_hir_id ());
214-
std::vector<std::unique_ptr<AST::Type> > elems;
215-
AST::TupleType *unit_type
216-
= new AST::TupleType (std::move (elems), Linemap::predeclared_location ());
217-
builtins.push_back (unit_type);
218-
tyctx->insert_builtin (unit_tyty->get_ref (), unit_type->get_node_id (),
219-
unit_tyty);
220-
set_unit_type_node_id (unit_type->get_node_id ());
221-
}
222-
223-
void
224-
Resolver::insert_new_definition (NodeId id, Definition def)
225-
{
226-
auto it = name_definitions.find (id);
227-
if (it != name_definitions.end ())
228-
{
229-
rust_assert (it->second.is_equal (def));
230-
return;
231-
}
232-
name_definitions[id] = def;
233-
}
234-
235-
bool
236-
Resolver::lookup_definition (NodeId id, Definition *def)
237-
{
238-
auto it = name_definitions.find (id);
239-
if (it == name_definitions.end ())
240-
return false;
241-
242-
*def = it->second;
243-
return true;
244-
}
245-
246-
void
247-
Resolver::insert_resolved_name (NodeId refId, NodeId defId)
248-
{
249-
resolved_names[refId] = defId;
250-
get_name_scope ().append_reference_for_def (refId, defId);
251-
}
252-
253-
bool
254-
Resolver::lookup_resolved_name (NodeId refId, NodeId *defId)
255-
{
256-
auto it = resolved_names.find (refId);
257-
if (it == resolved_names.end ())
258-
return false;
259-
260-
*defId = it->second;
261-
return true;
262-
}
263-
264-
void
265-
Resolver::insert_resolved_type (NodeId refId, NodeId defId)
266-
{
267-
// auto it = resolved_types.find (refId);
268-
// rust_assert (it == resolved_types.end ());
269-
270-
resolved_types[refId] = defId;
271-
get_type_scope ().append_reference_for_def (refId, defId);
272-
}
273-
274-
bool
275-
Resolver::lookup_resolved_type (NodeId refId, NodeId *defId)
276-
{
277-
auto it = resolved_types.find (refId);
278-
if (it == resolved_types.end ())
279-
return false;
280-
281-
*defId = it->second;
282-
return true;
283-
}
284-
285-
void
286-
Resolver::insert_resolved_label (NodeId refId, NodeId defId)
287-
{
288-
auto it = resolved_labels.find (refId);
289-
rust_assert (it == resolved_labels.end ());
290-
291-
resolved_labels[refId] = defId;
292-
get_label_scope ().append_reference_for_def (refId, defId);
293-
}
294-
295-
bool
296-
Resolver::lookup_resolved_label (NodeId refId, NodeId *defId)
297-
{
298-
auto it = resolved_labels.find (refId);
299-
if (it == resolved_labels.end ())
300-
return false;
301-
302-
*defId = it->second;
303-
return true;
304-
}
305-
306-
void
307-
Resolver::insert_resolved_macro (NodeId refId, NodeId defId)
308-
{
309-
auto it = resolved_macros.find (refId);
310-
rust_assert (it == resolved_macros.end ());
311-
312-
resolved_labels[refId] = defId;
313-
get_label_scope ().append_reference_for_def (refId, defId);
314-
}
315-
316-
bool
317-
Resolver::lookup_resolved_macro (NodeId refId, NodeId *defId)
318-
{
319-
auto it = resolved_macros.find (refId);
320-
if (it == resolved_macros.end ())
321-
return false;
322-
323-
*defId = it->second;
324-
return true;
325-
}
326-
32733
// NameResolution
32834

32935
NameResolution *

0 commit comments

Comments
 (0)