Skip to content
Permalink
Browse files

Initial implementation of short lambdas

  • Loading branch information
mortie committed Jan 23, 2020
1 parent 90f58ea commit e4726dc9d9d966978714fc3d85c6e9c335a38ab8
Showing with 28 additions and 0 deletions.
  1. +1 −0 clang/include/clang/Basic/TokenKinds.def
  2. +3 −0 clang/lib/Lex/Lexer.cpp
  3. +24 −0 clang/lib/Parse/ParseExprCXX.cpp
@@ -185,6 +185,7 @@ PUNCTUATOR(plusplus, "++")
PUNCTUATOR(plusequal, "+=")
PUNCTUATOR(minus, "-")
PUNCTUATOR(arrow, "->")
PUNCTUATOR(fatarrow, "=>")
PUNCTUATOR(minusminus, "--")
PUNCTUATOR(minusequal, "-=")
PUNCTUATOR(tilde, "~")
@@ -3825,6 +3825,9 @@ bool Lexer::LexTokenInternal(Token &Result, bool TokAtPhysicalStartOfLine) {

Kind = tok::equalequal;
CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
} else if (Char == '>') {
Kind = tok::fatarrow;
CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
} else {
Kind = tok::equal;
}
@@ -1484,6 +1484,30 @@ ExprResult Parser::ParseLambdaExpressionAfterIntroducer(

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

// If this is an arrow lambda, we just need to parse an expression.
// We parse the expression, then put that expression in a return statement,
// and use that return statement as our body.
if (Tok.is(tok::fatarrow)) {
SourceLocation ReturnLoc(ConsumeToken());

ExprResult Expr(ParseExpression());
if (Expr.isInvalid()) {
Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
return ExprError();
}

StmtResult Stmt = Actions.ActOnReturnStmt(ReturnLoc, Expr.get(), getCurScope());

BodyScope.Exit();
TemplateParamScope.Exit();

if (!Stmt.isInvalid() && !TrailingReturnType.isInvalid())
return Actions.ActOnLambdaExpr(LambdaBeginLoc, Stmt.get(), getCurScope());

Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope());
return ExprError();
}

// Parse compound-statement.
if (!Tok.is(tok::l_brace)) {
Diag(Tok, diag::err_expected_lambda_body);

0 comments on commit e4726dc

Please sign in to comment.
You can’t perform that action at this time.