-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathSILGen.h
296 lines (243 loc) · 11.4 KB
/
SILGen.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
//===--- SILGen.h - Implements Lowering of ASTs -> SIL ----------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 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
//
//===----------------------------------------------------------------------===//
#ifndef SILGEN_H
#define SILGEN_H
#include "ASTVisitor.h"
#include "Cleanup.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/DiagnosticEngine.h"
#include "swift/Basic/Optional.h"
#include "swift/SIL/SILDebugScope.h"
#include "swift/SIL/SILFunction.h"
#include "swift/SIL/SILModule.h"
#include "swift/SIL/TypeLowering.h"
#include "llvm/ADT/DenseMap.h"
namespace swift {
class SILBasicBlock;
namespace Lowering {
class LogicalPathComponent;
class LValue;
class RValue;
class RValueSource;
class ManagedValue;
class TypeConverter;
class SILGenFunction;
class Initialization;
class RValueSource;
class TemporaryInitialization;
/// An enum to indicate whether a protocol method requirement is satisfied by
/// a free function, as for an operator requirement.
enum IsFreeFunctionWitness_t : bool {
IsNotFreeFunctionWitness = false,
IsFreeFunctionWitness = true,
};
/// An enum to indicate whether a protocol method requirement is satisfied by a
/// method with an abstraction difference in the inout-ness of its self
/// parameter.
enum HasInOutSelfAbstractionDifference_t : bool {
DoesNotHaveInOutSelfAbstractionDifference = false,
HasInOutSelfAbstractionDifference = true,
};
/// An ASTVisitor for generating SIL from top-level declarations in a module.
class LLVM_LIBRARY_VISIBILITY SILGenModule : public ASTVisitor<SILGenModule> {
public:
/// The Module being constructed.
SILModule &M;
/// The type converter for the module.
TypeConverter &Types;
/// The Swift module we are visiting.
Module *SwiftModule;
/// TopLevelSGF - The SILGenFunction used to visit top-level code, or null if
/// the current source file is not a script source file.
SILGenFunction /*nullable*/ *TopLevelSGF;
/// Mapping from SILDeclRefs to emitted SILFunctions.
llvm::DenseMap<SILDeclRef, SILFunction*> emittedFunctions;
/// Mapping from ProtocolConformances to emitted SILWitnessTables.
llvm::DenseMap<ProtocolConformance*, SILWitnessTable*> emittedWitnessTables;
SILFunction *emitTopLevelFunction(SILLocation Loc);
size_t anonymousSymbolCounter = 0;
Optional<SILDeclRef> StringToNSStringFn;
Optional<SILDeclRef> NSStringToStringFn;
Optional<SILDeclRef> AnyObjectArrayToNSArrayFn;
Optional<SILDeclRef> NSArrayToAnyObjectArrayFn;
Optional<SILDeclRef> BoolToObjCBoolFn;
Optional<SILDeclRef> ObjCBoolToBoolFn;
Optional<SILDeclRef> CMutableVoidPointerToCOpaquePointerFn;
Optional<SILDeclRef> CConstVoidPointerToCOpaquePointerFn;
Optional<SILDeclRef> COpaquePointerToCMutableVoidPointerFn;
Optional<SILDeclRef> COpaquePointerToCConstVoidPointerFn;
Optional<SILDeclRef> StringDefaultInitFn;
Optional<SILDeclRef> ArrayDefaultInitFn;
Optional<SILDeclRef> CConstPointerToUnsafePointerFn;
Optional<SILDeclRef> CMutablePointerToUnsafePointerFn;
Optional<SILDeclRef> ObjCMutablePointerToUnsafePointerFn;
Optional<SILDeclRef> UnsafePointerToCConstPointerFn;
Optional<SILDeclRef> UnsafePointerToCMutablePointerFn;
Optional<SILDeclRef> UnsafePointerToObjCMutablePointerFn;
public:
SILGenModule(SILModule &M, Module *SM);
~SILGenModule();
SILGenModule(SILGenModule const &) = delete;
void operator=(SILGenModule const &) = delete;
ASTContext &getASTContext() { return M.getASTContext(); }
/// Returns the type of a constant reference.
SILType getConstantType(SILDeclRef constant);
/// Returns the calling convention for a function.
AbstractCC getConstantCC(SILDeclRef constant) {
return getConstantType(constant).getAbstractCC();
}
/// Get the function for a SILDeclRef.
SILFunction *getFunction(SILDeclRef constant,
ForDefinition_t forDefinition);
/// True if a function has been emitted for a given SILDeclRef.
bool hasFunction(SILDeclRef constant);
/// Get the lowered type for a Swift type.
SILType getLoweredType(Type t) {
return Types.getTypeLowering(t).getLoweredType();
}
/// Get or create the declaration of a reabstraction thunk with the
/// given signature.
SILFunction *getOrCreateReabstractionThunk(SILLocation loc,
GenericParamList *thunkContextParams,
CanSILFunctionType thunkType,
CanSILFunctionType fromType,
CanSILFunctionType toType);
//===--------------------------------------------------------------------===//
// Visitors for top-level forms
//===--------------------------------------------------------------------===//
// FIXME: visit other decls
void visitFuncDecl(FuncDecl *fd);
void visitPatternBindingDecl(PatternBindingDecl *vd);
void visitTopLevelCodeDecl(TopLevelCodeDecl *td);
void visitIfConfigDecl(IfConfigDecl *icd);
void visitNominalTypeDecl(NominalTypeDecl *ntd);
void visitExtensionDecl(ExtensionDecl *ed);
void visitVarDecl(VarDecl *vd);
void emitAbstractFuncDecl(AbstractFunctionDecl *AFD);
/// Generate code for a source file of the module.
void emitSourceFile(SourceFile *sf, unsigned startElem);
/// Generates code for the given FuncDecl and adds the
/// SILFunction to the current SILModule under the name SILDeclRef(decl). For
/// curried functions, curried entry point Functions are also generated and
/// added to the current SILModule.
void emitFunction(FuncDecl *fd);
/// \brief Generates code for the given closure expression and adds the
/// SILFunction to the current SILModule under the nane SILDeclRef(ce).
void emitClosure(AbstractClosureExpr *ce);
/// Generates code for the given ConstructorDecl and adds
/// the SILFunction to the current SILModule under the name SILDeclRef(decl).
void emitConstructor(ConstructorDecl *decl);
/// Generates code for the given class's destructor and adds
/// the SILFunction to the current SILModule under the name
/// SILDeclRef(cd, Destructor).
void emitDestructor(ClassDecl *cd, DestructorDecl *dd);
/// Generates the enum constructor for the given
/// EnumElementDecl under the name SILDeclRef(decl).
void emitEnumConstructor(EnumElementDecl *decl);
/// Emits the default argument generator with the given expression.
void emitDefaultArgGenerator(SILDeclRef constant, Expr *arg);
/// Emits the default argument generator for the given function.
void emitDefaultArgGenerators(SILDeclRef::Loc decl,
ArrayRef<Pattern*> patterns);
/// Emits the curry thunk between two uncurry levels of a function.
void emitCurryThunk(SILDeclRef entryPoint,
SILDeclRef nextEntryPoint,
FuncDecl *fd);
/// Emits a thunk from a foreign function to the native Swift conventions.
void emitForeignThunk(SILDeclRef thunk);
template<typename T>
SILFunction *preEmitFunction(SILDeclRef constant, T *astNode,
SILLocation L);
void postEmitFunction(SILDeclRef constant, SILFunction *F);
/// Add a global variable to the SILModule.
void addGlobalVariable(VarDecl *global);
/// Emit SIL related to a Clang-imported declaration.
void emitExternalDefinition(Decl *d);
/// Emit the ObjC-compatible entry point for a method.
void emitObjCMethodThunk(FuncDecl *method);
/// Emit the ObjC-compatible getter and setter for a property.
void emitObjCPropertyMethodThunks(AbstractStorageDecl *prop);
/// Emit the ObjC-compatible entry point for a constructor.
void emitObjCConstructorThunk(ConstructorDecl *constructor);
/// Emit the ObjC-compatible entry point for a destructor (i.e., -dealloc).
void emitObjCDestructorThunk(DestructorDecl *destructor);
/// Get or emit the witness table for a protocol conformance.
/// Return null if the conformance does not have a witness table directly
/// associated with itself.
SILWitnessTable *getWitnessTable(ProtocolConformance *conformance);
/// Emit a protocol witness entry point.
SILFunction *emitProtocolWitness(ProtocolConformance *conformance,
SILLinkage linkage,
SILDeclRef requirement,
SILDeclRef witness,
IsFreeFunctionWitness_t isFree,
ArrayRef<Substitution> witnessSubs);
/// Emit the lazy initializer function for a global pattern binding
/// declaration.
SILFunction *emitLazyGlobalInitializer(StringRef funcName,
PatternBindingDecl *binding);
/// Emit the accessor for a global variable or stored static property.
///
/// This ensures the lazy initializer has been run before returning the
/// address of the variable.
void emitGlobalAccessor(VarDecl *global,
FuncDecl *builtinOnceDecl,
SILGlobalVariable *onceToken,
SILFunction *onceFunc);
/// True if the given function requires an entry point for ObjC method
/// dispatch.
bool requiresObjCMethodEntryPoint(FuncDecl *method);
/// True if the given constructor requires an entry point for ObjC method
/// dispatch.
bool requiresObjCMethodEntryPoint(ConstructorDecl *constructor);
/// True if calling the given method or property should use ObjC dispatch.
bool requiresObjCDispatch(ValueDecl *vd);
/// True if super-calling the given method from a subclass should use ObjC
/// dispatch.
bool requiresObjCSuperDispatch(ValueDecl *vd);
/// Emit a global initialization.
void emitGlobalInitialization(PatternBindingDecl *initializer);
/// Known functions for bridging.
SILDeclRef getStringToNSStringFn();
SILDeclRef getNSStringToStringFn();
SILDeclRef getAnyObjectArrayToNSArrayFn();
SILDeclRef getNSArrayToAnyObjectArrayFn();
SILDeclRef getBoolToObjCBoolFn();
SILDeclRef getObjCBoolToBoolFn();
SILDeclRef getStringDefaultInitFn();
SILDeclRef getAnyObjectArrayDefaultInitFn();
SILDeclRef getObjCMutablePointerToUnsafePointerFn();
SILDeclRef getCMutablePointerToUnsafePointerFn();
SILDeclRef getCConstPointerToUnsafePointerFn();
SILDeclRef getCMutableVoidPointerToCOpaquePointerFn();
SILDeclRef getCConstVoidPointerToCOpaquePointerFn();
SILDeclRef getUnsafePointerToObjCMutablePointerFn();
SILDeclRef getUnsafePointerToCMutablePointerFn();
SILDeclRef getUnsafePointerToCConstPointerFn();
SILDeclRef getCOpaquePointerToCMutableVoidPointerFn();
SILDeclRef getCOpaquePointerToCConstVoidPointerFn();
/// Report a diagnostic.
template<typename...T, typename...U>
void diagnose(SourceLoc loc, Diag<T...> diag,
U &&...args) {
M.getASTContext().Diags.diagnose(loc, diag, std::forward<U>(args)...);
}
template<typename...T, typename...U>
void diagnose(SILLocation loc, Diag<T...> diag,
U &&...args) {
M.getASTContext().Diags.diagnose(loc.getSourceLoc(),
diag, std::forward<U>(args)...);
}
};
} // end namespace Lowering
} // end namespace swift
#endif