blob: dd043b304eccc310aef7b0e570b2ec3a2e11c26c [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 */
Ethan Nicholas11d53972016-11-28 11:23:23 -05007
ethannicholasb3058bd2016-07-01 08:22:01 -07008#include "SkSLIRGenerator.h"
9
10#include "limits.h"
Ethan Nicholasaf197692017-02-27 13:26:45 -050011#include <unordered_set>
ethannicholasb3058bd2016-07-01 08:22:01 -070012
Ethan Nicholas941e7e22016-12-12 15:33:30 -050013#include "SkSLCompiler.h"
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -040014#include "SkSLParser.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070015#include "ast/SkSLASTBoolLiteral.h"
16#include "ast/SkSLASTFieldSuffix.h"
17#include "ast/SkSLASTFloatLiteral.h"
18#include "ast/SkSLASTIndexSuffix.h"
19#include "ast/SkSLASTIntLiteral.h"
20#include "ir/SkSLBinaryExpression.h"
21#include "ir/SkSLBoolLiteral.h"
22#include "ir/SkSLBreakStatement.h"
23#include "ir/SkSLConstructor.h"
24#include "ir/SkSLContinueStatement.h"
25#include "ir/SkSLDiscardStatement.h"
26#include "ir/SkSLDoStatement.h"
Ethan Nicholasaae47c82017-11-10 15:34:03 -050027#include "ir/SkSLEnum.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070028#include "ir/SkSLExpressionStatement.h"
29#include "ir/SkSLField.h"
30#include "ir/SkSLFieldAccess.h"
31#include "ir/SkSLFloatLiteral.h"
32#include "ir/SkSLForStatement.h"
33#include "ir/SkSLFunctionCall.h"
34#include "ir/SkSLFunctionDeclaration.h"
35#include "ir/SkSLFunctionDefinition.h"
36#include "ir/SkSLFunctionReference.h"
37#include "ir/SkSLIfStatement.h"
38#include "ir/SkSLIndexExpression.h"
39#include "ir/SkSLInterfaceBlock.h"
40#include "ir/SkSLIntLiteral.h"
41#include "ir/SkSLLayout.h"
42#include "ir/SkSLPostfixExpression.h"
43#include "ir/SkSLPrefixExpression.h"
44#include "ir/SkSLReturnStatement.h"
Ethan Nicholas762466e2017-06-29 10:03:38 -040045#include "ir/SkSLSetting.h"
Ethan Nicholasaf197692017-02-27 13:26:45 -050046#include "ir/SkSLSwitchCase.h"
47#include "ir/SkSLSwitchStatement.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070048#include "ir/SkSLSwizzle.h"
49#include "ir/SkSLTernaryExpression.h"
50#include "ir/SkSLUnresolvedFunction.h"
51#include "ir/SkSLVariable.h"
ethannicholas22f939e2016-10-13 13:25:34 -070052#include "ir/SkSLVarDeclarations.h"
53#include "ir/SkSLVarDeclarationsStatement.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070054#include "ir/SkSLVariableReference.h"
55#include "ir/SkSLWhileStatement.h"
56
57namespace SkSL {
58
59class AutoSymbolTable {
60public:
Ethan Nicholas11d53972016-11-28 11:23:23 -050061 AutoSymbolTable(IRGenerator* ir)
ethannicholasb3058bd2016-07-01 08:22:01 -070062 : fIR(ir)
63 , fPrevious(fIR->fSymbolTable) {
64 fIR->pushSymbolTable();
65 }
66
67 ~AutoSymbolTable() {
68 fIR->popSymbolTable();
69 ASSERT(fPrevious == fIR->fSymbolTable);
70 }
71
72 IRGenerator* fIR;
73 std::shared_ptr<SymbolTable> fPrevious;
74};
75
ethannicholas22f939e2016-10-13 13:25:34 -070076class AutoLoopLevel {
77public:
Ethan Nicholas11d53972016-11-28 11:23:23 -050078 AutoLoopLevel(IRGenerator* ir)
ethannicholas22f939e2016-10-13 13:25:34 -070079 : fIR(ir) {
80 fIR->fLoopLevel++;
81 }
82
83 ~AutoLoopLevel() {
84 fIR->fLoopLevel--;
85 }
86
87 IRGenerator* fIR;
88};
89
Ethan Nicholasaf197692017-02-27 13:26:45 -050090class AutoSwitchLevel {
91public:
92 AutoSwitchLevel(IRGenerator* ir)
93 : fIR(ir) {
94 fIR->fSwitchLevel++;
95 }
96
97 ~AutoSwitchLevel() {
98 fIR->fSwitchLevel--;
99 }
100
101 IRGenerator* fIR;
102};
103
Ethan Nicholas11d53972016-11-28 11:23:23 -0500104IRGenerator::IRGenerator(const Context* context, std::shared_ptr<SymbolTable> symbolTable,
ethannicholasb3058bd2016-07-01 08:22:01 -0700105 ErrorReporter& errorReporter)
ethannicholasd598f792016-07-25 10:08:54 -0700106: fContext(*context)
107, fCurrentFunction(nullptr)
Ethan Nicholas762466e2017-06-29 10:03:38 -0400108, fRootSymbolTable(symbolTable)
109, fSymbolTable(symbolTable)
ethannicholas22f939e2016-10-13 13:25:34 -0700110, fLoopLevel(0)
Ethan Nicholasaf197692017-02-27 13:26:45 -0500111, fSwitchLevel(0)
Ethan Nicholas762466e2017-06-29 10:03:38 -0400112, fTmpCount(0)
ethannicholasd598f792016-07-25 10:08:54 -0700113, fErrors(errorReporter) {}
ethannicholasb3058bd2016-07-01 08:22:01 -0700114
115void IRGenerator::pushSymbolTable() {
Ethan Nicholas8feeff92017-03-30 14:11:58 -0400116 fSymbolTable.reset(new SymbolTable(std::move(fSymbolTable), &fErrors));
ethannicholasb3058bd2016-07-01 08:22:01 -0700117}
118
119void IRGenerator::popSymbolTable() {
120 fSymbolTable = fSymbolTable->fParent;
121}
122
Ethan Nicholas762466e2017-06-29 10:03:38 -0400123static void fill_caps(const SKSL_CAPS_CLASS& caps,
124 std::unordered_map<String, Program::Settings::Value>* capsMap) {
125#define CAP(name) capsMap->insert(std::make_pair(String(#name), \
126 Program::Settings::Value(caps.name())));
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500127 CAP(fbFetchSupport);
128 CAP(fbFetchNeedsCustomOutput);
129 CAP(bindlessTextureSupport);
130 CAP(dropsTileOnZeroDivide);
131 CAP(flatInterpolationSupport);
132 CAP(noperspectiveInterpolationSupport);
133 CAP(multisampleInterpolationSupport);
134 CAP(sampleVariablesSupport);
135 CAP(sampleMaskOverrideCoverageSupport);
136 CAP(externalTextureSupport);
137 CAP(texelFetchSupport);
138 CAP(imageLoadStoreSupport);
139 CAP(mustEnableAdvBlendEqs);
140 CAP(mustEnableSpecificAdvBlendEqs);
141 CAP(mustDeclareFragmentShaderOutput);
142 CAP(canUseAnyFunctionInShader);
Chris Dalton47c8ed32017-11-15 18:27:09 -0700143 CAP(floatIs32Bits);
Ethan Nicholas07990de2017-07-18 09:47:43 -0400144 CAP(integerSupport);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500145#undef CAP
146}
147
148void IRGenerator::start(const Program::Settings* settings) {
149 fSettings = settings;
150 fCapsMap.clear();
151 if (settings->fCaps) {
152 fill_caps(*settings->fCaps, &fCapsMap);
153 }
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500154 this->pushSymbolTable();
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400155 fInvocations = -1;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500156 fInputs.reset();
Ethan Nicholase7e81c12017-12-12 09:48:48 -0500157 fSkPerVertex = nullptr;
158 fRTAdjust = nullptr;
159 fRTAdjustInterfaceBlock = nullptr;
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500160}
161
162void IRGenerator::finish() {
163 this->popSymbolTable();
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500164 fSettings = nullptr;
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500165}
166
ethannicholasb3058bd2016-07-01 08:22:01 -0700167std::unique_ptr<Extension> IRGenerator::convertExtension(const ASTExtension& extension) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700168 return std::unique_ptr<Extension>(new Extension(extension.fOffset, extension.fName));
ethannicholasb3058bd2016-07-01 08:22:01 -0700169}
170
171std::unique_ptr<Statement> IRGenerator::convertStatement(const ASTStatement& statement) {
172 switch (statement.fKind) {
173 case ASTStatement::kBlock_Kind:
174 return this->convertBlock((ASTBlock&) statement);
175 case ASTStatement::kVarDeclaration_Kind:
176 return this->convertVarDeclarationStatement((ASTVarDeclarationStatement&) statement);
Ethan Nicholase7e81c12017-12-12 09:48:48 -0500177 case ASTStatement::kExpression_Kind: {
178 std::unique_ptr<Statement> result =
179 this->convertExpressionStatement((ASTExpressionStatement&) statement);
180 if (fRTAdjust && Program::kGeometry_Kind == fKind) {
181 ASSERT(result->fKind == Statement::kExpression_Kind);
182 Expression& expr = *((ExpressionStatement&) *result).fExpression;
183 if (expr.fKind == Expression::kFunctionCall_Kind) {
184 FunctionCall& fc = (FunctionCall&) expr;
185 if (fc.fFunction.fBuiltin && fc.fFunction.fName == "EmitVertex") {
186 std::vector<std::unique_ptr<Statement>> statements;
187 statements.push_back(getNormalizeSkPositionCode());
188 statements.push_back(std::move(result));
189 return std::unique_ptr<Block>(new Block(statement.fOffset,
190 std::move(statements),
191 fSymbolTable));
192 }
193 }
194 }
195 return result;
196 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700197 case ASTStatement::kIf_Kind:
198 return this->convertIf((ASTIfStatement&) statement);
199 case ASTStatement::kFor_Kind:
200 return this->convertFor((ASTForStatement&) statement);
201 case ASTStatement::kWhile_Kind:
202 return this->convertWhile((ASTWhileStatement&) statement);
203 case ASTStatement::kDo_Kind:
204 return this->convertDo((ASTDoStatement&) statement);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500205 case ASTStatement::kSwitch_Kind:
206 return this->convertSwitch((ASTSwitchStatement&) statement);
ethannicholasb3058bd2016-07-01 08:22:01 -0700207 case ASTStatement::kReturn_Kind:
208 return this->convertReturn((ASTReturnStatement&) statement);
209 case ASTStatement::kBreak_Kind:
210 return this->convertBreak((ASTBreakStatement&) statement);
211 case ASTStatement::kContinue_Kind:
212 return this->convertContinue((ASTContinueStatement&) statement);
213 case ASTStatement::kDiscard_Kind:
214 return this->convertDiscard((ASTDiscardStatement&) statement);
215 default:
216 ABORT("unsupported statement type: %d\n", statement.fKind);
217 }
218}
219
220std::unique_ptr<Block> IRGenerator::convertBlock(const ASTBlock& block) {
221 AutoSymbolTable table(this);
222 std::vector<std::unique_ptr<Statement>> statements;
223 for (size_t i = 0; i < block.fStatements.size(); i++) {
224 std::unique_ptr<Statement> statement = this->convertStatement(*block.fStatements[i]);
225 if (!statement) {
226 return nullptr;
227 }
228 statements.push_back(std::move(statement));
229 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700230 return std::unique_ptr<Block>(new Block(block.fOffset, std::move(statements), fSymbolTable));
ethannicholasb3058bd2016-07-01 08:22:01 -0700231}
232
233std::unique_ptr<Statement> IRGenerator::convertVarDeclarationStatement(
234 const ASTVarDeclarationStatement& s) {
ethannicholas14fe8cc2016-09-07 13:37:16 -0700235 auto decl = this->convertVarDeclarations(*s.fDeclarations, Variable::kLocal_Storage);
ethannicholasb3058bd2016-07-01 08:22:01 -0700236 if (!decl) {
237 return nullptr;
238 }
ethannicholas14fe8cc2016-09-07 13:37:16 -0700239 return std::unique_ptr<Statement>(new VarDeclarationsStatement(std::move(decl)));
ethannicholasb3058bd2016-07-01 08:22:01 -0700240}
241
ethannicholas14fe8cc2016-09-07 13:37:16 -0700242std::unique_ptr<VarDeclarations> IRGenerator::convertVarDeclarations(const ASTVarDeclarations& decl,
243 Variable::Storage storage) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000244 std::vector<std::unique_ptr<VarDeclaration>> variables;
ethannicholasd598f792016-07-25 10:08:54 -0700245 const Type* baseType = this->convertType(*decl.fType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700246 if (!baseType) {
247 return nullptr;
248 }
ethannicholas14fe8cc2016-09-07 13:37:16 -0700249 for (const auto& varDecl : decl.fVars) {
ethannicholasd598f792016-07-25 10:08:54 -0700250 const Type* type = baseType;
ethannicholas14fe8cc2016-09-07 13:37:16 -0700251 std::vector<std::unique_ptr<Expression>> sizes;
252 for (const auto& rawSize : varDecl.fSizes) {
253 if (rawSize) {
254 auto size = this->coerce(this->convertExpression(*rawSize), *fContext.fInt_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -0700255 if (!size) {
256 return nullptr;
257 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700258 String name(type->fName);
Ethan Nicholas50afc172017-02-16 14:49:57 -0500259 int64_t count;
ethannicholasb3058bd2016-07-01 08:22:01 -0700260 if (size->fKind == Expression::kIntLiteral_Kind) {
261 count = ((IntLiteral&) *size).fValue;
262 if (count <= 0) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700263 fErrors.error(size->fOffset, "array size must be positive");
ethannicholasb3058bd2016-07-01 08:22:01 -0700264 }
265 name += "[" + to_string(count) + "]";
266 } else {
267 count = -1;
268 name += "[]";
269 }
ethannicholasd598f792016-07-25 10:08:54 -0700270 type = new Type(name, Type::kArray_Kind, *type, (int) count);
271 fSymbolTable->takeOwnership((Type*) type);
ethannicholas14fe8cc2016-09-07 13:37:16 -0700272 sizes.push_back(std::move(size));
ethannicholasb3058bd2016-07-01 08:22:01 -0700273 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700274 type = new Type(type->name() + "[]", Type::kArray_Kind, *type, -1);
ethannicholasd598f792016-07-25 10:08:54 -0700275 fSymbolTable->takeOwnership((Type*) type);
ethannicholas14fe8cc2016-09-07 13:37:16 -0700276 sizes.push_back(nullptr);
ethannicholasb3058bd2016-07-01 08:22:01 -0700277 }
278 }
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000279 auto var = std::unique_ptr<Variable>(new Variable(decl.fOffset, decl.fModifiers,
280 varDecl.fName, *type, storage));
Ethan Nicholase7e81c12017-12-12 09:48:48 -0500281 if (var->fName == Compiler::RTADJUST_NAME) {
282 ASSERT(!fRTAdjust);
283 ASSERT(var->fType == *fContext.fFloat4_Type);
284 fRTAdjust = var.get();
285 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700286 std::unique_ptr<Expression> value;
ethannicholas14fe8cc2016-09-07 13:37:16 -0700287 if (varDecl.fValue) {
288 value = this->convertExpression(*varDecl.fValue);
ethannicholasb3058bd2016-07-01 08:22:01 -0700289 if (!value) {
290 return nullptr;
291 }
ethannicholasd598f792016-07-25 10:08:54 -0700292 value = this->coerce(std::move(value), *type);
Ethan Nicholascb670962017-04-20 19:31:52 -0400293 var->fWriteCount = 1;
Ethan Nicholas8f6c2ab2018-01-17 13:51:52 -0500294 var->fInitialValue = value.get();
ethannicholasb3058bd2016-07-01 08:22:01 -0700295 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700296 if (storage == Variable::kGlobal_Storage && varDecl.fName == "sk_FragColor" &&
ethannicholasea4567c2016-10-17 11:24:37 -0700297 (*fSymbolTable)[varDecl.fName]) {
ethannicholas5961bc92016-10-12 06:39:56 -0700298 // already defined, ignore
ethannicholasea4567c2016-10-17 11:24:37 -0700299 } else if (storage == Variable::kGlobal_Storage && (*fSymbolTable)[varDecl.fName] &&
300 (*fSymbolTable)[varDecl.fName]->fKind == Symbol::kVariable_Kind &&
ethannicholas5961bc92016-10-12 06:39:56 -0700301 ((Variable*) (*fSymbolTable)[varDecl.fName])->fModifiers.fLayout.fBuiltin >= 0) {
ethannicholasf789b382016-08-03 12:43:36 -0700302 // already defined, just update the modifiers
ethannicholas14fe8cc2016-09-07 13:37:16 -0700303 Variable* old = (Variable*) (*fSymbolTable)[varDecl.fName];
ethannicholasf789b382016-08-03 12:43:36 -0700304 old->fModifiers = var->fModifiers;
305 } else {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000306 variables.emplace_back(new VarDeclaration(var.get(), std::move(sizes),
307 std::move(value)));
ethannicholas14fe8cc2016-09-07 13:37:16 -0700308 fSymbolTable->add(varDecl.fName, std::move(var));
ethannicholasf789b382016-08-03 12:43:36 -0700309 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700310 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700311 return std::unique_ptr<VarDeclarations>(new VarDeclarations(decl.fOffset,
ethannicholas14fe8cc2016-09-07 13:37:16 -0700312 baseType,
313 std::move(variables)));
ethannicholasb3058bd2016-07-01 08:22:01 -0700314}
315
ethannicholas5961bc92016-10-12 06:39:56 -0700316std::unique_ptr<ModifiersDeclaration> IRGenerator::convertModifiersDeclaration(
317 const ASTModifiersDeclaration& m) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400318 Modifiers modifiers = m.fModifiers;
319 if (modifiers.fLayout.fInvocations != -1) {
320 fInvocations = modifiers.fLayout.fInvocations;
Chris Daltonf1b47bb2017-10-06 11:57:51 -0600321 if (fSettings->fCaps && !fSettings->fCaps->gsInvocationsSupport()) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400322 modifiers.fLayout.fInvocations = -1;
323 Variable* invocationId = (Variable*) (*fSymbolTable)["sk_InvocationID"];
324 ASSERT(invocationId);
325 invocationId->fModifiers.fLayout.fBuiltin = -1;
326 if (modifiers.fLayout.description() == "") {
327 return nullptr;
328 }
329 }
330 }
331 if (modifiers.fLayout.fMaxVertices != -1 && fInvocations > 0 && fSettings->fCaps &&
Chris Daltonf1b47bb2017-10-06 11:57:51 -0600332 !fSettings->fCaps->gsInvocationsSupport()) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400333 modifiers.fLayout.fMaxVertices *= fInvocations;
334 }
335 return std::unique_ptr<ModifiersDeclaration>(new ModifiersDeclaration(modifiers));
ethannicholas5961bc92016-10-12 06:39:56 -0700336}
337
ethannicholasb3058bd2016-07-01 08:22:01 -0700338std::unique_ptr<Statement> IRGenerator::convertIf(const ASTIfStatement& s) {
Ethan Nicholas11d53972016-11-28 11:23:23 -0500339 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*s.fTest),
ethannicholasd598f792016-07-25 10:08:54 -0700340 *fContext.fBool_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -0700341 if (!test) {
342 return nullptr;
343 }
344 std::unique_ptr<Statement> ifTrue = this->convertStatement(*s.fIfTrue);
345 if (!ifTrue) {
346 return nullptr;
347 }
348 std::unique_ptr<Statement> ifFalse;
349 if (s.fIfFalse) {
350 ifFalse = this->convertStatement(*s.fIfFalse);
351 if (!ifFalse) {
352 return nullptr;
353 }
354 }
ethannicholas08a92112016-11-09 13:26:45 -0800355 if (test->fKind == Expression::kBoolLiteral_Kind) {
356 // static boolean value, fold down to a single branch
357 if (((BoolLiteral&) *test).fValue) {
358 return ifTrue;
359 } else if (s.fIfFalse) {
360 return ifFalse;
361 } else {
362 // False & no else clause. Not an error, so don't return null!
363 std::vector<std::unique_ptr<Statement>> empty;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700364 return std::unique_ptr<Statement>(new Block(s.fOffset, std::move(empty),
ethannicholas08a92112016-11-09 13:26:45 -0800365 fSymbolTable));
366 }
367 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700368 return std::unique_ptr<Statement>(new IfStatement(s.fOffset, s.fIsStatic, std::move(test),
ethannicholasb3058bd2016-07-01 08:22:01 -0700369 std::move(ifTrue), std::move(ifFalse)));
370}
371
372std::unique_ptr<Statement> IRGenerator::convertFor(const ASTForStatement& f) {
ethannicholas22f939e2016-10-13 13:25:34 -0700373 AutoLoopLevel level(this);
ethannicholasb3058bd2016-07-01 08:22:01 -0700374 AutoSymbolTable table(this);
ethannicholas22f939e2016-10-13 13:25:34 -0700375 std::unique_ptr<Statement> initializer;
376 if (f.fInitializer) {
377 initializer = this->convertStatement(*f.fInitializer);
378 if (!initializer) {
379 return nullptr;
380 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700381 }
ethannicholas22f939e2016-10-13 13:25:34 -0700382 std::unique_ptr<Expression> test;
383 if (f.fTest) {
384 test = this->coerce(this->convertExpression(*f.fTest), *fContext.fBool_Type);
385 if (!test) {
386 return nullptr;
387 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700388 }
ethannicholas22f939e2016-10-13 13:25:34 -0700389 std::unique_ptr<Expression> next;
390 if (f.fNext) {
391 next = this->convertExpression(*f.fNext);
392 if (!next) {
393 return nullptr;
394 }
395 this->checkValid(*next);
ethannicholasb3058bd2016-07-01 08:22:01 -0700396 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700397 std::unique_ptr<Statement> statement = this->convertStatement(*f.fStatement);
398 if (!statement) {
399 return nullptr;
400 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700401 return std::unique_ptr<Statement>(new ForStatement(f.fOffset, std::move(initializer),
ethannicholasb3058bd2016-07-01 08:22:01 -0700402 std::move(test), std::move(next),
ethannicholasd598f792016-07-25 10:08:54 -0700403 std::move(statement), fSymbolTable));
ethannicholasb3058bd2016-07-01 08:22:01 -0700404}
405
406std::unique_ptr<Statement> IRGenerator::convertWhile(const ASTWhileStatement& w) {
ethannicholas22f939e2016-10-13 13:25:34 -0700407 AutoLoopLevel level(this);
Ethan Nicholas11d53972016-11-28 11:23:23 -0500408 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*w.fTest),
ethannicholasd598f792016-07-25 10:08:54 -0700409 *fContext.fBool_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -0700410 if (!test) {
411 return nullptr;
412 }
413 std::unique_ptr<Statement> statement = this->convertStatement(*w.fStatement);
414 if (!statement) {
415 return nullptr;
416 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700417 return std::unique_ptr<Statement>(new WhileStatement(w.fOffset, std::move(test),
ethannicholasb3058bd2016-07-01 08:22:01 -0700418 std::move(statement)));
419}
420
421std::unique_ptr<Statement> IRGenerator::convertDo(const ASTDoStatement& d) {
ethannicholas22f939e2016-10-13 13:25:34 -0700422 AutoLoopLevel level(this);
ethannicholasd598f792016-07-25 10:08:54 -0700423 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*d.fTest),
424 *fContext.fBool_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -0700425 if (!test) {
426 return nullptr;
427 }
428 std::unique_ptr<Statement> statement = this->convertStatement(*d.fStatement);
429 if (!statement) {
430 return nullptr;
431 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700432 return std::unique_ptr<Statement>(new DoStatement(d.fOffset, std::move(statement),
ethannicholasb3058bd2016-07-01 08:22:01 -0700433 std::move(test)));
434}
435
Ethan Nicholasaf197692017-02-27 13:26:45 -0500436std::unique_ptr<Statement> IRGenerator::convertSwitch(const ASTSwitchStatement& s) {
437 AutoSwitchLevel level(this);
438 std::unique_ptr<Expression> value = this->convertExpression(*s.fValue);
439 if (!value) {
440 return nullptr;
441 }
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500442 if (value->fType != *fContext.fUInt_Type && value->fType.kind() != Type::kEnum_Kind) {
Ethan Nicholasaf197692017-02-27 13:26:45 -0500443 value = this->coerce(std::move(value), *fContext.fInt_Type);
444 if (!value) {
445 return nullptr;
446 }
447 }
448 AutoSymbolTable table(this);
449 std::unordered_set<int> caseValues;
450 std::vector<std::unique_ptr<SwitchCase>> cases;
451 for (const auto& c : s.fCases) {
452 std::unique_ptr<Expression> caseValue;
453 if (c->fValue) {
454 caseValue = this->convertExpression(*c->fValue);
455 if (!caseValue) {
456 return nullptr;
457 }
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500458 caseValue = this->coerce(std::move(caseValue), value->fType);
459 if (!caseValue) {
460 return nullptr;
Ethan Nicholasaf197692017-02-27 13:26:45 -0500461 }
462 if (!caseValue->isConstant()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700463 fErrors.error(caseValue->fOffset, "case value must be a constant");
Ethan Nicholasaf197692017-02-27 13:26:45 -0500464 return nullptr;
465 }
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500466 int64_t v;
467 this->getConstantInt(*caseValue, &v);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500468 if (caseValues.find(v) != caseValues.end()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700469 fErrors.error(caseValue->fOffset, "duplicate case value");
Ethan Nicholasaf197692017-02-27 13:26:45 -0500470 }
471 caseValues.insert(v);
472 }
473 std::vector<std::unique_ptr<Statement>> statements;
474 for (const auto& s : c->fStatements) {
475 std::unique_ptr<Statement> converted = this->convertStatement(*s);
476 if (!converted) {
477 return nullptr;
478 }
479 statements.push_back(std::move(converted));
480 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700481 cases.emplace_back(new SwitchCase(c->fOffset, std::move(caseValue),
Ethan Nicholasaf197692017-02-27 13:26:45 -0500482 std::move(statements)));
483 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700484 return std::unique_ptr<Statement>(new SwitchStatement(s.fOffset, s.fIsStatic,
Ethan Nicholasc432b0c2017-07-18 13:22:37 -0400485 std::move(value), std::move(cases),
486 fSymbolTable));
Ethan Nicholasaf197692017-02-27 13:26:45 -0500487}
488
ethannicholasb3058bd2016-07-01 08:22:01 -0700489std::unique_ptr<Statement> IRGenerator::convertExpressionStatement(
490 const ASTExpressionStatement& s) {
491 std::unique_ptr<Expression> e = this->convertExpression(*s.fExpression);
492 if (!e) {
493 return nullptr;
494 }
495 this->checkValid(*e);
496 return std::unique_ptr<Statement>(new ExpressionStatement(std::move(e)));
497}
498
499std::unique_ptr<Statement> IRGenerator::convertReturn(const ASTReturnStatement& r) {
500 ASSERT(fCurrentFunction);
Ethan Nicholase7e81c12017-12-12 09:48:48 -0500501 // early returns from a vertex main function will bypass the sk_Position normalization, so
502 // assert that we aren't doing that. It is of course possible to fix this by adding a
503 // normalization before each return, but it will probably never actually be necessary.
504 ASSERT(Program::kVertex_Kind != fKind || !fRTAdjust || "main" != fCurrentFunction->fName);
ethannicholasb3058bd2016-07-01 08:22:01 -0700505 if (r.fExpression) {
506 std::unique_ptr<Expression> result = this->convertExpression(*r.fExpression);
507 if (!result) {
508 return nullptr;
509 }
ethannicholasd598f792016-07-25 10:08:54 -0700510 if (fCurrentFunction->fReturnType == *fContext.fVoid_Type) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700511 fErrors.error(result->fOffset, "may not return a value from a void function");
ethannicholasb3058bd2016-07-01 08:22:01 -0700512 } else {
513 result = this->coerce(std::move(result), fCurrentFunction->fReturnType);
514 if (!result) {
515 return nullptr;
516 }
517 }
518 return std::unique_ptr<Statement>(new ReturnStatement(std::move(result)));
519 } else {
ethannicholasd598f792016-07-25 10:08:54 -0700520 if (fCurrentFunction->fReturnType != *fContext.fVoid_Type) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700521 fErrors.error(r.fOffset, "expected function to return '" +
522 fCurrentFunction->fReturnType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -0700523 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700524 return std::unique_ptr<Statement>(new ReturnStatement(r.fOffset));
ethannicholasb3058bd2016-07-01 08:22:01 -0700525 }
526}
527
528std::unique_ptr<Statement> IRGenerator::convertBreak(const ASTBreakStatement& b) {
Ethan Nicholasaf197692017-02-27 13:26:45 -0500529 if (fLoopLevel > 0 || fSwitchLevel > 0) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700530 return std::unique_ptr<Statement>(new BreakStatement(b.fOffset));
ethannicholas22f939e2016-10-13 13:25:34 -0700531 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700532 fErrors.error(b.fOffset, "break statement must be inside a loop or switch");
ethannicholas22f939e2016-10-13 13:25:34 -0700533 return nullptr;
534 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700535}
536
537std::unique_ptr<Statement> IRGenerator::convertContinue(const ASTContinueStatement& c) {
ethannicholas22f939e2016-10-13 13:25:34 -0700538 if (fLoopLevel > 0) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700539 return std::unique_ptr<Statement>(new ContinueStatement(c.fOffset));
ethannicholas22f939e2016-10-13 13:25:34 -0700540 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700541 fErrors.error(c.fOffset, "continue statement must be inside a loop");
ethannicholas22f939e2016-10-13 13:25:34 -0700542 return nullptr;
543 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700544}
545
546std::unique_ptr<Statement> IRGenerator::convertDiscard(const ASTDiscardStatement& d) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700547 return std::unique_ptr<Statement>(new DiscardStatement(d.fOffset));
ethannicholasb3058bd2016-07-01 08:22:01 -0700548}
549
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500550std::unique_ptr<Block> IRGenerator::applyInvocationIDWorkaround(std::unique_ptr<Block> main) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400551 Layout invokeLayout;
552 Modifiers invokeModifiers(invokeLayout, Modifiers::kHasSideEffects_Flag);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700553 FunctionDeclaration* invokeDecl = new FunctionDeclaration(-1,
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400554 invokeModifiers,
555 "_invoke",
556 std::vector<const Variable*>(),
557 *fContext.fVoid_Type);
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500558 fProgramElements->push_back(std::unique_ptr<ProgramElement>(
559 new FunctionDefinition(-1, *invokeDecl, std::move(main))));
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400560 fSymbolTable->add(invokeDecl->fName, std::unique_ptr<FunctionDeclaration>(invokeDecl));
561
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000562 std::vector<std::unique_ptr<VarDeclaration>> variables;
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400563 Variable* loopIdx = (Variable*) (*fSymbolTable)["sk_InvocationID"];
564 ASSERT(loopIdx);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700565 std::unique_ptr<Expression> test(new BinaryExpression(-1,
566 std::unique_ptr<Expression>(new VariableReference(-1, *loopIdx)),
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400567 Token::LT,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700568 std::unique_ptr<IntLiteral>(new IntLiteral(fContext, -1, fInvocations)),
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400569 *fContext.fBool_Type));
570 std::unique_ptr<Expression> next(new PostfixExpression(
571 std::unique_ptr<Expression>(
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700572 new VariableReference(-1,
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400573 *loopIdx,
574 VariableReference::kReadWrite_RefKind)),
575 Token::PLUSPLUS));
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700576 ASTIdentifier endPrimitiveID = ASTIdentifier(-1, "EndPrimitive");
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400577 std::unique_ptr<Expression> endPrimitive = this->convertExpression(endPrimitiveID);
578 ASSERT(endPrimitive);
579
580 std::vector<std::unique_ptr<Statement>> loopBody;
581 std::vector<std::unique_ptr<Expression>> invokeArgs;
582 loopBody.push_back(std::unique_ptr<Statement>(new ExpressionStatement(
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700583 this->call(-1,
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400584 *invokeDecl,
585 std::vector<std::unique_ptr<Expression>>()))));
586 loopBody.push_back(std::unique_ptr<Statement>(new ExpressionStatement(
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700587 this->call(-1,
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400588 std::move(endPrimitive),
589 std::vector<std::unique_ptr<Expression>>()))));
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700590 std::unique_ptr<Expression> assignment(new BinaryExpression(-1,
591 std::unique_ptr<Expression>(new VariableReference(-1, *loopIdx)),
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400592 Token::EQ,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700593 std::unique_ptr<IntLiteral>(new IntLiteral(fContext, -1, 0)),
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400594 *fContext.fInt_Type));
595 std::unique_ptr<Statement> initializer(new ExpressionStatement(std::move(assignment)));
596 std::unique_ptr<Statement> loop = std::unique_ptr<Statement>(
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700597 new ForStatement(-1,
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400598 std::move(initializer),
599 std::move(test),
600 std::move(next),
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700601 std::unique_ptr<Block>(new Block(-1, std::move(loopBody))),
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400602 fSymbolTable));
603 std::vector<std::unique_ptr<Statement>> children;
604 children.push_back(std::move(loop));
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700605 return std::unique_ptr<Block>(new Block(-1, std::move(children)));
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400606}
607
Ethan Nicholase7e81c12017-12-12 09:48:48 -0500608std::unique_ptr<Statement> IRGenerator::getNormalizeSkPositionCode() {
609 // sk_Position = float4(sk_Position.x * rtAdjust.x + sk_Position.w * rtAdjust.y,
610 // sk_Position.y * rtAdjust.z + sk_Position.w * rtAdjust.w,
611 // 0,
612 // sk_Position.w);
613 ASSERT(fSkPerVertex && fRTAdjust);
614 #define REF(var) std::unique_ptr<Expression>(\
615 new VariableReference(-1, *var, VariableReference::kRead_RefKind))
616 #define FIELD(var, idx) std::unique_ptr<Expression>(\
617 new FieldAccess(REF(var), idx, FieldAccess::kAnonymousInterfaceBlock_OwnerKind))
618 #define POS std::unique_ptr<Expression>(new FieldAccess(REF(fSkPerVertex), 0, \
619 FieldAccess::kAnonymousInterfaceBlock_OwnerKind))
620 #define ADJUST (fRTAdjustInterfaceBlock ? \
621 FIELD(fRTAdjustInterfaceBlock, fRTAdjustFieldIndex) : \
622 REF(fRTAdjust))
623 #define SWIZZLE(expr, field) std::unique_ptr<Expression>(new Swizzle(fContext, expr, { field }))
624 #define OP(left, op, right) std::unique_ptr<Expression>(\
625 new BinaryExpression(-1, left, op, right, *fContext.fFloat_Type))
626 std::vector<std::unique_ptr<Expression>> children;
627 children.push_back(OP(OP(SWIZZLE(POS, 0), Token::STAR, SWIZZLE(ADJUST, 0)),
628 Token::PLUS,
629 OP(SWIZZLE(POS, 3), Token::STAR, SWIZZLE(ADJUST, 1))));
630 children.push_back(OP(OP(SWIZZLE(POS, 1), Token::STAR, SWIZZLE(ADJUST, 2)),
631 Token::PLUS,
632 OP(SWIZZLE(POS, 3), Token::STAR, SWIZZLE(ADJUST, 3))));
Ethan Nicholas9d8abc52018-01-16 16:37:58 -0500633 children.push_back(std::unique_ptr<Expression>(new FloatLiteral(fContext, -1, 0.0)));
Ethan Nicholase7e81c12017-12-12 09:48:48 -0500634 children.push_back(SWIZZLE(POS, 3));
635 std::unique_ptr<Expression> result = OP(POS, Token::EQ,
636 std::unique_ptr<Expression>(new Constructor(-1,
637 *fContext.fFloat4_Type,
638 std::move(children))));
639 return std::unique_ptr<Statement>(new ExpressionStatement(std::move(result)));
640}
641
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500642void IRGenerator::convertFunction(const ASTFunction& f) {
ethannicholasd598f792016-07-25 10:08:54 -0700643 const Type* returnType = this->convertType(*f.fReturnType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700644 if (!returnType) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400645 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700646 }
ethannicholasd598f792016-07-25 10:08:54 -0700647 std::vector<const Variable*> parameters;
ethannicholasb3058bd2016-07-01 08:22:01 -0700648 for (const auto& param : f.fParameters) {
ethannicholasd598f792016-07-25 10:08:54 -0700649 const Type* type = this->convertType(*param->fType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700650 if (!type) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400651 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700652 }
653 for (int j = (int) param->fSizes.size() - 1; j >= 0; j--) {
654 int size = param->fSizes[j];
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400655 String name = type->name() + "[" + to_string(size) + "]";
ethannicholasd598f792016-07-25 10:08:54 -0700656 Type* newType = new Type(std::move(name), Type::kArray_Kind, *type, size);
657 fSymbolTable->takeOwnership(newType);
658 type = newType;
ethannicholasb3058bd2016-07-01 08:22:01 -0700659 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700660 StringFragment name = param->fName;
661 Variable* var = new Variable(param->fOffset, param->fModifiers, name, *type,
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000662 Variable::kParameter_Storage);
ethannicholasd598f792016-07-25 10:08:54 -0700663 fSymbolTable->takeOwnership(var);
664 parameters.push_back(var);
ethannicholasb3058bd2016-07-01 08:22:01 -0700665 }
666
667 // find existing declaration
ethannicholasd598f792016-07-25 10:08:54 -0700668 const FunctionDeclaration* decl = nullptr;
669 auto entry = (*fSymbolTable)[f.fName];
ethannicholasb3058bd2016-07-01 08:22:01 -0700670 if (entry) {
ethannicholasd598f792016-07-25 10:08:54 -0700671 std::vector<const FunctionDeclaration*> functions;
ethannicholasb3058bd2016-07-01 08:22:01 -0700672 switch (entry->fKind) {
673 case Symbol::kUnresolvedFunction_Kind:
ethannicholasd598f792016-07-25 10:08:54 -0700674 functions = ((UnresolvedFunction*) entry)->fFunctions;
ethannicholasb3058bd2016-07-01 08:22:01 -0700675 break;
676 case Symbol::kFunctionDeclaration_Kind:
ethannicholasd598f792016-07-25 10:08:54 -0700677 functions.push_back((FunctionDeclaration*) entry);
ethannicholasb3058bd2016-07-01 08:22:01 -0700678 break;
679 default:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700680 fErrors.error(f.fOffset, "symbol '" + f.fName + "' was already defined");
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400681 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700682 }
683 for (const auto& other : functions) {
684 ASSERT(other->fName == f.fName);
685 if (parameters.size() == other->fParameters.size()) {
686 bool match = true;
687 for (size_t i = 0; i < parameters.size(); i++) {
688 if (parameters[i]->fType != other->fParameters[i]->fType) {
689 match = false;
690 break;
691 }
692 }
693 if (match) {
ethannicholasd598f792016-07-25 10:08:54 -0700694 if (*returnType != other->fReturnType) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700695 FunctionDeclaration newDecl(f.fOffset, f.fModifiers, f.fName, parameters,
Ethan Nicholascb670962017-04-20 19:31:52 -0400696 *returnType);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700697 fErrors.error(f.fOffset, "functions '" + newDecl.description() +
698 "' and '" + other->description() +
699 "' differ only in return type");
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400700 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700701 }
702 decl = other;
703 for (size_t i = 0; i < parameters.size(); i++) {
704 if (parameters[i]->fModifiers != other->fParameters[i]->fModifiers) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700705 fErrors.error(f.fOffset, "modifiers on parameter " +
706 to_string((uint64_t) i + 1) +
707 " differ between declaration and "
708 "definition");
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400709 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700710 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700711 }
712 if (other->fDefined) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700713 fErrors.error(f.fOffset, "duplicate definition of " +
714 other->description());
ethannicholasb3058bd2016-07-01 08:22:01 -0700715 }
716 break;
717 }
718 }
719 }
720 }
721 if (!decl) {
722 // couldn't find an existing declaration
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700723 auto newDecl = std::unique_ptr<FunctionDeclaration>(new FunctionDeclaration(f.fOffset,
Ethan Nicholascb670962017-04-20 19:31:52 -0400724 f.fModifiers,
ethannicholas471e8942016-10-28 09:02:46 -0700725 f.fName,
726 parameters,
727 *returnType));
728 decl = newDecl.get();
729 fSymbolTable->add(decl->fName, std::move(newDecl));
ethannicholasb3058bd2016-07-01 08:22:01 -0700730 }
ethannicholasd598f792016-07-25 10:08:54 -0700731 if (f.fBody) {
732 ASSERT(!fCurrentFunction);
733 fCurrentFunction = decl;
734 decl->fDefined = true;
735 std::shared_ptr<SymbolTable> old = fSymbolTable;
736 AutoSymbolTable table(this);
737 for (size_t i = 0; i < parameters.size(); i++) {
738 fSymbolTable->addWithoutOwnership(parameters[i]->fName, decl->fParameters[i]);
ethannicholasb3058bd2016-07-01 08:22:01 -0700739 }
Chris Daltonf1b47bb2017-10-06 11:57:51 -0600740 bool needInvocationIDWorkaround = fInvocations != -1 && f.fName == "main" &&
741 fSettings->fCaps &&
742 !fSettings->fCaps->gsInvocationsSupport();
Ethan Nicholas762466e2017-06-29 10:03:38 -0400743 ASSERT(!fExtraVars.size());
ethannicholasd598f792016-07-25 10:08:54 -0700744 std::unique_ptr<Block> body = this->convertBlock(*f.fBody);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400745 for (auto& v : fExtraVars) {
746 body->fStatements.insert(body->fStatements.begin(), std::move(v));
747 }
748 fExtraVars.clear();
ethannicholasd598f792016-07-25 10:08:54 -0700749 fCurrentFunction = nullptr;
750 if (!body) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400751 return;
752 }
753 if (needInvocationIDWorkaround) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500754 body = this->applyInvocationIDWorkaround(std::move(body));
ethannicholasd598f792016-07-25 10:08:54 -0700755 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400756 // conservatively assume all user-defined functions have side effects
757 ((Modifiers&) decl->fModifiers).fFlags |= Modifiers::kHasSideEffects_Flag;
Ethan Nicholase7e81c12017-12-12 09:48:48 -0500758 if (Program::kVertex_Kind == fKind && f.fName == "main" && fRTAdjust) {
759 body->fStatements.insert(body->fStatements.end(), this->getNormalizeSkPositionCode());
760 }
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500761 fProgramElements->push_back(std::unique_ptr<FunctionDefinition>(
762 new FunctionDefinition(f.fOffset, *decl, std::move(body))));
ethannicholasb3058bd2016-07-01 08:22:01 -0700763 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700764}
765
766std::unique_ptr<InterfaceBlock> IRGenerator::convertInterfaceBlock(const ASTInterfaceBlock& intf) {
767 std::shared_ptr<SymbolTable> old = fSymbolTable;
768 AutoSymbolTable table(this);
ethannicholasb3058bd2016-07-01 08:22:01 -0700769 std::vector<Type::Field> fields;
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400770 bool haveRuntimeArray = false;
Ethan Nicholase7e81c12017-12-12 09:48:48 -0500771 bool foundRTAdjust = false;
ethannicholasb3058bd2016-07-01 08:22:01 -0700772 for (size_t i = 0; i < intf.fDeclarations.size(); i++) {
ethannicholas14fe8cc2016-09-07 13:37:16 -0700773 std::unique_ptr<VarDeclarations> decl = this->convertVarDeclarations(
Ethan Nicholas11d53972016-11-28 11:23:23 -0500774 *intf.fDeclarations[i],
ethannicholasb3058bd2016-07-01 08:22:01 -0700775 Variable::kGlobal_Storage);
ethannicholas7effa7a2016-10-14 09:56:33 -0700776 if (!decl) {
777 return nullptr;
778 }
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000779 for (const auto& stmt : decl->fVars) {
780 VarDeclaration& vd = (VarDeclaration&) *stmt;
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400781 if (haveRuntimeArray) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000782 fErrors.error(decl->fOffset,
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400783 "only the last entry in an interface block may be a runtime-sized "
784 "array");
785 }
Ethan Nicholase7e81c12017-12-12 09:48:48 -0500786 if (vd.fVar == fRTAdjust) {
787 foundRTAdjust = true;
788 ASSERT(vd.fVar->fType == *fContext.fFloat4_Type);
789 fRTAdjustFieldIndex = fields.size();
790 }
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000791 fields.push_back(Type::Field(vd.fVar->fModifiers, vd.fVar->fName,
792 &vd.fVar->fType));
793 if (vd.fValue) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700794 fErrors.error(decl->fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -0700795 "initializers are not permitted on interface block fields");
796 }
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000797 if (vd.fVar->fModifiers.fFlags & (Modifiers::kIn_Flag |
798 Modifiers::kOut_Flag |
799 Modifiers::kUniform_Flag |
800 Modifiers::kBuffer_Flag |
801 Modifiers::kConst_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700802 fErrors.error(decl->fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -0700803 "interface block fields may not have storage qualifiers");
804 }
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000805 if (vd.fVar->fType.kind() == Type::kArray_Kind &&
806 vd.fVar->fType.columns() == -1) {
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400807 haveRuntimeArray = true;
808 }
Ethan Nicholas11d53972016-11-28 11:23:23 -0500809 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700810 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700811 Type* type = new Type(intf.fOffset, intf.fTypeName, fields);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500812 old->takeOwnership(type);
Ethan Nicholas50afc172017-02-16 14:49:57 -0500813 std::vector<std::unique_ptr<Expression>> sizes;
814 for (const auto& size : intf.fSizes) {
815 if (size) {
816 std::unique_ptr<Expression> converted = this->convertExpression(*size);
817 if (!converted) {
818 return nullptr;
819 }
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400820 String name = type->fName;
Ethan Nicholas50afc172017-02-16 14:49:57 -0500821 int64_t count;
822 if (converted->fKind == Expression::kIntLiteral_Kind) {
823 count = ((IntLiteral&) *converted).fValue;
824 if (count <= 0) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700825 fErrors.error(converted->fOffset, "array size must be positive");
Ethan Nicholas50afc172017-02-16 14:49:57 -0500826 }
827 name += "[" + to_string(count) + "]";
828 } else {
829 count = -1;
830 name += "[]";
831 }
832 type = new Type(name, Type::kArray_Kind, *type, (int) count);
833 fSymbolTable->takeOwnership((Type*) type);
834 sizes.push_back(std::move(converted));
835 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700836 type = new Type(type->name() + "[]", Type::kArray_Kind, *type, -1);
Ethan Nicholas50afc172017-02-16 14:49:57 -0500837 fSymbolTable->takeOwnership((Type*) type);
838 sizes.push_back(nullptr);
839 }
840 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700841 Variable* var = new Variable(intf.fOffset, intf.fModifiers,
842 intf.fInstanceName.fLength ? intf.fInstanceName : intf.fTypeName,
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000843 *type, Variable::kGlobal_Storage);
Ethan Nicholase7e81c12017-12-12 09:48:48 -0500844 if (foundRTAdjust) {
845 fRTAdjustInterfaceBlock = var;
846 }
Ethan Nicholas86a43402017-01-19 13:32:00 -0500847 old->takeOwnership(var);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700848 if (intf.fInstanceName.fLength) {
Ethan Nicholas50afc172017-02-16 14:49:57 -0500849 old->addWithoutOwnership(intf.fInstanceName, var);
ethannicholasb3058bd2016-07-01 08:22:01 -0700850 } else {
851 for (size_t i = 0; i < fields.size(); i++) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700852 old->add(fields[i].fName, std::unique_ptr<Field>(new Field(intf.fOffset, *var,
ethannicholasd598f792016-07-25 10:08:54 -0700853 (int) i)));
ethannicholasb3058bd2016-07-01 08:22:01 -0700854 }
855 }
Ethan Nicholase7e81c12017-12-12 09:48:48 -0500856 if (var->fName == Compiler::PERVERTEX_NAME) {
857 ASSERT(!fSkPerVertex);
858 fSkPerVertex = var;
859 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700860 return std::unique_ptr<InterfaceBlock>(new InterfaceBlock(intf.fOffset,
Ethan Nicholas8feeff92017-03-30 14:11:58 -0400861 var,
Ethan Nicholas50afc172017-02-16 14:49:57 -0500862 intf.fTypeName,
863 intf.fInstanceName,
864 std::move(sizes),
865 fSymbolTable));
ethannicholasb3058bd2016-07-01 08:22:01 -0700866}
867
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500868void IRGenerator::getConstantInt(const Expression& value, int64_t* out) {
869 switch (value.fKind) {
870 case Expression::kIntLiteral_Kind:
871 *out = ((const IntLiteral&) value).fValue;
872 break;
873 case Expression::kVariableReference_Kind: {
874 const Variable& var = ((VariableReference&) value).fVariable;
875 if ((var.fModifiers.fFlags & Modifiers::kConst_Flag) &&
876 var.fInitialValue) {
877 this->getConstantInt(*var.fInitialValue, out);
878 }
879 break;
880 }
881 default:
882 fErrors.error(value.fOffset, "expected a constant int");
883 }
884}
885
886void IRGenerator::convertEnum(const ASTEnum& e) {
887 std::vector<Variable*> variables;
888 int64_t currentValue = 0;
889 Layout layout;
890 ASTType enumType(e.fOffset, e.fTypeName, ASTType::kIdentifier_Kind, {});
891 const Type* type = this->convertType(enumType);
892 Modifiers modifiers(layout, Modifiers::kConst_Flag);
893 std::shared_ptr<SymbolTable> symbols(new SymbolTable(fSymbolTable, &fErrors));
894 fSymbolTable = symbols;
895 for (size_t i = 0; i < e.fNames.size(); i++) {
896 std::unique_ptr<Expression> value;
897 if (e.fValues[i]) {
898 value = this->convertExpression(*e.fValues[i]);
899 if (!value) {
900 fSymbolTable = symbols->fParent;
901 return;
902 }
903 this->getConstantInt(*value, &currentValue);
904 }
905 value = std::unique_ptr<Expression>(new IntLiteral(fContext, e.fOffset, currentValue));
906 ++currentValue;
907 auto var = std::unique_ptr<Variable>(new Variable(e.fOffset, modifiers, e.fNames[i],
908 *type, Variable::kGlobal_Storage,
909 value.get()));
910 variables.push_back(var.get());
911 symbols->add(e.fNames[i], std::move(var));
912 symbols->takeOwnership(value.release());
913 }
914 fProgramElements->push_back(std::unique_ptr<ProgramElement>(new Enum(e.fOffset, e.fTypeName,
915 symbols)));
916 fSymbolTable = symbols->fParent;
917}
918
ethannicholasd598f792016-07-25 10:08:54 -0700919const Type* IRGenerator::convertType(const ASTType& type) {
920 const Symbol* result = (*fSymbolTable)[type.fName];
ethannicholasb3058bd2016-07-01 08:22:01 -0700921 if (result && result->fKind == Symbol::kType_Kind) {
Ethan Nicholas50afc172017-02-16 14:49:57 -0500922 for (int size : type.fSizes) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700923 String name(result->fName);
924 name += "[";
Ethan Nicholas50afc172017-02-16 14:49:57 -0500925 if (size != -1) {
926 name += to_string(size);
927 }
928 name += "]";
929 result = new Type(name, Type::kArray_Kind, (const Type&) *result, size);
930 fSymbolTable->takeOwnership((Type*) result);
931 }
ethannicholasd598f792016-07-25 10:08:54 -0700932 return (const Type*) result;
ethannicholasb3058bd2016-07-01 08:22:01 -0700933 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700934 fErrors.error(type.fOffset, "unknown type '" + type.fName + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -0700935 return nullptr;
936}
937
938std::unique_ptr<Expression> IRGenerator::convertExpression(const ASTExpression& expr) {
939 switch (expr.fKind) {
940 case ASTExpression::kIdentifier_Kind:
941 return this->convertIdentifier((ASTIdentifier&) expr);
942 case ASTExpression::kBool_Kind:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700943 return std::unique_ptr<Expression>(new BoolLiteral(fContext, expr.fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -0700944 ((ASTBoolLiteral&) expr).fValue));
945 case ASTExpression::kInt_Kind:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700946 return std::unique_ptr<Expression>(new IntLiteral(fContext, expr.fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -0700947 ((ASTIntLiteral&) expr).fValue));
948 case ASTExpression::kFloat_Kind:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700949 return std::unique_ptr<Expression>(new FloatLiteral(fContext, expr.fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -0700950 ((ASTFloatLiteral&) expr).fValue));
951 case ASTExpression::kBinary_Kind:
952 return this->convertBinaryExpression((ASTBinaryExpression&) expr);
953 case ASTExpression::kPrefix_Kind:
954 return this->convertPrefixExpression((ASTPrefixExpression&) expr);
955 case ASTExpression::kSuffix_Kind:
956 return this->convertSuffixExpression((ASTSuffixExpression&) expr);
957 case ASTExpression::kTernary_Kind:
958 return this->convertTernaryExpression((ASTTernaryExpression&) expr);
959 default:
960 ABORT("unsupported expression type: %d\n", expr.fKind);
961 }
962}
963
964std::unique_ptr<Expression> IRGenerator::convertIdentifier(const ASTIdentifier& identifier) {
ethannicholasd598f792016-07-25 10:08:54 -0700965 const Symbol* result = (*fSymbolTable)[identifier.fText];
ethannicholasb3058bd2016-07-01 08:22:01 -0700966 if (!result) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700967 fErrors.error(identifier.fOffset, "unknown identifier '" + identifier.fText + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -0700968 return nullptr;
969 }
970 switch (result->fKind) {
971 case Symbol::kFunctionDeclaration_Kind: {
ethannicholasd598f792016-07-25 10:08:54 -0700972 std::vector<const FunctionDeclaration*> f = {
973 (const FunctionDeclaration*) result
ethannicholasb3058bd2016-07-01 08:22:01 -0700974 };
ethannicholasd598f792016-07-25 10:08:54 -0700975 return std::unique_ptr<FunctionReference>(new FunctionReference(fContext,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700976 identifier.fOffset,
ethannicholasd598f792016-07-25 10:08:54 -0700977 f));
ethannicholasb3058bd2016-07-01 08:22:01 -0700978 }
979 case Symbol::kUnresolvedFunction_Kind: {
ethannicholasd598f792016-07-25 10:08:54 -0700980 const UnresolvedFunction* f = (const UnresolvedFunction*) result;
981 return std::unique_ptr<FunctionReference>(new FunctionReference(fContext,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700982 identifier.fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -0700983 f->fFunctions));
984 }
985 case Symbol::kVariable_Kind: {
Ethan Nicholas38657112017-02-09 17:01:22 -0500986 const Variable* var = (const Variable*) result;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400987#ifndef SKSL_STANDALONE
Ethan Nicholas38657112017-02-09 17:01:22 -0500988 if (var->fModifiers.fLayout.fBuiltin == SK_FRAGCOORD_BUILTIN) {
989 fInputs.fFlipY = true;
990 if (fSettings->fFlipY &&
991 (!fSettings->fCaps ||
992 !fSettings->fCaps->fragCoordConventionsExtensionString())) {
993 fInputs.fRTHeight = true;
994 }
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500995 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400996#endif
Ethan Nicholas86a43402017-01-19 13:32:00 -0500997 // default to kRead_RefKind; this will be corrected later if the variable is written to
998 return std::unique_ptr<VariableReference>(new VariableReference(
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700999 identifier.fOffset,
Ethan Nicholas86a43402017-01-19 13:32:00 -05001000 *var,
1001 VariableReference::kRead_RefKind));
ethannicholasb3058bd2016-07-01 08:22:01 -07001002 }
1003 case Symbol::kField_Kind: {
ethannicholasd598f792016-07-25 10:08:54 -07001004 const Field* field = (const Field*) result;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001005 VariableReference* base = new VariableReference(identifier.fOffset, field->fOwner,
Ethan Nicholas86a43402017-01-19 13:32:00 -05001006 VariableReference::kRead_RefKind);
ethannicholasf789b382016-08-03 12:43:36 -07001007 return std::unique_ptr<Expression>(new FieldAccess(
1008 std::unique_ptr<Expression>(base),
1009 field->fFieldIndex,
1010 FieldAccess::kAnonymousInterfaceBlock_OwnerKind));
ethannicholasb3058bd2016-07-01 08:22:01 -07001011 }
1012 case Symbol::kType_Kind: {
ethannicholasd598f792016-07-25 10:08:54 -07001013 const Type* t = (const Type*) result;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001014 return std::unique_ptr<TypeReference>(new TypeReference(fContext, identifier.fOffset,
ethannicholasd598f792016-07-25 10:08:54 -07001015 *t));
ethannicholasb3058bd2016-07-01 08:22:01 -07001016 }
1017 default:
1018 ABORT("unsupported symbol type %d\n", result->fKind);
1019 }
Ethan Nicholasc0709392017-06-27 11:20:22 -04001020}
1021
Ethan Nicholas762466e2017-06-29 10:03:38 -04001022std::unique_ptr<Section> IRGenerator::convertSection(const ASTSection& s) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001023 return std::unique_ptr<Section>(new Section(s.fOffset, s.fName, s.fArgument, s.fText));
Ethan Nicholas762466e2017-06-29 10:03:38 -04001024}
1025
1026
Ethan Nicholas11d53972016-11-28 11:23:23 -05001027std::unique_ptr<Expression> IRGenerator::coerce(std::unique_ptr<Expression> expr,
ethannicholasd598f792016-07-25 10:08:54 -07001028 const Type& type) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001029 if (!expr) {
1030 return nullptr;
1031 }
ethannicholasd598f792016-07-25 10:08:54 -07001032 if (expr->fType == type) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001033 return expr;
1034 }
1035 this->checkValid(*expr);
ethannicholasd598f792016-07-25 10:08:54 -07001036 if (expr->fType == *fContext.fInvalid_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001037 return nullptr;
1038 }
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001039 if (expr->coercionCost(type) == INT_MAX) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001040 fErrors.error(expr->fOffset, "expected '" + type.description() + "', but found '" +
ethannicholasd598f792016-07-25 10:08:54 -07001041 expr->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001042 return nullptr;
1043 }
ethannicholasd598f792016-07-25 10:08:54 -07001044 if (type.kind() == Type::kScalar_Kind) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001045 std::vector<std::unique_ptr<Expression>> args;
1046 args.push_back(std::move(expr));
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001047 ASTIdentifier id(-1, type.fName);
ethannicholasb3058bd2016-07-01 08:22:01 -07001048 std::unique_ptr<Expression> ctor = this->convertIdentifier(id);
1049 ASSERT(ctor);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001050 return this->call(-1, std::move(ctor), std::move(args));
ethannicholasb3058bd2016-07-01 08:22:01 -07001051 }
ethannicholas5961bc92016-10-12 06:39:56 -07001052 std::vector<std::unique_ptr<Expression>> args;
1053 args.push_back(std::move(expr));
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001054 return std::unique_ptr<Expression>(new Constructor(-1, type, std::move(args)));
ethannicholasb3058bd2016-07-01 08:22:01 -07001055}
1056
ethannicholasf789b382016-08-03 12:43:36 -07001057static bool is_matrix_multiply(const Type& left, const Type& right) {
1058 if (left.kind() == Type::kMatrix_Kind) {
1059 return right.kind() == Type::kMatrix_Kind || right.kind() == Type::kVector_Kind;
1060 }
1061 return left.kind() == Type::kVector_Kind && right.kind() == Type::kMatrix_Kind;
1062}
ethannicholasea4567c2016-10-17 11:24:37 -07001063
ethannicholasb3058bd2016-07-01 08:22:01 -07001064/**
1065 * Determines the operand and result types of a binary expression. Returns true if the expression is
1066 * legal, false otherwise. If false, the values of the out parameters are undefined.
1067 */
Ethan Nicholas11d53972016-11-28 11:23:23 -05001068static bool determine_binary_type(const Context& context,
1069 Token::Kind op,
1070 const Type& left,
1071 const Type& right,
ethannicholasd598f792016-07-25 10:08:54 -07001072 const Type** outLeftType,
1073 const Type** outRightType,
1074 const Type** outResultType,
ethannicholasb3058bd2016-07-01 08:22:01 -07001075 bool tryFlipped) {
1076 bool isLogical;
ethannicholasea4567c2016-10-17 11:24:37 -07001077 bool validMatrixOrVectorOp;
ethannicholasb3058bd2016-07-01 08:22:01 -07001078 switch (op) {
ethannicholasea4567c2016-10-17 11:24:37 -07001079 case Token::EQ:
1080 *outLeftType = &left;
1081 *outRightType = &left;
1082 *outResultType = &left;
1083 return right.canCoerceTo(left);
ethannicholasb3058bd2016-07-01 08:22:01 -07001084 case Token::EQEQ: // fall through
ethannicholasea4567c2016-10-17 11:24:37 -07001085 case Token::NEQ:
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001086 if (left == right) {
1087 *outLeftType = &left;
1088 *outRightType = &right;
1089 *outResultType = context.fBool_Type.get();
1090 return true;
1091 }
ethannicholasea4567c2016-10-17 11:24:37 -07001092 isLogical = true;
1093 validMatrixOrVectorOp = true;
1094 break;
ethannicholasb3058bd2016-07-01 08:22:01 -07001095 case Token::LT: // fall through
1096 case Token::GT: // fall through
1097 case Token::LTEQ: // fall through
1098 case Token::GTEQ:
1099 isLogical = true;
ethannicholasea4567c2016-10-17 11:24:37 -07001100 validMatrixOrVectorOp = false;
ethannicholasb3058bd2016-07-01 08:22:01 -07001101 break;
1102 case Token::LOGICALOR: // fall through
1103 case Token::LOGICALAND: // fall through
1104 case Token::LOGICALXOR: // fall through
1105 case Token::LOGICALOREQ: // fall through
1106 case Token::LOGICALANDEQ: // fall through
1107 case Token::LOGICALXOREQ:
ethannicholasd598f792016-07-25 10:08:54 -07001108 *outLeftType = context.fBool_Type.get();
1109 *outRightType = context.fBool_Type.get();
1110 *outResultType = context.fBool_Type.get();
Ethan Nicholas11d53972016-11-28 11:23:23 -05001111 return left.canCoerceTo(*context.fBool_Type) &&
ethannicholasd598f792016-07-25 10:08:54 -07001112 right.canCoerceTo(*context.fBool_Type);
Ethan Nicholas11d53972016-11-28 11:23:23 -05001113 case Token::STAREQ:
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001114 if (left.kind() == Type::kScalar_Kind) {
1115 *outLeftType = &left;
1116 *outRightType = &left;
1117 *outResultType = &left;
1118 return right.canCoerceTo(left);
1119 }
1120 // fall through
1121 case Token::STAR:
ethannicholasf789b382016-08-03 12:43:36 -07001122 if (is_matrix_multiply(left, right)) {
1123 // determine final component type
1124 if (determine_binary_type(context, Token::STAR, left.componentType(),
1125 right.componentType(), outLeftType, outRightType,
1126 outResultType, false)) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001127 *outLeftType = &(*outResultType)->toCompound(context, left.columns(),
ethannicholasf789b382016-08-03 12:43:36 -07001128 left.rows());;
Ethan Nicholas11d53972016-11-28 11:23:23 -05001129 *outRightType = &(*outResultType)->toCompound(context, right.columns(),
ethannicholasf789b382016-08-03 12:43:36 -07001130 right.rows());;
1131 int leftColumns = left.columns();
1132 int leftRows = left.rows();
1133 int rightColumns;
1134 int rightRows;
1135 if (right.kind() == Type::kVector_Kind) {
1136 // matrix * vector treats the vector as a column vector, so we need to
1137 // transpose it
1138 rightColumns = right.rows();
1139 rightRows = right.columns();
1140 ASSERT(rightColumns == 1);
1141 } else {
1142 rightColumns = right.columns();
1143 rightRows = right.rows();
1144 }
1145 if (rightColumns > 1) {
1146 *outResultType = &(*outResultType)->toCompound(context, rightColumns,
1147 leftRows);
1148 } else {
1149 // result was a column vector, transpose it back to a row
1150 *outResultType = &(*outResultType)->toCompound(context, leftRows,
1151 rightColumns);
1152 }
1153 return leftColumns == rightRows;
1154 } else {
1155 return false;
1156 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001157 }
ethannicholasea4567c2016-10-17 11:24:37 -07001158 isLogical = false;
1159 validMatrixOrVectorOp = true;
1160 break;
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001161 case Token::PLUSEQ:
1162 case Token::MINUSEQ:
1163 case Token::SLASHEQ:
1164 case Token::PERCENTEQ:
1165 case Token::SHLEQ:
1166 case Token::SHREQ:
1167 if (left.kind() == Type::kScalar_Kind) {
1168 *outLeftType = &left;
1169 *outRightType = &left;
1170 *outResultType = &left;
1171 return right.canCoerceTo(left);
1172 }
1173 // fall through
ethannicholasea4567c2016-10-17 11:24:37 -07001174 case Token::PLUS: // fall through
ethannicholasea4567c2016-10-17 11:24:37 -07001175 case Token::MINUS: // fall through
ethannicholasea4567c2016-10-17 11:24:37 -07001176 case Token::SLASH: // fall through
ethannicholasea4567c2016-10-17 11:24:37 -07001177 isLogical = false;
1178 validMatrixOrVectorOp = true;
1179 break;
Ethan Nicholas4b330df2017-05-17 10:52:55 -04001180 case Token::COMMA:
1181 *outLeftType = &left;
1182 *outRightType = &right;
1183 *outResultType = &right;
1184 return true;
ethannicholasb3058bd2016-07-01 08:22:01 -07001185 default:
1186 isLogical = false;
ethannicholasea4567c2016-10-17 11:24:37 -07001187 validMatrixOrVectorOp = false;
ethannicholasb3058bd2016-07-01 08:22:01 -07001188 }
ethannicholasea4567c2016-10-17 11:24:37 -07001189 bool isVectorOrMatrix = left.kind() == Type::kVector_Kind || left.kind() == Type::kMatrix_Kind;
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001190 if (left.kind() == Type::kScalar_Kind && right.kind() == Type::kScalar_Kind &&
1191 right.canCoerceTo(left)) {
1192 if (left.priority() > right.priority()) {
1193 *outLeftType = &left;
1194 *outRightType = &left;
1195 } else {
1196 *outLeftType = &right;
1197 *outRightType = &right;
1198 }
1199 if (isLogical) {
1200 *outResultType = context.fBool_Type.get();
1201 } else {
1202 *outResultType = &left;
1203 }
1204 return true;
1205 }
1206 if (right.canCoerceTo(left) && isVectorOrMatrix && validMatrixOrVectorOp) {
ethannicholasd598f792016-07-25 10:08:54 -07001207 *outLeftType = &left;
1208 *outRightType = &left;
ethannicholasb3058bd2016-07-01 08:22:01 -07001209 if (isLogical) {
ethannicholasd598f792016-07-25 10:08:54 -07001210 *outResultType = context.fBool_Type.get();
ethannicholasb3058bd2016-07-01 08:22:01 -07001211 } else {
ethannicholasd598f792016-07-25 10:08:54 -07001212 *outResultType = &left;
ethannicholasb3058bd2016-07-01 08:22:01 -07001213 }
1214 return true;
1215 }
Ethan Nicholas11d53972016-11-28 11:23:23 -05001216 if ((left.kind() == Type::kVector_Kind || left.kind() == Type::kMatrix_Kind) &&
ethannicholasd598f792016-07-25 10:08:54 -07001217 (right.kind() == Type::kScalar_Kind)) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001218 if (determine_binary_type(context, op, left.componentType(), right, outLeftType,
ethannicholasd598f792016-07-25 10:08:54 -07001219 outRightType, outResultType, false)) {
1220 *outLeftType = &(*outLeftType)->toCompound(context, left.columns(), left.rows());
ethannicholasb3058bd2016-07-01 08:22:01 -07001221 if (!isLogical) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001222 *outResultType = &(*outResultType)->toCompound(context, left.columns(),
ethannicholasd598f792016-07-25 10:08:54 -07001223 left.rows());
ethannicholasb3058bd2016-07-01 08:22:01 -07001224 }
1225 return true;
1226 }
1227 return false;
1228 }
1229 if (tryFlipped) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001230 return determine_binary_type(context, op, right, left, outRightType, outLeftType,
ethannicholasd598f792016-07-25 10:08:54 -07001231 outResultType, false);
ethannicholasb3058bd2016-07-01 08:22:01 -07001232 }
1233 return false;
1234}
1235
ethannicholas08a92112016-11-09 13:26:45 -08001236std::unique_ptr<Expression> IRGenerator::constantFold(const Expression& left,
1237 Token::Kind op,
Ethan Nicholas86a43402017-01-19 13:32:00 -05001238 const Expression& right) const {
Ethan Nicholascb670962017-04-20 19:31:52 -04001239 if (!left.isConstant() || !right.isConstant()) {
1240 return nullptr;
1241 }
ethannicholas08a92112016-11-09 13:26:45 -08001242 // Note that we expressly do not worry about precision and overflow here -- we use the maximum
1243 // precision to calculate the results and hope the result makes sense. The plan is to move the
1244 // Skia caps into SkSL, so we have access to all of them including the precisions of the various
1245 // types, which will let us be more intelligent about this.
Ethan Nicholas11d53972016-11-28 11:23:23 -05001246 if (left.fKind == Expression::kBoolLiteral_Kind &&
ethannicholas08a92112016-11-09 13:26:45 -08001247 right.fKind == Expression::kBoolLiteral_Kind) {
1248 bool leftVal = ((BoolLiteral&) left).fValue;
1249 bool rightVal = ((BoolLiteral&) right).fValue;
1250 bool result;
1251 switch (op) {
1252 case Token::LOGICALAND: result = leftVal && rightVal; break;
1253 case Token::LOGICALOR: result = leftVal || rightVal; break;
1254 case Token::LOGICALXOR: result = leftVal ^ rightVal; break;
1255 default: return nullptr;
1256 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001257 return std::unique_ptr<Expression>(new BoolLiteral(fContext, left.fOffset, result));
ethannicholas08a92112016-11-09 13:26:45 -08001258 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001259 #define RESULT(t, op) std::unique_ptr<Expression>(new t ## Literal(fContext, left.fOffset, \
ethannicholas08a92112016-11-09 13:26:45 -08001260 leftVal op rightVal))
1261 if (left.fKind == Expression::kIntLiteral_Kind && right.fKind == Expression::kIntLiteral_Kind) {
1262 int64_t leftVal = ((IntLiteral&) left).fValue;
1263 int64_t rightVal = ((IntLiteral&) right).fValue;
1264 switch (op) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001265 case Token::PLUS: return RESULT(Int, +);
1266 case Token::MINUS: return RESULT(Int, -);
1267 case Token::STAR: return RESULT(Int, *);
Ethan Nicholas9a5610e2017-01-03 15:16:29 -05001268 case Token::SLASH:
1269 if (rightVal) {
1270 return RESULT(Int, /);
1271 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001272 fErrors.error(right.fOffset, "division by zero");
Ethan Nicholas9a5610e2017-01-03 15:16:29 -05001273 return nullptr;
Ethan Nicholas2503ab62017-01-05 10:44:25 -05001274 case Token::PERCENT:
1275 if (rightVal) {
1276 return RESULT(Int, %);
1277 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001278 fErrors.error(right.fOffset, "division by zero");
Ethan Nicholas2503ab62017-01-05 10:44:25 -05001279 return nullptr;
ethannicholas08a92112016-11-09 13:26:45 -08001280 case Token::BITWISEAND: return RESULT(Int, &);
1281 case Token::BITWISEOR: return RESULT(Int, |);
1282 case Token::BITWISEXOR: return RESULT(Int, ^);
1283 case Token::SHL: return RESULT(Int, <<);
1284 case Token::SHR: return RESULT(Int, >>);
1285 case Token::EQEQ: return RESULT(Bool, ==);
1286 case Token::NEQ: return RESULT(Bool, !=);
1287 case Token::GT: return RESULT(Bool, >);
1288 case Token::GTEQ: return RESULT(Bool, >=);
1289 case Token::LT: return RESULT(Bool, <);
1290 case Token::LTEQ: return RESULT(Bool, <=);
1291 default: return nullptr;
1292 }
1293 }
Ethan Nicholas11d53972016-11-28 11:23:23 -05001294 if (left.fKind == Expression::kFloatLiteral_Kind &&
ethannicholas08a92112016-11-09 13:26:45 -08001295 right.fKind == Expression::kFloatLiteral_Kind) {
1296 double leftVal = ((FloatLiteral&) left).fValue;
1297 double rightVal = ((FloatLiteral&) right).fValue;
1298 switch (op) {
1299 case Token::PLUS: return RESULT(Float, +);
1300 case Token::MINUS: return RESULT(Float, -);
1301 case Token::STAR: return RESULT(Float, *);
Ethan Nicholas9a5610e2017-01-03 15:16:29 -05001302 case Token::SLASH:
1303 if (rightVal) {
1304 return RESULT(Float, /);
1305 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001306 fErrors.error(right.fOffset, "division by zero");
Ethan Nicholas9a5610e2017-01-03 15:16:29 -05001307 return nullptr;
Ethan Nicholascb670962017-04-20 19:31:52 -04001308 case Token::EQEQ: return RESULT(Bool, ==);
1309 case Token::NEQ: return RESULT(Bool, !=);
1310 case Token::GT: return RESULT(Bool, >);
1311 case Token::GTEQ: return RESULT(Bool, >=);
1312 case Token::LT: return RESULT(Bool, <);
1313 case Token::LTEQ: return RESULT(Bool, <=);
ethannicholas08a92112016-11-09 13:26:45 -08001314 default: return nullptr;
1315 }
1316 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001317 if (left.fType.kind() == Type::kVector_Kind &&
1318 left.fType.componentType() == *fContext.fFloat_Type &&
1319 left.fType == right.fType) {
1320 ASSERT(left.fKind == Expression::kConstructor_Kind);
1321 ASSERT(right.fKind == Expression::kConstructor_Kind);
1322 std::vector<std::unique_ptr<Expression>> args;
1323 #define RETURN_VEC_COMPONENTWISE_RESULT(op) \
1324 for (int i = 0; i < left.fType.columns(); i++) { \
1325 float value = ((Constructor&) left).getFVecComponent(i) op \
1326 ((Constructor&) right).getFVecComponent(i); \
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001327 args.emplace_back(new FloatLiteral(fContext, -1, value)); \
Ethan Nicholascb670962017-04-20 19:31:52 -04001328 } \
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001329 return std::unique_ptr<Expression>(new Constructor(-1, left.fType, \
Ethan Nicholascb670962017-04-20 19:31:52 -04001330 std::move(args)));
1331 switch (op) {
Ethan Nicholas3deaeb22017-04-25 14:42:11 -04001332 case Token::EQEQ:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001333 return std::unique_ptr<Expression>(new BoolLiteral(fContext, -1,
Ethan Nicholas3deaeb22017-04-25 14:42:11 -04001334 left.compareConstant(fContext, right)));
1335 case Token::NEQ:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001336 return std::unique_ptr<Expression>(new BoolLiteral(fContext, -1,
Ethan Nicholas3deaeb22017-04-25 14:42:11 -04001337 !left.compareConstant(fContext, right)));
Ethan Nicholascb670962017-04-20 19:31:52 -04001338 case Token::PLUS: RETURN_VEC_COMPONENTWISE_RESULT(+);
1339 case Token::MINUS: RETURN_VEC_COMPONENTWISE_RESULT(-);
1340 case Token::STAR: RETURN_VEC_COMPONENTWISE_RESULT(*);
1341 case Token::SLASH: RETURN_VEC_COMPONENTWISE_RESULT(/);
1342 default: return nullptr;
1343 }
1344 }
Ethan Nicholas3deaeb22017-04-25 14:42:11 -04001345 if (left.fType.kind() == Type::kMatrix_Kind &&
1346 right.fType.kind() == Type::kMatrix_Kind &&
1347 left.fKind == right.fKind) {
1348 switch (op) {
1349 case Token::EQEQ:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001350 return std::unique_ptr<Expression>(new BoolLiteral(fContext, -1,
Ethan Nicholas3deaeb22017-04-25 14:42:11 -04001351 left.compareConstant(fContext, right)));
1352 case Token::NEQ:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001353 return std::unique_ptr<Expression>(new BoolLiteral(fContext, -1,
Ethan Nicholas3deaeb22017-04-25 14:42:11 -04001354 !left.compareConstant(fContext, right)));
1355 default:
1356 return nullptr;
1357 }
1358 }
ethannicholas08a92112016-11-09 13:26:45 -08001359 #undef RESULT
1360 return nullptr;
1361}
1362
ethannicholasb3058bd2016-07-01 08:22:01 -07001363std::unique_ptr<Expression> IRGenerator::convertBinaryExpression(
1364 const ASTBinaryExpression& expression) {
1365 std::unique_ptr<Expression> left = this->convertExpression(*expression.fLeft);
1366 if (!left) {
1367 return nullptr;
1368 }
1369 std::unique_ptr<Expression> right = this->convertExpression(*expression.fRight);
1370 if (!right) {
1371 return nullptr;
1372 }
ethannicholasd598f792016-07-25 10:08:54 -07001373 const Type* leftType;
1374 const Type* rightType;
1375 const Type* resultType;
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001376 const Type* rawLeftType;
1377 if (left->fKind == Expression::kIntLiteral_Kind && right->fType.isInteger()) {
1378 rawLeftType = &right->fType;
1379 } else {
1380 rawLeftType = &left->fType;
1381 }
1382 const Type* rawRightType;
1383 if (right->fKind == Expression::kIntLiteral_Kind && left->fType.isInteger()) {
1384 rawRightType = &left->fType;
1385 } else {
1386 rawRightType = &right->fType;
1387 }
1388 if (!determine_binary_type(fContext, expression.fOperator, *rawLeftType, *rawRightType,
1389 &leftType, &rightType, &resultType,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001390 !Compiler::IsAssignment(expression.fOperator))) {
1391 fErrors.error(expression.fOffset, String("type mismatch: '") +
1392 Compiler::OperatorName(expression.fOperator) +
1393 "' cannot operate on '" + left->fType.fName +
1394 "', '" + right->fType.fName + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001395 return nullptr;
1396 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001397 if (Compiler::IsAssignment(expression.fOperator)) {
Ethan Nicholas86a43402017-01-19 13:32:00 -05001398 this->markWrittenTo(*left, expression.fOperator != Token::EQ);
ethannicholasea4567c2016-10-17 11:24:37 -07001399 }
1400 left = this->coerce(std::move(left), *leftType);
1401 right = this->coerce(std::move(right), *rightType);
1402 if (!left || !right) {
1403 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07001404 }
Ethan Nicholas11d53972016-11-28 11:23:23 -05001405 std::unique_ptr<Expression> result = this->constantFold(*left.get(), expression.fOperator,
ethannicholas08a92112016-11-09 13:26:45 -08001406 *right.get());
1407 if (!result) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001408 result = std::unique_ptr<Expression>(new BinaryExpression(expression.fOffset,
ethannicholas08a92112016-11-09 13:26:45 -08001409 std::move(left),
1410 expression.fOperator,
1411 std::move(right),
1412 *resultType));
1413 }
1414 return result;
ethannicholasb3058bd2016-07-01 08:22:01 -07001415}
1416
Ethan Nicholas11d53972016-11-28 11:23:23 -05001417std::unique_ptr<Expression> IRGenerator::convertTernaryExpression(
ethannicholasb3058bd2016-07-01 08:22:01 -07001418 const ASTTernaryExpression& expression) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001419 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*expression.fTest),
ethannicholasd598f792016-07-25 10:08:54 -07001420 *fContext.fBool_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -07001421 if (!test) {
1422 return nullptr;
1423 }
1424 std::unique_ptr<Expression> ifTrue = this->convertExpression(*expression.fIfTrue);
1425 if (!ifTrue) {
1426 return nullptr;
1427 }
1428 std::unique_ptr<Expression> ifFalse = this->convertExpression(*expression.fIfFalse);
1429 if (!ifFalse) {
1430 return nullptr;
1431 }
ethannicholasd598f792016-07-25 10:08:54 -07001432 const Type* trueType;
1433 const Type* falseType;
1434 const Type* resultType;
1435 if (!determine_binary_type(fContext, Token::EQEQ, ifTrue->fType, ifFalse->fType, &trueType,
Ethan Nicholas2be687a2017-01-03 16:44:39 -05001436 &falseType, &resultType, true) || trueType != falseType) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001437 fErrors.error(expression.fOffset, "ternary operator result mismatch: '" +
1438 ifTrue->fType.fName + "', '" +
1439 ifFalse->fType.fName + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001440 return nullptr;
1441 }
ethannicholasd598f792016-07-25 10:08:54 -07001442 ifTrue = this->coerce(std::move(ifTrue), *trueType);
Ethan Nicholas2be687a2017-01-03 16:44:39 -05001443 if (!ifTrue) {
1444 return nullptr;
1445 }
ethannicholasd598f792016-07-25 10:08:54 -07001446 ifFalse = this->coerce(std::move(ifFalse), *falseType);
Ethan Nicholas2be687a2017-01-03 16:44:39 -05001447 if (!ifFalse) {
1448 return nullptr;
1449 }
ethannicholas08a92112016-11-09 13:26:45 -08001450 if (test->fKind == Expression::kBoolLiteral_Kind) {
1451 // static boolean test, just return one of the branches
1452 if (((BoolLiteral&) *test).fValue) {
1453 return ifTrue;
1454 } else {
1455 return ifFalse;
1456 }
1457 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001458 return std::unique_ptr<Expression>(new TernaryExpression(expression.fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -07001459 std::move(test),
Ethan Nicholas11d53972016-11-28 11:23:23 -05001460 std::move(ifTrue),
ethannicholasb3058bd2016-07-01 08:22:01 -07001461 std::move(ifFalse)));
1462}
1463
Ethan Nicholas5338f992017-04-19 15:54:07 -04001464// scales the texture coordinates by the texture size for sampling rectangle textures.
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001465// For float2coordinates, implements the transformation:
Ethan Nicholas5338f992017-04-19 15:54:07 -04001466// texture(sampler, coord) -> texture(sampler, textureSize(sampler) * coord)
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001467// For float3coordinates, implements the transformation:
1468// texture(sampler, coord) -> texture(sampler, float3textureSize(sampler), 1.0) * coord))
Ethan Nicholas5338f992017-04-19 15:54:07 -04001469void IRGenerator::fixRectSampling(std::vector<std::unique_ptr<Expression>>& arguments) {
1470 ASSERT(arguments.size() == 2);
1471 ASSERT(arguments[0]->fType == *fContext.fSampler2DRect_Type);
1472 ASSERT(arguments[0]->fKind == Expression::kVariableReference_Kind);
1473 const Variable& sampler = ((VariableReference&) *arguments[0]).fVariable;
1474 const Symbol* textureSizeSymbol = (*fSymbolTable)["textureSize"];
1475 ASSERT(textureSizeSymbol->fKind == Symbol::kFunctionDeclaration_Kind);
1476 const FunctionDeclaration& textureSize = (FunctionDeclaration&) *textureSizeSymbol;
1477 std::vector<std::unique_ptr<Expression>> sizeArguments;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001478 sizeArguments.emplace_back(new VariableReference(-1, sampler));
1479 std::unique_ptr<Expression> float2ize = call(-1, textureSize, std::move(sizeArguments));
Ethan Nicholas5338f992017-04-19 15:54:07 -04001480 const Type& type = arguments[1]->fType;
1481 std::unique_ptr<Expression> scale;
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001482 if (type == *fContext.fFloat2_Type) {
1483 scale = std::move(float2ize);
Ethan Nicholas5338f992017-04-19 15:54:07 -04001484 } else {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001485 ASSERT(type == *fContext.fFloat3_Type);
1486 std::vector<std::unique_ptr<Expression>> float3rguments;
1487 float3rguments.push_back(std::move(float2ize));
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001488 float3rguments.emplace_back(new FloatLiteral(fContext, -1, 1.0));
1489 scale.reset(new Constructor(-1, *fContext.fFloat3_Type, std::move(float3rguments)));
Ethan Nicholas5338f992017-04-19 15:54:07 -04001490 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001491 arguments[1].reset(new BinaryExpression(-1, std::move(scale), Token::STAR,
Ethan Nicholas5338f992017-04-19 15:54:07 -04001492 std::move(arguments[1]), type));
1493}
1494
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001495std::unique_ptr<Expression> IRGenerator::call(int offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -05001496 const FunctionDeclaration& function,
ethannicholasd598f792016-07-25 10:08:54 -07001497 std::vector<std::unique_ptr<Expression>> arguments) {
1498 if (function.fParameters.size() != arguments.size()) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001499 String msg = "call to '" + function.fName + "' expected " +
Ethan Nicholas11d53972016-11-28 11:23:23 -05001500 to_string((uint64_t) function.fParameters.size()) +
ethannicholasb3058bd2016-07-01 08:22:01 -07001501 " argument";
ethannicholasd598f792016-07-25 10:08:54 -07001502 if (function.fParameters.size() != 1) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001503 msg += "s";
1504 }
ethannicholas5961bc92016-10-12 06:39:56 -07001505 msg += ", but found " + to_string((uint64_t) arguments.size());
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001506 fErrors.error(offset, msg);
ethannicholasb3058bd2016-07-01 08:22:01 -07001507 return nullptr;
1508 }
ethannicholas471e8942016-10-28 09:02:46 -07001509 std::vector<const Type*> types;
1510 const Type* returnType;
1511 if (!function.determineFinalTypes(arguments, &types, &returnType)) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001512 String msg = "no match for " + function.fName + "(";
1513 String separator;
ethannicholas471e8942016-10-28 09:02:46 -07001514 for (size_t i = 0; i < arguments.size(); i++) {
1515 msg += separator;
1516 separator = ", ";
1517 msg += arguments[i]->fType.description();
1518 }
1519 msg += ")";
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001520 fErrors.error(offset, msg);
ethannicholas471e8942016-10-28 09:02:46 -07001521 return nullptr;
1522 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001523 for (size_t i = 0; i < arguments.size(); i++) {
ethannicholas471e8942016-10-28 09:02:46 -07001524 arguments[i] = this->coerce(std::move(arguments[i]), *types[i]);
ethannicholasea4567c2016-10-17 11:24:37 -07001525 if (!arguments[i]) {
1526 return nullptr;
1527 }
ethannicholasd598f792016-07-25 10:08:54 -07001528 if (arguments[i] && (function.fParameters[i]->fModifiers.fFlags & Modifiers::kOut_Flag)) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001529 this->markWrittenTo(*arguments[i],
Ethan Nicholascb670962017-04-20 19:31:52 -04001530 function.fParameters[i]->fModifiers.fFlags & Modifiers::kIn_Flag);
ethannicholasb3058bd2016-07-01 08:22:01 -07001531 }
1532 }
Ethan Nicholas5338f992017-04-19 15:54:07 -04001533 if (function.fBuiltin && function.fName == "texture" &&
1534 arguments[0]->fType == *fContext.fSampler2DRect_Type) {
1535 this->fixRectSampling(arguments);
1536 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001537 return std::unique_ptr<FunctionCall>(new FunctionCall(offset, *returnType, function,
ethannicholasb3058bd2016-07-01 08:22:01 -07001538 std::move(arguments)));
1539}
1540
1541/**
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001542 * Determines the cost of coercing the arguments of a function to the required types. Cost has no
1543 * particular meaning other than "lower costs are preferred". Returns INT_MAX if the call is not
1544 * valid.
ethannicholasb3058bd2016-07-01 08:22:01 -07001545 */
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001546int IRGenerator::callCost(const FunctionDeclaration& function,
1547 const std::vector<std::unique_ptr<Expression>>& arguments) {
ethannicholasd598f792016-07-25 10:08:54 -07001548 if (function.fParameters.size() != arguments.size()) {
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001549 return INT_MAX;
ethannicholasb3058bd2016-07-01 08:22:01 -07001550 }
1551 int total = 0;
ethannicholas471e8942016-10-28 09:02:46 -07001552 std::vector<const Type*> types;
1553 const Type* ignored;
1554 if (!function.determineFinalTypes(arguments, &types, &ignored)) {
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001555 return INT_MAX;
ethannicholas471e8942016-10-28 09:02:46 -07001556 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001557 for (size_t i = 0; i < arguments.size(); i++) {
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001558 int cost = arguments[i]->coercionCost(*types[i]);
1559 if (cost != INT_MAX) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001560 total += cost;
1561 } else {
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001562 return INT_MAX;
ethannicholasb3058bd2016-07-01 08:22:01 -07001563 }
1564 }
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001565 return total;
ethannicholasb3058bd2016-07-01 08:22:01 -07001566}
1567
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001568std::unique_ptr<Expression> IRGenerator::call(int offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -05001569 std::unique_ptr<Expression> functionValue,
ethannicholasb3058bd2016-07-01 08:22:01 -07001570 std::vector<std::unique_ptr<Expression>> arguments) {
1571 if (functionValue->fKind == Expression::kTypeReference_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001572 return this->convertConstructor(offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -05001573 ((TypeReference&) *functionValue).fValue,
ethannicholasb3058bd2016-07-01 08:22:01 -07001574 std::move(arguments));
1575 }
1576 if (functionValue->fKind != Expression::kFunctionReference_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001577 fErrors.error(offset, "'" + functionValue->description() + "' is not a function");
ethannicholasb3058bd2016-07-01 08:22:01 -07001578 return nullptr;
1579 }
1580 FunctionReference* ref = (FunctionReference*) functionValue.get();
1581 int bestCost = INT_MAX;
ethannicholasd598f792016-07-25 10:08:54 -07001582 const FunctionDeclaration* best = nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07001583 if (ref->fFunctions.size() > 1) {
1584 for (const auto& f : ref->fFunctions) {
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001585 int cost = this->callCost(*f, arguments);
1586 if (cost < bestCost) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001587 bestCost = cost;
1588 best = f;
1589 }
1590 }
1591 if (best) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001592 return this->call(offset, *best, std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07001593 }
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001594 String msg = "no match for " + ref->fFunctions[0]->fName + "(";
1595 String separator;
ethannicholasb3058bd2016-07-01 08:22:01 -07001596 for (size_t i = 0; i < arguments.size(); i++) {
1597 msg += separator;
1598 separator = ", ";
ethannicholasd598f792016-07-25 10:08:54 -07001599 msg += arguments[i]->fType.description();
ethannicholasb3058bd2016-07-01 08:22:01 -07001600 }
1601 msg += ")";
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001602 fErrors.error(offset, msg);
ethannicholasb3058bd2016-07-01 08:22:01 -07001603 return nullptr;
1604 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001605 return this->call(offset, *ref->fFunctions[0], std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07001606}
1607
Ethan Nicholas84645e32017-02-09 13:57:14 -05001608std::unique_ptr<Expression> IRGenerator::convertNumberConstructor(
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001609 int offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -05001610 const Type& type,
ethannicholasb3058bd2016-07-01 08:22:01 -07001611 std::vector<std::unique_ptr<Expression>> args) {
Ethan Nicholas84645e32017-02-09 13:57:14 -05001612 ASSERT(type.isNumber());
1613 if (args.size() != 1) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001614 fErrors.error(offset, "invalid arguments to '" + type.description() +
1615 "' constructor, (expected exactly 1 argument, but found " +
1616 to_string((uint64_t) args.size()) + ")");
ethannicholasb3058bd2016-07-01 08:22:01 -07001617 return nullptr;
1618 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001619 if (type == args[0]->fType) {
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001620 return std::move(args[0]);
1621 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001622 if (type.isFloat() && args.size() == 1 && args[0]->fKind == Expression::kFloatLiteral_Kind) {
1623 double value = ((FloatLiteral&) *args[0]).fValue;
1624 return std::unique_ptr<Expression>(new FloatLiteral(fContext, offset, value, &type));
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001625 }
1626 if (type.isFloat() && args.size() == 1 && args[0]->fKind == Expression::kIntLiteral_Kind) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001627 int64_t value = ((IntLiteral&) *args[0]).fValue;
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001628 return std::unique_ptr<Expression>(new FloatLiteral(fContext, offset, (double) value,
1629 &type));
ethannicholasb3058bd2016-07-01 08:22:01 -07001630 }
Ethan Nicholas84645e32017-02-09 13:57:14 -05001631 if (args[0]->fKind == Expression::kIntLiteral_Kind && (type == *fContext.fInt_Type ||
1632 type == *fContext.fUInt_Type)) {
1633 return std::unique_ptr<Expression>(new IntLiteral(fContext,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001634 offset,
Ethan Nicholas84645e32017-02-09 13:57:14 -05001635 ((IntLiteral&) *args[0]).fValue,
1636 &type));
ethannicholasb3058bd2016-07-01 08:22:01 -07001637 }
Ethan Nicholas84645e32017-02-09 13:57:14 -05001638 if (args[0]->fType == *fContext.fBool_Type) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001639 std::unique_ptr<IntLiteral> zero(new IntLiteral(fContext, offset, 0));
1640 std::unique_ptr<IntLiteral> one(new IntLiteral(fContext, offset, 1));
Ethan Nicholas84645e32017-02-09 13:57:14 -05001641 return std::unique_ptr<Expression>(
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001642 new TernaryExpression(offset, std::move(args[0]),
Ethan Nicholas84645e32017-02-09 13:57:14 -05001643 this->coerce(std::move(one), type),
1644 this->coerce(std::move(zero),
1645 type)));
1646 }
1647 if (!args[0]->fType.isNumber()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001648 fErrors.error(offset, "invalid argument to '" + type.description() +
1649 "' constructor (expected a number or bool, but found '" +
1650 args[0]->fType.description() + "')");
Ethan Nicholas84645e32017-02-09 13:57:14 -05001651 return nullptr;
1652 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001653 return std::unique_ptr<Expression>(new Constructor(offset, type, std::move(args)));
Ethan Nicholas84645e32017-02-09 13:57:14 -05001654}
1655
1656int component_count(const Type& type) {
1657 switch (type.kind()) {
1658 case Type::kVector_Kind:
1659 return type.columns();
1660 case Type::kMatrix_Kind:
1661 return type.columns() * type.rows();
1662 default:
1663 return 1;
1664 }
1665}
1666
1667std::unique_ptr<Expression> IRGenerator::convertCompoundConstructor(
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001668 int offset,
Ethan Nicholas84645e32017-02-09 13:57:14 -05001669 const Type& type,
1670 std::vector<std::unique_ptr<Expression>> args) {
1671 ASSERT(type.kind() == Type::kVector_Kind || type.kind() == Type::kMatrix_Kind);
1672 if (type.kind() == Type::kMatrix_Kind && args.size() == 1 &&
1673 args[0]->fType.kind() == Type::kMatrix_Kind) {
1674 // matrix from matrix is always legal
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001675 return std::unique_ptr<Expression>(new Constructor(offset, type, std::move(args)));
Ethan Nicholas84645e32017-02-09 13:57:14 -05001676 }
1677 int actual = 0;
1678 int expected = type.rows() * type.columns();
1679 if (args.size() != 1 || expected != component_count(args[0]->fType) ||
1680 type.componentType().isNumber() != args[0]->fType.componentType().isNumber()) {
ethannicholas5961bc92016-10-12 06:39:56 -07001681 for (size_t i = 0; i < args.size(); i++) {
Ethan Nicholas84645e32017-02-09 13:57:14 -05001682 if (args[i]->fType.kind() == Type::kVector_Kind) {
Ethan Nicholas49a36ba2017-02-09 17:04:23 +00001683 if (type.componentType().isNumber() !=
1684 args[i]->fType.componentType().isNumber()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001685 fErrors.error(offset, "'" + args[i]->fType.description() + "' is not a valid "
1686 "parameter to '" + type.description() +
1687 "' constructor");
Ethan Nicholas49a36ba2017-02-09 17:04:23 +00001688 return nullptr;
1689 }
Ethan Nicholas84645e32017-02-09 13:57:14 -05001690 actual += args[i]->fType.columns();
Ethan Nicholas49a36ba2017-02-09 17:04:23 +00001691 } else if (args[i]->fType.kind() == Type::kScalar_Kind) {
1692 actual += 1;
1693 if (type.kind() != Type::kScalar_Kind) {
1694 args[i] = this->coerce(std::move(args[i]), type.componentType());
1695 if (!args[i]) {
1696 return nullptr;
1697 }
1698 }
1699 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001700 fErrors.error(offset, "'" + args[i]->fType.description() + "' is not a valid "
1701 "parameter to '" + type.description() + "' constructor");
Ethan Nicholas49a36ba2017-02-09 17:04:23 +00001702 return nullptr;
1703 }
1704 }
Ethan Nicholas84645e32017-02-09 13:57:14 -05001705 if (actual != 1 && actual != expected) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001706 fErrors.error(offset, "invalid arguments to '" + type.description() +
1707 "' constructor (expected " + to_string(expected) +
1708 " scalars, but found " + to_string(actual) + ")");
Ethan Nicholas49a36ba2017-02-09 17:04:23 +00001709 return nullptr;
1710 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001711 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001712 return std::unique_ptr<Expression>(new Constructor(offset, type, std::move(args)));
ethannicholasb3058bd2016-07-01 08:22:01 -07001713}
1714
Ethan Nicholas84645e32017-02-09 13:57:14 -05001715std::unique_ptr<Expression> IRGenerator::convertConstructor(
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001716 int offset,
Ethan Nicholas84645e32017-02-09 13:57:14 -05001717 const Type& type,
1718 std::vector<std::unique_ptr<Expression>> args) {
1719 // FIXME: add support for structs
1720 Type::Kind kind = type.kind();
1721 if (args.size() == 1 && args[0]->fType == type) {
1722 // argument is already the right type, just return it
1723 return std::move(args[0]);
1724 }
1725 if (type.isNumber()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001726 return this->convertNumberConstructor(offset, type, std::move(args));
Ethan Nicholas84645e32017-02-09 13:57:14 -05001727 } else if (kind == Type::kArray_Kind) {
1728 const Type& base = type.componentType();
1729 for (size_t i = 0; i < args.size(); i++) {
1730 args[i] = this->coerce(std::move(args[i]), base);
1731 if (!args[i]) {
1732 return nullptr;
1733 }
1734 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001735 return std::unique_ptr<Expression>(new Constructor(offset, type, std::move(args)));
Ethan Nicholas84645e32017-02-09 13:57:14 -05001736 } else if (kind == Type::kVector_Kind || kind == Type::kMatrix_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001737 return this->convertCompoundConstructor(offset, type, std::move(args));
Ethan Nicholas84645e32017-02-09 13:57:14 -05001738 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001739 fErrors.error(offset, "cannot construct '" + type.description() + "'");
Ethan Nicholas84645e32017-02-09 13:57:14 -05001740 return nullptr;
1741 }
1742}
1743
ethannicholasb3058bd2016-07-01 08:22:01 -07001744std::unique_ptr<Expression> IRGenerator::convertPrefixExpression(
1745 const ASTPrefixExpression& expression) {
1746 std::unique_ptr<Expression> base = this->convertExpression(*expression.fOperand);
1747 if (!base) {
1748 return nullptr;
1749 }
1750 switch (expression.fOperator) {
1751 case Token::PLUS:
ethannicholasd598f792016-07-25 10:08:54 -07001752 if (!base->fType.isNumber() && base->fType.kind() != Type::kVector_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001753 fErrors.error(expression.fOffset,
ethannicholasd598f792016-07-25 10:08:54 -07001754 "'+' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001755 return nullptr;
1756 }
1757 return base;
1758 case Token::MINUS:
ethannicholasd598f792016-07-25 10:08:54 -07001759 if (!base->fType.isNumber() && base->fType.kind() != Type::kVector_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001760 fErrors.error(expression.fOffset,
ethannicholasd598f792016-07-25 10:08:54 -07001761 "'-' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001762 return nullptr;
1763 }
1764 if (base->fKind == Expression::kIntLiteral_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001765 return std::unique_ptr<Expression>(new IntLiteral(fContext, base->fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -07001766 -((IntLiteral&) *base).fValue));
1767 }
1768 if (base->fKind == Expression::kFloatLiteral_Kind) {
1769 double value = -((FloatLiteral&) *base).fValue;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001770 return std::unique_ptr<Expression>(new FloatLiteral(fContext, base->fOffset,
ethannicholasd598f792016-07-25 10:08:54 -07001771 value));
ethannicholasb3058bd2016-07-01 08:22:01 -07001772 }
1773 return std::unique_ptr<Expression>(new PrefixExpression(Token::MINUS, std::move(base)));
1774 case Token::PLUSPLUS:
ethannicholasd598f792016-07-25 10:08:54 -07001775 if (!base->fType.isNumber()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001776 fErrors.error(expression.fOffset,
1777 String("'") + Compiler::OperatorName(expression.fOperator) +
ethannicholasd598f792016-07-25 10:08:54 -07001778 "' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001779 return nullptr;
1780 }
Ethan Nicholas86a43402017-01-19 13:32:00 -05001781 this->markWrittenTo(*base, true);
ethannicholasb3058bd2016-07-01 08:22:01 -07001782 break;
Ethan Nicholas11d53972016-11-28 11:23:23 -05001783 case Token::MINUSMINUS:
ethannicholasd598f792016-07-25 10:08:54 -07001784 if (!base->fType.isNumber()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001785 fErrors.error(expression.fOffset,
1786 String("'") + Compiler::OperatorName(expression.fOperator) +
ethannicholasd598f792016-07-25 10:08:54 -07001787 "' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001788 return nullptr;
1789 }
Ethan Nicholas86a43402017-01-19 13:32:00 -05001790 this->markWrittenTo(*base, true);
ethannicholasb3058bd2016-07-01 08:22:01 -07001791 break;
ethannicholas5961bc92016-10-12 06:39:56 -07001792 case Token::LOGICALNOT:
ethannicholasd598f792016-07-25 10:08:54 -07001793 if (base->fType != *fContext.fBool_Type) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001794 fErrors.error(expression.fOffset,
1795 String("'") + Compiler::OperatorName(expression.fOperator) +
ethannicholasd598f792016-07-25 10:08:54 -07001796 "' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001797 return nullptr;
1798 }
ethannicholas08a92112016-11-09 13:26:45 -08001799 if (base->fKind == Expression::kBoolLiteral_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001800 return std::unique_ptr<Expression>(new BoolLiteral(fContext, base->fOffset,
ethannicholas08a92112016-11-09 13:26:45 -08001801 !((BoolLiteral&) *base).fValue));
1802 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001803 break;
ethannicholas5961bc92016-10-12 06:39:56 -07001804 case Token::BITWISENOT:
1805 if (base->fType != *fContext.fInt_Type) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001806 fErrors.error(expression.fOffset,
1807 String("'") + Compiler::OperatorName(expression.fOperator) +
ethannicholas5961bc92016-10-12 06:39:56 -07001808 "' cannot operate on '" + base->fType.description() + "'");
1809 return nullptr;
1810 }
1811 break;
Ethan Nicholas11d53972016-11-28 11:23:23 -05001812 default:
ethannicholasb3058bd2016-07-01 08:22:01 -07001813 ABORT("unsupported prefix operator\n");
1814 }
Ethan Nicholas11d53972016-11-28 11:23:23 -05001815 return std::unique_ptr<Expression>(new PrefixExpression(expression.fOperator,
ethannicholasb3058bd2016-07-01 08:22:01 -07001816 std::move(base)));
1817}
1818
1819std::unique_ptr<Expression> IRGenerator::convertIndex(std::unique_ptr<Expression> base,
1820 const ASTExpression& index) {
Ethan Nicholas50afc172017-02-16 14:49:57 -05001821 if (base->fKind == Expression::kTypeReference_Kind) {
1822 if (index.fKind == ASTExpression::kInt_Kind) {
1823 const Type& oldType = ((TypeReference&) *base).fValue;
1824 int64_t size = ((const ASTIntLiteral&) index).fValue;
1825 Type* newType = new Type(oldType.name() + "[" + to_string(size) + "]",
1826 Type::kArray_Kind, oldType, size);
1827 fSymbolTable->takeOwnership(newType);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001828 return std::unique_ptr<Expression>(new TypeReference(fContext, base->fOffset,
Ethan Nicholas50afc172017-02-16 14:49:57 -05001829 *newType));
1830
1831 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001832 fErrors.error(base->fOffset, "array size must be a constant");
Ethan Nicholas50afc172017-02-16 14:49:57 -05001833 return nullptr;
1834 }
1835 }
ethannicholas5961bc92016-10-12 06:39:56 -07001836 if (base->fType.kind() != Type::kArray_Kind && base->fType.kind() != Type::kMatrix_Kind &&
1837 base->fType.kind() != Type::kVector_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001838 fErrors.error(base->fOffset, "expected array, but found '" + base->fType.description() +
1839 "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001840 return nullptr;
1841 }
1842 std::unique_ptr<Expression> converted = this->convertExpression(index);
1843 if (!converted) {
1844 return nullptr;
1845 }
ethannicholas5961bc92016-10-12 06:39:56 -07001846 if (converted->fType != *fContext.fUInt_Type) {
1847 converted = this->coerce(std::move(converted), *fContext.fInt_Type);
1848 if (!converted) {
1849 return nullptr;
1850 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001851 }
Ethan Nicholas11d53972016-11-28 11:23:23 -05001852 return std::unique_ptr<Expression>(new IndexExpression(fContext, std::move(base),
ethannicholasd598f792016-07-25 10:08:54 -07001853 std::move(converted)));
ethannicholasb3058bd2016-07-01 08:22:01 -07001854}
1855
1856std::unique_ptr<Expression> IRGenerator::convertField(std::unique_ptr<Expression> base,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001857 StringFragment field) {
ethannicholasd598f792016-07-25 10:08:54 -07001858 auto fields = base->fType.fields();
ethannicholasb3058bd2016-07-01 08:22:01 -07001859 for (size_t i = 0; i < fields.size(); i++) {
1860 if (fields[i].fName == field) {
1861 return std::unique_ptr<Expression>(new FieldAccess(std::move(base), (int) i));
1862 }
1863 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001864 fErrors.error(base->fOffset, "type '" + base->fType.description() + "' does not have a "
1865 "field named '" + field + "");
ethannicholasb3058bd2016-07-01 08:22:01 -07001866 return nullptr;
1867}
1868
1869std::unique_ptr<Expression> IRGenerator::convertSwizzle(std::unique_ptr<Expression> base,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001870 StringFragment fields) {
ethannicholasd598f792016-07-25 10:08:54 -07001871 if (base->fType.kind() != Type::kVector_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001872 fErrors.error(base->fOffset, "cannot swizzle type '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001873 return nullptr;
1874 }
1875 std::vector<int> swizzleComponents;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001876 for (size_t i = 0; i < fields.fLength; i++) {
Ethan Nicholas9e1138d2016-11-21 10:39:35 -05001877 switch (fields[i]) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001878 case 'x': // fall through
1879 case 'r': // fall through
Ethan Nicholas11d53972016-11-28 11:23:23 -05001880 case 's':
ethannicholasb3058bd2016-07-01 08:22:01 -07001881 swizzleComponents.push_back(0);
1882 break;
1883 case 'y': // fall through
1884 case 'g': // fall through
1885 case 't':
ethannicholasd598f792016-07-25 10:08:54 -07001886 if (base->fType.columns() >= 2) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001887 swizzleComponents.push_back(1);
1888 break;
1889 }
1890 // fall through
1891 case 'z': // fall through
1892 case 'b': // fall through
Ethan Nicholas11d53972016-11-28 11:23:23 -05001893 case 'p':
ethannicholasd598f792016-07-25 10:08:54 -07001894 if (base->fType.columns() >= 3) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001895 swizzleComponents.push_back(2);
1896 break;
1897 }
1898 // fall through
1899 case 'w': // fall through
1900 case 'a': // fall through
1901 case 'q':
ethannicholasd598f792016-07-25 10:08:54 -07001902 if (base->fType.columns() >= 4) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001903 swizzleComponents.push_back(3);
1904 break;
1905 }
1906 // fall through
1907 default:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001908 fErrors.error(base->fOffset, String::printf("invalid swizzle component '%c'",
1909 fields[i]));
ethannicholasb3058bd2016-07-01 08:22:01 -07001910 return nullptr;
1911 }
1912 }
1913 ASSERT(swizzleComponents.size() > 0);
1914 if (swizzleComponents.size() > 4) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001915 fErrors.error(base->fOffset, "too many components in swizzle mask '" + fields + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001916 return nullptr;
1917 }
ethannicholasd598f792016-07-25 10:08:54 -07001918 return std::unique_ptr<Expression>(new Swizzle(fContext, std::move(base), swizzleComponents));
ethannicholasb3058bd2016-07-01 08:22:01 -07001919}
1920
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001921std::unique_ptr<Expression> IRGenerator::getCap(int offset, String name) {
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001922 auto found = fCapsMap.find(name);
1923 if (found == fCapsMap.end()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001924 fErrors.error(offset, "unknown capability flag '" + name + "'");
Ethan Nicholas3605ace2016-11-21 15:59:48 -05001925 return nullptr;
1926 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001927 String fullName = "sk_Caps." + name;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001928 return std::unique_ptr<Expression>(new Setting(offset, fullName,
1929 found->second.literal(fContext, offset)));
Ethan Nicholas762466e2017-06-29 10:03:38 -04001930}
1931
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001932std::unique_ptr<Expression> IRGenerator::getArg(int offset, String name) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001933 auto found = fSettings->fArgs.find(name);
1934 if (found == fSettings->fArgs.end()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001935 fErrors.error(offset, "unknown argument '" + name + "'");
Ethan Nicholas762466e2017-06-29 10:03:38 -04001936 return nullptr;
Ethan Nicholas3605ace2016-11-21 15:59:48 -05001937 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001938 String fullName = "sk_Args." + name;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001939 return std::unique_ptr<Expression>(new Setting(offset,
Ethan Nicholas762466e2017-06-29 10:03:38 -04001940 fullName,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001941 found->second.literal(fContext, offset)));
Ethan Nicholas3605ace2016-11-21 15:59:48 -05001942}
1943
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001944std::unique_ptr<Expression> IRGenerator::convertTypeField(int offset, const Type& type,
1945 StringFragment field) {
1946 std::unique_ptr<Expression> result;
1947 for (const auto& e : *fProgramElements) {
1948 if (e->fKind == ProgramElement::kEnum_Kind && type.name() == ((Enum&) *e).fTypeName) {
1949 std::shared_ptr<SymbolTable> old = fSymbolTable;
1950 fSymbolTable = ((Enum&) *e).fSymbols;
1951 result = convertIdentifier(ASTIdentifier(offset, field));
1952 fSymbolTable = old;
1953 }
1954 }
1955 if (!result) {
1956 fErrors.error(offset, "type '" + type.fName + "' does not have a field named '" + field +
1957 "'");
1958 }
1959 return result;
1960}
1961
ethannicholasb3058bd2016-07-01 08:22:01 -07001962std::unique_ptr<Expression> IRGenerator::convertSuffixExpression(
1963 const ASTSuffixExpression& expression) {
1964 std::unique_ptr<Expression> base = this->convertExpression(*expression.fBase);
1965 if (!base) {
1966 return nullptr;
1967 }
1968 switch (expression.fSuffix->fKind) {
ethannicholas5961bc92016-10-12 06:39:56 -07001969 case ASTSuffix::kIndex_Kind: {
1970 const ASTExpression* expr = ((ASTIndexSuffix&) *expression.fSuffix).fExpression.get();
1971 if (expr) {
1972 return this->convertIndex(std::move(base), *expr);
1973 } else if (base->fKind == Expression::kTypeReference_Kind) {
1974 const Type& oldType = ((TypeReference&) *base).fValue;
Ethan Nicholas11d53972016-11-28 11:23:23 -05001975 Type* newType = new Type(oldType.name() + "[]", Type::kArray_Kind, oldType,
ethannicholas5961bc92016-10-12 06:39:56 -07001976 -1);
1977 fSymbolTable->takeOwnership(newType);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001978 return std::unique_ptr<Expression>(new TypeReference(fContext, base->fOffset,
ethannicholas5961bc92016-10-12 06:39:56 -07001979 *newType));
1980 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001981 fErrors.error(expression.fOffset, "'[]' must follow a type name");
ethannicholasa54401d2016-10-14 08:37:32 -07001982 return nullptr;
ethannicholas5961bc92016-10-12 06:39:56 -07001983 }
1984 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001985 case ASTSuffix::kCall_Kind: {
1986 auto rawArguments = &((ASTCallSuffix&) *expression.fSuffix).fArguments;
1987 std::vector<std::unique_ptr<Expression>> arguments;
1988 for (size_t i = 0; i < rawArguments->size(); i++) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001989 std::unique_ptr<Expression> converted =
ethannicholasb3058bd2016-07-01 08:22:01 -07001990 this->convertExpression(*(*rawArguments)[i]);
1991 if (!converted) {
1992 return nullptr;
1993 }
1994 arguments.push_back(std::move(converted));
1995 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001996 return this->call(expression.fOffset, std::move(base), std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07001997 }
1998 case ASTSuffix::kField_Kind: {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001999 StringFragment field = ((ASTFieldSuffix&) *expression.fSuffix).fField;
Ethan Nicholas3605ace2016-11-21 15:59:48 -05002000 if (base->fType == *fContext.fSkCaps_Type) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002001 return this->getCap(expression.fOffset, field);
Ethan Nicholas3605ace2016-11-21 15:59:48 -05002002 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04002003 if (base->fType == *fContext.fSkArgs_Type) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002004 return this->getArg(expression.fOffset, field);
2005 }
2006 if (base->fKind == Expression::kTypeReference_Kind) {
2007 return this->convertTypeField(base->fOffset, ((TypeReference&) *base).fValue,
2008 field);
Ethan Nicholas762466e2017-06-29 10:03:38 -04002009 }
ethannicholasd598f792016-07-25 10:08:54 -07002010 switch (base->fType.kind()) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002011 case Type::kVector_Kind:
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002012 return this->convertSwizzle(std::move(base), field);
ethannicholasb3058bd2016-07-01 08:22:01 -07002013 case Type::kStruct_Kind:
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002014 return this->convertField(std::move(base), field);
ethannicholasb3058bd2016-07-01 08:22:01 -07002015 default:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002016 fErrors.error(base->fOffset, "cannot swizzle value of type '" +
2017 base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07002018 return nullptr;
2019 }
2020 }
2021 case ASTSuffix::kPostIncrement_Kind:
ethannicholasd598f792016-07-25 10:08:54 -07002022 if (!base->fType.isNumber()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002023 fErrors.error(expression.fOffset,
ethannicholasd598f792016-07-25 10:08:54 -07002024 "'++' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07002025 return nullptr;
2026 }
Ethan Nicholas86a43402017-01-19 13:32:00 -05002027 this->markWrittenTo(*base, true);
Ethan Nicholas11d53972016-11-28 11:23:23 -05002028 return std::unique_ptr<Expression>(new PostfixExpression(std::move(base),
ethannicholasb3058bd2016-07-01 08:22:01 -07002029 Token::PLUSPLUS));
2030 case ASTSuffix::kPostDecrement_Kind:
ethannicholasd598f792016-07-25 10:08:54 -07002031 if (!base->fType.isNumber()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002032 fErrors.error(expression.fOffset,
ethannicholasd598f792016-07-25 10:08:54 -07002033 "'--' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07002034 return nullptr;
2035 }
Ethan Nicholas86a43402017-01-19 13:32:00 -05002036 this->markWrittenTo(*base, true);
Ethan Nicholas11d53972016-11-28 11:23:23 -05002037 return std::unique_ptr<Expression>(new PostfixExpression(std::move(base),
ethannicholasb3058bd2016-07-01 08:22:01 -07002038 Token::MINUSMINUS));
2039 default:
2040 ABORT("unsupported suffix operator");
2041 }
2042}
2043
2044void IRGenerator::checkValid(const Expression& expr) {
2045 switch (expr.fKind) {
2046 case Expression::kFunctionReference_Kind:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002047 fErrors.error(expr.fOffset, "expected '(' to begin function call");
ethannicholasb3058bd2016-07-01 08:22:01 -07002048 break;
2049 case Expression::kTypeReference_Kind:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002050 fErrors.error(expr.fOffset, "expected '(' to begin constructor invocation");
ethannicholasb3058bd2016-07-01 08:22:01 -07002051 break;
2052 default:
ethannicholasea4567c2016-10-17 11:24:37 -07002053 if (expr.fType == *fContext.fInvalid_Type) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002054 fErrors.error(expr.fOffset, "invalid expression");
ethannicholasea4567c2016-10-17 11:24:37 -07002055 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002056 }
2057}
2058
ethannicholasb3058bd2016-07-01 08:22:01 -07002059static bool has_duplicates(const Swizzle& swizzle) {
2060 int bits = 0;
2061 for (int idx : swizzle.fComponents) {
2062 ASSERT(idx >= 0 && idx <= 3);
2063 int bit = 1 << idx;
2064 if (bits & bit) {
2065 return true;
2066 }
2067 bits |= bit;
2068 }
2069 return false;
2070}
2071
Ethan Nicholas86a43402017-01-19 13:32:00 -05002072void IRGenerator::markWrittenTo(const Expression& expr, bool readWrite) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002073 switch (expr.fKind) {
2074 case Expression::kVariableReference_Kind: {
ethannicholasd598f792016-07-25 10:08:54 -07002075 const Variable& var = ((VariableReference&) expr).fVariable;
ethannicholasb3058bd2016-07-01 08:22:01 -07002076 if (var.fModifiers.fFlags & (Modifiers::kConst_Flag | Modifiers::kUniform_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002077 fErrors.error(expr.fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -07002078 "cannot modify immutable variable '" + var.fName + "'");
2079 }
Ethan Nicholas86a43402017-01-19 13:32:00 -05002080 ((VariableReference&) expr).setRefKind(readWrite ? VariableReference::kReadWrite_RefKind
2081 : VariableReference::kWrite_RefKind);
ethannicholasb3058bd2016-07-01 08:22:01 -07002082 break;
2083 }
2084 case Expression::kFieldAccess_Kind:
Ethan Nicholas86a43402017-01-19 13:32:00 -05002085 this->markWrittenTo(*((FieldAccess&) expr).fBase, readWrite);
ethannicholasb3058bd2016-07-01 08:22:01 -07002086 break;
2087 case Expression::kSwizzle_Kind:
2088 if (has_duplicates((Swizzle&) expr)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002089 fErrors.error(expr.fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -07002090 "cannot write to the same swizzle field more than once");
2091 }
Ethan Nicholas86a43402017-01-19 13:32:00 -05002092 this->markWrittenTo(*((Swizzle&) expr).fBase, readWrite);
ethannicholasb3058bd2016-07-01 08:22:01 -07002093 break;
2094 case Expression::kIndex_Kind:
Ethan Nicholas86a43402017-01-19 13:32:00 -05002095 this->markWrittenTo(*((IndexExpression&) expr).fBase, readWrite);
ethannicholasb3058bd2016-07-01 08:22:01 -07002096 break;
2097 default:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002098 fErrors.error(expr.fOffset, "cannot assign to '" + expr.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07002099 break;
2100 }
2101}
2102
Ethan Nicholase7e81c12017-12-12 09:48:48 -05002103void IRGenerator::convertProgram(Program::Kind kind,
2104 const char* text,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002105 size_t length,
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002106 SymbolTable& types,
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002107 std::vector<std::unique_ptr<ProgramElement>>* out) {
Ethan Nicholase7e81c12017-12-12 09:48:48 -05002108 fKind = kind;
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002109 fProgramElements = out;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002110 Parser parser(text, length, types, fErrors);
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002111 std::vector<std::unique_ptr<ASTDeclaration>> parsed = parser.file();
2112 if (fErrors.errorCount()) {
2113 return;
2114 }
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002115 for (size_t i = 0; i < parsed.size(); i++) {
2116 ASTDeclaration& decl = *parsed[i];
2117 switch (decl.fKind) {
2118 case ASTDeclaration::kVar_Kind: {
2119 std::unique_ptr<VarDeclarations> s = this->convertVarDeclarations(
2120 (ASTVarDeclarations&) decl,
2121 Variable::kGlobal_Storage);
2122 if (s) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002123 fProgramElements->push_back(std::move(s));
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002124 }
2125 break;
2126 }
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002127 case ASTDeclaration::kEnum_Kind: {
2128 this->convertEnum((ASTEnum&) decl);
2129 break;
2130 }
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002131 case ASTDeclaration::kFunction_Kind: {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002132 this->convertFunction((ASTFunction&) decl);
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002133 break;
2134 }
2135 case ASTDeclaration::kModifiers_Kind: {
2136 std::unique_ptr<ModifiersDeclaration> f = this->convertModifiersDeclaration(
2137 (ASTModifiersDeclaration&) decl);
2138 if (f) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002139 fProgramElements->push_back(std::move(f));
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002140 }
2141 break;
2142 }
2143 case ASTDeclaration::kInterfaceBlock_Kind: {
2144 std::unique_ptr<InterfaceBlock> i = this->convertInterfaceBlock(
2145 (ASTInterfaceBlock&) decl);
2146 if (i) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002147 fProgramElements->push_back(std::move(i));
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002148 }
2149 break;
2150 }
2151 case ASTDeclaration::kExtension_Kind: {
2152 std::unique_ptr<Extension> e = this->convertExtension((ASTExtension&) decl);
2153 if (e) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002154 fProgramElements->push_back(std::move(e));
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002155 }
2156 break;
2157 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04002158 case ASTDeclaration::kSection_Kind: {
2159 std::unique_ptr<Section> s = this->convertSection((ASTSection&) decl);
2160 if (s) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002161 fProgramElements->push_back(std::move(s));
Ethan Nicholas762466e2017-06-29 10:03:38 -04002162 }
2163 break;
2164 }
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002165 default:
2166 ABORT("unsupported declaration: %s\n", decl.description().c_str());
2167 }
2168 }
2169}
2170
2171
ethannicholasb3058bd2016-07-01 08:22:01 -07002172}