Skip to content

[🦩] Add swiftASTGen library to build a C++ AST from a SwiftSyntax parse tree #61426

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 29 commits into from
Oct 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
80200ec
Add Swift-side infrastructure for ASTGen.
zoecarver Sep 3, 2022
129fd38
Move ASTGen into Swift from swift-syntax.
zoecarver Sep 7, 2022
73a7c0e
Apply Doug's review comments
zoecarver Sep 7, 2022
bed2742
Make ASTGen a swift package
zoecarver Sep 11, 2022
f7d501d
Add support for structs/classes.
zoecarver Sep 11, 2022
94f890d
[nfc] Split ASTGen into files + extensions.
zoecarver Sep 11, 2022
93f1505
Add support for bool literals.
zoecarver Sep 16, 2022
73fe413
Support functions with statements in their body.
zoecarver Sep 16, 2022
e21c463
Remove unnecessary frontend option
DougGregor Oct 2, 2022
2560aed
Rename experimental feature to ParserASTGen
DougGregor Oct 2, 2022
4da85ea
Clean up build logic for ASTGen library.
DougGregor Oct 3, 2022
c9f7060
Use ASTGen with the new Swift parser when requested
DougGregor Oct 3, 2022
8e66286
Fix the package that builds swiftASTGen.
DougGregor Oct 3, 2022
39f54bc
Use add_library for Swift ASTGen library
DougGregor Oct 4, 2022
9c00a0f
Fix CASTBridging.h for non-Clang host compilers
DougGregor Oct 4, 2022
54bcb33
[CMake] Make sure swiftASTGen links swiftAST.
DougGregor Oct 6, 2022
7483373
[ASTGen] Use UTF-8 rather than C `char`, and use BridgedIdentifier co…
DougGregor Oct 6, 2022
2f61489
Don't try to bridge diagnostics from C++ to Swift
DougGregor Oct 6, 2022
227bc1a
[ASTGen] Clean up casting with source locations
DougGregor Oct 6, 2022
d4e7371
[ASTGen] Make sure we spin the C++ parser even when we don't use it.
DougGregor Oct 6, 2022
8d7f4df
Remove xcworkspacedata and UserInterfaceState.xcuserstate
DougGregor Oct 6, 2022
f516223
[ASTGen] Do a little property-wrapper dance to move DeclContext into …
DougGregor Oct 6, 2022
c95b6c4
[ASTGen] Fix integer and string literal mapping
DougGregor Oct 6, 2022
a449dc1
Bring back an important assertion
DougGregor Oct 6, 2022
5a0d775
[ASTGen] Add a comment about the purporse of its Package.swift
DougGregor Oct 6, 2022
4b4751c
[CMake] Only add swiftCore dependency when the new Swift parser is in…
DougGregor Oct 6, 2022
7478df0
[CMake] Ensure that the SourceKit framework links Swift as needed
DougGregor Oct 7, 2022
7fd7e4f
[CMake Export swiftASTGen so the LLDB target can use it
DougGregor Oct 7, 2022
aed4f74
[ASTGen] Set the target triple for swiftASTGen.
DougGregor Oct 8, 2022
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
144 changes: 144 additions & 0 deletions include/swift/AST/CASTBridging.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
//===--- ASTBridging.h - header for the swift SILBridging module ----------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_C_AST_ASTBRIDGING_H
#define SWIFT_C_AST_ASTBRIDGING_H

#include <inttypes.h>

#if __clang__
// Provide macros to temporarily suppress warning about the use of
// _Nullable and _Nonnull.
#define SWIFT_BEGIN_NULLABILITY_ANNOTATIONS \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wnullability-extension\"") \
_Pragma("clang assume_nonnull begin")

#define SWIFT_END_NULLABILITY_ANNOTATIONS \
_Pragma("clang diagnostic pop") _Pragma("clang assume_nonnull end")
#else
#define SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
#define SWIFT_END_NULLABILITY_ANNOTATIONS
#define _Nullable
#endif

SWIFT_BEGIN_NULLABILITY_ANNOTATIONS

typedef long SwiftInt;
typedef unsigned long SwiftUInt;

typedef struct {
const void *_Nullable data;
long numElements;
} BridgedArrayRef;

typedef struct {
void *start;
SwiftInt byteLength;
} BridgedCharSourceRange;

typedef void *BridgedIdentifier;

#ifdef __cplusplus
extern "C" {

#define _Bool bool

#endif

BridgedIdentifier
SwiftASTContext_getIdentifier(
void *ctx, const uint8_t * _Nullable str, long len);

void *SwiftImportDecl_create(void *, void *, void *, char, void *,
BridgedArrayRef, BridgedArrayRef);

void *SwiftTopLevelCodeDecl_createStmt(void *ctx, void *DC, void *startLoc,
void *element, void *endLoc);
void *SwiftTopLevelCodeDecl_createExpr(void *ctx, void *DC, void *startLoc,
void *element, void *endLoc);

void *SwiftSequenceExpr_create(void *ctx, BridgedArrayRef exprs);

void *SwiftTupleExpr_create(void *ctx, void *lparen, BridgedArrayRef subs,
void *rparen);

void *SwiftFunctionCallExpr_create(void *ctx, void *fn, void *args);

void *SwiftIdentifierExpr_create(void *ctx, BridgedIdentifier base, void *loc);

void *SwiftStringLiteralExpr_create(void *ctx, const uint8_t * _Nullable string,
long len, void *TokenLoc);

void *SwiftIntegerLiteralExpr_create(void *ctx, const uint8_t * _Nullable string,
long len, void *TokenLoc);

void *SwiftBooleanLiteralExpr_create(void *ctx, _Bool value, void *TokenLoc);

void *SwiftVarDecl_create(void *ctx, BridgedIdentifier _Nullable name,
void *loc, _Bool isStatic, _Bool isLet, void *dc);

void *IfStmt_create(void *ctx, void *ifLoc, void *cond, void *_Nullable then, void *_Nullable elseLoc,
void *_Nullable elseStmt);

void *BraceStmt_createExpr(void *ctx, void *lbloc, BridgedArrayRef elements, void *rbloc);
void *BraceStmt_createStmt(void *ctx, void *lbloc, BridgedArrayRef elements, void *rbloc);

void *BridgedSourceLoc_advanced(void *loc, long len);

void *ParamDecl_create(void *ctx, void *loc,
void *_Nullable argLoc, void *_Nullable argName,
void *_Nullable paramLoc, void *_Nullable paramName,
void *declContext);

void *FuncDecl_create(void *ctx, void *staticLoc, _Bool isStatic, void *funcLoc,
BridgedIdentifier name, void *nameLoc,
_Bool isAsync, void *_Nullable asyncLoc,
_Bool throws, void *_Nullable throwsLoc,
void *paramLLoc, BridgedArrayRef params, void *paramRLoc,
void *_Nullable body, void *_Nullable returnType,
void *declContext);

void *SimpleIdentTypeRepr_create(void *ctx, void *loc, BridgedIdentifier id);

void *UnresolvedDotExpr_create(void *ctx, void *base, void *dotLoc, BridgedIdentifier name, void *nameLoc);

void *ClosureExpr_create(void *ctx, void *body, void *dc);

void NominalTypeDecl_setMembers(void *decl, BridgedArrayRef members);

struct DeclContextAndDecl {
void *declContext;
void *nominalDecl;
void *decl;
};

struct DeclContextAndDecl StructDecl_create(
void *ctx, void *loc, BridgedIdentifier name, void *nameLoc, void *dc);
struct DeclContextAndDecl ClassDecl_create(
void *ctx, void *loc, BridgedIdentifier name, void *nameLoc, void *dc);

void TopLevelCodeDecl_dump(void *);
void Expr_dump(void *);
void Decl_dump(void *);
void Stmt_dump(void *);

#ifdef __cplusplus
}
#endif

SWIFT_END_NULLABILITY_ANNOTATIONS

#undef SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
#undef SWIFT_END_NULLABILITY_ANNOTATIONS

#endif // SWIFT_C_AST_ASTBRIDGING_H
4 changes: 4 additions & 0 deletions include/swift/Basic/Features.def
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ EXPERIMENTAL_FEATURE(ParserSequenceFolding)
/// Enables implicit some while also enabling existential `any`
EXPERIMENTAL_FEATURE(ImplicitSome)

/// Parse using the Swift (swift-syntax) parser and use ASTGen to generate the
/// corresponding syntax tree.
EXPERIMENTAL_FEATURE(ParserASTGen)

#undef EXPERIMENTAL_FEATURE
#undef UPCOMING_FEATURE
#undef SUPPRESSIBLE_LANGUAGE_FEATURE
Expand Down
4 changes: 4 additions & 0 deletions include/swift/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ module ASTBridging {
export *
}

module CASTBridging {
header "AST/CASTBridging.h"
}

module SILBridging {
header "SIL/SILBridging.h"
header "SIL/SILLocation.h"
Expand Down
4 changes: 4 additions & 0 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3091,6 +3091,10 @@ static bool usesFeatureAdditiveArithmeticDerivedConformances(Decl *decl) {
return false;
}

static bool usesFeatureParserASTGen(Decl *decl) {
return false;
}

static void
suppressingFeatureNoAsyncAvailability(PrintOptions &options,
llvm::function_ref<void()> action) {
Expand Down
Loading