-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathTypeReprBridging.cpp
323 lines (284 loc) · 13.6 KB
/
TypeReprBridging.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
//===--- Bridging/TypeReprBridging.cpp ------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2022-2024 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
//
//===----------------------------------------------------------------------===//
#include "swift/AST/ASTBridging.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/Attr.h"
#include "swift/AST/Identifier.h"
#include "swift/AST/TypeRepr.h"
#include "swift/Basic/Assertions.h"
using namespace swift;
//===----------------------------------------------------------------------===//
// MARK: TypeReprs
//===----------------------------------------------------------------------===//
// Define `.asTypeRepr` on each BridgedXXXTypeRepr type.
#define TYPEREPR(Id, Parent) \
BridgedTypeRepr Bridged##Id##TypeRepr_asTypeRepr( \
Bridged##Id##TypeRepr typeRepr) { \
return static_cast<TypeRepr *>(typeRepr.unbridged()); \
}
#define ABSTRACT_TYPEREPR(Id, Parent) TYPEREPR(Id, Parent)
#include "swift/AST/TypeReprNodes.def"
BridgedUnqualifiedIdentTypeRepr BridgedUnqualifiedIdentTypeRepr_createParsed(
BridgedASTContext cContext, BridgedSourceLoc cLoc, BridgedIdentifier id) {
return UnqualifiedIdentTypeRepr::create(cContext.unbridged(),
DeclNameLoc(cLoc.unbridged()),
DeclNameRef(id.unbridged()));
}
BridgedUnqualifiedIdentTypeRepr BridgedUnqualifiedIdentTypeRepr_createParsed(
BridgedASTContext cContext, BridgedIdentifier name,
BridgedSourceLoc cNameLoc, BridgedArrayRef genericArgs,
BridgedSourceLoc cLAngleLoc, BridgedSourceLoc cRAngleLoc) {
ASTContext &context = cContext.unbridged();
auto Loc = DeclNameLoc(cNameLoc.unbridged());
auto Name = DeclNameRef(name.unbridged());
SourceLoc lAngleLoc = cLAngleLoc.unbridged();
SourceLoc rAngleLoc = cRAngleLoc.unbridged();
return UnqualifiedIdentTypeRepr::create(context, Loc, Name,
genericArgs.unbridged<TypeRepr *>(),
SourceRange{lAngleLoc, rAngleLoc});
}
BridgedOptionalTypeRepr
BridgedOptionalTypeRepr_createParsed(BridgedASTContext cContext,
BridgedTypeRepr base,
BridgedSourceLoc cQuestionLoc) {
ASTContext &context = cContext.unbridged();
return new (context)
OptionalTypeRepr(base.unbridged(), cQuestionLoc.unbridged());
}
BridgedImplicitlyUnwrappedOptionalTypeRepr
BridgedImplicitlyUnwrappedOptionalTypeRepr_createParsed(
BridgedASTContext cContext, BridgedTypeRepr base,
BridgedSourceLoc cExclamationLoc) {
ASTContext &context = cContext.unbridged();
return new (context) ImplicitlyUnwrappedOptionalTypeRepr(
base.unbridged(), cExclamationLoc.unbridged());
}
BridgedArrayTypeRepr BridgedArrayTypeRepr_createParsed(
BridgedASTContext cContext, BridgedTypeRepr base,
BridgedSourceLoc cLSquareLoc, BridgedSourceLoc cRSquareLoc) {
ASTContext &context = cContext.unbridged();
SourceLoc lSquareLoc = cLSquareLoc.unbridged();
SourceLoc rSquareLoc = cRSquareLoc.unbridged();
return new (context)
ArrayTypeRepr(base.unbridged(), SourceRange{lSquareLoc, rSquareLoc});
}
BridgedDictionaryTypeRepr BridgedDictionaryTypeRepr_createParsed(
BridgedASTContext cContext, BridgedSourceLoc cLSquareLoc,
BridgedTypeRepr keyType, BridgedSourceLoc cColonloc,
BridgedTypeRepr valueType, BridgedSourceLoc cRSquareLoc) {
ASTContext &context = cContext.unbridged();
SourceLoc lSquareLoc = cLSquareLoc.unbridged();
SourceLoc colonLoc = cColonloc.unbridged();
SourceLoc rSquareLoc = cRSquareLoc.unbridged();
return new (context)
DictionaryTypeRepr(keyType.unbridged(), valueType.unbridged(), colonLoc,
SourceRange{lSquareLoc, rSquareLoc});
}
BridgedErrorTypeRepr BridgedErrorTypeRepr_create(BridgedASTContext cContext,
BridgedSourceRange cRange) {
return ErrorTypeRepr::create(cContext.unbridged(), cRange.unbridged());
}
BridgedInverseTypeRepr
BridgedInverseTypeRepr_createParsed(BridgedASTContext cContext,
BridgedSourceLoc cTildeLoc,
BridgedTypeRepr cConstraint) {
return new (cContext.unbridged())
InverseTypeRepr(cTildeLoc.unbridged(), cConstraint.unbridged());
}
BridgedIsolatedTypeRepr
BridgedIsolatedTypeRepr_createParsed(BridgedASTContext cContext,
BridgedTypeRepr base,
BridgedSourceLoc cSpecifierLoc) {
return new (cContext.unbridged())
IsolatedTypeRepr(base.unbridged(), cSpecifierLoc.unbridged());
}
BridgedLifetimeDependentTypeRepr
BridgedLifetimeDependentTypeRepr_createParsed(BridgedASTContext cContext,
BridgedTypeRepr base,
BridgedLifetimeEntry cEntry) {
return new (cContext.unbridged())
LifetimeDependentTypeRepr(base.unbridged(), cEntry.unbridged());
}
BridgedMetatypeTypeRepr
BridgedMetatypeTypeRepr_createParsed(BridgedASTContext cContext,
BridgedTypeRepr baseType,
BridgedSourceLoc cTypeLoc) {
ASTContext &context = cContext.unbridged();
SourceLoc tyLoc = cTypeLoc.unbridged();
return new (context) MetatypeTypeRepr(baseType.unbridged(), tyLoc);
}
BridgedOwnershipTypeRepr BridgedOwnershipTypeRepr_createParsed(
BridgedASTContext cContext, BridgedTypeRepr base,
BridgedParamSpecifier cSpecifier, BridgedSourceLoc cSpecifierLoc) {
return new (cContext.unbridged()) OwnershipTypeRepr(
base.unbridged(), unbridge(cSpecifier), cSpecifierLoc.unbridged());
}
BridgedProtocolTypeRepr
BridgedProtocolTypeRepr_createParsed(BridgedASTContext cContext,
BridgedTypeRepr baseType,
BridgedSourceLoc cProtoLoc) {
ASTContext &context = cContext.unbridged();
SourceLoc protoLoc = cProtoLoc.unbridged();
return new (context) ProtocolTypeRepr(baseType.unbridged(), protoLoc);
}
BridgedPackElementTypeRepr
BridgedPackElementTypeRepr_createParsed(BridgedASTContext cContext,
BridgedTypeRepr base,
BridgedSourceLoc cEachLoc) {
ASTContext &context = cContext.unbridged();
return new (context)
PackElementTypeRepr(cEachLoc.unbridged(), base.unbridged());
}
BridgedPackExpansionTypeRepr
BridgedPackExpansionTypeRepr_createParsed(BridgedASTContext cContext,
BridgedTypeRepr base,
BridgedSourceLoc cRepeatLoc) {
ASTContext &context = cContext.unbridged();
return new (context)
PackExpansionTypeRepr(cRepeatLoc.unbridged(), base.unbridged());
}
BridgedAttributedTypeRepr
BridgedAttributedTypeRepr_createParsed(BridgedASTContext cContext,
BridgedTypeRepr base,
BridgedArrayRef cAttributes) {
SmallVector<TypeOrCustomAttr, 2> attrs;
for (auto elem : cAttributes.unbridged<BridgedTypeOrCustomAttr>()) {
switch (elem.getKind()) {
case BridgedTypeOrCustomAttr::TypeAttr:
attrs.push_back(elem.castToTypeAttr().unbridged());
break;
case BridgedTypeOrCustomAttr::CustomAttr:
attrs.push_back(elem.castToCustomAttr().unbridged());
break;
}
}
assert(!attrs.empty());
return AttributedTypeRepr::create(cContext.unbridged(), attrs,
base.unbridged());
}
BridgedSendingTypeRepr
BridgedSendingTypeRepr_createParsed(BridgedASTContext cContext,
BridgedTypeRepr base,
BridgedSourceLoc cSpecifierLoc) {
return new (cContext.unbridged())
SendingTypeRepr(base.unbridged(), cSpecifierLoc.unbridged());
}
BridgedVarargTypeRepr
BridgedVarargTypeRepr_createParsed(BridgedASTContext cContext,
BridgedTypeRepr base,
BridgedSourceLoc cEllipsisLoc) {
ASTContext &context = cContext.unbridged();
SourceLoc ellipsisLoc = cEllipsisLoc.unbridged();
TypeRepr *baseType = base.unbridged();
return new (context) VarargTypeRepr(baseType, ellipsisLoc);
}
BridgedTupleTypeRepr BridgedTupleTypeRepr_createParsed(
BridgedASTContext cContext, BridgedArrayRef elements,
BridgedSourceLoc cLParenLoc, BridgedSourceLoc cRParenLoc) {
ASTContext &context = cContext.unbridged();
SourceLoc lParen = cLParenLoc.unbridged();
SourceLoc rParen = cRParenLoc.unbridged();
SmallVector<TupleTypeReprElement, 8> tupleElements;
for (auto element : elements.unbridged<BridgedTupleTypeElement>()) {
TupleTypeReprElement elementRepr;
elementRepr.Name = element.Name.unbridged();
elementRepr.NameLoc = element.NameLoc.unbridged();
elementRepr.SecondName = element.SecondName.unbridged();
elementRepr.SecondNameLoc = element.SecondNameLoc.unbridged();
elementRepr.UnderscoreLoc = element.UnderscoreLoc.unbridged();
elementRepr.ColonLoc = element.ColonLoc.unbridged();
elementRepr.Type = element.Type.unbridged();
elementRepr.TrailingCommaLoc = element.TrailingCommaLoc.unbridged();
tupleElements.emplace_back(elementRepr);
}
return TupleTypeRepr::create(context, tupleElements,
SourceRange{lParen, rParen});
}
BridgedDeclRefTypeRepr BridgedDeclRefTypeRepr_createParsed(
BridgedASTContext cContext, BridgedTypeRepr cBase, BridgedIdentifier cName,
BridgedSourceLoc cLoc, BridgedArrayRef cGenericArguments,
BridgedSourceRange cAngleRange) {
ASTContext &context = cContext.unbridged();
auto genericArguments = cGenericArguments.unbridged<TypeRepr *>();
auto angleRange = cAngleRange.unbridged();
assert(angleRange.isValid() || genericArguments.empty());
return DeclRefTypeRepr::create(
context, cBase.unbridged(), DeclNameLoc(cLoc.unbridged()),
DeclNameRef(cName.unbridged()), genericArguments, angleRange);
}
BridgedCompositionTypeRepr
BridgedCompositionTypeRepr_createEmpty(BridgedASTContext cContext,
BridgedSourceLoc cAnyLoc) {
ASTContext &context = cContext.unbridged();
SourceLoc anyLoc = cAnyLoc.unbridged();
return CompositionTypeRepr::createEmptyComposition(context, anyLoc);
}
BridgedCompositionTypeRepr
BridgedCompositionTypeRepr_createParsed(BridgedASTContext cContext,
BridgedArrayRef cTypes,
BridgedSourceLoc cFirstAmpLoc) {
ASTContext &context = cContext.unbridged();
SourceLoc firstAmpLoc = cFirstAmpLoc.unbridged();
auto types = cTypes.unbridged<TypeRepr *>();
return CompositionTypeRepr::create(
context, types, types.front()->getStartLoc(),
SourceRange{firstAmpLoc, types.back()->getEndLoc()});
}
BridgedCompileTimeLiteralTypeRepr
BridgedCompileTimeLiteralTypeRepr_createParsed(BridgedASTContext cContext,
BridgedTypeRepr base,
BridgedSourceLoc cSpecifierLoc) {
return new (cContext.unbridged())
CompileTimeLiteralTypeRepr(base.unbridged(), cSpecifierLoc.unbridged());
}
BridgedFunctionTypeRepr BridgedFunctionTypeRepr_createParsed(
BridgedASTContext cContext, BridgedTypeRepr argsTy,
BridgedSourceLoc cAsyncLoc, BridgedSourceLoc cThrowsLoc,
BridgedNullableTypeRepr thrownType, BridgedSourceLoc cArrowLoc,
BridgedTypeRepr resultType) {
ASTContext &context = cContext.unbridged();
return new (context) FunctionTypeRepr(
nullptr, cast<TupleTypeRepr>(argsTy.unbridged()), cAsyncLoc.unbridged(),
cThrowsLoc.unbridged(), thrownType.unbridged(), cArrowLoc.unbridged(),
resultType.unbridged());
}
BridgedNamedOpaqueReturnTypeRepr
BridgedNamedOpaqueReturnTypeRepr_createParsed(BridgedASTContext cContext,
BridgedTypeRepr baseTy) {
ASTContext &context = cContext.unbridged();
return new (context) NamedOpaqueReturnTypeRepr(baseTy.unbridged(), nullptr);
}
BridgedOpaqueReturnTypeRepr
BridgedOpaqueReturnTypeRepr_createParsed(BridgedASTContext cContext,
BridgedSourceLoc cOpaqueLoc,
BridgedTypeRepr baseTy) {
ASTContext &context = cContext.unbridged();
return new (context)
OpaqueReturnTypeRepr(cOpaqueLoc.unbridged(), baseTy.unbridged());
}
BridgedExistentialTypeRepr
BridgedExistentialTypeRepr_createParsed(BridgedASTContext cContext,
BridgedSourceLoc cAnyLoc,
BridgedTypeRepr baseTy) {
ASTContext &context = cContext.unbridged();
return new (context)
ExistentialTypeRepr(cAnyLoc.unbridged(), baseTy.unbridged());
}
BridgedIntegerTypeRepr
BridgedIntegerTypeRepr_createParsed(BridgedASTContext cContext,
BridgedStringRef cString,
BridgedSourceLoc cLoc,
BridgedSourceLoc cMinusLoc) {
ASTContext &context = cContext.unbridged();
return new (context) IntegerTypeRepr(cString.unbridged(), cLoc.unbridged(),
cMinusLoc.unbridged());
}