Init const copy construction

This commit is contained in:
drmortalwombat 2023-07-02 20:48:46 +02:00
parent ef0a79b8f0
commit 6c7347310b
4 changed files with 95 additions and 72 deletions

View File

@ -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];
}

View File

@ -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;
};

View File

@ -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,7 +3023,10 @@ 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))
{

View File

@ -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)