1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-08 19:48:20 +02:00

File-scoped namespaces

This commit is contained in:
Brian Fiete 2022-04-17 08:40:25 -07:00
parent 2d808ec649
commit f3f2f4c6ec
8 changed files with 76 additions and 18 deletions

View file

@ -2065,7 +2065,7 @@ public:
BfTokenNode* mNamespaceNode; BfTokenNode* mNamespaceNode;
BfIdentifierNode* mNameNode; BfIdentifierNode* mNameNode;
BfBlock* mBlock; BfAstNode* mBody;
}; BF_AST_DECL(BfNamespaceDeclaration, BfAstNode); }; BF_AST_DECL(BfNamespaceDeclaration, BfAstNode);
class BfBinaryOperatorExpression : public BfExpression class BfBinaryOperatorExpression : public BfExpression

View file

@ -9548,7 +9548,7 @@ BF_EXPORT const char* BF_CALLTYPE BfCompiler_GetCollapseRegions(BfCompiler* bfCo
virtual void Visit(BfNamespaceDeclaration* namespaceDeclaration) override virtual void Visit(BfNamespaceDeclaration* namespaceDeclaration) override
{ {
Add(namespaceDeclaration->mNamespaceNode, namespaceDeclaration->mBlock, 'N'); Add(namespaceDeclaration->mNamespaceNode, namespaceDeclaration->mBody, 'N');
BfElementVisitor::Visit(namespaceDeclaration); BfElementVisitor::Visit(namespaceDeclaration);
} }

View file

@ -68,6 +68,7 @@ BfDefBuilder::BfDefBuilder(BfSystem* bfSystem)
mFullHashCtx = NULL; mFullHashCtx = NULL;
mSignatureHashCtx = NULL; mSignatureHashCtx = NULL;
mNamespaceBlockDepth = 0;
} }
BfDefBuilder::~BfDefBuilder() BfDefBuilder::~BfDefBuilder()
@ -2493,11 +2494,43 @@ void BfDefBuilder::Visit(BfUsingModDirective* usingDirective)
mStaticSearch.Add(usingDirective->mTypeRef); mStaticSearch.Add(usingDirective->mTypeRef);
} }
void BfDefBuilder::SetNamespaceState(const NamespaceState& namespaceState)
{
while ((int)mNamespaceSearch.size() > namespaceState.mNamespaceSearchCount)
{
BfAtomComposite& atomComposite = mNamespaceSearch[0];
mSystem->ReleaseAtomComposite(atomComposite);
mNamespaceSearch.RemoveAt(0);
}
mNamespace = namespaceState.mNamespace;
}
void BfDefBuilder::Visit(BfNamespaceDeclaration* namespaceDeclaration) void BfDefBuilder::Visit(BfNamespaceDeclaration* namespaceDeclaration)
{ {
BfAtomComposite prevNamespace = mNamespace; auto blockNode = BfNodeDynCast<BfBlock>(namespaceDeclaration->mBody);
int prevNamespaceSearchCount = (int)mNamespaceSearch.size(); bool isFileLevel = (blockNode == NULL) && (namespaceDeclaration->mBody != NULL);
if (mNamespaceBlockDepth < mFileLevelNamespaceState.mSize)
{
auto& prevNamespaceState = mFileLevelNamespaceState[mNamespaceBlockDepth];
if (prevNamespaceState.mNamespaceSearchCount != -1)
{
SetNamespaceState(prevNamespaceState);
prevNamespaceState.mNamespaceSearchCount = -1;
}
}
NamespaceState namespaceState;
namespaceState.mNamespace = mNamespace;
namespaceState.mNamespaceSearchCount = (int)mNamespaceSearch.size();
if (isFileLevel)
{
while (mNamespaceBlockDepth >= mFileLevelNamespaceState.mSize)
mFileLevelNamespaceState.Add(NamespaceState());
mFileLevelNamespaceState[mNamespaceBlockDepth] = namespaceState;
}
if (namespaceDeclaration->mNameNode == NULL) if (namespaceDeclaration->mNameNode == NULL)
return; return;
@ -2531,14 +2564,15 @@ void BfDefBuilder::Visit(BfNamespaceDeclaration* namespaceDeclaration)
mSystem->ReleaseAtom(namespaceAtom); mSystem->ReleaseAtom(namespaceAtom);
} }
VisitChild(namespaceDeclaration->mBlock); if (blockNode != NULL)
while ((int)mNamespaceSearch.size() > prevNamespaceSearchCount)
{ {
BfAtomComposite& atomComposite = mNamespaceSearch[0]; mNamespaceBlockDepth++;
mSystem->ReleaseAtomComposite(atomComposite); VisitChild(blockNode);
mNamespaceSearch.RemoveAt(0); mNamespaceBlockDepth--;
} }
mNamespace = prevNamespace;
if (!isFileLevel)
SetNamespaceState(namespaceState);
} }
void BfDefBuilder::Visit(BfBlock* block) void BfDefBuilder::Visit(BfBlock* block)

View file

