Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 Google LLC. |
| 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 | #include "src/sksl/SkSLAnalysis.h" |
| 9 | |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 10 | #include "include/private/SkSLSampleUsage.h" |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 11 | #include "src/sksl/SkSLErrorReporter.h" |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 12 | #include "src/sksl/ir/SkSLExpression.h" |
| 13 | #include "src/sksl/ir/SkSLProgram.h" |
| 14 | #include "src/sksl/ir/SkSLProgramElement.h" |
| 15 | #include "src/sksl/ir/SkSLStatement.h" |
| 16 | |
| 17 | // ProgramElements |
| 18 | #include "src/sksl/ir/SkSLEnum.h" |
| 19 | #include "src/sksl/ir/SkSLExtension.h" |
| 20 | #include "src/sksl/ir/SkSLFunctionDefinition.h" |
| 21 | #include "src/sksl/ir/SkSLInterfaceBlock.h" |
| 22 | #include "src/sksl/ir/SkSLModifiers.h" |
| 23 | #include "src/sksl/ir/SkSLSection.h" |
| 24 | #include "src/sksl/ir/SkSLVarDeclarations.h" |
| 25 | |
| 26 | // Statements |
| 27 | #include "src/sksl/ir/SkSLBlock.h" |
| 28 | #include "src/sksl/ir/SkSLBreakStatement.h" |
| 29 | #include "src/sksl/ir/SkSLContinueStatement.h" |
| 30 | #include "src/sksl/ir/SkSLDiscardStatement.h" |
| 31 | #include "src/sksl/ir/SkSLDoStatement.h" |
| 32 | #include "src/sksl/ir/SkSLExpressionStatement.h" |
| 33 | #include "src/sksl/ir/SkSLForStatement.h" |
| 34 | #include "src/sksl/ir/SkSLIfStatement.h" |
| 35 | #include "src/sksl/ir/SkSLNop.h" |
| 36 | #include "src/sksl/ir/SkSLReturnStatement.h" |
| 37 | #include "src/sksl/ir/SkSLSwitchStatement.h" |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 38 | #include "src/sksl/ir/SkSLWhileStatement.h" |
| 39 | |
| 40 | // Expressions |
| 41 | #include "src/sksl/ir/SkSLBinaryExpression.h" |
| 42 | #include "src/sksl/ir/SkSLBoolLiteral.h" |
| 43 | #include "src/sksl/ir/SkSLConstructor.h" |
| 44 | #include "src/sksl/ir/SkSLExternalFunctionCall.h" |
| 45 | #include "src/sksl/ir/SkSLExternalValueReference.h" |
| 46 | #include "src/sksl/ir/SkSLFieldAccess.h" |
| 47 | #include "src/sksl/ir/SkSLFloatLiteral.h" |
| 48 | #include "src/sksl/ir/SkSLFunctionCall.h" |
| 49 | #include "src/sksl/ir/SkSLFunctionReference.h" |
| 50 | #include "src/sksl/ir/SkSLIndexExpression.h" |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 51 | #include "src/sksl/ir/SkSLInlineMarker.h" |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 52 | #include "src/sksl/ir/SkSLIntLiteral.h" |
| 53 | #include "src/sksl/ir/SkSLNullLiteral.h" |
| 54 | #include "src/sksl/ir/SkSLPostfixExpression.h" |
| 55 | #include "src/sksl/ir/SkSLPrefixExpression.h" |
| 56 | #include "src/sksl/ir/SkSLSetting.h" |
| 57 | #include "src/sksl/ir/SkSLSwizzle.h" |
| 58 | #include "src/sksl/ir/SkSLTernaryExpression.h" |
| 59 | #include "src/sksl/ir/SkSLTypeReference.h" |
| 60 | #include "src/sksl/ir/SkSLVariableReference.h" |
| 61 | |
| 62 | namespace SkSL { |
| 63 | |
| 64 | namespace { |
| 65 | |
| 66 | static bool is_sample_call_to_fp(const FunctionCall& fc, const Variable& fp) { |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 67 | const FunctionDeclaration& f = fc.function(); |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 68 | return f.isBuiltin() && f.name() == "sample" && fc.arguments().size() >= 1 && |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 69 | fc.arguments()[0]->is<VariableReference>() && |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 70 | fc.arguments()[0]->as<VariableReference>().variable() == &fp; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 71 | } |
| 72 | |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 73 | // Visitor that determines the merged SampleUsage for a given child 'fp' in the program. |
| 74 | class MergeSampleUsageVisitor : public ProgramVisitor { |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 75 | public: |
John Stiles | 933abe3 | 2020-08-28 11:58:40 -0400 | [diff] [blame] | 76 | MergeSampleUsageVisitor(const Context& context, const Variable& fp) |
| 77 | : fContext(context), fFP(fp) {} |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 78 | |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 79 | SampleUsage visit(const Program& program) { |
| 80 | fUsage = SampleUsage(); // reset to none |
John Stiles | d7ab450 | 2020-09-24 22:41:00 -0400 | [diff] [blame] | 81 | INHERITED::visit(program); |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 82 | return fUsage; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | protected: |
John Stiles | 933abe3 | 2020-08-28 11:58:40 -0400 | [diff] [blame] | 86 | const Context& fContext; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 87 | const Variable& fFP; |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 88 | SampleUsage fUsage; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 89 | |
| 90 | bool visitExpression(const Expression& e) override { |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 91 | // Looking for sample(fp, inColor?, ...) |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 92 | if (e.kind() == Expression::Kind::kFunctionCall) { |
John Stiles | 403a363 | 2020-08-20 12:11:48 -0400 | [diff] [blame] | 93 | const FunctionCall& fc = e.as<FunctionCall>(); |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 94 | if (is_sample_call_to_fp(fc, fFP)) { |
| 95 | // Determine the type of call at this site, and merge it with the accumulated state |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 96 | const Expression* lastArg = fc.arguments().back().get(); |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 97 | |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 98 | if (lastArg->type() == *fContext.fFloat2_Type) { |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 99 | fUsage.merge(SampleUsage::Explicit()); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 100 | } else if (lastArg->type() == *fContext.fFloat3x3_Type) { |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 101 | // Determine the type of matrix for this call site |
| 102 | if (lastArg->isConstantOrUniform()) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 103 | if (lastArg->kind() == Expression::Kind::kVariableReference || |
| 104 | lastArg->kind() == Expression::Kind::kConstructor) { |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 105 | // FIXME if this is a constant, we should parse the float3x3 constructor |
| 106 | // and determine if the resulting matrix introduces perspective. |
| 107 | fUsage.merge(SampleUsage::UniformMatrix(lastArg->description())); |
| 108 | } else { |
| 109 | // FIXME this is really to workaround a restriction of the downstream |
| 110 | // code that relies on the SampleUsage's fExpression to identify uniform |
| 111 | // names. Once they are tracked separately, any uniform expression can |
| 112 | // work, but right now this avoids issues from '0.5 * matrix' that is |
| 113 | // both a constant AND a uniform. |
| 114 | fUsage.merge(SampleUsage::VariableMatrix()); |
| 115 | } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 116 | } else { |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 117 | fUsage.merge(SampleUsage::VariableMatrix()); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 118 | } |
| 119 | } else { |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 120 | // The only other signatures do pass-through sampling |
| 121 | fUsage.merge(SampleUsage::PassThrough()); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 122 | } |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 123 | // NOTE: we don't return true here just because we found a sample call. We need to |
| 124 | // process the entire program and merge across all encountered calls. |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
John Stiles | d7ab450 | 2020-09-24 22:41:00 -0400 | [diff] [blame] | 128 | return INHERITED::visitExpression(e); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 129 | } |
| 130 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 131 | using INHERITED = ProgramVisitor; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 132 | }; |
| 133 | |
Brian Osman | 92aac1e | 2020-08-05 16:48:58 -0400 | [diff] [blame] | 134 | // Visitor that searches through the program for references to a particular builtin variable |
| 135 | class BuiltinVariableVisitor : public ProgramVisitor { |
| 136 | public: |
| 137 | BuiltinVariableVisitor(int builtin) : fBuiltin(builtin) {} |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 138 | |
| 139 | bool visitExpression(const Expression& e) override { |
Brian Osman | 79457ef | 2020-09-24 15:01:27 -0400 | [diff] [blame] | 140 | if (e.is<VariableReference>()) { |
John Stiles | 403a363 | 2020-08-20 12:11:48 -0400 | [diff] [blame] | 141 | const VariableReference& var = e.as<VariableReference>(); |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 142 | return var.variable()->modifiers().fLayout.fBuiltin == fBuiltin; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 143 | } |
John Stiles | d7ab450 | 2020-09-24 22:41:00 -0400 | [diff] [blame] | 144 | return INHERITED::visitExpression(e); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 145 | } |
| 146 | |
Brian Osman | 92aac1e | 2020-08-05 16:48:58 -0400 | [diff] [blame] | 147 | int fBuiltin; |
| 148 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 149 | using INHERITED = ProgramVisitor; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 150 | }; |
| 151 | |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 152 | // Visitor that counts the number of nodes visited |
| 153 | class NodeCountVisitor : public ProgramVisitor { |
| 154 | public: |
John Stiles | 2c1e492 | 2020-10-01 09:14:14 -0400 | [diff] [blame] | 155 | NodeCountVisitor(int limit) : fLimit(limit) {} |
| 156 | |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 157 | int visit(const Statement& s) { |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 158 | this->visitStatement(s); |
| 159 | return fCount; |
| 160 | } |
| 161 | |
| 162 | bool visitExpression(const Expression& e) override { |
| 163 | ++fCount; |
John Stiles | 9b9415e | 2020-11-23 14:48:06 -0500 | [diff] [blame^] | 164 | return (fCount >= fLimit) || INHERITED::visitExpression(e); |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | bool visitProgramElement(const ProgramElement& p) override { |
| 168 | ++fCount; |
John Stiles | 9b9415e | 2020-11-23 14:48:06 -0500 | [diff] [blame^] | 169 | return (fCount >= fLimit) || INHERITED::visitProgramElement(p); |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | bool visitStatement(const Statement& s) override { |
| 173 | ++fCount; |
John Stiles | 9b9415e | 2020-11-23 14:48:06 -0500 | [diff] [blame^] | 174 | return (fCount >= fLimit) || INHERITED::visitStatement(s); |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | private: |
John Stiles | 2c1e492 | 2020-10-01 09:14:14 -0400 | [diff] [blame] | 178 | int fCount = 0; |
| 179 | int fLimit; |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 180 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 181 | using INHERITED = ProgramVisitor; |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 182 | }; |
| 183 | |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 184 | class ProgramUsageVisitor : public ProgramVisitor { |
Brian Osman | 2e25ff4 | 2020-10-15 10:32:04 -0400 | [diff] [blame] | 185 | public: |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 186 | ProgramUsageVisitor(ProgramUsage* usage, int delta) : fUsage(usage), fDelta(delta) {} |
| 187 | |
Brian Osman | 2e25ff4 | 2020-10-15 10:32:04 -0400 | [diff] [blame] | 188 | bool visitExpression(const Expression& e) override { |
| 189 | if (e.is<FunctionCall>()) { |
| 190 | const FunctionDeclaration* f = &e.as<FunctionCall>().function(); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 191 | fUsage->fCallCounts[f] += fDelta; |
| 192 | SkASSERT(fUsage->fCallCounts[f] >= 0); |
| 193 | } else if (e.is<VariableReference>()) { |
| 194 | const VariableReference& ref = e.as<VariableReference>(); |
| 195 | ProgramUsage::VariableCounts& counts = fUsage->fVariableCounts[ref.variable()]; |
| 196 | switch (ref.refKind()) { |
| 197 | case VariableRefKind::kRead: |
| 198 | counts.fRead += fDelta; |
| 199 | break; |
| 200 | case VariableRefKind::kWrite: |
| 201 | counts.fWrite += fDelta; |
| 202 | break; |
| 203 | case VariableRefKind::kReadWrite: |
| 204 | case VariableRefKind::kPointer: |
| 205 | counts.fRead += fDelta; |
| 206 | counts.fWrite += fDelta; |
| 207 | break; |
| 208 | } |
| 209 | SkASSERT(counts.fRead >= 0 && counts.fWrite >= 0); |
Brian Osman | 2e25ff4 | 2020-10-15 10:32:04 -0400 | [diff] [blame] | 210 | } |
| 211 | return INHERITED::visitExpression(e); |
| 212 | } |
| 213 | |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 214 | using ProgramVisitor::visitProgramElement; |
| 215 | using ProgramVisitor::visitStatement; |
| 216 | |
| 217 | ProgramUsage* fUsage; |
| 218 | int fDelta; |
Brian Osman | 2e25ff4 | 2020-10-15 10:32:04 -0400 | [diff] [blame] | 219 | using INHERITED = ProgramVisitor; |
| 220 | }; |
| 221 | |
Ethan Nicholas | 765d2fe | 2020-08-26 08:29:55 -0400 | [diff] [blame] | 222 | class VariableWriteVisitor : public ProgramVisitor { |
| 223 | public: |
| 224 | VariableWriteVisitor(const Variable* var) |
| 225 | : fVar(var) {} |
| 226 | |
| 227 | bool visit(const Statement& s) { |
| 228 | return this->visitStatement(s); |
| 229 | } |
| 230 | |
| 231 | bool visitExpression(const Expression& e) override { |
Brian Osman | 79457ef | 2020-09-24 15:01:27 -0400 | [diff] [blame] | 232 | if (e.is<VariableReference>()) { |
Ethan Nicholas | 765d2fe | 2020-08-26 08:29:55 -0400 | [diff] [blame] | 233 | const VariableReference& ref = e.as<VariableReference>(); |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 234 | if (ref.variable() == fVar && |
| 235 | (ref.refKind() == VariableReference::RefKind::kWrite || |
| 236 | ref.refKind() == VariableReference::RefKind::kReadWrite || |
| 237 | ref.refKind() == VariableReference::RefKind::kPointer)) { |
Ethan Nicholas | 765d2fe | 2020-08-26 08:29:55 -0400 | [diff] [blame] | 238 | return true; |
| 239 | } |
| 240 | } |
John Stiles | d7ab450 | 2020-09-24 22:41:00 -0400 | [diff] [blame] | 241 | return INHERITED::visitExpression(e); |
Ethan Nicholas | 765d2fe | 2020-08-26 08:29:55 -0400 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | private: |
| 245 | const Variable* fVar; |
| 246 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 247 | using INHERITED = ProgramVisitor; |
Ethan Nicholas | 765d2fe | 2020-08-26 08:29:55 -0400 | [diff] [blame] | 248 | }; |
| 249 | |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 250 | // If a caller doesn't care about errors, we can use this trivial reporter that just counts up. |
| 251 | class TrivialErrorReporter : public ErrorReporter { |
| 252 | public: |
| 253 | void error(int offset, String) override { ++fErrorCount; } |
| 254 | int errorCount() override { return fErrorCount; } |
| 255 | |
| 256 | private: |
| 257 | int fErrorCount = 0; |
| 258 | }; |
| 259 | |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 260 | // This isn't actually using ProgramVisitor, because it only considers a subset of the fields for |
| 261 | // any given expression kind. For instance, when indexing an array (e.g. `x[1]`), we only want to |
| 262 | // know if the base (`x`) is assignable; the index expression (`1`) doesn't need to be. |
| 263 | class IsAssignableVisitor { |
| 264 | public: |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 265 | IsAssignableVisitor(VariableReference** assignableVar, ErrorReporter* errors) |
| 266 | : fAssignableVar(assignableVar), fErrors(errors) { |
| 267 | if (fAssignableVar) { |
| 268 | *fAssignableVar = nullptr; |
| 269 | } |
| 270 | } |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 271 | |
| 272 | bool visit(Expression& expr) { |
| 273 | this->visitExpression(expr); |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 274 | return fErrors->errorCount() == 0; |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | void visitExpression(Expression& expr) { |
| 278 | switch (expr.kind()) { |
| 279 | case Expression::Kind::kVariableReference: { |
| 280 | VariableReference& varRef = expr.as<VariableReference>(); |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 281 | const Variable* var = varRef.variable(); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 282 | if (var->modifiers().fFlags & (Modifiers::kConst_Flag | Modifiers::kUniform_Flag | |
| 283 | Modifiers::kVarying_Flag)) { |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 284 | fErrors->error(expr.fOffset, |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 285 | "cannot modify immutable variable '" + var->name() + "'"); |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 286 | } else if (fAssignableVar) { |
| 287 | SkASSERT(*fAssignableVar == nullptr); |
| 288 | *fAssignableVar = &varRef; |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 289 | } |
| 290 | break; |
| 291 | } |
| 292 | case Expression::Kind::kFieldAccess: |
Ethan Nicholas | 7a95b20 | 2020-10-09 11:55:40 -0400 | [diff] [blame] | 293 | this->visitExpression(*expr.as<FieldAccess>().base()); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 294 | break; |
| 295 | |
| 296 | case Expression::Kind::kSwizzle: { |
| 297 | const Swizzle& swizzle = expr.as<Swizzle>(); |
| 298 | this->checkSwizzleWrite(swizzle); |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 299 | this->visitExpression(*swizzle.base()); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 300 | break; |
| 301 | } |
| 302 | case Expression::Kind::kIndex: |
Ethan Nicholas | 2a4952d | 2020-10-08 15:35:56 -0400 | [diff] [blame] | 303 | this->visitExpression(*expr.as<IndexExpression>().base()); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 304 | break; |
| 305 | |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 306 | case Expression::Kind::kExternalValue: { |
Ethan Nicholas | 444ccc6 | 2020-10-09 10:16:22 -0400 | [diff] [blame] | 307 | const ExternalValue& var = expr.as<ExternalValueReference>().value(); |
| 308 | if (!var.canWrite()) { |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 309 | fErrors->error(expr.fOffset, |
Ethan Nicholas | 444ccc6 | 2020-10-09 10:16:22 -0400 | [diff] [blame] | 310 | "cannot modify immutable external value '" + var.name() + "'"); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 311 | } |
| 312 | break; |
| 313 | } |
| 314 | default: |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 315 | fErrors->error(expr.fOffset, "cannot assign to this expression"); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 316 | break; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | private: |
| 321 | void checkSwizzleWrite(const Swizzle& swizzle) { |
| 322 | int bits = 0; |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 323 | for (int idx : swizzle.components()) { |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 324 | SkASSERT(idx <= 3); |
| 325 | int bit = 1 << idx; |
| 326 | if (bits & bit) { |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 327 | fErrors->error(swizzle.fOffset, |
| 328 | "cannot write to the same swizzle field more than once"); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 329 | break; |
| 330 | } |
| 331 | bits |= bit; |
| 332 | } |
| 333 | } |
| 334 | |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 335 | VariableReference** fAssignableVar; |
| 336 | ErrorReporter* fErrors; |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 337 | |
| 338 | using INHERITED = ProgramVisitor; |
| 339 | }; |
| 340 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 341 | } // namespace |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 342 | |
| 343 | //////////////////////////////////////////////////////////////////////////////// |
| 344 | // Analysis |
| 345 | |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 346 | SampleUsage Analysis::GetSampleUsage(const Program& program, const Variable& fp) { |
John Stiles | 933abe3 | 2020-08-28 11:58:40 -0400 | [diff] [blame] | 347 | MergeSampleUsageVisitor visitor(*program.fContext, fp); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 348 | return visitor.visit(program); |
| 349 | } |
| 350 | |
Brian Osman | 92aac1e | 2020-08-05 16:48:58 -0400 | [diff] [blame] | 351 | bool Analysis::ReferencesBuiltin(const Program& program, int builtin) { |
| 352 | BuiltinVariableVisitor visitor(builtin); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 353 | return visitor.visit(program); |
| 354 | } |
| 355 | |
Brian Osman | 92aac1e | 2020-08-05 16:48:58 -0400 | [diff] [blame] | 356 | bool Analysis::ReferencesSampleCoords(const Program& program) { |
| 357 | return Analysis::ReferencesBuiltin(program, SK_MAIN_COORDS_BUILTIN); |
| 358 | } |
| 359 | |
| 360 | bool Analysis::ReferencesFragCoords(const Program& program) { |
| 361 | return Analysis::ReferencesBuiltin(program, SK_FRAGCOORD_BUILTIN); |
| 362 | } |
| 363 | |
John Stiles | 9b9415e | 2020-11-23 14:48:06 -0500 | [diff] [blame^] | 364 | int Analysis::NodeCountUpToLimit(const FunctionDefinition& function, int limit) { |
| 365 | return NodeCountVisitor{limit}.visit(*function.body()); |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 366 | } |
| 367 | |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 368 | std::unique_ptr<ProgramUsage> Analysis::GetUsage(const Program& program) { |
| 369 | auto usage = std::make_unique<ProgramUsage>(); |
| 370 | ProgramUsageVisitor addRefs(usage.get(), /*delta=*/+1); |
| 371 | addRefs.visit(program); |
| 372 | return usage; |
| 373 | } |
| 374 | |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 375 | std::unique_ptr<ProgramUsage> Analysis::GetUsage(const LoadedModule& module) { |
| 376 | auto usage = std::make_unique<ProgramUsage>(); |
| 377 | ProgramUsageVisitor addRefs(usage.get(), /*delta=*/+1); |
| 378 | for (const auto& element : module.fElements) { |
| 379 | addRefs.visitProgramElement(*element); |
| 380 | } |
| 381 | return usage; |
| 382 | } |
| 383 | |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 384 | ProgramUsage::VariableCounts ProgramUsage::get(const Variable& v) const { |
| 385 | VariableCounts result = { 0, v.initialValue() ? 1 : 0 }; |
| 386 | if (const VariableCounts* counts = fVariableCounts.find(&v)) { |
| 387 | result.fRead += counts->fRead; |
| 388 | result.fWrite += counts->fWrite; |
| 389 | } |
| 390 | return result; |
| 391 | } |
| 392 | |
| 393 | bool ProgramUsage::isDead(const Variable& v) const { |
| 394 | const Modifiers& modifiers = v.modifiers(); |
| 395 | VariableCounts counts = this->get(v); |
| 396 | if ((v.storage() != Variable::Storage::kLocal && counts.fRead) || |
| 397 | (modifiers.fFlags & (Modifiers::kIn_Flag | Modifiers::kOut_Flag | Modifiers::kUniform_Flag | |
| 398 | Modifiers::kVarying_Flag))) { |
| 399 | return false; |
| 400 | } |
| 401 | return !counts.fWrite || (!counts.fRead && !(modifiers.fFlags & |
| 402 | (Modifiers::kPLS_Flag | Modifiers::kPLSOut_Flag))); |
| 403 | } |
| 404 | |
| 405 | int ProgramUsage::get(const FunctionDeclaration& f) const { |
| 406 | const int* count = fCallCounts.find(&f); |
| 407 | return count ? *count : 0; |
| 408 | } |
| 409 | |
| 410 | void ProgramUsage::replace(const Expression* oldExpr, const Expression* newExpr) { |
| 411 | if (oldExpr) { |
| 412 | ProgramUsageVisitor subRefs(this, /*delta=*/-1); |
| 413 | subRefs.visitExpression(*oldExpr); |
| 414 | } |
| 415 | if (newExpr) { |
| 416 | ProgramUsageVisitor addRefs(this, /*delta=*/+1); |
| 417 | addRefs.visitExpression(*newExpr); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | void ProgramUsage::add(const Statement* stmt) { |
| 422 | ProgramUsageVisitor addRefs(this, /*delta=*/+1); |
| 423 | addRefs.visitStatement(*stmt); |
| 424 | } |
| 425 | |
| 426 | void ProgramUsage::remove(const Expression* expr) { |
| 427 | ProgramUsageVisitor subRefs(this, /*delta=*/-1); |
| 428 | subRefs.visitExpression(*expr); |
| 429 | } |
| 430 | |
| 431 | void ProgramUsage::remove(const Statement* stmt) { |
| 432 | ProgramUsageVisitor subRefs(this, /*delta=*/-1); |
| 433 | subRefs.visitStatement(*stmt); |
| 434 | } |
| 435 | |
| 436 | void ProgramUsage::remove(const ProgramElement& element) { |
| 437 | ProgramUsageVisitor subRefs(this, /*delta=*/-1); |
| 438 | subRefs.visitProgramElement(element); |
Brian Osman | 2e25ff4 | 2020-10-15 10:32:04 -0400 | [diff] [blame] | 439 | } |
| 440 | |
Ethan Nicholas | 765d2fe | 2020-08-26 08:29:55 -0400 | [diff] [blame] | 441 | bool Analysis::StatementWritesToVariable(const Statement& stmt, const Variable& var) { |
| 442 | return VariableWriteVisitor(&var).visit(stmt); |
| 443 | } |
| 444 | |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 445 | bool Analysis::IsAssignable(Expression& expr, VariableReference** assignableVar, |
| 446 | ErrorReporter* errors) { |
| 447 | TrivialErrorReporter trivialErrors; |
| 448 | return IsAssignableVisitor{assignableVar, errors ? errors : &trivialErrors}.visit(expr); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 449 | } |
| 450 | |
John Stiles | c30fbca | 2020-11-19 16:25:49 -0500 | [diff] [blame] | 451 | bool Analysis::IsTrivialExpression(const Expression& expr) { |
| 452 | return expr.is<IntLiteral>() || |
| 453 | expr.is<FloatLiteral>() || |
| 454 | expr.is<BoolLiteral>() || |
| 455 | expr.is<VariableReference>() || |
| 456 | (expr.is<Swizzle>() && |
| 457 | IsTrivialExpression(*expr.as<Swizzle>().base())) || |
| 458 | (expr.is<FieldAccess>() && |
| 459 | IsTrivialExpression(*expr.as<FieldAccess>().base())) || |
| 460 | (expr.is<Constructor>() && |
| 461 | expr.as<Constructor>().arguments().size() == 1 && |
| 462 | IsTrivialExpression(*expr.as<Constructor>().arguments().front())) || |
| 463 | (expr.is<Constructor>() && |
| 464 | expr.isConstantOrUniform()) || |
| 465 | (expr.is<IndexExpression>() && |
| 466 | expr.as<IndexExpression>().index()->is<IntLiteral>() && |
| 467 | IsTrivialExpression(*expr.as<IndexExpression>().base())); |
| 468 | } |
| 469 | |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 470 | //////////////////////////////////////////////////////////////////////////////// |
| 471 | // ProgramVisitor |
| 472 | |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 473 | bool ProgramVisitor::visit(const Program& program) { |
| 474 | for (const ProgramElement* pe : program.elements()) { |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 475 | if (this->visitProgramElement(*pe)) { |
John Stiles | 933abe3 | 2020-08-28 11:58:40 -0400 | [diff] [blame] | 476 | return true; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 477 | } |
| 478 | } |
John Stiles | 933abe3 | 2020-08-28 11:58:40 -0400 | [diff] [blame] | 479 | return false; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 480 | } |
| 481 | |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 482 | template <typename PROG, typename EXPR, typename STMT, typename ELEM> |
| 483 | bool TProgramVisitor<PROG, EXPR, STMT, ELEM>::visitExpression(EXPR e) { |
| 484 | switch (e.kind()) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 485 | case Expression::Kind::kBoolLiteral: |
| 486 | case Expression::Kind::kDefined: |
| 487 | case Expression::Kind::kExternalValue: |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 488 | case Expression::Kind::kFloatLiteral: |
| 489 | case Expression::Kind::kFunctionReference: |
| 490 | case Expression::Kind::kIntLiteral: |
| 491 | case Expression::Kind::kNullLiteral: |
| 492 | case Expression::Kind::kSetting: |
| 493 | case Expression::Kind::kTypeReference: |
| 494 | case Expression::Kind::kVariableReference: |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 495 | // Leaf expressions return false |
| 496 | return false; |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 497 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 498 | case Expression::Kind::kBinary: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 499 | auto& b = e.template as<BinaryExpression>(); |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 500 | return (b.left() && this->visitExpression(*b.left())) || |
| 501 | (b.right() && this->visitExpression(*b.right())); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 502 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 503 | case Expression::Kind::kConstructor: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 504 | auto& c = e.template as<Constructor>(); |
| 505 | for (auto& arg : c.arguments()) { |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 506 | if (this->visitExpression(*arg)) { return true; } |
| 507 | } |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 508 | return false; |
| 509 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 510 | case Expression::Kind::kExternalFunctionCall: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 511 | auto& c = e.template as<ExternalFunctionCall>(); |
Ethan Nicholas | 6e86ec9 | 2020-09-30 14:29:56 -0400 | [diff] [blame] | 512 | for (auto& arg : c.arguments()) { |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 513 | if (this->visitExpression(*arg)) { return true; } |
| 514 | } |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 515 | return false; |
| 516 | } |
John Stiles | d7ab450 | 2020-09-24 22:41:00 -0400 | [diff] [blame] | 517 | case Expression::Kind::kFieldAccess: |
Ethan Nicholas | 7a95b20 | 2020-10-09 11:55:40 -0400 | [diff] [blame] | 518 | return this->visitExpression(*e.template as<FieldAccess>().base()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 519 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 520 | case Expression::Kind::kFunctionCall: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 521 | auto& c = e.template as<FunctionCall>(); |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 522 | for (auto& arg : c.arguments()) { |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 523 | if (arg && this->visitExpression(*arg)) { return true; } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 524 | } |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 525 | return false; |
| 526 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 527 | case Expression::Kind::kIndex: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 528 | auto& i = e.template as<IndexExpression>(); |
Ethan Nicholas | 2a4952d | 2020-10-08 15:35:56 -0400 | [diff] [blame] | 529 | return this->visitExpression(*i.base()) || this->visitExpression(*i.index()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 530 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 531 | case Expression::Kind::kPostfix: |
Ethan Nicholas | 444ccc6 | 2020-10-09 10:16:22 -0400 | [diff] [blame] | 532 | return this->visitExpression(*e.template as<PostfixExpression>().operand()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 533 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 534 | case Expression::Kind::kPrefix: |
Ethan Nicholas | 444ccc6 | 2020-10-09 10:16:22 -0400 | [diff] [blame] | 535 | return this->visitExpression(*e.template as<PrefixExpression>().operand()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 536 | |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 537 | case Expression::Kind::kSwizzle: { |
| 538 | auto& s = e.template as<Swizzle>(); |
| 539 | return s.base() && this->visitExpression(*s.base()); |
| 540 | } |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 541 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 542 | case Expression::Kind::kTernary: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 543 | auto& t = e.template as<TernaryExpression>(); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 544 | return this->visitExpression(*t.test()) || |
| 545 | (t.ifTrue() && this->visitExpression(*t.ifTrue())) || |
| 546 | (t.ifFalse() && this->visitExpression(*t.ifFalse())); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 547 | } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 548 | default: |
| 549 | SkUNREACHABLE; |
| 550 | } |
| 551 | } |
| 552 | |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 553 | template <typename PROG, typename EXPR, typename STMT, typename ELEM> |
| 554 | bool TProgramVisitor<PROG, EXPR, STMT, ELEM>::visitStatement(STMT s) { |
| 555 | switch (s.kind()) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 556 | case Statement::Kind::kBreak: |
| 557 | case Statement::Kind::kContinue: |
| 558 | case Statement::Kind::kDiscard: |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 559 | case Statement::Kind::kInlineMarker: |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 560 | case Statement::Kind::kNop: |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 561 | // Leaf statements just return false |
| 562 | return false; |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 563 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 564 | case Statement::Kind::kBlock: |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 565 | for (auto& stmt : s.template as<Block>().children()) { |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 566 | if (stmt && this->visitStatement(*stmt)) { |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 567 | return true; |
| 568 | } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 569 | } |
| 570 | return false; |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 571 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 572 | case Statement::Kind::kDo: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 573 | auto& d = s.template as<DoStatement>(); |
| 574 | return this->visitExpression(*d.test()) || this->visitStatement(*d.statement()); |
| 575 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 576 | case Statement::Kind::kExpression: |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 577 | return this->visitExpression(*s.template as<ExpressionStatement>().expression()); |
| 578 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 579 | case Statement::Kind::kFor: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 580 | auto& f = s.template as<ForStatement>(); |
Ethan Nicholas | 0d31ed5 | 2020-10-05 14:47:09 -0400 | [diff] [blame] | 581 | return (f.initializer() && this->visitStatement(*f.initializer())) || |
| 582 | (f.test() && this->visitExpression(*f.test())) || |
| 583 | (f.next() && this->visitExpression(*f.next())) || |
| 584 | this->visitStatement(*f.statement()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 585 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 586 | case Statement::Kind::kIf: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 587 | auto& i = s.template as<IfStatement>(); |
Brian Osman | 5567a60 | 2020-10-27 09:52:39 -0400 | [diff] [blame] | 588 | return (i.test() && this->visitExpression(*i.test())) || |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 589 | (i.ifTrue() && this->visitStatement(*i.ifTrue())) || |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 590 | (i.ifFalse() && this->visitStatement(*i.ifFalse())); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 591 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 592 | case Statement::Kind::kReturn: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 593 | auto& r = s.template as<ReturnStatement>(); |
Ethan Nicholas | 2a4952d | 2020-10-08 15:35:56 -0400 | [diff] [blame] | 594 | return r.expression() && this->visitExpression(*r.expression()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 595 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 596 | case Statement::Kind::kSwitch: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 597 | auto& sw = s.template as<SwitchStatement>(); |
Ethan Nicholas | 01b05e5 | 2020-10-22 15:53:41 -0400 | [diff] [blame] | 598 | if (this->visitExpression(*sw.value())) { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 599 | return true; |
| 600 | } |
Ethan Nicholas | 01b05e5 | 2020-10-22 15:53:41 -0400 | [diff] [blame] | 601 | for (const auto& c : sw.cases()) { |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 602 | if (c->value() && this->visitExpression(*c->value())) { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 603 | return true; |
| 604 | } |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 605 | for (auto& st : c->statements()) { |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 606 | if (st && this->visitStatement(*st)) { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 607 | return true; |
| 608 | } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 609 | } |
| 610 | } |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 611 | return false; |
| 612 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 613 | case Statement::Kind::kVarDeclaration: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 614 | auto& v = s.template as<VarDeclaration>(); |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 615 | for (const std::unique_ptr<Expression>& size : v.sizes()) { |
| 616 | if (size && this->visitExpression(*size)) { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 617 | return true; |
| 618 | } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 619 | } |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 620 | return v.value() && this->visitExpression(*v.value()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 621 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 622 | case Statement::Kind::kWhile: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 623 | auto& w = s.template as<WhileStatement>(); |
Ethan Nicholas | 2a4952d | 2020-10-08 15:35:56 -0400 | [diff] [blame] | 624 | return this->visitExpression(*w.test()) || this->visitStatement(*w.statement()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 625 | } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 626 | default: |
| 627 | SkUNREACHABLE; |
| 628 | } |
| 629 | } |
| 630 | |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 631 | template <typename PROG, typename EXPR, typename STMT, typename ELEM> |
| 632 | bool TProgramVisitor<PROG, EXPR, STMT, ELEM>::visitProgramElement(ELEM pe) { |
| 633 | switch (pe.kind()) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 634 | case ProgramElement::Kind::kEnum: |
| 635 | case ProgramElement::Kind::kExtension: |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 636 | case ProgramElement::Kind::kFunctionPrototype: |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 637 | case ProgramElement::Kind::kModifiers: |
| 638 | case ProgramElement::Kind::kSection: |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 639 | // Leaf program elements just return false by default |
| 640 | return false; |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 641 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 642 | case ProgramElement::Kind::kFunction: |
Ethan Nicholas | 0a5d096 | 2020-10-14 13:33:18 -0400 | [diff] [blame] | 643 | return this->visitStatement(*pe.template as<FunctionDefinition>().body()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 644 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 645 | case ProgramElement::Kind::kInterfaceBlock: |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 646 | for (auto& e : pe.template as<InterfaceBlock>().sizes()) { |
Ethan Nicholas | 825268f | 2020-10-02 16:26:00 -0400 | [diff] [blame] | 647 | if (e && this->visitExpression(*e)) { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 648 | return true; |
| 649 | } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 650 | } |
| 651 | return false; |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 652 | |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 653 | case ProgramElement::Kind::kGlobalVar: |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 654 | if (this->visitStatement(*pe.template as<GlobalVarDeclaration>().declaration())) { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 655 | return true; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 656 | } |
| 657 | return false; |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 658 | |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 659 | default: |
| 660 | SkUNREACHABLE; |
| 661 | } |
| 662 | } |
| 663 | |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 664 | template class TProgramVisitor<const Program&, const Expression&, |
| 665 | const Statement&, const ProgramElement&>; |
| 666 | template class TProgramVisitor<Program&, Expression&, Statement&, ProgramElement&>; |
| 667 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 668 | } // namespace SkSL |