Flutter Engine
The Flutter Engine
SkSLBlock.h
Go to the documentation of this file.
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
16
17#include <memory>
18#include <string>
19#include <utility>
20
21namespace SkSL {
22
23/**
24 * A block of multiple statements functioning as a single statement.
25 */
26class Block final : public Statement {
27public:
28 inline static constexpr Kind kIRNodeKind = Kind::kBlock;
29
30 // "kBracedScope" represents an actual language-level block. Other kinds of block are used to
31 // pass around multiple statements as if they were a single unit, with no semantic impact.
32 enum class Kind {
33 kUnbracedBlock, // Represents a group of statements without curly braces.
34 kBracedScope, // Represents a language-level Block, with curly braces.
35 kCompoundStatement, // A block which conceptually represents a single statement, such as
36 // `int a, b;`. (SkSL represents this internally as two statements:
37 // `int a; int b;`) Allowed to optimize away to its interior Statement.
38 // Treated as a single statement by the debugger.
39 };
40
42 StatementArray statements,
43 Kind kind = Kind::kBracedScope,
44 std::unique_ptr<SymbolTable> symbols = nullptr)
46 , fSymbolTable(std::move(symbols))
47 , fChildren(std::move(statements))
48 , fBlockKind(kind) {}
49
50 // Make is allowed to simplify compound statements. For a single-statement unscoped Block,
51 // Make can return the Statement as-is. For an empty unscoped Block, Make can return Nop.
52 static std::unique_ptr<Statement> Make(Position pos,
53 StatementArray statements,
54 Kind kind = Kind::kBracedScope,
55 std::unique_ptr<SymbolTable> symbols = nullptr);
56
57 // MakeCompoundStatement wraps two Statements into a single compound-statement Block.
58 // If either statement is empty, no Block will be created; the non-empty Statement is returned.
59 // If the first Statement is _already_ a compound-statement Block, the second statement will be
60 // appended to that block.
61 static std::unique_ptr<Statement> MakeCompoundStatement(std::unique_ptr<Statement> existing,
62 std::unique_ptr<Statement> additional);
63
64 // MakeBlock always makes a real Block object. This is important because many callers rely on
65 // Blocks specifically; e.g. a function body must be a scoped Block, nothing else will do.
66 static std::unique_ptr<Block> MakeBlock(Position pos,
67 StatementArray statements,
68 Kind kind = Kind::kBracedScope,
69 std::unique_ptr<SymbolTable> symbols = nullptr);
70
71 const StatementArray& children() const {
72 return fChildren;
73 }
74
76 return fChildren;
77 }
78
79 bool isScope() const {
80 return fBlockKind == Kind::kBracedScope;
81 }
82
83 Kind blockKind() const {
84 return fBlockKind;
85 }
86
88 fBlockKind = kind;
89 }
90
92 return fSymbolTable.get();
93 }
94
95 bool isEmpty() const override {
96 for (const std::unique_ptr<Statement>& stmt : this->children()) {
97 if (!stmt->isEmpty()) {
98 return false;
99 }
100 }
101 return true;
102 }
103
104 std::string description() const override;
105
106private:
107 std::unique_ptr<SymbolTable> fSymbolTable;
108 StatementArray fChildren;
109 Kind fBlockKind;
110
111 using INHERITED = Statement;
112};
113
114} // namespace SkSL
115
116#endif
SkPoint pos
bool isEmpty() const override
Definition: SkSLBlock.h:95
static std::unique_ptr< Statement > MakeCompoundStatement(std::unique_ptr< Statement > existing, std::unique_ptr< Statement > additional)
Definition: SkSLBlock.cpp:68
const StatementArray & children() const
Definition: SkSLBlock.h:71
bool isScope() const
Definition: SkSLBlock.h:79
static constexpr Kind kIRNodeKind
Definition: SkSLBlock.h:28
Block(Position pos, StatementArray statements, Kind kind=Kind::kBracedScope, std::unique_ptr< SymbolTable > symbols=nullptr)
Definition: SkSLBlock.h:41
std::string description() const override
Definition: SkSLBlock.cpp:97
static std::unique_ptr< Statement > Make(Position pos, StatementArray statements, Kind kind=Kind::kBracedScope, std::unique_ptr< SymbolTable > symbols=nullptr)
Definition: SkSLBlock.cpp:14
StatementArray & children()
Definition: SkSLBlock.h:75
void setBlockKind(Kind kind)
Definition: SkSLBlock.h:87
static std::unique_ptr< Block > MakeBlock(Position pos, StatementArray statements, Kind kind=Kind::kBracedScope, std::unique_ptr< SymbolTable > symbols=nullptr)
Definition: SkSLBlock.cpp:59
Kind blockKind() const
Definition: SkSLBlock.h:83
SymbolTable * symbolTable() const
Definition: SkSLBlock.h:91
Kind kind() const
Definition: SkSLStatement.h:28
Statement(Position pos, Kind kind)
Definition: SkSLStatement.h:23
Definition: ref_ptr.h:256