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/ir/SkSLDoStatement.h"
9
10 #include "include/core/SkTypes.h"
11 #include "src/sksl/SkSLAnalysis.h"
12 #include "src/sksl/SkSLBuiltinTypes.h"
13 #include "src/sksl/SkSLContext.h"
14 #include "src/sksl/SkSLErrorReporter.h"
15 #include "src/sksl/SkSLProgramSettings.h"
16 #include "src/sksl/ir/SkSLType.h"
17
18 namespace SkSL {
19
Convert(const Context & context,Position pos,std::unique_ptr<Statement> stmt,std::unique_ptr<Expression> test)20 std::unique_ptr<Statement> DoStatement::Convert(const Context& context,
21 Position pos,
22 std::unique_ptr<Statement> stmt,
23 std::unique_ptr<Expression> test) {
24 if (context.fConfig->strictES2Mode()) {
25 context.fErrors->error(pos, "do-while loops are not supported");
26 return nullptr;
27 }
28 test = context.fTypes.fBool->coerceExpression(std::move(test), context);
29 if (!test) {
30 return nullptr;
31 }
32 if (Analysis::DetectVarDeclarationWithoutScope(*stmt, context.fErrors)) {
33 return nullptr;
34 }
35 return DoStatement::Make(context, pos, std::move(stmt), std::move(test));
36 }
37
Make(const Context & context,Position pos,std::unique_ptr<Statement> stmt,std::unique_ptr<Expression> test)38 std::unique_ptr<Statement> DoStatement::Make(const Context& context,
39 Position pos,
40 std::unique_ptr<Statement> stmt,
41 std::unique_ptr<Expression> test) {
42 SkASSERT(!context.fConfig->strictES2Mode());
43 SkASSERT(test->type().matches(*context.fTypes.fBool));
44 SkASSERT(!Analysis::DetectVarDeclarationWithoutScope(*stmt));
45 return std::make_unique<DoStatement>(pos, std::move(stmt), std::move(test));
46 }
47
description() const48 std::string DoStatement::description() const {
49 return "do " + this->statement()->description() +
50 " while (" + this->test()->description() + ");";
51 }
52
53 } // namespace SkSL
54
55