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