blob: 697e5dbae94b524220aba202c84b64f143b6642e [file] [log] [blame]
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -04001/*
2 * Copyright 2018 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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/sksl/ir/SkSLVariableReference.h"
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/sksl/SkSLIRGenerator.h"
11#include "src/sksl/ir/SkSLConstructor.h"
12#include "src/sksl/ir/SkSLFloatLiteral.h"
13#include "src/sksl/ir/SkSLSetting.h"
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -040014
15namespace SkSL {
16
Brian Osman79457ef2020-09-24 15:01:27 -040017VariableReference::VariableReference(int offset, const Variable* variable, RefKind refKind)
Ethan Nicholas78686922020-10-08 06:46:27 -040018 : INHERITED(offset, VariableReferenceData{variable, (int8_t)refKind}) {
19 SkASSERT(this->variable());
20 this->variable()->referenceCreated(refKind);
Brian Osman26838062020-09-30 13:03:33 -040021}
22
23VariableReference::~VariableReference() {
Ethan Nicholas78686922020-10-08 06:46:27 -040024 this->variable()->referenceDestroyed(this->refKind());
25}
26
27const Type& VariableReference::type() const {
28 return this->variableReferenceData().fVariable->type();
Brian Osman26838062020-09-30 13:03:33 -040029}
30
Ethan Nicholas041fd0a2020-10-07 16:42:04 -040031bool VariableReference::hasProperty(Property property) const {
32 switch (property) {
33 case Property::kSideEffects: return false;
Ethan Nicholas78686922020-10-08 06:46:27 -040034 case Property::kContainsRTAdjust: return this->variable()->name() == "sk_RTAdjust";
Ethan Nicholas041fd0a2020-10-07 16:42:04 -040035 default:
36 SkASSERT(false);
37 return false;
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -040038 }
39}
40
Ethan Nicholas041fd0a2020-10-07 16:42:04 -040041bool VariableReference::isConstantOrUniform() const {
Ethan Nicholas78686922020-10-08 06:46:27 -040042 return (this->variable()->modifiers().fFlags & Modifiers::kUniform_Flag) != 0;
Ethan Nicholas041fd0a2020-10-07 16:42:04 -040043}
44
45String VariableReference::description() const {
Ethan Nicholas78686922020-10-08 06:46:27 -040046 return this->variable()->name();
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -040047}
48
49void VariableReference::setRefKind(RefKind refKind) {
Ethan Nicholas78686922020-10-08 06:46:27 -040050 this->variable()->referenceDestroyed(this->refKind());
51 this->variableReferenceData().fRefKind = refKind;
52 this->variable()->referenceCreated(this->refKind());
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -040053}
54
Brian Osman8e2ef022020-09-30 13:26:43 -040055void VariableReference::setVariable(const Variable* variable) {
Ethan Nicholas78686922020-10-08 06:46:27 -040056 this->variable()->referenceDestroyed(this->refKind());
57 this->variableReferenceData().fVariable = variable;
58 this->variable()->referenceCreated(this->refKind());
Brian Osman8e2ef022020-09-30 13:26:43 -040059}
60
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -040061std::unique_ptr<Expression> VariableReference::constantPropagate(const IRGenerator& irGenerator,
62 const DefinitionMap& definitions) {
Ethan Nicholas78686922020-10-08 06:46:27 -040063 if (this->refKind() != kRead_RefKind) {
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -040064 return nullptr;
65 }
Ethan Nicholas78686922020-10-08 06:46:27 -040066 const Expression* initialValue = this->variable()->initialValue();
67 if ((this->variable()->modifiers().fFlags & Modifiers::kConst_Flag) && initialValue &&
Ethan Nicholas041fd0a2020-10-07 16:42:04 -040068 initialValue->isCompileTimeConstant() &&
Ethan Nicholas30d30222020-09-11 12:27:26 -040069 this->type().typeKind() != Type::TypeKind::kArray) {
Ethan Nicholas041fd0a2020-10-07 16:42:04 -040070 return initialValue->clone();
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -040071 }
Ethan Nicholas78686922020-10-08 06:46:27 -040072 auto exprIter = definitions.find(this->variable());
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -040073 if (exprIter != definitions.end() && exprIter->second &&
Brian Osmanb6b95732020-06-30 11:44:27 -040074 (*exprIter->second)->isCompileTimeConstant()) {
John Stiles8614e492020-09-21 09:28:48 -040075 return (*exprIter->second)->clone();
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -040076 }
77 return nullptr;
78}
79
John Stilesa6841be2020-08-06 14:11:56 -040080} // namespace SkSL