Files
carbon-lang/explorer/ast/element.h
T
Adrien Leravat 563768c6d3 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).
2022-12-09 16:09:50 -08:00

139 lines
3.8 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
#ifndef CARBON_EXPLORER_AST_ELEMENT_H_
#define CARBON_EXPLORER_AST_ELEMENT_H_
#include <optional>
#include <string>
#include <string_view>
#include "explorer/ast/ast_rtti.h"
#include "explorer/common/nonnull.h"
#include "llvm/ADT/PointerUnion.h"
namespace Carbon {
class Declaration;
class Value;
// A NamedValue represents a value with a name, such as a single struct field.
struct NamedValue {
// The field name.
std::string name;
// The field's value.
Nonnull<const Value*> value;
};
// A generic member of a type.
//
// This is can be a named, positional or other type of member.
class Element {
protected:
explicit Element(ElementKind kind) : kind_(kind) {}
public:
virtual ~Element() = default;
// Prints the Member
virtual void Print(llvm::raw_ostream& out) const = 0;
// Return whether the member's name matches `name`.
virtual auto IsNamed(std::string_view name) const -> bool = 0;
// Returns the enumerator corresponding to the most-derived type of this
// object.
auto kind() const -> ElementKind { return kind_; }
// The declared type of the member, which might include type variables.
virtual auto type() const -> const Value& = 0;
private:
const ElementKind kind_;
};
// A named element of a type.
//
// This is either a declared member of a class, interface, or similar, or a
// member of a struct with no declaration.
class NamedElement : public Element {
public:
explicit NamedElement(Nonnull<const Declaration*> declaration);
explicit NamedElement(Nonnull<const NamedValue*> struct_member);
// Prints the element's name
void Print(llvm::raw_ostream& out) const override;
auto IsNamed(std::string_view name) const -> bool override;
static auto classof(const Element* member) -> bool {
return InheritsFromNamedElement(member->kind());
}
auto type() const -> const Value& override;
// The name of the member.
auto name() const -> std::string_view;
// A declaration of the member, if any exists.
auto declaration() const -> std::optional<Nonnull<const Declaration*>>;
private:
const llvm::PointerUnion<Nonnull<const Declaration*>,
Nonnull<const NamedValue*>>
element_;
};
// A positional element of a type.
//
// This is a positional tuple element, or other index-based value.
class PositionalElement : public Element {
public:
explicit PositionalElement(int index, Nonnull<const Value*> type)
: Element(ElementKind::PositionalElement), index_(index), type_(type) {}
// Prints the element
void Print(llvm::raw_ostream& out) const override;
// Return whether the member's name matches `name`.
auto IsNamed(std::string_view name) const -> bool override;
static auto classof(const Element* member) -> bool {
return InheritsFromPositionalElement(member->kind());
}
auto index() const -> int { return index_; }
auto type() const -> const Value& override { return *type_; }
private:
const int index_;
const Nonnull<const Value*> type_;
};
// A base class object.
//
// This is the base class object of a class value.
class BaseElement : public Element {
public:
explicit BaseElement(Nonnull<const Value*> type)
: Element(ElementKind::BaseElement), type_(type) {}
// Prints the Member
void Print(llvm::raw_ostream& out) const override;
// Return whether the member's name matches `name`.
auto IsNamed(std::string_view name) const -> bool override;
static auto classof(const Element* member) -> bool {
return InheritsFromBaseElement(member->kind());
}
auto type() const -> const Value& override { return *type_; }
private:
const Nonnull<const Value*> type_;
};
} // namespace Carbon
#endif // CARBON_EXPLORER_AST_ELEMENT_H_