blob: abd8a03fa40822971566fe5b4fc22e666ebb5240 [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 */
7
8#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()) {
ethannicholasd598f792016-07-25 10:08:54 -070024 case 2: return *context.fVec2_Type;
25 case 3: return *context.fVec3_Type;
26 case 4: return *context.fVec4_Type;
ethannicholasb3058bd2016-07-01 08:22:01 -070027 default: ASSERT(false);
28 }
29 } else {
ethannicholasd598f792016-07-25 10:08:54 -070030 ASSERT(type.componentType() == *context.fDouble_Type);
ethannicholas5961bc92016-10-12 06:39:56 -070031 switch (type.rows()) {
ethannicholasd598f792016-07-25 10:08:54 -070032 case 2: return *context.fDVec2_Type;
33 case 3: return *context.fDVec3_Type;
34 case 4: return *context.fDVec4_Type;
ethannicholasb3058bd2016-07-01 08:22:01 -070035 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 */
45struct IndexExpression : public Expression {
ethannicholasd598f792016-07-25 10:08:54 -070046 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))
ethannicholasb3058bd2016-07-01 08:22:01 -070049 , fBase(std::move(base))
50 , fIndex(std::move(index)) {
ethannicholas5961bc92016-10-12 06:39:56 -070051 ASSERT(fIndex->fType == *context.fInt_Type || fIndex->fType == *context.fUInt_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -070052 }
53
Greg Daniel792d0f12016-11-20 14:53:35 +000054 std::string description() const override {
ethannicholasb3058bd2016-07-01 08:22:01 -070055 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