| 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 |
#ifndef RUST_AST_RESOLVE_IMPLITEM_H |
| 20 |
#define RUST_AST_RESOLVE_IMPLITEM_H |
| 21 |
|
| 22 |
#include "rust-ast-resolve-base.h" |
| 23 |
#include "rust-ast-resolve-type.h" |
| 24 |
#include "rust-ast-full.h" |
| 25 |
|
| 26 |
namespace Rust { |
| 27 |
namespace Resolver { |
| 28 |
|
| 29 |
class ResolveToplevelImplItem : public ResolverBase |
| 30 |
{ |
| 31 |
using Rust::Resolver::ResolverBase::visit; |
| 32 |
|
| 33 |
public: |
| 34 |
static void go (AST::AssociatedItem &item, const CanonicalPath &prefix) |
| 35 |
{ |
| 36 |
if (item.is_marked_for_strip ()) |
| 37 |
return; |
| 38 |
|
| 39 |
ResolveToplevelImplItem resolver (prefix); |
| 40 |
item.accept_vis (resolver); |
| 41 |
} |
| 42 |
|
| 43 |
void visit (AST::TypeAlias &type) override |
| 44 |
{ |
| 45 |
auto decl = CanonicalPath::new_seg (type.get_node_id (), |
| 46 |
type.get_new_type_name ().as_string ()); |
| 47 |
auto path = prefix.append (decl); |
| 48 |
|
| 49 |
resolver->get_type_scope ().insert ( |
| 50 |
path, type.get_node_id (), type.get_locus (), false, Rib::ItemType::Type, |
| 51 |
[&] (const CanonicalPath &, NodeId, location_t locus) -> void { |
| 52 |
rich_location r (line_table, type.get_locus ()); |
| 53 |
r.add_range (locus); |
| 54 |
redefined_error (r); |
| 55 |
}); |
| 56 |
} |
| 57 |
|
| 58 |
void visit (AST::ConstantItem &constant) override |
| 59 |
{ |
| 60 |
auto decl = CanonicalPath::new_seg (constant.get_node_id (), |
| 61 |
constant.get_identifier ()); |
| 62 |
auto path = prefix.append (decl); |
| 63 |
|
| 64 |
resolver->get_name_scope ().insert ( |
| 65 |
path, constant.get_node_id (), constant.get_locus (), false, |
| 66 |
Rib::ItemType::Const, |
| 67 |
[&] (const CanonicalPath &, NodeId, location_t locus) -> void { |
| 68 |
rich_location r (line_table, constant.get_locus ()); |
| 69 |
r.add_range (locus); |
| 70 |
redefined_error (r); |
| 71 |
}); |
| 72 |
} |
| 73 |
|
| 74 |
void visit (AST::Function &function) override |
| 75 |
{ |
| 76 |
auto decl |
| 77 |
= CanonicalPath::new_seg (function.get_node_id (), |
| 78 |
function.get_function_name ().as_string ()); |
| 79 |
auto path = prefix.append (decl); |
| 80 |
|
| 81 |
resolver->get_name_scope ().insert ( |
| 82 |
path, function.get_node_id (), function.get_locus (), false, |
| 83 |
Rib::ItemType::Function, |
| 84 |
[&] (const CanonicalPath &, NodeId, location_t locus) -> void { |
| 85 |
rich_location r (line_table, function.get_locus ()); |
| 86 |
r.add_range (locus); |
| 87 |
redefined_error (r); |
| 88 |
}); |
| 89 |
} |
| 90 |
|
| 91 |
private: |
| 92 |
ResolveToplevelImplItem (const CanonicalPath &prefix) |
| 93 |
: ResolverBase (), prefix (prefix) |
| 94 |
{ |
| 95 |
rust_assert (!prefix.is_empty ()); |
| 96 |
} |
| 97 |
|
| 98 |
const CanonicalPath &prefix; |
| 99 |
}; |
| 100 |
|
| 101 |
class ResolveTopLevelTraitItems : public ResolverBase |
| 102 |
{ |
| 103 |
using Rust::Resolver::ResolverBase::visit; |
| 104 |
|
| 105 |
public: |
| 106 |
static void go (AST::AssociatedItem *item, const CanonicalPath &prefix, |
| 107 |
const CanonicalPath &canonical_prefix) |
| 108 |
{ |
| 109 |
ResolveTopLevelTraitItems resolver (prefix, canonical_prefix); |
| 110 |
item->accept_vis (resolver); |
| 111 |
}; |
| 112 |
|
| 113 |
void visit (AST::Function &function) override |
| 114 |
{ |
| 115 |
auto decl |
| 116 |
= CanonicalPath::new_seg (function.get_node_id (), |
| 117 |
function.get_function_name ().as_string ()); |
| 118 |
auto path = prefix.append (decl); |
| 119 |
auto cpath = canonical_prefix.append (decl); |
| 120 |
|
| 121 |
resolver->get_name_scope ().insert ( |
| 122 |
path, function.get_node_id (), function.get_locus (), false, |
| 123 |
Rib::ItemType::Function, |
| 124 |
[&] (const CanonicalPath &, NodeId, location_t locus) -> void { |
| 125 |
rich_location r (line_table, function.get_locus ()); |
| 126 |
r.add_range (locus); |
| 127 |
redefined_error (r); |
| 128 |
}); |
| 129 |
|
| 130 |
mappings.insert_canonical_path (function.get_node_id (), cpath); |
| 131 |
} |
| 132 |
|
| 133 |
void visit (AST::TraitItemConst &constant) override |
| 134 |
{ |
| 135 |
auto decl |
| 136 |
= CanonicalPath::new_seg (constant.get_node_id (), |
| 137 |
constant.get_identifier ().as_string ()); |
| 138 |
auto path = prefix.append (decl); |
| 139 |
auto cpath = canonical_prefix.append (decl); |
| 140 |
|
| 141 |
resolver->get_name_scope ().insert ( |
| 142 |
path, constant.get_node_id (), constant.get_locus (), false, |
| 143 |
Rib::ItemType::Const, |
| 144 |
[&] (const CanonicalPath &, NodeId, location_t locus) -> void { |
| 145 |
rich_location r (line_table, constant.get_locus ()); |
| 146 |
r.add_range (locus); |
| 147 |
redefined_error (r); |
| 148 |
}); |
| 149 |
|
| 150 |
mappings.insert_canonical_path (constant.get_node_id (), cpath); |
| 151 |
} |
| 152 |
|
| 153 |
void visit (AST::TraitItemType &type) override |
| 154 |
{ |
| 155 |
auto decl = CanonicalPath::new_seg (type.get_node_id (), |
| 156 |
type.get_identifier ().as_string ()); |
| 157 |
auto path = prefix.append (decl); |
| 158 |
auto cpath = canonical_prefix.append (decl); |
| 159 |
|
| 160 |
resolver->get_type_scope ().insert ( |
| 161 |
path, type.get_node_id (), type.get_locus (), false, Rib::ItemType::Type, |
| 162 |
[&] (const CanonicalPath &, NodeId, location_t locus) -> void { |
| 163 |
rich_location r (line_table, type.get_locus ()); |
| 164 |
r.add_range (locus); |
| 165 |
redefined_error (r); |
| 166 |
}); |
| 167 |
|
| 168 |
mappings.insert_canonical_path (type.get_node_id (), cpath); |
| 169 |
} |
| 170 |
|
| 171 |
private: |
| 172 |
ResolveTopLevelTraitItems (const CanonicalPath &prefix, |
| 173 |
const CanonicalPath &canonical_prefix) |
| 174 |
: ResolverBase (), prefix (prefix), canonical_prefix (canonical_prefix) |
| 175 |
{} |
| 176 |
|
| 177 |
const CanonicalPath &prefix; |
| 178 |
const CanonicalPath &canonical_prefix; |
| 179 |
}; |
| 180 |
|
| 181 |
class ResolveToplevelExternItem : public ResolverBase |
| 182 |
{ |
| 183 |
using Rust::Resolver::ResolverBase::visit; |
| 184 |
|
| 185 |
public: |
| 186 |
static void go (AST::ExternalItem &item, const CanonicalPath &prefix) |
| 187 |
{ |
| 188 |
ResolveToplevelExternItem resolver (prefix); |
| 189 |
item.accept_vis (resolver); |
| 190 |
}; |
| 191 |
|
| 192 |
void visit (AST::Function &function) override |
| 193 |
{ |
| 194 |
auto decl |
| 195 |
= CanonicalPath::new_seg (function.get_node_id (), |
| 196 |
function.get_function_name ().as_string ()); |
| 197 |
auto path = prefix.append (decl); |
| 198 |
|
| 199 |
resolver->get_name_scope ().insert ( |
| 200 |
path, function.get_node_id (), function.get_locus (), false, |
| 201 |
Rib::ItemType::Function, |
| 202 |
[&] (const CanonicalPath &, NodeId, location_t locus) -> void { |
| 203 |
rich_location r (line_table, function.get_locus ()); |
| 204 |
r.add_range (locus); |
| 205 |
redefined_error (r); |
| 206 |
}); |
| 207 |
|
| 208 |
NodeId current_module = resolver->peek_current_module_scope (); |
| 209 |
mappings.insert_module_child_item (current_module, decl); |
| 210 |
} |
| 211 |
|
| 212 |
void visit (AST::ExternalStaticItem &item) override |
| 213 |
{ |
| 214 |
auto decl = CanonicalPath::new_seg (item.get_node_id (), |
| 215 |
item.get_identifier ().as_string ()); |
| 216 |
auto path = prefix.append (decl); |
| 217 |
|
| 218 |
resolver->get_name_scope ().insert ( |
| 219 |
path, item.get_node_id (), item.get_locus (), false, |
| 220 |
Rib::ItemType::Static, |
| 221 |
[&] (const CanonicalPath &, NodeId, location_t locus) -> void { |
| 222 |
rich_location r (line_table, item.get_locus ()); |
| 223 |
r.add_range (locus); |
| 224 |
redefined_error (r); |
| 225 |
}); |
| 226 |
|
| 227 |
NodeId current_module = resolver->peek_current_module_scope (); |
| 228 |
mappings.insert_module_child_item (current_module, decl); |
| 229 |
} |
| 230 |
|
| 231 |
void visit (AST::ExternalTypeItem &type) override |
| 232 |
{ |
| 233 |
auto decl = CanonicalPath::new_seg (type.get_node_id (), |
| 234 |
type.get_identifier ().as_string ()); |
| 235 |
auto path = prefix.append (decl); |
| 236 |
|
| 237 |
resolver->get_type_scope ().insert ( |
| 238 |
path, type.get_node_id (), type.get_locus (), false, Rib::ItemType::Type, |
| 239 |
[&] (const CanonicalPath &, NodeId, location_t locus) -> void { |
| 240 |
rich_location r (line_table, type.get_locus ()); |
| 241 |
r.add_range (locus); |
| 242 |
redefined_error (r); |
| 243 |
}); |
| 244 |
|
| 245 |
NodeId current_module = resolver->peek_current_module_scope (); |
| 246 |
mappings.insert_module_child_item (current_module, decl); |
| 247 |
} |
| 248 |
|
| 249 |
private: |
| 250 |
ResolveToplevelExternItem (const CanonicalPath &prefix) |
| 251 |
: ResolverBase (), prefix (prefix) |
| 252 |
{} |
| 253 |
|
| 254 |
const CanonicalPath &prefix; |
| 255 |
}; |
| 256 |
|
| 257 |
} // namespace Resolver |
| 258 |
} // namespace Rust |
| 259 |
|
| 260 |
#endif // RUST_AST_RESOLVE_IMPLITEM_H |