Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#include "../autogen_base.h"
#include "spacetimedb/bsatn/bsatn.h"
#include "RawColumnDefaultValueV9.g.h"
#include "RawViewDefV9.g.h"
#include "RawProcedureDefV9.g.h"
#include "RawViewDefV9.g.h"

namespace SpacetimeDB::Internal {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
#include <memory>
#include "../autogen_base.h"
#include "spacetimedb/bsatn/bsatn.h"
#include "RawModuleDefV8.g.h"
#include "RawModuleDefV10.g.h"
#include "RawModuleDefV8.g.h"
#include "RawModuleDefV9.g.h"

namespace SpacetimeDB::Internal {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,17 @@ SPACETIMEDB_INTERNAL_PRODUCT_TYPE(RawModuleDefV10) {
}
SPACETIMEDB_PRODUCT_TYPE_EQUALITY(sections)
};

inline void RawSubmoduleV10::bsatn_serialize(::SpacetimeDB::bsatn::Writer& writer) const {
::SpacetimeDB::bsatn::serialize(writer, namespace_);
::SpacetimeDB::bsatn::serialize(writer, *module);
}

inline bool RawSubmoduleV10::operator==(const RawSubmoduleV10& other) const {
return namespace_ == other.namespace_ && module == other.module;
}

inline bool RawSubmoduleV10::operator!=(const RawSubmoduleV10& other) const {
return !(*this == other);
}
} // namespace SpacetimeDB::Internal
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@
#include <memory>
#include "../autogen_base.h"
#include "spacetimedb/bsatn/bsatn.h"
#include "RawTableDefV10.g.h"
#include "Typespace.g.h"
#include "CaseConversionPolicy.g.h"
#include "ExplicitNames.g.h"
#include "RawHttpHandlerDefV10.g.h"
#include "RawHttpRouteDefV10.g.h"
#include "RawLifeCycleReducerDefV10.g.h"
#include "RawProcedureDefV10.g.h"
#include "RawReducerDefV10.g.h"
#include "RawTypeDefV10.g.h"
#include "RawRowLevelSecurityDefV9.g.h"
#include "RawHttpRouteDefV10.g.h"
#include "RawScheduleDefV10.g.h"
#include "RawSubmoduleV10.g.h"
#include "RawTableDefV10.g.h"
#include "RawTypeDefV10.g.h"
#include "RawViewDefV10.g.h"
#include "ExplicitNames.g.h"
#include "RawProcedureDefV10.g.h"
#include "CaseConversionPolicy.g.h"
#include "RawScheduleDefV10.g.h"
#include "RawViewPrimaryKeyDefV10.g.h"
#include "RawHttpHandlerDefV10.g.h"
#include "Typespace.g.h"

