systemc: Add some additional error checks.

Change-Id: I19c5e6f1795c2777dbe7d210cfa01f6ced2020f3
Reviewed-on: https://gem5-review.googlesource.com/c/12815
Reviewed-by: Gabe Black <gabeblack@google.com>
Maintainer: Gabe Black <gabeblack@google.com>
diff --git a/src/systemc/core/sc_export.cc b/src/systemc/core/sc_export.cc
index 252f8c0..0524ca8 100644
--- a/src/systemc/core/sc_export.cc
+++ b/src/systemc/core/sc_export.cc
@@ -58,17 +58,17 @@
 {
     if (sc_is_running()) {
         reportError("(E121) insert sc_export failed", "simulation running",
-                n, kind());
+                name(), kind());
     }
     if (::sc_gem5::scheduler.elaborationDone()) {
         reportError("(E121) insert sc_export failed", "elaboration done",
-                n, kind());
+                name(), kind());
     }
 
     ::sc_gem5::Module *m = ::sc_gem5::currentModule();
     if (!m) {
         reportError("(E122) sc_export specified outside of module",
-                nullptr, n, kind());
+                nullptr, name(), kind());
     } else {
         m->exports.push_back(this);
     }
diff --git a/src/systemc/core/sc_module_name.cc b/src/systemc/core/sc_module_name.cc
index ca568e2..c18c0f9 100644
--- a/src/systemc/core/sc_module_name.cc
+++ b/src/systemc/core/sc_module_name.cc
@@ -30,14 +30,24 @@
 
 #include "base/logging.hh"
 #include "systemc/core/module.hh"
+#include "systemc/core/scheduler.hh"
+#include "systemc/ext/core/sc_main.hh"
 #include "systemc/ext/core/sc_module_name.hh"
+#include "systemc/ext/utils/sc_report_handler.hh"
 
 namespace sc_core
 {
 
 sc_module_name::sc_module_name(const char *name) :
-    _name(name), _gem5_module(new sc_gem5::Module(name)), _on_the_stack(true)
-{}
+    _name(name), _gem5_module(nullptr), _on_the_stack(true)
+{
+    if (sc_is_running())
+        SC_REPORT_ERROR("(E529) insert module failed", "simulation running");
+    else if (::sc_gem5::scheduler.elaborationDone())
+        SC_REPORT_ERROR("(E529) insert module failed", "elaboration done");
+    else
+        _gem5_module = new sc_gem5::Module(name);
+}
 
 sc_module_name::sc_module_name(const sc_module_name &other) :
     _name(other._name), _gem5_module(other._gem5_module), _on_the_stack(false)
diff --git a/src/systemc/core/sc_port.cc b/src/systemc/core/sc_port.cc
index 52f66b7..ddfe39f 100644
--- a/src/systemc/core/sc_port.cc
+++ b/src/systemc/core/sc_port.cc
@@ -55,22 +55,22 @@
 
 }
 
-sc_port_base::sc_port_base(const char *name, int n, sc_port_policy p) :
-    sc_object(name), _gem5Port(new ::sc_gem5::Port(this, n))
+sc_port_base::sc_port_base(const char *n, int max_size, sc_port_policy p) :
+    sc_object(n), _gem5Port(new ::sc_gem5::Port(this, max_size))
 {
     if (sc_is_running()) {
         reportError("(E110) insert port failed", "simulation running",
-                name, kind());
+                name(), kind());
     }
     if (::sc_gem5::scheduler.elaborationDone()) {
         reportError("(E110) insert port failed", "elaboration done",
-                name, kind());
+                name(), kind());
     }
 
     ::sc_gem5::Module *m = ::sc_gem5::currentModule();
     if (!m) {
         reportError("(E100) port specified outside of module",
-                nullptr, name, kind());
+                nullptr, name(), kind());
     } else {
         m->ports.push_back(this);
     }
diff --git a/src/systemc/core/sc_prim.cc b/src/systemc/core/sc_prim.cc
index 0f44101..216bd78 100644
--- a/src/systemc/core/sc_prim.cc
+++ b/src/systemc/core/sc_prim.cc
@@ -30,6 +30,7 @@
 #include "base/logging.hh"
 #include "systemc/core/channel.hh"
 #include "systemc/core/scheduler.hh"
+#include "systemc/ext/core/sc_main.hh"
 #include "systemc/ext/core/sc_prim.hh"
 
 namespace sc_gem5
@@ -42,13 +43,32 @@
 namespace sc_core
 {
 
-sc_prim_channel::sc_prim_channel() :
-    _gem5_channel(new sc_gem5::Channel(this))
-{}
+sc_prim_channel::sc_prim_channel() : _gem5_channel(nullptr)
+{
+    if (sc_is_running()) {
+        SC_REPORT_ERROR("(E113) insert primitive channel failed",
+                "simulation running");
+    } else if (::sc_gem5::scheduler.elaborationDone()) {
+        SC_REPORT_ERROR("(E113) insert primitive channel failed",
+                "elaboration done");
+    } else {
+        _gem5_channel = new sc_gem5::Channel(this);
+    }
+}
 
 sc_prim_channel::sc_prim_channel(const char *_name) :
-    sc_object(_name), _gem5_channel(new sc_gem5::Channel(this))
-{}
+    sc_object(_name), _gem5_channel(nullptr)
+{
+    if (sc_is_running()) {
+        SC_REPORT_ERROR("(E113) insert primitive channel failed",
+                "simulation running");
+    } else if (::sc_gem5::scheduler.elaborationDone()) {
+        SC_REPORT_ERROR("(E113) insert primitive channel failed",
+                "elaboration done");
+    } else {
+        _gem5_channel = new sc_gem5::Channel(this);
+    }
+}
 
 sc_prim_channel::~sc_prim_channel() { delete _gem5_channel; }