Skip to content

Commit

Permalink
Move newScope from dclass to include visitor in dsymbolsem
Browse files Browse the repository at this point in the history
  • Loading branch information
dchidindu5 committed Oct 29, 2024
1 parent 4f8866c commit cd775d0
Showing 1 changed file with 156 additions and 0 deletions.
156 changes: 156 additions & 0 deletions compiler/src/dmd/dsymbolsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -7482,7 +7482,163 @@ private extern(C++) class NewScopeVisitor : Visitor
}
sc = sc2;
}

override void visit(ClassDeclaration cld)
{
auto sc2 = cld.newScope(sc);
if (cld.isCOMclass())

Check warning on line 7489 in compiler/src/dmd/dsymbolsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/dsymbolsem.d#L7488-L7489

Added lines #L7488 - L7489 were not covered by tests
{
/* This enables us to use COM objects under Linux and
* work with things like XPCOM
*/
sc2.linkage = target.systemLinkage();

Check warning on line 7494 in compiler/src/dmd/dsymbolsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/dsymbolsem.d#L7494

Added line #L7494 was not covered by tests
}
sc = sc2;

Check warning on line 7496 in compiler/src/dmd/dsymbolsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/dsymbolsem.d#L7496

Added line #L7496 was not covered by tests
}

override void visit(InterfaceDeclaration ifd)
{
auto sc2 = ifd.newScope(sc);
if (ifd.com)
sc2.linkage = LINK.windows;
else if (ifd.classKind == ClassKind.cpp)
sc2.linkage = LINK.cpp;
else if (ifd.classKind == ClassKind.objc)
sc2.linkage = LINK.objc;
sc = sc2;

Check warning on line 7508 in compiler/src/dmd/dsymbolsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/dsymbolsem.d#L7501-L7508

Added lines #L7501 - L7508 were not covered by tests
}
}

extern(C++) Dsymbols* include(Dsymbol d, Scope* sc)
{
scope icv = new IncludeVisitor(sc);
d.accept(icv);
return icv.symbols;
}

extern(C++) class IncludeVisitor : Visitor
{
alias visit = typeof(super).visit;
Scope* sc;
Dsymbols* symbols;
this(Scope* sc)
{
this.sc = sc;
}

override void visit(AttribDeclaration ad)
{
if (ad.errors)
{
symbols = null;
return;
}
symbols = ad.decl;
return;
}

// Decide if 'then' or 'else' code should be included
override void visit(ConditionalDeclaration cdc)
{
//printf("ConditionalDeclaration::include(sc = %p) scope = %p\n", sc, _scope);

if (cdc.errors)
{
symbols = null;
return;
}
assert(cdc.condition);
symbols = cdc.condition.include(cdc._scope ? cdc._scope : sc) ? cdc.decl : cdc.elsedecl;
}

override void visit(StaticIfDeclaration sif)
{
/****************************************
* Different from other AttribDeclaration subclasses, include() call requires
* the completion of addMember and setScope phases.
*/
//printf("StaticIfDeclaration::include(sc = %p) scope = %p\n", sc, _scope);
if (sif.errors || sif.onStack)
{
symbols = null;
return;
}
sif.onStack = true;
scope(exit) sif.onStack = false;

if (sc && sif.condition.inc == Include.notComputed)
{
assert(sif.scopesym); // addMember is already done
assert(sif._scope); // setScope is already done

Scope* saved_scope = sc;
sc = sif._scope;
visit(cast(ConditionalDeclaration) sif);
Dsymbols* d = symbols;
sc = saved_scope;

if (d && !sif.addisdone)
{
// Add members lazily.
d.foreachDsymbol( s => s.addMember(sif._scope, sif.scopesym) );

// Set the member scopes lazily.
d.foreachDsymbol( s => s.setScope(sif._scope) );

sif.addisdone = true;
}
symbols = d;
return;
}
else
{
visit(cast(ConditionalDeclaration)sif);
}
}

override void visit(StaticForeachDeclaration sfd)
{
if (sfd.errors || sfd.onStack)
{
symbols = null;
return;
}
if (sfd.cached)
{
assert(!sfd.onStack);
symbols = sfd.cache;
return;
}
sfd.onStack = true;
scope(exit) sfd.onStack = false;

if (sfd._scope)
{
sfd.sfe.prepare(sfd._scope); // lower static foreach aggregate
}
if (!sfd.sfe.ready())
{
symbols = null;// TODO: ok?
return;
}

// expand static foreach
import dmd.statementsem: makeTupleForeach;
Dsymbols* d = makeTupleForeach(sfd._scope, true, true, sfd.sfe.aggrfe, sfd.decl, sfd.sfe.needExpansion).decl;
if (d) // process generated declarations
{
// Add members lazily.
d.foreachDsymbol( s => s.addMember(sfd._scope, sfd.scopesym) );

// Set the member scopes lazily.
d.foreachDsymbol( s => s.setScope(sfd._scope) );
}
sfd.cached = true;
sfd.cache = d;
symbols = d;
}
}

/**
* Called from a symbol's semantic to check if `gnuAbiTag` UDA
* can be applied to them
Expand Down

0 comments on commit cd775d0

Please sign in to comment.