/
type_opaque.go
183 lines (159 loc) 路 5.13 KB
/
type_opaque.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright 2016-2020, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package model
import (
"fmt"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/pkg/errors"
"github.com/pulumi/pulumi/pkg/codegen/hcl2/syntax"
"github.com/pulumi/pulumi/sdk/go/common/util/contract"
)
// OpaqueType represents a type that is named by a string.
type OpaqueType struct {
// Name is the type's name.
Name string
s string
}
// The set of opaque types, indexed by name.
var opaqueTypes = map[string]*OpaqueType{}
// GetOpaqueType fetches the opaque type for the given name.
func GetOpaqueType(name string) (*OpaqueType, bool) {
t, ok := opaqueTypes[name]
return t, ok
}
// MustNewOpaqueType creates a new opaque type with the given name.
func MustNewOpaqueType(name string) *OpaqueType {
t, err := NewOpaqueType(name)
if err != nil {
panic(err)
}
return t
}
// NewOpaqueType creates a new opaque type with the given name.
func NewOpaqueType(name string) (*OpaqueType, error) {
if _, ok := opaqueTypes[name]; ok {
return nil, errors.Errorf("opaque type %s is already defined", name)
}
t := &OpaqueType{Name: name}
opaqueTypes[name] = t
return t, nil
}
// SyntaxNode returns the syntax node for the type. This is always syntax.None.
func (*OpaqueType) SyntaxNode() hclsyntax.Node {
return syntax.None
}
// Traverse attempts to traverse the opaque type with the given traverser. The result type of traverse(opaque(name))
// is dynamic if name is "dynamic"; otherwise the traversal fails.
func (t *OpaqueType) Traverse(traverser hcl.Traverser) (Traversable, hcl.Diagnostics) {
if t == DynamicType {
return DynamicType, nil
}
return DynamicType, hcl.Diagnostics{unsupportedReceiverType(t, traverser.SourceRange())}
}
// AssignableFrom returns true if this type is assignable from the indicated source type. A token(name) is assignable
// from token(name).
func (t *OpaqueType) AssignableFrom(src Type) bool {
return assignableFrom(t, src, func() bool {
return false
})
}
func (t *OpaqueType) conversionFromImpl(src Type, unifying, checkUnsafe bool) ConversionKind {
return conversionFrom(t, src, unifying, func() ConversionKind {
switch {
case t == NumberType:
// src == NumberType is handled by t == src above
contract.Assert(src != NumberType)
cki := IntType.conversionFromImpl(src, unifying, false)
if cki == SafeConversion {
return SafeConversion
}
if cki == UnsafeConversion || checkUnsafe && StringType.conversionFromImpl(src, unifying, false).Exists() {
return UnsafeConversion
}
return NoConversion
case t == IntType:
if checkUnsafe && NumberType.conversionFromImpl(src, unifying, true).Exists() {
return UnsafeConversion
}
return NoConversion
case t == BoolType:
if checkUnsafe && StringType.conversionFromImpl(src, unifying, false).Exists() {
return UnsafeConversion
}
return NoConversion
case t == StringType:
ckb := BoolType.conversionFromImpl(src, unifying, false)
ckn := NumberType.conversionFromImpl(src, unifying, false)
if ckb == SafeConversion || ckn == SafeConversion {
return SafeConversion
}
if ckb == UnsafeConversion || ckn == UnsafeConversion {
return UnsafeConversion
}
return NoConversion
default:
return NoConversion
}
})
}
func (t *OpaqueType) conversionFrom(src Type, unifying bool) ConversionKind {
return t.conversionFromImpl(src, unifying, true)
}
func (t *OpaqueType) ConversionFrom(src Type) ConversionKind {
return t.conversionFrom(src, false)
}
func (t *OpaqueType) String() string {
if t.s == "" {
switch t {
case NumberType:
t.s = "number"
case IntType:
t.s = "int"
case BoolType:
t.s = "bool"
case StringType:
t.s = "string"
default:
if hclsyntax.ValidIdentifier(t.Name) {
t.s = t.Name
} else {
t.s = fmt.Sprintf("type(%s)", t.Name)
}
}
}
return t.s
}
var opaquePrecedence = []*OpaqueType{StringType, NumberType, IntType, BoolType}
func (t *OpaqueType) unify(other Type) (Type, ConversionKind) {
return unify(t, other, func() (Type, ConversionKind) {
if t == DynamicType || other == DynamicType {
// These should have been handled by unify.
contract.Failf("unexpected type %v in OpaqueType.unify", t)
return DynamicType, SafeConversion
}
for _, goal := range opaquePrecedence {
if t == goal {
return goal, goal.conversionFrom(other, true)
}
if other == goal {
return goal, goal.conversionFrom(t, true)
}
}
// There should be a total order on conversions to and from these types, so there should be a total order
// on unifications with these types.
return DynamicType, SafeConversion
})
}
func (*OpaqueType) isType() {}