blob: 691bea123ad58936923f7e2b3933283fb7f98adf [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)".
20 */
21struct Constructor : public Expression {
Greg Daniel64773e62016-11-22 09:44:03 -050022 Constructor(Position position, const Type& type,
ethannicholasb3058bd2016-07-01 08:22:01 -070023 std::vector<std::unique_ptr<Expression>> arguments)
ethannicholasd598f792016-07-25 10:08:54 -070024 : INHERITED(position, kConstructor_Kind, type)
ethannicholasb3058bd2016-07-01 08:22:01 -070025 , fArguments(std::move(arguments)) {}
26
Ethan Nicholas86a43402017-01-19 13:32:00 -050027 virtual std::unique_ptr<Expression> constantPropagate(
28 const IRGenerator& irGenerator,
29 const DefinitionMap& definitions) override {
30 if (fArguments.size() == 1 && fArguments[0]->fKind == Expression::kIntLiteral_Kind &&
31 // promote float(1) to 1.0
32 fType == *irGenerator.fContext.fFloat_Type) {
33 int64_t intValue = ((IntLiteral&) *fArguments[0]).fValue;
34 return std::unique_ptr<Expression>(new FloatLiteral(irGenerator.fContext,
35 fPosition,
36 intValue));
37 }
38 return nullptr;
39 }
40
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050041 SkString description() const override {
42 SkString result = fType.description() + "(";
43 SkString separator;
ethannicholasb3058bd2016-07-01 08:22:01 -070044 for (size_t i = 0; i < fArguments.size(); i++) {
45 result += separator;
46 result += fArguments[i]->description();
47 separator = ", ";
48 }
49 result += ")";
50 return result;
51 }
52
53 bool isConstant() const override {
54 for (size_t i = 0; i < fArguments.size(); i++) {
55 if (!fArguments[i]->isConstant()) {
56 return false;
57 }
58 }
59 return true;
60 }
61
Ethan Nicholas86a43402017-01-19 13:32:00 -050062 std::vector<std::unique_ptr<Expression>> fArguments;
ethannicholasb3058bd2016-07-01 08:22:01 -070063
64 typedef Expression INHERITED;
65};
66
67} // namespace
68
69#endif