Skip to content

Commit

Permalink
Add a version of TypeBandwidth that returns a status on error. (#95)
Browse files Browse the repository at this point in the history
Co-authored-by: PINS Team <[email protected]>
  • Loading branch information
jonathan-dilorenzo and PINS Team authored Apr 26, 2023
1 parent f424922 commit ad554b3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 0 additions & 1 deletion p4_constraints/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ cc_library(
":ast_cc_proto",
"//gutils:proto",
"//gutils:status",
"@com_google_absl//absl/base:log_severity",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/log",
Expand Down
11 changes: 11 additions & 0 deletions p4_constraints/ast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "google/protobuf/descriptor.h"
#include "google/protobuf/util/message_differencer.h"
#include "gutils/proto.h"
#include "gutils/status_builder.h"
#include "gutils/status_macros.h"
#include "p4_constraints/ast.pb.h"

Expand Down Expand Up @@ -117,6 +118,16 @@ absl::optional<int> TypeBitwidth(const Type& type) {
}
}

absl::StatusOr<int> TypeBitwidthOrStatus(const Type& type) {
std::optional<int> bitwidth = TypeBitwidth(type);
if (bitwidth.has_value()) {
return *bitwidth;
} else {
return gutils::InvalidArgumentErrorBuilder(GUTILS_LOC)
<< "expected a type with bitwidth, but got: " << type;
}
}

bool SetTypeBitwidth(Type* type, int bitwidth) {
switch (type->type_case()) {
case Type::kFixedUnsigned:
Expand Down
4 changes: 4 additions & 0 deletions p4_constraints/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ bool TypeHasOrdering(const Type& type);
// Returns bit-width of the given type, provided the type is fixed-size.
absl::optional<int> TypeBitwidth(const Type& type);

// Returns bit-width of the given type, provided the type is fixed-size.
// Otherwise returns an InvalidArgumentError.
absl::StatusOr<int> TypeBitwidthOrStatus(const ast::Type& type);

// Sets bitwidth of the given type, provided the type is fixed-size, or does
// nothing otherwise. Returns `true` in the former case and `false` in the
// latter case.
Expand Down

0 comments on commit ad554b3

Please sign in to comment.