ast: Add ast::NamedType
Have ast::Struct and ast::Alias derive from it. NamedType currently derives from ast::Type, but this might change in the future. Bug: tint:724 Change-Id: I46ab7751c89176f4dec34d63247d2459d02ab6bb Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/49525 Kokoro: Kokoro <noreply+kokoro@google.com> Commit-Queue: Ben Clayton <bclayton@google.com> Reviewed-by: Antonio Maiorano <amaiorano@google.com>
This commit is contained in:
parent
77a7518c95
commit
1737485a05
|
@ -342,6 +342,8 @@ libtint_source_set("libtint_core_all_src") {
|
|||
"ast/module.h",
|
||||
"ast/multisampled_texture.cc",
|
||||
"ast/multisampled_texture.h",
|
||||
"ast/named_type.cc",
|
||||
"ast/named_type.h",
|
||||
"ast/node.cc",
|
||||
"ast/node.h",
|
||||
"ast/override_decoration.cc",
|
||||
|
|
|
@ -124,6 +124,8 @@ set(TINT_LIB_SRCS
|
|||
ast/module.h
|
||||
ast/multisampled_texture.cc
|
||||
ast/multisampled_texture.h
|
||||
ast/named_type.cc
|
||||
ast/named_type.h
|
||||
ast/node.cc
|
||||
ast/node.h
|
||||
ast/override_decoration.cc
|
||||
|
|
|
@ -23,12 +23,11 @@ namespace ast {
|
|||
|
||||
Alias::Alias(ProgramID program_id,
|
||||
const Source& source,
|
||||
const Symbol& sym,
|
||||
const Symbol& name,
|
||||
Type* subtype)
|
||||
: Base(program_id, source),
|
||||
symbol_(sym),
|
||||
: Base(program_id, source, name),
|
||||
subtype_(subtype),
|
||||
type_name_("__alias_" + sym.to_str() + subtype->type_name()) {
|
||||
type_name_("__alias_" + name.to_str() + subtype->type_name()) {
|
||||
TINT_ASSERT(subtype_);
|
||||
}
|
||||
|
||||
|
@ -40,14 +39,10 @@ std::string Alias::type_name() const {
|
|||
return type_name_;
|
||||
}
|
||||
|
||||
std::string Alias::FriendlyName(const SymbolTable& symbols) const {
|
||||
return symbols.NameFor(symbol_);
|
||||
}
|
||||
|
||||
Alias* Alias::Clone(CloneContext* ctx) const {
|
||||
// Clone arguments outside of create() call to have deterministic ordering
|
||||
auto src = ctx->Clone(source());
|
||||
auto sym = ctx->Clone(symbol());
|
||||
auto sym = ctx->Clone(name());
|
||||
auto* ty = ctx->Clone(type());
|
||||
return ctx->dst->create<Alias>(src, sym, ty);
|
||||
}
|
||||
|
|
|
@ -17,48 +17,43 @@
|
|||
|
||||
#include <string>
|
||||
|
||||
#include "src/ast/type.h"
|
||||
#include "src/ast/named_type.h"
|
||||
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
|
||||
/// A type alias type. Holds a name and pointer to another type.
|
||||
class Alias : public Castable<Alias, Type> {
|
||||
class Alias : public Castable<Alias, NamedType> {
|
||||
public:
|
||||
/// Constructor
|
||||
/// @param program_id the identifier of the program that owns this node
|
||||
/// @param source the source of this node
|
||||
/// @param sym the symbol for the alias
|
||||
/// @param name the symbol for the alias
|
||||
/// @param subtype the alias'd type
|
||||
Alias(ProgramID program_id,
|
||||
const Source& source,
|
||||
const Symbol& sym,
|
||||
const Symbol& name,
|
||||
Type* subtype);
|
||||
/// Move constructor
|
||||
Alias(Alias&&);
|
||||
/// Destructor
|
||||
~Alias() override;
|
||||
|
||||
/// [DEPRECATED] use name()
|
||||
/// @returns the alias symbol
|
||||
Symbol symbol() const { return symbol_; }
|
||||
Symbol symbol() const { return name(); }
|
||||
/// @returns the alias type
|
||||
Type* type() const { return subtype_; }
|
||||
|
||||
/// @returns the type_name for this type
|
||||
std::string type_name() const override;
|
||||
|
||||
/// @param symbols the program's symbol table
|
||||
/// @returns the name for this type that closely resembles how it would be
|
||||
/// declared in WGSL.
|
||||
std::string FriendlyName(const SymbolTable& symbols) const override;
|
||||
|
||||
/// Clones this type and all transitive types using the `CloneContext` `ctx`.
|
||||
/// @param ctx the clone context
|
||||
/// @return the newly cloned type
|
||||
Alias* Clone(CloneContext* ctx) const override;
|
||||
|
||||
private:
|
||||
Symbol const symbol_;
|
||||
Type* const subtype_;
|
||||
std::string const type_name_;
|
||||
};
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
// Copyright 2021 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.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "src/ast/named_type.h"
|
||||
|
||||
#include "src/program_builder.h"
|
||||
|
||||
TINT_INSTANTIATE_TYPEINFO(tint::ast::NamedType);
|
||||
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
|
||||
NamedType::NamedType(ProgramID program_id, const Source& source, Symbol name)
|
||||
: Base(program_id, source), name_(name) {
|
||||
TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(name, program_id);
|
||||
}
|
||||
|
||||
NamedType::NamedType(NamedType&&) = default;
|
||||
|
||||
NamedType::~NamedType() = default;
|
||||
|
||||
std::string NamedType::FriendlyName(const SymbolTable& symbols) const {
|
||||
return symbols.NameFor(name());
|
||||
}
|
||||
|
||||
} // namespace ast
|
||||
} // namespace tint
|
|
@ -0,0 +1,56 @@
|
|||
// Copyright 2021 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.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef SRC_AST_NAMED_TYPE_H_
|
||||
#define SRC_AST_NAMED_TYPE_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "src/ast/type.h"
|
||||
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
|
||||
/// The base class for user declared, named types.
|
||||
class NamedType : public Castable<NamedType, Type> {
|
||||
public:
|
||||
/// Create a new struct statement
|
||||
/// @param program_id the identifier of the program that owns this node
|
||||
/// @param source The input source for the import statement
|
||||
/// @param name The name of the structure
|
||||
NamedType(ProgramID program_id, const Source& source, Symbol name);
|
||||
/// Move constructor
|
||||
NamedType(NamedType&&);
|
||||
|
||||
~NamedType() override;
|
||||
|
||||
/// @returns the name of the structure
|
||||
Symbol name() const { return name_; }
|
||||
|
||||
/// @param symbols the program's symbol table
|
||||
/// @returns the name for this type that closely resembles how it would be
|
||||
/// declared in WGSL.
|
||||
std::string FriendlyName(const SymbolTable& symbols) const override;
|
||||
|
||||
private:
|
||||
NamedType(const NamedType&) = delete;
|
||||
NamedType& operator=(const NamedType&) = delete;
|
||||
|
||||
Symbol const name_;
|
||||
};
|
||||
|
||||
} // namespace ast
|
||||
} // namespace tint
|
||||
|
||||
#endif // SRC_AST_NAMED_TYPE_H_
|
|
@ -29,8 +29,7 @@ Struct::Struct(ProgramID program_id,
|
|||
Symbol name,
|
||||
StructMemberList members,
|
||||
DecorationList decorations)
|
||||
: Base(program_id, source),
|
||||
name_(name),
|
||||
: Base(program_id, source, name),
|
||||
members_(std::move(members)),
|
||||
decorations_(std::move(decorations)) {
|
||||
for (auto* mem : members_) {
|
||||
|
@ -66,7 +65,19 @@ Struct* Struct::Clone(CloneContext* ctx) const {
|
|||
auto n = ctx->Clone(name());
|
||||
auto mem = ctx->Clone(members());
|
||||
auto decos = ctx->Clone(decorations());
|
||||
return ctx->dst->create<Struct>(src, n, mem, decos);
|
||||
auto* out = ctx->dst->create<Struct>(src, n, mem, decos);
|
||||
|
||||
// HACK(crbug.com/tint/724): AST nodes do not derive from ShareableCloneable,
|
||||
// and so each call to Clone() produces a new copy of the node. However,
|
||||
// during the type system migration of tint:724, we have the situation where
|
||||
// we have diamonds pointers to the ast::Struct - one from the ast::Module and
|
||||
// one from the sem::StructType. This is a hack to make ast::Struct act like
|
||||
// it derives from ShareableCloneable.
|
||||
// This should be removed (possibly along with ShareableCloneable) once
|
||||
// tint:724 is complete.
|
||||
ctx->Replace(this, out);
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
void Struct::to_str(const sem::Info& sem,
|
||||
|
@ -90,9 +101,5 @@ std::string Struct::type_name() const {
|
|||
return "__struct_" + name().to_str();
|
||||
}
|
||||
|
||||
std::string Struct::FriendlyName(const SymbolTable& symbols) const {
|
||||
return symbols.NameFor(name());
|
||||
}
|
||||
|
||||
} // namespace ast
|
||||
} // namespace tint
|
||||
|
|
|
@ -19,14 +19,14 @@
|
|||
#include <utility>
|
||||
|
||||
#include "src/ast/decoration.h"
|
||||
#include "src/ast/named_type.h"
|
||||
#include "src/ast/struct_member.h"
|
||||
#include "src/ast/type.h"
|
||||
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
|
||||
/// A struct statement.
|
||||
class Struct : public Castable<Struct, Type> {
|
||||
class Struct : public Castable<Struct, NamedType> {
|
||||
public:
|
||||
/// Create a new struct statement
|
||||
/// @param program_id the identifier of the program that owns this node
|
||||
|
@ -44,9 +44,6 @@ class Struct : public Castable<Struct, Type> {
|
|||
|
||||
~Struct() override;
|
||||
|
||||
/// @returns the name of the structure
|
||||
Symbol name() const { return name_; }
|
||||
|
||||
/// @returns the struct decorations
|
||||
const DecorationList& decorations() const { return decorations_; }
|
||||
|
||||
|
@ -78,15 +75,9 @@ class Struct : public Castable<Struct, Type> {
|
|||
/// @returns the name for the type
|
||||
std::string type_name() const override;
|
||||
|
||||
/// @param symbols the program's symbol table
|
||||
/// @returns the name for this type that closely resembles how it would be
|
||||
/// declared in WGSL.
|
||||
std::string FriendlyName(const SymbolTable& symbols) const override;
|
||||
|
||||
private:
|
||||
Struct(const Struct&) = delete;
|
||||
|
||||
Symbol const name_;
|
||||
StructMemberList const members_;
|
||||
DecorationList const decorations_;
|
||||
};
|
||||
|
|
|
@ -107,7 +107,7 @@ TEST_F(AstStructTest, Assert_DifferentProgramID_StructMember) {
|
|||
{
|
||||
ProgramBuilder b1;
|
||||
ProgramBuilder b2;
|
||||
b1.create<Struct>(b2.Sym("S"),
|
||||
b1.create<Struct>(b1.Sym("S"),
|
||||
StructMemberList{b2.Member("a", b2.ty.i32())},
|
||||
DecorationList{});
|
||||
},
|
||||
|
|
|
@ -40,7 +40,7 @@ TEST_F(ParserImplTest, TypeDecl_ParsesType) {
|
|||
TEST_F(ParserImplTest, TypeDecl_ParsesStruct_Ident) {
|
||||
auto p = parser("type a = B");
|
||||
|
||||
auto str = Structure(p->builder().Symbols().Register("B"), {});
|
||||
auto str = p->builder().Structure(p->builder().Symbols().Register("B"), {});
|
||||
p->register_constructed("B", str);
|
||||
|
||||
auto t = p->type_alias();
|
||||
|
|
Loading…
Reference in New Issue