Fix constructor call on declaration

This commit is contained in:
drmortalwombat 2024-09-20 18:10:01 +02:00
parent a93b495ac5
commit 84a0a9660f
6 changed files with 107 additions and 8 deletions

View File

@ -919,13 +919,19 @@ bool Compiler::GenerateCode(void)
if (!dec->mLinkerObject) if (!dec->mLinkerObject)
mInterCodeGenerator->TranslateProcedure(mInterCodeModule, dec->mValue, dec); mInterCodeGenerator->TranslateProcedure(mInterCodeModule, dec->mValue, dec);
} }
else }
for (int i = 0; i < mCompilationUnits->mReferenced.Size(); i++)
{
Declaration* dec = mCompilationUnits->mReferenced[i];
if (dec->mType != DT_CONST_FUNCTION)
{ {
if (!dec->mLinkerObject) if (!dec->mLinkerObject)
mInterCodeGenerator->InitGlobalVariable(mInterCodeModule, dec); mInterCodeGenerator->InitGlobalVariable(mInterCodeModule, dec);
} }
} }
mInterCodeGenerator->CompleteMainInit();
if (mErrors->mErrorCount != 0) if (mErrors->mErrorCount != 0)
return false; return false;

View File

@ -5839,9 +5839,7 @@ InterCodeProcedure* InterCodeGenerator::TranslateProcedure(InterCodeModule * mod
if (!strcmp(proc->mIdent->mString, "main")) if (!strcmp(proc->mIdent->mString, "main"))
{ {
InterInstruction* ins = new InterInstruction(proc->mLocation, IC_JUMP); mMainStartupBlock = entryBlock;
mMainInitBlock->Append(ins);
mMainInitBlock->Close(entryBlock, nullptr);
} }
} }
else else
@ -5851,7 +5849,7 @@ InterCodeProcedure* InterCodeGenerator::TranslateProcedure(InterCodeModule * mod
exitBlock->Append(ins); exitBlock->Append(ins);
exitBlock->Close(nullptr, nullptr); exitBlock->Close(nullptr, nullptr);
if (mErrors->mErrorCount == 0) if (mErrors->mErrorCount == 0 && proc != mMainInitProc)
{ {
if (mCompilerOptions & COPT_VERBOSE2) if (mCompilerOptions & COPT_VERBOSE2)
printf("Optimize intermediate code <%s>\n", proc->mIdent->mString); printf("Optimize intermediate code <%s>\n", proc->mIdent->mString);
@ -5863,3 +5861,18 @@ InterCodeProcedure* InterCodeGenerator::TranslateProcedure(InterCodeModule * mod
return proc; return proc;
} }
void InterCodeGenerator::CompleteMainInit(void)
{
if (mErrors->mErrorCount == 0)
{
InterInstruction* ins = new InterInstruction(mMainInitProc->mLocation, IC_JUMP);
mMainInitBlock->Append(ins);
mMainInitBlock->Close(mMainStartupBlock, nullptr);
if (mCompilerOptions & COPT_VERBOSE2)
printf("Optimize intermediate code <%s>\n", mMainInitProc->mIdent->mString);
mMainInitProc->Close();
}
}

View File

@ -49,6 +49,8 @@ public:
void InitGlobalVariable(InterCodeModule* mod, Declaration* dec); void InitGlobalVariable(InterCodeModule* mod, Declaration* dec);
void InitLocalVariable(InterCodeProcedure* proc, Declaration* dec, int index); void InitLocalVariable(InterCodeProcedure* proc, Declaration* dec, int index);
void InitParameter(InterCodeProcedure* proc, Declaration* dec, int index); void InitParameter(InterCodeProcedure* proc, Declaration* dec, int index);
void CompleteMainInit(void);
protected: protected:
Errors* mErrors; Errors* mErrors;
@ -85,7 +87,7 @@ protected:
}; };
InterCodeProcedure* mMainInitProc; InterCodeProcedure* mMainInitProc;
InterCodeBasicBlock* mMainInitBlock; InterCodeBasicBlock* mMainInitBlock, *mMainStartupBlock;
Location MapLocation(Expression * exp, InlineMapper* inlineMapper); Location MapLocation(Expression * exp, InlineMapper* inlineMapper);

