• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Tint Authors.
2 //
3 // Licensed under the Apache License, Version 2.0(the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef SRC_SEM_BLOCK_STATEMENT_H_
16 #define SRC_SEM_BLOCK_STATEMENT_H_
17 
18 #include <cstddef>
19 #include <vector>
20 
21 #include "src/sem/statement.h"
22 
23 // Forward declarations
24 namespace tint {
25 namespace ast {
26 class BlockStatement;
27 class ContinueStatement;
28 class Function;
29 class Variable;
30 }  // namespace ast
31 }  // namespace tint
32 
33 namespace tint {
34 namespace sem {
35 
36 /// Holds semantic information about a block, such as parent block and variables
37 /// declared in the block.
38 class BlockStatement : public Castable<BlockStatement, CompoundStatement> {
39  public:
40   /// Constructor
41   /// @param declaration the AST node for this block statement
42   /// @param parent the owning statement
43   /// @param function the owning function
44   BlockStatement(const ast::BlockStatement* declaration,
45                  const CompoundStatement* parent,
46                  const sem::Function* function);
47 
48   /// Destructor
49   ~BlockStatement() override;
50 
51   /// @returns the AST block statement associated with this semantic block
52   /// statement
53   const ast::BlockStatement* Declaration() const;
54 
55   /// @returns the declarations associated with this block
Decls()56   const std::vector<const ast::Variable*>& Decls() const { return decls_; }
57 
58   /// Associates a declaration with this block.
59   /// @param var a variable declaration to be added to the block
60   void AddDecl(const ast::Variable* var);
61 
62  private:
63   std::vector<const ast::Variable*> decls_;
64 };
65 
66 /// The root block statement for a function
67 class FunctionBlockStatement
68     : public Castable<FunctionBlockStatement, BlockStatement> {
69  public:
70   /// Constructor
71   /// @param function the owning function
72   explicit FunctionBlockStatement(const sem::Function* function);
73 
74   /// Destructor
75   ~FunctionBlockStatement() override;
76 };
77 
78 /// Holds semantic information about a loop body block or for-loop body block
79 class LoopBlockStatement : public Castable<LoopBlockStatement, BlockStatement> {
80  public:
81   /// Constructor
82   /// @param declaration the AST node for this block statement
83   /// @param parent the owning statement
84   /// @param function the owning function
85   LoopBlockStatement(const ast::BlockStatement* declaration,
86                      const CompoundStatement* parent,
87                      const sem::Function* function);
88 
89   /// Destructor
90   ~LoopBlockStatement() override;
91 
92   /// @returns the first continue statement in this loop block, or nullptr if
93   /// there are no continue statements in the block
FirstContinue()94   const ast::ContinueStatement* FirstContinue() const {
95     return first_continue_;
96   }
97 
98   /// @returns the number of variables declared before the first continue
99   /// statement
NumDeclsAtFirstContinue()100   size_t NumDeclsAtFirstContinue() const {
101     return num_decls_at_first_continue_;
102   }
103 
104   /// Allows the resolver to record the first continue statement in the block
105   /// and the number of variables declared prior to that statement.
106   /// @param first_continue the first continue statement in the block
107   /// @param num_decls the number of variable declarations before that continue
108   void SetFirstContinue(const ast::ContinueStatement* first_continue,
109                         size_t num_decls);
110 
111  private:
112   /// The first continue statement in this loop block.
113   const ast::ContinueStatement* first_continue_ = nullptr;
114 
115   /// The number of variables declared before the first continue statement.
116   size_t num_decls_at_first_continue_ = 0;
117 };
118 
119 }  // namespace sem
120 }  // namespace tint
121 
122 #endif  // SRC_SEM_BLOCK_STATEMENT_H_
123