Mercurial > projects > ldc
comparison dmd2/import.c @ 1526:54b3c1394d62
Merged dmdfe 2.031.
author | Robert Clipsham <robert@octarineparrot.com> |
---|---|
date | Tue, 07 Jul 2009 02:26:11 +0100 |
parents | 638d16625da2 |
children |
comparison
equal
deleted
inserted
replaced
1525:d28cd7c45267 | 1526:54b3c1394d62 |
---|---|
19 #include "scope.h" | 19 #include "scope.h" |
20 #include "hdrgen.h" | 20 #include "hdrgen.h" |
21 #include "mtype.h" | 21 #include "mtype.h" |
22 #include "declaration.h" | 22 #include "declaration.h" |
23 #include "id.h" | 23 #include "id.h" |
24 #include "attrib.h" | |
24 | 25 |
25 /********************************* Import ****************************/ | 26 /********************************* Import ****************************/ |
26 | 27 |
27 Import::Import(Loc loc, Array *packages, Identifier *id, Identifier *aliasId, | 28 Import::Import(Loc loc, Array *packages, Identifier *id, Identifier *aliasId, |
28 int isstatic) | 29 int isstatic) |
98 dst = Package::resolve(packages, NULL, &pkg); | 99 dst = Package::resolve(packages, NULL, &pkg); |
99 | 100 |
100 s = dst->lookup(id); | 101 s = dst->lookup(id); |
101 if (s) | 102 if (s) |
102 { | 103 { |
104 #if TARGET_NET | |
105 mod = (Module *)s; | |
106 #else | |
103 if (s->isModule()) | 107 if (s->isModule()) |
104 mod = (Module *)s; | 108 mod = (Module *)s; |
105 else | 109 else |
106 error("package and module have the same name"); | 110 error("package and module have the same name"); |
111 #endif | |
107 } | 112 } |
108 | 113 |
109 if (!mod) | 114 if (!mod) |
110 { | 115 { |
111 // Load module | 116 // Load module |
119 pkg = mod; | 124 pkg = mod; |
120 | 125 |
121 //printf("-Import::load('%s'), pkg = %p\n", toChars(), pkg); | 126 //printf("-Import::load('%s'), pkg = %p\n", toChars(), pkg); |
122 } | 127 } |
123 | 128 |
124 #if IN_LLVM | 129 void escapePath(OutBuffer *buf, const char *fname) |
125 char* escapePath(char* fname, char* buffer, int bufLen) { | 130 { |
126 char* res = buffer; | 131 while (1) |
127 bufLen -= 2; // for \0 and an occasional escape char | 132 { |
128 int dst = 0; | 133 switch (*fname) |
129 for (; dst < bufLen && *fname; ++dst, ++fname) { | 134 { |
130 switch (*fname) { | 135 case 0: |
136 return; | |
131 case '(': | 137 case '(': |
132 case ')': | 138 case ')': |
133 case '\\': | 139 case '\\': |
134 buffer[dst++] = '\\'; | 140 buf->writebyte('\\'); |
135 // fall through | |
136 | |
137 default: | 141 default: |
138 buffer[dst] = *fname; | 142 buf->writebyte(*fname); |
139 } | 143 break; |
140 } | 144 } |
141 buffer[dst] = '\0'; | 145 fname++; |
142 return buffer; | 146 } |
143 } | 147 } |
144 #endif | |
145 | 148 |
146 void Import::semantic(Scope *sc) | 149 void Import::semantic(Scope *sc) |
147 { | 150 { |
148 //printf("Import::semantic('%s')\n", toChars()); | 151 //printf("Import::semantic('%s')\n", toChars()); |
149 | 152 |
162 #endif | 165 #endif |
163 | 166 |
164 // Modules need a list of each imported module | 167 // Modules need a list of each imported module |
165 //printf("%s imports %s\n", sc->module->toChars(), mod->toChars()); | 168 //printf("%s imports %s\n", sc->module->toChars(), mod->toChars()); |
166 sc->module->aimports.push(mod); | 169 sc->module->aimports.push(mod); |
167 | |
168 mod->semantic(); | |
169 | 170 |
170 if (!isstatic && !aliasId && !names.dim) | 171 if (!isstatic && !aliasId && !names.dim) |
171 { | 172 { |
172 /* Default to private importing | 173 /* Default to private importing |
173 */ | 174 */ |
175 if (!sc->explicitProtection) | 176 if (!sc->explicitProtection) |
176 prot = PROTprivate; | 177 prot = PROTprivate; |
177 sc->scopesym->importScope(mod, prot); | 178 sc->scopesym->importScope(mod, prot); |
178 } | 179 } |
179 | 180 |
181 mod->semantic(); | |
182 | |
180 if (mod->needmoduleinfo) | 183 if (mod->needmoduleinfo) |
181 sc->module->needmoduleinfo = 1; | 184 sc->module->needmoduleinfo = 1; |
182 | 185 |
183 sc = sc->push(mod); | 186 sc = sc->push(mod); |
184 for (size_t i = 0; i < aliasdecls.dim; i++) | 187 for (size_t i = 0; i < aliasdecls.dim; i++) |
191 ad->importprot = protection; | 194 ad->importprot = protection; |
192 ad->semantic(sc); | 195 ad->semantic(sc); |
193 } | 196 } |
194 sc = sc->pop(); | 197 sc = sc->pop(); |
195 } | 198 } |
196 //printf("-Import::semantic('%s'), pkg = %p\n", toChars(), pkg); | 199 |
197 | 200 if (global.params.moduleDeps != NULL) |
198 | 201 { |
199 if (global.params.moduleDeps != NULL) { | 202 /* The grammar of the file is: |
200 char fnameBuf[262]; // MAX_PATH+2 | 203 * ImportDeclaration |
201 | 204 * ::= BasicImportDeclaration [ " : " ImportBindList ] [ " -> " |
202 OutBuffer *const ob = global.params.moduleDeps; | 205 * ModuleAliasIdentifier ] "\n" |
203 ob->printf("%s (%s) : ", | 206 * |
204 sc->module->toPrettyChars(), | 207 * BasicImportDeclaration |
205 escapePath(sc->module->srcfile->toChars(), fnameBuf, sizeof(fnameBuf) / sizeof(*fnameBuf)) | 208 * ::= ModuleFullyQualifiedName " (" FilePath ") : " Protection |
206 ); | 209 * " [ " static" ] : " ModuleFullyQualifiedName " (" FilePath ")" |
207 | 210 * |
208 char* protStr = ""; | 211 * FilePath |
209 switch (sc->protection) { | 212 * - any string with '(', ')' and '\' escaped with the '\' character |
210 case PROTpublic: protStr = "public"; break; | 213 */ |
211 case PROTprivate: protStr = "private"; break; | 214 |
212 case PROTpackage: protStr = "package"; break; | 215 OutBuffer *ob = global.params.moduleDeps; |
213 default: break; | 216 |
214 } | 217 ob->writestring(sc->module->toPrettyChars()); |
215 ob->writestring(protStr); | 218 ob->writestring(" ("); |
216 if (isstatic) { | 219 escapePath(ob, sc->module->srcfile->toChars()); |
217 ob->writestring(" static"); | 220 ob->writestring(") : "); |
218 } | 221 |
219 ob->writestring(" : "); | 222 ProtDeclaration::protectionToCBuffer(ob, sc->protection); |
220 | 223 if (isstatic) |
221 if (this->packages) { | 224 StorageClassDeclaration::stcToCBuffer(ob, STCstatic); |
222 for (size_t i = 0; i < this->packages->dim; i++) { | 225 ob->writestring(": "); |
223 Identifier *pid = (Identifier *)this->packages->data[i]; | 226 |
227 if (packages) | |
228 { | |
229 for (size_t i = 0; i < packages->dim; i++) | |
230 { | |
231 Identifier *pid = (Identifier *)packages->data[i]; | |
224 ob->printf("%s.", pid->toChars()); | 232 ob->printf("%s.", pid->toChars()); |
225 } | 233 } |
226 } | 234 } |
227 | 235 |
228 ob->printf("%s (%s)", | 236 ob->writestring(id->toChars()); |
229 this->id->toChars(), | 237 ob->writestring(" ("); |
230 mod ? escapePath(mod->srcfile->toChars(), fnameBuf, sizeof(fnameBuf) / sizeof(*fnameBuf)) : "???" | 238 if (mod) |
231 ); | 239 escapePath(ob, mod->srcfile->toChars()); |
232 | 240 else |
233 if (aliasId) { | 241 ob->writestring("???"); |
234 ob->printf(" -> %s", aliasId->toChars()); | 242 ob->writebyte(')'); |
235 } else { | 243 |
236 if (names.dim > 0) { | 244 for (size_t i = 0; i < names.dim; i++) |
237 ob->writestring(" : "); | 245 { |
238 for (size_t i = 0; i < names.dim; i++) | 246 if (i == 0) |
239 { | 247 ob->writebyte(':'); |
240 if (i > 0) { | 248 else |
241 ob->writebyte(','); | 249 ob->writebyte(','); |
242 } | 250 |
243 | 251 Identifier *name = (Identifier *)names.data[i]; |
244 Identifier *name = (Identifier *)names.data[i]; | 252 Identifier *alias = (Identifier *)aliases.data[i]; |
245 Identifier *alias = (Identifier *)aliases.data[i]; | 253 |
246 | 254 if (!alias) |
247 if (!alias) { | 255 { |
248 ob->printf("%s", name->toChars()); | 256 ob->printf("%s", name->toChars()); |
249 alias = name; | 257 alias = name; |
250 } else { | |
251 ob->printf("%s=%s", alias->toChars(), name->toChars()); | |
252 } | |
253 } | |
254 } | 258 } |
255 } | 259 else |
260 ob->printf("%s=%s", alias->toChars(), name->toChars()); | |
261 } | |
262 | |
263 if (aliasId) | |
264 ob->printf(" -> %s", aliasId->toChars()); | |
256 | 265 |
257 ob->writenl(); | 266 ob->writenl(); |
258 } | 267 } |
268 | |
269 //printf("-Import::semantic('%s'), pkg = %p\n", toChars(), pkg); | |
259 } | 270 } |
260 | 271 |
261 void Import::semantic2(Scope *sc) | 272 void Import::semantic2(Scope *sc) |
262 { | 273 { |
263 //printf("Import::semantic2('%s')\n", toChars()); | 274 //printf("Import::semantic2('%s')\n", toChars()); |
345 { Identifier *pid = (Identifier *)packages->data[i]; | 356 { Identifier *pid = (Identifier *)packages->data[i]; |
346 | 357 |
347 buf->printf("%s.", pid->toChars()); | 358 buf->printf("%s.", pid->toChars()); |
348 } | 359 } |
349 } | 360 } |
350 buf->printf("%s", id->toChars()); | 361 buf->printf("%s;", id->toChars()); |
351 if (names.dim > 0) { | |
352 buf->writebyte(':'); | |
353 for (size_t i = 0; i < names.dim; i++) | |
354 { | |
355 if (i > 0) { | |
356 buf->writebyte(','); | |
357 } | |
358 | |
359 Identifier *name = (Identifier *)names.data[i]; | |
360 Identifier *alias = (Identifier *)aliases.data[i]; | |
361 | |
362 if (!alias) { | |
363 buf->printf("%s", name->toChars()); | |
364 alias = name; | |
365 } else { | |
366 buf->printf("%s=%s", alias->toChars(), name->toChars()); | |
367 } | |
368 } | |
369 } | |
370 buf->writebyte(';'); | |
371 buf->writenl(); | 362 buf->writenl(); |
372 } | 363 } |
373 | 364 |