From 534a42d2fe0a946e05d88197c324c1ac682315e4 Mon Sep 17 00:00:00 2001 From: drmortalwombat <90205530+drmortalwombat@users.noreply.github.com> Date: Wed, 18 Sep 2024 16:27:16 +0200 Subject: [PATCH] Implement C vs C++ void pointer semantics --- include/opp/sstream.cpp | 6 +++--- include/opp/string.cpp | 30 +++++++++++++++--------------- oscar64/Declaration.cpp | 20 +++++++++++++++++--- oscar64/Declaration.h | 2 +- oscar64/Parser.cpp | 3 ++- 5 files changed, 38 insertions(+), 23 deletions(-) diff --git a/include/opp/sstream.cpp b/include/opp/sstream.cpp index 5954939..51aceb6 100644 --- a/include/opp/sstream.cpp +++ b/include/opp/sstream.cpp @@ -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 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); } diff --git a/oscar64/Declaration.cpp b/oscar64/Declaration.cpp index 27170e2..bec59cf 100644 --- a/oscar64/Declaration.cpp +++ b/oscar64/Declaration.cpp @@ -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; diff --git a/oscar64/Declaration.h b/oscar64/Declaration.h index 7234b0a..aa5e7c6 100644 --- a/oscar64/Declaration.h +++ b/oscar64/Declaration.h @@ -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; diff --git a/oscar64/Parser.cpp b/oscar64/Parser.cpp index e08c748..07ba972 100644 --- a/oscar64/Parser.cpp +++ b/oscar64/Parser.cpp @@ -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;