Mercurial > projects > ddmd
annotate dmd/IdentifierExp.d @ 178:e3afd1303184
Many small bugs fixed
Made all classes derive from TObject to detect memory leaks (functionality is disabled for now)
Began work on overriding backend memory allocations (to avoid memory leaks)
author | korDen |
---|---|
date | Sun, 17 Oct 2010 07:42:00 +0400 |
parents | e28b18c23469 |
children | cd48cb899aee |
rev | line source |
---|---|
72 | 1 module dmd.IdentifierExp; |
2 | |
114 | 3 import dmd.common; |
72 | 4 import dmd.Expression; |
5 import dmd.Declaration; | |
6 import dmd.TY; | |
7 import dmd.TypePointer; | |
8 import dmd.FuncDeclaration; | |
9 import dmd.TemplateInstance; | |
10 import dmd.TemplateDeclaration; | |
11 import dmd.TemplateExp; | |
12 import dmd.DsymbolExp; | |
13 import dmd.Identifier; | |
14 import dmd.OutBuffer; | |
15 import dmd.Loc; | |
16 import dmd.Scope; | |
17 import dmd.Dsymbol; | |
18 import dmd.WithScopeSymbol; | |
19 import dmd.VarExp; | |
20 import dmd.DotIdExp; | |
21 import dmd.Type; | |
22 import dmd.HdrGenState; | |
23 import dmd.TOK; | |
24 | |
0 | 25 class IdentifierExp : Expression |
26 { | |
27 Identifier ident; | |
28 | |
29 Declaration var; | |
30 | |
31 this(Loc loc, Identifier ident) | |
32 { | |
178 | 33 register(); |
72 | 34 super(loc, TOK.TOKidentifier, IdentifierExp.sizeof); |
0 | 35 this.ident = ident; |
36 } | |
37 | |
38 this(Loc loc, Declaration var) | |
39 { | |
178 | 40 register(); |
72 | 41 assert(false); |
0 | 42 super(loc, TOK.init, 0); |
43 } | |
44 | |
72 | 45 override Expression semantic(Scope sc) |
0 | 46 { |
72 | 47 Dsymbol s; |
48 Dsymbol scopesym; | |
49 | |
50 version (LOGSEMANTIC) { | |
51 printf("IdentifierExp.semantic('%s')\n", ident.toChars()); | |
52 } | |
53 s = sc.search(loc, ident, &scopesym); | |
54 if (s) | |
55 { | |
56 Expression e; | |
57 WithScopeSymbol withsym; | |
58 | |
59 /* See if the symbol was a member of an enclosing 'with' | |
60 */ | |
61 withsym = scopesym.isWithScopeSymbol(); | |
62 if (withsym) | |
63 { | |
64 version (DMDV2) { | |
65 /* Disallow shadowing | |
66 */ | |
67 // First find the scope of the with | |
68 Scope scwith = sc; | |
69 while (scwith.scopesym !is scopesym) | |
70 { | |
71 scwith = scwith.enclosing; | |
72 assert(scwith); | |
73 } | |
74 | |
75 // Look at enclosing scopes for symbols with the same name, | |
76 // in the same function | |
77 for (Scope scx = scwith; scx && scx.func == scwith.func; scx = scx.enclosing) | |
78 { | |
79 Dsymbol s2; | |
80 | |
81 if (scx.scopesym && scx.scopesym.symtab && (s2 = scx.scopesym.symtab.lookup(s.ident)) !is null && s !is s2) | |
82 { | |
83 error("with symbol %s is shadowing local symbol %s", s.toPrettyChars(), s2.toPrettyChars()); | |
84 } | |
85 } | |
86 } | |
87 s = s.toAlias(); | |
88 | |
89 // Same as wthis.ident | |
90 if (s.needThis() || s.isTemplateDeclaration()) | |
91 { | |
92 e = new VarExp(loc, withsym.withstate.wthis); | |
93 e = new DotIdExp(loc, e, ident); | |
94 } | |
95 else | |
96 { | |
97 Type t = withsym.withstate.wthis.type; | |
98 if (t.ty == TY.Tpointer) | |
99 t = (cast(TypePointer)t).next; | |
100 e = typeDotIdExp(loc, t, ident); | |
101 } | |
102 } | |
103 else | |
104 { | |
105 /* If f is really a function template, | |
106 * then replace f with the function template declaration. | |
107 */ | |
108 FuncDeclaration f = s.isFuncDeclaration(); | |
109 if (f && f.parent) | |
110 { | |
111 TemplateInstance ti = f.parent.isTemplateInstance(); | |
112 | |
113 if (ti && !ti.isTemplateMixin() && | |
114 (ti.name == f.ident || ti.toAlias().ident == f.ident) && | |
115 ti.tempdecl && ti.tempdecl.onemember) | |
116 { | |
117 TemplateDeclaration tempdecl = ti.tempdecl; | |
118 | |
119 if (tempdecl.overroot) // if not start of overloaded list of TemplateDeclaration's | |
120 tempdecl = tempdecl.overroot; // then get the start | |
121 | |
122 e = new TemplateExp(loc, tempdecl); | |
123 e = e.semantic(sc); | |
124 | |
125 return e; | |
126 } | |
127 } | |
128 | |
129 // Haven't done overload resolution yet, so pass 1 | |
130 e = new DsymbolExp(loc, s, 1); | |
131 } | |
132 | |
133 return e.semantic(sc); | |
134 } | |
135 | |
136 error("undefined identifier %s", ident.toChars()); | |
137 type = Type.terror; | |
0 | 138 return this; |
139 } | |
140 | |
72 | 141 override string toChars() |
0 | 142 { |
143 return ident.toChars(); | |
144 } | |
145 | |
72 | 146 override void dump(int indent) |
0 | 147 { |
148 assert(false); | |
149 } | |
150 | |
72 | 151 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 152 { |
72 | 153 if (hgs.hdrgen) |
154 buf.writestring(ident.toHChars2()); | |
155 else | |
0 | 156 buf.writestring(ident.toChars()); |
157 } | |
158 | |
72 | 159 override int isLvalue() |
0 | 160 { |
68
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
0
diff
changeset
|
161 return 1; |
0 | 162 } |
163 | |
72 | 164 override Expression toLvalue(Scope sc, Expression e) |
0 | 165 { |
72 | 166 static if (false) { |
167 tym = tybasic(e1.ET.Tty); | |
168 if (!(tyscalar(tym) || tym == TYM.TYstruct || tym == TYM.TYarray && e.Eoper == TOK.TOKaddr)) | |
169 synerr(EM_lvalue); // lvalue expected | |
170 } | |
0 | 171 return this; |
172 } | |
173 } | |
174 |