Skip to content

Commit

Permalink
ConstantValue: Make assignment patterns for containers optional
Browse files Browse the repository at this point in the history
  • Loading branch information
paulsc96 committed Nov 4, 2022
1 parent 79dc782 commit 9852b64
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
4 changes: 2 additions & 2 deletions include/slang/numeric/ConstantValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ class SLANG_EXPORT ConstantValue {
Variant& getVariant() { return value; }
const Variant& getVariant() const { return value; }

std::string toString(bitwidth_t abbreviateThresholdBits =
SVInt::DefaultStringAbbreviationThresholdBits, bool exactUnknowns = false) const;
std::string toString(bitwidth_t abbreviateThresholdBits = SVInt::DefaultStringAbbreviationThresholdBits,
bool exactUnknowns = false, bool useAssignmentPatterns = false) const;
size_t hash() const;

[[nodiscard]] bool empty() const;
Expand Down
17 changes: 9 additions & 8 deletions source/numeric/ConstantValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ struct always_false : std::false_type {};

const ConstantValue ConstantValue::Invalid;

std::string ConstantValue::toString(bitwidth_t abbreviateThresholdBits, bool exactUnknowns) const {
std::string ConstantValue::toString(bitwidth_t abbreviateThresholdBits, bool exactUnknowns,
bool useAssignmentPatterns) const {
return std::visit(
[abbreviateThresholdBits, exactUnknowns](auto&& arg) {
[abbreviateThresholdBits, exactUnknowns, useAssignmentPatterns](auto&& arg) {
using T = std::decay_t<decltype(arg)>;
if constexpr (std::is_same_v<T, std::monostate>)
return "<unset>"s;
Expand All @@ -37,22 +38,22 @@ std::string ConstantValue::toString(bitwidth_t abbreviateThresholdBits, bool exa
return "$"s;
else if constexpr (std::is_same_v<T, Elements>) {
FormatBuffer buffer;
buffer.append("'{");
buffer.append(useAssignmentPatterns ? "'{"sv : "["sv);
for (auto& element : arg) {
buffer.append(element.toString(abbreviateThresholdBits));
buffer.append(",");
}

if (!arg.empty())
buffer.pop_back();
buffer.append("}");
buffer.append(useAssignmentPatterns ? "}"sv : "]"sv);
return buffer.str();
}
else if constexpr (std::is_same_v<T, std::string>)
return fmt::format("\"{}\"", arg);
else if constexpr (std::is_same_v<T, Map>) {
FormatBuffer buffer;
buffer.append("'{");
buffer.append(useAssignmentPatterns ? "'{"sv : "["sv);
for (auto& [key, val] : *arg)
buffer.format("{}:{},", key.toString(abbreviateThresholdBits),
val.toString(abbreviateThresholdBits));
Expand All @@ -63,20 +64,20 @@ std::string ConstantValue::toString(bitwidth_t abbreviateThresholdBits, bool exa
else if (!arg->empty())
buffer.pop_back();

buffer.append("}");
buffer.append(useAssignmentPatterns ? "}"sv : "]"sv);
return buffer.str();
}
else if constexpr (std::is_same_v<T, Queue>) {
FormatBuffer buffer;
buffer.append("'{");
buffer.append(useAssignmentPatterns ? "'{"sv : "["sv);
for (auto& element : *arg) {
buffer.append(element.toString(abbreviateThresholdBits));
buffer.append(",");
}

if (!arg->empty())
buffer.pop_back();
buffer.append("}");
buffer.append(useAssignmentPatterns ? "}"sv : "]"sv);
return buffer.str();
}
else if constexpr (std::is_same_v<T, Union>) {
Expand Down

0 comments on commit 9852b64

Please sign in to comment.