paren_contents.cpp 862 B

1234567891011121314151617181920212223242526
  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. 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. Expression* ParenContents::AsTuple(int line_number) const {
  15. auto vec = new std::vector<std::pair<std::string, Carbon::Expression*>>();
  16. for (const FieldInitializer& initializer : fields_) {
  17. vec->push_back({initializer.name, initializer.expression});
  18. }
  19. return MakeTuple(line_number, vec);
  20. }
  21. } // namespace Carbon