SkSLVariable.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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_VARIABLE
  8. #define SKSL_VARIABLE
  9. #include "src/sksl/SkSLPosition.h"
  10. #include "src/sksl/ir/SkSLModifiers.h"
  11. #include "src/sksl/ir/SkSLSymbol.h"
  12. #include "src/sksl/ir/SkSLType.h"
  13. namespace SkSL {
  14. struct Expression;
  15. /**
  16. * Represents a variable, whether local, global, or a function parameter. This represents the
  17. * variable itself (the storage location), which is shared between all VariableReferences which
  18. * read or write that storage location.
  19. */
  20. struct Variable : public Symbol {
  21. enum Storage {
  22. kGlobal_Storage,
  23. kInterfaceBlock_Storage,
  24. kLocal_Storage,
  25. kParameter_Storage
  26. };
  27. Variable(int offset, Modifiers modifiers, StringFragment name, const Type& type,
  28. Storage storage, Expression* initialValue = nullptr)
  29. : INHERITED(offset, kVariable_Kind, name)
  30. , fModifiers(modifiers)
  31. , fType(type)
  32. , fStorage(storage)
  33. , fInitialValue(initialValue)
  34. , fReadCount(0)
  35. , fWriteCount(initialValue ? 1 : 0) {}
  36. ~Variable() override {
  37. // can't destroy a variable while there are remaining references to it
  38. if (fInitialValue) {
  39. --fWriteCount;
  40. }
  41. SkASSERT(!fReadCount && !fWriteCount);
  42. }
  43. virtual String description() const override {
  44. return fModifiers.description() + fType.fName + " " + fName;
  45. }
  46. bool dead() const {
  47. if ((fStorage != kLocal_Storage && fReadCount) ||
  48. (fModifiers.fFlags & (Modifiers::kIn_Flag | Modifiers::kOut_Flag |
  49. Modifiers::kUniform_Flag))) {
  50. return false;
  51. }
  52. return !fWriteCount ||
  53. (!fReadCount && !(fModifiers.fFlags & (Modifiers::kPLS_Flag |
  54. Modifiers::kPLSOut_Flag)));
  55. }
  56. mutable Modifiers fModifiers;
  57. const Type& fType;
  58. const Storage fStorage;
  59. Expression* fInitialValue = nullptr;
  60. // Tracks how many sites read from the variable. If this is zero for a non-out variable (or
  61. // becomes zero during optimization), the variable is dead and may be eliminated.
  62. mutable int fReadCount;
  63. // Tracks how many sites write to the variable. If this is zero, the variable is dead and may be
  64. // eliminated.
  65. mutable int fWriteCount;
  66. typedef Symbol INHERITED;
  67. };
  68. } // namespace SkSL
  69. #endif