Skip to content

Strip Out More TypeLocs #31390

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 5 commits into from
Apr 29, 2020
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
50 changes: 18 additions & 32 deletions include/swift/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -3386,7 +3386,7 @@ class BridgeToObjCExpr : public ImplicitConversionExpr {
/// UnresolvedSpecializeExpr - Represents an explicit specialization using
/// a type parameter list (e.g. "Vector<Int>") that has not been resolved.
class UnresolvedSpecializeExpr final : public Expr,
private llvm::TrailingObjects<UnresolvedSpecializeExpr, TypeLoc> {
private llvm::TrailingObjects<UnresolvedSpecializeExpr, TypeRepr *> {
friend TrailingObjects;

Expr *SubExpr;
Expand All @@ -3395,31 +3395,27 @@ class UnresolvedSpecializeExpr final : public Expr,

UnresolvedSpecializeExpr(Expr *SubExpr,
SourceLoc LAngleLoc,
ArrayRef<TypeLoc> UnresolvedParams,
ArrayRef<TypeRepr *> UnresolvedParams,
SourceLoc RAngleLoc)
: Expr(ExprKind::UnresolvedSpecialize, /*Implicit=*/false),
SubExpr(SubExpr), LAngleLoc(LAngleLoc), RAngleLoc(RAngleLoc) {
Bits.UnresolvedSpecializeExpr.NumUnresolvedParams = UnresolvedParams.size();
std::uninitialized_copy(UnresolvedParams.begin(), UnresolvedParams.end(),
getTrailingObjects<TypeLoc>());
getTrailingObjects<TypeRepr *>());
}

public:
static UnresolvedSpecializeExpr *
create(ASTContext &ctx, Expr *SubExpr, SourceLoc LAngleLoc,
ArrayRef<TypeLoc> UnresolvedParams, SourceLoc RAngleLoc);
ArrayRef<TypeRepr *> UnresolvedParams, SourceLoc RAngleLoc);

Expr *getSubExpr() const { return SubExpr; }
void setSubExpr(Expr *e) { SubExpr = e; }

/// Retrieve the list of type parameters. These parameters have not yet
/// been bound to archetypes of the entity to be specialized.
ArrayRef<TypeLoc> getUnresolvedParams() const {
return {getTrailingObjects<TypeLoc>(),
Bits.UnresolvedSpecializeExpr.NumUnresolvedParams};
}
MutableArrayRef<TypeLoc> getUnresolvedParams() {
return {getTrailingObjects<TypeLoc>(),
ArrayRef<TypeRepr *> getUnresolvedParams() const {
return {getTrailingObjects<TypeRepr *>(),
Bits.UnresolvedSpecializeExpr.NumUnresolvedParams};
}

Expand Down Expand Up @@ -3706,15 +3702,15 @@ class ClosureExpr : public AbstractClosureExpr {
SourceLoc InLoc;

/// The explicitly-specified result type.
TypeLoc ExplicitResultType;
TypeExpr *ExplicitResultType;
Copy link
Contributor

Choose a reason for hiding this comment

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

The only out of place thing about this is that TypeExpr * type is always a metatype which is not really intended here right? Maybe we should hide the fact that inside closure explicit type is represented via TypeExpr and instead only expose TypeRepr and Type to the outside?


/// The body of the closure, along with a bit indicating whether it
/// was originally just a single expression.
llvm::PointerIntPair<BraceStmt *, 1, bool> Body;
public:
ClosureExpr(SourceRange bracketRange, VarDecl *capturedSelfDecl,
ParameterList *params, SourceLoc throwsLoc, SourceLoc arrowLoc,
SourceLoc inLoc, TypeLoc explicitResultType,
SourceLoc inLoc, TypeExpr *explicitResultType,
unsigned discriminator, DeclContext *parent)
: AbstractClosureExpr(ExprKind::Closure, Type(), /*Implicit=*/false,
discriminator, parent),
Expand Down Expand Up @@ -3773,20 +3769,15 @@ class ClosureExpr : public AbstractClosureExpr {
return ThrowsLoc;
}

/// Retrieve the explicit result type location information.
TypeLoc &getExplicitResultTypeLoc() {
Type getExplicitResultType() const {
assert(hasExplicitResultType() && "No explicit result type");
return ExplicitResultType;
return ExplicitResultType->getInstanceType();
}
void setExplicitResultType(Type ty);

TypeRepr *getExplicitResultTypeRepr() const {
assert(hasExplicitResultType() && "No explicit result type");
return ExplicitResultType.getTypeRepr();
}

void setExplicitResultType(SourceLoc arrowLoc, TypeLoc resultType) {
ArrowLoc = arrowLoc;
ExplicitResultType = resultType;
return ExplicitResultType->getTypeRepr();
}

/// Determine whether the closure has a single expression for its
Expand Down Expand Up @@ -4939,25 +4930,20 @@ class UnresolvedPatternExpr : public Expr {
class EditorPlaceholderExpr : public Expr {
Identifier Placeholder;
SourceLoc Loc;
TypeLoc PlaceholderTy;
TypeRepr *PlaceholderTy;
TypeRepr *ExpansionTyR;
Expr *SemanticExpr;

public:
EditorPlaceholderExpr(Identifier Placeholder, SourceLoc Loc,
TypeLoc PlaceholderTy,
TypeRepr *ExpansionTyR)
: Expr(ExprKind::EditorPlaceholder, /*Implicit=*/false),
Placeholder(Placeholder), Loc(Loc),
PlaceholderTy(PlaceholderTy),
ExpansionTyR(ExpansionTyR),
SemanticExpr(nullptr) {
}
TypeRepr *PlaceholderTy, TypeRepr *ExpansionTyR)
: Expr(ExprKind::EditorPlaceholder, /*Implicit=*/false),
Placeholder(Placeholder), Loc(Loc), PlaceholderTy(PlaceholderTy),
ExpansionTyR(ExpansionTyR), SemanticExpr(nullptr) {}

Identifier getPlaceholder() const { return Placeholder; }
SourceRange getSourceRange() const { return Loc; }
TypeLoc &getTypeLoc() { return PlaceholderTy; }
TypeLoc getTypeLoc() const { return PlaceholderTy; }
TypeRepr *getPlaceholderTypeRepr() const { return PlaceholderTy; }
SourceLoc getTrailingAngleBracketLoc() const {
return Loc.getAdvancedLoc(Placeholder.getLength() - 1);
}
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -1551,7 +1551,7 @@ class Parser {
ParameterList *&params,
SourceLoc &throwsLoc,
SourceLoc &arrowLoc,
TypeRepr *&explicitResultType,
TypeExpr *&explicitResultType,
SourceLoc &inLoc);

Expr *parseExprAnonClosureArg();
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/ASTDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2695,7 +2695,7 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
}
}
OS << '\n';
auto *TyR = E->getTypeLoc().getTypeRepr();
auto *TyR = E->getPlaceholderTypeRepr();
auto *ExpTyR = E->getTypeForExpansion();
if (TyR)
printRec(TyR);
Expand Down
5 changes: 3 additions & 2 deletions lib/AST/ASTWalker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -810,9 +810,10 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
Expr *visitClosureExpr(ClosureExpr *expr) {
visit(expr->getParameters());

if (expr->hasExplicitResultType())
if (doIt(expr->getExplicitResultTypeLoc()))
if (expr->hasExplicitResultType()) {
if (doIt(expr->getExplicitResultTypeRepr()))
return nullptr;
}

// Handle single-expression closures.
if (expr->hasSingleExpressionBody()) {
Expand Down
9 changes: 7 additions & 2 deletions lib/AST/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1191,9 +1191,9 @@ ErasureExpr *ErasureExpr::create(ASTContext &ctx, Expr *subExpr, Type type,

UnresolvedSpecializeExpr *UnresolvedSpecializeExpr::create(ASTContext &ctx,
Expr *SubExpr, SourceLoc LAngleLoc,
ArrayRef<TypeLoc> UnresolvedParams,
ArrayRef<TypeRepr *> UnresolvedParams,
SourceLoc RAngleLoc) {
auto size = totalSizeToAlloc<TypeLoc>(UnresolvedParams.size());
auto size = totalSizeToAlloc<TypeRepr *>(UnresolvedParams.size());
auto mem = ctx.Allocate(size, alignof(UnresolvedSpecializeExpr));
return ::new(mem) UnresolvedSpecializeExpr(SubExpr, LAngleLoc,
UnresolvedParams, RAngleLoc);
Expand Down Expand Up @@ -1861,6 +1861,11 @@ bool ClosureExpr::capturesSelfEnablingImplictSelf() const {
return false;
}

void ClosureExpr::setExplicitResultType(Type ty) {
assert(ty && !ty->hasTypeVariable());
ExplicitResultType->setType(MetatypeType::get(ty));
}

FORWARD_SOURCE_LOCS_TO(AutoClosureExpr, Body)

void AutoClosureExpr::setBody(Expr *E) {
Expand Down
22 changes: 18 additions & 4 deletions lib/IDE/ExprContextAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "swift/Basic/SourceManager.h"
#include "swift/IDE/CodeCompletion.h"
#include "swift/Sema/IDETypeChecking.h"
#include "swift/Subsystems.h"
#include "clang/AST/Attr.h"
#include "clang/AST/Decl.h"

Expand Down Expand Up @@ -209,10 +210,23 @@ Type swift::ide::getReturnTypeFromContext(const DeclContext *DC) {
if (ACE->getType() && !ACE->getType()->hasError())
return ACE->getResultType();
if (auto CE = dyn_cast<ClosureExpr>(ACE)) {
if (CE->hasExplicitResultType())
return const_cast<ClosureExpr *>(CE)
->getExplicitResultTypeLoc()
.getType();
if (CE->hasExplicitResultType()) {
if (auto ty = CE->getExplicitResultType()) {
return ty;
}

auto typeLoc = TypeLoc{CE->getExplicitResultTypeRepr()};
if (swift::performTypeLocChecking(DC->getASTContext(),
typeLoc,
/*isSILMode*/ false,
/*isSILType*/ false,
DC->getGenericEnvironmentOfContext(),
const_cast<DeclContext *>(DC),
/*diagnostics*/ false)) {
return Type();
}
return typeLoc.getType();
}
}
}
return Type();
Expand Down
5 changes: 2 additions & 3 deletions lib/IDE/Formatting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2295,9 +2295,8 @@ class FormatWalker : public ASTWalker {

SourceLoc ContextLoc = getContextLocForArgs(SM, USE);
ListAligner Aligner(SM, TargetLocation, ContextLoc, L, R);
for (auto &Arg: USE->getUnresolvedParams()) {
if (auto *T = Arg.getTypeRepr())
Aligner.updateAlignment(T->getSourceRange(), T);
for (auto *T : USE->getUnresolvedParams()) {
Aligner.updateAlignment(T->getSourceRange(), T);
}
return Aligner.getContextAndSetAlignment(CtxOverride);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/IDE/SyntaxModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ std::pair<bool, Expr *> ModelASTWalker::walkToExprPre(Expr *E) {
SN.BodyRange = innerCharSourceRangeFromSourceRange(SM, E->getSourceRange());
if (Closure->hasExplicitResultType())
SN.TypeRange = charSourceRangeFromSourceRange(SM,
Closure->getExplicitResultTypeLoc().getSourceRange());
Closure->getExplicitResultTypeRepr()->getSourceRange());

pushStructureNode(SN, Closure);

Expand Down
42 changes: 19 additions & 23 deletions lib/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1161,13 +1161,10 @@ Parser::parseExprPostfixSuffix(ParserResult<Expr> Result, bool isExprBasic,
if (argStat.isError())
diagnose(LAngleLoc, diag::while_parsing_as_left_angle_bracket);

SmallVector<TypeLoc, 8> locArgs;
for (auto ty : args)
locArgs.push_back(ty);
SyntaxContext->createNodeInPlace(SyntaxKind::SpecializeExpr);
Result = makeParserResult(
Result, UnresolvedSpecializeExpr::create(
Context, Result.get(), LAngleLoc, locArgs, RAngleLoc));
Context, Result.get(), LAngleLoc, args, RAngleLoc));
}

continue;
Expand Down Expand Up @@ -2312,10 +2309,7 @@ Expr *Parser::parseExprIdentifier() {
}

if (hasGenericArgumentList) {
SmallVector<TypeLoc, 8> locArgs;
for (auto ty : args)
locArgs.push_back(ty);
E = UnresolvedSpecializeExpr::create(Context, E, LAngleLoc, locArgs,
E = UnresolvedSpecializeExpr::create(Context, E, LAngleLoc, args,
RAngleLoc);
}
return E;
Expand All @@ -2326,14 +2320,14 @@ Expr *Parser::parseExprEditorPlaceholder(Token PlaceholderTok,
assert(PlaceholderTok.is(tok::identifier));
assert(PlaceholderId.isEditorPlaceholder());

auto parseTypeForPlaceholder = [&](TypeLoc &TyLoc, TypeRepr *&ExpansionTyR) {
auto parseTypeForPlaceholder = [&]() -> std::pair<TypeRepr *, TypeRepr *> {
Optional<EditorPlaceholderData> DataOpt =
swift::parseEditorPlaceholder(PlaceholderTok.getText());
if (!DataOpt)
return;
return {nullptr, nullptr};
StringRef TypeStr = DataOpt->Type;
if (TypeStr.empty())
return;
return {nullptr, nullptr};

// Ensure that we restore the parser state at exit.
ParserPositionRAII PPR(*this);
Expand Down Expand Up @@ -2363,21 +2357,21 @@ Expr *Parser::parseExprEditorPlaceholder(Token PlaceholderTok,
return parseType().getPtrOrNull();
};

TypeRepr *TyR = parseTypeString(TypeStr);
TyLoc = TyR;
TypeRepr *PlaceholderTyR = parseTypeString(TypeStr);
TypeRepr *ExpansionTyR = nullptr;
if (DataOpt->TypeForExpansion == TypeStr) {
ExpansionTyR = TyR;
ExpansionTyR = PlaceholderTyR;
} else {
ExpansionTyR = parseTypeString(DataOpt->TypeForExpansion);
}
return {PlaceholderTyR, ExpansionTyR};
};

TypeLoc TyLoc;
TypeRepr *PlaceholderTyR = nullptr;
TypeRepr *ExpansionTyR = nullptr;
parseTypeForPlaceholder(TyLoc, ExpansionTyR);
return new (Context) EditorPlaceholderExpr(PlaceholderId,
PlaceholderTok.getLoc(),
TyLoc, ExpansionTyR);
std::tie(PlaceholderTyR, ExpansionTyR) = parseTypeForPlaceholder();
return new (Context) EditorPlaceholderExpr(
PlaceholderId, PlaceholderTok.getLoc(), PlaceholderTyR, ExpansionTyR);
}

// Extract names of the tuple elements and preserve the structure
Expand Down Expand Up @@ -2422,7 +2416,7 @@ parseClosureSignatureIfPresent(SourceRange &bracketRange,
VarDecl *&capturedSelfDecl,
ParameterList *&params, SourceLoc &throwsLoc,
SourceLoc &arrowLoc,
TypeRepr *&explicitResultType, SourceLoc &inLoc){
TypeExpr *&explicitResultType, SourceLoc &inLoc){
// Clear out result parameters.
bracketRange = SourceRange();
capturedSelfDecl = nullptr;
Expand Down Expand Up @@ -2686,12 +2680,14 @@ parseClosureSignatureIfPresent(SourceRange &bracketRange,
arrowLoc = consumeToken();

// Parse the type.
explicitResultType =
auto *explicitResultTypeRepr =
parseType(diag::expected_closure_result_type).getPtrOrNull();
if (!explicitResultType) {
if (!explicitResultTypeRepr) {
// If we couldn't parse the result type, clear out the arrow location.
arrowLoc = SourceLoc();
invalid = true;
} else {
explicitResultType = new (Context) TypeExpr(explicitResultTypeRepr);
}
}
}
Expand Down Expand Up @@ -2791,7 +2787,7 @@ ParserResult<Expr> Parser::parseExprClosure() {
ParameterList *params = nullptr;
SourceLoc throwsLoc;
SourceLoc arrowLoc;
TypeRepr *explicitResultType;
TypeExpr *explicitResultType;
SourceLoc inLoc;
parseClosureSignatureIfPresent(bracketRange, captureList,
capturedSelfDecl, params, throwsLoc,
Expand Down
4 changes: 4 additions & 0 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7696,6 +7696,10 @@ namespace {
auto *params = closure->getParameters();
TypeChecker::coerceParameterListToType(params, closure, fnType);

if (closure->hasExplicitResultType()) {
closure->setExplicitResultType(fnType->getResult());
}

if (auto transform =
Rewriter.getAppliedBuilderTransform(closure)) {
// Apply the function builder to the closure. We want to be in the
Expand Down
Loading