From fa9aa9c2bb2ef1f165e1a9ad9dc49f36a659d1cd Mon Sep 17 00:00:00 2001 From: drmortalwombat <90205530+drmortalwombat@users.noreply.github.com> Date: Thu, 29 Jun 2023 16:34:38 +0200 Subject: [PATCH] Visibility in base classes --- oscar64/Parser.cpp | 31 ++++++++++++++++++++++++------- oscar64/Parser.h | 2 +- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/oscar64/Parser.cpp b/oscar64/Parser.cpp index 4718930..4f39d9f 100644 --- a/oscar64/Parser.cpp +++ b/oscar64/Parser.cpp @@ -99,6 +99,15 @@ Declaration* Parser::ParseStructDeclaration(uint64 flags, DecType dt) if ((mCompilerOptions & COPT_CPLUSPLUS) && mScanner->mToken == TK_COLON) { mScanner->NextToken(); + uint64 pflags = flags; + + if (ConsumeTokenIf(TK_PUBLIC)) + pflags &= ~(DTF_PRIVATE | DTF_PROTECTED); + else if (ConsumeTokenIf(TK_PROTECTED)) + pflags = (pflags & ~DTF_PRIVATE) | DTF_PROTECTED; + else if (ConsumeTokenIf(TK_PRIVATE)) + pflags |= DTF_PRIVATE | DTF_PROTECTED; + Declaration* pdec = ParseQualIdent(); if (pdec) { @@ -106,6 +115,7 @@ Declaration* Parser::ParseStructDeclaration(uint64 flags, DecType dt) { Declaration* bcdec = new Declaration(mScanner->mLocation, DT_BASECLASS); bcdec->mBase = pdec; + bcdec->mFlags = pflags; dec->mSize = pdec->mSize; @@ -496,7 +506,7 @@ Declaration* Parser::ParseBaseTypeDeclaration(uint64 flags) dec = ParseStructDeclaration(flags, DT_TYPE_STRUCT); break; case TK_CLASS: - dec = ParseStructDeclaration(flags | DTF_PRIVATE, DT_TYPE_STRUCT); + dec = ParseStructDeclaration(flags | DTF_PRIVATE | DTF_PROTECTED, DT_TYPE_STRUCT); break; case TK_UNION: dec = ParseStructDeclaration(flags, DT_TYPE_UNION); @@ -3969,19 +3979,22 @@ Expression* Parser::ParseSimpleExpression(bool lhs) return exp; } -Declaration* Parser::MemberLookup(Declaration* dtype, const Ident* ident, int & offset) +Declaration* Parser::MemberLookup(Declaration* dtype, const Ident* ident, int & offset, uint64& flags) { Declaration* mdec = dtype->mScope->Lookup(ident); offset = 0; + flags = 0; if (!mdec) { Declaration* bcdec = dtype->mBase; - + while (bcdec) { - int noffset; - Declaration* ndec = MemberLookup(bcdec->mBase, ident, noffset); + int noffset; + uint64 nflags; + + Declaration* ndec = MemberLookup(bcdec->mBase, ident, noffset, nflags); if (ndec) { if (mdec) @@ -3990,11 +4003,14 @@ Declaration* Parser::MemberLookup(Declaration* dtype, const Ident* ident, int & { mdec = ndec; offset = noffset + bcdec->mOffset; + flags = nflags | bcdec->mFlags; } } bcdec = bcdec->mNext; } } + else + flags = mdec->mFlags; return mdec; } @@ -4014,11 +4030,12 @@ Expression* Parser::ParseQualify(Expression* exp) if (mScanner->mToken == TK_IDENT) { int moffset; - Declaration* mdec = MemberLookup(dtype, mScanner->mTokenIdent, moffset); + uint64 mflags; + Declaration* mdec = MemberLookup(dtype, mScanner->mTokenIdent, moffset, mflags); if (mdec) { - if (mdec->mFlags & DTF_PROTECTED) + if (mflags & DTF_PROTECTED) { if (!mThisPointer || mThisPointer->mBase->IsConstSame(dtype)) mErrors->Error(mScanner->mLocation, EERR_OBJECT_NOT_FOUND, "Struct member identifier not visible", mScanner->mTokenIdent); diff --git a/oscar64/Parser.h b/oscar64/Parser.h index 0ff0b9c..b0515fe 100644 --- a/oscar64/Parser.h +++ b/oscar64/Parser.h @@ -82,7 +82,7 @@ protected: Expression* ParseStatement(void); Expression* ParseSwitchStatement(void); - Declaration* MemberLookup(Declaration* dtype, const Ident * ident, int& offset); + Declaration* MemberLookup(Declaration* dtype, const Ident * ident, int& offset, uint64 & flags); Expression* ParseQualify(Expression * exp);