Implement C vs C++ void pointer semantics

This commit is contained in:
drmortalwombat 2024-09-18 16:27:16 +02:00
parent 226d8afc24
commit 534a42d2fe
5 changed files with 38 additions and 23 deletions

View File

@ -20,12 +20,12 @@ void ostringstream::bput(char ch)
{
mBSize = 15;
mBFill = 0;
mBuffer = malloc(15);
mBuffer = (char *)malloc(15);
}
else if (mBFill == mBSize)
{
mBSize *= 2;
char * b = malloc(mBSize);
char * b = (char *)malloc(mBSize);
for(char i=0; i<mBFill; i++)
b[i] = mBuffer[i];
free(mBuffer);
@ -46,7 +46,7 @@ void ostringstream::str(const string & str)
{
free(mBuffer);
mBSize = mBFill;
mBuffer = malloc(mBSize);
mBuffer = (char *)malloc(mBSize);
}
str.copyseg(mBuffer, 0, mBFill);
}

View File

@ -32,7 +32,7 @@ string::string(const string & s)
if (s.cstr)
{
char l = s.cstr[0];
cstr = malloc(char(l + 2));
cstr = (char *)malloc(char(l + 2));
smemcpy(cstr, s.cstr, l + 2);
}
else
@ -52,7 +52,7 @@ string::string(const char * s)
char l = sstrlen(s);
if (l)
{
cstr = malloc(char(l + 2));
cstr = (char *)malloc(char(l + 2));
cstr[0] = l;
smemcpy(cstr + 1, s, l + 1);
}
@ -67,7 +67,7 @@ string::string(const char * s, char size)
{
if (size)
{
cstr = malloc(char(size + 2));
cstr = (char *)malloc(char(size + 2));
cstr[0] = size;
smemcpy(cstr + 1, s, size);
cstr[size + 1] = 0;
@ -78,7 +78,7 @@ string::string(const char * s, char size)
string::string(char c)
{
cstr = malloc(3);
cstr = (char *)malloc(3);
cstr[0] = 1;
cstr[1] = c;
cstr[2] = 0;
@ -115,7 +115,7 @@ string & string::operator=(const string & s)
if (s.cstr)
{
char l = s.cstr[0];
cstr = malloc(char(l + 2));
cstr = (char *)malloc(char(l + 2));
smemcpy(cstr, s.cstr, l + 2);
}
else
@ -145,7 +145,7 @@ string & string::operator=(const char * s)
char l = sstrlen(s);
if (l)
{
cstr = malloc(char(l + 2));
cstr = (char *)malloc(char(l + 2));
cstr[0] = l;
smemcpy(cstr + 1, s, l + 1);
}
@ -167,7 +167,7 @@ string & string::operator+=(const string & s)
d = cstr[0];
char l = s.cstr[0] + d;
char * c = malloc(char(l + 2));
char * c = (char *)malloc(char(l + 2));
c[0] = l;
if (d)
@ -189,7 +189,7 @@ string & string::operator+=(const char * s)
if (cstr)
{
char l = sl + cstr[0];
char * c = malloc(char(l + 2));
char * c = (char *)malloc(char(l + 2));
c[0] = l;
smemcpy(c + 1, cstr + 1, cstr[0]);
smemcpy(c + 1 + cstr[0], s, sl + 1);
@ -198,7 +198,7 @@ string & string::operator+=(const char * s)
}
else
{
cstr = malloc(char(sl + 2));
cstr = (char *)malloc(char(sl + 2));
cstr[0] = sl;
smemcpy(cstr + 1, s, sl + 1);
}
@ -212,7 +212,7 @@ string & string::operator+=(char c)
if (cstr)
{
char l = cstr[0] + 1;
char * p = malloc(char(l + 2));
char * p = (char *)malloc(char(l + 2));
p[0] = l;
smemcpy(p + 1, cstr + 1, cstr[0]);
p[l] = c;
@ -222,7 +222,7 @@ string & string::operator+=(char c)
}
else
{
cstr = malloc(3);
cstr = (char *)malloc(3);
cstr[0] = 1;
cstr[1] = c;
cstr[2] = 0;
@ -258,7 +258,7 @@ string string::operator+(const string & s) const
if (s.cstr)
{
char l = cstr[0] + s.cstr[0];
char * p = malloc(char(l + 2));
char * p = (char *)malloc(char(l + 2));
smemcpy(p + 1, cstr + 1, cstr[0]);
smemcpy(p + 1 + cstr[0], s.cstr + 1, s.cstr[0]);
return string(l, p);
@ -280,7 +280,7 @@ string string::operator+(const char * s) const
if (sl)
{
char l = cstr[0] + sl;
char * p = malloc(char(l + 2));
char * p = (char *)malloc(char(l + 2));
smemcpy(p + 1, cstr + 1, cstr[0]);
smemcpy(p + 1 + cstr[0], s, sl);
return string(l, p);
@ -300,7 +300,7 @@ string string::operator+(char c) const
if (cstr)
{
char l = cstr[0] + 1;
char * p = malloc(char(l + 2));
char * p = (char *)malloc(char(l + 2));
smemcpy(p + 1, cstr + 1, cstr[0]);
p[l] = c;
return string(l, p);
@ -528,7 +528,7 @@ string string::substr(char pos, char len) const
if (pos + len > l)
len = l - pos;
char * p = malloc(len + 2);
char * p = (char *)malloc(len + 2);
memcpy(p + 1, cstr + 1 + pos, len);
return string(len, p);
}

View File

@ -2701,7 +2701,16 @@ bool Declaration::CanAssign(const Declaration* fromType) const
if (fromType->mType == DT_TYPE_POINTER || fromType->mType == DT_TYPE_ARRAY)
{
if (mBase->mType == DT_TYPE_VOID || fromType->mBase->mType == DT_TYPE_VOID)
return (mBase->mFlags & DTF_CONST) || !(fromType->mBase->mFlags & DTF_CONST);
{
if (mCompilerOptions & COPT_CPLUSPLUS)
{
if (fromType == TheNullPointerTypeDeclaration)
return true;
return mBase->mType == DT_TYPE_VOID && ((mBase->mFlags & DTF_CONST) || !(fromType->mBase->mFlags & DTF_CONST));
}
else
return true;
}
else if (mBase->mStripe == fromType->mBase->mStripe && mBase->IsSubType(fromType->mBase))
return true;
}
@ -2761,7 +2770,7 @@ Declaration* TheBoolTypeDeclaration, * TheFloatTypeDeclaration, * TheConstVoidPo
Declaration* TheVoidFunctionTypeDeclaration, * TheConstVoidValueDeclaration;
Declaration* TheCharPointerTypeDeclaration, * TheConstCharPointerTypeDeclaration;
Expression* TheVoidExpression;
Declaration* TheNullptrConstDeclaration, * TheZeroIntegerConstDeclaration, * TheZeroFloatConstDeclaration;
Declaration* TheNullptrConstDeclaration, * TheZeroIntegerConstDeclaration, * TheZeroFloatConstDeclaration, * TheNullPointerTypeDeclaration;
void InitDeclarations(void)
{
@ -2782,6 +2791,11 @@ void InitDeclarations(void)
TheConstVoidPointerTypeDeclaration->mSize = 2;
TheConstVoidPointerTypeDeclaration->mFlags = DTF_DEFINED;
TheNullPointerTypeDeclaration = new Declaration(noloc, DT_TYPE_POINTER);
TheNullPointerTypeDeclaration->mBase = TheVoidTypeDeclaration;
TheNullPointerTypeDeclaration->mSize = 2;
TheNullPointerTypeDeclaration->mFlags = DTF_DEFINED;
TheVoidFunctionTypeDeclaration = new Declaration(noloc, DT_TYPE_FUNCTION);
TheVoidFunctionTypeDeclaration->mBase = TheVoidTypeDeclaration;
TheVoidFunctionTypeDeclaration->mSize = 2;
@ -2847,7 +2861,7 @@ void InitDeclarations(void)
TheNullptrConstDeclaration = new Declaration(noloc, DT_CONST_ADDRESS);
TheNullptrConstDeclaration->mBase = TheVoidPointerTypeDeclaration;
TheNullptrConstDeclaration->mBase = TheNullPointerTypeDeclaration;
TheNullptrConstDeclaration->mSize = 2;
TheZeroIntegerConstDeclaration = new Declaration(noloc, DT_CONST_INTEGER);
TheZeroIntegerConstDeclaration->mBase = TheSignedIntTypeDeclaration;

View File

@ -369,6 +369,6 @@ extern Declaration* TheVoidTypeDeclaration, * TheConstVoidTypeDeclaration, * The
extern Declaration* TheBoolTypeDeclaration, * TheFloatTypeDeclaration, * TheVoidPointerTypeDeclaration, * TheConstVoidPointerTypeDeclaration, * TheSignedLongTypeDeclaration, * TheUnsignedLongTypeDeclaration;
extern Declaration* TheVoidFunctionTypeDeclaration, * TheConstVoidValueDeclaration;
extern Declaration* TheCharPointerTypeDeclaration, * TheConstCharPointerTypeDeclaration;
extern Declaration* TheNullptrConstDeclaration, * TheZeroIntegerConstDeclaration, * TheZeroFloatConstDeclaration;
extern Declaration* TheNullptrConstDeclaration, * TheZeroIntegerConstDeclaration, * TheZeroFloatConstDeclaration, * TheNullPointerTypeDeclaration;
extern Expression* TheVoidExpression;

View File

@ -1125,6 +1125,7 @@ Declaration* Parser::ParsePostfixDeclaration(void)
Declaration* ndec = new Declaration(mScanner->mLocation, DT_TYPE_POINTER);
ndec->mSize = 2;
ndec->mFlags |= DTF_DEFINED;
ndec->mCompilerOptions = mCompilerOptions;
for (;;)
{
@ -6014,7 +6015,7 @@ Expression* Parser::ParseSimpleExpression(bool lhs, bool tid)
break;
case TK_NULL:
dec = new Declaration(mScanner->mLocation, DT_CONST_ADDRESS);
dec->mBase = TheVoidPointerTypeDeclaration;
dec->mBase = TheNullPointerTypeDeclaration;
dec->mInteger = 0;
exp = new Expression(mScanner->mLocation, EX_CONSTANT);
exp->mDecValue = dec;