1 /*
2 * Copyright 2022 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/SkSLPool.h"
10 #include "src/sksl/SkSLProgramSettings.h"
11 #include "src/sksl/analysis/SkSLProgramUsage.h"
12 #include "src/sksl/ir/SkSLFunctionDeclaration.h"
13 #include "src/sksl/ir/SkSLProgram.h"
14 #include "src/sksl/ir/SkSLProgramElement.h"
15 #include "src/sksl/ir/SkSLSymbol.h"
16 #include "src/sksl/ir/SkSLSymbolTable.h" // IWYU pragma: keep
17
18 #include <utility>
19
20 namespace SkSL {
21
Program(std::unique_ptr<std::string> source,std::unique_ptr<ProgramConfig> config,std::shared_ptr<Context> context,std::vector<std::unique_ptr<ProgramElement>> elements,std::unique_ptr<SymbolTable> symbols,std::unique_ptr<Pool> pool)22 Program::Program(std::unique_ptr<std::string> source,
23 std::unique_ptr<ProgramConfig> config,
24 std::shared_ptr<Context> context,
25 std::vector<std::unique_ptr<ProgramElement>> elements,
26 std::unique_ptr<SymbolTable> symbols,
27 std::unique_ptr<Pool> pool)
28 : fSource(std::move(source))
29 , fConfig(std::move(config))
30 , fContext(context)
31 , fSymbols(std::move(symbols))
32 , fPool(std::move(pool))
33 , fOwnedElements(std::move(elements)) {
34 fUsage = Analysis::GetUsage(*this);
35 }
36
~Program()37 Program::~Program() {
38 // Some or all of the program elements are in the pool. To free them safely, we must attach
39 // the pool before destroying any program elements. (Otherwise, we may accidentally call
40 // delete on a pooled node.)
41 AutoAttachPoolToThread attach(fPool.get());
42
43 fOwnedElements.clear();
44 fContext.reset();
45 fSymbols.reset();
46 }
47
description() const48 std::string Program::description() const {
49 std::string result = fConfig->versionDescription();
50 for (const ProgramElement* e : this->elements()) {
51 result += e->description();
52 }
53 return result;
54 }
55
getFunction(const char * functionName) const56 const FunctionDeclaration* Program::getFunction(const char* functionName) const {
57 const Symbol* symbol = fSymbols->find(functionName);
58 bool valid = symbol && symbol->is<FunctionDeclaration>() &&
59 symbol->as<FunctionDeclaration>().definition();
60 return valid ? &symbol->as<FunctionDeclaration>() : nullptr;
61 }
62
63 } // namespace SkSL
64