@@ -890,92 +890,85 @@ ResolveItem::resolve_extern_item (AST::ExternalItem *item)
890
890
}
891
891
892
892
static void
893
- flatten_glob (const AST::UseTreeGlob &glob,
894
- std::vector<AST::SimplePath> &paths);
893
+ flatten_glob (const AST::UseTreeGlob &glob, std::vector<Import> &imports);
895
894
static void
896
- flatten_rebind (const AST::UseTreeRebind &glob,
897
- std::vector<AST::SimplePath> &paths);
895
+ flatten_rebind (const AST::UseTreeRebind &glob, std::vector<Import> &imports);
898
896
static void
899
- flatten_list (const AST::UseTreeList &glob,
900
- std::vector<AST::SimplePath> &paths);
897
+ flatten_list (const AST::UseTreeList &glob, std::vector<Import> &imports);
901
898
902
899
static void
903
- flatten (const AST::UseTree *tree, std::vector<AST::SimplePath > &paths )
900
+ flatten (const AST::UseTree *tree, std::vector<Import > &imports )
904
901
{
905
902
switch (tree->get_kind ())
906
903
{
907
904
case AST::UseTree::Glob: {
908
905
auto glob = static_cast <const AST::UseTreeGlob *> (tree);
909
- flatten_glob (*glob, paths );
906
+ flatten_glob (*glob, imports );
910
907
break ;
911
908
}
912
909
case AST::UseTree::Rebind: {
913
910
auto rebind = static_cast <const AST::UseTreeRebind *> (tree);
914
- flatten_rebind (*rebind, paths );
911
+ flatten_rebind (*rebind, imports );
915
912
break ;
916
913
}
917
914
case AST::UseTree::List: {
918
915
auto list = static_cast <const AST::UseTreeList *> (tree);
919
- flatten_list (*list, paths );
916
+ flatten_list (*list, imports );
920
917
break ;
921
918
}
922
919
break ;
923
920
}
924
921
}
925
922
926
923
static void
927
- flatten_glob (const AST::UseTreeGlob &glob, std::vector<AST::SimplePath > &paths )
924
+ flatten_glob (const AST::UseTreeGlob &glob, std::vector<Import > &imports )
928
925
{
929
926
if (glob.has_path ())
930
- paths .emplace_back (glob.get_path ());
927
+ imports .emplace_back (glob.get_path (), true , std::string ());
931
928
}
932
929
933
930
static void
934
- flatten_rebind (const AST::UseTreeRebind &rebind,
935
- std::vector<AST::SimplePath> &paths)
931
+ flatten_rebind (const AST::UseTreeRebind &rebind, std::vector<Import> &imports)
936
932
{
937
933
auto path = rebind.get_path ();
938
- if (rebind.has_path ())
939
- paths.emplace_back (path);
940
934
941
- // FIXME: Do we want to emplace the rebind here as well?
935
+ std::string label;
942
936
if (rebind.has_identifier ())
943
- {
944
- auto rebind_path = path;
945
- auto new_seg = rebind.get_identifier ();
946
-
947
- // Add the identifier as a new path
948
- rebind_path.get_segments ().back ()
949
- = AST::SimplePathSegment (new_seg, Location ());
937
+ label = rebind.get_identifier ();
938
+ else
939
+ label = path.get_final_segment ().as_string ();
950
940
951
- paths.emplace_back (rebind_path);
952
- }
941
+ imports.emplace_back (path, false , label);
953
942
}
954
943
955
944
static void
956
- flatten_list (const AST::UseTreeList &list, std::vector<AST::SimplePath > &paths )
945
+ flatten_list (const AST::UseTreeList &list, std::vector<Import > &imports )
957
946
{
958
947
auto prefix = AST::SimplePath::create_empty ();
959
948
if (list.has_path ())
960
949
prefix = list.get_path ();
961
950
962
951
for (const auto &tree : list.get_trees ())
963
952
{
964
- auto sub_paths = std::vector<AST::SimplePath> ();
965
- flatten (tree.get (), sub_paths);
953
+ // append imports to the main list, then modify them in-place
954
+ auto start_idx = imports.size ();
955
+ flatten (tree.get (), imports);
966
956
967
- for (auto &sub_path : sub_paths)
968
- {
969
- auto new_path = prefix;
970
- std::copy (sub_path.get_segments ().begin (),
971
- sub_path.get_segments ().end (),
972
- std::back_inserter (new_path.get_segments ()));
973
-
974
- paths.emplace_back (new_path);
975
- }
957
+ for (auto import = imports.begin () + start_idx; import != imports.end ();
958
+ import++)
959
+ import->add_prefix (prefix);
976
960
}
977
961
}
978
962
963
+ void
964
+ Import::add_prefix (AST::SimplePath prefix)
965
+ {
966
+ AST::SimplePath old_path (std::move (path));
967
+ path = std::move (prefix);
968
+ std::move (old_path.get_segments ().begin (), old_path.get_segments ().end (),
969
+ std::back_inserter (path.get_segments ()));
970
+ }
971
+
979
972
/* *
980
973
* Flatten a UseDeclaration's UseTree into multiple simple paths to resolve.
981
974
*
@@ -996,21 +989,21 @@ flatten_list (const AST::UseTreeList &list, std::vector<AST::SimplePath> &paths)
996
989
* Finally in the third case, we want to create two SimplePaths to resolve:
997
990
* [some::path::one, some::path::two]
998
991
*/
999
- static std::vector<AST::SimplePath >
1000
- flatten_use_dec_to_paths (const AST::UseDeclaration &use_item)
992
+ static std::vector<Import >
993
+ flatten_use_dec_to_imports (const AST::UseDeclaration &use_item)
1001
994
{
1002
- auto paths = std::vector<AST::SimplePath > ();
995
+ auto imports = std::vector<Import > ();
1003
996
1004
997
const auto &tree = use_item.get_tree ();
1005
- flatten (tree.get (), paths );
998
+ flatten (tree.get (), imports );
1006
999
1007
- return paths ;
1000
+ return imports ;
1008
1001
}
1009
1002
1010
1003
void
1011
1004
ResolveItem::visit (AST::UseDeclaration &use_item)
1012
1005
{
1013
- std::vector<AST::SimplePath > to_resolve = flatten_use_dec_to_paths (use_item);
1006
+ std::vector<Import > to_resolve = flatten_use_dec_to_imports (use_item);
1014
1007
1015
1008
// FIXME: I think this does not actually resolve glob use-decls and is going
1016
1009
// the wrong way about it. RFC #1560 specifies the following:
@@ -1022,18 +1015,20 @@ ResolveItem::visit (AST::UseDeclaration &use_item)
1022
1015
// Which is the opposite of what we're doing if I understand correctly?
1023
1016
1024
1017
NodeId current_module = resolver->peek_current_module_scope ();
1025
- for (auto &path : to_resolve)
1018
+ for (auto &import : to_resolve)
1026
1019
{
1020
+ auto &path = import.get_path ();
1021
+
1027
1022
rust_debug (" resolving use-decl path: [%s]" , path.as_string ().c_str ());
1028
1023
NodeId resolved_node_id = ResolvePath::go (&path);
1029
1024
bool ok = resolved_node_id != UNKNOWN_NODEID;
1030
1025
if (!ok)
1031
1026
continue ;
1032
1027
1033
- const AST::SimplePathSegment &final_seg = path.get_final_segment ();
1028
+ if (import.is_glob ())
1029
+ continue ;
1034
1030
1035
- auto decl
1036
- = CanonicalPath::new_seg (resolved_node_id, final_seg.as_string ());
1031
+ auto decl = CanonicalPath::new_seg (resolved_node_id, import.get_name ());
1037
1032
mappings->insert_module_child_item (current_module, decl);
1038
1033
1039
1034
resolver->get_type_scope ().insert (decl, resolved_node_id,
@@ -1157,13 +1152,13 @@ rust_flatten_nested_glob (void)
1157
1152
= Rust::AST::UseTreeGlob (Rust::AST::UseTreeGlob::PathType::PATH_PREFIXED,
1158
1153
foobar, Location ());
1159
1154
1160
- auto paths = std::vector<Rust::AST::SimplePath > ();
1161
- Rust::Resolver::flatten_glob (glob, paths );
1155
+ auto imports = std::vector<Rust::Resolver::Import > ();
1156
+ Rust::Resolver::flatten_glob (glob, imports );
1162
1157
1163
- ASSERT_TRUE (!paths .empty ());
1164
- ASSERT_EQ (paths .size (), 1 );
1165
- ASSERT_EQ (paths [0 ].get_segments ()[0 ].as_string (), " foo" );
1166
- ASSERT_EQ (paths [0 ].get_segments ()[1 ].as_string (), " bar" );
1158
+ ASSERT_TRUE (!imports .empty ());
1159
+ ASSERT_EQ (imports .size (), 1 );
1160
+ ASSERT_EQ (imports [0 ]. get_path () .get_segments ()[0 ].as_string (), " foo" );
1161
+ ASSERT_EQ (imports [0 ]. get_path () .get_segments ()[1 ].as_string (), " bar" );
1167
1162
}
1168
1163
1169
1164
static void
@@ -1175,12 +1170,12 @@ rust_flatten_glob (void)
1175
1170
= Rust::AST::UseTreeGlob (Rust::AST::UseTreeGlob::PathType::PATH_PREFIXED,
1176
1171
frob, Location ());
1177
1172
1178
- auto paths = std::vector<Rust::AST::SimplePath > ();
1179
- Rust::Resolver::flatten_glob (glob, paths );
1173
+ auto imports = std::vector<Rust::Resolver::Import > ();
1174
+ Rust::Resolver::flatten_glob (glob, imports );
1180
1175
1181
- ASSERT_TRUE (!paths .empty ());
1182
- ASSERT_EQ (paths .size (), 1 );
1183
- ASSERT_EQ (paths [0 ], " frobulator" );
1176
+ ASSERT_TRUE (!imports .empty ());
1177
+ ASSERT_EQ (imports .size (), 1 );
1178
+ ASSERT_EQ (imports [0 ]. get_path () , " frobulator" );
1184
1179
}
1185
1180
1186
1181
static void
@@ -1193,13 +1188,13 @@ rust_flatten_rebind_none (void)
1193
1188
auto rebind = Rust::AST::UseTreeRebind (Rust::AST::UseTreeRebind::NONE,
1194
1189
foobar, Location ());
1195
1190
1196
- auto paths = std::vector<Rust::AST::SimplePath > ();
1197
- Rust::Resolver::flatten_rebind (rebind, paths );
1191
+ auto imports = std::vector<Rust::Resolver::Import > ();
1192
+ Rust::Resolver::flatten_rebind (rebind, imports );
1198
1193
1199
- ASSERT_TRUE (!paths .empty ());
1200
- ASSERT_EQ (paths .size (), 1 );
1201
- ASSERT_EQ (paths [0 ].get_segments ()[0 ].as_string (), " foo" );
1202
- ASSERT_EQ (paths [0 ].get_segments ()[1 ].as_string (), " bar" );
1194
+ ASSERT_TRUE (!imports .empty ());
1195
+ ASSERT_EQ (imports .size (), 1 );
1196
+ ASSERT_EQ (imports [0 ]. get_path () .get_segments ()[0 ].as_string (), " foo" );
1197
+ ASSERT_EQ (imports [0 ]. get_path () .get_segments ()[1 ].as_string (), " bar" );
1203
1198
}
1204
1199
1205
1200
static void
@@ -1210,13 +1205,13 @@ rust_flatten_rebind (void)
1210
1205
auto rebind = Rust::AST::UseTreeRebind (Rust::AST::UseTreeRebind::IDENTIFIER,
1211
1206
frob, Location (), " saindoux" );
1212
1207
1213
- auto paths = std::vector<Rust::AST::SimplePath > ();
1214
- Rust::Resolver::flatten_rebind (rebind, paths );
1208
+ auto imports = std::vector<Rust::Resolver::Import > ();
1209
+ Rust::Resolver::flatten_rebind (rebind, imports );
1215
1210
1216
- ASSERT_TRUE (!paths .empty ());
1217
- ASSERT_EQ (paths .size (), 2 );
1218
- ASSERT_EQ (paths [0 ], " frobulator" );
1219
- ASSERT_EQ (paths[ 1 ] , " saindoux" );
1211
+ ASSERT_TRUE (!imports .empty ());
1212
+ ASSERT_EQ (imports .size (), 1 );
1213
+ ASSERT_EQ (imports [0 ]. get_path () , " frobulator" );
1214
+ ASSERT_EQ (imports[ 0 ]. get_name () , " saindoux" );
1220
1215
}
1221
1216
1222
1217
static void
@@ -1231,17 +1226,15 @@ rust_flatten_rebind_nested (void)
1231
1226
auto rebind = Rust::AST::UseTreeRebind (Rust::AST::UseTreeRebind::IDENTIFIER,
1232
1227
foo_bar_baz, Location (), " saindoux" );
1233
1228
1234
- auto paths = std::vector<Rust::AST::SimplePath> ();
1235
- Rust::Resolver::flatten_rebind (rebind, paths);
1236
-
1237
- ASSERT_TRUE (!paths.empty ());
1238
- ASSERT_EQ (paths.size (), 2 );
1239
- ASSERT_EQ (paths[0 ].get_segments ()[0 ].as_string (), " foo" );
1240
- ASSERT_EQ (paths[0 ].get_segments ()[1 ].as_string (), " bar" );
1241
- ASSERT_EQ (paths[0 ].get_segments ()[2 ].as_string (), " baz" );
1242
- ASSERT_EQ (paths[1 ].get_segments ()[0 ].as_string (), " foo" );
1243
- ASSERT_EQ (paths[1 ].get_segments ()[1 ].as_string (), " bar" );
1244
- ASSERT_EQ (paths[1 ].get_segments ()[2 ].as_string (), " saindoux" );
1229
+ auto imports = std::vector<Rust::Resolver::Import> ();
1230
+ Rust::Resolver::flatten_rebind (rebind, imports);
1231
+
1232
+ ASSERT_TRUE (!imports.empty ());
1233
+ ASSERT_EQ (imports.size (), 1 );
1234
+ ASSERT_EQ (imports[0 ].get_path ().get_segments ()[0 ].as_string (), " foo" );
1235
+ ASSERT_EQ (imports[0 ].get_path ().get_segments ()[1 ].as_string (), " bar" );
1236
+ ASSERT_EQ (imports[0 ].get_path ().get_segments ()[2 ].as_string (), " baz" );
1237
+ ASSERT_EQ (imports[0 ].get_name (), " saindoux" );
1245
1238
}
1246
1239
1247
1240
static void
@@ -1270,17 +1263,17 @@ rust_flatten_list (void)
1270
1263
auto list = Rust::AST::UseTreeList (Rust::AST::UseTreeList::PATH_PREFIXED,
1271
1264
foo_bar, std::move (uses), Location ());
1272
1265
1273
- auto paths = std::vector<Rust::AST::SimplePath > ();
1274
- Rust::Resolver::flatten_list (list, paths );
1275
-
1276
- ASSERT_TRUE (!paths .empty ());
1277
- ASSERT_EQ (paths .size (), 2 );
1278
- ASSERT_EQ (paths [0 ].get_segments ()[0 ].as_string (), " foo" );
1279
- ASSERT_EQ (paths [0 ].get_segments ()[1 ].as_string (), " bar" );
1280
- ASSERT_EQ (paths [0 ].get_segments ()[2 ].as_string (), " baz" );
1281
- ASSERT_EQ (paths [1 ].get_segments ()[0 ].as_string (), " foo" );
1282
- ASSERT_EQ (paths [1 ].get_segments ()[1 ].as_string (), " bar" );
1283
- ASSERT_EQ (paths [1 ].get_segments ()[2 ].as_string (), " bul" );
1266
+ auto imports = std::vector<Rust::Resolver::Import > ();
1267
+ Rust::Resolver::flatten_list (list, imports );
1268
+
1269
+ ASSERT_TRUE (!imports .empty ());
1270
+ ASSERT_EQ (imports .size (), 2 );
1271
+ ASSERT_EQ (imports [0 ]. get_path () .get_segments ()[0 ].as_string (), " foo" );
1272
+ ASSERT_EQ (imports [0 ]. get_path () .get_segments ()[1 ].as_string (), " bar" );
1273
+ ASSERT_EQ (imports [0 ]. get_path () .get_segments ()[2 ].as_string (), " baz" );
1274
+ ASSERT_EQ (imports [1 ]. get_path () .get_segments ()[0 ].as_string (), " foo" );
1275
+ ASSERT_EQ (imports [1 ]. get_path () .get_segments ()[1 ].as_string (), " bar" );
1276
+ ASSERT_EQ (imports [1 ]. get_path () .get_segments ()[2 ].as_string (), " bul" );
1284
1277
}
1285
1278
1286
1279
static void
0 commit comments