Warn about "assert X,Y"
[delight/core.git] / dmd2 / import.c
blobb1b326a150b09fca3ba8d1e226d2edffd95db463
2 // Compiler implementation of the D programming language
3 // Copyright (c) 1999-2006 by Digital Mars
4 // All Rights Reserved
5 // written by Walter Bright
6 // http://www.digitalmars.com
7 // License for redistribution is by either the Artistic License
8 // in artistic.txt, or the GNU General Public License in gnu.txt.
9 // See the included readme.txt for details.
11 #include <stdio.h>
12 #include <assert.h>
14 #include "root.h"
15 #include "dsymbol.h"
16 #include "import.h"
17 #include "identifier.h"
18 #include "module.h"
19 #include "scope.h"
20 #include "hdrgen.h"
21 #include "mtype.h"
22 #include "declaration.h"
23 #include "id.h"
25 /********************************* Import ****************************/
27 Import::Import(Loc loc, Array *packages, Identifier *id, Identifier *aliasId,
28 int isstatic)
29 : Dsymbol(id)
31 this->loc = loc;
32 this->packages = packages;
33 this->id = id;
34 this->aliasId = aliasId;
35 this->isstatic = isstatic;
36 pkg = NULL;
37 mod = NULL;
39 if (aliasId)
40 this->ident = aliasId;
41 // Kludge to change Import identifier to first package
42 else if (packages && packages->dim)
43 this->ident = (Identifier *)packages->data[0];
46 void Import::addAlias(Identifier *name, Identifier *alias)
48 if (isstatic)
49 error("cannot have an import bind list");
51 if (!aliasId)
52 this->ident = NULL; // make it an anonymous import
54 names.push(name);
55 aliases.push(alias);
58 char *Import::kind()
60 return isstatic ? (char *)"static import" : (char *)"import";
64 Dsymbol *Import::syntaxCopy(Dsymbol *s)
66 assert(!s);
68 Import *si;
70 si = new Import(loc, packages, id, aliasId, isstatic);
72 for (size_t i = 0; i < names.dim; i++)
74 si->addAlias((Identifier *)names.data[i], (Identifier *)aliases.data[i]);
77 return si;
80 void Import::load(Scope *sc)
82 DsymbolTable *dst;
83 Dsymbol *s;
85 //printf("Import::load('%s')\n", toChars());
87 // See if existing module
88 dst = Package::resolve(packages, NULL, &pkg);
90 s = dst->lookup(id);
91 if (s)
93 if (s->isModule())
94 mod = (Module *)s;
95 else
96 error("package and module have the same name");
99 if (!mod)
101 // Load module
102 mod = Module::load(loc, packages, id);
103 dst->insert(id, mod); // id may be different from mod->ident,
104 // if so then insert alias
105 if (!mod->importedFrom)
106 mod->importedFrom = sc ? sc->module->importedFrom : Module::rootModule;
108 if (!pkg)
109 pkg = mod;
110 mod->semantic();
112 //printf("-Import::load('%s'), pkg = %p\n", toChars(), pkg);
116 void Import::semantic(Scope *sc)
118 //printf("Import::semantic('%s')\n", toChars());
120 load(sc);
122 if (mod)
124 #if 0
125 if (mod->loc.linnum != 0)
126 { /* If the line number is not 0, then this is not
127 * a 'root' module, i.e. it was not specified on the command line.
129 mod->importedFrom = sc->module->importedFrom;
130 assert(mod->importedFrom);
132 #endif
134 if (!isstatic && !aliasId && !names.dim)
136 /* Default to private importing
138 enum PROT prot = sc->protection;
139 if (!sc->explicitProtection)
140 prot = PROTprivate;
141 sc->scopesym->importScope(mod, prot);
144 // Modules need a list of each imported module
145 sc->module->aimports.push(mod);
147 if (mod->needmoduleinfo)
148 sc->module->needmoduleinfo = 1;
150 sc = sc->push(mod);
151 for (size_t i = 0; i < aliasdecls.dim; i++)
152 { Dsymbol *s = (Dsymbol *)aliasdecls.data[i];
154 //printf("\tImport alias semantic('%s')\n", s->toChars());
155 if (!mod->search(loc, (Identifier *)names.data[i], 0))
156 error("%s not found", ((Identifier *)names.data[i])->toChars());
158 s->semantic(sc);
160 sc = sc->pop();
162 //printf("-Import::semantic('%s'), pkg = %p\n", toChars(), pkg);
165 void Import::semantic2(Scope *sc)
167 //printf("Import::semantic2('%s')\n", toChars());
168 mod->semantic2();
169 if (mod->needmoduleinfo)
170 sc->module->needmoduleinfo = 1;
173 Dsymbol *Import::toAlias()
175 if (aliasId)
176 return mod;
177 return this;
180 int Import::addMember(Scope *sc, ScopeDsymbol *sd, int memnum)
182 int result = 0;
184 if (names.dim == 0)
185 return Dsymbol::addMember(sc, sd, memnum);
187 if (aliasId)
188 result = Dsymbol::addMember(sc, sd, memnum);
190 for (size_t i = 0; i < names.dim; i++)
192 Identifier *name = (Identifier *)names.data[i];
193 Identifier *alias = (Identifier *)aliases.data[i];
195 if (!alias)
196 alias = name;
198 #if 1
199 TypeIdentifier *tname = new TypeIdentifier(loc, name);
200 #else
201 TypeIdentifier *tname = new TypeIdentifier(loc, NULL);
202 if (packages)
204 for (size_t j = 0; j < packages->dim; j++)
205 { Identifier *pid = (Identifier *)packages->data[j];
207 if (!tname->ident)
208 tname->ident = pid;
209 else
210 tname->addIdent(pid);
213 if (!tname->ident)
214 tname->ident = id;
215 else
216 tname->addIdent(id);
217 tname->addIdent(name);
218 #endif
219 AliasDeclaration *ad = new AliasDeclaration(loc, alias, tname);
220 result |= ad->addMember(sc, sd, memnum);
222 aliasdecls.push(ad);
225 return result;
228 Dsymbol *Import::search(Loc loc, Identifier *ident, int flags)
230 //printf("%s.Import::search(ident = '%s', flags = x%x)\n", toChars(), ident->toChars(), flags);
232 if (!pkg)
233 load(NULL);
235 // Forward it to the package/module
236 return pkg->search(loc, ident, flags);
239 int Import::overloadInsert(Dsymbol *s)
241 // Allow multiple imports of the same name
242 return s->isImport() != NULL;
245 void Import::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
247 if (hgs->hdrgen && id == Id::object)
248 return; // object is imported by default
250 if (isstatic)
251 buf->writestring("static ");
252 buf->writestring("import ");
253 if (aliasId)
255 buf->printf("%s = ", aliasId->toChars());
257 if (packages && packages->dim)
259 for (size_t i = 0; i < packages->dim; i++)
260 { Identifier *pid = (Identifier *)packages->data[i];
262 buf->printf("%s.", pid->toChars());
265 buf->printf("%s;", id->toChars());
266 buf->writenl();