@@ -16,6 +16,11 @@ inline llvm::ArrayRef<T> getArrayRef(BridgedArrayRef bridged) {
16
16
return {static_cast <const T *>(bridged.data ), size_t (bridged.numElements )};
17
17
}
18
18
19
+ static SourceLoc getSourceLocFromPointer (void *loc) {
20
+ auto smLoc = llvm::SMLoc::getFromPointer ((const char *)loc);
21
+ return SourceLoc (smLoc);
22
+ }
23
+
19
24
BridgedIdentifier
20
25
SwiftASTContext_getIdentifier (void *ctx, const uint8_t *_Nullable str, long len) {
21
26
return const_cast <void *>(
@@ -39,23 +44,24 @@ void *SwiftImportDecl_create(void *ctx, void *dc, void *importLoc, char kind,
39
44
importPath.push_back (ident, loc);
40
45
}
41
46
return ImportDecl::create (
42
- Context, static_cast <DeclContext *>(dc), *(SourceLoc *)&importLoc,
43
- static_cast <ImportKind>(kind), *(SourceLoc *)&kindLoc,
47
+ Context, static_cast <DeclContext *>(dc),
48
+ getSourceLocFromPointer (importLoc),
49
+ static_cast <ImportKind>(kind), getSourceLocFromPointer (kindLoc),
44
50
std::move (importPath).get ());
45
51
}
46
52
47
53
void *BridgedSourceLoc_advanced (void *loc, long len) {
48
- SourceLoc l = ((SourceLoc *)& loc)-> getAdvancedLoc (len);
49
- return &l; // TODO: what?
54
+ SourceLoc l = getSourceLocFromPointer ( loc). getAdvancedLoc (len);
55
+ return const_cast < void *>(l. getOpaquePointerValue ());
50
56
}
51
57
52
58
void *SwiftTopLevelCodeDecl_createStmt (void *ctx, void *DC, void *startLoc,
53
59
void *element, void *endLoc) {
54
60
ASTContext &Context = *static_cast <ASTContext *>(ctx);
55
61
auto *S = static_cast <Stmt *>(element);
56
62
auto Brace =
57
- BraceStmt::create (Context, *(SourceLoc *)& startLoc,
58
- {S}, *(SourceLoc *)& endLoc,
63
+ BraceStmt::create (Context, getSourceLocFromPointer ( startLoc) ,
64
+ {S}, getSourceLocFromPointer ( endLoc) ,
59
65
/* Implicit=*/ true );
60
66
auto *TLCD =
61
67
new (Context) TopLevelCodeDecl (static_cast <DeclContext *>(DC), Brace);
@@ -67,8 +73,8 @@ void *SwiftTopLevelCodeDecl_createExpr(void *ctx, void *DC, void *startLoc,
67
73
ASTContext &Context = *static_cast <ASTContext *>(ctx);
68
74
auto *E = static_cast <Expr *>(element);
69
75
auto Brace =
70
- BraceStmt::create (Context, *(SourceLoc *)& startLoc,
71
- {E}, *(SourceLoc *)& endLoc,
76
+ BraceStmt::create (Context, getSourceLocFromPointer ( startLoc) ,
77
+ {E}, getSourceLocFromPointer ( endLoc) ,
72
78
/* Implicit=*/ true );
73
79
auto *TLCD =
74
80
new (Context) TopLevelCodeDecl (static_cast <DeclContext *>(DC), Brace);
@@ -83,8 +89,8 @@ void *SwiftSequenceExpr_create(void *ctx, BridgedArrayRef exprs) {
83
89
void *SwiftTupleExpr_create (void *ctx, void *lparen, BridgedArrayRef subs,
84
90
void *rparen) {
85
91
return TupleExpr::create (
86
- *static_cast <ASTContext *>(ctx), *(SourceLoc *)& lparen,
87
- getArrayRef<Expr *>(subs), {}, {}, *(SourceLoc *)& rparen,
92
+ *static_cast <ASTContext *>(ctx), getSourceLocFromPointer ( lparen) ,
93
+ getArrayRef<Expr *>(subs), {}, {}, getSourceLocFromPointer ( rparen) ,
88
94
/* Implicit*/ false );
89
95
}
90
96
@@ -108,7 +114,7 @@ void *SwiftIdentifierExpr_create(void *ctx, BridgedIdentifier base, void *loc) {
108
114
auto name = DeclNameRef{
109
115
swift::Identifier::getFromOpaquePointer (base)};
110
116
Expr *E = new (Context) UnresolvedDeclRefExpr (
111
- name, DeclRefKind::Ordinary, DeclNameLoc{*(SourceLoc *)& loc});
117
+ name, DeclRefKind::Ordinary, DeclNameLoc{getSourceLocFromPointer ( loc) });
112
118
return E;
113
119
}
114
120
@@ -118,43 +124,46 @@ void *SwiftStringLiteralExpr_create(
118
124
ASTContext &Context = *static_cast <ASTContext *>(ctx);
119
125
return new (Context) StringLiteralExpr (
120
126
StringRef{reinterpret_cast <const char *>(string), size_t (len)},
121
- *(SourceLoc *)& TokenLoc);
127
+ getSourceLocFromPointer ( TokenLoc) );
122
128
}
123
129
124
130
void *SwiftIntegerLiteralExpr_create (
125
131
void *ctx, const uint8_t *_Nullable string, long len, void *TokenLoc) {
126
132
ASTContext &Context = *static_cast <ASTContext *>(ctx);
127
133
return new (Context) IntegerLiteralExpr (
128
134
StringRef{reinterpret_cast <const char *>(string), size_t (len)},
129
- *(SourceLoc *)& TokenLoc);
135
+ getSourceLocFromPointer ( TokenLoc) );
130
136
}
131
137
132
138
void *SwiftBooleanLiteralExpr_create (void *ctx, bool value, void *TokenLoc) {
133
139
ASTContext &Context = *static_cast <ASTContext *>(ctx);
134
- return new (Context) BooleanLiteralExpr (value, *(SourceLoc *)&TokenLoc);
140
+ return new (Context) BooleanLiteralExpr (
141
+ value, getSourceLocFromPointer (TokenLoc));
135
142
}
136
143
137
144
void *SwiftVarDecl_create (void *ctx, BridgedIdentifier _Nullable nameId,
138
145
void *loc, bool isStatic, bool isLet, void *dc) {
139
146
ASTContext &Context = *static_cast <ASTContext *>(ctx);
140
147
return new (Context) VarDecl (isStatic,
141
148
isLet ? VarDecl::Introducer::Let : VarDecl::Introducer::Var,
142
- *(SourceLoc *)&loc, Identifier::getFromOpaquePointer (nameId),
149
+ getSourceLocFromPointer (loc),
150
+ Identifier::getFromOpaquePointer (nameId),
143
151
reinterpret_cast <DeclContext *>(dc));
144
152
}
145
153
146
154
void *IfStmt_create (void *ctx, void *ifLoc, void *cond, void *_Nullable then, void *_Nullable elseLoc,
147
155
void *_Nullable elseStmt) {
148
156
ASTContext &Context = *static_cast <ASTContext *>(ctx);
149
- return new (Context) IfStmt (*(SourceLoc *)&ifLoc, (Expr *)cond, (Stmt *)then, *(SourceLoc *)&elseLoc,
150
- (Stmt *)elseStmt, None, Context);
157
+ return new (Context) IfStmt (
158
+ getSourceLocFromPointer (ifLoc), (Expr *)cond, (Stmt *)then,
159
+ getSourceLocFromPointer (elseLoc), (Stmt *)elseStmt, None, Context);
151
160
}
152
161
153
162
void *BraceStmt_createExpr (void *ctx, void *lbloc, BridgedArrayRef elements, void *rbloc) {
154
163
ASTContext &Context = *static_cast <ASTContext *>(ctx);
155
- return BraceStmt::create (Context, *(SourceLoc *)& lbloc,
164
+ return BraceStmt::create (Context, getSourceLocFromPointer ( lbloc) ,
156
165
getArrayRef<ASTNode>(elements),
157
- *(SourceLoc *)& rbloc);
166
+ getSourceLocFromPointer ( rbloc) );
158
167
}
159
168
160
169
void *BraceStmt_createStmt (void *ctx, void *lbloc, BridgedArrayRef elements, void *rbloc) {
@@ -164,9 +173,9 @@ void *BraceStmt_createStmt(void *ctx, void *lbloc, BridgedArrayRef elements, voi
164
173
}
165
174
166
175
ASTContext &Context = *static_cast <ASTContext *>(ctx);
167
- return BraceStmt::create (Context, *(SourceLoc *)& lbloc,
176
+ return BraceStmt::create (Context, getSourceLocFromPointer ( lbloc) ,
168
177
Context.AllocateCopy (nodes),
169
- *(SourceLoc *)& rbloc);
178
+ getSourceLocFromPointer ( rbloc) );
170
179
}
171
180
172
181
void *ParamDecl_create (
@@ -175,9 +184,10 @@ void *ParamDecl_create(
175
184
void *_Nullable paramLoc, BridgedIdentifier _Nullable paramName,
176
185
void *declContext) {
177
186
ASTContext &Context = *static_cast <ASTContext *>(ctx);
178
- return new (Context) ParamDecl (*(SourceLoc *)&loc, *(SourceLoc *)&argLoc,
187
+ return new (Context) ParamDecl (getSourceLocFromPointer (loc),
188
+ getSourceLocFromPointer (argLoc),
179
189
Identifier::getFromOpaquePointer (argName),
180
- *(SourceLoc *)& paramLoc,
190
+ getSourceLocFromPointer ( paramLoc) ,
181
191
Identifier::getFromOpaquePointer (paramName),
182
192
(DeclContext *)declContext);
183
193
}
@@ -190,16 +200,18 @@ void *FuncDecl_create(void *ctx, void *staticLoc, bool isStatic, void *funcLoc,
190
200
void *_Nullable body, void *_Nullable returnType,
191
201
void *declContext) {
192
202
auto *paramList = ParameterList::create (
193
- *static_cast <ASTContext *>(ctx), *(SourceLoc *)& paramLLoc,
194
- getArrayRef<ParamDecl *>(params), *(SourceLoc *)& paramRLoc);
203
+ *static_cast <ASTContext *>(ctx), getSourceLocFromPointer ( paramLLoc) ,
204
+ getArrayRef<ParamDecl *>(params), getSourceLocFromPointer ( paramRLoc) );
195
205
auto declName =
196
206
DeclName (*static_cast <ASTContext *>(ctx),
197
207
Identifier::getFromOpaquePointer (name), paramList);
198
208
auto *out = FuncDecl::create (
199
- *static_cast <ASTContext *>(ctx), *(SourceLoc *)& staticLoc,
209
+ *static_cast <ASTContext *>(ctx), getSourceLocFromPointer ( staticLoc) ,
200
210
isStatic ? StaticSpellingKind::KeywordStatic : StaticSpellingKind::None,
201
- *(SourceLoc *)&funcLoc, declName, *(SourceLoc *)&nameLoc, isAsync,
202
- *(SourceLoc *)&asyncLoc, throws, *(SourceLoc *)&throwsLoc, nullptr ,
211
+ getSourceLocFromPointer (funcLoc), declName,
212
+ getSourceLocFromPointer (nameLoc), isAsync,
213
+ getSourceLocFromPointer (asyncLoc), throws,
214
+ getSourceLocFromPointer (throwsLoc), nullptr ,
203
215
paramList, (TypeRepr *)returnType, (DeclContext *)declContext);
204
216
out->setBody ((BraceStmt *)body, FuncDecl::BodyKind::Parsed);
205
217
@@ -208,17 +220,19 @@ void *FuncDecl_create(void *ctx, void *staticLoc, bool isStatic, void *funcLoc,
208
220
209
221
void *SimpleIdentTypeRepr_create (void *ctx, void *loc, BridgedIdentifier id) {
210
222
ASTContext &Context = *static_cast <ASTContext *>(ctx);
211
- return new (Context) SimpleIdentTypeRepr (DeclNameLoc (*(SourceLoc *)&loc),
212
- DeclNameRef (Identifier::getFromOpaquePointer (id)));
223
+ return new (Context) SimpleIdentTypeRepr (
224
+ DeclNameLoc (getSourceLocFromPointer (loc)),
225
+ DeclNameRef (Identifier::getFromOpaquePointer (id)));
213
226
}
214
227
215
228
void *UnresolvedDotExpr_create (
216
229
void *ctx, void *base, void *dotLoc, BridgedIdentifier name,
217
230
void *nameLoc) {
218
231
ASTContext &Context = *static_cast <ASTContext *>(ctx);
219
- return new (Context) UnresolvedDotExpr ((Expr *)base, *(SourceLoc *)&dotLoc,
220
- DeclNameRef (Identifier::getFromOpaquePointer (name)),
221
- DeclNameLoc (*(SourceLoc *)&nameLoc), false );
232
+ return new (Context) UnresolvedDotExpr (
233
+ (Expr *)base, getSourceLocFromPointer (dotLoc),
234
+ DeclNameRef (Identifier::getFromOpaquePointer (name)),
235
+ DeclNameLoc (getSourceLocFromPointer (nameLoc)), false );
222
236
}
223
237
224
238
void *ClosureExpr_create (void *ctx, void *body, void *dc) {
@@ -246,9 +260,9 @@ void NominalTypeDecl_setMembers(void *decl, BridgedArrayRef members) {
246
260
DeclContextAndDecl StructDecl_create (
247
261
void *ctx, void *loc, BridgedIdentifier name, void *nameLoc, void *dc) {
248
262
ASTContext &Context = *static_cast <ASTContext *>(ctx);
249
- auto *out = new (Context) StructDecl (SourceLoc (), // *(SourceLoc *)& loc,
263
+ auto *out = new (Context) StructDecl (getSourceLocFromPointer ( loc) ,
250
264
Identifier::getFromOpaquePointer (name),
251
- SourceLoc (), // *(SourceLoc *)& nameLoc,
265
+ getSourceLocFromPointer ( nameLoc) ,
252
266
{}, nullptr ,
253
267
(DeclContext *)dc);
254
268
out->setImplicit (); // TODO: remove this.
@@ -258,17 +272,17 @@ DeclContextAndDecl StructDecl_create(
258
272
DeclContextAndDecl ClassDecl_create (
259
273
void *ctx, void *loc, BridgedIdentifier name, void *nameLoc, void *dc) {
260
274
ASTContext &Context = *static_cast <ASTContext *>(ctx);
261
- auto *out = new (Context) ClassDecl (SourceLoc (), // *(SourceLoc *)& loc,
275
+ auto *out = new (Context) ClassDecl (getSourceLocFromPointer ( loc) ,
262
276
Identifier::getFromOpaquePointer (name),
263
- SourceLoc (), // *(SourceLoc *)& nameLoc,
277
+ getSourceLocFromPointer ( nameLoc) ,
264
278
{}, nullptr ,
265
279
(DeclContext *)dc, false );
266
280
out->setImplicit (); // TODO: remove this.
267
281
return {(DeclContext *)out, (NominalTypeDecl *)out, (Decl *)out};
268
282
}
269
283
270
- void TopLevelCodeDecl_dump (void *decl) { ((TopLevelCodeDecl *)decl)->dump (); }
284
+ void TopLevelCodeDecl_dump (void *decl) { ((TopLevelCodeDecl *)decl)->dump (llvm::errs () ); }
271
285
272
- void Expr_dump (void *expr) { ((Expr *)expr)->dump (); }
273
- void Decl_dump (void *expr) { ((Decl *)expr)->dump (); }
274
- void Stmt_dump (void *expr) { ((Stmt *)expr)->dump (); }
286
+ void Expr_dump (void *expr) { ((Expr *)expr)->dump (llvm::errs () ); }
287
+ void Decl_dump (void *expr) { ((Decl *)expr)->dump (llvm::errs () ); }
288
+ void Stmt_dump (void *expr) { ((Stmt *)expr)->dump (llvm::errs () ); }
0 commit comments