changeset 817:e6fb7ed87d3a

Added error message MSG.ConflictingModuleAndPackage.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Wed, 12 Mar 2008 23:33:14 +0100
parents 35d238d502cb
children 372fa4fbbb1d
files src/dil/Messages.d src/dil/ModuleManager.d src/dil/semantic/Module.d
diffstat 3 files changed, 30 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/dil/Messages.d	Wed Mar 12 22:41:45 2008 +0100
+++ b/src/dil/Messages.d	Wed Mar 12 23:33:14 2008 +0100
@@ -156,7 +156,8 @@
   auto MissingCatchOrFinally = "try statement is missing a catch or finally body.";
   // Semantic analysis:
   auto CouldntLoadModule = "couldn't load module '{}'";
-  auto ConflictingModuleFiles = "module is in conflict with '{}'";
+  auto ConflictingModuleFiles = "module is in conflict with module '{}'";
+  auto ConflictingModuleAndPackage = "module is in conflict with package '{}'";
   auto ModuleNotInPackage = "expected module to be in package '{}'";
   auto UndefinedIdentifier = "undefined identifier '{}'";
   auto DeclConflictsWithDecl = "declaration '{}' conflicts with declaration @{}";
--- a/src/dil/ModuleManager.d	Wed Mar 12 22:41:45 2008 +0100
+++ b/src/dil/ModuleManager.d	Wed Mar 12 23:33:14 2008 +0100
@@ -55,11 +55,12 @@
     auto moduleFQNPath = newModule.getFQNPath();
     if (auto existingModule = moduleFQNPath in moduleFQNPathTable)
     { // Error: two module files have the same f.q. module name.
-      auto location = newModule.moduleDecl.begin.getErrorLocation();
+      auto location = newModule.getModuleDeclToken().getErrorLocation();
       auto msg = Format(MSG.ConflictingModuleFiles, newModule.filePath());
       infoMan ~= new SemanticError(location, msg);
       return *existingModule;
     }
+
     // Insert new module.
     moduleFQNPathTable[moduleFQNPath] = newModule;
     absFilePathTable[absFilePath] = newModule;
@@ -67,6 +68,14 @@
     // Add the module to its package.
     auto pckg = getPackage(newModule.packageName);
     pckg.add(newModule);
+
+    if (auto p = newModule.getFQN() in packageTable)
+    { // Error: module and package share the same name.
+      auto location = newModule.getModuleDeclToken().getErrorLocation();
+      auto msg = Format(MSG.ConflictingModuleAndPackage, newModule.getFQN());
+      infoMan ~= new SemanticError(location, msg);
+    }
+
     return newModule;
   }
 
@@ -102,11 +111,13 @@
     foreach_reverse (i, c; pckgFQN)
       if (c == '.')
       { lastDotIndex = i; break; } // Found last dot.
-    prevFQN = pckgFQN[0..lastDotIndex];
     if (lastDotIndex == 0)
       lastName = pckgFQN; // Special case - no dot found.
     else
+    {
+      prevFQN = pckgFQN[0..lastDotIndex];
       lastName = pckgFQN[lastDotIndex+1..$];
+    }
   }
 
   /// Loads a module given an FQN path.
@@ -123,7 +134,7 @@
       auto modul = loadModuleFile(moduleFilePath);
       if (modul.getFQNPath() != moduleFQNPath)
       { // Error: the requested module is not in the correct package.
-        auto location = modul.moduleDecl.begin.getErrorLocation();
+        auto location = modul.getModuleDeclToken().getErrorLocation();
         auto msg = Format(MSG.ModuleNotInPackage, getPackageFQN(moduleFQNPath));
         infoMan ~= new SemanticError(location, msg);
       }
--- a/src/dil/semantic/Module.d	Wed Mar 12 22:41:45 2008 +0100
+++ b/src/dil/semantic/Module.d	Wed Mar 12 23:33:14 2008 +0100
@@ -124,6 +124,13 @@
     return parser.lexer.firstToken();
   }
 
+  /// Returns the begin token of the module declaration
+  /// or, if it doesn't exist, the first token in the source text.
+  Token* getModuleDeclToken()
+  {
+    return moduleDecl ? moduleDecl.begin : firstToken();
+  }
+
   /// Returns true if there are errors in the source file.
   bool hasErrors()
   {
@@ -157,8 +164,13 @@
     for (; i != 0 && moduleFQN[i] != '.'; i--)
     {}
     this.moduleFQN = moduleFQN;
-    this.packageName = moduleFQN[0..i];
-    this.moduleName = moduleFQN[(i == 0 ? 0 : i+1) .. $];
+    if (i == 0)
+      this.moduleName = moduleFQN; // No dot found.
+    else
+    {
+      this.packageName = moduleFQN[0..i];
+      this.moduleName = moduleFQN[i+1..$];
+    }
   }
 
   /// Returns the module's FQN with slashes instead of dots.