root/branches/dmdfe/delegatize.c

Revision 656, 4.5 kB (checked in by Gregor, 1 year ago)

MERGE: DMD 1.014

Line 
1 // Compiler implementation of the D programming language
2 // Copyright (c) 1999-2007 by Digital Mars
3 // All Rights Reserved
4 // written by Walter Bright
5 // http://www.digitalmars.com
6 // License for redistribution is by either the Artistic License
7 // in artistic.txt, or the GNU General Public License in gnu.txt.
8 // See the included readme.txt for details.
9
10 #include <stdio.h>
11 #include <assert.h>
12
13 #include "mars.h"
14 #include "expression.h"
15 #include "statement.h"
16 #include "mtype.h"
17 #include "utf.h"
18 #include "declaration.h"
19 #include "aggregate.h"
20 #include "scope.h"
21
22 /********************************************
23  * Convert from expression to delegate that returns the expression,
24  * i.e. convert:
25  *  expr
26  * to:
27  *  t delegate() { return expr; }
28  */
29
30 Expression *Expression::toDelegate(Scope *sc, Type *t)
31 {
32     //printf("Expression::toDelegate(t = %s) %s\n", t->toChars(), toChars());
33     TypeFunction *tf = new TypeFunction(NULL, t, 0, LINKd);
34     FuncLiteralDeclaration *fld =
35     new FuncLiteralDeclaration(loc, loc, tf, TOKdelegate, NULL);
36     Expression *e;
37 #if 1
38     sc = sc->push();
39     sc->parent = fld;       // set current function to be the delegate
40     e = this;
41     e->scanForNestedRef(sc);
42     sc = sc->pop();
43 #else
44     e = this->syntaxCopy();
45 #endif
46     Statement *s = new ReturnStatement(loc, e);
47     fld->fbody = s;
48     e = new FuncExp(loc, fld);
49     e = e->semantic(sc);
50     return e;
51 }
52
53 /******************************
54  * Perform scanForNestedRef() on an array of Expressions.
55  */
56
57 void arrayExpressionScanForNestedRef(Scope *sc, Expressions *a)
58 {
59     //printf("arrayExpressionScanForNestedRef(%p)\n", a);
60     if (a)
61     {
62     for (int i = 0; i < a->dim; i++)
63     {   Expression *e = (Expression *)a->data[i];
64
65         if (e)
66         {
67         e->scanForNestedRef(sc);
68         }
69     }
70     }
71 }
72
73 void Expression::scanForNestedRef(Scope *sc)
74 {
75     //printf("Expression::scanForNestedRef(%s)\n", toChars());
76 }
77
78 void SymOffExp::scanForNestedRef(Scope *sc)
79 {
80     //printf("SymOffExp::scanForNestedRef(%s)\n", toChars());
81     VarDeclaration *v = var->isVarDeclaration();
82     if (v)
83     v->checkNestedReference(sc, 0);
84 }
85
86 void VarExp::scanForNestedRef(Scope *sc)
87 {
88     //printf("VarExp::scanForNestedRef(%s)\n", toChars());
89     VarDeclaration *v = var->isVarDeclaration();
90     if (v)
91     v->checkNestedReference(sc, 0);
92 }
93
94 void ThisExp::scanForNestedRef(Scope *sc)
95 {
96     assert(var);
97     var->isVarDeclaration()->checkNestedReference(sc, 0);
98 }
99
100 void SuperExp::scanForNestedRef(Scope *sc)
101 {
102     ThisExp::scanForNestedRef(sc);
103 }
104
105 void FuncExp::scanForNestedRef(Scope *sc)
106 {
107     //printf("FuncExp::scanForNestedRef(%s)\n", toChars());
108     //fd->parent = sc->parent;
109 }
110
111 void DeclarationExp::scanForNestedRef(Scope *sc)
112 {
113     //printf("DeclarationExp::scanForNestedRef() %s\n", toChars());
114     declaration->parent = sc->parent;
115 }
116
117 void NewExp::scanForNestedRef(Scope *sc)
118 {
119     //printf("NewExp::scanForNestedRef(Scope *sc): %s\n", toChars());
120
121     if (thisexp)
122     thisexp->scanForNestedRef(sc);
123     arrayExpressionScanForNestedRef(sc, newargs);
124     arrayExpressionScanForNestedRef(sc, arguments);
125 }
126
127 void UnaExp::scanForNestedRef(Scope *sc)
128 {
129     e1->scanForNestedRef(sc);
130 }
131
132 void BinExp::scanForNestedRef(Scope *sc)
133 {
134     e1->scanForNestedRef(sc);
135     e2->scanForNestedRef(sc);
136 }
137
138 void CallExp::scanForNestedRef(Scope *sc)
139 {
140     //printf("CallExp::scanForNestedRef(Scope *sc): %s\n", toChars());
141     e1->scanForNestedRef(sc);
142     arrayExpressionScanForNestedRef(sc, arguments);
143 }
144
145
146 void IndexExp::scanForNestedRef(Scope *sc)
147 {
148     e1->scanForNestedRef(sc);
149
150     if (lengthVar)
151     {   //printf("lengthVar\n");
152     lengthVar->parent = sc->parent;
153     }
154     e2->scanForNestedRef(sc);
155 }
156
157
158 void SliceExp::scanForNestedRef(Scope *sc)
159 {
160     e1->scanForNestedRef(sc);
161
162     if (lengthVar)
163     {   //printf("lengthVar\n");
164     lengthVar->parent = sc->parent;
165     }
166     if (lwr)
167     lwr->scanForNestedRef(sc);
168     if (upr)
169     upr->scanForNestedRef(sc);
170 }
171
172
173 void ArrayLiteralExp::scanForNestedRef(Scope *sc)
174 {
175     arrayExpressionScanForNestedRef(sc, elements);
176 }
177
178
179 void AssocArrayLiteralExp::scanForNestedRef(Scope *sc)
180 {
181     arrayExpressionScanForNestedRef(sc, keys);
182     arrayExpressionScanForNestedRef(sc, values);
183 }
184
185
186 void StructLiteralExp::scanForNestedRef(Scope *sc)
187 {
188     arrayExpressionScanForNestedRef(sc, elements);
189 }
190
191
192 void TupleExp::scanForNestedRef(Scope *sc)
193 {
194     arrayExpressionScanForNestedRef(sc, exps);
195 }
196
197
198 void ArrayExp::scanForNestedRef(Scope *sc)
199 {
200     e1->scanForNestedRef(sc);
201     arrayExpressionScanForNestedRef(sc, arguments);
202 }
203
204
205 void CondExp::scanForNestedRef(Scope *sc)
206 {
207     econd->scanForNestedRef(sc);
208     e1->scanForNestedRef(sc);
209     e2->scanForNestedRef(sc);
210 }
Note: See TracBrowser for help on using the browser.