Fix placement new for vector constructor

This commit is contained in:
drmortalwombat 2025-04-29 20:12:48 +02:00
parent f9acbc152d
commit 2b0994b086
2 changed files with 63 additions and 5 deletions

View File

@ -21,7 +21,7 @@ public:
vector(size_t n) : _data((T*)malloc(n * sizeof(T))), _size(n), _capacity(n) vector(size_t n) : _data((T*)malloc(n * sizeof(T))), _size(n), _capacity(n)
{ {
for(size_t i=0; i<n; i++) for(size_t i=0; i<n; i++)
new (_data + i) T; new (_data + i) T();
} }
vector(const vector & v) vector(const vector & v)
@ -232,7 +232,7 @@ void vector<T>::resize(size_t n)
else if (n < _capacity) else if (n < _capacity)
{ {
for(size_t i=_size; i<n; i++) for(size_t i=_size; i<n; i++)
new(_data + i)T; new(_data + i)T();
_size = n; _size = n;
} }
else else
@ -291,7 +291,7 @@ void vector<T>::insert(size_t at, const T & t)
{ {
if (_size == _capacity) if (_size == _capacity)
reserve(_size + 1 + (_size >> 1)); reserve(_size + 1 + (_size >> 1));
new (_data + _size)T; new (_data + _size)T();
for(size_t i=_size; i>at; i--) for(size_t i=_size; i>at; i--)
_data[i] = move(_data[i - 1]); _data[i] = move(_data[i - 1]);
_data[at] = t; _data[at] = t;
@ -318,7 +318,7 @@ T * vector<T>::insert(T * at, const T & t)
at = (T *)(f + unsigned(_data)); at = (T *)(f + unsigned(_data));
} }
T * dp = _data + _size; T * dp = _data + _size;
new (dp)T; new (dp)T();
while (dp != at) while (dp != at)
{ {
dp--; dp--;

View File

@ -7950,6 +7950,28 @@ Expression* Parser::ParsePostfixExpression(bool lhs)
nexp = CheckOperatorOverload(nexp); nexp = CheckOperatorOverload(nexp);
exp = nexp->ConstantFold(mErrors, mDataSection); exp = nexp->ConstantFold(mErrors, mDataSection);
} }
else if (exp->mDecType->IsSimpleType())
{
Expression* nexp = new Expression(mScanner->mLocation, EX_CONSTANT);
if (exp->mDecType->IsIntegerType())
{
nexp->mDecType = exp->mDecType;
nexp->mDecValue = TheZeroIntegerConstDeclaration;
}
else if (exp->mDecType->mType == DT_TYPE_FLOAT)
{
nexp->mDecType = TheFloatTypeDeclaration;
nexp->mDecValue = TheZeroFloatConstDeclaration;
}
else // Pointer
{
nexp->mDecType = TheNullPointerTypeDeclaration;
nexp->mDecValue = TheNullptrConstDeclaration;
}
exp = nexp;
}
else else
{ {
Declaration* tdec = new Declaration(mScanner->mLocation, DT_VARIABLE); Declaration* tdec = new Declaration(mScanner->mLocation, DT_VARIABLE);
@ -8398,13 +8420,49 @@ Expression* Parser::ParseNewOperator(void)
nexp = new Expression(mScanner->mLocation, EX_PREFIX); nexp = new Expression(mScanner->mLocation, EX_PREFIX);
nexp->mToken = TK_BINARY_AND; nexp->mToken = TK_BINARY_AND;
nexp->mDecType = nexp->mDecType; nexp->mDecType = iexp->mDecType;
nexp->mLeft = iexp; nexp->mLeft = iexp;
} }
else if (pexp) else if (pexp)
{ {
mErrors->Error(mScanner->mLocation, ERRO_NO_MATCHING_FUNCTION_CALL, "No matching constructor", dec->mIdent); mErrors->Error(mScanner->mLocation, ERRO_NO_MATCHING_FUNCTION_CALL, "No matching constructor", dec->mIdent);
} }
else if (nexp->mDecType->IsSimpleType())
{
Expression* cexp = new Expression(mScanner->mLocation, EX_CONSTANT);
if (nexp->mDecType->mBase->IsIntegerType())
{
cexp->mDecType = TheSignedIntTypeDeclaration;
cexp->mDecValue = TheZeroIntegerConstDeclaration;
}
else if (nexp->mDecType->mBase->mType == DT_TYPE_FLOAT)
{
cexp->mDecType = TheFloatTypeDeclaration;
cexp->mDecValue = TheZeroFloatConstDeclaration;
}
else // Pointer
{
cexp->mDecType = TheNullPointerTypeDeclaration;
cexp->mDecValue = TheNullptrConstDeclaration;
}
Expression* dexp = new Expression(mScanner->mLocation, EX_PREFIX);
dexp->mToken = TK_MUL;
dexp->mLeft = nexp;
dexp->mDecType = nexp->mDecType->mBase;
Expression* iexp = new Expression(mScanner->mLocation, EX_INITIALIZATION);
iexp->mToken = TK_ASSIGN;
iexp->mLeft = dexp;
iexp->mRight = cexp;
iexp->mDecType = nexp->mDecType;
nexp = new Expression(mScanner->mLocation, EX_PREFIX);
nexp->mToken = TK_BINARY_AND;
nexp->mDecType = iexp->mDecType;
nexp->mLeft = iexp;
}
} }
} }