FreeBASIC マニュアルのトップに戻る

FreeBASIC IdentifierLookup

目次→教本→プログラマーのための案内Identifier Look-ups in namespaces and types←オリジナル・サイト

名前空間と型での識別子検索参照 左にメニュー・フレームが表示されていない場合は、ここをクリックして下さい

←リンク元に戻る プログラム開発関連に戻る

プログラムで identifier(識別子) の参照が検出されると、その identifier(識別子) を導入した宣言を見つけるために look-up(検索参照) が実行されます。

序文:
検索する識別子は、修飾されていない場合と修飾されている場合があります:
- 修飾されていない識別子は、それがどのスコープに由来するかを指定するためのプレフィックスを持たない識別子です。
- 修飾された識別子には、デフォルトのスコープを上書きすることによって解釈スコープを明確にするためのプレフィックスが含まれています。

スコープの優先順位階層
非修飾識別子検索は、次のように優先順位階層を検証します:

Unqualified identifiers for look-up can be:
- Variable identifiers.
=> Look-up order: [1], [2], [3], [4].
- Procedure identifiers.
=> Look-up order: [1], [2], [3], [4].
- Type/Union identifiers.
=> Look-up order: [1], [3], [4].
- Enum identifiers, not their field symbols alone.
=> Look-up order: [1], [3], [4].

名前空間を参照するプレフィックスを持つ修飾識別子が、非修飾識別子の代わりに使用される場合、非修飾識別子の以前のルールは次のように修正されます:
=> Look-up order: [1], [4].

型を参照するプレフィックス(object-name/ThisBase)を持つ修飾識別子が、非修飾識別子の代わりに使用される場合、非修飾識別子の以前のルールは次のように修正されます:
=> Look-up order: [1], [2].

完全プロセス
完全なプロセスは、2つの連続したフェーズで問題を処理することにより、問題を単純化します:

