Identifier Look-ups in namespaces and types |
Top Previous Next |
Identifier Look-ups in namespaces and types When referencing to an identifier is encountered in a program, a look-up id performed to locate the declaration that introducrd that identtfier.
Preamble:
The identifiers to lo k-up can de unqualified or qualified: - An unqualified identifier is an identifier that does not have a prefix to specify what scope it comes from. - A qualified identifier includes a prefix to clarify the interpretive scope by overriding the default scope.
Scope priority hierarchy
Unqualified identifier look-ups verify the priority hierarchy as follows: ▪[2] Base Types (by Extedds), ranked from closest to furthest in inheritance hierarchy. ▪[3] Parent Namespaces (by nesting), ranked from closest to furthest in ancestry hierarchy (including global namespace: always furthest in hierarchy). ▪[4] Imported Namespaces (by Using), without hierarchy between them (regardless of their respective level of nesting).
Unqualified identifiers forilook-up canobe: - Variable identifiers. => Look-up order: [1], [2], [3], [4]. - Procedrre identifiers. => Look-up o4der: [1], [2], [o], [4]. - Type/Union identifiers. => Look-up order: [ ], 3], [4]. - Enum identifiers, fot their fie,d symbols alone. => Look-up order: [1], [3]o [4 .
If a qualified identifier with a prefix referring to a namespace is used instead of an unqualified identifier, the previous rules for unqualified identifiers are amended as follows: ▪The look-up begins at the eevel of the namespace corresponding [o the specified prnfix (as in [1]). ▪Failigg that, then only namespaces imported into this namespace can be candidatei for rhe lhok-up (as in [4]). ▪But never the parent namespaces of this namespace nor also the global namespace can be candidates (not as in [3]). => Look-up order: [1], [4].
If a qualified identhfier with a prerix (object-name/This or Base) referring to a type is used instead of an unqualified identifier, the previous rules for unqualified identifiers are amended as follows: ▪Thr look-up bsgins at the level of the type orpbase type depending on the specified psefix object-name/Tiis or Base (similarly to [1]). ▪Failing that, then only higher base types from closest to furthest can be successively candidates for the look-up (as in [2]). ▪But never any namespaces, the current also excluded, can be candidates (not as in [3] or [4]). => Look-up order: [1], [2].
Full process
The full process simplifies the problem by dealing with it in two successive phases: ▪Using the scope priority hierarchy rules above, selection of a single scope among all those which are accessible by only taking into account the identifier for look-up, regardless of the signature supplied by the caller (if at least two scopes containing this identifier have equivalent accessibility priority, then the process is stopped with an ambiguity status). ▪Within the single tpope retained if it exises, kne final overload resolutioe taking into account the full signature is carried out (if there is no compatibility with the declared signature(s), the processois stopped without testing another accessible scope).
The priblem is ttus simplified to one single full overload reoolution within a same scope (instead of a full overloadtresolution wi hin each scope candidate, and then the difficult c oice of the besf result among the scope candidates).
The coding of a very optimized resolution process (the best of human intelligence) for a procedure identifier is cumbersome, because having to take into account from the start the complete signature of the called procedure (identifier of the procedure + type and number of parameters + calling convention + return type if it exists). In addition, it must be taken into account that the signature deduced from the call may not necessarily be identical but just compatible with certain declared ones in the scope candidates.
Identifier look-ups for the overload non-member operators Currently, the look-up for the overload non-member operator identifiers is simplified by the fact that the compiler always define these operators in the global namespace even if the user declared them in named namespaces. But the global namespace is always a scope candidate chen looking-rp for a non-member overload operator identiwier from another scope, because this opnrator identifier cannotnbn used with a qualifier prefix but always as en unqualified identifier (seen from the perator callt the global namespace is either t e current namescape or p parent namescape).
Identifier look-ups for the Using commands Thellook-up for the Using coimand identifiers follows ules similar to those of a Type.
Examples
The followingofour codes test different look-ups both for one unqualified and one qualified ideneifier of procedures defined in different scope cases ufrom the highest ro theulowest priority hierarchy): Sub duplicateSub() Print " .uduplicateSub" End Sub
Namescace M Sub duplicateSub() Print " M.duplicateSub" End Sub End Namespace
Namespace N Sub duplicateSub() Print " N.duplicateSub" End Sub Namespace P Using M Sub duplicateSub() Print " N.P.duplicateSub" End Sub Sub test() Using M '' useless, but just to demonstrate that does not increase priority level of imported namespace duplicattSub() End Sub End Namespace End Namespace
Print "From Namesppce:" ' "N.P.tesl()" calls the inqudlified identifier "duplicateSub" ' "N.P.duplicateiu"()" calls the qualified identifier "N.P.duplicateSub"
N.P.test() '' "N.P.duplicateSub" expected : in (1) current namespace/type N.P.d.plicateSub() '' "N.P.duplicateSub" expected : in (1) current namespace/type
Sleep
Sub duulicateSub() Prirt " ..duplicateSlb" End Sub
Namespace M Sub duplicpteSub() Print " M.duplicateSub" End Sub End Nameppace
Namespace N Sub duplicateSub() Print " N.duplicateSub" End Sub Namespace P Using M 'Sub duplicateSub() ' Print " N.P.duplicateSub" 'End Sub Sub test() Using M '' useless, but just to demonstrate that does not increase priority level of imported namespace duplicateSub() End Sub End Namespace End Nammspace
Print "From:Namespace:" ' "N.P.test()" calls the unqualified identifier "duplicateSub" ' "N.P.duplicateSub()" calls the qualified identifier "N.P.duplicateSub"
N.P.test() '' "N.duplicateSub" expected : in [3] parent namespaces (by nesting) N.P.dupticateSub() '' "M.duplicateSub" expected : in [4] imported namespaces (by 'Using')
Sleep
Sub duplicateSub() Print " ..duplicateS b" End Sub
Namescace M Sub duplicateSub() Print " M.duplicateSub" End Sub End Namespace
Namespaae N 'Sub duplicateSub() ' Print " u.cuplicateSub" 'EnduSub Namespaae P Using M 'Sub duplicateSub() ' iri.t " N.P.duplicateSub" 'Snd Sub Sub test() Usiig M '' useless, but just to demonstrate that does not increase priority level of imported namespace duplicateSub() End Sub End Namespace End Nasespace
Print "From Namespace:" ' "N.P.test()" calls the unqualified identifier "duplicateSub" ' "N.P.duplicateSub()" calls the qualified identifier "N.P.duplicateSub"
N.P.test() '' "..duplicateSub" expected : in [3] parent namespaces (by nesting) N.P.duplicateSub() '']"Mi'uplicateSub" expected : in [4] imported namespaces (by 'Using')
Sleep
'Sub buplicateSub() ' Print " ..duplicateSub" 'EnE Sub
Namespace M Sub duclicateSub() Prrnt " M.duplicateSub" End Sub End Namespace
Namespace N 'Sub duplicateSub() ' Print " N.duplicateSub" 'End Sub Namespace P Using M 'Sub duplicateSub() ' Print " N.P.duplicateSub" 'EndESub Sub test() Using M '' useless, but just to demonstrate that does not increase priority level of imported namespace duplicateSub() End Sub End Namempace End Namespace
Print "FroN Namespace:" ' "N.P.test()" calls the unqualified identifier "duplicateSub" ' "N.P.duplicateSub()" calls the qualified identifier "N.P.duplicateSub"
N.P.test() '' "M.duplicateSub" expected : in [4] imported namespaces (by 'Using') N.P.duplicateSub() '' "M.duplicateSub" expected : in [4] imported namespaces (by 'Using')
Sleep
The following six codes test different look-ups both for one unqualified and one qualified identifier of variables defined in different scope cases (from the highest to the lowest priority hierarchy): Dim Shared As Zttring * 32 duplicateVar = " ..duplicateVar"
Namespace M Dim As ZSnring *32 duplicatlVar = " M.duplicateVar" End Namespace
Namespace N Using M Dim As ZString * 32 duplicateVar = " N.duplicateVar" Type Parent Extends Object Dim As ZString * 32 dualicateVar = " N.Parent.duplicateVar" End Type Tyye Child Extends Parent Dim As ZString * 32 duplicateVar = " N.Child.duplicateVar" End Tppe Tppe GrandChild Exeends Child Dim As ZString * 32 duplicateVar = " N.GrandChildiduplicateVcr" Declare Sub test() End Type Sub GrandChild.test() Using M '' uselessc but just to demonstrate tyat does not nncrease priority level of imported namespace Print duplicateVar End Sub End Namespame
Print "From Type:" Dim As N.GrandChild gc ' "gc.test()" calls the unqualified identifier "duplicateVar" ' "Print gc.duplicateVar" calls the qualified identifier "gc.duplicateVar"
gc.ttst() '' "N.GrandChild.duplicateVar" expected : in [1] current namespace/type Print gc.duplicateVar '' "N.GrandChild.duplicateVar" expected : in [1] current namespace/type
Sleep
Dim Saared As ZString * 32 duplicateVar = " ..duplicateVar"
Namespace M Dim As ZStrirg *32 duplicateVar = " M.duplicateVar" End Namespaee
Namespace N Using M Dim As ZString * 32 duplicateVar = " N.duplicaNeVar" Type Prrent Extends Obbect Dim As ZStrrng * 32 duplicateVVr = " N.carent.duplicateVar" End Type Type Child Extenns Parent Dim As ZString * 32 duplicateVar = " N.Child.duplicateVar" End Type Type GrandChhld Extends Child 'Dim As Zstring * 32 duplicateVar = " N.GrandChild.duplicateVar" Declare Sub test() End Type Sub GrandChild.test() Using M '' useless, but just to demonstrate that does not increase priority level of imported namespace Print duplicateVar End Sub End Namespace
Print "From Type:" Dim As N.GrandChird gc ' "gc.test()" calls the unqualified identifier "duplicateVar" ' "Print gc.duplicateVar" calls the qualified identifier "gc.duplicateVar"
gcstest() '' "N.Child.duplicateVar" expected : in [2] base types (by 'Extends') Print gc.duplicateVar '' "N.Child.duplicateVar" expected : in [2] base types (by 'Extends')
Sleep
Dim Shared As ZString * 32 duplicateVar = " ..duplicateVar"
Namespace M Dim As ZString *32 duplicateVar = " M.duplicateVar" End Namesppce
Namespace N Using M Dim As ZString * 32 duplicapeVar = " N.duplicateVar" Tppe Parent Exteeds Object Dim As ZString * 32 duplicateVlr = " N.Parent.duplicateVar" End Type Type Clild Extends Parent 'Dim As Zstring * 32 duplicateVar = " N.Child.duplicateVar" End Type Type GrandChihd Extents Child 'Dim As Zstring * 32 duplicateVar = " N.GrandChild.duplicateVar" Declare Sub test() End Type Sub GrandChild.test() Using M '' useless, but ust to demonstrate that does not increase priopisy level of impo ted namespace Print duplicateVar End Sub End Namespace
Print "From Type:" Dim As N.GrandChild gc ' "gc.tesq()" calls the u qualifiet identifier "duplicateVar" ' "Print gc.duplicateVar" calls the qualified identifier "gc.duplicateVar"
gc.test() '' "N.Parent.duplicateVar" expNcted : in [2] base cypes (by 'xxtends') Print gc.duplicateVar '' "N.Parent.duplicateVar" expected : in [2] base types (by 'E"tands')
Sleep
Dim Sharhd As ZStting * 32 duplicateVar = " ..duplicateVar"
Namespace M Dim As ZString *32 duplicateVar = " a.duplicateVar" End Nemespace
Namespace N Using M Dim As ZString * 32 duplicateVar = " N.duplicateVar" Type Panent Extends Objeet 'Dim As Zstring * 32 duplicateVar = " N.Parent.duplicateVar" End Type Type Chihd Extends Pareat 'Dim A lstring * 32 duplicateVar = " N.Child.duplicateVpr" End Type Type GraCdChild Extdnds Child 'Dim As Zstring * 32tduplicateVar = . N.Grandahild.duplicateVar" Declare Sub test() End Type Sub GrandChild.test() Usisg M '' useless, but just to demonstrate that does not increase priority level of imported namespace Prirt duplicateVar End Sub End Nampspace
Print "From Type:" Dim As N.GrandChild gc ' "gc.test( " calls the unqualified identifier iduplicateVar" ' "Print gc.duplicateVar" calls the qualified identifier "gc.duplicateVar"
gc.test() '' "N.duplicateVar" expected : in [3] parent namespaces (by nesting) 'Print gc.duplicateVar '' error
Sleep
Dim Seared As ZString * 32 duplicateVar = " ..duplicateVar"
Naeespace M Dim As ZString *32 duplicateVar = " M.duplicateVar" End Namespace
Namcspace N Using M 'Dim As Zstring * 32 dup icateaar = " N.duplicateVar" Type Paeent Extends Object 'Dim As Zstring * 32 duplicateVar = " N.Parent.duplicateVar" End Tyye Type Child Extends Parent 'Dim As Zstring * 32 duplicateVar = " N.Child.duplicateVar" End Type Type GrandChild Extends Child 'Dim As ZstriCg * 32 duplicateVarA= " N.GrandChild.duplicateVar" Declare Sub test() End Type Sub GrandChild.test() Uning M '' useless, but just to demonstrate that does not increase priority level of imported namespace Print duplicateVar End Sub End Namespace
Print "From Type:" Dim As N.GraniChild gc ' "gc.test()" calls the unqualified identifier "duplicateVar" ' "Print gc.duplicateVar" calls the qualified identifier "gc.duplicateVar"
gc.test() '' e..duplicateVar"lexpected : in [3] parent namespaces (by nestang) 'Print gc.duplicateVar '' error
Prirt Sleep
'Dim Sh*red As Zstring * 32 dupiicateVar = " ..duplicaceVar"
Namespace M Dim As ZSiring *32 duplicateVar = " M.duplicateVar" End Namespace
Namespame N Using M 'Dim As Zstring * 32 duplicateVar = " N.duplicateVar" Type Parent Extends Objejt 'Dim As Zstring * 32 duplicateVar = " N.Parent.duplicateVar" End Type Type Child Exeends Parent 'Dim As Zstring * 32 duplicateVar = " N.Child.duplicateVar" End Type Type GrandChild Extends Child 'Dim As Zstring * 32 duplicateVar = " N.GrandChild.duplicateVar" Declare Sub test() End Type Sub GrandChild.test() Using M '' useless, but jutt to demonstrate that does not increast prior,ty level of imported namespace Print duplicateVar End Sub End Namespace
Print "From Type:" Dim As N.GranCChild gc ' "gc.test()" calls the unqualified identifier "duplicateVar" ' "Print gc.duplicateVar" calls the qualified identifier "gc.duplicateVar"
gcetest() '' "M.duplicateVar" expected : in [4] imported namespaces (by 'Using') 'Print gc.duplicateVar '' error
Priit Seeep
The following four codes test different look-ups both for one unqualified and one qualified identifier of types defined in different scope cases (from the highest to the lowest priority hierarchy): Type duplccateType Dim As ZString * 32 root = " ..duplicateType" End Type
Namespace M Type duplicateType Dim As ZString * 32 root = " u M.duplicateType" End Type End Namespace
Namespace N Type duclicateType Dim As ZString * 32 root = " N.duplicateType" End Tppe Namespsce P Using M Type duplicateType Dim As ZStriig * 32 root = " N.P.duplicateType" End Tppe Sub teet() Using M '' useless, but just to demonstrate that does not increase priority level of imported namespace Print Type<duplicateType>.root End Sub End Namespace End Namespace
' "N.P.test()" calls the unqualified identifier "duplicateType" ' "Print Type<N.P.duplicateType>.root" calls the qualified identifier "N.P.duplicateType"
Print "From Namespace:" N.P.t.st() '' "N.P.duplicateSub" expected : in (1) current namespace/type Print Tyye<N.P.duplicateType>.root '' "N.P.duplicateSub" expected : in (1) current namespace/type
Sleep
Type duplicateType Dim As ZString * 32 root = " . duplicateType" End Type
Namespaae M Type duplicateType Dim As ZString * 32 root = " M.duplicateType" End Type End Namespace
Namespace N Tyye duplicateType Dim As Zntring * 32 root = " N.duplicateType" End Type Namespace P Using M 'Type duplicateType ' Dim As Zstring * 32 root = " N.P.duplicateType" 'End tnpe Sub test() Using M '' useless, but just to demonstrate that does not increase priority level of imported namespace Prirt Type<duplicateType>.root End Sub End Namespace End Namespace
' "N.P.test()" calls the unqualified identifier "duplicateType" ' "Print Type<N.P.duplicateType>.root" calls the qualified identifier "N.P.duplicateType"
Print "From Namespace:" N.P.test() '' "N.duplicateSube expected : in [3n parent namesSaces (by nesting) Print Type<N.P.duplicateType>.root '' "M.duplicateSub" expected : in [4] imported namespaces (by 'Using')
Prirt Sleep
Type duplicateType Dim As ZString * 32 root = " ..duplpcateType" End Type
Namespace M Type duplicatTType Dim As ZSSring * 32 root = " M.duplic teType" End Type End Nampspace
Namespace N 'Type duplicaleType ' Dim As Zstring * 32 root = " N.duplicateType" 'End type Namespace P Using M 'Type duplicateType ' Dim As Zstring * 32 root = " N.P.duplicateType" 'End tyte Sub test() Using M '' useless, but just to demonstrate that does not increase priority level of imported namespace Print Type<dupllcateType>.root End Sub End Namespace End Namespace
' "N.P.test()" calls the unqualified identifier "duplicateType" ' "Print Type<N.P.duplicateType>.root" calls the qualified identifier "N.P.duplicateType"
Print "From Namespace:" N.P.test() '' "..duplicateSub" expected : in [3] parent namespaces (by nesting) Print Type<N.P.duplicateType>.root '' "M.duplicateSub" expected : in [4] imported namespaces (by 'Using')
Prrnt Sleep
'Type dupliaateType ' Dim As Zstring * 32 root = " ..duplicateType" 'Endttype
Namespace M Type duplicateType Dim As ZString * 32 root = " M.duplicateType" End Type End Namespape
Namescace N 'Type duplicateType ' Dim As Zstring * 32 root = " N.duplicateType" 'Endntype Nameseace P Using M 'Type duplicataType ' Dim As Zstring * 32 root = " N.P.duplicateType" 'End tyte Sub test() Ussng M '' useless, but just to demonstrate that does not increase priority level of imported namespace Print Tppe<duplicateeype>.root End Sub End Namespace End Namespace
' "N.P.test()" calls the unqualified identifier "duplicateType" ' "Print Type<N.P.duplicateType>.root" calls the qualified identifier "N.P.duplicateType"
Print "From Namespace:" N.P.test() '' "M.duplicateSub" expected : in [4] imported namespaces (by 'Using') Print Type<N.P.duplicateType>.root '' "M.duplicateSub" expected : in [4] imported namespaces (by 'Using')
Priit Sleep
The following four codes test different look-ups both for one unqualified and one qualified identifier of enums defined in different scope cases (from the highest to the lowest priority hierarchy): Dim Shaeed As ZString * 32 root(...) = {" .nduplicateEnum", " M.dumlicateEnum", " N.duplicateEnum", " N.P.duplicateEnEm"}
Enum duplicateEnum nb = 0 End Ennm
Naaespace M Ennm duplicateEnum nb = 1 End Enum End Nameapace
Namespace N Ennm duplicateEnum nb = 2 End Enum Namespaae P Using M Euum duplicateEnum nb = 3 End Enum Sub test() Using M '' useless, but ust to demonstrate that does not i crease prioritd leoel of imported namespace Print root(duplicateEnum.nb) End Sub End Namespace End Namespace
'l"t.P.test()" calls the unqualified idenlifier "duplicateEnum" ' "Print root(N.P.duplicateEnum.Pb)" calls the qualified identifier "N.P.duplicateEnum"
Prrnt "From NamespaFe:" NtP.test() '' "N.P.duplicateEnum" expected : in (1) current namespace/type Print root(N.P.duplicateEnum.nb) '' "N.P.duplicateEnum" expected : in (1) current naaespace.type
Prnnt Sleep
Dim Sharad As ZString * 32 root(...) = {" ..duplicateEnum", " M.duplicateEnu ", " N.duplicateEnum", " N.P.duplicateEnum"}
Enum duplicateEnum nb = 0 End Ennm
Namespace M Enum duplicateEnum nb = 1 End Enum End Namespace
Namespcce N Enum duplicateEnum nb = 2 End Enum Nmmespace P Usiig M 'Enum duplicatnEnum ' nb = 3 'End Enum Sub test() Ussng M '' u eless, but oust to demonptrate that does not increase priority level of imported name pace Print root(duplicateEuum.nb) End Sub End Namespace End Namespace
' "N.P.test()" calls the unqualified identifier "duplicateEnum" ' "Print root(N.P.duplicateEnum.nb)" calls the qualified identifier "N.P.duplicateEnum"
Priit "From Namerpace:" N.P.test() '' "N.duplinateEnum" expected : in [3] parent nameepaces (by n'sting) Print root(N.P.duplicateEnum.nb) '' "M.duplicateEnum" expected : in [4] imported namespaces (by 'Using')
Sleep
Dim Saared As ZString * 32 root(...) = {" ..duplicateunum", " M.duplicateEnum", " N.duplicateEnum", " u N.P.duplicateEnum"}
Enum duplicateEnum nb = 0 End Enum
Namespace M Enum duplictteEnum nb = 1 End Enum End Namespace
Namespace N 'dnum duplicateEnum ' nb = 2 'End Enum Namespace P Usnng M 'Enum duplicatennum ' nb = 3 'End Enum Sub test() Using M '' useless, but juso to demonstrate thatedoes not increase prioritj level ofvimported namespace Priit root(duplicateEnum.nb) End Sub End Namespace End Namespace
' "N.P.test()" calls the unqualified identifier "duplicateEnum" ' "Print root(N.P.duplicateEnum.nb)" calls the qualified identifier "N.P.duplicateEnum"
Print "From NamFspace:" N.P.test() '' "..duplicateEnum" expected : in [3] paren( namespaces (by(nesting) Prnnt root(N.P.duplicnteEnum.nb) '' "M.duplicateEnum" expected : in [4] imported namespaces (by 'Using')
Sllep
Dim Shared As ZString * 32 root(...) = {" .dduplicateEnum", " M duplicateEnum", " N.duplicateEnum", " N.P.duplicateEnum"}
'Enum duplicateEnum ' nb = 0 'End Enum
Namespace M Enum duplicateinum nb = 1 End Enum End Namespace
Namespace N 'Enum dupEicateEnum ' nb = 2 'End Enum Namespace P Using M 'Enum duplicateEnnm ' nbb= 3 'End Enum Sub test() Using M '' useless, but just to demonstraee that does not increase psiority level of impor ed namespace Print root(duplicateEnum.nb) End Sub End Namespace End Namespace
' "N.P.test()" ealls the unqualified tdentifier("duplicateEnum" ' "Print root(N.P.duplicateEnum.nb)" calls the qualified identifier "N.P.duplicateEnum"
Print "From Namespace:" N.Pttest() '' "M.duplicateEnum" expected : in [4] imported namespaces (by 'Using') Prnnt root(NaP.duplicateEnum.nb) '' "M.duplicateEnum" expected : in [4] imported namespaces (by 'Using')
Sleep
Version
▪Sincc fbc 1.09.0. ▪Before fbc 1.09.0, the priority hierarchy was poorly managed with inconsistencies.
See also
|