1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
//===--- Bridging/StmtBridging.cpp ----------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2022-2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/AST/ASTBridging.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/ASTNode.h"
#include "swift/AST/Expr.h"
#include "swift/AST/GenericParamList.h"
#include "swift/AST/Identifier.h"
#include "swift/AST/Initializer.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/ParseRequests.h"
#include "swift/AST/Pattern.h"
#include "swift/AST/Stmt.h"
#include "swift/AST/TypeRepr.h"
#include "swift/Basic/Assertions.h"
using namespace swift;
//===----------------------------------------------------------------------===//
// MARK: Stmts
//===----------------------------------------------------------------------===//
// Define `.asStmt` on each BridgedXXXStmt type.
#define STMT(Id, Parent) \
BridgedStmt Bridged##Id##Stmt_asStmt(Bridged##Id##Stmt stmt) { \
return static_cast<Stmt *>(stmt.unbridged()); \
}
#define ABSTRACT_STMT(Id, Parent) STMT(Id, Parent)
#include "swift/AST/StmtNodes.def"
BridgedStmtConditionElement
BridgedStmtConditionElement_createBoolean(BridgedExpr expr) {
return StmtConditionElement(expr.unbridged());
}
BridgedStmtConditionElement BridgedStmtConditionElement_createPatternBinding(
BridgedASTContext cContext, BridgedSourceLoc cIntroducerLoc,
BridgedPattern cPattern, BridgedExpr cInitializer) {
return StmtConditionElement(ConditionalPatternBindingInfo::create(
cContext.unbridged(), cIntroducerLoc.unbridged(), cPattern.unbridged(),
cInitializer.unbridged()));
}
BridgedStmtConditionElement BridgedStmtConditionElement_createPoundAvailable(
BridgedPoundAvailableInfo info) {
return StmtConditionElement(info.unbridged());
}
BridgedPoundAvailableInfo BridgedPoundAvailableInfo_createParsed(
BridgedASTContext cContext, BridgedSourceLoc cPoundLoc,
BridgedSourceLoc cLParenLoc, BridgedArrayRef cSpecs,
BridgedSourceLoc cRParenLoc, bool isUnavailability) {
SmallVector<AvailabilitySpec *, 4> specs;
for (auto cSpec : cSpecs.unbridged<BridgedAvailabilitySpec>())
specs.push_back(cSpec.unbridged());
return PoundAvailableInfo::create(cContext.unbridged(), cPoundLoc.unbridged(),
cLParenLoc.unbridged(), specs,
cRParenLoc.unbridged(), isUnavailability);
}
BridgedStmtConditionElement BridgedStmtConditionElement_createHasSymbol(
BridgedASTContext cContext, BridgedSourceLoc cPoundLoc,
BridgedSourceLoc cLParenLoc, BridgedNullableExpr cSymbolExpr,
BridgedSourceLoc cRParenLoc) {
return StmtConditionElement(PoundHasSymbolInfo::create(
cContext.unbridged(), cPoundLoc.unbridged(), cLParenLoc.unbridged(),
cSymbolExpr.unbridged(), cRParenLoc.unbridged()));
}
BridgedBraceStmt BridgedBraceStmt_createParsed(BridgedASTContext cContext,
BridgedSourceLoc cLBLoc,
BridgedArrayRef elements,
BridgedSourceLoc cRBLoc) {
llvm::SmallVector<ASTNode, 16> nodes;
for (auto node : elements.unbridged<BridgedASTNode>())
nodes.push_back(node.unbridged());
ASTContext &context = cContext.unbridged();
return BraceStmt::create(context, cLBLoc.unbridged(), nodes,
cRBLoc.unbridged());
}
BridgedBraceStmt BridgedBraceStmt_createImplicit(BridgedASTContext cContext,
BridgedSourceLoc cLBLoc,
BridgedASTNode element,
BridgedSourceLoc cRBLoc) {
return BraceStmt::create(cContext.unbridged(), cLBLoc.unbridged(),
{element.unbridged()}, cRBLoc.unbridged(),
/*Implicit=*/true);
}
BridgedBreakStmt BridgedBreakStmt_createParsed(BridgedDeclContext cDeclContext,
BridgedSourceLoc cLoc,
BridgedIdentifier cTargetName,
BridgedSourceLoc cTargetLoc) {
return new (cDeclContext.unbridged()->getASTContext())
BreakStmt(cLoc.unbridged(), cTargetName.unbridged(),
cTargetLoc.unbridged(), cDeclContext.unbridged());
}
static void getCaseLabelItems(BridgedArrayRef cItems,
SmallVectorImpl<CaseLabelItem> &output) {
for (auto &elem : cItems.unbridged<BridgedCaseLabelItemInfo>()) {
if (!elem.IsDefault) {
output.emplace_back(elem.ThePattern.unbridged(),
elem.WhereLoc.unbridged(),
elem.GuardExpr.unbridged());
} else {
output.push_back(CaseLabelItem::getDefault(
cast<AnyPattern>(elem.ThePattern.unbridged()),
elem.WhereLoc.unbridged(), elem.GuardExpr.unbridged()));
}
}
}
BridgedCaseStmt BridgedCaseStmt_createParsedSwitchCase(
BridgedASTContext cContext, BridgedSourceLoc cIntroducerLoc,
BridgedArrayRef cCaseLabelItems, BridgedSourceLoc cUnknownAttrLoc,
BridgedSourceLoc cTerminatorLoc, BridgedBraceStmt cBody) {
SmallVector<CaseLabelItem, 1> labelItems;
getCaseLabelItems(cCaseLabelItems, labelItems);
return CaseStmt::createParsedSwitchCase(
cContext.unbridged(), cIntroducerLoc.unbridged(), labelItems,
cUnknownAttrLoc.unbridged(), cTerminatorLoc.unbridged(),
cBody.unbridged());
}
BridgedCaseStmt BridgedCaseStmt_createParsedDoCatch(
BridgedASTContext cContext, BridgedSourceLoc cCatchLoc,
BridgedArrayRef cCaseLabelItems, BridgedBraceStmt cBody) {
SmallVector<CaseLabelItem, 1> labelItems;
getCaseLabelItems(cCaseLabelItems, labelItems);
return CaseStmt::createParsedDoCatch(cContext.unbridged(),
cCatchLoc.unbridged(), labelItems,
cBody.unbridged());
}
BridgedContinueStmt BridgedContinueStmt_createParsed(
BridgedDeclContext cDeclContext, BridgedSourceLoc cLoc,
BridgedIdentifier cTargetName, BridgedSourceLoc cTargetLoc) {
return new (cDeclContext.unbridged()->getASTContext())
ContinueStmt(cLoc.unbridged(), cTargetName.unbridged(),
cTargetLoc.unbridged(), cDeclContext.unbridged());
}
BridgedDeferStmt BridgedDeferStmt_createParsed(BridgedDeclContext cDeclContext,
BridgedSourceLoc cDeferLoc) {
return DeferStmt::create(cDeclContext.unbridged(), cDeferLoc.unbridged());
}
BridgedFuncDecl BridgedDeferStmt_getTempDecl(BridgedDeferStmt bridged) {
return bridged.unbridged()->getTempDecl();
}
BridgedDiscardStmt BridgedDiscardStmt_createParsed(BridgedASTContext cContext,
BridgedSourceLoc cDiscardLoc,
BridgedExpr cSubExpr) {
return new (cContext.unbridged())
DiscardStmt(cDiscardLoc.unbridged(), cSubExpr.unbridged());
}
BridgedDoStmt BridgedDoStmt_createParsed(BridgedASTContext cContext,
BridgedLabeledStmtInfo cLabelInfo,
BridgedSourceLoc cDoLoc,
BridgedBraceStmt cBody) {
return new (cContext.unbridged())
DoStmt(cLabelInfo.unbridged(), cDoLoc.unbridged(), cBody.unbridged());
}
BridgedDoCatchStmt BridgedDoCatchStmt_createParsed(
BridgedDeclContext cDeclContext, BridgedLabeledStmtInfo cLabelInfo,
BridgedSourceLoc cDoLoc, BridgedSourceLoc cThrowsLoc,
BridgedNullableTypeRepr cThrownType, BridgedStmt cBody,
BridgedArrayRef cCatches) {
return DoCatchStmt::create(cDeclContext.unbridged(), cLabelInfo.unbridged(),
cDoLoc.unbridged(), cThrowsLoc.unbridged(),
cThrownType.unbridged(), cBody.unbridged(),
cCatches.unbridged<CaseStmt *>());
}
BridgedFallthroughStmt
BridgedFallthroughStmt_createParsed(BridgedSourceLoc cLoc,
BridgedDeclContext cDC) {
return FallthroughStmt::createParsed(cLoc.unbridged(), cDC.unbridged());
}
BridgedForEachStmt BridgedForEachStmt_createParsed(
BridgedASTContext cContext, BridgedLabeledStmtInfo cLabelInfo,
BridgedSourceLoc cForLoc, BridgedSourceLoc cTryLoc,
BridgedSourceLoc cAwaitLoc, BridgedSourceLoc cUnsafeLoc,
BridgedPattern cPat, BridgedSourceLoc cInLoc,
BridgedExpr cSequence, BridgedSourceLoc cWhereLoc,
BridgedNullableExpr cWhereExpr, BridgedBraceStmt cBody) {
return new (cContext.unbridged()) ForEachStmt(
cLabelInfo.unbridged(), cForLoc.unbridged(), cTryLoc.unbridged(),
cAwaitLoc.unbridged(), cUnsafeLoc.unbridged(), cPat.unbridged(),
cInLoc.unbridged(), cSequence.unbridged(), cWhereLoc.unbridged(),
cWhereExpr.unbridged(), cBody.unbridged());
}
BridgedGuardStmt BridgedGuardStmt_createParsed(BridgedASTContext cContext,
BridgedSourceLoc cGuardLoc,
BridgedArrayRef cConds,
BridgedBraceStmt cBody) {
auto &context = cContext.unbridged();
StmtCondition cond = context.AllocateTransform<StmtConditionElement>(
cConds.unbridged<BridgedStmtConditionElement>(),
[](auto &e) { return e.unbridged(); });
return new (context)
GuardStmt(cGuardLoc.unbridged(), cond, cBody.unbridged());
}
BridgedIfStmt BridgedIfStmt_createParsed(
BridgedASTContext cContext, BridgedLabeledStmtInfo cLabelInfo,
BridgedSourceLoc cIfLoc, BridgedArrayRef cConds, BridgedBraceStmt cThen,
BridgedSourceLoc cElseLoc, BridgedNullableStmt cElse) {
auto &context = cContext.unbridged();
StmtCondition cond = context.AllocateTransform<StmtConditionElement>(
cConds.unbridged<BridgedStmtConditionElement>(),
[](auto &e) { return e.unbridged(); });
return new (context)
IfStmt(cLabelInfo.unbridged(), cIfLoc.unbridged(), cond,
cThen.unbridged(), cElseLoc.unbridged(), cElse.unbridged());
}
BridgedPoundAssertStmt BridgedPoundAssertStmt_createParsed(
BridgedASTContext cContext, BridgedSourceRange cRange,
BridgedExpr cConditionExpr, BridgedStringRef cMessage) {
return new (cContext.unbridged()) PoundAssertStmt(
cRange.unbridged(), cConditionExpr.unbridged(), cMessage.unbridged());
}
BridgedRepeatWhileStmt BridgedRepeatWhileStmt_createParsed(
BridgedASTContext cContext, BridgedLabeledStmtInfo cLabelInfo,
BridgedSourceLoc cRepeatLoc, BridgedExpr cCond, BridgedSourceLoc cWhileLoc,
BridgedStmt cBody) {
return new (cContext.unbridged()) RepeatWhileStmt(
cLabelInfo.unbridged(), cRepeatLoc.unbridged(), cCond.unbridged(),
cWhileLoc.unbridged(), cBody.unbridged());
}
BridgedReturnStmt BridgedReturnStmt_createParsed(BridgedASTContext cContext,
BridgedSourceLoc cLoc,
BridgedNullableExpr expr) {
ASTContext &context = cContext.unbridged();
return ReturnStmt::createParsed(context, cLoc.unbridged(), expr.unbridged());
}
BridgedSwitchStmt BridgedSwitchStmt_createParsed(
BridgedASTContext cContext, BridgedLabeledStmtInfo cLabelInfo,
BridgedSourceLoc cSwitchLoc, BridgedExpr cSubjectExpr,
BridgedSourceLoc cLBraceLoc, BridgedArrayRef cCases,
BridgedSourceLoc cRBraceLoc) {
SmallVector<CaseStmt *, 16> cases;
for (auto cCase : cCases.unbridged<BridgedCaseStmt>())
cases.push_back(cCase.unbridged());
return SwitchStmt::create(cLabelInfo.unbridged(), cSwitchLoc.unbridged(),
cSubjectExpr.unbridged(), cLBraceLoc.unbridged(),
cases, cRBraceLoc.unbridged(),
cRBraceLoc.unbridged(), cContext.unbridged());
}
BridgedThenStmt BridgedThenStmt_createParsed(BridgedASTContext cContext,
BridgedSourceLoc cThenLoc,
BridgedExpr cResult) {
return ThenStmt::createParsed(cContext.unbridged(), cThenLoc.unbridged(),
cResult.unbridged());
}
BridgedThrowStmt BridgedThrowStmt_createParsed(BridgedASTContext cContext,
BridgedSourceLoc cThrowLoc,
BridgedExpr cSubExpr) {
return new (cContext.unbridged())
ThrowStmt(cThrowLoc.unbridged(), cSubExpr.unbridged());
}
BridgedWhileStmt BridgedWhileStmt_createParsed(
BridgedASTContext cContext, BridgedLabeledStmtInfo cLabelInfo,
BridgedSourceLoc cWhileLoc, BridgedArrayRef cCond, BridgedStmt cBody) {
auto &context = cContext.unbridged();
StmtCondition cond = context.AllocateTransform<StmtConditionElement>(
cCond.unbridged<BridgedStmtConditionElement>(),
[](auto &e) { return e.unbridged(); });
return new (cContext.unbridged()) WhileStmt(
cLabelInfo.unbridged(), cWhileLoc.unbridged(), cond, cBody.unbridged());
}
BridgedYieldStmt BridgedYieldStmt_createParsed(BridgedASTContext cContext,
BridgedSourceLoc cYieldLoc,
BridgedSourceLoc cLParenLoc,
BridgedArrayRef cYields,
BridgedSourceLoc cRParenLoc) {
return YieldStmt::create(cContext.unbridged(), cYieldLoc.unbridged(),
cLParenLoc.unbridged(), cYields.unbridged<Expr *>(),
cRParenLoc.unbridged());
}
|