diff --git a/src/ast/identifier_expression.cc b/src/ast/identifier_expression.cc index 25b0602157..7adb6bc972 100644 --- a/src/ast/identifier_expression.cc +++ b/src/ast/identifier_expression.cc @@ -18,33 +18,49 @@ namespace tint { namespace ast { IdentifierExpression::IdentifierExpression(const std::string& name) - : Expression(), name_({name}) {} + : Expression(), segments_({name}) {} IdentifierExpression::IdentifierExpression(const Source& source, const std::string& name) - : Expression(source), name_({name}) {} + : Expression(source), segments_({name}) {} -IdentifierExpression::IdentifierExpression(std::vector name) - : Expression(), name_(std::move(name)) {} +IdentifierExpression::IdentifierExpression(std::vector segments) + : Expression(), segments_(std::move(segments)) {} IdentifierExpression::IdentifierExpression(const Source& source, - std::vector name) - : Expression(source), name_(std::move(name)) {} + std::vector segments) + : Expression(source), segments_(std::move(segments)) {} IdentifierExpression::IdentifierExpression(IdentifierExpression&&) = default; IdentifierExpression::~IdentifierExpression() = default; +std::string IdentifierExpression::path() const { + if (segments_.empty() || segments_.size() < 2) { + return ""; + } + + std::string path = ""; + // We skip the last segment as that's the name, not part of the path + for (size_t i = 0; i < segments_.size() - 1; ++i) { + if (i > 0) { + path += "::"; + } + path += segments_[i]; + } + return path; +} + bool IdentifierExpression::IsIdentifier() const { return true; } bool IdentifierExpression::IsValid() const { - if (name_.size() == 0) + if (segments_.size() == 0) return false; - for (const auto& name : name_) { - if (name.size() == 0) + for (const auto& segment : segments_) { + if (segment.size() == 0) return false; } return true; @@ -53,14 +69,10 @@ bool IdentifierExpression::IsValid() const { void IdentifierExpression::to_str(std::ostream& out, size_t indent) const { make_indent(out, indent); out << "Identifier{"; - bool first = true; - for (const auto& name : name_) { - if (!first) - out << "::"; - - first = false; - out << name; + if (has_path()) { + out << path() << "::"; } + out << name(); out << "}" << std::endl; } diff --git a/src/ast/identifier_expression.h b/src/ast/identifier_expression.h index e6eee06f23..e45cd8fd2d 100644 --- a/src/ast/identifier_expression.h +++ b/src/ast/identifier_expression.h @@ -35,21 +35,30 @@ class IdentifierExpression : public Expression { /// @param name the name IdentifierExpression(const Source& source, const std::string& name); /// Constructor - /// @param name the name - explicit IdentifierExpression(std::vector name); + /// @param segments the name segments + explicit IdentifierExpression(std::vector segments); /// Constructor /// @param source the identifier expression source - /// @param name the name - IdentifierExpression(const Source& source, std::vector name); + /// @param segments the name segments + IdentifierExpression(const Source& source, std::vector segments); /// Move constructor IdentifierExpression(IdentifierExpression&&); ~IdentifierExpression() override; - /// Sets the name - /// @param name the name - void set_name(std::vector name) { name_ = std::move(name); } + /// Sets the name segments + /// @param segments the name segments + void set_segments(std::vector segments) { + segments_ = std::move(segments); + } /// @returns the name - std::vector name() const { return name_; } + std::vector segments() const { return segments_; } + + /// @returns true if this identifier has a path component + bool has_path() const { return segments_.size() > 1; } + /// @returns the path part of the identifier or blank if no path + std::string path() const; + /// @returns the name part of the identifier + std::string name() const { return segments_.back(); } /// @returns true if this is an identifier expression bool IsIdentifier() const override; @@ -65,7 +74,7 @@ class IdentifierExpression : public Expression { private: IdentifierExpression(const IdentifierExpression&) = delete; - std::vector name_; + std::vector segments_; }; } // namespace ast diff --git a/src/ast/identifier_expression_test.cc b/src/ast/identifier_expression_test.cc index 44eeb9f44c..0886953f9e 100644 --- a/src/ast/identifier_expression_test.cc +++ b/src/ast/identifier_expression_test.cc @@ -24,16 +24,20 @@ using IdentifierExpressionTest = testing::Test; TEST_F(IdentifierExpressionTest, Creation) { IdentifierExpression i("ident"); - ASSERT_EQ(i.name().size(), 1u); - EXPECT_EQ(i.name()[0], "ident"); + ASSERT_EQ(i.segments().size(), 1u); + EXPECT_EQ(i.segments()[0], "ident"); + EXPECT_EQ(i.path(), ""); + EXPECT_EQ(i.name(), "ident"); } TEST_F(IdentifierExpressionTest, Creation_WithSource) { IdentifierExpression i(Source{20, 2}, {"ns1", "ns2", "ident"}); - ASSERT_EQ(i.name().size(), 3u); - EXPECT_EQ(i.name()[0], "ns1"); - EXPECT_EQ(i.name()[1], "ns2"); - EXPECT_EQ(i.name()[2], "ident"); + ASSERT_EQ(i.segments().size(), 3u); + EXPECT_EQ(i.segments()[0], "ns1"); + EXPECT_EQ(i.segments()[1], "ns2"); + EXPECT_EQ(i.segments()[2], "ident"); + EXPECT_EQ(i.path(), "ns1::ns2"); + EXPECT_EQ(i.name(), "ident"); auto src = i.source(); EXPECT_EQ(src.line, 20u); diff --git a/src/reader/wgsl/parser_impl_additive_expression_test.cc b/src/reader/wgsl/parser_impl_additive_expression_test.cc index 25c772abc7..a80ab600a9 100644 --- a/src/reader/wgsl/parser_impl_additive_expression_test.cc +++ b/src/reader/wgsl/parser_impl_additive_expression_test.cc @@ -37,8 +37,7 @@ TEST_F(ParserImplTest, AdditiveExpression_Parses_Plus) { ASSERT_TRUE(rel->lhs()->IsIdentifier()); auto* ident = rel->lhs()->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "a"); + EXPECT_EQ(ident->name(), "a"); ASSERT_TRUE(rel->rhs()->IsConstructor()); ASSERT_TRUE(rel->rhs()->AsConstructor()->IsScalarConstructor()); @@ -59,8 +58,7 @@ TEST_F(ParserImplTest, AdditiveExpression_Parses_Minus) { ASSERT_TRUE(rel->lhs()->IsIdentifier()); auto* ident = rel->lhs()->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "a"); + EXPECT_EQ(ident->name(), "a"); ASSERT_TRUE(rel->rhs()->IsConstructor()); ASSERT_TRUE(rel->rhs()->AsConstructor()->IsScalarConstructor()); diff --git a/src/reader/wgsl/parser_impl_and_expression_test.cc b/src/reader/wgsl/parser_impl_and_expression_test.cc index d708267a44..6bd1039383 100644 --- a/src/reader/wgsl/parser_impl_and_expression_test.cc +++ b/src/reader/wgsl/parser_impl_and_expression_test.cc @@ -37,8 +37,7 @@ TEST_F(ParserImplTest, AndExpression_Parses) { ASSERT_TRUE(rel->lhs()->IsIdentifier()); auto* ident = rel->lhs()->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "a"); + EXPECT_EQ(ident->name(), "a"); ASSERT_TRUE(rel->rhs()->IsConstructor()); ASSERT_TRUE(rel->rhs()->AsConstructor()->IsScalarConstructor()); diff --git a/src/reader/wgsl/parser_impl_assignment_stmt_test.cc b/src/reader/wgsl/parser_impl_assignment_stmt_test.cc index 581b62b43a..9624223877 100644 --- a/src/reader/wgsl/parser_impl_assignment_stmt_test.cc +++ b/src/reader/wgsl/parser_impl_assignment_stmt_test.cc @@ -40,8 +40,7 @@ TEST_F(ParserImplTest, AssignmentStmt_Parses_ToVariable) { ASSERT_TRUE(e->lhs()->IsIdentifier()); auto* ident = e->lhs()->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "a"); + EXPECT_EQ(ident->name(), "a"); ASSERT_TRUE(e->rhs()->IsConstructor()); ASSERT_TRUE(e->rhs()->AsConstructor()->IsScalarConstructor()); @@ -74,8 +73,7 @@ TEST_F(ParserImplTest, AssignmentStmt_Parses_ToMember) { ASSERT_TRUE(mem->member()->IsIdentifier()); auto* ident = mem->member()->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "d"); + EXPECT_EQ(ident->name(), "d"); ASSERT_TRUE(mem->structure()->IsArrayAccessor()); auto* ary = mem->structure()->AsArrayAccessor(); @@ -91,21 +89,18 @@ TEST_F(ParserImplTest, AssignmentStmt_Parses_ToMember) { mem = ary->array()->AsMemberAccessor(); ASSERT_TRUE(mem->member()->IsIdentifier()); ident = mem->member()->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "c"); + EXPECT_EQ(ident->name(), "c"); ASSERT_TRUE(mem->structure()->IsMemberAccessor()); mem = mem->structure()->AsMemberAccessor(); ASSERT_TRUE(mem->structure()->IsIdentifier()); ident = mem->structure()->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "a"); + EXPECT_EQ(ident->name(), "a"); ASSERT_TRUE(mem->member()->IsIdentifier()); ident = mem->member()->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "b"); + EXPECT_EQ(ident->name(), "b"); } TEST_F(ParserImplTest, AssignmentStmt_MissingEqual) { diff --git a/src/reader/wgsl/parser_impl_equality_expression_test.cc b/src/reader/wgsl/parser_impl_equality_expression_test.cc index c427e6c83a..c3fefe12e1 100644 --- a/src/reader/wgsl/parser_impl_equality_expression_test.cc +++ b/src/reader/wgsl/parser_impl_equality_expression_test.cc @@ -37,8 +37,7 @@ TEST_F(ParserImplTest, EqualityExpression_Parses_Equal) { ASSERT_TRUE(rel->lhs()->IsIdentifier()); auto* ident = rel->lhs()->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "a"); + EXPECT_EQ(ident->name(), "a"); ASSERT_TRUE(rel->rhs()->IsConstructor()); ASSERT_TRUE(rel->rhs()->AsConstructor()->IsScalarConstructor()); @@ -59,8 +58,7 @@ TEST_F(ParserImplTest, EqualityExpression_Parses_NotEqual) { ASSERT_TRUE(rel->lhs()->IsIdentifier()); auto* ident = rel->lhs()->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "a"); + EXPECT_EQ(ident->name(), "a"); ASSERT_TRUE(rel->rhs()->IsConstructor()); ASSERT_TRUE(rel->rhs()->AsConstructor()->IsScalarConstructor()); diff --git a/src/reader/wgsl/parser_impl_exclusive_or_expression_test.cc b/src/reader/wgsl/parser_impl_exclusive_or_expression_test.cc index 1e2d1203b3..54d9e4cbe4 100644 --- a/src/reader/wgsl/parser_impl_exclusive_or_expression_test.cc +++ b/src/reader/wgsl/parser_impl_exclusive_or_expression_test.cc @@ -37,8 +37,7 @@ TEST_F(ParserImplTest, ExclusiveOrExpression_Parses) { ASSERT_TRUE(rel->lhs()->IsIdentifier()); auto* ident = rel->lhs()->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "a"); + EXPECT_EQ(ident->name(), "a"); ASSERT_TRUE(rel->rhs()->IsConstructor()); ASSERT_TRUE(rel->rhs()->AsConstructor()->IsScalarConstructor()); diff --git a/src/reader/wgsl/parser_impl_inclusive_or_expression_test.cc b/src/reader/wgsl/parser_impl_inclusive_or_expression_test.cc index 3311e74992..e7451e77eb 100644 --- a/src/reader/wgsl/parser_impl_inclusive_or_expression_test.cc +++ b/src/reader/wgsl/parser_impl_inclusive_or_expression_test.cc @@ -37,8 +37,7 @@ TEST_F(ParserImplTest, InclusiveOrExpression_Parses) { ASSERT_TRUE(rel->lhs()->IsIdentifier()); auto* ident = rel->lhs()->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "a"); + EXPECT_EQ(ident->name(), "a"); ASSERT_TRUE(rel->rhs()->IsConstructor()); ASSERT_TRUE(rel->rhs()->AsConstructor()->IsScalarConstructor()); diff --git a/src/reader/wgsl/parser_impl_logical_and_expression_test.cc b/src/reader/wgsl/parser_impl_logical_and_expression_test.cc index 6434e0a4e1..03b102bcd2 100644 --- a/src/reader/wgsl/parser_impl_logical_and_expression_test.cc +++ b/src/reader/wgsl/parser_impl_logical_and_expression_test.cc @@ -37,8 +37,7 @@ TEST_F(ParserImplTest, LogicalAndExpression_Parses) { ASSERT_TRUE(rel->lhs()->IsIdentifier()); auto* ident = rel->lhs()->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "a"); + EXPECT_EQ(ident->name(), "a"); ASSERT_TRUE(rel->rhs()->IsConstructor()); ASSERT_TRUE(rel->rhs()->AsConstructor()->IsScalarConstructor()); diff --git a/src/reader/wgsl/parser_impl_logical_or_expression_test.cc b/src/reader/wgsl/parser_impl_logical_or_expression_test.cc index 63e1c64bf7..444e871471 100644 --- a/src/reader/wgsl/parser_impl_logical_or_expression_test.cc +++ b/src/reader/wgsl/parser_impl_logical_or_expression_test.cc @@ -37,8 +37,7 @@ TEST_F(ParserImplTest, LogicalOrExpression_Parses) { ASSERT_TRUE(rel->lhs()->IsIdentifier()); auto* ident = rel->lhs()->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "a"); + EXPECT_EQ(ident->name(), "a"); ASSERT_TRUE(rel->rhs()->IsConstructor()); ASSERT_TRUE(rel->rhs()->AsConstructor()->IsScalarConstructor()); diff --git a/src/reader/wgsl/parser_impl_multiplicative_expression_test.cc b/src/reader/wgsl/parser_impl_multiplicative_expression_test.cc index 22a9dfc1fa..3c6c1faaa2 100644 --- a/src/reader/wgsl/parser_impl_multiplicative_expression_test.cc +++ b/src/reader/wgsl/parser_impl_multiplicative_expression_test.cc @@ -37,8 +37,7 @@ TEST_F(ParserImplTest, MultiplicativeExpression_Parses_Multiply) { ASSERT_TRUE(rel->lhs()->IsIdentifier()); auto* ident = rel->lhs()->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "a"); + EXPECT_EQ(ident->name(), "a"); ASSERT_TRUE(rel->rhs()->IsConstructor()); ASSERT_TRUE(rel->rhs()->AsConstructor()->IsScalarConstructor()); @@ -59,8 +58,7 @@ TEST_F(ParserImplTest, MultiplicativeExpression_Parses_Divide) { ASSERT_TRUE(rel->lhs()->IsIdentifier()); auto* ident = rel->lhs()->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "a"); + EXPECT_EQ(ident->name(), "a"); ASSERT_TRUE(rel->rhs()->IsConstructor()); ASSERT_TRUE(rel->rhs()->AsConstructor()->IsScalarConstructor()); @@ -81,8 +79,7 @@ TEST_F(ParserImplTest, MultiplicativeExpression_Parses_Modulo) { ASSERT_TRUE(rel->lhs()->IsIdentifier()); auto* ident = rel->lhs()->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "a"); + EXPECT_EQ(ident->name(), "a"); ASSERT_TRUE(rel->rhs()->IsConstructor()); ASSERT_TRUE(rel->rhs()->AsConstructor()->IsScalarConstructor()); diff --git a/src/reader/wgsl/parser_impl_postfix_expression_test.cc b/src/reader/wgsl/parser_impl_postfix_expression_test.cc index 5b7be35c21..6f9ff514e3 100644 --- a/src/reader/wgsl/parser_impl_postfix_expression_test.cc +++ b/src/reader/wgsl/parser_impl_postfix_expression_test.cc @@ -41,8 +41,7 @@ TEST_F(ParserImplTest, PostfixExpression_Array_ConstantIndex) { ASSERT_TRUE(ary->array()->IsIdentifier()); auto* ident = ary->array()->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "a"); + EXPECT_EQ(ident->name(), "a"); ASSERT_TRUE(ary->idx_expr()->IsConstructor()); ASSERT_TRUE(ary->idx_expr()->AsConstructor()->IsScalarConstructor()); @@ -62,8 +61,7 @@ TEST_F(ParserImplTest, PostfixExpression_Array_ExpressionIndex) { ASSERT_TRUE(ary->array()->IsIdentifier()); auto* ident = ary->array()->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "a"); + EXPECT_EQ(ident->name(), "a"); ASSERT_TRUE(ary->idx_expr()->IsBinary()); } @@ -103,8 +101,7 @@ TEST_F(ParserImplTest, PostfixExpression_Call_Empty) { ASSERT_TRUE(c->func()->IsIdentifier()); auto* func = c->func()->AsIdentifier(); - ASSERT_EQ(func->name().size(), 1u); - EXPECT_EQ(func->name()[0], "a"); + EXPECT_EQ(func->name(), "a"); EXPECT_EQ(c->params().size(), 0u); } @@ -120,9 +117,8 @@ TEST_F(ParserImplTest, PostfixExpression_Call_WithArgs) { ASSERT_TRUE(c->func()->IsIdentifier()); auto* func = c->func()->AsIdentifier(); - ASSERT_EQ(func->name().size(), 2u); - EXPECT_EQ(func->name()[0], "std"); - EXPECT_EQ(func->name()[1], "test"); + EXPECT_EQ(func->path(), "std"); + EXPECT_EQ(func->name(), "test"); EXPECT_EQ(c->params().size(), 3u); EXPECT_TRUE(c->params()[0]->IsConstructor()); @@ -163,12 +159,10 @@ TEST_F(ParserImplTest, PostfixExpression_MemberAccessor) { auto* m = e->AsMemberAccessor(); ASSERT_TRUE(m->structure()->IsIdentifier()); - ASSERT_EQ(m->structure()->AsIdentifier()->name().size(), 1u); - EXPECT_EQ(m->structure()->AsIdentifier()->name()[0], "a"); + EXPECT_EQ(m->structure()->AsIdentifier()->name(), "a"); ASSERT_TRUE(m->member()->IsIdentifier()); - ASSERT_EQ(m->member()->AsIdentifier()->name().size(), 1u); - EXPECT_EQ(m->member()->AsIdentifier()->name()[0], "b"); + EXPECT_EQ(m->member()->AsIdentifier()->name(), "b"); } TEST_F(ParserImplTest, PostfixExpression_MemberAccesssor_InvalidIdent) { diff --git a/src/reader/wgsl/parser_impl_primary_expression_test.cc b/src/reader/wgsl/parser_impl_primary_expression_test.cc index 29f11a34bb..80f294050d 100644 --- a/src/reader/wgsl/parser_impl_primary_expression_test.cc +++ b/src/reader/wgsl/parser_impl_primary_expression_test.cc @@ -42,8 +42,7 @@ TEST_F(ParserImplTest, PrimaryExpression_Ident) { ASSERT_NE(e, nullptr); ASSERT_TRUE(e->IsIdentifier()); auto* ident = e->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "a"); + EXPECT_EQ(ident->name(), "a"); } TEST_F(ParserImplTest, PrimaryExpression_Ident_WithNamespace) { @@ -53,11 +52,8 @@ TEST_F(ParserImplTest, PrimaryExpression_Ident_WithNamespace) { ASSERT_NE(e, nullptr); ASSERT_TRUE(e->IsIdentifier()); auto* ident = e->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 4u); - EXPECT_EQ(ident->name()[0], "a"); - EXPECT_EQ(ident->name()[1], "b"); - EXPECT_EQ(ident->name()[2], "c"); - EXPECT_EQ(ident->name()[3], "d"); + EXPECT_EQ(ident->path(), "a::b::c"); + EXPECT_EQ(ident->name(), "d"); } TEST_F(ParserImplTest, PrimaryExpression_Ident_MissingIdent) { diff --git a/src/reader/wgsl/parser_impl_relational_expression_test.cc b/src/reader/wgsl/parser_impl_relational_expression_test.cc index 11519774fe..982f496398 100644 --- a/src/reader/wgsl/parser_impl_relational_expression_test.cc +++ b/src/reader/wgsl/parser_impl_relational_expression_test.cc @@ -37,8 +37,7 @@ TEST_F(ParserImplTest, RelationalExpression_Parses_LessThan) { ASSERT_TRUE(rel->lhs()->IsIdentifier()); auto* ident = rel->lhs()->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "a"); + EXPECT_EQ(ident->name(), "a"); ASSERT_TRUE(rel->rhs()->IsConstructor()); ASSERT_TRUE(rel->rhs()->AsConstructor()->IsScalarConstructor()); @@ -59,8 +58,7 @@ TEST_F(ParserImplTest, RelationalExpression_Parses_GreaterThan) { ASSERT_TRUE(rel->lhs()->IsIdentifier()); auto* ident = rel->lhs()->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "a"); + EXPECT_EQ(ident->name(), "a"); ASSERT_TRUE(rel->rhs()->IsConstructor()); ASSERT_TRUE(rel->rhs()->AsConstructor()->IsScalarConstructor()); @@ -81,8 +79,7 @@ TEST_F(ParserImplTest, RelationalExpression_Parses_LessThanEqual) { ASSERT_TRUE(rel->lhs()->IsIdentifier()); auto* ident = rel->lhs()->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "a"); + EXPECT_EQ(ident->name(), "a"); ASSERT_TRUE(rel->rhs()->IsConstructor()); ASSERT_TRUE(rel->rhs()->AsConstructor()->IsScalarConstructor()); @@ -103,8 +100,7 @@ TEST_F(ParserImplTest, RelationalExpression_Parses_GreaterThanEqual) { ASSERT_TRUE(rel->lhs()->IsIdentifier()); auto* ident = rel->lhs()->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "a"); + EXPECT_EQ(ident->name(), "a"); ASSERT_TRUE(rel->rhs()->IsConstructor()); ASSERT_TRUE(rel->rhs()->AsConstructor()->IsScalarConstructor()); diff --git a/src/reader/wgsl/parser_impl_shift_expression_test.cc b/src/reader/wgsl/parser_impl_shift_expression_test.cc index f6e83514d2..25b3fded67 100644 --- a/src/reader/wgsl/parser_impl_shift_expression_test.cc +++ b/src/reader/wgsl/parser_impl_shift_expression_test.cc @@ -37,8 +37,7 @@ TEST_F(ParserImplTest, ShiftExpression_Parses_ShiftLeft) { ASSERT_TRUE(rel->lhs()->IsIdentifier()); auto* ident = rel->lhs()->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "a"); + EXPECT_EQ(ident->name(), "a"); ASSERT_TRUE(rel->rhs()->IsConstructor()); ASSERT_TRUE(rel->rhs()->AsConstructor()->IsScalarConstructor()); @@ -59,8 +58,7 @@ TEST_F(ParserImplTest, ShiftExpression_Parses_ShiftRight) { ASSERT_TRUE(rel->lhs()->IsIdentifier()); auto* ident = rel->lhs()->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "a"); + EXPECT_EQ(ident->name(), "a"); ASSERT_TRUE(rel->rhs()->IsConstructor()); ASSERT_TRUE(rel->rhs()->AsConstructor()->IsScalarConstructor()); @@ -81,8 +79,7 @@ TEST_F(ParserImplTest, ShiftExpression_Parses_ShiftRightArith) { ASSERT_TRUE(rel->lhs()->IsIdentifier()); auto* ident = rel->lhs()->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "a"); + EXPECT_EQ(ident->name(), "a"); ASSERT_TRUE(rel->rhs()->IsConstructor()); ASSERT_TRUE(rel->rhs()->AsConstructor()->IsScalarConstructor()); diff --git a/src/reader/wgsl/parser_impl_unary_expression_test.cc b/src/reader/wgsl/parser_impl_unary_expression_test.cc index 5d1b578af4..a11bc78327 100644 --- a/src/reader/wgsl/parser_impl_unary_expression_test.cc +++ b/src/reader/wgsl/parser_impl_unary_expression_test.cc @@ -38,8 +38,7 @@ TEST_F(ParserImplTest, UnaryExpression_Postix) { auto* ary = e->AsArrayAccessor(); ASSERT_TRUE(ary->array()->IsIdentifier()); auto* ident = ary->array()->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "a"); + EXPECT_EQ(ident->name(), "a"); ASSERT_TRUE(ary->idx_expr()->IsConstructor()); ASSERT_TRUE(ary->idx_expr()->AsConstructor()->IsScalarConstructor()); @@ -112,8 +111,7 @@ TEST_F(ParserImplTest, UnaryExpression_Any) { ASSERT_EQ(u->params().size(), 1u); ASSERT_TRUE(u->params()[0]->IsIdentifier()); auto* ident = u->params()[0]->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "a"); + EXPECT_EQ(ident->name(), "a"); } TEST_F(ParserImplTest, UnaryExpression_Any_MissingParenLeft) { @@ -160,8 +158,7 @@ TEST_F(ParserImplTest, UnaryExpression_All) { ASSERT_EQ(u->params().size(), 1u); ASSERT_TRUE(u->params()[0]->IsIdentifier()); auto* ident = u->params()[0]->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "a"); + EXPECT_EQ(ident->name(), "a"); } TEST_F(ParserImplTest, UnaryExpression_All_MissingParenLeft) { @@ -208,8 +205,7 @@ TEST_F(ParserImplTest, UnaryExpression_IsNan) { ASSERT_EQ(u->params().size(), 1u); ASSERT_TRUE(u->params()[0]->IsIdentifier()); auto* ident = u->params()[0]->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "a"); + EXPECT_EQ(ident->name(), "a"); } TEST_F(ParserImplTest, UnaryExpression_IsNan_MissingParenLeft) { @@ -256,8 +252,7 @@ TEST_F(ParserImplTest, UnaryExpression_IsInf) { ASSERT_EQ(u->params().size(), 1u); ASSERT_TRUE(u->params()[0]->IsIdentifier()); auto* ident = u->params()[0]->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "a"); + EXPECT_EQ(ident->name(), "a"); } TEST_F(ParserImplTest, UnaryExpression_IsInf_MissingParenLeft) { @@ -304,8 +299,7 @@ TEST_F(ParserImplTest, UnaryExpression_IsFinite) { ASSERT_EQ(u->params().size(), 1u); ASSERT_TRUE(u->params()[0]->IsIdentifier()); auto* ident = u->params()[0]->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "a"); + EXPECT_EQ(ident->name(), "a"); } TEST_F(ParserImplTest, UnaryExpression_IsFinite_MissingParenLeft) { @@ -352,8 +346,7 @@ TEST_F(ParserImplTest, UnaryExpression_IsNormal) { ASSERT_EQ(u->params().size(), 1u); ASSERT_TRUE(u->params()[0]->IsIdentifier()); auto* ident = u->params()[0]->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "a"); + EXPECT_EQ(ident->name(), "a"); } TEST_F(ParserImplTest, UnaryExpression_IsNormal_MissingParenLeft) { @@ -400,13 +393,11 @@ TEST_F(ParserImplTest, UnaryExpression_Dot) { ASSERT_EQ(u->params().size(), 2u); ASSERT_TRUE(u->params()[0]->IsIdentifier()); auto* ident = u->params()[0]->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "a"); + EXPECT_EQ(ident->name(), "a"); ASSERT_TRUE(u->params()[1]->IsIdentifier()); ident = u->params()[1]->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "b"); + EXPECT_EQ(ident->name(), "b"); } TEST_F(ParserImplTest, UnaryExpression_Dot_MissingParenLeft) { @@ -477,13 +468,11 @@ TEST_F(ParserImplTest, UnaryExpression_OuterProduct) { ASSERT_EQ(u->params().size(), 2u); ASSERT_TRUE(u->params()[0]->IsIdentifier()); auto* ident = u->params()[0]->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "a"); + EXPECT_EQ(ident->name(), "a"); ASSERT_TRUE(u->params()[1]->IsIdentifier()); ident = u->params()[1]->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "b"); + EXPECT_EQ(ident->name(), "b"); } TEST_F(ParserImplTest, UnaryExpression_OuterProduct_MissingParenLeft) { @@ -556,8 +545,7 @@ TEST_F(ParserImplTest, UnaryExpression_Dpdx_NoModifier) { ASSERT_NE(deriv->param(), nullptr); ASSERT_TRUE(deriv->param()->IsIdentifier()); auto* ident = deriv->param()->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "a"); + EXPECT_EQ(ident->name(), "a"); } TEST_F(ParserImplTest, UnaryExpression_Dpdx_WithModifier) { @@ -574,8 +562,7 @@ TEST_F(ParserImplTest, UnaryExpression_Dpdx_WithModifier) { ASSERT_NE(deriv->param(), nullptr); ASSERT_TRUE(deriv->param()->IsIdentifier()); auto* ident = deriv->param()->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "a"); + EXPECT_EQ(ident->name(), "a"); } TEST_F(ParserImplTest, UnaryExpression_Dpdx_MissingLessThan) { @@ -656,8 +643,7 @@ TEST_F(ParserImplTest, UnaryExpression_Dpdy_NoModifier) { ASSERT_NE(deriv->param(), nullptr); ASSERT_TRUE(deriv->param()->IsIdentifier()); auto* ident = deriv->param()->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "a"); + EXPECT_EQ(ident->name(), "a"); } TEST_F(ParserImplTest, UnaryExpression_Dpdy_WithModifier) { @@ -674,8 +660,7 @@ TEST_F(ParserImplTest, UnaryExpression_Dpdy_WithModifier) { ASSERT_NE(deriv->param(), nullptr); ASSERT_TRUE(deriv->param()->IsIdentifier()); auto* ident = deriv->param()->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "a"); + EXPECT_EQ(ident->name(), "a"); } TEST_F(ParserImplTest, UnaryExpression_Dpdy_MissingLessThan) { @@ -756,8 +741,7 @@ TEST_F(ParserImplTest, UnaryExpression_Fwidth_NoModifier) { ASSERT_NE(deriv->param(), nullptr); ASSERT_TRUE(deriv->param()->IsIdentifier()); auto* ident = deriv->param()->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "a"); + EXPECT_EQ(ident->name(), "a"); } TEST_F(ParserImplTest, UnaryExpression_Fwidth_WithModifier) { @@ -774,8 +758,7 @@ TEST_F(ParserImplTest, UnaryExpression_Fwidth_WithModifier) { ASSERT_NE(deriv->param(), nullptr); ASSERT_TRUE(deriv->param()->IsIdentifier()); auto* ident = deriv->param()->AsIdentifier(); - ASSERT_EQ(ident->name().size(), 1u); - EXPECT_EQ(ident->name()[0], "a"); + EXPECT_EQ(ident->name(), "a"); } TEST_F(ParserImplTest, UnaryExpression_Fwidth_MissingLessThan) { diff --git a/src/type_determiner.cc b/src/type_determiner.cc index e3d2a8ac3c..4b8c44b688 100644 --- a/src/type_determiner.cc +++ b/src/type_determiner.cc @@ -313,13 +313,13 @@ bool TypeDeterminer::DetermineConstructor(ast::ConstructorExpression* expr) { } bool TypeDeterminer::DetermineIdentifier(ast::IdentifierExpression* expr) { - if (expr->name().size() > 1) { + if (expr->has_path()) { // TODO(dsinclair): Handle imports set_error(expr->source(), "imports not handled in type determination"); return false; } - auto name = expr->name()[0]; + auto name = expr->name(); ast::Variable* var; if (variable_stack_.get(name, &var)) { expr->set_result_type(var->type()); @@ -344,7 +344,7 @@ bool TypeDeterminer::DetermineMemberAccessor( auto* data_type = expr->structure()->result_type(); if (data_type->IsStruct()) { auto* strct = data_type->AsStruct()->impl(); - auto name = expr->member()->name()[0]; + auto name = expr->member()->name(); for (const auto& member : strct->members()) { if (member->name() != name) { @@ -366,7 +366,7 @@ bool TypeDeterminer::DetermineMemberAccessor( // is correct. expr->set_result_type( ctx_.type_mgr().Get(std::make_unique( - vec->type(), expr->member()->name()[0].size()))); + vec->type(), expr->member()->name().size()))); return true; } diff --git a/src/writer/spirv/builder.cc b/src/writer/spirv/builder.cc index 19fab5b84b..4b5f67b4d7 100644 --- a/src/writer/spirv/builder.cc +++ b/src/writer/spirv/builder.cc @@ -433,8 +433,8 @@ uint32_t Builder::GenerateIdentifierExpression( // TODO(dsinclair): handle names with namespaces in them ... uint32_t val = 0; - if (!scope_stack_.get(expr->name()[0], &val)) { - error_ = "unable to find name for identifier: " + expr->name()[0]; + if (!scope_stack_.get(expr->name(), &val)) { + error_ = "unable to find name for identifier: " + expr->name(); return 0; } diff --git a/src/writer/wgsl/generator_impl.cc b/src/writer/wgsl/generator_impl.cc index 1b99cb3e6b..4805d953d6 100644 --- a/src/writer/wgsl/generator_impl.cc +++ b/src/writer/wgsl/generator_impl.cc @@ -319,14 +319,11 @@ bool GeneratorImpl::EmitLiteral(ast::Literal* lit) { } bool GeneratorImpl::EmitIdentifier(ast::IdentifierExpression* expr) { - bool first = true; - for (const auto& part : expr->AsIdentifier()->name()) { - if (!first) { - out_ << "::"; - } - first = false; - out_ << part; + auto* ident = expr->AsIdentifier(); + if (ident->has_path()) { + out_ << ident->path() << "::"; } + out_ << ident->name(); return true; }