Skip to content

Commit 606228e

Browse files
committed
Merge remote-tracking branch 'origin/master' into master-next
2 parents 4771a1c + bf6b01d commit 606228e

File tree

83 files changed

+1258
-1160
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+1258
-1160
lines changed

include/swift/AST/Decl.h

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5615,7 +5615,7 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
56155615
}
56165616

56175617
struct BodySynthesizer {
5618-
void (* Fn)(AbstractFunctionDecl *, void *);
5618+
std::pair<BraceStmt *, bool> (* Fn)(AbstractFunctionDecl *, void *);
56195619
void *Context;
56205620
};
56215621

@@ -5748,18 +5748,8 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
57485748
/// have a body for this function.
57495749
///
57505750
/// \sa hasBody()
5751-
BraceStmt *getBody(bool canSynthesize = true) const {
5752-
if (canSynthesize && getBodyKind() == BodyKind::Synthesize) {
5753-
const_cast<AbstractFunctionDecl *>(this)->setBodyKind(BodyKind::None);
5754-
(Synthesizer.Fn)(const_cast<AbstractFunctionDecl *>(this),
5755-
Synthesizer.Context);
5756-
}
5757-
if (getBodyKind() == BodyKind::Parsed ||
5758-
getBodyKind() == BodyKind::TypeChecked) {
5759-
return Body;
5760-
}
5761-
return nullptr;
5762-
}
5751+
BraceStmt *getBody(bool canSynthesize = true) const;
5752+
57635753
void setBody(BraceStmt *S, BodyKind NewBodyKind = BodyKind::Parsed) {
57645754
assert(getBodyKind() != BodyKind::Skipped &&
57655755
"cannot set a body if it was skipped");
@@ -5784,8 +5774,12 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
57845774
}
57855775

