Skip to content

Commit

Permalink
exception-neutral cpp code
Browse files Browse the repository at this point in the history
  • Loading branch information
hrissan committed Jun 13, 2024
1 parent ca5705b commit d25cc22
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 351 deletions.
409 changes: 138 additions & 271 deletions internal/tlcodegen/helpers_cpp.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions internal/tlcodegen/type_rw.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ func (w *TypeRWWrapper) cppTypeStringInNamespace(bytesVersion bool, hppInc *Dire
} else {
bArgs.WriteString(", ")
}
if halfResolve {
if halfResolve && i < len(halfResolved.Args) {
half := halfResolved.Args[i]
if half.Name != "" {
bArgs.WriteString(half.Name)
Expand All @@ -442,7 +442,7 @@ func (w *TypeRWWrapper) cppTypeStringInNamespace(bytesVersion bool, hppInc *Dire
} else {
bArgs.WriteString(", ")
}
if halfResolve {
if halfResolve && i < len(halfResolved.Args) {
half := halfResolved.Args[i]
if half.Name != "" {
bArgs.WriteString(half.Name)
Expand Down
16 changes: 8 additions & 8 deletions internal/tlcodegen/type_rw_bool_cpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ func (trw *TypeRWBool) CPPTypeResettingCode(bytesVersion bool, val string) strin

func (trw *TypeRWBool) CPPTypeWritingCode(bytesVersion bool, val string, bare bool, natArgs []string, last bool) string {
goGlobalName := addBytes(trw.wr.goGlobalName, bytesVersion)
return fmt.Sprintf("\t::%s::%sWrite%s(s, %s%s);", trw.wr.gen.DetailsCPPNamespace, goGlobalName, addBare(bare), val, joinWithCommas(natArgs))
return fmt.Sprintf("\tif (!::%s::%sWrite%s(s, %s%s)) { return false; }", trw.wr.gen.DetailsCPPNamespace, goGlobalName, addBare(bare), val, joinWithCommas(natArgs))
}

func (trw *TypeRWBool) CPPTypeReadingCode(bytesVersion bool, val string, bare bool, natArgs []string, last bool) string {
goGlobalName := addBytes(trw.wr.goGlobalName, bytesVersion)
return fmt.Sprintf("\t::%s::%sRead%s(s, %s%s);", trw.wr.gen.DetailsCPPNamespace, goGlobalName, addBare(bare), val, joinWithCommas(natArgs))
return fmt.Sprintf("\tif (!::%s::%sRead%s(s, %s%s) { return false; }", trw.wr.gen.DetailsCPPNamespace, goGlobalName, addBare(bare), val, joinWithCommas(natArgs))
}

func (trw *TypeRWBool) CPPGenerateCode(hpp *strings.Builder, hppInc *DirectIncludesCPP, hppIncFwd *DirectIncludesCPP, hppDet *strings.Builder, hppDetInc *DirectIncludesCPP, cppDet *strings.Builder, cppDetInc *DirectIncludesCPP, bytesVersion bool, forwardDeclaration bool) {
Expand All @@ -61,17 +61,17 @@ func (trw *TypeRWBool) CPPGenerateCode(hpp *strings.Builder, hppInc *DirectInclu
cppStartNamespace(hppDet, trw.wr.gen.DetailsCPPNamespaceElements)

hppDet.WriteString(fmt.Sprintf(`
void %[1]sReadBoxed(::basictl::tl_istream & s, bool& item);
void %[1]sWriteBoxed(::basictl::tl_ostream & s, bool item);
bool %[1]sReadBoxed(::basictl::tl_istream & s, bool& item);
bool %[1]sWriteBoxed(::basictl::tl_ostream & s, bool item);
`, addBytes(trw.wr.goGlobalName, bytesVersion)))

cppDet.WriteString(fmt.Sprintf(`
void %[6]s::%[1]sReadBoxed(::basictl::tl_istream & s, bool& item) {
item = s.bool_read(0x%[2]x, 0x%[3]x);
bool %[6]s::%[1]sReadBoxed(::basictl::tl_istream & s, bool& item) {
return s.bool_read(item, 0x%[2]x, 0x%[3]x);
}
void %[6]s::%[1]sWriteBoxed(::basictl::tl_ostream & s, bool item) {
s.nat_write(item ? 0x%[3]x : 0x%[2]x);
bool %[6]s::%[1]sWriteBoxed(::basictl::tl_ostream & s, bool item) {
return s.nat_write(item ? 0x%[3]x : 0x%[2]x);
}
`, addBytes(trw.wr.goGlobalName, bytesVersion), trw.falseTag, trw.trueTag, trw.falseGoName, trw.trueGoName, trw.wr.gen.DetailsCPPNamespace))

Expand Down
26 changes: 15 additions & 11 deletions internal/tlcodegen/type_rw_maybe_cpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ func (trw *TypeRWMaybe) CPPFillRecursiveChildren(visitedNodes map[*TypeRWWrapper

func (trw *TypeRWMaybe) cppTypeStringInNamespace(bytesVersion bool, hppInc *DirectIncludesCPP) string {
hppInc.ns[trw.wr.fileName] = struct{}{}
return "::basictl::optional<" + trw.element.t.CPPTypeStringInNamespace(bytesVersion, hppInc) + ">"
return "std::optional<" + trw.element.t.CPPTypeStringInNamespace(bytesVersion, hppInc) + ">"
}

func (trw *TypeRWMaybe) cppTypeStringInNamespaceHalfResolved(bytesVersion bool, hppInc *DirectIncludesCPP, halfResolved HalfResolvedArgument) string {
hppInc.ns[trw.wr.fileName] = struct{}{}
return "::basictl::optional<" + trw.element.t.CPPTypeStringInNamespaceHalfResolved(bytesVersion, hppInc, halfResolved.Args[0]) + ">"
return "std::optional<" + trw.element.t.CPPTypeStringInNamespaceHalfResolved(bytesVersion, hppInc, halfResolved.Args[0]) + ">"
}

func (trw *TypeRWMaybe) cppDefaultInitializer(halfResolved HalfResolvedArgument, halfResolve bool) string {
Expand All @@ -39,13 +39,13 @@ func (trw *TypeRWMaybe) CPPTypeResettingCode(bytesVersion bool, val string) stri

func (trw *TypeRWMaybe) CPPTypeWritingCode(bytesVersion bool, val string, bare bool, natArgs []string, last bool) string {
goGlobalName := addBytes(trw.wr.goGlobalName, bytesVersion)
return fmt.Sprintf("\t::%s::%sWrite%s(s, %s%s);", trw.wr.gen.DetailsCPPNamespace, goGlobalName, addBare(bare), val, joinWithCommas(natArgs))
return fmt.Sprintf("\tif (!::%s::%sWrite%s(s, %s%s)) { return false; }", trw.wr.gen.DetailsCPPNamespace, goGlobalName, addBare(bare), val, joinWithCommas(natArgs))
//return wrapLast(last, fmt.Sprintf("\t%s.Write%s( w %s)", val, addBare(bare), formatNatArgsCallCPP(natArgs)))
}

func (trw *TypeRWMaybe) CPPTypeReadingCode(bytesVersion bool, val string, bare bool, natArgs []string, last bool) string {
goGlobalName := addBytes(trw.wr.goGlobalName, bytesVersion)
return fmt.Sprintf("\t::%s::%sRead%s(s, %s%s);", trw.wr.gen.DetailsCPPNamespace, goGlobalName, addBare(bare), val, joinWithCommas(natArgs))
return fmt.Sprintf("\tif (!::%s::%sRead%s(s, %s%s)) { return false; }", trw.wr.gen.DetailsCPPNamespace, goGlobalName, addBare(bare), val, joinWithCommas(natArgs))
//return wrapLast(last, fmt.Sprintf("\t%s.Read%s( r %s)", val, addBare(bare), formatNatArgsCallCPP(natArgs)))
}

Expand All @@ -56,8 +56,8 @@ func (trw *TypeRWMaybe) CPPGenerateCode(hpp *strings.Builder, hppInc *DirectIncl
cppStartNamespace(hppDet, trw.wr.gen.DetailsCPPNamespaceElements)

hppDet.WriteString(fmt.Sprintf(`
void %[1]sReadBoxed(::basictl::tl_istream & s, %[2]s& item%[3]s);
void %[1]sWriteBoxed(::basictl::tl_ostream & s, const %[2]s& item%[3]s);
bool %[1]sReadBoxed(::basictl::tl_istream & s, %[2]s& item%[3]s);
bool %[1]sWriteBoxed(::basictl::tl_ostream & s, const %[2]s& item%[3]s);
`, goGlobalName,
myFullType,
Expand All @@ -66,22 +66,26 @@ void %[1]sWriteBoxed(::basictl::tl_ostream & s, const %[2]s& item%[3]s);
trw.wr.tlTag))

cppDet.WriteString(fmt.Sprintf(`
void %[6]s::%[1]sReadBoxed(::basictl::tl_istream & s, %[2]s& item%[3]s) {
if (s.bool_read(0x%[4]x, 0x%[5]x)) {
bool %[6]s::%[1]sReadBoxed(::basictl::tl_istream & s, %[2]s& item%[3]s) {
bool has_item = false;
if (!s.bool_read(has_item, 0x%[4]x, 0x%[5]x)) { return false; }
if (has_item) {
if (!item) {
item.emplace();
}
%[7]s
return;
return true;
}
item.reset();
return true;
}
void %[6]s::%[1]sWriteBoxed(::basictl::tl_ostream & s, const %[2]s& item%[3]s) {
s.nat_write(item ? 0x%[5]x : 0x%[4]x);
bool %[6]s::%[1]sWriteBoxed(::basictl::tl_ostream & s, const %[2]s& item%[3]s) {
if (!s.nat_write(item ? 0x%[5]x : 0x%[4]x)) { return false; }
if (item) {
%[8]s
}
return true;
}
`,
goGlobalName,
Expand Down
4 changes: 2 additions & 2 deletions internal/tlcodegen/type_rw_primitive_cpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ func (trw *TypeRWPrimitive) CPPTypeResettingCode(bytesVersion bool, val string)
}

func (trw *TypeRWPrimitive) CPPTypeWritingCode(bytesVersion bool, val string, bare bool, natArgs []string, last bool) string {
return fmt.Sprintf("\ts.%s_write(%s);", trw.cppFunctionSuffix, val)
return fmt.Sprintf("\tif (!s.%s_write(%s)) { return false;}", trw.cppFunctionSuffix, val)
}

func (trw *TypeRWPrimitive) CPPTypeReadingCode(bytesVersion bool, val string, bare bool, natArgs []string, last bool) string {
return fmt.Sprintf("\ts.%s_read(%s);", trw.cppFunctionSuffix, val)
return fmt.Sprintf("\tif (!s.%s_read(%s)) { return false; }", trw.cppFunctionSuffix, val)
}

func (trw *TypeRWPrimitive) CPPGenerateCode(hpp *strings.Builder, hppInc *DirectIncludesCPP, hppIncFwd *DirectIncludesCPP, hppDet *strings.Builder, hppDetInc *DirectIncludesCPP, cppDet *strings.Builder, cppDetInc *DirectIncludesCPP, bytesVersion bool, forwardDeclaration bool) {
Expand Down
60 changes: 33 additions & 27 deletions internal/tlcodegen/type_rw_struct_cpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,23 @@ func (trw *TypeRWStruct) CPPTypeWritingCode(bytesVersion bool, val string, bare
if trw.isUnwrapType() {
prefix := ""
if !bare {
prefix = fmt.Sprintf("\ts.nat_write(0x%x);\n", trw.wr.tlTag)
prefix = fmt.Sprintf("\tif (!s.nat_write(0x%x)) { return false; }\n", trw.wr.tlTag)
}
return prefix + trw.Fields[0].t.trw.CPPTypeWritingCode(bytesVersion, val, trw.Fields[0].Bare(), trw.replaceUnwrapArgs(natArgs), last)
}
return fmt.Sprintf("\t::%s::%sWrite%s(s, %s%s);", trw.wr.gen.DetailsCPPNamespace, goGlobalName, addBare(bare), val, joinWithCommas(natArgs))
return fmt.Sprintf("\tif (!::%s::%sWrite%s(s, %s%s)) { return false; }", trw.wr.gen.DetailsCPPNamespace, goGlobalName, addBare(bare), val, joinWithCommas(natArgs))
}

func (trw *TypeRWStruct) CPPTypeReadingCode(bytesVersion bool, val string, bare bool, natArgs []string, last bool) string {
goGlobalName := addBytes(trw.wr.goGlobalName, bytesVersion)
if trw.isUnwrapType() {
prefix := ""
if !bare {
prefix = fmt.Sprintf("\ts.nat_read_exact_tag(0x%x);\n", trw.wr.tlTag)
prefix = fmt.Sprintf("\tif (!s.nat_read_exact_tag(0x%x)) { return false;}\n", trw.wr.tlTag)
}
return prefix + trw.Fields[0].t.trw.CPPTypeReadingCode(bytesVersion, val, trw.Fields[0].Bare(), trw.replaceUnwrapArgs(natArgs), last)
}
return fmt.Sprintf("\t::%s::%sRead%s(s, %s%s);", trw.wr.gen.DetailsCPPNamespace, goGlobalName, addBare(bare), val, joinWithCommas(natArgs))
return fmt.Sprintf("\tif (!::%s::%sRead%s(s, %s%s)) { return false; }", trw.wr.gen.DetailsCPPNamespace, goGlobalName, addBare(bare), val, joinWithCommas(natArgs))
}

func (trw *TypeRWStruct) CPPGenerateCode(hpp *strings.Builder, hppInc *DirectIncludesCPP, hppIncFwd *DirectIncludesCPP, hppDet *strings.Builder, hppDetInc *DirectIncludesCPP, cppDet *strings.Builder, cppDetInc *DirectIncludesCPP, bytesVersion bool, forwardDeclaration bool) {
Expand Down Expand Up @@ -132,27 +132,29 @@ func (trw *TypeRWStruct) CPPGenerateCode(hpp *strings.Builder, hppInc *DirectInc
}
if trw.wr.tlTag != 0 { // anonymous square brackets citizens or other exotic type
hpp.WriteString(fmt.Sprintf(`
::basictl::string_view tl_name() const { return "%s"; }
std::string_view tl_name() const { return "%s"; }
uint32_t tl_tag() const { return 0x%08x; }
`, trw.wr.tlName, trw.wr.tlTag))
}
if len(myArgsDecl) == 0 {
// cppStartNamespace(cppDet, trw.wr.gen.RootCPPNamespaceElements)
hpp.WriteString(fmt.Sprintf(`
void read(::basictl::tl_istream & s%[1]s);
void write(::basictl::tl_ostream & s%[1]s)const;
bool read(::basictl::tl_istream & s%[1]s);
bool write(::basictl::tl_ostream & s%[1]s)const;
`,
formatNatArgsDeclCPP(trw.wr.NatParams),
trw.CPPTypeResettingCode(bytesVersion, "*this"),
trw.CPPTypeReadingCode(bytesVersion, "*this", true, formatNatArgsAddNat(trw.wr.NatParams), true),
trw.CPPTypeWritingCode(bytesVersion, "*this", true, formatNatArgsAddNat(trw.wr.NatParams), true)))
cppDet.WriteString(fmt.Sprintf(`
void %[5]s::read(::basictl::tl_istream & s%[1]s) {
bool %[5]s::read(::basictl::tl_istream & s%[1]s) {
%[3]s
return true;
}
void %[5]s::write(::basictl::tl_ostream & s%[1]s)const {
bool %[5]s::write(::basictl::tl_ostream & s%[1]s)const {
%[4]s
return true;
}
`,
formatNatArgsDeclCPP(trw.wr.NatParams),
Expand All @@ -162,20 +164,22 @@ void %[5]s::write(::basictl::tl_ostream & s%[1]s)const {
myFullTypeNoPrefix))
if trw.wr.tlTag != 0 { // anonymous square brackets citizens or other exotic type
hpp.WriteString(fmt.Sprintf(`
void read_boxed(::basictl::tl_istream & s%[1]s);
void write_boxed(::basictl::tl_ostream & s%[1]s)const;
bool read_boxed(::basictl::tl_istream & s%[1]s);
bool write_boxed(::basictl::tl_ostream & s%[1]s)const;
`,
formatNatArgsDeclCPP(trw.wr.NatParams),
trw.CPPTypeResettingCode(bytesVersion, "*this"),
trw.CPPTypeReadingCode(bytesVersion, "*this", false, formatNatArgsAddNat(trw.wr.NatParams), true),
trw.CPPTypeWritingCode(bytesVersion, "*this", false, formatNatArgsAddNat(trw.wr.NatParams), true)))
cppDet.WriteString(fmt.Sprintf(`
void %[5]s::read_boxed(::basictl::tl_istream & s%[1]s) {
bool %[5]s::read_boxed(::basictl::tl_istream & s%[1]s) {
%[3]s
return true;
}
void %[5]s::write_boxed(::basictl::tl_ostream & s%[1]s)const {
bool %[5]s::write_boxed(::basictl::tl_ostream & s%[1]s)const {
%[4]s
return true;
}
`,
formatNatArgsDeclCPP(trw.wr.NatParams),
Expand All @@ -194,11 +198,13 @@ void %[5]s::write_boxed(::basictl::tl_ostream & s%[1]s)const {
void %[7]s::%[1]sReset(%[2]s& item) {
%[4]s}
void %[7]s::%[1]sRead(::basictl::tl_istream & s, %[2]s& item%[3]s) {
%[5]s}
bool %[7]s::%[1]sRead(::basictl::tl_istream & s, %[2]s& item%[3]s) {
%[5]s return true;
}
void %[7]s::%[1]sWrite(::basictl::tl_ostream & s, const %[2]s& item%[3]s) {
%[6]s}
bool %[7]s::%[1]sWrite(::basictl::tl_ostream & s, const %[2]s& item%[3]s) {
%[6]s return true;
}
`,
goGlobalName,
myFullType,
Expand All @@ -212,27 +218,27 @@ void %[7]s::%[1]sWrite(::basictl::tl_ostream & s, const %[2]s& item%[3]s) {
cppStartNamespace(hppDet, trw.wr.gen.DetailsCPPNamespaceElements)
hppDet.WriteString(fmt.Sprintf(`
void %[1]sReset(%[2]s& item);
void %[1]sRead(::basictl::tl_istream & s, %[2]s& item%[3]s);
void %[1]sWrite(::basictl::tl_ostream & s, const %[2]s& item%[3]s);
bool %[1]sRead(::basictl::tl_istream & s, %[2]s& item%[3]s);
bool %[1]sWrite(::basictl::tl_ostream & s, const %[2]s& item%[3]s);
`, goGlobalName, myFullType, formatNatArgsDeclCPP(trw.wr.NatParams)))

if trw.wr.tlTag != 0 { // anonymous square brackets citizens or other exotic type
hppDet.WriteString(fmt.Sprintf(`void %[1]sReadBoxed(::basictl::tl_istream & s, %[2]s& item%[3]s);
void %[1]sWriteBoxed(::basictl::tl_ostream & s, const %[2]s& item%[3]s);
hppDet.WriteString(fmt.Sprintf(`bool %[1]sReadBoxed(::basictl::tl_istream & s, %[2]s& item%[3]s);
bool %[1]sWriteBoxed(::basictl::tl_ostream & s, const %[2]s& item%[3]s);
`,
goGlobalName,
myFullType,
formatNatArgsDeclCPP(trw.wr.NatParams)))

cppDet.WriteString(fmt.Sprintf(`
void %[7]s::%[1]sReadBoxed(::basictl::tl_istream & s, %[2]s& item%[3]s) {
s.nat_read_exact_tag(0x%08[9]x);
%[1]sRead(s, item%[8]s);
bool %[7]s::%[1]sReadBoxed(::basictl::tl_istream & s, %[2]s& item%[3]s) {
if (!s.nat_read_exact_tag(0x%08[9]x)) { return false; }
return %[1]sRead(s, item%[8]s);
}
void %[7]s::%[1]sWriteBoxed(::basictl::tl_ostream & s, const %[2]s& item%[3]s) {
s.nat_write(0x%08[9]x);
%[1]sWrite(s, item%[8]s);
bool %[7]s::%[1]sWriteBoxed(::basictl::tl_ostream & s, const %[2]s& item%[3]s) {
if (!s.nat_write(0x%08[9]x)) { return false; }
return %[1]sWrite(s, item%[8]s);
}
`,
goGlobalName,
Expand Down
Loading

0 comments on commit d25cc22

Please sign in to comment.