@ -11,6 +11,18 @@ class BfResolvePassData;
class BfDefBuilder : public BfStructuralVisitor class BfDefBuilder : public BfStructuralVisitor
{ {
public:
struct NamespaceState
{
BfAtomComposite mNamespace;
int mNamespaceSearchCount;
NamespaceState()
{
mNamespaceSearchCount = -1;
}
};
public: public:
BfSource* mCurSource; BfSource* mCurSource;
BfSystem* mSystem; BfSystem* mSystem;
@ -27,6 +39,9 @@ public:
Array<BfTypeReference*> mInternalAccessSet; Array<BfTypeReference*> mInternalAccessSet;
HashContext* mFullHashCtx; HashContext* mFullHashCtx;
HashContext* mSignatureHashCtx; HashContext* mSignatureHashCtx;
Array<NamespaceState> mFileLevelNamespaceState;
int mNamespaceBlockDepth;
public: public:
void ParseGenericParams(BfGenericParamsDeclaration* genericParamsDecl, BfGenericConstraintsDeclaration* genericConstraints, Array<BfGenericParamDef*>& genericParams, Array<BfExternalConstraintDef>* externConstraintDefs, int outerGenericSize, bool isInGeneric); void ParseGenericParams(BfGenericParamsDeclaration* genericParamsDecl, BfGenericConstraintsDeclaration* genericConstraints, Array<BfGenericParamDef*>& genericParams, Array<BfExternalConstraintDef>* externConstraintDefs, int outerGenericSize, bool isInGeneric);
@ -45,6 +60,7 @@ public:
void ParseAttributes(BfAttributeDirective* attributes, BfTypeDef* typeDef); void ParseAttributes(BfAttributeDirective* attributes, BfTypeDef* typeDef);
BfMethodDef* CreateMethodDef(BfMethodDeclaration* methodDecl, BfMethodDef* outerMethodDef = NULL); BfMethodDef* CreateMethodDef(BfMethodDeclaration* methodDecl, BfMethodDef* outerMethodDef = NULL);
BfError* Fail(const StringImpl& errorStr, BfAstNode* refNode); BfError* Fail(const StringImpl& errorStr, BfAstNode* refNode);
void SetNamespaceState(const NamespaceState& namespaceState);
public: public:
BfDefBuilder(BfSystem* bfSystem); BfDefBuilder(BfSystem* bfSystem);

View file

@ -1225,7 +1225,7 @@ void BfElementVisitor::Visit(BfNamespaceDeclaration* namespaceDeclaration)
VisitChild(namespaceDeclaration->mNamespaceNode); VisitChild(namespaceDeclaration->mNamespaceNode);
VisitChild(namespaceDeclaration->mNameNode); VisitChild(namespaceDeclaration->mNameNode);
VisitChild(namespaceDeclaration->mBlock); VisitChild(namespaceDeclaration->mBody);
} }
void BfElementVisitor::Visit(BfBlock* block) void BfElementVisitor::Visit(BfBlock* block)

View file

@ -76,7 +76,7 @@ void BfNamespaceVisitor::Visit(BfNamespaceDeclaration* namespaceDeclaration)
if (mResolvePassData->mAutoComplete != NULL) if (mResolvePassData->mAutoComplete != NULL)
mResolvePassData->mAutoComplete->CheckNamespace(namespaceDeclaration->mNameNode, mNamespace); mResolvePassData->mAutoComplete->CheckNamespace(namespaceDeclaration->mNameNode, mNamespace);
mResolvePassData->HandleNamespaceReference(namespaceDeclaration->mNameNode, mNamespace); mResolvePassData->HandleNamespaceReference(namespaceDeclaration->mNameNode, mNamespace);
VisitChild(namespaceDeclaration->mBlock); VisitChild(namespaceDeclaration->mBody);
mNamespace = prevNamespace; mNamespace = prevNamespace;
} }

View file

@ -3008,7 +3008,7 @@ void BfPrinter::Visit(BfNamespaceDeclaration* namespaceDeclaration)
VisitChild(namespaceDeclaration->mNamespaceNode); VisitChild(namespaceDeclaration->mNamespaceNode);
ExpectSpace(); ExpectSpace();
VisitChild(namespaceDeclaration->mNameNode); VisitChild(namespaceDeclaration->mNameNode);
VisitChild(namespaceDeclaration->mBlock); VisitChild(namespaceDeclaration->mBody);
} }
void BfPrinter::DoBlockOpen(BfAstNode* prevNode, BfTokenNode* blockOpen, BfTokenNode* blockClose, bool queue, BlockState& blockState) void BfPrinter::DoBlockOpen(BfAstNode* prevNode, BfTokenNode* blockOpen, BfTokenNode* blockClose, bool queue, BlockState& blockState)

View file

@ -8537,14 +8537,22 @@ BfAstNode* BfReducer::CreateTopLevelObject(BfTokenNode* tokenNode, BfAttributeDi
ReplaceNode(tokenNode, namespaceDeclaration); ReplaceNode(tokenNode, namespaceDeclaration);
MoveNode(identifierNode, namespaceDeclaration); MoveNode(identifierNode, namespaceDeclaration);
auto blockNode = ExpectBlockAfter(namespaceDeclaration); BfAstNode* bodyNode = NULL;
if (blockNode == NULL) BfBlock* blockNode = NULL;
if (auto nextToken = BfNodeDynCast<BfTokenNode>(mVisitorPos.GetNext()))
bodyNode = ExpectTokenAfter(namespaceDeclaration, BfToken_Semicolon);
else
bodyNode = blockNode = ExpectBlockAfter(namespaceDeclaration);
if (bodyNode == NULL)
return namespaceDeclaration; return namespaceDeclaration;
MoveNode(blockNode, namespaceDeclaration); MoveNode(bodyNode, namespaceDeclaration);
namespaceDeclaration->mBlock = blockNode; namespaceDeclaration->mBody = bodyNode;
mCurNamespaceStack.Add(namespaceDeclaration); mCurNamespaceStack.Add(namespaceDeclaration);
HandleTopLevel(namespaceDeclaration->mBlock); if (blockNode != NULL)
HandleTopLevel(blockNode);
mCurNamespaceStack.pop_back(); mCurNamespaceStack.pop_back();
return namespaceDeclaration; return namespaceDeclaration;
} }