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