Skip to content

Ghidra is a software reverse engineering (SRE) framework

Split out type_metatype into its file

caheckman5540df9d8541
diff --git a/Ghidra/Features/Decompiler/buildNatives.gradle b/Ghidra/Features/Decompiler/buildNatives.gradle
index 10556dc6a7..ebffd55672 100644
--- a/Ghidra/Features/Decompiler/buildNatives.gradle
+++ b/Ghidra/Features/Decompiler/buildNatives.gradle
@@ -62,6 +62,7 @@ model {
include "pcoderaw.cc"
include "translate.cc"
include "opcodes.cc"
+ include "metatype.cc"
include "globalcontext.cc"
include "capability.cc"
include "architecture.cc"
@@ -176,6 +177,7 @@ model {
include "pcoderaw.cc"
include "translate.cc"
include "opcodes.cc"
+ include "metatype.cc"
include "globalcontext.cc"
include "sleigh.cc"
include "pcodecompile.cc"
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/Makefile b/Ghidra/Features/Decompiler/src/decompile/cpp/Makefile
index 9dfd2390ee..ea89ec1e61 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/Makefile
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/Makefile
@@ -83,7 +83,7 @@ EXTERNAL_CONSOLEEXT_NAMES=$(subst .cc,,$(notdir $(EXTERNAL_CONSOLEEXT_SOURCE)))
# The following macros partition all the source files, there should be no overlaps
# Some core source files used in all projects
-CORE= xml marshal space float address pcoderaw translate opcodes globalcontext
+CORE= xml marshal space float address pcoderaw translate opcodes metatype globalcontext
# Additional core files for any projects that decompile
DECCORE=capability architecture options graph cover block cast typeop database cpool \
comment stringmanage modelrules fspec action loadimage grammar varnode op type \
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/marshal.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/marshal.hh
index cd41bc6f6c..75ecfbc159 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/marshal.hh
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/marshal.hh
@@ -18,6 +18,7 @@
#include "xml.hh"
#include "opcodes.hh"
+#include "metatype.hh"
#include <list>
#include <unordered_map>
@@ -26,8 +27,6 @@ namespace ghidra {
using std::list;
using std::unordered_map;
-enum type_metatype : int4; // Forward declaration
-
/// \brief An annotation for a data element to being transferred to/from a stream
///
/// This class parallels the XML concept of an \b attribute on an element. An AttributeId describes
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/metatype.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/metatype.cc
new file mode 100644
index 0000000000..6e18dc6439
--- /dev/null
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/metatype.cc
@@ -0,0 +1,154 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "metatype.hh"
+
+namespace ghidra {
+
+/// Convert a type \b meta-type into the string name of the meta-type
+/// \param metatype is the encoded type meta-type
+/// \param res will hold the resulting string
+void metatype2string(type_metatype metatype,string &res)
+
+{
+ switch(metatype) {
+ case TYPE_VOID:
+ res = "void";
+ break;
+ case TYPE_PTR:
+ res = "ptr";
+ break;
+ case TYPE_PTRREL:
+ res = "ptrrel";
+ break;
+ case TYPE_ARRAY:
+ res = "array";
+ break;
+ case TYPE_PARTIALENUM:
+ res = "partenum";
+ break;
+ case TYPE_PARTIALSTRUCT:
+ res = "partstruct";
+ break;
+ case TYPE_PARTIALUNION:
+ res = "partunion";
+ break;
+ case TYPE_ENUM_INT:
+ res = "enum_int";
+ break;
+ case TYPE_ENUM_UINT:
+ res = "enum_uint";
+ break;
+ case TYPE_STRUCT:
+ res = "struct";
+ break;
+ case TYPE_UNION:
+ res = "union";
+ break;
+ case TYPE_SPACEBASE:
+ res = "spacebase";
+ break;
+ case TYPE_UNKNOWN:
+ res = "unknown";
+ break;
+ case TYPE_UINT:
+ res = "uint";
+ break;
+ case TYPE_INT:
+ res = "int";
+ break;
+ case TYPE_BOOL:
+ res = "bool";
+ break;
+ case TYPE_CODE:
+ res = "code";
+ break;
+ case TYPE_FLOAT:
+ res = "float";
+ break;
+ default:
+ throw LowlevelError("Unknown metatype");
+ }
+}
+
+/// Given a string description of a type \b meta-type. Return the meta-type.
+/// \param metastring is the description of the meta-type
+/// \return the encoded type meta-type
+type_metatype string2metatype(const string &metastring)
+
+{
+ switch(metastring[0]) {
+ case 'p':
+ if (metastring=="ptr")
+ return TYPE_PTR;
+ else if (metastring=="ptrrel")
+ return TYPE_PTRREL;
+ else if (metastring=="partunion")
+ return TYPE_PARTIALUNION;
+ else if (metastring=="partstruct")
+ return TYPE_PARTIALSTRUCT;
+ break;
+ case 'a':
+ if (metastring=="array")
+ return TYPE_ARRAY;
+ break;
+ case 'e':
+ if (metastring=="enum_int")
+ return TYPE_ENUM_INT;
+ else if (metastring == "enum_uint")
+ return TYPE_ENUM_UINT;
+ break;
+ case 's':
+ if (metastring=="struct")
+ return TYPE_STRUCT;
+ if (metastring=="spacebase")
+ return TYPE_SPACEBASE;
+ break;
+ case 'u':
+ if (metastring=="unknown")
+ return TYPE_UNKNOWN;
+ else if (metastring=="uint")
+ return TYPE_UINT;
+ else if (metastring=="union")
+ return TYPE_UNION;
+ break;
+ case 'i':
+ if (metastring == "int")
+ return TYPE_INT;
+ break;
+ case 'f':
+ if (metastring == "float")
+ return TYPE_FLOAT;
+ break;
+ case 'b':
+ if (metastring == "bool")
+ return TYPE_BOOL;
+ break;
+ case 'c':
+ if (metastring == "code")
+ return TYPE_CODE;
+ break;
+ case 'v':
+ if (metastring == "void")
+ return TYPE_VOID;
+ break;
+ default:
+ break;
+ }
+ throw LowlevelError("Unknown metatype: "+metastring);
+}
+
+} // End namespace ghidra
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/metatype.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/metatype.hh
new file mode 100644
index 0000000000..aeb1ea5c6d
--- /dev/null
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/metatype.hh
@@ -0,0 +1,57 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/// \file metatype.hh
+/// \brief Enumeration describing classes of data-types
+
+#ifndef __METATYPE_HH__
+#define __METATYPE_HH__
+
+#include "error.hh"
+
+namespace ghidra {
+
+/// The core meta-types supported by the decompiler. These are sizeless templates
+/// for the elements making up the type algebra. Index is important for Datatype::base2sub array.
+enum type_metatype {
+ TYPE_VOID = 17, ///< Standard "void" type, absence of type
+ TYPE_SPACEBASE = 16, ///< Placeholder for symbol/type look-up calculations
+ TYPE_UNKNOWN = 15, ///< An unknown low-level type. Treated as an unsigned integer.
+ TYPE_INT = 14, ///< Signed integer. Signed is considered less specific than unsigned in C
+ TYPE_UINT = 13, ///< Unsigned integer
+ TYPE_BOOL = 12, ///< Boolean
+ TYPE_CODE = 11, ///< Data is actual executable code
+ TYPE_FLOAT = 10, ///< Floating-point
+
+ TYPE_PTR = 9, ///< Pointer data-type
+ TYPE_PTRREL = 8, ///< Pointer relative to another data-type (specialization of TYPE_PTR)
+ TYPE_ARRAY = 7, ///< Array data-type, made up of a sequence of "element" datatype
+ TYPE_ENUM_UINT = 6, ///< Unsigned enumeration data-type (specialization of TYPE_UINT)
+ TYPE_ENUM_INT = 5, ///< Signed enumeration data-type (specialization of TYPE_INT)
+ TYPE_STRUCT = 4, ///< Structure data-type, made up of component datatypes
+ TYPE_UNION = 3, ///< An overlapping union of multiple datatypes
+ TYPE_PARTIALENUM = 2, ///< Part of an enumerated value (specialization of TYPE_UINT)
+ TYPE_PARTIALSTRUCT = 1, ///< Part of a structure, stored separately from the whole
+ TYPE_PARTIALUNION = 0 ///< Part of a union
+};
+
+/// Convert type \b meta-type to name
+extern void metatype2string(type_metatype metatype,string &res);
+
+/// Convert string to type \b meta-type
+extern type_metatype string2metatype(const string &metastring);
+
+} // End namespace ghidra
+#endif
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/type.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/type.cc
index e745f67ed0..40370a3634 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/type.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/type.cc
@@ -249,139 +249,6 @@ int4 Datatype::compareDependency(const Datatype &op) const
return 0;
}
-/// Convert a type \b meta-type into the string name of the meta-type
-/// \param metatype is the encoded type meta-type
-/// \param res will hold the resulting string
-void metatype2string(type_metatype metatype,string &res)
-
-{
- switch(metatype) {
- case TYPE_VOID:
- res = "void";
- break;
- case TYPE_PTR:
- res = "ptr";
- break;
- case TYPE_PTRREL:
- res = "ptrrel";
- break;
- case TYPE_ARRAY:
- res = "array";
- break;
- case TYPE_PARTIALENUM:
- res = "partenum";
- break;
- case TYPE_PARTIALSTRUCT:
- res = "partstruct";
- break;
- case TYPE_PARTIALUNION:
- res = "partunion";
- break;
- case TYPE_ENUM_INT:
- res = "enum_int";
- break;
- case TYPE_ENUM_UINT:
- res = "enum_uint";
- break;
- case TYPE_STRUCT:
- res = "struct";
- break;
- case TYPE_UNION:
- res = "union";
- break;
- case TYPE_SPACEBASE:
- res = "spacebase";
- break;
- case TYPE_UNKNOWN:
- res = "unknown";
- break;
- case TYPE_UINT:
- res = "uint";
- break;
- case TYPE_INT:
- res = "int";
- break;
- case TYPE_BOOL:
- res = "bool";
- break;
- case TYPE_CODE:
- res = "code";
- break;
- case TYPE_FLOAT:
- res = "float";
- break;
- default:
- throw LowlevelError("Unknown metatype");
- }
-}
-
-/// Given a string description of a type \b meta-type. Return the meta-type.
-/// \param metastring is the description of the meta-type
-/// \return the encoded type meta-type
-type_metatype string2metatype(const string &metastring)
-
-{
- switch(metastring[0]) {
- case 'p':
- if (metastring=="ptr")
- return TYPE_PTR;
- else if (metastring=="ptrrel")
- return TYPE_PTRREL;
- else if (metastring=="partunion")
- return TYPE_PARTIALUNION;
- else if (metastring=="partstruct")
- return TYPE_PARTIALSTRUCT;
- break;
- case 'a':
- if (metastring=="array")
- return TYPE_ARRAY;
- break;
- case 'e':
- if (metastring=="enum_int")
- return TYPE_ENUM_INT;
- else if (metastring == "enum_uint")
- return TYPE_ENUM_UINT;
- break;
- case 's':
- if (metastring=="struct")
- return TYPE_STRUCT;
- if (metastring=="spacebase")
- return TYPE_SPACEBASE;
- break;
- case 'u':
- if (metastring=="unknown")
- return TYPE_UNKNOWN;
- else if (metastring=="uint")
- return TYPE_UINT;
- else if (metastring=="union")
- return TYPE_UNION;
- break;
- case 'i':
- if (metastring == "int")
- return TYPE_INT;
- break;
- case 'f':
- if (metastring == "float")
- return TYPE_FLOAT;
- break;
- case 'b':
- if (metastring == "bool")
- return TYPE_BOOL;
- break;
- case 'c':
- if (metastring == "code")
- return TYPE_CODE;
- break;
- case 'v':
- if (metastring == "void")
- return TYPE_VOID;
- break;
- default:
- break;
- }
- throw LowlevelError("Unknown metatype: "+metastring);
-}
-
/// Given a description of a data-type \e class, return the \b type_class.
/// \param classstring is the description of the class
/// \return the encoded type_class
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/type.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/type.hh
index 4f8848eb9a..f85825ccd4 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/type.hh
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/type.hh
@@ -20,6 +20,7 @@
#define __TYPE_HH__
#include "address.hh"
+#include "metatype.hh"
namespace ghidra {
@@ -75,30 +76,6 @@ extern void print_data(ostream &s,uint1 *buffer,int4 size,const Address &baseadd
//extern void print_char(ostream &s,int4 onechar);
//extern bool print_string(ostream &s,uint1 *buffer,int4 size);
-/// The core meta-types supported by the decompiler. These are sizeless templates
-/// for the elements making up the type algebra. Index is important for Datatype::base2sub array.
-enum type_metatype : int4 {
- TYPE_VOID = 17, ///< Standard "void" type, absence of type
- TYPE_SPACEBASE = 16, ///< Placeholder for symbol/type look-up calculations
- TYPE_UNKNOWN = 15, ///< An unknown low-level type. Treated as an unsigned integer.
- TYPE_INT = 14, ///< Signed integer. Signed is considered less specific than unsigned in C
- TYPE_UINT = 13, ///< Unsigned integer
- TYPE_BOOL = 12, ///< Boolean
- TYPE_CODE = 11, ///< Data is actual executable code
- TYPE_FLOAT = 10, ///< Floating-point
-
- TYPE_PTR = 9, ///< Pointer data-type
- TYPE_PTRREL = 8, ///< Pointer relative to another data-type (specialization of TYPE_PTR)
- TYPE_ARRAY = 7, ///< Array data-type, made up of a sequence of "element" datatype
- TYPE_ENUM_UINT = 6, ///< Unsigned enumeration data-type (specialization of TYPE_UINT)
- TYPE_ENUM_INT = 5, ///< Signed enumeration data-type (specialization of TYPE_INT)
- TYPE_STRUCT = 4, ///< Structure data-type, made up of component datatypes
- TYPE_UNION = 3, ///< An overlapping union of multiple datatypes
- TYPE_PARTIALENUM = 2, ///< Part of an enumerated value (specialization of TYPE_UINT)
- TYPE_PARTIALSTRUCT = 1, ///< Part of a structure, stored separately from the whole
- TYPE_PARTIALUNION = 0 ///< Part of a union
-};
-
/// Specializations of the core meta-types. Each enumeration is associated with a specific #type_metatype.
/// Ordering is important: The lower the number, the more \b specific the data-type, affecting propagation.
enum sub_metatype {
@@ -141,12 +118,6 @@ enum type_class {
TYPECLASS_CLASS4 = 103 ///< Architecture specific class 4
};
-/// Convert type \b meta-type to name
-extern void metatype2string(type_metatype metatype,string &res);
-
-/// Convert string to type \b meta-type
-extern type_metatype string2metatype(const string &metastring);
-
/// Convert a string to a data-type class
extern type_class string2typeclass(const string &classstring);