From 9d6691cf919ba0475dac91a859875f19a3f0b462 Mon Sep 17 00:00:00 2001 From: drmortalwombat <90205530+drmortalwombat@users.noreply.github.com> Date: Sat, 12 Aug 2023 18:19:46 +0200 Subject: [PATCH] Copy constructor and destruct for scalar types --- include/opp/vector.h | 42 ++++++++++++++++----- oscar64/Parser.cpp | 88 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 118 insertions(+), 12 deletions(-) diff --git a/include/opp/vector.h b/include/opp/vector.h index 4227ca1..d20f405 100644 --- a/include/opp/vector.h +++ b/include/opp/vector.h @@ -1,6 +1,8 @@ #ifndef OPP_VECTOR_H #define OPP_VECTOR_H +#include + template class vector { @@ -9,10 +11,16 @@ protected: int _size, _capacity; public: vector(void) : _data(nullptr), _size(0), _capacity(0) {} - vector(int n) : _data(new T[n]), _size(n), _capacity(n) {} + vector(int n) : _data((T*)malloc(n * sizeof(T))), _size(n), _capacity(n) + { + for(int i=0; i::reserve(int n) if (n > _capacity) { _capacity = n; - T * d = new T[_capacity]; + T * d = (T *)malloc(_capacity * sizeof(T)); for(int i=0; i<_size; i++) - d[i] = _data[i]; - delete[] _data; + { + new (d + i)T(_data[i]); + _data[i].~T(); + } + free(_data); _data = d; } } @@ -122,9 +133,17 @@ template void vector::resize(int n) { if (n < _size) + { + for(int i=n; i<_size; i++) + _data[i].~T(); _size = n; + } else if (n < _capacity) + { + for(int i=_size; i::shrink_to_fit(void) if (_size < _capacity) { _capacity = _size; - T * d = new T[_capacity]; + T * d = (T *)malloc(_capacity * sizeof(T)); for(int i=0; i<_size; i++) - d[i] = _data[i]; - delete[] _data; + { + new (d + i)T(_data[i]); + _data[i].~T(); + } + free(_data); _data = d; } } @@ -151,7 +173,7 @@ void vector::push_back(const T & t) { if (_size == _capacity) reserve(_size + 1 + (_size >> 1)); - _data[_size++] = t; + new (_data + _size++)T(t); } template @@ -159,6 +181,7 @@ void vector::insert(int at, const T & t) { if (_size == _capacity) reserve(_size + 1 + (_size >> 1)); + new (_data + _size)T; for(int i=_size; i>at; i--) _data[i] = _data[i - 1]; _data[at] = t; @@ -170,6 +193,7 @@ void vector::erase(int at, int n) _size -= n; for(int i=at; i<_size; i++) _data[i] = _data[i + n]; + _data[_size].~T(); } #endif diff --git a/oscar64/Parser.cpp b/oscar64/Parser.cpp index d6ff328..c3e50f9 100644 --- a/oscar64/Parser.cpp +++ b/oscar64/Parser.cpp @@ -3301,7 +3301,15 @@ Declaration* Parser::ParseDeclaration(Declaration * pdec, bool variable, bool ex mScanner->NextToken(); if (mScanner->mToken == TK_IDENT) { - if (mScanner->mTokenIdent != pthis->mBase->mIdent) + const Ident* ident = mScanner->mTokenIdent; + if (mTemplateScope) + { + Declaration* dec = mTemplateScope->Lookup(ident); + if (dec) + ident = dec->mIdent; + } + + if (ident != pthis->mBase->mIdent) mErrors->Error(mScanner->mLocation, EERR_INVALID_IDENTIFIER, "Wrong class name for destructor", pthis->mBase->mIdent); mScanner->NextToken(); } @@ -4662,7 +4670,19 @@ Expression* Parser::ParseQualify(Expression* exp) { mScanner->NextToken(); if (mScanner->mToken == TK_IDENT) - ident = mScanner->mTokenIdent->PreMangle("~"); + { + ident = mScanner->mTokenIdent; + if (mTemplateScope) + { + Declaration* dec = mTemplateScope->Lookup(ident); + if (dec) + ident = dec->mIdent; + } + + ident = ident->PreMangle("~"); + } + else + mErrors->Error(mScanner->mLocation, EERR_SYNTAX, "Identifier expected"); } else if (mScanner->mToken == TK_IDENT) ident = mScanner->mTokenIdent; @@ -4752,6 +4772,40 @@ Expression* Parser::ParseQualify(Expression* exp) mErrors->Error(mScanner->mLocation, EERR_SYNTAX, "Struct member identifier expected"); } + else if ((mCompilerOptions & COPT_CPLUSPLUS) && mScanner->mToken == TK_BINARY_NOT) + { + mScanner->NextToken(); + if (mScanner->mToken == TK_IDENT) + { + const Ident * ident = mScanner->mTokenIdent; + if (mTemplateScope) + { + Declaration* dec = mTemplateScope->Lookup(ident); + if (dec == dtype) + { + mScanner->NextToken(); + ConsumeToken(TK_OPEN_PARENTHESIS); + ConsumeToken(TK_CLOSE_PARENTHESIS); + + exp = new Expression(mScanner->mLocation, EX_VOID); + exp->mDecType = TheConstVoidTypeDeclaration; + } + } + else if (ident == dtype->mIdent) + { + mScanner->NextToken(); + ConsumeToken(TK_OPEN_PARENTHESIS); + ConsumeToken(TK_CLOSE_PARENTHESIS); + + exp = new Expression(mScanner->mLocation, EX_VOID); + exp->mDecType = TheConstVoidTypeDeclaration; + } + else + mErrors->Error(mScanner->mLocation, EERR_SYNTAX, "Destructor identifier expected"); + } + else + mErrors->Error(mScanner->mLocation, EERR_SYNTAX, "Identifier expected"); + } else mErrors->Error(mScanner->mLocation, EERR_INCOMPATIBLE_OPERATOR, "Struct expected"); @@ -5586,7 +5640,10 @@ Expression* Parser::ParseNewOperator(void) if (!ConsumeTokenIf(TK_CLOSE_PARENTHESIS)) { plist = true; - mdec = dec->mScope->Lookup(dec->mIdent->PreMangle("+")); + if (dec->mType == DT_TYPE_STRUCT) + mdec = dec->mScope->Lookup(dec->mIdent->PreMangle("+")); + else + mdec = nullptr; } } @@ -5654,6 +5711,31 @@ Expression* Parser::ParseNewOperator(void) nexp->mLeft = iexp; nexp->mRight = coexp; nexp->mDecType = coexp->mDecType; + } + else if (plist && !mdec) + { + Expression * pexp = ParseListExpression(false); + ConsumeToken(TK_CLOSE_PARENTHESIS); + + if (pexp && pexp->mType != EX_LIST) + { + 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 = pexp; + iexp->mDecType = nexp->mDecType; + + nexp = iexp; + } + else + mErrors->Error(mScanner->mLocation, ERRO_NO_MATCHING_FUNCTION_CALL, "No matching constructor", dec->mIdent); + + } else if (plist) {