cppast/src/refl/ClassMeta.cs

214 lines
7.2 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-06-21 22:20:13 +08:00
using static System.Net.Mime.MediaTypeNames;
using static System.Runtime.InteropServices.JavaScript.JSType;
2024-04-20 18:02:20 +08:00
namespace refl
{
internal class FieldData
{
public string Name { get; set; }
public string Meta { get; set; }
2024-06-21 22:20:13 +08:00
public string Ref { get; set; }
//public Dictionary<string, string> UserMeta = new Dictionary<string, string>();
2024-04-20 18:02:20 +08:00
public FieldData(string name, string meta)
{
Name = name;
2024-06-21 22:20:13 +08:00
Meta = "{" + meta + "}";
Ref = "";
2024-04-20 18:02:20 +08:00
}
}
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; }
2024-06-21 22:20:13 +08:00
public List<ClassMeta> ClassList;
2024-04-20 18:02:20 +08:00
public List<ModuleMeta> ChildList;
2024-06-21 22:20:13 +08:00
public static OverTree OverData = new OverTree();
2024-04-20 18:02:20 +08:00
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-06-21 22:20:13 +08:00
OverData.Clear();
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;
2024-06-21 22:20:13 +08:00
string? next;
MetaToken.ParseAllMeta(attribute.Arguments, out key, out value, out next);
FieldData data = new FieldData(key, value);
2024-06-13 21:34:13 +08:00
if (!cls_meta.Fields.ContainsKey(key))
{
cls_meta.Fields.Add(key, new FieldMeta());
}
2024-06-21 22:20:13 +08:00
cls_meta.Fields[key].CtorList.Add(data);
ParseClassUserMeta(cppClass, data, next);
2024-06-13 21:34:13 +08:00
}
}
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;
2024-06-21 22:20:13 +08:00
string? next;
MetaToken.ParseAllMeta(attribute.Arguments, out key, out value, out next);
FieldData data = new FieldData(func.Name, value);
2024-04-29 14:58:45 +08:00
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-06-21 22:20:13 +08:00
cls_meta.Fields[key].MethodList.Add(data);
ParseClassUserMeta(cppClass, data, next);
2024-04-20 18:02:20 +08:00
}
}
return cls_meta;
}
2024-06-21 22:20:13 +08:00
public static void ParseClassUserMeta(CppClass cppClass, FieldData data, string? next)
{
if (next == null)
return;
OverData.Init(cppClass);
string key, value;
while (next != null)
{
MetaToken.ParseAllMeta(next, out key, out value, out next);
if (key == "ref")
{
value = OverData.Find(value.Trim());
data.Ref = value;
}
//data.UserMeta.Add(key, value);
}
}
2024-04-20 18:02:20 +08:00
public static ModuleMeta ParseCppNamespaces(CppNamespace cpp)
{
var module = new ModuleMeta(cpp.Name);
foreach (var cppClass in cpp.Classes)
{
var cls_meta = ParseCppClass(cppClass);
2024-06-21 22:20:13 +08:00
if (cls_meta.Fields.Count > 0)
2024-04-20 18:02:20 +08:00
{
module.ClassList.Add(cls_meta);
}
}
foreach (var spaces in cpp.Namespaces)
{
var child = ParseCppNamespaces(spaces);
2024-06-21 22:20:13 +08:00
if (child.ClassList.Count > 0 || child.ChildList.Count > 0)
2024-06-20 22:32:20 +08:00
{
module.ChildList.Add(child);
}
2024-04-20 18:02:20 +08:00
}
return module;
}
public static ModuleMeta ParseCompileInfo(CppCompilation cpp)
{
var module = new ModuleMeta("");
foreach (var cppClass in cpp.Classes)
{
var cls_meta = ParseCppClass(cppClass);
2024-06-21 22:20:13 +08:00
if (cls_meta.Fields.Count > 0)
2024-06-20 22:32:20 +08:00
{
module.ClassList.Add(cls_meta);
}
2024-04-20 18:02:20 +08:00
}
2024-06-21 22:20:13 +08:00
foreach (var spaces in cpp.Namespaces)
2024-04-20 18:02:20 +08:00
{
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-06-21 22:20:13 +08:00
foreach (var child in module.ChildList)
2024-04-20 18:02:20 +08:00
{
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
{
2024-06-21 22:20:13 +08:00
foreach (var cls in module.ClassList)
2024-04-20 18:02:20 +08:00
{
2024-06-21 22:20:13 +08:00
foreach (var field in cls.Fields)
2024-04-20 18:02:20 +08:00
{
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
}
}
}
}
}
}