View File

@ -1312,10 +1312,19 @@ Declaration* Parser::ParsePostfixDeclaration(void)
ndec->mBase = dec; ndec->mBase = dec;
dec = ndec; dec = ndec;
} }
else if (mScanner->mToken == TK_OPEN_PARENTHESIS) else if (ConsumeTokenIf(TK_OPEN_PARENTHESIS))
{ {
if (mScanner->mToken == TK_CLOSE_PARENTHESIS || mScanner->mToken == TK_ELLIPSIS || IsTypeToken())
{
mScanner->UngetToken(TK_OPEN_PARENTHESIS);
dec = ParseFunctionDeclaration(dec); dec = ParseFunctionDeclaration(dec);
} }
else
{
mScanner->UngetToken(TK_OPEN_PARENTHESIS);
return dec;
}
}
else else
return dec; return dec;
} }
@ -11878,6 +11887,67 @@ bool Parser::ConsumeIdentIf(const char* ident)
return false; return false;
} }
bool Parser::IsTypeToken(void)
{
switch (mScanner->mToken)
{
case TK_INT:
case TK_SHORT:
case TK_LONG:
case TK_FLOAT:
case TK_CHAR:
case TK_BOOL:
case TK_VOID:
case TK_UNSIGNED:
case TK_SIGNED:
case TK_CONST:
case TK_VOLATILE:
case TK_STRUCT:
case TK_CLASS:
case TK_UNION:
case TK_ENUM:
case TK_TYPEDEF:
case TK_STATIC:
case TK_AUTO:
case TK_STRIPED:
return true;
case TK_IDENT:
{
const Ident* pident = mScanner->mTokenIdent;
if (mTemplateScope && mTemplateScope->Lookup(pident))
return true;
Declaration* dec = nullptr;
if (mThisPointer && !mScope->Lookup(pident, SLEVEL_FUNCTION))
{
int offset;
uint64 flags;
if (mThisPointer->mType == DT_ARGUMENT)
dec = MemberLookup(mThisPointer->mBase->mBase, mScanner->mTokenIdent, offset, flags);
else if (mThisPointer->mType == DT_TYPE_POINTER)
dec = MemberLookup(mThisPointer->mBase, mScanner->mTokenIdent, offset, flags);
}
if (!dec)
dec = mScope->Lookup(pident);
if (!dec)
return true;
if (dec->mType <= DT_TYPE_AUTO)
return true;
} break;
}
return false;
}
bool Parser::IsIntegerToken(void) bool Parser::IsIntegerToken(void)
{ {
return (mScanner->mToken == TK_INTEGER || mScanner->mToken == TK_INTEGERL || mScanner->mToken == TK_INTEGERU || mScanner->mToken == TK_INTEGERUL); return (mScanner->mToken == TK_INTEGER || mScanner->mToken == TK_INTEGERL || mScanner->mToken == TK_INTEGERU || mScanner->mToken == TK_INTEGERUL);

View File

@ -31,6 +31,7 @@ protected:
bool ConsumeTokenIf(Token token); bool ConsumeTokenIf(Token token);
bool ConsumeIdentIf(const char* ident); bool ConsumeIdentIf(const char* ident);
bool IsIntegerToken(void); bool IsIntegerToken(void);
bool IsTypeToken(void);
uint8 mCharMap[256]; uint8 mCharMap[256];
int mUnrollLoop; int mUnrollLoop;

View File

@ -486,6 +486,13 @@ void Scanner::UngetToken(Token token)
void Scanner::NextToken(void) void Scanner::NextToken(void)
{ {
if (mUngetToken)
{
mToken = mUngetToken;
mUngetToken = TK_NONE;
return;
}
if (mReplay) if (mReplay)
{ {
mLocation = mReplay->mLocation; mLocation = mReplay->mLocation;