Move the rest of rs_bindings_from_cc/util into crubit/common. PiperOrigin-RevId: 440432415
diff --git a/rs_bindings_from_cc/BUILD b/rs_bindings_from_cc/BUILD index e80de03..9a9546d 100644 --- a/rs_bindings_from_cc/BUILD +++ b/rs_bindings_from_cc/BUILD
@@ -69,7 +69,7 @@ ":ir_from_cc", ":src_code_gen", "//common:file_io", - "//rs_bindings_from_cc/util:status_macros", + "//common:status_macros", "@absl//container:flat_hash_map", "@absl//flags:parse", "@absl//status", @@ -83,7 +83,7 @@ cc_library( name = "bazel_types", hdrs = ["bazel_types.h"], - deps = ["//rs_bindings_from_cc/util:string_type"], + deps = ["//common:string_type"], ) cc_library( @@ -93,7 +93,7 @@ deps = [ ":bazel_types", ":ir", - "//rs_bindings_from_cc/util:status_macros", + "//common:status_macros", "@absl//container:flat_hash_map", "@absl//flags:flag", "@absl//status:statusor", @@ -131,7 +131,7 @@ hdrs = ["ast_consumer.h"], deps = [ ":importer", - "//rs_bindings_from_cc/util:check", + "//common:check", "@llvm///clang:ast", "@llvm///clang:frontend", ], @@ -145,10 +145,10 @@ ":ast_convert", ":bazel_types", ":ir", + "//common:check", + "//common:status_macros", "//lifetime_annotations", "//lifetime_annotations:type_lifetimes", - "//rs_bindings_from_cc/util:check", - "//rs_bindings_from_cc/util:status_macros", "@absl//container:flat_hash_map", "@absl//container:flat_hash_set", "@absl//status", @@ -185,8 +185,8 @@ hdrs = ["ir.h"], deps = [ ":bazel_types", - "//rs_bindings_from_cc/util:check", - "//rs_bindings_from_cc/util:strong_int", + "//common:check", + "//common:strong_int", "@absl//strings", "@llvm///llvm:Support", ], @@ -233,7 +233,7 @@ ":frontend_action", ":importer", ":ir", - "//rs_bindings_from_cc/util:check", + "//common:check", "@absl//container:flat_hash_map", "@absl//status", "@absl//status:statusor", @@ -322,7 +322,7 @@ hdrs = ["ast_convert.h"], deps = [ ":ir", - "//rs_bindings_from_cc/util:check", + "//common:check", "@absl//functional:function_ref", "@llvm///clang:ast", "@llvm///clang:basic",
diff --git a/rs_bindings_from_cc/ast_consumer.cc b/rs_bindings_from_cc/ast_consumer.cc index 1a0c830..eb74b5d 100644 --- a/rs_bindings_from_cc/ast_consumer.cc +++ b/rs_bindings_from_cc/ast_consumer.cc
@@ -4,8 +4,8 @@ #include "rs_bindings_from_cc/ast_consumer.h" +#include "common/check.h" #include "rs_bindings_from_cc/importer.h" -#include "rs_bindings_from_cc/util/check.h" #include "third_party/llvm/llvm-project/clang/include/clang/AST/ASTContext.h" #include "third_party/llvm/llvm-project/clang/include/clang/Frontend/CompilerInstance.h"
diff --git a/rs_bindings_from_cc/ast_convert.cc b/rs_bindings_from_cc/ast_convert.cc index 5e4b0d7..5f8742a 100644 --- a/rs_bindings_from_cc/ast_convert.cc +++ b/rs_bindings_from_cc/ast_convert.cc
@@ -5,8 +5,8 @@ #include "rs_bindings_from_cc/ast_convert.h" #include "third_party/absl/functional/function_ref.h" +#include "common/check.h" #include "rs_bindings_from_cc/ir.h" -#include "rs_bindings_from_cc/util/check.h" #include "third_party/llvm/llvm-project/clang/include/clang/AST/Decl.h" #include "third_party/llvm/llvm-project/clang/include/clang/AST/DeclCXX.h" #include "third_party/llvm/llvm-project/clang/include/clang/Basic/Specifiers.h"
diff --git a/rs_bindings_from_cc/bazel_types.h b/rs_bindings_from_cc/bazel_types.h index 59b1bb1..1c87ae1 100644 --- a/rs_bindings_from_cc/bazel_types.h +++ b/rs_bindings_from_cc/bazel_types.h
@@ -7,7 +7,7 @@ #include <string> -#include "rs_bindings_from_cc/util/string_type.h" +#include "common/string_type.h" namespace crubit {
diff --git a/rs_bindings_from_cc/cmdline.cc b/rs_bindings_from_cc/cmdline.cc index 3baeed3..8b617dd 100644 --- a/rs_bindings_from_cc/cmdline.cc +++ b/rs_bindings_from_cc/cmdline.cc
@@ -13,7 +13,7 @@ #include "third_party/absl/flags/flag.h" #include "third_party/absl/strings/str_cat.h" #include "third_party/absl/strings/substitute.h" -#include "rs_bindings_from_cc/util/status_macros.h" +#include "common/status_macros.h" #include "third_party/llvm/llvm-project/llvm/include/llvm/Support/JSON.h" ABSL_FLAG(bool, do_nothing, false,
diff --git a/rs_bindings_from_cc/importer.cc b/rs_bindings_from_cc/importer.cc index a867f36..c06fcbf 100644 --- a/rs_bindings_from_cc/importer.cc +++ b/rs_bindings_from_cc/importer.cc
@@ -25,12 +25,12 @@ #include "third_party/absl/strings/str_join.h" #include "third_party/absl/strings/string_view.h" #include "third_party/absl/strings/substitute.h" +#include "common/check.h" +#include "common/status_macros.h" #include "lifetime_annotations/type_lifetimes.h" #include "rs_bindings_from_cc/ast_convert.h" #include "rs_bindings_from_cc/bazel_types.h" #include "rs_bindings_from_cc/ir.h" -#include "rs_bindings_from_cc/util/check.h" -#include "rs_bindings_from_cc/util/status_macros.h" #include "third_party/llvm/llvm-project/clang/include/clang/AST/ASTContext.h" #include "third_party/llvm/llvm-project/clang/include/clang/AST/Attrs.inc" #include "third_party/llvm/llvm-project/clang/include/clang/AST/CXXInheritance.h"
diff --git a/rs_bindings_from_cc/importer.h b/rs_bindings_from_cc/importer.h index 22ba931..f989e6e 100644 --- a/rs_bindings_from_cc/importer.h +++ b/rs_bindings_from_cc/importer.h
@@ -17,11 +17,11 @@ #include "third_party/absl/container/flat_hash_set.h" #include "third_party/absl/status/statusor.h" #include "third_party/absl/types/span.h" +#include "common/check.h" #include "lifetime_annotations/lifetime_annotations.h" #include "lifetime_annotations/type_lifetimes.h" #include "rs_bindings_from_cc/bazel_types.h" #include "rs_bindings_from_cc/ir.h" -#include "rs_bindings_from_cc/util/check.h" #include "third_party/llvm/llvm-project/clang/include/clang/AST/ASTContext.h" #include "third_party/llvm/llvm-project/clang/include/clang/AST/Decl.h" #include "third_party/llvm/llvm-project/clang/include/clang/AST/Mangle.h"
diff --git a/rs_bindings_from_cc/ir.cc b/rs_bindings_from_cc/ir.cc index 673a68e..3d9d8a5 100644 --- a/rs_bindings_from_cc/ir.cc +++ b/rs_bindings_from_cc/ir.cc
@@ -14,9 +14,9 @@ #include <vector> #include "third_party/absl/strings/string_view.h" +#include "common/check.h" +#include "common/strong_int.h" #include "rs_bindings_from_cc/bazel_types.h" -#include "rs_bindings_from_cc/util/check.h" -#include "rs_bindings_from_cc/util/strong_int.h" #include "third_party/llvm/llvm-project/llvm/include/llvm/Support/JSON.h" namespace crubit { @@ -27,12 +27,12 @@ } template <typename TTag, typename TInt> -llvm::json::Value toJSON(const StrongInt<TTag, TInt> strong_int) { +llvm::json::Value toJSON(const crubit::StrongInt<TTag, TInt> strong_int) { return llvm::json::Value(strong_int.value()); } template <typename TTag> -llvm::json::Value toJSON(const StringType<TTag> string_type) { +llvm::json::Value toJSON(const crubit::StringType<TTag> string_type) { return llvm::json::Value(string_type.value()); }
diff --git a/rs_bindings_from_cc/ir.h b/rs_bindings_from_cc/ir.h index 50717c2..aeae4d1 100644 --- a/rs_bindings_from_cc/ir.h +++ b/rs_bindings_from_cc/ir.h
@@ -22,9 +22,9 @@ #include <vector> #include "third_party/absl/strings/string_view.h" +#include "common/check.h" +#include "common/strong_int.h" #include "rs_bindings_from_cc/bazel_types.h" -#include "rs_bindings_from_cc/util/check.h" -#include "rs_bindings_from_cc/util/strong_int.h" #include "third_party/llvm/llvm-project/llvm/include/llvm/ADT/APSInt.h" #include "third_party/llvm/llvm-project/llvm/include/llvm/ADT/Optional.h" #include "third_party/llvm/llvm-project/llvm/include/llvm/Support/FormatVariadic.h"
diff --git a/rs_bindings_from_cc/ir_from_cc.cc b/rs_bindings_from_cc/ir_from_cc.cc index 146b6f2..1fd9166 100644 --- a/rs_bindings_from_cc/ir_from_cc.cc +++ b/rs_bindings_from_cc/ir_from_cc.cc
@@ -15,11 +15,11 @@ #include "third_party/absl/strings/string_view.h" #include "third_party/absl/strings/substitute.h" #include "third_party/absl/types/span.h" +#include "common/check.h" #include "rs_bindings_from_cc/bazel_types.h" #include "rs_bindings_from_cc/frontend_action.h" #include "rs_bindings_from_cc/importer.h" #include "rs_bindings_from_cc/ir.h" -#include "rs_bindings_from_cc/util/check.h" #include "third_party/llvm/llvm-project/clang/include/clang/Basic/FileManager.h" #include "third_party/llvm/llvm-project/clang/include/clang/Basic/FileSystemOptions.h" #include "third_party/llvm/llvm-project/clang/include/clang/Frontend/FrontendAction.h"
diff --git a/rs_bindings_from_cc/rs_bindings_from_cc.cc b/rs_bindings_from_cc/rs_bindings_from_cc.cc index aef8e68..35cedc1 100644 --- a/rs_bindings_from_cc/rs_bindings_from_cc.cc +++ b/rs_bindings_from_cc/rs_bindings_from_cc.cc
@@ -16,12 +16,12 @@ #include "third_party/absl/status/statusor.h" #include "third_party/absl/strings/string_view.h" #include "common/file_io.h" +#include "common/status_macros.h" #include "rs_bindings_from_cc/bazel_types.h" #include "rs_bindings_from_cc/cmdline.h" #include "rs_bindings_from_cc/ir.h" #include "rs_bindings_from_cc/ir_from_cc.h" #include "rs_bindings_from_cc/src_code_gen.h" -#include "rs_bindings_from_cc/util/status_macros.h" #include "third_party/llvm/llvm-project/llvm/include/llvm/Support/FormatVariadic.h" #include "third_party/llvm/llvm-project/llvm/include/llvm/Support/JSON.h" #include "third_party/llvm/llvm-project/llvm/include/llvm/Support/raw_ostream.h"
diff --git a/rs_bindings_from_cc/util/BUILD b/rs_bindings_from_cc/util/BUILD deleted file mode 100644 index f07b18b..0000000 --- a/rs_bindings_from_cc/util/BUILD +++ /dev/null
@@ -1,45 +0,0 @@ -# Various utilities needed by rs_bindings_from_cc - -licenses(["notice"]) - -package(default_visibility = ["//third_party/crubit:__subpackages__"]) - -cc_library( - name = "check", - hdrs = ["check.h"], - deps = [ - "@absl//base:core_headers", - "@llvm///llvm:Support", - ], -) - -cc_library( - name = "strong_int", - hdrs = ["strong_int.h"], - deps = [ - "@absl//base:core_headers", - "@absl//meta:type_traits", - ], -) - -cc_library( - name = "string_type", - hdrs = ["string_type.h"], - deps = [ - "@absl//container:flat_hash_set", - "@absl//flags:marshalling", - "@absl//meta:type_traits", - "@absl//strings", - ], -) - -cc_library( - name = "status_macros", - hdrs = ["status_macros.h"], - deps = [ - "@absl//base:core_headers", - "@absl//status", - "@absl//status:statusor", - "@absl//types:source_location", - ], -)
diff --git a/rs_bindings_from_cc/util/README.md b/rs_bindings_from_cc/util/README.md deleted file mode 100644 index 1244391..0000000 --- a/rs_bindings_from_cc/util/README.md +++ /dev/null
@@ -1,2 +0,0 @@ -`util` subdirectory contains various utilities. Some of them are -forked copies of internal Google libraries that are not included in Abseil.
diff --git a/rs_bindings_from_cc/util/check.h b/rs_bindings_from_cc/util/check.h deleted file mode 100644 index 8e4a874..0000000 --- a/rs_bindings_from_cc/util/check.h +++ /dev/null
@@ -1,36 +0,0 @@ -// Part of the Crubit project, under the Apache License v2.0 with LLVM -// Exceptions. See /LICENSE for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - -#ifndef CRUBIT_RS_BINDINGS_FROM_CC_UTIL_CHECK_H_ -#define CRUBIT_RS_BINDINGS_FROM_CC_UTIL_CHECK_H_ - -#include "third_party/absl/base/attributes.h" -#include "third_party/absl/base/optimization.h" -#include "third_party/llvm/llvm-project/llvm/include/llvm/Support/ErrorHandling.h" -#include "third_party/llvm/llvm-project/llvm/include/llvm/Support/FormatVariadic.h" - -#define CRUBIT_CHECK(condition) \ - do { \ - if (ABSL_PREDICT_FALSE(!(condition))) { \ - ::llvm::report_fatal_error( \ - ::llvm::formatv("CRUBIT_CHECK failure: {0}:{1}: {2}", __FILE__, \ - __LINE__, #condition)); \ - } \ - } while (false) - -namespace crubit { -template <typename T> -ABSL_MUST_USE_RESULT T DieIfNull(const char* file, int line, - const char* exprtext, T&& t) { - if (ABSL_PREDICT_FALSE(t == nullptr)) { - ::llvm::report_fatal_error(llvm::formatv( - "CRUBIT_DIE_IF_NULL failure: {0}:{1}: {2}", file, line, exprtext)); - } - return std::forward<T>(t); -} -} // namespace crubit -#define CRUBIT_DIE_IF_NULL(value) \ - ::crubit::DieIfNull(__FILE__, __LINE__, #value, (value)) - -#endif // CRUBIT_RS_BINDINGS_FROM_CC_UTIL_CHECK_H_
diff --git a/rs_bindings_from_cc/util/status_macros.h b/rs_bindings_from_cc/util/status_macros.h deleted file mode 100644 index 5fa192e..0000000 --- a/rs_bindings_from_cc/util/status_macros.h +++ /dev/null
@@ -1,155 +0,0 @@ -// Part of the Crubit project, under the Apache License v2.0 with LLVM -// Exceptions. See /LICENSE for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - -// Helper macros and methods to return and propagate errors with `absl::Status`. - -#ifndef CRUBIT_RS_BINDINGS_FROM_CC_UTIL_STATUS_MACROS_H_ -#define CRUBIT_RS_BINDINGS_FROM_CC_UTIL_STATUS_MACROS_H_ - -#include <utility> - -#include "third_party/absl/base/optimization.h" -#include "third_party/absl/status/status.h" -#include "third_party/absl/status/statusor.h" -#include "third_party/absl/types/source_location.h" - -// Evaluates an expression that produces a `absl::Status`. If the status is not -// ok, returns it from the current function. -// -// For example: -// absl::Status MultiStepFunction() { -// CRUBIT_RETURN_IF_ERROR(Function(args...)); -// CRUBIT_RETURN_IF_ERROR(foo.Method(args...)); -// return absl::OkStatus(); -// } -#define CRUBIT_RETURN_IF_ERROR(expr) \ - do { \ - if (::absl::Status status = (expr); status.ok()) { \ - } else { \ - return status; \ - } \ - } while (false) - -// Executes an expression `rexpr` that returns an `absl::StatusOr<T>`. On OK, -// moves its value into the variable defined by `lhs`, otherwise returns -// from the current function. -// -// Interface: -// -// CRUBIT_ASSIGN_OR_RETURN(lhs, rexpr) -// -// WARNING: if lhs is parenthesized, the parentheses are removed. See examples -// for more details. -// -// WARNING: expands into multiple statements; it cannot be used in a single -// statement (e.g. as the body of an if statement without {})! -// -// Example: Declaring and initializing a new variable (ValueType can be anything -// that can be initialized with assignment, including references): -// CRUBIT_ASSIGN_OR_RETURN(ValueType value, MaybeGetValue(arg)); -// -// Example: Assigning to an existing variable: -// ValueType value; -// CRUBIT_ASSIGN_OR_RETURN(value, MaybeGetValue(arg)); -// -// Example: Assigning to an expression with side effects: -// MyProto data; -// CRUBIT_ASSIGN_OR_RETURN(*data.mutable_str(), MaybeGetValue(arg)); -// // No field "str" is added on error. -// -// Example: Initializing a `std::unique_ptr`. -// CRUBIT_ASSIGN_OR_RETURN(std::unique_ptr<T> ptr, MaybeGetPtr(arg)); -// -// Example: Initializing a map. Because of C++ preprocessor limitations, -// the type used in CRUBIT_ASSIGN_OR_RETURN cannot contain commas, so wrap the -// lhs in parentheses: -// CRUBIT_ASSIGN_OR_RETURN((absl::flat_hash_map<Foo, Bar> my_map), GetMap()); -// Or use `auto` if the type is obvious enough: -// CRUBIT_ASSIGN_OR_RETURN(auto my_map, GetMap()); -// -// Example: Assigning to structured bindings (<internal link>/169). The same situation -// with comma as in map, so wrap the statement in parentheses. -// CRUBIT_ASSIGN_OR_RETURN((auto [first, second]), GetPair()); -#define CRUBIT_ASSIGN_OR_RETURN(lhs, rexpr) \ - CRUBIT_STATUS_MACROS_IMPL_ASSIGN_OR_RETURN_( \ - CRUBIT_STATUS_MACROS_IMPL_CONCAT_(_status_or_value, __LINE__), lhs, \ - rexpr, \ - return absl::Status(std::move(CRUBIT_STATUS_MACROS_IMPL_CONCAT_( \ - _status_or_value, __LINE__)) \ - .status(), \ - ABSL_LOC)) - -// ================================================================= -// == Implementation details, do not rely on anything below here. == -// ================================================================= - -// Some builds do not support C++14 fully yet, using C++11 constexpr technique. -constexpr bool HasPotentialConditionalOperator(const char* lhs, int index) { - return (index == -1 ? false - : (lhs[index] == '?' ? true - : HasPotentialConditionalOperator( - lhs, index - 1))); -} - -#define CRUBIT_STATUS_MACROS_IMPL_ASSIGN_OR_RETURN_(statusor, lhs, rexpr, \ - error_expression) \ - auto statusor = (rexpr); \ - if (ABSL_PREDICT_FALSE(!statusor.ok())) { \ - error_expression; \ - } \ - { \ - static_assert( \ - #lhs[0] != '(' || #lhs[sizeof(#lhs) - 2] != ')' || \ - !HasPotentialConditionalOperator(#lhs, sizeof(#lhs) - 2), \ - "Identified potential conditional operator, consider not " \ - "using CRUBIT_ASSIGN_OR_RETURN"); \ - } \ - CRUBIT_STATUS_MACROS_IMPL_UNPARENTHESIZE_IF_PARENTHESIZED(lhs) = \ - std::move(statusor).ValueOrDie() - -// Internal helpers for macro expansion. -#define CRUBIT_STATUS_MACROS_IMPL_EAT(...) -#define CRUBIT_STATUS_MACROS_IMPL_REM(...) __VA_ARGS__ -#define CRUBIT_STATUS_MACROS_IMPL_EMPTY() - -// Internal helpers for emptyness arguments check. -#define CRUBIT_STATUS_MACROS_IMPL_IS_EMPTY_INNER(...) \ - CRUBIT_STATUS_MACROS_IMPL_IS_EMPTY_INNER_HELPER((__VA_ARGS__, 0, 1)) -// MSVC expands variadic macros incorrectly, so we need this extra indirection -// to work around that (b/110959038). -#define CRUBIT_STATUS_MACROS_IMPL_IS_EMPTY_INNER_HELPER(args) \ - CRUBIT_STATUS_MACROS_IMPL_IS_EMPTY_INNER_I args -#define CRUBIT_STATUS_MACROS_IMPL_IS_EMPTY_INNER_I(e0, e1, is_empty, ...) \ - is_empty - -#define CRUBIT_STATUS_MACROS_IMPL_IS_EMPTY(...) \ - CRUBIT_STATUS_MACROS_IMPL_IS_EMPTY_I(__VA_ARGS__) -#define CRUBIT_STATUS_MACROS_IMPL_IS_EMPTY_I(...) \ - CRUBIT_STATUS_MACROS_IMPL_IS_EMPTY_INNER(_, ##__VA_ARGS__) - -// Internal helpers for if statement. -#define CRUBIT_STATUS_MACROS_IMPL_IF_1(_Then, _Else) _Then -#define CRUBIT_STATUS_MACROS_IMPL_IF_0(_Then, _Else) _Else -#define CRUBIT_STATUS_MACROS_IMPL_IF(_Cond, _Then, _Else) \ - CRUBIT_STATUS_MACROS_IMPL_CONCAT_(CRUBIT_STATUS_MACROS_IMPL_IF_, _Cond) \ - (_Then, _Else) - -// Expands to 1 if the input is parenthesized. Otherwise expands to 0. -#define CRUBIT_STATUS_MACROS_IMPL_IS_PARENTHESIZED(...) \ - CRUBIT_STATUS_MACROS_IMPL_IS_EMPTY(CRUBIT_STATUS_MACROS_IMPL_EAT __VA_ARGS__) - -// If the input is parenthesized, removes the parentheses. Otherwise expands to -// the input unchanged. -#define CRUBIT_STATUS_MACROS_IMPL_UNPARENTHESIZE_IF_PARENTHESIZED(...) \ - CRUBIT_STATUS_MACROS_IMPL_IF( \ - CRUBIT_STATUS_MACROS_IMPL_IS_PARENTHESIZED(__VA_ARGS__), \ - CRUBIT_STATUS_MACROS_IMPL_REM, CRUBIT_STATUS_MACROS_IMPL_EMPTY()) \ - __VA_ARGS__ - -// Internal helper for concatenating macro values. -#define CRUBIT_STATUS_MACROS_IMPL_CONCAT_INNER_(x, y) x##y -#define CRUBIT_STATUS_MACROS_IMPL_CONCAT_(x, y) \ - CRUBIT_STATUS_MACROS_IMPL_CONCAT_INNER_(x, y) - -#endif // CRUBIT_RS_BINDINGS_FROM_CC_UTIL_STATUS_MACROS_H_
diff --git a/rs_bindings_from_cc/util/string_type.h b/rs_bindings_from_cc/util/string_type.h deleted file mode 100644 index 05a3b86..0000000 --- a/rs_bindings_from_cc/util/string_type.h +++ /dev/null
@@ -1,131 +0,0 @@ -// Part of the Crubit project, under the Apache License v2.0 with LLVM -// Exceptions. See /LICENSE for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - -// Typed strings. - -// Example usage: -// -// CRUBIT_DEFINE_STRING_TYPE(Foo); -// CRUBIT_DEFINE_STRING_TYPE(Bar); -// Foo foo("foo_value"); -// Bar bar("bar_value"); -// -// The following two statements will not compile. -// -// foo = bar; -// -// if (foo == bar) { } else { }; ... -// -// The strongly-typed types are hashable with the Abseil hashing framework, -// so they work out of the box with any modern hashing system. For non-Abseil -// uses, the explicit functors absl::Hash<Foo>, absl::Hash<Bar> may be used: -// -// std::unordered_set<Foo, absl::Hash<Foo>> -// __gnu_cxx::hash_map<Bar, int, absl::Hash<Bar>> -// -// (But absl::flat_hash_set<Foo> is much better!) - -#ifndef CRUBIT_RS_BINDINGS_FROM_CC_UTIL_STRING_TYPE_H_ -#define CRUBIT_RS_BINDINGS_FROM_CC_UTIL_STRING_TYPE_H_ - -#include <memory> -#include <ostream> // NOLINT -#include <string> -#include <utility> - -#include "third_party/absl/container/flat_hash_set.h" -#include "third_party/absl/flags/marshalling.h" -#include "third_party/absl/meta/type_traits.h" -#include "third_party/absl/strings/string_view.h" - -// Defines the StringType using StringTypeRepresentation and provides a type -// alias to string_type_name. The struct string_type_name ## _tag_ trickery is -// needed to ensure that a new type is created per string_type_name. -// -// StringTypeRepresentation classes, as a rule, should either *be* a string-like -// object, or should provide a "value()" method that returns a string-like -// object. If they can provide more optimal implementations of relational -// operators, they should define operator== and operator<; all other relational -// operators are defined in terms of those. If they can provide more optimal -// implementations of AbslHashValue or operator<<, they should provide those as -// well. -#define CRUBIT_DEFINE_STRING_TYPE(string_type_name) \ - using string_type_name = ::crubit::StringType<class string_type_name##_tag_>; - -namespace crubit { - -// StringType provides these operations: -// * relational operators (==, !=, <, <=, >, >=) -// * compare (future <=> operator) -// * AbslHashValue -// * streaming with operator<< -// * value(), which should return a string-like object (const string&, -// absl::string_view, ShortString<N>, etc.) -template <typename Tag> -class StringType { - public: - StringType() = default; - explicit StringType(std::string value) : s_(std::move(value)) {} - - const std::string& value() const { return s_; } - - bool empty() const { return value().empty(); } - - // If you want to optimize your relational methods, you need only implement - // these three: compare, operator==, and operator<. - int compare(const StringType& other) const { - return value().compare(other.value()); - } - friend bool operator==(const StringType& left, const StringType& right) { - return left.value() == right.value(); - } - friend bool operator<(const StringType& left, const StringType& right) { - return left.value() < right.value(); - } - - // These methods are defined in terms of the above. - friend bool operator!=(const StringType& left, const StringType& right) { - return !(left == right); - } - friend bool operator>(const StringType& left, const StringType& right) { - return right < left; - } - friend bool operator<=(const StringType& left, const StringType& right) { - return !(left > right); - } - friend bool operator>=(const StringType& left, const StringType& right) { - return !(left < right); - } - - template <typename H> - friend H AbslHashValue(H h, const StringType& s) { - H::combine(std::move(h), s.value()); - } - - friend std::ostream& operator<<(std::ostream& os, const StringType& s) { - return os << s.value(); - } - - private: - std::string s_; -}; - -// Allows typed strings to be used as ABSL_FLAG values. -// -// This is equivalent in behavior to just using a raw std::string. -template <typename Tag> -bool AbslParseFlag(absl::string_view text, StringType<Tag>* out, - std::string* error) { - *out = StringType<Tag>(text); - return true; -} - -template <typename Tag> -std::string AbslUnparseFlag(const StringType<Tag>& val) { - return absl::UnparseFlag(std::string(val.value())); -} - -} // namespace crubit - -#endif // CRUBIT_RS_BINDINGS_FROM_CC_UTIL_STRING_TYPE_H_
diff --git a/rs_bindings_from_cc/util/strong_int.h b/rs_bindings_from_cc/util/strong_int.h deleted file mode 100644 index 5553996..0000000 --- a/rs_bindings_from_cc/util/strong_int.h +++ /dev/null
@@ -1,166 +0,0 @@ -// Part of the Crubit project, under the Apache License v2.0 with LLVM -// Exceptions. See /LICENSE for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - -// StrongInt<T> is a simple template class mechanism for defining "logical" -// integer-like class types that support almost all of the same functionality -// as native integer types, but which prevents assignment, construction, and -// other operations from other integer-like types. In other words, you cannot -// assign from raw integer types or other StrongInt<> types, nor can you do -// most arithmetic or logical operations. -// -// A StrongInt<T> should compile away to a raw T in optimized mode. What this -// means is that the generated assembly for: -// -// int64 foo = 123; -// int64 bar = 456; -// bool is_equal = (foo == bar); -// constexpr int64 fubar = 789; -// -// ...should be identical to the generated assembly for: -// -// CRUBIT_DEFINE_STRONG_INT_TYPE(MyStrongInt, int64); -// MyStrongInt foo(123); -// MyStrongInt bar(456); -// bool is_equal = (foo == bar); -// constexpr MyStrongInt fubar(789); -// -// Since the methods are all inline and non-virtual and the class has just -// one data member, the compiler can erase the StrongInt class entirely in its -// code-generation phase. This also means that you can pass StrongInt<T> -// around by value just as you would a raw T. -// -// It is important to note that StrongInt does NOT generate compile time -// warnings or errors for overflows on implicit constant conversions. -// For example, the below demonstrates a case where the 2 are not equivalent -// at compile time and can lead to subtle initialization bugs: -// -// CRUBIT_DEFINE_STRONG_INT_TYPE(MyStrongInt8, int8); -// int8 foo = 1024; // Compile error: const conversion to ... -// MyStrongInt8 foo(1024); // Compiles ok: foo has undefined / 0 value. -// -// Usage: -// CRUBIT_DEFINE_STRONG_INT_TYPE(Name, NativeType); -// -// Defines a new StrongInt type named 'Name' in the current namespace with -// no validation of operations. -// -// Name: The desired name for the new StrongInt typedef. Must be unique -// within the current namespace. -// NativeType: The primitive integral type this StrongInt will hold, as -// defined by std::numeric_limits::is_integer (see <type_traits>). -// -// Supported operations: -// StrongInt<T> = StrongInt<T> -// StrongInt<T> == StrongInt<T> -// StrongInt<T> < StrongInt<T> -// -// This class also provides a .value() accessor method and defines a hash -// functor that allows the IntType to be used as key to hashable containers. - -#ifndef CRUBIT_RS_BINDINGS_FROM_CC_UTIL_STRONG_INT_H_ -#define CRUBIT_RS_BINDINGS_FROM_CC_UTIL_STRONG_INT_H_ - -#include <cstdint> -#include <iosfwd> -#include <limits> -#include <ostream> -#include <type_traits> -#include <utility> - -#include "third_party/absl/base/macros.h" -#include "third_party/absl/meta/type_traits.h" - -namespace crubit { - -// Holds an integer value (of type NativeType) and behaves as a NativeType by -// exposing assignment, unary, comparison, and arithmetic operators. -// -// This class is NOT thread-safe. -template <typename TagType, typename NativeType> -class StrongInt { - public: - typedef NativeType ValueType; - - // Default value initialization. - constexpr StrongInt() : value_(NativeType()) {} - - // Explicit initialization from a numeric primitive. - template < - class T, - class = std::enable_if_t<std::is_same_v< - decltype(static_cast<ValueType>(std::declval<T>())), ValueType>>> - explicit constexpr StrongInt(T init_value) - : value_(static_cast<ValueType>(init_value)) {} - - // Use the default copy constructor, assignment, and destructor. - - // Accesses the raw value. - constexpr ValueType value() const { return value_; } - - // Metadata functions. - static constexpr StrongInt Max() { - return StrongInt(std::numeric_limits<ValueType>::max()); - } - static constexpr StrongInt Min() { - return StrongInt(std::numeric_limits<ValueType>::min()); - } - - template <typename H> - friend H AbslHashValue(H h, const StrongInt &i) { - return H::combine(std::move(h), i.value_); - } - - private: - // The integer value of type ValueType. - ValueType value_; - - static_assert(std::numeric_limits<ValueType>::is_integer, - "invalid integer type for strong int"); -}; - -// Provide the << operator, primarily for logging purposes. -template <typename TagType, typename ValueType> -std::ostream &operator<<(std::ostream &os, StrongInt<TagType, ValueType> arg) { - return os << arg.value(); -} - -// Provide the << operator, primarily for logging purposes. Specialized for int8 -// so that an integer and not a character is printed. -template <typename TagType> -std::ostream &operator<<(std::ostream &os, StrongInt<TagType, int8_t> arg) { - return os << static_cast<int>(arg.value()); -} - -// Provide the << operator, primarily for logging purposes. Specialized for -// uint8 so that an integer and not a character is printed. -template <typename TagType> -std::ostream &operator<<(std::ostream &os, StrongInt<TagType, uint8_t> arg) { - return os << static_cast<unsigned int>(arg.value()); -} - -// Define comparison operators. We allow all comparison operators. -#define CRUBIT_STRONG_INT_COMPARISON_OP(op) \ - template <typename TagType, typename ValueType> \ - constexpr bool operator op(StrongInt<TagType, ValueType> lhs, \ - StrongInt<TagType, ValueType> rhs) { \ - return lhs.value() op rhs.value(); \ - } -CRUBIT_STRONG_INT_COMPARISON_OP(==); // NOLINT(whitespace/operators) -CRUBIT_STRONG_INT_COMPARISON_OP(!=); // NOLINT(whitespace/operators) -CRUBIT_STRONG_INT_COMPARISON_OP(<); // NOLINT(whitespace/operators) -CRUBIT_STRONG_INT_COMPARISON_OP(<=); // NOLINT(whitespace/operators) -CRUBIT_STRONG_INT_COMPARISON_OP(>); // NOLINT(whitespace/operators) -CRUBIT_STRONG_INT_COMPARISON_OP(>=); // NOLINT(whitespace/operators) -#undef CRUBIT_STRONG_INT_COMPARISON_OP - -} // namespace crubit - -// Defines the StrongInt using value_type and typedefs it to type_name. -// The struct int_type_name ## _tag_ trickery is needed to ensure that a new -// type is created per type_name. -#define CRUBIT_DEFINE_STRONG_INT_TYPE(type_name, value_type) \ - typedef ::crubit::StrongInt<class type_name##_strong_int_tag_, value_type> \ - type_name; - -#endif // CRUBIT_RS_BINDINGS_FROM_CC_UTIL_STRONG_INT_H_