Mercurial > projects > dil
annotate trunk/src/dil/Scope.d @ 589:de365ddcfbd4
Moved dil.Symbol to dil.semantic.Symbol.
author | Aziz K?ksal <aziz.koeksal@gmail.com> |
---|---|
date | Sat, 05 Jan 2008 23:40:54 +0100 |
parents | ae8f58a13917 |
children | 641041912670 |
rev | line source |
---|---|
407
5431c0faf3b5
Added modules dil.Scope and dil.Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
1 /++ |
5431c0faf3b5
Added modules dil.Scope and dil.Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
2 Author: Aziz Köksal |
5431c0faf3b5
Added modules dil.Scope and dil.Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
3 License: GPL3 |
5431c0faf3b5
Added modules dil.Scope and dil.Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
4 +/ |
5431c0faf3b5
Added modules dil.Scope and dil.Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
5 module dil.Scope; |
560
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
6 |
589
de365ddcfbd4
Moved dil.Symbol to dil.semantic.Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
574
diff
changeset
|
7 import dil.semantic.Symbol; |
560
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
8 import dil.Symbols; |
486
bd176bc73e43
Fixed a few things in the Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
430
diff
changeset
|
9 import dil.Information; |
560
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
10 import dil.Messages; |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
11 import dil.Token; |
407
5431c0faf3b5
Added modules dil.Scope and dil.Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
12 import common; |
5431c0faf3b5
Added modules dil.Scope and dil.Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
13 |
5431c0faf3b5
Added modules dil.Scope and dil.Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
14 class Scope |
5431c0faf3b5
Added modules dil.Scope and dil.Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
15 { |
430
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
16 Scope parent; /// The surrounding scope. |
532
50e64bab9c7a
Renamed InformationManager to InfoManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
514
diff
changeset
|
17 InfoManager infoMan; /// Collects errors reported during the semantic phase. |
407
5431c0faf3b5
Added modules dil.Scope and dil.Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
18 |
560
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
19 ScopeSymbol symbol; /// The current symbol with the symbol table. |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
20 |
430
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
21 this() |
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
22 { |
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
23 } |
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
24 |
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
25 /++ |
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
26 Find an identifier in this scope. |
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
27 +/ |
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
28 Symbol find(char[] ident) |
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
29 { |
486
bd176bc73e43
Fixed a few things in the Parser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
430
diff
changeset
|
30 return null; |
430
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
31 } |
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
32 |
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
33 /++ |
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
34 Add a symbol to this scope. |
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
35 +/ |
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
36 void add(Symbol sym) |
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
37 { |
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
38 |
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
39 } |
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
40 |
562
b0533550d64c
Added semantic() to VariableDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
560
diff
changeset
|
41 /// Insert a new variable symbol into this scope. |
b0533550d64c
Added semantic() to VariableDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
560
diff
changeset
|
42 void insert(Variable var) |
b0533550d64c
Added semantic() to VariableDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
560
diff
changeset
|
43 { |
b0533550d64c
Added semantic() to VariableDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
560
diff
changeset
|
44 auto sym = symbol.lookup(var.ident); |
b0533550d64c
Added semantic() to VariableDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
560
diff
changeset
|
45 if (sym) |
569
86fa0d36da51
Reporting location of symbol conflict error more accurately.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
567
diff
changeset
|
46 { |
86fa0d36da51
Reporting location of symbol conflict error more accurately.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
567
diff
changeset
|
47 auto loc = sym.node.begin.getLocation(); |
86fa0d36da51
Reporting location of symbol conflict error more accurately.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
567
diff
changeset
|
48 auto locString = Format("{}({},{})", loc.filePath, loc.lineNum, loc.colNum); |
574
ae8f58a13917
Added messages to struct MSG.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
570
diff
changeset
|
49 error(var.node.begin, MSG.VariableConflictsWithDecl, var.ident.str, locString); |
569
86fa0d36da51
Reporting location of symbol conflict error more accurately.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
567
diff
changeset
|
50 } |
562
b0533550d64c
Added semantic() to VariableDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
560
diff
changeset
|
51 else |
b0533550d64c
Added semantic() to VariableDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
560
diff
changeset
|
52 symbol.insert(var, var.ident); |
570
3ebdc510a7fc
Added semantic() to InterfaceDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
569
diff
changeset
|
53 // Set the current scope symbol as the parent. |
3ebdc510a7fc
Added semantic() to InterfaceDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
569
diff
changeset
|
54 var.parent = symbol; |
562
b0533550d64c
Added semantic() to VariableDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
560
diff
changeset
|
55 } |
b0533550d64c
Added semantic() to VariableDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
560
diff
changeset
|
56 |
430
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
57 /++ |
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
58 Create a new inner scope. |
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
59 +/ |
565
184a8d8bad2e
Added semantic() to StructDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
564
diff
changeset
|
60 Scope push(ScopeSymbol symbol) |
430
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
61 { |
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
62 auto sc = new Scope(); |
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
63 sc.parent = this; |
564
3c867a683258
Fixed VariableDeclaration.semantic().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
562
diff
changeset
|
64 sc.infoMan = this.infoMan; |
565
184a8d8bad2e
Added semantic() to StructDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
564
diff
changeset
|
65 sc.symbol = symbol; |
430
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
66 return sc; |
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
67 } |
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
68 |
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
69 /++ |
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
70 Destroy this scope and return the outer scope. |
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
71 +/ |
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
72 Scope pop() |
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
73 { |
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
74 auto sc = parent; |
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
75 // delete this; |
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
76 return sc; |
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
77 } |
490
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
486
diff
changeset
|
78 |
570
3ebdc510a7fc
Added semantic() to InterfaceDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
569
diff
changeset
|
79 bool isInterface() |
3ebdc510a7fc
Added semantic() to InterfaceDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
569
diff
changeset
|
80 { |
3ebdc510a7fc
Added semantic() to InterfaceDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
569
diff
changeset
|
81 return symbol.isInterface; |
3ebdc510a7fc
Added semantic() to InterfaceDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
569
diff
changeset
|
82 } |
3ebdc510a7fc
Added semantic() to InterfaceDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
569
diff
changeset
|
83 |
560
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
84 /// Search for the enclosing Class scope. |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
85 Scope classScope() |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
86 { |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
87 auto scop = this; |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
88 while (scop) |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
89 { |
567
ab9f5020cd02
Added 'is-methods' to class Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
565
diff
changeset
|
90 if (scop.symbol.isClass) |
560
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
91 return scop; |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
92 scop = scop.parent; |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
93 } |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
94 return null; |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
95 } |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
96 |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
97 /// Search for the enclosing Module scope. |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
98 Scope moduleScope() |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
99 { |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
100 auto scop = this; |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
101 while (scop) |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
102 { |
567
ab9f5020cd02
Added 'is-methods' to class Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
565
diff
changeset
|
103 if (scop.symbol.isModule) |
560
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
104 return scop; |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
105 scop = scop.parent; |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
106 } |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
107 return null; |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
108 } |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
109 |
490
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
486
diff
changeset
|
110 void error(Token* token, MID mid) |
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
486
diff
changeset
|
111 { |
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
486
diff
changeset
|
112 auto location = token.getLocation(); |
562
b0533550d64c
Added semantic() to VariableDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
560
diff
changeset
|
113 infoMan ~= new SemanticError(location, GetMsg(mid)); |
b0533550d64c
Added semantic() to VariableDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
560
diff
changeset
|
114 } |
b0533550d64c
Added semantic() to VariableDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
560
diff
changeset
|
115 |
574
ae8f58a13917
Added messages to struct MSG.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
570
diff
changeset
|
116 void error(Token* token, char[] formatMsg, ...) |
562
b0533550d64c
Added semantic() to VariableDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
560
diff
changeset
|
117 { |
569
86fa0d36da51
Reporting location of symbol conflict error more accurately.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
567
diff
changeset
|
118 auto location = token.getLocation(); |
574
ae8f58a13917
Added messages to struct MSG.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
570
diff
changeset
|
119 auto msg = Format(_arguments, _argptr, formatMsg); |
562
b0533550d64c
Added semantic() to VariableDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
560
diff
changeset
|
120 infoMan ~= new SemanticError(location, msg); |
490
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
486
diff
changeset
|
121 } |
407
5431c0faf3b5
Added modules dil.Scope and dil.Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
122 } |