57865776
/// Note that parsing for the body was delayed.
5787-
void setBodySynthesizer(void (* fn)(AbstractFunctionDecl *, void *),
5788-
void *context = nullptr) {
5777+
///
5778+
/// The function should return the body statement and a flag indicating
5779+
/// whether that body is already type-checked.
5780+
void setBodySynthesizer(
5781+
std::pair<BraceStmt *, bool> (* fn)(AbstractFunctionDecl *, void *),
5782+
void *context = nullptr) {
57895783
assert(getBodyKind() == BodyKind::None);
57905784
Synthesizer = {fn, context};
57915785
setBodyKind(BodyKind::Synthesize);

include/swift/AST/Expr.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -599,12 +599,6 @@ class CodeCompletionExpr : public Expr {
599599
class LiteralExpr : public Expr {
600600
public:
601601
LiteralExpr(ExprKind Kind, bool Implicit) : Expr(Kind, Implicit) {}
602-
603-
// Make an exact copy of this one AST node.
604-
LiteralExpr *
605-
shallowClone(ASTContext &Ctx,
606-
llvm::function_ref<void(Expr *, Type)> setType,
607-
llvm::function_ref<Type(const Expr *)> getType) const;
608602

609603
static bool classof(const Expr *E) {
610604
return E->getKind() >= ExprKind::First_LiteralExpr &&
@@ -1131,7 +1125,6 @@ class ObjectLiteralExpr final
11311125

11321126
private:
11331127
Expr *Arg;
1134-
Expr *SemanticExpr;
11351128
SourceLoc PoundLoc;
11361129
ConcreteDeclRef Initializer;
11371130

@@ -1181,9 +1174,6 @@ class ObjectLiteralExpr final
11811174
return Bits.ObjectLiteralExpr.HasTrailingClosure;
11821175
}
11831176

1184-
Expr *getSemanticExpr() const { return SemanticExpr; }
1185-
void setSemanticExpr(Expr *expr) { SemanticExpr = expr; }
1186-
11871177
SourceLoc getSourceLoc() const { return PoundLoc; }
11881178
SourceRange getSourceRange() const {
11891179
return SourceRange(PoundLoc, Arg->getEndLoc());

include/swift/AST/SimpleRequest.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ namespace detail {
113113

114114
/// Extract the first, nearest source location from a tuple.
115115
template<unsigned Index, typename ...Types,
116-
typename = typename std::enable_if<Index < sizeof...(Types)>::type>
116+
typename = typename std::enable_if<(Index < sizeof...(Types))>::type>
117117
SourceLoc extractNearestSourceLocTuple(const std::tuple<Types...> &value) {
118118
SourceLoc loc = maybeExtractNearestSourceLoc(std::get<Index>(value));
119119
if (loc.isValid())

include/swift/Basic/SourceManager.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,14 @@ class SourceManager {
231231
void verifyAllBuffers() const;
232232

233233
/// Translate line and column pair to the offset.
234+
/// If the column number is the maximum unsinged int, return the offset of the end of the line.
234235
llvm::Optional<unsigned> resolveFromLineCol(unsigned BufferId, unsigned Line,
235236
unsigned Col) const;
236237

238+
/// Translate the end position of the given line to the offset.
239+
llvm::Optional<unsigned> resolveOffsetForEndOfLine(unsigned BufferId,
240+
unsigned Line) const;
241+
237242
SourceLoc getLocForLineCol(unsigned BufferId, unsigned Line, unsigned Col) const {
238243
auto Offset = resolveFromLineCol(BufferId, Line, Col);
239244
return Offset.hasValue() ? getLocForOffset(BufferId, Offset.getValue()) :

lib/AST/ASTDumper.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1977,7 +1977,6 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
19771977
printArgumentLabels(E->getArgumentLabels());
19781978
OS << "\n";
19791979
printRec(E->getArg());
1980-
printSemanticExpr(E->getSemanticExpr());
19811980
PrintWithColorRAII(OS, ParenthesisColor) << ')';
19821981
}
19831982

lib/AST/ASTScopeSourceRange.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ SourceRange AbstractFunctionDeclScope::getChildlessSourceRange() const {
301301
assert(r.End.isValid());
302302
return r;
303303
}
304-
return decl->getBody()->getSourceRange();
304+
return decl->getBodySourceRange();
305305
}
306306

307307
SourceRange AbstractFunctionParamsScope::getChildlessSourceRange() const {

lib/AST/ASTWalker.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,6 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
484484
}
485485

486486
Expr *visitObjectLiteralExpr(ObjectLiteralExpr *E) {
487-
HANDLE_SEMANTIC_EXPR(E);
488-
489487
if (Expr *arg = E->getArg()) {
490488
if (Expr *arg2 = doIt(arg)) {
491489
E->setArg(arg2);

lib/AST/Decl.cpp

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6237,17 +6237,51 @@ bool AbstractFunctionDecl::argumentNameIsAPIByDefault() const {
62376237
return false;
62386238
}
62396239

6240+
BraceStmt *AbstractFunctionDecl::getBody(bool canSynthesize) const {
6241+
switch (getBodyKind()) {
6242+
case BodyKind::Deserialized:
6243+
case BodyKind::MemberwiseInitializer:
6244+
case BodyKind::None:
6245+
case BodyKind::Skipped:
6246+
return nullptr;
6247+
6248+
case BodyKind::Parsed:
6249+
case BodyKind::TypeChecked:
6250+
return Body;
6251+
6252+
case BodyKind::Unparsed:
6253+
// FIXME: Go parse now!
6254+
return nullptr;
6255+
6256+
case BodyKind::Synthesize: {
6257+
if (!canSynthesize)
6258+
return nullptr;
6259+
6260+
const_cast<AbstractFunctionDecl *>(this)->setBodyKind(BodyKind::None);
6261+
BraceStmt *body;
6262+
bool isTypeChecked;
6263+
6264+
auto mutableThis = const_cast<AbstractFunctionDecl *>(this);
6265+
std::tie(body, isTypeChecked) = (Synthesizer.Fn)(
6266+
mutableThis, Synthesizer.Context);
6267+
mutableThis->setBody(
6268+
body, isTypeChecked ? BodyKind::TypeChecked : BodyKind::Parsed);
6269+
return body;
6270+
}
6271+
}
6272+
}
6273+
62406274
SourceRange AbstractFunctionDecl::getBodySourceRange() const {
62416275
switch (getBodyKind()) {
62426276
case BodyKind::None:
62436277
case BodyKind::MemberwiseInitializer:
62446278
case BodyKind::Deserialized:
6279+
case BodyKind::Synthesize:
62456280
return SourceRange();
62466281

62476282
case BodyKind::Parsed:
6248-
case BodyKind::Synthesize:
62496283
case BodyKind::TypeChecked:
6250-
if (auto body = getBody())
6284+
if (auto body = getBody(/*canSynthesize=*/false))
62516285
return body->getSourceRange();
62526286

62536287
return SourceRange();
@@ -6969,9 +7003,9 @@ SourceRange FuncDecl::getSourceRange() const {
69697003
getBodyKind() == BodyKind::Skipped)
69707004
return { StartLoc, BodyRange.End };
69717005

6972-
if (auto *B = getBody(/*canSynthesize=*/false)) {
6973-
if (!B->isImplicit())
6974-
return { StartLoc, B->getEndLoc() };
7006+
SourceLoc RBraceLoc = getBodySourceRange().End;
7007+
if (RBraceLoc.isValid()) {
7008+
return { StartLoc, RBraceLoc };
69757009
}
69767010

69777011
if (isa<AccessorDecl>(this))
@@ -7070,13 +7104,7 @@ SourceRange ConstructorDecl::getSourceRange() const {
70707104
if (isImplicit())
70717105
return getConstructorLoc();
70727106

7073-
if (getBodyKind() == BodyKind::Unparsed ||
7074-
getBodyKind() == BodyKind::Skipped)
7075-
return { getConstructorLoc(), BodyRange.End };
7076-
7077-
SourceLoc End;
7078-
if (auto body = getBody())
7079-
End = body->getEndLoc();
7107+
SourceLoc End = getBodySourceRange().End;
70807108
if (End.isInvalid())
70817109
End = getGenericTrailingWhereClauseSourceRange().End;
70827110
if (End.isInvalid())
@@ -7284,14 +7312,12 @@ ConstructorDecl::getDelegatingOrChainedInitKind(DiagnosticEngine *diags,
72847312
}
72857313

72867314
SourceRange DestructorDecl::getSourceRange() const {
7287-
if (getBodyKind() == BodyKind::Unparsed ||
7288-
getBodyKind() == BodyKind::Skipped)
7289-
return { getDestructorLoc(), BodyRange.End };
7290-
7291-
if (getBodyKind() == BodyKind::None)
7292-
return getDestructorLoc();
7315+
SourceLoc End = getBodySourceRange().End;
7316+
if (End.isInvalid()) {
7317+
End = getDestructorLoc();
7318+
}
72937319

7294-
return { getDestructorLoc(), getBody()->getEndLoc() };
7320+
return { getDestructorLoc(), End };
72957321
}
72967322

72977323
StringRef swift::getAssociativitySpelling(Associativity value) {

lib/AST/Expr.cpp

Lines changed: 1 addition & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -773,104 +773,6 @@ llvm::DenseMap<Expr *, unsigned> Expr::getPreorderIndexMap() {
773773
// Support methods for Exprs.
774774
//===----------------------------------------------------------------------===//
775775

776-
static LiteralExpr *
777-
shallowCloneImpl(const NilLiteralExpr *E, ASTContext &Ctx,
778-
llvm::function_ref<Type(const Expr *)> getType) {
779-
return new (Ctx) NilLiteralExpr(E->getLoc());
780-
}
781-
782-
static LiteralExpr *
783-
shallowCloneImpl(const IntegerLiteralExpr *E, ASTContext &Ctx,
784-
llvm::function_ref<Type(const Expr *)> getType) {
785-
auto res = new (Ctx) IntegerLiteralExpr(E->getDigitsText(),
786-
E->getSourceRange().End);
787-
if (E->isNegative())
788-
res->setNegative(E->getSourceRange().Start);
789-
return res;
790-
}
791-
792-
static LiteralExpr *
793-
shallowCloneImpl(const FloatLiteralExpr *E, ASTContext &Ctx,
794-
llvm::function_ref<Type(const Expr *)> getType) {
795-
auto res = new (Ctx) FloatLiteralExpr(E->getDigitsText(),
796-
E->getSourceRange().End);
797-
if (E->isNegative())
798-
res->setNegative(E->getSourceRange().Start);
799-
return res;
800-
}
801-
static LiteralExpr *
802-
shallowCloneImpl(const BooleanLiteralExpr *E, ASTContext &Ctx,
803-
llvm::function_ref<Type(const Expr *)> getType) {
804-
return new (Ctx) BooleanLiteralExpr(E->getValue(), E->getLoc());
805-
}
806-
static LiteralExpr *
807-
shallowCloneImpl(const StringLiteralExpr *E, ASTContext &Ctx,
808-
llvm::function_ref<Type(const Expr *)> getType) {
809-
auto res = new (Ctx) StringLiteralExpr(E->getValue(), E->getSourceRange());
810-
res->setEncoding(E->getEncoding());
811-
return res;
812-
}
813-
814-
static LiteralExpr *
815-
shallowCloneImpl(const InterpolatedStringLiteralExpr *E, ASTContext &Ctx,
816-
llvm::function_ref<Type(const Expr *)> getType) {
817-
auto res = new (Ctx) InterpolatedStringLiteralExpr(E->getLoc(),
818-
E->getTrailingQuoteLoc(),
819-
E->getLiteralCapacity(),
820-
E->getInterpolationCount(),
821-
E->getAppendingExpr());
822-
res->setSemanticExpr(E->getSemanticExpr());
823-
return res;
824-
}
825-
826-
static LiteralExpr *
827-
shallowCloneImpl(const MagicIdentifierLiteralExpr *E, ASTContext &Ctx,
828-
llvm::function_ref<Type(const Expr *)> getType) {
829-
auto res = new (Ctx) MagicIdentifierLiteralExpr(E->getKind(),
830-
E->getSourceRange().End);
831-
if (res->isString())
832-
res->setStringEncoding(E->getStringEncoding());
833-
return res;
834-
}
835-
836-
static LiteralExpr *
837-
shallowCloneImpl(const ObjectLiteralExpr *E, ASTContext &Ctx,
838-
llvm::function_ref<Type(const Expr *)> getType) {
839-
auto res =
840-
ObjectLiteralExpr::create(Ctx, E->getStartLoc(), E->getLiteralKind(),
841-
E->getArg(), E->isImplicit(), getType);
842-
res->setSemanticExpr(E->getSemanticExpr());
843-
return res;
844-
}
845-
846-
// Make an exact copy of this AST node.
847-
LiteralExpr *LiteralExpr::shallowClone(
848-
ASTContext &Ctx, llvm::function_ref<void(Expr *, Type)> setType,
849-
llvm::function_ref<Type(const Expr *)> getType) const {
850-
LiteralExpr *Result = nullptr;
851-
switch (getKind()) {
852-
default: llvm_unreachable("Unknown literal type!");
853-
#define DISPATCH_CLONE(KIND) \
854-
case ExprKind::KIND: \
855-
Result = shallowCloneImpl(cast<KIND##Expr>(this), Ctx, getType); \
856-
break;
857-
858-
DISPATCH_CLONE(NilLiteral)
859-
DISPATCH_CLONE(IntegerLiteral)
860-
DISPATCH_CLONE(FloatLiteral)
861-
DISPATCH_CLONE(BooleanLiteral)
862-
DISPATCH_CLONE(StringLiteral)
863-
DISPATCH_CLONE(InterpolatedStringLiteral)
864-
DISPATCH_CLONE(ObjectLiteral)
865-
DISPATCH_CLONE(MagicIdentifierLiteral)
866-
#undef DISPATCH_CLONE
867-
}
868-
869-
setType(Result, getType(this));
870-
Result->setImplicit(isImplicit());
871-
return Result;
872-
}
873-
874776
IntegerLiteralExpr * IntegerLiteralExpr::createFromUnsigned(ASTContext &C, unsigned value) {
875777
llvm::SmallString<8> Scratch;
876778
llvm::APInt(sizeof(unsigned)*8, value).toString(Scratch, 10, /*signed*/ false);
@@ -1196,7 +1098,7 @@ ObjectLiteralExpr::ObjectLiteralExpr(SourceLoc PoundLoc, LiteralKind LitKind,
11961098
bool hasTrailingClosure,
11971099
bool implicit)
11981100
: LiteralExpr(ExprKind::ObjectLiteral, implicit),
1199-
Arg(Arg), SemanticExpr(nullptr), PoundLoc(PoundLoc) {
1101+
Arg(Arg), PoundLoc(PoundLoc) {
12001102
Bits.ObjectLiteralExpr.LitKind = static_cast<unsigned>(LitKind);
12011103
assert(getLiteralKind() == LitKind);
12021104
Bits.ObjectLiteralExpr.NumArgLabels = argLabels.size();

lib/Basic/SourceLoc.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,12 +310,19 @@ void CharSourceRange::dump(const SourceManager &SM) const {
310310
print(llvm::errs(), SM);
311311
}
312312

313+
llvm::Optional<unsigned>
314+
SourceManager::resolveOffsetForEndOfLine(unsigned BufferId,
315+
unsigned Line) const {
316+
return resolveFromLineCol(BufferId, Line, ~0u);
317+
}
318+
313319
llvm::Optional<unsigned> SourceManager::resolveFromLineCol(unsigned BufferId,
314320
unsigned Line,
315321
unsigned Col) const {
316322
if (Line == 0 || Col == 0) {
317323
return None;
318324
}
325+
const bool LineEnd = Col == ~0u;
319326
auto InputBuf = getLLVMSourceMgr().getMemoryBuffer(BufferId);
320327
const char *Ptr = InputBuf->getBufferStart();
321328
const char *End = InputBuf->getBufferEnd();
@@ -331,14 +338,18 @@ llvm::Optional<unsigned> SourceManager::resolveFromLineCol(unsigned BufferId,
331338
return None;
332339
}
333340
Ptr = LineStart;
334-
335341
// The <= here is to allow for non-inclusive range end positions at EOF
336-
for (; Ptr <= End; ++Ptr) {
342+
for (; ; ++Ptr) {
337343
--Col;
338344
if (Col == 0)
339345
return Ptr - InputBuf->getBufferStart();
340-
if (*Ptr == '\n')
341-
break;
346+
if (*Ptr == '\n' || Ptr == End) {
347+
if (LineEnd) {
348+
return Ptr - InputBuf->getBufferStart();
349+
} else {
350+
break;
351+
}
352+
}
342353
}
343354
return None;
344355
}

0 commit comments

Comments
 (0)