blob: 8bd93ad978c2290bac297d72ec7bfc1ed7928126 [file] [log] [blame]
Ethan Nicholas1ff76092021-01-28 10:02:43 -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/DSLFunction.h"
Ethan Nicholas1ff76092021-01-28 10:02:43 -05009
Ethan Nicholasdaed2592021-03-04 14:30:25 -050010#include "include/sksl/DSLVar.h"
Ethan Nicholas1ff76092021-01-28 10:02:43 -050011#include "src/sksl/SkSLAnalysis.h"
12#include "src/sksl/SkSLCompiler.h"
13#include "src/sksl/SkSLIRGenerator.h"
Ethan Nicholasdaed2592021-03-04 14:30:25 -050014#include "src/sksl/dsl/priv/DSLWriter.h"
Ethan Nicholas57709e12021-08-10 15:02:51 -040015#include "src/sksl/ir/SkSLFunctionPrototype.h"
Ethan Nicholas1ff76092021-01-28 10:02:43 -050016#include "src/sksl/ir/SkSLReturnStatement.h"
17
18namespace SkSL {
19
20namespace dsl {
21
Ethan Nicholas292a09d2021-07-14 09:52:16 -040022void DSLFunction::init(DSLModifiers modifiers, const DSLType& returnType, skstd::string_view name,
Ethan Nicholasa2d22b22021-07-15 10:35:54 -040023 SkTArray<DSLParameter*> params) {
Ethan Nicholas292a09d2021-07-14 09:52:16 -040024 // Conservatively assume all user-defined functions have side effects.
25 if (!DSLWriter::IsModule()) {
26 modifiers.fModifiers.fFlags |= Modifiers::kHasSideEffects_Flag;
27 }
28
Ethan Nicholas459777a2021-07-16 11:16:27 -040029 if (DSLWriter::Settings().fForceNoInline) {
Ethan Nicholas292a09d2021-07-14 09:52:16 -040030 // Apply the `noinline` modifier to every function. This allows us to test Runtime
31 // Effects without any inlining, even when the code is later added to a paint.
32 modifiers.fModifiers.fFlags &= ~Modifiers::kInline_Flag;
33 modifiers.fModifiers.fFlags |= Modifiers::kNoInline_Flag;
34 }
35
Ethan Nicholas371f6e12021-05-04 14:30:02 -040036 std::vector<std::unique_ptr<Variable>> paramVars;
Ethan Nicholasdaed2592021-03-04 14:30:25 -050037 paramVars.reserve(params.size());
Ethan Nicholasa2d22b22021-07-15 10:35:54 -040038 for (DSLParameter* param : params) {
Ethan Nicholas961d9442021-03-16 16:37:29 -040039 if (param->fDeclared) {
Ethan Nicholasa2d22b22021-07-15 10:35:54 -040040 DSLWriter::ReportError("error: parameter has already been used in another function\n");
Ethan Nicholas961d9442021-03-16 16:37:29 -040041 }
Ethan Nicholasa2d22b22021-07-15 10:35:54 -040042 SkASSERT(!param->fInitialValue.valid());
43 SkASSERT(!param->fDeclaration);
Ethan Nicholas961d9442021-03-16 16:37:29 -040044 param->fDeclared = true;
Ethan Nicholasa2d22b22021-07-15 10:35:54 -040045 std::unique_ptr<SkSL::Variable> paramVar = DSLWriter::CreateParameterVar(*param);
Ethan Nicholas6b3c6fc2021-05-04 15:36:06 -040046 if (!paramVar) {
47 return;
48 }
Ethan Nicholas371f6e12021-05-04 14:30:02 -040049 paramVars.push_back(std::move(paramVar));
Ethan Nicholasdaed2592021-03-04 14:30:25 -050050 }
Ethan Nicholas6b3c6fc2021-05-04 15:36:06 -040051 SkASSERT(paramVars.size() == params.size());
Ethan Nicholas371f6e12021-05-04 14:30:02 -040052 fDecl = SkSL::FunctionDeclaration::Convert(DSLWriter::Context(),
53 *DSLWriter::SymbolTable(),
John Stiles0b822792021-05-04 17:41:53 -040054 /*offset=*/-1,
Ethan Nicholas292a09d2021-07-14 09:52:16 -040055 DSLWriter::Modifiers(modifiers.fModifiers),
Ethan Nicholase0531f52021-07-26 16:07:23 -040056 name == "main" ? name : DSLWriter::Name(name),
Ethan Nicholas722cb672021-05-06 10:47:06 -040057 std::move(paramVars), &returnType.skslType(),
Ethan Nicholas292a09d2021-07-14 09:52:16 -040058 DSLWriter::IsModule());
59 DSLWriter::ReportErrors();
60 if (fDecl) {
61 for (size_t i = 0; i < params.size(); ++i) {
62 params[i]->fVar = fDecl->parameters()[i];
63 }
Ethan Nicholas57709e12021-08-10 15:02:51 -040064 // We don't know when this function is going to be defined; go ahead and add a prototype in
65 // case the definition is delayed. If we end up defining the function immediately, we'll
66 // remove the prototype in define().
67 DSLWriter::ProgramElements().push_back(std::make_unique<SkSL::FunctionPrototype>(
68 /*offset=*/-1, fDecl, DSLWriter::IsModule()));
Ethan Nicholas292a09d2021-07-14 09:52:16 -040069 }
Ethan Nicholasdaed2592021-03-04 14:30:25 -050070}
71
Ethan Nicholas1ff76092021-01-28 10:02:43 -050072void DSLFunction::define(DSLBlock block) {
Ethan Nicholas371f6e12021-05-04 14:30:02 -040073 if (!fDecl) {
Ethan Nicholas292a09d2021-07-14 09:52:16 -040074 // Evidently we failed to create the declaration; error should already have been reported.
75 // Release the block so we don't fail its destructor assert.
76 block.release();
Ethan Nicholas371f6e12021-05-04 14:30:02 -040077 return;
78 }
Ethan Nicholas57709e12021-08-10 15:02:51 -040079 if (!DSLWriter::ProgramElements().empty()) {
80 // If the last ProgramElement was the prototype for this function, it was unnecessary and we
81 // can remove it.
82 const SkSL::ProgramElement& last = *DSLWriter::ProgramElements().back();
83 if (last.is<SkSL::FunctionPrototype>()) {
84 const SkSL::FunctionPrototype& prototype = last.as<SkSL::FunctionPrototype>();
85 if (&prototype.declaration() == fDecl) {
86 DSLWriter::ProgramElements().pop_back();
87 }
88 }
89 }
Ethan Nicholas80f62352021-04-09 12:25:03 -040090 SkASSERTF(!fDecl->definition(), "function '%s' already defined", fDecl->description().c_str());
Ethan Nicholasebc9fad2021-07-09 15:35:23 -040091 std::unique_ptr<Block> body = block.release();
92 body = DSLWriter::IRGenerator().finalizeFunction(*fDecl, std::move(body));
Ethan Nicholas80f62352021-04-09 12:25:03 -040093 auto function = std::make_unique<SkSL::FunctionDefinition>(/*offset=*/-1, fDecl,
John Stiles958f4b52021-03-17 16:39:49 -040094 /*builtin=*/false, std::move(body));
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -040095 DSLWriter::ReportErrors();
Ethan Nicholas624a5292021-04-16 14:54:43 -040096 fDecl->fDefinition = function.get();
Ethan Nicholase2c05042021-02-03 10:27:22 -050097 DSLWriter::ProgramElements().push_back(std::move(function));
Ethan Nicholas1ff76092021-01-28 10:02:43 -050098}
99
Ethan Nicholas722cb672021-05-06 10:47:06 -0400100DSLExpression DSLFunction::call(SkTArray<DSLWrapper<DSLExpression>> args) {
Ethan Nicholas80f62352021-04-09 12:25:03 -0400101 ExpressionArray released;
102 released.reserve_back(args.size());
Ethan Nicholas722cb672021-05-06 10:47:06 -0400103 for (DSLWrapper<DSLExpression>& arg : args) {
104 released.push_back(arg->release());
Ethan Nicholas80f62352021-04-09 12:25:03 -0400105 }
Ethan Nicholas292a09d2021-07-14 09:52:16 -0400106 std::unique_ptr<SkSL::Expression> result = DSLWriter::Call(*fDecl, std::move(released));
107 return result ? DSLExpression(std::move(result)) : DSLExpression();
Ethan Nicholasdaed2592021-03-04 14:30:25 -0500108}
109
Ethan Nicholas1ff76092021-01-28 10:02:43 -0500110} // namespace dsl
111
112} // namespace SkSL