Cleanup IdentifierExpression API.

This CL renames the parts of the identifier to segments and then adds a
path() and name() methods to get the respective parts of the identifier.

Change-Id: I1a365df7cb83ac0dd3d6be9ec6ccb83bfc73dd91
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/19762
Reviewed-by: Ryan Harrison <rharrison@chromium.org>
This commit is contained in:
dan sinclair 2020-04-20 14:21:00 +00:00 committed by dan sinclair
parent b950e80a59
commit 50714622b4
20 changed files with 117 additions and 146 deletions

View File

@ -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<std::string> name)
: Expression(), name_(std::move(name)) {}
IdentifierExpression::IdentifierExpression(std::vector<std::string> segments)
: Expression(), segments_(std::move(segments)) {}
IdentifierExpression::IdentifierExpression(const Source& source,
std::vector<std::string> name)
: Expression(source), name_(std::move(name)) {}
std::vector<std::string> 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;
}

View File

@ -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<std::string> name);
/// @param segments the name segments
explicit IdentifierExpression(std::vector<std::string> segments);
/// Constructor
/// @param source the identifier expression source
/// @param name the name
IdentifierExpression(const Source& source, std::vector<std::string> name);
/// @param segments the name segments
IdentifierExpression(const Source& source, std::vector<std::string> segments);
/// Move constructor
IdentifierExpression(IdentifierExpression&&);
~IdentifierExpression() override;
/// Sets the name
/// @param name the name
void set_name(std::vector<std::string> name) { name_ = std::move(name); }
/// Sets the name segments
/// @param segments the name segments
void set_segments(std::vector<std::string> segments) {
segments_ = std::move(segments);
}
/// @returns the name
std::vector<std::string> name() const { return name_; }
std::vector<std::string> 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<std::string> name_;
std::vector<std::string> segments_;
};
} // namespace ast

View File

@ -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);

View File

@ -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());

View File

@ -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());

View File

@ -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) {

View File

@ -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());

View File

@ -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());

View File

@ -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());

View File

@ -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());

View File

@ -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());

View File

@ -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());

View File

@ -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) {

View File

@ -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) {

View File

@ -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());

View File

@ -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());

View File

@ -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) {

View File

@ -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<ast::type::VectorType>(
vec->type(), expr->member()->name()[0].size())));
vec->type(), expr->member()->name().size())));
return true;
}

View File

@ -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;
}

View File

@ -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;
}