/
MovePreprocessor.swift
198 lines (175 loc) · 8.6 KB
/
MovePreprocessor.swift
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
//
// MovePreprocessor.swift
// MoveGen
//
// Created by Franklin Schrans on 2/1/18.
//
import AST
import Foundation
import Source
import Lexer
import Diagnostic
/// A preprocessing step to update the program's AST before code generation.
public struct MovePreprocessor: ASTPass {
public init() {}
/// Returns assignment statements for all the properties which have been assigned default values.
func defaultValueAssignments(in passContext: ASTPassContext) -> [Statement] {
let enclosingType = passContext.enclosingTypeIdentifier!.name
let propertiesInEnclosingType = passContext.environment!.propertyDeclarations(in: enclosingType)
return propertiesInEnclosingType.compactMap { declaration -> Statement? in
guard let assignedExpression = declaration.value else { return nil }
var identifier = declaration.identifier
identifier.enclosingType = enclosingType
return .expression(
.binaryExpression(
BinaryExpression(lhs: .identifier(identifier),
op: Token(kind: .punctuation(.equal), sourceLocation: identifier.sourceLocation),
rhs: assignedExpression)))
}
}
public func process(type: Type, passContext: ASTPassContext) -> ASTPassResult<Type> {
// Used to turn external trait types into addresses
var type = type
if let environment = passContext.environment, type.rawType.isExternalTraitType(environment: environment) {
type.rawType = RawType.externalTraitType
}
return ASTPassResult(element: type, diagnostics: [], passContext: passContext)
}
// MARK: Statement
public func process(becomeStatement: BecomeStatement, passContext: ASTPassContext) -> ASTPassResult<BecomeStatement> {
var becomeStatement = becomeStatement
let enumName = ContractDeclaration.contractEnumPrefix + passContext.enclosingTypeIdentifier!.name
let enumReference: Expression = .identifier(
Identifier(identifierToken: Token(kind: .identifier(enumName), sourceLocation: becomeStatement.sourceLocation)))
let state = becomeStatement.expression.assigningEnclosingType(type: enumName)
let dot = Token(kind: .punctuation(.dot), sourceLocation: becomeStatement.sourceLocation)
becomeStatement.expression = .binaryExpression(BinaryExpression(lhs: enumReference, op: dot, rhs: state))
return ASTPassResult(element: becomeStatement, diagnostics: [], passContext: passContext)
}
public func postProcess(returnStatement: ReturnStatement,
passContext: ASTPassContext) -> ASTPassResult<ReturnStatement> {
var passContext = passContext
var returnStatement = returnStatement
returnStatement.cleanupStatements = passContext.postStatements
passContext.postStatements = [] // No post-statements after a return statement, it maketh no sense
return ASTPassResult(element: returnStatement, diagnostics: [], passContext: passContext)
}
public func process(statement: Statement, passContext: ASTPassContext) -> ASTPassResult<Statement> {
var statement = statement
var passContext = passContext
if case .expression(let expression) = statement {
var functionCall = expression
if case .binaryExpression(let binaryExpression) = expression,
case .punctuation(.dot) = binaryExpression.op.kind {
functionCall = binaryExpression.rhs
}
if case .functionCall(let call) = functionCall,
let environment = passContext.environment,
case .matchedFunction(let function) = environment.matchFunctionCall(
call,
enclosingType: passContext.enclosingTypeIdentifier?.name ?? "",
typeStates: passContext.contractBehaviorDeclarationContext?.typeStates ?? [],
callerProtections: passContext.contractBehaviorDeclarationContext?.callerProtections ?? [],
scopeContext: passContext.scopeContext!),
.basicType(.void) != function.resultType {
statement = Move.release(expression: expression, type: function.resultType)
}
}
return ASTPassResult(element: statement, diagnostics: [], passContext: passContext)
}
public func postProcess(statement: Statement, passContext: ASTPassContext) -> ASTPassResult<Statement> {
let passContext = passContext.withUpdates { $0.functionCallReceiverTrail = [] }
return ASTPassResult(element: statement, diagnostics: [], passContext: passContext)
}
}
extension ASTPass {
func preAssign(_ element: Expression,
passContext: inout ASTPassContext,
borrowLocal: Bool = false,
isReference: Bool = true) -> Expression {
guard let environment = passContext.environment,
var scopeContext = passContext.scopeContext,
let enclosingType = passContext.enclosingTypeIdentifier?.name else {
Diagnostics.add(Diagnostic(severity: .error,
sourceLocation: element.sourceLocation,
message: "Insufficient information to deduce type"))
Diagnostics.displayAndExit()
}
var type: RawType = environment.type(of: element,
enclosingType: enclosingType,
scopeContext: scopeContext)
if type.isExternalTraitType(environment: environment) {
type = RawType.externalTraitType
}
let expression: Expression
if borrowLocal || type.isBuiltInType || !isReference {
expression = element
} else {
expression = .inoutExpression(InoutExpression(
ampersandToken: Token(kind: .punctuation(.ampersand),
sourceLocation: element.sourceLocation),
expression: element
))
}
let temporaryId: Identifier
// Check if this expression's already been assigned
if let statement: Statement = passContext.preStatements.first(where: { (statement: Statement) in
if case .expression(.binaryExpression(let binary)) = statement,
binary.opToken == .equal,
case .identifier = binary.lhs {
return binary.rhs == expression
}
return false
}) {
guard case .expression(.binaryExpression(let binary)) = statement,
case .identifier(let identifier) = binary.lhs else {
fatalError("Cannot find expected identifier for `\(element)`")
}
temporaryId = identifier
} else {
// Otherwise create a new identifier and handle set-up and clean up
temporaryId = scopeContext.freshIdentifier(sourceLocation: element.sourceLocation)
let declaration: VariableDeclaration
if type.isBuiltInType || borrowLocal {
declaration = VariableDeclaration(identifier: temporaryId,
type: Type(inferredType: type,
identifier: temporaryId))
} else {
declaration = VariableDeclaration(identifier: temporaryId,
type: Type(inferredType: .inoutType(type),
identifier: temporaryId))
passContext.postStatements.append(Move.release(expression: .identifier(temporaryId),
type: .inoutType(type)))
}
passContext.preStatements.append(Statement.expression(.binaryExpression(BinaryExpression(
lhs: .identifier(temporaryId),
op: Token(kind: .punctuation(.equal), sourceLocation: element.sourceLocation),
rhs: expression
))))
passContext.blockContext?.scopeContext.localVariables.append(declaration)
passContext.functionDeclarationContext?.innerDeclarations.append(declaration)
passContext.specialDeclarationContext?.innerDeclarations.append(declaration)
passContext.functionDeclarationContext?.declaration.scopeContext?.localVariables.append(declaration)
passContext.specialDeclarationContext?.declaration.scopeContext.localVariables.append(declaration)
scopeContext.localVariables.append(declaration)
}
passContext.scopeContext = scopeContext
if borrowLocal {
return .inoutExpression(InoutExpression(
ampersandToken: Token(kind: .punctuation(.and),
sourceLocation: element.sourceLocation),
expression: .identifier(temporaryId)
))
}
return .identifier(temporaryId)
}
}
extension ASTPassContext {
var functionCallReceiverTrail: [Expression]? {
get { return self[FunctionCallReceiverTrailContextEntry.self] }
set { self[FunctionCallReceiverTrailContextEntry.self] = newValue }
}
}
struct FunctionCallReceiverTrailContextEntry: PassContextEntry {
typealias Value = [Expression]
}