aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>2024-04-25 10:48:47 +0200
committerArthur Cohen <arthur.cohen@embecosm.com>2025-03-17 16:35:23 +0100
commit2d9d7036f3bc831f34972cf243f8012d45da0ff0 (patch)
treef8bb517bc3d2c48067e1a5953d7e179443c6cdf2 /gcc
parent97f03ba35810fcbb3a4e05128d323ba655d74acf (diff)
downloadgcc-2d9d7036f3bc831f34972cf243f8012d45da0ff0.zip
gcc-2d9d7036f3bc831f34972cf243f8012d45da0ff0.tar.gz
gcc-2d9d7036f3bc831f34972cf243f8012d45da0ff0.tar.bz2
gccrs: Change return type for lookup_hir_item to optional
gcc/rust/ChangeLog: * backend/rust-compile-resolve-path.cc (HIRCompileBase::query_compile): Adapt function call to new return type. * backend/rust-mangle-v0.cc (v0_path): Likewise. * checks/errors/rust-const-checker.cc (ConstChecker::check_function_call): Likewise. * checks/errors/rust-unsafe-checker.cc (UnsafeChecker::check_use_of_static): Likewise. (UnsafeChecker::check_function_call): Likewise. (UnsafeChecker::check_function_attr): Likewise. * checks/lints/rust-lint-marklive.cc (MarkLive::go): Likewise. (MarkLive::visit): Likewise. * typecheck/rust-hir-trait-resolve.cc (TraitResolver::resolve_path_to_trait): Likewise. * typecheck/rust-type-util.cc (query_type): Likewise. * util/rust-hir-map.cc (Mappings::insert_hir_item): Likewise. (Mappings::lookup_hir_item): Change function return type to use optional. * util/rust-hir-map.h: Update function prototype. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/backend/rust-compile-resolve-path.cc8
-rw-r--r--gcc/rust/backend/rust-mangle-v0.cc9
-rw-r--r--gcc/rust/checks/errors/rust-const-checker.cc5
-rw-r--r--gcc/rust/checks/errors/rust-unsafe-checker.cc16
-rw-r--r--gcc/rust/checks/lints/rust-lint-marklive.cc11
-rw-r--r--gcc/rust/typecheck/rust-hir-trait-resolve.cc9
-rw-r--r--gcc/rust/typecheck/rust-type-util.cc8
-rw-r--r--gcc/rust/util/rust-hir-map.cc7
-rw-r--r--gcc/rust/util/rust-hir-map.h2
9 files changed, 36 insertions, 39 deletions
diff --git a/gcc/rust/backend/rust-compile-resolve-path.cc b/gcc/rust/backend/rust-compile-resolve-path.cc
index 48c8d4e..1f7dc7d 100644
--- a/gcc/rust/backend/rust-compile-resolve-path.cc
+++ b/gcc/rust/backend/rust-compile-resolve-path.cc
@@ -201,20 +201,18 @@ HIRCompileBase::query_compile (HirId ref, TyTy::BaseType *lookup,
const Analysis::NodeMapping &mappings,
location_t expr_locus, bool is_qualified_path)
{
- HIR::Item *resolved_item = ctx->get_mappings ().lookup_hir_item (ref);
HirId parent_block;
HIR::ExternalItem *resolved_extern_item
= ctx->get_mappings ().lookup_hir_extern_item (ref, &parent_block);
- bool is_hir_item = resolved_item != nullptr;
bool is_hir_extern_item = resolved_extern_item != nullptr;
bool is_fn = lookup->get_kind () == TyTy::TypeKind::FNDEF;
- if (is_hir_item)
+ if (auto resolved_item = ctx->get_mappings ().lookup_hir_item (ref))
{
if (!lookup->has_substitutions_defined ())
- return CompileItem::compile (resolved_item, ctx, nullptr, true,
+ return CompileItem::compile (*resolved_item, ctx, nullptr, true,
expr_locus);
else
- return CompileItem::compile (resolved_item, ctx, lookup, true,
+ return CompileItem::compile (*resolved_item, ctx, lookup, true,
expr_locus);
}
else if (is_hir_extern_item)
diff --git a/gcc/rust/backend/rust-mangle-v0.cc b/gcc/rust/backend/rust-mangle-v0.cc
index b2f11b1..9be7b2d 100644
--- a/gcc/rust/backend/rust-mangle-v0.cc
+++ b/gcc/rust/backend/rust-mangle-v0.cc
@@ -387,7 +387,6 @@ v0_path (Rust::Compile::Context *ctx, const TyTy::BaseType *ty,
HIR::ImplItem *impl_item
= mappings.lookup_hir_implitem (hir_id, &parent_impl_id);
HIR::TraitItem *trait_item = mappings.lookup_hir_trait_item (hir_id);
- HIR::Item *item = mappings.lookup_hir_item (hir_id);
HIR::Expr *expr = mappings.lookup_hir_expr (hir_id);
if (impl_item != nullptr)
@@ -428,11 +427,11 @@ v0_path (Rust::Compile::Context *ctx, const TyTy::BaseType *ty,
break;
}
}
- else if (item != nullptr)
- switch (item->get_item_kind ())
+ else if (auto item = mappings.lookup_hir_item (hir_id))
+ switch (item.value ()->get_item_kind ())
{
case HIR::Item::ItemKind::Function: {
- HIR::Function *fn = static_cast<HIR::Function *> (item);
+ HIR::Function *fn = static_cast<HIR::Function *> (*item);
v0path = v0_function_path (v0path, ctx, ty, fn,
v0_identifier (seg.get ()));
}
@@ -453,7 +452,7 @@ v0_path (Rust::Compile::Context *ctx, const TyTy::BaseType *ty,
case HIR::Item::ItemKind::Impl:
// Trait impl or inherent impl.
{
- HIR::ImplBlock *impl_block = static_cast<HIR::ImplBlock *> (item);
+ HIR::ImplBlock *impl_block = static_cast<HIR::ImplBlock *> (*item);
v0path = v0_inherent_or_trait_impl_path (ctx, impl_block);
}
break;
diff --git a/gcc/rust/checks/errors/rust-const-checker.cc b/gcc/rust/checks/errors/rust-const-checker.cc
index 0a99239..1bb2c37 100644
--- a/gcc/rust/checks/errors/rust-const-checker.cc
+++ b/gcc/rust/checks/errors/rust-const-checker.cc
@@ -308,7 +308,8 @@ ConstChecker::check_function_call (HirId fn_id, location_t locus)
return;
auto maybe_fn = mappings.lookup_hir_item (fn_id);
- if (maybe_fn && maybe_fn->get_item_kind () != Item::ItemKind::Function)
+ if (maybe_fn
+ && maybe_fn.value ()->get_item_kind () != Item::ItemKind::Function)
return;
// There are const extern functions (intrinsics)
@@ -325,7 +326,7 @@ ConstChecker::check_function_call (HirId fn_id, location_t locus)
auto is_error = false;
if (maybe_fn)
{
- auto fn = static_cast<Function *> (maybe_fn);
+ auto fn = static_cast<Function *> (*maybe_fn);
if (!fn->get_qualifiers ().is_const ())
is_error = true;
}
diff --git a/gcc/rust/checks/errors/rust-unsafe-checker.cc b/gcc/rust/checks/errors/rust-unsafe-checker.cc
index 92cf476..18e79e3 100644
--- a/gcc/rust/checks/errors/rust-unsafe-checker.cc
+++ b/gcc/rust/checks/errors/rust-unsafe-checker.cc
@@ -70,14 +70,12 @@ UnsafeChecker::check_use_of_static (HirId node_id, location_t locus)
if (unsafe_context.is_in_context ())
return;
- auto maybe_static_mut = mappings.lookup_hir_item (node_id);
-
HirId extern_block;
auto maybe_extern_static
= mappings.lookup_hir_extern_item (node_id, &extern_block);
- if (maybe_static_mut)
- check_static_mut (maybe_static_mut, locus);
+ if (auto maybe_static_mut = mappings.lookup_hir_item (node_id))
+ check_static_mut (*maybe_static_mut, locus);
if (maybe_extern_static)
check_extern_static (static_cast<ExternalItem *> (maybe_extern_static),
@@ -173,8 +171,9 @@ UnsafeChecker::check_function_call (HirId node_id, location_t locus)
auto maybe_extern
= mappings.lookup_hir_extern_item (node_id, &parent_extern_block);
- if (maybe_fn && maybe_fn->get_item_kind () == Item::ItemKind::Function)
- check_unsafe_call (static_cast<Function *> (maybe_fn), locus, "function");
+ if (maybe_fn
+ && maybe_fn.value ()->get_item_kind () == Item::ItemKind::Function)
+ check_unsafe_call (static_cast<Function *> (*maybe_fn), locus, "function");
if (maybe_extern)
check_extern_call (static_cast<ExternalItem *> (maybe_extern),
@@ -204,8 +203,9 @@ UnsafeChecker::check_function_attr (HirId node_id, location_t locus)
auto maybe_fn = mappings.lookup_hir_item (node_id);
- if (maybe_fn && maybe_fn->get_item_kind () == Item::ItemKind::Function)
- check_target_attr (static_cast<Function *> (maybe_fn), locus);
+ if (maybe_fn
+ && maybe_fn.value ()->get_item_kind () == Item::ItemKind::Function)
+ check_target_attr (static_cast<Function *> (*maybe_fn), locus);
}
void
diff --git a/gcc/rust/checks/lints/rust-lint-marklive.cc b/gcc/rust/checks/lints/rust-lint-marklive.cc
index 815fcf5..f64e8b5 100644
--- a/gcc/rust/checks/lints/rust-lint-marklive.cc
+++ b/gcc/rust/checks/lints/rust-lint-marklive.cc
@@ -86,11 +86,10 @@ MarkLive::go (HIR::Crate &)
HirId hirId = worklist.back ();
worklist.pop_back ();
scannedSymbols.emplace (hirId);
- HIR::Item *item = mappings.lookup_hir_item (hirId);
liveSymbols.emplace (hirId);
- if (item != nullptr)
+ if (auto item = mappings.lookup_hir_item (hirId))
{
- item->accept_vis (*this);
+ item.value ()->accept_vis (*this);
}
else
{ // the item maybe inside a trait impl
@@ -124,10 +123,10 @@ MarkLive::visit (HIR::PathInExpression &expr)
auto ref = hid.value ();
// it must resolve to some kind of HIR::Item or HIR::InheritImplItem
- HIR::Item *resolved_item = mappings.lookup_hir_item (ref);
- if (resolved_item != nullptr)
+ tl::optional<HIR::Item *> resolved_item = mappings.lookup_hir_item (ref);
+ if (resolved_item)
{
- mark_hir_id (resolved_item->get_mappings ().get_hirid ());
+ mark_hir_id (resolved_item.value ()->get_mappings ().get_hirid ());
}
else
{
diff --git a/gcc/rust/typecheck/rust-hir-trait-resolve.cc b/gcc/rust/typecheck/rust-hir-trait-resolve.cc
index b8075ee..51a6417 100644
--- a/gcc/rust/typecheck/rust-hir-trait-resolve.cc
+++ b/gcc/rust/typecheck/rust-hir-trait-resolve.cc
@@ -119,11 +119,12 @@ TraitResolver::resolve_path_to_trait (const HIR::TypePath &path,
if (auto hid = mappings.lookup_node_to_hir (ref))
{
- HIR::Item *resolved_item = mappings.lookup_hir_item (hid.value ());
- rust_assert (resolved_item != nullptr);
- rust_assert (resolved_item->get_item_kind ()
+ tl::optional<HIR::Item *> resolved_item
+ = mappings.lookup_hir_item (hid.value ());
+ rust_assert (resolved_item.has_value ());
+ rust_assert (resolved_item.value ()->get_item_kind ()
== HIR::Item::ItemKind::Trait);
- *resolved = static_cast<HIR::Trait *> (resolved_item);
+ *resolved = static_cast<HIR::Trait *> (*resolved_item);
return true;
}
diff --git a/gcc/rust/typecheck/rust-type-util.cc b/gcc/rust/typecheck/rust-type-util.cc
index 914e40e..f6f8c8e 100644
--- a/gcc/rust/typecheck/rust-type-util.cc
+++ b/gcc/rust/typecheck/rust-type-util.cc
@@ -61,11 +61,11 @@ query_type (HirId reference, TyTy::BaseType **result)
return true;
}
- HIR::Item *item = mappings.lookup_hir_item (reference);
- if (item != nullptr)
+ if (auto item = mappings.lookup_hir_item (reference))
{
- rust_debug_loc (item->get_locus (), "resolved item {%u} to", reference);
- *result = TypeCheckItem::Resolve (*item);
+ rust_debug_loc (item.value ()->get_locus (), "resolved item {%u} to",
+ reference);
+ *result = TypeCheckItem::Resolve (*item.value ());
context->query_completed (reference);
return true;
}
diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index 57a7667..535353b 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -366,19 +366,18 @@ void
Mappings::insert_hir_item (HIR::Item *item)
{
auto id = item->get_mappings ().get_hirid ();
- rust_assert (lookup_hir_item (id) == nullptr);
+ rust_assert (!lookup_hir_item (id).has_value ());
hirItemMappings[id] = item;
insert_node_to_hir (item->get_mappings ().get_nodeid (), id);
}
-HIR::Item *
+tl::optional<HIR::Item *>
Mappings::lookup_hir_item (HirId id)
{
auto it = hirItemMappings.find (id);
if (it == hirItemMappings.end ())
- return nullptr;
-
+ return tl::nullopt;
return it->second;
}
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index 6ed3bee..e6df05b 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -115,7 +115,7 @@ public:
HIR::Item *lookup_local_defid (CrateNum crateNum, LocalDefId id);
void insert_hir_item (HIR::Item *item);
- HIR::Item *lookup_hir_item (HirId id);
+ tl::optional<HIR::Item *> lookup_hir_item (HirId id);
void insert_hir_enumitem (HIR::Enum *parent, HIR::EnumItem *item);
std::pair<HIR::Enum *, HIR::EnumItem *> lookup_hir_enumitem (HirId id);