2 // Compiler implementation of the D programming language
3 // Copyright (c) 1999-2006 by Digital Mars
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.
17 #include "identifier.h"
22 #include "declaration.h"
25 /********************************* Import ****************************/
27 Import::Import(Loc loc
, Array
*packages
, Identifier
*id
, Identifier
*aliasId
,
32 this->packages
= packages
;
34 this->aliasId
= aliasId
;
35 this->isstatic
= isstatic
;
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
)
49 error("cannot have an import bind list");
52 this->ident
= NULL
; // make it an anonymous import
60 return isstatic
? (char *)"static import" : (char *)"import";
64 Dsymbol
*Import::syntaxCopy(Dsymbol
*s
)
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
]);
80 void Import::load(Scope
*sc
)
85 //printf("Import::load('%s')\n", toChars());
87 // See if existing module
88 dst
= Package::resolve(packages
, NULL
, &pkg
);
96 error("package and module have the same name");
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
;
112 //printf("-Import::load('%s'), pkg = %p\n", toChars(), pkg);
116 void Import::semantic(Scope
*sc
)
118 //printf("Import::semantic('%s')\n", toChars());
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
);
134 if (!isstatic
&& !aliasId
&& !names
.dim
)
136 /* Default to private importing
138 enum PROT prot
= sc
->protection
;
139 if (!sc
->explicitProtection
)
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;
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());
162 //printf("-Import::semantic('%s'), pkg = %p\n", toChars(), pkg);
165 void Import::semantic2(Scope
*sc
)
167 //printf("Import::semantic2('%s')\n", toChars());
169 if (mod
->needmoduleinfo
)
170 sc
->module
->needmoduleinfo
= 1;
173 Dsymbol
*Import::toAlias()
180 int Import::addMember(Scope
*sc
, ScopeDsymbol
*sd
, int memnum
)
185 return Dsymbol::addMember(sc
, sd
, memnum
);
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
];
199 TypeIdentifier
*tname
= new TypeIdentifier(loc
, name
);
201 TypeIdentifier
*tname
= new TypeIdentifier(loc
, NULL
);
204 for (size_t j
= 0; j
< packages
->dim
; j
++)
205 { Identifier
*pid
= (Identifier
*)packages
->data
[j
];
210 tname
->addIdent(pid
);
217 tname
->addIdent(name
);
219 AliasDeclaration
*ad
= new AliasDeclaration(loc
, alias
, tname
);
220 result
|= ad
->addMember(sc
, sd
, memnum
);
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);
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
251 buf
->writestring("static ");
252 buf
->writestring("import ");
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());