Skip to content

Commit

Permalink
Use currentClass for implicit this (#1468)
Browse files Browse the repository at this point in the history
  • Loading branch information
konradweiss authored Mar 22, 2024
1 parent ed50b24 commit 0ef85e5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ class ScopeManager : ScopeProvider {
/** The current function, according to the scope that is currently active. */
val currentFunction: FunctionDeclaration?
get() = this.firstScopeIsInstanceOrNull<FunctionScope>()?.astNode as? FunctionDeclaration

/**
* The current method in the active scope tree, this ensures that 'this' keywords are mapped
* correctly if a method contains a lambda or other types of function declarations
*/
val currentMethod: MethodDeclaration?
get() =
this.firstScopeOrNull { scope: Scope? -> scope?.astNode is MethodDeclaration }?.astNode
as? MethodDeclaration
/** The current record, according to the scope that is currently active. */
val currentRecord: RecordDeclaration?
get() = this.firstScopeIsInstanceOrNull<RecordScope>()?.astNode as? RecordDeclaration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration
import de.fraunhofer.aisec.cpg.frontends.Handler
import de.fraunhofer.aisec.cpg.frontends.HandlerInterface
import de.fraunhofer.aisec.cpg.graph.*
import de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration
import de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration
import de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration
import de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement
Expand Down Expand Up @@ -771,9 +770,7 @@ class ExpressionHandler(lang: JavaLanguageFrontend) :
private fun createImplicitThis():
de.fraunhofer.aisec.cpg.graph.statements.expressions.Expression {
val base: de.fraunhofer.aisec.cpg.graph.statements.expressions.Expression
val thisType =
(frontend.scopeManager.currentFunction as? MethodDeclaration?)?.receiver?.type
?: unknownType()
val thisType = frontend.scopeManager.currentRecord?.toType() ?: unknownType()
base = newReference("this", thisType).implicit("this")
return base
}
Expand Down

0 comments on commit 0ef85e5

Please sign in to comment.