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_AST_DECORATION_H_ 16 #define SRC_AST_DECORATION_H_ 17 18 #include <string> 19 #include <vector> 20 21 #include "src/ast/node.h" 22 23 namespace tint { 24 namespace ast { 25 26 /// The base class for all decorations 27 class Decoration : public Castable<Decoration, Node> { 28 public: 29 ~Decoration() override; 30 31 /// @returns the WGSL name for the decoration 32 virtual std::string Name() const = 0; 33 34 protected: 35 /// Constructor 36 /// @param pid the identifier of the program that owns this node 37 /// @param src the source of this node Decoration(ProgramID pid,const Source & src)38 Decoration(ProgramID pid, const Source& src) : Base(pid, src) {} 39 }; 40 41 /// A list of decorations 42 using DecorationList = std::vector<const Decoration*>; 43 44 /// @param decorations the list of decorations to search 45 /// @returns true if `decorations` includes a decoration of type `T` 46 template <typename T> HasDecoration(const DecorationList & decorations)47bool HasDecoration(const DecorationList& decorations) { 48 for (auto* deco : decorations) { 49 if (deco->Is<T>()) { 50 return true; 51 } 52 } 53 return false; 54 } 55 56 /// @param decorations the list of decorations to search 57 /// @returns a pointer to `T` from `decorations` if found, otherwise nullptr. 58 template <typename T> GetDecoration(const DecorationList & decorations)59const T* GetDecoration(const DecorationList& decorations) { 60 for (auto* deco : decorations) { 61 if (deco->Is<T>()) { 62 return deco->As<T>(); 63 } 64 } 65 return nullptr; 66 } 67 68 } // namespace ast 69 } // namespace tint 70 71 #endif // SRC_AST_DECORATION_H_ 72