[ast] Add constant id helper.

This CL adds constant id helper methods to the decorated variable class.

Bug: tint:150
Change-Id: Icfdc516f37d4f2680d2b990879bff722085cf732
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/29082
Reviewed-by: Ryan Harrison <rharrison@chromium.org>
Commit-Queue: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
dan sinclair 2020-09-29 20:00:00 +00:00 committed by Commit Bot service account
parent b403cb50ab
commit 9caffa4491
3 changed files with 70 additions and 0 deletions

View File

@ -14,6 +14,10 @@
#include "src/ast/decorated_variable.h"
#include <cassert>
#include "src/ast/constant_id_decoration.h"
namespace tint {
namespace ast {
@ -44,6 +48,25 @@ bool DecoratedVariable::HasBuiltinDecoration() const {
return false;
}
bool DecoratedVariable::HasConstantIdDecoration() const {
for (const auto& deco : decorations_) {
if (deco->IsConstantId()) {
return true;
}
}
return false;
}
uint32_t DecoratedVariable::constant_id() const {
assert(HasConstantIdDecoration());
for (const auto& deco : decorations_) {
if (deco->IsConstantId()) {
return deco->AsConstantId()->value();
}
}
return 0;
}
bool DecoratedVariable::IsDecorated() const {
return true;
}

View File

@ -49,6 +49,12 @@ class DecoratedVariable : public Variable {
bool HasLocationDecoration() const;
/// @returns true if the deocrations include a BuiltinDecoration
bool HasBuiltinDecoration() const;
/// @returns true if the decorations include a ConstantIdDecoration
bool HasConstantIdDecoration() const;
/// @returns the constant_id value for the variable. Assumes that
/// |HasConstantIdDecoration| has been called first.
uint32_t constant_id() const;
/// @returns true if this is a decorated variable
bool IsDecorated() const override;

View File

@ -16,7 +16,10 @@
#include "gtest/gtest.h"
#include "src/ast/binding_decoration.h"
#include "src/ast/builtin_decoration.h"
#include "src/ast/constant_id_decoration.h"
#include "src/ast/identifier_expression.h"
#include "src/ast/location_decoration.h"
#include "src/ast/set_decoration.h"
#include "src/ast/type/f32_type.h"
#include "src/ast/type/i32_type.h"
@ -54,6 +57,44 @@ TEST_F(DecoratedVariableTest, CreationWithSource) {
EXPECT_EQ(dv.column(), 4u);
}
TEST_F(DecoratedVariableTest, NoDecorations) {
type::I32Type t;
auto var = std::make_unique<Variable>("my_var", StorageClass::kFunction, &t);
DecoratedVariable dv(std::move(var));
EXPECT_FALSE(dv.HasLocationDecoration());
EXPECT_FALSE(dv.HasBuiltinDecoration());
EXPECT_FALSE(dv.HasConstantIdDecoration());
}
TEST_F(DecoratedVariableTest, WithDecorations) {
type::F32Type t;
auto var = std::make_unique<Variable>("my_var", StorageClass::kFunction, &t);
DecoratedVariable dv(std::move(var));
VariableDecorationList decos;
decos.push_back(std::make_unique<LocationDecoration>(1));
decos.push_back(std::make_unique<BuiltinDecoration>(ast::Builtin::kPosition));
decos.push_back(std::make_unique<ConstantIdDecoration>(1200));
dv.set_decorations(std::move(decos));
EXPECT_TRUE(dv.HasLocationDecoration());
EXPECT_TRUE(dv.HasBuiltinDecoration());
EXPECT_TRUE(dv.HasConstantIdDecoration());
}
TEST_F(DecoratedVariableTest, ConstantId) {
type::F32Type t;
auto var = std::make_unique<Variable>("my_var", StorageClass::kFunction, &t);
DecoratedVariable dv(std::move(var));
VariableDecorationList decos;
decos.push_back(std::make_unique<ConstantIdDecoration>(1200));
dv.set_decorations(std::move(decos));
EXPECT_EQ(dv.constant_id(), 1200u);
}
TEST_F(DecoratedVariableTest, IsValid) {
type::I32Type t;
auto var = std::make_unique<Variable>("my_var", StorageClass::kNone, &t);