Skip to content

Commit

Permalink
Blocked because of nested namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Nov 21, 2024
1 parent 3163354 commit c781e33
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -265,32 +265,41 @@ class ImportResolver(ctx: TranslationContext) : ComponentPass(ctx) {
}

private fun handleImportDeclaration(import: ImportDeclaration) {
// We always need to search at the global scope because we are "importing" something, so by
// definition, this is not in the scope of the current file.
val scope = scopeManager.globalScope ?: return

// Let's do some importing. We need to import either a wildcard
if (import.wildcardImport) {
val list = scopeManager.lookupSymbolByName(import.import, import.location, scope)
val symbol = list.singleOrNull()
if (symbol != null) {
// In this case, the symbol must point to a name scope
val symbolScope = scopeManager.lookupScope(symbol)
if (symbolScope is NameScope) {
import.importedSymbols = symbolScope.symbols
}
}
} else {
// or a symbol directly
val list =
scopeManager
.lookupSymbolByName(import.import, import.location, scope)
.toMutableList()
import.importedSymbols = mutableMapOf(import.symbol to list)
}
import.updateImportedSymbols()
}

override fun cleanup() {
// Nothing to do
}
}

/**
* This function updates the [ImportDeclaration.importedSymbols]. This is done once at the beginning
* by the [ImportResolver]. However, we need to update this list once we infer new symbols in
* namespaces that are imported at a later stage (e.g., in the [TypeResolver]), otherwise they won't
* be visible to the later passes.
*/
context(Pass<*>)
fun ImportDeclaration.updateImportedSymbols() {
// We always need to search at the global scope because we are "importing" something, so by
// definition, this is not in the scope of the current file.
val scope = scopeManager.globalScope ?: return

if (this.wildcardImport) {
val list = scopeManager.lookupSymbolByName(this.import, this.location, scope)
val symbol = list.singleOrNull()
if (symbol != null) {
// In this case, the symbol must point to a name scope
val symbolScope = scopeManager.lookupScope(symbol)
if (symbolScope is NameScope) {
this.importedSymbols = symbolScope.symbols
}
}
} else {
// or a symbol directly
val list =
scopeManager.lookupSymbolByName(this.import, this.location, scope).toMutableList()
this.importedSymbols = mutableMapOf(this.symbol to list)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import de.fraunhofer.aisec.cpg.ScopeManager
import de.fraunhofer.aisec.cpg.TranslationContext
import de.fraunhofer.aisec.cpg.TypeManager
import de.fraunhofer.aisec.cpg.graph.*
import de.fraunhofer.aisec.cpg.graph.declarations.ImportDeclaration
import de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration
import de.fraunhofer.aisec.cpg.graph.types.DeclaresType
import de.fraunhofer.aisec.cpg.graph.types.ObjectType
Expand Down Expand Up @@ -140,6 +141,9 @@ open class TypeResolver(ctx: TranslationContext) : ComponentPass(ctx) {
t.recordDeclaration = node
}
}
} else if (node is ImportDeclaration) {
// Update the imports, as they might have changed because of inference
node.updateImportedSymbols()
}
}

Expand Down

0 comments on commit c781e33

Please sign in to comment.