SkSLVarDeclarations.h 3.7 KB

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