browcol.pas 66 KB

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