blob: d64684e62dc3312ed3c885c46f9d064026799664 [file] [log] [blame]
ethannicholasb3058bd2016-07-01 08:22:01 -07001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkSLIRGenerator.h"
9
10#include "limits.h"
11
12#include "ast/SkSLASTBoolLiteral.h"
13#include "ast/SkSLASTFieldSuffix.h"
14#include "ast/SkSLASTFloatLiteral.h"
15#include "ast/SkSLASTIndexSuffix.h"
16#include "ast/SkSLASTIntLiteral.h"
17#include "ir/SkSLBinaryExpression.h"
18#include "ir/SkSLBoolLiteral.h"
19#include "ir/SkSLBreakStatement.h"
20#include "ir/SkSLConstructor.h"
21#include "ir/SkSLContinueStatement.h"
22#include "ir/SkSLDiscardStatement.h"
23#include "ir/SkSLDoStatement.h"
24#include "ir/SkSLExpressionStatement.h"
25#include "ir/SkSLField.h"
26#include "ir/SkSLFieldAccess.h"
27#include "ir/SkSLFloatLiteral.h"
28#include "ir/SkSLForStatement.h"
29#include "ir/SkSLFunctionCall.h"
30#include "ir/SkSLFunctionDeclaration.h"
31#include "ir/SkSLFunctionDefinition.h"
32#include "ir/SkSLFunctionReference.h"
33#include "ir/SkSLIfStatement.h"
34#include "ir/SkSLIndexExpression.h"
35#include "ir/SkSLInterfaceBlock.h"
36#include "ir/SkSLIntLiteral.h"
37#include "ir/SkSLLayout.h"
38#include "ir/SkSLPostfixExpression.h"
39#include "ir/SkSLPrefixExpression.h"
40#include "ir/SkSLReturnStatement.h"
41#include "ir/SkSLSwizzle.h"
42#include "ir/SkSLTernaryExpression.h"
43#include "ir/SkSLUnresolvedFunction.h"
44#include "ir/SkSLVariable.h"
45#include "ir/SkSLVarDeclaration.h"
46#include "ir/SkSLVarDeclarationStatement.h"
47#include "ir/SkSLVariableReference.h"
48#include "ir/SkSLWhileStatement.h"
49
50namespace SkSL {
51
52class AutoSymbolTable {
53public:
54 AutoSymbolTable(IRGenerator* ir)
55 : fIR(ir)
56 , fPrevious(fIR->fSymbolTable) {
57 fIR->pushSymbolTable();
58 }
59
60 ~AutoSymbolTable() {
61 fIR->popSymbolTable();
62 ASSERT(fPrevious == fIR->fSymbolTable);
63 }
64
65 IRGenerator* fIR;
66 std::shared_ptr<SymbolTable> fPrevious;
67};
68
ethannicholasd598f792016-07-25 10:08:54 -070069IRGenerator::IRGenerator(const Context* context, std::shared_ptr<SymbolTable> symbolTable,
ethannicholasb3058bd2016-07-01 08:22:01 -070070 ErrorReporter& errorReporter)
ethannicholasd598f792016-07-25 10:08:54 -070071: fContext(*context)
72, fCurrentFunction(nullptr)
73, fSymbolTable(std::move(symbolTable))
74, fErrors(errorReporter) {}
ethannicholasb3058bd2016-07-01 08:22:01 -070075
76void IRGenerator::pushSymbolTable() {
77 fSymbolTable.reset(new SymbolTable(std::move(fSymbolTable), fErrors));
78}
79
80void IRGenerator::popSymbolTable() {
81 fSymbolTable = fSymbolTable->fParent;
82}
83
84std::unique_ptr<Extension> IRGenerator::convertExtension(const ASTExtension& extension) {
85 return std::unique_ptr<Extension>(new Extension(extension.fPosition, extension.fName));
86}
87
88std::unique_ptr<Statement> IRGenerator::convertStatement(const ASTStatement& statement) {
89 switch (statement.fKind) {
90 case ASTStatement::kBlock_Kind:
91 return this->convertBlock((ASTBlock&) statement);
92 case ASTStatement::kVarDeclaration_Kind:
93 return this->convertVarDeclarationStatement((ASTVarDeclarationStatement&) statement);
94 case ASTStatement::kExpression_Kind:
95 return this->convertExpressionStatement((ASTExpressionStatement&) statement);
96 case ASTStatement::kIf_Kind:
97 return this->convertIf((ASTIfStatement&) statement);
98 case ASTStatement::kFor_Kind:
99 return this->convertFor((ASTForStatement&) statement);
100 case ASTStatement::kWhile_Kind:
101 return this->convertWhile((ASTWhileStatement&) statement);
102 case ASTStatement::kDo_Kind:
103 return this->convertDo((ASTDoStatement&) statement);
104 case ASTStatement::kReturn_Kind:
105 return this->convertReturn((ASTReturnStatement&) statement);
106 case ASTStatement::kBreak_Kind:
107 return this->convertBreak((ASTBreakStatement&) statement);
108 case ASTStatement::kContinue_Kind:
109 return this->convertContinue((ASTContinueStatement&) statement);
110 case ASTStatement::kDiscard_Kind:
111 return this->convertDiscard((ASTDiscardStatement&) statement);
112 default:
113 ABORT("unsupported statement type: %d\n", statement.fKind);
114 }
115}
116
117std::unique_ptr<Block> IRGenerator::convertBlock(const ASTBlock& block) {
118 AutoSymbolTable table(this);
119 std::vector<std::unique_ptr<Statement>> statements;
120 for (size_t i = 0; i < block.fStatements.size(); i++) {
121 std::unique_ptr<Statement> statement = this->convertStatement(*block.fStatements[i]);
122 if (!statement) {
123 return nullptr;
124 }
125 statements.push_back(std::move(statement));
126 }
ethannicholasd598f792016-07-25 10:08:54 -0700127 return std::unique_ptr<Block>(new Block(block.fPosition, std::move(statements), fSymbolTable));
ethannicholasb3058bd2016-07-01 08:22:01 -0700128}
129
130std::unique_ptr<Statement> IRGenerator::convertVarDeclarationStatement(
131 const ASTVarDeclarationStatement& s) {
ethannicholas14fe8cc2016-09-07 13:37:16 -0700132 auto decl = this->convertVarDeclarations(*s.fDeclarations, Variable::kLocal_Storage);
ethannicholasb3058bd2016-07-01 08:22:01 -0700133 if (!decl) {
134 return nullptr;
135 }
ethannicholas14fe8cc2016-09-07 13:37:16 -0700136 return std::unique_ptr<Statement>(new VarDeclarationsStatement(std::move(decl)));
ethannicholasb3058bd2016-07-01 08:22:01 -0700137}
138
139Modifiers IRGenerator::convertModifiers(const ASTModifiers& modifiers) {
140 return Modifiers(modifiers);
141}
142
ethannicholas14fe8cc2016-09-07 13:37:16 -0700143std::unique_ptr<VarDeclarations> IRGenerator::convertVarDeclarations(const ASTVarDeclarations& decl,
144 Variable::Storage storage) {
145 std::vector<VarDeclaration> variables;
ethannicholasd598f792016-07-25 10:08:54 -0700146 const Type* baseType = this->convertType(*decl.fType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700147 if (!baseType) {
148 return nullptr;
149 }
ethannicholas14fe8cc2016-09-07 13:37:16 -0700150 for (const auto& varDecl : decl.fVars) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700151 Modifiers modifiers = this->convertModifiers(decl.fModifiers);
ethannicholasd598f792016-07-25 10:08:54 -0700152 const Type* type = baseType;
ethannicholasb3058bd2016-07-01 08:22:01 -0700153 ASSERT(type->kind() != Type::kArray_Kind);
ethannicholas14fe8cc2016-09-07 13:37:16 -0700154 std::vector<std::unique_ptr<Expression>> sizes;
155 for (const auto& rawSize : varDecl.fSizes) {
156 if (rawSize) {
157 auto size = this->coerce(this->convertExpression(*rawSize), *fContext.fInt_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -0700158 if (!size) {
159 return nullptr;
160 }
161 std::string name = type->fName;
162 uint64_t count;
163 if (size->fKind == Expression::kIntLiteral_Kind) {
164 count = ((IntLiteral&) *size).fValue;
165 if (count <= 0) {
166 fErrors.error(size->fPosition, "array size must be positive");
167 }
168 name += "[" + to_string(count) + "]";
169 } else {
170 count = -1;
171 name += "[]";
172 }
ethannicholasd598f792016-07-25 10:08:54 -0700173 type = new Type(name, Type::kArray_Kind, *type, (int) count);
174 fSymbolTable->takeOwnership((Type*) type);
ethannicholas14fe8cc2016-09-07 13:37:16 -0700175 sizes.push_back(std::move(size));
ethannicholasb3058bd2016-07-01 08:22:01 -0700176 } else {
ethannicholasd598f792016-07-25 10:08:54 -0700177 type = new Type(type->fName + "[]", Type::kArray_Kind, *type, -1);
178 fSymbolTable->takeOwnership((Type*) type);
ethannicholas14fe8cc2016-09-07 13:37:16 -0700179 sizes.push_back(nullptr);
ethannicholasb3058bd2016-07-01 08:22:01 -0700180 }
181 }
ethannicholas14fe8cc2016-09-07 13:37:16 -0700182 auto var = std::unique_ptr<Variable>(new Variable(decl.fPosition, modifiers, varDecl.fName,
ethannicholasd598f792016-07-25 10:08:54 -0700183 *type, storage));
ethannicholasb3058bd2016-07-01 08:22:01 -0700184 std::unique_ptr<Expression> value;
ethannicholas14fe8cc2016-09-07 13:37:16 -0700185 if (varDecl.fValue) {
186 value = this->convertExpression(*varDecl.fValue);
ethannicholasb3058bd2016-07-01 08:22:01 -0700187 if (!value) {
188 return nullptr;
189 }
ethannicholasd598f792016-07-25 10:08:54 -0700190 value = this->coerce(std::move(value), *type);
ethannicholasb3058bd2016-07-01 08:22:01 -0700191 }
ethannicholasdcfe6db2016-10-10 10:09:00 -0700192 if ("sk_FragColor" == varDecl.fName && (*fSymbolTable)[varDecl.fName]) {
193 // already defined, ignore
194 } else if ((*fSymbolTable)[varDecl.fName] &&
195 (*fSymbolTable)[varDecl.fName]->fKind == Symbol::kVariable_Kind &&
196 ((Variable*) (*fSymbolTable)[varDecl.fName])->fModifiers.fLayout.fBuiltin >= 0) {
ethannicholasf789b382016-08-03 12:43:36 -0700197 // already defined, just update the modifiers
ethannicholas14fe8cc2016-09-07 13:37:16 -0700198 Variable* old = (Variable*) (*fSymbolTable)[varDecl.fName];
ethannicholasf789b382016-08-03 12:43:36 -0700199 old->fModifiers = var->fModifiers;
200 } else {
ethannicholas14fe8cc2016-09-07 13:37:16 -0700201 variables.emplace_back(var.get(), std::move(sizes), std::move(value));
202 fSymbolTable->add(varDecl.fName, std::move(var));
ethannicholasf789b382016-08-03 12:43:36 -0700203 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700204 }
ethannicholas14fe8cc2016-09-07 13:37:16 -0700205 return std::unique_ptr<VarDeclarations>(new VarDeclarations(decl.fPosition,
206 baseType,
207 std::move(variables)));
ethannicholasb3058bd2016-07-01 08:22:01 -0700208}
209
ethannicholasdcfe6db2016-10-10 10:09:00 -0700210std::unique_ptr<ModifiersDeclaration> IRGenerator::convertModifiersDeclaration(
211 const ASTModifiersDeclaration& m) {
212 Modifiers modifiers = this->convertModifiers(m.fModifiers);
213 return std::unique_ptr<ModifiersDeclaration>(new ModifiersDeclaration(modifiers));
214}
215
ethannicholasb3058bd2016-07-01 08:22:01 -0700216std::unique_ptr<Statement> IRGenerator::convertIf(const ASTIfStatement& s) {
ethannicholasd598f792016-07-25 10:08:54 -0700217 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*s.fTest),
218 *fContext.fBool_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -0700219 if (!test) {
220 return nullptr;
221 }
222 std::unique_ptr<Statement> ifTrue = this->convertStatement(*s.fIfTrue);
223 if (!ifTrue) {
224 return nullptr;
225 }
226 std::unique_ptr<Statement> ifFalse;
227 if (s.fIfFalse) {
228 ifFalse = this->convertStatement(*s.fIfFalse);
229 if (!ifFalse) {
230 return nullptr;
231 }
232 }
233 return std::unique_ptr<Statement>(new IfStatement(s.fPosition, std::move(test),
234 std::move(ifTrue), std::move(ifFalse)));
235}
236
237std::unique_ptr<Statement> IRGenerator::convertFor(const ASTForStatement& f) {
238 AutoSymbolTable table(this);
239 std::unique_ptr<Statement> initializer = this->convertStatement(*f.fInitializer);
240 if (!initializer) {
241 return nullptr;
242 }
ethannicholasd598f792016-07-25 10:08:54 -0700243 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*f.fTest),
244 *fContext.fBool_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -0700245 if (!test) {
246 return nullptr;
247 }
248 std::unique_ptr<Expression> next = this->convertExpression(*f.fNext);
249 if (!next) {
250 return nullptr;
251 }
252 this->checkValid(*next);
253 std::unique_ptr<Statement> statement = this->convertStatement(*f.fStatement);
254 if (!statement) {
255 return nullptr;
256 }
257 return std::unique_ptr<Statement>(new ForStatement(f.fPosition, std::move(initializer),
258 std::move(test), std::move(next),
ethannicholasd598f792016-07-25 10:08:54 -0700259 std::move(statement), fSymbolTable));
ethannicholasb3058bd2016-07-01 08:22:01 -0700260}
261
262std::unique_ptr<Statement> IRGenerator::convertWhile(const ASTWhileStatement& w) {
ethannicholasd598f792016-07-25 10:08:54 -0700263 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*w.fTest),
264 *fContext.fBool_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -0700265 if (!test) {
266 return nullptr;
267 }
268 std::unique_ptr<Statement> statement = this->convertStatement(*w.fStatement);
269 if (!statement) {
270 return nullptr;
271 }
272 return std::unique_ptr<Statement>(new WhileStatement(w.fPosition, std::move(test),
273 std::move(statement)));
274}
275
276std::unique_ptr<Statement> IRGenerator::convertDo(const ASTDoStatement& d) {
ethannicholasd598f792016-07-25 10:08:54 -0700277 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*d.fTest),
278 *fContext.fBool_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -0700279 if (!test) {
280 return nullptr;
281 }
282 std::unique_ptr<Statement> statement = this->convertStatement(*d.fStatement);
283 if (!statement) {
284 return nullptr;
285 }
286 return std::unique_ptr<Statement>(new DoStatement(d.fPosition, std::move(statement),
287 std::move(test)));
288}
289
290std::unique_ptr<Statement> IRGenerator::convertExpressionStatement(
291 const ASTExpressionStatement& s) {
292 std::unique_ptr<Expression> e = this->convertExpression(*s.fExpression);
293 if (!e) {
294 return nullptr;
295 }
296 this->checkValid(*e);
297 return std::unique_ptr<Statement>(new ExpressionStatement(std::move(e)));
298}
299
300std::unique_ptr<Statement> IRGenerator::convertReturn(const ASTReturnStatement& r) {
301 ASSERT(fCurrentFunction);
302 if (r.fExpression) {
303 std::unique_ptr<Expression> result = this->convertExpression(*r.fExpression);
304 if (!result) {
305 return nullptr;
306 }
ethannicholasd598f792016-07-25 10:08:54 -0700307 if (fCurrentFunction->fReturnType == *fContext.fVoid_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700308 fErrors.error(result->fPosition, "may not return a value from a void function");
309 } else {
310 result = this->coerce(std::move(result), fCurrentFunction->fReturnType);
311 if (!result) {
312 return nullptr;
313 }
314 }
315 return std::unique_ptr<Statement>(new ReturnStatement(std::move(result)));
316 } else {
ethannicholasd598f792016-07-25 10:08:54 -0700317 if (fCurrentFunction->fReturnType != *fContext.fVoid_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700318 fErrors.error(r.fPosition, "expected function to return '" +
ethannicholasd598f792016-07-25 10:08:54 -0700319 fCurrentFunction->fReturnType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -0700320 }
321 return std::unique_ptr<Statement>(new ReturnStatement(r.fPosition));
322 }
323}
324
325std::unique_ptr<Statement> IRGenerator::convertBreak(const ASTBreakStatement& b) {
326 return std::unique_ptr<Statement>(new BreakStatement(b.fPosition));
327}
328
329std::unique_ptr<Statement> IRGenerator::convertContinue(const ASTContinueStatement& c) {
330 return std::unique_ptr<Statement>(new ContinueStatement(c.fPosition));
331}
332
333std::unique_ptr<Statement> IRGenerator::convertDiscard(const ASTDiscardStatement& d) {
334 return std::unique_ptr<Statement>(new DiscardStatement(d.fPosition));
335}
336
ethannicholasd598f792016-07-25 10:08:54 -0700337static const Type& expand_generics(const Type& type, int i) {
338 if (type.kind() == Type::kGeneric_Kind) {
339 return *type.coercibleTypes()[i];
ethannicholasb3058bd2016-07-01 08:22:01 -0700340 }
341 return type;
342}
343
ethannicholasd598f792016-07-25 10:08:54 -0700344static void expand_generics(const FunctionDeclaration& decl,
345 std::shared_ptr<SymbolTable> symbolTable) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700346 for (int i = 0; i < 4; i++) {
ethannicholasd598f792016-07-25 10:08:54 -0700347 const Type& returnType = expand_generics(decl.fReturnType, i);
348 std::vector<const Variable*> parameters;
ethannicholasb3058bd2016-07-01 08:22:01 -0700349 for (const auto& p : decl.fParameters) {
ethannicholasd598f792016-07-25 10:08:54 -0700350 Variable* var = new Variable(p->fPosition, Modifiers(p->fModifiers), p->fName,
351 expand_generics(p->fType, i),
352 Variable::kParameter_Storage);
353 symbolTable->takeOwnership(var);
354 parameters.push_back(var);
ethannicholasb3058bd2016-07-01 08:22:01 -0700355 }
ethannicholasd598f792016-07-25 10:08:54 -0700356 symbolTable->add(decl.fName, std::unique_ptr<FunctionDeclaration>(new FunctionDeclaration(
357 decl.fPosition,
358 decl.fName,
359 std::move(parameters),
360 std::move(returnType))));
ethannicholasb3058bd2016-07-01 08:22:01 -0700361 }
362}
363
364std::unique_ptr<FunctionDefinition> IRGenerator::convertFunction(const ASTFunction& f) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700365 bool isGeneric;
ethannicholasd598f792016-07-25 10:08:54 -0700366 const Type* returnType = this->convertType(*f.fReturnType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700367 if (!returnType) {
368 return nullptr;
369 }
370 isGeneric = returnType->kind() == Type::kGeneric_Kind;
ethannicholasd598f792016-07-25 10:08:54 -0700371 std::vector<const Variable*> parameters;
ethannicholasb3058bd2016-07-01 08:22:01 -0700372 for (const auto& param : f.fParameters) {
ethannicholasd598f792016-07-25 10:08:54 -0700373 const Type* type = this->convertType(*param->fType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700374 if (!type) {
375 return nullptr;
376 }
377 for (int j = (int) param->fSizes.size() - 1; j >= 0; j--) {
378 int size = param->fSizes[j];
379 std::string name = type->name() + "[" + to_string(size) + "]";
ethannicholasd598f792016-07-25 10:08:54 -0700380 Type* newType = new Type(std::move(name), Type::kArray_Kind, *type, size);
381 fSymbolTable->takeOwnership(newType);
382 type = newType;
ethannicholasb3058bd2016-07-01 08:22:01 -0700383 }
384 std::string name = param->fName;
385 Modifiers modifiers = this->convertModifiers(param->fModifiers);
386 Position pos = param->fPosition;
ethannicholasd598f792016-07-25 10:08:54 -0700387 Variable* var = new Variable(pos, modifiers, std::move(name), *type,
388 Variable::kParameter_Storage);
389 fSymbolTable->takeOwnership(var);
390 parameters.push_back(var);
ethannicholasb3058bd2016-07-01 08:22:01 -0700391 isGeneric |= type->kind() == Type::kGeneric_Kind;
392 }
393
394 // find existing declaration
ethannicholasd598f792016-07-25 10:08:54 -0700395 const FunctionDeclaration* decl = nullptr;
396 auto entry = (*fSymbolTable)[f.fName];
ethannicholasb3058bd2016-07-01 08:22:01 -0700397 if (entry) {
ethannicholasd598f792016-07-25 10:08:54 -0700398 std::vector<const FunctionDeclaration*> functions;
ethannicholasb3058bd2016-07-01 08:22:01 -0700399 switch (entry->fKind) {
400 case Symbol::kUnresolvedFunction_Kind:
ethannicholasd598f792016-07-25 10:08:54 -0700401 functions = ((UnresolvedFunction*) entry)->fFunctions;
ethannicholasb3058bd2016-07-01 08:22:01 -0700402 break;
403 case Symbol::kFunctionDeclaration_Kind:
ethannicholasd598f792016-07-25 10:08:54 -0700404 functions.push_back((FunctionDeclaration*) entry);
ethannicholasb3058bd2016-07-01 08:22:01 -0700405 break;
406 default:
407 fErrors.error(f.fPosition, "symbol '" + f.fName + "' was already defined");
408 return nullptr;
409 }
410 for (const auto& other : functions) {
411 ASSERT(other->fName == f.fName);
412 if (parameters.size() == other->fParameters.size()) {
413 bool match = true;
414 for (size_t i = 0; i < parameters.size(); i++) {
415 if (parameters[i]->fType != other->fParameters[i]->fType) {
416 match = false;
417 break;
418 }
419 }
420 if (match) {
ethannicholasd598f792016-07-25 10:08:54 -0700421 if (*returnType != other->fReturnType) {
422 FunctionDeclaration newDecl(f.fPosition, f.fName, parameters, *returnType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700423 fErrors.error(f.fPosition, "functions '" + newDecl.description() +
424 "' and '" + other->description() +
425 "' differ only in return type");
426 return nullptr;
427 }
428 decl = other;
429 for (size_t i = 0; i < parameters.size(); i++) {
430 if (parameters[i]->fModifiers != other->fParameters[i]->fModifiers) {
431 fErrors.error(f.fPosition, "modifiers on parameter " +
ethannicholasdcfe6db2016-10-10 10:09:00 -0700432 to_string((uint64_t) i + 1) +
433 " differ between declaration and "
434 "definition");
ethannicholasb3058bd2016-07-01 08:22:01 -0700435 return nullptr;
436 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700437 }
438 if (other->fDefined) {
439 fErrors.error(f.fPosition, "duplicate definition of " +
440 other->description());
441 }
442 break;
443 }
444 }
445 }
446 }
447 if (!decl) {
448 // couldn't find an existing declaration
ethannicholasd598f792016-07-25 10:08:54 -0700449 if (isGeneric) {
450 ASSERT(!f.fBody);
451 expand_generics(FunctionDeclaration(f.fPosition, f.fName, parameters, *returnType),
452 fSymbolTable);
453 } else {
454 auto newDecl = std::unique_ptr<FunctionDeclaration>(new FunctionDeclaration(
455 f.fPosition,
456 f.fName,
457 parameters,
458 *returnType));
459 decl = newDecl.get();
460 fSymbolTable->add(decl->fName, std::move(newDecl));
ethannicholasb3058bd2016-07-01 08:22:01 -0700461 }
462 }
ethannicholasd598f792016-07-25 10:08:54 -0700463 if (f.fBody) {
464 ASSERT(!fCurrentFunction);
465 fCurrentFunction = decl;
466 decl->fDefined = true;
467 std::shared_ptr<SymbolTable> old = fSymbolTable;
468 AutoSymbolTable table(this);
469 for (size_t i = 0; i < parameters.size(); i++) {
470 fSymbolTable->addWithoutOwnership(parameters[i]->fName, decl->fParameters[i]);
ethannicholasb3058bd2016-07-01 08:22:01 -0700471 }
ethannicholasd598f792016-07-25 10:08:54 -0700472 std::unique_ptr<Block> body = this->convertBlock(*f.fBody);
473 fCurrentFunction = nullptr;
474 if (!body) {
475 return nullptr;
476 }
477 return std::unique_ptr<FunctionDefinition>(new FunctionDefinition(f.fPosition, *decl,
478 std::move(body)));
ethannicholasb3058bd2016-07-01 08:22:01 -0700479 }
480 return nullptr;
481}
482
483std::unique_ptr<InterfaceBlock> IRGenerator::convertInterfaceBlock(const ASTInterfaceBlock& intf) {
484 std::shared_ptr<SymbolTable> old = fSymbolTable;
485 AutoSymbolTable table(this);
486 Modifiers mods = this->convertModifiers(intf.fModifiers);
487 std::vector<Type::Field> fields;
488 for (size_t i = 0; i < intf.fDeclarations.size(); i++) {
ethannicholas14fe8cc2016-09-07 13:37:16 -0700489 std::unique_ptr<VarDeclarations> decl = this->convertVarDeclarations(
ethannicholasb3058bd2016-07-01 08:22:01 -0700490 *intf.fDeclarations[i],
491 Variable::kGlobal_Storage);
ethannicholas14fe8cc2016-09-07 13:37:16 -0700492 for (const auto& var : decl->fVars) {
493 fields.push_back(Type::Field(var.fVar->fModifiers, var.fVar->fName,
494 &var.fVar->fType));
495 if (var.fValue) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700496 fErrors.error(decl->fPosition,
497 "initializers are not permitted on interface block fields");
498 }
ethannicholas14fe8cc2016-09-07 13:37:16 -0700499 if (var.fVar->fModifiers.fFlags & (Modifiers::kIn_Flag |
500 Modifiers::kOut_Flag |
501 Modifiers::kUniform_Flag |
502 Modifiers::kConst_Flag)) {
503 fErrors.error(decl->fPosition,
ethannicholasb3058bd2016-07-01 08:22:01 -0700504 "interface block fields may not have storage qualifiers");
505 }
506 }
507 }
ethannicholasd598f792016-07-25 10:08:54 -0700508 Type* type = new Type(intf.fInterfaceName, fields);
509 fSymbolTable->takeOwnership(type);
ethannicholasb3058bd2016-07-01 08:22:01 -0700510 std::string name = intf.fValueName.length() > 0 ? intf.fValueName : intf.fInterfaceName;
ethannicholasd598f792016-07-25 10:08:54 -0700511 Variable* var = new Variable(intf.fPosition, mods, name, *type, Variable::kGlobal_Storage);
512 fSymbolTable->takeOwnership(var);
ethannicholasb3058bd2016-07-01 08:22:01 -0700513 if (intf.fValueName.length()) {
ethannicholasd598f792016-07-25 10:08:54 -0700514 old->addWithoutOwnership(intf.fValueName, var);
ethannicholasb3058bd2016-07-01 08:22:01 -0700515 } else {
516 for (size_t i = 0; i < fields.size(); i++) {
ethannicholasd598f792016-07-25 10:08:54 -0700517 old->add(fields[i].fName, std::unique_ptr<Field>(new Field(intf.fPosition, *var,
518 (int) i)));
ethannicholasb3058bd2016-07-01 08:22:01 -0700519 }
520 }
ethannicholasd598f792016-07-25 10:08:54 -0700521 return std::unique_ptr<InterfaceBlock>(new InterfaceBlock(intf.fPosition, *var, fSymbolTable));
ethannicholasb3058bd2016-07-01 08:22:01 -0700522}
523
ethannicholasd598f792016-07-25 10:08:54 -0700524const Type* IRGenerator::convertType(const ASTType& type) {
525 const Symbol* result = (*fSymbolTable)[type.fName];
ethannicholasb3058bd2016-07-01 08:22:01 -0700526 if (result && result->fKind == Symbol::kType_Kind) {
ethannicholasd598f792016-07-25 10:08:54 -0700527 return (const Type*) result;
ethannicholasb3058bd2016-07-01 08:22:01 -0700528 }
529 fErrors.error(type.fPosition, "unknown type '" + type.fName + "'");
530 return nullptr;
531}
532
533std::unique_ptr<Expression> IRGenerator::convertExpression(const ASTExpression& expr) {
534 switch (expr.fKind) {
535 case ASTExpression::kIdentifier_Kind:
536 return this->convertIdentifier((ASTIdentifier&) expr);
537 case ASTExpression::kBool_Kind:
ethannicholasd598f792016-07-25 10:08:54 -0700538 return std::unique_ptr<Expression>(new BoolLiteral(fContext, expr.fPosition,
ethannicholasb3058bd2016-07-01 08:22:01 -0700539 ((ASTBoolLiteral&) expr).fValue));
540 case ASTExpression::kInt_Kind:
ethannicholasd598f792016-07-25 10:08:54 -0700541 return std::unique_ptr<Expression>(new IntLiteral(fContext, expr.fPosition,
ethannicholasb3058bd2016-07-01 08:22:01 -0700542 ((ASTIntLiteral&) expr).fValue));
543 case ASTExpression::kFloat_Kind:
ethannicholasd598f792016-07-25 10:08:54 -0700544 return std::unique_ptr<Expression>(new FloatLiteral(fContext, expr.fPosition,
ethannicholasb3058bd2016-07-01 08:22:01 -0700545 ((ASTFloatLiteral&) expr).fValue));
546 case ASTExpression::kBinary_Kind:
547 return this->convertBinaryExpression((ASTBinaryExpression&) expr);
548 case ASTExpression::kPrefix_Kind:
549 return this->convertPrefixExpression((ASTPrefixExpression&) expr);
550 case ASTExpression::kSuffix_Kind:
551 return this->convertSuffixExpression((ASTSuffixExpression&) expr);
552 case ASTExpression::kTernary_Kind:
553 return this->convertTernaryExpression((ASTTernaryExpression&) expr);
554 default:
555 ABORT("unsupported expression type: %d\n", expr.fKind);
556 }
557}
558
559std::unique_ptr<Expression> IRGenerator::convertIdentifier(const ASTIdentifier& identifier) {
ethannicholasd598f792016-07-25 10:08:54 -0700560 const Symbol* result = (*fSymbolTable)[identifier.fText];
ethannicholasb3058bd2016-07-01 08:22:01 -0700561 if (!result) {
562 fErrors.error(identifier.fPosition, "unknown identifier '" + identifier.fText + "'");
563 return nullptr;
564 }
565 switch (result->fKind) {
566 case Symbol::kFunctionDeclaration_Kind: {
ethannicholasd598f792016-07-25 10:08:54 -0700567 std::vector<const FunctionDeclaration*> f = {
568 (const FunctionDeclaration*) result
ethannicholasb3058bd2016-07-01 08:22:01 -0700569 };
ethannicholasd598f792016-07-25 10:08:54 -0700570 return std::unique_ptr<FunctionReference>(new FunctionReference(fContext,
571 identifier.fPosition,
572 f));
ethannicholasb3058bd2016-07-01 08:22:01 -0700573 }
574 case Symbol::kUnresolvedFunction_Kind: {
ethannicholasd598f792016-07-25 10:08:54 -0700575 const UnresolvedFunction* f = (const UnresolvedFunction*) result;
576 return std::unique_ptr<FunctionReference>(new FunctionReference(fContext,
577 identifier.fPosition,
ethannicholasb3058bd2016-07-01 08:22:01 -0700578 f->fFunctions));
579 }
580 case Symbol::kVariable_Kind: {
ethannicholasd598f792016-07-25 10:08:54 -0700581 const Variable* var = (const Variable*) result;
582 this->markReadFrom(*var);
ethannicholasb3058bd2016-07-01 08:22:01 -0700583 return std::unique_ptr<VariableReference>(new VariableReference(identifier.fPosition,
ethannicholasd598f792016-07-25 10:08:54 -0700584 *var));
ethannicholasb3058bd2016-07-01 08:22:01 -0700585 }
586 case Symbol::kField_Kind: {
ethannicholasd598f792016-07-25 10:08:54 -0700587 const Field* field = (const Field*) result;
ethannicholasb3058bd2016-07-01 08:22:01 -0700588 VariableReference* base = new VariableReference(identifier.fPosition, field->fOwner);
ethannicholasf789b382016-08-03 12:43:36 -0700589 return std::unique_ptr<Expression>(new FieldAccess(
590 std::unique_ptr<Expression>(base),
591 field->fFieldIndex,
592 FieldAccess::kAnonymousInterfaceBlock_OwnerKind));
ethannicholasb3058bd2016-07-01 08:22:01 -0700593 }
594 case Symbol::kType_Kind: {
ethannicholasd598f792016-07-25 10:08:54 -0700595 const Type* t = (const Type*) result;
596 return std::unique_ptr<TypeReference>(new TypeReference(fContext, identifier.fPosition,
597 *t));
ethannicholasb3058bd2016-07-01 08:22:01 -0700598 }
599 default:
600 ABORT("unsupported symbol type %d\n", result->fKind);
601 }
602
603}
604
605std::unique_ptr<Expression> IRGenerator::coerce(std::unique_ptr<Expression> expr,
ethannicholasd598f792016-07-25 10:08:54 -0700606 const Type& type) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700607 if (!expr) {
608 return nullptr;
609 }
ethannicholasd598f792016-07-25 10:08:54 -0700610 if (expr->fType == type) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700611 return expr;
612 }
613 this->checkValid(*expr);
ethannicholasd598f792016-07-25 10:08:54 -0700614 if (expr->fType == *fContext.fInvalid_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700615 return nullptr;
616 }
ethannicholasd598f792016-07-25 10:08:54 -0700617 if (!expr->fType.canCoerceTo(type)) {
618 fErrors.error(expr->fPosition, "expected '" + type.description() + "', but found '" +
619 expr->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -0700620 return nullptr;
621 }
ethannicholasd598f792016-07-25 10:08:54 -0700622 if (type.kind() == Type::kScalar_Kind) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700623 std::vector<std::unique_ptr<Expression>> args;
624 args.push_back(std::move(expr));
ethannicholasd598f792016-07-25 10:08:54 -0700625 ASTIdentifier id(Position(), type.description());
ethannicholasb3058bd2016-07-01 08:22:01 -0700626 std::unique_ptr<Expression> ctor = this->convertIdentifier(id);
627 ASSERT(ctor);
628 return this->call(Position(), std::move(ctor), std::move(args));
629 }
ethannicholasdcfe6db2016-10-10 10:09:00 -0700630 std::vector<std::unique_ptr<Expression>> args;
631 args.push_back(std::move(expr));
632 return std::unique_ptr<Expression>(new Constructor(Position(), type, std::move(args)));
ethannicholasb3058bd2016-07-01 08:22:01 -0700633}
634
ethannicholasf789b382016-08-03 12:43:36 -0700635static bool is_matrix_multiply(const Type& left, const Type& right) {
636 if (left.kind() == Type::kMatrix_Kind) {
637 return right.kind() == Type::kMatrix_Kind || right.kind() == Type::kVector_Kind;
638 }
639 return left.kind() == Type::kVector_Kind && right.kind() == Type::kMatrix_Kind;
640}
ethannicholasb3058bd2016-07-01 08:22:01 -0700641/**
642 * Determines the operand and result types of a binary expression. Returns true if the expression is
643 * legal, false otherwise. If false, the values of the out parameters are undefined.
644 */
ethannicholasd598f792016-07-25 10:08:54 -0700645static bool determine_binary_type(const Context& context,
646 Token::Kind op,
647 const Type& left,
648 const Type& right,
649 const Type** outLeftType,
650 const Type** outRightType,
651 const Type** outResultType,
ethannicholasb3058bd2016-07-01 08:22:01 -0700652 bool tryFlipped) {
653 bool isLogical;
654 switch (op) {
655 case Token::EQEQ: // fall through
656 case Token::NEQ: // fall through
657 case Token::LT: // fall through
658 case Token::GT: // fall through
659 case Token::LTEQ: // fall through
660 case Token::GTEQ:
661 isLogical = true;
662 break;
663 case Token::LOGICALOR: // fall through
664 case Token::LOGICALAND: // fall through
665 case Token::LOGICALXOR: // fall through
666 case Token::LOGICALOREQ: // fall through
667 case Token::LOGICALANDEQ: // fall through
668 case Token::LOGICALXOREQ:
ethannicholasd598f792016-07-25 10:08:54 -0700669 *outLeftType = context.fBool_Type.get();
670 *outRightType = context.fBool_Type.get();
671 *outResultType = context.fBool_Type.get();
672 return left.canCoerceTo(*context.fBool_Type) &&
673 right.canCoerceTo(*context.fBool_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -0700674 case Token::STAR: // fall through
675 case Token::STAREQ:
ethannicholasf789b382016-08-03 12:43:36 -0700676 if (is_matrix_multiply(left, right)) {
677 // determine final component type
678 if (determine_binary_type(context, Token::STAR, left.componentType(),
679 right.componentType(), outLeftType, outRightType,
680 outResultType, false)) {
681 *outLeftType = &(*outResultType)->toCompound(context, left.columns(),
682 left.rows());;
683 *outRightType = &(*outResultType)->toCompound(context, right.columns(),
684 right.rows());;
685 int leftColumns = left.columns();
686 int leftRows = left.rows();
687 int rightColumns;
688 int rightRows;
689 if (right.kind() == Type::kVector_Kind) {
690 // matrix * vector treats the vector as a column vector, so we need to
691 // transpose it
692 rightColumns = right.rows();
693 rightRows = right.columns();
694 ASSERT(rightColumns == 1);
695 } else {
696 rightColumns = right.columns();
697 rightRows = right.rows();
698 }
699 if (rightColumns > 1) {
700 *outResultType = &(*outResultType)->toCompound(context, rightColumns,
701 leftRows);
702 } else {
703 // result was a column vector, transpose it back to a row
704 *outResultType = &(*outResultType)->toCompound(context, leftRows,
705 rightColumns);
706 }
707 return leftColumns == rightRows;
708 } else {
709 return false;
710 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700711 }
712 // fall through
713 default:
714 isLogical = false;
715 }
716 // FIXME: need to disallow illegal operations like vec3 > vec3. Also do not currently have
717 // full support for numbers other than float.
718 if (left == right) {
ethannicholasd598f792016-07-25 10:08:54 -0700719 *outLeftType = &left;
720 *outRightType = &left;
ethannicholasb3058bd2016-07-01 08:22:01 -0700721 if (isLogical) {
ethannicholasd598f792016-07-25 10:08:54 -0700722 *outResultType = context.fBool_Type.get();
ethannicholasb3058bd2016-07-01 08:22:01 -0700723 } else {
ethannicholasd598f792016-07-25 10:08:54 -0700724 *outResultType = &left;
ethannicholasb3058bd2016-07-01 08:22:01 -0700725 }
726 return true;
727 }
728 // FIXME: incorrect for shift operations
ethannicholasd598f792016-07-25 10:08:54 -0700729 if (left.canCoerceTo(right)) {
730 *outLeftType = &right;
731 *outRightType = &right;
ethannicholasb3058bd2016-07-01 08:22:01 -0700732 if (isLogical) {
ethannicholasd598f792016-07-25 10:08:54 -0700733 *outResultType = context.fBool_Type.get();
ethannicholasb3058bd2016-07-01 08:22:01 -0700734 } else {
ethannicholasd598f792016-07-25 10:08:54 -0700735 *outResultType = &right;
ethannicholasb3058bd2016-07-01 08:22:01 -0700736 }
737 return true;
738 }
ethannicholasd598f792016-07-25 10:08:54 -0700739 if ((left.kind() == Type::kVector_Kind || left.kind() == Type::kMatrix_Kind) &&
740 (right.kind() == Type::kScalar_Kind)) {
741 if (determine_binary_type(context, op, left.componentType(), right, outLeftType,
742 outRightType, outResultType, false)) {
743 *outLeftType = &(*outLeftType)->toCompound(context, left.columns(), left.rows());
ethannicholasb3058bd2016-07-01 08:22:01 -0700744 if (!isLogical) {
ethannicholasd598f792016-07-25 10:08:54 -0700745 *outResultType = &(*outResultType)->toCompound(context, left.columns(),
746 left.rows());
ethannicholasb3058bd2016-07-01 08:22:01 -0700747 }
748 return true;
749 }
750 return false;
751 }
752 if (tryFlipped) {
ethannicholasd598f792016-07-25 10:08:54 -0700753 return determine_binary_type(context, op, right, left, outRightType, outLeftType,
754 outResultType, false);
ethannicholasb3058bd2016-07-01 08:22:01 -0700755 }
756 return false;
757}
758
759std::unique_ptr<Expression> IRGenerator::convertBinaryExpression(
760 const ASTBinaryExpression& expression) {
761 std::unique_ptr<Expression> left = this->convertExpression(*expression.fLeft);
762 if (!left) {
763 return nullptr;
764 }
765 std::unique_ptr<Expression> right = this->convertExpression(*expression.fRight);
766 if (!right) {
767 return nullptr;
768 }
ethannicholasd598f792016-07-25 10:08:54 -0700769 const Type* leftType;
770 const Type* rightType;
771 const Type* resultType;
772 if (!determine_binary_type(fContext, expression.fOperator, left->fType, right->fType, &leftType,
ethannicholasb3058bd2016-07-01 08:22:01 -0700773 &rightType, &resultType, true)) {
774 fErrors.error(expression.fPosition, "type mismatch: '" +
775 Token::OperatorName(expression.fOperator) +
ethannicholasd598f792016-07-25 10:08:54 -0700776 "' cannot operate on '" + left->fType.fName +
777 "', '" + right->fType.fName + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -0700778 return nullptr;
779 }
780 switch (expression.fOperator) {
781 case Token::EQ: // fall through
782 case Token::PLUSEQ: // fall through
783 case Token::MINUSEQ: // fall through
784 case Token::STAREQ: // fall through
785 case Token::SLASHEQ: // fall through
786 case Token::PERCENTEQ: // fall through
787 case Token::SHLEQ: // fall through
788 case Token::SHREQ: // fall through
789 case Token::BITWISEOREQ: // fall through
790 case Token::BITWISEXOREQ: // fall through
791 case Token::BITWISEANDEQ: // fall through
792 case Token::LOGICALOREQ: // fall through
793 case Token::LOGICALXOREQ: // fall through
794 case Token::LOGICALANDEQ:
795 this->markWrittenTo(*left);
796 default:
797 break;
798 }
799 return std::unique_ptr<Expression>(new BinaryExpression(expression.fPosition,
ethannicholasd598f792016-07-25 10:08:54 -0700800 this->coerce(std::move(left),
801 *leftType),
ethannicholasb3058bd2016-07-01 08:22:01 -0700802 expression.fOperator,
803 this->coerce(std::move(right),
ethannicholasd598f792016-07-25 10:08:54 -0700804 *rightType),
805 *resultType));
ethannicholasb3058bd2016-07-01 08:22:01 -0700806}
807
808std::unique_ptr<Expression> IRGenerator::convertTernaryExpression(
809 const ASTTernaryExpression& expression) {
810 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*expression.fTest),
ethannicholasd598f792016-07-25 10:08:54 -0700811 *fContext.fBool_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -0700812 if (!test) {
813 return nullptr;
814 }
815 std::unique_ptr<Expression> ifTrue = this->convertExpression(*expression.fIfTrue);
816 if (!ifTrue) {
817 return nullptr;
818 }
819 std::unique_ptr<Expression> ifFalse = this->convertExpression(*expression.fIfFalse);
820 if (!ifFalse) {
821 return nullptr;
822 }
ethannicholasd598f792016-07-25 10:08:54 -0700823 const Type* trueType;
824 const Type* falseType;
825 const Type* resultType;
826 if (!determine_binary_type(fContext, Token::EQEQ, ifTrue->fType, ifFalse->fType, &trueType,
ethannicholasb3058bd2016-07-01 08:22:01 -0700827 &falseType, &resultType, true)) {
828 fErrors.error(expression.fPosition, "ternary operator result mismatch: '" +
ethannicholasd598f792016-07-25 10:08:54 -0700829 ifTrue->fType.fName + "', '" +
830 ifFalse->fType.fName + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -0700831 return nullptr;
832 }
833 ASSERT(trueType == falseType);
ethannicholasd598f792016-07-25 10:08:54 -0700834 ifTrue = this->coerce(std::move(ifTrue), *trueType);
835 ifFalse = this->coerce(std::move(ifFalse), *falseType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700836 return std::unique_ptr<Expression>(new TernaryExpression(expression.fPosition,
837 std::move(test),
838 std::move(ifTrue),
839 std::move(ifFalse)));
840}
841
ethannicholasd598f792016-07-25 10:08:54 -0700842std::unique_ptr<Expression> IRGenerator::call(Position position,
843 const FunctionDeclaration& function,
844 std::vector<std::unique_ptr<Expression>> arguments) {
845 if (function.fParameters.size() != arguments.size()) {
846 std::string msg = "call to '" + function.fName + "' expected " +
ethannicholasdcfe6db2016-10-10 10:09:00 -0700847 to_string((uint64_t) function.fParameters.size()) +
ethannicholasb3058bd2016-07-01 08:22:01 -0700848 " argument";
ethannicholasd598f792016-07-25 10:08:54 -0700849 if (function.fParameters.size() != 1) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700850 msg += "s";
851 }
ethannicholasdcfe6db2016-10-10 10:09:00 -0700852 msg += ", but found " + to_string((uint64_t) arguments.size());
ethannicholasb3058bd2016-07-01 08:22:01 -0700853 fErrors.error(position, msg);
854 return nullptr;
855 }
856 for (size_t i = 0; i < arguments.size(); i++) {
ethannicholasd598f792016-07-25 10:08:54 -0700857 arguments[i] = this->coerce(std::move(arguments[i]), function.fParameters[i]->fType);
858 if (arguments[i] && (function.fParameters[i]->fModifiers.fFlags & Modifiers::kOut_Flag)) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700859 this->markWrittenTo(*arguments[i]);
860 }
861 }
ethannicholasd598f792016-07-25 10:08:54 -0700862 return std::unique_ptr<FunctionCall>(new FunctionCall(position, function,
ethannicholasb3058bd2016-07-01 08:22:01 -0700863 std::move(arguments)));
864}
865
866/**
867 * Determines the cost of coercing the arguments of a function to the required types. Returns true
868 * if the cost could be computed, false if the call is not valid. Cost has no particular meaning
869 * other than "lower costs are preferred".
870 */
ethannicholasd598f792016-07-25 10:08:54 -0700871bool IRGenerator::determineCallCost(const FunctionDeclaration& function,
ethannicholasb3058bd2016-07-01 08:22:01 -0700872 const std::vector<std::unique_ptr<Expression>>& arguments,
873 int* outCost) {
ethannicholasd598f792016-07-25 10:08:54 -0700874 if (function.fParameters.size() != arguments.size()) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700875 return false;
876 }
877 int total = 0;
878 for (size_t i = 0; i < arguments.size(); i++) {
879 int cost;
ethannicholasd598f792016-07-25 10:08:54 -0700880 if (arguments[i]->fType.determineCoercionCost(function.fParameters[i]->fType, &cost)) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700881 total += cost;
882 } else {
883 return false;
884 }
885 }
886 *outCost = total;
887 return true;
888}
889
890std::unique_ptr<Expression> IRGenerator::call(Position position,
891 std::unique_ptr<Expression> functionValue,
892 std::vector<std::unique_ptr<Expression>> arguments) {
893 if (functionValue->fKind == Expression::kTypeReference_Kind) {
894 return this->convertConstructor(position,
895 ((TypeReference&) *functionValue).fValue,
896 std::move(arguments));
897 }
898 if (functionValue->fKind != Expression::kFunctionReference_Kind) {
899 fErrors.error(position, "'" + functionValue->description() + "' is not a function");
900 return nullptr;
901 }
902 FunctionReference* ref = (FunctionReference*) functionValue.get();
903 int bestCost = INT_MAX;
ethannicholasd598f792016-07-25 10:08:54 -0700904 const FunctionDeclaration* best = nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -0700905 if (ref->fFunctions.size() > 1) {
906 for (const auto& f : ref->fFunctions) {
907 int cost;
ethannicholasd598f792016-07-25 10:08:54 -0700908 if (this->determineCallCost(*f, arguments, &cost) && cost < bestCost) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700909 bestCost = cost;
910 best = f;
911 }
912 }
913 if (best) {
ethannicholasd598f792016-07-25 10:08:54 -0700914 return this->call(position, *best, std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -0700915 }
916 std::string msg = "no match for " + ref->fFunctions[0]->fName + "(";
917 std::string separator = "";
918 for (size_t i = 0; i < arguments.size(); i++) {
919 msg += separator;
920 separator = ", ";
ethannicholasd598f792016-07-25 10:08:54 -0700921 msg += arguments[i]->fType.description();
ethannicholasb3058bd2016-07-01 08:22:01 -0700922 }
923 msg += ")";
924 fErrors.error(position, msg);
925 return nullptr;
926 }
ethannicholasd598f792016-07-25 10:08:54 -0700927 return this->call(position, *ref->fFunctions[0], std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -0700928}
929
930std::unique_ptr<Expression> IRGenerator::convertConstructor(
931 Position position,
ethannicholasd598f792016-07-25 10:08:54 -0700932 const Type& type,
ethannicholasb3058bd2016-07-01 08:22:01 -0700933 std::vector<std::unique_ptr<Expression>> args) {
934 // FIXME: add support for structs and arrays
ethannicholasd598f792016-07-25 10:08:54 -0700935 Type::Kind kind = type.kind();
ethannicholasdcfe6db2016-10-10 10:09:00 -0700936 if (!type.isNumber() && kind != Type::kVector_Kind && kind != Type::kMatrix_Kind &&
937 kind != Type::kArray_Kind) {
ethannicholasd598f792016-07-25 10:08:54 -0700938 fErrors.error(position, "cannot construct '" + type.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -0700939 return nullptr;
940 }
ethannicholasd598f792016-07-25 10:08:54 -0700941 if (type == *fContext.fFloat_Type && args.size() == 1 &&
ethannicholasb3058bd2016-07-01 08:22:01 -0700942 args[0]->fKind == Expression::kIntLiteral_Kind) {
943 int64_t value = ((IntLiteral&) *args[0]).fValue;
ethannicholasd598f792016-07-25 10:08:54 -0700944 return std::unique_ptr<Expression>(new FloatLiteral(fContext, position, (double) value));
ethannicholasb3058bd2016-07-01 08:22:01 -0700945 }
946 if (args.size() == 1 && args[0]->fType == type) {
947 // argument is already the right type, just return it
948 return std::move(args[0]);
949 }
ethannicholasd598f792016-07-25 10:08:54 -0700950 if (type.isNumber()) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700951 if (args.size() != 1) {
ethannicholasd598f792016-07-25 10:08:54 -0700952 fErrors.error(position, "invalid arguments to '" + type.description() +
ethannicholasb3058bd2016-07-01 08:22:01 -0700953 "' constructor, (expected exactly 1 argument, but found " +
ethannicholasdcfe6db2016-10-10 10:09:00 -0700954 to_string((uint64_t) args.size()) + ")");
ethannicholasb3058bd2016-07-01 08:22:01 -0700955 }
ethannicholasd598f792016-07-25 10:08:54 -0700956 if (args[0]->fType == *fContext.fBool_Type) {
957 std::unique_ptr<IntLiteral> zero(new IntLiteral(fContext, position, 0));
958 std::unique_ptr<IntLiteral> one(new IntLiteral(fContext, position, 1));
ethannicholasb3058bd2016-07-01 08:22:01 -0700959 return std::unique_ptr<Expression>(
960 new TernaryExpression(position, std::move(args[0]),
961 this->coerce(std::move(one), type),
962 this->coerce(std::move(zero),
963 type)));
ethannicholasd598f792016-07-25 10:08:54 -0700964 } else if (!args[0]->fType.isNumber()) {
965 fErrors.error(position, "invalid argument to '" + type.description() +
ethannicholasb3058bd2016-07-01 08:22:01 -0700966 "' constructor (expected a number or bool, but found '" +
ethannicholasd598f792016-07-25 10:08:54 -0700967 args[0]->fType.description() + "')");
ethannicholasb3058bd2016-07-01 08:22:01 -0700968 }
ethannicholasdcfe6db2016-10-10 10:09:00 -0700969 if (args[0]->fKind == Expression::kIntLiteral_Kind && (type == *fContext.fInt_Type ||
970 type == *fContext.fUInt_Type)) {
971 return std::unique_ptr<Expression>(new IntLiteral(fContext,
972 position,
973 ((IntLiteral&) *args[0]).fValue,
974 &type));
975 }
976 } else if (kind == Type::kArray_Kind) {
977 const Type& base = type.componentType();
978 for (size_t i = 0; i < args.size(); i++) {
979 args[i] = this->coerce(std::move(args[i]), base);
980 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700981 } else {
982 ASSERT(kind == Type::kVector_Kind || kind == Type::kMatrix_Kind);
983 int actual = 0;
984 for (size_t i = 0; i < args.size(); i++) {
ethannicholasd598f792016-07-25 10:08:54 -0700985 if (args[i]->fType.kind() == Type::kVector_Kind ||
986 args[i]->fType.kind() == Type::kMatrix_Kind) {
987 int columns = args[i]->fType.columns();
988 int rows = args[i]->fType.rows();
ethannicholasb3058bd2016-07-01 08:22:01 -0700989 args[i] = this->coerce(std::move(args[i]),
ethannicholasd598f792016-07-25 10:08:54 -0700990 type.componentType().toCompound(fContext, columns, rows));
991 actual += args[i]->fType.rows() * args[i]->fType.columns();
992 } else if (args[i]->fType.kind() == Type::kScalar_Kind) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700993 actual += 1;
ethannicholasd598f792016-07-25 10:08:54 -0700994 if (type.kind() != Type::kScalar_Kind) {
995 args[i] = this->coerce(std::move(args[i]), type.componentType());
ethannicholasb3058bd2016-07-01 08:22:01 -0700996 }
997 } else {
ethannicholasd598f792016-07-25 10:08:54 -0700998 fErrors.error(position, "'" + args[i]->fType.description() + "' is not a valid "
999 "parameter to '" + type.description() + "' constructor");
ethannicholasb3058bd2016-07-01 08:22:01 -07001000 return nullptr;
1001 }
1002 }
ethannicholasd598f792016-07-25 10:08:54 -07001003 int min = type.rows() * type.columns();
1004 int max = type.columns() > 1 ? INT_MAX : min;
ethannicholasb3058bd2016-07-01 08:22:01 -07001005 if ((actual < min || actual > max) &&
1006 !((kind == Type::kVector_Kind || kind == Type::kMatrix_Kind) && (actual == 1))) {
ethannicholasd598f792016-07-25 10:08:54 -07001007 fErrors.error(position, "invalid arguments to '" + type.description() +
ethannicholasb3058bd2016-07-01 08:22:01 -07001008 "' constructor (expected " + to_string(min) + " scalar" +
1009 (min == 1 ? "" : "s") + ", but found " + to_string(actual) +
1010 ")");
1011 return nullptr;
1012 }
1013 }
1014 return std::unique_ptr<Expression>(new Constructor(position, std::move(type), std::move(args)));
1015}
1016
1017std::unique_ptr<Expression> IRGenerator::convertPrefixExpression(
1018 const ASTPrefixExpression& expression) {
1019 std::unique_ptr<Expression> base = this->convertExpression(*expression.fOperand);
1020 if (!base) {
1021 return nullptr;
1022 }
1023 switch (expression.fOperator) {
1024 case Token::PLUS:
ethannicholasd598f792016-07-25 10:08:54 -07001025 if (!base->fType.isNumber() && base->fType.kind() != Type::kVector_Kind) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001026 fErrors.error(expression.fPosition,
ethannicholasd598f792016-07-25 10:08:54 -07001027 "'+' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001028 return nullptr;
1029 }
1030 return base;
1031 case Token::MINUS:
ethannicholasd598f792016-07-25 10:08:54 -07001032 if (!base->fType.isNumber() && base->fType.kind() != Type::kVector_Kind) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001033 fErrors.error(expression.fPosition,
ethannicholasd598f792016-07-25 10:08:54 -07001034 "'-' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001035 return nullptr;
1036 }
1037 if (base->fKind == Expression::kIntLiteral_Kind) {
ethannicholasd598f792016-07-25 10:08:54 -07001038 return std::unique_ptr<Expression>(new IntLiteral(fContext, base->fPosition,
ethannicholasb3058bd2016-07-01 08:22:01 -07001039 -((IntLiteral&) *base).fValue));
1040 }
1041 if (base->fKind == Expression::kFloatLiteral_Kind) {
1042 double value = -((FloatLiteral&) *base).fValue;
ethannicholasd598f792016-07-25 10:08:54 -07001043 return std::unique_ptr<Expression>(new FloatLiteral(fContext, base->fPosition,
1044 value));
ethannicholasb3058bd2016-07-01 08:22:01 -07001045 }
1046 return std::unique_ptr<Expression>(new PrefixExpression(Token::MINUS, std::move(base)));
1047 case Token::PLUSPLUS:
ethannicholasd598f792016-07-25 10:08:54 -07001048 if (!base->fType.isNumber()) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001049 fErrors.error(expression.fPosition,
1050 "'" + Token::OperatorName(expression.fOperator) +
ethannicholasd598f792016-07-25 10:08:54 -07001051 "' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001052 return nullptr;
1053 }
1054 this->markWrittenTo(*base);
1055 break;
1056 case Token::MINUSMINUS:
ethannicholasd598f792016-07-25 10:08:54 -07001057 if (!base->fType.isNumber()) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001058 fErrors.error(expression.fPosition,
1059 "'" + Token::OperatorName(expression.fOperator) +
ethannicholasd598f792016-07-25 10:08:54 -07001060 "' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001061 return nullptr;
1062 }
1063 this->markWrittenTo(*base);
1064 break;
ethannicholasdcfe6db2016-10-10 10:09:00 -07001065 case Token::LOGICALNOT:
ethannicholasd598f792016-07-25 10:08:54 -07001066 if (base->fType != *fContext.fBool_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001067 fErrors.error(expression.fPosition,
1068 "'" + Token::OperatorName(expression.fOperator) +
ethannicholasd598f792016-07-25 10:08:54 -07001069 "' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001070 return nullptr;
1071 }
1072 break;
ethannicholasdcfe6db2016-10-10 10:09:00 -07001073 case Token::BITWISENOT:
1074 if (base->fType != *fContext.fInt_Type) {
1075 fErrors.error(expression.fPosition,
1076 "'" + Token::OperatorName(expression.fOperator) +
1077 "' cannot operate on '" + base->fType.description() + "'");
1078 return nullptr;
1079 }
1080 break;
ethannicholasb3058bd2016-07-01 08:22:01 -07001081 default:
1082 ABORT("unsupported prefix operator\n");
1083 }
1084 return std::unique_ptr<Expression>(new PrefixExpression(expression.fOperator,
1085 std::move(base)));
1086}
1087
1088std::unique_ptr<Expression> IRGenerator::convertIndex(std::unique_ptr<Expression> base,
1089 const ASTExpression& index) {
ethannicholasdcfe6db2016-10-10 10:09:00 -07001090 if (base->fType.kind() != Type::kArray_Kind && base->fType.kind() != Type::kMatrix_Kind &&
1091 base->fType.kind() != Type::kVector_Kind) {
ethannicholasd598f792016-07-25 10:08:54 -07001092 fErrors.error(base->fPosition, "expected array, but found '" + base->fType.description() +
ethannicholasb3058bd2016-07-01 08:22:01 -07001093 "'");
1094 return nullptr;
1095 }
1096 std::unique_ptr<Expression> converted = this->convertExpression(index);
1097 if (!converted) {
1098 return nullptr;
1099 }
ethannicholasdcfe6db2016-10-10 10:09:00 -07001100 if (converted->fType != *fContext.fUInt_Type) {
1101 converted = this->coerce(std::move(converted), *fContext.fInt_Type);
1102 if (!converted) {
1103 return nullptr;
1104 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001105 }
ethannicholasd598f792016-07-25 10:08:54 -07001106 return std::unique_ptr<Expression>(new IndexExpression(fContext, std::move(base),
1107 std::move(converted)));
ethannicholasb3058bd2016-07-01 08:22:01 -07001108}
1109
1110std::unique_ptr<Expression> IRGenerator::convertField(std::unique_ptr<Expression> base,
1111 const std::string& field) {
ethannicholasd598f792016-07-25 10:08:54 -07001112 auto fields = base->fType.fields();
ethannicholasb3058bd2016-07-01 08:22:01 -07001113 for (size_t i = 0; i < fields.size(); i++) {
1114 if (fields[i].fName == field) {
1115 return std::unique_ptr<Expression>(new FieldAccess(std::move(base), (int) i));
1116 }
1117 }
ethannicholasd598f792016-07-25 10:08:54 -07001118 fErrors.error(base->fPosition, "type '" + base->fType.description() + "' does not have a "
ethannicholasb3058bd2016-07-01 08:22:01 -07001119 "field named '" + field + "");
1120 return nullptr;
1121}
1122
1123std::unique_ptr<Expression> IRGenerator::convertSwizzle(std::unique_ptr<Expression> base,
1124 const std::string& fields) {
ethannicholasd598f792016-07-25 10:08:54 -07001125 if (base->fType.kind() != Type::kVector_Kind) {
1126 fErrors.error(base->fPosition, "cannot swizzle type '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001127 return nullptr;
1128 }
1129 std::vector<int> swizzleComponents;
1130 for (char c : fields) {
1131 switch (c) {
1132 case 'x': // fall through
1133 case 'r': // fall through
1134 case 's':
1135 swizzleComponents.push_back(0);
1136 break;
1137 case 'y': // fall through
1138 case 'g': // fall through
1139 case 't':
ethannicholasd598f792016-07-25 10:08:54 -07001140 if (base->fType.columns() >= 2) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001141 swizzleComponents.push_back(1);
1142 break;
1143 }
1144 // fall through
1145 case 'z': // fall through
1146 case 'b': // fall through
1147 case 'p':
ethannicholasd598f792016-07-25 10:08:54 -07001148 if (base->fType.columns() >= 3) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001149 swizzleComponents.push_back(2);
1150 break;
1151 }
1152 // fall through
1153 case 'w': // fall through
1154 case 'a': // fall through
1155 case 'q':
ethannicholasd598f792016-07-25 10:08:54 -07001156 if (base->fType.columns() >= 4) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001157 swizzleComponents.push_back(3);
1158 break;
1159 }
1160 // fall through
1161 default:
1162 fErrors.error(base->fPosition, "invalid swizzle component '" + std::string(1, c) +
1163 "'");
1164 return nullptr;
1165 }
1166 }
1167 ASSERT(swizzleComponents.size() > 0);
1168 if (swizzleComponents.size() > 4) {
1169 fErrors.error(base->fPosition, "too many components in swizzle mask '" + fields + "'");
1170 return nullptr;
1171 }
ethannicholasd598f792016-07-25 10:08:54 -07001172 return std::unique_ptr<Expression>(new Swizzle(fContext, std::move(base), swizzleComponents));
ethannicholasb3058bd2016-07-01 08:22:01 -07001173}
1174
1175std::unique_ptr<Expression> IRGenerator::convertSuffixExpression(
1176 const ASTSuffixExpression& expression) {
1177 std::unique_ptr<Expression> base = this->convertExpression(*expression.fBase);
1178 if (!base) {
1179 return nullptr;
1180 }
1181 switch (expression.fSuffix->fKind) {
ethannicholasdcfe6db2016-10-10 10:09:00 -07001182 case ASTSuffix::kIndex_Kind: {
1183 const ASTExpression* expr = ((ASTIndexSuffix&) *expression.fSuffix).fExpression.get();
1184 if (expr) {
1185 return this->convertIndex(std::move(base), *expr);
1186 } else if (base->fKind == Expression::kTypeReference_Kind) {
1187 const Type& oldType = ((TypeReference&) *base).fValue;
1188 Type* newType = new Type(oldType.name() + "[]", Type::kArray_Kind, oldType,
1189 -1);
1190 fSymbolTable->takeOwnership(newType);
1191 return std::unique_ptr<Expression>(new TypeReference(fContext, base->fPosition,
1192 *newType));
1193 } else {
1194 fErrors.error(expression.fPosition, "'[]' must follow a type name");
1195 }
1196 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001197 case ASTSuffix::kCall_Kind: {
1198 auto rawArguments = &((ASTCallSuffix&) *expression.fSuffix).fArguments;
1199 std::vector<std::unique_ptr<Expression>> arguments;
1200 for (size_t i = 0; i < rawArguments->size(); i++) {
1201 std::unique_ptr<Expression> converted =
1202 this->convertExpression(*(*rawArguments)[i]);
1203 if (!converted) {
1204 return nullptr;
1205 }
1206 arguments.push_back(std::move(converted));
1207 }
1208 return this->call(expression.fPosition, std::move(base), std::move(arguments));
1209 }
1210 case ASTSuffix::kField_Kind: {
ethannicholasd598f792016-07-25 10:08:54 -07001211 switch (base->fType.kind()) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001212 case Type::kVector_Kind:
1213 return this->convertSwizzle(std::move(base),
1214 ((ASTFieldSuffix&) *expression.fSuffix).fField);
1215 case Type::kStruct_Kind:
1216 return this->convertField(std::move(base),
1217 ((ASTFieldSuffix&) *expression.fSuffix).fField);
1218 default:
1219 fErrors.error(base->fPosition, "cannot swizzle value of type '" +
ethannicholasd598f792016-07-25 10:08:54 -07001220 base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001221 return nullptr;
1222 }
1223 }
1224 case ASTSuffix::kPostIncrement_Kind:
ethannicholasd598f792016-07-25 10:08:54 -07001225 if (!base->fType.isNumber()) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001226 fErrors.error(expression.fPosition,
ethannicholasd598f792016-07-25 10:08:54 -07001227 "'++' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001228 return nullptr;
1229 }
1230 this->markWrittenTo(*base);
1231 return std::unique_ptr<Expression>(new PostfixExpression(std::move(base),
1232 Token::PLUSPLUS));
1233 case ASTSuffix::kPostDecrement_Kind:
ethannicholasd598f792016-07-25 10:08:54 -07001234 if (!base->fType.isNumber()) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001235 fErrors.error(expression.fPosition,
ethannicholasd598f792016-07-25 10:08:54 -07001236 "'--' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001237 return nullptr;
1238 }
1239 this->markWrittenTo(*base);
1240 return std::unique_ptr<Expression>(new PostfixExpression(std::move(base),
1241 Token::MINUSMINUS));
1242 default:
1243 ABORT("unsupported suffix operator");
1244 }
1245}
1246
1247void IRGenerator::checkValid(const Expression& expr) {
1248 switch (expr.fKind) {
1249 case Expression::kFunctionReference_Kind:
1250 fErrors.error(expr.fPosition, "expected '(' to begin function call");
1251 break;
1252 case Expression::kTypeReference_Kind:
1253 fErrors.error(expr.fPosition, "expected '(' to begin constructor invocation");
1254 break;
1255 default:
ethannicholasd598f792016-07-25 10:08:54 -07001256 ASSERT(expr.fType != *fContext.fInvalid_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -07001257 break;
1258 }
1259}
1260
ethannicholasd598f792016-07-25 10:08:54 -07001261void IRGenerator::markReadFrom(const Variable& var) {
1262 var.fIsReadFrom = true;
ethannicholasb3058bd2016-07-01 08:22:01 -07001263}
1264
1265static bool has_duplicates(const Swizzle& swizzle) {
1266 int bits = 0;
1267 for (int idx : swizzle.fComponents) {
1268 ASSERT(idx >= 0 && idx <= 3);
1269 int bit = 1 << idx;
1270 if (bits & bit) {
1271 return true;
1272 }
1273 bits |= bit;
1274 }
1275 return false;
1276}
1277
1278void IRGenerator::markWrittenTo(const Expression& expr) {
1279 switch (expr.fKind) {
1280 case Expression::kVariableReference_Kind: {
ethannicholasd598f792016-07-25 10:08:54 -07001281 const Variable& var = ((VariableReference&) expr).fVariable;
ethannicholasb3058bd2016-07-01 08:22:01 -07001282 if (var.fModifiers.fFlags & (Modifiers::kConst_Flag | Modifiers::kUniform_Flag)) {
1283 fErrors.error(expr.fPosition,
1284 "cannot modify immutable variable '" + var.fName + "'");
1285 }
1286 var.fIsWrittenTo = true;
1287 break;
1288 }
1289 case Expression::kFieldAccess_Kind:
1290 this->markWrittenTo(*((FieldAccess&) expr).fBase);
1291 break;
1292 case Expression::kSwizzle_Kind:
1293 if (has_duplicates((Swizzle&) expr)) {
1294 fErrors.error(expr.fPosition,
1295 "cannot write to the same swizzle field more than once");
1296 }
1297 this->markWrittenTo(*((Swizzle&) expr).fBase);
1298 break;
1299 case Expression::kIndex_Kind:
1300 this->markWrittenTo(*((IndexExpression&) expr).fBase);
1301 break;
1302 default:
1303 fErrors.error(expr.fPosition, "cannot assign to '" + expr.description() + "'");
1304 break;
1305 }
1306}
1307
1308}