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