Skip to content

Commit d59e306

Browse files
committed
Move function definition out of header file
gcc/rust/ChangeLog: * resolve/rust-name-resolution-context.cc (BindingLayer::BindingLayer): Add new constructor for binding layer. (BindingLayer::bind_test): Add a function to test a binding constraint. (BindingLayer::push): Push a new binding group. (BindingLayer::and_binded): Add function to test and-binding constraint. (BindingLayer::or_binded): Add function to test or-binding constraints. (BindingLayer::insert_ident): Insert a new identifier in the current binding group. (BindingLayer::merge): Merge current binding group with it's parent. (BindingLayer::get_source): Get the source of the current binding group. (BindingContext::and_binded): Add function to test and-binding for current layer. (BindingContext::or_binded): Add function to test or-binding for current layer. (BindingContext::merge): Add function to apply merge on current layer. (BindingContext::push): Add function to apply push on current layer. (BindingContext::insert_ident): Add function to apply insert_ident on current layer. (BindingContext::new_binding): Push new binding over the stack of layers. (BindingContext::clear): Remove last binding layer from the stack. (BindingContext::get_source): Get the source of current binding layer. * resolve/rust-name-resolution-context.h (enum class): Remove function definition. Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
1 parent ded7e5a commit d59e306

File tree

2 files changed

+160
-52
lines changed

2 files changed

+160
-52
lines changed

gcc/rust/resolve/rust-name-resolution-context.cc

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,113 @@
2323
namespace Rust {
2424
namespace Resolver2_0 {
2525

26+
BindingLayer::BindingLayer (BindingSource source) : source (source)
27+
{
28+
push (Binding::Kind::Product);
29+
}
30+
31+
bool
32+
BindingLayer::bind_test (Identifier ident, Binding::Kind kind)
33+
{
34+
for (auto &bind : bindings)
35+
{
36+
if (bind.set.find (ident) != bind.set.cend () && bind.kind == kind)
37+
{
38+
return true;
39+
}
40+
}
41+
return false;
42+
}
43+
44+
void
45+
BindingLayer::push (Binding::Kind kind)
46+
{
47+
bindings.push_back (Binding (kind));
48+
}
49+
50+
bool
51+
BindingLayer::and_binded (Identifier ident)
52+
{
53+
return bind_test (ident, Binding::Kind::Product);
54+
}
55+
56+
bool
57+
BindingLayer::or_binded (Identifier ident)
58+
{
59+
return bind_test (ident, Binding::Kind::Or);
60+
}
61+
62+
void
63+
BindingLayer::insert_ident (Identifier ident)
64+
{
65+
bindings.back ().set.insert (ident);
66+
}
67+
68+
void
69+
BindingLayer::merge ()
70+
{
71+
auto last_binding = bindings.back ();
72+
bindings.pop_back ();
73+
for (auto &value : last_binding.set)
74+
{
75+
bindings.back ().set.insert (value);
76+
}
77+
}
78+
79+
BindingSource
80+
BindingLayer::get_source () const
81+
{
82+
return source;
83+
}
84+
85+
bool
86+
BindingContext::and_binded (Identifier ident)
87+
{
88+
return bindings.back ().and_binded (ident);
89+
}
90+
91+
bool
92+
BindingContext::or_binded (Identifier ident)
93+
{
94+
return bindings.back ().or_binded (ident);
95+
}
96+
97+
void
98+
BindingContext::merge ()
99+
{
100+
bindings.back ().merge ();
101+
}
102+
103+
void
104+
BindingContext::push (Binding::Kind kind)
105+
{
106+
bindings.back ().push (kind);
107+
}
108+
109+
void
110+
BindingContext::insert_ident (Identifier ident)
111+
{
112+
bindings.back ().insert_ident (ident);
113+
}
114+
115+
void
116+
BindingContext::new_binding (BindingSource source)
117+
{
118+
bindings.emplace_back (source);
119+
}
120+
121+
void
122+
BindingContext::clear ()
123+
{
124+
bindings.pop_back ();
125+
}
126+
127+
BindingSource
128+
BindingContext::get_source () const
129+
{
130+
return bindings.back ().get_source ();
131+
}
132+
26133
NameResolutionContext::NameResolutionContext ()
27134
: mappings (Analysis::Mappings::get ())
28135
{}

gcc/rust/resolve/rust-name-resolution-context.h

Lines changed: 53 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,15 @@ struct Binding
169169
Binding (Binding::Kind kind) : kind (kind) {}
170170
};
171171

172+
/**
173+
* Used to identify the source of a binding, and emit the correct error message.
174+
*/
172175
enum class BindingSource
173176
{
174177
Match,
175178
Let,
176179
For,
180+
/* Closure param or function param */
177181
Param
178182
};
179183

@@ -182,79 +186,76 @@ class BindingLayer
182186
BindingSource source;
183187
std::vector<Binding> bindings;
184188

185-
bool bind_test (Identifier ident, Binding::Kind kind)
186-
{
187-
for (auto &bind : bindings)
188-
{
189-
if (bind.set.find (ident) != bind.set.cend () && bind.kind == kind)
190-
{
191-
return true;
192-
}
193-
}
194-
return false;
195-
}
189+
bool bind_test (Identifier ident, Binding::Kind kind);
196190

197191
public:
198-
void push (Binding::Kind kind) { bindings.push_back (Binding (kind)); }
192+
void push (Binding::Kind kind);
199193

200-
BindingLayer (BindingSource source) : source (source)
201-
{
202-
push (Binding::Kind::Product);
203-
}
204-
bool and_binded (Identifier ident)
205-
{
206-
return bind_test (ident, Binding::Kind::Product);
207-
}
194+
BindingLayer (BindingSource source);
208195

209-
bool or_binded (Identifier ident)
210-
{
211-
return bind_test (ident, Binding::Kind::Or);
212-
}
196+
/**
197+
* Identifies if the identifier has been used in a product binding context.
198+
* eg. `let (a, a) = test();`
199+
*/
200+
bool and_binded (Identifier ident);
213201

214-
void insert_ident (Identifier ident) { bindings.back ().set.insert (ident); }
202+
/**
203+
* Identifies if the identifier has been used in a or context.
204+
* eg. `let (a, 1) | (a, 2) = test()`
205+
*/
206+
bool or_binded (Identifier ident);
215207

216-
void merge ()
217-
{
218-
auto last_binding = bindings.back ();
219-
bindings.pop_back ();
220-
for (auto &value : last_binding.set)
221-
{
222-
bindings.back ().set.insert (value);
223-
}
224-
}
208+
void insert_ident (Identifier ident);
225209

226-
BindingSource get_source () const { return source; }
210+
void merge ();
211+
212+
BindingSource get_source () const;
227213
};
228214

