diff --git a/.gitignore b/.gitignore index 61f85782..05492a05 100644 --- a/.gitignore +++ b/.gitignore @@ -23,4 +23,7 @@ go.work /target /.tmp -/.idea \ No newline at end of file +/.idea + +/internal/tlcodegen/test/gen/test_cpp +internal/tlcodegen/test/gen/cpp/ \ No newline at end of file diff --git a/Makefile b/Makefile index c8325722..91df20a1 100644 --- a/Makefile +++ b/Makefile @@ -136,11 +136,11 @@ qtpl: qtc -dir=./internal -skipLineComments; \ fi -# TODO: in progress... -#.PHONY: cpp -#cpp: build -# @./target/bin/tlgen -language=cpp -v \ -# --outdir=./test/cpp \ -# --basicPkgPath=gitlab.mvk.com/go/vkgo/pkg/basictl \ -# ./test/cpp.tl -# g++ -o test/test_cpp test/test_cpp.cpp test/cpp/all.cpp -std=c++17 -O3 -Wno-noexcept-type -g -Wall -Wextra -Werror=return-type -Wno-unused-parameter +.PHONY: cpp +cpp: build + @./target/bin/tlgen -language=cpp -v \ + --outdir=./$(GEN_PATH)/cpp \ + --generateRPCCode=false \ + --basicPkgPath=$(BASIC_TL_PATH) \ + ./$(TLS_PATH)/cpp.tl + g++ -o $(GEN_PATH)/test_cpp $(GEN_PATH)/test_cpp.cpp $(GEN_PATH)/cpp/all.cpp -std=c++17 -O3 -Wno-noexcept-type -g -Wall -Wextra -Werror=return-type -Wno-unused-parameter diff --git a/internal/tlcodegen/test/gen/test_cpp.cpp b/internal/tlcodegen/test/gen/test_cpp.cpp new file mode 100644 index 00000000..855fe481 --- /dev/null +++ b/internal/tlcodegen/test/gen/test_cpp.cpp @@ -0,0 +1,40 @@ +#include +#include "cpp/a.top2.hpp" +#include "cpp/a.top3.hpp" + +std::string to_hex(const uint8_t *data, size_t count) { + static const char hexdigits[] = "0123456789abcdef"; + + std::string result(count * 2, char()); + for (size_t i = 0; i != count; ++i) { + uint8_t ch = data[i]; + result[i * 2] = hexdigits[(ch >> 4) & 0xf]; + result[i * 2 + 1] = hexdigits[ch & 0xf]; + } + return result; +} + +int main() { + basictl::tl_ostream_string str; + + tl2::a::Top2 top2; + + top2.write(str); + auto & buf = str.get_buffer(); + std::cout << top2.tl_name() << ": " << to_hex(reinterpret_cast(buf.data()), buf.size()) << std::endl; + + tl2::a::Top3 top3; + top3.n = 2; + // top3.a.a = "Hi!"; + top3.c.b.a.a.push_back(5); + top3.c.b.a.a.push_back(7); + + basictl::tl_ostream_string str2; + + top3.write(str2); + + auto & buf2 = str2.get_buffer(); + std::cout << top3.tl_name() << ": " << to_hex(reinterpret_cast(buf2.data()), buf2.size()) << std::endl; + + return 0; +} diff --git a/internal/tlcodegen/test/tls/cpp.tl b/internal/tlcodegen/test/tls/cpp.tl new file mode 100644 index 00000000..ba8c22bf --- /dev/null +++ b/internal/tlcodegen/test/tls/cpp.tl @@ -0,0 +1,7 @@ + +pair {X:Type} {Y:Type} a:X b:Y = Pair X Y; + +a.inner {I:#} a:I*[int] = a.Inner I; +a.middle {W:#} {P:Type} {Q:Type} a:(a.inner W) b:(pair P Q) = a.Middle W P Q; +a.top2 n:# m:# c:(a.middle m (pair (a.inner n) (a.inner n)) (a.Inner 3)) d:(a.Inner 4) = a.Top2; +a.top3 n:# m:# c:(a.middle 5 (a.inner n) (a.Inner 3)) = a.Top3; diff --git a/internal/tlcodegen/tlgen_kernel.go b/internal/tlcodegen/tlgen_kernel.go index c3c0ec1b..3afe2f2d 100644 --- a/internal/tlcodegen/tlgen_kernel.go +++ b/internal/tlcodegen/tlgen_kernel.go @@ -173,6 +173,7 @@ func (gen *Gen2) getType(lrc LocalResolveContext, t tlast.TypeRef, unionParent * exist, ok := gen.generatedTypes[canonicalName] if !ok { // log.Printf("adding canonical type: %s\n", canonicalName) + //log.Printf(" half resolved type: %v\n", halfResolved) gen.generatedTypes[canonicalName] = kernelType gen.generatedTypesList = append(gen.generatedTypesList, kernelType) // We added our type already, so others can reference it diff --git a/internal/tlcodegen/type_rw_tuple_cpp.go b/internal/tlcodegen/type_rw_tuple_cpp.go index f1cd94ed..be3c008b 100644 --- a/internal/tlcodegen/type_rw_tuple_cpp.go +++ b/internal/tlcodegen/type_rw_tuple_cpp.go @@ -39,13 +39,16 @@ func (trw *TypeRWBrackets) cppTypeStringInNamespaceHalfResolved(bytesVersion boo // trw.dictKeyField.t.CPPTypeStringInNamespace(bytesVersion, hppInc, trw.dictKeyField.resolvedType, halfResolve), // trw.dictValueField.t.CPPTypeStringInNamespace(bytesVersion, hppInc, trw.dictValueField.resolvedType, halfResolve)) //} - if trw.vectorLike || trw.dynamicSize { + if trw.vectorLike { return fmt.Sprintf("std::vector<%s>", trw.element.t.CPPTypeStringInNamespaceHalfResolved(bytesVersion, hppInc, halfResolved.Args[0])) } - if halfResolved.Args[1].Name != "" { - return fmt.Sprintf("std::array<%s, %s>", trw.element.t.CPPTypeStringInNamespaceHalfResolved(bytesVersion, hppInc, halfResolved.Args[0]), halfResolved.Args[1].Name) + if trw.dynamicSize { + return fmt.Sprintf("std::vector<%s>", trw.element.t.CPPTypeStringInNamespaceHalfResolved(bytesVersion, hppInc, halfResolved.Args[1])) + } + if halfResolved.Args[0].Name != "" { + return fmt.Sprintf("std::array<%s, %s>", trw.element.t.CPPTypeStringInNamespaceHalfResolved(bytesVersion, hppInc, halfResolved.Args[1]), halfResolved.Args[0].Name) } - return fmt.Sprintf("std::array<%s, %d>", trw.element.t.CPPTypeStringInNamespaceHalfResolved(bytesVersion, hppInc, halfResolved.Args[0]), trw.size) + return fmt.Sprintf("std::array<%s, %d>", trw.element.t.CPPTypeStringInNamespaceHalfResolved(bytesVersion, hppInc, halfResolved.Args[1]), trw.size) } func (trw *TypeRWBrackets) cppDefaultInitializer(halfResolved HalfResolvedArgument, halfResolve bool) string {