browcol.pas 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277
  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 precdef(definition)^.symtable<>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. defcount:=Table^.number_defs;
  844. symcount:=Table^.number_symbols;
  845. { for I:=0 to defcount-1 do
  846. begin
  847. Def:=Table^.GetDefNr(I);
  848. end;}
  849. for I:=1 to symcount do
  850. begin
  851. Sym:=Table^.GetsymNr(I);
  852. if Sym=nil then Continue;
  853. ParamCount:=0;
  854. New(Symbol, Init(Sym^.Name,Sym^.Typ,'',nil));
  855. case Sym^.Typ of
  856. varsym :
  857. with pvarsym(sym)^ do
  858. begin
  859. if assigned(definition) then
  860. if assigned(definition^.sym) then
  861. SetVType(Symbol,definition^.sym^.name)
  862. else
  863. SetVType(Symbol,GetDefinitionStr(definition));
  864. ProcessDefIfStruct(definition);
  865. MemInfo.Addr:=address;
  866. if assigned(localvarsym) then
  867. MemInfo.LocalAddr:=localvarsym^.address
  868. else
  869. MemInfo.LocalAddr:=0;
  870. MemInfo.Size:=getsize;
  871. MemInfo.PushSize:=getpushsize;
  872. Symbol^.SetMemInfo(MemInfo);
  873. end;
  874. constsym :
  875. SetDType(Symbol,GetConstValueName(pconstsym(sym)));
  876. enumsym :
  877. if assigned(penumsym(sym)^.definition) then
  878. SetDType(Symbol,GetEnumItemName(penumsym(sym)));
  879. unitsym :
  880. begin
  881. { ProcessSymTable(Symbol^.Items,punitsym(sym)^.unitsymtable);}
  882. end;
  883. syssym :
  884. { if assigned(Table^.Name) then
  885. if Table^.Name^='SYSTEM' then}
  886. begin
  887. Symbol^.Params:=TypeNames^.Add('...');
  888. end;
  889. funcretsym :
  890. if Assigned(OwnerSym) then
  891. with pfuncretsym(sym)^ do
  892. if assigned(funcretdef) then
  893. if assigned(funcretdef^.sym) then
  894. SetVType(OwnerSym,funcretdef^.sym^.name);
  895. procsym :
  896. begin
  897. with pprocsym(sym)^ do
  898. if assigned(definition) then
  899. begin
  900. ProcessSymTable(Symbol,Symbol^.Items,definition^.parast);
  901. if assigned(definition^.parast) then
  902. begin
  903. Symbol^.Params:=TypeNames^.Add(GetAbsProcParmDefStr(definition));
  904. end
  905. else { param-definition is NOT assigned }
  906. if assigned(Table^.Name) then
  907. if Table^.Name^='SYSTEM' then
  908. begin
  909. Symbol^.Params:=TypeNames^.Add('...');
  910. end;
  911. if assigned(definition^.localst) and
  912. (definition^.localst^.symtabletype<>staticsymtable) then
  913. ProcessSymTable(Symbol,Symbol^.Items,definition^.localst);
  914. end;
  915. end;
  916. typesym :
  917. begin
  918. with ptypesym(sym)^ do
  919. if assigned(definition) then
  920. case definition^.deftype of
  921. arraydef :
  922. SetDType(Symbol,GetArrayDefStr(parraydef(definition)));
  923. enumdef :
  924. SetDType(Symbol,GetEnumDefStr(penumdef(definition)));
  925. procdef :
  926. SetDType(Symbol,GetProcDefStr(pprocdef(definition)));
  927. procvardef :
  928. SetDType(Symbol,GetProcVarDefStr(pprocvardef(definition)));
  929. objectdef :
  930. with pobjectdef(definition)^ do
  931. begin
  932. ObjDef:=childof;
  933. Symbol^.ObjectID:=longint(definition);
  934. if ObjDef<>nil then
  935. Symbol^.AncestorID:=longint(ObjDef);{TypeNames^.Add(S);}
  936. Symbol^.Flags:=(Symbol^.Flags or sfObject);
  937. if (options and oo_is_class)<>0 then
  938. Symbol^.Flags:=(Symbol^.Flags or sfClass);
  939. ProcessSymTable(Symbol,Symbol^.Items,pobjectdef(definition)^.publicsyms);
  940. end;
  941. recorddef :
  942. begin
  943. Symbol^.Flags:=(Symbol^.Flags or sfRecord);
  944. ProcessSymTable(Symbol,Symbol^.Items,precdef(definition)^.symtable);
  945. end;
  946. filedef :
  947. SetDType(Symbol,GetFileDefStr(pfiledef(definition)));
  948. setdef :
  949. SetDType(Symbol,GetSetDefStr(psetdef(definition)));
  950. end;
  951. end;
  952. end;
  953. Ref:=Sym^.defref;
  954. while Assigned(Symbol) and assigned(Ref) do
  955. begin
  956. inputfile:=get_source_file(ref^.moduleindex,ref^.posinfo.fileindex);
  957. if Assigned(inputfile) and Assigned(inputfile^.name) then
  958. begin
  959. New(Reference, Init(ModuleNames^.Add(inputfile^.name^),
  960. ref^.posinfo.line,ref^.posinfo.column));
  961. Symbol^.References^.Insert(Reference);
  962. end;
  963. Ref:=Ref^.nextref;
  964. end;
  965. if Assigned(Symbol) then
  966. Owner^.Insert(Symbol);
  967. end;
  968. end;
  969. var
  970. T: PSymTable;
  971. UnitS: PSymbol;
  972. hp : pmodule;
  973. begin
  974. DisposeBrowserCol;
  975. NewBrowserCol;
  976. hp:=pmodule(loaded_units.first);
  977. while assigned(hp) do
  978. begin
  979. t:=psymtable(hp^.globalsymtable);
  980. if assigned(t) then
  981. begin
  982. New(UnitS, Init(T^.Name^,unitsym,'',nil));
  983. Modules^.Insert(UnitS);
  984. ProcessSymTable(UnitS,UnitS^.Items,T);
  985. if cs_local_browser in aktmoduleswitches then
  986. begin
  987. t:=psymtable(hp^.localsymtable);
  988. if assigned(t) then
  989. ProcessSymTable(UnitS,UnitS^.Items,T);
  990. end;
  991. end;
  992. hp:=pmodule(hp^.next);
  993. end;
  994. BuildObjectInfo;
  995. end;
  996. procedure BuildObjectInfo;
  997. var C: PIDSortedSymbolCollection;
  998. ObjectC: PObjectSymbolCollection;
  999. ObjectsSymbol: PObjectSymbol;
  1000. procedure InsertSymbolCollection(Symbols: PSymbolCollection);
  1001. var I: sw_integer;
  1002. P: PSymbol;
  1003. begin
  1004. for I:=0 to Symbols^.Count-1 do
  1005. begin
  1006. P:=Symbols^.At(I);
  1007. if (P^.Flags and sfObject)<>0 then
  1008. C^.Insert(P);
  1009. if P^.Items<>nil then
  1010. InsertSymbolCollection(P^.Items);
  1011. end;
  1012. end;
  1013. function SearchObjectForSym(O: PSymbol): PObjectSymbol;
  1014. var I,Idx: sw_integer;
  1015. OS,P: PObjectSymbol;
  1016. begin
  1017. P:=nil;
  1018. for I:=0 to ObjectC^.Count-1 do
  1019. begin
  1020. OS:=ObjectC^.At(I);
  1021. if OS^.Symbol=O then
  1022. begin P:=OS; Break; end;
  1023. end;
  1024. SearchObjectForSym:=P;
  1025. end;
  1026. procedure BuildTree;
  1027. var I: sw_integer;
  1028. Symbol: PSymbol;
  1029. Parent,OS: PObjectSymbol;
  1030. begin
  1031. I:=0;
  1032. while (I<C^.Count) do
  1033. begin
  1034. Symbol:=C^.At(I);
  1035. if Symbol^.Ancestor=nil then
  1036. Parent:=ObjectsSymbol
  1037. else
  1038. Parent:=SearchObjectForSym(Symbol^.Ancestor);
  1039. if Parent<>nil then
  1040. begin
  1041. New(OS, Init(Parent, Symbol));
  1042. Parent^.AddDescendant(OS);
  1043. ObjectC^.Insert(OS);
  1044. C^.AtDelete(I);
  1045. end
  1046. else
  1047. Inc(I);
  1048. end;
  1049. end;
  1050. var Pass: integer;
  1051. I: sw_integer;
  1052. P: PSymbol;
  1053. begin
  1054. New(C, Init(1000,5000));
  1055. InsertSymbolCollection(Modules);
  1056. { --- Resolve ancestor<->descendant references --- }
  1057. for I:=0 to C^.Count-1 do
  1058. begin
  1059. P:=C^.At(I);
  1060. if P^.AncestorID<>0 then
  1061. P^.Ancestor:=C^.SearchSymbolByID(P^.AncestorID);
  1062. end;
  1063. { --- Build object tree --- }
  1064. if assigned(ObjectTree) then Dispose(ObjectTree, Done);
  1065. New(ObjectsSymbol, InitName('Objects'));
  1066. ObjectTree:=ObjectsSymbol;
  1067. New(ObjectC, Init(C^.Count,100));
  1068. Pass:=0;
  1069. if C^.Count>0 then
  1070. repeat
  1071. BuildTree;
  1072. Inc(Pass);
  1073. until (C^.Count=0) or (Pass>20); { more than 20 levels ? - then there must be a bug }
  1074. ObjectC^.DeleteAll; Dispose(ObjectC, Done);
  1075. C^.DeleteAll; Dispose(C, Done);
  1076. end;
  1077. function SearchObjectForSymbol(O: PSymbol): PObjectSymbol;
  1078. function ScanObjectCollection(Parent: PObjectSymbol): PObjectSymbol;
  1079. var I: sw_integer;
  1080. OS,P: PObjectSymbol;
  1081. ObjectC: PObjectSymbolCollection;
  1082. begin
  1083. P:=nil;
  1084. if Parent<>nil then
  1085. if Parent^.Descendants<>nil then
  1086. begin
  1087. ObjectC:=Parent^.Descendants;
  1088. for I:=0 to ObjectC^.Count-1 do
  1089. begin
  1090. OS:=ObjectC^.At(I);
  1091. if OS^.Symbol=O then
  1092. begin P:=OS; Break; end;
  1093. if OS^.Descendants<>nil then
  1094. begin
  1095. P:=ScanObjectCollection(OS);
  1096. if P<>nil then Break;
  1097. end;
  1098. end;
  1099. end;
  1100. ScanObjectCollection:=P;
  1101. end;
  1102. begin
  1103. SearchObjectForSymbol:=ScanObjectCollection(ObjectTree);
  1104. end;
  1105. {*****************************************************************************
  1106. Initialize
  1107. *****************************************************************************}
  1108. var
  1109. oldexit : pointer;
  1110. procedure browcol_exit;{$ifndef FPC}far;{$endif}
  1111. begin
  1112. exitproc:=oldexit;
  1113. DisposeBrowserCol;
  1114. end;
  1115. procedure InitBrowserCol;
  1116. begin
  1117. end;
  1118. procedure DoneBrowserCol;
  1119. begin
  1120. { nothing, the collections are freed in the exitproc }
  1121. end;
  1122. begin
  1123. oldexit:=exitproc;
  1124. exitproc:=@browcol_exit;
  1125. end.
  1126. {
  1127. $Log$
  1128. Revision 1.14 1999-04-15 09:01:32 peter
  1129. * fixed set loading
  1130. * object inheritance support for browser
  1131. Revision 1.13 1999/04/14 18:59:52 peter
  1132. * fixed wrong variable names
  1133. Revision 1.12 1999/04/10 16:15:00 peter
  1134. * fixed browcol
  1135. + -ar to show regalloc info in .s file
  1136. Revision 1.11 1999/04/08 10:17:42 peter
  1137. + objects support
  1138. Revision 1.8 1999/03/03 01:38:11 pierre
  1139. * avoid infinite recursion in ProcessDefIfStruct
  1140. Revision 1.7 1999/02/22 11:51:32 peter
  1141. * browser updates from gabor
  1142. Revision 1.6 1999/02/04 09:31:59 pierre
  1143. + added objects and records symbol tables
  1144. Revision 1.5 1999/02/03 09:44:32 pierre
  1145. * symbol nubering begins with 1 in number_symbols
  1146. * program tmodule has globalsymtable for its staticsymtable
  1147. (to get it displayed in IDE globals list)
  1148. + list of symbol (browcol) greatly improved for IDE
  1149. Revision 1.4 1999/02/02 16:38:38 peter
  1150. * no endless loop with localst=staticsymtable
  1151. Revision 1.3 1999/01/22 10:19:43 peter
  1152. * fixed typo
  1153. Revision 1.2 1999/01/21 11:49:14 peter
  1154. * updates from gabor
  1155. Revision 1.1 1999/01/12 14:25:24 peter
  1156. + BrowserLog for browser.log generation
  1157. + BrowserCol for browser info in TCollections
  1158. * released all other UseBrowser
  1159. }