Skip to content

Commit 7bc67c9

Browse files
committed
Add Swift-side infrastructure for ASTGen.
1 parent cc454f6 commit 7bc67c9

File tree

15 files changed

+401
-9
lines changed

15 files changed

+401
-9
lines changed

include/swift/AST/CASTBridging.h

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
//===--- ASTBridging.h - header for the swift SILBridging module ----------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef SWIFT_C_AST_ASTBRIDGING_H
14+
#define SWIFT_C_AST_ASTBRIDGING_H
15+
16+
// Provide macros to temporarily suppress warning about the use of
17+
// _Nullable and _Nonnull.
18+
#define SWIFT_BEGIN_NULLABILITY_ANNOTATIONS \
19+
_Pragma("clang diagnostic push") \
20+
_Pragma("clang diagnostic ignored \"-Wnullability-extension\"") \
21+
_Pragma("clang assume_nonnull begin")
22+
23+
#define SWIFT_END_NULLABILITY_ANNOTATIONS \
24+
_Pragma("clang diagnostic pop") _Pragma("clang assume_nonnull end")
25+
26+
//===----------------------------------------------------------------------===//
27+
// Diagnostic Engine
28+
//===----------------------------------------------------------------------===//
29+
30+
// TODO: Move this to somewhere common header.
31+
#if __has_attribute(enum_extensibility)
32+
#define ENUM_EXTENSIBILITY_ATTR(arg) __attribute__((enum_extensibility(arg)))
33+
#else
34+
#define ENUM_EXTENSIBILITY_ATTR(arg)
35+
#endif
36+
37+
// NOTE: This must be the same underlying value as C++ 'swift::DiagID' defined
38+
// in 'DiagnosticList.cpp'.
39+
typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedDiagID : unsigned {
40+
#define DIAG(KIND, ID, Options, Text, Signature) BridgedDiagID_##ID,
41+
#include "swift/AST/DiagnosticsAll.def"
42+
} BridgedDiagID;
43+
44+
SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
45+
46+
typedef long SwiftInt;
47+
typedef unsigned long SwiftUInt;
48+
49+
typedef struct {
50+
const void *_Nullable data;
51+
long numElements;
52+
} BridgedArrayRef;
53+
54+
typedef struct {
55+
void *start;
56+
SwiftInt byteLength;
57+
} BridgedCharSourceRange;
58+
59+
// FIXME: Can we bridge InFlightDiagnostic?
60+
void DiagnosticEngine_diagnose(void *, void *loc, BridgedDiagID diagID,
61+
BridgedArrayRef arguments,
62+
BridgedCharSourceRange highlight,
63+
BridgedArrayRef fixIts);
64+
65+
int DiagnosticEngine_hadAnyError(void *);
66+
67+
typedef const char *BridgedIdentifier;
68+
69+
#ifdef __cplusplus
70+
extern "C" {
71+
72+
#define _Bool bool
73+
74+
#endif
75+
76+
BridgedIdentifier
77+
SwiftASTContext_getIdentifier(void *ctx, const char *_Nullable str, long len);
78+
79+
void *SwiftImportDecl_create(void *, void *, void *, char, void *,
80+
BridgedArrayRef, BridgedArrayRef);
81+
82+
void *SwiftTopLevelCodeDecl_createStmt(void *ctx, void *DC, void *startLoc,
83+
void *element, void *endLoc);
84+
void *SwiftTopLevelCodeDecl_createExpr(void *ctx, void *DC, void *startLoc,
85+
void *element, void *endLoc);
86+
87+
void *SwiftSequenceExpr_create(void *ctx, BridgedArrayRef exprs);
88+
89+
void *SwiftTupleExpr_create(void *ctx, void *lparen, BridgedArrayRef subs,
90+
void *rparen);
91+
92+
void *SwiftFunctionCallExpr_create(void *ctx, void *fn, void *args);
93+
94+
void *SwiftIdentifierExpr_create(void *ctx, const char *base, void *loc);
95+
96+
void *SwiftStringLiteralExpr_create(void *ctx, const char *_Nullable string,
97+
long len, void *TokenLoc);
98+
99+
void *SwiftIntegerLiteralExpr_create(void *ctx, const char *_Nullable string,
100+
long len, void *TokenLoc);
101+
102+
void *SwiftVarDecl_create(void *ctx, const char *_Nullable name,
103+
void *loc, _Bool isStatic, _Bool isLet, void *dc);
104+
105+
void *IfStmt_create(void *ctx, void *ifLoc, void *cond, void *_Nullable then, void *_Nullable elseLoc,
106+
void *_Nullable elseStmt);
107+
108+
void *BraceStmt_create(void *ctx, void *lbloc, BridgedArrayRef elements, void *rbloc);
109+
110+
void *BridgedSourceLoc_advanced(void *loc, long len);
111+
112+
void *ParamDecl_create(void *ctx, void *loc,
113+
void *_Nullable argLoc, void *_Nullable argName,
114+
void *_Nullable paramLoc, void *_Nullable paramName,
115+
void *declContext);
116+
117+
void *FuncDecl_create(void *ctx, void *staticLoc, _Bool isStatic, void *funcLoc,
118+
const char *name, void *nameLoc,
119+
_Bool isAsync, void *_Nullable asyncLoc,
120+
_Bool throws, void *_Nullable throwsLoc,
121+
void *paramLLoc, BridgedArrayRef params, void *paramRLoc,
122+
void *_Nullable body, void *_Nullable returnType,
123+
void *declContext);
124+
125+
void *SimpleIdentTypeRepr_create(void *ctx, void *loc, const char *id);
126+
127+
void *UnresolvedDotExpr_create(void *ctx, void *base, void *dotLoc, const char *name, void *nameLoc);
128+
129+
void TopLevelCodeDecl_dump(void *);
130+
void Expr_dump(void *);
131+
void Decl_dump(void *);
132+
void Stmt_dump(void *);
133+
134+
#ifdef __cplusplus
135+
}
136+
#endif
137+
138+
SWIFT_END_NULLABILITY_ANNOTATIONS
139+
140+
#endif // SWIFT_C_AST_ASTBRIDGING_H

