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 | |
John Stiles | 106e0cd | 2021-09-07 11:43:51 -0400 | [diff] [blame] | 10 | #include "include/private/SkFloatingPoint.h" |
Ethan Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 11 | #include "include/private/SkSLModifiers.h" |
Ethan Nicholas | 24c1772 | 2021-03-09 13:10:59 -0500 | [diff] [blame] | 12 | #include "include/private/SkSLProgramElement.h" |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 13 | #include "include/private/SkSLSampleUsage.h" |
Ethan Nicholas | 24c1772 | 2021-03-09 13:10:59 -0500 | [diff] [blame] | 14 | #include "include/private/SkSLStatement.h" |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 15 | #include "include/sksl/SkSLErrorReporter.h" |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 16 | #include "src/sksl/SkSLCompiler.h" |
Brian Osman | 448b2d5 | 2021-09-23 11:36:15 -0400 | [diff] [blame] | 17 | #include "src/sksl/SkSLConstantFolder.h" |
Ethan Nicholas | 1780755 | 2021-10-01 09:42:36 -0400 | [diff] [blame] | 18 | #include "src/sksl/analysis/SkSLProgramVisitor.h" |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 19 | #include "src/sksl/ir/SkSLExpression.h" |
| 20 | #include "src/sksl/ir/SkSLProgram.h" |
Ethan Nicholas | 1780755 | 2021-10-01 09:42:36 -0400 | [diff] [blame] | 21 | #include "src/sksl/transform/SkSLProgramWriter.h" |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 22 | |
| 23 | // ProgramElements |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 24 | #include "src/sksl/ir/SkSLExtension.h" |
| 25 | #include "src/sksl/ir/SkSLFunctionDefinition.h" |
| 26 | #include "src/sksl/ir/SkSLInterfaceBlock.h" |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 27 | #include "src/sksl/ir/SkSLVarDeclarations.h" |
| 28 | |
| 29 | // Statements |
| 30 | #include "src/sksl/ir/SkSLBlock.h" |
| 31 | #include "src/sksl/ir/SkSLBreakStatement.h" |
| 32 | #include "src/sksl/ir/SkSLContinueStatement.h" |
| 33 | #include "src/sksl/ir/SkSLDiscardStatement.h" |
| 34 | #include "src/sksl/ir/SkSLDoStatement.h" |
| 35 | #include "src/sksl/ir/SkSLExpressionStatement.h" |
| 36 | #include "src/sksl/ir/SkSLForStatement.h" |
| 37 | #include "src/sksl/ir/SkSLIfStatement.h" |
| 38 | #include "src/sksl/ir/SkSLNop.h" |
| 39 | #include "src/sksl/ir/SkSLReturnStatement.h" |
| 40 | #include "src/sksl/ir/SkSLSwitchStatement.h" |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 41 | |
| 42 | // Expressions |
| 43 | #include "src/sksl/ir/SkSLBinaryExpression.h" |
Brian Osman | eb0f29d | 2021-08-04 11:34:16 -0400 | [diff] [blame] | 44 | #include "src/sksl/ir/SkSLChildCall.h" |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 45 | #include "src/sksl/ir/SkSLConstructor.h" |
John Stiles | e118278 | 2021-03-30 22:09:37 -0400 | [diff] [blame] | 46 | #include "src/sksl/ir/SkSLConstructorDiagonalMatrix.h" |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 47 | #include "src/sksl/ir/SkSLConstructorMatrixResize.h" |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 48 | #include "src/sksl/ir/SkSLExternalFunctionCall.h" |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 49 | #include "src/sksl/ir/SkSLExternalFunctionReference.h" |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 50 | #include "src/sksl/ir/SkSLFieldAccess.h" |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 51 | #include "src/sksl/ir/SkSLFunctionCall.h" |
| 52 | #include "src/sksl/ir/SkSLFunctionReference.h" |
| 53 | #include "src/sksl/ir/SkSLIndexExpression.h" |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 54 | #include "src/sksl/ir/SkSLInlineMarker.h" |
John Stiles | 7591d4b | 2021-09-13 13:32:06 -0400 | [diff] [blame] | 55 | #include "src/sksl/ir/SkSLLiteral.h" |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 56 | #include "src/sksl/ir/SkSLPostfixExpression.h" |
| 57 | #include "src/sksl/ir/SkSLPrefixExpression.h" |
| 58 | #include "src/sksl/ir/SkSLSetting.h" |
| 59 | #include "src/sksl/ir/SkSLSwizzle.h" |
| 60 | #include "src/sksl/ir/SkSLTernaryExpression.h" |
| 61 | #include "src/sksl/ir/SkSLTypeReference.h" |
| 62 | #include "src/sksl/ir/SkSLVariableReference.h" |
| 63 | |
| 64 | namespace SkSL { |
| 65 | |
| 66 | namespace { |
| 67 | |
Brian Osman | eb0f29d | 2021-08-04 11:34:16 -0400 | [diff] [blame] | 68 | // Visitor that determines the merged SampleUsage for a given child in the program. |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 69 | class MergeSampleUsageVisitor : public ProgramVisitor { |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 70 | public: |
Brian Osman | eb0f29d | 2021-08-04 11:34:16 -0400 | [diff] [blame] | 71 | MergeSampleUsageVisitor(const Context& context, |
| 72 | const Variable& child, |
| 73 | bool writesToSampleCoords) |
| 74 | : fContext(context), fChild(child), fWritesToSampleCoords(writesToSampleCoords) {} |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 75 | |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 76 | SampleUsage visit(const Program& program) { |
| 77 | fUsage = SampleUsage(); // reset to none |
John Stiles | d7ab450 | 2020-09-24 22:41:00 -0400 | [diff] [blame] | 78 | INHERITED::visit(program); |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 79 | return fUsage; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 80 | } |
| 81 | |
Brian Osman | 8cdf28f | 2021-05-24 09:52:39 -0400 | [diff] [blame] | 82 | int elidedSampleCoordCount() const { return fElidedSampleCoordCount; } |
| 83 | |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 84 | protected: |
John Stiles | 933abe3 | 2020-08-28 11:58:40 -0400 | [diff] [blame] | 85 | const Context& fContext; |
Brian Osman | eb0f29d | 2021-08-04 11:34:16 -0400 | [diff] [blame] | 86 | const Variable& fChild; |
Brian Osman | 4d57111 | 2021-04-27 09:10:10 -0400 | [diff] [blame] | 87 | const bool fWritesToSampleCoords; |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 88 | SampleUsage fUsage; |
Brian Osman | 8cdf28f | 2021-05-24 09:52:39 -0400 | [diff] [blame] | 89 | int fElidedSampleCoordCount = 0; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 90 | |
| 91 | bool visitExpression(const Expression& e) override { |
Brian Osman | eb0f29d | 2021-08-04 11:34:16 -0400 | [diff] [blame] | 92 | // Looking for child(...) |
| 93 | if (e.is<ChildCall>() && &e.as<ChildCall>().child() == &fChild) { |
| 94 | // Determine the type of call at this site, and merge it with the accumulated state |
| 95 | const ExpressionArray& arguments = e.as<ChildCall>().arguments(); |
| 96 | SkASSERT(arguments.size() >= 1); |
| 97 | |
| 98 | const Expression* maybeCoords = arguments[0].get(); |
| 99 | if (maybeCoords->type() == *fContext.fTypes.fFloat2) { |
| 100 | // If the coords are a direct reference to the program's sample-coords, and those |
| 101 | // coords are never modified, we can conservatively turn this into PassThrough |
| 102 | // sampling. In all other cases, we consider it Explicit. |
| 103 | if (!fWritesToSampleCoords && maybeCoords->is<VariableReference>() && |
| 104 | maybeCoords->as<VariableReference>().variable()->modifiers().fLayout.fBuiltin == |
| 105 | SK_MAIN_COORDS_BUILTIN) { |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 106 | fUsage.merge(SampleUsage::PassThrough()); |
Brian Osman | eb0f29d | 2021-08-04 11:34:16 -0400 | [diff] [blame] | 107 | ++fElidedSampleCoordCount; |
| 108 | } else { |
| 109 | fUsage.merge(SampleUsage::Explicit()); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 110 | } |
Brian Osman | eb0f29d | 2021-08-04 11:34:16 -0400 | [diff] [blame] | 111 | } else { |
| 112 | // child(inputColor) or child(srcColor, dstColor) -> PassThrough |
| 113 | fUsage.merge(SampleUsage::PassThrough()); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
John Stiles | d7ab450 | 2020-09-24 22:41:00 -0400 | [diff] [blame] | 117 | return INHERITED::visitExpression(e); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 118 | } |
| 119 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 120 | using INHERITED = ProgramVisitor; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 121 | }; |
| 122 | |
Brian Osman | 92aac1e | 2020-08-05 16:48:58 -0400 | [diff] [blame] | 123 | // Visitor that searches through the program for references to a particular builtin variable |
| 124 | class BuiltinVariableVisitor : public ProgramVisitor { |
| 125 | public: |
| 126 | BuiltinVariableVisitor(int builtin) : fBuiltin(builtin) {} |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 127 | |
| 128 | bool visitExpression(const Expression& e) override { |
Brian Osman | 79457ef | 2020-09-24 15:01:27 -0400 | [diff] [blame] | 129 | if (e.is<VariableReference>()) { |
John Stiles | 403a363 | 2020-08-20 12:11:48 -0400 | [diff] [blame] | 130 | const VariableReference& var = e.as<VariableReference>(); |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 131 | return var.variable()->modifiers().fLayout.fBuiltin == fBuiltin; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 132 | } |
John Stiles | d7ab450 | 2020-09-24 22:41:00 -0400 | [diff] [blame] | 133 | return INHERITED::visitExpression(e); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 134 | } |
| 135 | |
Brian Osman | 92aac1e | 2020-08-05 16:48:58 -0400 | [diff] [blame] | 136 | int fBuiltin; |
| 137 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 138 | using INHERITED = ProgramVisitor; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 139 | }; |
| 140 | |
Brian Osman | eb0f29d | 2021-08-04 11:34:16 -0400 | [diff] [blame] | 141 | // Visitor that searches for child calls from a function other than main() |
Brian Osman | 04d79fc | 2021-07-02 13:25:35 -0400 | [diff] [blame] | 142 | class SampleOutsideMainVisitor : public ProgramVisitor { |
| 143 | public: |
| 144 | SampleOutsideMainVisitor() {} |
| 145 | |
| 146 | bool visitExpression(const Expression& e) override { |
Brian Osman | eb0f29d | 2021-08-04 11:34:16 -0400 | [diff] [blame] | 147 | if (e.is<ChildCall>()) { |
| 148 | return true; |
Brian Osman | 04d79fc | 2021-07-02 13:25:35 -0400 | [diff] [blame] | 149 | } |
| 150 | return INHERITED::visitExpression(e); |
| 151 | } |
| 152 | |
| 153 | bool visitProgramElement(const ProgramElement& p) override { |
| 154 | return p.is<FunctionDefinition>() && |
| 155 | !p.as<FunctionDefinition>().declaration().isMain() && |
| 156 | INHERITED::visitProgramElement(p); |
| 157 | } |
| 158 | |
| 159 | using INHERITED = ProgramVisitor; |
| 160 | }; |
| 161 | |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 162 | // Visitor that counts the number of nodes visited |
| 163 | class NodeCountVisitor : public ProgramVisitor { |
| 164 | public: |
John Stiles | 2c1e492 | 2020-10-01 09:14:14 -0400 | [diff] [blame] | 165 | NodeCountVisitor(int limit) : fLimit(limit) {} |
| 166 | |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 167 | int visit(const Statement& s) { |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 168 | this->visitStatement(s); |
| 169 | return fCount; |
| 170 | } |
| 171 | |
| 172 | bool visitExpression(const Expression& e) override { |
| 173 | ++fCount; |
John Stiles | 9b9415e | 2020-11-23 14:48:06 -0500 | [diff] [blame] | 174 | return (fCount >= fLimit) || INHERITED::visitExpression(e); |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | bool visitProgramElement(const ProgramElement& p) override { |
| 178 | ++fCount; |
John Stiles | 9b9415e | 2020-11-23 14:48:06 -0500 | [diff] [blame] | 179 | return (fCount >= fLimit) || INHERITED::visitProgramElement(p); |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | bool visitStatement(const Statement& s) override { |
| 183 | ++fCount; |
John Stiles | 9b9415e | 2020-11-23 14:48:06 -0500 | [diff] [blame] | 184 | return (fCount >= fLimit) || INHERITED::visitStatement(s); |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | private: |
John Stiles | 2c1e492 | 2020-10-01 09:14:14 -0400 | [diff] [blame] | 188 | int fCount = 0; |
| 189 | int fLimit; |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 190 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 191 | using INHERITED = ProgramVisitor; |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 192 | }; |
| 193 | |
Ethan Nicholas | 765d2fe | 2020-08-26 08:29:55 -0400 | [diff] [blame] | 194 | class VariableWriteVisitor : public ProgramVisitor { |
| 195 | public: |
| 196 | VariableWriteVisitor(const Variable* var) |
| 197 | : fVar(var) {} |
| 198 | |
| 199 | bool visit(const Statement& s) { |
| 200 | return this->visitStatement(s); |
| 201 | } |
| 202 | |
| 203 | bool visitExpression(const Expression& e) override { |
Brian Osman | 79457ef | 2020-09-24 15:01:27 -0400 | [diff] [blame] | 204 | if (e.is<VariableReference>()) { |
Ethan Nicholas | 765d2fe | 2020-08-26 08:29:55 -0400 | [diff] [blame] | 205 | const VariableReference& ref = e.as<VariableReference>(); |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 206 | if (ref.variable() == fVar && |
| 207 | (ref.refKind() == VariableReference::RefKind::kWrite || |
| 208 | ref.refKind() == VariableReference::RefKind::kReadWrite || |
| 209 | ref.refKind() == VariableReference::RefKind::kPointer)) { |
Ethan Nicholas | 765d2fe | 2020-08-26 08:29:55 -0400 | [diff] [blame] | 210 | return true; |
| 211 | } |
| 212 | } |
John Stiles | d7ab450 | 2020-09-24 22:41:00 -0400 | [diff] [blame] | 213 | return INHERITED::visitExpression(e); |
Ethan Nicholas | 765d2fe | 2020-08-26 08:29:55 -0400 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | private: |
| 217 | const Variable* fVar; |
| 218 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 219 | using INHERITED = ProgramVisitor; |
Ethan Nicholas | 765d2fe | 2020-08-26 08:29:55 -0400 | [diff] [blame] | 220 | }; |
| 221 | |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 222 | // If a caller doesn't care about errors, we can use this trivial reporter that just counts up. |
| 223 | class TrivialErrorReporter : public ErrorReporter { |
| 224 | public: |
John Stiles | bb8cf58 | 2021-08-26 23:34:59 -0400 | [diff] [blame] | 225 | ~TrivialErrorReporter() override { this->reportPendingErrors({}); } |
Ethan Nicholas | 3272412 | 2021-09-07 13:49:07 -0400 | [diff] [blame] | 226 | void handleError(skstd::string_view, PositionInfo) override {} |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 227 | }; |
| 228 | |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 229 | // This isn't actually using ProgramVisitor, because it only considers a subset of the fields for |
| 230 | // any given expression kind. For instance, when indexing an array (e.g. `x[1]`), we only want to |
| 231 | // know if the base (`x`) is assignable; the index expression (`1`) doesn't need to be. |
| 232 | class IsAssignableVisitor { |
| 233 | public: |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 234 | IsAssignableVisitor(ErrorReporter* errors) : fErrors(errors) {} |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 235 | |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 236 | bool visit(Expression& expr, Analysis::AssignmentInfo* info) { |
Ethan Nicholas | 67a0a8a | 2021-01-13 12:36:02 -0500 | [diff] [blame] | 237 | int oldErrorCount = fErrors->errorCount(); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 238 | this->visitExpression(expr); |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 239 | if (info) { |
| 240 | info->fAssignedVar = fAssignedVar; |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 241 | } |
Ethan Nicholas | 67a0a8a | 2021-01-13 12:36:02 -0500 | [diff] [blame] | 242 | return fErrors->errorCount() == oldErrorCount; |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | void visitExpression(Expression& expr) { |
| 246 | switch (expr.kind()) { |
| 247 | case Expression::Kind::kVariableReference: { |
| 248 | VariableReference& varRef = expr.as<VariableReference>(); |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 249 | const Variable* var = varRef.variable(); |
Brian Osman | e49703f | 2021-04-19 11:15:24 -0400 | [diff] [blame] | 250 | if (var->modifiers().fFlags & (Modifiers::kConst_Flag | Modifiers::kUniform_Flag)) { |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame] | 251 | fErrors->error(expr.fLine, |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 252 | "cannot modify immutable variable '" + var->name() + "'"); |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 253 | } else { |
| 254 | SkASSERT(fAssignedVar == nullptr); |
| 255 | fAssignedVar = &varRef; |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 256 | } |
| 257 | break; |
| 258 | } |
| 259 | case Expression::Kind::kFieldAccess: |
Ethan Nicholas | 7a95b20 | 2020-10-09 11:55:40 -0400 | [diff] [blame] | 260 | this->visitExpression(*expr.as<FieldAccess>().base()); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 261 | break; |
| 262 | |
| 263 | case Expression::Kind::kSwizzle: { |
| 264 | const Swizzle& swizzle = expr.as<Swizzle>(); |
| 265 | this->checkSwizzleWrite(swizzle); |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 266 | this->visitExpression(*swizzle.base()); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 267 | break; |
| 268 | } |
John Stiles | 47c0a74 | 2021-02-09 09:30:35 -0500 | [diff] [blame] | 269 | case Expression::Kind::kIndex: |
| 270 | this->visitExpression(*expr.as<IndexExpression>().base()); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 271 | break; |
John Stiles | 47c0a74 | 2021-02-09 09:30:35 -0500 | [diff] [blame] | 272 | |
Ethan Nicholas | c898d04 | 2021-08-28 19:53:34 -0400 | [diff] [blame] | 273 | case Expression::Kind::kPoison: |
| 274 | break; |
| 275 | |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 276 | default: |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame] | 277 | fErrors->error(expr.fLine, "cannot assign to this expression"); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 278 | break; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | private: |
| 283 | void checkSwizzleWrite(const Swizzle& swizzle) { |
| 284 | int bits = 0; |
John Stiles | 6e88e04 | 2021-02-19 14:09:38 -0500 | [diff] [blame] | 285 | for (int8_t idx : swizzle.components()) { |
| 286 | SkASSERT(idx >= SwizzleComponent::X && idx <= SwizzleComponent::W); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 287 | int bit = 1 << idx; |
| 288 | if (bits & bit) { |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame] | 289 | fErrors->error(swizzle.fLine, |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 290 | "cannot write to the same swizzle field more than once"); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 291 | break; |
| 292 | } |
| 293 | bits |= bit; |
| 294 | } |
| 295 | } |
| 296 | |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 297 | ErrorReporter* fErrors; |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 298 | VariableReference* fAssignedVar = nullptr; |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 299 | |
| 300 | using INHERITED = ProgramVisitor; |
| 301 | }; |
| 302 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 303 | } // namespace |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 304 | |
| 305 | //////////////////////////////////////////////////////////////////////////////// |
| 306 | // Analysis |
| 307 | |
Brian Osman | 4d57111 | 2021-04-27 09:10:10 -0400 | [diff] [blame] | 308 | SampleUsage Analysis::GetSampleUsage(const Program& program, |
Brian Osman | 293497e | 2021-08-24 14:08:50 -0400 | [diff] [blame] | 309 | const Variable& child, |
Brian Osman | 8cdf28f | 2021-05-24 09:52:39 -0400 | [diff] [blame] | 310 | bool writesToSampleCoords, |
| 311 | int* elidedSampleCoordCount) { |
Brian Osman | 293497e | 2021-08-24 14:08:50 -0400 | [diff] [blame] | 312 | MergeSampleUsageVisitor visitor(*program.fContext, child, writesToSampleCoords); |
Brian Osman | 8cdf28f | 2021-05-24 09:52:39 -0400 | [diff] [blame] | 313 | SampleUsage result = visitor.visit(program); |
| 314 | if (elidedSampleCoordCount) { |
| 315 | *elidedSampleCoordCount += visitor.elidedSampleCoordCount(); |
| 316 | } |
| 317 | return result; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 318 | } |
| 319 | |
Brian Osman | 92aac1e | 2020-08-05 16:48:58 -0400 | [diff] [blame] | 320 | bool Analysis::ReferencesBuiltin(const Program& program, int builtin) { |
| 321 | BuiltinVariableVisitor visitor(builtin); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 322 | return visitor.visit(program); |
| 323 | } |
| 324 | |
Brian Osman | 92aac1e | 2020-08-05 16:48:58 -0400 | [diff] [blame] | 325 | bool Analysis::ReferencesSampleCoords(const Program& program) { |
| 326 | return Analysis::ReferencesBuiltin(program, SK_MAIN_COORDS_BUILTIN); |
| 327 | } |
| 328 | |
| 329 | bool Analysis::ReferencesFragCoords(const Program& program) { |
| 330 | return Analysis::ReferencesBuiltin(program, SK_FRAGCOORD_BUILTIN); |
| 331 | } |
| 332 | |
Brian Osman | 04d79fc | 2021-07-02 13:25:35 -0400 | [diff] [blame] | 333 | bool Analysis::CallsSampleOutsideMain(const Program& program) { |
| 334 | SampleOutsideMainVisitor visitor; |
| 335 | return visitor.visit(program); |
| 336 | } |
| 337 | |
Ethan Nicholas | 98eae1e | 2021-09-01 14:37:29 -0400 | [diff] [blame] | 338 | bool Analysis::DetectVarDeclarationWithoutScope(const Statement& stmt, ErrorReporter* errors) { |
| 339 | // A variable declaration can create either a lone VarDeclaration or an unscoped Block |
| 340 | // containing multiple VarDeclaration statements. We need to detect either case. |
| 341 | const Variable* var; |
| 342 | if (stmt.is<VarDeclaration>()) { |
| 343 | // The single-variable case. No blocks at all. |
| 344 | var = &stmt.as<VarDeclaration>().var(); |
| 345 | } else if (stmt.is<Block>()) { |
| 346 | // The multiple-variable case: an unscoped, non-empty block... |
| 347 | const Block& block = stmt.as<Block>(); |
| 348 | if (block.isScope() || block.children().empty()) { |
| 349 | return false; |
| 350 | } |
| 351 | // ... holding a variable declaration. |
| 352 | const Statement& innerStmt = *block.children().front(); |
| 353 | if (!innerStmt.is<VarDeclaration>()) { |
| 354 | return false; |
| 355 | } |
| 356 | var = &innerStmt.as<VarDeclaration>().var(); |
| 357 | } else { |
| 358 | // This statement wasn't a variable declaration. No problem. |
| 359 | return false; |
| 360 | } |
| 361 | |
| 362 | // Report an error. |
| 363 | SkASSERT(var); |
| 364 | if (errors) { |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame] | 365 | errors->error(stmt.fLine, "variable '" + var->name() + "' must be created in a scope"); |
Ethan Nicholas | 98eae1e | 2021-09-01 14:37:29 -0400 | [diff] [blame] | 366 | } |
| 367 | return true; |
| 368 | } |
| 369 | |
John Stiles | 9b9415e | 2020-11-23 14:48:06 -0500 | [diff] [blame] | 370 | int Analysis::NodeCountUpToLimit(const FunctionDefinition& function, int limit) { |
| 371 | return NodeCountVisitor{limit}.visit(*function.body()); |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 372 | } |
| 373 | |
Ethan Nicholas | 765d2fe | 2020-08-26 08:29:55 -0400 | [diff] [blame] | 374 | bool Analysis::StatementWritesToVariable(const Statement& stmt, const Variable& var) { |
| 375 | return VariableWriteVisitor(&var).visit(stmt); |
| 376 | } |
| 377 | |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 378 | bool Analysis::IsAssignable(Expression& expr, AssignmentInfo* info, ErrorReporter* errors) { |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 379 | TrivialErrorReporter trivialErrors; |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 380 | return IsAssignableVisitor{errors ? errors : &trivialErrors}.visit(expr, info); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 381 | } |
| 382 | |
John Stiles | bb8cf58 | 2021-08-26 23:34:59 -0400 | [diff] [blame] | 383 | bool Analysis::UpdateVariableRefKind(Expression* expr, |
| 384 | VariableReference::RefKind kind, |
| 385 | ErrorReporter* errors) { |
John Stiles | 516704b | 2021-02-26 15:01:57 -0500 | [diff] [blame] | 386 | Analysis::AssignmentInfo info; |
| 387 | if (!Analysis::IsAssignable(*expr, &info, errors)) { |
| 388 | return false; |
| 389 | } |
| 390 | if (!info.fAssignedVar) { |
John Stiles | bb8cf58 | 2021-08-26 23:34:59 -0400 | [diff] [blame] | 391 | if (errors) { |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame] | 392 | errors->error(expr->fLine, "can't assign to expression '" + expr->description() + "'"); |
John Stiles | bb8cf58 | 2021-08-26 23:34:59 -0400 | [diff] [blame] | 393 | } |
John Stiles | 516704b | 2021-02-26 15:01:57 -0500 | [diff] [blame] | 394 | return false; |
| 395 | } |
| 396 | info.fAssignedVar->setRefKind(kind); |
| 397 | return true; |
| 398 | } |
| 399 | |
John Stiles | c30fbca | 2020-11-19 16:25:49 -0500 | [diff] [blame] | 400 | bool Analysis::IsTrivialExpression(const Expression& expr) { |
John Stiles | 7591d4b | 2021-09-13 13:32:06 -0400 | [diff] [blame] | 401 | return expr.is<Literal>() || |
John Stiles | c30fbca | 2020-11-19 16:25:49 -0500 | [diff] [blame] | 402 | expr.is<VariableReference>() || |
| 403 | (expr.is<Swizzle>() && |
| 404 | IsTrivialExpression(*expr.as<Swizzle>().base())) || |
| 405 | (expr.is<FieldAccess>() && |
| 406 | IsTrivialExpression(*expr.as<FieldAccess>().base())) || |
John Stiles | 2938eea | 2021-04-01 18:58:25 -0400 | [diff] [blame] | 407 | (expr.isAnyConstructor() && |
| 408 | expr.asAnyConstructor().argumentSpan().size() == 1 && |
| 409 | IsTrivialExpression(*expr.asAnyConstructor().argumentSpan().front())) || |
| 410 | (expr.isAnyConstructor() && |
John Stiles | c30fbca | 2020-11-19 16:25:49 -0500 | [diff] [blame] | 411 | expr.isConstantOrUniform()) || |
| 412 | (expr.is<IndexExpression>() && |
John Stiles | 7591d4b | 2021-09-13 13:32:06 -0400 | [diff] [blame] | 413 | expr.as<IndexExpression>().index()->isIntLiteral() && |
John Stiles | c30fbca | 2020-11-19 16:25:49 -0500 | [diff] [blame] | 414 | IsTrivialExpression(*expr.as<IndexExpression>().base())); |
| 415 | } |
| 416 | |
John Stiles | 5676c57 | 2021-03-08 17:10:52 -0500 | [diff] [blame] | 417 | bool Analysis::IsSameExpressionTree(const Expression& left, const Expression& right) { |
John Stiles | 95d0bad | 2021-03-01 17:02:28 -0500 | [diff] [blame] | 418 | if (left.kind() != right.kind() || left.type() != right.type()) { |
| 419 | return false; |
| 420 | } |
| 421 | |
John Stiles | 5676c57 | 2021-03-08 17:10:52 -0500 | [diff] [blame] | 422 | // This isn't a fully exhaustive list of expressions by any stretch of the imagination; for |
| 423 | // instance, `x[y+1] = x[y+1]` isn't detected because we don't look at BinaryExpressions. |
| 424 | // Since this is intended to be used for optimization purposes, handling the common cases is |
| 425 | // sufficient. |
John Stiles | 95d0bad | 2021-03-01 17:02:28 -0500 | [diff] [blame] | 426 | switch (left.kind()) { |
John Stiles | 7591d4b | 2021-09-13 13:32:06 -0400 | [diff] [blame] | 427 | case Expression::Kind::kLiteral: |
| 428 | return left.as<Literal>().value() == right.as<Literal>().value(); |
John Stiles | 5676c57 | 2021-03-08 17:10:52 -0500 | [diff] [blame] | 429 | |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 430 | case Expression::Kind::kConstructorArray: |
John Stiles | e3ae968 | 2021-08-05 10:35:01 -0400 | [diff] [blame] | 431 | case Expression::Kind::kConstructorArrayCast: |
John Stiles | 8cad637 | 2021-04-07 12:31:13 -0400 | [diff] [blame] | 432 | case Expression::Kind::kConstructorCompound: |
| 433 | case Expression::Kind::kConstructorCompoundCast: |
John Stiles | 2938eea | 2021-04-01 18:58:25 -0400 | [diff] [blame] | 434 | case Expression::Kind::kConstructorDiagonalMatrix: |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 435 | case Expression::Kind::kConstructorMatrixResize: |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 436 | case Expression::Kind::kConstructorScalarCast: |
John Stiles | d47330f | 2021-04-08 23:25:52 -0400 | [diff] [blame] | 437 | case Expression::Kind::kConstructorStruct: |
John Stiles | 268a73f | 2021-04-07 12:30:22 -0400 | [diff] [blame] | 438 | case Expression::Kind::kConstructorSplat: { |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 439 | if (left.kind() != right.kind()) { |
| 440 | return false; |
| 441 | } |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 442 | const AnyConstructor& leftCtor = left.asAnyConstructor(); |
| 443 | const AnyConstructor& rightCtor = right.asAnyConstructor(); |
| 444 | const auto leftSpan = leftCtor.argumentSpan(); |
| 445 | const auto rightSpan = rightCtor.argumentSpan(); |
| 446 | if (leftSpan.size() != rightSpan.size()) { |
John Stiles | 5676c57 | 2021-03-08 17:10:52 -0500 | [diff] [blame] | 447 | return false; |
| 448 | } |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 449 | for (size_t index = 0; index < leftSpan.size(); ++index) { |
| 450 | if (!IsSameExpressionTree(*leftSpan[index], *rightSpan[index])) { |
John Stiles | 5676c57 | 2021-03-08 17:10:52 -0500 | [diff] [blame] | 451 | return false; |
| 452 | } |
| 453 | } |
| 454 | return true; |
| 455 | } |
John Stiles | 95d0bad | 2021-03-01 17:02:28 -0500 | [diff] [blame] | 456 | case Expression::Kind::kFieldAccess: |
| 457 | return left.as<FieldAccess>().fieldIndex() == right.as<FieldAccess>().fieldIndex() && |
John Stiles | 5676c57 | 2021-03-08 17:10:52 -0500 | [diff] [blame] | 458 | IsSameExpressionTree(*left.as<FieldAccess>().base(), |
| 459 | *right.as<FieldAccess>().base()); |
John Stiles | 95d0bad | 2021-03-01 17:02:28 -0500 | [diff] [blame] | 460 | |
| 461 | case Expression::Kind::kIndex: |
John Stiles | 5676c57 | 2021-03-08 17:10:52 -0500 | [diff] [blame] | 462 | return IsSameExpressionTree(*left.as<IndexExpression>().index(), |
| 463 | *right.as<IndexExpression>().index()) && |
| 464 | IsSameExpressionTree(*left.as<IndexExpression>().base(), |
| 465 | *right.as<IndexExpression>().base()); |
John Stiles | 95d0bad | 2021-03-01 17:02:28 -0500 | [diff] [blame] | 466 | |
| 467 | case Expression::Kind::kSwizzle: |
| 468 | return left.as<Swizzle>().components() == right.as<Swizzle>().components() && |
John Stiles | 5676c57 | 2021-03-08 17:10:52 -0500 | [diff] [blame] | 469 | IsSameExpressionTree(*left.as<Swizzle>().base(), *right.as<Swizzle>().base()); |
John Stiles | 95d0bad | 2021-03-01 17:02:28 -0500 | [diff] [blame] | 470 | |
| 471 | case Expression::Kind::kVariableReference: |
| 472 | return left.as<VariableReference>().variable() == |
| 473 | right.as<VariableReference>().variable(); |
| 474 | |
| 475 | default: |
| 476 | return false; |
| 477 | } |
| 478 | } |
| 479 | |
Brian Osman | 7b36149 | 2021-02-25 11:25:30 -0500 | [diff] [blame] | 480 | // Checks for ES2 constant-expression rules, and (optionally) constant-index-expression rules |
| 481 | // (if loopIndices is non-nullptr) |
| 482 | class ConstantExpressionVisitor : public ProgramVisitor { |
Brian Osman | ea485e5 | 2021-01-15 13:20:32 -0500 | [diff] [blame] | 483 | public: |
Brian Osman | 7b36149 | 2021-02-25 11:25:30 -0500 | [diff] [blame] | 484 | ConstantExpressionVisitor(const std::set<const Variable*>* loopIndices) |
Brian Osman | ea485e5 | 2021-01-15 13:20:32 -0500 | [diff] [blame] | 485 | : fLoopIndices(loopIndices) {} |
| 486 | |
| 487 | bool visitExpression(const Expression& e) override { |
| 488 | // A constant-(index)-expression is one of... |
| 489 | switch (e.kind()) { |
| 490 | // ... a literal value |
John Stiles | 7591d4b | 2021-09-13 13:32:06 -0400 | [diff] [blame] | 491 | case Expression::Kind::kLiteral: |
Brian Osman | ea485e5 | 2021-01-15 13:20:32 -0500 | [diff] [blame] | 492 | return false; |
| 493 | |
John Stiles | 532138c | 2021-03-04 16:29:22 -0500 | [diff] [blame] | 494 | // ... settings can appear in fragment processors; they will resolve when compiled |
| 495 | case Expression::Kind::kSetting: |
| 496 | return false; |
| 497 | |
Brian Osman | 7b36149 | 2021-02-25 11:25:30 -0500 | [diff] [blame] | 498 | // ... a global or local variable qualified as 'const', excluding function parameters. |
| 499 | // ... loop indices as defined in section 4. [constant-index-expression] |
| 500 | case Expression::Kind::kVariableReference: { |
| 501 | const Variable* v = e.as<VariableReference>().variable(); |
| 502 | if ((v->storage() == Variable::Storage::kGlobal || |
| 503 | v->storage() == Variable::Storage::kLocal) && |
| 504 | (v->modifiers().fFlags & Modifiers::kConst_Flag)) { |
| 505 | return false; |
| 506 | } |
| 507 | return !fLoopIndices || fLoopIndices->find(v) == fLoopIndices->end(); |
| 508 | } |
Brian Osman | ea485e5 | 2021-01-15 13:20:32 -0500 | [diff] [blame] | 509 | |
| 510 | // ... expressions composed of both of the above |
| 511 | case Expression::Kind::kBinary: |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 512 | case Expression::Kind::kConstructorArray: |
John Stiles | e3ae968 | 2021-08-05 10:35:01 -0400 | [diff] [blame] | 513 | case Expression::Kind::kConstructorArrayCast: |
John Stiles | 8cad637 | 2021-04-07 12:31:13 -0400 | [diff] [blame] | 514 | case Expression::Kind::kConstructorCompound: |
| 515 | case Expression::Kind::kConstructorCompoundCast: |
John Stiles | e118278 | 2021-03-30 22:09:37 -0400 | [diff] [blame] | 516 | case Expression::Kind::kConstructorDiagonalMatrix: |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 517 | case Expression::Kind::kConstructorMatrixResize: |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 518 | case Expression::Kind::kConstructorScalarCast: |
John Stiles | 2938eea | 2021-04-01 18:58:25 -0400 | [diff] [blame] | 519 | case Expression::Kind::kConstructorSplat: |
John Stiles | d47330f | 2021-04-08 23:25:52 -0400 | [diff] [blame] | 520 | case Expression::Kind::kConstructorStruct: |
Brian Osman | ea485e5 | 2021-01-15 13:20:32 -0500 | [diff] [blame] | 521 | case Expression::Kind::kFieldAccess: |
| 522 | case Expression::Kind::kIndex: |
| 523 | case Expression::Kind::kPrefix: |
| 524 | case Expression::Kind::kPostfix: |
| 525 | case Expression::Kind::kSwizzle: |
| 526 | case Expression::Kind::kTernary: |
| 527 | return INHERITED::visitExpression(e); |
| 528 | |
John Stiles | 7bd3f1c | 2021-08-27 16:12:10 -0400 | [diff] [blame] | 529 | // Function calls are completely disallowed in SkSL constant-(index)-expressions. |
| 530 | // GLSL does mandate that calling a built-in function where the arguments are all |
| 531 | // constant-expressions should result in a constant-expression. SkSL handles this by |
| 532 | // optimizing fully-constant function calls into literals in FunctionCall::Make. |
Brian Osman | ea485e5 | 2021-01-15 13:20:32 -0500 | [diff] [blame] | 533 | case Expression::Kind::kFunctionCall: |
John Stiles | 7bd3f1c | 2021-08-27 16:12:10 -0400 | [diff] [blame] | 534 | case Expression::Kind::kExternalFunctionCall: |
| 535 | case Expression::Kind::kChildCall: |
Brian Osman | ea485e5 | 2021-01-15 13:20:32 -0500 | [diff] [blame] | 536 | |
John Stiles | 7bd3f1c | 2021-08-27 16:12:10 -0400 | [diff] [blame] | 537 | // These shouldn't appear in a valid program at all, and definitely aren't |
| 538 | // constant-index-expressions. |
Ethan Nicholas | 549c6b8 | 2021-06-25 12:31:44 -0400 | [diff] [blame] | 539 | case Expression::Kind::kPoison: |
John Stiles | 7bd3f1c | 2021-08-27 16:12:10 -0400 | [diff] [blame] | 540 | case Expression::Kind::kFunctionReference: |
| 541 | case Expression::Kind::kExternalFunctionReference: |
Brian Osman | 3099f79 | 2021-09-01 13:12:16 -0400 | [diff] [blame] | 542 | case Expression::Kind::kMethodReference: |
John Stiles | 7bd3f1c | 2021-08-27 16:12:10 -0400 | [diff] [blame] | 543 | case Expression::Kind::kTypeReference: |
| 544 | case Expression::Kind::kCodeString: |
Ethan Nicholas | 549c6b8 | 2021-06-25 12:31:44 -0400 | [diff] [blame] | 545 | return true; |
| 546 | |
Brian Osman | ea485e5 | 2021-01-15 13:20:32 -0500 | [diff] [blame] | 547 | default: |
| 548 | SkDEBUGFAIL("Unexpected expression type"); |
| 549 | return true; |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | private: |
| 554 | const std::set<const Variable*>* fLoopIndices; |
| 555 | using INHERITED = ProgramVisitor; |
| 556 | }; |
| 557 | |
| 558 | class ES2IndexingVisitor : public ProgramVisitor { |
| 559 | public: |
| 560 | ES2IndexingVisitor(ErrorReporter& errors) : fErrors(errors) {} |
| 561 | |
| 562 | bool visitStatement(const Statement& s) override { |
| 563 | if (s.is<ForStatement>()) { |
| 564 | const ForStatement& f = s.as<ForStatement>(); |
| 565 | SkASSERT(f.initializer() && f.initializer()->is<VarDeclaration>()); |
| 566 | const Variable* var = &f.initializer()->as<VarDeclaration>().var(); |
| 567 | auto [iter, inserted] = fLoopIndices.insert(var); |
| 568 | SkASSERT(inserted); |
| 569 | bool result = this->visitStatement(*f.statement()); |
| 570 | fLoopIndices.erase(iter); |
| 571 | return result; |
| 572 | } |
| 573 | return INHERITED::visitStatement(s); |
| 574 | } |
| 575 | |
| 576 | bool visitExpression(const Expression& e) override { |
| 577 | if (e.is<IndexExpression>()) { |
| 578 | const IndexExpression& i = e.as<IndexExpression>(); |
Brian Osman | 7b36149 | 2021-02-25 11:25:30 -0500 | [diff] [blame] | 579 | ConstantExpressionVisitor indexerInvalid(&fLoopIndices); |
Brian Osman | ea485e5 | 2021-01-15 13:20:32 -0500 | [diff] [blame] | 580 | if (indexerInvalid.visitExpression(*i.index())) { |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame] | 581 | fErrors.error(i.fLine, "index expression must be constant"); |
Brian Osman | ea485e5 | 2021-01-15 13:20:32 -0500 | [diff] [blame] | 582 | return true; |
| 583 | } |
| 584 | } |
| 585 | return INHERITED::visitExpression(e); |
| 586 | } |
| 587 | |
| 588 | using ProgramVisitor::visitProgramElement; |
| 589 | |
| 590 | private: |
| 591 | ErrorReporter& fErrors; |
| 592 | std::set<const Variable*> fLoopIndices; |
| 593 | using INHERITED = ProgramVisitor; |
| 594 | }; |
| 595 | |
| 596 | |
| 597 | void Analysis::ValidateIndexingForES2(const ProgramElement& pe, ErrorReporter& errors) { |
| 598 | ES2IndexingVisitor visitor(errors); |
| 599 | visitor.visitProgramElement(pe); |
| 600 | } |
| 601 | |
Brian Osman | 7b36149 | 2021-02-25 11:25:30 -0500 | [diff] [blame] | 602 | bool Analysis::IsConstantExpression(const Expression& expr) { |
| 603 | ConstantExpressionVisitor visitor(/*loopIndices=*/nullptr); |
| 604 | return !visitor.visitExpression(expr); |
| 605 | } |
| 606 | |
John Stiles | 2ecc595 | 2021-09-01 14:41:36 -0400 | [diff] [blame] | 607 | void Analysis::VerifyStaticTestsAndExpressions(const Program& program) { |
| 608 | class TestsAndExpressions : public ProgramVisitor { |
John Stiles | 0fc6bed | 2021-09-01 11:35:59 -0400 | [diff] [blame] | 609 | public: |
John Stiles | 2ecc595 | 2021-09-01 14:41:36 -0400 | [diff] [blame] | 610 | TestsAndExpressions(const Context& ctx) : fContext(ctx) {} |
John Stiles | 0fc6bed | 2021-09-01 11:35:59 -0400 | [diff] [blame] | 611 | |
| 612 | using ProgramVisitor::visitProgramElement; |
| 613 | |
| 614 | bool visitStatement(const Statement& stmt) override { |
John Stiles | 2ecc595 | 2021-09-01 14:41:36 -0400 | [diff] [blame] | 615 | if (!fContext.fConfig->fSettings.fPermitInvalidStaticTests) { |
| 616 | switch (stmt.kind()) { |
| 617 | case Statement::Kind::kIf: |
| 618 | if (stmt.as<IfStatement>().isStatic()) { |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame] | 619 | fContext.fErrors->error(stmt.fLine, "static if has non-static test"); |
John Stiles | 2ecc595 | 2021-09-01 14:41:36 -0400 | [diff] [blame] | 620 | } |
| 621 | break; |
John Stiles | 0fc6bed | 2021-09-01 11:35:59 -0400 | [diff] [blame] | 622 | |
John Stiles | 2ecc595 | 2021-09-01 14:41:36 -0400 | [diff] [blame] | 623 | case Statement::Kind::kSwitch: |
| 624 | if (stmt.as<SwitchStatement>().isStatic()) { |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame] | 625 | fContext.fErrors->error(stmt.fLine, |
John Stiles | 2ecc595 | 2021-09-01 14:41:36 -0400 | [diff] [blame] | 626 | "static switch has non-static test"); |
| 627 | } |
| 628 | break; |
John Stiles | 0fc6bed | 2021-09-01 11:35:59 -0400 | [diff] [blame] | 629 | |
John Stiles | 2ecc595 | 2021-09-01 14:41:36 -0400 | [diff] [blame] | 630 | default: |
| 631 | break; |
| 632 | } |
John Stiles | 0fc6bed | 2021-09-01 11:35:59 -0400 | [diff] [blame] | 633 | } |
| 634 | return INHERITED::visitStatement(stmt); |
| 635 | } |
| 636 | |
John Stiles | 2ecc595 | 2021-09-01 14:41:36 -0400 | [diff] [blame] | 637 | bool visitExpression(const Expression& expr) override { |
| 638 | switch (expr.kind()) { |
| 639 | case Expression::Kind::kFunctionCall: { |
| 640 | const FunctionDeclaration& decl = expr.as<FunctionCall>().function(); |
| 641 | if (!decl.isBuiltin() && !decl.definition()) { |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame] | 642 | fContext.fErrors->error(expr.fLine, "function '" + decl.description() + |
| 643 | "' is not defined"); |
John Stiles | 2ecc595 | 2021-09-01 14:41:36 -0400 | [diff] [blame] | 644 | } |
| 645 | break; |
| 646 | } |
| 647 | case Expression::Kind::kExternalFunctionReference: |
| 648 | case Expression::Kind::kFunctionReference: |
Brian Osman | 3099f79 | 2021-09-01 13:12:16 -0400 | [diff] [blame] | 649 | case Expression::Kind::kMethodReference: |
John Stiles | 2ecc595 | 2021-09-01 14:41:36 -0400 | [diff] [blame] | 650 | case Expression::Kind::kTypeReference: |
| 651 | SkDEBUGFAIL("invalid reference-expr, should have been reported by coerce()"); |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame] | 652 | fContext.fErrors->error(expr.fLine, "invalid expression"); |
John Stiles | 2ecc595 | 2021-09-01 14:41:36 -0400 | [diff] [blame] | 653 | break; |
| 654 | default: |
| 655 | if (expr.type() == *fContext.fTypes.fInvalid) { |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame] | 656 | fContext.fErrors->error(expr.fLine, "invalid expression"); |
John Stiles | 2ecc595 | 2021-09-01 14:41:36 -0400 | [diff] [blame] | 657 | } |
| 658 | break; |
| 659 | } |
| 660 | return INHERITED::visitExpression(expr); |
John Stiles | 0fc6bed | 2021-09-01 11:35:59 -0400 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | private: |
| 664 | using INHERITED = ProgramVisitor; |
John Stiles | 2ecc595 | 2021-09-01 14:41:36 -0400 | [diff] [blame] | 665 | const Context& fContext; |
John Stiles | 0fc6bed | 2021-09-01 11:35:59 -0400 | [diff] [blame] | 666 | }; |
| 667 | |
John Stiles | 0fc6bed | 2021-09-01 11:35:59 -0400 | [diff] [blame] | 668 | // Check all of the program's owned elements. (Built-in elements are assumed to be valid.) |
John Stiles | 2ecc595 | 2021-09-01 14:41:36 -0400 | [diff] [blame] | 669 | TestsAndExpressions visitor{*program.fContext}; |
John Stiles | 0fc6bed | 2021-09-01 11:35:59 -0400 | [diff] [blame] | 670 | for (const std::unique_ptr<ProgramElement>& element : program.ownedElements()) { |
John Stiles | 2ecc595 | 2021-09-01 14:41:36 -0400 | [diff] [blame] | 671 | visitor.visitProgramElement(*element); |
John Stiles | 0fc6bed | 2021-09-01 11:35:59 -0400 | [diff] [blame] | 672 | } |
| 673 | } |
| 674 | |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 675 | //////////////////////////////////////////////////////////////////////////////// |
| 676 | // ProgramVisitor |
| 677 | |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 678 | bool ProgramVisitor::visit(const Program& program) { |
| 679 | for (const ProgramElement* pe : program.elements()) { |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 680 | if (this->visitProgramElement(*pe)) { |
John Stiles | 933abe3 | 2020-08-28 11:58:40 -0400 | [diff] [blame] | 681 | return true; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 682 | } |
| 683 | } |
John Stiles | 933abe3 | 2020-08-28 11:58:40 -0400 | [diff] [blame] | 684 | return false; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 685 | } |
| 686 | |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 687 | template <typename T> bool TProgramVisitor<T>::visitExpression(typename T::Expression& e) { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 688 | switch (e.kind()) { |
John Stiles | 6d02346 | 2021-10-06 12:51:10 -0400 | [diff] [blame] | 689 | case Expression::Kind::kCodeString: |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 690 | case Expression::Kind::kExternalFunctionReference: |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 691 | case Expression::Kind::kFunctionReference: |
John Stiles | 7591d4b | 2021-09-13 13:32:06 -0400 | [diff] [blame] | 692 | case Expression::Kind::kLiteral: |
Brian Osman | 3099f79 | 2021-09-01 13:12:16 -0400 | [diff] [blame] | 693 | case Expression::Kind::kMethodReference: |
Ethan Nicholas | 549c6b8 | 2021-06-25 12:31:44 -0400 | [diff] [blame] | 694 | case Expression::Kind::kPoison: |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 695 | case Expression::Kind::kSetting: |
| 696 | case Expression::Kind::kTypeReference: |
| 697 | case Expression::Kind::kVariableReference: |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 698 | // Leaf expressions return false |
| 699 | return false; |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 700 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 701 | case Expression::Kind::kBinary: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 702 | auto& b = e.template as<BinaryExpression>(); |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 703 | return (b.left() && this->visitExpressionPtr(b.left())) || |
| 704 | (b.right() && this->visitExpressionPtr(b.right())); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 705 | } |
Brian Osman | eb0f29d | 2021-08-04 11:34:16 -0400 | [diff] [blame] | 706 | case Expression::Kind::kChildCall: { |
| 707 | // We don't visit the child variable itself, just the arguments |
| 708 | auto& c = e.template as<ChildCall>(); |
| 709 | for (auto& arg : c.arguments()) { |
| 710 | if (arg && this->visitExpressionPtr(arg)) { return true; } |
| 711 | } |
| 712 | return false; |
| 713 | } |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 714 | case Expression::Kind::kConstructorArray: |
John Stiles | e3ae968 | 2021-08-05 10:35:01 -0400 | [diff] [blame] | 715 | case Expression::Kind::kConstructorArrayCast: |
John Stiles | 8cad637 | 2021-04-07 12:31:13 -0400 | [diff] [blame] | 716 | case Expression::Kind::kConstructorCompound: |
| 717 | case Expression::Kind::kConstructorCompoundCast: |
John Stiles | 2938eea | 2021-04-01 18:58:25 -0400 | [diff] [blame] | 718 | case Expression::Kind::kConstructorDiagonalMatrix: |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 719 | case Expression::Kind::kConstructorMatrixResize: |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 720 | case Expression::Kind::kConstructorScalarCast: |
John Stiles | d47330f | 2021-04-08 23:25:52 -0400 | [diff] [blame] | 721 | case Expression::Kind::kConstructorSplat: |
| 722 | case Expression::Kind::kConstructorStruct: { |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 723 | auto& c = e.asAnyConstructor(); |
| 724 | for (auto& arg : c.argumentSpan()) { |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 725 | if (this->visitExpressionPtr(arg)) { return true; } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 726 | } |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 727 | return false; |
| 728 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 729 | case Expression::Kind::kExternalFunctionCall: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 730 | auto& c = e.template as<ExternalFunctionCall>(); |
Ethan Nicholas | 6e86ec9 | 2020-09-30 14:29:56 -0400 | [diff] [blame] | 731 | for (auto& arg : c.arguments()) { |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 732 | if (this->visitExpressionPtr(arg)) { return true; } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 733 | } |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 734 | return false; |
| 735 | } |
John Stiles | d7ab450 | 2020-09-24 22:41:00 -0400 | [diff] [blame] | 736 | case Expression::Kind::kFieldAccess: |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 737 | return this->visitExpressionPtr(e.template as<FieldAccess>().base()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 738 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 739 | case Expression::Kind::kFunctionCall: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 740 | auto& c = e.template as<FunctionCall>(); |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 741 | for (auto& arg : c.arguments()) { |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 742 | if (arg && this->visitExpressionPtr(arg)) { return true; } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 743 | } |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 744 | return false; |
| 745 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 746 | case Expression::Kind::kIndex: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 747 | auto& i = e.template as<IndexExpression>(); |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 748 | return this->visitExpressionPtr(i.base()) || this->visitExpressionPtr(i.index()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 749 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 750 | case Expression::Kind::kPostfix: |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 751 | return this->visitExpressionPtr(e.template as<PostfixExpression>().operand()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 752 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 753 | case Expression::Kind::kPrefix: |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 754 | return this->visitExpressionPtr(e.template as<PrefixExpression>().operand()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 755 | |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 756 | case Expression::Kind::kSwizzle: { |
| 757 | auto& s = e.template as<Swizzle>(); |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 758 | return s.base() && this->visitExpressionPtr(s.base()); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 759 | } |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 760 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 761 | case Expression::Kind::kTernary: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 762 | auto& t = e.template as<TernaryExpression>(); |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 763 | return this->visitExpressionPtr(t.test()) || |
| 764 | (t.ifTrue() && this->visitExpressionPtr(t.ifTrue())) || |
| 765 | (t.ifFalse() && this->visitExpressionPtr(t.ifFalse())); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 766 | } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 767 | default: |
| 768 | SkUNREACHABLE; |
| 769 | } |
| 770 | } |
| 771 | |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 772 | template <typename T> bool TProgramVisitor<T>::visitStatement(typename T::Statement& s) { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 773 | switch (s.kind()) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 774 | case Statement::Kind::kBreak: |
| 775 | case Statement::Kind::kContinue: |
| 776 | case Statement::Kind::kDiscard: |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 777 | case Statement::Kind::kInlineMarker: |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 778 | case Statement::Kind::kNop: |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 779 | // Leaf statements just return false |
| 780 | return false; |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 781 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 782 | case Statement::Kind::kBlock: |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 783 | for (auto& stmt : s.template as<Block>().children()) { |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 784 | if (stmt && this->visitStatementPtr(stmt)) { |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 785 | return true; |
| 786 | } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 787 | } |
| 788 | return false; |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 789 | |
John Stiles | a0e56e3 | 2021-03-03 13:14:37 -0500 | [diff] [blame] | 790 | case Statement::Kind::kSwitchCase: { |
| 791 | auto& sc = s.template as<SwitchCase>(); |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 792 | if (sc.value() && this->visitExpressionPtr(sc.value())) { |
John Stiles | a0e56e3 | 2021-03-03 13:14:37 -0500 | [diff] [blame] | 793 | return true; |
| 794 | } |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 795 | return this->visitStatementPtr(sc.statement()); |
John Stiles | a0e56e3 | 2021-03-03 13:14:37 -0500 | [diff] [blame] | 796 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 797 | case Statement::Kind::kDo: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 798 | auto& d = s.template as<DoStatement>(); |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 799 | return this->visitExpressionPtr(d.test()) || this->visitStatementPtr(d.statement()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 800 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 801 | case Statement::Kind::kExpression: |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 802 | return this->visitExpressionPtr(s.template as<ExpressionStatement>().expression()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 803 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 804 | case Statement::Kind::kFor: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 805 | auto& f = s.template as<ForStatement>(); |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 806 | return (f.initializer() && this->visitStatementPtr(f.initializer())) || |
| 807 | (f.test() && this->visitExpressionPtr(f.test())) || |
| 808 | (f.next() && this->visitExpressionPtr(f.next())) || |
| 809 | this->visitStatementPtr(f.statement()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 810 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 811 | case Statement::Kind::kIf: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 812 | auto& i = s.template as<IfStatement>(); |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 813 | return (i.test() && this->visitExpressionPtr(i.test())) || |
| 814 | (i.ifTrue() && this->visitStatementPtr(i.ifTrue())) || |
| 815 | (i.ifFalse() && this->visitStatementPtr(i.ifFalse())); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 816 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 817 | case Statement::Kind::kReturn: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 818 | auto& r = s.template as<ReturnStatement>(); |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 819 | return r.expression() && this->visitExpressionPtr(r.expression()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 820 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 821 | case Statement::Kind::kSwitch: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 822 | auto& sw = s.template as<SwitchStatement>(); |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 823 | if (this->visitExpressionPtr(sw.value())) { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 824 | return true; |
| 825 | } |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 826 | for (auto& c : sw.cases()) { |
| 827 | if (this->visitStatementPtr(c)) { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 828 | return true; |
| 829 | } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 830 | } |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 831 | return false; |
| 832 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 833 | case Statement::Kind::kVarDeclaration: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 834 | auto& v = s.template as<VarDeclaration>(); |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 835 | return v.value() && this->visitExpressionPtr(v.value()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 836 | } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 837 | default: |
| 838 | SkUNREACHABLE; |
| 839 | } |
| 840 | } |
| 841 | |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 842 | template <typename T> bool TProgramVisitor<T>::visitProgramElement(typename T::ProgramElement& pe) { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 843 | switch (pe.kind()) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 844 | case ProgramElement::Kind::kExtension: |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 845 | case ProgramElement::Kind::kFunctionPrototype: |
John Stiles | d39aec0 | 2020-12-03 10:42:26 -0500 | [diff] [blame] | 846 | case ProgramElement::Kind::kInterfaceBlock: |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 847 | case ProgramElement::Kind::kModifiers: |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 848 | case ProgramElement::Kind::kStructDefinition: |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 849 | // Leaf program elements just return false by default |
| 850 | return false; |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 851 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 852 | case ProgramElement::Kind::kFunction: |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 853 | return this->visitStatementPtr(pe.template as<FunctionDefinition>().body()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 854 | |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 855 | case ProgramElement::Kind::kGlobalVar: |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 856 | return this->visitStatementPtr(pe.template as<GlobalVarDeclaration>().declaration()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 857 | |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 858 | default: |
| 859 | SkUNREACHABLE; |
| 860 | } |
| 861 | } |
| 862 | |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 863 | template class TProgramVisitor<ProgramVisitorTypes>; |
| 864 | template class TProgramVisitor<ProgramWriterTypes>; |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 865 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 866 | } // namespace SkSL |