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

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

Issue 2312233002: refactored SkSL VarDeclaration handling (Closed)
Patch Set: fixed nits Created 4 years, 3 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
« no previous file with comments | « src/sksl/SkSLIRGenerator.h ('k') | src/sksl/SkSLParser.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2016 Google Inc. 2 * Copyright 2016 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkSLIRGenerator.h" 8 #include "SkSLIRGenerator.h"
9 9
10 #include "limits.h" 10 #include "limits.h"
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 if (!statement) { 122 if (!statement) {
123 return nullptr; 123 return nullptr;
124 } 124 }
125 statements.push_back(std::move(statement)); 125 statements.push_back(std::move(statement));
126 } 126 }
127 return std::unique_ptr<Block>(new Block(block.fPosition, std::move(statement s), fSymbolTable)); 127 return std::unique_ptr<Block>(new Block(block.fPosition, std::move(statement s), fSymbolTable));
128 } 128 }
129 129
130 std::unique_ptr<Statement> IRGenerator::convertVarDeclarationStatement( 130 std::unique_ptr<Statement> IRGenerator::convertVarDeclarationStatement(
131 const ASTVarDeclar ationStatement& s) { 131 const ASTVarDeclar ationStatement& s) {
132 auto decl = this->convertVarDeclaration(*s.fDeclaration, Variable::kLocal_St orage); 132 auto decl = this->convertVarDeclarations(*s.fDeclarations, Variable::kLocal_ Storage);
133 if (!decl) { 133 if (!decl) {
134 return nullptr; 134 return nullptr;
135 } 135 }
136 return std::unique_ptr<Statement>(new VarDeclarationStatement(std::move(decl ))); 136 return std::unique_ptr<Statement>(new VarDeclarationsStatement(std::move(dec l)));
137 } 137 }
138 138
139 Modifiers IRGenerator::convertModifiers(const ASTModifiers& modifiers) { 139 Modifiers IRGenerator::convertModifiers(const ASTModifiers& modifiers) {
140 return Modifiers(modifiers); 140 return Modifiers(modifiers);
141 } 141 }
142 142
143 std::unique_ptr<VarDeclaration> IRGenerator::convertVarDeclaration(const ASTVarD eclaration& decl, 143 std::unique_ptr<VarDeclarations> IRGenerator::convertVarDeclarations(const ASTVa rDeclarations& decl,
144 Variable::Sto rage storage) { 144 Variable::S torage storage) {
145 std::vector<const Variable*> variables; 145 std::vector<VarDeclaration> variables;
146 std::vector<std::vector<std::unique_ptr<Expression>>> sizes;
147 std::vector<std::unique_ptr<Expression>> values;
148 const Type* baseType = this->convertType(*decl.fType); 146 const Type* baseType = this->convertType(*decl.fType);
149 if (!baseType) { 147 if (!baseType) {
150 return nullptr; 148 return nullptr;
151 } 149 }
152 for (size_t i = 0; i < decl.fNames.size(); i++) { 150 for (const auto& varDecl : decl.fVars) {
153 Modifiers modifiers = this->convertModifiers(decl.fModifiers); 151 Modifiers modifiers = this->convertModifiers(decl.fModifiers);
154 const Type* type = baseType; 152 const Type* type = baseType;
155 ASSERT(type->kind() != Type::kArray_Kind); 153 ASSERT(type->kind() != Type::kArray_Kind);
156 std::vector<std::unique_ptr<Expression>> currentVarSizes; 154 std::vector<std::unique_ptr<Expression>> sizes;
157 for (size_t j = 0; j < decl.fSizes[i].size(); j++) { 155 for (const auto& rawSize : varDecl.fSizes) {
158 if (decl.fSizes[i][j]) { 156 if (rawSize) {
159 ASTExpression& rawSize = *decl.fSizes[i][j]; 157 auto size = this->coerce(this->convertExpression(*rawSize), *fCo ntext.fInt_Type);
160 auto size = this->coerce(this->convertExpression(rawSize), *fCon text.fInt_Type);
161 if (!size) { 158 if (!size) {
162 return nullptr; 159 return nullptr;
163 } 160 }
164 std::string name = type->fName; 161 std::string name = type->fName;
165 uint64_t count; 162 uint64_t count;
166 if (size->fKind == Expression::kIntLiteral_Kind) { 163 if (size->fKind == Expression::kIntLiteral_Kind) {
167 count = ((IntLiteral&) *size).fValue; 164 count = ((IntLiteral&) *size).fValue;
168 if (count <= 0) { 165 if (count <= 0) {
169 fErrors.error(size->fPosition, "array size must be posit ive"); 166 fErrors.error(size->fPosition, "array size must be posit ive");
170 } 167 }
171 name += "[" + to_string(count) + "]"; 168 name += "[" + to_string(count) + "]";
172 } else { 169 } else {
173 count = -1; 170 count = -1;
174 name += "[]"; 171 name += "[]";
175 } 172 }
176 type = new Type(name, Type::kArray_Kind, *type, (int) count); 173 type = new Type(name, Type::kArray_Kind, *type, (int) count);
177 fSymbolTable->takeOwnership((Type*) type); 174 fSymbolTable->takeOwnership((Type*) type);
178 currentVarSizes.push_back(std::move(size)); 175 sizes.push_back(std::move(size));
179 } else { 176 } else {
180 type = new Type(type->fName + "[]", Type::kArray_Kind, *type, -1 ); 177 type = new Type(type->fName + "[]", Type::kArray_Kind, *type, -1 );
181 fSymbolTable->takeOwnership((Type*) type); 178 fSymbolTable->takeOwnership((Type*) type);
182 currentVarSizes.push_back(nullptr); 179 sizes.push_back(nullptr);
183 } 180 }
184 } 181 }
185 auto var = std::unique_ptr<Variable>(new Variable(decl.fPosition, modifi ers, decl.fNames[i], 182 auto var = std::unique_ptr<Variable>(new Variable(decl.fPosition, modifi ers, varDecl.fName,
186 *type, storage)); 183 *type, storage));
187 std::unique_ptr<Expression> value; 184 std::unique_ptr<Expression> value;
188 if (decl.fValues[i]) { 185 if (varDecl.fValue) {
189 value = this->convertExpression(*decl.fValues[i]); 186 value = this->convertExpression(*varDecl.fValue);
190 if (!value) { 187 if (!value) {
191 return nullptr; 188 return nullptr;
192 } 189 }
193 value = this->coerce(std::move(value), *type); 190 value = this->coerce(std::move(value), *type);
194 } 191 }
195 if ("gl_FragCoord" == decl.fNames[i] && (*fSymbolTable)[decl.fNames[i]]) { 192 if ("gl_FragCoord" == varDecl.fName && (*fSymbolTable)[varDecl.fName]) {
196 // already defined, just update the modifiers 193 // already defined, just update the modifiers
197 Variable* old = (Variable*) (*fSymbolTable)[decl.fNames[i]]; 194 Variable* old = (Variable*) (*fSymbolTable)[varDecl.fName];
198 old->fModifiers = var->fModifiers; 195 old->fModifiers = var->fModifiers;
199 } else { 196 } else {
200 variables.push_back(var.get()); 197 variables.emplace_back(var.get(), std::move(sizes), std::move(value) );
201 fSymbolTable->add(decl.fNames[i], std::move(var)); 198 fSymbolTable->add(varDecl.fName, std::move(var));
202 values.push_back(std::move(value));
203 sizes.push_back(std::move(currentVarSizes));
204 } 199 }
205 } 200 }
206 return std::unique_ptr<VarDeclaration>(new VarDeclaration(decl.fPosition, 201 return std::unique_ptr<VarDeclarations>(new VarDeclarations(decl.fPosition,
207 baseType, 202 baseType,
208 std::move(variable s), 203 std::move(variab les)));
209 std::move(sizes),
210 std::move(values)) );
211 } 204 }
212 205
213 std::unique_ptr<Statement> IRGenerator::convertIf(const ASTIfStatement& s) { 206 std::unique_ptr<Statement> IRGenerator::convertIf(const ASTIfStatement& s) {
214 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*s.f Test), 207 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*s.f Test),
215 *fContext.fBool_Type); 208 *fContext.fBool_Type);
216 if (!test) { 209 if (!test) {
217 return nullptr; 210 return nullptr;
218 } 211 }
219 std::unique_ptr<Statement> ifTrue = this->convertStatement(*s.fIfTrue); 212 std::unique_ptr<Statement> ifTrue = this->convertStatement(*s.fIfTrue);
220 if (!ifTrue) { 213 if (!ifTrue) {
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after
475 } 468 }
476 return nullptr; 469 return nullptr;
477 } 470 }
478 471
479 std::unique_ptr<InterfaceBlock> IRGenerator::convertInterfaceBlock(const ASTInte rfaceBlock& intf) { 472 std::unique_ptr<InterfaceBlock> IRGenerator::convertInterfaceBlock(const ASTInte rfaceBlock& intf) {
480 std::shared_ptr<SymbolTable> old = fSymbolTable; 473 std::shared_ptr<SymbolTable> old = fSymbolTable;
481 AutoSymbolTable table(this); 474 AutoSymbolTable table(this);
482 Modifiers mods = this->convertModifiers(intf.fModifiers); 475 Modifiers mods = this->convertModifiers(intf.fModifiers);
483 std::vector<Type::Field> fields; 476 std::vector<Type::Field> fields;
484 for (size_t i = 0; i < intf.fDeclarations.size(); i++) { 477 for (size_t i = 0; i < intf.fDeclarations.size(); i++) {
485 std::unique_ptr<VarDeclaration> decl = this->convertVarDeclaration( 478 std::unique_ptr<VarDeclarations> decl = this->convertVarDeclarations(
486 *intf.f Declarations[i], 479 *intf.f Declarations[i],
487 Variabl e::kGlobal_Storage); 480 Variabl e::kGlobal_Storage);
488 for (size_t j = 0; j < decl->fVars.size(); j++) { 481 for (const auto& var : decl->fVars) {
489 fields.push_back(Type::Field(decl->fVars[j]->fModifiers, decl->fVars [j]->fName, 482 fields.push_back(Type::Field(var.fVar->fModifiers, var.fVar->fName,
490 &decl->fVars[j]->fType)); 483 &var.fVar->fType));
491 if (decl->fValues[j]) { 484 if (var.fValue) {
492 fErrors.error(decl->fPosition, 485 fErrors.error(decl->fPosition,
493 "initializers are not permitted on interface block fields"); 486 "initializers are not permitted on interface block fields");
494 } 487 }
495 if (decl->fVars[j]->fModifiers.fFlags & (Modifiers::kIn_Flag | 488 if (var.fVar->fModifiers.fFlags & (Modifiers::kIn_Flag |
496 Modifiers::kOut_Flag | 489 Modifiers::kOut_Flag |
497 Modifiers::kUniform_Flag | 490 Modifiers::kUniform_Flag |
498 Modifiers::kConst_Flag)) { 491 Modifiers::kConst_Flag)) {
499 fErrors.error(decl->fPosition, 492 fErrors.error(decl->fPosition,
500 "interface block fields may not have storage quali fiers"); 493 "interface block fields may not have storage quali fiers");
501 } 494 }
502 } 495 }
503 } 496 }
504 Type* type = new Type(intf.fInterfaceName, fields); 497 Type* type = new Type(intf.fInterfaceName, fields);
505 fSymbolTable->takeOwnership(type); 498 fSymbolTable->takeOwnership(type);
506 std::string name = intf.fValueName.length() > 0 ? intf.fValueName : intf.fIn terfaceName; 499 std::string name = intf.fValueName.length() > 0 ? intf.fValueName : intf.fIn terfaceName;
507 Variable* var = new Variable(intf.fPosition, mods, name, *type, Variable::kG lobal_Storage); 500 Variable* var = new Variable(intf.fPosition, mods, name, *type, Variable::kG lobal_Storage);
508 fSymbolTable->takeOwnership(var); 501 fSymbolTable->takeOwnership(var);
509 if (intf.fValueName.length()) { 502 if (intf.fValueName.length()) {
(...skipping 748 matching lines...) Expand 10 before | Expand all | Expand 10 after
1258 case Expression::kIndex_Kind: 1251 case Expression::kIndex_Kind:
1259 this->markWrittenTo(*((IndexExpression&) expr).fBase); 1252 this->markWrittenTo(*((IndexExpression&) expr).fBase);
1260 break; 1253 break;
1261 default: 1254 default:
1262 fErrors.error(expr.fPosition, "cannot assign to '" + expr.descriptio n() + "'"); 1255 fErrors.error(expr.fPosition, "cannot assign to '" + expr.descriptio n() + "'");
1263 break; 1256 break;
1264 } 1257 }
1265 } 1258 }
1266 1259
1267 } 1260 }
OLDNEW
« no previous file with comments | « src/sksl/SkSLIRGenerator.h ('k') | src/sksl/SkSLParser.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698