Drop support for named tuple fields (#886)

Rationale: Based on the status of #478 and #505, Carbon won't have this feature for a while, and it will be simpler not to support it on spec in the meantime.
This commit is contained in:
Geoff Romer
2021-10-15 13:19:57 -07:00
committed by GitHub
parent c7c653adba
commit bb28d37eed
22 changed files with 175 additions and 469 deletions
+6 -43
View File
@@ -26,25 +26,12 @@ namespace Carbon {
// either `Expression` or `Pattern`.
template <typename Term>
struct ParenContents {
struct Element {
std::optional<std::string> name;
Nonnull<Term*> term;
};
// If this object represents a single term, with no name and no trailing
// comma, this method returns that term. This typically means the parentheses
// can be interpreted as grouping.
// If this object represents a single term with no trailing comma, this
// method returns that term. This typically means the parentheses can be
// interpreted as grouping.
auto SingleTerm() const -> std::optional<Nonnull<Term*>>;
// Converts `elements` to std::vector<TupleElement>. TupleElement must
// have a constructor that takes a std::string and a Nonnull<Term*>.
//
// TODO: Find a way to deduce TupleElement from Term.
template <typename TupleElement>
auto TupleElements(SourceLocation source_loc) const
-> std::vector<TupleElement>;
std::vector<Element> elements;
std::vector<Nonnull<Term*>> elements;
bool has_trailing_comma;
};
@@ -52,37 +39,13 @@ struct ParenContents {
template <typename Term>
auto ParenContents<Term>::SingleTerm() const -> std::optional<Nonnull<Term*>> {
if (elements.size() == 1 && !elements.front().name.has_value() &&
!has_trailing_comma) {
return elements.front().term;
if (elements.size() == 1 && !has_trailing_comma) {
return elements.front();
} else {
return std::nullopt;
}
}
template <typename Term>
template <typename TupleElement>
auto ParenContents<Term>::TupleElements(SourceLocation source_loc) const
-> std::vector<TupleElement> {
std::vector<TupleElement> result;
int i = 0;
bool seen_named_member = false;
for (auto element : elements) {
if (element.name.has_value()) {
seen_named_member = true;
result.push_back(TupleElement(*element.name, element.term));
} else {
if (seen_named_member) {
FATAL_PROGRAM_ERROR(source_loc)
<< "positional members must come before named members";
}
result.push_back(TupleElement(std::to_string(i), element.term));
}
++i;
}
return result;
}
} // namespace Carbon
#endif // EXECUTABLE_SEMANTICS_AST_PAREN_CONTENTS_H_