diff options
author | Arthur Cohen <arthur.cohen@embecosm.com> | 2023-06-26 14:47:06 +0200 |
---|---|---|
committer | CohenArthur <arthur.cohen@embecosm.com> | 2023-07-10 15:54:56 +0000 |
commit | 3016c44e535fe134d88c03ec3d61e3066e43180c (patch) | |
tree | ede922bdb7f1124def97b0d8788fafa02c85d98d /gcc | |
parent | 1fe255e4928dd3a6d1ab574f92d472b1eea894c2 (diff) | |
download | gcc-3016c44e535fe134d88c03ec3d61e3066e43180c.zip gcc-3016c44e535fe134d88c03ec3d61e3066e43180c.tar.gz gcc-3016c44e535fe134d88c03ec3d61e3066e43180c.tar.bz2 |
gccrs: Remove Rust::Optional in favor of tl::optional
gcc/rust/ChangeLog:
* Make-lang.in: Remove rust-optional-test.cc's object file
* ast/rust-macro.h: Remove use of Rust::Optional
* backend/rust-compile-base.cc
(HIRCompileBase::resolve_method_address): Likewise.
* checks/errors/privacy/rust-privacy-reporter.cc
(PrivacyReporter::check_for_privacy_violation): Likewise.
(PrivacyReporter::visit): Likewise.
* checks/errors/privacy/rust-privacy-reporter.h: Likewise.
* checks/errors/rust-feature-gate.cc (FeatureGate::check): Likewise.
* checks/errors/rust-feature.cc (Feature::create): Likewise.
(Feature::as_name): Likewise.
* checks/errors/rust-feature.h: Likewise.
* expand/rust-macro-builtins.cc: Likewise.
* lex/rust-lex.cc (Lexer::Lexer): Likewise.
(Lexer::skip_token): Likewise.
(Lexer::dump_and_skip): Likewise.
* lex/rust-lex.h: Likewise.
* resolve/rust-ast-resolve-path.cc (ResolvePath::resolve_path): Likewise.
* resolve/rust-ast-resolve-type.cc (ResolveRelativeTypePath::go): Likewise.
* rust-lang.cc (run_rust_tests): Likewise.
* rust-session-manager.cc (Session::compile_crate): Likewise.
(TargetOptions::dump_target_options): Likewise.
* rust-session-manager.h (struct TargetOptions): Likewise.
* util/rust-hir-map.cc (Mappings::lookup_module_children): Likewise.
(Mappings::lookup_module_chidren_items): Likewise.
(Mappings::lookup_module_child): Likewise.
(Mappings::lookup_parent_module): Likewise.
* util/rust-hir-map.h (RUST_HIR_MAP_H): Likewise.
* util/rust-optional-test.cc: Removed.
* util/rust-optional.h: Removed.
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/Make-lang.in | 1 | ||||
-rw-r--r-- | gcc/rust/ast/rust-macro.h | 18 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-base.cc | 2 | ||||
-rw-r--r-- | gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc | 11 | ||||
-rw-r--r-- | gcc/rust/checks/errors/privacy/rust-privacy-reporter.h | 2 | ||||
-rw-r--r-- | gcc/rust/checks/errors/rust-feature-gate.cc | 4 | ||||
-rw-r--r-- | gcc/rust/checks/errors/rust-feature.cc | 22 | ||||
-rw-r--r-- | gcc/rust/checks/errors/rust-feature.h | 8 | ||||
-rw-r--r-- | gcc/rust/expand/rust-macro-builtins.cc | 2 | ||||
-rw-r--r-- | gcc/rust/lex/rust-lex.cc | 8 | ||||
-rw-r--r-- | gcc/rust/lex/rust-lex.h | 7 | ||||
-rw-r--r-- | gcc/rust/resolve/rust-ast-resolve-path.cc | 8 | ||||
-rw-r--r-- | gcc/rust/resolve/rust-ast-resolve-type.cc | 4 | ||||
-rw-r--r-- | gcc/rust/rust-lang.cc | 3 | ||||
-rw-r--r-- | gcc/rust/rust-session-manager.cc | 16 | ||||
-rw-r--r-- | gcc/rust/rust-session-manager.h | 47 | ||||
-rw-r--r-- | gcc/rust/util/rust-hir-map.cc | 33 | ||||
-rw-r--r-- | gcc/rust/util/rust-hir-map.h | 10 | ||||
-rw-r--r-- | gcc/rust/util/rust-optional-test.cc | 110 | ||||
-rw-r--r-- | gcc/rust/util/rust-optional.h | 288 |
20 files changed, 98 insertions, 506 deletions
diff --git a/gcc/rust/Make-lang.in b/gcc/rust/Make-lang.in index b4342dd..5d90863 100644 --- a/gcc/rust/Make-lang.in +++ b/gcc/rust/Make-lang.in @@ -161,7 +161,6 @@ GRS_OBJS = \ rust/rust-compile-pattern.o \ rust/rust-compile-fnparam.o \ rust/rust-base62.o \ - rust/rust-optional-test.o \ rust/rust-compile-item.o \ rust/rust-compile-implitem.o \ rust/rust-compile-stmt.o \ diff --git a/gcc/rust/ast/rust-macro.h b/gcc/rust/ast/rust-macro.h index 554dadf..2609d12 100644 --- a/gcc/rust/ast/rust-macro.h +++ b/gcc/rust/ast/rust-macro.h @@ -622,9 +622,8 @@ public: Location locus, bool is_semi_coloned = false) { return std::unique_ptr<MacroInvocation> ( - new MacroInvocation (InvocKind::Regular, Optional<BuiltinMacro>::none (), - invoc_data, outer_attrs, locus, is_semi_coloned, - {})); + new MacroInvocation (InvocKind::Regular, tl::nullopt, invoc_data, + outer_attrs, locus, is_semi_coloned, {})); } /** @@ -640,9 +639,8 @@ public: bool is_semi_coloned = false) { return std::unique_ptr<MacroInvocation> ( - new MacroInvocation (InvocKind::Builtin, - Optional<BuiltinMacro>::some (kind), invoc_data, - outer_attrs, locus, is_semi_coloned, + new MacroInvocation (InvocKind::Builtin, kind, invoc_data, outer_attrs, + locus, is_semi_coloned, std::move (pending_eager_invocations))); } @@ -685,7 +683,7 @@ public: bool has_semicolon () const { return is_semi_coloned; } InvocKind get_kind () const { return kind; } - Optional<BuiltinMacro> get_builtin_kind () const { return builtin_kind; } + tl::optional<BuiltinMacro> get_builtin_kind () const { return builtin_kind; } /** * Turn the current MacroInvocation into a builtin macro invocation @@ -693,7 +691,7 @@ public: void map_to_builtin (BuiltinMacro macro) { kind = InvocKind::Builtin; - builtin_kind = Optional<BuiltinMacro>::some (macro); + builtin_kind = macro; } /** @@ -711,7 +709,7 @@ public: private: /* Full constructor */ MacroInvocation ( - InvocKind kind, Optional<BuiltinMacro> builtin_kind, + InvocKind kind, tl::optional<BuiltinMacro> builtin_kind, MacroInvocData invoc_data, std::vector<Attribute> outer_attrs, Location locus, bool is_semi_coloned, std::vector<std::unique_ptr<MacroInvocation>> &&pending_eager_invocs) @@ -748,7 +746,7 @@ private: InvocKind kind; /* If it is a builtin macro, which one */ - Optional<BuiltinMacro> builtin_kind = Optional<BuiltinMacro>::none (); + tl::optional<BuiltinMacro> builtin_kind = tl::nullopt; /** * Pending invocations within a builtin macro invocation. This vector is empty diff --git a/gcc/rust/backend/rust-compile-base.cc b/gcc/rust/backend/rust-compile-base.cc index 3e97109..5b618b5 100644 --- a/gcc/rust/backend/rust-compile-base.cc +++ b/gcc/rust/backend/rust-compile-base.cc @@ -816,7 +816,7 @@ HIRCompileBase::resolve_method_address (TyTy::FnType *fntype, rust_assert (ok); // found rust_assert (trait_item_ref->is_optional ()); // has definition - // FIXME Optional means it has a definition and an associated + // FIXME tl::optional means it has a definition and an associated // block which can be a default implementation, if it does not // contain an implementation we should actually return // error_mark_node diff --git a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc index c7dde6b..5932600 100644 --- a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc +++ b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc @@ -28,7 +28,7 @@ PrivacyReporter::PrivacyReporter ( Analysis::Mappings &mappings, Resolver::Resolver &resolver, const Rust::Resolver::TypeCheckContext &ty_ctx) : mappings (mappings), resolver (resolver), ty_ctx (ty_ctx), - current_module (Optional<NodeId>::none ()) + current_module (tl::nullopt) {} void @@ -92,7 +92,7 @@ PrivacyReporter::check_for_privacy_violation (const NodeId &use_id, case ModuleVisibility::Restricted: { // If we are in the crate, everything is restricted correctly, but we // can't get a module for it - if (current_module.is_none ()) + if (!current_module.has_value ()) return; auto module = mappings.lookup_defid (vis.get_module_id ()); @@ -102,12 +102,12 @@ PrivacyReporter::check_for_privacy_violation (const NodeId &use_id, // We are in the module referenced by the pub(restricted) visibility. // This is valid - if (mod_node_id == current_module.get ()) + if (mod_node_id == current_module.value ()) break; // FIXME: This needs a LOT of TLC: hinting about the definition, a // string to say if it's a module, function, type, etc... - if (!is_child_module (mappings, mod_node_id, current_module.get ())) + if (!is_child_module (mappings, mod_node_id, current_module.value ())) valid = false; } break; @@ -584,8 +584,7 @@ PrivacyReporter::visit (HIR::Module &module) // FIXME: We also need to think about module privacy auto old_module = current_module; - current_module - = Optional<NodeId>::some (module.get_mappings ().get_nodeid ()); + current_module = module.get_mappings ().get_nodeid (); for (auto &item : module.get_items ()) item->accept_vis (*this); diff --git a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.h b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.h index 26b8354..19bb71d 100644 --- a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.h +++ b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.h @@ -158,7 +158,7 @@ types const Rust::Resolver::TypeCheckContext &ty_ctx; // `None` means we're in the root module - the crate - Optional<NodeId> current_module; + tl::optional<NodeId> current_module; }; } // namespace Privacy diff --git a/gcc/rust/checks/errors/rust-feature-gate.cc b/gcc/rust/checks/errors/rust-feature-gate.cc index 185afb4..ca7b920 100644 --- a/gcc/rust/checks/errors/rust-feature-gate.cc +++ b/gcc/rust/checks/errors/rust-feature-gate.cc @@ -42,9 +42,9 @@ FeatureGate::check (AST::Crate &crate) { const auto &name_str = item->as_string (); auto tname = Feature::as_name (name_str); - if (!tname.is_none ()) + if (tname.has_value ()) { - auto name = tname.get (); + auto name = tname.value (); valid_features.insert (name); } diff --git a/gcc/rust/checks/errors/rust-feature.cc b/gcc/rust/checks/errors/rust-feature.cc index e36e540..ca31313 100644 --- a/gcc/rust/checks/errors/rust-feature.cc +++ b/gcc/rust/checks/errors/rust-feature.cc @@ -29,24 +29,19 @@ Feature::create (Feature::Name name) case Feature::Name::ASSOCIATED_TYPE_BOUNDS: return Feature (Feature::Name::ASSOCIATED_TYPE_BOUNDS, Feature::State::ACCEPTED, "associated_type_bounds", - "1.34.0", 52662, - Optional<CompileOptions::Edition>::none (), ""); + "1.34.0", 52662, tl::nullopt, ""); case Feature::Name::INTRINSICS: return Feature (Feature::Name::INTRINSICS, Feature::State::ACCEPTED, - "intrinsics", "1.0.0", 0, - Optional<CompileOptions::Edition>::none (), ""); + "intrinsics", "1.0.0", 0, tl::nullopt, ""); case Feature::Name::RUSTC_ATTRS: return Feature (Feature::Name::RUSTC_ATTRS, Feature::State::ACCEPTED, - "rustc_attrs", "1.0.0", 0, - Optional<CompileOptions::Edition>::none (), ""); + "rustc_attrs", "1.0.0", 0, tl::nullopt, ""); case Feature::Name::DECL_MACRO: return Feature (Feature::Name::DECL_MACRO, Feature::State::ACCEPTED, - "decl_macro", "1.0.0", 0, - Optional<CompileOptions::Edition>::none (), ""); + "decl_macro", "1.0.0", 0, tl::nullopt, ""); case Feature::Name::EXTERN_TYPES: return Feature (Feature::Name::EXTERN_TYPES, Feature::State::ACTIVE, - "extern_types", "1.23.0", 43467, - Optional<CompileOptions::Edition>::none (), ""); + "extern_types", "1.23.0", 43467, tl::nullopt, ""); default: gcc_unreachable (); } @@ -63,12 +58,13 @@ const std::map<std::string, Feature::Name> Feature::name_hash_map = { {"extern_types", Feature::Name::EXTERN_TYPES}, }; // namespace Rust -Optional<Feature::Name> +tl::optional<Feature::Name> Feature::as_name (const std::string &name) { if (Feature::name_hash_map.count (name)) - return Optional<Feature::Name>::some (Feature::name_hash_map.at (name)); - return Optional<Feature::Name>::none (); + return Feature::name_hash_map.at (name); + + return tl::nullopt; } } // namespace Rust
\ No newline at end of file diff --git a/gcc/rust/checks/errors/rust-feature.h b/gcc/rust/checks/errors/rust-feature.h index 6f2021f..e613fdf 100644 --- a/gcc/rust/checks/errors/rust-feature.h +++ b/gcc/rust/checks/errors/rust-feature.h @@ -20,7 +20,7 @@ #define RUST_FEATURE_H #include "rust-session-manager.h" -#include "rust-optional.h" +#include "optional.h" namespace Rust { @@ -51,13 +51,13 @@ public: State state () { return m_state; } unsigned issue () { return m_issue; } - static Optional<Name> as_name (const std::string &name); + static tl::optional<Name> as_name (const std::string &name); static Feature create (Name name); private: Feature (Name name, State state, const char *name_str, const char *rustc_since, unsigned issue_number, - const Optional<CompileOptions::Edition> &edition, + const tl::optional<CompileOptions::Edition> &edition, const char *description) : m_state (state), m_name (name), m_name_str (name_str), m_rustc_since (rustc_since), m_issue (issue_number), edition (edition), @@ -69,7 +69,7 @@ private: std::string m_name_str; std::string m_rustc_since; unsigned m_issue; - Optional<CompileOptions::Edition> edition; + tl::optional<CompileOptions::Edition> edition; std::string m_description; static const std::map<std::string, Name> name_hash_map; diff --git a/gcc/rust/expand/rust-macro-builtins.cc b/gcc/rust/expand/rust-macro-builtins.cc index 6b3f3c7..0423e51 100644 --- a/gcc/rust/expand/rust-macro-builtins.cc +++ b/gcc/rust/expand/rust-macro-builtins.cc @@ -123,7 +123,7 @@ std::unordered_map< {"Hash", MacroBuiltin::proc_macro_builtin}, }; -// FIXME: This should return an Optional +// FIXME: This should return an tl::optional BuiltinMacro builtin_macro_from_string (const std::string &identifier) { diff --git a/gcc/rust/lex/rust-lex.cc b/gcc/rust/lex/rust-lex.cc index 04efc53..bc739a6 100644 --- a/gcc/rust/lex/rust-lex.cc +++ b/gcc/rust/lex/rust-lex.cc @@ -138,13 +138,13 @@ is_identifier_continue (uint32_t codepoint) Lexer::Lexer (const std::string &input) : input (RAIIFile::create_error ()), current_line (1), current_column (1), - line_map (nullptr), dump_lex_out (Optional<std::ofstream &>::none ()), + line_map (nullptr), dump_lex_out ({}), raw_input_source (new BufferInputSource (input, 0)), input_queue{*raw_input_source}, token_queue (TokenSource (this)) {} Lexer::Lexer (const char *filename, RAIIFile file_input, Linemap *linemap, - Optional<std::ofstream &> dump_lex_opt) + tl::optional<std::ofstream &> dump_lex_opt) : input (std::move (file_input)), current_line (1), current_column (1), line_map (linemap), dump_lex_out (dump_lex_opt), raw_input_source (new FileInputSource (input.get_raw ())), @@ -218,7 +218,7 @@ void Lexer::skip_token (int n) { // dump tokens if dump-lex option is enabled - if (dump_lex_out.is_some ()) + if (dump_lex_out.has_value ()) dump_and_skip (n); else token_queue.skip (n); @@ -227,7 +227,7 @@ Lexer::skip_token (int n) void Lexer::dump_and_skip (int n) { - std::ofstream &out = dump_lex_out.get (); + std::ofstream &out = dump_lex_out.value (); bool found_eof = false; const_TokenPtr tok; for (int i = 0; i < n + 1; i++) diff --git a/gcc/rust/lex/rust-lex.h b/gcc/rust/lex/rust-lex.h index 2034fd7..046a6e5 100644 --- a/gcc/rust/lex/rust-lex.h +++ b/gcc/rust/lex/rust-lex.h @@ -22,7 +22,7 @@ #include "rust-linemap.h" #include "rust-buffered-queue.h" #include "rust-token.h" -#include "rust-optional.h" +#include "optional.h" #include "selftest.h" namespace Rust { @@ -158,8 +158,7 @@ private: public: // Construct lexer with input file and filename provided Lexer (const char *filename, RAIIFile input, Linemap *linemap, - Optional<std::ofstream &> dump_lex_opt - = Optional<std::ofstream &>::none ()); + tl::optional<std::ofstream &> dump_lex_opt = tl::nullopt); // Lex the contents of a string instead of a file Lexer (const std::string &input); @@ -397,7 +396,7 @@ private: * allocating new linemap */ static const int max_column_hint = 80; - Optional<std::ofstream &> dump_lex_out; + tl::optional<std::ofstream &> dump_lex_out; // The input source for the lexer. // InputSource input_source; diff --git a/gcc/rust/resolve/rust-ast-resolve-path.cc b/gcc/rust/resolve/rust-ast-resolve-path.cc index 1b0edcc..0e23ef1 100644 --- a/gcc/rust/resolve/rust-ast-resolve-path.cc +++ b/gcc/rust/resolve/rust-ast-resolve-path.cc @@ -164,10 +164,10 @@ ResolvePath::resolve_path (AST::PathInExpression *expr) if (resolved_node_id == UNKNOWN_NODEID && previous_resolved_node_id == module_scope_id) { - Optional<CanonicalPath &> resolved_child + tl::optional<CanonicalPath &> resolved_child = mappings->lookup_module_child (module_scope_id, ident_seg.as_string ()); - if (resolved_child.is_some ()) + if (resolved_child.has_value ()) { NodeId resolved_node = resolved_child->get_node_id (); if (resolver->get_name_scope ().decl_was_declared_here ( @@ -303,10 +303,10 @@ ResolvePath::resolve_path (AST::SimplePath *expr) continue; } - Optional<CanonicalPath &> resolved_child + tl::optional<CanonicalPath &> resolved_child = mappings->lookup_module_child (module_scope_id, segment.get_segment_name ()); - if (resolved_child.is_some ()) + if (resolved_child.has_value ()) { NodeId resolved_node = resolved_child->get_node_id (); if (resolver->get_name_scope ().decl_was_declared_here ( diff --git a/gcc/rust/resolve/rust-ast-resolve-type.cc b/gcc/rust/resolve/rust-ast-resolve-type.cc index 6ffb36d..34d45a9 100644 --- a/gcc/rust/resolve/rust-ast-resolve-type.cc +++ b/gcc/rust/resolve/rust-ast-resolve-type.cc @@ -198,10 +198,10 @@ ResolveRelativeTypePath::go (AST::TypePath &path, NodeId &resolved_node_id) if (resolved_node_id == UNKNOWN_NODEID && previous_resolved_node_id == module_scope_id) { - Optional<CanonicalPath &> resolved_child + tl::optional<CanonicalPath &> resolved_child = mappings->lookup_module_child (module_scope_id, ident_seg.as_string ()); - if (resolved_child.is_some ()) + if (resolved_child.has_value ()) { NodeId resolved_node = resolved_child->get_node_id (); if (resolver->get_name_scope ().decl_was_declared_here ( diff --git a/gcc/rust/rust-lang.cc b/gcc/rust/rust-lang.cc index 9d05bc8..4bf82d0 100644 --- a/gcc/rust/rust-lang.cc +++ b/gcc/rust/rust-lang.cc @@ -37,8 +37,8 @@ #include "rust-cfg-parser.h" #include "rust-privacy-ctx.h" #include "rust-ast-resolve-item.h" -#include "rust-optional.h" #include "rust-lex.h" +#include "optional.h" #include <mpfr.h> // note: header files must be in this order or else forward declarations don't @@ -458,7 +458,6 @@ run_rust_tests () rust_privacy_ctx_test (); rust_crate_name_validation_test (); rust_simple_path_resolve_test (); - rust_optional_test (); } } // namespace selftest diff --git a/gcc/rust/rust-session-manager.cc b/gcc/rust/rust-session-manager.cc index c27b456..8344bca 100644 --- a/gcc/rust/rust-session-manager.cc +++ b/gcc/rust/rust-session-manager.cc @@ -481,18 +481,16 @@ Session::compile_crate (const char *filename) // parse file here /* create lexer and parser - these are file-specific and so aren't instance * variables */ - Optional<std::ofstream &> dump_lex_opt = Optional<std::ofstream &>::none (); + tl::optional<std::ofstream &> dump_lex_opt = tl::nullopt; std::ofstream dump_lex_stream; if (options.dump_option_enabled (CompileOptions::LEXER_DUMP)) { dump_lex_stream.open (kLexDumpFile); if (dump_lex_stream.fail ()) - { - rust_error_at (UNKNOWN_LOCATION, "cannot open %s:%m; ignored", - kLexDumpFile); - } - auto stream = Optional<std::ofstream &>::some (dump_lex_stream); - dump_lex_opt = std::move (stream); + rust_error_at (UNKNOWN_LOCATION, "cannot open %s:%m; ignored", + kLexDumpFile); + + dump_lex_opt = dump_lex_stream; } Lexer lex (filename, std::move (file_wrap), linemap, dump_lex_opt); @@ -1051,8 +1049,8 @@ TargetOptions::dump_target_options () const { for (const auto &value : pairs.second) { - if (value.is_some ()) - out << pairs.first + ": \"" + value.get () + "\"\n"; + if (value.has_value ()) + out << pairs.first + ": \"" + value.value () + "\"\n"; else out << pairs.first + "\n"; } diff --git a/gcc/rust/rust-session-manager.h b/gcc/rust/rust-session-manager.h index fd62c42..85f003a 100644 --- a/gcc/rust/rust-session-manager.h +++ b/gcc/rust/rust-session-manager.h @@ -30,7 +30,7 @@ #include "coretypes.h" #include "options.h" -#include "rust-optional.h" +#include "optional.h" namespace Rust { // parser forward decl @@ -51,7 +51,7 @@ struct TargetOptions { /* TODO: maybe make private and access through helpers to allow changes to * impl */ - std::unordered_map<std::string, std::unordered_set<Optional<std::string>>> + std::unordered_map<std::string, std::unordered_set<tl::optional<std::string>>> features; public: @@ -60,8 +60,7 @@ public: { auto it = features.find (key); return it != features.end () - && it->second.find (Optional<std::string>::none ()) - != it->second.end (); + && it->second.find (tl::nullopt) != it->second.end (); } // Returns whether a key exists with the given value in the feature set. @@ -71,7 +70,7 @@ public: if (it != features.end ()) { auto set = it->second; - auto it2 = set.find (Optional<std::string>::some (value)); + auto it2 = set.find (value); if (it2 != set.end ()) return true; } @@ -86,8 +85,8 @@ public: if (it != features.end ()) { auto set = it->second; - if (set.size () == 1 && set.begin ()->is_some ()) - return set.begin ()->get (); + if (set.size () == 1 && set.begin ()->has_value ()) + return set.begin ()->value (); } return ""; } @@ -103,8 +102,8 @@ public: return {}; for (auto &val : it->second) - if (val.is_some ()) - ret.insert (val.get ()); + if (val.has_value ()) + ret.insert (val.value ()); return ret; } @@ -117,13 +116,14 @@ public: auto it = features.find (key); if (it == features.end ()) - it = features - .insert ( - std::make_pair (std::move (key), - std::unordered_set<Optional<std::string>> ())) - .first; - - return it->second.insert (Optional<std::string>::none ()).second; + it + = features + .insert ( + std::make_pair (std::move (key), + std::unordered_set<tl::optional<std::string>> ())) + .first; + + return it->second.insert (tl::nullopt).second; } // Inserts a key-value pair into the feature set. @@ -132,13 +132,14 @@ public: auto it = features.find (key); if (it == features.end ()) - it = features - .insert ( - std::make_pair (std::move (key), - std::unordered_set<Optional<std::string>> ())) - .first; - - it->second.insert (Optional<std::string>::some (std::move (value))); + it + = features + .insert ( + std::make_pair (std::move (key), + std::unordered_set<tl::optional<std::string>> ())) + .first; + + it->second.insert (std::move (value)); } // Dump all target options to stderr. diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc index 783e899..793d547 100644 --- a/gcc/rust/util/rust-hir-map.cc +++ b/gcc/rust/util/rust-hir-map.cc @@ -1035,14 +1035,14 @@ Mappings::insert_module_child (NodeId module, NodeId child) it->second.emplace_back (child); } -Optional<std::vector<NodeId> &> +tl::optional<std::vector<NodeId> &> Mappings::lookup_module_children (NodeId module) { auto it = module_child_map.find (module); if (it == module_child_map.end ()) - return Optional<std::vector<NodeId> &>::none (); + return tl::nullopt; - return Optional<std::vector<NodeId> &>::some (it->second); + return it->second; } void @@ -1059,33 +1059,34 @@ Mappings::insert_module_child_item (NodeId module, it->second.emplace_back (child); } -Optional<std::vector<Resolver::CanonicalPath> &> +tl::optional<std::vector<Resolver::CanonicalPath> &> Mappings::lookup_module_chidren_items (NodeId module) { auto it = module_child_items.find (module); if (it == module_child_items.end ()) - return Optional<std::vector<Resolver::CanonicalPath> &>::none (); + return tl::nullopt; - return Optional<std::vector<Resolver::CanonicalPath> &>::some (it->second); + return it->second; } -Optional<Resolver::CanonicalPath &> +tl::optional<Resolver::CanonicalPath &> Mappings::lookup_module_child (NodeId module, const std::string &item_name) { - Optional<std::vector<Resolver::CanonicalPath> &> children + tl::optional<std::vector<Resolver::CanonicalPath> &> children = lookup_module_chidren_items (module); - if (children.is_none ()) - return Optional<Resolver::CanonicalPath &>::none (); + if (!children.has_value ()) + return tl::nullopt; // lookup the children to match the name if we can - for (auto &child : children.get ()) + for (auto &child : children.value ()) { const std::string &raw_identifier = child.get (); bool found = raw_identifier.compare (item_name) == 0; if (found) - return Optional<Resolver::CanonicalPath &>::some (child); + return child; } - return Optional<Resolver::CanonicalPath &>::none (); + + return tl::nullopt; } void @@ -1095,14 +1096,14 @@ Mappings::insert_child_item_to_parent_module_mapping (NodeId child_item, child_to_parent_module_map.insert ({child_item, parent_module}); } -Optional<NodeId> +tl::optional<NodeId> Mappings::lookup_parent_module (NodeId child_item) { auto it = child_to_parent_module_map.find (child_item); if (it == child_to_parent_module_map.end ()) - return Optional<NodeId>::none (); + return tl::nullopt; - return Optional<NodeId>::some (it->second); + return it->second; } bool diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h index 964b626..495bca2 100644 --- a/gcc/rust/util/rust-hir-map.h +++ b/gcc/rust/util/rust-hir-map.h @@ -19,7 +19,7 @@ #ifndef RUST_HIR_MAP_H #define RUST_HIR_MAP_H -#include "rust-optional.h" +#include "optional.h" #include "rust-system.h" #include "rust-location.h" #include "rust-mapping-common.h" @@ -303,17 +303,17 @@ public: bool lookup_visibility (NodeId id, Privacy::ModuleVisibility &def); void insert_module_child (NodeId module, NodeId child); - Optional<std::vector<NodeId> &> lookup_module_children (NodeId module); + tl::optional<std::vector<NodeId> &> lookup_module_children (NodeId module); void insert_module_child_item (NodeId module, Resolver::CanonicalPath item); - Optional<std::vector<Resolver::CanonicalPath> &> + tl::optional<std::vector<Resolver::CanonicalPath> &> lookup_module_chidren_items (NodeId module); - Optional<Resolver::CanonicalPath &> + tl::optional<Resolver::CanonicalPath &> lookup_module_child (NodeId module, const std::string &item_name); void insert_child_item_to_parent_module_mapping (NodeId child_item, NodeId parent_module); - Optional<NodeId> lookup_parent_module (NodeId child_item); + tl::optional<NodeId> lookup_parent_module (NodeId child_item); bool node_is_module (NodeId query); void insert_ast_item (AST::Item *item); diff --git a/gcc/rust/util/rust-optional-test.cc b/gcc/rust/util/rust-optional-test.cc deleted file mode 100644 index 9fbbe7d..0000000 --- a/gcc/rust/util/rust-optional-test.cc +++ /dev/null @@ -1,110 +0,0 @@ -// Copyright (C) 2020-2023 Free Software Foundation, Inc. - -// This file is part of GCC. - -// GCC is free software; you can redistribute it and/or modify it under -// the terms of the GNU General Public License as published by the Free -// Software Foundation; either version 3, or (at your option) any later -// version. - -// GCC is distributed in the hope that it will be useful, but WITHOUT ANY -// WARRANTY; without even the implied warranty of MERCHANTABILITY or -// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -// for more details. - -// You should have received a copy of the GNU General Public License -// along with GCC; see the file COPYING3. If not see -// <http://www.gnu.org/licenses/>. - -#include "rust-system.h" -#include "rust-optional.h" -#include "selftest.h" - -#if CHECKING_P - -static void -rust_optional_create () -{ - auto opt = Rust::Optional<int>::some (15); - - ASSERT_TRUE (opt.is_some ()); - ASSERT_EQ (opt.get (), 15); - - Rust::Optional<int> const_opt = Rust::Optional<int>::some (15); - const int &value = const_opt.get (); - - ASSERT_EQ (value, 15); -} - -static void -rust_optional_operators () -{ - auto opt = Rust::Optional<int>::some (15); - - // as bool - ASSERT_TRUE (opt); - - // deref - ASSERT_EQ (*opt, 15); - - class Methodable - { - public: - int method () { return 15; } - }; - - auto m_opt = Rust::Optional<Methodable>::some (Methodable ()); - ASSERT_EQ (m_opt->method (), 15); -} - -static void -rust_optional_take () -{ - auto opt = Rust::Optional<int>::some (15); - auto value = opt.take (); - - ASSERT_EQ (value, 15); - ASSERT_TRUE (opt.is_none ()); -} - -static void -rust_optional_map () -{ - auto opt = Rust::Optional<int>::some (15); - auto twice = opt.map<int> ([] (int value) { return value * 2; }); - - ASSERT_FALSE (opt); - ASSERT_TRUE (twice); - ASSERT_EQ (*twice, 30); -} - -static void -rust_optional_reference () -{ - auto value = std::vector<std::string> (); - value.emplace_back ("rust"); - value.emplace_back ("+"); - value.emplace_back ("gcc"); - value.emplace_back ("="); - value.emplace_back ("<3"); - - auto opt = Rust::Optional<std::vector<std::string> &>::some (value); - - ASSERT_EQ (opt->at (0), "rust"); - ASSERT_EQ (opt->at (2), "gcc"); -} - -#endif /* #if CHECKING_P */ - -void -rust_optional_test () -{ -#if CHECKING_P - rust_optional_create (); - rust_optional_operators (); - rust_optional_take (); - rust_optional_map (); - rust_optional_reference (); - -#endif /* #if CHECKING_P */ -} diff --git a/gcc/rust/util/rust-optional.h b/gcc/rust/util/rust-optional.h deleted file mode 100644 index bdfbd28..0000000 --- a/gcc/rust/util/rust-optional.h +++ /dev/null @@ -1,288 +0,0 @@ -// Copyright (C) 2020-2023 Free Software Foundation, Inc. - -// This file is part of GCC. - -// GCC is free software; you can redistribute it and/or modify it under -// the terms of the GNU General Public License as published by the Free -// Software Foundation; either version 3, or (at your option) any later -// version. - -// GCC is distributed in the hope that it will be useful, but WITHOUT ANY -// WARRANTY; without even the implied warranty of MERCHANTABILITY or -// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -// for more details. - -// You should have received a copy of the GNU General Public License -// along with GCC; see the file COPYING3. If not see -// <http://www.gnu.org/licenses/>. - -#ifndef RUST_OPTIONAL_H -#define RUST_OPTIONAL_H - -#include "config.h" -#include "rust-system.h" - -#include "selftest.h" - -namespace Rust { - -/** - * Tagged union to try and simulate a sum type. This is safer and more ergonomic - * than one of the two alternatives we're currently using in the compiler: - * - * 1. Storing a raw pointer, which can be `nullptr` or valid - * - * This is wildly unsafe, and usable in conjunction with local references, stack - * variables, or pointers managed elsewhere, which can cause crashes, hard to - * debug issues or undefined behavior. Likewise, if you do not check for the - * pointer's validity, this will cause a crash. - * - * 2. Storing an extra boolean alongside the object - * - * This causes implementors to use a "dummy object": Either an empty version or - * an error version. But what happens if what you really wanted to store was - * the empty or error version? You can also easily incorporate logic bugs if you - * forget to check for the associated boolean. - * - * The `Optional<T>` type has the same "ergonomic" cost: You need to check - * whether your option is valid or not. However, the main advantage is that it - * is more restrictive: You can only acess the member it contains "safely". - * It is similar to storing a value + an associated boolean, but has the - * advantage of making up only one member in your class. - * You also benefit from some helper methods such as `map()`. - * - * You also get helper functions and operator overloading to "seamlessly" - * replace raw pointer alternatives. - * - * ```c++ - * MyType *raw_pointer = something_that_can_fail(); - * if (raw_pointer) - * raw_pointer->method(); - * - * // or - * - * Optional<MyType> opt = something_that_can_fail2(); - * if (opt) - * opt->method(); - * - * // equivalent to - * - * if (opt.is_some()) - * opt.get().method(); - * ``` - */ -template <typename T> class Optional -{ -private: - struct tag_some - { - }; - struct tag_none - { - }; - - bool field_is_some; - - union - { - T value; - // prevents initialization warnings - // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80635#c53 - // FIXME: remove - volatile char unused; - }; - - Optional (tag_some, const T &value) : field_is_some (true), value (value) {} - Optional (tag_some, T &&value) : field_is_some (true), value (value) {} - - Optional (tag_none) : field_is_some (false) {} - -public: - Optional (const Optional &other) - { - if ((field_is_some = other.field_is_some)) - new (&value) T (other.value); - } - - Optional (Optional &&other) - { - if ((field_is_some = other.field_is_some)) - new (&value) T (other.value); - } - - Optional &operator= (const Optional &other) - { - if (is_some ()) - value.~T (); - if ((field_is_some = other.field_is_some)) - new (&value) T (other.value); - return *this; - } - - Optional &operator= (Optional &&other) - { - if (is_some ()) - value.~T (); - if ((field_is_some = other.field_is_some)) - new (&value) T (other.value); - return *this; - } - - ~Optional () - { - if (is_some ()) - value.~T (); - } - - static Optional some (const T &value) - { - return Optional (tag_some (), value); - } - - static Optional some (T &&value) { return Optional (tag_some (), value); } - - static Optional none () { return Optional (tag_none ()); } - - bool is_some () const { return field_is_some; } - bool is_none () const { return !is_some (); } - - /** - * Enable boolean-like comparisons. - */ - operator bool () { return is_some (); } - - /** - * Enables dereferencing to access the contained value - */ - T &operator* () { return get (); } - const T &operator* () const { return get (); } - T *operator-> () { return &get (); } - const T *operator-> () const { return &get (); } - - const T &get () const - { - rust_assert (is_some ()); - - return value; - } - - T &get () - { - rust_assert (is_some ()); - - return value; - } - - T take () - { - rust_assert (is_some ()); - - T to_return = std::move (value); - value.~T (); - - field_is_some = false; - - return to_return; - } - - template <typename U> Optional<U> map (std::function<U (T)> functor) - { - if (is_none ()) - return Optional::none (); - - auto value = functor (take ()); - - return Optional::some (value); - } -}; - -template <typename T> class Optional<T &> -{ -private: - T *inner; - - Optional (T *inner) : inner (inner) {} - -public: - static Optional<T &> some (T &value) { return Optional (&value); } - - static Optional<T &> none () { return Optional (nullptr); } - - bool is_some () const { return inner; } - bool is_none () const { return !is_some (); } - - // FIXME: Can we factor this in a single class? - - /** - * Enable boolean-like comparisons. - */ - operator bool () { return is_some (); } - - /** - * Enables dereferencing to access the contained value - */ - T &operator* () { return get (); } - const T &operator* () const { return get (); } - T *operator-> () { return &get (); } - const T *operator-> () const { return &get (); } - - T &get () const - { - rust_assert (is_some ()); - - return *inner; - } - - T &take () - { - rust_assert (is_some ()); - - T *to_return = inner; - inner = nullptr; - - return *to_return; - } - - template <typename U> Optional<U &> map (std::function<U &(T &)> functor) - { - if (is_none ()) - return Optional::none (); - - auto value = functor (take ()); - - return Optional::some (value); - } -}; - -template <typename T, typename U> -bool -operator== (const Optional<T> &t, const Optional<U> &u) -{ - if (t.is_some ()) - return u.is_some () && t.get () == u.get (); - else - return u.is_none (); -} - -} // namespace Rust - -namespace std { - -template <typename T> struct hash<Rust::Optional<T>> -{ - size_t operator() (const Rust::Optional<T> &op) const - { - return op.is_some () ? std::hash<T> () (op.get ()) : 0; - } -}; - -} // namespace std - -#ifdef CHECKING_P - -void -rust_optional_test (); - -#endif // !CHECKING_P - -#endif // !RUST_OPTIONAL_H |