-
Notifications
You must be signed in to change notification settings - Fork 10.2k
/
PrintOptions.h
443 lines (362 loc) · 14.9 KB
/
PrintOptions.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
//===--- PrintOptions.h - AST printing options ------------------*- 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
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_AST_PRINTOPTIONS_H
#define SWIFT_AST_PRINTOPTIONS_H
#include "swift/Basic/STLExtras.h"
#include "swift/AST/AttrKind.h"
#include "swift/AST/Identifier.h"
#include <vector>
namespace swift {
class GenericParamList;
class CanType;
class Decl;
class ValueDecl;
class ExtensionDecl;
class NominalTypeDecl;
class TypeBase;
class DeclContext;
class Type;
enum DeclAttrKind : unsigned;
class PrinterArchetypeTransformer;
class SynthesizedExtensionAnalyzer;
struct PrintOptions;
/// Necessary information for archetype transformation during printing.
struct ArchetypeTransformContext {
Type getTypeBase();
NominalTypeDecl *getNominal();
PrinterArchetypeTransformer *getTransformer();
bool isPrintingSynthesizedExtension();
bool isPrintingTypeInterface();
ArchetypeTransformContext(PrinterArchetypeTransformer *Transformer);
ArchetypeTransformContext(PrinterArchetypeTransformer *Transformer,
Type T);
ArchetypeTransformContext(PrinterArchetypeTransformer *Transformer,
NominalTypeDecl *NTD,
SynthesizedExtensionAnalyzer *Analyzer);
Type transform(Type Input);
StringRef transform(StringRef Input);
bool shouldPrintRequirement(ExtensionDecl *ED, StringRef Req);
~ArchetypeTransformContext();
private:
struct Implementation;
Implementation &Impl;
};
typedef std::pair<ExtensionDecl*, bool> ExtensionAndIsSynthesized;
typedef llvm::function_ref<void(ArrayRef<ExtensionAndIsSynthesized>)>
ExtensionGroupOperation;
class SynthesizedExtensionAnalyzer {
struct Implementation;
Implementation &Impl;
public:
SynthesizedExtensionAnalyzer(NominalTypeDecl *Target,
PrintOptions Options,
bool IncludeUnconditional = true);
~SynthesizedExtensionAnalyzer();
enum class MergeGroupKind : char {
All,
MergeableWithTypeDef,
UnmergeableWithTypeDef,
};
void forEachExtensionMergeGroup(MergeGroupKind Kind,
ExtensionGroupOperation Fn);
bool isInSynthesizedExtension(const ValueDecl *VD);
bool shouldPrintRequirement(ExtensionDecl *ED, StringRef Req);
bool hasMergeGroup(MergeGroupKind Kind);
};
class BracketOptions {
Decl* Target;
bool OpenExtension;
bool CloseExtension;
bool CloseNominal;
public:
BracketOptions(Decl *Target = nullptr, bool OpenExtension = true,
bool CloseExtension = true, bool CloseNominal = true) :
Target(Target), OpenExtension(OpenExtension),
CloseExtension(CloseExtension),
CloseNominal(CloseNominal) {}
bool shouldOpenExtension(const Decl *D) {
return D != Target || OpenExtension;
}
bool shouldCloseExtension(const Decl *D) {
return D != Target || CloseExtension;
}
bool shouldCloseNominal(const Decl *D) {
return D != Target || CloseNominal;
}
};
/// Options for printing AST nodes.
///
/// A default-constructed PrintOptions is suitable for printing to users;
/// there are also factory methods for specific use cases.
struct PrintOptions {
/// \brief The indentation width.
unsigned Indent = 2;
/// \brief Whether to print function definitions.
bool FunctionDefinitions = false;
/// \brief Whether to print '{ get set }' on readwrite computed properties.
bool PrintGetSetOnRWProperties = true;
/// \brief Whether to print *any* accessors on properties.
bool PrintPropertyAccessors = true;
/// \brief Whether to print the accessors of a property abstractly,
/// i.e. always as get and set rather than the specific accessors
/// actually used to implement the property.
///
/// Printing function definitions takes priority over this setting.
bool AbstractAccessors = true;
/// \brief Whether to print type definitions.
bool TypeDefinitions = false;
/// \brief Whether to print variable initializers.
bool VarInitializers = false;
/// \brief Whether to print a placeholder for default parameters.
bool PrintDefaultParameterPlaceholder = true;
/// \brief Whether to print enum raw value expressions.
bool EnumRawValues = false;
/// \brief Whether to prefer printing TypeReprs instead of Types,
/// if a TypeRepr is available. This allows us to print the original
/// spelling of the type name.
///
/// \note This should be \c true when printing AST with the intention show
/// it to the user.
bool PreferTypeRepr = true;
/// \brief Whether to print fully qualified Types.
bool FullyQualifiedTypes = false;
/// \brief Print fully qualified types if our heuristics say that a certain
/// type might be ambiguous.
bool FullyQualifiedTypesIfAmbiguous = false;
/// \brief Print Swift.Array and Swift.Optional with sugared syntax
/// ([] and ?), even if there are no sugar type nodes.
bool SynthesizeSugarOnTypes = false;
/// \brief If true, the printer will explode a pattern like this:
/// \code
/// var (a, b) = f()
/// \endcode
/// into multiple variable declarations.
///
/// For this option to work correctly, \c VarInitializers should be
/// \c false.
bool ExplodePatternBindingDecls = false;
/// If true, the printer will explode an enum case like this:
/// \code
/// case A, B
/// \endcode
/// into multiple case declarations.
bool ExplodeEnumCaseDecls = false;
/// \brief Whether to print implicit parts of the AST.
bool SkipImplicit = false;
/// \brief Whether to print unavailable parts of the AST.
bool SkipUnavailable = false;
/// Whether to skip internal stdlib declarations.
bool SkipPrivateStdlibDecls = false;
/// Whether to skip underscored stdlib protocols.
/// Protocols marked with @_show_in_interface are still printed.
bool SkipUnderscoredStdlibProtocols = false;
/// Whether to skip extensions that don't add protocols or no members.
bool SkipEmptyExtensionDecls = true;
/// Whether to print attributes.
bool SkipAttributes = false;
/// Whether to print keywords like 'func'.
bool SkipIntroducerKeywords = false;
/// Whether to print destructors.
bool SkipDeinit = false;
/// Whether to skip printing 'import' declarations.
bool SkipImports = false;
/// \brief Whether to skip printing overrides and witnesses for
/// protocol requirements.
bool SkipOverrides = false;
/// Whether to print a long attribute like '\@available' on a separate line
/// from the declaration or other attributes.
bool PrintLongAttrsOnSeparateLines = false;
bool PrintImplicitAttrs = true;
/// Whether to print decl attributes that are only used internally,
/// such as _silgen_name, transparent, etc.
bool PrintUserInaccessibleAttrs = true;
/// List of attribute kinds that should not be printed.
std::vector<DeclAttrKind> ExcludeAttrList =
{ DAK_Transparent, DAK_Effects, DAK_FixedLayout };
/// List of attribute kinds that should be printed exclusively.
/// Empty means allow all.
std::vector<DeclAttrKind> ExclusiveAttrList;
/// Whether to print function @convention attribute on function types.
bool PrintFunctionRepresentationAttrs = true;
/// Whether to print storage representation attributes on types, e.g.
/// '@sil_weak', '@sil_unmanaged'.
bool PrintStorageRepresentationAttrs = false;
/// Whether to print 'override' keyword on overridden decls.
bool PrintOverrideKeyword = true;
/// Whether to print accessibility information on all value decls.
bool PrintAccessibility = false;
/// If \c PrintAccessibility is true, this determines whether to print
/// 'internal' keyword.
bool PrintInternalAccessibilityKeyword = true;
/// Print all decls that have at least this level of access.
Accessibility AccessibilityFilter = Accessibility::Private;
/// Print IfConfigDecls and IfConfigStmts.
bool PrintIfConfig = true;
/// Whether we are printing for sil.
bool PrintForSIL = false;
/// Whether we are printing part of SIL body.
bool PrintInSILBody = false;
/// Whether to use an empty line to separate two members in a single decl.
bool EmptyLineBetweenMembers = false;
enum class ArgAndParamPrintingMode {
ArgumentOnly,
MatchSource,
BothAlways,
};
/// Whether to print the doc-comment from the conformance if a member decl
/// has no associated doc-comment by itself.
bool ElevateDocCommentFromConformance = false;
/// Whether to print the content of an extension decl inside the type decl where it
/// extends from.
std::function<bool(const ExtensionDecl *)> printExtensionContentAsMembers =
[] (const ExtensionDecl *) { return false; };
/// How to print the keyword argument and parameter name in functions.
ArgAndParamPrintingMode ArgAndParamPrinting =
ArgAndParamPrintingMode::MatchSource;
/// \brief Whether to print documentation comments attached to declarations.
/// Note that this may print documentation comments from related declarations
/// (e.g. the overridden method in the superclass) if such comment is found.
bool PrintDocumentationComments = false;
/// \brief Whether to print regular comments from clang module headers.
bool PrintRegularClangComments = false;
/// When true, printing interface from a source file will print the original
/// source text for applicable declarations, in order to preserve the
/// formatting.
bool PrintOriginalSourceText = false;
/// \brief Print dependent types as references into this generic parameter
/// list.
GenericParamList *ContextGenericParams = nullptr;
/// \brief Print types with alternative names from their canonical names.
llvm::DenseMap<CanType, Identifier> *AlternativeTypeNames = nullptr;
/// \brief The information for converting archetypes to specialized types.
std::shared_ptr<ArchetypeTransformContext> TransformContext;
BracketOptions BracketOptions;
/// Retrieve the set of options for verbose printing to users.
static PrintOptions printVerbose() {
PrintOptions result;
result.TypeDefinitions = true;
result.VarInitializers = true;
result.PrintDefaultParameterPlaceholder = true;
result.PrintDocumentationComments = true;
result.PrintRegularClangComments = true;
result.PrintLongAttrsOnSeparateLines = true;
return result;
}
/// Retrieve the set of options suitable for diagnostics printing.
static PrintOptions printForDiagnostics() {
PrintOptions result = printVerbose();
result.PrintAccessibility = true;
result.Indent = 4;
result.FullyQualifiedTypesIfAmbiguous = true;
result.SynthesizeSugarOnTypes = true;
result.PrintUserInaccessibleAttrs = false;
result.PrintImplicitAttrs = false;
result.ExcludeAttrList.push_back(DAK_Exported);
result.ExcludeAttrList.push_back(DAK_Inline);
result.ExcludeAttrList.push_back(DAK_Rethrows);
result.ExcludeAttrList.push_back(DAK_Swift3Migration);
result.PrintOverrideKeyword = false;
result.AccessibilityFilter = Accessibility::Public;
result.PrintIfConfig = false;
return result;
}
/// Retrieve the set of options suitable for interface generation.
static PrintOptions printInterface() {
PrintOptions result = printForDiagnostics();
result.SkipUnavailable = true;
result.SkipImplicit = true;
result.SkipPrivateStdlibDecls = true;
result.SkipUnderscoredStdlibProtocols = true;
result.SkipDeinit = true;
result.ExcludeAttrList.push_back(DAK_WarnUnusedResult);
result.ExcludeAttrList.push_back(DAK_DiscardableResult);
result.EmptyLineBetweenMembers = true;
result.ElevateDocCommentFromConformance = true;
return result;
}
static PrintOptions printTypeInterface(Type T, const DeclContext *DC);
void setArchetypeTransform(Type T, const DeclContext *DC);
void setArchetypeTransformForQuickHelp(Type T, DeclContext *DC);
void initArchetypeTransformerForSynthesizedExtensions(NominalTypeDecl *D,
SynthesizedExtensionAnalyzer *SynAnalyzer);
void clearArchetypeTransformerForSynthesizedExtensions();
/// Retrieve the print options that are suitable to print the testable interface.
static PrintOptions printTestableInterface() {
PrintOptions result = printInterface();
result.AccessibilityFilter = Accessibility::Internal;
return result;
}
/// Retrieve the print options that are suitable to print interface for a
/// swift file.
static PrintOptions printSwiftFileInterface() {
PrintOptions result = printInterface();
result.AccessibilityFilter = Accessibility::Internal;
result.EmptyLineBetweenMembers = true;
return result;
}
/// Retrieve the set of options suitable for interface generation for
/// documentation purposes.
static PrintOptions printDocInterface() {
PrintOptions result = PrintOptions::printInterface();
result.PrintAccessibility = false;
result.SkipUnavailable = false;
result.ExcludeAttrList.push_back(DAK_Available);
result.ExcludeAttrList.push_back(DAK_Swift3Migration);
result.ArgAndParamPrinting =
PrintOptions::ArgAndParamPrintingMode::BothAlways;
result.PrintDocumentationComments = false;
result.PrintRegularClangComments = false;
result.PrintAccessibility = false;
result.PrintFunctionRepresentationAttrs = false;
return result;
}
/// Retrieve the set of options suitable for printing SIL functions.
static PrintOptions printSIL() {
PrintOptions result;
result.PrintLongAttrsOnSeparateLines = true;
result.PrintStorageRepresentationAttrs = true;
result.AbstractAccessors = false;
result.PrintForSIL = true;
result.PrintInSILBody = true;
return result;
}
/// \brief Retrieve the set of options that prints everything.
///
/// This is only intended for debug output.
static PrintOptions printEverything() {
PrintOptions result = printVerbose();
result.ExcludeAttrList.clear();
result.ExcludeAttrList.push_back(DAK_FixedLayout);
result.PrintStorageRepresentationAttrs = true;
result.AbstractAccessors = false;
result.PrintAccessibility = true;
result.SkipEmptyExtensionDecls = false;
return result;
}
/// Print in the style of quick help declaration.
static PrintOptions printQuickHelpDeclaration() {
PrintOptions PO;
PO.EnumRawValues = true;
PO.PrintDefaultParameterPlaceholder = true;
PO.PrintImplicitAttrs = false;
PO.PrintFunctionRepresentationAttrs = false;
PO.PrintDocumentationComments = false;
PO.ExcludeAttrList.push_back(DAK_Available);
PO.ExcludeAttrList.push_back(DAK_Swift3Migration);
PO.ExcludeAttrList.push_back(DAK_WarnUnusedResult);
PO.SkipPrivateStdlibDecls = true;
PO.ExplodeEnumCaseDecls = true;
return PO;
}
};
}
#endif // LLVM_SWIFT_AST_PRINTOPTIONS_H