Skip to content

Commit 7aa2aab

Browse files
committed
fix path handling
1 parent c337d62 commit 7aa2aab

File tree

8 files changed

+120
-32
lines changed

8 files changed

+120
-32
lines changed

gcc/rust/ast/rust-ast-builder.cc

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,63 @@ Builder::type_path (LangItem::Kind lang_item) const
276276
return type_path (type_path_segment (lang_item));
277277
}
278278

279+
TypePath
280+
Builder::type_path_core (std::vector<std::unique_ptr<TypePathSegment>> &&segments) const
281+
{
282+
bool opening_scope;
283+
284+
auto &mappings = Analysis::Mappings::get ();
285+
if (mappings.get_current_crate_name () == "core")
286+
{
287+
segments.emplace (segments.cbegin (), type_path_segment ("crate"));
288+
opening_scope = false;
289+
}
290+
else
291+
{
292+
segments.emplace (segments.cbegin (), type_path_segment ("core"));
293+
opening_scope = true;
294+
}
295+
296+
return TypePath (std::move (segments), loc, opening_scope);
297+
}
298+
299+
TypePath
300+
Builder::type_path_core (std::vector<std::string> &&segments) const
301+
{
302+
auto type_segments = std::vector<std::unique_ptr<TypePathSegment>> ();
303+
type_segments.reserve (segments.size () + 1);
304+
bool opening_scope;
305+
306+
auto &mappings = Analysis::Mappings::get ();
307+
if (mappings.get_current_crate_name () == "core")
308+
{
309+
type_segments.emplace_back (type_path_segment ("crate"));
310+
opening_scope = false;
311+
}
312+
else
313+
{
314+
type_segments.emplace_back (type_path_segment ("core"));
315+
opening_scope = true;
316+
}
317+
318+
for (auto &&seg : segments)
319+
type_segments.emplace_back (type_path_segment (seg));
320+
321+
return TypePath (std::move (type_segments), loc, opening_scope);
322+
}
323+
324+
TypePath
325+
Builder::type_path_core (std::initializer_list<const char *> segments) const
326+
{
327+
std::vector<std::string> segments_out;
328+
segments_out.reserve (segments.size ());
329+
330+
for (auto seg : segments)
331+
segments_out.emplace_back (seg);
332+
333+
return type_path_core (std::move (segments_out));
334+
}
335+
279336
std::unique_ptr<Type>
280337
Builder::reference_type (std::unique_ptr<TypeNoBounds> &&inner_type,
281338
bool mutability) const
@@ -301,6 +358,31 @@ Builder::path_in_expression (LangItem::Kind lang_item) const
301358
return PathInExpression (lang_item, {}, loc);
302359
}
303360

361+
PathInExpression
362+
Builder::path_in_expression_core (std::vector<std::string> &&segments) const
363+
{
364+
auto path_segments = std::vector<PathExprSegment> ();
365+
path_segments.reserve (segments.size () + 1);
366+
bool opening_scope;
367+
368+
auto &mappings = Analysis::Mappings::get ();
369+
if (mappings.get_current_crate_name () == "core")
370+
{
371+
path_segments.emplace_back (path_segment ("crate"));
372+
opening_scope = false;
373+
}
374+
else
375+
{
376+
path_segments.emplace_back (path_segment ("core"));
377+
opening_scope = true;
378+
}
379+
380+
for (auto &&seg : segments)
381+
path_segments.emplace_back (path_segment (seg));
382+
383+
return PathInExpression (std::move (path_segments), {}, loc, opening_scope);
384+
}
385+
304386
PathInExpression
305387
Builder::variant_path (const std::string &enum_path,
306388
const std::string &variant) const
@@ -544,7 +626,7 @@ std::unique_ptr<Stmt>
544626
Builder::discriminant_value (std::string binding_name, std::string instance)
545627
{
546628
auto intrinsic = ptrify (
547-
path_in_expression ({"core", "intrinsics", "discriminant_value"}, true));
629+
path_in_expression_core ({"intrinsics", "discriminant_value"}));
548630

