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 #ifndef SKSL_VARDECLARATIONS 9 #define SKSL_VARDECLARATIONS 10 11 #include "SkSLExpression.h" 12 #include "SkSLProgramElement.h" 13 #include "SkSLStatement.h" 14 #include "SkSLVariable.h" 15 16 namespace SkSL { 17 18 /** 19 * A single variable declaration within a var declaration statement. For instance, the statement 20 * 'int x = 2, y[3];' is a VarDeclarations statement containing two individual VarDeclaration 21 * instances. 22 */ 23 struct VarDeclaration : public Statement { VarDeclarationVarDeclaration24 VarDeclaration(const Variable* var, 25 std::vector<std::unique_ptr<Expression>> sizes, 26 std::unique_ptr<Expression> value) 27 : INHERITED(var->fPosition, Statement::kVarDeclaration_Kind) 28 , fVar(var) 29 , fSizes(std::move(sizes)) 30 , fValue(std::move(value)) {} 31 descriptionVarDeclaration32 String description() const { 33 String result = fVar->fName; 34 for (const auto& size : fSizes) { 35 if (size) { 36 result += "[" + size->description() + "]"; 37 } else { 38 result += "[]"; 39 } 40 } 41 if (fValue) { 42 result += " = " + fValue->description(); 43 } 44 return result; 45 } 46 47 const Variable* fVar; 48 std::vector<std::unique_ptr<Expression>> fSizes; 49 std::unique_ptr<Expression> fValue; 50 51 typedef Statement INHERITED; 52 }; 53 54 /** 55 * A variable declaration statement, which may consist of one or more individual variables. 56 */ 57 struct VarDeclarations : public ProgramElement { VarDeclarationsVarDeclarations58 VarDeclarations(Position position, const Type* baseType, 59 std::vector<std::unique_ptr<VarDeclaration>> vars) 60 : INHERITED(position, kVar_Kind) 61 , fBaseType(*baseType) { 62 for (auto& var : vars) { 63 fVars.push_back(std::unique_ptr<Statement>(var.release())); 64 } 65 } 66 descriptionVarDeclarations67 String description() const override { 68 if (!fVars.size()) { 69 return String(); 70 } 71 String result = ((VarDeclaration&) *fVars[0]).fVar->fModifiers.description() + 72 fBaseType.description() + " "; 73 String separator; 74 for (const auto& var : fVars) { 75 result += separator; 76 separator = ", "; 77 result += var->description(); 78 } 79 return result; 80 } 81 82 const Type& fBaseType; 83 // this *should* be a vector of unique_ptr<VarDeclaration>, but it significantly simplifies the 84 // CFG to only have to worry about unique_ptr<Statement> 85 std::vector<std::unique_ptr<Statement>> fVars; 86 87 typedef ProgramElement INHERITED; 88 }; 89 90 } // namespace 91 92 #endif 93