Skip to content

SMT2: encode structs and multi-constructor ADTs as SMT-LIB datatypes#9072

Open
tautschnig wants to merge 1 commit into
diffblue:developfrom
tautschnig:strata/smt2-adt-struct-datatypes
Open

SMT2: encode structs and multi-constructor ADTs as SMT-LIB datatypes#9072
tautschnig wants to merge 1 commit into
diffblue:developfrom
tautschnig:strata/smt2-adt-struct-datatypes

Conversation

@tautschnig

Copy link
Copy Markdown
Collaborator

Declare/parse struct and #adt_constructors (algebraic data) types as SMT-LIB datatypes keyed by tag/#index_type, and handle their construction, member access, and 'with' updates (parse_struct, convert_struct, convert_member, convert_with, find_symbols_rec).

  • Each commit message has a non-empty body, explaining why the change was made.
  • Methods or procedures I have added are documented, following the guidelines provided in CODING_STANDARD.md.
  • The feature or user visible behaviour I have added or modified has been documented in the User Guide in doc/cprover-manual/
  • Regression or unit tests are included, or existing tests cover the modified code (in this case I have detailed which ones those are in the commit message).
  • My commit message includes data points confirming performance improvements (if claimed).
  • My PR is restricted to a single feature or bugfix.
  • White-space or formatting changes outside the feature-related changed lines are in commits of their own.

@tautschnig tautschnig self-assigned this Jun 18, 2026
Copilot AI review requested due to automatic review settings June 18, 2026 21:46

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR extends the SMT2 backend to encode CBMC structs and multi-constructor ADTs as SMT-LIB datatypes, including parsing/printing constructors, member access, and with updates.

Changes:

  • Add multi-constructor ADT handling in struct parsing (parse_struct) and printing (convert_struct).
  • Encode member access and with updates for ADT-backed datatypes using (_ is ...) discriminator checks.
  • Extend symbol discovery to traverse additional type metadata (ID_C_index_type) and reuse datatype names by struct tag.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +3591 to +3606
for(std::size_t ci = 0; ci < ctors.size(); ++ci)
{
if(ci + 1 < ctors.size())
{ out << "(ite (= "; convert_expr(tag_op); out << " " << ci << ") "; }
const auto &ctor_fields = ctors[ci].find(irep_idt("fields")).get_sub();
if(ctor_fields.empty())
out << ctors[ci].find(ID_name).id();
else
{
out << "(" << ctors[ci].find(ID_name).id();
for(const auto &field : ctor_fields)
for(std::size_t j = 0; j < components.size(); ++j)
if(components[j].get_name() == field.id())
{ out << " "; convert_expr(expr.operands()[j]); }
out << ")";
}
Comment thread src/solvers/smt2/smt2_conv.cpp Outdated
Comment on lines +4971 to +4978
const auto &ctors = adt_ctors_cm.get_sub();
for(std::size_t i = 0; i < ctors.size(); ++i)
{
if(i + 1 < ctors.size())
{ out << "(ite ((_ is " << ctors[i].find(ID_name).id() << ") "; convert_expr(struct_op); out << ") " << i << " "; }
else out << i;
}
for(std::size_t i = 0; i + 1 < ctors.size(); ++i) out << ")";
Comment thread src/solvers/smt2/smt2_conv.cpp Outdated
Comment on lines +4717 to +4739
const auto &adt_ctors_cw = struct_type.find(irep_idt("#adt_constructors"));
if(adt_ctors_cw.is_not_nil() && adt_ctors_cw.get_sub().size() > 1)
{
const auto &ctors = adt_ctors_cw.get_sub();
for(std::size_t ci = 0; ci < ctors.size(); ++ci)
{
const auto &cn = ctors[ci].find(ID_name).id();
const auto &flds = ctors[ci].find(irep_idt("fields")).get_sub();
if(ci + 1 < ctors.size())
{ out << "(ite ((_ is " << cn << ") "; convert_expr(expr.old()); out << ") "; }
if(flds.empty())
out << cn;
else
{
out << "(" << cn;
for(const auto &fld : flds)
{ out << " "; if(fld.id() == component_name) convert_expr(value); else { out << "(" << smt_typename << "." << fld.id() << " "; convert_expr(expr.old()); out << ")"; } }
out << ")";
}
if(ci + 1 < ctors.size()) out << " ";
}
for(std::size_t ci = 0; ci + 1 < ctors.size(); ++ci) out << ")";
}
Comment thread src/solvers/smt2/smt2_conv.cpp Outdated
Comment on lines +6354 to +6376
// Reuse existing datatype for structs with the same tag name
// (variants may differ in non-semantic annotations like array sizes).
const irep_idt &tag = to_struct_type(type).get_tag();
std::string smt_typename;
if(!tag.empty())
{
for(const auto &entry : datatype_map)
{
if(
entry.first.id() == ID_struct &&
to_struct_type(entry.first).get_tag() == tag)
{
smt_typename = entry.second;
break;
}
}
}
if(smt_typename.empty())
{
smt_typename =
"struct." + std::to_string(datatype_map.size());
need_decl = true;
}
Comment on lines +6317 to +6320
const typet &idx_type =
static_cast<const typet &>(type.find(ID_C_index_type));
if(idx_type.is_not_nil())
find_symbols_rec(idx_type, recstack);
Comment thread src/solvers/smt2/smt2_conv.cpp Outdated
if(adt_ctors.is_nil() || adt_ctors.get_sub().size() <= 1)
{
// Let's also declare convenience functions to update individual
// members of the struct whil we're at it. The functions are
@codecov

codecov Bot commented Jun 19, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 34.59459% with 121 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.64%. Comparing base (321ba11) to head (9d2fb95).

Files with missing lines Patch % Lines
src/solvers/smt2/smt2_conv.cpp 34.59% 121 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #9072      +/-   ##
===========================================
- Coverage    80.68%   80.64%   -0.05%     
===========================================
  Files         1714     1714              
  Lines       189501   189640     +139     
  Branches        73       73              
===========================================
+ Hits        152902   152930      +28     
- Misses       36599    36710     +111     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@tautschnig tautschnig force-pushed the strata/smt2-adt-struct-datatypes branch from b06f999 to 7cb0c09 Compare June 19, 2026 08:22
Declare/parse struct and #adt_constructors (algebraic data) types as SMT-LIB
datatypes keyed by tag/#index_type, and handle their construction, member
access, and 'with' updates (parse_struct, convert_struct, convert_member,
convert_with, find_symbols_rec).

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
@tautschnig tautschnig force-pushed the strata/smt2-adt-struct-datatypes branch from 7cb0c09 to 9d2fb95 Compare June 19, 2026 12:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants