Mercurial > projects > dil
annotate trunk/src/dil/semantic/Symbols.d @ 623:e2cd28cfc6ae
Added dil.semantic.Template.
author | Aziz K?ksal <aziz.koeksal@gmail.com> |
---|---|
date | Fri, 11 Jan 2008 01:45:24 +0100 |
parents | 0749f30ef2d0 |
children | 1ae72234db26 |
rev | line source |
---|---|
517 | 1 /++ |
2 Author: Aziz Köksal | |
3 License: GPL3 | |
4 +/ | |
590
641041912670
Moved dil.Symbols to dil.semantic.Symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
589
diff
changeset
|
5 module dil.semantic.Symbols; |
560
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
6 |
589
de365ddcfbd4
Moved dil.Symbol to dil.semantic.Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
584
diff
changeset
|
7 import dil.semantic.Symbol; |
591
26addda6365b
Moved dil.SymbolTable to dil.semantic.SymbolTable.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
590
diff
changeset
|
8 import dil.semantic.SymbolTable; |
600
041eae272362
Moved dil.Identifier to dil.lexer.Identifier.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
591
diff
changeset
|
9 import dil.semantic.Types; |
580
fa6d3c52757d
Moved SyntaxTree.d to new package 'ast'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
573
diff
changeset
|
10 import dil.ast.Node; |
600
041eae272362
Moved dil.Identifier to dil.lexer.Identifier.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
591
diff
changeset
|
11 import dil.lexer.Identifier; |
560
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
12 import dil.Enums; |
517 | 13 import common; |
14 | |
560
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
15 /// A symbol that has its own scope with a symbol table. |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
16 class ScopeSymbol : Symbol |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
17 { |
614 | 18 SymbolTable symbolTable; /// The symbol table. |
19 Symbol[] members; /// The member symbols (in lexical order.) | |
560
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
20 |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
21 this() |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
22 { |
562
b0533550d64c
Added semantic() to VariableDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
561
diff
changeset
|
23 } |
b0533550d64c
Added semantic() to VariableDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
561
diff
changeset
|
24 |
b0533550d64c
Added semantic() to VariableDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
561
diff
changeset
|
25 /// Look up ident in the table. |
b0533550d64c
Added semantic() to VariableDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
561
diff
changeset
|
26 Symbol lookup(Identifier* ident) |
b0533550d64c
Added semantic() to VariableDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
561
diff
changeset
|
27 { |
b0533550d64c
Added semantic() to VariableDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
561
diff
changeset
|
28 return symbolTable.lookup(ident); |
b0533550d64c
Added semantic() to VariableDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
561
diff
changeset
|
29 } |
b0533550d64c
Added semantic() to VariableDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
561
diff
changeset
|
30 |
b0533550d64c
Added semantic() to VariableDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
561
diff
changeset
|
31 /// Insert a symbol into the table. |
b0533550d64c
Added semantic() to VariableDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
561
diff
changeset
|
32 void insert(Symbol s, Identifier* ident) |
b0533550d64c
Added semantic() to VariableDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
561
diff
changeset
|
33 { |
b0533550d64c
Added semantic() to VariableDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
561
diff
changeset
|
34 symbolTable.insert(s, ident); |
614 | 35 members ~= s; |
560
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
36 } |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
37 } |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
38 |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
39 /// Aggregates have function and field members. |
615
a05457530ac2
Added member ident to class Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
614
diff
changeset
|
40 abstract class Aggregate : ScopeSymbol |
517 | 41 { |
42 Function[] funcs; | |
43 Variable[] fields; | |
562
b0533550d64c
Added semantic() to VariableDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
561
diff
changeset
|
44 |
b0533550d64c
Added semantic() to VariableDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
561
diff
changeset
|
45 override void insert(Symbol s, Identifier* ident) |
b0533550d64c
Added semantic() to VariableDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
561
diff
changeset
|
46 { |
567
ab9f5020cd02
Added 'is-methods' to class Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
565
diff
changeset
|
47 if (s.isVariable) |
562
b0533550d64c
Added semantic() to VariableDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
561
diff
changeset
|
48 // Append variable to fields. |
b0533550d64c
Added semantic() to VariableDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
561
diff
changeset
|
49 fields ~= cast(Variable)cast(void*)s; |
567
ab9f5020cd02
Added 'is-methods' to class Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
565
diff
changeset
|
50 else if (s.isFunction) |
562
b0533550d64c
Added semantic() to VariableDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
561
diff
changeset
|
51 // Append function to funcs. |
b0533550d64c
Added semantic() to VariableDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
561
diff
changeset
|
52 funcs ~= cast(Function)cast(void*)s; |
b0533550d64c
Added semantic() to VariableDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
561
diff
changeset
|
53 super.insert(s, ident); |
b0533550d64c
Added semantic() to VariableDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
561
diff
changeset
|
54 } |
517 | 55 } |
56 | |
57 class Class : Aggregate | |
58 { | |
573
87c4474a1295
Added semantic() to ClassDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
572
diff
changeset
|
59 this(Identifier* ident, Node classNode) |
560
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
60 { |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
61 this.sid = SYM.Class; |
573
87c4474a1295
Added semantic() to ClassDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
572
diff
changeset
|
62 this.ident = ident; |
87c4474a1295
Added semantic() to ClassDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
572
diff
changeset
|
63 this.node = classNode; |
560
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
64 } |
517 | 65 } |
66 | |
561
302e50e71ec2
Added Interface symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
560
diff
changeset
|
67 class Interface : Aggregate |
302e50e71ec2
Added Interface symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
560
diff
changeset
|
68 { |
572
751d84733e07
Added semantic() to UnionDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
570
diff
changeset
|
69 this(Identifier* ident, Node interfaceNode) |
561
302e50e71ec2
Added Interface symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
560
diff
changeset
|
70 { |
302e50e71ec2
Added Interface symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
560
diff
changeset
|
71 this.sid = SYM.Interface; |
570
3ebdc510a7fc
Added semantic() to InterfaceDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
568
diff
changeset
|
72 this.ident = ident; |
572
751d84733e07
Added semantic() to UnionDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
570
diff
changeset
|
73 this.node = interfaceNode; |
561
302e50e71ec2
Added Interface symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
560
diff
changeset
|
74 } |
302e50e71ec2
Added Interface symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
560
diff
changeset
|
75 } |
302e50e71ec2
Added Interface symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
560
diff
changeset
|
76 |
517 | 77 class Union : Aggregate |
78 { | |
572
751d84733e07
Added semantic() to UnionDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
570
diff
changeset
|
79 this(Identifier* ident, Node unionNode) |
560
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
80 { |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
81 this.sid = SYM.Union; |
572
751d84733e07
Added semantic() to UnionDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
570
diff
changeset
|
82 this.ident = ident; |
751d84733e07
Added semantic() to UnionDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
570
diff
changeset
|
83 this.node = unionNode; |
560
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
84 } |
517 | 85 } |
86 | |
87 class Struct : Aggregate | |
88 { | |
570
3ebdc510a7fc
Added semantic() to InterfaceDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
568
diff
changeset
|
89 this(Identifier* ident, Node structNode) |
560
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
90 { |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
91 this.sid = SYM.Struct; |
570
3ebdc510a7fc
Added semantic() to InterfaceDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
568
diff
changeset
|
92 this.ident = ident; |
3ebdc510a7fc
Added semantic() to InterfaceDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
568
diff
changeset
|
93 this.node = structNode; |
560
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
94 } |
517 | 95 } |
96 | |
614 | 97 class Enum : ScopeSymbol |
98 { | |
617
0749f30ef2d0
Added member 'EnumType type' to class Enum.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
615
diff
changeset
|
99 EnumType type; |
614 | 100 this(Identifier* ident, Node enumNode) |
101 { | |
102 this.sid = SYM.Enum; | |
103 this.ident = ident; | |
104 this.node = enumNode; | |
105 } | |
617
0749f30ef2d0
Added member 'EnumType type' to class Enum.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
615
diff
changeset
|
106 |
0749f30ef2d0
Added member 'EnumType type' to class Enum.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
615
diff
changeset
|
107 void setType(EnumType type) |
0749f30ef2d0
Added member 'EnumType type' to class Enum.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
615
diff
changeset
|
108 { |
0749f30ef2d0
Added member 'EnumType type' to class Enum.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
615
diff
changeset
|
109 this.type = type; |
0749f30ef2d0
Added member 'EnumType type' to class Enum.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
615
diff
changeset
|
110 } |
614 | 111 } |
112 | |
623
e2cd28cfc6ae
Added dil.semantic.Template.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
617
diff
changeset
|
113 class Template : ScopeSymbol |
e2cd28cfc6ae
Added dil.semantic.Template.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
617
diff
changeset
|
114 { |
e2cd28cfc6ae
Added dil.semantic.Template.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
617
diff
changeset
|
115 this(Identifier* ident, Node templateNode) |
e2cd28cfc6ae
Added dil.semantic.Template.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
617
diff
changeset
|
116 { |
e2cd28cfc6ae
Added dil.semantic.Template.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
617
diff
changeset
|
117 this.sid = SYM.Template; |
e2cd28cfc6ae
Added dil.semantic.Template.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
617
diff
changeset
|
118 this.ident = ident; |
e2cd28cfc6ae
Added dil.semantic.Template.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
617
diff
changeset
|
119 this.node = templateNode; |
e2cd28cfc6ae
Added dil.semantic.Template.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
617
diff
changeset
|
120 } |
e2cd28cfc6ae
Added dil.semantic.Template.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
617
diff
changeset
|
121 } |
e2cd28cfc6ae
Added dil.semantic.Template.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
617
diff
changeset
|
122 |
560
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
123 class Function : ScopeSymbol |
517 | 124 { |
560
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
125 StorageClass stc; |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
126 LinkageType linkType; |
517 | 127 |
560
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
128 Type returnType; |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
129 Identifier* ident; |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
130 Variable[] params; |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
131 |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
132 this() |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
133 { |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
134 this.sid = SYM.Function; |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
135 } |
517 | 136 } |
137 | |
138 class Variable : Symbol | |
139 { | |
615
a05457530ac2
Added member ident to class Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
614
diff
changeset
|
140 StorageClass stc; /// The storage classes. |
a05457530ac2
Added member ident to class Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
614
diff
changeset
|
141 LinkageType linkType; /// The linkage type. |
517 | 142 |
615
a05457530ac2
Added member ident to class Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
614
diff
changeset
|
143 Type type; /// The type of this variable. |
560
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
144 |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
145 this(StorageClass stc, LinkageType linkType, |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
146 Type type, Identifier* ident, Node varDecl) |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
147 { |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
148 this.sid = SYM.Variable; |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
149 |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
150 this.stc = stc; |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
151 this.linkType = linkType; |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
152 this.type = type; |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
153 this.ident = ident; |
568
c8861b452eb3
Added members 'node' and 'parent' to class Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
567
diff
changeset
|
154 this.node = varDecl; |
560
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
517
diff
changeset
|
155 } |
517 | 156 } |