// - simplify invalid handling: maybe just use Typ[Invalid] as marker, get rid of invalid Mode for values?
// - rethink error handling: should all callers check if x.mode == valid after making a call?
-func (check *checker) tag(field *ast.Field) string {
- if t := field.Tag; t != nil {
- assert(t.Kind == token.STRING)
- if tag, err := strconv.Unquote(t.Value); err == nil {
- return tag
+func (check *checker) collectParams(list *ast.FieldList) (params ObjList, isVariadic bool) {
+ if list == nil {
+ return
+ }
+ for _, field := range list.List {
+ ftype := field.Type
+ if t, ok := ftype.(*ast.Ellipsis); ok {
+ ftype = t.Elt
+ isVariadic = true
+ }
+ // the parser ensures that f.Tag is nil and we don't
+ // care if a constructed AST contains a non-nil tag
+ typ := check.typ(ftype, true)
+ if len(field.Names) > 0 {
+ // named parameter
+ for _, name := range field.Names {
+ obj := name.Obj
+ obj.Type = typ
+ params = append(params, obj)
+ }
+ } else {
+ // anonymous parameter
+ obj := ast.NewObj(ast.Var, "")
+ obj.Type = typ
+ params = append(params, obj)
}
- check.invalidAST(t.Pos(), "incorrect tag syntax: %q", t.Value)
}
- return ""
+ return
}
-// collectFields collects interface methods (tok = token.INTERFACE), and function arguments/results (tok = token.FUNC).
-func (check *checker) collectFields(tok token.Token, list *ast.FieldList, cycleOk bool) (fields ObjList, tags []string, isVariadic bool) {
- if list != nil {
- for _, field := range list.List {
- ftype := field.Type
- if t, ok := ftype.(*ast.Ellipsis); ok {
- ftype = t.Elt
- isVariadic = true
+func (check *checker) collectMethods(list *ast.FieldList) (methods ObjList) {
+ if list == nil {
+ return
+ }
+ for _, f := range list.List {
+ typ := check.typ(f.Type, len(f.Names) > 0) // cycles are not ok for embedded interfaces
+ // the parser ensures that f.Tag is nil and we don't
+ // care if a constructed AST contains a non-nil tag
+ if len(f.Names) > 0 {
+ // methods (the parser ensures that there's only one
+ // and we don't care if a constructed AST has more)
+ if _, ok := typ.(*Signature); !ok {
+ check.invalidAST(f.Type.Pos(), "%s is not a method signature", typ)
+ continue
}
- typ := check.typ(ftype, cycleOk)
- tag := check.tag(field)
- if len(field.Names) > 0 {
- // named fields
- for _, name := range field.Names {
- obj := name.Obj
- obj.Type = typ
- fields = append(fields, obj)
- if tok == token.STRUCT {
- tags = append(tags, tag)
- }
- }
- } else {
- // anonymous field
- switch tok {
- case token.FUNC:
- obj := ast.NewObj(ast.Var, "")
- obj.Type = typ
- fields = append(fields, obj)
- case token.INTERFACE:
- utyp := underlying(typ)
- if typ, ok := utyp.(*Interface); ok {
- // TODO(gri) This is not good enough. Check for double declarations!
- fields = append(fields, typ.Methods...)
- } else if utyp != Typ[Invalid] {
- // if utyp is invalid, don't complain (the root cause was reported before)
- check.errorf(ftype.Pos(), "interface contains embedded non-interface type")
- }
- default:
- panic("unreachable")
- }
+ for _, name := range f.Names {
+ obj := name.Obj
+ obj.Type = typ
+ methods = append(methods, obj)
}
+ } else {
+ // embedded interface
+ utyp := underlying(typ)
+ if ityp, ok := utyp.(*Interface); ok {
+ methods = append(methods, ityp.Methods...)
+ } else if utyp != Typ[Invalid] {
+ // if utyp is invalid, don't complain (the root cause was reported before)
+ check.errorf(f.Type.Pos(), "%s is not an interface type", typ)
+ }
+ }
+ }
+ // check for double declarations
+ methods.Sort()
+ prev := ""
+ for _, obj := range methods {
+ if obj.Name == prev {
+ check.errorf(list.Pos(), "multiple methods named %s", prev)
+ return // keep multiple entries, lookup will only return the first entry
}
}
return
}
-func (check *checker) collectStructFields(list *ast.FieldList, cycleOk bool) (fields []*StructField) {
+func (check *checker) tag(t *ast.BasicLit) string {
+ if t != nil {
+ if t.Kind == token.STRING {
+ if val, err := strconv.Unquote(t.Value); err == nil {
+ return val
+ }
+ }
+ check.invalidAST(t.Pos(), "incorrect tag syntax: %q", t.Value)
+ }
+ return ""
+}
+
+func (check *checker) collectFields(list *ast.FieldList, cycleOk bool) (fields []*StructField) {
if list == nil {
return
}
for _, f := range list.List {
typ := check.typ(f.Type, cycleOk)
- tag := check.tag(f)
+ tag := check.tag(f.Tag)
if len(f.Names) > 0 {
// named fields
for _, name := range f.Names {
func (check *checker) op(m opPredicates, x *operand, op token.Token) bool {
if pred := m[op]; pred != nil {
if !pred(x.typ) {
- // TODO(gri) better error message for <-x where x is a send-only channel
- // (<- is defined but not permitted). Special-case here or
- // handle higher up.
check.invalidOp(x.pos(), "operator %s not defined for %s", op, x)
return false
}
}
case *ast.FuncLit:
- x.mode = value
- x.typ = check.typ(e.Type, false)
- // TODO(gri) handle errors (e.g. x.typ is not a *Signature)
- check.function(x.typ.(*Signature), e.Body)
+ if typ, ok := check.typ(e.Type, false).(*Signature); ok {
+ x.mode = value
+ x.typ = typ
+ check.function(typ, e.Body)
+ } else {
+ check.invalidAST(e.Pos(), "invalid function literal %s", e)
+ goto Error
+ }
case *ast.CompositeLit:
- // TODO(gri)
- // - determine element type if nil
- // - deal with map elements
- var typ Type
+ typ := hint
if e.Type != nil {
- // TODO(gri) Fix this - just to get going for now
typ = check.typ(e.Type, false)
}
- for _, e := range e.Elts {
- var x operand
- check.expr(&x, e, hint, iota)
- // TODO(gri) check assignment compatibility to element type
+ if typ == nil {
+ check.errorf(e.Pos(), "missing type in composite literal")
+ goto Error
+ }
+
+ // TODO(gri) try to factor code below better
+
+ switch utyp := underlying(deref(typ)).(type) {
+ case *Struct:
+ if len(e.Elts) == 0 {
+ break
+ }
+ fields := utyp.Fields
+ if _, ok := e.Elts[0].(*ast.KeyValueExpr); ok {
+ // all elements must have keys
+ visited := make([]bool, len(fields))
+ for _, e := range e.Elts {
+ kv, _ := e.(*ast.KeyValueExpr)
+ if kv == nil {
+ check.errorf(e.Pos(), "mixture of field:value and value elements in struct literal")
+ continue
+ }
+ key, _ := kv.Key.(*ast.Ident)
+ if key == nil {
+ check.errorf(kv.Pos(), "invalid field name %s in struct literal", kv.Key)
+ continue
+ }
+ i := utyp.fieldIndex(key.Name)
+ if i < 0 {
+ check.errorf(kv.Pos(), "unknown field %s in struct literal", key.Name)
+ continue
+ }
+ // 0 <= i < len(fields)
+ if visited[i] {
+ check.errorf(kv.Pos(), "duplicate field name %s in struct literal", key.Name)
+ continue
+ }
+ visited[i] = true
+ check.expr(x, kv.Value, nil, iota)
+ etyp := fields[i].Type
+ if !x.isAssignable(etyp) {
+ check.errorf(x.pos(), "cannot use %s as %s value in struct literal", x, etyp)
+ continue
+ }
+ }
+ } else {
+ // no element must have a key
+ for i, e := range e.Elts {
+ if kv, _ := e.(*ast.KeyValueExpr); kv != nil {
+ check.errorf(kv.Pos(), "mixture of field:value and value elements in struct literal")
+ continue
+ }
+ check.expr(x, e, nil, iota)
+ if i >= len(fields) {
+ check.errorf(x.pos(), "too many values in struct literal")
+ goto Error
+ }
+ etyp := fields[i].Type
+ if !x.isAssignable(etyp) {
+ check.errorf(x.pos(), "cannot use %s as an element of type %s in struct literal", x, etyp)
+ continue
+ }
+ }
+ if len(e.Elts) < len(fields) {
+ check.errorf(e.Rbrace, "too few values in struct literal")
+ goto Error
+ }
+ }
+
+ case *Array:
+ var index int64
+ for _, e := range e.Elts {
+ eval := e
+ if kv, _ := e.(*ast.KeyValueExpr); kv != nil {
+ check.index(kv.Key, -1, iota)
+ eval = kv.Value
+ }
+ // TODO(gri) missing index range & duplicate check
+ check.expr(x, eval, utyp.Elt, iota)
+ if !x.isAssignable(utyp.Elt) {
+ check.errorf(x.pos(), "cannot use %s as %s value in array literal", x, utyp.Elt)
+ }
+ index++
+ }
+
+ case *Slice:
+ var index int64
+ for _, e := range e.Elts {
+ eval := e
+ if kv, _ := e.(*ast.KeyValueExpr); kv != nil {
+ // TODO(gri) check key
+ check.index(kv.Key, -1, iota)
+ eval = kv.Value
+ }
+ // TODO(gri) missing index range & duplicate check
+ check.expr(x, eval, utyp.Elt, iota)
+ if !x.isAssignable(utyp.Elt) {
+ check.errorf(x.pos(), "cannot use %s as %s value in slice literal", x, utyp.Elt)
+ }
+ index++
+ }
+
+ case *Map:
+ visited := make(map[interface{}]bool, len(e.Elts))
+ for _, e := range e.Elts {
+ kv, _ := e.(*ast.KeyValueExpr)
+ if kv == nil {
+ check.errorf(e.Pos(), "missing key in map literal")
+ continue
+ }
+ check.expr(x, kv.Key, nil, iota)
+ if !x.isAssignable(utyp.Key) {
+ check.errorf(x.pos(), "cannot use %s as %s key in map literal", x, utyp.Key)
+ continue
+ }
+ if x.mode == constant {
+ if visited[x.val] {
+ check.errorf(x.pos(), "duplicate key %s in map literal", x.val)
+ continue
+ }
+ visited[x.val] = true
+ }
+ check.expr(x, kv.Value, utyp.Elt, iota)
+ if !x.isAssignable(utyp.Elt) {
+ check.errorf(x.pos(), "cannot use %s as %s value in map literal", x, utyp.Elt)
+ continue
+ }
+ }
+
+ default:
+ check.errorf(e.Pos(), "%s is not a valid composite literal type", typ)
+ goto Error
}
- // TODO(gri) this is not correct - leave for now to get going
- x.mode = variable
+
+ x.mode = variable // TODO(gri) mode is really a value - keep for now to get going
x.typ = typ
case *ast.ParenExpr:
}
mode, typ := lookupField(x.typ, sel)
if mode == invalid {
- check.invalidOp(e.Pos(), "%s has no field or method %s", x, sel)
+ check.invalidOp(e.Pos(), "%s has no single field or method %s", x, sel)
goto Error
}
if x.mode == typexpr {
// the receiver type becomes the type of the first function
// argument of the method expression's function type
// TODO(gri) at the moment, method sets don't correctly track
- // pointer vs non-pointer receivers -> typechecker is too lenient
+ // pointer vs non-pointer receivers => typechecker is too lenient
arg := ast.NewObj(ast.Var, "")
arg.Type = x.typ
x.mode = value
x.typ = typ.Elt
case *Map:
- // TODO(gri) check index type
+ var key operand
+ check.expr(&key, e.Index, nil, iota)
+ if key.mode == invalid || !key.isAssignable(typ.Key) {
+ check.invalidOp(x.pos(), "cannot use %s as map index of type %s", &key, typ.Key)
+ goto Error
+ }
x.mode = valueok
x.typ = typ.Elt
return
check.binary(x, &y, e.Op, hint)
case *ast.KeyValueExpr:
- unimplemented()
+ // key:value expressions are handled in composite literals
+ check.invalidAST(e.Pos(), "no key:value expected")
+ goto Error
case *ast.ArrayType:
if e.Len != nil {
case *ast.StructType:
x.mode = typexpr
- x.typ = &Struct{Fields: check.collectStructFields(e.Fields, cycleOk)}
+ x.typ = &Struct{Fields: check.collectFields(e.Fields, cycleOk)}
case *ast.FuncType:
- params, _, isVariadic := check.collectFields(token.FUNC, e.Params, true)
- results, _, _ := check.collectFields(token.FUNC, e.Results, true)
+ params, isVariadic := check.collectParams(e.Params)
+ results, _ := check.collectParams(e.Results)
x.mode = typexpr
x.typ = &Signature{Recv: nil, Params: params, Results: results, IsVariadic: isVariadic}
case *ast.InterfaceType:
- methods, _, _ := check.collectFields(token.INTERFACE, e.Methods, cycleOk)
- methods.Sort()
x.mode = typexpr
- x.typ = &Interface{Methods: methods}
+ x.typ = &Interface{Methods: check.collectMethods(e.Methods)}
case *ast.MapType:
x.mode = typexpr
return true // avoid spurious errors
}
- unimplemented()
+ // x implements T if it implements all methods of T.
+ // TODO(gri): distinguish pointer and non-pointer receivers
+ for _, m := range T.Methods {
+ mode, typ := lookupField(x.typ, m.Name)
+ if mode == invalid || !isIdentical(typ, m.Type.(Type)) {
+ // TODO(gri) should report which method is missing
+ return false
+ }
+ }
+
return true
}
return x.mode == constant && x.val == nilConst
}
+// TODO(gri) The functions operand.isAssignable, checker.convertUntyped,
+// checker.isRepresentable, and checker.assignOperand are
+// overlapping in functionality. Need to simplify and clean up.
+
// isAssignable reports whether x is assignable to a variable of type T.
func (x *operand) isAssignable(T Type) bool {
if x.mode == invalid || T == Typ[Invalid] {
}
// x is an untyped constant representable by a value of type T
- // - this is taken care of in the assignment check
- // TODO(gri) double-check - isAssignable is used elsewhere
+ // TODO(gri) This is borrowing from checker.convertUntyped and
+ // checker.isRepresentable. Need to clean up.
+ if isUntyped(Vu) {
+ switch t := Tu.(type) {
+ case *Basic:
+ return x.mode == constant && isRepresentableConst(x.val, t.Kind)
+ case *Interface:
+ return x.isNil() || len(t.Methods) == 0
+ case *Pointer, *Signature, *Slice, *Map, *Chan:
+ return x.isNil()
+ }
+ }
return false
}
typ Type
}
-// lookupFieldRecursive is similar to FieldByNameFunc in reflect/type.go
-// TODO(gri): FieldByNameFunc seems more complex - what are we missing?
-func lookupFieldRecursive(list []*NamedType, name string) (res lookupResult) {
- // visited records the types that have been searched already
- visited := make(map[Type]bool)
+type embeddedType struct {
+ typ *NamedType
+ multiples bool // if set, typ is embedded multiple times at the same level
+}
+
+// lookupFieldBreadthFirst searches all types in list for a single entry (field
+// or method) of the given name. If such a field is found, the result describes
+// the field mode and type; otherwise the result mode is invalid.
+// (This function is similar in structure to FieldByNameFunc in reflect/type.go)
+//
+func lookupFieldBreadthFirst(list []embeddedType, name string) (res lookupResult) {
+ // visited records the types that have been searched already.
+ visited := make(map[*NamedType]bool)
// embedded types of the next lower level
- var next []*NamedType
+ var next []embeddedType
- potentialMatch := func(mode operandMode, typ Type) bool {
- if res.mode != invalid {
- // name appeared multiple times at this level - annihilate
+ // potentialMatch is invoked every time a match is found.
+ potentialMatch := func(multiples bool, mode operandMode, typ Type) bool {
+ if multiples || res.mode != invalid {
+ // name appeared already at this level - annihilate
res.mode = invalid
return false
}
+ // first appearance of name
res.mode = mode
res.typ = typ
return true
}
- // look for name in all types of this level
+ // Search the current level if there is any work to do and collect
+ // embedded types of the next lower level in the next list.
for len(list) > 0 {
+ // The res.mode indicates whether we have found a match already
+ // on this level (mode != invalid), or not (mode == invalid).
assert(res.mode == invalid)
- for _, typ := range list {
+
+ // start with empty next list (don't waste underlying array)
+ next = next[:0]
+
+ // look for name in all types at this level
+ for _, e := range list {
+ typ := e.typ
if visited[typ] {
- // We have seen this type before, at a higher level.
- // That higher level shadows the lower level we are
- // at now, and either we would have found or not
- // found the field before. Ignore this type now.
continue
}
visited[typ] = true
if data := typ.Obj.Data; data != nil {
if obj := data.(*ast.Scope).Lookup(name); obj != nil {
assert(obj.Type != nil)
- if !potentialMatch(value, obj.Type.(Type)) {
+ if !potentialMatch(e.multiples, value, obj.Type.(Type)) {
return // name collision
}
}
switch typ := underlying(typ).(type) {
case *Struct:
- // look for a matching fieldm and collect embedded types
+ // look for a matching field and collect embedded types
for _, f := range typ.Fields {
if f.Name == name {
assert(f.Type != nil)
- if !potentialMatch(variable, f.Type) {
+ if !potentialMatch(e.multiples, variable, f.Type) {
return // name collision
}
continue
}
// Collect embedded struct fields for searching the next
- // lower level, but only if we have not seen a match yet.
+ // lower level, but only if we have not seen a match yet
+ // (if we have a match it is either the desired field or
+ // we have a name collision on the same level; in either
+ // case we don't need to look further).
// Embedded fields are always of the form T or *T where
- // T is a named type.
+ // T is a named type. If typ appeared multiple times at
+ // this level, f.Type appears multiple times at the next
+ // level.
if f.IsAnonymous && res.mode == invalid {
- next = append(next, deref(f.Type).(*NamedType))
+ next = append(next, embeddedType{deref(f.Type).(*NamedType), e.multiples})
}
}
for _, obj := range typ.Methods {
if obj.Name == name {
assert(obj.Type != nil)
- if !potentialMatch(value, obj.Type.(Type)) {
+ if !potentialMatch(e.multiples, value, obj.Type.(Type)) {
return // name collision
}
}
}
if res.mode != invalid {
- // we found a match on this level
+ // we found a single match on this level
return
}
- // search the next level
- list = append(list[:0], next...) // don't waste underlying arrays
- next = next[:0]
+ // No match and no collision so far.
+ // Compute the list to search for the next level.
+ list = list[:0] // don't waste underlying array
+ for _, e := range next {
+ // Instead of adding the same type multiple times, look for
+ // it in the list and mark it as multiple if it was added
+ // before.
+ // We use a sequential search (instead of a map for next)
+ // because the lists tend to be small, can easily be reused,
+ // and explicit search appears to be faster in this case.
+ if alt := findType(list, e.typ); alt != nil {
+ alt.multiples = true
+ } else {
+ list = append(list, e)
+ }
+ }
+
}
+
return
}
+func findType(list []embeddedType, typ *NamedType) *embeddedType {
+ for i := range list {
+ if p := &list[i]; p.typ == typ {
+ return p
+ }
+ }
+ return nil
+}
+
func lookupField(typ Type, name string) (operandMode, Type) {
typ = deref(typ)
switch typ := underlying(typ).(type) {
case *Struct:
- var list []*NamedType
+ var next []embeddedType
for _, f := range typ.Fields {
if f.Name == name {
return variable, f.Type
}
if f.IsAnonymous {
- list = append(list, deref(f.Type).(*NamedType))
+ // Possible optimization: If the embedded type
+ // is a pointer to the current type we could
+ // ignore it.
+ next = append(next, embeddedType{typ: deref(f.Type).(*NamedType)})
}
}
- if len(list) > 0 {
- res := lookupFieldRecursive(list, name)
+ if len(next) > 0 {
+ res := lookupFieldBreadthFirst(next, name)
return res.mode, res.typ
}