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_BLOCK 9 #define SKSL_BLOCK 10 11 #include "include/private/SkSLStatement.h" 12 #include "src/sksl/ir/SkSLSymbolTable.h" 13 14 namespace SkSL { 15 16 /** 17 * A block of multiple statements functioning as a single statement. 18 */ 19 class Block final : public Statement { 20 public: 21 static constexpr Kind kStatementKind = Kind::kBlock; 22 23 Block(int offset, StatementArray statements, 24 const std::shared_ptr<SymbolTable> symbols = nullptr, bool isScope = true) INHERITED(offset,kStatementKind)25 : INHERITED(offset, kStatementKind) 26 , fChildren(std::move(statements)) 27 , fSymbolTable(std::move(symbols)) 28 , fIsScope(isScope) {} 29 30 // Make always makes a real Block object. This is important because many callers rely on Blocks 31 // specifically; e.g. a function body must be a scoped Block, nothing else will do. 32 static std::unique_ptr<Block> Make(int offset, 33 StatementArray statements, 34 std::shared_ptr<SymbolTable> symbols = nullptr, 35 bool isScope = true); 36 37 // An unscoped Block is just a collection of Statements. For a single-statement Block, 38 // MakeUnscoped will return the Statement as-is. For an empty Block, MakeUnscoped returns Nop. 39 static std::unique_ptr<Statement> MakeUnscoped(int offset, StatementArray statements); 40 children()41 const StatementArray& children() const { 42 return fChildren; 43 } 44 children()45 StatementArray& children() { 46 return fChildren; 47 } 48 isScope()49 bool isScope() const { 50 return fIsScope; 51 } 52 setIsScope(bool isScope)53 void setIsScope(bool isScope) { 54 fIsScope = isScope; 55 } 56 symbolTable()57 std::shared_ptr<SymbolTable> symbolTable() const { 58 return fSymbolTable; 59 } 60 isEmpty()61 bool isEmpty() const override { 62 for (const std::unique_ptr<Statement>& stmt : this->children()) { 63 if (!stmt->isEmpty()) { 64 return false; 65 } 66 } 67 return true; 68 } 69 70 std::unique_ptr<Statement> clone() const override; 71 72 String description() const override; 73 74 private: 75 StatementArray fChildren; 76 std::shared_ptr<SymbolTable> fSymbolTable; 77 // If isScope is false, this is just a group of statements rather than an actual language-level 78 // block. This allows us to pass around multiple statements as if they were a single unit, with 79 // no semantic impact. 80 bool fIsScope; 81 82 using INHERITED = Statement; 83 }; 84 85 } // namespace SkSL 86 87 #endif 88