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_EXPRESSION
      9 #define SKSL_EXPRESSION
     10 
     11 #include "SkSLType.h"
     12 #include "SkSLVariable.h"
     13 
     14 #include <unordered_map>
     15 
     16 namespace SkSL {
     17 
     18 struct Expression;
     19 class IRGenerator;
     20 
     21 typedef std::unordered_map<const Variable*, std::unique_ptr<Expression>*> DefinitionMap;
     22 
     23 /**
     24  * Abstract supertype of all expressions.
     25  */
     26 struct Expression : public IRNode {
     27     enum Kind {
     28         kBinary_Kind,
     29         kBoolLiteral_Kind,
     30         kConstructor_Kind,
     31         kIntLiteral_Kind,
     32         kFieldAccess_Kind,
     33         kFloatLiteral_Kind,
     34         kFunctionReference_Kind,
     35         kFunctionCall_Kind,
     36         kIndex_Kind,
     37         kPrefix_Kind,
     38         kPostfix_Kind,
     39         kSwizzle_Kind,
     40         kVariableReference_Kind,
     41         kTernary_Kind,
     42         kTypeReference_Kind,
     43         kDefined_Kind
     44     };
     45 
     46     Expression(Position position, Kind kind, const Type& type)
     47     : INHERITED(position)
     48     , fKind(kind)
     49     , fType(std::move(type)) {}
     50 
     51     virtual bool isConstant() const {
     52         return false;
     53     }
     54 
     55     /**
     56      * Given a map of known constant variable values, substitute them in for references to those
     57      * variables occurring in this expression and its subexpressions.  Similar simplifications, such
     58      * as folding a constant binary expression down to a single value, may also be performed.
     59      * Returns a new expression which replaces this expression, or null if no replacements were
     60      * made. If a new expression is returned, this expression is no longer valid.
     61      */
     62     virtual std::unique_ptr<Expression> constantPropagate(const IRGenerator& irGenerator,
     63                                                           const DefinitionMap& definitions) {
     64         return nullptr;
     65     }
     66 
     67     const Kind fKind;
     68     const Type& fType;
     69 
     70     typedef IRNode INHERITED;
     71 };
     72 
     73 } // namespace
     74 
     75 #endif
     76