Skip to content

Commit bbcad1c

Browse files
committed
Removing old 'resolve' function
1 parent dd94874 commit bbcad1c

File tree

3 files changed

+6
-84
lines changed

3 files changed

+6
-84
lines changed

cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/ScopeManager.kt

-82
Original file line numberDiff line numberDiff line change
@@ -630,88 +630,6 @@ class ScopeManager : ScopeProvider {
630630
return ret
631631
}
632632

633-
/**
634-
* Traverses the scope upwards and looks for declarations of type [T] which matches the
635-
* condition [predicate].
636-
*
637-
* It returns a list of all declarations that match the predicate, ordered by reachability in
638-
* the scope stack. This means that "local" declarations will be in the list first, global items
639-
* will be last.
640-
*
641-
* @param searchScope the scope to start the search in
642-
* @param predicate predicate the element must match to
643-
* @param <T>
644-
*/
645-
internal inline fun <reified T : Declaration> resolve(
646-
searchScope: Scope?,
647-
stopIfFound: Boolean = false,
648-
noinline predicate: (T) -> Boolean
649-
): List<T> {
650-
return resolve(T::class.java, searchScope, stopIfFound, predicate)
651-
}
652-
653-
internal fun <T : Declaration> resolve(
654-
klass: Class<T>,
655-
searchScope: Scope?,
656-
stopIfFound: Boolean = false,
657-
predicate: (T) -> Boolean
658-
): List<T> {
659-
var scope = searchScope
660-
val declarations = mutableListOf<T>()
661-
662-
while (scope != null) {
663-
if (scope is ValueDeclarationScope) {
664-
declarations.addAll(
665-
scope.valueDeclarations.filterIsInstance(klass).filter(predicate)
666-
)
667-
}
668-
669-
if (scope is StructureDeclarationScope) {
670-
var list = scope.structureDeclarations.filterIsInstance(klass).filter(predicate)
671-
672-
// this was taken over from the old resolveStructureDeclaration.
673-
// TODO(oxisto): why is this only when the list is empty?
674-
if (list.isEmpty()) {
675-
for (declaration in scope.structureDeclarations) {
676-
if (declaration is RecordDeclaration) {
677-
list = declaration.templates.filterIsInstance(klass).filter(predicate)
678-
}
679-
}
680-
}
681-
682-
declarations.addAll(list)
683-
}
684-
685-
// some (all?) languages require us to stop immediately if we found something on this
686-
// scope. This is the case where function overloading is allowed, but only within the
687-
// same scope
688-
if (stopIfFound && declarations.isNotEmpty()) {
689-
return declarations
690-
}
691-
692-
// go upwards in the scope tree
693-
scope = scope.parent
694-
}
695-
696-
return declarations
697-
}
698-
699-
/**
700-
* Resolves function templates of the given [CallExpression].
701-
*
702-
* @param scope where we are searching for the FunctionTemplateDeclarations
703-
* @param call CallExpression we want to resolve an invocation target for
704-
* @return List of FunctionTemplateDeclaration that match the name provided in the
705-
* CallExpression and therefore are invocation candidates
706-
*/
707-
@JvmOverloads
708-
fun resolveFunctionTemplateDeclaration(
709-
call: CallExpression,
710-
scope: Scope? = currentScope
711-
): List<FunctionTemplateDeclaration> {
712-
return resolve(scope, true) { c -> c.name.lastPartsMatch(call.name) }
713-
}
714-
715633
/**
716634
* Retrieves the [RecordDeclaration] for the given name in the given scope.
717635
*

cpg-language-cxx/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/cxx/CPPLanguage.kt

+5-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,11 @@ open class CPPLanguage :
244244
needsExactMatch: Boolean
245245
): Pair<Boolean, List<FunctionDeclaration>> {
246246
val instantiationCandidates =
247-
ctx.scopeManager.resolveFunctionTemplateDeclaration(templateCall)
247+
ctx.scopeManager
248+
.lookupSymbolByName(templateCall.name, templateCall.location, templateCall.scope) {
249+
it is FunctionTemplateDeclaration
250+
}
251+
.filterIsInstance<FunctionTemplateDeclaration>()
248252
for (functionTemplateDeclaration in instantiationCandidates) {
249253
val initializationType =
250254
mutableMapOf<Node?, TemplateDeclaration.TemplateInitialization?>()

cpg-language-cxx/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/cxx/DeclarationHandler.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,6 @@ class DeclarationHandler(lang: CXXLanguageFrontend) :
268268
}
269269

270270
templateDeclaration.location = frontend.locationOf(ctx)
271-
frontend.scopeManager.addDeclaration(templateDeclaration)
272271
frontend.scopeManager.enterScope(templateDeclaration)
273272
addTemplateParameters(ctx, templateDeclaration)
274273

@@ -284,6 +283,7 @@ class DeclarationHandler(lang: CXXLanguageFrontend) :
284283
}
285284

286285
addRealizationToScope(templateDeclaration)
286+
frontend.scopeManager.addDeclaration(templateDeclaration)
287287

288288
return templateDeclaration
289289
}

0 commit comments

Comments
 (0)