ppuout.pp 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  1. {
  2. Copyright (c) 2013 by Yury Sidorov and the FPC Development Team
  3. Base classes for a custom output of a PPU File
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************}
  16. unit ppuout;
  17. {$mode objfpc}{$H+}
  18. {$I+}
  19. interface
  20. uses SysUtils, cclasses, Classes;
  21. type
  22. TPpuDefType = (dtNone, dtUnit, dtObject, dtRecord, dtProc, dtField, dtProp, dtParam, dtVar,
  23. dtTypeRef, dtConst, dtProcType, dtEnum, dtSet, dtClassRef, dtArray, dtPointer,
  24. dtOrd);
  25. TPpuDef = class;
  26. TPpuContainerDef = class;
  27. TPpuUnitDef = class;
  28. { TPpuOutput }
  29. TPpuOutput = class
  30. private
  31. FOutFile: ^Text;
  32. FIndent: integer;
  33. FIndentSize: integer;
  34. FIndStr: string;
  35. FNoIndent: boolean;
  36. procedure SetIndent(AValue: integer);
  37. procedure SetIndentSize(AValue: integer);
  38. protected
  39. procedure WriteObjectStart(const AName: string; Def: TPpuDef = nil); virtual;
  40. procedure WriteObjectEnd(Def: TPpuDef = nil); virtual;
  41. procedure WriteArrayStart(const AName: string); virtual;
  42. procedure WriteArrayEnd; virtual;
  43. procedure WriteStr(const AName, AValue: string); virtual;
  44. procedure WriteInt(const AName: string; AValue: Int64; Signed: boolean = True); virtual;
  45. procedure WriteFloat(const AName: string; AValue: extended); virtual;
  46. procedure WriteBool(const AName: string; AValue: boolean); virtual;
  47. procedure WriteNull(const AName: string); virtual;
  48. public
  49. constructor Create(var OutFile: Text); virtual;
  50. destructor Destroy; override;
  51. procedure Write(const s: string);
  52. procedure WriteLn(const s: string = '');
  53. procedure IncI; virtual;
  54. procedure DecI; virtual;
  55. property Indent: integer read FIndent write SetIndent;
  56. property IndentSize: integer read FIndentSize write SetIndentSize;
  57. end;
  58. { TPpuRef }
  59. TPpuRef = class
  60. private
  61. FId: cardinal;
  62. function GetId: cardinal;
  63. function GetIsSymId: boolean;
  64. procedure SetId(AValue: cardinal);
  65. procedure SetIsSymId(AValue: boolean);
  66. public
  67. UnitIndex: word;
  68. constructor Create;
  69. procedure Write(Output: TPpuOutput; const RefName: string);
  70. property Id: cardinal read GetId write SetId;
  71. property IsSymId: boolean read GetIsSymId write SetIsSymId;
  72. function IsCurUnit: boolean; inline;
  73. function IsNull: boolean; inline;
  74. end;
  75. TPpuFilePos = record
  76. FileIndex: dword;
  77. Line, Col: integer;
  78. end;
  79. TPpuDefVisibility = (dvPublic, dvPublished, dvProtected, dvPrivate, dvHidden);
  80. { TPpuDef }
  81. TPpuDef = class
  82. private
  83. FId: cardinal;
  84. FParent: TPpuContainerDef;
  85. FParentUnit: TPpuUnitDef;
  86. function GetDefTypeName: string;
  87. function GetId: cardinal;
  88. function GetParentUnit: TPpuUnitDef;
  89. procedure SetId(AValue: cardinal);
  90. procedure SetParent(AValue: TPpuContainerDef);
  91. protected
  92. procedure WriteDef(Output: TPpuOutput); virtual;
  93. public
  94. DefType: TPpuDefType;
  95. Name: string;
  96. FilePos: TPpuFilePos;
  97. // Symbol/definition reference
  98. Ref: TPpuRef;
  99. Visibility: TPpuDefVisibility;
  100. constructor Create(AParent: TPpuContainerDef); virtual; reintroduce;
  101. destructor Destroy; override;
  102. procedure Write(Output: TPpuOutput; const AttrName: string = '');
  103. function CanWrite: boolean; virtual;
  104. procedure SetSymId(AId: integer);
  105. property Parent: TPpuContainerDef read FParent write SetParent;
  106. property ParentUnit: TPpuUnitDef read GetParentUnit;
  107. property Id: cardinal read GetId write SetId;
  108. property DefTypeName: string read GetDefTypeName;
  109. end;
  110. { TPpuContainerDef }
  111. TPpuContainerDef = class(TPpuDef)
  112. private
  113. FItems: TList;
  114. function GetCount: integer;
  115. function GetItem(Index: Integer): TPpuDef;
  116. procedure SetItem(Index: Integer; AValue: TPpuDef);
  117. protected
  118. procedure WriteDef(Output: TPpuOutput); override;
  119. procedure BeforeWriteItems(Output: TPpuOutput); virtual;
  120. public
  121. ItemsName: string;
  122. constructor Create(AParent: TPpuContainerDef); override;
  123. destructor Destroy; override;
  124. function Add(Def: TPpuDef): integer;
  125. property Items[Index: Integer]: TPpuDef read GetItem write SetItem; default;
  126. property Count: integer read GetCount;
  127. end;
  128. { TPpuTypeRef }
  129. TPpuTypeRef = class(TPpuDef)
  130. protected
  131. procedure WriteDef(Output: TPpuOutput); override;
  132. public
  133. constructor Create(AParent: TPpuContainerDef); override;
  134. end;
  135. { TPpuUnitDef }
  136. TPpuUnitDef = class(TPpuContainerDef)
  137. private
  138. FIndexById: THashSet;
  139. protected
  140. procedure WriteDef(Output: TPpuOutput); override;
  141. public
  142. Version: cardinal;
  143. Crc, IntfCrc: cardinal;
  144. TargetOS, TargetCPU: string;
  145. UsedUnits: TPpuContainerDef;
  146. RefUnits: array of string;
  147. SourceFiles: TPpuContainerDef;
  148. constructor Create(AParent: TPpuContainerDef); override;
  149. destructor Destroy; override;
  150. function FindById(AId: integer; FindSym: boolean = False): TPpuDef;
  151. end;
  152. { TPpuSrcFile }
  153. TPpuSrcFile = class(TPpuDef)
  154. protected
  155. procedure WriteDef(Output: TPpuOutput); override;
  156. public
  157. FileTime: TDateTime;
  158. end;
  159. TPpuProcOption = (poProcedure, poFunction, poConstructor, poDestructor, poOperator,
  160. poClassMethod, poVirtual, poAbstract, poOverriding, poOverload, poInline);
  161. TPpuProcOptions = set of TPpuProcOption;
  162. { TPpuProcDef }
  163. TPpuProcDef = class(TPpuContainerDef)
  164. protected
  165. procedure BeforeWriteItems(Output: TPpuOutput); override;
  166. public
  167. ReturnType: TPpuRef;
  168. Options: TPpuProcOptions;
  169. constructor Create(AParent: TPpuContainerDef); override;
  170. destructor Destroy; override;
  171. end;
  172. { TPpuProcTypeDef }
  173. TPpuProcTypeDef = class(TPpuProcDef)
  174. public
  175. constructor Create(AParent: TPpuContainerDef); override;
  176. end;
  177. TPpuConstType = (ctUnknown, ctInt, ctFloat, ctStr, ctSet, ctPtr);
  178. { TPpuConstDef }
  179. TPpuConstDef = class(TPpuDef)
  180. protected
  181. procedure WriteDef(Output: TPpuOutput); override;
  182. public
  183. ConstType: TPpuConstType;
  184. TypeRef: TPpuRef;
  185. VInt: Int64;
  186. VFloat: extended;
  187. VStr: string;
  188. VSet: array[0..31] of byte;
  189. constructor Create(AParent: TPpuContainerDef); override;
  190. destructor Destroy; override;
  191. function CanWrite: boolean; override;
  192. end;
  193. { TPpuVarDef }
  194. TPpuVarDef = class(TPpuDef)
  195. protected
  196. procedure WriteDef(Output: TPpuOutput); override;
  197. public
  198. VarType: TPpuRef;
  199. constructor Create(AParent: TPpuContainerDef); override;
  200. destructor Destroy; override;
  201. end;
  202. TPpuParamSpez = (psValue, psVar, psOut, psConst, psConstRef, psHidden);
  203. { TPpuParamDef }
  204. TPpuParamDef = class(TPpuVarDef)
  205. protected
  206. procedure WriteDef(Output: TPpuOutput); override;
  207. public
  208. Spez: TPpuParamSpez;
  209. DefaultValue: TPpuRef;
  210. constructor Create(AParent: TPpuContainerDef); override;
  211. destructor Destroy; override;
  212. function CanWrite: boolean; override;
  213. end;
  214. TPpuObjType = (otUnknown, otClass, otObject, otInterface, otHelper);
  215. TPpuObjOption = (ooIsAbstract, ooCopied);
  216. TPpuObjOptions = set of TPpuObjOption;
  217. { TPpuObjectDef }
  218. TPpuObjectDef = class(TPpuContainerDef)
  219. protected
  220. procedure BeforeWriteItems(Output: TPpuOutput); override;
  221. public
  222. ObjType: TPpuObjType;
  223. Ancestor: TPpuRef;
  224. Options: TPpuObjOptions;
  225. constructor Create(AParent: TPpuContainerDef); override;
  226. destructor Destroy; override;
  227. function CanWrite: boolean; override;
  228. end;
  229. { TPpuFieldDef }
  230. TPpuFieldDef = class(TPpuVarDef)
  231. public
  232. constructor Create(AParent: TPpuContainerDef); override;
  233. end;
  234. { TPpuPropDef }
  235. TPpuPropDef = class(TPpuContainerDef)
  236. protected
  237. procedure BeforeWriteItems(Output: TPpuOutput); override;
  238. public
  239. PropType: TPpuRef;
  240. Getter, Setter: TPpuRef;
  241. constructor Create(AParent: TPpuContainerDef); override;
  242. destructor Destroy; override;
  243. end;
  244. { TPpuRecordDef }
  245. TPpuRecordDef = class(TPpuObjectDef)
  246. protected
  247. procedure BeforeWriteItems(Output: TPpuOutput); override;
  248. public
  249. constructor Create(AParent: TPpuContainerDef); override;
  250. function CanWrite: boolean; override;
  251. end;
  252. { TPpuClassRefDef }
  253. TPpuClassRefDef = class(TPpuDef)
  254. protected
  255. procedure WriteDef(Output: TPpuOutput); override;
  256. public
  257. ClassRef: TPpuRef;
  258. constructor Create(AParent: TPpuContainerDef); override;
  259. destructor Destroy; override;
  260. end;
  261. TPpuArrayOption = (aoDynamic);
  262. TPpuArrayOptions = set of TPpuArrayOption;
  263. { TPpuArrayDef }
  264. TPpuArrayDef = class(TPpuDef)
  265. protected
  266. procedure WriteDef(Output: TPpuOutput); override;
  267. public
  268. ElType: TPpuRef;
  269. RangeType: TPpuRef;
  270. RangeLow, RangeHigh: Int64;
  271. Options: TPpuArrayOptions;
  272. constructor Create(AParent: TPpuContainerDef); override;
  273. destructor Destroy; override;
  274. function CanWrite: boolean; override;
  275. end;
  276. { TPpuEnumDef }
  277. TPpuEnumDef = class(TPpuContainerDef)
  278. protected
  279. procedure BeforeWriteItems(Output: TPpuOutput); override;
  280. public
  281. ElLow, ElHigh: integer;
  282. Size: byte;
  283. CopyFrom: TPpuRef;
  284. constructor Create(AParent: TPpuContainerDef); override;
  285. destructor Destroy; override;
  286. end;
  287. { TPpuSetDef }
  288. TPpuSetDef = class(TPpuDef)
  289. protected
  290. procedure WriteDef(Output: TPpuOutput); override;
  291. public
  292. ElType: TPpuRef;
  293. SetBase, SetMax: integer;
  294. Size: byte;
  295. constructor Create(AParent: TPpuContainerDef); override;
  296. destructor Destroy; override;
  297. end;
  298. { TPpuPointerDef }
  299. TPpuPointerDef = class(TPpuDef)
  300. protected
  301. procedure WriteDef(Output: TPpuOutput); override;
  302. public
  303. Ptr: TPpuRef;
  304. constructor Create(AParent: TPpuContainerDef); override;
  305. destructor Destroy; override;
  306. end;
  307. TPpuOrdType = (otVoid, otUInt, otSInt, otPasBool, otBool, otChar, otCurrency);
  308. { TPpuOrdDef }
  309. TPpuOrdDef = class(TPpuDef)
  310. protected
  311. procedure WriteDef(Output: TPpuOutput); override;
  312. public
  313. OrdType: TPpuOrdType;
  314. Size: byte;
  315. RangeLow, RangeHigh: Int64;
  316. constructor Create(AParent: TPpuContainerDef); override;
  317. end;
  318. implementation
  319. const
  320. DefTypeNames: array[TPpuDefType] of string =
  321. ('', 'unit', 'obj', 'rec', 'proc', 'field', 'prop', 'param', 'var',
  322. 'type', 'const', 'proctype', 'enum', 'set', 'classref', 'array', 'ptr', 'ord');
  323. ProcOptionNames: array[TPpuProcOption] of string =
  324. ('procedure', 'function', 'constructor', 'destructor', 'operator',
  325. 'classmethod', 'virtual', 'abstract', 'overriding', 'overload', 'inline');
  326. DefVisibilityNames: array[TPpuDefVisibility] of string =
  327. ('public', 'published', 'protected', 'private', '');
  328. ParamSpezNames: array[TPpuParamSpez] of string =
  329. ('value', 'var', 'out', 'const', 'constref', '');
  330. ObjTypeNames: array[TPpuObjType] of string =
  331. ('', 'class', 'object', 'interface', 'helper');
  332. ObjOptionNames: array[TPpuObjOption] of string =
  333. ('abstract','copied');
  334. ArrayOptionNames: array[TPpuArrayOption] of string =
  335. ('dynamic');
  336. ConstTypeNames: array[TPpuConstType] of string =
  337. ('', 'int', 'float', 'string', 'set', 'pointer');
  338. OrdTypeNames: array[TPpuOrdType] of string =
  339. ('void', 'uint', 'sint', 'pasbool', 'bool', 'char', 'currency');
  340. SymIdBit = $80000000;
  341. InvalidId = cardinal(-1);
  342. InvalidUnit = word(-1);
  343. function IsSymId(Id: cardinal): boolean; inline;
  344. begin
  345. Result:=Id and SymIdBit <> 0;
  346. end;
  347. { TPpuOrdDef }
  348. procedure TPpuOrdDef.WriteDef(Output: TPpuOutput);
  349. var
  350. Signed: boolean;
  351. begin
  352. inherited WriteDef(Output);
  353. with Output do begin
  354. WriteStr('OrdType', OrdTypeNames[OrdType]);
  355. WriteInt('Size', Size);
  356. Signed:=OrdType in [otSInt, otCurrency, otBool];
  357. WriteInt('Low', RangeLow, Signed);
  358. WriteInt('High', RangeHigh, Signed);
  359. end;
  360. end;
  361. constructor TPpuOrdDef.Create(AParent: TPpuContainerDef);
  362. begin
  363. inherited Create(AParent);
  364. DefType:=dtOrd;
  365. end;
  366. { TPpuPointerDef }
  367. procedure TPpuPointerDef.WriteDef(Output: TPpuOutput);
  368. begin
  369. inherited WriteDef(Output);
  370. Ptr.Write(Output, 'Ptr');
  371. end;
  372. constructor TPpuPointerDef.Create(AParent: TPpuContainerDef);
  373. begin
  374. inherited Create(AParent);
  375. DefType:=dtPointer;
  376. Ptr:=TPpuRef.Create;
  377. end;
  378. destructor TPpuPointerDef.Destroy;
  379. begin
  380. Ptr.Free;
  381. inherited Destroy;
  382. end;
  383. { TPpuSetDef }
  384. procedure TPpuSetDef.WriteDef(Output: TPpuOutput);
  385. begin
  386. inherited WriteDef(Output);
  387. with Output do begin
  388. WriteInt('Size', Size);
  389. WriteInt('Base', SetBase);
  390. WriteInt('Max', SetMax);
  391. end;
  392. ElType.Write(Output, 'ElType');
  393. end;
  394. constructor TPpuSetDef.Create(AParent: TPpuContainerDef);
  395. begin
  396. inherited Create(AParent);
  397. DefType:=dtSet;
  398. ElType:=TPpuRef.Create;
  399. end;
  400. destructor TPpuSetDef.Destroy;
  401. begin
  402. ElType.Free;
  403. inherited Destroy;
  404. end;
  405. { TPpuEnumDef }
  406. procedure TPpuEnumDef.BeforeWriteItems(Output: TPpuOutput);
  407. begin
  408. inherited BeforeWriteItems(Output);
  409. with Output do begin
  410. WriteInt('Low', ElLow);
  411. WriteInt('High', ElHigh);
  412. WriteInt('Size', Size);
  413. end;
  414. if not CopyFrom.IsNull then
  415. CopyFrom.Write(Output, 'CopyFrom');
  416. end;
  417. constructor TPpuEnumDef.Create(AParent: TPpuContainerDef);
  418. begin
  419. inherited Create(AParent);
  420. DefType:=dtEnum;
  421. ItemsName:='Elements';
  422. CopyFrom:=TPpuRef.Create;
  423. end;
  424. destructor TPpuEnumDef.Destroy;
  425. begin
  426. CopyFrom.Free;
  427. inherited Destroy;
  428. end;
  429. { TPpuConstDef }
  430. procedure TPpuConstDef.WriteDef(Output: TPpuOutput);
  431. var
  432. s, ss: string;
  433. i: integer;
  434. begin
  435. inherited WriteDef(Output);
  436. with Output do begin
  437. WriteStr('ValType', ConstTypeNames[ConstType]);
  438. s:='Value';
  439. case ConstType of
  440. ctInt:
  441. WriteInt(s, VInt);
  442. ctFloat:
  443. WriteFloat(s, VFloat);
  444. ctStr:
  445. WriteStr(s, VStr);
  446. ctPtr:
  447. if VInt = 0 then
  448. WriteNull(s)
  449. else
  450. if QWord(VInt) > $FFFFFFFF then
  451. WriteStr(s, hexStr(QWord(VInt), 8))
  452. else
  453. WriteStr(s, hexStr(QWord(VInt), 16));
  454. ctSet:
  455. begin
  456. ss:='';
  457. for i:=Low(VSet) to High(VSet) do
  458. ss:=ss + hexStr(VSet[i], 2);
  459. WriteStr(s, ss);
  460. end;
  461. end;
  462. end;
  463. if not TypeRef.IsNull then
  464. TypeRef.Write(Output, 'TypeRef');
  465. end;
  466. constructor TPpuConstDef.Create(AParent: TPpuContainerDef);
  467. begin
  468. inherited Create(AParent);
  469. DefType:=dtConst;
  470. TypeRef:=TPpuRef.Create;
  471. ConstType:=ctUnknown;
  472. end;
  473. destructor TPpuConstDef.Destroy;
  474. begin
  475. TypeRef.Free;
  476. inherited Destroy;
  477. end;
  478. function TPpuConstDef.CanWrite: boolean;
  479. begin
  480. Result:=inherited CanWrite and (ConstType <> ctUnknown);
  481. end;
  482. { TPpuArrayDef }
  483. procedure TPpuArrayDef.WriteDef(Output: TPpuOutput);
  484. var
  485. opt: TPpuArrayOption;
  486. begin
  487. inherited WriteDef(Output);
  488. if Options <> [] then begin
  489. Output.WriteArrayStart('Options');
  490. for opt:=Low(opt) to High(opt) do
  491. if opt in Options then
  492. Output.WriteStr('', ArrayOptionNames[opt]);
  493. Output.WriteArrayEnd;
  494. end;
  495. ElType.Write(Output, 'ElType');
  496. RangeType.Write(Output, 'RangeType');;
  497. Output.WriteInt('Low', RangeLow);
  498. Output.WriteInt('High', RangeHigh);
  499. end;
  500. constructor TPpuArrayDef.Create(AParent: TPpuContainerDef);
  501. begin
  502. inherited Create(AParent);
  503. DefType:=dtArray;
  504. ElType:=TPpuRef.Create;
  505. RangeType:=TPpuRef.Create;
  506. end;
  507. destructor TPpuArrayDef.Destroy;
  508. begin
  509. ElType.Free;
  510. RangeType.Free;
  511. inherited Destroy;
  512. end;
  513. function TPpuArrayDef.CanWrite: boolean;
  514. begin
  515. Result:=inherited CanWrite and (Name <> '');
  516. end;
  517. { TPpuClassRefDef }
  518. procedure TPpuClassRefDef.WriteDef(Output: TPpuOutput);
  519. begin
  520. inherited WriteDef(Output);
  521. ClassRef.Write(Output, 'Ref');
  522. end;
  523. constructor TPpuClassRefDef.Create(AParent: TPpuContainerDef);
  524. begin
  525. inherited Create(AParent);
  526. DefType:=dtClassRef;
  527. ClassRef:=TPpuRef.Create;
  528. end;
  529. destructor TPpuClassRefDef.Destroy;
  530. begin
  531. ClassRef.Free;
  532. inherited Destroy;
  533. end;
  534. { TPpuRecordDef }
  535. procedure TPpuRecordDef.BeforeWriteItems(Output: TPpuOutput);
  536. begin
  537. inherited BeforeWriteItems(Output);
  538. if ooCopied in Options then
  539. Ancestor.Write(Output, 'CopyFrom');
  540. end;
  541. constructor TPpuRecordDef.Create(AParent: TPpuContainerDef);
  542. begin
  543. inherited Create(AParent);
  544. DefType:=dtRecord;
  545. end;
  546. function TPpuRecordDef.CanWrite: boolean;
  547. begin
  548. Result:=True;
  549. end;
  550. { TPpuPropDef }
  551. procedure TPpuPropDef.BeforeWriteItems(Output: TPpuOutput);
  552. begin
  553. inherited BeforeWriteItems(Output);
  554. PropType.Write(Output, 'PropType');
  555. Getter.Write(Output, 'Getter');
  556. Setter.Write(Output, 'Setter');
  557. end;
  558. constructor TPpuPropDef.Create(AParent: TPpuContainerDef);
  559. begin
  560. inherited Create(AParent);
  561. DefType:=dtProp;
  562. ItemsName:='Params';
  563. PropType:=TPpuRef.Create;
  564. Getter:=TPpuRef.Create;
  565. Setter:=TPpuRef.Create;
  566. end;
  567. destructor TPpuPropDef.Destroy;
  568. begin
  569. Getter.Free;
  570. Setter.Free;
  571. PropType.Free;
  572. inherited Destroy;
  573. end;
  574. { TPpuTypeRef }
  575. procedure TPpuTypeRef.WriteDef(Output: TPpuOutput);
  576. begin
  577. inherited WriteDef(Output);
  578. Ref.Write(Output, 'Ref');
  579. end;
  580. constructor TPpuTypeRef.Create(AParent: TPpuContainerDef);
  581. begin
  582. inherited Create(AParent);
  583. DefType:=dtTypeRef;
  584. end;
  585. { TPpuFieldDef }
  586. constructor TPpuFieldDef.Create(AParent: TPpuContainerDef);
  587. begin
  588. inherited Create(AParent);
  589. DefType:=dtField;
  590. end;
  591. { TPpuParamDef }
  592. procedure TPpuParamDef.WriteDef(Output: TPpuOutput);
  593. var
  594. i, j: integer;
  595. d: TPpuDef;
  596. begin
  597. inherited WriteDef(Output);
  598. if Spez <> psValue then
  599. Output.WriteStr('Spez', ParamSpezNames[Spez]);
  600. if not DefaultValue.IsNull then begin
  601. j:=DefaultValue.Id;
  602. for i:=0 to Parent.Count - 1 do begin
  603. d:=Parent[i];
  604. if (d.DefType = dtConst) and (d.Id = j) then begin
  605. d.Visibility:=dvPublic;
  606. d.Name:='';
  607. d.Write(Output, 'Default');
  608. d.Visibility:=dvHidden;
  609. break;
  610. end;
  611. end;
  612. end;
  613. end;
  614. constructor TPpuParamDef.Create(AParent: TPpuContainerDef);
  615. begin
  616. inherited Create(AParent);
  617. DefType:=dtParam;
  618. Spez:=psValue;
  619. DefaultValue:=TPpuRef.Create;
  620. end;
  621. destructor TPpuParamDef.Destroy;
  622. begin
  623. DefaultValue.Free;
  624. inherited Destroy;
  625. end;
  626. function TPpuParamDef.CanWrite: boolean;
  627. begin
  628. Result:=inherited CanWrite and (Spez <> psHidden);
  629. end;
  630. { TPpuVarDef }
  631. procedure TPpuVarDef.WriteDef(Output: TPpuOutput);
  632. begin
  633. inherited WriteDef(Output);
  634. VarType.Write(Output, 'VarType');
  635. end;
  636. constructor TPpuVarDef.Create(AParent: TPpuContainerDef);
  637. begin
  638. inherited Create(AParent);
  639. DefType:=dtVar;
  640. VarType:=TPpuRef.Create;
  641. end;
  642. destructor TPpuVarDef.Destroy;
  643. begin
  644. VarType.Free;
  645. inherited Destroy;
  646. end;
  647. { TPpuObjectDef }
  648. procedure TPpuObjectDef.BeforeWriteItems(Output: TPpuOutput);
  649. var
  650. opt: TPpuObjOption;
  651. begin
  652. inherited BeforeWriteItems(Output);
  653. if ObjType <> otUnknown then begin
  654. Output.WriteStr('ObjType', ObjTypeNames[ObjType]);
  655. Ancestor.Write(Output, 'Ancestor');
  656. end;
  657. if Options <> [] then begin
  658. Output.WriteArrayStart('Options');
  659. for opt:=Low(opt) to High(opt) do
  660. if opt in Options then
  661. Output.WriteStr('', ObjOptionNames[opt]);
  662. Output.WriteArrayEnd;
  663. end;
  664. end;
  665. constructor TPpuObjectDef.Create(AParent: TPpuContainerDef);
  666. begin
  667. inherited Create(AParent);
  668. DefType:=dtObject;
  669. ItemsName:='Fields';
  670. ObjType:=otUnknown;
  671. Ancestor:=TPpuRef.Create;
  672. end;
  673. destructor TPpuObjectDef.Destroy;
  674. begin
  675. Ancestor.Free;
  676. inherited Destroy;
  677. end;
  678. function TPpuObjectDef.CanWrite: boolean;
  679. begin
  680. Result:=inherited CanWrite and (ObjType <> otUnknown);
  681. end;
  682. { TPpuRef }
  683. function TPpuRef.GetId: cardinal;
  684. begin
  685. if FId = InvalidId then
  686. Result:=InvalidId
  687. else
  688. Result:=FId and not SymIdBit;
  689. end;
  690. function TPpuRef.GetIsSymId: boolean;
  691. begin
  692. Result:=FId and SymIdBit <> 0;
  693. end;
  694. procedure TPpuRef.SetId(AValue: cardinal);
  695. begin
  696. if (FId = InvalidId) or (AValue = InvalidId) then
  697. FId:=AValue
  698. else
  699. FId:=AValue or (FId and SymIdBit);
  700. end;
  701. procedure TPpuRef.SetIsSymId(AValue: boolean);
  702. begin
  703. if AValue then
  704. FId:=FId or SymIdBit
  705. else
  706. FId:=FId and not SymIdBit;
  707. end;
  708. constructor TPpuRef.Create;
  709. begin
  710. UnitIndex:=InvalidUnit;
  711. FId:=InvalidId;
  712. end;
  713. procedure TPpuRef.Write(Output: TPpuOutput; const RefName: string);
  714. begin
  715. with Output do
  716. if IsNull then
  717. WriteNull(RefName)
  718. else begin
  719. WriteObjectStart(RefName);
  720. if not IsCurUnit then
  721. WriteInt('Unit', UnitIndex);
  722. if IsSymId then
  723. WriteInt('SymId', Id)
  724. else
  725. WriteInt('Id', Id);
  726. WriteObjectEnd;
  727. end;
  728. end;
  729. function TPpuRef.IsCurUnit: boolean;
  730. begin
  731. Result:=UnitIndex = InvalidUnit;
  732. end;
  733. function TPpuRef.IsNull: boolean;
  734. begin
  735. Result:=Id = InvalidId;
  736. end;
  737. { TPpuProcTypeDef }
  738. constructor TPpuProcTypeDef.Create(AParent: TPpuContainerDef);
  739. begin
  740. inherited Create(AParent);
  741. DefType:=dtProcType;
  742. end;
  743. { TPpuProcDef }
  744. procedure TPpuProcDef.BeforeWriteItems(Output: TPpuOutput);
  745. var
  746. opt: TPpuProcOption;
  747. begin
  748. inherited BeforeWriteItems(Output);
  749. if Options <> [] then begin
  750. Output.WriteArrayStart('Options');
  751. for opt:=Low(opt) to High(opt) do
  752. if opt in Options then
  753. Output.WriteStr('', ProcOptionNames[opt]);
  754. Output.WriteArrayEnd;
  755. end;
  756. if Options*[poProcedure, poDestructor] = [] then
  757. ReturnType.Write(Output, 'RetType');
  758. end;
  759. constructor TPpuProcDef.Create(AParent: TPpuContainerDef);
  760. begin
  761. inherited Create(AParent);
  762. DefType:=dtProc;
  763. ItemsName:='Params';
  764. ReturnType:=TPpuRef.Create;
  765. end;
  766. destructor TPpuProcDef.Destroy;
  767. begin
  768. ReturnType.Free;
  769. inherited Destroy;
  770. end;
  771. { TPpuSrcFile }
  772. procedure TPpuSrcFile.WriteDef(Output: TPpuOutput);
  773. begin
  774. inherited WriteDef(Output);
  775. Output.WriteStr('Time', FormatDateTime('yyyy"-"mm"-"dd hh":"nn":"ss', FileTime));
  776. end;
  777. { TPpuOutput }
  778. procedure TPpuOutput.SetIndent(AValue: integer);
  779. begin
  780. if FIndent=AValue then Exit;
  781. FIndent:=AValue;
  782. if FIndent < 0 then
  783. FIndent:=0;
  784. SetLength(FIndStr, FIndent*IndentSize);
  785. if FIndent > 0 then
  786. FillChar(FIndStr[1], FIndent*IndentSize, ' ');
  787. end;
  788. procedure TPpuOutput.SetIndentSize(AValue: integer);
  789. begin
  790. if FIndentSize=AValue then Exit;
  791. FIndentSize:=AValue;
  792. end;
  793. procedure TPpuOutput.WriteStr(const AName, AValue: string);
  794. begin
  795. end;
  796. procedure TPpuOutput.WriteInt(const AName: string; AValue: Int64; Signed: boolean);
  797. begin
  798. if Signed then
  799. WriteStr(AName, IntToStr(AValue))
  800. else
  801. WriteStr(AName, IntToStr(QWord(AValue)));
  802. end;
  803. procedure TPpuOutput.WriteFloat(const AName: string; AValue: extended);
  804. var
  805. s: string;
  806. begin
  807. Str(AValue, s);
  808. WriteStr(AName, s);
  809. end;
  810. procedure TPpuOutput.WriteBool(const AName: string; AValue: boolean);
  811. begin
  812. if AValue then
  813. WriteStr(AName, '1')
  814. else
  815. WriteStr(AName, '0');
  816. end;
  817. procedure TPpuOutput.WriteNull(const AName: string);
  818. begin
  819. WriteStr(AName, '');
  820. end;
  821. procedure TPpuOutput.WriteArrayStart(const AName: string);
  822. begin
  823. IncI;
  824. end;
  825. procedure TPpuOutput.WriteArrayEnd;
  826. begin
  827. DecI;
  828. end;
  829. procedure TPpuOutput.WriteObjectStart(const AName: string; Def: TPpuDef);
  830. begin
  831. IncI;
  832. if Def = nil then
  833. exit;
  834. if Def.DefType <> dtNone then
  835. WriteStr('Type', Def.DefTypeName);
  836. if Def.Name <> '' then
  837. WriteStr('Name', Def.Name);
  838. end;
  839. procedure TPpuOutput.WriteObjectEnd(Def: TPpuDef);
  840. begin
  841. DecI;
  842. end;
  843. constructor TPpuOutput.Create(var OutFile: Text);
  844. begin
  845. FOutFile:=@OutFile;
  846. FIndentSize:=2;
  847. end;
  848. destructor TPpuOutput.Destroy;
  849. begin
  850. inherited Destroy;
  851. end;
  852. procedure TPpuOutput.Write(const s: string);
  853. begin
  854. if not FNoIndent then
  855. System.Write(FOutFile^, FIndStr);
  856. System.Write(FOutFile^, s);
  857. FNoIndent:=True;
  858. end;
  859. procedure TPpuOutput.WriteLn(const s: string);
  860. begin
  861. Self.Write(s + LineEnding);
  862. FNoIndent:=False;
  863. end;
  864. procedure TPpuOutput.IncI;
  865. begin
  866. Indent:=Indent + 1;
  867. end;
  868. procedure TPpuOutput.DecI;
  869. begin
  870. Indent:=Indent - 1;
  871. end;
  872. { TPpuUnitDef }
  873. procedure TPpuUnitDef.WriteDef(Output: TPpuOutput);
  874. var
  875. i: integer;
  876. begin
  877. with Output do begin
  878. if Version <> 0 then
  879. WriteInt('Version', Version);
  880. if TargetCPU <> '' then
  881. WriteStr('TargetCPU', TargetCPU);
  882. if TargetOS <> '' then
  883. WriteStr('TargetOS', TargetOS);
  884. if Crc <> 0 then
  885. WriteStr('CRC', hexStr(Crc, 8));
  886. if IntfCrc <> 0 then
  887. WriteStr('InterfaceCRC', hexStr(IntfCrc, 8));
  888. UsedUnits.WriteDef(Output);
  889. if Length(RefUnits) > 0 then begin
  890. WriteArrayStart('Units');
  891. for i:=0 to High(RefUnits) do
  892. WriteStr('', RefUnits[i]);
  893. WriteArrayEnd;
  894. end;
  895. SourceFiles.WriteDef(Output);
  896. end;
  897. inherited WriteDef(Output);
  898. end;
  899. constructor TPpuUnitDef.Create(AParent: TPpuContainerDef);
  900. begin
  901. inherited Create(AParent);
  902. DefType:=dtUnit;
  903. ItemsName:='Interface';
  904. UsedUnits:=TPpuContainerDef.Create(nil);
  905. UsedUnits.FParent:=Self;
  906. UsedUnits.ItemsName:='Uses';
  907. SourceFiles:=TPpuContainerDef.Create(nil);
  908. SourceFiles.FParent:=Self;
  909. SourceFiles.ItemsName:='Files';
  910. FIndexById:=THashSet.Create(64, True, False);
  911. end;
  912. destructor TPpuUnitDef.Destroy;
  913. begin
  914. UsedUnits.Free;
  915. SourceFiles.Free;
  916. FIndexById.Free;
  917. inherited Destroy;
  918. end;
  919. function TPpuUnitDef.FindById(AId: integer; FindSym: boolean): TPpuDef;
  920. var
  921. h: PHashSetItem;
  922. i: cardinal;
  923. begin
  924. Result:=nil;
  925. if AId = -1 then
  926. exit;
  927. i:=AId;
  928. if FindSym then
  929. i:=i or SymIdBit;
  930. h:=FIndexById.Find(@i, SizeOf(i));
  931. if h <> nil then
  932. Result:=TPpuDef(h^.Data)
  933. else
  934. Result:=nil;
  935. end;
  936. { TPpuContainerDef }
  937. function TPpuContainerDef.GetCount: integer;
  938. begin
  939. Result:=FItems.Count;
  940. end;
  941. function TPpuContainerDef.GetItem(Index: Integer): TPpuDef;
  942. begin
  943. Result:=TPpuDef(FItems[Index]);
  944. end;
  945. procedure TPpuContainerDef.SetItem(Index: Integer; AValue: TPpuDef);
  946. begin
  947. FItems[Index]:=AValue;
  948. end;
  949. procedure TPpuContainerDef.WriteDef(Output: TPpuOutput);
  950. var
  951. i: integer;
  952. begin
  953. inherited WriteDef(Output);
  954. BeforeWriteItems(Output);
  955. if Count = 0 then
  956. exit;
  957. Output.WriteArrayStart(ItemsName);
  958. for i:=0 to Count - 1 do
  959. Items[i].Write(Output);
  960. Output.WriteArrayEnd;
  961. end;
  962. procedure TPpuContainerDef.BeforeWriteItems(Output: TPpuOutput);
  963. begin
  964. end;
  965. constructor TPpuContainerDef.Create(AParent: TPpuContainerDef);
  966. begin
  967. inherited Create(AParent);
  968. FItems:=TList.Create;
  969. ItemsName:='Contents';
  970. end;
  971. destructor TPpuContainerDef.Destroy;
  972. var
  973. i: integer;
  974. begin
  975. for i:=0 to FItems.Count - 1 do
  976. TObject(FItems[i]).Free;
  977. FItems.Free;
  978. inherited Destroy;
  979. end;
  980. function TPpuContainerDef.Add(Def: TPpuDef): integer;
  981. begin
  982. Result:=FItems.Add(Def);
  983. Def.FParent:=Self;
  984. end;
  985. { TPpuDef }
  986. function TPpuDef.GetDefTypeName: string;
  987. begin
  988. Result:=DefTypeNames[DefType];
  989. end;
  990. function TPpuDef.GetId: cardinal;
  991. begin
  992. if FId = InvalidId then
  993. Result:=InvalidId
  994. else
  995. Result:=FId and not SymIdBit;
  996. end;
  997. function TPpuDef.GetParentUnit: TPpuUnitDef;
  998. var
  999. d: TPpuContainerDef;
  1000. begin
  1001. if FParentUnit = nil then begin
  1002. d:=Parent;
  1003. while (d <> nil) and (d.DefType <> dtUnit) do
  1004. d:=d.Parent;
  1005. FParentUnit:=TPpuUnitDef(d);
  1006. end;
  1007. Result:=FParentUnit;
  1008. end;
  1009. procedure TPpuDef.SetId(AValue: cardinal);
  1010. var
  1011. h: PHashSetItem;
  1012. u: TPpuUnitDef;
  1013. begin
  1014. if FId = AValue then Exit;
  1015. u:=ParentUnit;
  1016. if (FId <> InvalidId) and (u <> nil) then begin
  1017. h:=u.FIndexById.Find(@FId, SizeOf(FId));
  1018. if h <> nil then
  1019. u.FIndexById.Remove(h);
  1020. end;
  1021. FId:=AValue;
  1022. if (FId <> InvalidId) and (u <> nil) then begin;
  1023. h:=u.FIndexById.FindOrAdd(@FId, SizeOf(FId));
  1024. h^.Data:=Self;
  1025. end;
  1026. end;
  1027. procedure TPpuDef.SetParent(AValue: TPpuContainerDef);
  1028. var
  1029. i: cardinal;
  1030. begin
  1031. if FParent=AValue then Exit;
  1032. if FParent <> nil then
  1033. raise Exception.Create('Parent can not be modified.');
  1034. AValue.Add(Self);
  1035. if FId <> InvalidId then begin
  1036. i:=FId;
  1037. FId:=InvalidId;
  1038. SetId(i);
  1039. end;
  1040. end;
  1041. procedure TPpuDef.SetSymId(AId: integer);
  1042. begin
  1043. Id:=cardinal(AId) or SymIdBit;
  1044. end;
  1045. procedure TPpuDef.WriteDef(Output: TPpuOutput);
  1046. begin
  1047. with Output do begin
  1048. if FId <> InvalidId then
  1049. if IsSymId(FId) then
  1050. WriteInt('SymId', Id)
  1051. else begin
  1052. WriteInt('Id', Id);
  1053. if not Ref.IsNull then
  1054. WriteInt('SymId', Ref.Id);
  1055. end;
  1056. if FilePos.Line > 0 then begin
  1057. WriteObjectStart('Pos');
  1058. if FilePos.FileIndex > 0 then
  1059. WriteInt('File', FilePos.FileIndex);
  1060. WriteInt('Line', FilePos.Line);
  1061. WriteInt('Col', FilePos.Col);
  1062. WriteObjectEnd;
  1063. end;
  1064. if Visibility <> dvPublic then
  1065. WriteStr('Visibility', DefVisibilityNames[Visibility]);
  1066. end;
  1067. end;
  1068. constructor TPpuDef.Create(AParent: TPpuContainerDef);
  1069. begin
  1070. FId:=InvalidId;
  1071. Ref:=TPpuRef.Create;
  1072. Visibility:=dvPublic;
  1073. if AParent <> nil then
  1074. AParent.Add(Self);
  1075. end;
  1076. destructor TPpuDef.Destroy;
  1077. begin
  1078. Ref.Free;
  1079. inherited Destroy;
  1080. end;
  1081. procedure TPpuDef.Write(Output: TPpuOutput; const AttrName: string);
  1082. begin
  1083. if not CanWrite then
  1084. exit;
  1085. if Parent <> nil then
  1086. Output.WriteObjectStart(AttrName, Self);
  1087. WriteDef(Output);
  1088. if Parent <> nil then
  1089. Output.WriteObjectEnd(Self);
  1090. end;
  1091. function TPpuDef.CanWrite: boolean;
  1092. begin
  1093. Result:=Visibility <> dvHidden;
  1094. end;
  1095. end.