20 #include "pedigree/kernel/utilities/demangle.h" 21 #include "pedigree/kernel/utilities/StaticString.h" 22 #include "pedigree/kernel/utilities/utility.h" 34 : nSubstitutions(0), nTemplateParams(0), nParams(0), nLevel(0),
39 size_t nSubstitutions;
41 size_t nTemplateParams;
45 size_t nNameParseLevel;
52 static int parse##id(LargeStaticString &, LargeStaticString &, demangle_t &) 53 #define DECLARE_LVAL(id) \ 54 static int parse##id( \ 55 LargeStaticString &, LargeStaticString &, demangle_t &, int &) 56 #define DECLARE_SVAL(id) \ 57 static int parse##id( \ 58 LargeStaticString &, LargeStaticString &, demangle_t &, \ 62 DECLARE(UnscopedName);
63 DECLARE(UnscopedTemplateName);
66 DECLARE(UnqualifiedName);
68 DECLARE(CvQualifiers);
71 DECLARE(FunctionType);
74 DECLARE(PointerToMemberType);
75 DECLARE(TemplateParam);
76 DECLARE(TemplateTemplateParam);
77 DECLARE(TemplateArgs);
81 DECLARE(Substitution);
83 DECLARE(OperatorName);
84 DECLARE(CtorDtorName);
87 DECLARE_LVAL(Identifier);
91 #define END_SUCCESS(id) \ 95 "[%d] Success:\t %s, \t%s -> %s\n", data.nLevel--, id, \ 96 (const char *) src, (const char *) dest); \ 99 #define END_FAIL(id) \ 103 "[%d] Fail:\t %s, \t%s -> %s\n", data.nLevel--, id, \ 104 (const char *) src, (const char *) dest); \ 111 "[%d] Start:\t %s, \t%s -> %s\n", ++data.nLevel, id, \ 112 (const char *) src, (const char *) dest); \ 115 #define END_SUCCESS(id) return SUCCESS 116 #define END_FAIL(id) return FAIL 123 for (
size_t i = 0; i < data.nSubstitutions; i++)
124 if (!StringCompare(data.substitutions[i], str))
129 if (str[0] ==
':' && str[1] ==
':')
132 data.substitutions[data.nSubstitutions++] = str;
138 static int parseMangledName(
141 START(
"MangledName");
143 if (src[0] !=
'_' || src[1] !=
'Z')
144 END_FAIL(
"MangledName");
146 if (parseName(src, dest, data) == FAIL)
147 END_FAIL(
"MangledName");
152 if (data.nSubstitutions)
153 data.nSubstitutions--;
158 data.nNameParseLevel = 5;
163 if (data.nTemplateParams > 0)
164 if (parseType(src, tmp, data) == FAIL)
165 END_FAIL(
"MangledName");
170 if (parseType(src, data.params[data.nParams], data) == FAIL)
171 END_FAIL(
"MangledName");
173 }
while (src.length() > 0);
174 END_SUCCESS(
"MangledName");
187 data.nNameParseLevel++;
194 size_t ret = parseNestedName(src, dest, data);
195 data.nNameParseLevel--;
204 if (parseUnscopedTemplateName(src, dest, data) == SUCCESS &&
205 parseTemplateArgs(src, dest, data) == SUCCESS)
207 data.nNameParseLevel--;
214 if (parseUnscopedName(src, dest, data) == SUCCESS)
216 data.nNameParseLevel--;
221 data.nNameParseLevel--;
231 static int parseUnscopedName(
234 START(
"UnscopedName");
235 if (parseUnqualifiedName(src, dest, data) == SUCCESS)
236 END_SUCCESS(
"UnscopedName");
238 END_FAIL(
"UnscopedName");
245 static int parseUnscopedTemplateName(
248 START(
"UnscopedTemplateName");
253 if (parseSubstitution(src, dest, data) == SUCCESS)
254 END_SUCCESS(
"UnscopedTemplateName");
259 if (parseUnqualifiedName(src, dest, data) == SUCCESS)
260 END_SUCCESS(
"UnscopedTemplateName");
262 END_FAIL(
"UnscopedTemplateName");
271 static int parseNestedName(
277 END_FAIL(
"NestedName");
281 if (src[0] ==
'r' || src[0] ==
'K' || src[0] ==
'V')
283 if (parseCvQualifiers(src, dest, data) == FAIL)
284 END_FAIL(
"NestedName");
288 if (parsePrefix(src, dest, data, thePrefix) == FAIL)
289 END_FAIL(
"NestedName");
292 END_FAIL(
"NestedName");
295 END_SUCCESS(
"NestedName");
311 static int parsePrefix(
328 if (parseSubstitution(src, dest, data) == SUCCESS)
329 END_SUCCESS(
"Prefix");
336 if (parseUnqualifiedName(src, unqualName, data) == SUCCESS)
344 thisPrefix += unqualName;
346 addSubstitution(thisPrefix, data);
350 if (parseTemplateArgs(src, unqualName, data) == FAIL)
358 thisPrefix += unqualName;
359 addSubstitution(thisPrefix, data);
363 if (parsePrefix(src, dest, data, thisPrefix) == SUCCESS)
364 END_SUCCESS(
"Prefix");
373 END_SUCCESS(
"Prefix");
392 static int parseUnqualifiedName(
395 START(
"UnqualifiedName");
399 if (parseOperatorName(src, dest, data) == SUCCESS)
400 END_SUCCESS(
"UnqualifiedName");
405 if (parseCtorDtorName(src, dest, data) == SUCCESS)
406 END_SUCCESS(
"UnqualifiedName");
411 if (parseSourceName(src, dest, data) == SUCCESS)
412 END_SUCCESS(
"UnqualifiedName");
414 END_FAIL(
"UnqualifiedName");
421 static int parseCtorDtorName(
424 START(
"CtorDtorName");
425 if (src[0] ==
'C' && (src[1] ==
'0' || src[1] ==
'1' || src[1] ==
'2'))
430 else if (src[0] ==
'D' && (src[1] ==
'0' || src[1] ==
'1' || src[1] ==
'2'))
436 END_FAIL(
"CtorDtorName");
437 END_SUCCESS(
"CtorDtorName");
492 static int parseOperatorName(
495 START(
"OperatorName");
501 dest +=
"operator new";
503 dest +=
"operator new[]";
505 dest +=
"operator delete";
507 dest +=
"operator delete[]";
509 dest +=
"operator+(unary)";
511 dest +=
"operator-(unary)";
513 dest +=
"operator&(unary)";
515 dest +=
"operator*(unary)";
537 dest +=
"operator+=";
539 dest +=
"operator-=";
541 dest +=
"operator*=";
543 dest +=
"operator/=";
545 dest +=
"operator%=";
547 dest +=
"operator&=";
549 dest +=
"operator|=";
551 dest +=
"operator^=";
553 dest +=
"operator<<";
555 dest +=
"operator>>";
557 dest +=
"operator<<=";
559 dest +=
"operator>>=";
561 dest +=
"operator==";
563 dest +=
"operator!=";
569 dest +=
"operator<=";
571 dest +=
"operator>=";
575 dest +=
"operator&&";
577 dest +=
"operator||";
579 dest +=
"operator++";
581 dest +=
"operator--";
585 dest +=
"operator->*";
587 dest +=
"operator->";
589 dest +=
"operator()";
591 dest +=
"operator[]";
595 dest +=
"operator sizeof";
597 dest +=
"operator sizeof";
601 if (parseType(src, dest, data) == FAIL)
602 END_FAIL(
"OperatorName");
605 END_FAIL(
"OperatorName");
607 END_SUCCESS(
"OperatorName");
613 static int parseSourceName(
619 if (parseNumber(src, dest, data, lval) == SUCCESS && lval >= 0 &&
620 parseIdentifier(src, dest, data, lval) == SUCCESS)
621 END_SUCCESS(
"SourceName");
623 END_FAIL(
"SourceName");
629 static int parseNumber(
636 bool bNegative =
false;
644 if (src[0] <
'0' || src[0] >
'9')
649 while (src[nLength] >=
'0' && src[nLength] <=
'9')
651 str[nLength] = src[nLength];
655 lval = StringToUnsignedLong(str, 0, 10);
659 src.stripFirst(nLength);
661 END_SUCCESS(
"Number");
667 static int parseIdentifier(
672 if (static_cast<int>(src.length()) < lval)
673 END_FAIL(
"Identifier");
674 for (
int i = 0; i < lval; i++)
676 src.stripFirst(lval);
677 END_SUCCESS(
"Identifier");
683 static int parseCvQualifiers(
686 START(
"CvQualifiers");
687 bool bParsedOne =
false;
707 END_FAIL(
"CvQualifiers");
709 END_SUCCESS(
"CvQualifiers");
731 if (src[0] ==
'r' || src[0] ==
'V' || src[0] ==
'K')
734 if (parseCvQualifiers(src, tmp, data) == FAIL)
736 if (parseType(src, tmp, data) == FAIL)
739 addSubstitution(tmp, data);
743 else if (src[0] ==
'P')
747 if (parseType(src, tmp, data) == FAIL)
752 addSubstitution(tmp, data);
756 else if (src[0] ==
'R')
760 if (parseType(src, tmp, data) == FAIL)
765 addSubstitution(tmp, data);
769 else if (src[0] ==
'F')
771 if (parseFunctionType(src, dest, data) == FAIL)
776 else if (src[0] ==
'A')
778 if (parseArrayType(src, dest, data) == FAIL)
783 else if (src[0] ==
'M')
785 if (parsePointerToMemberType(src, dest, data) == FAIL)
790 else if (src[0] ==
'T')
795 if (parseTemplateTemplateParam(src, dest, data) == SUCCESS &&
796 parseTemplateArgs(src, dest, data) == SUCCESS)
802 if (parseTemplateParam(src, dest, data) == FAIL)
812 if (parseBuiltinType(src, dest, data) == SUCCESS)
819 if (parseName(src, tmp, data) == SUCCESS)
822 addSubstitution(tmp, data);
830 if (parseSubstitution(src, dest, data) == FAIL)
862 static int parseBuiltinType(
865 START(
"BuiltinType");
884 dest +=
"unsigned char";
890 dest +=
"unsigned short";
896 dest +=
"unsigned int";
902 dest +=
"unsigned long";
908 dest +=
"unsigned __int64";
914 dest +=
"unsigned __int128";
926 END_FAIL(
"BuiltinType");
929 END_SUCCESS(
"BuiltinType");
935 static int parseFunctionType(
938 START(
"FunctionType");
942 END_FAIL(
"FunctionType");
950 if (parseType(src, dest, data) == FAIL)
951 END_FAIL(
"FunctionType");
954 bool bIsFirst =
true;
961 if (parseType(src, dest, data) == FAIL)
962 END_FAIL(
"FunctionType");
963 }
while (src[0] !=
'E');
967 END_SUCCESS(
"FunctionType");
983 static int parseArrayType(
995 if (src[0] >=
'0' && src[0] <=
'9')
998 while (src[nLength] >=
'0' && src[nLength] <=
'9')
999 bound += src[nLength++];
1000 src.stripFirst(nLength);
1002 else if (parseExpression(src, bound, data) == FAIL)
1012 if (parseType(src, dest, data) == FAIL)
1020 END_SUCCESS(
"Array");
1026 static int parsePointerToMemberType(
1029 START(
"PointerToMemberType");
1033 END_FAIL(
"PointerToMemberType");
1038 if (parseType(src, classType, data) == FAIL)
1039 END_FAIL(
"PointerToMemberType");
1049 if (parseFunctionType(src,
function, data) == FAIL)
1050 END_FAIL(
"PointerToMemberType");
1053 while (
function[nLength] !=
'(' ||
function[nLength + 1] !=
')')
1056 dest +=
function.left(nLength + 1);
1059 dest +=
function.
right(
function.length() - nLength - 1);
1063 if (parseType(src, dest, data) == FAIL)
1064 END_FAIL(
"PointerToMemberType");
1069 END_SUCCESS(
"PointerToMemberType");
1076 static int parseTemplateParam(
1079 START(
"TemplateParam");
1083 END_FAIL(
"TemplateParam");
1090 parseNumber(src, dest, data, nId);
1096 END_FAIL(
"TemplateParam");
1101 if (nId >= static_cast<int>(data.nTemplateParams))
1102 END_FAIL(
"TemplateParam");
1105 dest += data.templateParams[nId];
1106 END_SUCCESS(
"TemplateParam");
1113 static int parseTemplateTemplateParam(
1116 START(
"TemplateTemplateParam");
1117 if (parseTemplateParam(src, dest, data) == SUCCESS)
1118 END_SUCCESS(
"TemplateTemplateParam");
1120 END_FAIL(
"TemplateTemplateParam");
1126 static int parseTemplateArgs(
1129 START(
"TemplateArgs");
1132 if (data.nNameParseLevel == 1)
1133 data.nTemplateParams = 0;
1136 END_FAIL(
"TemplateArgs");
1139 bool bIsFirst =
true;
1147 if (parseTemplateArg(src, tmp, data) == FAIL)
1148 END_FAIL(
"TemplateArgs");
1150 if (data.nNameParseLevel == 1)
1152 data.templateParams[data.nTemplateParams] = tmp;
1153 data.nTemplateParams++;
1155 }
while (src[0] !=
'E');
1159 END_SUCCESS(
"TemplateArgs");
1167 static int parseTemplateArg(
1170 START(
"TemplateArg");
1174 if (parseType(src, dest, data) == SUCCESS)
1175 END_SUCCESS(
"TemplateArg");
1183 if (parseExpression(src, dest, data) == FAIL)
1184 END_FAIL(
"TemplateArg");
1186 END_FAIL(
"TemplateArg");
1188 END_SUCCESS(
"TemplateArg");
1194 if (parseExprPrimary(src, dest, data) == SUCCESS)
1195 END_SUCCESS(
"TemplateArg");
1197 END_FAIL(
"TemplateArg");
1210 static int parseExpression(
1213 START(
"Expression");
1217 if (parseTemplateParam(src, dest, data) == FAIL)
1218 END_FAIL(
"Expression");
1220 END_SUCCESS(
"Expression");
1222 else if (src[0] ==
'L')
1224 if (parseExprPrimary(src, dest, data) == FAIL)
1225 END_FAIL(
"Expression");
1227 END_SUCCESS(
"Expression");
1233 #define UNARY_OP(prefix) \ 1236 if (parseExpression(src, dest, data) == FAIL) \ 1237 END_FAIL("Expression"); \ 1239 END_SUCCESS("Expression"); \ 1241 #define BINARY_OP(infix) \ 1244 if (parseExpression(src, dest, data) == FAIL) \ 1245 END_FAIL("Expression"); \ 1247 if (parseExpression(src, dest, data) == FAIL) \ 1248 END_FAIL("Expression"); \ 1252 END_SUCCESS("Expression"); \ 1257 else if (op ==
"ng")
1259 else if (op ==
"ad")
1261 else if (op ==
"de")
1263 else if (op ==
"co")
1265 else if (op ==
"nt")
1267 else if (op ==
"pl")
1269 else if (op ==
"mi")
1271 else if (op ==
"ml")
1273 else if (op ==
"dv")
1275 else if (op ==
"rm")
1277 else if (op ==
"an")
1279 else if (op ==
"or")
1281 else if (op ==
"eo")
1283 else if (op ==
"ls")
1285 else if (op ==
"rs")
1287 else if (op ==
"st")
1290 if (parseType(src, dest, data) == FAIL)
1291 END_FAIL(
"Expression");
1293 END_SUCCESS(
"Expression");
1296 END_FAIL(
"Expression");
1304 static int parseExprPrimary(
1307 START(
"ExprPrimary");
1311 END_FAIL(
"ExprPrimary");
1317 const char *cookie = 0;
1338 if (parseType(src, dest, data) == FAIL)
1339 END_FAIL(
"ExprPrimary");
1345 if (parseNumber(src, dest, data, n) == FAIL)
1346 END_FAIL(
"ExprPrimary");
1347 dest +=
static_cast<ssize_t
>(n);
1350 END_FAIL(
"ExprPrimary");
1354 END_SUCCESS(
"ExprPrimary");
1361 static int parseSubstitution(
1364 START(
"Substitution");
1368 END_FAIL(
"Substitution");
1375 parseSeqId(src, dest, data, nId);
1381 END_FAIL(
"Substitution");
1386 if (nId >= static_cast<int>(data.nSubstitutions))
1387 END_FAIL(
"Substitution");
1390 dest += data.substitutions[nId];
1392 END_SUCCESS(
"Substitution");
1398 static int parseSeqId(
1405 while ((src[nLength] >=
'0' && src[nLength] <=
'9') ||
1406 ((src[nLength] >=
'A') && src[nLength] <=
'Z'))
1408 str[nLength] = src[nLength];
1411 str[nLength] =
'\0';
1412 lval = StringToUnsignedLong(str, 0, 36);
1414 src.stripFirst(nLength);
1415 END_SUCCESS(
"SeqId");
1476 data.nSubstitutions = 0;
1477 data.nNameParseLevel = 0;
1478 int code = parseMangledName(src, sym->name, data);
1481 if (sym->name[0] ==
':' && sym->name[1] ==
':')
1482 sym->name.stripFirst(2);
1491 sym->nParams = data.nParams;
1492 for (
size_t i = 0; i < data.nParams; i++)
1493 sym->params[i] = data.params[i];
1501 data.nSubstitutions = 0;
1502 data.nNameParseLevel = 0;
1505 int code = parseMangledName(src, dest, data);
1508 if (dest[0] ==
':' && dest[1] ==
':')
1518 for (
size_t i = 0; i < data.nParams; i++)
1523 dest += data.params[i];
StaticString right(int n) const