lib/Sema/DerivedConformanceCodingKey.cpp
/// Sets the body of the given function to return a string value based on
/// switching on `self`.
///
/// \param strValDecl The function whose body to set.
Sets the body of the given function to return a string value based on
switching on `self`.

\param strValDecl The function whose body to set.
Sets the body of the given function to return a string value based on switching on ` self ` . \ param strValDecl The function whose body to set .
static void
deriveBodyCodingKey_enum_stringValue(AbstractFunctionDecl *strValDecl, void *) {
  auto *parentDC = strValDecl->getDeclContext();
  auto &C = parentDC->getASTContext();
  auto *enumDecl = parentDC->getSelfEnumDecl();
  Type enumType = parentDC->getDeclaredTypeInContext();
  BraceStmt *body = nullptr;
  auto elements = enumDecl->getAllElements();
  if (elements.empty() ) {
    auto *emptyStringExpr = new (C) StringLiteralExpr("", SourceRange(),
                                                      true);
    auto *returnStmt = new (C) ReturnStmt(SourceLoc(), emptyStringExpr);
    body = BraceStmt::create(C, SourceLoc(), ASTNode(returnStmt),
                             SourceLoc());
  } else {
    SmallVector<ASTNode, 4> cases;
    for (auto *elt : elements) {
      auto *pat = new (C) EnumElementPattern(TypeLoc::withoutLoc(enumType),
                                             SourceLoc(), SourceLoc(),
                                             Identifier(), elt, nullptr);
      pat->setImplicit();
      auto labelItem = CaseLabelItem(pat);
      auto *caseValue = new (C) StringLiteralExpr(elt->getNameStr(),
                                                  SourceRange(),
                                                  true);
      auto *returnStmt = new (C) ReturnStmt(SourceLoc(), caseValue);
      auto *caseBody = BraceStmt::create(C, SourceLoc(), ASTNode(returnStmt),
                                         SourceLoc());
      cases.push_back(CaseStmt::create(C, SourceLoc(), labelItem, SourceLoc(),
                                       SourceLoc(), caseBody,
                                        None));
    }
    auto *selfRef = DerivedConformance::createSelfDeclRef(strValDecl);
    auto *switchStmt = SwitchStmt::create(LabeledStmtInfo(), SourceLoc(),
                                          selfRef, SourceLoc(), cases,
                                          SourceLoc(), C);
    body = BraceStmt::create(C, SourceLoc(), ASTNode(switchStmt), SourceLoc());
  }
  strValDecl->setBody(body);
}
static void deriveBodyCodingKey_enum_stringValue ( AbstractFunctionDecl * strValDecl , void * ) { auto * parentDC = strValDecl -> getDeclContext ( ) ; auto & C = parentDC -> getASTContext ( ) ; auto * enumDecl = parentDC -> getSelfEnumDecl ( ) ; Type enumType = parentDC -> getDeclaredTypeInContext ( ) ; BraceStmt * body = nullptr ; auto elements = enumDecl -> getAllElements ( ) ; if ( elements . empty ( ) ) { auto * emptyStringExpr = new ( C ) StringLiteralExpr ( " " , SourceRange ( ) , true ) ; auto * returnStmt = new ( C ) ReturnStmt ( SourceLoc ( ) , emptyStringExpr ) ; body = BraceStmt :: create ( C , SourceLoc ( ) , ASTNode ( returnStmt ) , SourceLoc ( ) ) ; } else { SmallVector < ASTNode , 4 > cases ; for ( auto * elt : elements ) { auto * pat = new ( C ) EnumElementPattern ( TypeLoc :: withoutLoc ( enumType ) , SourceLoc ( ) , SourceLoc ( ) , Identifier ( ) , elt , nullptr ) ; pat -> setImplicit ( ) ; auto labelItem = CaseLabelItem ( pat ) ; auto * caseValue = new ( C ) StringLiteralExpr ( elt -> getNameStr ( ) , SourceRange ( ) , true ) ; auto * returnStmt = new ( C ) ReturnStmt ( SourceLoc ( ) , caseValue ) ; auto * caseBody = BraceStmt :: create ( C , SourceLoc ( ) , ASTNode ( returnStmt ) , SourceLoc ( ) ) ; cases . push_back ( CaseStmt :: create ( C , SourceLoc ( ) , labelItem , SourceLoc ( ) , SourceLoc ( ) , caseBody , None ) ) ; } auto * selfRef = DerivedConformance :: createSelfDeclRef ( strValDecl ) ; auto * switchStmt = SwitchStmt :: create ( LabeledStmtInfo ( ) , SourceLoc ( ) , selfRef , SourceLoc ( ) , cases , SourceLoc ( ) , C ) ; body = BraceStmt :: create ( C , SourceLoc ( ) , ASTNode ( switchStmt ) , SourceLoc ( ) ) ; } strValDecl -> setBody ( body ) ; }
Sets the body of the given function to return a string value based on
switching on `self`.
Sets the body of the given function to return a string value based on switching on ` self ` .
// enum SomeEnum { //   case A, B, C //   @derived var stringValue: String { //     switch self { //       case A: //         return "A" //       case B: //         return "B" //       case C: //         return "C" //     } //   } // } /* empty enum */ // return "" /*Implicit=*/ /*Implicit=*/ /*case body var decls*/