Skip to content

Commit 7483373

Browse files
committed
[ASTGen] Use UTF-8 rather than C char, and use BridgedIdentifier consistently
1 parent 54bcb33 commit 7483373

File tree

3 files changed

+60
-48
lines changed

3 files changed

+60
-48
lines changed

include/swift/AST/CASTBridging.h

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#ifndef SWIFT_C_AST_ASTBRIDGING_H
1414
#define SWIFT_C_AST_ASTBRIDGING_H
1515

16+
#include <inttypes.h>
17+
1618
#if __clang__
1719
// Provide macros to temporarily suppress warning about the use of
1820
// _Nullable and _Nonnull.
@@ -26,6 +28,7 @@
2628
#else
2729
#define SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
2830
#define SWIFT_END_NULLABILITY_ANNOTATIONS
31+
#define _Nullable
2932
#endif
3033

3134
//===----------------------------------------------------------------------===//
@@ -73,7 +76,7 @@ void DiagnosticEngine_diagnose(void *, void *loc, BridgedDiagID diagID,
7376

7477
int DiagnosticEngine_hadAnyError(void *);
7578

76-
typedef const char *BridgedIdentifier;
79+
typedef void *BridgedIdentifier;
7780

7881
#ifdef __cplusplus
7982
extern "C" {
@@ -83,7 +86,8 @@ extern "C" {
8386
#endif
8487

8588
BridgedIdentifier
86-
SwiftASTContext_getIdentifier(void *ctx, const char *_Nullable str, long len);
89+
SwiftASTContext_getIdentifier(
90+
void *ctx, const uint8_t * _Nullable str, long len);
8791

8892
void *SwiftImportDecl_create(void *, void *, void *, char, void *,
8993
BridgedArrayRef, BridgedArrayRef);
@@ -100,17 +104,17 @@ void *SwiftTupleExpr_create(void *ctx, void *lparen, BridgedArrayRef subs,
100104

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

103-
void *SwiftIdentifierExpr_create(void *ctx, const char *base, void *loc);
107+
void *SwiftIdentifierExpr_create(void *ctx, BridgedIdentifier base, void *loc);
104108

105-
void *SwiftStringLiteralExpr_create(void *ctx, const char *_Nullable string,
109+
void *SwiftStringLiteralExpr_create(void *ctx, const uint8_t * _Nullable string,
106110
long len, void *TokenLoc);
107111

108-
void *SwiftIntegerLiteralExpr_create(void *ctx, const char *_Nullable string,
112+
void *SwiftIntegerLiteralExpr_create(void *ctx, const uint8_t * _Nullable string,
109113
long len, void *TokenLoc);
110114

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

113-
void *SwiftVarDecl_create(void *ctx, const char *_Nullable name,
117+
void *SwiftVarDecl_create(void *ctx, BridgedIdentifier _Nullable name,
114118
void *loc, _Bool isStatic, _Bool isLet, void *dc);
115119

116120
void *IfStmt_create(void *ctx, void *ifLoc, void *cond, void *_Nullable then, void *_Nullable elseLoc,
@@ -127,16 +131,16 @@ void *ParamDecl_create(void *ctx, void *loc,
127131
void *declContext);
128132

129133
void *FuncDecl_create(void *ctx, void *staticLoc, _Bool isStatic, void *funcLoc,
130-
const char *name, void *nameLoc,
134+
BridgedIdentifier name, void *nameLoc,
131135
_Bool isAsync, void *_Nullable asyncLoc,
132136
_Bool throws, void *_Nullable throwsLoc,
133137
void *paramLLoc, BridgedArrayRef params, void *paramRLoc,
134138
void *_Nullable body, void *_Nullable returnType,
135139
void *declContext);
136140

137-
void *SimpleIdentTypeRepr_create(void *ctx, void *loc, const char *id);
141+
void *SimpleIdentTypeRepr_create(void *ctx, void *loc, BridgedIdentifier id);
138142

139-
void *UnresolvedDotExpr_create(void *ctx, void *base, void *dotLoc, const char *name, void *nameLoc);
143+
void *UnresolvedDotExpr_create(void *ctx, void *base, void *dotLoc, BridgedIdentifier name, void *nameLoc);
140144

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

@@ -148,10 +152,10 @@ struct DeclContextAndDecl {
148152
void *decl;
149153
};
150154

151-
struct DeclContextAndDecl StructDecl_create(void *ctx, void *loc, const char *name, void *nameLoc,
152-
void *dc);
153-
struct DeclContextAndDecl ClassDecl_create(void *ctx, void *loc, const char *name, void *nameLoc,
154-
void *dc);
155+
struct DeclContextAndDecl StructDecl_create(
156+
void *ctx, void *loc, BridgedIdentifier name, void *nameLoc, void *dc);
157+
struct DeclContextAndDecl ClassDecl_create(
158+
void *ctx, void *loc, BridgedIdentifier name, void *nameLoc, void *dc);
155159

156160
void TopLevelCodeDecl_dump(void *);
157161
void Expr_dump(void *);

lib/AST/CASTBridging.cpp

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ inline llvm::ArrayRef<T> getArrayRef(BridgedArrayRef bridged) {
1717
}
1818

1919
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();
20+
SwiftASTContext_getIdentifier(void *ctx, const uint8_t *_Nullable str, long len) {
21+
return const_cast<void *>(
22+
static_cast<ASTContext *>(ctx)
23+
->getIdentifier(
24+
StringRef{reinterpret_cast<const char *>(str), size_t(len)})
25+
.getAsOpaquePointer());
2426
}
2527

2628
void *SwiftImportDecl_create(void *ctx, void *dc, void *importLoc, char kind,
@@ -101,40 +103,43 @@ void *SwiftFunctionCallExpr_create(void *ctx, void *fn, void *args) {
101103
/*implicit*/ false);
102104
}
103105

104-
void *SwiftIdentifierExpr_create(void *ctx, const char *base, void *loc) {
106+
void *SwiftIdentifierExpr_create(void *ctx, BridgedIdentifier base, void *loc) {
105107
ASTContext &Context = *static_cast<ASTContext *>(ctx);
106108
auto name = DeclNameRef{
107-
swift::Identifier::getFromOpaquePointer(const_cast<char *>(base))};
109+
swift::Identifier::getFromOpaquePointer(base)};
108110
Expr *E = new (Context) UnresolvedDeclRefExpr(
109111
name, DeclRefKind::Ordinary, DeclNameLoc{*(SourceLoc *)&loc});
110112
return E;
111113
}
112114

113-
void *SwiftStringLiteralExpr_create(void *ctx, const char *_Nullable string,
114-
long len, void *TokenLoc) {
115+
void *SwiftStringLiteralExpr_create(
116+
void *ctx, const uint8_t *_Nullable string,
117+
long len, void *TokenLoc) {
115118
ASTContext &Context = *static_cast<ASTContext *>(ctx);
116-
return new (Context) StringLiteralExpr(StringRef{string, size_t(len)},
117-
*(SourceLoc *)&TokenLoc);
119+
return new (Context) StringLiteralExpr(
120+
StringRef{reinterpret_cast<const char *>(string), size_t(len)},
121+
*(SourceLoc *)&TokenLoc);
118122
}
119123

120-
void *SwiftIntegerLiteralExpr_create(void *ctx, const char *_Nullable string,
121-
long len, void *TokenLoc) {
124+
void *SwiftIntegerLiteralExpr_create(
125+
void *ctx, const uint8_t *_Nullable string, long len, void *TokenLoc) {
122126
ASTContext &Context = *static_cast<ASTContext *>(ctx);
123-
return new (Context) IntegerLiteralExpr(StringRef{string, size_t(len)},
124-
*(SourceLoc *)&TokenLoc);
127+
return new (Context) IntegerLiteralExpr(
128+
StringRef{reinterpret_cast<const char *>(string), size_t(len)},
129+
*(SourceLoc *)&TokenLoc);
125130
}
126131

127132
void *SwiftBooleanLiteralExpr_create(void *ctx, bool value, void *TokenLoc) {
128133
ASTContext &Context = *static_cast<ASTContext *>(ctx);
129134
return new (Context) BooleanLiteralExpr(value, *(SourceLoc *)&TokenLoc);
130135
}
131136

132-
void *SwiftVarDecl_create(void *ctx, const char *_Nullable nameId,
137+
void *SwiftVarDecl_create(void *ctx, BridgedIdentifier _Nullable nameId,
133138
void *loc, bool isStatic, bool isLet, void *dc) {
134139
ASTContext &Context = *static_cast<ASTContext *>(ctx);
135140
return new (Context) VarDecl(isStatic,
136141
isLet ? VarDecl::Introducer::Let : VarDecl::Introducer::Var,
137-
*(SourceLoc *)&loc, Identifier::getFromOpaquePointer((void *)nameId),
142+
*(SourceLoc *)&loc, Identifier::getFromOpaquePointer(nameId),
138143
reinterpret_cast<DeclContext *>(dc));
139144
}
140145

@@ -164,10 +169,11 @@ void *BraceStmt_createStmt(void *ctx, void *lbloc, BridgedArrayRef elements, voi
164169
*(SourceLoc *)&rbloc);
165170
}
166171

167-
void *ParamDecl_create(void *ctx, void *loc,
168-
void *_Nullable argLoc, void *_Nullable argName,
169-
void *_Nullable paramLoc, void *_Nullable paramName,
170-
void *declContext) {
172+
void *ParamDecl_create(
173+
void *ctx, void *loc,
174+
void *_Nullable argLoc, BridgedIdentifier _Nullable argName,
175+
void *_Nullable paramLoc, BridgedIdentifier _Nullable paramName,
176+
void *declContext) {
171177
ASTContext &Context = *static_cast<ASTContext *>(ctx);
172178
return new (Context) ParamDecl(*(SourceLoc *)&loc, *(SourceLoc *)&argLoc,
173179
Identifier::getFromOpaquePointer(argName),
@@ -177,7 +183,7 @@ void *ParamDecl_create(void *ctx, void *loc,
177183
}
178184

179185
void *FuncDecl_create(void *ctx, void *staticLoc, bool isStatic, void *funcLoc,
180-
const char *name, void *nameLoc,
186+
BridgedIdentifier name, void *nameLoc,
181187
bool isAsync, void *_Nullable asyncLoc,
182188
bool throws, void *_Nullable throwsLoc,
183189
void *paramLLoc, BridgedArrayRef params, void *paramRLoc,
@@ -188,7 +194,7 @@ void *FuncDecl_create(void *ctx, void *staticLoc, bool isStatic, void *funcLoc,
188194
getArrayRef<ParamDecl *>(params), *(SourceLoc *)&paramRLoc);
189195
auto declName =
190196
DeclName(*static_cast<ASTContext *>(ctx),
191-
Identifier::getFromOpaquePointer((void *)name), paramList);
197+
Identifier::getFromOpaquePointer(name), paramList);
192198
auto *out = FuncDecl::create(
193199
*static_cast<ASTContext *>(ctx), *(SourceLoc *)&staticLoc,
194200
isStatic ? StaticSpellingKind::KeywordStatic : StaticSpellingKind::None,
@@ -200,16 +206,18 @@ void *FuncDecl_create(void *ctx, void *staticLoc, bool isStatic, void *funcLoc,
200206
return static_cast<Decl *>(out);
201207
}
202208

203-
void *SimpleIdentTypeRepr_create(void *ctx, void *loc, const char *id) {
209+
void *SimpleIdentTypeRepr_create(void *ctx, void *loc, BridgedIdentifier id) {
204210
ASTContext &Context = *static_cast<ASTContext *>(ctx);
205211
return new (Context) SimpleIdentTypeRepr(DeclNameLoc(*(SourceLoc *)&loc),
206-
DeclNameRef(Identifier::getFromOpaquePointer((void *)id)));
212+
DeclNameRef(Identifier::getFromOpaquePointer(id)));
207213
}
208214

209-
void *UnresolvedDotExpr_create(void *ctx, void *base, void *dotLoc, const char *name, void *nameLoc) {
215+
void *UnresolvedDotExpr_create(
216+
void *ctx, void *base, void *dotLoc, BridgedIdentifier name,
217+
void *nameLoc) {
210218
ASTContext &Context = *static_cast<ASTContext *>(ctx);
211219
return new (Context) UnresolvedDotExpr((Expr *)base, *(SourceLoc *)&dotLoc,
212-
DeclNameRef(Identifier::getFromOpaquePointer((void *)name)),
220+
DeclNameRef(Identifier::getFromOpaquePointer(name)),
213221
DeclNameLoc(*(SourceLoc *)&nameLoc), false);
214222
}
215223

@@ -235,23 +243,23 @@ void NominalTypeDecl_setMembers(void *decl, BridgedArrayRef members) {
235243
((NominalTypeDecl *)decl)->addMember(m);
236244
}
237245

238-
DeclContextAndDecl StructDecl_create(void *ctx, void *loc, const char *name, void *nameLoc,
239-
void *dc) {
246+
DeclContextAndDecl StructDecl_create(
247+
void *ctx, void *loc, BridgedIdentifier name, void *nameLoc, void *dc) {
240248
ASTContext &Context = *static_cast<ASTContext *>(ctx);
241249
auto *out = new (Context) StructDecl(SourceLoc(), // *(SourceLoc *)&loc,
242-
Identifier::getFromOpaquePointer((void *)name),
250+
Identifier::getFromOpaquePointer(name),
243251
SourceLoc(), // *(SourceLoc *)&nameLoc,
244252
{}, nullptr,
245253
(DeclContext *)dc);
246254
out->setImplicit(); // TODO: remove this.
247255
return {(DeclContext *)out, (NominalTypeDecl *)out, (Decl *)out};
248256
}
249257

250-
DeclContextAndDecl ClassDecl_create(void *ctx, void *loc, const char *name, void *nameLoc,
251-
void *dc) {
258+
DeclContextAndDecl ClassDecl_create(
259+
void *ctx, void *loc, BridgedIdentifier name, void *nameLoc, void *dc) {
252260
ASTContext &Context = *static_cast<ASTContext *>(ctx);
253261
auto *out = new (Context) ClassDecl(SourceLoc(), // *(SourceLoc *)&loc,
254-
Identifier::getFromOpaquePointer((void *)name),
262+
Identifier::getFromOpaquePointer(name),
255263
SourceLoc(), // *(SourceLoc *)&nameLoc,
256264
{}, nullptr,
257265
(DeclContext *)dc, false);
@@ -263,4 +271,4 @@ void TopLevelCodeDecl_dump(void *decl) { ((TopLevelCodeDecl *)decl)->dump(); }
263271

264272
void Expr_dump(void *expr) { ((Expr *)expr)->dump(); }
265273
void Decl_dump(void *expr) { ((Decl *)expr)->dump(); }
266-
void Stmt_dump(void *expr) { ((Stmt *)expr)->dump(); }
274+
void Stmt_dump(void *expr) { ((Stmt *)expr)->dump(); }

lib/ASTGen/Sources/ASTGen/Decls.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ extension ASTGenVisitor {
7272
if let nodeFirstName = node.firstName {
7373
var text = nodeFirstName.text
7474
firstName = text.withUTF8 { buf in
75-
SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count).raw
75+
SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count)
7676
}
7777
} else {
7878
firstName = nil
@@ -81,7 +81,7 @@ extension ASTGenVisitor {
8181
if let nodeSecondName = node.secondName {
8282
var text = nodeSecondName.text
8383
secondName = text.withUTF8 { buf in
84-
SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count).raw
84+
SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count)
8585
}
8686
} else {
8787
secondName = nil

0 commit comments

Comments
 (0)