mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-25 12:10:10 +01:00
`Pattern` is intended to pilot some changes I would like to apply to all our sum types: - The alternatives are expressed as derived classes rather than members of a `std::variant`. - The alternatives are classes in the [style guide sense](https://google.github.io/styleguide/cppguide.html#Structs_vs._Classes), meaning they can have invariants, but can't have public data members. - Creating an object is expressed using a constructor rather than a factory function. - Accessing an alternative is expressed as a cast (using LLVM's RTTI system) rather than `std::get` or a `Get` method. Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
31 lines
804 B
C++
31 lines
804 B
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 "executable_semantics/ast/member.h"
|
|
|
|
namespace Carbon {
|
|
|
|
auto Member::MakeFieldMember(int line_num, const BindingPattern* binding)
|
|
-> Member* {
|
|
auto m = new Member();
|
|
m->line_num = line_num;
|
|
m->value = FieldMember({.binding = binding});
|
|
return m;
|
|
}
|
|
|
|
auto Member::GetFieldMember() const -> const FieldMember& {
|
|
return std::get<FieldMember>(value);
|
|
}
|
|
|
|
void Member::Print(llvm::raw_ostream& out) const {
|
|
switch (tag()) {
|
|
case MemberKind::FieldMember:
|
|
const auto& field = GetFieldMember();
|
|
out << "var " << field.binding << ";\n";
|
|
break;
|
|
}
|
|
}
|
|
|
|
} // namespace Carbon
|