comparison dmd/import.c @ 1587:def7a1d494fd

Merge DMD 1.051
author Christian Kamm <kamm incasoftware de>
date Fri, 06 Nov 2009 23:58:01 +0100
parents 1311dabc6a1f
children
comparison
equal deleted inserted replaced
1586:7f728c52e63c 1587:def7a1d494fd
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.
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)
29 : Dsymbol(id) 30 : Dsymbol(id)
30 { 31 {
32 assert(id);
31 this->loc = loc; 33 this->loc = loc;
32 this->packages = packages; 34 this->packages = packages;
33 this->id = id; 35 this->id = id;
34 this->aliasId = aliasId; 36 this->aliasId = aliasId;
35 this->isstatic = isstatic; 37 this->isstatic = isstatic;
82 return si; 84 return si;
83 } 85 }
84 86
85 void Import::load(Scope *sc) 87 void Import::load(Scope *sc)
86 { 88 {
87 DsymbolTable *dst;
88 Dsymbol *s;
89
90 //printf("Import::load('%s')\n", toChars()); 89 //printf("Import::load('%s')\n", toChars());
91 90
92 // See if existing module 91 // See if existing module
93 dst = Package::resolve(packages, NULL, &pkg); 92 DsymbolTable *dst = Package::resolve(packages, NULL, &pkg);
94 93
95 s = dst->lookup(id); 94 Dsymbol *s = dst->lookup(id);
96 if (s) 95 if (s)
97 { 96 {
97 #if TARGET_NET
98 mod = (Module *)s;
99 #else
98 if (s->isModule()) 100 if (s->isModule())
99 mod = (Module *)s; 101 mod = (Module *)s;
100 else 102 else
101 error("package and module have the same name"); 103 error("package and module have the same name");
104 #endif
102 } 105 }
103 106
104 if (!mod) 107 if (!mod)
105 { 108 {
106 // Load module 109 // Load module
114 pkg = mod; 117 pkg = mod;
115 118
116 //printf("-Import::load('%s'), pkg = %p\n", toChars(), pkg); 119 //printf("-Import::load('%s'), pkg = %p\n", toChars(), pkg);
117 } 120 }
118 121
119 char* escapePath(char* fname, char* buffer, int bufLen) { 122 void escapePath(OutBuffer *buf, const char *fname)
120 char* res = buffer; 123 {
121 bufLen -= 2; // for \0 and an occasional escape char 124 while (1)
122 int dst = 0; 125 {
123 for (; dst < bufLen && *fname; ++dst, ++fname) { 126 switch (*fname)
124 switch (*fname) { 127 {
128 case 0:
129 return;
125 case '(': 130 case '(':
126 case ')': 131 case ')':
127 case '\\': 132 case '\\':
128 buffer[dst++] = '\\'; 133 buf->writebyte('\\');
129 // fall through
130
131 default: 134 default:
132 buffer[dst] = *fname; 135 buf->writebyte(*fname);
133 } 136 break;
134 } 137 }
135 buffer[dst] = '\0'; 138 fname++;
136 return buffer; 139 }
140 }
141
142 void Import::importAll(Scope *sc)
143 {
144 if (!mod)
145 {
146 load(sc);
147 mod->importAll(0);
148
149 if (!isstatic && !aliasId && !names.dim)
150 {
151 /* Default to private importing
152 */
153 enum PROT prot = sc->protection;
154 if (!sc->explicitProtection)
155 prot = PROTprivate;
156 sc->scopesym->importScope(mod, prot);
157 }
158 }
137 } 159 }
138 160
139 void Import::semantic(Scope *sc) 161 void Import::semantic(Scope *sc)
140 { 162 {
141 //printf("Import::semantic('%s')\n", toChars()); 163 //printf("Import::semantic('%s')\n", toChars());
142 164
143 load(sc); 165 // Load if not already done so
166 if (!mod)
167 { load(sc);
168 mod->importAll(0);
169 }
144 170
145 if (mod) 171 if (mod)
146 { 172 {
147 #if 0 173 #if 0
148 if (mod->loc.linnum != 0) 174 if (mod->loc.linnum != 0)
156 182
157 // Modules need a list of each imported module 183 // Modules need a list of each imported module
158 //printf("%s imports %s\n", sc->module->toChars(), mod->toChars()); 184 //printf("%s imports %s\n", sc->module->toChars(), mod->toChars());
159 sc->module->aimports.push(mod); 185 sc->module->aimports.push(mod);
160 186
161 mod->semantic();
162
163 /* Default to private importing 187 /* Default to private importing
164 */ 188 */
165 protection = sc->protection; 189 protection = sc->protection;
166 if (!sc->explicitProtection) 190 if (!sc->explicitProtection)
167 protection = PROTprivate; 191 protection = PROTprivate;
169 if (!isstatic && !aliasId && !names.dim) 193 if (!isstatic && !aliasId && !names.dim)
170 { 194 {
171 sc->scopesym->importScope(mod, protection); 195 sc->scopesym->importScope(mod, protection);
172 } 196 }
173 197
198 mod->semantic();
199
174 if (mod->needmoduleinfo) 200 if (mod->needmoduleinfo)
175 sc->module->needmoduleinfo = 1; 201 sc->module->needmoduleinfo = 1;
176 202
177 sc = sc->push(mod); 203 sc = sc->push(mod);
178 for (size_t i = 0; i < aliasdecls.dim; i++) 204 for (size_t i = 0; i < aliasdecls.dim; i++)
187 } 213 }
188 sc = sc->pop(); 214 sc = sc->pop();
189 } 215 }
190 //printf("-Import::semantic('%s'), pkg = %p\n", toChars(), pkg); 216 //printf("-Import::semantic('%s'), pkg = %p\n", toChars(), pkg);
191 217
192 218 if (global.params.moduleDeps != NULL)
193 if (global.params.moduleDeps != NULL) { 219 {
194 char fnameBuf[262]; // MAX_PATH+2 220 /* The grammar of the file is:
195 221 * ImportDeclaration
196 OutBuffer *const ob = global.params.moduleDeps; 222 * ::= BasicImportDeclaration [ " : " ImportBindList ] [ " -> "
197 ob->printf("%s (%s) : ", 223 * ModuleAliasIdentifier ] "\n"
198 sc->module->toPrettyChars(), 224 *
199 escapePath(sc->module->srcfile->toChars(), fnameBuf, sizeof(fnameBuf) / sizeof(*fnameBuf)) 225 * BasicImportDeclaration
200 ); 226 * ::= ModuleFullyQualifiedName " (" FilePath ") : " Protection
201 227 * " [ " static" ] : " ModuleFullyQualifiedName " (" FilePath ")"
202 char* protStr = ""; 228 *
203 switch (sc->protection) { 229 * FilePath
204 case PROTpublic: protStr = "public"; break; 230 * - any string with '(', ')' and '\' escaped with the '\' character
205 case PROTprivate: protStr = "private"; break; 231 */
206 case PROTpackage: protStr = "package"; break; 232
207 default: break; 233 OutBuffer *ob = global.params.moduleDeps;
208 } 234
209 ob->writestring(protStr); 235 ob->writestring(sc->module->toPrettyChars());
210 if (isstatic) { 236 ob->writestring(" (");
211 ob->writestring(" static"); 237 escapePath(ob, sc->module->srcfile->toChars());
212 } 238 ob->writestring(") : ");
213 ob->writestring(" : "); 239
214 240 ProtDeclaration::protectionToCBuffer(ob, sc->protection);
215 if (this->packages) { 241 if (isstatic)
216 for (size_t i = 0; i < this->packages->dim; i++) { 242 StorageClassDeclaration::stcToCBuffer(ob, STCstatic);
217 Identifier *pid = (Identifier *)this->packages->data[i]; 243 ob->writestring(": ");
244
245 if (packages)
246 {
247 for (size_t i = 0; i < packages->dim; i++)
248 {
249 Identifier *pid = (Identifier *)packages->data[i];
218 ob->printf("%s.", pid->toChars()); 250 ob->printf("%s.", pid->toChars());
219 } 251 }
220 } 252 }
221 253
222 ob->printf("%s (%s)", 254 ob->writestring(id->toChars());
223 this->id->toChars(), 255 ob->writestring(" (");
224 mod ? escapePath(mod->srcfile->toChars(), fnameBuf, sizeof(fnameBuf) / sizeof(*fnameBuf)) : "???" 256 if (mod)
225 ); 257 escapePath(ob, mod->srcfile->toChars());
226 258 else
227 if (aliasId) { 259 ob->writestring("???");
228 ob->printf(" -> %s", aliasId->toChars()); 260 ob->writebyte(')');
229 } else { 261
230 if (names.dim > 0) { 262 for (size_t i = 0; i < names.dim; i++)
231 ob->writestring(" : "); 263 {
232 for (size_t i = 0; i < names.dim; i++) 264 if (i == 0)
233 { 265 ob->writebyte(':');
234 if (i > 0) { 266 else
235 ob->writebyte(','); 267 ob->writebyte(',');
236 } 268
237 269 Identifier *name = (Identifier *)names.data[i];
238 Identifier *name = (Identifier *)names.data[i]; 270 Identifier *alias = (Identifier *)aliases.data[i];
239 Identifier *alias = (Identifier *)aliases.data[i]; 271
240 272 if (!alias)
241 if (!alias) { 273 {
242 ob->printf("%s", name->toChars()); 274 ob->printf("%s", name->toChars());
243 alias = name; 275 alias = name;
244 } else {
245 ob->printf("%s=%s", alias->toChars(), name->toChars());
246 }
247 }
248 } 276 }
249 } 277 else
278 ob->printf("%s=%s", alias->toChars(), name->toChars());
279 }
280
281 if (aliasId)
282 ob->printf(" -> %s", aliasId->toChars());
250 283
251 ob->writenl(); 284 ob->writenl();
252 } 285 }
286
287 //printf("-Import::semantic('%s'), pkg = %p\n", toChars(), pkg);
253 } 288 }
254 289
255 void Import::semantic2(Scope *sc) 290 void Import::semantic2(Scope *sc)
256 { 291 {
257 //printf("Import::semantic2('%s')\n", toChars()); 292 //printf("Import::semantic2('%s')\n", toChars());