Mercurial > projects > ldc
comparison dmd2/import.c @ 1452:638d16625da2
LDC 2 compiles again.
author | Robert Clipsham <robert@octarineparrot.com> |
---|---|
date | Sat, 30 May 2009 17:23:32 +0100 |
parents | 356e65836fb5 |
children | 54b3c1394d62 |
comparison
equal
deleted
inserted
replaced
1423:42bd767ec5a4 | 1452:638d16625da2 |
---|---|
1 | 1 |
2 // Compiler implementation of the D programming language | 2 // Compiler implementation of the D programming language |
3 // Copyright (c) 1999-2006 by Digital Mars | 3 // Copyright (c) 1999-2009 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. |
26 | 26 |
27 Import::Import(Loc loc, Array *packages, Identifier *id, Identifier *aliasId, | 27 Import::Import(Loc loc, Array *packages, Identifier *id, Identifier *aliasId, |
28 int isstatic) | 28 int isstatic) |
29 : Dsymbol(id) | 29 : Dsymbol(id) |
30 { | 30 { |
31 assert(id); | |
31 this->loc = loc; | 32 this->loc = loc; |
32 this->packages = packages; | 33 this->packages = packages; |
33 this->id = id; | 34 this->id = id; |
34 this->aliasId = aliasId; | 35 this->aliasId = aliasId; |
35 this->isstatic = isstatic; | 36 this->isstatic = isstatic; |
37 #if IN_LLVM | |
36 protection = PROTundefined; | 38 protection = PROTundefined; |
39 #endif | |
37 pkg = NULL; | 40 pkg = NULL; |
38 mod = NULL; | 41 mod = NULL; |
39 | 42 |
40 if (aliasId) | 43 if (aliasId) |
41 this->ident = aliasId; | 44 this->ident = aliasId; |
59 const char *Import::kind() | 62 const char *Import::kind() |
60 { | 63 { |
61 return isstatic ? (char *)"static import" : (char *)"import"; | 64 return isstatic ? (char *)"static import" : (char *)"import"; |
62 } | 65 } |
63 | 66 |
67 #if IN_LLVM | |
64 enum PROT Import::prot() | 68 enum PROT Import::prot() |
65 { | 69 { |
66 return protection; | 70 return protection; |
67 } | 71 } |
72 #endif | |
68 | 73 |
69 Dsymbol *Import::syntaxCopy(Dsymbol *s) | 74 Dsymbol *Import::syntaxCopy(Dsymbol *s) |
70 { | 75 { |
71 assert(!s); | 76 assert(!s); |
72 | 77 |
110 if (!mod->importedFrom) | 115 if (!mod->importedFrom) |
111 mod->importedFrom = sc ? sc->module->importedFrom : Module::rootModule; | 116 mod->importedFrom = sc ? sc->module->importedFrom : Module::rootModule; |
112 } | 117 } |
113 if (!pkg) | 118 if (!pkg) |
114 pkg = mod; | 119 pkg = mod; |
115 mod->semantic(); | |
116 | 120 |
117 //printf("-Import::load('%s'), pkg = %p\n", toChars(), pkg); | 121 //printf("-Import::load('%s'), pkg = %p\n", toChars(), pkg); |
118 } | 122 } |
119 | 123 |
124 #if IN_LLVM | |
125 char* escapePath(char* fname, char* buffer, int bufLen) { | |
126 char* res = buffer; | |
127 bufLen -= 2; // for \0 and an occasional escape char | |
128 int dst = 0; | |
129 for (; dst < bufLen && *fname; ++dst, ++fname) { | |
130 switch (*fname) { | |
131 case '(': | |
132 case ')': | |
133 case '\\': | |
134 buffer[dst++] = '\\'; | |
135 // fall through | |
136 | |
137 default: | |
138 buffer[dst] = *fname; | |
139 } | |
140 } | |
141 buffer[dst] = '\0'; | |
142 return buffer; | |
143 } | |
144 #endif | |
120 | 145 |
121 void Import::semantic(Scope *sc) | 146 void Import::semantic(Scope *sc) |
122 { | 147 { |
123 //printf("Import::semantic('%s')\n", toChars()); | 148 //printf("Import::semantic('%s')\n", toChars()); |
124 | 149 |
134 mod->importedFrom = sc->module->importedFrom; | 159 mod->importedFrom = sc->module->importedFrom; |
135 assert(mod->importedFrom); | 160 assert(mod->importedFrom); |
136 } | 161 } |
137 #endif | 162 #endif |
138 | 163 |
139 /* Default to private importing | 164 // Modules need a list of each imported module |
140 */ | 165 //printf("%s imports %s\n", sc->module->toChars(), mod->toChars()); |
141 protection = sc->protection; | 166 sc->module->aimports.push(mod); |
142 if (!sc->explicitProtection) | 167 |
143 protection = PROTprivate; | 168 mod->semantic(); |
144 | 169 |
145 if (!isstatic && !aliasId && !names.dim) | 170 if (!isstatic && !aliasId && !names.dim) |
146 { | 171 { |
147 sc->scopesym->importScope(mod, protection); | 172 /* Default to private importing |
148 } | 173 */ |
149 | 174 enum PROT prot = sc->protection; |
150 // Modules need a list of each imported module | 175 if (!sc->explicitProtection) |
151 sc->module->aimports.push(mod); | 176 prot = PROTprivate; |
177 sc->scopesym->importScope(mod, prot); | |
178 } | |
152 | 179 |
153 if (mod->needmoduleinfo) | 180 if (mod->needmoduleinfo) |
154 sc->module->needmoduleinfo = 1; | 181 sc->module->needmoduleinfo = 1; |
155 | 182 |
156 sc = sc->push(mod); | 183 sc = sc->push(mod); |
159 | 186 |
160 //printf("\tImport alias semantic('%s')\n", s->toChars()); | 187 //printf("\tImport alias semantic('%s')\n", s->toChars()); |
161 if (!mod->search(loc, (Identifier *)names.data[i], 0)) | 188 if (!mod->search(loc, (Identifier *)names.data[i], 0)) |
162 error("%s not found", ((Identifier *)names.data[i])->toChars()); | 189 error("%s not found", ((Identifier *)names.data[i])->toChars()); |
163 | 190 |
191 ad->importprot = protection; | |
164 ad->semantic(sc); | 192 ad->semantic(sc); |
165 ad->protection = protection; | |
166 } | 193 } |
167 sc = sc->pop(); | 194 sc = sc->pop(); |
168 } | 195 } |
169 //printf("-Import::semantic('%s'), pkg = %p\n", toChars(), pkg); | 196 //printf("-Import::semantic('%s'), pkg = %p\n", toChars(), pkg); |
197 | |
198 | |
199 if (global.params.moduleDeps != NULL) { | |
200 char fnameBuf[262]; // MAX_PATH+2 | |
201 | |
202 OutBuffer *const ob = global.params.moduleDeps; | |
203 ob->printf("%s (%s) : ", | |
204 sc->module->toPrettyChars(), | |
205 escapePath(sc->module->srcfile->toChars(), fnameBuf, sizeof(fnameBuf) / sizeof(*fnameBuf)) | |
206 ); | |
207 | |
208 char* protStr = ""; | |
209 switch (sc->protection) { | |
210 case PROTpublic: protStr = "public"; break; | |
211 case PROTprivate: protStr = "private"; break; | |
212 case PROTpackage: protStr = "package"; break; | |
213 default: break; | |
214 } | |
215 ob->writestring(protStr); | |
216 if (isstatic) { | |
217 ob->writestring(" static"); | |
218 } | |
219 ob->writestring(" : "); | |
220 | |
221 if (this->packages) { | |
222 for (size_t i = 0; i < this->packages->dim; i++) { | |
223 Identifier *pid = (Identifier *)this->packages->data[i]; | |
224 ob->printf("%s.", pid->toChars()); | |
225 } | |
226 } | |
227 | |
228 ob->printf("%s (%s)", | |
229 this->id->toChars(), | |
230 mod ? escapePath(mod->srcfile->toChars(), fnameBuf, sizeof(fnameBuf) / sizeof(*fnameBuf)) : "???" | |
231 ); | |
232 | |
233 if (aliasId) { | |
234 ob->printf(" -> %s", aliasId->toChars()); | |
235 } else { | |
236 if (names.dim > 0) { | |
237 ob->writestring(" : "); | |
238 for (size_t i = 0; i < names.dim; i++) | |
239 { | |
240 if (i > 0) { | |
241 ob->writebyte(','); | |
242 } | |
243 | |
244 Identifier *name = (Identifier *)names.data[i]; | |
245 Identifier *alias = (Identifier *)aliases.data[i]; | |
246 | |
247 if (!alias) { | |
248 ob->printf("%s", name->toChars()); | |
249 alias = name; | |
250 } else { | |
251 ob->printf("%s=%s", alias->toChars(), name->toChars()); | |
252 } | |
253 } | |
254 } | |
255 } | |
256 | |
257 ob->writenl(); | |
258 } | |
170 } | 259 } |
171 | 260 |
172 void Import::semantic2(Scope *sc) | 261 void Import::semantic2(Scope *sc) |
173 { | 262 { |
174 //printf("Import::semantic2('%s')\n", toChars()); | 263 //printf("Import::semantic2('%s')\n", toChars()); |
222 Dsymbol *Import::search(Loc loc, Identifier *ident, int flags) | 311 Dsymbol *Import::search(Loc loc, Identifier *ident, int flags) |
223 { | 312 { |
224 //printf("%s.Import::search(ident = '%s', flags = x%x)\n", toChars(), ident->toChars(), flags); | 313 //printf("%s.Import::search(ident = '%s', flags = x%x)\n", toChars(), ident->toChars(), flags); |
225 | 314 |
226 if (!pkg) | 315 if (!pkg) |
227 load(NULL); | 316 { load(NULL); |
317 mod->semantic(); | |
318 } | |
228 | 319 |
229 // Forward it to the package/module | 320 // Forward it to the package/module |
230 return pkg->search(loc, ident, flags); | 321 return pkg->search(loc, ident, flags); |
231 } | 322 } |
232 | 323 |
254 { Identifier *pid = (Identifier *)packages->data[i]; | 345 { Identifier *pid = (Identifier *)packages->data[i]; |
255 | 346 |
256 buf->printf("%s.", pid->toChars()); | 347 buf->printf("%s.", pid->toChars()); |
257 } | 348 } |
258 } | 349 } |
259 buf->printf("%s;", id->toChars()); | 350 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(';'); | |
260 buf->writenl(); | 371 buf->writenl(); |
261 } | 372 } |
262 | 373 |