Golang 编译器代码浅析
  • 0. Golang 编译器代码浅析
  • 1. golang 编译器 - 前言
    • 1.1 编译器简介
    • 1.2 Golang 编译器
    • 1.3 Go 语言版本
    • 1.4 项目设置
    • 1.5 约定
    • 1.6 写作目的
  • 2. golang 编译器 - 词法分析
    • 2.1 简介
    • 2.2 代码结构
    • 2.3 处理字符
    • 2.4 扫描Token
    • 2.5 总结
  • 3.a 语法分析理论知识
    • 3A.1 语法分析简介
    • 3A.2 文法
    • 3A.3 语法解析
    • 3A.3.1 自顶向下(Top-Down)
    • 3A.3.2 自顶向下 - 递归下降
    • 3A.3.3 自顶向下 - LL(1)文法
    • 3A.3.4 自底向上(Bottom-Up)
    • 3A.3.5 自底向上 - LR(0)项集及SLR预测表
    • 3A.3.6 自底向上 - LR(1)、LALR
    • 3A.4 语法分析工具
    • 3A.5 总结
  • 3B. golang 编译器 - 语法分析
    • 3B.1 简介
    • 3B.2 代码结构
    • 3B.3 数据结构
    • 3B.4 构造语法树
    • 3B.5 Unit Test及AST可视化
  • 4. Golang 编译器 - 类型检查
    • 4.1 简介
    • 4.2 代码结构
    • 4.3 符号解析
    • 4.4.1 数据结构 - 作用域
    • 4.4.2 数据结构 - Package
    • 4.4.3 数据结构 - Object 对象
    • 4.4.4-1 类型数据结构 - 简介
    • 4.4.4-2 类型接口
    • 4.4.4-3 基础类型
    • 4.4.4-4 内置复合类型
    • 4.4.4-5 Struct 类型
    • 4.4.4-6 Interface 类型
    • 4.4.4-7 Named 类型
    • 4.4.4-8 Tuple 类型
    • 4.4.4-9 Sum 类型
    • 4.4.4-10 Function & Method 类型
    • 4.4.4-11 泛型类型
    • 4.4.4-12 类型的等价规则
    • 4.4.4-13 类型的比较规则
    • 4.4.4-14 总结
    • 4.4.5 类型检查器
    • 4.4.6 总结
    • 4.5.1 类型检查逻辑 - 包加载器
    • 4.5.2 类型检查逻辑 - 初始化
    • 4.5.2-1 全局作用域
    • 4.5.2-2 类型检查器
    • 4.5.3 类型检查逻辑 - 流程分析
    • 4.5.3-1.1 总体流程
    • 4.5.3-1.2 类型检查准备工作
    • 4.5.3-1.3 类型检查核心逻辑
    • 4.5.3-1.3a 总体介绍
    • 4.5.3-1.3b 类型表达式的类型检查
    • 4.5.3-1.3c 求值表达式的类型检查
    • 4.5.3-1.3d 类型兼容性检查
    • 4.5.3-1.3e 处理delayed队列
    • 4.5.3-1.4 构建初始化顺序
    • 4.5.3-1.5 总结
    • 4.5.3-2 特定问题分析
    • 4.5.3-2a 对象循环依赖检查
    • 4.5.3-2b 方法与属性查找
    • 4.5.3-2c Underlying Type
    • 4.6 如何测试
    • 4.7 总结
  • 5. Golang 编译器 - IR Tree
    • 5.1 简介
    • 5.2 代码结构
    • 5.3 数据结构
    • 5.4 处理逻辑
    • 5.5 编译日志
    • 5.6 Unit Test
    • 5.7 总结
  • 6. golang 编译器 - 初始化任务
    • 6.1 简介
    • 6.2 代码结构
    • 6.3 总体逻辑
    • 6.4 赋值语句
    • 6.5 编译日志
    • 6.6 Unit Test
    • 6.7 总结
  • 7. golang 编译器 - 清除无效代码
    • 7.1 简介
    • 7.2 处理逻辑
    • 7.3 Unit Test
  • 8. golang 编译器 - Inline
    • 8.1 简介
    • 8.2 Inline的问题
    • 8.3 代码结构
    • 8.4 处理逻辑
    • 8.4.1 遍历调用链
    • 8.4.2 内联判断
    • 8.4.3 内联操作
    • 8.4.4 编译日志
    • 8.4.5 Unit Test
    • 8.4.6 总结
  • 9. golang 编译器 - 逃逸分析
    • 9.1 什么是逃逸分析
    • 9.2 Go 的逃逸分析
    • 9.3 算法思路
    • 9.4 代码结构
    • 9.5 处理逻辑
    • 9.5.1总体逻辑
    • 9.5.2 数据结构
    • 9.5.3 构建数据流有向图
    • 9.5.4 逃逸分析
    • 9.6 编译日志
    • 9.7 Unit Test
    • 9.8 总结
  • 10. golang 编译器 - 函数编译及导出
    • 10.1 简介
    • 10.2 编译函数
    • 10.2.1 SSA
    • 10.2.2 ABI
    • 10.2.3 并发控制
    • 10.3 导出对象文件
    • 10.4 总结
  • 11. Golang 编译器 - 写在最后
