-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathConversion.h
453 lines (372 loc) · 14.6 KB
/
Conversion.h
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
//===--- Conversion.h - Types for value conversion --------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 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
//
//===----------------------------------------------------------------------===//
//
// Defines the Conversion class as well as ConvertingInitialization.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_LOWERING_CONVERSION_H
#define SWIFT_LOWERING_CONVERSION_H
#include "swift/Basic/ExternalUnion.h"
#include "Initialization.h"
#include "SGFContext.h"
namespace swift {
namespace Lowering {
/// An abstraction representing certain kinds of conversion that SILGen can
/// do automatically in various situations.
class Conversion {
public:
enum KindTy {
/// A bridging conversion to a foreign type.
BridgeToObjC,
/// A bridging conversion to a foreign type following a force.
/// Although it's not reflected in the name, this is always an
/// implicit force cast.
ForceAndBridgeToObjC,
/// A bridging conversion from a foreign type.
BridgeFromObjC,
/// A bridging conversion for a function result.
BridgeResultFromObjC,
/// An erasure to Any (possibly wrapped in optional conversions).
/// This is sortof a bridging conversion? Really it's more of a
/// subtype conversion, but we're calling it out separately here,
/// and that's easier.
AnyErasure,
/// A subtype conversion.
Subtype,
/// An orig-to-subst conversion.
OrigToSubst,
/// A subst-to-orig conversion. These can always be annihilated.
SubstToOrig,
};
static bool isBridgingKind(KindTy kind) {
switch (kind) {
case BridgeToObjC:
case ForceAndBridgeToObjC:
case BridgeFromObjC:
case BridgeResultFromObjC:
case AnyErasure:
case Subtype:
return true;
case OrigToSubst:
case SubstToOrig:
return false;
}
llvm_unreachable("bad kind");
}
static bool isReabstractionKind(KindTy kind) {
switch (kind) {
case OrigToSubst:
case SubstToOrig:
return true;
case BridgeToObjC:
case ForceAndBridgeToObjC:
case BridgeFromObjC:
case BridgeResultFromObjC:
case AnyErasure:
case Subtype:
return false;
}
llvm_unreachable("bad kind");
}
private:
KindTy Kind;
struct BridgingTypes {
CanType OrigType;
CanType ResultType;
SILType LoweredResultType;
bool IsExplicit;
};
struct ReabstractionTypes {
// Whether this abstraction pattern applies to the input or output
// substituted type is determined by the kind.
AbstractionPattern OrigType;
CanType SubstSourceType;
CanType SubstResultType;
SILType LoweredResultType;
};
using Members = ExternalUnionMembers<BridgingTypes, ReabstractionTypes>;
static Members::Index getStorageIndexForKind(KindTy kind) {
switch (kind) {
case BridgeToObjC:
case ForceAndBridgeToObjC:
case BridgeFromObjC:
case BridgeResultFromObjC:
case AnyErasure:
case Subtype:
return Members::indexOf<BridgingTypes>();
case OrigToSubst:
case SubstToOrig:
return Members::indexOf<ReabstractionTypes>();
}
llvm_unreachable("bad kind");
}
ExternalUnion<KindTy, Members, getStorageIndexForKind> Types;
static_assert(decltype(Types)::union_is_trivially_copyable,
"define the special members if this changes");
Conversion(KindTy kind, CanType origType, CanType resultType,
SILType loweredResultTy, bool isExplicit)
: Kind(kind) {
Types.emplaceAggregate<BridgingTypes>(kind, origType, resultType,
loweredResultTy, isExplicit);
}
Conversion(KindTy kind, AbstractionPattern origType, CanType substSourceType,
CanType substResultType, SILType loweredResultTy)
: Kind(kind) {
Types.emplaceAggregate<ReabstractionTypes>(kind, origType, substSourceType,
substResultType, loweredResultTy);
}
public:
static Conversion getOrigToSubst(AbstractionPattern origType,
CanType substType,
SILType loweredResultTy) {
return Conversion(OrigToSubst, origType, substType, substType, loweredResultTy);
}
static Conversion getSubstToOrig(AbstractionPattern origType,
CanType substType,
SILType loweredResultTy) {
return Conversion(SubstToOrig, origType, substType, substType, loweredResultTy);
}
static Conversion getSubstToOrig(CanType inputSubstType,
AbstractionPattern outputOrigType,
CanType outputSubstType,
SILType loweredResultTy) {
return Conversion(SubstToOrig, outputOrigType, inputSubstType,
outputSubstType, loweredResultTy);
}
static Conversion getBridging(KindTy kind, CanType origType,
CanType resultType, SILType loweredResultTy,
bool isExplicit = false) {
assert(isBridgingKind(kind));
return Conversion(kind, origType, resultType, loweredResultTy, isExplicit);
}
static Conversion getSubtype(CanType origType, CanType substType,
SILType loweredResultTy) {
return getBridging(Subtype, origType, substType, loweredResultTy);
}
KindTy getKind() const {
return Kind;
}
bool isBridging() const {
return isBridgingKind(getKind());
}
bool isReabstraction() const {
return isReabstractionKind(getKind());
}
AbstractionPattern getReabstractionOrigType() const {
return Types.get<ReabstractionTypes>(Kind).OrigType;
}
CanType getReabstractionSubstSourceType() const {
return Types.get<ReabstractionTypes>(Kind).SubstSourceType;
}
CanType getReabstractionSubstResultType() const {
return Types.get<ReabstractionTypes>(Kind).SubstResultType;
}
SILType getReabstractionLoweredResultType() const {
return Types.get<ReabstractionTypes>(Kind).LoweredResultType;
}
bool isBridgingExplicit() const {
return Types.get<BridgingTypes>(Kind).IsExplicit;
}
CanType getBridgingSourceType() const {
return Types.get<BridgingTypes>(Kind).OrigType;
}
CanType getBridgingResultType() const {
return Types.get<BridgingTypes>(Kind).ResultType;
}
SILType getBridgingLoweredResultType() const {
return Types.get<BridgingTypes>(Kind).LoweredResultType;
}
ManagedValue emit(SILGenFunction &SGF, SILLocation loc,
ManagedValue source, SGFContext ctxt) const;
/// Try to form a conversion that does an optional injection
/// or optional-to-optional conversion followed by this conversion.
std::optional<Conversion>
adjustForInitialOptionalConversions(CanType newSourceType) const;
/// Try to form a conversion that does a force-value followed by
/// this conversion.
std::optional<Conversion> adjustForInitialForceValue() const;
void dump() const LLVM_ATTRIBUTE_USED;
void print(llvm::raw_ostream &out) const;
};
/// Information about how to peephole two conversions.
///
/// This is really the private state of SILGenConvert.
class ConversionPeepholeHint {
public:
enum Kind : uint8_t {
/// The value will be exactly the right type.
Identity,
/// The value needs to be bridged to AnyObject (possibly optionally).
BridgeToAnyObject,
/// The value just needs to undergo a subtype conversion.
Subtype,
/// The inner conversion is a subtype conversion and can be done implicitly
/// as part of the outer conversion.
SubtypeIntoSubstToOrig,
};
private:
Kind TheKind;
bool Forced;
public:
ConversionPeepholeHint(Kind kind, bool forced)
: TheKind(kind), Forced(forced) {
}
Kind getKind() const { return TheKind; }
/// Does the value need to be forced before the conversion?
/// This comes up with result conversions where the result was imported
/// as non-optional, as well as with implicitly unwrapped optionals.
bool isForced() const { return Forced; }
};
std::optional<ConversionPeepholeHint>
canPeepholeConversions(SILGenFunction &SGF, const Conversion &outerConversion,
const Conversion &innerConversion);
/// An initialization where we ultimately want to apply a conversion to
/// the value before completing the initialization.
///
/// Value generators may call getAsConversion() to check whether an
/// Initialization is one of these. This adds initWithConvertedValue
/// to the normal set of ways to receive an initializing value.
class ConvertingInitialization final : public Initialization {
private:
enum StateTy {
/// Nothing has happened.
Uninitialized,
/// The converted value has been set.
Initialized,
/// finishInitialization has been called.
Finished,
/// The converted value has been extracted.
Extracted,
/// We're doing pack initialization instead of the normal state
/// transition, and we haven't been finished yet.
PackExpanding,
/// We're doing pack initialization instead of the normal state
/// transition, and finishInitialization has been called.
FinishedPackExpanding,
};
StateTy State;
/// The conversion that needs to be applied to the formal value.
Conversion TheConversion;
/// The converted value, set if the initializing code calls tryPeephole,
/// setReabstractedValue, or copyOrInitValueInto.
ManagedValue Value;
SGFContext FinalContext;
StateTy getState() const {
return State;
}
InitializationPtr OwnedSubInitialization;
public:
ConvertingInitialization(Conversion conversion, SGFContext finalContext)
: State(Uninitialized), TheConversion(conversion),
FinalContext(finalContext) {}
ConvertingInitialization(Conversion conversion,
InitializationPtr subInitialization)
: State(Uninitialized), TheConversion(conversion),
FinalContext(SGFContext(subInitialization.get())) {
OwnedSubInitialization = std::move(subInitialization);
}
/// Return the conversion to apply to the unconverted value.
const Conversion &getConversion() const {
return TheConversion;
}
/// Return the context into which to emit the converted value.
SGFContext getFinalContext() const {
return FinalContext;
}
// The three ways to perform this initialization:
/// Set the converted value for this initialization.
///
/// If the converted value has been emitted into the final context, you
/// can pass ManagedValue::forInContext() to this function. In this
/// case, you must call finishInitialization on the final initialization
/// yourself prior to calling this. finishEmission will return
/// ManagedValue::forInContext().
///
/// Otherwise, if the final context exists, this will forward the value
/// into it and finish it. finishEmission will return
/// ManagedValue::forInContext().
///
/// Otherwise, this will store the value internally, and finishEmission
/// will return it.
///
/// You must call finishInitialization after calling this.
void initWithConvertedValue(SILGenFunction &SGF, SILLocation loc,
ManagedValue value);
/// Set the unconverted value for this initialization. The value will
/// first be converted. If the final context has an initialization,
/// the converted value will be forwarded into it, and finishEmission
/// will return ManagedValue::forInContext(). Otherwise, finishEmission
/// will return the converted value.
///
/// You must call finishInitialization after calling this.
void copyOrInitValueInto(SILGenFunction &SGF, SILLocation loc,
ManagedValue value, bool isInit) override;
/// Given that the result of the given expression needs to sequentially
/// undergo the given conversion and then this conversion, attempt to
/// peephole the result.
///
/// If this returns true, this initialization will have been initialized
/// as if initWithConvertedValue has been called. You must call
/// finishInitialization in this path.
///
/// Otherwise, there is no state change for the conversion.
bool tryPeephole(SILGenFunction &SGF, Expr *E, Conversion innerConversion);
bool tryPeephole(SILGenFunction &SGF, SILLocation loc,
Conversion innerConversion, ValueProducerRef producer);
bool tryPeephole(SILGenFunction &SGF, SILLocation loc, ManagedValue value,
Conversion innerConversion);
/// Given that an emitter was able to adjust the conversion when
/// emitting into this initialization, continue emission into the
/// new conversion.
ManagedValue emitWithAdjustedConversion(SILGenFunction &SGF, SILLocation loc,
Conversion adjustedConversion,
ValueProducerRef producer);
/// Given the unconverted result, i.e. the result of emitting a
/// value formally of the unconverted type with this initialization
/// as the SGFContext, produce the converted result.
///
/// If this initialization was initialized, the unconverted result
/// must be ManagedValue::forInContext(), and vice-versa.
///
/// The result of this function may itself be
/// ManagedValue::forInContext() if this Initialization was created
/// with an SGFContext which contains another Initialization.
ManagedValue finishEmission(SILGenFunction &SGF, SILLocation loc,
ManagedValue formalResult);
// Implement to make the cast work.
ConvertingInitialization *getAsConversion() override {
return this;
}
// Bookkeeping.
void finishInitialization(SILGenFunction &SGF) override {
if (getState() == PackExpanding) {
FinalContext.getEmitInto()->finishInitialization(SGF);
State = FinishedPackExpanding;
} else {
assert(getState() == Initialized);
State = Finished;
}
}
// Support pack-expansion initialization.
bool canPerformPackExpansionInitialization() const override {
if (auto finalInit = FinalContext.getEmitInto())
return finalInit->canPerformPackExpansionInitialization();
return false;
}
void performPackExpansionInitialization(SILGenFunction &SGF,
SILLocation loc,
SILValue indexWithinComponent,
llvm::function_ref<void(Initialization *into)> fn) override;
};
} // end namespace Lowering
} // end namespace swift
#endif