Fix namespaces for templates

This commit is contained in:
drmortalwombat 2023-08-15 12:46:57 +02:00
parent 50cc2afb52
commit 69b46c4b7b
22 changed files with 89 additions and 32 deletions

View File

@ -3,8 +3,8 @@
int main(void)
{
array<int, 10> a10;
array<int, 20> a20;
opp::array<int, 10> a10;
opp::array<int, 20> a20;
for(int i=0; i<10; i++)
a10[i] = i;

View File

@ -4,6 +4,10 @@
#include <opp/sstream.h>
#include <math.h>
using opp::ostringstream;
using opp::istringstream;
using opp::endl;
float fdist(float a, float b)
{
float d = fabs(a - b);
@ -21,8 +25,6 @@ int main(void)
os << i << endl;
}
costream cout;
istringstream is(os.str());
int j = 0, k = 47;
@ -35,7 +37,7 @@ int main(void)
assert(j == 40);
#endif
os.str(string());
os.str(opp::string());
#if 0
cout << "[" << os.str() << "]" << endl;

View File

@ -3,6 +3,8 @@
#include <assert.h>
#include <string.h>
using opp::string;
static const char HelloWorld[] = "Hello World";
static const char AndBeyond[] = "And Beyond";
static const char And[] = "And";

View File

@ -3,7 +3,7 @@
int main(void)
{
vector<int> a;
opp::vector<int> a;
for(int i=0; i<10; i++)
a.push_back(i);

View File

@ -4,6 +4,9 @@
#include <stdlib.h>
#include <opp/iostream.h>
using opp::string;
using opp::vector;
string join(const vector<string> & vs)
{
string sj;

View File

@ -1,6 +1,8 @@
#ifndef OPP_ALGORITHM_H
#define OPP_ALGORITHM_H
namespace opp {
template <class T>
inline void swap(T & x, T & y)
{
@ -33,5 +35,6 @@ void sort(T s, T e)
}
}
}
#endif

View File

@ -1,6 +1,7 @@
#ifndef OPP_ARRAY_H
#define OPP_ARRAY_H
namespace opp {
template <class T, int n>
class array
@ -110,17 +111,7 @@ public:
}
};
#if 0
void swap(array<T, n> & a)
{
for(int i=0; i<n; i++)
{
T t(_data[i]);
_data[i] = a._data[i];
a._data[i] = t;
}
}
#endif
}
#endif

View File

@ -1,6 +1,8 @@
#include "ifstream.h"
#include <c64/kernalio.h>
namespace opp {
ifstream::ifstream(char fnum, char device, char channel, const string & name)
{
this->fnum = fnum;
@ -19,3 +21,4 @@ void ifstream::refill(void)
mBufferFill = krnio_read(fnum, mBuffer, 32);
}
}

View File

@ -4,6 +4,7 @@
#include "iostream.h"
#include "string.h"
namespace opp {
class ifstream : public istream
{
@ -17,7 +18,7 @@ protected:
char fnum;
};
}
#pragma compile("ifstream.cpp")

View File

@ -2,6 +2,8 @@
#include <math.h>
#include <stdio.h>
namespace opp {
ios::ios(void)
: mFlags(0), mState(0), mWidth(0), mPrecision(6), mFill(' ')
{}
@ -975,3 +977,5 @@ void cistream::refill(void)
cistream cin;
costream cout;
}

View File

@ -3,6 +3,8 @@
#include <opp/string.h>
namespace opp {
class ios
{
public:
@ -205,6 +207,8 @@ ostream & operator<<(ostream & os, const iosetfill & s);
extern cistream cin;
extern costream cout;
}
#pragma compile("iostream.cpp");
#endif

View File

@ -1,10 +1,14 @@
#ifndef OPP_MOVE_H
#define OPP_MOVE_H
namespace opp {
template <class T>
T && move(T & m)
{
return (T &&)m;
}
}
#endif

View File

@ -1,6 +1,8 @@
#include "ofstream.h"
#include <c64/kernalio.h>
namespace opp {
ofstream::ofstream(char fnum, char device, char channel, const string & name)
{
this->fnum = fnum;
@ -27,3 +29,4 @@ void ofstream::bput(char ch)
}
}
}

