| 1 |
// Copyright (C) 2020-2025 Free Software Foundation, Inc. |
| 2 |
|
| 3 |
// This file is part of GCC. |
| 4 |
|
| 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 |
| 8 |
// version. |
| 9 |
|
| 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 |
| 13 |
// for more details. |
| 14 |
|
| 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/>. |
| 18 |
|
| 19 |
#include "rust-ast-resolve-item.h" |
| 20 |
#include "rust-ast-full-decls.h" |
| 21 |
#include "rust-ast-resolve-toplevel.h" |
| 22 |
#include "rust-ast-resolve-type.h" |
| 23 |
#include "rust-ast-resolve-pattern.h" |
| 24 |
#include "rust-ast-resolve-path.h" |
| 25 |
|
| 26 |
#include "rust-item.h" |
| 27 |
#include "selftest.h" |
| 28 |
|
| 29 |
namespace Rust { |
| 30 |
namespace Resolver { |
| 31 |
|
| 32 |
ResolveTraitItems::ResolveTraitItems (const CanonicalPath &prefix, |
| 33 |
const CanonicalPath &canonical_prefix) |
| 34 |
: ResolverBase (), prefix (prefix), canonical_prefix (canonical_prefix) |
| 35 |
{} |
| 36 |
|
| 37 |
void |
| 38 |
ResolveTraitItems::go (AST::AssociatedItem *item, const CanonicalPath &prefix, |
| 39 |
const CanonicalPath &canonical_prefix) |
| 40 |
{ |
| 41 |
if (item->is_marked_for_strip ()) |
| 42 |
return; |
| 43 |
|
| 44 |
ResolveTraitItems resolver (prefix, canonical_prefix); |
| 45 |
item->accept_vis (resolver); |
| 46 |
} |
| 47 |
|
| 48 |
void |
| 49 |
ResolveTraitItems::visit (AST::Function &function) |
| 50 |
{ |
| 51 |
auto decl |
| 52 |
= CanonicalPath::new_seg (function.get_node_id (), |
| 53 |
function.get_function_name ().as_string ()); |
| 54 |
auto path = prefix.append (decl); |
| 55 |
auto cpath = canonical_prefix.append (decl); |
| 56 |
mappings.insert_canonical_path (function.get_node_id (), cpath); |
| 57 |
|
| 58 |
NodeId scope_node_id = function.get_node_id (); |
| 59 |
resolver->get_name_scope ().push (scope_node_id); |
| 60 |
resolver->get_type_scope ().push (scope_node_id); |
| 61 |
resolver->get_label_scope ().push (scope_node_id); |
| 62 |
resolver->push_new_name_rib (resolver->get_name_scope ().peek ()); |
| 63 |
resolver->push_new_type_rib (resolver->get_type_scope ().peek ()); |
| 64 |
resolver->push_new_label_rib (resolver->get_label_scope ().peek ()); |
| 65 |
|
| 66 |
if (function.has_generics ()) |
| 67 |
ResolveGenericParams::go (function.get_generic_params (), prefix, |
| 68 |
canonical_prefix); |
| 69 |
|
| 70 |
if (function.has_return_type ()) |
| 71 |
ResolveType::go (function.get_return_type ()); |
| 72 |
|
| 73 |
// self turns into (self: Self) as a function param |
| 74 |
std::vector<PatternBinding> bindings |
| 75 |
= {PatternBinding (PatternBoundCtx::Product, std::set<Identifier> ())}; |
| 76 |
|
| 77 |
// we make a new scope so the names of parameters are resolved and shadowed |
| 78 |
// correctly |
| 79 |
for (auto &p : function.get_function_params ()) |
| 80 |
{ |
| 81 |
if (p->is_variadic ()) |
| 82 |
{ |
| 83 |
auto param = static_cast<AST::VariadicParam &> (*p); |
| 84 |
PatternDeclaration::go (param.get_pattern (), Rib::ItemType::Param, |
| 85 |
bindings); |
| 86 |
} |
| 87 |
else if (p->is_self ()) |
| 88 |
{ |
| 89 |
auto ¶m = static_cast<AST::SelfParam &> (*p); |
| 90 |
// FIXME: which location should be used for Rust::Identifier `self`? |
| 91 |
AST::IdentifierPattern self_pattern ( |
| 92 |
param.get_node_id (), {"self"}, param.get_locus (), |
| 93 |
param.get_has_ref (), param.get_is_mut (), |
| 94 |
std::unique_ptr<AST::Pattern> (nullptr)); |
| 95 |
|
| 96 |
PatternDeclaration::go (self_pattern, Rib::ItemType::Param); |
| 97 |
|
| 98 |
if (param.has_type ()) |
| 99 |
{ |
| 100 |
// This shouldn't happen the parser should already error for this |
| 101 |
rust_assert (!param.get_has_ref ()); |
| 102 |
ResolveType::go (param.get_type ()); |
| 103 |
} |
| 104 |
else |
| 105 |
{ |
| 106 |
// here we implicitly make self have a type path of Self |
| 107 |
std::vector<std::unique_ptr<AST::TypePathSegment>> segments; |
| 108 |
segments.push_back (std::unique_ptr<AST::TypePathSegment> ( |
| 109 |
new AST::TypePathSegment ("Self", false, param.get_locus ()))); |
| 110 |
|
| 111 |
AST::TypePath self_type_path (std::move (segments), |
| 112 |
param.get_locus ()); |
| 113 |
ResolveType::go (self_type_path); |
| 114 |
} |
| 115 |
} |
| 116 |
else |
| 117 |
{ |
| 118 |
auto ¶m = static_cast<AST::FunctionParam &> (*p); |
| 119 |
ResolveType::go (param.get_type ()); |
| 120 |
PatternDeclaration::go (param.get_pattern (), Rib::ItemType::Param, |
| 121 |
bindings); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
if (function.has_where_clause ()) |
| 126 |
ResolveWhereClause::Resolve (function.get_where_clause ()); |
| 127 |
|
| 128 |
// trait items have an optional body |
| 129 |
if (function.has_body ()) |
| 130 |
ResolveExpr::go (*function.get_definition ().value (), path, cpath); |
| 131 |
|
| 132 |
resolver->get_name_scope ().pop (); |
| 133 |
resolver->get_type_scope ().pop (); |
| 134 |
resolver->get_label_scope ().pop (); |
| 135 |
} |
| 136 |
void |
| 137 |
ResolveTraitItems::visit (AST::TraitItemType &type) |
| 138 |
{ |
| 139 |
auto decl = CanonicalPath::new_seg (type.get_node_id (), |
| 140 |
type.get_identifier ().as_string ()); |
| 141 |
auto path = prefix.append (decl); |
| 142 |
auto cpath = canonical_prefix.append (decl); |
| 143 |
mappings.insert_canonical_path (type.get_node_id (), cpath); |
| 144 |
|
| 145 |
for (auto &bound : type.get_type_param_bounds ()) |
| 146 |
ResolveTypeBound::go (*bound); |
| 147 |
} |
| 148 |
|
| 149 |
void |
| 150 |
ResolveTraitItems::visit (AST::TraitItemConst &constant) |
| 151 |
{ |
| 152 |
auto decl = CanonicalPath::new_seg (constant.get_node_id (), |
| 153 |
constant.get_identifier ().as_string ()); |
| 154 |
auto path = prefix.append (decl); |
| 155 |
auto cpath = canonical_prefix.append (decl); |
| 156 |
mappings.insert_canonical_path (constant.get_node_id (), cpath); |
| 157 |
|
| 158 |
ResolveType::go (constant.get_type ()); |
| 159 |
|
| 160 |
if (constant.has_expr ()) |
| 161 |
ResolveExpr::go (constant.get_expr (), path, cpath); |
| 162 |
} |
| 163 |
|
| 164 |
ResolveItem::ResolveItem (const CanonicalPath &prefix, |
| 165 |
const CanonicalPath &canonical_prefix) |
| 166 |
: ResolverBase (), prefix (prefix), canonical_prefix (canonical_prefix) |
| 167 |
{} |
| 168 |
|
| 169 |
void |
| 170 |
ResolveItem::go (AST::Item &item, const CanonicalPath &prefix, |
| 171 |
const CanonicalPath &canonical_prefix) |
| 172 |
{ |
| 173 |
ResolveItem resolver (prefix, canonical_prefix); |
| 174 |
item.accept_vis (resolver); |
| 175 |
} |
| 176 |
|
| 177 |
void |
| 178 |
ResolveItem::visit (AST::TypeAlias &alias) |
| 179 |
{ |
| 180 |
auto talias |
| 181 |
= CanonicalPath::new_seg (alias.get_node_id (), |
| 182 |
alias.get_new_type_name ().as_string ()); |
| 183 |
auto path = prefix.append (talias); |
| 184 |
auto cpath = canonical_prefix.append (talias); |
| 185 |
mappings.insert_canonical_path (alias.get_node_id (), cpath); |
| 186 |
|
| 187 |
NodeId scope_node_id = alias.get_node_id (); |
| 188 |
resolver->get_type_scope ().push (scope_node_id); |
| 189 |
|
| 190 |
if (alias.has_generics ()) |
| 191 |
ResolveGenericParams::go (alias.get_generic_params (), prefix, |
| 192 |
canonical_prefix); |
| 193 |
|
| 194 |
if (alias.has_where_clause ()) |
| 195 |
ResolveWhereClause::Resolve (alias.get_where_clause ()); |
| 196 |
|
| 197 |
ResolveType::go (alias.get_type_aliased ()); |
| 198 |
|
| 199 |
resolver->get_type_scope ().pop (); |
| 200 |
} |
| 201 |
|
| 202 |
void |
| 203 |
ResolveItem::visit (AST::Module &module) |
| 204 |
{ |
| 205 |
auto mod = CanonicalPath::new_seg (module.get_node_id (), |
| 206 |
module.get_name ().as_string ()); |
| 207 |
auto path = prefix.append (mod); |
| 208 |
auto cpath = canonical_prefix.append (mod); |
| 209 |
mappings.insert_canonical_path (module.get_node_id (), cpath); |
| 210 |
|
| 211 |
resolve_visibility (module.get_visibility ()); |
| 212 |
|
| 213 |
NodeId scope_node_id = module.get_node_id (); |
| 214 |
resolver->get_name_scope ().push (scope_node_id); |
| 215 |
resolver->get_type_scope ().push (scope_node_id); |
| 216 |
resolver->get_label_scope ().push (scope_node_id); |
| 217 |
resolver->push_new_name_rib (resolver->get_name_scope ().peek ()); |
| 218 |
resolver->push_new_type_rib (resolver->get_type_scope ().peek ()); |
| 219 |
resolver->push_new_label_rib (resolver->get_label_scope ().peek ()); |
| 220 |
|
| 221 |
// FIXME: Should we reinsert a child here? Any reason we ResolveTopLevel::go |
| 222 |
// in ResolveTopLevel::visit (AST::Module) as well as here? |
| 223 |
for (auto &item : module.get_items ()) |
| 224 |
ResolveTopLevel::go (*item, CanonicalPath::create_empty (), cpath); |
| 225 |
|
| 226 |
resolver->push_new_module_scope (module.get_node_id ()); |
| 227 |
for (auto &item : module.get_items ()) |
| 228 |
ResolveItem::go (*item, path, cpath); |
| 229 |
|
| 230 |
resolver->pop_module_scope (); |
| 231 |
|
| 232 |
resolver->get_name_scope ().pop (); |
| 233 |
resolver->get_type_scope ().pop (); |
| 234 |
resolver->get_label_scope ().pop (); |
| 235 |
} |
| 236 |
|
| 237 |
void |
| 238 |
ResolveItem::visit (AST::TupleStruct &struct_decl) |
| 239 |
{ |
| 240 |
auto decl |
| 241 |
= CanonicalPath::new_seg (struct_decl.get_node_id (), |
| 242 |
struct_decl.get_identifier ().as_string ()); |
| 243 |
auto path = prefix.append (decl); |
| 244 |
auto cpath = canonical_prefix.append (decl); |
| 245 |
mappings.insert_canonical_path (struct_decl.get_node_id (), cpath); |
| 246 |
|
| 247 |
resolve_visibility (struct_decl.get_visibility ()); |
| 248 |
|
| 249 |
NodeId scope_node_id = struct_decl.get_node_id (); |
| 250 |
resolver->get_type_scope ().push (scope_node_id); |
| 251 |
|
| 252 |
if (struct_decl.has_generics ()) |
| 253 |
ResolveGenericParams::go (struct_decl.get_generic_params (), prefix, |
| 254 |
canonical_prefix); |
| 255 |
|
| 256 |
if (struct_decl.has_where_clause ()) |
| 257 |
ResolveWhereClause::Resolve (struct_decl.get_where_clause ()); |
| 258 |
|
| 259 |
for (AST::TupleField &field : struct_decl.get_fields ()) |
| 260 |
{ |
| 261 |
if (field.get_field_type ().is_marked_for_strip ()) |
| 262 |
continue; |
| 263 |
|
| 264 |
resolve_visibility (field.get_visibility ()); |
| 265 |
|
| 266 |
ResolveType::go (field.get_field_type ()); |
| 267 |
} |
| 268 |
|
| 269 |
resolver->get_type_scope ().pop (); |
| 270 |
} |
| 271 |
|
| 272 |
void |
| 273 |
ResolveItem::visit (AST::Enum &enum_decl) |
| 274 |
{ |
| 275 |
auto decl = CanonicalPath::new_seg (enum_decl.get_node_id (), |
| 276 |
enum_decl.get_identifier ().as_string ()); |
| 277 |
auto path = prefix.append (decl); |
| 278 |
auto cpath = canonical_prefix.append (decl); |
| 279 |
mappings.insert_canonical_path (enum_decl.get_node_id (), cpath); |
| 280 |
|
| 281 |
resolve_visibility (enum_decl.get_visibility ()); |
| 282 |
|
| 283 |
NodeId scope_node_id = enum_decl.get_node_id (); |
| 284 |
resolver->get_type_scope ().push (scope_node_id); |
| 285 |
|
| 286 |
if (enum_decl.has_generics ()) |
| 287 |
ResolveGenericParams::go (enum_decl.get_generic_params (), prefix, |
| 288 |
canonical_prefix); |
| 289 |
|
| 290 |
if (enum_decl.has_where_clause ()) |
| 291 |
ResolveWhereClause::Resolve (enum_decl.get_where_clause ()); |
| 292 |
|
| 293 |
/* The actual fields are inside the variants. */ |
| 294 |
for (auto &variant : enum_decl.get_variants ()) |
| 295 |
ResolveItem::go (*variant, path, cpath); |
| 296 |
|
| 297 |
resolver->get_type_scope ().pop (); |
| 298 |
} |
| 299 |
|
| 300 |
/* EnumItem doesn't need to be handled, no fields. */ |
| 301 |
void |
| 302 |
ResolveItem::visit (AST::EnumItem &item) |
| 303 |
{ |
| 304 |
// Since at this point we cannot have visibilities on enum items anymore, we |
| 305 |
// can skip handling them |
| 306 |
|
| 307 |
auto decl = CanonicalPath::new_seg (item.get_node_id (), |
| 308 |
item.get_identifier ().as_string ()); |
| 309 |
auto path = prefix.append (decl); |
| 310 |
auto cpath = canonical_prefix.append (decl); |
| 311 |
mappings.insert_canonical_path (item.get_node_id (), cpath); |
| 312 |
} |
| 313 |
|
| 314 |
void |
| 315 |
ResolveItem::visit (AST::EnumItemTuple &item) |
| 316 |
{ |
| 317 |
auto decl = CanonicalPath::new_seg (item.get_node_id (), |
| 318 |
item.get_identifier ().as_string ()); |
| 319 |
auto path = prefix.append (decl); |
| 320 |
auto cpath = canonical_prefix.append (decl); |
| 321 |
mappings.insert_canonical_path (item.get_node_id (), cpath); |
| 322 |
|
| 323 |
for (auto &field : item.get_tuple_fields ()) |
| 324 |
{ |
| 325 |
if (field.get_field_type ().is_marked_for_strip ()) |
| 326 |
continue; |
| 327 |
|
| 328 |
ResolveType::go (field.get_field_type ()); |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
void |
| 333 |
ResolveItem::visit (AST::EnumItemStruct &item) |
| 334 |
{ |
| 335 |
auto decl = CanonicalPath::new_seg (item.get_node_id (), |
| 336 |
item.get_identifier ().as_string ()); |
| 337 |
auto path = prefix.append (decl); |
| 338 |
auto cpath = canonical_prefix.append (decl); |
| 339 |
mappings.insert_canonical_path (item.get_node_id (), cpath); |
| 340 |
|
| 341 |
for (auto &field : item.get_struct_fields ()) |
| 342 |
{ |
| 343 |
if (field.get_field_type ().is_marked_for_strip ()) |
| 344 |
continue; |
| 345 |
|
| 346 |
ResolveType::go (field.get_field_type ()); |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
void |
| 351 |
ResolveItem::visit (AST::EnumItemDiscriminant &item) |
| 352 |
{ |
| 353 |
auto decl = CanonicalPath::new_seg (item.get_node_id (), |
| 354 |
item.get_identifier ().as_string ()); |
| 355 |
auto path = prefix.append (decl); |
| 356 |
auto cpath = canonical_prefix.append (decl); |
| 357 |
|
| 358 |
mappings.insert_canonical_path (item.get_node_id (), cpath); |
| 359 |
|
| 360 |
ResolveExpr::go (item.get_expr (), path, cpath); |
| 361 |
} |
| 362 |
|
| 363 |
void |
| 364 |
ResolveItem::visit (AST::StructStruct &struct_decl) |
| 365 |
{ |
| 366 |
auto decl |
| 367 |
= CanonicalPath::new_seg (struct_decl.get_node_id (), |
| 368 |
struct_decl.get_identifier ().as_string ()); |
| 369 |
auto path = prefix.append (decl); |
| 370 |
auto cpath = canonical_prefix.append (decl); |
| 371 |
mappings.insert_canonical_path (struct_decl.get_node_id (), cpath); |
| 372 |
|
| 373 |
resolve_visibility (struct_decl.get_visibility ()); |
| 374 |
|
| 375 |
NodeId scope_node_id = struct_decl.get_node_id (); |
| 376 |
resolver->get_type_scope ().push (scope_node_id); |
| 377 |
|
| 378 |
if (struct_decl.has_generics ()) |
| 379 |
ResolveGenericParams::go (struct_decl.get_generic_params (), prefix, |
| 380 |
canonical_prefix); |
| 381 |
|
| 382 |
if (struct_decl.has_where_clause ()) |
| 383 |
ResolveWhereClause::Resolve (struct_decl.get_where_clause ()); |
| 384 |
|
| 385 |
for (AST::StructField &field : struct_decl.get_fields ()) |
| 386 |
{ |
| 387 |
if (field.get_field_type ().is_marked_for_strip ()) |
| 388 |
continue; |
| 389 |
|
| 390 |
resolve_visibility (field.get_visibility ()); |
| 391 |
|
| 392 |
ResolveType::go (field.get_field_type ()); |
| 393 |
} |
| 394 |
|
| 395 |
resolver->get_type_scope ().pop (); |
| 396 |
} |
| 397 |
|
| 398 |
void |
| 399 |
ResolveItem::visit (AST::Union &union_decl) |
| 400 |
{ |
| 401 |
auto decl |
| 402 |
= CanonicalPath::new_seg (union_decl.get_node_id (), |
| 403 |
union_decl.get_identifier ().as_string ()); |
| 404 |
auto path = prefix.append (decl); |
| 405 |
auto cpath = canonical_prefix.append (decl); |
| 406 |
mappings.insert_canonical_path (union_decl.get_node_id (), cpath); |
| 407 |
|
| 408 |
resolve_visibility (union_decl.get_visibility ()); |
| 409 |
|
| 410 |
NodeId scope_node_id = union_decl.get_node_id (); |
| 411 |
resolver->get_type_scope ().push (scope_node_id); |
| 412 |
|
| 413 |
if (union_decl.has_generics ()) |
| 414 |
ResolveGenericParams::go (union_decl.get_generic_params (), prefix, |
| 415 |
canonical_prefix); |
| 416 |
|
| 417 |
if (union_decl.has_where_clause ()) |
| 418 |
ResolveWhereClause::Resolve (union_decl.get_where_clause ()); |
| 419 |
|
| 420 |
for (AST::StructField &field : union_decl.get_variants ()) |
| 421 |
{ |
| 422 |
if (field.get_field_type ().is_marked_for_strip ()) |
| 423 |
continue; |
| 424 |
|
| 425 |
ResolveType::go (field.get_field_type ()); |
| 426 |
} |
| 427 |
|
| 428 |
resolver->get_type_scope ().pop (); |
| 429 |
} |
| 430 |
|
| 431 |
void |
| 432 |
ResolveItem::visit (AST::StaticItem &var) |
| 433 |
{ |
| 434 |
auto decl = CanonicalPath::new_seg (var.get_node_id (), |
| 435 |
var.get_identifier ().as_string ()); |
| 436 |
auto path = prefix.append (decl); |
| 437 |
auto cpath = canonical_prefix.append (decl); |
| 438 |
mappings.insert_canonical_path (var.get_node_id (), cpath); |
| 439 |
|
| 440 |
ResolveType::go (var.get_type ()); |
| 441 |
ResolveExpr::go (var.get_expr (), path, cpath); |
| 442 |
} |
| 443 |
|
| 444 |
void |
| 445 |
ResolveItem::visit (AST::ConstantItem &constant) |
| 446 |
{ |
| 447 |
auto decl = CanonicalPath::new_seg (constant.get_node_id (), |
| 448 |
constant.get_identifier ()); |
| 449 |
auto path = prefix.append (decl); |
| 450 |
auto cpath = canonical_prefix.append (decl); |
| 451 |
mappings.insert_canonical_path (constant.get_node_id (), cpath); |
| 452 |
|
| 453 |
resolve_visibility (constant.get_visibility ()); |
| 454 |
|
| 455 |
ResolveType::go (constant.get_type ()); |
| 456 |
ResolveExpr::go (constant.get_expr (), path, cpath); |
| 457 |
} |
| 458 |
|
| 459 |
void |
| 460 |
ResolveItem::visit (AST::Function &function) |
| 461 |
{ |
| 462 |
auto decl |
| 463 |
= CanonicalPath::new_seg (function.get_node_id (), |
| 464 |
function.get_function_name ().as_string ()); |
| 465 |
auto path = prefix.append (decl); |
| 466 |
auto cpath = canonical_prefix.append (decl); |
| 467 |
|
| 468 |
mappings.insert_canonical_path (function.get_node_id (), cpath); |
| 469 |
|
| 470 |
resolve_visibility (function.get_visibility ()); |
| 471 |
|
| 472 |
NodeId scope_node_id = function.get_node_id (); |
| 473 |
resolver->get_name_scope ().push (scope_node_id); |
| 474 |
resolver->get_type_scope ().push (scope_node_id); |
| 475 |
resolver->get_label_scope ().push (scope_node_id); |
| 476 |
resolver->push_new_name_rib (resolver->get_name_scope ().peek ()); |
| 477 |
resolver->push_new_type_rib (resolver->get_type_scope ().peek ()); |
| 478 |
resolver->push_new_label_rib (resolver->get_label_scope ().peek ()); |
| 479 |
|
| 480 |
if (function.has_generics ()) |
| 481 |
ResolveGenericParams::go (function.get_generic_params (), prefix, |
| 482 |
canonical_prefix); |
| 483 |
|
| 484 |
// resolve any where clause items |
| 485 |
if (function.has_where_clause ()) |
| 486 |
ResolveWhereClause::Resolve (function.get_where_clause ()); |
| 487 |
|
| 488 |
if (function.has_return_type ()) |
| 489 |
ResolveType::go (function.get_return_type ()); |
| 490 |
|
| 491 |
if (function.has_self_param ()) |
| 492 |
{ |
| 493 |
// self turns into (self: Self) as a function param |
| 494 |
AST::Param &s_param = function.get_self_param (); |
| 495 |
auto &self_param = static_cast<AST::SelfParam &> (s_param); |
| 496 |
|
| 497 |
// FIXME: which location should be used for Rust::Identifier `self`? |
| 498 |
AST::IdentifierPattern self_pattern ( |
| 499 |
self_param.get_node_id (), {"self"}, self_param.get_locus (), |
| 500 |
self_param.get_has_ref (), self_param.get_is_mut (), |
| 501 |
std::unique_ptr<AST::Pattern> (nullptr)); |
| 502 |
PatternDeclaration::go (self_pattern, Rib::ItemType::Param); |
| 503 |
|
| 504 |
if (self_param.has_type ()) |
| 505 |
{ |
| 506 |
// This shouldn't happen the parser should already error for this |
| 507 |
rust_assert (!self_param.get_has_ref ()); |
| 508 |
ResolveType::go (self_param.get_type ()); |
| 509 |
} |
| 510 |
else |
| 511 |
{ |
| 512 |
// here we implicitly make self have a type path of Self |
| 513 |
std::vector<std::unique_ptr<AST::TypePathSegment>> segments; |
| 514 |
segments.push_back (std::unique_ptr<AST::TypePathSegment> ( |
| 515 |
new AST::TypePathSegment ("Self", false, self_param.get_locus ()))); |
| 516 |
|
| 517 |
AST::TypePath self_type_path (std::move (segments), |
| 518 |
self_param.get_locus ()); |
| 519 |
ResolveType::go (self_type_path); |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
std::vector<PatternBinding> bindings |
| 524 |
= {PatternBinding (PatternBoundCtx::Product, std::set<Identifier> ())}; |
| 525 |
|
| 526 |
// we make a new scope so the names of parameters are resolved and shadowed |
| 527 |
// correctly |
| 528 |
for (auto &p : function.get_function_params ()) |
| 529 |
{ |
| 530 |
if (p->is_variadic ()) |
| 531 |
{ |
| 532 |
auto ¶m = static_cast<AST::VariadicParam &> (*p); |
| 533 |
if (param.has_pattern ()) |
| 534 |
PatternDeclaration::go (param.get_pattern (), Rib::ItemType::Param, |
| 535 |
bindings); |
| 536 |
} |
| 537 |
else if (p->is_self ()) |
| 538 |
{ |
| 539 |
auto ¶m = static_cast<AST::SelfParam &> (*p); |
| 540 |
if (param.has_type ()) |
| 541 |
ResolveType::go (param.get_type ()); |
| 542 |
} |
| 543 |
else |
| 544 |
{ |
| 545 |
auto ¶m = static_cast<AST::FunctionParam &> (*p); |
| 546 |
ResolveType::go (param.get_type ()); |
| 547 |
PatternDeclaration::go (param.get_pattern (), Rib::ItemType::Param, |
| 548 |
bindings); |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
// resolve the function body |
| 553 |
ResolveExpr::go (*function.get_definition ().value (), path, cpath); |
| 554 |
|
| 555 |
resolver->get_name_scope ().pop (); |
| 556 |
resolver->get_type_scope ().pop (); |
| 557 |
resolver->get_label_scope ().pop (); |
| 558 |
} |
| 559 |
|
| 560 |
void |
| 561 |
ResolveItem::visit (AST::InherentImpl &impl_block) |
| 562 |
{ |
| 563 |
NodeId scope_node_id = impl_block.get_node_id (); |
| 564 |
resolver->get_name_scope ().push (scope_node_id); |
| 565 |
resolver->get_type_scope ().push (scope_node_id); |
| 566 |
resolver->push_new_name_rib (resolver->get_name_scope ().peek ()); |
| 567 |
resolver->push_new_type_rib (resolver->get_type_scope ().peek ()); |
| 568 |
|
| 569 |
resolve_visibility (impl_block.get_visibility ()); |
| 570 |
|
| 571 |
if (impl_block.has_generics ()) |
| 572 |
ResolveGenericParams::go (impl_block.get_generic_params (), prefix, |
| 573 |
canonical_prefix); |
| 574 |
|
| 575 |
// resolve any where clause items |
| 576 |
if (impl_block.has_where_clause ()) |
| 577 |
ResolveWhereClause::Resolve (impl_block.get_where_clause ()); |
| 578 |
|
| 579 |
// FIXME this needs to be protected behind nominal type-checks see: |
| 580 |
// rustc --explain E0118 |
| 581 |
// issue #2634 |
| 582 |
ResolveType::go (impl_block.get_type ()); |
| 583 |
|
| 584 |
// Setup paths |
| 585 |
CanonicalPath self_cpath = CanonicalPath::create_empty (); |
| 586 |
bool ok = ResolveTypeToCanonicalPath::go (impl_block.get_type (), self_cpath); |
| 587 |
if (!ok) |
| 588 |
{ |
| 589 |
resolver->get_name_scope ().pop (); |
| 590 |
resolver->get_type_scope ().pop (); |
| 591 |
resolver->get_label_scope ().pop (); |
| 592 |
return; |
| 593 |
} |
| 594 |
|
| 595 |
rust_debug ("AST::InherentImpl resolve Self: {%s}", |
| 596 |
self_cpath.get ().c_str ()); |
| 597 |
|
| 598 |
CanonicalPath impl_type = self_cpath; |
| 599 |
CanonicalPath impl_type_seg |
| 600 |
= CanonicalPath::inherent_impl_seg (impl_block.get_node_id (), impl_type); |
| 601 |
CanonicalPath impl_prefix = prefix.append (impl_type_seg); |
| 602 |
|
| 603 |
// see https://godbolt.org/z/a3vMbsT6W |
| 604 |
CanonicalPath cpath = CanonicalPath::create_empty (); |
| 605 |
if (canonical_prefix.size () <= 1) |
| 606 |
{ |
| 607 |
cpath = impl_prefix; |
| 608 |
} |
| 609 |
else |
| 610 |
{ |
| 611 |
std::string seg_buf = "<impl " + self_cpath.get () + ">"; |
| 612 |
CanonicalPath seg |
| 613 |
= CanonicalPath::new_seg (impl_block.get_node_id (), seg_buf); |
| 614 |
cpath = canonical_prefix.append (seg); |
| 615 |
} |
| 616 |
|
| 617 |
// done setup paths |
| 618 |
|
| 619 |
auto Self |
| 620 |
= CanonicalPath::get_big_self (impl_block.get_type ().get_node_id ()); |
| 621 |
|
| 622 |
resolver->get_type_scope ().insert (Self, |
| 623 |
impl_block.get_type ().get_node_id (), |
| 624 |
impl_block.get_type ().get_locus ()); |
| 625 |
|
| 626 |
for (auto &impl_item : impl_block.get_impl_items ()) |
| 627 |
{ |
| 628 |
rust_debug ( |
| 629 |
"AST::InherentImpl resolve_impl_item: impl_prefix={%s} cpath={%s}", |
| 630 |
impl_prefix.get ().c_str (), cpath.get ().c_str ()); |
| 631 |
resolve_impl_item (*impl_item, impl_prefix, cpath); |
| 632 |
} |
| 633 |
|
| 634 |
resolver->get_type_scope ().peek ()->clear_name ( |
| 635 |
Self, impl_block.get_type ().get_node_id ()); |
| 636 |
|
| 637 |
resolver->get_type_scope ().pop (); |
| 638 |
resolver->get_name_scope ().pop (); |
| 639 |
} |
| 640 |
|
| 641 |
void |
| 642 |
ResolveItem::visit (AST::TraitImpl &impl_block) |
| 643 |
{ |
| 644 |
NodeId scope_node_id = impl_block.get_node_id (); |
| 645 |
|
| 646 |
resolve_visibility (impl_block.get_visibility ()); |
| 647 |
|
| 648 |
resolver->get_name_scope ().push (scope_node_id); |
| 649 |
resolver->get_type_scope ().push (scope_node_id); |
| 650 |
resolver->get_label_scope ().push (scope_node_id); |
| 651 |
resolver->push_new_name_rib (resolver->get_name_scope ().peek ()); |
| 652 |
resolver->push_new_type_rib (resolver->get_type_scope ().peek ()); |
| 653 |
resolver->push_new_label_rib (resolver->get_label_scope ().peek ()); |
| 654 |
|
| 655 |
if (impl_block.has_generics ()) |
| 656 |
ResolveGenericParams::go (impl_block.get_generic_params (), prefix, |
| 657 |
canonical_prefix); |
| 658 |
|
| 659 |
// resolve any where clause items |
| 660 |
if (impl_block.has_where_clause ()) |
| 661 |
ResolveWhereClause::Resolve (impl_block.get_where_clause ()); |
| 662 |
|
| 663 |
// CanonicalPath canonical_trait_type = CanonicalPath::create_empty (); |
| 664 |
NodeId trait_resolved_node = ResolveType::go (impl_block.get_trait_path ()); |
| 665 |
if (trait_resolved_node == UNKNOWN_NODEID) |
| 666 |
{ |
| 667 |
resolver->get_name_scope ().pop (); |
| 668 |
resolver->get_type_scope ().pop (); |
| 669 |
resolver->get_label_scope ().pop (); |
| 670 |
return; |
| 671 |
} |
| 672 |
|
| 673 |
// CanonicalPath canonical_impl_type = CanonicalPath::create_empty (); |
| 674 |
NodeId type_resolved_node = ResolveType::go (impl_block.get_type ()); |
| 675 |
if (type_resolved_node == UNKNOWN_NODEID) |
| 676 |
{ |
| 677 |
resolver->get_name_scope ().pop (); |
| 678 |
resolver->get_type_scope ().pop (); |
| 679 |
resolver->get_label_scope ().pop (); |
| 680 |
return; |
| 681 |
} |
| 682 |
|
| 683 |
bool ok = true; |
| 684 |
|
| 685 |
// setup paths |
| 686 |
CanonicalPath canonical_trait_type = CanonicalPath::create_empty (); |
| 687 |
|
| 688 |
ok = ResolveTypeToCanonicalPath::go (impl_block.get_trait_path (), |
| 689 |
canonical_trait_type); |
| 690 |
if (!ok) |
| 691 |
{ |
| 692 |
resolver->get_name_scope ().pop (); |
| 693 |
resolver->get_type_scope ().pop (); |
| 694 |
resolver->get_label_scope ().pop (); |
| 695 |
return; |
| 696 |
} |
| 697 |
|
| 698 |
rust_debug ("AST::TraitImpl resolve trait type: {%s}", |
| 699 |
canonical_trait_type.get ().c_str ()); |
| 700 |
|
| 701 |
CanonicalPath canonical_impl_type = CanonicalPath::create_empty (); |
| 702 |
ok = ResolveTypeToCanonicalPath::go (impl_block.get_type (), |
| 703 |
canonical_impl_type); |
| 704 |
if (!ok) |
| 705 |
{ |
| 706 |
resolver->get_name_scope ().pop (); |
| 707 |
resolver->get_type_scope ().pop (); |
| 708 |
resolver->get_label_scope ().pop (); |
| 709 |
return; |
| 710 |
} |
| 711 |
|
| 712 |
rust_debug ("AST::TraitImpl resolve self: {%s}", |
| 713 |
canonical_impl_type.get ().c_str ()); |
| 714 |
|
| 715 |
// raw paths |
| 716 |
CanonicalPath impl_type_seg = canonical_impl_type; |
| 717 |
CanonicalPath trait_type_seg = canonical_trait_type; |
| 718 |
CanonicalPath projection |
| 719 |
= CanonicalPath::trait_impl_projection_seg (impl_block.get_node_id (), |
| 720 |
trait_type_seg, impl_type_seg); |
| 721 |
CanonicalPath impl_prefix = prefix.append (projection); |
| 722 |
|
| 723 |
// setup canonical-path |
| 724 |
CanonicalPath canonical_projection |
| 725 |
= CanonicalPath::trait_impl_projection_seg (impl_block.get_node_id (), |
| 726 |
canonical_trait_type, |
| 727 |
canonical_impl_type); |
| 728 |
CanonicalPath cpath = CanonicalPath::create_empty (); |
| 729 |
if (canonical_prefix.size () <= 1) |
| 730 |
{ |
| 731 |
cpath = canonical_projection; |
| 732 |
} |
| 733 |
else |
| 734 |
{ |
| 735 |
std::string projection_str = canonical_projection.get (); |
| 736 |
std::string seg_buf |
| 737 |
= "<impl " + projection_str.substr (1, projection_str.size () - 2) |
| 738 |
+ ">"; |
| 739 |
CanonicalPath seg |
| 740 |
= CanonicalPath::new_seg (impl_block.get_node_id (), seg_buf); |
| 741 |
cpath = canonical_prefix.append (seg); |
| 742 |
} |
| 743 |
|
| 744 |
// DONE setup canonical-path |
| 745 |
|
| 746 |
auto Self |
| 747 |
= CanonicalPath::get_big_self (impl_block.get_type ().get_node_id ()); |
| 748 |
|
| 749 |
resolver->get_type_scope ().insert (Self, |
| 750 |
impl_block.get_type ().get_node_id (), |
| 751 |
impl_block.get_type ().get_locus ()); |
| 752 |
|
| 753 |
for (auto &impl_item : impl_block.get_impl_items ()) |
| 754 |
{ |
| 755 |
rust_debug ( |
| 756 |
"AST::TraitImpl resolve_impl_item: impl_prefix={%s} cpath={%s}", |
| 757 |
impl_prefix.get ().c_str (), cpath.get ().c_str ()); |
| 758 |
resolve_impl_item (*impl_item, impl_prefix, cpath); |
| 759 |
} |
| 760 |
|
| 761 |
Rib *r = resolver->get_type_scope ().peek (); |
| 762 |
r->clear_name (Self, impl_block.get_type ().get_node_id ()); |
| 763 |
|
| 764 |
resolver->get_name_scope ().pop (); |
| 765 |
resolver->get_type_scope ().pop (); |
| 766 |
resolver->get_label_scope ().pop (); |
| 767 |
} |
| 768 |
|
| 769 |
void |
| 770 |
ResolveItem::visit (AST::Trait &trait) |
| 771 |
{ |
| 772 |
NodeId scope_node_id = trait.get_node_id (); |
| 773 |
|
| 774 |
resolve_visibility (trait.get_visibility ()); |
| 775 |
|
| 776 |
resolver->get_name_scope ().push (scope_node_id); |
| 777 |
resolver->get_type_scope ().push (scope_node_id); |
| 778 |
resolver->push_new_name_rib (resolver->get_name_scope ().peek ()); |
| 779 |
resolver->push_new_type_rib (resolver->get_type_scope ().peek ()); |
| 780 |
|
| 781 |
ResolveGenericParams::go_single (trait.get_implicit_self (), prefix, |
| 782 |
canonical_prefix); |
| 783 |
ResolveGenericParams::go (trait.get_generic_params (), prefix, |
| 784 |
canonical_prefix); |
| 785 |
|
| 786 |
// Self is an implicit TypeParam so lets mark it as such |
| 787 |
resolver->get_type_scope ().append_reference_for_def ( |
| 788 |
trait.get_node_id (), trait.get_implicit_self ().get_node_id ()); |
| 789 |
|
| 790 |
if (trait.has_type_param_bounds ()) |
| 791 |
{ |
| 792 |
for (auto &bound : trait.get_type_param_bounds ()) |
| 793 |
{ |
| 794 |
ResolveTypeBound::go (*bound); |
| 795 |
} |
| 796 |
} |
| 797 |
|
| 798 |
// resolve any where clause items |
| 799 |
if (trait.has_where_clause ()) |
| 800 |
ResolveWhereClause::Resolve (trait.get_where_clause ()); |
| 801 |
|
| 802 |
// resolve the paths |
| 803 |
CanonicalPath path = CanonicalPath::create_empty (); |
| 804 |
CanonicalPath cpath = CanonicalPath::create_empty (); |
| 805 |
// |
| 806 |
|
| 807 |
for (auto &item : trait.get_trait_items ()) |
| 808 |
{ |
| 809 |
ResolveTraitItems::go (item.get (), path, cpath); |
| 810 |
} |
| 811 |
|
| 812 |
resolver->get_type_scope ().pop (); |
| 813 |
resolver->get_name_scope ().pop (); |
| 814 |
} |
| 815 |
|
| 816 |
void |
| 817 |
ResolveItem::visit (AST::ExternBlock &extern_block) |
| 818 |
{ |
| 819 |
resolve_visibility (extern_block.get_visibility ()); |
| 820 |
|
| 821 |
for (auto &item : extern_block.get_extern_items ()) |
| 822 |
{ |
| 823 |
resolve_extern_item (*item); |
| 824 |
} |
| 825 |
} |
| 826 |
|
| 827 |
void |
| 828 |
ResolveItem::resolve_impl_item (AST::AssociatedItem &item, |
| 829 |
const CanonicalPath &prefix, |
| 830 |
const CanonicalPath &canonical_prefix) |
| 831 |
{ |
| 832 |
ResolveImplItems::go (item, prefix, canonical_prefix); |
| 833 |
} |
| 834 |
|
| 835 |
void |
| 836 |
ResolveItem::resolve_extern_item (AST::ExternalItem &item) |
| 837 |
{ |
| 838 |
ResolveExternItem::go (item, prefix, canonical_prefix); |
| 839 |
} |
| 840 |
|
| 841 |
static void |
| 842 |
flatten_glob (const AST::UseTreeGlob &glob, std::vector<Import> &imports); |
| 843 |
static void |
| 844 |
flatten_rebind (const AST::UseTreeRebind &glob, std::vector<Import> &imports); |
| 845 |
static void |
| 846 |
flatten_list (const AST::UseTreeList &glob, std::vector<Import> &imports); |
| 847 |
|
| 848 |
static void |
| 849 |
flatten (const AST::UseTree *tree, std::vector<Import> &imports) |
| 850 |
{ |
| 851 |
switch (tree->get_kind ()) |
| 852 |
{ |
| 853 |
case AST::UseTree::Glob: { |
| 854 |
auto glob = static_cast<const AST::UseTreeGlob *> (tree); |
| 855 |
flatten_glob (*glob, imports); |
| 856 |
break; |
| 857 |
} |
| 858 |
case AST::UseTree::Rebind: { |
| 859 |
auto rebind = static_cast<const AST::UseTreeRebind *> (tree); |
| 860 |
flatten_rebind (*rebind, imports); |
| 861 |
break; |
| 862 |
} |
| 863 |
case AST::UseTree::List: { |
| 864 |
auto list = static_cast<const AST::UseTreeList *> (tree); |
| 865 |
flatten_list (*list, imports); |
| 866 |
break; |
| 867 |
} |
| 868 |
break; |
| 869 |
} |
| 870 |
} |
| 871 |
|
| 872 |
static void |
| 873 |
flatten_glob (const AST::UseTreeGlob &glob, std::vector<Import> &imports) |
| 874 |
{ |
| 875 |
if (glob.has_path ()) |
| 876 |
imports.emplace_back (glob.get_path (), true, std::string ()); |
| 877 |
} |
| 878 |
|
| 879 |
static void |
| 880 |
flatten_rebind (const AST::UseTreeRebind &rebind, std::vector<Import> &imports) |
| 881 |
{ |
| 882 |
auto path = rebind.get_path (); |
| 883 |
|
| 884 |
std::string label; |
| 885 |
if (rebind.has_identifier ()) |
| 886 |
label = rebind.get_identifier ().as_string (); |
| 887 |
else |
| 888 |
label = path.get_final_segment ().as_string (); |
| 889 |
|
| 890 |
imports.emplace_back (path, false, label); |
| 891 |
} |
| 892 |
|
| 893 |
static void |
| 894 |
flatten_list (const AST::UseTreeList &list, std::vector<Import> &imports) |
| 895 |
{ |
| 896 |
auto prefix = AST::SimplePath::create_empty (); |
| 897 |
if (list.has_path ()) |
| 898 |
prefix = list.get_path (); |
| 899 |
|
| 900 |
for (const auto &tree : list.get_trees ()) |
| 901 |
{ |
| 902 |
// append imports to the main list, then modify them in-place |
| 903 |
auto start_idx = imports.size (); |
| 904 |
flatten (tree.get (), imports); |
| 905 |
|
| 906 |
for (auto import = imports.begin () + start_idx; import != imports.end (); |
| 907 |
import++) |
| 908 |
{ |
| 909 |
// avoid duplicate node ids |
| 910 |
auto prefix_copy |
| 911 |
= AST::SimplePath ({}, prefix.has_opening_scope_resolution (), |
| 912 |
prefix.get_locus ()); |
| 913 |
for (auto &seg : prefix.get_segments ()) |
| 914 |
prefix_copy.get_segments ().push_back ( |
| 915 |
AST::SimplePathSegment (seg.get_segment_name (), |
| 916 |
seg.get_locus ())); |
| 917 |
|
| 918 |
import->add_prefix (std::move (prefix_copy)); |
| 919 |
} |
| 920 |
} |
| 921 |
} |
| 922 |
|
| 923 |
void |
| 924 |
Import::add_prefix (AST::SimplePath prefix) |
| 925 |
{ |
| 926 |
AST::SimplePath old_path (std::move (path)); |
| 927 |
path = std::move (prefix); |
| 928 |
std::move (old_path.get_segments ().begin (), old_path.get_segments ().end (), |
| 929 |
std::back_inserter (path.get_segments ())); |
| 930 |
} |
| 931 |
|
| 932 |
/** |
| 933 |
* Flatten a UseDeclaration's UseTree into multiple simple paths to resolve. |
| 934 |
* |
| 935 |
* Given the following use declarations: |
| 936 |
* ``` |
| 937 |
* use some::path::to_resolve; #1 |
| 938 |
* use some::path::to_glob::*; #2 |
| 939 |
* use some::path::{one, two}; #2 |
| 940 |
* ``` |
| 941 |
* |
| 942 |
* In the first case, we simply want to return a vector with a single |
| 943 |
* SimplePath: |
| 944 |
* [some::path::to_resolve] |
| 945 |
* |
| 946 |
* In the second case, we want to resolve the glob's "origin path": |
| 947 |
* [some::path::to_glob] |
| 948 |
* |
| 949 |
* Finally in the third case, we want to create two SimplePaths to resolve: |
| 950 |
* [some::path::one, some::path::two] |
| 951 |
*/ |
| 952 |
static std::vector<Import> |
| 953 |
flatten_use_dec_to_imports (const AST::UseDeclaration &use_item) |
| 954 |
{ |
| 955 |
auto imports = std::vector<Import> (); |
| 956 |
|
| 957 |
const auto &tree = use_item.get_tree (); |
| 958 |
flatten (tree.get (), imports); |
| 959 |
|
| 960 |
return imports; |
| 961 |
} |
| 962 |
|
| 963 |
void |
| 964 |
ResolveItem::visit (AST::UseDeclaration &use_item) |
| 965 |
{ |
| 966 |
std::vector<Import> to_resolve = flatten_use_dec_to_imports (use_item); |
| 967 |
|
| 968 |
// FIXME: I think this does not actually resolve glob use-decls and is going |
| 969 |
// the wrong way about it. RFC #1560 specifies the following: |
| 970 |
// |
| 971 |
// > When we find a glob import, we have to record a 'back link', so that when |
| 972 |
// a public name is added for the supplying module, we can add it for the |
| 973 |
// importing module. |
| 974 |
// |
| 975 |
// Which is the opposite of what we're doing if I understand correctly? |
| 976 |
|
| 977 |
NodeId current_module = resolver->peek_current_module_scope (); |
| 978 |
for (auto &import : to_resolve) |
| 979 |
{ |
| 980 |
auto &path = import.get_path (); |
| 981 |
|
| 982 |
rust_debug ("resolving use-decl path: [%s]", path.as_string ().c_str ()); |
| 983 |
NodeId resolved_node_id = ResolvePath::go (path); |
| 984 |
bool ok = resolved_node_id != UNKNOWN_NODEID; |
| 985 |
if (!ok) |
| 986 |
continue; |
| 987 |
|
| 988 |
if (import.is_glob ()) |
| 989 |
continue; |
| 990 |
|
| 991 |
auto decl = CanonicalPath::new_seg (resolved_node_id, import.get_name ()); |
| 992 |
mappings.insert_module_child_item (current_module, decl); |
| 993 |
|
| 994 |
resolver->get_type_scope ().insert (decl, resolved_node_id, |
| 995 |
path.get_locus (), |
| 996 |
Rib::ItemType::Type); |
| 997 |
rust_debug ("use-decl rexporting: [%s]", decl.get ().c_str ()); |
| 998 |
} |
| 999 |
} |
| 1000 |
|
| 1001 |
ResolveImplItems::ResolveImplItems (const CanonicalPath &prefix, |
| 1002 |
const CanonicalPath &canonical_prefix) |
| 1003 |
: ResolveItem (prefix, canonical_prefix) |
| 1004 |
{} |
| 1005 |
|
| 1006 |
void |
| 1007 |
ResolveImplItems::go (AST::AssociatedItem &item, const CanonicalPath &prefix, |
| 1008 |
const CanonicalPath &canonical_prefix) |
| 1009 |
{ |
| 1010 |
if (item.is_marked_for_strip ()) |
| 1011 |
return; |
| 1012 |
|
| 1013 |
ResolveImplItems resolver (prefix, canonical_prefix); |
| 1014 |
item.accept_vis (resolver); |
| 1015 |
} |
| 1016 |
|
| 1017 |
void |
| 1018 |
ResolveImplItems::visit (AST::TypeAlias &alias) |
| 1019 |
{ |
| 1020 |
ResolveItem::visit (alias); |
| 1021 |
|
| 1022 |
resolve_visibility (alias.get_visibility ()); |
| 1023 |
|
| 1024 |
// FIXME this stops the erronious unused decls which will be fixed later on |
| 1025 |
resolver->get_type_scope ().append_reference_for_def (alias.get_node_id (), |
| 1026 |
alias.get_node_id ()); |
| 1027 |
} |
| 1028 |
|
| 1029 |
void |
| 1030 |
ResolveExternItem::go (AST::ExternalItem &item, const CanonicalPath &prefix, |
| 1031 |
const CanonicalPath &canonical_prefix) |
| 1032 |
{ |
| 1033 |
ResolveExternItem resolver (prefix, canonical_prefix); |
| 1034 |
item.accept_vis (resolver); |
| 1035 |
} |
| 1036 |
|
| 1037 |
void |
| 1038 |
ResolveExternItem::visit (AST::Function &function) |
| 1039 |
{ |
| 1040 |
NodeId scope_node_id = function.get_node_id (); |
| 1041 |
auto decl |
| 1042 |
= CanonicalPath::new_seg (function.get_node_id (), |
| 1043 |
function.get_function_name ().as_string ()); |
| 1044 |
auto path = prefix.append (decl); |
| 1045 |
auto cpath = canonical_prefix.append (decl); |
| 1046 |
|
| 1047 |
mappings.insert_canonical_path (function.get_node_id (), cpath); |
| 1048 |
|
| 1049 |
resolve_visibility (function.get_visibility ()); |
| 1050 |
|
| 1051 |
resolver->get_name_scope ().push (scope_node_id); |
| 1052 |
resolver->get_type_scope ().push (scope_node_id); |
| 1053 |
resolver->get_label_scope ().push (scope_node_id); |
| 1054 |
resolver->push_new_name_rib (resolver->get_name_scope ().peek ()); |
| 1055 |
resolver->push_new_type_rib (resolver->get_type_scope ().peek ()); |
| 1056 |
resolver->push_new_label_rib (resolver->get_label_scope ().peek ()); |
| 1057 |
|
| 1058 |
// resolve the generics |
| 1059 |
if (function.has_generics ()) |
| 1060 |
ResolveGenericParams::go (function.get_generic_params (), prefix, |
| 1061 |
canonical_prefix); |
| 1062 |
|
| 1063 |
if (function.has_return_type ()) |
| 1064 |
ResolveType::go (function.get_return_type ()); |
| 1065 |
|
| 1066 |
// we make a new scope so the names of parameters are resolved and shadowed |
| 1067 |
// correctly |
| 1068 |
for (auto ¶m : function.get_function_params ()) |
| 1069 |
if (!param->is_variadic ()) |
| 1070 |
{ |
| 1071 |
auto &p = static_cast<AST::FunctionParam &> (*param); |
| 1072 |
ResolveType::go (p.get_type ()); |
| 1073 |
} |
| 1074 |
|
| 1075 |
// done |
| 1076 |
resolver->get_name_scope ().pop (); |
| 1077 |
resolver->get_type_scope ().pop (); |
| 1078 |
resolver->get_label_scope ().pop (); |
| 1079 |
} |
| 1080 |
|
| 1081 |
void |
| 1082 |
ResolveExternItem::visit (AST::ExternalStaticItem &item) |
| 1083 |
{ |
| 1084 |
resolve_visibility (item.get_visibility ()); |
| 1085 |
|
| 1086 |
ResolveType::go (item.get_type ()); |
| 1087 |
} |
| 1088 |
|
| 1089 |
} // namespace Resolver |
| 1090 |
} // namespace Rust |
| 1091 |
|
| 1092 |
#if CHECKING_P |
| 1093 |
|
| 1094 |
namespace selftest { |
| 1095 |
|
| 1096 |
static void |
| 1097 |
rust_flatten_nested_glob (void) |
| 1098 |
{ |
| 1099 |
auto foo = Rust::AST::SimplePathSegment ("foo", UNDEF_LOCATION); |
| 1100 |
auto bar = Rust::AST::SimplePathSegment ("bar", UNDEF_LOCATION); |
| 1101 |
auto foobar = Rust::AST::SimplePath ({foo, bar}); |
| 1102 |
|
| 1103 |
auto glob |
| 1104 |
= Rust::AST::UseTreeGlob (Rust::AST::UseTreeGlob::PathType::PATH_PREFIXED, |
| 1105 |
foobar, UNDEF_LOCATION); |
| 1106 |
|
| 1107 |
auto imports = std::vector<Rust::Resolver::Import> (); |
| 1108 |
Rust::Resolver::flatten_glob (glob, imports); |
| 1109 |
|
| 1110 |
ASSERT_TRUE (!imports.empty ()); |
| 1111 |
ASSERT_EQ (imports.size (), 1); |
| 1112 |
ASSERT_EQ (imports[0].get_path ().get_segments ()[0].as_string (), "foo"); |
| 1113 |
ASSERT_EQ (imports[0].get_path ().get_segments ()[1].as_string (), "bar"); |
| 1114 |
} |
| 1115 |
|
| 1116 |
static void |
| 1117 |
rust_flatten_glob (void) |
| 1118 |
{ |
| 1119 |
auto frob = Rust::AST::SimplePath::from_str ("frobulator", UNDEF_LOCATION); |
| 1120 |
|
| 1121 |
auto glob |
| 1122 |
= Rust::AST::UseTreeGlob (Rust::AST::UseTreeGlob::PathType::PATH_PREFIXED, |
| 1123 |
frob, UNDEF_LOCATION); |
| 1124 |
|
| 1125 |
auto imports = std::vector<Rust::Resolver::Import> (); |
| 1126 |
Rust::Resolver::flatten_glob (glob, imports); |
| 1127 |
|
| 1128 |
ASSERT_TRUE (!imports.empty ()); |
| 1129 |
ASSERT_EQ (imports.size (), 1); |
| 1130 |
ASSERT_EQ (imports[0].get_path (), "frobulator"); |
| 1131 |
} |
| 1132 |
|
| 1133 |
static void |
| 1134 |
rust_flatten_rebind_none (void) |
| 1135 |
{ |
| 1136 |
auto foo = Rust::AST::SimplePathSegment ("foo", UNDEF_LOCATION); |
| 1137 |
auto bar = Rust::AST::SimplePathSegment ("bar", UNDEF_LOCATION); |
| 1138 |
auto foobar = Rust::AST::SimplePath ({foo, bar}); |
| 1139 |
|
| 1140 |
auto rebind = Rust::AST::UseTreeRebind (Rust::AST::UseTreeRebind::NONE, |
| 1141 |
foobar, UNDEF_LOCATION); |
| 1142 |
|
| 1143 |
auto imports = std::vector<Rust::Resolver::Import> (); |
| 1144 |
Rust::Resolver::flatten_rebind (rebind, imports); |
| 1145 |
|
| 1146 |
ASSERT_TRUE (!imports.empty ()); |
| 1147 |
ASSERT_EQ (imports.size (), 1); |
| 1148 |
ASSERT_EQ (imports[0].get_path ().get_segments ()[0].as_string (), "foo"); |
| 1149 |
ASSERT_EQ (imports[0].get_path ().get_segments ()[1].as_string (), "bar"); |
| 1150 |
} |
| 1151 |
|
| 1152 |
static void |
| 1153 |
rust_flatten_rebind (void) |
| 1154 |
{ |
| 1155 |
auto frob = Rust::AST::SimplePath::from_str ("frobulator", UNDEF_LOCATION); |
| 1156 |
|
| 1157 |
auto rebind = Rust::AST::UseTreeRebind (Rust::AST::UseTreeRebind::IDENTIFIER, |
| 1158 |
frob, UNDEF_LOCATION, {"saindoux"}); |
| 1159 |
|
| 1160 |
auto imports = std::vector<Rust::Resolver::Import> (); |
| 1161 |
Rust::Resolver::flatten_rebind (rebind, imports); |
| 1162 |
|
| 1163 |
ASSERT_TRUE (!imports.empty ()); |
| 1164 |
ASSERT_EQ (imports.size (), 1); |
| 1165 |
ASSERT_EQ (imports[0].get_path (), "frobulator"); |
| 1166 |
ASSERT_EQ (imports[0].get_name (), "saindoux"); |
| 1167 |
} |
| 1168 |
|
| 1169 |
static void |
| 1170 |
rust_flatten_rebind_nested (void) |
| 1171 |
{ |
| 1172 |
auto foo = Rust::AST::SimplePathSegment ("foo", UNDEF_LOCATION); |
| 1173 |
auto bar = Rust::AST::SimplePathSegment ("bar", UNDEF_LOCATION); |
| 1174 |
auto baz = Rust::AST::SimplePathSegment ("baz", UNDEF_LOCATION); |
| 1175 |
|
| 1176 |
auto foo_bar_baz = Rust::AST::SimplePath ({foo, bar, baz}); |
| 1177 |
|
| 1178 |
auto rebind |
| 1179 |
= Rust::AST::UseTreeRebind (Rust::AST::UseTreeRebind::IDENTIFIER, |
| 1180 |
foo_bar_baz, UNDEF_LOCATION, {"saindoux"}); |
| 1181 |
|
| 1182 |
auto imports = std::vector<Rust::Resolver::Import> (); |
| 1183 |
Rust::Resolver::flatten_rebind (rebind, imports); |
| 1184 |
|
| 1185 |
ASSERT_TRUE (!imports.empty ()); |
| 1186 |
ASSERT_EQ (imports.size (), 1); |
| 1187 |
ASSERT_EQ (imports[0].get_path ().get_segments ()[0].as_string (), "foo"); |
| 1188 |
ASSERT_EQ (imports[0].get_path ().get_segments ()[1].as_string (), "bar"); |
| 1189 |
ASSERT_EQ (imports[0].get_path ().get_segments ()[2].as_string (), "baz"); |
| 1190 |
ASSERT_EQ (imports[0].get_name (), "saindoux"); |
| 1191 |
} |
| 1192 |
|
| 1193 |
static void |
| 1194 |
rust_flatten_list (void) |
| 1195 |
{ |
| 1196 |
auto foo = Rust::AST::SimplePathSegment ("foo", UNDEF_LOCATION); |
| 1197 |
auto bar = Rust::AST::SimplePathSegment ("bar", UNDEF_LOCATION); |
| 1198 |
auto foo_bar = Rust::AST::SimplePath ({foo, bar}); |
| 1199 |
|
| 1200 |
auto baz = Rust::AST::SimplePath::from_str ("baz", UNDEF_LOCATION); |
| 1201 |
auto bul = Rust::AST::SimplePath::from_str ("bul", UNDEF_LOCATION); |
| 1202 |
|
| 1203 |
// use foo::bar::{baz, bul}; |
| 1204 |
|
| 1205 |
auto use0 = std::unique_ptr<Rust::AST::UseTree> ( |
| 1206 |
new Rust::AST::UseTreeRebind (Rust::AST::UseTreeRebind::NONE, baz, |
| 1207 |
UNDEF_LOCATION)); |
| 1208 |
auto use1 = std::unique_ptr<Rust::AST::UseTree> ( |
| 1209 |
new Rust::AST::UseTreeRebind (Rust::AST::UseTreeRebind::NONE, bul, |
| 1210 |
UNDEF_LOCATION)); |
| 1211 |
|
| 1212 |
auto uses = std::vector<std::unique_ptr<Rust::AST::UseTree>> (); |
| 1213 |
uses.emplace_back (std::move (use0)); |
| 1214 |
uses.emplace_back (std::move (use1)); |
| 1215 |
|
| 1216 |
auto list |
| 1217 |
= Rust::AST::UseTreeList (Rust::AST::UseTreeList::PATH_PREFIXED, foo_bar, |
| 1218 |
std::move (uses), UNDEF_LOCATION); |
| 1219 |
|
| 1220 |
auto imports = std::vector<Rust::Resolver::Import> (); |
| 1221 |
Rust::Resolver::flatten_list (list, imports); |
| 1222 |
|
| 1223 |
ASSERT_TRUE (!imports.empty ()); |
| 1224 |
ASSERT_EQ (imports.size (), 2); |
| 1225 |
ASSERT_EQ (imports[0].get_path ().get_segments ()[0].as_string (), "foo"); |
| 1226 |
ASSERT_EQ (imports[0].get_path ().get_segments ()[1].as_string (), "bar"); |
| 1227 |
ASSERT_EQ (imports[0].get_path ().get_segments ()[2].as_string (), "baz"); |
| 1228 |
ASSERT_EQ (imports[1].get_path ().get_segments ()[0].as_string (), "foo"); |
| 1229 |
ASSERT_EQ (imports[1].get_path ().get_segments ()[1].as_string (), "bar"); |
| 1230 |
ASSERT_EQ (imports[1].get_path ().get_segments ()[2].as_string (), "bul"); |
| 1231 |
} |
| 1232 |
|
| 1233 |
static void |
| 1234 |
rust_use_dec_flattening (void) |
| 1235 |
{ |
| 1236 |
rust_flatten_glob (); |
| 1237 |
rust_flatten_nested_glob (); |
| 1238 |
rust_flatten_rebind_none (); |
| 1239 |
rust_flatten_rebind (); |
| 1240 |
rust_flatten_rebind_nested (); |
| 1241 |
rust_flatten_list (); |
| 1242 |
} |
| 1243 |
|
| 1244 |
void |
| 1245 |
rust_simple_path_resolve_test (void) |
| 1246 |
{ |
| 1247 |
rust_use_dec_flattening (); |
| 1248 |
} |
| 1249 |
|
| 1250 |
} // namespace selftest |
| 1251 |
|
| 1252 |
#endif // CHECKING_P |