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_CONSTRUCTOR
9 : #define SKSL_CONSTRUCTOR
10 :
11 : #include "SkSLExpression.h"
12 : #include "SkSLFloatLiteral.h"
13 : #include "SkSLIntLiteral.h"
14 : #include "SkSLIRGenerator.h"
15 :
16 : namespace SkSL {
17 :
18 : /**
19 : * Represents the construction of a compound type, such as "vec2(x, y)".
20 : *
21 : * Vector constructors will always consist of either exactly 1 scalar, or a collection of vectors
22 : * and scalars totalling exactly the right number of scalar components.
23 : *
24 : * Matrix constructors will always consist of either exactly 1 scalar, exactly 1 matrix, or a
25 : * collection of vectors and scalars totalling exactly the right number of scalar components.
26 : */
27 0 : struct Constructor : public Expression {
28 0 : Constructor(Position position, const Type& type,
29 : std::vector<std::unique_ptr<Expression>> arguments)
30 0 : : INHERITED(position, kConstructor_Kind, type)
31 0 : , fArguments(std::move(arguments)) {}
32 :
33 0 : virtual std::unique_ptr<Expression> constantPropagate(
34 : const IRGenerator& irGenerator,
35 : const DefinitionMap& definitions) override {
36 0 : if (fArguments.size() == 1 && fArguments[0]->fKind == Expression::kIntLiteral_Kind &&
37 : // promote float(1) to 1.0
38 0 : fType == *irGenerator.fContext.fFloat_Type) {
39 0 : int64_t intValue = ((IntLiteral&) *fArguments[0]).fValue;
40 : return std::unique_ptr<Expression>(new FloatLiteral(irGenerator.fContext,
41 0 : fPosition,
42 0 : intValue));
43 : }
44 0 : return nullptr;
45 : }
46 :
47 0 : String description() const override {
48 0 : String result = fType.description() + "(";
49 0 : String separator;
50 0 : for (size_t i = 0; i < fArguments.size(); i++) {
51 0 : result += separator;
52 0 : result += fArguments[i]->description();
53 0 : separator = ", ";
54 : }
55 0 : result += ")";
56 0 : return result;
57 : }
58 :
59 0 : bool isConstant() const override {
60 0 : for (size_t i = 0; i < fArguments.size(); i++) {
61 0 : if (!fArguments[i]->isConstant()) {
62 0 : return false;
63 : }
64 : }
65 0 : return true;
66 : }
67 :
68 : std::vector<std::unique_ptr<Expression>> fArguments;
69 :
70 : typedef Expression INHERITED;
71 : };
72 :
73 : } // namespace
74 :
75 : #endif
|