Strlit* path;
char* prefix;
Pkg* link;
- int exported;
+ char exported; // import line written in export data
+ char direct; // imported directly
};
typedef struct Iter Iter;
EXTERN Pkg* stringpkg; // fake package for C strings
EXTERN Pkg* typepkg; // fake package for runtime type info
EXTERN Pkg* unsafepkg; // package unsafe
+EXTERN Pkg* phash[128];
EXTERN int tptr; // either TPTR32 or TPTR64
extern char* runtimeimport;
extern char* unsafeimport;
EXTERN int typecheckok;
EXTERN int packagequotes;
+EXTERN int compiling_runtime;
+
/*
* y.tab.c
*/
LPACKAGE sym ';'
{
importpkg->name = $2->name;
+ importpkg->direct = 1;
// PGNS: fixme
if(strcmp($2->name, "main") == 0)
// PGNS: This should go away once we get
// rid of the global package name space.
- if(localpkg->name && strcmp($2->name, localpkg->name) == 0 && strcmp($2->name, "runtime") != 0)
+ if(localpkg->name && strcmp($2->name, localpkg->name) == 0 && !compiling_runtime)
yyerror("package cannot import itself");
}
if(pkg == nil)
return dgostringptr(s, ot, nil);
- // PGNS: This needs to be import path instead of pkg->name,
- // but we need to figure out how to fill it in during 6l when
- // trying to refer to localpkg.
+ // Emit reference to go.importpath.""., which 6l will
+ // rewrite using the correct import path. Every package
+ // that imports this one directly defines the symbol.
+ if(pkg == localpkg) {
+ static Sym *ns;
+
+ if(ns == nil)
+ ns = pkglookup("importpath.\"\".", mkpkg(strlit("go")));
+ return dsymptr(s, ot, ns, 0);
+ }
+
return dgostringptr(s, ot, pkg->name);
}
+static void
+dimportpath(Pkg *p)
+{
+ static Pkg *gopkg;
+ char *nam;
+ Node *n;
+
+ if(gopkg == nil) {
+ gopkg = mkpkg(strlit("go"));
+ gopkg->name = "go";
+ }
+ nam = smprint("importpath.%s.", p->prefix);
+
+ n = nod(ONAME, N, N);
+ n->sym = pkglookup(nam, gopkg);
+ free(nam);
+ n->class = PEXTERN;
+ n->xoffset = 0;
+
+ gdatastring(n, p->path);
+ ggloblsym(n->sym, types[TSTRING]->width, 1);
+}
/*
* uncommonType
else
tsym = t->sym;
- // PGNS: Fixme
- if(strcmp(localpkg->name, "runtime") == 0) {
+ if(compiling_runtime) {
if(t == types[t->etype])
goto ok;
if(t1 && t1 == types[t1->etype])
NodeList *l;
Node *n;
Type *t;
+ Pkg *p;
// copy types from externdcl list to signatlist
for(l=externdcl; l; l=l->next) {
dtypesym(ptrto(t));
}
+ // generate import strings for imported packages
+ for(i=0; i<nelem(phash); i++)
+ for(p=phash[i]; p; p=p->link)
+ if(p->direct)
+ dimportpath(p);
+
// do basic types if compiling package runtime.
// they have to be in at least one package,
- // and reflect is always loaded implicitly,
+ // and runtime is always loaded implicitly,
// so this is as good as any.
// another possible choice would be package main,
// but using runtime means fewer copies in .6 files.
- if(strcmp(localpkg->name, "runtime") == 0) { // PGNS: fixme
+ if(compiling_runtime) {
for(i=1; i<=TBOOL; i++)
dtypesym(ptrto(types[i]));
dtypesym(ptrto(types[TSTRING]));
dtypesym(typ(TDDD));
dtypesym(ptrto(pkglookup("Pointer", unsafepkg)->def->type));
+
+ // add paths for runtime and main, which 6l imports implicitly.
+ dimportpath(runtimepkg);
+ dimportpath(mkpkg(strlit("main")));
}
}