Skip to content

🍒[6.1] Add a flag to dump the AST as JSON. #78955

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions include/swift/AST/Attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -1709,6 +1709,12 @@ class ImplementsAttr : public DeclAttribute {
DeclName MemberName);

ProtocolDecl *getProtocol(DeclContext *dc) const;

/// Returns the protocol declaration containing the requirement being
/// implemented by the attributed declaration if it has already been computed,
/// otherwise `nullopt`. This should only be used for dumping.
std::optional<ProtocolDecl *> getCachedProtocol(DeclContext *dc) const;

TypeRepr *getProtocolTypeRepr() const { return TyR; }

DeclName getMemberName() const { return MemberName; }
Expand Down
5 changes: 5 additions & 0 deletions include/swift/AST/CatchNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ class CatchNode: public llvm::PointerUnion<
/// needs to be inferred.
Type getExplicitCaughtType(ASTContext &ctx) const;

/// Returns the explicitly-specified type error that will be caught by this
/// catch node, or `nullopt` if it has not yet been computed. This should only
/// be used for dumping.
std::optional<Type> getCachedExplicitCaughtType(ASTContext &ctx) const;

friend llvm::hash_code hash_value(CatchNode catchNode) {
using llvm::hash_value;
return hash_value(catchNode.getOpaqueValue());
Expand Down
16 changes: 16 additions & 0 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -3241,6 +3241,10 @@ class ValueDecl : public Decl {
/// Get the decl for this value's opaque result type, if it has one.
OpaqueTypeDecl *getOpaqueResultTypeDecl() const;

/// Gets the decl for this value's opaque result type if it has already been
/// computed, or `nullopt` otherwise. This should only be used for dumping.
std::optional<OpaqueTypeDecl *> getCachedOpaqueResultTypeDecl() const;

/// Get the representative for this value's opaque result type, if it has one.
/// Returns a `TypeRepr` instead of an `OpaqueReturnTypeRepr` because 'some'
/// types might appear in one or more structural positions, e.g. (some P,
Expand Down Expand Up @@ -7657,6 +7661,10 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
/// Retrieves the thrown interface type.
Type getThrownInterfaceType() const;

/// Returns the thrown interface type of this function if it has already been
/// computed, otherwise `nullopt`. This should only be used for dumping.
std::optional<Type> getCachedThrownInterfaceType() const;

/// Retrieve the "effective" thrown interface type, or std::nullopt if
/// this function cannot throw.
///
Expand Down Expand Up @@ -8217,6 +8225,10 @@ class FuncDecl : public AbstractFunctionDecl {
/// Retrieve the result interface type of this function.
Type getResultInterfaceType() const;

/// Returns the result interface type of this function if it has already been
/// computed, otherwise `nullopt`. This should only be used for dumping.
std::optional<Type> getCachedResultInterfaceType() const;

/// isUnaryOperator - Determine whether this is a unary operator
/// implementation. This check is a syntactic rather than type-based check,
/// which looks at the number of parameters specified, in order to allow
Expand Down Expand Up @@ -9417,6 +9429,10 @@ class MacroDecl : public GenericContext, public ValueDecl {
/// Retrieve the interface type produced when expanding this macro.
Type getResultInterfaceType() const;

/// Returns the result interface type of this macro if it has already been
/// computed, otherwise `nullopt`. This should only be used for dumping.
std::optional<Type> getCachedResultInterfaceType() const;

/// Determine the contexts in which this macro can be applied.
MacroRoles getMacroRoles() const;

Expand Down
10 changes: 10 additions & 0 deletions include/swift/AST/DiagnosticsFrontend.def
Original file line number Diff line number Diff line change
Expand Up @@ -589,5 +589,15 @@ ERROR(experimental_not_supported_in_production,none,
"experimental feature '%0' cannot be enabled in production compiler",
(StringRef))

ERROR(json_zlib_not_supported,none,
"this compiler was not built with zlib compression support enabled; "
"'-dump-ast-format json-zlib' cannot be used", ())

ERROR(ast_format_requires_dump_ast,none,
"structured AST formats are only supported when using -dump-ast", ())

ERROR(unknown_dump_ast_format,none,
"unknown format '%0' requested with '-dump-ast-format'", (StringRef))

#define UNDEFINE_DIAGNOSTIC_MACROS
#include "DefineDiagnosticMacros.h"
3 changes: 3 additions & 0 deletions include/swift/AST/SourceFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,9 @@ class SourceFile final : public FileUnit {
SWIFT_DEBUG_DUMP;
void dump(raw_ostream &os, bool parseIfNeeded = false) const;

/// Dumps this source file's AST in JSON format to the given output stream.
void dumpJSON(raw_ostream &os) const;

/// Pretty-print the contents of this source file.
///
/// \param Printer The AST printer used for printing the contents.
Expand Down
10 changes: 10 additions & 0 deletions include/swift/Frontend/FrontendOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,16 @@ class FrontendOptions {
/// -dump-scope-maps.
SmallVector<std::pair<unsigned, unsigned>, 2> DumpScopeMapLocations;

/// The possible output formats supported for dumping ASTs.
enum class ASTFormat {
Default, ///< S-expressions for debugging
JSON, ///< Structured JSON for analysis
JSONZlib, ///< Like JSON, but zlib-compressed
};

/// The output format generated by the `-dump-ast` flag.
ASTFormat DumpASTFormat = ASTFormat::Default;

/// Determines whether the static or shared resource folder is used.
/// When set to `true`, the default resource folder will be set to
/// '.../lib/swift', otherwise '.../lib/swift_static'.
Expand Down
6 changes: 6 additions & 0 deletions include/swift/Option/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -1343,6 +1343,12 @@ def emit_parse : Flag<["-"], "emit-parse">, Alias<dump_parse>,
def dump_ast : Flag<["-"], "dump-ast">,
HelpText<"Parse and type-check input file(s) and dump AST(s)">, ModeOpt,
Flags<[FrontendOption, NoInteractiveOption, DoesNotAffectIncrementalBuild]>;
def dump_ast_format : Separate<["-"], "dump-ast-format">,
HelpText<"Desired format for -dump-ast output "
"('default', 'json', or 'json-zlib'); no format is guaranteed "
"stable across different compiler versions">,
MetaVarName<"<format>">,
Flags<[FrontendOption, NoInteractiveOption, DoesNotAffectIncrementalBuild]>;
def emit_ast : Flag<["-"], "emit-ast">, Alias<dump_ast>,
Flags<[FrontendOption, NoInteractiveOption, DoesNotAffectIncrementalBuild]>;
def dump_scope_maps : Separate<["-"], "dump-scope-maps">,
Expand Down
Loading