549631
return let (identifier_pattern (binding_name), nullptr,
550632
call (std::move (intrinsic), identifier (instance)));

gcc/rust/ast/rust-ast-builder.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,14 @@ class Builder
220220
TypePath type_path (std::string type) const;
221221
TypePath type_path (LangItem::Kind lang_item) const;
222222

223+
/**
224+
* Creates a type path relative to libcore
225+
* Either `::core...` or `crate...`
226+
*/
227+
TypePath type_path_core (std::vector<std::unique_ptr<TypePathSegment>> &&segments) const;
228+
TypePath type_path_core (std::vector<std::string> &&segments) const;
229+
TypePath type_path_core (std::initializer_list<const char *> segments) const;
230+
223231
std::unique_ptr<Type>
224232
reference_type (std::unique_ptr<TypeNoBounds> &&inner_type,
225233
bool mutability = false) const;
@@ -237,6 +245,12 @@ class Builder
237245
*/
238246
PathInExpression path_in_expression (LangItem::Kind lang_item) const;
239247

248+
/**
249+
* Create a path in expression relative to libcore
250+
* Either `::core...` or `crate...`
251+
*/
252+
PathInExpression path_in_expression_core (std::vector<std::string> &&segments) const;
253+
240254
/* Create the path to an enum's variant (`Result::Ok`) */
241255
PathInExpression variant_path (const std::string &enum_path,
242256
const std::string &variant) const;
@@ -331,11 +345,6 @@ class Builder
331345

332346
/* Location of the generated AST nodes */
333347
location_t loc;
334-
335-
private:
336-
/* Some constexpr helpers for some of the builders */
337-
static constexpr std::initializer_list<const char *> discriminant_value_path
338-
= {"core", "intrinsics", "discriminant_value"};
339348
};
340349

341350
} // namespace AST

gcc/rust/expand/rust-derive-debug.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ DeriveDebug::stub_debug_fn ()
5555
auto self = builder.self_ref_param ();
5656

5757
auto return_type
58-
= ptrify (builder.type_path ({"core", "fmt", "Result"}, true));
58+
= ptrify (builder.type_path_core ({"fmt", "Result"}));
5959

6060
auto mut_fmt_type_inner
61-
= ptrify (builder.type_path ({"core", "fmt", "Formatter"}, true));
61+
= ptrify (builder.type_path_core ({"fmt", "Formatter"}));
6262

