blob: 3360ace78eba2689c04b6e01b0139f3c6d08ec79 [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 */
Greg Daniel64773e62016-11-22 09:44:03 -05007
ethannicholasb3058bd2016-07-01 08:22:01 -07008#ifndef SKSL_CONSTRUCTOR
9#define SKSL_CONSTRUCTOR
10
11#include "SkSLExpression.h"
Ethan Nicholas86a43402017-01-19 13:32:00 -050012#include "SkSLFloatLiteral.h"
13#include "SkSLIntLiteral.h"
14#include "SkSLIRGenerator.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070015
16namespace SkSL {
17
18/**
19 * Represents the construction of a compound type, such as "vec2(x, y)".
Ethan Nicholas84645e32017-02-09 13:57:14 -050020 *
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.
ethannicholasb3058bd2016-07-01 08:22:01 -070026 */
27struct Constructor : public Expression {
Greg Daniel64773e62016-11-22 09:44:03 -050028 Constructor(Position position, const Type& type,
ethannicholasb3058bd2016-07-01 08:22:01 -070029 std::vector<std::unique_ptr<Expression>> arguments)
ethannicholasd598f792016-07-25 10:08:54 -070030 : INHERITED(position, kConstructor_Kind, type)
ethannicholasb3058bd2016-07-01 08:22:01 -070031 , fArguments(std::move(arguments)) {}
32
Ethan Nicholase1d9cb82017-02-06 18:53:07 +000033 virtual std::unique_ptr<Expression> constantPropagate(
34 const IRGenerator& irGenerator,
35 const DefinitionMap& definitions) override {
36 if (fArguments.size() == 1 && fArguments[0]->fKind == Expression::kIntLiteral_Kind &&
37 // promote float(1) to 1.0
38 fType == *irGenerator.fContext.fFloat_Type) {
39 int64_t intValue = ((IntLiteral&) *fArguments[0]).fValue;
40 return std::unique_ptr<Expression>(new FloatLiteral(irGenerator.fContext,
41 fPosition,
42 intValue));
Ethan Nicholas86a43402017-01-19 13:32:00 -050043 }
44 return nullptr;
45 }
46
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050047 SkString description() const override {
48 SkString result = fType.description() + "(";
49 SkString separator;
ethannicholasb3058bd2016-07-01 08:22:01 -070050 for (size_t i = 0; i < fArguments.size(); i++) {
51 result += separator;
52 result += fArguments[i]->description();
53 separator = ", ";
54 }
55 result += ")";
56 return result;
57 }
58
59 bool isConstant() const override {
60 for (size_t i = 0; i < fArguments.size(); i++) {
61 if (!fArguments[i]->isConstant()) {
62 return false;
63 }
64 }
65 return true;
66 }
67
Ethan Nicholas86a43402017-01-19 13:32:00 -050068 std::vector<std::unique_ptr<Expression>> fArguments;
ethannicholasb3058bd2016-07-01 08:22:01 -070069
70 typedef Expression INHERITED;
71};
72
73} // namespace
74
75#endif