• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 Google Inc.
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 #ifndef SKSL_FUNCTIONDEFINITION
9 #define SKSL_FUNCTIONDEFINITION
10 
11 #include "include/private/SkSLProgramElement.h"
12 #include "src/sksl/ir/SkSLBlock.h"
13 #include "src/sksl/ir/SkSLFunctionDeclaration.h"
14 
15 namespace SkSL {
16 
17 struct ASTNode;
18 
19 /**
20  * A function definition (a declaration plus an associated block of code).
21  */
22 class FunctionDefinition final : public ProgramElement {
23 public:
24     static constexpr Kind kProgramElementKind = Kind::kFunction;
25 
26     FunctionDefinition(int offset,
27                        const FunctionDeclaration* declaration, bool builtin,
28                        std::unique_ptr<Statement> body,
29                        std::unordered_set<const FunctionDeclaration*> referencedIntrinsics = {})
INHERITED(offset,kProgramElementKind)30         : INHERITED(offset, kProgramElementKind)
31         , fDeclaration(declaration)
32         , fBuiltin(builtin)
33         , fBody(std::move(body))
34         , fReferencedIntrinsics(std::move(referencedIntrinsics))
35         , fSource(nullptr) {}
36 
declaration()37     const FunctionDeclaration& declaration() const {
38         return *fDeclaration;
39     }
40 
isBuiltin()41     bool isBuiltin() const {
42         return fBuiltin;
43     }
44 
body()45     std::unique_ptr<Statement>& body() {
46         return fBody;
47     }
48 
body()49     const std::unique_ptr<Statement>& body() const {
50         return fBody;
51     }
52 
referencedIntrinsics()53     const std::unordered_set<const FunctionDeclaration*>& referencedIntrinsics() const {
54         return fReferencedIntrinsics;
55     }
56 
source()57     const ASTNode* source() const {
58         return fSource;
59     }
60 
setSource(const ASTNode * source)61     void setSource(const ASTNode* source) {
62         fSource = source;
63     }
64 
clone()65     std::unique_ptr<ProgramElement> clone() const override {
66         return std::make_unique<FunctionDefinition>(fOffset, &this->declaration(),
67                                                     /*builtin=*/false, this->body()->clone(),
68                                                     this->referencedIntrinsics());
69     }
70 
description()71     String description() const override {
72         return this->declaration().description() + " " + this->body()->description();
73     }
74 
75 private:
76     const FunctionDeclaration* fDeclaration;
77     bool fBuiltin;
78     std::unique_ptr<Statement> fBody;
79     // We track intrinsic functions we reference so that we can ensure that all of them end up
80     // copied into the final output.
81     std::unordered_set<const FunctionDeclaration*> fReferencedIntrinsics;
82     // This pointer may be null, and even when non-null is not guaranteed to remain valid for
83     // the entire lifespan of this object. The parse tree's lifespan is normally controlled by
84     // IRGenerator, so the IRGenerator being destroyed or being used to compile another file
85     // will invalidate this pointer.
86     const ASTNode* fSource;
87 
88     using INHERITED = ProgramElement;
89 };
90 
91 }  // namespace SkSL
92 
93 #endif
94