comparison dmd/attrib.c @ 336:aaade6ded589 trunk

[svn r357] Merged DMD 1.033
author lindquist
date Sat, 12 Jul 2008 19:38:31 +0200
parents 5acec6b2eef8
children 44f08170f4ef
comparison
equal deleted inserted replaced
335:17b844102023 336:aaade6ded589
1 1
2 // Compiler implementation of the D programming language 2 // Compiler implementation of the D programming language
3 // Copyright (c) 1999-2007 by Digital Mars 3 // Copyright (c) 1999-2008 by Digital Mars
4 // All Rights Reserved 4 // All Rights Reserved
5 // written by Walter Bright 5 // written by Walter Bright
6 // http://www.digitalmars.com 6 // http://www.digitalmars.com
7 // License for redistribution is by either the Artistic License 7 // License for redistribution is by either the Artistic License
8 // in artistic.txt, or the GNU General Public License in gnu.txt. 8 // in artistic.txt, or the GNU General Public License in gnu.txt.
33 33
34 #include "../gen/enums.h" 34 #include "../gen/enums.h"
35 #include "../gen/logger.h" 35 #include "../gen/logger.h"
36 36
37 extern void obj_includelib(char *name); 37 extern void obj_includelib(char *name);
38 void obj_startaddress(Symbol *s);
38 39
39 40
40 /********************************* AttribDeclaration ****************************/ 41 /********************************* AttribDeclaration ****************************/
41 42
42 AttribDeclaration::AttribDeclaration(Array *decl) 43 AttribDeclaration::AttribDeclaration(Array *decl)
50 return decl; 51 return decl;
51 } 52 }
52 53
53 int AttribDeclaration::addMember(Scope *sc, ScopeDsymbol *sd, int memnum) 54 int AttribDeclaration::addMember(Scope *sc, ScopeDsymbol *sd, int memnum)
54 { 55 {
55 unsigned i;
56 int m = 0; 56 int m = 0;
57 Array *d = include(sc, sd); 57 Array *d = include(sc, sd);
58 58
59 if (d) 59 if (d)
60 { 60 {
61 for (i = 0; i < d->dim; i++) 61 for (unsigned i = 0; i < d->dim; i++)
62 { Dsymbol *s; 62 { Dsymbol *s = (Dsymbol *)d->data[i];
63
64 s = (Dsymbol *)d->data[i];
65 m |= s->addMember(sc, sd, m | memnum); 63 m |= s->addMember(sc, sd, m | memnum);
66 } 64 }
67 } 65 }
68 return m; 66 return m;
69 } 67 }
70 68
71 void AttribDeclaration::semantic(Scope *sc) 69 void AttribDeclaration::semantic(Scope *sc)
72 { 70 {
73 Array *d = include(sc, NULL); 71 Array *d = include(sc, NULL);
74 72
75 //printf("\tAttribDeclaration::semantic '%s'\n",toChars()); 73 //printf("\tAttribDeclaration::semantic '%s', d = %p\n",toChars(), d);
76 if (d) 74 if (d)
77 { 75 {
78 for (unsigned i = 0; i < d->dim; i++) 76 for (unsigned i = 0; i < d->dim; i++)
79 { 77 {
80 Dsymbol *s = (Dsymbol *)d->data[i]; 78 Dsymbol *s = (Dsymbol *)d->data[i];
84 } 82 }
85 } 83 }
86 84
87 void AttribDeclaration::semantic2(Scope *sc) 85 void AttribDeclaration::semantic2(Scope *sc)
88 { 86 {
89 unsigned i;
90 Array *d = include(sc, NULL); 87 Array *d = include(sc, NULL);
91 88
92 if (d) 89 if (d)
93 { 90 {
94 for (i = 0; i < d->dim; i++) 91 for (unsigned i = 0; i < d->dim; i++)
95 { Dsymbol *s; 92 { Dsymbol *s = (Dsymbol *)d->data[i];
96
97 s = (Dsymbol *)d->data[i];
98 s->semantic2(sc); 93 s->semantic2(sc);
99 } 94 }
100 } 95 }
101 } 96 }
102 97
103 void AttribDeclaration::semantic3(Scope *sc) 98 void AttribDeclaration::semantic3(Scope *sc)
104 { 99 {
105 unsigned i;
106 Array *d = include(sc, NULL); 100 Array *d = include(sc, NULL);
107 101
108 if (d) 102 if (d)
109 { 103 {
110 for (i = 0; i < d->dim; i++) 104 for (unsigned i = 0; i < d->dim; i++)
111 { Dsymbol *s; 105 { Dsymbol *s = (Dsymbol *)d->data[i];
112
113 s = (Dsymbol *)d->data[i];
114 s->semantic3(sc); 106 s->semantic3(sc);
115 } 107 }
116 } 108 }
117 } 109 }
118 110
119 void AttribDeclaration::inlineScan() 111 void AttribDeclaration::inlineScan()
120 { 112 {
121 unsigned i;
122 Array *d = include(NULL, NULL); 113 Array *d = include(NULL, NULL);
123 114
124 if (d) 115 if (d)
125 { 116 {
126 for (i = 0; i < d->dim; i++) 117 for (unsigned i = 0; i < d->dim; i++)
127 { Dsymbol *s; 118 { Dsymbol *s = (Dsymbol *)d->data[i];
128
129 s = (Dsymbol *)d->data[i];
130 //printf("AttribDeclaration::inlineScan %s\n", s->toChars()); 119 //printf("AttribDeclaration::inlineScan %s\n", s->toChars());
131 s->inlineScan(); 120 s->inlineScan();
132 } 121 }
133 } 122 }
134 } 123 }
135 124
136 void AttribDeclaration::addComment(unsigned char *comment) 125 void AttribDeclaration::addComment(unsigned char *comment)
137 { 126 {
138 if (comment) 127 if (comment)
139 { 128 {
140 unsigned i;
141 Array *d = include(NULL, NULL); 129 Array *d = include(NULL, NULL);
142 130
143 if (d) 131 if (d)
144 { 132 {
145 for (i = 0; i < d->dim; i++) 133 for (unsigned i = 0; i < d->dim; i++)
146 { Dsymbol *s; 134 { Dsymbol *s = (Dsymbol *)d->data[i];
147
148 s = (Dsymbol *)d->data[i];
149 //printf("AttribDeclaration::addComment %s\n", s->toChars()); 135 //printf("AttribDeclaration::addComment %s\n", s->toChars());
150 s->addComment(comment); 136 s->addComment(comment);
151 } 137 }
152 } 138 }
153 } 139 }
161 * a template, then include(NULL, NULL) will fail. 147 * a template, then include(NULL, NULL) will fail.
162 */ 148 */
163 // if (sc->docbuf) 149 // if (sc->docbuf)
164 // return; 150 // return;
165 151
166 unsigned i;
167 Array *d = include(NULL, NULL); 152 Array *d = include(NULL, NULL);
168 153
169 if (d) 154 if (d)
170 { 155 {
171 for (i = 0; i < d->dim; i++) 156 for (unsigned i = 0; i < d->dim; i++)
172 { Dsymbol *s; 157 { Dsymbol *s = (Dsymbol *)d->data[i];
173
174 s = (Dsymbol *)d->data[i];
175 //printf("AttribDeclaration::emitComment %s\n", s->toChars()); 158 //printf("AttribDeclaration::emitComment %s\n", s->toChars());
176 s->emitComment(sc); 159 s->emitComment(sc);
177 } 160 }
178 } 161 }
179 } 162 }
180 163
181 void AttribDeclaration::toObjFile() 164 void AttribDeclaration::toObjFile(int multiobj)
182 { 165 {
183 unsigned i;
184 Array *d = include(NULL, NULL); 166 Array *d = include(NULL, NULL);
185 167
186 if (d) 168 if (d)
187 { 169 {
188 for (i = 0; i < d->dim; i++) 170 for (unsigned i = 0; i < d->dim; i++)
189 { Dsymbol *s; 171 { Dsymbol *s = (Dsymbol *)d->data[i];
190 172 s->toObjFile(multiobj);
191 s = (Dsymbol *)d->data[i];
192 s->toObjFile();
193 } 173 }
194 } 174 }
195 } 175 }
196 176
197 int AttribDeclaration::cvMember(unsigned char *p) 177 int AttribDeclaration::cvMember(unsigned char *p)
198 { 178 {
199 unsigned i;
200 int nwritten = 0; 179 int nwritten = 0;
201 int n; 180 int n;
202 Array *d = include(NULL, NULL); 181 Array *d = include(NULL, NULL);
203 182
204 if (d) 183 if (d)
205 { 184 {
206 for (i = 0; i < d->dim; i++) 185 for (unsigned i = 0; i < d->dim; i++)
207 { Dsymbol *s; 186 { Dsymbol *s = (Dsymbol *)d->data[i];
208
209 s = (Dsymbol *)d->data[i];
210 n = s->cvMember(p); 187 n = s->cvMember(p);
211 if (p) 188 if (p)
212 p += n; 189 p += n;
213 nwritten += n; 190 nwritten += n;
214 } 191 }
230 } 207 }
231 } 208 }
232 return 0; 209 return 0;
233 } 210 }
234 211
235 char *AttribDeclaration::kind() 212 const char *AttribDeclaration::kind()
236 { 213 {
237 return "attribute"; 214 return "attribute";
238 } 215 }
239 216
240 int AttribDeclaration::oneMember(Dsymbol **ps) 217 int AttribDeclaration::oneMember(Dsymbol **ps)
244 return Dsymbol::oneMembers(d, ps); 221 return Dsymbol::oneMembers(d, ps);
245 } 222 }
246 223
247 void AttribDeclaration::checkCtorConstInit() 224 void AttribDeclaration::checkCtorConstInit()
248 { 225 {
249 unsigned i;
250 Array *d = include(NULL, NULL); 226 Array *d = include(NULL, NULL);
251 227
252 if (d) 228 if (d)
253 { 229 {
254 for (i = 0; i < d->dim; i++) 230 for (unsigned i = 0; i < d->dim; i++)
255 { Dsymbol *s; 231 { Dsymbol *s = (Dsymbol *)d->data[i];
256
257 s = (Dsymbol *)d->data[i];
258 s->checkCtorConstInit(); 232 s->checkCtorConstInit();
259 } 233 }
260 } 234 }
261 } 235 }
262 236
263 /**************************************** 237 /****************************************
264 */ 238 */
265 239
266 void AttribDeclaration::addLocalClass(ClassDeclarations *aclasses) 240 void AttribDeclaration::addLocalClass(ClassDeclarations *aclasses)
267 { unsigned i; 241 {
268 Array *d = include(NULL, NULL); 242 Array *d = include(NULL, NULL);
269 243
270 if (d) 244 if (d)
271 { 245 {
272 for (i = 0; i < d->dim; i++) 246 for (unsigned i = 0; i < d->dim; i++)
273 { Dsymbol *s; 247 { Dsymbol *s = (Dsymbol *)d->data[i];
274
275 s = (Dsymbol *)d->data[i];
276 s->addLocalClass(aclasses); 248 s->addLocalClass(aclasses);
277 } 249 }
278 } 250 }
279 } 251 }
280 252
721 } 693 }
722 } 694 }
723 buf->writestring("}\n"); 695 buf->writestring("}\n");
724 } 696 }
725 697
726 char *AnonDeclaration::kind() 698 const char *AnonDeclaration::kind()
727 { 699 {
728 return (char *)(isunion ? "anonymous union" : "anonymous struct"); 700 return (char *)(isunion ? "anonymous union" : "anonymous struct");
729 } 701 }
730 702
731 /********************************* PragmaDeclaration ****************************/ 703 /********************************* PragmaDeclaration ****************************/
1021 { 993 {
1022 *ps = NULL; 994 *ps = NULL;
1023 return TRUE; 995 return TRUE;
1024 } 996 }
1025 997
1026 char *PragmaDeclaration::kind() 998 const char *PragmaDeclaration::kind()
1027 { 999 {
1028 return "pragma"; 1000 return "pragma";
1029 } 1001 }
1030 1002
1031 void PragmaDeclaration::toObjFile() 1003 void PragmaDeclaration::toObjFile(int multiobj)
1032 { 1004 {
1033 if (ident == Id::lib) 1005 if (ident == Id::lib)
1034 { 1006 {
1035 assert(args && args->dim == 1); 1007 assert(args && args->dim == 1);
1036 1008
1042 char *name = (char *)mem.malloc(se->len + 1); 1014 char *name = (char *)mem.malloc(se->len + 1);
1043 memcpy(name, se->string, se->len); 1015 memcpy(name, se->string, se->len);
1044 name[se->len] = 0; 1016 name[se->len] = 0;
1045 obj_includelib(name); 1017 obj_includelib(name);
1046 } 1018 }
1047 AttribDeclaration::toObjFile(); 1019 AttribDeclaration::toObjFile(multiobj);
1048 } 1020 }
1049 1021
1050 void PragmaDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) 1022 void PragmaDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
1051 { 1023 {
1052 buf->printf("pragma(%s", ident->toChars()); 1024 buf->printf("pragma(%s", ident->toChars());
1212 } 1184 }
1213 1185
1214 1186
1215 int StaticIfDeclaration::addMember(Scope *sc, ScopeDsymbol *sd, int memnum) 1187 int StaticIfDeclaration::addMember(Scope *sc, ScopeDsymbol *sd, int memnum)
1216 { 1188 {
1189 //printf("StaticIfDeclaration::addMember() '%s'\n",toChars());
1217 /* This is deferred until semantic(), so that 1190 /* This is deferred until semantic(), so that
1218 * expressions in the condition can refer to declarations 1191 * expressions in the condition can refer to declarations
1219 * in the same scope, such as: 1192 * in the same scope, such as:
1220 * 1193 *
1221 * template Foo(int i) 1194 * template Foo(int i)
1238 1211
1239 void StaticIfDeclaration::semantic(Scope *sc) 1212 void StaticIfDeclaration::semantic(Scope *sc)
1240 { 1213 {
1241 Array *d = include(sc, sd); 1214 Array *d = include(sc, sd);
1242 1215
1243 //printf("\tStaticIfDeclaration::semantic '%s'\n",toChars()); 1216 //printf("\tStaticIfDeclaration::semantic '%s', d = %p\n",toChars(), d);
1244 if (d) 1217 if (d)
1245 { 1218 {
1246 if (!addisdone) 1219 if (!addisdone)
1247 { AttribDeclaration::addMember(sc, sd, 1); 1220 { AttribDeclaration::addMember(sc, sd, 1);
1248 addisdone = 1; 1221 addisdone = 1;
1255 s->semantic(sc); 1228 s->semantic(sc);
1256 } 1229 }
1257 } 1230 }
1258 } 1231 }
1259 1232
1260 char *StaticIfDeclaration::kind() 1233 const char *StaticIfDeclaration::kind()
1261 { 1234 {
1262 return "static if"; 1235 return "static if";
1263 } 1236 }
1264 1237
1265 1238
1266 /***************************** CompileDeclaration *****************************/ 1239 /***************************** CompileDeclaration *****************************/
1267 1240
1268 CompileDeclaration::CompileDeclaration(Loc loc, Expression *exp) 1241 CompileDeclaration::CompileDeclaration(Loc loc, Expression *exp)
1269 : AttribDeclaration(NULL) 1242 : AttribDeclaration(NULL)
1270 { 1243 {
1244 this->loc = loc;
1271 this->exp = exp; 1245 this->exp = exp;
1272 this->sd = NULL; 1246 this->sd = NULL;
1247 this->compiled = 0;
1273 } 1248 }
1274 1249
1275 Dsymbol *CompileDeclaration::syntaxCopy(Dsymbol *s) 1250 Dsymbol *CompileDeclaration::syntaxCopy(Dsymbol *s)
1276 { 1251 {
1277 //printf("CompileDeclaration::syntaxCopy('%s')\n", toChars()); 1252 //printf("CompileDeclaration::syntaxCopy('%s')\n", toChars());
1279 return sc; 1254 return sc;
1280 } 1255 }
1281 1256
1282 int CompileDeclaration::addMember(Scope *sc, ScopeDsymbol *sd, int memnum) 1257 int CompileDeclaration::addMember(Scope *sc, ScopeDsymbol *sd, int memnum)
1283 { 1258 {
1259 //printf("CompileDeclaration::addMember(sc = %p)\n", sc);
1284 this->sd = sd; 1260 this->sd = sd;
1261 if (memnum == 0)
1262 { /* No members yet, so parse the mixin now
1263 */
1264 compileIt(sc);
1265 memnum |= AttribDeclaration::addMember(sc, sd, memnum);
1266 compiled = 1;
1267 }
1285 return memnum; 1268 return memnum;
1286 } 1269 }
1287 1270
1288 void CompileDeclaration::semantic(Scope *sc) 1271 void CompileDeclaration::compileIt(Scope *sc)
1289 { 1272 {
1290 //printf("CompileDeclaration::semantic()\n"); 1273 //printf("CompileDeclaration::compileIt()\n");
1291 exp = exp->semantic(sc); 1274 exp = exp->semantic(sc);
1292 exp = resolveProperties(sc, exp); 1275 exp = resolveProperties(sc, exp);
1293 exp = exp->optimize(WANTvalue | WANTinterpret); 1276 exp = exp->optimize(WANTvalue | WANTinterpret);
1294 if (exp->op != TOKstring) 1277 if (exp->op != TOKstring)
1295 { error("argument to mixin must be a string, not (%s)", exp->toChars()); 1278 { exp->error("argument to mixin must be a string, not (%s)", exp->toChars());
1296 return; 1279 }
1297 } 1280 else
1298 StringExp *se = (StringExp *)exp; 1281 {
1299 se = se->toUTF8(sc); 1282 StringExp *se = (StringExp *)exp;
1300 Parser p(sc->module, (unsigned char *)se->string, se->len, 0); 1283 se = se->toUTF8(sc);
1301 p.loc = loc; 1284 Parser p(sc->module, (unsigned char *)se->string, se->len, 0);
1302 p.nextToken(); 1285 p.loc = loc;
1303 decl = p.parseDeclDefs(0); 1286 p.nextToken();
1304 if (p.token.value != TOKeof) 1287 decl = p.parseDeclDefs(0);
1305 { 1288 if (p.token.value != TOKeof)
1306 error("incomplete mixin declaration (%s)", se->toChars()); 1289 exp->error("incomplete mixin declaration (%s)", se->toChars());
1307 } 1290 }
1308 1291 }
1309 AttribDeclaration::addMember(sc, sd, 0); 1292
1293 void CompileDeclaration::semantic(Scope *sc)
1294 {
1295 //printf("CompileDeclaration::semantic()\n");
1296
1297 if (!compiled)
1298 {
1299 compileIt(sc);
1300 AttribDeclaration::addMember(sc, sd, 0);
1301 compiled = 1;
1302 }
1310 AttribDeclaration::semantic(sc); 1303 AttribDeclaration::semantic(sc);
1311 } 1304 }
1312 1305
1313 void CompileDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs) 1306 void CompileDeclaration::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
1314 { 1307 {