forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConstraint.h
651 lines (544 loc) · 22.9 KB
/
Constraint.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
//===--- Constraint.h - Constraint in the Type Checker ----------*- 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
//
//===----------------------------------------------------------------------===//
//
// This file provides the \c Constraint class and its related types,
// which is used by the constraint-based type checker to describe a
// constraint that must be solved.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_SEMA_CONSTRAINT_H
#define SWIFT_SEMA_CONSTRAINT_H
#include "CSFix.h"
#include "OverloadChoice.h"
#include "swift/AST/FunctionRefKind.h"
#include "swift/AST/Identifier.h"
#include "swift/AST/Type.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/ilist.h"
#include "llvm/ADT/ilist_node.h"
#include "llvm/Support/TrailingObjects.h"
namespace llvm {
class raw_ostream;
}
namespace swift {
class ProtocolDecl;
class SourceManager;
class TypeVariableType;
namespace constraints {
class ConstraintLocator;
class ConstraintSystem;
/// \brief Describes the kind of constraint placed on one or more types.
enum class ConstraintKind : char {
/// \brief The two types must be bound to the same type. This is the only
/// truly symmetric constraint.
Bind,
/// \brief The two types must be bound to the same type, dropping
/// lvalueness when comparing a type variable to a type.
Equal,
/// \brief The first type is the type of a function parameter; the second
/// type is the type of a reference to that parameter from within the
/// function body. Specifically, the left type is an inout type iff the right
/// type is an lvalue type with the same object type. Otherwise, the two
/// types must be the same type.
BindParam,
/// \brief Binds the first type to the element type of the second type.
BindToPointerType,
/// \brief The first type is a subtype of the second type, i.e., a value
/// of the type of the first type can be used wherever a value of the
/// second type is expected.
Subtype,
/// \brief The first type is convertible to the second type.
Conversion,
/// \brief The first type can be bridged to the second type.
BridgingConversion,
/// \brief The first type is the element of an argument tuple that is
/// convertible to the second type (which represents the corresponding
/// parameter type).
ArgumentConversion,
/// \brief The first type is convertible to the second type, including inout.
OperatorArgumentConversion,
/// \brief The first type must conform to the second type (which is a
/// protocol type).
ConformsTo,
/// \brief The first type describes a literal that conforms to the second
/// type, which is one of the known expressible-by-literal protocols.
LiteralConformsTo,
/// A checked cast from the first type to the second.
CheckedCast,
/// \brief The first type can act as the Self type of the second type (which
/// is a protocol).
///
/// This constraint is slightly looser than a conforms-to constraint, because
/// an existential can be used as the Self of any protocol within the
/// existential, even if it doesn't conform to that protocol (e.g., due to
/// the use of associated types).
SelfObjectOfProtocol,
/// \brief Both types are function types. The first function type's
/// input is the value being passed to the function and its output
/// is a type variable that describes the output. The second
/// function type is expected to become a function type. Note, we
/// do not require the function type attributes to match.
ApplicableFunction,
/// \brief The first type is the type of the dynamicType member of the
/// second type.
DynamicTypeOf,
/// \brief Binds the left-hand type to a particular overload choice.
BindOverload,
/// \brief The first type has a member with the given name, and the
/// type of that member, when referenced as a value, is the second type.
ValueMember,
/// \brief The first type (which is implicit) has a member with the given
/// name, and the type of that member, when referenced as a value, is the
/// second type.
UnresolvedValueMember,
/// \brief The first type can be defaulted to the second (which currently
/// cannot be dependent). This is more like a type property than a
/// relational constraint.
Defaultable,
/// \brief A disjunction constraint that specifies that one or more of the
/// stored constraints must hold.
Disjunction,
/// \brief The first type is an optional type whose object type is the second
/// type, preserving lvalue-ness.
OptionalObject,
/// \brief The first type is the same function type as the second type, but
/// made @escaping.
EscapableFunctionOf,
/// \brief The first type is an opened type from the second type (which is
/// an existential).
OpenedExistentialOf,
/// \brief A relation between three types. The first is the key path type,
// the second is the root type, and the third is the projected value type.
// The second and third types can be lvalues depending on the kind of key
// path.
KeyPathApplication,
/// \brief A relation between three types. The first is the key path type,
// the second is its root type, and the third is the projected value type.
// The key path type is chosen based on the selection of overloads for the
// member references along the path.
KeyPath,
/// \brief The first type is a function type, the second is the function's
/// input type.
FunctionInput,
/// \brief The first type is a function type, the second is the function's
/// result type.
FunctionResult,
};
/// \brief Classification of the different kinds of constraints.
enum class ConstraintClassification : char {
/// \brief A relational constraint, which relates two types.
Relational,
/// \brief A member constraint, which names a member of a type and assigns
/// it a reference type.
Member,
/// \brief A property of a single type, such as whether it is defaultable to
/// a particular type.
TypeProperty,
/// \brief A disjunction constraint.
Disjunction
};
/// Specifies a restriction on the kind of conversion that should be
/// performed between the types in a constraint.
///
/// It's common for there to be multiple potential conversions that can
/// apply between two types, e.g., given class types A and B, there might be
/// a superclass conversion from A to B or there might be a user-defined
/// conversion from A to B. The solver may need to explore both paths.
enum class ConversionRestrictionKind {
/// Tuple-to-tuple conversion.
TupleToTuple,
/// Deep equality comparison.
DeepEquality,
/// Subclass-to-superclass conversion.
Superclass,
/// Class metatype to AnyObject conversion.
ClassMetatypeToAnyObject,
/// Existential metatype to AnyObject conversion.
ExistentialMetatypeToAnyObject,
/// Protocol value metatype to Protocol class conversion.
ProtocolMetatypeToProtocolClass,
/// Inout-to-pointer conversion.
InoutToPointer,
/// Array-to-pointer conversion.
ArrayToPointer,
/// String-to-pointer conversion.
StringToPointer,
/// Pointer-to-pointer conversion.
PointerToPointer,
/// Lvalue-to-rvalue conversion.
LValueToRValue,
/// Value to existential value conversion, or existential erasure.
Existential,
/// Metatype to existential metatype conversion.
MetatypeToExistentialMetatype,
/// Existential metatype to metatype conversion.
ExistentialMetatypeToMetatype,
/// T -> U? value to optional conversion (or to implicitly unwrapped optional).
ValueToOptional,
/// T? -> U? optional to optional conversion (or unchecked to unchecked).
OptionalToOptional,
/// Implicit upcast conversion of array types.
ArrayUpcast,
/// Implicit upcast conversion of dictionary types, which includes
/// bridging.
DictionaryUpcast,
/// Implicit upcast conversion of set types, which includes bridging.
SetUpcast,
/// T:Hashable -> AnyHashable conversion.
HashableToAnyHashable,
/// Implicit conversion from a CF type to its toll-free-bridged Objective-C
/// class type.
CFTollFreeBridgeToObjC,
/// Implicit conversion from an Objective-C class type to its
/// toll-free-bridged CF type.
ObjCTollFreeBridgeToCF
};
/// Return a string representation of a conversion restriction.
llvm::StringRef getName(ConversionRestrictionKind kind);
/// Should we record which choice was taken in this disjunction for
/// the purposes of applying it later?
enum RememberChoice_t : bool {
ForgetChoice = false,
RememberChoice = true
};
/// \brief A constraint between two type variables.
class Constraint final : public llvm::ilist_node<Constraint>,
private llvm::TrailingObjects<Constraint, TypeVariableType *> {
friend TrailingObjects;
/// \brief The kind of constraint.
ConstraintKind Kind : 8;
/// The kind of restriction placed on this constraint.
ConversionRestrictionKind Restriction : 8;
/// The fix to be applied to the constraint before visiting it.
ConstraintFix *TheFix = nullptr;
/// Whether the \c Restriction field is valid.
unsigned HasRestriction : 1;
/// Whether this constraint is currently active, i.e., stored in the worklist.
unsigned IsActive : 1;
/// Was this constraint was determined to be inconsistent with the
/// constraint graph during constraint propagation?
unsigned IsDisabled : 1;
/// Whether the choice of this disjunction should be recorded in the
/// solver state.
unsigned RememberChoice : 1;
/// Whether or not this constraint is 'favored' in the sense that, if
/// successfully applied, it should be preferred over any other constraints
/// in its disjunction.
unsigned IsFavored : 1;
/// The number of type variables referenced by this constraint.
///
/// The type variables themselves are tail-allocated.
unsigned NumTypeVariables : 11;
/// The kind of function reference, for member references.
unsigned TheFunctionRefKind : 2;
union {
struct {
/// \brief The first type.
Type First;
/// \brief The second type.
Type Second;
/// \brief The third type, if any.
Type Third;
} Types;
struct {
/// \brief The type of the base.
Type First;
/// \brief The type of the member.
Type Second;
/// \brief If non-null, the name of a member of the first type is that
/// being related to the second type.
DeclName Member;
/// \brief The DC in which the use appears.
DeclContext *UseDC;
} Member;
/// The set of constraints for a disjunction.
ArrayRef<Constraint *> Nested;
struct {
/// \brief The first type
Type First;
/// \brief The overload choice
OverloadChoice Choice;
/// \brief The DC in which the use appears.
DeclContext *UseDC;
} Overload;
};
/// \brief The locator that describes where in the expression this
/// constraint applies.
ConstraintLocator *Locator;
/// \brief Constraints are always allocated within a given constraint
/// system.
void *operator new(size_t) = delete;
Constraint(ConstraintKind kind, ArrayRef<Constraint *> constraints,
ConstraintLocator *locator, ArrayRef<TypeVariableType *> typeVars);
/// Construct a new constraint.
Constraint(ConstraintKind kind, Type first, Type second,
ConstraintLocator *locator,
ArrayRef<TypeVariableType *> typeVars);
/// Construct a new constraint.
Constraint(ConstraintKind kind, Type first, Type second, Type third,
ConstraintLocator *locator,
ArrayRef<TypeVariableType *> typeVars);
/// Construct a new member constraint.
Constraint(ConstraintKind kind, Type first, Type second, DeclName member,
DeclContext *useDC, FunctionRefKind functionRefKind,
ConstraintLocator *locator,
ArrayRef<TypeVariableType *> typeVars);
/// Construct a new overload-binding constraint.
Constraint(Type type, OverloadChoice choice, DeclContext *useDC,
ConstraintLocator *locator, ArrayRef<TypeVariableType *> typeVars);
/// Construct a restricted constraint.
Constraint(ConstraintKind kind, ConversionRestrictionKind restriction,
Type first, Type second, ConstraintLocator *locator,
ArrayRef<TypeVariableType *> typeVars);
/// Construct a relational constraint with a fix.
Constraint(ConstraintKind kind, ConstraintFix *fix, Type first, Type second,
ConstraintLocator *locator, ArrayRef<TypeVariableType *> typeVars);
/// Retrieve the type variables buffer, for internal mutation.
MutableArrayRef<TypeVariableType *> getTypeVariablesBuffer() {
return { getTrailingObjects<TypeVariableType *>(), NumTypeVariables };
}
public:
/// Create a new constraint.
static Constraint *create(ConstraintSystem &cs, ConstraintKind Kind,
Type First, Type Second,
ConstraintLocator *locator);
/// Create a new constraint.
static Constraint *create(ConstraintSystem &cs, ConstraintKind Kind,
Type First, Type Second, Type Third,
ConstraintLocator *locator);
/// Create a new member constraint, or a disjunction of that with the outer
/// alternatives.
static Constraint *createMemberOrOuterDisjunction(
ConstraintSystem &cs, ConstraintKind kind, Type first, Type second,
DeclName member, DeclContext *useDC, FunctionRefKind functionRefKind,
ArrayRef<OverloadChoice> outerAlternatives, ConstraintLocator *locator);
/// Create a new member constraint.
static Constraint *createMember(ConstraintSystem &cs, ConstraintKind kind,
Type first, Type second, DeclName member,
DeclContext *useDC,
FunctionRefKind functionRefKind,
ConstraintLocator *locator);
/// Create an overload-binding constraint.
static Constraint *createBindOverload(ConstraintSystem &cs, Type type,
OverloadChoice choice,
DeclContext *useDC,
ConstraintLocator *locator);
/// Create a restricted relational constraint.
static Constraint *createRestricted(ConstraintSystem &cs, ConstraintKind kind,
ConversionRestrictionKind restriction,
Type first, Type second,
ConstraintLocator *locator);
/// Create a relational constraint with a fix.
static Constraint *createFixed(ConstraintSystem &cs, ConstraintKind kind,
ConstraintFix *fix, Type first, Type second,
ConstraintLocator *locator);
/// Create a new disjunction constraint.
static Constraint *createDisjunction(ConstraintSystem &cs,
ArrayRef<Constraint *> constraints,
ConstraintLocator *locator,
RememberChoice_t shouldRememberChoice
= ForgetChoice);
/// \brief Determine the kind of constraint.
ConstraintKind getKind() const { return Kind; }
/// Retrieve the restriction placed on this constraint.
Optional<ConversionRestrictionKind> getRestriction() const {
if (!HasRestriction)
return None;
return Restriction;
}
/// Retrieve the fix associated with this constraint.
ConstraintFix *getFix() const { return TheFix; }
/// Whether this constraint is active, i.e., in the worklist.
bool isActive() const { return IsActive; }
/// Set whether this constraint is active or not.
void setActive(bool active) {
assert(!isDisabled() && "Cannot activate a constraint that is disabled!");
IsActive = active;
}
/// Whether this constraint is active, i.e., in the worklist.
bool isDisabled() const { return IsDisabled; }
/// Set whether this constraint is active or not.
void setDisabled() {
assert(!isActive() && "Cannot disable constraint marked as active!");
IsDisabled = true;
}
void setEnabled() {
assert(isDisabled() && "Can't re-enable already active constraint!");
IsDisabled = false;
}
/// Mark or retrieve whether this constraint should be favored in the system.
void setFavored() { IsFavored = true; }
bool isFavored() const { return IsFavored; }
/// Whether the solver should remember which choice was taken for
/// this constraint.
bool shouldRememberChoice() const { return RememberChoice; }
/// Retrieve the set of type variables referenced by this constraint.
ArrayRef<TypeVariableType *> getTypeVariables() const {
return {getTrailingObjects<TypeVariableType*>(), NumTypeVariables};
}
/// \brief Determine the classification of this constraint, providing
/// a broader categorization than \c getKind().
ConstraintClassification getClassification() const {
switch (Kind) {
case ConstraintKind::Bind:
case ConstraintKind::Equal:
case ConstraintKind::BindParam:
case ConstraintKind::BindToPointerType:
case ConstraintKind::Subtype:
case ConstraintKind::Conversion:
case ConstraintKind::BridgingConversion:
case ConstraintKind::ArgumentConversion:
case ConstraintKind::OperatorArgumentConversion:
case ConstraintKind::ConformsTo:
case ConstraintKind::LiteralConformsTo:
case ConstraintKind::CheckedCast:
case ConstraintKind::SelfObjectOfProtocol:
case ConstraintKind::ApplicableFunction:
case ConstraintKind::BindOverload:
case ConstraintKind::OptionalObject:
return ConstraintClassification::Relational;
case ConstraintKind::ValueMember:
case ConstraintKind::UnresolvedValueMember:
return ConstraintClassification::Member;
case ConstraintKind::DynamicTypeOf:
case ConstraintKind::EscapableFunctionOf:
case ConstraintKind::OpenedExistentialOf:
case ConstraintKind::KeyPath:
case ConstraintKind::KeyPathApplication:
case ConstraintKind::Defaultable:
case ConstraintKind::FunctionInput:
case ConstraintKind::FunctionResult:
return ConstraintClassification::TypeProperty;
case ConstraintKind::Disjunction:
return ConstraintClassification::Disjunction;
}
llvm_unreachable("Unhandled ConstraintKind in switch.");
}
/// \brief Retrieve the first type in the constraint.
Type getFirstType() const {
switch (getKind()) {
case ConstraintKind::Disjunction:
llvm_unreachable("disjunction constraints have no type operands");
case ConstraintKind::BindOverload:
return Overload.First;
case ConstraintKind::ValueMember:
case ConstraintKind::UnresolvedValueMember:
return Member.First;
default:
return Types.First;
}
}
/// \brief Retrieve the second type in the constraint.
Type getSecondType() const {
switch (getKind()) {
case ConstraintKind::Disjunction:
case ConstraintKind::BindOverload:
llvm_unreachable("constraint has no second type");
case ConstraintKind::ValueMember:
case ConstraintKind::UnresolvedValueMember:
return Member.Second;
default:
return Types.Second;
}
}
/// \brief Retrieve the third type in the constraint.
Type getThirdType() const {
switch (getKind()) {
case ConstraintKind::KeyPath:
case ConstraintKind::KeyPathApplication:
return Types.Third;
default:
llvm_unreachable("no third type");
}
}
/// \brief Retrieve the protocol in a conformance constraint.
ProtocolDecl *getProtocol() const;
/// \brief Retrieve the name of the member for a member constraint.
DeclName getMember() const {
assert(Kind == ConstraintKind::ValueMember ||
Kind == ConstraintKind::UnresolvedValueMember);
return Member.Member;
}
/// \brief Determine whether this constraint kind has a second type.
static bool hasMember(ConstraintKind kind) {
return kind == ConstraintKind::ValueMember
|| kind == ConstraintKind::UnresolvedValueMember;
}
/// Determine the kind of function reference we have for a member reference.
FunctionRefKind getFunctionRefKind() const {
if (Kind == ConstraintKind::ValueMember ||
Kind == ConstraintKind::UnresolvedValueMember)
return static_cast<FunctionRefKind>(TheFunctionRefKind);
// Conservative answer: drop all of the labels.
return FunctionRefKind::Compound;
}
/// Retrieve the set of constraints in a disjunction.
ArrayRef<Constraint *> getNestedConstraints() const {
assert(Kind == ConstraintKind::Disjunction);
return Nested;
}
unsigned countActiveNestedConstraints() const {
unsigned count = 0;
for (auto *constraint : Nested)
if (!constraint->isDisabled())
count++;
return count;
}
/// Determine if this constraint represents explicit conversion,
/// e.g. coercion constraint "as X" which forms a disjunction.
bool isExplicitConversion() const;
/// Retrieve the overload choice for an overload-binding constraint.
OverloadChoice getOverloadChoice() const {
assert(Kind == ConstraintKind::BindOverload);
return Overload.Choice;
}
/// Retrieve the DC in which the overload was used.
DeclContext *getOverloadUseDC() const {
assert(Kind == ConstraintKind::BindOverload);
return Overload.UseDC;
}
/// Retrieve the DC in which the member was used.
DeclContext *getMemberUseDC() const {
assert(Kind == ConstraintKind::ValueMember ||
Kind == ConstraintKind::UnresolvedValueMember);
return Member.UseDC;
}
/// \brief Retrieve the locator for this constraint.
ConstraintLocator *getLocator() const { return Locator; }
/// Clone the given constraint.
Constraint *clone(ConstraintSystem &cs) const;
void print(llvm::raw_ostream &Out, SourceManager *sm) const;
LLVM_ATTRIBUTE_DEPRECATED(
void dump(SourceManager *SM) const LLVM_ATTRIBUTE_USED,
"only for use within the debugger");
LLVM_ATTRIBUTE_DEPRECATED(
void dump(ConstraintSystem *CS) const LLVM_ATTRIBUTE_USED,
"only for use within the debugger");
void *operator new(size_t bytes, ConstraintSystem& cs,
size_t alignment = alignof(Constraint));
inline void operator delete(void *, const ConstraintSystem &cs, size_t) {}
void *operator new(size_t bytes, void *mem) { return mem; }
void operator delete(void *mem) { }
};
} // end namespace constraints
} // end namespace swift
namespace llvm {
/// Specialization of \c ilist_traits for constraints.
template<>
struct ilist_traits<swift::constraints::Constraint>
: public ilist_node_traits<swift::constraints::Constraint> {
using Element = swift::constraints::Constraint;
static Element *createNode(const Element &V) = delete;
static void deleteNode(Element *V) { /* never deleted */ }
};
} // end namespace llvm
#endif // LLVM_SWIFT_SEMA_CONSTRAINT_H