-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
SILDeclRef.h
447 lines (382 loc) · 16.1 KB
/
SILDeclRef.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
//===--- SILDeclRef.h - Defines the SILDeclRef struct -----------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file defines the SILDeclRef struct, which is used to identify a SIL
// global identifier that can be used as the operand of a FunctionRefInst
// instruction or that can have a SIL Function associated with it.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_SIL_SILDeclRef_H
#define SWIFT_SIL_SILDeclRef_H
#include "swift/AST/Decl.h"
#include "swift/AST/Expr.h"
#include "swift/AST/ResilienceExpansion.h"
#include "swift/AST/Types.h"
#include "llvm/ADT/Hashing.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/PointerUnion.h"
#include "llvm/Support/PrettyStackTrace.h"
namespace llvm {
class raw_ostream;
}
namespace swift {
class ValueDecl;
class AbstractClosureExpr;
class ClosureExpr;
class AutoClosureExpr;
class ASTContext;
class ClassDecl;
class SILFunctionType;
enum class SILLinkage : unsigned char;
class SILModule;
class SILLocation;
class AnyFunctionRef;
/// How a method is dispatched.
enum class MethodDispatch {
// The method implementation can be referenced statically.
Static,
// The method implementation uses class_method dispatch.
Class,
};
/// Get the method dispatch mechanism for a method.
MethodDispatch getMethodDispatch(AbstractFunctionDecl *method);
/// True if calling the given method or property should use ObjC dispatch.
bool requiresObjCDispatch(ValueDecl *vd);
/// True if the entry point is natively foreign.
bool requiresForeignToNativeThunk(ValueDecl *vd);
enum ForDefinition_t : bool {
NotForDefinition = false,
ForDefinition = true
};
/// \brief A key for referencing a Swift declaration in SIL.
///
/// This can currently be either a reference to a ValueDecl for functions,
/// methods, constructors, and other named entities, or a reference to a
/// AbstractClosureExpr for an anonymous function. In addition to the AST
/// reference, there are discriminators for referencing different
/// implementation-level entities associated with a single language-level
/// declaration, such as uncurry levels of a function, the allocating and
/// initializing entry points of a constructor, etc.
struct SILDeclRef {
typedef llvm::PointerUnion<ValueDecl *, AbstractClosureExpr *> Loc;
/// Represents the "kind" of the SILDeclRef. For some Swift decls there
/// are multiple SIL entry points, and the kind is used to distinguish them.
enum class Kind : unsigned {
/// \brief This constant references the FuncDecl or AbstractClosureExpr
/// in loc.
Func,
/// Allocator - this constant references the allocating constructor
/// entry point of a class ConstructorDecl or the constructor of a value
/// ConstructorDecl.
Allocator,
/// Initializer - this constant references the initializing constructor
/// entry point of the class ConstructorDecl in loc.
Initializer,
/// EnumElement - this constant references the injection function for
/// an EnumElementDecl.
EnumElement,
/// Destroyer - this constant references the destroying destructor for the
/// DestructorDecl in loc.
Destroyer,
/// Deallocator - this constant references the deallocating
/// destructor for the DestructorDecl in loc.
Deallocator,
/// GlobalAccessor - this constant references the lazy-initializing
/// accessor for the global VarDecl in loc.
GlobalAccessor,
/// GlobalGetter - this constant references the lazy-initializing
/// getter for the global VarDecl.
GlobalGetter,
/// References the generator for a default argument of a function.
DefaultArgGenerator,
/// References the ivar initializer for the ClassDecl in loc.
///
/// Only classes that are allocated using Objective-C's allocation
/// routines have an ivar initializer, which is emitted as
/// .cxx_construct.
IVarInitializer,
/// References the ivar destroyer for the ClassDecl in loc.
///
/// Only classes that are allocated using Objective-C's allocation
/// routines have an ivar destroyer, which is emitted as
/// .cxx_destruct.
IVarDestroyer,
};
/// The ValueDecl or AbstractClosureExpr represented by this SILDeclRef.
Loc loc;
/// The Kind of this SILDeclRef.
Kind kind : 4;
/// The uncurry level of this SILDeclRef.
unsigned uncurryLevel : 8;
/// The required resilience expansion of the declaration.
unsigned Expansion : 1;
/// True if the SILDeclRef is a curry thunk.
unsigned isCurried : 1;
/// True if this references a foreign entry point for the referenced decl.
unsigned isForeign : 1;
/// True if this is a direct reference to a class's method implementation
/// that isn't dynamically dispatched.
unsigned isDirectReference : 1;
/// The default argument index for a default argument getter.
unsigned defaultArgIndex : 10;
/// A magic value for SILDeclRef constructors to ask for the natural uncurry
/// level of the constant.
enum : unsigned { ConstructAtNaturalUncurryLevel = ~0U };
/// A magic value for SILDeclRef constructors to ask for the best
/// available resilience expansion of the constant.
static constexpr ResilienceExpansion ConstructAtBestResilienceExpansion
= /*TODO*/ ResilienceExpansion::Minimal;
/// Produces a null SILDeclRef.
SILDeclRef() : loc(), kind(Kind::Func), uncurryLevel(0), Expansion(0),
isCurried(0), isForeign(0), isDirectReference(0),
defaultArgIndex(0) {}
/// Produces a SILDeclRef of the given kind for the given decl.
explicit SILDeclRef(ValueDecl *decl, Kind kind,
ResilienceExpansion expansion
= ResilienceExpansion::Minimal,
unsigned uncurryLevel = ConstructAtNaturalUncurryLevel,
bool isForeign = false);
/// Produces the 'natural' SILDeclRef for the given ValueDecl or
/// AbstractClosureExpr:
/// - If 'loc' is a func or closure, this returns a Func SILDeclRef.
/// - If 'loc' is a ConstructorDecl, this returns the Allocator SILDeclRef
/// for the constructor.
/// - If 'loc' is an EnumElementDecl, this returns the EnumElement
/// SILDeclRef for the enum element.
/// - If 'loc' is a DestructorDecl, this returns the Destructor SILDeclRef
/// for the containing ClassDecl.
/// - If 'loc' is a global VarDecl, this returns its GlobalAccessor
/// SILDeclRef.
/// If the uncurry level is unspecified or specified as NaturalUncurryLevel,
/// then the SILDeclRef for the natural uncurry level of the definition is
/// used.
explicit SILDeclRef(Loc loc,
ResilienceExpansion expansion
= ResilienceExpansion::Minimal,
unsigned uncurryLevel = ConstructAtNaturalUncurryLevel,
bool isForeign = false);
/// Produce a SIL constant for a default argument generator.
static SILDeclRef getDefaultArgGenerator(Loc loc, unsigned defaultArgIndex);
bool isNull() const { return loc.isNull(); }
explicit operator bool() const { return !isNull(); }
bool hasDecl() const { return loc.is<ValueDecl *>(); }
bool hasClosureExpr() const {
return loc.is<AbstractClosureExpr *>()
&& isa<ClosureExpr>(getAbstractClosureExpr());
}
bool hasAutoClosureExpr() const {
return loc.is<AbstractClosureExpr *>()
&& isa<AutoClosureExpr>(getAbstractClosureExpr());
}
bool hasFuncDecl() const {
return loc.is<ValueDecl *>() && isa<FuncDecl>(getDecl());
}
ValueDecl *getDecl() const { return loc.get<ValueDecl *>(); }
AbstractClosureExpr *getAbstractClosureExpr() const {
return loc.dyn_cast<AbstractClosureExpr *>();
}
ClosureExpr *getClosureExpr() const {
return dyn_cast<ClosureExpr>(getAbstractClosureExpr());
}
AutoClosureExpr *getAutoClosureExpr() const {
return dyn_cast<AutoClosureExpr>(getAbstractClosureExpr());
}
FuncDecl *getFuncDecl() const { return dyn_cast<FuncDecl>(getDecl()); }
AbstractFunctionDecl *getAbstractFunctionDecl() const {
return dyn_cast<AbstractFunctionDecl>(getDecl());
}
Optional<AnyFunctionRef> getAnyFunctionRef() const;
SILLocation getAsRegularLocation() const;
/// Produce a mangled form of this constant.
///
/// If 'prefix' is non-empty, it will be used in place of the standard '_T'
/// prefix.
std::string mangle(StringRef prefix = {}) const;
/// True if the SILDeclRef references a function.
bool isFunc() const {
return kind == Kind::Func;
}
/// True if the SILDeclRef references a constructor entry point.
bool isConstructor() const {
return kind == Kind::Allocator || kind == Kind::Initializer;
}
/// True if the SILDeclRef references a destructor entry point.
bool isDestructor() const {
return kind == Kind::Destroyer || kind == Kind::Deallocator;
}
/// True if the SILDeclRef references an enum entry point.
bool isEnumElement() const {
return kind == Kind::EnumElement;
}
/// True if the SILDeclRef references a global variable accessor.
bool isGlobal() const {
return kind == Kind::GlobalAccessor || kind == Kind::GlobalGetter;
}
/// True if the SILDeclRef references the generator for a default argument of
/// a function.
bool isDefaultArgGenerator() const {
return kind == Kind::DefaultArgGenerator;
}
/// \brief True if the function should be treated as transparent.
bool isTransparent() const;
/// \brief True if the function has noinline attribute.
bool isNoinline() const;
/// \brief True if the function has __always inline attribute.
bool isAlwaysInline() const;
/// \return True if the function has an effects attribute.
bool hasEffectsAttribute() const;
/// \return the effects kind of the function.
EffectsKind getEffectsAttribute() const;
/// \brief Return the expected linkage of this declaration.
SILLinkage getLinkage(ForDefinition_t forDefinition) const;
/// \brief Return the hash code for the SIL declaration.
llvm::hash_code getHashCode() const {
return llvm::hash_combine(loc.getOpaqueValue(),
static_cast<int>(kind),
Expansion, uncurryLevel,
isForeign, isDirectReference,
defaultArgIndex);
}
bool operator==(SILDeclRef rhs) const {
return loc.getOpaqueValue() == rhs.loc.getOpaqueValue()
&& kind == rhs.kind
&& Expansion == rhs.Expansion
&& uncurryLevel == rhs.uncurryLevel
&& isForeign == rhs.isForeign
&& isDirectReference == rhs.isDirectReference
&& defaultArgIndex == rhs.defaultArgIndex;
}
bool operator!=(SILDeclRef rhs) const {
return !(*this == rhs);
}
void print(llvm::raw_ostream &os) const;
void dump() const;
ResilienceExpansion getResilienceExpansion() const {
return ResilienceExpansion(Expansion);
}
// Returns the SILDeclRef for an entity at a shallower uncurry level.
SILDeclRef atUncurryLevel(unsigned level) const {
assert(level <= uncurryLevel && "can't safely go to deeper uncurry level");
bool willBeCurried = isCurried || level < uncurryLevel;
// Curry thunks are never foreign.
bool willBeForeign = isForeign && !willBeCurried;
bool willBeDirect = isDirectReference;
return SILDeclRef(loc.getOpaqueValue(), kind, Expansion, level,
willBeCurried, willBeDirect, willBeForeign,
defaultArgIndex);
}
/// Returns the foreign (or native) entry point corresponding to the same
/// decl.
SILDeclRef asForeign(bool foreign = true) const {
return SILDeclRef(loc.getOpaqueValue(), kind, Expansion, uncurryLevel,
isCurried, isDirectReference, foreign, defaultArgIndex);
}
SILDeclRef asDirectReference(bool direct = true) const {
SILDeclRef r = *this;
// The 'direct' distinction only makes sense for curry thunks.
if (r.isCurried)
r.isDirectReference = direct;
return r;
}
/// True if the decl ref references a thunk from a natively foreign
/// declaration to Swift calling convention.
bool isForeignToNativeThunk() const;
/// True if the decl ref references a thunk from a natively Swift declaration
/// to foreign C or ObjC calling convention.
bool isNativeToForeignThunk() const;
/// Return a SILDeclRef to the declaration overridden by this one, or
/// a null SILDeclRef if there is no override.
SILDeclRef getOverridden() const {
if (!hasDecl())
return SILDeclRef();
auto overridden = getDecl()->getOverriddenDecl();
if (!overridden)
return SILDeclRef();
return SILDeclRef(overridden, kind, getResilienceExpansion(), uncurryLevel);
}
/// Return a SILDeclRef to the declaration whose vtable entry this declaration
/// overrides. This may be different from "getOverridden" because some
/// declarations do not always have vtable entries.
SILDeclRef getNextOverriddenVTableEntry() const;
/// Return a SILDeclRef referring to the ultimate base class's declaration,
/// which must be used with getConstantOverrideInfo.
SILDeclRef getBaseOverriddenVTableEntry() const;
/// True if the referenced entity is some kind of thunk.
bool isThunk() const;
/// True if the referenced entity is emitted by Swift on behalf of the Clang
/// importer.
bool isClangImported() const;
/// True if the referenced entity is emitted by Clang on behalf of the Clang
/// importer.
bool isClangGenerated() const;
bool isImplicit() const {
if (hasDecl())
return getDecl()->isImplicit();
return getAbstractClosureExpr()->isImplicit();
}
private:
friend struct llvm::DenseMapInfo<swift::SILDeclRef>;
/// Produces a SILDeclRef from an opaque value.
explicit SILDeclRef(void *opaqueLoc,
Kind kind,
unsigned rawExpansion,
unsigned uncurryLevel,
bool isCurried,
bool isDirectReference,
bool isForeign,
unsigned defaultArgIndex)
: loc(Loc::getFromOpaqueValue(opaqueLoc)),
kind(kind), uncurryLevel(uncurryLevel), Expansion(rawExpansion),
isCurried(isCurried),
isForeign(isForeign), isDirectReference(isDirectReference),
defaultArgIndex(defaultArgIndex)
{}
};
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, SILDeclRef C) {
C.print(OS);
return OS;
}
} // end swift namespace
namespace llvm {
// DenseMap key support for SILDeclRef.
template<> struct DenseMapInfo<swift::SILDeclRef> {
using SILDeclRef = swift::SILDeclRef;
using Kind = SILDeclRef::Kind;
using Loc = SILDeclRef::Loc;
using ResilienceExpansion = swift::ResilienceExpansion;
using PointerInfo = DenseMapInfo<void*>;
using UnsignedInfo = DenseMapInfo<unsigned>;
static SILDeclRef getEmptyKey() {
return SILDeclRef(PointerInfo::getEmptyKey(), Kind::Func,
0, 0, false, false, false, 0);
}
static SILDeclRef getTombstoneKey() {
return SILDeclRef(PointerInfo::getTombstoneKey(), Kind::Func,
0, 0, false, false, false, 0);
}
static unsigned getHashValue(swift::SILDeclRef Val) {
unsigned h1 = PointerInfo::getHashValue(Val.loc.getOpaqueValue());
unsigned h2 = UnsignedInfo::getHashValue(unsigned(Val.kind));
unsigned h3 = (Val.kind == Kind::DefaultArgGenerator)
? UnsignedInfo::getHashValue(Val.defaultArgIndex)
: UnsignedInfo::getHashValue(Val.uncurryLevel);
unsigned h4 = UnsignedInfo::getHashValue(Val.isForeign);
unsigned h5 = UnsignedInfo::getHashValue(Val.isDirectReference);
return h1 ^ (h2 << 4) ^ (h3 << 9) ^ (h4 << 7) ^ (h5 << 11);
}
static bool isEqual(swift::SILDeclRef const &LHS,
swift::SILDeclRef const &RHS) {
return LHS == RHS;
}
};
} // end llvm namespace
#endif