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 *)¶mLoc,
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 *)¶mLLoc,
170+ getArrayRef<ParamDecl *>(params), *(SourceLoc *)¶mRLoc);
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 (); }
0 commit comments