-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwnt_LetASTNode.cpp
More file actions
238 lines (186 loc) · 6.59 KB
/
wnt_LetASTNode.cpp
File metadata and controls
238 lines (186 loc) · 6.59 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*=====================================================================
LetASTNode.cpp
--------------
Copyright Glare Technologies Limited 2016 -
=====================================================================*/
#include "wnt_LetASTNode.h"
#include "VMState.h"
#include "utils/StringUtils.h"
namespace Winter
{
LetASTNode::LetASTNode(const std::vector<LetNodeVar>& vars_, const ASTNodeRef& expr_, const SrcLocation& loc)
: ASTNode(LetType, loc),
expr(expr_),
vars(vars_),
traced(false)
{
assert(!vars.empty());
}
ValueRef LetASTNode::exec(VMState& vmstate)
{
ValueRef res = this->expr->exec(vmstate);
if(vmstate.trace && !traced)
{
*vmstate.ostream << vmstate.indent() << " " << this->vars[0].name << " = " << res->toString() << std::endl;
traced = true;
}
return res;
}
void LetASTNode::print(int depth, std::ostream& s) const
{
printMargin(depth, s);
s << "Let node (" + toHexString((uint64)this) + "), var_name = '" + this->vars[0].name + "'\n"; // TODO: print out other var names as well
this->expr->print(depth+1, s);
}
std::string LetASTNode::sourceString(int depth) const
{
if(vars.size() == 1)
{
return vars[0].name + " = " + expr->sourceString(depth);
}
else
{
std::string s;
for(size_t i=0; i<vars.size(); ++i)
{
s += vars[i].name;
if(i + 1 < vars.size())
s += ", ";
}
s += " = " + expr->sourceString(depth);
return s;
}
}
std::string LetASTNode::emitOpenCLC(EmitOpenCLCodeParams& params) const
{
return this->expr->emitOpenCLC(params);
}
void LetASTNode::traverse(TraversalPayload& payload, std::vector<ASTNode*>& stack)
{
/*if(payload.operation == TraversalPayload::ConstantFolding)
{
checkFoldExpression(expr, payload);
}*/
stack.push_back(this);
expr->traverse(payload, stack);
if(payload.operation == TraversalPayload::TypeCheck)
{
if(vars.size() == 1)
{
// Check that the return type of the body expression is equal to the declared return type
// of this function.
if(vars[0].declared_type.nonNull())
if(*expr->type() != *vars[0].declared_type)
throw ExceptionWithPosition("Type error for let '" + vars[0].name + "': Computed return type '" + this->expr->type()->toString() +
"' is not equal to the declared return type '" + vars[0].declared_type->toString() + "'.", errorContext(*this));
}
else
{
assert(vars.size() > 1);
if(expr->type()->getType() != Type::TupleTypeType)
throw ExceptionWithPosition("Type error for let with destructuring assignment. Value expression must have tuple type.", errorContext(*this));
const TupleTypeRef tuple_type = expr->type().downcast<TupleType>();
if(tuple_type->component_types.size() != vars.size())
throw ExceptionWithPosition("Number of let vars must equal num elements in tuple.", errorContext(*this));
for(size_t i=0; i<vars.size(); ++i)
{
if(vars[i].declared_type.nonNull())
{
if(*tuple_type->component_types[i] != *vars[i].declared_type)
throw ExceptionWithPosition("Type error for let '" + vars[i].name + "': Computed return type '" + tuple_type->component_types[i]->toString() +
"' is not equal to the declared return type '" + vars[i].declared_type->toString() + "'.", errorContext(*this));
}
}
}
}
else if(payload.operation == TraversalPayload::TypeCoercion)
{
for(size_t i=0; i<vars.size(); ++i)
{
// Do int -> float coercion
if(expr->nodeType() == ASTNode::IntLiteralType && vars[i].declared_type.nonNull() && vars[i].declared_type->getType() == Type::FloatType)
{
const IntLiteral* body_lit = static_cast<const IntLiteral*>(expr.getPointer());
if(isIntExactlyRepresentableAsFloat(body_lit->value))
{
expr = new FloatLiteral((float)body_lit->value, body_lit->srcLocation());
payload.tree_changed = true;
}
}
// Do int -> double coercion
if(expr->nodeType() == ASTNode::IntLiteralType && vars[i].declared_type.nonNull() && vars[i].declared_type->getType() == Type::DoubleType)
{
const IntLiteral* body_lit = static_cast<const IntLiteral*>(expr.getPointer());
expr = new DoubleLiteral((double)body_lit->value, body_lit->srcLocation());
payload.tree_changed = true;
}
}
}
else if(payload.operation == TraversalPayload::ComputeCanConstantFold)
{
//this->can_constant_fold = expr->can_constant_fold && expressionIsWellTyped(*this, payload);
const bool is_literal = checkFoldExpression(expr, payload, stack);
this->can_maybe_constant_fold = is_literal;
}
else if(payload.operation == TraversalPayload::GetAllNamesInScope)
{
for(size_t i=0; i<vars.size(); ++i)
payload.used_names->insert(vars[i].name);
}
else if(payload.operation == TraversalPayload::CustomVisit)
{
if(payload.custom_visitor.nonNull())
payload.custom_visitor->visit(*this, payload);
}
stack.pop_back();
}
void LetASTNode::updateChild(const ASTNode* old_val, ASTNodeRef& new_val)
{
if(expr.ptr() == old_val)
expr = new_val;
else
assert(0);
}
llvm::Value* LetASTNode::emitLLVMCode(EmitLLVMCodeParams& params, llvm::Value* ret_space_ptr) const
{
//if(!llvm_value)
// llvm_value = expr->emitLLVMCode(params);
//return llvm_value;
return expr->emitLLVMCode(params);
//llvm::Value* v = expr->emitLLVMCode(params);
// If this is a string value, need to decr ref count at end of func.
/*if(this->type()->getType() == Type::StringType)
{
params.cleanup_values.push_back(CleanUpInfo(this, v));
}*/
//return v;
}
//void LetASTNode::emitCleanupLLVMCode(EmitLLVMCodeParams& params, llvm::Value* val) const
//{
// //if(!(expr->nodeType() == ASTNode::VariableASTNodeType && expr.downcastToPtr<Variable>()->vartype == Variable::LetVariable)) // Don't decr let var ref counts, the ref block will do that.
// // this->type()->emitDecrRefCount(params, val);
// // RefCounting::emitCleanupLLVMCode(params, this->type(), val);
//}
Reference<ASTNode> LetASTNode::clone(CloneMapType& clone_map)
{
LetASTNode* e = new LetASTNode(this->vars, this->expr->clone(clone_map), this->srcLocation());
clone_map.insert(std::make_pair(this, e));
return e;
}
bool LetASTNode::isConstant() const
{
return expr->isConstant();
}
size_t LetASTNode::getTimeBound(GetTimeBoundParams& params) const
{
return expr->getTimeBound(params);
}
GetSpaceBoundResults LetASTNode::getSpaceBound(GetSpaceBoundParams& params) const
{
return expr->getSpaceBound(params);
}
size_t LetASTNode::getSubtreeCodeComplexity() const
{
return expr->getSubtreeCodeComplexity();
}
} // end namespace Winter