SkSLEnum.h 1.6 KB

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