Skip to content

Commit 9907afa

Browse files
authored
Merge pull request #78463 from allevato/json-ast-new
Add a flag to dump the AST as JSON.
2 parents ed71c20 + d2fd347 commit 9907afa

22 files changed

+3255
-1528
lines changed

include/swift/AST/Attr.h

+6
Original file line numberDiff line numberDiff line change
@@ -1744,6 +1744,12 @@ class ImplementsAttr : public DeclAttribute {
17441744
DeclName MemberName);
17451745

17461746
ProtocolDecl *getProtocol(DeclContext *dc) const;
1747+
1748+
/// Returns the protocol declaration containing the requirement being
1749+
/// implemented by the attributed declaration if it has already been computed,
1750+
/// otherwise `nullopt`. This should only be used for dumping.
1751+
std::optional<ProtocolDecl *> getCachedProtocol(DeclContext *dc) const;
1752+
17471753
TypeRepr *getProtocolTypeRepr() const { return TyR; }
17481754

17491755
DeclName getMemberName() const { return MemberName; }

include/swift/AST/CatchNode.h

+5
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

+16
Original file line numberDiff line numberDiff line change
@@ -3326,6 +3326,10 @@ class ValueDecl : public Decl {
33263326
/// Get the decl for this value's opaque result type, if it has one.
33273327
OpaqueTypeDecl *getOpaqueResultTypeDecl() const;
33283328

3329+
/// Gets the decl for this value's opaque result type if it has already been
3330+
/// computed, or `nullopt` otherwise. This should only be used for dumping.
3331+
std::optional<OpaqueTypeDecl *> getCachedOpaqueResultTypeDecl() const;
3332+
33293333
/// Get the representative for this value's opaque result type, if it has one.
33303334
/// Returns a `TypeRepr` instead of an `OpaqueReturnTypeRepr` because 'some'
33313335
/// types might appear in one or more structural positions, e.g. (some P,
@@ -7769,6 +7773,10 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
77697773
/// Retrieves the thrown interface type.
77707774
Type getThrownInterfaceType() const;
77717775

7776+
/// Returns the thrown interface type of this function if it has already been
7777+
/// computed, otherwise `nullopt`. This should only be used for dumping.
7778+
std::optional<Type> getCachedThrownInterfaceType() const;
7779+
77727780
/// Retrieve the "effective" thrown interface type, or std::nullopt if
77737781
/// this function cannot throw.
77747782
///
@@ -8329,6 +8337,10 @@ class FuncDecl : public AbstractFunctionDecl {
83298337
/// Retrieve the result interface type of this function.
83308338
Type getResultInterfaceType() const;
83318339

8340+
/// Returns the result interface type of this function if it has already been
8341+
/// computed, otherwise `nullopt`. This should only be used for dumping.
8342+
std::optional<Type> getCachedResultInterfaceType() const;
8343+
83328344
/// isUnaryOperator - Determine whether this is a unary operator
83338345
/// implementation. This check is a syntactic rather than type-based check,
83348346
/// which looks at the number of parameters specified, in order to allow
@@ -9529,6 +9541,10 @@ class MacroDecl : public GenericContext, public ValueDecl {
95299541
/// Retrieve the interface type produced when expanding this macro.
95309542
Type getResultInterfaceType() const;
95319543

9544+
/// Returns the result interface type of this macro if it has already been
9545+
/// computed, otherwise `nullopt`. This should only be used for dumping.
9546+
std::optional<Type> getCachedResultInterfaceType() const;
9547+
95329548
/// Determine the contexts in which this macro can be applied.
95339549
MacroRoles getMacroRoles() const;
95349550

include/swift/AST/DiagnosticsFrontend.def

+10
Original file line numberDiff line numberDiff line change
@@ -603,5 +603,15 @@ GROUPED_WARNING(command_line_conflicts_with_strict_safety,Unsafe,none,
603603
"'%0' is not memory-safe and should not be combined with "
604604
"strict memory safety checking", (StringRef))
605605

606+
ERROR(json_zlib_not_supported,none,
607+
"this compiler was not built with zlib compression support enabled; "
608+
"'-dump-ast-format json-zlib' cannot be used", ())
609+
610+
ERROR(ast_format_requires_dump_ast,none,
611+
"structured AST formats are only supported when using -dump-ast", ())
612+
613+
ERROR(unknown_dump_ast_format,none,
614+
"unknown format '%0' requested with '-dump-ast-format'", (StringRef))
615+
606616
#define UNDEFINE_DIAGNOSTIC_MACROS
607617
#include "DefineDiagnosticMacros.h"

include/swift/AST/SourceFile.h

+3
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,9 @@ class SourceFile final : public FileUnit {
686686
SWIFT_DEBUG_DUMP;
687687
void dump(raw_ostream &os, bool parseIfNeeded = false) const;
688688

689+
/// Dumps this source file's AST in JSON format to the given output stream.
690+
void dumpJSON(raw_ostream &os) const;
691+
689692
/// Pretty-print the contents of this source file.
690693
///
691694
/// \param Printer The AST printer used for printing the contents.

include/swift/Frontend/FrontendOptions.h

+10
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

+6
Original file line numberDiff line numberDiff line change
@@ -1350,6 +1350,12 @@ def emit_parse : Flag<["-"], "emit-parse">, Alias<dump_parse>,
13501350
def dump_ast : Flag<["-"], "dump-ast">,
13511351
HelpText<"Parse and type-check input file(s) and dump AST(s)">, ModeOpt,
13521352
Flags<[FrontendOption, NoInteractiveOption, DoesNotAffectIncrementalBuild]>;
1353+
def dump_ast_format : Separate<["-"], "dump-ast-format">,
1354+
HelpText<"Desired format for -dump-ast output "
1355+
"('default', 'json', or 'json-zlib'); no format is guaranteed "
1356+
"stable across different compiler versions">,
1357+
MetaVarName<"<format>">,
1358+
Flags<[FrontendOption, NoInteractiveOption, DoesNotAffectIncrementalBuild]>;
13531359
def emit_ast : Flag<["-"], "emit-ast">, Alias<dump_ast>,
13541360
Flags<[FrontendOption, NoInteractiveOption, DoesNotAffectIncrementalBuild]>;
13551361
def dump_scope_maps : Separate<["-"], "dump-scope-maps">,

0 commit comments

Comments
 (0)