13
13
#include " swift/AST/Decl.h"
14
14
#include " swift/AST/Types.h"
15
15
#include " llvm/ADT/FoldingSet.h"
16
+ #include " llvm/ADT/PointerIntPair.h"
16
17
#include " llvm/Support/raw_ostream.h"
17
18
#include < algorithm>
18
19
#include < vector>
@@ -38,50 +39,40 @@ const StringRef Symbol::Kinds[] = {
38
39
// / the Storage type is the allocated backing storage.
39
40
struct Symbol ::Storage final
40
41
: public llvm::FoldingSetNode,
41
- public llvm::TrailingObjects<Storage, const ProtocolDecl * , Term> {
42
+ public llvm::TrailingObjects<Storage, unsigned , Term> {
42
43
friend class Symbol ;
43
44
44
- unsigned Kind : 3 ;
45
- unsigned NumSubstitutions : 14 ;
45
+ llvm::PointerIntPair<const ProtocolDecl *, 3 > ProtoAndKind;
46
46
47
47
union {
48
48
Identifier Name;
49
49
CanType ConcreteType;
50
50
LayoutConstraint Layout;
51
- const ProtocolDecl *Proto;
52
51
GenericTypeParamType *GenericParam;
53
52
};
54
53
55
54
explicit Storage (Identifier name) {
56
- Kind = unsigned (Symbol::Kind::Name);
57
- NumSubstitutions = 0 ;
55
+ ProtoAndKind.setInt (unsigned (Symbol::Kind::Name));
58
56
Name = name;
59
57
}
60
58
61
59
explicit Storage (LayoutConstraint layout) {
62
- Kind = unsigned (Symbol::Kind::Layout);
63
- NumSubstitutions = 0 ;
60
+ ProtoAndKind.setInt (unsigned (Symbol::Kind::Layout));
64
61
Layout = layout;
65
62
}
66
63
67
64
explicit Storage (const ProtocolDecl *proto) {
68
- Kind = unsigned (Symbol::Kind::Protocol);
69
- NumSubstitutions = 0 ;
70
- Proto = proto;
65
+ ProtoAndKind.setPointerAndInt (proto, unsigned (Symbol::Kind::Protocol));
71
66
}
72
67
73
68
explicit Storage (GenericTypeParamType *param) {
74
- Kind = unsigned (Symbol::Kind::GenericParam);
75
- NumSubstitutions = 0 ;
69
+ ProtoAndKind.setInt (unsigned (Symbol::Kind::GenericParam));
76
70
GenericParam = param;
77
71
}
78
72
79
73
Storage (const ProtocolDecl *proto, Identifier name) {
80
- Kind = unsigned (Symbol::Kind::AssociatedType);
81
- NumSubstitutions = 0 ;
74
+ ProtoAndKind.setPointerAndInt (proto, unsigned (Symbol::Kind::AssociatedType));
82
75
Name = name;
83
-
84
- *getTrailingObjects<const ProtocolDecl *>() = proto;
85
76
}
86
77
87
78
Storage (Symbol::Kind kind, CanType type, ArrayRef<Term> substitutions) {
@@ -90,10 +81,11 @@ struct Symbol::Storage final
90
81
assert (!type->hasTypeVariable ());
91
82
assert (type->hasTypeParameter () != substitutions.empty ());
92
83
93
- Kind = unsigned (kind);
94
- NumSubstitutions = substitutions.size ();
84
+ ProtoAndKind.setInt (unsigned (kind));
95
85
ConcreteType = type;
96
86
87
+ *getTrailingObjects<unsigned >() = substitutions.size ();
88
+
97
89
for (unsigned i : indices (substitutions))
98
90
getSubstitutions ()[i] = substitutions[i];
99
91
}
@@ -102,38 +94,44 @@ struct Symbol::Storage final
102
94
assert (!type->hasTypeVariable ());
103
95
assert (type->hasTypeParameter () != substitutions.empty ());
104
96
105
- Kind = unsigned (Symbol::Kind::ConcreteConformance);
106
- NumSubstitutions = substitutions.size ();
97
+ ProtoAndKind.setPointerAndInt (proto, unsigned (Symbol::Kind::ConcreteConformance));
98
+
99
+ *getTrailingObjects<unsigned >() = substitutions.size ();
107
100
ConcreteType = type;
108
101
109
102
for (unsigned i : indices (substitutions))
110
103
getSubstitutions ()[i] = substitutions[i];
111
-
112
- *getTrailingObjects<const ProtocolDecl *>() = proto;
113
104
}
114
105
115
- size_t numTrailingObjects (OverloadToken<const ProtocolDecl *>) const {
116
- return (Kind == unsigned (Symbol::Kind::AssociatedType) ||
117
- Kind == unsigned (Symbol::Kind::ConcreteConformance));
106
+ size_t numTrailingObjects (OverloadToken<unsigned >) const {
107
+ auto kind = Symbol::Kind (ProtoAndKind.getInt ());
108
+ return (kind == Symbol::Kind::Superclass ||
109
+ kind == Symbol::Kind::ConcreteType ||
110
+ kind == Symbol::Kind::ConcreteConformance);
118
111
}
119
112
120
113
size_t numTrailingObjects (OverloadToken<Term>) const {
121
- return NumSubstitutions;
114
+ return getNumSubstitutions ();
115
+ }
116
+
117
+ unsigned getNumSubstitutions () const {
118
+ assert (numTrailingObjects (OverloadToken<unsigned >()) == 1 );
119
+ return *getTrailingObjects<unsigned >();
122
120
}
123
121
124
122
MutableArrayRef<Term> getSubstitutions () {
125
- return {getTrailingObjects<Term>(), NumSubstitutions };
123
+ return {getTrailingObjects<Term>(), getNumSubstitutions () };
126
124
}
127
125
128
126
ArrayRef<Term> getSubstitutions () const {
129
- return {getTrailingObjects<Term>(), NumSubstitutions };
127
+ return {getTrailingObjects<Term>(), getNumSubstitutions () };
130
128
}
131
129
132
130
void Profile (llvm::FoldingSetNodeID &id) const ;
133
131
};
134
132
135
133
Symbol::Kind Symbol::getKind () const {
136
- return Kind (Ptr->Kind );
134
+ return Kind (Ptr->ProtoAndKind . getInt () );
137
135
}
138
136
139
137
// / Get the identifier associated with an unbound name symbol or an
@@ -147,12 +145,10 @@ Identifier Symbol::getName() const {
147
145
// / Get the protocol declaration associated with a protocol or associated type
148
146
// / symbol.
149
147
const ProtocolDecl *Symbol::getProtocol () const {
150
- if (getKind () == Kind::Protocol)
151
- return Ptr->Proto ;
152
-
153
- assert (getKind () == Kind::AssociatedType ||
148
+ assert (getKind () == Kind::Protocol ||
149
+ getKind () == Kind::AssociatedType ||
154
150
getKind () == Kind::ConcreteConformance);
155
- return * Ptr->getTrailingObjects < const ProtocolDecl *> ();
151
+ return Ptr->ProtoAndKind . getPointer ();
156
152
}
157
153
158
154
// / Get the generic parameter associated with a generic parameter symbol.
@@ -179,9 +175,6 @@ CanType Symbol::getConcreteType() const {
179
175
// / Get the list of substitution terms associated with a superclass,
180
176
// / concrete type or concrete conformance symbol.
181
177
ArrayRef<Term> Symbol::getSubstitutions () const {
182
- assert (getKind () == Kind::Superclass ||
183
- getKind () == Kind::ConcreteType ||
184
- getKind () == Kind::ConcreteConformance);
185
178
return Ptr->getSubstitutions ();
186
179
}
187
180
@@ -196,7 +189,7 @@ Symbol Symbol::forName(Identifier name,
196
189
if (auto *symbol = ctx.Symbols .FindNodeOrInsertPos (id, insertPos))
197
190
return symbol;
198
191
199
- unsigned size = Storage::totalSizeToAlloc<const ProtocolDecl * , Term>(0 , 0 );
192
+ unsigned size = Storage::totalSizeToAlloc<unsigned , Term>(0 , 0 );
200
193
void *mem = ctx.Allocator .Allocate (size, alignof (Storage));
201
194
auto *symbol = new (mem) Storage (name);
202
195
@@ -225,7 +218,7 @@ Symbol Symbol::forProtocol(const ProtocolDecl *proto,
225
218
if (auto *symbol = ctx.Symbols .FindNodeOrInsertPos (id, insertPos))
226
219
return symbol;
227
220
228
- unsigned size = Storage::totalSizeToAlloc<const ProtocolDecl * , Term>(0 , 0 );
221
+ unsigned size = Storage::totalSizeToAlloc<unsigned , Term>(0 , 0 );
229
222
void *mem = ctx.Allocator .Allocate (size, alignof (Storage));
230
223
auto *symbol = new (mem) Storage (proto);
231
224
@@ -254,8 +247,7 @@ Symbol Symbol::forAssociatedType(const ProtocolDecl *proto,
254
247
if (auto *symbol = ctx.Symbols .FindNodeOrInsertPos (id, insertPos))
255
248
return symbol;
256
249
257
- unsigned size = Storage::totalSizeToAlloc<const ProtocolDecl *, Term>(
258
- 1 , 0 );
250
+ unsigned size = Storage::totalSizeToAlloc<unsigned , Term>(0 , 0 );
259
251
void *mem = ctx.Allocator .Allocate (size, alignof (Storage));
260
252
auto *symbol = new (mem) Storage (proto, name);
261
253
@@ -286,7 +278,7 @@ Symbol Symbol::forGenericParam(GenericTypeParamType *param,
286
278
if (auto *symbol = ctx.Symbols .FindNodeOrInsertPos (id, insertPos))
287
279
return symbol;
288
280
289
- unsigned size = Storage::totalSizeToAlloc<const ProtocolDecl * , Term>(0 , 0 );
281
+ unsigned size = Storage::totalSizeToAlloc<unsigned , Term>(0 , 0 );
290
282
void *mem = ctx.Allocator .Allocate (size, alignof (Storage));
291
283
auto *symbol = new (mem) Storage (param);
292
284
@@ -313,7 +305,7 @@ Symbol Symbol::forLayout(LayoutConstraint layout,
313
305
if (auto *symbol = ctx.Symbols .FindNodeOrInsertPos (id, insertPos))
314
306
return symbol;
315
307
316
- unsigned size = Storage::totalSizeToAlloc<const ProtocolDecl * , Term>(0 , 0 );
308
+ unsigned size = Storage::totalSizeToAlloc<unsigned , Term>(0 , 0 );
317
309
void *mem = ctx.Allocator .Allocate (size, alignof (Storage));
318
310
auto *symbol = new (mem) Storage (layout);
319
311
@@ -344,8 +336,8 @@ Symbol Symbol::forSuperclass(CanType type, ArrayRef<Term> substitutions,
344
336
if (auto *symbol = ctx.Symbols .FindNodeOrInsertPos (id, insertPos))
345
337
return symbol;
346
338
347
- unsigned size = Storage::totalSizeToAlloc<const ProtocolDecl * , Term>(
348
- 0 , substitutions.size ());
339
+ unsigned size = Storage::totalSizeToAlloc<unsigned , Term>(
340
+ 1 , substitutions.size ());
349
341
void *mem = ctx.Allocator .Allocate (size, alignof (Storage));
350
342
auto *symbol = new (mem) Storage (Kind::Superclass, type, substitutions);
351
343
@@ -375,8 +367,8 @@ Symbol Symbol::forConcreteType(CanType type, ArrayRef<Term> substitutions,
375
367
if (auto *symbol = ctx.Symbols .FindNodeOrInsertPos (id, insertPos))
376
368
return symbol;
377
369
378
- unsigned size = Storage::totalSizeToAlloc<const ProtocolDecl * , Term>(
379
- 0 , substitutions.size ());
370
+ unsigned size = Storage::totalSizeToAlloc<unsigned , Term>(
371
+ 1 , substitutions.size ());
380
372
void *mem = ctx.Allocator .Allocate (size, alignof (Storage));
381
373
auto *symbol = new (mem) Storage (Kind::ConcreteType, type, substitutions);
382
374
@@ -399,18 +391,18 @@ Symbol Symbol::forConcreteConformance(CanType type,
399
391
RewriteContext &ctx) {
400
392
llvm::FoldingSetNodeID id;
401
393
id.AddInteger (unsigned (Kind::ConcreteConformance));
394
+ id.AddPointer (proto);
402
395
id.AddPointer (type.getPointer ());
403
396
id.AddInteger (unsigned (substitutions.size ()));
404
397
for (auto substitution : substitutions)
405
398
id.AddPointer (substitution.getOpaquePointer ());
406
- id.AddPointer (proto);
407
399
408
400
void *insertPos = nullptr ;
409
401
if (auto *symbol = ctx.Symbols .FindNodeOrInsertPos (id, insertPos))
410
402
return symbol;
411
403
412
- unsigned size = Storage::totalSizeToAlloc<const ProtocolDecl * , Term>(
413
- /* protos= */ 1 , substitutions.size ());
404
+ unsigned size = Storage::totalSizeToAlloc<unsigned , Term>(
405
+ 1 , substitutions.size ());
414
406
void *mem = ctx.Allocator .Allocate (size, alignof (Storage));
415
407
auto *symbol = new (mem) Storage (type, substitutions, proto);
416
408
@@ -712,9 +704,9 @@ void Symbol::dump(llvm::raw_ostream &out) const {
712
704
}
713
705
714
706
void Symbol::Storage::Profile (llvm::FoldingSetNodeID &id) const {
715
- id.AddInteger (Kind );
707
+ id.AddInteger (ProtoAndKind. getInt () );
716
708
717
- switch (Symbol::Kind (Kind )) {
709
+ switch (Symbol::Kind (ProtoAndKind. getInt () )) {
718
710
case Symbol::Kind::Name:
719
711
id.AddPointer (Name.get ());
720
712
return ;
@@ -724,38 +716,37 @@ void Symbol::Storage::Profile(llvm::FoldingSetNodeID &id) const {
724
716
return ;
725
717
726
718
case Symbol::Kind::Protocol:
727
- id.AddPointer (Proto );
719
+ id.AddPointer (ProtoAndKind. getPointer () );
728
720
return ;
729
721
730
722
case Symbol::Kind::GenericParam:
731
723
id.AddPointer (GenericParam);
732
724
return ;
733
725
734
726
case Symbol::Kind::AssociatedType: {
735
- id.AddPointer (*getTrailingObjects< const ProtocolDecl *> ());
727
+ id.AddPointer (ProtoAndKind. getPointer ());
736
728
id.AddPointer (Name.get ());
737
729
return ;
738
730
}
739
731
740
732
case Symbol::Kind::Superclass:
741
733
case Symbol::Kind::ConcreteType: {
742
734
id.AddPointer (ConcreteType.getPointer ());
743
-
744
- id.AddInteger (NumSubstitutions);
735
+ id.AddInteger (getNumSubstitutions ());
745
736
for (auto term : getSubstitutions ())
746
737
id.AddPointer (term.getOpaquePointer ());
747
738
748
739
return ;
749
740
}
750
741
751
742
case Symbol::Kind::ConcreteConformance: {
743
+ id.AddPointer (ProtoAndKind.getPointer ());
752
744
id.AddPointer (ConcreteType.getPointer ());
753
745
754
- id.AddInteger (NumSubstitutions );
746
+ id.AddInteger (getNumSubstitutions () );
755
747
for (auto term : getSubstitutions ())
756
748
id.AddPointer (term.getOpaquePointer ());
757
749
758
- id.AddPointer (*getTrailingObjects<const ProtocolDecl *>());
759
750
return ;
760
751
}
761
752
}
0 commit comments