Skip to content

Commit 4970157

Browse files
author
Manman Ren
committed
[SILParser] parsing extensions.
Fix a bug introduced in r20818, where we should print get for computed property. Make sure we can round-trip extension of a generic class and extension of an inner class. rdar://17927072 Swift SVN r21151
1 parent ebf7db3 commit 4970157

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

lib/AST/ASTPrinter.cpp

+20-3
Original file line numberDiff line numberDiff line change
@@ -582,12 +582,16 @@ bool PrintAST::shouldPrint(const Decl *D) {
582582

583583
void PrintAST::printAccessors(AbstractStorageDecl *ASD) {
584584
// If we are printing StoredWithTrivialAccessors in sil, print get|set
585-
// to differentiate from stored property.
585+
// to differentiate from stored property. FIXME: Parser can't handle
586+
// "let {get}", so do not print it.
586587
if (!ASD->hasAccessorFunctions() ||
587-
(Options.PrintForSIL && !ASD->getSetter()) ||
588+
(Options.PrintForSIL &&
589+
ASD->getStorageKind() == AbstractStorageDecl::StoredWithTrivialAccessors
590+
&& isa<VarDecl>(ASD) && cast<VarDecl>(ASD)->isLet() &&
591+
!ASD->getSetter()) ||
588592
(!Options.PrintForSIL &&
589593
ASD->getStorageKind()
590-
== AbstractStorageDecl::StoredWithTrivialAccessors)){
594+
== AbstractStorageDecl::StoredWithTrivialAccessors)) {
591595
// This is a 'let' vardecl. We could print the initializer if we could
592596
// print expressions.
593597
return;
@@ -860,6 +864,19 @@ void PrintAST::visitExtensionDecl(ExtensionDecl *decl) {
860864
// We cannot extend sugared types.
861865
auto *nominal = decl->getExtendedType()->getAnyNominal();
862866
assert(nominal && "extension of non-nominal type");
867+
868+
if (auto ct = decl->getExtendedType()->getAs<ClassType>()) {
869+
if (auto ParentType = ct->getParent()) {
870+
ParentType.print(Printer, Options);
871+
Printer << ".";
872+
}
873+
}
874+
if (auto st = decl->getExtendedType()->getAs<StructType>()) {
875+
if (auto ParentType = st->getParent()) {
876+
ParentType.print(Printer, Options);
877+
Printer << ".";
878+
}
879+
}
863880
Printer.printTypeRef(nominal, nominal->getName());
864881
});
865882
printInherited(decl);

lib/SIL/SILPrinter.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1508,7 +1508,8 @@ void SILModule::print(llvm::raw_ostream &OS, bool Verbose,
15081508
SmallVector<Decl *, 32> topLevelDecls;
15091509
M->getTopLevelDecls(topLevelDecls);
15101510
for (const Decl *D : topLevelDecls) {
1511-
if ((isa<ValueDecl>(D) || isa<OperatorDecl>(D)) &&
1511+
if ((isa<ValueDecl>(D) || isa<OperatorDecl>(D) ||
1512+
isa<ExtensionDecl>(D)) &&
15121513
!emittedFunctions.count(D) &&
15131514
!D->isImplicit()) {
15141515
D->print(OS, Options);

test/SIL/Parser/extension.swift

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: %swift %s -emit-silgen | sil-opt -verify
2+
3+
public struct X {
4+
}
5+
6+
extension X {
7+
public struct Y {}
8+
}
9+
10+
extension X.Y {
11+
var startIndex: Int {
12+
return 0
13+
}
14+
}
15+
16+
class X2<T> {
17+
}
18+
19+
extension X2 {
20+
var startIndex: Int {
21+
return 0
22+
}
23+
}
24+
25+
public func ==(lhs: X.Y, rhs: X.Y) -> Bool { return true }

0 commit comments

Comments
 (0)