Skip to content
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
12 changes: 8 additions & 4 deletions include/swift/AST/GenericParamList.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,9 @@ class GenericParamList final :
if (WhereLoc.isInvalid())
return SourceRange();

if (Requirements.empty())
return WhereLoc;

auto endLoc = Requirements.back().getSourceRange().End;
return SourceRange(WhereLoc, endLoc);
}
Expand Down Expand Up @@ -352,16 +355,18 @@ class alignas(RequirementRepr) TrailingWhereClause final :
friend TrailingObjects;

SourceLoc WhereLoc;
SourceLoc EndLoc;

/// The number of requirements. The actual requirements are tail-allocated.
unsigned NumRequirements;

TrailingWhereClause(SourceLoc whereLoc,
TrailingWhereClause(SourceLoc whereLoc, SourceLoc endLoc,
ArrayRef<RequirementRepr> requirements);

public:
/// Create a new trailing where clause with the given set of requirements.
static TrailingWhereClause *create(ASTContext &ctx, SourceLoc whereLoc,
static TrailingWhereClause *create(ASTContext &ctx,
SourceLoc whereLoc, SourceLoc endLoc,
ArrayRef<RequirementRepr> requirements);

/// Retrieve the location of the 'where' keyword.
Expand All @@ -379,8 +384,7 @@ class alignas(RequirementRepr) TrailingWhereClause final :

/// Compute the source range containing this trailing where clause.
SourceRange getSourceRange() const {
return SourceRange(WhereLoc,
getRequirements().back().getSourceRange().End);
return SourceRange(WhereLoc, EndLoc);
}

void print(llvm::raw_ostream &OS, bool printWhereKeyword) const;
Expand Down
5 changes: 3 additions & 2 deletions include/swift/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -1689,8 +1689,9 @@ class Parser {
parseFreestandingGenericWhereClause(GenericContext *genCtx);

ParserStatus parseGenericWhereClause(
SourceLoc &WhereLoc, SmallVectorImpl<RequirementRepr> &Requirements,
bool &FirstTypeInComplete, bool AllowLayoutConstraints = false);
SourceLoc &WhereLoc, SourceLoc &EndLoc,
SmallVectorImpl<RequirementRepr> &Requirements,
bool AllowLayoutConstraints = false);

ParserStatus
parseProtocolOrAssociatedTypeWhereClause(TrailingWhereClause *&trailingWhere,
Expand Down
7 changes: 4 additions & 3 deletions lib/AST/GenericParamList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ GenericTypeParamDecl *GenericParamList::lookUpGenericParam(
}

TrailingWhereClause::TrailingWhereClause(
SourceLoc whereLoc,
SourceLoc whereLoc, SourceLoc endLoc,
ArrayRef<RequirementRepr> requirements)
: WhereLoc(whereLoc),
: WhereLoc(whereLoc), EndLoc(endLoc),
NumRequirements(requirements.size())
{
std::uninitialized_copy(requirements.begin(), requirements.end(),
Expand All @@ -122,8 +122,9 @@ TrailingWhereClause::TrailingWhereClause(
TrailingWhereClause *TrailingWhereClause::create(
ASTContext &ctx,
SourceLoc whereLoc,
SourceLoc endLoc,
ArrayRef<RequirementRepr> requirements) {
unsigned size = totalSizeToAlloc<RequirementRepr>(requirements.size());
void *mem = ctx.Allocate(size, alignof(TrailingWhereClause));
return new (mem) TrailingWhereClause(whereLoc, requirements);
return new (mem) TrailingWhereClause(whereLoc, endLoc, requirements);
}
27 changes: 12 additions & 15 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -717,13 +717,12 @@ bool Parser::parseSpecializeAttributeArguments(

// Parse the where clause.
if (Tok.is(tok::kw_where)) {
SourceLoc whereLoc;
SourceLoc whereLoc, endLoc;
SmallVector<RequirementRepr, 4> requirements;
bool firstTypeInComplete;
parseGenericWhereClause(whereLoc, requirements, firstTypeInComplete,
parseGenericWhereClause(whereLoc, endLoc, requirements,
/* AllowLayoutConstraints */ true);
TrailingWhereClause =
TrailingWhereClause::create(Context, whereLoc, requirements);
TrailingWhereClause::create(Context, whereLoc, endLoc, requirements);
}
return true;
}
Expand Down Expand Up @@ -1053,12 +1052,12 @@ bool Parser::parseDifferentiableAttributeArguments(

// Parse a trailing 'where' clause if any.
if (Tok.is(tok::kw_where)) {
SourceLoc whereLoc;
SourceLoc whereLoc, endLoc;
SmallVector<RequirementRepr, 4> requirements;
bool firstTypeInComplete;
parseGenericWhereClause(whereLoc, requirements, firstTypeInComplete,
parseGenericWhereClause(whereLoc, endLoc, requirements,
/*AllowLayoutConstraints*/ true);
whereClause = TrailingWhereClause::create(Context, whereLoc, requirements);
whereClause =
TrailingWhereClause::create(Context, whereLoc, endLoc, requirements);
}
return false;
}
Expand Down Expand Up @@ -3813,7 +3812,7 @@ void Parser::setLocalDiscriminator(ValueDecl *D) {
return;

if (auto TD = dyn_cast<TypeDecl>(D))
if (!getScopeInfo().isInactiveConfigBlock())
if (!InInactiveClauseEnvironment)
SF.LocalTypeDecls.insert(TD);

const Identifier name = D->getBaseIdentifier();
Expand Down Expand Up @@ -4878,19 +4877,17 @@ Parser::parseDeclExtension(ParseDeclOptions Flags, DeclAttributes &Attributes) {
TrailingWhereClause *trailingWhereClause = nullptr;
bool trailingWhereHadCodeCompletion = false;
if (Tok.is(tok::kw_where)) {
SourceLoc whereLoc;
SourceLoc whereLoc, endLoc;
SmallVector<RequirementRepr, 4> requirements;
bool firstTypeInComplete;
auto whereStatus = parseGenericWhereClause(whereLoc, requirements,
firstTypeInComplete);
auto whereStatus = parseGenericWhereClause(whereLoc, endLoc, requirements);
if (whereStatus.hasCodeCompletion()) {
if (isCodeCompletionFirstPass())
return whereStatus;
trailingWhereHadCodeCompletion = true;
}
if (!requirements.empty()) {
trailingWhereClause = TrailingWhereClause::create(Context, whereLoc,
requirements);
trailingWhereClause =
TrailingWhereClause::create(Context, whereLoc, endLoc, requirements);
}
status |= whereStatus;
}
Expand Down
38 changes: 16 additions & 22 deletions lib/Parse/ParseGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ Parser::parseGenericParameters(SourceLoc LAngleLoc) {

// Parse the optional where-clause.
SourceLoc WhereLoc;
SourceLoc EndLoc;
SmallVector<RequirementRepr, 4> Requirements;
bool FirstTypeInComplete;
if (Tok.is(tok::kw_where) &&
parseGenericWhereClause(WhereLoc, Requirements,
FirstTypeInComplete).isErrorOrHasCompletion()) {
parseGenericWhereClause(WhereLoc, EndLoc, Requirements)
.isErrorOrHasCompletion()) {
Invalid = true;
}

Expand Down Expand Up @@ -261,16 +261,14 @@ Parser::diagnoseWhereClauseInGenericParamList(const GenericParamList *
/// same-type-requirement:
/// type-identifier '==' type
ParserStatus Parser::parseGenericWhereClause(
SourceLoc &WhereLoc,
SourceLoc &WhereLoc, SourceLoc &EndLoc,
SmallVectorImpl<RequirementRepr> &Requirements,
bool &FirstTypeInComplete,
bool AllowLayoutConstraints) {
SyntaxParsingContext ClauseContext(SyntaxContext,
SyntaxKind::GenericWhereClause);
ParserStatus Status;
// Parse the 'where'.
WhereLoc = consumeToken(tok::kw_where);
FirstTypeInComplete = false;
SyntaxParsingContext ReqListContext(SyntaxContext,
SyntaxKind::GenericRequirementList);
bool HasNextReq;
Expand All @@ -283,7 +281,7 @@ ParserStatus Parser::parseGenericWhereClause(
if (Tok.is(tok::code_complete)) {
if (CodeCompletion)
CodeCompletion->completeGenericRequirement();
consumeToken(tok::code_complete);
EndLoc = consumeToken(tok::code_complete);
Status.setHasCodeCompletionAndIsError();
break;
}
Expand All @@ -295,7 +293,6 @@ ParserStatus Parser::parseGenericWhereClause(
if (FirstType.hasCodeCompletion()) {
BodyContext->setTransparent();
Status.setHasCodeCompletionAndIsError();
FirstTypeInComplete = true;
}

if (FirstType.isNull()) {
Expand Down Expand Up @@ -383,8 +380,10 @@ ParserStatus Parser::parseGenericWhereClause(
}
} while (HasNextReq);

if (Requirements.empty())
WhereLoc = SourceLoc();
if (!Requirements.empty())
EndLoc = Requirements.back().getSourceRange().End;
else if (EndLoc.isInvalid())
EndLoc = WhereLoc;

return Status;
}
Expand All @@ -396,31 +395,26 @@ parseFreestandingGenericWhereClause(GenericContext *genCtx) {
assert(Tok.is(tok::kw_where) && "Shouldn't call this without a where");

SmallVector<RequirementRepr, 4> Requirements;
SourceLoc WhereLoc;
bool FirstTypeInComplete;
auto result = parseGenericWhereClause(WhereLoc, Requirements,
FirstTypeInComplete);
if (result.isErrorOrHasCompletion() || Requirements.empty())
return result;
SourceLoc WhereLoc, EndLoc;
auto result = parseGenericWhereClause(WhereLoc, EndLoc, Requirements);

genCtx->setTrailingWhereClause(
TrailingWhereClause::create(Context, WhereLoc, Requirements));
TrailingWhereClause::create(Context, WhereLoc, EndLoc, Requirements));

return ParserStatus();
return result;
}

/// Parse a where clause after a protocol or associated type declaration.
ParserStatus Parser::parseProtocolOrAssociatedTypeWhereClause(
TrailingWhereClause *&trailingWhere, bool isProtocol) {
assert(Tok.is(tok::kw_where) && "Shouldn't call this without a where");
SourceLoc whereLoc;
SourceLoc whereLoc, endLoc;
SmallVector<RequirementRepr, 4> requirements;
bool firstTypeInComplete;
auto whereStatus =
parseGenericWhereClause(whereLoc, requirements, firstTypeInComplete);
parseGenericWhereClause(whereLoc, endLoc, requirements);
if (whereStatus.isSuccess() && !whereStatus.hasCodeCompletion()) {
trailingWhere =
TrailingWhereClause::create(Context, whereLoc, requirements);
TrailingWhereClause::create(Context, whereLoc, endLoc, requirements);
} else if (whereStatus.hasCodeCompletion()) {
return whereStatus;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Parse/ParseIfConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ ParserResult<IfConfigDecl> Parser::parseIfConfig(
// Don't evaluate if it's in '-parse' mode, etc.
shouldEvaluatePoundIfDecls() &&
// If it's in inactive #if ... #endif block, there's no point to do it.
!getScopeInfo().isInactiveConfigBlock() &&
!InInactiveClauseEnvironment &&
// If this directive contains code completion location, 'isActive' is
// determined solely by which block has the completion token.
!codeCompletionClauseLoc.isValid();
Expand Down
1 change: 1 addition & 0 deletions tools/swift-ide-test/swift-ide-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3820,6 +3820,7 @@ int main(int argc, char *argv[]) {
InitInvok.setModuleName(options::ModuleName);

InitInvok.setSDKPath(options::SDK);
InitInvok.getLangOptions().DisableParserLookup = true;
InitInvok.getLangOptions().CollectParsedToken = true;
InitInvok.getLangOptions().BuildSyntaxTree = true;
InitInvok.getLangOptions().RequestEvaluatorGraphVizPath =
Expand Down