Skip to content

Commit

Permalink
Making valueDeclarations read-only and deriving them from symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed May 3, 2024
1 parent b40a1ce commit cd64e28
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,11 @@ class ScopeManager : ScopeProvider {
// will add it to the underlying AST node as well. This was already done by the
// respective sub-scope manager. We add it directly to the declarations array
// instead.
existing.valueDeclarations.addAll(entry.value.valueDeclarations)
existing.structureDeclarations.addAll(entry.value.structureDeclarations)

// merge symbols
existing.symbols.mergeFrom(entry.value.symbols)

// copy over the typedefs as well just to be sure
existing.typedefs.putAll(entry.value.typedefs)

Expand Down Expand Up @@ -412,7 +414,7 @@ class ScopeManager : ScopeProvider {
}
is ValueDeclaration -> {
val scope = this.firstScopeIsInstanceOrNull<ValueDeclarationScope>()
scope?.addValueDeclaration(declaration, addToAST)
scope?.addDeclaration(declaration, addToAST)
}
is EnumDeclaration,
is RecordDeclaration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ class GlobalScope : StructureDeclarationScope(null) {
fun mergeFrom(others: Collection<GlobalScope>) {
for (other in others) {
structureDeclarations.addAll(other.structureDeclarations)
valueDeclarations.addAll(other.valueDeclarations)
typedefs.putAll(other.typedefs)

// Make sure, the child scopes of the global scope point to the new global scope parent
Expand All @@ -60,10 +59,9 @@ class GlobalScope : StructureDeclarationScope(null) {
}

// Merge symbols lists
for (symbolList in other.symbols) {
val list = symbols.computeIfAbsent(symbolList.key) { mutableListOf() }
list += symbolList.value
symbols.mergeFrom(other.symbols)

for (symbolList in other.symbols) {
// Update the scope property of all nodes that live on the global scope to our new
// global scope (this)
for (symbol in symbolList.value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,11 @@ private fun MutableList<Declaration>.replaceImports(symbol: Symbol) {
}
}
}

/** This function merges in all entries from the [symbolMap] into the current [SymbolMap]. */
fun SymbolMap.mergeFrom(symbolMap: SymbolMap) {
for (entry in symbolMap) {
val list = this.computeIfAbsent(entry.key) { mutableListOf() }
list += entry.value
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ import org.slf4j.LoggerFactory
* Works for if, for, and extends to the block scope
*/
open class ValueDeclarationScope(override var astNode: Node?) : Scope(astNode) {
@Transient var valueDeclarations = mutableListOf<ValueDeclaration>()
val valueDeclarations: List<ValueDeclaration>
get() {
return symbols.flatMap { it.value }.filterIsInstance<ValueDeclaration>()
}

/** A map of typedefs keyed by their alias. */
@Transient val typedefs = mutableMapOf<Type, TypedefDeclaration>()
Expand Down Expand Up @@ -68,8 +71,7 @@ open class ValueDeclarationScope(override var astNode: Node?) : Scope(astNode) {
* @param valueDeclaration the [ValueDeclaration]
* @param addToAST whether to also add the declaration to the AST of its holder.
*/
fun addValueDeclaration(valueDeclaration: ValueDeclaration, addToAST: Boolean) {
valueDeclarations.add(valueDeclaration)
protected fun addValueDeclaration(valueDeclaration: ValueDeclaration, addToAST: Boolean) {
if (addToAST) {
if (astNode is DeclarationHolder) {
val holder = astNode as DeclarationHolder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ class DeclaratorHandler(lang: CXXLanguageFrontend) :
// AST field, (for now) we only want those methods in there, that were actual AST
// parents. This is also something that we need to figure out how we want to handle
// this.
parentScope.valueDeclarations.add(declaration)
parentScope.addSymbol(declaration.symbol, declaration)
} else {
// Add the declaration via the scope manager
frontend.scopeManager.addDeclaration(declaration)
Expand Down

0 comments on commit cd64e28

Please sign in to comment.