Mercurial > projects > dang
annotate sema/Symbol.d @ 136:2be29b296081
Lots of changes:
- Parsing classes and interfaces
- Fixed some seg faults in sema
- Supporting "private" to some extend
- And a lot of other small fixes
author | johnsen@johnsen-laptop |
---|---|
date | Fri, 11 Jul 2008 21:47:57 +0200 |
parents | 9869194de9b7 |
children | 57b0b4464a0b |
rev | line source |
---|---|
129 | 1 module sema.Symbol; |
2 | |
3 import tango.text.convert.Integer : format; | |
4 import tango.io.Stdout; | |
5 | |
6 import sema.DType; | |
7 | |
136 | 8 import ast.Decl; |
9 | |
129 | 10 /// |
11 class Symbol | |
12 { | |
13 /// Create a root symbol - representing a module | |
14 this() { actual = this; } | |
15 | |
16 /// Get a simple human readable name (bar) | |
17 char[] getName() { return name; } | |
18 | |
19 /// Get a human readable name (foo.bar) | |
20 char[] getFQN() | |
21 { | |
22 char[] prefix; | |
23 if (parent !is null && parent.name !is null) | |
24 prefix ~= parent.getFQN() ~ "."; | |
25 return prefix ~ name; | |
26 } | |
27 | |
28 /// Get a machine readable name (_D3foo3barFZi) | |
29 char[] getMangledFQN() | |
30 { | |
31 char[] n = `_D`; | |
32 Symbol p = parent; | |
33 while (p !is null) { | |
34 n ~= p.internalFQN(); | |
35 p = p.parent; | |
36 } | |
37 n ~= internalFQN(); | |
38 n ~= type.mangle(); | |
39 return n; | |
40 } | |
41 | |
42 /** | |
43 Try to find a contained symbol with the given name - returns null if not | |
44 found | |
45 **/ | |
46 Symbol findMember(char[] member) | |
47 { | |
131
3635fc5428b1
Try and make aliases work
Anders Halager <halager@gmail.com>
parents:
129
diff
changeset
|
48 foreach (possible; actual.contained) |
129 | 49 if (possible.name == member) |
50 return possible; | |
51 return null; | |
52 } | |
53 | |
136 | 54 /** |
55 Get the members of the symbol | |
56 **/ | |
57 Symbol[] getMembers() | |
58 { | |
59 return actual.contained; | |
60 } | |
61 | |
129 | 62 void dump() |
63 { | |
64 Stdout("Symbol: "); | |
65 Symbol p = parent; | |
66 while (p !is null) { | |
67 Stdout.format("{}.", p.name); | |
68 p = p.parent; | |
69 } | |
70 Stdout.formatln("{}", name); | |
71 } | |
72 | |
73 /// Create a member with the given name and type | |
136 | 74 Symbol createMember(char[] member, DType type, Decl decl) |
129 | 75 { |
76 auto res = new Symbol(member, type, this); | |
136 | 77 res.decl = decl; |
131
3635fc5428b1
Try and make aliases work
Anders Halager <halager@gmail.com>
parents:
129
diff
changeset
|
78 actual.contained ~= res; |
129 | 79 return res; |
80 } | |
81 | |
82 /** | |
83 Create an alias of another symbol with the given name. | |
84 | |
85 The target symbol can be a member of another symbol | |
86 **/ | |
136 | 87 Symbol createAlias(char[] aliasedName, Symbol target, Decl decl) |
129 | 88 { |
89 auto res = new Symbol(aliasedName, target, this); | |
136 | 90 res.decl = decl; |
131
3635fc5428b1
Try and make aliases work
Anders Halager <halager@gmail.com>
parents:
129
diff
changeset
|
91 actual.contained ~= res; |
129 | 92 return res; |
93 } | |
94 | |
95 // The type of this symbol | |
96 DType type; | |
136 | 97 // The declaration of this symbol |
98 Decl decl; | |
99 // If the symbol is an alias, this will point to the actual symbol | |
100 Symbol actual; | |
101 // If this symbol is contained within a struct or similar this will point | |
102 // to that symbol | |
103 Symbol parent; | |
129 | 104 |
105 private: | |
106 // Helper for getMangledFQN - gets the FQN without _D and the type | |
107 char[] internalFQN() | |
108 { | |
109 if (actual.name !is null && actual.name.length > 0) | |
110 { | |
111 char[32] len; | |
112 return format(len, actual.name.length) ~ actual.name; | |
113 } | |
114 return ""; | |
115 } | |
116 | |
117 this(char[] name, Symbol actual, Symbol parent) | |
118 { | |
119 this.name = name; | |
120 this.actual = actual; | |
121 this.parent = parent; | |
122 this.type = actual.type; | |
123 } | |
124 | |
125 this(char[] name, DType type, Symbol parent) | |
126 { | |
127 this.name = name; | |
128 this.actual = this; | |
129 this.parent = parent; | |
130 this.type = type; | |
131 } | |
132 | |
133 private: | |
134 char[] name; | |
135 | |
136 // All the symbols contained within this symbol | |
137 Symbol[] contained; | |
138 | |
139 // The module that contains this symbol (root of the parent-chain) | |
140 // DModule mod; | |
141 } | |
142 |