/
type.go
104 lines (88 loc) · 2.69 KB
/
type.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ir
import (
"fmt"
"github.com/go-asm/go/cmd/compile/base"
"github.com/go-asm/go/cmd/compile/types"
"github.com/go-asm/go/cmd/src"
)
// Nodes that represent the syntax of a type before type-checking.
// After type-checking, they serve only as shells around a *types.Type.
// Calling TypeNode converts a *types.Type to a Node shell.
// An Ntype is a Node that syntactically looks like a type.
// It can be the raw syntax for a type before typechecking,
// or it can be an OTYPE with Type() set to a *types.Type.
// Note that syntax doesn't guarantee it's a type: an expression
// like *fmt is an Ntype (we don't know whether names are types yet),
// but at least 1+1 is not an Ntype.
type Ntype interface {
Node
CanBeNtype()
}
// A Field is a declared function parameter.
// It is not a Node.
type Field struct {
Pos src.XPos
Sym *types.Sym
Type *types.Type
IsDDD bool
}
func NewField(pos src.XPos, sym *types.Sym, typ *types.Type) *Field {
return &Field{Pos: pos, Sym: sym, Type: typ}
}
func (f *Field) String() string {
if f.Sym != nil {
return fmt.Sprintf("%v %v", f.Sym, f.Type)
}
return fmt.Sprint(f.Type)
}
// A typeNode is a Node wrapper for type t.
type typeNode struct {
miniNode
typ *types.Type
}
func newTypeNode(typ *types.Type) *typeNode {
n := &typeNode{typ: typ}
n.pos = src.NoXPos
n.op = OTYPE
return n
}
func (n *typeNode) Type() *types.Type { return n.typ }
func (n *typeNode) Sym() *types.Sym { return n.typ.Sym() }
func (n *typeNode) CanBeNtype() {}
// TypeNode returns the Node representing the type t.
func TypeNode(t *types.Type) Ntype {
if n := t.Obj(); n != nil {
if n.Type() != t {
base.Fatalf("type skew: %v has type %v, but expected %v", n, n.Type(), t)
}
return n.(Ntype)
}
return newTypeNode(t)
}
// A DynamicType represents a type expression whose exact type must be
// computed dynamically.
type DynamicType struct {
miniExpr
// RType is an expression that yields a *runtime._type value
// representing the asserted type.
//
// BUG(mdempsky): If ITab is non-nil, RType may be nil.
RType Node
// ITab is an expression that yields a *runtime.itab value
// representing the asserted type within the assertee expression's
// original interface type.
//
// ITab is only used for assertions (including type switches) from
// non-empty interface type to a concrete (i.e., non-interface)
// type. For all other assertions, ITab is nil.
ITab Node
}
func NewDynamicType(pos src.XPos, rtype Node) *DynamicType {
n := &DynamicType{RType: rtype}
n.pos = pos
n.op = ODYNAMICTYPE
return n
}