/
check_conditional.go
209 lines (176 loc) Β· 5.59 KB
/
check_conditional.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* Cadence - The resource-oriented smart contract programming language
*
* Copyright Dapper Labs, Inc.
*
* 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 sema
import (
"github.com/onflow/cadence/runtime/ast"
"github.com/onflow/cadence/runtime/common/persistent"
"github.com/onflow/cadence/runtime/errors"
)
func (checker *Checker) VisitIfStatement(statement *ast.IfStatement) (_ struct{}) {
thenElement := statement.Then
switch test := statement.Test.(type) {
case ast.Expression:
checker.VisitExpression(test, BoolType)
checker.checkConditionalBranches(
func() Type {
checker.checkBlock(statement.Then)
return nil
},
func() Type {
if statement.Else != nil {
checker.checkBlock(statement.Else)
}
return nil
},
)
case *ast.VariableDeclaration:
checker.checkConditionalBranches(
func() Type {
checker.enterValueScope()
defer checker.leaveValueScope(thenElement.EndPosition, true)
checker.visitVariableDeclaration(test, true)
checker.checkBlock(thenElement)
return nil
},
func() Type {
if statement.Else != nil {
checker.checkBlock(statement.Else)
}
return nil
},
)
default:
panic(errors.NewUnreachableError())
}
return
}
func (checker *Checker) VisitConditionalExpression(expression *ast.ConditionalExpression) Type {
expectedType := checker.expectedType
checker.VisitExpression(expression.Test, BoolType)
thenType, elseType := checker.checkConditionalBranches(
func() Type {
return checker.VisitExpression(expression.Then, expectedType)
},
func() Type {
return checker.VisitExpression(expression.Else, expectedType)
},
)
if thenType == nil || elseType == nil {
panic(errors.NewUnreachableError())
}
if thenType.IsResourceType() {
checker.report(
&InvalidConditionalResourceOperandError{
Range: ast.NewRangeFromPositioned(checker.memoryGauge, expression.Then),
},
)
}
if elseType.IsResourceType() {
checker.report(
&InvalidConditionalResourceOperandError{
Range: ast.NewRangeFromPositioned(checker.memoryGauge, expression.Else),
},
)
}
if expectedType != nil {
return expectedType
}
if thenType.Equal(elseType) {
return thenType
}
return LeastCommonSuperType(thenType, elseType)
}
// checkConditionalBranches checks two conditional branches.
// It is assumed that either one of the branches is taken, so function returns,
// resource uses and invalidations, as well as field initializations,
// are only potential in each branch, but definite if they occur in both branches.
func (checker *Checker) checkConditionalBranches(
checkThen TypeCheckFunc,
checkElse TypeCheckFunc,
) (
thenType, elseType Type,
) {
functionActivation := checker.functionActivations.Current()
initialReturnInfo := functionActivation.ReturnInfo
thenReturnInfo := initialReturnInfo.Clone()
elseReturnInfo := initialReturnInfo.Clone()
var thenInitializedMembers *persistent.OrderedSet[*Member]
var elseInitializedMembers *persistent.OrderedSet[*Member]
if functionActivation.InitializationInfo != nil {
initialInitializedMembers := functionActivation.InitializationInfo.InitializedFieldMembers
thenInitializedMembers = initialInitializedMembers.Clone()
elseInitializedMembers = initialInitializedMembers.Clone()
}
initialResources := checker.resources
thenResources := initialResources.Clone()
defer thenResources.Reclaim()
elseResources := initialResources.Clone()
defer elseResources.Reclaim()
thenType = checker.checkBranch(
checkThen,
thenReturnInfo,
thenInitializedMembers,
thenResources,
)
elseType = checker.checkBranch(
checkElse,
elseReturnInfo,
elseInitializedMembers,
elseResources,
)
functionActivation.ReturnInfo.MergeBranches(thenReturnInfo, elseReturnInfo)
if functionActivation.InitializationInfo != nil {
// If one side definitely halted, the initializations in the other side can be considered definite
if thenReturnInfo.DefinitelyHalted {
functionActivation.InitializationInfo.InitializedFieldMembers = elseInitializedMembers
} else if elseReturnInfo.DefinitelyHalted {
functionActivation.InitializationInfo.InitializedFieldMembers = thenInitializedMembers
} else {
functionActivation.InitializationInfo.InitializedFieldMembers.
AddIntersection(thenInitializedMembers, elseInitializedMembers)
}
}
checker.resources.MergeBranches(
thenResources,
thenReturnInfo,
elseResources,
elseReturnInfo,
)
return
}
// checkBranch checks a conditional branch.
// It is assumed that function returns, resource uses and invalidations,
// as well as field initializations, are only potential / temporary.
func (checker *Checker) checkBranch(
check TypeCheckFunc,
temporaryReturnInfo *ReturnInfo,
temporaryInitializedMembers *persistent.OrderedSet[*Member],
temporaryResources *Resources,
) Type {
return wrapTypeCheck(check,
func(f TypeCheckFunc) Type {
return checker.checkWithResources(f, temporaryResources)
},
func(f TypeCheckFunc) Type {
return checker.checkWithInitializedMembers(f, temporaryInitializedMembers)
},
func(f TypeCheckFunc) Type {
return checker.checkWithReturnInfo(f, temporaryReturnInfo)
},
)()
}