本文整理汇总了C++中ParserResult::get方法的典型用法代码示例。如果您正苦于以下问题:C++ ParserResult::get方法的具体用法?C++ ParserResult::get怎么用?C++ ParserResult::get使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ParserResult
的用法示例。
在下文中一共展示了ParserResult::get方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: backtrack
/// Parse a pattern with an optional type annotation.
///
/// typed-pattern ::= pattern (':' type)?
///
ParserResult<Pattern> Parser::parseTypedPattern() {
auto result = parsePattern();
// Now parse an optional type annotation.
if (Tok.is(tok::colon)) {
SourceLoc colonLoc = consumeToken(tok::colon);
if (result.isNull()) // Recover by creating AnyPattern.
result = makeParserErrorResult(new (Context) AnyPattern(colonLoc));
ParserResult<TypeRepr> Ty = parseType();
if (Ty.hasCodeCompletion())
return makeParserCodeCompletionResult<Pattern>();
if (!Ty.isNull()) {
// Attempt to diagnose initializer calls incorrectly written
// as typed patterns, such as "var x: [Int]()".
if (Tok.isFollowingLParen()) {
BacktrackingScope backtrack(*this);
// Create a local context if needed so we can parse trailing closures.
LocalContext dummyContext;
Optional<ContextChange> contextChange;
if (!CurLocalContext) {
contextChange.emplace(*this, CurDeclContext, &dummyContext);
}
SourceLoc lParenLoc, rParenLoc;
SmallVector<Expr *, 2> args;
SmallVector<Identifier, 2> argLabels;
SmallVector<SourceLoc, 2> argLabelLocs;
Expr *trailingClosure;
ParserStatus status = parseExprList(tok::l_paren, tok::r_paren,
/*isPostfix=*/true,
/*isExprBasic=*/false,
lParenLoc, args, argLabels,
argLabelLocs, rParenLoc,
trailingClosure);
if (status.isSuccess()) {
backtrack.cancelBacktrack();
// Suggest replacing ':' with '='
diagnose(lParenLoc, diag::initializer_as_typed_pattern)
.highlight({Ty.get()->getStartLoc(), rParenLoc})
.fixItReplace(colonLoc, " = ");
result.setIsParseError();
}
}
} else {
Ty = makeParserResult(new (Context) ErrorTypeRepr(PreviousLoc));
}
result = makeParserResult(result,
new (Context) TypedPattern(result.get(), Ty.get()));
}
return result;
}
示例2: parsingCollection
ParserResult<TypeRepr> Parser::parseTypeCollection() {
// Parse the leading '['.
assert(Tok.is(tok::l_square));
Parser::StructureMarkerRAII parsingCollection(*this, Tok);
SourceLoc lsquareLoc = consumeToken();
// Parse the element type.
ParserResult<TypeRepr> firstTy = parseType(diag::expected_element_type);
// If there is a ':', this is a dictionary type.
SourceLoc colonLoc;
ParserResult<TypeRepr> secondTy;
if (Tok.is(tok::colon)) {
colonLoc = consumeToken();
// Parse the second type.
secondTy = parseType(diag::expected_dictionary_value_type);
}
// Parse the closing ']'.
SourceLoc rsquareLoc;
parseMatchingToken(tok::r_square, rsquareLoc,
colonLoc.isValid()
? diag::expected_rbracket_dictionary_type
: diag::expected_rbracket_array_type,
lsquareLoc);
if (firstTy.hasCodeCompletion() || secondTy.hasCodeCompletion())
return makeParserCodeCompletionStatus();
// If we couldn't parse anything for one of the types, propagate the error.
if (firstTy.isNull() || (colonLoc.isValid() && secondTy.isNull()))
return makeParserError();
// Form the dictionary type.
SourceRange brackets(lsquareLoc, rsquareLoc);
if (colonLoc.isValid())
return makeParserResult(ParserStatus(firstTy) | ParserStatus(secondTy),
new (Context) DictionaryTypeRepr(firstTy.get(),
secondTy.get(),
colonLoc,
brackets));
// Form the array type.
return makeParserResult(firstTy,
new (Context) ArrayTypeRepr(firstTy.get(),
brackets));
}
示例3: makeParserResult
/// Parse an optional type annotation on a pattern.
///
/// pattern-type-annotation ::= (':' type)?
///
ParserResult<Pattern> Parser::
parseOptionalPatternTypeAnnotation(ParserResult<Pattern> result,
bool isOptional) {
// Parse an optional type annotation.
if (!consumeIf(tok::colon))
return result;
Pattern *P;
if (result.isNull()) // Recover by creating AnyPattern.
P = new (Context) AnyPattern(Tok.getLoc());
else
P = result.get();
ParserResult<TypeRepr> Ty = parseType();
if (Ty.hasCodeCompletion())
return makeParserCodeCompletionResult<Pattern>();
TypeRepr *repr = Ty.getPtrOrNull();
if (!repr)
repr = new (Context) ErrorTypeRepr(PreviousLoc);
// In an if-let, the actual type of the expression is Optional of whatever
// was written.
if (isOptional)
repr = new (Context) OptionalTypeRepr(repr, Tok.getLoc());
return makeParserResult(new (Context) TypedPattern(P, repr));
}
示例4: parseGenericArguments
bool Parser::parseGenericArguments(SmallVectorImpl<TypeRepr*> &Args,
SourceLoc &LAngleLoc,
SourceLoc &RAngleLoc) {
// Parse the opening '<'.
assert(startsWithLess(Tok) && "Generic parameter list must start with '<'");
LAngleLoc = consumeStartingLess();
do {
ParserResult<TypeRepr> Ty = parseType(diag::expected_type);
if (Ty.isNull() || Ty.hasCodeCompletion()) {
// Skip until we hit the '>'.
RAngleLoc = skipUntilGreaterInTypeList();
return true;
}
Args.push_back(Ty.get());
// Parse the comma, if the list continues.
} while (consumeIf(tok::comma));
if (!startsWithGreater(Tok)) {
checkForInputIncomplete();
diagnose(Tok, diag::expected_rangle_generic_arg_list);
diagnose(LAngleLoc, diag::opening_angle);
// Skip until we hit the '>'.
RAngleLoc = skipUntilGreaterInTypeList();
return true;
} else {
RAngleLoc = consumeStartingGreater();
}
return false;
}
示例5: parseMatchingPatternAsLetOrVar
/// matching-pattern ::= 'is' type
/// matching-pattern ::= matching-pattern-var
/// matching-pattern ::= expr
///
ParserResult<Pattern> Parser::parseMatchingPattern(bool isExprBasic) {
// TODO: Since we expect a pattern in this position, we should optimistically
// parse pattern nodes for productions shared by pattern and expression
// grammar. For short-term ease of initial implementation, we always go
// through the expr parser for ambiguous productions.
// Parse productions that can only be patterns.
if (Tok.isAny(tok::kw_var, tok::kw_let)) {
assert(Tok.isAny(tok::kw_let, tok::kw_var) && "expects var or let");
bool isLet = Tok.is(tok::kw_let);
SourceLoc varLoc = consumeToken();
return parseMatchingPatternAsLetOrVar(isLet, varLoc, isExprBasic);
}
// matching-pattern ::= 'is' type
if (Tok.is(tok::kw_is)) {
SourceLoc isLoc = consumeToken(tok::kw_is);
ParserResult<TypeRepr> castType = parseType();
if (castType.isNull() || castType.hasCodeCompletion())
return nullptr;
return makeParserResult(new (Context) IsPattern(isLoc, castType.get(),
nullptr));
}
// matching-pattern ::= expr
// Fall back to expression parsing for ambiguous forms. Name lookup will
// disambiguate.
ParserResult<Expr> subExpr =
parseExprImpl(diag::expected_pattern, isExprBasic);
if (subExpr.hasCodeCompletion())
return makeParserCodeCompletionStatus();
if (subExpr.isNull())
return nullptr;
// The most common case here is to parse something that was a lexically
// obvious pattern, which will come back wrapped in an immediate
// UnresolvedPatternExpr. Transform this now to simplify later code.
if (auto *UPE = dyn_cast<UnresolvedPatternExpr>(subExpr.get()))
return makeParserResult(UPE->getSubPattern());
return makeParserResult(new (Context) ExprPattern(subExpr.get()));
}
示例6: makeParserResult
/// parseTypeComposition
///
/// type-composition:
/// 'protocol' '<' type-composition-list? '>'
///
/// type-composition-list:
/// type-identifier (',' type-identifier)*
///
ParserResult<ProtocolCompositionTypeRepr> Parser::parseTypeComposition() {
SourceLoc ProtocolLoc = consumeToken(tok::kw_protocol);
// Check for the starting '<'.
if (!startsWithLess(Tok)) {
diagnose(Tok, diag::expected_langle_protocol);
return nullptr;
}
SourceLoc LAngleLoc = consumeStartingLess();
// Check for empty protocol composition.
if (startsWithGreater(Tok)) {
SourceLoc RAngleLoc = consumeStartingGreater();
return makeParserResult(new (Context) ProtocolCompositionTypeRepr(
ArrayRef<IdentTypeRepr *>(),
ProtocolLoc,
SourceRange(LAngleLoc,
RAngleLoc)));
}
// Parse the type-composition-list.
ParserStatus Status;
SmallVector<IdentTypeRepr *, 4> Protocols;
do {
// Parse the type-identifier.
ParserResult<IdentTypeRepr> Protocol = parseTypeIdentifier();
Status |= Protocol;
if (Protocol.isNonNull())
Protocols.push_back(Protocol.get());
} while (consumeIf(tok::comma));
// Check for the terminating '>'.
SourceLoc EndLoc = PreviousLoc;
if (startsWithGreater(Tok)) {
EndLoc = consumeStartingGreater();
} else {
if (Status.isSuccess()) {
diagnose(Tok, diag::expected_rangle_protocol);
diagnose(LAngleLoc, diag::opening_angle);
Status.setIsParseError();
}
// Skip until we hit the '>'.
EndLoc = skipUntilGreaterInTypeList(/*protocolComposition=*/true);
}
return makeParserResult(Status, ProtocolCompositionTypeRepr::create(
Context, Protocols, ProtocolLoc, SourceRange(LAngleLoc, EndLoc)));
}
示例7: parseType
ParserResult<TypeRepr> Parser::parseTypeIdentifierWithRecovery(
Diag<> MessageID, Diag<TypeLoc> NonIdentifierTypeMessageID) {
ParserResult<TypeRepr> Ty = parseType(MessageID);
if (!Ty.isParseError() && !isa<IdentTypeRepr>(Ty.get()) &&
!isa<ErrorTypeRepr>(Ty.get())) {
diagnose(Ty.get()->getStartLoc(), NonIdentifierTypeMessageID, Ty.get())
.highlight(Ty.get()->getSourceRange());
Ty.setIsParseError();
Ty = makeParserResult(
Ty, new (Context) ErrorTypeRepr(Ty.get()->getSourceRange()));
}
assert(Ty.isNull() ||
isa<IdentTypeRepr>(Ty.get()) ||
isa<ErrorTypeRepr>(Ty.get()));
return Ty;
}
示例8: parsePattern
/// Parse a pattern with an optional type annotation.
///
/// typed-pattern ::= pattern (':' type)?
///
ParserResult<Pattern> Parser::parseTypedPattern() {
auto result = parsePattern();
// Now parse an optional type annotation.
if (consumeIf(tok::colon)) {
if (result.isNull()) // Recover by creating AnyPattern.
result = makeParserErrorResult(new (Context) AnyPattern(PreviousLoc));
ParserResult<TypeRepr> Ty = parseType();
if (Ty.hasCodeCompletion())
return makeParserCodeCompletionResult<Pattern>();
if (Ty.isNull())
Ty = makeParserResult(new (Context) ErrorTypeRepr(PreviousLoc));
result = makeParserResult(result,
new (Context) TypedPattern(result.get(), Ty.get()));
}
return result;
}
示例9: make_pair
/// Parse an element of a tuple pattern.
///
/// pattern-tuple-element:
/// (identifier ':')? pattern
std::pair<ParserStatus, Optional<TuplePatternElt>>
Parser::parsePatternTupleElement() {
// If this element has a label, parse it.
Identifier Label;
SourceLoc LabelLoc;
// If the tuple element has a label, parse it.
if (Tok.is(tok::identifier) && peekToken().is(tok::colon)) {
LabelLoc = consumeIdentifier(&Label);
consumeToken(tok::colon);
}
// Parse the pattern.
ParserResult<Pattern> pattern = parsePattern();
if (pattern.hasCodeCompletion())
return std::make_pair(makeParserCodeCompletionStatus(), None);
if (pattern.isNull())
return std::make_pair(makeParserError(), None);
auto Elt = TuplePatternElt(Label, LabelLoc, pattern.get());
return std::make_pair(makeParserSuccess(), Elt);
}
示例10: ParsingTypeTuple
/// parseTypeTupleBody
/// type-tuple:
/// '(' type-tuple-body? ')'
/// type-tuple-body:
/// type-tuple-element (',' type-tuple-element)* '...'?
/// type-tuple-element:
/// identifier ':' type
/// type
ParserResult<TupleTypeRepr> Parser::parseTypeTupleBody() {
Parser::StructureMarkerRAII ParsingTypeTuple(*this, Tok);
SourceLoc RPLoc, LPLoc = consumeToken(tok::l_paren);
SourceLoc EllipsisLoc;
unsigned EllipsisIdx;
SmallVector<TypeRepr *, 8> ElementsR;
// We keep track of the labels separately, and apply them at the end.
SmallVector<std::tuple<Identifier, SourceLoc, Identifier, SourceLoc>, 4>
Labels;
ParserStatus Status = parseList(tok::r_paren, LPLoc, RPLoc,
tok::comma, /*OptionalSep=*/false,
/*AllowSepAfterLast=*/false,
diag::expected_rparen_tuple_type_list,
[&] () -> ParserStatus {
// If this is a deprecated use of the inout marker in an argument list,
// consume the inout.
SourceLoc InOutLoc;
bool hasAnyInOut = false;
bool hasValidInOut = false;
if (consumeIf(tok::kw_inout, InOutLoc)) {
hasAnyInOut = true;
hasValidInOut = false;
}
// If the tuple element starts with a potential argument label followed by a
// ':' or another potential argument label, then the identifier is an
// element tag, and it is followed by a type annotation.
if (Tok.canBeArgumentLabel() &&
(peekToken().is(tok::colon) || peekToken().canBeArgumentLabel())) {
// Consume the name
Identifier name;
if (!Tok.is(tok::kw__))
name = Context.getIdentifier(Tok.getText());
SourceLoc nameLoc = consumeToken();
// If there is a second name, consume it as well.
Identifier secondName;
SourceLoc secondNameLoc;
if (Tok.canBeArgumentLabel()) {
if (!Tok.is(tok::kw__))
secondName = Context.getIdentifier(Tok.getText());
secondNameLoc = consumeToken();
}
// Consume the ':'.
if (!consumeIf(tok::colon))
diagnose(Tok, diag::expected_parameter_colon);
SourceLoc postColonLoc = Tok.getLoc();
// Consume 'inout' if present.
if (!hasAnyInOut && consumeIf(tok::kw_inout, InOutLoc)) {
hasValidInOut = true;
}
SourceLoc extraneousInOutLoc;
while (consumeIf(tok::kw_inout, extraneousInOutLoc)) {
diagnose(Tok, diag::parameter_inout_var_let_repeated)
.fixItRemove(extraneousInOutLoc);
}
// Parse the type annotation.
ParserResult<TypeRepr> type = parseType(diag::expected_type);
if (type.hasCodeCompletion())
return makeParserCodeCompletionStatus();
if (type.isNull())
return makeParserError();
if (!hasValidInOut && hasAnyInOut) {
diagnose(Tok.getLoc(), diag::inout_as_attr_disallowed)
.fixItRemove(InOutLoc)
.fixItInsert(postColonLoc, "inout ");
}
// If an 'inout' marker was specified, build the type. Note that we bury
// the inout locator within the named locator. This is weird but required
// by sema apparently.
if (InOutLoc.isValid())
type = makeParserResult(new (Context) InOutTypeRepr(type.get(),
InOutLoc));
// Record the label. We will look at these at the end.
if (Labels.empty()) {
Labels.assign(ElementsR.size(),
std::make_tuple(Identifier(), SourceLoc(),
Identifier(), SourceLoc()));
}
Labels.push_back(std::make_tuple(name, nameLoc,
secondName, secondNameLoc));
ElementsR.push_back(type.get());
//.........这里部分代码省略.........
示例11: consumeIf
/// parseTypeSimple
/// type-simple:
/// type-identifier
/// type-tuple
/// type-composition
/// 'Any'
/// type-simple '.Type'
/// type-simple '.Protocol'
/// type-simple '?'
/// type-simple '!'
/// type-collection
ParserResult<TypeRepr> Parser::parseTypeSimple(Diag<> MessageID,
bool HandleCodeCompletion) {
ParserResult<TypeRepr> ty;
// If this is an "inout" marker for an identifier type, consume the inout.
SourceLoc InOutLoc;
consumeIf(tok::kw_inout, InOutLoc);
switch (Tok.getKind()) {
case tok::kw_Self:
ty = parseTypeIdentifier();
break;
case tok::identifier:
case tok::kw_protocol:
case tok::kw_Any:
ty = parseTypeIdentifierOrTypeComposition();
break;
case tok::l_paren:
ty = parseTypeTupleBody();
break;
case tok::code_complete:
if (!HandleCodeCompletion)
break;
if (CodeCompletion)
CodeCompletion->completeTypeSimpleBeginning();
// Eat the code completion token because we handled it.
consumeToken(tok::code_complete);
return makeParserCodeCompletionResult<TypeRepr>();
case tok::kw_super:
case tok::kw_self:
// These keywords don't start a decl or a statement, and thus should be
// safe to skip over.
diagnose(Tok, MessageID);
ty = makeParserErrorResult(new (Context) ErrorTypeRepr(Tok.getLoc()));
consumeToken();
// FIXME: we could try to continue to parse.
return ty;
case tok::l_square:
ty = parseTypeCollection();
break;
default:
checkForInputIncomplete();
diagnose(Tok, MessageID);
return nullptr;
}
// '.Type', '.Protocol', '?', and '!' still leave us with type-simple.
while (ty.isNonNull()) {
if ((Tok.is(tok::period) || Tok.is(tok::period_prefix))) {
if (peekToken().isContextualKeyword("Type")) {
consumeToken();
SourceLoc metatypeLoc = consumeToken(tok::identifier);
ty = makeParserResult(ty,
new (Context) MetatypeTypeRepr(ty.get(), metatypeLoc));
continue;
}
if (peekToken().isContextualKeyword("Protocol")) {
consumeToken();
SourceLoc protocolLoc = consumeToken(tok::identifier);
ty = makeParserResult(ty,
new (Context) ProtocolTypeRepr(ty.get(), protocolLoc));
continue;
}
}
if (!Tok.isAtStartOfLine()) {
if (isOptionalToken(Tok)) {
ty = parseTypeOptional(ty.get());
continue;
}
if (isImplicitlyUnwrappedOptionalToken(Tok)) {
ty = parseTypeImplicitlyUnwrappedOptional(ty.get());
continue;
}
}
break;
}
// If we parsed an inout modifier, prepend it.
if (InOutLoc.isValid())
ty = makeParserResult(new (Context) InOutTypeRepr(ty.get(),
InOutLoc));
return ty;
}
示例12: makeParserResult
/// parseType
/// type:
/// attribute-list type-function
/// attribute-list type-array
///
/// type-function:
/// type-simple '->' type
///
ParserResult<TypeRepr> Parser::parseType(Diag<> MessageID,
bool HandleCodeCompletion) {
// Parse attributes.
TypeAttributes attrs;
parseTypeAttributeList(attrs);
// Parse Generic Parameters. Generic Parameters are visible in the function
// body.
GenericParamList *generics = nullptr;
if (isInSILMode()) {
generics = maybeParseGenericParams().getPtrOrNull();
}
ParserResult<TypeRepr> ty = parseTypeSimple(MessageID, HandleCodeCompletion);
if (ty.hasCodeCompletion())
return makeParserCodeCompletionResult<TypeRepr>();
if (ty.isNull())
return nullptr;
// Parse a throws specifier. 'throw' is probably a typo for 'throws',
// but in local contexts we could just be at the end of a statement,
// so we need to check for the arrow.
ParserPosition beforeThrowsPos;
SourceLoc throwsLoc;
bool rethrows = false;
if (Tok.isAny(tok::kw_throws, tok::kw_rethrows) ||
(Tok.is(tok::kw_throw) && peekToken().is(tok::arrow))) {
if (Tok.is(tok::kw_throw)) {
diagnose(Tok.getLoc(), diag::throw_in_function_type)
.fixItReplace(Tok.getLoc(), "throws");
}
beforeThrowsPos = getParserPosition();
rethrows = Tok.is(tok::kw_rethrows);
throwsLoc = consumeToken();
}
// Handle type-function if we have an arrow.
SourceLoc arrowLoc;
if (consumeIf(tok::arrow, arrowLoc)) {
ParserResult<TypeRepr> SecondHalf =
parseType(diag::expected_type_function_result);
if (SecondHalf.hasCodeCompletion())
return makeParserCodeCompletionResult<TypeRepr>();
if (SecondHalf.isNull())
return nullptr;
if (rethrows) {
// 'rethrows' is only allowed on function declarations for now.
diagnose(throwsLoc, diag::rethrowing_function_type);
}
auto fnTy = new (Context) FunctionTypeRepr(generics, ty.get(),
throwsLoc,
arrowLoc,
SecondHalf.get());
return makeParserResult(applyAttributeToType(fnTy, attrs));
} else if (throwsLoc.isValid()) {
// Don't consume 'throws', so we can emit a more useful diagnostic when
// parsing a function decl.
restoreParserPosition(beforeThrowsPos);
return ty;
}
// Only function types may be generic.
if (generics) {
auto brackets = generics->getSourceRange();
diagnose(brackets.Start, diag::generic_non_function);
}
// Parse legacy array types for migration.
while (ty.isNonNull() && !Tok.isAtStartOfLine()) {
if (Tok.is(tok::l_square)) {
ty = parseTypeArray(ty.get());
} else {
break;
}
}
if (ty.isNonNull() && !ty.hasCodeCompletion()) {
ty = makeParserResult(applyAttributeToType(ty.get(), attrs));
}
return ty;
}
示例13: create
GenericParamList *Parser::parseGenericParameters(SourceLoc LAngleLoc) {
// Parse the generic parameter list.
SmallVector<GenericTypeParamDecl *, 4> GenericParams;
bool Invalid = false;
do {
// Parse the name of the parameter.
Identifier Name;
SourceLoc NameLoc;
if (parseIdentifier(Name, NameLoc, diag::expected_generics_parameter_name)) {
Invalid = true;
break;
}
// Parse the ':' followed by a type.
SmallVector<TypeLoc, 1> Inherited;
if (Tok.is(tok::colon)) {
(void)consumeToken();
ParserResult<TypeRepr> Ty;
if (Tok.getKind() == tok::identifier) {
Ty = parseTypeIdentifier();
} else if (Tok.getKind() == tok::kw_protocol) {
Ty = parseTypeComposition();
} else {
diagnose(Tok, diag::expected_generics_type_restriction, Name);
Invalid = true;
}
// FIXME: code completion not handled.
if (Ty.isNonNull())
Inherited.push_back(Ty.get());
}
// We always create generic type parameters with a depth of zero.
// Semantic analysis fills in the depth when it processes the generic
// parameter list.
auto Param = new (Context) GenericTypeParamDecl(CurDeclContext, Name,
NameLoc, /*Depth=*/0,
GenericParams.size());
if (!Inherited.empty())
Param->setInherited(Context.AllocateCopy(Inherited));
GenericParams.push_back(Param);
// Add this parameter to the scope.
addToScope(Param);
// Parse the comma, if the list continues.
} while (consumeIf(tok::comma));
// Parse the optional where-clause.
SourceLoc WhereLoc;
SmallVector<RequirementRepr, 4> Requirements;
if (Tok.is(tok::kw_where) &&
parseGenericWhereClause(WhereLoc, Requirements)) {
Invalid = true;
}
// Parse the closing '>'.
SourceLoc RAngleLoc;
if (!startsWithGreater(Tok)) {
if (!Invalid) {
diagnose(Tok, diag::expected_rangle_generics_param);
diagnose(LAngleLoc, diag::opening_angle);
Invalid = true;
}
// Skip until we hit the '>'.
skipUntilGreaterInTypeList();
if (startsWithGreater(Tok))
RAngleLoc = consumeStartingGreater();
else
RAngleLoc = Tok.getLoc();
} else {
RAngleLoc = consumeStartingGreater();
}
if (GenericParams.empty())
return nullptr;
return GenericParamList::create(Context, LAngleLoc, GenericParams,
WhereLoc, Requirements, RAngleLoc);
}
示例14: GPSContext
ParserStatus
Parser::parseGenericParametersBeforeWhere(SourceLoc LAngleLoc,
SmallVectorImpl<GenericTypeParamDecl *> &GenericParams) {
ParserStatus Result;
SyntaxParsingContext GPSContext(SyntaxContext, SyntaxKind::GenericParameterList);
bool HasNextParam;
do {
SyntaxParsingContext GParamContext(SyntaxContext, SyntaxKind::GenericParameter);
// Note that we're parsing a declaration.
StructureMarkerRAII ParsingDecl(*this, Tok.getLoc(),
StructureMarkerKind::Declaration);
if (ParsingDecl.isFailed()) {
return makeParserError();
}
// Parse attributes.
DeclAttributes attributes;
if (Tok.hasComment())
attributes.add(new (Context) RawDocCommentAttr(Tok.getCommentRange()));
bool foundCCTokenInAttr;
parseDeclAttributeList(attributes, foundCCTokenInAttr);
// Parse the name of the parameter.
Identifier Name;
SourceLoc NameLoc;
if (parseIdentifier(Name, NameLoc,
diag::expected_generics_parameter_name)) {
Result.setIsParseError();
break;
}
// Parse the ':' followed by a type.
SmallVector<TypeLoc, 1> Inherited;
if (Tok.is(tok::colon)) {
(void)consumeToken();
ParserResult<TypeRepr> Ty;
if (Tok.isAny(tok::identifier, tok::code_complete, tok::kw_protocol,
tok::kw_Any)) {
Ty = parseType();
} else if (Tok.is(tok::kw_class)) {
diagnose(Tok, diag::unexpected_class_constraint);
diagnose(Tok, diag::suggest_anyobject)
.fixItReplace(Tok.getLoc(), "AnyObject");
consumeToken();
Result.setIsParseError();
} else {
diagnose(Tok, diag::expected_generics_type_restriction, Name);
Result.setIsParseError();
}
if (Ty.hasCodeCompletion())
return makeParserCodeCompletionStatus();
if (Ty.isNonNull())
Inherited.push_back(Ty.get());
}
// We always create generic type parameters with an invalid depth.
// Semantic analysis fills in the depth when it processes the generic
// parameter list.
auto Param = new (Context) GenericTypeParamDecl(CurDeclContext, Name, NameLoc,
GenericTypeParamDecl::InvalidDepth,
GenericParams.size());
if (!Inherited.empty())
Param->setInherited(Context.AllocateCopy(Inherited));
GenericParams.push_back(Param);
// Attach attributes.
Param->getAttrs() = attributes;
// Add this parameter to the scope.
addToScope(Param);
// Parse the comma, if the list continues.
HasNextParam = consumeIf(tok::comma);
} while (HasNextParam);
return Result;
}
示例15: parseGenericWhereClause
/// parseGenericWhereClause - Parse a 'where' clause, which places additional
/// constraints on generic parameters or types based on them.
///
/// where-clause:
/// 'where' requirement (',' requirement) *
///
/// requirement:
/// conformance-requirement
/// same-type-requirement
///
/// conformance-requirement:
/// type-identifier ':' type-identifier
/// type-identifier ':' type-composition
///
/// same-type-requirement:
/// type-identifier '==' type
ParserStatus Parser::parseGenericWhereClause(
SourceLoc &WhereLoc,
SmallVectorImpl<RequirementRepr> &Requirements,
bool &FirstTypeInComplete) {
ParserStatus Status;
// Parse the 'where'.
WhereLoc = consumeToken(tok::kw_where);
FirstTypeInComplete = false;
do {
// Parse the leading type-identifier.
ParserResult<TypeRepr> FirstType = parseTypeIdentifier();
if (FirstType.isNull()) {
Status.setIsParseError();
if (FirstType.hasCodeCompletion()) {
Status.setHasCodeCompletion();
FirstTypeInComplete = true;
}
break;
}
if (Tok.is(tok::colon)) {
// A conformance-requirement.
SourceLoc ColonLoc = consumeToken();
// Parse the protocol or composition.
ParserResult<TypeRepr> Protocol = parseTypeIdentifierOrTypeComposition();
if (Protocol.isNull()) {
Status.setIsParseError();
if (Protocol.hasCodeCompletion())
Status.setHasCodeCompletion();
break;
}
// Add the requirement.
Requirements.push_back(RequirementRepr::getTypeConstraint(FirstType.get(),
ColonLoc, Protocol.get()));
} else if ((Tok.isAnyOperator() && Tok.getText() == "==") ||
Tok.is(tok::equal)) {
// A same-type-requirement
if (Tok.is(tok::equal)) {
diagnose(Tok, diag::requires_single_equal)
.fixItReplace(SourceRange(Tok.getLoc()), "==");
}
SourceLoc EqualLoc = consumeToken();
// Parse the second type.
ParserResult<TypeRepr> SecondType = parseType();
if (SecondType.isNull()) {
Status.setIsParseError();
if (SecondType.hasCodeCompletion())
Status.setHasCodeCompletion();
break;
}
// Add the requirement
Requirements.push_back(RequirementRepr::getSameType(FirstType.get(),
EqualLoc,
SecondType.get()));
} else {
diagnose(Tok, diag::expected_requirement_delim);
Status.setIsParseError();
break;
}
// If there's a comma, keep parsing the list.
} while (consumeIf(tok::comma));
return Status;
}