Skip to content

Commit

Permalink
More cleanup of SymbolResolver pass
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Sep 16, 2023
1 parent 6ab19f9 commit 572c7bd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ open class Reference : Expression(), HasType.TypeObserver {

override fun toString(): String {
return ToStringBuilder(this, TO_STRING_STYLE)
.append(super.toString())
.appendSuper(super.toString())
.append("refersTo", refersTo)
.toString()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import de.fraunhofer.aisec.cpg.passes.inference.startInference
import de.fraunhofer.aisec.cpg.passes.order.DependsOn
import de.fraunhofer.aisec.cpg.processing.strategy.Strategy
import java.util.*
import java.util.ArrayDeque
import java.util.regex.Pattern
import org.slf4j.Logger
import org.slf4j.LoggerFactory
Expand Down Expand Up @@ -114,7 +113,7 @@ open class SymbolResolver(ctx: TranslationContext) : ComponentPass(ctx) {

walker.strategy = Strategy::EOG_FORWARD
walker.clearCallbacks()
walker.registerHandler(::resolveCalls)
walker.registerHandler(this::resolve)
for (tu in component.translationUnits) {
currentTU = tu
// gather all resolution start holders and their start nodes
Expand Down Expand Up @@ -182,11 +181,7 @@ open class SymbolResolver(ctx: TranslationContext) : ComponentPass(ctx) {
)
}

protected fun resolveReference(
currentClass: RecordDeclaration?,
parent: Node?,
current: Node?
) {
protected fun resolveReference(currentClass: RecordDeclaration?, current: Node?) {
val language = current?.language

if (current !is Reference || current is MemberExpression) return
Expand Down Expand Up @@ -284,7 +279,7 @@ open class SymbolResolver(ctx: TranslationContext) : ComponentPass(ctx) {
}
}

protected fun resolveFieldUsages(curClass: RecordDeclaration?, parent: Node?, current: Node?) {
protected fun resolveMemberExpression(curClass: RecordDeclaration?, current: Node?) {
if (current !is MemberExpression) {
return
}
Expand Down Expand Up @@ -341,35 +336,18 @@ open class SymbolResolver(ctx: TranslationContext) : ComponentPass(ctx) {
base.type = objectType
}
} else {
baseTarget = resolveBase(current.base as Reference)
base.refersTo = baseTarget
}
if (baseTarget is EnumDeclaration) {
val memberTarget =
baseTarget.entries.firstOrNull { it.name.lastPartsMatch(current.name) }
if (memberTarget != null) {
current.refersTo = memberTarget
return
// The base should have been resolved by now. Maybe we have some other clue about
// this base from the type system, so we can set the declaration accordingly.
if (base.refersTo == null) {
base.refersTo = base.type.recordDeclaration
}
} else if (baseTarget is RecordDeclaration) {
current.refersTo = resolveMember(baseTarget.toType(), current)
return
}
}

val baseType = current.base.type.root
current.refersTo = resolveMember(baseType, current)
}

protected fun resolveBase(reference: Reference): Declaration? {
val declaration = scopeManager.resolveReference(reference)
if (declaration != null) {
return declaration
}

return reference.type.recordDeclaration
}

protected fun resolveMember(containingClass: Type, reference: Reference): ValueDeclaration? {
if (isSuperclassReference(reference)) {
// if we have a "super" on the member side, this is a member call. We need to resolve
Expand Down Expand Up @@ -493,34 +471,13 @@ open class SymbolResolver(ctx: TranslationContext) : ComponentPass(ctx) {
)
}

protected fun resolveCalls(currClass: RecordDeclaration?, parent: Node?, node: Node?) {
protected fun resolve(node: Node?, currClass: RecordDeclaration?) {
when (node) {
is TranslationUnitDeclaration -> {
currentTU = node
}
is MemberExpression -> {
resolveFieldUsages(currClass, parent, node)
}
is Reference -> {
resolveReference(currClass, parent, node)
}
is ConstructorCallExpression -> {
handleConstructorCallExpression(node)
}
is ConstructExpression -> {
// We might have call expressions inside our arguments, so in order to correctly
// resolve this call's signature, we need to make sure any call expression arguments
// are fully resolved
handleArguments(node)
handleConstructExpression(node)
}
is CallExpression -> {
// We might have call expressions inside our arguments, so in order to correctly
// resolve this call's signature, we need to make sure any call expression arguments
// are fully resolved
handleArguments(node)
handleCallExpression(scopeManager.currentRecord, node)
}
is MemberExpression -> resolveMemberExpression(currClass, node)
is Reference -> resolveReference(currClass, node)
is ConstructorCallExpression -> handleConstructorCallExpression(node)
is ConstructExpression -> handleConstructExpression(node)
is CallExpression -> handleCallExpression(scopeManager.currentRecord, node)
}
}

Expand Down Expand Up @@ -625,25 +582,6 @@ open class SymbolResolver(ctx: TranslationContext) : ComponentPass(ctx) {
}
}

protected fun handleArguments(call: CallExpression) {
val worklist: Deque<Node> = ArrayDeque()
call.arguments.forEach { worklist.push(it) }
while (worklist.isNotEmpty()) {
val curr = worklist.pop()
if (curr is CallExpression) {
resolveCalls(null, null, curr)
} else {
val it = Strategy.AST_FORWARD(curr)
while (it.hasNext()) {
val astChild = it.next()
if (astChild !is RecordDeclaration) {
worklist.push(astChild)
}
}
}
}
}

/**
* Resolves a [CallExpression.callee] of type [Reference] to a possible list of
* [FunctionDeclaration] nodes.
Expand Down

0 comments on commit 572c7bd

Please sign in to comment.