Fix finding in-class methods when searching by ID

dev-new-models
melex750 2016-09-17 07:59:34 -04:00
parent 4a14a44f3f
commit 3debfb9182
4 changed files with 32 additions and 13 deletions

View File

@ -639,7 +639,6 @@ bool CBotClass::CompileDefItem(CBotToken* &p, CBotCStack* pStack, bool bSecond)
{ {
// return a method precompiled in pass 1 // return a method precompiled in pass 1
CBotFunction* pf = m_pMethod; CBotFunction* pf = m_pMethod;
CBotFunction* prev = nullptr;
CBotToken* ppp = p; CBotToken* ppp = p;
CBotCStack* pStk = pStack->TokenStack(nullptr, true); CBotCStack* pStk = pStack->TokenStack(nullptr, true);
CBotDefParam* params = CBotDefParam::Compile(p, pStk ); CBotDefParam* params = CBotDefParam::Compile(p, pStk );
@ -648,7 +647,6 @@ bool CBotClass::CompileDefItem(CBotToken* &p, CBotCStack* pStack, bool bSecond)
while ( pf != nullptr ) // search by name and parameters while ( pf != nullptr ) // search by name and parameters
{ {
if (pf->GetName() == pp && pf->CheckParam( params )) break; if (pf->GetName() == pp && pf->CheckParam( params )) break;
prev = pf;
pf = pf->Next(); pf = pf->Next();
} }
@ -693,18 +691,12 @@ bool CBotClass::CompileDefItem(CBotToken* &p, CBotCStack* pStack, bool bSecond)
// compiles a method // compiles a method
p = pBase; p = pBase;
CBotFunction* f = CBotFunction* f =
CBotFunction::Compile(p, pile, nullptr/*, false*/); CBotFunction::Compile(p, pile, pf/*, false*/);
if ( f != nullptr ) if ( f != nullptr )
{ {
f->m_pProg = pStack->GetProgram(); f->m_pProg = pStack->GetProgram();
f->m_bSynchro = bSynchro; f->m_bSynchro = bSynchro;
// replaces the element in the chain
f->m_next = pf->m_next;
pf->m_next = nullptr;
delete pf;
if (prev == nullptr) m_pMethod = f;
else prev->m_next = f;
} }
pStack->Return(nullptr, pile); pStack->Return(nullptr, pile);
} }

View File

@ -224,10 +224,6 @@ CBotFunction* CBotFunction::Compile(CBotToken* &p, CBotCStack* pStack, CBotFunct
func->m_closeblk = (p != nullptr && p->GetPrev() != nullptr) ? *(p->GetPrev()) : CBotToken(); func->m_closeblk = (p != nullptr && p->GetPrev() != nullptr) ? *(p->GetPrev()) : CBotToken();
if ( pStk->IsOk() ) if ( pStk->IsOk() )
{ {
if ( func->m_bPublic ) // public function, return known for all
{
CBotFunction::AddPublic(func);
}
return pStack->ReturnFunc(func, pStk); return pStack->ReturnFunc(func, pStk);
} }
} }

View File

@ -132,6 +132,7 @@ bool CBotProgram::Compile(const std::string& program, std::vector<std::string>&
m_bCompileClass = false; m_bCompileClass = false;
CBotFunction::Compile(p, pStack.get(), next); CBotFunction::Compile(p, pStack.get(), next);
if (next->IsExtern()) functions.push_back(next->GetName()/* + next->GetParams()*/); if (next->IsExtern()) functions.push_back(next->GetName()/* + next->GetParams()*/);
if (next->IsPublic()) CBotFunction::AddPublic(next);
next->m_pProg = this; // keeps pointers to the module next->m_pProg = this; // keeps pointers to the module
next = next->Next(); next = next->Next();
} }

View File

@ -1874,3 +1874,33 @@ TEST_F(CBotUT, AmbiguousCallWithNumbers)
"}\n" "}\n"
); );
} }
TEST_F(CBotUT, ClassMethodWithPublicKeyword)
{
auto publicProgram = ExecuteTest(
"public class TestClass {\n"
" public int Test() { return 1; }\n"
"}\n"
);
ExecuteTest(
"int Test() { return 2; }\n"
"\n"
"extern void DontCallMethodInTestClass()\n"
"{\n"
" ASSERT(2 == Test());\n"
"}\n"
);
ExecuteTest(
"int Test() { return 2; }\n"
"\n"
"public class OtherClass {}\n"
"\n"
"extern void OtherClass::TestCallWithThis()\n"
"{\n"
" this.Test();\n"
"}\n",
CBotErrUndefCall
);
}