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_VARIABLE 9 #define SKSL_VARIABLE 10 11 #include "include/private/SkSLModifiers.h" 12 #include "include/private/SkSLSymbol.h" 13 #include "src/sksl/SkSLPosition.h" 14 #include "src/sksl/ir/SkSLExpression.h" 15 #include "src/sksl/ir/SkSLType.h" 16 #include "src/sksl/ir/SkSLVariableReference.h" 17 18 namespace SkSL { 19 20 class Expression; 21 class VarDeclaration; 22 23 namespace dsl { 24 class DSLCore; 25 class DSLFunction; 26 } // namespace dsl 27 28 enum class VariableStorage : int8_t { 29 kGlobal, 30 kInterfaceBlock, 31 kLocal, 32 kParameter 33 }; 34 35 /** 36 * Represents a variable, whether local, global, or a function parameter. This represents the 37 * variable itself (the storage location), which is shared between all VariableReferences which 38 * read or write that storage location. 39 */ 40 class Variable final : public Symbol { 41 public: 42 using Storage = VariableStorage; 43 44 static constexpr Kind kSymbolKind = Kind::kVariable; 45 Variable(int offset,const Modifiers * modifiers,StringFragment name,const Type * type,bool builtin,Storage storage)46 Variable(int offset, const Modifiers* modifiers, StringFragment name, const Type* type, 47 bool builtin, Storage storage) 48 : INHERITED(offset, kSymbolKind, name, type) 49 , fModifiers(modifiers) 50 , fStorage(storage) 51 , fBuiltin(builtin) {} 52 53 ~Variable() override; 54 modifiers()55 const Modifiers& modifiers() const { 56 return *fModifiers; 57 } 58 setModifiers(const Modifiers * modifiers)59 void setModifiers(const Modifiers* modifiers) { 60 fModifiers = modifiers; 61 } 62 isBuiltin()63 bool isBuiltin() const { 64 return fBuiltin; 65 } 66 storage()67 Storage storage() const { 68 return (Storage) fStorage; 69 } 70 71 const Expression* initialValue() const; 72 setDeclaration(VarDeclaration * declaration)73 void setDeclaration(VarDeclaration* declaration) { 74 SkASSERT(!fDeclaration); 75 fDeclaration = declaration; 76 } 77 detachDeadVarDeclaration()78 void detachDeadVarDeclaration() const { 79 // The VarDeclaration is being deleted, so our reference to it has become stale. 80 // This variable is now dead, so it shouldn't matter that we are modifying its symbol. 81 const_cast<Variable*>(this)->fDeclaration = nullptr; 82 } 83 description()84 String description() const override { 85 return this->modifiers().description() + this->type().name() + " " + this->name(); 86 } 87 88 private: 89 VarDeclaration* fDeclaration = nullptr; 90 const Modifiers* fModifiers; 91 VariableStorage fStorage; 92 bool fBuiltin; 93 94 using INHERITED = Symbol; 95 96 friend class dsl::DSLCore; 97 friend class dsl::DSLFunction; 98 friend class VariableReference; 99 }; 100 101 } // namespace SkSL 102 103 #endif 104