mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[Property delegates] Fix printing of memberwise initializer default arguments
This commit is contained in:
@@ -42,6 +42,7 @@
|
||||
#include "swift/AST/TypeCheckRequests.h"
|
||||
#include "swift/AST/TypeLoc.h"
|
||||
#include "swift/AST/SwiftNameTranslation.h"
|
||||
#include "swift/Parse/Lexer.h"
|
||||
#include "clang/Lex/MacroInfo.h"
|
||||
#include "llvm/ADT/SmallString.h"
|
||||
#include "llvm/ADT/SmallPtrSet.h"
|
||||
@@ -5522,6 +5523,31 @@ void ParamDecl::setDefaultArgumentInitContext(Initializer *initContext) {
|
||||
DefaultValueAndFlags.getPointer()->InitContext = initContext;
|
||||
}
|
||||
|
||||
Expr *swift::findOriginalPropertyDelegateInitialValue(VarDecl *var,
|
||||
Expr *init) {
|
||||
auto attr = var->getAttachedPropertyDelegate();
|
||||
assert(attr && "No attached property delegate?");
|
||||
|
||||
// Direct initialization implies no original initial value.
|
||||
if (attr->getArg())
|
||||
return nullptr;
|
||||
|
||||
// Look through any expressions wrapping the initializer.
|
||||
init = init->getSemanticsProvidingExpr();
|
||||
auto initCall = dyn_cast<CallExpr>(init);
|
||||
if (!initCall)
|
||||
return nullptr;
|
||||
|
||||
auto initArg = cast<TupleExpr>(initCall->getArg())->getElement(0);
|
||||
initArg = initArg->getSemanticsProvidingExpr();
|
||||
if (auto autoclosure = dyn_cast<AutoClosureExpr>(initArg)) {
|
||||
initArg =
|
||||
autoclosure->getSingleExpressionBody()->getSemanticsProvidingExpr();
|
||||
}
|
||||
|
||||
return initArg;
|
||||
}
|
||||
|
||||
StringRef
|
||||
ParamDecl::getDefaultValueStringRepresentation(
|
||||
SmallVectorImpl<char> &scratch) const {
|
||||
@@ -5552,6 +5578,23 @@ ParamDecl::getDefaultValueStringRepresentation(
|
||||
if (!existing.empty())
|
||||
return existing;
|
||||
auto var = getStoredProperty();
|
||||
|
||||
if (auto original = var->getOriginalDelegatedProperty()) {
|
||||
if (auto attr = original->getAttachedPropertyDelegate()) {
|
||||
if (auto arg = attr->getArg()) {
|
||||
SourceRange fullRange(attr->getTypeLoc().getSourceRange().Start,
|
||||
arg->getEndLoc());
|
||||
auto charRange = Lexer::getCharSourceRangeFromSourceRange(
|
||||
getASTContext().SourceMgr, fullRange);
|
||||
return getASTContext().SourceMgr.extractText(charRange);
|
||||
}
|
||||
|
||||
auto init = findOriginalPropertyDelegateInitialValue(
|
||||
original, original->getParentInitializer());
|
||||
return extractInlinableText(getASTContext().SourceMgr, init, scratch);
|
||||
}
|
||||
}
|
||||
|
||||
return extractInlinableText(getASTContext().SourceMgr,
|
||||
var->getParentInitializer(),
|
||||
scratch);
|
||||
|
||||
Reference in New Issue
Block a user