tint/ast: Generate interpolate_attribute.[h|cc]
Emits all the enum info from the single-source-of-truth `intrinsics.def` file
Change-Id: Ie9deba9e64927945133027cf243777944119ea41
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/105327
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Dan Sinclair <dsinclair@chromium.org>
Commit-Queue: Ben Clayton <bclayton@google.com>
diff --git a/src/tint/ast/builtin_value.h b/src/tint/ast/builtin_value.h
index eb45b39..f7dd660 100644
--- a/src/tint/ast/builtin_value.h
+++ b/src/tint/ast/builtin_value.h
@@ -27,7 +27,7 @@
namespace tint::ast {
-/// Storage class of a given pointer.
+/// Builtin value defined with `@builtin(<name>)`.
enum class BuiltinValue {
kInvalid,
kFragDepth,
diff --git a/src/tint/ast/builtin_value.h.tmpl b/src/tint/ast/builtin_value.h.tmpl
index 1985305..ea63d56 100644
--- a/src/tint/ast/builtin_value.h.tmpl
+++ b/src/tint/ast/builtin_value.h.tmpl
@@ -18,7 +18,7 @@
namespace tint::ast {
-/// Storage class of a given pointer.
+/// Builtin value defined with `@builtin(<name>)`.
{{ Eval "DeclareEnum" $enum}}
} // namespace tint::ast
diff --git a/src/tint/ast/interpolate_attribute.cc b/src/tint/ast/interpolate_attribute.cc
index 29e3bfe..7c89ee1 100644
--- a/src/tint/ast/interpolate_attribute.cc
+++ b/src/tint/ast/interpolate_attribute.cc
@@ -12,6 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.
+////////////////////////////////////////////////////////////////////////////////
+// File generated by tools/src/cmd/gen
+// using the template:
+// src/tint/ast/interpolate_attribute.cc.tmpl
+//
+// Do not modify this file directly
+////////////////////////////////////////////////////////////////////////////////
+
#include "src/tint/ast/interpolate_attribute.h"
#include <string>
@@ -41,44 +49,64 @@
return ctx->dst->create<InterpolateAttribute>(src, type, sampling);
}
-std::ostream& operator<<(std::ostream& out, InterpolationType type) {
- switch (type) {
- case InterpolationType::kPerspective: {
- out << "perspective";
- break;
- }
- case InterpolationType::kLinear: {
- out << "linear";
- break;
- }
- case InterpolationType::kFlat: {
- out << "flat";
- break;
- }
+/// ParseInterpolationType parses a InterpolationType from a string.
+/// @param str the string to parse
+/// @returns the parsed enum, or InterpolationType::kInvalid if the string could not be parsed.
+InterpolationType ParseInterpolationType(std::string_view str) {
+ if (str == "flat") {
+ return InterpolationType::kFlat;
}
- return out;
+ if (str == "linear") {
+ return InterpolationType::kLinear;
+ }
+ if (str == "perspective") {
+ return InterpolationType::kPerspective;
+ }
+ return InterpolationType::kInvalid;
}
-std::ostream& operator<<(std::ostream& out, InterpolationSampling sampling) {
- switch (sampling) {
- case InterpolationSampling::kNone: {
- out << "none";
- break;
- }
- case InterpolationSampling::kCenter: {
- out << "center";
- break;
- }
- case InterpolationSampling::kCentroid: {
- out << "centroid";
- break;
- }
- case InterpolationSampling::kSample: {
- out << "sample";
- break;
- }
+std::ostream& operator<<(std::ostream& out, InterpolationType value) {
+ switch (value) {
+ case InterpolationType::kInvalid:
+ return out << "invalid";
+ case InterpolationType::kFlat:
+ return out << "flat";
+ case InterpolationType::kLinear:
+ return out << "linear";
+ case InterpolationType::kPerspective:
+ return out << "perspective";
}
- return out;
+ return out << "<unknown>";
+}
+
+/// ParseInterpolationSampling parses a InterpolationSampling from a string.
+/// @param str the string to parse
+/// @returns the parsed enum, or InterpolationSampling::kInvalid if the string could not be parsed.
+InterpolationSampling ParseInterpolationSampling(std::string_view str) {
+ if (str == "center") {
+ return InterpolationSampling::kCenter;
+ }
+ if (str == "centroid") {
+ return InterpolationSampling::kCentroid;
+ }
+ if (str == "sample") {
+ return InterpolationSampling::kSample;
+ }
+ return InterpolationSampling::kInvalid;
+}
+
+std::ostream& operator<<(std::ostream& out, InterpolationSampling value) {
+ switch (value) {
+ case InterpolationSampling::kInvalid:
+ return out << "invalid";
+ case InterpolationSampling::kCenter:
+ return out << "center";
+ case InterpolationSampling::kCentroid:
+ return out << "centroid";
+ case InterpolationSampling::kSample:
+ return out << "sample";
+ }
+ return out << "<unknown>";
}
} // namespace tint::ast
diff --git a/src/tint/ast/interpolate_attribute.cc.tmpl b/src/tint/ast/interpolate_attribute.cc.tmpl
new file mode 100644
index 0000000..b6f0b35
--- /dev/null
+++ b/src/tint/ast/interpolate_attribute.cc.tmpl
@@ -0,0 +1,50 @@
+{{- /*
+--------------------------------------------------------------------------------
+Template file for use with tools/src/cmd/gen to generate builtin_value.cc
+
+See:
+* tools/src/cmd/gen for structures used by this template
+* https://golang.org/pkg/text/template/ for documentation on the template syntax
+--------------------------------------------------------------------------------
+*/ -}}
+
+{{- Import "src/tint/templates/enums.tmpl.inc" -}}
+
+#include "src/tint/ast/interpolate_attribute.h"
+
+#include <string>
+
+#include "src/tint/program_builder.h"
+
+TINT_INSTANTIATE_TYPEINFO(tint::ast::InterpolateAttribute);
+
+namespace tint::ast {
+
+InterpolateAttribute::InterpolateAttribute(ProgramID pid,
+ NodeID nid,
+ const Source& src,
+ InterpolationType ty,
+ InterpolationSampling smpl)
+ : Base(pid, nid, src), type(ty), sampling(smpl) {}
+
+InterpolateAttribute::~InterpolateAttribute() = default;
+
+std::string InterpolateAttribute::Name() const {
+ return "interpolate";
+}
+
+const InterpolateAttribute* InterpolateAttribute::Clone(CloneContext* ctx) const {
+ // Clone arguments outside of create() call to have deterministic ordering
+ auto src = ctx->Clone(source);
+ return ctx->dst->create<InterpolateAttribute>(src, type, sampling);
+}
+
+{{ Eval "ParseEnum" (Sem.Enum "interpolation_type")}}
+
+{{ Eval "EnumOStream" (Sem.Enum "interpolation_type")}}
+
+{{ Eval "ParseEnum" (Sem.Enum "interpolation_sampling")}}
+
+{{ Eval "EnumOStream" (Sem.Enum "interpolation_sampling")}}
+
+} // namespace tint::ast
diff --git a/src/tint/ast/interpolate_attribute.h b/src/tint/ast/interpolate_attribute.h
index 4f9ea9d..c6b4af0 100644
--- a/src/tint/ast/interpolate_attribute.h
+++ b/src/tint/ast/interpolate_attribute.h
@@ -1,4 +1,4 @@
-// Copyright 2021 The Tint Authors.
+// Copyright 2022 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -12,6 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.
+////////////////////////////////////////////////////////////////////////////////
+// File generated by tools/src/cmd/gen
+// using the template:
+// src/tint/ast/interpolate_attribute.h.tmpl
+//
+// Do not modify this file directly
+////////////////////////////////////////////////////////////////////////////////
+
#ifndef SRC_TINT_AST_INTERPOLATE_ATTRIBUTE_H_
#define SRC_TINT_AST_INTERPOLATE_ATTRIBUTE_H_
@@ -23,10 +31,52 @@
namespace tint::ast {
/// The interpolation type.
-enum class InterpolationType { kPerspective, kLinear, kFlat };
+enum class InterpolationType {
+ kInvalid,
+ kFlat,
+ kLinear,
+ kPerspective,
+};
+
+/// @param out the std::ostream to write to
+/// @param value the InterpolationType
+/// @returns `out` so calls can be chained
+std::ostream& operator<<(std::ostream& out, InterpolationType value);
+
+/// ParseInterpolationType parses a InterpolationType from a string.
+/// @param str the string to parse
+/// @returns the parsed enum, or InterpolationType::kInvalid if the string could not be parsed.
+InterpolationType ParseInterpolationType(std::string_view str);
+
+constexpr const char* kInterpolationTypeStrings[] = {
+ "flat",
+ "linear",
+ "perspective",
+};
/// The interpolation sampling.
-enum class InterpolationSampling { kNone = -1, kCenter, kCentroid, kSample };
+enum class InterpolationSampling {
+ kInvalid,
+ kCenter,
+ kCentroid,
+ kSample,
+};
+
+/// @param out the std::ostream to write to
+/// @param value the InterpolationSampling
+/// @returns `out` so calls can be chained
+std::ostream& operator<<(std::ostream& out, InterpolationSampling value);
+
+/// ParseInterpolationSampling parses a InterpolationSampling from a string.
+/// @param str the string to parse
+/// @returns the parsed enum, or InterpolationSampling::kInvalid if the string could not be parsed.
+InterpolationSampling ParseInterpolationSampling(std::string_view str);
+
+constexpr const char* kInterpolationSamplingStrings[] = {
+ "center",
+ "centroid",
+ "sample",
+};
/// An interpolate attribute
class InterpolateAttribute final : public Castable<InterpolateAttribute, Attribute> {
@@ -60,16 +110,6 @@
const InterpolationSampling sampling;
};
-/// @param out the std::ostream to write to
-/// @param type the interpolation type
-/// @return the std::ostream so calls can be chained
-std::ostream& operator<<(std::ostream& out, InterpolationType type);
-
-/// @param out the std::ostream to write to
-/// @param sampling the interpolation sampling
-/// @return the std::ostream so calls can be chained
-std::ostream& operator<<(std::ostream& out, InterpolationSampling sampling);
-
} // namespace tint::ast
#endif // SRC_TINT_AST_INTERPOLATE_ATTRIBUTE_H_
diff --git a/src/tint/ast/interpolate_attribute.h.tmpl b/src/tint/ast/interpolate_attribute.h.tmpl
new file mode 100644
index 0000000..c225cb6
--- /dev/null
+++ b/src/tint/ast/interpolate_attribute.h.tmpl
@@ -0,0 +1,63 @@
+{{- /*
+--------------------------------------------------------------------------------
+Template file for use with tools/src/cmd/gen to generate interpolate_attribute.h
+
+See:
+* tools/src/cmd/gen for structures used by this template
+* https://golang.org/pkg/text/template/ for documentation on the template syntax
+--------------------------------------------------------------------------------
+*/ -}}
+
+{{- Import "src/tint/templates/enums.tmpl.inc" -}}
+
+#ifndef SRC_TINT_AST_INTERPOLATE_ATTRIBUTE_H_
+#define SRC_TINT_AST_INTERPOLATE_ATTRIBUTE_H_
+
+#include <ostream>
+#include <string>
+
+#include "src/tint/ast/attribute.h"
+
+namespace tint::ast {
+
+/// The interpolation type.
+{{ Eval "DeclareEnum" (Sem.Enum "interpolation_type") }}
+
+/// The interpolation sampling.
+{{ Eval "DeclareEnum" (Sem.Enum "interpolation_sampling") }}
+
+/// An interpolate attribute
+class InterpolateAttribute final : public Castable<InterpolateAttribute, Attribute> {
+ public:
+ /// Create an interpolate attribute.
+ /// @param pid the identifier of the program that owns this node
+ /// @param nid the unique node identifier
+ /// @param src the source of this node
+ /// @param type the interpolation type
+ /// @param sampling the interpolation sampling
+ InterpolateAttribute(ProgramID pid,
+ NodeID nid,
+ const Source& src,
+ InterpolationType type,
+ InterpolationSampling sampling);
+ ~InterpolateAttribute() override;
+
+ /// @returns the WGSL name for the attribute
+ std::string Name() const override;
+
+ /// Clones this node and all transitive child nodes using the `CloneContext`
+ /// `ctx`.
+ /// @param ctx the clone context
+ /// @return the newly cloned node
+ const InterpolateAttribute* Clone(CloneContext* ctx) const override;
+
+ /// The interpolation type
+ const InterpolationType type;
+
+ /// The interpolation sampling
+ const InterpolationSampling sampling;
+};
+
+} // namespace tint::ast
+
+#endif // SRC_TINT_AST_INTERPOLATE_ATTRIBUTE_H_