• 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_INFO_H_
16 #define SRC_SEM_INFO_H_
17 
18 #include <type_traits>
19 #include <unordered_map>
20 
21 #include "src/debug.h"
22 #include "src/sem/node.h"
23 #include "src/sem/type_mappings.h"
24 
25 namespace tint {
26 namespace sem {
27 
28 /// Info holds all the resolved semantic information for a Program.
29 class Info {
30  public:
31   /// Placeholder type used by Get() to provide a default value for EXPLICIT_SEM
32   using InferFromAST = std::nullptr_t;
33 
34   /// Resolves to the return type of the Get() method given the desired sementic
35   /// type and AST type.
36   template <typename SEM, typename AST_OR_TYPE>
37   using GetResultType =
38       std::conditional_t<std::is_same<SEM, InferFromAST>::value,
39                          SemanticNodeTypeFor<AST_OR_TYPE>,
40                          SEM>;
41 
42   /// Constructor
43   Info();
44 
45   /// Move constructor
46   Info(Info&&);
47 
48   /// Destructor
49   ~Info();
50 
51   /// Move assignment operator
52   /// @param rhs the Program to move
53   /// @return this Program
54   Info& operator=(Info&& rhs);
55 
56   /// Get looks up the semantic information for the AST or type node `node`.
57   /// @param node the AST or type node
58   /// @returns a pointer to the semantic node if found, otherwise nullptr
59   template <typename SEM = InferFromAST,
60             typename AST_OR_TYPE = CastableBase,
61             typename RESULT = GetResultType<SEM, AST_OR_TYPE>>
Get(const AST_OR_TYPE * node)62   const RESULT* Get(const AST_OR_TYPE* node) const {
63     auto it = map.find(node);
64     if (it == map.end()) {
65       return nullptr;
66     }
67     return As<RESULT>(it->second);
68   }
69 
70   /// Add registers the semantic node `sem_node` for the AST or type node
71   /// `node`.
72   /// @param node the AST or type node
73   /// @param sem_node the semantic node
74   template <typename AST_OR_TYPE>
Add(const AST_OR_TYPE * node,const SemanticNodeTypeFor<AST_OR_TYPE> * sem_node)75   void Add(const AST_OR_TYPE* node,
76            const SemanticNodeTypeFor<AST_OR_TYPE>* sem_node) {
77     // Check there's no semantic info already existing for the node
78     TINT_ASSERT(Semantic, Get(node) == nullptr);
79     map.emplace(node, sem_node);
80   }
81 
82   /// Wrap returns a new Info created with the contents of `inner`.
83   /// The Info returned by Wrap is intended to temporarily extend the contents
84   /// of an existing immutable Info.
85   /// As the copied contents are owned by `inner`, `inner` must not be
86   /// destructed or assigned while using the returned Info.
87   /// @param inner the immutable Info to extend
88   /// @return the Info that wraps `inner`
Wrap(const Info & inner)89   static Info Wrap(const Info& inner) {
90     Info out;
91     out.map = inner.map;
92     return out;
93   }
94 
95  private:
96   // TODO(crbug.com/tint/724): Once finished, this map should be:
97   // std::unordered_map<const ast::Node*, const sem::Node*>
98   std::unordered_map<const CastableBase*, const CastableBase*> map;
99 };
100 
101 }  // namespace sem
102 }  // namespace tint
103 
104 #endif  // SRC_SEM_INFO_H_
105