Line data Source code
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 "SkSLContext.h"
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 : */
20 0 : static const Type& index_type(const Context& context, const Type& type) {
21 0 : if (type.kind() == Type::kMatrix_Kind) {
22 0 : if (type.componentType() == *context.fFloat_Type) {
23 0 : switch (type.rows()) {
24 0 : case 2: return *context.fVec2_Type;
25 0 : case 3: return *context.fVec3_Type;
26 0 : case 4: return *context.fVec4_Type;
27 0 : default: ASSERT(false);
28 : }
29 : } else {
30 0 : ASSERT(type.componentType() == *context.fDouble_Type);
31 0 : switch (type.rows()) {
32 0 : case 2: return *context.fDVec2_Type;
33 0 : case 3: return *context.fDVec3_Type;
34 0 : case 4: return *context.fDVec4_Type;
35 0 : default: ASSERT(false);
36 : }
37 : }
38 : }
39 0 : return type.componentType();
40 : }
41 :
42 : /**
43 : * An expression which extracts a value from an array or matrix, as in 'm[2]'.
44 : */
45 0 : struct IndexExpression : public Expression {
46 0 : IndexExpression(const Context& context, std::unique_ptr<Expression> base,
47 : std::unique_ptr<Expression> index)
48 0 : : INHERITED(base->fPosition, kIndex_Kind, index_type(context, base->fType))
49 0 : , fBase(std::move(base))
50 0 : , fIndex(std::move(index)) {
51 0 : ASSERT(fIndex->fType == *context.fInt_Type || fIndex->fType == *context.fUInt_Type);
52 0 : }
53 :
54 0 : String description() const override {
55 0 : return fBase->description() + "[" + fIndex->description() + "]";
56 : }
57 :
58 : std::unique_ptr<Expression> fBase;
59 : std::unique_ptr<Expression> fIndex;
60 :
61 : typedef Expression INHERITED;
62 : };
63 :
64 : } // namespace
65 :
66 : #endif
|