+class WGlobal:
+ orig_text = None
+ wtype = attr_types.default
+ name = None
+ containing_file = None
+ namespace = ""
+ is_const = False
+
+ def from_string(str_def, containing_file, line_number, namespace):
+ glbl = WGlobal()
+ glbl.orig_text = str_def
+ glbl.wtype = None
+ glbl.name = ""
+ glbl.containing_file = containing_file
+ glbl.namespace = namespace
+ glbl.is_const = False
+
+ if not str.startswith(str_def, "extern"):
+ return None
+ str_def = str_def[7:]
+
+ if str.startswith(str_def, "const "):
+ glbl.is_const = True
+ str_def = str_def[6:]
+
+ if str_def.count(" ") == 0:
+ return None
+
+ parts = split_list(str_def.strip(), " ")
+
+ prefix = ""
+ i = 0
+ for part in parts:
+ if part in ["unsigned", "long", "short"]:
+ prefix += part + " "
+ i += 1
+ else:
+ break
+ parts = parts[i:]
+
+ if len(parts) <= 1:
+ return None
+
+ glbl.wtype = WType.from_string(prefix + parts[0], containing_file, line_number)
+
+ if glbl.wtype == None:
+ return None
+
+ str_def = parts[1]
+ for part in parts[2:]:
+ str_def = str_def + " " + part
+
+ if str_def.find("(") != -1 or str_def.find(")") != -1 or str_def.find("{") != -1 or str_def.find("}") != -1:
+ return None
+
+ found = str_def.find(";")
+ if found == -1:
+ return None
+
+ found_eq = str_def.find("=")
+ if found_eq != -1:
+ found = found_eq
+
+ glbl.name = str_def[:found]
+ str_def = str_def[found+1:]
+ if glbl.name.find("*") == 0:
+ glbl.name = glbl.name.replace("*", "")
+ glbl.wtype.attr_type = attr_types.star
+ if glbl.name.find("&&") == 0:
+ glbl.name = glbl.name.replace("&&", "")
+ glbl.wtype.attr_type = attr_types.ampamp
+ if glbl.name.find("&") == 0:
+ glbl.name = glbl.name.replace("&", "")
+ glbl.wtype.attr_type = attr_types.amp
+
+ if(len(str_def.strip()) != 0):
+ return None
+
+ if len(glbl.name.split(",")) > 1:
+ glbl_list = []
+ for name in glbl.name.split(","):
+ name = name.strip();
+ glbl_list.append(WGlobal())
+ glbl_list[-1].orig_text = glbl.orig_text
+ glbl_list[-1].wtype = glbl.wtype
+ glbl_list[-1].name = name
+ glbl_list[-1].containing_file = glbl.containing_file
+ glbl_list[-1].namespace = glbl.namespace
+ glbl_list[-1].is_const = glbl.is_const
+ return glbl_list
+
+ return glbl
+
+ def gen_def(self):
+ text = "\n\t"
+ if self.is_const:
+ text += "const "
+ text += self.wtype.gen_text() + " get_var_py_" + self.name + "()"
+ text += "\n\t{\n\t\t"
+ if self.wtype.attr_type == attr_types.star:
+ text += "if(" + self.namespace + "::" + self.name + " == NULL)\n\t\t\t"
+ text += "throw std::runtime_error(\"" + self.namespace + "::" + self.name + " is NULL\");\n\t\t"
+ if self.wtype.name in known_containers:
+ text += self.wtype.gen_text_cpp()
+ else:
+ if self.is_const:
+ text += "const "
+ text += self.wtype.gen_text()
+
+ if self.wtype.name in classnames or (self.wtype.name in known_containers and self.wtype.attr_type == attr_types.star):
+ text += "*"
+ text += " ret_ = "
+ if self.wtype.name in classnames:
+ text += self.wtype.name + "::get_py_obj("
+ if self.wtype.attr_type != attr_types.star:
+ text += "&"
+ text += self.namespace + "::" + self.name
+ if self.wtype.name in classnames:
+ text += ")"
+ text += ";"
+
+ if self.wtype.name in classnames:
+ text += "\n\t\treturn *ret_;"
+ elif self.wtype.name in known_containers:
+ text += known_containers[self.wtype.name].translate_cpp("ret_", self.wtype.cont.args, "\n\t\t", self.wtype.attr_type == attr_types.star)
+ text += "\n\t\treturn ret____tmp;"
+ else:
+ text += "\n\t\treturn ret_;"
+ text += "\n\t}\n"
+
+ if self.is_const:
+ return text
+
+ ret = Attribute(self.wtype, "rhs");
+
+ if self.wtype.name in classnames:
+ text += "\n\tvoid set_var_py_" + self.name + "(" + self.wtype.gen_text() + " *rhs)"
+ else:
+ text += "\n\tvoid set_var_py_" + self.name + "(" + self.wtype.gen_text() + " rhs)"
+ text += "\n\t{"
+ text += ret.gen_translation()
+ text += "\n\t\t" + self.namespace + "::" + self.name + " = " + ret.gen_call() + ";"
+ text += "\n\t}\n"
+
+ return text;
+
+ def gen_boost_py(self):
+ text = "\n\t\t\t.add_static_property(\"" + self.name + "\", &" + "YOSYS_PYTHON::get_var_py_" + self.name
+ if not self.is_const:
+ text += ", &YOSYS_PYTHON::set_var_py_" + self.name
+ text += ")"
+ return text
+