blob: 080491fa5f8eb8117ea5dfc46f74e7d1976da5b8 [file] [log] [blame]
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001/*
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
Ethan Nicholasdaed2592021-03-04 14:30:25 -05008#include "include/sksl/DSLStatement.h"
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05009
Ethan Nicholasdaed2592021-03-04 14:30:25 -050010#include "include/sksl/DSLBlock.h"
11#include "include/sksl/DSLExpression.h"
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -050012#include "src/sksl/SkSLCompiler.h"
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -050013#include "src/sksl/dsl/priv/DSLWriter.h"
Ethan Nicholasdb2326b2021-04-19 10:55:18 -040014#include "src/sksl/ir/SkSLBlock.h"
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -050015#include "src/sksl/ir/SkSLExpressionStatement.h"
Ethan Nicholas549c6b82021-06-25 12:31:44 -040016#include "src/sksl/ir/SkSLNop.h"
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -050017
Ethan Nicholas3af656b2021-02-17 11:17:56 -050018#if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU
19#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
20#endif
21
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -050022namespace SkSL {
23
24namespace dsl {
25
Ethan Nicholasdaed2592021-03-04 14:30:25 -050026DSLStatement::DSLStatement() {}
27
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -050028DSLStatement::DSLStatement(DSLBlock block)
29 : fStatement(block.release()) {}
30
31DSLStatement::DSLStatement(DSLExpression expr) {
32 std::unique_ptr<SkSL::Expression> skslExpr = expr.release();
33 if (skslExpr) {
Ethan Nicholasdd2fdea2021-07-20 15:23:04 -040034 fStatement = SkSL::ExpressionStatement::Make(DSLWriter::Context(), std::move(skslExpr));
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -050035 }
36}
37
38DSLStatement::DSLStatement(std::unique_ptr<SkSL::Expression> expr)
Ethan Nicholasdd2fdea2021-07-20 15:23:04 -040039 : fStatement(SkSL::ExpressionStatement::Make(DSLWriter::Context(), std::move(expr))) {
Ethan Nicholas51b4b862021-08-31 16:12:40 -040040 SkASSERT(this->hasValue());
Ethan Nicholas549c6b82021-06-25 12:31:44 -040041}
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -050042
43DSLStatement::DSLStatement(std::unique_ptr<SkSL::Statement> stmt)
44 : fStatement(std::move(stmt)) {
Ethan Nicholas51b4b862021-08-31 16:12:40 -040045 SkASSERT(this->hasValue());
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -050046}
47
Ethan Nicholasb9563042021-02-25 09:45:49 -050048DSLStatement::DSLStatement(DSLPossibleExpression expr, PositionInfo pos)
49 : DSLStatement(DSLExpression(std::move(expr), pos)) {}
50
51DSLStatement::DSLStatement(DSLPossibleStatement stmt, PositionInfo pos) {
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -040052 DSLWriter::ReportErrors(pos);
Ethan Nicholas51b4b862021-08-31 16:12:40 -040053 if (stmt.hasValue()) {
Ethan Nicholas549c6b82021-06-25 12:31:44 -040054 fStatement = std::move(stmt.fStatement);
55 } else {
56 fStatement = SkSL::Nop::Make();
57 }
Brian Osmancc914522021-09-24 18:58:37 +000058 if (pos.offset() != -1) {
59 fStatement->fOffset = pos.offset();
Ethan Nicholas81b70602021-09-15 13:28:29 -040060 }
Ethan Nicholasb9563042021-02-25 09:45:49 -050061}
62
Ethan Nicholas3af656b2021-02-17 11:17:56 -050063DSLStatement::~DSLStatement() {
64#if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU
65 if (fStatement && DSLWriter::InFragmentProcessor()) {
66 DSLWriter::CurrentEmitArgs()->fFragBuilder->codeAppend(this->release());
67 return;
68 }
69#endif
Ethan Nicholas459777a2021-07-16 11:16:27 -040070 SkASSERTF(!fStatement || !DSLWriter::Settings().fAssertDSLObjectsReleased,
71 "Statement destroyed without being incorporated into program (see "
72 "ProgramSettings::fAssertDSLObjectsReleased)");
Ethan Nicholas3af656b2021-02-17 11:17:56 -050073}
74
Ethan Nicholasb9563042021-02-25 09:45:49 -050075DSLPossibleStatement::DSLPossibleStatement(std::unique_ptr<SkSL::Statement> statement)
76 : fStatement(std::move(statement)) {}
77
78DSLPossibleStatement::~DSLPossibleStatement() {
79 if (fStatement) {
80 // this handles incorporating the expression into the output tree
81 DSLStatement(std::move(fStatement));
82 }
83}
84
Ethan Nicholasdb2326b2021-04-19 10:55:18 -040085DSLStatement operator,(DSLStatement left, DSLStatement right) {
Ethan Nicholas5fad2b82021-09-27 10:39:18 -040086 int line = left.fStatement->fOffset;
Ethan Nicholasdb2326b2021-04-19 10:55:18 -040087 StatementArray stmts;
88 stmts.reserve_back(2);
89 stmts.push_back(left.release());
90 stmts.push_back(right.release());
Ethan Nicholas5fad2b82021-09-27 10:39:18 -040091 return DSLStatement(SkSL::Block::MakeUnscoped(line, std::move(stmts)));
Ethan Nicholasdb2326b2021-04-19 10:55:18 -040092}
93
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -050094} // namespace dsl
95
96} // namespace SkSL