Skip to content

Commit

Permalink
read draft
Browse files Browse the repository at this point in the history
  • Loading branch information
Brat-vseznamus committed Dec 28, 2024
1 parent 259cb2e commit 032f719
Show file tree
Hide file tree
Showing 12 changed files with 542 additions and 25 deletions.
4 changes: 4 additions & 0 deletions internal/tlcodegen/tlgen_kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ func (gen *Gen2) getType(lrc LocalResolveContext, t tlast.TypeRef, unionParent *
func (gen *Gen2) generateType(myWrapper *TypeRWWrapper) error {
tlType := myWrapper.origTL

if tlType[0].Construct.Name.String() == "test.dataIgnoreFlags" {
print("debug")
}

lrc := LocalResolveContext{
localTypeArgs: map[string]LocalTypeArg{},
localNatArgs: map[string]LocalNatArg{},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ import (
"strings"
)

type TypeRWCPPData interface {
CPPFillRecursiveChildren(visitedNodes map[*TypeRWWrapper]bool)
cppTypeStringInNamespace(bytesVersion bool, hppInc *DirectIncludesCPP) string
cppTypeStringInNamespaceHalfResolved2(bytesVersion bool, typeReduction EvaluatedType) string
cppTypeStringInNamespaceHalfResolved(bytesVersion bool, hppInc *DirectIncludesCPP, halfResolved HalfResolvedArgument) string
cppDefaultInitializer(halfResolved HalfResolvedArgument, halfResolve bool) string
CPPHasBytesVersion() bool
CPPTypeResettingCode(bytesVersion bool, val string) string
CPPTypeWritingJsonCode(bytesVersion bool, val string, bare bool, natArgs []string, last bool) string
CPPTypeWritingCode(bytesVersion bool, val string, bare bool, natArgs []string, last bool) string
CPPTypeReadingCode(bytesVersion bool, val string, bare bool, natArgs []string, last bool) string
CPPTypeJSONEmptyCondition(bytesVersion bool, val string, ref bool, deps []string) string
CPPGenerateCode(hpp *strings.Builder, hppInc *DirectIncludesCPP, hppIncFwd *DirectIncludesCPP, hppDet *strings.Builder, hppDetInc *DirectIncludesCPP, cppDet *strings.Builder, cppDetInc *DirectIncludesCPP, bytesVersion bool, forwardDeclaration bool)
}

func cppStartNamespace(s *strings.Builder, ns []string) {
for _, n := range ns {
s.WriteString(fmt.Sprintf("namespace %s { ", n))
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ import (
"strings"
)

type TypeRWPHPData interface {
PhpClassName(withPath bool, bare bool) string
PhpClassNameReplaced() bool
PhpTypeName(withPath bool, bare bool) string
PhpGenerateCode(code *strings.Builder, bytes bool) error
// PhpDefaultInit return not null type initialization value
PhpDefaultInit() string
// PhpDefaultValue return default value for field of this type (can be null)
PhpDefaultValue() string
PhpIterateReachableTypes(reachableTypes *map[*TypeRWWrapper]bool)
PhpReadMethodCall(targetName string, bare bool, args []string) []string
}

type PhpClassMeta struct {
UsedOnlyInInternal bool
UsedInFunctions bool
Expand Down Expand Up @@ -33,15 +46,20 @@ func (gen *Gen2) generateCodePHP(generateByteVersions []string) error {
createdTypes := make(map[string]bool)

for _, wrapper := range gen.generatedTypesList {
if wrapper.trw.PhpClassName(false, true) == "logs2_dictionarySetInfo" {
print("debug")
}
if createdTypes[wrapper.trw.PhpClassName(true, true)] {
continue
}
if !wrapper.PHPNeedsCode() {
continue
}
fmt.Println(fmt.Sprintf(

Check failure on line 55 in internal/tlcodegen/tlgen_lang_php.go

View workflow job for this annotation

GitHub Actions / ci-go

should use fmt.Printf instead of fmt.Println(fmt.Sprintf(...)) (but don't forget the newline) (S1038)

Check failure on line 55 in internal/tlcodegen/tlgen_lang_php.go

View workflow job for this annotation

GitHub Actions / ci-go

should use fmt.Printf instead of fmt.Println(fmt.Sprintf(...)) (but don't forget the newline) (S1038)
"PHP{%[1]s} in GO{%[2]s}",
wrapper.trw.PhpClassName(false, true),
wrapper.goGlobalName,
wrapper.NatParams,
wrapper.origTL[0].TemplateArguments,
wrapper.arguments),
)
err := phpGenerateCodeForWrapper(gen, wrapper, createdTypes, true, wrapper.PHPGenerateCode)
if err != nil {
return err
Expand Down Expand Up @@ -159,3 +177,11 @@ func PHPSpecialMembersTypes(wrapper *TypeRWWrapper) string {
}
return ""
}

func phpFormatArgs(args []string) string {
s := ""
for _, arg := range args {
s += ", " + arg
}
return s
}
102 changes: 80 additions & 22 deletions internal/tlcodegen/type_rw.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package tlcodegen

import (
"fmt"
"github.com/vkcom/tl/internal/utils"
"golang.org/x/exp/slices"
"regexp"
"strconv"
Expand Down Expand Up @@ -633,6 +634,84 @@ func (w *TypeRWWrapper) PHPNeedsCode() bool {
return !w.trw.PhpClassNameReplaced()
}

type TypeArgumentsTree struct {
value string
leaf bool
children []*TypeArgumentsTree
}

func (t *TypeArgumentsTree) IsEmpty() bool {
if t.leaf {
return false
}
for _, child := range t.children {
if child != nil && !child.IsEmpty() {
return false
}
}
return true
}

func (t *TypeArgumentsTree) EnumerateWithPrefixes() []string {
const natPrefix = ""
values := make([]string, 0)
t.enumerateWithPrefixes(&values, "")
values = utils.MapSlice(values, func(s string) string { return natPrefix + s })
return values
}

func (t *TypeArgumentsTree) EnumerateSubTreeWithPrefixes(childIndex int) []string {
if !(0 <= childIndex && childIndex < len(t.children)) {
panic("no such subtree")
}
ct := *t
ct.children = []*TypeArgumentsTree{t.children[childIndex]}
return ct.EnumerateWithPrefixes()
}

func (t *TypeArgumentsTree) enumerateWithPrefixes(values *[]string, curPrefix string) {
const delimiter = "_"
if t.leaf {
*values = append(*values, curPrefix)
} else {
for _, child := range t.children {
if child != nil {
prefix := curPrefix + child.value
if !child.leaf {
prefix += delimiter
}
child.enumerateWithPrefixes(values, prefix)
}
}
}
}

func (w *TypeRWWrapper) PHPGetNatTypeDependenciesDeclAsArray() []string {
t := TypeArgumentsTree{}
w.PHPGetNatTypeDependenciesDecl(&t)
return t.EnumerateWithPrefixes()
}

func (w *TypeRWWrapper) PHPGetNatTypeDependenciesDecl(tree *TypeArgumentsTree) {
for i, template := range w.origTL[0].TemplateArguments {
tree.children = append(tree.children, nil)
actualArg := w.arguments[i]
if template.IsNat {
tree.children[i] = &TypeArgumentsTree{}
tree.children[i].leaf = true
tree.children[i].value = template.FieldName
} else {
tree.children[i] = &TypeArgumentsTree{}
tree.children[i].leaf = false
tree.children[i].value = template.FieldName
actualArg.tip.PHPGetNatTypeDependenciesDecl(tree.children[i])
if tree.children[i].IsEmpty() {
tree.children[i] = nil
}
}
}
}

func (w *TypeRWWrapper) PhpIterateReachableTypes(reachableTypes *map[*TypeRWWrapper]bool) {
if (*reachableTypes)[w] {
return
Expand Down Expand Up @@ -877,15 +956,6 @@ outer:
return result
}

type TypeRWPHPData interface {
PhpClassName(withPath bool, bare bool) string
PhpClassNameReplaced() bool
PhpTypeName(withPath bool, bare bool) string
PhpGenerateCode(code *strings.Builder, bytes bool) error
PhpDefaultValue() string
PhpIterateReachableTypes(reachableTypes *map[*TypeRWWrapper]bool)
}

// TODO remove skipAlias after we start generating go code like we do for C++
type TypeRW interface {
// methods below are target language independent
Expand Down Expand Up @@ -918,19 +988,7 @@ type TypeRW interface {
typeJSON2ReadingCode(bytesVersion bool, directImports *DirectImports, ins *InternalNamespace, jvalue string, val string, natArgs []string, ref bool) string
GenerateCode(bytesVersion bool, directImports *DirectImports) string

CPPFillRecursiveChildren(visitedNodes map[*TypeRWWrapper]bool)
cppTypeStringInNamespace(bytesVersion bool, hppInc *DirectIncludesCPP) string
cppTypeStringInNamespaceHalfResolved2(bytesVersion bool, typeReduction EvaluatedType) string
cppTypeStringInNamespaceHalfResolved(bytesVersion bool, hppInc *DirectIncludesCPP, halfResolved HalfResolvedArgument) string
cppDefaultInitializer(halfResolved HalfResolvedArgument, halfResolve bool) string
CPPHasBytesVersion() bool
CPPTypeResettingCode(bytesVersion bool, val string) string
CPPTypeWritingJsonCode(bytesVersion bool, val string, bare bool, natArgs []string, last bool) string
CPPTypeWritingCode(bytesVersion bool, val string, bare bool, natArgs []string, last bool) string
CPPTypeReadingCode(bytesVersion bool, val string, bare bool, natArgs []string, last bool) string
CPPTypeJSONEmptyCondition(bytesVersion bool, val string, ref bool, deps []string) string
CPPGenerateCode(hpp *strings.Builder, hppInc *DirectIncludesCPP, hppIncFwd *DirectIncludesCPP, hppDet *strings.Builder, hppDetInc *DirectIncludesCPP, cppDet *strings.Builder, cppDetInc *DirectIncludesCPP, bytesVersion bool, forwardDeclaration bool)

TypeRWCPPData
TypeRWPHPData
}

Expand Down
21 changes: 21 additions & 0 deletions internal/tlcodegen/type_rw_bool_php.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,24 @@ func (trw *TypeRWBool) PhpDefaultValue() string {

func (trw *TypeRWBool) PhpIterateReachableTypes(reachableTypes *map[*TypeRWWrapper]bool) {
}

func (trw *TypeRWBool) PhpReadMethodCall(targetName string, bare bool, args []string) []string {
if !bare {
return []string{
fmt.Sprintf(
"[%[1]s, $success] = $stream->read_bool(0x%08[2]x, 0x%08[3]x);",
targetName,
trw.falseTag,
trw.trueTag,
),
"if (!$success) {",
" return false;",
"}",
}
}
return nil
}

func (trw *TypeRWBool) PhpDefaultInit() string {
return "false"
}
39 changes: 39 additions & 0 deletions internal/tlcodegen/type_rw_maybe_php.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,42 @@ func (trw *TypeRWMaybe) PhpDefaultValue() string {
func (trw *TypeRWMaybe) PhpIterateReachableTypes(reachableTypes *map[*TypeRWWrapper]bool) {
trw.element.t.PhpIterateReachableTypes(reachableTypes)
}

func (trw *TypeRWMaybe) PhpReadMethodCall(targetName string, bare bool, args []string) []string {
if !bare {
result := []string{
fmt.Sprintf(
"[$maybeContainsValue, $success] = $stream->read_bool(0x%08[1]x, 0x%08[2]x)",
trw.emptyTag,
trw.okTag,
),
"if (!$success) {",
" return false;",
"}",
"if ($maybeContainsValue) {",
}
if trw.element.t == trw.getInnerTarget().t {
result = append(result,
fmt.Sprintf(" if (%[1]s == null) {", targetName),
fmt.Sprintf(" %[1]s = %[2]s;", targetName, trw.element.t.trw.PhpDefaultInit()),
" }",
)
}
bodyReader := trw.element.t.trw.PhpReadMethodCall(targetName, trw.element.bare, args)
for i, _ := range bodyReader {

Check failure on line 63 in internal/tlcodegen/type_rw_maybe_php.go

View workflow job for this annotation

GitHub Actions / ci-go

unnecessary assignment to the blank identifier (S1005)

Check failure on line 63 in internal/tlcodegen/type_rw_maybe_php.go

View workflow job for this annotation

GitHub Actions / ci-go

unnecessary assignment to the blank identifier (S1005)
bodyReader[i] = " " + bodyReader[i]
}
result = append(result, bodyReader...)
result = append(result,
"} else {",
fmt.Sprintf(" %[1]s = null;", targetName),
"}",
)
return result
}
return nil
}

func (trw *TypeRWMaybe) PhpDefaultInit() string {
return trw.element.t.trw.PhpDefaultInit()
}
29 changes: 29 additions & 0 deletions internal/tlcodegen/type_rw_primitive_php.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,32 @@ func (trw *TypeRWPrimitive) PhpDefaultValue() string {
}

func (trw *TypeRWPrimitive) PhpIterateReachableTypes(reachableTypes *map[*TypeRWWrapper]bool) {}

func (trw *TypeRWPrimitive) phpIOMethodsSuffix() string {
switch trw.goType {
case "int32", "int64", "uint32", "string":
return trw.goType
case "float32":
return "float"
case "float64":
return "double"
default:
return fmt.Sprintf("<? %s>", trw.tlType)
}
}

func (trw *TypeRWPrimitive) PhpReadMethodCall(targetName string, bare bool, args []string) []string {
if !bare {
panic("can't be boxed")
}
return []string{
fmt.Sprintf("[%[1]s, $success] = $stream->read_%[2]s();", targetName, trw.phpIOMethodsSuffix()),
"if (!$success) {",
" return false;",
"}",
}
}

func (trw *TypeRWPrimitive) PhpDefaultInit() string {
return trw.PhpDefaultValue()
}
Loading

0 comments on commit 032f719

Please sign in to comment.