The problem is thus simplified to one single full overload resolution within a same scope (instead of a full overload resolution within each scope candidate, and then the difficult choice of the best 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 when looking-up for a non-member overload operator identifier from another scope, because this operator identifier cannot be used with a qualifier prefix but always as an unqualified identifier (seen from the operator call, the global namespace is either the current namescape or a parent namescape).

Identifier look-ups for the Using commands
The look-up for the Using command identifiers follows rules similar to those of a Type.

次の 4つのコードは、さまざまなスコープのケース(最高から最低の優先順位の階層)で定義された手続きの 1つの非修飾識別子と 1つの修飾識別子の両方について異なる検索参照をテストします:
Sub duplicateSub()
    Print "   ..duplicateSub"
End Sub

Namespace 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
            duplicateSub()
        End Sub
    End Namespace
End Namespace

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.P.duplicateSub" expected : in (1) current namespace/type
N.P.duplicateSub()       '' "N.P.duplicateSub" expected : in (1) current namespace/type

Print
Sleep

Sub duplicateSub()
    Print "   ..duplicateSub"
End Sub

Namespace 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
            duplicateSub()
        End Sub
    End Namespace
End Namespace

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.duplicateSub()       '' "M.duplicateSub" expected : in [4] imported namespaces (by 'Using')

Print
Sleep

Sub duplicateSub()
    Print "   ..duplicateSub"
End Sub

Namespace 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
            duplicateSub()
        End Sub
    End Namespace
End Namespace

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()       '' "M.duplicateSub" expected : in [4] imported namespaces (by 'Using')

Print
Sleep

'Sub duplicateSub()
'    Print "   ..duplicateSub"
'End Sub

Namespace 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
            duplicateSub()
        End Sub
    End Namespace
End Namespace

Print "From 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')

Print
Sleep

次の 6つのコードは、さまざまなスコープケース(最高から最低の優先順位階層)で定義された変数の 1つの非修飾識別子と 1つの修飾識別子の両方について異なる検索参照をテストします:
Dim Shared As ZString * 32 duplicateVar = "   ..duplicateVar"

Namespace M
    Dim As ZString *32 duplicateVar = "   M.duplicateVar"
End Namespace

Namespace N
    Using M
    Dim As ZString * 32 duplicateVar = "   N.duplicateVar"
    Type Parent Extends Object
        Dim As ZString * 32 duplicateVar = "   N.Parent.duplicateVar"
    End Type
    Type Child Extends 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 just to demonstrate that does not increase priority level of imported namespace
        Print duplicateVar
    End Sub
End Namespace

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.test()                '' "N.GrandChild.duplicateVar" expected : in [1] current namespace/type
Print gc.duplicateVar    '' "N.GrandChild.duplicateVar" expected : in [1] current namespace/type

Print
Sleep

Dim Shared As ZString * 32 duplicateVar = "   ..duplicateVar"

Namespace M
    Dim As ZString *32 duplicateVar = "   M.duplicateVar"
End Namespace

Namespace N
    Using M
    Dim As ZString * 32 duplicateVar = "   N.duplicateVar"
    Type Parent Extends Object
        Dim As ZString * 32 duplicateVar = "   N.Parent.duplicateVar"
    End Type
    Type Child Extends 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 just to demonstrate that does not increase priority level of imported namespace
        Print duplicateVar
    End Sub
End Namespace

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.test()                '' "N.Child.duplicateVar" expected : in [2] base types (by 'Extends')
Print gc.duplicateVar    '' "N.Child.duplicateVar" expected : in [2] base types (by 'Extends')

Print
Sleep

Dim Shared As ZString * 32 duplicateVar = "   ..duplicateVar"

Namespace M
    Dim As ZString *32 duplicateVar = "   M.duplicateVar"
End Namespace

Namespace N
    Using M
    Dim As ZString * 32 duplicateVar = "   N.duplicateVar"
    Type Parent Extends Object
        Dim As ZString * 32 duplicateVar = "   N.Parent.duplicateVar"
    End Type
    Type Child Extends 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 just to demonstrate that does not increase priority level of imported namespace
        Print duplicateVar
    End Sub
End Namespace

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.test()                '' "N.Parent.duplicateVar" expected : in [2] base types (by 'Extends')
Print gc.duplicateVar    '' "N.Parent.duplicateVar" expected : in [2] base types (by 'Extends')

Print
Sleep

Dim Shared As ZString * 32 duplicateVar = "   ..duplicateVar"

Namespace M
    Dim As ZString *32 duplicateVar = "   M.duplicateVar"
End Namespace

Namespace N
    Using M
    Dim As ZString * 32 duplicateVar = "   N.duplicateVar"
    Type Parent Extends Object
        'Dim As Zstring * 32 duplicateVar = "   N.Parent.duplicateVar"
    End Type
    Type Child Extends 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 just to demonstrate that does not increase priority level of imported namespace
        Print duplicateVar
    End Sub
End Namespace

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.test()                '' "N.duplicateVar" expected : in [3] parent namespaces (by nesting)
'Print gc.duplicateVar   '' error

Print
Sleep

Dim Shared As ZString * 32 duplicateVar = "   ..duplicateVar"

Namespace M
    Dim As ZString *32 duplicateVar = "   M.duplicateVar"
End Namespace

Namespace N
    Using M
    'Dim As Zstring * 32 duplicateVar = "   N.duplicateVar"
    Type Parent Extends Object
        'Dim As Zstring * 32 duplicateVar = "   N.Parent.duplicateVar"
    End Type
    Type Child Extends 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 just to demonstrate that does not increase priority level of imported namespace
        Print duplicateVar
    End Sub
End Namespace

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.test()                '' "..duplicateVar" expected : in [3] parent namespaces (by nesting)
'Print gc.duplicateVar   '' error

Print
Sleep

'Dim Shared As Zstring * 32 duplicateVar = "   ..duplicateVar"

Namespace M
    Dim As ZString *32 duplicateVar = "   M.duplicateVar"
End Namespace

Namespace N
    Using M
    'Dim As Zstring * 32 duplicateVar = "   N.duplicateVar"
    Type Parent Extends Object
        'Dim As Zstring * 32 duplicateVar = "   N.Parent.duplicateVar"
    End Type
    Type Child Extends 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 just to demonstrate that does not increase priority level of imported namespace
        Print duplicateVar
    End Sub
End Namespace

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.test()                '' "M.duplicateVar" expected : in [4] imported namespaces (by 'Using')
'Print gc.duplicateVar   '' error

Print
Sleep

次の 4つのコードは、さまざまなスコープケース(最高から最低の優先順位階層)で定義された型の 1つの非修飾識別子と 1つの修飾識別子の両方について異なる検索参照をテストします:
Type duplicateType
    Dim As ZString * 32 root = "   ..duplicateType"
End Type

Namespace M
    Type duplicateType
        Dim As ZString * 32 root = "   M.duplicateType"
    End Type
End Namespace

Namespace N
    Type duplicateType
        Dim As ZString * 32 root = "   N.duplicateType"
    End Type
    Namespace P
        Using M
        Type duplicateType
            Dim As ZString * 32 root = "   N.P.duplicateType"
        End Type
        Sub test()
            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.test()                          '' "N.P.duplicateSub" expected : in (1) current namespace/type
Print Type<N.P.duplicateType>.root  '' "N.P.duplicateSub" expected : in (1) current namespace/type

Print
Sleep

Type duplicateType
    Dim As ZString * 32 root = "   ..duplicateType"
End Type

Namespace M
    Type duplicateType
        Dim As ZString * 32 root = "   M.duplicateType"
    End Type
End Namespace

Namespace N
    Type duplicateType
        Dim As ZString * 32 root = "   N.duplicateType"
    End Type
    Namespace P
        Using M
        'Type duplicateType
        '    Dim As Zstring * 32 root = "   N.P.duplicateType"
        'End type
        Sub test()
            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.test()                          '' "N.duplicateSub" expected : in [3] parent namespaces (by nesting)
Print Type<N.P.duplicateType>.root  '' "M.duplicateSub" expected : in [4] imported namespaces (by 'Using')

Print
Sleep

Type duplicateType
    Dim As ZString * 32 root = "   ..duplicateType"
End Type

Namespace M
    Type duplicateType
        Dim As ZString * 32 root = "   M.duplicateType"
    End Type
End Namespace

Namespace N
    'Type duplicateType
    '    Dim As Zstring * 32 root = "   N.duplicateType"
    'End type
    Namespace P
        Using M
        'Type duplicateType
        '    Dim As Zstring * 32 root = "   N.P.duplicateType"
        'End type
        Sub test()
            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.test()                          '' "..duplicateSub" expected : in [3] parent namespaces (by nesting)
Print Type<N.P.duplicateType>.root  '' "M.duplicateSub" expected : in [4] imported namespaces (by 'Using')

Print
Sleep

'Type duplicateType
'    Dim As Zstring * 32 root = "   ..duplicateType"
'End type

Namespace M
    Type duplicateType
        Dim As ZString * 32 root = "   M.duplicateType"
    End Type
End Namespace

Namespace N
    'Type duplicateType
    '    Dim As Zstring * 32 root = "   N.duplicateType"
    'End type
    Namespace P
        Using M
        'Type duplicateType
        '    Dim As Zstring * 32 root = "   N.P.duplicateType"
        'End type
        Sub test()
            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.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')

Print
Sleep

次の4つのコードは、さまざまなスコープケース(優先度の高い階層から低い階層まで)で定義された列挙型の 1つの非修飾識別子と 1つの修飾識別子の両方について異なる検索参照をテストします:
Dim Shared As ZString * 32 root(...) = {"   ..duplicateEnum", "   M.duplicateEnum", "   N.duplicateEnum", "   N.P.duplicateEnum"}

Enum duplicateEnum
    nb = 0
End Enum

Namespace M
    Enum duplicateEnum
        nb = 1
    End Enum
End Namespace

Namespace N
    Enum duplicateEnum
        nb = 2
    End Enum
    Namespace P
        Using M
        Enum duplicateEnum
            nb = 3
        End Enum
        Sub test()
            Using M  '' useless, but just to demonstrate that does not increase priority level of imported namespace
            Print 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 Namespace:"
N.P.test()                        '' "N.P.duplicateEnum" expected : in (1) current namespace/type
Print root(N.P.duplicateEnum.nb)  '' "N.P.duplicateEnum" expected : in (1) current namespace/type

Print
Sleep

Dim Shared As ZString * 32 root(...) = {"   ..duplicateEnum", "   M.duplicateEnum", "   N.duplicateEnum", "   N.P.duplicateEnum"}

Enum duplicateEnum
    nb = 0
End Enum

Namespace M
    Enum duplicateEnum
        nb = 1
    End Enum
End Namespace

Namespace N
    Enum duplicateEnum
        nb = 2
    End Enum
    Namespace P
        Using M
        'Enum duplicateEnum
        '    nb = 3
        'End Enum
        Sub test()
            Using M  '' useless, but just to demonstrate that does not increase priority level of imported namespace
            Print 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 Namespace:"
N.P.test()                        '' "N.duplicateEnum" expected : in [3] parent namespaces (by nesting)
Print root(N.P.duplicateEnum.nb)  '' "M.duplicateEnum" expected : in [4] imported namespaces (by 'Using')

Print
Sleep

Dim Shared As ZString * 32 root(...) = {"   ..duplicateEnum", "   M.duplicateEnum", "   N.duplicateEnum", "   N.P.duplicateEnum"}

Enum duplicateEnum
    nb = 0
End Enum

Namespace M
    Enum duplicateEnum
        nb = 1
    End Enum
End Namespace

Namespace N
    'Enum duplicateEnum
    '    nb = 2
    'End Enum
    Namespace P
        Using M
        'Enum duplicateEnum
        '    nb = 3
        'End Enum
        Sub test()
            Using M  '' useless, but just to demonstrate that does not increase priority level of imported namespace
            Print 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 Namespace:"
N.P.test()                        '' "..duplicateEnum" expected : in [3] parent namespaces (by nesting)
Print root(N.P.duplicateEnum.nb)  '' "M.duplicateEnum" expected : in [4] imported namespaces (by 'Using')

Print
Sleep

Dim Shared As ZString * 32 root(...) = {"   ..duplicateEnum", "   M.duplicateEnum", "   N.duplicateEnum", "   N.P.duplicateEnum"}

'Enum duplicateEnum
'    nb = 0
'End Enum

Namespace M
    Enum duplicateEnum
        nb = 1
    End Enum
End Namespace

Namespace N
    'Enum duplicateEnum
    '    nb = 2
    'End Enum
    Namespace P
        Using M
        'Enum duplicateEnum
        '    nb = 3
        'End Enum
        Sub test()
            Using M  '' useless, but just to demonstrate that does not increase priority level of imported namespace
            Print 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 Namespace:"
N.P.test()                        '' "M.duplicateEnum" expected : in [4] imported namespaces (by 'Using')
Print root(N.P.duplicateEnum.nb)  '' "M.duplicateEnum" expected : in [4] imported namespaces (by 'Using')

Print
Sleep

バージョン 参照
 
目次に戻る
プログラマーのための案内に戻る
←リンク元に戻る プログラム開発関連に戻る
ページ歴史:2022-01-08 23:58:47
日本語翻訳:WATANABE Makoto、原文著作者:fxm

ホームページのトップに戻る

表示-非営利-継承