Skip to content

More progress on uncurry level removal #12785

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions include/swift/SIL/SILDeclRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ bool requiresForeignEntryPoint(ValueDecl *vd);
/// True if the entry point is natively foreign.
bool requiresForeignToNativeThunk(ValueDecl *vd);

unsigned getNaturalUncurryLevel(ValueDecl *vd);

enum ForDefinition_t : bool {
NotForDefinition = false,
ForDefinition = true
Expand Down Expand Up @@ -304,7 +302,7 @@ struct SILDeclRef {
void print(llvm::raw_ostream &os) const;
void dump() const;

unsigned getUncurryLevel() const;
unsigned getParameterListCount() const;

ResilienceExpansion getResilienceExpansion() const {
return ResilienceExpansion(Expansion);
Expand Down
4 changes: 2 additions & 2 deletions lib/ParseSIL/ParseSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1236,7 +1236,7 @@ bool SILParser::parseSILDeclRef(SILDeclRef &Result,
if (!P.consumeIf(tok::sil_exclamation)) {
// Construct SILDeclRef.
Result = SILDeclRef(VD, Kind, expansion, /*isCurried=*/false, IsObjC);
if (uncurryLevel < Result.getUncurryLevel())
if (uncurryLevel < Result.getParameterListCount() - 1)
Result = Result.asCurried();
return false;
}
Expand Down Expand Up @@ -1327,7 +1327,7 @@ bool SILParser::parseSILDeclRef(SILDeclRef &Result,

// Construct SILDeclRef.
Result = SILDeclRef(VD, Kind, expansion, /*isCurried=*/false, IsObjC);
if (uncurryLevel < Result.getUncurryLevel())
if (uncurryLevel < Result.getParameterListCount() - 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using a constant - 1 here, can you turn this into a method on Result with a descriptive name/comment.

Result = Result.asCurried();
return false;
}
Expand Down
50 changes: 19 additions & 31 deletions lib/SIL/SILDeclRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,29 +227,6 @@ static bool hasLoweredLocalCaptures(AnyFunctionRef AFR,
return false;
}

static unsigned getFuncNaturalUncurryLevel(AnyFunctionRef AFR) {
assert(AFR.getParameterLists().size() >= 1 && "no arguments for func?!");
return AFR.getParameterLists().size() - 1;
}

unsigned swift::getNaturalUncurryLevel(ValueDecl *vd) {
if (auto *func = dyn_cast<FuncDecl>(vd)) {
return getFuncNaturalUncurryLevel(func);
} else if (isa<ConstructorDecl>(vd)) {
return 1;
} else if (auto *ed = dyn_cast<EnumElementDecl>(vd)) {
return ed->hasAssociatedValues() ? 1 : 0;
} else if (isa<DestructorDecl>(vd)) {
return 0;
} else if (isa<ClassDecl>(vd)) {
return 1;
} else if (isa<VarDecl>(vd)) {
return 0;
} else {
llvm_unreachable("Unhandled ValueDecl for SILDeclRef");
}
}

SILDeclRef::SILDeclRef(ValueDecl *vd, SILDeclRef::Kind kind,
ResilienceExpansion expansion,
bool isCurried, bool isForeign)
Expand Down Expand Up @@ -888,12 +865,23 @@ SubclassScope SILDeclRef::getSubclassScope() const {
llvm_unreachable("Unhandled access level in switch.");
}

unsigned SILDeclRef::getUncurryLevel() const {
if (isCurried)
return 0;
if (!hasDecl())
return getFuncNaturalUncurryLevel(*getAnyFunctionRef());
if (kind == Kind::DefaultArgGenerator)
return 0;
return getNaturalUncurryLevel(getDecl());
unsigned SILDeclRef::getParameterListCount() const {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add more documentation here and use early-exits instead of else-if return?

What I want is for each case to say /why/ it has X amounts of parameters. For instance, why does ClassDecl have 2 parameters? It would also be nice to have a bigger comment explaining this methods use/importance in the larger world of SIL.

if (isCurried || !hasDecl() || kind == Kind::DefaultArgGenerator)
return 1;

auto *vd = getDecl();

if (auto *func = dyn_cast<AbstractFunctionDecl>(vd)) {
return func->getParameterLists().size();
} else if (auto *ed = dyn_cast<EnumElementDecl>(vd)) {
return ed->hasAssociatedValues() ? 2 : 1;
} else if (isa<DestructorDecl>(vd)) {
return 1;
} else if (isa<ClassDecl>(vd)) {
return 2;
} else if (isa<VarDecl>(vd)) {
return 1;
} else {
llvm_unreachable("Unhandled ValueDecl for SILDeclRef");
}
}
4 changes: 2 additions & 2 deletions lib/SIL/SILFunctionType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1777,7 +1777,7 @@ static bool isImporterGeneratedAccessor(const clang::Decl *clangDecl,
return false;

// Must be a type member.
if (constant.getUncurryLevel() != 1)
if (constant.getParameterListCount() != 2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment here why you are checking against 2? Couldn't this be a method on SILDeclRef with a nice comment that explains what is happening here?

return false;

// Must be imported from a function.
Expand Down Expand Up @@ -2486,7 +2486,7 @@ getAbstractionPatternForConstant(ASTContext &ctx, SILDeclRef constant,
TypeConverter::LoweredFormalTypes
TypeConverter::getLoweredFormalTypes(SILDeclRef constant,
CanAnyFunctionType fnType) {
unsigned uncurryLevel = constant.getUncurryLevel();
unsigned uncurryLevel = constant.getParameterListCount() - 1;
auto extInfo = fnType->getExtInfo();

// Form an abstraction pattern for bridging purposes.
Expand Down
2 changes: 1 addition & 1 deletion lib/SIL/SILPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ void SILDeclRef::print(raw_ostream &OS) const {
break;
}

auto uncurryLevel = getUncurryLevel();
auto uncurryLevel = getParameterListCount() - 1;
if (uncurryLevel != 0)
OS << (isDot ? '.' : '!') << uncurryLevel;

Expand Down
Loading