Skip to content

Downgrade The TypeLoc in VarDecl to a TypeRepr #27624

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 3 commits into from
Oct 11, 2019
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
29 changes: 9 additions & 20 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -369,21 +369,14 @@ class alignas(1 << DeclAlignInBits) Decl {
IsPropertyWrapperBackingProperty : 1
);

SWIFT_INLINE_BITFIELD(ParamDecl, VarDecl, 1+2+1+NumDefaultArgumentKindBits,
SWIFT_INLINE_BITFIELD(ParamDecl, VarDecl, 1+2+NumDefaultArgumentKindBits,
/// Whether we've computed the specifier yet.
SpecifierComputed : 1,

/// The specifier associated with this parameter. This determines
/// the storage semantics of the value e.g. mutability.
Specifier : 2,

/// True if the type is implicitly specified in the source, but this has an
/// apparently valid typeRepr. This is used in accessors, which look like:
/// set (value) {
/// but need to get the typeRepr from the property as a whole so Sema can
/// resolve the type.
IsTypeLocImplicit : 1,

/// Information about a symbolic default argument, like #file.
defaultArgumentKind : NumDefaultArgumentKindBits
);
Expand Down Expand Up @@ -4804,8 +4797,7 @@ class VarDecl : public AbstractStorageDecl {
bool issCaptureList, SourceLoc nameLoc, Identifier name,
DeclContext *dc, StorageIsMutable_t supportsMutation);

/// This is the type specified, including location information.
TypeLoc typeLoc;
TypeRepr *ParentRepr = nullptr;

Type typeInContext;

Expand All @@ -4825,8 +4817,10 @@ class VarDecl : public AbstractStorageDecl {
return hasName() ? getBaseName().getIdentifier().str() : "_";
}

TypeLoc &getTypeLoc() { return typeLoc; }
TypeLoc getTypeLoc() const { return typeLoc; }
/// Retrieve the TypeRepr corresponding to the parsed type of the parent
/// pattern, if it exists.
TypeRepr *getTypeRepr() const { return ParentRepr; }
void setTypeRepr(TypeRepr *repr) { ParentRepr = repr; }

bool hasType() const {
// We have a type if either the type has been computed already or if
Expand Down Expand Up @@ -5201,10 +5195,8 @@ class ParamDecl : public VarDecl {
Identifier argumentName, SourceLoc parameterNameLoc,
Identifier parameterName, DeclContext *dc);

/// Clone constructor, allocates a new ParamDecl identical to the first.
/// Intentionally not defined as a typical copy constructor to avoid
/// accidental copies.
ParamDecl(ParamDecl *PD, bool withTypes);
/// Create a new ParamDecl identical to the first except without the interface type.
static ParamDecl *cloneWithoutType(const ASTContext &Ctx, ParamDecl *PD);

/// Retrieve the argument (API) name for this function parameter.
Identifier getArgumentName() const { return ArgumentName; }
Expand All @@ -5221,10 +5213,7 @@ class ParamDecl : public VarDecl {
SourceLoc getParameterNameLoc() const { return ParameterNameLoc; }

SourceLoc getSpecifierLoc() const { return SpecifierLoc; }

bool isTypeLocImplicit() const { return Bits.ParamDecl.IsTypeLocImplicit; }
void setIsTypeLocImplicit(bool val) { Bits.ParamDecl.IsTypeLocImplicit = val; }


DefaultArgumentKind getDefaultArgumentKind() const {
return static_cast<DefaultArgumentKind>(Bits.ParamDecl.defaultArgumentKind);
}
Expand Down
2 changes: 0 additions & 2 deletions include/swift/AST/ParameterList.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,6 @@ class alignas(ParamDecl *) ParameterList final :
/// The cloned pattern is for an inherited constructor; mark default
/// arguments as inherited, and mark unnamed arguments as named.
Inherited = 0x02,
/// The cloned pattern will strip type information.
WithoutTypes = 0x04,
};

friend OptionSet<CloneFlags> operator|(CloneFlags flag1, CloneFlags flag2) {
Expand Down
14 changes: 9 additions & 5 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2523,8 +2523,10 @@ void PrintAST::visitVarDecl(VarDecl *decl) {
});
if (decl->hasInterfaceType()) {
Printer << ": ";
auto tyLoc = decl->getTypeLoc();
if (!tyLoc.getTypeRepr())
TypeLoc tyLoc;
if (auto *repr = decl->getTypeRepr())
tyLoc = TypeLoc(repr, decl->getInterfaceType());
else
tyLoc = TypeLoc::withoutLoc(decl->getInterfaceType());

Printer.printDeclResultTypePre(decl, tyLoc);
Expand Down Expand Up @@ -2592,13 +2594,15 @@ void PrintAST::printOneParameter(const ParamDecl *param,
Printer << ": ";
};

auto TheTypeLoc = param->getTypeLoc();

printAttributes(param);
printArgName();

if (!TheTypeLoc.getTypeRepr() && param->hasInterfaceType())
TypeLoc TheTypeLoc;
if (auto *repr = param->getTypeRepr()) {
TheTypeLoc = TypeLoc(repr, param->getInterfaceType());
} else {
TheTypeLoc = TypeLoc::withoutLoc(param->getInterfaceType());
}

// If the parameter is variadic, we will print the "..." after it, but we have
// to strip off the added array type.
Expand Down
10 changes: 7 additions & 3 deletions lib/AST/ASTWalker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1158,9 +1158,13 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,

// Don't walk into the type if the decl is implicit, or if the type is
// implicit.
if (!P->isImplicit() && !P->isTypeLocImplicit() &&
doIt(P->getTypeLoc()))
return true;
if (!P->isImplicit()) {
if (auto *repr = P->getTypeRepr()) {
if (doIt(repr)) {
return true;
}
}
}

if (auto *E = P->getDefaultValue()) {
auto res = doIt(E);
Expand Down
60 changes: 25 additions & 35 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2579,7 +2579,7 @@ void ValueDecl::setOverriddenDecls(ArrayRef<ValueDecl *> overridden) {
}

OpaqueReturnTypeRepr *ValueDecl::getOpaqueResultTypeRepr() const {
TypeLoc returnLoc;
TypeRepr *returnRepr = nullptr;
if (auto *VD = dyn_cast<VarDecl>(this)) {
if (auto *P = VD->getParentPattern()) {
while (auto *PP = dyn_cast<ParenPattern>(P))
Expand All @@ -2591,19 +2591,19 @@ OpaqueReturnTypeRepr *ValueDecl::getOpaqueResultTypeRepr() const {
assert(NP->getDecl() == VD);
(void) NP;

returnLoc = TP->getTypeLoc();
returnRepr = TP->getTypeLoc().getTypeRepr();
}
}
} else {
returnLoc = VD->getTypeLoc();
returnRepr = VD->getTypeRepr();
}
} else if (auto *FD = dyn_cast<FuncDecl>(this)) {
returnLoc = FD->getBodyResultTypeLoc();
returnRepr = FD->getBodyResultTypeLoc().getTypeRepr();
} else if (auto *SD = dyn_cast<SubscriptDecl>(this)) {
returnLoc = SD->getElementTypeLoc();
returnRepr = SD->getElementTypeLoc().getTypeRepr();
}

return dyn_cast_or_null<OpaqueReturnTypeRepr>(returnLoc.getTypeRepr());
return dyn_cast_or_null<OpaqueReturnTypeRepr>(returnRepr);
}

OpaqueTypeDecl *ValueDecl::getOpaqueResultTypeDecl() const {
Expand Down Expand Up @@ -5175,7 +5175,7 @@ SourceRange VarDecl::getSourceRange() const {
SourceRange VarDecl::getTypeSourceRangeForDiagnostics() const {
// For a parameter, map back to its parameter to get the TypeLoc.
if (auto *PD = dyn_cast<ParamDecl>(this)) {
if (auto typeRepr = PD->getTypeLoc().getTypeRepr())
if (auto typeRepr = PD->getTypeRepr())
return typeRepr->getSourceRange();
}

Expand Down Expand Up @@ -5726,36 +5726,26 @@ ParamDecl::ParamDecl(SourceLoc specifierLoc,
ArgumentName(argumentName), ParameterNameLoc(parameterNameLoc),
ArgumentNameLoc(argumentNameLoc), SpecifierLoc(specifierLoc) {
Bits.ParamDecl.SpecifierComputed = false;
Bits.ParamDecl.IsTypeLocImplicit = false;
Bits.ParamDecl.defaultArgumentKind =
static_cast<unsigned>(DefaultArgumentKind::None);
}

/// Clone constructor, allocates a new ParamDecl identical to the first.
/// Intentionally not defined as a copy constructor to avoid accidental copies.
ParamDecl::ParamDecl(ParamDecl *PD, bool withTypes)
: VarDecl(DeclKind::Param, /*IsStatic*/false, PD->getIntroducer(),
/*IsCaptureList*/false, PD->getNameLoc(), PD->getName(),
PD->getDeclContext(),
StorageIsMutable_t(!isImmutableSpecifier(PD->getSpecifier()))),
ArgumentName(PD->getArgumentName()),
ArgumentNameLoc(PD->getArgumentNameLoc()),
SpecifierLoc(PD->getSpecifierLoc()),
DefaultValueAndFlags(nullptr, PD->DefaultValueAndFlags.getInt()) {
Bits.ParamDecl.IsTypeLocImplicit = PD->Bits.ParamDecl.IsTypeLocImplicit;
Bits.ParamDecl.defaultArgumentKind = PD->Bits.ParamDecl.defaultArgumentKind;
typeLoc = PD->getTypeLoc().clone(PD->getASTContext());
if (!withTypes && typeLoc.getTypeRepr())
typeLoc.setType(Type());
ParamDecl *ParamDecl::cloneWithoutType(const ASTContext &Ctx, ParamDecl *PD) {
auto *Clone = new (Ctx) ParamDecl(
PD->getSpecifierLoc(), PD->getArgumentNameLoc(), PD->getArgumentName(),
PD->getArgumentNameLoc(), PD->getParameterName(), PD->getDeclContext());
Clone->DefaultValueAndFlags.setPointerAndInt(
nullptr, PD->DefaultValueAndFlags.getInt());
Clone->Bits.ParamDecl.defaultArgumentKind =
PD->Bits.ParamDecl.defaultArgumentKind;
if (auto *repr = PD->getTypeRepr())
Clone->setTypeRepr(repr->clone(Ctx));

if (withTypes && PD->hasInterfaceType())
setInterfaceType(PD->getInterfaceType());

setSpecifier(PD->getSpecifier());
setImplicitlyUnwrappedOptional(PD->isImplicitlyUnwrappedOptional());
Clone->setSpecifier(PD->getSpecifier());
Clone->setImplicitlyUnwrappedOptional(PD->isImplicitlyUnwrappedOptional());
return Clone;
}


/// Retrieve the type of 'self' for the given context.
Type DeclContext::getSelfTypeInContext() const {
assert(isTypeContext());
Expand Down Expand Up @@ -5794,9 +5784,9 @@ SourceRange ParamDecl::getSourceRange() const {
startLoc = APINameLoc;
else if (nameLoc.isValid())
startLoc = nameLoc;
else {
startLoc = getTypeLoc().getSourceRange().Start;
}
else if (auto *repr = getTypeRepr())
startLoc = repr->getStartLoc();

if (startLoc.isInvalid())
return SourceRange();

Expand All @@ -5810,9 +5800,9 @@ SourceRange ParamDecl::getSourceRange() const {
}

// If the typeloc has a valid location, use it to end the range.
if (auto typeRepr = getTypeLoc().getTypeRepr()) {
if (auto typeRepr = getTypeRepr()) {
auto endLoc = typeRepr->getEndLoc();
if (endLoc.isValid() && !isTypeLocImplicit())
if (endLoc.isValid())
return SourceRange(startLoc, endLoc);
}

Expand Down
5 changes: 2 additions & 3 deletions lib/AST/GenericSignatureBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5116,9 +5116,8 @@ void GenericSignatureBuilder::inferRequirements(
ModuleDecl &module,
ParameterList *params) {
for (auto P : *params) {
inferRequirements(module, P->getTypeLoc().getType(),
FloatingRequirementSource::forInferred(
P->getTypeLoc().getTypeRepr()));
inferRequirements(module, P->getInterfaceType(),
FloatingRequirementSource::forInferred(P->getTypeRepr()));
}
}

Expand Down
3 changes: 1 addition & 2 deletions lib/AST/Parameter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,11 @@ ParameterList *ParameterList::clone(const ASTContext &C,
SmallVector<ParamDecl*, 8> params(begin(), end());

// Remap the ParamDecls inside of the ParameterList.
bool withTypes = !options.contains(ParameterList::WithoutTypes);
for (auto &decl : params) {
bool hadDefaultArgument =
decl->getDefaultArgumentKind() == DefaultArgumentKind::Normal;

decl = new (C) ParamDecl(decl, withTypes);
decl = ParamDecl::cloneWithoutType(C, decl);
if (options & Implicit)
decl->setImplicit();

Expand Down
6 changes: 4 additions & 2 deletions lib/IDE/SwiftSourceDocInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,10 @@ static std::vector<CharSourceRange> getEnumParamListInfo(SourceManager &SM,
for (ParamDecl *Param: *PL) {
if (Param->isImplicit())
continue;

SourceLoc LabelStart(Param->getTypeLoc().getLoc());

SourceLoc LabelStart;
if (auto *repr = Param->getTypeRepr())
LabelStart = repr->getLoc();
SourceLoc LabelEnd(LabelStart);

if (Param->getNameLoc().isValid()) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Migrator/APIDiffMigratorPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class ChildIndexFinder : public TypeReprVisitor<ChildIndexFinder, FoundResult> {

for (auto *Param: *Parent->getParameters()) {
if (!--NextIndex) {
return findChild(Param->getTypeLoc());
return findChild(Param->getTypeRepr());
}
}
llvm_unreachable("child index out of bounds");
Expand Down
3 changes: 0 additions & 3 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4310,9 +4310,6 @@ static ParamDecl *createSetterAccessorArgument(SourceLoc nameLoc,
if (isNameImplicit)
result->setImplicit();

// AST Walker shouldn't go into the type recursively.
result->setIsTypeLocImplicit(true);

return result;
}

Expand Down
6 changes: 2 additions & 4 deletions lib/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2673,8 +2673,7 @@ parseClosureSignatureIfPresent(SmallVectorImpl<CaptureListEntry> &captureList,
auto isTupleDestructuring = [](ParamDecl *param) -> bool {
if (!param->isInvalid())
return false;
auto &typeLoc = param->getTypeLoc();
if (auto typeRepr = typeLoc.getTypeRepr())
if (auto *typeRepr = param->getTypeRepr())
return !param->hasName() && isa<TupleTypeRepr>(typeRepr);
return false;
};
Expand All @@ -2685,7 +2684,6 @@ parseClosureSignatureIfPresent(SmallVectorImpl<CaptureListEntry> &captureList,
continue;

auto argName = "arg" + std::to_string(i);
auto typeLoc = param->getTypeLoc();

SmallString<64> fixIt;
llvm::raw_svector_ostream OS(fixIt);
Expand All @@ -2695,7 +2693,7 @@ parseClosureSignatureIfPresent(SmallVectorImpl<CaptureListEntry> &captureList,
OS << '\n' << indent;

OS << "let ";
printTupleNames(typeLoc.getTypeRepr(), OS);
printTupleNames(param->getTypeRepr(), OS);
OS << " = " << argName << (isMultiLine ? "\n" + indent : "; ");

diagnose(param->getStartLoc(), diag::anon_closure_tuple_param_destructuring)
Expand Down
5 changes: 2 additions & 3 deletions lib/Parse/ParsePattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,6 @@ mapParsedParameters(Parser &parser,
auto setInvalid = [&]{
if (param->isInvalid())
return;
param->getTypeLoc().setInvalidType(ctx);
param->setInvalid();
};

Expand Down Expand Up @@ -512,7 +511,7 @@ mapParsedParameters(Parser &parser,
"__owned",
parsingEnumElt);
}
param->getTypeLoc() = TypeLoc(type);
param->setTypeRepr(type);

// If there is `@autoclosure` attribute associated with the type
// let's mark that in the declaration as well, because it
Expand Down Expand Up @@ -629,7 +628,7 @@ mapParsedParameters(Parser &parser,
.fixItRemove(param.EllipsisLoc);

param.EllipsisLoc = SourceLoc();
} else if (!result->getTypeLoc().getTypeRepr()) {
} else if (!result->getTypeRepr()) {
parser.diagnose(param.EllipsisLoc, diag::untyped_pattern_ellipsis)
.highlight(result->getSourceRange());

Expand Down
10 changes: 6 additions & 4 deletions lib/Sema/CSDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,10 @@ bool NoEscapeFuncToTypeConversionFailure::diagnoseParameterUse() const {
emitDiagnostic(PD->getLoc(), diag::noescape_parameter, PD->getName());

if (!PD->isAutoClosure()) {
note.fixItInsert(PD->getTypeLoc().getSourceRange().Start, "@escaping ");
SourceLoc reprLoc;
if (auto *repr = PD->getTypeRepr())
reprLoc = repr->getStartLoc();
note.fixItInsert(reprLoc, "@escaping ");
} // TODO: add in a fixit for autoclosure

return true;
Expand Down Expand Up @@ -4125,9 +4128,8 @@ bool ClosureParamDestructuringFailure::diagnoseAsError() {
// with parameters, if there are, we'll have to add
// type information to the replacement argument.
bool explicitTypes =
llvm::any_of(params->getArray(), [](const ParamDecl *param) {
return param->getTypeLoc().getTypeRepr();
});
llvm::any_of(params->getArray(),
[](const ParamDecl *param) { return param->getTypeRepr(); });

if (isMultiLineClosure)
OS << '\n' << indent;
Expand Down
4 changes: 2 additions & 2 deletions lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2027,8 +2027,8 @@ namespace {
Type paramType, internalType;

// If a type was explicitly specified, use its opened type.
if (auto type = param->getTypeLoc().getType()) {
paramType = closureExpr->mapTypeIntoContext(type);
if (param->getTypeRepr()) {
paramType = closureExpr->mapTypeIntoContext(param->getInterfaceType());
// FIXME: Need a better locator for a pattern as a base.
paramType = CS.openUnboundGenericType(paramType, locator);
internalType = paramType;
Expand Down
Loading