browcol.pas 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Berczi Gabor
  4. Modifications Copyright (c) 1999-2002 Florian Klaempfl and Pierre Muller
  5. Support routines for getting browser info in collections
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. {$ifdef TP}
  20. {$N+,E+}
  21. {$endif}
  22. unit browcol;
  23. interface
  24. uses
  25. objects,
  26. cclasses,
  27. symconst,symtable;
  28. {$ifndef FPC}
  29. type
  30. sw_integer = integer;
  31. {$endif FPC}
  32. const
  33. SymbolTypLen : integer = 6;
  34. RecordTypes : set of tsymtyp =
  35. ([typesym,unitsym]);
  36. sfRecord = $00000001;
  37. sfObject = $00000002;
  38. sfClass = $00000004;
  39. sfPointer = $00000008;
  40. sfHasMemInfo = $80000000;
  41. type
  42. TStoreCollection = object(TStringCollection)
  43. function Add(const S: string): PString;
  44. end;
  45. PModuleNameCollection = ^TModuleNameCollection;
  46. TModuleNameCollection = object(TStoreCollection)
  47. end;
  48. PTypeNameCollection = ^TTypeNameCollection;
  49. TTypeNameCollection = object(TStoreCollection)
  50. end;
  51. PSymbolCollection = ^TSymbolCollection;
  52. PSortedSymbolCollection = ^TSortedSymbolCollection;
  53. PReferenceCollection = ^TReferenceCollection;
  54. PReference = ^TReference;
  55. TReference = object(TObject)
  56. FileName : PString;
  57. Position : TPoint;
  58. constructor Init(AFileName: PString; ALine, AColumn: Sw_integer);
  59. function GetFileName: string;
  60. destructor Done; virtual;
  61. constructor Load(var S: TStream);
  62. procedure Store(var S: TStream);
  63. end;
  64. PSymbolMemInfo = ^TSymbolMemInfo;
  65. TSymbolMemInfo = record
  66. Addr : longint;
  67. Size : longint;
  68. PushSize : longint;
  69. end;
  70. PSymbol = ^TSymbol;
  71. TSymbol = object(TObject)
  72. Name : PString;
  73. Typ : tsymtyp;
  74. Params : PString;
  75. References : PReferenceCollection;
  76. Items : PSymbolCollection;
  77. DType : PString;
  78. VType : PString;
  79. TypeID : Ptrint;
  80. RelatedTypeID : Ptrint;
  81. DebuggerCount : longint;
  82. Ancestor : PSymbol;
  83. Flags : longint;
  84. MemInfo : PSymbolMemInfo;
  85. constructor Init(const AName: string; ATyp: tsymtyp; AParams: string; AMemInfo: PSymbolMemInfo);
  86. procedure SetMemInfo(const AMemInfo: TSymbolMemInfo);
  87. function GetReferenceCount: Sw_integer;
  88. function GetReference(Index: Sw_integer): PReference;
  89. function GetItemCount: Sw_integer;
  90. function GetItem(Index: Sw_integer): PSymbol;
  91. function GetName: string;
  92. function GetText: string;
  93. function GetTypeName: string;
  94. destructor Done; virtual;
  95. constructor Load(var S: TStream);
  96. procedure Store(var S: TStream);
  97. end;
  98. PExport = ^TExport;
  99. TExport = object(TObject)
  100. constructor Init(const AName: string; AIndex: longint; ASymbol: PSymbol);
  101. function GetDisplayText: string;
  102. destructor Done; virtual;
  103. private
  104. Name: PString;
  105. Index: longint;
  106. Symbol: PSymbol;
  107. end;
  108. PExportCollection = ^TExportCollection;
  109. TExportCollection = object(TSortedCollection)
  110. function At(Index: sw_Integer): PExport;
  111. function Compare(Key1, Key2: Pointer): sw_Integer; virtual;
  112. end;
  113. PImport = ^TImport;
  114. TImport = object(TObject)
  115. constructor Init(const ALibName, AFuncName,ARealName: string; AIndex: longint);
  116. function GetDisplayText: string;
  117. destructor Done; virtual;
  118. private
  119. LibName: PString;
  120. FuncName: PString;
  121. RealName: PString;
  122. Index: longint;
  123. end;
  124. PImportCollection = ^TImportCollection;
  125. TImportCollection = object(TSortedCollection)
  126. function At(Index: sw_Integer): PImport;
  127. function Compare(Key1, Key2: Pointer): sw_Integer; virtual;
  128. end;
  129. PObjectSymbolCollection = ^TObjectSymbolCollection;
  130. PObjectSymbol = ^TObjectSymbol;
  131. TObjectSymbol = object(TObject)
  132. Parent : PObjectSymbol;
  133. Symbol : PSymbol;
  134. Expanded : boolean;
  135. constructor Init(AParent: PObjectSymbol; ASymbol: PSymbol);
  136. constructor InitName(const AName: string);
  137. function GetName: string;
  138. function GetDescendantCount: sw_integer;
  139. function GetDescendant(Index: sw_integer): PObjectSymbol;
  140. procedure AddDescendant(P: PObjectSymbol);
  141. destructor Done; virtual;
  142. constructor Load(var S: TStream);
  143. procedure Store(S: TStream);
  144. private
  145. Name: PString;
  146. Descendants: PObjectSymbolCollection;
  147. end;
  148. TSymbolCollection = object(TSortedCollection)
  149. constructor Init(ALimit, ADelta: Integer);
  150. function At(Index: Sw_Integer): PSymbol;
  151. procedure Insert(Item: Pointer); virtual;
  152. function LookUp(const S: string; var Idx: sw_integer): string; virtual;
  153. end;
  154. TSortedSymbolCollection = object(TSymbolCollection)
  155. function Compare(Key1, Key2: Pointer): Sw_Integer; virtual;
  156. procedure Insert(Item: Pointer); virtual;
  157. function LookUp(const S: string; var Idx: sw_integer): string; virtual;
  158. end;
  159. PIDSortedSymbolCollection = ^TIDSortedSymbolCollection;
  160. TIDSortedSymbolCollection = object(TSymbolCollection)
  161. function Compare(Key1, Key2: Pointer): Sw_Integer; virtual;
  162. procedure Insert(Item: Pointer); virtual;
  163. function SearchSymbolByID(AID: longint): PSymbol;
  164. end;
  165. TObjectSymbolCollection = object(TSortedCollection)
  166. constructor Init(ALimit, ADelta: Integer);
  167. function Compare(Key1, Key2: Pointer): Sw_Integer; virtual;
  168. function LookUp(const S: string; var Idx: sw_integer): string; virtual;
  169. function At(Index: Sw_Integer): PObjectSymbol;
  170. end;
  171. TReferenceCollection = object(TCollection)
  172. function At(Index: Sw_Integer): PReference;
  173. end;
  174. PSourceFile = ^TSourceFile;
  175. TSourceFile = object(TObject)
  176. SourceFileName: PString;
  177. ObjFileName: PString;
  178. PPUFileName: PString;
  179. constructor Init(ASourceFileName, AObjFileName, APPUFileName: string);
  180. destructor Done; virtual;
  181. function GetSourceFilename: string;
  182. function GetObjFileName: string;
  183. function GetPPUFileName: string;
  184. end;
  185. PSourceFileCollection = ^TSourceFileCollection;
  186. TSourceFileCollection = object(TCollection)
  187. function At(Index: sw_Integer): PSourceFile;
  188. end;
  189. PModuleSymbol = ^TModuleSymbol;
  190. TModuleSymbol = object(TSymbol)
  191. Exports_ : PExportCollection;
  192. Imports : PImportCollection;
  193. LoadedFrom : PString;
  194. UsedUnits : PSymbolCollection;
  195. DependentUnits: PSymbolCollection;
  196. MainSource: PString;
  197. SourceFiles: PStringCollection;
  198. constructor Init(const AName, AMainSource: string);
  199. procedure SetLoadedFrom(const AModuleName: string);
  200. procedure AddUsedUnit(P: PSymbol);
  201. procedure AddDependentUnit(P: PSymbol);
  202. procedure AddSourceFile(const Path: string);
  203. destructor Done; virtual;
  204. end;
  205. const
  206. Modules : PSymbolCollection = nil;
  207. ModuleNames : PModuleNameCollection = nil;
  208. TypeNames : PTypeNameCollection = nil;
  209. ObjectTree : PObjectSymbol = nil;
  210. SourceFiles : PSourceFileCollection = nil;
  211. procedure DisposeBrowserCol;
  212. procedure NewBrowserCol;
  213. procedure CreateBrowserCol;
  214. procedure InitBrowserCol;
  215. procedure DoneBrowserCol;
  216. function LoadBrowserCol(S: PStream): boolean;
  217. function StoreBrowserCol(S: PStream) : boolean;
  218. procedure BuildObjectInfo;
  219. procedure BuildSourceList;
  220. function SearchObjectForSymbol(O: PSymbol): PObjectSymbol;
  221. procedure RegisterSymbols;
  222. implementation
  223. uses
  224. Dos,{$ifndef FPC}strings,{$endif}
  225. {$ifdef DEBUG}
  226. verbose,
  227. {$endif DEBUG}
  228. CUtils,
  229. globtype,globals,comphook,
  230. finput,fmodule,
  231. cpuinfo,cgbase,aasmbase,aasmtai,paramgr,
  232. symsym,symdef,symtype,symbase,defutil;
  233. const
  234. RModuleNameCollection: TStreamRec = (
  235. ObjType: 3001;
  236. VmtLink: Ofs(TypeOf(TModuleNameCollection)^);
  237. Load: @TModuleNameCollection.Load;
  238. Store: @TModuleNameCollection.Store
  239. );
  240. RTypeNameCollection: TStreamRec = (
  241. ObjType: 3002;
  242. VmtLink: Ofs(TypeOf(TTypeNameCollection)^);
  243. Load: @TTypeNameCollection.Load;
  244. Store: @TTypeNameCollection.Store
  245. );
  246. RReference: TStreamRec = (
  247. ObjType: 3003;
  248. VmtLink: Ofs(TypeOf(TReference)^);
  249. Load: @TReference.Load;
  250. Store: @TReference.Store
  251. );
  252. RSymbol: TStreamRec = (
  253. ObjType: 3004;
  254. VmtLink: Ofs(TypeOf(TSymbol)^);
  255. Load: @TSymbol.Load;
  256. Store: @TSymbol.Store
  257. );
  258. RObjectSymbol: TStreamRec = (
  259. ObjType: 3005;
  260. VmtLink: Ofs(TypeOf(TObjectSymbol)^);
  261. Load: @TObjectSymbol.Load;
  262. Store: @TObjectSymbol.Store
  263. );
  264. RSymbolCollection: TStreamRec = (
  265. ObjType: 3006;
  266. VmtLink: Ofs(TypeOf(TSymbolCollection)^);
  267. Load: @TSymbolCollection.Load;
  268. Store: @TSymbolCollection.Store
  269. );
  270. RSortedSymbolCollection: TStreamRec = (
  271. ObjType: 3007;
  272. VmtLink: Ofs(TypeOf(TSortedSymbolCollection)^);
  273. Load: @TSortedSymbolCollection.Load;
  274. Store: @TSortedSymbolCollection.Store
  275. );
  276. RIDSortedSymbolCollection: TStreamRec = (
  277. ObjType: 3008;
  278. VmtLink: Ofs(TypeOf(TIDSortedSymbolCollection)^);
  279. Load: @TIDSortedSymbolCollection.Load;
  280. Store: @TIDSortedSymbolCollection.Store
  281. );
  282. RObjectSymbolCollection: TStreamRec = (
  283. ObjType: 3009;
  284. VmtLink: Ofs(TypeOf(TObjectSymbolCollection)^);
  285. Load: @TObjectSymbolCollection.Load;
  286. Store: @TObjectSymbolCollection.Store
  287. );
  288. RReferenceCollection: TStreamRec = (
  289. ObjType: 3010;
  290. VmtLink: Ofs(TypeOf(TReferenceCollection)^);
  291. Load: @TReferenceCollection.Load;
  292. Store: @TReferenceCollection.Store
  293. );
  294. RModuleSymbol: TStreamRec = (
  295. ObjType: 3011;
  296. VmtLink: Ofs(TypeOf(TModuleSymbol)^);
  297. Load: @TModuleSymbol.Load;
  298. Store: @TModuleSymbol.Store
  299. );
  300. {****************************************************************************
  301. Helpers
  302. ****************************************************************************}
  303. function GetStr(P: PString): string;
  304. begin
  305. if P=nil then
  306. GetStr:=''
  307. else
  308. GetStr:=P^;
  309. end;
  310. function IntToStr(L: longint): string;
  311. var S: string;
  312. begin
  313. Str(L,S);
  314. IntToStr:=S;
  315. end;
  316. function UpcaseStr(S: string): string;
  317. var I: integer;
  318. begin
  319. for I:=1 to length(S) do
  320. S[I]:=Upcase(S[I]);
  321. UpcaseStr:=S;
  322. end;
  323. function FloatToStr(E: extended): string;
  324. var S: string;
  325. begin
  326. Str(E:0:24,S);
  327. if Pos('.',S)>0 then
  328. begin
  329. while (length(S)>0) and (S[length(S)]='0') do
  330. Delete(S,length(S),1);
  331. if (length(S)>0) and (S[length(S)]='.') then
  332. Delete(S,length(S),1);
  333. end;
  334. if S='' then S:='0';
  335. FloatToStr:=S;
  336. end;
  337. {****************************************************************************
  338. TStoreCollection
  339. ****************************************************************************}
  340. function TStoreCollection.Add(const S: string): PString;
  341. var P: PString;
  342. Index: Sw_integer;
  343. begin
  344. if S='' then P:=nil else
  345. if Search(@S,Index) then P:=At(Index) else
  346. begin
  347. P:=NewStr(S);
  348. Insert(P);
  349. end;
  350. Add:=P;
  351. end;
  352. {****************************************************************************
  353. TSymbolCollection
  354. ****************************************************************************}
  355. constructor TSymbolCollection.Init(ALimit, ADelta: Integer);
  356. begin
  357. inherited Init(ALimit,ADelta);
  358. { Duplicates:=true;}
  359. end;
  360. function TSymbolCollection.At(Index: Sw_Integer): PSymbol;
  361. begin
  362. At:=inherited At(Index);
  363. end;
  364. procedure TSymbolCollection.Insert(Item: Pointer);
  365. begin
  366. TCollection.Insert(Item);
  367. end;
  368. function TSymbolCollection.LookUp(const S: string; var Idx: sw_integer): string;
  369. begin
  370. Idx:=-1;
  371. LookUp:='';
  372. end;
  373. {****************************************************************************
  374. TReferenceCollection
  375. ****************************************************************************}
  376. function TReferenceCollection.At(Index: Sw_Integer): PReference;
  377. begin
  378. At:=inherited At(Index);
  379. end;
  380. {****************************************************************************
  381. TSortedSymbolCollection
  382. ****************************************************************************}
  383. function TSortedSymbolCollection.Compare(Key1, Key2: Pointer): Sw_Integer;
  384. var K1: PSymbol absolute Key1;
  385. K2: PSymbol absolute Key2;
  386. R: Sw_integer;
  387. S1,S2: string;
  388. begin
  389. S1:=Upper(K1^.GetName);
  390. S2:=Upper(K2^.GetName);
  391. if S1<S2 then R:=-1 else
  392. if S1>S2 then R:=1 else
  393. if K1^.TypeID=K2^.TypeID then R:=0 else
  394. begin
  395. S1:=K1^.GetName;
  396. S2:=K2^.GetName;
  397. if S1<S2 then R:=-1 else
  398. if S1>S2 then R:=1 else
  399. if K1^.TypeID<K2^.TypeID then R:=-1 else
  400. if K1^.TypeID>K2^.TypeID then R:= 1 else
  401. R:=0;
  402. end;
  403. Compare:=R;
  404. end;
  405. procedure TSortedSymbolCollection.Insert(Item: Pointer);
  406. begin
  407. TSortedCollection.Insert(Item);
  408. end;
  409. function TSortedSymbolCollection.LookUp(const S: string; var Idx: sw_integer): string;
  410. var OLI,ORI,Left,Right,Mid: integer;
  411. LeftP,RightP,MidP: PSymbol;
  412. LeftS,MidS,RightS: string;
  413. FoundS: string;
  414. UpS : string;
  415. begin
  416. Idx:=-1; FoundS:='';
  417. Left:=0; Right:=Count-1;
  418. UpS:=Upper(S);
  419. if Left<Right then
  420. begin
  421. while (Left<Right) do
  422. begin
  423. OLI:=Left; ORI:=Right;
  424. Mid:=Left+(Right-Left) div 2;
  425. MidP:=At(Mid);
  426. {$ifdef DEBUG}
  427. LeftP:=At(Left); RightP:=At(Right);
  428. LeftS:=Upper(LeftP^.GetName);
  429. RightS:=Upper(RightP^.GetName);
  430. {$endif DEBUG}
  431. MidS:=Upper(MidP^.GetName);
  432. if copy(MidS,1,length(UpS))=UpS then
  433. begin
  434. Idx:=Mid;
  435. FoundS:=MidS;
  436. end;
  437. { else}
  438. if UpS<MidS then
  439. Right:=Mid
  440. else
  441. Left:=Mid;
  442. if (OLI=Left) and (ORI=Right) then
  443. begin
  444. if idX<>-1 then
  445. break;
  446. if Mid=Left then
  447. begin
  448. RightP:=At(Right);
  449. RightS:=Upper(RightP^.GetName);
  450. if copy(RightS,1,length(UpS))=UpS then
  451. begin
  452. Idx:=Right;
  453. FoundS:=RightS;
  454. end;
  455. end;
  456. if Mid=Right then
  457. begin
  458. LeftP:=At(Left);
  459. LeftS:=Upper(LeftP^.GetName);
  460. if copy(LeftS,1,length(UpS))=UpS then
  461. begin
  462. Idx:=Left;
  463. FoundS:=LeftS;
  464. end;
  465. end;
  466. Break;
  467. end;
  468. end;
  469. end;
  470. LookUp:=FoundS;
  471. end;
  472. {****************************************************************************
  473. TIDSortedSymbolCollection
  474. ****************************************************************************}
  475. function TIDSortedSymbolCollection.Compare(Key1, Key2: Pointer): Sw_Integer;
  476. var K1: PSymbol absolute Key1;
  477. K2: PSymbol absolute Key2;
  478. R: Sw_integer;
  479. begin
  480. if K1^.TypeID<K2^.TypeID then R:=-1 else
  481. if K1^.TypeID>K2^.TypeID then R:= 1 else
  482. R:=0;
  483. Compare:=R;
  484. end;
  485. procedure TIDSortedSymbolCollection.Insert(Item: Pointer);
  486. begin
  487. TSortedCollection.Insert(Item);
  488. end;
  489. function TIDSortedSymbolCollection.SearchSymbolByID(AID: longint): PSymbol;
  490. var S: TSymbol;
  491. Index: sw_integer;
  492. P: PSymbol;
  493. begin
  494. S.TypeID:=AID;
  495. if Search(@S,Index)=false then P:=nil else
  496. P:=At(Index);
  497. SearchSymbolByID:=P;
  498. end;
  499. {****************************************************************************
  500. TObjectSymbolCollection
  501. ****************************************************************************}
  502. function TObjectSymbolCollection.At(Index: Sw_Integer): PObjectSymbol;
  503. begin
  504. At:=inherited At(Index);
  505. end;
  506. constructor TObjectSymbolCollection.Init(ALimit, ADelta: Integer);
  507. begin
  508. inherited Init(ALimit,ADelta);
  509. end;
  510. function TObjectSymbolCollection.Compare(Key1, Key2: Pointer): Sw_Integer;
  511. var K1: PObjectSymbol absolute Key1;
  512. K2: PObjectSymbol absolute Key2;
  513. R: Sw_integer;
  514. S1,S2: string;
  515. begin
  516. S1:=Upper(K1^.GetName);
  517. S2:=Upper(K2^.GetName);
  518. if S1<S2 then R:=-1 else
  519. if S1>S2 then R:=1 else
  520. { make sure that we distinguish between different objects with the same name }
  521. if Ptrint(K1^.Symbol)<Ptrint(K2^.Symbol) then R:=-1 else
  522. if Ptrint(K1^.Symbol)>Ptrint(K2^.Symbol) then R:= 1 else
  523. R:=0;
  524. Compare:=R;
  525. end;
  526. function TObjectSymbolCollection.LookUp(const S: string; var Idx: sw_integer): string;
  527. var OLI,ORI,Left,Right,Mid: integer;
  528. {LeftP,RightP,}MidP: PObjectSymbol;
  529. {LeftS,RightS,}MidS: string;
  530. FoundS: string;
  531. UpS : string;
  532. begin
  533. Idx:=-1; FoundS:='';
  534. Left:=0; Right:=Count-1;
  535. UpS:=Upper(S);
  536. if Left<Right then
  537. begin
  538. while (Left<Right) do
  539. begin
  540. OLI:=Left; ORI:=Right;
  541. Mid:=Left+(Right-Left) div 2;
  542. {LeftP:=At(Left);
  543. LeftS:=Upper(LeftP^.GetName);}
  544. MidP:=At(Mid);
  545. MidS:=Upper(MidP^.GetName);
  546. {RightP:=At(Right);
  547. RightS:=Upper(RightP^.GetName);}
  548. if copy(MidS,1,length(UpS))=UpS then
  549. begin
  550. Idx:=Mid;
  551. FoundS:=MidS;
  552. end;
  553. { else}
  554. if UpS<MidS then
  555. Right:=Mid
  556. else
  557. Left:=Mid;
  558. if (OLI=Left) and (ORI=Right) then
  559. Break;
  560. end;
  561. end;
  562. LookUp:=FoundS;
  563. end;
  564. {****************************************************************************
  565. TReference
  566. ****************************************************************************}
  567. constructor TReference.Init(AFileName: PString; ALine, AColumn: Sw_integer);
  568. begin
  569. inherited Init;
  570. FileName:=AFileName;
  571. Position.X:=AColumn;
  572. Position.Y:=ALine;
  573. end;
  574. function TReference.GetFileName: string;
  575. begin
  576. GetFileName:=GetStr(FileName);
  577. end;
  578. destructor TReference.Done;
  579. begin
  580. inherited Done;
  581. end;
  582. constructor TReference.Load(var S: TStream);
  583. begin
  584. S.Read(Position, SizeOf(Position));
  585. { --- items needing fixup --- }
  586. S.Read(FileName, SizeOf(FileName)); { ->ModulesNames^.Item }
  587. end;
  588. procedure TReference.Store(var S: TStream);
  589. begin
  590. S.Write(Position, SizeOf(Position));
  591. { --- items needing fixup --- }
  592. S.Write(FileName, SizeOf(FileName));
  593. end;
  594. {****************************************************************************
  595. TSymbol
  596. ****************************************************************************}
  597. constructor TSymbol.Init(const AName: string; ATyp: tsymtyp; AParams: string; AMemInfo: PSymbolMemInfo);
  598. begin
  599. inherited Init;
  600. Name:=NewStr(AName); Typ:=ATyp;
  601. if AMemInfo<>nil then
  602. SetMemInfo(AMemInfo^);
  603. New(References, Init(20,50));
  604. if ATyp in RecordTypes then
  605. begin
  606. Items:=New(PSortedSymbolCollection, Init(50,100));
  607. end;
  608. end;
  609. procedure TSymbol.SetMemInfo(const AMemInfo: TSymbolMemInfo);
  610. begin
  611. if MemInfo=nil then New(MemInfo);
  612. Move(AMemInfo,MemInfo^,SizeOf(MemInfo^));
  613. Flags:=Flags or sfHasMemInfo;
  614. end;
  615. function TSymbol.GetReferenceCount: Sw_integer;
  616. var Count: Sw_integer;
  617. begin
  618. if References=nil then Count:=0 else
  619. Count:=References^.Count;
  620. GetReferenceCount:=Count;
  621. end;
  622. function TSymbol.GetReference(Index: Sw_integer): PReference;
  623. begin
  624. GetReference:=References^.At(Index);
  625. end;
  626. function TSymbol.GetItemCount: Sw_integer;
  627. var Count: Sw_integer;
  628. begin
  629. if Items=nil then Count:=0 else
  630. Count:=Items^.Count;
  631. GetItemCount:=Count;
  632. end;
  633. function TSymbol.GetItem(Index: Sw_integer): PSymbol;
  634. begin
  635. GetItem:=Items^.At(Index);
  636. end;
  637. function TSymbol.GetName: string;
  638. begin
  639. GetName:=GetStr(Name);
  640. end;
  641. function TSymbol.GetText: string;
  642. var S: string;
  643. begin
  644. S:=GetTypeName;
  645. if length(S)>SymbolTypLen then
  646. S:=Copy(S,1,SymbolTypLen)
  647. else
  648. begin
  649. while length(S)<SymbolTypLen do
  650. S:=S+' ';
  651. end;
  652. S:=S+' '+GetName;
  653. if (Flags and sfRecord)<>0 then
  654. S:=S+' = record'
  655. else
  656. if (Flags and sfObject)<>0 then
  657. begin
  658. S:=S+' = ';
  659. if (Flags and sfClass)<>0 then
  660. S:=S+'class'
  661. else
  662. S:=S+'object';
  663. if Ancestor<>nil then
  664. S:=S+'('+Ancestor^.GetName+')';
  665. end
  666. else
  667. begin
  668. if Assigned(DType) then
  669. S:=S+' = '+DType^;
  670. if Assigned(Params) then
  671. S:=S+'('+Params^+')';
  672. if Assigned(VType) then
  673. S:=S+': '+VType^;
  674. end;
  675. GetText:=S;
  676. end;
  677. function TSymbol.GetTypeName: string;
  678. var S: string;
  679. begin
  680. case Typ of
  681. abstractsym : S:='abst';
  682. varsym : S:='var';
  683. typesym : S:='type';
  684. procsym : if VType=nil then
  685. S:='proc'
  686. else
  687. S:='func';
  688. unitsym : S:='unit';
  689. constsym : S:='const';
  690. enumsym : S:='enum';
  691. typedconstsym: S:='const';
  692. errorsym : S:='error';
  693. syssym : S:='sys';
  694. labelsym : S:='label';
  695. absolutesym : S:='abs';
  696. propertysym : S:='prop';
  697. macrosym : S:='macro';
  698. else S:='';
  699. end;
  700. GetTypeName:=S;
  701. end;
  702. destructor TSymbol.Done;
  703. begin
  704. inherited Done;
  705. if assigned(MemInfo) then
  706. Dispose(MemInfo);
  707. if assigned(References) then
  708. Dispose(References, Done);
  709. if assigned(Items) then
  710. Dispose(Items, Done);
  711. if assigned(Name) then
  712. DisposeStr(Name);
  713. { if assigned(Params) then
  714. DisposeStr(Params); in TypeNames
  715. if assigned(VType) then
  716. DisposeStr(VType);
  717. if assigned(DType) then
  718. DisposeStr(DType);
  719. if assigned(Ancestor) then
  720. DisposeStr(Ancestor);}
  721. end;
  722. constructor TSymbol.Load(var S: TStream);
  723. var MI: TSymbolMemInfo;
  724. W: word;
  725. begin
  726. TObject.Init;
  727. S.Read(Typ,SizeOf(Typ));
  728. S.Read(TypeID, SizeOf(TypeID));
  729. S.Read(RelatedTypeID, SizeOf(RelatedTypeID));
  730. S.Read(Flags, SizeOf(Flags));
  731. Name:=S.ReadStr;
  732. if (Flags and sfHasMemInfo)<>0 then
  733. begin
  734. S.Read(MI,SizeOf(MI));
  735. SetMemInfo(MI);
  736. end;
  737. W:=0;
  738. S.Read(W,SizeOf(W));
  739. if (W and 1)<>0 then
  740. New(References, Load(S));
  741. if (W and 2)<>0 then
  742. New(Items, Load(S));
  743. { --- items needing fixup --- }
  744. S.Read(DType, SizeOf(DType));
  745. S.Read(VType, SizeOf(VType));
  746. S.Read(Params, SizeOf(Params));
  747. end;
  748. procedure TSymbol.Store(var S: TStream);
  749. var W: word;
  750. begin
  751. S.Write(Typ,SizeOf(Typ));
  752. S.Write(TypeID, SizeOf(TypeID));
  753. S.Write(RelatedTypeID, SizeOf(RelatedTypeID));
  754. S.Write(Flags, SizeOf(Flags));
  755. S.WriteStr(Name);
  756. if (Flags and sfHasMemInfo)<>0 then
  757. S.Write(MemInfo^,SizeOf(MemInfo^));
  758. W:=0;
  759. if Assigned(References) then W:=W or 1;
  760. if Assigned(Items) then W:=W or 2;
  761. S.Write(W,SizeOf(W));
  762. if Assigned(References) then References^.Store(S);
  763. if Assigned(Items) then Items^.Store(S);
  764. { --- items needing fixup --- }
  765. S.Write(DType, SizeOf(DType));
  766. S.Write(VType, SizeOf(VType));
  767. S.Write(Params, SizeOf(Params));
  768. end;
  769. constructor TExport.Init(const AName: string; AIndex: longint; ASymbol: PSymbol);
  770. begin
  771. inherited Init;
  772. Name:=NewStr(AName); Index:=AIndex;
  773. Symbol:=ASymbol;
  774. end;
  775. function TExport.GetDisplayText: string;
  776. var S: string;
  777. begin
  778. S:=GetStr(Name)+' '+IntToStr(Index);
  779. if Assigned(Symbol) and (UpcaseStr(Symbol^.GetName)<>UpcaseStr(GetStr(Name))) then
  780. S:=S+' ('+Symbol^.GetName+')';
  781. GetDisplayText:=S;
  782. end;
  783. destructor TExport.Done;
  784. begin
  785. if Assigned(Name) then DisposeStr(Name);
  786. inherited Done;
  787. end;
  788. constructor TImport.Init(const ALibName, AFuncName,ARealName: string; AIndex: longint);
  789. begin
  790. inherited Init;
  791. LibName:=NewStr(ALibName);
  792. FuncName:=NewStr(AFuncName); RealName:=NewStr(ARealName);
  793. Index:=AIndex;
  794. end;
  795. function TImport.GetDisplayText: string;
  796. var S: string;
  797. begin
  798. S:=GetStr(RealName);
  799. if Assigned(FuncName) then S:=GetStr(FuncName)+' ('+S+')';
  800. if S='' then S:=IntToStr(Index);
  801. S:=GetStr(LibName)+' '+S;
  802. GetDisplayText:=S;
  803. end;
  804. destructor TImport.Done;
  805. begin
  806. if Assigned(LibName) then DisposeStr(LibName);
  807. if Assigned(FuncName) then DisposeStr(FuncName);
  808. if Assigned(RealName) then DisposeStr(RealName);
  809. inherited Done;
  810. end;
  811. function TImportCollection.At(Index: sw_Integer): PImport;
  812. begin
  813. At:=inherited At(Index);
  814. end;
  815. function TImportCollection.Compare(Key1, Key2: Pointer): sw_Integer;
  816. var K1: PImport absolute Key1;
  817. K2: PImport absolute Key2;
  818. S1: string;
  819. S2: string;
  820. R: sw_integer;
  821. begin
  822. if (K1^.RealName=nil) and (K2^.RealName<>nil) then R:= 1 else
  823. if (K1^.RealName<>nil) and (K2^.RealName=nil) then R:=-1 else
  824. if (K1^.RealName=nil) and (K2^.RealName=nil) then
  825. begin
  826. if K1^.Index<K2^.Index then R:=-1 else
  827. if K1^.Index>K2^.Index then R:= 1 else
  828. R:=0;
  829. end
  830. else
  831. begin
  832. if K1^.FuncName=nil then S1:=GetStr(K1^.RealName) else S1:=GetStr(K1^.FuncName);
  833. if K2^.FuncName=nil then S2:=GetStr(K2^.RealName) else S2:=GetStr(K2^.FuncName);
  834. S1:=UpcaseStr(S1); S2:=UpcaseStr(S2);
  835. if S1<S2 then R:=-1 else
  836. if S1>S2 then R:= 1 else
  837. R:=0;
  838. end;
  839. Compare:=R;
  840. end;
  841. function TExportCollection.At(Index: sw_Integer): PExport;
  842. begin
  843. At:=inherited At(Index);
  844. end;
  845. function TExportCollection.Compare(Key1, Key2: Pointer): sw_Integer;
  846. var K1: PExport absolute Key1;
  847. K2: PExport absolute Key2;
  848. S1: string;
  849. S2: string;
  850. R: sw_integer;
  851. begin
  852. S1:=UpcaseStr(GetStr(K1^.Name)); S2:=UpcaseStr(GetStr(K2^.Name));
  853. if S1<S2 then R:=-1 else
  854. if S1>S2 then R:= 1 else
  855. R:=0;
  856. Compare:=R;
  857. end;
  858. constructor TModuleSymbol.Init(const AName, AMainSource: string);
  859. begin
  860. inherited Init(AName,unitsym,'',nil);
  861. MainSource:=NewStr(AMainSource);
  862. end;
  863. procedure TModuleSymbol.SetLoadedFrom(const AModuleName: string);
  864. begin
  865. SetStr(LoadedFrom,AModuleName);
  866. end;
  867. procedure TModuleSymbol.AddUsedUnit(P: PSymbol);
  868. begin
  869. if Assigned(UsedUnits)=false then
  870. New(UsedUnits, Init(10,10));
  871. UsedUnits^.Insert(P);
  872. end;
  873. procedure TModuleSymbol.AddDependentUnit(P: PSymbol);
  874. begin
  875. if Assigned(DependentUnits)=false then
  876. New(DependentUnits, Init(10,10));
  877. DependentUnits^.Insert(P);
  878. end;
  879. procedure TModuleSymbol.AddSourceFile(const Path: string);
  880. begin
  881. if Assigned(SourceFiles)=false then
  882. New(SourceFiles, Init(10,10));
  883. sourcefiles^.Insert(NewStr(Path));
  884. end;
  885. destructor TModuleSymbol.Done;
  886. begin
  887. inherited Done;
  888. if Assigned(MainSource) then DisposeStr(MainSource);
  889. if assigned(Exports_) then
  890. Dispose(Exports_, Done);
  891. if Assigned(Imports) then
  892. Dispose(Imports, Done);
  893. if Assigned(LoadedFrom) then
  894. DisposeStr(LoadedFrom);
  895. if Assigned(UsedUnits) then
  896. begin
  897. UsedUnits^.DeleteAll;
  898. Dispose(UsedUnits, Done);
  899. end;
  900. if Assigned(DependentUnits) then
  901. begin
  902. DependentUnits^.DeleteAll;
  903. Dispose(DependentUnits, Done);
  904. end;
  905. if Assigned(SourceFiles) then Dispose(SourceFiles, Done);
  906. end;
  907. constructor TObjectSymbol.Init(AParent: PObjectSymbol; ASymbol: PSymbol);
  908. begin
  909. inherited Init;
  910. Parent:=AParent;
  911. Symbol:=ASymbol;
  912. end;
  913. constructor TObjectSymbol.InitName(const AName: string);
  914. begin
  915. inherited Init;
  916. Name:=NewStr(AName);
  917. end;
  918. function TObjectSymbol.GetName: string;
  919. begin
  920. if Name<>nil then
  921. GetName:=Name^
  922. else
  923. GetName:=Symbol^.GetName;
  924. end;
  925. function TObjectSymbol.GetDescendantCount: sw_integer;
  926. var Count: sw_integer;
  927. begin
  928. if Descendants=nil then Count:=0 else
  929. Count:=Descendants^.Count;
  930. GetDescendantCount:=Count;
  931. end;
  932. function TObjectSymbol.GetDescendant(Index: sw_integer): PObjectSymbol;
  933. begin
  934. GetDescendant:=Descendants^.At(Index);
  935. end;
  936. procedure TObjectSymbol.AddDescendant(P: PObjectSymbol);
  937. begin
  938. if Descendants=nil then
  939. New(Descendants, Init(50,10));
  940. Descendants^.Insert(P);
  941. end;
  942. destructor TObjectSymbol.Done;
  943. begin
  944. if Assigned(Name) then DisposeStr(Name); Name:=nil;
  945. if Assigned(Descendants) then Dispose(Descendants, Done); Descendants:=nil;
  946. inherited Done;
  947. end;
  948. constructor TObjectSymbol.Load(var S: TStream);
  949. begin
  950. end;
  951. procedure TObjectSymbol.Store(S: TStream);
  952. begin
  953. end;
  954. {****************************************************************************
  955. TSourceFile
  956. ****************************************************************************}
  957. constructor TSourceFile.Init(ASourceFileName, AObjFileName, APPUFileName: string);
  958. begin
  959. inherited Init;
  960. SourceFileName:=NewStr(ASourceFileName);
  961. ObjFileName:=NewStr(AObjFileName);
  962. PPUFileName:=NewStr(APPUFileName);
  963. end;
  964. destructor TSourceFile.Done;
  965. begin
  966. if assigned(SourceFileName) then DisposeStr(SourceFileName);
  967. if assigned(ObjFileName) then DisposeStr(ObjFileName);
  968. if assigned(PPUFileName) then DisposeStr(PPUFileName);
  969. inherited Done;
  970. end;
  971. function TSourceFile.GetSourceFilename: string;
  972. begin
  973. GetSourceFilename:=GetStr(SourceFileName);
  974. end;
  975. function TSourceFile.GetObjFileName: string;
  976. begin
  977. GetObjFilename:=GetStr(ObjFileName);
  978. end;
  979. function TSourceFile.GetPPUFileName: string;
  980. begin
  981. GetPPUFilename:=GetStr(PPUFileName);
  982. end;
  983. function TSourceFileCollection.At(Index: sw_Integer): PSourceFile;
  984. begin
  985. At:=inherited At(Index);
  986. end;
  987. {*****************************************************************************
  988. Main Routines
  989. *****************************************************************************}
  990. procedure DisposeBrowserCol;
  991. begin
  992. if assigned(Modules) then
  993. begin
  994. dispose(Modules,Done);
  995. Modules:=nil;
  996. end;
  997. if assigned(ModuleNames) then
  998. begin
  999. dispose(ModuleNames,Done);
  1000. ModuleNames:=nil;
  1001. end;
  1002. if assigned(TypeNames) then
  1003. begin
  1004. dispose(TypeNames,Done);
  1005. TypeNames:=nil;
  1006. end;
  1007. if assigned(ObjectTree) then
  1008. begin
  1009. Dispose(ObjectTree, Done);
  1010. ObjectTree:=nil;
  1011. end;
  1012. end;
  1013. procedure NewBrowserCol;
  1014. begin
  1015. New(Modules, Init(50,50));
  1016. New(ModuleNames, Init(50,50));
  1017. New(TypeNames, Init(1000,5000));
  1018. end;
  1019. procedure ProcessSymTable(OwnerSym: PSymbol; var Owner: PSymbolCollection; Table: TSymTable);
  1020. var J: longint;
  1021. Ref: TRef;
  1022. Sym: TSym;
  1023. Symbol: PSymbol;
  1024. Reference: PReference;
  1025. inputfile : Tinputfile;
  1026. procedure SetVType(Symbol: PSymbol; VType: string);
  1027. begin
  1028. Symbol^.VType:=TypeNames^.Add(VType);
  1029. end;
  1030. procedure SetDType(Symbol: PSymbol; DType: string);
  1031. begin
  1032. Symbol^.DType:=TypeNames^.Add(DType);
  1033. end;
  1034. function GetDefinitionStr(def: tdef): string; forward;
  1035. function GetEnumDefStr(def: tenumdef): string;
  1036. var Name: string;
  1037. esym: tenumsym;
  1038. Count: integer;
  1039. begin
  1040. Name:='(';
  1041. esym:=tenumsym(def.Firstenum); Count:=0;
  1042. while (esym<>nil) do
  1043. begin
  1044. if Count>0 then
  1045. Name:=Name+', ';
  1046. Name:=Name+esym.name;
  1047. esym:=esym.nextenum;
  1048. Inc(Count);
  1049. end;
  1050. Name:=Name+')';
  1051. GetEnumDefStr:=Name;
  1052. end;
  1053. function GetArrayDefStr(def: tarraydef): string;
  1054. var Name: string;
  1055. begin
  1056. Name:='array ['+IntToStr(def.lowrange)+'..'+IntToStr(def.highrange)+'] of ';
  1057. if assigned(def.elementtype.def) then
  1058. Name:=Name+GetDefinitionStr(def.elementtype.def);
  1059. GetArrayDefStr:=Name;
  1060. end;
  1061. function GetFileDefStr(def: tfiledef): string;
  1062. var Name: string;
  1063. begin
  1064. Name:='';
  1065. case def.filetyp of
  1066. ft_text : Name:='text';
  1067. ft_untyped : Name:='file';
  1068. ft_typed : Name:='file of '+GetDefinitionStr(def.typedfiletype.def);
  1069. end;
  1070. GetFileDefStr:=Name;
  1071. end;
  1072. function GetStringDefStr(def: tstringdef): string;
  1073. var Name: string;
  1074. begin
  1075. Name:='';
  1076. case def.string_typ of
  1077. st_shortstring :
  1078. if def.len=255 then
  1079. Name:='shortstring'
  1080. else
  1081. Name:='string['+IntToStr(def.len)+']';
  1082. st_longstring :
  1083. Name:='longstring';
  1084. st_ansistring :
  1085. Name:='ansistring';
  1086. st_widestring :
  1087. Name:='widestring';
  1088. else ;
  1089. end;
  1090. GetStringDefStr:=Name;
  1091. end;
  1092. function retdefassigned(def: tabstractprocdef): boolean;
  1093. var OK: boolean;
  1094. begin
  1095. OK:=false;
  1096. if assigned(def.rettype.def) then
  1097. if UpcaseStr(GetDefinitionStr(def.rettype.def))<>'VOID' then
  1098. OK:=true;
  1099. retdefassigned:=OK;
  1100. end;
  1101. function GetAbsProcParmDefStr(def: tabstractprocdef): string;
  1102. var Name: string;
  1103. dc: tparaitem;
  1104. Count: integer;
  1105. CurName: string;
  1106. begin
  1107. Name:='';
  1108. dc:=tparaitem(def.para.first);
  1109. Count:=0;
  1110. while assigned(dc) do
  1111. begin
  1112. CurName:='';
  1113. case dc.paratyp of
  1114. vs_Value : ;
  1115. vs_Const : CurName:=CurName+'const ';
  1116. vs_Var : CurName:=CurName+'var ';
  1117. end;
  1118. if assigned(dc.paratype.def) then
  1119. CurName:=CurName+GetDefinitionStr(dc.paratype.def);
  1120. if dc.next<>nil then
  1121. CurName:=', '+CurName;
  1122. Name:=CurName+Name;
  1123. dc:=tparaitem(dc.next);
  1124. Inc(Count);
  1125. end;
  1126. GetAbsProcParmDefStr:=Name;
  1127. end;
  1128. function GetAbsProcDefStr(def: tabstractprocdef): string;
  1129. var Name: string;
  1130. begin
  1131. Name:=GetAbsProcParmDefStr(def);
  1132. if Name<>'' then Name:='('+Name+')';
  1133. if retdefassigned(def) then
  1134. Name:='function'+Name+': '+GetDefinitionStr(def.rettype.def)
  1135. else
  1136. Name:='procedure'+Name;
  1137. GetAbsProcDefStr:=Name;
  1138. end;
  1139. function GetProcDefStr(def: tprocdef): string;
  1140. var DName: string;
  1141. {J: integer;}
  1142. begin
  1143. { DName:='';
  1144. if assigned(def) then
  1145. begin
  1146. if assigned(def.parast) then
  1147. begin
  1148. with def.parast^ do
  1149. for J:=1 to number_symbols do
  1150. begin
  1151. if J<>1 then DName:=DName+', ';
  1152. ParSym:=GetsymNr(J);
  1153. if ParSym=nil then Break;
  1154. DName:=DName+ParSym^.Name;
  1155. end;
  1156. end
  1157. end;}
  1158. DName:=GetAbsProcDefStr(def);
  1159. GetProcDefStr:=DName;
  1160. end;
  1161. function GetProcVarDefStr(def: tprocvardef): string;
  1162. begin
  1163. GetProcVarDefStr:=GetAbsProcDefStr(def);
  1164. end;
  1165. function GetSetDefStr(def: tsetdef): string;
  1166. var Name: string;
  1167. begin
  1168. Name:='';
  1169. case def.settype of
  1170. normset : Name:='set';
  1171. smallset : Name:='set';
  1172. varset : Name:='varset';
  1173. end;
  1174. Name:=Name+' of ';
  1175. Name:=Name+GetDefinitionStr(def.elementtype.def);
  1176. GetSetDefStr:=Name;
  1177. end;
  1178. function GetPointerDefStr(def: tpointerdef): string;
  1179. begin
  1180. GetPointerDefStr:='^'+GetDefinitionStr(def.pointertype.def);
  1181. end;
  1182. function GetDefinitionStr(def: tdef): string;
  1183. var Name: string;
  1184. begin
  1185. Name:='';
  1186. if def<>nil then
  1187. begin
  1188. if assigned(def.typesym) then
  1189. Name:=def.typesym.name;
  1190. if Name='' then
  1191. case def.deftype of
  1192. arraydef :
  1193. Name:=GetArrayDefStr(tarraydef(def));
  1194. stringdef :
  1195. Name:=GetStringDefStr(tstringdef(def));
  1196. enumdef :
  1197. Name:=GetEnumDefStr(tenumdef(def));
  1198. procdef :
  1199. Name:=GetProcDefStr(tprocdef(def));
  1200. procvardef :
  1201. Name:=GetProcVarDefStr(tprocvardef(def));
  1202. filedef :
  1203. Name:=GetFileDefStr(tfiledef(def));
  1204. setdef :
  1205. Name:=GetSetDefStr(tsetdef(def));
  1206. end;
  1207. end;
  1208. GetDefinitionStr:=Name;
  1209. end;
  1210. function GetEnumItemName(Sym: tenumsym): string;
  1211. var Name: string;
  1212. {ES: tenumsym;}
  1213. begin
  1214. Name:='';
  1215. if assigned(sym) and assigned(sym.definition) then
  1216. if assigned(sym.definition.typesym) then
  1217. begin
  1218. { ES:=sym.definition.First;
  1219. while (ES<>nil) and (ES^.Value<>sym.value) do
  1220. ES:=ES^.next;
  1221. if assigned(es) and (es^.value=sym.value) then
  1222. Name:=}
  1223. Name:=sym.definition.typesym.name;
  1224. if Name<>'' then
  1225. Name:=Name+'('+IntToStr(sym.value)+')';
  1226. end;
  1227. GetEnumItemName:=Name;
  1228. end;
  1229. function GetConstValueName(sym: tconstsym): string;
  1230. var Name: string;
  1231. begin
  1232. Name:='';
  1233. if Name='' then
  1234. case sym.consttyp of
  1235. constord :
  1236. begin
  1237. if sym.consttype.def.deftype=enumdef then
  1238. Name:=sym.consttype.def.typesym.name+'('+IntToStr(sym.value.valueord)+')'
  1239. else
  1240. if is_boolean(sym.consttype.def) then
  1241. Name:='Longbool('+IntToStr(sym.value.valueord)+')'
  1242. else
  1243. if is_char(sym.consttype.def) or
  1244. is_widechar(sym.consttype.def) then
  1245. Name:=''''+chr(sym.value.valueord)+''''
  1246. else
  1247. Name:=IntToStr(sym.value.valueord);
  1248. end;
  1249. constresourcestring,
  1250. conststring :
  1251. Name:=''''+StrPas(pchar(sym.value.valueptr))+'''';
  1252. constreal:
  1253. Name:=FloatToStr(PBestReal(sym.value.valueptr)^);
  1254. constset:
  1255. { Name:=SetToStr(pnormalset(sym.value.valueptr)) };
  1256. constnil: ;
  1257. end;
  1258. GetConstValueName:=Name;
  1259. end;
  1260. procedure ProcessDefIfStruct(definition: tdef);
  1261. begin
  1262. { still led to infinite recursions
  1263. only usefull for unamed types PM }
  1264. if assigned(definition) and not assigned(definition.typesym) then
  1265. begin
  1266. case definition.deftype of
  1267. recorddef :
  1268. if trecorddef(definition).symtable<>Table then
  1269. ProcessSymTable(Symbol,Symbol^.Items,trecorddef(definition).symtable);
  1270. objectdef :
  1271. if tobjectdef(definition).symtable<>Table then
  1272. ProcessSymTable(Symbol,Symbol^.Items,tobjectdef(definition).symtable);
  1273. { leads to infinite loops !!
  1274. pointerdef :
  1275. with tpointerdef(definition)^ do
  1276. if assigned(definition) then
  1277. if assigned(definition.sym) then
  1278. ProcessDefIfStruct(definition.sym.definition);}
  1279. end;
  1280. end;
  1281. end;
  1282. var MemInfo: TSymbolMemInfo;
  1283. ObjDef: tobjectdef;
  1284. begin
  1285. if not Assigned(Table) then
  1286. Exit;
  1287. if Owner=nil then
  1288. Owner:=New(PSortedSymbolCollection, Init(10,50));
  1289. sym:=tsym(Table.symindex.first);
  1290. while assigned(sym) do
  1291. begin
  1292. New(Symbol, Init(Sym.Name,Sym.Typ,'',nil));
  1293. case Sym.Typ of
  1294. varsym :
  1295. with tvarsym(sym) do
  1296. begin
  1297. if assigned(vartype.def) then
  1298. if assigned(vartype.def.typesym) then
  1299. SetVType(Symbol,vartype.def.typesym.name)
  1300. else
  1301. SetVType(Symbol,GetDefinitionStr(vartype.def));
  1302. ProcessDefIfStruct(vartype.def);
  1303. if assigned(vartype.def) then
  1304. if (vartype.def.deftype=pointerdef) and
  1305. assigned(tpointerdef(vartype.def).pointertype.def) then
  1306. begin
  1307. Symbol^.Flags:=(Symbol^.Flags or sfPointer);
  1308. Symbol^.RelatedTypeID:=Ptrint(tpointerdef(vartype.def).pointertype.def);
  1309. end;
  1310. if Table.symtabletype in [recordsymtable,objectsymtable] then
  1311. MemInfo.Addr:=fieldoffset
  1312. else
  1313. if localloc.loc=LOC_REFERENCE then
  1314. MemInfo.Addr:=localloc.reference.offset
  1315. else
  1316. MemInfo.Addr:=0;
  1317. if assigned(vartype.def) and (vartype.def.deftype=arraydef) then
  1318. begin
  1319. if tarraydef(vartype.def).highrange<tarraydef(vartype.def).lowrange then
  1320. MemInfo.Size:=-1
  1321. else
  1322. MemInfo.Size:=getsize;
  1323. end
  1324. else
  1325. MemInfo.Size:=getsize;
  1326. { this is not completely correct... }
  1327. MemInfo.PushSize:=paramanager.push_size(varspez,vartype.def,pocall_default);
  1328. Symbol^.SetMemInfo(MemInfo);
  1329. end;
  1330. constsym :
  1331. SetDType(Symbol,GetConstValueName(tconstsym(sym)));
  1332. enumsym :
  1333. if assigned(tenumsym(sym).definition) then
  1334. SetDType(Symbol,GetEnumItemName(tenumsym(sym)));
  1335. unitsym :
  1336. begin
  1337. { ProcessSymTable(Symbol^.Items,tunitsym(sym).unitsymtable);}
  1338. end;
  1339. syssym :
  1340. { if assigned(Table.Name) then
  1341. if Table.Name^='SYSTEM' then}
  1342. begin
  1343. Symbol^.Params:=TypeNames^.Add('...');
  1344. end;
  1345. procsym :
  1346. begin
  1347. with tprocsym(sym) do
  1348. if assigned(first_procdef) then
  1349. begin
  1350. if cs_local_browser in aktmoduleswitches then
  1351. ProcessSymTable(Symbol,Symbol^.Items,first_procdef.parast);
  1352. if assigned(first_procdef.parast) then
  1353. begin
  1354. Symbol^.Params:=TypeNames^.Add(GetAbsProcParmDefStr(first_procdef));
  1355. end
  1356. else { param-definition is NOT assigned }
  1357. if assigned(Table.Name) then
  1358. if Table.Name^='SYSTEM' then
  1359. begin
  1360. Symbol^.Params:=TypeNames^.Add('...');
  1361. end;
  1362. if cs_local_browser in aktmoduleswitches then
  1363. begin
  1364. if assigned(first_procdef.localst) and
  1365. (first_procdef.localst.symtabletype<>staticsymtable) then
  1366. ProcessSymTable(Symbol,Symbol^.Items,first_procdef.localst);
  1367. end;
  1368. end;
  1369. end;
  1370. typesym :
  1371. begin
  1372. with ttypesym(sym) do
  1373. if assigned(restype.def) then
  1374. begin
  1375. Symbol^.TypeID:=Ptrint(restype.def);
  1376. case restype.def.deftype of
  1377. arraydef :
  1378. SetDType(Symbol,GetArrayDefStr(tarraydef(restype.def)));
  1379. enumdef :
  1380. SetDType(Symbol,GetEnumDefStr(tenumdef(restype.def)));
  1381. procdef :
  1382. SetDType(Symbol,GetProcDefStr(tprocdef(restype.def)));
  1383. procvardef :
  1384. SetDType(Symbol,GetProcVarDefStr(tprocvardef(restype.def)));
  1385. objectdef :
  1386. with tobjectdef(restype.def) do
  1387. begin
  1388. ObjDef:=childof;
  1389. if ObjDef<>nil then
  1390. Symbol^.RelatedTypeID:=Ptrint(ObjDef);{TypeNames^.Add(S);}
  1391. Symbol^.Flags:=(Symbol^.Flags or sfObject);
  1392. if tobjectdef(restype.def).objecttype=odt_class then
  1393. Symbol^.Flags:=(Symbol^.Flags or sfClass);
  1394. ProcessSymTable(Symbol,Symbol^.Items,tobjectdef(restype.def).symtable);
  1395. end;
  1396. recorddef :
  1397. begin
  1398. Symbol^.Flags:=(Symbol^.Flags or sfRecord);
  1399. ProcessSymTable(Symbol,Symbol^.Items,trecorddef(restype.def).symtable);
  1400. end;
  1401. pointerdef :
  1402. begin
  1403. Symbol^.Flags:=(Symbol^.Flags or sfPointer);
  1404. Symbol^.RelatedTypeID:=Ptrint(tpointerdef(restype.def).pointertype.def);{TypeNames^.Add(S);}
  1405. SetDType(Symbol,GetPointerDefStr(tpointerdef(restype.def)));
  1406. end;
  1407. filedef :
  1408. SetDType(Symbol,GetFileDefStr(tfiledef(restype.def)));
  1409. setdef :
  1410. SetDType(Symbol,GetSetDefStr(tsetdef(restype.def)));
  1411. end;
  1412. end;
  1413. end;
  1414. end;
  1415. Ref:=tstoredsym(sym).defref;
  1416. while Assigned(Symbol) and assigned(Ref) do
  1417. begin
  1418. inputfile:=get_source_file(ref.moduleindex,ref.posinfo.fileindex);
  1419. if Assigned(inputfile) and Assigned(inputfile.name) then
  1420. begin
  1421. New(Reference, Init(ModuleNames^.Add(inputfile.name^),
  1422. ref.posinfo.line,ref.posinfo.column));
  1423. Symbol^.References^.Insert(Reference);
  1424. end;
  1425. Ref:=Ref.nextref;
  1426. end;
  1427. if Assigned(Symbol) then
  1428. begin
  1429. if not Owner^.Search(Symbol,J) then
  1430. Owner^.Insert(Symbol)
  1431. else
  1432. begin
  1433. Dispose(Symbol,done);
  1434. Symbol:=nil;
  1435. end;
  1436. end;
  1437. sym:=tsym(sym.indexnext);
  1438. end;
  1439. end;
  1440. function SearchModule(const Name: string): PModuleSymbol;
  1441. function Match(P: PModuleSymbol): boolean; {$ifndef FPC}far;{$endif}
  1442. begin
  1443. Match:=CompareText(P^.GetName,Name)=0;
  1444. end;
  1445. var P: PModuleSymbol;
  1446. begin
  1447. P:=nil;
  1448. if Assigned(Modules) then
  1449. P:=Modules^.FirstThat(@Match);
  1450. SearchModule:=P;
  1451. end;
  1452. procedure CreateBrowserCol;
  1453. var
  1454. T: TSymTable;
  1455. UnitS,PM: PModuleSymbol;
  1456. hp : tmodule;
  1457. puu: tused_unit;
  1458. pdu: tdependent_unit;
  1459. pif: tinputfile;
  1460. begin
  1461. DisposeBrowserCol;
  1462. if (cs_browser in aktmoduleswitches) then
  1463. NewBrowserCol;
  1464. hp:=tmodule(loaded_units.first);
  1465. if (cs_browser in aktmoduleswitches) then
  1466. while assigned(hp) do
  1467. begin
  1468. t:=tsymtable(hp.globalsymtable);
  1469. if assigned(t) then
  1470. begin
  1471. New(UnitS, Init(T.Name^,hp.mainsource^));
  1472. if Assigned(hp.loaded_from) then
  1473. if assigned(hp.loaded_from.globalsymtable) then
  1474. UnitS^.SetLoadedFrom(tsymtable(hp.loaded_from.globalsymtable).name^);
  1475. { pimportlist(current_module^.imports^.first);}
  1476. if assigned(hp.sourcefiles) then
  1477. begin
  1478. pif:=hp.sourcefiles.files;
  1479. while (pif<>nil) do
  1480. begin
  1481. UnitS^.AddSourceFile(pif.path^+pif.name^);
  1482. pif:=pif.next;
  1483. end;
  1484. end;
  1485. Modules^.Insert(UnitS);
  1486. ProcessSymTable(UnitS,UnitS^.Items,T);
  1487. if cs_local_browser in aktmoduleswitches then
  1488. begin
  1489. t:=tsymtable(hp.localsymtable);
  1490. if assigned(t) then
  1491. ProcessSymTable(UnitS,UnitS^.Items,T);
  1492. end;
  1493. end;
  1494. hp:=tmodule(hp.next);
  1495. end;
  1496. hp:=tmodule(loaded_units.first);
  1497. if (cs_browser in aktmoduleswitches) then
  1498. while assigned(hp) do
  1499. begin
  1500. t:=tsymtable(hp.globalsymtable);
  1501. if assigned(t) then
  1502. begin
  1503. UnitS:=SearchModule(T.Name^);
  1504. puu:=tused_unit(hp.used_units.first);
  1505. while (puu<>nil) do
  1506. begin
  1507. PM:=SearchModule(puu.u.modulename^);
  1508. if Assigned(PM) then
  1509. UnitS^.AddUsedUnit(PM);
  1510. puu:=tused_unit(puu.next);
  1511. end;
  1512. pdu:=tdependent_unit(hp.dependent_units.first);
  1513. while (pdu<>nil) do
  1514. begin
  1515. PM:=SearchModule(tsymtable(pdu.u.globalsymtable).name^);
  1516. if Assigned(PM) then
  1517. UnitS^.AddDependentUnit(PM);
  1518. pdu:=tdependent_unit(pdu.next);
  1519. end;
  1520. end;
  1521. hp:=tmodule(hp.next);
  1522. end;
  1523. if (cs_browser in aktmoduleswitches) then
  1524. BuildObjectInfo;
  1525. { can allways be done
  1526. needed to know when recompilation of sources is necessary }
  1527. BuildSourceList;
  1528. end;
  1529. procedure BuildObjectInfo;
  1530. var C,D: PIDSortedSymbolCollection;
  1531. E : PCollection;
  1532. ObjectC: PObjectSymbolCollection;
  1533. ObjectsSymbol: PObjectSymbol;
  1534. procedure InsertSymbolCollection(Symbols: PSymbolCollection);
  1535. var I: sw_integer;
  1536. P: PSymbol;
  1537. begin
  1538. for I:=0 to Symbols^.Count-1 do
  1539. begin
  1540. P:=Symbols^.At(I);
  1541. if (P^.Flags and sfObject)<>0 then
  1542. C^.Insert(P);
  1543. if (P^.typ=typesym) then
  1544. D^.Insert(P);
  1545. if (P^.typ=varsym) and ((P^.flags and sfPointer)<>0) then
  1546. E^.Insert(P);
  1547. if P^.Items<>nil then
  1548. InsertSymbolCollection(P^.Items);
  1549. end;
  1550. end;
  1551. function SearchObjectForSym(O: PSymbol): PObjectSymbol;
  1552. var I: sw_integer;
  1553. OS,P: PObjectSymbol;
  1554. begin
  1555. P:=nil;
  1556. for I:=0 to ObjectC^.Count-1 do
  1557. begin
  1558. OS:=ObjectC^.At(I);
  1559. if OS^.Symbol=O then
  1560. begin P:=OS; Break; end;
  1561. end;
  1562. SearchObjectForSym:=P;
  1563. end;
  1564. procedure BuildTree;
  1565. var I: sw_integer;
  1566. Symbol: PSymbol;
  1567. Parent,OS: PObjectSymbol;
  1568. begin
  1569. I:=0;
  1570. while (I<C^.Count) do
  1571. begin
  1572. Symbol:=C^.At(I);
  1573. if Symbol^.Ancestor=nil then
  1574. Parent:=ObjectsSymbol
  1575. else
  1576. Parent:=SearchObjectForSym(Symbol^.Ancestor);
  1577. if Parent<>nil then
  1578. begin
  1579. New(OS, Init(Parent, Symbol));
  1580. Parent^.AddDescendant(OS);
  1581. ObjectC^.Insert(OS);
  1582. C^.AtDelete(I);
  1583. end
  1584. else
  1585. Inc(I);
  1586. end;
  1587. end;
  1588. var Pass: integer;
  1589. I: sw_integer;
  1590. P: PSymbol;
  1591. begin
  1592. New(C, Init(1000,5000));
  1593. New(D, Init(1000,5000));
  1594. New(E, Init(1000,5000));
  1595. InsertSymbolCollection(Modules);
  1596. { --- Resolve ancestor<->descendant references --- }
  1597. for I:=0 to C^.Count-1 do
  1598. begin
  1599. P:=C^.At(I);
  1600. if P^.RelatedTypeID<>0 then
  1601. P^.Ancestor:=C^.SearchSymbolByID(P^.RelatedTypeID);
  1602. end;
  1603. { --- Resolve pointer definition references --- }
  1604. for I:=0 to D^.Count-1 do
  1605. begin
  1606. P:=D^.At(I);
  1607. if P^.RelatedTypeID<>0 then
  1608. P^.Ancestor:=D^.SearchSymbolByID(P^.RelatedTypeID);
  1609. end;
  1610. { --- Resolve pointer var definition references --- }
  1611. for I:=0 to E^.Count-1 do
  1612. begin
  1613. P:=PSymbol(E^.At(I));
  1614. if P^.RelatedTypeID<>0 then
  1615. P^.Ancestor:=D^.SearchSymbolByID(P^.RelatedTypeID);
  1616. end;
  1617. { E is not needed anymore }
  1618. E^.DeleteAll;
  1619. Dispose(E,Done);
  1620. { D is not needed anymore }
  1621. D^.DeleteAll;
  1622. Dispose(D,Done);
  1623. { --- Build object tree --- }
  1624. if assigned(ObjectTree) then
  1625. Dispose(ObjectTree, Done);
  1626. New(ObjectsSymbol, InitName('Objects'));
  1627. ObjectTree:=ObjectsSymbol;
  1628. New(ObjectC, Init(C^.Count,100));
  1629. Pass:=0;
  1630. if C^.Count>0 then
  1631. repeat
  1632. BuildTree;
  1633. Inc(Pass);
  1634. until (C^.Count=0) or (Pass>20); { more than 20 levels ? - then there must be a bug }
  1635. ObjectC^.DeleteAll; Dispose(ObjectC, Done);
  1636. C^.DeleteAll; Dispose(C, Done);
  1637. end;
  1638. function SearchObjectForSymbol(O: PSymbol): PObjectSymbol;
  1639. function ScanObjectCollection(Parent: PObjectSymbol): PObjectSymbol;
  1640. var I: sw_integer;
  1641. OS,P: PObjectSymbol;
  1642. ObjectC: PObjectSymbolCollection;
  1643. begin
  1644. P:=nil;
  1645. if Parent<>nil then
  1646. if Parent^.Descendants<>nil then
  1647. begin
  1648. ObjectC:=Parent^.Descendants;
  1649. for I:=0 to ObjectC^.Count-1 do
  1650. begin
  1651. OS:=ObjectC^.At(I);
  1652. if OS^.Symbol=O then
  1653. begin P:=OS; Break; end;
  1654. if OS^.Descendants<>nil then
  1655. begin
  1656. P:=ScanObjectCollection(OS);
  1657. if P<>nil then Break;
  1658. end;
  1659. end;
  1660. end;
  1661. ScanObjectCollection:=P;
  1662. end;
  1663. begin
  1664. SearchObjectForSymbol:=ScanObjectCollection(ObjectTree);
  1665. end;
  1666. procedure BuildSourceList;
  1667. var m: tmodule;
  1668. s: tinputfile;
  1669. p: cutils.pstring;
  1670. ppu,obj: string;
  1671. source: string;
  1672. begin
  1673. if Assigned(SourceFiles) then
  1674. begin
  1675. Dispose(SourceFiles, Done);
  1676. SourceFiles:=nil;
  1677. end;
  1678. if assigned(loaded_units.first) then
  1679. begin
  1680. New(SourceFiles, Init(50,10));
  1681. m:=tmodule(loaded_units.first);
  1682. while assigned(m) do
  1683. begin
  1684. obj:=fexpand(m.objfilename^);
  1685. ppu:=''; source:='';
  1686. if m.is_unit then
  1687. ppu:=fexpand(m.ppufilename^);
  1688. if (m.is_unit=false) and (m.islibrary=false) then
  1689. ppu:=fexpand(m.exefilename^);
  1690. if assigned(m.sourcefiles) then
  1691. begin
  1692. s:=m.sourcefiles.files;
  1693. while assigned(s) do
  1694. begin
  1695. source:='';
  1696. p:=s.path;
  1697. if assigned(p) then
  1698. source:=source+p^;
  1699. p:=s.name;
  1700. if assigned(p) then
  1701. source:=source+p^;
  1702. source:=fexpand(source);
  1703. sourcefiles^.Insert(New(PSourceFile, Init(source,obj,ppu)));
  1704. s:=s.ref_next;
  1705. end;
  1706. end;
  1707. m:=tmodule(m.next);
  1708. end;
  1709. end;
  1710. end;
  1711. {*****************************************************************************
  1712. Initialize
  1713. *****************************************************************************}
  1714. var
  1715. oldexit : pointer;
  1716. procedure browcol_exit;{$ifndef FPC}far;{$endif}
  1717. begin
  1718. exitproc:=oldexit;
  1719. DisposeBrowserCol;
  1720. if Assigned(SourceFiles) then
  1721. begin
  1722. Dispose(SourceFiles, Done);
  1723. SourceFiles:=nil;
  1724. end;
  1725. if assigned(ObjectTree) then
  1726. begin
  1727. Dispose(ObjectTree, Done);
  1728. ObjectTree:=nil;
  1729. end;
  1730. end;
  1731. procedure InitBrowserCol;
  1732. begin
  1733. end;
  1734. procedure DoneBrowserCol;
  1735. begin
  1736. { nothing, the collections are freed in the exitproc - ??? }
  1737. { nothing? then why do we've this routine for ? IMHO, either we should
  1738. remove this, or it should destroy the browser info when it's called. - BG }
  1739. end;
  1740. type
  1741. PPointerXRef = ^TPointerXRef;
  1742. TPointerXRef = record
  1743. PtrValue : pointer;
  1744. DataPtr : pointer;
  1745. end;
  1746. PPointerDictionary = ^TPointerDictionary;
  1747. TPointerDictionary = object(TSortedCollection)
  1748. function At(Index: sw_Integer): PPointerXRef;
  1749. function Compare(Key1, Key2: Pointer): sw_Integer; virtual;
  1750. procedure FreeItem(Item: Pointer); virtual;
  1751. function SearchXRef(PtrValue: pointer): PPointerXRef;
  1752. function AddPtr(PtrValue, DataPtr: pointer): PPointerXRef;
  1753. procedure Resolve(var P);
  1754. end;
  1755. function NewPointerXRef(APtrValue, ADataPtr: pointer): PPointerXRef;
  1756. var P: PPointerXRef;
  1757. begin
  1758. New(P); FillChar(P^,SizeOf(P^),0);
  1759. with P^ do begin PtrValue:=APtrValue; DataPtr:=ADataPtr; end;
  1760. NewPointerXRef:=P;
  1761. end;
  1762. procedure DisposePointerXRef(P: PPointerXRef);
  1763. begin
  1764. if Assigned(P) then Dispose(P);
  1765. end;
  1766. function TPointerDictionary.At(Index: sw_Integer): PPointerXRef;
  1767. begin
  1768. At:=inherited At(Index);
  1769. end;
  1770. function TPointerDictionary.Compare(Key1, Key2: Pointer): sw_Integer;
  1771. var K1: PPointerXRef absolute Key1;
  1772. K2: PPointerXRef absolute Key2;
  1773. R: integer;
  1774. begin
  1775. if Ptrint(K1^.PtrValue)<Ptrint(K2^.PtrValue) then R:=-1 else
  1776. if Ptrint(K1^.PtrValue)>Ptrint(K2^.PtrValue) then R:= 1 else
  1777. R:=0;
  1778. Compare:=R;
  1779. end;
  1780. procedure TPointerDictionary.FreeItem(Item: Pointer);
  1781. begin
  1782. if Assigned(Item) then DisposePointerXRef(Item);
  1783. end;
  1784. function TPointerDictionary.SearchXRef(PtrValue: pointer): PPointerXRef;
  1785. var P: PPointerXRef;
  1786. T: TPointerXRef;
  1787. Index: sw_integer;
  1788. begin
  1789. T.PtrValue:=PtrValue;
  1790. if Search(@T,Index)=false then P:=nil else
  1791. P:=At(Index);
  1792. SearchXRef:=P;
  1793. end;
  1794. function TPointerDictionary.AddPtr(PtrValue, DataPtr: pointer): PPointerXRef;
  1795. var P: PPointerXRef;
  1796. begin
  1797. P:=SearchXRef(PtrValue);
  1798. if P=nil then
  1799. begin
  1800. P:=NewPointerXRef(PtrValue,DataPtr);
  1801. Insert(P);
  1802. {$ifdef DEBUG}
  1803. end
  1804. else
  1805. begin
  1806. if P^.DataPtr<>DataPtr then
  1807. InternalError(987654);
  1808. {$endif DEBUG}
  1809. end;
  1810. AddPtr:=P;
  1811. end;
  1812. procedure TPointerDictionary.Resolve(var P);
  1813. var X: PPointerXRef;
  1814. V: pointer;
  1815. begin
  1816. Move(P,V,SizeOf(V));
  1817. X:=SearchXRef(V);
  1818. if X=nil then V:=nil else
  1819. V:=X^.DataPtr;
  1820. Move(V,P,SizeOf(V));
  1821. end;
  1822. procedure ReadPointers(S: PStream; C: PCollection; D: PPointerDictionary);
  1823. var W,I: sw_integer;
  1824. P: pointer;
  1825. begin
  1826. S^.Read(W,SizeOf(W));
  1827. for I:=0 to W-1 do
  1828. begin
  1829. S^.Read(P,SizeOf(P));
  1830. D^.AddPtr(P,C^.At(I));
  1831. end;
  1832. end;
  1833. function LoadBrowserCol(S: PStream): boolean;
  1834. var PD: PPointerDictionary;
  1835. procedure FixupPointers;
  1836. procedure FixupReference(P: PReference); {$ifndef FPC}far;{$endif}
  1837. begin
  1838. PD^.Resolve(P^.FileName);
  1839. end;
  1840. procedure FixupSymbol(P: PSymbol); {$ifndef FPC}far;{$endif}
  1841. var I: sw_integer;
  1842. begin
  1843. PD^.Resolve(P^.DType);
  1844. PD^.Resolve(P^.VType);
  1845. PD^.Resolve(P^.Params);
  1846. if Assigned(P^.References) then
  1847. with P^.References^ do
  1848. for I:=0 to Count-1 do
  1849. FixupReference(At(I));
  1850. if Assigned(P^.Items) then
  1851. with P^.Items^ do
  1852. for I:=0 to Count-1 do
  1853. FixupSymbol(At(I));
  1854. end;
  1855. begin
  1856. Modules^.ForEach(@FixupSymbol);
  1857. end;
  1858. procedure ReadSymbolPointers(P: PSymbol); {$ifndef FPC}far;{$endif}
  1859. var I: sw_integer;
  1860. PV: pointer;
  1861. begin
  1862. S^.Read(PV, SizeOf(PV));
  1863. PD^.AddPtr(PV,P);
  1864. if Assigned(P^.Items) then
  1865. with P^.Items^ do
  1866. for I:=0 to Count-1 do
  1867. ReadSymbolPointers(At(I));
  1868. end;
  1869. begin
  1870. DisposeBrowserCol;
  1871. New(ModuleNames, Load(S^));
  1872. New(TypeNames, Load(S^));
  1873. New(Modules, Load(S^));
  1874. New(PD, Init(4000,1000));
  1875. ReadPointers(S,ModuleNames,PD);
  1876. ReadPointers(S,TypeNames,PD);
  1877. ReadPointers(S,Modules,PD);
  1878. Modules^.ForEach(@ReadSymbolPointers);
  1879. FixupPointers;
  1880. Dispose(PD, Done);
  1881. BuildObjectInfo;
  1882. LoadBrowserCol:=(S^.Status=stOK);
  1883. end;
  1884. procedure StorePointers(S: PStream; C: PCollection);
  1885. var W,I: sw_integer;
  1886. P: pointer;
  1887. begin
  1888. W:=C^.Count;
  1889. S^.Write(W,SizeOf(W));
  1890. for I:=0 to W-1 do
  1891. begin
  1892. P:=C^.At(I);
  1893. S^.Write(P,SizeOf(P));
  1894. end;
  1895. end;
  1896. function StoreBrowserCol(S: PStream) : boolean;
  1897. procedure WriteSymbolPointers(P: PSymbol); {$ifndef FPC}far;{$endif}
  1898. var I: sw_integer;
  1899. begin
  1900. S^.Write(P, SizeOf(P));
  1901. if Assigned(P^.Items) then
  1902. with P^.Items^ do
  1903. for I:=0 to Count-1 do
  1904. WriteSymbolPointers(At(I));
  1905. end;
  1906. begin
  1907. ModuleNames^.Store(S^);
  1908. TypeNames^.Store(S^);
  1909. Modules^.Store(S^);
  1910. StorePointers(S,ModuleNames);
  1911. StorePointers(S,TypeNames);
  1912. StorePointers(S,Modules);
  1913. Modules^.ForEach(@WriteSymbolPointers);
  1914. StoreBrowserCol:=(S^.Status=stOK);
  1915. end;
  1916. procedure RegisterSymbols;
  1917. begin
  1918. RegisterType(RModuleNameCollection);
  1919. RegisterType(RTypeNameCollection);
  1920. RegisterType(RReference);
  1921. RegisterType(RSymbol);
  1922. RegisterType(RObjectSymbol);
  1923. RegisterType(RSymbolCollection);
  1924. RegisterType(RSortedSymbolCollection);
  1925. RegisterType(RIDSortedSymbolCollection);
  1926. RegisterType(RObjectSymbolCollection);
  1927. RegisterType(RReferenceCollection);
  1928. RegisterType(RModuleSymbol);
  1929. end;
  1930. begin
  1931. oldexit:=exitproc;
  1932. exitproc:=@browcol_exit;
  1933. end.
  1934. {
  1935. $Log$
  1936. Revision 1.39 2004-06-20 08:55:28 florian
  1937. * logs truncated
  1938. Revision 1.38 2004/06/16 20:07:07 florian
  1939. * dwarf branch merged
  1940. Revision 1.37.2.1 2004/05/03 21:08:56 peter
  1941. * use ptrint
  1942. Revision 1.37 2004/03/23 22:34:49 peter
  1943. * constants ordinals now always have a type assigned
  1944. * integer constants have the smallest type, unsigned prefered over
  1945. signed
  1946. }