From 57c3f938221c427b78da7087f8a832ba4a271a7c Mon Sep 17 00:00:00 2001 From: Julius Ikkala Date: Fri, 23 May 2025 22:27:37 +0300 Subject: Implement throw & catch statements (#6916) * Implement throw statement It already existed in the IR, so only parsing, checking and lowering was missing. * Initial catch implementation Likely very broken. * Error out when catch() isn't last in scope * Prevent accessing variables from scope preceding catch As those may actually not be available at that point. * Add IError and use it in Result type lowering * Add diagnostic tests * Allow caught throws in non-throw functions * Fix catch propagating between functions & SPIR-V merge issue * Add test for non-trivial error types * Fix MSVC build * Fix invalid value type from Result lowering * Also lower error handling in templates * Lower result types only after specialization * Attempt to disambiguate error enums by witness table * Revert matching by witness, types should be distinct too * Don't assert valueField when getting Result's error value It may not exist if the function returns void, but getting the error value is still legitimate. * Update tests for new error numbers & get rid of expected.txt * Change catch lowering to resemble breaking a loop ... To make SPIR-V happy. * Fix dead catch blocks and invalid cached dominator tree * More SPIR-V adjustment * Lower catch as two nested loops * Add defer interaction test and revert broken defer changes * Fix enum type when throwing literals * Cleanup and bikeshedding * Document error handling mechanism * Fix table of contents * Use boolean tag in Result * Use anyValue storage for Result * Remove IError * Fix formatting * Eradicate success values from docs and tests * Use parseModernParamDecl for catch parameter * Implement do-catch syntax * Implement catch-all * Fix formatting * Fix marshalling native calls that throw --------- Co-authored-by: Yong He --- source/slang/slang-parser.cpp | 82 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 76 insertions(+), 6 deletions(-) (limited to 'source/slang/slang-parser.cpp') diff --git a/source/slang/slang-parser.cpp b/source/slang/slang-parser.cpp index b2a006adc..f968f9fe1 100644 --- a/source/slang/slang-parser.cpp +++ b/source/slang/slang-parser.cpp @@ -211,11 +211,14 @@ public: Stmt* parseIfLetStatement(); ForStmt* ParseForStatement(); WhileStmt* ParseWhileStatement(); - DoWhileStmt* ParseDoWhileStatement(); + DoWhileStmt* ParseDoWhileStatement(Stmt* body); + CatchStmt* ParseDoCatchStatement(Stmt* body); + Stmt* ParseDoStatement(); BreakStmt* ParseBreakStatement(); ContinueStmt* ParseContinueStatement(); ReturnStmt* ParseReturnStatement(); DeferStmt* ParseDeferStatement(); + ThrowStmt* ParseThrowStatement(); ExpressionStmt* ParseExpressionStatement(); Expr* ParseExpression(Precedence level = Precedence::Comma); @@ -5741,7 +5744,7 @@ Stmt* Parser::ParseStatement(Stmt* parentStmt) else if (LookAheadToken("while")) statement = ParseWhileStatement(); else if (LookAheadToken("do")) - statement = ParseDoWhileStatement(); + statement = ParseDoStatement(); else if (LookAheadToken("break")) statement = ParseBreakStatement(); else if (LookAheadToken("continue")) @@ -5781,6 +5784,10 @@ Stmt* Parser::ParseStatement(Stmt* parentStmt) { statement = ParseExpressionStatement(); } + else if (LookAheadToken("throw")) + { + statement = ParseThrowStatement(); + } else if (LookAheadToken(TokenType::Identifier) || LookAheadToken(TokenType::Scope)) { if (LookAheadToken(TokenType::Identifier) && LookAheadToken(TokenType::Colon, 1)) @@ -5941,7 +5948,6 @@ Stmt* Parser::parseBlockStatement() Stmt* body = nullptr; - if (!tokenReader.isAtEnd()) { FillPosition(blockStatement); @@ -6259,12 +6265,11 @@ WhileStmt* Parser::ParseWhileStatement() return whileStatement; } -DoWhileStmt* Parser::ParseDoWhileStatement() +DoWhileStmt* Parser::ParseDoWhileStatement(Stmt* body) { DoWhileStmt* doWhileStatement = astBuilder->create(); FillPosition(doWhileStatement); - ReadToken("do"); - doWhileStatement->statement = ParseStatement(); + doWhileStatement->statement = body; ReadToken("while"); ReadToken(TokenType::LParent); doWhileStatement->predicate = ParseExpression(); @@ -6273,6 +6278,62 @@ DoWhileStmt* Parser::ParseDoWhileStatement() return doWhileStatement; } +CatchStmt* Parser::ParseDoCatchStatement(Stmt* body) +{ + for (;;) + { + ScopeDecl* scopeDecl = astBuilder->create(); + pushScopeAndSetParent(scopeDecl); + + CatchStmt* catchStatement = astBuilder->create(); + FillPosition(catchStatement); + ReadToken("catch"); + + // Optional error parameter. If not given, the catch catches all error + // types. + if (AdvanceIf(this, TokenType::LParent)) + { + ParamDecl* errorVar = parseModernParamDecl(this); + catchStatement->errorVar = errorVar; + AddMember(scopeDecl, errorVar); + ReadToken(TokenType::RParent); + } + + catchStatement->tryBody = body; + catchStatement->handleBody = ParseStatement(); + + PopScope(); + + if (!LookAheadToken("catch")) + return catchStatement; + + // Use this catch as the body for the next one, if multiple are chained. + body = catchStatement; + } +} + +Stmt* Parser::ParseDoStatement() +{ + SourceLoc position = tokenReader.peekLoc(); + ReadToken("do"); + Stmt* statement = ParseStatement(); + if (LookAheadToken("while")) + { + Stmt* whileStatement = ParseDoWhileStatement(statement); + whileStatement->loc = position; + return whileStatement; + } + else if (LookAheadToken("catch")) + { + return ParseDoCatchStatement(statement); + } + else + { + Unexpected(this, "while' or 'catch"); + return statement; + } +} + BreakStmt* Parser::ParseBreakStatement() { BreakStmt* breakStatement = astBuilder->create(); @@ -6315,6 +6376,15 @@ DeferStmt* Parser::ParseDeferStatement() return deferStatement; } +ThrowStmt* Parser::ParseThrowStatement() +{ + ThrowStmt* throwStatement = astBuilder->create(); + FillPosition(throwStatement); + ReadToken("throw"); + throwStatement->expression = ParseExpression(); + return throwStatement; +} + ExpressionStmt* Parser::ParseExpressionStatement() { ExpressionStmt* statement = astBuilder->create(); -- cgit v1.2.3