Fix template expansion in multiple cpp
This commit is contained in:
parent
020afb8722
commit
2027ac5d4c
|
@ -1,5 +1,7 @@
|
|||
#include <opp/vector.h>
|
||||
#include <opp/algorithm.h>
|
||||
#include <assert.h>
|
||||
#include <opp/iostream.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
@ -23,5 +25,44 @@ int main(void)
|
|||
|
||||
assert(s == 1 + 3 + 5 + 7 + 9);
|
||||
|
||||
opp::vector<int> v;
|
||||
|
||||
for(int i=0; i<10; i++)
|
||||
v.push_back(i);
|
||||
|
||||
assert(v.size() == 10);
|
||||
v.insert(0, 20);
|
||||
assert(v.size() == 11);
|
||||
v.insert(6, 21);
|
||||
assert(v.size() == 12);
|
||||
v.insert(12, 22);
|
||||
|
||||
int * fi = opp::find(v.begin(), v.end(), 21);
|
||||
|
||||
fi = v.insert(fi, 30);
|
||||
fi = v.insert(fi, 31);
|
||||
fi = v.insert(fi, 32);
|
||||
|
||||
assert(v.size() == 16);
|
||||
assert(v[0] == 20);
|
||||
assert(v[15] == 22);
|
||||
assert(v[8] == 32);
|
||||
|
||||
fi = opp::find(v.begin(), v.end(), 32);
|
||||
|
||||
for(int i=0; i<30; i++)
|
||||
{
|
||||
fi = v.insert(fi, i + 40);
|
||||
}
|
||||
|
||||
assert(v.size() == 46);
|
||||
assert(v[28] == 60);
|
||||
|
||||
v.erase(10, 10);
|
||||
|
||||
for(int i : v)
|
||||
opp::cout << i << ", ";
|
||||
opp::cout << "\n";
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -1,14 +1,9 @@
|
|||
#ifndef OPP_ALGORITHM_H
|
||||
#define OPP_ALGORITHM_H
|
||||
|
||||
#include "utility.h"
|
||||
namespace opp {
|
||||
|
||||
template <class T>
|
||||
inline void swap(T & x, T & y)
|
||||
{
|
||||
T t(x); x = y; y = t;
|
||||
}
|
||||
|
||||
template<class T, class LT>
|
||||
void sort(T s, T e)
|
||||
{
|
||||
|
@ -72,6 +67,18 @@ OI copy(II first, II last, OI result)
|
|||
return result;
|
||||
}
|
||||
|
||||
|
||||
template<class II, class T>
|
||||
II find (II first, II last, const T& val)
|
||||
{
|
||||
while (first != last)
|
||||
{
|
||||
if (*first == val) return first;
|
||||
++first;
|
||||
}
|
||||
return last;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
@ -4,11 +4,17 @@
|
|||
namespace opp {
|
||||
|
||||
template <class T>
|
||||
T && move(T & m)
|
||||
inline T && move(T & m)
|
||||
{
|
||||
return (T &&)m;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline void swap(T & x, T & y)
|
||||
{
|
||||
T t(x); x = y; y = move(t);
|
||||
}
|
||||
|
||||
|
||||
template<class T1, class T2>
|
||||
struct pair
|
||||
|
|
|
@ -12,22 +12,22 @@ class vector
|
|||
{
|
||||
protected:
|
||||
T * _data;
|
||||
int _size, _capacity;
|
||||
size_t _size, _capacity;
|
||||
public:
|
||||
typedef T element_type;
|
||||
|
||||
vector(void) : _data(nullptr), _size(0), _capacity(0) {}
|
||||
|
||||
vector(int 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(int i=0; i<n; i++)
|
||||
for(size_t i=0; i<n; i++)
|
||||
new (_data + i) T;
|
||||
}
|
||||
|
||||
vector(const vector & v)
|
||||
: _data((T*)malloc(v._size * sizeof(T))), _size(v._size), _capacity(v._size)
|
||||
{
|
||||
for(int i=0; i<_size; i++)
|
||||
for(size_t i=0; i<_size; i++)
|
||||
new (_data + i)T(v._data[i]);
|
||||
}
|
||||
|
||||
|
@ -41,11 +41,39 @@ public:
|
|||
|
||||
~vector(void)
|
||||
{
|
||||
for(int i=0; i<_size; i++)
|
||||
for(size_t i=0; i<_size; i++)
|
||||
_data[i].~T();
|
||||
free(_data);
|
||||
}
|
||||
|
||||
vector & operator=(const vector & v)
|
||||
{
|
||||
if (this != &v)
|
||||
{
|
||||
for(size_t i=0; i<_size; i++)
|
||||
_data[i].~T();
|
||||
free(_data);
|
||||
|
||||
_data = (T*)malloc(v._size * sizeof(T));
|
||||
_size = v._size;
|
||||
_capacity = v._size;
|
||||
for(size_t i=0; i<_size; i++)
|
||||
new (_data + i)T(v._data[i]);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
vector & operator=(vector && v)
|
||||
{
|
||||
if (this != &v)
|
||||
{
|
||||
swap(_data, v._data);
|
||||
swap(_size, v._size);
|
||||
swap(_capacity, v._capacity);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
int size(void) const
|
||||
{
|
||||
return _size;
|
||||
|
@ -66,28 +94,28 @@ public:
|
|||
return _capacity;
|
||||
}
|
||||
|
||||
void resize(int n);
|
||||
void resize(size_t n);
|
||||
|
||||
void reserve(int n);
|
||||
void reserve(size_t n);
|
||||
|
||||
void shrink_to_fit(void);
|
||||
|
||||
T & at(int at)
|
||||
T & at(size_t at)
|
||||
{
|
||||
return _data[at];
|
||||
}
|
||||
|
||||
const T & at(int at) const
|
||||
const T & at(size_t at) const
|
||||
{
|
||||
return _data[at];
|
||||
}
|
||||
|
||||
T & operator[](int at)
|
||||
T & operator[](size_t at)
|
||||
{
|
||||
return _data[at];
|
||||
}
|
||||
|
||||
const T & operator[](int at) const
|
||||
const T & operator[](size_t at) const
|
||||
{
|
||||
return _data[at];
|
||||
}
|
||||
|
@ -162,9 +190,11 @@ public:
|
|||
_data[_size].~T();
|
||||
}
|
||||
|
||||
void insert(int at, const T & t);
|
||||
void insert(size_t at, const T & t);
|
||||
|
||||
void erase(int at, int n = 1);
|
||||
void erase(size_t at, size_t n = 1);
|
||||
|
||||
T * insert(T * at, const T & t);
|
||||
|
||||
template <typename ...P>
|
||||
void emplace_back(const P&... p);
|
||||
|
@ -174,13 +204,13 @@ protected:
|
|||
|
||||
|
||||
template <class T>
|
||||
void vector<T>::reserve(int n)
|
||||
void vector<T>::reserve(size_t n)
|
||||
{
|
||||
if (n > _capacity)
|
||||
{
|
||||
_capacity = n;
|
||||
T * d = (T *)malloc(_capacity * sizeof(T));
|
||||
for(int i=0; i<_size; i++)
|
||||
for(size_t i=0; i<_size; i++)
|
||||
{
|
||||
new (d + i)T(move(_data[i]));
|
||||
_data[i].~T();
|
||||
|
@ -191,17 +221,17 @@ void vector<T>::reserve(int n)
|
|||
}
|
||||
|
||||
template <class T>
|
||||
void vector<T>::resize(int n)
|
||||
void vector<T>::resize(size_t n)
|
||||
{
|
||||
if (n < _size)
|
||||
{
|
||||
for(int i=n; i<_size; i++)
|
||||
for(size_t i=n; i<_size; i++)
|
||||
_data[i].~T();
|
||||
_size = n;
|
||||
}
|
||||
else if (n < _capacity)
|
||||
{
|
||||
for(int i=_size; i<n; i++)
|
||||
for(size_t i=_size; i<n; i++)
|
||||
new(_data + i)T;
|
||||
_size = n;
|
||||
}
|
||||
|
@ -219,7 +249,7 @@ void vector<T>::shrink_to_fit(void)
|
|||
{
|
||||
_capacity = _size;
|
||||
T * d = (T *)malloc(_capacity * sizeof(T));
|
||||
for(int i=0; i<_size; i++)
|
||||
for(size_t i=0; i<_size; i++)
|
||||
{
|
||||
new (d + i)T(move(_data[i]));
|
||||
_data[i].~T();
|
||||
|
@ -257,23 +287,46 @@ void vector<T>::emplace_back(const P&... p)
|
|||
}
|
||||
|
||||
template <class T>
|
||||
void vector<T>::insert(int at, const T & t)
|
||||
void vector<T>::insert(size_t at, const T & t)
|
||||
{
|
||||
if (_size == _capacity)
|
||||
reserve(_size + 1 + (_size >> 1));
|
||||
new (_data + _size)T;
|
||||
for(int i=_size; i>at; i--)
|
||||
for(size_t i=_size; i>at; i--)
|
||||
_data[i] = move(_data[i - 1]);
|
||||
_data[at] = t;
|
||||
_size++;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void vector<T>::erase(int at, int n)
|
||||
void vector<T>::erase(size_t at, size_t n)
|
||||
{
|
||||
_size -= n;
|
||||
for(int i=at; i<_size; i++)
|
||||
for(size_t i=at; i<_size; i++)
|
||||
_data[i] = move(_data[i + n]);
|
||||
_data[_size].~T();
|
||||
for(size_t i=0; i<n; i++)
|
||||
_data[_size + i].~T();
|
||||
}
|
||||
|
||||
template <class T>
|
||||
T * vector<T>::insert(T * at, const T & t)
|
||||
{
|
||||
if (_size == _capacity)
|
||||
{
|
||||
unsigned f = unsigned(at) - unsigned(_data);
|
||||
reserve(_size + 1 + (_size >> 1));
|
||||
at = (T *)(f + unsigned(_data));
|
||||
}
|
||||
T * dp = _data + _size;
|
||||
new (dp)T;
|
||||
while (dp != at)
|
||||
{
|
||||
dp--;
|
||||
dp[1] = move(dp[0]);
|
||||
}
|
||||
dp[0] = t;
|
||||
_size++;
|
||||
return dp + 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -2221,6 +2221,8 @@ bool Declaration::IsTemplateSame(const Declaration* dec, const Declaration * tde
|
|||
else
|
||||
return false;
|
||||
}
|
||||
else if (dec->mBase->mType == DT_TYPE_TEMPLATE)
|
||||
return mBase->IsTemplateSame(dec->mBase, tdec);
|
||||
else
|
||||
return this->Stride() == dec->Stride() && mBase->IsTemplateSame(dec->mBase, tdec);
|
||||
}
|
||||
|
|
|
@ -7631,9 +7631,9 @@ void InterCodeBasicBlock::UpdateLocalIntegerRangeSets(const GrowingVariableArray
|
|||
else
|
||||
{
|
||||
if (mLocalValueRange[s0].mMaxState == IntegerValueRange::S_BOUND)
|
||||
mTrueValueRange[s1].LimitMax(mLocalValueRange[s0].mMaxValue - 1);
|
||||
mTrueValueRange[s1].LimitMaxWeak(mLocalValueRange[s0].mMaxValue - 1);
|
||||
if (mLocalValueRange[s0].mMinState == IntegerValueRange::S_BOUND)
|
||||
mFalseValueRange[s1].LimitMin(mLocalValueRange[s0].mMinValue);
|
||||
mFalseValueRange[s1].LimitMinWeak(mLocalValueRange[s0].mMinValue);
|
||||
}
|
||||
break;
|
||||
case IA_CMPLES:
|
||||
|
@ -17434,7 +17434,7 @@ void InterCodeProcedure::Close(void)
|
|||
{
|
||||
GrowingTypeArray tstack(IT_NONE);
|
||||
|
||||
CheckFunc = !strcmp(mIdent->mString, "mod");
|
||||
CheckFunc = !strcmp(mIdent->mString, "main");
|
||||
|
||||
mEntryBlock = mBlocks[0];
|
||||
|
||||
|
|
|
@ -1620,27 +1620,29 @@ Expression* Parser::ParseInitExpression(Declaration* dtype)
|
|||
mErrors->Error(mScanner->mLocation, EERR_CONSTANT_INITIALIZER, "Identifier expected");
|
||||
}
|
||||
|
||||
if (!mdec)
|
||||
if (mdec)
|
||||
{
|
||||
Expression* texp = ParseInitExpression(mdec->mBase);
|
||||
|
||||
Declaration* cdec = CopyConstantInitializer(mdec->mOffset, mdec->mBase, texp);
|
||||
cdec->mBits = mdec->mBits;
|
||||
cdec->mShift = mdec->mShift;
|
||||
|
||||
if (last)
|
||||
last->mNext = cdec;
|
||||
else
|
||||
dec->mParams = cdec;
|
||||
last = cdec;
|
||||
|
||||
if (!ConsumeTokenIf(TK_COMMA))
|
||||
break;
|
||||
|
||||
mdec = mdec->mNext;
|
||||
while (!mdec && path.Size())
|
||||
mdec = path.Pop()->mParams;
|
||||
}
|
||||
else if (!ConsumeTokenIf(TK_COMMA))
|
||||
break;
|
||||
|
||||
Expression* texp = ParseInitExpression(mdec->mBase);
|
||||
|
||||
Declaration* cdec = CopyConstantInitializer(mdec->mOffset, mdec->mBase, texp);
|
||||
cdec->mBits = mdec->mBits;
|
||||
cdec->mShift = mdec->mShift;
|
||||
|
||||
if (last)
|
||||
last->mNext = cdec;
|
||||
else
|
||||
dec->mParams = cdec;
|
||||
last = cdec;
|
||||
|
||||
if (!ConsumeTokenIf(TK_COMMA))
|
||||
break;
|
||||
|
||||
mdec = mdec->mNext;
|
||||
while (!mdec && path.Size())
|
||||
mdec = path.Pop()->mParams;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -4555,7 +4557,7 @@ Declaration* Parser::ParseDeclaration(Declaration * pdec, bool variable, bool ex
|
|||
{
|
||||
if (ndec->mBase->mType == DT_TYPE_FUNCTION)
|
||||
{
|
||||
if (ndec->mFlags & DTF_DEFINED)
|
||||
if ((ndec->mFlags & DTF_DEFINED) && !(ndec->mFlags & DTF_REQUEST_INLINE))
|
||||
mErrors->Error(mScanner->mLocation, EERR_DUPLICATE_DEFINITION, "Duplicate function definition", ndec->mQualIdent);
|
||||
|
||||
ndec->mCompilerOptions = mCompilerOptions;
|
||||
|
|
Loading…
Reference in New Issue