browcol.pas 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by the FPC development team
  4. Support routines for getting browser info in collections
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. {$ifdef TP}
  19. {$N+,E+}
  20. {$endif}
  21. unit browcol;
  22. interface
  23. uses
  24. objects,symtable;
  25. const
  26. SymbolTypLen : integer = 6;
  27. RecordTypes : set of tsymtyp =
  28. ([typesym,unitsym,programsym]);
  29. sfRecord = $00000001;
  30. sfObject = $00000002;
  31. sfClass = $00000004;
  32. type
  33. TStoreCollection = object(TStringCollection)
  34. function Add(const S: string): PString;
  35. end;
  36. PModuleNameCollection = ^TModuleNameCollection;
  37. TModuleNameCollection = object(TStoreCollection)
  38. end;
  39. PTypeNameCollection = ^TTypeNameCollection;
  40. TTypeNameCollection = object(TStoreCollection)
  41. end;
  42. PSymbolCollection = ^TSymbolCollection;
  43. PSortedSymbolCollection = ^TSortedSymbolCollection;
  44. PReferenceCollection = ^TReferenceCollection;
  45. PReference = ^TReference;
  46. TReference = object(TObject)
  47. FileName : PString;
  48. Position : TPoint;
  49. constructor Init(AFileName: PString; ALine, AColumn: Sw_integer);
  50. function GetFileName: string;
  51. destructor Done; virtual;
  52. end;
  53. PSymbolMemInfo = ^TSymbolMemInfo;
  54. TSymbolMemInfo = record
  55. Addr : longint;
  56. LocalAddr : longint;
  57. Size : longint;
  58. PushSize : longint;
  59. end;
  60. PSymbol = ^TSymbol;
  61. TSymbol = object(TObject)
  62. Name : PString;
  63. Typ : tsymtyp;
  64. Params : PString;
  65. References : PReferenceCollection;
  66. Items : PSymbolCollection;
  67. DType : PString;
  68. VType : PString;
  69. ObjectID : longint;
  70. AncestorID : longint;
  71. Ancestor : PSymbol;
  72. Flags : longint;
  73. MemInfo : PSymbolMemInfo;
  74. constructor Init(const AName: string; ATyp: tsymtyp; AParams: string; AMemInfo: PSymbolMemInfo);
  75. procedure SetMemInfo(const AMemInfo: TSymbolMemInfo);
  76. function GetReferenceCount: Sw_integer;
  77. function GetReference(Index: Sw_integer): PReference;
  78. function GetItemCount: Sw_integer;
  79. function GetItem(Index: Sw_integer): PSymbol;
  80. function GetName: string;
  81. function GetText: string;
  82. function GetTypeName: string;
  83. destructor Done; virtual;
  84. end;
  85. PObjectSymbolCollection = ^TObjectSymbolCollection;
  86. PObjectSymbol = ^TObjectSymbol;
  87. TObjectSymbol = object(TObject)
  88. Parent : PObjectSymbol;
  89. Symbol : PSymbol;
  90. Expanded : boolean;
  91. constructor Init(AParent: PObjectSymbol; ASymbol: PSymbol);
  92. constructor InitName(const AName: string);
  93. function GetName: string;
  94. function GetDescendantCount: sw_integer;
  95. function GetDescendant(Index: sw_integer): PObjectSymbol;
  96. procedure AddDescendant(P: PObjectSymbol);
  97. destructor Done; virtual;
  98. private
  99. Name: PString;
  100. Descendants: PObjectSymbolCollection;
  101. end;
  102. TSymbolCollection = object(TSortedCollection)
  103. function At(Index: Sw_Integer): PSymbol;
  104. procedure Insert(Item: Pointer); virtual;
  105. function LookUp(const S: string; var Idx: sw_integer): string; virtual;
  106. end;
  107. TSortedSymbolCollection = object(TSymbolCollection)
  108. function Compare(Key1, Key2: Pointer): Sw_Integer; virtual;
  109. procedure Insert(Item: Pointer); virtual;
  110. function LookUp(const S: string; var Idx: sw_integer): string; virtual;
  111. end;
  112. PIDSortedSymbolCollection = ^TIDSortedSymbolCollection;
  113. TIDSortedSymbolCollection = object(TSymbolCollection)
  114. function Compare(Key1, Key2: Pointer): Sw_Integer; virtual;
  115. procedure Insert(Item: Pointer); virtual;
  116. function SearchSymbolByID(AID: longint): PSymbol;
  117. end;
  118. TObjectSymbolCollection = object(TSortedCollection)
  119. function Compare(Key1, Key2: Pointer): Sw_Integer; virtual;
  120. function LookUp(const S: string; var Idx: sw_integer): string; virtual;
  121. function At(Index: Sw_Integer): PObjectSymbol;
  122. end;
  123. TReferenceCollection = object(TCollection)
  124. function At(Index: Sw_Integer): PReference;
  125. end;
  126. const
  127. Modules : PSymbolCollection = nil;
  128. ModuleNames : PModuleNameCollection = nil;
  129. TypeNames : PTypeNameCollection = nil;
  130. ObjectTree : PObjectSymbol = nil;
  131. procedure DisposeBrowserCol;
  132. procedure NewBrowserCol;
  133. procedure CreateBrowserCol;
  134. procedure InitBrowserCol;
  135. procedure DoneBrowserCol;
  136. procedure BuildObjectInfo;
  137. function SearchObjectForSymbol(O: PSymbol): PObjectSymbol;
  138. implementation
  139. uses
  140. Drivers,Views,App,
  141. aasm,globtype,globals,files,comphook;
  142. {****************************************************************************
  143. Helpers
  144. ****************************************************************************}
  145. function GetStr(P: PString): string;
  146. begin
  147. if P=nil then
  148. GetStr:=''
  149. else
  150. GetStr:=P^;
  151. end;
  152. function IntToStr(L: longint): string;
  153. var S: string;
  154. begin
  155. Str(L,S);
  156. IntToStr:=S;
  157. end;
  158. function UpcaseStr(S: string): string;
  159. var I: integer;
  160. begin
  161. for I:=1 to length(S) do
  162. S[I]:=Upcase(S[I]);
  163. UpcaseStr:=S;
  164. end;
  165. function FloatToStr(E: extended): string;
  166. var S: string;
  167. begin
  168. Str(E:0:24,S);
  169. if Pos('.',S)>0 then
  170. begin
  171. while (length(S)>0) and (S[length(S)]='0') do
  172. Delete(S,length(S),1);
  173. if (length(S)>0) and (S[length(S)]='.') then
  174. Delete(S,length(S),1);
  175. end;
  176. if S='' then S:='0';
  177. FloatToStr:=S;
  178. end;
  179. {****************************************************************************
  180. TStoreCollection
  181. ****************************************************************************}
  182. function TStoreCollection.Add(const S: string): PString;
  183. var P: PString;
  184. Index: Sw_integer;
  185. begin
  186. if S='' then P:=nil else
  187. if Search(@S,Index) then P:=At(Index) else
  188. begin
  189. P:=NewStr(S);
  190. Insert(P);
  191. end;
  192. Add:=P;
  193. end;
  194. {****************************************************************************
  195. TSymbolCollection
  196. ****************************************************************************}
  197. function TSymbolCollection.At(Index: Sw_Integer): PSymbol;
  198. begin
  199. At:=inherited At(Index);
  200. end;
  201. procedure TSymbolCollection.Insert(Item: Pointer);
  202. begin
  203. TCollection.Insert(Item);
  204. end;
  205. function TSymbolCollection.LookUp(const S: string; var Idx: sw_integer): string;
  206. begin
  207. Idx:=-1;
  208. LookUp:='';
  209. end;
  210. {****************************************************************************
  211. TReferenceCollection
  212. ****************************************************************************}
  213. function TReferenceCollection.At(Index: Sw_Integer): PReference;
  214. begin
  215. At:=inherited At(Index);
  216. end;
  217. {****************************************************************************
  218. TSortedSymbolCollection
  219. ****************************************************************************}
  220. function TSortedSymbolCollection.Compare(Key1, Key2: Pointer): Sw_Integer;
  221. var K1: PSymbol absolute Key1;
  222. K2: PSymbol absolute Key2;
  223. R: Sw_integer;
  224. S1,S2: string;
  225. begin
  226. S1:=Upper(K1^.GetName);
  227. S2:=Upper(K2^.GetName);
  228. if S1<S2 then R:=-1 else
  229. if S1>S2 then R:=1 else
  230. R:=0;
  231. Compare:=R;
  232. end;
  233. procedure TSortedSymbolCollection.Insert(Item: Pointer);
  234. begin
  235. TSortedCollection.Insert(Item);
  236. end;
  237. function TSortedSymbolCollection.LookUp(const S: string; var Idx: sw_integer): string;
  238. var OLI,ORI,Left,Right,Mid: integer;
  239. LeftP,RightP,MidP: PSymbol;
  240. RL: integer;
  241. LeftS,MidS,RightS: string;
  242. FoundS: string;
  243. UpS : string;
  244. begin
  245. Idx:=-1; FoundS:='';
  246. Left:=0; Right:=Count-1;
  247. UpS:=Upper(S);
  248. if Left<Right then
  249. begin
  250. while (Left<Right) do
  251. begin
  252. OLI:=Left; ORI:=Right;
  253. Mid:=Left+(Right-Left) div 2;
  254. LeftP:=At(Left); RightP:=At(Right); MidP:=At(Mid);
  255. LeftS:=Upper(LeftP^.GetName); MidS:=Upper(MidP^.GetName);
  256. RightS:=Upper(RightP^.GetName);
  257. if copy(MidS,1,length(UpS))=UpS then
  258. begin
  259. Idx:=Mid; FoundS:=copy(MidS,1,length(S));
  260. end;
  261. { else}
  262. if UpS<MidS then
  263. Right:=Mid
  264. else
  265. Left:=Mid;
  266. if (OLI=Left) and (ORI=Right) then
  267. Break;
  268. end;
  269. end;
  270. LookUp:=FoundS;
  271. end;
  272. {****************************************************************************
  273. TIDSortedSymbolCollection
  274. ****************************************************************************}
  275. function TIDSortedSymbolCollection.Compare(Key1, Key2: Pointer): Sw_Integer;
  276. var K1: PSymbol absolute Key1;
  277. K2: PSymbol absolute Key2;
  278. R: Sw_integer;
  279. begin
  280. if K1^.ObjectID<K2^.ObjectID then R:=-1 else
  281. if K1^.ObjectID>K2^.ObjectID then R:=1 else
  282. R:=0;
  283. Compare:=R;
  284. end;
  285. procedure TIDSortedSymbolCollection.Insert(Item: Pointer);
  286. begin
  287. TSortedCollection.Insert(Item);
  288. end;
  289. function TIDSortedSymbolCollection.SearchSymbolByID(AID: longint): PSymbol;
  290. var S: TSymbol;
  291. Index: sw_integer;
  292. P: PSymbol;
  293. begin
  294. S.ObjectID:=AID;
  295. if Search(@S,Index)=false then P:=nil else
  296. P:=At(Index);
  297. SearchSymbolByID:=P;
  298. end;
  299. {****************************************************************************
  300. TObjectSymbolCollection
  301. ****************************************************************************}
  302. function TObjectSymbolCollection.At(Index: Sw_Integer): PObjectSymbol;
  303. begin
  304. At:=inherited At(Index);
  305. end;
  306. function TObjectSymbolCollection.Compare(Key1, Key2: Pointer): Sw_Integer;
  307. var K1: PObjectSymbol absolute Key1;
  308. K2: PObjectSymbol absolute Key2;
  309. R: Sw_integer;
  310. S1,S2: string;
  311. begin
  312. S1:=Upper(K1^.GetName);
  313. S2:=Upper(K2^.GetName);
  314. if S1<S2 then R:=-1 else
  315. if S1>S2 then R:=1 else
  316. R:=0;
  317. Compare:=R;
  318. end;
  319. function TObjectSymbolCollection.LookUp(const S: string; var Idx: sw_integer): string;
  320. var OLI,ORI,Left,Right,Mid: integer;
  321. LeftP,RightP,MidP: PObjectSymbol;
  322. RL: integer;
  323. LeftS,MidS,RightS: string;
  324. FoundS: string;
  325. UpS : string;
  326. begin
  327. Idx:=-1; FoundS:='';
  328. Left:=0; Right:=Count-1;
  329. UpS:=Upper(S);
  330. if Left<Right then
  331. begin
  332. while (Left<Right) do
  333. begin
  334. OLI:=Left; ORI:=Right;
  335. Mid:=Left+(Right-Left) div 2;
  336. LeftP:=At(Left); RightP:=At(Right); MidP:=At(Mid);
  337. LeftS:=Upper(LeftP^.GetName); MidS:=Upper(MidP^.GetName);
  338. RightS:=Upper(RightP^.GetName);
  339. if copy(MidS,1,length(UpS))=UpS then
  340. begin
  341. Idx:=Mid; FoundS:=copy(MidS,1,length(S));
  342. end;
  343. { else}
  344. if UpS<MidS then
  345. Right:=Mid
  346. else
  347. Left:=Mid;
  348. if (OLI=Left) and (ORI=Right) then
  349. Break;
  350. end;
  351. end;
  352. LookUp:=FoundS;
  353. end;
  354. {****************************************************************************
  355. TReference
  356. ****************************************************************************}
  357. constructor TReference.Init(AFileName: PString; ALine, AColumn: Sw_integer);
  358. begin
  359. inherited Init;
  360. FileName:=AFileName;
  361. Position.X:=AColumn;
  362. Position.Y:=ALine;
  363. end;
  364. function TReference.GetFileName: string;
  365. begin
  366. GetFileName:=GetStr(FileName);
  367. end;
  368. destructor TReference.Done;
  369. begin
  370. inherited Done;
  371. end;
  372. {****************************************************************************
  373. TSymbol
  374. ****************************************************************************}
  375. constructor TSymbol.Init(const AName: string; ATyp: tsymtyp; AParams: string; AMemInfo: PSymbolMemInfo);
  376. begin
  377. inherited Init;
  378. Name:=NewStr(AName); Typ:=ATyp;
  379. if AMemInfo<>nil then
  380. SetMemInfo(AMemInfo^);
  381. New(References, Init(20,50));
  382. if ATyp in RecordTypes then
  383. begin
  384. Items:=New(PSortedSymbolCollection, Init(50,100));
  385. end;
  386. end;
  387. procedure TSymbol.SetMemInfo(const AMemInfo: TSymbolMemInfo);
  388. begin
  389. if MemInfo=nil then New(MemInfo);
  390. Move(AMemInfo,MemInfo^,SizeOf(MemInfo^));
  391. end;
  392. function TSymbol.GetReferenceCount: Sw_integer;
  393. var Count: Sw_integer;
  394. begin
  395. if References=nil then Count:=0 else
  396. Count:=References^.Count;
  397. GetReferenceCount:=Count;
  398. end;
  399. function TSymbol.GetReference(Index: Sw_integer): PReference;
  400. begin
  401. GetReference:=References^.At(Index);
  402. end;
  403. function TSymbol.GetItemCount: Sw_integer;
  404. var Count: Sw_integer;
  405. begin
  406. if Items=nil then Count:=0 else
  407. Count:=Items^.Count;
  408. GetItemCount:=Count;
  409. end;
  410. function TSymbol.GetItem(Index: Sw_integer): PSymbol;
  411. begin
  412. GetItem:=Items^.At(Index);
  413. end;
  414. function TSymbol.GetName: string;
  415. begin
  416. GetName:=GetStr(Name);
  417. end;
  418. function TSymbol.GetText: string;
  419. var S: string;
  420. I: Sw_integer;
  421. begin
  422. S:=GetTypeName;
  423. if length(S)>SymbolTypLen then
  424. S:=Copy(S,1,SymbolTypLen)
  425. else
  426. begin
  427. while length(S)<SymbolTypLen do
  428. S:=S+' ';
  429. end;
  430. S:=S+' '+GetName;
  431. if (Flags and sfRecord)<>0 then
  432. S:=S+' = record'
  433. else
  434. if (Flags and sfObject)<>0 then
  435. begin
  436. S:=S+' = ';
  437. if (Flags and sfClass)<>0 then
  438. S:=S+'class'
  439. else
  440. S:=S+'object';
  441. if Ancestor<>nil then
  442. S:=S+'('+Ancestor^.GetName+')';
  443. end
  444. else
  445. begin
  446. if Assigned(DType) then
  447. S:=S+' = '+DType^;
  448. if Assigned(Params) then
  449. S:=S+'('+Params^+')';
  450. if Assigned(VType) then
  451. S:=S+': '+VType^;
  452. end;
  453. GetText:=S;
  454. end;
  455. function TSymbol.GetTypeName: string;
  456. var S: string;
  457. begin
  458. case Typ of
  459. abstractsym : S:='abst';
  460. varsym : S:='var';
  461. typesym : S:='type';
  462. procsym : if VType=nil then
  463. S:='proc'
  464. else
  465. S:='func';
  466. unitsym : S:='unit';
  467. programsym : S:='prog';
  468. constsym : S:='const';
  469. enumsym : S:='enum';
  470. typedconstsym: S:='const';
  471. errorsym : S:='error';
  472. syssym : S:='sys';
  473. labelsym : S:='label';
  474. absolutesym : S:='abs';
  475. propertysym : S:='prop';
  476. funcretsym : S:='func';
  477. macrosym : S:='macro';
  478. else S:='';
  479. end;
  480. GetTypeName:=S;
  481. end;
  482. destructor TSymbol.Done;
  483. begin
  484. inherited Done;
  485. if assigned(MemInfo) then
  486. Dispose(MemInfo);
  487. if assigned(References) then
  488. Dispose(References, Done);
  489. if assigned(Items) then
  490. Dispose(Items, Done);
  491. if assigned(Name) then
  492. DisposeStr(Name);
  493. { if assigned(Params) then
  494. DisposeStr(Params);
  495. if assigned(VType) then
  496. DisposeStr(VType);
  497. if assigned(DType) then
  498. DisposeStr(DType);
  499. if assigned(Ancestor) then
  500. DisposeStr(Ancestor);}
  501. end;
  502. constructor TObjectSymbol.Init(AParent: PObjectSymbol; ASymbol: PSymbol);
  503. begin
  504. inherited Init;
  505. Parent:=AParent;
  506. Symbol:=ASymbol;
  507. end;
  508. constructor TObjectSymbol.InitName(const AName: string);
  509. begin
  510. inherited Init;
  511. Name:=NewStr(AName);
  512. end;
  513. function TObjectSymbol.GetName: string;
  514. begin
  515. if Name<>nil then
  516. GetName:=Name^
  517. else
  518. GetName:=Symbol^.GetName;
  519. end;
  520. function TObjectSymbol.GetDescendantCount: sw_integer;
  521. var Count: sw_integer;
  522. begin
  523. if Descendants=nil then Count:=0 else
  524. Count:=Descendants^.Count;
  525. GetDescendantCount:=Count;
  526. end;
  527. function TObjectSymbol.GetDescendant(Index: sw_integer): PObjectSymbol;
  528. begin
  529. GetDescendant:=Descendants^.At(Index);
  530. end;
  531. procedure TObjectSymbol.AddDescendant(P: PObjectSymbol);
  532. begin
  533. if Descendants=nil then
  534. New(Descendants, Init(50,10));
  535. Descendants^.Insert(P);
  536. end;
  537. destructor TObjectSymbol.Done;
  538. begin
  539. if Assigned(Name) then DisposeStr(Name); Name:=nil;
  540. if Assigned(Descendants) then Dispose(Descendants, Done); Descendants:=nil;
  541. inherited Done;
  542. end;
  543. {*****************************************************************************
  544. Main Routines
  545. *****************************************************************************}
  546. procedure DisposeBrowserCol;
  547. begin
  548. if assigned(Modules) then
  549. begin
  550. dispose(Modules,Done);
  551. Modules:=nil;
  552. end;
  553. if assigned(ModuleNames) then
  554. begin
  555. dispose(ModuleNames,Done);
  556. ModuleNames:=nil;
  557. end;
  558. if assigned(TypeNames) then
  559. begin
  560. dispose(TypeNames,Done);
  561. TypeNames:=nil;
  562. end;
  563. if assigned(ObjectTree) then
  564. begin
  565. Dispose(ObjectTree, Done);
  566. ObjectTree:=nil;
  567. end;
  568. end;
  569. procedure NewBrowserCol;
  570. begin
  571. New(Modules, Init(50,50));
  572. New(ModuleNames, Init(50,50));
  573. New(TypeNames, Init(1000,5000));
  574. end;
  575. procedure CreateBrowserCol;
  576. procedure ProcessSymTable(OwnerSym: PSymbol; var Owner: PSymbolCollection; Table: PSymTable);
  577. var I,J,defcount,symcount: longint;
  578. Ref: PRef;
  579. Sym,ParSym: PSym;
  580. Symbol: PSymbol;
  581. Reference: PReference;
  582. ParamCount: Sw_integer;
  583. Params: array[0..20] of PString;
  584. inputfile : pinputfile;
  585. Idx: sw_integer;
  586. S: string;
  587. procedure SetVType(Symbol: PSymbol; VType: string);
  588. begin
  589. Symbol^.VType:=TypeNames^.Add(VType);
  590. end;
  591. procedure SetDType(Symbol: PSymbol; DType: string);
  592. begin
  593. Symbol^.DType:=TypeNames^.Add(DType);
  594. end;
  595. function GetDefinitionStr(def: pdef): string; forward;
  596. function GetEnumDefStr(def: penumdef): string;
  597. var Name: string;
  598. esym: penumsym;
  599. Count: integer;
  600. begin
  601. Name:='(';
  602. esym:=def^.First; Count:=0;
  603. while (esym<>nil) do
  604. begin
  605. if Count>0 then Name:=Name+', ';
  606. Name:=Name+esym^.name;
  607. esym:=esym^.next; Inc(Count);
  608. end;
  609. Name:=Name+')';
  610. GetEnumDefStr:=Name;
  611. end;
  612. function GetArrayDefStr(def: parraydef): string;
  613. var Name: string;
  614. begin
  615. Name:='array ['+IntToStr(def^.lowrange)+'..'+IntToStr(def^.highrange)+'] of ';
  616. if assigned(def^.definition) then
  617. Name:=Name+GetDefinitionStr(def^.definition);
  618. GetArrayDefStr:=Name;
  619. end;
  620. function GetFileDefStr(def: pfiledef): string;
  621. var Name: string;
  622. begin
  623. Name:='';
  624. case def^.filetype of
  625. ft_text : Name:='text';
  626. ft_untyped : Name:='file';
  627. ft_typed : Name:='file of '+GetDefinitionStr(def^.typed_as);
  628. end;
  629. GetFileDefStr:=Name;
  630. end;
  631. function GetStringDefStr(def: pstringdef): string;
  632. var Name: string;
  633. begin
  634. Name:='';
  635. case def^.string_typ of
  636. st_shortstring :
  637. if def^.len=255 then
  638. Name:='shortstring'
  639. else
  640. Name:='string['+IntToStr(def^.len)+']';
  641. st_longstring :
  642. Name:='longstring';
  643. st_ansistring :
  644. Name:='ansistring';
  645. st_widestring :
  646. Name:='widestring';
  647. else ;
  648. end;
  649. GetStringDefStr:=Name;
  650. end;
  651. function retdefassigned(def: pabstractprocdef): boolean;
  652. var OK: boolean;
  653. begin
  654. OK:=false;
  655. if assigned(def^.retdef) then
  656. if UpcaseStr(GetDefinitionStr(def^.retdef))<>'VOID' then
  657. OK:=true;
  658. retdefassigned:=OK;
  659. end;
  660. function GetAbsProcParmDefStr(def: pabstractprocdef): string;
  661. var Name: string;
  662. dc: pdefcoll;
  663. Count: integer;
  664. CurName: string;
  665. begin
  666. Name:='';
  667. dc:=def^.para1; Count:=0;
  668. while dc<>nil do
  669. begin
  670. CurName:='';
  671. case dc^.paratyp of
  672. vs_Value : ;
  673. vs_Const : CurName:=CurName+'const ';
  674. vs_Var : CurName:=CurName+'var ';
  675. end;
  676. if assigned(dc^.data) then
  677. CurName:=CurName+GetDefinitionStr(dc^.data);
  678. if dc^.next<>nil then
  679. CurName:=', '+CurName;
  680. Name:=CurName+Name;
  681. dc:=dc^.next; Inc(Count);
  682. end;
  683. GetAbsProcParmDefStr:=Name;
  684. end;
  685. function GetAbsProcDefStr(def: pabstractprocdef): string;
  686. var Name: string;
  687. begin
  688. Name:=GetAbsProcParmDefStr(def);
  689. if Name<>'' then Name:='('+Name+')';
  690. if retdefassigned(def) then
  691. Name:='function'+Name+': '+GetDefinitionStr(def^.retdef)
  692. else
  693. Name:='procedure'+Name;
  694. GetAbsProcDefStr:=Name;
  695. end;
  696. function GetProcDefStr(def: pprocdef): string;
  697. var DName: string;
  698. J: integer;
  699. begin
  700. { DName:='';
  701. if assigned(def) then
  702. begin
  703. if assigned(def^.parast) then
  704. begin
  705. with def^.parast^ do
  706. for J:=1 to number_symbols do
  707. begin
  708. if J<>1 then DName:=DName+', ';
  709. ParSym:=GetsymNr(J);
  710. if ParSym=nil then Break;
  711. DName:=DName+ParSym^.Name;
  712. end;
  713. end
  714. end;}
  715. DName:=GetAbsProcDefStr(def);
  716. GetProcDefStr:=DName;
  717. end;
  718. function GetProcVarDefStr(def: pprocvardef): string;
  719. begin
  720. GetProcVarDefStr:=GetAbsProcDefStr(def);
  721. end;
  722. function GetSetDefStr(def: psetdef): string;
  723. var Name: string;
  724. begin
  725. Name:='';
  726. case def^.settype of
  727. normset : Name:='set';
  728. smallset : Name:='set';
  729. varset : Name:='varset';
  730. end;
  731. Name:=Name+' of ';
  732. Name:=Name+GetDefinitionStr(def^.setof);
  733. GetSetDefStr:=Name;
  734. end;
  735. function GetDefinitionStr(def: pdef): string;
  736. var Name: string;
  737. sym: psym;
  738. begin
  739. Name:='';
  740. if def<>nil then
  741. begin
  742. if assigned(def^.sym) then
  743. Name:=def^.sym^.name;
  744. if Name='' then
  745. case def^.deftype of
  746. arraydef :
  747. Name:=GetArrayDefStr(parraydef(def));
  748. stringdef :
  749. Name:=GetStringDefStr(pstringdef(def));
  750. enumdef :
  751. Name:=GetEnumDefStr(penumdef(def));
  752. procdef :
  753. Name:=GetProcDefStr(pprocdef(def));
  754. procvardef :
  755. Name:=GetProcVarDefStr(pprocvardef(def));
  756. filedef :
  757. Name:=GetFileDefStr(pfiledef(def));
  758. setdef :
  759. Name:=GetSetDefStr(psetdef(def));
  760. end;
  761. end;
  762. GetDefinitionStr:=Name;
  763. end;
  764. function GetEnumItemName(Sym: penumsym): string;
  765. var Name: string;
  766. ES: penumsym;
  767. begin
  768. Name:='';
  769. if assigned(sym) and assigned(sym^.definition) then
  770. if assigned(sym^.definition^.sym) then
  771. begin
  772. { ES:=sym^.definition^.First;
  773. while (ES<>nil) and (ES^.Value<>sym^.Value) do
  774. ES:=ES^.next;
  775. if assigned(es) and (es^.value=sym^.value) then
  776. Name:=}
  777. Name:=sym^.definition^.sym^.name;
  778. if Name<>'' then
  779. Name:=Name+'('+IntToStr(sym^.value)+')';
  780. end;
  781. GetEnumItemName:=Name;
  782. end;
  783. function GetConstValueName(sym: pconstsym): string;
  784. var Name: string;
  785. begin
  786. Name:='';
  787. { if assigned(sym^.definition) then
  788. if assigned(sym^.definition^.sym) then
  789. Name:=sym^.definition^.sym^.name;}
  790. if Name='' then
  791. case sym^.consttype of
  792. constord :
  793. Name:=sym^.definition^.sym^.name+'('+IntToStr(sym^.value)+')';
  794. conststring :
  795. Name:=''''+GetStr(PString(sym^.Value))+'''';
  796. constreal:
  797. Name:=FloatToStr(PBestReal(sym^.Value)^);
  798. constbool:
  799. { if boolean(sym^.Value)=true then
  800. Name:='TRUE'
  801. else
  802. Name:='FALSE';}
  803. Name:='Longbool('+IntToStr(sym^.Value)+')';
  804. constint:
  805. Name:=IntToStr(sym^.value);
  806. constchar:
  807. Name:=''''+chr(sym^.Value)+'''';
  808. constset:
  809. { Name:=SetToStr(pnormalset(sym^.Value))};
  810. constnil: ;
  811. end;
  812. GetConstValueName:=Name;
  813. end;
  814. procedure ProcessDefIfStruct(definition: pdef);
  815. begin
  816. if assigned(definition) then
  817. begin
  818. case definition^.deftype of
  819. recorddef :
  820. if precdef(definition)^.symtable<>Table then
  821. ProcessSymTable(Symbol,Symbol^.Items,precdef(definition)^.symtable);
  822. objectdef :
  823. if precdef(definition)^.symtable<>Table then
  824. ProcessSymTable(Symbol,Symbol^.Items,pobjectdef(definition)^.publicsyms);
  825. { leads to infinite loops !!
  826. pointerdef :
  827. with ppointerdef(definition)^ do
  828. if assigned(definition) then
  829. if assigned(definition^.sym) then
  830. ProcessDefIfStruct(definition^.sym^.definition);}
  831. end;
  832. end;
  833. end;
  834. var MemInfo: TSymbolMemInfo;
  835. ObjDef: pobjectdef;
  836. begin
  837. if not Assigned(Table) then
  838. Exit;
  839. if Owner=nil then
  840. Owner:=New(PSortedSymbolCollection, Init(10,50));
  841. defcount:=Table^.number_defs;
  842. symcount:=Table^.number_symbols;
  843. { for I:=0 to defcount-1 do
  844. begin
  845. Def:=Table^.GetDefNr(I);
  846. end;}
  847. for I:=1 to symcount do
  848. begin
  849. Sym:=Table^.GetsymNr(I);
  850. if Sym=nil then Continue;
  851. ParamCount:=0;
  852. New(Symbol, Init(Sym^.Name,Sym^.Typ,'',nil));
  853. case Sym^.Typ of
  854. varsym :
  855. with pvarsym(sym)^ do
  856. begin
  857. if assigned(definition) then
  858. if assigned(definition^.sym) then
  859. SetVType(Symbol,definition^.sym^.name)
  860. else
  861. SetVType(Symbol,GetDefinitionStr(definition));
  862. ProcessDefIfStruct(definition);
  863. MemInfo.Addr:=address;
  864. MemInfo.LocalAddr:=localaddress;
  865. MemInfo.Size:=getsize;
  866. MemInfo.PushSize:=getpushsize;
  867. Symbol^.SetMemInfo(MemInfo);
  868. end;
  869. constsym :
  870. SetDType(Symbol,GetConstValueName(pconstsym(sym)));
  871. enumsym :
  872. if assigned(penumsym(sym)^.definition) then
  873. SetDType(Symbol,GetEnumItemName(penumsym(sym)));
  874. unitsym :
  875. begin
  876. { ProcessSymTable(Symbol^.Items,punitsym(sym)^.unitsymtable);}
  877. end;
  878. syssym :
  879. { if assigned(Table^.Name) then
  880. if Table^.Name^='SYSTEM' then}
  881. begin
  882. Symbol^.Params:=TypeNames^.Add('...');
  883. end;
  884. funcretsym :
  885. if Assigned(OwnerSym) then
  886. with pfuncretsym(sym)^ do
  887. if assigned(funcretdef) then
  888. if assigned(funcretdef^.sym) then
  889. SetVType(OwnerSym,funcretdef^.sym^.name);
  890. procsym :
  891. begin
  892. with pprocsym(sym)^ do
  893. if assigned(definition) then
  894. begin
  895. ProcessSymTable(Symbol,Symbol^.Items,definition^.parast);
  896. if assigned(definition^.parast) then
  897. begin
  898. Symbol^.Params:=TypeNames^.Add(GetAbsProcParmDefStr(definition));
  899. end
  900. else { param-definition is NOT assigned }
  901. if assigned(Table^.Name) then
  902. if Table^.Name^='SYSTEM' then
  903. begin
  904. Symbol^.Params:=TypeNames^.Add('...');
  905. end;
  906. if assigned(definition^.localst) and
  907. (definition^.localst^.symtabletype<>staticsymtable) then
  908. ProcessSymTable(Symbol,Symbol^.Items,definition^.localst);
  909. end;
  910. end;
  911. typesym :
  912. begin
  913. with ptypesym(sym)^ do
  914. if assigned(definition) then
  915. case definition^.deftype of
  916. arraydef :
  917. SetDType(Symbol,GetArrayDefStr(parraydef(definition)));
  918. enumdef :
  919. SetDType(Symbol,GetEnumDefStr(penumdef(definition)));
  920. procdef :
  921. SetDType(Symbol,GetProcDefStr(pprocdef(definition)));
  922. procvardef :
  923. SetDType(Symbol,GetProcVarDefStr(pprocvardef(definition)));
  924. objectdef :
  925. with pobjectdef(definition)^ do
  926. begin
  927. ObjDef:=childof;
  928. Symbol^.ObjectID:=longint(definition);
  929. if ObjDef<>nil then
  930. Symbol^.AncestorID:=longint(ObjDef);{TypeNames^.Add(S);}
  931. Symbol^.Flags:=(Symbol^.Flags or sfObject);
  932. if (options and oo_is_class)<>0 then
  933. Symbol^.Flags:=(Symbol^.Flags or sfClass);
  934. ProcessSymTable(Symbol,Symbol^.Items,pobjectdef(definition)^.publicsyms);
  935. end;
  936. recorddef :
  937. begin
  938. Symbol^.Flags:=(Symbol^.Flags or sfRecord);
  939. ProcessSymTable(Symbol,Symbol^.Items,precdef(definition)^.symtable);
  940. end;
  941. filedef :
  942. SetDType(Symbol,GetFileDefStr(pfiledef(definition)));
  943. setdef :
  944. SetDType(Symbol,GetSetDefStr(psetdef(definition)));
  945. end;
  946. end;
  947. end;
  948. Ref:=Sym^.defref;
  949. while Assigned(Symbol) and assigned(Ref) do
  950. begin
  951. inputfile:=get_source_file(ref^.moduleindex,ref^.posinfo.fileindex);
  952. if Assigned(inputfile) and Assigned(inputfile^.name) then
  953. begin
  954. New(Reference, Init(ModuleNames^.Add(inputfile^.name^),
  955. ref^.posinfo.line,ref^.posinfo.column));
  956. Symbol^.References^.Insert(Reference);
  957. end;
  958. Ref:=Ref^.nextref;
  959. end;
  960. if Assigned(Symbol) then
  961. Owner^.Insert(Symbol);
  962. end;
  963. end;
  964. var
  965. T: PSymTable;
  966. UnitS: PSymbol;
  967. hp : pmodule;
  968. begin
  969. DisposeBrowserCol;
  970. NewBrowserCol;
  971. hp:=pmodule(loaded_units.first);
  972. while assigned(hp) do
  973. begin
  974. t:=psymtable(hp^.globalsymtable);
  975. if assigned(t) then
  976. begin
  977. New(UnitS, Init(T^.Name^,unitsym,'',nil));
  978. Modules^.Insert(UnitS);
  979. ProcessSymTable(UnitS,UnitS^.Items,T);
  980. if cs_local_browser in aktmoduleswitches then
  981. begin
  982. t:=psymtable(hp^.localsymtable);
  983. if assigned(t) then
  984. ProcessSymTable(UnitS,UnitS^.Items,T);
  985. end;
  986. end;
  987. hp:=pmodule(hp^.next);
  988. end;
  989. BuildObjectInfo;
  990. end;
  991. procedure BuildObjectInfo;
  992. var C: PIDSortedSymbolCollection;
  993. ObjectC: PObjectSymbolCollection;
  994. ObjectsSymbol: PObjectSymbol;
  995. procedure InsertSymbolCollection(Symbols: PSymbolCollection);
  996. var I: sw_integer;
  997. P: PSymbol;
  998. begin
  999. for I:=0 to Symbols^.Count-1 do
  1000. begin
  1001. P:=Symbols^.At(I);
  1002. if (P^.Flags and sfObject)<>0 then
  1003. C^.Insert(P);
  1004. if P^.Items<>nil then
  1005. InsertSymbolCollection(P^.Items);
  1006. end;
  1007. end;
  1008. function SearchObjectForSymbol(O: PSymbol): PObjectSymbol;
  1009. var I,Idx: sw_integer;
  1010. OS,P: PObjectSymbol;
  1011. begin
  1012. P:=nil;
  1013. for I:=0 to ObjectC^.Count-1 do
  1014. begin
  1015. OS:=ObjectC^.At(I);
  1016. if OS^.Symbol=O then
  1017. begin P:=OS; Break; end;
  1018. end;
  1019. SearchObjectForSymbol:=P;
  1020. end;
  1021. procedure BuildTree;
  1022. var I: sw_integer;
  1023. Symbol: PSymbol;
  1024. Parent,OS: PObjectSymbol;
  1025. begin
  1026. I:=0;
  1027. while (I<C^.Count) do
  1028. begin
  1029. Symbol:=C^.At(I);
  1030. if Symbol^.Ancestor=nil then
  1031. Parent:=ObjectsSymbol
  1032. else
  1033. Parent:=SearchObjectForSymbol(Symbol^.Ancestor);
  1034. if Parent<>nil then
  1035. begin
  1036. New(OS, Init(Parent, Symbol));
  1037. Parent^.AddDescendant(OS);
  1038. ObjectC^.Insert(OS);
  1039. C^.AtDelete(I);
  1040. end
  1041. else
  1042. Inc(I);
  1043. end;
  1044. end;
  1045. var Pass: integer;
  1046. I: sw_integer;
  1047. P: PSymbol;
  1048. begin
  1049. New(C, Init(1000,5000));
  1050. InsertSymbolCollection(Modules);
  1051. { --- Resolve ancestor<->descendant references --- }
  1052. for I:=0 to C^.Count-1 do
  1053. begin
  1054. P:=C^.At(I);
  1055. if P^.AncestorID<>0 then
  1056. P^.Ancestor:=C^.SearchSymbolByID(P^.AncestorID);
  1057. end;
  1058. { --- Build object tree --- }
  1059. if assigned(ObjectTree) then Dispose(ObjectTree, Done);
  1060. New(ObjectsSymbol, InitName('Objects'));
  1061. ObjectTree:=ObjectsSymbol;
  1062. New(ObjectC, Init(C^.Count,100));
  1063. Pass:=0;
  1064. if C^.Count>0 then
  1065. repeat
  1066. BuildTree;
  1067. Inc(Pass);
  1068. until (C^.Count=0) or (Pass>20); { more than 20 levels ? - then there must be a bug }
  1069. ObjectC^.DeleteAll; Dispose(ObjectC, Done);
  1070. C^.DeleteAll; Dispose(C, Done);
  1071. end;
  1072. function SearchObjectForSymbol(O: PSymbol): PObjectSymbol;
  1073. var I,Idx: sw_integer;
  1074. OS,P: PObjectSymbol;
  1075. ObjectC: PObjectSymbolCollection;
  1076. begin
  1077. P:=nil;
  1078. if ObjectTree<>nil then
  1079. begin
  1080. ObjectC:=ObjectTree^.Descendants;
  1081. for I:=0 to ObjectC^.Count-1 do
  1082. begin
  1083. OS:=ObjectC^.At(I);
  1084. if OS^.Symbol=O then
  1085. begin P:=OS; Break; end;
  1086. end;
  1087. end;
  1088. SearchObjectForSymbol:=P;
  1089. end;
  1090. {*****************************************************************************
  1091. Initialize
  1092. *****************************************************************************}
  1093. var
  1094. oldexit : pointer;
  1095. procedure browcol_exit;{$ifndef FPC}far;{$endif}
  1096. begin
  1097. exitproc:=oldexit;
  1098. DisposeBrowserCol;
  1099. end;
  1100. procedure InitBrowserCol;
  1101. begin
  1102. end;
  1103. procedure DoneBrowserCol;
  1104. begin
  1105. { nothing, the collections are freed in the exitproc }
  1106. end;
  1107. begin
  1108. oldexit:=exitproc;
  1109. exitproc:=@browcol_exit;
  1110. end.
  1111. {
  1112. $Log$
  1113. Revision 1.11 1999-04-08 10:17:42 peter
  1114. + objects support
  1115. Revision 1.8 1999/03/03 01:38:11 pierre
  1116. * avoid infinite recursion in ProcessDefIfStruct
  1117. Revision 1.7 1999/02/22 11:51:32 peter
  1118. * browser updates from gabor
  1119. Revision 1.6 1999/02/04 09:31:59 pierre
  1120. + added objects and records symbol tables
  1121. Revision 1.5 1999/02/03 09:44:32 pierre
  1122. * symbol nubering begins with 1 in number_symbols
  1123. * program tmodule has globalsymtable for its staticsymtable
  1124. (to get it displayed in IDE globals list)
  1125. + list of symbol (browcol) greatly improved for IDE
  1126. Revision 1.4 1999/02/02 16:38:38 peter
  1127. * no endless loop with localst=staticsymtable
  1128. Revision 1.3 1999/01/22 10:19:43 peter
  1129. * fixed typo
  1130. Revision 1.2 1999/01/21 11:49:14 peter
  1131. * updates from gabor
  1132. Revision 1.1 1999/01/12 14:25:24 peter
  1133. + BrowserLog for browser.log generation
  1134. + BrowserCol for browser info in TCollections
  1135. * released all other UseBrowser
  1136. }