Skip to content

Commit 9121a6e

Browse files
committed
[CS] Define locator path elements with macros
This allows us to define a `LocatorPathElt` subclass for each path element kind.
1 parent d1c87f3 commit 9121a6e

File tree

2 files changed

+185
-110
lines changed

2 files changed

+185
-110
lines changed

lib/Sema/ConstraintLocator.h

Lines changed: 15 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -53,90 +53,9 @@ class ConstraintLocator : public llvm::FoldingSetNode {
5353
/// Describes the kind of a particular path element, e.g.,
5454
/// "tuple element", "call result", "base of member lookup", etc.
5555
enum PathElementKind : unsigned char {
56-
/// The argument of function application.
57-
ApplyArgument,
58-
/// The function being applied.
59-
ApplyFunction,
60-
/// Matching an argument to a parameter.
61-
ApplyArgToParam,
62-
/// A generic parameter being opened.
63-
///
64-
/// Also contains the generic parameter type itself.
65-
GenericParameter,
66-
/// The argument type of a function.
67-
FunctionArgument,
68-
/// The result type of a function.
69-
FunctionResult,
70-
/// A tuple element referenced by position.
71-
TupleElement,
72-
/// A tuple element referenced by name.
73-
NamedTupleElement,
74-
/// An optional payload.
75-
OptionalPayload,
76-
/// A generic argument.
77-
/// FIXME: Add support for named generic arguments?
78-
GenericArgument,
79-
/// A member.
80-
/// FIXME: Do we need the actual member name here?
81-
Member,
82-
/// An unresolved member.
83-
UnresolvedMember,
84-
/// The base of a member expression.
85-
MemberRefBase,
86-
/// The lookup for a subscript member.
87-
SubscriptMember,
88-
/// The lookup for a constructor member.
89-
ConstructorMember,
90-
/// An implicit @lvalue-to-inout conversion; only valid for operator
91-
/// arguments.
92-
LValueConversion,
93-
/// RValue adjustment.
94-
RValueAdjustment,
95-
/// The result of a closure.
96-
ClosureResult,
97-
/// The parent of a nested type.
98-
ParentType,
99-
/// The superclass of a protocol existential type.
100-
ExistentialSuperclassType,
101-
/// The instance of a metatype type.
102-
InstanceType,
103-
/// The element type of a sequence in a for ... in ... loop.
104-
SequenceElementType,
105-
/// An argument passed in an autoclosure parameter
106-
/// position, which must match the autoclosure return type.
107-
AutoclosureResult,
108-
/// The requirement that we're matching during protocol conformance
109-
/// checking.
110-
Requirement,
111-
/// The candidate witness during protocol conformance checking.
112-
Witness,
113-
/// This is referring to a type produced by opening a generic type at the
114-
/// base of the locator.
115-
OpenedGeneric,
116-
/// A component of a key path.
117-
KeyPathComponent,
118-
/// The Nth conditional requirement in the parent locator's conformance.
119-
ConditionalRequirement,
120-
/// A single requirement placed on the type parameters.
121-
TypeParameterRequirement,
122-
/// Locator for a binding from an IUO disjunction choice.
123-
ImplicitlyUnwrappedDisjunctionChoice,
124-
/// A result of an expression involving dynamic lookup.
125-
DynamicLookupResult,
126-
/// The desired contextual type passed in to the constraint system.
127-
ContextualType,
128-
/// The missing argument synthesized by the solver.
129-
SynthesizedArgument,
130-
/// The member looked up via keypath based dynamic lookup.
131-
KeyPathDynamicMember,
132-
/// The type of the key path expression
133-
KeyPathType,
134-
/// The root of a key path
135-
KeyPathRoot,
136-
/// The value of a key path
137-
KeyPathValue,
138-
/// The result type of a key path component. Not used for subscripts.
139-
KeyPathComponentResult,
56+
#define LOCATOR_PATH_ELT(Name) Name,
57+
#define ABSTRACT_LOCATOR_PATH_ELT(Name)
58+
#include "ConstraintLocatorPathElts.def"
14059
};
14160

14261
/// Determine the number of numeric values used for the given path
@@ -356,23 +275,8 @@ class ConstraintLocator : public llvm::FoldingSetNode {
356275
friend class ConstraintLocator;
357276

358277
public:
359-
class ApplyArgToParam;
360-
class SynthesizedArgument;
361-
class AnyTupleElement;
362-
class TupleElement;
363-
class NamedTupleElement;
364-
class KeyPathComponent;
365-
class GenericArgument;
366-
class AnyRequirement;
367-
class ConditionalRequirement;
368-
class TypeParameterRequirement;
369-
class ContextualType;
370-
class Witness;
371-
class Requirement;
372-
class GenericParameter;
373-
class OpenedGeneric;
374-
class KeyPathDynamicMember;
375-
class UnresolvedMember;
278+
#define LOCATOR_PATH_ELT(Name) class Name;
279+
#include "ConstraintLocatorPathElts.def"
376280

377281
PathElement(PathElementKind kind)
378282
: storage(encodeStorage(kind, 0)), storedKind(StoredKindAndValue)
@@ -701,6 +605,16 @@ template <class X>
701605
inline typename llvm::cast_retty<X, LocatorPathElt>::ret_type
702606
dyn_cast(const LocatorPathElt &) = delete; // Use LocatorPathElt::getAs instead.
703607

608+
#define SIMPLE_LOCATOR_PATH_ELT(Name) \
609+
class LocatorPathElt:: Name final : public LocatorPathElt { \
610+
public: \
611+
Name () : LocatorPathElt(ConstraintLocator:: Name) {} \
612+
\
613+
static bool classof(const LocatorPathElt *elt) { \
614+
return elt->getKind() == ConstraintLocator:: Name; \
615+
} \
616+
};
617+
#include "ConstraintLocatorPathElts.def"
704618

705619
// The following LocatorPathElt subclasses are used to expose accessors for
706620
// specific path element information. They shouldn't introduce additional
@@ -928,15 +842,6 @@ class LocatorPathElt::KeyPathDynamicMember final : public LocatorPathElt {
928842
}
929843
};
930844

931-
class LocatorPathElt::UnresolvedMember final : public LocatorPathElt {
932-
public:
933-
UnresolvedMember() : LocatorPathElt(PathElementKind::UnresolvedMember, 0) {}
934-
935-
static bool classof(const LocatorPathElt *elt) {
936-
return elt->getKind() == ConstraintLocator::UnresolvedMember;
937-
}
938-
};
939-
940845
/// A simple stack-only builder object that constructs a
941846
/// constraint locator without allocating memory.
942847
///
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
//===--- ConstraintLocatorPathElts.def - Constraint Locator Path Elements -===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2019 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
///
13+
/// This file enumerates the elements that can make up the path of a
14+
/// ConstraintLocator.
15+
///
16+
//===----------------------------------------------------------------------===//
17+
18+
/// Describes any kind of path element.
19+
#ifndef LOCATOR_PATH_ELT
20+
#define LOCATOR_PATH_ELT(Name)
21+
#endif
22+
23+
/// Defines a path element which is characterized only by its kind, and as such
24+
/// doesn't store additional values.
25+
#ifndef SIMPLE_LOCATOR_PATH_ELT
26+
#define SIMPLE_LOCATOR_PATH_ELT(Name) LOCATOR_PATH_ELT(Name)
27+
#endif
28+
29+
/// Defines a path element that requires a class definition to be provided in
30+
/// order to expose things like accessors for path element info.
31+
#ifndef CUSTOM_LOCATOR_PATH_ELT
32+
#define CUSTOM_LOCATOR_PATH_ELT(Name) LOCATOR_PATH_ELT(Name)
33+
#endif
34+
35+
/// Defines an abstract path element superclass which doesn't itself have a path
36+
/// element kind.
37+
#ifndef ABSTRACT_LOCATOR_PATH_ELT
38+
#define ABSTRACT_LOCATOR_PATH_ELT(Name) CUSTOM_LOCATOR_PATH_ELT(Name)
39+
#endif
40+
41+
/// Matching an argument to a parameter.
42+
CUSTOM_LOCATOR_PATH_ELT(ApplyArgToParam)
43+
44+
/// The argument of function application.
45+
SIMPLE_LOCATOR_PATH_ELT(ApplyArgument)
46+
47+
/// The function being applied.
48+
SIMPLE_LOCATOR_PATH_ELT(ApplyFunction)
49+
50+
/// An argument passed in an autoclosure parameter
51+
/// position, which must match the autoclosure return type.
52+
SIMPLE_LOCATOR_PATH_ELT(AutoclosureResult)
53+
54+
/// The result of a closure.
55+
SIMPLE_LOCATOR_PATH_ELT(ClosureResult)
56+
57+
/// The lookup for a constructor member.
58+
SIMPLE_LOCATOR_PATH_ELT(ConstructorMember)
59+
60+
/// The desired contextual type passed in to the constraint system.
61+
CUSTOM_LOCATOR_PATH_ELT(ContextualType)
62+
63+
/// A result of an expression involving dynamic lookup.
64+
SIMPLE_LOCATOR_PATH_ELT(DynamicLookupResult)
65+
66+
/// The superclass of a protocol existential type.
67+
SIMPLE_LOCATOR_PATH_ELT(ExistentialSuperclassType)
68+
69+
/// The argument type of a function.
70+
SIMPLE_LOCATOR_PATH_ELT(FunctionArgument)
71+
72+
/// The result type of a function.
73+
SIMPLE_LOCATOR_PATH_ELT(FunctionResult)
74+
75+
/// A generic argument.
76+
/// FIXME: Add support for named generic arguments?
77+
CUSTOM_LOCATOR_PATH_ELT(GenericArgument)
78+
79+
/// Locator for a binding from an IUO disjunction choice.
80+
SIMPLE_LOCATOR_PATH_ELT(ImplicitlyUnwrappedDisjunctionChoice)
81+
82+
/// The instance of a metatype type.
83+
SIMPLE_LOCATOR_PATH_ELT(InstanceType)
84+
85+
/// A generic parameter being opened.
86+
///
87+
/// Also contains the generic parameter type itself.
88+
CUSTOM_LOCATOR_PATH_ELT(GenericParameter)
89+
90+
/// A component of a key path.
91+
CUSTOM_LOCATOR_PATH_ELT(KeyPathComponent)
92+
93+
/// The result type of a key path component. Not used for subscripts.
94+
SIMPLE_LOCATOR_PATH_ELT(KeyPathComponentResult)
95+
96+
/// The member looked up via keypath based dynamic lookup.
97+
CUSTOM_LOCATOR_PATH_ELT(KeyPathDynamicMember)
98+
99+
/// The root of a key path.
100+
SIMPLE_LOCATOR_PATH_ELT(KeyPathRoot)
101+
102+
/// The type of the key path expression.
103+
SIMPLE_LOCATOR_PATH_ELT(KeyPathType)
104+
105+
/// The value of a key path.
106+
SIMPLE_LOCATOR_PATH_ELT(KeyPathValue)
107+
108+
/// An implicit @lvalue-to-inout conversion; only valid for operator
109+
/// arguments.
110+
SIMPLE_LOCATOR_PATH_ELT(LValueConversion)
111+
112+
/// A member.
113+
/// FIXME: Do we need the actual member name here?
114+
SIMPLE_LOCATOR_PATH_ELT(Member)
115+
116+
/// The base of a member expression.
117+
SIMPLE_LOCATOR_PATH_ELT(MemberRefBase)
118+
119+
/// This is referring to a type produced by opening a generic type at the
120+
/// base of the locator.
121+
CUSTOM_LOCATOR_PATH_ELT(OpenedGeneric)
122+
123+
/// An optional payload.
124+
SIMPLE_LOCATOR_PATH_ELT(OptionalPayload)
125+
126+
/// The parent of a nested type.
127+
SIMPLE_LOCATOR_PATH_ELT(ParentType)
128+
129+
/// The requirement that we're matching during protocol conformance
130+
/// checking.
131+
CUSTOM_LOCATOR_PATH_ELT(Requirement)
132+
133+
/// Type parameter requirements.
134+
ABSTRACT_LOCATOR_PATH_ELT(AnyRequirement)
135+
/// The Nth conditional requirement in the parent locator's conformance.
136+
CUSTOM_LOCATOR_PATH_ELT(ConditionalRequirement)
137+
138+
/// A single requirement placed on the type parameters.
139+
CUSTOM_LOCATOR_PATH_ELT(TypeParameterRequirement)
140+
141+
/// RValue adjustment.
142+
SIMPLE_LOCATOR_PATH_ELT(RValueAdjustment)
143+
144+
/// The element type of a sequence in a for ... in ... loop.
145+
SIMPLE_LOCATOR_PATH_ELT(SequenceElementType)
146+
147+
/// The lookup for a subscript member.
148+
SIMPLE_LOCATOR_PATH_ELT(SubscriptMember)
149+
150+
/// The missing argument synthesized by the solver.
151+
CUSTOM_LOCATOR_PATH_ELT(SynthesizedArgument)
152+
153+
/// Tuple elements.
154+
ABSTRACT_LOCATOR_PATH_ELT(AnyTupleElement)
155+
/// A tuple element referenced by position.
156+
CUSTOM_LOCATOR_PATH_ELT(TupleElement)
157+
158+
/// A tuple element referenced by name.
159+
CUSTOM_LOCATOR_PATH_ELT(NamedTupleElement)
160+
161+
/// An unresolved member.
162+
SIMPLE_LOCATOR_PATH_ELT(UnresolvedMember)
163+
164+
/// The candidate witness during protocol conformance checking.
165+
CUSTOM_LOCATOR_PATH_ELT(Witness)
166+
167+
#undef LOCATOR_PATH_ELT
168+
#undef CUSTOM_LOCATOR_PATH_ELT
169+
#undef SIMPLE_LOCATOR_PATH_ELT
170+
#undef ABSTRACT_LOCATOR_PATH_ELT

0 commit comments

Comments
 (0)