由 GitBook 提供支持
在本页

这有帮助吗?

  1. 4. Golang 编译器 - 类型检查

4.4.4-13 类型的比较规则

上一页4.4.4-12 类型的等价规则下一页4.4.4-14 总结

最后更新于3年前

这有帮助吗?

除了类型的等价性,程序中还经常需要对比两个实例是否相等,或者其大小关系,这类操作通过比较运算符来实现。go 定义了 6 个(==, !=, >=, <=, >, <), 其中 != 与 == 应用于可比较类型(Comparable type),而剩下四个运算符应用于可排序类型(Ordered type);Ordered type 都是与数字、字符串相关的基础类型,在中有明确定义。Comparable type 指的是那些可以判断不同实例是否相等的类型,例如 map 的 key, 就只能使用 Comparable Type 的类型。所有的基本类型都是 Comparable 的,除此之外还有一些复合类型也是 Comparable Type,各种类型的比较规则在有详细的说明。

在文件$GCROOT/compile/internal/type2/predicates.go中定义了各种判断类型属性的方法,其中就包括判断给定类型是否是可排序的,或者是否是可比较的,代码如下:

func isOrdered(typ Type) bool { return is(typ, IsOrdered) }
func Comparable(T Type) bool  { return comparable(T, nil) } // 
func comparable(T Type, seen map[Type]bool) bool {
	if seen[T] {
		return true
	}
	if seen == nil {
		seen = make(map[Type]bool)
	}
	seen[T] = true

	// If T is a type parameter not constrained by any type
	// list (i.e., it's underlying type is the top type),
	// T is comparable if it has the == method. Otherwise,
	// the underlying type "wins". For instance
	//
	//     interface{ comparable; type []byte }
	//
	// is not comparable because []byte is not comparable.
	if t := asTypeParam(T); t != nil && optype(t) == theTop {
		return t.Bound().IsComparable()
	}

	switch t := optype(T).(type) {
	case *Basic:
		// assume invalid types to be comparable
		// to avoid follow-up errors
		return t.kind != UntypedNil
	case *Pointer, *Interface, *Chan:
		return true
	case *Struct:
		for _, f := range t.fields {
			if !comparable(f.typ, seen) {
				return false
			}
		}
		return true
	case *Array:
		return comparable(t.elem, seen)
	case *Sum:
		pred := func(t Type) bool {
			return comparable(t, seen)
		}
		return t.is(pred)
	case *TypeParam:
		return t.Bound().IsComparable()
	}
	return false
}

可以发现 Function 类型不在 comparable 函数中的任何一个 case 中,因此它是不可比较的,而 struct 类型可比较的前提是其所有属性都是可比较类型。来看如下代码:

func typecheck() {
	type User struct {
		Name string
	}

	type People struct {
		Name string
	}

	var u1, u2 User

	// case 1: 相同类型,且类型可比较,无编译错误
	if u1 == u2 {
		// No compile error
	}

	// case 2: 不同类型,虽然两个类型都是可比较类型,但由于类型不同,相互之间无法比较
	var p People
	if u1 == p {
		// Compile error: cannot compare u1 == p (mismatched types User and People) [MismatchedTypes]
	}

	// case 3: 相同类型,但函数类型不可比较,所以变量之间不可比较
	var f1 func()
	var f2 func()

	if f1 == f2 {
		// Compile error: cannot compare f1 == f2 (operator == not defined for func())
	}
}

上例中 User 类型所有的属性都是可比较的,所以 u1 == u2 没有问题,而 u1 与 p 是不同的类型,所以无法比较。再看如下代码:

type User struct {
	Name string
	Age  func() int
}

var u1, u2 User

if u1 == u2 {
	// Compile error: cannot compare u1 == u2 (operator == not defuned for User) [UndefinedOp]
}

当我们给 User 添加一个函数字段时,类型检查器报告了编译错误,因为通过上面 comparable 方法可以发现,函数属性的加入让 User 变成不可比较的类型了,所以类型检查器会说== 操作符对该类型没有定义。

比较运算符
基础类型的 BasicInfo
go 语言规范中