Add simple return type deduction

This commit is contained in:
drmortalwombat 2023-09-11 17:49:04 +02:00
parent ab49281b0d
commit c926456560
10 changed files with 41 additions and 14 deletions

View File

@ -71,7 +71,7 @@ inline BitVector::BitVector(int size, unsigned char * data)
else else
bits = nullptr; bits = nullptr;
if (size) if (size > 0)
{ {
memcpy(bits, data, (size + 7) / 8); memcpy(bits, data, (size + 7) / 8);
} }
@ -93,7 +93,7 @@ inline BitVector::BitVector(int size, bool set)
else else
bits = NULL; bits = NULL;
if (size) if (size > 0)
{ {
if (set) if (set)
{ {

View File

@ -1067,7 +1067,7 @@ bool Compiler::BuildLZO(const char* targetPath)
CompilationUnit* cunit; CompilationUnit* cunit;
char data[65536]; char * data = new char[65536];
int n = 0; int n = 0;
while (mErrors->mErrorCount == 0 && (cunit = mCompilationUnits->PendingUnit())) while (mErrors->mErrorCount == 0 && (cunit = mCompilationUnits->PendingUnit()))
@ -1110,14 +1110,20 @@ bool Compiler::BuildLZO(const char* targetPath)
{ {
int done = fwrite(data, 1, n, file); int done = fwrite(data, 1, n, file);
fclose(file); fclose(file);
delete[] data;
return done == n; return done == n;
} }
else else
{
delete[] data;
return false; return false;
} }
}
else else
{
delete[] data;
return false; return false;
}
} }
bool Compiler::WriteOutputFile(const char* targetPath, DiskImage * d64) bool Compiler::WriteOutputFile(const char* targetPath, DiskImage * d64)

View File

@ -882,7 +882,7 @@ Expression* Expression::ConstantFold(Errors * errors, LinkerSection * dataSectio
} }
Declaration::Declaration(const Location& loc, DecType type) Declaration::Declaration(const Location& loc, DecType type)
: mLocation(loc), mEndLocation(loc), mType(type), mScope(nullptr), mData(nullptr), mIdent(nullptr), mQualIdent(nullptr), : mLocation(loc), mEndLocation(loc), mType(type), mScope(nullptr), mData(nullptr), mIdent(nullptr), mQualIdent(nullptr), mMangleIdent(nullptr),
mSize(0), mOffset(0), mFlags(0), mComplexity(0), mLocalSize(0), mSize(0), mOffset(0), mFlags(0), mComplexity(0), mLocalSize(0),
mBase(nullptr), mParams(nullptr), mValue(nullptr), mNext(nullptr), mPrev(nullptr), mBase(nullptr), mParams(nullptr), mValue(nullptr), mNext(nullptr), mPrev(nullptr),
mConst(nullptr), mMutable(nullptr), mConst(nullptr), mMutable(nullptr),

View File

@ -250,7 +250,7 @@ bool DiskImage::WriteFile(const char* fname, bool compressed)
if (OpenFile(dname)) if (OpenFile(dname))
{ {
uint8 buffer[65536], cbuffer[65536]; uint8 * buffer = new uint8[65536], * cbuffer = new uint8[65536];
int size = fread(buffer, 1, 65536, file); int size = fread(buffer, 1, 65536, file);
int csize = 0; int csize = 0;
@ -311,6 +311,9 @@ bool DiskImage::WriteFile(const char* fname, bool compressed)
else else
WriteBytes(buffer, size); WriteBytes(buffer, size);
CloseFile(); CloseFile();
delete[] buffer;
delete[] cbuffer;
} }
fclose(file); fclose(file);

View File

