Explorer: split Member into dedicated classes (#2421)

Features:
* Split `Member` class into 3 dedicated classes covering named, positional, and a new "base class" element

Changes:
* Rename `Member` to `Element` to better reflect the variants covered
* Add `NamedElement`, `PositionalElement`, and `BaseElement` child classes for `Element`
* Split `GetMember` into 3 function variants based on available attributes (index, name, nothing).
* Add some unit tests to provide coverage of core features

Motivation:
This changeset splits Member into (currently 2) classes, as we see the need for more Member variants (base class access needed for #2378, possibly unnamed mixins, ...), which in addition to the current ones, also have significantly different attributes. This will allow supporting more Member types in the future cleanly.

Alternatives considered:
The alternative solution, "one class for positional, named & base class access", would expose unused or unavailable attributes depending on the Member actual type (`index()` only for positional, `name()` only for named, and neither for base class access).
This commit is contained in:
Adrien Leravat
2022-12-09 16:09:50 -08:00
committed by GitHub
parent dd2f7d732c
commit 563768c6d3
16 changed files with 507 additions and 307 deletions
+96
View File
@@ -0,0 +1,96 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "explorer/ast/element.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <optional>
#include "explorer/ast/bindings.h"
#include "explorer/ast/declaration.h"
#include "explorer/ast/expression.h"
#include "explorer/common/arena.h"
#include "explorer/interpreter/value.h"
#include "llvm/Support/Casting.h"
namespace Carbon::Testing {
namespace {
static auto FakeSourceLoc(int line_num) -> SourceLocation {
return SourceLocation("<test>", line_num);
}
class ElementTest : public ::testing::Test {
protected:
Arena arena;
};
TEST_F(ElementTest, NamedElementType) {
const auto src_loc = FakeSourceLoc(1);
VariableDeclaration decl{
src_loc,
arena.New<BindingPattern>(src_loc, "valuename",
arena.New<AutoPattern>(src_loc),
ValueCategory::Var),
std::nullopt, ValueCategory::Var};
const auto* static_type = arena.New<IntValue>(1);
decl.set_static_type(static_type);
NamedElement element_decl(&decl);
EXPECT_EQ(&element_decl.type(), static_type);
NamedElement named_val(
arena.New<NamedValue>(NamedValue{"valuename", static_type}));
EXPECT_EQ(&named_val.type(), static_type);
}
TEST_F(ElementTest, NamedElementDeclaration) {
const auto src_loc = FakeSourceLoc(1);
VariableDeclaration decl{
src_loc,
arena.New<BindingPattern>(src_loc, "valuename",
arena.New<AutoPattern>(src_loc),
ValueCategory::Var),
std::nullopt, ValueCategory::Var};
const auto* static_type = arena.New<IntValue>(1);
NamedElement element_decl(&decl);
EXPECT_TRUE(element_decl.declaration());
NamedElement named_val(
arena.New<NamedValue>(NamedValue{"valuename", static_type}));
EXPECT_FALSE(named_val.declaration());
}
TEST_F(ElementTest, NamedElementIsNamed) {
const auto src_loc = FakeSourceLoc(1);
VariableDeclaration decl{
src_loc,
arena.New<BindingPattern>(src_loc, "valuename",
arena.New<AutoPattern>(src_loc),
ValueCategory::Var),
std::nullopt, ValueCategory::Var};
NamedElement member_decl(&decl);
EXPECT_TRUE(member_decl.IsNamed("valuename"));
EXPECT_FALSE(member_decl.IsNamed("anything"));
NamedElement named_val(
arena.New<NamedValue>(NamedValue{"valuename", arena.New<IntValue>(1)}));
EXPECT_TRUE(named_val.IsNamed("valuename"));
EXPECT_FALSE(named_val.IsNamed("anything"));
}
TEST_F(ElementTest, PositionalElementIsNamed) {
PositionalElement element(1, arena.New<IntValue>(1));
EXPECT_FALSE(element.IsNamed("anything"));
}
TEST_F(ElementTest, BaseElementIsNamed) {
BaseElement element(arena.New<IntValue>(1));
EXPECT_FALSE(element.IsNamed("anything"));
}
} // namespace
} // namespace Carbon::Testing