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 { VarDeclarationVarDeclaration24 VarDeclaration(const Variable* var, 25 std::vector<std::unique_ptr<Expression>> sizes, 26 std::unique_ptr<Expression> value) 27 : fVar(var) 28 , fSizes(std::move(sizes)) 29 , fValue(std::move(value)) {} 30 descriptionVarDeclaration31 SkString description() const { 32 SkString result = fVar->fName; 33 for (const auto& size : fSizes) { 34 if (size) { 35 result += "[" + size->description() + "]"; 36 } else { 37 result += "[]"; 38 } 39 } 40 if (fValue) { 41 result += " = " + fValue->description(); 42 } 43 return result; 44 } 45 46 const Variable* fVar; 47 std::vector<std::unique_ptr<Expression>> fSizes; 48 std::unique_ptr<Expression> fValue; 49 }; 50 51 /** 52 * A variable declaration statement, which may consist of one or more individual variables. 53 */ 54 struct VarDeclarations : public ProgramElement { VarDeclarationsVarDeclarations55 VarDeclarations(Position position, const Type* baseType, 56 std::vector<VarDeclaration> vars) 57 : INHERITED(position, kVar_Kind) 58 , fBaseType(*baseType) 59 , fVars(std::move(vars)) {} 60 descriptionVarDeclarations61 SkString description() const override { 62 if (!fVars.size()) { 63 return SkString(); 64 } 65 SkString result = fVars[0].fVar->fModifiers.description() + fBaseType.description() + " "; 66 SkString separator; 67 for (const auto& var : fVars) { 68 result += separator; 69 separator = ", "; 70 result += var.description(); 71 } 72 return result; 73 } 74 75 const Type& fBaseType; 76 std::vector<VarDeclaration> fVars; 77 78 typedef ProgramElement INHERITED; 79 }; 80 81 } // namespace 82 83 #endif 84