ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 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 | |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 11 | #include "SkSLContext.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 12 | #include "SkSLExpression.h" |
| 13 | #include "SkSLUtil.h" |
| 14 | |
| 15 | namespace SkSL { |
| 16 | |
| 17 | /** |
| 18 | * Given a type, returns the type that will result from extracting an array value from it. |
| 19 | */ |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 20 | static const Type& index_type(const Context& context, const Type& type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 21 | if (type.kind() == Type::kMatrix_Kind) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 22 | if (type.componentType() == *context.fFloat_Type) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 23 | switch (type.rows()) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 24 | case 2: return *context.fVec2_Type; |
| 25 | case 3: return *context.fVec3_Type; |
| 26 | case 4: return *context.fVec4_Type; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 27 | default: ASSERT(false); |
| 28 | } |
| 29 | } else { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 30 | ASSERT(type.componentType() == *context.fDouble_Type); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 31 | switch (type.rows()) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 32 | case 2: return *context.fDVec2_Type; |
| 33 | case 3: return *context.fDVec3_Type; |
| 34 | case 4: return *context.fDVec4_Type; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 35 | default: ASSERT(false); |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | return type.componentType(); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * An expression which extracts a value from an array or matrix, as in 'm[2]'. |
| 44 | */ |
| 45 | struct IndexExpression : public Expression { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 46 | IndexExpression(const Context& context, std::unique_ptr<Expression> base, |
| 47 | std::unique_ptr<Expression> index) |
| 48 | : INHERITED(base->fPosition, kIndex_Kind, index_type(context, base->fType)) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 49 | , fBase(std::move(base)) |
| 50 | , fIndex(std::move(index)) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 51 | ASSERT(fIndex->fType == *context.fInt_Type || fIndex->fType == *context.fUInt_Type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Greg Daniel | 792d0f1 | 2016-11-20 14:53:35 +0000 | [diff] [blame^] | 54 | std::string description() const override { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 55 | return fBase->description() + "[" + fIndex->description() + "]"; |
| 56 | } |
| 57 | |
| 58 | const std::unique_ptr<Expression> fBase; |
| 59 | const std::unique_ptr<Expression> fIndex; |
| 60 | |
| 61 | typedef Expression INHERITED; |
| 62 | }; |
| 63 | |
| 64 | } // namespace |
| 65 | |
| 66 | #endif |