using System; using System.Collections.Generic; using System.IO; using System.Linq; using Ryujinx.HLE.HOS.Diagnostics.Demangler.Ast; namespace Ryujinx.HLE.HOS.Diagnostics.Demangler { class Demangler { private static readonly string BASE_36 = "0123456789abcdefghijklmnopqrstuvwxyz"; private List SubstitutionList = new List(); private List TemplateParamList = new List(); private List ForwardTemplateReferenceList = new List(); public string Mangled { get; private set; } private int Position; private int Length; private bool CanForwardTemplateReference; private bool CanParseTemplateArgs; public Demangler(string Mangled) { this.Mangled = Mangled; Position = 0; Length = Mangled.Length; CanParseTemplateArgs = true; } private bool ConsumeIf(string ToConsume) { string MangledPart = Mangled.Substring(Position); if (MangledPart.StartsWith(ToConsume)) { Position += ToConsume.Length; return true; } return false; } private string PeekString(int Offset = 0, int Length = 1) { if (Position + Offset >= Length) { return null; } return Mangled.Substring(Position + Offset, Length); } private char Peek(int Offset = 0) { if (Position + Offset >= Length) { return '\0'; } return Mangled[Position + Offset]; } private char Consume() { if (Position < Length) { return Mangled[Position++]; } return '\0'; } private int Count() { return Length - Position; } private static int FromBase36(string Encoded) { char[] ReversedEncoded = Encoded.ToLower().ToCharArray().Reverse().ToArray(); int Result = 0; for (int i = 0; i < ReversedEncoded.Length; i++) { int Value = BASE_36.IndexOf(ReversedEncoded[i]); if (Value == -1) { return -1; } Result += Value * (int)Math.Pow(36, i); } return Result; } private int ParseSeqId() { string Part = Mangled.Substring(Position); int SeqIdLen = 0; for (; SeqIdLen < Part.Length; SeqIdLen++) { if (!char.IsLetterOrDigit(Part[SeqIdLen])) { break; } } Position += SeqIdLen; return FromBase36(Part.Substring(0, SeqIdLen)); } // ::= S _ // ::= S_ // ::= St # std:: // ::= Sa # std::allocator // ::= Sb # std::basic_string // ::= Ss # std::basic_string, std::allocator > // ::= Si # std::basic_istream > // ::= So # std::basic_ostream > // ::= Sd # std::basic_iostream > private BaseNode ParseSubstitution() { if (!ConsumeIf("S")) { return null; } char SubstitutionSecondChar = Peek(); if (char.IsLower(SubstitutionSecondChar)) { switch (SubstitutionSecondChar) { case 'a': Position++; return new SpecialSubstitution(SpecialSubstitution.SpecialType.Allocator); case 'b': Position++; return new SpecialSubstitution(SpecialSubstitution.SpecialType.BasicString); case 's': Position++; return new SpecialSubstitution(SpecialSubstitution.SpecialType.String); case 'i': Position++; return new SpecialSubstitution(SpecialSubstitution.SpecialType.IStream); case 'o': Position++; return new SpecialSubstitution(SpecialSubstitution.SpecialType.OStream); case 'd': Position++; return new SpecialSubstitution(SpecialSubstitution.SpecialType.IOStream); default: return null; } } // ::= S_ if (ConsumeIf("_")) { if (SubstitutionList.Count != 0) { return SubstitutionList[0]; } return null; } // ::= S _ int SeqId = ParseSeqId(); if (SeqId < 0) { return null; } SeqId++; if (!ConsumeIf("_") || SeqId >= SubstitutionList.Count) { return null; } return SubstitutionList[SeqId]; } // NOTE: thoses data aren't used in the output // ::= h _ // ::= v _ // ::= // # non-virtual base override // ::= _ // # virtual base override, with vcall offset private bool ParseCallOffset() { if (ConsumeIf("h")) { return ParseNumber(true).Length == 0 || !ConsumeIf("_"); } else if (ConsumeIf("v")) { return ParseNumber(true).Length == 0 || !ConsumeIf("_") || ParseNumber(true).Length == 0 || !ConsumeIf("_"); } return true; } // ::= # non-dependent type name, dependent type name, or dependent typename-specifier // ::= Ts # dependent elaborated type specifier using 'struct' or 'class' // ::= Tu # dependent elaborated type specifier using 'union' // ::= Te # dependent elaborated type specifier using 'enum' private BaseNode ParseClassEnumType() { string ElaboratedType = null; if (ConsumeIf("Ts")) { ElaboratedType = "struct"; } else if (ConsumeIf("Tu")) { ElaboratedType = "union"; } else if (ConsumeIf("Te")) { ElaboratedType = "enum"; } BaseNode Name = ParseName(); if (Name == null) { return null; } if (ElaboratedType == null) { return Name; } return new ElaboratedType(ElaboratedType, Name); } // ::= [] [] [Dx] F [Y] [] E // ::= + // # types are possible return type, then parameter types // ::= Do # non-throwing exception-specification (e.g., noexcept, throw()) // ::= DO E # computed (instantiation-dependent) noexcept // ::= Dw + E # dynamic exception specification with instantiation-dependent types private BaseNode ParseFunctionType() { CV CVQualifiers = ParseCVQualifiers(); BaseNode ExceptionSpec = null; if (ConsumeIf("Do")) { ExceptionSpec = new NameType("noexcept"); } else if (ConsumeIf("DO")) { BaseNode Expression = ParseExpression(); if (Expression == null || !ConsumeIf("E")) { return null; } ExceptionSpec = new NoexceptSpec(Expression); } else if (ConsumeIf("Dw")) { List Types = new List(); while (!ConsumeIf("E")) { BaseNode Type = ParseType(); if (Type == null) { return null; } Types.Add(Type); } ExceptionSpec = new DynamicExceptionSpec(new NodeArray(Types)); } // We don't need the transaction ConsumeIf("Dx"); if (!ConsumeIf("F")) { return null; } // extern "C" ConsumeIf("Y"); BaseNode ReturnType = ParseType(); if (ReturnType == null) { return null; } Reference ReferenceQualifier = Reference.None; List Params = new List(); while (true) { if (ConsumeIf("E")) { break; } if (ConsumeIf("v")) { continue; } if (ConsumeIf("RE")) { ReferenceQualifier = Reference.LValue; break; } else if (ConsumeIf("OE")) { ReferenceQualifier = Reference.RValue; break; } BaseNode Type = ParseType(); if (Type == null) { return null; } Params.Add(Type); } return new FunctionType(ReturnType, new NodeArray(Params), new CVType(CVQualifiers, null), new SimpleReferenceType(ReferenceQualifier, null), ExceptionSpec); } // ::= A _ // ::= A [] _ private BaseNode ParseArrayType() { if (!ConsumeIf("A")) { return null; } BaseNode ElementType; if (char.IsDigit(Peek())) { string Dimension = ParseNumber(); if (Dimension.Length == 0 || !ConsumeIf("_")) { return null; } ElementType = ParseType(); if (ElementType == null) { return null; } return new ArrayType(ElementType, Dimension); } if (!ConsumeIf("_")) { BaseNode DimensionExpression = ParseExpression(); if (DimensionExpression == null || !ConsumeIf("_")) { return null; } ElementType = ParseType(); if (ElementType == null) { return null; } return new ArrayType(ElementType, DimensionExpression); } ElementType = ParseType(); if (ElementType == null) { return null; } return new ArrayType(ElementType); } // ::= // ::= (PARTIAL) // ::= // ::= // ::= (TODO) // ::= (TODO) // ::= // ::= // ::= // ::= P # pointer // ::= R # l-value reference // ::= O # r-value reference (C++11) // ::= C # complex pair (C99) // ::= G # imaginary (C99) // ::= # See Compression below private BaseNode ParseType(NameParserContext Context = null) { // Temporary context if (Context == null) { Context = new NameParserContext(); } BaseNode Result = null; switch (Peek()) { case 'r': case 'V': case 'K': int TypePos = 0; if (Peek(TypePos) == 'r') { TypePos++; } if (Peek(TypePos) == 'V') { TypePos++; } if (Peek(TypePos) == 'K') { TypePos++; } if (Peek(TypePos) == 'F' || (Peek(TypePos) == 'D' && (Peek(TypePos + 1) == 'o' || Peek(TypePos + 1) == 'O' || Peek(TypePos + 1) == 'w' || Peek(TypePos + 1) == 'x'))) { Result = ParseFunctionType(); break; } CV CV = ParseCVQualifiers(); Result = ParseType(Context); if (Result == null) { return null; } Result = new CVType(CV, Result); break; case 'U': // TODO: return null; case 'v': Position++; return new NameType("void"); case 'w': Position++; return new NameType("wchar_t"); case 'b': Position++; return new NameType("bool"); case 'c': Position++; return new NameType("char"); case 'a': Position++; return new NameType("signed char"); case 'h': Position++; return new NameType("unsigned char"); case 's': Position++; return new NameType("short"); case 't': Position++; return new NameType("unsigned short"); case 'i': Position++; return new NameType("int"); case 'j': Position++; return new NameType("unsigned int"); case 'l': Position++; return new NameType("long"); case 'm': Position++; return new NameType("unsigned long"); case 'x': Position++; return new NameType("long long"); case 'y': Position++; return new NameType("unsigned long long"); case 'n': Position++; return new NameType("__int128"); case 'o': Position++; return new NameType("unsigned __int128"); case 'f': Position++; return new NameType("float"); case 'd': Position++; return new NameType("double"); case 'e': Position++; return new NameType("long double"); case 'g': Position++; return new NameType("__float128"); case 'z': Position++; return new NameType("..."); case 'u': Position++; return ParseSourceName(); case 'D': switch (Peek(1)) { case 'd': Position += 2; return new NameType("decimal64"); case 'e': Position += 2; return new NameType("decimal128"); case 'f': Position += 2; return new NameType("decimal32"); case 'h': Position += 2; // FIXME: GNU c++flit returns this but that is not what is supposed to be returned. return new NameType("half"); //return new NameType("decimal16"); case 'i': Position += 2; return new NameType("char32_t"); case 's': Position += 2; return new NameType("char16_t"); case 'a': Position += 2; return new NameType("decltype(auto)"); case 'n': Position += 2; // FIXME: GNU c++flit returns this but that is not what is supposed to be returned. return new NameType("decltype(nullptr)"); //return new NameType("std::nullptr_t"); case 't': case 'T': Position += 2; Result = ParseDecltype(); break; case 'o': case 'O': case 'w': case 'x': Result = ParseFunctionType(); break; default: return null; } break; case 'F': Result = ParseFunctionType(); break; case 'A': return ParseArrayType(); case 'M': // TODO: Position++; return null; case 'T': // might just be a class enum type if (Peek(1) == 's' || Peek(1) == 'u' || Peek(1) == 'e') { Result = ParseClassEnumType(); break; } Result = ParseTemplateParam(); if (Result == null) { return null; } if (CanParseTemplateArgs && Peek() == 'I') { BaseNode TemplateArguments = ParseTemplateArguments(); if (TemplateArguments == null) { return null; } Result = new NameTypeWithTemplateArguments(Result, TemplateArguments); } break; case 'P': Position++; Result = ParseType(Context); if (Result == null) { return null; } Result = new PointerType(Result); break; case 'R': Position++; Result = ParseType(Context); if (Result == null) { return null; } Result = new ReferenceType("&", Result); break; case 'O': Position++; Result = ParseType(Context); if (Result == null) { return null; } Result = new ReferenceType("&&", Result); break; case 'C': Position++; Result = ParseType(Context); if (Result == null) { return null; } Result = new PostfixQualifiedType(" complex", Result); break; case 'G': Position++; Result = ParseType(Context); if (Result == null) { return null; } Result = new PostfixQualifiedType(" imaginary", Result); break; case 'S': if (Peek(1) != 't') { BaseNode Substitution = ParseSubstitution(); if (Substitution == null) { return null; } if (CanParseTemplateArgs && Peek() == 'I') { BaseNode TemplateArgument = ParseTemplateArgument(); if (TemplateArgument == null) { return null; } Result = new NameTypeWithTemplateArguments(Substitution, TemplateArgument); break; } return Substitution; } else { Result = ParseClassEnumType(); break; } default: Result = ParseClassEnumType(); break; } if (Result != null) { SubstitutionList.Add(Result); } return Result; } // ::= TV # virtual table // ::= TT # VTT structure (construction vtable index) // ::= TI # typeinfo structure // ::= TS # typeinfo name (null-terminated byte string) // ::= Tc // ::= TW # Thread-local wrapper // ::= TH # Thread-local initialization // ::= T // # base is the nominal target function of thunk // ::= GV # Guard variable for one-time initialization private BaseNode ParseSpecialName(NameParserContext Context = null) { if (Peek() != 'T') { if (ConsumeIf("GV")) { BaseNode Name = ParseName(); if (Name == null) { return null; } return new SpecialName("guard variable for ", Name); } return null; } BaseNode Node; switch (Peek(1)) { // ::= TV # virtual table case 'V': Position += 2; Node = ParseType(Context); if (Node == null) { return null; } return new SpecialName("vtable for ", Node); // ::= TT # VTT structure (construction vtable index) case 'T': Position += 2; Node = ParseType(Context); if (Node == null) { return null; } return new SpecialName("VTT for ", Node); // ::= TI # typeinfo structure case 'I': Position += 2; Node = ParseType(Context); if (Node == null) { return null; } return new SpecialName("typeinfo for ", Node); // ::= TS # typeinfo name (null-terminated byte string) case 'S': Position += 2; Node = ParseType(Context); if (Node == null) { return null; } return new SpecialName("typeinfo name for ", Node); // ::= Tc case 'c': Position += 2; if (ParseCallOffset() || ParseCallOffset()) { return null; } Node = ParseEncoding(); if (Node == null) { return null; } return new SpecialName("covariant return thunk to ", Node); // extension ::= TC _ case 'C': Position += 2; BaseNode FirstType = ParseType(); if (FirstType == null || ParseNumber(true).Length == 0 || !ConsumeIf("_")) { return null; } BaseNode SecondType = ParseType(); return new CtorVtableSpecialName(SecondType, FirstType); // ::= TH # Thread-local initialization case 'H': Position += 2; Node = ParseName(); if (Node == null) { return null; } return new SpecialName("thread-local initialization routine for ", Node); // ::= TW # Thread-local wrapper case 'W': Position += 2; Node = ParseName(); if (Node == null) { return null; } return new SpecialName("thread-local wrapper routine for ", Node); default: Position++; bool IsVirtual = Peek() == 'v'; if (ParseCallOffset()) { return null; } Node = ParseEncoding(); if (Node == null) { return null; } if (IsVirtual) { return new SpecialName("virtual thunk to ", Node); } return new SpecialName("non-virtual thunk to ", Node); } } // ::= [r] [V] [K] # restrict (C99), volatile, const private CV ParseCVQualifiers() { CV Qualifiers = CV.None; if (ConsumeIf("r")) { Qualifiers |= CV.Restricted; } if (ConsumeIf("V")) { Qualifiers |= CV.Volatile; } if (ConsumeIf("K")) { Qualifiers |= CV.Const; } return Qualifiers; } // ::= R # & ref-qualifier // ::= O # && ref-qualifier private SimpleReferenceType ParseRefQualifiers() { Reference Result = Reference.None; if (ConsumeIf("O")) { Result = Reference.RValue; } else if (ConsumeIf("R")) { Result = Reference.LValue; } return new SimpleReferenceType(Result, null); } private BaseNode CreateNameNode(BaseNode Prev, BaseNode Name, NameParserContext Context) { BaseNode Result = Name; if (Prev != null) { Result = new NestedName(Name, Prev); } if (Context != null) { Context.FinishWithTemplateArguments = false; } return Result; } private int ParsePositiveNumber() { string Part = Mangled.Substring(Position); int NumberLength = 0; for (; NumberLength < Part.Length; NumberLength++) { if (!char.IsDigit(Part[NumberLength])) { break; } } Position += NumberLength; if (NumberLength == 0) { return -1; } return int.Parse(Part.Substring(0, NumberLength)); } private string ParseNumber(bool IsSigned = false) { if (IsSigned) { ConsumeIf("n"); } if (Count() == 0 || !char.IsDigit(Mangled[Position])) { return null; } string Part = Mangled.Substring(Position); int NumberLength = 0; for (; NumberLength < Part.Length; NumberLength++) { if (!char.IsDigit(Part[NumberLength])) { break; } } Position += NumberLength; return Part.Substring(0, NumberLength); } // ::= private BaseNode ParseSourceName() { int Length = ParsePositiveNumber(); if (Count() < Length || Length <= 0) { return null; } string Name = Mangled.Substring(Position, Length); Position += Length; if (Name.StartsWith("_GLOBAL__N")) { return new NameType("(anonymous namespace)"); } return new NameType(Name); } // ::= nw # new // ::= na # new[] // ::= dl # delete // ::= da # delete[] // ::= ps # + (unary) // ::= ng # - (unary) // ::= ad # & (unary) // ::= de # * (unary) // ::= co # ~ // ::= pl # + // ::= mi # - // ::= ml # * // ::= dv # / // ::= rm # % // ::= an # & // ::= or # | // ::= eo # ^ // ::= aS # = // ::= pL # += // ::= mI # -= // ::= mL # *= // ::= dV # /= // ::= rM # %= // ::= aN # &= // ::= oR # |= // ::= eO # ^= // ::= ls # << // ::= rs # >> // ::= lS # <<= // ::= rS # >>= // ::= eq # == // ::= ne # != // ::= lt # < // ::= gt # > // ::= le # <= // ::= ge # >= // ::= ss # <=> // ::= nt # ! // ::= aa # && // ::= oo # || // ::= pp # ++ (postfix in context) // ::= mm # -- (postfix in context) // ::= cm # , // ::= pm # ->* // ::= pt # -> // ::= cl # () // ::= ix # [] // ::= qu # ? // ::= cv # (cast) (TODO) // ::= li # operator "" // ::= v # vendor extended operator (TODO) private BaseNode ParseOperatorName(NameParserContext Context) { switch (Peek()) { case 'a': switch (Peek(1)) { case 'a': Position += 2; return new NameType("operator&&"); case 'd': case 'n': Position += 2; return new NameType("operator&"); case 'N': Position += 2; return new NameType("operator&="); case 'S': Position += 2; return new NameType("operator="); default: return null; } case 'c': switch (Peek(1)) { case 'l': Position += 2; return new NameType("operator()"); case 'm': Position += 2; return new NameType("operator,"); case 'o': Position += 2; return new NameType("operator~"); case 'v': Position += 2; bool CanParseTemplateArgsBackup = CanParseTemplateArgs; bool CanForwardTemplateReferenceBackup = CanForwardTemplateReference; CanParseTemplateArgs = false; CanForwardTemplateReference = CanForwardTemplateReferenceBackup || Context != null; BaseNode Type = ParseType(); CanParseTemplateArgs = CanParseTemplateArgsBackup; CanForwardTemplateReference = CanForwardTemplateReferenceBackup; if (Type == null) { return null; } if (Context != null) { Context.CtorDtorConversion = true; } return new ConversionOperatorType(Type); default: return null; } case 'd': switch (Peek(1)) { case 'a': Position += 2; return new NameType("operator delete[]"); case 'e': Position += 2; return new NameType("operator*"); case 'l': Position += 2; return new NameType("operator delete"); case 'v': Position += 2; return new NameType("operator/"); case 'V': Position += 2; return new NameType("operator/="); default: return null; } case 'e': switch (Peek(1)) { case 'o': Position += 2; return new NameType("operator^"); case 'O': Position += 2; return new NameType("operator^="); case 'q': Position += 2; return new NameType("operator=="); default: return null; } case 'g': switch (Peek(1)) { case 'e': Position += 2; return new NameType("operator>="); case 't': Position += 2; return new NameType("operator>"); default: return null; } case 'i': if (Peek(1) == 'x') { Position += 2; return new NameType("operator[]"); } return null; case 'l': switch (Peek(1)) { case 'e': Position += 2; return new NameType("operator<="); case 'i': Position += 2; BaseNode SourceName = ParseSourceName(); if (SourceName == null) { return null; } return new LiteralOperator(SourceName); case 's': Position += 2; return new NameType("operator<<"); case 'S': Position += 2; return new NameType("operator<<="); case 't': Position += 2; return new NameType("operator<"); default: return null; } case 'm': switch (Peek(1)) { case 'i': Position += 2; return new NameType("operator-"); case 'I': Position += 2; return new NameType("operator-="); case 'l': Position += 2; return new NameType("operator*"); case 'L': Position += 2; return new NameType("operator*="); case 'm': Position += 2; return new NameType("operator--"); default: return null; } case 'n': switch (Peek(1)) { case 'a': Position += 2; return new NameType("operator new[]"); case 'e': Position += 2; return new NameType("operator!="); case 'g': Position += 2; return new NameType("operator-"); case 't': Position += 2; return new NameType("operator!"); case 'w': Position += 2; return new NameType("operator new"); default: return null; } case 'o': switch (Peek(1)) { case 'o': Position += 2; return new NameType("operator||"); case 'r': Position += 2; return new NameType("operator|"); case 'R': Position += 2; return new NameType("operator|="); default: return null; } case 'p': switch (Peek(1)) { case 'm': Position += 2; return new NameType("operator->*"); case 's': case 'l': Position += 2; return new NameType("operator+"); case 'L': Position += 2; return new NameType("operator+="); case 'p': Position += 2; return new NameType("operator++"); case 't': Position += 2; return new NameType("operator->"); default: return null; } case 'q': if (Peek(1) == 'u') { Position += 2; return new NameType("operator?"); } return null; case 'r': switch (Peek(1)) { case 'm': Position += 2; return new NameType("operator%"); case 'M': Position += 2; return new NameType("operator%="); case 's': Position += 2; return new NameType("operator>>"); case 'S': Position += 2; return new NameType("operator>>="); default: return null; } case 's': if (Peek(1) == 's') { Position += 2; return new NameType("operator<=>"); } return null; case 'v': // TODO: ::= v # vendor extended operator return null; default: return null; } } // ::= [ (TODO)] // ::= (TODO) // ::= // ::= (TODO) // ::= DC + E # structured binding declaration (TODO) private BaseNode ParseUnqualifiedName(NameParserContext Context) { BaseNode Result = null; char C = Peek(); if (C == 'U') { // TODO: Unnamed Type Name // throw new Exception("Unnamed Type Name not implemented"); } else if (char.IsDigit(C)) { Result = ParseSourceName(); } else if (ConsumeIf("DC")) { // TODO: Structured Binding Declaration // throw new Exception("Structured Binding Declaration not implemented"); } else { Result = ParseOperatorName(Context); } if (Result != null) { // TODO: ABI Tags //throw new Exception("ABI Tags not implemented"); } return Result; } // ::= C1 # complete object constructor // ::= C2 # base object constructor // ::= C3 # complete object allocating constructor // ::= D0 # deleting destructor // ::= D1 # complete object destructor // ::= D2 # base object destructor private BaseNode ParseCtorDtorName(NameParserContext Context, BaseNode Prev) { if (Prev.Type == NodeType.SpecialSubstitution && Prev is SpecialSubstitution) { ((SpecialSubstitution)Prev).SetExtended(); } if (ConsumeIf("C")) { bool IsInherited = ConsumeIf("I"); char CtorDtorType = Peek(); if (CtorDtorType != '1' && CtorDtorType != '2' && CtorDtorType != '3') { return null; } Position++; if (Context != null) { Context.CtorDtorConversion = true; } if (IsInherited && ParseName(Context) == null) { return null; } return new CtorDtorNameType(Prev, false); } if (ConsumeIf("D")) { char C = Peek(); if (C != '0' && C != '1' && C != '2') { return null; } Position++; if (Context != null) { Context.CtorDtorConversion = true; } return new CtorDtorNameType(Prev, true); } return null; } // ::= fp _ # L == 0, first parameter // ::= fp _ # L == 0, second and later parameters // ::= fL p _ # L > 0, first parameter // ::= fL p _ # L > 0, second and later parameters private BaseNode ParseFunctionParameter() { if (ConsumeIf("fp")) { // ignored ParseCVQualifiers(); if (!ConsumeIf("_")) { return null; } return new FunctionParameter(ParseNumber()); } else if (ConsumeIf("fL")) { string L1Number = ParseNumber(); if (L1Number == null || L1Number.Length == 0) { return null; } if (!ConsumeIf("p")) { return null; } // ignored ParseCVQualifiers(); if (!ConsumeIf("_")) { return null; } return new FunctionParameter(ParseNumber()); } return null; } // ::= fL // ::= fR // ::= fl // ::= fr private BaseNode ParseFoldExpression() { if (!ConsumeIf("f")) { return null; } char FoldKind = Peek(); bool HasInitializer = FoldKind == 'L' || FoldKind == 'R'; bool IsLeftFold = FoldKind == 'l' || FoldKind == 'L'; if (!IsLeftFold && !(FoldKind == 'r' || FoldKind == 'R')) { return null; } Position++; string OperatorName = null; switch (PeekString(0, 2)) { case "aa": OperatorName = "&&"; break; case "an": OperatorName = "&"; break; case "aN": OperatorName = "&="; break; case "aS": OperatorName = "="; break; case "cm": OperatorName = ","; break; case "ds": OperatorName = ".*"; break; case "dv": OperatorName = "/"; break; case "dV": OperatorName = "/="; break; case "eo": OperatorName = "^"; break; case "eO": OperatorName = "^="; break; case "eq": OperatorName = "=="; break; case "ge": OperatorName = ">="; break; case "gt": OperatorName = ">"; break; case "le": OperatorName = "<="; break; case "ls": OperatorName = "<<"; break; case "lS": OperatorName = "<<="; break; case "lt": OperatorName = "<"; break; case "mi": OperatorName = "-"; break; case "mI": OperatorName = "-="; break; case "ml": OperatorName = "*"; break; case "mL": OperatorName = "*="; break; case "ne": OperatorName = "!="; break; case "oo": OperatorName = "||"; break; case "or": OperatorName = "|"; break; case "oR": OperatorName = "|="; break; case "pl": OperatorName = "+"; break; case "pL": OperatorName = "+="; break; case "rm": OperatorName = "%"; break; case "rM": OperatorName = "%="; break; case "rs": OperatorName = ">>"; break; case "rS": OperatorName = ">>="; break; default: return null; } Position += 2; BaseNode Expression = ParseExpression(); if (Expression == null) { return null; } BaseNode Initializer = null; if (HasInitializer) { Initializer = ParseExpression(); if (Initializer == null) { return null; } } if (IsLeftFold && Initializer != null) { BaseNode Temp = Expression; Expression = Initializer; Initializer = Temp; } return new FoldExpression(IsLeftFold, OperatorName, new PackedTemplateParameterExpansion(Expression), Initializer); } // ::= cv # type (expression), conversion with one argument // ::= cv _ * E # type (expr-list), conversion with other than one argument private BaseNode ParseConversionExpression() { if (!ConsumeIf("cv")) { return null; } bool CanParseTemplateArgsBackup = CanParseTemplateArgs; CanParseTemplateArgs = false; BaseNode Type = ParseType(); CanParseTemplateArgs = CanParseTemplateArgsBackup; if (Type == null) { return null; } List Expressions = new List(); if (ConsumeIf("_")) { while (!ConsumeIf("E")) { BaseNode Expression = ParseExpression(); if (Expression == null) { return null; } Expressions.Add(Expression); } } else { BaseNode Expression = ParseExpression(); if (Expression == null) { return null; } Expressions.Add(Expression); } return new ConversionExpression(Type, new NodeArray(Expressions)); } private BaseNode ParseBinaryExpression(string Name) { BaseNode LeftPart = ParseExpression(); if (LeftPart == null) { return null; } BaseNode RightPart = ParseExpression(); if (RightPart == null) { return null; } return new BinaryExpression(LeftPart, Name, RightPart); } private BaseNode ParsePrefixExpression(string Name) { BaseNode Expression = ParseExpression(); if (Expression == null) { return null; } return new PrefixExpression(Name, Expression); } // ::= // ::= di # .name = expr // ::= dx # [expr] = expr // ::= dX // # [expr ... expr] = expr private BaseNode ParseBracedExpression() { if (Peek() == 'd') { BaseNode BracedExpressionNode; switch (Peek(1)) { case 'i': Position += 2; BaseNode Field = ParseSourceName(); if (Field == null) { return null; } BracedExpressionNode = ParseBracedExpression(); if (BracedExpressionNode == null) { return null; } return new BracedExpression(Field, BracedExpressionNode, false); case 'x': Position += 2; BaseNode Index = ParseExpression(); if (Index == null) { return null; } BracedExpressionNode = ParseBracedExpression(); if (BracedExpressionNode == null) { return null; } return new BracedExpression(Index, BracedExpressionNode, true); case 'X': Position += 2; BaseNode RangeBeginExpression = ParseExpression(); if (RangeBeginExpression == null) { return null; } BaseNode RangeEndExpression = ParseExpression(); if (RangeEndExpression == null) { return null; } BracedExpressionNode = ParseBracedExpression(); if (BracedExpressionNode == null) { return null; } return new BracedRangeExpression(RangeBeginExpression, RangeEndExpression, BracedExpressionNode); } } return ParseExpression(); } // ::= [gs] nw * _ E # new (expr-list) type // ::= [gs] nw * _ # new (expr-list) type (init) // ::= [gs] na * _ E # new[] (expr-list) type // ::= [gs] na * _ # new[] (expr-list) type (init) // // ::= pi * E # parenthesized initialization private BaseNode ParseNewExpression() { bool IsGlobal = ConsumeIf("gs"); bool IsArray = Peek(1) == 'a'; if (!ConsumeIf("nw") || !ConsumeIf("na")) { return null; } List Expressions = new List(); List Initializers = new List(); while (!ConsumeIf("_")) { BaseNode Expression = ParseExpression(); if (Expression == null) { return null; } Expressions.Add(Expression); } BaseNode TypeNode = ParseType(); if (TypeNode == null) { return null; } if (ConsumeIf("pi")) { while (!ConsumeIf("E")) { BaseNode Initializer = ParseExpression(); if (Initializer == null) { return null; } Initializers.Add(Initializer); } } else if (!ConsumeIf("E")) { return null; } return new NewExpression(new NodeArray(Expressions), TypeNode, new NodeArray(Initializers), IsGlobal, IsArray); } // ::= // ::= // ::= // ::= pp_ # prefix ++ // ::= mm_ # prefix -- // ::= cl + E # expression (expr-list), call // ::= cv # type (expression), conversion with one argument // ::= cv _ * E # type (expr-list), conversion with other than one argument // ::= tl * E # type {expr-list}, conversion with braced-init-list argument // ::= il * E # {expr-list}, braced-init-list in any other context // ::= [gs] nw * _ E # new (expr-list) type // ::= [gs] nw * _ # new (expr-list) type (init) // ::= [gs] na * _ E # new[] (expr-list) type // ::= [gs] na * _ # new[] (expr-list) type (init) // ::= [gs] dl # delete expression // ::= [gs] da # delete[] expression // ::= dc # dynamic_cast (expression) // ::= sc # static_cast (expression) // ::= cc # const_cast (expression) // ::= rc # reinterpret_cast (expression) // ::= ti # typeid (type) // ::= te # typeid (expression) // ::= st # sizeof (type) // ::= sz # sizeof (expression) // ::= at # alignof (type) // ::= az # alignof (expression) // ::= nx # noexcept (expression) // ::= // ::= // ::= dt # expr.name // ::= pt # expr->name // ::= ds # expr.*expr // ::= sZ # sizeof...(T), size of a template parameter pack // ::= sZ # sizeof...(parameter), size of a function parameter pack // ::= sP * E # sizeof...(T), size of a captured template parameter pack from an alias template // ::= sp # expression..., pack expansion // ::= tw # throw expression // ::= tr # throw with no operand (rethrow) // ::= # f(p), N::f(p), ::f(p), // # freestanding dependent name (e.g., T::x), // # objectless nonstatic member reference // ::= private BaseNode ParseExpression() { bool IsGlobal = ConsumeIf("gs"); BaseNode Expression = null; if (Count() < 2) { return null; } switch (Peek()) { case 'L': return ParseExpressionPrimary(); case 'T': return ParseTemplateParam(); case 'f': char C = Peek(1); if (C == 'p' || (C == 'L' && char.IsDigit(Peek(2)))) { return ParseFunctionParameter(); } return ParseFoldExpression(); case 'a': switch (Peek(1)) { case 'a': Position += 2; return ParseBinaryExpression("&&"); case 'd': case 'n': Position += 2; return ParseBinaryExpression("&"); case 'N': Position += 2; return ParseBinaryExpression("&="); case 'S': Position += 2; return ParseBinaryExpression("="); case 't': Position += 2; BaseNode Type = ParseType(); if (Type == null) { return null; } return new EnclosedExpression("alignof (", Type, ")"); case 'z': Position += 2; Expression = ParseExpression(); if (Expression == null) { return null; } return new EnclosedExpression("alignof (", Expression, ")"); } return null; case 'c': switch (Peek(1)) { case 'c': Position += 2; BaseNode To = ParseType(); if (To == null) { return null; } BaseNode From = ParseExpression(); if (From == null) { return null; } return new CastExpression("const_cast", To, From); case 'l': Position += 2; BaseNode Callee = ParseExpression(); if (Callee == null) { return null; } List Names = new List(); while (!ConsumeIf("E")) { Expression = ParseExpression(); if (Expression == null) { return null; } Names.Add(Expression); } return new CallExpression(Callee, Names); case 'm': Position += 2; return ParseBinaryExpression(","); case 'o': Position += 2; return ParsePrefixExpression("~"); case 'v': return ParseConversionExpression(); } return null; case 'd': BaseNode LeftNode = null; BaseNode RightNode = null; switch (Peek(1)) { case 'a': Position += 2; Expression = ParseExpression(); if (Expression == null) { return Expression; } return new DeleteExpression(Expression, IsGlobal, true); case 'c': Position += 2; BaseNode Type = ParseType(); if (Type == null) { return null; } Expression = ParseExpression(); if (Expression == null) { return Expression; } return new CastExpression("dynamic_cast", Type, Expression); case 'e': Position += 2; return ParsePrefixExpression("*"); case 'l': Position += 2; Expression = ParseExpression(); if (Expression == null) { return null; } return new DeleteExpression(Expression, IsGlobal, false); case 'n': return ParseUnresolvedName(); case 's': Position += 2; LeftNode = ParseExpression(); if (LeftNode == null) { return null; } RightNode = ParseExpression(); if (RightNode == null) { return null; } return new MemberExpression(LeftNode, ".*", RightNode); case 't': Position += 2; LeftNode = ParseExpression(); if (LeftNode == null) { return null; } RightNode = ParseExpression(); if (RightNode == null) { return null; } return new MemberExpression(LeftNode, ".", RightNode); case 'v': Position += 2; return ParseBinaryExpression("/"); case 'V': Position += 2; return ParseBinaryExpression("/="); } return null; case 'e': switch (Peek(1)) { case 'o': Position += 2; return ParseBinaryExpression("^"); case 'O': Position += 2; return ParseBinaryExpression("^="); case 'q': Position += 2; return ParseBinaryExpression("=="); } return null; case 'g': switch (Peek(1)) { case 'e': Position += 2; return ParseBinaryExpression(">="); case 't': Position += 2; return ParseBinaryExpression(">"); } return null; case 'i': switch (Peek(1)) { case 'x': Position += 2; BaseNode Base = ParseExpression(); if (Base == null) { return null; } BaseNode Subscript = ParseExpression(); if (Base == null) { return null; } return new ArraySubscriptingExpression(Base, Subscript); case 'l': Position += 2; List BracedExpressions = new List(); while (!ConsumeIf("E")) { Expression = ParseBracedExpression(); if (Expression == null) { return null; } BracedExpressions.Add(Expression); } return new InitListExpression(null, BracedExpressions); } return null; case 'l': switch (Peek(1)) { case 'e': Position += 2; return ParseBinaryExpression("<="); case 's': Position += 2; return ParseBinaryExpression("<<"); case 'S': Position += 2; return ParseBinaryExpression("<<="); case 't': Position += 2; return ParseBinaryExpression("<"); } return null; case 'm': switch (Peek(1)) { case 'i': Position += 2; return ParseBinaryExpression("-"); case 'I': Position += 2; return ParseBinaryExpression("-="); case 'l': Position += 2; return ParseBinaryExpression("*"); case 'L': Position += 2; return ParseBinaryExpression("*="); case 'm': Position += 2; if (ConsumeIf("_")) { return ParsePrefixExpression("--"); } Expression = ParseExpression(); if (Expression == null) { return null; } return new PostfixExpression(Expression, "--"); } return null; case 'n': switch (Peek(1)) { case 'a': case 'w': Position += 2; return ParseNewExpression(); case 'e': Position += 2; return ParseBinaryExpression("!="); case 'g': Position += 2; return ParsePrefixExpression("-"); case 't': Position += 2; return ParsePrefixExpression("!"); case 'x': Position += 2; Expression = ParseExpression(); if (Expression == null) { return null; } return new EnclosedExpression("noexcept (", Expression, ")"); } return null; case 'o': switch (Peek(1)) { case 'n': return ParseUnresolvedName(); case 'o': Position += 2; return ParseBinaryExpression("||"); case 'r': Position += 2; return ParseBinaryExpression("|"); case 'R': Position += 2; return ParseBinaryExpression("|="); } return null; case 'p': switch (Peek(1)) { case 'm': Position += 2; return ParseBinaryExpression("->*"); case 'l': case 's': Position += 2; return ParseBinaryExpression("+"); case 'L': Position += 2; return ParseBinaryExpression("+="); case 'p': Position += 2; if (ConsumeIf("_")) { return ParsePrefixExpression("++"); } Expression = ParseExpression(); if (Expression == null) { return null; } return new PostfixExpression(Expression, "++"); case 't': Position += 2; LeftNode = ParseExpression(); if (LeftNode == null) { return null; } RightNode = ParseExpression(); if (RightNode == null) { return null; } return new MemberExpression(LeftNode, "->", RightNode); } return null; case 'q': if (Peek(1) == 'u') { Position += 2; BaseNode Condition = ParseExpression(); if (Condition == null) { return null; } LeftNode = ParseExpression(); if (LeftNode == null) { return null; } RightNode = ParseExpression(); if (RightNode == null) { return null; } return new ConditionalExpression(Condition, LeftNode, RightNode); } return null; case 'r': switch (Peek(1)) { case 'c': Position += 2; BaseNode To = ParseType(); if (To == null) { return null; } BaseNode From = ParseExpression(); if (From == null) { return null; } return new CastExpression("reinterpret_cast", To, From); case 'm': Position += 2; return ParseBinaryExpression("%"); case 'M': Position += 2; return ParseBinaryExpression("%"); case 's': Position += 2; return ParseBinaryExpression(">>"); case 'S': Position += 2; return ParseBinaryExpression(">>="); } return null; case 's': switch (Peek(1)) { case 'c': Position += 2; BaseNode To = ParseType(); if (To == null) { return null; } BaseNode From = ParseExpression(); if (From == null) { return null; } return new CastExpression("static_cast", To, From); case 'p': Position += 2; Expression = ParseExpression(); if (Expression == null) { return null; } return new PackedTemplateParameterExpansion(Expression); case 'r': return ParseUnresolvedName(); case 't': Position += 2; BaseNode EnclosedType = ParseType(); if (EnclosedType == null) { return null; } return new EnclosedExpression("sizeof (", EnclosedType, ")"); case 'z': Position += 2; Expression = ParseExpression(); if (Expression == null) { return null; } return new EnclosedExpression("sizeof (", Expression, ")"); case 'Z': Position += 2; BaseNode SizeofParamNode = null; switch (Peek()) { case 'T': // FIXME: ??? Not entire sure if it's right SizeofParamNode = ParseFunctionParameter(); if (SizeofParamNode == null) { return null; } return new EnclosedExpression("sizeof...(", new PackedTemplateParameterExpansion(SizeofParamNode), ")"); case 'f': SizeofParamNode = ParseFunctionParameter(); if (SizeofParamNode == null) { return null; } return new EnclosedExpression("sizeof...(", SizeofParamNode, ")"); } return null; case 'P': Position += 2; List Arguments = new List(); while (!ConsumeIf("E")) { BaseNode Argument = ParseTemplateArgument(); if (Argument == null) { return null; } Arguments.Add(Argument); } return new EnclosedExpression("sizeof...(", new NodeArray(Arguments), ")"); } return null; case 't': switch (Peek(1)) { case 'e': Expression = ParseExpression(); if (Expression == null) { return null; } return new EnclosedExpression("typeid (", Expression, ")"); case 't': BaseNode EnclosedType = ParseExpression(); if (EnclosedType == null) { return null; } return new EnclosedExpression("typeid (", EnclosedType, ")"); case 'l': Position += 2; BaseNode TypeNode = ParseType(); if (TypeNode == null) { return null; } List BracedExpressions = new List(); while (!ConsumeIf("E")) { Expression = ParseBracedExpression(); if (Expression == null) { return null; } BracedExpressions.Add(Expression); } return new InitListExpression(TypeNode, BracedExpressions); case 'r': Position += 2; return new NameType("throw"); case 'w': Position += 2; Expression = ParseExpression(); if (Expression == null) { return null; } return new ThrowExpression(Expression); } return null; } if (char.IsDigit(Peek())) { return ParseUnresolvedName(); } return null; } private BaseNode ParseIntegerLiteral(string LiteralName) { string Number = ParseNumber(true); if (Number == null || Number.Length == 0 || !ConsumeIf("E")) { return null; } return new IntegerLiteral(LiteralName, Number); } // ::= L E # integer literal // ::= L E # floating literal (TODO) // ::= L E # string literal // ::= L E # nullptr literal (i.e., "LDnE") // ::= L 0 E # null pointer template argument // ::= L _ E # complex floating point literal (C 2000) // ::= L _Z E # external name private BaseNode ParseExpressionPrimary() { if (!ConsumeIf("L")) { return null; } switch (Peek()) { case 'w': Position++; return ParseIntegerLiteral("wchar_t"); case 'b': if (ConsumeIf("b0E")) { return new NameType("false", NodeType.BooleanExpression); } if (ConsumeIf("b1E")) { return new NameType("true", NodeType.BooleanExpression); } return null; case 'c': Position++; return ParseIntegerLiteral("char"); case 'a': Position++; return ParseIntegerLiteral("signed char"); case 'h': Position++; return ParseIntegerLiteral("unsigned char"); case 's': Position++; return ParseIntegerLiteral("short"); case 't': Position++; return ParseIntegerLiteral("unsigned short"); case 'i': Position++; return ParseIntegerLiteral(""); case 'j': Position++; return ParseIntegerLiteral("u"); case 'l': Position++; return ParseIntegerLiteral("l"); case 'm': Position++; return ParseIntegerLiteral("ul"); case 'x': Position++; return ParseIntegerLiteral("ll"); case 'y': Position++; return ParseIntegerLiteral("ull"); case 'n': Position++; return ParseIntegerLiteral("__int128"); case 'o': Position++; return ParseIntegerLiteral("unsigned __int128"); case 'd': case 'e': case 'f': // TODO: floating literal return null; case '_': if (ConsumeIf("_Z")) { BaseNode Encoding = ParseEncoding(); if (Encoding != null && ConsumeIf("E")) { return Encoding; } } return null; case 'T': return null; default: BaseNode Type = ParseType(); if (Type == null) { return null; } string Number = ParseNumber(); if (Number == null || Number.Length == 0 || !ConsumeIf("E")) { return null; } return new IntegerCastExpression(Type, Number); } } // ::= Dt E # decltype of an id-expression or class member access (C++0x) // ::= DT E # decltype of an expression (C++0x) private BaseNode ParseDecltype() { if (!ConsumeIf("D") || (!ConsumeIf("t") && !ConsumeIf("T"))) { return null; } BaseNode Expression = ParseExpression(); if (Expression == null) { return null; } if (!ConsumeIf("E")) { return null; } return new EnclosedExpression("decltype(", Expression, ")"); } // ::= T_ # first template parameter // ::= T _ // ::= // ::= private BaseNode ParseTemplateParam() { if (!ConsumeIf("T")) { return null; } int Index = 0; if (!ConsumeIf("_")) { Index = ParsePositiveNumber(); if (Index < 0) { return null; } Index++; if (!ConsumeIf("_")) { return null; } } // 5.1.8: TODO: lambda? // if (IsParsingLambdaParameters) // return new NameType("auto"); if (CanForwardTemplateReference) { ForwardTemplateReference ForwardTemplateReference = new ForwardTemplateReference(Index); ForwardTemplateReferenceList.Add(ForwardTemplateReference); return ForwardTemplateReference; } if (Index >= TemplateParamList.Count) { return null; } return TemplateParamList[Index]; } // ::= I + E private BaseNode ParseTemplateArguments(bool HasContext = false) { if (!ConsumeIf("I")) { return null; } if (HasContext) { TemplateParamList.Clear(); } List Args = new List(); while (!ConsumeIf("E")) { if (HasContext) { List TemplateParamListTemp = new List(TemplateParamList); BaseNode TemplateArgument = ParseTemplateArgument(); TemplateParamList = TemplateParamListTemp; if (TemplateArgument == null) { return null; } Args.Add(TemplateArgument); if (TemplateArgument.GetType().Equals(NodeType.PackedTemplateArgument)) { TemplateArgument = new PackedTemplateParameter(((NodeArray)TemplateArgument).Nodes); } TemplateParamList.Add(TemplateArgument); } else { BaseNode TemplateArgument = ParseTemplateArgument(); if (TemplateArgument == null) { return null; } Args.Add(TemplateArgument); } } return new TemplateArguments(Args); } // ::= # type or template // ::= X E # expression // ::= # simple expressions // ::= J * E # argument pack private BaseNode ParseTemplateArgument() { switch (Peek()) { // X E case 'X': Position++; BaseNode Expression = ParseExpression(); if (Expression == null || !ConsumeIf("E")) { return null; } return Expression; // case 'L': return ParseExpressionPrimary(); // J * E case 'J': Position++; List TemplateArguments = new List(); while (!ConsumeIf("E")) { BaseNode TemplateArgument = ParseTemplateArgument(); if (TemplateArgument == null) { return null; } TemplateArguments.Add(TemplateArgument); } return new NodeArray(TemplateArguments, NodeType.PackedTemplateArgument); // default: return ParseType(); } } class NameParserContext { public CVType CV; public SimpleReferenceType Ref; public bool FinishWithTemplateArguments; public bool CtorDtorConversion; } // ::= [ ] # T:: or T:: // ::= # decltype(p):: // ::= private BaseNode ParseUnresolvedType() { if (Peek() == 'T') { BaseNode TemplateParam = ParseTemplateParam(); if (TemplateParam == null) { return null; } SubstitutionList.Add(TemplateParam); return TemplateParam; } else if (Peek() == 'D') { BaseNode DeclType = ParseDecltype(); if (DeclType == null) { return null; } SubstitutionList.Add(DeclType); return DeclType; } return ParseSubstitution(); } // ::= [ ] private BaseNode ParseSimpleId() { BaseNode SourceName = ParseSourceName(); if (SourceName == null) { return null; } if (Peek() == 'I') { BaseNode TemplateArguments = ParseTemplateArguments(); if (TemplateArguments == null) { return null; } return new NameTypeWithTemplateArguments(SourceName, TemplateArguments); } return SourceName; } // ::= # e.g., ~T or ~decltype(f()) // ::= # e.g., ~A<2*N> private BaseNode ParseDestructorName() { BaseNode Node; if (char.IsDigit(Peek())) { Node = ParseSimpleId(); } else { Node = ParseUnresolvedType(); } if (Node == null) { return null; } return new DtorName(Node); } // ::= # unresolved name // extension ::= # unresolved operator-function-id // extension ::= # unresolved operator template-id // ::= on # unresolved operator-function-id // ::= on # unresolved operator template-id // ::= dn # destructor or pseudo-destructor; // # e.g. ~X or ~X private BaseNode ParseBaseUnresolvedName() { if (char.IsDigit(Peek())) { return ParseSimpleId(); } else if (ConsumeIf("dn")) { return ParseDestructorName(); } ConsumeIf("on"); BaseNode OperatorName = ParseOperatorName(null); if (OperatorName == null) { return null; } if (Peek() == 'I') { BaseNode TemplateArguments = ParseTemplateArguments(); if (TemplateArguments == null) { return null; } return new NameTypeWithTemplateArguments(OperatorName, TemplateArguments); } return OperatorName; } // ::= [gs] # x or (with "gs") ::x // ::= sr # T::x / decltype(p)::x // ::= srN + E // # T::N::x /decltype(p)::N::x // ::= [gs] sr + E // # A::x, N::y, A::z; "gs" means leading "::" private BaseNode ParseUnresolvedName(NameParserContext Context = null) { BaseNode Result = null; if (ConsumeIf("srN")) { Result = ParseUnresolvedType(); if (Result == null) { return null; } if (Peek() == 'I') { BaseNode TemplateArguments = ParseTemplateArguments(); if (TemplateArguments == null) { return null; } Result = new NameTypeWithTemplateArguments(Result, TemplateArguments); if (Result == null) { return null; } } while (!ConsumeIf("E")) { BaseNode SimpleId = ParseSimpleId(); if (SimpleId == null) { return null; } Result = new QualifiedName(Result, SimpleId); if (Result == null) { return null; } } BaseNode BaseName = ParseBaseUnresolvedName(); if (BaseName == null) { return null; } return new QualifiedName(Result, BaseName); } bool IsGlobal = ConsumeIf("gs"); // ::= [gs] # x or (with "gs") ::x if (!ConsumeIf("sr")) { Result = ParseBaseUnresolvedName(); if (Result == null) { return null; } if (IsGlobal) { Result = new GlobalQualifiedName(Result); } return Result; } // ::= [gs] sr + E if (char.IsDigit(Peek())) { do { BaseNode Qualifier = ParseSimpleId(); if (Qualifier == null) { return null; } if (Result != null) { Result = new QualifiedName(Result, Qualifier); } else if (IsGlobal) { Result = new GlobalQualifiedName(Qualifier); } else { Result = Qualifier; } if (Result == null) { return null; } } while (!ConsumeIf("E")); } // ::= sr [tempate-args] # T::x / decltype(p)::x else { Result = ParseUnresolvedType(); if (Result == null) { return null; } if (Peek() == 'I') { BaseNode TemplateArguments = ParseTemplateArguments(); if (TemplateArguments == null) { return null; } Result = new NameTypeWithTemplateArguments(Result, TemplateArguments); if (Result == null) { return null; } } } if (Result == null) { return null; } BaseNode BaseUnresolvedName = ParseBaseUnresolvedName(); if (BaseUnresolvedName == null) { return null; } return new QualifiedName(Result, BaseUnresolvedName); } // ::= // ::= St # ::std:: private BaseNode ParseUnscopedName(NameParserContext Context) { if (ConsumeIf("St")) { BaseNode UnresolvedName = ParseUnresolvedName(Context); if (UnresolvedName == null) { return null; } return new StdQualifiedName(UnresolvedName); } return ParseUnresolvedName(Context); } // ::= N [] [] E // ::= N [] [] E private BaseNode ParseNestedName(NameParserContext Context) { // Impossible in theory if (Consume() != 'N') { return null; } BaseNode Result = null; CVType CV = new CVType(ParseCVQualifiers(), null); if (Context != null) { Context.CV = CV; } SimpleReferenceType Ref = ParseRefQualifiers(); if (Context != null) { Context.Ref = Ref; } if (ConsumeIf("St")) { Result = new NameType("std"); } while (!ConsumeIf("E")) { // end if (ConsumeIf("M")) { if (Result == null) { return null; } continue; } char C = Peek(); // TODO: template args if (C == 'T') { BaseNode TemplateParam = ParseTemplateParam(); if (TemplateParam == null) { return null; } Result = CreateNameNode(Result, TemplateParam, Context); SubstitutionList.Add(Result); continue; } // if (C == 'I') { BaseNode TemplateArgument = ParseTemplateArguments(Context != null); if (TemplateArgument == null || Result == null) { return null; } Result = new NameTypeWithTemplateArguments(Result, TemplateArgument); if (Context != null) { Context.FinishWithTemplateArguments = true; } SubstitutionList.Add(Result); continue; } // if (C == 'D' && (Peek(1) == 't' || Peek(1) == 'T')) { BaseNode Decltype = ParseDecltype(); if (Decltype == null) { return null; } Result = CreateNameNode(Result, Decltype, Context); SubstitutionList.Add(Result); continue; } // if (C == 'S' && Peek(1) != 't') { BaseNode Substitution = ParseSubstitution(); if (Substitution == null) { return null; } Result = CreateNameNode(Result, Substitution, Context); if (Result != Substitution) { SubstitutionList.Add(Substitution); } continue; } // of ParseUnqualifiedName if (C == 'C' || (C == 'D' && Peek(1) != 'C')) { // We cannot have nothing before this if (Result == null) { return null; } BaseNode CtOrDtorName = ParseCtorDtorName(Context, Result); if (CtOrDtorName == null) { return null; } Result = CreateNameNode(Result, CtOrDtorName, Context); // TODO: ABI Tags (before) if (Result == null) { return null; } SubstitutionList.Add(Result); continue; } BaseNode UnqualifiedName = ParseUnqualifiedName(Context); if (UnqualifiedName == null) { return null; } Result = CreateNameNode(Result, UnqualifiedName, Context); SubstitutionList.Add(Result); } if (Result == null || SubstitutionList.Count == 0) { return null; } SubstitutionList.RemoveAt(SubstitutionList.Count - 1); return Result; } // ::= _ # when number < 10 // ::= __ _ # when number >= 10 private void ParseDiscriminator() { if (Count() == 0) { return; } // We ignore the discriminator, we don't need it. if (ConsumeIf("_")) { ConsumeIf("_"); while (char.IsDigit(Peek()) && Count() != 0) { Consume(); } ConsumeIf("_"); } } // ::= Z E [] // ::= Z E s [] // ::= Z Ed [ ] _ private BaseNode ParseLocalName(NameParserContext Context) { if (!ConsumeIf("Z")) { return null; } BaseNode Encoding = ParseEncoding(); if (Encoding == null || !ConsumeIf("E")) { return null; } BaseNode EntityName; if (ConsumeIf("s")) { ParseDiscriminator(); return new LocalName(Encoding, new NameType("string literal")); } else if (ConsumeIf("d")) { ParseNumber(true); if (!ConsumeIf("_")) { return null; } EntityName = ParseName(Context); if (EntityName == null) { return null; } return new LocalName(Encoding, EntityName); } EntityName = ParseName(Context); if (EntityName == null) { return null; } ParseDiscriminator(); return new LocalName(Encoding, EntityName); } // ::= // ::= // ::= // ::= # See Scope Encoding below (TODO) private BaseNode ParseName(NameParserContext Context = null) { ConsumeIf("L"); if (Peek() == 'N') { return ParseNestedName(Context); } if (Peek() == 'Z') { return ParseLocalName(Context); } if (Peek() == 'S' && Peek(1) != 't') { BaseNode Substitution = ParseSubstitution(); if (Substitution == null) { return null; } if (Peek() != 'I') { return null; } BaseNode TemplateArguments = ParseTemplateArguments(Context != null); if (TemplateArguments == null) { return null; } if (Context != null) { Context.FinishWithTemplateArguments = true; } return new NameTypeWithTemplateArguments(Substitution, TemplateArguments); } BaseNode Result = ParseUnscopedName(Context); if (Result == null) { return null; } if (Peek() == 'I') { SubstitutionList.Add(Result); BaseNode TemplateArguments = ParseTemplateArguments(Context != null); if (TemplateArguments == null) { return null; } if (Context != null) { Context.FinishWithTemplateArguments = true; } return new NameTypeWithTemplateArguments(Result, TemplateArguments); } return Result; } private bool IsEncodingEnd() { char C = Peek(); return Count() == 0 || C == 'E' || C == '.' || C == '_'; } // ::= // ::= // ::= private BaseNode ParseEncoding() { NameParserContext Context = new NameParserContext(); if (Peek() == 'T' || (Peek() == 'G' && Peek(1) == 'V')) { return ParseSpecialName(Context); } BaseNode Name = ParseName(Context); if (Name == null) { return null; } // TODO: compute template refs here if (IsEncodingEnd()) { return Name; } // TODO: Ua9enable_ifI BaseNode ReturnType = null; if (!Context.CtorDtorConversion && Context.FinishWithTemplateArguments) { ReturnType = ParseType(); if (ReturnType == null) { return null; } } if (ConsumeIf("v")) { return new EncodedFunction(Name, null, Context.CV, Context.Ref, null, ReturnType); } List Params = new List(); // backup because that can be destroyed by parseType CVType CV = Context.CV; SimpleReferenceType Ref = Context.Ref; while (!IsEncodingEnd()) { BaseNode Param = ParseType(); if (Param == null) { return null; } Params.Add(Param); } return new EncodedFunction(Name, new NodeArray(Params), CV, Ref, null, ReturnType); } // ::= _Z // ::= private BaseNode Parse() { if (ConsumeIf("_Z")) { BaseNode Encoding = ParseEncoding(); if (Encoding != null && Count() == 0) { return Encoding; } return null; } else { BaseNode Type = ParseType(); if (Type != null && Count() == 0) { return Type; } return null; } } public static string Parse(string OriginalMangled) { Demangler Instance = new Demangler(OriginalMangled); BaseNode ResNode = Instance.Parse(); if (ResNode != null) { StringWriter Writer = new StringWriter(); ResNode.Print(Writer); return Writer.ToString(); } return OriginalMangled; } } }