设为首页 加入收藏

TOP

Vczh Library++3.0之ManagedX语言检查类型的可见性 (一)
2014-11-23 22:30:32 来源: 作者: 【 】 浏览:6
Tags:Vczh Library 3.0 ManagedX 语言 检查 类型 可见

Vczh Library++3.0的山寨C#的ManagedX今天完成了一个功能,就是编译器检查一个类型在他上下文里面是否可用。这个过程足够复杂,我写了足足3个小时。

ManagedX的符号表里面的类型已经被大大简化了。函数指针是类,基本数字类型也是类,所以归根结底只有
1:子类型
2:类
3:模板类型

因为某些关系,对于类型别名没有直接在符号表里面体现出来。举个例子:

1 generic
2 class A
3 {
4 generic
5 public using S = B;
6 }
7
8 generic
9 class B
10 {
11 public using T = Dictionary;
12 }
下面的类型与符号表中类型结构的展开后关系是:

1 ================================================
2 A ->
3 TypeSymbol(A)
4 {
5 GetSymbol() = A
6 GetParentType() = 0
7 GetGenericDeclaration() = TypeSymbol(A)
8 {
9 GetSymbol() = A
10 GetParentType() = 0
11 GetGenericDeclaration() = 0
12 GetGenericArguments() = []
13 }
14 GetGenericArguments() = [int]
15 }
16 ================================================
17 A.S ->
18 TypeSymbol(A.S)
19 {
20 GetSymbol() = A.S
21 GetParentType() = 0
22 GetGenericDeclaration() = TypeSymbol(A.S)
23 {
24 GetSymbol() = A.S
25 GetParentType() = TypeSymbol(A)
26 GetGenericDeclaration() = 0
27 GetGenericArguments() = []
28 }
29 GetGenericArguments() = [string]
30 }
31 ================================================
32 A.S.T ->
33 TypeSymbol(Dictionary)
34 {
35 GetSymbol() = Dictionary
36 GetParentType() = 0
37 GetGenericDeclaration() = TypeSymbol(Dictionary)
38 GetGenericArguments() = [int, string]
39 }
40 ================================================
对于展开前的类型结构,A.S.T其实上是指向了GetSymbol()是A.S.T,而ParentType()是A.S的这样一个结构。然后再经过符号表把所有类型别名的目标类型(譬如A.S就是B)拿出来,替换掉必要的模板参数,最后获得展开后的类型。

因为有了继承关系、父子类型和类型别名,所以在判断他们的accessor(也就是public、protected、private、internal和protected internal)是否可见的时候,就非常复杂。代码已经上传到Vczh Library++3.0的主页了,下面是核心函数的代码:

1 void EnsureTypeVisibility(
2 ManagedLanguageElement* languageElement,
3 ManagedTypeSymbol* type,
4 const MAP& argument,
5 List& thisTypes,
6 List& baseTypes
7 )
8 {
9 List typeLevels;
10 {
11 ManagedTypeSymbol* currentType=type;
12 while(currentType)
13 {
14 typeLevels.Add(currentType);
15 currentType=currentType->GetGenericDeclaration()
16 currentType->GetGenericDeclaration()->GetParentType()
17 :currentType->GetParentType()
18 ;
19 }
20 }
21
22 ManagedTypeSymbol* parentType=0;
23 for(vint i=typeLevels.Count()-1;i>=0;i--)
24 {
25 ManagedTypeSymbol* currentType=typeLevels[i];
26 ManagedTypeSymbol* currentDeclaration=currentType->GetGenericDeclaration()
27 currentType->GetGenericDeclaration()
28 :currentType
29 ;
30 if(currentType->GetGenericDeclaration())
31

首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDU 1116 Play on Words 下一篇pipe 函数 (C语言)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: