browcol.pas 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  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^.Firstenum; Count:=0;
  603. while (esym<>nil) do
  604. begin
  605. if Count>0 then
  606. Name:=Name+', ';
  607. Name:=Name+esym^.name;
  608. esym:=esym^.nextenum;
  609. Inc(Count);
  610. end;
  611. Name:=Name+')';
  612. GetEnumDefStr:=Name;
  613. end;
  614. function GetArrayDefStr(def: parraydef): string;
  615. var Name: string;
  616. begin
  617. Name:='array ['+IntToStr(def^.lowrange)+'..'+IntToStr(def^.highrange)+'] of ';
  618. if assigned(def^.definition) then
  619. Name:=Name+GetDefinitionStr(def^.definition);
  620. GetArrayDefStr:=Name;
  621. end;
  622. function GetFileDefStr(def: pfiledef): string;
  623. var Name: string;
  624. begin
  625. Name:='';
  626. case def^.filetype of
  627. ft_text : Name:='text';
  628. ft_untyped : Name:='file';
  629. ft_typed : Name:='file of '+GetDefinitionStr(def^.typed_as);
  630. end;
  631. GetFileDefStr:=Name;
  632. end;
  633. function GetStringDefStr(def: pstringdef): string;
  634. var Name: string;
  635. begin
  636. Name:='';
  637. case def^.string_typ of
  638. st_shortstring :
  639. if def^.len=255 then
  640. Name:='shortstring'
  641. else
  642. Name:='string['+IntToStr(def^.len)+']';
  643. st_longstring :
  644. Name:='longstring';
  645. st_ansistring :
  646. Name:='ansistring';
  647. st_widestring :
  648. Name:='widestring';
  649. else ;
  650. end;
  651. GetStringDefStr:=Name;
  652. end;
  653. function retdefassigned(def: pabstractprocdef): boolean;
  654. var OK: boolean;
  655. begin
  656. OK:=false;
  657. if assigned(def^.retdef) then
  658. if UpcaseStr(GetDefinitionStr(def^.retdef))<>'VOID' then
  659. OK:=true;
  660. retdefassigned:=OK;
  661. end;
  662. function GetAbsProcParmDefStr(def: pabstractprocdef): string;
  663. var Name: string;
  664. dc: pdefcoll;
  665. Count: integer;
  666. CurName: string;
  667. begin
  668. Name:='';
  669. dc:=def^.para1; Count:=0;
  670. while dc<>nil do
  671. begin
  672. CurName:='';
  673. case dc^.paratyp of
  674. vs_Value : ;
  675. vs_Const : CurName:=CurName+'const ';
  676. vs_Var : CurName:=CurName+'var ';
  677. end;
  678. if assigned(dc^.data) then
  679. CurName:=CurName+GetDefinitionStr(dc^.data);
  680. if dc^.next<>nil then
  681. CurName:=', '+CurName;
  682. Name:=CurName+Name;
  683. dc:=dc^.next; Inc(Count);
  684. end;
  685. GetAbsProcParmDefStr:=Name;
  686. end;
  687. function GetAbsProcDefStr(def: pabstractprocdef): string;
  688. var Name: string;
  689. begin
  690. Name:=GetAbsProcParmDefStr(def);
  691. if Name<>'' then Name:='('+Name+')';
  692. if retdefassigned(def) then
  693. Name:='function'+Name+': '+GetDefinitionStr(def^.retdef)
  694. else
  695. Name:='procedure'+Name;
  696. GetAbsProcDefStr:=Name;
  697. end;
  698. function GetProcDefStr(def: pprocdef): string;
  699. var DName: string;
  700. J: integer;
  701. begin
  702. { DName:='';
  703. if assigned(def) then
  704. begin
  705. if assigned(def^.parast) then
  706. begin
  707. with def^.parast^ do
  708. for J:=1 to number_symbols do
  709. begin
  710. if J<>1 then DName:=DName+', ';
  711. ParSym:=GetsymNr(J);
  712. if ParSym=nil then Break;
  713. DName:=DName+ParSym^.Name;
  714. end;
  715. end
  716. end;}
  717. DName:=GetAbsProcDefStr(def);
  718. GetProcDefStr:=DName;
  719. end;
  720. function GetProcVarDefStr(def: pprocvardef): string;
  721. begin
  722. GetProcVarDefStr:=GetAbsProcDefStr(def);
  723. end;
  724. function GetSetDefStr(def: psetdef): string;
  725. var Name: string;
  726. begin
  727. Name:='';
  728. case def^.settype of
  729. normset : Name:='set';
  730. smallset : Name:='set';
  731. varset : Name:='varset';
  732. end;
  733. Name:=Name+' of ';
  734. Name:=Name+GetDefinitionStr(def^.setof);
  735. GetSetDefStr:=Name;
  736. end;
  737. function GetDefinitionStr(def: pdef): string;
  738. var Name: string;
  739. sym: psym;
  740. begin
  741. Name:='';
  742. if def<>nil then
  743. begin
  744. if assigned(def^.sym) then
  745. Name:=def^.sym^.name;
  746. if Name='' then
  747. case def^.deftype of
  748. arraydef :
  749. Name:=GetArrayDefStr(parraydef(def));
  750. stringdef :
  751. Name:=GetStringDefStr(pstringdef(def));
  752. enumdef :
  753. Name:=GetEnumDefStr(penumdef(def));
  754. procdef :
  755. Name:=GetProcDefStr(pprocdef(def));
  756. procvardef :
  757. Name:=GetProcVarDefStr(pprocvardef(def));
  758. filedef :
  759. Name:=GetFileDefStr(pfiledef(def));
  760. setdef :
  761. Name:=GetSetDefStr(psetdef(def));
  762. end;
  763. end;
  764. GetDefinitionStr:=Name;
  765. end;
  766. function GetEnumItemName(Sym: penumsym): string;
  767. var Name: string;
  768. ES: penumsym;
  769. begin
  770. Name:='';
  771. if assigned(sym) and assigned(sym^.definition) then
  772. if assigned(sym^.definition^.sym) then
  773. begin
  774. { ES:=sym^.definition^.First;
  775. while (ES<>nil) and (ES^.Value<>sym^.Value) do
  776. ES:=ES^.next;
  777. if assigned(es) and (es^.value=sym^.value) then
  778. Name:=}
  779. Name:=sym^.definition^.sym^.name;
  780. if Name<>'' then
  781. Name:=Name+'('+IntToStr(sym^.value)+')';
  782. end;
  783. GetEnumItemName:=Name;
  784. end;
  785. function GetConstValueName(sym: pconstsym): string;
  786. var Name: string;
  787. begin
  788. Name:='';
  789. { if assigned(sym^.definition) then
  790. if assigned(sym^.definition^.sym) then
  791. Name:=sym^.definition^.sym^.name;}
  792. if Name='' then
  793. case sym^.consttype of
  794. constord :
  795. Name:=sym^.definition^.sym^.name+'('+IntToStr(sym^.value)+')';
  796. conststring :
  797. Name:=''''+GetStr(PString(sym^.Value))+'''';
  798. constreal:
  799. Name:=FloatToStr(PBestReal(sym^.Value)^);
  800. constbool:
  801. { if boolean(sym^.Value)=true then
  802. Name:='TRUE'
  803. else
  804. Name:='FALSE';}
  805. Name:='Longbool('+IntToStr(sym^.Value)+')';
  806. constint:
  807. Name:=IntToStr(sym^.value);
  808. constchar:
  809. Name:=''''+chr(sym^.Value)+'''';
  810. constset:
  811. { Name:=SetToStr(pnormalset(sym^.Value))};
  812. constnil: ;
  813. end;
  814. GetConstValueName:=Name;
  815. end;
  816. procedure ProcessDefIfStruct(definition: pdef);
  817. begin
  818. if assigned(definition) then
  819. begin
  820. case definition^.deftype of
  821. recorddef :
  822. if precdef(definition)^.symtable<>Table then
  823. ProcessSymTable(Symbol,Symbol^.Items,precdef(definition)^.symtable);
  824. objectdef :
  825. if pobjectdef(definition)^.publicsyms<>Table then
  826. ProcessSymTable(Symbol,Symbol^.Items,pobjectdef(definition)^.publicsyms);
  827. { leads to infinite loops !!
  828. pointerdef :
  829. with ppointerdef(definition)^ do
  830. if assigned(definition) then
  831. if assigned(definition^.sym) then
  832. ProcessDefIfStruct(definition^.sym^.definition);}
  833. end;
  834. end;
  835. end;
  836. var MemInfo: TSymbolMemInfo;
  837. ObjDef: pobjectdef;
  838. begin
  839. if not Assigned(Table) then
  840. Exit;
  841. if Owner=nil then
  842. Owner:=New(PSortedSymbolCollection, Init(10,50));
  843. sym:=psym(Table^.symindex^.first);
  844. while assigned(sym) do
  845. begin
  846. ParamCount:=0;
  847. New(Symbol, Init(Sym^.Name,Sym^.Typ,'',nil));
  848. case Sym^.Typ of
  849. varsym :
  850. with pvarsym(sym)^ do
  851. begin
  852. if assigned(definition) then
  853. if assigned(definition^.sym) then
  854. SetVType(Symbol,definition^.sym^.name)
  855. else
  856. SetVType(Symbol,GetDefinitionStr(definition));
  857. ProcessDefIfStruct(definition);
  858. MemInfo.Addr:=address;
  859. if assigned(localvarsym) then
  860. MemInfo.LocalAddr:=localvarsym^.address
  861. else
  862. MemInfo.LocalAddr:=0;
  863. MemInfo.Size:=getsize;
  864. MemInfo.PushSize:=getpushsize;
  865. Symbol^.SetMemInfo(MemInfo);
  866. end;
  867. constsym :
  868. SetDType(Symbol,GetConstValueName(pconstsym(sym)));
  869. enumsym :
  870. if assigned(penumsym(sym)^.definition) then
  871. SetDType(Symbol,GetEnumItemName(penumsym(sym)));
  872. unitsym :
  873. begin
  874. { ProcessSymTable(Symbol^.Items,punitsym(sym)^.unitsymtable);}
  875. end;
  876. syssym :
  877. { if assigned(Table^.Name) then
  878. if Table^.Name^='SYSTEM' then}
  879. begin
  880. Symbol^.Params:=TypeNames^.Add('...');
  881. end;
  882. funcretsym :
  883. if Assigned(OwnerSym) then
  884. with pfuncretsym(sym)^ do
  885. if assigned(funcretdef) then
  886. if assigned(funcretdef^.sym) then
  887. SetVType(OwnerSym,funcretdef^.sym^.name);
  888. procsym :
  889. begin
  890. with pprocsym(sym)^ do
  891. if assigned(definition) then
  892. begin
  893. if cs_local_browser in aktmoduleswitches then
  894. ProcessSymTable(Symbol,Symbol^.Items,definition^.parast);
  895. if assigned(definition^.parast) then
  896. begin
  897. Symbol^.Params:=TypeNames^.Add(GetAbsProcParmDefStr(definition));
  898. end
  899. else { param-definition is NOT assigned }
  900. if assigned(Table^.Name) then
  901. if Table^.Name^='SYSTEM' then
  902. begin
  903. Symbol^.Params:=TypeNames^.Add('...');
  904. end;
  905. if cs_local_browser in aktmoduleswitches then
  906. begin
  907. if assigned(definition^.localst) and
  908. (definition^.localst^.symtabletype<>staticsymtable) then
  909. ProcessSymTable(Symbol,Symbol^.Items,definition^.localst);
  910. end;
  911. end;
  912. end;
  913. typesym :
  914. begin
  915. with ptypesym(sym)^ do
  916. if assigned(definition) then
  917. case definition^.deftype of
  918. arraydef :
  919. SetDType(Symbol,GetArrayDefStr(parraydef(definition)));
  920. enumdef :
  921. SetDType(Symbol,GetEnumDefStr(penumdef(definition)));
  922. procdef :
  923. SetDType(Symbol,GetProcDefStr(pprocdef(definition)));
  924. procvardef :
  925. SetDType(Symbol,GetProcVarDefStr(pprocvardef(definition)));
  926. objectdef :
  927. with pobjectdef(definition)^ do
  928. begin
  929. ObjDef:=childof;
  930. Symbol^.ObjectID:=longint(definition);
  931. if ObjDef<>nil then
  932. Symbol^.AncestorID:=longint(ObjDef);{TypeNames^.Add(S);}
  933. Symbol^.Flags:=(Symbol^.Flags or sfObject);
  934. if (options and oo_is_class)<>0 then
  935. Symbol^.Flags:=(Symbol^.Flags or sfClass);
  936. ProcessSymTable(Symbol,Symbol^.Items,pobjectdef(definition)^.publicsyms);
  937. end;
  938. recorddef :
  939. begin
  940. Symbol^.Flags:=(Symbol^.Flags or sfRecord);
  941. ProcessSymTable(Symbol,Symbol^.Items,precdef(definition)^.symtable);
  942. end;
  943. filedef :
  944. SetDType(Symbol,GetFileDefStr(pfiledef(definition)));
  945. setdef :
  946. SetDType(Symbol,GetSetDefStr(psetdef(definition)));
  947. end;
  948. end;
  949. end;
  950. Ref:=Sym^.defref;
  951. while Assigned(Symbol) and assigned(Ref) do
  952. begin
  953. inputfile:=get_source_file(ref^.moduleindex,ref^.posinfo.fileindex);
  954. if Assigned(inputfile) and Assigned(inputfile^.name) then
  955. begin
  956. New(Reference, Init(ModuleNames^.Add(inputfile^.name^),
  957. ref^.posinfo.line,ref^.posinfo.column));
  958. Symbol^.References^.Insert(Reference);
  959. end;
  960. Ref:=Ref^.nextref;
  961. end;
  962. if Assigned(Symbol) then
  963. Owner^.Insert(Symbol);
  964. sym:=psym(sym^.next);
  965. end;
  966. end;
  967. var
  968. T: PSymTable;
  969. UnitS: PSymbol;
  970. hp : pmodule;
  971. begin
  972. DisposeBrowserCol;
  973. NewBrowserCol;
  974. hp:=pmodule(loaded_units.first);
  975. while assigned(hp) do
  976. begin
  977. t:=psymtable(hp^.globalsymtable);
  978. if assigned(t) then
  979. begin
  980. New(UnitS, Init(T^.Name^,unitsym,'',nil));
  981. Modules^.Insert(UnitS);
  982. ProcessSymTable(UnitS,UnitS^.Items,T);
  983. if cs_local_browser in aktmoduleswitches then
  984. begin
  985. t:=psymtable(hp^.localsymtable);
  986. if assigned(t) then
  987. ProcessSymTable(UnitS,UnitS^.Items,T);
  988. end;
  989. end;
  990. hp:=pmodule(hp^.next);
  991. end;
  992. BuildObjectInfo;
  993. end;
  994. procedure BuildObjectInfo;
  995. var C: PIDSortedSymbolCollection;
  996. ObjectC: PObjectSymbolCollection;
  997. ObjectsSymbol: PObjectSymbol;
  998. procedure InsertSymbolCollection(Symbols: PSymbolCollection);
  999. var I: sw_integer;
  1000. P: PSymbol;
  1001. begin
  1002. for I:=0 to Symbols^.Count-1 do
  1003. begin
  1004. P:=Symbols^.At(I);
  1005. if (P^.Flags and sfObject)<>0 then
  1006. C^.Insert(P);
  1007. if P^.Items<>nil then
  1008. InsertSymbolCollection(P^.Items);
  1009. end;
  1010. end;
  1011. function SearchObjectForSym(O: PSymbol): PObjectSymbol;
  1012. var I,Idx: sw_integer;
  1013. OS,P: PObjectSymbol;
  1014. begin
  1015. P:=nil;
  1016. for I:=0 to ObjectC^.Count-1 do
  1017. begin
  1018. OS:=ObjectC^.At(I);
  1019. if OS^.Symbol=O then
  1020. begin P:=OS; Break; end;
  1021. end;
  1022. SearchObjectForSym:=P;
  1023. end;
  1024. procedure BuildTree;
  1025. var I: sw_integer;
  1026. Symbol: PSymbol;
  1027. Parent,OS: PObjectSymbol;
  1028. begin
  1029. I:=0;
  1030. while (I<C^.Count) do
  1031. begin
  1032. Symbol:=C^.At(I);
  1033. if Symbol^.Ancestor=nil then
  1034. Parent:=ObjectsSymbol
  1035. else
  1036. Parent:=SearchObjectForSym(Symbol^.Ancestor);
  1037. if Parent<>nil then
  1038. begin
  1039. New(OS, Init(Parent, Symbol));
  1040. Parent^.AddDescendant(OS);
  1041. ObjectC^.Insert(OS);
  1042. C^.AtDelete(I);
  1043. end
  1044. else
  1045. Inc(I);
  1046. end;
  1047. end;
  1048. var Pass: integer;
  1049. I: sw_integer;
  1050. P: PSymbol;
  1051. begin
  1052. New(C, Init(1000,5000));
  1053. InsertSymbolCollection(Modules);
  1054. { --- Resolve ancestor<->descendant references --- }
  1055. for I:=0 to C^.Count-1 do
  1056. begin
  1057. P:=C^.At(I);
  1058. if P^.AncestorID<>0 then
  1059. P^.Ancestor:=C^.SearchSymbolByID(P^.AncestorID);
  1060. end;
  1061. { --- Build object tree --- }
  1062. if assigned(ObjectTree) then Dispose(ObjectTree, Done);
  1063. New(ObjectsSymbol, InitName('Objects'));
  1064. ObjectTree:=ObjectsSymbol;
  1065. New(ObjectC, Init(C^.Count,100));
  1066. Pass:=0;
  1067. if C^.Count>0 then
  1068. repeat
  1069. BuildTree;
  1070. Inc(Pass);
  1071. until (C^.Count=0) or (Pass>20); { more than 20 levels ? - then there must be a bug }
  1072. ObjectC^.DeleteAll; Dispose(ObjectC, Done);
  1073. C^.DeleteAll; Dispose(C, Done);
  1074. end;
  1075. function SearchObjectForSymbol(O: PSymbol): PObjectSymbol;
  1076. function ScanObjectCollection(Parent: PObjectSymbol): PObjectSymbol;
  1077. var I: sw_integer;
  1078. OS,P: PObjectSymbol;
  1079. ObjectC: PObjectSymbolCollection;
  1080. begin
  1081. P:=nil;
  1082. if Parent<>nil then
  1083. if Parent^.Descendants<>nil then
  1084. begin
  1085. ObjectC:=Parent^.Descendants;
  1086. for I:=0 to ObjectC^.Count-1 do
  1087. begin
  1088. OS:=ObjectC^.At(I);
  1089. if OS^.Symbol=O then
  1090. begin P:=OS; Break; end;
  1091. if OS^.Descendants<>nil then
  1092. begin
  1093. P:=ScanObjectCollection(OS);
  1094. if P<>nil then Break;
  1095. end;
  1096. end;
  1097. end;
  1098. ScanObjectCollection:=P;
  1099. end;
  1100. begin
  1101. SearchObjectForSymbol:=ScanObjectCollection(ObjectTree);
  1102. end;
  1103. {*****************************************************************************
  1104. Initialize
  1105. *****************************************************************************}
  1106. var
  1107. oldexit : pointer;
  1108. procedure browcol_exit;{$ifndef FPC}far;{$endif}
  1109. begin
  1110. exitproc:=oldexit;
  1111. DisposeBrowserCol;
  1112. end;
  1113. procedure InitBrowserCol;
  1114. begin
  1115. end;
  1116. procedure DoneBrowserCol;
  1117. begin
  1118. { nothing, the collections are freed in the exitproc }
  1119. end;
  1120. begin
  1121. oldexit:=exitproc;
  1122. exitproc:=@browcol_exit;
  1123. end.
  1124. {
  1125. $Log$
  1126. Revision 1.16 1999-05-13 21:59:20 peter
  1127. * removed oldppu code
  1128. * warning if objpas is loaded from uses
  1129. * first things for new deref writing
  1130. Revision 1.15 1999/04/29 09:36:55 peter
  1131. * fixed crash
  1132. * check if localbrowser is set
  1133. Revision 1.14 1999/04/15 09:01:32 peter
  1134. * fixed set loading
  1135. * object inheritance support for browser
  1136. Revision 1.13 1999/04/14 18:59:52 peter
  1137. * fixed wrong variable names
  1138. Revision 1.12 1999/04/10 16:15:00 peter
  1139. * fixed browcol
  1140. + -ar to show regalloc info in .s file
  1141. Revision 1.11 1999/04/08 10:17:42 peter
  1142. + objects support
  1143. Revision 1.8 1999/03/03 01:38:11 pierre
  1144. * avoid infinite recursion in ProcessDefIfStruct
  1145. Revision 1.7 1999/02/22 11:51:32 peter
  1146. * browser updates from gabor
  1147. Revision 1.6 1999/02/04 09:31:59 pierre
  1148. + added objects and records symbol tables
  1149. Revision 1.5 1999/02/03 09:44:32 pierre
  1150. * symbol nubering begins with 1 in number_symbols
  1151. * program tmodule has globalsymtable for its staticsymtable
  1152. (to get it displayed in IDE globals list)
  1153. + list of symbol (browcol) greatly improved for IDE
  1154. Revision 1.4 1999/02/02 16:38:38 peter
  1155. * no endless loop with localst=staticsymtable
  1156. Revision 1.3 1999/01/22 10:19:43 peter
  1157. * fixed typo
  1158. Revision 1.2 1999/01/21 11:49:14 peter
  1159. * updates from gabor
  1160. Revision 1.1 1999/01/12 14:25:24 peter
  1161. + BrowserLog for browser.log generation
  1162. + BrowserCol for browser info in TCollections
  1163. * released all other UseBrowser
  1164. }