@@ -578,6 +578,7 @@ def LinkLibraryReferences(self):
578
578
DEFAULT_LIBRARIES = ("std" ,)
579
579
580
580
for designUnit in self .IterateDesignUnits (DesignUnits .WithContext ):
581
+ # All primary units supporting a context, have at least one library implicitly referenced
581
582
if isinstance (designUnit , PrimaryUnit ):
582
583
for libraryIdentifier in DEFAULT_LIBRARIES :
583
584
designUnit ._referencedLibraries [libraryIdentifier ] = self ._libraries [libraryIdentifier ]
@@ -589,6 +590,7 @@ def LinkLibraryReferences(self):
589
590
libraryIdentifier = workingLibrary .NormalizedIdentifier
590
591
designUnit ._referencedLibraries [libraryIdentifier ] = self ._libraries [libraryIdentifier ]
591
592
designUnit ._referencedPackages [libraryIdentifier ] = {}
593
+ # All secondary units inherit referenced libraries from their primary units.
592
594
else :
593
595
if isinstance (designUnit , Architecture ):
594
596
referencedLibraries = designUnit .Entity .Entity ._referencedLibraries
@@ -601,15 +603,16 @@ def LinkLibraryReferences(self):
601
603
designUnit ._referencedLibraries [libraryIdentifier ] = library
602
604
603
605
for libraryReference in designUnit ._libraryReferences :
606
+ # A library clause can have multiple comma-separated references
604
607
for librarySymbol in libraryReference .Symbols :
605
608
libraryIdentifier = librarySymbol .NormalizedIdentifier
606
609
try :
607
- lib = self ._libraries [libraryIdentifier ]
610
+ library = self ._libraries [libraryIdentifier ]
608
611
except KeyError :
609
612
raise Exception (f"Library '{ librarySymbol .Identifier } ' referenced by library clause of design unit '{ designUnit .Identifier } ' doesn't exist in design." )
610
613
611
- librarySymbol .Library = lib
612
- designUnit ._referencedLibraries [libraryIdentifier ] = lib
614
+ librarySymbol .Library = library
615
+ designUnit ._referencedLibraries [libraryIdentifier ] = library
613
616
designUnit ._referencedPackages [libraryIdentifier ] = {}
614
617
# TODO: warn duplicate library reference
615
618
@@ -619,6 +622,7 @@ def LinkPackageReferences(self):
619
622
)
620
623
621
624
for designUnit in self .IterateDesignUnits (DesignUnits .WithContext ):
625
+ # All primary units supporting a context, have at least one package implicitly referenced
622
626
if isinstance (designUnit , PrimaryUnit ):
623
627
for lib in DEFAULT_PACKAGES :
624
628
if lib [0 ] not in designUnit ._referencedLibraries :
@@ -627,6 +631,7 @@ def LinkPackageReferences(self):
627
631
designUnit ._referencedPackages [lib [0 ]][pack ] = self ._libraries [lib [0 ]]._packages [pack ]
628
632
# TODO: catch KeyError on self._libraries[lib[0]]._packages[pack]
629
633
# TODO: warn duplicate package reference
634
+ # All secondary units inherit referenced packages from their primary units.
630
635
else :
631
636
if isinstance (designUnit , Architecture ):
632
637
referencedPackages = designUnit .Entity .Entity ._referencedPackages
@@ -639,13 +644,15 @@ def LinkPackageReferences(self):
639
644
designUnit ._referencedPackages [packageIdentifier ] = package
640
645
641
646
for packageReference in designUnit .PackageReferences :
647
+ # A use clause can have multiple comma-separated references
642
648
for symbol in packageReference .Symbols :
643
649
packageSymbol = symbol .Prefix
644
650
librarySymbol = packageSymbol .Prefix
645
651
646
652
libraryIdentifier = librarySymbol .NormalizedIdentifier
647
653
packageIdentifier = packageSymbol .NormalizedIdentifier
648
654
655
+ # In case work is used, resolve to the real library name.
649
656
if libraryIdentifier == "work" :
650
657
library : Library = designUnit .Library
651
658
libraryIdentifier = library .NormalizedIdentifier
@@ -666,6 +673,7 @@ def LinkPackageReferences(self):
666
673
librarySymbol .Library = library
667
674
packageSymbol .Package = package
668
675
676
+ # TODO: update the namespace with visible members
669
677
if isinstance (symbol , AllPackageMembersReferenceSymbol ):
670
678
pass
671
679
@@ -679,19 +687,68 @@ def LinkContextReferences(self):
679
687
680
688
def LinkContexts (self ):
681
689
for context in self .IterateDesignUnits (DesignUnits .Context ):
690
+ # Create entries in _referenced*** for the current working library under its real name.
691
+ workingLibrary : Library = context .Library
692
+ libraryIdentifier = workingLibrary .NormalizedIdentifier
693
+ context ._referencedLibraries [libraryIdentifier ] = self ._libraries [libraryIdentifier ]
694
+ context ._referencedPackages [libraryIdentifier ] = {}
695
+
696
+ # Process all library clauses
682
697
for libraryReference in context ._libraryReferences :
698
+ # A library clause can have multiple comma-separated references
683
699
for librarySymbol in libraryReference .Symbols :
684
700
libraryIdentifier = librarySymbol .NormalizedIdentifier
685
701
try :
686
- lib = self ._libraries [libraryIdentifier ]
702
+ library = self ._libraries [libraryIdentifier ]
687
703
except KeyError :
688
704
raise Exception (f"Library '{ librarySymbol .Identifier } ' referenced by library clause of context '{ context .Identifier } ' doesn't exist in design." )
705
+ # TODO: add position to these messages
689
706
690
- librarySymbol .Library = lib
691
- context ._referencedLibraries [libraryIdentifier ] = lib
707
+ librarySymbol .Library = library
708
+ context ._referencedLibraries [libraryIdentifier ] = library
692
709
context ._referencedPackages [libraryIdentifier ] = {}
693
710
# TODO: warn duplicate library reference
694
711
712
+ # Process all use clauses
713
+ for packageReference in context .PackageReferences :
714
+ # A use clause can have multiple comma-separated references
715
+ for symbol in packageReference .Symbols :
716
+ packageSymbol = symbol .Prefix
717
+ librarySymbol = packageSymbol .Prefix
718
+
719
+ libraryIdentifier = librarySymbol .NormalizedIdentifier
720
+ packageIdentifier = packageSymbol .NormalizedIdentifier
721
+
722
+ # In case work is used, resolve to the real library name.
723
+ if libraryIdentifier == "work" :
724
+ library : Library = context .Library
725
+ libraryIdentifier = library .NormalizedIdentifier
726
+ elif libraryIdentifier not in context ._referencedLibraries :
727
+ # TODO: This check doesn't trigger if it's the working library.
728
+ raise Exception (f"Use clause references library '{ librarySymbol .Identifier } ', which was not referenced by a library clause." )
729
+ else :
730
+ library = self ._libraries [libraryIdentifier ]
731
+
732
+ try :
733
+ package = library ._packages [packageIdentifier ]
734
+ except KeyError :
735
+ raise Exception (f"Package '{ packageSymbol .Identifier } ' not found in { 'working ' if librarySymbol .NormalizedIdentifier == 'work' else '' } library '{ library .Identifier } '." )
736
+
737
+ # TODO: warn duplicate package reference
738
+ context ._referencedPackages [libraryIdentifier ][packageIdentifier ] = package
739
+
740
+ librarySymbol .Library = library
741
+ packageSymbol .Package = package
742
+
743
+ # TODO: update the namespace with visible members
744
+ if isinstance (symbol , AllPackageMembersReferenceSymbol ):
745
+ pass
746
+
747
+ elif isinstance (symbol , PackageMembersReferenceSymbol ):
748
+ raise NotImplementedError ()
749
+ else :
750
+ raise Exception ()
751
+
695
752
def LinkArchitectures (self ):
696
753
for library in self ._libraries .values ():
697
754
library .LinkArchitectures ()
@@ -793,6 +850,7 @@ def LinkArchitectures(self):
793
850
794
851
for architecture in architecturesPerEntity .values ():
795
852
entity = self ._entities [entityName ]
853
+ entity ._architectures .append (architecture ) # TODO: convert to dict
796
854
architecture .Entity .Entity = entity
797
855
798
856
def LinkPackageBodies (self ):
0 commit comments