@@ -343,47 +343,11 @@ class ScopeManager : ScopeProvider {
343
343
/* *
344
344
* This function MUST be called when a language frontend first handles a [Declaration]. It adds
345
345
* a declaration to the scope manager, taking into account the currently active scope.
346
- * Furthermore, it adds the declaration to the [de.fraunhofer.aisec.cpg.graph.DeclarationHolder]
347
- * that is associated with the current scope through [ValueDeclarationScope.addValueDeclaration]
348
- * and [StructureDeclarationScope.addStructureDeclaration].
349
- *
350
- * Setting [Scope.astNode] to false is useful, if you want to make sure a certain declaration is
351
- * visible within a scope, but is not directly part of the scope's AST. An example is the way
352
- * C/C++ handles unscoped enum constants. They are visible in the enclosing scope, e.g., a
353
- * translation unit, but they are added to the AST of their enum declaration, not the
354
- * translation unit. The enum declaration is then added to the translation unit.
355
346
*
356
347
* @param declaration the declaration to add
357
- * @param addToAST specifies, whether the declaration also gets added to the [Scope.astNode] of
358
- * the current scope (if it implements [DeclarationHolder]). Defaults to true.
359
348
*/
360
- @JvmOverloads
361
- fun addDeclaration (declaration : Declaration ? , addToAST : Boolean = true) {
362
- if (declaration != null ) {
363
- // New stuff here
364
- currentScope?.addSymbol(declaration.symbol, declaration)
365
- }
366
-
367
- // Legacy stuff here
368
- when (declaration) {
369
- is ProblemDeclaration ,
370
- is IncludeDeclaration -> {
371
- // directly add problems and includes to the global scope
372
- this .globalScope?.addDeclaration(declaration, addToAST, this )
373
- }
374
- is ValueDeclaration -> {
375
- val scope = this .firstScopeIsInstanceOrNull<ValueDeclarationScope >()
376
- scope?.addDeclaration(declaration, addToAST, this )
377
- }
378
- is ImportDeclaration ,
379
- is EnumDeclaration ,
380
- is RecordDeclaration ,
381
- is NamespaceDeclaration ,
382
- is TemplateDeclaration -> {
383
- val scope = this .firstScopeIsInstanceOrNull<StructureDeclarationScope >()
384
- scope?.addDeclaration(declaration, addToAST, this )
385
- }
386
- }
349
+ fun addDeclaration (declaration : Declaration ) {
350
+ currentScope?.addSymbol(declaration.symbol, declaration)
387
351
}
388
352
389
353
/* *
@@ -483,11 +447,10 @@ class ScopeManager : ScopeProvider {
483
447
}
484
448
485
449
/* *
486
- * Adds typedefs to a [ValueDeclarationScope]. The language frontend needs to decide on the
487
- * scope of the typedef. Most likely, typedefs are global. Therefore, the [GlobalScope] is set
488
- * as default.
450
+ * Adds typedefs to a [Scope]. The language frontend needs to decide on the scope of the
451
+ * typedef. Most likely, typedefs are global. Therefore, the [GlobalScope] is set as default.
489
452
*/
490
- fun addTypedef (typedef : TypedefDeclaration , scope : ValueDeclarationScope ? = globalScope) {
453
+ fun addTypedef (typedef : TypedefDeclaration , scope : Scope ? = globalScope) {
491
454
scope?.addTypedef(typedef)
492
455
}
493
456
@@ -684,45 +647,43 @@ class ScopeManager : ScopeProvider {
684
647
// We need to build a path from the current scope to the top most one. This ensures us that
685
648
// a local definition overwrites / shadows one that was there on a higher scope.
686
649
while (current != null ) {
687
- if (current is ValueDeclarationScope ) {
688
- // This is a little bit of a hack to support partial FQN resolution at least with
689
- // typedefs, but it's not really ideal.
690
- // And this also should be merged with the scope manager logic when resolving names.
691
- //
692
- // The better approach would be to harmonize the FQN of all types in one pass before
693
- // all this happens.
694
- //
695
- // This process has several steps:
696
- // First, do a quick local lookup, to see if we have a typedef our current scope
697
- // (only do this if the name is not qualified)
698
- if (! alias.isQualified() && current == scope) {
699
- val decl = current.typedefs[alias]
700
- if (decl != null ) {
701
- return decl.type
702
- }
650
+ // This is a little bit of a hack to support partial FQN resolution at least with
651
+ // typedefs, but it's not really ideal.
652
+ // And this also should be merged with the scope manager logic when resolving names.
653
+ //
654
+ // The better approach would be to harmonize the FQN of all types in one pass before
655
+ // all this happens.
656
+ //
657
+ // This process has several steps:
658
+ // First, do a quick local lookup, to see if we have a typedef our current scope
659
+ // (only do this if the name is not qualified)
660
+ if (! alias.isQualified() && current == scope) {
661
+ val decl = current.typedefs[alias]
662
+ if (decl != null ) {
663
+ return decl.type
703
664
}
665
+ }
704
666
705
- // Next, try to look up the name either by its FQN (if it is qualified) or make it
706
- // qualified based on the current namespace
707
- val key =
708
- current.typedefs.keys.firstOrNull {
709
- var lookupName = alias
710
-
711
- // If the lookup name is already a FQN, we can use the name directly
712
- lookupName =
713
- if (lookupName.isQualified()) {
714
- lookupName
715
- } else {
716
- // Otherwise, we want to make an FQN out of it using the current
717
- // namespace
718
- currentNamespace?.fqn(lookupName.localName) ? : lookupName
719
- }
720
-
721
- it.lastPartsMatch(lookupName)
722
- }
723
- if (key != null ) {
724
- return current.typedefs[key]?.type
667
+ // Next, try to look up the name either by its FQN (if it is qualified) or make it
668
+ // qualified based on the current namespace
669
+ val key =
670
+ current.typedefs.keys.firstOrNull {
671
+ var lookupName = alias
672
+
673
+ // If the lookup name is already a FQN, we can use the name directly
674
+ lookupName =
675
+ if (lookupName.isQualified()) {
676
+ lookupName
677
+ } else {
678
+ // Otherwise, we want to make an FQN out of it using the current
679
+ // namespace
680
+ currentNamespace?.fqn(lookupName.localName) ? : lookupName
681
+ }
682
+
683
+ it.lastPartsMatch(lookupName)
725
684
}
685
+ if (key != null ) {
686
+ return current.typedefs[key]?.type
726
687
}
727
688
728
689
current = current.parent
0 commit comments