Skip to content

Commit cfe9e6a

Browse files
committed
IDE: Use GenericSignatures and interface types (mostly)
There was a ton of complicated logic here to work around two problems: - Same-type constraints were not represented properly in RequirementReprs, requiring us to store them in strong form and parse them out when printing type interfaces. - The TypeBase::getAllGenericArgs() method did not do the right thing for members of protocols and protocol extensions, and so instead of simple calls to Type::subst(), we had an elaborate 'ArchetypeTransformer' abstraction repeated in two places. Rewrite this code to use GenericSignatures and GenericFunctionType instead of old-school GenericParamLists and PolymorphicFunctionType. This changes the code completion and AST printer output slightly. A few of the changes are actually fixes for cases where the old code didn't handle substitutions properly. A few others are subjective, for example a generic parameter list of the form <T : Proto> now prints as <T where T : Proto>. We can add heuristics to make the output whatever we want here; the important thing is that now we're using modern abstractions.
1 parent 0b8beea commit cfe9e6a

39 files changed

+1558
-2086
lines changed

include/swift/AST/PrintOptions.h

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "swift/Basic/STLExtras.h"
1717
#include "swift/AST/AttrKind.h"
1818
#include "swift/AST/Identifier.h"
19+
#include "llvm/ADT/Optional.h"
1920
#include <limits.h>
2021
#include <vector>
2122

@@ -31,32 +32,21 @@ class DeclContext;
3132
class Type;
3233
class ModuleDecl;
3334
enum DeclAttrKind : unsigned;
34-
class PrinterTypeTransformer;
3535
class SynthesizedExtensionAnalyzer;
3636
struct PrintOptions;
3737

3838
/// Necessary information for archetype transformation during printing.
3939
struct TypeTransformContext {
40-
Type getTypeBase();
41-
NominalTypeDecl *getNominal();
42-
PrinterTypeTransformer *getTransformer();
43-
bool isPrintingSynthesizedExtension();
44-
bool isPrintingTypeInterface();
45-
TypeTransformContext(PrinterTypeTransformer *Transformer);
46-
TypeTransformContext(PrinterTypeTransformer *Transformer,
47-
Type T);
48-
TypeTransformContext(PrinterTypeTransformer *Transformer,
49-
NominalTypeDecl *NTD,
50-
SynthesizedExtensionAnalyzer *Analyzer);
51-
Type transform(Type Input);
52-
StringRef transform(StringRef Input);
40+
TypeBase *BaseType;
41+
NominalTypeDecl *Nominal = nullptr;
5342

54-
bool shouldPrintRequirement(ExtensionDecl *ED, StringRef Req);
43+
explicit TypeTransformContext(Type T);
44+
explicit TypeTransformContext(NominalTypeDecl* NTD);
5545

56-
~TypeTransformContext();
57-
private:
58-
struct Implementation;
59-
Implementation &Impl;
46+
Type getTypeBase() const;
47+
NominalTypeDecl *getNominal() const;
48+
49+
bool isPrintingSynthesizedExtension() const;
6050
};
6151

6252
typedef std::pair<ExtensionDecl*, bool> ExtensionAndIsSynthesized;
@@ -196,6 +186,10 @@ struct PrintOptions {
196186
/// ([] and ?), even if there are no sugar type nodes.
197187
bool SynthesizeSugarOnTypes = false;
198188

189+
/// \brief Print a dynamic Self type as its underlying type, rather than
190+
/// the keyword `Self`.
191+
bool StripDynamicSelf = false;
192+
199193
/// \brief If true, the printer will explode a pattern like this:
200194
/// \code
201195
/// var (a, b) = f()
@@ -350,7 +344,7 @@ struct PrintOptions {
350344
ModuleDecl *CurrentModule = nullptr;
351345

352346
/// \brief The information for converting archetypes to specialized types.
353-
std::shared_ptr<TypeTransformContext> TransformContext;
347+
llvm::Optional<TypeTransformContext> TransformContext;
354348

355349
/// \brief If this is not \c nullptr then functions (including accessors and
356350
/// constructors) will be printed with a body that is determined by this
@@ -413,16 +407,15 @@ struct PrintOptions {
413407
return result;
414408
}
415409

416-
static PrintOptions printTypeInterface(Type T, DeclContext *DC);
410+
static PrintOptions printTypeInterface(Type T);
417411

418-
void setArchetypeSelfTransform(Type T, DeclContext *DC);
412+
void setArchetypeSelfTransform(Type T);
419413

420-
void setArchetypeSelfTransformForQuickHelp(Type T, DeclContext *DC);
414+
void setArchetypeSelfTransformForQuickHelp(Type T);
421415

422-
void setArchetypeAndDynamicSelfTransform(Type T, DeclContext *DC);
416+
void setArchetypeAndDynamicSelfTransform(Type T);
423417

424-
void initArchetypeTransformerForSynthesizedExtensions(NominalTypeDecl *D,
425-
SynthesizedExtensionAnalyzer *SynAnalyzer);
418+
void initArchetypeTransformerForSynthesizedExtensions(NominalTypeDecl *D);
426419

427420
void clearArchetypeTransformerForSynthesizedExtensions();
428421

include/swift/AST/Requirement.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class Requirement {
7676
}
7777

7878
void dump() const;
79+
void print(raw_ostream &os, const PrintOptions &opts) const;
7980
};
8081

8182
} // end namespace swift

include/swift/IDE/Utils.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,6 @@ class SemaLocResolver : public SourceEntityWalker {
197197
};
198198
} // namespace ide
199199

200-
class ArchetypeTransformer {
201-
struct Implementation;
202-
Implementation &Impl;
203-
public:
204-
ArchetypeTransformer(DeclContext *DC, Type Ty);
205-
llvm::function_ref<Type(Type)> getTransformerFunc();
206-
~ArchetypeTransformer();
207-
};
208200
} // namespace swift
209201

210202
#endif // SWIFT_IDE_UTILS_H

include/swift/Sema/IDETypeChecking.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ namespace swift {
4646

4747
bool canPossiblyConvertTo(Type T1, Type T2, DeclContext &DC);
4848

49-
Type lookUpTypeInContext(DeclContext *DC, StringRef Name);
50-
5149
void collectDefaultImplementationForProtocolMembers(ProtocolDecl *PD,
5250
llvm::SmallDenseMap<ValueDecl*, ValueDecl*> &DefaultMap);
5351

@@ -57,11 +55,6 @@ namespace swift {
5755
bool typeCheckUnresolvedExpr(DeclContext &DC, Expr* E,
5856
Expr *P, SmallVectorImpl<Type> &PossibleTypes);
5957

60-
/// \brief Given the base type and the trailing identifiers, this function tries
61-
/// to infer the type of BaseType.Name1.Name2.Name3
62-
/// \returns Resolved type on success, nullptr on error.
63-
Type checkMemberType(DeclContext &DC, Type BaseTy, ArrayRef<Identifier> Names);
64-
6558
struct ResolveMemberResult {
6659
ValueDecl *Favored = nullptr;
6760
std::vector<ValueDecl*> OtherViables;

0 commit comments

Comments
 (0)