| 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 "src/sksl/ir/SkSLBlock.h" |
| 12 | #include "src/sksl/ir/SkSLFunctionDeclaration.h" |
| 13 | #include "src/sksl/ir/SkSLProgramElement.h" |
| 14 | |
| 15 | #include <unordered_set> |
| 16 | |
| 17 | namespace SkSL { |
| 18 | |
| 19 | struct ASTNode; |
| 20 | |
| 21 | /** |
| 22 | * A function definition (a declaration plus an associated block of code). |
| 23 | */ |
| 24 | struct FunctionDefinition : public ProgramElement { |
| 25 | FunctionDefinition(int offset, |
| 26 | const FunctionDeclaration& declaration, |
| 27 | std::unique_ptr<Statement> body, |
| 28 | std::unordered_set<const FunctionDeclaration*> referencedIntrinsics = {}) |
| 29 | : INHERITED(offset, kFunction_Kind) |
| 30 | , fDeclaration(declaration) |
| 31 | , fBody(std::move(body)) |
| 32 | , fReferencedIntrinsics(std::move(referencedIntrinsics)) {} |
| 33 | |
| 34 | int inlinedFunctionSize() const { |
| 35 | return fBody->nodeCount(); |
| 36 | } |
| 37 | |
| 38 | std::unique_ptr<ProgramElement> clone() const override { |
| 39 | return std::make_unique<FunctionDefinition>(fOffset, fDeclaration, |
| 40 | fBody->clone(), fReferencedIntrinsics); |
| 41 | } |
| 42 | |
| 43 | String description() const override { |
| 44 | return fDeclaration.description() + " " + fBody->description(); |
| 45 | } |
| 46 | |
| 47 | const FunctionDeclaration& fDeclaration; |
| 48 | std::unique_ptr<Statement> fBody; |
| 49 | // We track intrinsic functions we reference so that we can ensure that all of them end up |
| 50 | // copied into the final output. |
| 51 | std::unordered_set<const FunctionDeclaration*> fReferencedIntrinsics; |
| 52 | // This pointer may be null, and even when non-null is not guaranteed to remain valid for the |
| 53 | // entire lifespan of this object. The parse tree's lifespan is normally controlled by |
| 54 | // IRGenerator, so the IRGenerator being destroyed or being used to compile another file will |
| 55 | // invalidate this pointer. |
| 56 | const ASTNode* fSource = nullptr; |
| 57 | |
| 58 | typedef ProgramElement INHERITED; |
| 59 | }; |
| 60 | |
| 61 | } // namespace SkSL |
| 62 | |
| 63 | #endif |
| 64 | |