From d3fec31879fc93e932d9ca882b32d165c3070b27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C3=ABl=20Kooi?= <48814281+RA-Kooi@users.noreply.github.com> Date: Tue, 21 Dec 2021 07:18:15 +0100 Subject: [PATCH] Support different way of adding member functions Different version of CW did the right thing this time where they add member functions as class/struct members, rather than making them global and not even adding access level attributes to it. Though when I say "the right thing" take this with a heavy grain of salt, as apparently it was at the expense of parameter names. Though perhaps this game was compiled with different debug parameters? --- DwarfOne2C/CWriter/CWriter.cs | 5 +++++ DwarfOne2C/CWriter/Function.cs | 29 +++++++++++++++++++++++++++ DwarfOne2C/Parsing/BlockParser.cs | 1 + DwarfOne2C/Parsing/CompilationUnit.cs | 17 ++++++++++++++-- DwarfOne2C/Tags.cs | 1 + 5 files changed, 51 insertions(+), 2 deletions(-) diff --git a/DwarfOne2C/CWriter/CWriter.cs b/DwarfOne2C/CWriter/CWriter.cs index c908a8f..9762d8e 100644 --- a/DwarfOne2C/CWriter/CWriter.cs +++ b/DwarfOne2C/CWriter/CWriter.cs @@ -174,6 +174,11 @@ private static List TagDispatcher( case TagType.Enum: code.AddRange(GenerateEnum(allTags, IDToIndex, current, depth)); break; + case TagType.MemberFunc: + { + code.AddRange( + GenerateMemberFunction(allTags, IDToIndex, current, depth)); + } break; } return code; diff --git a/DwarfOne2C/CWriter/Function.cs b/DwarfOne2C/CWriter/Function.cs index df8e45f..b21be60 100644 --- a/DwarfOne2C/CWriter/Function.cs +++ b/DwarfOne2C/CWriter/Function.cs @@ -105,5 +105,34 @@ private static List GenerateFunction( return code; } + + private static List GenerateMemberFunction( + List allTags, + Dictionary IDToIndex, + Tag current, + int depth) + { + List code = new(); + + string tabs = new('\t', depth); + + if(current.comment != null) + code.Add(tabs + "// " + current.comment); + + Tag referenced = allTags[IDToIndex[current.typeID]]; + + // Reforge the tag to generate properly. + referenced.tagType = TagType.GlobalFunc; + referenced.name = current.name; + + code.AddRange( + GenerateFunction( + allTags, + IDToIndex, + referenced, + depth)); + + return code; + } } } diff --git a/DwarfOne2C/Parsing/BlockParser.cs b/DwarfOne2C/Parsing/BlockParser.cs index be26dd7..79c0fa0 100644 --- a/DwarfOne2C/Parsing/BlockParser.cs +++ b/DwarfOne2C/Parsing/BlockParser.cs @@ -165,6 +165,7 @@ private Tag ParseFunction( && !ParseName(line, tag) && !ParseTypes(line, tag) && !ParseLoUser(line, tag) + && !ParseAccessLevel(line, tag) && !ParseAtMember(line, tag)) Console.WriteLine("Unknown attribute: " + line + " @" + current); } diff --git a/DwarfOne2C/Parsing/CompilationUnit.cs b/DwarfOne2C/Parsing/CompilationUnit.cs index a7ff286..ecafe98 100644 --- a/DwarfOne2C/Parsing/CompilationUnit.cs +++ b/DwarfOne2C/Parsing/CompilationUnit.cs @@ -252,8 +252,6 @@ public void SecondPass() } } - // Fixup TAG_padding being member variables, global variables, and - // global functions at the same time... void Recurse(Tag parent, int depth) { if(parent.firstChild == -1) @@ -263,6 +261,8 @@ void Recurse(Tag parent, int depth) child.sibling != Tag.NoSibling; child = allTags[IDToIndex[child.sibling]]) { + // Fixup TAG_padding being member variables, global variables, + // and global functions at the same time... if(child.tagType == TagType.Padding) { if(depth == 0) @@ -280,6 +280,19 @@ void Recurse(Tag parent, int depth) break; } } + else if(child.tagType == TagType.GlobalFunc) + { + if(depth > 0) + { + // Remove staticness from functions that are + // not top level + child.isStatic = false; + + // It is also a class/struct member function + // in this case + child.tagType = TagType.MemberFunc; + } + } if(child.tagType == TagType.Class || child.tagType == TagType.Struct) diff --git a/DwarfOne2C/Tags.cs b/DwarfOne2C/Tags.cs index d5d3a08..f020a2e 100644 --- a/DwarfOne2C/Tags.cs +++ b/DwarfOne2C/Tags.cs @@ -46,6 +46,7 @@ public enum TagType TypeDef, // In reverse order Union, Padding, // Local variable OR member... + MemberFunc, } public TagType tagType;