| 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_INDEX |
| 9 |
#define SKSL_INDEX |
| 10 |
|
| 11 |
#include "src/sksl/SkSLPosition.h" |
| 12 |
#include "src/sksl/ir/SkSLExpression.h" |
| 13 |
#include "src/sksl/ir/SkSLIRNode.h" |
| 14 |
|
| 15 |
#include <cstdint> |
| 16 |
#include <memory> |
| 17 |
#include <string> |
| 18 |
#include <utility> |
| 19 |
|
| 20 |
namespace SkSL { |
| 21 |
|
| 22 |
class Context; |
| 23 |
class Type; |
| 24 |
enum class OperatorPrecedence : uint8_t; |
| 25 |
|
| 26 |
/** |
| 27 |
* An expression which extracts a value from an array, vector or matrix, as in 'm[2]'. |
| 28 |
*/ |
| 29 |
class IndexExpression final : public Expression { |
| 30 |
public: |
| 31 |
inline static constexpr Kind kIRNodeKind = Kind::kIndex; |
| 32 |
|
| 33 |
IndexExpression(const Context& context, Position pos, std::unique_ptr<Expression> base, |
| 34 |
std::unique_ptr<Expression> index) |
| 35 |
: INHERITED(pos, kIRNodeKind, &IndexType(context, base->type())) |
| 36 |
, fBase(std::move(base)) |
| 37 |
, fIndex(std::move(index)) {} |
| 38 |
|
| 39 |
// Returns a simplified index-expression; reports errors via the ErrorReporter. |
| 40 |
static std::unique_ptr<Expression> Convert(const Context& context, |
| 41 |
Position pos, |
| 42 |
std::unique_ptr<Expression> base, |
| 43 |
std::unique_ptr<Expression> index); |
| 44 |
|
| 45 |
// Returns a simplified index-expression; reports errors via ASSERT. |
| 46 |
static std::unique_ptr<Expression> Make(const Context& context, |
| 47 |
Position pos, |
| 48 |
std::unique_ptr<Expression> base, |
| 49 |
std::unique_ptr<Expression> index); |
| 50 |
|
| 51 |
/** |
| 52 |
* Given a type, returns the type that will result from extracting an array value from it. |
| 53 |
*/ |
| 54 |
static const Type& IndexType(const Context& context, const Type& type); |
| 55 |
|
| 56 |
std::unique_ptr<Expression>& base() { |
| 57 |
return fBase; |
| 58 |
} |
| 59 |
|
| 60 |
const std::unique_ptr<Expression>& base() const { |
| 61 |
return fBase; |
| 62 |
} |
| 63 |
|
| 64 |
std::unique_ptr<Expression>& index() { |
| 65 |
return fIndex; |
| 66 |
} |
| 67 |
|
| 68 |
const std::unique_ptr<Expression>& index() const { |
| 69 |
return fIndex; |
| 70 |
} |
| 71 |
|
| 72 |
std::unique_ptr<Expression> clone(Position pos) const override { |
| 73 |
return std::unique_ptr<Expression>(new IndexExpression(pos, this->base()->clone(), |
| 74 |
this->index()->clone(), |
| 75 |
&this->type())); |
| 76 |
} |
| 77 |
|
| 78 |
std::string description(OperatorPrecedence) const override; |
| 79 |
|
| 80 |
using INHERITED = Expression; |
| 81 |
|
| 82 |
private: |
| 83 |
IndexExpression(Position pos, std::unique_ptr<Expression> base, |
| 84 |
std::unique_ptr<Expression> index, const Type* type) |
| 85 |
: INHERITED(pos, Kind::kIndex, type) |
| 86 |
, fBase(std::move(base)) |
| 87 |
, fIndex(std::move(index)) {} |
| 88 |
|
| 89 |
std::unique_ptr<Expression> fBase; |
| 90 |
std::unique_ptr<Expression> fIndex; |
| 91 |
}; |
| 92 |
|
| 93 |
} // namespace SkSL |
| 94 |
|
| 95 |
#endif |