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" |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 11 | #include "src/sksl/SkSLCompiler.h" |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 12 | #include "src/sksl/SkSLErrorReporter.h" |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 13 | #include "src/sksl/ir/SkSLExpression.h" |
| 14 | #include "src/sksl/ir/SkSLProgram.h" |
| 15 | #include "src/sksl/ir/SkSLProgramElement.h" |
| 16 | #include "src/sksl/ir/SkSLStatement.h" |
| 17 | |
| 18 | // ProgramElements |
| 19 | #include "src/sksl/ir/SkSLEnum.h" |
| 20 | #include "src/sksl/ir/SkSLExtension.h" |
| 21 | #include "src/sksl/ir/SkSLFunctionDefinition.h" |
| 22 | #include "src/sksl/ir/SkSLInterfaceBlock.h" |
| 23 | #include "src/sksl/ir/SkSLModifiers.h" |
| 24 | #include "src/sksl/ir/SkSLSection.h" |
| 25 | #include "src/sksl/ir/SkSLVarDeclarations.h" |
| 26 | |
| 27 | // Statements |
| 28 | #include "src/sksl/ir/SkSLBlock.h" |
| 29 | #include "src/sksl/ir/SkSLBreakStatement.h" |
| 30 | #include "src/sksl/ir/SkSLContinueStatement.h" |
| 31 | #include "src/sksl/ir/SkSLDiscardStatement.h" |
| 32 | #include "src/sksl/ir/SkSLDoStatement.h" |
| 33 | #include "src/sksl/ir/SkSLExpressionStatement.h" |
| 34 | #include "src/sksl/ir/SkSLForStatement.h" |
| 35 | #include "src/sksl/ir/SkSLIfStatement.h" |
| 36 | #include "src/sksl/ir/SkSLNop.h" |
| 37 | #include "src/sksl/ir/SkSLReturnStatement.h" |
| 38 | #include "src/sksl/ir/SkSLSwitchStatement.h" |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 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" |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 45 | #include "src/sksl/ir/SkSLExternalFunctionReference.h" |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 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" |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 53 | #include "src/sksl/ir/SkSLPostfixExpression.h" |
| 54 | #include "src/sksl/ir/SkSLPrefixExpression.h" |
| 55 | #include "src/sksl/ir/SkSLSetting.h" |
| 56 | #include "src/sksl/ir/SkSLSwizzle.h" |
| 57 | #include "src/sksl/ir/SkSLTernaryExpression.h" |
| 58 | #include "src/sksl/ir/SkSLTypeReference.h" |
| 59 | #include "src/sksl/ir/SkSLVariableReference.h" |
| 60 | |
| 61 | namespace SkSL { |
| 62 | |
| 63 | namespace { |
| 64 | |
| 65 | 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] | 66 | const FunctionDeclaration& f = fc.function(); |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 67 | return f.isBuiltin() && f.name() == "sample" && fc.arguments().size() >= 1 && |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 68 | fc.arguments()[0]->is<VariableReference>() && |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 69 | fc.arguments()[0]->as<VariableReference>().variable() == &fp; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 70 | } |
| 71 | |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 72 | // Visitor that determines the merged SampleUsage for a given child 'fp' in the program. |
| 73 | class MergeSampleUsageVisitor : public ProgramVisitor { |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 74 | public: |
John Stiles | 933abe3 | 2020-08-28 11:58:40 -0400 | [diff] [blame] | 75 | MergeSampleUsageVisitor(const Context& context, const Variable& fp) |
| 76 | : fContext(context), fFP(fp) {} |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 77 | |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 78 | SampleUsage visit(const Program& program) { |
| 79 | fUsage = SampleUsage(); // reset to none |
John Stiles | d7ab450 | 2020-09-24 22:41:00 -0400 | [diff] [blame] | 80 | INHERITED::visit(program); |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 81 | return fUsage; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | protected: |
John Stiles | 933abe3 | 2020-08-28 11:58:40 -0400 | [diff] [blame] | 85 | const Context& fContext; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 86 | const Variable& fFP; |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 87 | SampleUsage fUsage; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 88 | |
| 89 | bool visitExpression(const Expression& e) override { |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 90 | // Looking for sample(fp, inColor?, ...) |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 91 | if (e.kind() == Expression::Kind::kFunctionCall) { |
John Stiles | 403a363 | 2020-08-20 12:11:48 -0400 | [diff] [blame] | 92 | const FunctionCall& fc = e.as<FunctionCall>(); |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 93 | if (is_sample_call_to_fp(fc, fFP)) { |
| 94 | // 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] | 95 | const Expression* lastArg = fc.arguments().back().get(); |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 96 | |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 97 | if (lastArg->type() == *fContext.fTypes.fFloat2) { |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 98 | fUsage.merge(SampleUsage::Explicit()); |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 99 | } else if (lastArg->type() == *fContext.fTypes.fFloat3x3) { |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 100 | // Determine the type of matrix for this call site |
| 101 | if (lastArg->isConstantOrUniform()) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 102 | if (lastArg->kind() == Expression::Kind::kVariableReference || |
| 103 | lastArg->kind() == Expression::Kind::kConstructor) { |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 104 | // FIXME if this is a constant, we should parse the float3x3 constructor |
| 105 | // and determine if the resulting matrix introduces perspective. |
| 106 | fUsage.merge(SampleUsage::UniformMatrix(lastArg->description())); |
| 107 | } else { |
| 108 | // FIXME this is really to workaround a restriction of the downstream |
| 109 | // code that relies on the SampleUsage's fExpression to identify uniform |
| 110 | // names. Once they are tracked separately, any uniform expression can |
| 111 | // work, but right now this avoids issues from '0.5 * matrix' that is |
| 112 | // both a constant AND a uniform. |
| 113 | fUsage.merge(SampleUsage::VariableMatrix()); |
| 114 | } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 115 | } else { |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 116 | fUsage.merge(SampleUsage::VariableMatrix()); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 117 | } |
| 118 | } else { |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 119 | // The only other signatures do pass-through sampling |
| 120 | fUsage.merge(SampleUsage::PassThrough()); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 121 | } |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 122 | // NOTE: we don't return true here just because we found a sample call. We need to |
| 123 | // process the entire program and merge across all encountered calls. |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
John Stiles | d7ab450 | 2020-09-24 22:41:00 -0400 | [diff] [blame] | 127 | return INHERITED::visitExpression(e); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 128 | } |
| 129 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 130 | using INHERITED = ProgramVisitor; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 131 | }; |
| 132 | |
Brian Osman | 92aac1e | 2020-08-05 16:48:58 -0400 | [diff] [blame] | 133 | // Visitor that searches through the program for references to a particular builtin variable |
| 134 | class BuiltinVariableVisitor : public ProgramVisitor { |
| 135 | public: |
| 136 | BuiltinVariableVisitor(int builtin) : fBuiltin(builtin) {} |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 137 | |
| 138 | bool visitExpression(const Expression& e) override { |
Brian Osman | 79457ef | 2020-09-24 15:01:27 -0400 | [diff] [blame] | 139 | if (e.is<VariableReference>()) { |
John Stiles | 403a363 | 2020-08-20 12:11:48 -0400 | [diff] [blame] | 140 | const VariableReference& var = e.as<VariableReference>(); |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 141 | return var.variable()->modifiers().fLayout.fBuiltin == fBuiltin; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 142 | } |
John Stiles | d7ab450 | 2020-09-24 22:41:00 -0400 | [diff] [blame] | 143 | return INHERITED::visitExpression(e); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 144 | } |
| 145 | |
Brian Osman | 92aac1e | 2020-08-05 16:48:58 -0400 | [diff] [blame] | 146 | int fBuiltin; |
| 147 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 148 | using INHERITED = ProgramVisitor; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 149 | }; |
| 150 | |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 151 | // Visitor that counts the number of nodes visited |
| 152 | class NodeCountVisitor : public ProgramVisitor { |
| 153 | public: |
John Stiles | 2c1e492 | 2020-10-01 09:14:14 -0400 | [diff] [blame] | 154 | NodeCountVisitor(int limit) : fLimit(limit) {} |
| 155 | |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 156 | int visit(const Statement& s) { |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 157 | this->visitStatement(s); |
| 158 | return fCount; |
| 159 | } |
| 160 | |
| 161 | bool visitExpression(const Expression& e) override { |
| 162 | ++fCount; |
John Stiles | 9b9415e | 2020-11-23 14:48:06 -0500 | [diff] [blame] | 163 | return (fCount >= fLimit) || INHERITED::visitExpression(e); |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | bool visitProgramElement(const ProgramElement& p) override { |
| 167 | ++fCount; |
John Stiles | 9b9415e | 2020-11-23 14:48:06 -0500 | [diff] [blame] | 168 | return (fCount >= fLimit) || INHERITED::visitProgramElement(p); |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | bool visitStatement(const Statement& s) override { |
| 172 | ++fCount; |
John Stiles | 9b9415e | 2020-11-23 14:48:06 -0500 | [diff] [blame] | 173 | return (fCount >= fLimit) || INHERITED::visitStatement(s); |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | private: |
John Stiles | 2c1e492 | 2020-10-01 09:14:14 -0400 | [diff] [blame] | 177 | int fCount = 0; |
| 178 | int fLimit; |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 179 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 180 | using INHERITED = ProgramVisitor; |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 181 | }; |
| 182 | |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 183 | class ProgramUsageVisitor : public ProgramVisitor { |
Brian Osman | 2e25ff4 | 2020-10-15 10:32:04 -0400 | [diff] [blame] | 184 | public: |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 185 | ProgramUsageVisitor(ProgramUsage* usage, int delta) : fUsage(usage), fDelta(delta) {} |
| 186 | |
Brian Osman | 2e25ff4 | 2020-10-15 10:32:04 -0400 | [diff] [blame] | 187 | bool visitExpression(const Expression& e) override { |
| 188 | if (e.is<FunctionCall>()) { |
| 189 | const FunctionDeclaration* f = &e.as<FunctionCall>().function(); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 190 | fUsage->fCallCounts[f] += fDelta; |
| 191 | SkASSERT(fUsage->fCallCounts[f] >= 0); |
| 192 | } else if (e.is<VariableReference>()) { |
| 193 | const VariableReference& ref = e.as<VariableReference>(); |
| 194 | ProgramUsage::VariableCounts& counts = fUsage->fVariableCounts[ref.variable()]; |
| 195 | switch (ref.refKind()) { |
| 196 | case VariableRefKind::kRead: |
| 197 | counts.fRead += fDelta; |
| 198 | break; |
| 199 | case VariableRefKind::kWrite: |
| 200 | counts.fWrite += fDelta; |
| 201 | break; |
| 202 | case VariableRefKind::kReadWrite: |
| 203 | case VariableRefKind::kPointer: |
| 204 | counts.fRead += fDelta; |
| 205 | counts.fWrite += fDelta; |
| 206 | break; |
| 207 | } |
| 208 | SkASSERT(counts.fRead >= 0 && counts.fWrite >= 0); |
Brian Osman | 2e25ff4 | 2020-10-15 10:32:04 -0400 | [diff] [blame] | 209 | } |
| 210 | return INHERITED::visitExpression(e); |
| 211 | } |
| 212 | |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 213 | using ProgramVisitor::visitProgramElement; |
| 214 | using ProgramVisitor::visitStatement; |
| 215 | |
| 216 | ProgramUsage* fUsage; |
| 217 | int fDelta; |
Brian Osman | 2e25ff4 | 2020-10-15 10:32:04 -0400 | [diff] [blame] | 218 | using INHERITED = ProgramVisitor; |
| 219 | }; |
| 220 | |
Ethan Nicholas | 765d2fe | 2020-08-26 08:29:55 -0400 | [diff] [blame] | 221 | class VariableWriteVisitor : public ProgramVisitor { |
| 222 | public: |
| 223 | VariableWriteVisitor(const Variable* var) |
| 224 | : fVar(var) {} |
| 225 | |
| 226 | bool visit(const Statement& s) { |
| 227 | return this->visitStatement(s); |
| 228 | } |
| 229 | |
| 230 | bool visitExpression(const Expression& e) override { |
Brian Osman | 79457ef | 2020-09-24 15:01:27 -0400 | [diff] [blame] | 231 | if (e.is<VariableReference>()) { |
Ethan Nicholas | 765d2fe | 2020-08-26 08:29:55 -0400 | [diff] [blame] | 232 | const VariableReference& ref = e.as<VariableReference>(); |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 233 | if (ref.variable() == fVar && |
| 234 | (ref.refKind() == VariableReference::RefKind::kWrite || |
| 235 | ref.refKind() == VariableReference::RefKind::kReadWrite || |
| 236 | ref.refKind() == VariableReference::RefKind::kPointer)) { |
Ethan Nicholas | 765d2fe | 2020-08-26 08:29:55 -0400 | [diff] [blame] | 237 | return true; |
| 238 | } |
| 239 | } |
John Stiles | d7ab450 | 2020-09-24 22:41:00 -0400 | [diff] [blame] | 240 | return INHERITED::visitExpression(e); |
Ethan Nicholas | 765d2fe | 2020-08-26 08:29:55 -0400 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | private: |
| 244 | const Variable* fVar; |
| 245 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 246 | using INHERITED = ProgramVisitor; |
Ethan Nicholas | 765d2fe | 2020-08-26 08:29:55 -0400 | [diff] [blame] | 247 | }; |
| 248 | |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 249 | // If a caller doesn't care about errors, we can use this trivial reporter that just counts up. |
| 250 | class TrivialErrorReporter : public ErrorReporter { |
| 251 | public: |
| 252 | void error(int offset, String) override { ++fErrorCount; } |
| 253 | int errorCount() override { return fErrorCount; } |
John Stiles | 8d3642e | 2021-01-22 09:50:04 -0500 | [diff] [blame] | 254 | void setErrorCount(int c) override { fErrorCount = c; } |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 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 | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 265 | IsAssignableVisitor(ErrorReporter* errors) : fErrors(errors) {} |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 266 | |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 267 | bool visit(Expression& expr, Analysis::AssignmentInfo* info) { |
Ethan Nicholas | 67a0a8a | 2021-01-13 12:36:02 -0500 | [diff] [blame] | 268 | int oldErrorCount = fErrors->errorCount(); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 269 | this->visitExpression(expr); |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 270 | if (info) { |
| 271 | info->fAssignedVar = fAssignedVar; |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 272 | } |
Ethan Nicholas | 67a0a8a | 2021-01-13 12:36:02 -0500 | [diff] [blame] | 273 | return fErrors->errorCount() == oldErrorCount; |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | void visitExpression(Expression& expr) { |
| 277 | switch (expr.kind()) { |
| 278 | case Expression::Kind::kVariableReference: { |
| 279 | VariableReference& varRef = expr.as<VariableReference>(); |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 280 | const Variable* var = varRef.variable(); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 281 | if (var->modifiers().fFlags & (Modifiers::kConst_Flag | Modifiers::kUniform_Flag | |
| 282 | Modifiers::kVarying_Flag)) { |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 283 | fErrors->error(expr.fOffset, |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 284 | "cannot modify immutable variable '" + var->name() + "'"); |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 285 | } else { |
| 286 | SkASSERT(fAssignedVar == nullptr); |
| 287 | fAssignedVar = &varRef; |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 288 | } |
| 289 | break; |
| 290 | } |
| 291 | case Expression::Kind::kFieldAccess: |
Ethan Nicholas | 7a95b20 | 2020-10-09 11:55:40 -0400 | [diff] [blame] | 292 | this->visitExpression(*expr.as<FieldAccess>().base()); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 293 | break; |
| 294 | |
| 295 | case Expression::Kind::kSwizzle: { |
| 296 | const Swizzle& swizzle = expr.as<Swizzle>(); |
| 297 | this->checkSwizzleWrite(swizzle); |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 298 | this->visitExpression(*swizzle.base()); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 299 | break; |
| 300 | } |
John Stiles | 47c0a74 | 2021-02-09 09:30:35 -0500 | [diff] [blame] | 301 | case Expression::Kind::kIndex: |
| 302 | this->visitExpression(*expr.as<IndexExpression>().base()); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 303 | break; |
John Stiles | 47c0a74 | 2021-02-09 09:30:35 -0500 | [diff] [blame] | 304 | |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 305 | default: |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 306 | fErrors->error(expr.fOffset, "cannot assign to this expression"); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 307 | break; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | private: |
| 312 | void checkSwizzleWrite(const Swizzle& swizzle) { |
| 313 | int bits = 0; |
John Stiles | 6e88e04 | 2021-02-19 14:09:38 -0500 | [diff] [blame] | 314 | for (int8_t idx : swizzle.components()) { |
| 315 | SkASSERT(idx >= SwizzleComponent::X && idx <= SwizzleComponent::W); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 316 | int bit = 1 << idx; |
| 317 | if (bits & bit) { |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 318 | fErrors->error(swizzle.fOffset, |
| 319 | "cannot write to the same swizzle field more than once"); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 320 | break; |
| 321 | } |
| 322 | bits |= bit; |
| 323 | } |
| 324 | } |
| 325 | |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 326 | ErrorReporter* fErrors; |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 327 | VariableReference* fAssignedVar = nullptr; |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 328 | |
| 329 | using INHERITED = ProgramVisitor; |
| 330 | }; |
| 331 | |
John Stiles | 642cde2 | 2021-02-23 14:57:01 -0500 | [diff] [blame] | 332 | class SwitchCaseContainsExit : public ProgramVisitor { |
| 333 | public: |
| 334 | SwitchCaseContainsExit(bool conditionalExits) : fConditionalExits(conditionalExits) {} |
| 335 | |
| 336 | bool visitStatement(const Statement& stmt) override { |
| 337 | switch (stmt.kind()) { |
| 338 | case Statement::Kind::kBlock: |
| 339 | return INHERITED::visitStatement(stmt); |
| 340 | |
| 341 | case Statement::Kind::kReturn: |
| 342 | // Returns are an early exit regardless of the surrounding control structures. |
| 343 | return fConditionalExits ? fInConditional : !fInConditional; |
| 344 | |
| 345 | case Statement::Kind::kContinue: |
| 346 | // Continues are an early exit from switches, but not loops. |
| 347 | return !fInLoop && |
| 348 | (fConditionalExits ? fInConditional : !fInConditional); |
| 349 | |
| 350 | case Statement::Kind::kBreak: |
| 351 | // Breaks cannot escape from switches or loops. |
| 352 | return !fInLoop && !fInSwitch && |
| 353 | (fConditionalExits ? fInConditional : !fInConditional); |
| 354 | |
| 355 | case Statement::Kind::kIf: { |
| 356 | ++fInConditional; |
| 357 | bool result = INHERITED::visitStatement(stmt); |
| 358 | --fInConditional; |
| 359 | return result; |
| 360 | } |
| 361 | |
| 362 | case Statement::Kind::kFor: |
| 363 | case Statement::Kind::kDo: { |
| 364 | // Loops are treated as conditionals because a loop could potentially execute zero |
| 365 | // times. We don't have a straightforward way to determine that a loop definitely |
| 366 | // executes at least once. |
| 367 | ++fInConditional; |
| 368 | ++fInLoop; |
| 369 | bool result = INHERITED::visitStatement(stmt); |
| 370 | --fInLoop; |
| 371 | --fInConditional; |
| 372 | return result; |
| 373 | } |
| 374 | |
| 375 | case Statement::Kind::kSwitch: { |
| 376 | ++fInSwitch; |
| 377 | bool result = INHERITED::visitStatement(stmt); |
| 378 | --fInSwitch; |
| 379 | return result; |
| 380 | } |
| 381 | |
| 382 | default: |
| 383 | return false; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | bool fConditionalExits = false; |
| 388 | int fInConditional = 0; |
| 389 | int fInLoop = 0; |
| 390 | int fInSwitch = 0; |
| 391 | using INHERITED = ProgramVisitor; |
| 392 | }; |
| 393 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 394 | } // namespace |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 395 | |
| 396 | //////////////////////////////////////////////////////////////////////////////// |
| 397 | // Analysis |
| 398 | |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 399 | SampleUsage Analysis::GetSampleUsage(const Program& program, const Variable& fp) { |
John Stiles | 933abe3 | 2020-08-28 11:58:40 -0400 | [diff] [blame] | 400 | MergeSampleUsageVisitor visitor(*program.fContext, fp); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 401 | return visitor.visit(program); |
| 402 | } |
| 403 | |
Brian Osman | 92aac1e | 2020-08-05 16:48:58 -0400 | [diff] [blame] | 404 | bool Analysis::ReferencesBuiltin(const Program& program, int builtin) { |
| 405 | BuiltinVariableVisitor visitor(builtin); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 406 | return visitor.visit(program); |
| 407 | } |
| 408 | |
Brian Osman | 92aac1e | 2020-08-05 16:48:58 -0400 | [diff] [blame] | 409 | bool Analysis::ReferencesSampleCoords(const Program& program) { |
| 410 | return Analysis::ReferencesBuiltin(program, SK_MAIN_COORDS_BUILTIN); |
| 411 | } |
| 412 | |
| 413 | bool Analysis::ReferencesFragCoords(const Program& program) { |
| 414 | return Analysis::ReferencesBuiltin(program, SK_FRAGCOORD_BUILTIN); |
| 415 | } |
| 416 | |
John Stiles | 9b9415e | 2020-11-23 14:48:06 -0500 | [diff] [blame] | 417 | int Analysis::NodeCountUpToLimit(const FunctionDefinition& function, int limit) { |
| 418 | return NodeCountVisitor{limit}.visit(*function.body()); |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 419 | } |
| 420 | |
John Stiles | 642cde2 | 2021-02-23 14:57:01 -0500 | [diff] [blame] | 421 | bool Analysis::SwitchCaseContainsUnconditionalExit(Statement& stmt) { |
| 422 | return SwitchCaseContainsExit{/*conditionalExits=*/false}.visitStatement(stmt); |
| 423 | } |
| 424 | |
| 425 | bool Analysis::SwitchCaseContainsConditionalExit(Statement& stmt) { |
| 426 | return SwitchCaseContainsExit{/*conditionalExits=*/true}.visitStatement(stmt); |
| 427 | } |
| 428 | |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 429 | std::unique_ptr<ProgramUsage> Analysis::GetUsage(const Program& program) { |
| 430 | auto usage = std::make_unique<ProgramUsage>(); |
| 431 | ProgramUsageVisitor addRefs(usage.get(), /*delta=*/+1); |
| 432 | addRefs.visit(program); |
| 433 | return usage; |
| 434 | } |
| 435 | |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 436 | std::unique_ptr<ProgramUsage> Analysis::GetUsage(const LoadedModule& module) { |
| 437 | auto usage = std::make_unique<ProgramUsage>(); |
| 438 | ProgramUsageVisitor addRefs(usage.get(), /*delta=*/+1); |
| 439 | for (const auto& element : module.fElements) { |
| 440 | addRefs.visitProgramElement(*element); |
| 441 | } |
| 442 | return usage; |
| 443 | } |
| 444 | |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 445 | ProgramUsage::VariableCounts ProgramUsage::get(const Variable& v) const { |
| 446 | VariableCounts result = { 0, v.initialValue() ? 1 : 0 }; |
| 447 | if (const VariableCounts* counts = fVariableCounts.find(&v)) { |
| 448 | result.fRead += counts->fRead; |
| 449 | result.fWrite += counts->fWrite; |
| 450 | } |
| 451 | return result; |
| 452 | } |
| 453 | |
| 454 | bool ProgramUsage::isDead(const Variable& v) const { |
| 455 | const Modifiers& modifiers = v.modifiers(); |
| 456 | VariableCounts counts = this->get(v); |
| 457 | if ((v.storage() != Variable::Storage::kLocal && counts.fRead) || |
| 458 | (modifiers.fFlags & (Modifiers::kIn_Flag | Modifiers::kOut_Flag | Modifiers::kUniform_Flag | |
| 459 | Modifiers::kVarying_Flag))) { |
| 460 | return false; |
| 461 | } |
Brian Osman | 9cb3f98 | 2021-02-18 12:44:57 -0500 | [diff] [blame] | 462 | return !counts.fWrite || !counts.fRead; |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | int ProgramUsage::get(const FunctionDeclaration& f) const { |
| 466 | const int* count = fCallCounts.find(&f); |
| 467 | return count ? *count : 0; |
| 468 | } |
| 469 | |
| 470 | void ProgramUsage::replace(const Expression* oldExpr, const Expression* newExpr) { |
| 471 | if (oldExpr) { |
| 472 | ProgramUsageVisitor subRefs(this, /*delta=*/-1); |
| 473 | subRefs.visitExpression(*oldExpr); |
| 474 | } |
| 475 | if (newExpr) { |
| 476 | ProgramUsageVisitor addRefs(this, /*delta=*/+1); |
| 477 | addRefs.visitExpression(*newExpr); |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | void ProgramUsage::add(const Statement* stmt) { |
| 482 | ProgramUsageVisitor addRefs(this, /*delta=*/+1); |
| 483 | addRefs.visitStatement(*stmt); |
| 484 | } |
| 485 | |
| 486 | void ProgramUsage::remove(const Expression* expr) { |
| 487 | ProgramUsageVisitor subRefs(this, /*delta=*/-1); |
| 488 | subRefs.visitExpression(*expr); |
| 489 | } |
| 490 | |
| 491 | void ProgramUsage::remove(const Statement* stmt) { |
| 492 | ProgramUsageVisitor subRefs(this, /*delta=*/-1); |
| 493 | subRefs.visitStatement(*stmt); |
| 494 | } |
| 495 | |
| 496 | void ProgramUsage::remove(const ProgramElement& element) { |
| 497 | ProgramUsageVisitor subRefs(this, /*delta=*/-1); |
| 498 | subRefs.visitProgramElement(element); |
Brian Osman | 2e25ff4 | 2020-10-15 10:32:04 -0400 | [diff] [blame] | 499 | } |
| 500 | |
Ethan Nicholas | 765d2fe | 2020-08-26 08:29:55 -0400 | [diff] [blame] | 501 | bool Analysis::StatementWritesToVariable(const Statement& stmt, const Variable& var) { |
| 502 | return VariableWriteVisitor(&var).visit(stmt); |
| 503 | } |
| 504 | |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 505 | bool Analysis::IsAssignable(Expression& expr, AssignmentInfo* info, ErrorReporter* errors) { |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 506 | TrivialErrorReporter trivialErrors; |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 507 | return IsAssignableVisitor{errors ? errors : &trivialErrors}.visit(expr, info); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 508 | } |
| 509 | |
John Stiles | 47c0a74 | 2021-02-09 09:30:35 -0500 | [diff] [blame] | 510 | void Analysis::UpdateRefKind(Expression* expr, VariableRefKind refKind) { |
| 511 | class RefKindWriter : public ProgramWriter { |
| 512 | public: |
| 513 | RefKindWriter(VariableReference::RefKind refKind) : fRefKind(refKind) {} |
| 514 | |
| 515 | bool visitExpression(Expression& expr) override { |
| 516 | if (expr.is<VariableReference>()) { |
| 517 | expr.as<VariableReference>().setRefKind(fRefKind); |
| 518 | } |
| 519 | return INHERITED::visitExpression(expr); |
| 520 | } |
| 521 | |
| 522 | private: |
| 523 | VariableReference::RefKind fRefKind; |
| 524 | |
| 525 | using INHERITED = ProgramWriter; |
| 526 | }; |
| 527 | |
| 528 | RefKindWriter{refKind}.visitExpression(*expr); |
| 529 | } |
| 530 | |
John Stiles | 516704b | 2021-02-26 15:01:57 -0500 | [diff] [blame] | 531 | bool Analysis::MakeAssignmentExpr(Expression* expr, |
| 532 | VariableReference::RefKind kind, |
| 533 | ErrorReporter* errors) { |
| 534 | Analysis::AssignmentInfo info; |
| 535 | if (!Analysis::IsAssignable(*expr, &info, errors)) { |
| 536 | return false; |
| 537 | } |
| 538 | if (!info.fAssignedVar) { |
| 539 | errors->error(expr->fOffset, "can't assign to expression '" + expr->description() + "'"); |
| 540 | return false; |
| 541 | } |
| 542 | info.fAssignedVar->setRefKind(kind); |
| 543 | return true; |
| 544 | } |
| 545 | |
John Stiles | c30fbca | 2020-11-19 16:25:49 -0500 | [diff] [blame] | 546 | bool Analysis::IsTrivialExpression(const Expression& expr) { |
| 547 | return expr.is<IntLiteral>() || |
| 548 | expr.is<FloatLiteral>() || |
| 549 | expr.is<BoolLiteral>() || |
| 550 | expr.is<VariableReference>() || |
| 551 | (expr.is<Swizzle>() && |
| 552 | IsTrivialExpression(*expr.as<Swizzle>().base())) || |
| 553 | (expr.is<FieldAccess>() && |
| 554 | IsTrivialExpression(*expr.as<FieldAccess>().base())) || |
| 555 | (expr.is<Constructor>() && |
| 556 | expr.as<Constructor>().arguments().size() == 1 && |
| 557 | IsTrivialExpression(*expr.as<Constructor>().arguments().front())) || |
| 558 | (expr.is<Constructor>() && |
| 559 | expr.isConstantOrUniform()) || |
| 560 | (expr.is<IndexExpression>() && |
| 561 | expr.as<IndexExpression>().index()->is<IntLiteral>() && |
| 562 | IsTrivialExpression(*expr.as<IndexExpression>().base())); |
| 563 | } |
| 564 | |
John Stiles | 95d0bad | 2021-03-01 17:02:28 -0500 | [diff] [blame] | 565 | bool Analysis::IsSelfAssignment(const Expression& left, const Expression& right) { |
| 566 | if (left.kind() != right.kind() || left.type() != right.type()) { |
| 567 | return false; |
| 568 | } |
| 569 | |
John Stiles | 786d42c | 2021-03-02 09:57:28 -0500 | [diff] [blame] | 570 | // This isn't a fully exhaustive list of expressions that could be involved in a self- |
| 571 | // assignment, particularly when arrays are involved; for instance, `x[y+1] = x[y+1]` isn't |
| 572 | // detected because we don't look at BinaryExpressions. Since this is intended to be used for |
| 573 | // optimization purposes, handling the common cases is sufficient. |
John Stiles | 95d0bad | 2021-03-01 17:02:28 -0500 | [diff] [blame] | 574 | switch (left.kind()) { |
| 575 | case Expression::Kind::kIntLiteral: |
| 576 | return left.as<IntLiteral>().value() == right.as<IntLiteral>().value(); |
| 577 | |
| 578 | case Expression::Kind::kFieldAccess: |
| 579 | return left.as<FieldAccess>().fieldIndex() == right.as<FieldAccess>().fieldIndex() && |
| 580 | IsSelfAssignment(*left.as<FieldAccess>().base(), |
| 581 | *right.as<FieldAccess>().base()); |
| 582 | |
| 583 | case Expression::Kind::kIndex: |
| 584 | return IsSelfAssignment(*left.as<IndexExpression>().index(), |
| 585 | *right.as<IndexExpression>().index()) && |
| 586 | IsSelfAssignment(*left.as<IndexExpression>().base(), |
| 587 | *right.as<IndexExpression>().base()); |
| 588 | |
| 589 | case Expression::Kind::kSwizzle: |
| 590 | return left.as<Swizzle>().components() == right.as<Swizzle>().components() && |
| 591 | IsSelfAssignment(*left.as<Swizzle>().base(), *right.as<Swizzle>().base()); |
| 592 | |
| 593 | case Expression::Kind::kVariableReference: |
| 594 | return left.as<VariableReference>().variable() == |
| 595 | right.as<VariableReference>().variable(); |
| 596 | |
| 597 | default: |
| 598 | return false; |
| 599 | } |
| 600 | } |
| 601 | |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 602 | static const char* invalid_for_ES2(int offset, |
| 603 | const Statement* loopInitializer, |
| 604 | const Expression* loopTest, |
| 605 | const Expression* loopNext, |
| 606 | const Statement* loopStatement, |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 607 | Analysis::UnrollableLoopInfo& loopInfo) { |
| 608 | auto getConstant = [&](const std::unique_ptr<Expression>& expr, double* val) { |
| 609 | if (!expr->isCompileTimeConstant()) { |
| 610 | return false; |
| 611 | } |
Brian Osman | 6c7910e | 2021-01-14 09:50:52 -0500 | [diff] [blame] | 612 | if (!expr->type().isNumber()) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 613 | SkDEBUGFAIL("unexpected constant type"); |
| 614 | return false; |
| 615 | } |
| 616 | |
| 617 | *val = expr->type().isInteger() ? static_cast<double>(expr->getConstantInt()) |
| 618 | : static_cast<double>(expr->getConstantFloat()); |
| 619 | return true; |
| 620 | }; |
| 621 | |
| 622 | // |
| 623 | // init_declaration has the form: type_specifier identifier = constant_expression |
| 624 | // |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 625 | if (!loopInitializer) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 626 | return "missing init declaration"; |
| 627 | } |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 628 | if (!loopInitializer->is<VarDeclaration>()) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 629 | return "invalid init declaration"; |
| 630 | } |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 631 | const VarDeclaration& initDecl = loopInitializer->as<VarDeclaration>(); |
Brian Osman | 6c7910e | 2021-01-14 09:50:52 -0500 | [diff] [blame] | 632 | if (!initDecl.baseType().isNumber()) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 633 | return "invalid type for loop index"; |
| 634 | } |
| 635 | if (initDecl.arraySize() != 0) { |
| 636 | return "invalid type for loop index"; |
| 637 | } |
| 638 | if (!initDecl.value()) { |
| 639 | return "missing loop index initializer"; |
| 640 | } |
| 641 | if (!getConstant(initDecl.value(), &loopInfo.fStart)) { |
| 642 | return "loop index initializer must be a constant expression"; |
| 643 | } |
| 644 | |
| 645 | loopInfo.fIndex = &initDecl.var(); |
| 646 | |
| 647 | auto is_loop_index = [&](const std::unique_ptr<Expression>& expr) { |
| 648 | return expr->is<VariableReference>() && |
| 649 | expr->as<VariableReference>().variable() == loopInfo.fIndex; |
| 650 | }; |
| 651 | |
| 652 | // |
| 653 | // condition has the form: loop_index relational_operator constant_expression |
| 654 | // |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 655 | if (!loopTest) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 656 | return "missing condition"; |
| 657 | } |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 658 | if (!loopTest->is<BinaryExpression>()) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 659 | return "invalid condition"; |
| 660 | } |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 661 | const BinaryExpression& cond = loopTest->as<BinaryExpression>(); |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 662 | if (!is_loop_index(cond.left())) { |
| 663 | return "expected loop index on left hand side of condition"; |
| 664 | } |
| 665 | // relational_operator is one of: > >= < <= == or != |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 666 | switch (cond.getOperator().kind()) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 667 | case Token::Kind::TK_GT: |
| 668 | case Token::Kind::TK_GTEQ: |
| 669 | case Token::Kind::TK_LT: |
| 670 | case Token::Kind::TK_LTEQ: |
| 671 | case Token::Kind::TK_EQEQ: |
| 672 | case Token::Kind::TK_NEQ: |
| 673 | break; |
| 674 | default: |
| 675 | return "invalid relational operator"; |
| 676 | } |
| 677 | double loopEnd = 0; |
| 678 | if (!getConstant(cond.right(), &loopEnd)) { |
| 679 | return "loop index must be compared with a constant expression"; |
| 680 | } |
| 681 | |
| 682 | // |
| 683 | // expression has one of the following forms: |
| 684 | // loop_index++ |
| 685 | // loop_index-- |
| 686 | // loop_index += constant_expression |
| 687 | // loop_index -= constant_expression |
| 688 | // The spec doesn't mention prefix increment and decrement, but there is some consensus that |
| 689 | // it's an oversight, so we allow those as well. |
| 690 | // |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 691 | if (!loopNext) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 692 | return "missing loop expression"; |
| 693 | } |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 694 | switch (loopNext->kind()) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 695 | case Expression::Kind::kBinary: { |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 696 | const BinaryExpression& next = loopNext->as<BinaryExpression>(); |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 697 | if (!is_loop_index(next.left())) { |
| 698 | return "expected loop index in loop expression"; |
| 699 | } |
| 700 | if (!getConstant(next.right(), &loopInfo.fDelta)) { |
| 701 | return "loop index must be modified by a constant expression"; |
| 702 | } |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 703 | switch (next.getOperator().kind()) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 704 | case Token::Kind::TK_PLUSEQ: break; |
| 705 | case Token::Kind::TK_MINUSEQ: loopInfo.fDelta = -loopInfo.fDelta; break; |
| 706 | default: |
| 707 | return "invalid operator in loop expression"; |
| 708 | } |
| 709 | } break; |
| 710 | case Expression::Kind::kPrefix: { |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 711 | const PrefixExpression& next = loopNext->as<PrefixExpression>(); |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 712 | if (!is_loop_index(next.operand())) { |
| 713 | return "expected loop index in loop expression"; |
| 714 | } |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 715 | switch (next.getOperator().kind()) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 716 | case Token::Kind::TK_PLUSPLUS: loopInfo.fDelta = 1; break; |
| 717 | case Token::Kind::TK_MINUSMINUS: loopInfo.fDelta = -1; break; |
| 718 | default: |
| 719 | return "invalid operator in loop expression"; |
| 720 | } |
| 721 | } break; |
| 722 | case Expression::Kind::kPostfix: { |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 723 | const PostfixExpression& next = loopNext->as<PostfixExpression>(); |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 724 | if (!is_loop_index(next.operand())) { |
| 725 | return "expected loop index in loop expression"; |
| 726 | } |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 727 | switch (next.getOperator().kind()) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 728 | case Token::Kind::TK_PLUSPLUS: loopInfo.fDelta = 1; break; |
| 729 | case Token::Kind::TK_MINUSMINUS: loopInfo.fDelta = -1; break; |
| 730 | default: |
| 731 | return "invalid operator in loop expression"; |
| 732 | } |
| 733 | } break; |
| 734 | default: |
| 735 | return "invalid loop expression"; |
| 736 | } |
| 737 | |
| 738 | // |
| 739 | // Within the body of the loop, the loop index is not statically assigned to, nor is it used as |
| 740 | // argument to a function 'out' or 'inout' parameter. |
| 741 | // |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 742 | if (Analysis::StatementWritesToVariable(*loopStatement, initDecl.var())) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 743 | return "loop index must not be modified within body of the loop"; |
| 744 | } |
| 745 | |
| 746 | // Finally, compute the iteration count, based on the bounds, and the termination operator. |
| 747 | constexpr int kMaxUnrollableLoopLength = 128; |
| 748 | loopInfo.fCount = 0; |
| 749 | |
| 750 | double val = loopInfo.fStart; |
| 751 | auto evalCond = [&]() { |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 752 | switch (cond.getOperator().kind()) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 753 | case Token::Kind::TK_GT: return val > loopEnd; |
| 754 | case Token::Kind::TK_GTEQ: return val >= loopEnd; |
| 755 | case Token::Kind::TK_LT: return val < loopEnd; |
| 756 | case Token::Kind::TK_LTEQ: return val <= loopEnd; |
| 757 | case Token::Kind::TK_EQEQ: return val == loopEnd; |
| 758 | case Token::Kind::TK_NEQ: return val != loopEnd; |
| 759 | default: SkUNREACHABLE; |
| 760 | } |
| 761 | }; |
| 762 | |
| 763 | for (loopInfo.fCount = 0; loopInfo.fCount <= kMaxUnrollableLoopLength; ++loopInfo.fCount) { |
| 764 | if (!evalCond()) { |
| 765 | break; |
| 766 | } |
| 767 | val += loopInfo.fDelta; |
| 768 | } |
| 769 | |
| 770 | if (loopInfo.fCount > kMaxUnrollableLoopLength) { |
| 771 | return "loop must guarantee termination in fewer iterations"; |
| 772 | } |
| 773 | |
| 774 | return nullptr; // All checks pass |
| 775 | } |
| 776 | |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 777 | bool Analysis::ForLoopIsValidForES2(int offset, |
| 778 | const Statement* loopInitializer, |
| 779 | const Expression* loopTest, |
| 780 | const Expression* loopNext, |
| 781 | const Statement* loopStatement, |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 782 | Analysis::UnrollableLoopInfo* outLoopInfo, |
| 783 | ErrorReporter* errors) { |
| 784 | UnrollableLoopInfo ignored, |
| 785 | *loopInfo = outLoopInfo ? outLoopInfo : &ignored; |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 786 | if (const char* msg = invalid_for_ES2( |
| 787 | offset, loopInitializer, loopTest, loopNext, loopStatement, *loopInfo)) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 788 | if (errors) { |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 789 | errors->error(offset, msg); |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 790 | } |
| 791 | return false; |
| 792 | } |
| 793 | return true; |
| 794 | } |
| 795 | |
Brian Osman | 7b36149 | 2021-02-25 11:25:30 -0500 | [diff] [blame] | 796 | // Checks for ES2 constant-expression rules, and (optionally) constant-index-expression rules |
| 797 | // (if loopIndices is non-nullptr) |
| 798 | class ConstantExpressionVisitor : public ProgramVisitor { |
Brian Osman | ea485e5 | 2021-01-15 13:20:32 -0500 | [diff] [blame] | 799 | public: |
Brian Osman | 7b36149 | 2021-02-25 11:25:30 -0500 | [diff] [blame] | 800 | ConstantExpressionVisitor(const std::set<const Variable*>* loopIndices) |
Brian Osman | ea485e5 | 2021-01-15 13:20:32 -0500 | [diff] [blame] | 801 | : fLoopIndices(loopIndices) {} |
| 802 | |
| 803 | bool visitExpression(const Expression& e) override { |
| 804 | // A constant-(index)-expression is one of... |
| 805 | switch (e.kind()) { |
| 806 | // ... a literal value |
| 807 | case Expression::Kind::kBoolLiteral: |
| 808 | case Expression::Kind::kIntLiteral: |
| 809 | case Expression::Kind::kFloatLiteral: |
| 810 | return false; |
| 811 | |
Brian Osman | 7b36149 | 2021-02-25 11:25:30 -0500 | [diff] [blame] | 812 | // ... a global or local variable qualified as 'const', excluding function parameters. |
| 813 | // ... loop indices as defined in section 4. [constant-index-expression] |
| 814 | case Expression::Kind::kVariableReference: { |
| 815 | const Variable* v = e.as<VariableReference>().variable(); |
| 816 | if ((v->storage() == Variable::Storage::kGlobal || |
| 817 | v->storage() == Variable::Storage::kLocal) && |
| 818 | (v->modifiers().fFlags & Modifiers::kConst_Flag)) { |
| 819 | return false; |
| 820 | } |
| 821 | return !fLoopIndices || fLoopIndices->find(v) == fLoopIndices->end(); |
| 822 | } |
Brian Osman | ea485e5 | 2021-01-15 13:20:32 -0500 | [diff] [blame] | 823 | |
| 824 | // ... expressions composed of both of the above |
| 825 | case Expression::Kind::kBinary: |
| 826 | case Expression::Kind::kConstructor: |
| 827 | case Expression::Kind::kFieldAccess: |
| 828 | case Expression::Kind::kIndex: |
| 829 | case Expression::Kind::kPrefix: |
| 830 | case Expression::Kind::kPostfix: |
| 831 | case Expression::Kind::kSwizzle: |
| 832 | case Expression::Kind::kTernary: |
| 833 | return INHERITED::visitExpression(e); |
| 834 | |
Brian Osman | 7b36149 | 2021-02-25 11:25:30 -0500 | [diff] [blame] | 835 | // These are completely disallowed in SkSL constant-(index)-expressions. GLSL allows |
Brian Osman | ea485e5 | 2021-01-15 13:20:32 -0500 | [diff] [blame] | 836 | // calls to built-in functions where the arguments are all constant-expressions, but |
| 837 | // we don't guarantee that behavior. (skbug.com/10835) |
| 838 | case Expression::Kind::kExternalFunctionCall: |
| 839 | case Expression::Kind::kFunctionCall: |
| 840 | return true; |
| 841 | |
| 842 | // These should never appear in final IR |
| 843 | case Expression::Kind::kDefined: |
| 844 | case Expression::Kind::kExternalFunctionReference: |
| 845 | case Expression::Kind::kFunctionReference: |
| 846 | case Expression::Kind::kSetting: |
| 847 | case Expression::Kind::kTypeReference: |
| 848 | default: |
| 849 | SkDEBUGFAIL("Unexpected expression type"); |
| 850 | return true; |
| 851 | } |
| 852 | } |
| 853 | |
| 854 | private: |
| 855 | const std::set<const Variable*>* fLoopIndices; |
| 856 | using INHERITED = ProgramVisitor; |
| 857 | }; |
| 858 | |
| 859 | class ES2IndexingVisitor : public ProgramVisitor { |
| 860 | public: |
| 861 | ES2IndexingVisitor(ErrorReporter& errors) : fErrors(errors) {} |
| 862 | |
| 863 | bool visitStatement(const Statement& s) override { |
| 864 | if (s.is<ForStatement>()) { |
| 865 | const ForStatement& f = s.as<ForStatement>(); |
| 866 | SkASSERT(f.initializer() && f.initializer()->is<VarDeclaration>()); |
| 867 | const Variable* var = &f.initializer()->as<VarDeclaration>().var(); |
| 868 | auto [iter, inserted] = fLoopIndices.insert(var); |
| 869 | SkASSERT(inserted); |
| 870 | bool result = this->visitStatement(*f.statement()); |
| 871 | fLoopIndices.erase(iter); |
| 872 | return result; |
| 873 | } |
| 874 | return INHERITED::visitStatement(s); |
| 875 | } |
| 876 | |
| 877 | bool visitExpression(const Expression& e) override { |
| 878 | if (e.is<IndexExpression>()) { |
| 879 | const IndexExpression& i = e.as<IndexExpression>(); |
Brian Osman | 7b36149 | 2021-02-25 11:25:30 -0500 | [diff] [blame] | 880 | ConstantExpressionVisitor indexerInvalid(&fLoopIndices); |
Brian Osman | ea485e5 | 2021-01-15 13:20:32 -0500 | [diff] [blame] | 881 | if (indexerInvalid.visitExpression(*i.index())) { |
| 882 | fErrors.error(i.fOffset, "index expression must be constant"); |
| 883 | return true; |
| 884 | } |
| 885 | } |
| 886 | return INHERITED::visitExpression(e); |
| 887 | } |
| 888 | |
| 889 | using ProgramVisitor::visitProgramElement; |
| 890 | |
| 891 | private: |
| 892 | ErrorReporter& fErrors; |
| 893 | std::set<const Variable*> fLoopIndices; |
| 894 | using INHERITED = ProgramVisitor; |
| 895 | }; |
| 896 | |
| 897 | |
| 898 | void Analysis::ValidateIndexingForES2(const ProgramElement& pe, ErrorReporter& errors) { |
| 899 | ES2IndexingVisitor visitor(errors); |
| 900 | visitor.visitProgramElement(pe); |
| 901 | } |
| 902 | |
Brian Osman | 7b36149 | 2021-02-25 11:25:30 -0500 | [diff] [blame] | 903 | bool Analysis::IsConstantExpression(const Expression& expr) { |
| 904 | ConstantExpressionVisitor visitor(/*loopIndices=*/nullptr); |
| 905 | return !visitor.visitExpression(expr); |
| 906 | } |
| 907 | |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 908 | //////////////////////////////////////////////////////////////////////////////// |
| 909 | // ProgramVisitor |
| 910 | |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 911 | bool ProgramVisitor::visit(const Program& program) { |
| 912 | for (const ProgramElement* pe : program.elements()) { |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 913 | if (this->visitProgramElement(*pe)) { |
John Stiles | 933abe3 | 2020-08-28 11:58:40 -0400 | [diff] [blame] | 914 | return true; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 915 | } |
| 916 | } |
John Stiles | 933abe3 | 2020-08-28 11:58:40 -0400 | [diff] [blame] | 917 | return false; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 918 | } |
| 919 | |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 920 | template <typename PROG, typename EXPR, typename STMT, typename ELEM> |
| 921 | bool TProgramVisitor<PROG, EXPR, STMT, ELEM>::visitExpression(EXPR e) { |
| 922 | switch (e.kind()) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 923 | case Expression::Kind::kBoolLiteral: |
| 924 | case Expression::Kind::kDefined: |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 925 | case Expression::Kind::kExternalFunctionReference: |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 926 | case Expression::Kind::kFloatLiteral: |
| 927 | case Expression::Kind::kFunctionReference: |
| 928 | case Expression::Kind::kIntLiteral: |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 929 | case Expression::Kind::kSetting: |
| 930 | case Expression::Kind::kTypeReference: |
| 931 | case Expression::Kind::kVariableReference: |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 932 | // Leaf expressions return false |
| 933 | return false; |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 934 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 935 | case Expression::Kind::kBinary: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 936 | auto& b = e.template as<BinaryExpression>(); |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 937 | return (b.left() && this->visitExpression(*b.left())) || |
| 938 | (b.right() && this->visitExpression(*b.right())); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 939 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 940 | case Expression::Kind::kConstructor: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 941 | auto& c = e.template as<Constructor>(); |
| 942 | for (auto& arg : c.arguments()) { |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 943 | if (this->visitExpression(*arg)) { return true; } |
| 944 | } |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 945 | return false; |
| 946 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 947 | case Expression::Kind::kExternalFunctionCall: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 948 | auto& c = e.template as<ExternalFunctionCall>(); |
Ethan Nicholas | 6e86ec9 | 2020-09-30 14:29:56 -0400 | [diff] [blame] | 949 | for (auto& arg : c.arguments()) { |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 950 | if (this->visitExpression(*arg)) { return true; } |
| 951 | } |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 952 | return false; |
| 953 | } |
John Stiles | d7ab450 | 2020-09-24 22:41:00 -0400 | [diff] [blame] | 954 | case Expression::Kind::kFieldAccess: |
Ethan Nicholas | 7a95b20 | 2020-10-09 11:55:40 -0400 | [diff] [blame] | 955 | return this->visitExpression(*e.template as<FieldAccess>().base()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 956 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 957 | case Expression::Kind::kFunctionCall: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 958 | auto& c = e.template as<FunctionCall>(); |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 959 | for (auto& arg : c.arguments()) { |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 960 | if (arg && this->visitExpression(*arg)) { return true; } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 961 | } |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 962 | return false; |
| 963 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 964 | case Expression::Kind::kIndex: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 965 | auto& i = e.template as<IndexExpression>(); |
Ethan Nicholas | 2a4952d | 2020-10-08 15:35:56 -0400 | [diff] [blame] | 966 | return this->visitExpression(*i.base()) || this->visitExpression(*i.index()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 967 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 968 | case Expression::Kind::kPostfix: |
Ethan Nicholas | 444ccc6 | 2020-10-09 10:16:22 -0400 | [diff] [blame] | 969 | return this->visitExpression(*e.template as<PostfixExpression>().operand()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 970 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 971 | case Expression::Kind::kPrefix: |
Ethan Nicholas | 444ccc6 | 2020-10-09 10:16:22 -0400 | [diff] [blame] | 972 | return this->visitExpression(*e.template as<PrefixExpression>().operand()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 973 | |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 974 | case Expression::Kind::kSwizzle: { |
| 975 | auto& s = e.template as<Swizzle>(); |
| 976 | return s.base() && this->visitExpression(*s.base()); |
| 977 | } |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 978 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 979 | case Expression::Kind::kTernary: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 980 | auto& t = e.template as<TernaryExpression>(); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 981 | return this->visitExpression(*t.test()) || |
| 982 | (t.ifTrue() && this->visitExpression(*t.ifTrue())) || |
| 983 | (t.ifFalse() && this->visitExpression(*t.ifFalse())); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 984 | } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 985 | default: |
| 986 | SkUNREACHABLE; |
| 987 | } |
| 988 | } |
| 989 | |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 990 | template <typename PROG, typename EXPR, typename STMT, typename ELEM> |
| 991 | bool TProgramVisitor<PROG, EXPR, STMT, ELEM>::visitStatement(STMT s) { |
| 992 | switch (s.kind()) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 993 | case Statement::Kind::kBreak: |
| 994 | case Statement::Kind::kContinue: |
| 995 | case Statement::Kind::kDiscard: |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 996 | case Statement::Kind::kInlineMarker: |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 997 | case Statement::Kind::kNop: |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 998 | // Leaf statements just return false |
| 999 | return false; |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1000 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1001 | case Statement::Kind::kBlock: |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1002 | for (auto& stmt : s.template as<Block>().children()) { |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1003 | if (stmt && this->visitStatement(*stmt)) { |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 1004 | return true; |
| 1005 | } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 1006 | } |
| 1007 | return false; |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1008 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1009 | case Statement::Kind::kDo: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1010 | auto& d = s.template as<DoStatement>(); |
| 1011 | return this->visitExpression(*d.test()) || this->visitStatement(*d.statement()); |
| 1012 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1013 | case Statement::Kind::kExpression: |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1014 | return this->visitExpression(*s.template as<ExpressionStatement>().expression()); |
| 1015 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1016 | case Statement::Kind::kFor: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1017 | auto& f = s.template as<ForStatement>(); |
Ethan Nicholas | 0d31ed5 | 2020-10-05 14:47:09 -0400 | [diff] [blame] | 1018 | return (f.initializer() && this->visitStatement(*f.initializer())) || |
| 1019 | (f.test() && this->visitExpression(*f.test())) || |
| 1020 | (f.next() && this->visitExpression(*f.next())) || |
| 1021 | this->visitStatement(*f.statement()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1022 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1023 | case Statement::Kind::kIf: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1024 | auto& i = s.template as<IfStatement>(); |
Brian Osman | 5567a60 | 2020-10-27 09:52:39 -0400 | [diff] [blame] | 1025 | return (i.test() && this->visitExpression(*i.test())) || |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1026 | (i.ifTrue() && this->visitStatement(*i.ifTrue())) || |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 1027 | (i.ifFalse() && this->visitStatement(*i.ifFalse())); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1028 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1029 | case Statement::Kind::kReturn: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1030 | auto& r = s.template as<ReturnStatement>(); |
Ethan Nicholas | 2a4952d | 2020-10-08 15:35:56 -0400 | [diff] [blame] | 1031 | return r.expression() && this->visitExpression(*r.expression()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1032 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1033 | case Statement::Kind::kSwitch: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1034 | auto& sw = s.template as<SwitchStatement>(); |
Ethan Nicholas | 01b05e5 | 2020-10-22 15:53:41 -0400 | [diff] [blame] | 1035 | if (this->visitExpression(*sw.value())) { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1036 | return true; |
| 1037 | } |
Ethan Nicholas | 01b05e5 | 2020-10-22 15:53:41 -0400 | [diff] [blame] | 1038 | for (const auto& c : sw.cases()) { |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 1039 | if (c->value() && this->visitExpression(*c->value())) { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1040 | return true; |
| 1041 | } |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 1042 | for (auto& st : c->statements()) { |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1043 | if (st && this->visitStatement(*st)) { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1044 | return true; |
| 1045 | } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 1046 | } |
| 1047 | } |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1048 | return false; |
| 1049 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1050 | case Statement::Kind::kVarDeclaration: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1051 | auto& v = s.template as<VarDeclaration>(); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 1052 | return v.value() && this->visitExpression(*v.value()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1053 | } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 1054 | default: |
| 1055 | SkUNREACHABLE; |
| 1056 | } |
| 1057 | } |
| 1058 | |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1059 | template <typename PROG, typename EXPR, typename STMT, typename ELEM> |
| 1060 | bool TProgramVisitor<PROG, EXPR, STMT, ELEM>::visitProgramElement(ELEM pe) { |
| 1061 | switch (pe.kind()) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1062 | case ProgramElement::Kind::kEnum: |
| 1063 | case ProgramElement::Kind::kExtension: |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1064 | case ProgramElement::Kind::kFunctionPrototype: |
John Stiles | d39aec0 | 2020-12-03 10:42:26 -0500 | [diff] [blame] | 1065 | case ProgramElement::Kind::kInterfaceBlock: |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1066 | case ProgramElement::Kind::kModifiers: |
| 1067 | case ProgramElement::Kind::kSection: |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 1068 | case ProgramElement::Kind::kStructDefinition: |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 1069 | // Leaf program elements just return false by default |
| 1070 | return false; |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1071 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1072 | case ProgramElement::Kind::kFunction: |
Ethan Nicholas | 0a5d096 | 2020-10-14 13:33:18 -0400 | [diff] [blame] | 1073 | return this->visitStatement(*pe.template as<FunctionDefinition>().body()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1074 | |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1075 | case ProgramElement::Kind::kGlobalVar: |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 1076 | if (this->visitStatement(*pe.template as<GlobalVarDeclaration>().declaration())) { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1077 | return true; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 1078 | } |
| 1079 | return false; |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1080 | |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 1081 | default: |
| 1082 | SkUNREACHABLE; |
| 1083 | } |
| 1084 | } |
| 1085 | |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1086 | template class TProgramVisitor<const Program&, const Expression&, |
| 1087 | const Statement&, const ProgramElement&>; |
| 1088 | template class TProgramVisitor<Program&, Expression&, Statement&, ProgramElement&>; |
| 1089 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 1090 | } // namespace SkSL |