6363
auto mut_fmt_type
6464
= builder.reference_type (std::move (mut_fmt_type_inner), true);
@@ -81,7 +81,7 @@ DeriveDebug::stub_derive_impl (
8181
{
8282
auto trait_items = vec (stub_debug_fn ());
8383

84-
auto debug = builder.type_path ({"core", "fmt", "Debug"}, true);
84+
auto debug = builder.type_path_core ({"fmt", "Debug"});
8585
auto generics
8686
= setup_impl_generics (name, type_generics, builder.trait_bound (debug));
8787

gcc/rust/expand/rust-derive-default.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ DeriveDefault::go (Item &item)
4242
std::unique_ptr<Expr>
4343
DeriveDefault::default_call (std::unique_ptr<Type> &&type)
4444
{
45-
auto default_trait = builder.type_path ({"core", "default", "Default"}, true);
45+
auto default_trait = builder.type_path_core ({"default", "Default"});
4646

4747
auto default_fn
4848
= builder.qualified_path_in_expression (std::move (type), default_trait,
@@ -69,7 +69,7 @@ DeriveDefault::default_impl (
6969
std::unique_ptr<AssociatedItem> &&default_fn, std::string name,
7070
const std::vector<std::unique_ptr<GenericParam>> &type_generics)
7171
{
72-
auto default_path = builder.type_path ({"core", "default", "Default"}, true);
72+
auto default_path = builder.type_path_core ({"default", "Default"});
7373

7474
auto trait_items = vec (std::move (default_fn));
7575

gcc/rust/expand/rust-derive-eq.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ std::unique_ptr<Stmt>
6161
DeriveEq::assert_param_is_eq ()
6262
{
6363
auto eq_bound = std::unique_ptr<TypeParamBound> (
64-
new TraitBound (builder.type_path ({"core", "cmp", "Eq"}, true), loc));
64+
new TraitBound (builder.type_path_core ({"cmp", "Eq"}), loc));
6565

6666
auto sized_bound = std::unique_ptr<TypeParamBound> (
6767
new TraitBound (builder.type_path (LangItem::Kind::SIZED), loc, false,
@@ -113,9 +113,9 @@ DeriveEq::eq_impls (
113113
const std::vector<std::unique_ptr<GenericParam>> &type_generics)
114114
{
115115
// We create two copies of the type-path to avoid duplicate NodeIds
116-
auto eq = builder.type_path ({"core", "cmp", "Eq"}, true);
116+
auto eq = builder.type_path_core ({"cmp", "Eq"});
117117
auto eq_bound
118-
= builder.trait_bound (builder.type_path ({"core", "cmp", "Eq"}, true));
118+
= builder.trait_bound (builder.type_path_core ({"cmp", "Eq"}));
119119

120120
auto steq = builder.type_path (LangItem::Kind::STRUCTURAL_TEQ);
121121

gcc/rust/expand/rust-derive-hash.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ std::unique_ptr<Expr>
4242
DeriveHash::hash_call (std::unique_ptr<Expr> &&value)
4343
{
4444
auto hash
45-
= builder.path_in_expression ({"core", "hash", "Hash", "hash"}, true);
45+
= builder.path_in_expression_core ({"hash", "Hash", "hash"});
4646

4747
return builder.call (ptrify (hash),
4848
vec (std::move (value),
@@ -64,7 +64,7 @@ DeriveHash::hash_fn (std::unique_ptr<BlockExpr> &&block)
6464

6565
auto params = vec (builder.self_ref_param (), std::move (state_param));
6666
auto bounds = vec (
67-
builder.trait_bound (builder.type_path ({"core", "hash", "Hasher"}, true)));
67+
builder.trait_bound (builder.type_path_core ({"hash", "Hasher"})));
6868
auto generics = vec (
6969
builder.generic_type_param (DeriveHash::state_type, std::move (bounds)));
7070

@@ -77,7 +77,7 @@ DeriveHash::hash_impl (
7777
std::unique_ptr<AssociatedItem> &&hash_fn, std::string name,
7878
const std::vector<std::unique_ptr<GenericParam>> &type_generics)
7979
{
80-
auto hash_path = builder.type_path ({"core", "hash", "Hash"}, true);
80+
auto hash_path = builder.type_path_core ({"hash", "Hash"});
8181

8282
auto trait_items = vec (std::move (hash_fn));
8383

gcc/rust/expand/rust-derive-ord.cc

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ std::unique_ptr<Expr>
4242
DeriveOrd::cmp_call (std::unique_ptr<Expr> &&self_expr,
4343
std::unique_ptr<Expr> &&other_expr)
4444
{
45-
auto cmp_fn_path = builder.path_in_expression (
46-
{"core", "cmp", trait (ordering), fn (ordering)}, true);
45+
auto cmp_fn_path = builder.path_in_expression_core (
46+
{"cmp", trait (ordering), fn (ordering)});
4747

4848
return builder.call (ptrify (cmp_fn_path),
4949
vec (builder.ref (std::move (self_expr)),
@@ -58,10 +58,10 @@ DeriveOrd::cmp_impl (
5858
auto fn = cmp_fn (std::move (fn_block), type_name);
5959

6060
auto trait = ordering == Ordering::Partial ? "PartialOrd" : "Ord";
61-
auto trait_path = builder.type_path ({"core", "cmp", trait}, true);
61+
auto trait_path = builder.type_path_core ({"cmp", trait});
6262

6363
auto trait_bound
64-
= builder.trait_bound (builder.type_path ({"core", "cmp", trait}, true));
64+
= builder.trait_bound (builder.type_path_core ({"cmp", trait}));
6565

6666
auto trait_items = vec (std::move (fn));
6767

@@ -78,7 +78,7 @@ std::unique_ptr<AssociatedItem>
7878
DeriveOrd::cmp_fn (std::unique_ptr<BlockExpr> &&block, Identifier type_name)
7979
{
8080
// Ordering
81-
auto return_type = builder.type_path ({"core", "cmp", "Ordering"}, true);
81+
auto return_type = builder.type_path_core ({"cmp", "Ordering"});
8282

8383
// In the case of PartialOrd, we return an Option<Ordering>
8484
if (ordering == Ordering::Partial)
@@ -87,13 +87,11 @@ DeriveOrd::cmp_fn (std::unique_ptr<BlockExpr> &&block, Identifier type_name)
8787

8888
auto generic_seg = builder.type_path_segment_generic (
8989
"Option", GenericArgs ({}, {generic}, {}, loc));
90-
auto core = builder.type_path_segment ("core");
9190
auto option = builder.type_path_segment ("option");
9291

9392
return_type
94-
= builder.type_path (vec (std::move (core), std::move (option),
95-
std::move (generic_seg)),
96-
true);
93+
= builder.type_path_core (vec (std::move (option),
94+
std::move (generic_seg)));
9795
}
9896

9997
// &self, other: &Self
@@ -113,7 +111,7 @@ std::unique_ptr<Pattern>
113111
DeriveOrd::make_equal ()
114112
{
115113
std::unique_ptr<Pattern> equal = ptrify (
116-
builder.path_in_expression ({"core", "cmp", "Ordering", "Equal"}, true));
114+
builder.path_in_expression_core ({"cmp", "Ordering", "Equal"}));
117115

118116
// We need to wrap the pattern in Option::Some if we are doing partial
119117
// ordering
@@ -148,8 +146,7 @@ DeriveOrd::recursive_match (std::vector<SelfOther> &&members)
148146
if (members.empty ())
149147
{
150148
std::unique_ptr<Expr> value = ptrify (
151-
builder.path_in_expression ({"core", "cmp", "Ordering", "Equal"},
152-
true));
149+
builder.path_in_expression_core ({"cmp", "Ordering", "Equal"}));
153150

154151
if (ordering == Ordering::Partial)
155152
value = builder.call (ptrify (builder.path_in_expression (

gcc/rust/expand/rust-expand-format-args.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ format_arg (const AST::Builder &builder, std::unique_ptr<AST::Expr> &&to_format,
3636
const std::string &trait)
3737
{
3838
auto formatter_fn = std::unique_ptr<AST::Expr> (new AST::PathInExpression (
39-
builder.path_in_expression ({"core", "fmt", trait, "fmt"})));
39+
builder.path_in_expression_core ({"fmt", trait, "fmt"})));
4040

4141
auto path = std::unique_ptr<AST::Expr> (new AST::PathInExpression (
42-
builder.path_in_expression ({"core", "fmt", "ArgumentV1", "new"})));
42+
builder.path_in_expression_core ({"fmt", "ArgumentV1", "new"})));
4343

4444
auto args = std::vector<std::unique_ptr<AST::Expr>> ();
4545
args.emplace_back (std::move (to_format));
@@ -123,7 +123,7 @@ expand_format_args (AST::FormatArgs &fmt,
123123
auto args_slice = builder.ref (builder.array (std::move (args_array)));
124124

125125
auto final_path = std::make_unique<AST::PathInExpression> (
126-
builder.path_in_expression ({"core", "fmt", "Arguments", "new_v1"}));
126+
builder.path_in_expression_core ({"fmt", "Arguments", "new_v1"}));
127127
auto final_args = std::vector<std::unique_ptr<AST::Expr>> ();
128128
final_args.emplace_back (std::move (pieces));
129129
final_args.emplace_back (std::move (args_slice));

0 commit comments

Comments
 (0)