Skip to content

Commit e4726dc

Browse files
committed
Initial implementation of short lambdas
1 parent 90f58ea commit e4726dc

3 files changed

Lines changed: 28 additions & 0 deletions

File tree

clang/include/clang/Basic/TokenKinds.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ PUNCTUATOR(plusplus, "++")
185185
PUNCTUATOR(plusequal, "+=")
186186
PUNCTUATOR(minus, "-")
187187
PUNCTUATOR(arrow, "->")
188+
PUNCTUATOR(fatarrow, "=>")
188189
PUNCTUATOR(minusminus, "--")
189190
PUNCTUATOR(minusequal, "-=")
190191
PUNCTUATOR(tilde, "~")

clang/lib/Lex/Lexer.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3825,6 +3825,9 @@ bool Lexer::LexTokenInternal(Token &Result, bool TokAtPhysicalStartOfLine) {
38253825

38263826
Kind = tok::equalequal;
38273827
CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
3828+
} else if (Char == '>') {
3829+
Kind = tok::fatarrow;
3830+
CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
38283831
} else {
38293832
Kind = tok::equal;
38303833
}

clang/lib/Parse/ParseExprCXX.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,6 +1484,30 @@ ExprResult Parser::ParseLambdaExpressionAfterIntroducer(
14841484

14851485
Actions.ActOnStartOfLambdaDefinition(Intro, D, getCurScope());
14861486

1487+
// If this is an arrow lambda, we just need to parse an expression.
1488+
// We parse the expression, then put that expression in a return statement,
1489+
// and use that return statement as our body.
1490+
if (Tok.is(tok::fatarrow)) {
1491+
SourceLocation ReturnLoc(ConsumeToken());
1492+
1493+
ExprResult Expr(ParseExpression());
1494+
if (Expr.isInvalid()) {
1495+
Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
1496+
return ExprError();
1497+
}
1498+
1499+
StmtResult Stmt = Actions.ActOnReturnStmt(ReturnLoc, Expr.get(), getCurScope());
1500+
1501+
BodyScope.Exit();
1502+
TemplateParamScope.Exit();
1503+
1504+
if (!Stmt.isInvalid() && !TrailingReturnType.isInvalid())
1505+
return Actions.ActOnLambdaExpr(LambdaBeginLoc, Stmt.get(), getCurScope());
1506+
1507+
Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
1508+
return ExprError();
1509+
}
1510+
14871511
// Parse compound-statement.
14881512
if (!Tok.is(tok::l_brace)) {
14891513
Diag(Tok, diag::err_expected_lambda_body);

0 commit comments

Comments
 (0)