@@ -168,12 +168,15 @@ parseProtocolListFromFile(StringRef protocolListFilePath,
168
168
}
169
169
170
170
std::vector<std::shared_ptr<BuilderValue::BuilderMember>>
171
- getResultBuilderMembersFromBraceStmt (BraceStmt *braceStmt);
171
+ getResultBuilderMembersFromBraceStmt (BraceStmt *braceStmt,
172
+ const DeclContext *declContext);
172
173
173
- static std::shared_ptr<CompileTimeValue> extractCompileTimeValue (Expr *expr);
174
+ static std::shared_ptr<CompileTimeValue>
175
+ extractCompileTimeValue (Expr *expr, const DeclContext *declContext);
174
176
175
177
static std::vector<FunctionParameter>
176
- extractFunctionArguments (const ArgumentList *args) {
178
+ extractFunctionArguments (const ArgumentList *args,
179
+ const DeclContext *declContext) {
177
180
std::vector<FunctionParameter> parameters;
178
181
179
182
for (auto arg : *args) {
@@ -188,7 +191,8 @@ extractFunctionArguments(const ArgumentList *args) {
188
191
} else if (auto optionalInject = dyn_cast<InjectIntoOptionalExpr>(argExpr)) {
189
192
argExpr = optionalInject->getSubExpr ();
190
193
}
191
- parameters.push_back ({label, type, extractCompileTimeValue (argExpr)});
194
+ parameters.push_back (
195
+ {label, type, extractCompileTimeValue (argExpr, declContext)});
192
196
}
193
197
194
198
return parameters;
@@ -224,7 +228,8 @@ static std::optional<std::string> extractRawLiteral(Expr *expr) {
224
228
return std::nullopt;
225
229
}
226
230
227
- static std::shared_ptr<CompileTimeValue> extractCompileTimeValue (Expr *expr) {
231
+ static std::shared_ptr<CompileTimeValue>
232
+ extractCompileTimeValue (Expr *expr, const DeclContext *declContext) {
228
233
if (expr) {
229
234
switch (expr->getKind ()) {
230
235
case ExprKind::BooleanLiteral:
@@ -247,7 +252,8 @@ static std::shared_ptr<CompileTimeValue> extractCompileTimeValue(Expr *expr) {
247
252
auto arrayExpr = cast<ArrayExpr>(expr);
248
253
std::vector<std::shared_ptr<CompileTimeValue>> elementValues;
249
254
for (const auto elementExpr : arrayExpr->getElements ()) {
250
- elementValues.push_back (extractCompileTimeValue (elementExpr));
255
+ elementValues.push_back (
256
+ extractCompileTimeValue (elementExpr, declContext));
251
257
}
252
258
return std::make_shared<ArrayValue>(elementValues);
253
259
}
@@ -256,7 +262,7 @@ static std::shared_ptr<CompileTimeValue> extractCompileTimeValue(Expr *expr) {
256
262
auto dictionaryExpr = cast<DictionaryExpr>(expr);
257
263
std::vector<std::shared_ptr<TupleValue>> tuples;
258
264
for (auto elementExpr : dictionaryExpr->getElements ()) {
259
- auto elementValue = extractCompileTimeValue (elementExpr);
265
+ auto elementValue = extractCompileTimeValue (elementExpr, declContext );
260
266
if (isa<TupleValue>(elementValue.get ())) {
261
267
tuples.push_back (std::static_pointer_cast<TupleValue>(elementValue));
262
268
}
@@ -279,13 +285,15 @@ static std::shared_ptr<CompileTimeValue> extractCompileTimeValue(Expr *expr) {
279
285
? std::nullopt
280
286
: std::optional<std::string>(elementName.str ().str ());
281
287
282
- elements.push_back ({label, elementExpr->getType (),
283
- extractCompileTimeValue (elementExpr)});
288
+ elements.push_back (
289
+ {label, elementExpr->getType (),
290
+ extractCompileTimeValue (elementExpr, declContext)});
284
291
}
285
292
} else {
286
293
for (auto elementExpr : tupleExpr->getElements ()) {
287
- elements.push_back ({std::nullopt, elementExpr->getType (),
288
- extractCompileTimeValue (elementExpr)});
294
+ elements.push_back (
295
+ {std::nullopt, elementExpr->getType (),
296
+ extractCompileTimeValue (elementExpr, declContext)});
289
297
}
290
298
}
291
299
return std::make_shared<TupleValue>(elements);
@@ -301,13 +309,13 @@ static std::shared_ptr<CompileTimeValue> extractCompileTimeValue(Expr *expr) {
301
309
declRefExpr->getDecl ()->getName ().getBaseIdentifier ().str ().str ();
302
310
303
311
std::vector<FunctionParameter> parameters =
304
- extractFunctionArguments (callExpr->getArgs ());
312
+ extractFunctionArguments (callExpr->getArgs (), declContext );
305
313
return std::make_shared<FunctionCallValue>(identifier, parameters);
306
314
}
307
315
308
316
if (functionKind == ExprKind::ConstructorRefCall) {
309
317
std::vector<FunctionParameter> parameters =
310
- extractFunctionArguments (callExpr->getArgs ());
318
+ extractFunctionArguments (callExpr->getArgs (), declContext );
311
319
return std::make_shared<InitCallValue>(callExpr->getType (), parameters);
312
320
}
313
321
@@ -320,7 +328,7 @@ static std::shared_ptr<CompileTimeValue> extractCompileTimeValue(Expr *expr) {
320
328
declRefExpr->getDecl ()->getName ().getBaseIdentifier ().str ().str ();
321
329
322
330
std::vector<FunctionParameter> parameters =
323
- extractFunctionArguments (callExpr->getArgs ());
331
+ extractFunctionArguments (callExpr->getArgs (), declContext );
324
332
325
333
auto declRef = dotSyntaxCallExpr->getFn ()->getReferencedDecl ();
326
334
switch (declRef.getDecl ()->getKind ()) {
@@ -364,23 +372,23 @@ static std::shared_ptr<CompileTimeValue> extractCompileTimeValue(Expr *expr) {
364
372
365
373
case ExprKind::Erasure: {
366
374
auto erasureExpr = cast<ErasureExpr>(expr);
367
- return extractCompileTimeValue (erasureExpr->getSubExpr ());
375
+ return extractCompileTimeValue (erasureExpr->getSubExpr (), declContext );
368
376
}
369
377
370
378
case ExprKind::Paren: {
371
379
auto parenExpr = cast<ParenExpr>(expr);
372
- return extractCompileTimeValue (parenExpr->getSubExpr ());
380
+ return extractCompileTimeValue (parenExpr->getSubExpr (), declContext );
373
381
}
374
382
375
383
case ExprKind::PropertyWrapperValuePlaceholder: {
376
384
auto placeholderExpr = cast<PropertyWrapperValuePlaceholderExpr>(expr);
377
- return extractCompileTimeValue (
378
- placeholderExpr-> getOriginalWrappedValue () );
385
+ return extractCompileTimeValue (placeholderExpr-> getOriginalWrappedValue (),
386
+ declContext );
379
387
}
380
388
381
389
case ExprKind::Coerce: {
382
390
auto coerceExpr = cast<CoerceExpr>(expr);
383
- return extractCompileTimeValue (coerceExpr->getSubExpr ());
391
+ return extractCompileTimeValue (coerceExpr->getSubExpr (), declContext );
384
392
}
385
393
386
394
case ExprKind::DotSelf: {
@@ -394,7 +402,8 @@ static std::shared_ptr<CompileTimeValue> extractCompileTimeValue(Expr *expr) {
394
402
395
403
case ExprKind::UnderlyingToOpaque: {
396
404
auto underlyingToOpaque = cast<UnderlyingToOpaqueExpr>(expr);
397
- return extractCompileTimeValue (underlyingToOpaque->getSubExpr ());
405
+ return extractCompileTimeValue (underlyingToOpaque->getSubExpr (),
406
+ declContext);
398
407
}
399
408
400
409
case ExprKind::DefaultArgument: {
@@ -445,12 +454,13 @@ static std::shared_ptr<CompileTimeValue> extractCompileTimeValue(Expr *expr) {
445
454
446
455
case ExprKind::InjectIntoOptional: {
447
456
auto injectIntoOptionalExpr = cast<InjectIntoOptionalExpr>(expr);
448
- return extractCompileTimeValue (injectIntoOptionalExpr->getSubExpr ());
457
+ return extractCompileTimeValue (injectIntoOptionalExpr->getSubExpr (),
458
+ declContext);
449
459
}
450
460
451
461
case ExprKind::Load: {
452
462
auto loadExpr = cast<LoadExpr>(expr);
453
- return extractCompileTimeValue (loadExpr->getSubExpr ());
463
+ return extractCompileTimeValue (loadExpr->getSubExpr (), declContext );
454
464
}
455
465
456
466
case ExprKind::MemberRef: {
@@ -474,7 +484,7 @@ static std::shared_ptr<CompileTimeValue> extractCompileTimeValue(Expr *expr) {
474
484
Ctx, [&](bool isInterpolation, CallExpr *segment) -> void {
475
485
auto arg = segment->getArgs ()->get (0 );
476
486
auto expr = arg.getExpr ();
477
- segments.push_back (extractCompileTimeValue (expr));
487
+ segments.push_back (extractCompileTimeValue (expr, declContext ));
478
488
});
479
489
480
490
return std::make_shared<InterpolatedStringLiteralValue>(segments);
@@ -483,7 +493,8 @@ static std::shared_ptr<CompileTimeValue> extractCompileTimeValue(Expr *expr) {
483
493
case ExprKind::Closure: {
484
494
auto closureExpr = cast<ClosureExpr>(expr);
485
495
auto body = closureExpr->getBody ();
486
- auto resultBuilderMembers = getResultBuilderMembersFromBraceStmt (body);
496
+ auto resultBuilderMembers =
497
+ getResultBuilderMembersFromBraceStmt (body, declContext);
487
498
488
499
if (!resultBuilderMembers.empty ()) {
489
500
return std::make_shared<BuilderValue>(resultBuilderMembers);
@@ -493,7 +504,7 @@ static std::shared_ptr<CompileTimeValue> extractCompileTimeValue(Expr *expr) {
493
504
494
505
case ExprKind::DerivedToBase: {
495
506
auto derivedExpr = cast<DerivedToBaseExpr>(expr);
496
- return extractCompileTimeValue (derivedExpr->getSubExpr ());
507
+ return extractCompileTimeValue (derivedExpr->getSubExpr (), declContext );
497
508
}
498
509
default : {
499
510
break ;
@@ -504,8 +515,8 @@ static std::shared_ptr<CompileTimeValue> extractCompileTimeValue(Expr *expr) {
504
515
return std::make_shared<RuntimeValue>();
505
516
}
506
517
507
- static CustomAttrValue
508
- extractAttributeValue ( const CustomAttr *attr ) {
518
+ static CustomAttrValue extractAttributeValue ( const CustomAttr *attr,
519
+ const DeclContext *declContext ) {
509
520
std::vector<FunctionParameter> parameters;
510
521
if (const auto *args = attr->getArgs ()) {
511
522
for (auto arg : *args) {
@@ -518,8 +529,8 @@ extractAttributeValue(const CustomAttr *attr) {
518
529
argExpr = decl->getTypeCheckedDefaultExpr ();
519
530
}
520
531
}
521
- parameters.push_back (
522
- {label, argExpr-> getType (), extractCompileTimeValue (argExpr)});
532
+ parameters.push_back ({label, argExpr-> getType (),
533
+ extractCompileTimeValue (argExpr, declContext )});
523
534
}
524
535
}
525
536
return {attr, parameters};
@@ -529,7 +540,8 @@ static AttrValueVector
529
540
extractPropertyWrapperAttrValues (VarDecl *propertyDecl) {
530
541
AttrValueVector customAttrValues;
531
542
for (auto *propertyWrapper : propertyDecl->getAttachedPropertyWrappers ())
532
- customAttrValues.push_back (extractAttributeValue (propertyWrapper));
543
+ customAttrValues.push_back (
544
+ extractAttributeValue (propertyWrapper, propertyDecl->getDeclContext ()));
533
545
return customAttrValues;
534
546
}
535
547
@@ -541,7 +553,9 @@ extractTypePropertyInfo(VarDecl *propertyDecl) {
541
553
542
554
if (const auto binding = propertyDecl->getParentPatternBinding ()) {
543
555
if (const auto originalInit = binding->getInit (0 )) {
544
- return {propertyDecl, extractCompileTimeValue (originalInit),
556
+ return {propertyDecl,
557
+ extractCompileTimeValue (originalInit,
558
+ propertyDecl->getInnermostDeclContext ()),
545
559
propertyWrapperValues};
546
560
}
547
561
}
@@ -551,9 +565,11 @@ extractTypePropertyInfo(VarDecl *propertyDecl) {
551
565
auto node = body->getFirstElement ();
552
566
if (auto *stmt = node.dyn_cast <Stmt *>()) {
553
567
if (stmt->getKind () == StmtKind::Return) {
554
- return {propertyDecl,
555
- extractCompileTimeValue (cast<ReturnStmt>(stmt)->getResult ()),
556
- propertyWrapperValues};
568
+ return {
569
+ propertyDecl,
570
+ extractCompileTimeValue (cast<ReturnStmt>(stmt)->getResult (),
571
+ accessorDecl->getInnermostDeclContext ()),
572
+ propertyWrapperValues};
557
573
}
558
574
}
559
575
}
@@ -992,16 +1008,19 @@ getResultBuilderElementFromASTNode(const ASTNode node) {
992
1008
if (auto *D = node.dyn_cast <Decl *>()) {
993
1009
if (auto *patternBinding = dyn_cast<PatternBindingDecl>(D)) {
994
1010
if (auto originalInit = patternBinding->getOriginalInit (0 )) {
995
- return extractCompileTimeValue (originalInit);
1011
+ return extractCompileTimeValue (
1012
+ originalInit, patternBinding->getInnermostDeclContext ());
996
1013
}
997
1014
}
998
1015
}
999
1016
return std::nullopt;
1000
1017
}
1001
1018
1002
1019
BuilderValue::ConditionalMember
1003
- getConditionalMemberFromIfStmt (const IfStmt *ifStmt) {
1004
- std::vector<AvailabilitySpec> AvailabilityAttributes;
1020
+ getConditionalMemberFromIfStmt (const IfStmt *ifStmt,
1021
+ const DeclContext *declContext) {
1022
+ std::vector<BuilderValue::ConditionalMember::AvailabilitySpec>
1023
+ AvailabilitySpecs;
1005
1024
std::vector<std::shared_ptr<BuilderValue::BuilderMember>> IfElements;
1006
1025
std::vector<std::shared_ptr<BuilderValue::BuilderMember>> ElseElements;
1007
1026
if (auto thenBraceStmt = ifStmt->getThenStmt ()) {
@@ -1016,7 +1035,7 @@ getConditionalMemberFromIfStmt(const IfStmt *ifStmt) {
1016
1035
if (auto elseStmt = ifStmt->getElseStmt ()) {
1017
1036
if (auto *elseIfStmt = dyn_cast<IfStmt>(elseStmt)) {
1018
1037
ElseElements.push_back (std::make_shared<BuilderValue::ConditionalMember>(
1019
- getConditionalMemberFromIfStmt (elseIfStmt)));
1038
+ getConditionalMemberFromIfStmt (elseIfStmt, declContext )));
1020
1039
} else if (auto *elseBraceStmt = dyn_cast<BraceStmt>(elseStmt)) {
1021
1040
for (auto elem : elseBraceStmt->getElements ()) {
1022
1041
if (auto memberElement = getResultBuilderElementFromASTNode (elem)) {
@@ -1035,20 +1054,22 @@ getConditionalMemberFromIfStmt(const IfStmt *ifStmt) {
1035
1054
if (elt.getKind () == StmtConditionElement::CK_Availability) {
1036
1055
for (auto *Q : elt.getAvailability ()->getQueries ()) {
1037
1056
if (Q->getPlatform () != PlatformKind::none) {
1038
- AvailabilityAttributes.push_back (*Q);
1057
+ auto spec = BuilderValue::ConditionalMember::AvailabilitySpec (
1058
+ *Q->getDomain (), Q->getVersion ());
1059
+ AvailabilitySpecs.push_back (spec);
1039
1060
}
1040
1061
}
1041
1062
memberKind = BuilderValue::LimitedAvailability;
1042
1063
break ;
1043
1064
}
1044
1065
}
1045
1066
1046
- if (AvailabilityAttributes .empty ()) {
1067
+ if (AvailabilitySpecs .empty ()) {
1047
1068
return BuilderValue::ConditionalMember (memberKind, IfElements,
1048
1069
ElseElements);
1049
1070
}
1050
1071
1051
- return BuilderValue::ConditionalMember (memberKind, AvailabilityAttributes ,
1072
+ return BuilderValue::ConditionalMember (memberKind, AvailabilitySpecs ,
1052
1073
IfElements, ElseElements);
1053
1074
}
1054
1075
@@ -1067,7 +1088,8 @@ getBuildArrayMemberFromForEachStmt(const ForEachStmt *forEachStmt) {
1067
1088
}
1068
1089
1069
1090
std::vector<std::shared_ptr<BuilderValue::BuilderMember>>
1070
- getResultBuilderMembersFromBraceStmt (BraceStmt *braceStmt) {
1091
+ getResultBuilderMembersFromBraceStmt (BraceStmt *braceStmt,
1092
+ const DeclContext *declContext) {
1071
1093
std::vector<std::shared_ptr<BuilderValue::BuilderMember>>
1072
1094
ResultBuilderMembers;
1073
1095
for (auto elem : braceStmt->getElements ()) {
@@ -1079,7 +1101,7 @@ getResultBuilderMembersFromBraceStmt(BraceStmt *braceStmt) {
1079
1101
if (auto *ifStmt = dyn_cast<IfStmt>(stmt)) {
1080
1102
ResultBuilderMembers.push_back (
1081
1103
std::make_shared<BuilderValue::ConditionalMember>(
1082
- getConditionalMemberFromIfStmt (ifStmt)));
1104
+ getConditionalMemberFromIfStmt (ifStmt, declContext )));
1083
1105
} else if (auto *doStmt = dyn_cast<DoStmt>(stmt)) {
1084
1106
if (auto body = doStmt->getBody ()) {
1085
1107
for (auto elem : body->getElements ()) {
@@ -1106,7 +1128,8 @@ createBuilderCompileTimeValue(CustomAttr *AttachedResultBuilder,
1106
1128
if (!VarDecl->getAllAccessors ().empty ()) {
1107
1129
if (auto accessor = VarDecl->getAllAccessors ()[0 ]) {
1108
1130
if (auto braceStmt = accessor->getTypecheckedBody ()) {
1109
- ResultBuilderMembers = getResultBuilderMembersFromBraceStmt (braceStmt);
1131
+ ResultBuilderMembers = getResultBuilderMembersFromBraceStmt (
1132
+ braceStmt, accessor->getDeclContext ());
1110
1133
}
1111
1134
}
1112
1135
}
@@ -1159,12 +1182,13 @@ void writeBuilderMember(
1159
1182
1160
1183
default : {
1161
1184
auto member = cast<BuilderValue::ConditionalMember>(Member);
1162
- if (auto availabilityAttributes = member->getAvailabilityAttributes ()) {
1185
+ if (auto availabilitySpecs = member->getAvailabilitySpecs ()) {
1163
1186
JSON.attributeArray (" availabilityAttributes" , [&] {
1164
- for (auto elem : *availabilityAttributes ) {
1187
+ for (auto elem : *availabilitySpecs ) {
1165
1188
JSON.object ([&] {
1166
- JSON.attribute (" platform" ,
1167
- platformString (elem.getPlatform ()).str ());
1189
+ JSON.attribute (
1190
+ " platform" ,
1191
+ platformString (elem.getDomain ().getPlatformKind ()).str ());
1168
1192
JSON.attribute (" minVersion" , elem.getVersion ().getAsString ());
1169
1193
});
1170
1194
}
0 commit comments