129
|
1 module sema.Symbol;
|
|
2
|
|
3 import tango.text.convert.Integer : format;
|
|
4 import tango.io.Stdout;
|
|
5
|
|
6 import sema.DType;
|
|
7
|
|
8 ///
|
|
9 class Symbol
|
|
10 {
|
|
11 /// Create a root symbol - representing a module
|
|
12 this() { actual = this; }
|
|
13
|
|
14 /// Get a simple human readable name (bar)
|
|
15 char[] getName() { return name; }
|
|
16
|
|
17 /// Get a human readable name (foo.bar)
|
|
18 char[] getFQN()
|
|
19 {
|
|
20 char[] prefix;
|
|
21 if (parent !is null && parent.name !is null)
|
|
22 prefix ~= parent.getFQN() ~ ".";
|
|
23 return prefix ~ name;
|
|
24 }
|
|
25
|
|
26 /// Get a machine readable name (_D3foo3barFZi)
|
|
27 char[] getMangledFQN()
|
|
28 {
|
|
29 char[] n = `_D`;
|
|
30 Symbol p = parent;
|
|
31 while (p !is null) {
|
|
32 n ~= p.internalFQN();
|
|
33 p = p.parent;
|
|
34 }
|
|
35 n ~= internalFQN();
|
|
36 n ~= type.mangle();
|
|
37 return n;
|
|
38 }
|
|
39
|
|
40 /**
|
|
41 Try to find a contained symbol with the given name - returns null if not
|
|
42 found
|
|
43 **/
|
|
44 Symbol findMember(char[] member)
|
|
45 {
|
|
46 Stdout.formatln("Trying to find {} in {}", member, name);
|
|
47 foreach (possible; contained)
|
|
48 if (possible.name == member)
|
|
49 {
|
|
50 Stdout.formatln(" - found it: {} ({})", possible.getFQN(), possible.getMangledFQN());
|
|
51 return possible;
|
|
52 }
|
|
53 return null;
|
|
54 }
|
|
55
|
|
56 void dump()
|
|
57 {
|
|
58 Stdout("Symbol: ");
|
|
59 Symbol p = parent;
|
|
60 while (p !is null) {
|
|
61 Stdout.format("{}.", p.name);
|
|
62 p = p.parent;
|
|
63 }
|
|
64 Stdout.formatln("{}", name);
|
|
65 }
|
|
66
|
|
67 /// Create a member with the given name and type
|
|
68 Symbol createMember(char[] member, DType type)
|
|
69 {
|
|
70 //Stdout.formatln("Creating {} of type {} in {}", member, type.mangle, name);
|
|
71 auto res = new Symbol(member, type, this);
|
|
72 contained ~= res;
|
|
73 return res;
|
|
74 }
|
|
75
|
|
76 /**
|
|
77 Create an alias of another symbol with the given name.
|
|
78
|
|
79 The target symbol can be a member of another symbol
|
|
80 **/
|
|
81 Symbol createAlias(char[] aliasedName, Symbol target)
|
|
82 {
|
|
83 auto res = new Symbol(aliasedName, target, this);
|
|
84 contained ~= res;
|
|
85 return res;
|
|
86 }
|
|
87
|
|
88 // The type of this symbol
|
|
89 DType type;
|
|
90
|
|
91 private:
|
|
92 // Helper for getMangledFQN - gets the FQN without _D and the type
|
|
93 char[] internalFQN()
|
|
94 {
|
|
95 if (actual.name !is null && actual.name.length > 0)
|
|
96 {
|
|
97 char[32] len;
|
|
98 return format(len, actual.name.length) ~ actual.name;
|
|
99 }
|
|
100 return "";
|
|
101 }
|
|
102
|
|
103 this(char[] name, Symbol actual, Symbol parent)
|
|
104 {
|
|
105 this.name = name;
|
|
106 this.actual = actual;
|
|
107 this.parent = parent;
|
|
108 this.type = actual.type;
|
|
109 }
|
|
110
|
|
111 this(char[] name, DType type, Symbol parent)
|
|
112 {
|
|
113 this.name = name;
|
|
114 this.actual = this;
|
|
115 this.parent = parent;
|
|
116 this.type = type;
|
|
117 }
|
|
118
|
|
119 private:
|
|
120 char[] name;
|
|
121
|
|
122 // If the symbol is an alias, this will point to the actual symbol
|
|
123 Symbol actual;
|
|
124 // If this symbol is contained within a struct or similar this will point
|
|
125 // to that symbol
|
|
126 Symbol parent;
|
|
127 // All the symbols contained within this symbol
|
|
128 Symbol[] contained;
|
|
129
|
|
130 // The module that contains this symbol (root of the parent-chain)
|
|
131 // DModule mod;
|
|
132 }
|
|
133
|