2
0

ppuout.pp 33 KB

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