1 | /** |
2 | * Copyright (c) Glow Contributors. See CONTRIBUTORS file. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | #ifndef GLOW_TOOLS_CLASSGEN_MEMBERTYPE_H |
17 | #define GLOW_TOOLS_CLASSGEN_MEMBERTYPE_H |
18 | |
19 | #include <cassert> |
20 | #include <string> |
21 | #include <unordered_map> |
22 | |
23 | /// Define a MemberTypeInfo for the type T. The definition of type T does not |
24 | /// need to be known when compiling the NodeGen.cpp, but it should be known when |
25 | /// compiling the generated files. T can be a simple type like CustomType or |
26 | /// have a more complex form like e.g. CustomType * or CustomType &. |
27 | #define MEMBER_TYPE_INFO(T) \ |
28 | MemberTypeInfo { MemberType::UserDefinedType, #T, #T, #T, "" } |
29 | |
30 | /// Define a MemberTypeInfo for the type T that uses a custom comparator instead |
31 | /// of ==. The definition of type T does not need to be known when compiling the |
32 | /// NodeGen.cpp, but it should be known when compiling the generated files. T |
33 | /// can be a simple type like CustomType or have a more complex form like e.g. |
34 | /// CustomType * or CustomType &. |
35 | #define MEMBER_TYPE_INFO_WITH_CMP(T, CMP_FN) \ |
36 | MemberTypeInfo { MemberType::UserDefinedType, #T, #T, #T, "", CMP_FN } |
37 | |
38 | enum class MemberType : unsigned { |
39 | TypeRef, |
40 | Float, |
41 | Unsigned, |
42 | Boolean, |
43 | Int64, |
44 | String, |
45 | VectorFloat, |
46 | VectorSigned, |
47 | VectorUnsigned, |
48 | VectorInt64, |
49 | VectorSizeT, |
50 | VectorDimT, |
51 | VectorNodeValue, |
52 | Enum, |
53 | UserDefinedType, |
54 | }; |
55 | |
56 | /// This struct encapsulates all of the information NodeBuilder needs about the |
57 | /// type of a member in order to generate a cloner, hasher, equator, etc. |
58 | struct MemberTypeInfo { |
59 | /// Kind of the member type. |
60 | MemberType type; |
61 | /// Type to be used when this member type is returned. |
62 | std::string returnTypename; |
63 | /// Type to be used for storing members of this type. |
64 | std::string storageTypename; |
65 | /// Type to be used when providing this member type as a constructor arguments |
66 | /// of a node. |
67 | std::string ctorArgTypename; |
68 | /// Forward declaration of this member type. |
69 | std::string forwardDecl; |
70 | /// Comparator function to be used for comparing members of this type. |
71 | std::string cmpFn = "==" ; |
72 | /// Whether to include a setter for this member type. |
73 | bool addSetter{false}; |
74 | }; |
75 | |
76 | /// These are instances of MemberTypeInfo for commonly used types. |
77 | extern MemberTypeInfo kTypeRefTypeInfo; |
78 | extern MemberTypeInfo kFloatTypeInfo; |
79 | extern MemberTypeInfo kUnsignedTypeInfo; |
80 | extern MemberTypeInfo kBooleanTypeInfo; |
81 | extern MemberTypeInfo kInt64TypeInfo; |
82 | extern MemberTypeInfo kStringTypeInfo; |
83 | extern MemberTypeInfo kVectorFloatTypeInfo; |
84 | extern MemberTypeInfo kVectorUnsignedTypeInfo; |
85 | extern MemberTypeInfo kVectorInt64TypeInfo; |
86 | extern MemberTypeInfo kVectorSignedTypeInfo; |
87 | extern MemberTypeInfo kVectorSizeTTypeInfo; |
88 | extern MemberTypeInfo kVectorDimTTypeInfo; |
89 | extern MemberTypeInfo kVectorNodeValueTypeInfo; |
90 | extern MemberTypeInfo kEnumTypeInfo; |
91 | |
92 | inline const char *getReturnTypename(const MemberTypeInfo *typeInfo) { |
93 | return typeInfo->returnTypename.c_str(); |
94 | } |
95 | |
96 | inline const char *getStorageTypename(const MemberTypeInfo *typeInfo) { |
97 | return typeInfo->storageTypename.c_str(); |
98 | } |
99 | |
100 | inline const char *getCtorArgTypename(const MemberTypeInfo *typeInfo) { |
101 | return typeInfo->ctorArgTypename.c_str(); |
102 | } |
103 | |
104 | /// TODO: Remove after modifying InstrGen to use MemberTypeInfo as well? |
105 | inline const char *getReturnTypename(MemberType type) { |
106 | const char *returnTypes[] = {"TypeRef" , |
107 | "float" , |
108 | "unsigned_t" , |
109 | "bool" , |
110 | "int64_t" , |
111 | "llvm::StringRef" , |
112 | "llvm::ArrayRef<float>" , |
113 | "llvm::ArrayRef<int>" , |
114 | "llvm::ArrayRef<unsigned_t>" , |
115 | "llvm::ArrayRef<int64_t>" , |
116 | "llvm::ArrayRef<size_t>" , |
117 | "llvm::ArrayRef<dim_t>" , |
118 | "NodeValueArrayRef" , |
119 | nullptr, |
120 | nullptr}; |
121 | return returnTypes[(int)type]; |
122 | } |
123 | |
124 | inline const char *getStorageTypename(MemberType type) { |
125 | const char *storageTypes[] = {"TypeRef" , |
126 | "float" , |
127 | "unsigned_t" , |
128 | "bool" , |
129 | "int64_t" , |
130 | "std::string" , |
131 | "std::vector<float>" , |
132 | "std::vector<int>" , |
133 | "std::vector<unsigned_t>" , |
134 | "std::vector<int64_t>" , |
135 | "std::vector<size_t>" , |
136 | "std::vector<dim_t>" , |
137 | "std::vector<NodeHandle>" , |
138 | nullptr, |
139 | nullptr}; |
140 | return storageTypes[(int)type]; |
141 | } |
142 | |
143 | inline const char *getCtorArgTypename(MemberType type) { |
144 | const char *ctorArgTypes[] = {"TypeRef" , |
145 | "float" , |
146 | "unsigned_t" , |
147 | "bool" , |
148 | "int64_t" , |
149 | "std::string" , |
150 | "std::vector<float>" , |
151 | "std::vector<int>" , |
152 | "std::vector<unsigned_t>" , |
153 | "std::vector<int64_t>" , |
154 | "std::vector<size_t>" , |
155 | "std::vector<dim_t>" , |
156 | "std::vector<NodeValue>" , |
157 | nullptr, |
158 | nullptr}; |
159 | return ctorArgTypes[(int)type]; |
160 | } |
161 | |
162 | #endif // GLOW_TOOLS_CLASSGEN_MEMBERTYPE_H |
163 | |