View File

@ -4,6 +4,7 @@
#include "iostream.h"
#include "string.h"
namespace opp {
class ofstream : public ostream
{
@ -20,7 +21,7 @@ protected:
char fnum;
};
}
#pragma compile("ofstream.cpp")

View File

@ -1,6 +1,8 @@
#include "sstream.h"
#include <stdlib.h>
namespace opp {
ostringstream::ostringstream(void)
{
mBuffer = nullptr;
@ -80,3 +82,5 @@ void istringstream::refill(void)
mBuffer[mBufferFill++] = mString[mSPos++];
}
}
}

View File

@ -3,6 +3,8 @@
#include "iostream.h"
namespace opp {
class ostringstream : public ostream
{
public:
@ -33,6 +35,7 @@ protected:
char mSPos;
};
}
#pragma compile("sstream.cpp")

View File

@ -2,6 +2,8 @@
#include <string.h>
#include <stdlib.h>
namespace opp {
static inline void smemcpy(char * dp, const char * sp, char s)
{
for(char i=0; i<s; i++)
@ -570,3 +572,5 @@ int string::find(char c, char pos) const
return -1;
}
}

View File

@ -1,6 +1,8 @@
#ifndef OPP_STRING_H
#define OPP_STRING_H
namespace opp {
class string
{
private:
@ -74,6 +76,8 @@ protected:
void swap(string & u, string & v);
}
#pragma compile("string.cpp")
#endif

View File

@ -4,6 +4,8 @@
#include <stdlib.h>
#include <opp/move.h>
namespace opp {
template <class T>
class vector
{
@ -237,4 +239,5 @@ void vector<T>::erase(int at, int n)
_data[_size].~T();
}
}
#endif

View File

@ -121,11 +121,14 @@ Declaration* DeclarationScope::Lookup(const Ident* ident, ScopeLevel limit)
for (int i = 0; i < mUsed.Size(); i++)
{
Declaration* dec = mUsed[i]->Lookup(ident, limit);
Declaration* dec = mUsed[i]->Lookup(ident, SLEVEL_NAMESPACE);
if (dec)
return dec;
}
if (limit == SLEVEL_USING)
return nullptr;
return mParent ? mParent->Lookup(ident, limit) : nullptr;
}

View File

