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 /** 19 * Represents a variable, whether local, global, or a function parameter. This represents the 20 * variable itself (the storage location), which is shared between all VariableReferences which 21 * read or write that storage location. 22 */ 23 struct Variable : public Symbol { 24 enum Storage { 25 kGlobal_Storage, 26 kLocal_Storage, 27 kParameter_Storage 28 }; 29 VariableVariable30 Variable(Position position, Modifiers modifiers, String name, const Type& type, 31 Storage storage) 32 : INHERITED(position, kVariable_Kind, std::move(name)) 33 , fModifiers(modifiers) 34 , fType(type) 35 , fStorage(storage) 36 , fReadCount(0) 37 , fWriteCount(0) {} 38 descriptionVariable39 virtual String description() const override { 40 return fModifiers.description() + fType.fName + " " + fName; 41 } 42 deadVariable43 bool dead() const { 44 return !fWriteCount || (!fReadCount && !(fModifiers.fFlags & Modifiers::kOut_Flag)); 45 } 46 47 mutable Modifiers fModifiers; 48 const Type& fType; 49 const Storage fStorage; 50 51 // Tracks how many sites read from the variable. If this is zero for a non-out variable (or 52 // becomes zero during optimization), the variable is dead and may be eliminated. 53 mutable int fReadCount; 54 // Tracks how many sites write to the variable. If this is zero, the variable is dead and may be 55 // eliminated. 56 mutable int fWriteCount; 57 58 typedef Symbol INHERITED; 59 }; 60 61 } // namespace SkSL 62 63 #endif 64