From fa60b00167e4d5989fc69ac182cce5b155b28fe5 Mon Sep 17 00:00:00 2001 From: "Vladimir Gladkov (office-win10)" Date: Tue, 25 Jun 2024 14:42:10 +0800 Subject: [PATCH] [jnc_ct] split addOnEventHandler into createOnEventHandler and addOnEventBindings this way, if argument signature check fails, we still can have code assist inside onevent handlers --- .../jnc_ct_ControlFlowMgr.h | 14 +++++---- .../jnc_ct_ControlFlowMgr_Reactor.cpp | 30 ++++++++++++------- .../jnc_ct_Module/jnc_ct_CodeAssistMgr.cpp | 6 ++-- src/jnc_ct/jnc_ct_Parser/jnc_ct_Parser.cpp | 18 +++++++++++ src/jnc_ct/jnc_ct_Parser/jnc_ct_Parser.llk | 8 +++++ src/jnc_ct/jnc_ct_Parser/jnc_ct_Stmt.llk | 10 +++---- src/jnc_ext/jnc_io_hid/jnc_io_HidDevice.h | 1 - 7 files changed, 63 insertions(+), 24 deletions(-) diff --git a/src/jnc_ct/jnc_ct_ControlFlowMgr/jnc_ct_ControlFlowMgr.h b/src/jnc_ct/jnc_ct_ControlFlowMgr/jnc_ct_ControlFlowMgr.h index 6978e63ba..6ecd0024e 100644 --- a/src/jnc_ct/jnc_ct_ControlFlowMgr/jnc_ct_ControlFlowMgr.h +++ b/src/jnc_ct/jnc_ct_ControlFlowMgr/jnc_ct_ControlFlowMgr.h @@ -243,12 +243,16 @@ class ControlFlowMgr { void finalizeReaction(size_t reactionIdx); + Function* + createOnEventHandler( + const lex::LineCol& pos, + const sl::Array& argArray + ); + bool - addOnEventHandler( - const sl::BoxList& valueList, - const sl::Array& argArray, - const PragmaConfig* pragmaConfig, - sl::List* tokenList + addOnEventBindings( + Function* handler, + const sl::BoxList& valueList ); // blocks diff --git a/src/jnc_ct/jnc_ct_ControlFlowMgr/jnc_ct_ControlFlowMgr_Reactor.cpp b/src/jnc_ct/jnc_ct_ControlFlowMgr/jnc_ct_ControlFlowMgr_Reactor.cpp index a71887d48..70e3906d3 100644 --- a/src/jnc_ct/jnc_ct_ControlFlowMgr/jnc_ct_ControlFlowMgr_Reactor.cpp +++ b/src/jnc_ct/jnc_ct_ControlFlowMgr/jnc_ct_ControlFlowMgr_Reactor.cpp @@ -102,34 +102,42 @@ ControlFlowMgr::leaveReactor() { return true; } -bool -ControlFlowMgr::addOnEventHandler( - const sl::BoxList& valueList, - const sl::Array& argArray, - const PragmaConfig* pragmaConfig, - sl::List* tokenList +Function* +ControlFlowMgr::createOnEventHandler( + const lex::LineCol& pos, + const sl::Array& argArray ) { ASSERT(m_reactorBody); - FunctionType* functionType = argArray.isEmpty() ? + FunctionType* handlerType = argArray.isEmpty() ? m_module->m_typeMgr.getFunctionType(argArray) : m_module->m_typeMgr.createUserFunctionType(argArray); - Function* handler = m_reactorBody->m_reactorType->createUnnamedMethod(FunctionKind_Internal, functionType); + Function* handler = m_reactorBody->m_reactorType->createUnnamedMethod(FunctionKind_Internal, handlerType); handler->m_parentUnit = m_module->m_unitMgr.getCurrentUnit(); handler->m_parentNamespace = m_module->m_namespaceMgr.getCurrentNamespace(); + handler->m_pos = pos; handler->m_flags |= ModuleItemFlag_User; - handler->setBody(pragmaConfig, tokenList); size_t onEventIdx = m_reactorBody->m_reactionBlockArray.getCount(); m_reactorBody->m_reactorType->addOnEventHandler(onEventIdx, handler); m_reactorBody->m_reactionBlockArray.append(NULL); // onevent occupies one reaction slot + return handler; +} +bool +ControlFlowMgr::addOnEventBindings( + Function* handler, + const sl::BoxList& valueList +) { + ASSERT(!m_reactorBody->m_reactionBlockArray.isEmpty()); + size_t onEventIdx = m_reactorBody->m_reactionBlockArray.getCount() - 1; Function* addBindingFunc = getReactorMethod(m_module, ReactorMethod_AddOnEventBinding); Value thisValue = m_module->m_functionMgr.getThisValue(); Value onEventIdxValue(onEventIdx, m_module->m_typeMgr.getPrimitiveType(TypeKind_SizeT)); - sl::String argSignature = functionType->getArgSignature(); + FunctionType* handlerType = handler->getType()->getShortType(); + sl::String argSignature = handlerType->getArgSignature(); sl::BoxList::ConstIterator it = valueList.getHead(); for (; it; it++) { Type* type = it->getType(); @@ -144,7 +152,7 @@ ControlFlowMgr::addOnEventHandler( err::setFormatStringError( "onevent argument signature mismatch: '%s' vs '%s'", bindingSiteType->getTypeStringSuffix().sz(), - functionType->getTypeStringSuffix().sz() + handlerType->getTypeStringSuffix().sz() ); return false; diff --git a/src/jnc_ct/jnc_ct_Module/jnc_ct_CodeAssistMgr.cpp b/src/jnc_ct/jnc_ct_Module/jnc_ct_CodeAssistMgr.cpp index eeb712031..252f7da4c 100644 --- a/src/jnc_ct/jnc_ct_Module/jnc_ct_CodeAssistMgr.cpp +++ b/src/jnc_ct/jnc_ct_Module/jnc_ct_CodeAssistMgr.cpp @@ -106,11 +106,13 @@ CodeAssistMgr::generateCodeAssistImpl(ModuleItem* item) { ((AsyncLauncherFunction*)item)->generateCodeAssist(); else ((Function*)item)->compile(); + + generateCodeAssist(); // process nested containers break; case ModuleItemKind_Namespace: ((GlobalNamespace*)item)->ensureNamespaceReady(); - generateCodeAssist(); + generateCodeAssist(); // process nested containers break; case ModuleItemKind_Type: @@ -121,7 +123,7 @@ CodeAssistMgr::generateCodeAssistImpl(ModuleItem* item) { } ((NamedType*)item)->ensureNamespaceReady(); - generateCodeAssist(); + generateCodeAssist(); // process nested containers break; } diff --git a/src/jnc_ct/jnc_ct_Parser/jnc_ct_Parser.cpp b/src/jnc_ct/jnc_ct_Parser/jnc_ct_Parser.cpp index daeb2a92c..2ddf76fc4 100644 --- a/src/jnc_ct/jnc_ct_Parser/jnc_ct_Parser.cpp +++ b/src/jnc_ct/jnc_ct_Parser/jnc_ct_Parser.cpp @@ -3380,6 +3380,24 @@ Parser::addScopeAnchorToken( stmt->m_scopeAnchorToken = token; } +bool +Parser::finalizeOnEventStmt( + const lex::LineCol& pos, + DeclFunctionSuffix* functionSuffix, + const sl::BoxList& valueList, + sl::List* bodyTokenList +) { + Function* handler = m_module->m_controlFlowMgr.createOnEventHandler(pos, functionSuffix->getArgArray()); + bool result = setBody(handler, bodyTokenList); + ASSERT(result); + + result = m_module->m_controlFlowMgr.addOnEventBindings(handler, valueList); + if (!result) + handler->ensureSrcPosError(); + + return result; +} + // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . void diff --git a/src/jnc_ct/jnc_ct_Parser/jnc_ct_Parser.llk b/src/jnc_ct/jnc_ct_Parser/jnc_ct_Parser.llk index 2f06c5525..e0e15f771 100644 --- a/src/jnc_ct/jnc_ct_Parser/jnc_ct_Parser.llk +++ b/src/jnc_ct/jnc_ct_Parser/jnc_ct_Parser.llk @@ -674,6 +674,14 @@ protected: BasicBlock* assertCondition(sl::List* tokenList); + bool + finalizeOnEventStmt( + const lex::LineCol& pos, + DeclFunctionSuffix* functionSuffix, + const sl::BoxList& valueList, + sl::List* bodyTokenList + ); + bool finalizeAssertStmt( const lex::LineCol& pos, diff --git a/src/jnc_ct/jnc_ct_Parser/jnc_ct_Stmt.llk b/src/jnc_ct/jnc_ct_Parser/jnc_ct_Stmt.llk index 089c09d89..50740873f 100644 --- a/src/jnc_ct/jnc_ct_Parser/jnc_ct_Stmt.llk +++ b/src/jnc_ct/jnc_ct_Parser/jnc_ct_Stmt.llk @@ -475,17 +475,17 @@ onevent_stmt postDeclarator(&$declarator); } : TokenKind_OnEvent - event_name + event_name $n { postDeclaratorName(&$declarator); } function_suffix<&$declarator> declaration_body_pass1 $b { - return m_module->m_controlFlowMgr.addOnEventHandler( - $2.m_valueList, - $declarator.getFunctionSuffix()->getArgArray(), - getPragmaConfigSnapshot(), + return finalizeOnEventStmt( + $1.m_pos, + $declarator.getFunctionSuffix(), + $n.m_valueList, &$b.m_tokenList ); } diff --git a/src/jnc_ext/jnc_io_hid/jnc_io_HidDevice.h b/src/jnc_ext/jnc_io_hid/jnc_io_HidDevice.h index aba36800b..c5c59391b 100644 --- a/src/jnc_ext/jnc_io_hid/jnc_io_HidDevice.h +++ b/src/jnc_ext/jnc_io_hid/jnc_io_HidDevice.h @@ -161,7 +161,6 @@ class HidDevice: return m_device.sendFeatureReport(ptr.m_p, size); } - handle_t JNC_CDECL wait(