Implement C vs C++ void pointer semantics
This commit is contained in:
parent
226d8afc24
commit
534a42d2fe
|
@ -20,12 +20,12 @@ void ostringstream::bput(char ch)
|
||||||
{
|
{
|
||||||
mBSize = 15;
|
mBSize = 15;
|
||||||
mBFill = 0;
|
mBFill = 0;
|
||||||
mBuffer = malloc(15);
|
mBuffer = (char *)malloc(15);
|
||||||
}
|
}
|
||||||
else if (mBFill == mBSize)
|
else if (mBFill == mBSize)
|
||||||
{
|
{
|
||||||
mBSize *= 2;
|
mBSize *= 2;
|
||||||
char * b = malloc(mBSize);
|
char * b = (char *)malloc(mBSize);
|
||||||
for(char i=0; i<mBFill; i++)
|
for(char i=0; i<mBFill; i++)
|
||||||
b[i] = mBuffer[i];
|
b[i] = mBuffer[i];
|
||||||
free(mBuffer);
|
free(mBuffer);
|
||||||
|
@ -46,7 +46,7 @@ void ostringstream::str(const string & str)
|
||||||
{
|
{
|
||||||
free(mBuffer);
|
free(mBuffer);
|
||||||
mBSize = mBFill;
|
mBSize = mBFill;
|
||||||
mBuffer = malloc(mBSize);
|
mBuffer = (char *)malloc(mBSize);
|
||||||
}
|
}
|
||||||
str.copyseg(mBuffer, 0, mBFill);
|
str.copyseg(mBuffer, 0, mBFill);
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,7 +32,7 @@ string::string(const string & s)
|
||||||
if (s.cstr)
|
if (s.cstr)
|
||||||
{
|
{
|
||||||
char l = s.cstr[0];
|
char l = s.cstr[0];
|
||||||
cstr = malloc(char(l + 2));
|
cstr = (char *)malloc(char(l + 2));
|
||||||
smemcpy(cstr, s.cstr, l + 2);
|
smemcpy(cstr, s.cstr, l + 2);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -52,7 +52,7 @@ string::string(const char * s)
|
||||||
char l = sstrlen(s);
|
char l = sstrlen(s);
|
||||||
if (l)
|
if (l)
|
||||||
{
|
{
|
||||||
cstr = malloc(char(l + 2));
|
cstr = (char *)malloc(char(l + 2));
|
||||||
cstr[0] = l;
|
cstr[0] = l;
|
||||||
smemcpy(cstr + 1, s, l + 1);
|
smemcpy(cstr + 1, s, l + 1);
|
||||||
}
|
}
|
||||||
|
@ -67,7 +67,7 @@ string::string(const char * s, char size)
|
||||||
{
|
{
|
||||||
if (size)
|
if (size)
|
||||||
{
|
{
|
||||||
cstr = malloc(char(size + 2));
|
cstr = (char *)malloc(char(size + 2));
|
||||||
cstr[0] = size;
|
cstr[0] = size;
|
||||||
smemcpy(cstr + 1, s, size);
|
smemcpy(cstr + 1, s, size);
|
||||||
cstr[size + 1] = 0;
|
cstr[size + 1] = 0;
|
||||||
|
@ -78,7 +78,7 @@ string::string(const char * s, char size)
|
||||||
|
|
||||||
string::string(char c)
|
string::string(char c)
|
||||||
{
|
{
|
||||||
cstr = malloc(3);
|
cstr = (char *)malloc(3);
|
||||||
cstr[0] = 1;
|
cstr[0] = 1;
|
||||||
cstr[1] = c;
|
cstr[1] = c;
|
||||||
cstr[2] = 0;
|
cstr[2] = 0;
|
||||||
|
@ -115,7 +115,7 @@ string & string::operator=(const string & s)
|
||||||
if (s.cstr)
|
if (s.cstr)
|
||||||
{
|
{
|
||||||
char l = s.cstr[0];
|
char l = s.cstr[0];
|
||||||
cstr = malloc(char(l + 2));
|
cstr = (char *)malloc(char(l + 2));
|
||||||
smemcpy(cstr, s.cstr, l + 2);
|
smemcpy(cstr, s.cstr, l + 2);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -145,7 +145,7 @@ string & string::operator=(const char * s)
|
||||||
char l = sstrlen(s);
|
char l = sstrlen(s);
|
||||||
if (l)
|
if (l)
|
||||||
{
|
{
|
||||||
cstr = malloc(char(l + 2));
|
cstr = (char *)malloc(char(l + 2));
|
||||||
cstr[0] = l;
|
cstr[0] = l;
|
||||||
smemcpy(cstr + 1, s, l + 1);
|
smemcpy(cstr + 1, s, l + 1);
|
||||||
}
|
}
|
||||||
|
@ -167,7 +167,7 @@ string & string::operator+=(const string & s)
|
||||||
d = cstr[0];
|
d = cstr[0];
|
||||||
|
|
||||||
char l = s.cstr[0] + d;
|
char l = s.cstr[0] + d;
|
||||||
char * c = malloc(char(l + 2));
|
char * c = (char *)malloc(char(l + 2));
|
||||||
c[0] = l;
|
c[0] = l;
|
||||||
|
|
||||||
if (d)
|
if (d)
|
||||||
|
@ -189,7 +189,7 @@ string & string::operator+=(const char * s)
|
||||||
if (cstr)
|
if (cstr)
|
||||||
{
|
{
|
||||||
char l = sl + cstr[0];
|
char l = sl + cstr[0];
|
||||||
char * c = malloc(char(l + 2));
|
char * c = (char *)malloc(char(l + 2));
|
||||||
c[0] = l;
|
c[0] = l;
|
||||||
smemcpy(c + 1, cstr + 1, cstr[0]);
|
smemcpy(c + 1, cstr + 1, cstr[0]);
|
||||||
smemcpy(c + 1 + cstr[0], s, sl + 1);
|
smemcpy(c + 1 + cstr[0], s, sl + 1);
|
||||||
|
@ -198,7 +198,7 @@ string & string::operator+=(const char * s)
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
cstr = malloc(char(sl + 2));
|
cstr = (char *)malloc(char(sl + 2));
|
||||||
cstr[0] = sl;
|
cstr[0] = sl;
|
||||||
smemcpy(cstr + 1, s, sl + 1);
|
smemcpy(cstr + 1, s, sl + 1);
|
||||||
}
|
}
|
||||||
|
@ -212,7 +212,7 @@ string & string::operator+=(char c)
|
||||||
if (cstr)
|
if (cstr)
|
||||||
{
|
{
|
||||||
char l = cstr[0] + 1;
|
char l = cstr[0] + 1;
|
||||||
char * p = malloc(char(l + 2));
|
char * p = (char *)malloc(char(l + 2));
|
||||||
p[0] = l;
|
p[0] = l;
|
||||||
smemcpy(p + 1, cstr + 1, cstr[0]);
|
smemcpy(p + 1, cstr + 1, cstr[0]);
|
||||||
p[l] = c;
|
p[l] = c;
|
||||||
|
@ -222,7 +222,7 @@ string & string::operator+=(char c)
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
cstr = malloc(3);
|
cstr = (char *)malloc(3);
|
||||||
cstr[0] = 1;
|
cstr[0] = 1;
|
||||||
cstr[1] = c;
|
cstr[1] = c;
|
||||||
cstr[2] = 0;
|
cstr[2] = 0;
|
||||||
|
@ -258,7 +258,7 @@ string string::operator+(const string & s) const
|
||||||
if (s.cstr)
|
if (s.cstr)
|
||||||
{
|
{
|
||||||
char l = cstr[0] + s.cstr[0];
|
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 + 1, cstr[0]);
|
||||||
smemcpy(p + 1 + cstr[0], s.cstr + 1, s.cstr[0]);
|
smemcpy(p + 1 + cstr[0], s.cstr + 1, s.cstr[0]);
|
||||||
return string(l, p);
|
return string(l, p);
|
||||||
|
@ -280,7 +280,7 @@ string string::operator+(const char * s) const
|
||||||
if (sl)
|
if (sl)
|
||||||
{
|
{
|
||||||
char l = cstr[0] + 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 + 1, cstr[0]);
|
||||||
smemcpy(p + 1 + cstr[0], s, sl);
|
smemcpy(p + 1 + cstr[0], s, sl);
|
||||||
return string(l, p);
|
return string(l, p);
|
||||||
|
@ -300,7 +300,7 @@ string string::operator+(char c) const
|
||||||
if (cstr)
|
if (cstr)
|
||||||
{
|
{
|
||||||
char l = cstr[0] + 1;
|
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]);
|
smemcpy(p + 1, cstr + 1, cstr[0]);
|
||||||
p[l] = c;
|
p[l] = c;
|
||||||
return string(l, p);
|
return string(l, p);
|
||||||
|
@ -528,7 +528,7 @@ string string::substr(char pos, char len) const
|
||||||
if (pos + len > l)
|
if (pos + len > l)
|
||||||
len = l - pos;
|
len = l - pos;
|
||||||
|
|
||||||
char * p = malloc(len + 2);
|
char * p = (char *)malloc(len + 2);
|
||||||
memcpy(p + 1, cstr + 1 + pos, len);
|
memcpy(p + 1, cstr + 1 + pos, len);
|
||||||
return string(len, p);
|
return string(len, p);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2701,7 +2701,16 @@ bool Declaration::CanAssign(const Declaration* fromType) const
|
||||||
if (fromType->mType == DT_TYPE_POINTER || fromType->mType == DT_TYPE_ARRAY)
|
if (fromType->mType == DT_TYPE_POINTER || fromType->mType == DT_TYPE_ARRAY)
|
||||||
{
|
{
|
||||||
if (mBase->mType == DT_TYPE_VOID || fromType->mBase->mType == DT_TYPE_VOID)
|
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))
|
else if (mBase->mStripe == fromType->mBase->mStripe && mBase->IsSubType(fromType->mBase))
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -2761,7 +2770,7 @@ Declaration* TheBoolTypeDeclaration, * TheFloatTypeDeclaration, * TheConstVoidPo
|
||||||
Declaration* TheVoidFunctionTypeDeclaration, * TheConstVoidValueDeclaration;
|
Declaration* TheVoidFunctionTypeDeclaration, * TheConstVoidValueDeclaration;
|
||||||
Declaration* TheCharPointerTypeDeclaration, * TheConstCharPointerTypeDeclaration;
|
Declaration* TheCharPointerTypeDeclaration, * TheConstCharPointerTypeDeclaration;
|
||||||
Expression* TheVoidExpression;
|
Expression* TheVoidExpression;
|
||||||
Declaration* TheNullptrConstDeclaration, * TheZeroIntegerConstDeclaration, * TheZeroFloatConstDeclaration;
|
Declaration* TheNullptrConstDeclaration, * TheZeroIntegerConstDeclaration, * TheZeroFloatConstDeclaration, * TheNullPointerTypeDeclaration;
|
||||||
|
|
||||||
void InitDeclarations(void)
|
void InitDeclarations(void)
|
||||||
{
|
{
|
||||||
|
@ -2782,6 +2791,11 @@ void InitDeclarations(void)
|
||||||
TheConstVoidPointerTypeDeclaration->mSize = 2;
|
TheConstVoidPointerTypeDeclaration->mSize = 2;
|
||||||
TheConstVoidPointerTypeDeclaration->mFlags = DTF_DEFINED;
|
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 = new Declaration(noloc, DT_TYPE_FUNCTION);
|
||||||
TheVoidFunctionTypeDeclaration->mBase = TheVoidTypeDeclaration;
|
TheVoidFunctionTypeDeclaration->mBase = TheVoidTypeDeclaration;
|
||||||
TheVoidFunctionTypeDeclaration->mSize = 2;
|
TheVoidFunctionTypeDeclaration->mSize = 2;
|
||||||
|
@ -2847,7 +2861,7 @@ void InitDeclarations(void)
|
||||||
|
|
||||||
|
|
||||||
TheNullptrConstDeclaration = new Declaration(noloc, DT_CONST_ADDRESS);
|
TheNullptrConstDeclaration = new Declaration(noloc, DT_CONST_ADDRESS);
|
||||||
TheNullptrConstDeclaration->mBase = TheVoidPointerTypeDeclaration;
|
TheNullptrConstDeclaration->mBase = TheNullPointerTypeDeclaration;
|
||||||
TheNullptrConstDeclaration->mSize = 2;
|
TheNullptrConstDeclaration->mSize = 2;
|
||||||
TheZeroIntegerConstDeclaration = new Declaration(noloc, DT_CONST_INTEGER);
|
TheZeroIntegerConstDeclaration = new Declaration(noloc, DT_CONST_INTEGER);
|
||||||
TheZeroIntegerConstDeclaration->mBase = TheSignedIntTypeDeclaration;
|
TheZeroIntegerConstDeclaration->mBase = TheSignedIntTypeDeclaration;
|
||||||
|
|
|
@ -369,6 +369,6 @@ extern Declaration* TheVoidTypeDeclaration, * TheConstVoidTypeDeclaration, * The
|
||||||
extern Declaration* TheBoolTypeDeclaration, * TheFloatTypeDeclaration, * TheVoidPointerTypeDeclaration, * TheConstVoidPointerTypeDeclaration, * TheSignedLongTypeDeclaration, * TheUnsignedLongTypeDeclaration;
|
extern Declaration* TheBoolTypeDeclaration, * TheFloatTypeDeclaration, * TheVoidPointerTypeDeclaration, * TheConstVoidPointerTypeDeclaration, * TheSignedLongTypeDeclaration, * TheUnsignedLongTypeDeclaration;
|
||||||
extern Declaration* TheVoidFunctionTypeDeclaration, * TheConstVoidValueDeclaration;
|
extern Declaration* TheVoidFunctionTypeDeclaration, * TheConstVoidValueDeclaration;
|
||||||
extern Declaration* TheCharPointerTypeDeclaration, * TheConstCharPointerTypeDeclaration;
|
extern Declaration* TheCharPointerTypeDeclaration, * TheConstCharPointerTypeDeclaration;
|
||||||
extern Declaration* TheNullptrConstDeclaration, * TheZeroIntegerConstDeclaration, * TheZeroFloatConstDeclaration;
|
extern Declaration* TheNullptrConstDeclaration, * TheZeroIntegerConstDeclaration, * TheZeroFloatConstDeclaration, * TheNullPointerTypeDeclaration;
|
||||||
extern Expression* TheVoidExpression;
|
extern Expression* TheVoidExpression;
|
||||||
|
|
||||||
|
|
|
@ -1125,6 +1125,7 @@ Declaration* Parser::ParsePostfixDeclaration(void)
|
||||||
Declaration* ndec = new Declaration(mScanner->mLocation, DT_TYPE_POINTER);
|
Declaration* ndec = new Declaration(mScanner->mLocation, DT_TYPE_POINTER);
|
||||||
ndec->mSize = 2;
|
ndec->mSize = 2;
|
||||||
ndec->mFlags |= DTF_DEFINED;
|
ndec->mFlags |= DTF_DEFINED;
|
||||||
|
ndec->mCompilerOptions = mCompilerOptions;
|
||||||
|
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
|
@ -6014,7 +6015,7 @@ Expression* Parser::ParseSimpleExpression(bool lhs, bool tid)
|
||||||
break;
|
break;
|
||||||
case TK_NULL:
|
case TK_NULL:
|
||||||
dec = new Declaration(mScanner->mLocation, DT_CONST_ADDRESS);
|
dec = new Declaration(mScanner->mLocation, DT_CONST_ADDRESS);
|
||||||
dec->mBase = TheVoidPointerTypeDeclaration;
|
dec->mBase = TheNullPointerTypeDeclaration;
|
||||||
dec->mInteger = 0;
|
dec->mInteger = 0;
|
||||||
exp = new Expression(mScanner->mLocation, EX_CONSTANT);
|
exp = new Expression(mScanner->mLocation, EX_CONSTANT);
|
||||||
exp->mDecValue = dec;
|
exp->mDecValue = dec;
|
||||||
|
|
Loading…
Reference in New Issue