Skip to content

Commit 8498861

Browse files
authored
Merge pull request #78955 from allevato/6.1-json-ast
🍒[6.1] Add a flag to dump the AST as JSON.
2 parents e5f514d + 5b53cd8 commit 8498861

28 files changed

+3775
-1409
lines changed

include/swift/AST/Attr.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,6 +1709,12 @@ class ImplementsAttr : public DeclAttribute {
17091709
DeclName MemberName);
17101710

17111711
ProtocolDecl *getProtocol(DeclContext *dc) const;
1712+
1713+
/// Returns the protocol declaration containing the requirement being
1714+
/// implemented by the attributed declaration if it has already been computed,
1715+
/// otherwise `nullopt`. This should only be used for dumping.
1716+
std::optional<ProtocolDecl *> getCachedProtocol(DeclContext *dc) const;
1717+
17121718
TypeRepr *getProtocolTypeRepr() const { return TyR; }
17131719

17141720
DeclName getMemberName() const { return MemberName; }

include/swift/AST/CatchNode.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ class CatchNode: public llvm::PointerUnion<
4545
/// needs to be inferred.
4646
Type getExplicitCaughtType(ASTContext &ctx) const;
4747

48+
/// Returns the explicitly-specified type error that will be caught by this
49+
/// catch node, or `nullopt` if it has not yet been computed. This should only
50+
/// be used for dumping.
51+
std::optional<Type> getCachedExplicitCaughtType(ASTContext &ctx) const;
52+
4853
friend llvm::hash_code hash_value(CatchNode catchNode) {
4954
using llvm::hash_value;
5055
return hash_value(catchNode.getOpaqueValue());

include/swift/AST/Decl.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3241,6 +3241,10 @@ class ValueDecl : public Decl {
32413241
/// Get the decl for this value's opaque result type, if it has one.
32423242
OpaqueTypeDecl *getOpaqueResultTypeDecl() const;
32433243

3244+
/// Gets the decl for this value's opaque result type if it has already been
3245+
/// computed, or `nullopt` otherwise. This should only be used for dumping.
3246+
std::optional<OpaqueTypeDecl *> getCachedOpaqueResultTypeDecl() const;
3247+
32443248
/// Get the representative for this value's opaque result type, if it has one.
32453249
/// Returns a `TypeRepr` instead of an `OpaqueReturnTypeRepr` because 'some'
32463250
/// types might appear in one or more structural positions, e.g. (some P,
@@ -7657,6 +7661,10 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
76577661
/// Retrieves the thrown interface type.
76587662
Type getThrownInterfaceType() const;
76597663

7664+
/// Returns the thrown interface type of this function if it has already been
7665+
/// computed, otherwise `nullopt`. This should only be used for dumping.
7666+
std::optional<Type> getCachedThrownInterfaceType() const;
7667+
76607668
/// Retrieve the "effective" thrown interface type, or std::nullopt if
76617669
/// this function cannot throw.
76627670
///
@@ -8217,6 +8225,10 @@ class FuncDecl : public AbstractFunctionDecl {
82178225
/// Retrieve the result interface type of this function.
82188226
Type getResultInterfaceType() const;
82198227

8228+
/// Returns the result interface type of this function if it has already been
8229+
/// computed, otherwise `nullopt`. This should only be used for dumping.
8230+
std::optional<Type> getCachedResultInterfaceType() const;
8231+
82208232
/// isUnaryOperator - Determine whether this is a unary operator
82218233
/// implementation. This check is a syntactic rather than type-based check,
82228234
/// which looks at the number of parameters specified, in order to allow
@@ -9417,6 +9429,10 @@ class MacroDecl : public GenericContext, public ValueDecl {
94179429
/// Retrieve the interface type produced when expanding this macro.
94189430
Type getResultInterfaceType() const;
94199431

9432+
/// Returns the result interface type of this macro if it has already been
9433+
/// computed, otherwise `nullopt`. This should only be used for dumping.
9434+
std::optional<Type> getCachedResultInterfaceType() const;
9435+
94209436
/// Determine the contexts in which this macro can be applied.
94219437
MacroRoles getMacroRoles() const;
94229438

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,5 +589,15 @@ ERROR(experimental_not_supported_in_production,none,
589589
"experimental feature '%0' cannot be enabled in production compiler",
590590
(StringRef))
591591

592+
ERROR(json_zlib_not_supported,none,
593+
"this compiler was not built with zlib compression support enabled; "
594+
"'-dump-ast-format json-zlib' cannot be used", ())
595+
596+
ERROR(ast_format_requires_dump_ast,none,
597+
"structured AST formats are only supported when using -dump-ast", ())
598+
599+
ERROR(unknown_dump_ast_format,none,
600+
"unknown format '%0' requested with '-dump-ast-format'", (StringRef))
601+
592602
#define UNDEFINE_DIAGNOSTIC_MACROS
593603
#include "DefineDiagnosticMacros.h"

include/swift/AST/SourceFile.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,9 @@ class SourceFile final : public FileUnit {
677677
SWIFT_DEBUG_DUMP;
678678
void dump(raw_ostream &os, bool parseIfNeeded = false) const;
679679

680+
/// Dumps this source file's AST in JSON format to the given output stream.
681+
void dumpJSON(raw_ostream &os) const;
682+
680683
/// Pretty-print the contents of this source file.
681684
///
682685
/// \param Printer The AST printer used for printing the contents.

include/swift/Frontend/FrontendOptions.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,16 @@ class FrontendOptions {
433433
/// -dump-scope-maps.
434434
SmallVector<std::pair<unsigned, unsigned>, 2> DumpScopeMapLocations;
435435

436+
/// The possible output formats supported for dumping ASTs.
437+
enum class ASTFormat {
438+
Default, ///< S-expressions for debugging
439+
JSON, ///< Structured JSON for analysis
440+
JSONZlib, ///< Like JSON, but zlib-compressed
441+
};
442+
443+
/// The output format generated by the `-dump-ast` flag.
444+
ASTFormat DumpASTFormat = ASTFormat::Default;
445+
436446
/// Determines whether the static or shared resource folder is used.
437447
/// When set to `true`, the default resource folder will be set to
438448
/// '.../lib/swift', otherwise '.../lib/swift_static'.

include/swift/Option/Options.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,6 +1343,12 @@ def emit_parse : Flag<["-"], "emit-parse">, Alias<dump_parse>,
13431343
def dump_ast : Flag<["-"], "dump-ast">,
13441344
HelpText<"Parse and type-check input file(s) and dump AST(s)">, ModeOpt,
13451345
Flags<[FrontendOption, NoInteractiveOption, DoesNotAffectIncrementalBuild]>;
1346+
def dump_ast_format : Separate<["-"], "dump-ast-format">,
1347+
HelpText<"Desired format for -dump-ast output "
1348+
"('default', 'json', or 'json-zlib'); no format is guaranteed "
1349+
"stable across different compiler versions">,
1350+
MetaVarName<"<format>">,
1351+
Flags<[FrontendOption, NoInteractiveOption, DoesNotAffectIncrementalBuild]>;
13461352
def emit_ast : Flag<["-"], "emit-ast">, Alias<dump_ast>,
13471353
Flags<[FrontendOption, NoInteractiveOption, DoesNotAffectIncrementalBuild]>;
13481354
def dump_scope_maps : Separate<["-"], "dump-scope-maps">,

0 commit comments

Comments
 (0)