1 // Copyright 2020 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_BOUND_ARRAY_ACCESSORS_TRANSFORM_H_ 16 #define SRC_TRANSFORM_BOUND_ARRAY_ACCESSORS_TRANSFORM_H_ 17 18 #include <string> 19 20 #include "src/ast/array_accessor_expression.h" 21 #include "src/ast/expression.h" 22 #include "src/ast/module.h" 23 #include "src/ast/statement.h" 24 #include "src/context.h" 25 #include "src/scope_stack.h" 26 #include "src/transform/transformer.h" 27 28 namespace tint { 29 namespace transform { 30 31 /// This transformer is responsible for clamping all array accesses to be within 32 /// the bounds of the array. Any access before the start of the array will clamp 33 /// to zero and any access past the end of the array will clamp to 34 /// (array length - 1). 35 class BoundArrayAccessorsTransform : public Transformer { 36 public: 37 /// Constructor 38 /// @param ctx the Tint context object 39 /// @param mod the module transform 40 explicit BoundArrayAccessorsTransform(Context* ctx, ast::Module* mod); 41 ~BoundArrayAccessorsTransform() override; 42 43 /// @returns true if the transformation was successful 44 bool Run() override; 45 46 private: 47 bool ProcessStatement(ast::Statement* stmt); 48 bool ProcessExpression(ast::Expression* expr); 49 bool ProcessArrayAccessor(ast::ArrayAccessorExpression* expr); 50 bool ProcessAccessExpression(ast::ArrayAccessorExpression* expr, 51 uint32_t size); 52 53 ScopeStack<ast::Variable*> scope_stack_; 54 }; 55 56 } // namespace transform 57 } // namespace tint 58 59 #endif // SRC_TRANSFORM_BOUND_ARRAY_ACCESSORS_TRANSFORM_H_ 60