• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         kInterfaceBlock_Storage,
29         kLocal_Storage,
30         kParameter_Storage
31     };
32 
33     Variable(int offset, Modifiers modifiers, StringFragment name, const Type& type,
34              Storage storage, Expression* initialValue = nullptr)
INHERITEDVariable35     : INHERITED(offset, kVariable_Kind, name)
36     , fModifiers(modifiers)
37     , fType(type)
38     , fStorage(storage)
39     , fInitialValue(initialValue)
40     , fReadCount(0)
41     , fWriteCount(initialValue ? 1 : 0) {}
42 
~VariableVariable43     ~Variable() override {
44         // can't destroy a variable while there are remaining references to it
45         if (fInitialValue) {
46             --fWriteCount;
47         }
48         SkASSERT(!fReadCount && !fWriteCount);
49     }
50 
descriptionVariable51     virtual String description() const override {
52         return fModifiers.description() + fType.fName + " " + fName;
53     }
54 
deadVariable55     bool dead() const {
56         return !fWriteCount || (!fReadCount && !(fModifiers.fFlags & (Modifiers::kOut_Flag |
57                                                                       Modifiers::kPLS_Flag |
58                                                                       Modifiers::kPLSOut_Flag)));
59     }
60 
61     mutable Modifiers fModifiers;
62     const Type& fType;
63     const Storage fStorage;
64 
65     Expression* fInitialValue = nullptr;
66 
67     // Tracks how many sites read from the variable. If this is zero for a non-out variable (or
68     // becomes zero during optimization), the variable is dead and may be eliminated.
69     mutable int fReadCount;
70     // Tracks how many sites write to the variable. If this is zero, the variable is dead and may be
71     // eliminated.
72     mutable int fWriteCount;
73 
74     typedef Symbol INHERITED;
75 };
76 
77 } // namespace SkSL
78 
79 #endif
80