Home | History | Annotate | Download | only in ir
      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_VARIABLEREFERENCE
      9 #define SKSL_VARIABLEREFERENCE
     10 
     11 #include "SkSLExpression.h"
     12 
     13 namespace SkSL {
     14 
     15 class IRGenerator;
     16 
     17 /**
     18  * A reference to a variable, through which it can be read or written. In the statement:
     19  *
     20  * x = x + 1;
     21  *
     22  * there is only one Variable 'x', but two VariableReferences to it.
     23  */
     24 struct VariableReference : public Expression {
     25     enum RefKind {
     26         kRead_RefKind,
     27         kWrite_RefKind,
     28         kReadWrite_RefKind,
     29         // taking the address of a variable - we consider this a read & write but don't complain if
     30         // the variable was not previously assigned
     31         kPointer_RefKind
     32     };
     33 
     34     VariableReference(int offset, const Variable& variable, RefKind refKind = kRead_RefKind);
     35 
     36     ~VariableReference() override;
     37 
     38     RefKind refKind() const {
     39         return fRefKind;
     40     }
     41 
     42     void setRefKind(RefKind refKind);
     43 
     44     bool hasSideEffects() const override {
     45         return false;
     46     }
     47 
     48     bool isConstant() const override {
     49         return 0 != (fVariable.fModifiers.fFlags & Modifiers::kConst_Flag);
     50     }
     51 
     52     std::unique_ptr<Expression> clone() const override {
     53         return std::unique_ptr<Expression>(new VariableReference(fOffset, fVariable, fRefKind));
     54     }
     55 
     56     String description() const override {
     57         return fVariable.fName;
     58     }
     59 
     60     static std::unique_ptr<Expression> copy_constant(const IRGenerator& irGenerator,
     61                                                      const Expression* expr);
     62 
     63     std::unique_ptr<Expression> constantPropagate(const IRGenerator& irGenerator,
     64                                                   const DefinitionMap& definitions) override;
     65 
     66     const Variable& fVariable;
     67     RefKind fRefKind;
     68 
     69 private:
     70     typedef Expression INHERITED;
     71 };
     72 
     73 } // namespace
     74 
     75 #endif
     76