Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(162)

Side by Side Diff: src/sksl/SkSLIRGenerator.cpp

Issue 1984363002: initial checkin of SkSL compiler (Closed) Base URL: https://skia.googlesource.com/skia@master
Patch Set: more cleanups Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "SkSLIRGenerator.h"
9
10 #include "limits.h"
11
12 #include "ast/SkSLASTBoolLiteral.h"
13 #include "ast/SkSLASTFieldSuffix.h"
14 #include "ast/SkSLASTFloatLiteral.h"
15 #include "ast/SkSLASTIndexSuffix.h"
16 #include "ast/SkSLASTIntLiteral.h"
17 #include "ir/SkSLBinaryExpression.h"
18 #include "ir/SkSLBoolLiteral.h"
19 #include "ir/SkSLBreakStatement.h"
20 #include "ir/SkSLConstructor.h"
21 #include "ir/SkSLContinueStatement.h"
22 #include "ir/SkSLDiscardStatement.h"
23 #include "ir/SkSLDoStatement.h"
24 #include "ir/SkSLExpressionStatement.h"
25 #include "ir/SkSLField.h"
26 #include "ir/SkSLFieldAccess.h"
27 #include "ir/SkSLFloatLiteral.h"
28 #include "ir/SkSLForStatement.h"
29 #include "ir/SkSLFunctionCall.h"
30 #include "ir/SkSLFunctionDeclaration.h"
31 #include "ir/SkSLFunctionDefinition.h"
32 #include "ir/SkSLFunctionReference.h"
33 #include "ir/SkSLIfStatement.h"
34 #include "ir/SkSLIndexExpression.h"
35 #include "ir/SkSLInterfaceBlock.h"
36 #include "ir/SkSLIntLiteral.h"
37 #include "ir/SkSLLayout.h"
38 #include "ir/SkSLPostfixExpression.h"
39 #include "ir/SkSLPrefixExpression.h"
40 #include "ir/SkSLReturnStatement.h"
41 #include "ir/SkSLSwizzle.h"
42 #include "ir/SkSLTernaryExpression.h"
43 #include "ir/SkSLUnresolvedFunction.h"
44 #include "ir/SkSLVariable.h"
45 #include "ir/SkSLVarDeclaration.h"
46 #include "ir/SkSLVarDeclarationStatement.h"
47 #include "ir/SkSLVariableReference.h"
48 #include "ir/SkSLWhileStatement.h"
49
50 namespace SkSL {
51
52 class AutoSymbolTable {
53 public:
54 AutoSymbolTable(IRGenerator* ir)
55 : fIR(ir)
56 , fPrevious(fIR->fSymbolTable) {
57 fIR->pushSymbolTable();
58 }
59
60 ~AutoSymbolTable() {
61 fIR->popSymbolTable();
62 ASSERT(fPrevious == fIR->fSymbolTable);
63 }
64
65 IRGenerator* fIR;
66 std::shared_ptr<SymbolTable> fPrevious;
67 };
68
69 IRGenerator::IRGenerator(std::shared_ptr<SymbolTable> symbolTable, ErrorReporter & errorReporter)
70 : fSymbolTable(symbolTable)
dogben 2016/06/23 15:25:10 nit: std::move
71 , fErrors(errorReporter) {
72 }
73
74 void IRGenerator::pushSymbolTable() {
75 fSymbolTable.reset(new SymbolTable(fSymbolTable, fErrors));
dogben 2016/06/23 15:25:08 nit: std::move
76 }
77
78 void IRGenerator::popSymbolTable() {
79 fSymbolTable = fSymbolTable->fParent;
80 }
81
82 std::unique_ptr<Extension> IRGenerator::convertExtension(ASTExtension& extension ) {
83 return std::unique_ptr<Extension>(new Extension(extension.fPosition, extensi on.fName));
84 }
85
86 std::unique_ptr<Statement> IRGenerator::convertStatement(ASTStatement& statement ) {
87 switch (statement.fKind) {
88 case ASTStatement::kBlock_Kind:
89 return this->convertBlock((ASTBlock&) statement);
90 case ASTStatement::kVarDeclaration_Kind:
91 return this->convertVarDeclarationStatement((ASTVarDeclarationStatem ent&) statement);
92 case ASTStatement::kExpression_Kind:
93 return this->convertExpressionStatement((ASTExpressionStatement&) st atement);
94 case ASTStatement:: kIf_Kind:
dogben 2016/06/23 15:25:10 nit: extra space
95 return this->convertIf((ASTIfStatement&) statement);
96 case ASTStatement::kFor_Kind:
97 return this->convertFor((ASTForStatement&) statement);
98 case ASTStatement::kWhile_Kind:
99 return this->convertWhile((ASTWhileStatement&) statement);
100 case ASTStatement::kDo_Kind:
101 return this->convertDo((ASTDoStatement&) statement);
102 case ASTStatement::kReturn_Kind:
103 return this->convertReturn((ASTReturnStatement&) statement);
104 case ASTStatement::kBreak_Kind:
105 return this->convertBreak((ASTBreakStatement&) statement);
106 case ASTStatement::kContinue_Kind:
107 return this->convertContinue((ASTContinueStatement&) statement);
108 case ASTStatement::kDiscard_Kind:
109 return this->convertDiscard((ASTDiscardStatement&) statement);
110 default:
dogben 2016/06/23 15:25:08 nit: Do we compile with the warning enabled when n
ethannicholas 2016/06/24 21:23:08 Without the 'default:' it works on most platforms,
111 ABORT("unsupported statement type: %d\n", statement.fKind);
112 }
113 }
114
115 std::unique_ptr<Block> IRGenerator::convertBlock(ASTBlock& block) {
116 AutoSymbolTable table(this);
117 std::vector<std::unique_ptr<Statement>> statements;
118 for (size_t i = 0; i < block.fStatements.size(); i++) {
119 std::unique_ptr<Statement> statement = this->convertStatement(*block.fSt atements[i]);
120 if (statement == nullptr) {
121 return nullptr;
122 }
123 statements.push_back(std::move(statement));
124 }
125 return std::unique_ptr<Block>(new Block(block.fPosition, std::move(statement s)));
126 }
127
128 std::unique_ptr<Statement> IRGenerator::convertVarDeclarationStatement(
129 ASTVarDeclar ationStatement& s) {
dogben 2016/06/23 15:25:09 nit: odd indentation
130 auto decl = this->convertVarDeclaration(*s.fDeclaration, Variable::kLocal_St orage);
131 if (decl == nullptr) {
132 return nullptr;
133 }
134 return std::unique_ptr<Statement>(new VarDeclarationStatement(std::move(decl )));
135 }
136
137 Modifiers IRGenerator::convertModifiers(const ASTModifiers& modifiers) {
138 return Modifiers(modifiers);
dogben 2016/06/23 15:25:08 Shouldn't we check that modifiers don't conflict?
ethannicholas 2016/06/24 21:23:08 It's on my list. This is just the initial checkin;
139 }
140
141 std::unique_ptr<VarDeclaration> IRGenerator::convertVarDeclaration(ASTVarDeclara tion& decl,
142 Variable::Sto rage storage) {
143 std::vector<std::shared_ptr<Variable>> variables;
144 std::vector<std::vector<std::unique_ptr<Expression>>> sizes;
145 std::vector<std::unique_ptr<Expression>> values;
146 std::shared_ptr<Type> baseType = this->convertType(*decl.fType);
147 if (baseType == nullptr) {
148 return nullptr;
149 }
150 for (size_t i = 0; i < decl.fNames.size(); i++) {
151 Modifiers modifiers = this->convertModifiers(decl.fModifiers);
dogben 2016/06/23 15:25:09 nit: could be moved outside the loop?
152 std::shared_ptr<Type> type = baseType;
153 std::vector<std::unique_ptr<Expression>> currentVarSizes;
dogben 2016/06/23 15:25:08 If baseType is an array type, should the sizes be
ethannicholas 2016/06/24 21:23:08 baseType can't be an array type, since it's the si
dogben 2016/06/26 03:57:52 The GLSL spec contains these example var declarati
ethannicholas 2016/06/27 20:35:03 Skia (and every shader I have discovered in the wi
154 for (size_t j = 0; j < decl.fSizes[i].size(); j++) {
155 if (decl.fSizes[i][j] != nullptr) {
156 ASTExpression& rawSize = *decl.fSizes[i][j];
157 std::unique_ptr<Expression> size = this->convertExpression(rawSi ze);
158 if (size == nullptr) {
159 return nullptr;
160 }
161 std::string name = type->fName;
162 uint64_t count;
163 if (size->fKind == Expression::kIntLiteral_Kind) {
164 count = ((IntLiteral&) *size).fValue;
dogben 2016/06/23 15:25:11 Check that count is positive?
165 name += "[" + to_string(count) + "]";
166 } else {
167 count = -1;
dogben 2016/06/23 15:25:10 Check that size's type is integer?
ethannicholas 2016/06/24 21:23:09 Done.
168 name += "[]";
169 }
170 type = std::shared_ptr<Type>(new Type(name, Type::kArray_Kind, t ype, (int) count));
171 currentVarSizes.push_back(std::move(size));
172 } else {
173 currentVarSizes.push_back(nullptr);
dogben 2016/06/23 15:25:10 Don't we need to update type here also?
174 }
175 }
176 sizes.push_back(std::move(currentVarSizes));
177 auto var = std::shared_ptr<Variable>(new Variable(decl.fPosition, modifi ers,
dogben 2016/06/23 15:25:10 nit: std::make_shared
178 decl.fNames[i], type, storage));
179 variables.push_back(var);
180 std::unique_ptr<Expression> value;
181 if (decl.fValues[i] != nullptr) {
182 value = this->convertExpression(*decl.fValues[i]);
183 if (value == nullptr) {
184 return nullptr;
185 }
186 value = this->coerce(std::move(value), type);
187 } else {
188 value = nullptr;
dogben 2016/06/23 15:25:09 nit: redundant
189 }
190 values.push_back(std::move(value));
191 fSymbolTable->add(var->fName, var);
dogben 2016/06/23 15:25:10 nit: move this line before variables.push_back?
192 }
193 return std::unique_ptr<VarDeclaration>(new VarDeclaration(decl.fPosition, va riables,
dogben 2016/06/23 15:25:11 nit: std::move(variables)
194 std::move(sizes), std::move(values)));
195 }
196
197 std::unique_ptr<Statement> IRGenerator::convertIf(ASTIfStatement& s) {
198 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*s.f Test), kBool_Type);
199 if (test == nullptr) {
200 return nullptr;
201 }
202 std::unique_ptr<Statement> ifTrue = this->convertStatement(*s.fIfTrue);
203 if (ifTrue == nullptr) {
204 return nullptr;
205 }
206 std::unique_ptr<Statement> ifFalse;
207 if (s.fIfFalse != nullptr) {
208 ifFalse = this->convertStatement(*s.fIfFalse);
209 if (ifFalse == nullptr) {
210 return nullptr;
211 }
212 }
213 return std::unique_ptr<Statement>(new IfStatement(s.fPosition, std::move(tes t),
214 std::move(ifTrue), std::mo ve(ifFalse)));
215 }
216
217 std::unique_ptr<Statement> IRGenerator::convertFor(ASTForStatement& f) {
218 AutoSymbolTable table(this);
219 std::unique_ptr<Statement> initializer = this->convertStatement(*f.fInitiali zer);
220 if (initializer == nullptr) {
221 return nullptr;
222 }
223 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*f.f Test), kBool_Type);
224 if (test == nullptr) {
225 return nullptr;
226 }
227 std::unique_ptr<Expression> next = this->convertExpression(*f.fNext);
dogben 2016/06/23 15:25:10 Check that next is not FunctionReference or TypeRe
228 if (next == nullptr) {
229 return nullptr;
230 }
231 std::unique_ptr<Statement> statement = this->convertStatement(*f.fStatement) ;
232 if (statement == nullptr) {
233 return nullptr;
234 }
235 return std::unique_ptr<Statement>(new ForStatement(f.fPosition, std::move(in itializer),
236 std::move(test), std::mov e(next),
237 std::move(statement)));
238 }
239
240 std::unique_ptr<Statement> IRGenerator::convertWhile(ASTWhileStatement& w) {
241 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*w.f Test), kBool_Type);
242 if (test == nullptr) {
243 return nullptr;
244 }
245 std::unique_ptr<Statement> statement = this->convertStatement(*w.fStatement) ;
246 if (statement == nullptr) {
247 return nullptr;
248 }
249 return std::unique_ptr<Statement>(new WhileStatement(w.fPosition, std::move( test),
250 std::move(statement)));
251 }
252
253 std::unique_ptr<Statement> IRGenerator::convertDo(ASTDoStatement& d) {
254 std::unique_ptr<Expression> test = this->convertExpression(*d.fTest);
dogben 2016/06/23 15:25:10 Other tests are coerced. Why not this one?
ethannicholas 2016/06/24 21:23:08 Oversight. Fixed.
255 if (test == nullptr) {
256 return nullptr;
257 }
258 if (test->fType != kBool_Type) {
259 fErrors.error(d.fPosition, "expected 'bool', but found '" +
260 test->fType->description() + "'");
261 return nullptr;
262 }
263 std::unique_ptr<Statement> statement = this->convertStatement(*d.fStatement) ;
264 if (statement == nullptr) {
265 return nullptr;
266 }
267 return std::unique_ptr<Statement>(new DoStatement(d.fPosition, std::move(sta tement),
268 std::move(test)));
269 }
270
271 std::unique_ptr<Statement> IRGenerator::convertExpressionStatement(ASTExpression Statement& s) {
272 std::unique_ptr<Expression> e = this->convertExpression(*s.fExpression);
273 if (e == nullptr) {
274 return nullptr;
275 }
276 return std::unique_ptr<Statement>(new ExpressionStatement(std::move(e)));
dogben 2016/06/23 15:25:11 Check that e is not FunctionReference or TypeRefer
ethannicholas 2016/06/24 21:23:09 Done.
277 }
278
279 std::unique_ptr<Statement> IRGenerator::convertReturn(ASTReturnStatement& r) {
280 if (r.fExpression) {
281 std::unique_ptr<Expression> result = this->convertExpression(*r.fExpress ion);
282 if (result == nullptr) {
283 return nullptr;
284 }
285 ASSERT(fCurrentFunction);
dogben 2016/06/23 15:25:08 nit: Move outside of if?
286 if (fCurrentFunction->fReturnType == kVoid_Type) {
287 fErrors.error(result->fPosition, "may not return a value from a void function");
288 } else {
289 result = this->coerce(std::move(result), fCurrentFunction->fReturnTy pe);
290 if (result == nullptr) {
291 return nullptr;
292 }
293 }
294 return std::unique_ptr<Statement>(new ReturnStatement(std::move(result)) );
295 } else {
296 if (fCurrentFunction->fReturnType != kVoid_Type) {
297 fErrors.error(r.fPosition, "expected function to return '" +
298 fCurrentFunction->fReturnType->descriptio n() + "'");
299 }
300 return std::unique_ptr<Statement>(new ReturnStatement(r.fPosition));
301 }
302 }
303
304 std::unique_ptr<Statement> IRGenerator::convertBreak(ASTBreakStatement& b) {
305 return std::unique_ptr<Statement>(new BreakStatement(b.fPosition));
306 }
307
308 std::unique_ptr<Statement> IRGenerator::convertContinue(ASTContinueStatement& c) {
309 return std::unique_ptr<Statement>(new ContinueStatement(c.fPosition));
310 }
311
312 std::unique_ptr<Statement> IRGenerator::convertDiscard(ASTDiscardStatement& d) {
313 return std::unique_ptr<Statement>(new DiscardStatement(d.fPosition));
314 }
315
316 static std::shared_ptr<Type> expand_generics(std::shared_ptr<Type> type, int i) {
dogben 2016/06/23 15:25:09 nit: This function should probably accept and retu
ethannicholas 2016/06/24 21:23:09 I don't think it can. If it returns Type*, then I
317 if (type->kind() == Type::kGeneric_Kind) {
318 return type->coercibleTypes()[i];
319 }
320 return type;
321 }
322
323 static void expand_generics(std::shared_ptr<FunctionDeclaration> decl,
dogben 2016/06/23 15:25:09 nit: This function is not taking ownership of its
324 std::shared_ptr<SymbolTable> symbolTable) {
325 for (int i = 0; i < 4; i++) {
326 std::shared_ptr<Type> returnType = expand_generics(decl->fReturnType, i) ;
327 std::vector<std::shared_ptr<Variable>> parameters;
328 for (auto p : decl->fParameters) {
dogben 2016/06/23 15:25:11 nit: const ref
329 parameters.push_back(std::shared_ptr<Variable>(new Variable(decl->fP osition,
dogben 2016/06/23 15:25:08 Should this be p->fPosition?
330 Modifier s(p->fModifiers),
331 p->fName ,
332 expand_g enerics(p->fType,
333 i),
334 Variable::kP arameter_Storage)));
dogben 2016/06/23 15:25:10 nit: odd indentation
335 }
336 std::shared_ptr<FunctionDeclaration> expanded(new FunctionDeclaration(de cl->fPosition,
337 de cl->fName,
338 pa rameters,
dogben 2016/06/23 15:25:11 nit: std::move
339 re turnType));
dogben 2016/06/23 15:25:10 nit: std::move
340 symbolTable->add(expanded->fName, expanded);
341 }
342 }
343
344 std::unique_ptr<FunctionDefinition> IRGenerator::convertFunction(ASTFunction& f) {
345 std::shared_ptr<SymbolTable> old = fSymbolTable;
346 AutoSymbolTable table(this);
347 bool isGeneric;
348 std::shared_ptr<Type> returnType = this->convertType(*f.fReturnType);
349 if (returnType == nullptr) {
350 return nullptr;
351 }
352 isGeneric = returnType->kind() == Type::kGeneric_Kind;
dogben 2016/06/23 15:25:11 nit: declare and initialize in one statement.
353 std::vector<std::shared_ptr<Variable>> parameters;
354 for (size_t i = 0; i < f.fParameters.size(); i++) {
dogben 2016/06/23 15:25:08 nit: could use range-based for
355 std::shared_ptr<Type> type = this->convertType(*f.fParameters[i]->fType) ;
356 if (type == nullptr) {
357 return nullptr;
358 }
359 for (int j = (int) f.fParameters[i]->fSizes.size() - 1; j >= 0; j--) {
360 int size = f.fParameters[i]->fSizes[j];
361 type = std::shared_ptr<Type>(new Type(type->name() + "[" + to_string (size) + "]",
362 Type::kArray_Kind, type, size) );
dogben 2016/06/23 15:25:09 nit: std::move(type)
363 }
364 std::string name = f.fParameters[i]->fName;
365 Modifiers modifiers = this->convertModifiers(f.fParameters[i]->fModifier s);
366 Position pos = f.fParameters[i]->fPosition;
367 std::shared_ptr<Variable> var = std::shared_ptr<Variable>(new Variable(p os,
368 m odifiers,
369 n ame,
dogben 2016/06/23 15:25:09 nit: std::move
370 t ype,
dogben 2016/06/23 15:25:10 nit: std::move
371 Variable::k Parameter_Storage));
dogben 2016/06/23 15:25:11 nit: odd indentation
372 parameters.push_back(var);
dogben 2016/06/23 15:25:10 nit: std::move
373 isGeneric |= type->kind() == Type::kGeneric_Kind;
374 }
375
376 // find existing declaration
377 std::shared_ptr<FunctionDeclaration> decl;
378 auto entry = (*old)[f.fName];
379 if (entry) {
380 std::vector<std::shared_ptr<FunctionDeclaration>> functions;
381 switch (entry->fKind) {
382 case Symbol::kUnresolvedFunction_Kind:
383 functions = std::static_pointer_cast<UnresolvedFunction>(entry)- >fFunctions;
384 break;
385 case Symbol::kFunctionDeclaration_Kind:
386 functions.push_back(std::static_pointer_cast<FunctionDeclaration >(entry));
387 break;
388 default:
389 fErrors.error(f.fPosition, "symbol '" + f.fName + "' was already defined");
390 return nullptr;
391 }
392 for (auto other : functions) {
dogben 2016/06/23 15:25:09 nit: const ref
393 ASSERT(other->fName == f.fName);
394 if (parameters.size() == other->fParameters.size()) {
395 bool match = true;
396 for (size_t i = 0; i < parameters.size(); i++) {
397 if (parameters[i]->fType != other->fParameters[i]->fType) {
398 match = false;
399 break;
400 }
401 }
402 if (match) {
403 if (returnType != other->fReturnType) {
404 FunctionDeclaration newDecl = FunctionDeclaration(f.fPos ition,
405 f.fNam e,
406 parame ters,
407 return Type);
408 fErrors.error(f.fPosition, "functions '" + newDecl.descr iption() +
409 "' and '" + other->descriptio n() +
410 "' differ only in return type ");
411 return nullptr;
412 }
413 if (other->fDefined) {
414 fErrors.error(f.fPosition, "duplicate definition of " +
415 other->description());
416 }
417 decl = other;
418 for (size_t i = 0; i < parameters.size(); i++) {
419 fSymbolTable->add(parameters[i]->fName, decl->fParameter s[i]);
dogben 2016/06/23 15:25:11 Are modifiers allowed to differ between declaratio
ethannicholas 2016/06/24 21:23:09 Good catch. No, they should not, I will add an err
420 }
421 break;
422 }
423 }
424 }
425 }
426 if (!decl) {
427 // couldn't find an existing declaration
428 decl.reset(new FunctionDeclaration(f.fPosition, f.fName, parameters, ret urnType));
429 for (auto var : parameters) {
dogben 2016/06/23 15:25:09 nit: const ref
430 fSymbolTable->add(var->fName, var);
431 }
432 }
433 decl->fDefined = true;
434 if (isGeneric) {
435 ASSERT(f.fBody == nullptr);
436 expand_generics(decl, old);
437 } else {
438 old->add(decl->fName, decl);
439 if (f.fBody != nullptr) {
440 ASSERT(fCurrentFunction == nullptr);
441 fCurrentFunction = decl;
442 std::unique_ptr<Block> body = this->convertBlock(*f.fBody);
dogben 2016/06/23 15:25:09 Seems like a very minor issue, but the spec says "
ethannicholas 2016/06/24 21:23:09 I might end up changing this, but in general it is
443 fCurrentFunction = nullptr;
444 if (body == nullptr) {
445 return nullptr;
446 }
447 return std::unique_ptr<FunctionDefinition>(new FunctionDefinition(f. fPosition, decl,
dogben 2016/06/24 17:05:26 Check for return at end of body? (Maybe easier to
ethannicholas 2016/06/24 21:23:09 You need control flow analysis to prove that a fun
448 st d::move(body)));
449 }
450 }
451 return nullptr;
452 }
453
454 std::unique_ptr<InterfaceBlock> IRGenerator::convertInterfaceBlock(ASTInterfaceB lock& intf) {
455 std::shared_ptr<SymbolTable> old = fSymbolTable;
456 AutoSymbolTable table(this);
457 Modifiers mods = this->convertModifiers(intf.fModifiers);
458 std::vector<Type::Field> fields;
459 for (size_t i = 0; i < intf.fDeclarations.size(); i++) {
460 std::unique_ptr<VarDeclaration> decl = this->convertVarDeclaration(*intf .fDeclarations[i],
461 Variable::kGloba l_Storage);
dogben 2016/06/23 15:25:09 nit: odd indentation
462 for (size_t j = 0; j < decl->fVars.size(); j++) {
463 fields.push_back(Type::Field(decl->fVars[j]->fModifiers, decl->fVars [j]->fName,
dogben 2016/06/23 15:25:08 Shouldn't we OR in mods.fFlags? And check that the
ethannicholas 2016/06/24 21:23:08 There are probably some illegal combinations we sh
464 decl->fVars[j]->fType));
465 if (decl->fValues[j] != nullptr) {
466 fErrors.error(decl->fPosition, "initializers are not permitted o n interface block "
467 "fields");
468 }
469 }
470 }
471 std::shared_ptr<Type> type = std::shared_ptr<Type>(new Type(intf.fInterfaceN ame, fields));
472 std::string name = intf.fValueName.length() > 0 ? intf.fValueName : intf.fIn terfaceName;
473 std::shared_ptr<Variable> var = std::shared_ptr<Variable>(new Variable(intf. fPosition, mods,
474 name, type,
475 Variable::kGloba l_Storage));
dogben 2016/06/23 15:25:11 nit: odd indentation
476 if (intf.fValueName.length()) {
477 old->add(intf.fValueName, var);
478
479 } else {
480 for (size_t i = 0; i < fields.size(); i++) {
481 std::shared_ptr<Field> field = std::shared_ptr<Field>(new Field(intf .fPosition, var,
482 (int ) i));
483 old->add(fields[i].fName, field);
484 }
485 }
486 return std::unique_ptr<InterfaceBlock>(new InterfaceBlock(intf.fPosition, va r));
487 }
488
489 std::shared_ptr<Type> IRGenerator::convertType(ASTType& type) {
490 std::shared_ptr<Symbol> result = (*fSymbolTable)[type.fName];
491 if (result != nullptr && result->fKind == Symbol::kType_Kind) {
492 return std::static_pointer_cast<Type>(result);
493 }
494 fErrors.error(type.fPosition, "unknown type '" + type.fName + "'");
495 return nullptr;
496 }
497
498 std::unique_ptr<Expression> IRGenerator::convertExpression(ASTExpression& expr) {
499 switch (expr.fKind) {
500 case ASTExpression::kIdentifier_Kind:
501 return this->convertIdentifier((ASTIdentifier&) expr);
502 case ASTExpression::kBool_Kind:
503 return std::unique_ptr<Expression>(new BoolLiteral(expr.fPosition,
504 ((ASTBoolLiteral& ) expr).fValue));
505 case ASTExpression::kInt_Kind:
506 return std::unique_ptr<Expression>(new IntLiteral(expr.fPosition,
507 ((ASTIntLiteral&) expr).fValue));
508 case ASTExpression::kFloat_Kind:
509 return std::unique_ptr<Expression>(new FloatLiteral(expr.fPosition,
510 ((ASTFloatLitera l&) expr).fValue));
511 case ASTExpression::kBinary_Kind:
512 return this->convertBinaryExpression((ASTBinaryExpression&) expr);
513 case ASTExpression::kPrefix_Kind:
514 return this->convertPrefixExpression((ASTPrefixExpression&) expr);
515 case ASTExpression::kSuffix_Kind:
516 return this->convertSuffixExpression((ASTSuffixExpression&) expr);
517 case ASTExpression::kTernary_Kind:
518 return this->convertTernaryExpression((ASTTernaryExpression&) expr);
519 default:
520 ABORT("unsupported expression type: %d\n", expr.fKind);
521 }
522 }
523
524 std::unique_ptr<Expression> IRGenerator::convertIdentifier(ASTIdentifier& identi fier) {
525 std::shared_ptr<Symbol> result = (*fSymbolTable)[identifier.fText];
526 if (result == nullptr) {
527 fErrors.error(identifier.fPosition, "unknown identifier '" + identifier. fText + "'");
528 return nullptr;
529 }
530 switch (result->fKind) {
531 case Symbol::kFunctionDeclaration_Kind: {
532 std::vector<std::shared_ptr<FunctionDeclaration>> f;
533 f.push_back(std::static_pointer_cast<FunctionDeclaration>(result));
534 return std::unique_ptr<FunctionReference>(new FunctionReference(iden tifier.fPosition,
535 f));
dogben 2016/06/23 15:25:11 nit: std::move
536 }
537 case Symbol::kUnresolvedFunction_Kind: {
538 auto f = std::static_pointer_cast<UnresolvedFunction>(result);
539 return std::unique_ptr<FunctionReference>(new FunctionReference(iden tifier.fPosition,
540 f->f Functions));
541 }
542 case Symbol::kVariable_Kind: {
543 std::shared_ptr<Variable> var = std::static_pointer_cast<Variable>(r esult);
544 this->markReadFrom(var);
545 return std::unique_ptr<VariableReference>(new VariableReference(iden tifier.fPosition,
546 var) );
dogben 2016/06/23 15:25:09 nit: std::move
547 }
548 case Symbol::kField_Kind: {
549 std::shared_ptr<Field> field = std::static_pointer_cast<Field>(resul t);
550 VariableReference* base = new VariableReference(identifier.fPosition , field->fOwner);
551 return std::unique_ptr<Expression>(new FieldAccess(std::unique_ptr<E xpression>(base),
552 field->fFieldInde x));
553 }
554 case Symbol::kType_Kind: {
555 auto t = std::static_pointer_cast<Type>(result);
556 return std::unique_ptr<TypeReference>(new TypeReference(identifier.f Position, t));
dogben 2016/06/23 15:25:11 nit: std::move(t)
557 }
558 default:
559 ABORT("unsupported symbol type %d\n", result->fKind);
560 }
561
562 }
563
564 std::unique_ptr<Expression> IRGenerator::coerce(std::unique_ptr<Expression> expr ,
565 std::shared_ptr<Type> type) {
566 if (expr == nullptr) {
567 return nullptr;
568 }
569 if (*expr->fType == *type) {
dogben 2016/06/23 15:25:11 nit: Elsewhere, you compare types by pointer addre
570 return expr;
571 }
572 if (!expr->fType->canCoerceTo(type)) {
573 fErrors.error(expr->fPosition, "expected '" + type->description() + "', but found '" +
574 expr->fType->description() + "'");
575 return nullptr;
576 }
577 if (type->kind() == Type::kScalar_Kind) {
dogben 2016/06/23 15:25:09 Why only scalars?
ethannicholas 2016/06/24 21:23:09 Because we don't currently support compound types
578 std::vector<std::unique_ptr<Expression>> parameters;
dogben 2016/06/23 15:25:11 nit: s/parameters/arguments/ (or just pass {std::m
ethannicholas 2016/06/24 21:23:08 This would have been easier had I realized you cou
579 parameters.push_back(std::move(expr));
580 ASTIdentifier id(Position(), type->description());
581 std::unique_ptr<Expression> ctor = this->convertIdentifier(id);
582 ASSERT(ctor);
583 return this->call(Position(), std::move(ctor), std::move(parameters));
584 }
585 ABORT("cannot coerce %s to %s", expr->fType->description().c_str(), type->na me().c_str());
dogben 2016/06/23 15:25:10 nit: s/name()/description()/?
586 }
587
588 /**
589 * Determines the operand and result types of a binary expression. Returns true if the expression is
590 * legal, false otherwise. If false, the values of the out parameters are undefi ned.
591 */
592 static bool determine_binary_type(Token::Kind op, std::shared_ptr<Type> left,
593 std::shared_ptr<Type> right,
594 std::shared_ptr<Type>* outLeftType,
595 std::shared_ptr<Type>* outRightType,
596 std::shared_ptr<Type>* outResultType,
597 bool tryFlipped) {
598 if (op == Token::STAR || op == Token::STAREQ) {
599 if (left->kind() == Type::kMatrix_Kind && right->kind() == Type::kVector _Kind) {
600 *outLeftType = left;
601 *outRightType = right;
602 *outResultType = right;
603 return left->rows() == right->columns();
604 }
605 if (left->kind() == Type::kVector_Kind && right->kind() == Type::kMatrix _Kind) {
606 *outLeftType = left;
607 *outRightType = right;
608 *outResultType = left;
609 return left->columns() == right->columns();
610 }
611 }
dogben 2016/06/23 15:25:09 Don't we need a special case for matrix * matrix a
ethannicholas 2016/06/24 21:23:08 Non-square matrices are on my list; obviously ther
612 bool isLogical;
613 switch (op) {
614 case Token::EQEQ: // fall through
615 case Token::NEQ: // fall through
616 case Token::LT: // fall through
617 case Token::GT: // fall through
618 case Token::LTEQ: // fall through
619 case Token::GTEQ:
620 isLogical = true;
621 break;
622 default:
623 isLogical = false;
624 }
625 // FIXME: need to disallow illegal operations like vec3 > vec3
dogben 2016/06/23 15:25:11 Also, bitwise and shift operations only apply to i
ethannicholas 2016/06/24 21:23:09 Only floats are fully supported for now, I've adde
626 if (left == right) {
627 *outLeftType = left;
628 *outRightType = left;
629 if (isLogical) {
630 *outResultType = kBool_Type;
631 } else {
632 *outResultType = left;
633 }
634 return true;
635 }
636 if (left->canCoerceTo(right)) {
dogben 2016/06/23 15:25:10 This case is incorrect for shift operations.
637 *outLeftType = right;
638 *outRightType = right;
639 if (isLogical) {
640 *outResultType = kBool_Type;
641 } else {
642 *outResultType = right;
643 }
644 return true;
645 }
646 if ((left->columns() > 1) && (right->kind() == Type::kScalar_Kind)) {
dogben 2016/06/23 15:25:11 This case shouldn't apply to arrays, right?
ethannicholas 2016/06/24 21:23:08 Good catch. This was written long before I added s
647 if (determine_binary_type(op, left->componentType(), right, outLeftType, outRightType,
648 outResultType, false)) {
649 *outLeftType = (*outLeftType)->toCompound(left->columns(), left->row s());
650 if (!isLogical) {
651 *outResultType = (*outResultType)->toCompound(left->columns(), l eft->rows());
652 }
653 return true;
654 }
655 return false;
656 }
657 if (tryFlipped) {
658 return determine_binary_type(op, right, left, outRightType, outLeftType, outResultType,
659 false);
660 }
661 return false;
662 }
663
664 std::unique_ptr<Expression> IRGenerator::convertBinaryExpression(ASTBinaryExpres sion& expression) {
665 std::unique_ptr<Expression> left = this->convertExpression(*expression.fLeft );
666 if (left == nullptr) {
667 return nullptr;
668 }
669 std::unique_ptr<Expression> right = this->convertExpression(*expression.fRig ht);
670 if (right == nullptr) {
671 return nullptr;
672 }
673 std::shared_ptr<Type> leftType;
674 std::shared_ptr<Type> rightType;
675 std::shared_ptr<Type> resultType;
676 if (!determine_binary_type(expression.fOperator, left->fType, right->fType, &leftType,
dogben 2016/06/23 15:25:10 Seems like we need special handling for logical op
ethannicholas 2016/06/24 21:23:09 In what way?
dogben 2016/06/26 03:57:52 Sorry for being unclear. Is "an_int || another_in
ethannicholas 2016/06/27 20:35:03 No, int cannot be coerced to bool.
dogben 2016/06/28 02:14:54 Sorry for being unclear. If you give "an_int || an
ethannicholas 2016/06/30 15:19:28 Sorry, gotcha.
677 &rightType, &resultType, true)) {
dogben 2016/06/23 15:25:11 tryFlipped probably shouldn't be true for shift op
ethannicholas 2016/06/24 21:23:08 It doesn't actually flip the operation around, it
dogben 2016/06/26 03:57:53 Understood. This comment really depends on what yo
678 fErrors.error(expression.fPosition, "type mismatch: '" +
679 Token::OperatorName(expression.fOper ator) +
680 "' cannot operate on '" + left->fTyp e->fName +
681 "', '" + right->fType->fName + "'");
682 return nullptr;
683 }
684 switch (expression.fOperator) {
685 case Token::EQ: // fall through
686 case Token::PLUSEQ: // fall through
687 case Token::MINUSEQ: // fall through
688 case Token::STAREQ: // fall through
689 case Token::SLASHEQ: // fall through
690 case Token::PERCENTEQ: // fall through
691 case Token::SHLEQ: // fall through
692 case Token::SHREQ: // fall through
693 case Token::BITWISEOREQ: // fall through
694 case Token::BITWISEXOREQ: // fall through
695 case Token::BITWISEANDEQ: // fall through
696 case Token::LOGICALOREQ: // fall through
697 case Token::LOGICALXOREQ: // fall through
698 case Token::LOGICALANDEQ:
699 this->markWrittenTo(*left);
700 default:
701 break;
702 }
703 return std::unique_ptr<Expression>(new BinaryExpression(expression.fPosition ,
704 this->coerce(std::mo ve(left), leftType),
dogben 2016/06/23 15:25:10 nit: assert not null? (also for next coerce)
705 expression.fOperator ,
706 this->coerce(std::mo ve(right),
707 rightTy pe),
708 resultType));
709 }
710
711 std::unique_ptr<Expression> IRGenerator::convertTernaryExpression(ASTTernaryExpr ession& expression) {
712 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*exp ression.fTest),
713 kBool_Type);
714 if (test == nullptr) {
715 return nullptr;
716 }
717 std::unique_ptr<Expression> ifTrue = this->convertExpression(*expression.fIf True);
718 if (ifTrue == nullptr) {
719 return nullptr;
720 }
721 std::unique_ptr<Expression> ifFalse = this->convertExpression(*expression.fI fFalse);
722 if (ifFalse == nullptr) {
723 return nullptr;
724 }
725 std::shared_ptr<Type> trueType;
726 std::shared_ptr<Type> falseType;
727 std::shared_ptr<Type> resultType;
728 if (!determine_binary_type(Token::EQEQ, ifTrue->fType, ifFalse->fType, &true Type,
729 &falseType, &resultType, true)) {
730 fErrors.error(expression.fPosition, "ternary operator result mismatch: ' " +
731 ifTrue->fType->fName + "', '" +
732 ifFalse->fType->fName + "'");
733 return nullptr;
734 }
735 ASSERT(trueType == falseType);
736 ifTrue = this->coerce(std::move(ifTrue), trueType);
737 ifFalse = this->coerce(std::move(ifFalse), falseType);
738 return std::unique_ptr<Expression>(new TernaryExpression(expression.fPositio n,
739 std::move(test),
740 std::move(ifTrue),
741 std::move(ifFalse)) );
742 }
743
744 std::unique_ptr<Expression> IRGenerator::call(Position position,
745 std::shared_ptr<FunctionDeclaratio n> function,
746 std::vector<std::unique_ptr<Expres sion>> parameters) {
dogben 2016/06/23 15:25:10 nit: s/parameters/arguments/
747 if (function->fParameters.size() != parameters.size()) {
748 std::string msg = "call to '" + function->fName + "' expected " +
749 to_string(function->fParameters.size()) +
750 " parameter";
751 if (function->fParameters.size() != 1) {
752 msg += "s";
753 }
754 msg += ", but found " + to_string(parameters.size());
755 fErrors.error(position, msg);
756 return nullptr;
757 }
758 for (size_t i = 0; i < parameters.size(); i++) {
759 parameters[i] = this->coerce(std::move(parameters[i]), function->fParame ters[i]->fType);
dogben 2016/06/23 15:25:09 1. How does this work for out/inout parameters? 2.
ethannicholas 2016/06/24 21:23:08 Good catch, was missing a markWrittenTo here.
760 }
761 return std::unique_ptr<FunctionCall>(new FunctionCall(position, function,
762 std::move(parameters)) );
763 }
764
765 /**
766 * Determines the cost of coercing the parameters of a function to the required types. Returns true
767 * if the cost could be computed, false if the call is not valid. Cost has no pa rticular meaning
768 * other than "lower costs are preferred".
769 */
770 bool IRGenerator::determineCallCost(std::shared_ptr<FunctionDeclaration> functio n,
771 std::vector<std::unique_ptr<Expression>>& pa rameters,
dogben 2016/06/23 15:25:08 nit: s/parameters/arguments/ nit: const?
772 int* outCost) {
773 if (function->fParameters.size() != parameters.size()) {
774 return false;
775 }
776 int total = 0;
777 for (size_t i = 0; i < parameters.size(); i++) {
778 int cost;
779 if (parameters[i]->fType->determineCoercionCost(function->fParameters[i] ->fType, &cost)) {
780 total += cost;
781 } else {
782 return false;
783 }
784 }
785 *outCost = total;
786 return true;
787 }
788
789 std::unique_ptr<Expression> IRGenerator::call(Position position,
790 std::unique_ptr<Expression> functi onValue,
791 std::vector<std::unique_ptr<Expres sion>> parameters) {
dogben 2016/06/23 15:25:09 nit: s/parameters/arguments/
792 if (functionValue->fKind == Expression::kTypeReference_Kind) {
793 return this->convertConstructor(position,
794 ((TypeReference&) *functionValue).fValue ,
795 std::move(parameters));
796 }
797 if (functionValue->fKind != Expression::kFunctionReference_Kind) {
798 fErrors.error(position, "'" + functionValue->description() + "' is not a function");
799 return nullptr;
800 }
801 FunctionReference* ref = (FunctionReference*) functionValue.get();
802 int bestCost = INT_MAX;
803 std::shared_ptr<FunctionDeclaration> best;
804 if (ref->fFunctions.size() > 1) {
805 for (auto f : ref->fFunctions) {
dogben 2016/06/23 15:25:11 nit: const ref
806 int cost;
807 if (this->determineCallCost(f, parameters, &cost) && cost < bestCost ) {
808 bestCost = cost;
809 best = f;
810 }
811 }
812 if (best != nullptr) {
813 return this->call(position, best, std::move(parameters));
dogben 2016/06/23 15:25:09 nit: std::move(best)
814 }
815 std::string msg = "no match for " + ref->fFunctions[0]->fName + "(";
816 std::string separator = "";
817 for (size_t i = 0; i < parameters.size(); i++) {
818 msg += separator;
819 separator = ", ";
820 msg += parameters[i]->fType->description();
821 }
822 msg += ")";
823 fErrors.error(position, msg);
824 return nullptr;
825 }
826 return this->call(position, ref->fFunctions[0], std::move(parameters));
827 }
828
829 std::unique_ptr<Expression> IRGenerator::convertConstructor(Position position,
830 std::shared_ptr<Type > type,
831 std::vector<std::unique_ptr<Ex pression>> params) {
dogben 2016/06/23 15:25:10 nit: s/params/args/ nit: odd indentation
832 if (type == kFloat_Type && params.size() == 1 &&
833 params[0]->fKind == Expression::kIntLiteral_Kind) {
834 int64_t value = ((IntLiteral&) *params[0]).fValue;
835 return std::unique_ptr<Expression>(new FloatLiteral(position, (double) v alue));
836 }
837 int min = type->rows() * type->columns();
838 int max = type->columns() > 1 ? INT_MAX : min;
dogben 2016/06/24 17:05:26 This doesn't seem like it would work for struct co
ethannicholas 2016/06/24 21:23:09 We don't use them. I've got it on my list of thing
839 int actual = 0;
840 for (size_t i = 0; i < params.size(); i++) {
841 if (params[i]->fType->kind() == Type::kScalar_Kind) {
842 actual += 1;
843 if (type->kind() != Type::kScalar_Kind) {
dogben 2016/06/23 15:25:10 Is this the correct criteria? I would expect we wa
ethannicholas 2016/06/24 21:23:08 I only officially support float vectors and matric
844 params[i] = coerce(std::move(params[i]), type->componentType());
dogben 2016/06/23 15:25:11 Check that coerce returns non-null? nit: this->coe
ethannicholas 2016/06/24 21:23:08 Fixed the nit, but I don't think the null check ma
845 }
846 } else {
847 actual += params[i]->fType->rows() * params[i]->fType->columns();
dogben 2016/06/23 15:25:11 Check that rows() and columns() are not -1?
ethannicholas 2016/06/24 21:23:08 rows() and columns() will both generate an asserti
848 }
849 }
850 if ((type->kind() != Type::kVector_Kind || actual != 1) &&
dogben 2016/06/23 15:25:09 nit: Might be a bit clearer to rephrase as: (actua
851 (type->kind() != Type::kMatrix_Kind || actual != 1) &&
852 (actual < min || actual > max)) {
dogben 2016/06/23 15:25:10 Checking my understanding: Are all of the followin
dogben 2016/06/24 17:05:26 I happened upon the section of the spec that talks
ethannicholas 2016/06/24 21:23:08 Yes! This is permitted by GLSL, though I am consid
853 fErrors.error(position, "invalid parameters to '" + type->description() +
854 "' constructor (expected " + to_string(min) + " scalars, " +
855 "but found " + to_string(actual) + ")");
856 return nullptr;
857 }
858 if (type->isNumber()) {
859 ASSERT(params.size() == 1);
860 if (params[0]->fType == kBool_Type) {
861 return std::unique_ptr<Expression>(new TernaryExpression(position, s td::move(params[0]),
dogben 2016/06/23 15:25:11 nit: position maybe should be params[0]->fPosition
862 std::unique_ptr<Expression>(new IntLi teral(position, 1)),
dogben 2016/06/23 15:25:10 nit: odd indentation
863 std::unique_ptr<Expression>(new IntLite ral(position, 0))));
864 }
865 }
866 if (params.size() == 1 && params[0]->fType == type) {
867 // parameter is already the right type, just return it
868 return std::move(params[0]);
869 }
870 return std::unique_ptr<Expression>(new Constructor(position, type, std::move (params)));
dogben 2016/06/23 15:25:10 nit: std::move(type)
871 }
872
873 std::unique_ptr<Expression> IRGenerator::convertPrefixExpression(ASTPrefixExpres sion& expression) {
874 std::unique_ptr<Expression> base = this->convertExpression(*expression.fOper and);
875 if (base == nullptr) {
876 return nullptr;
877 }
878 switch (expression.fOperator) {
879 case Token::PLUS:
880 if (!base->fType->isNumber() && base->fType->kind() != Type::kVector _Kind) {
881 fErrors.error(expression.fPosition,
882 "'+' cannot operate on '" + base->fType->descripti on() + "'");
883 return nullptr;
884 }
885 return base;
886 case Token::MINUS:
887 if (!base->fType->isNumber() && base->fType->kind() != Type::kVector _Kind) {
888 fErrors.error(expression.fPosition,
889 "'-' cannot operate on '" + base->fType->descripti on() + "'");
890 return nullptr;
891 }
892 if (base->fKind == Expression::kIntLiteral_Kind) {
893 return std::unique_ptr<Expression>(new IntLiteral(base->fPositio n,
894 -((IntLiteral& ) *base).fValue));
895 }
896 if (base->fKind == Expression::kFloatLiteral_Kind) {
897 double value = -((FloatLiteral&) *base).fValue;
898 return std::unique_ptr<Expression>(new FloatLiteral(base->fPosit ion, value));
899 }
900 return std::unique_ptr<Expression>(new PrefixExpression(Token::MINUS , std::move(base)));
901 case Token::PLUSPLUS:
902 if (!base->fType->isNumber()) {
903 fErrors.error(expression.fPosition,
904 "'" + Token::OperatorName(expression.fOperator) +
905 "' cannot operate on '" + base->fType->description () + "'");
906 return nullptr;
907 }
908 this->markWrittenTo(*base);
909 break;
910 case Token::MINUSMINUS:
911 if (!base->fType->isNumber()) {
912 fErrors.error(expression.fPosition,
913 "'" + Token::OperatorName(expression.fOperator) +
914 "' cannot operate on '" + base->fType->description () + "'");
915 return nullptr;
916 }
917 this->markWrittenTo(*base);
918 break;
919 case Token::NOT:
920 if (base->fType != kBool_Type) {
921 fErrors.error(expression.fPosition,
922 "'" + Token::OperatorName(expression.fOperator) +
923 "' cannot operate on '" + base->fType->description () + "'");
924 return nullptr;
925 }
926 break;
927 default:
928 ABORT("unsupported prefix operator\n");
929 }
930 return std::unique_ptr<Expression>(new PrefixExpression(expression.fOperator ,
931 std::move(base)));
932 }
933
934 std::unique_ptr<Expression> IRGenerator::convertIndex(std::unique_ptr<Expression > base,
935 ASTExpression& index) {
936 if (base->fType->kind() != Type::kArray_Kind && base->fType->kind() != Type: :kMatrix_Kind &&
937 base->fType->kind() != Type::kVector_Kind) {
938 fErrors.error(base->fPosition, "expected array, but found '" + base->fTy pe->description() +
939 "'");
940 return nullptr;
941 }
942 std::unique_ptr<Expression> converted = this->convertExpression(index);
943 if (converted == nullptr) {
944 return nullptr;
945 }
946 converted = this->coerce(std::move(converted), kInt_Type);
947 if (converted == nullptr) {
948 return nullptr;
949 }
950 return std::unique_ptr<Expression>(new IndexExpression(std::move(base), std: :move(converted)));
dogben 2016/06/23 15:25:09 If index is constant and base size is known, shoul
ethannicholas 2016/06/24 21:23:09 I was planning to worry about that when I've got a
951 }
952
953 std::unique_ptr<Expression> IRGenerator::convertField(std::unique_ptr<Expression > base,
954 std::string field) {
dogben 2016/06/23 15:25:09 nit: const ref?
955 auto fields = base->fType->fields();
956 for (size_t i = 0; i < fields.size(); i++) {
957 if (fields[i].fName == field) {
958 return std::unique_ptr<Expression>(new FieldAccess(std::move(base), (int) i));
959 }
960 }
961 fErrors.error(base->fPosition, "type '" + base->fType->description() + "' do es not have a "
dogben 2016/06/23 15:25:11 The spec says something about arrays having a leng
ethannicholas 2016/06/24 21:23:08 Not yet, at least.
962 "field named '" + field + "");
963 return nullptr;
964 }
965
966 std::unique_ptr<Expression> IRGenerator::convertSwizzle(std::unique_ptr<Expressi on> base,
967 std::string fields) {
dogben 2016/06/23 15:25:09 nit: const ref?
968 if (base->fType->columns() == 0) {
dogben 2016/06/23 15:25:10 nit: Should this just be an assert?
969 fErrors.error(base->fPosition, "cannot swizzle type '" + base->fType->de scription() + "'");
970 return nullptr;
971 }
972 std::vector<int> swizzleComponents;
973 for (char c : fields) {
974 switch (c) {
975 case 'x': // fall through
976 case 'r': // fall through
977 case 's':
978 swizzleComponents.push_back(0);
979 break;
980 case 'y': // fall through
981 case 'g': // fall through
982 case 't':
983 if (base->fType->columns() >= 2) {
984 swizzleComponents.push_back(1);
985 break;
986 }
987 // fall through
988 case 'z': // fall through
989 case 'b': // fall through
990 case 'p':
991 if (base->fType->columns() >= 3) {
992 swizzleComponents.push_back(2);
993 break;
994 }
995 // fall through
996 case 'w': // fall through
997 case 'a': // fall through
998 case 'q':
999 if (base->fType->columns() >= 4) {
1000 swizzleComponents.push_back(3);
1001 break;
1002 }
1003 // fall through
1004 default:
1005 fErrors.error(base->fPosition, "invalid swizzle component '" + s td::string(1, c) +
1006 "'");
1007 return nullptr;
1008 }
1009 }
1010 ASSERT(swizzleComponents.size() > 0);
1011 if (swizzleComponents.size() > 4) {
1012 fErrors.error(base->fPosition, "too many components in swizzle mask '" + fields + "'");
1013 return nullptr;
1014 }
1015 return std::unique_ptr<Expression>(new Swizzle(std::move(base), swizzleCompo nents));
1016 }
1017
1018 std::unique_ptr<Expression> IRGenerator::convertSuffixExpression(ASTSuffixExpres sion& expression) {
1019 std::unique_ptr<Expression> base = this->convertExpression(*expression.fBase );
1020 if (base == nullptr) {
1021 return nullptr;
1022 }
1023 switch (expression.fSuffix->fKind) {
1024 case ASTSuffix::kIndex_Kind:
1025 return this->convertIndex(std::move(base),
1026 *((ASTIndexSuffix&) *expression.fSuffix).f Expression);
1027 case ASTSuffix::kCall_Kind: {
1028 auto rawParameters = &((ASTCallSuffix&) *expression.fSuffix).fParame ters;
dogben 2016/06/23 15:25:09 nit: s/parameters/arguments/ in this block
1029 std::vector<std::unique_ptr<Expression>> parameters;
1030 for (size_t i = 0; i < rawParameters->size(); i++) {
1031 std::unique_ptr<Expression> converted = this->convertExpression(
1032 *( *rawParameters)[i]);
dogben 2016/06/23 15:25:08 nit: odd indentation
1033 if (converted == nullptr) {
1034 return nullptr;
1035 }
1036 parameters.push_back(std::move(converted));
1037 }
1038 return this->call(expression.fPosition, std::move(base), std::move(p arameters));
1039 }
1040 case ASTSuffix::kField_Kind: {
1041 std::string field = ((ASTFieldSuffix&) *expression.fSuffix).fField;
dogben 2016/06/23 15:25:09 nit: const ref?
1042 switch (base->fType->kind()) {
1043 case Type::kVector_Kind:
1044 return this->convertSwizzle(std::move(base), field);
1045 case Type::kStruct_Kind:
1046 return this->convertField(std::move(base), field);
1047 default:
1048 fErrors.error(base->fPosition, "cannot swizzle value of type '" +
1049 base->fType->description() + "'");
1050 return nullptr;
1051 }
1052 }
1053 case ASTSuffix::kPostIncrement_Kind:
1054 if (!base->fType->isNumber()) {
1055 fErrors.error(expression.fPosition,
1056 "'++' cannot operate on '" + base->fType->descript ion() + "'");
1057 return nullptr;
1058 }
1059 this->markWrittenTo(*base);
1060 return std::unique_ptr<Expression>(new PostfixExpression(std::move(b ase),
1061 Token::PLUS PLUS));
1062 case ASTSuffix::kPostDecrement_Kind:
1063 if (!base->fType->isNumber()) {
1064 fErrors.error(expression.fPosition,
1065 "'--' cannot operate on '" + base->fType->descript ion() + "'");
1066 return nullptr;
1067 }
1068 this->markWrittenTo(*base);
1069 return std::unique_ptr<Expression>(new PostfixExpression(std::move(b ase),
1070 Token::MINU SMINUS));
1071 default:
1072 ABORT("unsupported suffix operator");
1073 }
1074 }
1075
1076 void IRGenerator::markReadFrom(std::shared_ptr<Variable> var) {
1077 var->fIsReadFrom = true;
1078 }
1079
1080 void IRGenerator::markWrittenTo(Expression& expr) {
1081 switch (expr.fKind) {
1082 case Expression::kVariableReference_Kind:
1083 ((VariableReference&) expr).fVariable->fIsWrittenTo = true;
1084 break;
1085 case Expression::kFieldAccess_Kind:
1086 this->markWrittenTo(*((FieldAccess&) expr).fBase);
1087 break;
1088 case Expression::kSwizzle_Kind:
1089 this->markWrittenTo(*((Swizzle&) expr).fBase);
1090 break;
1091 case Expression::kIndex_Kind:
1092 this->markWrittenTo(*((IndexExpression&) expr).fBase);
1093 break;
1094 default:
1095 fErrors.error(expr.fPosition, "cannot assign to '" + expr.descriptio n() + "'");
1096 break;
1097 }
1098 }
1099
1100 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698