blob: 68d9f9c0caae1e0d90227782760db30e68d56a95 [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);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500129 CAP(dropsTileOnZeroDivide);
130 CAP(flatInterpolationSupport);
131 CAP(noperspectiveInterpolationSupport);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500132 CAP(externalTextureSupport);
133 CAP(texelFetchSupport);
134 CAP(imageLoadStoreSupport);
135 CAP(mustEnableAdvBlendEqs);
136 CAP(mustEnableSpecificAdvBlendEqs);
137 CAP(mustDeclareFragmentShaderOutput);
138 CAP(canUseAnyFunctionInShader);
Chris Dalton47c8ed32017-11-15 18:27:09 -0700139 CAP(floatIs32Bits);
Ethan Nicholas07990de2017-07-18 09:47:43 -0400140 CAP(integerSupport);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500141#undef CAP
142}
143
144void IRGenerator::start(const Program::Settings* settings) {
145 fSettings = settings;
146 fCapsMap.clear();
147 if (settings->fCaps) {
148 fill_caps(*settings->fCaps, &fCapsMap);
149 }
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500150 this->pushSymbolTable();
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400151 fInvocations = -1;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500152 fInputs.reset();
Robert Phillipsfe8da172018-01-24 14:52:02 +0000153 fSkPerVertex = nullptr;
154 fRTAdjust = nullptr;
155 fRTAdjustInterfaceBlock = nullptr;
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500156}
157
158void IRGenerator::finish() {
159 this->popSymbolTable();
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500160 fSettings = nullptr;
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500161}
162
ethannicholasb3058bd2016-07-01 08:22:01 -0700163std::unique_ptr<Extension> IRGenerator::convertExtension(const ASTExtension& extension) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700164 return std::unique_ptr<Extension>(new Extension(extension.fOffset, extension.fName));
ethannicholasb3058bd2016-07-01 08:22:01 -0700165}
166
167std::unique_ptr<Statement> IRGenerator::convertStatement(const ASTStatement& statement) {
168 switch (statement.fKind) {
169 case ASTStatement::kBlock_Kind:
170 return this->convertBlock((ASTBlock&) statement);
171 case ASTStatement::kVarDeclaration_Kind:
172 return this->convertVarDeclarationStatement((ASTVarDeclarationStatement&) statement);
Robert Phillipsfe8da172018-01-24 14:52:02 +0000173 case ASTStatement::kExpression_Kind: {
174 std::unique_ptr<Statement> result =
175 this->convertExpressionStatement((ASTExpressionStatement&) statement);
176 if (fRTAdjust && Program::kGeometry_Kind == fKind) {
177 ASSERT(result->fKind == Statement::kExpression_Kind);
178 Expression& expr = *((ExpressionStatement&) *result).fExpression;
179 if (expr.fKind == Expression::kFunctionCall_Kind) {
180 FunctionCall& fc = (FunctionCall&) expr;
181 if (fc.fFunction.fBuiltin && fc.fFunction.fName == "EmitVertex") {
182 std::vector<std::unique_ptr<Statement>> statements;
183 statements.push_back(getNormalizeSkPositionCode());
184 statements.push_back(std::move(result));
185 return std::unique_ptr<Block>(new Block(statement.fOffset,
186 std::move(statements),
187 fSymbolTable));
188 }
189 }
190 }
191 return result;
192 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700193 case ASTStatement::kIf_Kind:
194 return this->convertIf((ASTIfStatement&) statement);
195 case ASTStatement::kFor_Kind:
196 return this->convertFor((ASTForStatement&) statement);
197 case ASTStatement::kWhile_Kind:
198 return this->convertWhile((ASTWhileStatement&) statement);
199 case ASTStatement::kDo_Kind:
200 return this->convertDo((ASTDoStatement&) statement);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500201 case ASTStatement::kSwitch_Kind:
202 return this->convertSwitch((ASTSwitchStatement&) statement);
ethannicholasb3058bd2016-07-01 08:22:01 -0700203 case ASTStatement::kReturn_Kind:
204 return this->convertReturn((ASTReturnStatement&) statement);
205 case ASTStatement::kBreak_Kind:
206 return this->convertBreak((ASTBreakStatement&) statement);
207 case ASTStatement::kContinue_Kind:
208 return this->convertContinue((ASTContinueStatement&) statement);
209 case ASTStatement::kDiscard_Kind:
210 return this->convertDiscard((ASTDiscardStatement&) statement);
211 default:
212 ABORT("unsupported statement type: %d\n", statement.fKind);
213 }
214}
215
216std::unique_ptr<Block> IRGenerator::convertBlock(const ASTBlock& block) {
217 AutoSymbolTable table(this);
218 std::vector<std::unique_ptr<Statement>> statements;
219 for (size_t i = 0; i < block.fStatements.size(); i++) {
220 std::unique_ptr<Statement> statement = this->convertStatement(*block.fStatements[i]);
221 if (!statement) {
222 return nullptr;
223 }
224 statements.push_back(std::move(statement));
225 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700226 return std::unique_ptr<Block>(new Block(block.fOffset, std::move(statements), fSymbolTable));
ethannicholasb3058bd2016-07-01 08:22:01 -0700227}
228
229std::unique_ptr<Statement> IRGenerator::convertVarDeclarationStatement(
230 const ASTVarDeclarationStatement& s) {
ethannicholas14fe8cc2016-09-07 13:37:16 -0700231 auto decl = this->convertVarDeclarations(*s.fDeclarations, Variable::kLocal_Storage);
ethannicholasb3058bd2016-07-01 08:22:01 -0700232 if (!decl) {
233 return nullptr;
234 }
ethannicholas14fe8cc2016-09-07 13:37:16 -0700235 return std::unique_ptr<Statement>(new VarDeclarationsStatement(std::move(decl)));
ethannicholasb3058bd2016-07-01 08:22:01 -0700236}
237
ethannicholas14fe8cc2016-09-07 13:37:16 -0700238std::unique_ptr<VarDeclarations> IRGenerator::convertVarDeclarations(const ASTVarDeclarations& decl,
239 Variable::Storage storage) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000240 std::vector<std::unique_ptr<VarDeclaration>> variables;
ethannicholasd598f792016-07-25 10:08:54 -0700241 const Type* baseType = this->convertType(*decl.fType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700242 if (!baseType) {
243 return nullptr;
244 }
ethannicholas14fe8cc2016-09-07 13:37:16 -0700245 for (const auto& varDecl : decl.fVars) {
Ethan Nicholas6c942712018-03-16 09:45:11 -0400246 if (decl.fModifiers.fLayout.fLocation == 0 && decl.fModifiers.fLayout.fIndex == 0 &&
247 (decl.fModifiers.fFlags & Modifiers::kOut_Flag) && fKind == Program::kFragment_Kind &&
248 varDecl.fName != "sk_FragColor") {
249 fErrors.error(decl.fOffset,
250 "out location=0, index=0 is reserved for sk_FragColor");
251 }
ethannicholasd598f792016-07-25 10:08:54 -0700252 const Type* type = baseType;
ethannicholas14fe8cc2016-09-07 13:37:16 -0700253 std::vector<std::unique_ptr<Expression>> sizes;
254 for (const auto& rawSize : varDecl.fSizes) {
255 if (rawSize) {
256 auto size = this->coerce(this->convertExpression(*rawSize), *fContext.fInt_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -0700257 if (!size) {
258 return nullptr;
259 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700260 String name(type->fName);
Ethan Nicholas50afc172017-02-16 14:49:57 -0500261 int64_t count;
ethannicholasb3058bd2016-07-01 08:22:01 -0700262 if (size->fKind == Expression::kIntLiteral_Kind) {
263 count = ((IntLiteral&) *size).fValue;
264 if (count <= 0) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700265 fErrors.error(size->fOffset, "array size must be positive");
ethannicholasb3058bd2016-07-01 08:22:01 -0700266 }
267 name += "[" + to_string(count) + "]";
268 } else {
269 count = -1;
270 name += "[]";
271 }
ethannicholasd598f792016-07-25 10:08:54 -0700272 type = new Type(name, Type::kArray_Kind, *type, (int) count);
273 fSymbolTable->takeOwnership((Type*) type);
ethannicholas14fe8cc2016-09-07 13:37:16 -0700274 sizes.push_back(std::move(size));
ethannicholasb3058bd2016-07-01 08:22:01 -0700275 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700276 type = new Type(type->name() + "[]", Type::kArray_Kind, *type, -1);
ethannicholasd598f792016-07-25 10:08:54 -0700277 fSymbolTable->takeOwnership((Type*) type);
ethannicholas14fe8cc2016-09-07 13:37:16 -0700278 sizes.push_back(nullptr);
ethannicholasb3058bd2016-07-01 08:22:01 -0700279 }
280 }
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000281 auto var = std::unique_ptr<Variable>(new Variable(decl.fOffset, decl.fModifiers,
282 varDecl.fName, *type, storage));
Robert Phillipsfe8da172018-01-24 14:52:02 +0000283 if (var->fName == Compiler::RTADJUST_NAME) {
284 ASSERT(!fRTAdjust);
285 ASSERT(var->fType == *fContext.fFloat4_Type);
286 fRTAdjust = var.get();
287 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700288 std::unique_ptr<Expression> value;
ethannicholas14fe8cc2016-09-07 13:37:16 -0700289 if (varDecl.fValue) {
290 value = this->convertExpression(*varDecl.fValue);
ethannicholasb3058bd2016-07-01 08:22:01 -0700291 if (!value) {
292 return nullptr;
293 }
ethannicholasd598f792016-07-25 10:08:54 -0700294 value = this->coerce(std::move(value), *type);
Ethan Nicholas68dd2c12018-03-01 15:05:17 -0500295 if (!value) {
296 return nullptr;
297 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400298 var->fWriteCount = 1;
Ethan Nicholas8f6c2ab2018-01-17 13:51:52 -0500299 var->fInitialValue = value.get();
ethannicholasb3058bd2016-07-01 08:22:01 -0700300 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700301 if (storage == Variable::kGlobal_Storage && varDecl.fName == "sk_FragColor" &&
ethannicholasea4567c2016-10-17 11:24:37 -0700302 (*fSymbolTable)[varDecl.fName]) {
ethannicholas5961bc92016-10-12 06:39:56 -0700303 // already defined, ignore
ethannicholasea4567c2016-10-17 11:24:37 -0700304 } else if (storage == Variable::kGlobal_Storage && (*fSymbolTable)[varDecl.fName] &&
305 (*fSymbolTable)[varDecl.fName]->fKind == Symbol::kVariable_Kind &&
ethannicholas5961bc92016-10-12 06:39:56 -0700306 ((Variable*) (*fSymbolTable)[varDecl.fName])->fModifiers.fLayout.fBuiltin >= 0) {
ethannicholasf789b382016-08-03 12:43:36 -0700307 // already defined, just update the modifiers
ethannicholas14fe8cc2016-09-07 13:37:16 -0700308 Variable* old = (Variable*) (*fSymbolTable)[varDecl.fName];
ethannicholasf789b382016-08-03 12:43:36 -0700309 old->fModifiers = var->fModifiers;
310 } else {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000311 variables.emplace_back(new VarDeclaration(var.get(), std::move(sizes),
312 std::move(value)));
ethannicholas14fe8cc2016-09-07 13:37:16 -0700313 fSymbolTable->add(varDecl.fName, std::move(var));
ethannicholasf789b382016-08-03 12:43:36 -0700314 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700315 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700316 return std::unique_ptr<VarDeclarations>(new VarDeclarations(decl.fOffset,
ethannicholas14fe8cc2016-09-07 13:37:16 -0700317 baseType,
318 std::move(variables)));
ethannicholasb3058bd2016-07-01 08:22:01 -0700319}
320
ethannicholas5961bc92016-10-12 06:39:56 -0700321std::unique_ptr<ModifiersDeclaration> IRGenerator::convertModifiersDeclaration(
322 const ASTModifiersDeclaration& m) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400323 Modifiers modifiers = m.fModifiers;
324 if (modifiers.fLayout.fInvocations != -1) {
325 fInvocations = modifiers.fLayout.fInvocations;
Chris Daltonf1b47bb2017-10-06 11:57:51 -0600326 if (fSettings->fCaps && !fSettings->fCaps->gsInvocationsSupport()) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400327 modifiers.fLayout.fInvocations = -1;
328 Variable* invocationId = (Variable*) (*fSymbolTable)["sk_InvocationID"];
329 ASSERT(invocationId);
Ethan Nicholasd1d52562018-03-20 16:30:34 -0400330 invocationId->fModifiers.fFlags = 0;
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400331 invocationId->fModifiers.fLayout.fBuiltin = -1;
332 if (modifiers.fLayout.description() == "") {
333 return nullptr;
334 }
335 }
336 }
337 if (modifiers.fLayout.fMaxVertices != -1 && fInvocations > 0 && fSettings->fCaps &&
Chris Daltonf1b47bb2017-10-06 11:57:51 -0600338 !fSettings->fCaps->gsInvocationsSupport()) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400339 modifiers.fLayout.fMaxVertices *= fInvocations;
340 }
341 return std::unique_ptr<ModifiersDeclaration>(new ModifiersDeclaration(modifiers));
ethannicholas5961bc92016-10-12 06:39:56 -0700342}
343
ethannicholasb3058bd2016-07-01 08:22:01 -0700344std::unique_ptr<Statement> IRGenerator::convertIf(const ASTIfStatement& s) {
Ethan Nicholas11d53972016-11-28 11:23:23 -0500345 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*s.fTest),
ethannicholasd598f792016-07-25 10:08:54 -0700346 *fContext.fBool_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -0700347 if (!test) {
348 return nullptr;
349 }
350 std::unique_ptr<Statement> ifTrue = this->convertStatement(*s.fIfTrue);
351 if (!ifTrue) {
352 return nullptr;
353 }
354 std::unique_ptr<Statement> ifFalse;
355 if (s.fIfFalse) {
356 ifFalse = this->convertStatement(*s.fIfFalse);
357 if (!ifFalse) {
358 return nullptr;
359 }
360 }
ethannicholas08a92112016-11-09 13:26:45 -0800361 if (test->fKind == Expression::kBoolLiteral_Kind) {
362 // static boolean value, fold down to a single branch
363 if (((BoolLiteral&) *test).fValue) {
364 return ifTrue;
365 } else if (s.fIfFalse) {
366 return ifFalse;
367 } else {
368 // False & no else clause. Not an error, so don't return null!
369 std::vector<std::unique_ptr<Statement>> empty;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700370 return std::unique_ptr<Statement>(new Block(s.fOffset, std::move(empty),
ethannicholas08a92112016-11-09 13:26:45 -0800371 fSymbolTable));
372 }
373 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700374 return std::unique_ptr<Statement>(new IfStatement(s.fOffset, s.fIsStatic, std::move(test),
ethannicholasb3058bd2016-07-01 08:22:01 -0700375 std::move(ifTrue), std::move(ifFalse)));
376}
377
378std::unique_ptr<Statement> IRGenerator::convertFor(const ASTForStatement& f) {
ethannicholas22f939e2016-10-13 13:25:34 -0700379 AutoLoopLevel level(this);
ethannicholasb3058bd2016-07-01 08:22:01 -0700380 AutoSymbolTable table(this);
ethannicholas22f939e2016-10-13 13:25:34 -0700381 std::unique_ptr<Statement> initializer;
382 if (f.fInitializer) {
383 initializer = this->convertStatement(*f.fInitializer);
384 if (!initializer) {
385 return nullptr;
386 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700387 }
ethannicholas22f939e2016-10-13 13:25:34 -0700388 std::unique_ptr<Expression> test;
389 if (f.fTest) {
390 test = this->coerce(this->convertExpression(*f.fTest), *fContext.fBool_Type);
391 if (!test) {
392 return nullptr;
393 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700394 }
ethannicholas22f939e2016-10-13 13:25:34 -0700395 std::unique_ptr<Expression> next;
396 if (f.fNext) {
397 next = this->convertExpression(*f.fNext);
398 if (!next) {
399 return nullptr;
400 }
401 this->checkValid(*next);
ethannicholasb3058bd2016-07-01 08:22:01 -0700402 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700403 std::unique_ptr<Statement> statement = this->convertStatement(*f.fStatement);
404 if (!statement) {
405 return nullptr;
406 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700407 return std::unique_ptr<Statement>(new ForStatement(f.fOffset, std::move(initializer),
ethannicholasb3058bd2016-07-01 08:22:01 -0700408 std::move(test), std::move(next),
ethannicholasd598f792016-07-25 10:08:54 -0700409 std::move(statement), fSymbolTable));
ethannicholasb3058bd2016-07-01 08:22:01 -0700410}
411
412std::unique_ptr<Statement> IRGenerator::convertWhile(const ASTWhileStatement& w) {
ethannicholas22f939e2016-10-13 13:25:34 -0700413 AutoLoopLevel level(this);
Ethan Nicholas11d53972016-11-28 11:23:23 -0500414 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*w.fTest),
ethannicholasd598f792016-07-25 10:08:54 -0700415 *fContext.fBool_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -0700416 if (!test) {
417 return nullptr;
418 }
419 std::unique_ptr<Statement> statement = this->convertStatement(*w.fStatement);
420 if (!statement) {
421 return nullptr;
422 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700423 return std::unique_ptr<Statement>(new WhileStatement(w.fOffset, std::move(test),
ethannicholasb3058bd2016-07-01 08:22:01 -0700424 std::move(statement)));
425}
426
427std::unique_ptr<Statement> IRGenerator::convertDo(const ASTDoStatement& d) {
ethannicholas22f939e2016-10-13 13:25:34 -0700428 AutoLoopLevel level(this);
ethannicholasd598f792016-07-25 10:08:54 -0700429 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*d.fTest),
430 *fContext.fBool_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -0700431 if (!test) {
432 return nullptr;
433 }
434 std::unique_ptr<Statement> statement = this->convertStatement(*d.fStatement);
435 if (!statement) {
436 return nullptr;
437 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700438 return std::unique_ptr<Statement>(new DoStatement(d.fOffset, std::move(statement),
ethannicholasb3058bd2016-07-01 08:22:01 -0700439 std::move(test)));
440}
441
Ethan Nicholasaf197692017-02-27 13:26:45 -0500442std::unique_ptr<Statement> IRGenerator::convertSwitch(const ASTSwitchStatement& s) {
443 AutoSwitchLevel level(this);
444 std::unique_ptr<Expression> value = this->convertExpression(*s.fValue);
445 if (!value) {
446 return nullptr;
447 }
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500448 if (value->fType != *fContext.fUInt_Type && value->fType.kind() != Type::kEnum_Kind) {
Ethan Nicholasaf197692017-02-27 13:26:45 -0500449 value = this->coerce(std::move(value), *fContext.fInt_Type);
450 if (!value) {
451 return nullptr;
452 }
453 }
454 AutoSymbolTable table(this);
455 std::unordered_set<int> caseValues;
456 std::vector<std::unique_ptr<SwitchCase>> cases;
457 for (const auto& c : s.fCases) {
458 std::unique_ptr<Expression> caseValue;
459 if (c->fValue) {
460 caseValue = this->convertExpression(*c->fValue);
461 if (!caseValue) {
462 return nullptr;
463 }
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500464 caseValue = this->coerce(std::move(caseValue), value->fType);
465 if (!caseValue) {
466 return nullptr;
Ethan Nicholasaf197692017-02-27 13:26:45 -0500467 }
468 if (!caseValue->isConstant()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700469 fErrors.error(caseValue->fOffset, "case value must be a constant");
Ethan Nicholasaf197692017-02-27 13:26:45 -0500470 return nullptr;
471 }
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500472 int64_t v;
473 this->getConstantInt(*caseValue, &v);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500474 if (caseValues.find(v) != caseValues.end()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700475 fErrors.error(caseValue->fOffset, "duplicate case value");
Ethan Nicholasaf197692017-02-27 13:26:45 -0500476 }
477 caseValues.insert(v);
478 }
479 std::vector<std::unique_ptr<Statement>> statements;
480 for (const auto& s : c->fStatements) {
481 std::unique_ptr<Statement> converted = this->convertStatement(*s);
482 if (!converted) {
483 return nullptr;
484 }
485 statements.push_back(std::move(converted));
486 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700487 cases.emplace_back(new SwitchCase(c->fOffset, std::move(caseValue),
Ethan Nicholasaf197692017-02-27 13:26:45 -0500488 std::move(statements)));
489 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700490 return std::unique_ptr<Statement>(new SwitchStatement(s.fOffset, s.fIsStatic,
Ethan Nicholasc432b0c2017-07-18 13:22:37 -0400491 std::move(value), std::move(cases),
492 fSymbolTable));
Ethan Nicholasaf197692017-02-27 13:26:45 -0500493}
494
ethannicholasb3058bd2016-07-01 08:22:01 -0700495std::unique_ptr<Statement> IRGenerator::convertExpressionStatement(
496 const ASTExpressionStatement& s) {
497 std::unique_ptr<Expression> e = this->convertExpression(*s.fExpression);
498 if (!e) {
499 return nullptr;
500 }
501 this->checkValid(*e);
502 return std::unique_ptr<Statement>(new ExpressionStatement(std::move(e)));
503}
504
505std::unique_ptr<Statement> IRGenerator::convertReturn(const ASTReturnStatement& r) {
506 ASSERT(fCurrentFunction);
Robert Phillipsfe8da172018-01-24 14:52:02 +0000507 // early returns from a vertex main function will bypass the sk_Position normalization, so
508 // assert that we aren't doing that. It is of course possible to fix this by adding a
509 // normalization before each return, but it will probably never actually be necessary.
510 ASSERT(Program::kVertex_Kind != fKind || !fRTAdjust || "main" != fCurrentFunction->fName);
ethannicholasb3058bd2016-07-01 08:22:01 -0700511 if (r.fExpression) {
512 std::unique_ptr<Expression> result = this->convertExpression(*r.fExpression);
513 if (!result) {
514 return nullptr;
515 }
ethannicholasd598f792016-07-25 10:08:54 -0700516 if (fCurrentFunction->fReturnType == *fContext.fVoid_Type) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700517 fErrors.error(result->fOffset, "may not return a value from a void function");
ethannicholasb3058bd2016-07-01 08:22:01 -0700518 } else {
519 result = this->coerce(std::move(result), fCurrentFunction->fReturnType);
520 if (!result) {
521 return nullptr;
522 }
523 }
524 return std::unique_ptr<Statement>(new ReturnStatement(std::move(result)));
525 } else {
ethannicholasd598f792016-07-25 10:08:54 -0700526 if (fCurrentFunction->fReturnType != *fContext.fVoid_Type) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700527 fErrors.error(r.fOffset, "expected function to return '" +
528 fCurrentFunction->fReturnType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -0700529 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700530 return std::unique_ptr<Statement>(new ReturnStatement(r.fOffset));
ethannicholasb3058bd2016-07-01 08:22:01 -0700531 }
532}
533
534std::unique_ptr<Statement> IRGenerator::convertBreak(const ASTBreakStatement& b) {
Ethan Nicholasaf197692017-02-27 13:26:45 -0500535 if (fLoopLevel > 0 || fSwitchLevel > 0) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700536 return std::unique_ptr<Statement>(new BreakStatement(b.fOffset));
ethannicholas22f939e2016-10-13 13:25:34 -0700537 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700538 fErrors.error(b.fOffset, "break statement must be inside a loop or switch");
ethannicholas22f939e2016-10-13 13:25:34 -0700539 return nullptr;
540 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700541}
542
543std::unique_ptr<Statement> IRGenerator::convertContinue(const ASTContinueStatement& c) {
ethannicholas22f939e2016-10-13 13:25:34 -0700544 if (fLoopLevel > 0) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700545 return std::unique_ptr<Statement>(new ContinueStatement(c.fOffset));
ethannicholas22f939e2016-10-13 13:25:34 -0700546 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700547 fErrors.error(c.fOffset, "continue statement must be inside a loop");
ethannicholas22f939e2016-10-13 13:25:34 -0700548 return nullptr;
549 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700550}
551
552std::unique_ptr<Statement> IRGenerator::convertDiscard(const ASTDiscardStatement& d) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700553 return std::unique_ptr<Statement>(new DiscardStatement(d.fOffset));
ethannicholasb3058bd2016-07-01 08:22:01 -0700554}
555
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500556std::unique_ptr<Block> IRGenerator::applyInvocationIDWorkaround(std::unique_ptr<Block> main) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400557 Layout invokeLayout;
558 Modifiers invokeModifiers(invokeLayout, Modifiers::kHasSideEffects_Flag);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700559 FunctionDeclaration* invokeDecl = new FunctionDeclaration(-1,
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400560 invokeModifiers,
561 "_invoke",
562 std::vector<const Variable*>(),
563 *fContext.fVoid_Type);
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500564 fProgramElements->push_back(std::unique_ptr<ProgramElement>(
565 new FunctionDefinition(-1, *invokeDecl, std::move(main))));
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400566 fSymbolTable->add(invokeDecl->fName, std::unique_ptr<FunctionDeclaration>(invokeDecl));
567
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000568 std::vector<std::unique_ptr<VarDeclaration>> variables;
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400569 Variable* loopIdx = (Variable*) (*fSymbolTable)["sk_InvocationID"];
570 ASSERT(loopIdx);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700571 std::unique_ptr<Expression> test(new BinaryExpression(-1,
572 std::unique_ptr<Expression>(new VariableReference(-1, *loopIdx)),
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400573 Token::LT,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700574 std::unique_ptr<IntLiteral>(new IntLiteral(fContext, -1, fInvocations)),
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400575 *fContext.fBool_Type));
576 std::unique_ptr<Expression> next(new PostfixExpression(
577 std::unique_ptr<Expression>(
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700578 new VariableReference(-1,
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400579 *loopIdx,
580 VariableReference::kReadWrite_RefKind)),
581 Token::PLUSPLUS));
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700582 ASTIdentifier endPrimitiveID = ASTIdentifier(-1, "EndPrimitive");
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400583 std::unique_ptr<Expression> endPrimitive = this->convertExpression(endPrimitiveID);
584 ASSERT(endPrimitive);
585
586 std::vector<std::unique_ptr<Statement>> loopBody;
587 std::vector<std::unique_ptr<Expression>> invokeArgs;
588 loopBody.push_back(std::unique_ptr<Statement>(new ExpressionStatement(
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700589 this->call(-1,
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400590 *invokeDecl,
591 std::vector<std::unique_ptr<Expression>>()))));
592 loopBody.push_back(std::unique_ptr<Statement>(new ExpressionStatement(
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700593 this->call(-1,
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400594 std::move(endPrimitive),
595 std::vector<std::unique_ptr<Expression>>()))));
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700596 std::unique_ptr<Expression> assignment(new BinaryExpression(-1,
597 std::unique_ptr<Expression>(new VariableReference(-1, *loopIdx)),
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400598 Token::EQ,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700599 std::unique_ptr<IntLiteral>(new IntLiteral(fContext, -1, 0)),
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400600 *fContext.fInt_Type));
601 std::unique_ptr<Statement> initializer(new ExpressionStatement(std::move(assignment)));
602 std::unique_ptr<Statement> loop = std::unique_ptr<Statement>(
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700603 new ForStatement(-1,
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400604 std::move(initializer),
605 std::move(test),
606 std::move(next),
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700607 std::unique_ptr<Block>(new Block(-1, std::move(loopBody))),
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400608 fSymbolTable));
609 std::vector<std::unique_ptr<Statement>> children;
610 children.push_back(std::move(loop));
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700611 return std::unique_ptr<Block>(new Block(-1, std::move(children)));
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400612}
613
Robert Phillipsfe8da172018-01-24 14:52:02 +0000614std::unique_ptr<Statement> IRGenerator::getNormalizeSkPositionCode() {
615 // sk_Position = float4(sk_Position.x * rtAdjust.x + sk_Position.w * rtAdjust.y,
616 // sk_Position.y * rtAdjust.z + sk_Position.w * rtAdjust.w,
617 // 0,
618 // sk_Position.w);
619 ASSERT(fSkPerVertex && fRTAdjust);
620 #define REF(var) std::unique_ptr<Expression>(\
621 new VariableReference(-1, *var, VariableReference::kRead_RefKind))
622 #define FIELD(var, idx) std::unique_ptr<Expression>(\
623 new FieldAccess(REF(var), idx, FieldAccess::kAnonymousInterfaceBlock_OwnerKind))
624 #define POS std::unique_ptr<Expression>(new FieldAccess(REF(fSkPerVertex), 0, \
625 FieldAccess::kAnonymousInterfaceBlock_OwnerKind))
626 #define ADJUST (fRTAdjustInterfaceBlock ? \
627 FIELD(fRTAdjustInterfaceBlock, fRTAdjustFieldIndex) : \
628 REF(fRTAdjust))
629 #define SWIZZLE(expr, field) std::unique_ptr<Expression>(new Swizzle(fContext, expr, { field }))
630 #define OP(left, op, right) std::unique_ptr<Expression>(\
631 new BinaryExpression(-1, left, op, right, *fContext.fFloat_Type))
632 std::vector<std::unique_ptr<Expression>> children;
633 children.push_back(OP(OP(SWIZZLE(POS, 0), Token::STAR, SWIZZLE(ADJUST, 0)),
634 Token::PLUS,
635 OP(SWIZZLE(POS, 3), Token::STAR, SWIZZLE(ADJUST, 1))));
636 children.push_back(OP(OP(SWIZZLE(POS, 1), Token::STAR, SWIZZLE(ADJUST, 2)),
637 Token::PLUS,
638 OP(SWIZZLE(POS, 3), Token::STAR, SWIZZLE(ADJUST, 3))));
639 children.push_back(std::unique_ptr<Expression>(new FloatLiteral(fContext, -1, 0.0)));
640 children.push_back(SWIZZLE(POS, 3));
641 std::unique_ptr<Expression> result = OP(POS, Token::EQ,
642 std::unique_ptr<Expression>(new Constructor(-1,
643 *fContext.fFloat4_Type,
644 std::move(children))));
645 return std::unique_ptr<Statement>(new ExpressionStatement(std::move(result)));
646}
647
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500648void IRGenerator::convertFunction(const ASTFunction& f) {
ethannicholasd598f792016-07-25 10:08:54 -0700649 const Type* returnType = this->convertType(*f.fReturnType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700650 if (!returnType) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400651 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700652 }
ethannicholasd598f792016-07-25 10:08:54 -0700653 std::vector<const Variable*> parameters;
ethannicholasb3058bd2016-07-01 08:22:01 -0700654 for (const auto& param : f.fParameters) {
ethannicholasd598f792016-07-25 10:08:54 -0700655 const Type* type = this->convertType(*param->fType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700656 if (!type) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400657 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700658 }
659 for (int j = (int) param->fSizes.size() - 1; j >= 0; j--) {
660 int size = param->fSizes[j];
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400661 String name = type->name() + "[" + to_string(size) + "]";
ethannicholasd598f792016-07-25 10:08:54 -0700662 Type* newType = new Type(std::move(name), Type::kArray_Kind, *type, size);
663 fSymbolTable->takeOwnership(newType);
664 type = newType;
ethannicholasb3058bd2016-07-01 08:22:01 -0700665 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700666 StringFragment name = param->fName;
667 Variable* var = new Variable(param->fOffset, param->fModifiers, name, *type,
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000668 Variable::kParameter_Storage);
ethannicholasd598f792016-07-25 10:08:54 -0700669 fSymbolTable->takeOwnership(var);
670 parameters.push_back(var);
ethannicholasb3058bd2016-07-01 08:22:01 -0700671 }
672
673 // find existing declaration
ethannicholasd598f792016-07-25 10:08:54 -0700674 const FunctionDeclaration* decl = nullptr;
675 auto entry = (*fSymbolTable)[f.fName];
ethannicholasb3058bd2016-07-01 08:22:01 -0700676 if (entry) {
ethannicholasd598f792016-07-25 10:08:54 -0700677 std::vector<const FunctionDeclaration*> functions;
ethannicholasb3058bd2016-07-01 08:22:01 -0700678 switch (entry->fKind) {
679 case Symbol::kUnresolvedFunction_Kind:
ethannicholasd598f792016-07-25 10:08:54 -0700680 functions = ((UnresolvedFunction*) entry)->fFunctions;
ethannicholasb3058bd2016-07-01 08:22:01 -0700681 break;
682 case Symbol::kFunctionDeclaration_Kind:
ethannicholasd598f792016-07-25 10:08:54 -0700683 functions.push_back((FunctionDeclaration*) entry);
ethannicholasb3058bd2016-07-01 08:22:01 -0700684 break;
685 default:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700686 fErrors.error(f.fOffset, "symbol '" + f.fName + "' was already defined");
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400687 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700688 }
689 for (const auto& other : functions) {
690 ASSERT(other->fName == f.fName);
691 if (parameters.size() == other->fParameters.size()) {
692 bool match = true;
693 for (size_t i = 0; i < parameters.size(); i++) {
694 if (parameters[i]->fType != other->fParameters[i]->fType) {
695 match = false;
696 break;
697 }
698 }
699 if (match) {
ethannicholasd598f792016-07-25 10:08:54 -0700700 if (*returnType != other->fReturnType) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700701 FunctionDeclaration newDecl(f.fOffset, f.fModifiers, f.fName, parameters,
Ethan Nicholascb670962017-04-20 19:31:52 -0400702 *returnType);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700703 fErrors.error(f.fOffset, "functions '" + newDecl.description() +
704 "' and '" + other->description() +
705 "' differ only in return type");
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400706 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700707 }
708 decl = other;
709 for (size_t i = 0; i < parameters.size(); i++) {
710 if (parameters[i]->fModifiers != other->fParameters[i]->fModifiers) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700711 fErrors.error(f.fOffset, "modifiers on parameter " +
712 to_string((uint64_t) i + 1) +
713 " differ between declaration and "
714 "definition");
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400715 return;
ethannicholasb3058bd2016-07-01 08:22:01 -0700716 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700717 }
718 if (other->fDefined) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700719 fErrors.error(f.fOffset, "duplicate definition of " +
720 other->description());
ethannicholasb3058bd2016-07-01 08:22:01 -0700721 }
722 break;
723 }
724 }
725 }
726 }
727 if (!decl) {
728 // couldn't find an existing declaration
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700729 auto newDecl = std::unique_ptr<FunctionDeclaration>(new FunctionDeclaration(f.fOffset,
Ethan Nicholascb670962017-04-20 19:31:52 -0400730 f.fModifiers,
ethannicholas471e8942016-10-28 09:02:46 -0700731 f.fName,
732 parameters,
733 *returnType));
734 decl = newDecl.get();
735 fSymbolTable->add(decl->fName, std::move(newDecl));
ethannicholasb3058bd2016-07-01 08:22:01 -0700736 }
ethannicholasd598f792016-07-25 10:08:54 -0700737 if (f.fBody) {
738 ASSERT(!fCurrentFunction);
739 fCurrentFunction = decl;
740 decl->fDefined = true;
741 std::shared_ptr<SymbolTable> old = fSymbolTable;
742 AutoSymbolTable table(this);
743 for (size_t i = 0; i < parameters.size(); i++) {
744 fSymbolTable->addWithoutOwnership(parameters[i]->fName, decl->fParameters[i]);
ethannicholasb3058bd2016-07-01 08:22:01 -0700745 }
Chris Daltonf1b47bb2017-10-06 11:57:51 -0600746 bool needInvocationIDWorkaround = fInvocations != -1 && f.fName == "main" &&
747 fSettings->fCaps &&
748 !fSettings->fCaps->gsInvocationsSupport();
Ethan Nicholas762466e2017-06-29 10:03:38 -0400749 ASSERT(!fExtraVars.size());
ethannicholasd598f792016-07-25 10:08:54 -0700750 std::unique_ptr<Block> body = this->convertBlock(*f.fBody);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400751 for (auto& v : fExtraVars) {
752 body->fStatements.insert(body->fStatements.begin(), std::move(v));
753 }
754 fExtraVars.clear();
ethannicholasd598f792016-07-25 10:08:54 -0700755 fCurrentFunction = nullptr;
756 if (!body) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -0400757 return;
758 }
759 if (needInvocationIDWorkaround) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500760 body = this->applyInvocationIDWorkaround(std::move(body));
ethannicholasd598f792016-07-25 10:08:54 -0700761 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400762 // conservatively assume all user-defined functions have side effects
763 ((Modifiers&) decl->fModifiers).fFlags |= Modifiers::kHasSideEffects_Flag;
Robert Phillipsfe8da172018-01-24 14:52:02 +0000764 if (Program::kVertex_Kind == fKind && f.fName == "main" && fRTAdjust) {
765 body->fStatements.insert(body->fStatements.end(), this->getNormalizeSkPositionCode());
766 }
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500767 fProgramElements->push_back(std::unique_ptr<FunctionDefinition>(
768 new FunctionDefinition(f.fOffset, *decl, std::move(body))));
ethannicholasb3058bd2016-07-01 08:22:01 -0700769 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700770}
771
772std::unique_ptr<InterfaceBlock> IRGenerator::convertInterfaceBlock(const ASTInterfaceBlock& intf) {
773 std::shared_ptr<SymbolTable> old = fSymbolTable;
Ethan Nicholas68dd2c12018-03-01 15:05:17 -0500774 this->pushSymbolTable();
775 std::shared_ptr<SymbolTable> symbols = fSymbolTable;
ethannicholasb3058bd2016-07-01 08:22:01 -0700776 std::vector<Type::Field> fields;
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400777 bool haveRuntimeArray = false;
Robert Phillipsfe8da172018-01-24 14:52:02 +0000778 bool foundRTAdjust = false;
ethannicholasb3058bd2016-07-01 08:22:01 -0700779 for (size_t i = 0; i < intf.fDeclarations.size(); i++) {
ethannicholas14fe8cc2016-09-07 13:37:16 -0700780 std::unique_ptr<VarDeclarations> decl = this->convertVarDeclarations(
Ethan Nicholas11d53972016-11-28 11:23:23 -0500781 *intf.fDeclarations[i],
ethannicholasb3058bd2016-07-01 08:22:01 -0700782 Variable::kGlobal_Storage);
ethannicholas7effa7a2016-10-14 09:56:33 -0700783 if (!decl) {
784 return nullptr;
785 }
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000786 for (const auto& stmt : decl->fVars) {
787 VarDeclaration& vd = (VarDeclaration&) *stmt;
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400788 if (haveRuntimeArray) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000789 fErrors.error(decl->fOffset,
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400790 "only the last entry in an interface block may be a runtime-sized "
791 "array");
792 }
Robert Phillipsfe8da172018-01-24 14:52:02 +0000793 if (vd.fVar == fRTAdjust) {
794 foundRTAdjust = true;
795 ASSERT(vd.fVar->fType == *fContext.fFloat4_Type);
796 fRTAdjustFieldIndex = fields.size();
797 }
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000798 fields.push_back(Type::Field(vd.fVar->fModifiers, vd.fVar->fName,
799 &vd.fVar->fType));
800 if (vd.fValue) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700801 fErrors.error(decl->fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -0700802 "initializers are not permitted on interface block fields");
803 }
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000804 if (vd.fVar->fModifiers.fFlags & (Modifiers::kIn_Flag |
805 Modifiers::kOut_Flag |
806 Modifiers::kUniform_Flag |
807 Modifiers::kBuffer_Flag |
808 Modifiers::kConst_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700809 fErrors.error(decl->fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -0700810 "interface block fields may not have storage qualifiers");
811 }
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000812 if (vd.fVar->fType.kind() == Type::kArray_Kind &&
813 vd.fVar->fType.columns() == -1) {
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400814 haveRuntimeArray = true;
815 }
Ethan Nicholas11d53972016-11-28 11:23:23 -0500816 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700817 }
Ethan Nicholas68dd2c12018-03-01 15:05:17 -0500818 this->popSymbolTable();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700819 Type* type = new Type(intf.fOffset, intf.fTypeName, fields);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500820 old->takeOwnership(type);
Ethan Nicholas50afc172017-02-16 14:49:57 -0500821 std::vector<std::unique_ptr<Expression>> sizes;
822 for (const auto& size : intf.fSizes) {
823 if (size) {
824 std::unique_ptr<Expression> converted = this->convertExpression(*size);
825 if (!converted) {
826 return nullptr;
827 }
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400828 String name = type->fName;
Ethan Nicholas50afc172017-02-16 14:49:57 -0500829 int64_t count;
830 if (converted->fKind == Expression::kIntLiteral_Kind) {
831 count = ((IntLiteral&) *converted).fValue;
832 if (count <= 0) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700833 fErrors.error(converted->fOffset, "array size must be positive");
Ethan Nicholas50afc172017-02-16 14:49:57 -0500834 }
835 name += "[" + to_string(count) + "]";
836 } else {
837 count = -1;
838 name += "[]";
839 }
840 type = new Type(name, Type::kArray_Kind, *type, (int) count);
Ethan Nicholas68dd2c12018-03-01 15:05:17 -0500841 symbols->takeOwnership((Type*) type);
Ethan Nicholas50afc172017-02-16 14:49:57 -0500842 sizes.push_back(std::move(converted));
843 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700844 type = new Type(type->name() + "[]", Type::kArray_Kind, *type, -1);
Ethan Nicholas68dd2c12018-03-01 15:05:17 -0500845 symbols->takeOwnership((Type*) type);
Ethan Nicholas50afc172017-02-16 14:49:57 -0500846 sizes.push_back(nullptr);
847 }
848 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700849 Variable* var = new Variable(intf.fOffset, intf.fModifiers,
850 intf.fInstanceName.fLength ? intf.fInstanceName : intf.fTypeName,
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000851 *type, Variable::kGlobal_Storage);
Robert Phillipsfe8da172018-01-24 14:52:02 +0000852 if (foundRTAdjust) {
853 fRTAdjustInterfaceBlock = var;
854 }
Ethan Nicholas86a43402017-01-19 13:32:00 -0500855 old->takeOwnership(var);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700856 if (intf.fInstanceName.fLength) {
Ethan Nicholas50afc172017-02-16 14:49:57 -0500857 old->addWithoutOwnership(intf.fInstanceName, var);
ethannicholasb3058bd2016-07-01 08:22:01 -0700858 } else {
859 for (size_t i = 0; i < fields.size(); i++) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700860 old->add(fields[i].fName, std::unique_ptr<Field>(new Field(intf.fOffset, *var,
ethannicholasd598f792016-07-25 10:08:54 -0700861 (int) i)));
ethannicholasb3058bd2016-07-01 08:22:01 -0700862 }
863 }
Robert Phillipsfe8da172018-01-24 14:52:02 +0000864 if (var->fName == Compiler::PERVERTEX_NAME) {
865 ASSERT(!fSkPerVertex);
866 fSkPerVertex = var;
867 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700868 return std::unique_ptr<InterfaceBlock>(new InterfaceBlock(intf.fOffset,
Ethan Nicholas8feeff92017-03-30 14:11:58 -0400869 var,
Ethan Nicholas50afc172017-02-16 14:49:57 -0500870 intf.fTypeName,
871 intf.fInstanceName,
872 std::move(sizes),
Ethan Nicholas68dd2c12018-03-01 15:05:17 -0500873 symbols));
ethannicholasb3058bd2016-07-01 08:22:01 -0700874}
875
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500876void IRGenerator::getConstantInt(const Expression& value, int64_t* out) {
877 switch (value.fKind) {
878 case Expression::kIntLiteral_Kind:
879 *out = ((const IntLiteral&) value).fValue;
880 break;
881 case Expression::kVariableReference_Kind: {
882 const Variable& var = ((VariableReference&) value).fVariable;
883 if ((var.fModifiers.fFlags & Modifiers::kConst_Flag) &&
884 var.fInitialValue) {
885 this->getConstantInt(*var.fInitialValue, out);
886 }
887 break;
888 }
889 default:
890 fErrors.error(value.fOffset, "expected a constant int");
891 }
892}
893
894void IRGenerator::convertEnum(const ASTEnum& e) {
895 std::vector<Variable*> variables;
896 int64_t currentValue = 0;
897 Layout layout;
898 ASTType enumType(e.fOffset, e.fTypeName, ASTType::kIdentifier_Kind, {});
899 const Type* type = this->convertType(enumType);
900 Modifiers modifiers(layout, Modifiers::kConst_Flag);
901 std::shared_ptr<SymbolTable> symbols(new SymbolTable(fSymbolTable, &fErrors));
902 fSymbolTable = symbols;
903 for (size_t i = 0; i < e.fNames.size(); i++) {
904 std::unique_ptr<Expression> value;
905 if (e.fValues[i]) {
906 value = this->convertExpression(*e.fValues[i]);
907 if (!value) {
908 fSymbolTable = symbols->fParent;
909 return;
910 }
911 this->getConstantInt(*value, &currentValue);
912 }
913 value = std::unique_ptr<Expression>(new IntLiteral(fContext, e.fOffset, currentValue));
914 ++currentValue;
915 auto var = std::unique_ptr<Variable>(new Variable(e.fOffset, modifiers, e.fNames[i],
916 *type, Variable::kGlobal_Storage,
917 value.get()));
918 variables.push_back(var.get());
919 symbols->add(e.fNames[i], std::move(var));
920 symbols->takeOwnership(value.release());
921 }
922 fProgramElements->push_back(std::unique_ptr<ProgramElement>(new Enum(e.fOffset, e.fTypeName,
923 symbols)));
924 fSymbolTable = symbols->fParent;
925}
926
ethannicholasd598f792016-07-25 10:08:54 -0700927const Type* IRGenerator::convertType(const ASTType& type) {
928 const Symbol* result = (*fSymbolTable)[type.fName];
ethannicholasb3058bd2016-07-01 08:22:01 -0700929 if (result && result->fKind == Symbol::kType_Kind) {
Ethan Nicholas50afc172017-02-16 14:49:57 -0500930 for (int size : type.fSizes) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700931 String name(result->fName);
932 name += "[";
Ethan Nicholas50afc172017-02-16 14:49:57 -0500933 if (size != -1) {
934 name += to_string(size);
935 }
936 name += "]";
937 result = new Type(name, Type::kArray_Kind, (const Type&) *result, size);
938 fSymbolTable->takeOwnership((Type*) result);
939 }
ethannicholasd598f792016-07-25 10:08:54 -0700940 return (const Type*) result;
ethannicholasb3058bd2016-07-01 08:22:01 -0700941 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700942 fErrors.error(type.fOffset, "unknown type '" + type.fName + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -0700943 return nullptr;
944}
945
946std::unique_ptr<Expression> IRGenerator::convertExpression(const ASTExpression& expr) {
947 switch (expr.fKind) {
948 case ASTExpression::kIdentifier_Kind:
949 return this->convertIdentifier((ASTIdentifier&) expr);
950 case ASTExpression::kBool_Kind:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700951 return std::unique_ptr<Expression>(new BoolLiteral(fContext, expr.fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -0700952 ((ASTBoolLiteral&) expr).fValue));
953 case ASTExpression::kInt_Kind:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700954 return std::unique_ptr<Expression>(new IntLiteral(fContext, expr.fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -0700955 ((ASTIntLiteral&) expr).fValue));
956 case ASTExpression::kFloat_Kind:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700957 return std::unique_ptr<Expression>(new FloatLiteral(fContext, expr.fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -0700958 ((ASTFloatLiteral&) expr).fValue));
959 case ASTExpression::kBinary_Kind:
960 return this->convertBinaryExpression((ASTBinaryExpression&) expr);
961 case ASTExpression::kPrefix_Kind:
962 return this->convertPrefixExpression((ASTPrefixExpression&) expr);
963 case ASTExpression::kSuffix_Kind:
964 return this->convertSuffixExpression((ASTSuffixExpression&) expr);
965 case ASTExpression::kTernary_Kind:
966 return this->convertTernaryExpression((ASTTernaryExpression&) expr);
967 default:
968 ABORT("unsupported expression type: %d\n", expr.fKind);
969 }
970}
971
972std::unique_ptr<Expression> IRGenerator::convertIdentifier(const ASTIdentifier& identifier) {
ethannicholasd598f792016-07-25 10:08:54 -0700973 const Symbol* result = (*fSymbolTable)[identifier.fText];
ethannicholasb3058bd2016-07-01 08:22:01 -0700974 if (!result) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700975 fErrors.error(identifier.fOffset, "unknown identifier '" + identifier.fText + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -0700976 return nullptr;
977 }
978 switch (result->fKind) {
979 case Symbol::kFunctionDeclaration_Kind: {
ethannicholasd598f792016-07-25 10:08:54 -0700980 std::vector<const FunctionDeclaration*> f = {
981 (const FunctionDeclaration*) result
ethannicholasb3058bd2016-07-01 08:22:01 -0700982 };
ethannicholasd598f792016-07-25 10:08:54 -0700983 return std::unique_ptr<FunctionReference>(new FunctionReference(fContext,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700984 identifier.fOffset,
ethannicholasd598f792016-07-25 10:08:54 -0700985 f));
ethannicholasb3058bd2016-07-01 08:22:01 -0700986 }
987 case Symbol::kUnresolvedFunction_Kind: {
ethannicholasd598f792016-07-25 10:08:54 -0700988 const UnresolvedFunction* f = (const UnresolvedFunction*) result;
989 return std::unique_ptr<FunctionReference>(new FunctionReference(fContext,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700990 identifier.fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -0700991 f->fFunctions));
992 }
993 case Symbol::kVariable_Kind: {
Ethan Nicholas38657112017-02-09 17:01:22 -0500994 const Variable* var = (const Variable*) result;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400995#ifndef SKSL_STANDALONE
Ethan Nicholas38657112017-02-09 17:01:22 -0500996 if (var->fModifiers.fLayout.fBuiltin == SK_FRAGCOORD_BUILTIN) {
997 fInputs.fFlipY = true;
998 if (fSettings->fFlipY &&
999 (!fSettings->fCaps ||
1000 !fSettings->fCaps->fragCoordConventionsExtensionString())) {
1001 fInputs.fRTHeight = true;
1002 }
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001003 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001004#endif
Ethan Nicholas86a43402017-01-19 13:32:00 -05001005 // default to kRead_RefKind; this will be corrected later if the variable is written to
1006 return std::unique_ptr<VariableReference>(new VariableReference(
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001007 identifier.fOffset,
Ethan Nicholas86a43402017-01-19 13:32:00 -05001008 *var,
1009 VariableReference::kRead_RefKind));
ethannicholasb3058bd2016-07-01 08:22:01 -07001010 }
1011 case Symbol::kField_Kind: {
ethannicholasd598f792016-07-25 10:08:54 -07001012 const Field* field = (const Field*) result;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001013 VariableReference* base = new VariableReference(identifier.fOffset, field->fOwner,
Ethan Nicholas86a43402017-01-19 13:32:00 -05001014 VariableReference::kRead_RefKind);
ethannicholasf789b382016-08-03 12:43:36 -07001015 return std::unique_ptr<Expression>(new FieldAccess(
1016 std::unique_ptr<Expression>(base),
1017 field->fFieldIndex,
1018 FieldAccess::kAnonymousInterfaceBlock_OwnerKind));
ethannicholasb3058bd2016-07-01 08:22:01 -07001019 }
1020 case Symbol::kType_Kind: {
ethannicholasd598f792016-07-25 10:08:54 -07001021 const Type* t = (const Type*) result;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001022 return std::unique_ptr<TypeReference>(new TypeReference(fContext, identifier.fOffset,
ethannicholasd598f792016-07-25 10:08:54 -07001023 *t));
ethannicholasb3058bd2016-07-01 08:22:01 -07001024 }
1025 default:
1026 ABORT("unsupported symbol type %d\n", result->fKind);
1027 }
Ethan Nicholasc0709392017-06-27 11:20:22 -04001028}
1029
Ethan Nicholas762466e2017-06-29 10:03:38 -04001030std::unique_ptr<Section> IRGenerator::convertSection(const ASTSection& s) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001031 return std::unique_ptr<Section>(new Section(s.fOffset, s.fName, s.fArgument, s.fText));
Ethan Nicholas762466e2017-06-29 10:03:38 -04001032}
1033
1034
Ethan Nicholas11d53972016-11-28 11:23:23 -05001035std::unique_ptr<Expression> IRGenerator::coerce(std::unique_ptr<Expression> expr,
ethannicholasd598f792016-07-25 10:08:54 -07001036 const Type& type) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001037 if (!expr) {
1038 return nullptr;
1039 }
ethannicholasd598f792016-07-25 10:08:54 -07001040 if (expr->fType == type) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001041 return expr;
1042 }
1043 this->checkValid(*expr);
ethannicholasd598f792016-07-25 10:08:54 -07001044 if (expr->fType == *fContext.fInvalid_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001045 return nullptr;
1046 }
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001047 if (expr->coercionCost(type) == INT_MAX) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001048 fErrors.error(expr->fOffset, "expected '" + type.description() + "', but found '" +
ethannicholasd598f792016-07-25 10:08:54 -07001049 expr->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001050 return nullptr;
1051 }
ethannicholasd598f792016-07-25 10:08:54 -07001052 if (type.kind() == Type::kScalar_Kind) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001053 std::vector<std::unique_ptr<Expression>> args;
1054 args.push_back(std::move(expr));
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001055 ASTIdentifier id(-1, type.fName);
ethannicholasb3058bd2016-07-01 08:22:01 -07001056 std::unique_ptr<Expression> ctor = this->convertIdentifier(id);
1057 ASSERT(ctor);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001058 return this->call(-1, std::move(ctor), std::move(args));
ethannicholasb3058bd2016-07-01 08:22:01 -07001059 }
ethannicholas5961bc92016-10-12 06:39:56 -07001060 std::vector<std::unique_ptr<Expression>> args;
1061 args.push_back(std::move(expr));
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001062 return std::unique_ptr<Expression>(new Constructor(-1, type, std::move(args)));
ethannicholasb3058bd2016-07-01 08:22:01 -07001063}
1064
ethannicholasf789b382016-08-03 12:43:36 -07001065static bool is_matrix_multiply(const Type& left, const Type& right) {
1066 if (left.kind() == Type::kMatrix_Kind) {
1067 return right.kind() == Type::kMatrix_Kind || right.kind() == Type::kVector_Kind;
1068 }
1069 return left.kind() == Type::kVector_Kind && right.kind() == Type::kMatrix_Kind;
1070}
ethannicholasea4567c2016-10-17 11:24:37 -07001071
ethannicholasb3058bd2016-07-01 08:22:01 -07001072/**
1073 * Determines the operand and result types of a binary expression. Returns true if the expression is
1074 * legal, false otherwise. If false, the values of the out parameters are undefined.
1075 */
Ethan Nicholas11d53972016-11-28 11:23:23 -05001076static bool determine_binary_type(const Context& context,
1077 Token::Kind op,
1078 const Type& left,
1079 const Type& right,
ethannicholasd598f792016-07-25 10:08:54 -07001080 const Type** outLeftType,
1081 const Type** outRightType,
1082 const Type** outResultType,
ethannicholasb3058bd2016-07-01 08:22:01 -07001083 bool tryFlipped) {
1084 bool isLogical;
ethannicholasea4567c2016-10-17 11:24:37 -07001085 bool validMatrixOrVectorOp;
ethannicholasb3058bd2016-07-01 08:22:01 -07001086 switch (op) {
ethannicholasea4567c2016-10-17 11:24:37 -07001087 case Token::EQ:
1088 *outLeftType = &left;
1089 *outRightType = &left;
1090 *outResultType = &left;
1091 return right.canCoerceTo(left);
ethannicholasb3058bd2016-07-01 08:22:01 -07001092 case Token::EQEQ: // fall through
ethannicholasea4567c2016-10-17 11:24:37 -07001093 case Token::NEQ:
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001094 if (left == right) {
1095 *outLeftType = &left;
1096 *outRightType = &right;
1097 *outResultType = context.fBool_Type.get();
1098 return true;
1099 }
ethannicholasea4567c2016-10-17 11:24:37 -07001100 isLogical = true;
1101 validMatrixOrVectorOp = true;
1102 break;
ethannicholasb3058bd2016-07-01 08:22:01 -07001103 case Token::LT: // fall through
1104 case Token::GT: // fall through
1105 case Token::LTEQ: // fall through
1106 case Token::GTEQ:
1107 isLogical = true;
ethannicholasea4567c2016-10-17 11:24:37 -07001108 validMatrixOrVectorOp = false;
ethannicholasb3058bd2016-07-01 08:22:01 -07001109 break;
1110 case Token::LOGICALOR: // fall through
1111 case Token::LOGICALAND: // fall through
1112 case Token::LOGICALXOR: // fall through
1113 case Token::LOGICALOREQ: // fall through
1114 case Token::LOGICALANDEQ: // fall through
1115 case Token::LOGICALXOREQ:
ethannicholasd598f792016-07-25 10:08:54 -07001116 *outLeftType = context.fBool_Type.get();
1117 *outRightType = context.fBool_Type.get();
1118 *outResultType = context.fBool_Type.get();
Ethan Nicholas11d53972016-11-28 11:23:23 -05001119 return left.canCoerceTo(*context.fBool_Type) &&
ethannicholasd598f792016-07-25 10:08:54 -07001120 right.canCoerceTo(*context.fBool_Type);
Ethan Nicholas11d53972016-11-28 11:23:23 -05001121 case Token::STAREQ:
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001122 if (left.kind() == Type::kScalar_Kind) {
1123 *outLeftType = &left;
1124 *outRightType = &left;
1125 *outResultType = &left;
1126 return right.canCoerceTo(left);
1127 }
1128 // fall through
1129 case Token::STAR:
ethannicholasf789b382016-08-03 12:43:36 -07001130 if (is_matrix_multiply(left, right)) {
1131 // determine final component type
1132 if (determine_binary_type(context, Token::STAR, left.componentType(),
1133 right.componentType(), outLeftType, outRightType,
1134 outResultType, false)) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001135 *outLeftType = &(*outResultType)->toCompound(context, left.columns(),
ethannicholasf789b382016-08-03 12:43:36 -07001136 left.rows());;
Ethan Nicholas11d53972016-11-28 11:23:23 -05001137 *outRightType = &(*outResultType)->toCompound(context, right.columns(),
ethannicholasf789b382016-08-03 12:43:36 -07001138 right.rows());;
1139 int leftColumns = left.columns();
1140 int leftRows = left.rows();
1141 int rightColumns;
1142 int rightRows;
1143 if (right.kind() == Type::kVector_Kind) {
1144 // matrix * vector treats the vector as a column vector, so we need to
1145 // transpose it
1146 rightColumns = right.rows();
1147 rightRows = right.columns();
1148 ASSERT(rightColumns == 1);
1149 } else {
1150 rightColumns = right.columns();
1151 rightRows = right.rows();
1152 }
1153 if (rightColumns > 1) {
1154 *outResultType = &(*outResultType)->toCompound(context, rightColumns,
1155 leftRows);
1156 } else {
1157 // result was a column vector, transpose it back to a row
1158 *outResultType = &(*outResultType)->toCompound(context, leftRows,
1159 rightColumns);
1160 }
1161 return leftColumns == rightRows;
1162 } else {
1163 return false;
1164 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001165 }
ethannicholasea4567c2016-10-17 11:24:37 -07001166 isLogical = false;
1167 validMatrixOrVectorOp = true;
1168 break;
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001169 case Token::PLUSEQ:
1170 case Token::MINUSEQ:
1171 case Token::SLASHEQ:
1172 case Token::PERCENTEQ:
1173 case Token::SHLEQ:
1174 case Token::SHREQ:
1175 if (left.kind() == Type::kScalar_Kind) {
1176 *outLeftType = &left;
1177 *outRightType = &left;
1178 *outResultType = &left;
1179 return right.canCoerceTo(left);
1180 }
1181 // fall through
ethannicholasea4567c2016-10-17 11:24:37 -07001182 case Token::PLUS: // fall through
ethannicholasea4567c2016-10-17 11:24:37 -07001183 case Token::MINUS: // fall through
ethannicholasea4567c2016-10-17 11:24:37 -07001184 case Token::SLASH: // fall through
ethannicholasea4567c2016-10-17 11:24:37 -07001185 isLogical = false;
1186 validMatrixOrVectorOp = true;
1187 break;
Ethan Nicholas4b330df2017-05-17 10:52:55 -04001188 case Token::COMMA:
1189 *outLeftType = &left;
1190 *outRightType = &right;
1191 *outResultType = &right;
1192 return true;
ethannicholasb3058bd2016-07-01 08:22:01 -07001193 default:
1194 isLogical = false;
ethannicholasea4567c2016-10-17 11:24:37 -07001195 validMatrixOrVectorOp = false;
ethannicholasb3058bd2016-07-01 08:22:01 -07001196 }
ethannicholasea4567c2016-10-17 11:24:37 -07001197 bool isVectorOrMatrix = left.kind() == Type::kVector_Kind || left.kind() == Type::kMatrix_Kind;
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001198 if (left.kind() == Type::kScalar_Kind && right.kind() == Type::kScalar_Kind &&
1199 right.canCoerceTo(left)) {
1200 if (left.priority() > right.priority()) {
1201 *outLeftType = &left;
1202 *outRightType = &left;
1203 } else {
1204 *outLeftType = &right;
1205 *outRightType = &right;
1206 }
1207 if (isLogical) {
1208 *outResultType = context.fBool_Type.get();
1209 } else {
1210 *outResultType = &left;
1211 }
1212 return true;
1213 }
1214 if (right.canCoerceTo(left) && isVectorOrMatrix && validMatrixOrVectorOp) {
ethannicholasd598f792016-07-25 10:08:54 -07001215 *outLeftType = &left;
1216 *outRightType = &left;
ethannicholasb3058bd2016-07-01 08:22:01 -07001217 if (isLogical) {
ethannicholasd598f792016-07-25 10:08:54 -07001218 *outResultType = context.fBool_Type.get();
ethannicholasb3058bd2016-07-01 08:22:01 -07001219 } else {
ethannicholasd598f792016-07-25 10:08:54 -07001220 *outResultType = &left;
ethannicholasb3058bd2016-07-01 08:22:01 -07001221 }
1222 return true;
1223 }
Ethan Nicholas11d53972016-11-28 11:23:23 -05001224 if ((left.kind() == Type::kVector_Kind || left.kind() == Type::kMatrix_Kind) &&
ethannicholasd598f792016-07-25 10:08:54 -07001225 (right.kind() == Type::kScalar_Kind)) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001226 if (determine_binary_type(context, op, left.componentType(), right, outLeftType,
ethannicholasd598f792016-07-25 10:08:54 -07001227 outRightType, outResultType, false)) {
1228 *outLeftType = &(*outLeftType)->toCompound(context, left.columns(), left.rows());
ethannicholasb3058bd2016-07-01 08:22:01 -07001229 if (!isLogical) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001230 *outResultType = &(*outResultType)->toCompound(context, left.columns(),
ethannicholasd598f792016-07-25 10:08:54 -07001231 left.rows());
ethannicholasb3058bd2016-07-01 08:22:01 -07001232 }
1233 return true;
1234 }
1235 return false;
1236 }
1237 if (tryFlipped) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001238 return determine_binary_type(context, op, right, left, outRightType, outLeftType,
ethannicholasd598f792016-07-25 10:08:54 -07001239 outResultType, false);
ethannicholasb3058bd2016-07-01 08:22:01 -07001240 }
1241 return false;
1242}
1243
ethannicholas08a92112016-11-09 13:26:45 -08001244std::unique_ptr<Expression> IRGenerator::constantFold(const Expression& left,
1245 Token::Kind op,
Ethan Nicholas86a43402017-01-19 13:32:00 -05001246 const Expression& right) const {
Ethan Nicholascb670962017-04-20 19:31:52 -04001247 if (!left.isConstant() || !right.isConstant()) {
1248 return nullptr;
1249 }
ethannicholas08a92112016-11-09 13:26:45 -08001250 // Note that we expressly do not worry about precision and overflow here -- we use the maximum
1251 // precision to calculate the results and hope the result makes sense. The plan is to move the
1252 // Skia caps into SkSL, so we have access to all of them including the precisions of the various
1253 // types, which will let us be more intelligent about this.
Ethan Nicholas11d53972016-11-28 11:23:23 -05001254 if (left.fKind == Expression::kBoolLiteral_Kind &&
ethannicholas08a92112016-11-09 13:26:45 -08001255 right.fKind == Expression::kBoolLiteral_Kind) {
1256 bool leftVal = ((BoolLiteral&) left).fValue;
1257 bool rightVal = ((BoolLiteral&) right).fValue;
1258 bool result;
1259 switch (op) {
1260 case Token::LOGICALAND: result = leftVal && rightVal; break;
1261 case Token::LOGICALOR: result = leftVal || rightVal; break;
1262 case Token::LOGICALXOR: result = leftVal ^ rightVal; break;
1263 default: return nullptr;
1264 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001265 return std::unique_ptr<Expression>(new BoolLiteral(fContext, left.fOffset, result));
ethannicholas08a92112016-11-09 13:26:45 -08001266 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001267 #define RESULT(t, op) std::unique_ptr<Expression>(new t ## Literal(fContext, left.fOffset, \
ethannicholas08a92112016-11-09 13:26:45 -08001268 leftVal op rightVal))
1269 if (left.fKind == Expression::kIntLiteral_Kind && right.fKind == Expression::kIntLiteral_Kind) {
1270 int64_t leftVal = ((IntLiteral&) left).fValue;
1271 int64_t rightVal = ((IntLiteral&) right).fValue;
1272 switch (op) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001273 case Token::PLUS: return RESULT(Int, +);
1274 case Token::MINUS: return RESULT(Int, -);
1275 case Token::STAR: return RESULT(Int, *);
Ethan Nicholas9a5610e2017-01-03 15:16:29 -05001276 case Token::SLASH:
1277 if (rightVal) {
1278 return RESULT(Int, /);
1279 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001280 fErrors.error(right.fOffset, "division by zero");
Ethan Nicholas9a5610e2017-01-03 15:16:29 -05001281 return nullptr;
Ethan Nicholas2503ab62017-01-05 10:44:25 -05001282 case Token::PERCENT:
1283 if (rightVal) {
1284 return RESULT(Int, %);
1285 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001286 fErrors.error(right.fOffset, "division by zero");
Ethan Nicholas2503ab62017-01-05 10:44:25 -05001287 return nullptr;
ethannicholas08a92112016-11-09 13:26:45 -08001288 case Token::BITWISEAND: return RESULT(Int, &);
1289 case Token::BITWISEOR: return RESULT(Int, |);
1290 case Token::BITWISEXOR: return RESULT(Int, ^);
1291 case Token::SHL: return RESULT(Int, <<);
1292 case Token::SHR: return RESULT(Int, >>);
1293 case Token::EQEQ: return RESULT(Bool, ==);
1294 case Token::NEQ: return RESULT(Bool, !=);
1295 case Token::GT: return RESULT(Bool, >);
1296 case Token::GTEQ: return RESULT(Bool, >=);
1297 case Token::LT: return RESULT(Bool, <);
1298 case Token::LTEQ: return RESULT(Bool, <=);
1299 default: return nullptr;
1300 }
1301 }
Ethan Nicholas11d53972016-11-28 11:23:23 -05001302 if (left.fKind == Expression::kFloatLiteral_Kind &&
ethannicholas08a92112016-11-09 13:26:45 -08001303 right.fKind == Expression::kFloatLiteral_Kind) {
1304 double leftVal = ((FloatLiteral&) left).fValue;
1305 double rightVal = ((FloatLiteral&) right).fValue;
1306 switch (op) {
1307 case Token::PLUS: return RESULT(Float, +);
1308 case Token::MINUS: return RESULT(Float, -);
1309 case Token::STAR: return RESULT(Float, *);
Ethan Nicholas9a5610e2017-01-03 15:16:29 -05001310 case Token::SLASH:
1311 if (rightVal) {
1312 return RESULT(Float, /);
1313 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001314 fErrors.error(right.fOffset, "division by zero");
Ethan Nicholas9a5610e2017-01-03 15:16:29 -05001315 return nullptr;
Ethan Nicholascb670962017-04-20 19:31:52 -04001316 case Token::EQEQ: return RESULT(Bool, ==);
1317 case Token::NEQ: return RESULT(Bool, !=);
1318 case Token::GT: return RESULT(Bool, >);
1319 case Token::GTEQ: return RESULT(Bool, >=);
1320 case Token::LT: return RESULT(Bool, <);
1321 case Token::LTEQ: return RESULT(Bool, <=);
ethannicholas08a92112016-11-09 13:26:45 -08001322 default: return nullptr;
1323 }
1324 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001325 if (left.fType.kind() == Type::kVector_Kind &&
1326 left.fType.componentType() == *fContext.fFloat_Type &&
1327 left.fType == right.fType) {
1328 ASSERT(left.fKind == Expression::kConstructor_Kind);
1329 ASSERT(right.fKind == Expression::kConstructor_Kind);
1330 std::vector<std::unique_ptr<Expression>> args;
1331 #define RETURN_VEC_COMPONENTWISE_RESULT(op) \
1332 for (int i = 0; i < left.fType.columns(); i++) { \
1333 float value = ((Constructor&) left).getFVecComponent(i) op \
1334 ((Constructor&) right).getFVecComponent(i); \
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001335 args.emplace_back(new FloatLiteral(fContext, -1, value)); \
Ethan Nicholascb670962017-04-20 19:31:52 -04001336 } \
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001337 return std::unique_ptr<Expression>(new Constructor(-1, left.fType, \
Ethan Nicholascb670962017-04-20 19:31:52 -04001338 std::move(args)));
1339 switch (op) {
Ethan Nicholas3deaeb22017-04-25 14:42:11 -04001340 case Token::EQEQ:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001341 return std::unique_ptr<Expression>(new BoolLiteral(fContext, -1,
Ethan Nicholas3deaeb22017-04-25 14:42:11 -04001342 left.compareConstant(fContext, right)));
1343 case Token::NEQ:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001344 return std::unique_ptr<Expression>(new BoolLiteral(fContext, -1,
Ethan Nicholas3deaeb22017-04-25 14:42:11 -04001345 !left.compareConstant(fContext, right)));
Ethan Nicholascb670962017-04-20 19:31:52 -04001346 case Token::PLUS: RETURN_VEC_COMPONENTWISE_RESULT(+);
1347 case Token::MINUS: RETURN_VEC_COMPONENTWISE_RESULT(-);
1348 case Token::STAR: RETURN_VEC_COMPONENTWISE_RESULT(*);
1349 case Token::SLASH: RETURN_VEC_COMPONENTWISE_RESULT(/);
1350 default: return nullptr;
1351 }
1352 }
Ethan Nicholas3deaeb22017-04-25 14:42:11 -04001353 if (left.fType.kind() == Type::kMatrix_Kind &&
1354 right.fType.kind() == Type::kMatrix_Kind &&
1355 left.fKind == right.fKind) {
1356 switch (op) {
1357 case Token::EQEQ:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001358 return std::unique_ptr<Expression>(new BoolLiteral(fContext, -1,
Ethan Nicholas3deaeb22017-04-25 14:42:11 -04001359 left.compareConstant(fContext, right)));
1360 case Token::NEQ:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001361 return std::unique_ptr<Expression>(new BoolLiteral(fContext, -1,
Ethan Nicholas3deaeb22017-04-25 14:42:11 -04001362 !left.compareConstant(fContext, right)));
1363 default:
1364 return nullptr;
1365 }
1366 }
ethannicholas08a92112016-11-09 13:26:45 -08001367 #undef RESULT
1368 return nullptr;
1369}
1370
ethannicholasb3058bd2016-07-01 08:22:01 -07001371std::unique_ptr<Expression> IRGenerator::convertBinaryExpression(
1372 const ASTBinaryExpression& expression) {
1373 std::unique_ptr<Expression> left = this->convertExpression(*expression.fLeft);
1374 if (!left) {
1375 return nullptr;
1376 }
1377 std::unique_ptr<Expression> right = this->convertExpression(*expression.fRight);
1378 if (!right) {
1379 return nullptr;
1380 }
ethannicholasd598f792016-07-25 10:08:54 -07001381 const Type* leftType;
1382 const Type* rightType;
1383 const Type* resultType;
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001384 const Type* rawLeftType;
1385 if (left->fKind == Expression::kIntLiteral_Kind && right->fType.isInteger()) {
1386 rawLeftType = &right->fType;
1387 } else {
1388 rawLeftType = &left->fType;
1389 }
1390 const Type* rawRightType;
1391 if (right->fKind == Expression::kIntLiteral_Kind && left->fType.isInteger()) {
1392 rawRightType = &left->fType;
1393 } else {
1394 rawRightType = &right->fType;
1395 }
1396 if (!determine_binary_type(fContext, expression.fOperator, *rawLeftType, *rawRightType,
1397 &leftType, &rightType, &resultType,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001398 !Compiler::IsAssignment(expression.fOperator))) {
1399 fErrors.error(expression.fOffset, String("type mismatch: '") +
1400 Compiler::OperatorName(expression.fOperator) +
1401 "' cannot operate on '" + left->fType.fName +
1402 "', '" + right->fType.fName + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001403 return nullptr;
1404 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001405 if (Compiler::IsAssignment(expression.fOperator)) {
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -04001406 this->setRefKind(*left, expression.fOperator != Token::EQ ?
1407 VariableReference::kReadWrite_RefKind :
1408 VariableReference::kWrite_RefKind);
ethannicholasea4567c2016-10-17 11:24:37 -07001409 }
1410 left = this->coerce(std::move(left), *leftType);
1411 right = this->coerce(std::move(right), *rightType);
1412 if (!left || !right) {
1413 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07001414 }
Ethan Nicholas11d53972016-11-28 11:23:23 -05001415 std::unique_ptr<Expression> result = this->constantFold(*left.get(), expression.fOperator,
ethannicholas08a92112016-11-09 13:26:45 -08001416 *right.get());
1417 if (!result) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001418 result = std::unique_ptr<Expression>(new BinaryExpression(expression.fOffset,
ethannicholas08a92112016-11-09 13:26:45 -08001419 std::move(left),
1420 expression.fOperator,
1421 std::move(right),
1422 *resultType));
1423 }
1424 return result;
ethannicholasb3058bd2016-07-01 08:22:01 -07001425}
1426
Ethan Nicholas11d53972016-11-28 11:23:23 -05001427std::unique_ptr<Expression> IRGenerator::convertTernaryExpression(
ethannicholasb3058bd2016-07-01 08:22:01 -07001428 const ASTTernaryExpression& expression) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001429 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*expression.fTest),
ethannicholasd598f792016-07-25 10:08:54 -07001430 *fContext.fBool_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -07001431 if (!test) {
1432 return nullptr;
1433 }
1434 std::unique_ptr<Expression> ifTrue = this->convertExpression(*expression.fIfTrue);
1435 if (!ifTrue) {
1436 return nullptr;
1437 }
1438 std::unique_ptr<Expression> ifFalse = this->convertExpression(*expression.fIfFalse);
1439 if (!ifFalse) {
1440 return nullptr;
1441 }
ethannicholasd598f792016-07-25 10:08:54 -07001442 const Type* trueType;
1443 const Type* falseType;
1444 const Type* resultType;
1445 if (!determine_binary_type(fContext, Token::EQEQ, ifTrue->fType, ifFalse->fType, &trueType,
Ethan Nicholas2be687a2017-01-03 16:44:39 -05001446 &falseType, &resultType, true) || trueType != falseType) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001447 fErrors.error(expression.fOffset, "ternary operator result mismatch: '" +
1448 ifTrue->fType.fName + "', '" +
1449 ifFalse->fType.fName + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001450 return nullptr;
1451 }
ethannicholasd598f792016-07-25 10:08:54 -07001452 ifTrue = this->coerce(std::move(ifTrue), *trueType);
Ethan Nicholas2be687a2017-01-03 16:44:39 -05001453 if (!ifTrue) {
1454 return nullptr;
1455 }
ethannicholasd598f792016-07-25 10:08:54 -07001456 ifFalse = this->coerce(std::move(ifFalse), *falseType);
Ethan Nicholas2be687a2017-01-03 16:44:39 -05001457 if (!ifFalse) {
1458 return nullptr;
1459 }
ethannicholas08a92112016-11-09 13:26:45 -08001460 if (test->fKind == Expression::kBoolLiteral_Kind) {
1461 // static boolean test, just return one of the branches
1462 if (((BoolLiteral&) *test).fValue) {
1463 return ifTrue;
1464 } else {
1465 return ifFalse;
1466 }
1467 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001468 return std::unique_ptr<Expression>(new TernaryExpression(expression.fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -07001469 std::move(test),
Ethan Nicholas11d53972016-11-28 11:23:23 -05001470 std::move(ifTrue),
ethannicholasb3058bd2016-07-01 08:22:01 -07001471 std::move(ifFalse)));
1472}
1473
Ethan Nicholas5338f992017-04-19 15:54:07 -04001474// scales the texture coordinates by the texture size for sampling rectangle textures.
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001475// For float2coordinates, implements the transformation:
Ethan Nicholas5338f992017-04-19 15:54:07 -04001476// texture(sampler, coord) -> texture(sampler, textureSize(sampler) * coord)
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001477// For float3coordinates, implements the transformation:
1478// texture(sampler, coord) -> texture(sampler, float3textureSize(sampler), 1.0) * coord))
Ethan Nicholas5338f992017-04-19 15:54:07 -04001479void IRGenerator::fixRectSampling(std::vector<std::unique_ptr<Expression>>& arguments) {
1480 ASSERT(arguments.size() == 2);
1481 ASSERT(arguments[0]->fType == *fContext.fSampler2DRect_Type);
1482 ASSERT(arguments[0]->fKind == Expression::kVariableReference_Kind);
1483 const Variable& sampler = ((VariableReference&) *arguments[0]).fVariable;
1484 const Symbol* textureSizeSymbol = (*fSymbolTable)["textureSize"];
1485 ASSERT(textureSizeSymbol->fKind == Symbol::kFunctionDeclaration_Kind);
1486 const FunctionDeclaration& textureSize = (FunctionDeclaration&) *textureSizeSymbol;
1487 std::vector<std::unique_ptr<Expression>> sizeArguments;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001488 sizeArguments.emplace_back(new VariableReference(-1, sampler));
1489 std::unique_ptr<Expression> float2ize = call(-1, textureSize, std::move(sizeArguments));
Ethan Nicholas5338f992017-04-19 15:54:07 -04001490 const Type& type = arguments[1]->fType;
1491 std::unique_ptr<Expression> scale;
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001492 if (type == *fContext.fFloat2_Type) {
1493 scale = std::move(float2ize);
Ethan Nicholas5338f992017-04-19 15:54:07 -04001494 } else {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001495 ASSERT(type == *fContext.fFloat3_Type);
1496 std::vector<std::unique_ptr<Expression>> float3rguments;
1497 float3rguments.push_back(std::move(float2ize));
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001498 float3rguments.emplace_back(new FloatLiteral(fContext, -1, 1.0));
1499 scale.reset(new Constructor(-1, *fContext.fFloat3_Type, std::move(float3rguments)));
Ethan Nicholas5338f992017-04-19 15:54:07 -04001500 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001501 arguments[1].reset(new BinaryExpression(-1, std::move(scale), Token::STAR,
Ethan Nicholas5338f992017-04-19 15:54:07 -04001502 std::move(arguments[1]), type));
1503}
1504
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001505std::unique_ptr<Expression> IRGenerator::call(int offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -05001506 const FunctionDeclaration& function,
ethannicholasd598f792016-07-25 10:08:54 -07001507 std::vector<std::unique_ptr<Expression>> arguments) {
1508 if (function.fParameters.size() != arguments.size()) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001509 String msg = "call to '" + function.fName + "' expected " +
Ethan Nicholas11d53972016-11-28 11:23:23 -05001510 to_string((uint64_t) function.fParameters.size()) +
ethannicholasb3058bd2016-07-01 08:22:01 -07001511 " argument";
ethannicholasd598f792016-07-25 10:08:54 -07001512 if (function.fParameters.size() != 1) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001513 msg += "s";
1514 }
ethannicholas5961bc92016-10-12 06:39:56 -07001515 msg += ", but found " + to_string((uint64_t) arguments.size());
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001516 fErrors.error(offset, msg);
ethannicholasb3058bd2016-07-01 08:22:01 -07001517 return nullptr;
1518 }
ethannicholas471e8942016-10-28 09:02:46 -07001519 std::vector<const Type*> types;
1520 const Type* returnType;
1521 if (!function.determineFinalTypes(arguments, &types, &returnType)) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001522 String msg = "no match for " + function.fName + "(";
1523 String separator;
ethannicholas471e8942016-10-28 09:02:46 -07001524 for (size_t i = 0; i < arguments.size(); i++) {
1525 msg += separator;
1526 separator = ", ";
1527 msg += arguments[i]->fType.description();
1528 }
1529 msg += ")";
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001530 fErrors.error(offset, msg);
ethannicholas471e8942016-10-28 09:02:46 -07001531 return nullptr;
1532 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001533 for (size_t i = 0; i < arguments.size(); i++) {
ethannicholas471e8942016-10-28 09:02:46 -07001534 arguments[i] = this->coerce(std::move(arguments[i]), *types[i]);
ethannicholasea4567c2016-10-17 11:24:37 -07001535 if (!arguments[i]) {
1536 return nullptr;
1537 }
ethannicholasd598f792016-07-25 10:08:54 -07001538 if (arguments[i] && (function.fParameters[i]->fModifiers.fFlags & Modifiers::kOut_Flag)) {
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -04001539 this->setRefKind(*arguments[i],
1540 function.fParameters[i]->fModifiers.fFlags & Modifiers::kIn_Flag ?
1541 VariableReference::kReadWrite_RefKind :
1542 VariableReference::kPointer_RefKind);
ethannicholasb3058bd2016-07-01 08:22:01 -07001543 }
1544 }
Ethan Nicholas5338f992017-04-19 15:54:07 -04001545 if (function.fBuiltin && function.fName == "texture" &&
1546 arguments[0]->fType == *fContext.fSampler2DRect_Type) {
1547 this->fixRectSampling(arguments);
1548 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001549 return std::unique_ptr<FunctionCall>(new FunctionCall(offset, *returnType, function,
ethannicholasb3058bd2016-07-01 08:22:01 -07001550 std::move(arguments)));
1551}
1552
1553/**
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001554 * Determines the cost of coercing the arguments of a function to the required types. Cost has no
1555 * particular meaning other than "lower costs are preferred". Returns INT_MAX if the call is not
1556 * valid.
ethannicholasb3058bd2016-07-01 08:22:01 -07001557 */
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001558int IRGenerator::callCost(const FunctionDeclaration& function,
1559 const std::vector<std::unique_ptr<Expression>>& arguments) {
ethannicholasd598f792016-07-25 10:08:54 -07001560 if (function.fParameters.size() != arguments.size()) {
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001561 return INT_MAX;
ethannicholasb3058bd2016-07-01 08:22:01 -07001562 }
1563 int total = 0;
ethannicholas471e8942016-10-28 09:02:46 -07001564 std::vector<const Type*> types;
1565 const Type* ignored;
1566 if (!function.determineFinalTypes(arguments, &types, &ignored)) {
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001567 return INT_MAX;
ethannicholas471e8942016-10-28 09:02:46 -07001568 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001569 for (size_t i = 0; i < arguments.size(); i++) {
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001570 int cost = arguments[i]->coercionCost(*types[i]);
1571 if (cost != INT_MAX) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001572 total += cost;
1573 } else {
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001574 return INT_MAX;
ethannicholasb3058bd2016-07-01 08:22:01 -07001575 }
1576 }
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001577 return total;
ethannicholasb3058bd2016-07-01 08:22:01 -07001578}
1579
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001580std::unique_ptr<Expression> IRGenerator::call(int offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -05001581 std::unique_ptr<Expression> functionValue,
ethannicholasb3058bd2016-07-01 08:22:01 -07001582 std::vector<std::unique_ptr<Expression>> arguments) {
1583 if (functionValue->fKind == Expression::kTypeReference_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001584 return this->convertConstructor(offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -05001585 ((TypeReference&) *functionValue).fValue,
ethannicholasb3058bd2016-07-01 08:22:01 -07001586 std::move(arguments));
1587 }
1588 if (functionValue->fKind != Expression::kFunctionReference_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001589 fErrors.error(offset, "'" + functionValue->description() + "' is not a function");
ethannicholasb3058bd2016-07-01 08:22:01 -07001590 return nullptr;
1591 }
1592 FunctionReference* ref = (FunctionReference*) functionValue.get();
1593 int bestCost = INT_MAX;
ethannicholasd598f792016-07-25 10:08:54 -07001594 const FunctionDeclaration* best = nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07001595 if (ref->fFunctions.size() > 1) {
1596 for (const auto& f : ref->fFunctions) {
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001597 int cost = this->callCost(*f, arguments);
1598 if (cost < bestCost) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001599 bestCost = cost;
1600 best = f;
1601 }
1602 }
1603 if (best) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001604 return this->call(offset, *best, std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07001605 }
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001606 String msg = "no match for " + ref->fFunctions[0]->fName + "(";
1607 String separator;
ethannicholasb3058bd2016-07-01 08:22:01 -07001608 for (size_t i = 0; i < arguments.size(); i++) {
1609 msg += separator;
1610 separator = ", ";
ethannicholasd598f792016-07-25 10:08:54 -07001611 msg += arguments[i]->fType.description();
ethannicholasb3058bd2016-07-01 08:22:01 -07001612 }
1613 msg += ")";
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001614 fErrors.error(offset, msg);
ethannicholasb3058bd2016-07-01 08:22:01 -07001615 return nullptr;
1616 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001617 return this->call(offset, *ref->fFunctions[0], std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07001618}
1619
Ethan Nicholas84645e32017-02-09 13:57:14 -05001620std::unique_ptr<Expression> IRGenerator::convertNumberConstructor(
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001621 int offset,
Ethan Nicholas11d53972016-11-28 11:23:23 -05001622 const Type& type,
ethannicholasb3058bd2016-07-01 08:22:01 -07001623 std::vector<std::unique_ptr<Expression>> args) {
Ethan Nicholas84645e32017-02-09 13:57:14 -05001624 ASSERT(type.isNumber());
1625 if (args.size() != 1) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001626 fErrors.error(offset, "invalid arguments to '" + type.description() +
1627 "' constructor, (expected exactly 1 argument, but found " +
1628 to_string((uint64_t) args.size()) + ")");
ethannicholasb3058bd2016-07-01 08:22:01 -07001629 return nullptr;
1630 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001631 if (type == args[0]->fType) {
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001632 return std::move(args[0]);
1633 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001634 if (type.isFloat() && args.size() == 1 && args[0]->fKind == Expression::kFloatLiteral_Kind) {
1635 double value = ((FloatLiteral&) *args[0]).fValue;
1636 return std::unique_ptr<Expression>(new FloatLiteral(fContext, offset, value, &type));
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001637 }
1638 if (type.isFloat() && args.size() == 1 && args[0]->fKind == Expression::kIntLiteral_Kind) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001639 int64_t value = ((IntLiteral&) *args[0]).fValue;
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001640 return std::unique_ptr<Expression>(new FloatLiteral(fContext, offset, (double) value,
1641 &type));
ethannicholasb3058bd2016-07-01 08:22:01 -07001642 }
Ethan Nicholas84645e32017-02-09 13:57:14 -05001643 if (args[0]->fKind == Expression::kIntLiteral_Kind && (type == *fContext.fInt_Type ||
1644 type == *fContext.fUInt_Type)) {
1645 return std::unique_ptr<Expression>(new IntLiteral(fContext,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001646 offset,
Ethan Nicholas84645e32017-02-09 13:57:14 -05001647 ((IntLiteral&) *args[0]).fValue,
1648 &type));
ethannicholasb3058bd2016-07-01 08:22:01 -07001649 }
Ethan Nicholas84645e32017-02-09 13:57:14 -05001650 if (args[0]->fType == *fContext.fBool_Type) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001651 std::unique_ptr<IntLiteral> zero(new IntLiteral(fContext, offset, 0));
1652 std::unique_ptr<IntLiteral> one(new IntLiteral(fContext, offset, 1));
Ethan Nicholas84645e32017-02-09 13:57:14 -05001653 return std::unique_ptr<Expression>(
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001654 new TernaryExpression(offset, std::move(args[0]),
Ethan Nicholas84645e32017-02-09 13:57:14 -05001655 this->coerce(std::move(one), type),
1656 this->coerce(std::move(zero),
1657 type)));
1658 }
1659 if (!args[0]->fType.isNumber()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001660 fErrors.error(offset, "invalid argument to '" + type.description() +
1661 "' constructor (expected a number or bool, but found '" +
1662 args[0]->fType.description() + "')");
Ethan Nicholas84645e32017-02-09 13:57:14 -05001663 return nullptr;
1664 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001665 return std::unique_ptr<Expression>(new Constructor(offset, type, std::move(args)));
Ethan Nicholas84645e32017-02-09 13:57:14 -05001666}
1667
1668int component_count(const Type& type) {
1669 switch (type.kind()) {
1670 case Type::kVector_Kind:
1671 return type.columns();
1672 case Type::kMatrix_Kind:
1673 return type.columns() * type.rows();
1674 default:
1675 return 1;
1676 }
1677}
1678
1679std::unique_ptr<Expression> IRGenerator::convertCompoundConstructor(
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001680 int offset,
Ethan Nicholas84645e32017-02-09 13:57:14 -05001681 const Type& type,
1682 std::vector<std::unique_ptr<Expression>> args) {
1683 ASSERT(type.kind() == Type::kVector_Kind || type.kind() == Type::kMatrix_Kind);
1684 if (type.kind() == Type::kMatrix_Kind && args.size() == 1 &&
1685 args[0]->fType.kind() == Type::kMatrix_Kind) {
1686 // matrix from matrix is always legal
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001687 return std::unique_ptr<Expression>(new Constructor(offset, type, std::move(args)));
Ethan Nicholas84645e32017-02-09 13:57:14 -05001688 }
1689 int actual = 0;
1690 int expected = type.rows() * type.columns();
1691 if (args.size() != 1 || expected != component_count(args[0]->fType) ||
1692 type.componentType().isNumber() != args[0]->fType.componentType().isNumber()) {
ethannicholas5961bc92016-10-12 06:39:56 -07001693 for (size_t i = 0; i < args.size(); i++) {
Ethan Nicholas84645e32017-02-09 13:57:14 -05001694 if (args[i]->fType.kind() == Type::kVector_Kind) {
Ethan Nicholas49a36ba2017-02-09 17:04:23 +00001695 if (type.componentType().isNumber() !=
1696 args[i]->fType.componentType().isNumber()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001697 fErrors.error(offset, "'" + args[i]->fType.description() + "' is not a valid "
1698 "parameter to '" + type.description() +
1699 "' constructor");
Ethan Nicholas49a36ba2017-02-09 17:04:23 +00001700 return nullptr;
1701 }
Ethan Nicholas84645e32017-02-09 13:57:14 -05001702 actual += args[i]->fType.columns();
Ethan Nicholas49a36ba2017-02-09 17:04:23 +00001703 } else if (args[i]->fType.kind() == Type::kScalar_Kind) {
1704 actual += 1;
1705 if (type.kind() != Type::kScalar_Kind) {
1706 args[i] = this->coerce(std::move(args[i]), type.componentType());
1707 if (!args[i]) {
1708 return nullptr;
1709 }
1710 }
1711 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001712 fErrors.error(offset, "'" + args[i]->fType.description() + "' is not a valid "
1713 "parameter to '" + type.description() + "' constructor");
Ethan Nicholas49a36ba2017-02-09 17:04:23 +00001714 return nullptr;
1715 }
1716 }
Ethan Nicholas84645e32017-02-09 13:57:14 -05001717 if (actual != 1 && actual != expected) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001718 fErrors.error(offset, "invalid arguments to '" + type.description() +
1719 "' constructor (expected " + to_string(expected) +
1720 " scalars, but found " + to_string(actual) + ")");
Ethan Nicholas49a36ba2017-02-09 17:04:23 +00001721 return nullptr;
1722 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001723 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001724 return std::unique_ptr<Expression>(new Constructor(offset, type, std::move(args)));
ethannicholasb3058bd2016-07-01 08:22:01 -07001725}
1726
Ethan Nicholas84645e32017-02-09 13:57:14 -05001727std::unique_ptr<Expression> IRGenerator::convertConstructor(
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001728 int offset,
Ethan Nicholas84645e32017-02-09 13:57:14 -05001729 const Type& type,
1730 std::vector<std::unique_ptr<Expression>> args) {
1731 // FIXME: add support for structs
1732 Type::Kind kind = type.kind();
1733 if (args.size() == 1 && args[0]->fType == type) {
1734 // argument is already the right type, just return it
1735 return std::move(args[0]);
1736 }
1737 if (type.isNumber()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001738 return this->convertNumberConstructor(offset, type, std::move(args));
Ethan Nicholas84645e32017-02-09 13:57:14 -05001739 } else if (kind == Type::kArray_Kind) {
1740 const Type& base = type.componentType();
1741 for (size_t i = 0; i < args.size(); i++) {
1742 args[i] = this->coerce(std::move(args[i]), base);
1743 if (!args[i]) {
1744 return nullptr;
1745 }
1746 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001747 return std::unique_ptr<Expression>(new Constructor(offset, type, std::move(args)));
Ethan Nicholas84645e32017-02-09 13:57:14 -05001748 } else if (kind == Type::kVector_Kind || kind == Type::kMatrix_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001749 return this->convertCompoundConstructor(offset, type, std::move(args));
Ethan Nicholas84645e32017-02-09 13:57:14 -05001750 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001751 fErrors.error(offset, "cannot construct '" + type.description() + "'");
Ethan Nicholas84645e32017-02-09 13:57:14 -05001752 return nullptr;
1753 }
1754}
1755
ethannicholasb3058bd2016-07-01 08:22:01 -07001756std::unique_ptr<Expression> IRGenerator::convertPrefixExpression(
1757 const ASTPrefixExpression& expression) {
1758 std::unique_ptr<Expression> base = this->convertExpression(*expression.fOperand);
1759 if (!base) {
1760 return nullptr;
1761 }
1762 switch (expression.fOperator) {
1763 case Token::PLUS:
ethannicholasd598f792016-07-25 10:08:54 -07001764 if (!base->fType.isNumber() && base->fType.kind() != Type::kVector_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001765 fErrors.error(expression.fOffset,
ethannicholasd598f792016-07-25 10:08:54 -07001766 "'+' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001767 return nullptr;
1768 }
1769 return base;
1770 case Token::MINUS:
ethannicholasd598f792016-07-25 10:08:54 -07001771 if (!base->fType.isNumber() && base->fType.kind() != Type::kVector_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001772 fErrors.error(expression.fOffset,
ethannicholasd598f792016-07-25 10:08:54 -07001773 "'-' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001774 return nullptr;
1775 }
1776 if (base->fKind == Expression::kIntLiteral_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001777 return std::unique_ptr<Expression>(new IntLiteral(fContext, base->fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -07001778 -((IntLiteral&) *base).fValue));
1779 }
1780 if (base->fKind == Expression::kFloatLiteral_Kind) {
1781 double value = -((FloatLiteral&) *base).fValue;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001782 return std::unique_ptr<Expression>(new FloatLiteral(fContext, base->fOffset,
ethannicholasd598f792016-07-25 10:08:54 -07001783 value));
ethannicholasb3058bd2016-07-01 08:22:01 -07001784 }
1785 return std::unique_ptr<Expression>(new PrefixExpression(Token::MINUS, std::move(base)));
1786 case Token::PLUSPLUS:
ethannicholasd598f792016-07-25 10:08:54 -07001787 if (!base->fType.isNumber()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001788 fErrors.error(expression.fOffset,
1789 String("'") + Compiler::OperatorName(expression.fOperator) +
ethannicholasd598f792016-07-25 10:08:54 -07001790 "' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001791 return nullptr;
1792 }
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -04001793 this->setRefKind(*base, VariableReference::kReadWrite_RefKind);
ethannicholasb3058bd2016-07-01 08:22:01 -07001794 break;
Ethan Nicholas11d53972016-11-28 11:23:23 -05001795 case Token::MINUSMINUS:
ethannicholasd598f792016-07-25 10:08:54 -07001796 if (!base->fType.isNumber()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001797 fErrors.error(expression.fOffset,
1798 String("'") + Compiler::OperatorName(expression.fOperator) +
ethannicholasd598f792016-07-25 10:08:54 -07001799 "' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001800 return nullptr;
1801 }
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -04001802 this->setRefKind(*base, VariableReference::kReadWrite_RefKind);
ethannicholasb3058bd2016-07-01 08:22:01 -07001803 break;
ethannicholas5961bc92016-10-12 06:39:56 -07001804 case Token::LOGICALNOT:
ethannicholasd598f792016-07-25 10:08:54 -07001805 if (base->fType != *fContext.fBool_Type) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001806 fErrors.error(expression.fOffset,
1807 String("'") + Compiler::OperatorName(expression.fOperator) +
ethannicholasd598f792016-07-25 10:08:54 -07001808 "' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001809 return nullptr;
1810 }
ethannicholas08a92112016-11-09 13:26:45 -08001811 if (base->fKind == Expression::kBoolLiteral_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001812 return std::unique_ptr<Expression>(new BoolLiteral(fContext, base->fOffset,
ethannicholas08a92112016-11-09 13:26:45 -08001813 !((BoolLiteral&) *base).fValue));
1814 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001815 break;
ethannicholas5961bc92016-10-12 06:39:56 -07001816 case Token::BITWISENOT:
1817 if (base->fType != *fContext.fInt_Type) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001818 fErrors.error(expression.fOffset,
1819 String("'") + Compiler::OperatorName(expression.fOperator) +
ethannicholas5961bc92016-10-12 06:39:56 -07001820 "' cannot operate on '" + base->fType.description() + "'");
1821 return nullptr;
1822 }
1823 break;
Ethan Nicholas11d53972016-11-28 11:23:23 -05001824 default:
ethannicholasb3058bd2016-07-01 08:22:01 -07001825 ABORT("unsupported prefix operator\n");
1826 }
Ethan Nicholas11d53972016-11-28 11:23:23 -05001827 return std::unique_ptr<Expression>(new PrefixExpression(expression.fOperator,
ethannicholasb3058bd2016-07-01 08:22:01 -07001828 std::move(base)));
1829}
1830
1831std::unique_ptr<Expression> IRGenerator::convertIndex(std::unique_ptr<Expression> base,
1832 const ASTExpression& index) {
Ethan Nicholas50afc172017-02-16 14:49:57 -05001833 if (base->fKind == Expression::kTypeReference_Kind) {
1834 if (index.fKind == ASTExpression::kInt_Kind) {
1835 const Type& oldType = ((TypeReference&) *base).fValue;
1836 int64_t size = ((const ASTIntLiteral&) index).fValue;
1837 Type* newType = new Type(oldType.name() + "[" + to_string(size) + "]",
1838 Type::kArray_Kind, oldType, size);
1839 fSymbolTable->takeOwnership(newType);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001840 return std::unique_ptr<Expression>(new TypeReference(fContext, base->fOffset,
Ethan Nicholas50afc172017-02-16 14:49:57 -05001841 *newType));
1842
1843 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001844 fErrors.error(base->fOffset, "array size must be a constant");
Ethan Nicholas50afc172017-02-16 14:49:57 -05001845 return nullptr;
1846 }
1847 }
ethannicholas5961bc92016-10-12 06:39:56 -07001848 if (base->fType.kind() != Type::kArray_Kind && base->fType.kind() != Type::kMatrix_Kind &&
1849 base->fType.kind() != Type::kVector_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001850 fErrors.error(base->fOffset, "expected array, but found '" + base->fType.description() +
1851 "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001852 return nullptr;
1853 }
1854 std::unique_ptr<Expression> converted = this->convertExpression(index);
1855 if (!converted) {
1856 return nullptr;
1857 }
ethannicholas5961bc92016-10-12 06:39:56 -07001858 if (converted->fType != *fContext.fUInt_Type) {
1859 converted = this->coerce(std::move(converted), *fContext.fInt_Type);
1860 if (!converted) {
1861 return nullptr;
1862 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001863 }
Ethan Nicholas11d53972016-11-28 11:23:23 -05001864 return std::unique_ptr<Expression>(new IndexExpression(fContext, std::move(base),
ethannicholasd598f792016-07-25 10:08:54 -07001865 std::move(converted)));
ethannicholasb3058bd2016-07-01 08:22:01 -07001866}
1867
1868std::unique_ptr<Expression> IRGenerator::convertField(std::unique_ptr<Expression> base,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001869 StringFragment field) {
ethannicholasd598f792016-07-25 10:08:54 -07001870 auto fields = base->fType.fields();
ethannicholasb3058bd2016-07-01 08:22:01 -07001871 for (size_t i = 0; i < fields.size(); i++) {
1872 if (fields[i].fName == field) {
1873 return std::unique_ptr<Expression>(new FieldAccess(std::move(base), (int) i));
1874 }
1875 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001876 fErrors.error(base->fOffset, "type '" + base->fType.description() + "' does not have a "
1877 "field named '" + field + "");
ethannicholasb3058bd2016-07-01 08:22:01 -07001878 return nullptr;
1879}
1880
1881std::unique_ptr<Expression> IRGenerator::convertSwizzle(std::unique_ptr<Expression> base,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001882 StringFragment fields) {
ethannicholasd598f792016-07-25 10:08:54 -07001883 if (base->fType.kind() != Type::kVector_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001884 fErrors.error(base->fOffset, "cannot swizzle type '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001885 return nullptr;
1886 }
1887 std::vector<int> swizzleComponents;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001888 for (size_t i = 0; i < fields.fLength; i++) {
Ethan Nicholas9e1138d2016-11-21 10:39:35 -05001889 switch (fields[i]) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001890 case 'x': // fall through
1891 case 'r': // fall through
Ethan Nicholas11d53972016-11-28 11:23:23 -05001892 case 's':
ethannicholasb3058bd2016-07-01 08:22:01 -07001893 swizzleComponents.push_back(0);
1894 break;
1895 case 'y': // fall through
1896 case 'g': // fall through
1897 case 't':
ethannicholasd598f792016-07-25 10:08:54 -07001898 if (base->fType.columns() >= 2) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001899 swizzleComponents.push_back(1);
1900 break;
1901 }
1902 // fall through
1903 case 'z': // fall through
1904 case 'b': // fall through
Ethan Nicholas11d53972016-11-28 11:23:23 -05001905 case 'p':
ethannicholasd598f792016-07-25 10:08:54 -07001906 if (base->fType.columns() >= 3) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001907 swizzleComponents.push_back(2);
1908 break;
1909 }
1910 // fall through
1911 case 'w': // fall through
1912 case 'a': // fall through
1913 case 'q':
ethannicholasd598f792016-07-25 10:08:54 -07001914 if (base->fType.columns() >= 4) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001915 swizzleComponents.push_back(3);
1916 break;
1917 }
1918 // fall through
1919 default:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001920 fErrors.error(base->fOffset, String::printf("invalid swizzle component '%c'",
1921 fields[i]));
ethannicholasb3058bd2016-07-01 08:22:01 -07001922 return nullptr;
1923 }
1924 }
1925 ASSERT(swizzleComponents.size() > 0);
1926 if (swizzleComponents.size() > 4) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001927 fErrors.error(base->fOffset, "too many components in swizzle mask '" + fields + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001928 return nullptr;
1929 }
ethannicholasd598f792016-07-25 10:08:54 -07001930 return std::unique_ptr<Expression>(new Swizzle(fContext, std::move(base), swizzleComponents));
ethannicholasb3058bd2016-07-01 08:22:01 -07001931}
1932
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001933std::unique_ptr<Expression> IRGenerator::getCap(int offset, String name) {
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001934 auto found = fCapsMap.find(name);
1935 if (found == fCapsMap.end()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001936 fErrors.error(offset, "unknown capability flag '" + name + "'");
Ethan Nicholas3605ace2016-11-21 15:59:48 -05001937 return nullptr;
1938 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001939 String fullName = "sk_Caps." + name;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001940 return std::unique_ptr<Expression>(new Setting(offset, fullName,
1941 found->second.literal(fContext, offset)));
Ethan Nicholas762466e2017-06-29 10:03:38 -04001942}
1943
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001944std::unique_ptr<Expression> IRGenerator::getArg(int offset, String name) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001945 auto found = fSettings->fArgs.find(name);
1946 if (found == fSettings->fArgs.end()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001947 fErrors.error(offset, "unknown argument '" + name + "'");
Ethan Nicholas762466e2017-06-29 10:03:38 -04001948 return nullptr;
Ethan Nicholas3605ace2016-11-21 15:59:48 -05001949 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001950 String fullName = "sk_Args." + name;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001951 return std::unique_ptr<Expression>(new Setting(offset,
Ethan Nicholas762466e2017-06-29 10:03:38 -04001952 fullName,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001953 found->second.literal(fContext, offset)));
Ethan Nicholas3605ace2016-11-21 15:59:48 -05001954}
1955
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001956std::unique_ptr<Expression> IRGenerator::convertTypeField(int offset, const Type& type,
1957 StringFragment field) {
1958 std::unique_ptr<Expression> result;
1959 for (const auto& e : *fProgramElements) {
1960 if (e->fKind == ProgramElement::kEnum_Kind && type.name() == ((Enum&) *e).fTypeName) {
1961 std::shared_ptr<SymbolTable> old = fSymbolTable;
1962 fSymbolTable = ((Enum&) *e).fSymbols;
1963 result = convertIdentifier(ASTIdentifier(offset, field));
1964 fSymbolTable = old;
1965 }
1966 }
1967 if (!result) {
1968 fErrors.error(offset, "type '" + type.fName + "' does not have a field named '" + field +
1969 "'");
1970 }
1971 return result;
1972}
1973
ethannicholasb3058bd2016-07-01 08:22:01 -07001974std::unique_ptr<Expression> IRGenerator::convertSuffixExpression(
1975 const ASTSuffixExpression& expression) {
1976 std::unique_ptr<Expression> base = this->convertExpression(*expression.fBase);
1977 if (!base) {
1978 return nullptr;
1979 }
1980 switch (expression.fSuffix->fKind) {
ethannicholas5961bc92016-10-12 06:39:56 -07001981 case ASTSuffix::kIndex_Kind: {
1982 const ASTExpression* expr = ((ASTIndexSuffix&) *expression.fSuffix).fExpression.get();
1983 if (expr) {
1984 return this->convertIndex(std::move(base), *expr);
1985 } else if (base->fKind == Expression::kTypeReference_Kind) {
1986 const Type& oldType = ((TypeReference&) *base).fValue;
Ethan Nicholas11d53972016-11-28 11:23:23 -05001987 Type* newType = new Type(oldType.name() + "[]", Type::kArray_Kind, oldType,
ethannicholas5961bc92016-10-12 06:39:56 -07001988 -1);
1989 fSymbolTable->takeOwnership(newType);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001990 return std::unique_ptr<Expression>(new TypeReference(fContext, base->fOffset,
ethannicholas5961bc92016-10-12 06:39:56 -07001991 *newType));
1992 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001993 fErrors.error(expression.fOffset, "'[]' must follow a type name");
ethannicholasa54401d2016-10-14 08:37:32 -07001994 return nullptr;
ethannicholas5961bc92016-10-12 06:39:56 -07001995 }
1996 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001997 case ASTSuffix::kCall_Kind: {
1998 auto rawArguments = &((ASTCallSuffix&) *expression.fSuffix).fArguments;
1999 std::vector<std::unique_ptr<Expression>> arguments;
2000 for (size_t i = 0; i < rawArguments->size(); i++) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05002001 std::unique_ptr<Expression> converted =
ethannicholasb3058bd2016-07-01 08:22:01 -07002002 this->convertExpression(*(*rawArguments)[i]);
2003 if (!converted) {
2004 return nullptr;
2005 }
2006 arguments.push_back(std::move(converted));
2007 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002008 return this->call(expression.fOffset, std::move(base), std::move(arguments));
ethannicholasb3058bd2016-07-01 08:22:01 -07002009 }
2010 case ASTSuffix::kField_Kind: {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002011 StringFragment field = ((ASTFieldSuffix&) *expression.fSuffix).fField;
Ethan Nicholas3605ace2016-11-21 15:59:48 -05002012 if (base->fType == *fContext.fSkCaps_Type) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002013 return this->getCap(expression.fOffset, field);
Ethan Nicholas3605ace2016-11-21 15:59:48 -05002014 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04002015 if (base->fType == *fContext.fSkArgs_Type) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002016 return this->getArg(expression.fOffset, field);
2017 }
2018 if (base->fKind == Expression::kTypeReference_Kind) {
2019 return this->convertTypeField(base->fOffset, ((TypeReference&) *base).fValue,
2020 field);
Ethan Nicholas762466e2017-06-29 10:03:38 -04002021 }
ethannicholasd598f792016-07-25 10:08:54 -07002022 switch (base->fType.kind()) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002023 case Type::kVector_Kind:
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002024 return this->convertSwizzle(std::move(base), field);
ethannicholasb3058bd2016-07-01 08:22:01 -07002025 case Type::kStruct_Kind:
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002026 return this->convertField(std::move(base), field);
ethannicholasb3058bd2016-07-01 08:22:01 -07002027 default:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002028 fErrors.error(base->fOffset, "cannot swizzle value of type '" +
2029 base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07002030 return nullptr;
2031 }
2032 }
2033 case ASTSuffix::kPostIncrement_Kind:
ethannicholasd598f792016-07-25 10:08:54 -07002034 if (!base->fType.isNumber()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002035 fErrors.error(expression.fOffset,
ethannicholasd598f792016-07-25 10:08:54 -07002036 "'++' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07002037 return nullptr;
2038 }
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -04002039 this->setRefKind(*base, VariableReference::kReadWrite_RefKind);
Ethan Nicholas11d53972016-11-28 11:23:23 -05002040 return std::unique_ptr<Expression>(new PostfixExpression(std::move(base),
ethannicholasb3058bd2016-07-01 08:22:01 -07002041 Token::PLUSPLUS));
2042 case ASTSuffix::kPostDecrement_Kind:
ethannicholasd598f792016-07-25 10:08:54 -07002043 if (!base->fType.isNumber()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002044 fErrors.error(expression.fOffset,
ethannicholasd598f792016-07-25 10:08:54 -07002045 "'--' cannot operate on '" + base->fType.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07002046 return nullptr;
2047 }
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -04002048 this->setRefKind(*base, VariableReference::kReadWrite_RefKind);
Ethan Nicholas11d53972016-11-28 11:23:23 -05002049 return std::unique_ptr<Expression>(new PostfixExpression(std::move(base),
ethannicholasb3058bd2016-07-01 08:22:01 -07002050 Token::MINUSMINUS));
2051 default:
2052 ABORT("unsupported suffix operator");
2053 }
2054}
2055
2056void IRGenerator::checkValid(const Expression& expr) {
2057 switch (expr.fKind) {
2058 case Expression::kFunctionReference_Kind:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002059 fErrors.error(expr.fOffset, "expected '(' to begin function call");
ethannicholasb3058bd2016-07-01 08:22:01 -07002060 break;
2061 case Expression::kTypeReference_Kind:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002062 fErrors.error(expr.fOffset, "expected '(' to begin constructor invocation");
ethannicholasb3058bd2016-07-01 08:22:01 -07002063 break;
2064 default:
ethannicholasea4567c2016-10-17 11:24:37 -07002065 if (expr.fType == *fContext.fInvalid_Type) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002066 fErrors.error(expr.fOffset, "invalid expression");
ethannicholasea4567c2016-10-17 11:24:37 -07002067 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002068 }
2069}
2070
ethannicholasb3058bd2016-07-01 08:22:01 -07002071static bool has_duplicates(const Swizzle& swizzle) {
2072 int bits = 0;
2073 for (int idx : swizzle.fComponents) {
2074 ASSERT(idx >= 0 && idx <= 3);
2075 int bit = 1 << idx;
2076 if (bits & bit) {
2077 return true;
2078 }
2079 bits |= bit;
2080 }
2081 return false;
2082}
2083
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -04002084void IRGenerator::setRefKind(const Expression& expr, VariableReference::RefKind kind) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002085 switch (expr.fKind) {
2086 case Expression::kVariableReference_Kind: {
ethannicholasd598f792016-07-25 10:08:54 -07002087 const Variable& var = ((VariableReference&) expr).fVariable;
ethannicholasb3058bd2016-07-01 08:22:01 -07002088 if (var.fModifiers.fFlags & (Modifiers::kConst_Flag | Modifiers::kUniform_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002089 fErrors.error(expr.fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -07002090 "cannot modify immutable variable '" + var.fName + "'");
2091 }
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -04002092 ((VariableReference&) expr).setRefKind(kind);
ethannicholasb3058bd2016-07-01 08:22:01 -07002093 break;
2094 }
2095 case Expression::kFieldAccess_Kind:
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -04002096 this->setRefKind(*((FieldAccess&) expr).fBase, kind);
ethannicholasb3058bd2016-07-01 08:22:01 -07002097 break;
2098 case Expression::kSwizzle_Kind:
2099 if (has_duplicates((Swizzle&) expr)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002100 fErrors.error(expr.fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -07002101 "cannot write to the same swizzle field more than once");
2102 }
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -04002103 this->setRefKind(*((Swizzle&) expr).fBase, kind);
ethannicholasb3058bd2016-07-01 08:22:01 -07002104 break;
2105 case Expression::kIndex_Kind:
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -04002106 this->setRefKind(*((IndexExpression&) expr).fBase, kind);
ethannicholasb3058bd2016-07-01 08:22:01 -07002107 break;
Ethan Nicholasa583b812018-01-18 13:32:11 -05002108 case Expression::kTernary_Kind: {
2109 TernaryExpression& t = (TernaryExpression&) expr;
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -04002110 this->setRefKind(*t.fIfTrue, kind);
2111 this->setRefKind(*t.fIfFalse, kind);
Ethan Nicholasa583b812018-01-18 13:32:11 -05002112 break;
2113 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002114 default:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002115 fErrors.error(expr.fOffset, "cannot assign to '" + expr.description() + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07002116 break;
2117 }
2118}
2119
Robert Phillipsfe8da172018-01-24 14:52:02 +00002120void IRGenerator::convertProgram(Program::Kind kind,
2121 const char* text,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002122 size_t length,
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002123 SymbolTable& types,
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002124 std::vector<std::unique_ptr<ProgramElement>>* out) {
Robert Phillipsfe8da172018-01-24 14:52:02 +00002125 fKind = kind;
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002126 fProgramElements = out;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002127 Parser parser(text, length, types, fErrors);
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002128 std::vector<std::unique_ptr<ASTDeclaration>> parsed = parser.file();
2129 if (fErrors.errorCount()) {
2130 return;
2131 }
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002132 for (size_t i = 0; i < parsed.size(); i++) {
2133 ASTDeclaration& decl = *parsed[i];
2134 switch (decl.fKind) {
2135 case ASTDeclaration::kVar_Kind: {
2136 std::unique_ptr<VarDeclarations> s = this->convertVarDeclarations(
2137 (ASTVarDeclarations&) decl,
2138 Variable::kGlobal_Storage);
2139 if (s) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002140 fProgramElements->push_back(std::move(s));
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002141 }
2142 break;
2143 }
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002144 case ASTDeclaration::kEnum_Kind: {
2145 this->convertEnum((ASTEnum&) decl);
2146 break;
2147 }
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002148 case ASTDeclaration::kFunction_Kind: {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002149 this->convertFunction((ASTFunction&) decl);
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002150 break;
2151 }
2152 case ASTDeclaration::kModifiers_Kind: {
2153 std::unique_ptr<ModifiersDeclaration> f = this->convertModifiersDeclaration(
2154 (ASTModifiersDeclaration&) decl);
2155 if (f) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002156 fProgramElements->push_back(std::move(f));
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002157 }
2158 break;
2159 }
2160 case ASTDeclaration::kInterfaceBlock_Kind: {
2161 std::unique_ptr<InterfaceBlock> i = this->convertInterfaceBlock(
2162 (ASTInterfaceBlock&) decl);
2163 if (i) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002164 fProgramElements->push_back(std::move(i));
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002165 }
2166 break;
2167 }
2168 case ASTDeclaration::kExtension_Kind: {
2169 std::unique_ptr<Extension> e = this->convertExtension((ASTExtension&) decl);
2170 if (e) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002171 fProgramElements->push_back(std::move(e));
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002172 }
2173 break;
2174 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04002175 case ASTDeclaration::kSection_Kind: {
2176 std::unique_ptr<Section> s = this->convertSection((ASTSection&) decl);
2177 if (s) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05002178 fProgramElements->push_back(std::move(s));
Ethan Nicholas762466e2017-06-29 10:03:38 -04002179 }
2180 break;
2181 }
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04002182 default:
2183 ABORT("unsupported declaration: %s\n", decl.description().c_str());
2184 }
2185 }
2186}
2187
2188
ethannicholasb3058bd2016-07-01 08:22:01 -07002189}