// TODO: test swap corresponding types (e.g. u1 <-> u2 and u2 <-> u1)
// TODO: test exported alias refers to something in another package -- does correspondence work then?
// TODO: CODE COVERAGE
// TODO: note that we may miss correspondences because we bail early when we compare a signature (e.g. when lengths differ; we could do up to the shorter)
// TODO: if you add an unexported method to an exposed interface, you have to check that
//		every exposed type that previously implemented the interface still does. Otherwise
//		an external assignment of the exposed type to the interface type could fail.
// TODO: check constant values: large values aren't representable by some types.
// TODO: Document all the incompatibilities we don't check for.

package apidiff

import (
	
	
	
	
)

// Changes reports on the differences between the APIs of the old and new packages.
// It classifies each difference as either compatible or incompatible (breaking.) For
// a detailed discussion of what constitutes an incompatible change, see the package
// documentation.
func (,  *types.Package) Report {
	 := newDiffer(, )
	.checkPackage()
	 := Report{}
	for ,  := range .incompatibles.collect() {
		.Changes = append(.Changes, Change{Message: , Compatible: false})
	}
	for ,  := range .compatibles.collect() {
		.Changes = append(.Changes, Change{Message: , Compatible: true})
	}
	return 
}

type differ struct {
	old, new *types.Package
	// Correspondences between named types.
	// Even though it is the named types (*types.Named) that correspond, we use
	// *types.TypeName as a map key because they are canonical.
	// The values can be either named types or basic types.
	correspondMap map[*types.TypeName]types.Type

	// Messages.
	incompatibles messageSet
	compatibles   messageSet
}

func (,  *types.Package) *differ {
	return &differ{
		old:           ,
		new:           ,
		correspondMap: map[*types.TypeName]types.Type{},
		incompatibles: messageSet{},
		compatibles:   messageSet{},
	}
}

func ( *differ) ( types.Object, ,  string,  ...interface{}) {
	addMessage(.incompatibles, , , , )
}

func ( *differ) ( types.Object, ,  string,  ...interface{}) {
	addMessage(.compatibles, , , , )
}

func ( messageSet,  types.Object, ,  string,  []interface{}) {
	.add(, , fmt.Sprintf(, ...))
}

func ( *differ) () {
	// Old changes.
	for ,  := range .old.Scope().Names() {
		 := .old.Scope().Lookup()
		if !.Exported() {
			continue
		}
		 := .new.Scope().Lookup()
		if  == nil {
			.incompatible(, "", "removed")
			continue
		}
		.checkObjects(, )
	}
	// New additions.
	for ,  := range .new.Scope().Names() {
		 := .new.Scope().Lookup()
		if .Exported() && .old.Scope().Lookup() == nil {
			.compatible(, "", "added")
		}
	}

	// Whole-package satisfaction.
	// For every old exposed interface oIface and its corresponding new interface nIface...
	for ,  := range .correspondMap {
		,  := .Type().Underlying().(*types.Interface)
		if ! {
			continue
		}
		,  := .Underlying().(*types.Interface)
		if ! {
			// If nt1 isn't an interface but otn1 is, then that's an incompatibility that
			// we've already noticed, so there's no need to do anything here.
			continue
		}
		// For every old type that implements oIface, its corresponding new type must implement
		// nIface.
		for ,  := range .correspondMap {
			if  ==  {
				continue
			}
			if types.Implements(.Type(), ) && !types.Implements(, ) {
				.incompatible(, "", "no longer implements %s", objectString())
			}
		}
	}
}

func ( *differ) (,  types.Object) {
	switch old := .(type) {
	case *types.Const:
		if ,  := .(*types.Const);  {
			.constChanges(, )
			return
		}
	case *types.Var:
		if ,  := .(*types.Var);  {
			.checkCorrespondence(, "", .Type(), .Type())
			return
		}
	case *types.Func:
		switch new := .(type) {
		case *types.Func:
			.checkCorrespondence(, "", .Type(), .Type())
			return
		case *types.Var:
			.compatible(, "", "changed from func to var")
			.checkCorrespondence(, "", .Type(), .Type())
			return

		}
	case *types.TypeName:
		if ,  := .(*types.TypeName);  {
			.checkCorrespondence(, "", .Type(), .Type())
			return
		}
	default:
		panic("unexpected obj type")
	}
	// Here if kind of type changed.
	.incompatible(, "", "changed from %s to %s",
		objectKindString(), objectKindString())
}

// Compare two constants.
func ( *differ) (,  *types.Const) {
	 := .Type()
	 := .Type()
	// Check for change of type.
	if !.correspond(, ) {
		.typeChanged(, "", , )
		return
	}
	// Check for change of value.
	// We know the types are the same, so constant.Compare shouldn't panic.
	if !constant.Compare(.Val(), token.EQL, .Val()) {
		.incompatible(, "", "value changed from %s to %s", .Val(), .Val())
	}
}

func ( types.Object) string {
	switch .(type) {
	case *types.Const:
		return "const"
	case *types.Var:
		return "var"
	case *types.Func:
		return "func"
	case *types.TypeName:
		return "type"
	default:
		return "???"
	}
}

func ( *differ) ( types.Object,  string, ,  types.Type) {
	if !.correspond(, ) {
		.typeChanged(, , , )
	}
}

func ( *differ) ( types.Object,  string, ,  types.Type) {
	 = removeNamesFromSignature()
	 = removeNamesFromSignature()
	 := types.TypeString(, types.RelativeTo(.old))
	 := types.TypeString(, types.RelativeTo(.new))
	.incompatible(, , "changed from %s to %s", , )
}

// go/types always includes the argument and result names when formatting a signature.
// Since these can change without affecting compatibility, we don't want users to
// be distracted by them, so we remove them.
func ( types.Type) types.Type {
	,  := .(*types.Signature)
	if ! {
		return 
	}

	 := func( *types.Tuple) *types.Tuple {
		var  []*types.Var
		for  := 0;  < .Len(); ++ {
			 := .At()
			 = append(, types.NewVar(.Pos(), .Pkg(), "", .Type()))
		}
		return types.NewTuple(...)
	}

	return types.NewSignature(.Recv(), (.Params()), (.Results()), .Variadic())
}