2
0

browcol.pas 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515
  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. const MaxBrowseLevelDepth = 32;
  1177. type
  1178. { For circular reference test in ProcessSymTable }
  1179. { Generics tend to reference themselves }
  1180. PLinkList = ^TLinkList;
  1181. TLinkList = record
  1182. Depth: Longword;
  1183. Prev: PLinkList;
  1184. Sym: TSymTable;
  1185. end;
  1186. procedure ProcessSymTable(OwnerSym: PSymbol; var Owner: PSymbolCollection; Table: TSymTable; Pll : PLinkList);
  1187. var SymPll : PLinkList;
  1188. procedure SetVType(Symbol: PSymbol; VType: string);
  1189. begin
  1190. Symbol^.VType:=TypeNames^.Add(VType);
  1191. end;
  1192. procedure SetDType(Symbol: PSymbol; DType: string);
  1193. begin
  1194. Symbol^.DType:=TypeNames^.Add(DType);
  1195. end;
  1196. function GetDefinitionStr(def: tdef): string; forward;
  1197. function GetEnumDefStr(def: tenumdef): string;
  1198. var
  1199. Name: string;
  1200. esym: tenumsym;
  1201. i: integer;
  1202. begin
  1203. Name:='(';
  1204. for i := 0 to def.symtable.SymList.Count - 1 do
  1205. begin
  1206. if i>0 then
  1207. Name:=Name+', ';
  1208. Name:=Name+tenumsym(def.symtable.SymList[i]).RealName;
  1209. end;
  1210. Name:=Name+')';
  1211. GetEnumDefStr:=Name;
  1212. end;
  1213. function GetArrayDefStr(def: tarraydef): string;
  1214. var Name: string;
  1215. begin
  1216. Name:='array ['+IntToStr(def.lowrange)+'..'+IntToStr(def.highrange)+'] of ';
  1217. if assigned(def.elementdef) then
  1218. Name:=Name+GetDefinitionStr(def.elementdef);
  1219. GetArrayDefStr:=Name;
  1220. end;
  1221. function GetFileDefStr(def: tfiledef): string;
  1222. var Name: string;
  1223. begin
  1224. Name:='';
  1225. case def.filetyp of
  1226. ft_text : Name:='text';
  1227. ft_untyped : Name:='file';
  1228. ft_typed : Name:='file of '+GetDefinitionStr(def.typedfiledef);
  1229. end;
  1230. GetFileDefStr:=Name;
  1231. end;
  1232. function GetOrdDefStr(def: torddef): string;
  1233. begin
  1234. GetOrdDefStr:=def.GetTypeName;
  1235. end;
  1236. function GetStringDefStr(def: tstringdef): string;
  1237. var Name: string;
  1238. begin
  1239. Name:='';
  1240. case def.stringtype of
  1241. st_shortstring :
  1242. if def.len=255 then
  1243. Name:='shortstring'
  1244. else
  1245. Name:='string['+IntToStr(def.len)+']';
  1246. st_longstring :
  1247. Name:='longstring';
  1248. st_ansistring :
  1249. Name:='ansistring';
  1250. st_widestring :
  1251. Name:='widestring';
  1252. st_unicodestring :
  1253. Name:='unicodestring';
  1254. else ;
  1255. end;
  1256. GetStringDefStr:=Name;
  1257. end;
  1258. function retdefassigned(def: tabstractprocdef): boolean;
  1259. var OK: boolean;
  1260. begin
  1261. OK:=false;
  1262. if assigned(def.returndef) then
  1263. if UpcaseStr(GetDefinitionStr(def.returndef))<>'$VOID' then
  1264. OK:=true;
  1265. retdefassigned:=OK;
  1266. end;
  1267. function GetAbsProcParmDefStr(def: tabstractprocdef): string;
  1268. var Name: string;
  1269. dc: tabstractvarsym;
  1270. i,
  1271. Count: integer;
  1272. CurName: string;
  1273. begin
  1274. Name:='';
  1275. Count:=0;
  1276. for i:=0 to def.paras.count-1 do
  1277. begin
  1278. dc:=tabstractvarsym(def.paras[i]);
  1279. if not (vo_is_hidden_para in dc.VarOptions) then
  1280. begin
  1281. CurName:='';
  1282. if assigned(dc.vardef) then
  1283. CurName:=': '+GetDefinitionStr(dc.vardef);
  1284. CurName:=dc.RealName+CurName;
  1285. case dc.varspez of
  1286. vs_Value : ;
  1287. vs_Const : CurName:='const '+CurName;
  1288. vs_Var : CurName:='var '+CurName;
  1289. vs_Out : CurName:='out '+CurName;
  1290. vs_Constref : CurName:='constref '+CurName;
  1291. end;
  1292. if Count>0 then
  1293. CurName:='; '+CurName;
  1294. Name:=Name+CurName;
  1295. Inc(Count);
  1296. end;
  1297. end;
  1298. GetAbsProcParmDefStr:=Name;
  1299. end;
  1300. function GetAbsProcDefStr(def: tabstractprocdef): string;
  1301. var Name: string;
  1302. begin
  1303. Name:=GetAbsProcParmDefStr(def);
  1304. if Name<>'' then Name:='('+Name+')';
  1305. if retdefassigned(def) then
  1306. Name:='function'+Name+': '+GetDefinitionStr(def.returndef)+';'
  1307. else
  1308. Name:='procedure'+Name+';';
  1309. GetAbsProcDefStr:=Name;
  1310. end;
  1311. function GetProcDefStr(def: tprocdef): string;
  1312. var DName: string;
  1313. {J: integer;}
  1314. begin
  1315. { DName:='';
  1316. if assigned(def) then
  1317. begin
  1318. if assigned(def.parast) then
  1319. begin
  1320. with def.parast^ do
  1321. for J:=1 to number_symbols do
  1322. begin
  1323. if J<>1 then DName:=DName+', ';
  1324. ParSym:=GetsymNr(J);
  1325. if ParSym=nil then Break;
  1326. DName:=DName+ParSym^.Name;
  1327. end;
  1328. end
  1329. end;}
  1330. DName:=GetAbsProcDefStr(def);
  1331. GetProcDefStr:=DName;
  1332. end;
  1333. function GetProcVarDefStr(def: tprocvardef): string;
  1334. begin
  1335. GetProcVarDefStr:=GetAbsProcDefStr(def);
  1336. end;
  1337. function GetSetDefStr(def: tsetdef): string;
  1338. var Name: string;
  1339. begin
  1340. Name:='set of '+GetDefinitionStr(def.elementdef);
  1341. GetSetDefStr:=Name;
  1342. end;
  1343. function GetPointerDefStr(def: tpointerdef): string;
  1344. begin
  1345. GetPointerDefStr:='^'+GetDefinitionStr(def.pointeddef);
  1346. end;
  1347. function GetDefinitionStr(def: tdef): string;
  1348. var Name: string;
  1349. begin
  1350. Name:='';
  1351. if def<>nil then
  1352. begin
  1353. if assigned(def.typesym) then
  1354. Name:=def.typesym.RealName;
  1355. if Name='' then
  1356. case def.typ of
  1357. arraydef :
  1358. Name:=GetArrayDefStr(tarraydef(def));
  1359. stringdef :
  1360. Name:=GetStringDefStr(tstringdef(def));
  1361. enumdef :
  1362. Name:=GetEnumDefStr(tenumdef(def));
  1363. procdef :
  1364. Name:=GetProcDefStr(tprocdef(def));
  1365. procvardef :
  1366. Name:=GetProcVarDefStr(tprocvardef(def));
  1367. filedef :
  1368. Name:=GetFileDefStr(tfiledef(def));
  1369. setdef :
  1370. Name:=GetSetDefStr(tsetdef(def));
  1371. end;
  1372. end;
  1373. GetDefinitionStr:=Name;
  1374. end;
  1375. function GetEnumItemName(Sym: tenumsym): string;
  1376. var Name: string;
  1377. {ES: tenumsym;}
  1378. begin
  1379. Name:='';
  1380. if assigned(sym) and assigned(sym.definition) then
  1381. if assigned(sym.definition.typesym) then
  1382. begin
  1383. { ES:=sym.definition.First;
  1384. while (ES<>nil) and (ES^.Value<>sym.value) do
  1385. ES:=ES^.next;
  1386. if assigned(es) and (es^.value=sym.value) then
  1387. Name:=}
  1388. Name:=sym.definition.typesym.RealName;
  1389. if Name<>'' then
  1390. Name:=Name+'('+IntToStr(sym.value)+')';
  1391. end else Name:=IntToStr(sym.value);
  1392. GetEnumItemName:=Name;
  1393. end;
  1394. function GetConstValueName(sym: tconstsym): string;
  1395. var Name: string;
  1396. begin
  1397. Name:='';
  1398. if Name='' then
  1399. case sym.consttyp of
  1400. constord :
  1401. begin
  1402. if sym.constdef.typ=enumdef then
  1403. Name:=sym.constdef.typesym.RealName+'('+tostr(sym.value.valueord)+')'
  1404. else
  1405. if is_boolean(sym.constdef) then
  1406. Name:='Longbool('+tostr(sym.value.valueord)+')'
  1407. else
  1408. if is_char(sym.constdef) or
  1409. is_widechar(sym.constdef) then
  1410. Name:=''''+chr(sym.value.valueord.svalue)+''''
  1411. else
  1412. Name:=tostr(sym.value.valueord);
  1413. end;
  1414. constresourcestring,
  1415. conststring :
  1416. Name:=''''+StrPas(pchar(sym.value.valueptr))+'''';
  1417. constreal:
  1418. Name:=FloatToStr(PBestReal(sym.value.valueptr)^);
  1419. constset:
  1420. Name:=SetToStr(pnormalset(sym.value.valueptr));
  1421. constnil: ;
  1422. end;
  1423. GetConstValueName:=Name;
  1424. end;
  1425. procedure ProcessDefIfStruct(Symbol: PSymbol; definition: tdef);
  1426. begin
  1427. { still led to infinite recursions
  1428. only useful for unnamed types PM }
  1429. if assigned(definition) and not assigned(definition.typesym) then
  1430. begin
  1431. case definition.typ of
  1432. recorddef :
  1433. if trecorddef(definition).symtable<>Table then
  1434. ProcessSymTable(Symbol,Symbol^.Items,trecorddef(definition).symtable,SymPll);
  1435. objectdef :
  1436. if tobjectdef(definition).symtable<>Table then
  1437. ProcessSymTable(Symbol,Symbol^.Items,tobjectdef(definition).symtable,SymPll);
  1438. { leads to infinite loops !!
  1439. pointerdef :
  1440. with tpointerdef(definition)^ do
  1441. if assigned(definition) then
  1442. if assigned(definition.sym) then
  1443. ProcessDefIfStruct(definition.sym.definition);}
  1444. end;
  1445. end;
  1446. end;
  1447. function GetPropVarDef (Table : TSymTable):string;
  1448. var symidx : longint;
  1449. Sym: TSym;
  1450. st : string;
  1451. begin
  1452. st:='';
  1453. for symidx:=0 to Table.SymList.Count-1 do
  1454. begin
  1455. sym:=tsym(Table.SymList[symidx]);
  1456. case Sym.Typ of
  1457. paravarsym :
  1458. with tabstractvarsym(sym) do
  1459. begin
  1460. if st<>'' then st:=';';
  1461. st:=st+Sym.RealName;
  1462. if assigned(vardef) then
  1463. if assigned(vardef.typesym) then
  1464. st:=st+': '+vardef.typesym.RealName
  1465. else
  1466. st:=st+': '+GetDefinitionStr(vardef);
  1467. end;
  1468. end;
  1469. end;
  1470. GetPropVarDef:=st;
  1471. end;
  1472. function ProcessAccessList(OwnerSym: PSymbol; var Owner: PSymbolCollection; List:tpropaccesslist):string; forward;
  1473. procedure ProcessSym(OwnerSym: PSymbol; var Owner: PSymbolCollection; Sym: TSym);
  1474. var MemInfo: TSymbolMemInfo;
  1475. ObjDef: tobjectdef;
  1476. I: longint;
  1477. pd : TProcDef;
  1478. Reference: PReference;
  1479. inputfile : Tinputfile;
  1480. Ref : TRefItem;
  1481. DefPos : TFilePosInfo;
  1482. Symbol:PSymbol;
  1483. begin
  1484. if Sym.Typ <> unitsym then
  1485. New(Symbol, Init(Sym.RealName,Sym.Typ,'',nil))
  1486. else
  1487. begin
  1488. Symbol:=SearchModule(Sym.Name);
  1489. if not assigned(Symbol) then
  1490. begin
  1491. { fpintres unit go here for some reason }
  1492. Symbol:=New(PModuleSymbol,Init(Sym.Name,''));
  1493. Modules^.Insert(Symbol);
  1494. end;
  1495. end;
  1496. case Sym.Typ of
  1497. staticvarsym,
  1498. localvarsym,
  1499. absolutevarsym,
  1500. paravarsym :
  1501. begin
  1502. Symbol^.SetVarOptions(tabstractvarsym(sym).VarOptions);
  1503. Symbol^.SetVarSpez(tabstractvarsym(sym).VarSpez);
  1504. end;
  1505. end;
  1506. case Sym.Typ of
  1507. staticvarsym,
  1508. localvarsym,
  1509. absolutevarsym,
  1510. paravarsym :
  1511. with tabstractvarsym(sym) do
  1512. begin
  1513. MemInfo.PushSize:=-1;
  1514. if (sym.typ = absolutevarsym) and (tabsolutevarsym(Sym).abstyp = toVar) then
  1515. begin
  1516. if assigned(vardef) then
  1517. if assigned(vardef.typesym) then
  1518. SetVType(Symbol,vardef.typesym.RealName+' absolute '+ProcessAccessList(Symbol,Symbol^.Items,tabsolutevarsym(Sym).ref))
  1519. else
  1520. SetVType(Symbol,GetDefinitionStr(vardef)+' absolute '+ProcessAccessList(Symbol,Symbol^.Items,tabsolutevarsym(Sym).ref));
  1521. end else
  1522. if assigned(vardef) then
  1523. if assigned(vardef.typesym) then
  1524. SetVType(Symbol,vardef.typesym.RealName)
  1525. else
  1526. SetVType(Symbol,GetDefinitionStr(vardef));
  1527. ProcessDefIfStruct(Symbol,vardef);
  1528. if assigned(vardef) then
  1529. if (vardef.typ=pointerdef) and
  1530. assigned(tpointerdef(vardef).pointeddef) then
  1531. begin
  1532. Symbol^.Flags:=(Symbol^.Flags or sfPointer);
  1533. Symbol^.RelatedTypeID:=Ptrint(tpointerdef(vardef).pointeddef);
  1534. end;
  1535. if tabstractnormalvarsym(sym).localloc.loc=LOC_REFERENCE then
  1536. MemInfo.Addr:=tabstractnormalvarsym(sym).localloc.reference.offset
  1537. else
  1538. MemInfo.Addr:=0;
  1539. if assigned(vardef) and (vardef.typ=arraydef) then
  1540. begin
  1541. if tarraydef(vardef).highrange<tarraydef(vardef).lowrange then
  1542. MemInfo.Size:=-1
  1543. else
  1544. MemInfo.Size:=getsize;
  1545. end
  1546. else
  1547. MemInfo.Size:=getsize;
  1548. { this is not completely correct... }
  1549. if assigned(vardef) then
  1550. MemInfo.PushSize:=paramanager.push_size(varspez,vardef,pocall_default)
  1551. else
  1552. begin
  1553. { This can happen, why? }
  1554. MemInfo.PushSize:=0;
  1555. end;
  1556. Symbol^.SetMemInfo(MemInfo);
  1557. end;
  1558. fieldvarsym :
  1559. with tfieldvarsym(sym) do
  1560. begin
  1561. MemInfo.PushSize:=-1;
  1562. if assigned(vardef) then
  1563. if assigned(vardef.typesym) then
  1564. SetVType(Symbol,vardef.typesym.RealName)
  1565. else
  1566. SetVType(Symbol,GetDefinitionStr(vardef));
  1567. if assigned(vardef) and (vardef.typ=arraydef) then
  1568. begin
  1569. if tarraydef(vardef).highrange<tarraydef(vardef).lowrange then
  1570. MemInfo.Size:=-1
  1571. else
  1572. MemInfo.Size:=getsize;
  1573. end
  1574. else
  1575. MemInfo.Size:=getsize;
  1576. Symbol^.SetMemInfo(MemInfo);
  1577. end;
  1578. propertysym :
  1579. with tpropertysym(sym) do
  1580. begin
  1581. if assigned(parast) then
  1582. Symbol^.Params:=TypeNames^.Add(GetPropVarDef(parast));
  1583. if assigned(propdef) then
  1584. if assigned(propdef.typesym) then
  1585. SetVType(Symbol,propdef.typesym.RealName)
  1586. else
  1587. SetVType(Symbol,GetDefinitionStr(propdef));
  1588. ProcessAccessList(Symbol,Symbol^.Items,propaccesslist[palt_read]);
  1589. ProcessAccessList(Symbol,Symbol^.Items,propaccesslist[palt_write]);
  1590. end;
  1591. constsym :
  1592. SetDType(Symbol,GetConstValueName(tconstsym(sym)));
  1593. enumsym :
  1594. if assigned(tenumsym(sym).definition) then
  1595. SetDType(Symbol,GetEnumItemName(tenumsym(sym)));
  1596. unitsym :
  1597. begin
  1598. { ProcessSymTable(Symbol^.Items,tunitsym(sym).unitsymtable);}
  1599. end;
  1600. syssym :
  1601. { if assigned(Table.Name) then
  1602. if Table.Name^='SYSTEM' then}
  1603. begin
  1604. Symbol^.Params:=TypeNames^.Add('...');
  1605. end;
  1606. procsym :
  1607. begin
  1608. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  1609. begin
  1610. if i>0 then
  1611. begin
  1612. if Assigned(Symbol) then
  1613. Owner^.Insert(Symbol);
  1614. New(Symbol, Init(Sym.RealName,Sym.Typ,'',nil));
  1615. end;
  1616. with tprocsym(sym) do
  1617. begin
  1618. pd:=tprocdef(procdeflist[i]);
  1619. if assigned(pd) then
  1620. begin
  1621. ProcessSymTable(Symbol,Symbol^.Items,pd.parast,SymPll);
  1622. if retdefassigned(tabstractprocdef(pd)) then
  1623. begin
  1624. SetVType(Symbol,GetDefinitionStr(tabstractprocdef(pd).returndef));
  1625. end;
  1626. if assigned(pd.parast) then
  1627. begin
  1628. Symbol^.Params:=TypeNames^.Add(
  1629. GetAbsProcParmDefStr(pd));
  1630. end
  1631. else { param-definition is NOT assigned }
  1632. if assigned(Table.Name) and
  1633. (Table.Name^='SYSTEM') then
  1634. begin
  1635. Symbol^.Params:=TypeNames^.Add('...');
  1636. end;
  1637. if cs_local_browser in current_settings.moduleswitches then
  1638. begin
  1639. if assigned(pd.localst) and
  1640. (pd.localst.symtabletype<>staticsymtable) then
  1641. ProcessSymTable(Symbol,Symbol^.Items,pd.localst,SymPll);
  1642. end;
  1643. end;
  1644. end;
  1645. end;
  1646. end;
  1647. typesym :
  1648. begin
  1649. with ttypesym(sym) do
  1650. if assigned(typedef) then
  1651. begin
  1652. Symbol^.TypeID:=Ptrint(typedef);
  1653. case typedef.typ of
  1654. floatdef:
  1655. SetDType(Symbol,tfloatdef(typedef).GetTypeName);
  1656. classrefdef:
  1657. SetDType(Symbol,tclassrefdef(typedef).GetTypeName);
  1658. orddef :
  1659. SetDType(Symbol,GetOrdDefStr(torddef(typedef)));
  1660. stringdef:
  1661. SetDType(Symbol,GetStringDefStr(tstringdef(typedef)));
  1662. arraydef :
  1663. SetDType(Symbol,GetArrayDefStr(tarraydef(typedef)));
  1664. enumdef :
  1665. begin
  1666. SetDType(Symbol,GetEnumDefStr(tenumdef(typedef)));
  1667. ProcessSymTable(Symbol,Symbol^.Items,tenumdef(typedef).symtable,SymPll);
  1668. end;
  1669. procdef :
  1670. SetDType(Symbol,GetProcDefStr(tprocdef(typedef)));
  1671. procvardef :
  1672. SetDType(Symbol,GetProcVarDefStr(tprocvardef(typedef)));
  1673. objectdef :
  1674. with tobjectdef(typedef) do
  1675. begin
  1676. ObjDef:=childof;
  1677. if ObjDef<>nil then
  1678. Symbol^.RelatedTypeID:=Ptrint(ObjDef);{TypeNames^.Add(S);}
  1679. Symbol^.Flags:=(Symbol^.Flags or sfObject);
  1680. if tobjectdef(typedef).objecttype=odt_class then
  1681. Symbol^.Flags:=(Symbol^.Flags or sfClass);
  1682. if (trecorddef(typedef).symtable<>Table) then
  1683. if not(df_generic in typedef.defoptions) then
  1684. ProcessSymTable(Symbol,Symbol^.Items,tobjectdef(typedef).symtable,SymPll);
  1685. end;
  1686. recorddef :
  1687. begin
  1688. Symbol^.Flags:=(Symbol^.Flags or sfRecord);
  1689. if (trecorddef(typedef).symtable<>Table) then
  1690. ProcessSymTable(Symbol,Symbol^.Items,trecorddef(typedef).symtable,SymPll);
  1691. end;
  1692. pointerdef :
  1693. begin
  1694. Symbol^.Flags:=(Symbol^.Flags or sfPointer);
  1695. Symbol^.RelatedTypeID:=Ptrint(tpointerdef(typedef).pointeddef);{TypeNames^.Add(S);}
  1696. SetDType(Symbol,GetPointerDefStr(tpointerdef(typedef)));
  1697. end;
  1698. filedef :
  1699. SetDType(Symbol,GetFileDefStr(tfiledef(typedef)));
  1700. setdef :
  1701. begin
  1702. SetDType(Symbol,GetSetDefStr(tsetdef(typedef)));
  1703. if assigned(tsetdef(typedef).elementdef) then
  1704. if tsetdef(typedef).elementdef.typ=enumdef then
  1705. if assigned(tenumdef(tsetdef(typedef).elementdef).symtable) then
  1706. ProcessSymTable(Symbol,Symbol^.Items,tenumdef(tsetdef(typedef).elementdef).symtable,SymPll);
  1707. end;
  1708. end;
  1709. end;
  1710. end;
  1711. end;
  1712. if assigned(sym) then
  1713. begin
  1714. DefPos:=tstoredsym(sym).FileInfo;
  1715. inputfile:=get_source_file(defpos.moduleindex,defpos.fileindex);
  1716. if Assigned(inputfile) and (inputfile.name<>'') then
  1717. begin
  1718. New(Reference, Init(ModuleNames^.Add(inputfile.name),
  1719. DefPos.line,DefPos.column));
  1720. Symbol^.References^.Insert(Reference);
  1721. end;
  1722. end;
  1723. if assigned(Symbol) and assigned(sym.RefList) then
  1724. begin
  1725. Ref:=TRefItem(tstoredsym(sym).RefList.First);
  1726. while assigned(Ref) do
  1727. begin
  1728. inputfile:=get_source_file(ref.refinfo.moduleindex,ref.refinfo.fileindex);
  1729. if Assigned(inputfile) and (inputfile.name<>'') then
  1730. begin
  1731. New(Reference, Init(ModuleNames^.Add(inputfile.name),
  1732. ref.refinfo.line,ref.refinfo.column));
  1733. Symbol^.References^.Insert(Reference);
  1734. end;
  1735. Ref:=TRefItem(Ref.next);
  1736. end;
  1737. end;
  1738. if Assigned(Symbol) then
  1739. begin
  1740. if Assigned(sym) and (sym.typ=unitsym) then exit; { Units are in Modules list already }
  1741. (* if not Owner^.Search(Symbol,J) then *)
  1742. Owner^.Insert(Symbol)
  1743. (*else
  1744. begin
  1745. Dispose(Symbol,done);
  1746. Symbol:=nil;
  1747. end;*)
  1748. end;
  1749. end;
  1750. function ProcessAccessList(OwnerSym: PSymbol; var Owner: PSymbolCollection; List:tpropaccesslist):string;
  1751. var Item : ppropaccesslistitem;
  1752. sym : tsym;
  1753. begin
  1754. ProcessAccessList:='';
  1755. with List do
  1756. begin
  1757. Item := firstsym;
  1758. while assigned(Item) do
  1759. begin
  1760. Sym:=Item^.Sym;
  1761. ProcessAccessList:=Sym.RealName;
  1762. if Owner=nil then
  1763. Owner:=New(PSortedSymbolCollection, Init(10,50));
  1764. ProcessSym(OwnerSym,Owner,Sym);
  1765. if Item = lastsym then break;
  1766. Item:=Item^.next;
  1767. end;
  1768. end;
  1769. end;
  1770. {------- ProcessSymTable main body ---- }
  1771. var symidx : longint;
  1772. Sym: TSym;
  1773. SymTll: TLinkList;
  1774. begin
  1775. if not Assigned(Table) then
  1776. Exit;
  1777. if Pll^.Depth = MaxBrowseLevelDepth then
  1778. Exit; { can not browse any deeper, some browser information will be missing}
  1779. { check for generic symbol referencing to themselves }
  1780. SymPll:=Pll;
  1781. symidx:=0;
  1782. while assigned(SymPll^.Prev) do
  1783. begin
  1784. if SymPll^.Sym=Table then
  1785. exit; { circular reference, exit to avoid infinite recursion }
  1786. SymPll:=SymPll^.Prev;
  1787. inc(symidx);
  1788. end;
  1789. SymPll:=@SymTll;
  1790. SymTll.Depth:=Pll^.Depth+1;
  1791. SymTll.Prev:=Pll;
  1792. SymTll.Sym:=Table;
  1793. { Scan throught all symbols }
  1794. if Owner=nil then
  1795. Owner:=New(PSortedSymbolCollection, Init(10,50));
  1796. for symidx:=0 to Table.SymList.Count-1 do
  1797. begin
  1798. sym:=tsym(Table.SymList[symidx]);
  1799. ProcessSym(OwnerSym,Owner,Sym);
  1800. end;
  1801. end;
  1802. procedure CreateBrowserCol;
  1803. var
  1804. path,module,
  1805. name,msource : string;
  1806. SymTll: TLinkList;
  1807. T: TSymTable;
  1808. UnitS,PM: PModuleSymbol;
  1809. hp : tmodule;
  1810. puu: tused_unit;
  1811. pdu: tdependent_unit;
  1812. pif: tinputfile;
  1813. ifile: sizeint;
  1814. begin
  1815. DisposeBrowserCol;
  1816. if (cs_browser in current_settings.moduleswitches) then
  1817. NewBrowserCol;
  1818. {-- first only create Modules list --}
  1819. hp:=tmodule(loaded_units.first);
  1820. if (cs_browser in current_settings.moduleswitches) then
  1821. while assigned(hp) do
  1822. begin
  1823. if hp.is_unit then
  1824. t:=tsymtable(hp.globalsymtable)
  1825. else
  1826. t:=tsymtable(hp.localsymtable);
  1827. if assigned(t) then
  1828. begin
  1829. name:=GetStr(T.Name);
  1830. msource:=hp.mainsource;
  1831. New(UnitS, Init(Name,msource));
  1832. { // A unit can be loaded from many other places, so a single loaded_from is misleading.
  1833. if Assigned(hp.loaded_from) then
  1834. if assigned(hp.loaded_from.globalsymtable) then
  1835. UnitS^.SetLoadedFrom(tsymtable(hp.loaded_from.globalsymtable).name^);
  1836. }
  1837. { pimportlist(current_module^.imports^.first);}
  1838. if assigned(hp.sourcefiles) then
  1839. for ifile:=hp.sourcefiles.nfiles-1 downto 0 do
  1840. begin
  1841. pif:=hp.sourcefiles.files[ifile];
  1842. path:=pif.path;
  1843. name:=pif.name;
  1844. UnitS^.AddSourceFile(path+name);
  1845. end;
  1846. Modules^.Insert(UnitS);
  1847. end;
  1848. hp:=tmodule(hp.next);
  1849. end;
  1850. {-- collect symbol information --}
  1851. FillChar(SymTll,SizeOf(TLinkList),0);
  1852. hp:=tmodule(loaded_units.first);
  1853. if (cs_browser in current_settings.moduleswitches) then
  1854. while assigned(hp) do
  1855. begin
  1856. if hp.is_unit then
  1857. t:=tsymtable(hp.globalsymtable)
  1858. else
  1859. t:=tsymtable(hp.localsymtable);
  1860. if assigned(t) then
  1861. begin
  1862. name:=GetStr(T.Name);
  1863. UnitS:=SearchModule(Name);
  1864. { all modules have to be in list before first call to ProcessSymTable }
  1865. ProcessSymTable(UnitS,UnitS^.Items,T,@SymTll);
  1866. if hp.is_unit then
  1867. if cs_local_browser in current_settings.moduleswitches then
  1868. begin
  1869. t:=tsymtable(hp.localsymtable);
  1870. if assigned(t) then
  1871. ProcessSymTable(UnitS,UnitS^.Items,T,@SymTll);
  1872. end;
  1873. end;
  1874. hp:=tmodule(hp.next);
  1875. end;
  1876. {-- used modules and dependant modules --}
  1877. hp:=tmodule(loaded_units.first);
  1878. if (cs_browser in current_settings.moduleswitches) then
  1879. while assigned(hp) do
  1880. begin
  1881. if hp.is_unit then
  1882. t:=tsymtable(hp.globalsymtable)
  1883. else
  1884. t:=tsymtable(hp.localsymtable);
  1885. if assigned(t) then
  1886. begin
  1887. name:=GetStr(T.Name);
  1888. UnitS:=SearchModule(Name);
  1889. puu:=tused_unit(hp.used_units.first);
  1890. while (puu<>nil) do
  1891. begin
  1892. module:=GetStr(puu.u.modulename);
  1893. PM:=SearchModule(module);
  1894. if Assigned(PM) then
  1895. UnitS^.AddUsedUnit(PM);
  1896. puu:=tused_unit(puu.next);
  1897. end;
  1898. pdu:=tdependent_unit(hp.dependent_units.first);
  1899. while (pdu<>nil) do
  1900. begin
  1901. name:=GetStr(tsymtable(pdu.u.globalsymtable).name);
  1902. PM:=SearchModule(Name);
  1903. if Assigned(PM) then
  1904. UnitS^.AddDependentUnit(PM);
  1905. pdu:=tdependent_unit(pdu.next);
  1906. end;
  1907. end;
  1908. hp:=tmodule(hp.next);
  1909. end;
  1910. if (cs_browser in current_settings.moduleswitches) then
  1911. BuildObjectInfo;
  1912. { can always be done
  1913. needed to know when recompilation of sources is necessary }
  1914. BuildSourceList;
  1915. end;
  1916. procedure BuildObjectInfo;
  1917. var C,D: PIDSortedSymbolCollection;
  1918. E : PCollection;
  1919. ObjectC: PObjectSymbolCollection;
  1920. ObjectsSymbol: PObjectSymbol;
  1921. procedure InsertSymbolCollection(Symbols: PSymbolCollection);
  1922. var I: sw_integer;
  1923. P: PSymbol;
  1924. begin
  1925. for I:=0 to Symbols^.Count-1 do
  1926. begin
  1927. P:=Symbols^.At(I);
  1928. if (P^.Flags and (sfObject or sfClass))<>0 then
  1929. C^.Insert(P);
  1930. if (P^.typ=typesym) then
  1931. D^.Insert(P);
  1932. if (P^.typ in [staticvarsym,localvarsym,paravarsym]) and ((P^.flags and sfPointer)<>0) then
  1933. E^.Insert(P);
  1934. if P^.Items<>nil then
  1935. InsertSymbolCollection(P^.Items);
  1936. end;
  1937. end;
  1938. function SearchObjectForSym(O: PSymbol): PObjectSymbol;
  1939. var I: sw_integer;
  1940. OS,P: PObjectSymbol;
  1941. begin
  1942. P:=nil;
  1943. for I:=0 to ObjectC^.Count-1 do
  1944. begin
  1945. OS:=ObjectC^.At(I);
  1946. if OS^.Symbol=O then
  1947. begin P:=OS; Break; end;
  1948. end;
  1949. SearchObjectForSym:=P;
  1950. end;
  1951. procedure BuildTree;
  1952. var I: sw_integer;
  1953. Symbol: PSymbol;
  1954. Parent,OS: PObjectSymbol;
  1955. begin
  1956. I:=0;
  1957. while (I<C^.Count) do
  1958. begin
  1959. Symbol:=C^.At(I);
  1960. if Symbol^.Ancestor=nil then
  1961. Parent:=ObjectsSymbol
  1962. else
  1963. Parent:=SearchObjectForSym(Symbol^.Ancestor);
  1964. if Parent<>nil then
  1965. begin
  1966. New(OS, Init(Parent, Symbol));
  1967. Parent^.AddDescendant(OS);
  1968. ObjectC^.Insert(OS);
  1969. C^.AtDelete(I);
  1970. end
  1971. else
  1972. Inc(I);
  1973. end;
  1974. end;
  1975. var Pass: integer;
  1976. I: sw_integer;
  1977. P: PSymbol;
  1978. begin
  1979. New(C, Init(1000,5000));
  1980. New(D, Init(1000,5000));
  1981. New(E, Init(1000,5000));
  1982. InsertSymbolCollection(Modules);
  1983. { --- Resolve ancestor<->descendant references --- }
  1984. for I:=0 to C^.Count-1 do
  1985. begin
  1986. P:=C^.At(I);
  1987. if P^.RelatedTypeID<>0 then
  1988. P^.Ancestor:=C^.SearchSymbolByID(P^.RelatedTypeID);
  1989. end;
  1990. { --- Resolve pointer definition references --- }
  1991. for I:=0 to D^.Count-1 do
  1992. begin
  1993. P:=D^.At(I);
  1994. if P^.RelatedTypeID<>0 then
  1995. P^.Ancestor:=D^.SearchSymbolByID(P^.RelatedTypeID);
  1996. end;
  1997. { --- Resolve pointer var definition references --- }
  1998. for I:=0 to E^.Count-1 do
  1999. begin
  2000. P:=PSymbol(E^.At(I));
  2001. if P^.RelatedTypeID<>0 then
  2002. P^.Ancestor:=D^.SearchSymbolByID(P^.RelatedTypeID);
  2003. end;
  2004. { E is not needed anymore }
  2005. E^.DeleteAll;
  2006. Dispose(E,Done);
  2007. { D is not needed anymore }
  2008. D^.DeleteAll;
  2009. Dispose(D,Done);
  2010. { --- Build object tree --- }
  2011. if assigned(ObjectTree) then
  2012. Dispose(ObjectTree, Done);
  2013. New(ObjectsSymbol, InitName('Objects'));
  2014. ObjectTree:=ObjectsSymbol;
  2015. New(ObjectC, Init(C^.Count,100));
  2016. Pass:=0;
  2017. if C^.Count>0 then
  2018. repeat
  2019. BuildTree;
  2020. Inc(Pass);
  2021. until (C^.Count=0) or (Pass>20); { more than 20 levels ? - then there must be a bug }
  2022. ObjectC^.DeleteAll; Dispose(ObjectC, Done);
  2023. C^.DeleteAll; Dispose(C, Done);
  2024. end;
  2025. function SearchObjectForSymbol(O: PSymbol): PObjectSymbol;
  2026. function ScanObjectCollection(Parent: PObjectSymbol): PObjectSymbol;
  2027. var I: sw_integer;
  2028. OS,P: PObjectSymbol;
  2029. ObjectC: PObjectSymbolCollection;
  2030. begin
  2031. P:=nil;
  2032. if Parent<>nil then
  2033. if Parent^.Descendants<>nil then
  2034. begin
  2035. ObjectC:=Parent^.Descendants;
  2036. for I:=0 to ObjectC^.Count-1 do
  2037. begin
  2038. OS:=ObjectC^.At(I);
  2039. if OS^.Symbol=O then
  2040. begin P:=OS; Break; end;
  2041. if OS^.Descendants<>nil then
  2042. begin
  2043. P:=ScanObjectCollection(OS);
  2044. if P<>nil then Break;
  2045. end;
  2046. end;
  2047. end;
  2048. ScanObjectCollection:=P;
  2049. end;
  2050. begin
  2051. SearchObjectForSymbol:=ScanObjectCollection(ObjectTree);
  2052. end;
  2053. procedure BuildSourceList;
  2054. var m: tmodule;
  2055. s: tinputfile;
  2056. ppu,obj: string;
  2057. source: string;
  2058. ifile: sizeint;
  2059. begin
  2060. if Assigned(SourceFiles) then
  2061. begin
  2062. Dispose(SourceFiles, Done);
  2063. SourceFiles:=nil;
  2064. end;
  2065. if assigned(loaded_units.first) then
  2066. begin
  2067. New(SourceFiles, Init(50,10));
  2068. m:=tmodule(loaded_units.first);
  2069. while assigned(m) do
  2070. begin
  2071. obj:=ExpandFileName(m.objfilename);
  2072. ppu:=''; source:='';
  2073. if m.is_unit then
  2074. ppu:=ExpandFileName(m.ppufilename);
  2075. if (m.is_unit=false) and (m.islibrary=false) then
  2076. ppu:=ExpandFileName(m.exefilename);
  2077. if assigned(m.sourcefiles) then
  2078. for ifile:=m.sourcefiles.nfiles-1 downto 0 do
  2079. begin
  2080. s:=m.sourcefiles.files[ifile];
  2081. source:=s.path+s.name;
  2082. source:=ExpandFileName(source);
  2083. sourcefiles^.Insert(New(PSourceFile, Init(source,obj,ppu)));
  2084. end;
  2085. m:=tmodule(m.next);
  2086. end;
  2087. end;
  2088. end;
  2089. {*****************************************************************************
  2090. Initialize
  2091. *****************************************************************************}
  2092. var
  2093. oldexit : pointer;
  2094. procedure browcol_exit;
  2095. begin
  2096. exitproc:=oldexit;
  2097. DisposeBrowserCol;
  2098. if Assigned(SourceFiles) then
  2099. begin
  2100. Dispose(SourceFiles, Done);
  2101. SourceFiles:=nil;
  2102. end;
  2103. if assigned(ObjectTree) then
  2104. begin
  2105. Dispose(ObjectTree, Done);
  2106. ObjectTree:=nil;
  2107. end;
  2108. end;
  2109. procedure InitBrowserCol;
  2110. begin
  2111. end;
  2112. procedure DoneBrowserCol;
  2113. begin
  2114. { nothing, the collections are freed in the exitproc - ??? }
  2115. { nothing? then why do we've this routine for ? IMHO, either we should
  2116. remove this, or it should destroy the browser info when it's called. - BG }
  2117. end;
  2118. type
  2119. PPointerXRef = ^TPointerXRef;
  2120. TPointerXRef = record
  2121. PtrValue : pointer;
  2122. DataPtr : pointer;
  2123. end;
  2124. PPointerDictionary = ^TPointerDictionary;
  2125. TPointerDictionary = object(TSortedCollection)
  2126. function At(Index: sw_Integer): PPointerXRef;
  2127. function Compare(Key1, Key2: Pointer): sw_Integer; virtual;
  2128. procedure FreeItem(Item: Pointer); virtual;
  2129. function SearchXRef(PtrValue: pointer): PPointerXRef;
  2130. function AddPtr(PtrValue, DataPtr: pointer): PPointerXRef;
  2131. procedure Resolve(var P);
  2132. end;
  2133. function NewPointerXRef(APtrValue, ADataPtr: pointer): PPointerXRef;
  2134. var P: PPointerXRef;
  2135. begin
  2136. New(P); FillChar(P^,SizeOf(P^),0);
  2137. with P^ do begin PtrValue:=APtrValue; DataPtr:=ADataPtr; end;
  2138. NewPointerXRef:=P;
  2139. end;
  2140. procedure DisposePointerXRef(P: PPointerXRef);
  2141. begin
  2142. if Assigned(P) then Dispose(P);
  2143. end;
  2144. function TPointerDictionary.At(Index: sw_Integer): PPointerXRef;
  2145. begin
  2146. At:=inherited At(Index);
  2147. end;
  2148. function TPointerDictionary.Compare(Key1, Key2: Pointer): sw_Integer;
  2149. var K1: PPointerXRef absolute Key1;
  2150. K2: PPointerXRef absolute Key2;
  2151. R: integer;
  2152. begin
  2153. if PtrUInt(K1^.PtrValue)<PtrUInt(K2^.PtrValue) then R:=-1 else
  2154. if PtrUInt(K1^.PtrValue)>PtrUInt(K2^.PtrValue) then R:= 1 else
  2155. R:=0;
  2156. Compare:=R;
  2157. end;
  2158. procedure TPointerDictionary.FreeItem(Item: Pointer);
  2159. begin
  2160. if Assigned(Item) then DisposePointerXRef(Item);
  2161. end;
  2162. function TPointerDictionary.SearchXRef(PtrValue: pointer): PPointerXRef;
  2163. var P: PPointerXRef;
  2164. T: TPointerXRef;
  2165. Index: sw_integer;
  2166. begin
  2167. T.PtrValue:=PtrValue;
  2168. if Search(@T,Index)=false then P:=nil else
  2169. P:=At(Index);
  2170. SearchXRef:=P;
  2171. end;
  2172. function TPointerDictionary.AddPtr(PtrValue, DataPtr: pointer): PPointerXRef;
  2173. var P: PPointerXRef;
  2174. begin
  2175. P:=SearchXRef(PtrValue);
  2176. if P=nil then
  2177. begin
  2178. P:=NewPointerXRef(PtrValue,DataPtr);
  2179. Insert(P);
  2180. {$ifdef DEBUG}
  2181. end
  2182. else
  2183. begin
  2184. if P^.DataPtr<>DataPtr then
  2185. InternalError(987654);
  2186. {$endif DEBUG}
  2187. end;
  2188. AddPtr:=P;
  2189. end;
  2190. procedure TPointerDictionary.Resolve(var P);
  2191. var X: PPointerXRef;
  2192. V: pointer;
  2193. begin
  2194. Move(P,V,SizeOf(V));
  2195. X:=SearchXRef(V);
  2196. if X=nil then V:=nil else
  2197. V:=X^.DataPtr;
  2198. Move(V,P,SizeOf(V));
  2199. end;
  2200. procedure ReadPointers(S: PStream; C: PCollection; D: PPointerDictionary);
  2201. var W,I: sw_integer;
  2202. P: pointer;
  2203. begin
  2204. S^.Read(W,SizeOf(W));
  2205. for I:=0 to W-1 do
  2206. begin
  2207. S^.Read(P,SizeOf(P));
  2208. D^.AddPtr(P,C^.At(I));
  2209. end;
  2210. end;
  2211. function LoadBrowserCol(S: PStream): boolean;
  2212. var PD: PPointerDictionary;
  2213. procedure FixupPointers;
  2214. procedure FixupReference(P: PReference);
  2215. begin
  2216. PD^.Resolve(P^.FileName);
  2217. end;
  2218. procedure FixupSymbol(P: PSymbol);
  2219. var I: sw_integer;
  2220. begin
  2221. PD^.Resolve(P^.DType);
  2222. PD^.Resolve(P^.VType);
  2223. PD^.Resolve(P^.Params);
  2224. if Assigned(P^.References) then
  2225. with P^.References^ do
  2226. for I:=0 to Count-1 do
  2227. FixupReference(At(I));
  2228. if Assigned(P^.Items) then
  2229. with P^.Items^ do
  2230. for I:=0 to Count-1 do
  2231. FixupSymbol(At(I));
  2232. end;
  2233. begin
  2234. Modules^.ForEach(TCallbackProcParam(@FixupSymbol));
  2235. end;
  2236. procedure ReadSymbolPointers(P: PSymbol);
  2237. var I: sw_integer;
  2238. PV: pointer;
  2239. begin
  2240. S^.Read(PV, SizeOf(PV));
  2241. PD^.AddPtr(PV,P);
  2242. if Assigned(P^.Items) then
  2243. with P^.Items^ do
  2244. for I:=0 to Count-1 do
  2245. ReadSymbolPointers(At(I));
  2246. end;
  2247. begin
  2248. DisposeBrowserCol;
  2249. New(ModuleNames, Load(S^));
  2250. New(TypeNames, Load(S^));
  2251. New(Modules, Load(S^));
  2252. New(PD, Init(4000,1000));
  2253. ReadPointers(S,ModuleNames,PD);
  2254. ReadPointers(S,TypeNames,PD);
  2255. ReadPointers(S,Modules,PD);
  2256. Modules^.ForEach(TCallbackProcParam(@ReadSymbolPointers));
  2257. FixupPointers;
  2258. Dispose(PD, Done);
  2259. BuildObjectInfo;
  2260. LoadBrowserCol:=(S^.Status=stOK);
  2261. end;
  2262. procedure StorePointers(S: PStream; C: PCollection);
  2263. var W,I: sw_integer;
  2264. P: pointer;
  2265. begin
  2266. W:=C^.Count;
  2267. S^.Write(W,SizeOf(W));
  2268. for I:=0 to W-1 do
  2269. begin
  2270. P:=C^.At(I);
  2271. S^.Write(P,SizeOf(P));
  2272. end;
  2273. end;
  2274. function StoreBrowserCol(S: PStream) : boolean;
  2275. procedure WriteSymbolPointers(P: PSymbol);
  2276. var I: sw_integer;
  2277. begin
  2278. S^.Write(P, SizeOf(P));
  2279. if Assigned(P^.Items) then
  2280. with P^.Items^ do
  2281. for I:=0 to Count-1 do
  2282. WriteSymbolPointers(At(I));
  2283. end;
  2284. begin
  2285. ModuleNames^.Store(S^);
  2286. TypeNames^.Store(S^);
  2287. Modules^.Store(S^);
  2288. StorePointers(S,ModuleNames);
  2289. StorePointers(S,TypeNames);
  2290. StorePointers(S,Modules);
  2291. Modules^.ForEach(TCallbackProcParam(@WriteSymbolPointers));
  2292. StoreBrowserCol:=(S^.Status=stOK);
  2293. end;
  2294. procedure RegisterSymbols;
  2295. begin
  2296. RegisterType(RModuleNameCollection);
  2297. RegisterType(RTypeNameCollection);
  2298. RegisterType(RReference);
  2299. RegisterType(RSymbol);
  2300. RegisterType(RObjectSymbol);
  2301. RegisterType(RSymbolCollection);
  2302. RegisterType(RSortedSymbolCollection);
  2303. RegisterType(RIDSortedSymbolCollection);
  2304. RegisterType(RObjectSymbolCollection);
  2305. RegisterType(RReferenceCollection);
  2306. RegisterType(RModuleSymbol);
  2307. end;
  2308. begin
  2309. oldexit:=exitproc;
  2310. exitproc:=@browcol_exit;
  2311. end.