Skip to content

Commit ae1aabc

Browse files
authored
Handle aliased types (#406)
Allow for aliased types. By far the cleanest solution is to just use a replace directive. Fixes #250
1 parent 12a3436 commit ae1aabc

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

_generated/def.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ import (
2424

2525
type Block [32]byte
2626

27+
// test fixed-size struct
28+
// size compilation
29+
type Fixed struct {
30+
A float64
31+
B bool
32+
}
33+
34+
type AliasedType = Fixed
35+
type AliasedType2 = *Fixed
36+
type AliasedType3 = uint64
37+
type AliasedType4 = *uint64
38+
2739
// tests edge-cases with
2840
// compiling size compilation.
2941
type X struct {
@@ -42,13 +54,10 @@ type X struct {
4254
ZCBytesMap map[string][]byte `msg:",zerocopy"`
4355
ZCBytesMapDeep map[string]map[string][]byte `msg:",zerocopy"`
4456
CustomBytes CustomBytes `msg:",zerocopy"`
45-
}
46-
47-
// test fixed-size struct
48-
// size compilation
49-
type Fixed struct {
50-
A float64
51-
B bool
57+
Renamed1 AliasedType
58+
Renamed2 AliasedType2
59+
Renamed3 AliasedType3
60+
Renamed4 AliasedType4
5261
}
5362

5463
type TestType struct {

parse/getast.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type FileSet struct {
1919
Package string // package name
2020
Specs map[string]ast.Expr // type specs in file
2121
Identities map[string]gen.Elem // processed from specs
22+
Aliased map[string]string // Aliased types.
2223
Directives []string // raw preprocessor directives
2324
Imports []*ast.ImportSpec // imports
2425
CompactFloats bool // Use smaller floats when feasible
@@ -121,6 +122,22 @@ func (fs *FileSet) applyDirectives() {
121122
}
122123
}
123124
}
125+
// Apply aliases last, so we don't overrule any manually specified replace directives.
126+
for _, d := range fs.Aliased {
127+
chunks := strings.Split(d, " ")
128+
if len(chunks) > 0 {
129+
if fn, ok := directives[chunks[0]]; ok {
130+
pushstate(chunks[0])
131+
err := fn(chunks, fs)
132+
if err != nil {
133+
warnf("directive error: %s", err)
134+
}
135+
popstate()
136+
} else {
137+
newdirs = append(newdirs, d)
138+
}
139+
}
140+
}
124141
fs.Directives = newdirs
125142
}
126143

@@ -318,6 +335,15 @@ func (fs *FileSet) getTypeSpecs(f *ast.File) {
318335
for _, s := range g.Specs {
319336
// for ast.TypeSpecs....
320337
if ts, ok := s.(*ast.TypeSpec); ok {
338+
// Handle type aliases, by adding a "replace" directive.
339+
if ts.Assign != 0 {
340+
if fs.Aliased == nil {
341+
fs.Aliased = make(map[string]string)
342+
}
343+
fs.Aliased[ts.Name.Name] = fmt.Sprintf("replace %s with:%s", ts.Name.Name, stringify(ts.Type))
344+
continue
345+
}
346+
321347
switch ts.Type.(type) {
322348
// this is the list of parse-able
323349
// type specs
@@ -589,7 +615,7 @@ func (fs *FileSet) parseExpr(e ast.Expr) gen.Elem {
589615
// can be done later, once we've resolved
590616
// everything else.
591617
if b.Value == gen.IDENT {
592-
if _, ok := fs.Specs[e.Name]; !ok {
618+
if _, ok := fs.Specs[e.Name]; !ok && fs.Aliased[e.Name] == "" {
593619
warnf("possible non-local identifier: %s\n", e.Name)
594620
}
595621
}

0 commit comments

Comments
 (0)