paren_contents.cpp 886 B

123456789101112131415161718192021222324252627
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "executable_semantics/syntax/paren_contents.h"
  5. namespace Carbon {
  6. const Expression* ParenContents::AsExpression(int line_number) const {
  7. if (fields_.size() == 1 && fields_.front().name == "" &&
  8. has_trailing_comma_ == HasTrailingComma::No) {
  9. return fields_.front().expression;
  10. } else {
  11. return AsTuple(line_number);
  12. }
  13. }
  14. const Expression* ParenContents::AsTuple(int line_number) const {
  15. auto vec =
  16. new std::vector<std::pair<std::string, const Carbon::Expression*>>();
  17. for (const FieldInitializer& initializer : fields_) {
  18. vec->push_back({initializer.name, initializer.expression});
  19. }
  20. return MakeTuple(line_number, vec);
  21. }
  22. } // namespace Carbon