diff --git a/include/slang/numeric/ConstantValue.h b/include/slang/numeric/ConstantValue.h index 1dd27f9fb..448c18822 100644 --- a/include/slang/numeric/ConstantValue.h +++ b/include/slang/numeric/ConstantValue.h @@ -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; diff --git a/source/numeric/ConstantValue.cpp b/source/numeric/ConstantValue.cpp index a8811a4ae..744bdf835 100644 --- a/source/numeric/ConstantValue.cpp +++ b/source/numeric/ConstantValue.cpp @@ -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; if constexpr (std::is_same_v) return ""s; @@ -37,7 +38,7 @@ std::string ConstantValue::toString(bitwidth_t abbreviateThresholdBits, bool exa return "$"s; else if constexpr (std::is_same_v) { FormatBuffer buffer; - buffer.append("'{"); + buffer.append(useAssignmentPatterns ? "'{"sv : "["sv); for (auto& element : arg) { buffer.append(element.toString(abbreviateThresholdBits)); buffer.append(","); @@ -45,14 +46,14 @@ std::string ConstantValue::toString(bitwidth_t abbreviateThresholdBits, bool exa if (!arg.empty()) buffer.pop_back(); - buffer.append("}"); + buffer.append(useAssignmentPatterns ? "}"sv : "]"sv;); return buffer.str(); } else if constexpr (std::is_same_v) return fmt::format("\"{}\"", arg); else if constexpr (std::is_same_v) { FormatBuffer buffer; - buffer.append("'{"); + buffer.append(useAssignmentPatterns ? "'{"sv : "["sv); for (auto& [key, val] : *arg) buffer.format("{}:{},", key.toString(abbreviateThresholdBits), val.toString(abbreviateThresholdBits)); @@ -63,12 +64,12 @@ 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) { FormatBuffer buffer; - buffer.append("'{"); + buffer.append(useAssignmentPatterns ? "'{"sv : "["sv); for (auto& element : *arg) { buffer.append(element.toString(abbreviateThresholdBits)); buffer.append(","); @@ -76,7 +77,7 @@ std::string ConstantValue::toString(bitwidth_t abbreviateThresholdBits, bool exa if (!arg->empty()) buffer.pop_back(); - buffer.append("}"); + buffer.append(useAssignmentPatterns ? "}"sv : "]"sv;); return buffer.str(); } else if constexpr (std::is_same_v) {