John Stiles | 8d13084 | 2021-08-27 12:42:45 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 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 | #include "src/sksl/SkSLContext.h" |
| 10 | #include "src/sksl/SkSLProgramSettings.h" |
| 11 | #include "src/sksl/ir/SkSLFunctionCall.h" |
| 12 | #include "src/sksl/ir/SkSLFunctionDefinition.h" |
| 13 | #include "src/sksl/ir/SkSLReturnStatement.h" |
| 14 | |
John Stiles | 1c5eb4b | 2021-09-21 14:36:34 -0400 | [diff] [blame] | 15 | #include <forward_list> |
| 16 | |
John Stiles | 8d13084 | 2021-08-27 12:42:45 -0400 | [diff] [blame] | 17 | namespace SkSL { |
| 18 | |
John Stiles | 3b20489 | 2021-08-27 17:35:35 -0400 | [diff] [blame] | 19 | std::unique_ptr<FunctionDefinition> FunctionDefinition::Convert(const Context& context, |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame^] | 20 | int line, |
John Stiles | 3b20489 | 2021-08-27 17:35:35 -0400 | [diff] [blame] | 21 | const FunctionDeclaration& function, |
| 22 | std::unique_ptr<Statement> body, |
| 23 | bool builtin) { |
John Stiles | 8d13084 | 2021-08-27 12:42:45 -0400 | [diff] [blame] | 24 | class Finalizer : public ProgramWriter { |
| 25 | public: |
| 26 | Finalizer(const Context& context, const FunctionDeclaration& function, |
| 27 | IntrinsicSet* referencedIntrinsics) |
| 28 | : fContext(context) |
| 29 | , fFunction(function) |
| 30 | , fReferencedIntrinsics(referencedIntrinsics) {} |
| 31 | |
| 32 | ~Finalizer() override { |
John Stiles | 1c5eb4b | 2021-09-21 14:36:34 -0400 | [diff] [blame] | 33 | SkASSERT(fBreakableLevel == 0); |
| 34 | SkASSERT(fContinuableLevel == std::forward_list<int>{0}); |
John Stiles | 8d13084 | 2021-08-27 12:42:45 -0400 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | bool functionReturnsValue() const { |
| 38 | return !fFunction.returnType().isVoid(); |
| 39 | } |
| 40 | |
| 41 | bool visitExpression(Expression& expr) override { |
| 42 | if (expr.is<FunctionCall>()) { |
| 43 | const FunctionDeclaration& func = expr.as<FunctionCall>().function(); |
| 44 | if (func.isBuiltin() && func.definition()) { |
| 45 | fReferencedIntrinsics->insert(&func); |
| 46 | } |
| 47 | } |
| 48 | return INHERITED::visitExpression(expr); |
| 49 | } |
| 50 | |
| 51 | bool visitStatement(Statement& stmt) override { |
| 52 | switch (stmt.kind()) { |
| 53 | case Statement::Kind::kReturn: { |
| 54 | // Early returns from a vertex main() function will bypass sk_Position |
| 55 | // normalization, so SkASSERT that we aren't doing that. If this becomes an |
| 56 | // issue, we can add normalization before each return statement. |
| 57 | if (fContext.fConfig->fKind == ProgramKind::kVertex && fFunction.isMain()) { |
| 58 | fContext.fErrors->error( |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame^] | 59 | stmt.fLine, |
John Stiles | 8d13084 | 2021-08-27 12:42:45 -0400 | [diff] [blame] | 60 | "early returns from vertex programs are not supported"); |
| 61 | } |
| 62 | |
| 63 | // Verify that the return statement matches the function's return type. |
| 64 | ReturnStatement& returnStmt = stmt.as<ReturnStatement>(); |
| 65 | if (returnStmt.expression()) { |
| 66 | if (this->functionReturnsValue()) { |
| 67 | // Coerce return expression to the function's return type. |
| 68 | returnStmt.setExpression(fFunction.returnType().coerceExpression( |
| 69 | std::move(returnStmt.expression()), fContext)); |
| 70 | } else { |
| 71 | // Returning something from a function with a void return type. |
| 72 | returnStmt.setExpression(nullptr); |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame^] | 73 | fContext.fErrors->error(returnStmt.fLine, |
John Stiles | 8d13084 | 2021-08-27 12:42:45 -0400 | [diff] [blame] | 74 | "may not return a value from a void function"); |
| 75 | } |
| 76 | } else { |
| 77 | if (this->functionReturnsValue()) { |
| 78 | // Returning nothing from a function with a non-void return type. |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame^] | 79 | fContext.fErrors->error(returnStmt.fLine, |
John Stiles | 8d13084 | 2021-08-27 12:42:45 -0400 | [diff] [blame] | 80 | "expected function to return '" + |
| 81 | fFunction.returnType().displayName() + "'"); |
| 82 | } |
| 83 | } |
| 84 | break; |
| 85 | } |
| 86 | case Statement::Kind::kDo: |
| 87 | case Statement::Kind::kFor: { |
| 88 | ++fBreakableLevel; |
John Stiles | 1c5eb4b | 2021-09-21 14:36:34 -0400 | [diff] [blame] | 89 | ++fContinuableLevel.front(); |
John Stiles | 8d13084 | 2021-08-27 12:42:45 -0400 | [diff] [blame] | 90 | bool result = INHERITED::visitStatement(stmt); |
John Stiles | 1c5eb4b | 2021-09-21 14:36:34 -0400 | [diff] [blame] | 91 | --fContinuableLevel.front(); |
John Stiles | 8d13084 | 2021-08-27 12:42:45 -0400 | [diff] [blame] | 92 | --fBreakableLevel; |
| 93 | return result; |
| 94 | } |
| 95 | case Statement::Kind::kSwitch: { |
| 96 | ++fBreakableLevel; |
John Stiles | 1c5eb4b | 2021-09-21 14:36:34 -0400 | [diff] [blame] | 97 | fContinuableLevel.push_front(0); |
John Stiles | 8d13084 | 2021-08-27 12:42:45 -0400 | [diff] [blame] | 98 | bool result = INHERITED::visitStatement(stmt); |
John Stiles | 1c5eb4b | 2021-09-21 14:36:34 -0400 | [diff] [blame] | 99 | fContinuableLevel.pop_front(); |
John Stiles | 8d13084 | 2021-08-27 12:42:45 -0400 | [diff] [blame] | 100 | --fBreakableLevel; |
| 101 | return result; |
| 102 | } |
| 103 | case Statement::Kind::kBreak: |
John Stiles | 1c5eb4b | 2021-09-21 14:36:34 -0400 | [diff] [blame] | 104 | if (fBreakableLevel == 0) { |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame^] | 105 | fContext.fErrors->error(stmt.fLine, |
John Stiles | 8d13084 | 2021-08-27 12:42:45 -0400 | [diff] [blame] | 106 | "break statement must be inside a loop or switch"); |
| 107 | } |
| 108 | break; |
| 109 | case Statement::Kind::kContinue: |
John Stiles | 1c5eb4b | 2021-09-21 14:36:34 -0400 | [diff] [blame] | 110 | if (fContinuableLevel.front() == 0) { |
| 111 | if (std::any_of(fContinuableLevel.begin(), |
| 112 | fContinuableLevel.end(), |
| 113 | [](int level) { return level > 0; })) { |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame^] | 114 | fContext.fErrors->error(stmt.fLine, |
John Stiles | 1c5eb4b | 2021-09-21 14:36:34 -0400 | [diff] [blame] | 115 | "continue statement cannot be used in a switch"); |
| 116 | } else { |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame^] | 117 | fContext.fErrors->error(stmt.fLine, |
John Stiles | 1c5eb4b | 2021-09-21 14:36:34 -0400 | [diff] [blame] | 118 | "continue statement must be inside a loop"); |
| 119 | } |
John Stiles | 8d13084 | 2021-08-27 12:42:45 -0400 | [diff] [blame] | 120 | } |
| 121 | break; |
| 122 | default: |
| 123 | break; |
| 124 | } |
| 125 | return INHERITED::visitStatement(stmt); |
| 126 | } |
| 127 | |
| 128 | private: |
| 129 | const Context& fContext; |
| 130 | const FunctionDeclaration& fFunction; |
| 131 | // which intrinsics have we encountered in this function |
| 132 | IntrinsicSet* fReferencedIntrinsics; |
| 133 | // how deeply nested we are in breakable constructs (for, do, switch). |
| 134 | int fBreakableLevel = 0; |
| 135 | // how deeply nested we are in continuable constructs (for, do). |
John Stiles | 1c5eb4b | 2021-09-21 14:36:34 -0400 | [diff] [blame] | 136 | // We keep a stack (via a forward_list) in order to disallow continue inside of switch. |
| 137 | std::forward_list<int> fContinuableLevel{0}; |
John Stiles | 8d13084 | 2021-08-27 12:42:45 -0400 | [diff] [blame] | 138 | |
| 139 | using INHERITED = ProgramWriter; |
| 140 | }; |
| 141 | |
John Stiles | 3b20489 | 2021-08-27 17:35:35 -0400 | [diff] [blame] | 142 | IntrinsicSet referencedIntrinsics; |
| 143 | Finalizer(context, function, &referencedIntrinsics).visitStatement(*body); |
John Stiles | 8d13084 | 2021-08-27 12:42:45 -0400 | [diff] [blame] | 144 | |
| 145 | if (Analysis::CanExitWithoutReturningValue(function, *body)) { |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame^] | 146 | context.fErrors->error(function.fLine, "function '" + function.name() + |
John Stiles | 8d13084 | 2021-08-27 12:42:45 -0400 | [diff] [blame] | 147 | "' can exit without returning a value"); |
| 148 | } |
John Stiles | 3b20489 | 2021-08-27 17:35:35 -0400 | [diff] [blame] | 149 | |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame^] | 150 | return std::make_unique<FunctionDefinition>(line, &function, builtin, std::move(body), |
John Stiles | 3b20489 | 2021-08-27 17:35:35 -0400 | [diff] [blame] | 151 | std::move(referencedIntrinsics)); |
John Stiles | 8d13084 | 2021-08-27 12:42:45 -0400 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | } // namespace SkSL |