diff 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
line wrap: on
line diff
--- a/dmd/import.c	Fri Nov 06 21:51:41 2009 +0100
+++ b/dmd/import.c	Fri Nov 06 23:58:01 2009 +0100
@@ -1,6 +1,6 @@
 
 // Compiler implementation of the D programming language
-// Copyright (c) 1999-2006 by Digital Mars
+// Copyright (c) 1999-2009 by Digital Mars
 // All Rights Reserved
 // written by Walter Bright
 // http://www.digitalmars.com
@@ -21,6 +21,7 @@
 #include "mtype.h"
 #include "declaration.h"
 #include "id.h"
+#include "attrib.h"
 
 /********************************* Import ****************************/
 
@@ -28,6 +29,7 @@
 	int isstatic)
     : Dsymbol(id)
 {
+    assert(id);
     this->loc = loc;
     this->packages = packages;
     this->id = id;
@@ -84,21 +86,22 @@
 
 void Import::load(Scope *sc)
 {
-    DsymbolTable *dst;
-    Dsymbol *s;
-
     //printf("Import::load('%s')\n", toChars());
 
     // See if existing module
-    dst = Package::resolve(packages, NULL, &pkg);
+    DsymbolTable *dst = Package::resolve(packages, NULL, &pkg);
 
-    s = dst->lookup(id);
+    Dsymbol *s = dst->lookup(id);
     if (s)
     {
+#if TARGET_NET
+	mod = (Module *)s;
+#else
 	if (s->isModule())
 	    mod = (Module *)s;
 	else
 	    error("package and module have the same name");
+#endif
     }
 
     if (!mod)
@@ -116,31 +119,54 @@
     //printf("-Import::load('%s'), pkg = %p\n", toChars(), pkg);
 }
 
-char* escapePath(char* fname, char* buffer, int bufLen) {
-    char* res = buffer;
-    bufLen -= 2;    // for \0 and an occasional escape char
-    int dst = 0;
-    for (; dst < bufLen && *fname; ++dst, ++fname) {
-	switch (*fname) {
+void escapePath(OutBuffer *buf, const char *fname)
+{
+    while (1)
+    {
+	switch (*fname)
+	{
+	    case 0:
+		return;
 	    case '(':
 	    case ')':
 	    case '\\':
-		    buffer[dst++] = '\\';
-		    // fall through
-
+		buf->writebyte('\\');
 	    default:
-		    buffer[dst] = *fname;
+		buf->writebyte(*fname);
+		break;
 	}
+	fname++;
     }
-    buffer[dst] = '\0';
-    return buffer;
+}
+
+void Import::importAll(Scope *sc)
+{
+    if (!mod)
+    {
+       load(sc);
+       mod->importAll(0);
+
+       if (!isstatic && !aliasId && !names.dim)
+       {
+           /* Default to private importing
+            */
+           enum PROT prot = sc->protection;
+           if (!sc->explicitProtection)
+               prot = PROTprivate;
+           sc->scopesym->importScope(mod, prot);
+       }
+    }
 }
 
 void Import::semantic(Scope *sc)
 {
     //printf("Import::semantic('%s')\n", toChars());
 
-    load(sc);
+    // Load if not already done so
+    if (!mod)
+    {	load(sc);
+	mod->importAll(0);
+    }
 
     if (mod)
     {
@@ -158,8 +184,6 @@
 	//printf("%s imports %s\n", sc->module->toChars(), mod->toChars());
 	sc->module->aimports.push(mod);
 
-	mod->semantic();
-
 	/* Default to private importing
 	 */
 	protection = sc->protection;
@@ -171,6 +195,8 @@
 	    sc->scopesym->importScope(mod, protection);
 	}
 
+	mod->semantic();
+
 	if (mod->needmoduleinfo)
 	    sc->module->needmoduleinfo = 1;
 
@@ -189,67 +215,76 @@
     }
     //printf("-Import::semantic('%s'), pkg = %p\n", toChars(), pkg);
 
-
-    if (global.params.moduleDeps != NULL) {
-	char fnameBuf[262];		// MAX_PATH+2
-
-	OutBuffer *const ob = global.params.moduleDeps;
-	ob->printf("%s (%s) : ",
-	    sc->module->toPrettyChars(),
-	    escapePath(sc->module->srcfile->toChars(), fnameBuf, sizeof(fnameBuf) / sizeof(*fnameBuf))
-	);
+    if (global.params.moduleDeps != NULL)
+    {
+	/* The grammar of the file is:
+	 *	ImportDeclaration
+	 *	    ::= BasicImportDeclaration [ " : " ImportBindList ] [ " -> "
+	 *	ModuleAliasIdentifier ] "\n"
+	 *
+	 *	BasicImportDeclaration
+	 *	    ::= ModuleFullyQualifiedName " (" FilePath ") : " Protection
+	 *		" [ " static" ] : " ModuleFullyQualifiedName " (" FilePath ")"
+	 *
+	 *	FilePath
+	 *	    - any string with '(', ')' and '\' escaped with the '\' character
+	 */
 
-	char* protStr = "";
-	switch (sc->protection) {
-	    case PROTpublic: protStr = "public"; break;
-	    case PROTprivate: protStr = "private"; break;
-	    case PROTpackage: protStr = "package"; break;
-	    default: break;
-	}
-	ob->writestring(protStr);
-	if (isstatic) {
-	    ob->writestring(" static");
-	}
-	ob->writestring(" : ");
+	OutBuffer *ob = global.params.moduleDeps;
+
+	ob->writestring(sc->module->toPrettyChars());
+	ob->writestring(" (");
+	escapePath(ob, sc->module->srcfile->toChars());
+	ob->writestring(") : ");
 
-	if (this->packages) {
-	    for (size_t i = 0; i < this->packages->dim; i++) {
-		Identifier *pid = (Identifier *)this->packages->data[i];
+	ProtDeclaration::protectionToCBuffer(ob, sc->protection);
+	if (isstatic)
+	    StorageClassDeclaration::stcToCBuffer(ob, STCstatic);
+	ob->writestring(": ");
+
+	if (packages)
+	{
+	    for (size_t i = 0; i < packages->dim; i++)
+	    {
+		Identifier *pid = (Identifier *)packages->data[i];
 		ob->printf("%s.", pid->toChars());
 	    }
 	}
 
-	ob->printf("%s (%s)",
-	    this->id->toChars(),
-	    mod ? escapePath(mod->srcfile->toChars(), fnameBuf, sizeof(fnameBuf) / sizeof(*fnameBuf)) : "???"
-	);
+	ob->writestring(id->toChars());
+	ob->writestring(" (");
+	if (mod)
+	    escapePath(ob, mod->srcfile->toChars());
+	else
+	    ob->writestring("???");
+	ob->writebyte(')');
+
+	for (size_t i = 0; i < names.dim; i++)
+	{
+	    if (i == 0)
+		ob->writebyte(':');
+	    else
+		ob->writebyte(',');
 
-	if (aliasId) {
-	    ob->printf(" -> %s", aliasId->toChars());
-	} else {
-	    if (names.dim > 0) {
-		ob->writestring(" : ");
-		for (size_t i = 0; i < names.dim; i++)
-		{
-		    if (i > 0) {
-			ob->writebyte(',');
-		    }
+	    Identifier *name = (Identifier *)names.data[i];
+	    Identifier *alias = (Identifier *)aliases.data[i];
 
-		    Identifier *name = (Identifier *)names.data[i];
-		    Identifier *alias = (Identifier *)aliases.data[i];
+	    if (!alias)
+	    {
+		ob->printf("%s", name->toChars());
+		alias = name;
+	    }
+	    else
+		ob->printf("%s=%s", alias->toChars(), name->toChars());
+	}
 
-		    if (!alias) {
-			ob->printf("%s", name->toChars());
-			alias = name;
-		    } else {
-			ob->printf("%s=%s", alias->toChars(), name->toChars());
-		    }
-		}
-	    }
-	}
+	if (aliasId)
+		ob->printf(" -> %s", aliasId->toChars());
 
 	ob->writenl();
     }
+
+    //printf("-Import::semantic('%s'), pkg = %p\n", toChars(), pkg);
 }
 
 void Import::semantic2(Scope *sc)