1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
|
{ $Id: gdbtypeinfo.pp 29178 2011-01-23 19:06:01Z vincents $ }
{ ----------------------------------------------
GDBTypeInfo.pp - Debugger helper class
----------------------------------------------
@created(Wed Mar 29th WET 2003)
@lastmod($Date: 2011-01-23 19:06:01 +0000 (Sun, 23 Jan 2011) $)
@author(Marc Weustink <marc@@dommelstein.net>)
This unit contains a helper class for decoding PType output.
***************************************************************************
* *
* This source is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This code is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* General Public License for more details. *
* *
* A copy of the GNU General Public License is available on the World *
* Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
* obtain it by writing to the Free Software Foundation, *
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
* *
***************************************************************************
}
unit GDBTypeInfo;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Debugger, LclProc, DebugUtils;
(*
ptype = {
family = "class" | "record" | "enum" | "set" | "procedure" | "function" | "simple" | "pointer"
[ ancestor = "...", ]
[ private = "[" ( "{" name = "...", type = ptype "}" )* "}," ]
[ protected = "[" ( "{" name = "...", type = ptype "}" )* "}," ]
[ public = "[" ( "{" name = "...", type = ptype "}" )* "},"]
[ published = "[" ( "{" name = "...", type = ptype "}" )* "}," ]
[ members = "[" ( "..." )* "]," | "[" ( "{" name = "...", type = "..." "}" )* "]," ]
[ args = "[" ( "..." )* "]," ]
[ result = "..." ]
[ name = "..." ]
[ type = "..." ]
Examples: (tested with fpc 2.4.2 and 2.5.1 (Jan 2011) / gdb 7.0, gdb 7.2
(excluding the '~"type = ' and the '\n"')
* procedure x(ArgTFoo: TFoo; var VArgTFoo: TFoo); // TFoo = class end;
* procedure x(ArgPFoo: PFoo; var VArgPFoo: PFoo); // PFoo = ^TFoo;
"WhatIs" Results: Normal Param-by-ref
Stabs Dwarf | Stabs Dwarf
ArgTFoo TFOO TFOO | VArgTFoo TFOO &TFOO
@ArgTFoo PFOO ^TFOO | @VArgTFoo PFOO ^&TFOO ## whatis @ArgTFoo may be ^TFoo under Stabs if no named type PFoo exists
ArgPFoo PFOO PFOO | VArgPFoo PFOO &PFOO
@ArgPFoo PPFOO ^PFOO | @VArgPFoo PPFOO ^&PFOO ## whatis @ArgPFoo may be ^PFoo under Stabs if no named type PPFoo exists
Stabs Dwarf
TFoo TFOO ^TFOO = class
PFoo PFOO ^TFOO
"PType" Results:
ptype Arg<YYY> ~"type = <???> = class : public TOBJECT \n" ## followed by lines of fields (exlude inherited)
Normal Param-by-ref
Stabs Dwarf | Stabs Dwarf
ArgTFoo ^TFOO ^TFOO | VArgTFoo ^TFOO &TFOO
@ArgTFoo ^TFOO ^TFOO | @VArgTFoo ^TFOO ^&TFOO
ArgPFoo ^TFOO ^TFOO | VArgPFoo ^TFOO &TFOO
@ArgPFoo ^TFOO ^TFOO | @VArgPFoo ^TFOO ^&TFOO
Stabs Dwarf
TFoo TFOO ^TFOO
PFoo ^TFOO ^TFOO
==> "ptype SomeVariable" does not differ between TFoo and PFoo
==> dwarf ptype is the same for TFoo and PFoo (whatis can tell the diff)
* procedure x(ArgEnum: TEnum); // TEnum = (One, Two, Three);
* procedure x(ArgEnumSet: TEnumSet; var VArgEnumSet: TEnumSet); // TEnumSet = set of TEnum;
* procedure x(ArgSet: TSet; var VArgSet: TSet); // TSet = Set of (Alpha, Beta, Gamma);
* var VarEnumA: (e1,e2,e3); VarEnumSetA: set of TEnum; VarSetA: Set of (s1,s2,s3);
"WhatIs" Results (| marks a new line / gdb starts a new line with ~"):
Stabs Dwarf Dwarf without -godwarfset
ArgEnumSet TENUMSET TENUMSET TENUMSET
VArgEnumSet TENUMSET &TENUMSET &TENUMSET
ArgSet TSET TSET TSET
VArgSet TSET &TSET &TSET
VarEnumSetA set of TENUM set of |ONE..THREE <invalid unnamed pascal type code 8>
VarSetA set of = (...) set of |S1..S3 <invalid unnamed pascal type code 8>
TEnumSet TENUMSET set of |ONE..THREE TENUMSET
TSet TSET set of |ALPHA..GAMMA TSET
ArgEnum TENUM ## same for stabs (both)
VarEnumA = (...) ## same for stabs (both)
TEnum TENUN ## same for stabs (both)
"PType" Results:
Stabs Dwarf Dwarf without -godwarfset
ArgEnumSet set of TENUM set of |ONE..THREE TENUMSET
VArgEnumSet set of TENUM &set of |ONE..THREE &TENUMSET
ArgSet set of = (ALPHA, BETA, GAMMA) set of |ALPHA..GAMMA TSET
VArgSet set of = (ALPHA, BETA, GAMMA) &set of |ALPHA..GAMMA &TSET
VarEnumSetA set of TENUM set of |ONE..THREE <invalid unnamed pascal type code 8>
VarSetA set of = (S1, S2, S3) set of |S1..S3 <invalid unnamed pascal type code 8>
TEnumSet set of TENUM set of |ONE..THREE TENUMSET
TSet set of = (ALPHA, BETA, GAMMA) set of |ALPHA..GAMMA TSET
ArgEnum TENUM = (ONE, TWO, THREE) ## same for stabs (both)
VarEnumA = (E1, E2, E3) ## same for stabs (both)
TEnum TENUM = (ONE, TWO, THREE) ## same for stabs (both)
## Alternative new lines: set of ONE|..THREE| set of S1|..S3|
## All results can be prefixed by ^, for unamed pointertypes (^& for var param)
TODO: functions ? Stabs seem to always add pointer; dwarf does not?
*)
type
TGDBPTypeResultFlag =
(ptprfParamByRef,
ptprfPointer,
ptprfNoStructure, // for Class or Record: no full class declaration, type ends after class keyword; DWARF "whatis TFoo"
// includes "record {...}"
ptprfEmpty
);
TGDBPTypeResultFlags = set of TGDBPTypeResultFlag;
TGDBPTypeResultKind =
(ptprkError, ptprkSimple, ptprkClass, ptprkRecord,
ptprkEnum, ptprkSet, ptprkArray, ptprkProcedure, ptprkFunction);
TGDBPTypeResult = record
GdbDescription: string;
Flags: TGDBPTypeResultFlags;
Kind: TGDBPTypeResultKind;
Name, BaseName: TPCharWithLen; // BaseName is without ^&
Declaration: TPCharWithLen;
end;
PGDBPTypeRequest = ^TGDBPTypeRequest;
TGDBPTypeRequest = record
Request: string;
Result: TGDBPTypeResult;
Error: string;
Next: PGDBPTypeRequest;
end;
{ TGDBTypes }
TGDBTypes = class(TDBGTypes)
public
constructor CreateFromCSV(AValues: String);
end;
{ TGDBType }
TGDBTypeProcessState =
(gtpsInitial,
gtpsInitialPType,
gtpsInitialPType2, gtpsInitialPType3, // with "whaties Expr" // with "whaties Expr" and "ptype <whaties Expr>"
gtpsSimplePointer,
gtpsClassWhatIs, gtpsClassNameWhatIs, gtpsClassNamePType
);
TGDBType = class(TDBGType)
private
FInternalTypeName: string;
private
FEvalError: boolean;
FEvalRequest: PGDBPTypeRequest;
FExpression: string;
FClassIsPointer: Boolean;
FProcessState: TGDBTypeProcessState;
FPTypeExprReq, FWhatIsExprReq: TGDBPTypeRequest;
FExtraReq: TGDBPTypeRequest;
procedure AddTypeReq(var AReq :TGDBPTypeRequest; const ACmd: string = '');
public
constructor CreateForExpression(const AnExpression: string;
const AClassIsPointer: Boolean = False);
function ProcessExpression: Boolean;
property EvalRequest: PGDBPTypeRequest read FEvalRequest;
property EvalError: boolean read FEvalError;
public
// InternalTypeName: include ^ for TObject, if needed
property InternalTypeName: string read FInternalTypeName;
end;
function CreatePTypeValueList(AResultValues: String): TStringList;
function ParseTypeFromGdb(const ATypeText: string): TGDBPTypeResult;
implementation
(*
function GetPart(const ASkipTo, AnEnd: array of String; var ASource: String): String;
var
n, i, idx, SkipLen: Integer;
begin
idx := 0;
SkipLen := 0;
if High(ASkipTo) <> -1
then begin
for n := Low(ASkipTo) to High(ASkipTo) do
begin
if ASkipTo[n] <> ''
then begin
i := Pos(ASkipTo[n], ASource);
if (i > 0) and ((idx = 0) or (i < idx))
then begin
idx := i;
SkipLen := Length(ASkipTo[n]);
end;
end;
end;
if idx = 0
then begin
Result := '';
Exit;
end;
Delete(ASource, 1, idx + SkipLen - 1);
end;
idx := 0;
for n := Low(AnEnd) to High(AnEnd) do
begin
if AnEnd[n] <> ''
then begin
i := Pos(AnEnd[n], ASource);
if (i > 0) and ((idx = 0) or (i < idx))
then idx := i;
end;
end;
if idx = 0
then begin
Result := ASource;
ASource := '';
end
else begin
Result := Copy(ASource, 1, idx - 1);
Delete(ASource, 1, idx - 1);
end;
end;
*)
function CreatePTypeValueList(AResultValues: String): TStringList;
var
S, Line: String;
Lines: TStringList;
procedure DoRecord;
var
n: Integer;
S, Members: String;
begin
Result.Add('family=record');
Members := '';
//concatinate all lines and skip last end
S := '';
for n := 0 to Lines.Count - 2 do
S := S + Lines[n];
while S <> '' do
begin
if Members <> '' then Members := Members + ',';
Members := Members + '{name=' + GetPart([' '], [' '], S);
Members := Members + ',type=' + GetPart([' : '], [';'], S) + '}';
end;
Result.Add('members=[' + Members + ']');
end;
procedure DoEnum;
var
n: Integer;
S: String;
begin
Result.Add('family=enum');
S := GetPart(['('], [], Line);
//concatinate all lines
for n := 0 to Lines.Count - 1 do
S := S + Lines[n];
S := GetPart([], [')'], S);
Result.Add('members=[' + StringReplace(S, ' ', '', [rfReplaceAll]) + ']');
end;
procedure DoProcedure;
var
n: Integer;
S: String;
begin
Result.Add('family=procedure');
S := GetPart(['('], [''], Line);
//concatinate all lines
for n := 0 to Lines.Count - 1 do
S := S + Lines[n];
S := GetPart([''], [')'], S);
Result.Add('args=[' + StringReplace(S, ', ', ',', [rfReplaceAll]) + ']');
end;
procedure DoFunction;
var
n: Integer;
S, Args: String;
begin
Result.Add('family=function');
S := GetPart(['('], [], Line);
//concatinate all lines
for n := 0 to Lines.Count - 1 do
S := S + Lines[n];
Args := GetPart([], [')'], S);
S := GetPart([' : '], [], S);
Result.Add('args=[' + StringReplace(Args, ', ', ',', [rfReplaceAll]) + ']');
Result.Add('result=' + S);
end;
procedure DoClass;
begin
Result.Add('family=class');
Result.Add('ancestor=' + GetPart([': public '], [' '], Line));
end;
begin
Result := TStringList.Create;
if AResultValues = '' then Exit;
Lines := TStringList.Create;
try
Lines.Text := AResultValues;
if Lines.Count = 0 then Exit;
Line := Lines[0];
Lines.Delete(0);
S := GetPart(['type = '], [' '], Line);
if S = '' then Exit;
if Pos(' = class ', Line) > 0
then DoClass
else if S[1] = '^'
then begin
Result.Add('family=pointer');
Result.Add('type=' + GetPart(['^'], [' ='], S));
end
else if S = 'set'
then begin
Result.Add('family=set');
Result.Add('type=' + Copy(Line, 5, Length(Line)));
end
else if S = 'procedure'
then DoProcedure
else if S = 'function'
then DoFunction
else if Pos(' = (', Line) > 0
then DoEnum
else if Pos(' = record', Line) > 0
then DoRecord
else begin
Result.Add('family=simple');
Result.Add('type=' + S);
end;
finally
Lines.Free;
end;
end;
function ParseTypeFromGdb(const ATypeText: string): TGDBPTypeResult;
var
i, StartIdx, EndIdx: Integer;
CurPtr, HelpPtr, HelpPtr2, EndPtr, DeclPtr: PChar;
procedure SkipSpaces(var p: PChar); inline;
begin
while (p^ = ' ') do inc(p);
end;
function CheckKeyword: TGDBPTypeResultKind;
begin
Result := ptprkSimple;
// Might be: "set of ", "class", "record =", "array [", '<invalid unnamed"...,
case CurPtr^ of
's', 'S': begin
if (EndPtr - CurPtr >= 6 )
and (UpperCase(copy(CurPtr, 1, 7)) = 'SET OF ')
then
Result := ptprkSet;
end;
'r', 'R': begin
if (EndPtr - CurPtr >= 6 )
and (UpperCase(copy(CurPtr, 1, 6)) = 'RECORD')
and ((CurPtr+6)^ in [' ', ')', #13])
then
Result := ptprkRecord;
end;
'c', 'C': begin
if (EndPtr - CurPtr >= 5 )
and (UpperCase(copy(CurPtr, 1, 5)) = 'CLASS')
and ((CurPtr+5)^ in [' ', ')', #13])
then
Result := ptprkClass;
end;
'a', 'A': begin
if (EndPtr - CurPtr >= 5 )
and (UpperCase(copy(CurPtr, 1, 6)) = 'ARRAY ')
then
Result := ptprkArray;
end;
'<': begin
if (EndPtr - CurPtr >= 35 )
and (copy(CurPtr, 1, 36) = '<invalid unnamed pascal type code 8>')
then
Result := ptprkSet;
end;
'p', 'P': begin
if (EndPtr - CurPtr >= 9 )
and (UpperCase(copy(CurPtr, 1, 9)) = 'PROCEDURE')
and ((CurPtr+9)^ in [' ', '(', ')', #13])
then
Result := ptprkProcedure;
end;
'f', 'F': begin
if (EndPtr - CurPtr >= 8 )
and (UpperCase(copy(CurPtr, 1, 8)) = 'FUNCTION')
and ((CurPtr+8)^ in [' ', '(', ')', #13])
then
Result := ptprkFunction;
end;
end;
end;
function CheckIsEnum: Integer;
var
p: PChar;
begin
Result := -1;
if CurPtr^ <> '(' then exit;
p := CurPtr;
while not(p^ in [')', #0]) do inc(p);
if (p <= EndPtr) and (p^ = ')') then
Result := p - CurPtr + 1;
end;
begin
Result.GdbDescription := ATypeText;
Result.Flags := [];
Result.Kind := ptprkError;
Result.Name.Ptr := nil;
Result.Name.Len := 0;
Result.BaseName.Ptr := nil;
Result.BaseName.Len := 0;
Result.Declaration.Ptr := nil;
Result.Declaration.Len := 0;
If ATypeText = '' then exit;
(* // Clean the gdb outpu, remove ~"...."; replace \n by #13
if (length(ATypeText) >= 2) and (ATypeText[1] = '~') and (ATypeText[2] = '"') then
UniqueString(Result.GdbDescription);
CurPtr := @Result.GdbDescription[1];
EndPtr := CurPtr;
while (EndPtr^ <> #0) do begin
if (EndPtr^ = '~') and ((EndPtr+1)^ = '"') then begin
inc(EndPtr, 2);
while not (EndPtr^ in [#0..#31]) do begin
if (EndPtr^ = '\') then begin
inc(EndPtr);
if (EndPtr^ = 'n')
then CurPtr^ := #13 // internal marker only, no need for OS specific
else CurPtr^ := EndPtr^;
end
else
CurPtr^ := EndPtr^;
inc(EndPtr);
inc(CurPtr);
end;
dec(CurPtr);
if CurPtr^ <> '"' then begin
// something wrong
debugln('** WARNING: ptype info format error (end-quote): ' + ATypeText);
Result.GdbDescription := ATypeText;
CurPtr := @Result.GdbDescription[length(Result.GdbDescription)] + 1;
break;
end;
end
else begin
// something wrong
debugln('** WARNING: ptype info format error (start-quote): ' + ATypeText);
Result.GdbDescription := ATypeText;
CurPtr := @Result.GdbDescription[length(Result.GdbDescription)] + 1;
break;
end;
while (EndPtr^ in [#10, #13]) do inc(EndPtr);
end;
SetLength(Result.GdbDescription, CurPtr - @Result.GdbDescription[1]);
*)
StartIdx := pos('type = ', Result.GdbDescription);
if StartIdx <= 0 then exit;
inc(StartIdx, 7);
CurPtr := @Result.GdbDescription[StartIdx];
EndIdx := pos(LineEnding, Result.GdbDescription); // the first \n, even if not on the first line
if EndIdx <= 0 then EndIdx := length(Result.GdbDescription)+1;
EndPtr := @Result.GdbDescription[EndIdx-1];
// Pointer indicators
DeclPtr := CurPtr;
while True do begin
case CurPtr^ of
'^': include(Result.Flags, ptprfPointer);
'&': include(Result.Flags, ptprfParamByRef);
else break;
end;
inc(CurPtr);
end;
SkipSpaces(CurPtr); // shouldn'tever happen
if CurPtr > EndPtr then begin
include(Result.Flags, ptprfEmpty);
exit;
end;
if CurPtr^ = '=' then begin
// un-nmaed type
inc(CurPtr);
SkipSpaces(CurPtr);
i := CheckIsEnum;
if i > 0 then begin
// un-named enum // type = = (e1, e2, e3)
Result.Kind := ptprkEnum;
Result.Declaration.Ptr := CurPtr;
Result.Declaration.Len := i;
exit;
end;
// Unexpected, see if we have a keyword
Result.Kind := CheckKeyword;
if Result.Kind = ptprkSimple then begin
Result.Kind := ptprkError;
debugln('** WARNING: ptype info format error: ' + ATypeText);
exit;
end;
end
else
begin
if CurPtr^ = '(' then begin
// type in brackets, eg ^(array...)
inc(CurPtr);
end;
SkipSpaces(CurPtr); // shouldn'tever happen
Result.Kind := CheckKeyword;
if Result.Kind = ptprkSimple then begin
// we may have type = NAME = ....
HelpPtr := CurPtr;
while not (HelpPtr^ in [#0..#31, ' ']) do inc(HelpPtr);
HelpPtr2 := HelpPtr;
SkipSpaces(HelpPtr2);
if ((HelpPtr^ = ' ') and ((HelpPtr2)^ = '='))
or (HelpPtr^ in [#0, #10, #13])
then begin
// Type without space, use as name
Result.Name.Ptr := DeclPtr; //CurPtr;
Result.Name.Len := HelpPtr - DeclPtr; // CurPtr;
while DeclPtr^ in ['&', '^'] do inc(DeclPtr);
Result.BaseName.Ptr := DeclPtr; //CurPtr;
Result.BaseName.Len := HelpPtr - DeclPtr; // CurPtr;
if (HelpPtr^ in [#0, #10, #13]) then exit;
// now there must be a keyword or set
CurPtr := HelpPtr2 + 1;
// Todo: in this case the declaration doe not include the pointer, if any => maybe add flag?
SkipSpaces(CurPtr);
DeclPtr := CurPtr;
i := CheckIsEnum;
if i > 0 then begin
Result.Kind := ptprkEnum;
Result.Declaration.Ptr := CurPtr;
Result.Declaration.Len := i;
exit;
end;
Result.Kind := CheckKeyword;
if Result.Kind = ptprkSimple then begin
Result.Kind := ptprkError;
debugln('** WARNING: ptype info format error: ' + ATypeText);
exit;
end;
end
else begin
// Type is a declaration with spaces
while EndPtr^ = ' ' do dec(EndPtr);
Result.Declaration.Ptr := CurPtr;
Result.Declaration.Len := EndPtr - CurPtr + 1;
exit;
end;
end;
end;
// now we should be AT a keyword, we may have a name set already // Enum are handled already too
while EndPtr^ = ' ' do dec(EndPtr);
case Result.Kind of
ptprkClass: begin
HelpPtr := CurPtr + 5;
SkipSpaces(HelpPtr);
if HelpPtr^ in [#10, #13] then include(Result.Flags, ptprfNoStructure);
Result.Declaration.Ptr := DeclPtr;
Result.Declaration.Len := EndPtr - DeclPtr + 1;
end;
ptprkRecord: begin
HelpPtr := CurPtr + 6;
SkipSpaces(HelpPtr);
Result.Declaration.Ptr := DeclPtr;
if HelpPtr^ in ['{'] then begin
include(Result.Flags, ptprfNoStructure);
Result.Declaration.Len := CurPtr + 6 - DeclPtr;
end
else
Result.Declaration.Len := EndPtr - DeclPtr + 1;
end;
ptprkSet: begin
if CurPtr^ <> '<' then begin;
Result.Declaration.Ptr := DeclPtr;
Result.Declaration.Len := EndPtr - DeclPtr + 1;
end;
end;
ptprkArray: begin
Result.Declaration.Ptr := DeclPtr;
Result.Declaration.Len := EndPtr - DeclPtr + 1;
end;
ptprkProcedure, ptprkFunction: begin
Result.Declaration.Ptr := DeclPtr;
Result.Declaration.Len := EndPtr - DeclPtr + 1;
end;
end;
end;
{ TGDBPType }
procedure TGDBType.AddTypeReq(var AReq: TGDBPTypeRequest; const ACmd: string = '');
begin
AReq.Result.Kind := ptprkError;
AReq.Request := ACmd;
AReq.Error := '';
AReq.Next := FEvalRequest;
FEvalRequest := @AReq;
end;
constructor TGDBType.CreateForExpression(const AnExpression: string;
const AClassIsPointer: Boolean);
begin
Create(skSimple, ''); // initialize
FInternalTypeName := '';
FEvalError := False;
FExpression := AnExpression;
FClassIsPointer := AClassIsPointer;
FEvalRequest := nil;
FProcessState := gtpsInitial;
end;
function TGDBType.ProcessExpression: Boolean;
var
Lines: TStringList;
function ClearAmpersand(s: string): string;
var i: Integer;
begin
Result := s;
i := pos('&', Result);
if i > 0 then delete(Result, i, 1);
end;
Procedure InitLinesFrom(AReq: TGDBPTypeRequest);
begin
FreeAndNil(Lines);
Lines := TStringList.Create;
Lines.Text := AReq.Result.GdbDescription;
end;
procedure DoEnum;
var
S: String;
begin
FKind := skEnum;
S := PCLenToString(FPTypeExprReq.Result.Declaration);
S := GetPart(['('], [')'], S);
if (S = '') or (S = '...') then
exit;
FMembers := TStringList.Create;
FMembers.Text := StringReplace(S, ' ', #13#10, [rfReplaceAll]);
end;
procedure DoProcedure;
var
S: String;
begin
FKind := skProcedure;
S := PCLenToString(FPTypeExprReq.Result.Declaration);
S := GetPart(['('], [')'], S);
if (S = '') then
exit;
FArguments := TGDBTypes.CreateFromCSV(S);
end;
procedure DoFunction;
var
S: String;
begin
FKind := skFunction;
S := PCLenToString(FPTypeExprReq.Result.Declaration);
S := GetPart(['('], [')'], S);
if (S = '') then
exit;
FArguments := TGDBTypes.CreateFromCSV(S);
S := PCLenToString(FPTypeExprReq.Result.Declaration);
FResult := TGDBType.Create(skSimple, GetPart([' : '], [], S));
end;
procedure DoSet;
var
S: String;
begin
FKind := skSet;
S := PCLenToString(FPTypeExprReq.Result.Declaration);
S := GetPart(['('], [')'], S);
if (S = '') or (S = '...') then
exit;
FMembers := TStringList.Create;
FMembers.Text := StringReplace(StringReplace(S, ',', #13#10, [rfReplaceAll]), ' ', '', [rfReplaceAll]);
end;
{%region * Record * }
procedure DoRecord;
var
n: Integer;
S, S1, S2: String;
Field: TDBGField;
begin
if (FTypeName = 'Variant') or
(FTypeName = 'VARIANT') then
FKind := skVariant
else
if (FTypeName = 'ShortString') or
(FTypeName = 'SHORTSTRING') or
(FTypeName = '&ShortString') then
FKind := skSimple
else
FKind := skRecord;
FFields := TDBGFields.Create;
InitLinesFrom(FPTypeExprReq);
//concatenate all lines and skip last end
for n := 1 to Lines.Count - 2 do begin
S := Lines[n];
S1 := Trim(GetPart([' '], [':'], S));
S2 := Trim(GetPart([':'], [';'], S));
Field := TDBGField.Create(
S1,
TGDBType.Create(skSimple, S2),
flPublic
);
FFields.Add(Field);
end;
end;
{%endregion * Record * }
{%region * Class * }
procedure DoClass;
var
n: Integer;
S: String;
Name: String;
DBGType: TDBGType;
Location: TDBGFieldLocation;
Flags: TDBGFieldFlags;
begin
include(FAttributes, saInternalPointer);
FKind := skClass;
InitLinesFrom(FPTypeExprReq);
FFields := TDBGFields.Create;
if Lines.Count < 1 then exit;
s := Lines[0];
FAncestor := GetPart([': public '], [' '], s);
Location := flPublished;
for n := 1 to Lines.Count - 2 do
begin
S := Lines[n];
if S = '' then Continue;
if S = ' private' then Location := flPrivate
else if S = ' protected' then Location := flProtected
else if S = ' public' then Location := flPublic
else if S = ' published' then Location := flPublished
else begin
Flags := [];
if Pos(' procedure ', S) > 0
then begin
Name := GetPart(['procedure '], [' ', ';'], S);
DBGType := TGDBType.Create(
skProcedure,
TGDBTypes.CreateFromCSV(GetPart(['('], [')'], S))
);
if GetPart(['; '], [';'], S) = 'virtual'
then Flags := [ffVirtual];
end
else if Pos(' destructor ~', S) > 0
then begin
Name := GetPart(['destructor ~'], [' ', ';'], S);
DBGType := TGDBType.Create(
skProcedure,
TGDBTypes.CreateFromCSV(GetPart(['('], [')'], S))
);
if GetPart(['; '], [';'], S) = 'virtual'
then Flags := [ffVirtual];
Include(Flags, ffDestructor);
end
else if Pos(' constructor ', S) > 0
then begin
Name := GetPart(['constructor '], [' ', ';'], S);
DBGType := TGDBType.Create(
skFunction,
TGDBTypes.CreateFromCSV(GetPart(['('], [')'], S)),
TGDBType.Create(skSimple, GetPart([' : '], [';'], S))
);
if GetPart(['; '], [';'], S) = 'virtual'
then Flags := [ffVirtual];
Include(Flags, ffConstructor);
end
else if Pos(' function ', S) > 0
then begin
Name := GetPart(['function '], [' ', ';'], S);
DBGType := TGDBType.Create(
skFunction,
TGDBTypes.CreateFromCSV(GetPart(['('], [')'], S)),
TGDBType.Create(skSimple, GetPart([' : '], [';'], S))
);
if GetPart(['; '], [';'], S) = 'virtual'
then Flags := [ffVirtual];
end
else begin
Name := GetPart([' '], [' '], S);
DBGType := TGDBType.Create(skSimple, GetPart([' : '], [';'], S));
end;
FFields.Add(TDBGField.Create(Name, DBGType, Location, Flags));
end;
end;
end;
procedure ProcessClassNamePType;
begin
// Stabs
FTypeName := ClearAmpersand(PCLenToString(FWhatIsExprReq.Result.Name));
FInternalTypeName := FTypeName;
if (FExtraReq.Error <> '') or (FExtraReq.Result.Kind = ptprkError)
or (FExtraReq.Result.BaseName.Len = 0)
then begin
// FExtraReq failed;
debugln('Failed "PTYPE" request for class type name');
DoClass
end
else
if (not (ptprfPointer in FExtraReq.Result.Flags))
then begin
// Actual Class, Not a pointer
DoClass
end
else begin
// Pointer
FKind := skPointer;
end;
Result := True;
// ====> DONE
end;
procedure ProcessClassNameWhatIs;
begin
// Dwarf, Classes are always pointer
FTypeName := ClearAmpersand(PCLenToString(FWhatIsExprReq.Result.Name));
FInternalTypeName := FTypeName;
if (FExtraReq.Error <> '') or (FExtraReq.Result.Kind = ptprkError)
or (FExtraReq.Result.BaseName.Len = 0)
then begin
// FExtraReq failed;
debugln('Failed "WHATIS" request for class type name');
DoClass
end
else
// dwarf, expect always pointer, but may have " = class"
if (not (ptprfPointer in FExtraReq.Result.Flags))
or (FExtraReq.Result.Kind = ptprkClass)
then begin
// Actual Class, Not a pointer
DoClass
end
else begin
// Pointer
FKind := skPointer;
end;
Result := True;
// ====> DONE
end;
procedure ProcessClassWhatIs;
begin
if (FWhatIsExprReq.Error <> '') or (FWhatIsExprReq.Result.Kind = ptprkError)
or (FWhatIsExprReq.Result.BaseName.Len = 0)
then begin
// failed to get a classname => assume class
debugln('Failed "WHATIS" request for class expression');
FTypeName := PCLenToString(FPTypeExprReq.Result.BaseName);
FInternalTypeName := FTypeName;
DoClass;
Result := True;
// ====> DONE
exit;
end;
if (ptprfParamByRef in FWhatIsExprReq.Result.Flags) then
include(FAttributes, saRefParam);
if (ptprfPointer in FWhatIsExprReq.Result.Flags)
and (FWhatIsExprReq.Result.Kind = ptprkSimple) // Typename alias, must be simple
then begin
// pointer to class
FKind := skPointer;
FTypeName := ClearAmpersand(PCLenToString(FWhatIsExprReq.Result.Name));
FInternalTypeName := FTypeName;
Result := True;
// ====> DONE
exit;
end;
if FClassIsPointer then begin
// Dwarf, Classes are always pointer // need Whatis <type>
AddTypeReq(FExtraReq, 'whatis ' + PCLenToString(FWhatIsExprReq.Result.BaseName));
FProcessState := gtpsClassNameWhatIs;
// ====> state = ClassNameWhats
end
else begin
// Stabs // need PType <type>
AddTypeReq(FExtraReq, 'ptype ' + PCLenToString(FWhatIsExprReq.Result.BaseName));
FProcessState := gtpsClassNamePType;
// ====> state = ClassNamePType
end;
end;
{%endregion * Class * }
{%region * Simple * }
procedure ProcessSimplePointer;
begin
FKind := skPointer;
if (FWhatIsExprReq.Error = '') and (FWhatIsExprReq.Result.Kind = ptprkSimple) then begin
// Whatis result is ok
if (ptprfParamByRef in FWhatIsExprReq.Result.Flags) then
include(FAttributes, saRefParam);
FTypeName := ClearAmpersand(PCLenToString(FWhatIsExprReq.Result.Name));
end
else begin
// Whatis result failed
FTypeName := ClearAmpersand((PCLenToString(FPTypeExprReq.Result.Name)));
end;
FInternalTypeName := FTypeName;
Result := True;
// ====> DONE
end;
{%endregion * Simple * }
procedure ProcessInitialPType;
begin
if FPTypeExprReq.Error <> '' then begin
FEvalError := True;
exit;
end;
if (ptprfParamByRef in FPTypeExprReq.Result.Flags) then
include(FAttributes, saRefParam);
if (ptprfPointer in FPTypeExprReq.Result.Flags)
and ( (FPTypeExprReq.Result.Kind in
[ptprkSimple, ptprkRecord, ptprkEnum, ptprkSet])
or (FClassIsPointer and (FPTypeExprReq.Result.Kind in
[ptprkProcedure, ptprkFunction]) )
)
then begin
// there may be multiply levels of pointer, get the name of this pointer
AddTypeReq(FWhatIsExprReq, 'whatis ' + FExpression);
FProcessState := gtpsSimplePointer;
// ====> state = SimplePointer
exit;
end;
if (ptprfParamByRef in FPTypeExprReq.Result.Flags)
and not (FPTypeExprReq.Result.Kind in [ptprkError, ptprkClass])
then begin
// could be a pointer // need ptype of whatis
if FProcessState = gtpsInitialPType then begin
AddTypeReq(FWhatIsExprReq, 'whatis ' + FExpression);
FProcessState := gtpsInitialPType2;
// ====> state = gtpsInitialPType2
exit;
end
else if (FProcessState = gtpsInitialPType2) and (FWhatIsExprReq.Result.BaseName.Len > 0)
then begin
AddTypeReq(FExtraReq, 'ptype ' + PCLenToString(FWhatIsExprReq.Result.BaseName));
FProcessState := gtpsInitialPType3;
// ====> state = gtpsInitialPType2
exit;
end
else // must be gtpsInitialPType3
if (FExtraReq.Error = '') and (ptprfPointer in FExtraReq.Result.Flags) then begin
// pointer
FKind := skPointer;
FTypeName := ClearAmpersand(PCLenToString(FWhatIsExprReq.Result.Name));
FInternalTypeName := FTypeName;
Result := True;
// ====> DONE
exit;
end;
end;
case FPTypeExprReq.Result.Kind of
ptprkError: begin
// could be empty pointer @ArgProcedure
Result := True; // nothing to be done, keep simple type, no name
end;
ptprkSimple: begin
// may only need whatis, if current name isn't usable?
if FProcessState = gtpsInitialPType then begin
AddTypeReq(FWhatIsExprReq, 'whatis ' + FExpression);
FProcessState := gtpsInitialPType2;
// ====> state = gtpsInitialPType2
end
else begin
if (FWhatIsExprReq.Result.BaseName.Len > 0) then
FTypeName := PCLenToString(FWhatIsExprReq.Result.BaseName)
else
FTypeName := PCLenToString(FPTypeExprReq.Result.BaseName);
FInternalTypeName := FTypeName; // There may be an alias?
FKind := skSimple;
Result := True;
// ====> DONE
end;
end;
ptprkClass: begin
AddTypeReq(FWhatIsExprReq, 'whatis ' + FExpression);
FProcessState := gtpsClassWhatIs; // ====> state = ClassWhatis
end;
ptprkRecord: begin
FTypeName := PCLenToString(FPTypeExprReq.Result.BaseName);
FInternalTypeName := FTypeName; // There may be an alias?
DoRecord;
Result := True;
// ====> DONE
end;
ptprkEnum: begin
FTypeName := PCLenToString(FPTypeExprReq.Result.BaseName);
FInternalTypeName := FTypeName; //s There may be an alias?
DoEnum;
Result := True;
// ====> DONE
end;
ptprkSet: begin
if FProcessState = gtpsInitialPType then begin
AddTypeReq(FWhatIsExprReq, 'whatis ' + FExpression);
FProcessState := gtpsInitialPType2;
// ====> state = gtpsInitialPType2
end
else begin
if (FWhatIsExprReq.Result.BaseName.Len > 0) then
FTypeName := PCLenToString(FWhatIsExprReq.Result.BaseName)
else
FTypeName := PCLenToString(FPTypeExprReq.Result.BaseName);
FInternalTypeName := FTypeName;
DoSet;
Result := True;
// ====> DONE
end;
end;
ptprkArray: begin
if FProcessState = gtpsInitialPType then begin
AddTypeReq(FWhatIsExprReq, 'whatis ' + FExpression);
FProcessState := gtpsInitialPType2;
// ====> state = gtpsInitialPType2
end
else begin
FKind := skSimple;
if (FWhatIsExprReq.Result.BaseName.Len > 0) then
FTypeName := PCLenToString(FWhatIsExprReq.Result.BaseName)
else
FTypeName := PCLenToString(FPTypeExprReq.Result.BaseName);
FInternalTypeName := FTypeName;
Result := True;
// ====> DONE
end;
end;
ptprkProcedure: begin
// under stabs, procedure/function are always pointer // pointer to proc/func return empty type
if FClassIsPointer // Dwarf
and (ptprfPointer in FPTypeExprReq.Result.Flags)
then begin
AddTypeReq(FWhatIsExprReq, 'whatis ' + FExpression);
FProcessState := gtpsSimplePointer;
// ====> state = SimplePointer
end
else
if FProcessState = gtpsInitialPType then begin
AddTypeReq(FWhatIsExprReq, 'whatis ' + FExpression);
FProcessState := gtpsInitialPType2;
// ====> state = gtpsInitialPType2
end
else begin
if (FWhatIsExprReq.Result.BaseName.Len > 0) then
FTypeName := PCLenToString(FWhatIsExprReq.Result.BaseName)
else
FTypeName := PCLenToString(FPTypeExprReq.Result.BaseName);
if FTypeName = '' then FTypeName := 'procedure';
FInternalTypeName := FTypeName;
DoProcedure;
Result := True;
// ====> DONE
end;
end;
ptprkFunction: begin
// under stabs, procedure/function are always pointer // pointer to proc/func return empty type
if FClassIsPointer // Dwarf
and (ptprfPointer in FPTypeExprReq.Result.Flags)
then begin
AddTypeReq(FWhatIsExprReq, 'whatis ' + FExpression);
FProcessState := gtpsSimplePointer;
// ====> state = SimplePointer
end
else
if FProcessState = gtpsInitialPType then begin
AddTypeReq(FWhatIsExprReq, 'whatis ' + FExpression);
FProcessState := gtpsInitialPType2;
// ====> state = gtpsInitialPType2
end
else begin
if (FWhatIsExprReq.Result.BaseName.Len > 0) then
FTypeName := PCLenToString(FWhatIsExprReq.Result.BaseName)
else
FTypeName := PCLenToString(FPTypeExprReq.Result.BaseName);
if FTypeName = '' then FTypeName := 'function';
FInternalTypeName := FTypeName;
DoFunction;
Result := True;
// ====> DONE
end;
end;
end;
end;
procedure InitializeProcessing;
begin
AddTypeReq(FPTypeExprReq, 'ptype ' + FExpression);
FProcessState := gtpsInitialPType;
end;
var
OldProcessState: TGDBTypeProcessState;
begin
Result := False;
FEvalRequest := nil;
Lines := nil;
OldProcessState := FProcessState;
case FProcessState of
gtpsInitial: InitializeProcessing;
gtpsInitialPType,
gtpsInitialPType2,
gtpsInitialPType3: ProcessInitialPType;
gtpsSimplePointer: ProcessSimplePointer;
gtpsClassWhatIs: ProcessClassWhatIs;
gtpsClassNameWhatIs: ProcessClassNameWhatIs;
gtpsClassNamePType: ProcessClassNamePType;
end;
FreeAndNil(Lines);
if (FProcessState = OldProcessState) and (not Result) and (FEvalRequest = nil)
then begin
debugln('ERROR: detected state loop in ProcessExpression');
Result := True;
end;
end;
{ TGDBPTypes }
constructor TGDBTypes.CreateFromCSV(AValues: String);
var
GDBType: TGDBType;
begin
Create;
while AValues <> '' do
begin
GDBType := TGDBType.Create(skSimple, GetPart([], [', '], AValues));
FList.Add(GDBType);
{if Length(AValues) >= 2 then} Delete(AValues, 1, 2);
end;
end;
end.
|