@@ -1019,6 +1019,10 @@ namespace {
1019
1019
return Writer.hasNonStandardOutput ();
1020
1020
}
1021
1021
1022
+ bool isTypeChecked () const {
1023
+ return MemberLoading == ASTDumpMemberLoading::TypeChecked;
1024
+ }
1025
+
1022
1026
// / Call `Body` in a context where the printer is ready for a child to be
1023
1027
// / printed.
1024
1028
void printRecArbitrary (std::function<void (Label)> body, Label label) {
@@ -1613,11 +1617,17 @@ namespace {
1613
1617
}
1614
1618
}
1615
1619
1620
+ template <typename T>
1621
+ void printDeclContext (const T *D) {
1622
+ printField (static_cast <void *>(D->getDeclContext ()),
1623
+ Label::always (" decl_context" ));
1624
+ }
1625
+
1616
1626
// / Prints a field containing the name or the USR (based on parsability of
1617
1627
// / the output) of a decl that is being referenced elsewhere.
1618
1628
template <typename T>
1619
1629
void printReferencedDeclField (const T *D, Label label) {
1620
- if (Writer.isParsable ()) {
1630
+ if (Writer.isParsable () && isTypeChecked () ) {
1621
1631
printFieldQuoted (declUSR (D), label);
1622
1632
} else {
1623
1633
printFieldQuoted (D->getName (), label);
@@ -1629,7 +1639,7 @@ namespace {
1629
1639
template <typename T>
1630
1640
void printReferencedDeclWithContextField (const T *D, Label label,
1631
1641
TerminalColor Color = DeclColor) {
1632
- if (Writer.isParsable ()) {
1642
+ if (Writer.isParsable () && isTypeChecked () ) {
1633
1643
printFieldQuoted (declUSR (D), label, Color);
1634
1644
} else {
1635
1645
printFieldQuoted (D->printRef (), label, Color);
@@ -1639,7 +1649,7 @@ namespace {
1639
1649
// / Print a field containing a concrete reference to a declaration.
1640
1650
void printDeclRefField (ConcreteDeclRef declRef, Label label,
1641
1651
TerminalColor Color = DeclColor) {
1642
- if (Writer.isParsable ()) {
1652
+ if (Writer.isParsable () && isTypeChecked () ) {
1643
1653
// Just omit the key/value for parsable formats if there's no decl.
1644
1654
if (!declRef.getDecl ())
1645
1655
return ;
@@ -1808,6 +1818,7 @@ namespace {
1808
1818
}
1809
1819
void visitExprPattern (ExprPattern *P, Label label) {
1810
1820
printCommon (P, " pattern_expr" , label);
1821
+ printDeclContext (P);
1811
1822
switch (P->getCachedMatchOperandOwnership ()) {
1812
1823
case ValueOwnership::Default:
1813
1824
break ;
@@ -1838,6 +1849,7 @@ namespace {
1838
1849
}
1839
1850
void visitEnumElementPattern (EnumElementPattern *P, Label label) {
1840
1851
printCommon (P, " pattern_enum_element" , label);
1852
+ printDeclContext (P);
1841
1853
1842
1854
if (Writer.isParsable ()) {
1843
1855
printName (P->getName ().getFullName (), Label::always (" element" ));
@@ -1894,12 +1906,14 @@ namespace {
1894
1906
// Parsable outputs include the USR for each decl since they can be used
1895
1907
// to cross-reference them (within the AST dump itself and with other data
1896
1908
// sources like indexstore and SourceKit).
1897
- if (Writer.isParsable ()) {
1909
+ if (Writer.isParsable () && isTypeChecked () ) {
1898
1910
if (auto usr = declUSR (D); !usr.empty ()) {
1899
1911
printFieldQuoted (usr, Label::always (" usr" ));
1900
1912
}
1901
1913
}
1902
1914
1915
+ printDeclContext (D);
1916
+
1903
1917
printFlag (D->isImplicit (), " implicit" , DeclModifierColor);
1904
1918
printFlag (D->isHoisted (), " hoisted" , DeclModifierColor);
1905
1919
@@ -1918,7 +1932,7 @@ namespace {
1918
1932
printFlag (D->TrailingSemiLoc .isValid (), " trailing_semi" ,
1919
1933
DeclModifierColor);
1920
1934
1921
- if (Writer.isParsable ()) {
1935
+ if (Writer.isParsable () && isTypeChecked () ) {
1922
1936
// Print just the USRs of any auxiliary decls associated with this decl,
1923
1937
// which lets us relate macro expansions back to their originating decl
1924
1938
// if desired.
@@ -2097,13 +2111,15 @@ namespace {
2097
2111
2098
2112
printWhereRequirements (decl);
2099
2113
if (decl->overriddenDeclsComputed ()) {
2100
- printStringListField (decl->getOverriddenDecls (),
2101
- [&](AssociatedTypeDecl *overridden) {
2102
- if (Writer.isParsable ()) {
2103
- return declUSR (overridden->getProtocol ());
2104
- }
2105
- return std::string (overridden->getProtocol ()->getName ().str ());
2106
- }, Label::always (" overridden" ), /* delimiter=*/ " , " );
2114
+ printStringListField (
2115
+ decl->getOverriddenDecls (),
2116
+ [&](AssociatedTypeDecl *overridden) {
2117
+ if (Writer.isParsable () && isTypeChecked ()) {
2118
+ return declUSR (overridden->getProtocol ());
2119
+ }
2120
+ return std::string (overridden->getProtocol ()->getName ().str ());
2121
+ },
2122
+ Label::always (" overridden" ), /* delimiter=*/ " , " );
2107
2123
}
2108
2124
2109
2125
printAttributes (decl);
@@ -2213,15 +2229,18 @@ namespace {
2213
2229
2214
2230
if (VD->overriddenDeclsComputed ()) {
2215
2231
auto overridden = VD->getOverriddenDecls ();
2216
- printStringListField (overridden, [&](ValueDecl *overridden) {
2217
- if (Writer.isParsable ()) {
2218
- return declUSR (overridden);
2219
- }
2220
- std::string value;
2221
- llvm::raw_string_ostream SOS (value);
2222
- overridden->dumpRef (SOS);
2223
- return value;
2224
- }, Label::always (" override" ), /* delimiter=*/ " , " , OverrideColor);
2232
+ printStringListField (
2233
+ overridden,
2234
+ [&](ValueDecl *overridden) {
2235
+ if (Writer.isParsable () && isTypeChecked ()) {
2236
+ return declUSR (overridden);
2237
+ }
2238
+ std::string value;
2239
+ llvm::raw_string_ostream SOS (value);
2240
+ overridden->dumpRef (SOS);
2241
+ return value;
2242
+ },
2243
+ Label::always (" override" ), /* delimiter=*/ " , " , OverrideColor);
2225
2244
}
2226
2245
2227
2246
auto VarD = dyn_cast<VarDecl>(VD);
@@ -2423,6 +2442,8 @@ namespace {
2423
2442
printHead (" parameter" , ParameterColor, label);
2424
2443
2425
2444
printDeclName (PD, Label::optional (" name" ));
2445
+
2446
+ printDeclContext (PD);
2426
2447
if (!PD->getArgumentName ().empty ())
2427
2448
printFieldQuoted (PD->getArgumentName (), Label::always (" apiName" ),
2428
2449
IdentifierColor);
@@ -2521,23 +2542,32 @@ namespace {
2521
2542
printCommon (PBD, " pattern_binding_decl" , label);
2522
2543
printAttributes (PBD);
2523
2544
2524
- printList (range (PBD->getNumPatternEntries ()), [&](auto idx, Label label) {
2525
- // Ensure that we have an object structure printed in parsable modes
2526
- // so that the children aren't directly rendered as array elements.
2527
- if (Writer.isParsable ())
2528
- printHead (" pattern_entry" , FieldLabelColor, label);
2529
-
2530
- printRec (PBD->getPattern (idx), Label::optional (" pattern" ));
2531
- if (PBD->getOriginalInit (idx)) {
2532
- printRec (PBD->getOriginalInit (idx), Label::always (" original_init" ));
2533
- }
2534
- if (PBD->getInit (idx)) {
2535
- printRec (PBD->getInit (idx), Label::always (" processed_init" ));
2536
- }
2545
+ printList (
2546
+ range (PBD->getNumPatternEntries ()),
2547
+ [&](auto idx, Label label) {
2548
+ printRecArbitrary (
2549
+ [&](Label label) {
2550
+ printHead (" pattern_entry" , FieldLabelColor, label);
2551
+
2552
+ if (PBD->getInitContext (idx))
2553
+ printField (PBD->getInitContext (idx),
2554
+ Label::always (" init_context" ));
2555
+
2556
+ printRec (PBD->getPattern (idx), Label::optional (" pattern" ));
2557
+ if (PBD->getOriginalInit (idx)) {
2558
+ printRec (PBD->getOriginalInit (idx),
2559
+ Label::always (" original_init" ));
2560
+ }
2561
+ if (PBD->getInit (idx)) {
2562
+ printRec (PBD->getInit (idx),
2563
+ Label::always (" processed_init" ));
2564
+ }
2537
2565
2538
- if (Writer.isParsable ())
2539
- printFoot ();
2540
- }, Label::optional (" pattern_entries" ));
2566
+ printFoot ();
2567
+ },
2568
+ Label::optional (" pattern_entry" ));
2569
+ },
2570
+ Label::optional (" pattern_entries" ));
2541
2571
printFoot ();
2542
2572
}
2543
2573
@@ -3094,14 +3124,17 @@ class PrintStmt : public StmtVisitor<PrintStmt, void, Label>,
3094
3124
}
3095
3125
void visitBreakStmt (BreakStmt *S, Label label) {
3096
3126
printCommon (S, " break_stmt" , label);
3127
+ printDeclContext (S);
3097
3128
printFoot ();
3098
3129
}
3099
3130
void visitContinueStmt (ContinueStmt *S, Label label) {
3100
3131
printCommon (S, " continue_stmt" , label);
3132
+ printDeclContext (S);
3101
3133
printFoot ();
3102
3134
}
3103
3135
void visitFallthroughStmt (FallthroughStmt *S, Label label) {
3104
3136
printCommon (S, " fallthrough_stmt" , label);
3137
+ printDeclContext (S);
3105
3138
printFoot ();
3106
3139
}
3107
3140
void visitSwitchStmt (SwitchStmt *S, Label label) {
@@ -3185,6 +3218,7 @@ class PrintStmt : public StmtVisitor<PrintStmt, void, Label>,
3185
3218
3186
3219
void visitDoCatchStmt (DoCatchStmt *S, Label label) {
3187
3220
printCommon (S, " do_catch_stmt" , label);
3221
+ printDeclContext (S);
3188
3222
printThrowDest (S->rethrows (), /* wantNothrow=*/ true );
3189
3223
printRec (S->getBody (), Label::always (" body" ));
3190
3224
printRecRange (S->getCatches (), Ctx, Label::always (" catch_stmts" ));
@@ -4347,6 +4381,7 @@ class PrintExpr : public ExprVisitor<PrintExpr, void, Label>,
4347
4381
4348
4382
void visitSingleValueStmtExpr (SingleValueStmtExpr *E, Label label) {
4349
4383
printCommon (E, " single_value_stmt_expr" , label);
4384
+ printDeclContext (E);
4350
4385
printRec (E->getStmt (), &E->getDeclContext ()->getASTContext (),
4351
4386
Label::optional (" stmt" ));
4352
4387
printFoot ();
@@ -4381,6 +4416,7 @@ class PrintExpr : public ExprVisitor<PrintExpr, void, Label>,
4381
4416
4382
4417
void visitMacroExpansionExpr (MacroExpansionExpr *E, Label label) {
4383
4418
printCommon (E, " macro_expansion_expr" , label);
4419
+ printDeclContext (E);
4384
4420
4385
4421
printFieldQuoted (E->getMacroName (), Label::always (" name" ), IdentifierColor);
4386
4422
printField (E->getRawDiscriminator (), Label::always (" discriminator" ),
@@ -4482,6 +4518,7 @@ class PrintTypeRepr : public TypeReprVisitor<PrintTypeRepr, void, Label>,
4482
4518
printFieldQuoted (T->getNameRef (), Label::always (" id" ), IdentifierColor);
4483
4519
if (T->isBound ()) {
4484
4520
printReferencedDeclWithContextField (T->getBoundDecl (), Label::always (" bind" ));
4521
+ printDeclContext (T);
4485
4522
} else {
4486
4523
printFlag (" unbound" );
4487
4524
}
@@ -4974,6 +5011,11 @@ class PrintAttribute : public AttributeVisitor<PrintAttribute, void, Label>,
4974
5011
}
4975
5012
void visitCustomAttr (CustomAttr *Attr, Label label) {
4976
5013
printCommon (Attr, " custom_attr" , label);
5014
+
5015
+ printField (
5016
+ static_cast <void *>(static_cast <DeclContext *>(Attr->getInitContext ())),
5017
+ Label::always (" init_context" ));
5018
+
4977
5019
if (Attr->getType ()) {
4978
5020
printTypeField (Attr->getType (), Label::always (" type" ));
4979
5021
} else if (MemberLoading == ASTDumpMemberLoading::TypeChecked) {
@@ -5050,7 +5092,7 @@ class PrintAttribute : public AttributeVisitor<PrintAttribute, void, Label>,
5050
5092
}
5051
5093
void visitImplementsAttr (ImplementsAttr *Attr, Label label) {
5052
5094
printCommon (Attr, " implements_attr" , label);
5053
- if (Writer.isParsable ()) {
5095
+ if (Writer.isParsable () && isTypeChecked () ) {
5054
5096
// Print the resolved protocol's USR in parsable outputs, not the
5055
5097
// TypeRepr.
5056
5098
if (auto PD = Attr->getCachedProtocol (DC); PD && *PD != nullptr ) {
@@ -5212,7 +5254,7 @@ class PrintAttribute : public AttributeVisitor<PrintAttribute, void, Label>,
5212
5254
Label label) {
5213
5255
printCommon (Attr, " restated_objc_conformance_attr" , label);
5214
5256
if (Attr->Proto ) {
5215
- if (Writer.isParsable ()) {
5257
+ if (Writer.isParsable () && isTypeChecked () ) {
5216
5258
printFieldQuoted (declUSR (Attr->Proto ), Label::optional (" proto" ));
5217
5259
} else {
5218
5260
printFieldRaw ([&](auto &out) { Attr->Proto ->dumpRef (out); },
@@ -5293,7 +5335,7 @@ class PrintAttribute : public AttributeVisitor<PrintAttribute, void, Label>,
5293
5335
Label label) {
5294
5336
printCommon (Attr, " synthesized_protocol_attr" , label);
5295
5337
printFlag (Attr->isUnchecked (), " unchecked" );
5296
- if (Writer.isParsable ()) {
5338
+ if (Writer.isParsable () && isTypeChecked () ) {
5297
5339
printFieldQuoted (declUSR (Attr->getProtocol ()),
5298
5340
Label::optional (" protocol" ));
5299
5341
} else {
@@ -5490,7 +5532,7 @@ class PrintConformance : public PrintBase {
5490
5532
printFlag (" no_witness" );
5491
5533
else if (witness.getDecl () == req)
5492
5534
printFlag (" dynamic_witness" );
5493
- else if (Writer.isParsable ()) {
5535
+ else if (Writer.isParsable () && isTypeChecked () ) {
5494
5536
printFieldQuoted (declUSR (witness.getDecl ()),
5495
5537
Label::always (" witness" ));
5496
5538
} else {
0 commit comments