include/swift/Basic/LangOptions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,8 @@ namespace swift {
275275
/// disabled because it is not complete.
276276
bool EnableCXXInterop = false;
277277

278+
bool EnableSwiftParser = false;
279+
278280
/// Imports getters and setters as computed properties.
279281
bool CxxInteropGettersSettersAsProperties = false;
280282

include/swift/Option/Options.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,12 @@ def enable_experimental_cxx_interop_in_clang_header :
635635
Flags<[FrontendOption, NoDriverOption, HelpHidden]>,
636636
HelpText<"Enable experimental Swift to C++ interop code generation in generated Clang header">;
637637

638+
def enable_experimental_swift_parser :
639+
Flag<["-"], "enable-experimental-swift-parser">,
640+
Flags<[FrontendOption, HelpHidden, ModuleInterfaceOption]>,
641+
HelpText<"Enable the new and expiremental Swift parser + AST generation.">;
642+
643+
638644
def experimental_cxx_stdlib :
639645
Separate<["-"], "experimental-cxx-stdlib">,
640646
Flags<[HelpHidden]>,

include/swift/module.modulemap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ module ASTBridging {
1515
export *
1616
}
1717

18+
module CASTBridging {
19+
header "AST/CASTBridging.h"
20+
}
21+
1822
module SILBridging {
1923
header "SIL/SILBridging.h"
2024
header "SIL/SILLocation.h"

lib/AST/CASTBridging.cpp

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
#include "swift/AST/CASTBridging.h"
2+
3+
#include "swift/AST/ASTContext.h"
4+
#include "swift/AST/ASTNode.h"
5+
#include "swift/AST/Decl.h"
6+
#include "swift/AST/Expr.h"
7+
#include "swift/AST/Stmt.h"
8+
#include "swift/AST/Identifier.h"
9+
#include "swift/AST/ParameterList.h"
10+
#include "swift/AST/TypeRepr.h"
11+
12+
using namespace swift;
13+
14+
template <typename T>
15+
inline llvm::ArrayRef<T> getArrayRef(BridgedArrayRef bridged) {
16+
return {static_cast<const T *>(bridged.data), size_t(bridged.numElements)};
17+
}
18+
19+
BridgedIdentifier
20+
SwiftASTContext_getIdentifier(void *ctx, const char *_Nullable str, long len) {
21+
return static_cast<ASTContext *>(ctx)
22+
->getIdentifier(StringRef{str, size_t(len)})
23+
.get();
24+
}
25+
26+
void *SwiftImportDecl_create(void *ctx, void *dc, void *importLoc, char kind,
27+
void *kindLoc, BridgedArrayRef path,
28+
BridgedArrayRef pathLocs) {
29+
assert(path.numElements == pathLocs.numElements);
30+
ASTContext &Context = *static_cast<ASTContext *>(ctx);
31+
ImportPath::Builder importPath;
32+
for (auto p : llvm::zip(getArrayRef<Identifier>(path),
33+
getArrayRef<SourceLoc>(pathLocs))) {
34+
Identifier ident;
35+
SourceLoc loc;
36+
std::tie(ident, loc) = p;
37+
importPath.push_back(ident, loc);
38+
}
39+
return ImportDecl::create(
40+
Context, static_cast<DeclContext *>(dc), *(SourceLoc *)&importLoc,
41+
static_cast<ImportKind>(kind), *(SourceLoc *)&kindLoc,
42+
std::move(importPath).get());
43+
}
44+
45+
void *BridgedSourceLoc_advanced(void *loc, long len) {
46+
SourceLoc l = ((SourceLoc *)&loc)->getAdvancedLoc(len);
47+
return &l; // TODO: what?
48+
}
49+
50+
void *SwiftTopLevelCodeDecl_createStmt(void *ctx, void *DC, void *startLoc,
51+
void *element, void *endLoc) {
52+
ASTContext &Context = *static_cast<ASTContext *>(ctx);
53+
auto *S = static_cast<Stmt *>(element);
54+
auto Brace =
55+
BraceStmt::create(Context, *(SourceLoc *)&startLoc,
56+
{S}, *(SourceLoc *)&endLoc,
57+
/*Implicit=*/true);
58+
auto *TLCD =
59+
new (Context) TopLevelCodeDecl(static_cast<DeclContext *>(DC), Brace);
60+
return (Decl *)TLCD;
61+
}
62+
63+
void *SwiftTopLevelCodeDecl_createExpr(void *ctx, void *DC, void *startLoc,
64+
void *element, void *endLoc) {
65+
ASTContext &Context = *static_cast<ASTContext *>(ctx);
66+
auto *E = static_cast<Expr *>(element);
67+
auto Brace =
68+
BraceStmt::create(Context, *(SourceLoc *)&startLoc,
69+
{E}, *(SourceLoc *)&endLoc,
70+
/*Implicit=*/true);
71+
auto *TLCD =
72+
new (Context) TopLevelCodeDecl(static_cast<DeclContext *>(DC), Brace);
73+
return (Decl *)TLCD;
74+
}
75+
76+
void *SwiftSequenceExpr_create(void *ctx, BridgedArrayRef exprs) {
77+
return SequenceExpr::create(*static_cast<ASTContext *>(ctx),
78+
getArrayRef<Expr *>(exprs));
79+
}
80+
81+
void *SwiftTupleExpr_create(void *ctx, void *lparen, BridgedArrayRef subs,
82+
void *rparen) {
83+
return TupleExpr::create(
84+
*static_cast<ASTContext *>(ctx), *(SourceLoc *)&lparen,
85+
getArrayRef<Expr *>(subs), {}, {}, *(SourceLoc *)&rparen,
86+
/*Implicit*/ false);
87+
}
88+
89+
void *SwiftFunctionCallExpr_create(void *ctx, void *fn, void *args) {
90+
ASTContext &Context = *static_cast<ASTContext *>(ctx);
91+
TupleExpr *TE = static_cast<TupleExpr *>(args);
92+
SmallVector<Argument, 8> arguments;
93+
for (unsigned i = 0; i < TE->getNumElements(); ++i) {
94+
arguments.emplace_back(TE->getElementNameLoc(i), TE->getElementName(i),
95+
TE->getElement(i));
96+
}
97+
auto *argList = ArgumentList::create(Context, TE->getLParenLoc(), arguments,
98+
TE->getRParenLoc(), None,
99+
/*isImplicit*/ false);
100+
return CallExpr::create(Context, static_cast<Expr *>(fn), argList,
101+
/*implicit*/ false);
102+
}
103+
104+
void *SwiftIdentifierExpr_create(void *ctx, const char *base, void *loc) {
105+
ASTContext &Context = *static_cast<ASTContext *>(ctx);
106+
auto name = DeclNameRef{
107+
swift::Identifier::getFromOpaquePointer(const_cast<char *>(base))};
108+
Expr *E = new (Context) UnresolvedDeclRefExpr(
109+
name, DeclRefKind::Ordinary, DeclNameLoc{*(SourceLoc *)&loc});
110+
return E;
111+
}
112+
113+
void *SwiftStringLiteralExpr_create(void *ctx, const char *_Nullable string,
114+
long len, void *TokenLoc) {
115+
ASTContext &Context = *static_cast<ASTContext *>(ctx);
116+
return new (Context) StringLiteralExpr(StringRef{string, size_t(len)},
117+
*(SourceLoc *)&TokenLoc);
118+
}
119+
120+
void *SwiftIntegerLiteralExpr_create(void *ctx, const char *_Nullable string,
121+
long len, void *TokenLoc) {
122+
ASTContext &Context = *static_cast<ASTContext *>(ctx);
123+
return new (Context) IntegerLiteralExpr(StringRef{string, size_t(len)},
124+
*(SourceLoc *)&TokenLoc);
125+
}
126+
127+
void *SwiftVarDecl_create(void *ctx, const char *_Nullable nameId,
128+
void *loc, bool isStatic, bool isLet, void *dc) {
129+
ASTContext &Context = *static_cast<ASTContext *>(ctx);
130+
return new (Context) VarDecl(isStatic,
131+
isLet ? VarDecl::Introducer::Let : VarDecl::Introducer::Var,
132+
*(SourceLoc *)&loc, Identifier::getFromOpaquePointer((void *)nameId),
133+
reinterpret_cast<DeclContext *>(dc));
134+
}
135+
136+
void *IfStmt_create(void *ctx, void *ifLoc, void *cond, void *_Nullable then, void *_Nullable elseLoc,
137+
void *_Nullable elseStmt) {
138+
ASTContext &Context = *static_cast<ASTContext *>(ctx);
139+
return new (Context) IfStmt(*(SourceLoc *)&ifLoc, (Expr *)cond, (Stmt *)then, *(SourceLoc *)&elseLoc,
140+
(Stmt *)elseStmt, None, Context);
141+
}
142+
143+
void *BraceStmt_create(void *ctx, void *lbloc, BridgedArrayRef elements, void *rbloc) {
144+
return BraceStmt::create(*static_cast<ASTContext *>(ctx), *(SourceLoc *)&lbloc,
145+
getArrayRef<ASTNode>(elements),
146+
*(SourceLoc *)&rbloc);
147+
}
148+
149+
void *ParamDecl_create(void *ctx, void *loc,
150+
void *_Nullable argLoc, void *_Nullable argName,
151+
void *_Nullable paramLoc, void *_Nullable paramName,
152+
void *declContext) {
153+
ASTContext &Context = *static_cast<ASTContext *>(ctx);
154+
return new (Context) ParamDecl(*(SourceLoc *)&loc, *(SourceLoc *)&argLoc,
155+
Identifier::getFromOpaquePointer(argName),
156+
*(SourceLoc *)&paramLoc,
157+
Identifier::getFromOpaquePointer(paramName),
158+
(DeclContext *)declContext);
159+
}
160+
161+
void *FuncDecl_create(void *ctx, void *staticLoc, bool isStatic, void *funcLoc,
162+
const char *name, void *nameLoc,
163+
bool isAsync, void *_Nullable asyncLoc,
164+
bool throws, void *_Nullable throwsLoc,
165+
void *paramLLoc, BridgedArrayRef params, void *paramRLoc,
166+
void *_Nullable body, void *_Nullable returnType,
167+
void *declContext) {
168+
auto *paramList = ParameterList::create(
169+
*static_cast<ASTContext *>(ctx), *(SourceLoc *)&paramLLoc,
170+
getArrayRef<ParamDecl *>(params), *(SourceLoc *)&paramRLoc);
171+
auto declName =
172+
DeclName(*static_cast<ASTContext *>(ctx),
173+
Identifier::getFromOpaquePointer((void *)name), paramList);
174+
auto *out = FuncDecl::create(
175+
*static_cast<ASTContext *>(ctx), *(SourceLoc *)&staticLoc,
176+
isStatic ? StaticSpellingKind::KeywordStatic : StaticSpellingKind::None,
177+
*(SourceLoc *)&funcLoc, declName, *(SourceLoc *)&nameLoc, isAsync,
178+
*(SourceLoc *)&asyncLoc, throws, *(SourceLoc *)&throwsLoc, nullptr,
179+
paramList, (TypeRepr *)returnType, (DeclContext *)declContext);
180+
out->setBody((BraceStmt *)body, FuncDecl::BodyKind::Parsed);
181+
182+
return static_cast<Decl *>(out);
183+
}
184+
185+
void *SimpleIdentTypeRepr_create(void *ctx, void *loc, const char *id) {
186+
ASTContext &Context = *static_cast<ASTContext *>(ctx);
187+
return new (Context) SimpleIdentTypeRepr(DeclNameLoc(*(SourceLoc *)&loc),
188+
DeclNameRef(Identifier::getFromOpaquePointer((void *)id)));
189+
}
190+
191+
void *UnresolvedDotExpr_create(void *ctx, void *base, void *dotLoc, const char *name, void *nameLoc) {
192+
ASTContext &Context = *static_cast<ASTContext *>(ctx);
193+
return new (Context) UnresolvedDotExpr((Expr *)base, *(SourceLoc *)&dotLoc,
194+
DeclNameRef(Identifier::getFromOpaquePointer((void *)name)),
195+
DeclNameLoc(*(SourceLoc *)&nameLoc), false);
196+
}
197+
198+
void TopLevelCodeDecl_dump(void *decl) { ((TopLevelCodeDecl *)decl)->dump(); }
199+
200+
void Expr_dump(void *expr) { ((Expr *)expr)->dump(); }
201+
void Decl_dump(void *expr) { ((Decl *)expr)->dump(); }
202+
void Stmt_dump(void *expr) { ((Stmt *)expr)->dump(); }

lib/AST/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ add_swift_host_library(swiftAST STATIC
3333
Availability.cpp
3434
AvailabilitySpec.cpp
3535
Builtins.cpp
36+
CASTBridging.cpp
3637
CaptureInfo.cpp
3738
ClangSwiftTypeCorrespondence.cpp
3839
ClangTypeConverter.cpp
@@ -155,7 +156,7 @@ endif()
155156

156157
target_link_libraries(swiftAST
157158
PUBLIC swiftBasic
158-
PRIVATE swiftSyntax)
159+
PRIVATE legacySwiftSyntax)
159160
if(SWIFT_BUILD_ONLY_SYNTAXPARSERLIB)
160161
# Remove dependencies from clangBasic to avoid bringing along some llvm
161162
# libraries that we don't need to be building.

0 commit comments

Comments
 (0)