cppast/src/refl/ClassMeta.cs

183 lines
6.1 KiB
C#
Raw Normal View History

2024-04-20 18:02:20 +08:00
using CppAst;
2024-06-13 21:34:13 +08:00
using Irony;
2024-04-20 18:02:20 +08:00
namespace refl
{
internal class FieldData
{
public string Name { get; set; }
public string Meta { get; set; }
public FieldData(string name, string meta)
{
Name = name;
Meta = meta;
}
}
internal class FieldMeta
{
public List<FieldData> MemberList { get; set; }
public List<FieldData> MethodList { get; set; }
2024-06-13 21:34:13 +08:00
public List<FieldData> CtorList { get; set; }
2024-04-20 18:02:20 +08:00
public FieldMeta()
{
MemberList = new List<FieldData>();
MethodList = new List<FieldData>();
2024-06-13 21:34:13 +08:00
CtorList = new List<FieldData>();
2024-04-20 18:02:20 +08:00
}
}
internal class ClassMeta
{
public string Name { get; set; }
2024-04-21 21:44:53 +08:00
public string ParentName { get; set; }
2024-04-20 18:02:20 +08:00
public string Path { get; set; }
public Dictionary<string, FieldMeta> Fields { get; set; }
2024-04-21 21:44:53 +08:00
public ClassMeta(string name, string parentName)
2024-04-20 18:02:20 +08:00
{
Name = name;
2024-04-21 21:44:53 +08:00
ParentName = parentName;
2024-04-20 18:02:20 +08:00
Path = "";
Fields = new Dictionary<string, FieldMeta>();
}
}
internal class ModuleMeta
{
2024-04-25 21:47:15 +08:00
public static HashSet<string> NameSet = new HashSet<string>();
2024-04-20 18:02:20 +08:00
public string NameSpace { get; set; }
public List<ClassMeta> ClassList;
public List<ModuleMeta> ChildList;
public ModuleMeta(string name_space)
{
NameSpace = name_space;
ClassList = new List<ClassMeta>();
ChildList = new List<ModuleMeta>();
}
2024-04-26 19:22:14 +08:00
public bool HasMeta()
{
return ClassList.Count > 0 || ChildList.Count > 0;
}
2024-04-20 18:02:20 +08:00
public static ClassMeta ParseCppClass(CppClass cppClass)
{
2024-04-21 21:44:53 +08:00
string parentName = "void";
if (cppClass.BaseTypes.Count == 1)
{
var type = cppClass.BaseTypes[0].Type;
CppClass? parent = type as CppClass;
if (parent != null)
{
parentName = parent.Name;
}
}
ClassMeta cls_meta = new ClassMeta(cppClass.Name, parentName);
2024-04-20 18:02:20 +08:00
foreach (var field in cppClass.Fields)
{
if (field.Attributes.Count == 0)
continue;
foreach (var attribute in field.Attributes)
{
2024-04-29 14:58:45 +08:00
string key, value;
MetaToken.ParseMeta(attribute.Arguments, out key, out value);
if (!cls_meta.Fields.ContainsKey(key))
2024-04-20 18:02:20 +08:00
{
2024-04-29 14:58:45 +08:00
cls_meta.Fields.Add(key, new FieldMeta());
2024-04-20 18:02:20 +08:00
}
2024-04-29 14:58:45 +08:00
cls_meta.Fields[key].MemberList.Add(new FieldData(field.Name, value));
2024-04-20 18:02:20 +08:00
}
}
2024-06-13 21:34:13 +08:00
foreach (var func in cppClass.Constructors)
{
if (func.Attributes.Count == 0)
continue;
foreach (var attribute in func.Attributes)
{
string key, value;
MetaToken.ParseMeta(attribute.Arguments, out key, out value);
if (!cls_meta.Fields.ContainsKey(key))
{
cls_meta.Fields.Add(key, new FieldMeta());
}
List<string> nameList = new List<string>{ cppClass.Name };
foreach (var parm in func.Parameters)
{
nameList.Add(parm.Type.FullName);
}
cls_meta.Fields[key].CtorList.Add(new FieldData(string.Join(",", nameList), value));
}
}
2024-04-20 18:02:20 +08:00
foreach (var func in cppClass.Functions)
{
if (func.Attributes.Count == 0)
continue;
foreach (var attribute in func.Attributes)
{
2024-04-29 14:58:45 +08:00
string key, value;
MetaToken.ParseMeta(attribute.Arguments, out key, out value);
if (!cls_meta.Fields.ContainsKey(key))
2024-04-20 18:02:20 +08:00
{
2024-04-29 14:58:45 +08:00
cls_meta.Fields.Add(key, new FieldMeta());
2024-04-20 18:02:20 +08:00
}
2024-04-29 14:58:45 +08:00
cls_meta.Fields[key].MethodList.Add(new FieldData(func.Name, value));
2024-04-20 18:02:20 +08:00
}
}
return cls_meta;
}
public static ModuleMeta ParseCppNamespaces(CppNamespace cpp)
{
var module = new ModuleMeta(cpp.Name);
foreach (var cppClass in cpp.Classes)
{
var cls_meta = ParseCppClass(cppClass);
if(cls_meta.Fields.Count > 0)
{
module.ClassList.Add(cls_meta);
}
}
foreach (var spaces in cpp.Namespaces)
{
var child = ParseCppNamespaces(spaces);
module.ChildList.Add(child);
}
return module;
}
public static ModuleMeta ParseCompileInfo(CppCompilation cpp)
{
var module = new ModuleMeta("");
foreach (var cppClass in cpp.Classes)
{
var cls_meta = ParseCppClass(cppClass);
module.ClassList.Add(cls_meta);
}
foreach(var spaces in cpp.Namespaces)
{
var child = ParseCppNamespaces(spaces);
module.ChildList.Add(child);
}
2024-04-25 21:47:15 +08:00
ParseMetaName(module);
2024-04-20 18:02:20 +08:00
return module;
}
2024-04-25 21:47:15 +08:00
public static void ParseMetaName(ModuleMeta module)
2024-04-20 18:02:20 +08:00
{
2024-04-25 21:47:15 +08:00
ParseClassMetaName(module);
2024-04-20 18:02:20 +08:00
foreach(var child in module.ChildList)
{
2024-04-25 21:47:15 +08:00
ParseMetaName(child);
2024-04-20 18:02:20 +08:00
}
}
2024-04-25 21:47:15 +08:00
public static void ParseClassMetaName(ModuleMeta module)
2024-04-20 18:02:20 +08:00
{
foreach(var cls in module.ClassList)
{
foreach(var field in cls.Fields)
{
2024-04-25 21:47:15 +08:00
if (!NameSet.Contains(field.Key))
2024-04-20 18:02:20 +08:00
{
2024-04-25 21:47:15 +08:00
NameSet.Add(field.Key);
2024-04-20 18:02:20 +08:00
}
}
}
}
}
}