tint/ast: Generate access.[h|cc]

Emits all the enum info from the single-source-of-truth `intrinsics.def` file

Change-Id: Ib9170a2337597d4d81983c446d50582b518c6d71
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/105329
Reviewed-by: Dan Sinclair <dsinclair@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Ben Clayton <bclayton@chromium.org>
This commit is contained in:
Ben Clayton
2022-10-12 09:42:00 +00:00
committed by Dawn LUCI CQ
parent 47b7fe5785
commit bccd87c37a
27 changed files with 235 additions and 157 deletions

View File

@@ -12,30 +12,46 @@
// 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/access.cc.tmpl
//
// Do not modify this file directly
////////////////////////////////////////////////////////////////////////////////
#include "src/tint/ast/access.h"
namespace tint::ast {
std::ostream& operator<<(std::ostream& out, Access access) {
switch (access) {
case ast::Access::kUndefined: {
out << "undefined";
break;
}
case ast::Access::kRead: {
out << "read";
break;
}
case ast::Access::kReadWrite: {
out << "read_write";
break;
}
case ast::Access::kWrite: {
out << "write";
break;
}
/// ParseAccess parses a Access from a string.
/// @param str the string to parse
/// @returns the parsed enum, or Access::kInvalid if the string could not be parsed.
Access ParseAccess(std::string_view str) {
if (str == "read") {
return Access::kRead;
}
return out;
if (str == "read_write") {
return Access::kReadWrite;
}
if (str == "write") {
return Access::kWrite;
}
return Access::kInvalid;
}
std::ostream& operator<<(std::ostream& out, Access value) {
switch (value) {
case Access::kInvalid:
return out << "invalid";
case Access::kRead:
return out << "read";
case Access::kReadWrite:
return out << "read_write";
case Access::kWrite:
return out << "write";
}
return out << "<unknown>";
}
} // namespace tint::ast

View File

@@ -0,0 +1,25 @@
{{- /*
--------------------------------------------------------------------------------
Template file for use with tools/src/cmd/gen to generate access.cc
To update the generated file, run:
./tools/run gen
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" -}}
{{- $enum := (Sem.Enum "access") -}}
#include "src/tint/ast/access.h"
namespace tint::ast {
{{ Eval "ParseEnum" $enum}}
{{ Eval "EnumOStream" $enum}}
} // namespace tint::ast

View File

@@ -12,32 +12,44 @@
// 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/access.h.tmpl
//
// Do not modify this file directly
////////////////////////////////////////////////////////////////////////////////
#ifndef SRC_TINT_AST_ACCESS_H_
#define SRC_TINT_AST_ACCESS_H_
#include <ostream>
#include <string>
namespace tint::ast {
/// The access control settings
enum Access {
/// Not declared in the source
kUndefined = 0,
/// Read only
/// Address space of a given pointer.
enum class Access {
kInvalid,
kRead,
/// Write only
kWrite,
/// Read write
kReadWrite,
// Last valid access mode
kLastValid = kReadWrite,
kWrite,
};
/// @param out the std::ostream to write to
/// @param access the Access
/// @return the std::ostream so calls can be chained
std::ostream& operator<<(std::ostream& out, Access access);
/// @param value the Access
/// @returns `out` so calls can be chained
std::ostream& operator<<(std::ostream& out, Access value);
/// ParseAccess parses a Access from a string.
/// @param str the string to parse
/// @returns the parsed enum, or Access::kInvalid if the string could not be parsed.
Access ParseAccess(std::string_view str);
constexpr const char* kAccessStrings[] = {
"read",
"read_write",
"write",
};
} // namespace tint::ast

View File

@@ -0,0 +1,29 @@
{{- /*
--------------------------------------------------------------------------------
Template file for use with tools/src/cmd/gen to generate access.h
To update the generated file, run:
./tools/run gen
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" -}}
{{- $enum := (Sem.Enum "access") -}}
#ifndef SRC_TINT_AST_ACCESS_H_
#define SRC_TINT_AST_ACCESS_H_
#include <ostream>
namespace tint::ast {
/// Address space of a given pointer.
{{ Eval "DeclareEnum" $enum}}
} // namespace tint::ast
#endif // SRC_TINT_AST_ACCESS_H_

View File

@@ -35,7 +35,7 @@ std::string Pointer::FriendlyName(const SymbolTable& symbols) const {
out << address_space << ", ";
}
out << type->FriendlyName(symbols);
if (access != ast::Access::kUndefined) {
if (access != ast::Access::kInvalid) {
out << ", " << access;
}
out << ">";

View File

@@ -32,7 +32,7 @@ TEST_F(AstPointerTest, Creation) {
TEST_F(AstPointerTest, FriendlyName) {
auto* i32 = create<I32>();
auto* p = create<Pointer>(i32, ast::AddressSpace::kWorkgroup, Access::kUndefined);
auto* p = create<Pointer>(i32, ast::AddressSpace::kWorkgroup, Access::kInvalid);
EXPECT_EQ(p->FriendlyName(Symbols()), "ptr<workgroup, i32>");
}