@ -118,6 +118,7 @@ class Declaration;
enum ScopeLevel
{
SLEVEL_SCOPE,
SLEVEL_USING,
SLEVEL_GLOBAL,
SLEVEL_STATIC,
SLEVEL_NAMESPACE,

View File

@ -166,7 +166,7 @@ Declaration* Parser::ParseStructDeclaration(uint64 flags, DecType dt)
{
dec->mIdent = structName;
dec->mQualIdent = mScope->Mangle(structName);
dec->mScope = new DeclarationScope(nullptr, SLEVEL_CLASS, structName);
dec->mScope = new DeclarationScope(nullptr, SLEVEL_CLASS, dec->mQualIdent);
}
if ((mCompilerOptions & COPT_CPLUSPLUS) && mScanner->mToken == TK_COLON)
@ -607,7 +607,7 @@ Declaration* Parser::ParseBaseTypeDeclaration(uint64 flags, bool qualified)
if (dec && dec->mTemplate)
dec = ParseTemplateExpansion(dec->mTemplate, nullptr);
while (qualified && dec && dec->mType == DT_TYPE_STRUCT && ConsumeTokenIf(TK_COLCOLON))
while (qualified && dec && (dec->mType == DT_TYPE_STRUCT || dec->mType == DT_NAMESPACE) && ConsumeTokenIf(TK_COLCOLON))
{
if (ExpectToken(TK_IDENT))
{
@ -3797,7 +3797,7 @@ Declaration* Parser::ParseDeclaration(Declaration * pdec, bool variable, bool ex
{
pdec = mCompilationUnits->mScope->Insert(ndec->mQualIdent, ndec);
if (ndec->mIdent == ndec->mQualIdent)
if (mScope->Mangle(ndec->mIdent) == ndec->mQualIdent)
{
Declaration* ldec = mScope->Insert(ndec->mIdent, pdec ? pdec : ndec);
#if 0
@ -4290,7 +4290,7 @@ Declaration* Parser::ParseQualIdent(void)
{
if (dec->mType == DT_NAMESPACE || dec->mType == DT_TYPE_STRUCT)
{
Declaration* ndec = dec->mScope->Lookup(mScanner->mTokenIdent, SLEVEL_CLASS);
Declaration* ndec = dec->mScope->Lookup(mScanner->mTokenIdent, SLEVEL_USING);
if (ndec)
dec = ndec;
@ -7451,7 +7451,7 @@ Declaration* Parser::ParseTemplateExpansion(Declaration* tmpld, Declaration* exp
{
if (!(mdec->mFlags & DTF_DEFINED))
{
Declaration* mpdec = mScope->Lookup(tmpld->mScope->Mangle(mdec->mIdent));
Declaration* mpdec = mCompilationUnits->mScope->Lookup(tmpld->mScope->Mangle(mdec->mIdent));
while (mpdec && !mdec->mBase->IsTemplateSameParams(mpdec->mBase, tdec))
mpdec = mpdec->mNext;
if (mpdec && mpdec->mTemplate)
@ -7485,7 +7485,7 @@ void Parser::CompleteTemplateExpansion(Declaration* tmpld)
{
while (mdec)
{
Declaration* mpdec = mScope->Lookup(mdec->mQualIdent);
Declaration* mpdec = mCompilationUnits->mScope->Lookup(mdec->mQualIdent);
while (mpdec && !mpdec->IsSameParams(mdec->mParams))
mpdec = mpdec->mNext;
if (mpdec && mpdec->mType == DT_TEMPLATE)
@ -7569,7 +7569,7 @@ void Parser::ParseTemplateDeclaration(void)
if (mScanner->mToken == TK_IDENT)
{
bdec->mIdent = mScanner->mTokenIdent;
bdec->mQualIdent = mScope->Mangle(bdec->mIdent);
bdec->mQualIdent = mScope->Mangle(mScanner->mTokenIdent);
while (mScanner->mToken != TK_SEMICOLON && mScanner->mToken != TK_OPEN_BRACE)
mScanner->NextToken();
@ -7631,7 +7631,12 @@ void Parser::ParseTemplateDeclaration(void)
}
}
else
{
mErrors->Error(bdec->mLocation, EERR_FUNCTION_TEMPLATE, "Function template expected");
adec->mType = DT_TYPE_VOID;
tdec->mBase = adec;
adec->mTemplate = tdec;
}
}
tdec->mTokens = mScanner->CompleteRecord();
@ -7640,14 +7645,15 @@ void Parser::ParseTemplateDeclaration(void)
tdec->mQualIdent = tdec->mBase->mQualIdent;
tdec->mScope->mName = tdec->mQualIdent;
Declaration * pdec = mScope->Insert(tdec->mQualIdent, tdec->mBase);
if (tdec->mQualIdent == mScope->Mangle(tdec->mIdent))
mScope->Insert(tdec->mIdent, tdec->mBase);
Declaration* pdec = mCompilationUnits->mScope->Insert(tdec->mQualIdent, tdec->mBase);
if (pdec)
{
tdec->mBase->mNext = pdec->mNext;
pdec->mNext = tdec->mBase;
}
mCompilationUnits->mScope->Insert(tdec->mQualIdent, tdec->mBase);
}
@ -9180,8 +9186,11 @@ void Parser::ParseNamespace(void)
if (!ns)
{
ns = new Declaration(mScanner->mLocation, DT_NAMESPACE);
mScope->Insert(ident, ns);
ns->mScope = new DeclarationScope(mScope, SLEVEL_NAMESPACE, ident);
ns->mIdent = ident;
ns->mQualIdent = mScope->Mangle(ident);
mScope->Insert(ident, ns);
}
mScanner->NextToken();
@ -9198,6 +9207,11 @@ void Parser::ParseNamespace(void)
}
else if (mScanner->mToken == TK_SEMICOLON)
mScanner->NextToken();
else if (mScanner->mToken == TK_TEMPLATE)
{
mScanner->NextToken();
ParseTemplateDeclaration();
}
else if (mScanner->mToken == TK_NAMESPACE)
{
mScanner->NextToken();