ppuout.pp 33 KB

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