Skip to content

Commit

Permalink
move checkNestedFuncReference to expressionssem.d and make private
Browse files Browse the repository at this point in the history
  • Loading branch information
thewilsonator committed Oct 6, 2024
1 parent 20a2fed commit 808cba5
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 84 deletions.
84 changes: 84 additions & 0 deletions compiler/src/dmd/expressionsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -17368,3 +17368,87 @@ private Expression expandInitializer(VarDeclaration vd, Loc loc)
e.loc = loc; // for better error message
return e;
}

/*********************************************
* In the current function 'sc.func', we are calling 'fd'.
* 1. Check to see if the current function can call 'fd' , issue error if not.
* 2. If the current function is not the parent of 'fd' , then add
* the current function to the list of siblings of 'fd' .
* 3. If the current function is a literal, and it's accessing an uplevel scope,
* then mark it as a delegate.
* Returns true if error occurs.
*/
private bool checkNestedFuncReference(FuncDeclaration fd, Scope* sc, const ref Loc loc)
{
//printf("FuncDeclaration::checkNestedFuncReference() %s\n", toPrettyChars());
if (auto fld = fd.isFuncLiteralDeclaration())
{
if (fld.tok == TOK.reserved)
{
fld.tok = TOK.function_;
fld.vthis = null;
}
}
if (!fd.parent || fd.parent == sc.parent)
return false;
if (fd.ident == Id.require || fd.ident == Id.ensure)
return false;
if (!fd.isThis() && !fd.isNested())
return false;
// The current function
FuncDeclaration fdthis = sc.parent.isFuncDeclaration();
if (!fdthis)
return false; // out of function scope
Dsymbol p = fd.toParentLocal();
Dsymbol p2 = fd.toParent2();
// Function literals from fdthis to p must be delegates
ensureStaticLinkTo(fdthis, p);
if (p != p2)
ensureStaticLinkTo(fdthis, p2);
if (!fd.isNested())
return false;

// The function that this function is in
bool checkEnclosing(FuncDeclaration fdv)
{
if (!fdv)
return false;
if (fdv == fdthis)
return false;
//printf("this = %s in [%s]\n", this.toChars(), this.loc.toChars());
//printf("fdv = %s in [%s]\n", fdv .toChars(), fdv .loc.toChars());
//printf("fdthis = %s in [%s]\n", fdthis.toChars(), fdthis.loc.toChars());
// Add this function to the list of those which called us
if (fdthis != fd)
{
bool found = false;
for (size_t i = 0; i < fd.siblingCallers.length; ++i)
{
if (fd.siblingCallers[i] == fdthis)
found = true;
}
if (!found)
{
//printf("\tadding sibling %s to %s\n", fdthis.toPrettyChars(), toPrettyChars());
if (!sc.intypeof && !sc.traitsCompiles)
{
fd.siblingCallers.push(fdthis);
fd.computedEscapingSiblings = false;
}
}
}
const lv = fdthis.getLevelAndCheck(loc, sc, fdv, fd);
if (lv == fd.LevelError)
return true; // error
if (lv == -1)
return false; // downlevel call

Check warning on line 17444 in compiler/src/dmd/expressionsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/expressionsem.d#L17444

Added line #L17444 was not covered by tests
if (lv == 0)
return false; // same level call
return false; // Uplevel call
}
if (checkEnclosing(p.isFuncDeclaration()))
return true;
if (checkEnclosing(p == p2 ? null : p2.isFuncDeclaration()))
return true;

Check warning on line 17452 in compiler/src/dmd/expressionsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/expressionsem.d#L17452

Added line #L17452 was not covered by tests
return false;
}
84 changes: 0 additions & 84 deletions compiler/src/dmd/funcsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -2263,90 +2263,6 @@ bool canInferAttributes(FuncDeclaration fd, Scope* sc)
return false;
}

/*********************************************
* In the current function 'sc.func', we are calling 'fd'.
* 1. Check to see if the current function can call 'fd' , issue error if not.
* 2. If the current function is not the parent of 'fd' , then add
* the current function to the list of siblings of 'fd' .
* 3. If the current function is a literal, and it's accessing an uplevel scope,
* then mark it as a delegate.
* Returns true if error occurs.
*/
bool checkNestedFuncReference(FuncDeclaration fd, Scope* sc, const ref Loc loc)
{
//printf("FuncDeclaration::checkNestedFuncReference() %s\n", toPrettyChars());
if (auto fld = fd.isFuncLiteralDeclaration())
{
if (fld.tok == TOK.reserved)
{
fld.tok = TOK.function_;
fld.vthis = null;
}
}
if (!fd.parent || fd.parent == sc.parent)
return false;
if (fd.ident == Id.require || fd.ident == Id.ensure)
return false;
if (!fd.isThis() && !fd.isNested())
return false;
// The current function
FuncDeclaration fdthis = sc.parent.isFuncDeclaration();
if (!fdthis)
return false; // out of function scope
Dsymbol p = fd.toParentLocal();
Dsymbol p2 = fd.toParent2();
// Function literals from fdthis to p must be delegates
ensureStaticLinkTo(fdthis, p);
if (p != p2)
ensureStaticLinkTo(fdthis, p2);
if (!fd.isNested())
return false;

// The function that this function is in
bool checkEnclosing(FuncDeclaration fdv)
{
if (!fdv)
return false;
if (fdv == fdthis)
return false;
//printf("this = %s in [%s]\n", this.toChars(), this.loc.toChars());
//printf("fdv = %s in [%s]\n", fdv .toChars(), fdv .loc.toChars());
//printf("fdthis = %s in [%s]\n", fdthis.toChars(), fdthis.loc.toChars());
// Add this function to the list of those which called us
if (fdthis != fd)
{
bool found = false;
for (size_t i = 0; i < fd.siblingCallers.length; ++i)
{
if (fd.siblingCallers[i] == fdthis)
found = true;
}
if (!found)
{
//printf("\tadding sibling %s to %s\n", fdthis.toPrettyChars(), toPrettyChars());
if (!sc.intypeof && !sc.traitsCompiles)
{
fd.siblingCallers.push(fdthis);
fd.computedEscapingSiblings = false;
}
}
}
const lv = fdthis.getLevelAndCheck(loc, sc, fdv, fd);
if (lv == fd.LevelError)
return true; // error
if (lv == -1)
return false; // downlevel call
if (lv == 0)
return false; // same level call
return false; // Uplevel call
}
if (checkEnclosing(p.isFuncDeclaration()))
return true;
if (checkEnclosing(p == p2 ? null : p2.isFuncDeclaration()))
return true;
return false;
}

/****************************************************
* Check whether result variable can be built.
* Returns:
Expand Down

0 comments on commit 808cba5

Please sign in to comment.