Skip to content

Commit

Permalink
stabilize orders
Browse files Browse the repository at this point in the history
  • Loading branch information
Brat-vseznamus committed Jun 17, 2024
1 parent 2a8772a commit 294bcd4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 42 deletions.
88 changes: 47 additions & 41 deletions internal/tlcodegen/tlgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package tlcodegen

import (
"fmt"
"github.com/google/go-cmp/cmp"
"io"
"log"
"os"
Expand Down Expand Up @@ -694,6 +695,9 @@ func (gen *Gen2) WriteToDir(outdir string) error {
if string(was) == code {
notTouched++
continue
} else {
fmt.Printf("File \"%s\":\n", f)
fmt.Println(cmp.Diff(string(was), code))
}
}
if filepathName != TlJSONHTML { // not deterministic, do not write marker if json help changed
Expand All @@ -715,9 +719,9 @@ func (gen *Gen2) WriteToDir(outdir string) error {
}
for filepathName := range relativeFiles {
f := filepath.Join(outdir, filepathName)
//if strings.HasSuffix(f, ".o") {
// continue
//}
if strings.HasSuffix(f, ".o") {
continue
}
deleted++
if err := os.Remove(f); err != nil {
return fmt.Errorf("error deleting previous file %q: %w", f, err)
Expand Down Expand Up @@ -1141,6 +1145,10 @@ func GenerateCode(tl tlast.TL, options Gen2Options) (*Gen2, error) {
fmt.Printf("prevented unwrap of %v\n", v.tlName)
}
}

_, order := findAllTypesDependencyComponents(sortedTypes)
gen.componentsOrder = order

// in BeforeCodeGenerationStep we split recursion. Which links will be broken depends on order of nodes visited
for _, v := range sortedTypes {
if len(v.arguments) == 0 {
Expand All @@ -1150,36 +1158,6 @@ func GenerateCode(tl tlast.TL, options Gen2Options) (*Gen2, error) {
}
}

_, order := findAllTypesDependencyComponents(sortedTypes)
gen.componentsOrder = order

//deps, order := findAllTypesDependencyComponents(sortedTypes)
//gen.componentsOrder = order
//
//compoments := make(map[int][]*TypeRWWrapper)
//cmpnts := make([]int, 0)
//
//for _, v := range sortedTypes {
// if _, ok := compoments[v.typeComponent]; !ok {
// cmpnts = append(cmpnts, v.typeComponent)
// }
// compoments[v.typeComponent] = append(compoments[v.typeComponent], v)
//}
//
//sort.Ints(cmpnts)
//
//for i, _ := range cmpnts {
// cId := order[i]
// curDeps := make([]int, 0)
// for dep, _ := range deps[cId] {
// curDeps = append(curDeps, dep)
// }
// fmt.Printf("C[%d]: %v\n", cId, curDeps)
// for _, tp := range compoments[cId] {
// fmt.Printf("\tGo: %s, TL: %s\n", tp.goGlobalName, tp.tlName.String())
// }
//}

for _, v := range sortedTypes {
v.trw.BeforeCodeGenerationStep1()
}
Expand Down Expand Up @@ -1251,6 +1229,19 @@ func GenerateCode(tl tlast.TL, options Gen2Options) (*Gen2, error) {
return gen, nil
}

var TypeComparator = func(a, b *TypeRWWrapper) int {
return strings.Compare(a.goGlobalName, b.goGlobalName)
}

func stabilizeOrder(mp *map[*TypeRWWrapper][]*TypeRWWrapper) (keyOrder []*TypeRWWrapper) {
for k, v := range *mp {
slices.SortFunc(v, TypeComparator)
keyOrder = append(keyOrder, k)
}
slices.SortFunc(keyOrder, TypeComparator)
return
}

func findAllTypesDependencyComponents(types []*TypeRWWrapper) (map[int]map[int]bool, []int) {
dependencyGraph := make(map[*TypeRWWrapper][]*TypeRWWrapper)
reverseDependencyGraph := make(map[*TypeRWWrapper][]*TypeRWWrapper)
Expand All @@ -1263,6 +1254,9 @@ func findAllTypesDependencyComponents(types []*TypeRWWrapper) (map[int]map[int]b
}
}

_ = stabilizeOrder(&dependencyGraph)
_ = stabilizeOrder(&reverseDependencyGraph)

visitedTypes := make(map[*TypeRWWrapper]bool)
order := make([]*TypeRWWrapper, 0)
for _, tp := range types {
Expand Down Expand Up @@ -1303,12 +1297,27 @@ func findAllTypesDependencyComponents(types []*TypeRWWrapper) (map[int]map[int]b
}
}

componentsOrdered := make([]int, 0)
componentsDepsOrdered := make(map[int][]int)

for componentId, itsDeps := range componentsDeps {
componentsOrdered = append(componentsOrdered, componentId)
list := make([]int, len(itsDeps))
for dep, _ := range itsDeps {
list = append(list, dep)
}
sort.Ints(list)
componentsDepsOrdered[componentId] = list
}

sort.Ints(componentsOrdered)

compOrder := make([]int, 0)
compVisited := make(map[int]bool)

for cmp := range componentsDeps {
if !compVisited[cmp] {
sortComponents(cmp, &compVisited, &componentsDeps, &compOrder)
for comp := range componentsOrdered {
if !compVisited[comp] {
sortComponents(comp, &compVisited, &componentsDepsOrdered, &compOrder)
}
}

Expand Down Expand Up @@ -1345,9 +1354,9 @@ func findAllTypesDependencyComponentsStep2(
}
}

func sortComponents(target int, visited *map[int]bool, deps *map[int]map[int]bool, order *[]int) {
func sortComponents(target int, visited *map[int]bool, deps *map[int][]int, order *[]int) {
(*visited)[target] = true
for next, _ := range (*deps)[target] {
for _, next := range (*deps)[target] {
if !(*visited)[next] {
sortComponents(next, visited, deps, order)
}
Expand Down Expand Up @@ -1449,9 +1458,6 @@ func processCombinators(types map[string]*tlast.Combinator) *TypesInfo {
existingConstructors := make(map[ConstructorName]*Constructor)

for _, comb := range types {
//if comb.IsFunction {
// continue
//}
declaredType := comb.TypeDecl.Name
if comb.Builtin {
declaredType = comb.Construct.Name
Expand Down
3 changes: 3 additions & 0 deletions internal/tlcodegen/type_rw.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package tlcodegen
import (
"fmt"
"regexp"
"sort"
"strconv"
"strings"

Expand Down Expand Up @@ -246,6 +247,7 @@ func (d DirectIncludesCPP) sortedNames() []string {
for im := range d.ns { // Imports of this file.
sortedNames = append(sortedNames, im)
}
sort.Strings(sortedNames)
return sortedNames
}

Expand All @@ -263,6 +265,7 @@ func (d DirectIncludesCPP) sortedIncludes(componentOrder []int) (result []string
}

for _, files := range filesByCID {
sort.Strings(files)
for _, f := range files {
result = append(result, f)
}
Expand Down
6 changes: 5 additions & 1 deletion internal/tlcodegen/type_rw_struct_cpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package tlcodegen

import (
"fmt"
"golang.org/x/exp/slices"
"strings"
)

Expand Down Expand Up @@ -107,7 +108,10 @@ func (trw *TypeRWStruct) CPPGenerateCode(hpp *strings.Builder, hppInc *DirectInc
}

if !forwardDeclaration {
for _, typeDep := range trw.AllTypeDependencies() {
deps := trw.AllTypeDependencies()
slices.SortFunc(deps, TypeComparator)

for _, typeDep := range deps {
if typeDep.typeComponent == trw.wr.typeComponent {
typeDep.trw.CPPGenerateCode(hpp, nil, nil, nil, hppDetInc, nil, cppDetInc, bytesVersion, true)
}
Expand Down

0 comments on commit 294bcd4

Please sign in to comment.