Skip to content
Merged
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
23 changes: 16 additions & 7 deletions _generated/def.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ import (

type Block [32]byte

// test fixed-size struct
// size compilation
type Fixed struct {
A float64
B bool
}

type AliasedType = Fixed
type AliasedType2 = *Fixed
type AliasedType3 = uint64
type AliasedType4 = *uint64

// tests edge-cases with
// compiling size compilation.
type X struct {
Expand All @@ -42,13 +54,10 @@ type X struct {
ZCBytesMap map[string][]byte `msg:",zerocopy"`
ZCBytesMapDeep map[string]map[string][]byte `msg:",zerocopy"`
CustomBytes CustomBytes `msg:",zerocopy"`
}

// test fixed-size struct
// size compilation
type Fixed struct {
A float64
B bool
Renamed1 AliasedType
Renamed2 AliasedType2
Renamed3 AliasedType3
Renamed4 AliasedType4
}

type TestType struct {
Expand Down
28 changes: 27 additions & 1 deletion parse/getast.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type FileSet struct {
Package string // package name
Specs map[string]ast.Expr // type specs in file
Identities map[string]gen.Elem // processed from specs
Aliased map[string]string // Aliased types.
Directives []string // raw preprocessor directives
Imports []*ast.ImportSpec // imports
CompactFloats bool // Use smaller floats when feasible
Expand Down Expand Up @@ -121,6 +122,22 @@ func (fs *FileSet) applyDirectives() {
}
}
}
// Apply aliases last, so we don't overrule any manually specified replace directives.
for _, d := range fs.Aliased {
chunks := strings.Split(d, " ")
if len(chunks) > 0 {
if fn, ok := directives[chunks[0]]; ok {
pushstate(chunks[0])
err := fn(chunks, fs)
if err != nil {
warnf("directive error: %s", err)
}
popstate()
} else {
newdirs = append(newdirs, d)
}
}
}
fs.Directives = newdirs
}

Expand Down Expand Up @@ -318,6 +335,15 @@ func (fs *FileSet) getTypeSpecs(f *ast.File) {
for _, s := range g.Specs {
// for ast.TypeSpecs....
if ts, ok := s.(*ast.TypeSpec); ok {
// Handle type aliases, by adding a "replace" directive.
if ts.Assign != 0 {
if fs.Aliased == nil {
fs.Aliased = make(map[string]string)
}
fs.Aliased[ts.Name.Name] = fmt.Sprintf("replace %s with:%s", ts.Name.Name, stringify(ts.Type))
continue
}

switch ts.Type.(type) {
// this is the list of parse-able
// type specs
Expand Down Expand Up @@ -589,7 +615,7 @@ func (fs *FileSet) parseExpr(e ast.Expr) gen.Elem {
// can be done later, once we've resolved
// everything else.
if b.Value == gen.IDENT {
if _, ok := fs.Specs[e.Name]; !ok {
if _, ok := fs.Specs[e.Name]; !ok && fs.Aliased[e.Name] == "" {
warnf("possible non-local identifier: %s\n", e.Name)
}
}
Expand Down
Loading