Add pkg to ast Func, Struct and TypeExpr nodes

This commit is contained in:
Nigel Tao committed 2017-12-29 12:08:17 +11:00
1 parent 10e4936f08
commit ed03c5943a
16 files changed
+311 -245

No files matched your search

+38 -22
View File
@@ -143,15 +143,15 @@ type Node struct {
// Expr operator literal/ident . Expr
// Field . . name Field
// File . . . File
// Func receiver . name Func
// Func funcName receiverPkg receiverName Func
// If . . . If
// Iterate . label . Iterate
// Jump keyword label . Jump
// PackageID . literal . PackageID
// Ret keyword . . Ret
// Status keyword pkg literal Status
// Struct . . name Struct
// TypeExpr decorator . name TypeExpr
// Struct . pkg name Struct
// TypeExpr decorator pkg name TypeExpr
// Use . literal . Use
// Var operator . name Var
// While . label . While
@@ -238,11 +238,21 @@ func (n *Raw) SetFilenameLine(f string, l uint32) { n.filename, n.line = f, l }
func (n *Raw) SetPackage(tm *t.Map, pkg t.ID) error {
return n.Node().Walk(func(o *Node) error {
switch o.Kind() {
case KConst, KStatus:
case KConst, KFunc, KStatus, KStruct, KTypeExpr:
if o.id1 != 0 {
return fmt.Errorf(`invalid SetPackage(%q) call: old package: got %q, want ""`,
pkg.Str(tm), o.id1.Str(tm))
}
// TODO: Add signed integer types, as per the builtin.Types var?
//
// Or, don't hard code these, and instead require built-in types to
// have qualified names, such as "builtin.u8" or "base.reader1"?
switch o.id2.Key() {
case t.KeyU8, t.KeyU16, t.KeyU32, t.KeyU64, t.KeyBool, t.KeyStatus, t.KeyReader1, t.KeyWriter1:
return nil
}
o.id1 = pkg
}
return nil
@@ -574,7 +584,8 @@ const MaxTypeExprDepth = 63
// TypeExpr is a type expression, such as "u32", "u32[..8]", "pkg.foo", "ptr
// T", "[8] T" or "[] T":
// - ID0: <0|package name|IDPtr|IDOpenBracket|IDColon>
// - ID0: <0|IDPtr|IDOpenBracket|IDColon|IDOpenParen>
// - ID1: <0|pkg>
// - ID2: <0|type name>
// - LHS: <nil|Expr>
// - MHS: <nil|Expr>
@@ -586,23 +597,25 @@ const MaxTypeExprDepth = 63
//
// An IDColon ID0 means "[] RHS". RHS is the inner type.
//
// An IDOpenParen ID0 means "LHS.ID1", a function or method type. LHS is the
// An IDOpenParen ID0 means "LHS.ID2", a function or method type. LHS is the
// receiver type, which may be nil. If non-nil, it will be a pointee type: "T"
// instead of "ptr T", "ptr ptr T", etc.
//
// TODO: method effects: "foo" vs "foo!" vs "foo?".
//
// Other ID0 values mean a (possibly package-qualified) type like "pkg.foo" or
// "foo". ID0 is the "pkg" or zero, ID1 is the "foo". Such a type can be
// refined as "foo[LHS..MHS]". LHS and MHS are Expr's, possibly nil. For
// example, the LHS for "u32[..4095]" is nil.
// A zero ID0 means a (possibly package-qualified) type like "pkg.foo" or
// "foo". ID1 is the "pkg" or zero, ID2 is the "foo".
//
// TODO: struct types, list types.
// Numeric types can be refined as "foo[LHS..MHS]". LHS and MHS are Expr's,
// possibly nil. For example, the LHS for "u32[..4095]" is nil.
//
// TODO: struct types, list types, nptr vs ptr.
type TypeExpr Node
func (n *TypeExpr) Node() *Node { return (*Node)(n) }
func (n *TypeExpr) Decorator() t.ID { return n.id0 }
func (n *TypeExpr) Name() t.ID { return n.id2 }
func (n *TypeExpr) QID() t.QID { return t.QID{n.id1, n.id2} }
func (n *TypeExpr) FuncName() t.ID { return n.id2 }
func (n *TypeExpr) ArrayLength() *Expr { return n.lhs.Expr() }
func (n *TypeExpr) Receiver() *TypeExpr { return n.lhs.TypeExpr() }
func (n *TypeExpr) Bounds() [2]*Expr { return [2]*Expr{n.lhs.Expr(), n.mhs.Expr()} }
@@ -676,10 +689,11 @@ func (n *TypeExpr) Unrefined() *TypeExpr {
return &o
}
func NewTypeExpr(decorator t.ID, name t.ID, alenRecvMin *Node, max *Expr, inner *TypeExpr) *TypeExpr {
func NewTypeExpr(decorator t.ID, pkg t.ID, name t.ID, alenRecvMin *Node, max *Expr, inner *TypeExpr) *TypeExpr {
return &TypeExpr{
kind: KTypeExpr,
id0: decorator,
id1: pkg,
id2: name,
lhs: alenRecvMin,
mhs: max.Node(),
@@ -694,8 +708,9 @@ const MaxBodyDepth = 255
// - FlagsImpure is "ID1" vs "ID1!"
// - FlagsSuspendible is "ID1" vs "ID1?", it implies FlagsImpure
// - FlagsPublic is "pub" vs "pri"
// - ID0: <0|receiver>
// - ID2: name
// - ID0: funcName
// - ID1: <0|receiverPkg> (set by calling SetPackage)
// - ID2: <0|receiverName>
// - LHS: <Struct> in-parameters
// - RHS: <Struct> out-parameters
// - List1: <Assert> asserts
@@ -720,22 +735,22 @@ func (n *Func) Suspendible() bool { return n.flags&FlagsSuspendible != 0 }
func (n *Func) Public() bool { return n.flags&FlagsPublic != 0 }
func (n *Func) Filename() string { return n.filename }
func (n *Func) Line() uint32 { return n.line }
func (n *Func) QID() t.QID { return t.QID{n.id0, n.id2} }
func (n *Func) Receiver() t.ID { return n.id0 }
func (n *Func) Name() t.ID { return n.id2 }
func (n *Func) QQID() t.QQID { return t.QQID{n.id1, n.id2, n.id0} }
func (n *Func) Receiver() t.QID { return t.QID{n.id1, n.id2} }
func (n *Func) FuncName() t.ID { return n.id0 }
func (n *Func) In() *Struct { return n.lhs.Struct() }
func (n *Func) Out() *Struct { return n.rhs.Struct() }
func (n *Func) Asserts() []*Node { return n.list1 }
func (n *Func) Body() []*Node { return n.list2 }
func NewFunc(flags Flags, filename string, line uint32, receiver t.ID, name t.ID, in *Struct, out *Struct, asserts []*Node, body []*Node) *Func {
func NewFunc(flags Flags, filename string, line uint32, receiverName t.ID, funcName t.ID, in *Struct, out *Struct, asserts []*Node, body []*Node) *Func {
return &Func{
kind: KFunc,
flags: flags,
filename: filename,
line: line,
id0: receiver,
id2: name,
id0: funcName,
id2: receiverName,
lhs: in.Node(),
rhs: out.Node(),
list1: asserts,
@@ -799,6 +814,7 @@ func NewConst(flags Flags, filename string, line uint32, name t.ID, xType *TypeE
// Struct is "struct ID1(List0)":
// - FlagsSuspendible is "ID1" vs "ID1?"
// - FlagsPublic is "pub" vs "pri"
// - ID1: <0|pkg> (set by calling SetPackage)
// - ID2: name
// - List0: <Field> fields
type Struct Node
@@ -808,7 +824,7 @@ func (n *Struct) Suspendible() bool { return n.flags&FlagsSuspendible != 0 }
func (n *Struct) Public() bool { return n.flags&FlagsPublic != 0 }
func (n *Struct) Filename() string { return n.filename }
func (n *Struct) Line() uint32 { return n.line }
func (n *Struct) Name() t.ID { return n.id2 }
func (n *Struct) QID() t.QID { return t.QID{n.id1, n.id2} }
func (n *Struct) Fields() []*Node { return n.list0 }
func NewStruct(flags Flags, filename string, line uint32, name t.ID, fields []*Node) *Struct {
+6 -9
View File
@@ -22,14 +22,14 @@ func TopologicalSortStructs(ns []*Struct) (sorted []*Struct, ok bool) {
// Algorithm is a depth-first search as per
// https://en.wikipedia.org/wiki/Topological_sorting#Depth-first_search
sorted = make([]*Struct, 0, len(ns))
byName := map[t.ID]*Struct{}
byQID := map[t.QID]*Struct{}
for _, n := range ns {
byName[n.Name()] = n
byQID[n.QID()] = n
}
marks := map[*Struct]uint8{}
for _, n := range ns {
if _, ok := marks[n]; !ok {
sorted, ok = tssVisit(sorted, n, byName, marks)
sorted, ok = tssVisit(sorted, n, byQID, marks)
if !ok {
return nil, false
}
@@ -38,7 +38,7 @@ func TopologicalSortStructs(ns []*Struct) (sorted []*Struct, ok bool) {
return sorted, true
}
func tssVisit(dst []*Struct, n *Struct, byName map[t.ID]*Struct, marks map[*Struct]uint8) ([]*Struct, bool) {
func tssVisit(dst []*Struct, n *Struct, byQID map[t.QID]*Struct, marks map[*Struct]uint8) ([]*Struct, bool) {
const (
unmarked = 0
temporary = 1
@@ -54,12 +54,9 @@ func tssVisit(dst []*Struct, n *Struct, byName map[t.ID]*Struct, marks map[*Stru
for _, f := range n.Fields() {
x := f.Field().XType().Innermost()
if x.Decorator() != 0 {
continue
}
if o := byName[x.Name()]; o != nil {
if o := byQID[x.QID()]; o != nil {
var ok bool
dst, ok = tssVisit(dst, o, byName, marks)
dst, ok = tssVisit(dst, o, byQID, marks)
if !ok {
return nil, false
}
+4 -6
View File
@@ -176,7 +176,7 @@ func (n *TypeExpr) Str(tm *t.Map) string {
return ""
}
if n.Decorator() == 0 && n.Min() == nil && n.Max() == nil {
return tm.ByID(n.Name())
return n.QID().Str(tm)
}
return string(n.appendStr(nil, tm, 0))
}
@@ -192,7 +192,7 @@ func (n *TypeExpr) appendStr(buf []byte, tm *t.Map, depth uint32) []byte {
switch n.Decorator().Key() {
case 0:
buf = append(buf, tm.ByID(n.Name())...)
buf = append(buf, n.QID().Str(tm)...)
case t.KeyPtr:
buf = append(buf, "ptr "...)
return n.Inner().appendStr(buf, tm, depth)
@@ -208,11 +208,9 @@ func (n *TypeExpr) appendStr(buf []byte, tm *t.Map, depth uint32) []byte {
buf = append(buf, "func "...)
buf = n.Receiver().appendStr(buf, tm, depth)
buf = append(buf, '.')
return append(buf, tm.ByID(n.Name())...)
return append(buf, n.FuncName().Str(tm)...)
default:
buf = append(buf, tm.ByID(n.Decorator())...)
buf = append(buf, '.')
buf = append(buf, tm.ByID(n.Name())...)
return append(buf, "!invalid_type!"...)
}
if n.Min() != nil || n.Max() != nil {
buf = append(buf, '[')