namespace SpacetimeDB::Internal {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
#include "../autogen_base.h"
#include "spacetimedb/bsatn/bsatn.h"
#include "MiscModuleExport.g.h"
#include "Typespace.g.h"
#include "TableDesc.g.h"
#include "ReducerDef.g.h"
#include "TableDesc.g.h"
#include "Typespace.g.h"

namespace SpacetimeDB::Internal {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
#include <memory>
#include "../autogen_base.h"
#include "spacetimedb/bsatn/bsatn.h"
#include "RawTableDefV9.g.h"
#include "RawMiscModuleExportV9.g.h"
#include "RawReducerDefV9.g.h"
#include "RawRowLevelSecurityDefV9.g.h"
#include "RawTableDefV9.g.h"
#include "RawTypeDefV9.g.h"
#include "Typespace.g.h"
#include "RawMiscModuleExportV9.g.h"
#include "RawRowLevelSecurityDefV9.g.h"

namespace SpacetimeDB::Internal {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
#include <memory>
#include "../autogen_base.h"
#include "spacetimedb/bsatn/bsatn.h"
#include "AlgebraicType.g.h"
#include "FunctionVisibility.g.h"
#include "ProductType.g.h"
#include "AlgebraicType.g.h"

namespace SpacetimeDB::Internal {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
#include <memory>
#include "../autogen_base.h"
#include "spacetimedb/bsatn/bsatn.h"
#include "AlgebraicType.g.h"
#include "FunctionVisibility.g.h"
#include "ProductType.g.h"
#include "AlgebraicType.g.h"

namespace SpacetimeDB::Internal {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
#include <memory>
#include "../autogen_base.h"
#include "spacetimedb/bsatn/bsatn.h"
#include "ProductType.g.h"
#include "Lifecycle.g.h"
#include "ProductType.g.h"

namespace SpacetimeDB::Internal {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ SPACETIMEDB_INTERNAL_PRODUCT_TYPE(RawSubmoduleV10) {
std::string namespace_;
std::shared_ptr<SpacetimeDB::Internal::RawModuleDefV10> module;

void bsatn_serialize(::SpacetimeDB::bsatn::Writer& writer) const {
::SpacetimeDB::bsatn::serialize(writer, namespace_);
::SpacetimeDB::bsatn::serialize(writer, *module);
}
SPACETIMEDB_PRODUCT_TYPE_EQUALITY(namespace_, module)
void bsatn_serialize(::SpacetimeDB::bsatn::Writer& writer) const;
bool operator==(const RawSubmoduleV10& other) const;
bool operator!=(const RawSubmoduleV10& other) const;
};
} // namespace SpacetimeDB::Internal
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
#include <memory>
#include "../autogen_base.h"
#include "spacetimedb/bsatn/bsatn.h"
#include "TableType.g.h"
#include "TableAccess.g.h"
#include "RawIndexDefV10.g.h"
#include "RawColumnDefaultValueV10.g.h"
#include "RawConstraintDefV10.g.h"
#include "RawIndexDefV10.g.h"
#include "RawSequenceDefV10.g.h"
#include "RawColumnDefaultValueV10.g.h"
#include "TableAccess.g.h"
#include "TableType.g.h"

namespace SpacetimeDB::Internal {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
#include <memory>
#include "../autogen_base.h"
#include "spacetimedb/bsatn/bsatn.h"
#include "RawSequenceDefV8.g.h"
#include "RawIndexDefV8.g.h"
#include "RawConstraintDefV8.g.h"
#include "RawColumnDefV8.g.h"
#include "RawConstraintDefV8.g.h"
#include "RawIndexDefV8.g.h"
#include "RawSequenceDefV8.g.h"

namespace SpacetimeDB::Internal {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
#include "../autogen_base.h"
#include "spacetimedb/bsatn/bsatn.h"
#include "RawConstraintDefV9.g.h"
#include "RawSequenceDefV9.g.h"
#include "RawIndexDefV9.g.h"
#include "RawScheduleDefV9.g.h"
#include "RawSequenceDefV9.g.h"
#include "TableAccess.g.h"
#include "TableType.g.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
#include <memory>
#include "../autogen_base.h"
#include "spacetimedb/bsatn/bsatn.h"
#include "ProductType.g.h"
#include "AlgebraicType.g.h"
#include "ProductType.g.h"

namespace SpacetimeDB::Internal {

Expand Down
10 changes: 7 additions & 3 deletions crates/bindings-cpp/include/spacetimedb/internal/autogen_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class TaggedEnumBase {
return std::get<Index>(data_);
}

// Get variant by index (mutable)
// Get variant by index (mutable)
template<size_t Index>
auto& get() {
return std::get<Index>(data_);
Expand Down Expand Up @@ -90,8 +90,12 @@ class TaggedEnumBase {
using Base::set; \
using Base::is; \
using Base::visit; \
using Base::operator==; \
using Base::operator!=; \
bool operator==(const TypeName& other) const noexcept { \
return static_cast<const Base&>(*this) == static_cast<const Base&>(other); \
} \
bool operator!=(const TypeName& other) const noexcept { \
return !(*this == other); \
} \
\
void bsatn_serialize(::SpacetimeDB::bsatn::Writer& writer) const { \
/* Write tag (variant index) */ \
Expand Down
45 changes: 43 additions & 2 deletions crates/codegen/examples/regen-cpp-moduledef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,40 @@ use spacetimedb_lib::RawModuleDef;
use spacetimedb_schema::def::ModuleDef;
use std::path::Path;

fn replace_required(code: String, from: &str, to: &str) -> anyhow::Result<String> {
if !code.contains(from) {
anyhow::bail!("expected generated C++ moduledef snippet was not found: {from:?}");
}
Ok(code.replace(from, to))
}

fn rewrite_raw_submodule_v10(code: String) -> anyhow::Result<String> {
let code = replace_required(code, "#include \"RawModuleDefV10.g.h\"\n", "")?;
let code = replace_required(
code,
"namespace SpacetimeDB::Internal {\n\nSPACETIMEDB_INTERNAL_PRODUCT_TYPE(RawSubmoduleV10)",
"namespace SpacetimeDB::Internal {\nstruct RawModuleDefV10;\n} // namespace SpacetimeDB::Internal\n\nnamespace SpacetimeDB::Internal {\n\nSPACETIMEDB_INTERNAL_PRODUCT_TYPE(RawSubmoduleV10)",
)?;
let code = replace_required(
code,
" SpacetimeDB::Internal::RawModuleDefV10 module;",
" std::shared_ptr<SpacetimeDB::Internal::RawModuleDefV10> module;",
)?;
replace_required(
code,
" void bsatn_serialize(::SpacetimeDB::bsatn::Writer& writer) const {\n ::SpacetimeDB::bsatn::serialize(writer, namespace_);\n ::SpacetimeDB::bsatn::serialize(writer, module);\n }\n SPACETIMEDB_PRODUCT_TYPE_EQUALITY(namespace_, module)",
" void bsatn_serialize(::SpacetimeDB::bsatn::Writer& writer) const;\n bool operator==(const RawSubmoduleV10& other) const;\n bool operator!=(const RawSubmoduleV10& other) const;",
)
}

fn append_raw_submodule_v10_definitions(code: String) -> anyhow::Result<String> {
replace_required(
code,
"\n};\n} // namespace SpacetimeDB::Internal\n",
"\n};\n\ninline void RawSubmoduleV10::bsatn_serialize(::SpacetimeDB::bsatn::Writer& writer) const {\n ::SpacetimeDB::bsatn::serialize(writer, namespace_);\n ::SpacetimeDB::bsatn::serialize(writer, *module);\n}\n\ninline bool RawSubmoduleV10::operator==(const RawSubmoduleV10& other) const {\n return namespace_ == other.namespace_ && module == other.module;\n}\n\ninline bool RawSubmoduleV10::operator!=(const RawSubmoduleV10& other) const {\n return !(*this == other);\n}\n} // namespace SpacetimeDB::Internal\n",
)
}

fn main() -> anyhow::Result<()> {
let mut builder = RawModuleDefV10Builder::new();
builder.add_type::<RawModuleDef>();
Expand Down Expand Up @@ -39,16 +73,23 @@ fn main() -> anyhow::Result<()> {
&CodegenOptions::default(),
)
.into_iter()
.try_for_each(|OutputFile { filename, code }| {
.try_for_each(|OutputFile { filename, code }| -> anyhow::Result<()> {
// Remove any prefix and just use the filename
let filename = if let Some(name) = filename.strip_prefix("Types/") {
name
} else {
&filename
};

let code = match filename {
"RawSubmoduleV10.g.h" => rewrite_raw_submodule_v10(code)?,
"RawModuleDefV10.g.h" => append_raw_submodule_v10_definitions(code)?,
_ => code,
};

println!("Generating {}", filename);
fs::write(dir.join(filename), code)
fs::write(dir.join(filename), code)?;
Ok(())
})?;

println!("C++ autogen files written to: {}", dir.display());
Expand Down
43 changes: 10 additions & 33 deletions crates/codegen/src/cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ impl<'opts> Cpp<'opts> {
}
}

fn is_recursive_mount_module_field(&self, type_name: &str, field_name: &str) -> bool {
type_name == "RawSubmoduleV10" && field_name == "module"
}

fn write_header_comment(&self, output: &mut String) {
writeln!(
output,
Expand Down Expand Up @@ -179,13 +175,7 @@ impl<'opts> Cpp<'opts> {
// Write fields only
for (field_name, field_type) in &product.elements {
write!(output, " ").unwrap();
if self.is_recursive_mount_module_field(type_name, field_name) {
// Temporary special-case to preserve the recursive RawSubmoduleV10 ->
// RawModuleDefV10 shape while breaking the include cycle in generated C++.
write!(output, "std::shared_ptr<{}::RawModuleDefV10>", self.namespace).unwrap();
} else {
self.write_algebraic_type(output, module, field_type).unwrap();
}
self.write_algebraic_type(output, module, field_type).unwrap();
write!(output, " ").unwrap();
self.write_cpp_field_name(output, field_name).unwrap();
writeln!(output, ";").unwrap();
Expand All @@ -200,24 +190,14 @@ impl<'opts> Cpp<'opts> {
)
.unwrap();
for (field_name, _) in &product.elements {
if self.is_recursive_mount_module_field(type_name, field_name) {
write!(output, " ::SpacetimeDB::bsatn::serialize(writer, *").unwrap();
self.write_cpp_field_name(output, field_name).unwrap();
writeln!(output, ");").unwrap();
} else {
write!(output, " ::SpacetimeDB::bsatn::serialize(writer, ").unwrap();
self.write_cpp_field_name(output, field_name).unwrap();
writeln!(output, ");").unwrap();
}
write!(output, " ::SpacetimeDB::bsatn::serialize(writer, ").unwrap();
self.write_cpp_field_name(output, field_name).unwrap();
writeln!(output, ");").unwrap();
}
writeln!(output, " }}").unwrap();

// Generate equality method
if type_name == "RawSubmoduleV10" {
// Pointer equality is sufficient for this internal autogen type. Mounts are not
// emitted by the C++ module path yet; this exists to keep the schema shape aligned.
writeln!(output, " SPACETIMEDB_PRODUCT_TYPE_EQUALITY(namespace_, module)").unwrap();
} else if !product.elements.is_empty() {
if !product.elements.is_empty() {
write!(output, " SPACETIMEDB_PRODUCT_TYPE_EQUALITY(").unwrap();
for (i, (field_name, _)) in product.elements.iter().enumerate() {
if i > 0 {
Expand Down Expand Up @@ -490,7 +470,8 @@ impl Lang for Cpp<'_> {

// Add includes for dependencies
if let Some(AlgebraicTypeDef::Product(product)) = module.typespace_for_generate().get(table.product_type_ref) {
let deps = self.collect_product_dependencies(module, product);
let mut deps: Vec<_> = self.collect_product_dependencies(module, product).into_iter().collect();
deps.sort();
for dep in deps {
if dep != table.name.to_string() {
writeln!(output, "#include \"{}.g.h\"", dep).unwrap();
Expand Down Expand Up @@ -540,19 +521,15 @@ impl Lang for Cpp<'_> {
};

let type_name = name.to_string();
let mut deps: Vec<_> = deps.into_iter().collect();
deps.sort();
for dep in deps {
if dep != type_name && !(type_name == "RawSubmoduleV10" && dep == "RawModuleDefV10") {
if dep != type_name {
writeln!(output, "#include \"{}.g.h\"", dep).unwrap();
}
}

writeln!(output).unwrap();
if type_name == "RawSubmoduleV10" {
writeln!(output, "namespace {} {{", self.namespace).unwrap();
writeln!(output, "struct RawModuleDefV10;").unwrap();
writeln!(output, "}} // namespace {}", self.namespace).unwrap();
writeln!(output).unwrap();
}
writeln!(output, "namespace {} {{", self.namespace).unwrap();
writeln!(output).unwrap();

Expand Down
Loading