@@ -370,19 +370,19 @@ class ScopeManager : ScopeProvider {
370
370
is ProblemDeclaration ,
371
371
is IncludeDeclaration -> {
372
372
// directly add problems and includes to the global scope
373
- this .globalScope?.addDeclaration(declaration, addToAST)
373
+ this .globalScope?.addDeclaration(declaration, addToAST, this )
374
374
}
375
375
is ValueDeclaration -> {
376
376
val scope = this .firstScopeIsInstanceOrNull<ValueDeclarationScope >()
377
- scope?.addDeclaration(declaration, addToAST)
377
+ scope?.addDeclaration(declaration, addToAST, this )
378
378
}
379
379
is ImportDeclaration ,
380
380
is EnumDeclaration ,
381
381
is RecordDeclaration ,
382
382
is NamespaceDeclaration ,
383
383
is TemplateDeclaration -> {
384
384
val scope = this .firstScopeIsInstanceOrNull<StructureDeclarationScope >()
385
- scope?.addDeclaration(declaration, addToAST)
385
+ scope?.addDeclaration(declaration, addToAST, this )
386
386
}
387
387
}
388
388
}
@@ -571,52 +571,52 @@ class ScopeManager : ScopeProvider {
571
571
* the given [Name]. It also does this recursively.
572
572
*/
573
573
fun resolveParentAlias (name : Name , scope : Scope ? ): Name {
574
- var parentName = name.parent ? : return name
575
- parentName = resolveParentAlias(parentName, scope)
574
+ if (name.parent == null ) {
575
+ return name
576
+ }
576
577
577
- // Build a new name based on the eventual resolved parent alias
578
- var newName =
579
- if (parentName != name.parent) {
580
- Name (name.localName, parentName, delimiter = name.delimiter)
581
- } else {
582
- name
583
- }
584
- var decl =
585
- scope?.lookupSymbol(parentName.localName)?.singleOrNull {
586
- it is NamespaceDeclaration || it is RecordDeclaration
587
- }
588
- if (decl != null && parentName != decl.name) {
578
+ val parentName = resolveParentAlias(name.parent, scope)
579
+
580
+ // Look for an alias in the current scope. This is also resolves partial FQNs to their full
581
+ // FQN
582
+ var newScope =
583
+ scope
584
+ ?.lookupSymbol(parentName.localName) {
585
+ it is NamespaceDeclaration || it is RecordDeclaration
586
+ }
587
+ ?.map { nameScopeMap[it.name] }
588
+ ?.toSet()
589
+ ?.singleOrNull()
590
+ if (newScope != null ) {
589
591
// This is probably an already resolved alias so, we take this one
590
- return Name (newName.localName, decl.name, delimiter = newName.delimiter )
592
+ return adjustNameIfNecessary(newScope.name, parentName, name )
591
593
}
592
594
593
595
// Some special handling of typedefs; this should somehow be merged with the above but not
594
596
// exactly sure how. The issue is that we cannot take the "name" of the typedef declaration,
595
597
// but we rather want its original type name.
596
598
// TODO: This really needs to be handled better somehow, maybe a common interface for
597
599
// typedefs, namespaces and records that return the correct name?
598
- decl = scope?.lookupSymbol(parentName.localName)?.singleOrNull { it is TypedefDeclaration }
600
+ val decl =
601
+ scope?.lookupSymbol(parentName.localName)?.singleOrNull { it is TypedefDeclaration }
599
602
if ((decl as ? TypedefDeclaration ) != null ) {
600
- return Name (newName.localName, decl.type.name, delimiter = newName.delimiter )
603
+ return adjustNameIfNecessary( decl.type.name, parentName, name )
601
604
}
602
605
603
- // If we do not have a match yet, it could be that we are trying to resolve an FQN type
604
- // during frontend translation. This is deprecated and will be replaced in the future
605
- // by a system that also resolves type during symbol resolving. However, to support aliases
606
- // from imports in this intermediate stage, we have to look for unresolved import
607
- // declarations and also take their aliases into account
608
- decl =
609
- scope
610
- ?.lookupSymbol(parentName.localName)
611
- ?.filterIsInstance<ImportDeclaration >()
612
- ?.singleOrNull()
613
- if (decl != null && decl.importedSymbols.isEmpty() && parentName != decl.import) {
614
- newName = Name (newName.localName, decl.import, delimiter = newName.delimiter)
615
- }
606
+ // Otherwise, just build a new name based on the eventual resolved parent alias (if
607
+ // necessary)
608
+ var newName = adjustNameIfNecessary(parentName, name.parent, name)
616
609
617
610
return newName
618
611
}
619
612
613
+ private fun adjustNameIfNecessary (newParentName : Name , oldParentName : Name , name : Name ): Name =
614
+ if (newParentName != oldParentName) {
615
+ Name (name.localName, newParentName, delimiter = name.delimiter)
616
+ } else {
617
+ name
618
+ }
619
+
620
620
/* *
621
621
* Directly jumps to a given scope. Returns the previous scope. Do not forget to set the scope
622
622
* back to the old scope after performing the actions inside this scope.
0 commit comments