blob: de44b1adb28b1444410863bd1844ad584547135f [file] [log] [blame]
ethannicholasb3058bd2016-07-01 08:22:01 -07001/*
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 */
Ethan Nicholas0df1b042017-03-31 13:56:23 -04007
ethannicholasb3058bd2016-07-01 08:22:01 -07008#ifndef SKSL_INDEX
9#define SKSL_INDEX
10
ethannicholas22f939e2016-10-13 13:25:34 -070011#include "SkSLContext.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070012#include "SkSLExpression.h"
13#include "SkSLUtil.h"
14
15namespace SkSL {
16
17/**
18 * Given a type, returns the type that will result from extracting an array value from it.
19 */
ethannicholasd598f792016-07-25 10:08:54 -070020static const Type& index_type(const Context& context, const Type& type) {
ethannicholasb3058bd2016-07-01 08:22:01 -070021 if (type.kind() == Type::kMatrix_Kind) {
ethannicholasd598f792016-07-25 10:08:54 -070022 if (type.componentType() == *context.fFloat_Type) {
ethannicholas5961bc92016-10-12 06:39:56 -070023 switch (type.rows()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040024 case 2: return *context.fFloat2_Type;
25 case 3: return *context.fFloat3_Type;
26 case 4: return *context.fFloat4_Type;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -040027 default: SkASSERT(false);
ethannicholasb3058bd2016-07-01 08:22:01 -070028 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -040029 } else if (type.componentType() == *context.fHalf_Type) {
30 switch (type.rows()) {
31 case 2: return *context.fHalf2_Type;
32 case 3: return *context.fHalf3_Type;
33 case 4: return *context.fHalf4_Type;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -040034 default: SkASSERT(false);
Ethan Nicholasf7b88202017-09-18 14:10:39 -040035 }
ethannicholasb3058bd2016-07-01 08:22:01 -070036 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -040037 SkASSERT(type.componentType() == *context.fDouble_Type);
ethannicholas5961bc92016-10-12 06:39:56 -070038 switch (type.rows()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040039 case 2: return *context.fDouble2_Type;
40 case 3: return *context.fDouble3_Type;
41 case 4: return *context.fDouble4_Type;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -040042 default: SkASSERT(false);
ethannicholasb3058bd2016-07-01 08:22:01 -070043 }
44 }
45 }
46 return type.componentType();
47}
48
49/**
50 * An expression which extracts a value from an array or matrix, as in 'm[2]'.
51 */
52struct IndexExpression : public Expression {
Ethan Nicholas0df1b042017-03-31 13:56:23 -040053 IndexExpression(const Context& context, std::unique_ptr<Expression> base,
ethannicholasd598f792016-07-25 10:08:54 -070054 std::unique_ptr<Expression> index)
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070055 : INHERITED(base->fOffset, kIndex_Kind, index_type(context, base->fType))
ethannicholasb3058bd2016-07-01 08:22:01 -070056 , fBase(std::move(base))
57 , fIndex(std::move(index)) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -040058 SkASSERT(fIndex->fType == *context.fInt_Type || fIndex->fType == *context.fUInt_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -070059 }
60
Ethan Nicholascb670962017-04-20 19:31:52 -040061 bool hasSideEffects() const override {
62 return fBase->hasSideEffects() || fIndex->hasSideEffects();
63 }
64
Ethan Nicholas0df1b042017-03-31 13:56:23 -040065 String description() const override {
ethannicholasb3058bd2016-07-01 08:22:01 -070066 return fBase->description() + "[" + fIndex->description() + "]";
67 }
68
Ethan Nicholas86a43402017-01-19 13:32:00 -050069 std::unique_ptr<Expression> fBase;
70 std::unique_ptr<Expression> fIndex;
ethannicholasb3058bd2016-07-01 08:22:01 -070071
72 typedef Expression INHERITED;
73};
74
75} // namespace
76
77#endif