From 6c7347310be138c17afef803ca5a22d1d00aebe3 Mon Sep 17 00:00:00 2001 From: drmortalwombat <90205530+drmortalwombat@users.noreply.github.com> Date: Sun, 2 Jul 2023 20:48:46 +0200 Subject: [PATCH] Init const copy construction --- include/opp/string.cpp | 15 ++++ include/opp/string.h | 5 ++ oscar64/InterCodeGenerator.cpp | 16 +++- oscar64/Parser.cpp | 131 ++++++++++++++++----------------- 4 files changed, 95 insertions(+), 72 deletions(-) diff --git a/include/opp/string.cpp b/include/opp/string.cpp index 9d79206..b64136f 100644 --- a/include/opp/string.cpp +++ b/include/opp/string.cpp @@ -75,6 +75,11 @@ inline const char * string::tocstr(void) const return cstr; } +inline unsigned string::size(void) const +{ + return strlen(cstr); +} + string string::operator+(const string & s) { return string(cstr, s.cstr); @@ -145,3 +150,13 @@ inline bool string::operator>=(const char * s) const { return strcmp(cstr, s) >= 0; } + +inline char & string::operator[](unsigned t) +{ + return cstr[t]; +} + +inline char string::operator[](unsigned t) const +{ + return cstr[t]; +} diff --git a/include/opp/string.h b/include/opp/string.h index a0b4926..bde2f0a 100644 --- a/include/opp/string.h +++ b/include/opp/string.h @@ -13,6 +13,8 @@ public: string(const char * s1, const char * s2); ~string(void); + unsigned size(void) const; + string & operator=(const string & s); string & operator=(const char * s); @@ -37,6 +39,9 @@ public: bool operator>=(const string & s) const; bool operator>=(const char * s) const; + char & operator[](unsigned t); + char operator[](unsigned t) const; + const char * tocstr(void) const; }; diff --git a/oscar64/InterCodeGenerator.cpp b/oscar64/InterCodeGenerator.cpp index 2e094bb..27a5519 100644 --- a/oscar64/InterCodeGenerator.cpp +++ b/oscar64/InterCodeGenerator.cpp @@ -1073,7 +1073,10 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateInline(Declaration* pro block->Append(ins); - return ExValue(rdec->mBase, ins->mDst.mTemp, 1); + ExValue rv(rdec->mBase, ins->mDst.mTemp, 1); + if (ins->mDst.mType != DT_TYPE_REFERENCE) + rv = Dereference(proc, exp, block, rv); + return rv; } else if (lrexp) { @@ -2991,7 +2994,9 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* pex = nullptr; } - ExValue vp(pdec ? pdec->mBase : TheSignedIntTypeDeclaration, ains->mDst.mTemp, 1); + Declaration* ptype = pdec ? pdec->mBase : texp->mDecType; + + ExValue vp(ptype ? ptype : TheSignedIntTypeDeclaration, ains->mDst.mTemp, 1); if (pdec && pdec->mBase->mType == DT_TYPE_REFERENCE && texp->mType == EX_CALL) { @@ -3018,8 +3023,11 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* #endif } - vr = TranslateExpression(procType, proc, block, texp, destack, breakBlock, continueBlock, inlineMapper, &vp); - + if (ptype && (ptype->mType == DT_TYPE_STRUCT || ptype->mType == DT_TYPE_UNION)) + vr = TranslateExpression(procType, proc, block, texp, destack, breakBlock, continueBlock, inlineMapper, &vp); + else + vr = TranslateExpression(procType, proc, block, texp, destack, breakBlock, continueBlock, inlineMapper, nullptr); + if (!(pdec && pdec->mBase->mType == DT_TYPE_REFERENCE) && (vr.mType->mType == DT_TYPE_STRUCT || vr.mType->mType == DT_TYPE_UNION)) { if (pdec && !pdec->mBase->CanAssign(vr.mType)) diff --git a/oscar64/Parser.cpp b/oscar64/Parser.cpp index a988f19..9488b7e 100644 --- a/oscar64/Parser.cpp +++ b/oscar64/Parser.cpp @@ -2660,78 +2660,73 @@ void Parser::ParseVariableInit(Declaration* ndec) else mScanner->NextToken(); - if (pexp && pexp->mType != EX_LIST && ndec->mBase->CanAssign(pexp->mDecType)) + Declaration* fcons = ndec->mBase->mScope ? ndec->mBase->mScope->Lookup(ndec->mBase->mIdent) : nullptr; + + if (fcons) + { + Declaration* mtype = ndec->mBase->ToMutableType(); + + Expression* vexp = new Expression(mScanner->mLocation, EX_VARIABLE); + vexp->mDecType = mtype; + vexp->mDecValue = ndec; + + Expression* cexp = new Expression(mScanner->mLocation, EX_CONSTANT); + cexp->mDecValue = fcons; + cexp->mDecType = cexp->mDecValue->mBase; + + Expression* fexp = new Expression(mScanner->mLocation, EX_CALL); + fexp->mLeft = cexp; + fexp->mRight = pexp; + + Expression* texp = new Expression(mScanner->mLocation, EX_PREFIX); + texp->mToken = TK_BINARY_AND; + texp->mLeft = vexp; + texp->mDecType = new Declaration(mScanner->mLocation, DT_TYPE_POINTER); + texp->mDecType->mFlags |= DTF_CONST | DTF_DEFINED; + texp->mDecType->mBase = mtype; + texp->mDecType->mSize = 2; + + if (fexp->mRight) + { + Expression* lexp = new Expression(mScanner->mLocation, EX_LIST); + lexp->mLeft = texp; + lexp->mRight = fexp->mRight; + fexp->mRight = lexp; + } + else + fexp->mRight = texp; + + ResolveOverloadCall(cexp, fexp->mRight); + + Expression* dexp = nullptr; + if (ndec->mBase->mDestructor) + { + Expression* cexp = new Expression(mScanner->mLocation, EX_CONSTANT); + cexp->mDecValue = ndec->mBase->mDestructor; + cexp->mDecType = cexp->mDecValue->mBase; + + dexp = new Expression(mScanner->mLocation, EX_CALL); + dexp->mLeft = cexp; + dexp->mRight = texp; + } + + Expression* nexp = new Expression(mScanner->mLocation, EX_CONSTRUCT); + + nexp->mLeft = new Expression(mScanner->mLocation, EX_LIST); + nexp->mLeft->mLeft = fexp; + nexp->mLeft->mRight = dexp; + + nexp->mRight = vexp; + nexp->mDecType = vexp->mDecType; + + ndec->mValue = nexp; + } + else if (pexp && pexp->mType != EX_LIST && ndec->mBase->CanAssign(pexp->mDecType)) { ndec->mValue = pexp; } else - { - Declaration* fcons = ndec->mBase->mScope ? ndec->mBase->mScope->Lookup(ndec->mBase->mIdent) : nullptr; - - if (fcons) - { - Declaration* mtype = ndec->mBase->ToMutableType(); - - Expression* vexp = new Expression(mScanner->mLocation, EX_VARIABLE); - vexp->mDecType = mtype; - vexp->mDecValue = ndec; - - Expression* cexp = new Expression(mScanner->mLocation, EX_CONSTANT); - cexp->mDecValue = fcons; - cexp->mDecType = cexp->mDecValue->mBase; - - Expression* fexp = new Expression(mScanner->mLocation, EX_CALL); - fexp->mLeft = cexp; - fexp->mRight = pexp; - - Expression* texp = new Expression(mScanner->mLocation, EX_PREFIX); - texp->mToken = TK_BINARY_AND; - texp->mLeft = vexp; - texp->mDecType = new Declaration(mScanner->mLocation, DT_TYPE_POINTER); - texp->mDecType->mFlags |= DTF_CONST | DTF_DEFINED; - texp->mDecType->mBase = mtype; - texp->mDecType->mSize = 2; - - if (fexp->mRight) - { - Expression* lexp = new Expression(mScanner->mLocation, EX_LIST); - lexp->mLeft = texp; - lexp->mRight = fexp->mRight; - fexp->mRight = lexp; - } - else - fexp->mRight = texp; - - ResolveOverloadCall(cexp, fexp->mRight); - - Expression* dexp = nullptr; - if (ndec->mBase->mDestructor) - { - Expression* cexp = new Expression(mScanner->mLocation, EX_CONSTANT); - cexp->mDecValue = ndec->mBase->mDestructor; - cexp->mDecType = cexp->mDecValue->mBase; - - dexp = new Expression(mScanner->mLocation, EX_CALL); - dexp->mLeft = cexp; - dexp->mRight = texp; - } - - Expression* nexp = new Expression(mScanner->mLocation, EX_CONSTRUCT); - - nexp->mLeft = new Expression(mScanner->mLocation, EX_LIST); - nexp->mLeft->mLeft = fexp; - nexp->mLeft->mRight = dexp; - - nexp->mRight = vexp; - nexp->mDecType = vexp->mDecType; - - ndec->mValue = nexp; - } - else - { - ndec->mValue = pexp; - } - } + mErrors->Error(pexp->mLocation, EERR_INCOMPATIBLE_TYPES, "Can not initialize variable with expression", ndec->mIdent); } Declaration* Parser::ParseDeclaration(Declaration * pdec, bool variable, bool expression, Declaration* pthis)