//protected string GetValue(AstToken token) //{ // switch (token.name) // { // case "InitListExpr": // System.Text.StringBuilder builder = new System.Text.StringBuilder(); // builder.Append("["); // for (int i = 0; i < token.children.Count; i++) // { // if (i != 0) builder.Append(","); // builder.Append(GetValue(token.children[i])); // } // builder.Append("]"); // return builder.ToString(); // case "ImplicitCastExpr": // if (token.children.Count > 1) throw new InvalidCastException(); // return GetValue(token.children[0]); // case "CXXBoolLiteralExpr": // return token.properties[1]; // property 0 is 'bool' // case "IntegerLiteral": // return token.properties[1]; // property 0 is 'int'; if this one have context - this may be define // case "GNUNullExpr": // return "null"; // case "StringLiteral": // return token.properties[2]; // property 0 is 'char[...]', property 1 is 'lvalue' // case "CStyleCastExpr": // if (token.children.Count > 1) throw new InvalidCastException(); // return GetValue(token.children[0]); // here probably need append cast prefix // case "DeclRefExpr": // return token.properties[3]; // here need check for pointer to declaration // case "UnaryOperator": // var operation = token.properties[1]; // if (operation == "prefix") return token.properties[token.properties.Length - 1] + GetValue(token.children[0]); // else if (operation == "postfix") return GetValue(token.children[0]) + token.properties[token.properties.Length - 1]; // else throw new Exception(operation); // case "BinaryOperator": // return GetValue(token.children[0]) + token.properties[token.properties.Length - 1] + GetValue(token.children[1]); // case "ParenExpr": // return "(" + GetValue(token.children[0]) + ")"; // default: throw new Exception(token.name); // } //} private VariableDeclaration GetVariableDeclaration(AstToken token) { var variable = new VariableDeclaration() { name = token.properties[0], type = token.properties[1], }; if (token.Attributes.HasFlag(AstAttributes.CInit)) { variable.value = GetFunctionBody(token.children[0]); } if (token.Attributes.HasFlag(AstAttributes.Extern)) { variable.isExtern = true; } if (token.Attributes.HasFlag(AstAttributes.Extern)) { variable.isExtern = true; } for (int i = 2; i < token.properties.Length; i++) { switch (token.properties[i]) { default: throw new Exception(token.properties[i]); } } return(variable); }
private static IEnumerable <DeclRefExprProperties> GetVariableThatGetsSaid(AstToken methodDecl) { var memberExpressions = methodDecl.VisitEnumerable ( x => x.unknownName == "MemberExpr" && x.parent.unknownName == "CXXMemberCallExpr" && x.properties.Contains("->Say") ); foreach (var memberExpression in memberExpressions) { var otherchildren = memberExpression.parent .VisitEnumerable(x => x != memberExpression) .Where(x => x.unknownName == "DeclRefExpr") .Select(x => new DeclRefExprProperties(x)) .ToList() ; //Just take the last one if (otherchildren.Count == 1) { yield return(otherchildren.FirstOrDefault()); } else { //Multiple (path: pSession->Say( &vMSG )) yield return(otherchildren.FirstOrDefault(x => x.Token.properties[0] == "CPacket")); } } }
private bool TryGetMethodCallWrite(AstToken token, out string name) { //ImplicitCastExpr name = string.Empty; DeclRefExprProperties properties = null; switch (token.Type) { case AstType.MemberExpr when token.properties[0] == "<bound member function type>": name = token.properties[1]; name = name.TrimStart("->").TrimStart("Get"); return(true); case AstType.CXXMemberCallExpr: foreach (var tokenChild in token.children) { if (TryGetMethodCallWrite(tokenChild, out name)) { return(true); } } break; default: break; } return(false); return(false); }
public static (string targetClass, TypeDeclaration aliasType) GetTypeDef(AstToken token) { var targetClass = GetTranslatedTargetClass(token.context.sourceFile); //var targetFileName = GetTranslatedTargetFile(token.context.sourceFile); var aliasType = GetTypeDeclaration(token.children[0]); return(targetClass, aliasType); }
public override bool OnNodeParsed(AstFileReaderContext readerContext, AstToken token, int depth) { if (depth == 1 && token.Type != AstType.CXXMethodDecl) { readerContext.SkipSubTree(); return(true); } return(false); }
protected void ProcessStructDeclaration(AstToken token) { var targetClass = GetTranslatedTargetClass(token.context.sourceFile); var targetFileName = GetTranslatedTargetFile(token.context.sourceFile); var translationFile = GetTranslation(targetClass); var structure = GetStructDeclaration(token); translationFile.structures.Add(token.offset, structure); }
protected void ProcessTypedef(AstToken token) { var(targetClass, aliasType) = GetTypeDef(token); var translationFile = GetTranslation(targetClass); translationFile.typedef.Add(token.offset, new TypedefDeclaration() { name = token.properties[0], alias = aliasType }); }
public static void Visit(this AstToken token, Func <AstToken, bool> predicate, Action <AstToken> onFound) { foreach (var tokenChild in token.children) { if (predicate(tokenChild)) { onFound(tokenChild); } Visit(tokenChild, predicate, onFound); } }
private static FunctionProtoDeclaration GetFunctionProtoDeclaration(AstToken token) { var function = new FunctionProtoDeclaration() { returnType = GetTypeDeclaration(token.children[0]) }; for (int i = 1; i < token.children.Count; i++) { function.parameters.Add(GetTypeDeclaration(token.children[i])); } return(function); }
private static void ProcessSetInstruction(AstToken methodDecl) { var setId = methodDecl.VisitEnumerable(x => x.unknownName == "MemberExpr" && x.properties.Contains(".SetID")).FirstOrDefault(); if (setId != null) { var literals = setId.parent.VisitEnumerable(x => x.unknownName == "BinaryOperator"); foreach (var literal in literals) { var exp = literal.AsExpression(); Console.WriteLine($"Set: {exp}"); } } }
private FunctionDeclaration GetFunctionDeclaration(AstToken token) { var function = new FunctionDeclaration() { name = token.properties[0], }; foreach (var childToken in token.children) { switch (childToken.Type) { case AstType.ParmVarDecl: FunctionDeclaration.Parameter parameter = null; if (childToken.properties.Length == 1) { parameter = new FunctionDeclaration.Parameter() { name = null, type = childToken.properties[0], }; } else if (childToken.properties.Length == 2) { parameter = new FunctionDeclaration.Parameter() { name = childToken.properties[0], type = childToken.properties[1], }; } if (token.Attributes.HasFlag(AstAttributes.CInit)) { parameter.value = GetFunctionBody(childToken.children[0]); } function.parameters.Add(parameter); break; case AstType.CompoundStmt: function.body = GetFunctionBody(childToken); break; case AstType.FullComment: break; // ignore default: throw new Exception(childToken.unknownName); } } return(function); }
protected void ProcessVariableDeclaration(AstToken token) { var targetClass = GetTranslatedTargetClass(token.context.sourceFile); var targetFileName = GetTranslatedTargetFile(token.context.sourceFile); var translationFile = GetTranslation(targetClass); var variable = GetVariableDeclaration(token); translationFile.variables.Add(token.offset, variable); //if (variable.value != null) //{ // Console.WriteLine(variable.name + " = " + variable.value); //} }
public static IEnumerable <AstToken> VisitEnumerable(this AstToken token, Func <AstToken, bool> predicate) { foreach (var tokenChild in token.children) { if (predicate(tokenChild)) { yield return(tokenChild); } var results = VisitEnumerable(tokenChild, predicate); foreach (var astToken in results) { yield return(astToken); } } }
protected void ProcessMainLevelToken(AstToken token) { // process only non standart file source if (!token.context.sourceFile.StartsWith("<", StringComparison.InvariantCulture) && !token.context.sourceFile.StartsWith("/usr", StringComparison.InvariantCulture)) { switch (token.Type) { case AstType.TypedefDecl: ProcessTypedef(token); break; case AstType.UsingDirectiveDecl: // Console.WriteLine(" using {0}", string.Join(" ", token.properties)); break; case AstType.VarDecl: ProcessVariableDeclaration(token); break; case AstType.FunctionDecl: ProcessFunctionDeclaration(token); break; case AstType.EnumDecl: ProcessEnum(token); break; case AstType.CXXRecordDecl: // declaration of struct/class without body ProcessStructDeclaration(token); break; case AstType.EmptyDecl: break; case AstType.LinkageSpecDecl: foreach (var childToken in token.children) { ProcessMainLevelToken(childToken); } break; default: throw new NotImplementedException(token.unknownName); } } }
public List <AstToken> Parse(AstParserInterceptor interceptor) { var fastReader = new FastLineReader(_filePath); var readerContext = new AstFileReaderContext(fastReader.ReadLine()); AstToken currentRoot = null; var rootTokens = new List <AstToken>(); foreach (var rawLine in readerContext) { AstTokenParserUtils.GetEssentialPart(rawLine, out var lineDepth, out var line); var token = new AstToken(true) { Line = readerContext.CurrentLine };; AstTokenParserUtils.ParseTokenDescription(token, line); if (lineDepth == 0) { currentRoot = token; rootTokens.Add(token); continue; } if (currentRoot == null) { currentRoot = new AstToken(true) { unknownName = "Unknown" }; rootTokens.Add(currentRoot); } if (interceptor.OnNodeParsed(readerContext, token, lineDepth)) { continue; } currentRoot.AddChild(token, lineDepth - 1); } return(rootTokens); }
protected void ProcessFunctionDeclaration(AstToken token) { var targetClass = GetTranslatedTargetClass(token.context.sourceFile); var targetFileName = GetTranslatedTargetFile(token.context.sourceFile); var translationFile = GetTranslation(targetClass); var function = GetFunctionDeclaration(token); translationFile.functions.Add(token.offset, function); //Console.Write(function.name + "("); //foreach (var param in function.parameters) //{ // Console.Write(param.type+" "); // if (param.name != null) // Console.Write(param.name + " "); // if (param.value != null) // Console.Write("= "+param.value); // Console.Write(", "); //} //Console.WriteLine(")"); }
protected void ProcessEnum(AstToken token) { var targetClass = GetTranslatedTargetClass(token.context.sourceFile); var targetFileName = GetTranslatedTargetFile(token.context.sourceFile); var translationFile = GetTranslation(targetClass); EnumDeclaration enumDeclaration = new EnumDeclaration() { name = token.properties.Length > 0 ? token.properties[0] : "[UnnamedEnum]" }; foreach (var childToken in token.children) { switch (childToken.Type) { case AstType.EnumConstantDecl: var enumConstant = new EnumDeclaration.Property() { name = childToken.properties[0] }; if (childToken.children.Count > 0) { foreach (var subChildToken in childToken.children) { enumConstant.value = GetFunctionBody(subChildToken); } } enumDeclaration.properties.Add(enumConstant); break; case AstType.FullComment: break; default: throw new Exception(childToken.unknownName); } } translationFile.enums.Add(token.offset, enumDeclaration); }
public static Expression AsExpression(this AstToken token) { var name = token.unknownName; if (name == "BinaryOperator") { ExpressionType type = token.properties[1] switch { "+" => ExpressionType.Add, "'+'" => ExpressionType.Add, _ => throw new NotImplementedException(token.properties[1]) }; return(Expression.MakeBinary(type, token.children[0].AsExpression(), token.children[1].AsExpression())); } if (name == "ParenExpr") { if (token.children.Count == 1) { return(token.children[0].AsExpression()); } throw new NotSupportedException("ParenExpr with many children is Not supported"); } if (name == "IntegerLiteral") { if (token.properties[0] == "int") { return(Expression.Constant(int.Parse(token.properties[1]))); } throw new NotSupportedException("This literal is not supported"); } throw new NotSupportedException("This exp type is not supported"); }
private bool TryGetSimpleWriteFromVar(AstToken token, out DeclRefExprProperties properties) { properties = null; switch (token.Type) { case AstType.DeclRefExpr when token.children.Count == 0: properties = new DeclRefExprProperties(token); return(true); case AstType.ImplicitCastExpr: foreach (var tokenChild in token.children) { if (TryGetSimpleWriteFromVar(tokenChild, out properties)) { return(true); } } break; default: break; } return(false); }
private static TypeDeclaration GetTypeDeclaration(AstToken token) { switch (token.Type) { case AstType.BuiltinType: return(new TypeDeclaration() { name = token.properties[0], isBuildIn = true }); case AstType.PointerType: var pointerDeclaration = GetTypeDeclaration(token.children[0]); pointerDeclaration.isPointer = true; return(pointerDeclaration); case AstType.RecordType: return(new TypeDeclaration() { name = token.properties[0] }); case AstType.ElaboratedType: // struct return(GetTypeDeclaration(token.children[0])); case AstType.TypedefType: return(new TypeDeclaration() { name = token.properties[0] }); case AstType.ParenType: switch (token.children[0].Type) { case AstType.FunctionProtoType: var functionDeclaration = GetFunctionProtoDeclaration(token.children[0]); functionDeclaration.name = token.properties[0]; functionDeclaration.isBuildIn = false; return(functionDeclaration); default: throw new NotImplementedException(token.children[0].unknownName); } case AstType.QualType: // type with modificator return(GetTypeDeclaration(token.children[0])); case AstType.EnumType: // type with modificator return(new TypeDeclaration() { name = token.properties[0] }); case AstType.NOT_PARSED when token.unknownName is "...": // params return(new TypeDeclaration() { name = token.unknownName }); case AstType.TemplateSpecializationType: return(null); // TODO default: throw new NotImplementedException(token.Type.ToString()); } }
public void VisitAstToken(AstToken token) { Formatter.StartNode(token); //Formatter.WriteToken(token.Value); Formatter.EndNode(); }
/* * // reference accessor (pInfo->wNpcID) SendCS_AUCTIONREG_REQ * | | | | | | | | `-ImplicitCastExpr 0x1f38c61d1e8 <line:2201:6, col:13> 'WORD':'unsigned short' <LValueToRValue> | | | | | | | | `-MemberExpr 0x1f38c61c578 <col:6, col:13> 'WORD':'unsigned short' lvalue ->wNpcID 0x1f38a41b8f0 | | | | | | | | `-ImplicitCastExpr 0x1f38c61c560 <col:6> 'LPTAUCTIONREGINFO':'struct tagTAUCTIONREGINFO *' <LValueToRValue> | | | | | | | | `-DeclRefExpr 0x1f38c61c540 <col:6> 'LPTAUCTIONREGINFO':'struct tagTAUCTIONREGINFO *' lvalue ParmVar 0x1f38c61c1a0 'pInfo' 'LPTAUCTIONREGINFO':'struct tagTAUCTIONREGINFO *' | | / Size Accessor | `-CXXFunctionalCastExpr 0x1f38c6b7060 <line:3008:6, col:25> 'BYTE':'unsigned char' functional cast to BYTE <NoOp> | | `-ImplicitCastExpr 0x1f38c6b7048 <col:11, col:24> 'BYTE':'unsigned char' <IntegralCast> part_of_explicit_cast | | `-CXXMemberCallExpr 0x1f38c6b6ff8 <col:11, col:24> 'std::vector<tagTSKILLTARGET *, std::allocator<tagTSKILLTARGET *>>::size_type':'unsigned long long' | | `-MemberExpr 0x1f38c6b6fc8 <col:11, col:19> '<bound member function type>' .size 0x1f38a7d6710 | | `-DeclRefExpr 0x1f38c6b6fa8 <col:11> 'const VTSKILLTARGET':'const std::vector<tagTSKILLTARGET *, std::allocator<tagTSKILLTARGET *>>' lvalue ParmVar 0x1f38c6ae888 'vTarget' 'const VTSKILLTARGET &' | | //DeclRefExpr | [0] "const VTSKILLTARGET':'const std::vector<tagTSKILLTARGET *, std::allocator<tagTSKILLTARGET *>>" string | [1] "lvalue" string | [2] "ParmVar" string | [3] "0x1f38c6ae888" string | [4] "vTarget" string | [5] "const VTSKILLTARGET &" string | | //MemberExpr | [0] "<bound member function type>" string | [1] ".size" string | [2] "0x1f38a7d6710" string | | //Parses size accessor like Vector.size() and converts it to Array.Length; */ private bool TryParseSizeAccessor(AstToken token) { var found = token.children.Select(TryParseSizeAccessor).FirstOrDefault(x => x == true); if (token.children.Any() && !found) { return(false); } switch (token.Type) { case AstType.CXXFunctionalCastExpr: case AstType.ImplicitCastExpr: break; case AstType.CXXMemberCallExpr: break; case AstType.MemberExpr: if (token.properties[1].EndsWith("size")) { //Console.Write("Length"); GetResultObject <SecondaryObjectPacketWriteEntry>().FieldName = "Length"; return(true); } var toWrite = token.properties[1] == "lvalue" ? token.properties[2] : token.properties[1]; //->wNpcID GetResultObject <SecondaryObjectPacketWriteEntry>().FieldName = toWrite.TrimStart("->"); Console.Write($" {toWrite}"); return(true); //Debugger.Break(); break; case AstType.DeclRefExpr: var vectorRegex = new Regex("std\\:\\:vector\\<(.*?) .*?\\,"); var matches = vectorRegex.Match(token.properties[0]); if (matches.Success) { //its a vector var structName = matches.Groups[1].Value.TrimStart("tag"); GetResultObject <SecondaryObjectPacketWriteEntry>().ObjectType = structName; GetResultObject <SecondaryObjectPacketWriteEntry>().IsArray = true; //GetResultObject<SecondaryObjectPacketWriteEntry>().ObjectName = match.Groups[2].Value; //Console.Write($"Vector found: {structName}"); } else { var structTypeRegex = new Regex("':'(.*?) (.*?) "); var match = structTypeRegex.Match(token.properties[0]); if (match.Success) { //struct found: tagTAUCTIONREGINFO //Console.Write($"{match.Groups[1].Value} found: {match.Groups[2].Value}"); GetResultObject <SecondaryObjectPacketWriteEntry>().ObjectType = match.Groups[1].Value; GetResultObject <SecondaryObjectPacketWriteEntry>().ObjectName = match.Groups[2].Value.TrimStart("tag"); } } //pInfo //Console.Write($" Name: {token.properties[4]}"); //Debugger.Break(); return(true); break; default: Debugger.Break(); return(found); } return(found); }
//packetShiftToken token.children[2] public InstructionVisitor(AstToken token) { _token = token; }
public static string GetFunctionBody(AstToken token) { string reference; System.Text.StringBuilder stringBuilder; switch (token.Type) { case AstType.CompoundStmt: stringBuilder = new System.Text.StringBuilder(); stringBuilder.AppendLine("{"); foreach (var childToken in token.children) { stringBuilder.AppendLine(GetFunctionBody(childToken)); } stringBuilder.AppendLine("}"); return(stringBuilder.ToString()); case AstType.ReturnStmt: if (token.children.Count > 1) { throw new ArgumentException(); } if (token.children.Count == 0) { return("return;"); } return("return " + GetFunctionBody(token.children[0]) + ";"); case AstType.ConditionalOperator: if (token.children.Count != 3) { throw new ArgumentException(); } return(GetFunctionBody(token.children[0]) + "?" + GetFunctionBody(token.children[1]) + ":" + GetFunctionBody(token.children[2])); case AstType.ParenExpr: if (token.children.Count != 1) { throw new ArgumentException(); } return("(" + GetFunctionBody(token.children[0]) + ")"); case AstType.BinaryOperator: return(GetFunctionBody(token.children[0]) + token.properties[token.properties.Length - 1] + GetFunctionBody(token.children[1])); case AstType.UnaryOperator: if (token.children.Count != 1) { throw new ArgumentException(); } var operation = token.properties[1]; if (operation == "lvalue") { operation = token.properties[2]; } if (operation == "prefix") { return(token.properties[token.properties.Length - 1] + GetFunctionBody(token.children[0])); } else if (operation == "postfix") { return(GetFunctionBody(token.children[0]) + token.properties[token.properties.Length - 1]); } else { throw new ArgumentException(); } case AstType.ImplicitCastExpr: if (token.children.Count != 1) { throw new ArgumentException(); } return(GetFunctionBody(token.children[0])); case AstType.DeclRefExpr: // variable reference = token.properties[1]; if (reference == "lvalue") { return(token.properties[4]); // remove quotes } return(token.properties[3]); // remove quotes case AstType.InitListExpr: stringBuilder = new System.Text.StringBuilder(); stringBuilder.Append("["); for (int i = 0; i < token.children.Count; i++) { if (i != 0) { stringBuilder.Append(","); } stringBuilder.Append(GetFunctionBody(token.children[i])); } stringBuilder.Append("]"); return(stringBuilder.ToString()); case AstType.IntegerLiteral: return(token.properties[1]); // property 0 is 'int'; if this one have context - this may be define case AstType.CharacterLiteral: return(token.properties[1]); // property 0 is 'char' case AstType.FloatingLiteral: return(token.properties[1]); // property 0 is 'float/double' case AstType.StringLiteral: reference = token.properties[1]; // property 0 is 'char[...]' if (reference == "lvalue") { return(token.properties[2]); } return(token.properties[1]); case AstType.CXXBoolLiteralExpr: return(token.properties[1]); // property 0 is 'bool' case AstType.CStyleCastExpr: if (token.children.Count != 1) { throw new ArgumentException(); } return(GetFunctionBody(token.children[0])); // maybe need cast with parameter[0] case AstType.MemberExpr: if (token.children.Count != 1) { throw new ArgumentException(); } reference = token.properties[1]; if (reference == "lvalue") { return(GetFunctionBody(token.children[0]) + token.properties[2]); } return(GetFunctionBody(token.children[0]) + token.properties[1]); case AstType.CallExpr: if (token.children.Count == 0) { throw new ArgumentException(); } stringBuilder = new System.Text.StringBuilder(); stringBuilder.Append(GetFunctionBody(token.children[0])); stringBuilder.Append("("); for (int i = 1; i < token.children.Count; i++) { if (token.children[i].Type == AstType.CXXDefaultArgExpr) { break; } if (i > 1) { stringBuilder.Append(","); } stringBuilder.Append(GetFunctionBody(token.children[i])); } stringBuilder.Append(")"); return(stringBuilder.ToString()); case AstType.ArraySubscriptExpr: if (token.children.Count != 2) { throw new ArgumentException(); } return(GetFunctionBody(token.children[0]) + "[" + GetFunctionBody(token.children[1]) + "]"); case AstType.DeclStmt: stringBuilder = new System.Text.StringBuilder(); foreach (var childToken in token.children) { stringBuilder.AppendLine(GetFunctionBody(childToken)); } return(stringBuilder.ToString()); case AstType.VarDecl: if (token.properties.Length != 2) { throw new ArgumentException(); } stringBuilder = new System.Text.StringBuilder(); stringBuilder.Append(token.properties[1]); stringBuilder.Append(" "); stringBuilder.Append(token.properties[0]); if (token.Attributes.HasFlag(AstAttributes.CInit)) { if (token.children.Count != 1) { token.children = token.children.Where((tok, index) => tok.Type != AstType.FullComment).ToList(); if (token.children.Count != 1) { throw new ArgumentException(); } } stringBuilder.Append(" = "); stringBuilder.Append(GetFunctionBody(token.children[0])); } return(stringBuilder.ToString() + ";"); case AstType.WhileStmt: if (token.children.Count != 3) { throw new ArgumentException(); } return("while(" + GetFunctionBody(token.children[1]) + ")\n" + GetFunctionBody(token.children[2])); case AstType.DoStmt: if (token.children.Count != 2) { throw new ArgumentException(); } return("do" + GetFunctionBody(token.children[0]) + "\nwhile(" + GetFunctionBody(token.children[1]) + ");"); case AstType.IfStmt: if (token.children.Count != 5) { throw new ArgumentException(); } return("if(" + GetFunctionBody(token.children[2]) + ")\n" + GetFunctionBody(token.children[3])); case AstType.ForStmt: if (token.children.Count != 5) { throw new ArgumentException(); } return("for(" + GetFunctionBody(token.children[0]) + ";" + GetFunctionBody(token.children[2]) + ";" + GetFunctionBody(token.children[3]) + ")\n" + GetFunctionBody(token.children[4])); case AstType.GNUNullExpr: return("null"); case AstType.NullStmt: return(";"); case AstType.CompoundAssignOperator: if (token.children.Count != 2) { throw new ArgumentException(); } reference = token.properties[1]; if (reference == "lvalue") { return(GetFunctionBody(token.children[0]) + token.properties[2] + GetFunctionBody(token.children[1])); } return(GetFunctionBody(token.children[0]) + token.properties[1] + GetFunctionBody(token.children[1])); case AstType.UnaryExprOrTypeTraitExpr: if (token.properties.Length == 3) { return(token.properties[1] + "(" + token.properties[2] + ")"); } if (token.children.Count != 1) { throw new ArgumentException(); } return(token.properties[1] + GetFunctionBody(token.children[0])); case AstType.BreakStmt: return("break;"); case AstType.ContinueStmt: return("continue;"); case AstType.GotoStmt: return("goto " + token.properties[0] + ";"); case AstType.LabelStmt: stringBuilder = new System.Text.StringBuilder(); stringBuilder.Append(token.properties[0]); stringBuilder.AppendLine(":"); foreach (var childToken in token.children) { stringBuilder.AppendLine(GetFunctionBody(childToken)); } return(stringBuilder.ToString()); case AstType.ExprWithCleanups: // i not sure about this one if (token.children.Count != 1) { throw new ArgumentException(); } return(GetFunctionBody(token.children[0])); case AstType.CXXConstructExpr: // i not sure about this one if (token.children.Count != 1) { throw new ArgumentException(); } return(GetFunctionBody(token.children[0])); case AstType.MaterializeTemporaryExpr: // i not sure about this one if (token.children.Count != 1) { throw new ArgumentException(); } return(GetFunctionBody(token.children[0])); case AstType.CXXMemberCallExpr: // i not sure about this one stringBuilder = new System.Text.StringBuilder(); stringBuilder.Append(GetFunctionBody(token.children[0])); stringBuilder.Append("("); for (int i = 1; i < token.children.Count; i++) { //if (token.children[i].name == "CXXDefaultArgExpr") break; if (i > 1) { stringBuilder.Append(","); } stringBuilder.Append(GetFunctionBody(token.children[i])); } stringBuilder.Append(")"); return(stringBuilder.ToString()); case AstType.CXXOperatorCallExpr: // i not sure about this one if (token.children.Count == 3) { return(GetFunctionBody(token.children[1]) + GetFunctionBody(token.children[0]) + GetFunctionBody(token.children[2])); } else if (token.children.Count == 2) { return(GetFunctionBody(token.children[1]) + GetFunctionBody(token.children[0])); } else { throw new ArgumentException(); } case AstType.ImplicitValueInitExpr: // don't know what to do if (token.children.Count != 0) { throw new ArgumentException(); } return(""); case AstType.SwitchStmt: if (token.children.Count != 4) { throw new ArgumentException(); } stringBuilder = new System.Text.StringBuilder(); stringBuilder.Append("switch("); stringBuilder.Append(GetFunctionBody(token.children[2])); stringBuilder.Append(")"); stringBuilder.Append(GetFunctionBody(token.children[3])); return(stringBuilder.ToString()); case AstType.CaseStmt: if (token.children.Count != 3) { throw new ArgumentException(); } stringBuilder = new System.Text.StringBuilder(); stringBuilder.Append("case:"); stringBuilder.AppendLine(GetFunctionBody(token.children[0])); stringBuilder.Append(GetFunctionBody(token.children[2])); return(stringBuilder.ToString()); case AstType.DefaultStmt: stringBuilder = new System.Text.StringBuilder(); for (int i = 0; i < token.children.Count; i++) { stringBuilder.Append(GetFunctionBody(token.children[i])); } return(stringBuilder.ToString()); case AstType.ArrayType: return(""); // no idea what to do... case AstType.CxxCastExpr: if (token.children.Count != 1) { throw new ArgumentException(); } return(GetFunctionBody(token.children[0])); case AstType.CXXFunctionalCastExpr: // i not sure it works right return("(" + token.properties[0] + ")"); case AstType.PredefinedExpr: // i not sure it works right operation = token.properties[1]; if (operation == "lvalue") { operation = token.properties[2]; } return(operation); case AstType.NULL: return(""); case AstType.GCCAsmStmt: stringBuilder = new System.Text.StringBuilder(); for (int i = 0; i < token.children.Count; i++) { stringBuilder.Append(GetFunctionBody(token.children[i])); } return(stringBuilder.ToString()); case AstType.VAArgExpr: // i not sure it right return("...args"); default: throw new Exception(token.unknownName + " " + token.Type.ToString()); //default: return "Unknown"; } }
private static IEnumerable <AstToken> GetUsages(DeclRefExprProperties saidMsg, AstToken methodDecl) { return(methodDecl.VisitEnumerable ( x => x.unknownName == "DeclRefExpr" && new DeclRefExprProperties(x).Equals(saidMsg) )); }
private static void ProcessMethod(HashSet <string> forbidden, AstToken methodDecl) { // var meth = JsonConvert.SerializeObject(methodDecl.AsTokenDto(),Formatting.Indented); var methodName = methodDecl.properties.FirstOrDefault(); Console.WriteLine($"Name: {methodName}"); if (methodName == "SendCS_FINISHSKILL_ACK") { Debugger.Break(); } var saidMsg = GetVariableThatGetsSaid(methodDecl).FirstOrDefault(); if (saidMsg == null) { Console.WriteLine("\tNo message said"); return; } var usages = GetUsages(saidMsg, methodDecl) // .Where(x => x.TraverseParents(true).Any(m => m.name == "MemberExpr" && m.properties.Contains(".SetID"))) .ToList(); foreach (var usage in usages) { //var allParents = usage.parent.TraverseParents().ToList(); var parentsInMethod = EnumerableExtensions.TakeWhile(usage.parent .TraverseParents(), x => x != methodDecl) .ToList(); var goodParents = parentsInMethod .Where(x => x.unknownName == "CXXOperatorCallExpr") .Where(x => x.properties.Length == 3 && (x.properties[2] == "<<" || x.properties[2] == "'<<'")) .Where(x => x.children.Count == 3) .ToList(); foreach (var parent in goodParents) { var decl = parent.children[2].VisitEnumerable(x => x.unknownName == "DeclRefExpr").FirstOrDefault(); if (decl == null) { continue; } var properties = new DeclRefExprProperties(decl); Console.WriteLine($"\t[{properties.InstanceId}] {properties.Name} ({properties.Type})"); var illegalOperation = EnumerableExtensions.TakeWhile(decl.TraverseParents(), x => x != methodDecl).FirstOrDefault(x => forbidden.Contains(x.unknownName)); if (illegalOperation != null) { Console.WriteLine($"\tIllegal operation - {illegalOperation.unknownName}"); DebugNodes(EnumerableExtensions.TakeWhile(decl.TraverseParents(), x => x != methodDecl)); Console.WriteLine(); } } } //var found = new List<AstToken>(); //methodDecl.Visit //( // x => (x.name == "DeclRefExpr" && x.properties[2] == "ParmVar"), // x => found.Add(x) //); //ProcessSetInstruction(methodDecl); //var compounts = found.Select(x => new //{ // Name = x.properties[4], // Type = x.properties[5], // Ast = x //}).ToList(); //foreach (var compount in compounts) //{ // var illegalStatement = compount.Ast.TraverseParents().FirstOrDefault(x => forbidden.Contains(x.name)); // var addition = illegalStatement != null ? $"- Illegal statement found: {illegalStatement.name}" : string.Empty; // Console.WriteLine($"\t{compount.Name} - {compount.Type} {addition}"); // if (addition != string.Empty) // { // //foreach (var cAst in compount.Ast.TraverseParents()) // //{ // // try // // { // // Console.WriteLine(); // // } // // catch (Exception e) // // { // // } // //} // Console.WriteLine(); // Debugger.Break(); // } //} }
public void PutBack(AstToken token) { _bufferIndex++; _tokenBuffer.Add(token); }
private StructureDeclaration GetStructDeclaration(AstToken token) { StructureDeclaration structure = new StructureDeclaration(); if (token.properties[0] == "union") { structure.isUnion = true; } else if (token.properties[0] == "class") { structure.isClass = true; } else if (token.properties[0] != "struct") { throw new ArgumentException(); } if (token.properties.Length > 1) { structure.name = token.properties[1]; } foreach (var childToken in token.children) { switch (childToken.Type) { case AstType.DefinitionData: // almost no idea how to parse it // TODO break; case AstType.CXXRecordDecl: structure.subStructures.Add(GetStructDeclaration(childToken)); break; case AstType.FieldDecl: structure.properties.Add(new StructureDeclaration.Property() { name = childToken.properties[0], type = childToken.properties[1] }); break; case AstType.Public: // TODO break; case AstType.FullComment: // ignore this. or TODO if you wish break; case AstType.AccessSpecDecl: // TODO local modificator based on properties[0] break; case AstType.CXXConstructorDecl: // TODO break; case AstType.CXXDestructorDecl: // TODO break; case AstType.CXXMethodDecl: // TODO structure.others.Add(childToken.properties[0] + " " + childToken.properties[1]); break; default: throw new Exception(childToken.unknownName); } } return(structure); }
public static string SerializeFriendly(this AstToken token) { return(JsonConvert.SerializeObject(token.AsTokenDto(), Formatting.Indented)); }
public abstract bool OnNodeParsed(AstFileReaderContext readerContext, AstToken token, int depth);