Skip to content

Commit

Permalink
[jnc_ct] split addOnEventHandler into createOnEventHandler and addOnE…
Browse files Browse the repository at this point in the history
…ventBindings

this way, if argument signature check fails, we still can have code assist inside onevent handlers
  • Loading branch information
vovkos committed Jun 26, 2024
1 parent 025ffa2 commit fa60b00
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 24 deletions.
14 changes: 9 additions & 5 deletions src/jnc_ct/jnc_ct_ControlFlowMgr/jnc_ct_ControlFlowMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,16 @@ class ControlFlowMgr {
void
finalizeReaction(size_t reactionIdx);

Function*
createOnEventHandler(
const lex::LineCol& pos,
const sl::Array<FunctionArg*>& argArray
);

bool
addOnEventHandler(
const sl::BoxList<Value>& valueList,
const sl::Array<FunctionArg*>& argArray,
const PragmaConfig* pragmaConfig,
sl::List<Token>* tokenList
addOnEventBindings(
Function* handler,
const sl::BoxList<Value>& valueList
);

// blocks
Expand Down
30 changes: 19 additions & 11 deletions src/jnc_ct/jnc_ct_ControlFlowMgr/jnc_ct_ControlFlowMgr_Reactor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,34 +102,42 @@ ControlFlowMgr::leaveReactor() {
return true;
}

bool
ControlFlowMgr::addOnEventHandler(
const sl::BoxList<Value>& valueList,
const sl::Array<FunctionArg*>& argArray,
const PragmaConfig* pragmaConfig,
sl::List<Token>* tokenList
Function*
ControlFlowMgr::createOnEventHandler(
const lex::LineCol& pos,
const sl::Array<FunctionArg*>& 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<Value>& 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<Value>::ConstIterator it = valueList.getHead();
for (; it; it++) {
Type* type = it->getType();
Expand All @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions src/jnc_ct/jnc_ct_Module/jnc_ct_CodeAssistMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -121,7 +123,7 @@ CodeAssistMgr::generateCodeAssistImpl(ModuleItem* item) {
}

((NamedType*)item)->ensureNamespaceReady();
generateCodeAssist();
generateCodeAssist(); // process nested containers
break;
}

Expand Down
18 changes: 18 additions & 0 deletions src/jnc_ct/jnc_ct_Parser/jnc_ct_Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3380,6 +3380,24 @@ Parser::addScopeAnchorToken(
stmt->m_scopeAnchorToken = token;
}

bool
Parser::finalizeOnEventStmt(
const lex::LineCol& pos,
DeclFunctionSuffix* functionSuffix,
const sl::BoxList<Value>& valueList,
sl::List<Token>* 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
Expand Down
8 changes: 8 additions & 0 deletions src/jnc_ct/jnc_ct_Parser/jnc_ct_Parser.llk
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,14 @@ protected:
BasicBlock*
assertCondition(sl::List<Token>* tokenList);

bool
finalizeOnEventStmt(
const lex::LineCol& pos,
DeclFunctionSuffix* functionSuffix,
const sl::BoxList<Value>& valueList,
sl::List<Token>* bodyTokenList
);

bool
finalizeAssertStmt(
const lex::LineCol& pos,
Expand Down
10 changes: 5 additions & 5 deletions src/jnc_ct/jnc_ct_Parser/jnc_ct_Stmt.llk
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
Expand Down
1 change: 0 additions & 1 deletion src/jnc_ext/jnc_io_hid/jnc_io_HidDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ class HidDevice:
return m_device.sendFeatureReport(ptr.m_p, size);
}


handle_t
JNC_CDECL
wait(
Expand Down

0 comments on commit fa60b00

Please sign in to comment.