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_WRAP_ARRAYS_IN_STRUCTS_H_ 16 #define SRC_TRANSFORM_WRAP_ARRAYS_IN_STRUCTS_H_ 17 18 #include <string> 19 #include <unordered_map> 20 21 #include "src/transform/transform.h" 22 23 // Forward declarations 24 namespace tint { 25 namespace ast { 26 class Type; 27 } // namespace ast 28 } // namespace tint 29 30 namespace tint { 31 namespace transform { 32 33 /// WrapArraysInStructs is a transform that replaces all array types with a 34 /// structure holding a single field of that array type. 35 /// Array index expressions and constructors are also adjusted to deal with this 36 /// wrapping. 37 /// This transform helps with backends that cannot directly return arrays or use 38 /// them as parameters. 39 class WrapArraysInStructs : public Castable<WrapArraysInStructs, Transform> { 40 public: 41 /// Constructor 42 WrapArraysInStructs(); 43 44 /// Destructor 45 ~WrapArraysInStructs() override; 46 47 protected: 48 /// Runs the transform using the CloneContext built for transforming a 49 /// program. Run() is responsible for calling Clone() on the CloneContext. 50 /// @param ctx the CloneContext primed with the input program and 51 /// ProgramBuilder 52 /// @param inputs optional extra transform-specific input data 53 /// @param outputs optional extra transform-specific output data 54 void Run(CloneContext& ctx, const DataMap& inputs, DataMap& outputs) override; 55 56 private: 57 struct WrappedArrayInfo { 58 WrappedArrayInfo(); 59 WrappedArrayInfo(const WrappedArrayInfo&); 60 ~WrappedArrayInfo(); 61 62 Symbol wrapper_name; 63 std::function<const ast::Type*(CloneContext&)> array_type; 64 65 operator bool() { return wrapper_name.IsValid(); } 66 }; 67 68 /// WrapArray wraps the fixed-size array type in a new structure (if it hasn't 69 /// already been wrapped). WrapArray will recursively wrap arrays-of-arrays. 70 /// The new structure will be added to module-scope type declarations of 71 /// `ctx.dst`. 72 /// @param ctx the CloneContext 73 /// @param wrapped_arrays a map of src array type to the wrapped structure 74 /// name 75 /// @param array the array type 76 /// @return the name of the structure that wraps the array, or an invalid 77 /// Symbol if this array should not be wrapped 78 WrappedArrayInfo WrapArray( 79 CloneContext& ctx, 80 std::unordered_map<const sem::Array*, WrappedArrayInfo>& wrapped_arrays, 81 const sem::Array* array) const; 82 }; 83 84 } // namespace transform 85 } // namespace tint 86 87 #endif // SRC_TRANSFORM_WRAP_ARRAYS_IN_STRUCTS_H_ 88