Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 1 | {{- /* |
| 2 | -------------------------------------------------------------------------------- |
Ben Clayton | bccd87c | 2022-10-12 09:42:00 +0000 | [diff] [blame] | 3 | Template file for use with tools/src/cmd/gen to generate intrinsic_table.inl |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 4 | Used by BuiltinTable.cc for builtin overload resolution. |
| 5 | |
Ben Clayton | a123b89 | 2022-07-27 16:36:35 +0000 | [diff] [blame] | 6 | To update the generated file, run: |
| 7 | ./tools/run gen |
| 8 | |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 9 | See: |
Ben Clayton | cde5009 | 2022-07-26 15:46:44 +0000 | [diff] [blame] | 10 | * tools/src/cmd/gen for structures used by this template |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 11 | * https://golang.org/pkg/text/template/ for documentation on the template syntax |
| 12 | -------------------------------------------------------------------------------- |
| 13 | */ -}} |
| 14 | |
| 15 | // clang-format off |
| 16 | |
Ben Clayton | cde5009 | 2022-07-26 15:46:44 +0000 | [diff] [blame] | 17 | {{ with Sem -}} |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 18 | {{ range .Types -}} |
| 19 | {{ template "Type" . }} |
| 20 | {{ end -}} |
| 21 | {{ range .TypeMatchers -}} |
| 22 | {{ template "TypeMatcher" . }} |
| 23 | {{ end -}} |
| 24 | {{ range .EnumMatchers -}} |
| 25 | {{ template "EnumMatcher" . }} |
| 26 | {{ end -}} |
| 27 | {{- end -}} |
| 28 | |
Ben Clayton | e6e96de | 2022-05-09 18:08:23 +0000 | [diff] [blame] | 29 | {{- with IntrinsicTable -}} |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 30 | {{- template "Matchers" . }} |
| 31 | |
| 32 | constexpr MatcherIndex kMatcherIndices[] = { |
| 33 | {{- range $i, $idx := .MatcherIndices }} |
| 34 | /* [{{$i}}] */ {{$idx}}, |
| 35 | {{- end }} |
| 36 | }; |
| 37 | |
| 38 | // Assert that the MatcherIndex is big enough to index all the matchers, plus |
| 39 | // kNoMatcher. |
| 40 | static_assert(static_cast<int>(sizeof(kMatcherIndices) / sizeof(kMatcherIndices[0])) < |
| 41 | static_cast<int>(std::numeric_limits<MatcherIndex>::max() - 1), |
| 42 | "MatcherIndex is not large enough to index kMatcherIndices"); |
| 43 | |
| 44 | constexpr ParameterInfo kParameters[] = { |
| 45 | {{- range $i, $p := .Parameters }} |
| 46 | { |
| 47 | /* [{{$i}}] */ |
| 48 | /* usage */ ParameterUsage:: |
| 49 | {{- if $p.Usage }}k{{PascalCase $p.Usage}} |
| 50 | {{- else }}kNone |
| 51 | {{- end }}, |
| 52 | /* matcher indices */ &kMatcherIndices[{{$p.MatcherIndicesOffset}}], |
| 53 | }, |
| 54 | {{- end }} |
| 55 | }; |
| 56 | |
Ben Clayton | 4c9ed74 | 2022-05-17 22:42:32 +0000 | [diff] [blame] | 57 | constexpr TemplateTypeInfo kTemplateTypes[] = { |
| 58 | {{- range $i, $o := .TemplateTypes }} |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 59 | { |
| 60 | /* [{{$i}}] */ |
| 61 | /* name */ "{{$o.Name}}", |
| 62 | /* matcher index */ |
| 63 | {{- if ge $o.MatcherIndex 0 }} {{$o.MatcherIndex}} |
| 64 | {{- else }} kNoMatcher |
| 65 | {{- end }}, |
| 66 | }, |
| 67 | {{- end }} |
| 68 | }; |
| 69 | |
Ben Clayton | 4c9ed74 | 2022-05-17 22:42:32 +0000 | [diff] [blame] | 70 | constexpr TemplateNumberInfo kTemplateNumbers[] = { |
| 71 | {{- range $i, $o := .TemplateNumbers }} |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 72 | { |
| 73 | /* [{{$i}}] */ |
| 74 | /* name */ "{{$o.Name}}", |
| 75 | /* matcher index */ |
| 76 | {{- if ge $o.MatcherIndex 0 }} {{$o.MatcherIndex}} |
| 77 | {{- else }} kNoMatcher |
| 78 | {{- end }}, |
| 79 | }, |
| 80 | {{- end }} |
| 81 | }; |
| 82 | |
| 83 | constexpr OverloadInfo kOverloads[] = { |
| 84 | {{- range $i, $o := .Overloads }} |
| 85 | { |
| 86 | /* [{{$i}}] */ |
| 87 | /* num parameters */ {{$o.NumParameters}}, |
Ben Clayton | 4c9ed74 | 2022-05-17 22:42:32 +0000 | [diff] [blame] | 88 | /* num template types */ {{$o.NumTemplateTypes}}, |
| 89 | /* num template numbers */ {{$o.NumTemplateNumbers}}, |
| 90 | /* template types */ |
| 91 | {{- if $o.TemplateTypesOffset }} &kTemplateTypes[{{$o.TemplateTypesOffset}}], |
Ben Clayton | 451eee0 | 2022-06-01 23:57:20 +0000 | [diff] [blame] | 92 | {{- else }} nullptr, |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 93 | {{- end }} |
Ben Clayton | 4c9ed74 | 2022-05-17 22:42:32 +0000 | [diff] [blame] | 94 | /* template numbers */ |
| 95 | {{- if $o.TemplateNumbersOffset }} &kTemplateNumbers[{{$o.TemplateNumbersOffset}}] |
Ben Clayton | 451eee0 | 2022-06-01 23:57:20 +0000 | [diff] [blame] | 96 | {{- else }} nullptr |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 97 | {{- end }}, |
| 98 | /* parameters */ &kParameters[{{$o.ParametersOffset}}], |
| 99 | /* return matcher indices */ |
| 100 | {{- if $o.ReturnMatcherIndicesOffset }} &kMatcherIndices[{{$o.ReturnMatcherIndicesOffset}}] |
| 101 | {{- else }} nullptr |
| 102 | {{- end }}, |
Ben Clayton | 59e2394 | 2022-05-13 16:14:17 +0000 | [diff] [blame] | 103 | /* flags */ OverloadFlags(OverloadFlag::kIs{{Title $o.Kind}} |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 104 | {{- range $i, $u := $o.CanBeUsedInStage.List -}} |
Ben Clayton | 59e2394 | 2022-05-13 16:14:17 +0000 | [diff] [blame] | 105 | , OverloadFlag::kSupports{{Title $u}}Pipeline |
| 106 | {{- end }} |
Ben Clayton | d849032 | 2023-02-22 13:52:21 +0000 | [diff] [blame] | 107 | {{- if $o.MustUse}}, OverloadFlag::kMustUse{{end}} |
| 108 | {{- if $o.IsDeprecated}}, OverloadFlag::kIsDeprecated{{end -}} |
| 109 | ), |
Ben Clayton | 451eee0 | 2022-06-01 23:57:20 +0000 | [diff] [blame] | 110 | /* const eval */ |
Ben Clayton | ac660c2 | 2022-07-15 23:54:10 +0000 | [diff] [blame] | 111 | {{- if $o.ConstEvalFunction }} {{template "ConstEvalFn" $o}}, |
Ben Clayton | 451eee0 | 2022-06-01 23:57:20 +0000 | [diff] [blame] | 112 | {{- else }} nullptr, |
| 113 | {{- end }} |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 114 | }, |
| 115 | {{- end }} |
| 116 | }; |
| 117 | |
Ben Clayton | e6e96de | 2022-05-09 18:08:23 +0000 | [diff] [blame] | 118 | constexpr IntrinsicInfo kBuiltins[] = { |
| 119 | {{- range $i, $b := .Builtins }} |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 120 | { |
| 121 | /* [{{$i}}] */ |
Ben Clayton | e6e96de | 2022-05-09 18:08:23 +0000 | [diff] [blame] | 122 | {{- range $b.OverloadDescriptions }} |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 123 | /* {{.}} */ |
| 124 | {{- end }} |
Ben Clayton | e6e96de | 2022-05-09 18:08:23 +0000 | [diff] [blame] | 125 | /* num overloads */ {{$b.NumOverloads}}, |
| 126 | /* overloads */ &kOverloads[{{$b.OverloadsOffset}}], |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 127 | }, |
| 128 | {{- end }} |
| 129 | }; |
| 130 | |
Ben Clayton | 77473b4 | 2022-05-13 14:35:37 +0000 | [diff] [blame] | 131 | constexpr IntrinsicInfo kUnaryOperators[] = { |
| 132 | {{- range $i, $o := .UnaryOperators }} |
Ben Clayton | 9fb29a3 | 2022-05-09 20:00:13 +0000 | [diff] [blame] | 133 | { |
| 134 | /* [{{$i}}] */ |
| 135 | {{- range $o.OverloadDescriptions }} |
| 136 | /* {{.}} */ |
| 137 | {{- end }} |
| 138 | /* num overloads */ {{$o.NumOverloads}}, |
| 139 | /* overloads */ &kOverloads[{{$o.OverloadsOffset}}], |
| 140 | }, |
| 141 | {{- end }} |
| 142 | }; |
| 143 | |
Ben Clayton | 77473b4 | 2022-05-13 14:35:37 +0000 | [diff] [blame] | 144 | {{- range $i, $o := .UnaryOperators }} |
Ben Clayton | ac660c2 | 2022-07-15 23:54:10 +0000 | [diff] [blame] | 145 | constexpr uint8_t kUnaryOperator{{ template "ExpandName" $o.Name}} = {{$i}}; |
Ben Clayton | 77473b4 | 2022-05-13 14:35:37 +0000 | [diff] [blame] | 146 | {{- end }} |
| 147 | |
| 148 | constexpr IntrinsicInfo kBinaryOperators[] = { |
| 149 | {{- range $i, $o := .BinaryOperators }} |
| 150 | { |
| 151 | /* [{{$i}}] */ |
| 152 | {{- range $o.OverloadDescriptions }} |
| 153 | /* {{.}} */ |
| 154 | {{- end }} |
| 155 | /* num overloads */ {{$o.NumOverloads}}, |
| 156 | /* overloads */ &kOverloads[{{$o.OverloadsOffset}}], |
| 157 | }, |
| 158 | {{- end }} |
| 159 | }; |
| 160 | |
| 161 | {{- range $i, $o := .BinaryOperators }} |
Ben Clayton | ac660c2 | 2022-07-15 23:54:10 +0000 | [diff] [blame] | 162 | constexpr uint8_t kBinaryOperator{{ template "ExpandName" $o.Name}} = {{$i}}; |
Ben Clayton | 9fb29a3 | 2022-05-09 20:00:13 +0000 | [diff] [blame] | 163 | {{- end }} |
| 164 | |
Ben Clayton | 54a104e | 2023-02-22 20:04:40 +0000 | [diff] [blame] | 165 | constexpr IntrinsicInfo kConstructorsAndConverters[] = { |
| 166 | {{- range $i, $o := .ConstructorsAndConverters }} |
Ben Clayton | 6ae608c | 2022-05-16 20:54:42 +0000 | [diff] [blame] | 167 | { |
| 168 | /* [{{$i}}] */ |
| 169 | {{- range $o.OverloadDescriptions }} |
| 170 | /* {{.}} */ |
| 171 | {{- end }} |
| 172 | /* num overloads */ {{$o.NumOverloads}}, |
| 173 | /* overloads */ &kOverloads[{{$o.OverloadsOffset}}], |
| 174 | }, |
| 175 | {{- end }} |
| 176 | }; |
| 177 | |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 178 | // clang-format on |
| 179 | {{ end -}} |
| 180 | |
| 181 | {{- /* ------------------------------------------------------------------ */ -}} |
| 182 | {{- define "Type" -}} |
| 183 | {{- /* ------------------------------------------------------------------ */ -}} |
| 184 | {{- $class := PascalCase .Name -}} |
| 185 | /// TypeMatcher for 'type {{.Name}}' |
| 186 | {{- if .Decl.Source.S.Filepath }} |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 187 | {{- end }} |
| 188 | class {{$class}} : public TypeMatcher { |
| 189 | public: |
| 190 | /// Checks whether the given type matches the matcher rules. |
Ben Clayton | 23696b1 | 2022-05-18 21:56:28 +0000 | [diff] [blame] | 191 | /// Match may define and refine the template types and numbers in state. |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 192 | /// @param state the MatchState |
| 193 | /// @param type the type to match |
| 194 | /// @returns the canonicalized type on match, otherwise nullptr |
dan sinclair | 5f764d8 | 2022-12-08 00:32:27 +0000 | [diff] [blame] | 195 | const type::Type* Match(MatchState& state, |
| 196 | const type::Type* type) const override; |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 197 | /// @param state the MatchState |
| 198 | /// @return a string representation of the matcher. |
Ben Clayton | b1fa457 | 2022-05-19 18:26:09 +0000 | [diff] [blame] | 199 | std::string String(MatchState* state) const override; |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 200 | }; |
| 201 | |
dan sinclair | 5f764d8 | 2022-12-08 00:32:27 +0000 | [diff] [blame] | 202 | const type::Type* {{$class}}::Match(MatchState& state, const type::Type* ty) const { |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 203 | {{- range .TemplateParams }} |
| 204 | {{- template "DeclareLocalTemplateParam" . }} |
| 205 | {{- end }} |
Antonio Maiorano | fc7994b | 2022-10-04 22:19:48 +0000 | [diff] [blame] | 206 | if (!match_{{TrimLeft .Name "_"}}(state, ty{{range .TemplateParams}}, {{.GetName}}{{end}})) { |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 207 | return nullptr; |
| 208 | } |
| 209 | {{- range .TemplateParams }} |
| 210 | {{.Name}} = {{ template "MatchTemplateParam" .}}({{.Name}}); |
| 211 | if ({{ template "IsTemplateParamInvalid" .}}) { |
| 212 | return nullptr; |
| 213 | } |
| 214 | {{- end }} |
| 215 | return build_{{TrimLeft .Name "_"}}(state{{range .TemplateParams}}, {{.GetName}}{{end}}); |
| 216 | } |
| 217 | |
Ben Clayton | b1fa457 | 2022-05-19 18:26:09 +0000 | [diff] [blame] | 218 | std::string {{$class}}::String(MatchState*{{if .TemplateParams}} state{{end}}) const { |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 219 | {{- range .TemplateParams }} |
| 220 | {{- template "DeclareLocalTemplateParamName" . }} |
| 221 | {{- end }} |
| 222 | |
| 223 | {{- if .DisplayName }} |
dan sinclair | b23cda4 | 2023-02-28 17:31:54 +0000 | [diff] [blame] | 224 | utils::StringStream ss; |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 225 | ss{{range SplitDisplayName .DisplayName}} << {{.}}{{end}}; |
| 226 | return ss.str(); |
| 227 | {{- else if .TemplateParams }} |
| 228 | return "{{.Name}}<"{{template "AppendTemplateParamNames" .TemplateParams}} + ">"; |
| 229 | {{- else }} |
| 230 | return "{{.Name}}"; |
| 231 | {{- end }} |
| 232 | } |
| 233 | {{ end -}} |
| 234 | |
| 235 | {{- /* ------------------------------------------------------------------ */ -}} |
| 236 | {{- define "TypeMatcher" -}} |
| 237 | {{- /* ------------------------------------------------------------------ */ -}} |
| 238 | {{- $class := PascalCase .Name -}} |
| 239 | /// TypeMatcher for 'match {{.Name}}' |
| 240 | {{- if .Decl.Source.S.Filepath }} |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 241 | {{- end }} |
| 242 | class {{$class}} : public TypeMatcher { |
| 243 | public: |
| 244 | /// Checks whether the given type matches the matcher rules, and returns the |
| 245 | /// expected, canonicalized type on success. |
Ben Clayton | 23696b1 | 2022-05-18 21:56:28 +0000 | [diff] [blame] | 246 | /// Match may define and refine the template types and numbers in state. |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 247 | /// @param state the MatchState |
| 248 | /// @param type the type to match |
| 249 | /// @returns the canonicalized type on match, otherwise nullptr |
dan sinclair | 5f764d8 | 2022-12-08 00:32:27 +0000 | [diff] [blame] | 250 | const type::Type* Match(MatchState& state, |
| 251 | const type::Type* type) const override; |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 252 | /// @param state the MatchState |
| 253 | /// @return a string representation of the matcher. |
Ben Clayton | b1fa457 | 2022-05-19 18:26:09 +0000 | [diff] [blame] | 254 | std::string String(MatchState* state) const override; |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 255 | }; |
| 256 | |
dan sinclair | 5f764d8 | 2022-12-08 00:32:27 +0000 | [diff] [blame] | 257 | const type::Type* {{$class}}::Match(MatchState& state, const type::Type* ty) const { |
Ben Clayton | c670018 | 2022-05-18 18:56:58 +0000 | [diff] [blame] | 258 | {{- range .PrecedenceSortedTypes }} |
Antonio Maiorano | fc7994b | 2022-10-04 22:19:48 +0000 | [diff] [blame] | 259 | if (match_{{.Name}}(state, ty)) { |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 260 | return build_{{.Name}}(state); |
| 261 | } |
| 262 | {{- end }} |
| 263 | return nullptr; |
| 264 | } |
| 265 | |
Ben Clayton | b1fa457 | 2022-05-19 18:26:09 +0000 | [diff] [blame] | 266 | std::string {{$class}}::String(MatchState*) const { |
dan sinclair | b23cda4 | 2023-02-28 17:31:54 +0000 | [diff] [blame] | 267 | utils::StringStream ss; |
Ben Clayton | b1fa457 | 2022-05-19 18:26:09 +0000 | [diff] [blame] | 268 | // Note: We pass nullptr to the TypeMatcher::String() functions, as 'matcher's do not support |
| 269 | // template arguments, nor can they match sub-types. As such, they have no use for the MatchState. |
| 270 | ss |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 271 | {{- range .Types -}} |
Ben Clayton | b1fa457 | 2022-05-19 18:26:09 +0000 | [diff] [blame] | 272 | {{- if IsFirstIn . $.Types }} << {{PascalCase .Name}}().String(nullptr) |
| 273 | {{- else if IsLastIn . $.Types }} << " or " << {{PascalCase .Name}}().String(nullptr) |
| 274 | {{- else }} << ", " << {{PascalCase .Name}}().String(nullptr) |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 275 | {{- end -}} |
Ben Clayton | b1fa457 | 2022-05-19 18:26:09 +0000 | [diff] [blame] | 276 | {{- end -}}; |
| 277 | return ss.str(); |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 278 | } |
| 279 | {{ end -}} |
| 280 | |
| 281 | {{- /* ------------------------------------------------------------------ */ -}} |
| 282 | {{- define "EnumMatcher" -}} |
| 283 | {{- /* ------------------------------------------------------------------ */ -}} |
| 284 | {{- $class := PascalCase .Name -}} |
| 285 | {{- $enum := PascalCase .Enum.Name -}} |
| 286 | /// EnumMatcher for 'match {{.Name}}' |
| 287 | {{- if .Decl.Source.S.Filepath }} |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 288 | {{- end }} |
| 289 | class {{$class}} : public NumberMatcher { |
| 290 | public: |
| 291 | /// Checks whether the given number matches the enum matcher rules. |
Ben Clayton | 23696b1 | 2022-05-18 21:56:28 +0000 | [diff] [blame] | 292 | /// Match may define template numbers in state. |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 293 | /// @param state the MatchState |
| 294 | /// @param number the enum value as a Number |
| 295 | /// @return true if the enum value matches the set |
| 296 | Number Match(MatchState& state, Number number) const override; |
| 297 | /// @param state the MatchState |
| 298 | /// @return a string representation of the matcher. |
Ben Clayton | b1fa457 | 2022-05-19 18:26:09 +0000 | [diff] [blame] | 299 | std::string String(MatchState* state) const override; |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 300 | }; |
| 301 | |
| 302 | {{ if eq 1 (len .Options) -}} |
| 303 | {{- $option := index .Options 0 }} |
| 304 | {{- $entry := printf "k%v" (PascalCase $option.Name) -}} |
| 305 | Number {{$class}}::Match(MatchState&, Number number) const { |
| 306 | if (number.IsAny() || number.Value() == static_cast<uint32_t>({{$enum}}::{{$entry}})) { |
| 307 | return Number(static_cast<uint32_t>({{$enum}}::{{$entry}})); |
| 308 | } |
| 309 | return Number::invalid; |
| 310 | } |
| 311 | {{- else -}} |
| 312 | Number {{$class}}::Match(MatchState&, Number number) const { |
| 313 | switch (static_cast<{{$enum}}>(number.Value())) { |
| 314 | {{- range .Options }} |
| 315 | case {{$enum}}::k{{PascalCase .Name}}: |
| 316 | {{- end }} |
| 317 | return number; |
| 318 | default: |
| 319 | return Number::invalid; |
| 320 | } |
| 321 | } |
| 322 | {{- end }} |
| 323 | |
Ben Clayton | b1fa457 | 2022-05-19 18:26:09 +0000 | [diff] [blame] | 324 | std::string {{$class}}::String(MatchState*) const { |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 325 | return " |
| 326 | {{- range .Options -}} |
| 327 | {{- if IsFirstIn . $.Options }}{{.Name}} |
| 328 | {{- else if IsLastIn . $.Options }} or {{.Name}} |
| 329 | {{- else }}, {{.Name}} |
| 330 | {{- end -}} |
| 331 | {{- end -}} |
| 332 | "; |
| 333 | } |
| 334 | {{ end -}} |
| 335 | |
| 336 | {{- /* ------------------------------------------------------------------ */ -}} |
| 337 | {{- define "Matchers" -}} |
| 338 | {{- /* ------------------------------------------------------------------ */ -}} |
| 339 | /// Matchers holds type and number matchers |
| 340 | class Matchers { |
| 341 | private: |
| 342 | {{- $t_names := Map -}} |
| 343 | {{- $n_names := Map -}} |
Ben Clayton | cde5009 | 2022-07-26 15:46:44 +0000 | [diff] [blame] | 344 | {{- range Iterate Sem.MaxTemplateTypes -}} |
Ben Clayton | 4c9ed74 | 2022-05-17 22:42:32 +0000 | [diff] [blame] | 345 | {{- $name := printf "template_type_%v" . -}} |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 346 | {{- $t_names.Put . $name }} |
Ben Clayton | 4c9ed74 | 2022-05-17 22:42:32 +0000 | [diff] [blame] | 347 | TemplateTypeMatcher {{$name}}_{ {{- . -}} }; |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 348 | {{- end }} |
Ben Clayton | cde5009 | 2022-07-26 15:46:44 +0000 | [diff] [blame] | 349 | {{- range Iterate Sem.MaxTemplateNumbers -}} |
Ben Clayton | 4c9ed74 | 2022-05-17 22:42:32 +0000 | [diff] [blame] | 350 | {{- $name := printf "template_number_%v" . -}} |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 351 | {{- $n_names.Put . $name }} |
Ben Clayton | 4c9ed74 | 2022-05-17 22:42:32 +0000 | [diff] [blame] | 352 | TemplateNumberMatcher {{$name}}_{ {{- . -}} }; |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 353 | {{- end }} |
Ben Clayton | cde5009 | 2022-07-26 15:46:44 +0000 | [diff] [blame] | 354 | {{- range Sem.Types -}} |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 355 | {{- $name := PascalCase .Name -}} |
| 356 | {{- $t_names.Put . $name }} |
| 357 | {{$name}} {{$name}}_; |
| 358 | {{- end }} |
Ben Clayton | cde5009 | 2022-07-26 15:46:44 +0000 | [diff] [blame] | 359 | {{- range Sem.TypeMatchers -}} |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 360 | {{- $name := PascalCase .Name -}} |
| 361 | {{- $t_names.Put . $name }} |
| 362 | {{$name}} {{$name}}_; |
| 363 | {{- end }} |
Ben Clayton | cde5009 | 2022-07-26 15:46:44 +0000 | [diff] [blame] | 364 | {{- range Sem.EnumMatchers -}} |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 365 | {{- $name := PascalCase .Name -}} |
| 366 | {{- $n_names.Put . $name }} |
| 367 | {{$name}} {{$name}}_; |
| 368 | {{- end }} |
| 369 | |
| 370 | public: |
| 371 | /// Constructor |
| 372 | Matchers(); |
| 373 | /// Destructor |
| 374 | ~Matchers(); |
| 375 | |
Ben Clayton | 4c9ed74 | 2022-05-17 22:42:32 +0000 | [diff] [blame] | 376 | /// The template types, types, and type matchers |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 377 | TypeMatcher const* const type[{{len .TMatchers}}] = { |
| 378 | {{- range $i, $m := .TMatchers }} |
| 379 | /* [{{$i}}] */ |
| 380 | {{- if $m }} &{{$t_names.Get $m}}_, |
| 381 | {{- else }} &{{$t_names.Get $i}}_, |
| 382 | {{- end }} |
| 383 | {{- end }} |
| 384 | }; |
| 385 | |
Ben Clayton | 4c9ed74 | 2022-05-17 22:42:32 +0000 | [diff] [blame] | 386 | /// The template numbers, and number matchers |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 387 | NumberMatcher const* const number[{{len .NMatchers}}] = { |
| 388 | {{- range $i, $m := .NMatchers }} |
| 389 | /* [{{$i}}] */ |
| 390 | {{- if $m }} &{{$n_names.Get $m}}_, |
| 391 | {{- else }} &{{$n_names.Get $i}}_, |
| 392 | {{- end }} |
| 393 | {{- end }} |
| 394 | }; |
| 395 | }; |
| 396 | |
| 397 | Matchers::Matchers() = default; |
| 398 | Matchers::~Matchers() = default; |
| 399 | {{- end -}} |
| 400 | |
| 401 | {{- /* ------------------------------------------------------------------ */ -}} |
| 402 | {{- define "DeclareLocalTemplateParam" -}} |
| 403 | {{- /* ------------------------------------------------------------------ */ -}} |
| 404 | {{- if IsTemplateTypeParam . }} |
dan sinclair | 5f764d8 | 2022-12-08 00:32:27 +0000 | [diff] [blame] | 405 | const type::Type* {{.Name}} = nullptr; |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 406 | {{- else if IsTemplateNumberParam . }} |
| 407 | Number {{.Name}} = Number::invalid; |
| 408 | {{- else if IsTemplateEnumParam . }} |
| 409 | Number {{.Name}} = Number::invalid; |
| 410 | {{- end -}} |
| 411 | {{- end -}} |
| 412 | |
| 413 | {{- /* ------------------------------------------------------------------ */ -}} |
| 414 | {{- define "DeclareLocalTemplateParamName" -}} |
| 415 | {{- /* ------------------------------------------------------------------ */ -}} |
| 416 | {{- if IsTemplateTypeParam . }} |
Ben Clayton | b1fa457 | 2022-05-19 18:26:09 +0000 | [diff] [blame] | 417 | const std::string {{.Name}} = state->TypeName(); |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 418 | {{- else if IsTemplateNumberParam . }} |
Ben Clayton | b1fa457 | 2022-05-19 18:26:09 +0000 | [diff] [blame] | 419 | const std::string {{.Name}} = state->NumName(); |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 420 | {{- else if IsTemplateEnumParam . }} |
Ben Clayton | b1fa457 | 2022-05-19 18:26:09 +0000 | [diff] [blame] | 421 | const std::string {{.Name}} = state->NumName(); |
Ben Clayton | b85e692 | 2022-02-02 23:07:11 +0000 | [diff] [blame] | 422 | {{- end -}} |
| 423 | {{- end -}} |
| 424 | |
| 425 | {{- /* ------------------------------------------------------------------ */ -}} |
| 426 | {{- define "MatchTemplateParam" -}} |
| 427 | {{- /* ------------------------------------------------------------------ */ -}} |
| 428 | {{- if IsTemplateTypeParam . -}} |
| 429 | state.Type |
| 430 | {{- else if IsTemplateNumberParam . -}} |
| 431 | state.Num |
| 432 | {{- else if IsTemplateEnumParam . -}} |
| 433 | state.Num |
| 434 | {{- end -}} |
| 435 | {{- end -}} |
| 436 | |
| 437 | {{- /* ------------------------------------------------------------------ */ -}} |
| 438 | {{- define "IsTemplateParamInvalid" -}} |
| 439 | {{- /* ------------------------------------------------------------------ */ -}} |
| 440 | {{- if IsTemplateTypeParam . -}} |
| 441 | {{.Name}} == nullptr |
| 442 | {{- else if IsTemplateNumberParam . -}} |
| 443 | !{{.Name}}.IsValid() |
| 444 | {{- else if IsTemplateEnumParam . -}} |
| 445 | !{{.Name}}.IsValid() |
| 446 | {{- end -}} |
| 447 | {{- end -}} |
| 448 | |
| 449 | {{- /* ------------------------------------------------------------------ */ -}} |
| 450 | {{- define "AppendTemplateParamNames" -}} |
| 451 | {{- /* ------------------------------------------------------------------ */ -}} |
| 452 | {{- range $i, $ := . -}} |
| 453 | {{- if $i }} + ", " + {{.Name}} |
| 454 | {{- else }} + {{.Name}} |
| 455 | {{- end -}} |
| 456 | {{- end -}} |
| 457 | {{- end -}} |
Ben Clayton | 9fb29a3 | 2022-05-09 20:00:13 +0000 | [diff] [blame] | 458 | |
| 459 | {{- /* ------------------------------------------------------------------ */ -}} |
Ben Clayton | ac660c2 | 2022-07-15 23:54:10 +0000 | [diff] [blame] | 460 | {{- define "ExpandName" -}} |
Ben Clayton | 9fb29a3 | 2022-05-09 20:00:13 +0000 | [diff] [blame] | 461 | {{- /* ------------------------------------------------------------------ */ -}} |
| 462 | {{- if eq . "<<" -}}ShiftLeft |
| 463 | {{- else if eq . "&" -}}And |
| 464 | {{- else if eq . "|" -}}Or |
| 465 | {{- else if eq . "^" -}}Xor |
| 466 | {{- else if eq . "&&" -}}LogicalAnd |
| 467 | {{- else if eq . "||" -}}LogicalOr |
| 468 | {{- else if eq . "==" -}}Equal |
Ben Clayton | b61e045 | 2022-05-09 21:22:24 +0000 | [diff] [blame] | 469 | {{- else if eq . "!" -}}Not |
Ben Clayton | 9fb29a3 | 2022-05-09 20:00:13 +0000 | [diff] [blame] | 470 | {{- else if eq . "!=" -}}NotEqual |
Ben Clayton | b61e045 | 2022-05-09 21:22:24 +0000 | [diff] [blame] | 471 | {{- else if eq . "~" -}}Complement |
Ben Clayton | 9fb29a3 | 2022-05-09 20:00:13 +0000 | [diff] [blame] | 472 | {{- else if eq . "<" -}}LessThan |
| 473 | {{- else if eq . ">" -}}GreaterThan |
| 474 | {{- else if eq . "<=" -}}LessThanEqual |
| 475 | {{- else if eq . ">=" -}}GreaterThanEqual |
| 476 | {{- else if eq . "<<" -}}ShiftLeft |
| 477 | {{- else if eq . ">>" -}}ShiftRight |
| 478 | {{- else if eq . "+" -}}Plus |
| 479 | {{- else if eq . "-" -}}Minus |
| 480 | {{- else if eq . "*" -}}Star |
| 481 | {{- else if eq . "/" -}}Divide |
| 482 | {{- else if eq . "%" -}}Modulo |
Ben Clayton | ac660c2 | 2022-07-15 23:54:10 +0000 | [diff] [blame] | 483 | {{- else -}}{{.}} |
Ben Clayton | 9fb29a3 | 2022-05-09 20:00:13 +0000 | [diff] [blame] | 484 | {{- end -}} |
| 485 | {{- end -}} |
Ben Clayton | ac660c2 | 2022-07-15 23:54:10 +0000 | [diff] [blame] | 486 | |
| 487 | |
| 488 | {{- /* ------------------------------------------------------------------ */ -}} |
| 489 | {{- define "ConstEvalFn" -}} |
| 490 | {{- /* ------------------------------------------------------------------ */ -}} |
| 491 | &ConstEval:: |
| 492 | {{- if eq .Kind "operator" -}}Op{{end -}} |
| 493 | {{template "ExpandName" .ConstEvalFunction}} |
| 494 | {{- end -}} |
| 495 | |