• 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_TRANSFORM_CALCULATE_ARRAY_LENGTH_H_
16 #define SRC_TRANSFORM_CALCULATE_ARRAY_LENGTH_H_
17 
18 #include <string>
19 
20 #include "src/ast/internal_decoration.h"
21 #include "src/transform/transform.h"
22 
23 namespace tint {
24 
25 // Forward declarations
26 class CloneContext;
27 
28 namespace transform {
29 
30 /// CalculateArrayLength is a transform used to replace calls to arrayLength()
31 /// with a value calculated from the size of the storage buffer.
32 class CalculateArrayLength : public Castable<CalculateArrayLength, Transform> {
33  public:
34   /// BufferSizeIntrinsic is an InternalDecoration that's applied to intrinsic
35   /// functions used to obtain the runtime size of a storage buffer.
36   class BufferSizeIntrinsic
37       : public Castable<BufferSizeIntrinsic, ast::InternalDecoration> {
38    public:
39     /// Constructor
40     /// @param program_id the identifier of the program that owns this node
41     explicit BufferSizeIntrinsic(ProgramID program_id);
42     /// Destructor
43     ~BufferSizeIntrinsic() override;
44 
45     /// @return "buffer_size"
46     std::string InternalName() const override;
47 
48     /// Performs a deep clone of this object using the CloneContext `ctx`.
49     /// @param ctx the clone context
50     /// @return the newly cloned object
51     const BufferSizeIntrinsic* Clone(CloneContext* ctx) const override;
52   };
53 
54   /// Constructor
55   CalculateArrayLength();
56   /// Destructor
57   ~CalculateArrayLength() override;
58 
59  protected:
60   /// Runs the transform using the CloneContext built for transforming a
61   /// program. Run() is responsible for calling Clone() on the CloneContext.
62   /// @param ctx the CloneContext primed with the input program and
63   /// ProgramBuilder
64   /// @param inputs optional extra transform-specific input data
65   /// @param outputs optional extra transform-specific output data
66   void Run(CloneContext& ctx, const DataMap& inputs, DataMap& outputs) override;
67 };
68 
69 }  // namespace transform
70 }  // namespace tint
71 
72 #endif  // SRC_TRANSFORM_CALCULATE_ARRAY_LENGTH_H_
73