mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
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).
67 lines
2.0 KiB
C++
67 lines
2.0 KiB
C++
// 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 "common/check.h"
|
|
#include "explorer/ast/declaration.h"
|
|
|
|
namespace Carbon {
|
|
NamedElement::NamedElement(Nonnull<const Declaration*> declaration)
|
|
: Element(ElementKind::NamedElement), element_(declaration) {}
|
|
|
|
NamedElement::NamedElement(Nonnull<const NamedValue*> struct_member)
|
|
: Element(ElementKind::NamedElement), element_(struct_member) {}
|
|
|
|
auto NamedElement::IsNamed(std::string_view name) const -> bool {
|
|
return this->name() == name;
|
|
}
|
|
|
|
auto NamedElement::name() const -> std::string_view {
|
|
if (const auto* decl = element_.dyn_cast<const Declaration*>()) {
|
|
return GetName(*decl).value();
|
|
} else {
|
|
const auto* named_value = element_.dyn_cast<const NamedValue*>();
|
|
return named_value->name;
|
|
}
|
|
}
|
|
|
|
auto NamedElement::type() const -> const Value& {
|
|
if (const auto* decl = element_.dyn_cast<const Declaration*>()) {
|
|
return decl->static_type();
|
|
} else {
|
|
const auto* named_value = element_.dyn_cast<const NamedValue*>();
|
|
return *named_value->value;
|
|
}
|
|
}
|
|
|
|
auto NamedElement::declaration() const
|
|
-> std::optional<Nonnull<const Declaration*>> {
|
|
if (const auto* decl = element_.dyn_cast<const Declaration*>()) {
|
|
return decl;
|
|
}
|
|
return std::nullopt;
|
|
}
|
|
|
|
void NamedElement::Print(llvm::raw_ostream& out) const { out << name(); }
|
|
|
|
// Prints the Element
|
|
void PositionalElement::Print(llvm::raw_ostream& out) const {
|
|
out << "element #" << index_;
|
|
}
|
|
|
|
// Return whether the element's name matches `name`.
|
|
auto PositionalElement::IsNamed(std::string_view /*name*/) const -> bool {
|
|
return false;
|
|
}
|
|
|
|
void BaseElement::Print(llvm::raw_ostream& out) const { out << "base class"; }
|
|
|
|
// Return whether the element's name matches `name`.
|
|
auto BaseElement::IsNamed(std::string_view /*name*/) const -> bool {
|
|
return false;
|
|
}
|
|
|
|
} // namespace Carbon
|