case *types.Named:
name = typ.Obj().Name()
default:
- p.error("anonymous field expected")
+ p.error("embedded field expected")
}
}
}
}
}
-// update sets the type map entries for the given type numbers nlist to t.
-func (p *parser) update(t types.Type, nlist []int) {
+// update sets the type map entries for the entries in nlist to t.
+// An entry in nlist can be a type number in p.typeList,
+// used to resolve named types, or it can be a *types.Pointer,
+// used to resolve pointers to named types in case they are referenced
+// by embedded fields.
+func (p *parser) update(t types.Type, nlist []interface{}) {
if len(nlist) != 0 {
if t == reserved {
p.errorf("internal error: update(%v) invoked on reserved", nlist)
}
}
for _, n := range nlist {
- if p.typeList[n] == t {
- continue
- }
- if p.typeList[n] != reserved {
- p.errorf("internal error: update(%v): %d not reserved", nlist, n)
+ switch n := n.(type) {
+ case int:
+ if p.typeList[n] == t {
+ continue
+ }
+ if p.typeList[n] != reserved {
+ p.errorf("internal error: update(%v): %d not reserved", nlist, n)
+ }
+ p.typeList[n] = t
+ case *types.Pointer:
+ if *n != (types.Pointer{}) {
+ elem := n.Elem()
+ if elem == t {
+ continue
+ }
+ p.errorf("internal error: update: pointer already set to %v, expected %v", elem, t)
+ }
+ *n = *types.NewPointer(t)
+ default:
+ p.errorf("internal error: %T on nlist", n)
}
- p.typeList[n] = t
}
}
// NamedType = TypeName [ "=" ] Type { Method } .
// TypeName = ExportedName .
// Method = "func" "(" Param ")" Name ParamList ResultList [InlineBody] ";" .
-func (p *parser) parseNamedType(nlist []int) types.Type {
+func (p *parser) parseNamedType(nlist []interface{}) types.Type {
pkg, name := p.parseExportedName()
scope := pkg.Scope()
obj := scope.Lookup(name)
// type alias
if p.tok == '=' {
p.next()
- p.aliases[nlist[len(nlist)-1]] = name
+ p.aliases[nlist[len(nlist)-1].(int)] = name
if obj != nil {
// use the previously imported (canonical) type
t := obj.Type()
}
// ArrayOrSliceType = "[" [ int ] "]" Type .
-func (p *parser) parseArrayOrSliceType(pkg *types.Package, nlist []int) types.Type {
+func (p *parser) parseArrayOrSliceType(pkg *types.Package, nlist []interface{}) types.Type {
p.expect('[')
if p.tok == ']' {
p.next()
}
// MapType = "map" "[" Type "]" Type .
-func (p *parser) parseMapType(pkg *types.Package, nlist []int) types.Type {
+func (p *parser) parseMapType(pkg *types.Package, nlist []interface{}) types.Type {
p.expectKeyword("map")
t := new(types.Map)
}
// ChanType = "chan" ["<-" | "-<"] Type .
-func (p *parser) parseChanType(pkg *types.Package, nlist []int) types.Type {
+func (p *parser) parseChanType(pkg *types.Package, nlist []interface{}) types.Type {
p.expectKeyword("chan")
t := new(types.Chan)
}
// StructType = "struct" "{" { Field } "}" .
-func (p *parser) parseStructType(pkg *types.Package, nlist []int) types.Type {
+func (p *parser) parseStructType(pkg *types.Package, nlist []interface{}) types.Type {
p.expectKeyword("struct")
t := new(types.Struct)
}
// FunctionType = ParamList ResultList .
-func (p *parser) parseFunctionType(pkg *types.Package, nlist []int) *types.Signature {
+func (p *parser) parseFunctionType(pkg *types.Package, nlist []interface{}) *types.Signature {
t := new(types.Signature)
p.update(t, nlist)
}
// InterfaceType = "interface" "{" { ("?" Type | Func) ";" } "}" .
-func (p *parser) parseInterfaceType(pkg *types.Package, nlist []int) types.Type {
+func (p *parser) parseInterfaceType(pkg *types.Package, nlist []interface{}) types.Type {
p.expectKeyword("interface")
t := new(types.Interface)
}
// PointerType = "*" ("any" | Type) .
-func (p *parser) parsePointerType(pkg *types.Package, nlist []int) types.Type {
+func (p *parser) parsePointerType(pkg *types.Package, nlist []interface{}) types.Type {
p.expect('*')
if p.tok == scanner.Ident {
p.expectKeyword("any")
t := new(types.Pointer)
p.update(t, nlist)
- *t = *types.NewPointer(p.parseType(pkg))
+ *t = *types.NewPointer(p.parseType(pkg, t))
return t
}
// TypeSpec = NamedType | MapType | ChanType | StructType | InterfaceType | PointerType | ArrayOrSliceType | FunctionType .
-func (p *parser) parseTypeSpec(pkg *types.Package, nlist []int) types.Type {
+func (p *parser) parseTypeSpec(pkg *types.Package, nlist []interface{}) types.Type {
switch p.tok {
case scanner.String:
return p.parseNamedType(nlist)
//
// parseType updates the type map to t for all type numbers n.
//
-func (p *parser) parseType(pkg *types.Package, n ...int) types.Type {
+func (p *parser) parseType(pkg *types.Package, n ...interface{}) types.Type {
p.expect('<')
t, _ := p.parseTypeAfterAngle(pkg, n...)
return t
}
// (*parser).Type after reading the "<".
-func (p *parser) parseTypeAfterAngle(pkg *types.Package, n ...int) (t types.Type, n1 int) {
+func (p *parser) parseTypeAfterAngle(pkg *types.Package, n ...interface{}) (t types.Type, n1 int) {
p.expectKeyword("type")
n1 = 0
// parseTypeExtended is identical to parseType, but if the type in
// question is a saved type, returns the index as well as the type
// pointer (index returned is zero if we parsed a builtin).
-func (p *parser) parseTypeExtended(pkg *types.Package, n ...int) (t types.Type, n1 int) {
+func (p *parser) parseTypeExtended(pkg *types.Package, n ...interface{}) (t types.Type, n1 int) {
p.expect('<')
t, n1 = p.parseTypeAfterAngle(pkg, n...)
return
}
for i := 1; i < int(exportedp1); i++ {
- p.parseSavedType(pkg, i, []int{})
+ p.parseSavedType(pkg, i, nil)
}
}
// parseSavedType parses one saved type definition.
-func (p *parser) parseSavedType(pkg *types.Package, i int, nlist []int) {
+func (p *parser) parseSavedType(pkg *types.Package, i int, nlist []interface{}) {
defer func(s *scanner.Scanner, tok rune, lit string) {
p.scanner = s
p.tok = tok