browcol.pas 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327
  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. Verbose,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. begin
  231. S1:=K1^.GetName;
  232. S2:=K2^.GetName;
  233. if S1<S2 then R:=-1 else
  234. if S1>S2 then R:=1 else
  235. R:=0;
  236. end;
  237. Compare:=R;
  238. end;
  239. procedure TSortedSymbolCollection.Insert(Item: Pointer);
  240. begin
  241. TSortedCollection.Insert(Item);
  242. end;
  243. function TSortedSymbolCollection.LookUp(const S: string; var Idx: sw_integer): string;
  244. var OLI,ORI,Left,Right,Mid: integer;
  245. LeftP,RightP,MidP: PSymbol;
  246. RL: integer;
  247. LeftS,MidS,RightS: string;
  248. FoundS: string;
  249. UpS : string;
  250. begin
  251. Idx:=-1; FoundS:='';
  252. Left:=0; Right:=Count-1;
  253. UpS:=Upper(S);
  254. if Left<Right then
  255. begin
  256. while (Left<Right) do
  257. begin
  258. OLI:=Left; ORI:=Right;
  259. Mid:=Left+(Right-Left) div 2;
  260. LeftP:=At(Left); RightP:=At(Right); MidP:=At(Mid);
  261. LeftS:=Upper(LeftP^.GetName);
  262. MidS:=Upper(MidP^.GetName);
  263. RightS:=Upper(RightP^.GetName);
  264. if copy(MidS,1,length(UpS))=UpS then
  265. begin
  266. Idx:=Mid; FoundS:=UpS{copy(MidS,1,length(S)) same and easier };
  267. end;
  268. { else}
  269. if UpS<MidS then
  270. Right:=Mid
  271. else
  272. Left:=Mid;
  273. if (OLI=Left) and (ORI=Right) then
  274. Break;
  275. end;
  276. end;
  277. LookUp:=FoundS;
  278. end;
  279. {****************************************************************************
  280. TIDSortedSymbolCollection
  281. ****************************************************************************}
  282. function TIDSortedSymbolCollection.Compare(Key1, Key2: Pointer): Sw_Integer;
  283. var K1: PSymbol absolute Key1;
  284. K2: PSymbol absolute Key2;
  285. R: Sw_integer;
  286. begin
  287. if K1^.ObjectID<K2^.ObjectID then R:=-1 else
  288. if K1^.ObjectID>K2^.ObjectID then R:=1 else
  289. R:=0;
  290. Compare:=R;
  291. end;
  292. procedure TIDSortedSymbolCollection.Insert(Item: Pointer);
  293. begin
  294. TSortedCollection.Insert(Item);
  295. end;
  296. function TIDSortedSymbolCollection.SearchSymbolByID(AID: longint): PSymbol;
  297. var S: TSymbol;
  298. Index: sw_integer;
  299. P: PSymbol;
  300. begin
  301. S.ObjectID:=AID;
  302. if Search(@S,Index)=false then P:=nil else
  303. P:=At(Index);
  304. SearchSymbolByID:=P;
  305. end;
  306. {****************************************************************************
  307. TObjectSymbolCollection
  308. ****************************************************************************}
  309. function TObjectSymbolCollection.At(Index: Sw_Integer): PObjectSymbol;
  310. begin
  311. At:=inherited At(Index);
  312. end;
  313. function TObjectSymbolCollection.Compare(Key1, Key2: Pointer): Sw_Integer;
  314. var K1: PObjectSymbol absolute Key1;
  315. K2: PObjectSymbol absolute Key2;
  316. R: Sw_integer;
  317. S1,S2: string;
  318. begin
  319. S1:=Upper(K1^.GetName);
  320. S2:=Upper(K2^.GetName);
  321. if S1<S2 then R:=-1 else
  322. if S1>S2 then R:=1 else
  323. begin
  324. S1:=K1^.GetName;
  325. S2:=K2^.GetName;
  326. if S1<S2 then R:=-1 else
  327. if S1>S2 then R:=1 else
  328. R:=0;
  329. end;
  330. Compare:=R;
  331. end;
  332. function TObjectSymbolCollection.LookUp(const S: string; var Idx: sw_integer): string;
  333. var OLI,ORI,Left,Right,Mid: integer;
  334. LeftP,RightP,MidP: PObjectSymbol;
  335. RL: integer;
  336. LeftS,MidS,RightS: string;
  337. FoundS: string;
  338. UpS : string;
  339. begin
  340. Idx:=-1; FoundS:='';
  341. Left:=0; Right:=Count-1;
  342. UpS:=Upper(S);
  343. if Left<Right then
  344. begin
  345. while (Left<Right) do
  346. begin
  347. OLI:=Left; ORI:=Right;
  348. Mid:=Left+(Right-Left) div 2;
  349. LeftP:=At(Left); RightP:=At(Right); MidP:=At(Mid);
  350. LeftS:=Upper(LeftP^.GetName);
  351. MidS:=Upper(MidP^.GetName);
  352. RightS:=Upper(RightP^.GetName);
  353. if copy(MidS,1,length(UpS))=UpS then
  354. begin
  355. Idx:=Mid; FoundS:=UpS;
  356. end;
  357. { else}
  358. if UpS<MidS then
  359. Right:=Mid
  360. else
  361. Left:=Mid;
  362. if (OLI=Left) and (ORI=Right) then
  363. Break;
  364. end;
  365. end;
  366. LookUp:=FoundS;
  367. end;
  368. {****************************************************************************
  369. TReference
  370. ****************************************************************************}
  371. constructor TReference.Init(AFileName: PString; ALine, AColumn: Sw_integer);
  372. begin
  373. inherited Init;
  374. FileName:=AFileName;
  375. Position.X:=AColumn;
  376. Position.Y:=ALine;
  377. end;
  378. function TReference.GetFileName: string;
  379. begin
  380. GetFileName:=GetStr(FileName);
  381. end;
  382. destructor TReference.Done;
  383. begin
  384. inherited Done;
  385. end;
  386. {****************************************************************************
  387. TSymbol
  388. ****************************************************************************}
  389. constructor TSymbol.Init(const AName: string; ATyp: tsymtyp; AParams: string; AMemInfo: PSymbolMemInfo);
  390. begin
  391. inherited Init;
  392. Name:=NewStr(AName); Typ:=ATyp;
  393. if AMemInfo<>nil then
  394. SetMemInfo(AMemInfo^);
  395. New(References, Init(20,50));
  396. if ATyp in RecordTypes then
  397. begin
  398. Items:=New(PSortedSymbolCollection, Init(50,100));
  399. end;
  400. end;
  401. procedure TSymbol.SetMemInfo(const AMemInfo: TSymbolMemInfo);
  402. begin
  403. if MemInfo=nil then New(MemInfo);
  404. Move(AMemInfo,MemInfo^,SizeOf(MemInfo^));
  405. end;
  406. function TSymbol.GetReferenceCount: Sw_integer;
  407. var Count: Sw_integer;
  408. begin
  409. if References=nil then Count:=0 else
  410. Count:=References^.Count;
  411. GetReferenceCount:=Count;
  412. end;
  413. function TSymbol.GetReference(Index: Sw_integer): PReference;
  414. begin
  415. GetReference:=References^.At(Index);
  416. end;
  417. function TSymbol.GetItemCount: Sw_integer;
  418. var Count: Sw_integer;
  419. begin
  420. if Items=nil then Count:=0 else
  421. Count:=Items^.Count;
  422. GetItemCount:=Count;
  423. end;
  424. function TSymbol.GetItem(Index: Sw_integer): PSymbol;
  425. begin
  426. GetItem:=Items^.At(Index);
  427. end;
  428. function TSymbol.GetName: string;
  429. begin
  430. GetName:=GetStr(Name);
  431. end;
  432. function TSymbol.GetText: string;
  433. var S: string;
  434. I: Sw_integer;
  435. begin
  436. S:=GetTypeName;
  437. if length(S)>SymbolTypLen then
  438. S:=Copy(S,1,SymbolTypLen)
  439. else
  440. begin
  441. while length(S)<SymbolTypLen do
  442. S:=S+' ';
  443. end;
  444. S:=S+' '+GetName;
  445. if (Flags and sfRecord)<>0 then
  446. S:=S+' = record'
  447. else
  448. if (Flags and sfObject)<>0 then
  449. begin
  450. S:=S+' = ';
  451. if (Flags and sfClass)<>0 then
  452. S:=S+'class'
  453. else
  454. S:=S+'object';
  455. if Ancestor<>nil then
  456. S:=S+'('+Ancestor^.GetName+')';
  457. end
  458. else
  459. begin
  460. if Assigned(DType) then
  461. S:=S+' = '+DType^;
  462. if Assigned(Params) then
  463. S:=S+'('+Params^+')';
  464. if Assigned(VType) then
  465. S:=S+': '+VType^;
  466. end;
  467. GetText:=S;
  468. end;
  469. function TSymbol.GetTypeName: string;
  470. var S: string;
  471. begin
  472. case Typ of
  473. abstractsym : S:='abst';
  474. varsym : S:='var';
  475. typesym : S:='type';
  476. procsym : if VType=nil then
  477. S:='proc'
  478. else
  479. S:='func';
  480. unitsym : S:='unit';
  481. programsym : S:='prog';
  482. constsym : S:='const';
  483. enumsym : S:='enum';
  484. typedconstsym: S:='const';
  485. errorsym : S:='error';
  486. syssym : S:='sys';
  487. labelsym : S:='label';
  488. absolutesym : S:='abs';
  489. propertysym : S:='prop';
  490. funcretsym : S:='res';
  491. macrosym : S:='macro';
  492. else S:='';
  493. end;
  494. GetTypeName:=S;
  495. end;
  496. destructor TSymbol.Done;
  497. begin
  498. inherited Done;
  499. if assigned(MemInfo) then
  500. Dispose(MemInfo);
  501. if assigned(References) then
  502. Dispose(References, Done);
  503. if assigned(Items) then
  504. Dispose(Items, Done);
  505. if assigned(Name) then
  506. DisposeStr(Name);
  507. { if assigned(Params) then
  508. DisposeStr(Params);
  509. if assigned(VType) then
  510. DisposeStr(VType);
  511. if assigned(DType) then
  512. DisposeStr(DType);
  513. if assigned(Ancestor) then
  514. DisposeStr(Ancestor);}
  515. end;
  516. constructor TObjectSymbol.Init(AParent: PObjectSymbol; ASymbol: PSymbol);
  517. begin
  518. inherited Init;
  519. Parent:=AParent;
  520. Symbol:=ASymbol;
  521. end;
  522. constructor TObjectSymbol.InitName(const AName: string);
  523. begin
  524. inherited Init;
  525. Name:=NewStr(AName);
  526. end;
  527. function TObjectSymbol.GetName: string;
  528. begin
  529. if Name<>nil then
  530. GetName:=Name^
  531. else
  532. GetName:=Symbol^.GetName;
  533. end;
  534. function TObjectSymbol.GetDescendantCount: sw_integer;
  535. var Count: sw_integer;
  536. begin
  537. if Descendants=nil then Count:=0 else
  538. Count:=Descendants^.Count;
  539. GetDescendantCount:=Count;
  540. end;
  541. function TObjectSymbol.GetDescendant(Index: sw_integer): PObjectSymbol;
  542. begin
  543. GetDescendant:=Descendants^.At(Index);
  544. end;
  545. procedure TObjectSymbol.AddDescendant(P: PObjectSymbol);
  546. begin
  547. if Descendants=nil then
  548. New(Descendants, Init(50,10));
  549. Descendants^.Insert(P);
  550. end;
  551. destructor TObjectSymbol.Done;
  552. begin
  553. if Assigned(Name) then DisposeStr(Name); Name:=nil;
  554. if Assigned(Descendants) then Dispose(Descendants, Done); Descendants:=nil;
  555. inherited Done;
  556. end;
  557. {*****************************************************************************
  558. Main Routines
  559. *****************************************************************************}
  560. procedure DisposeBrowserCol;
  561. begin
  562. if assigned(Modules) then
  563. begin
  564. dispose(Modules,Done);
  565. Modules:=nil;
  566. end;
  567. if assigned(ModuleNames) then
  568. begin
  569. dispose(ModuleNames,Done);
  570. ModuleNames:=nil;
  571. end;
  572. if assigned(TypeNames) then
  573. begin
  574. dispose(TypeNames,Done);
  575. TypeNames:=nil;
  576. end;
  577. if assigned(ObjectTree) then
  578. begin
  579. Dispose(ObjectTree, Done);
  580. ObjectTree:=nil;
  581. end;
  582. end;
  583. procedure NewBrowserCol;
  584. begin
  585. New(Modules, Init(50,50));
  586. New(ModuleNames, Init(50,50));
  587. New(TypeNames, Init(1000,5000));
  588. end;
  589. procedure CreateBrowserCol;
  590. procedure ProcessSymTable(OwnerSym: PSymbol; var Owner: PSymbolCollection; Table: PSymTable);
  591. var I,J,defcount,symcount: longint;
  592. Ref: PRef;
  593. Sym,ParSym: PSym;
  594. Symbol: PSymbol;
  595. Reference: PReference;
  596. ParamCount: Sw_integer;
  597. Params: array[0..20] of PString;
  598. inputfile : pinputfile;
  599. Idx: sw_integer;
  600. S: string;
  601. procedure SetVType(Symbol: PSymbol; VType: string);
  602. begin
  603. Symbol^.VType:=TypeNames^.Add(VType);
  604. end;
  605. procedure SetDType(Symbol: PSymbol; DType: string);
  606. begin
  607. Symbol^.DType:=TypeNames^.Add(DType);
  608. end;
  609. function GetDefinitionStr(def: pdef): string; forward;
  610. function GetEnumDefStr(def: penumdef): string;
  611. var Name: string;
  612. esym: penumsym;
  613. Count: integer;
  614. begin
  615. Name:='(';
  616. esym:=def^.Firstenum; Count:=0;
  617. while (esym<>nil) do
  618. begin
  619. if Count>0 then
  620. Name:=Name+', ';
  621. Name:=Name+esym^.name;
  622. esym:=esym^.nextenum;
  623. Inc(Count);
  624. end;
  625. Name:=Name+')';
  626. GetEnumDefStr:=Name;
  627. end;
  628. function GetArrayDefStr(def: parraydef): string;
  629. var Name: string;
  630. begin
  631. Name:='array ['+IntToStr(def^.lowrange)+'..'+IntToStr(def^.highrange)+'] of ';
  632. if assigned(def^.definition) then
  633. Name:=Name+GetDefinitionStr(def^.definition);
  634. GetArrayDefStr:=Name;
  635. end;
  636. function GetFileDefStr(def: pfiledef): string;
  637. var Name: string;
  638. begin
  639. Name:='';
  640. case def^.filetype of
  641. ft_text : Name:='text';
  642. ft_untyped : Name:='file';
  643. ft_typed : Name:='file of '+GetDefinitionStr(def^.typed_as);
  644. end;
  645. GetFileDefStr:=Name;
  646. end;
  647. function GetStringDefStr(def: pstringdef): string;
  648. var Name: string;
  649. begin
  650. Name:='';
  651. case def^.string_typ of
  652. st_shortstring :
  653. if def^.len=255 then
  654. Name:='shortstring'
  655. else
  656. Name:='string['+IntToStr(def^.len)+']';
  657. st_longstring :
  658. Name:='longstring';
  659. st_ansistring :
  660. Name:='ansistring';
  661. st_widestring :
  662. Name:='widestring';
  663. else ;
  664. end;
  665. GetStringDefStr:=Name;
  666. end;
  667. function retdefassigned(def: pabstractprocdef): boolean;
  668. var OK: boolean;
  669. begin
  670. OK:=false;
  671. if assigned(def^.retdef) then
  672. if UpcaseStr(GetDefinitionStr(def^.retdef))<>'VOID' then
  673. OK:=true;
  674. retdefassigned:=OK;
  675. end;
  676. function GetAbsProcParmDefStr(def: pabstractprocdef): string;
  677. var Name: string;
  678. dc: pdefcoll;
  679. Count: integer;
  680. CurName: string;
  681. begin
  682. Name:='';
  683. dc:=def^.para1; Count:=0;
  684. while dc<>nil do
  685. begin
  686. CurName:='';
  687. case dc^.paratyp of
  688. vs_Value : ;
  689. vs_Const : CurName:=CurName+'const ';
  690. vs_Var : CurName:=CurName+'var ';
  691. end;
  692. if assigned(dc^.data) then
  693. CurName:=CurName+GetDefinitionStr(dc^.data);
  694. if dc^.next<>nil then
  695. CurName:=', '+CurName;
  696. Name:=CurName+Name;
  697. dc:=dc^.next; Inc(Count);
  698. end;
  699. GetAbsProcParmDefStr:=Name;
  700. end;
  701. function GetAbsProcDefStr(def: pabstractprocdef): string;
  702. var Name: string;
  703. begin
  704. Name:=GetAbsProcParmDefStr(def);
  705. if Name<>'' then Name:='('+Name+')';
  706. if retdefassigned(def) then
  707. Name:='function'+Name+': '+GetDefinitionStr(def^.retdef)
  708. else
  709. Name:='procedure'+Name;
  710. GetAbsProcDefStr:=Name;
  711. end;
  712. function GetProcDefStr(def: pprocdef): string;
  713. var DName: string;
  714. J: integer;
  715. begin
  716. { DName:='';
  717. if assigned(def) then
  718. begin
  719. if assigned(def^.parast) then
  720. begin
  721. with def^.parast^ do
  722. for J:=1 to number_symbols do
  723. begin
  724. if J<>1 then DName:=DName+', ';
  725. ParSym:=GetsymNr(J);
  726. if ParSym=nil then Break;
  727. DName:=DName+ParSym^.Name;
  728. end;
  729. end
  730. end;}
  731. DName:=GetAbsProcDefStr(def);
  732. GetProcDefStr:=DName;
  733. end;
  734. function GetProcVarDefStr(def: pprocvardef): string;
  735. begin
  736. GetProcVarDefStr:=GetAbsProcDefStr(def);
  737. end;
  738. function GetSetDefStr(def: psetdef): string;
  739. var Name: string;
  740. begin
  741. Name:='';
  742. case def^.settype of
  743. normset : Name:='set';
  744. smallset : Name:='set';
  745. varset : Name:='varset';
  746. end;
  747. Name:=Name+' of ';
  748. Name:=Name+GetDefinitionStr(def^.setof);
  749. GetSetDefStr:=Name;
  750. end;
  751. function GetDefinitionStr(def: pdef): string;
  752. var Name: string;
  753. sym: psym;
  754. begin
  755. Name:='';
  756. if def<>nil then
  757. begin
  758. if assigned(def^.sym) then
  759. Name:=def^.sym^.name;
  760. if Name='' then
  761. case def^.deftype of
  762. arraydef :
  763. Name:=GetArrayDefStr(parraydef(def));
  764. stringdef :
  765. Name:=GetStringDefStr(pstringdef(def));
  766. enumdef :
  767. Name:=GetEnumDefStr(penumdef(def));
  768. procdef :
  769. Name:=GetProcDefStr(pprocdef(def));
  770. procvardef :
  771. Name:=GetProcVarDefStr(pprocvardef(def));
  772. filedef :
  773. Name:=GetFileDefStr(pfiledef(def));
  774. setdef :
  775. Name:=GetSetDefStr(psetdef(def));
  776. end;
  777. end;
  778. GetDefinitionStr:=Name;
  779. end;
  780. function GetEnumItemName(Sym: penumsym): string;
  781. var Name: string;
  782. ES: penumsym;
  783. begin
  784. Name:='';
  785. if assigned(sym) and assigned(sym^.definition) then
  786. if assigned(sym^.definition^.sym) then
  787. begin
  788. { ES:=sym^.definition^.First;
  789. while (ES<>nil) and (ES^.Value<>sym^.Value) do
  790. ES:=ES^.next;
  791. if assigned(es) and (es^.value=sym^.value) then
  792. Name:=}
  793. Name:=sym^.definition^.sym^.name;
  794. if Name<>'' then
  795. Name:=Name+'('+IntToStr(sym^.value)+')';
  796. end;
  797. GetEnumItemName:=Name;
  798. end;
  799. function GetConstValueName(sym: pconstsym): string;
  800. var Name: string;
  801. begin
  802. Name:='';
  803. { if assigned(sym^.definition) then
  804. if assigned(sym^.definition^.sym) then
  805. Name:=sym^.definition^.sym^.name;}
  806. if Name='' then
  807. case sym^.consttype of
  808. constord :
  809. Name:=sym^.definition^.sym^.name+'('+IntToStr(sym^.value)+')';
  810. conststring :
  811. Name:=''''+GetStr(PString(sym^.Value))+'''';
  812. constreal:
  813. Name:=FloatToStr(PBestReal(sym^.Value)^);
  814. constbool:
  815. { if boolean(sym^.Value)=true then
  816. Name:='TRUE'
  817. else
  818. Name:='FALSE';}
  819. Name:='Longbool('+IntToStr(sym^.Value)+')';
  820. constint:
  821. Name:=IntToStr(sym^.value);
  822. constchar:
  823. Name:=''''+chr(sym^.Value)+'''';
  824. constset:
  825. { Name:=SetToStr(pnormalset(sym^.Value))};
  826. constnil: ;
  827. end;
  828. GetConstValueName:=Name;
  829. end;
  830. procedure ProcessDefIfStruct(definition: pdef);
  831. begin
  832. if assigned(definition) then
  833. begin
  834. case definition^.deftype of
  835. recorddef :
  836. if precdef(definition)^.symtable<>Table then
  837. ProcessSymTable(Symbol,Symbol^.Items,precdef(definition)^.symtable);
  838. objectdef :
  839. if pobjectdef(definition)^.publicsyms<>Table then
  840. ProcessSymTable(Symbol,Symbol^.Items,pobjectdef(definition)^.publicsyms);
  841. { leads to infinite loops !!
  842. pointerdef :
  843. with ppointerdef(definition)^ do
  844. if assigned(definition) then
  845. if assigned(definition^.sym) then
  846. ProcessDefIfStruct(definition^.sym^.definition);}
  847. end;
  848. end;
  849. end;
  850. var MemInfo: TSymbolMemInfo;
  851. ObjDef: pobjectdef;
  852. begin
  853. if not Assigned(Table) then
  854. Exit;
  855. if Owner=nil then
  856. Owner:=New(PSortedSymbolCollection, Init(10,50));
  857. sym:=psym(Table^.symindex^.first);
  858. while assigned(sym) do
  859. begin
  860. ParamCount:=0;
  861. New(Symbol, Init(Sym^.Name,Sym^.Typ,'',nil));
  862. case Sym^.Typ of
  863. varsym :
  864. with pvarsym(sym)^ do
  865. begin
  866. if assigned(definition) then
  867. if assigned(definition^.sym) then
  868. SetVType(Symbol,definition^.sym^.name)
  869. else
  870. SetVType(Symbol,GetDefinitionStr(definition));
  871. ProcessDefIfStruct(definition);
  872. MemInfo.Addr:=address;
  873. if assigned(localvarsym) then
  874. MemInfo.LocalAddr:=localvarsym^.address
  875. else
  876. MemInfo.LocalAddr:=0;
  877. MemInfo.Size:=getsize;
  878. MemInfo.PushSize:=getpushsize;
  879. Symbol^.SetMemInfo(MemInfo);
  880. end;
  881. constsym :
  882. SetDType(Symbol,GetConstValueName(pconstsym(sym)));
  883. enumsym :
  884. if assigned(penumsym(sym)^.definition) then
  885. SetDType(Symbol,GetEnumItemName(penumsym(sym)));
  886. unitsym :
  887. begin
  888. { ProcessSymTable(Symbol^.Items,punitsym(sym)^.unitsymtable);}
  889. end;
  890. syssym :
  891. { if assigned(Table^.Name) then
  892. if Table^.Name^='SYSTEM' then}
  893. begin
  894. Symbol^.Params:=TypeNames^.Add('...');
  895. end;
  896. funcretsym :
  897. if Assigned(OwnerSym) then
  898. with pfuncretsym(sym)^ do
  899. if assigned(funcretdef) then
  900. if assigned(funcretdef^.sym) then
  901. SetVType(OwnerSym,funcretdef^.sym^.name);
  902. procsym :
  903. begin
  904. with pprocsym(sym)^ do
  905. if assigned(definition) then
  906. if assigned(definition^.nextoverloaded) then
  907. begin
  908. { Several overloaded functions }
  909. Symbol^.Params:=TypeNames^.Add('...');
  910. end
  911. else
  912. begin
  913. if cs_local_browser in aktmoduleswitches then
  914. ProcessSymTable(Symbol,Symbol^.Items,definition^.parast);
  915. if assigned(definition^.parast) then
  916. begin
  917. Symbol^.Params:=TypeNames^.Add(GetAbsProcParmDefStr(definition));
  918. end
  919. else { param-definition is NOT assigned }
  920. if assigned(Table^.Name) then
  921. if Table^.Name^='SYSTEM' then
  922. begin
  923. Symbol^.Params:=TypeNames^.Add('...');
  924. end;
  925. if cs_local_browser in aktmoduleswitches then
  926. begin
  927. if assigned(definition^.localst) and
  928. (definition^.localst^.symtabletype<>staticsymtable) then
  929. ProcessSymTable(Symbol,Symbol^.Items,definition^.localst);
  930. end;
  931. end;
  932. end;
  933. typesym :
  934. begin
  935. with ptypesym(sym)^ do
  936. if assigned(definition) then
  937. case definition^.deftype of
  938. arraydef :
  939. SetDType(Symbol,GetArrayDefStr(parraydef(definition)));
  940. enumdef :
  941. SetDType(Symbol,GetEnumDefStr(penumdef(definition)));
  942. procdef :
  943. SetDType(Symbol,GetProcDefStr(pprocdef(definition)));
  944. procvardef :
  945. SetDType(Symbol,GetProcVarDefStr(pprocvardef(definition)));
  946. objectdef :
  947. with pobjectdef(definition)^ do
  948. begin
  949. ObjDef:=childof;
  950. Symbol^.ObjectID:=longint(definition);
  951. if ObjDef<>nil then
  952. Symbol^.AncestorID:=longint(ObjDef);{TypeNames^.Add(S);}
  953. Symbol^.Flags:=(Symbol^.Flags or sfObject);
  954. if (options and oo_is_class)<>0 then
  955. Symbol^.Flags:=(Symbol^.Flags or sfClass);
  956. ProcessSymTable(Symbol,Symbol^.Items,pobjectdef(definition)^.publicsyms);
  957. end;
  958. recorddef :
  959. begin
  960. Symbol^.Flags:=(Symbol^.Flags or sfRecord);
  961. ProcessSymTable(Symbol,Symbol^.Items,precdef(definition)^.symtable);
  962. end;
  963. filedef :
  964. SetDType(Symbol,GetFileDefStr(pfiledef(definition)));
  965. setdef :
  966. SetDType(Symbol,GetSetDefStr(psetdef(definition)));
  967. end;
  968. end;
  969. end;
  970. Ref:=Sym^.defref;
  971. while Assigned(Symbol) and assigned(Ref) do
  972. begin
  973. inputfile:=get_source_file(ref^.moduleindex,ref^.posinfo.fileindex);
  974. if Assigned(inputfile) and Assigned(inputfile^.name) then
  975. begin
  976. New(Reference, Init(ModuleNames^.Add(inputfile^.name^),
  977. ref^.posinfo.line,ref^.posinfo.column));
  978. Symbol^.References^.Insert(Reference);
  979. end;
  980. Ref:=Ref^.nextref;
  981. end;
  982. if Assigned(Symbol) then
  983. begin
  984. If Not Owner^.Search(Symbol,i) then
  985. Owner^.Insert(Symbol)
  986. else
  987. begin
  988. Comment(V_Warning,sym^.name+' already in SymbolCollection '+sym^.owner^.name^);
  989. dispose(Symbol,Done);
  990. end;
  991. end;
  992. sym:=psym(sym^.next);
  993. end;
  994. end;
  995. var
  996. T: PSymTable;
  997. UnitS: PSymbol;
  998. hp : pmodule;
  999. begin
  1000. DisposeBrowserCol;
  1001. NewBrowserCol;
  1002. hp:=pmodule(loaded_units.first);
  1003. while assigned(hp) do
  1004. begin
  1005. t:=psymtable(hp^.globalsymtable);
  1006. if assigned(t) then
  1007. begin
  1008. New(UnitS, Init(T^.Name^,unitsym,'',nil));
  1009. Modules^.Insert(UnitS);
  1010. ProcessSymTable(UnitS,UnitS^.Items,T);
  1011. if cs_local_browser in aktmoduleswitches then
  1012. begin
  1013. t:=psymtable(hp^.localsymtable);
  1014. if assigned(t) then
  1015. ProcessSymTable(UnitS,UnitS^.Items,T);
  1016. end;
  1017. end;
  1018. hp:=pmodule(hp^.next);
  1019. end;
  1020. BuildObjectInfo;
  1021. end;
  1022. procedure BuildObjectInfo;
  1023. var C: PIDSortedSymbolCollection;
  1024. ObjectC: PObjectSymbolCollection;
  1025. ObjectsSymbol: PObjectSymbol;
  1026. procedure InsertSymbolCollection(Symbols: PSymbolCollection);
  1027. var I: sw_integer;
  1028. P: PSymbol;
  1029. begin
  1030. for I:=0 to Symbols^.Count-1 do
  1031. begin
  1032. P:=Symbols^.At(I);
  1033. if (P^.Flags and sfObject)<>0 then
  1034. C^.Insert(P);
  1035. if P^.Items<>nil then
  1036. InsertSymbolCollection(P^.Items);
  1037. end;
  1038. end;
  1039. function SearchObjectForSym(O: PSymbol): PObjectSymbol;
  1040. var I,Idx: sw_integer;
  1041. OS,P: PObjectSymbol;
  1042. begin
  1043. P:=nil;
  1044. for I:=0 to ObjectC^.Count-1 do
  1045. begin
  1046. OS:=ObjectC^.At(I);
  1047. if OS^.Symbol=O then
  1048. begin P:=OS; Break; end;
  1049. end;
  1050. SearchObjectForSym:=P;
  1051. end;
  1052. procedure BuildTree;
  1053. var I: sw_integer;
  1054. Symbol: PSymbol;
  1055. Parent,OS: PObjectSymbol;
  1056. begin
  1057. I:=0;
  1058. while (I<C^.Count) do
  1059. begin
  1060. Symbol:=C^.At(I);
  1061. if Symbol^.Ancestor=nil then
  1062. Parent:=ObjectsSymbol
  1063. else
  1064. Parent:=SearchObjectForSym(Symbol^.Ancestor);
  1065. if Parent<>nil then
  1066. begin
  1067. New(OS, Init(Parent, Symbol));
  1068. Parent^.AddDescendant(OS);
  1069. ObjectC^.Insert(OS);
  1070. C^.AtDelete(I);
  1071. end
  1072. else
  1073. Inc(I);
  1074. end;
  1075. end;
  1076. var Pass: integer;
  1077. I: sw_integer;
  1078. P: PSymbol;
  1079. begin
  1080. New(C, Init(1000,5000));
  1081. InsertSymbolCollection(Modules);
  1082. { --- Resolve ancestor<->descendant references --- }
  1083. for I:=0 to C^.Count-1 do
  1084. begin
  1085. P:=C^.At(I);
  1086. if P^.AncestorID<>0 then
  1087. P^.Ancestor:=C^.SearchSymbolByID(P^.AncestorID);
  1088. end;
  1089. { --- Build object tree --- }
  1090. if assigned(ObjectTree) then Dispose(ObjectTree, Done);
  1091. New(ObjectsSymbol, InitName('Objects'));
  1092. ObjectTree:=ObjectsSymbol;
  1093. New(ObjectC, Init(C^.Count,100));
  1094. Pass:=0;
  1095. if C^.Count>0 then
  1096. repeat
  1097. BuildTree;
  1098. Inc(Pass);
  1099. until (C^.Count=0) or (Pass>20); { more than 20 levels ? - then there must be a bug }
  1100. ObjectC^.DeleteAll; Dispose(ObjectC, Done);
  1101. C^.DeleteAll; Dispose(C, Done);
  1102. end;
  1103. function SearchObjectForSymbol(O: PSymbol): PObjectSymbol;
  1104. function ScanObjectCollection(Parent: PObjectSymbol): PObjectSymbol;
  1105. var I: sw_integer;
  1106. OS,P: PObjectSymbol;
  1107. ObjectC: PObjectSymbolCollection;
  1108. begin
  1109. P:=nil;
  1110. if Parent<>nil then
  1111. if Parent^.Descendants<>nil then
  1112. begin
  1113. ObjectC:=Parent^.Descendants;
  1114. for I:=0 to ObjectC^.Count-1 do
  1115. begin
  1116. OS:=ObjectC^.At(I);
  1117. if OS^.Symbol=O then
  1118. begin P:=OS; Break; end;
  1119. if OS^.Descendants<>nil then
  1120. begin
  1121. P:=ScanObjectCollection(OS);
  1122. if P<>nil then Break;
  1123. end;
  1124. end;
  1125. end;
  1126. ScanObjectCollection:=P;
  1127. end;
  1128. begin
  1129. SearchObjectForSymbol:=ScanObjectCollection(ObjectTree);
  1130. end;
  1131. {*****************************************************************************
  1132. Initialize
  1133. *****************************************************************************}
  1134. var
  1135. oldexit : pointer;
  1136. procedure browcol_exit;{$ifndef FPC}far;{$endif}
  1137. begin
  1138. exitproc:=oldexit;
  1139. DisposeBrowserCol;
  1140. end;
  1141. procedure InitBrowserCol;
  1142. begin
  1143. end;
  1144. procedure DoneBrowserCol;
  1145. begin
  1146. { nothing, the collections are freed in the exitproc }
  1147. end;
  1148. begin
  1149. oldexit:=exitproc;
  1150. exitproc:=@browcol_exit;
  1151. end.
  1152. {
  1153. $Log$
  1154. Revision 1.18 1999-06-25 00:27:41 pierre
  1155. merged from fixes-0_99_12
  1156. Revision 1.16.2.1 1999/06/25 00:22:23 pierre
  1157. * avoid problem with lowercase symbols
  1158. (compare returns zero only if excat match,
  1159. ordering is first done case unsensitive
  1160. for a correct browser order)
  1161. this solves memory leaks :
  1162. TV and FV do not delete not inserted items in
  1163. a sorted collection without duplicates (is this a bug or a feature ?)
  1164. Revision 1.17 1999/06/22 16:24:39 pierre
  1165. * local browser stuff corrected
  1166. Revision 1.16 1999/05/13 21:59:20 peter
  1167. * removed oldppu code
  1168. * warning if objpas is loaded from uses
  1169. * first things for new deref writing
  1170. Revision 1.15 1999/04/29 09:36:55 peter
  1171. * fixed crash
  1172. * check if localbrowser is set
  1173. Revision 1.14 1999/04/15 09:01:32 peter
  1174. * fixed set loading
  1175. * object inheritance support for browser
  1176. Revision 1.13 1999/04/14 18:59:52 peter
  1177. * fixed wrong variable names
  1178. Revision 1.12 1999/04/10 16:15:00 peter
  1179. * fixed browcol
  1180. + -ar to show regalloc info in .s file
  1181. Revision 1.11 1999/04/08 10:17:42 peter
  1182. + objects support
  1183. Revision 1.8 1999/03/03 01:38:11 pierre
  1184. * avoid infinite recursion in ProcessDefIfStruct
  1185. Revision 1.7 1999/02/22 11:51:32 peter
  1186. * browser updates from gabor
  1187. Revision 1.6 1999/02/04 09:31:59 pierre
  1188. + added objects and records symbol tables
  1189. Revision 1.5 1999/02/03 09:44:32 pierre
  1190. * symbol nubering begins with 1 in number_symbols
  1191. * program tmodule has globalsymtable for its staticsymtable
  1192. (to get it displayed in IDE globals list)
  1193. + list of symbol (browcol) greatly improved for IDE
  1194. Revision 1.4 1999/02/02 16:38:38 peter
  1195. * no endless loop with localst=staticsymtable
  1196. Revision 1.3 1999/01/22 10:19:43 peter
  1197. * fixed typo
  1198. Revision 1.2 1999/01/21 11:49:14 peter
  1199. * updates from gabor
  1200. Revision 1.1 1999/01/12 14:25:24 peter
  1201. + BrowserLog for browser.log generation
  1202. + BrowserCol for browser info in TCollections
  1203. * released all other UseBrowser
  1204. }