// 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 declaration) : Element(ElementKind::NamedElement), element_(declaration) {} NamedElement::NamedElement(Nonnull 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()) { return GetName(*decl).value(); } else { const auto* named_value = cast(element_); return named_value->name; } } auto NamedElement::type() const -> const Value& { if (const auto* decl = element_.dyn_cast()) { return decl->static_type(); } else { const auto* named_value = cast(element_); return *named_value->value; } } auto NamedElement::declaration() const -> std::optional> { if (const auto* decl = element_.dyn_cast()) { return decl; } return std::nullopt; } auto NamedElement::struct_member() const -> std::optional> { if (const auto* member = element_.dyn_cast()) { return member; } 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