@ -245,7 +245,7 @@ class InterVariable
{ {
public: public:
bool mUsed, mAliased, mTemp; bool mUsed, mAliased, mTemp;
int mIndex, mSize, mOffset, mAddr, mTempIndex; int mIndex, mSize, mOffset, mTempIndex;
int mNumReferences; int mNumReferences;
const Ident * mIdent; const Ident * mIdent;
LinkerObject * mLinkerObject; LinkerObject * mLinkerObject;

View File

@ -22386,8 +22386,8 @@ bool NativeCodeBasicBlock::PatchCrossBlock16BitFlood(const NativeCodeBasicBlock*
mPatchExit = true; mPatchExit = true;
mExitRequiredRegs |= sreg; mExitRequiredRegs += sreg;
mExitRequiredRegs |= sreg + 1; mExitRequiredRegs += sreg + 1;
if (mTrueJump && mTrueJump->PatchCrossBlock16BitFlood(block, sreg, dreg, 0)) if (mTrueJump && mTrueJump->PatchCrossBlock16BitFlood(block, sreg, dreg, 0))
changed = true; changed = true;

View File

@ -120,6 +120,8 @@ NumberSet& NumberSet::operator=(const NumberSet& set)
NumberSet& NumberSet::operator&=(const NumberSet& set) NumberSet& NumberSet::operator&=(const NumberSet& set)
{ {
assert(dwsize == set.dwsize);
int size = dwsize; int size = dwsize;
const uint32* sbits = set.bits; const uint32* sbits = set.bits;
uint32* dbits = bits; uint32* dbits = bits;
@ -132,6 +134,8 @@ NumberSet& NumberSet::operator&=(const NumberSet& set)
NumberSet& NumberSet::operator|=(const NumberSet& set) NumberSet& NumberSet::operator|=(const NumberSet& set)
{ {
assert(dwsize == set.dwsize);
int size = dwsize; int size = dwsize;
const uint32* sbits = set.bits; const uint32* sbits = set.bits;
uint32* dbits = bits; uint32* dbits = bits;
@ -144,6 +148,8 @@ NumberSet& NumberSet::operator|=(const NumberSet& set)
NumberSet& NumberSet::operator-=(const NumberSet& set) NumberSet& NumberSet::operator-=(const NumberSet& set)
{ {
assert(dwsize == set.dwsize);
int i; int i;
for (i = 0; i < dwsize; i++) for (i = 0; i < dwsize; i++)
@ -154,6 +160,8 @@ NumberSet& NumberSet::operator-=(const NumberSet& set)
bool NumberSet::operator<=(const NumberSet& set) const bool NumberSet::operator<=(const NumberSet& set) const
{ {
assert(dwsize == set.dwsize);
int i; int i;
for (i = 0; i < dwsize; i++) for (i = 0; i < dwsize; i++)

View File

@ -20,6 +20,7 @@ Parser::Parser(Errors* errors, Scanner* scanner, CompilationUnits* compilationUn
mCompilerOptionSP = 0; mCompilerOptionSP = 0;
mThisPointer = nullptr; mThisPointer = nullptr;
mFunction = nullptr; mFunction = nullptr;
mFunctionType = nullptr;
for (int i = 0; i < 256; i++) for (int i = 0; i < 256; i++)
mCharMap[i] = i; mCharMap[i] = i;
@ -7110,7 +7111,7 @@ Expression* Parser::ParseFunction(Declaration * dec)
if (dec->mFlags & DTF_FUNC_THIS) if (dec->mFlags & DTF_FUNC_THIS)
mThisPointer = dec->mParams; mThisPointer = dec->mParams;
mReturnType = dec->mBase; mFunctionType = dec;
DeclarationScope* oscope = mScope; DeclarationScope* oscope = mScope;
while (mScope->mLevel == SLEVEL_CLASS) while (mScope->mLevel == SLEVEL_CLASS)
@ -7508,10 +7509,16 @@ Expression* Parser::ParseStatement(void)
if (mScanner->mToken != TK_SEMICOLON) if (mScanner->mToken != TK_SEMICOLON)
{ {
exp->mLeft = ParseRExpression(); exp->mLeft = ParseRExpression();
if (mReturnType) if (mFunctionType && mFunctionType->mBase)
exp->mLeft = CoerceExpression(exp->mLeft, mReturnType); {
if (mFunctionType->mBase->mType == DT_TYPE_AUTO)
{
mFunctionType->mBase = exp->mLeft->mDecType;
}
exp->mLeft = CoerceExpression(exp->mLeft, mFunctionType->mBase);
}
exp->mLeft = CleanupExpression(exp->mLeft); exp->mLeft = CleanupExpression(exp->mLeft);
if (exp->mLeft->mType == EX_CONSTRUCT && mReturnType && mReturnType->mType == DT_TYPE_STRUCT) if (exp->mLeft->mType == EX_CONSTRUCT && mFunctionType && mFunctionType->mBase && mFunctionType->mBase->mType == DT_TYPE_STRUCT)
{ {
Expression* cexp = exp->mLeft->mLeft->mLeft; Expression* cexp = exp->mLeft->mLeft->mLeft;

View File

@ -15,7 +15,7 @@ public:
DeclarationScope * mGlobals, * mScope, * mTemplateScope; DeclarationScope * mGlobals, * mScope, * mTemplateScope;
int mLocalIndex; int mLocalIndex;
CompilationUnits * mCompilationUnits; CompilationUnits * mCompilationUnits;
Declaration * mThisPointer, * mReturnType, * mFunction; Declaration * mThisPointer, * mFunctionType, * mFunction;
LinkerSection * mCodeSection, * mDataSection, * mBSSection; LinkerSection * mCodeSection, * mDataSection, * mBSSection;

View File

@ -1277,6 +1277,7 @@ void Scanner::NextRawToken(void)
{ {
int n = 0; int n = 0;
char tkprep[128]; char tkprep[128];
tkprep[0] = 0;
while (NextChar() && IsAlpha(mTokenChar)) while (NextChar() && IsAlpha(mTokenChar))
{ {
@ -1371,6 +1372,8 @@ void Scanner::NextRawToken(void)
{ {
int n = 0; int n = 0;
char tkident[256]; char tkident[256];
tkident[0] = 0;
for (;;) for (;;)
{ {
if (IsIdentChar(mTokenChar)) if (IsIdentChar(mTokenChar))