1 /* 2 * Copyright 2017 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_ENUM 9 #define SKSL_ENUM 10 11 #include "src/sksl/ir/SkSLExpression.h" 12 #include "src/sksl/ir/SkSLProgramElement.h" 13 #include "src/sksl/ir/SkSLSymbolTable.h" 14 #include "src/sksl/ir/SkSLVariable.h" 15 16 #include <algorithm> 17 #include <vector> 18 19 namespace SkSL { 20 21 struct Symbol; 22 23 struct Enum : public ProgramElement { EnumEnum24 Enum(int offset, StringFragment typeName, std::shared_ptr<SymbolTable> symbols) 25 : INHERITED(offset, kEnum_Kind) 26 , fTypeName(typeName) 27 , fSymbols(std::move(symbols)) {} 28 cloneEnum29 std::unique_ptr<ProgramElement> clone() const override { 30 return std::unique_ptr<ProgramElement>(new Enum(fOffset, fTypeName, fSymbols)); 31 } 32 descriptionEnum33 String description() const override { 34 String result = "enum class " + fTypeName + " {\n"; 35 String separator; 36 std::vector<const Symbol*> sortedSymbols; 37 for (const auto& pair : *fSymbols) { 38 sortedSymbols.push_back(pair.second); 39 } 40 std::sort(sortedSymbols.begin(), sortedSymbols.end(), 41 [](const Symbol* a, const Symbol* b) { return a->fName < b->fName; }); 42 for (const auto& s : sortedSymbols) { 43 result += separator + " " + s->fName + " = " + 44 ((Variable*) s)->fInitialValue->description(); 45 separator = ",\n"; 46 } 47 result += "\n};"; 48 return result; 49 } 50 51 bool fBuiltin = false; 52 const StringFragment fTypeName; 53 const std::shared_ptr<SymbolTable> fSymbols; 54 55 typedef ProgramElement INHERITED; 56 }; 57 58 } // namespace 59 60 #endif 61