229215
class BindingContext
230216
{
231217
std::vector<BindingLayer> bindings;
232218

233219
public:
234-
bool and_binded (Identifier ident)
235-
{
236-
return bindings.back ().and_binded (ident);
237-
}
220+
/**
221+
* Identifies if the identifier is and-binded in the current layer's bindings.
222+
*/
223+
bool and_binded (Identifier ident);
238224

239-
bool or_binded (Identifier ident)
240-
{
241-
return bindings.back ().or_binded (ident);
242-
}
225+
/**
226+
* Identifies if the identifier is or-binded in the current layer's bindings.
227+
*/
228+
bool or_binded (Identifier ident);
243229

244-
void merge () { bindings.back ().merge (); }
230+
/**
231+
* Remove current layer's last binding level and merge it with it's parent.
232+
*/
233+
void merge ();
245234

246-
void push (Binding::Kind kind) { bindings.back ().push (kind); }
235+
/**
236+
* Push a new binding level in the current binding layer.
237+
*/
238+
void push (Binding::Kind kind);
247239

248-
void insert_ident (Identifier ident)
249-
{
250-
bindings.back ().insert_ident (ident);
251-
}
240+
/**
241+
* Insert a new identifier in the current binding layer.
242+
*/
243+
void insert_ident (Identifier ident);
252244

253-
void new_binding (BindingSource source) { bindings.emplace_back (source); }
245+
/**
246+
* Get current layer's binding source.
247+
*/
248+
BindingSource get_source () const;
254249

255-
void clear () { bindings.pop_back (); }
250+
/**
251+
* Create a new binding layer.
252+
*/
253+
void new_binding (BindingSource source);
256254

257-
BindingSource get_source () const { return bindings.back ().get_source (); }
255+
/**
256+
* Remove current binding layer.
257+
*/
258+
void clear ();
258259
};
259260

260261
// Now our resolver, which keeps track of all the `ForeverStack`s we could want

0 commit comments

Comments
 (0)